blob: 3cbdfb206fbdc8a915478833016bd7864c84ad69 [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";
Allen Lu50e75472020-02-27 11:14:29 +0800192static const char *UsbAdbEnableCTS = " androidboot.adb_enable_cts=0";
tracychuid184b912020-06-05 17:31:38 +0800193#endif
194/*[20200605][TracyChui] Implement get Serial Number end*/
Shashank Mittalcd98d472011-08-02 14:29:24 -0700195static const char *auth_kernel = " androidboot.authorized_kernel=true";
Pavel Nedev5614d222013-06-17 18:01:02 +0300196static const char *secondary_gpt_enable = " gpt";
Monika Singh292b3e92018-03-17 22:40:23 +0530197#ifdef MDTP_SUPPORT
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200198static const char *mdtp_activated_flag = " mdtp";
Monika Singh292b3e92018-03-17 22:40:23 +0530199#endif
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800200static const char *baseband_apq = " androidboot.baseband=apq";
201static const char *baseband_msm = " androidboot.baseband=msm";
202static const char *baseband_csfb = " androidboot.baseband=csfb";
203static const char *baseband_svlte2a = " androidboot.baseband=svlte2a";
Ajay Dudani403bc492011-09-30 16:17:21 -0700204static const char *baseband_mdm = " androidboot.baseband=mdm";
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800205static const char *baseband_mdm2 = " androidboot.baseband=mdm2";
Amol Jadi5c61a952012-05-04 17:05:35 -0700206static const char *baseband_sglte = " androidboot.baseband=sglte";
Amol Jadi2a15a272013-01-22 12:03:36 -0800207static const char *baseband_dsda = " androidboot.baseband=dsda";
208static const char *baseband_dsda2 = " androidboot.baseband=dsda2";
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800209static const char *baseband_sglte2 = " androidboot.baseband=sglte2";
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800210static const char *warmboot_cmdline = " qpnp-power-on.warm_boot=1";
Vijay Kumar Pendotib228cfc2016-06-13 20:15:23 +0530211static const char *baseband_apq_nowgr = " androidboot.baseband=baseband_apq_nowgr";
Mayank Grover351a75e2017-05-30 20:06:08 +0530212static const char *androidboot_slot_suffix = " androidboot.slot_suffix=";
213static const char *skip_ramfs = " skip_initramfs";
tracychui3b4abb72020-06-15 10:50:49 +0800214/* [TracyChui]Add memory_config property 20200615 start */
215static const char *memory_config_3G = " androidboot.memory_config=32GB,3GB";
216static const char *memory_config_4G = " androidboot.memory_config=64GB,4GB";
217/* [TracyChui]Add memory_config property 20200615 end */
Mayank Grover466d6562018-05-10 14:52:20 +0530218
219#if HIBERNATION_SUPPORT
220static const char *resume = " resume=/dev/mmcblk0p";
221#endif
222
Sourabh Banerjee6c7153c2018-03-20 01:21:57 +0530223#ifdef INIT_BIN_LE
224static const char *sys_path_cmdline = " rootwait ro init="INIT_BIN_LE;
225#else
Mayank Grover3804be72017-06-22 11:59:23 +0530226static const char *sys_path_cmdline = " rootwait ro init=/init";
Sourabh Banerjee6c7153c2018-03-20 01:21:57 +0530227#endif
Sourabh Banerjee386b1322018-02-27 09:37:28 +0530228
229#if VERITY_LE
230static const char *verity_dev = " root=/dev/dm-0";
231static const char *verity_system_part = " dm=\"system";
232static const char *verity_params = " none ro,0 1 android-verity /dev/mmcblk0p";
233#else
Mayank Grover5384ed82018-05-09 12:09:24 +0530234static const char *sys_path = " root=/dev/mmcblk0p";
lijuang83ef4b22018-08-23 11:01:55 +0800235
236#define MAX_DTBO_IDX_STR 64
237static const char *android_boot_dtbo_idx = " androidboot.dtbo_idx=";
Parth Dixit1375d9e2019-07-08 20:56:13 +0530238
239#define MAX_DTB_IDX_STR MAX_DTBO_IDX_STR
240static const char *android_boot_dtb_idx = " androidboot.dtb_idx=";
Sourabh Banerjee386b1322018-02-27 09:37:28 +0530241#endif
Ajay Dudanid04110c2011-01-17 23:55:07 -0800242
michaellincb058f12020-04-24 15:05:10 +0800243//<20200424-michaellin, Add PCBA stage to system properties
244#include <platform/gpio.h>
245static const char *PCBA_STAGE_0 = " androidboot.pcbastage=EP0";
246static const char *PCBA_STAGE_1 = " androidboot.pcbastage=EP1";
247static const char *PCBA_STAGE_2 = " androidboot.pcbastage=EP2";
248static const char *PCBA_STAGE_3 = " androidboot.pcbastage=FP";
249static const char *PCBA_STAGE_4 = " androidboot.pcbastage=MP";
michaellin52b647a2020-05-05 14:58:47 +0800250static const char *PCBA_STAGE_5 = " androidboot.pcbastage=MP-8903MB_001";
michaellincb058f12020-04-24 15:05:10 +0800251static const char *PCBA_STAGE_F = " androidboot.pcbastage=Reserved";
252//>20200424-michaellin
253
tedwu15b28c02020-07-03 18:02:48 +0800254//<2020/07/02-tedwu, Add RF variants in system properties
255#define RF_VARIANT_PIN_0 89
256#define RF_VARIANT_PIN_1 141
257
258static const char *rfvar_prefix = " androidboot.rfvariant=";
259static const char *rfvar_0 = "B13";
260static const char *rfvar_1 = "B28B";
261static const char *rfvar_2 = "B28A";
262static const char *rfvar_3 = "03";
263//>2020/07/02-tedwu, Add RF variants in system properties
264
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700265#if VERIFIED_BOOT
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700266static const char *verity_mode = " androidboot.veritymode=";
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700267static const char *verified_state= " androidboot.verifiedbootstate=";
Parth Dixita5715a02015-10-29 12:25:10 +0530268static const char *keymaster_v1= " androidboot.keymaster=1";
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700269//indexed based on enum values, green is 0 by default
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700270
271struct verified_boot_verity_mode vbvm[] =
272{
Channagoud Kadabi86b0c112016-03-16 19:23:16 -0700273#if ENABLE_VB_ATTEST
274 {false, "eio"},
275#else
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700276 {false, "logging"},
Channagoud Kadabi86b0c112016-03-16 19:23:16 -0700277#endif
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700278 {true, "enforcing"},
279};
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700280struct verified_boot_state_name vbsn[] =
281{
282 {GREEN, "green"},
283 {ORANGE, "orange"},
284 {YELLOW,"yellow"},
285 {RED,"red" },
286};
287#endif
Channagoud Kadabi86b0c112016-03-16 19:23:16 -0700288/*As per spec delay wait time before shutdown in Red state*/
289#define DELAY_WAIT 30000
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700290static unsigned page_size = 0;
291static unsigned page_mask = 0;
Gaurav Nebhwani4d2389c2016-03-17 21:10:05 +0530292static unsigned mmc_blocksize = 0;
293static unsigned mmc_blocksize_mask = 0;
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700294static char ffbm_mode_string[FFBM_MODE_BUF_SIZE];
295static bool boot_into_ffbm;
vijay kumar870515d2015-08-31 16:37:24 +0530296static char *target_boot_params = NULL;
Matthew Qind886f3c2014-01-17 16:52:01 +0800297static bool boot_reason_alarm;
Channagoud Kadabi80a182b2015-03-11 17:04:23 -0700298static bool devinfo_present = true;
tracychuid184b912020-06-05 17:31:38 +0800299/*[20200605][TracyChui] Implement get Serial Number start */
300#if defined(ENABLE_PRODINFO_ACCESS)
301static bool prodinfo_present = true;
302#endif
303/*[20200605][TracyChui] Implement get Serial Number end*/
Channagoud Kadabi736c4962015-08-21 11:56:52 -0700304bool boot_into_fastboot = false;
Parth Dixit4097b622016-03-15 14:42:27 +0530305static uint32_t dt_size = 0;
Monika Singh292b3e92018-03-17 22:40:23 +0530306static char *vbcmdline;
307static bootinfo info = {0};
Mayank Grovere1ab96c2018-09-04 20:31:31 +0530308static void *recovery_dtbo_buf = NULL;
309static uint32_t recovery_dtbo_size = 0;
310
Shashank Mittalcd98d472011-08-02 14:29:24 -0700311/* Assuming unauthorized kernel image by default */
312static int auth_kernel_img = 0;
Monika Singh292b3e92018-03-17 22:40:23 +0530313static device_info device = {DEVICE_MAGIC,0,0,0,0,{0},{0},{0},1,{0},0,{0}};
tracychuid184b912020-06-05 17:31:38 +0800314/*[20200605][TracyChui] Implement get Serial Number start*/
315#if defined(ENABLE_PRODINFO_ACCESS)
Allen Lu50e75472020-02-27 11:14:29 +0800316static prod_info prod = {PRODINFO_MAGIC, {0}, {0}, 0, 0};
tracychuid184b912020-06-05 17:31:38 +0800317#endif
318/*[20200605][TracyChui] Implement get Serial Number end*/
Monika Singh292b3e92018-03-17 22:40:23 +0530319
jhchen7a504d12020-04-24 15:45:57 +0800320/*[Arima_8910][jhchen] add fuse check property 20181031 begin*/
321#ifdef ENABLE_FUSE_CHECK
322static const char *fuse_blown = " androidboot.oem.color=red";
323static const char *fuse_not_blown = " androidboot.oem.color=brown";
324#endif
325/*[Arima_8910][jhchen] 20181031 end*/
326
327/*[Arima_8910][jhchen] add fuse check property 20181031 begin*/
328#ifdef ENABLE_FUSE_CHECK
329 int is_fused = 0;
330#endif
331/*[Arima_8910][jhchen] 20181031 end*/
332
vijay kumarc65876c2015-04-24 13:29:16 +0530333static bool is_allow_unlock = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -0700334
vijay kumarca2e6812015-07-08 20:28:25 +0530335static char frp_ptns[2][8] = {"config","frp"};
336
lijuang511a2b52015-08-14 20:50:51 +0800337static const char *critical_flash_allowed_ptn[] = {
338 "aboot",
339 "rpm",
340 "tz",
341 "sbl",
342 "sdi",
343 "sbl1",
344 "xbl",
345 "hyp",
346 "pmic",
347 "bootloader",
348 "devinfo",
349 "partition"};
350
Dima Zavin42168f22009-01-30 11:52:22 -0800351struct atag_ptbl_entry
352{
353 char name[16];
354 unsigned offset;
355 unsigned size;
356 unsigned flags;
357};
358
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700359/*
360 * Partition info, required to be published
361 * for fastboot
362 */
363struct getvar_partition_info {
Mayank Grover7b8a8f82017-10-27 13:07:59 +0530364 char part_name[MAX_GPT_NAME_SIZE]; /* Partition name */
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700365 char getvar_size[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for size */
366 char getvar_type[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for type */
367 char size_response[MAX_RSP_SIZE]; /* fastboot response for size */
368 char type_response[MAX_RSP_SIZE]; /* fastboot response for type */
369};
370
371/*
Mayank Grover7b8a8f82017-10-27 13:07:59 +0530372 * Update the part_type_known for known paritions types.
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700373 */
Mayank Groverd38fe012018-03-13 15:33:16 +0530374#define RAW_STR "raw"
Mayank Grover52cd10a2018-03-15 12:57:54 +0530375#define EXT_STR "ext4"
376#define F2FS_STR "f2fs"
377
378#define FS_SUPERBLOCK_OFFSET 0x400
379#define EXT_MAGIC 0xEF53
380#define EXT_MAGIC_OFFSET_SB 0x38
381#define F2FS_MAGIC 0xF2F52010 // F2FS Magic Number
382#define F2FS_MAGIC_OFFSET_SB 0x0
383
384typedef enum fs_signature_type {
385 EXT_FS_SIGNATURE = 1,
386 EXT_F2FS_SIGNATURE = 2,
387 NO_FS = -1
388} fs_signature_type;
389
Mayank Grover7b8a8f82017-10-27 13:07:59 +0530390struct getvar_partition_info part_info[NUM_PARTITIONS];
391struct getvar_partition_info part_type_known[] =
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700392{
Mayank Grover49920212018-02-09 18:15:55 +0530393 { "system" , "partition-size:", "partition-type:", "", "ext4" },
394 { "userdata" , "partition-size:", "partition-type:", "", "ext4" },
395 { "cache" , "partition-size:", "partition-type:", "", "ext4" },
396 { "recoveryfs" , "partition-size:", "partition-type:", "", "ext4" },
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700397};
398
399char max_download_size[MAX_RSP_SIZE];
Ameya Thakur11cf1a62013-08-05 12:44:48 -0700400char charger_screen_enabled[MAX_RSP_SIZE];
tracychuid184b912020-06-05 17:31:38 +0800401/*[20200605][TracyChui] Implement get Serial Number start*/
402#if defined(ENABLE_PRODINFO_ACCESS)
403char cust_sn_buf[PRODINFO_MAX_SSN_LEN + 1];
404char factory_sn_buf[PRODINFO_MAX_SSN_LEN + 1];
405char AdbEnable[MAX_RSP_SIZE];
Allen Lu50e75472020-02-27 11:14:29 +0800406char AdbEnableCTS[MAX_RSP_SIZE];
tracychuid184b912020-06-05 17:31:38 +0800407#endif
408#if defined(ENABLE_PRODINFO_ACCESS)
409char sn_buf[PRODINFO_MAX_ISN_LEN + 1];
410#else
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800411char sn_buf[13];
tracychuid184b912020-06-05 17:31:38 +0800412#endif
413/*[20200605][TracyChui] Implement get Serial Number end*/
Dhaval Patel223ec952013-07-18 14:49:44 -0700414char display_panel_buf[MAX_PANEL_BUF_SIZE];
Unnati Gandhi62c8ab82014-01-24 11:01:01 +0530415char panel_display_mode[MAX_RSP_SIZE];
Mayank Grovera64dfbe2018-05-17 14:06:34 +0530416char soc_version_str[MAX_RSP_SIZE];
Mayank Grover7b8a8f82017-10-27 13:07:59 +0530417char block_size_string[MAX_RSP_SIZE];
Mukesh Ojhabb6e7f32018-03-02 15:04:06 +0530418#if PRODUCT_IOT
Mayank Grover7b8a8f82017-10-27 13:07:59 +0530419
420/* For IOT we are using custom version */
421#define PRODUCT_IOT_VERSION "IOT001"
422char bootloader_version_string[MAX_RSP_SIZE];
423#endif
lijuang102dfa92015-10-09 18:31:03 +0800424
425#if CHECK_BAT_VOLTAGE
lijuang65c5a822015-08-29 16:35:36 +0800426char battery_voltage[MAX_RSP_SIZE];
lijuang102dfa92015-10-09 18:31:03 +0800427char battery_soc_ok [MAX_RSP_SIZE];
428#endif
429
lijuangf16461c2015-08-03 17:09:34 +0800430char get_variant[MAX_RSP_SIZE];
Greg Griscod6250552011-06-29 14:40:23 -0700431
Greg Griscod2471ef2011-07-14 13:00:42 -0700432extern int emmc_recovery_init(void);
433
Kinson Chik0b1c8162011-08-31 16:31:57 -0700434#if NO_KEYPAD_DRIVER
435extern int fastboot_trigger(void);
436#endif
Greg Griscod2471ef2011-07-14 13:00:42 -0700437
Mayank Grovere1ab96c2018-09-04 20:31:31 +0530438static void update_ker_tags_rdisk_addr(boot_img_hdr *hdr, bool is_arm64)
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700439{
440 /* overwrite the destination of specified for the project */
Channagoud Kadabi7042fa32013-04-26 16:44:14 -0700441#ifdef ABOOT_IGNORE_BOOT_HEADER_ADDRS
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800442 if (is_arm64)
443 hdr->kernel_addr = ABOOT_FORCE_KERNEL64_ADDR;
444 else
445 hdr->kernel_addr = ABOOT_FORCE_KERNEL_ADDR;
Channagoud Kadabi7042fa32013-04-26 16:44:14 -0700446 hdr->ramdisk_addr = ABOOT_FORCE_RAMDISK_ADDR;
447 hdr->tags_addr = ABOOT_FORCE_TAGS_ADDR;
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700448#endif
449}
450
Dima Zavin42168f22009-01-30 11:52:22 -0800451static void ptentry_to_tag(unsigned **ptr, struct ptentry *ptn)
452{
453 struct atag_ptbl_entry atag_ptn;
454
455 memcpy(atag_ptn.name, ptn->name, 16);
456 atag_ptn.name[15] = '\0';
457 atag_ptn.offset = ptn->start;
458 atag_ptn.size = ptn->length;
459 atag_ptn.flags = ptn->flags;
460 memcpy(*ptr, &atag_ptn, sizeof(struct atag_ptbl_entry));
461 *ptr += sizeof(struct atag_ptbl_entry) / sizeof(unsigned);
462}
Mayank Grover9df84c02018-08-30 15:46:35 +0530463#ifdef VERIFIED_BOOT_2
464void load_vbmeta_image(void **vbmeta_image_buf, uint32_t *vbmeta_image_sz)
465{
466 int index = 0;
467 char *vbm_img_buf = NULL;
468 unsigned long long ptn = 0;
469 unsigned long long ptn_size = 0;
470
471 /* Immediately return if dtbo is not supported */
472 index = partition_get_index("vbmeta");
473 ptn = partition_get_offset(index);
474 if(!ptn)
475 {
476 dprintf(CRITICAL, "ERROR: vbmeta partition not found.\n");
477 return;
478 }
479
480 ptn_size = partition_get_size(index);
481 if (ptn_size > MAX_SUPPORTED_VBMETA_IMG_BUF)
482 {
483 dprintf(CRITICAL, "ERROR: vbmeta parition size is greater than supported.\n");
484 return;
485 }
486
487 vbm_img_buf = (char *)memalign(CACHE_LINE, ROUNDUP((uint32_t)ptn_size, CACHE_LINE));
488 if (!vbm_img_buf)
489 {
490 dprintf(CRITICAL, "ERROR: vbmeta unable to locate buffer\n");
491 return;
492 }
493
494 mmc_set_lun(partition_get_lun(index));
495 if (mmc_read(ptn, (uint32_t *)vbm_img_buf, (uint32_t)ptn_size))
496 {
497 dprintf(CRITICAL, "ERROR: vbmeta read failure\n");
498 free(vbm_img_buf);
499 return;
500 }
501
502 *vbmeta_image_buf = vbm_img_buf;
503 *vbmeta_image_sz = (uint32_t)ptn_size;
504 return;
505}
506#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -0800507
lijuang102dfa92015-10-09 18:31:03 +0800508#if CHECK_BAT_VOLTAGE
509void update_battery_status(void)
510{
511 snprintf(battery_voltage,MAX_RSP_SIZE, "%d",target_get_battery_voltage());
512 snprintf(battery_soc_ok ,MAX_RSP_SIZE, "%s",target_battery_soc_ok()? "yes":"no");
513}
514#endif
515
michaellincb058f12020-04-24 15:05:10 +0800516//<20200424-michaellin, Add PCBA stage to system properties
517uint32_t GetPcbaVariant(void)
518{
519 uint8_t GPIO_97=10;
520 uint8_t GPIO_98=10;
521 uint8_t GPIO_99=10;
522
523 uint8_t pcba_stage = 0;
524
525 // Config GPIO
526 gpio_tlmm_config(97, 0, GPIO_INPUT, GPIO_NO_PULL, GPIO_2MA, GPIO_ENABLE);
527 gpio_tlmm_config(98, 0, GPIO_INPUT, GPIO_NO_PULL, GPIO_2MA, GPIO_ENABLE);
528 gpio_tlmm_config(99, 0, GPIO_INPUT, GPIO_NO_PULL, GPIO_2MA, GPIO_ENABLE);
529
530 /* Wait for the gpio config to take effect - debounce time */
531 thread_sleep(10);
532
533 /* Get status of GPIO */
534 GPIO_97 = gpio_status(97);
535 GPIO_98 = gpio_status(98);
536 GPIO_99 = gpio_status(99);
537
538 pcba_stage = (GPIO_99 << 2) + (GPIO_98 << 1) + GPIO_97;
539 dprintf(ALWAYS, "pcba_stage status: %u\n", pcba_stage);
michaellin52b647a2020-05-05 14:58:47 +0800540 // GPIO_99 GPIO_98 GPIO_97
michaellincb058f12020-04-24 15:05:10 +0800541 // 0 0 0 EP0
542 // 0 0 1 EP1
543 // 0 1 0 EP2
michaellin52b647a2020-05-05 14:58:47 +0800544 // 0 1 1 FP(8901MB-007)/PP
545 // 1 0 0 MP(8901MB-008)
546 // 1 0 1 MP-8903MB_001
michaellincb058f12020-04-24 15:05:10 +0800547 return pcba_stage;
548}
549//>20200424-michaellin
550
tedwu15b28c02020-07-03 18:02:48 +0800551//<2020/07/02-tedwu, Add RF variant in system properties
552uint32_t get_rf_variant(void)
553{
554 static uint32_t rf_id = 0xFF;
555
556 if (0xFF == rf_id) {
557 rf_id = (gpio_status(RF_VARIANT_PIN_1) << 1) + gpio_status(RF_VARIANT_PIN_0);
558 }
559 dprintf(CRITICAL, "RF Variant ID: %d\n", rf_id);
560
561 return rf_id;
562}
563//>2020/07/02-tedwu, Add RF variant in system properties
564
Neeti Desaie245d492012-06-01 12:52:13 -0700565unsigned char *update_cmdline(const char * cmdline)
Brian Swetland9c4c0752009-01-25 16:23:50 -0800566{
David Ng183a7422009-12-07 14:55:21 -0800567 int cmdline_len = 0;
568 int have_cmdline = 0;
Amol Jadi168b7712012-03-06 16:15:00 -0800569 unsigned char *cmdline_final = NULL;
Neeti Desaie245d492012-06-01 12:52:13 -0700570 int pause_at_bootup = 0;
tracychuid184b912020-06-05 17:31:38 +0800571/*[20200605][TracyChui] Implement get Serial Number start*/
572#if defined(ENABLE_PRODINFO_ACCESS)
573 int AdbEnable = 0;
Allen Lu50e75472020-02-27 11:14:29 +0800574 int AdbEnableCTS = 0;
tracychuid184b912020-06-05 17:31:38 +0800575#endif
576/*[20200605][TracyChui] Implement get Serial Number end*/
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800577 bool warm_boot = false;
Pavel Nedev5614d222013-06-17 18:01:02 +0300578 bool gpt_exists = partition_gpt_exists();
Joonwoo Park61112782013-10-02 19:50:39 -0700579 int have_target_boot_params = 0;
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700580 char *boot_dev_buf = NULL;
Monika Singh292b3e92018-03-17 22:40:23 +0530581#ifdef MDTP_SUPPORT
Mayank Grover351a75e2017-05-30 20:06:08 +0530582 bool is_mdtp_activated = 0;
Monika Singh292b3e92018-03-17 22:40:23 +0530583#endif
Mayank Grover351a75e2017-05-30 20:06:08 +0530584 int current_active_slot = INVALID;
Mayank Grover3804be72017-06-22 11:59:23 +0530585 int system_ptn_index = -1;
586 unsigned int lun = 0;
587 char lun_char_base = 'a';
Sourabh Banerjee386b1322018-02-27 09:37:28 +0530588#if VERITY_LE
589 int syspath_buflen = strlen(verity_dev)
590 + strlen(verity_system_part) + (sizeof(char) * 2) + 2
591 + strlen(verity_params) + sizeof(int) + 2;
592#else
lijuang83ef4b22018-08-23 11:01:55 +0800593 int syspath_buflen = strlen(sys_path) + sizeof(int) + 2; /*allocate buflen for largest possible string*/
594 char dtbo_idx_str[MAX_DTBO_IDX_STR] = "\0";
595 int dtbo_idx = INVALID_PTN;
Parth Dixit1375d9e2019-07-08 20:56:13 +0530596 char dtb_idx_str[MAX_DTB_IDX_STR] = "\0";
597 int dtb_idx = INVALID_PTN;
Sourabh Banerjee386b1322018-02-27 09:37:28 +0530598#endif
Mayank Grover3804be72017-06-22 11:59:23 +0530599 char syspath_buf[syspath_buflen];
Mayank Grover466d6562018-05-10 14:52:20 +0530600#if HIBERNATION_SUPPORT
601 int resume_buflen = strlen(resume) + sizeof(int) + 2;
602 char resume_buf[resume_buflen];
603 int swap_ptn_index = INVALID_PTN;
604#endif
605
Mayank Grover889be1b2017-09-12 20:12:23 +0530606#if VERIFIED_BOOT
607 uint32_t boot_state = RED;
608#endif
Hareesh Gauthamabf1e1e2017-04-12 18:09:24 +0530609
tedwu15b28c02020-07-03 18:02:48 +0800610 //<2020/07/02-tedwu, Add RF variant in system properties
611 const char *pRFvar[4] = {rfvar_0, rfvar_1, rfvar_2, rfvar_3};
612 //>2020/07/02-tedwu, Add RF variant in system properties
613
Hareesh Gauthamabf1e1e2017-04-12 18:09:24 +0530614#if USE_LE_SYSTEMD
615 is_systemd_present=true;
616#endif
617
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700618#if VERIFIED_BOOT
Mayank Grover912eaa62017-10-26 12:08:53 +0530619 if (VB_M <= target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +0530620 {
621 boot_state = boot_verify_get_state();
622 }
Parth Dixitddbc7352015-10-18 03:13:31 +0530623#endif
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700624
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200625#ifdef MDTP_SUPPORT
626 mdtp_activated(&is_mdtp_activated);
627#endif /* MDTP_SUPPORT */
Dima Zavin42168f22009-01-30 11:52:22 -0800628
Brian Swetland9c4c0752009-01-25 16:23:50 -0800629 if (cmdline && cmdline[0]) {
David Ng183a7422009-12-07 14:55:21 -0800630 cmdline_len = strlen(cmdline);
631 have_cmdline = 1;
632 }
633 if (target_is_emmc_boot()) {
634 cmdline_len += strlen(emmc_cmdline);
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700635 boot_dev_buf = (char *) malloc(sizeof(char) * BOOT_DEV_MAX_LEN);
Mayank Groverb716edf2019-05-08 16:06:55 +0530636 if (!boot_dev_buf) {
637 dprintf(CRITICAL, "ERROR: Failed to allocate boot_dev_buf\n");
638 } else {
639 platform_boot_dev_cmdline(boot_dev_buf);
640#if USE_BOOTDEV_CMDLINE
641 cmdline_len += strlen(boot_dev_buf);
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700642#endif
Mayank Groverb716edf2019-05-08 16:06:55 +0530643 if (target_dynamic_partition_supported()) {
644 cmdline_len += strlen(dynamic_bootdev_cmdline);
645 cmdline_len += strlen(boot_dev_buf);
646 }
647 }
David Ng183a7422009-12-07 14:55:21 -0800648 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800649
650 cmdline_len += strlen(usb_sn_cmdline);
651 cmdline_len += strlen(sn_buf);
tracychuid184b912020-06-05 17:31:38 +0800652/*[20200605][TracyChui] Implement get Serial Number start */
653#if defined(ENABLE_PRODINFO_ACCESS)
654 cmdline_len += strlen(cust_sn_cmdline);
655 cmdline_len += strlen(cust_sn_buf);
656 cmdline_len += strlen(factory_sn_cmdline);
657 cmdline_len += strlen(factory_sn_buf);
658#endif
659/*[20200605][TracyChui] Implement get Serial Number end */
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800660
tracychui3b4abb72020-06-15 10:50:49 +0800661
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700662#if VERIFIED_BOOT
Mayank Grover912eaa62017-10-26 12:08:53 +0530663 if (VB_M <= target_get_vb_version())
Sridhar Parasuram7d8bb9a2015-09-23 18:21:19 -0700664 {
Mayank Grover889be1b2017-09-12 20:12:23 +0530665 cmdline_len += strlen(verified_state) + strlen(vbsn[boot_state].name);
666 if ((device.verity_mode != 0 ) && (device.verity_mode != 1))
667 {
668 dprintf(CRITICAL, "Devinfo paritition possibly corrupted!!!. Please erase devinfo partition to continue booting\n");
669 ASSERT(0);
670 }
671 cmdline_len += strlen(verity_mode) + strlen(vbvm[device.verity_mode].name);
672 cmdline_len += strlen(keymaster_v1);
Sridhar Parasuram7d8bb9a2015-09-23 18:21:19 -0700673 }
Parth Dixitddbc7352015-10-18 03:13:31 +0530674#endif
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700675
Monika Singh292b3e92018-03-17 22:40:23 +0530676
677 if (vbcmdline != NULL) {
678 dprintf(DEBUG, "UpdateCmdLine vbcmdline present len %d\n",
679 strlen(vbcmdline));
680 cmdline_len += strlen(vbcmdline);
681 }
682
Pavel Nedev5614d222013-06-17 18:01:02 +0300683 if (boot_into_recovery && gpt_exists)
684 cmdline_len += strlen(secondary_gpt_enable);
685
Monika Singh292b3e92018-03-17 22:40:23 +0530686#ifdef MDTP_SUPPORT
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200687 if(is_mdtp_activated)
688 cmdline_len += strlen(mdtp_activated_flag);
Monika Singh292b3e92018-03-17 22:40:23 +0530689#endif
Pavel Nedev328ac822013-04-05 15:25:11 +0300690 if (boot_into_ffbm) {
691 cmdline_len += strlen(androidboot_mode);
Hareesh Gauthamabf1e1e2017-04-12 18:09:24 +0530692
693 if(is_systemd_present)
694 cmdline_len += strlen(systemd_ffbm_mode);
695
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700696 cmdline_len += strlen(ffbm_mode_string);
Pavel Nedev898298c2013-02-27 12:36:09 -0800697 /* reduce kernel console messages to speed-up boot */
698 cmdline_len += strlen(loglevel);
Matthew Qind886f3c2014-01-17 16:52:01 +0800699 } else if (boot_reason_alarm) {
700 cmdline_len += strlen(alarmboot_cmdline);
Zhenhua Huang431dafa2015-06-30 16:13:37 +0800701 } else if ((target_build_variant_user() || device.charger_screen_enabled)
lijuang266b17d2018-08-17 18:53:42 +0800702 && target_pause_for_battery_charge() && !boot_into_recovery) {
David Ngf773dde2010-07-26 19:55:08 -0700703 pause_at_bootup = 1;
704 cmdline_len += strlen(battchg_pause);
705 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800706
tracychuid184b912020-06-05 17:31:38 +0800707/*[20200605][TracyChui] Implement get Serial Number start*/
708#if defined(ENABLE_PRODINFO_ACCESS)
709 if (prod.is_adb_enabled) {
710 dprintf(CRITICAL, "Device will enable adb\n");
711
Allen Lu50e75472020-02-27 11:14:29 +0800712 if (prod.is_adb_enabled_cts) {
713 prod.is_adb_enabled = 1;
714 } else {
715 prod.is_adb_enabled = 0;
716 }
tracychuid184b912020-06-05 17:31:38 +0800717 write_prod_info(&prod);
718
719 AdbEnable = 1;
720 cmdline_len += strlen(UsbAdbEnable);
Allen Lu50e75472020-02-27 11:14:29 +0800721 cmdline_len += strlen(UsbAdbEnableCTS);
tracychuid184b912020-06-05 17:31:38 +0800722 }
723#endif
724/*[20200605][TracyChui] Implement get Serial Number end*/
725
Shashank Mittalcd98d472011-08-02 14:29:24 -0700726 if(target_use_signed_kernel() && auth_kernel_img) {
727 cmdline_len += strlen(auth_kernel);
728 }
729
Joonwoo Park61112782013-10-02 19:50:39 -0700730 if (get_target_boot_params(cmdline, boot_into_recovery ? "recoveryfs" :
731 "system",
vijay kumar870515d2015-08-31 16:37:24 +0530732 &target_boot_params) == 0) {
Joonwoo Park61112782013-10-02 19:50:39 -0700733 have_target_boot_params = 1;
734 cmdline_len += strlen(target_boot_params);
735 }
736
Ajay Dudanid04110c2011-01-17 23:55:07 -0800737 /* Determine correct androidboot.baseband to use */
738 switch(target_baseband())
739 {
740 case BASEBAND_APQ:
741 cmdline_len += strlen(baseband_apq);
742 break;
743
744 case BASEBAND_MSM:
745 cmdline_len += strlen(baseband_msm);
746 break;
747
748 case BASEBAND_CSFB:
749 cmdline_len += strlen(baseband_csfb);
750 break;
751
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800752 case BASEBAND_SVLTE2A:
753 cmdline_len += strlen(baseband_svlte2a);
Ajay Dudanid04110c2011-01-17 23:55:07 -0800754 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700755
756 case BASEBAND_MDM:
757 cmdline_len += strlen(baseband_mdm);
758 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700759
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800760 case BASEBAND_MDM2:
761 cmdline_len += strlen(baseband_mdm2);
762 break;
763
Amol Jadi5c61a952012-05-04 17:05:35 -0700764 case BASEBAND_SGLTE:
765 cmdline_len += strlen(baseband_sglte);
766 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530767
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800768 case BASEBAND_SGLTE2:
769 cmdline_len += strlen(baseband_sglte2);
770 break;
771
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530772 case BASEBAND_DSDA:
773 cmdline_len += strlen(baseband_dsda);
774 break;
Amol Jadi2a15a272013-01-22 12:03:36 -0800775
776 case BASEBAND_DSDA2:
777 cmdline_len += strlen(baseband_dsda2);
778 break;
Vijay Kumar Pendotib228cfc2016-06-13 20:15:23 +0530779 case BASEBAND_APQ_NOWGR:
780 cmdline_len += strlen(baseband_apq_nowgr);
781 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800782 }
783
michaellincb058f12020-04-24 15:05:10 +0800784 //<20200424-michaellin, Add PCBA stage to system properties
785 switch(GetPcbaVariant())
786 {
787 case 0: // EP0
788 cmdline_len += strlen(PCBA_STAGE_0);
789 break;
790
791 case 1: // EP1
792 cmdline_len += strlen(PCBA_STAGE_1);
793 break;
794
795 case 2: // EP2
796 cmdline_len += strlen(PCBA_STAGE_2);
797 break;
798
michaellin52b647a2020-05-05 14:58:47 +0800799 case 3: // FP(8901MB-007)/PP
michaellincb058f12020-04-24 15:05:10 +0800800 cmdline_len += strlen(PCBA_STAGE_3);
801 break;
802
michaellin52b647a2020-05-05 14:58:47 +0800803 case 4: // MP(8901MB-008)
michaellincb058f12020-04-24 15:05:10 +0800804 cmdline_len += strlen(PCBA_STAGE_4);
tracychui3b4abb72020-06-15 10:50:49 +0800805 break;
michaellincb058f12020-04-24 15:05:10 +0800806
michaellin52b647a2020-05-05 14:58:47 +0800807 case 5: // MP-8903MB_001
808 cmdline_len += strlen(PCBA_STAGE_5);
tracychui3b4abb72020-06-15 10:50:49 +0800809 break;
michaellin52b647a2020-05-05 14:58:47 +0800810
michaellincb058f12020-04-24 15:05:10 +0800811 default:// Reserved
812 cmdline_len += strlen(PCBA_STAGE_F);
813 break;
814 }
815 //>20200424-michaellin
816
tedwu15b28c02020-07-03 18:02:48 +0800817 //<2020/07/02-tedwu, Add RF variant in system properties
818 cmdline_len += strlen(rfvar_prefix);
819 cmdline_len += strlen(pRFvar[get_rf_variant()]);
820 //>2020/07/02-tedwu, Add RF variant in system properties
821
tracychui3b4abb72020-06-15 10:50:49 +0800822 /* [TracyChui]Add memory_config property 20200615 start */
823 if(smem_get_ddr_size() == MEM_3GB )
824 {
825 cmdline_len += strlen(memory_config_3G);
826 }
827 else
828 {
829 cmdline_len += strlen(memory_config_4G);
830 }
831 /* [TracyChui]Add memory_config property 20200615 end */
832
Vladislav Levenetz3cd45242017-04-05 08:58:07 +0300833#if ENABLE_DISPLAY
Lijuan Gao4a5b8322014-07-24 10:38:42 +0800834 if (cmdline) {
835 if ((strstr(cmdline, DISPLAY_DEFAULT_PREFIX) == NULL) &&
Padmanabhan Komandurubccbcdc2015-06-30 16:19:24 +0530836 target_display_panel_node(display_panel_buf,
837 MAX_PANEL_BUF_SIZE) &&
Lijuan Gao4a5b8322014-07-24 10:38:42 +0800838 strlen(display_panel_buf)) {
839 cmdline_len += strlen(display_panel_buf);
840 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700841 }
Vladislav Levenetz3cd45242017-04-05 08:58:07 +0300842#endif
Dhaval Patel223ec952013-07-18 14:49:44 -0700843
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800844 if (target_warm_boot()) {
845 warm_boot = true;
846 cmdline_len += strlen(warmboot_cmdline);
847 }
848
Mayank Grover5384ed82018-05-09 12:09:24 +0530849 if (target_uses_system_as_root() ||
850 partition_multislot_is_supported())
Mayank Grover351a75e2017-05-30 20:06:08 +0530851 {
852 current_active_slot = partition_find_active_slot();
853 cmdline_len += (strlen(androidboot_slot_suffix)+
854 strlen(SUFFIX_SLOT(current_active_slot)));
855
Mayank Grover3804be72017-06-22 11:59:23 +0530856 system_ptn_index = partition_get_index("system");
857 if (platform_boot_dev_isemmc())
858 {
Sourabh Banerjee386b1322018-02-27 09:37:28 +0530859#if VERITY_LE
860 /*
861 Condition 4: Verity and A/B both enabled
862 Eventual command line looks like:
863 ... androidboot.slot_suffix=<slot_suffix> ... rootfstype=ext4 ...
864 ... root=/dev/dm-0 dm="system_<slot_suffix> none ro,0 1 android-verity /dev/mmcblk0p<NN>"
865 */
866 snprintf(syspath_buf, syspath_buflen, " %s %s%s %s%d\"",
867 verity_dev,
868 verity_system_part, suffix_slot[current_active_slot],
869 verity_params, system_ptn_index + 1);
870#else
871 /*
872 Condition 5: A/B enabled, but verity disabled
873 Eventual command line looks like:
874 ... androidboot.slot_suffix=<slot_suffix> ... rootfstype=ext4 ...
875 ... root=/dev/mmcblk0p<NN> ...
876 */
877 snprintf(syspath_buf, syspath_buflen, " %s%d",
878 sys_path, system_ptn_index + 1);
879#endif
Mayank Grover3804be72017-06-22 11:59:23 +0530880 }
881 else
882 {
883 lun = partition_get_lun(system_ptn_index);
884 snprintf(syspath_buf, syspath_buflen, " root=/dev/sd%c%d",
885 lun_char_base + lun,
886 partition_get_index_in_lun("system", lun));
887 }
Mayank Grover351a75e2017-05-30 20:06:08 +0530888
Monika Singh292b3e92018-03-17 22:40:23 +0530889#ifndef VERIFIED_BOOT_2
Mayank Grover3804be72017-06-22 11:59:23 +0530890 cmdline_len += strlen(syspath_buf);
Monika Singh292b3e92018-03-17 22:40:23 +0530891#endif
Mayank Grover5384ed82018-05-09 12:09:24 +0530892 }
893
894 if (target_uses_system_as_root() ||
895 partition_multislot_is_supported())
896 {
897 cmdline_len += strlen(sys_path_cmdline);
Mayank Groverb716edf2019-05-08 16:06:55 +0530898
899 /* For dynamic partition, support skip skip_initramfs */
900 if (!target_dynamic_partition_supported() &&
901 !boot_into_recovery)
Mayank Grover351a75e2017-05-30 20:06:08 +0530902 cmdline_len += strlen(skip_ramfs);
903 }
904
Mayank Grover466d6562018-05-10 14:52:20 +0530905#if HIBERNATION_SUPPORT
906 if (platform_boot_dev_isemmc())
907 {
908 swap_ptn_index = partition_get_index("swap");
909 if (swap_ptn_index != INVALID_PTN)
910 {
911 snprintf(resume_buf, resume_buflen,
912 " %s%d", resume,
913 (swap_ptn_index + 1));
914 cmdline_len += strlen(resume_buf);
915 }
916 else
917 {
918 dprintf(INFO, "WARNING: swap partition not found\n");
919 }
920 }
921#endif
922
Channagoud Kadabi85c7ec32016-01-28 23:09:21 -0800923#if TARGET_CMDLINE_SUPPORT
924 char *target_cmdline_buf = malloc(TARGET_MAX_CMDLNBUF);
925 int target_cmd_line_len;
926 ASSERT(target_cmdline_buf);
927 target_cmd_line_len = target_update_cmdline(target_cmdline_buf);
928 cmdline_len += target_cmd_line_len;
929#endif
930
lijuang83ef4b22018-08-23 11:01:55 +0800931#if !VERITY_LE
932 dtbo_idx = get_dtbo_idx ();
933 if (dtbo_idx != INVALID_PTN) {
934 snprintf(dtbo_idx_str, sizeof(dtbo_idx_str), "%s%d",
935 android_boot_dtbo_idx, dtbo_idx);
936 cmdline_len += strlen (dtbo_idx_str);
937 }
Parth Dixit1375d9e2019-07-08 20:56:13 +0530938
939 dtb_idx = get_dtb_idx ();
940 if (dtb_idx != INVALID_PTN) {
941 snprintf(dtb_idx_str, sizeof(dtb_idx_str), "%s%d",
942 android_boot_dtb_idx, dtb_idx);
943 cmdline_len += strlen (dtb_idx_str);
944 }
lijuang83ef4b22018-08-23 11:01:55 +0800945#endif
946
jhchen7a504d12020-04-24 15:45:57 +0800947/*[Arima_8910][jhchen] add fuse check property 20181031 begin*/
948#ifdef ENABLE_FUSE_CHECK
949 {
950 int hw_key_status = readl(0xA01D0);
951 is_fused = (hw_key_status & 0x2) >> 1;
952 dprintf(CRITICAL, "hw_key_status = 0x%x, is_fused=%d\n", hw_key_status, is_fused);
953 if (is_fused) {
954 cmdline_len += strlen(fuse_blown);
955 } else {
956 cmdline_len += strlen(fuse_not_blown);
957 }
958 }
959#endif
960/*[Arima_8910][jhchen] 20181031 end*/
961
David Ng183a7422009-12-07 14:55:21 -0800962 if (cmdline_len > 0) {
963 const char *src;
Maria Yu52254c02014-07-04 16:14:54 +0800964 unsigned char *dst;
965
966 cmdline_final = (unsigned char*) malloc((cmdline_len + 4) & (~3));
967 ASSERT(cmdline_final != NULL);
vijay kumar287bb542015-09-29 13:01:52 +0530968 memset((void *)cmdline_final, 0, sizeof(*cmdline_final));
Maria Yu52254c02014-07-04 16:14:54 +0800969 dst = cmdline_final;
Neeti Desaie245d492012-06-01 12:52:13 -0700970
Amol Jadi168b7712012-03-06 16:15:00 -0800971 /* Save start ptr for debug print */
David Ng183a7422009-12-07 14:55:21 -0800972 if (have_cmdline) {
973 src = cmdline;
974 while ((*dst++ = *src++));
975 }
976 if (target_is_emmc_boot()) {
977 src = emmc_cmdline;
978 if (have_cmdline) --dst;
David Ngf773dde2010-07-26 19:55:08 -0700979 have_cmdline = 1;
980 while ((*dst++ = *src++));
Sridhar Parasuram7bd4aaf2015-02-12 11:14:38 -0800981#if USE_BOOTDEV_CMDLINE
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700982 src = boot_dev_buf;
Mayank Groverb716edf2019-05-08 16:06:55 +0530983 if (have_cmdline &&
984 boot_dev_buf) {
985 --dst;
986 while ((*dst++ = *src++));
987 }
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700988#endif
Mayank Groverb716edf2019-05-08 16:06:55 +0530989 /* Dynamic partition append boot_devices */
990 if (target_dynamic_partition_supported() &&
991 boot_dev_buf) {
992 src = dynamic_bootdev_cmdline;
993 if (have_cmdline) --dst;
994 while ((*dst++ = *src++));
995 src = boot_dev_buf;
996 if (have_cmdline) --dst;
997 while ((*dst++ = *src++));
998 }
David Ngf773dde2010-07-26 19:55:08 -0700999 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -08001000
michaellincb058f12020-04-24 15:05:10 +08001001 //<20200424-michaellin, Add PCBA stage to system properties
1002 switch(GetPcbaVariant())
1003 {
1004 case 0:
1005 src = PCBA_STAGE_0;
1006 if (have_cmdline) --dst;
1007 while ((*dst++ = *src++));
1008 break;
1009 case 1:
1010 src = PCBA_STAGE_1;
1011 if (have_cmdline) --dst;
1012 while ((*dst++ = *src++));
1013 break;
1014 case 2:
1015 src = PCBA_STAGE_2;
1016 if (have_cmdline) --dst;
1017 while ((*dst++ = *src++));
1018 break;
1019 case 3:
1020 src = PCBA_STAGE_3;
1021 if (have_cmdline) --dst;
1022 while ((*dst++ = *src++));
1023 break;
1024 case 4:
1025 src = PCBA_STAGE_4;
1026 if (have_cmdline) --dst;
1027 while ((*dst++ = *src++));
1028 break;
michaellin52b647a2020-05-05 14:58:47 +08001029 case 5:
1030 src = PCBA_STAGE_5;
1031 if (have_cmdline) --dst;
1032 while ((*dst++ = *src++));
1033 break;
michaellincb058f12020-04-24 15:05:10 +08001034 default:
1035 src = PCBA_STAGE_F;
1036 if (have_cmdline) --dst;
1037 while ((*dst++ = *src++));
1038 break;
1039 }
1040 //>20200424-michaellin
1041
tedwu15b28c02020-07-03 18:02:48 +08001042 //<2020/07/02-tedwu, Add RF variant in system properties
1043 src = rfvar_prefix;
1044 if (have_cmdline) --dst;
1045 while ((*dst++ = *src++));
1046 src = pRFvar[get_rf_variant()];
1047 if (have_cmdline) --dst;
1048 while ((*dst++ = *src++));
1049 //>2020/07/02-tedwu, Add RF variant in system properties
1050
tracychui3b4abb72020-06-15 10:50:49 +08001051 /* [TracyChui]Add memory_config property 20200615 start */
1052 if(smem_get_ddr_size() == MEM_3GB )
1053 {
1054 src = memory_config_3G;
1055 if (have_cmdline) --dst;
1056 while ((*dst++ = *src++));
1057 }
1058 else
1059 {
1060 src = memory_config_4G;
1061 if (have_cmdline) --dst;
1062 while ((*dst++ = *src++));
1063 }
1064 /* [TracyChui]Add memory_config property 20200615 end */
1065
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -07001066#if VERIFIED_BOOT
Mayank Grover912eaa62017-10-26 12:08:53 +05301067 if (VB_M <= target_get_vb_version())
Sridhar Parasuram7d8bb9a2015-09-23 18:21:19 -07001068 {
Mayank Grover889be1b2017-09-12 20:12:23 +05301069 src = verified_state;
1070 if(have_cmdline) --dst;
1071 have_cmdline = 1;
1072 while ((*dst++ = *src++));
1073 src = vbsn[boot_state].name;
1074 if(have_cmdline) --dst;
1075 while ((*dst++ = *src++));
1076
1077 if ((device.verity_mode != 0 ) && (device.verity_mode != 1))
1078 {
1079 dprintf(CRITICAL, "Devinfo paritition possibly corrupted!!!. Please erase devinfo partition to continue booting\n");
1080 ASSERT(0);
1081 }
1082 src = verity_mode;
1083 if(have_cmdline) --dst;
1084 while ((*dst++ = *src++));
1085 src = vbvm[device.verity_mode].name;
1086 if(have_cmdline) -- dst;
1087 while ((*dst++ = *src++));
1088 src = keymaster_v1;
1089 if(have_cmdline) --dst;
1090 while ((*dst++ = *src++));
Sridhar Parasuram7d8bb9a2015-09-23 18:21:19 -07001091 }
Parth Dixitddbc7352015-10-18 03:13:31 +05301092#endif
Monika Singh292b3e92018-03-17 22:40:23 +05301093
1094 if (vbcmdline != NULL) {
1095 src = vbcmdline;
1096 if (have_cmdline) --dst;
1097 while ((*dst++ = *src++));
1098 }
1099
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -08001100 src = usb_sn_cmdline;
1101 if (have_cmdline) --dst;
1102 have_cmdline = 1;
1103 while ((*dst++ = *src++));
1104 src = sn_buf;
1105 if (have_cmdline) --dst;
1106 have_cmdline = 1;
1107 while ((*dst++ = *src++));
tracychuid184b912020-06-05 17:31:38 +08001108
1109 /* [20200605][TracyChui] Implement get Serial Number */
1110 #if defined(ENABLE_PRODINFO_ACCESS)
1111 src = cust_sn_cmdline;
1112 if (have_cmdline) --dst;
1113 have_cmdline = 1;
1114 while ((*dst++ = *src++));
1115 src = cust_sn_buf;
1116 if (have_cmdline) --dst;
1117 have_cmdline = 1;
1118 while ((*dst++ = *src++));
1119 src = factory_sn_cmdline;
1120 if (have_cmdline) --dst;
1121 have_cmdline = 1;
1122 while ((*dst++ = *src++));
1123 src = factory_sn_buf;
1124 if (have_cmdline) --dst;
1125 have_cmdline = 1;
1126 while ((*dst++ = *src++));
1127 #endif
1128 /*[20200605][TracyChui] Implement get Serial Number end */
Hanumant Singh8e1ac232014-01-29 13:41:51 -08001129 if (warm_boot) {
1130 if (have_cmdline) --dst;
1131 src = warmboot_cmdline;
1132 while ((*dst++ = *src++));
1133 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -08001134
Pavel Nedev5614d222013-06-17 18:01:02 +03001135 if (boot_into_recovery && gpt_exists) {
1136 src = secondary_gpt_enable;
1137 if (have_cmdline) --dst;
1138 while ((*dst++ = *src++));
1139 }
Monika Singh292b3e92018-03-17 22:40:23 +05301140#ifdef MDTP_SUPPORT
Shay Nachmanibc10dfe2015-02-10 14:45:55 +02001141 if (is_mdtp_activated) {
1142 src = mdtp_activated_flag;
1143 if (have_cmdline) --dst;
1144 while ((*dst++ = *src++));
1145 }
Monika Singh292b3e92018-03-17 22:40:23 +05301146#endif
Pavel Nedev328ac822013-04-05 15:25:11 +03001147 if (boot_into_ffbm) {
1148 src = androidboot_mode;
1149 if (have_cmdline) --dst;
1150 while ((*dst++ = *src++));
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07001151 src = ffbm_mode_string;
Pavel Nedev328ac822013-04-05 15:25:11 +03001152 if (have_cmdline) --dst;
1153 while ((*dst++ = *src++));
Hareesh Gauthamabf1e1e2017-04-12 18:09:24 +05301154
1155 if(is_systemd_present) {
1156 src = systemd_ffbm_mode;
1157 if (have_cmdline) --dst;
1158 while ((*dst++ = *src++));
1159 }
1160
Pavel Nedev898298c2013-02-27 12:36:09 -08001161 src = loglevel;
1162 if (have_cmdline) --dst;
1163 while ((*dst++ = *src++));
Matthew Qind886f3c2014-01-17 16:52:01 +08001164 } else if (boot_reason_alarm) {
1165 src = alarmboot_cmdline;
1166 if (have_cmdline) --dst;
1167 while ((*dst++ = *src++));
Pavel Nedev328ac822013-04-05 15:25:11 +03001168 } else if (pause_at_bootup) {
David Ngf773dde2010-07-26 19:55:08 -07001169 src = battchg_pause;
1170 if (have_cmdline) --dst;
David Ng183a7422009-12-07 14:55:21 -08001171 while ((*dst++ = *src++));
1172 }
Ajay Dudanid04110c2011-01-17 23:55:07 -08001173
tracychuid184b912020-06-05 17:31:38 +08001174/*[20200605][TracyChui] Implement get Serial Number start*/
1175#if defined(ENABLE_PRODINFO_ACCESS)
1176 if (AdbEnable) {
1177 src = UsbAdbEnable;
1178 if (have_cmdline) --dst;
1179 while ((*dst++ = *src++));
1180 }
Allen Lu50e75472020-02-27 11:14:29 +08001181
1182 if (AdbEnableCTS) {
1183 src = UsbAdbEnableCTS;
1184 if (have_cmdline) --dst;
1185 while ((*dst++ = *src++));
1186 }
tracychuid184b912020-06-05 17:31:38 +08001187#endif
1188/*[20200605][TracyChui] Implement get Serial Number end*/
1189
Shashank Mittalcd98d472011-08-02 14:29:24 -07001190 if(target_use_signed_kernel() && auth_kernel_img) {
1191 src = auth_kernel;
1192 if (have_cmdline) --dst;
1193 while ((*dst++ = *src++));
1194 }
1195
Ajay Dudanid04110c2011-01-17 23:55:07 -08001196 switch(target_baseband())
1197 {
1198 case BASEBAND_APQ:
1199 src = baseband_apq;
1200 if (have_cmdline) --dst;
1201 while ((*dst++ = *src++));
1202 break;
1203
1204 case BASEBAND_MSM:
1205 src = baseband_msm;
1206 if (have_cmdline) --dst;
1207 while ((*dst++ = *src++));
1208 break;
1209
1210 case BASEBAND_CSFB:
1211 src = baseband_csfb;
1212 if (have_cmdline) --dst;
1213 while ((*dst++ = *src++));
1214 break;
1215
Ajay Dudani6cff85e2011-02-04 16:02:16 -08001216 case BASEBAND_SVLTE2A:
1217 src = baseband_svlte2a;
Ajay Dudanid04110c2011-01-17 23:55:07 -08001218 if (have_cmdline) --dst;
1219 while ((*dst++ = *src++));
1220 break;
Ajay Dudani403bc492011-09-30 16:17:21 -07001221
1222 case BASEBAND_MDM:
1223 src = baseband_mdm;
1224 if (have_cmdline) --dst;
1225 while ((*dst++ = *src++));
1226 break;
Amol Jadi5c61a952012-05-04 17:05:35 -07001227
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -08001228 case BASEBAND_MDM2:
1229 src = baseband_mdm2;
1230 if (have_cmdline) --dst;
1231 while ((*dst++ = *src++));
1232 break;
1233
Amol Jadi5c61a952012-05-04 17:05:35 -07001234 case BASEBAND_SGLTE:
1235 src = baseband_sglte;
1236 if (have_cmdline) --dst;
1237 while ((*dst++ = *src++));
1238 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +05301239
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -08001240 case BASEBAND_SGLTE2:
1241 src = baseband_sglte2;
1242 if (have_cmdline) --dst;
1243 while ((*dst++ = *src++));
1244 break;
1245
Channagoud Kadabi141f2982012-10-31 11:23:02 +05301246 case BASEBAND_DSDA:
1247 src = baseband_dsda;
1248 if (have_cmdline) --dst;
1249 while ((*dst++ = *src++));
1250 break;
Amol Jadi2a15a272013-01-22 12:03:36 -08001251
1252 case BASEBAND_DSDA2:
1253 src = baseband_dsda2;
1254 if (have_cmdline) --dst;
1255 while ((*dst++ = *src++));
1256 break;
Vijay Kumar Pendotib228cfc2016-06-13 20:15:23 +05301257 case BASEBAND_APQ_NOWGR:
1258 src = baseband_apq_nowgr;
1259 if (have_cmdline) --dst;
1260 while ((*dst++ = *src++));
1261 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -08001262 }
Dhaval Patel223ec952013-07-18 14:49:44 -07001263
1264 if (strlen(display_panel_buf)) {
Dhaval Patel223ec952013-07-18 14:49:44 -07001265 src = display_panel_buf;
1266 if (have_cmdline) --dst;
1267 while ((*dst++ = *src++));
1268 }
Joonwoo Park61112782013-10-02 19:50:39 -07001269
1270 if (have_target_boot_params) {
1271 if (have_cmdline) --dst;
1272 src = target_boot_params;
1273 while ((*dst++ = *src++));
vijay kumar870515d2015-08-31 16:37:24 +05301274 free(target_boot_params);
Joonwoo Park61112782013-10-02 19:50:39 -07001275 }
Channagoud Kadabi85c7ec32016-01-28 23:09:21 -08001276
Mayank Grover351a75e2017-05-30 20:06:08 +05301277 if (partition_multislot_is_supported() && have_cmdline)
1278 {
1279 src = androidboot_slot_suffix;
1280 --dst;
1281 while ((*dst++ = *src++));
1282 --dst;
1283 src = SUFFIX_SLOT(current_active_slot);
1284 while ((*dst++ = *src++));
Mayank Grover5384ed82018-05-09 12:09:24 +05301285 }
Mayank Grover351a75e2017-05-30 20:06:08 +05301286
Mayank Grover351a75e2017-05-30 20:06:08 +05301287
Mayank Grover5384ed82018-05-09 12:09:24 +05301288 /*
1289 * System-As-Root behaviour, system.img should contain both
1290 * system content and ramdisk content, and should be mounted at
1291 * root(a/b).
1292 * Apending skip_ramfs for non a/b builds which use, system as root.
1293 */
1294 if ((target_uses_system_as_root() ||
1295 partition_multislot_is_supported()) &&
1296 have_cmdline)
1297 {
Mayank Groverb716edf2019-05-08 16:06:55 +05301298 if (!target_dynamic_partition_supported() &&
1299 !boot_into_recovery)
Mayank Grover5384ed82018-05-09 12:09:24 +05301300 {
1301 src = skip_ramfs;
Mayank Grover351a75e2017-05-30 20:06:08 +05301302 --dst;
1303 while ((*dst++ = *src++));
Mayank Grover5384ed82018-05-09 12:09:24 +05301304 }
1305
1306 src = sys_path_cmdline;
1307 --dst;
1308 while ((*dst++ = *src++));
Mayank Grover3804be72017-06-22 11:59:23 +05301309
Monika Singh292b3e92018-03-17 22:40:23 +05301310#ifndef VERIFIED_BOOT_2
Mayank Grover5384ed82018-05-09 12:09:24 +05301311 src = syspath_buf;
1312 --dst;
1313 while ((*dst++ = *src++));
Monika Singh292b3e92018-03-17 22:40:23 +05301314#endif
Mayank Grover351a75e2017-05-30 20:06:08 +05301315 }
1316
Mayank Grover466d6562018-05-10 14:52:20 +05301317#if HIBERNATION_SUPPORT
1318 if (swap_ptn_index != INVALID_PTN)
1319 {
1320 src = resume_buf;
1321 --dst;
1322 while ((*dst++ = *src++));
1323 }
1324#endif
1325
Channagoud Kadabi85c7ec32016-01-28 23:09:21 -08001326#if TARGET_CMDLINE_SUPPORT
1327 if (target_cmdline_buf && target_cmd_line_len)
1328 {
1329 if (have_cmdline) --dst;
1330 src = target_cmdline_buf;
1331 while((*dst++ = *src++));
1332 free(target_cmdline_buf);
1333 }
1334#endif
lijuang83ef4b22018-08-23 11:01:55 +08001335
1336#if !VERITY_LE
1337 if (dtbo_idx != INVALID_PTN) {
1338 src = dtbo_idx_str;
1339 --dst;
1340 while ((*dst++ = *src++));
1341 }
Parth Dixit1375d9e2019-07-08 20:56:13 +05301342
1343 if (dtb_idx != INVALID_PTN) {
1344 src = dtb_idx_str;
1345 --dst;
1346 while ((*dst++ = *src++));
1347 }
lijuang83ef4b22018-08-23 11:01:55 +08001348#endif
jhchen7a504d12020-04-24 15:45:57 +08001349
1350/*[Arima_8910][jhchen] add fuse check property 20181031 begin*/
1351#ifdef ENABLE_FUSE_CHECK
1352 if (is_fused) {
1353 src = fuse_blown;
1354 if (have_cmdline) --dst;
1355 while ((*dst++ = *src++));
1356 } else {
1357 src = fuse_not_blown;
1358 if (have_cmdline) --dst;
1359 while ((*dst++ = *src++));
1360 }
1361#endif
1362/*[Arima_8910][jhchen] 20181031 end*/
Neeti Desaie245d492012-06-01 12:52:13 -07001363 }
Dhaval Patel223ec952013-07-18 14:49:44 -07001364
1365
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -07001366 if (boot_dev_buf)
1367 free(boot_dev_buf);
1368
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -08001369 if (cmdline_final)
1370 dprintf(INFO, "cmdline: %s\n", cmdline_final);
1371 else
1372 dprintf(INFO, "cmdline is NULL\n");
Neeti Desaie245d492012-06-01 12:52:13 -07001373 return cmdline_final;
1374}
1375
1376unsigned *atag_core(unsigned *ptr)
1377{
1378 /* CORE */
1379 *ptr++ = 2;
1380 *ptr++ = 0x54410001;
1381
1382 return ptr;
1383
1384}
1385
1386unsigned *atag_ramdisk(unsigned *ptr, void *ramdisk,
1387 unsigned ramdisk_size)
1388{
1389 if (ramdisk_size) {
1390 *ptr++ = 4;
1391 *ptr++ = 0x54420005;
1392 *ptr++ = (unsigned)ramdisk;
1393 *ptr++ = ramdisk_size;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001394 }
1395
Neeti Desaie245d492012-06-01 12:52:13 -07001396 return ptr;
1397}
1398
1399unsigned *atag_ptable(unsigned **ptr_addr)
1400{
1401 int i;
1402 struct ptable *ptable;
1403
1404 if ((ptable = flash_get_ptable()) && (ptable->count != 0)) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001405 *(*ptr_addr)++ = 2 + (ptable->count * (sizeof(struct atag_ptbl_entry) /
1406 sizeof(unsigned)));
Neeti Desaie245d492012-06-01 12:52:13 -07001407 *(*ptr_addr)++ = 0x4d534d70;
1408 for (i = 0; i < ptable->count; ++i)
1409 ptentry_to_tag(ptr_addr, ptable_get(ptable, i));
1410 }
1411
1412 return (*ptr_addr);
1413}
1414
1415unsigned *atag_cmdline(unsigned *ptr, const char *cmdline)
1416{
1417 int cmdline_length = 0;
1418 int n;
Neeti Desaie245d492012-06-01 12:52:13 -07001419 char *dest;
1420
Amol Jadi10c7d1c2013-01-25 13:24:29 -08001421 cmdline_length = strlen((const char*)cmdline);
Neeti Desaie245d492012-06-01 12:52:13 -07001422 n = (cmdline_length + 4) & (~3);
1423
1424 *ptr++ = (n / 4) + 2;
1425 *ptr++ = 0x54410009;
1426 dest = (char *) ptr;
Amol Jadi10c7d1c2013-01-25 13:24:29 -08001427 while ((*dest++ = *cmdline++));
Neeti Desaie245d492012-06-01 12:52:13 -07001428 ptr += (n / 4);
1429
1430 return ptr;
1431}
1432
1433unsigned *atag_end(unsigned *ptr)
1434{
Brian Swetland9c4c0752009-01-25 16:23:50 -08001435 /* END */
1436 *ptr++ = 0;
1437 *ptr++ = 0;
1438
Neeti Desaie245d492012-06-01 12:52:13 -07001439 return ptr;
1440}
1441
1442void generate_atags(unsigned *ptr, const char *cmdline,
1443 void *ramdisk, unsigned ramdisk_size)
1444{
vijay kumar21a37452015-12-29 16:23:21 +05301445 unsigned *orig_ptr = ptr;
Neeti Desaie245d492012-06-01 12:52:13 -07001446 ptr = atag_core(ptr);
1447 ptr = atag_ramdisk(ptr, ramdisk, ramdisk_size);
1448 ptr = target_atag_mem(ptr);
1449
1450 /* Skip NAND partition ATAGS for eMMC boot */
1451 if (!target_is_emmc_boot()){
1452 ptr = atag_ptable(&ptr);
1453 }
1454
vijay kumar21a37452015-12-29 16:23:21 +05301455 /*
1456 * Atags size filled till + cmdline size + 1 unsigned for 4-byte boundary + 4 unsigned
1457 * for atag identifier in atag_cmdline and atag_end should be with in MAX_TAGS_SIZE bytes
1458 */
Mayank Groverbc8a2ef2018-02-23 18:03:36 +05301459 if (!cmdline)
1460 return;
1461
vijay kumar21a37452015-12-29 16:23:21 +05301462 if (((ptr - orig_ptr) + strlen(cmdline) + 5 * sizeof(unsigned)) < MAX_TAGS_SIZE) {
1463 ptr = atag_cmdline(ptr, cmdline);
1464 ptr = atag_end(ptr);
1465 }
1466 else {
1467 dprintf(CRITICAL,"Crossing ATAGs Max size allowed\n");
1468 ASSERT(0);
1469 }
Neeti Desaie245d492012-06-01 12:52:13 -07001470}
1471
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001472typedef void entry_func_ptr(unsigned, unsigned, unsigned*);
Neeti Desaie245d492012-06-01 12:52:13 -07001473void boot_linux(void *kernel, unsigned *tags,
1474 const char *cmdline, unsigned machtype,
1475 void *ramdisk, unsigned ramdisk_size)
1476{
Amol Jadi10c7d1c2013-01-25 13:24:29 -08001477 unsigned char *final_cmdline;
Amol Jadib6be5c12012-11-14 13:39:51 -08001478#if DEVICE_TREE
Neeti Desai17379b82012-06-04 18:42:53 -07001479 int ret = 0;
Amol Jadib6be5c12012-11-14 13:39:51 -08001480#endif
1481
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001482 void (*entry)(unsigned, unsigned, unsigned*) = (entry_func_ptr*)(PA((addr_t)kernel));
Deepa Dinamani25a9f762012-11-30 15:57:15 -08001483 uint32_t tags_phys = PA((addr_t)tags);
vijay kumar1a50a642015-11-16 12:41:15 +05301484 struct kernel64_hdr *kptr = ((struct kernel64_hdr*)(PA((addr_t)kernel)));
Deepa Dinamani25a9f762012-11-30 15:57:15 -08001485
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301486 ramdisk = (void *)PA((addr_t)ramdisk);
Neeti Desaie245d492012-06-01 12:52:13 -07001487
Amol Jadi10c7d1c2013-01-25 13:24:29 -08001488 final_cmdline = update_cmdline((const char*)cmdline);
1489
Neeti Desai17379b82012-06-04 18:42:53 -07001490#if DEVICE_TREE
Amol Jadib6be5c12012-11-14 13:39:51 -08001491 dprintf(INFO, "Updating device tree: start\n");
1492
Neeti Desai17379b82012-06-04 18:42:53 -07001493 /* Update the Device Tree */
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301494 ret = update_device_tree((void *)tags,(const char *)final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -07001495 if(ret)
1496 {
1497 dprintf(CRITICAL, "ERROR: Updating Device Tree Failed \n");
1498 ASSERT(0);
1499 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001500 dprintf(INFO, "Updating device tree: done\n");
Neeti Desai17379b82012-06-04 18:42:53 -07001501#else
Neeti Desaie245d492012-06-01 12:52:13 -07001502 /* Generating the Atags */
Amol Jadi10c7d1c2013-01-25 13:24:29 -08001503 generate_atags(tags, final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -07001504#endif
Neeti Desaie245d492012-06-01 12:52:13 -07001505
Monika Singh0ec6db82019-07-18 16:57:58 +05301506#if VERIFIED_BOOT
Monika Singhb0db4b82018-09-26 12:18:02 +05301507 if (VB_M <= target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +05301508 {
1509 if (device.verity_mode == 0) {
lijuangbdd9bb42016-03-01 18:22:17 +08001510#if FBCON_DISPLAY_MSG
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07001511#if ENABLE_VB_ATTEST
Mayank Grover889be1b2017-09-12 20:12:23 +05301512 display_bootverify_menu(DISPLAY_MENU_EIO);
1513 wait_for_users_action();
1514 if(!pwr_key_is_pressed)
1515 shutdown_device();
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07001516#else
Mayank Grover889be1b2017-09-12 20:12:23 +05301517 display_bootverify_menu(DISPLAY_MENU_LOGGING);
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07001518#endif
Mayank Grover889be1b2017-09-12 20:12:23 +05301519 wait_for_users_action();
lijuangbdd9bb42016-03-01 18:22:17 +08001520#else
Mayank Grover889be1b2017-09-12 20:12:23 +05301521 dprintf(CRITICAL,
1522 "The dm-verity is not started in enforcing mode.\nWait for 5 seconds before proceeding\n");
1523 mdelay(5000);
lijuangbdd9bb42016-03-01 18:22:17 +08001524#endif
Mayank Grover889be1b2017-09-12 20:12:23 +05301525 }
lijuangbdd9bb42016-03-01 18:22:17 +08001526 }
lijuangbdd9bb42016-03-01 18:22:17 +08001527#endif
1528
1529#if VERIFIED_BOOT
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001530 /* Write protect the device info */
Channagoud Kadabi3bd9d1e2015-05-05 16:18:20 -07001531 if (!boot_into_recovery && target_build_variant_user() && devinfo_present && mmc_write_protect("devinfo", 1))
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001532 {
1533 dprintf(INFO, "Failed to write protect dev info\n");
1534 ASSERT(0);
1535 }
1536#endif
1537
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08001538 /* Turn off splash screen if enabled */
1539#if DISPLAY_SPLASH_SCREEN
1540 target_display_shutdown();
1541#endif
1542
Veera Sundaram Sankaran67ea0932015-09-25 10:09:30 -07001543 /* Perform target specific cleanup */
1544 target_uninit();
Monika Singh292b3e92018-03-17 22:40:23 +05301545 free_verified_boot_resource(&info);
1546 if (final_cmdline)
1547 free(final_cmdline);
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08001548
Deepa Dinamani33734bc2013-03-06 12:16:06 -08001549 dprintf(INFO, "booting linux @ %p, ramdisk @ %p (%d), tags/device tree @ %p\n",
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301550 entry, ramdisk, ramdisk_size, (void *)tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001551
1552 enter_critical_section();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001553
Amol Jadi4421e652011-06-16 15:00:48 -07001554 /* do any platform specific cleanup before kernel entry */
1555 platform_uninit();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001556
Brian Swetland9c4c0752009-01-25 16:23:50 -08001557 arch_disable_cache(UCACHE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001558
Amol Jadi504f9fe2012-08-16 13:56:48 -07001559#if ARM_WITH_MMU
Brian Swetland9c4c0752009-01-25 16:23:50 -08001560 arch_disable_mmu();
Amol Jadi504f9fe2012-08-16 13:56:48 -07001561#endif
Amol Jadi492d5a52013-03-15 16:12:34 -07001562 bs_set_timestamp(BS_KERNEL_ENTRY);
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001563
1564 if (IS_ARM64(kptr))
1565 /* Jump to a 64bit kernel */
1566 scm_elexec_call((paddr_t)kernel, tags_phys);
1567 else
1568 /* Jump to a 32bit kernel */
1569 entry(0, machtype, (unsigned*)tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001570}
1571
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001572/* Function to check if the memory address range falls within the aboot
1573 * boundaries.
1574 * start: Start of the memory region
1575 * size: Size of the memory region
1576 */
Vijay Kumar Pendoti9c002ad2016-03-09 13:52:45 +05301577int check_aboot_addr_range_overlap(uintptr_t start, uint32_t size)
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001578{
1579 /* Check for boundary conditions. */
Sundarajan Srinivasance2a0ea2013-12-16 17:02:56 -08001580 if ((UINT_MAX - start) < size)
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001581 return -1;
1582
1583 /* Check for memory overlap. */
1584 if ((start < MEMBASE) && ((start + size) <= MEMBASE))
1585 return 0;
Channagoud Kadabi94143912013-10-15 12:53:52 -07001586 else if (start >= (MEMBASE + MEMSIZE))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001587 return 0;
1588 else
1589 return -1;
1590}
1591
Mayank Grovere559cec2017-10-17 15:12:03 +05301592/* Function to check if the memory address range falls beyond ddr region.
1593 * start: Start of the memory region
1594 * size: Size of the memory region
1595 */
1596int check_ddr_addr_range_bound(uintptr_t start, uint32_t size)
1597{
1598 uintptr_t ddr_pa_start_addr = PA(get_ddr_start());
1599 uint64_t ddr_size = smem_get_ddr_size();
1600 uint64_t ddr_pa_end_addr = ddr_pa_start_addr + ddr_size;
1601 uintptr_t pa_start_addr = PA(start);
1602
1603 /* Check for boundary conditions. */
1604 if ((UINT_MAX - start) < size)
1605 return -1;
1606
1607 /* Check if memory range is beyond the ddr range. */
1608 if (pa_start_addr < ddr_pa_start_addr ||
1609 pa_start_addr >= (ddr_pa_end_addr) ||
1610 (pa_start_addr + size) > ddr_pa_end_addr)
1611 return -1;
1612 else
1613 return 0;
1614}
1615
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001616BUF_DMA_ALIGN(buf, BOOT_IMG_MAX_PAGE_SIZE); //Equal to max-supported pagesize
Dima Zavin214cc642009-01-26 11:16:21 -08001617
Mayank Groverdd080e82018-09-04 20:12:13 +05301618int getimage(void **image_buffer, uint32_t *imgsize,
1619 const char *imgname)
Monika Singh292b3e92018-03-17 22:40:23 +05301620{
Mayank Groverdd080e82018-09-04 20:12:13 +05301621 uint32_t loadedindex;
1622 if (image_buffer == NULL || imgsize == NULL ||
Monika Singh292b3e92018-03-17 22:40:23 +05301623 imgname == NULL) {
1624 dprintf(CRITICAL, "getimage: invalid parameters\n");
1625 return -1;
1626 }
Mayank Groverdd080e82018-09-04 20:12:13 +05301627 for (loadedindex = 0; loadedindex < info.num_loaded_images; loadedindex++) {
1628 if (!strncmp(info.images[loadedindex].name, imgname,
Monika Singh292b3e92018-03-17 22:40:23 +05301629 strlen(imgname))) {
Mayank Groverdd080e82018-09-04 20:12:13 +05301630 *image_buffer = info.images[loadedindex].image_buffer;
1631 *imgsize = info.images[loadedindex].imgsize;
Mayank Grover027a9412018-09-04 15:12:05 +05301632 dprintf(SPEW, "getimage(): Loaded image [%s|%d]\n",
1633 info.images[loadedindex].name,
1634 info.images[loadedindex].imgsize);
Monika Singh292b3e92018-03-17 22:40:23 +05301635 return 0;
1636 }
1637 }
1638 return -1;
1639}
1640
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001641static void verify_signed_bootimg(uint32_t bootimg_addr, uint32_t bootimg_size)
1642{
1643 int ret;
Channagoud Kadabia8c623f2015-01-13 14:48:48 -08001644
1645#if !VERIFIED_BOOT
Sundarajan Srinivasance54d6e2013-11-11 12:45:00 -08001646#if IMAGE_VERIF_ALGO_SHA1
1647 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA1;
1648#else
1649 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA256;
1650#endif
Channagoud Kadabia8c623f2015-01-13 14:48:48 -08001651#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001652
1653 /* Assume device is rooted at this time. */
1654 device.is_tampered = 1;
1655
1656 dprintf(INFO, "Authenticating boot image (%d): start\n", bootimg_size);
1657
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001658#if VERIFIED_BOOT
Monika Singh7d2fc272018-03-16 17:16:01 +05301659 uint32_t bootstate;
1660 if(boot_into_recovery &&
Mayank Grover68fbf0d2017-10-24 14:13:39 +05301661 (!partition_multislot_is_supported()))
Monika Singh7d2fc272018-03-16 17:16:01 +05301662 {
1663 ret = boot_verify_image((unsigned char *)bootimg_addr,
1664 bootimg_size, "/recovery", &bootstate);
1665 }
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001666 else
Monika Singh7d2fc272018-03-16 17:16:01 +05301667 {
1668 ret = boot_verify_image((unsigned char *)bootimg_addr,
1669 bootimg_size, "/boot", &bootstate);
1670 }
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001671 boot_verify_print_state();
1672#else
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001673 ret = image_verify((unsigned char *)bootimg_addr,
1674 (unsigned char *)(bootimg_addr + bootimg_size),
1675 bootimg_size,
Sundarajan Srinivasance54d6e2013-11-11 12:45:00 -08001676 auth_algo);
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001677#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001678 dprintf(INFO, "Authenticating boot image: done return value = %d\n", ret);
1679
1680 if (ret)
1681 {
1682 /* Authorized kernel */
1683 device.is_tampered = 0;
Sundarajan Srinivasan3fb21f12013-09-16 18:36:15 -07001684 auth_kernel_img = 1;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001685 }
1686
Amit Blay4aa292f2015-04-28 21:55:59 +03001687#ifdef MDTP_SUPPORT
1688 {
1689 /* Verify MDTP lock.
1690 * For boot & recovery partitions, use aboot's verification result.
1691 */
1692 mdtp_ext_partition_verification_t ext_partition;
1693 ext_partition.partition = boot_into_recovery ? MDTP_PARTITION_RECOVERY : MDTP_PARTITION_BOOT;
1694 ext_partition.integrity_state = device.is_tampered ? MDTP_PARTITION_STATE_INVALID : MDTP_PARTITION_STATE_VALID;
1695 ext_partition.page_size = 0; /* Not needed since already validated */
1696 ext_partition.image_addr = 0; /* Not needed since already validated */
1697 ext_partition.image_size = 0; /* Not needed since already validated */
1698 ext_partition.sig_avail = FALSE; /* Not needed since already validated */
1699 mdtp_fwlock_verify_lock(&ext_partition);
1700 }
1701#endif /* MDTP_SUPPORT */
1702
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001703#if USE_PCOM_SECBOOT
1704 set_tamper_flag(device.is_tampered);
1705#endif
1706
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001707#if VERIFIED_BOOT
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001708 switch(boot_verify_get_state())
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001709 {
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001710 case RED:
lijuanga40d6302015-07-20 20:10:13 +08001711#if FBCON_DISPLAY_MSG
lijuang9a7d3b92015-11-30 14:41:24 +08001712 display_bootverify_menu(DISPLAY_MENU_RED);
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07001713#if ENABLE_VB_ATTEST
1714 mdelay(DELAY_WAIT);
1715 shutdown_device();
1716#else
lijuanga40d6302015-07-20 20:10:13 +08001717 wait_for_users_action();
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07001718#endif
lijuanga40d6302015-07-20 20:10:13 +08001719#else
1720 dprintf(CRITICAL,
1721 "Your device has failed verification and may not work properly.\nWait for 5 seconds before proceeding\n");
1722 mdelay(5000);
1723#endif
1724
1725 break;
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001726 case YELLOW:
lijuanga40d6302015-07-20 20:10:13 +08001727#if FBCON_DISPLAY_MSG
lijuang9a7d3b92015-11-30 14:41:24 +08001728 display_bootverify_menu(DISPLAY_MENU_YELLOW);
lijuanga40d6302015-07-20 20:10:13 +08001729 wait_for_users_action();
1730#else
1731 dprintf(CRITICAL,
1732 "Your device has loaded a different operating system.\nWait for 5 seconds before proceeding\n");
1733 mdelay(5000);
1734#endif
1735 break;
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001736 default:
lijuanga40d6302015-07-20 20:10:13 +08001737 break;
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001738 }
1739#endif
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001740#if !VERIFIED_BOOT
Unnati Gandhi1be04752015-03-27 19:41:53 +05301741 if(device.is_tampered)
1742 {
1743 write_device_info_mmc(&device);
1744 #ifdef TZ_TAMPER_FUSE
Monika Singh98257462018-06-06 11:28:49 +05301745 set_tamper_fuse_cmd(HLOS_IMG_TAMPER_FUSE);
Unnati Gandhi1be04752015-03-27 19:41:53 +05301746 #endif
1747 #ifdef ASSERT_ON_TAMPER
1748 dprintf(CRITICAL, "Device is tampered. Asserting..\n");
1749 ASSERT(0);
1750 #endif
1751 }
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001752#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001753}
1754
Maunik Shah0f3c8ac2014-03-02 17:47:58 +05301755static bool check_format_bit()
1756{
1757 bool ret = false;
1758 int index;
1759 uint64_t offset;
1760 struct boot_selection_info *in = NULL;
1761 char *buf = NULL;
1762
1763 index = partition_get_index("bootselect");
1764 if (index == INVALID_PTN)
1765 {
1766 dprintf(INFO, "Unable to locate /bootselect partition\n");
1767 return ret;
1768 }
1769 offset = partition_get_offset(index);
1770 if(!offset)
1771 {
1772 dprintf(INFO, "partition /bootselect doesn't exist\n");
1773 return ret;
1774 }
1775 buf = (char *) memalign(CACHE_LINE, ROUNDUP(page_size, CACHE_LINE));
Mayank Grover48860402016-11-29 12:34:53 +05301776 mmc_set_lun(partition_get_lun(index));
Maunik Shah0f3c8ac2014-03-02 17:47:58 +05301777 ASSERT(buf);
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301778 if (mmc_read(offset, (uint32_t *)buf, page_size))
Maunik Shah0f3c8ac2014-03-02 17:47:58 +05301779 {
1780 dprintf(INFO, "mmc read failure /bootselect %d\n", page_size);
1781 free(buf);
1782 return ret;
1783 }
1784 in = (struct boot_selection_info *) buf;
1785 if ((in->signature == BOOTSELECT_SIGNATURE) &&
1786 (in->version == BOOTSELECT_VERSION)) {
1787 if ((in->state_info & BOOTSELECT_FORMAT) &&
1788 !(in->state_info & BOOTSELECT_FACTORY))
1789 ret = true;
1790 } else {
1791 dprintf(CRITICAL, "Signature: 0x%08x or version: 0x%08x mismatched of /bootselect\n",
1792 in->signature, in->version);
1793 ASSERT(0);
1794 }
1795 free(buf);
1796 return ret;
1797}
1798
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001799void boot_verifier_init()
1800{
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001801 uint32_t boot_state;
1802 /* Check if device unlock */
1803 if(device.is_unlocked)
1804 {
1805 boot_verify_send_event(DEV_UNLOCK);
1806 boot_verify_print_state();
1807 dprintf(CRITICAL, "Device is unlocked! Skipping verification...\n");
1808 return;
1809 }
1810 else
1811 {
1812 boot_verify_send_event(BOOT_INIT);
1813 }
1814
1815 /* Initialize keystore */
1816 boot_state = boot_verify_keystore_init();
1817 if(boot_state == YELLOW)
1818 {
1819 boot_verify_print_state();
1820 dprintf(CRITICAL, "Keystore verification failed! Continuing anyways...\n");
1821 }
1822}
1823
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301824/* Function to return recovery appended dtbo buffer info */
1825void get_recovery_dtbo_info(uint32_t *dtbo_size, void **dtbo_buf)
1826{
1827 *dtbo_size = recovery_dtbo_size;
1828 *dtbo_buf = recovery_dtbo_buf;
1829 return;
1830}
1831
Shashank Mittal23b8f422010-04-16 19:27:21 -07001832int boot_linux_from_mmc(void)
1833{
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301834 boot_img_hdr *hdr = (void*) buf;
1835 boot_img_hdr *uhdr;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001836 unsigned offset = 0;
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07001837 int rcode;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001838 unsigned long long ptn = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001839 int index = INVALID_PTN;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001840
Shashank Mittalcd98d472011-08-02 14:29:24 -07001841 unsigned char *image_addr = 0;
1842 unsigned kernel_actual;
1843 unsigned ramdisk_actual;
1844 unsigned imagesize_actual;
Neeti Desai465491e2012-07-31 12:53:35 -07001845 unsigned second_actual = 0;
Parth Dixit1375d9e2019-07-08 20:56:13 +05301846 void * image_buf = NULL;
Neeti Desai465491e2012-07-31 12:53:35 -07001847
Matthew Qin271927e2015-03-31 22:07:07 -04001848 unsigned int dtb_size = 0;
Parth Dixit1375d9e2019-07-08 20:56:13 +05301849 unsigned dtb_image_size = 0;
1850 uint32_t dtb_image_offset = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08001851 unsigned int out_len = 0;
1852 unsigned int out_avai_len = 0;
1853 unsigned char *out_addr = NULL;
1854 uint32_t dtb_offset = 0;
1855 unsigned char *kernel_start_addr = NULL;
1856 unsigned int kernel_size = 0;
Ameya Thakur10a33452016-06-13 14:24:26 -07001857 unsigned int patched_kernel_hdr_size = 0;
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301858 uint64_t image_size = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08001859 int rc;
Monika Singh292b3e92018-03-17 22:40:23 +05301860#if VERIFIED_BOOT_2
1861 int status;
Mayank Grover027a9412018-09-04 15:12:05 +05301862 void *dtbo_image_buf = NULL;
1863 uint32_t dtbo_image_sz = 0;
Mayank Grover9df84c02018-08-30 15:46:35 +05301864 void *vbmeta_image_buf = NULL;
1865 uint32_t vbmeta_image_sz = 0;
Monika Singh292b3e92018-03-17 22:40:23 +05301866#endif
Mayank Groverc93cad82017-10-03 12:23:45 +05301867 char *ptn_name = NULL;
Neeti Desai465491e2012-07-31 12:53:35 -07001868#if DEVICE_TREE
1869 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -07001870 struct dt_entry dt_entry;
Neeti Desai465491e2012-07-31 12:53:35 -07001871 unsigned dt_table_offset;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001872 uint32_t dt_actual;
Deepa Dinamani19648b42013-09-05 17:05:55 -07001873 uint32_t dt_hdr_size;
Matthew Qin271927e2015-03-31 22:07:07 -04001874 unsigned char *best_match_dt_addr = NULL;
Neeti Desai465491e2012-07-31 12:53:35 -07001875#endif
Matthew Qin49e51fa2015-02-09 10:40:45 +08001876 struct kernel64_hdr *kptr = NULL;
Mayank Grover351a75e2017-05-30 20:06:08 +05301877 int current_active_slot = INVALID;
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001878
Maunik Shah0f3c8ac2014-03-02 17:47:58 +05301879 if (check_format_bit())
1880 boot_into_recovery = 1;
1881
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07001882 if (!boot_into_recovery) {
1883 memset(ffbm_mode_string, '\0', sizeof(ffbm_mode_string));
1884 rcode = get_ffbm(ffbm_mode_string, sizeof(ffbm_mode_string));
1885 if (rcode <= 0) {
1886 boot_into_ffbm = false;
1887 if (rcode < 0)
1888 dprintf(CRITICAL,"failed to get ffbm cookie");
1889 } else
1890 boot_into_ffbm = true;
1891 } else
1892 boot_into_ffbm = false;
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301893 uhdr = (boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001894 if (!memcmp(uhdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1895 dprintf(INFO, "Unified boot method!\n");
1896 hdr = uhdr;
1897 goto unified_boot;
1898 }
Mayank Groverc93cad82017-10-03 12:23:45 +05301899
1900 /* For a/b recovery image code is on boot partition.
1901 If we support multislot, always use boot partition. */
1902 if (boot_into_recovery &&
Mayank Grover8ab4a762019-04-25 17:23:34 +05301903 ((!partition_multislot_is_supported()) ||
1904 (target_dynamic_partition_supported())))
Mayank Groverc93cad82017-10-03 12:23:45 +05301905 ptn_name = "recovery";
1906 else
1907 ptn_name = "boot";
1908
1909 index = partition_get_index(ptn_name);
1910 ptn = partition_get_offset(index);
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301911 image_size = partition_get_size(index);
1912 if(ptn == 0 || image_size == 0) {
Mayank Groverc93cad82017-10-03 12:23:45 +05301913 dprintf(CRITICAL, "ERROR: No %s partition found\n", ptn_name);
1914 return -1;
Kinson Chikf1a43512011-07-14 11:28:39 -07001915 }
Mayank Groverc93cad82017-10-03 12:23:45 +05301916
Channagoud Kadabief0547c2015-02-10 12:57:38 -08001917 /* Set Lun for boot & recovery partitions */
1918 mmc_set_lun(partition_get_lun(index));
Shashank Mittal23b8f422010-04-16 19:27:21 -07001919
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301920 if (mmc_read(ptn + offset, (uint32_t *) buf, page_size)) {
Shashank Mittal23b8f422010-04-16 19:27:21 -07001921 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
1922 return -1;
1923 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001924
1925 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001926 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Mayank Grover351a75e2017-05-30 20:06:08 +05301927 return ERR_INVALID_BOOT_MAGIC;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001928 }
1929
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07001930 if (hdr->page_size && (hdr->page_size != page_size)) {
vijay kumar2e21b3a2014-06-26 17:40:15 +05301931
1932 if (hdr->page_size > BOOT_IMG_MAX_PAGE_SIZE) {
1933 dprintf(CRITICAL, "ERROR: Invalid page size\n");
1934 return -1;
1935 }
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07001936 page_size = hdr->page_size;
1937 page_mask = page_size - 1;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001938 }
1939
Matthew Qin49e51fa2015-02-09 10:40:45 +08001940 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1941 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Kishor PKd8ddcad2017-07-27 13:53:57 +05301942 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001943
Matthew Qin49e51fa2015-02-09 10:40:45 +08001944 image_addr = (unsigned char *)target_get_scratch_address();
Mayank Grover6fd36792018-09-10 13:26:58 +05301945#if VERIFIED_BOOT_2
1946 /* Create hole in start of image for VB salt to copy */
1947 image_addr += SALT_BUFF_OFFSET;
1948#endif
Kishor PK9e91b592017-05-24 12:14:55 +05301949 memcpy(image_addr, (void *)buf, page_size);
1950
1951 /* ensure commandline is terminated */
1952 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
Matthew Qin49e51fa2015-02-09 10:40:45 +08001953
1954#if DEVICE_TREE
Parth Dixit4097b622016-03-15 14:42:27 +05301955#ifndef OSVERSION_IN_BOOTIMAGE
1956 dt_size = hdr->dt_size;
Mayank Grover5a502582018-09-12 11:24:49 +05301957#else
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301958 dprintf(INFO, "BootImage Header: %d\n", hdr->header_version);
Mayank Grover5a502582018-09-12 11:24:49 +05301959#endif
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301960
Parth Dixit4097b622016-03-15 14:42:27 +05301961 dt_actual = ROUND_TO_PAGE(dt_size, page_mask);
Kishor PKd8ddcad2017-07-27 13:53:57 +05301962 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 +05301963 dprintf(CRITICAL, "Integer overflow detected in bootimage header fields at %u in %s\n",__LINE__,__FILE__);
1964 return -1;
1965 }
Kishor PKd8ddcad2017-07-27 13:53:57 +05301966 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + second_actual + dt_actual);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001967#else
Kishor PKd8ddcad2017-07-27 13:53:57 +05301968 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 +05301969 dprintf(CRITICAL, "Integer overflow detected in bootimage header fields at %u in %s\n",__LINE__,__FILE__);
1970 return -1;
1971 }
Kishor PKd8ddcad2017-07-27 13:53:57 +05301972 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + second_actual);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001973#endif
Parth Dixit1375d9e2019-07-08 20:56:13 +05301974 dtb_image_size = hdr->kernel_size;
Matthew Qin49e51fa2015-02-09 10:40:45 +08001975
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301976#ifdef OSVERSION_IN_BOOTIMAGE
1977 /* If header version is ONE and booting into recovery,
1978 dtbo is appended with recovery image.
1979 Doing following:
1980 * Validating the recovery offset and size.
1981 * Extracting recovery dtbo to be used as dtbo.
1982 */
1983 if (boot_into_recovery &&
1984 hdr->header_version == BOOT_HEADER_VERSION_ONE)
1985 {
1986 struct boot_img_hdr_v1 *hdr1 =
1987 (struct boot_img_hdr_v1 *) (image_addr + sizeof(boot_img_hdr));
Mayank Grovera1a83c72018-09-24 11:23:15 +05301988 unsigned int recovery_dtbo_actual = 0;
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301989
Mayank Grovera1a83c72018-09-24 11:23:15 +05301990 recovery_dtbo_actual = ROUND_TO_PAGE(hdr1->recovery_dtbo_size, page_mask);
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301991 if ((hdr1->header_size !=
1992 sizeof(struct boot_img_hdr_v1) + sizeof(boot_img_hdr)))
1993 {
1994 dprintf(CRITICAL, "Invalid boot image header: %d\n", hdr1->header_size);
1995 return -1;
1996 }
1997
Mayank Grovera1a83c72018-09-24 11:23:15 +05301998 if (recovery_dtbo_actual > MAX_SUPPORTED_DTBO_IMG_BUF)
1999 {
2000 dprintf(CRITICAL, "Recovery Dtbo Size too big %x, Allowed size %x\n", recovery_dtbo_actual,
Mayank Grovere1ab96c2018-09-04 20:31:31 +05302001 MAX_SUPPORTED_DTBO_IMG_BUF);
2002 return -1;
2003 }
2004
Mayank Grovera1a83c72018-09-24 11:23:15 +05302005 if (UINT_MAX < ((uint64_t)imagesize_actual + recovery_dtbo_actual))
Mayank Grovere1ab96c2018-09-04 20:31:31 +05302006 {
2007 dprintf(CRITICAL, "Integer overflow detected in recoveryimage header fields at %u in %s\n",__LINE__,__FILE__);
2008 return -1;
2009 }
2010
Mayank Grovera1a83c72018-09-24 11:23:15 +05302011 if (UINT_MAX < (hdr1->recovery_dtbo_offset + recovery_dtbo_actual)) {
2012 dprintf(CRITICAL,
2013 "Integer overflow detected in recovery image header fields at %u in %s\n",__LINE__,__FILE__);
2014 return -1;
2015 }
2016
2017 if (hdr1->recovery_dtbo_offset + recovery_dtbo_actual > image_size)
Mayank Grovere1ab96c2018-09-04 20:31:31 +05302018 {
2019 dprintf(CRITICAL, "Invalid recovery dtbo: Recovery Dtbo Offset=0x%llx,"
2020 " Recovery Dtbo Size=0x%x, Image Size=0x%llx\n",
2021 hdr1->recovery_dtbo_offset, recovery_dtbo_size, image_size);
2022 return -1;
2023 }
2024
2025 recovery_dtbo_buf = (void *)(hdr1->recovery_dtbo_offset + image_addr);
Mayank Grovera1a83c72018-09-24 11:23:15 +05302026 recovery_dtbo_size = recovery_dtbo_actual;
Mayank Grovere1ab96c2018-09-04 20:31:31 +05302027 imagesize_actual += recovery_dtbo_size;
2028
2029 dprintf(SPEW, "Header version: %d\n", hdr->header_version);
2030 dprintf(SPEW, "Recovery Dtbo Size 0x%x\n", recovery_dtbo_size);
2031 dprintf(SPEW, "Recovery Dtbo Offset 0x%llx\n", hdr1->recovery_dtbo_offset);
2032
2033 }
Parth Dixit1375d9e2019-07-08 20:56:13 +05302034
2035 if ( hdr->header_version == BOOT_HEADER_VERSION_TWO) {
2036 struct boot_img_hdr_v1 *hdr1 =
2037 (struct boot_img_hdr_v1 *) (image_addr + sizeof(boot_img_hdr));
2038 struct boot_img_hdr_v2 *hdr2 = (struct boot_img_hdr_v2 *)
2039 (image_addr + sizeof(boot_img_hdr) +
2040 BOOT_IMAGE_HEADER_V2_OFFSET);
2041 unsigned int recovery_dtbo_actual = 0;
2042
lijuang0b17ba22019-09-04 14:20:47 +08002043 recovery_dtbo_actual =
2044 ROUND_TO_PAGE(hdr1->recovery_dtbo_size, page_mask);
2045 imagesize_actual += recovery_dtbo_actual;
Parth Dixit1375d9e2019-07-08 20:56:13 +05302046
2047 imagesize_actual += ROUND_TO_PAGE(hdr2->dtb_size, page_mask);
2048
2049
2050 dtb_image_offset = page_size + patched_kernel_hdr_size +
2051 kernel_actual + ramdisk_actual + second_actual +
2052 recovery_dtbo_actual;
2053
2054 dprintf(SPEW, "Header version: %d\n", hdr->header_version);
2055 dprintf(SPEW, "Dtb image offset 0x%x\n", dtb_image_offset);
2056 }
Mayank Grovere1ab96c2018-09-04 20:31:31 +05302057#endif
2058
Parth Dixit1375d9e2019-07-08 20:56:13 +05302059
Saranya Chidurab1aaf232018-11-19 15:47:50 +05302060 /* Validate the boot/recovery image size is within the bounds of partition size */
2061 if (imagesize_actual > image_size) {
2062 dprintf(CRITICAL, "Image size is greater than partition size.\n");
2063 return -1;
2064 }
2065
Matthew Qin49e51fa2015-02-09 10:40:45 +08002066#if VERIFIED_BOOT
2067 boot_verifier_init();
2068#endif
2069
Mayank Grover1ceaee22019-04-01 17:54:32 +05302070#if VERIFIED_BOOT_2
2071 /* read full partition if device is unlocked */
2072 if (device.is_unlocked)
2073 imagesize_actual = image_size;
2074#endif
2075
Vijay Kumar Pendoti9c002ad2016-03-09 13:52:45 +05302076 if (check_aboot_addr_range_overlap((uintptr_t) image_addr, imagesize_actual))
Matthew Qin49e51fa2015-02-09 10:40:45 +08002077 {
2078 dprintf(CRITICAL, "Boot image buffer address overlaps with aboot addresses.\n");
2079 return -1;
2080 }
2081
Matthew Qinbb7923d2015-02-09 10:56:09 +08002082 /*
2083 * Update loading flow of bootimage to support compressed/uncompressed
2084 * bootimage on both 64bit and 32bit platform.
2085 * 1. Load bootimage from emmc partition onto DDR.
2086 * 2. Check if bootimage is gzip format. If yes, decompress compressed kernel
2087 * 3. Check kernel header and update kernel load addr for 64bit and 32bit
2088 * platform accordingly.
2089 * 4. Sanity Check on kernel_addr and ramdisk_addr and copy data.
2090 */
Mayank Grover351a75e2017-05-30 20:06:08 +05302091 if (partition_multislot_is_supported())
2092 {
2093 current_active_slot = partition_find_active_slot();
2094 dprintf(INFO, "Loading boot image (%d) active_slot(%s): start\n",
2095 imagesize_actual, SUFFIX_SLOT(current_active_slot));
2096 }
2097 else
2098 {
2099 dprintf(INFO, "Loading (%s) image (%d): start\n",
2100 (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual);
2101 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08002102 bs_set_timestamp(BS_KERNEL_LOAD_START);
2103
Gaurav Nebhwani43e2a462016-03-17 21:32:56 +05302104 if ((target_get_max_flash_size() - page_size) < imagesize_actual)
2105 {
2106 dprintf(CRITICAL, "booimage size is greater than DDR can hold\n");
2107 return -1;
2108 }
Kishor PK9e91b592017-05-24 12:14:55 +05302109 offset = page_size;
2110 /* Read image without signature and header*/
2111 if (mmc_read(ptn + offset, (void *)(image_addr + offset), imagesize_actual - page_size))
Matthew Qin49e51fa2015-02-09 10:40:45 +08002112 {
2113 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
2114 return -1;
2115 }
2116
Mayank Grover351a75e2017-05-30 20:06:08 +05302117 if (partition_multislot_is_supported())
2118 {
2119 dprintf(INFO, "Loading boot image (%d) active_slot(%s): done\n",
2120 imagesize_actual, SUFFIX_SLOT(current_active_slot));
2121 }
2122 else
2123 {
2124 dprintf(INFO, "Loading (%s) image (%d): done\n",
Channagoud Kadabif0705b52015-08-20 14:16:08 -07002125 (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual);
2126
Mayank Grover351a75e2017-05-30 20:06:08 +05302127 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08002128 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
2129
2130 /* Authenticate Kernel */
2131 dprintf(INFO, "use_signed_kernel=%d, is_unlocked=%d, is_tampered=%d.\n",
2132 (int) target_use_signed_kernel(),
2133 device.is_unlocked,
2134 device.is_tampered);
Monika Singh292b3e92018-03-17 22:40:23 +05302135#if VERIFIED_BOOT_2
Mayank Grover1ceaee22019-04-01 17:54:32 +05302136 /* if device is unlocked skip reading signature, as full partition is read */
2137 if (!device.is_unlocked)
Monika Singh292b3e92018-03-17 22:40:23 +05302138 {
Mayank Grover1ceaee22019-04-01 17:54:32 +05302139 offset = imagesize_actual;
2140 if (check_aboot_addr_range_overlap((uintptr_t)image_addr + offset, page_size))
2141 {
2142 dprintf(CRITICAL, "Signature read buffer address overlaps with aboot addresses.\n");
2143 return -1;
2144 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08002145
Mayank Grover1ceaee22019-04-01 17:54:32 +05302146 /* Read signature */
2147 if(mmc_read(ptn + offset, (void *)(image_addr + offset), page_size))
2148 {
2149 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
2150 return -1;
2151 }
Monika Singh292b3e92018-03-17 22:40:23 +05302152 }
2153
Mayank Grover027a9412018-09-04 15:12:05 +05302154 /* load and validate dtbo partition */
2155 load_validate_dtbo_image(&dtbo_image_buf, &dtbo_image_sz);
2156
Mayank Grover9df84c02018-08-30 15:46:35 +05302157 /* load vbmeta partition */
2158 load_vbmeta_image(&vbmeta_image_buf, &vbmeta_image_sz);
2159
Monika Singh292b3e92018-03-17 22:40:23 +05302160 memset(&info, 0, sizeof(bootinfo));
Mayank Grover027a9412018-09-04 15:12:05 +05302161
2162 /* Pass loaded boot image passed */
Mayank Grover6fd36792018-09-10 13:26:58 +05302163 info.images[IMG_BOOT].image_buffer = SUB_SALT_BUFF_OFFSET(image_addr);
Mayank Grover027a9412018-09-04 15:12:05 +05302164 info.images[IMG_BOOT].imgsize = imagesize_actual;
2165 info.images[IMG_BOOT].name = ptn_name;
2166 ++info.num_loaded_images;
2167
2168 /* Pass loaded dtbo image */
2169 if (dtbo_image_buf != NULL) {
Mayank Grover6fd36792018-09-10 13:26:58 +05302170 info.images[IMG_DTBO].image_buffer =
2171 SUB_SALT_BUFF_OFFSET(dtbo_image_buf);
Mayank Grover027a9412018-09-04 15:12:05 +05302172 info.images[IMG_DTBO].imgsize = dtbo_image_sz;
2173 info.images[IMG_DTBO].name = "dtbo";
2174 ++info.num_loaded_images;
2175 }
2176
Mayank Grover9df84c02018-08-30 15:46:35 +05302177 /* Pass loaded vbmeta image */
2178 if (vbmeta_image_buf != NULL) {
2179 info.images[IMG_VBMETA].image_buffer = vbmeta_image_buf;
2180 info.images[IMG_VBMETA].imgsize = vbmeta_image_sz;
2181 info.images[IMG_VBMETA].name = "vbmeta";
2182 ++info.num_loaded_images;
2183 }
2184
Mayank Grovere1ab96c2018-09-04 20:31:31 +05302185 info.header_version = hdr->header_version;
Monika Singh292b3e92018-03-17 22:40:23 +05302186 info.multi_slot_boot = partition_multislot_is_supported();
2187 info.bootreason_alarm = boot_reason_alarm;
2188 info.bootinto_recovery = boot_into_recovery;
2189 status = load_image_and_auth(&info);
2190 if(status)
2191 return -1;
2192
2193 vbcmdline = info.vbcmdline;
Mayank Grover9df84c02018-08-30 15:46:35 +05302194
2195 /* Free the buffer allocated to vbmeta post verification */
Saranya Chidurab4933332018-10-15 17:30:06 +05302196 if (vbmeta_image_buf != NULL) {
2197 free(vbmeta_image_buf);
2198 --info.num_loaded_images;
2199 }
Monika Singh292b3e92018-03-17 22:40:23 +05302200#else
Channagoud Kadabi736c4962015-08-21 11:56:52 -07002201 /* Change the condition a little bit to include the test framework support.
2202 * We would never reach this point if device is in fastboot mode, even if we did
2203 * that means we are in test mode, so execute kernel authentication part for the
2204 * tests */
Parth Dixitcb0c6082015-12-30 15:01:13 +05302205 if((target_use_signed_kernel() && (!device.is_unlocked)) || is_test_mode_enabled())
Matthew Qin49e51fa2015-02-09 10:40:45 +08002206 {
2207 offset = imagesize_actual;
Vijay Kumar Pendoti9c002ad2016-03-09 13:52:45 +05302208 if (check_aboot_addr_range_overlap((uintptr_t)image_addr + offset, page_size))
Matthew Qin49e51fa2015-02-09 10:40:45 +08002209 {
2210 dprintf(CRITICAL, "Signature read buffer address overlaps with aboot addresses.\n");
2211 return -1;
2212 }
2213
2214 /* Read signature */
2215 if(mmc_read(ptn + offset, (void *)(image_addr + offset), page_size))
2216 {
2217 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
2218 return -1;
2219 }
2220
2221 verify_signed_bootimg((uint32_t)image_addr, imagesize_actual);
Channagoud Kadabi736c4962015-08-21 11:56:52 -07002222 /* The purpose of our test is done here */
Parth Dixitcb0c6082015-12-30 15:01:13 +05302223 if(is_test_mode_enabled() && auth_kernel_img)
Channagoud Kadabi736c4962015-08-21 11:56:52 -07002224 return 0;
Matthew Qin49e51fa2015-02-09 10:40:45 +08002225 } else {
2226 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
2227 #ifdef TZ_SAVE_KERNEL_HASH
2228 aboot_save_boot_hash_mmc((uint32_t) image_addr, imagesize_actual);
2229 #endif /* TZ_SAVE_KERNEL_HASH */
Amit Blay4aa292f2015-04-28 21:55:59 +03002230
2231#ifdef MDTP_SUPPORT
2232 {
2233 /* Verify MDTP lock.
2234 * For boot & recovery partitions, MDTP will use boot_verifier APIs,
2235 * since verification was skipped in aboot. The signature is not part of the loaded image.
2236 */
2237 mdtp_ext_partition_verification_t ext_partition;
2238 ext_partition.partition = boot_into_recovery ? MDTP_PARTITION_RECOVERY : MDTP_PARTITION_BOOT;
2239 ext_partition.integrity_state = MDTP_PARTITION_STATE_UNSET;
2240 ext_partition.page_size = page_size;
2241 ext_partition.image_addr = (uint32)image_addr;
2242 ext_partition.image_size = imagesize_actual;
2243 ext_partition.sig_avail = FALSE;
2244 mdtp_fwlock_verify_lock(&ext_partition);
2245 }
2246#endif /* MDTP_SUPPORT */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08002247 }
Monika Singh292b3e92018-03-17 22:40:23 +05302248#endif
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08002249
Sridhar Parasuram00bfedb2015-05-26 14:21:27 -07002250#if VERIFIED_BOOT
taozhang93088bd2016-11-16 15:50:46 +08002251 if((boot_verify_get_state() == ORANGE) && (!boot_into_ffbm))
Reut Zysmanba8a9d52016-02-18 11:44:04 +02002252 {
2253#if FBCON_DISPLAY_MSG
2254 display_bootverify_menu(DISPLAY_MENU_ORANGE);
2255 wait_for_users_action();
2256#else
2257 dprintf(CRITICAL,
2258 "Your device has been unlocked and can't be trusted.\nWait for 5 seconds before proceeding\n");
2259 mdelay(5000);
2260#endif
2261 }
2262#endif
2263
2264#if VERIFIED_BOOT
Monika Singh292b3e92018-03-17 22:40:23 +05302265 if (VB_M == target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +05302266 {
2267 /* set boot and system versions. */
2268 set_os_version((unsigned char *)image_addr);
2269 // send root of trust
2270 if(!send_rot_command((uint32_t)device.is_unlocked))
2271 ASSERT(0);
2272 }
Parth Dixitbc9b6492015-10-18 00:41:38 +05302273#endif
Channagoud Kadabia22144f2013-03-20 11:49:01 -07002274 /*
Matthew Qinbb7923d2015-02-09 10:56:09 +08002275 * Check if the kernel image is a gzip package. If yes, need to decompress it.
2276 * If not, continue booting.
2277 */
2278 if (is_gzip_package((unsigned char *)(image_addr + page_size), hdr->kernel_size))
2279 {
2280 out_addr = (unsigned char *)(image_addr + imagesize_actual + page_size);
2281 out_avai_len = target_get_max_flash_size() - imagesize_actual - page_size;
Mayank Grover027a9412018-09-04 15:12:05 +05302282#if VERIFIED_BOOT_2
2283 if (dtbo_image_sz)
2284 out_avai_len -= DTBO_IMG_BUF;
2285#endif
Matthew Qin0b15b322015-05-19 05:20:54 -04002286 dprintf(INFO, "decompressing kernel image: start\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08002287 rc = decompress((unsigned char *)(image_addr + page_size),
2288 hdr->kernel_size, out_addr, out_avai_len,
2289 &dtb_offset, &out_len);
2290 if (rc)
2291 {
Matthew Qin0b15b322015-05-19 05:20:54 -04002292 dprintf(CRITICAL, "decompressing kernel image failed!!!\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08002293 ASSERT(0);
2294 }
2295
Matthew Qin0b15b322015-05-19 05:20:54 -04002296 dprintf(INFO, "decompressing kernel image: done\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08002297 kptr = (struct kernel64_hdr *)out_addr;
2298 kernel_start_addr = out_addr;
2299 kernel_size = out_len;
2300 } else {
Ameya Thakur10a33452016-06-13 14:24:26 -07002301 dprintf(INFO, "Uncpmpressed kernel in use\n");
2302 if (!strncmp((char*)(image_addr + page_size),
2303 PATCHED_KERNEL_MAGIC,
2304 sizeof(PATCHED_KERNEL_MAGIC) - 1)) {
2305 dprintf(INFO, "Patched kernel detected\n");
2306 kptr = (struct kernel64_hdr *)(image_addr + page_size +
2307 PATCHED_KERNEL_HEADER_SIZE);
2308 //The size of the kernel is stored at start of kernel image + 16
2309 //The dtb would start just after the kernel
2310 dtb_offset = *((uint32_t*)((unsigned char*)
2311 (image_addr + page_size +
2312 sizeof(PATCHED_KERNEL_MAGIC) -
2313 1)));
2314 //The actual kernel starts after the 20 byte header.
2315 kernel_start_addr = (unsigned char*)(image_addr +
2316 page_size + PATCHED_KERNEL_HEADER_SIZE);
2317 kernel_size = hdr->kernel_size;
2318 patched_kernel_hdr_size = PATCHED_KERNEL_HEADER_SIZE;
2319 } else {
2320 dprintf(INFO, "Kernel image not patched..Unable to locate dt offset\n");
2321 kptr = (struct kernel64_hdr *)(image_addr + page_size);
2322 kernel_start_addr = (unsigned char *)(image_addr + page_size);
2323 kernel_size = hdr->kernel_size;
2324 }
Matthew Qinbb7923d2015-02-09 10:56:09 +08002325 }
2326
2327 /*
Channagoud Kadabia22144f2013-03-20 11:49:01 -07002328 * Update the kernel/ramdisk/tags address if the boot image header
2329 * has default values, these default values come from mkbootimg when
2330 * the boot image is flashed using fastboot flash:raw
2331 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08002332 update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr));
Channagoud Kadabia22144f2013-03-20 11:49:01 -07002333
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002334 /* Get virtual addresses since the hdr saves physical addresses. */
2335 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
2336 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
2337 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002338
Matthew Qinbb7923d2015-02-09 10:56:09 +08002339 kernel_size = ROUND_TO_PAGE(kernel_size, page_mask);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002340 /* Check if the addresses in the header are valid. */
Matthew Qinbb7923d2015-02-09 10:56:09 +08002341 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_size) ||
Mayank Grovere559cec2017-10-17 15:12:03 +05302342 check_ddr_addr_range_bound(hdr->kernel_addr, kernel_size) ||
2343 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual) ||
2344 check_ddr_addr_range_bound(hdr->ramdisk_addr, ramdisk_actual))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002345 {
Mayank Grovere559cec2017-10-17 15:12:03 +05302346 dprintf(CRITICAL, "kernel/ramdisk addresses are not valid.\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002347 return -1;
2348 }
2349
2350#ifndef DEVICE_TREE
Mayank Grovere559cec2017-10-17 15:12:03 +05302351 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE) ||
2352 check_ddr_addr_range_bound(hdr->tags_addr, MAX_TAGS_SIZE))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002353 {
Mayank Grovere559cec2017-10-17 15:12:03 +05302354 dprintf(CRITICAL, "Tags addresses are not valid.\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002355 return -1;
2356 }
2357#endif
2358
Matthew Qin49e51fa2015-02-09 10:40:45 +08002359 /* Move kernel, ramdisk and device tree to correct address */
Matthew Qinbb7923d2015-02-09 10:56:09 +08002360 memmove((void*) hdr->kernel_addr, kernel_start_addr, kernel_size);
Matthew Qin49e51fa2015-02-09 10:40:45 +08002361 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002362
Matthew Qin49e51fa2015-02-09 10:40:45 +08002363 #if DEVICE_TREE
Parth Dixit4097b622016-03-15 14:42:27 +05302364 if(dt_size) {
Matthew Qin49e51fa2015-02-09 10:40:45 +08002365 dt_table_offset = ((uint32_t)image_addr + page_size + kernel_actual + ramdisk_actual + second_actual);
2366 table = (struct dt_table*) dt_table_offset;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002367
Matthew Qin49e51fa2015-02-09 10:40:45 +08002368 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
2369 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
2370 return -1;
2371 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002372
P.V. Phani Kumar82916762016-03-09 09:20:19 +05302373 /* Its Error if, dt_hdr_size (table->num_entries * dt_entry size + Dev_Tree Header)
2374 goes beyound hdr->dt_size*/
Parth Dixit4097b622016-03-15 14:42:27 +05302375 if (dt_hdr_size > ROUND_TO_PAGE(dt_size,hdr->page_size)) {
P.V. Phani Kumar82916762016-03-09 09:20:19 +05302376 dprintf(CRITICAL, "ERROR: Invalid Device Tree size \n");
2377 return -1;
2378 }
2379
Matthew Qin49e51fa2015-02-09 10:40:45 +08002380 /* Find index of device tree within device tree table */
2381 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
2382 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
2383 return -1;
2384 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002385
P.V. Phani Kumar3c333202016-03-09 11:54:37 +05302386 if(dt_entry.offset > (UINT_MAX - dt_entry.size)) {
2387 dprintf(CRITICAL, "ERROR: Device tree contents are Invalid\n");
2388 return -1;
2389 }
2390
2391 /* Ensure we are not overshooting dt_size with the dt_entry selected */
Parth Dixit4097b622016-03-15 14:42:27 +05302392 if ((dt_entry.offset + dt_entry.size) > dt_size) {
P.V. Phani Kumar3c333202016-03-09 11:54:37 +05302393 dprintf(CRITICAL, "ERROR: Device tree contents are Invalid\n");
2394 return -1;
2395 }
2396
Matthew Qin271927e2015-03-31 22:07:07 -04002397 if (is_gzip_package((unsigned char *)dt_table_offset + dt_entry.offset, dt_entry.size))
2398 {
2399 unsigned int compressed_size = 0;
2400 out_addr += out_len;
2401 out_avai_len -= out_len;
Matthew Qin0b15b322015-05-19 05:20:54 -04002402 dprintf(INFO, "decompressing dtb: start\n");
Matthew Qin271927e2015-03-31 22:07:07 -04002403 rc = decompress((unsigned char *)dt_table_offset + dt_entry.offset,
2404 dt_entry.size, out_addr, out_avai_len,
2405 &compressed_size, &dtb_size);
2406 if (rc)
2407 {
Matthew Qin0b15b322015-05-19 05:20:54 -04002408 dprintf(CRITICAL, "decompressing dtb failed!!!\n");
Matthew Qin271927e2015-03-31 22:07:07 -04002409 ASSERT(0);
2410 }
2411
Matthew Qin0b15b322015-05-19 05:20:54 -04002412 dprintf(INFO, "decompressing dtb: done\n");
Matthew Qin271927e2015-03-31 22:07:07 -04002413 best_match_dt_addr = out_addr;
2414 } else {
2415 best_match_dt_addr = (unsigned char *)dt_table_offset + dt_entry.offset;
2416 dtb_size = dt_entry.size;
2417 }
2418
Matthew Qin49e51fa2015-02-09 10:40:45 +08002419 /* Validate and Read device device tree in the tags_addr */
Mayank Grovere559cec2017-10-17 15:12:03 +05302420 if (check_aboot_addr_range_overlap(hdr->tags_addr, dtb_size) ||
2421 check_ddr_addr_range_bound(hdr->tags_addr, dtb_size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002422 {
Mayank Grovere559cec2017-10-17 15:12:03 +05302423 dprintf(CRITICAL, "Device tree addresses are not valid\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002424 return -1;
2425 }
Shashank Mittal162244e2011-08-08 19:01:25 -07002426
Matthew Qin271927e2015-03-31 22:07:07 -04002427 memmove((void *)hdr->tags_addr, (char *)best_match_dt_addr, dtb_size);
Matthew Qin49e51fa2015-02-09 10:40:45 +08002428 } else {
2429 /* Validate the tags_addr */
Mayank Grovere559cec2017-10-17 15:12:03 +05302430 if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual) ||
2431 check_ddr_addr_range_bound(hdr->tags_addr, kernel_actual))
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07002432 {
Mayank Grovere559cec2017-10-17 15:12:03 +05302433 dprintf(CRITICAL, "Device tree addresses are not valid.\n");
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07002434 return -1;
2435 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08002436 /*
2437 * If appended dev tree is found, update the atags with
2438 * memory address to the DTB appended location on RAM.
2439 * Else update with the atags address in the kernel header
Mayank Grover1ed3e8b2018-09-19 15:16:50 +05302440 *
2441 * Make sure everything from scratch address is read before next step!
2442 * In case of dtbo, this API is going to read dtbo on scratch.
Matthew Qin49e51fa2015-02-09 10:40:45 +08002443 */
2444 void *dtb;
Parth Dixit1375d9e2019-07-08 20:56:13 +05302445 image_buf = (void*)(image_addr + page_size + patched_kernel_hdr_size);
2446
Nagireddy Annem0d60bb62019-08-16 19:16:00 +05302447#ifdef OSVERSION_IN_BOOTIMAGE
Parth Dixit1375d9e2019-07-08 20:56:13 +05302448 if ( hdr->header_version == BOOT_HEADER_VERSION_TWO) {
2449
2450 image_buf = (void*)(image_addr);
2451 dtb_offset = dtb_image_offset;
2452 dtb_image_size = imagesize_actual;
2453 }
Nagireddy Annem0d60bb62019-08-16 19:16:00 +05302454#endif
Parth Dixit1375d9e2019-07-08 20:56:13 +05302455
2456 dtb = dev_tree_appended(image_buf, dtb_image_size, dtb_offset,
Ameya Thakur10a33452016-06-13 14:24:26 -07002457 (void *)hdr->tags_addr);
Matthew Qin49e51fa2015-02-09 10:40:45 +08002458 if (!dtb) {
2459 dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07002460 return -1;
2461 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07002462 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08002463 #endif
Shashank Mittal23b8f422010-04-16 19:27:21 -07002464
Stanimir Varbanov69ec5462013-07-18 18:17:42 +03002465 if (boot_into_recovery && !device.is_unlocked && !device.is_tampered)
2466 target_load_ssd_keystore();
2467
Shashank Mittal23b8f422010-04-16 19:27:21 -07002468unified_boot:
Shashank Mittal23b8f422010-04-16 19:27:21 -07002469
Dima Zavin77e41f32013-03-06 16:10:43 -08002470 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07002471 (const char *)hdr->cmdline, board_machtype(),
Shashank Mittal23b8f422010-04-16 19:27:21 -07002472 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
2473
2474 return 0;
2475}
2476
Dima Zavin214cc642009-01-26 11:16:21 -08002477int boot_linux_from_flash(void)
2478{
Mayank Grovere1ab96c2018-09-04 20:31:31 +05302479 boot_img_hdr *hdr = (void*) buf;
Dima Zavin214cc642009-01-26 11:16:21 -08002480 struct ptentry *ptn;
2481 struct ptable *ptable;
2482 unsigned offset = 0;
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08002483
Shashank Mittalcd98d472011-08-02 14:29:24 -07002484 unsigned char *image_addr = 0;
2485 unsigned kernel_actual;
2486 unsigned ramdisk_actual;
2487 unsigned imagesize_actual;
vijay kumar8f53e362015-11-24 13:38:11 +05302488 unsigned second_actual = 0;
Shashank Mittalcd98d472011-08-02 14:29:24 -07002489
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07002490#if DEVICE_TREE
Monika Singh292b3e92018-03-17 22:40:23 +05302491 struct dt_table *table = NULL;
Joel Kingaa335dc2013-06-03 16:11:08 -07002492 struct dt_entry dt_entry;
vijay kumar8f53e362015-11-24 13:38:11 +05302493 unsigned dt_table_offset;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08002494 uint32_t dt_actual;
Monika Singh292b3e92018-03-17 22:40:23 +05302495 uint32_t dt_hdr_size = 0;
Vivek Kumar07bd0862018-04-10 14:19:23 +05302496 uint32_t dtb_offset = 0;
vijay kumar8f53e362015-11-24 13:38:11 +05302497 unsigned int dtb_size = 0;
2498 unsigned char *best_match_dt_addr = NULL;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07002499#endif
2500
David Ng183a7422009-12-07 14:55:21 -08002501 if (target_is_emmc_boot()) {
Mayank Grovere1ab96c2018-09-04 20:31:31 +05302502 hdr = (boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
David Ng183a7422009-12-07 14:55:21 -08002503 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
2504 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
2505 return -1;
2506 }
2507 goto continue_boot;
2508 }
2509
Dima Zavin214cc642009-01-26 11:16:21 -08002510 ptable = flash_get_ptable();
2511 if (ptable == NULL) {
2512 dprintf(CRITICAL, "ERROR: Partition table not found\n");
2513 return -1;
2514 }
2515
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08002516 if(!boot_into_recovery)
2517 {
2518 ptn = ptable_find(ptable, "boot");
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07002519
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08002520 if (ptn == NULL) {
2521 dprintf(CRITICAL, "ERROR: No boot partition found\n");
2522 return -1;
2523 }
2524 }
2525 else
2526 {
2527 ptn = ptable_find(ptable, "recovery");
2528 if (ptn == NULL) {
2529 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
2530 return -1;
2531 }
Dima Zavin214cc642009-01-26 11:16:21 -08002532 }
2533
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05302534 /* Read boot.img header from flash */
Shashank Mittaldcc2e352009-11-19 19:11:16 -08002535 if (flash_read(ptn, offset, buf, page_size)) {
Dima Zavin214cc642009-01-26 11:16:21 -08002536 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
2537 return -1;
2538 }
Dima Zavin214cc642009-01-26 11:16:21 -08002539
2540 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07002541 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Dima Zavin214cc642009-01-26 11:16:21 -08002542 return -1;
2543 }
2544
Shashank Mittaldcc2e352009-11-19 19:11:16 -08002545 if (hdr->page_size != page_size) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07002546 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 -08002547 return -1;
2548 }
2549
Kishor PK9e91b592017-05-24 12:14:55 +05302550 image_addr = (unsigned char *)target_get_scratch_address();
2551 memcpy(image_addr, (void *)buf, page_size);
vijay kumar287bb542015-09-29 13:01:52 +05302552
Channagoud Kadabia22144f2013-03-20 11:49:01 -07002553 /*
2554 * Update the kernel/ramdisk/tags address if the boot image header
2555 * has default values, these default values come from mkbootimg when
2556 * the boot image is flashed using fastboot flash:raw
2557 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08002558 update_ker_tags_rdisk_addr(hdr, false);
Channagoud Kadabia22144f2013-03-20 11:49:01 -07002559
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002560 /* Get virtual addresses since the hdr saves physical addresses. */
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002561 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
2562 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
2563 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
2564
2565 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
2566 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Mayank Grover032736f2017-07-14 20:34:51 +05302567 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002568
Kishor PK9e91b592017-05-24 12:14:55 +05302569 /* ensure commandline is terminated */
2570 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
2571
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002572 /* Check if the addresses in the header are valid. */
2573 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
Mayank Grovere559cec2017-10-17 15:12:03 +05302574 check_ddr_addr_range_bound(hdr->kernel_addr, kernel_actual) ||
2575 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual) ||
2576 check_ddr_addr_range_bound(hdr->ramdisk_addr, ramdisk_actual))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002577 {
Mayank Grovere559cec2017-10-17 15:12:03 +05302578 dprintf(CRITICAL, "kernel/ramdisk addresses are not valid.\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002579 return -1;
2580 }
2581
2582#ifndef DEVICE_TREE
Kishor PKd8ddcad2017-07-27 13:53:57 +05302583 if (UINT_MAX < ((uint64_t)kernel_actual + (uint64_t)ramdisk_actual+ (uint64_t)second_actual + page_size)) {
Mayank Grover032736f2017-07-14 20:34:51 +05302584 dprintf(CRITICAL, "Integer overflow detected in bootimage header fields\n");
2585 return -1;
2586 }
Kishor PKd8ddcad2017-07-27 13:53:57 +05302587 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + second_actual);
Mayank Grover032736f2017-07-14 20:34:51 +05302588
Mayank Grovere559cec2017-10-17 15:12:03 +05302589 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE) ||
2590 check_ddr_addr_range_bound(hdr->tags_addr, MAX_TAGS_SIZE))
Vijay Kumar Pendotid3ed20e2016-09-20 00:34:46 +05302591 {
Mayank Grovere559cec2017-10-17 15:12:03 +05302592 dprintf(CRITICAL, "Tags addresses are not valid.\n");
Vijay Kumar Pendotid3ed20e2016-09-20 00:34:46 +05302593 return -1;
2594 }
2595#else
2596
2597#ifndef OSVERSION_IN_BOOTIMAGE
2598 dt_size = hdr->dt_size;
2599#endif
Mayank Grover032736f2017-07-14 20:34:51 +05302600 dt_actual = ROUND_TO_PAGE(dt_size, page_mask);
Kishor PKd8ddcad2017-07-27 13:53:57 +05302601 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 +05302602 dprintf(CRITICAL, "Integer overflow detected in bootimage header fields\n");
2603 return -1;
2604 }
2605
Kishor PKd8ddcad2017-07-27 13:53:57 +05302606 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + second_actual + dt_actual);
Mayank Grover032736f2017-07-14 20:34:51 +05302607
Mayank Grovere559cec2017-10-17 15:12:03 +05302608 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_size) ||
2609 check_ddr_addr_range_bound(hdr->tags_addr, dt_size))
Mayank Grover032736f2017-07-14 20:34:51 +05302610 {
Mayank Grovere559cec2017-10-17 15:12:03 +05302611 dprintf(CRITICAL, "Device tree addresses are not valid.\n");
Mayank Grover032736f2017-07-14 20:34:51 +05302612 return -1;
2613 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002614#endif
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002615
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05302616 /* Read full boot.img from flash */
2617 dprintf(INFO, "Loading (%s) image (%d): start\n",
2618 (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual);
2619 bs_set_timestamp(BS_KERNEL_LOAD_START);
2620
2621 if (UINT_MAX - page_size < imagesize_actual)
2622 {
2623 dprintf(CRITICAL,"Integer overflow detected in bootimage header fields %u %s\n", __LINE__,__func__);
2624 return -1;
2625 }
2626
2627 /*Check the availability of RAM before reading boot image + max signature length from flash*/
2628 if (target_get_max_flash_size() < (imagesize_actual + page_size))
2629 {
2630 dprintf(CRITICAL, "bootimage size is greater than DDR can hold\n");
2631 return -1;
2632 }
2633
2634 offset = page_size;
2635 /* Read image without signature and header */
2636 if (flash_read(ptn, offset, (void *)(image_addr + offset), imagesize_actual - page_size))
2637 {
2638 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
2639 return -1;
2640 }
2641
2642 dprintf(INFO, "Loading (%s) image (%d): done\n",
2643 (!boot_into_recovery ? "boot" : "recovery"), imagesize_actual);
2644 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
2645
Shashank Mittalcd98d472011-08-02 14:29:24 -07002646 /* Authenticate Kernel */
Deepa Dinamani23b60d42013-06-24 18:10:52 -07002647 if(target_use_signed_kernel() && (!device.is_unlocked))
Shashank Mittalcd98d472011-08-02 14:29:24 -07002648 {
Shashank Mittalcd98d472011-08-02 14:29:24 -07002649 offset = imagesize_actual;
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05302650
Shashank Mittalcd98d472011-08-02 14:29:24 -07002651 /* Read signature */
2652 if (flash_read(ptn, offset, (void *)(image_addr + offset), page_size))
2653 {
2654 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002655 return -1;
Shashank Mittalcd98d472011-08-02 14:29:24 -07002656 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07002657
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05302658 verify_signed_bootimg((uint32_t)image_addr, imagesize_actual);
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05302659 }
2660 offset = page_size;
2661 if(hdr->second_size != 0) {
2662 if (UINT_MAX - offset < second_actual)
2663 {
2664 dprintf(CRITICAL, "ERROR: Integer overflow in boot image header %s\t%d\n",__func__,__LINE__);
2665 return -1;
vijay kumar8f53e362015-11-24 13:38:11 +05302666 }
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05302667 offset += second_actual;
2668 /* Second image loading not implemented. */
2669 ASSERT(0);
2670 }
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05302671 /* Move kernel and ramdisk to correct address */
2672 memmove((void*) hdr->kernel_addr, (char*) (image_addr + page_size), hdr->kernel_size);
2673 memmove((void*) hdr->ramdisk_addr, (char*) (image_addr + page_size + kernel_actual), hdr->ramdisk_size);
2674
2675#if DEVICE_TREE
2676 if(dt_size != 0) {
2677
2678 dt_table_offset = ((uint32_t)image_addr + page_size + kernel_actual + ramdisk_actual + second_actual);
2679
2680 table = (struct dt_table*) dt_table_offset;
2681
2682 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
2683 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
2684 return -1;
2685 }
2686
2687 /* Its Error if, dt_hdr_size (table->num_entries * dt_entry size + Dev_Tree Header)
2688 goes beyound hdr->dt_size*/
2689 if (dt_hdr_size > ROUND_TO_PAGE(dt_size,hdr->page_size)) {
2690 dprintf(CRITICAL, "ERROR: Invalid Device Tree size \n");
2691 return -1;
2692 }
2693
2694 /* Find index of device tree within device tree table */
2695 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
2696 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
2697 return -1;
2698 }
2699
2700 /* Validate and Read device device tree in the "tags_add */
2701 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size) ||
2702 check_ddr_addr_range_bound(hdr->tags_addr, dt_entry.size))
2703 {
2704 dprintf(CRITICAL, "Device tree addresses are not valid.\n");
2705 return -1;
2706 }
2707
2708 if(dt_entry.offset > (UINT_MAX - dt_entry.size)) {
2709 dprintf(CRITICAL, "ERROR: Device tree contents are Invalid\n");
2710 return -1;
2711 }
2712
2713 /* Ensure we are not overshooting dt_size with the dt_entry selected */
2714 if ((dt_entry.offset + dt_entry.size) > dt_size) {
2715 dprintf(CRITICAL, "ERROR: Device tree contents are Invalid\n");
2716 return -1;
2717 }
2718
2719 best_match_dt_addr = (unsigned char *)table + dt_entry.offset;
2720 dtb_size = dt_entry.size;
2721 memmove((void *)hdr->tags_addr, (char *)best_match_dt_addr, dtb_size);
Vivek Kumar07bd0862018-04-10 14:19:23 +05302722
2723 } else {
2724 /* Validate the tags_addr */
2725 if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual) ||
2726 check_ddr_addr_range_bound(hdr->tags_addr, kernel_actual))
2727 {
2728 dprintf(CRITICAL, "Device tree addresses are not valid.\n");
2729 return -1;
2730 }
2731 /*
2732 * If appended dev tree is found, update the atags with
2733 * memory address to the DTB appended location on RAM.
2734 * Else update with the atags address in the kernel header
Mayank Grover1ed3e8b2018-09-19 15:16:50 +05302735 *
2736 * Make sure everything from scratch address is read before next step!
2737 * In case of dtbo, this API is going to read dtbo on scratch.
Vivek Kumar07bd0862018-04-10 14:19:23 +05302738 */
2739 void *dtb = NULL;
2740 dtb = dev_tree_appended((void*)(image_addr + page_size ),hdr->kernel_size, dtb_offset, (void *)hdr->tags_addr);
2741 if (!dtb) {
2742 dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
2743 return -1;
2744 }
2745 }
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05302746#endif
2747 if(target_use_signed_kernel() && (!device.is_unlocked))
2748 {
Shashank Mittal162244e2011-08-08 19:01:25 -07002749 /* Make sure everything from scratch address is read before next step!*/
Shashank Mittala0032282011-08-26 14:50:11 -07002750 if(device.is_tampered)
Shashank Mittal162244e2011-08-08 19:01:25 -07002751 {
2752 write_device_info_flash(&device);
2753 }
Channagoud Kadabi5c86fe32012-02-16 10:58:48 +05302754#if USE_PCOM_SECBOOT
2755 set_tamper_flag(device.is_tampered);
2756#endif
Shashank Mittalcd98d472011-08-02 14:29:24 -07002757 }
David Ng183a7422009-12-07 14:55:21 -08002758continue_boot:
Dima Zavin214cc642009-01-26 11:16:21 -08002759
Dima Zavin214cc642009-01-26 11:16:21 -08002760 /* TODO: create/pass atags to kernel */
2761
Ajay Dudanie28a6072011-07-01 13:59:46 -07002762 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07002763 (const char *)hdr->cmdline, board_machtype(),
Dima Zavin214cc642009-01-26 11:16:21 -08002764 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
2765
2766 return 0;
2767}
Brian Swetland9c4c0752009-01-25 16:23:50 -08002768
Shashank Mittal162244e2011-08-08 19:01:25 -07002769void write_device_info_mmc(device_info *dev)
2770{
Shashank Mittal162244e2011-08-08 19:01:25 -07002771 unsigned long long ptn = 0;
2772 unsigned long long size;
2773 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002774 uint8_t lun = 0;
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002775 uint32_t ret = 0;
Mayank Groverddd348d2018-01-23 14:07:09 +05302776 uint32_t device_info_sz = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07002777
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002778 if (devinfo_present)
2779 index = partition_get_index("devinfo");
2780 else
2781 index = partition_get_index("aboot");
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07002782
Shashank Mittal162244e2011-08-08 19:01:25 -07002783 ptn = partition_get_offset(index);
2784 if(ptn == 0)
2785 {
2786 return;
2787 }
2788
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002789 lun = partition_get_lun(index);
2790 mmc_set_lun(lun);
2791
Shashank Mittal162244e2011-08-08 19:01:25 -07002792 size = partition_get_size(index);
2793
Mayank Groverddd348d2018-01-23 14:07:09 +05302794 device_info_sz = ROUND_TO_PAGE(sizeof(struct device_info),
2795 mmc_blocksize_mask);
2796 if (device_info_sz == UINT_MAX)
2797 {
2798 dprintf(CRITICAL, "ERROR: Incorrect blocksize of card\n");
2799 return;
2800 }
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07002801
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002802 if (devinfo_present)
Mayank Groverddd348d2018-01-23 14:07:09 +05302803 ret = mmc_write(ptn, device_info_sz, (void *)info_buf);
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002804 else
Mayank Groverddd348d2018-01-23 14:07:09 +05302805 ret = mmc_write((ptn + size - device_info_sz), device_info_sz, (void *)info_buf);
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002806 if (ret)
Shashank Mittal162244e2011-08-08 19:01:25 -07002807 {
2808 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
Sridhar Parasurame1a97922015-07-27 15:31:22 -07002809 ASSERT(0);
Shashank Mittal162244e2011-08-08 19:01:25 -07002810 }
2811}
2812
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002813void read_device_info_mmc(struct device_info *info)
Shashank Mittal162244e2011-08-08 19:01:25 -07002814{
Shashank Mittal162244e2011-08-08 19:01:25 -07002815 unsigned long long ptn = 0;
2816 unsigned long long size;
2817 int index = INVALID_PTN;
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002818 uint32_t ret = 0;
Mayank Groverddd348d2018-01-23 14:07:09 +05302819 uint32_t device_info_sz = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07002820
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002821 if ((index = partition_get_index("devinfo")) < 0)
2822 {
2823 devinfo_present = false;
2824 index = partition_get_index("aboot");
2825 }
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07002826
Shashank Mittal162244e2011-08-08 19:01:25 -07002827 ptn = partition_get_offset(index);
2828 if(ptn == 0)
2829 {
2830 return;
2831 }
2832
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07002833 mmc_set_lun(partition_get_lun(index));
2834
Shashank Mittal162244e2011-08-08 19:01:25 -07002835 size = partition_get_size(index);
2836
Mayank Groverddd348d2018-01-23 14:07:09 +05302837 device_info_sz = ROUND_TO_PAGE(sizeof(struct device_info),
2838 mmc_blocksize_mask);
2839 if (device_info_sz == UINT_MAX)
2840 {
2841 dprintf(CRITICAL, "ERROR: Incorrect blocksize of card\n");
2842 return;
2843 }
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07002844
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002845 if (devinfo_present)
Mayank Groverddd348d2018-01-23 14:07:09 +05302846 ret = mmc_read(ptn, (void *)info_buf, device_info_sz);
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002847 else
Mayank Groverddd348d2018-01-23 14:07:09 +05302848 ret = mmc_read((ptn + size - device_info_sz), (void *)info_buf, device_info_sz);
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002849 if (ret)
Shashank Mittal162244e2011-08-08 19:01:25 -07002850 {
2851 dprintf(CRITICAL, "ERROR: Cannot read device info\n");
Sridhar Parasurame1a97922015-07-27 15:31:22 -07002852 ASSERT(0);
Shashank Mittal162244e2011-08-08 19:01:25 -07002853 }
Shashank Mittal162244e2011-08-08 19:01:25 -07002854}
2855
tracychuid184b912020-06-05 17:31:38 +08002856/*[20200605][TracyChui] Implement get Serial Number start*/
2857#if defined(ENABLE_PRODINFO_ACCESS)
2858void write_prod_info_mmc(prod_info *dev)
2859{
2860 unsigned long long ptn = 0;
2861 unsigned long long size;
2862 int index = INVALID_PTN;
2863 uint32_t blocksize;
2864 uint8_t lun = 0;
2865 uint32_t ret = 0;
2866
2867 if (prodinfo_present)
2868 index = partition_get_index("prodinfo");
2869 else
2870 index = partition_get_index("aboot");
2871
2872 ptn = partition_get_offset(index);
2873 if(ptn == 0)
2874 {
2875 return;
2876 }
2877
2878 lun = partition_get_lun(index);
2879 mmc_set_lun(lun);
2880
2881 size = partition_get_size(index);
2882
2883 blocksize = mmc_get_device_blocksize();
2884
2885 if (prodinfo_present)
2886 ret = mmc_write(ptn, blocksize, (void *)prodinfo_buf);
2887 else
2888 ret = mmc_write((ptn + size - blocksize), blocksize, (void *)prodinfo_buf);
2889 if (ret)
2890 {
2891 dprintf(CRITICAL, "ERROR: Cannot write prod info\n");
2892 ASSERT(0);
2893 }
2894}
2895
2896void read_prod_info_mmc(struct prod_info *info)
2897{
2898 unsigned long long ptn = 0;
2899 unsigned long long size;
2900 int index = INVALID_PTN;
2901 uint32_t blocksize;
2902 uint32_t ret = 0;
2903
2904 if ((index = partition_get_index("prodinfo")) < 0)
2905 {
2906 prodinfo_present = false;
2907 index = partition_get_index("aboot");
2908 }
2909
2910 ptn = partition_get_offset(index);
2911 if(ptn == 0)
2912 {
2913 return;
2914 }
2915
2916 mmc_set_lun(partition_get_lun(index));
2917
2918 size = partition_get_size(index);
2919
2920 blocksize = mmc_get_device_blocksize();
2921
2922 if (prodinfo_present)
2923 ret = mmc_read(ptn, (void *)prodinfo_buf, blocksize);
2924 else
2925 ret = mmc_read((ptn + size - blocksize), (void *)prodinfo_buf, blocksize);
2926 if (ret)
2927 {
2928 dprintf(CRITICAL, "ERROR: Cannot read prod info\n");
2929 ASSERT(0);
2930 }
2931}
2932#endif
2933/*[20200605][TracyChui] Implement get Serial Number end*/
2934
Shashank Mittal162244e2011-08-08 19:01:25 -07002935void write_device_info_flash(device_info *dev)
2936{
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002937 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
Shashank Mittal162244e2011-08-08 19:01:25 -07002938 struct ptentry *ptn;
2939 struct ptable *ptable;
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002940 if(info == NULL)
2941 {
2942 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
2943 ASSERT(0);
2944 }
2945 info_buf = info;
Shashank Mittal162244e2011-08-08 19:01:25 -07002946 ptable = flash_get_ptable();
2947 if (ptable == NULL)
2948 {
2949 dprintf(CRITICAL, "ERROR: Partition table not found\n");
2950 return;
2951 }
2952
2953 ptn = ptable_find(ptable, "devinfo");
2954 if (ptn == NULL)
2955 {
Smita Ghosh670c8b82015-05-07 09:30:03 -07002956 dprintf(CRITICAL, "ERROR: No devinfo partition found\n");
Shashank Mittal162244e2011-08-08 19:01:25 -07002957 return;
2958 }
2959
Mayank Groverdedbc892017-10-24 13:41:34 +05302960 memset(info, 0, BOOT_IMG_MAX_PAGE_SIZE);
Shashank Mittal162244e2011-08-08 19:01:25 -07002961 memcpy(info, dev, sizeof(device_info));
2962
2963 if (flash_write(ptn, 0, (void *)info_buf, page_size))
2964 {
2965 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
2966 return;
2967 }
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002968 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07002969}
2970
vijay kumarc65876c2015-04-24 13:29:16 +05302971static int read_allow_oem_unlock(device_info *dev)
2972{
vijay kumarc65876c2015-04-24 13:29:16 +05302973 unsigned offset;
2974 int index;
2975 unsigned long long ptn;
2976 unsigned long long ptn_size;
2977 unsigned blocksize = mmc_get_device_blocksize();
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07002978 STACKBUF_DMA_ALIGN(buf, blocksize);
vijay kumarc65876c2015-04-24 13:29:16 +05302979
vijay kumarca2e6812015-07-08 20:28:25 +05302980 index = partition_get_index(frp_ptns[0]);
vijay kumarc65876c2015-04-24 13:29:16 +05302981 if (index == INVALID_PTN)
2982 {
vijay kumarca2e6812015-07-08 20:28:25 +05302983 index = partition_get_index(frp_ptns[1]);
2984 if (index == INVALID_PTN)
2985 {
2986 dprintf(CRITICAL, "Neither '%s' nor '%s' partition found\n", frp_ptns[0],frp_ptns[1]);
2987 return -1;
2988 }
vijay kumarc65876c2015-04-24 13:29:16 +05302989 }
2990
2991 ptn = partition_get_offset(index);
2992 ptn_size = partition_get_size(index);
2993 offset = ptn_size - blocksize;
2994
Mayank Grover48860402016-11-29 12:34:53 +05302995 /* Set Lun for partition */
2996 mmc_set_lun(partition_get_lun(index));
2997
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07002998 if (mmc_read(ptn + offset, (void *)buf, blocksize))
vijay kumarc65876c2015-04-24 13:29:16 +05302999 {
3000 dprintf(CRITICAL, "Reading MMC failed\n");
3001 return -1;
3002 }
3003
3004 /*is_allow_unlock is a bool value stored at the LSB of last byte*/
3005 is_allow_unlock = buf[blocksize-1] & 0x01;
3006 return 0;
3007}
3008
3009static int write_allow_oem_unlock(bool allow_unlock)
3010{
vijay kumarc65876c2015-04-24 13:29:16 +05303011 unsigned offset;
vijay kumarc65876c2015-04-24 13:29:16 +05303012 int index;
3013 unsigned long long ptn;
3014 unsigned long long ptn_size;
3015 unsigned blocksize = mmc_get_device_blocksize();
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07003016 STACKBUF_DMA_ALIGN(buf, blocksize);
vijay kumarc65876c2015-04-24 13:29:16 +05303017
vijay kumarca2e6812015-07-08 20:28:25 +05303018 index = partition_get_index(frp_ptns[0]);
vijay kumarc65876c2015-04-24 13:29:16 +05303019 if (index == INVALID_PTN)
3020 {
vijay kumarca2e6812015-07-08 20:28:25 +05303021 index = partition_get_index(frp_ptns[1]);
3022 if (index == INVALID_PTN)
3023 {
3024 dprintf(CRITICAL, "Neither '%s' nor '%s' partition found\n", frp_ptns[0],frp_ptns[1]);
3025 return -1;
3026 }
vijay kumarc65876c2015-04-24 13:29:16 +05303027 }
3028
3029 ptn = partition_get_offset(index);
3030 ptn_size = partition_get_size(index);
3031 offset = ptn_size - blocksize;
Mayank Grover48860402016-11-29 12:34:53 +05303032 mmc_set_lun(partition_get_lun(index));
vijay kumarc65876c2015-04-24 13:29:16 +05303033
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07003034 if (mmc_read(ptn + offset, (void *)buf, blocksize))
vijay kumarc65876c2015-04-24 13:29:16 +05303035 {
3036 dprintf(CRITICAL, "Reading MMC failed\n");
3037 return -1;
3038 }
3039
3040 /*is_allow_unlock is a bool value stored at the LSB of last byte*/
3041 buf[blocksize-1] = allow_unlock;
3042 if (mmc_write(ptn + offset, blocksize, buf))
3043 {
3044 dprintf(CRITICAL, "Writing MMC failed\n");
3045 return -1;
3046 }
3047
3048 return 0;
3049}
3050
Shashank Mittal162244e2011-08-08 19:01:25 -07003051void read_device_info_flash(device_info *dev)
3052{
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07003053 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
Shashank Mittal162244e2011-08-08 19:01:25 -07003054 struct ptentry *ptn;
3055 struct ptable *ptable;
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07003056 if(info == NULL)
3057 {
3058 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
3059 ASSERT(0);
3060 }
3061 info_buf = info;
Shashank Mittal162244e2011-08-08 19:01:25 -07003062 ptable = flash_get_ptable();
3063 if (ptable == NULL)
3064 {
3065 dprintf(CRITICAL, "ERROR: Partition table not found\n");
3066 return;
3067 }
3068
3069 ptn = ptable_find(ptable, "devinfo");
3070 if (ptn == NULL)
3071 {
Smita Ghosh670c8b82015-05-07 09:30:03 -07003072 dprintf(CRITICAL, "ERROR: No devinfo partition found\n");
Shashank Mittal162244e2011-08-08 19:01:25 -07003073 return;
3074 }
3075
3076 if (flash_read(ptn, 0, (void *)info_buf, page_size))
3077 {
3078 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
3079 return;
3080 }
3081
3082 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
3083 {
Shashank Mittal162244e2011-08-08 19:01:25 -07003084 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
3085 info->is_unlocked = 0;
Shashank Mittala0032282011-08-26 14:50:11 -07003086 info->is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07003087 write_device_info_flash(info);
3088 }
3089 memcpy(dev, info, sizeof(device_info));
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07003090 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07003091}
3092
3093void write_device_info(device_info *dev)
3094{
3095 if(target_is_emmc_boot())
3096 {
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07003097 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
3098 if(info == NULL)
3099 {
3100 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
3101 ASSERT(0);
3102 }
3103 info_buf = info;
Channagoud Kadabi036c6052015-02-09 15:19:59 -08003104 memcpy(info, dev, sizeof(struct device_info));
3105
3106#if USE_RPMB_FOR_DEVINFO
Mayank Grover912eaa62017-10-26 12:08:53 +05303107 if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05303108 is_secure_boot_enable()) {
3109 if((write_device_info_rpmb((void*) info, PAGE_SIZE)) < 0)
3110 ASSERT(0);
Sridhar Parasurame1a97922015-07-27 15:31:22 -07003111 }
Channagoud Kadabic80cb492015-04-28 16:08:28 -07003112 else
3113 write_device_info_mmc(info);
Channagoud Kadabi036c6052015-02-09 15:19:59 -08003114#else
3115 write_device_info_mmc(info);
3116#endif
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07003117 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07003118 }
3119 else
3120 {
3121 write_device_info_flash(dev);
3122 }
3123}
3124
Monika Singh94316462018-03-15 18:39:01 +05303125int read_rollback_index(uint32_t loc, uint64_t *roll_back_index)
3126{
3127 if (!devinfo_present) {
3128 dprintf(CRITICAL, "DeviceInfo not initalized \n");
3129 return -EINVAL;
3130 }
3131 if (loc >= ARRAY_SIZE(device.rollback_index)) {
3132 dprintf(CRITICAL, "%s() Loc out of range index: %d, array len: %d\n",
3133 __func__, loc, ARRAY_SIZE(device.rollback_index));
3134 ASSERT(0);
3135 }
3136
3137 *roll_back_index = device.rollback_index[loc];
3138 return 0;
3139}
3140
3141int write_rollback_index(uint32_t loc, uint64_t roll_back_index)
3142{
3143 if (!devinfo_present) {
3144 dprintf(CRITICAL, "DeviceInfo not initalized \n");
3145 return -EINVAL;
3146 }
3147 if (loc >= ARRAY_SIZE(device.rollback_index)) {
3148 dprintf(CRITICAL, "%s() Loc out of range index: %d, array len: %d\n",
3149 __func__, loc, ARRAY_SIZE(device.rollback_index));
3150 ASSERT(0);
3151 }
3152
3153 device.rollback_index[loc] = roll_back_index;
3154 write_device_info(&device);
3155 return 0;
3156}
3157
Monika Singhb0fad822018-03-15 18:50:55 +05303158int store_userkey(uint8_t *user_key, uint32_t user_key_size)
3159{
3160 if (!devinfo_present) {
3161 dprintf(CRITICAL, "DeviceInfo not initalized \n");
3162 return -EINVAL;
3163 }
3164
3165 if (user_key_size > ARRAY_SIZE(device.user_public_key)) {
3166 dprintf(CRITICAL, "StoreUserKey, UserKeySize too large!\n");
3167 return -ENODEV;
3168 }
3169
3170 memcpy(device.user_public_key, user_key, user_key_size);
3171 device.user_public_key_length = user_key_size;
3172 write_device_info(&device);
3173 return 0;
3174}
3175
3176int erase_userkey()
3177{
3178 if (!devinfo_present) {
3179 dprintf(CRITICAL, "DeviceInfo not initalized \n");
3180 return -EINVAL;
3181 }
3182 memset(device.user_public_key, 0, ARRAY_SIZE(device.user_public_key));
3183 device.user_public_key_length = 0;
3184 write_device_info(&device);
3185 return 0;
3186}
3187
3188int get_userkey(uint8_t **user_key, uint32_t *user_key_size)
3189{
3190 if (!devinfo_present) {
3191 dprintf(CRITICAL, "DeviceInfo not initalized \n");
3192 return -EINVAL;
3193 }
3194 *user_key = device.user_public_key;
3195 *user_key_size = device.user_public_key_length;
3196 return 0;
3197}
3198
Shashank Mittal162244e2011-08-08 19:01:25 -07003199void read_device_info(device_info *dev)
3200{
3201 if(target_is_emmc_boot())
3202 {
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07003203 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
3204 if(info == NULL)
3205 {
3206 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
3207 ASSERT(0);
3208 }
3209 info_buf = info;
Channagoud Kadabi036c6052015-02-09 15:19:59 -08003210
3211#if USE_RPMB_FOR_DEVINFO
Mayank Grover912eaa62017-10-26 12:08:53 +05303212 if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05303213 is_secure_boot_enable()) {
3214 if((read_device_info_rpmb((void*) info, PAGE_SIZE)) < 0)
3215 ASSERT(0);
Sridhar Parasurame1a97922015-07-27 15:31:22 -07003216 }
Channagoud Kadabic80cb492015-04-28 16:08:28 -07003217 else
3218 read_device_info_mmc(info);
Channagoud Kadabi036c6052015-02-09 15:19:59 -08003219#else
3220 read_device_info_mmc(info);
3221#endif
3222
3223 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
3224 {
3225 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
lijuang511a2b52015-08-14 20:50:51 +08003226 if (is_secure_boot_enable()) {
Channagoud Kadabi05f78ba2015-07-06 11:58:14 -07003227 info->is_unlocked = 0;
Monika Singh292b3e92018-03-17 22:40:23 +05303228#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover912eaa62017-10-26 12:08:53 +05303229 if (VB_M <= target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +05303230 info->is_unlock_critical = 0;
Parth Dixitddbc7352015-10-18 03:13:31 +05303231#endif
lijuang511a2b52015-08-14 20:50:51 +08003232 } else {
Channagoud Kadabi2fda4092015-07-07 13:34:11 -07003233 info->is_unlocked = 1;
Monika Singh292b3e92018-03-17 22:40:23 +05303234#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover912eaa62017-10-26 12:08:53 +05303235 if (VB_M <= target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +05303236 info->is_unlock_critical = 1;
Parth Dixitddbc7352015-10-18 03:13:31 +05303237#endif
lijuang511a2b52015-08-14 20:50:51 +08003238 }
Channagoud Kadabi036c6052015-02-09 15:19:59 -08003239 info->is_tampered = 0;
3240 info->charger_screen_enabled = 0;
Monika Singh292b3e92018-03-17 22:40:23 +05303241#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover912eaa62017-10-26 12:08:53 +05303242 if (VB_M <= target_get_vb_version())
Monika Singh1c71a1c2019-12-03 12:12:48 +05303243 {
Mayank Grover889be1b2017-09-12 20:12:23 +05303244 info->verity_mode = 1; //enforcing by default
Monika Singh1c71a1c2019-12-03 12:12:48 +05303245 info->user_public_key_length = 0;
3246 memset(info->rollback_index, 0, sizeof(info->rollback_index));
3247 memset(info->user_public_key, 0, sizeof(info->user_public_key));
3248 }
Parth Dixitddbc7352015-10-18 03:13:31 +05303249#endif
Channagoud Kadabi036c6052015-02-09 15:19:59 -08003250 write_device_info(info);
3251 }
jessicatseng1b9da342020-04-22 11:13:20 +08003252
3253//<2020/04/22-JessicaTseng, Enable off-charging
3254 info->charger_screen_enabled = 1;
3255 write_device_info(info);
3256//>2020/04/22-JessicaTseng
3257
Channagoud Kadabi036c6052015-02-09 15:19:59 -08003258 memcpy(dev, info, sizeof(device_info));
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07003259 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07003260 }
3261 else
3262 {
3263 read_device_info_flash(dev);
3264 }
3265}
tracychuid184b912020-06-05 17:31:38 +08003266/*[20200605][TracyChui] Implement get Serial Number start*/
3267#if defined(ENABLE_PRODINFO_ACCESS)
3268void write_prod_info(prod_info *dev)
3269{
3270 if(target_is_emmc_boot())
3271 {
3272 struct prod_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
3273 if(info == NULL)
3274 {
3275 dprintf(CRITICAL, "Failed to allocate memory for prod info struct\n");
3276 ASSERT(0);
3277 }
3278 prodinfo_buf = info;
3279 memcpy(info, dev, sizeof(struct prod_info));
3280
3281 write_prod_info_mmc(info);
3282 free(info);
3283 }
3284}
3285
3286void read_prod_info(prod_info *dev)
3287{
3288 if(target_is_emmc_boot())
3289 {
3290 struct prod_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
3291 if(info == NULL)
3292 {
3293 dprintf(CRITICAL, "Failed to allocate memory for prod info struct\n");
3294 ASSERT(0);
3295 }
3296 prodinfo_buf = info;
3297
3298 read_prod_info_mmc(info);
3299
3300 if (memcmp(info->magic, PRODINFO_MAGIC, PRODINFO_MAGIC_SIZE))
3301 {
3302 memcpy(info->magic, PRODINFO_MAGIC, PRODINFO_MAGIC_SIZE);
3303 memcpy(info->isn, "No_Serial_Number", PRODINFO_MAX_ISN_LEN);
3304 memcpy(info->ssn, "No_Custer_Serial_Number", PRODINFO_MAX_SSN_LEN);
3305 info->is_adb_enabled = 0;
Allen Lu50e75472020-02-27 11:14:29 +08003306 info->is_adb_enabled_cts = 0;
tracychuid184b912020-06-05 17:31:38 +08003307 write_prod_info(info);
3308 }
3309 memcpy(dev, info, sizeof(prod_info));
3310 free(info);
3311 }
3312}
3313#endif
3314/*[20200605][TracyChui] Implement get Serial Number end*/
Shashank Mittal162244e2011-08-08 19:01:25 -07003315
3316void reset_device_info()
3317{
3318 dprintf(ALWAYS, "reset_device_info called.");
Shashank Mittala0032282011-08-26 14:50:11 -07003319 device.is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07003320 write_device_info(&device);
3321}
3322
3323void set_device_root()
3324{
3325 dprintf(ALWAYS, "set_device_root called.");
Shashank Mittala0032282011-08-26 14:50:11 -07003326 device.is_tampered = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -07003327 write_device_info(&device);
3328}
3329
lijuang4ece1e72015-08-14 21:02:36 +08003330/* set device unlock value
3331 * Must check FRP before call this function
3332 * Need to wipe data when unlock status changed
3333 * type 0: oem unlock
3334 * type 1: unlock critical
3335 * status 0: unlock as false
3336 * status 1: lock as true
3337 */
3338void set_device_unlock_value(int type, bool status)
lijuang21f12f52015-08-22 16:22:19 +08003339{
lijuang4ece1e72015-08-14 21:02:36 +08003340 if (type == UNLOCK)
3341 device.is_unlocked = status;
Monika Singh292b3e92018-03-17 22:40:23 +05303342#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover912eaa62017-10-26 12:08:53 +05303343 else if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05303344 type == UNLOCK_CRITICAL)
3345 device.is_unlock_critical = status;
Parth Dixitddbc7352015-10-18 03:13:31 +05303346#endif
lijuang4ece1e72015-08-14 21:02:36 +08003347 write_device_info(&device);
3348}
3349
3350static void set_device_unlock(int type, bool status)
3351{
3352 int is_unlocked = -1;
3353 char response[MAX_RSP_SIZE];
3354
3355 /* check device unlock status if it is as expected */
3356 if (type == UNLOCK)
3357 is_unlocked = device.is_unlocked;
Monika Singh292b3e92018-03-17 22:40:23 +05303358#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover912eaa62017-10-26 12:08:53 +05303359 if(VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05303360 type == UNLOCK_CRITICAL)
3361 {
3362 is_unlocked = device.is_unlock_critical;
3363 }
Parth Dixitddbc7352015-10-18 03:13:31 +05303364#endif
lijuang4ece1e72015-08-14 21:02:36 +08003365 if (is_unlocked == status) {
3366 snprintf(response, sizeof(response), "\tDevice already : %s", (status ? "unlocked!" : "locked!"));
3367 fastboot_info(response);
3368 fastboot_okay("");
3369 return;
3370 }
3371
3372 /* status is true, it means to unlock device */
lijuang07c5d6e2018-04-23 18:32:13 +08003373 if (status && !is_allow_unlock) {
3374 fastboot_fail("oem unlock is not allowed");
3375 return;
3376 }
lijuang21f12f52015-08-22 16:22:19 +08003377
ericlin8ef2adf2020-04-29 14:14:24 +08003378 //<2018/01/09-EricLin, To remove display_unlock_menu for quiet mode.
3379 if(target_build_variant_user()){
lijuang4ece1e72015-08-14 21:02:36 +08003380#if FBCON_DISPLAY_MSG
lijuang07c5d6e2018-04-23 18:32:13 +08003381 display_unlock_menu(type, status);
3382 fastboot_okay("");
3383 return;
lijuang4ece1e72015-08-14 21:02:36 +08003384#else
lijuang07c5d6e2018-04-23 18:32:13 +08003385 if (status && type == UNLOCK) {
3386 fastboot_fail("Need wipe userdata. Do 'fastboot oem unlock-go'");
3387 return;
lijuang21f12f52015-08-22 16:22:19 +08003388 }
lijuang07c5d6e2018-04-23 18:32:13 +08003389#endif
ericlin8ef2adf2020-04-29 14:14:24 +08003390 }
3391 //<2018/01/09-EricLin
lijuang4ece1e72015-08-14 21:02:36 +08003392
3393 set_device_unlock_value(type, status);
3394
3395 /* wipe data */
3396 struct recovery_message msg;
Kishor PK60a68212017-05-08 16:55:57 +05303397 memset(&msg, 0, sizeof(msg));
lijuang4ece1e72015-08-14 21:02:36 +08003398 snprintf(msg.recovery, sizeof(msg.recovery), "recovery\n--wipe_data");
3399 write_misc(0, &msg, sizeof(msg));
3400
3401 fastboot_okay("");
ericlin8ef2adf2020-04-29 14:14:24 +08003402 //<2018/10/16-EricLin, To remove reset after oem lock/unlock.
3403 //reboot_device(RECOVERY_MODE);
3404 //<2018/10/16-EricLin
lijuang21f12f52015-08-22 16:22:19 +08003405}
3406
tracychui8fd71dc2020-06-11 10:17:13 +08003407/*[TracyChui] Add lock unlock fastboot commnad to skip ui check 20200611 start*/
3408#if defined(ENABLE_LOCK_UNLOCK_SKIP_UI_CHECK)
3409static void set_device_unlock_skip_ui_check(int type, bool status)
3410{
3411 int is_unlocked = -1;
3412 char response[MAX_RSP_SIZE];
3413
3414 /* check device unlock status if it is as expected */
3415 if (type == UNLOCK)
3416 is_unlocked = device.is_unlocked;
3417#if VERIFIED_BOOT || VERIFIED_BOOT_2
3418 if(VB_M <= target_get_vb_version() &&
3419 type == UNLOCK_CRITICAL)
3420 {
3421 is_unlocked = device.is_unlock_critical;
3422 }
3423#endif
3424 if (is_unlocked == status) {
3425 snprintf(response, sizeof(response), "\tDevice already : %s", (status ? "unlocked!" : "locked!"));
3426 fastboot_info(response);
3427 fastboot_okay("");
3428 return;
3429 }
3430
3431 set_device_unlock_value(type, status);
3432 fastboot_okay("");
3433}
3434#endif
tracychui3b4abb72020-06-15 10:50:49 +08003435/*[TracyChui] Add lock unlock fastboot commnad to skip ui check 20200611 end */
tracychui8fd71dc2020-06-11 10:17:13 +08003436
lijuang511a2b52015-08-14 20:50:51 +08003437static bool critical_flash_allowed(const char * entry)
3438{
3439 uint32_t i = 0;
3440 if (entry == NULL)
3441 return false;
3442
3443 for (i = 0; i < ARRAY_SIZE(critical_flash_allowed_ptn); i++) {
3444 if(!strcmp(entry, critical_flash_allowed_ptn[i]))
3445 return true;
3446 }
3447 return false;
Matthew Qin271927e2015-03-31 22:07:07 -04003448}
3449
3450#if DEVICE_TREE
Amol Jadicb524072012-08-09 16:40:18 -07003451int copy_dtb(uint8_t *boot_image_start, unsigned int scratch_offset)
3452{
3453 uint32 dt_image_offset = 0;
Amol Jadicb524072012-08-09 16:40:18 -07003454 uint32_t n;
Monika Singh292b3e92018-03-17 22:40:23 +05303455 struct dt_table *table = NULL;
Amol Jadicb524072012-08-09 16:40:18 -07003456 struct dt_entry dt_entry;
Monika Singh292b3e92018-03-17 22:40:23 +05303457 uint32_t dt_hdr_size = 0;
Amol Jadicb524072012-08-09 16:40:18 -07003458 unsigned int compressed_size = 0;
3459 unsigned int dtb_size = 0;
3460 unsigned int out_avai_len = 0;
3461 unsigned char *out_addr = NULL;
3462 unsigned char *best_match_dt_addr = NULL;
3463 int rc;
3464
Mayank Grovere1ab96c2018-09-04 20:31:31 +05303465 boot_img_hdr *hdr = (boot_img_hdr *) (boot_image_start);
Amol Jadicb524072012-08-09 16:40:18 -07003466
Parth Dixit4097b622016-03-15 14:42:27 +05303467#ifndef OSVERSION_IN_BOOTIMAGE
3468 dt_size = hdr->dt_size;
3469#endif
3470
3471 if(dt_size != 0) {
Amol Jadicb524072012-08-09 16:40:18 -07003472 /* add kernel offset */
3473 dt_image_offset += page_size;
3474 n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
3475 dt_image_offset += n;
3476
3477 /* add ramdisk offset */
3478 n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
3479 dt_image_offset += n;
3480
3481 /* add second offset */
3482 if(hdr->second_size != 0) {
3483 n = ROUND_TO_PAGE(hdr->second_size, page_mask);
3484 dt_image_offset += n;
3485 }
3486
3487 /* offset now point to start of dt.img */
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07003488 table = (struct dt_table*)(boot_image_start + dt_image_offset);
Amol Jadicb524072012-08-09 16:40:18 -07003489
Deepa Dinamani19648b42013-09-05 17:05:55 -07003490 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Amol Jadicb524072012-08-09 16:40:18 -07003491 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
3492 return -1;
3493 }
P.V. Phani Kumar82916762016-03-09 09:20:19 +05303494
3495 /* Its Error if, dt_hdr_size (table->num_entries * dt_entry size + Dev_Tree Header)
3496 goes beyound hdr->dt_size*/
Parth Dixit4097b622016-03-15 14:42:27 +05303497 if (dt_hdr_size > ROUND_TO_PAGE(dt_size,hdr->page_size)) {
P.V. Phani Kumar82916762016-03-09 09:20:19 +05303498 dprintf(CRITICAL, "ERROR: Invalid Device Tree size \n");
3499 return -1;
3500 }
3501
Joel Kingaa335dc2013-06-03 16:11:08 -07003502 /* Find index of device tree within device tree table */
3503 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Amol Jadicb524072012-08-09 16:40:18 -07003504 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
3505 return -1;
3506 }
3507
Matthew Qin271927e2015-03-31 22:07:07 -04003508 best_match_dt_addr = (unsigned char *)boot_image_start + dt_image_offset + dt_entry.offset;
3509 if (is_gzip_package(best_match_dt_addr, dt_entry.size))
3510 {
3511 out_addr = (unsigned char *)target_get_scratch_address() + scratch_offset;
3512 out_avai_len = target_get_max_flash_size() - scratch_offset;
Matthew Qin0b15b322015-05-19 05:20:54 -04003513 dprintf(INFO, "decompressing dtb: start\n");
Matthew Qin271927e2015-03-31 22:07:07 -04003514 rc = decompress(best_match_dt_addr,
3515 dt_entry.size, out_addr, out_avai_len,
3516 &compressed_size, &dtb_size);
3517 if (rc)
3518 {
Matthew Qin0b15b322015-05-19 05:20:54 -04003519 dprintf(CRITICAL, "decompressing dtb failed!!!\n");
Matthew Qin271927e2015-03-31 22:07:07 -04003520 ASSERT(0);
3521 }
3522
Matthew Qin0b15b322015-05-19 05:20:54 -04003523 dprintf(INFO, "decompressing dtb: done\n");
Matthew Qin271927e2015-03-31 22:07:07 -04003524 best_match_dt_addr = out_addr;
3525 } else {
3526 dtb_size = dt_entry.size;
3527 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07003528 /* Validate and Read device device tree in the "tags_add */
Mayank Grovere559cec2017-10-17 15:12:03 +05303529 if (check_aboot_addr_range_overlap(hdr->tags_addr, dtb_size) ||
3530 check_ddr_addr_range_bound(hdr->tags_addr, dtb_size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07003531 {
Mayank Grovere559cec2017-10-17 15:12:03 +05303532 dprintf(CRITICAL, "Device tree addresses are not valid.\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07003533 return -1;
3534 }
3535
Amol Jadicb524072012-08-09 16:40:18 -07003536 /* Read device device tree in the "tags_add */
Matthew Qin271927e2015-03-31 22:07:07 -04003537 memmove((void*) hdr->tags_addr, (void *)best_match_dt_addr, dtb_size);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07003538 } else
3539 return -1;
Amol Jadicb524072012-08-09 16:40:18 -07003540
3541 /* Everything looks fine. Return success. */
3542 return 0;
3543}
3544#endif
3545
Brian Swetland9c4c0752009-01-25 16:23:50 -08003546void cmd_boot(const char *arg, void *data, unsigned sz)
3547{
3548 unsigned kernel_actual;
3549 unsigned ramdisk_actual;
Kishor PKd8ddcad2017-07-27 13:53:57 +05303550 unsigned second_actual;
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07003551 uint32_t image_actual;
3552 uint32_t dt_actual = 0;
Mayank Grovere1ab96c2018-09-04 20:31:31 +05303553 boot_img_hdr *hdr = NULL;
Matthew Qinbb7923d2015-02-09 10:56:09 +08003554 struct kernel64_hdr *kptr = NULL;
Brian Swetland9c4c0752009-01-25 16:23:50 -08003555 char *ptr = ((char*) data);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07003556 int ret = 0;
3557 uint8_t dtb_copied = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08003558 unsigned int out_len = 0;
3559 unsigned int out_avai_len = 0;
3560 unsigned char *out_addr = NULL;
3561 uint32_t dtb_offset = 0;
3562 unsigned char *kernel_start_addr = NULL;
3563 unsigned int kernel_size = 0;
Matthew Qin271927e2015-03-31 22:07:07 -04003564 unsigned int scratch_offset = 0;
Saranya Chidurab4933332018-10-15 17:30:06 +05303565#if VERIFIED_BOOT_2
3566 void *dtbo_image_buf = NULL;
3567 uint32_t dtbo_image_sz = 0;
3568 void *vbmeta_image_buf = NULL;
3569 uint32_t vbmeta_image_sz = 0;
3570#endif
Monika Singh292b3e92018-03-17 22:40:23 +05303571#if !VERIFIED_BOOT_2
3572 uint32_t sig_actual = 0;
3573 uint32_t sig_size = 0;
3574#ifdef MDTP_SUPPORT
3575 static bool is_mdtp_activated = 0;
3576#endif /* MDTP_SUPPORT */
3577#endif
Matthew Qinbb7923d2015-02-09 10:56:09 +08003578
lijuang2008ff22016-03-07 17:56:27 +08003579#if FBCON_DISPLAY_MSG
3580 /* Exit keys' detection thread firstly */
3581 exit_menu_keys_detection();
3582#endif
3583
Monika Singh292b3e92018-03-17 22:40:23 +05303584#if VERIFIED_BOOT || VERIFIED_BOOT_2
Channagoud Kadabi6d5375e2015-06-23 17:15:42 -07003585 if(target_build_variant_user() && !device.is_unlocked)
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003586 {
3587 fastboot_fail("unlock device to use this command");
lijuang2008ff22016-03-07 17:56:27 +08003588 goto boot_failed;
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003589 }
3590#endif
3591
Brian Swetland9c4c0752009-01-25 16:23:50 -08003592 if (sz < sizeof(hdr)) {
3593 fastboot_fail("invalid bootimage header");
lijuang2008ff22016-03-07 17:56:27 +08003594 goto boot_failed;
Brian Swetland9c4c0752009-01-25 16:23:50 -08003595 }
3596
Mayank Grovere1ab96c2018-09-04 20:31:31 +05303597 hdr = (boot_img_hdr *)data;
Brian Swetland9c4c0752009-01-25 16:23:50 -08003598
3599 /* ensure commandline is terminated */
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07003600 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -08003601
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07003602 if(target_is_emmc_boot() && hdr->page_size) {
3603 page_size = hdr->page_size;
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07003604 page_mask = page_size - 1;
3605 }
3606
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07003607 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
3608 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Kishor PKd8ddcad2017-07-27 13:53:57 +05303609 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07003610#if DEVICE_TREE
Parth Dixit4097b622016-03-15 14:42:27 +05303611#ifndef OSVERSION_IN_BOOTIMAGE
3612 dt_size = hdr->dt_size;
3613#endif
3614 dt_actual = ROUND_TO_PAGE(dt_size, page_mask);
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07003615#endif
3616
3617 image_actual = ADD_OF(page_size, kernel_actual);
3618 image_actual = ADD_OF(image_actual, ramdisk_actual);
Kishor PKd8ddcad2017-07-27 13:53:57 +05303619 image_actual = ADD_OF(image_actual, second_actual);
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07003620 image_actual = ADD_OF(image_actual, dt_actual);
3621
Kishor PK5134b332017-05-09 17:50:08 +05303622 /* Checking to prevent oob access in read_der_message_length */
3623 if (image_actual > sz) {
3624 fastboot_fail("bootimage header fields are invalid");
3625 goto boot_failed;
3626 }
Saranya Chidurab4933332018-10-15 17:30:06 +05303627
Monika Singh292b3e92018-03-17 22:40:23 +05303628#if VERIFIED_BOOT_2
Mayank Grover1ceaee22019-04-01 17:54:32 +05303629 /* Pass size of boot partition, as imgsize, to avoid
3630 read fewer bytes error */
3631 image_actual = partition_get_size(partition_get_index("boot"));
Saranya Chidurab4933332018-10-15 17:30:06 +05303632
3633 /* load and validate dtbo partition */
3634 load_validate_dtbo_image(&dtbo_image_buf, &dtbo_image_sz);
3635
3636 /* load vbmeta partition */
3637 load_vbmeta_image(&vbmeta_image_buf, &vbmeta_image_sz);
3638
Monika Singh292b3e92018-03-17 22:40:23 +05303639 memset(&info, 0, sizeof(bootinfo));
Saranya Chidurab4933332018-10-15 17:30:06 +05303640
3641 info.images[IMG_BOOT].image_buffer = SUB_SALT_BUFF_OFFSET(data);
3642 info.images[IMG_BOOT].imgsize = image_actual;
3643 info.images[IMG_BOOT].name = "boot";
3644 ++info.num_loaded_images;
3645
3646 /* Pass loaded dtbo image */
3647 if (dtbo_image_buf != NULL) {
3648 info.images[IMG_DTBO].image_buffer = SUB_SALT_BUFF_OFFSET(dtbo_image_buf);
3649 info.images[IMG_DTBO].imgsize = dtbo_image_sz;
3650 info.images[IMG_DTBO].name = "dtbo";
3651 ++info.num_loaded_images;
3652 }
3653
3654 /* Pass loaded vbmeta image */
3655 if (vbmeta_image_buf != NULL) {
3656 info.images[IMG_VBMETA].image_buffer = vbmeta_image_buf;
3657 info.images[IMG_VBMETA].imgsize = vbmeta_image_sz;
3658 info.images[IMG_VBMETA].name = "vbmeta";
3659 ++info.num_loaded_images;
3660 }
3661
Monika Singh292b3e92018-03-17 22:40:23 +05303662 info.multi_slot_boot = partition_multislot_is_supported();
3663 if (load_image_and_auth(&info))
3664 goto boot_failed;
3665 vbcmdline = info.vbcmdline;
Saranya Chidurab4933332018-10-15 17:30:06 +05303666
3667 /* Free the buffer allocated to vbmeta post verification */
3668 if (vbmeta_image_buf != NULL) {
3669 free(vbmeta_image_buf);
3670 --info.num_loaded_images;
3671 }
Monika Singh292b3e92018-03-17 22:40:23 +05303672#else
Kishor PK5134b332017-05-09 17:50:08 +05303673 sig_size = sz - image_actual;
3674
P.V. Phani Kumareecfd822016-03-09 20:09:03 +05303675 if (target_use_signed_kernel() && (!device.is_unlocked)) {
Monika Singh292b3e92018-03-17 22:40:23 +05303676 unsigned chk;
P.V. Phani Kumareecfd822016-03-09 20:09:03 +05303677 /* Calculate the signature length from boot image */
3678 sig_actual = read_der_message_length(
Kishor PK5134b332017-05-09 17:50:08 +05303679 (unsigned char*)(data + image_actual), sig_size);
Monika Singh292b3e92018-03-17 22:40:23 +05303680 chk = ADD_OF(image_actual, sig_actual);
Channagoud Kadabi3d839012014-06-26 14:26:39 -07003681
Monika Singh292b3e92018-03-17 22:40:23 +05303682 if (chk > sz) {
Kishor PK5134b332017-05-09 17:50:08 +05303683 fastboot_fail("bootimage header fields are invalid");
3684 goto boot_failed;
3685 }
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07003686 }
3687
Sridhar Parasuram361e4cd2015-09-24 11:19:19 -07003688 // Initialize boot state before trying to verify boot.img
3689#if VERIFIED_BOOT
Channagoud Kadabi699466e2015-11-03 12:37:42 -08003690 boot_verifier_init();
Mayank Groverb337e932017-01-18 20:00:40 +05303691#endif
Channagoud Kadabibf6ce7d2015-09-17 13:25:35 -07003692 /* Handle overflow if the input image size is greater than
3693 * boot image buffer can hold
3694 */
Monika Singh292b3e92018-03-17 22:40:23 +05303695 if ((target_get_max_flash_size() - page_size) < image_actual)
Channagoud Kadabibf6ce7d2015-09-17 13:25:35 -07003696 {
3697 fastboot_fail("booimage: size is greater than boot image buffer can hold");
lijuang2008ff22016-03-07 17:56:27 +08003698 goto boot_failed;
Channagoud Kadabibf6ce7d2015-09-17 13:25:35 -07003699 }
Sridhar Parasuram361e4cd2015-09-24 11:19:19 -07003700
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07003701 /* Verify the boot image
3702 * device & page_size are initialized in aboot_init
3703 */
Sridhar Parasuram7ae9fbc2015-09-17 09:56:30 -07003704 if (target_use_signed_kernel() && (!device.is_unlocked)) {
Channagoud Kadabi3d839012014-06-26 14:26:39 -07003705 /* Pass size excluding signature size, otherwise we would try to
3706 * access signature beyond its length
3707 */
Monika Singh292b3e92018-03-17 22:40:23 +05303708 verify_signed_bootimg((uint32_t)data, image_actual);
Sridhar Parasuram7ae9fbc2015-09-17 09:56:30 -07003709 }
Amit Blay4aa292f2015-04-28 21:55:59 +03003710#ifdef MDTP_SUPPORT
3711 else
3712 {
Shay Nachmani062aa3f2015-05-17 17:28:44 +03003713 /* fastboot boot is not allowed when MDTP is activated */
Amit Blay4aa292f2015-04-28 21:55:59 +03003714 mdtp_ext_partition_verification_t ext_partition;
Amit Blay8a510302015-08-17 09:20:01 +03003715
3716 if (!is_mdtp_activated) {
3717 ext_partition.partition = MDTP_PARTITION_NONE;
3718 mdtp_fwlock_verify_lock(&ext_partition);
3719 }
Amit Blay4aa292f2015-04-28 21:55:59 +03003720 }
Shay Nachmani062aa3f2015-05-17 17:28:44 +03003721
Amir Kotzer7c768c02016-04-13 09:08:05 +03003722 /* If mdtp state cannot be validate, block fastboot boot*/
3723 if(mdtp_activated(&is_mdtp_activated)){
3724 dprintf(CRITICAL, "mdtp_activated cannot validate state.\n");
3725 dprintf(CRITICAL, "Can not proceed with fastboot boot command.\n");
3726 goto boot_failed;
3727 }
Shay Nachmani062aa3f2015-05-17 17:28:44 +03003728 if(is_mdtp_activated){
3729 dprintf(CRITICAL, "fastboot boot command is not available.\n");
lijuang2008ff22016-03-07 17:56:27 +08003730 goto boot_failed;
Shay Nachmani062aa3f2015-05-17 17:28:44 +03003731 }
Amit Blay4aa292f2015-04-28 21:55:59 +03003732#endif /* MDTP_SUPPORT */
Monika Singh292b3e92018-03-17 22:40:23 +05303733#endif /* VERIFIED_BOOT_2 else */
Amit Blay4aa292f2015-04-28 21:55:59 +03003734
Sridhar Parasuram361e4cd2015-09-24 11:19:19 -07003735#if VERIFIED_BOOT
Monika Singh292b3e92018-03-17 22:40:23 +05303736 if (VB_M == target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +05303737 {
3738 /* set boot and system versions. */
3739 set_os_version((unsigned char *)data);
3740 // send root of trust
3741 if(!send_rot_command((uint32_t)device.is_unlocked))
3742 ASSERT(0);
3743 }
Parth Dixitbc9b6492015-10-18 00:41:38 +05303744#endif
Channagoud Kadabia22144f2013-03-20 11:49:01 -07003745 /*
Matthew Qinbb7923d2015-02-09 10:56:09 +08003746 * Check if the kernel image is a gzip package. If yes, need to decompress it.
3747 * If not, continue booting.
3748 */
3749 if (is_gzip_package((unsigned char *)(data + page_size), hdr->kernel_size))
3750 {
3751 out_addr = (unsigned char *)target_get_scratch_address();
3752 out_addr = (unsigned char *)(out_addr + image_actual + page_size);
3753 out_avai_len = target_get_max_flash_size() - image_actual - page_size;
Saranya Chidurab4933332018-10-15 17:30:06 +05303754#if VERIFIED_BOOT_2
3755 if (dtbo_image_sz)
3756 out_avai_len -= DTBO_IMG_BUF;
3757#endif
Matthew Qin0b15b322015-05-19 05:20:54 -04003758 dprintf(INFO, "decompressing kernel image: start\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08003759 ret = decompress((unsigned char *)(ptr + page_size),
3760 hdr->kernel_size, out_addr, out_avai_len,
3761 &dtb_offset, &out_len);
3762 if (ret)
3763 {
Matthew Qin0b15b322015-05-19 05:20:54 -04003764 dprintf(CRITICAL, "decompressing image failed!!!\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08003765 ASSERT(0);
3766 }
3767
Matthew Qin0b15b322015-05-19 05:20:54 -04003768 dprintf(INFO, "decompressing kernel image: done\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08003769 kptr = (struct kernel64_hdr *)out_addr;
3770 kernel_start_addr = out_addr;
3771 kernel_size = out_len;
3772 } else {
3773 kptr = (struct kernel64_hdr*)((char *)data + page_size);
3774 kernel_start_addr = (unsigned char *)((char *)data + page_size);
3775 kernel_size = hdr->kernel_size;
3776 }
3777
3778 /*
Channagoud Kadabia22144f2013-03-20 11:49:01 -07003779 * Update the kernel/ramdisk/tags address if the boot image header
3780 * has default values, these default values come from mkbootimg when
3781 * the boot image is flashed using fastboot flash:raw
3782 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08003783 update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr));
Dima Zavin3cadfff2013-03-21 14:30:48 -07003784
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07003785 /* Get virtual addresses since the hdr saves physical addresses. */
3786 hdr->kernel_addr = VA(hdr->kernel_addr);
3787 hdr->ramdisk_addr = VA(hdr->ramdisk_addr);
3788 hdr->tags_addr = VA(hdr->tags_addr);
Brian Swetland9c4c0752009-01-25 16:23:50 -08003789
Matthew Qinbb7923d2015-02-09 10:56:09 +08003790 kernel_size = ROUND_TO_PAGE(kernel_size, page_mask);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07003791 /* Check if the addresses in the header are valid. */
Matthew Qinbb7923d2015-02-09 10:56:09 +08003792 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_size) ||
Mayank Grovere559cec2017-10-17 15:12:03 +05303793 check_ddr_addr_range_bound(hdr->kernel_addr, kernel_size) ||
3794 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual) ||
3795 check_ddr_addr_range_bound(hdr->ramdisk_addr, ramdisk_actual))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07003796 {
Mayank Grovere559cec2017-10-17 15:12:03 +05303797 dprintf(CRITICAL, "kernel/ramdisk addresses are not valid.\n");
lijuang2008ff22016-03-07 17:56:27 +08003798 goto boot_failed;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07003799 }
3800
Amol Jadicb524072012-08-09 16:40:18 -07003801#if DEVICE_TREE
Matthew Qin271927e2015-03-31 22:07:07 -04003802 scratch_offset = image_actual + page_size + out_len;
Amol Jadicb524072012-08-09 16:40:18 -07003803 /* find correct dtb and copy it to right location */
Matthew Qin271927e2015-03-31 22:07:07 -04003804 ret = copy_dtb(data, scratch_offset);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07003805
3806 dtb_copied = !ret ? 1 : 0;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07003807#else
Mayank Grovere559cec2017-10-17 15:12:03 +05303808 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE) ||
3809 check_ddr_addr_range_bound(hdr->tags_addr, MAX_TAGS_SIZE))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07003810 {
Mayank Grovere559cec2017-10-17 15:12:03 +05303811 dprintf(CRITICAL, "Tags addresses are not valid.\n");
lijuang2008ff22016-03-07 17:56:27 +08003812 goto boot_failed;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07003813 }
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07003814#endif
3815
3816 /* Load ramdisk & kernel */
3817 memmove((void*) hdr->ramdisk_addr, ptr + page_size + kernel_actual, hdr->ramdisk_size);
Matthew Qinbb7923d2015-02-09 10:56:09 +08003818 memmove((void*) hdr->kernel_addr, (char*) (kernel_start_addr), kernel_size);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07003819
3820#if DEVICE_TREE
Mayank Grovere559cec2017-10-17 15:12:03 +05303821 if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual) ||
3822 check_ddr_addr_range_bound(hdr->tags_addr, kernel_actual))
Matthew Qinbb7923d2015-02-09 10:56:09 +08003823 {
Mayank Grovere559cec2017-10-17 15:12:03 +05303824 dprintf(CRITICAL, "Tags addresses are not valid.\n");
lijuang2008ff22016-03-07 17:56:27 +08003825 goto boot_failed;
Matthew Qinbb7923d2015-02-09 10:56:09 +08003826 }
3827
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07003828 /*
3829 * If dtb is not found look for appended DTB in the kernel.
3830 * If appended dev tree is found, update the atags with
3831 * memory address to the DTB appended location on RAM.
3832 * Else update with the atags address in the kernel header
3833 */
3834 if (!dtb_copied) {
3835 void *dtb;
Matthew Qinbb7923d2015-02-09 10:56:09 +08003836 dtb = dev_tree_appended((void*)(ptr + page_size),
3837 hdr->kernel_size, dtb_offset,
Dima Zavine63e5572013-05-03 12:23:06 -07003838 (void *)hdr->tags_addr);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07003839 if (!dtb) {
3840 fastboot_fail("dtb not found");
lijuang2008ff22016-03-07 17:56:27 +08003841 goto boot_failed;
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07003842 }
Amol Jadicb524072012-08-09 16:40:18 -07003843 }
3844#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08003845
3846 fastboot_okay("");
Amol Jadi7c4316c2013-10-07 14:19:26 -07003847 fastboot_stop();
Brian Swetland9c4c0752009-01-25 16:23:50 -08003848
Dima Zavin77e41f32013-03-06 16:10:43 -08003849 boot_linux((void*) hdr->kernel_addr, (void*) hdr->tags_addr,
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07003850 (const char*) hdr->cmdline, board_machtype(),
3851 (void*) hdr->ramdisk_addr, hdr->ramdisk_size);
lijuang2008ff22016-03-07 17:56:27 +08003852
3853 /* fastboot already stop, it's no need to show fastboot menu */
3854 return;
3855boot_failed:
3856#if FBCON_DISPLAY_MSG
3857 /* revert to fastboot menu if boot failed */
3858 display_fastboot_menu();
3859#endif
3860 return;
Brian Swetland9c4c0752009-01-25 16:23:50 -08003861}
3862
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003863void cmd_erase_nand(const char *arg, void *data, unsigned sz)
Dima Zavin214cc642009-01-26 11:16:21 -08003864{
3865 struct ptentry *ptn;
3866 struct ptable *ptable;
3867
3868 ptable = flash_get_ptable();
3869 if (ptable == NULL) {
3870 fastboot_fail("partition table doesn't exist");
3871 return;
3872 }
3873
3874 ptn = ptable_find(ptable, arg);
3875 if (ptn == NULL) {
3876 fastboot_fail("unknown partition name");
3877 return;
3878 }
3879
Monika Singh292b3e92018-03-17 22:40:23 +05303880 if (!strncmp(arg, "avb_custom_key", strlen("avb_custom_key"))) {
3881 dprintf(INFO, "erasing avb_custom_key\n");
3882 if (erase_userkey()) {
3883 fastboot_fail("Erasing avb_custom_key failed");
3884 } else {
3885 fastboot_okay("");
3886 }
3887 return;
3888 }
3889
Dima Zavin214cc642009-01-26 11:16:21 -08003890 if (flash_erase(ptn)) {
3891 fastboot_fail("failed to erase partition");
3892 return;
3893 }
3894 fastboot_okay("");
3895}
3896
Bikas Gurungd48bd242010-09-04 19:54:32 -07003897
3898void cmd_erase_mmc(const char *arg, void *data, unsigned sz)
3899{
3900 unsigned long long ptn = 0;
Oliver Wangcee448d2013-10-22 18:40:13 +08003901 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07003902 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003903 uint8_t lun = 0;
Vijay Kumar Pendoti0b21f462016-05-02 17:09:18 +05303904 char *footer = NULL;
Bikas Gurungd48bd242010-09-04 19:54:32 -07003905
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003906#if VERIFIED_BOOT
3907 if(!strcmp(arg, KEYSTORE_PTN_NAME))
3908 {
3909 if(!device.is_unlocked)
3910 {
3911 fastboot_fail("unlock device to erase keystore");
3912 return;
3913 }
3914 }
3915#endif
3916
Kinson Chikf1a43512011-07-14 11:28:39 -07003917 index = partition_get_index(arg);
3918 ptn = partition_get_offset(index);
Oliver Wangcee448d2013-10-22 18:40:13 +08003919 size = partition_get_size(index);
Neeti Desaica8c9602011-10-06 11:40:00 -07003920
Monika Singhc4778b72018-05-16 11:16:42 +05303921 if (!strncmp(arg, "avb_custom_key", strlen("avb_custom_key"))) {
3922 dprintf(INFO, "erasing avb_custom_key\n");
3923 if (erase_userkey()) {
3924 fastboot_fail("Erasing avb_custom_key failed");
3925 } else {
3926 fastboot_okay("");
3927 }
3928 return;
3929 }
3930
Kinson Chikf1a43512011-07-14 11:28:39 -07003931 if(ptn == 0) {
Neeti Desaica8c9602011-10-06 11:40:00 -07003932 fastboot_fail("Partition table doesn't exist\n");
Bikas Gurungd48bd242010-09-04 19:54:32 -07003933 return;
3934 }
Kun Liang2f1601a2013-08-12 16:29:54 +08003935
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003936 lun = partition_get_lun(index);
3937 mmc_set_lun(lun);
3938
Sridhar Parasuramd7957122015-02-27 11:33:40 -08003939 if (platform_boot_dev_isemmc())
3940 {
3941 if (mmc_erase_card(ptn, size)) {
3942 fastboot_fail("failed to erase partition\n");
3943 return;
3944 }
3945 } else {
3946 BUF_DMA_ALIGN(out, DEFAULT_ERASE_SIZE);
3947 size = partition_get_size(index);
3948 if (size > DEFAULT_ERASE_SIZE)
3949 size = DEFAULT_ERASE_SIZE;
Kun Liang2f1601a2013-08-12 16:29:54 +08003950
Sridhar Parasuramd7957122015-02-27 11:33:40 -08003951 /* Simple inefficient version of erase. Just writing
3952 0 in first several blocks */
3953 if (mmc_write(ptn , size, (unsigned int *)out)) {
3954 fastboot_fail("failed to erase partition");
3955 return;
3956 }
Vijay Kumar Pendoti0b21f462016-05-02 17:09:18 +05303957 /*Erase FDE metadata at the userdata footer*/
3958 if(!(strncmp(arg, "userdata", 8)))
3959 {
3960 footer = memalign(CACHE_LINE, FOOTER_SIZE);
3961 memset((void *)footer, 0, FOOTER_SIZE);
3962
3963 size = partition_get_size(index);
3964
3965 if (mmc_write((ptn + size) - FOOTER_SIZE , FOOTER_SIZE, (unsigned int *)footer)) {
3966 fastboot_fail("failed to erase userdata footer");
3967 free(footer);
3968 return;
3969 }
3970 free(footer);
3971 }
Bikas Gurungd48bd242010-09-04 19:54:32 -07003972 }
Monika Singh292b3e92018-03-17 22:40:23 +05303973#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover912eaa62017-10-26 12:08:53 +05303974 if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05303975 !(strncmp(arg, "userdata", 8)) &&
3976 send_delete_keys_to_tz())
Sridhar Parasuram32b30662015-07-10 13:33:22 -07003977 ASSERT(0);
3978#endif
Bikas Gurungd48bd242010-09-04 19:54:32 -07003979 fastboot_okay("");
3980}
3981
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003982void cmd_erase(const char *arg, void *data, unsigned sz)
3983{
Monika Singh292b3e92018-03-17 22:40:23 +05303984#if VERIFIED_BOOT || VERIFIED_BOOT_2
Channagoud Kadabi35297672015-06-13 11:09:49 -07003985 if (target_build_variant_user())
Channagoud Kadabice3cf422015-04-17 16:02:30 -07003986 {
Sridhar Parasuramc32d07d2015-07-12 10:57:48 -07003987 if(!device.is_unlocked)
Channagoud Kadabice3cf422015-04-17 16:02:30 -07003988 {
Channagoud Kadabi35297672015-06-13 11:09:49 -07003989 fastboot_fail("device is locked. Cannot erase");
Channagoud Kadabice3cf422015-04-17 16:02:30 -07003990 return;
3991 }
3992 }
3993#endif
3994
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003995 if(target_is_emmc_boot())
3996 cmd_erase_mmc(arg, data, sz);
3997 else
3998 cmd_erase_nand(arg, data, sz);
3999}
Bikas Gurungd48bd242010-09-04 19:54:32 -07004000
Mayank Grover11ff9692018-01-11 11:54:49 +05304001/* Get the size from partiton name */
4002static void get_partition_size(const char *arg, char *response)
4003{
4004 uint64_t ptn = 0;
4005 uint64_t size;
4006 int index = INVALID_PTN;
4007
4008 index = partition_get_index(arg);
4009
4010 if (index == INVALID_PTN)
4011 {
4012 dprintf(CRITICAL, "Invalid partition index\n");
4013 return;
4014 }
4015
4016 ptn = partition_get_offset(index);
4017
4018 if(!ptn)
4019 {
4020 dprintf(CRITICAL, "Invalid partition name %s\n", arg);
4021 return;
4022 }
4023
4024 size = partition_get_size(index);
4025
4026 snprintf(response, MAX_RSP_SIZE, "\t 0x%llx", size);
4027 return;
4028}
4029
Mayank Grover52cd10a2018-03-15 12:57:54 +05304030/* Function to check partition type of a partition*/
4031static fs_signature_type
4032check_partition_fs_signature(const char *arg)
4033{
4034 fs_signature_type ret = NO_FS;
4035 int index;
4036 unsigned long long ptn;
Mayank Grover399826a2018-08-27 12:15:15 +05304037 char *buffer = memalign(CACHE_LINE, mmc_blocksize);
4038 uint32_t sb_blk_offset = 0;
4039 char *sb_buffer = buffer;
4040
Mayank Grover52cd10a2018-03-15 12:57:54 +05304041 if (!sb_buffer)
4042 {
4043 dprintf(CRITICAL, "ERROR: Failed to allocate buffer for superblock\n");
4044 goto out;
4045 }
4046
4047 /* Read super block */
4048 if ((index = partition_get_index(arg)) < 0)
4049 {
4050 dprintf(CRITICAL, "ERROR: %s() doesn't exsit\n", arg);
4051 goto out;
4052 }
4053 ptn = partition_get_offset(index);
4054 mmc_set_lun(partition_get_lun(index));
Mayank Grover399826a2018-08-27 12:15:15 +05304055 sb_blk_offset = (FS_SUPERBLOCK_OFFSET/mmc_blocksize);
4056
4057 if(mmc_read(ptn + (sb_blk_offset * mmc_blocksize),
Mayank Grover52cd10a2018-03-15 12:57:54 +05304058 (void *)sb_buffer, mmc_blocksize))
4059 {
4060 dprintf(CRITICAL, "ERROR: Failed to read Superblock\n");
4061 goto out;
4062 }
4063
Mayank Grover399826a2018-08-27 12:15:15 +05304064 if (sb_blk_offset == 0)
4065 sb_buffer += FS_SUPERBLOCK_OFFSET;
4066
4067 if (*((uint16 *)(&sb_buffer[EXT_MAGIC_OFFSET_SB])) == (uint16)EXT_MAGIC)
Mayank Grover52cd10a2018-03-15 12:57:54 +05304068 {
4069 dprintf(SPEW, "%s() Found EXT FS\n", arg);
4070 ret = EXT_FS_SIGNATURE;
4071 }
Mayank Grover399826a2018-08-27 12:15:15 +05304072 else if (*((uint32 *)(&sb_buffer[F2FS_MAGIC_OFFSET_SB])) == F2FS_MAGIC)
Mayank Grover52cd10a2018-03-15 12:57:54 +05304073 {
4074 dprintf(SPEW, "%s() Found F2FS FS\n", arg);
4075 ret = EXT_F2FS_SIGNATURE;
4076 }
4077 else
4078 {
4079 dprintf(SPEW, "%s() Reverting to default 0x%x\n",
4080 arg, *((uint16 *)(&sb_buffer[EXT_MAGIC_OFFSET_SB])));
4081 ret = NO_FS;
4082 }
4083
4084out:
Mayank Grover399826a2018-08-27 12:15:15 +05304085 if(buffer)
4086 free(buffer);
Mayank Grover52cd10a2018-03-15 12:57:54 +05304087 return ret;
4088}
4089
Mayank Groverd38fe012018-03-13 15:33:16 +05304090/* Function to get partition type */
4091static void get_partition_type(const char *arg, char *response)
4092{
4093 uint n = 0;
Mayank Grover52cd10a2018-03-15 12:57:54 +05304094 fs_signature_type fs_signature;
Mayank Groverd38fe012018-03-13 15:33:16 +05304095
4096 if (arg == NULL ||
4097 response == NULL)
4098 {
4099 dprintf(CRITICAL, "Invalid input parameter\n");
4100 return;
4101 }
4102
4103 /* By default copy raw to response */
Mayank Grover0e559042018-04-11 17:49:30 +05304104 strlcpy(response, RAW_STR, MAX_RSP_SIZE);
Mayank Groverd38fe012018-03-13 15:33:16 +05304105
4106 /* Mark partiton type for known paritions only */
4107 for (n=0; n < ARRAY_SIZE(part_type_known); n++)
4108 {
4109 if (!strncmp(part_type_known[n].part_name, arg, strlen(arg)))
4110 {
Mayank Grover52cd10a2018-03-15 12:57:54 +05304111 /* Check partition for FS signature */
4112 fs_signature = check_partition_fs_signature(arg);
4113 switch (fs_signature)
4114 {
4115 case EXT_FS_SIGNATURE:
Mayank Grover0e559042018-04-11 17:49:30 +05304116 strlcpy(response, EXT_STR, MAX_RSP_SIZE);
Mayank Grover52cd10a2018-03-15 12:57:54 +05304117 break;
4118 case EXT_F2FS_SIGNATURE:
Mayank Grover0e559042018-04-11 17:49:30 +05304119 strlcpy(response, F2FS_STR, MAX_RSP_SIZE);
Mayank Grover52cd10a2018-03-15 12:57:54 +05304120 break;
4121 case NO_FS:
Mayank Grover0e559042018-04-11 17:49:30 +05304122 strlcpy(response, part_type_known[n].type_response, MAX_RSP_SIZE);
Mayank Grover52cd10a2018-03-15 12:57:54 +05304123 }
Mayank Groverd38fe012018-03-13 15:33:16 +05304124 }
4125 }
4126 return;
4127}
4128
Mayank Grover11ff9692018-01-11 11:54:49 +05304129/*
4130 * Publish the partition type & size info
4131 * fastboot getvar will publish the required information.
4132 * fastboot getvar partition_size:<partition_name>: partition size in hex
4133 * fastboot getvar partition_type:<partition_name>: partition type (ext/fat)
4134 */
4135static void publish_getvar_partition_info(struct getvar_partition_info *info, uint8_t num_parts)
4136{
Mayank Groverd38fe012018-03-13 15:33:16 +05304137 uint8_t i;
Mayank Grover11ff9692018-01-11 11:54:49 +05304138 static bool published = false;
4139 struct partition_entry *ptn_entry =
4140 partition_get_partition_entries();
4141 memset(info, 0, sizeof(struct getvar_partition_info)* num_parts);
4142
4143 for (i = 0; i < num_parts; i++) {
4144 strlcat(info[i].part_name, (const char *)ptn_entry[i].name, MAX_RSP_SIZE);
4145 strlcat(info[i].getvar_size, "partition-size:", MAX_GET_VAR_NAME_SIZE);
4146 strlcat(info[i].getvar_type, "partition-type:", MAX_GET_VAR_NAME_SIZE);
4147
Mayank Groverd38fe012018-03-13 15:33:16 +05304148 get_partition_type(info[i].part_name, info[i].type_response);
Mayank Grover11ff9692018-01-11 11:54:49 +05304149 get_partition_size(info[i].part_name, info[i].size_response);
Mayank Grover11ff9692018-01-11 11:54:49 +05304150 if (strlcat(info[i].getvar_size, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
4151 {
4152 dprintf(CRITICAL, "partition size name truncated\n");
4153 return;
4154 }
4155 if (strlcat(info[i].getvar_type, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
4156 {
4157 dprintf(CRITICAL, "partition type name truncated\n");
4158 return;
4159 }
4160
4161 if (!published)
4162 {
4163 /* publish partition size & type info */
4164 fastboot_publish((const char *) info[i].getvar_size, (const char *) info[i].size_response);
4165 fastboot_publish((const char *) info[i].getvar_type, (const char *) info[i].type_response);
4166 }
4167 }
4168 if (!published)
4169 published = true;
4170}
4171
4172
Ajay Dudani5c761132011-04-07 20:19:04 -07004173void cmd_flash_mmc_img(const char *arg, void *data, unsigned sz)
Shashank Mittal23b8f422010-04-16 19:27:21 -07004174{
4175 unsigned long long ptn = 0;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07004176 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07004177 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08004178 char *token = NULL;
4179 char *pname = NULL;
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07004180 char *sp;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08004181 uint8_t lun = 0;
4182 bool lun_set = false;
Mayank Grover351a75e2017-05-30 20:06:08 +05304183 int current_active_slot = INVALID;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07004184
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07004185 token = strtok_r((char *)arg, ":", &sp);
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08004186 pname = token;
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07004187 token = strtok_r(NULL, ":", &sp);
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08004188 if(token)
4189 {
4190 lun = atoi(token);
4191 mmc_set_lun(lun);
4192 lun_set = true;
4193 }
4194
Mao Jinlong226f33a2014-07-04 17:24:10 +08004195 if (pname)
Greg Grisco6e754772011-06-23 12:19:39 -07004196 {
Channagoud Kadabiad259832015-05-29 11:14:17 -07004197 if (!strncmp(pname, "frp-unlock", strlen("frp-unlock")))
4198 {
4199 if (!aboot_frp_unlock(pname, data, sz))
4200 {
4201 fastboot_info("FRP unlock successful");
4202 fastboot_okay("");
4203 }
4204 else
4205 fastboot_fail("Secret key is invalid, please update the bootloader with secret key");
4206
4207 return;
4208 }
4209
Mao Jinlong226f33a2014-07-04 17:24:10 +08004210 if (!strcmp(pname, "partition"))
4211 {
4212 dprintf(INFO, "Attempt to write partition image.\n");
4213 if (write_partition(sz, (unsigned char *) data)) {
4214 fastboot_fail("failed to write partition");
Greg Grisco6e754772011-06-23 12:19:39 -07004215 return;
4216 }
Mayank Grover11ff9692018-01-11 11:54:49 +05304217 /* Re-publish partition table */
4218 publish_getvar_partition_info(part_info, partition_get_partition_count());
Mayank Grover351a75e2017-05-30 20:06:08 +05304219
4220 /* Rescan partition table to ensure we have multislot support*/
4221 if (partition_scan_for_multislot())
4222 {
4223 current_active_slot = partition_find_active_slot();
4224 dprintf(INFO, "Multislot supported: Slot %s active",
4225 (SUFFIX_SLOT(current_active_slot)));
4226 }
4227 partition_mark_active_slot(current_active_slot);
Greg Grisco6e754772011-06-23 12:19:39 -07004228 }
Mao Jinlong226f33a2014-07-04 17:24:10 +08004229 else
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08004230 {
Shashank Mittald3e54dd2014-08-28 15:24:02 -07004231#if VERIFIED_BOOT
4232 if(!strcmp(pname, KEYSTORE_PTN_NAME))
4233 {
4234 if(!device.is_unlocked)
4235 {
4236 fastboot_fail("unlock device to flash keystore");
4237 return;
4238 }
Gaurav Nebhwanic7313cc2015-12-15 22:25:04 +05304239 if(!boot_verify_validate_keystore((unsigned char *)data,sz))
Shashank Mittald3e54dd2014-08-28 15:24:02 -07004240 {
4241 fastboot_fail("image is not a keystore file");
4242 return;
4243 }
4244 }
4245#endif
Mao Jinlong226f33a2014-07-04 17:24:10 +08004246 index = partition_get_index(pname);
4247 ptn = partition_get_offset(index);
4248 if(ptn == 0) {
4249 fastboot_fail("partition table doesn't exist");
4250 return;
4251 }
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08004252
Mayank Grover351a75e2017-05-30 20:06:08 +05304253 if (!strncmp(pname, "boot", strlen("boot"))
4254 || !strcmp(pname, "recovery"))
4255 {
Mao Jinlong226f33a2014-07-04 17:24:10 +08004256 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
4257 fastboot_fail("image is not a boot image");
4258 return;
4259 }
Mayank Grover351a75e2017-05-30 20:06:08 +05304260
4261 /* Reset multislot_partition attributes in case of flashing boot */
4262 if (partition_multislot_is_supported())
4263 {
4264 partition_reset_attributes(index);
4265 }
Mao Jinlong226f33a2014-07-04 17:24:10 +08004266 }
4267
4268 if(!lun_set)
4269 {
4270 lun = partition_get_lun(index);
4271 mmc_set_lun(lun);
4272 }
4273
4274 size = partition_get_size(index);
Gaurav Nebhwani4d2389c2016-03-17 21:10:05 +05304275 if (ROUND_TO_PAGE(sz, mmc_blocksize_mask) > size) {
Mao Jinlong226f33a2014-07-04 17:24:10 +08004276 fastboot_fail("size too large");
4277 return;
4278 }
4279 else if (mmc_write(ptn , sz, (unsigned int *)data)) {
4280 fastboot_fail("flash write failure");
4281 return;
4282 }
Greg Grisco6e754772011-06-23 12:19:39 -07004283 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07004284 }
4285 fastboot_okay("");
4286 return;
4287}
4288
Ajay Dudanide984792015-03-02 09:57:41 -08004289void cmd_flash_meta_img(const char *arg, void *data, unsigned sz)
4290{
4291 int i, images;
4292 meta_header_t *meta_header;
4293 img_header_entry_t *img_header_entry;
Parth Dixit3e2d3032016-03-04 17:11:52 +05304294 /*End of the image address*/
4295 uintptr_t data_end;
4296
4297 if( (UINT_MAX - sz) > (uintptr_t)data )
4298 data_end = (uintptr_t)data + sz;
4299 else
4300 {
4301 fastboot_fail("Cannot flash: image header corrupt");
4302 return;
4303 }
4304
4305 if( data_end < ((uintptr_t)data + sizeof(meta_header_t)))
4306 {
4307 fastboot_fail("Cannot flash: image header corrupt");
4308 return;
4309 }
Ajay Dudanide984792015-03-02 09:57:41 -08004310
lijuang8ee21882016-04-19 16:57:11 +08004311 /* If device is locked:
4312 * Forbid to flash image to avoid the device to bypass the image
4313 * which with "any" name other than bootloader. Because it maybe
4314 * a meta package of all partitions.
4315 */
Monika Singh292b3e92018-03-17 22:40:23 +05304316#if VERIFIED_BOOT || VERIFIED_BOOT_2
lijuang8ee21882016-04-19 16:57:11 +08004317 if (target_build_variant_user()) {
4318 if (!device.is_unlocked) {
4319 fastboot_fail("Device is locked, meta image flashing is not allowed");
4320 return;
4321 }
Mayank Grover889be1b2017-09-12 20:12:23 +05304322
Mayank Grover912eaa62017-10-26 12:08:53 +05304323 if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05304324 !device.is_unlock_critical)
4325 {
lijuang8ee21882016-04-19 16:57:11 +08004326 fastboot_fail("Device is critical locked, Meta image flashing is not allowed");
4327 return;
4328 }
lijuang8ee21882016-04-19 16:57:11 +08004329 }
4330#endif
4331
Ajay Dudanide984792015-03-02 09:57:41 -08004332 meta_header = (meta_header_t*) data;
Parth Dixit3e2d3032016-03-04 17:11:52 +05304333 if( data_end < ((uintptr_t)data + meta_header->img_hdr_sz))
4334 {
4335 fastboot_fail("Cannot flash: image header corrupt");
4336 return;
4337 }
Ajay Dudanide984792015-03-02 09:57:41 -08004338 img_header_entry = (img_header_entry_t*) (data+sizeof(meta_header_t));
4339
4340 images = meta_header->img_hdr_sz / sizeof(img_header_entry_t);
4341
4342 for (i=0; i<images; i++) {
4343
4344 if((img_header_entry[i].ptn_name == NULL) ||
4345 (img_header_entry[i].start_offset == 0) ||
4346 (img_header_entry[i].size == 0))
4347 break;
Kishor PK49831502017-04-21 17:55:43 +05304348 if ((UINT_MAX - img_header_entry[i].start_offset) < (uintptr_t)data) {
4349 fastboot_fail("Integer overflow detected in start_offset of img");
4350 break;
4351 }
4352 else if ((UINT_MAX - (img_header_entry[i].start_offset + (uintptr_t)data)) < img_header_entry[i].size) {
4353 fastboot_fail("Integer overflow detected in size of img");
4354 break;
4355 }
Parth Dixit3e2d3032016-03-04 17:11:52 +05304356 if( data_end < ((uintptr_t)data + img_header_entry[i].start_offset
4357 + img_header_entry[i].size) )
4358 {
4359 fastboot_fail("Cannot flash: image size mismatch");
4360 break;
4361 }
4362
Ajay Dudanide984792015-03-02 09:57:41 -08004363 cmd_flash_mmc_img(img_header_entry[i].ptn_name,
4364 (void *) data + img_header_entry[i].start_offset,
4365 img_header_entry[i].size);
4366 }
4367
Channagoud Kadabiaafbbfc2015-03-04 17:52:56 -08004368 if (!strncmp(arg, "bootloader", strlen("bootloader")))
4369 {
4370 strlcpy(device.bootloader_version, TARGET(BOARD), MAX_VERSION_LEN);
4371 strlcat(device.bootloader_version, "-", MAX_VERSION_LEN);
4372 strlcat(device.bootloader_version, meta_header->img_version, MAX_VERSION_LEN);
4373 }
4374 else
4375 {
4376 strlcpy(device.radio_version, TARGET(BOARD), MAX_VERSION_LEN);
4377 strlcat(device.radio_version, "-", MAX_VERSION_LEN);
4378 strlcat(device.radio_version, meta_header->img_version, MAX_VERSION_LEN);
4379 }
4380
4381 write_device_info(&device);
Ajay Dudanide984792015-03-02 09:57:41 -08004382 fastboot_okay("");
4383 return;
4384}
4385
Ajay Dudani5c761132011-04-07 20:19:04 -07004386void cmd_flash_mmc_sparse_img(const char *arg, void *data, unsigned sz)
4387{
4388 unsigned int chunk;
wufeng.jiang813dc352015-06-02 23:02:46 -04004389 uint64_t chunk_data_sz;
Channagoud Kadabif627cf82013-09-09 14:08:20 -07004390 uint32_t *fill_buf = NULL;
4391 uint32_t fill_val;
Mayank Grover4f50bba2017-07-19 18:17:08 +05304392 uint32_t blk_sz_actual = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07004393 sparse_header_t *sparse_header;
4394 chunk_header_t *chunk_header;
Ajay Dudaniab18f022011-05-12 14:39:22 -07004395 uint32_t total_blocks = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07004396 unsigned long long ptn = 0;
Channagoud Kadabi65b91002011-10-11 17:34:33 +05304397 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07004398 int index = INVALID_PTN;
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05304399 uint32_t i;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08004400 uint8_t lun = 0;
vijay kumar800255e2015-04-24 20:26:19 +05304401 /*End of the sparse image address*/
Parth Dixit64b1a482016-03-07 16:31:26 +05304402 uintptr_t data_end = (uintptr_t)data + sz;
Ajay Dudani5c761132011-04-07 20:19:04 -07004403
Kinson Chikf1a43512011-07-14 11:28:39 -07004404 index = partition_get_index(arg);
4405 ptn = partition_get_offset(index);
4406 if(ptn == 0) {
Ajay Dudani5c761132011-04-07 20:19:04 -07004407 fastboot_fail("partition table doesn't exist");
4408 return;
4409 }
4410
Channagoud Kadabi65b91002011-10-11 17:34:33 +05304411 size = partition_get_size(index);
Channagoud Kadabi65b91002011-10-11 17:34:33 +05304412
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08004413 lun = partition_get_lun(index);
4414 mmc_set_lun(lun);
4415
vijay kumar800255e2015-04-24 20:26:19 +05304416 if (sz < sizeof(sparse_header_t)) {
4417 fastboot_fail("size too low");
4418 return;
4419 }
4420
Ajay Dudani5c761132011-04-07 20:19:04 -07004421 /* Read and skip over sparse image header */
4422 sparse_header = (sparse_header_t *) data;
vijay kumar800255e2015-04-24 20:26:19 +05304423
Mayank Grover48bd9bb2017-07-19 12:04:16 +05304424 if (!sparse_header->blk_sz || (sparse_header->blk_sz % 4)){
4425 fastboot_fail("Invalid block size\n");
4426 return;
4427 }
4428
vijay kumar1321f342015-03-27 12:13:42 +05304429 if (((uint64_t)sparse_header->total_blks * (uint64_t)sparse_header->blk_sz) > size) {
Ajay Dudani876b3282012-12-21 14:12:17 -08004430 fastboot_fail("size too large");
4431 return;
4432 }
4433
vijay kumarde4fcf62015-04-23 13:05:49 +05304434 data += sizeof(sparse_header_t);
vijay kumar800255e2015-04-24 20:26:19 +05304435
Parth Dixit64b1a482016-03-07 16:31:26 +05304436 if (data_end < (uintptr_t)data) {
vijay kumar800255e2015-04-24 20:26:19 +05304437 fastboot_fail("buffer overreads occured due to invalid sparse header");
4438 return;
4439 }
4440
vijay kumarde4fcf62015-04-23 13:05:49 +05304441 if(sparse_header->file_hdr_sz != sizeof(sparse_header_t))
Ajay Dudani5c761132011-04-07 20:19:04 -07004442 {
vijay kumarde4fcf62015-04-23 13:05:49 +05304443 fastboot_fail("sparse header size mismatch");
4444 return;
Ajay Dudani5c761132011-04-07 20:19:04 -07004445 }
4446
Ajay Dudanib06c05f2011-05-12 14:46:10 -07004447 dprintf (SPEW, "=== Sparse Image Header ===\n");
4448 dprintf (SPEW, "magic: 0x%x\n", sparse_header->magic);
4449 dprintf (SPEW, "major_version: 0x%x\n", sparse_header->major_version);
4450 dprintf (SPEW, "minor_version: 0x%x\n", sparse_header->minor_version);
4451 dprintf (SPEW, "file_hdr_sz: %d\n", sparse_header->file_hdr_sz);
4452 dprintf (SPEW, "chunk_hdr_sz: %d\n", sparse_header->chunk_hdr_sz);
4453 dprintf (SPEW, "blk_sz: %d\n", sparse_header->blk_sz);
4454 dprintf (SPEW, "total_blks: %d\n", sparse_header->total_blks);
4455 dprintf (SPEW, "total_chunks: %d\n", sparse_header->total_chunks);
Ajay Dudani5c761132011-04-07 20:19:04 -07004456
4457 /* Start processing chunks */
4458 for (chunk=0; chunk<sparse_header->total_chunks; chunk++)
4459 {
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05304460 /* Make sure the total image size does not exceed the partition size */
4461 if(((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz) >= size) {
4462 fastboot_fail("size too large");
4463 return;
4464 }
Ajay Dudani5c761132011-04-07 20:19:04 -07004465 /* Read and skip over chunk header */
4466 chunk_header = (chunk_header_t *) data;
4467 data += sizeof(chunk_header_t);
4468
Parth Dixit64b1a482016-03-07 16:31:26 +05304469 if (data_end < (uintptr_t)data) {
vijay kumar800255e2015-04-24 20:26:19 +05304470 fastboot_fail("buffer overreads occured due to invalid sparse header");
4471 return;
4472 }
4473
Ajay Dudani5c761132011-04-07 20:19:04 -07004474 dprintf (SPEW, "=== Chunk Header ===\n");
4475 dprintf (SPEW, "chunk_type: 0x%x\n", chunk_header->chunk_type);
4476 dprintf (SPEW, "chunk_data_sz: 0x%x\n", chunk_header->chunk_sz);
4477 dprintf (SPEW, "total_size: 0x%x\n", chunk_header->total_sz);
4478
vijay kumar800255e2015-04-24 20:26:19 +05304479 if(sparse_header->chunk_hdr_sz != sizeof(chunk_header_t))
Ajay Dudani5c761132011-04-07 20:19:04 -07004480 {
vijay kumar800255e2015-04-24 20:26:19 +05304481 fastboot_fail("chunk header size mismatch");
4482 return;
Ajay Dudani5c761132011-04-07 20:19:04 -07004483 }
4484
wufeng.jiang813dc352015-06-02 23:02:46 -04004485 chunk_data_sz = (uint64_t)sparse_header->blk_sz * chunk_header->chunk_sz;
4486
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05304487 /* Make sure that the chunk size calculated from sparse image does not
4488 * exceed partition size
4489 */
4490 if ((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz + chunk_data_sz > size)
4491 {
4492 fastboot_fail("Chunk data size exceeds partition size");
4493 return;
4494 }
4495
Ajay Dudani5c761132011-04-07 20:19:04 -07004496 switch (chunk_header->chunk_type)
4497 {
4498 case CHUNK_TYPE_RAW:
wufeng.jiang813dc352015-06-02 23:02:46 -04004499 if((uint64_t)chunk_header->total_sz != ((uint64_t)sparse_header->chunk_hdr_sz +
Ajay Dudani5c761132011-04-07 20:19:04 -07004500 chunk_data_sz))
4501 {
4502 fastboot_fail("Bogus chunk size for chunk type Raw");
4503 return;
4504 }
4505
Parth Dixit64b1a482016-03-07 16:31:26 +05304506 if (data_end < (uintptr_t)data + chunk_data_sz) {
vijay kumar800255e2015-04-24 20:26:19 +05304507 fastboot_fail("buffer overreads occured due to invalid sparse header");
4508 return;
4509 }
4510
wufeng.jiang813dc352015-06-02 23:02:46 -04004511 /* chunk_header->total_sz is uint32,So chunk_data_sz is now less than 2^32
4512 otherwise it will return in the line above
4513 */
Ajay Dudaniab18f022011-05-12 14:39:22 -07004514 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
wufeng.jiang813dc352015-06-02 23:02:46 -04004515 (uint32_t)chunk_data_sz,
Ajay Dudaniab18f022011-05-12 14:39:22 -07004516 (unsigned int*)data))
Ajay Dudani5c761132011-04-07 20:19:04 -07004517 {
4518 fastboot_fail("flash write failure");
4519 return;
4520 }
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05304521 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
4522 fastboot_fail("Bogus size for RAW chunk type");
4523 return;
4524 }
Ajay Dudani5c761132011-04-07 20:19:04 -07004525 total_blocks += chunk_header->chunk_sz;
wufeng.jiang813dc352015-06-02 23:02:46 -04004526 data += (uint32_t)chunk_data_sz;
Ajay Dudani5c761132011-04-07 20:19:04 -07004527 break;
4528
Channagoud Kadabif627cf82013-09-09 14:08:20 -07004529 case CHUNK_TYPE_FILL:
4530 if(chunk_header->total_sz != (sparse_header->chunk_hdr_sz +
4531 sizeof(uint32_t)))
4532 {
4533 fastboot_fail("Bogus chunk size for chunk type FILL");
4534 return;
4535 }
4536
Mayank Grover4f50bba2017-07-19 18:17:08 +05304537 blk_sz_actual = ROUNDUP(sparse_header->blk_sz, CACHE_LINE);
4538 /* Integer overflow detected */
4539 if (blk_sz_actual < sparse_header->blk_sz)
4540 {
4541 fastboot_fail("Invalid block size");
4542 return;
4543 }
4544
4545 fill_buf = (uint32_t *)memalign(CACHE_LINE, blk_sz_actual);
Channagoud Kadabif627cf82013-09-09 14:08:20 -07004546 if (!fill_buf)
4547 {
4548 fastboot_fail("Malloc failed for: CHUNK_TYPE_FILL");
4549 return;
4550 }
4551
Parth Dixit64b1a482016-03-07 16:31:26 +05304552 if (data_end < (uintptr_t)data + sizeof(uint32_t)) {
vijay kumar800255e2015-04-24 20:26:19 +05304553 fastboot_fail("buffer overreads occured due to invalid sparse header");
Gaurav Nebhwani163cbf82016-03-17 21:21:27 +05304554 free(fill_buf);
vijay kumar800255e2015-04-24 20:26:19 +05304555 return;
4556 }
Channagoud Kadabif627cf82013-09-09 14:08:20 -07004557 fill_val = *(uint32_t *)data;
4558 data = (char *) data + sizeof(uint32_t);
Channagoud Kadabif627cf82013-09-09 14:08:20 -07004559
4560 for (i = 0; i < (sparse_header->blk_sz / sizeof(fill_val)); i++)
4561 {
4562 fill_buf[i] = fill_val;
4563 }
4564
Gaurav Nebhwanie94cbe12016-03-17 21:16:34 +05304565 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz))
4566 {
4567 fastboot_fail("bogus size for chunk FILL type");
4568 free(fill_buf);
4569 return;
4570 }
4571
wufeng.jiang813dc352015-06-02 23:02:46 -04004572 for (i = 0; i < chunk_header->chunk_sz; i++)
Channagoud Kadabif627cf82013-09-09 14:08:20 -07004573 {
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05304574 /* Make sure that the data written to partition does not exceed partition size */
4575 if ((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz + sparse_header->blk_sz > size)
4576 {
4577 fastboot_fail("Chunk data size for fill type exceeds partition size");
Gaurav Nebhwani163cbf82016-03-17 21:21:27 +05304578 free(fill_buf);
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05304579 return;
4580 }
4581
Channagoud Kadabif627cf82013-09-09 14:08:20 -07004582 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
4583 sparse_header->blk_sz,
4584 fill_buf))
4585 {
4586 fastboot_fail("flash write failure");
4587 free(fill_buf);
4588 return;
4589 }
4590
4591 total_blocks++;
4592 }
4593
4594 free(fill_buf);
4595 break;
4596
Ajay Dudani5c761132011-04-07 20:19:04 -07004597 case CHUNK_TYPE_DONT_CARE:
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05304598 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
4599 fastboot_fail("bogus size for chunk DONT CARE type");
4600 return;
4601 }
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07004602 total_blocks += chunk_header->chunk_sz;
4603 break;
4604
Ajay Dudani5c761132011-04-07 20:19:04 -07004605 case CHUNK_TYPE_CRC:
4606 if(chunk_header->total_sz != sparse_header->chunk_hdr_sz)
4607 {
wufeng.jiang813dc352015-06-02 23:02:46 -04004608 fastboot_fail("Bogus chunk size for chunk type CRC");
Ajay Dudani5c761132011-04-07 20:19:04 -07004609 return;
4610 }
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05304611 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
4612 fastboot_fail("bogus size for chunk CRC type");
4613 return;
4614 }
Ajay Dudani5c761132011-04-07 20:19:04 -07004615 total_blocks += chunk_header->chunk_sz;
Parth Dixit64b1a482016-03-07 16:31:26 +05304616 if ((uintptr_t)data > UINT_MAX - chunk_data_sz) {
vijay kumar800255e2015-04-24 20:26:19 +05304617 fastboot_fail("integer overflow occured");
4618 return;
4619 }
wufeng.jiang813dc352015-06-02 23:02:46 -04004620 data += (uint32_t)chunk_data_sz;
Parth Dixit64b1a482016-03-07 16:31:26 +05304621 if (data_end < (uintptr_t)data) {
vijay kumar800255e2015-04-24 20:26:19 +05304622 fastboot_fail("buffer overreads occured due to invalid sparse header");
4623 return;
4624 }
Ajay Dudani5c761132011-04-07 20:19:04 -07004625 break;
4626
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07004627 default:
Channagoud Kadabif627cf82013-09-09 14:08:20 -07004628 dprintf(CRITICAL, "Unkown chunk type: %x\n",chunk_header->chunk_type);
Ajay Dudani5c761132011-04-07 20:19:04 -07004629 fastboot_fail("Unknown chunk type");
4630 return;
4631 }
4632 }
4633
Ajay Dudani0c6927b2011-05-18 11:12:16 -07004634 dprintf(INFO, "Wrote %d blocks, expected to write %d blocks\n",
4635 total_blocks, sparse_header->total_blks);
4636
4637 if(total_blocks != sparse_header->total_blks)
4638 {
4639 fastboot_fail("sparse image write failure");
4640 }
Ajay Dudani5c761132011-04-07 20:19:04 -07004641
4642 fastboot_okay("");
4643 return;
4644}
4645
4646void cmd_flash_mmc(const char *arg, void *data, unsigned sz)
4647{
4648 sparse_header_t *sparse_header;
Ajay Dudanide984792015-03-02 09:57:41 -08004649 meta_header_t *meta_header;
Ajay Dudani5c761132011-04-07 20:19:04 -07004650
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07004651#ifdef SSD_ENABLE
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -08004652 /* 8 Byte Magic + 2048 Byte xml + Encrypted Data */
4653 unsigned int *magic_number = (unsigned int *) data;
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08004654 int ret=0;
4655 uint32 major_version=0;
4656 uint32 minor_version=0;
4657
4658 ret = scm_svc_version(&major_version,&minor_version);
4659 if(!ret)
4660 {
4661 if(major_version >= 2)
4662 {
Amir Samuelovbb65ce02013-05-05 12:20:18 +03004663 if( !strcmp(arg, "ssd") || !strcmp(arg, "tqs") )
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08004664 {
4665 ret = encrypt_scm((uint32 **) &data, &sz);
4666 if (ret != 0) {
4667 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
4668 return;
4669 }
4670
Amir Samuelovbb65ce02013-05-05 12:20:18 +03004671 /* Protect only for SSD */
4672 if (!strcmp(arg, "ssd")) {
4673 ret = scm_protect_keystore((uint32 *) data, sz);
4674 if (ret != 0) {
4675 dprintf(CRITICAL, "ERROR: scm_protect_keystore Failed\n");
4676 return;
4677 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08004678 }
4679 }
4680 else
4681 {
4682 ret = decrypt_scm_v2((uint32 **) &data, &sz);
4683 if(ret != 0)
4684 {
4685 dprintf(CRITICAL,"ERROR: Decryption Failure\n");
4686 return;
4687 }
4688 }
4689 }
4690 else
4691 {
4692 if (magic_number[0] == DECRYPT_MAGIC_0 &&
4693 magic_number[1] == DECRYPT_MAGIC_1)
4694 {
4695 ret = decrypt_scm((uint32 **) &data, &sz);
4696 if (ret != 0) {
4697 dprintf(CRITICAL, "ERROR: Invalid secure image\n");
4698 return;
4699 }
4700 }
4701 else if (magic_number[0] == ENCRYPT_MAGIC_0 &&
4702 magic_number[1] == ENCRYPT_MAGIC_1)
4703 {
4704 ret = encrypt_scm((uint32 **) &data, &sz);
4705 if (ret != 0) {
4706 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
4707 return;
4708 }
4709 }
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07004710 }
4711 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08004712 else
Neeti Desai127b9e02012-03-20 16:11:23 -07004713 {
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08004714 dprintf(CRITICAL,"INVALID SVC Version\n");
4715 return;
Neeti Desai127b9e02012-03-20 16:11:23 -07004716 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08004717#endif /* SSD_ENABLE */
Neeti Desai127b9e02012-03-20 16:11:23 -07004718
Monika Singh292b3e92018-03-17 22:40:23 +05304719#if VERIFIED_BOOT || VERIFIED_BOOT_2
Channagoud Kadabi35297672015-06-13 11:09:49 -07004720 if (target_build_variant_user())
Shashank Mittald3e54dd2014-08-28 15:24:02 -07004721 {
lijuang511a2b52015-08-14 20:50:51 +08004722 /* if device is locked:
4723 * common partition will not allow to be flashed
4724 * critical partition will allow to flash image.
4725 */
4726 if(!device.is_unlocked && !critical_flash_allowed(arg)) {
4727 fastboot_fail("Partition flashing is not allowed");
4728 return;
4729 }
Mayank Grover889be1b2017-09-12 20:12:23 +05304730
lijuang511a2b52015-08-14 20:50:51 +08004731 /* if device critical is locked:
4732 * common partition will allow to be flashed
4733 * critical partition will not allow to flash image.
4734 */
Mayank Grover912eaa62017-10-26 12:08:53 +05304735 if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05304736 !device.is_unlock_critical &&
4737 critical_flash_allowed(arg)) {
4738 fastboot_fail("Critical partition flashing is not allowed");
4739 return;
Shashank Mittald3e54dd2014-08-28 15:24:02 -07004740 }
4741 }
4742#endif
Monika Singh292b3e92018-03-17 22:40:23 +05304743 if (!strncmp(arg, "avb_custom_key", strlen("avb_custom_key"))) {
4744 dprintf(INFO, "flashing avb_custom_key\n");
4745 if (store_userkey(data, sz)) {
4746 fastboot_fail("Flashing avb_custom_key failed");
4747 } else {
4748 fastboot_okay("");
4749 }
4750 return;
4751 }
Shashank Mittald3e54dd2014-08-28 15:24:02 -07004752
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07004753 sparse_header = (sparse_header_t *) data;
Ajay Dudanide984792015-03-02 09:57:41 -08004754 meta_header = (meta_header_t *) data;
4755 if (sparse_header->magic == SPARSE_HEADER_MAGIC)
Ajay Dudani5c761132011-04-07 20:19:04 -07004756 cmd_flash_mmc_sparse_img(arg, data, sz);
Ajay Dudanide984792015-03-02 09:57:41 -08004757 else if (meta_header->magic == META_HEADER_MAGIC)
4758 cmd_flash_meta_img(arg, data, sz);
4759 else
4760 cmd_flash_mmc_img(arg, data, sz);
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07004761
4762#if VERIFIED_BOOT
Mayank Grover912eaa62017-10-26 12:08:53 +05304763 if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05304764 (!strncmp(arg, "system", 6)) &&
4765 !device.verity_mode)
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07004766 // reset dm_verity mode to enforcing
4767 device.verity_mode = 1;
4768 write_device_info(&device);
Parth Dixitddbc7352015-10-18 03:13:31 +05304769#endif
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07004770
Ajay Dudani5c761132011-04-07 20:19:04 -07004771 return;
4772}
4773
Tanya Brokhman84eaaf62015-02-24 21:57:06 +02004774void cmd_updatevol(const char *vol_name, void *data, unsigned sz)
4775{
4776 struct ptentry *sys_ptn;
4777 struct ptable *ptable;
4778
4779 ptable = flash_get_ptable();
4780 if (ptable == NULL) {
4781 fastboot_fail("partition table doesn't exist");
4782 return;
4783 }
4784
4785 sys_ptn = ptable_find(ptable, "system");
4786 if (sys_ptn == NULL) {
4787 fastboot_fail("system partition not found");
4788 return;
4789 }
4790
4791 sz = ROUND_TO_PAGE(sz, page_mask);
4792 if (update_ubi_vol(sys_ptn, vol_name, data, sz))
4793 fastboot_fail("update_ubi_vol failed");
4794 else
4795 fastboot_okay("");
4796}
4797
Sridhar Parasurame94e8152014-10-24 14:06:03 -07004798void cmd_flash_nand(const char *arg, void *data, unsigned sz)
Dima Zavin214cc642009-01-26 11:16:21 -08004799{
4800 struct ptentry *ptn;
4801 struct ptable *ptable;
4802 unsigned extra = 0;
Gaurav Nebhwanid9730712016-05-06 14:28:29 +05304803 uint64_t partition_size = 0;
Vijay Kumar Pendotic6f22a72017-05-19 22:44:34 +05304804 unsigned bytes_to_round_page = 0;
4805 unsigned rounded_size = 0;
Dima Zavin214cc642009-01-26 11:16:21 -08004806
Kishor PK38ed93d2017-04-25 14:19:26 +05304807 if((uintptr_t)data > (UINT_MAX - sz)) {
4808 fastboot_fail("Cannot flash: image header corrupt");
4809 return;
4810 }
4811
Dima Zavin214cc642009-01-26 11:16:21 -08004812 ptable = flash_get_ptable();
4813 if (ptable == NULL) {
4814 fastboot_fail("partition table doesn't exist");
4815 return;
4816 }
4817
4818 ptn = ptable_find(ptable, arg);
4819 if (ptn == NULL) {
Tanya Brokhman84eaaf62015-02-24 21:57:06 +02004820 dprintf(INFO, "unknown partition name (%s). Trying updatevol\n",
4821 arg);
4822 cmd_updatevol(arg, data, sz);
Dima Zavin214cc642009-01-26 11:16:21 -08004823 return;
4824 }
4825
Monika Singh292b3e92018-03-17 22:40:23 +05304826 if (!strncmp(arg, "avb_custom_key", strlen("avb_custom_key"))) {
4827 dprintf(INFO, "flashing avb_custom_key\n");
4828 if (store_userkey(data, sz)) {
4829 fastboot_fail("Flashing avb_custom_key failed");
4830 } else {
4831 fastboot_okay("");
4832 }
4833 return;
4834 }
4835
Dima Zavin214cc642009-01-26 11:16:21 -08004836 if (!strcmp(ptn->name, "boot") || !strcmp(ptn->name, "recovery")) {
Kishor PK38ed93d2017-04-25 14:19:26 +05304837 if((sz > BOOT_MAGIC_SIZE) && (!memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE))) {
4838 dprintf(INFO, "Verified the BOOT_MAGIC in image header \n");
4839 } else {
4840 fastboot_fail("Image is not a boot image");
Dima Zavin214cc642009-01-26 11:16:21 -08004841 return;
4842 }
4843 }
4844
Amol Jadi5c61a952012-05-04 17:05:35 -07004845 if (!strcmp(ptn->name, "system")
Deepa Dinamani13e32c42012-03-12 14:34:17 -07004846 || !strcmp(ptn->name, "userdata")
4847 || !strcmp(ptn->name, "persist")
Sundarajan Srinivasanb063a852013-11-19 14:02:27 -08004848 || !strcmp(ptn->name, "recoveryfs")
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -08004849 || !strcmp(ptn->name, "modem"))
Tanya Brokhman1c94f1a2015-02-15 09:05:03 +02004850 extra = 1;
Kishor PK38ed93d2017-04-25 14:19:26 +05304851 else {
Vijay Kumar Pendotic6f22a72017-05-19 22:44:34 +05304852 rounded_size = ROUNDUP(sz, page_size);
4853 bytes_to_round_page = rounded_size - sz;
4854 if (bytes_to_round_page) {
4855 if (((uintptr_t)data + sz ) > (UINT_MAX - bytes_to_round_page)) {
4856 fastboot_fail("Integer overflow detected");
4857 return;
4858 }
4859 if (((uintptr_t)data + sz + bytes_to_round_page) >
4860 ((uintptr_t)target_get_scratch_address() + target_get_max_flash_size())) {
4861 fastboot_fail("Buffer size is not aligned to page_size");
4862 return;
4863 }
4864 else {
4865 memset(data + sz, 0, bytes_to_round_page);
4866 sz = rounded_size;
4867 }
Kishor PK38ed93d2017-04-25 14:19:26 +05304868 }
Gaurav Nebhwanid9730712016-05-06 14:28:29 +05304869 }
4870
Kishor PK38ed93d2017-04-25 14:19:26 +05304871 /*Checking partition_size for the possible integer overflow */
4872 partition_size = validate_partition_size(ptn);
4873
Gaurav Nebhwanid9730712016-05-06 14:28:29 +05304874 if (sz > partition_size) {
4875 fastboot_fail("Image size too large");
4876 return;
4877 }
4878
Dima Zavin214cc642009-01-26 11:16:21 -08004879 dprintf(INFO, "writing %d bytes to '%s'\n", sz, ptn->name);
Mayank Grover6cde9352017-06-06 18:45:23 +05304880 if ((sz > UBI_EC_HDR_SIZE) &&
4881 (!memcmp((void *)data, UBI_MAGIC, UBI_MAGIC_SIZE))) {
Tanya Brokhman1c94f1a2015-02-15 09:05:03 +02004882 if (flash_ubi_img(ptn, data, sz)) {
4883 fastboot_fail("flash write failure");
4884 return;
4885 }
4886 } else {
4887 if (flash_write(ptn, extra, data, sz)) {
4888 fastboot_fail("flash write failure");
4889 return;
4890 }
Dima Zavin214cc642009-01-26 11:16:21 -08004891 }
4892 dprintf(INFO, "partition '%s' updated\n", ptn->name);
4893 fastboot_okay("");
4894}
4895
Kishor PK38ed93d2017-04-25 14:19:26 +05304896
4897static inline uint64_t validate_partition_size(struct ptentry *ptn)
4898{
4899 if (ptn->length && flash_num_pages_per_blk() && page_size) {
4900 if ((ptn->length < ( UINT_MAX / flash_num_pages_per_blk())) && ((ptn->length * flash_num_pages_per_blk()) < ( UINT_MAX / page_size))) {
4901 return ptn->length * flash_num_pages_per_blk() * page_size;
4902 }
4903 }
4904 return 0;
4905}
4906
4907
Sridhar Parasurame94e8152014-10-24 14:06:03 -07004908void cmd_flash(const char *arg, void *data, unsigned sz)
4909{
4910 if(target_is_emmc_boot())
4911 cmd_flash_mmc(arg, data, sz);
4912 else
4913 cmd_flash_nand(arg, data, sz);
4914}
4915
Dima Zavin214cc642009-01-26 11:16:21 -08004916void cmd_continue(const char *arg, void *data, unsigned sz)
4917{
4918 fastboot_okay("");
Amol Jadi7c4316c2013-10-07 14:19:26 -07004919 fastboot_stop();
Shashank Mittald3e54dd2014-08-28 15:24:02 -07004920
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004921 if (target_is_emmc_boot())
4922 {
lijuanga40d6302015-07-20 20:10:13 +08004923#if FBCON_DISPLAY_MSG
lijuangde34d502016-02-26 16:04:50 +08004924 /* Exit keys' detection thread firstly */
4925 exit_menu_keys_detection();
lijuanga40d6302015-07-20 20:10:13 +08004926#endif
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004927 boot_linux_from_mmc();
4928 }
4929 else
4930 {
4931 boot_linux_from_flash();
4932 }
Dima Zavin214cc642009-01-26 11:16:21 -08004933}
4934
Chandan Uddaraju94183c02010-01-15 15:13:59 -08004935void cmd_reboot(const char *arg, void *data, unsigned sz)
4936{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004937 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08004938 fastboot_okay("");
4939 reboot_device(0);
4940}
4941
Mayank Grover351a75e2017-05-30 20:06:08 +05304942void cmd_set_active(const char *arg, void *data, unsigned sz)
4943{
Mayank Grover6ccc1c92017-07-04 17:36:46 +05304944 char *p, *sp = NULL;
Mayank Grover351a75e2017-05-30 20:06:08 +05304945 unsigned i,current_active_slot;
4946 const char *current_slot_suffix;
4947
4948 if (!partition_multislot_is_supported())
4949 {
4950 fastboot_fail("Command not supported");
4951 return;
4952 }
4953
4954 if (arg)
4955 {
Mayank Grover6ccc1c92017-07-04 17:36:46 +05304956 p = strtok_r((char *)arg, ":", &sp);
Mayank Grover5eb5f692017-07-19 20:16:04 +05304957 if (p)
Mayank Grover351a75e2017-05-30 20:06:08 +05304958 {
4959 current_active_slot = partition_find_active_slot();
4960
4961 /* Check if trying to make curent slot active */
4962 current_slot_suffix = SUFFIX_SLOT(current_active_slot);
Mayank Grover6ccc1c92017-07-04 17:36:46 +05304963 current_slot_suffix = strtok_r((char *)current_slot_suffix,
4964 (char *)suffix_delimiter, &sp);
4965
Mayank Grover5eb5f692017-07-19 20:16:04 +05304966 if (current_slot_suffix &&
4967 !strncmp(p, current_slot_suffix, strlen(current_slot_suffix)))
Mayank Grover351a75e2017-05-30 20:06:08 +05304968 {
4969 fastboot_okay("Slot already set active");
4970 return;
4971 }
4972 else
4973 {
4974 for (i = 0; i < AB_SUPPORTED_SLOTS; i++)
4975 {
4976 current_slot_suffix = SUFFIX_SLOT(i);
Mayank Grover6ccc1c92017-07-04 17:36:46 +05304977 current_slot_suffix = strtok_r((char *)current_slot_suffix,
4978 (char *)suffix_delimiter, &sp);
Mayank Grover5eb5f692017-07-19 20:16:04 +05304979 if (current_slot_suffix &&
4980 !strncmp(p, current_slot_suffix, strlen(current_slot_suffix)))
Mayank Grover351a75e2017-05-30 20:06:08 +05304981 {
lijuang8b03e5f2019-07-12 18:16:19 +08004982 partition_switch_slots(current_active_slot, i, true);
Mayank Grover351a75e2017-05-30 20:06:08 +05304983 publish_getvar_multislot_vars();
4984 fastboot_okay("");
4985 return;
4986 }
4987 }
4988 }
4989 }
4990 }
4991 fastboot_fail("Invalid slot suffix.");
4992 return;
4993}
4994
Mayank Grover98c4c742019-04-25 17:21:37 +05304995#if DYNAMIC_PARTITION_SUPPORT
4996void cmd_reboot_fastboot(const char *arg, void *data, unsigned sz)
4997{
4998 dprintf(INFO, "rebooting the device - userspace fastboot\n");
4999 if (send_recovery_cmd(RECOVERY_BOOT_FASTBOOT_CMD)) {
5000 dprintf(CRITICAL, "ERROR: Failed to update recovery commands\n");
5001 fastboot_fail("Failed to update recovery command");
5002 return;
5003 }
5004 fastboot_okay("");
5005 reboot_device(REBOOT_MODE_UNKNOWN);
5006
5007 //shouldn't come here.
5008 dprintf(CRITICAL, "ERROR: Failed to reboot device\n");
5009 return;
5010}
5011
5012void cmd_reboot_recovery(const char *arg, void *data, unsigned sz)
5013{
5014 dprintf(INFO, "rebooting the device - recovery\n");
5015 if (send_recovery_cmd(RECOVERY_BOOT_RECOVERY_CMD)) {
5016 dprintf(CRITICAL, "ERROR: Failed to update recovery commands\n");
5017 fastboot_fail("Failed to update recovery command");
5018 return;
5019 }
5020 fastboot_okay("");
5021 reboot_device(REBOOT_MODE_UNKNOWN);
5022
5023 //shouldn't come here.
5024 dprintf(CRITICAL, "ERROR: Failed to reboot device\n");
5025 return;
5026}
5027#endif
5028
Chandan Uddaraju94183c02010-01-15 15:13:59 -08005029void cmd_reboot_bootloader(const char *arg, void *data, unsigned sz)
5030{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07005031 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08005032 fastboot_okay("");
5033 reboot_device(FASTBOOT_MODE);
5034}
5035
Ameya Thakur11cf1a62013-08-05 12:44:48 -07005036void cmd_oem_enable_charger_screen(const char *arg, void *data, unsigned size)
5037{
5038 dprintf(INFO, "Enabling charger screen check\n");
5039 device.charger_screen_enabled = 1;
5040 write_device_info(&device);
5041 fastboot_okay("");
5042}
5043
5044void cmd_oem_disable_charger_screen(const char *arg, void *data, unsigned size)
5045{
5046 dprintf(INFO, "Disabling charger screen check\n");
5047 device.charger_screen_enabled = 0;
5048 write_device_info(&device);
5049 fastboot_okay("");
5050}
5051
tracychuid184b912020-06-05 17:31:38 +08005052/*[20200605][TracyChui] Implement get Serial Number start*/
5053#if defined(ENABLE_PRODINFO_ACCESS)
5054void CmdOemEnableAdb(const char *arg, void *data, unsigned size)
5055{
5056 dprintf(INFO, "Enabling Adb\n");
5057 prod.is_adb_enabled = 1;
5058 write_prod_info(&prod);
5059 fastboot_okay("");
5060}
Allen Lu50e75472020-02-27 11:14:29 +08005061
5062void CmdOemEnableAdbCTS(const char *arg, void *data, unsigned size)
5063{
5064 dprintf(INFO, "Enabling Adb CTS\n");
5065 prod.is_adb_enabled = 1;
5066 prod.is_adb_enabled_cts = 1;
5067 write_prod_info(&prod);
5068 fastboot_okay("");
5069}
5070
5071void CmdOemDisableAdbCTS(const char *arg, void *data, unsigned size)
5072{
5073 dprintf(INFO, "disabling Adb CTS\n");
5074 prod.is_adb_enabled = 0;
5075 prod.is_adb_enabled_cts = 0;
5076 write_prod_info(&prod);
5077 fastboot_okay("");
5078}
tracychuid184b912020-06-05 17:31:38 +08005079#endif
5080/*[20200605][TracyChui] Implement get Serial Number end*/
5081
lijuanga25d8bb2015-09-16 13:11:52 +08005082void cmd_oem_off_mode_charger(const char *arg, void *data, unsigned size)
5083{
5084 char *p = NULL;
5085 const char *delim = " \t\n\r";
Parth Dixit407f15b2016-01-07 14:47:37 +05305086 char *sp;
lijuanga25d8bb2015-09-16 13:11:52 +08005087
5088 if (arg) {
Parth Dixit407f15b2016-01-07 14:47:37 +05305089 p = strtok_r((char *)arg, delim, &sp);
lijuanga25d8bb2015-09-16 13:11:52 +08005090 if (p) {
5091 if (!strncmp(p, "0", 1)) {
5092 device.charger_screen_enabled = 0;
5093 } else if (!strncmp(p, "1", 1)) {
5094 device.charger_screen_enabled = 1;
5095 }
5096 }
5097 }
5098
5099 /* update charger_screen_enabled value for getvar
5100 * command
5101 */
5102 snprintf(charger_screen_enabled, MAX_RSP_SIZE, "%d",
5103 device.charger_screen_enabled);
5104
5105 write_device_info(&device);
5106 fastboot_okay("");
5107}
5108
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05305109void cmd_oem_select_display_panel(const char *arg, void *data, unsigned size)
5110{
5111 dprintf(INFO, "Selecting display panel %s\n", arg);
5112 if (arg)
5113 strlcpy(device.display_panel, arg,
5114 sizeof(device.display_panel));
5115 write_device_info(&device);
5116 fastboot_okay("");
5117}
5118
Shashank Mittal162244e2011-08-08 19:01:25 -07005119void cmd_oem_unlock(const char *arg, void *data, unsigned sz)
5120{
lijuang4ece1e72015-08-14 21:02:36 +08005121 set_device_unlock(UNLOCK, TRUE);
vijay kumarc65876c2015-04-24 13:29:16 +05305122}
5123
tracychui8fd71dc2020-06-11 10:17:13 +08005124/*[TracyChui] Add lock unlock fastboot commnad to skip ui check 20200611 start*/
5125#if defined(ENABLE_LOCK_UNLOCK_SKIP_UI_CHECK)
5126void cmd_oem_unlock_skip_ui_check(const char *arg, void *data, unsigned sz)
5127{
5128 set_device_unlock_skip_ui_check(UNLOCK, TRUE);
5129}
5130#endif
5131/*[TracyChui] Add lock unlock fastboot commnad to skip ui check 20200611 end */
5132
vijay kumarc65876c2015-04-24 13:29:16 +05305133void cmd_oem_unlock_go(const char *arg, void *data, unsigned sz)
5134{
lijuang4ece1e72015-08-14 21:02:36 +08005135 if(!device.is_unlocked) {
vijay kumarc65876c2015-04-24 13:29:16 +05305136 if(!is_allow_unlock) {
5137 fastboot_fail("oem unlock is not allowed");
5138 return;
5139 }
5140
lijuang4ece1e72015-08-14 21:02:36 +08005141 set_device_unlock_value(UNLOCK, TRUE);
vijay kumarc65876c2015-04-24 13:29:16 +05305142
lijuang4ece1e72015-08-14 21:02:36 +08005143 /* wipe data */
vijay kumarc65876c2015-04-24 13:29:16 +05305144 struct recovery_message msg;
Kishor PK60a68212017-05-08 16:55:57 +05305145 memset(&msg, 0, sizeof(msg));
vijay kumarc65876c2015-04-24 13:29:16 +05305146 snprintf(msg.recovery, sizeof(msg.recovery), "recovery\n--wipe_data");
5147 write_misc(0, &msg, sizeof(msg));
5148
5149 fastboot_okay("");
5150 reboot_device(RECOVERY_MODE);
Shashank Mittald3e54dd2014-08-28 15:24:02 -07005151 }
5152 fastboot_okay("");
5153}
5154
Channagoud Kadabiad259832015-05-29 11:14:17 -07005155static int aboot_frp_unlock(char *pname, void *data, unsigned sz)
5156{
Mayank Grover70f8b0e2017-05-17 11:49:00 +05305157 int ret=1;
5158 bool authentication_success=false;
Channagoud Kadabiad259832015-05-29 11:14:17 -07005159
Mayank Grover70f8b0e2017-05-17 11:49:00 +05305160 /*
5161 Authentication method not implemented.
5162
5163 OEM to implement, authentication system which on successful validataion,
5164 calls write_allow_oem_unlock() with is_allow_unlock.
5165 */
5166#if 0
5167 authentication_success = oem_specific_auth_mthd();
5168#endif
5169
5170 if (authentication_success)
Channagoud Kadabiad259832015-05-29 11:14:17 -07005171 {
Mayank Grover70f8b0e2017-05-17 11:49:00 +05305172 is_allow_unlock = true;
5173 write_allow_oem_unlock(is_allow_unlock);
5174 ret = 0;
Channagoud Kadabiad259832015-05-29 11:14:17 -07005175 }
5176 return ret;
5177}
5178
Shashank Mittald3e54dd2014-08-28 15:24:02 -07005179void cmd_oem_lock(const char *arg, void *data, unsigned sz)
5180{
lijuang4ece1e72015-08-14 21:02:36 +08005181 set_device_unlock(UNLOCK, FALSE);
Shashank Mittal162244e2011-08-08 19:01:25 -07005182}
5183
tracychui8fd71dc2020-06-11 10:17:13 +08005184/*[TracyChui] Add lock unlock fastboot commnad to skip ui check 20200611 start*/
5185#if defined(ENABLE_LOCK_UNLOCK_SKIP_UI_CHECK)
5186void cmd_oem_lock_skip_ui_check(const char *arg, void *data, unsigned sz)
5187{
5188 set_device_unlock_skip_ui_check(UNLOCK, FALSE);
5189}
5190#endif
5191/*[TracyChui] Add lock unlock fastboot commnad to skip ui check 20200611 end */
5192
Shashank Mittala0032282011-08-26 14:50:11 -07005193void cmd_oem_devinfo(const char *arg, void *data, unsigned sz)
5194{
lijuang511a2b52015-08-14 20:50:51 +08005195 char response[MAX_RSP_SIZE];
Ameya Thakur11cf1a62013-08-05 12:44:48 -07005196 snprintf(response, sizeof(response), "\tDevice tampered: %s", (device.is_tampered ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07005197 fastboot_info(response);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07005198 snprintf(response, sizeof(response), "\tDevice unlocked: %s", (device.is_unlocked ? "true" : "false"));
5199 fastboot_info(response);
Monika Singh292b3e92018-03-17 22:40:23 +05305200#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover912eaa62017-10-26 12:08:53 +05305201 if (VB_M <= target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +05305202 {
5203 snprintf(response, sizeof(response), "\tDevice critical unlocked: %s",
5204 (device.is_unlock_critical ? "true" : "false"));
5205 fastboot_info(response);
5206 }
Parth Dixitddbc7352015-10-18 03:13:31 +05305207#endif
Ameya Thakur11cf1a62013-08-05 12:44:48 -07005208 snprintf(response, sizeof(response), "\tCharger screen enabled: %s", (device.charger_screen_enabled ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07005209 fastboot_info(response);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05305210 snprintf(response, sizeof(response), "\tDisplay panel: %s", (device.display_panel));
5211 fastboot_info(response);
tracychuid184b912020-06-05 17:31:38 +08005212/*[20200605][TracyChui] Implement get Serial Number start*/
5213#if defined(ENABLE_PRODINFO_ACCESS)
5214 snprintf(response, sizeof(response), "\tAdb enabled: %s", prod.is_adb_enabled ? "true" : "false");
5215 fastboot_info(response);
5216#endif
5217/*[20200605][TracyChui] Implement get Serial Number end*/
Shashank Mittala0032282011-08-26 14:50:11 -07005218 fastboot_okay("");
5219}
5220
lijuang511a2b52015-08-14 20:50:51 +08005221void cmd_flashing_get_unlock_ability(const char *arg, void *data, unsigned sz)
5222{
5223 char response[MAX_RSP_SIZE];
5224 snprintf(response, sizeof(response), "\tget_unlock_ability: %d", is_allow_unlock);
5225 fastboot_info(response);
5226 fastboot_okay("");
5227}
5228
5229void cmd_flashing_lock_critical(const char *arg, void *data, unsigned sz)
5230{
lijuang4ece1e72015-08-14 21:02:36 +08005231 set_device_unlock(UNLOCK_CRITICAL, FALSE);
lijuang511a2b52015-08-14 20:50:51 +08005232}
5233
5234void cmd_flashing_unlock_critical(const char *arg, void *data, unsigned sz)
5235{
lijuang4ece1e72015-08-14 21:02:36 +08005236 set_device_unlock(UNLOCK_CRITICAL, TRUE);
lijuang511a2b52015-08-14 20:50:51 +08005237}
5238
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07005239void cmd_preflash(const char *arg, void *data, unsigned sz)
5240{
5241 fastboot_okay("");
5242}
5243
Mao Flynn7b379f32015-04-20 00:28:30 +08005244static uint8_t logo_header[LOGO_IMG_HEADER_SIZE];
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05305245
Mao Flynn7b379f32015-04-20 00:28:30 +08005246int splash_screen_check_header(logo_img_header *header)
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05305247{
Mao Flynn7b379f32015-04-20 00:28:30 +08005248 if (memcmp(header->magic, LOGO_IMG_MAGIC, 8))
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05305249 return -1;
Mao Flynn7b379f32015-04-20 00:28:30 +08005250 if (header->width == 0 || header->height == 0)
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05305251 return -1;
5252 return 0;
5253}
5254
Mao Flynn7b379f32015-04-20 00:28:30 +08005255int splash_screen_flash()
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07005256{
5257 struct ptentry *ptn;
5258 struct ptable *ptable;
Mao Flynn7b379f32015-04-20 00:28:30 +08005259 struct logo_img_header *header;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07005260 struct fbcon_config *fb_display = NULL;
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08005261
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05305262 ptable = flash_get_ptable();
5263 if (ptable == NULL) {
Mao Flynn7b379f32015-04-20 00:28:30 +08005264 dprintf(CRITICAL, "ERROR: Partition table not found\n");
5265 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05305266 }
Mao Flynn7b379f32015-04-20 00:28:30 +08005267
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05305268 ptn = ptable_find(ptable, "splash");
5269 if (ptn == NULL) {
5270 dprintf(CRITICAL, "ERROR: splash Partition not found\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08005271 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05305272 }
Mao Flynn7b379f32015-04-20 00:28:30 +08005273 if (flash_read(ptn, 0, (void *)logo_header, LOGO_IMG_HEADER_SIZE)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05305274 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08005275 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05305276 }
5277
Mao Flynn7b379f32015-04-20 00:28:30 +08005278 header = (struct logo_img_header *)logo_header;
5279 if (splash_screen_check_header(header)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05305280 dprintf(CRITICAL, "ERROR: Boot image header invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08005281 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05305282 }
5283
5284 fb_display = fbcon_display();
5285 if (fb_display) {
raghavendra ambadas554f4fd2018-10-09 15:50:40 +05305286 if (header->type && (header->blocks != 0) &&
5287 (UINT_MAX >= header->blocks * 512) &&
5288 ((header->blocks * 512) <= (fb_display->width *
5289 fb_display->height * (fb_display->bpp / 8)))) {
5290 /* RLE24 compressed data */
Mao Flynn7b379f32015-04-20 00:28:30 +08005291 uint8_t *base = (uint8_t *) fb_display->base + LOGO_IMG_OFFSET;
5292
5293 /* if the logo is full-screen size, remove "fbcon_clear()" */
5294 if ((header->width != fb_display->width)
5295 || (header->height != fb_display->height))
5296 fbcon_clear();
5297
5298 if (flash_read(ptn + LOGO_IMG_HEADER_SIZE, 0,
5299 (uint32_t *)base,
5300 (header->blocks * 512))) {
5301 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
5302 return -1;
5303 }
5304 fbcon_extract_to_screen(header, base);
5305 return 0;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07005306 }
Mao Flynn7b379f32015-04-20 00:28:30 +08005307
Veera Sundaram Sankaranc33416f2015-09-28 10:25:55 -07005308 if ((header->width > fb_display->width) || (header->height > fb_display->height)) {
5309 dprintf(CRITICAL, "Logo config greater than fb config. Fall back default logo\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08005310 return -1;
5311 }
5312
Vineet Bajaj99291ed2014-09-09 12:29:46 +05305313 uint8_t *base = (uint8_t *) fb_display->base;
Sachin Bhayare619e9e42017-05-15 13:10:31 +05305314 uint32_t fb_size = ROUNDUP(fb_display->width *
5315 fb_display->height *
5316 (fb_display->bpp / 8), 4096);
5317 uint32_t splash_size = ((((header->width * header->height *
5318 fb_display->bpp/8) + 511) >> 9) << 9);
5319
5320 if (splash_size > fb_size) {
5321 dprintf(CRITICAL, "ERROR: Splash image size invalid\n");
5322 return -1;
5323 }
5324
Mao Flynn7b379f32015-04-20 00:28:30 +08005325 if (flash_read(ptn + LOGO_IMG_HEADER_SIZE, 0,
5326 (uint32_t *)base,
5327 ((((header->width * header->height * fb_display->bpp/8) + 511) >> 9) << 9))) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05305328 fbcon_clear();
Vineet Bajaj99291ed2014-09-09 12:29:46 +05305329 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08005330 return -1;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07005331 }
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05305332 }
5333
Mao Flynn7b379f32015-04-20 00:28:30 +08005334 return 0;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05305335}
5336
Mao Flynn7b379f32015-04-20 00:28:30 +08005337int splash_screen_mmc()
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05305338{
5339 int index = INVALID_PTN;
5340 unsigned long long ptn = 0;
5341 struct fbcon_config *fb_display = NULL;
Mao Flynn7b379f32015-04-20 00:28:30 +08005342 struct logo_img_header *header;
Mao Flynn1893a7f2015-06-03 12:03:36 +08005343 uint32_t blocksize, realsize, readsize;
5344 uint8_t *base;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05305345
5346 index = partition_get_index("splash");
5347 if (index == 0) {
5348 dprintf(CRITICAL, "ERROR: splash Partition table not found\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08005349 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05305350 }
5351
5352 ptn = partition_get_offset(index);
5353 if (ptn == 0) {
5354 dprintf(CRITICAL, "ERROR: splash Partition invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08005355 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05305356 }
5357
Mao Flynn1893a7f2015-06-03 12:03:36 +08005358 mmc_set_lun(partition_get_lun(index));
5359
5360 blocksize = mmc_get_device_blocksize();
5361 if (blocksize == 0) {
5362 dprintf(CRITICAL, "ERROR:splash Partition invalid blocksize\n");
5363 return -1;
5364 }
5365
5366 fb_display = fbcon_display();
Channagoud Kadabidaf10a62015-07-22 11:51:55 -07005367 if (!fb_display)
5368 {
5369 dprintf(CRITICAL, "ERROR: fb config is not allocated\n");
5370 return -1;
5371 }
5372
Mao Flynn1893a7f2015-06-03 12:03:36 +08005373 base = (uint8_t *) fb_display->base;
5374
jialongjhan1efba002020-05-22 18:25:56 +08005375 //[Arima][8901][JialongJhan]splash image partition offset might be incorrect 20190503 Start
5376 if (mmc_read(ptn , (uint32_t *)(base + LOGO_IMG_OFFSET), blocksize)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05305377 dprintf(CRITICAL, "ERROR: Cannot read splash image header\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08005378 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05305379 }
jialongjhan1efba002020-05-22 18:25:56 +08005380 //[Arima][8901][JialongJhan]splash image partition offset might be incorrect 20190503 End
Mao Flynn1893a7f2015-06-03 12:03:36 +08005381 header = (struct logo_img_header *)(base + LOGO_IMG_OFFSET);
Mao Flynn7b379f32015-04-20 00:28:30 +08005382 if (splash_screen_check_header(header)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05305383 dprintf(CRITICAL, "ERROR: Splash image header invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08005384 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05305385 }
5386
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05305387 if (fb_display) {
raghavendra ambadas554f4fd2018-10-09 15:50:40 +05305388 if (header->type && (header->blocks != 0) &&
5389 (UINT_MAX >= header->blocks * 512 + LOGO_IMG_HEADER_SIZE) &&
5390 ((header->blocks * 512) <= (fb_display->width *
5391 fb_display->height * (fb_display->bpp / 8)))) {
5392 /* 1 RLE24 compressed data */
Mao Flynn1893a7f2015-06-03 12:03:36 +08005393 base += LOGO_IMG_OFFSET;
Mao Flynn7b379f32015-04-20 00:28:30 +08005394
Mao Flynn1893a7f2015-06-03 12:03:36 +08005395 realsize = header->blocks * 512;
5396 readsize = ROUNDUP((realsize + LOGO_IMG_HEADER_SIZE), blocksize) - blocksize;
5397
5398 /* if the logo is not full-screen size, clean screen */
Mao Flynn7b379f32015-04-20 00:28:30 +08005399 if ((header->width != fb_display->width)
5400 || (header->height != fb_display->height))
5401 fbcon_clear();
5402
Sachin Bhayare619e9e42017-05-15 13:10:31 +05305403 uint32_t fb_size = ROUNDUP(fb_display->width *
5404 fb_display->height *
5405 (fb_display->bpp / 8), 4096);
5406
5407 if (readsize > fb_size) {
5408 dprintf(CRITICAL, "ERROR: Splash image size invalid\n");
5409 return -1;
5410 }
5411
jialongjhan1efba002020-05-22 18:25:56 +08005412 //[Arima][8901][JialongJhan]splash image partition offset might be incorrect 20190503 Start
5413 if (mmc_read(ptn + blocksize, (uint32_t *)(base + blocksize), readsize)) {
Mao Flynn7b379f32015-04-20 00:28:30 +08005414 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
5415 return -1;
5416 }
jialongjhan1efba002020-05-22 18:25:56 +08005417 //[Arima][8901][JialongJhan]splash image partition offset might be incorrect 20190503 End
Mao Flynn7b379f32015-04-20 00:28:30 +08005418
Mao Flynn1893a7f2015-06-03 12:03:36 +08005419 fbcon_extract_to_screen(header, (base + LOGO_IMG_HEADER_SIZE));
5420 } else { /* 2 Raw BGR data */
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05305421
Veera Sundaram Sankaranc33416f2015-09-28 10:25:55 -07005422 if ((header->width > fb_display->width) || (header->height > fb_display->height)) {
5423 dprintf(CRITICAL, "Logo config greater than fb config. Fall back default logo\n");
Mao Flynn1893a7f2015-06-03 12:03:36 +08005424 return -1;
5425 }
5426
5427 realsize = header->width * header->height * fb_display->bpp / 8;
5428 readsize = ROUNDUP((realsize + LOGO_IMG_HEADER_SIZE), blocksize) - blocksize;
5429
5430 if (blocksize == LOGO_IMG_HEADER_SIZE) { /* read the content directly */
Kishor PKee5c0a32018-03-06 16:49:46 +05305431 if (mmc_read((ptn + PLL_CODES_OFFSET + LOGO_IMG_HEADER_SIZE), (uint32_t *)base, readsize)) {
Mao Flynn1893a7f2015-06-03 12:03:36 +08005432 fbcon_clear();
5433 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
5434 return -1;
5435 }
5436 } else {
Kishor PKee5c0a32018-03-06 16:49:46 +05305437 if (mmc_read(ptn + PLL_CODES_OFFSET + blocksize ,
Mao Flynn1893a7f2015-06-03 12:03:36 +08005438 (uint32_t *)(base + LOGO_IMG_OFFSET + blocksize), readsize)) {
5439 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
5440 return -1;
5441 }
5442 memmove(base, (base + LOGO_IMG_OFFSET + LOGO_IMG_HEADER_SIZE), realsize);
5443 }
5444 }
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05305445 }
5446
Mao Flynn7b379f32015-04-20 00:28:30 +08005447 return 0;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05305448}
5449
Mao Flynn7b379f32015-04-20 00:28:30 +08005450int fetch_image_from_partition()
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05305451{
5452 if (target_is_emmc_boot()) {
5453 return splash_screen_mmc();
5454 } else {
5455 return splash_screen_flash();
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07005456 }
5457}
5458
Mayank Grover351a75e2017-05-30 20:06:08 +05305459void publish_getvar_multislot_vars()
5460{
5461 int i,count;
5462 static bool published = false;
5463 static char slot_count[MAX_RSP_SIZE];
5464 static struct ab_slot_info slot_info[AB_SUPPORTED_SLOTS];
5465 static char active_slot_suffix[MAX_RSP_SIZE];
5466 static char has_slot_pname[NUM_PARTITIONS][MAX_GET_VAR_NAME_SIZE];
5467 static char has_slot_reply[NUM_PARTITIONS][MAX_RSP_SIZE];
5468 const char *tmp;
5469 char tmpbuff[MAX_GET_VAR_NAME_SIZE];
Mayank Grover2bd84bb2017-06-20 11:06:07 +05305470 signed active_slt;
Mayank Grover351a75e2017-05-30 20:06:08 +05305471
5472 if (!published)
5473 {
5474 /* Update slot meta info */
5475 count = partition_fill_partition_meta(has_slot_pname, has_slot_reply,
5476 partition_get_partition_count());
5477 for(i=0; i<count; i++)
5478 {
5479 memset(tmpbuff, 0, MAX_GET_VAR_NAME_SIZE);
Mayank Grover2bd84bb2017-06-20 11:06:07 +05305480 snprintf(tmpbuff, MAX_GET_VAR_NAME_SIZE,"has-slot:%s",
5481 has_slot_pname[i]);
5482 strlcpy(has_slot_pname[i], tmpbuff, MAX_GET_VAR_NAME_SIZE);
Mayank Grover351a75e2017-05-30 20:06:08 +05305483 fastboot_publish(has_slot_pname[i], has_slot_reply[i]);
5484 }
5485
5486 for (i=0; i<AB_SUPPORTED_SLOTS; i++)
5487 {
5488 tmp = SUFFIX_SLOT(i);
Mayank Grover7b8a8f82017-10-27 13:07:59 +05305489 tmp++; // to remove "_" from slot_suffix.
Mayank Grover2bd84bb2017-06-20 11:06:07 +05305490 snprintf(slot_info[i].slot_is_unbootable, sizeof(slot_info[i].slot_is_unbootable),
5491 "slot-unbootable:%s", tmp);
5492 snprintf(slot_info[i].slot_is_active, sizeof(slot_info[i].slot_is_active),
5493 "slot-active:%s", tmp);
5494 snprintf(slot_info[i].slot_is_succesful, sizeof(slot_info[i].slot_is_succesful),
5495 "slot-success:%s", tmp);
5496 snprintf(slot_info[i].slot_retry_count, sizeof(slot_info[i].slot_retry_count),
5497 "slot-retry-count:%s", tmp);
Mayank Grover351a75e2017-05-30 20:06:08 +05305498 fastboot_publish(slot_info[i].slot_is_unbootable,
5499 slot_info[i].slot_is_unbootable_rsp);
5500 fastboot_publish(slot_info[i].slot_is_active,
5501 slot_info[i].slot_is_active_rsp);
5502 fastboot_publish(slot_info[i].slot_is_succesful,
5503 slot_info[i].slot_is_succesful_rsp);
5504 fastboot_publish(slot_info[i].slot_retry_count,
5505 slot_info[i].slot_retry_count_rsp);
5506 }
5507 fastboot_publish("current-slot", active_slot_suffix);
5508 snprintf(slot_count, sizeof(slot_count),"%d", AB_SUPPORTED_SLOTS);
5509 fastboot_publish("slot-count", slot_count);
5510 published = true;
5511 }
5512
Mayank Grover2bd84bb2017-06-20 11:06:07 +05305513 active_slt = partition_find_active_slot();
5514 if (active_slt != INVALID)
Mayank Grover7b8a8f82017-10-27 13:07:59 +05305515 {
5516 tmp = SUFFIX_SLOT(active_slt);
5517 tmp++; // to remove "_" from slot_suffix.
5518 snprintf(active_slot_suffix, sizeof(active_slot_suffix), "%s", tmp);
5519 }
Mayank Grover2bd84bb2017-06-20 11:06:07 +05305520 else
5521 strlcpy(active_slot_suffix, "INVALID", sizeof(active_slot_suffix));
5522
Mayank Grover351a75e2017-05-30 20:06:08 +05305523 /* Update partition meta information */
5524 partition_fill_slot_meta(slot_info);
5525 return;
5526}
5527
lijuang4ece1e72015-08-14 21:02:36 +08005528void get_product_name(unsigned char *buf)
5529{
5530 snprintf((char*)buf, MAX_RSP_SIZE, "%s", TARGET(BOARD));
5531 return;
5532}
5533
Mayank Grover7b8a8f82017-10-27 13:07:59 +05305534#if PRODUCT_IOT
5535void get_bootloader_version_iot(unsigned char *buf)
5536{
5537 if (buf != NULL)
5538 {
5539 strlcpy(buf, TARGET(BOARD), MAX_VERSION_LEN);
5540 strlcat(buf, "-", MAX_VERSION_LEN);
5541 strlcat(buf, PRODUCT_IOT_VERSION, MAX_VERSION_LEN);
5542 }
5543 return;
5544}
5545#endif
5546
lijuang4ece1e72015-08-14 21:02:36 +08005547void get_bootloader_version(unsigned char *buf)
5548{
jhchen7a504d12020-04-24 15:45:57 +08005549 snprintf((char*)buf, MAX_RSP_SIZE, "%s", device.bootloader_version);
lijuang4ece1e72015-08-14 21:02:36 +08005550 return;
5551}
5552
5553void get_baseband_version(unsigned char *buf)
5554{
jhchen7a504d12020-04-24 15:45:57 +08005555 snprintf((char*)buf, MAX_RSP_SIZE, "%s", device.radio_version);
lijuang4ece1e72015-08-14 21:02:36 +08005556 return;
5557}
5558
Monika Singh32a09f72018-03-14 13:08:29 +05305559bool is_device_locked_critical()
5560{
5561 return device.is_unlock_critical ? false:true;
5562}
5563
lijuang4ece1e72015-08-14 21:02:36 +08005564bool is_device_locked()
5565{
5566 return device.is_unlocked ? false:true;
5567}
5568
Monika Singh32a09f72018-03-14 13:08:29 +05305569bool is_verity_enforcing()
5570{
5571 return device.verity_mode ? true:false;
5572}
5573
Amol Jadi5edf3552013-07-23 14:15:34 -07005574/* register commands and variables for fastboot */
5575void aboot_fastboot_register_commands(void)
5576{
Sridhar Parasurame94e8152014-10-24 14:06:03 -07005577 int i;
lijuangf16461c2015-08-03 17:09:34 +08005578 char hw_platform_buf[MAX_RSP_SIZE];
Amol Jadi5edf3552013-07-23 14:15:34 -07005579
Sridhar Parasurame94e8152014-10-24 14:06:03 -07005580 struct fastboot_cmd_desc cmd_list[] = {
lijuang511a2b52015-08-14 20:50:51 +08005581 /* By default the enabled list is empty. */
5582 {"", NULL},
5583 /* move commands enclosed within the below ifndef to here
5584 * if they need to be enabled in user build.
5585 */
Sridhar Parasurame94e8152014-10-24 14:06:03 -07005586#ifndef DISABLE_FASTBOOT_CMDS
lijuang511a2b52015-08-14 20:50:51 +08005587 /* Register the following commands only for non-user builds */
5588 {"flash:", cmd_flash},
5589 {"erase:", cmd_erase},
5590 {"boot", cmd_boot},
5591 {"continue", cmd_continue},
5592 {"reboot", cmd_reboot},
5593 {"reboot-bootloader", cmd_reboot_bootloader},
5594 {"oem unlock", cmd_oem_unlock},
5595 {"oem unlock-go", cmd_oem_unlock_go},
5596 {"oem lock", cmd_oem_lock},
tracychui8fd71dc2020-06-11 10:17:13 +08005597/*[TracyChui] Add lock unlock fastboot commnad to skip ui check 20200611 start */
5598#if defined(ENABLE_LOCK_UNLOCK_SKIP_UI_CHECK)
5599 {"oem 8901_unlock", cmd_oem_unlock_skip_ui_check},
5600 {"oem 8901_lock", cmd_oem_lock_skip_ui_check},
5601#endif
5602/*[TracyChui] Add lock unlock fastboot commnad to skip ui check 20200611 end */
lijuang511a2b52015-08-14 20:50:51 +08005603 {"flashing unlock", cmd_oem_unlock},
5604 {"flashing lock", cmd_oem_lock},
5605 {"flashing lock_critical", cmd_flashing_lock_critical},
5606 {"flashing unlock_critical", cmd_flashing_unlock_critical},
5607 {"flashing get_unlock_ability", cmd_flashing_get_unlock_ability},
5608 {"oem device-info", cmd_oem_devinfo},
5609 {"preflash", cmd_preflash},
5610 {"oem enable-charger-screen", cmd_oem_enable_charger_screen},
5611 {"oem disable-charger-screen", cmd_oem_disable_charger_screen},
lijuanga25d8bb2015-09-16 13:11:52 +08005612 {"oem off-mode-charge", cmd_oem_off_mode_charger},
lijuang511a2b52015-08-14 20:50:51 +08005613 {"oem select-display-panel", cmd_oem_select_display_panel},
Mayank Grover6ccc1c92017-07-04 17:36:46 +05305614 {"set_active",cmd_set_active},
Mayank Grover98c4c742019-04-25 17:21:37 +05305615#if DYNAMIC_PARTITION_SUPPORT
5616 {"reboot-fastboot",cmd_reboot_fastboot},
5617 {"reboot-recovery",cmd_reboot_recovery},
5618#endif
Channagoud Kadabi736c4962015-08-21 11:56:52 -07005619#if UNITTEST_FW_SUPPORT
Channagoud Kadabid5ddf482015-09-28 10:31:35 -07005620 {"oem run-tests", cmd_oem_runtests},
Channagoud Kadabi736c4962015-08-21 11:56:52 -07005621#endif
Sridhar Parasurame94e8152014-10-24 14:06:03 -07005622#endif
tracychuid184b912020-06-05 17:31:38 +08005623/*[20200605][TracyChui] Implement get Serial Number start*/
5624#if defined(ENABLE_PRODINFO_ACCESS)
5625 {"oem adb_enable", CmdOemEnableAdb},
Allen Lu50e75472020-02-27 11:14:29 +08005626 {"oem adb_enable_cts", CmdOemEnableAdbCTS},
5627 {"oem adb_disable_cts", CmdOemDisableAdbCTS},
tracychuid184b912020-06-05 17:31:38 +08005628#endif
5629/*[20200605][TracyChui] Implement get Serial Number end*/
lijuang511a2b52015-08-14 20:50:51 +08005630 };
Sridhar Parasurame94e8152014-10-24 14:06:03 -07005631
5632 int fastboot_cmds_count = sizeof(cmd_list)/sizeof(cmd_list[0]);
5633 for (i = 1; i < fastboot_cmds_count; i++)
5634 fastboot_register(cmd_list[i].name,cmd_list[i].cb);
5635
Amol Jadi5edf3552013-07-23 14:15:34 -07005636 /* publish variables and their values */
5637 fastboot_publish("product", TARGET(BOARD));
5638 fastboot_publish("kernel", "lk");
5639 fastboot_publish("serialno", sn_buf);
5640
Mayank Grovera64dfbe2018-05-17 14:06:34 +05305641 /*publish hw-revision major(upper 16 bits) and minor(lower 16 bits)*/
5642 snprintf(soc_version_str, MAX_RSP_SIZE, "%x", board_soc_version());
5643 fastboot_publish("hw-revision", soc_version_str);
5644
Amol Jadi5edf3552013-07-23 14:15:34 -07005645 /*
5646 * partition info is supported only for emmc partitions
5647 * Calling this for NAND prints some error messages which
5648 * is harmless but misleading. Avoid calling this for NAND
5649 * devices.
5650 */
5651 if (target_is_emmc_boot())
Mayank Grover7b8a8f82017-10-27 13:07:59 +05305652 publish_getvar_partition_info(part_info, partition_get_partition_count());
Amol Jadi5edf3552013-07-23 14:15:34 -07005653
Mayank Grover351a75e2017-05-30 20:06:08 +05305654 if (partition_multislot_is_supported())
5655 publish_getvar_multislot_vars();
5656
Amol Jadi5edf3552013-07-23 14:15:34 -07005657 /* Max download size supported */
lijuang2fbed912019-07-12 14:15:05 +08005658#if !VERIFIED_BOOT_2
Ameya Thakur11cf1a62013-08-05 12:44:48 -07005659 snprintf(max_download_size, MAX_RSP_SIZE, "\t0x%x",
5660 target_get_max_flash_size());
lijuang2fbed912019-07-12 14:15:05 +08005661#else
5662 snprintf(max_download_size, MAX_RSP_SIZE, "\t0x%x",
5663 SUB_SALT_BUFF_OFFSET(target_get_max_flash_size()));
5664#endif
5665
Amol Jadi5edf3552013-07-23 14:15:34 -07005666 fastboot_publish("max-download-size", (const char *) max_download_size);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07005667 /* Is the charger screen check enabled */
5668 snprintf(charger_screen_enabled, MAX_RSP_SIZE, "%d",
5669 device.charger_screen_enabled);
5670 fastboot_publish("charger-screen-enabled",
5671 (const char *) charger_screen_enabled);
tracychuid184b912020-06-05 17:31:38 +08005672/*[20200605][TracyChui] Implement get Serial Number start*/
5673#if defined(ENABLE_PRODINFO_ACCESS)
5674 read_prod_info(&prod);
5675 snprintf(AdbEnable, MAX_RSP_SIZE, "%d",
5676 prod.is_adb_enabled);
5677 fastboot_publish("adb-enabled",
5678 (const char *) AdbEnable);
5679#endif
5680/*[20200605][TracyChui] Implement get Serial Number end*/
lijuanga25d8bb2015-09-16 13:11:52 +08005681 fastboot_publish("off-mode-charge", (const char *) charger_screen_enabled);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05305682 snprintf(panel_display_mode, MAX_RSP_SIZE, "%s",
5683 device.display_panel);
5684 fastboot_publish("display-panel",
5685 (const char *) panel_display_mode);
Mukesh Ojhabb6e7f32018-03-02 15:04:06 +05305686
5687 if (target_is_emmc_boot())
5688 {
5689 mmc_blocksize = mmc_get_device_blocksize();
5690 }
5691 else
5692 {
5693 mmc_blocksize = flash_block_size();
5694 }
5695 snprintf(block_size_string, MAX_RSP_SIZE, "0x%x", mmc_blocksize);
5696 fastboot_publish("erase-block-size", (const char *) block_size_string);
5697 fastboot_publish("logical-block-size", (const char *) block_size_string);
Mayank Grover7b8a8f82017-10-27 13:07:59 +05305698#if PRODUCT_IOT
5699 get_bootloader_version_iot(&bootloader_version_string);
5700 fastboot_publish("version-bootloader", (const char *) bootloader_version_string);
5701
5702 /* Version baseband is n/a for apq iot devices */
5703 fastboot_publish("version-baseband", "N/A");
Mayank Grover7b8a8f82017-10-27 13:07:59 +05305704#else
Channagoud Kadabiaafbbfc2015-03-04 17:52:56 -08005705 fastboot_publish("version-bootloader", (const char *) device.bootloader_version);
5706 fastboot_publish("version-baseband", (const char *) device.radio_version);
Mayank Grover7b8a8f82017-10-27 13:07:59 +05305707#endif
lijuangf16461c2015-08-03 17:09:34 +08005708 fastboot_publish("secure", is_secure_boot_enable()? "yes":"no");
Mayank Grover7b8a8f82017-10-27 13:07:59 +05305709 fastboot_publish("unlocked", device.is_unlocked ? "yes":"no");
lijuangf16461c2015-08-03 17:09:34 +08005710 smem_get_hw_platform_name((unsigned char *) hw_platform_buf, sizeof(hw_platform_buf));
5711 snprintf(get_variant, MAX_RSP_SIZE, "%s %s", hw_platform_buf,
5712 target_is_emmc_boot()? "eMMC":"UFS");
5713 fastboot_publish("variant", (const char *) get_variant);
lijuang65c5a822015-08-29 16:35:36 +08005714#if CHECK_BAT_VOLTAGE
lijuang102dfa92015-10-09 18:31:03 +08005715 update_battery_status();
lijuang65c5a822015-08-29 16:35:36 +08005716 fastboot_publish("battery-voltage", (const char *) battery_voltage);
lijuang102dfa92015-10-09 18:31:03 +08005717 fastboot_publish("battery-soc-ok", (const char *) battery_soc_ok);
lijuang65c5a822015-08-29 16:35:36 +08005718#endif
Mayank Grover98c4c742019-04-25 17:21:37 +05305719 if (target_dynamic_partition_supported())
5720 fastboot_publish("is-userspace", "no");
Amol Jadi5edf3552013-07-23 14:15:34 -07005721}
5722
Brian Swetland9c4c0752009-01-25 16:23:50 -08005723void aboot_init(const struct app_descriptor *app)
5724{
Shashank Mittal4f99a882010-02-01 13:58:50 -08005725 unsigned reboot_mode = 0;
Mayank Grover351a75e2017-05-30 20:06:08 +05305726 int boot_err_type = 0;
5727 int boot_slot = INVALID;
jessicatseng67c4fd02020-05-26 13:17:56 +08005728//<2020/05/26-JessicaTseng, Add low battery icon
5729 int vbat = 0;
5730 char boot_vbat[MAX_RSP_SIZE];
5731//>2020/05/26-JessicaTseng
Chandan Uddarajubedca152010-06-02 23:05:15 -07005732
lijuang39831732016-01-08 17:49:02 +08005733 /* Initialise wdog to catch early lk crashes */
5734#if WDOG_SUPPORT
5735 msm_wdog_init();
5736#endif
5737
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07005738 /* Setup page size information for nv storage */
Shashank Mittald8c42bf2010-06-09 15:44:28 -07005739 if (target_is_emmc_boot())
5740 {
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07005741 page_size = mmc_page_size();
Shashank Mittald8c42bf2010-06-09 15:44:28 -07005742 page_mask = page_size - 1;
Gaurav Nebhwani4d2389c2016-03-17 21:10:05 +05305743 mmc_blocksize = mmc_get_device_blocksize();
5744 mmc_blocksize_mask = mmc_blocksize - 1;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07005745 }
5746 else
5747 {
5748 page_size = flash_page_size();
5749 page_mask = page_size - 1;
5750 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07005751 ASSERT((MEMBASE + MEMSIZE) > MEMBASE);
5752
Ameya Thakur11cf1a62013-08-05 12:44:48 -07005753 read_device_info(&device);
tracychuid184b912020-06-05 17:31:38 +08005754/*[20200605][TracyChui] Implement get Serial Number start*/
5755#if defined(ENABLE_PRODINFO_ACCESS)
5756 read_prod_info(&prod);
5757#endif
5758/*[20200605][TracyChui] Implement get Serial Number end*/
vijay kumarc65876c2015-04-24 13:29:16 +05305759 read_allow_oem_unlock(&device);
Shashank Mittal162244e2011-08-08 19:01:25 -07005760
Mayank Grover351a75e2017-05-30 20:06:08 +05305761 /* Detect multi-slot support */
5762 if (partition_multislot_is_supported())
5763 {
5764 boot_slot = partition_find_active_slot();
5765 if (boot_slot == INVALID)
5766 {
5767 boot_into_fastboot = true;
5768 dprintf(INFO, "Active Slot: (INVALID)\n");
5769 }
5770 else
5771 {
5772 /* Setting the state of system to boot active slot */
5773 partition_mark_active_slot(boot_slot);
5774 dprintf(INFO, "Active Slot: (%s)\n", SUFFIX_SLOT(boot_slot));
5775 }
5776 }
5777
Greg Griscod6250552011-06-29 14:40:23 -07005778 target_serialno((unsigned char *) sn_buf);
tracychuid184b912020-06-05 17:31:38 +08005779/*[20200605][TracyChui] Implement get Serial Number start*/
5780#if defined(ENABLE_PRODINFO_ACCESS)
5781 dprintf(CRITICAL,"serial number: %s\n",sn_buf);
5782#else
Ajay Dudanib06c05f2011-05-12 14:46:10 -07005783 dprintf(SPEW,"serial number: %s\n",sn_buf);
tracychuid184b912020-06-05 17:31:38 +08005784#endif
5785/*[20200605][TracyChui] Implement get Serial Number end*/
5786/*[20200605][TracyChui] Implement get Serial Number start */
5787#if defined(ENABLE_PRODINFO_ACCESS)
5788 read_prod_info(&prod);
5789 snprintf((char *)cust_sn_buf, PRODINFO_MAX_SSN_LEN + 1, "%s", prod.ssn);
5790 dprintf(CRITICAL,"customer serial number: %s\n", cust_sn_buf);
5791 snprintf((char *)factory_sn_buf, PRODINFO_MAX_ISN_LEN + 1, "%s", prod.isn);
5792 dprintf(CRITICAL,"factory serial number: %s\n", factory_sn_buf);
5793#endif
5794 /*[20200605][TracyChui] Implement get Serial Number end */
Dhaval Patel223ec952013-07-18 14:49:44 -07005795 memset(display_panel_buf, '\0', MAX_PANEL_BUF_SIZE);
5796
Matthew Qindefd5562014-07-11 18:02:40 +08005797 /*
5798 * Check power off reason if user force reset,
5799 * if yes phone will do normal boot.
5800 */
5801 if (is_user_force_reset())
5802 goto normal_boot;
5803
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07005804 /* Check if we should do something other than booting up */
Ameya Thakur0b9c2442013-05-10 13:22:56 -07005805 if (keys_get_state(KEY_VOLUMEUP) && keys_get_state(KEY_VOLUMEDOWN))
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07005806 {
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03005807 dprintf(ALWAYS,"dload mode key sequence detected\n");
lijuang395b5e62015-11-19 17:39:44 +08005808 reboot_device(EMERGENCY_DLOAD);
5809 dprintf(CRITICAL,"Failed to reboot into dload mode\n");
5810
Ameya Thakur0b9c2442013-05-10 13:22:56 -07005811 boot_into_fastboot = true;
5812 }
5813 if (!boot_into_fastboot)
5814 {
5815 if (keys_get_state(KEY_HOME) || keys_get_state(KEY_VOLUMEUP))
5816 boot_into_recovery = 1;
5817 if (!boot_into_recovery &&
5818 (keys_get_state(KEY_BACK) || keys_get_state(KEY_VOLUMEDOWN)))
Pavel Nedev5d91d412013-04-29 11:34:24 +03005819 boot_into_fastboot = true;
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07005820 }
Shashank Mittald8c42bf2010-06-09 15:44:28 -07005821 #if NO_KEYPAD_DRIVER
Kinson Chik0b1c8162011-08-31 16:31:57 -07005822 if (fastboot_trigger())
Pavel Nedev5d91d412013-04-29 11:34:24 +03005823 boot_into_fastboot = true;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07005824 #endif
Chandan Uddarajubedca152010-06-02 23:05:15 -07005825
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07005826#if USE_PON_REBOOT_REG
5827 reboot_mode = check_hard_reboot_mode();
5828#else
Ajay Dudani77421292010-10-27 19:34:06 -07005829 reboot_mode = check_reboot_mode();
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07005830#endif
5831 if (reboot_mode == RECOVERY_MODE)
5832 {
Ajay Dudani77421292010-10-27 19:34:06 -07005833 boot_into_recovery = 1;
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07005834 }
5835 else if(reboot_mode == FASTBOOT_MODE)
5836 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03005837 boot_into_fastboot = true;
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07005838 }
5839 else if(reboot_mode == ALARM_BOOT)
5840 {
Matthew Qind886f3c2014-01-17 16:52:01 +08005841 boot_reason_alarm = true;
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07005842 }
Monika Singhb0db4b82018-09-26 12:18:02 +05305843#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover912eaa62017-10-26 12:08:53 +05305844 else if (VB_M <= target_get_vb_version())
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07005845 {
Mayank Grover889be1b2017-09-12 20:12:23 +05305846 if (reboot_mode == DM_VERITY_ENFORCING)
5847 {
5848 device.verity_mode = 1;
5849 write_device_info(&device);
5850 }
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07005851#if ENABLE_VB_ATTEST
Mayank Grover889be1b2017-09-12 20:12:23 +05305852 else if (reboot_mode == DM_VERITY_EIO)
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07005853#else
Mayank Grover889be1b2017-09-12 20:12:23 +05305854 else if (reboot_mode == DM_VERITY_LOGGING)
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07005855#endif
Mayank Grover889be1b2017-09-12 20:12:23 +05305856 {
5857 device.verity_mode = 0;
5858 write_device_info(&device);
5859 }
5860 else if (reboot_mode == DM_VERITY_KEYSCLEAR)
5861 {
5862 if(send_delete_keys_to_tz())
5863 ASSERT(0);
5864 }
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07005865 }
Parth Dixitddbc7352015-10-18 03:13:31 +05305866#endif
Ajay Dudani77421292010-10-27 19:34:06 -07005867
jialongjhan1efba002020-05-22 18:25:56 +08005868//[Arima][8901][JialongJhan][20200522]modify Display splash screen if enabled Start
5869 /* Display splash screen if enabled */
5870#if DISPLAY_SPLASH_SCREEN
5871#if NO_ALARM_DISPLAY
5872 if (!check_alarm_boot()) {
5873#endif
5874 dprintf(SPEW, "Display Init: Start\n");
5875#if DISPLAY_HDMI_PRIMARY
5876 if (!strlen(device.display_panel))
5877 strlcpy(device.display_panel, DISPLAY_PANEL_HDMI,
5878 sizeof(device.display_panel));
5879#endif
5880#if ENABLE_WBC
5881 /* Wait if the display shutdown is in progress */
5882 while(pm_app_display_shutdown_in_prgs());
5883 if (!pm_appsbl_display_init_done())
5884 target_display_init(device.display_panel);
5885 else
5886 display_image_on_screen();
5887#else
5888 target_display_init(device.display_panel);
5889#endif
5890 dprintf(SPEW, "Display Init: Done\n");
5891#if NO_ALARM_DISPLAY
5892 }
5893#endif
5894#endif
5895
5896//[Arima][8901][JialongJhan][20200522]modify Display splash screen if enabled End
5897
Matthew Qindefd5562014-07-11 18:02:40 +08005898normal_boot:
Pavel Nedev5d91d412013-04-29 11:34:24 +03005899 if (!boot_into_fastboot)
Shashank Mittald8c42bf2010-06-09 15:44:28 -07005900 {
jessicatseng67c4fd02020-05-26 13:17:56 +08005901//<2020/05/26-JessicaTseng, Add low battery icon
5902 if(!target_pause_for_battery_charge())
5903 {
5904 vbat = target_get_battery_voltage();
5905 snprintf(boot_vbat, MAX_RSP_SIZE, "%d", vbat);
5906 dprintf(CRITICAL,"battery_voltage: %s\n", boot_vbat);
5907 if(vbat < 3500000)
5908 {
5909 display_lowbattery_image_on_screen();
5910 //[Arima][8901][JialongJhan] Command mode reflash screen when low battery logo shown 20190516 Start
5911 msm_display_flush();
5912 //[Arima][8901][JialongJhan] Command mode reflash screen when low battery logo shown 20190516 End
5913 dprintf(CRITICAL,"Low battery, cannot boot up...\n");
5914 mdelay(3000);
5915 shutdown_device();
5916 }
5917 }
5918//>2020/05/26-JessicaTseng
5919
Pavel Nedev5d91d412013-04-29 11:34:24 +03005920 if (target_is_emmc_boot())
Shashank Mittala0032282011-08-26 14:50:11 -07005921 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03005922 if(emmc_recovery_init())
5923 dprintf(ALWAYS,"error in emmc_recovery_init\n");
5924 if(target_use_signed_kernel())
Shashank Mittala0032282011-08-26 14:50:11 -07005925 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03005926 if((device.is_unlocked) || (device.is_tampered))
5927 {
5928 #ifdef TZ_TAMPER_FUSE
Monika Singh98257462018-06-06 11:28:49 +05305929 set_tamper_fuse_cmd(HLOS_IMG_TAMPER_FUSE);
Pavel Nedev5d91d412013-04-29 11:34:24 +03005930 #endif
5931 #if USE_PCOM_SECBOOT
5932 set_tamper_flag(device.is_tampered);
5933 #endif
5934 }
Shashank Mittala0032282011-08-26 14:50:11 -07005935 }
Amit Blay6281ebc2015-01-11 14:44:08 +02005936
Mayank Grover351a75e2017-05-30 20:06:08 +05305937retry_boot:
5938 /* Trying to boot active partition */
5939 if (partition_multislot_is_supported())
5940 {
5941 boot_slot = partition_find_boot_slot();
5942 partition_mark_active_slot(boot_slot);
5943 if (boot_slot == INVALID)
5944 goto fastboot;
5945 }
5946
5947 boot_err_type = boot_linux_from_mmc();
5948 switch (boot_err_type)
5949 {
5950 case ERR_INVALID_PAGE_SIZE:
5951 case ERR_DT_PARSE:
5952 case ERR_ABOOT_ADDR_OVERLAP:
Mayank Grover10cabbe2017-10-30 19:11:05 +05305953 case ERR_INVALID_BOOT_MAGIC:
Mayank Grover351a75e2017-05-30 20:06:08 +05305954 if(partition_multislot_is_supported())
Mayank Grover10cabbe2017-10-30 19:11:05 +05305955 {
5956 /*
5957 * Deactivate current slot, as it failed to
5958 * boot, and retry next slot.
5959 */
5960 partition_deactivate_slot(boot_slot);
Mayank Grover351a75e2017-05-30 20:06:08 +05305961 goto retry_boot;
Mayank Grover10cabbe2017-10-30 19:11:05 +05305962 }
Mayank Grover351a75e2017-05-30 20:06:08 +05305963 else
5964 break;
Mayank Grover351a75e2017-05-30 20:06:08 +05305965 default:
5966 break;
5967 /* going to fastboot menu */
5968 }
Shashank Mittala0032282011-08-26 14:50:11 -07005969 }
Pavel Nedev5d91d412013-04-29 11:34:24 +03005970 else
5971 {
5972 recovery_init();
5973 #if USE_PCOM_SECBOOT
5974 if((device.is_unlocked) || (device.is_tampered))
5975 set_tamper_flag(device.is_tampered);
5976 #endif
5977 boot_linux_from_flash();
5978 }
5979 dprintf(CRITICAL, "ERROR: Could not do normal boot. Reverting "
5980 "to fastboot mode.\n");
Shashank Mittald8c42bf2010-06-09 15:44:28 -07005981 }
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07005982
Mayank Grover351a75e2017-05-30 20:06:08 +05305983fastboot:
jialongjhan1efba002020-05-22 18:25:56 +08005984
5985 //[Arima][8901][JialongJhan]Show Fastboot logo 1 second 20190627 Start
5986 mdelay(1000);
5987 //[Arima][8901][JialongJhan]Show Fastboot logo 1 second 20190627 End
5988
Amol Jadi5edf3552013-07-23 14:15:34 -07005989 /* We are here means regular boot did not happen. Start fastboot. */
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07005990
Amol Jadi5edf3552013-07-23 14:15:34 -07005991 /* register aboot specific fastboot commands */
5992 aboot_fastboot_register_commands();
Amol Jadi57abe4c2011-05-24 15:47:27 -07005993
Amol Jadi5edf3552013-07-23 14:15:34 -07005994 /* dump partition table for debug info */
Kinson Chikf1a43512011-07-14 11:28:39 -07005995 partition_dump();
Amol Jadi5edf3552013-07-23 14:15:34 -07005996
5997 /* initialize and start fastboot */
Saranya Chidura41b34532018-10-16 12:29:52 +05305998#if !VERIFIED_BOOT_2
Amol Jadi5edf3552013-07-23 14:15:34 -07005999 fastboot_init(target_get_scratch_address(), target_get_max_flash_size());
Saranya Chidura41b34532018-10-16 12:29:52 +05306000#else
6001 /* Add salt buffer offset at start of image address to copy VB salt */
6002 fastboot_init(ADD_SALT_BUFF_OFFSET(target_get_scratch_address()),
6003 SUB_SALT_BUFF_OFFSET(target_get_max_flash_size()));
6004#endif
lijuang4ece1e72015-08-14 21:02:36 +08006005#if FBCON_DISPLAY_MSG
lijuang9a7d3b92015-11-30 14:41:24 +08006006 display_fastboot_menu();
lijuang4ece1e72015-08-14 21:02:36 +08006007#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08006008}
6009
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07006010uint32_t get_page_size()
6011{
6012 return page_size;
6013}
6014
Amir Samuelov57a6fa22013-06-05 16:36:43 +03006015/*
6016 * Calculated and save hash (SHA256) for non-signed boot image.
6017 *
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07006018 * @param image_addr - Boot image address
6019 * @param image_size - Size of the boot image
Amir Samuelov57a6fa22013-06-05 16:36:43 +03006020 *
6021 * @return int - 0 on success, negative value on failure.
6022 */
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07006023static int aboot_save_boot_hash_mmc(uint32_t image_addr, uint32_t image_size)
Amir Samuelov57a6fa22013-06-05 16:36:43 +03006024{
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07006025 unsigned int digest[8];
6026#if IMAGE_VERIF_ALGO_SHA1
6027 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA1;
6028#else
6029 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA256;
6030#endif
Amir Samuelov57a6fa22013-06-05 16:36:43 +03006031
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07006032 target_crypto_init_params();
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -08006033 hash_find((unsigned char *) image_addr, image_size, (unsigned char *)&digest, auth_algo);
Amir Samuelov57a6fa22013-06-05 16:36:43 +03006034
6035 save_kernel_hash_cmd(digest);
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07006036 dprintf(INFO, "aboot_save_boot_hash_mmc: imagesize_actual size %d bytes.\n", (int) image_size);
Amir Samuelov57a6fa22013-06-05 16:36:43 +03006037
6038 return 0;
6039}
6040
Mayank Grover351a75e2017-05-30 20:06:08 +05306041
Brian Swetland9c4c0752009-01-25 16:23:50 -08006042APP_START(aboot)
6043 .init = aboot_init,
6044APP_END