blob: b546e2e7568f32eabffb5772030cd417f9e2b10d [file] [log] [blame]
Brian Swetland9c4c0752009-01-25 16:23:50 -08001/*
2 * Copyright (c) 2009, Google Inc.
3 * All rights reserved.
4 *
Rahul Shaharee7baede2021-02-02 13:23:57 +05305 * Copyright (c) 2009-2021, The Linux Foundation. All rights reserved.
Brian Swetland9c4c0752009-01-25 16:23:50 -08006 *
Chandan Uddaraju5fa471a2009-12-02 17:31:34 -08007 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070014 * * Neither the name of The Linux Foundation nor
Chandan Uddaraju5fa471a2009-12-02 17:31:34 -080015 * the names of its contributors may be used to endorse or promote
16 * products derived from this software without specific prior written
17 * permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22 * NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 *
Brian Swetland9c4c0752009-01-25 16:23:50 -080031 */
32
33#include <app.h>
34#include <debug.h>
35#include <arch/arm.h>
Brian Swetland9c4c0752009-01-25 16:23:50 -080036#include <string.h>
Channagoud Kadabi132ff552013-04-19 14:34:44 -070037#include <stdlib.h>
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -070038#include <limits.h>
Brian Swetland9c4c0752009-01-25 16:23:50 -080039#include <kernel/thread.h>
40#include <arch/ops.h>
41
Dima Zavin214cc642009-01-26 11:16:21 -080042#include <dev/flash.h>
Tanya Brokhman1c94f1a2015-02-15 09:05:03 +020043#include <dev/flash-ubi.h>
Dima Zavin214cc642009-01-26 11:16:21 -080044#include <lib/ptable.h>
Dima Zavinb4283602009-01-26 16:36:57 -080045#include <dev/keys.h>
Shashank Mittal4f99a882010-02-01 13:58:50 -080046#include <dev/fbcon.h>
Ajay Dudanid04110c2011-01-17 23:55:07 -080047#include <baseband.h>
Greg Griscod6250552011-06-29 14:40:23 -070048#include <target.h>
49#include <mmc.h>
Kinson Chikf1a43512011-07-14 11:28:39 -070050#include <partition_parser.h>
Mayank Grover351a75e2017-05-30 20:06:08 +053051#include <ab_partition_parser.h>
Monika Singh292b3e92018-03-17 22:40:23 +053052#include <verifiedboot.h>
Greg Griscod6250552011-06-29 14:40:23 -070053#include <platform.h>
Shashank Mittalcd98d472011-08-02 14:29:24 -070054#include <crypto_hash.h>
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070055#include <malloc.h>
Amol Jadi492d5a52013-03-15 16:12:34 -070056#include <boot_stats.h>
Amir Samuelov57a6fa22013-06-05 16:36:43 +030057#include <sha.h>
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -070058#include <platform/iomap.h>
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -070059#include <boot_device.h>
Shashank Mittald3e54dd2014-08-28 15:24:02 -070060#include <boot_verifier.h>
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +053061#include <image_verify.h>
Matthew Qinbb7923d2015-02-09 10:56:09 +080062#include <decompress.h>
Unnati Gandhi17b3bfc2015-05-11 12:58:16 +053063#include <platform/timer.h>
lijuang511a2b52015-08-14 20:50:51 +080064#include <sys/types.h>
Channagoud Kadabi036c6052015-02-09 15:19:59 -080065#if USE_RPMB_FOR_DEVINFO
66#include <rpmb.h>
67#endif
Dima Zavin214cc642009-01-26 11:16:21 -080068
Channagoud Kadabi90869ce2015-04-27 11:15:14 -070069#if ENABLE_WBC
Umang Chheda4c140de2019-12-19 14:30:38 +053070#include <pm_smbchg_common.h>
Channagoud Kadabi90869ce2015-04-27 11:15:14 -070071#endif
72
Neeti Desai17379b82012-06-04 18:42:53 -070073#if DEVICE_TREE
74#include <libfdt.h>
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070075#include <dev_tree.h>
Neeti Desai17379b82012-06-04 18:42:53 -070076#endif
77
Aparna Mallavarapu118ccae2015-06-03 13:47:11 +053078#if WDOG_SUPPORT
79#include <wdog.h>
80#endif
81
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -070082#include <reboot.h>
Shashank Mittalcd98d472011-08-02 14:29:24 -070083#include "image_verify.h"
Shashank Mittal024c0332010-02-03 11:44:00 -080084#include "recovery.h"
Brian Swetland9c4c0752009-01-25 16:23:50 -080085#include "bootimg.h"
86#include "fastboot.h"
Ajay Dudani5c761132011-04-07 20:19:04 -070087#include "sparse_format.h"
Ajay Dudanide984792015-03-02 09:57:41 -080088#include "meta_format.h"
Greg Grisco6e754772011-06-23 12:19:39 -070089#include "mmc.h"
Shashank Mittal162244e2011-08-08 19:01:25 -070090#include "devinfo.h"
Neeti Desai465491e2012-07-31 12:53:35 -070091#include "board.h"
Shashank Mittal162244e2011-08-08 19:01:25 -070092#include "scm.h"
Amit Blay6281ebc2015-01-11 14:44:08 +020093#include "mdtp.h"
Sridhar Parasuram32b30662015-07-10 13:33:22 -070094#include "secapp_loader.h"
lijuanga40d6302015-07-20 20:10:13 +080095#include <menu_keys_detect.h>
96#include <display_menu.h>
Channagoud Kadabi736c4962015-08-21 11:56:52 -070097#include "fastboot_test.h"
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -070098
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070099extern bool target_use_signed_kernel(void);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700100extern void platform_uninit(void);
Channagoud Kadabi33defe22013-06-18 18:35:40 -0700101extern void target_uninit(void);
Joonwoo Park61112782013-10-02 19:50:39 -0700102extern int get_target_boot_params(const char *cmdline, const char *part,
vijay kumar870515d2015-08-31 16:37:24 +0530103 char **buf);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700104
Sridhar Parasuram7e16d172015-07-05 11:35:23 -0700105void *info_buf;
tracychui9d60fc52020-06-05 17:31:38 +0800106#if defined(ENABLE_PRODINFO_ACCESS)
107void *prodinfo_buf;
108#endif
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700109void write_device_info_mmc(device_info *dev);
110void write_device_info_flash(device_info *dev);
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -0700111static int aboot_save_boot_hash_mmc(uint32_t image_addr, uint32_t image_size);
Channagoud Kadabiad259832015-05-29 11:14:17 -0700112static int aboot_frp_unlock(char *pname, void *data, unsigned sz);
Kishor PK38ed93d2017-04-25 14:19:26 +0530113static inline uint64_t validate_partition_size();
Parth Dixit54ac3bb2017-03-07 15:52:48 +0530114bool pwr_key_is_pressed = false;
Hareesh Gauthamabf1e1e2017-04-12 18:09:24 +0530115static bool is_systemd_present=false;
Mayank Grover351a75e2017-05-30 20:06:08 +0530116static void publish_getvar_multislot_vars();
Sridhar Parasurame94e8152014-10-24 14:06:03 -0700117/* fastboot command function pointer */
118typedef void (*fastboot_cmd_fn) (const char *, void *, unsigned);
Monika Singh292b3e92018-03-17 22:40:23 +0530119bool get_perm_attr_status();
tracychui9d60fc52020-06-05 17:31:38 +0800120#if defined(ENABLE_PRODINFO_ACCESS)
121void write_prod_info(prod_info *dev);
122#endif
Sridhar Parasurame94e8152014-10-24 14:06:03 -0700123
124struct fastboot_cmd_desc {
125 char * name;
126 fastboot_cmd_fn cb;
127};
128
Subbaraman Narayanamurthyeb92bcc2010-07-20 14:32:46 -0700129#define EXPAND(NAME) #NAME
130#define TARGET(NAME) EXPAND(NAME)
Brian Swetland2defe162009-08-18 14:35:59 -0700131
Ajay Singh Parmara7865a82015-04-16 21:27:52 -0700132#define DISPLAY_PANEL_HDMI "hdmi"
133
Ajay Dudanicd01f9b2010-02-23 21:13:04 -0800134#ifdef MEMBASE
135#define EMMC_BOOT_IMG_HEADER_ADDR (0xFF000+(MEMBASE))
136#else
David Ng183a7422009-12-07 14:55:21 -0800137#define EMMC_BOOT_IMG_HEADER_ADDR 0xFF000
Ajay Dudanicd01f9b2010-02-23 21:13:04 -0800138#endif
139
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700140#ifndef MEMSIZE
141#define MEMSIZE 1024*1024
142#endif
143
144#define MAX_TAGS_SIZE 1024
Kishor PKee5c0a32018-03-06 16:49:46 +0530145#define PLL_CODES_OFFSET 4096
Kun Liang2f1601a2013-08-12 16:29:54 +0800146/* make 4096 as default size to ensure EFS,EXT4's erasing */
147#define DEFAULT_ERASE_SIZE 4096
Ujwal Patelc0b0a252015-08-16 14:05:35 -0700148#define MAX_PANEL_BUF_SIZE 196
Vijay Kumar Pendoti0b21f462016-05-02 17:09:18 +0530149#define FOOTER_SIZE 16384
Kun Liang2f1601a2013-08-12 16:29:54 +0800150
Dhaval Patelf83d73b2014-06-23 16:24:37 -0700151#define DISPLAY_DEFAULT_PREFIX "mdss_mdp"
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700152#define BOOT_DEV_MAX_LEN 64
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -0800153
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800154#define IS_ARM64(ptr) (ptr->magic_64 == KERNEL64_HDR_MAGIC) ? true : false
155
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -0700156#define ADD_OF(a, b) (UINT_MAX - b > a) ? (a + b) : UINT_MAX
157
Ameya Thakur10a33452016-06-13 14:24:26 -0700158//Size of the header that is used in case the boot image has
159//a uncompressed kernel + appended dtb
160#define PATCHED_KERNEL_HEADER_SIZE 20
161
162//String used to determine if the boot image has
163//a uncompressed kernel + appended dtb
164#define PATCHED_KERNEL_MAGIC "UNCOMPRESSED_IMG"
165
Sridhar Parasuram7bd4aaf2015-02-12 11:14:38 -0800166#if USE_BOOTDEV_CMDLINE
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700167static const char *emmc_cmdline = " androidboot.bootdevice=";
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700168#else
David Ng183a7422009-12-07 14:55:21 -0800169static const char *emmc_cmdline = " androidboot.emmc=true";
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700170#endif
Mayank Groverb716edf2019-05-08 16:06:55 +0530171static const char *dynamic_bootdev_cmdline =
172 " androidboot.boot_devices=soc/";
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800173static const char *usb_sn_cmdline = " androidboot.serialno=";
Pavel Nedev328ac822013-04-05 15:25:11 +0300174static const char *androidboot_mode = " androidboot.mode=";
Hareesh Gauthamabf1e1e2017-04-12 18:09:24 +0530175
176static const char *systemd_ffbm_mode = " systemd.unit=ffbm.target";
Matthew Qind886f3c2014-01-17 16:52:01 +0800177static const char *alarmboot_cmdline = " androidboot.alarmboot=true";
Pavel Nedev898298c2013-02-27 12:36:09 -0800178static const char *loglevel = " quiet";
Ajay Dudanica3a33c2011-11-18 08:31:40 -0800179static const char *battchg_pause = " androidboot.mode=charger";
tracychui9d60fc52020-06-05 17:31:38 +0800180#if defined(ENABLE_PRODINFO_ACCESS)
181static const char *cust_sn_cmdline = " androidboot.customer_serialno=";
182static const char *factory_sn_cmdline = " androidboot.factory_serialno=";
183static const char *UsbAdbEnable = " androidboot.adb_enable=1";
184#endif
Shashank Mittalcd98d472011-08-02 14:29:24 -0700185static const char *auth_kernel = " androidboot.authorized_kernel=true";
Pavel Nedev5614d222013-06-17 18:01:02 +0300186static const char *secondary_gpt_enable = " gpt";
Monika Singh292b3e92018-03-17 22:40:23 +0530187#ifdef MDTP_SUPPORT
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200188static const char *mdtp_activated_flag = " mdtp";
Monika Singh292b3e92018-03-17 22:40:23 +0530189#endif
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800190static const char *baseband_apq = " androidboot.baseband=apq";
191static const char *baseband_msm = " androidboot.baseband=msm";
192static const char *baseband_csfb = " androidboot.baseband=csfb";
193static const char *baseband_svlte2a = " androidboot.baseband=svlte2a";
Ajay Dudani403bc492011-09-30 16:17:21 -0700194static const char *baseband_mdm = " androidboot.baseband=mdm";
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800195static const char *baseband_mdm2 = " androidboot.baseband=mdm2";
Amol Jadi5c61a952012-05-04 17:05:35 -0700196static const char *baseband_sglte = " androidboot.baseband=sglte";
Amol Jadi2a15a272013-01-22 12:03:36 -0800197static const char *baseband_dsda = " androidboot.baseband=dsda";
198static const char *baseband_dsda2 = " androidboot.baseband=dsda2";
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800199static const char *baseband_sglte2 = " androidboot.baseband=sglte2";
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800200static const char *warmboot_cmdline = " qpnp-power-on.warm_boot=1";
Vijay Kumar Pendotib228cfc2016-06-13 20:15:23 +0530201static const char *baseband_apq_nowgr = " androidboot.baseband=baseband_apq_nowgr";
Mayank Grover351a75e2017-05-30 20:06:08 +0530202static const char *androidboot_slot_suffix = " androidboot.slot_suffix=";
203static const char *skip_ramfs = " skip_initramfs";
Mayank Grover466d6562018-05-10 14:52:20 +0530204
205#if HIBERNATION_SUPPORT
206static const char *resume = " resume=/dev/mmcblk0p";
207#endif
208
Sourabh Banerjee6c7153c2018-03-20 01:21:57 +0530209#ifdef INIT_BIN_LE
210static const char *sys_path_cmdline = " rootwait ro init="INIT_BIN_LE;
211#else
Mayank Grover3804be72017-06-22 11:59:23 +0530212static const char *sys_path_cmdline = " rootwait ro init=/init";
Sourabh Banerjee6c7153c2018-03-20 01:21:57 +0530213#endif
Sourabh Banerjee386b1322018-02-27 09:37:28 +0530214
215#if VERITY_LE
216static const char *verity_dev = " root=/dev/dm-0";
217static const char *verity_system_part = " dm=\"system";
218static const char *verity_params = " none ro,0 1 android-verity /dev/mmcblk0p";
219#else
Mayank Grover5384ed82018-05-09 12:09:24 +0530220static const char *sys_path = " root=/dev/mmcblk0p";
lijuang83ef4b22018-08-23 11:01:55 +0800221
222#define MAX_DTBO_IDX_STR 64
223static const char *android_boot_dtbo_idx = " androidboot.dtbo_idx=";
Parth Dixit1375d9e2019-07-08 20:56:13 +0530224
225#define MAX_DTB_IDX_STR MAX_DTBO_IDX_STR
226static const char *android_boot_dtb_idx = " androidboot.dtb_idx=";
Sourabh Banerjee386b1322018-02-27 09:37:28 +0530227#endif
Ajay Dudanid04110c2011-01-17 23:55:07 -0800228
michaellin6be1f392020-04-24 15:05:10 +0800229#include <platform/gpio.h>
230static const char *PCBA_STAGE_0 = " androidboot.pcbastage=EP0";
231static const char *PCBA_STAGE_1 = " androidboot.pcbastage=EP1";
232static const char *PCBA_STAGE_2 = " androidboot.pcbastage=EP2";
233static const char *PCBA_STAGE_3 = " androidboot.pcbastage=FP";
234static const char *PCBA_STAGE_4 = " androidboot.pcbastage=MP";
michaellinaf8857a2020-05-05 14:58:47 +0800235static const char *PCBA_STAGE_5 = " androidboot.pcbastage=MP-8903MB_001";
michaellin6be1f392020-04-24 15:05:10 +0800236static const char *PCBA_STAGE_F = " androidboot.pcbastage=Reserved";
237
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700238#if VERIFIED_BOOT
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700239static const char *verity_mode = " androidboot.veritymode=";
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700240static const char *verified_state= " androidboot.verifiedbootstate=";
Parth Dixita5715a02015-10-29 12:25:10 +0530241static const char *keymaster_v1= " androidboot.keymaster=1";
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700242//indexed based on enum values, green is 0 by default
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700243
244struct verified_boot_verity_mode vbvm[] =
245{
Channagoud Kadabi86b0c112016-03-16 19:23:16 -0700246#if ENABLE_VB_ATTEST
247 {false, "eio"},
248#else
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700249 {false, "logging"},
Channagoud Kadabi86b0c112016-03-16 19:23:16 -0700250#endif
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700251 {true, "enforcing"},
252};
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700253struct verified_boot_state_name vbsn[] =
254{
255 {GREEN, "green"},
256 {ORANGE, "orange"},
257 {YELLOW,"yellow"},
258 {RED,"red" },
259};
260#endif
Channagoud Kadabi86b0c112016-03-16 19:23:16 -0700261/*As per spec delay wait time before shutdown in Red state*/
262#define DELAY_WAIT 30000
Ashish Bhimanpalliwar8cb34fd2020-11-12 19:26:41 +0530263static unsigned page_size = BOARD_KERNEL_PAGESIZE;
264
265uint32_t kernel_hdr_page_size()
266{
267 return page_size;
268}
269
270
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700271static unsigned page_mask = 0;
Gaurav Nebhwani4d2389c2016-03-17 21:10:05 +0530272static unsigned mmc_blocksize = 0;
273static unsigned mmc_blocksize_mask = 0;
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700274static char ffbm_mode_string[FFBM_MODE_BUF_SIZE];
275static bool boot_into_ffbm;
vijay kumar870515d2015-08-31 16:37:24 +0530276static char *target_boot_params = NULL;
Matthew Qind886f3c2014-01-17 16:52:01 +0800277static bool boot_reason_alarm;
Channagoud Kadabi80a182b2015-03-11 17:04:23 -0700278static bool devinfo_present = true;
tracychui9d60fc52020-06-05 17:31:38 +0800279#if defined(ENABLE_PRODINFO_ACCESS)
280static bool prodinfo_present = true;
281#endif
Channagoud Kadabi736c4962015-08-21 11:56:52 -0700282bool boot_into_fastboot = false;
Parth Dixit4097b622016-03-15 14:42:27 +0530283static uint32_t dt_size = 0;
Monika Singh292b3e92018-03-17 22:40:23 +0530284static char *vbcmdline;
285static bootinfo info = {0};
Mayank Grovere1ab96c2018-09-04 20:31:31 +0530286static void *recovery_dtbo_buf = NULL;
287static uint32_t recovery_dtbo_size = 0;
288
Shashank Mittalcd98d472011-08-02 14:29:24 -0700289/* Assuming unauthorized kernel image by default */
290static int auth_kernel_img = 0;
Monika Singh292b3e92018-03-17 22:40:23 +0530291static device_info device = {DEVICE_MAGIC,0,0,0,0,{0},{0},{0},1,{0},0,{0}};
292
tracychui9d60fc52020-06-05 17:31:38 +0800293#if defined(ENABLE_PRODINFO_ACCESS)
294static prod_info prod = {PRODINFO_MAGIC, {0}, {0}, 0};
295#endif
296
jhchena8a15dd2020-04-24 15:45:57 +0800297#ifdef ENABLE_FUSE_CHECK
298static const char *fuse_blown = " androidboot.oem.color=red";
299static const char *fuse_not_blown = " androidboot.oem.color=brown";
300#endif
301
302#ifdef ENABLE_FUSE_CHECK
303 int is_fused = 0;
304#endif
305
vijay kumarc65876c2015-04-24 13:29:16 +0530306static bool is_allow_unlock = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -0700307
vijay kumarca2e6812015-07-08 20:28:25 +0530308static char frp_ptns[2][8] = {"config","frp"};
309
lijuang511a2b52015-08-14 20:50:51 +0800310static const char *critical_flash_allowed_ptn[] = {
311 "aboot",
312 "rpm",
313 "tz",
314 "sbl",
315 "sdi",
316 "sbl1",
317 "xbl",
318 "hyp",
319 "pmic",
320 "bootloader",
321 "devinfo",
322 "partition"};
323
Rahul Shaharef7a5f8b2021-02-04 16:51:36 +0530324static const char *VirtualAbCriticalPartitions[] = {
325 "misc",
326 "metadata",
327 "userdata"};
328
329static bool CheckVirtualAbCriticalPartition (const char *PartitionName);
330
Rahul Shaharee7baede2021-02-02 13:23:57 +0530331static const char *VabSnapshotMergeStatus[] = {
332 "none",
333 "unknown",
334 "snapshotted",
335 "merging",
336 "cancelled"};
337
Dima Zavin42168f22009-01-30 11:52:22 -0800338struct atag_ptbl_entry
339{
340 char name[16];
341 unsigned offset;
342 unsigned size;
343 unsigned flags;
344};
345
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700346/*
347 * Partition info, required to be published
348 * for fastboot
349 */
350struct getvar_partition_info {
Mayank Grover7b8a8f82017-10-27 13:07:59 +0530351 char part_name[MAX_GPT_NAME_SIZE]; /* Partition name */
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700352 char getvar_size[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for size */
353 char getvar_type[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for type */
354 char size_response[MAX_RSP_SIZE]; /* fastboot response for size */
355 char type_response[MAX_RSP_SIZE]; /* fastboot response for type */
356};
357
358/*
Mayank Grover7b8a8f82017-10-27 13:07:59 +0530359 * Update the part_type_known for known paritions types.
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700360 */
Mayank Groverd38fe012018-03-13 15:33:16 +0530361#define RAW_STR "raw"
Mayank Grover52cd10a2018-03-15 12:57:54 +0530362#define EXT_STR "ext4"
363#define F2FS_STR "f2fs"
364
365#define FS_SUPERBLOCK_OFFSET 0x400
366#define EXT_MAGIC 0xEF53
367#define EXT_MAGIC_OFFSET_SB 0x38
368#define F2FS_MAGIC 0xF2F52010 // F2FS Magic Number
369#define F2FS_MAGIC_OFFSET_SB 0x0
370
371typedef enum fs_signature_type {
372 EXT_FS_SIGNATURE = 1,
373 EXT_F2FS_SIGNATURE = 2,
374 NO_FS = -1
375} fs_signature_type;
376
Mayank Grover7b8a8f82017-10-27 13:07:59 +0530377struct getvar_partition_info part_info[NUM_PARTITIONS];
378struct getvar_partition_info part_type_known[] =
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700379{
Mayank Grover49920212018-02-09 18:15:55 +0530380 { "system" , "partition-size:", "partition-type:", "", "ext4" },
381 { "userdata" , "partition-size:", "partition-type:", "", "ext4" },
382 { "cache" , "partition-size:", "partition-type:", "", "ext4" },
383 { "recoveryfs" , "partition-size:", "partition-type:", "", "ext4" },
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700384};
385
386char max_download_size[MAX_RSP_SIZE];
Ameya Thakur11cf1a62013-08-05 12:44:48 -0700387char charger_screen_enabled[MAX_RSP_SIZE];
tracychui9d60fc52020-06-05 17:31:38 +0800388#if defined(ENABLE_PRODINFO_ACCESS)
389char cust_sn_buf[PRODINFO_MAX_SSN_LEN + 1];
390char factory_sn_buf[PRODINFO_MAX_SSN_LEN + 1];
391char AdbEnable[MAX_RSP_SIZE];
392#endif
393#if defined(ENABLE_PRODINFO_ACCESS)
394char sn_buf[PRODINFO_MAX_ISN_LEN + 1];
395#else
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800396char sn_buf[13];
tracychui9d60fc52020-06-05 17:31:38 +0800397#endif
Dhaval Patel223ec952013-07-18 14:49:44 -0700398char display_panel_buf[MAX_PANEL_BUF_SIZE];
Unnati Gandhi62c8ab82014-01-24 11:01:01 +0530399char panel_display_mode[MAX_RSP_SIZE];
Mayank Grovera64dfbe2018-05-17 14:06:34 +0530400char soc_version_str[MAX_RSP_SIZE];
Mayank Grover7b8a8f82017-10-27 13:07:59 +0530401char block_size_string[MAX_RSP_SIZE];
Rahul Shaharee7baede2021-02-02 13:23:57 +0530402static char SnapshotMergeState[MAX_RSP_SIZE];
Mukesh Ojhabb6e7f32018-03-02 15:04:06 +0530403#if PRODUCT_IOT
Mayank Grover7b8a8f82017-10-27 13:07:59 +0530404
405/* For IOT we are using custom version */
406#define PRODUCT_IOT_VERSION "IOT001"
407char bootloader_version_string[MAX_RSP_SIZE];
408#endif
lijuang102dfa92015-10-09 18:31:03 +0800409
410#if CHECK_BAT_VOLTAGE
lijuang65c5a822015-08-29 16:35:36 +0800411char battery_voltage[MAX_RSP_SIZE];
lijuang102dfa92015-10-09 18:31:03 +0800412char battery_soc_ok [MAX_RSP_SIZE];
413#endif
414
lijuangf16461c2015-08-03 17:09:34 +0800415char get_variant[MAX_RSP_SIZE];
Greg Griscod6250552011-06-29 14:40:23 -0700416
Greg Griscod2471ef2011-07-14 13:00:42 -0700417extern int emmc_recovery_init(void);
418
Kinson Chik0b1c8162011-08-31 16:31:57 -0700419#if NO_KEYPAD_DRIVER
420extern int fastboot_trigger(void);
421#endif
Greg Griscod2471ef2011-07-14 13:00:42 -0700422
Mayank Grovere1ab96c2018-09-04 20:31:31 +0530423static void update_ker_tags_rdisk_addr(boot_img_hdr *hdr, bool is_arm64)
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700424{
425 /* overwrite the destination of specified for the project */
Channagoud Kadabi7042fa32013-04-26 16:44:14 -0700426#ifdef ABOOT_IGNORE_BOOT_HEADER_ADDRS
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800427 if (is_arm64)
428 hdr->kernel_addr = ABOOT_FORCE_KERNEL64_ADDR;
429 else
430 hdr->kernel_addr = ABOOT_FORCE_KERNEL_ADDR;
Channagoud Kadabi7042fa32013-04-26 16:44:14 -0700431 hdr->ramdisk_addr = ABOOT_FORCE_RAMDISK_ADDR;
432 hdr->tags_addr = ABOOT_FORCE_TAGS_ADDR;
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700433#endif
434}
435
Dima Zavin42168f22009-01-30 11:52:22 -0800436static void ptentry_to_tag(unsigned **ptr, struct ptentry *ptn)
437{
438 struct atag_ptbl_entry atag_ptn;
439
440 memcpy(atag_ptn.name, ptn->name, 16);
441 atag_ptn.name[15] = '\0';
442 atag_ptn.offset = ptn->start;
443 atag_ptn.size = ptn->length;
444 atag_ptn.flags = ptn->flags;
445 memcpy(*ptr, &atag_ptn, sizeof(struct atag_ptbl_entry));
446 *ptr += sizeof(struct atag_ptbl_entry) / sizeof(unsigned);
447}
Mayank Grover9df84c02018-08-30 15:46:35 +0530448#ifdef VERIFIED_BOOT_2
449void load_vbmeta_image(void **vbmeta_image_buf, uint32_t *vbmeta_image_sz)
450{
451 int index = 0;
452 char *vbm_img_buf = NULL;
453 unsigned long long ptn = 0;
454 unsigned long long ptn_size = 0;
455
456 /* Immediately return if dtbo is not supported */
457 index = partition_get_index("vbmeta");
458 ptn = partition_get_offset(index);
459 if(!ptn)
460 {
461 dprintf(CRITICAL, "ERROR: vbmeta partition not found.\n");
462 return;
463 }
464
465 ptn_size = partition_get_size(index);
466 if (ptn_size > MAX_SUPPORTED_VBMETA_IMG_BUF)
467 {
468 dprintf(CRITICAL, "ERROR: vbmeta parition size is greater than supported.\n");
469 return;
470 }
471
472 vbm_img_buf = (char *)memalign(CACHE_LINE, ROUNDUP((uint32_t)ptn_size, CACHE_LINE));
473 if (!vbm_img_buf)
474 {
475 dprintf(CRITICAL, "ERROR: vbmeta unable to locate buffer\n");
476 return;
477 }
478
479 mmc_set_lun(partition_get_lun(index));
480 if (mmc_read(ptn, (uint32_t *)vbm_img_buf, (uint32_t)ptn_size))
481 {
482 dprintf(CRITICAL, "ERROR: vbmeta read failure\n");
483 free(vbm_img_buf);
484 return;
485 }
486
487 *vbmeta_image_buf = vbm_img_buf;
488 *vbmeta_image_sz = (uint32_t)ptn_size;
489 return;
490}
491#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -0800492
lijuang102dfa92015-10-09 18:31:03 +0800493#if CHECK_BAT_VOLTAGE
494void update_battery_status(void)
495{
496 snprintf(battery_voltage,MAX_RSP_SIZE, "%d",target_get_battery_voltage());
497 snprintf(battery_soc_ok ,MAX_RSP_SIZE, "%s",target_battery_soc_ok()? "yes":"no");
498}
499#endif
500
michaellin6be1f392020-04-24 15:05:10 +0800501uint32_t GetPcbaVariant(void)
502{
503 uint8_t GPIO_97=10;
504 uint8_t GPIO_98=10;
505 uint8_t GPIO_99=10;
506
507 uint8_t pcba_stage = 0;
508
509 // Config GPIO
510 gpio_tlmm_config(97, 0, GPIO_INPUT, GPIO_NO_PULL, GPIO_2MA, GPIO_ENABLE);
511 gpio_tlmm_config(98, 0, GPIO_INPUT, GPIO_NO_PULL, GPIO_2MA, GPIO_ENABLE);
512 gpio_tlmm_config(99, 0, GPIO_INPUT, GPIO_NO_PULL, GPIO_2MA, GPIO_ENABLE);
513
514 /* Wait for the gpio config to take effect - debounce time */
515 thread_sleep(10);
516
517 /* Get status of GPIO */
518 GPIO_97 = gpio_status(97);
519 GPIO_98 = gpio_status(98);
520 GPIO_99 = gpio_status(99);
521
522 pcba_stage = (GPIO_99 << 2) + (GPIO_98 << 1) + GPIO_97;
523 dprintf(ALWAYS, "pcba_stage status: %u\n", pcba_stage);
524 // GPIO_99 GPIO_98 GPIO_97
525 // 0 0 0 EP0
526 // 0 0 1 EP1
527 // 0 1 0 EP2
528 // 0 1 1 PP
michaellinaf8857a2020-05-05 14:58:47 +0800529 // 0 1 1 FP(8901MB-007)/PP
530 // 1 0 0 MP(8901MB-008)
531 // 1 0 1 MP-8903MB_001
michaellin6be1f392020-04-24 15:05:10 +0800532 return pcba_stage;
533}
534
Neeti Desaie245d492012-06-01 12:52:13 -0700535unsigned char *update_cmdline(const char * cmdline)
Brian Swetland9c4c0752009-01-25 16:23:50 -0800536{
David Ng183a7422009-12-07 14:55:21 -0800537 int cmdline_len = 0;
538 int have_cmdline = 0;
Amol Jadi168b7712012-03-06 16:15:00 -0800539 unsigned char *cmdline_final = NULL;
Neeti Desaie245d492012-06-01 12:52:13 -0700540 int pause_at_bootup = 0;
tracychui9d60fc52020-06-05 17:31:38 +0800541#if defined(ENABLE_PRODINFO_ACCESS)
542 int AdbEnable = 0;
543#endif
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800544 bool warm_boot = false;
Pavel Nedev5614d222013-06-17 18:01:02 +0300545 bool gpt_exists = partition_gpt_exists();
Joonwoo Park61112782013-10-02 19:50:39 -0700546 int have_target_boot_params = 0;
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700547 char *boot_dev_buf = NULL;
Monika Singh292b3e92018-03-17 22:40:23 +0530548#ifdef MDTP_SUPPORT
Mayank Grover351a75e2017-05-30 20:06:08 +0530549 bool is_mdtp_activated = 0;
Monika Singh292b3e92018-03-17 22:40:23 +0530550#endif
Mayank Grover351a75e2017-05-30 20:06:08 +0530551 int current_active_slot = INVALID;
Mayank Grover3804be72017-06-22 11:59:23 +0530552 int system_ptn_index = -1;
553 unsigned int lun = 0;
554 char lun_char_base = 'a';
Sourabh Banerjee386b1322018-02-27 09:37:28 +0530555#if VERITY_LE
556 int syspath_buflen = strlen(verity_dev)
557 + strlen(verity_system_part) + (sizeof(char) * 2) + 2
558 + strlen(verity_params) + sizeof(int) + 2;
559#else
lijuang83ef4b22018-08-23 11:01:55 +0800560 int syspath_buflen = strlen(sys_path) + sizeof(int) + 2; /*allocate buflen for largest possible string*/
561 char dtbo_idx_str[MAX_DTBO_IDX_STR] = "\0";
562 int dtbo_idx = INVALID_PTN;
Parth Dixit1375d9e2019-07-08 20:56:13 +0530563 char dtb_idx_str[MAX_DTB_IDX_STR] = "\0";
564 int dtb_idx = INVALID_PTN;
Sourabh Banerjee386b1322018-02-27 09:37:28 +0530565#endif
Mayank Grover3804be72017-06-22 11:59:23 +0530566 char syspath_buf[syspath_buflen];
Mayank Grover466d6562018-05-10 14:52:20 +0530567#if HIBERNATION_SUPPORT
568 int resume_buflen = strlen(resume) + sizeof(int) + 2;
569 char resume_buf[resume_buflen];
570 int swap_ptn_index = INVALID_PTN;
571#endif
572
Mayank Grover889be1b2017-09-12 20:12:23 +0530573#if VERIFIED_BOOT
574 uint32_t boot_state = RED;
575#endif
Hareesh Gauthamabf1e1e2017-04-12 18:09:24 +0530576
577#if USE_LE_SYSTEMD
578 is_systemd_present=true;
579#endif
580
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700581#if VERIFIED_BOOT
Mayank Grover912eaa62017-10-26 12:08:53 +0530582 if (VB_M <= target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +0530583 {
584 boot_state = boot_verify_get_state();
585 }
Parth Dixitddbc7352015-10-18 03:13:31 +0530586#endif
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700587
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200588#ifdef MDTP_SUPPORT
589 mdtp_activated(&is_mdtp_activated);
590#endif /* MDTP_SUPPORT */
Dima Zavin42168f22009-01-30 11:52:22 -0800591
Brian Swetland9c4c0752009-01-25 16:23:50 -0800592 if (cmdline && cmdline[0]) {
David Ng183a7422009-12-07 14:55:21 -0800593 cmdline_len = strlen(cmdline);
594 have_cmdline = 1;
595 }
596 if (target_is_emmc_boot()) {
597 cmdline_len += strlen(emmc_cmdline);
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700598 boot_dev_buf = (char *) malloc(sizeof(char) * BOOT_DEV_MAX_LEN);
Mayank Groverb716edf2019-05-08 16:06:55 +0530599 if (!boot_dev_buf) {
600 dprintf(CRITICAL, "ERROR: Failed to allocate boot_dev_buf\n");
601 } else {
lijuangd28587b2021-04-27 15:54:05 +0800602 platform_boot_dev_cmdline(boot_dev_buf, sizeof(char) * BOOT_DEV_MAX_LEN);
Mayank Groverb716edf2019-05-08 16:06:55 +0530603#if USE_BOOTDEV_CMDLINE
604 cmdline_len += strlen(boot_dev_buf);
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700605#endif
Mayank Groverb716edf2019-05-08 16:06:55 +0530606 if (target_dynamic_partition_supported()) {
607 cmdline_len += strlen(dynamic_bootdev_cmdline);
608 cmdline_len += strlen(boot_dev_buf);
609 }
610 }
David Ng183a7422009-12-07 14:55:21 -0800611 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800612
613 cmdline_len += strlen(usb_sn_cmdline);
614 cmdline_len += strlen(sn_buf);
tracychui9d60fc52020-06-05 17:31:38 +0800615#if defined(ENABLE_PRODINFO_ACCESS)
616 cmdline_len += strlen(cust_sn_cmdline);
617 cmdline_len += strlen(cust_sn_buf);
618 cmdline_len += strlen(factory_sn_cmdline);
619 cmdline_len += strlen(factory_sn_buf);
620#endif
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800621
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700622#if VERIFIED_BOOT
Mayank Grover912eaa62017-10-26 12:08:53 +0530623 if (VB_M <= target_get_vb_version())
Sridhar Parasuram7d8bb9a2015-09-23 18:21:19 -0700624 {
Mayank Grover889be1b2017-09-12 20:12:23 +0530625 cmdline_len += strlen(verified_state) + strlen(vbsn[boot_state].name);
626 if ((device.verity_mode != 0 ) && (device.verity_mode != 1))
627 {
628 dprintf(CRITICAL, "Devinfo paritition possibly corrupted!!!. Please erase devinfo partition to continue booting\n");
629 ASSERT(0);
630 }
631 cmdline_len += strlen(verity_mode) + strlen(vbvm[device.verity_mode].name);
632 cmdline_len += strlen(keymaster_v1);
Sridhar Parasuram7d8bb9a2015-09-23 18:21:19 -0700633 }
Parth Dixitddbc7352015-10-18 03:13:31 +0530634#endif
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700635
Monika Singh292b3e92018-03-17 22:40:23 +0530636
637 if (vbcmdline != NULL) {
638 dprintf(DEBUG, "UpdateCmdLine vbcmdline present len %d\n",
639 strlen(vbcmdline));
640 cmdline_len += strlen(vbcmdline);
641 }
642
Pavel Nedev5614d222013-06-17 18:01:02 +0300643 if (boot_into_recovery && gpt_exists)
644 cmdline_len += strlen(secondary_gpt_enable);
645
Monika Singh292b3e92018-03-17 22:40:23 +0530646#ifdef MDTP_SUPPORT
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200647 if(is_mdtp_activated)
648 cmdline_len += strlen(mdtp_activated_flag);
Monika Singh292b3e92018-03-17 22:40:23 +0530649#endif
Pavel Nedev328ac822013-04-05 15:25:11 +0300650 if (boot_into_ffbm) {
651 cmdline_len += strlen(androidboot_mode);
Hareesh Gauthamabf1e1e2017-04-12 18:09:24 +0530652
653 if(is_systemd_present)
654 cmdline_len += strlen(systemd_ffbm_mode);
655
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700656 cmdline_len += strlen(ffbm_mode_string);
Pavel Nedev898298c2013-02-27 12:36:09 -0800657 /* reduce kernel console messages to speed-up boot */
658 cmdline_len += strlen(loglevel);
Matthew Qind886f3c2014-01-17 16:52:01 +0800659 } else if (boot_reason_alarm) {
660 cmdline_len += strlen(alarmboot_cmdline);
Zhenhua Huang431dafa2015-06-30 16:13:37 +0800661 } else if ((target_build_variant_user() || device.charger_screen_enabled)
lijuang266b17d2018-08-17 18:53:42 +0800662 && target_pause_for_battery_charge() && !boot_into_recovery) {
David Ngf773dde2010-07-26 19:55:08 -0700663 pause_at_bootup = 1;
664 cmdline_len += strlen(battchg_pause);
665 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800666
tracychui9d60fc52020-06-05 17:31:38 +0800667#if defined(ENABLE_PRODINFO_ACCESS)
668 if (prod.is_adb_enabled) {
669 dprintf(CRITICAL, "Device will enable adb\n");
670
671 prod.is_adb_enabled = 0;
672 write_prod_info(&prod);
673
674 AdbEnable = 1;
675 cmdline_len += strlen(UsbAdbEnable);
676 }
677#endif
678
Shashank Mittalcd98d472011-08-02 14:29:24 -0700679 if(target_use_signed_kernel() && auth_kernel_img) {
680 cmdline_len += strlen(auth_kernel);
681 }
682
Joonwoo Park61112782013-10-02 19:50:39 -0700683 if (get_target_boot_params(cmdline, boot_into_recovery ? "recoveryfs" :
684 "system",
vijay kumar870515d2015-08-31 16:37:24 +0530685 &target_boot_params) == 0) {
Joonwoo Park61112782013-10-02 19:50:39 -0700686 have_target_boot_params = 1;
687 cmdline_len += strlen(target_boot_params);
688 }
689
Ajay Dudanid04110c2011-01-17 23:55:07 -0800690 /* Determine correct androidboot.baseband to use */
691 switch(target_baseband())
692 {
693 case BASEBAND_APQ:
694 cmdline_len += strlen(baseband_apq);
695 break;
696
697 case BASEBAND_MSM:
698 cmdline_len += strlen(baseband_msm);
699 break;
700
701 case BASEBAND_CSFB:
702 cmdline_len += strlen(baseband_csfb);
703 break;
704
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800705 case BASEBAND_SVLTE2A:
706 cmdline_len += strlen(baseband_svlte2a);
Ajay Dudanid04110c2011-01-17 23:55:07 -0800707 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700708
709 case BASEBAND_MDM:
710 cmdline_len += strlen(baseband_mdm);
711 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700712
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800713 case BASEBAND_MDM2:
714 cmdline_len += strlen(baseband_mdm2);
715 break;
716
Amol Jadi5c61a952012-05-04 17:05:35 -0700717 case BASEBAND_SGLTE:
718 cmdline_len += strlen(baseband_sglte);
719 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530720
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800721 case BASEBAND_SGLTE2:
722 cmdline_len += strlen(baseband_sglte2);
723 break;
724
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530725 case BASEBAND_DSDA:
726 cmdline_len += strlen(baseband_dsda);
727 break;
Amol Jadi2a15a272013-01-22 12:03:36 -0800728
729 case BASEBAND_DSDA2:
730 cmdline_len += strlen(baseband_dsda2);
731 break;
Vijay Kumar Pendotib228cfc2016-06-13 20:15:23 +0530732 case BASEBAND_APQ_NOWGR:
733 cmdline_len += strlen(baseband_apq_nowgr);
734 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800735 }
736
michaellin6be1f392020-04-24 15:05:10 +0800737 switch(GetPcbaVariant())
738 {
739 case 0: // EP0
740 cmdline_len += strlen(PCBA_STAGE_0);
741 break;
742
743 case 1: // EP1
744 cmdline_len += strlen(PCBA_STAGE_1);
745 break;
746
747 case 2: // EP2
748 cmdline_len += strlen(PCBA_STAGE_2);
749 break;
750
michaellinaf8857a2020-05-05 14:58:47 +0800751 case 3: // FP(8901MB-007)/PP
michaellin6be1f392020-04-24 15:05:10 +0800752 cmdline_len += strlen(PCBA_STAGE_3);
753 break;
754
michaellinaf8857a2020-05-05 14:58:47 +0800755 case 4: // MP(8901MB-008)
michaellin6be1f392020-04-24 15:05:10 +0800756 cmdline_len += strlen(PCBA_STAGE_4);
757 break;
758
michaellinaf8857a2020-05-05 14:58:47 +0800759 case 5: // MP-8903MB_001
760 cmdline_len += strlen(PCBA_STAGE_5);
761 break;
762
michaellin6be1f392020-04-24 15:05:10 +0800763 default:// Reserved
764 cmdline_len += strlen(PCBA_STAGE_F);
765 break;
766 }
767
Vladislav Levenetz3cd45242017-04-05 08:58:07 +0300768#if ENABLE_DISPLAY
Lijuan Gao4a5b8322014-07-24 10:38:42 +0800769 if (cmdline) {
770 if ((strstr(cmdline, DISPLAY_DEFAULT_PREFIX) == NULL) &&
Padmanabhan Komandurubccbcdc2015-06-30 16:19:24 +0530771 target_display_panel_node(display_panel_buf,
772 MAX_PANEL_BUF_SIZE) &&
Lijuan Gao4a5b8322014-07-24 10:38:42 +0800773 strlen(display_panel_buf)) {
774 cmdline_len += strlen(display_panel_buf);
775 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700776 }
Vladislav Levenetz3cd45242017-04-05 08:58:07 +0300777#endif
Dhaval Patel223ec952013-07-18 14:49:44 -0700778
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800779 if (target_warm_boot()) {
780 warm_boot = true;
781 cmdline_len += strlen(warmboot_cmdline);
782 }
783
Mayank Grover5384ed82018-05-09 12:09:24 +0530784 if (target_uses_system_as_root() ||
785 partition_multislot_is_supported())
Mayank Grover351a75e2017-05-30 20:06:08 +0530786 {
787 current_active_slot = partition_find_active_slot();
788 cmdline_len += (strlen(androidboot_slot_suffix)+
789 strlen(SUFFIX_SLOT(current_active_slot)));
790
Mayank Grover3804be72017-06-22 11:59:23 +0530791 system_ptn_index = partition_get_index("system");
792 if (platform_boot_dev_isemmc())
793 {
Sourabh Banerjee386b1322018-02-27 09:37:28 +0530794#if VERITY_LE
795 /*
796 Condition 4: Verity and A/B both enabled
797 Eventual command line looks like:
798 ... androidboot.slot_suffix=<slot_suffix> ... rootfstype=ext4 ...
799 ... root=/dev/dm-0 dm="system_<slot_suffix> none ro,0 1 android-verity /dev/mmcblk0p<NN>"
800 */
801 snprintf(syspath_buf, syspath_buflen, " %s %s%s %s%d\"",
802 verity_dev,
803 verity_system_part, suffix_slot[current_active_slot],
804 verity_params, system_ptn_index + 1);
805#else
806 /*
807 Condition 5: A/B enabled, but verity disabled
808 Eventual command line looks like:
809 ... androidboot.slot_suffix=<slot_suffix> ... rootfstype=ext4 ...
810 ... root=/dev/mmcblk0p<NN> ...
811 */
812 snprintf(syspath_buf, syspath_buflen, " %s%d",
813 sys_path, system_ptn_index + 1);
814#endif
Mayank Grover3804be72017-06-22 11:59:23 +0530815 }
816 else
817 {
818 lun = partition_get_lun(system_ptn_index);
819 snprintf(syspath_buf, syspath_buflen, " root=/dev/sd%c%d",
820 lun_char_base + lun,
821 partition_get_index_in_lun("system", lun));
822 }
Mayank Grover351a75e2017-05-30 20:06:08 +0530823
Monika Singh292b3e92018-03-17 22:40:23 +0530824#ifndef VERIFIED_BOOT_2
Mayank Grover3804be72017-06-22 11:59:23 +0530825 cmdline_len += strlen(syspath_buf);
Monika Singh292b3e92018-03-17 22:40:23 +0530826#endif
Mayank Grover5384ed82018-05-09 12:09:24 +0530827 }
828
829 if (target_uses_system_as_root() ||
830 partition_multislot_is_supported())
831 {
832 cmdline_len += strlen(sys_path_cmdline);
Mayank Groverb716edf2019-05-08 16:06:55 +0530833
834 /* For dynamic partition, support skip skip_initramfs */
835 if (!target_dynamic_partition_supported() &&
836 !boot_into_recovery)
Mayank Grover351a75e2017-05-30 20:06:08 +0530837 cmdline_len += strlen(skip_ramfs);
838 }
839
Mayank Grover466d6562018-05-10 14:52:20 +0530840#if HIBERNATION_SUPPORT
841 if (platform_boot_dev_isemmc())
842 {
843 swap_ptn_index = partition_get_index("swap");
844 if (swap_ptn_index != INVALID_PTN)
845 {
846 snprintf(resume_buf, resume_buflen,
847 " %s%d", resume,
848 (swap_ptn_index + 1));
849 cmdline_len += strlen(resume_buf);
850 }
851 else
852 {
853 dprintf(INFO, "WARNING: swap partition not found\n");
854 }
855 }
856#endif
857
Channagoud Kadabi85c7ec32016-01-28 23:09:21 -0800858#if TARGET_CMDLINE_SUPPORT
859 char *target_cmdline_buf = malloc(TARGET_MAX_CMDLNBUF);
860 int target_cmd_line_len;
861 ASSERT(target_cmdline_buf);
862 target_cmd_line_len = target_update_cmdline(target_cmdline_buf);
863 cmdline_len += target_cmd_line_len;
864#endif
865
lijuang83ef4b22018-08-23 11:01:55 +0800866#if !VERITY_LE
867 dtbo_idx = get_dtbo_idx ();
868 if (dtbo_idx != INVALID_PTN) {
869 snprintf(dtbo_idx_str, sizeof(dtbo_idx_str), "%s%d",
870 android_boot_dtbo_idx, dtbo_idx);
871 cmdline_len += strlen (dtbo_idx_str);
872 }
Parth Dixit1375d9e2019-07-08 20:56:13 +0530873
874 dtb_idx = get_dtb_idx ();
875 if (dtb_idx != INVALID_PTN) {
876 snprintf(dtb_idx_str, sizeof(dtb_idx_str), "%s%d",
877 android_boot_dtb_idx, dtb_idx);
878 cmdline_len += strlen (dtb_idx_str);
879 }
lijuang83ef4b22018-08-23 11:01:55 +0800880#endif
881
jhchena8a15dd2020-04-24 15:45:57 +0800882#ifdef ENABLE_FUSE_CHECK
883 {
884 int hw_key_status = readl(0xA01D0);
885 is_fused = (hw_key_status & 0x2) >> 1;
886 dprintf(CRITICAL, "hw_key_status = 0x%x, is_fused=%d\n", hw_key_status, is_fused);
887 if (is_fused) {
888 cmdline_len += strlen(fuse_blown);
889 } else {
890 cmdline_len += strlen(fuse_not_blown);
891 }
892 }
893#endif
894
David Ng183a7422009-12-07 14:55:21 -0800895 if (cmdline_len > 0) {
896 const char *src;
Maria Yu52254c02014-07-04 16:14:54 +0800897 unsigned char *dst;
898
899 cmdline_final = (unsigned char*) malloc((cmdline_len + 4) & (~3));
900 ASSERT(cmdline_final != NULL);
vijay kumar287bb542015-09-29 13:01:52 +0530901 memset((void *)cmdline_final, 0, sizeof(*cmdline_final));
Maria Yu52254c02014-07-04 16:14:54 +0800902 dst = cmdline_final;
Neeti Desaie245d492012-06-01 12:52:13 -0700903
Amol Jadi168b7712012-03-06 16:15:00 -0800904 /* Save start ptr for debug print */
David Ng183a7422009-12-07 14:55:21 -0800905 if (have_cmdline) {
906 src = cmdline;
907 while ((*dst++ = *src++));
908 }
909 if (target_is_emmc_boot()) {
910 src = emmc_cmdline;
911 if (have_cmdline) --dst;
David Ngf773dde2010-07-26 19:55:08 -0700912 have_cmdline = 1;
913 while ((*dst++ = *src++));
Sridhar Parasuram7bd4aaf2015-02-12 11:14:38 -0800914#if USE_BOOTDEV_CMDLINE
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700915 src = boot_dev_buf;
Mayank Groverb716edf2019-05-08 16:06:55 +0530916 if (have_cmdline &&
917 boot_dev_buf) {
918 --dst;
919 while ((*dst++ = *src++));
920 }
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700921#endif
Mayank Groverb716edf2019-05-08 16:06:55 +0530922 /* Dynamic partition append boot_devices */
923 if (target_dynamic_partition_supported() &&
924 boot_dev_buf) {
925 src = dynamic_bootdev_cmdline;
926 if (have_cmdline) --dst;
927 while ((*dst++ = *src++));
928 src = boot_dev_buf;
929 if (have_cmdline) --dst;
930 while ((*dst++ = *src++));
931 }
David Ngf773dde2010-07-26 19:55:08 -0700932 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800933
michaellin6be1f392020-04-24 15:05:10 +0800934 switch(GetPcbaVariant())
935 {
936 case 0:
937 src = PCBA_STAGE_0;
938 if (have_cmdline) --dst;
939 while ((*dst++ = *src++));
940 break;
941 case 1:
942 src = PCBA_STAGE_1;
943 if (have_cmdline) --dst;
944 while ((*dst++ = *src++));
945 break;
946 case 2:
947 src = PCBA_STAGE_2;
948 if (have_cmdline) --dst;
949 while ((*dst++ = *src++));
950 break;
951 case 3:
952 src = PCBA_STAGE_3;
953 if (have_cmdline) --dst;
954 while ((*dst++ = *src++));
955 break;
956 case 4:
957 src = PCBA_STAGE_4;
958 if (have_cmdline) --dst;
959 while ((*dst++ = *src++));
960 break;
michaellinaf8857a2020-05-05 14:58:47 +0800961 case 5:
962 src = PCBA_STAGE_5;
963 if (have_cmdline) --dst;
964 while ((*dst++ = *src++));
965 break;
michaellin6be1f392020-04-24 15:05:10 +0800966 default:
967 src = PCBA_STAGE_F;
968 if (have_cmdline) --dst;
969 while ((*dst++ = *src++));
970 break;
971 }
972
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700973#if VERIFIED_BOOT
Mayank Grover912eaa62017-10-26 12:08:53 +0530974 if (VB_M <= target_get_vb_version())
Sridhar Parasuram7d8bb9a2015-09-23 18:21:19 -0700975 {
Mayank Grover889be1b2017-09-12 20:12:23 +0530976 src = verified_state;
977 if(have_cmdline) --dst;
978 have_cmdline = 1;
979 while ((*dst++ = *src++));
980 src = vbsn[boot_state].name;
981 if(have_cmdline) --dst;
982 while ((*dst++ = *src++));
983
984 if ((device.verity_mode != 0 ) && (device.verity_mode != 1))
985 {
986 dprintf(CRITICAL, "Devinfo paritition possibly corrupted!!!. Please erase devinfo partition to continue booting\n");
987 ASSERT(0);
988 }
989 src = verity_mode;
990 if(have_cmdline) --dst;
991 while ((*dst++ = *src++));
992 src = vbvm[device.verity_mode].name;
993 if(have_cmdline) -- dst;
994 while ((*dst++ = *src++));
995 src = keymaster_v1;
996 if(have_cmdline) --dst;
997 while ((*dst++ = *src++));
Sridhar Parasuram7d8bb9a2015-09-23 18:21:19 -0700998 }
Parth Dixitddbc7352015-10-18 03:13:31 +0530999#endif
Monika Singh292b3e92018-03-17 22:40:23 +05301000
1001 if (vbcmdline != NULL) {
1002 src = vbcmdline;
1003 if (have_cmdline) --dst;
1004 while ((*dst++ = *src++));
1005 }
1006
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -08001007 src = usb_sn_cmdline;
1008 if (have_cmdline) --dst;
1009 have_cmdline = 1;
1010 while ((*dst++ = *src++));
1011 src = sn_buf;
1012 if (have_cmdline) --dst;
1013 have_cmdline = 1;
1014 while ((*dst++ = *src++));
tracychui9d60fc52020-06-05 17:31:38 +08001015
1016 #if defined(ENABLE_PRODINFO_ACCESS)
1017 src = cust_sn_cmdline;
1018 if (have_cmdline) --dst;
1019 have_cmdline = 1;
1020 while ((*dst++ = *src++));
1021 src = cust_sn_buf;
1022 if (have_cmdline) --dst;
1023 have_cmdline = 1;
1024 while ((*dst++ = *src++));
1025 src = factory_sn_cmdline;
1026 if (have_cmdline) --dst;
1027 have_cmdline = 1;
1028 while ((*dst++ = *src++));
1029 src = factory_sn_buf;
1030 if (have_cmdline) --dst;
1031 have_cmdline = 1;
1032 while ((*dst++ = *src++));
1033 #endif
Hanumant Singh8e1ac232014-01-29 13:41:51 -08001034 if (warm_boot) {
1035 if (have_cmdline) --dst;
1036 src = warmboot_cmdline;
1037 while ((*dst++ = *src++));
1038 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -08001039
Pavel Nedev5614d222013-06-17 18:01:02 +03001040 if (boot_into_recovery && gpt_exists) {
1041 src = secondary_gpt_enable;
1042 if (have_cmdline) --dst;
1043 while ((*dst++ = *src++));
1044 }
Monika Singh292b3e92018-03-17 22:40:23 +05301045#ifdef MDTP_SUPPORT
Shay Nachmanibc10dfe2015-02-10 14:45:55 +02001046 if (is_mdtp_activated) {
1047 src = mdtp_activated_flag;
1048 if (have_cmdline) --dst;
1049 while ((*dst++ = *src++));
1050 }
Monika Singh292b3e92018-03-17 22:40:23 +05301051#endif
Pavel Nedev328ac822013-04-05 15:25:11 +03001052 if (boot_into_ffbm) {
1053 src = androidboot_mode;
1054 if (have_cmdline) --dst;
1055 while ((*dst++ = *src++));
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07001056 src = ffbm_mode_string;
Pavel Nedev328ac822013-04-05 15:25:11 +03001057 if (have_cmdline) --dst;
1058 while ((*dst++ = *src++));
Hareesh Gauthamabf1e1e2017-04-12 18:09:24 +05301059
1060 if(is_systemd_present) {
1061 src = systemd_ffbm_mode;
1062 if (have_cmdline) --dst;
1063 while ((*dst++ = *src++));
1064 }
1065
Pavel Nedev898298c2013-02-27 12:36:09 -08001066 src = loglevel;
1067 if (have_cmdline) --dst;
1068 while ((*dst++ = *src++));
Matthew Qind886f3c2014-01-17 16:52:01 +08001069 } else if (boot_reason_alarm) {
1070 src = alarmboot_cmdline;
1071 if (have_cmdline) --dst;
1072 while ((*dst++ = *src++));
Pavel Nedev328ac822013-04-05 15:25:11 +03001073 } else if (pause_at_bootup) {
David Ngf773dde2010-07-26 19:55:08 -07001074 src = battchg_pause;
1075 if (have_cmdline) --dst;
David Ng183a7422009-12-07 14:55:21 -08001076 while ((*dst++ = *src++));
1077 }
Ajay Dudanid04110c2011-01-17 23:55:07 -08001078
tracychui9d60fc52020-06-05 17:31:38 +08001079#if defined(ENABLE_PRODINFO_ACCESS)
1080 if (AdbEnable) {
1081 src = UsbAdbEnable;
1082 if (have_cmdline) --dst;
1083 while ((*dst++ = *src++));
1084 }
1085#endif
1086
Shashank Mittalcd98d472011-08-02 14:29:24 -07001087 if(target_use_signed_kernel() && auth_kernel_img) {
1088 src = auth_kernel;
1089 if (have_cmdline) --dst;
1090 while ((*dst++ = *src++));
1091 }
1092
Ajay Dudanid04110c2011-01-17 23:55:07 -08001093 switch(target_baseband())
1094 {
1095 case BASEBAND_APQ:
1096 src = baseband_apq;
1097 if (have_cmdline) --dst;
1098 while ((*dst++ = *src++));
1099 break;
1100
1101 case BASEBAND_MSM:
1102 src = baseband_msm;
1103 if (have_cmdline) --dst;
1104 while ((*dst++ = *src++));
1105 break;
1106
1107 case BASEBAND_CSFB:
1108 src = baseband_csfb;
1109 if (have_cmdline) --dst;
1110 while ((*dst++ = *src++));
1111 break;
1112
Ajay Dudani6cff85e2011-02-04 16:02:16 -08001113 case BASEBAND_SVLTE2A:
1114 src = baseband_svlte2a;
Ajay Dudanid04110c2011-01-17 23:55:07 -08001115 if (have_cmdline) --dst;
1116 while ((*dst++ = *src++));
1117 break;
Ajay Dudani403bc492011-09-30 16:17:21 -07001118
1119 case BASEBAND_MDM:
1120 src = baseband_mdm;
1121 if (have_cmdline) --dst;
1122 while ((*dst++ = *src++));
1123 break;
Amol Jadi5c61a952012-05-04 17:05:35 -07001124
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -08001125 case BASEBAND_MDM2:
1126 src = baseband_mdm2;
1127 if (have_cmdline) --dst;
1128 while ((*dst++ = *src++));
1129 break;
1130
Amol Jadi5c61a952012-05-04 17:05:35 -07001131 case BASEBAND_SGLTE:
1132 src = baseband_sglte;
1133 if (have_cmdline) --dst;
1134 while ((*dst++ = *src++));
1135 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +05301136
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -08001137 case BASEBAND_SGLTE2:
1138 src = baseband_sglte2;
1139 if (have_cmdline) --dst;
1140 while ((*dst++ = *src++));
1141 break;
1142
Channagoud Kadabi141f2982012-10-31 11:23:02 +05301143 case BASEBAND_DSDA:
1144 src = baseband_dsda;
1145 if (have_cmdline) --dst;
1146 while ((*dst++ = *src++));
1147 break;
Amol Jadi2a15a272013-01-22 12:03:36 -08001148
1149 case BASEBAND_DSDA2:
1150 src = baseband_dsda2;
1151 if (have_cmdline) --dst;
1152 while ((*dst++ = *src++));
1153 break;
Vijay Kumar Pendotib228cfc2016-06-13 20:15:23 +05301154 case BASEBAND_APQ_NOWGR:
1155 src = baseband_apq_nowgr;
1156 if (have_cmdline) --dst;
1157 while ((*dst++ = *src++));
1158 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -08001159 }
Dhaval Patel223ec952013-07-18 14:49:44 -07001160
1161 if (strlen(display_panel_buf)) {
Dhaval Patel223ec952013-07-18 14:49:44 -07001162 src = display_panel_buf;
1163 if (have_cmdline) --dst;
1164 while ((*dst++ = *src++));
1165 }
Joonwoo Park61112782013-10-02 19:50:39 -07001166
1167 if (have_target_boot_params) {
1168 if (have_cmdline) --dst;
1169 src = target_boot_params;
1170 while ((*dst++ = *src++));
vijay kumar870515d2015-08-31 16:37:24 +05301171 free(target_boot_params);
Joonwoo Park61112782013-10-02 19:50:39 -07001172 }
Channagoud Kadabi85c7ec32016-01-28 23:09:21 -08001173
Mayank Grover351a75e2017-05-30 20:06:08 +05301174 if (partition_multislot_is_supported() && have_cmdline)
1175 {
1176 src = androidboot_slot_suffix;
1177 --dst;
1178 while ((*dst++ = *src++));
1179 --dst;
1180 src = SUFFIX_SLOT(current_active_slot);
1181 while ((*dst++ = *src++));
Mayank Grover5384ed82018-05-09 12:09:24 +05301182 }
Mayank Grover351a75e2017-05-30 20:06:08 +05301183
Mayank Grover351a75e2017-05-30 20:06:08 +05301184
Mayank Grover5384ed82018-05-09 12:09:24 +05301185 /*
1186 * System-As-Root behaviour, system.img should contain both
1187 * system content and ramdisk content, and should be mounted at
1188 * root(a/b).
1189 * Apending skip_ramfs for non a/b builds which use, system as root.
1190 */
1191 if ((target_uses_system_as_root() ||
1192 partition_multislot_is_supported()) &&
1193 have_cmdline)
1194 {
Mayank Groverb716edf2019-05-08 16:06:55 +05301195 if (!target_dynamic_partition_supported() &&
1196 !boot_into_recovery)
Mayank Grover5384ed82018-05-09 12:09:24 +05301197 {
1198 src = skip_ramfs;
Mayank Grover351a75e2017-05-30 20:06:08 +05301199 --dst;
1200 while ((*dst++ = *src++));
Mayank Grover5384ed82018-05-09 12:09:24 +05301201 }
1202
1203 src = sys_path_cmdline;
1204 --dst;
1205 while ((*dst++ = *src++));
Mayank Grover3804be72017-06-22 11:59:23 +05301206
Monika Singh292b3e92018-03-17 22:40:23 +05301207#ifndef VERIFIED_BOOT_2
Mayank Grover5384ed82018-05-09 12:09:24 +05301208 src = syspath_buf;
1209 --dst;
1210 while ((*dst++ = *src++));
Monika Singh292b3e92018-03-17 22:40:23 +05301211#endif
Mayank Grover351a75e2017-05-30 20:06:08 +05301212 }
1213
Mayank Grover466d6562018-05-10 14:52:20 +05301214#if HIBERNATION_SUPPORT
1215 if (swap_ptn_index != INVALID_PTN)
1216 {
1217 src = resume_buf;
1218 --dst;
1219 while ((*dst++ = *src++));
1220 }
1221#endif
1222
Channagoud Kadabi85c7ec32016-01-28 23:09:21 -08001223#if TARGET_CMDLINE_SUPPORT
1224 if (target_cmdline_buf && target_cmd_line_len)
1225 {
1226 if (have_cmdline) --dst;
1227 src = target_cmdline_buf;
1228 while((*dst++ = *src++));
1229 free(target_cmdline_buf);
1230 }
1231#endif
lijuang83ef4b22018-08-23 11:01:55 +08001232
1233#if !VERITY_LE
1234 if (dtbo_idx != INVALID_PTN) {
1235 src = dtbo_idx_str;
1236 --dst;
1237 while ((*dst++ = *src++));
1238 }
Parth Dixit1375d9e2019-07-08 20:56:13 +05301239
1240 if (dtb_idx != INVALID_PTN) {
1241 src = dtb_idx_str;
1242 --dst;
1243 while ((*dst++ = *src++));
1244 }
lijuang83ef4b22018-08-23 11:01:55 +08001245#endif
jhchena8a15dd2020-04-24 15:45:57 +08001246
1247#ifdef ENABLE_FUSE_CHECK
1248 if (is_fused) {
1249 src = fuse_blown;
1250 if (have_cmdline) --dst;
1251 while ((*dst++ = *src++));
1252 } else {
1253 src = fuse_not_blown;
1254 if (have_cmdline) --dst;
1255 while ((*dst++ = *src++));
1256 }
1257#endif
Neeti Desaie245d492012-06-01 12:52:13 -07001258 }
Dhaval Patel223ec952013-07-18 14:49:44 -07001259
1260
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -07001261 if (boot_dev_buf)
1262 free(boot_dev_buf);
1263
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -08001264 if (cmdline_final)
1265 dprintf(INFO, "cmdline: %s\n", cmdline_final);
1266 else
1267 dprintf(INFO, "cmdline is NULL\n");
Neeti Desaie245d492012-06-01 12:52:13 -07001268 return cmdline_final;
1269}
1270
1271unsigned *atag_core(unsigned *ptr)
1272{
1273 /* CORE */
1274 *ptr++ = 2;
1275 *ptr++ = 0x54410001;
1276
1277 return ptr;
1278
1279}
1280
1281unsigned *atag_ramdisk(unsigned *ptr, void *ramdisk,
1282 unsigned ramdisk_size)
1283{
1284 if (ramdisk_size) {
1285 *ptr++ = 4;
1286 *ptr++ = 0x54420005;
1287 *ptr++ = (unsigned)ramdisk;
1288 *ptr++ = ramdisk_size;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001289 }
1290
Neeti Desaie245d492012-06-01 12:52:13 -07001291 return ptr;
1292}
1293
1294unsigned *atag_ptable(unsigned **ptr_addr)
1295{
1296 int i;
1297 struct ptable *ptable;
1298
1299 if ((ptable = flash_get_ptable()) && (ptable->count != 0)) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001300 *(*ptr_addr)++ = 2 + (ptable->count * (sizeof(struct atag_ptbl_entry) /
1301 sizeof(unsigned)));
Neeti Desaie245d492012-06-01 12:52:13 -07001302 *(*ptr_addr)++ = 0x4d534d70;
1303 for (i = 0; i < ptable->count; ++i)
1304 ptentry_to_tag(ptr_addr, ptable_get(ptable, i));
1305 }
1306
1307 return (*ptr_addr);
1308}
1309
1310unsigned *atag_cmdline(unsigned *ptr, const char *cmdline)
1311{
1312 int cmdline_length = 0;
1313 int n;
Neeti Desaie245d492012-06-01 12:52:13 -07001314 char *dest;
1315
Amol Jadi10c7d1c2013-01-25 13:24:29 -08001316 cmdline_length = strlen((const char*)cmdline);
Neeti Desaie245d492012-06-01 12:52:13 -07001317 n = (cmdline_length + 4) & (~3);
1318
1319 *ptr++ = (n / 4) + 2;
1320 *ptr++ = 0x54410009;
1321 dest = (char *) ptr;
Amol Jadi10c7d1c2013-01-25 13:24:29 -08001322 while ((*dest++ = *cmdline++));
Neeti Desaie245d492012-06-01 12:52:13 -07001323 ptr += (n / 4);
1324
1325 return ptr;
1326}
1327
1328unsigned *atag_end(unsigned *ptr)
1329{
Brian Swetland9c4c0752009-01-25 16:23:50 -08001330 /* END */
1331 *ptr++ = 0;
1332 *ptr++ = 0;
1333
Neeti Desaie245d492012-06-01 12:52:13 -07001334 return ptr;
1335}
1336
1337void generate_atags(unsigned *ptr, const char *cmdline,
1338 void *ramdisk, unsigned ramdisk_size)
1339{
vijay kumar21a37452015-12-29 16:23:21 +05301340 unsigned *orig_ptr = ptr;
Neeti Desaie245d492012-06-01 12:52:13 -07001341 ptr = atag_core(ptr);
1342 ptr = atag_ramdisk(ptr, ramdisk, ramdisk_size);
1343 ptr = target_atag_mem(ptr);
1344
1345 /* Skip NAND partition ATAGS for eMMC boot */
1346 if (!target_is_emmc_boot()){
1347 ptr = atag_ptable(&ptr);
1348 }
1349
vijay kumar21a37452015-12-29 16:23:21 +05301350 /*
1351 * Atags size filled till + cmdline size + 1 unsigned for 4-byte boundary + 4 unsigned
1352 * for atag identifier in atag_cmdline and atag_end should be with in MAX_TAGS_SIZE bytes
1353 */
Mayank Groverbc8a2ef2018-02-23 18:03:36 +05301354 if (!cmdline)
1355 return;
1356
vijay kumar21a37452015-12-29 16:23:21 +05301357 if (((ptr - orig_ptr) + strlen(cmdline) + 5 * sizeof(unsigned)) < MAX_TAGS_SIZE) {
1358 ptr = atag_cmdline(ptr, cmdline);
1359 ptr = atag_end(ptr);
1360 }
1361 else {
1362 dprintf(CRITICAL,"Crossing ATAGs Max size allowed\n");
1363 ASSERT(0);
1364 }
Neeti Desaie245d492012-06-01 12:52:13 -07001365}
1366
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001367typedef void entry_func_ptr(unsigned, unsigned, unsigned*);
Neeti Desaie245d492012-06-01 12:52:13 -07001368void boot_linux(void *kernel, unsigned *tags,
1369 const char *cmdline, unsigned machtype,
1370 void *ramdisk, unsigned ramdisk_size)
1371{
Amol Jadi10c7d1c2013-01-25 13:24:29 -08001372 unsigned char *final_cmdline;
Amol Jadib6be5c12012-11-14 13:39:51 -08001373#if DEVICE_TREE
Neeti Desai17379b82012-06-04 18:42:53 -07001374 int ret = 0;
Amol Jadib6be5c12012-11-14 13:39:51 -08001375#endif
1376
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001377 void (*entry)(unsigned, unsigned, unsigned*) = (entry_func_ptr*)(PA((addr_t)kernel));
Deepa Dinamani25a9f762012-11-30 15:57:15 -08001378 uint32_t tags_phys = PA((addr_t)tags);
vijay kumar1a50a642015-11-16 12:41:15 +05301379 struct kernel64_hdr *kptr = ((struct kernel64_hdr*)(PA((addr_t)kernel)));
Deepa Dinamani25a9f762012-11-30 15:57:15 -08001380
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301381 ramdisk = (void *)PA((addr_t)ramdisk);
Neeti Desaie245d492012-06-01 12:52:13 -07001382
Amol Jadi10c7d1c2013-01-25 13:24:29 -08001383 final_cmdline = update_cmdline((const char*)cmdline);
1384
Neeti Desai17379b82012-06-04 18:42:53 -07001385#if DEVICE_TREE
Amol Jadib6be5c12012-11-14 13:39:51 -08001386 dprintf(INFO, "Updating device tree: start\n");
1387
Neeti Desai17379b82012-06-04 18:42:53 -07001388 /* Update the Device Tree */
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301389 ret = update_device_tree((void *)tags,(const char *)final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -07001390 if(ret)
1391 {
1392 dprintf(CRITICAL, "ERROR: Updating Device Tree Failed \n");
1393 ASSERT(0);
1394 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001395 dprintf(INFO, "Updating device tree: done\n");
Neeti Desai17379b82012-06-04 18:42:53 -07001396#else
Neeti Desaie245d492012-06-01 12:52:13 -07001397 /* Generating the Atags */
Amol Jadi10c7d1c2013-01-25 13:24:29 -08001398 generate_atags(tags, final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -07001399#endif
Neeti Desaie245d492012-06-01 12:52:13 -07001400
Monika Singh0ec6db82019-07-18 16:57:58 +05301401#if VERIFIED_BOOT
Monika Singhb0db4b82018-09-26 12:18:02 +05301402 if (VB_M <= target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +05301403 {
1404 if (device.verity_mode == 0) {
lijuangbdd9bb42016-03-01 18:22:17 +08001405#if FBCON_DISPLAY_MSG
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07001406#if ENABLE_VB_ATTEST
Mayank Grover889be1b2017-09-12 20:12:23 +05301407 display_bootverify_menu(DISPLAY_MENU_EIO);
1408 wait_for_users_action();
1409 if(!pwr_key_is_pressed)
1410 shutdown_device();
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07001411#else
Mayank Grover889be1b2017-09-12 20:12:23 +05301412 display_bootverify_menu(DISPLAY_MENU_LOGGING);
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07001413#endif
Mayank Grover889be1b2017-09-12 20:12:23 +05301414 wait_for_users_action();
lijuangbdd9bb42016-03-01 18:22:17 +08001415#else
Mayank Grover889be1b2017-09-12 20:12:23 +05301416 dprintf(CRITICAL,
1417 "The dm-verity is not started in enforcing mode.\nWait for 5 seconds before proceeding\n");
1418 mdelay(5000);
lijuangbdd9bb42016-03-01 18:22:17 +08001419#endif
Mayank Grover889be1b2017-09-12 20:12:23 +05301420 }
lijuangbdd9bb42016-03-01 18:22:17 +08001421 }
lijuangbdd9bb42016-03-01 18:22:17 +08001422#endif
1423
1424#if VERIFIED_BOOT
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001425 /* Write protect the device info */
Channagoud Kadabi3bd9d1e2015-05-05 16:18:20 -07001426 if (!boot_into_recovery && target_build_variant_user() && devinfo_present && mmc_write_protect("devinfo", 1))
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001427 {
1428 dprintf(INFO, "Failed to write protect dev info\n");
1429 ASSERT(0);
1430 }
1431#endif
1432
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08001433 /* Turn off splash screen if enabled */
1434#if DISPLAY_SPLASH_SCREEN
1435 target_display_shutdown();
1436#endif
1437
Veera Sundaram Sankaran67ea0932015-09-25 10:09:30 -07001438 /* Perform target specific cleanup */
1439 target_uninit();
Monika Singh292b3e92018-03-17 22:40:23 +05301440 free_verified_boot_resource(&info);
1441 if (final_cmdline)
1442 free(final_cmdline);
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08001443
Deepa Dinamani33734bc2013-03-06 12:16:06 -08001444 dprintf(INFO, "booting linux @ %p, ramdisk @ %p (%d), tags/device tree @ %p\n",
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301445 entry, ramdisk, ramdisk_size, (void *)tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001446
1447 enter_critical_section();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001448
Amol Jadi4421e652011-06-16 15:00:48 -07001449 /* do any platform specific cleanup before kernel entry */
1450 platform_uninit();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001451
Brian Swetland9c4c0752009-01-25 16:23:50 -08001452 arch_disable_cache(UCACHE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001453
Amol Jadi504f9fe2012-08-16 13:56:48 -07001454#if ARM_WITH_MMU
Brian Swetland9c4c0752009-01-25 16:23:50 -08001455 arch_disable_mmu();
Amol Jadi504f9fe2012-08-16 13:56:48 -07001456#endif
Amol Jadi492d5a52013-03-15 16:12:34 -07001457 bs_set_timestamp(BS_KERNEL_ENTRY);
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001458
1459 if (IS_ARM64(kptr))
1460 /* Jump to a 64bit kernel */
1461 scm_elexec_call((paddr_t)kernel, tags_phys);
1462 else
1463 /* Jump to a 32bit kernel */
1464 entry(0, machtype, (unsigned*)tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001465}
1466
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001467/* Function to check if the memory address range falls within the aboot
1468 * boundaries.
1469 * start: Start of the memory region
1470 * size: Size of the memory region
1471 */
Vijay Kumar Pendoti9c002ad2016-03-09 13:52:45 +05301472int check_aboot_addr_range_overlap(uintptr_t start, uint32_t size)
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001473{
1474 /* Check for boundary conditions. */
Sundarajan Srinivasance2a0ea2013-12-16 17:02:56 -08001475 if ((UINT_MAX - start) < size)
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001476 return -1;
1477
1478 /* Check for memory overlap. */
1479 if ((start < MEMBASE) && ((start + size) <= MEMBASE))
1480 return 0;
Channagoud Kadabi94143912013-10-15 12:53:52 -07001481 else if (start >= (MEMBASE + MEMSIZE))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001482 return 0;
1483 else
1484 return -1;
1485}
1486
Mayank Grovere559cec2017-10-17 15:12:03 +05301487/* Function to check if the memory address range falls beyond ddr region.
1488 * start: Start of the memory region
1489 * size: Size of the memory region
1490 */
1491int check_ddr_addr_range_bound(uintptr_t start, uint32_t size)
1492{
1493 uintptr_t ddr_pa_start_addr = PA(get_ddr_start());
1494 uint64_t ddr_size = smem_get_ddr_size();
1495 uint64_t ddr_pa_end_addr = ddr_pa_start_addr + ddr_size;
1496 uintptr_t pa_start_addr = PA(start);
1497
1498 /* Check for boundary conditions. */
1499 if ((UINT_MAX - start) < size)
1500 return -1;
1501
1502 /* Check if memory range is beyond the ddr range. */
1503 if (pa_start_addr < ddr_pa_start_addr ||
1504 pa_start_addr >= (ddr_pa_end_addr) ||
1505 (pa_start_addr + size) > ddr_pa_end_addr)
1506 return -1;
1507 else
1508 return 0;
1509}
1510
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001511BUF_DMA_ALIGN(buf, BOOT_IMG_MAX_PAGE_SIZE); //Equal to max-supported pagesize
Dima Zavin214cc642009-01-26 11:16:21 -08001512
Mayank Groverdd080e82018-09-04 20:12:13 +05301513int getimage(void **image_buffer, uint32_t *imgsize,
1514 const char *imgname)
Monika Singh292b3e92018-03-17 22:40:23 +05301515{
Mayank Groverdd080e82018-09-04 20:12:13 +05301516 uint32_t loadedindex;
1517 if (image_buffer == NULL || imgsize == NULL ||
Monika Singh292b3e92018-03-17 22:40:23 +05301518 imgname == NULL) {
1519 dprintf(CRITICAL, "getimage: invalid parameters\n");
1520 return -1;
1521 }
Mayank Groverdd080e82018-09-04 20:12:13 +05301522 for (loadedindex = 0; loadedindex < info.num_loaded_images; loadedindex++) {
1523 if (!strncmp(info.images[loadedindex].name, imgname,
Monika Singh292b3e92018-03-17 22:40:23 +05301524 strlen(imgname))) {
Mayank Groverdd080e82018-09-04 20:12:13 +05301525 *image_buffer = info.images[loadedindex].image_buffer;
1526 *imgsize = info.images[loadedindex].imgsize;
Mayank Grover027a9412018-09-04 15:12:05 +05301527 dprintf(SPEW, "getimage(): Loaded image [%s|%d]\n",
1528 info.images[loadedindex].name,
1529 info.images[loadedindex].imgsize);
Monika Singh292b3e92018-03-17 22:40:23 +05301530 return 0;
1531 }
1532 }
1533 return -1;
1534}
1535
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001536static void verify_signed_bootimg(uint32_t bootimg_addr, uint32_t bootimg_size)
1537{
1538 int ret;
Channagoud Kadabia8c623f2015-01-13 14:48:48 -08001539
1540#if !VERIFIED_BOOT
Sundarajan Srinivasance54d6e2013-11-11 12:45:00 -08001541#if IMAGE_VERIF_ALGO_SHA1
1542 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA1;
1543#else
1544 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA256;
1545#endif
Channagoud Kadabia8c623f2015-01-13 14:48:48 -08001546#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001547
1548 /* Assume device is rooted at this time. */
1549 device.is_tampered = 1;
1550
1551 dprintf(INFO, "Authenticating boot image (%d): start\n", bootimg_size);
1552
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001553#if VERIFIED_BOOT
Monika Singh7d2fc272018-03-16 17:16:01 +05301554 uint32_t bootstate;
1555 if(boot_into_recovery &&
Mayank Grover68fbf0d2017-10-24 14:13:39 +05301556 (!partition_multislot_is_supported()))
Monika Singh7d2fc272018-03-16 17:16:01 +05301557 {
1558 ret = boot_verify_image((unsigned char *)bootimg_addr,
1559 bootimg_size, "/recovery", &bootstate);
1560 }
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001561 else
Monika Singh7d2fc272018-03-16 17:16:01 +05301562 {
1563 ret = boot_verify_image((unsigned char *)bootimg_addr,
1564 bootimg_size, "/boot", &bootstate);
1565 }
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001566 boot_verify_print_state();
1567#else
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001568 ret = image_verify((unsigned char *)bootimg_addr,
1569 (unsigned char *)(bootimg_addr + bootimg_size),
1570 bootimg_size,
Sundarajan Srinivasance54d6e2013-11-11 12:45:00 -08001571 auth_algo);
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001572#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001573 dprintf(INFO, "Authenticating boot image: done return value = %d\n", ret);
1574
1575 if (ret)
1576 {
1577 /* Authorized kernel */
1578 device.is_tampered = 0;
Sundarajan Srinivasan3fb21f12013-09-16 18:36:15 -07001579 auth_kernel_img = 1;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001580 }
1581
Amit Blay4aa292f2015-04-28 21:55:59 +03001582#ifdef MDTP_SUPPORT
1583 {
1584 /* Verify MDTP lock.
1585 * For boot & recovery partitions, use aboot's verification result.
1586 */
1587 mdtp_ext_partition_verification_t ext_partition;
1588 ext_partition.partition = boot_into_recovery ? MDTP_PARTITION_RECOVERY : MDTP_PARTITION_BOOT;
1589 ext_partition.integrity_state = device.is_tampered ? MDTP_PARTITION_STATE_INVALID : MDTP_PARTITION_STATE_VALID;
1590 ext_partition.page_size = 0; /* Not needed since already validated */
1591 ext_partition.image_addr = 0; /* Not needed since already validated */
1592 ext_partition.image_size = 0; /* Not needed since already validated */
1593 ext_partition.sig_avail = FALSE; /* Not needed since already validated */
1594 mdtp_fwlock_verify_lock(&ext_partition);
1595 }
1596#endif /* MDTP_SUPPORT */
1597
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001598#if USE_PCOM_SECBOOT
1599 set_tamper_flag(device.is_tampered);
1600#endif
1601
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001602#if VERIFIED_BOOT
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001603 switch(boot_verify_get_state())
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001604 {
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001605 case RED:
lijuanga40d6302015-07-20 20:10:13 +08001606#if FBCON_DISPLAY_MSG
lijuang9a7d3b92015-11-30 14:41:24 +08001607 display_bootverify_menu(DISPLAY_MENU_RED);
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07001608#if ENABLE_VB_ATTEST
1609 mdelay(DELAY_WAIT);
1610 shutdown_device();
1611#else
lijuanga40d6302015-07-20 20:10:13 +08001612 wait_for_users_action();
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07001613#endif
lijuanga40d6302015-07-20 20:10:13 +08001614#else
1615 dprintf(CRITICAL,
1616 "Your device has failed verification and may not work properly.\nWait for 5 seconds before proceeding\n");
1617 mdelay(5000);
1618#endif
1619
1620 break;
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001621 case YELLOW:
lijuanga40d6302015-07-20 20:10:13 +08001622#if FBCON_DISPLAY_MSG
lijuang9a7d3b92015-11-30 14:41:24 +08001623 display_bootverify_menu(DISPLAY_MENU_YELLOW);
lijuanga40d6302015-07-20 20:10:13 +08001624 wait_for_users_action();
1625#else
1626 dprintf(CRITICAL,
1627 "Your device has loaded a different operating system.\nWait for 5 seconds before proceeding\n");
1628 mdelay(5000);
1629#endif
1630 break;
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001631 default:
lijuanga40d6302015-07-20 20:10:13 +08001632 break;
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001633 }
1634#endif
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001635#if !VERIFIED_BOOT
Unnati Gandhi1be04752015-03-27 19:41:53 +05301636 if(device.is_tampered)
1637 {
1638 write_device_info_mmc(&device);
1639 #ifdef TZ_TAMPER_FUSE
Monika Singh98257462018-06-06 11:28:49 +05301640 set_tamper_fuse_cmd(HLOS_IMG_TAMPER_FUSE);
Unnati Gandhi1be04752015-03-27 19:41:53 +05301641 #endif
1642 #ifdef ASSERT_ON_TAMPER
1643 dprintf(CRITICAL, "Device is tampered. Asserting..\n");
1644 ASSERT(0);
1645 #endif
1646 }
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001647#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001648}
1649
Maunik Shah0f3c8ac2014-03-02 17:47:58 +05301650static bool check_format_bit()
1651{
1652 bool ret = false;
1653 int index;
1654 uint64_t offset;
1655 struct boot_selection_info *in = NULL;
1656 char *buf = NULL;
1657
1658 index = partition_get_index("bootselect");
1659 if (index == INVALID_PTN)
1660 {
1661 dprintf(INFO, "Unable to locate /bootselect partition\n");
1662 return ret;
1663 }
1664 offset = partition_get_offset(index);
1665 if(!offset)
1666 {
1667 dprintf(INFO, "partition /bootselect doesn't exist\n");
1668 return ret;
1669 }
1670 buf = (char *) memalign(CACHE_LINE, ROUNDUP(page_size, CACHE_LINE));
Mayank Grover48860402016-11-29 12:34:53 +05301671 mmc_set_lun(partition_get_lun(index));
Maunik Shah0f3c8ac2014-03-02 17:47:58 +05301672 ASSERT(buf);
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301673 if (mmc_read(offset, (uint32_t *)buf, page_size))
Maunik Shah0f3c8ac2014-03-02 17:47:58 +05301674 {
1675 dprintf(INFO, "mmc read failure /bootselect %d\n", page_size);
1676 free(buf);
1677 return ret;
1678 }
1679 in = (struct boot_selection_info *) buf;
1680 if ((in->signature == BOOTSELECT_SIGNATURE) &&
1681 (in->version == BOOTSELECT_VERSION)) {
1682 if ((in->state_info & BOOTSELECT_FORMAT) &&
1683 !(in->state_info & BOOTSELECT_FACTORY))
1684 ret = true;
1685 } else {
1686 dprintf(CRITICAL, "Signature: 0x%08x or version: 0x%08x mismatched of /bootselect\n",
1687 in->signature, in->version);
1688 ASSERT(0);
1689 }
1690 free(buf);
1691 return ret;
1692}
1693
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001694void boot_verifier_init()
1695{
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001696 uint32_t boot_state;
1697 /* Check if device unlock */
1698 if(device.is_unlocked)
1699 {
1700 boot_verify_send_event(DEV_UNLOCK);
1701 boot_verify_print_state();
1702 dprintf(CRITICAL, "Device is unlocked! Skipping verification...\n");
1703 return;
1704 }
1705 else
1706 {
1707 boot_verify_send_event(BOOT_INIT);
1708 }
1709
1710 /* Initialize keystore */
1711 boot_state = boot_verify_keystore_init();
1712 if(boot_state == YELLOW)
1713 {
1714 boot_verify_print_state();
1715 dprintf(CRITICAL, "Keystore verification failed! Continuing anyways...\n");
1716 }
1717}
1718
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301719/* Function to return recovery appended dtbo buffer info */
1720void get_recovery_dtbo_info(uint32_t *dtbo_size, void **dtbo_buf)
1721{
1722 *dtbo_size = recovery_dtbo_size;
1723 *dtbo_buf = recovery_dtbo_buf;
1724 return;
1725}
1726
Shashank Mittal23b8f422010-04-16 19:27:21 -07001727int boot_linux_from_mmc(void)
1728{
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301729 boot_img_hdr *hdr = (void*) buf;
1730 boot_img_hdr *uhdr;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001731 unsigned offset = 0;
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07001732 int rcode;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001733 unsigned long long ptn = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001734 int index = INVALID_PTN;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001735
Shashank Mittalcd98d472011-08-02 14:29:24 -07001736 unsigned char *image_addr = 0;
1737 unsigned kernel_actual;
1738 unsigned ramdisk_actual;
1739 unsigned imagesize_actual;
Neeti Desai465491e2012-07-31 12:53:35 -07001740 unsigned second_actual = 0;
Parth Dixit1375d9e2019-07-08 20:56:13 +05301741 void * image_buf = NULL;
Neeti Desai465491e2012-07-31 12:53:35 -07001742
Matthew Qin271927e2015-03-31 22:07:07 -04001743 unsigned int dtb_size = 0;
Parth Dixit1375d9e2019-07-08 20:56:13 +05301744 unsigned dtb_image_size = 0;
1745 uint32_t dtb_image_offset = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08001746 unsigned int out_len = 0;
1747 unsigned int out_avai_len = 0;
1748 unsigned char *out_addr = NULL;
1749 uint32_t dtb_offset = 0;
1750 unsigned char *kernel_start_addr = NULL;
1751 unsigned int kernel_size = 0;
Ameya Thakur10a33452016-06-13 14:24:26 -07001752 unsigned int patched_kernel_hdr_size = 0;
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301753 uint64_t image_size = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08001754 int rc;
Monika Singh292b3e92018-03-17 22:40:23 +05301755#if VERIFIED_BOOT_2
1756 int status;
Mayank Grover027a9412018-09-04 15:12:05 +05301757 void *dtbo_image_buf = NULL;
1758 uint32_t dtbo_image_sz = 0;
Mayank Grover9df84c02018-08-30 15:46:35 +05301759 void *vbmeta_image_buf = NULL;
1760 uint32_t vbmeta_image_sz = 0;
Monika Singh292b3e92018-03-17 22:40:23 +05301761#endif
Mayank Groverc93cad82017-10-03 12:23:45 +05301762 char *ptn_name = NULL;
Neeti Desai465491e2012-07-31 12:53:35 -07001763#if DEVICE_TREE
1764 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -07001765 struct dt_entry dt_entry;
Neeti Desai465491e2012-07-31 12:53:35 -07001766 unsigned dt_table_offset;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001767 uint32_t dt_actual;
Deepa Dinamani19648b42013-09-05 17:05:55 -07001768 uint32_t dt_hdr_size;
Matthew Qin271927e2015-03-31 22:07:07 -04001769 unsigned char *best_match_dt_addr = NULL;
Neeti Desai465491e2012-07-31 12:53:35 -07001770#endif
Matthew Qin49e51fa2015-02-09 10:40:45 +08001771 struct kernel64_hdr *kptr = NULL;
Mayank Grover351a75e2017-05-30 20:06:08 +05301772 int current_active_slot = INVALID;
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001773
Maunik Shah0f3c8ac2014-03-02 17:47:58 +05301774 if (check_format_bit())
1775 boot_into_recovery = 1;
1776
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07001777 if (!boot_into_recovery) {
1778 memset(ffbm_mode_string, '\0', sizeof(ffbm_mode_string));
1779 rcode = get_ffbm(ffbm_mode_string, sizeof(ffbm_mode_string));
1780 if (rcode <= 0) {
1781 boot_into_ffbm = false;
1782 if (rcode < 0)
1783 dprintf(CRITICAL,"failed to get ffbm cookie");
1784 } else
1785 boot_into_ffbm = true;
1786 } else
1787 boot_into_ffbm = false;
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301788 uhdr = (boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001789 if (!memcmp(uhdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1790 dprintf(INFO, "Unified boot method!\n");
1791 hdr = uhdr;
1792 goto unified_boot;
1793 }
Mayank Groverc93cad82017-10-03 12:23:45 +05301794
1795 /* For a/b recovery image code is on boot partition.
1796 If we support multislot, always use boot partition. */
1797 if (boot_into_recovery &&
Mayank Grover8ab4a762019-04-25 17:23:34 +05301798 ((!partition_multislot_is_supported()) ||
1799 (target_dynamic_partition_supported())))
Mayank Groverc93cad82017-10-03 12:23:45 +05301800 ptn_name = "recovery";
1801 else
1802 ptn_name = "boot";
1803
1804 index = partition_get_index(ptn_name);
1805 ptn = partition_get_offset(index);
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301806 image_size = partition_get_size(index);
1807 if(ptn == 0 || image_size == 0) {
Mayank Groverc93cad82017-10-03 12:23:45 +05301808 dprintf(CRITICAL, "ERROR: No %s partition found\n", ptn_name);
1809 return -1;
Kinson Chikf1a43512011-07-14 11:28:39 -07001810 }
Mayank Groverc93cad82017-10-03 12:23:45 +05301811
Channagoud Kadabief0547c2015-02-10 12:57:38 -08001812 /* Set Lun for boot & recovery partitions */
1813 mmc_set_lun(partition_get_lun(index));
Shashank Mittal23b8f422010-04-16 19:27:21 -07001814
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301815 if (mmc_read(ptn + offset, (uint32_t *) buf, page_size)) {
Shashank Mittal23b8f422010-04-16 19:27:21 -07001816 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
1817 return -1;
1818 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001819
1820 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001821 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Mayank Grover351a75e2017-05-30 20:06:08 +05301822 return ERR_INVALID_BOOT_MAGIC;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001823 }
1824
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07001825 if (hdr->page_size && (hdr->page_size != page_size)) {
vijay kumar2e21b3a2014-06-26 17:40:15 +05301826
1827 if (hdr->page_size > BOOT_IMG_MAX_PAGE_SIZE) {
1828 dprintf(CRITICAL, "ERROR: Invalid page size\n");
1829 return -1;
1830 }
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07001831 page_size = hdr->page_size;
1832 page_mask = page_size - 1;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001833 }
1834
Matthew Qin49e51fa2015-02-09 10:40:45 +08001835 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1836 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Kishor PKd8ddcad2017-07-27 13:53:57 +05301837 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001838
Matthew Qin49e51fa2015-02-09 10:40:45 +08001839 image_addr = (unsigned char *)target_get_scratch_address();
Mayank Grover6fd36792018-09-10 13:26:58 +05301840#if VERIFIED_BOOT_2
1841 /* Create hole in start of image for VB salt to copy */
1842 image_addr += SALT_BUFF_OFFSET;
1843#endif
Kishor PK9e91b592017-05-24 12:14:55 +05301844 memcpy(image_addr, (void *)buf, page_size);
1845
1846 /* ensure commandline is terminated */
1847 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
Matthew Qin49e51fa2015-02-09 10:40:45 +08001848
1849#if DEVICE_TREE
Parth Dixit4097b622016-03-15 14:42:27 +05301850#ifndef OSVERSION_IN_BOOTIMAGE
1851 dt_size = hdr->dt_size;
Mayank Grover5a502582018-09-12 11:24:49 +05301852#else
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301853 dprintf(INFO, "BootImage Header: %d\n", hdr->header_version);
Mayank Grover5a502582018-09-12 11:24:49 +05301854#endif
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301855
Parth Dixit4097b622016-03-15 14:42:27 +05301856 dt_actual = ROUND_TO_PAGE(dt_size, page_mask);
Kishor PKd8ddcad2017-07-27 13:53:57 +05301857 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 +05301858 dprintf(CRITICAL, "Integer overflow detected in bootimage header fields at %u in %s\n",__LINE__,__FILE__);
1859 return -1;
1860 }
Kishor PKd8ddcad2017-07-27 13:53:57 +05301861 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + second_actual + dt_actual);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001862#else
Kishor PKd8ddcad2017-07-27 13:53:57 +05301863 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 +05301864 dprintf(CRITICAL, "Integer overflow detected in bootimage header fields at %u in %s\n",__LINE__,__FILE__);
1865 return -1;
1866 }
Kishor PKd8ddcad2017-07-27 13:53:57 +05301867 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + second_actual);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001868#endif
Parth Dixit1375d9e2019-07-08 20:56:13 +05301869 dtb_image_size = hdr->kernel_size;
Matthew Qin49e51fa2015-02-09 10:40:45 +08001870
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301871#ifdef OSVERSION_IN_BOOTIMAGE
1872 /* If header version is ONE and booting into recovery,
1873 dtbo is appended with recovery image.
1874 Doing following:
1875 * Validating the recovery offset and size.
1876 * Extracting recovery dtbo to be used as dtbo.
1877 */
1878 if (boot_into_recovery &&
1879 hdr->header_version == BOOT_HEADER_VERSION_ONE)
1880 {
1881 struct boot_img_hdr_v1 *hdr1 =
1882 (struct boot_img_hdr_v1 *) (image_addr + sizeof(boot_img_hdr));
Mayank Grovera1a83c72018-09-24 11:23:15 +05301883 unsigned int recovery_dtbo_actual = 0;
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301884
Mayank Grovera1a83c72018-09-24 11:23:15 +05301885 recovery_dtbo_actual = ROUND_TO_PAGE(hdr1->recovery_dtbo_size, page_mask);
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301886 if ((hdr1->header_size !=
1887 sizeof(struct boot_img_hdr_v1) + sizeof(boot_img_hdr)))
1888 {
1889 dprintf(CRITICAL, "Invalid boot image header: %d\n", hdr1->header_size);
1890 return -1;
1891 }
1892
Mayank Grovera1a83c72018-09-24 11:23:15 +05301893 if (recovery_dtbo_actual > MAX_SUPPORTED_DTBO_IMG_BUF)
1894 {
1895 dprintf(CRITICAL, "Recovery Dtbo Size too big %x, Allowed size %x\n", recovery_dtbo_actual,
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301896 MAX_SUPPORTED_DTBO_IMG_BUF);
1897 return -1;
1898 }
1899
Mayank Grovera1a83c72018-09-24 11:23:15 +05301900 if (UINT_MAX < ((uint64_t)imagesize_actual + recovery_dtbo_actual))
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301901 {
1902 dprintf(CRITICAL, "Integer overflow detected in recoveryimage header fields at %u in %s\n",__LINE__,__FILE__);
1903 return -1;
1904 }
1905
Mayank Grovera1a83c72018-09-24 11:23:15 +05301906 if (UINT_MAX < (hdr1->recovery_dtbo_offset + recovery_dtbo_actual)) {
1907 dprintf(CRITICAL,
1908 "Integer overflow detected in recovery image header fields at %u in %s\n",__LINE__,__FILE__);
1909 return -1;
1910 }
1911
1912 if (hdr1->recovery_dtbo_offset + recovery_dtbo_actual > image_size)
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301913 {
1914 dprintf(CRITICAL, "Invalid recovery dtbo: Recovery Dtbo Offset=0x%llx,"
1915 " Recovery Dtbo Size=0x%x, Image Size=0x%llx\n",
1916 hdr1->recovery_dtbo_offset, recovery_dtbo_size, image_size);
1917 return -1;
1918 }
1919
1920 recovery_dtbo_buf = (void *)(hdr1->recovery_dtbo_offset + image_addr);
Mayank Grovera1a83c72018-09-24 11:23:15 +05301921 recovery_dtbo_size = recovery_dtbo_actual;
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301922 imagesize_actual += recovery_dtbo_size;
1923
1924 dprintf(SPEW, "Header version: %d\n", hdr->header_version);
1925 dprintf(SPEW, "Recovery Dtbo Size 0x%x\n", recovery_dtbo_size);
1926 dprintf(SPEW, "Recovery Dtbo Offset 0x%llx\n", hdr1->recovery_dtbo_offset);
1927
1928 }
Parth Dixit1375d9e2019-07-08 20:56:13 +05301929
1930 if ( hdr->header_version == BOOT_HEADER_VERSION_TWO) {
1931 struct boot_img_hdr_v1 *hdr1 =
1932 (struct boot_img_hdr_v1 *) (image_addr + sizeof(boot_img_hdr));
1933 struct boot_img_hdr_v2 *hdr2 = (struct boot_img_hdr_v2 *)
1934 (image_addr + sizeof(boot_img_hdr) +
1935 BOOT_IMAGE_HEADER_V2_OFFSET);
1936 unsigned int recovery_dtbo_actual = 0;
1937
lijuang0b17ba22019-09-04 14:20:47 +08001938 recovery_dtbo_actual =
1939 ROUND_TO_PAGE(hdr1->recovery_dtbo_size, page_mask);
1940 imagesize_actual += recovery_dtbo_actual;
Parth Dixit1375d9e2019-07-08 20:56:13 +05301941
1942 imagesize_actual += ROUND_TO_PAGE(hdr2->dtb_size, page_mask);
1943
1944
1945 dtb_image_offset = page_size + patched_kernel_hdr_size +
1946 kernel_actual + ramdisk_actual + second_actual +
1947 recovery_dtbo_actual;
1948
1949 dprintf(SPEW, "Header version: %d\n", hdr->header_version);
1950 dprintf(SPEW, "Dtb image offset 0x%x\n", dtb_image_offset);
1951 }
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301952#endif
1953
Parth Dixit1375d9e2019-07-08 20:56:13 +05301954
Saranya Chidurab1aaf232018-11-19 15:47:50 +05301955 /* Validate the boot/recovery image size is within the bounds of partition size */
1956 if (imagesize_actual > image_size) {
1957 dprintf(CRITICAL, "Image size is greater than partition size.\n");
1958 return -1;
1959 }
1960
Matthew Qin49e51fa2015-02-09 10:40:45 +08001961#if VERIFIED_BOOT
1962 boot_verifier_init();
1963#endif
1964
Mayank Grover1ceaee22019-04-01 17:54:32 +05301965#if VERIFIED_BOOT_2
1966 /* read full partition if device is unlocked */
1967 if (device.is_unlocked)
1968 imagesize_actual = image_size;
1969#endif
1970
Vijay Kumar Pendoti9c002ad2016-03-09 13:52:45 +05301971 if (check_aboot_addr_range_overlap((uintptr_t) image_addr, imagesize_actual))
Matthew Qin49e51fa2015-02-09 10:40:45 +08001972 {
1973 dprintf(CRITICAL, "Boot image buffer address overlaps with aboot addresses.\n");
1974 return -1;
1975 }
1976
Matthew Qinbb7923d2015-02-09 10:56:09 +08001977 /*
1978 * Update loading flow of bootimage to support compressed/uncompressed
1979 * bootimage on both 64bit and 32bit platform.
1980 * 1. Load bootimage from emmc partition onto DDR.
1981 * 2. Check if bootimage is gzip format. If yes, decompress compressed kernel
1982 * 3. Check kernel header and update kernel load addr for 64bit and 32bit
1983 * platform accordingly.
1984 * 4. Sanity Check on kernel_addr and ramdisk_addr and copy data.
1985 */
Mayank Grover351a75e2017-05-30 20:06:08 +05301986 if (partition_multislot_is_supported())
1987 {
1988 current_active_slot = partition_find_active_slot();
1989 dprintf(INFO, "Loading boot image (%d) active_slot(%s): start\n",
1990 imagesize_actual, SUFFIX_SLOT(current_active_slot));
1991 }
1992 else
1993 {
1994 dprintf(INFO, "Loading (%s) image (%d): start\n",
1995 (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual);
1996 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08001997 bs_set_timestamp(BS_KERNEL_LOAD_START);
1998
Gaurav Nebhwani43e2a462016-03-17 21:32:56 +05301999 if ((target_get_max_flash_size() - page_size) < imagesize_actual)
2000 {
2001 dprintf(CRITICAL, "booimage size is greater than DDR can hold\n");
2002 return -1;
2003 }
Kishor PK9e91b592017-05-24 12:14:55 +05302004 offset = page_size;
2005 /* Read image without signature and header*/
2006 if (mmc_read(ptn + offset, (void *)(image_addr + offset), imagesize_actual - page_size))
Matthew Qin49e51fa2015-02-09 10:40:45 +08002007 {
2008 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
2009 return -1;
2010 }
2011
Mayank Grover351a75e2017-05-30 20:06:08 +05302012 if (partition_multislot_is_supported())
2013 {
2014 dprintf(INFO, "Loading boot image (%d) active_slot(%s): done\n",
2015 imagesize_actual, SUFFIX_SLOT(current_active_slot));
2016 }
2017 else
2018 {
2019 dprintf(INFO, "Loading (%s) image (%d): done\n",
Channagoud Kadabif0705b52015-08-20 14:16:08 -07002020 (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual);
2021
Mayank Grover351a75e2017-05-30 20:06:08 +05302022 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08002023 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
2024
2025 /* Authenticate Kernel */
2026 dprintf(INFO, "use_signed_kernel=%d, is_unlocked=%d, is_tampered=%d.\n",
2027 (int) target_use_signed_kernel(),
2028 device.is_unlocked,
2029 device.is_tampered);
Monika Singh292b3e92018-03-17 22:40:23 +05302030#if VERIFIED_BOOT_2
Mayank Grover1ceaee22019-04-01 17:54:32 +05302031 /* if device is unlocked skip reading signature, as full partition is read */
2032 if (!device.is_unlocked)
Monika Singh292b3e92018-03-17 22:40:23 +05302033 {
Mayank Grover1ceaee22019-04-01 17:54:32 +05302034 offset = imagesize_actual;
2035 if (check_aboot_addr_range_overlap((uintptr_t)image_addr + offset, page_size))
2036 {
2037 dprintf(CRITICAL, "Signature read buffer address overlaps with aboot addresses.\n");
2038 return -1;
2039 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08002040
Mayank Grover1ceaee22019-04-01 17:54:32 +05302041 /* Read signature */
2042 if(mmc_read(ptn + offset, (void *)(image_addr + offset), page_size))
2043 {
2044 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
2045 return -1;
2046 }
Monika Singh292b3e92018-03-17 22:40:23 +05302047 }
2048
Mayank Grover027a9412018-09-04 15:12:05 +05302049 /* load and validate dtbo partition */
2050 load_validate_dtbo_image(&dtbo_image_buf, &dtbo_image_sz);
2051
Mayank Grover9df84c02018-08-30 15:46:35 +05302052 /* load vbmeta partition */
2053 load_vbmeta_image(&vbmeta_image_buf, &vbmeta_image_sz);
2054
Monika Singh292b3e92018-03-17 22:40:23 +05302055 memset(&info, 0, sizeof(bootinfo));
Mayank Grover027a9412018-09-04 15:12:05 +05302056
2057 /* Pass loaded boot image passed */
Mayank Grover6fd36792018-09-10 13:26:58 +05302058 info.images[IMG_BOOT].image_buffer = SUB_SALT_BUFF_OFFSET(image_addr);
Mayank Grover027a9412018-09-04 15:12:05 +05302059 info.images[IMG_BOOT].imgsize = imagesize_actual;
2060 info.images[IMG_BOOT].name = ptn_name;
2061 ++info.num_loaded_images;
2062
2063 /* Pass loaded dtbo image */
2064 if (dtbo_image_buf != NULL) {
Mayank Grover6fd36792018-09-10 13:26:58 +05302065 info.images[IMG_DTBO].image_buffer =
2066 SUB_SALT_BUFF_OFFSET(dtbo_image_buf);
Mayank Grover027a9412018-09-04 15:12:05 +05302067 info.images[IMG_DTBO].imgsize = dtbo_image_sz;
2068 info.images[IMG_DTBO].name = "dtbo";
2069 ++info.num_loaded_images;
2070 }
2071
Mayank Grover9df84c02018-08-30 15:46:35 +05302072 /* Pass loaded vbmeta image */
2073 if (vbmeta_image_buf != NULL) {
2074 info.images[IMG_VBMETA].image_buffer = vbmeta_image_buf;
2075 info.images[IMG_VBMETA].imgsize = vbmeta_image_sz;
2076 info.images[IMG_VBMETA].name = "vbmeta";
2077 ++info.num_loaded_images;
2078 }
2079
Mayank Grovere1ab96c2018-09-04 20:31:31 +05302080 info.header_version = hdr->header_version;
Monika Singh292b3e92018-03-17 22:40:23 +05302081 info.multi_slot_boot = partition_multislot_is_supported();
2082 info.bootreason_alarm = boot_reason_alarm;
2083 info.bootinto_recovery = boot_into_recovery;
2084 status = load_image_and_auth(&info);
2085 if(status)
2086 return -1;
2087
2088 vbcmdline = info.vbcmdline;
Mayank Grover9df84c02018-08-30 15:46:35 +05302089
2090 /* Free the buffer allocated to vbmeta post verification */
Saranya Chidurab4933332018-10-15 17:30:06 +05302091 if (vbmeta_image_buf != NULL) {
2092 free(vbmeta_image_buf);
2093 --info.num_loaded_images;
2094 }
Monika Singh292b3e92018-03-17 22:40:23 +05302095#else
Channagoud Kadabi736c4962015-08-21 11:56:52 -07002096 /* Change the condition a little bit to include the test framework support.
2097 * We would never reach this point if device is in fastboot mode, even if we did
2098 * that means we are in test mode, so execute kernel authentication part for the
2099 * tests */
Parth Dixitcb0c6082015-12-30 15:01:13 +05302100 if((target_use_signed_kernel() && (!device.is_unlocked)) || is_test_mode_enabled())
Matthew Qin49e51fa2015-02-09 10:40:45 +08002101 {
2102 offset = imagesize_actual;
Vijay Kumar Pendoti9c002ad2016-03-09 13:52:45 +05302103 if (check_aboot_addr_range_overlap((uintptr_t)image_addr + offset, page_size))
Matthew Qin49e51fa2015-02-09 10:40:45 +08002104 {
2105 dprintf(CRITICAL, "Signature read buffer address overlaps with aboot addresses.\n");
2106 return -1;
2107 }
2108
2109 /* Read signature */
2110 if(mmc_read(ptn + offset, (void *)(image_addr + offset), page_size))
2111 {
2112 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
2113 return -1;
2114 }
2115
2116 verify_signed_bootimg((uint32_t)image_addr, imagesize_actual);
Channagoud Kadabi736c4962015-08-21 11:56:52 -07002117 /* The purpose of our test is done here */
Parth Dixitcb0c6082015-12-30 15:01:13 +05302118 if(is_test_mode_enabled() && auth_kernel_img)
Channagoud Kadabi736c4962015-08-21 11:56:52 -07002119 return 0;
Matthew Qin49e51fa2015-02-09 10:40:45 +08002120 } else {
2121 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
2122 #ifdef TZ_SAVE_KERNEL_HASH
2123 aboot_save_boot_hash_mmc((uint32_t) image_addr, imagesize_actual);
2124 #endif /* TZ_SAVE_KERNEL_HASH */
Amit Blay4aa292f2015-04-28 21:55:59 +03002125
2126#ifdef MDTP_SUPPORT
2127 {
2128 /* Verify MDTP lock.
2129 * For boot & recovery partitions, MDTP will use boot_verifier APIs,
2130 * since verification was skipped in aboot. The signature is not part of the loaded image.
2131 */
2132 mdtp_ext_partition_verification_t ext_partition;
2133 ext_partition.partition = boot_into_recovery ? MDTP_PARTITION_RECOVERY : MDTP_PARTITION_BOOT;
2134 ext_partition.integrity_state = MDTP_PARTITION_STATE_UNSET;
2135 ext_partition.page_size = page_size;
2136 ext_partition.image_addr = (uint32)image_addr;
2137 ext_partition.image_size = imagesize_actual;
2138 ext_partition.sig_avail = FALSE;
2139 mdtp_fwlock_verify_lock(&ext_partition);
2140 }
2141#endif /* MDTP_SUPPORT */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08002142 }
Monika Singh292b3e92018-03-17 22:40:23 +05302143#endif
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08002144
Sridhar Parasuram00bfedb2015-05-26 14:21:27 -07002145#if VERIFIED_BOOT
taozhang93088bd2016-11-16 15:50:46 +08002146 if((boot_verify_get_state() == ORANGE) && (!boot_into_ffbm))
Reut Zysmanba8a9d52016-02-18 11:44:04 +02002147 {
2148#if FBCON_DISPLAY_MSG
2149 display_bootverify_menu(DISPLAY_MENU_ORANGE);
2150 wait_for_users_action();
2151#else
2152 dprintf(CRITICAL,
2153 "Your device has been unlocked and can't be trusted.\nWait for 5 seconds before proceeding\n");
2154 mdelay(5000);
2155#endif
2156 }
2157#endif
2158
2159#if VERIFIED_BOOT
Monika Singh292b3e92018-03-17 22:40:23 +05302160 if (VB_M == target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +05302161 {
2162 /* set boot and system versions. */
2163 set_os_version((unsigned char *)image_addr);
2164 // send root of trust
2165 if(!send_rot_command((uint32_t)device.is_unlocked))
2166 ASSERT(0);
2167 }
Parth Dixitbc9b6492015-10-18 00:41:38 +05302168#endif
Channagoud Kadabia22144f2013-03-20 11:49:01 -07002169 /*
Matthew Qinbb7923d2015-02-09 10:56:09 +08002170 * Check if the kernel image is a gzip package. If yes, need to decompress it.
2171 * If not, continue booting.
2172 */
2173 if (is_gzip_package((unsigned char *)(image_addr + page_size), hdr->kernel_size))
2174 {
2175 out_addr = (unsigned char *)(image_addr + imagesize_actual + page_size);
2176 out_avai_len = target_get_max_flash_size() - imagesize_actual - page_size;
Mayank Grover027a9412018-09-04 15:12:05 +05302177#if VERIFIED_BOOT_2
2178 if (dtbo_image_sz)
2179 out_avai_len -= DTBO_IMG_BUF;
2180#endif
Matthew Qin0b15b322015-05-19 05:20:54 -04002181 dprintf(INFO, "decompressing kernel image: start\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08002182 rc = decompress((unsigned char *)(image_addr + page_size),
2183 hdr->kernel_size, out_addr, out_avai_len,
2184 &dtb_offset, &out_len);
2185 if (rc)
2186 {
Matthew Qin0b15b322015-05-19 05:20:54 -04002187 dprintf(CRITICAL, "decompressing kernel image failed!!!\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08002188 ASSERT(0);
2189 }
2190
Matthew Qin0b15b322015-05-19 05:20:54 -04002191 dprintf(INFO, "decompressing kernel image: done\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08002192 kptr = (struct kernel64_hdr *)out_addr;
2193 kernel_start_addr = out_addr;
2194 kernel_size = out_len;
2195 } else {
Ameya Thakur10a33452016-06-13 14:24:26 -07002196 dprintf(INFO, "Uncpmpressed kernel in use\n");
2197 if (!strncmp((char*)(image_addr + page_size),
2198 PATCHED_KERNEL_MAGIC,
2199 sizeof(PATCHED_KERNEL_MAGIC) - 1)) {
2200 dprintf(INFO, "Patched kernel detected\n");
2201 kptr = (struct kernel64_hdr *)(image_addr + page_size +
2202 PATCHED_KERNEL_HEADER_SIZE);
2203 //The size of the kernel is stored at start of kernel image + 16
2204 //The dtb would start just after the kernel
2205 dtb_offset = *((uint32_t*)((unsigned char*)
2206 (image_addr + page_size +
2207 sizeof(PATCHED_KERNEL_MAGIC) -
2208 1)));
2209 //The actual kernel starts after the 20 byte header.
2210 kernel_start_addr = (unsigned char*)(image_addr +
2211 page_size + PATCHED_KERNEL_HEADER_SIZE);
2212 kernel_size = hdr->kernel_size;
2213 patched_kernel_hdr_size = PATCHED_KERNEL_HEADER_SIZE;
2214 } else {
2215 dprintf(INFO, "Kernel image not patched..Unable to locate dt offset\n");
2216 kptr = (struct kernel64_hdr *)(image_addr + page_size);
2217 kernel_start_addr = (unsigned char *)(image_addr + page_size);
2218 kernel_size = hdr->kernel_size;
2219 }
Matthew Qinbb7923d2015-02-09 10:56:09 +08002220 }
2221
2222 /*
Channagoud Kadabia22144f2013-03-20 11:49:01 -07002223 * Update the kernel/ramdisk/tags address if the boot image header
2224 * has default values, these default values come from mkbootimg when
2225 * the boot image is flashed using fastboot flash:raw
2226 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08002227 update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr));
Channagoud Kadabia22144f2013-03-20 11:49:01 -07002228
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002229 /* Get virtual addresses since the hdr saves physical addresses. */
2230 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
2231 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
2232 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002233
Matthew Qinbb7923d2015-02-09 10:56:09 +08002234 kernel_size = ROUND_TO_PAGE(kernel_size, page_mask);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002235 /* Check if the addresses in the header are valid. */
Matthew Qinbb7923d2015-02-09 10:56:09 +08002236 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_size) ||
Mayank Grovere559cec2017-10-17 15:12:03 +05302237 check_ddr_addr_range_bound(hdr->kernel_addr, kernel_size) ||
2238 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual) ||
2239 check_ddr_addr_range_bound(hdr->ramdisk_addr, ramdisk_actual))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002240 {
Mayank Grovere559cec2017-10-17 15:12:03 +05302241 dprintf(CRITICAL, "kernel/ramdisk addresses are not valid.\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002242 return -1;
2243 }
2244
2245#ifndef DEVICE_TREE
Mayank Grovere559cec2017-10-17 15:12:03 +05302246 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE) ||
2247 check_ddr_addr_range_bound(hdr->tags_addr, MAX_TAGS_SIZE))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002248 {
Mayank Grovere559cec2017-10-17 15:12:03 +05302249 dprintf(CRITICAL, "Tags addresses are not valid.\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002250 return -1;
2251 }
2252#endif
2253
Matthew Qin49e51fa2015-02-09 10:40:45 +08002254 /* Move kernel, ramdisk and device tree to correct address */
Matthew Qinbb7923d2015-02-09 10:56:09 +08002255 memmove((void*) hdr->kernel_addr, kernel_start_addr, kernel_size);
Matthew Qin49e51fa2015-02-09 10:40:45 +08002256 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002257
Matthew Qin49e51fa2015-02-09 10:40:45 +08002258 #if DEVICE_TREE
Parth Dixit4097b622016-03-15 14:42:27 +05302259 if(dt_size) {
Matthew Qin49e51fa2015-02-09 10:40:45 +08002260 dt_table_offset = ((uint32_t)image_addr + page_size + kernel_actual + ramdisk_actual + second_actual);
2261 table = (struct dt_table*) dt_table_offset;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002262
Matthew Qin49e51fa2015-02-09 10:40:45 +08002263 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
2264 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
2265 return -1;
2266 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002267
P.V. Phani Kumar82916762016-03-09 09:20:19 +05302268 /* Its Error if, dt_hdr_size (table->num_entries * dt_entry size + Dev_Tree Header)
2269 goes beyound hdr->dt_size*/
Parth Dixit4097b622016-03-15 14:42:27 +05302270 if (dt_hdr_size > ROUND_TO_PAGE(dt_size,hdr->page_size)) {
P.V. Phani Kumar82916762016-03-09 09:20:19 +05302271 dprintf(CRITICAL, "ERROR: Invalid Device Tree size \n");
2272 return -1;
2273 }
2274
Matthew Qin49e51fa2015-02-09 10:40:45 +08002275 /* Find index of device tree within device tree table */
2276 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
2277 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
2278 return -1;
2279 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002280
P.V. Phani Kumar3c333202016-03-09 11:54:37 +05302281 if(dt_entry.offset > (UINT_MAX - dt_entry.size)) {
2282 dprintf(CRITICAL, "ERROR: Device tree contents are Invalid\n");
2283 return -1;
2284 }
2285
2286 /* Ensure we are not overshooting dt_size with the dt_entry selected */
Parth Dixit4097b622016-03-15 14:42:27 +05302287 if ((dt_entry.offset + dt_entry.size) > dt_size) {
P.V. Phani Kumar3c333202016-03-09 11:54:37 +05302288 dprintf(CRITICAL, "ERROR: Device tree contents are Invalid\n");
2289 return -1;
2290 }
2291
Matthew Qin271927e2015-03-31 22:07:07 -04002292 if (is_gzip_package((unsigned char *)dt_table_offset + dt_entry.offset, dt_entry.size))
2293 {
2294 unsigned int compressed_size = 0;
2295 out_addr += out_len;
2296 out_avai_len -= out_len;
Matthew Qin0b15b322015-05-19 05:20:54 -04002297 dprintf(INFO, "decompressing dtb: start\n");
Matthew Qin271927e2015-03-31 22:07:07 -04002298 rc = decompress((unsigned char *)dt_table_offset + dt_entry.offset,
2299 dt_entry.size, out_addr, out_avai_len,
2300 &compressed_size, &dtb_size);
2301 if (rc)
2302 {
Matthew Qin0b15b322015-05-19 05:20:54 -04002303 dprintf(CRITICAL, "decompressing dtb failed!!!\n");
Matthew Qin271927e2015-03-31 22:07:07 -04002304 ASSERT(0);
2305 }
2306
Matthew Qin0b15b322015-05-19 05:20:54 -04002307 dprintf(INFO, "decompressing dtb: done\n");
Matthew Qin271927e2015-03-31 22:07:07 -04002308 best_match_dt_addr = out_addr;
2309 } else {
2310 best_match_dt_addr = (unsigned char *)dt_table_offset + dt_entry.offset;
2311 dtb_size = dt_entry.size;
2312 }
2313
Matthew Qin49e51fa2015-02-09 10:40:45 +08002314 /* Validate and Read device device tree in the tags_addr */
Mayank Grovere559cec2017-10-17 15:12:03 +05302315 if (check_aboot_addr_range_overlap(hdr->tags_addr, dtb_size) ||
2316 check_ddr_addr_range_bound(hdr->tags_addr, dtb_size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002317 {
Mayank Grovere559cec2017-10-17 15:12:03 +05302318 dprintf(CRITICAL, "Device tree addresses are not valid\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002319 return -1;
2320 }
Shashank Mittal162244e2011-08-08 19:01:25 -07002321
Matthew Qin271927e2015-03-31 22:07:07 -04002322 memmove((void *)hdr->tags_addr, (char *)best_match_dt_addr, dtb_size);
Matthew Qin49e51fa2015-02-09 10:40:45 +08002323 } else {
2324 /* Validate the tags_addr */
Mayank Grovere559cec2017-10-17 15:12:03 +05302325 if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual) ||
2326 check_ddr_addr_range_bound(hdr->tags_addr, kernel_actual))
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07002327 {
Mayank Grovere559cec2017-10-17 15:12:03 +05302328 dprintf(CRITICAL, "Device tree addresses are not valid.\n");
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07002329 return -1;
2330 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08002331 /*
2332 * If appended dev tree is found, update the atags with
2333 * memory address to the DTB appended location on RAM.
2334 * Else update with the atags address in the kernel header
Mayank Grover1ed3e8b2018-09-19 15:16:50 +05302335 *
2336 * Make sure everything from scratch address is read before next step!
2337 * In case of dtbo, this API is going to read dtbo on scratch.
Matthew Qin49e51fa2015-02-09 10:40:45 +08002338 */
2339 void *dtb;
Parth Dixit1375d9e2019-07-08 20:56:13 +05302340 image_buf = (void*)(image_addr + page_size + patched_kernel_hdr_size);
2341
Nagireddy Annem0d60bb62019-08-16 19:16:00 +05302342#ifdef OSVERSION_IN_BOOTIMAGE
Parth Dixit1375d9e2019-07-08 20:56:13 +05302343 if ( hdr->header_version == BOOT_HEADER_VERSION_TWO) {
2344
2345 image_buf = (void*)(image_addr);
2346 dtb_offset = dtb_image_offset;
2347 dtb_image_size = imagesize_actual;
2348 }
Nagireddy Annem0d60bb62019-08-16 19:16:00 +05302349#endif
Parth Dixit1375d9e2019-07-08 20:56:13 +05302350
2351 dtb = dev_tree_appended(image_buf, dtb_image_size, dtb_offset,
Ameya Thakur10a33452016-06-13 14:24:26 -07002352 (void *)hdr->tags_addr);
Matthew Qin49e51fa2015-02-09 10:40:45 +08002353 if (!dtb) {
2354 dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07002355 return -1;
2356 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07002357 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08002358 #endif
Shashank Mittal23b8f422010-04-16 19:27:21 -07002359
Stanimir Varbanov69ec5462013-07-18 18:17:42 +03002360 if (boot_into_recovery && !device.is_unlocked && !device.is_tampered)
2361 target_load_ssd_keystore();
2362
Shashank Mittal23b8f422010-04-16 19:27:21 -07002363unified_boot:
Shashank Mittal23b8f422010-04-16 19:27:21 -07002364
Dima Zavin77e41f32013-03-06 16:10:43 -08002365 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07002366 (const char *)hdr->cmdline, board_machtype(),
Shashank Mittal23b8f422010-04-16 19:27:21 -07002367 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
2368
2369 return 0;
2370}
2371
Dima Zavin214cc642009-01-26 11:16:21 -08002372int boot_linux_from_flash(void)
2373{
Mayank Grovere1ab96c2018-09-04 20:31:31 +05302374 boot_img_hdr *hdr = (void*) buf;
Dima Zavin214cc642009-01-26 11:16:21 -08002375 struct ptentry *ptn;
2376 struct ptable *ptable;
2377 unsigned offset = 0;
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08002378
Shashank Mittalcd98d472011-08-02 14:29:24 -07002379 unsigned char *image_addr = 0;
2380 unsigned kernel_actual;
2381 unsigned ramdisk_actual;
2382 unsigned imagesize_actual;
vijay kumar8f53e362015-11-24 13:38:11 +05302383 unsigned second_actual = 0;
Shashank Mittalcd98d472011-08-02 14:29:24 -07002384
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07002385#if DEVICE_TREE
Monika Singh292b3e92018-03-17 22:40:23 +05302386 struct dt_table *table = NULL;
Joel Kingaa335dc2013-06-03 16:11:08 -07002387 struct dt_entry dt_entry;
vijay kumar8f53e362015-11-24 13:38:11 +05302388 unsigned dt_table_offset;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08002389 uint32_t dt_actual;
Monika Singh292b3e92018-03-17 22:40:23 +05302390 uint32_t dt_hdr_size = 0;
Vivek Kumar07bd0862018-04-10 14:19:23 +05302391 uint32_t dtb_offset = 0;
vijay kumar8f53e362015-11-24 13:38:11 +05302392 unsigned int dtb_size = 0;
2393 unsigned char *best_match_dt_addr = NULL;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07002394#endif
2395
David Ng183a7422009-12-07 14:55:21 -08002396 if (target_is_emmc_boot()) {
Mayank Grovere1ab96c2018-09-04 20:31:31 +05302397 hdr = (boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
David Ng183a7422009-12-07 14:55:21 -08002398 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
2399 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
2400 return -1;
2401 }
2402 goto continue_boot;
2403 }
2404
Dima Zavin214cc642009-01-26 11:16:21 -08002405 ptable = flash_get_ptable();
2406 if (ptable == NULL) {
2407 dprintf(CRITICAL, "ERROR: Partition table not found\n");
2408 return -1;
2409 }
2410
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08002411 if(!boot_into_recovery)
2412 {
2413 ptn = ptable_find(ptable, "boot");
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07002414
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08002415 if (ptn == NULL) {
2416 dprintf(CRITICAL, "ERROR: No boot partition found\n");
2417 return -1;
2418 }
2419 }
2420 else
2421 {
2422 ptn = ptable_find(ptable, "recovery");
2423 if (ptn == NULL) {
2424 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
2425 return -1;
2426 }
Dima Zavin214cc642009-01-26 11:16:21 -08002427 }
2428
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05302429 /* Read boot.img header from flash */
Shashank Mittaldcc2e352009-11-19 19:11:16 -08002430 if (flash_read(ptn, offset, buf, page_size)) {
Dima Zavin214cc642009-01-26 11:16:21 -08002431 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
2432 return -1;
2433 }
Dima Zavin214cc642009-01-26 11:16:21 -08002434
2435 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07002436 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Dima Zavin214cc642009-01-26 11:16:21 -08002437 return -1;
2438 }
2439
Shashank Mittaldcc2e352009-11-19 19:11:16 -08002440 if (hdr->page_size != page_size) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07002441 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 -08002442 return -1;
2443 }
2444
Kishor PK9e91b592017-05-24 12:14:55 +05302445 image_addr = (unsigned char *)target_get_scratch_address();
2446 memcpy(image_addr, (void *)buf, page_size);
vijay kumar287bb542015-09-29 13:01:52 +05302447
Channagoud Kadabia22144f2013-03-20 11:49:01 -07002448 /*
2449 * Update the kernel/ramdisk/tags address if the boot image header
2450 * has default values, these default values come from mkbootimg when
2451 * the boot image is flashed using fastboot flash:raw
2452 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08002453 update_ker_tags_rdisk_addr(hdr, false);
Channagoud Kadabia22144f2013-03-20 11:49:01 -07002454
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002455 /* Get virtual addresses since the hdr saves physical addresses. */
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002456 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
2457 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
2458 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
2459
2460 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
2461 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Mayank Grover032736f2017-07-14 20:34:51 +05302462 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002463
Kishor PK9e91b592017-05-24 12:14:55 +05302464 /* ensure commandline is terminated */
2465 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
2466
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002467 /* Check if the addresses in the header are valid. */
2468 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
Mayank Grovere559cec2017-10-17 15:12:03 +05302469 check_ddr_addr_range_bound(hdr->kernel_addr, kernel_actual) ||
2470 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual) ||
2471 check_ddr_addr_range_bound(hdr->ramdisk_addr, ramdisk_actual))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002472 {
Mayank Grovere559cec2017-10-17 15:12:03 +05302473 dprintf(CRITICAL, "kernel/ramdisk addresses are not valid.\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002474 return -1;
2475 }
2476
2477#ifndef DEVICE_TREE
Kishor PKd8ddcad2017-07-27 13:53:57 +05302478 if (UINT_MAX < ((uint64_t)kernel_actual + (uint64_t)ramdisk_actual+ (uint64_t)second_actual + page_size)) {
Mayank Grover032736f2017-07-14 20:34:51 +05302479 dprintf(CRITICAL, "Integer overflow detected in bootimage header fields\n");
2480 return -1;
2481 }
Kishor PKd8ddcad2017-07-27 13:53:57 +05302482 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + second_actual);
Mayank Grover032736f2017-07-14 20:34:51 +05302483
Mayank Grovere559cec2017-10-17 15:12:03 +05302484 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE) ||
2485 check_ddr_addr_range_bound(hdr->tags_addr, MAX_TAGS_SIZE))
Vijay Kumar Pendotid3ed20e2016-09-20 00:34:46 +05302486 {
Mayank Grovere559cec2017-10-17 15:12:03 +05302487 dprintf(CRITICAL, "Tags addresses are not valid.\n");
Vijay Kumar Pendotid3ed20e2016-09-20 00:34:46 +05302488 return -1;
2489 }
2490#else
2491
2492#ifndef OSVERSION_IN_BOOTIMAGE
2493 dt_size = hdr->dt_size;
2494#endif
Mayank Grover032736f2017-07-14 20:34:51 +05302495 dt_actual = ROUND_TO_PAGE(dt_size, page_mask);
Kishor PKd8ddcad2017-07-27 13:53:57 +05302496 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 +05302497 dprintf(CRITICAL, "Integer overflow detected in bootimage header fields\n");
2498 return -1;
2499 }
2500
Kishor PKd8ddcad2017-07-27 13:53:57 +05302501 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + second_actual + dt_actual);
Mayank Grover032736f2017-07-14 20:34:51 +05302502
Mayank Grovere559cec2017-10-17 15:12:03 +05302503 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_size) ||
2504 check_ddr_addr_range_bound(hdr->tags_addr, dt_size))
Mayank Grover032736f2017-07-14 20:34:51 +05302505 {
Mayank Grovere559cec2017-10-17 15:12:03 +05302506 dprintf(CRITICAL, "Device tree addresses are not valid.\n");
Mayank Grover032736f2017-07-14 20:34:51 +05302507 return -1;
2508 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002509#endif
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002510
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05302511 /* Read full boot.img from flash */
2512 dprintf(INFO, "Loading (%s) image (%d): start\n",
2513 (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual);
2514 bs_set_timestamp(BS_KERNEL_LOAD_START);
2515
2516 if (UINT_MAX - page_size < imagesize_actual)
2517 {
2518 dprintf(CRITICAL,"Integer overflow detected in bootimage header fields %u %s\n", __LINE__,__func__);
2519 return -1;
2520 }
2521
2522 /*Check the availability of RAM before reading boot image + max signature length from flash*/
2523 if (target_get_max_flash_size() < (imagesize_actual + page_size))
2524 {
2525 dprintf(CRITICAL, "bootimage size is greater than DDR can hold\n");
2526 return -1;
2527 }
2528
2529 offset = page_size;
2530 /* Read image without signature and header */
2531 if (flash_read(ptn, offset, (void *)(image_addr + offset), imagesize_actual - page_size))
2532 {
2533 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
2534 return -1;
2535 }
2536
2537 dprintf(INFO, "Loading (%s) image (%d): done\n",
2538 (!boot_into_recovery ? "boot" : "recovery"), imagesize_actual);
2539 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
2540
Shashank Mittalcd98d472011-08-02 14:29:24 -07002541 /* Authenticate Kernel */
Deepa Dinamani23b60d42013-06-24 18:10:52 -07002542 if(target_use_signed_kernel() && (!device.is_unlocked))
Shashank Mittalcd98d472011-08-02 14:29:24 -07002543 {
Shashank Mittalcd98d472011-08-02 14:29:24 -07002544 offset = imagesize_actual;
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05302545
Shashank Mittalcd98d472011-08-02 14:29:24 -07002546 /* Read signature */
2547 if (flash_read(ptn, offset, (void *)(image_addr + offset), page_size))
2548 {
2549 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002550 return -1;
Shashank Mittalcd98d472011-08-02 14:29:24 -07002551 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07002552
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05302553 verify_signed_bootimg((uint32_t)image_addr, imagesize_actual);
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05302554 }
2555 offset = page_size;
2556 if(hdr->second_size != 0) {
2557 if (UINT_MAX - offset < second_actual)
2558 {
2559 dprintf(CRITICAL, "ERROR: Integer overflow in boot image header %s\t%d\n",__func__,__LINE__);
2560 return -1;
vijay kumar8f53e362015-11-24 13:38:11 +05302561 }
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05302562 offset += second_actual;
2563 /* Second image loading not implemented. */
2564 ASSERT(0);
2565 }
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05302566 /* Move kernel and ramdisk to correct address */
2567 memmove((void*) hdr->kernel_addr, (char*) (image_addr + page_size), hdr->kernel_size);
2568 memmove((void*) hdr->ramdisk_addr, (char*) (image_addr + page_size + kernel_actual), hdr->ramdisk_size);
2569
2570#if DEVICE_TREE
2571 if(dt_size != 0) {
2572
2573 dt_table_offset = ((uint32_t)image_addr + page_size + kernel_actual + ramdisk_actual + second_actual);
2574
2575 table = (struct dt_table*) dt_table_offset;
2576
2577 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
2578 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
2579 return -1;
2580 }
2581
2582 /* Its Error if, dt_hdr_size (table->num_entries * dt_entry size + Dev_Tree Header)
2583 goes beyound hdr->dt_size*/
2584 if (dt_hdr_size > ROUND_TO_PAGE(dt_size,hdr->page_size)) {
2585 dprintf(CRITICAL, "ERROR: Invalid Device Tree size \n");
2586 return -1;
2587 }
2588
2589 /* Find index of device tree within device tree table */
2590 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
2591 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
2592 return -1;
2593 }
2594
2595 /* Validate and Read device device tree in the "tags_add */
2596 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size) ||
2597 check_ddr_addr_range_bound(hdr->tags_addr, dt_entry.size))
2598 {
2599 dprintf(CRITICAL, "Device tree addresses are not valid.\n");
2600 return -1;
2601 }
2602
2603 if(dt_entry.offset > (UINT_MAX - dt_entry.size)) {
2604 dprintf(CRITICAL, "ERROR: Device tree contents are Invalid\n");
2605 return -1;
2606 }
2607
2608 /* Ensure we are not overshooting dt_size with the dt_entry selected */
2609 if ((dt_entry.offset + dt_entry.size) > dt_size) {
2610 dprintf(CRITICAL, "ERROR: Device tree contents are Invalid\n");
2611 return -1;
2612 }
2613
2614 best_match_dt_addr = (unsigned char *)table + dt_entry.offset;
2615 dtb_size = dt_entry.size;
2616 memmove((void *)hdr->tags_addr, (char *)best_match_dt_addr, dtb_size);
Vivek Kumar07bd0862018-04-10 14:19:23 +05302617
2618 } else {
2619 /* Validate the tags_addr */
2620 if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual) ||
2621 check_ddr_addr_range_bound(hdr->tags_addr, kernel_actual))
2622 {
2623 dprintf(CRITICAL, "Device tree addresses are not valid.\n");
2624 return -1;
2625 }
2626 /*
2627 * If appended dev tree is found, update the atags with
2628 * memory address to the DTB appended location on RAM.
2629 * Else update with the atags address in the kernel header
Mayank Grover1ed3e8b2018-09-19 15:16:50 +05302630 *
2631 * Make sure everything from scratch address is read before next step!
2632 * In case of dtbo, this API is going to read dtbo on scratch.
Vivek Kumar07bd0862018-04-10 14:19:23 +05302633 */
2634 void *dtb = NULL;
2635 dtb = dev_tree_appended((void*)(image_addr + page_size ),hdr->kernel_size, dtb_offset, (void *)hdr->tags_addr);
2636 if (!dtb) {
2637 dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
2638 return -1;
2639 }
2640 }
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05302641#endif
2642 if(target_use_signed_kernel() && (!device.is_unlocked))
2643 {
Shashank Mittal162244e2011-08-08 19:01:25 -07002644 /* Make sure everything from scratch address is read before next step!*/
Shashank Mittala0032282011-08-26 14:50:11 -07002645 if(device.is_tampered)
Shashank Mittal162244e2011-08-08 19:01:25 -07002646 {
2647 write_device_info_flash(&device);
2648 }
Channagoud Kadabi5c86fe32012-02-16 10:58:48 +05302649#if USE_PCOM_SECBOOT
2650 set_tamper_flag(device.is_tampered);
2651#endif
Shashank Mittalcd98d472011-08-02 14:29:24 -07002652 }
David Ng183a7422009-12-07 14:55:21 -08002653continue_boot:
Dima Zavin214cc642009-01-26 11:16:21 -08002654
Dima Zavin214cc642009-01-26 11:16:21 -08002655 /* TODO: create/pass atags to kernel */
2656
Ajay Dudanie28a6072011-07-01 13:59:46 -07002657 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07002658 (const char *)hdr->cmdline, board_machtype(),
Dima Zavin214cc642009-01-26 11:16:21 -08002659 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
2660
2661 return 0;
2662}
Brian Swetland9c4c0752009-01-25 16:23:50 -08002663
Shashank Mittal162244e2011-08-08 19:01:25 -07002664void write_device_info_mmc(device_info *dev)
2665{
Shashank Mittal162244e2011-08-08 19:01:25 -07002666 unsigned long long ptn = 0;
2667 unsigned long long size;
2668 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002669 uint8_t lun = 0;
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002670 uint32_t ret = 0;
Mayank Groverddd348d2018-01-23 14:07:09 +05302671 uint32_t device_info_sz = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07002672
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002673 if (devinfo_present)
2674 index = partition_get_index("devinfo");
2675 else
2676 index = partition_get_index("aboot");
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07002677
Shashank Mittal162244e2011-08-08 19:01:25 -07002678 ptn = partition_get_offset(index);
2679 if(ptn == 0)
2680 {
2681 return;
2682 }
2683
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002684 lun = partition_get_lun(index);
2685 mmc_set_lun(lun);
2686
Shashank Mittal162244e2011-08-08 19:01:25 -07002687 size = partition_get_size(index);
2688
Mayank Groverddd348d2018-01-23 14:07:09 +05302689 device_info_sz = ROUND_TO_PAGE(sizeof(struct device_info),
2690 mmc_blocksize_mask);
2691 if (device_info_sz == UINT_MAX)
2692 {
2693 dprintf(CRITICAL, "ERROR: Incorrect blocksize of card\n");
2694 return;
2695 }
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07002696
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002697 if (devinfo_present)
Mayank Groverddd348d2018-01-23 14:07:09 +05302698 ret = mmc_write(ptn, device_info_sz, (void *)info_buf);
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002699 else
Mayank Groverddd348d2018-01-23 14:07:09 +05302700 ret = mmc_write((ptn + size - device_info_sz), device_info_sz, (void *)info_buf);
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002701 if (ret)
Shashank Mittal162244e2011-08-08 19:01:25 -07002702 {
2703 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
Sridhar Parasurame1a97922015-07-27 15:31:22 -07002704 ASSERT(0);
Shashank Mittal162244e2011-08-08 19:01:25 -07002705 }
2706}
2707
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002708void read_device_info_mmc(struct device_info *info)
Shashank Mittal162244e2011-08-08 19:01:25 -07002709{
Shashank Mittal162244e2011-08-08 19:01:25 -07002710 unsigned long long ptn = 0;
2711 unsigned long long size;
2712 int index = INVALID_PTN;
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002713 uint32_t ret = 0;
Mayank Groverddd348d2018-01-23 14:07:09 +05302714 uint32_t device_info_sz = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07002715
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002716 if ((index = partition_get_index("devinfo")) < 0)
2717 {
2718 devinfo_present = false;
2719 index = partition_get_index("aboot");
2720 }
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07002721
Shashank Mittal162244e2011-08-08 19:01:25 -07002722 ptn = partition_get_offset(index);
2723 if(ptn == 0)
2724 {
2725 return;
2726 }
2727
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07002728 mmc_set_lun(partition_get_lun(index));
2729
Shashank Mittal162244e2011-08-08 19:01:25 -07002730 size = partition_get_size(index);
2731
Mayank Groverddd348d2018-01-23 14:07:09 +05302732 device_info_sz = ROUND_TO_PAGE(sizeof(struct device_info),
2733 mmc_blocksize_mask);
2734 if (device_info_sz == UINT_MAX)
2735 {
2736 dprintf(CRITICAL, "ERROR: Incorrect blocksize of card\n");
2737 return;
2738 }
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07002739
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002740 if (devinfo_present)
Mayank Groverddd348d2018-01-23 14:07:09 +05302741 ret = mmc_read(ptn, (void *)info_buf, device_info_sz);
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002742 else
Mayank Groverddd348d2018-01-23 14:07:09 +05302743 ret = mmc_read((ptn + size - device_info_sz), (void *)info_buf, device_info_sz);
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002744 if (ret)
Shashank Mittal162244e2011-08-08 19:01:25 -07002745 {
2746 dprintf(CRITICAL, "ERROR: Cannot read device info\n");
Sridhar Parasurame1a97922015-07-27 15:31:22 -07002747 ASSERT(0);
Shashank Mittal162244e2011-08-08 19:01:25 -07002748 }
Shashank Mittal162244e2011-08-08 19:01:25 -07002749}
2750
tracychui9d60fc52020-06-05 17:31:38 +08002751#if defined(ENABLE_PRODINFO_ACCESS)
2752void write_prod_info_mmc(prod_info *dev)
2753{
2754 unsigned long long ptn = 0;
2755 unsigned long long size;
2756 int index = INVALID_PTN;
2757 uint32_t blocksize;
2758 uint8_t lun = 0;
2759 uint32_t ret = 0;
2760
2761 if (prodinfo_present)
2762 index = partition_get_index("prodinfo");
2763 else
2764 index = partition_get_index("aboot");
2765
2766 ptn = partition_get_offset(index);
2767 if(ptn == 0)
2768 {
2769 return;
2770 }
2771
2772 lun = partition_get_lun(index);
2773 mmc_set_lun(lun);
2774
2775 size = partition_get_size(index);
2776
2777 blocksize = mmc_get_device_blocksize();
2778
2779 if (prodinfo_present)
2780 ret = mmc_write(ptn, blocksize, (void *)prodinfo_buf);
2781 else
2782 ret = mmc_write((ptn + size - blocksize), blocksize, (void *)prodinfo_buf);
2783 if (ret)
2784 {
2785 dprintf(CRITICAL, "ERROR: Cannot write prod info\n");
2786 ASSERT(0);
2787 }
2788}
2789
2790void read_prod_info_mmc(struct prod_info *info)
2791{
2792 unsigned long long ptn = 0;
2793 unsigned long long size;
2794 int index = INVALID_PTN;
2795 uint32_t blocksize;
2796 uint32_t ret = 0;
2797
2798 if ((index = partition_get_index("prodinfo")) < 0)
2799 {
2800 prodinfo_present = false;
2801 index = partition_get_index("aboot");
2802 }
2803
2804 ptn = partition_get_offset(index);
2805 if(ptn == 0)
2806 {
2807 return;
2808 }
2809
2810 mmc_set_lun(partition_get_lun(index));
2811
2812 size = partition_get_size(index);
2813
2814 blocksize = mmc_get_device_blocksize();
2815
2816 if (prodinfo_present)
2817 ret = mmc_read(ptn, (void *)prodinfo_buf, blocksize);
2818 else
2819 ret = mmc_read((ptn + size - blocksize), (void *)prodinfo_buf, blocksize);
2820 if (ret)
2821 {
2822 dprintf(CRITICAL, "ERROR: Cannot read prod info\n");
2823 ASSERT(0);
2824 }
2825}
2826#endif
2827
Shashank Mittal162244e2011-08-08 19:01:25 -07002828void write_device_info_flash(device_info *dev)
2829{
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002830 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
Shashank Mittal162244e2011-08-08 19:01:25 -07002831 struct ptentry *ptn;
2832 struct ptable *ptable;
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002833 if(info == NULL)
2834 {
2835 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
2836 ASSERT(0);
2837 }
2838 info_buf = info;
Shashank Mittal162244e2011-08-08 19:01:25 -07002839 ptable = flash_get_ptable();
2840 if (ptable == NULL)
2841 {
2842 dprintf(CRITICAL, "ERROR: Partition table not found\n");
2843 return;
2844 }
2845
2846 ptn = ptable_find(ptable, "devinfo");
2847 if (ptn == NULL)
2848 {
Smita Ghosh670c8b82015-05-07 09:30:03 -07002849 dprintf(CRITICAL, "ERROR: No devinfo partition found\n");
Shashank Mittal162244e2011-08-08 19:01:25 -07002850 return;
2851 }
2852
Mayank Groverdedbc892017-10-24 13:41:34 +05302853 memset(info, 0, BOOT_IMG_MAX_PAGE_SIZE);
Shashank Mittal162244e2011-08-08 19:01:25 -07002854 memcpy(info, dev, sizeof(device_info));
2855
2856 if (flash_write(ptn, 0, (void *)info_buf, page_size))
2857 {
2858 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
2859 return;
2860 }
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002861 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07002862}
2863
vijay kumarc65876c2015-04-24 13:29:16 +05302864static int read_allow_oem_unlock(device_info *dev)
2865{
vijay kumarc65876c2015-04-24 13:29:16 +05302866 unsigned offset;
2867 int index;
2868 unsigned long long ptn;
2869 unsigned long long ptn_size;
2870 unsigned blocksize = mmc_get_device_blocksize();
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07002871 STACKBUF_DMA_ALIGN(buf, blocksize);
vijay kumarc65876c2015-04-24 13:29:16 +05302872
vijay kumarca2e6812015-07-08 20:28:25 +05302873 index = partition_get_index(frp_ptns[0]);
vijay kumarc65876c2015-04-24 13:29:16 +05302874 if (index == INVALID_PTN)
2875 {
vijay kumarca2e6812015-07-08 20:28:25 +05302876 index = partition_get_index(frp_ptns[1]);
2877 if (index == INVALID_PTN)
2878 {
2879 dprintf(CRITICAL, "Neither '%s' nor '%s' partition found\n", frp_ptns[0],frp_ptns[1]);
2880 return -1;
2881 }
vijay kumarc65876c2015-04-24 13:29:16 +05302882 }
2883
2884 ptn = partition_get_offset(index);
2885 ptn_size = partition_get_size(index);
2886 offset = ptn_size - blocksize;
2887
Mayank Grover48860402016-11-29 12:34:53 +05302888 /* Set Lun for partition */
2889 mmc_set_lun(partition_get_lun(index));
2890
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07002891 if (mmc_read(ptn + offset, (void *)buf, blocksize))
vijay kumarc65876c2015-04-24 13:29:16 +05302892 {
2893 dprintf(CRITICAL, "Reading MMC failed\n");
2894 return -1;
2895 }
2896
2897 /*is_allow_unlock is a bool value stored at the LSB of last byte*/
2898 is_allow_unlock = buf[blocksize-1] & 0x01;
2899 return 0;
2900}
2901
2902static int write_allow_oem_unlock(bool allow_unlock)
2903{
vijay kumarc65876c2015-04-24 13:29:16 +05302904 unsigned offset;
vijay kumarc65876c2015-04-24 13:29:16 +05302905 int index;
2906 unsigned long long ptn;
2907 unsigned long long ptn_size;
2908 unsigned blocksize = mmc_get_device_blocksize();
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07002909 STACKBUF_DMA_ALIGN(buf, blocksize);
vijay kumarc65876c2015-04-24 13:29:16 +05302910
vijay kumarca2e6812015-07-08 20:28:25 +05302911 index = partition_get_index(frp_ptns[0]);
vijay kumarc65876c2015-04-24 13:29:16 +05302912 if (index == INVALID_PTN)
2913 {
vijay kumarca2e6812015-07-08 20:28:25 +05302914 index = partition_get_index(frp_ptns[1]);
2915 if (index == INVALID_PTN)
2916 {
2917 dprintf(CRITICAL, "Neither '%s' nor '%s' partition found\n", frp_ptns[0],frp_ptns[1]);
2918 return -1;
2919 }
vijay kumarc65876c2015-04-24 13:29:16 +05302920 }
2921
2922 ptn = partition_get_offset(index);
2923 ptn_size = partition_get_size(index);
2924 offset = ptn_size - blocksize;
Mayank Grover48860402016-11-29 12:34:53 +05302925 mmc_set_lun(partition_get_lun(index));
vijay kumarc65876c2015-04-24 13:29:16 +05302926
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07002927 if (mmc_read(ptn + offset, (void *)buf, blocksize))
vijay kumarc65876c2015-04-24 13:29:16 +05302928 {
2929 dprintf(CRITICAL, "Reading MMC failed\n");
2930 return -1;
2931 }
2932
2933 /*is_allow_unlock is a bool value stored at the LSB of last byte*/
2934 buf[blocksize-1] = allow_unlock;
2935 if (mmc_write(ptn + offset, blocksize, buf))
2936 {
2937 dprintf(CRITICAL, "Writing MMC failed\n");
2938 return -1;
2939 }
2940
2941 return 0;
2942}
2943
Shashank Mittal162244e2011-08-08 19:01:25 -07002944void read_device_info_flash(device_info *dev)
2945{
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002946 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
Shashank Mittal162244e2011-08-08 19:01:25 -07002947 struct ptentry *ptn;
2948 struct ptable *ptable;
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002949 if(info == NULL)
2950 {
2951 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
2952 ASSERT(0);
2953 }
2954 info_buf = info;
Shashank Mittal162244e2011-08-08 19:01:25 -07002955 ptable = flash_get_ptable();
2956 if (ptable == NULL)
2957 {
2958 dprintf(CRITICAL, "ERROR: Partition table not found\n");
2959 return;
2960 }
2961
2962 ptn = ptable_find(ptable, "devinfo");
2963 if (ptn == NULL)
2964 {
Smita Ghosh670c8b82015-05-07 09:30:03 -07002965 dprintf(CRITICAL, "ERROR: No devinfo partition found\n");
Shashank Mittal162244e2011-08-08 19:01:25 -07002966 return;
2967 }
2968
2969 if (flash_read(ptn, 0, (void *)info_buf, page_size))
2970 {
2971 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
2972 return;
2973 }
2974
2975 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
2976 {
Shashank Mittal162244e2011-08-08 19:01:25 -07002977 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
2978 info->is_unlocked = 0;
Shashank Mittala0032282011-08-26 14:50:11 -07002979 info->is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07002980 write_device_info_flash(info);
2981 }
2982 memcpy(dev, info, sizeof(device_info));
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002983 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07002984}
2985
2986void write_device_info(device_info *dev)
2987{
2988 if(target_is_emmc_boot())
2989 {
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002990 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
2991 if(info == NULL)
2992 {
2993 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
2994 ASSERT(0);
2995 }
2996 info_buf = info;
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002997 memcpy(info, dev, sizeof(struct device_info));
2998
2999#if USE_RPMB_FOR_DEVINFO
Mayank Grover912eaa62017-10-26 12:08:53 +05303000 if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05303001 is_secure_boot_enable()) {
3002 if((write_device_info_rpmb((void*) info, PAGE_SIZE)) < 0)
3003 ASSERT(0);
Sridhar Parasurame1a97922015-07-27 15:31:22 -07003004 }
Channagoud Kadabic80cb492015-04-28 16:08:28 -07003005 else
3006 write_device_info_mmc(info);
Channagoud Kadabi036c6052015-02-09 15:19:59 -08003007#else
3008 write_device_info_mmc(info);
3009#endif
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07003010 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07003011 }
3012 else
3013 {
3014 write_device_info_flash(dev);
3015 }
3016}
3017
Monika Singh94316462018-03-15 18:39:01 +05303018int read_rollback_index(uint32_t loc, uint64_t *roll_back_index)
3019{
3020 if (!devinfo_present) {
3021 dprintf(CRITICAL, "DeviceInfo not initalized \n");
3022 return -EINVAL;
3023 }
3024 if (loc >= ARRAY_SIZE(device.rollback_index)) {
3025 dprintf(CRITICAL, "%s() Loc out of range index: %d, array len: %d\n",
3026 __func__, loc, ARRAY_SIZE(device.rollback_index));
3027 ASSERT(0);
3028 }
3029
3030 *roll_back_index = device.rollback_index[loc];
3031 return 0;
3032}
3033
3034int write_rollback_index(uint32_t loc, uint64_t roll_back_index)
3035{
3036 if (!devinfo_present) {
3037 dprintf(CRITICAL, "DeviceInfo not initalized \n");
3038 return -EINVAL;
3039 }
3040 if (loc >= ARRAY_SIZE(device.rollback_index)) {
3041 dprintf(CRITICAL, "%s() Loc out of range index: %d, array len: %d\n",
3042 __func__, loc, ARRAY_SIZE(device.rollback_index));
3043 ASSERT(0);
3044 }
3045
3046 device.rollback_index[loc] = roll_back_index;
3047 write_device_info(&device);
3048 return 0;
3049}
3050
Monika Singhb0fad822018-03-15 18:50:55 +05303051int store_userkey(uint8_t *user_key, uint32_t user_key_size)
3052{
3053 if (!devinfo_present) {
3054 dprintf(CRITICAL, "DeviceInfo not initalized \n");
3055 return -EINVAL;
3056 }
3057
3058 if (user_key_size > ARRAY_SIZE(device.user_public_key)) {
3059 dprintf(CRITICAL, "StoreUserKey, UserKeySize too large!\n");
3060 return -ENODEV;
3061 }
3062
3063 memcpy(device.user_public_key, user_key, user_key_size);
3064 device.user_public_key_length = user_key_size;
3065 write_device_info(&device);
3066 return 0;
3067}
3068
3069int erase_userkey()
3070{
3071 if (!devinfo_present) {
3072 dprintf(CRITICAL, "DeviceInfo not initalized \n");
3073 return -EINVAL;
3074 }
3075 memset(device.user_public_key, 0, ARRAY_SIZE(device.user_public_key));
3076 device.user_public_key_length = 0;
3077 write_device_info(&device);
3078 return 0;
3079}
3080
3081int get_userkey(uint8_t **user_key, uint32_t *user_key_size)
3082{
3083 if (!devinfo_present) {
3084 dprintf(CRITICAL, "DeviceInfo not initalized \n");
3085 return -EINVAL;
3086 }
3087 *user_key = device.user_public_key;
3088 *user_key_size = device.user_public_key_length;
3089 return 0;
3090}
3091
Shashank Mittal162244e2011-08-08 19:01:25 -07003092void read_device_info(device_info *dev)
3093{
3094 if(target_is_emmc_boot())
3095 {
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07003096 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
3097 if(info == NULL)
3098 {
3099 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
3100 ASSERT(0);
3101 }
3102 info_buf = info;
Channagoud Kadabi036c6052015-02-09 15:19:59 -08003103
3104#if USE_RPMB_FOR_DEVINFO
Mayank Grover912eaa62017-10-26 12:08:53 +05303105 if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05303106 is_secure_boot_enable()) {
3107 if((read_device_info_rpmb((void*) info, PAGE_SIZE)) < 0)
3108 ASSERT(0);
Sridhar Parasurame1a97922015-07-27 15:31:22 -07003109 }
Channagoud Kadabic80cb492015-04-28 16:08:28 -07003110 else
3111 read_device_info_mmc(info);
Channagoud Kadabi036c6052015-02-09 15:19:59 -08003112#else
3113 read_device_info_mmc(info);
3114#endif
3115
3116 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
3117 {
3118 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
lijuang511a2b52015-08-14 20:50:51 +08003119 if (is_secure_boot_enable()) {
Channagoud Kadabi05f78ba2015-07-06 11:58:14 -07003120 info->is_unlocked = 0;
Monika Singh292b3e92018-03-17 22:40:23 +05303121#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover912eaa62017-10-26 12:08:53 +05303122 if (VB_M <= target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +05303123 info->is_unlock_critical = 0;
Parth Dixitddbc7352015-10-18 03:13:31 +05303124#endif
lijuang511a2b52015-08-14 20:50:51 +08003125 } else {
Channagoud Kadabi2fda4092015-07-07 13:34:11 -07003126 info->is_unlocked = 1;
Monika Singh292b3e92018-03-17 22:40:23 +05303127#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover912eaa62017-10-26 12:08:53 +05303128 if (VB_M <= target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +05303129 info->is_unlock_critical = 1;
Parth Dixitddbc7352015-10-18 03:13:31 +05303130#endif
lijuang511a2b52015-08-14 20:50:51 +08003131 }
Channagoud Kadabi036c6052015-02-09 15:19:59 -08003132 info->is_tampered = 0;
3133 info->charger_screen_enabled = 0;
Monika Singh292b3e92018-03-17 22:40:23 +05303134#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover912eaa62017-10-26 12:08:53 +05303135 if (VB_M <= target_get_vb_version())
Monika Singh1c71a1c2019-12-03 12:12:48 +05303136 {
Mayank Grover889be1b2017-09-12 20:12:23 +05303137 info->verity_mode = 1; //enforcing by default
Monika Singh1c71a1c2019-12-03 12:12:48 +05303138 info->user_public_key_length = 0;
3139 memset(info->rollback_index, 0, sizeof(info->rollback_index));
3140 memset(info->user_public_key, 0, sizeof(info->user_public_key));
3141 }
Parth Dixitddbc7352015-10-18 03:13:31 +05303142#endif
Channagoud Kadabi036c6052015-02-09 15:19:59 -08003143 write_device_info(info);
3144 }
jessicatseng8e7644c2020-04-22 11:13:20 +08003145
3146 info->charger_screen_enabled = 1;
3147 write_device_info(info);
3148
Channagoud Kadabi036c6052015-02-09 15:19:59 -08003149 memcpy(dev, info, sizeof(device_info));
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07003150 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07003151 }
3152 else
3153 {
3154 read_device_info_flash(dev);
3155 }
3156}
tracychui9d60fc52020-06-05 17:31:38 +08003157#if defined(ENABLE_PRODINFO_ACCESS)
3158void write_prod_info(prod_info *dev)
3159{
3160 if(target_is_emmc_boot())
3161 {
3162 struct prod_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
3163 if(info == NULL)
3164 {
3165 dprintf(CRITICAL, "Failed to allocate memory for prod info struct\n");
3166 ASSERT(0);
3167 }
3168 prodinfo_buf = info;
3169 memcpy(info, dev, sizeof(struct prod_info));
3170
3171 write_prod_info_mmc(info);
3172 free(info);
3173 }
3174}
3175
3176void read_prod_info(prod_info *dev)
3177{
3178 if(target_is_emmc_boot())
3179 {
3180 struct prod_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
3181 if(info == NULL)
3182 {
3183 dprintf(CRITICAL, "Failed to allocate memory for prod info struct\n");
3184 ASSERT(0);
3185 }
3186 prodinfo_buf = info;
3187
3188 read_prod_info_mmc(info);
3189
3190 if (memcmp(info->magic, PRODINFO_MAGIC, PRODINFO_MAGIC_SIZE))
3191 {
3192 memcpy(info->magic, PRODINFO_MAGIC, PRODINFO_MAGIC_SIZE);
3193 memcpy(info->isn, "No_Serial_Number", PRODINFO_MAX_ISN_LEN);
3194 memcpy(info->ssn, "No_Custer_Serial_Number", PRODINFO_MAX_SSN_LEN);
3195 info->is_adb_enabled = 0;
3196 write_prod_info(info);
3197 }
3198 memcpy(dev, info, sizeof(prod_info));
3199 free(info);
3200 }
3201}
3202#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07003203
3204void reset_device_info()
3205{
3206 dprintf(ALWAYS, "reset_device_info called.");
Shashank Mittala0032282011-08-26 14:50:11 -07003207 device.is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07003208 write_device_info(&device);
3209}
3210
3211void set_device_root()
3212{
3213 dprintf(ALWAYS, "set_device_root called.");
Shashank Mittala0032282011-08-26 14:50:11 -07003214 device.is_tampered = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -07003215 write_device_info(&device);
3216}
3217
lijuang4ece1e72015-08-14 21:02:36 +08003218/* set device unlock value
3219 * Must check FRP before call this function
3220 * Need to wipe data when unlock status changed
3221 * type 0: oem unlock
3222 * type 1: unlock critical
3223 * status 0: unlock as false
3224 * status 1: lock as true
3225 */
3226void set_device_unlock_value(int type, bool status)
lijuang21f12f52015-08-22 16:22:19 +08003227{
lijuang4ece1e72015-08-14 21:02:36 +08003228 if (type == UNLOCK)
3229 device.is_unlocked = status;
Monika Singh292b3e92018-03-17 22:40:23 +05303230#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover912eaa62017-10-26 12:08:53 +05303231 else if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05303232 type == UNLOCK_CRITICAL)
3233 device.is_unlock_critical = status;
Parth Dixitddbc7352015-10-18 03:13:31 +05303234#endif
lijuang4ece1e72015-08-14 21:02:36 +08003235 write_device_info(&device);
3236}
3237
3238static void set_device_unlock(int type, bool status)
3239{
3240 int is_unlocked = -1;
3241 char response[MAX_RSP_SIZE];
3242
3243 /* check device unlock status if it is as expected */
3244 if (type == UNLOCK)
3245 is_unlocked = device.is_unlocked;
Monika Singh292b3e92018-03-17 22:40:23 +05303246#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover912eaa62017-10-26 12:08:53 +05303247 if(VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05303248 type == UNLOCK_CRITICAL)
3249 {
3250 is_unlocked = device.is_unlock_critical;
3251 }
Parth Dixitddbc7352015-10-18 03:13:31 +05303252#endif
lijuang4ece1e72015-08-14 21:02:36 +08003253 if (is_unlocked == status) {
3254 snprintf(response, sizeof(response), "\tDevice already : %s", (status ? "unlocked!" : "locked!"));
3255 fastboot_info(response);
3256 fastboot_okay("");
3257 return;
3258 }
3259
3260 /* status is true, it means to unlock device */
lijuang07c5d6e2018-04-23 18:32:13 +08003261 if (status && !is_allow_unlock) {
3262 fastboot_fail("oem unlock is not allowed");
3263 return;
3264 }
lijuang21f12f52015-08-22 16:22:19 +08003265
ericlin4e397c42020-04-29 14:14:24 +08003266 if(target_build_variant_user()){
lijuang4ece1e72015-08-14 21:02:36 +08003267#if FBCON_DISPLAY_MSG
lijuang07c5d6e2018-04-23 18:32:13 +08003268 display_unlock_menu(type, status);
3269 fastboot_okay("");
3270 return;
lijuang4ece1e72015-08-14 21:02:36 +08003271#else
lijuang07c5d6e2018-04-23 18:32:13 +08003272 if (status && type == UNLOCK) {
3273 fastboot_fail("Need wipe userdata. Do 'fastboot oem unlock-go'");
3274 return;
lijuang21f12f52015-08-22 16:22:19 +08003275 }
lijuang07c5d6e2018-04-23 18:32:13 +08003276#endif
ericlin4e397c42020-04-29 14:14:24 +08003277 }
lijuang4ece1e72015-08-14 21:02:36 +08003278
3279 set_device_unlock_value(type, status);
3280
3281 /* wipe data */
3282 struct recovery_message msg;
Kishor PK60a68212017-05-08 16:55:57 +05303283 memset(&msg, 0, sizeof(msg));
lijuang4ece1e72015-08-14 21:02:36 +08003284 snprintf(msg.recovery, sizeof(msg.recovery), "recovery\n--wipe_data");
3285 write_misc(0, &msg, sizeof(msg));
3286
3287 fastboot_okay("");
ericlin4e397c42020-04-29 14:14:24 +08003288 //reboot_device(RECOVERY_MODE);
lijuang21f12f52015-08-22 16:22:19 +08003289}
3290
lijuang511a2b52015-08-14 20:50:51 +08003291static bool critical_flash_allowed(const char * entry)
3292{
3293 uint32_t i = 0;
3294 if (entry == NULL)
3295 return false;
3296
3297 for (i = 0; i < ARRAY_SIZE(critical_flash_allowed_ptn); i++) {
3298 if(!strcmp(entry, critical_flash_allowed_ptn[i]))
3299 return true;
3300 }
3301 return false;
Matthew Qin271927e2015-03-31 22:07:07 -04003302}
3303
3304#if DEVICE_TREE
Amol Jadicb524072012-08-09 16:40:18 -07003305int copy_dtb(uint8_t *boot_image_start, unsigned int scratch_offset)
3306{
3307 uint32 dt_image_offset = 0;
Amol Jadicb524072012-08-09 16:40:18 -07003308 uint32_t n;
Monika Singh292b3e92018-03-17 22:40:23 +05303309 struct dt_table *table = NULL;
Amol Jadicb524072012-08-09 16:40:18 -07003310 struct dt_entry dt_entry;
Monika Singh292b3e92018-03-17 22:40:23 +05303311 uint32_t dt_hdr_size = 0;
Amol Jadicb524072012-08-09 16:40:18 -07003312 unsigned int compressed_size = 0;
3313 unsigned int dtb_size = 0;
3314 unsigned int out_avai_len = 0;
3315 unsigned char *out_addr = NULL;
3316 unsigned char *best_match_dt_addr = NULL;
3317 int rc;
3318
Mayank Grovere1ab96c2018-09-04 20:31:31 +05303319 boot_img_hdr *hdr = (boot_img_hdr *) (boot_image_start);
Amol Jadicb524072012-08-09 16:40:18 -07003320
Parth Dixit4097b622016-03-15 14:42:27 +05303321#ifndef OSVERSION_IN_BOOTIMAGE
3322 dt_size = hdr->dt_size;
3323#endif
3324
3325 if(dt_size != 0) {
Amol Jadicb524072012-08-09 16:40:18 -07003326 /* add kernel offset */
3327 dt_image_offset += page_size;
3328 n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
3329 dt_image_offset += n;
3330
3331 /* add ramdisk offset */
3332 n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
3333 dt_image_offset += n;
3334
3335 /* add second offset */
3336 if(hdr->second_size != 0) {
3337 n = ROUND_TO_PAGE(hdr->second_size, page_mask);
3338 dt_image_offset += n;
3339 }
3340
3341 /* offset now point to start of dt.img */
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07003342 table = (struct dt_table*)(boot_image_start + dt_image_offset);
Amol Jadicb524072012-08-09 16:40:18 -07003343
Deepa Dinamani19648b42013-09-05 17:05:55 -07003344 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Amol Jadicb524072012-08-09 16:40:18 -07003345 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
3346 return -1;
3347 }
P.V. Phani Kumar82916762016-03-09 09:20:19 +05303348
3349 /* Its Error if, dt_hdr_size (table->num_entries * dt_entry size + Dev_Tree Header)
3350 goes beyound hdr->dt_size*/
Parth Dixit4097b622016-03-15 14:42:27 +05303351 if (dt_hdr_size > ROUND_TO_PAGE(dt_size,hdr->page_size)) {
P.V. Phani Kumar82916762016-03-09 09:20:19 +05303352 dprintf(CRITICAL, "ERROR: Invalid Device Tree size \n");
3353 return -1;
3354 }
3355
Joel Kingaa335dc2013-06-03 16:11:08 -07003356 /* Find index of device tree within device tree table */
3357 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Amol Jadicb524072012-08-09 16:40:18 -07003358 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
3359 return -1;
3360 }
3361
Matthew Qin271927e2015-03-31 22:07:07 -04003362 best_match_dt_addr = (unsigned char *)boot_image_start + dt_image_offset + dt_entry.offset;
3363 if (is_gzip_package(best_match_dt_addr, dt_entry.size))
3364 {
3365 out_addr = (unsigned char *)target_get_scratch_address() + scratch_offset;
3366 out_avai_len = target_get_max_flash_size() - scratch_offset;
Matthew Qin0b15b322015-05-19 05:20:54 -04003367 dprintf(INFO, "decompressing dtb: start\n");
Matthew Qin271927e2015-03-31 22:07:07 -04003368 rc = decompress(best_match_dt_addr,
3369 dt_entry.size, out_addr, out_avai_len,
3370 &compressed_size, &dtb_size);
3371 if (rc)
3372 {
Matthew Qin0b15b322015-05-19 05:20:54 -04003373 dprintf(CRITICAL, "decompressing dtb failed!!!\n");
Matthew Qin271927e2015-03-31 22:07:07 -04003374 ASSERT(0);
3375 }
3376
Matthew Qin0b15b322015-05-19 05:20:54 -04003377 dprintf(INFO, "decompressing dtb: done\n");
Matthew Qin271927e2015-03-31 22:07:07 -04003378 best_match_dt_addr = out_addr;
3379 } else {
3380 dtb_size = dt_entry.size;
3381 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07003382 /* Validate and Read device device tree in the "tags_add */
Mayank Grovere559cec2017-10-17 15:12:03 +05303383 if (check_aboot_addr_range_overlap(hdr->tags_addr, dtb_size) ||
3384 check_ddr_addr_range_bound(hdr->tags_addr, dtb_size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07003385 {
Mayank Grovere559cec2017-10-17 15:12:03 +05303386 dprintf(CRITICAL, "Device tree addresses are not valid.\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07003387 return -1;
3388 }
3389
Amol Jadicb524072012-08-09 16:40:18 -07003390 /* Read device device tree in the "tags_add */
Matthew Qin271927e2015-03-31 22:07:07 -04003391 memmove((void*) hdr->tags_addr, (void *)best_match_dt_addr, dtb_size);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07003392 } else
3393 return -1;
Amol Jadicb524072012-08-09 16:40:18 -07003394
3395 /* Everything looks fine. Return success. */
3396 return 0;
3397}
3398#endif
3399
Brian Swetland9c4c0752009-01-25 16:23:50 -08003400void cmd_boot(const char *arg, void *data, unsigned sz)
3401{
3402 unsigned kernel_actual;
3403 unsigned ramdisk_actual;
Kishor PKd8ddcad2017-07-27 13:53:57 +05303404 unsigned second_actual;
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07003405 uint32_t image_actual;
3406 uint32_t dt_actual = 0;
Mayank Grovere1ab96c2018-09-04 20:31:31 +05303407 boot_img_hdr *hdr = NULL;
Matthew Qinbb7923d2015-02-09 10:56:09 +08003408 struct kernel64_hdr *kptr = NULL;
Brian Swetland9c4c0752009-01-25 16:23:50 -08003409 char *ptr = ((char*) data);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07003410 int ret = 0;
3411 uint8_t dtb_copied = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08003412 unsigned int out_len = 0;
3413 unsigned int out_avai_len = 0;
3414 unsigned char *out_addr = NULL;
3415 uint32_t dtb_offset = 0;
3416 unsigned char *kernel_start_addr = NULL;
3417 unsigned int kernel_size = 0;
Matthew Qin271927e2015-03-31 22:07:07 -04003418 unsigned int scratch_offset = 0;
Saranya Chidurab4933332018-10-15 17:30:06 +05303419#if VERIFIED_BOOT_2
3420 void *dtbo_image_buf = NULL;
3421 uint32_t dtbo_image_sz = 0;
3422 void *vbmeta_image_buf = NULL;
3423 uint32_t vbmeta_image_sz = 0;
3424#endif
Monika Singh292b3e92018-03-17 22:40:23 +05303425#if !VERIFIED_BOOT_2
3426 uint32_t sig_actual = 0;
3427 uint32_t sig_size = 0;
3428#ifdef MDTP_SUPPORT
3429 static bool is_mdtp_activated = 0;
3430#endif /* MDTP_SUPPORT */
3431#endif
Matthew Qinbb7923d2015-02-09 10:56:09 +08003432
lijuang2008ff22016-03-07 17:56:27 +08003433#if FBCON_DISPLAY_MSG
3434 /* Exit keys' detection thread firstly */
3435 exit_menu_keys_detection();
3436#endif
3437
Monika Singh292b3e92018-03-17 22:40:23 +05303438#if VERIFIED_BOOT || VERIFIED_BOOT_2
Channagoud Kadabi6d5375e2015-06-23 17:15:42 -07003439 if(target_build_variant_user() && !device.is_unlocked)
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003440 {
3441 fastboot_fail("unlock device to use this command");
lijuang2008ff22016-03-07 17:56:27 +08003442 goto boot_failed;
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003443 }
3444#endif
3445
Brian Swetland9c4c0752009-01-25 16:23:50 -08003446 if (sz < sizeof(hdr)) {
3447 fastboot_fail("invalid bootimage header");
lijuang2008ff22016-03-07 17:56:27 +08003448 goto boot_failed;
Brian Swetland9c4c0752009-01-25 16:23:50 -08003449 }
3450
Mayank Grovere1ab96c2018-09-04 20:31:31 +05303451 hdr = (boot_img_hdr *)data;
Brian Swetland9c4c0752009-01-25 16:23:50 -08003452
3453 /* ensure commandline is terminated */
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07003454 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -08003455
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07003456 if(target_is_emmc_boot() && hdr->page_size) {
3457 page_size = hdr->page_size;
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07003458 page_mask = page_size - 1;
3459 }
3460
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07003461 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
3462 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Kishor PKd8ddcad2017-07-27 13:53:57 +05303463 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07003464#if DEVICE_TREE
Parth Dixit4097b622016-03-15 14:42:27 +05303465#ifndef OSVERSION_IN_BOOTIMAGE
3466 dt_size = hdr->dt_size;
3467#endif
3468 dt_actual = ROUND_TO_PAGE(dt_size, page_mask);
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07003469#endif
3470
3471 image_actual = ADD_OF(page_size, kernel_actual);
3472 image_actual = ADD_OF(image_actual, ramdisk_actual);
Kishor PKd8ddcad2017-07-27 13:53:57 +05303473 image_actual = ADD_OF(image_actual, second_actual);
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07003474 image_actual = ADD_OF(image_actual, dt_actual);
3475
Kishor PK5134b332017-05-09 17:50:08 +05303476 /* Checking to prevent oob access in read_der_message_length */
3477 if (image_actual > sz) {
3478 fastboot_fail("bootimage header fields are invalid");
3479 goto boot_failed;
3480 }
Saranya Chidurab4933332018-10-15 17:30:06 +05303481
Monika Singh292b3e92018-03-17 22:40:23 +05303482#if VERIFIED_BOOT_2
Mayank Grover1ceaee22019-04-01 17:54:32 +05303483 /* Pass size of boot partition, as imgsize, to avoid
3484 read fewer bytes error */
3485 image_actual = partition_get_size(partition_get_index("boot"));
Saranya Chidurab4933332018-10-15 17:30:06 +05303486
3487 /* load and validate dtbo partition */
3488 load_validate_dtbo_image(&dtbo_image_buf, &dtbo_image_sz);
3489
3490 /* load vbmeta partition */
3491 load_vbmeta_image(&vbmeta_image_buf, &vbmeta_image_sz);
3492
Monika Singh292b3e92018-03-17 22:40:23 +05303493 memset(&info, 0, sizeof(bootinfo));
Saranya Chidurab4933332018-10-15 17:30:06 +05303494
3495 info.images[IMG_BOOT].image_buffer = SUB_SALT_BUFF_OFFSET(data);
3496 info.images[IMG_BOOT].imgsize = image_actual;
3497 info.images[IMG_BOOT].name = "boot";
3498 ++info.num_loaded_images;
3499
3500 /* Pass loaded dtbo image */
3501 if (dtbo_image_buf != NULL) {
3502 info.images[IMG_DTBO].image_buffer = SUB_SALT_BUFF_OFFSET(dtbo_image_buf);
3503 info.images[IMG_DTBO].imgsize = dtbo_image_sz;
3504 info.images[IMG_DTBO].name = "dtbo";
3505 ++info.num_loaded_images;
3506 }
3507
3508 /* Pass loaded vbmeta image */
3509 if (vbmeta_image_buf != NULL) {
3510 info.images[IMG_VBMETA].image_buffer = vbmeta_image_buf;
3511 info.images[IMG_VBMETA].imgsize = vbmeta_image_sz;
3512 info.images[IMG_VBMETA].name = "vbmeta";
3513 ++info.num_loaded_images;
3514 }
3515
Monika Singh292b3e92018-03-17 22:40:23 +05303516 info.multi_slot_boot = partition_multislot_is_supported();
3517 if (load_image_and_auth(&info))
3518 goto boot_failed;
3519 vbcmdline = info.vbcmdline;
Saranya Chidurab4933332018-10-15 17:30:06 +05303520
3521 /* Free the buffer allocated to vbmeta post verification */
3522 if (vbmeta_image_buf != NULL) {
3523 free(vbmeta_image_buf);
3524 --info.num_loaded_images;
3525 }
Monika Singh292b3e92018-03-17 22:40:23 +05303526#else
Kishor PK5134b332017-05-09 17:50:08 +05303527 sig_size = sz - image_actual;
3528
P.V. Phani Kumareecfd822016-03-09 20:09:03 +05303529 if (target_use_signed_kernel() && (!device.is_unlocked)) {
Monika Singh292b3e92018-03-17 22:40:23 +05303530 unsigned chk;
P.V. Phani Kumareecfd822016-03-09 20:09:03 +05303531 /* Calculate the signature length from boot image */
3532 sig_actual = read_der_message_length(
Kishor PK5134b332017-05-09 17:50:08 +05303533 (unsigned char*)(data + image_actual), sig_size);
Monika Singh292b3e92018-03-17 22:40:23 +05303534 chk = ADD_OF(image_actual, sig_actual);
Channagoud Kadabi3d839012014-06-26 14:26:39 -07003535
Monika Singh292b3e92018-03-17 22:40:23 +05303536 if (chk > sz) {
Kishor PK5134b332017-05-09 17:50:08 +05303537 fastboot_fail("bootimage header fields are invalid");
3538 goto boot_failed;
3539 }
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07003540 }
3541
Sridhar Parasuram361e4cd2015-09-24 11:19:19 -07003542 // Initialize boot state before trying to verify boot.img
3543#if VERIFIED_BOOT
Channagoud Kadabi699466e2015-11-03 12:37:42 -08003544 boot_verifier_init();
Mayank Groverb337e932017-01-18 20:00:40 +05303545#endif
Channagoud Kadabibf6ce7d2015-09-17 13:25:35 -07003546 /* Handle overflow if the input image size is greater than
3547 * boot image buffer can hold
3548 */
Monika Singh292b3e92018-03-17 22:40:23 +05303549 if ((target_get_max_flash_size() - page_size) < image_actual)
Channagoud Kadabibf6ce7d2015-09-17 13:25:35 -07003550 {
3551 fastboot_fail("booimage: size is greater than boot image buffer can hold");
lijuang2008ff22016-03-07 17:56:27 +08003552 goto boot_failed;
Channagoud Kadabibf6ce7d2015-09-17 13:25:35 -07003553 }
Sridhar Parasuram361e4cd2015-09-24 11:19:19 -07003554
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07003555 /* Verify the boot image
3556 * device & page_size are initialized in aboot_init
3557 */
Sridhar Parasuram7ae9fbc2015-09-17 09:56:30 -07003558 if (target_use_signed_kernel() && (!device.is_unlocked)) {
Channagoud Kadabi3d839012014-06-26 14:26:39 -07003559 /* Pass size excluding signature size, otherwise we would try to
3560 * access signature beyond its length
3561 */
Monika Singh292b3e92018-03-17 22:40:23 +05303562 verify_signed_bootimg((uint32_t)data, image_actual);
Sridhar Parasuram7ae9fbc2015-09-17 09:56:30 -07003563 }
Amit Blay4aa292f2015-04-28 21:55:59 +03003564#ifdef MDTP_SUPPORT
3565 else
3566 {
Shay Nachmani062aa3f2015-05-17 17:28:44 +03003567 /* fastboot boot is not allowed when MDTP is activated */
Amit Blay4aa292f2015-04-28 21:55:59 +03003568 mdtp_ext_partition_verification_t ext_partition;
Amit Blay8a510302015-08-17 09:20:01 +03003569
3570 if (!is_mdtp_activated) {
3571 ext_partition.partition = MDTP_PARTITION_NONE;
3572 mdtp_fwlock_verify_lock(&ext_partition);
3573 }
Amit Blay4aa292f2015-04-28 21:55:59 +03003574 }
Shay Nachmani062aa3f2015-05-17 17:28:44 +03003575
Amir Kotzer7c768c02016-04-13 09:08:05 +03003576 /* If mdtp state cannot be validate, block fastboot boot*/
3577 if(mdtp_activated(&is_mdtp_activated)){
3578 dprintf(CRITICAL, "mdtp_activated cannot validate state.\n");
3579 dprintf(CRITICAL, "Can not proceed with fastboot boot command.\n");
3580 goto boot_failed;
3581 }
Shay Nachmani062aa3f2015-05-17 17:28:44 +03003582 if(is_mdtp_activated){
3583 dprintf(CRITICAL, "fastboot boot command is not available.\n");
lijuang2008ff22016-03-07 17:56:27 +08003584 goto boot_failed;
Shay Nachmani062aa3f2015-05-17 17:28:44 +03003585 }
Amit Blay4aa292f2015-04-28 21:55:59 +03003586#endif /* MDTP_SUPPORT */
Monika Singh292b3e92018-03-17 22:40:23 +05303587#endif /* VERIFIED_BOOT_2 else */
Amit Blay4aa292f2015-04-28 21:55:59 +03003588
Sridhar Parasuram361e4cd2015-09-24 11:19:19 -07003589#if VERIFIED_BOOT
Monika Singh292b3e92018-03-17 22:40:23 +05303590 if (VB_M == target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +05303591 {
3592 /* set boot and system versions. */
3593 set_os_version((unsigned char *)data);
3594 // send root of trust
3595 if(!send_rot_command((uint32_t)device.is_unlocked))
3596 ASSERT(0);
3597 }
Parth Dixitbc9b6492015-10-18 00:41:38 +05303598#endif
Channagoud Kadabia22144f2013-03-20 11:49:01 -07003599 /*
Matthew Qinbb7923d2015-02-09 10:56:09 +08003600 * Check if the kernel image is a gzip package. If yes, need to decompress it.
3601 * If not, continue booting.
3602 */
3603 if (is_gzip_package((unsigned char *)(data + page_size), hdr->kernel_size))
3604 {
3605 out_addr = (unsigned char *)target_get_scratch_address();
3606 out_addr = (unsigned char *)(out_addr + image_actual + page_size);
3607 out_avai_len = target_get_max_flash_size() - image_actual - page_size;
Saranya Chidurab4933332018-10-15 17:30:06 +05303608#if VERIFIED_BOOT_2
3609 if (dtbo_image_sz)
3610 out_avai_len -= DTBO_IMG_BUF;
3611#endif
Matthew Qin0b15b322015-05-19 05:20:54 -04003612 dprintf(INFO, "decompressing kernel image: start\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08003613 ret = decompress((unsigned char *)(ptr + page_size),
3614 hdr->kernel_size, out_addr, out_avai_len,
3615 &dtb_offset, &out_len);
3616 if (ret)
3617 {
Matthew Qin0b15b322015-05-19 05:20:54 -04003618 dprintf(CRITICAL, "decompressing image failed!!!\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08003619 ASSERT(0);
3620 }
3621
Matthew Qin0b15b322015-05-19 05:20:54 -04003622 dprintf(INFO, "decompressing kernel image: done\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08003623 kptr = (struct kernel64_hdr *)out_addr;
3624 kernel_start_addr = out_addr;
3625 kernel_size = out_len;
3626 } else {
3627 kptr = (struct kernel64_hdr*)((char *)data + page_size);
3628 kernel_start_addr = (unsigned char *)((char *)data + page_size);
3629 kernel_size = hdr->kernel_size;
3630 }
3631
3632 /*
Channagoud Kadabia22144f2013-03-20 11:49:01 -07003633 * Update the kernel/ramdisk/tags address if the boot image header
3634 * has default values, these default values come from mkbootimg when
3635 * the boot image is flashed using fastboot flash:raw
3636 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08003637 update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr));
Dima Zavin3cadfff2013-03-21 14:30:48 -07003638
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07003639 /* Get virtual addresses since the hdr saves physical addresses. */
3640 hdr->kernel_addr = VA(hdr->kernel_addr);
3641 hdr->ramdisk_addr = VA(hdr->ramdisk_addr);
3642 hdr->tags_addr = VA(hdr->tags_addr);
Brian Swetland9c4c0752009-01-25 16:23:50 -08003643
Matthew Qinbb7923d2015-02-09 10:56:09 +08003644 kernel_size = ROUND_TO_PAGE(kernel_size, page_mask);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07003645 /* Check if the addresses in the header are valid. */
Matthew Qinbb7923d2015-02-09 10:56:09 +08003646 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_size) ||
Mayank Grovere559cec2017-10-17 15:12:03 +05303647 check_ddr_addr_range_bound(hdr->kernel_addr, kernel_size) ||
3648 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual) ||
3649 check_ddr_addr_range_bound(hdr->ramdisk_addr, ramdisk_actual))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07003650 {
Mayank Grovere559cec2017-10-17 15:12:03 +05303651 dprintf(CRITICAL, "kernel/ramdisk addresses are not valid.\n");
lijuang2008ff22016-03-07 17:56:27 +08003652 goto boot_failed;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07003653 }
3654
Amol Jadicb524072012-08-09 16:40:18 -07003655#if DEVICE_TREE
Matthew Qin271927e2015-03-31 22:07:07 -04003656 scratch_offset = image_actual + page_size + out_len;
Amol Jadicb524072012-08-09 16:40:18 -07003657 /* find correct dtb and copy it to right location */
Matthew Qin271927e2015-03-31 22:07:07 -04003658 ret = copy_dtb(data, scratch_offset);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07003659
3660 dtb_copied = !ret ? 1 : 0;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07003661#else
Mayank Grovere559cec2017-10-17 15:12:03 +05303662 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE) ||
3663 check_ddr_addr_range_bound(hdr->tags_addr, MAX_TAGS_SIZE))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07003664 {
Mayank Grovere559cec2017-10-17 15:12:03 +05303665 dprintf(CRITICAL, "Tags addresses are not valid.\n");
lijuang2008ff22016-03-07 17:56:27 +08003666 goto boot_failed;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07003667 }
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07003668#endif
3669
3670 /* Load ramdisk & kernel */
3671 memmove((void*) hdr->ramdisk_addr, ptr + page_size + kernel_actual, hdr->ramdisk_size);
Matthew Qinbb7923d2015-02-09 10:56:09 +08003672 memmove((void*) hdr->kernel_addr, (char*) (kernel_start_addr), kernel_size);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07003673
3674#if DEVICE_TREE
Mayank Grovere559cec2017-10-17 15:12:03 +05303675 if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual) ||
3676 check_ddr_addr_range_bound(hdr->tags_addr, kernel_actual))
Matthew Qinbb7923d2015-02-09 10:56:09 +08003677 {
Mayank Grovere559cec2017-10-17 15:12:03 +05303678 dprintf(CRITICAL, "Tags addresses are not valid.\n");
lijuang2008ff22016-03-07 17:56:27 +08003679 goto boot_failed;
Matthew Qinbb7923d2015-02-09 10:56:09 +08003680 }
3681
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07003682 /*
3683 * If dtb is not found look for appended DTB in the kernel.
3684 * If appended dev tree is found, update the atags with
3685 * memory address to the DTB appended location on RAM.
3686 * Else update with the atags address in the kernel header
3687 */
3688 if (!dtb_copied) {
3689 void *dtb;
Matthew Qinbb7923d2015-02-09 10:56:09 +08003690 dtb = dev_tree_appended((void*)(ptr + page_size),
3691 hdr->kernel_size, dtb_offset,
Dima Zavine63e5572013-05-03 12:23:06 -07003692 (void *)hdr->tags_addr);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07003693 if (!dtb) {
3694 fastboot_fail("dtb not found");
lijuang2008ff22016-03-07 17:56:27 +08003695 goto boot_failed;
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07003696 }
Amol Jadicb524072012-08-09 16:40:18 -07003697 }
3698#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08003699
3700 fastboot_okay("");
Amol Jadi7c4316c2013-10-07 14:19:26 -07003701 fastboot_stop();
Brian Swetland9c4c0752009-01-25 16:23:50 -08003702
Dima Zavin77e41f32013-03-06 16:10:43 -08003703 boot_linux((void*) hdr->kernel_addr, (void*) hdr->tags_addr,
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07003704 (const char*) hdr->cmdline, board_machtype(),
3705 (void*) hdr->ramdisk_addr, hdr->ramdisk_size);
lijuang2008ff22016-03-07 17:56:27 +08003706
3707 /* fastboot already stop, it's no need to show fastboot menu */
3708 return;
3709boot_failed:
3710#if FBCON_DISPLAY_MSG
3711 /* revert to fastboot menu if boot failed */
3712 display_fastboot_menu();
3713#endif
3714 return;
Brian Swetland9c4c0752009-01-25 16:23:50 -08003715}
3716
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003717void cmd_erase_nand(const char *arg, void *data, unsigned sz)
Dima Zavin214cc642009-01-26 11:16:21 -08003718{
3719 struct ptentry *ptn;
3720 struct ptable *ptable;
3721
3722 ptable = flash_get_ptable();
3723 if (ptable == NULL) {
3724 fastboot_fail("partition table doesn't exist");
3725 return;
3726 }
3727
3728 ptn = ptable_find(ptable, arg);
3729 if (ptn == NULL) {
3730 fastboot_fail("unknown partition name");
3731 return;
3732 }
3733
Monika Singh292b3e92018-03-17 22:40:23 +05303734 if (!strncmp(arg, "avb_custom_key", strlen("avb_custom_key"))) {
3735 dprintf(INFO, "erasing avb_custom_key\n");
3736 if (erase_userkey()) {
3737 fastboot_fail("Erasing avb_custom_key failed");
3738 } else {
3739 fastboot_okay("");
3740 }
3741 return;
3742 }
3743
Dima Zavin214cc642009-01-26 11:16:21 -08003744 if (flash_erase(ptn)) {
3745 fastboot_fail("failed to erase partition");
3746 return;
3747 }
3748 fastboot_okay("");
3749}
3750
Bikas Gurungd48bd242010-09-04 19:54:32 -07003751
3752void cmd_erase_mmc(const char *arg, void *data, unsigned sz)
3753{
3754 unsigned long long ptn = 0;
Oliver Wangcee448d2013-10-22 18:40:13 +08003755 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07003756 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003757 uint8_t lun = 0;
Vijay Kumar Pendoti0b21f462016-05-02 17:09:18 +05303758 char *footer = NULL;
Rahul Shaharef7a5f8b2021-02-04 16:51:36 +05303759 char EraseResultStr[MAX_RSP_SIZE] = "";
3760 VirtualAbMergeStatus SnapshotMergeStatus;
Bikas Gurungd48bd242010-09-04 19:54:32 -07003761
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003762#if VERIFIED_BOOT
3763 if(!strcmp(arg, KEYSTORE_PTN_NAME))
3764 {
3765 if(!device.is_unlocked)
3766 {
3767 fastboot_fail("unlock device to erase keystore");
3768 return;
3769 }
3770 }
3771#endif
3772
Kinson Chikf1a43512011-07-14 11:28:39 -07003773 index = partition_get_index(arg);
3774 ptn = partition_get_offset(index);
Oliver Wangcee448d2013-10-22 18:40:13 +08003775 size = partition_get_size(index);
Neeti Desaica8c9602011-10-06 11:40:00 -07003776
Rahul Shaharef7a5f8b2021-02-04 16:51:36 +05303777 if (target_virtual_ab_supported()) {
3778 if (CheckVirtualAbCriticalPartition (arg)) {
3779 snprintf(EraseResultStr, MAX_RSP_SIZE,"Erase of %s is not allowed in %s state",
3780 arg, SnapshotMergeState);
3781 fastboot_fail(EraseResultStr);
3782 return;
3783 }
3784 SnapshotMergeStatus = GetSnapshotMergeStatus ();
3785 if (((SnapshotMergeStatus == MERGING) || (SnapshotMergeStatus == SNAPSHOTTED)) &&
3786 !strncmp (arg, "super", strlen ("super"))) {
3787
3788 if(SetSnapshotMergeStatus (CANCELLED))
3789 {
3790 fastboot_fail("Failed to update snapshot state to cancel");
3791 return;
3792 }
3793
3794 //updating fbvar snapshot-merge-state
3795 snprintf(SnapshotMergeState,strlen(VabSnapshotMergeStatus[NONE_MERGE_STATUS]) + 1,
3796 "%s", VabSnapshotMergeStatus[NONE_MERGE_STATUS]);
3797 }
3798 }
3799
Monika Singhc4778b72018-05-16 11:16:42 +05303800 if (!strncmp(arg, "avb_custom_key", strlen("avb_custom_key"))) {
3801 dprintf(INFO, "erasing avb_custom_key\n");
3802 if (erase_userkey()) {
3803 fastboot_fail("Erasing avb_custom_key failed");
3804 } else {
3805 fastboot_okay("");
3806 }
3807 return;
3808 }
3809
Kinson Chikf1a43512011-07-14 11:28:39 -07003810 if(ptn == 0) {
Neeti Desaica8c9602011-10-06 11:40:00 -07003811 fastboot_fail("Partition table doesn't exist\n");
Bikas Gurungd48bd242010-09-04 19:54:32 -07003812 return;
3813 }
Kun Liang2f1601a2013-08-12 16:29:54 +08003814
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003815 lun = partition_get_lun(index);
3816 mmc_set_lun(lun);
3817
Sridhar Parasuramd7957122015-02-27 11:33:40 -08003818 if (platform_boot_dev_isemmc())
3819 {
3820 if (mmc_erase_card(ptn, size)) {
3821 fastboot_fail("failed to erase partition\n");
3822 return;
3823 }
3824 } else {
3825 BUF_DMA_ALIGN(out, DEFAULT_ERASE_SIZE);
3826 size = partition_get_size(index);
3827 if (size > DEFAULT_ERASE_SIZE)
3828 size = DEFAULT_ERASE_SIZE;
Kun Liang2f1601a2013-08-12 16:29:54 +08003829
Sridhar Parasuramd7957122015-02-27 11:33:40 -08003830 /* Simple inefficient version of erase. Just writing
3831 0 in first several blocks */
3832 if (mmc_write(ptn , size, (unsigned int *)out)) {
3833 fastboot_fail("failed to erase partition");
3834 return;
3835 }
Vijay Kumar Pendoti0b21f462016-05-02 17:09:18 +05303836 /*Erase FDE metadata at the userdata footer*/
3837 if(!(strncmp(arg, "userdata", 8)))
3838 {
3839 footer = memalign(CACHE_LINE, FOOTER_SIZE);
3840 memset((void *)footer, 0, FOOTER_SIZE);
3841
3842 size = partition_get_size(index);
3843
3844 if (mmc_write((ptn + size) - FOOTER_SIZE , FOOTER_SIZE, (unsigned int *)footer)) {
3845 fastboot_fail("failed to erase userdata footer");
3846 free(footer);
3847 return;
3848 }
3849 free(footer);
3850 }
Bikas Gurungd48bd242010-09-04 19:54:32 -07003851 }
Monika Singh292b3e92018-03-17 22:40:23 +05303852#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover912eaa62017-10-26 12:08:53 +05303853 if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05303854 !(strncmp(arg, "userdata", 8)) &&
3855 send_delete_keys_to_tz())
Sridhar Parasuram32b30662015-07-10 13:33:22 -07003856 ASSERT(0);
3857#endif
Bikas Gurungd48bd242010-09-04 19:54:32 -07003858 fastboot_okay("");
3859}
3860
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003861void cmd_erase(const char *arg, void *data, unsigned sz)
3862{
Monika Singh292b3e92018-03-17 22:40:23 +05303863#if VERIFIED_BOOT || VERIFIED_BOOT_2
Channagoud Kadabi35297672015-06-13 11:09:49 -07003864 if (target_build_variant_user())
Channagoud Kadabice3cf422015-04-17 16:02:30 -07003865 {
Sridhar Parasuramc32d07d2015-07-12 10:57:48 -07003866 if(!device.is_unlocked)
Channagoud Kadabice3cf422015-04-17 16:02:30 -07003867 {
Channagoud Kadabi35297672015-06-13 11:09:49 -07003868 fastboot_fail("device is locked. Cannot erase");
Channagoud Kadabice3cf422015-04-17 16:02:30 -07003869 return;
3870 }
3871 }
3872#endif
3873
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003874 if(target_is_emmc_boot())
3875 cmd_erase_mmc(arg, data, sz);
3876 else
3877 cmd_erase_nand(arg, data, sz);
3878}
Bikas Gurungd48bd242010-09-04 19:54:32 -07003879
Mayank Grover11ff9692018-01-11 11:54:49 +05303880/* Get the size from partiton name */
3881static void get_partition_size(const char *arg, char *response)
3882{
3883 uint64_t ptn = 0;
3884 uint64_t size;
3885 int index = INVALID_PTN;
3886
3887 index = partition_get_index(arg);
3888
3889 if (index == INVALID_PTN)
3890 {
3891 dprintf(CRITICAL, "Invalid partition index\n");
3892 return;
3893 }
3894
3895 ptn = partition_get_offset(index);
3896
3897 if(!ptn)
3898 {
3899 dprintf(CRITICAL, "Invalid partition name %s\n", arg);
3900 return;
3901 }
3902
3903 size = partition_get_size(index);
3904
3905 snprintf(response, MAX_RSP_SIZE, "\t 0x%llx", size);
3906 return;
3907}
3908
Mayank Grover52cd10a2018-03-15 12:57:54 +05303909/* Function to check partition type of a partition*/
3910static fs_signature_type
3911check_partition_fs_signature(const char *arg)
3912{
3913 fs_signature_type ret = NO_FS;
3914 int index;
3915 unsigned long long ptn;
Mayank Grover399826a2018-08-27 12:15:15 +05303916 char *buffer = memalign(CACHE_LINE, mmc_blocksize);
3917 uint32_t sb_blk_offset = 0;
3918 char *sb_buffer = buffer;
3919
Mayank Grover52cd10a2018-03-15 12:57:54 +05303920 if (!sb_buffer)
3921 {
3922 dprintf(CRITICAL, "ERROR: Failed to allocate buffer for superblock\n");
3923 goto out;
3924 }
3925
3926 /* Read super block */
3927 if ((index = partition_get_index(arg)) < 0)
3928 {
3929 dprintf(CRITICAL, "ERROR: %s() doesn't exsit\n", arg);
3930 goto out;
3931 }
3932 ptn = partition_get_offset(index);
3933 mmc_set_lun(partition_get_lun(index));
Mayank Grover399826a2018-08-27 12:15:15 +05303934 sb_blk_offset = (FS_SUPERBLOCK_OFFSET/mmc_blocksize);
3935
3936 if(mmc_read(ptn + (sb_blk_offset * mmc_blocksize),
Mayank Grover52cd10a2018-03-15 12:57:54 +05303937 (void *)sb_buffer, mmc_blocksize))
3938 {
3939 dprintf(CRITICAL, "ERROR: Failed to read Superblock\n");
3940 goto out;
3941 }
3942
Mayank Grover399826a2018-08-27 12:15:15 +05303943 if (sb_blk_offset == 0)
3944 sb_buffer += FS_SUPERBLOCK_OFFSET;
3945
3946 if (*((uint16 *)(&sb_buffer[EXT_MAGIC_OFFSET_SB])) == (uint16)EXT_MAGIC)
Mayank Grover52cd10a2018-03-15 12:57:54 +05303947 {
3948 dprintf(SPEW, "%s() Found EXT FS\n", arg);
3949 ret = EXT_FS_SIGNATURE;
3950 }
Mayank Grover399826a2018-08-27 12:15:15 +05303951 else if (*((uint32 *)(&sb_buffer[F2FS_MAGIC_OFFSET_SB])) == F2FS_MAGIC)
Mayank Grover52cd10a2018-03-15 12:57:54 +05303952 {
3953 dprintf(SPEW, "%s() Found F2FS FS\n", arg);
3954 ret = EXT_F2FS_SIGNATURE;
3955 }
3956 else
3957 {
3958 dprintf(SPEW, "%s() Reverting to default 0x%x\n",
3959 arg, *((uint16 *)(&sb_buffer[EXT_MAGIC_OFFSET_SB])));
3960 ret = NO_FS;
3961 }
3962
3963out:
Mayank Grover399826a2018-08-27 12:15:15 +05303964 if(buffer)
3965 free(buffer);
Mayank Grover52cd10a2018-03-15 12:57:54 +05303966 return ret;
3967}
3968
Mayank Groverd38fe012018-03-13 15:33:16 +05303969/* Function to get partition type */
3970static void get_partition_type(const char *arg, char *response)
3971{
3972 uint n = 0;
Mayank Grover52cd10a2018-03-15 12:57:54 +05303973 fs_signature_type fs_signature;
Mayank Groverd38fe012018-03-13 15:33:16 +05303974
3975 if (arg == NULL ||
3976 response == NULL)
3977 {
3978 dprintf(CRITICAL, "Invalid input parameter\n");
3979 return;
3980 }
3981
3982 /* By default copy raw to response */
Mayank Grover0e559042018-04-11 17:49:30 +05303983 strlcpy(response, RAW_STR, MAX_RSP_SIZE);
Mayank Groverd38fe012018-03-13 15:33:16 +05303984
3985 /* Mark partiton type for known paritions only */
3986 for (n=0; n < ARRAY_SIZE(part_type_known); n++)
3987 {
3988 if (!strncmp(part_type_known[n].part_name, arg, strlen(arg)))
3989 {
Mayank Grover52cd10a2018-03-15 12:57:54 +05303990 /* Check partition for FS signature */
3991 fs_signature = check_partition_fs_signature(arg);
3992 switch (fs_signature)
3993 {
3994 case EXT_FS_SIGNATURE:
Mayank Grover0e559042018-04-11 17:49:30 +05303995 strlcpy(response, EXT_STR, MAX_RSP_SIZE);
Mayank Grover52cd10a2018-03-15 12:57:54 +05303996 break;
3997 case EXT_F2FS_SIGNATURE:
Mayank Grover0e559042018-04-11 17:49:30 +05303998 strlcpy(response, F2FS_STR, MAX_RSP_SIZE);
Mayank Grover52cd10a2018-03-15 12:57:54 +05303999 break;
4000 case NO_FS:
Mayank Grover0e559042018-04-11 17:49:30 +05304001 strlcpy(response, part_type_known[n].type_response, MAX_RSP_SIZE);
Mayank Grover52cd10a2018-03-15 12:57:54 +05304002 }
Mayank Groverd38fe012018-03-13 15:33:16 +05304003 }
4004 }
4005 return;
4006}
4007
Mayank Grover11ff9692018-01-11 11:54:49 +05304008/*
4009 * Publish the partition type & size info
4010 * fastboot getvar will publish the required information.
4011 * fastboot getvar partition_size:<partition_name>: partition size in hex
4012 * fastboot getvar partition_type:<partition_name>: partition type (ext/fat)
4013 */
4014static void publish_getvar_partition_info(struct getvar_partition_info *info, uint8_t num_parts)
4015{
Mayank Groverd38fe012018-03-13 15:33:16 +05304016 uint8_t i;
Mayank Grover11ff9692018-01-11 11:54:49 +05304017 static bool published = false;
4018 struct partition_entry *ptn_entry =
4019 partition_get_partition_entries();
4020 memset(info, 0, sizeof(struct getvar_partition_info)* num_parts);
4021
4022 for (i = 0; i < num_parts; i++) {
4023 strlcat(info[i].part_name, (const char *)ptn_entry[i].name, MAX_RSP_SIZE);
4024 strlcat(info[i].getvar_size, "partition-size:", MAX_GET_VAR_NAME_SIZE);
4025 strlcat(info[i].getvar_type, "partition-type:", MAX_GET_VAR_NAME_SIZE);
4026
Mayank Groverd38fe012018-03-13 15:33:16 +05304027 get_partition_type(info[i].part_name, info[i].type_response);
Mayank Grover11ff9692018-01-11 11:54:49 +05304028 get_partition_size(info[i].part_name, info[i].size_response);
Mayank Grover11ff9692018-01-11 11:54:49 +05304029 if (strlcat(info[i].getvar_size, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
4030 {
4031 dprintf(CRITICAL, "partition size name truncated\n");
4032 return;
4033 }
4034 if (strlcat(info[i].getvar_type, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
4035 {
4036 dprintf(CRITICAL, "partition type name truncated\n");
4037 return;
4038 }
4039
4040 if (!published)
4041 {
4042 /* publish partition size & type info */
4043 fastboot_publish((const char *) info[i].getvar_size, (const char *) info[i].size_response);
4044 fastboot_publish((const char *) info[i].getvar_type, (const char *) info[i].type_response);
4045 }
4046 }
4047 if (!published)
4048 published = true;
4049}
4050
4051
Ajay Dudani5c761132011-04-07 20:19:04 -07004052void cmd_flash_mmc_img(const char *arg, void *data, unsigned sz)
Shashank Mittal23b8f422010-04-16 19:27:21 -07004053{
4054 unsigned long long ptn = 0;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07004055 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07004056 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08004057 char *token = NULL;
4058 char *pname = NULL;
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07004059 char *sp;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08004060 uint8_t lun = 0;
4061 bool lun_set = false;
Mayank Grover351a75e2017-05-30 20:06:08 +05304062 int current_active_slot = INVALID;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07004063
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07004064 token = strtok_r((char *)arg, ":", &sp);
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08004065 pname = token;
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07004066 token = strtok_r(NULL, ":", &sp);
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08004067 if(token)
4068 {
4069 lun = atoi(token);
4070 mmc_set_lun(lun);
4071 lun_set = true;
4072 }
4073
Mao Jinlong226f33a2014-07-04 17:24:10 +08004074 if (pname)
Greg Grisco6e754772011-06-23 12:19:39 -07004075 {
Channagoud Kadabiad259832015-05-29 11:14:17 -07004076 if (!strncmp(pname, "frp-unlock", strlen("frp-unlock")))
4077 {
4078 if (!aboot_frp_unlock(pname, data, sz))
4079 {
4080 fastboot_info("FRP unlock successful");
4081 fastboot_okay("");
4082 }
4083 else
4084 fastboot_fail("Secret key is invalid, please update the bootloader with secret key");
4085
4086 return;
4087 }
4088
Mao Jinlong226f33a2014-07-04 17:24:10 +08004089 if (!strcmp(pname, "partition"))
4090 {
4091 dprintf(INFO, "Attempt to write partition image.\n");
4092 if (write_partition(sz, (unsigned char *) data)) {
4093 fastboot_fail("failed to write partition");
Greg Grisco6e754772011-06-23 12:19:39 -07004094 return;
4095 }
Mayank Grover11ff9692018-01-11 11:54:49 +05304096 /* Re-publish partition table */
4097 publish_getvar_partition_info(part_info, partition_get_partition_count());
Mayank Grover351a75e2017-05-30 20:06:08 +05304098
4099 /* Rescan partition table to ensure we have multislot support*/
4100 if (partition_scan_for_multislot())
4101 {
4102 current_active_slot = partition_find_active_slot();
4103 dprintf(INFO, "Multislot supported: Slot %s active",
4104 (SUFFIX_SLOT(current_active_slot)));
4105 }
4106 partition_mark_active_slot(current_active_slot);
Greg Grisco6e754772011-06-23 12:19:39 -07004107 }
Mao Jinlong226f33a2014-07-04 17:24:10 +08004108 else
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08004109 {
Shashank Mittald3e54dd2014-08-28 15:24:02 -07004110#if VERIFIED_BOOT
4111 if(!strcmp(pname, KEYSTORE_PTN_NAME))
4112 {
4113 if(!device.is_unlocked)
4114 {
4115 fastboot_fail("unlock device to flash keystore");
4116 return;
4117 }
Gaurav Nebhwanic7313cc2015-12-15 22:25:04 +05304118 if(!boot_verify_validate_keystore((unsigned char *)data,sz))
Shashank Mittald3e54dd2014-08-28 15:24:02 -07004119 {
4120 fastboot_fail("image is not a keystore file");
4121 return;
4122 }
4123 }
4124#endif
Mao Jinlong226f33a2014-07-04 17:24:10 +08004125 index = partition_get_index(pname);
4126 ptn = partition_get_offset(index);
4127 if(ptn == 0) {
4128 fastboot_fail("partition table doesn't exist");
4129 return;
4130 }
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08004131
Mayank Grover351a75e2017-05-30 20:06:08 +05304132 if (!strncmp(pname, "boot", strlen("boot"))
4133 || !strcmp(pname, "recovery"))
4134 {
Ashish Bhimanpalliwar84f7db92020-09-23 11:59:16 +05304135 if ((sz < BOOT_MAGIC_SIZE) || memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Mao Jinlong226f33a2014-07-04 17:24:10 +08004136 fastboot_fail("image is not a boot image");
4137 return;
4138 }
Mayank Grover351a75e2017-05-30 20:06:08 +05304139
4140 /* Reset multislot_partition attributes in case of flashing boot */
4141 if (partition_multislot_is_supported())
4142 {
4143 partition_reset_attributes(index);
4144 }
Mao Jinlong226f33a2014-07-04 17:24:10 +08004145 }
4146
4147 if(!lun_set)
4148 {
4149 lun = partition_get_lun(index);
4150 mmc_set_lun(lun);
4151 }
4152
4153 size = partition_get_size(index);
Gaurav Nebhwani4d2389c2016-03-17 21:10:05 +05304154 if (ROUND_TO_PAGE(sz, mmc_blocksize_mask) > size) {
Mao Jinlong226f33a2014-07-04 17:24:10 +08004155 fastboot_fail("size too large");
4156 return;
4157 }
4158 else if (mmc_write(ptn , sz, (unsigned int *)data)) {
4159 fastboot_fail("flash write failure");
4160 return;
4161 }
Greg Grisco6e754772011-06-23 12:19:39 -07004162 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07004163 }
4164 fastboot_okay("");
4165 return;
4166}
4167
Ajay Dudanide984792015-03-02 09:57:41 -08004168void cmd_flash_meta_img(const char *arg, void *data, unsigned sz)
4169{
4170 int i, images;
4171 meta_header_t *meta_header;
4172 img_header_entry_t *img_header_entry;
Parth Dixit3e2d3032016-03-04 17:11:52 +05304173 /*End of the image address*/
4174 uintptr_t data_end;
4175
4176 if( (UINT_MAX - sz) > (uintptr_t)data )
4177 data_end = (uintptr_t)data + sz;
4178 else
4179 {
4180 fastboot_fail("Cannot flash: image header corrupt");
4181 return;
4182 }
4183
Ashish Bhimanpalliwar21494e12020-10-07 13:24:03 +05304184 if((UINT_MAX - sizeof(meta_header_t) < (uintptr_t)data) || (data_end < ((uintptr_t)data + sizeof(meta_header_t))))
Parth Dixit3e2d3032016-03-04 17:11:52 +05304185 {
4186 fastboot_fail("Cannot flash: image header corrupt");
4187 return;
4188 }
Ajay Dudanide984792015-03-02 09:57:41 -08004189
lijuang8ee21882016-04-19 16:57:11 +08004190 /* If device is locked:
4191 * Forbid to flash image to avoid the device to bypass the image
4192 * which with "any" name other than bootloader. Because it maybe
4193 * a meta package of all partitions.
4194 */
Monika Singh292b3e92018-03-17 22:40:23 +05304195#if VERIFIED_BOOT || VERIFIED_BOOT_2
lijuang8ee21882016-04-19 16:57:11 +08004196 if (target_build_variant_user()) {
4197 if (!device.is_unlocked) {
4198 fastboot_fail("Device is locked, meta image flashing is not allowed");
4199 return;
4200 }
Mayank Grover889be1b2017-09-12 20:12:23 +05304201
Mayank Grover912eaa62017-10-26 12:08:53 +05304202 if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05304203 !device.is_unlock_critical)
4204 {
lijuang8ee21882016-04-19 16:57:11 +08004205 fastboot_fail("Device is critical locked, Meta image flashing is not allowed");
4206 return;
4207 }
lijuang8ee21882016-04-19 16:57:11 +08004208 }
4209#endif
4210
Ajay Dudanide984792015-03-02 09:57:41 -08004211 meta_header = (meta_header_t*) data;
Ashish Bhimanpalliwar21494e12020-10-07 13:24:03 +05304212 if(((UINT_MAX - sizeof(meta_header_t) - meta_header->img_hdr_sz) < (uintptr_t)data) || data_end < ((uintptr_t)data + sizeof(meta_header_t) + meta_header->img_hdr_sz))
Parth Dixit3e2d3032016-03-04 17:11:52 +05304213 {
4214 fastboot_fail("Cannot flash: image header corrupt");
4215 return;
4216 }
Ajay Dudanide984792015-03-02 09:57:41 -08004217 img_header_entry = (img_header_entry_t*) (data+sizeof(meta_header_t));
4218
4219 images = meta_header->img_hdr_sz / sizeof(img_header_entry_t);
4220
4221 for (i=0; i<images; i++) {
4222
4223 if((img_header_entry[i].ptn_name == NULL) ||
4224 (img_header_entry[i].start_offset == 0) ||
4225 (img_header_entry[i].size == 0))
4226 break;
Kishor PK49831502017-04-21 17:55:43 +05304227 if ((UINT_MAX - img_header_entry[i].start_offset) < (uintptr_t)data) {
4228 fastboot_fail("Integer overflow detected in start_offset of img");
4229 break;
4230 }
4231 else if ((UINT_MAX - (img_header_entry[i].start_offset + (uintptr_t)data)) < img_header_entry[i].size) {
4232 fastboot_fail("Integer overflow detected in size of img");
4233 break;
4234 }
Parth Dixit3e2d3032016-03-04 17:11:52 +05304235 if( data_end < ((uintptr_t)data + img_header_entry[i].start_offset
4236 + img_header_entry[i].size) )
4237 {
4238 fastboot_fail("Cannot flash: image size mismatch");
4239 break;
4240 }
4241
Ajay Dudanide984792015-03-02 09:57:41 -08004242 cmd_flash_mmc_img(img_header_entry[i].ptn_name,
4243 (void *) data + img_header_entry[i].start_offset,
4244 img_header_entry[i].size);
4245 }
4246
Channagoud Kadabiaafbbfc2015-03-04 17:52:56 -08004247 if (!strncmp(arg, "bootloader", strlen("bootloader")))
4248 {
4249 strlcpy(device.bootloader_version, TARGET(BOARD), MAX_VERSION_LEN);
4250 strlcat(device.bootloader_version, "-", MAX_VERSION_LEN);
4251 strlcat(device.bootloader_version, meta_header->img_version, MAX_VERSION_LEN);
4252 }
4253 else
4254 {
4255 strlcpy(device.radio_version, TARGET(BOARD), MAX_VERSION_LEN);
4256 strlcat(device.radio_version, "-", MAX_VERSION_LEN);
4257 strlcat(device.radio_version, meta_header->img_version, MAX_VERSION_LEN);
4258 }
4259
4260 write_device_info(&device);
Ajay Dudanide984792015-03-02 09:57:41 -08004261 fastboot_okay("");
4262 return;
4263}
4264
Ajay Dudani5c761132011-04-07 20:19:04 -07004265void cmd_flash_mmc_sparse_img(const char *arg, void *data, unsigned sz)
4266{
4267 unsigned int chunk;
wufeng.jiang813dc352015-06-02 23:02:46 -04004268 uint64_t chunk_data_sz;
Channagoud Kadabif627cf82013-09-09 14:08:20 -07004269 uint32_t *fill_buf = NULL;
4270 uint32_t fill_val;
Mayank Grover4f50bba2017-07-19 18:17:08 +05304271 uint32_t blk_sz_actual = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07004272 sparse_header_t *sparse_header;
4273 chunk_header_t *chunk_header;
Ajay Dudaniab18f022011-05-12 14:39:22 -07004274 uint32_t total_blocks = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07004275 unsigned long long ptn = 0;
Channagoud Kadabi65b91002011-10-11 17:34:33 +05304276 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07004277 int index = INVALID_PTN;
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05304278 uint32_t i;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08004279 uint8_t lun = 0;
vijay kumar800255e2015-04-24 20:26:19 +05304280 /*End of the sparse image address*/
Parth Dixit64b1a482016-03-07 16:31:26 +05304281 uintptr_t data_end = (uintptr_t)data + sz;
Ajay Dudani5c761132011-04-07 20:19:04 -07004282
Kinson Chikf1a43512011-07-14 11:28:39 -07004283 index = partition_get_index(arg);
4284 ptn = partition_get_offset(index);
4285 if(ptn == 0) {
Ajay Dudani5c761132011-04-07 20:19:04 -07004286 fastboot_fail("partition table doesn't exist");
4287 return;
4288 }
4289
Channagoud Kadabi65b91002011-10-11 17:34:33 +05304290 size = partition_get_size(index);
Channagoud Kadabi65b91002011-10-11 17:34:33 +05304291
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08004292 lun = partition_get_lun(index);
4293 mmc_set_lun(lun);
4294
vijay kumar800255e2015-04-24 20:26:19 +05304295 if (sz < sizeof(sparse_header_t)) {
4296 fastboot_fail("size too low");
4297 return;
4298 }
4299
Ajay Dudani5c761132011-04-07 20:19:04 -07004300 /* Read and skip over sparse image header */
4301 sparse_header = (sparse_header_t *) data;
vijay kumar800255e2015-04-24 20:26:19 +05304302
Mayank Grover48bd9bb2017-07-19 12:04:16 +05304303 if (!sparse_header->blk_sz || (sparse_header->blk_sz % 4)){
4304 fastboot_fail("Invalid block size\n");
4305 return;
4306 }
4307
vijay kumar1321f342015-03-27 12:13:42 +05304308 if (((uint64_t)sparse_header->total_blks * (uint64_t)sparse_header->blk_sz) > size) {
Ajay Dudani876b3282012-12-21 14:12:17 -08004309 fastboot_fail("size too large");
4310 return;
4311 }
4312
vijay kumarde4fcf62015-04-23 13:05:49 +05304313 data += sizeof(sparse_header_t);
vijay kumar800255e2015-04-24 20:26:19 +05304314
Parth Dixit64b1a482016-03-07 16:31:26 +05304315 if (data_end < (uintptr_t)data) {
vijay kumar800255e2015-04-24 20:26:19 +05304316 fastboot_fail("buffer overreads occured due to invalid sparse header");
4317 return;
4318 }
4319
vijay kumarde4fcf62015-04-23 13:05:49 +05304320 if(sparse_header->file_hdr_sz != sizeof(sparse_header_t))
Ajay Dudani5c761132011-04-07 20:19:04 -07004321 {
vijay kumarde4fcf62015-04-23 13:05:49 +05304322 fastboot_fail("sparse header size mismatch");
4323 return;
Ajay Dudani5c761132011-04-07 20:19:04 -07004324 }
4325
Ajay Dudanib06c05f2011-05-12 14:46:10 -07004326 dprintf (SPEW, "=== Sparse Image Header ===\n");
4327 dprintf (SPEW, "magic: 0x%x\n", sparse_header->magic);
4328 dprintf (SPEW, "major_version: 0x%x\n", sparse_header->major_version);
4329 dprintf (SPEW, "minor_version: 0x%x\n", sparse_header->minor_version);
4330 dprintf (SPEW, "file_hdr_sz: %d\n", sparse_header->file_hdr_sz);
4331 dprintf (SPEW, "chunk_hdr_sz: %d\n", sparse_header->chunk_hdr_sz);
4332 dprintf (SPEW, "blk_sz: %d\n", sparse_header->blk_sz);
4333 dprintf (SPEW, "total_blks: %d\n", sparse_header->total_blks);
4334 dprintf (SPEW, "total_chunks: %d\n", sparse_header->total_chunks);
Ajay Dudani5c761132011-04-07 20:19:04 -07004335
4336 /* Start processing chunks */
4337 for (chunk=0; chunk<sparse_header->total_chunks; chunk++)
4338 {
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05304339 /* Make sure the total image size does not exceed the partition size */
4340 if(((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz) >= size) {
4341 fastboot_fail("size too large");
4342 return;
4343 }
Ajay Dudani5c761132011-04-07 20:19:04 -07004344 /* Read and skip over chunk header */
4345 chunk_header = (chunk_header_t *) data;
4346 data += sizeof(chunk_header_t);
4347
Parth Dixit64b1a482016-03-07 16:31:26 +05304348 if (data_end < (uintptr_t)data) {
vijay kumar800255e2015-04-24 20:26:19 +05304349 fastboot_fail("buffer overreads occured due to invalid sparse header");
4350 return;
4351 }
4352
Ajay Dudani5c761132011-04-07 20:19:04 -07004353 dprintf (SPEW, "=== Chunk Header ===\n");
4354 dprintf (SPEW, "chunk_type: 0x%x\n", chunk_header->chunk_type);
4355 dprintf (SPEW, "chunk_data_sz: 0x%x\n", chunk_header->chunk_sz);
4356 dprintf (SPEW, "total_size: 0x%x\n", chunk_header->total_sz);
4357
vijay kumar800255e2015-04-24 20:26:19 +05304358 if(sparse_header->chunk_hdr_sz != sizeof(chunk_header_t))
Ajay Dudani5c761132011-04-07 20:19:04 -07004359 {
vijay kumar800255e2015-04-24 20:26:19 +05304360 fastboot_fail("chunk header size mismatch");
4361 return;
Ajay Dudani5c761132011-04-07 20:19:04 -07004362 }
4363
wufeng.jiang813dc352015-06-02 23:02:46 -04004364 chunk_data_sz = (uint64_t)sparse_header->blk_sz * chunk_header->chunk_sz;
4365
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05304366 /* Make sure that the chunk size calculated from sparse image does not
4367 * exceed partition size
4368 */
4369 if ((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz + chunk_data_sz > size)
4370 {
4371 fastboot_fail("Chunk data size exceeds partition size");
4372 return;
4373 }
4374
Ajay Dudani5c761132011-04-07 20:19:04 -07004375 switch (chunk_header->chunk_type)
4376 {
4377 case CHUNK_TYPE_RAW:
wufeng.jiang813dc352015-06-02 23:02:46 -04004378 if((uint64_t)chunk_header->total_sz != ((uint64_t)sparse_header->chunk_hdr_sz +
Ajay Dudani5c761132011-04-07 20:19:04 -07004379 chunk_data_sz))
4380 {
4381 fastboot_fail("Bogus chunk size for chunk type Raw");
4382 return;
4383 }
4384
Parth Dixit64b1a482016-03-07 16:31:26 +05304385 if (data_end < (uintptr_t)data + chunk_data_sz) {
vijay kumar800255e2015-04-24 20:26:19 +05304386 fastboot_fail("buffer overreads occured due to invalid sparse header");
4387 return;
4388 }
4389
wufeng.jiang813dc352015-06-02 23:02:46 -04004390 /* chunk_header->total_sz is uint32,So chunk_data_sz is now less than 2^32
4391 otherwise it will return in the line above
4392 */
Ajay Dudaniab18f022011-05-12 14:39:22 -07004393 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
wufeng.jiang813dc352015-06-02 23:02:46 -04004394 (uint32_t)chunk_data_sz,
Ajay Dudaniab18f022011-05-12 14:39:22 -07004395 (unsigned int*)data))
Ajay Dudani5c761132011-04-07 20:19:04 -07004396 {
4397 fastboot_fail("flash write failure");
4398 return;
4399 }
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05304400 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
4401 fastboot_fail("Bogus size for RAW chunk type");
4402 return;
4403 }
Ajay Dudani5c761132011-04-07 20:19:04 -07004404 total_blocks += chunk_header->chunk_sz;
wufeng.jiang813dc352015-06-02 23:02:46 -04004405 data += (uint32_t)chunk_data_sz;
Ajay Dudani5c761132011-04-07 20:19:04 -07004406 break;
4407
Channagoud Kadabif627cf82013-09-09 14:08:20 -07004408 case CHUNK_TYPE_FILL:
4409 if(chunk_header->total_sz != (sparse_header->chunk_hdr_sz +
4410 sizeof(uint32_t)))
4411 {
4412 fastboot_fail("Bogus chunk size for chunk type FILL");
4413 return;
4414 }
4415
Mayank Grover4f50bba2017-07-19 18:17:08 +05304416 blk_sz_actual = ROUNDUP(sparse_header->blk_sz, CACHE_LINE);
4417 /* Integer overflow detected */
4418 if (blk_sz_actual < sparse_header->blk_sz)
4419 {
4420 fastboot_fail("Invalid block size");
4421 return;
4422 }
4423
4424 fill_buf = (uint32_t *)memalign(CACHE_LINE, blk_sz_actual);
Channagoud Kadabif627cf82013-09-09 14:08:20 -07004425 if (!fill_buf)
4426 {
4427 fastboot_fail("Malloc failed for: CHUNK_TYPE_FILL");
4428 return;
4429 }
4430
Parth Dixit64b1a482016-03-07 16:31:26 +05304431 if (data_end < (uintptr_t)data + sizeof(uint32_t)) {
vijay kumar800255e2015-04-24 20:26:19 +05304432 fastboot_fail("buffer overreads occured due to invalid sparse header");
Gaurav Nebhwani163cbf82016-03-17 21:21:27 +05304433 free(fill_buf);
vijay kumar800255e2015-04-24 20:26:19 +05304434 return;
4435 }
Channagoud Kadabif627cf82013-09-09 14:08:20 -07004436 fill_val = *(uint32_t *)data;
4437 data = (char *) data + sizeof(uint32_t);
Channagoud Kadabif627cf82013-09-09 14:08:20 -07004438
4439 for (i = 0; i < (sparse_header->blk_sz / sizeof(fill_val)); i++)
4440 {
4441 fill_buf[i] = fill_val;
4442 }
4443
Gaurav Nebhwanie94cbe12016-03-17 21:16:34 +05304444 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz))
4445 {
4446 fastboot_fail("bogus size for chunk FILL type");
4447 free(fill_buf);
4448 return;
4449 }
4450
wufeng.jiang813dc352015-06-02 23:02:46 -04004451 for (i = 0; i < chunk_header->chunk_sz; i++)
Channagoud Kadabif627cf82013-09-09 14:08:20 -07004452 {
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05304453 /* Make sure that the data written to partition does not exceed partition size */
4454 if ((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz + sparse_header->blk_sz > size)
4455 {
4456 fastboot_fail("Chunk data size for fill type exceeds partition size");
Gaurav Nebhwani163cbf82016-03-17 21:21:27 +05304457 free(fill_buf);
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05304458 return;
4459 }
4460
Channagoud Kadabif627cf82013-09-09 14:08:20 -07004461 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
4462 sparse_header->blk_sz,
4463 fill_buf))
4464 {
4465 fastboot_fail("flash write failure");
4466 free(fill_buf);
4467 return;
4468 }
4469
4470 total_blocks++;
4471 }
4472
4473 free(fill_buf);
4474 break;
4475
Ajay Dudani5c761132011-04-07 20:19:04 -07004476 case CHUNK_TYPE_DONT_CARE:
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05304477 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
4478 fastboot_fail("bogus size for chunk DONT CARE type");
4479 return;
4480 }
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07004481 total_blocks += chunk_header->chunk_sz;
4482 break;
4483
Ajay Dudani5c761132011-04-07 20:19:04 -07004484 case CHUNK_TYPE_CRC:
4485 if(chunk_header->total_sz != sparse_header->chunk_hdr_sz)
4486 {
wufeng.jiang813dc352015-06-02 23:02:46 -04004487 fastboot_fail("Bogus chunk size for chunk type CRC");
Ajay Dudani5c761132011-04-07 20:19:04 -07004488 return;
4489 }
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05304490 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
4491 fastboot_fail("bogus size for chunk CRC type");
4492 return;
4493 }
Ajay Dudani5c761132011-04-07 20:19:04 -07004494 total_blocks += chunk_header->chunk_sz;
Parth Dixit64b1a482016-03-07 16:31:26 +05304495 if ((uintptr_t)data > UINT_MAX - chunk_data_sz) {
vijay kumar800255e2015-04-24 20:26:19 +05304496 fastboot_fail("integer overflow occured");
4497 return;
4498 }
wufeng.jiang813dc352015-06-02 23:02:46 -04004499 data += (uint32_t)chunk_data_sz;
Parth Dixit64b1a482016-03-07 16:31:26 +05304500 if (data_end < (uintptr_t)data) {
vijay kumar800255e2015-04-24 20:26:19 +05304501 fastboot_fail("buffer overreads occured due to invalid sparse header");
4502 return;
4503 }
Ajay Dudani5c761132011-04-07 20:19:04 -07004504 break;
4505
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07004506 default:
Channagoud Kadabif627cf82013-09-09 14:08:20 -07004507 dprintf(CRITICAL, "Unkown chunk type: %x\n",chunk_header->chunk_type);
Ajay Dudani5c761132011-04-07 20:19:04 -07004508 fastboot_fail("Unknown chunk type");
4509 return;
4510 }
4511 }
4512
Ajay Dudani0c6927b2011-05-18 11:12:16 -07004513 dprintf(INFO, "Wrote %d blocks, expected to write %d blocks\n",
4514 total_blocks, sparse_header->total_blks);
4515
4516 if(total_blocks != sparse_header->total_blks)
4517 {
4518 fastboot_fail("sparse image write failure");
4519 }
Ajay Dudani5c761132011-04-07 20:19:04 -07004520
4521 fastboot_okay("");
4522 return;
4523}
4524
Rahul Shaharef7a5f8b2021-02-04 16:51:36 +05304525static bool CheckVirtualAbCriticalPartition (const char *PartitionName)
4526{
4527 VirtualAbMergeStatus SnapshotMergeStatus;
4528 uint32 Iter = 0;
4529
4530 SnapshotMergeStatus = GetSnapshotMergeStatus ();
4531 if ((SnapshotMergeStatus == MERGING || SnapshotMergeStatus == SNAPSHOTTED)) {
4532 for (Iter = 0; Iter < ARRAY_SIZE (VirtualAbCriticalPartitions); Iter++) {
4533 if (!strncmp (PartitionName, VirtualAbCriticalPartitions[Iter],
4534 strlen (VirtualAbCriticalPartitions[Iter])))
4535 return true;
4536 }
4537 }
4538 return false;
4539}
4540
Ajay Dudani5c761132011-04-07 20:19:04 -07004541void cmd_flash_mmc(const char *arg, void *data, unsigned sz)
4542{
4543 sparse_header_t *sparse_header;
Ajay Dudanide984792015-03-02 09:57:41 -08004544 meta_header_t *meta_header;
Rahul Shaharef7a5f8b2021-02-04 16:51:36 +05304545 VirtualAbMergeStatus SnapshotMergeStatus;
4546 char FlashResultStr[MAX_RSP_SIZE] = "";
Ajay Dudani5c761132011-04-07 20:19:04 -07004547
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07004548#ifdef SSD_ENABLE
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -08004549 /* 8 Byte Magic + 2048 Byte xml + Encrypted Data */
4550 unsigned int *magic_number = (unsigned int *) data;
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08004551 int ret=0;
4552 uint32 major_version=0;
4553 uint32 minor_version=0;
4554
4555 ret = scm_svc_version(&major_version,&minor_version);
4556 if(!ret)
4557 {
4558 if(major_version >= 2)
4559 {
Amir Samuelovbb65ce02013-05-05 12:20:18 +03004560 if( !strcmp(arg, "ssd") || !strcmp(arg, "tqs") )
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08004561 {
4562 ret = encrypt_scm((uint32 **) &data, &sz);
4563 if (ret != 0) {
4564 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
4565 return;
4566 }
4567
Amir Samuelovbb65ce02013-05-05 12:20:18 +03004568 /* Protect only for SSD */
4569 if (!strcmp(arg, "ssd")) {
4570 ret = scm_protect_keystore((uint32 *) data, sz);
4571 if (ret != 0) {
4572 dprintf(CRITICAL, "ERROR: scm_protect_keystore Failed\n");
4573 return;
4574 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08004575 }
4576 }
4577 else
4578 {
4579 ret = decrypt_scm_v2((uint32 **) &data, &sz);
4580 if(ret != 0)
4581 {
4582 dprintf(CRITICAL,"ERROR: Decryption Failure\n");
4583 return;
4584 }
4585 }
4586 }
4587 else
4588 {
4589 if (magic_number[0] == DECRYPT_MAGIC_0 &&
4590 magic_number[1] == DECRYPT_MAGIC_1)
4591 {
4592 ret = decrypt_scm((uint32 **) &data, &sz);
4593 if (ret != 0) {
4594 dprintf(CRITICAL, "ERROR: Invalid secure image\n");
4595 return;
4596 }
4597 }
4598 else if (magic_number[0] == ENCRYPT_MAGIC_0 &&
4599 magic_number[1] == ENCRYPT_MAGIC_1)
4600 {
4601 ret = encrypt_scm((uint32 **) &data, &sz);
4602 if (ret != 0) {
4603 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
4604 return;
4605 }
4606 }
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07004607 }
4608 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08004609 else
Neeti Desai127b9e02012-03-20 16:11:23 -07004610 {
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08004611 dprintf(CRITICAL,"INVALID SVC Version\n");
4612 return;
Neeti Desai127b9e02012-03-20 16:11:23 -07004613 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08004614#endif /* SSD_ENABLE */
Neeti Desai127b9e02012-03-20 16:11:23 -07004615
Monika Singh292b3e92018-03-17 22:40:23 +05304616#if VERIFIED_BOOT || VERIFIED_BOOT_2
Channagoud Kadabi35297672015-06-13 11:09:49 -07004617 if (target_build_variant_user())
Shashank Mittald3e54dd2014-08-28 15:24:02 -07004618 {
lijuang511a2b52015-08-14 20:50:51 +08004619 /* if device is locked:
4620 * common partition will not allow to be flashed
4621 * critical partition will allow to flash image.
4622 */
4623 if(!device.is_unlocked && !critical_flash_allowed(arg)) {
4624 fastboot_fail("Partition flashing is not allowed");
4625 return;
4626 }
Mayank Grover889be1b2017-09-12 20:12:23 +05304627
lijuang511a2b52015-08-14 20:50:51 +08004628 /* if device critical is locked:
4629 * common partition will allow to be flashed
4630 * critical partition will not allow to flash image.
4631 */
Mayank Grover912eaa62017-10-26 12:08:53 +05304632 if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05304633 !device.is_unlock_critical &&
4634 critical_flash_allowed(arg)) {
4635 fastboot_fail("Critical partition flashing is not allowed");
4636 return;
Shashank Mittald3e54dd2014-08-28 15:24:02 -07004637 }
4638 }
4639#endif
Rahul Shaharef7a5f8b2021-02-04 16:51:36 +05304640
4641 if (target_virtual_ab_supported())
4642 {
4643 if (CheckVirtualAbCriticalPartition (arg)) {
4644 snprintf(FlashResultStr, MAX_RSP_SIZE,"Flashing of %s is not allowed in %s state",
4645 arg, SnapshotMergeState);
4646 fastboot_fail(FlashResultStr);
4647 return;
4648 }
4649
4650 SnapshotMergeStatus = GetSnapshotMergeStatus ();
4651 if (((SnapshotMergeStatus == MERGING) || (SnapshotMergeStatus == SNAPSHOTTED)) &&
4652 !strncmp (arg, "super", strlen ("super"))) {
4653 if(SetSnapshotMergeStatus (CANCELLED))
4654 {
4655 fastboot_fail("Failed to update snapshot state to cancel");
4656 return;
4657 }
4658
4659 //updating fbvar snapshot-merge-state
4660 snprintf(SnapshotMergeState,strlen(VabSnapshotMergeStatus[NONE_MERGE_STATUS]) + 1,
4661 "%s", VabSnapshotMergeStatus[NONE_MERGE_STATUS]);
4662 }
4663 }
4664
Monika Singh292b3e92018-03-17 22:40:23 +05304665 if (!strncmp(arg, "avb_custom_key", strlen("avb_custom_key"))) {
4666 dprintf(INFO, "flashing avb_custom_key\n");
4667 if (store_userkey(data, sz)) {
4668 fastboot_fail("Flashing avb_custom_key failed");
4669 } else {
4670 fastboot_okay("");
4671 }
4672 return;
4673 }
Shashank Mittald3e54dd2014-08-28 15:24:02 -07004674
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07004675 sparse_header = (sparse_header_t *) data;
Ajay Dudanide984792015-03-02 09:57:41 -08004676 meta_header = (meta_header_t *) data;
4677 if (sparse_header->magic == SPARSE_HEADER_MAGIC)
Ajay Dudani5c761132011-04-07 20:19:04 -07004678 cmd_flash_mmc_sparse_img(arg, data, sz);
Ajay Dudanide984792015-03-02 09:57:41 -08004679 else if (meta_header->magic == META_HEADER_MAGIC)
4680 cmd_flash_meta_img(arg, data, sz);
4681 else
4682 cmd_flash_mmc_img(arg, data, sz);
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07004683
4684#if VERIFIED_BOOT
Mayank Grover912eaa62017-10-26 12:08:53 +05304685 if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05304686 (!strncmp(arg, "system", 6)) &&
4687 !device.verity_mode)
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07004688 // reset dm_verity mode to enforcing
4689 device.verity_mode = 1;
4690 write_device_info(&device);
Parth Dixitddbc7352015-10-18 03:13:31 +05304691#endif
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07004692
Ajay Dudani5c761132011-04-07 20:19:04 -07004693 return;
4694}
4695
Tanya Brokhman84eaaf62015-02-24 21:57:06 +02004696void cmd_updatevol(const char *vol_name, void *data, unsigned sz)
4697{
4698 struct ptentry *sys_ptn;
4699 struct ptable *ptable;
4700
4701 ptable = flash_get_ptable();
4702 if (ptable == NULL) {
4703 fastboot_fail("partition table doesn't exist");
4704 return;
4705 }
4706
4707 sys_ptn = ptable_find(ptable, "system");
4708 if (sys_ptn == NULL) {
4709 fastboot_fail("system partition not found");
4710 return;
4711 }
4712
4713 sz = ROUND_TO_PAGE(sz, page_mask);
4714 if (update_ubi_vol(sys_ptn, vol_name, data, sz))
4715 fastboot_fail("update_ubi_vol failed");
4716 else
4717 fastboot_okay("");
4718}
4719
Sridhar Parasurame94e8152014-10-24 14:06:03 -07004720void cmd_flash_nand(const char *arg, void *data, unsigned sz)
Dima Zavin214cc642009-01-26 11:16:21 -08004721{
4722 struct ptentry *ptn;
4723 struct ptable *ptable;
4724 unsigned extra = 0;
Gaurav Nebhwanid9730712016-05-06 14:28:29 +05304725 uint64_t partition_size = 0;
Vijay Kumar Pendotic6f22a72017-05-19 22:44:34 +05304726 unsigned bytes_to_round_page = 0;
4727 unsigned rounded_size = 0;
Dima Zavin214cc642009-01-26 11:16:21 -08004728
Kishor PK38ed93d2017-04-25 14:19:26 +05304729 if((uintptr_t)data > (UINT_MAX - sz)) {
4730 fastboot_fail("Cannot flash: image header corrupt");
4731 return;
4732 }
4733
Dima Zavin214cc642009-01-26 11:16:21 -08004734 ptable = flash_get_ptable();
4735 if (ptable == NULL) {
4736 fastboot_fail("partition table doesn't exist");
4737 return;
4738 }
4739
4740 ptn = ptable_find(ptable, arg);
4741 if (ptn == NULL) {
Tanya Brokhman84eaaf62015-02-24 21:57:06 +02004742 dprintf(INFO, "unknown partition name (%s). Trying updatevol\n",
4743 arg);
4744 cmd_updatevol(arg, data, sz);
Dima Zavin214cc642009-01-26 11:16:21 -08004745 return;
4746 }
4747
Monika Singh292b3e92018-03-17 22:40:23 +05304748 if (!strncmp(arg, "avb_custom_key", strlen("avb_custom_key"))) {
4749 dprintf(INFO, "flashing avb_custom_key\n");
4750 if (store_userkey(data, sz)) {
4751 fastboot_fail("Flashing avb_custom_key failed");
4752 } else {
4753 fastboot_okay("");
4754 }
4755 return;
4756 }
4757
Dima Zavin214cc642009-01-26 11:16:21 -08004758 if (!strcmp(ptn->name, "boot") || !strcmp(ptn->name, "recovery")) {
Kishor PK38ed93d2017-04-25 14:19:26 +05304759 if((sz > BOOT_MAGIC_SIZE) && (!memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE))) {
4760 dprintf(INFO, "Verified the BOOT_MAGIC in image header \n");
4761 } else {
4762 fastboot_fail("Image is not a boot image");
Dima Zavin214cc642009-01-26 11:16:21 -08004763 return;
4764 }
4765 }
4766
Amol Jadi5c61a952012-05-04 17:05:35 -07004767 if (!strcmp(ptn->name, "system")
Deepa Dinamani13e32c42012-03-12 14:34:17 -07004768 || !strcmp(ptn->name, "userdata")
4769 || !strcmp(ptn->name, "persist")
Sundarajan Srinivasanb063a852013-11-19 14:02:27 -08004770 || !strcmp(ptn->name, "recoveryfs")
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -08004771 || !strcmp(ptn->name, "modem"))
Tanya Brokhman1c94f1a2015-02-15 09:05:03 +02004772 extra = 1;
Kishor PK38ed93d2017-04-25 14:19:26 +05304773 else {
Vijay Kumar Pendotic6f22a72017-05-19 22:44:34 +05304774 rounded_size = ROUNDUP(sz, page_size);
4775 bytes_to_round_page = rounded_size - sz;
4776 if (bytes_to_round_page) {
4777 if (((uintptr_t)data + sz ) > (UINT_MAX - bytes_to_round_page)) {
4778 fastboot_fail("Integer overflow detected");
4779 return;
4780 }
4781 if (((uintptr_t)data + sz + bytes_to_round_page) >
4782 ((uintptr_t)target_get_scratch_address() + target_get_max_flash_size())) {
4783 fastboot_fail("Buffer size is not aligned to page_size");
4784 return;
4785 }
4786 else {
4787 memset(data + sz, 0, bytes_to_round_page);
4788 sz = rounded_size;
4789 }
Kishor PK38ed93d2017-04-25 14:19:26 +05304790 }
Gaurav Nebhwanid9730712016-05-06 14:28:29 +05304791 }
4792
Kishor PK38ed93d2017-04-25 14:19:26 +05304793 /*Checking partition_size for the possible integer overflow */
4794 partition_size = validate_partition_size(ptn);
4795
Gaurav Nebhwanid9730712016-05-06 14:28:29 +05304796 if (sz > partition_size) {
4797 fastboot_fail("Image size too large");
4798 return;
4799 }
4800
Dima Zavin214cc642009-01-26 11:16:21 -08004801 dprintf(INFO, "writing %d bytes to '%s'\n", sz, ptn->name);
Mayank Grover6cde9352017-06-06 18:45:23 +05304802 if ((sz > UBI_EC_HDR_SIZE) &&
4803 (!memcmp((void *)data, UBI_MAGIC, UBI_MAGIC_SIZE))) {
Tanya Brokhman1c94f1a2015-02-15 09:05:03 +02004804 if (flash_ubi_img(ptn, data, sz)) {
4805 fastboot_fail("flash write failure");
4806 return;
4807 }
4808 } else {
4809 if (flash_write(ptn, extra, data, sz)) {
4810 fastboot_fail("flash write failure");
4811 return;
4812 }
Dima Zavin214cc642009-01-26 11:16:21 -08004813 }
4814 dprintf(INFO, "partition '%s' updated\n", ptn->name);
4815 fastboot_okay("");
4816}
4817
Kishor PK38ed93d2017-04-25 14:19:26 +05304818
4819static inline uint64_t validate_partition_size(struct ptentry *ptn)
4820{
4821 if (ptn->length && flash_num_pages_per_blk() && page_size) {
4822 if ((ptn->length < ( UINT_MAX / flash_num_pages_per_blk())) && ((ptn->length * flash_num_pages_per_blk()) < ( UINT_MAX / page_size))) {
4823 return ptn->length * flash_num_pages_per_blk() * page_size;
4824 }
4825 }
4826 return 0;
4827}
4828
4829
Sridhar Parasurame94e8152014-10-24 14:06:03 -07004830void cmd_flash(const char *arg, void *data, unsigned sz)
4831{
4832 if(target_is_emmc_boot())
4833 cmd_flash_mmc(arg, data, sz);
4834 else
4835 cmd_flash_nand(arg, data, sz);
4836}
4837
Dima Zavin214cc642009-01-26 11:16:21 -08004838void cmd_continue(const char *arg, void *data, unsigned sz)
4839{
4840 fastboot_okay("");
Amol Jadi7c4316c2013-10-07 14:19:26 -07004841 fastboot_stop();
Shashank Mittald3e54dd2014-08-28 15:24:02 -07004842
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004843 if (target_is_emmc_boot())
4844 {
lijuanga40d6302015-07-20 20:10:13 +08004845#if FBCON_DISPLAY_MSG
lijuangde34d502016-02-26 16:04:50 +08004846 /* Exit keys' detection thread firstly */
4847 exit_menu_keys_detection();
lijuanga40d6302015-07-20 20:10:13 +08004848#endif
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004849 boot_linux_from_mmc();
4850 }
4851 else
4852 {
4853 boot_linux_from_flash();
4854 }
Dima Zavin214cc642009-01-26 11:16:21 -08004855}
4856
Chandan Uddaraju94183c02010-01-15 15:13:59 -08004857void cmd_reboot(const char *arg, void *data, unsigned sz)
4858{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004859 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08004860 fastboot_okay("");
4861 reboot_device(0);
4862}
4863
Mayank Grover351a75e2017-05-30 20:06:08 +05304864void cmd_set_active(const char *arg, void *data, unsigned sz)
4865{
Mayank Grover6ccc1c92017-07-04 17:36:46 +05304866 char *p, *sp = NULL;
Mayank Grover351a75e2017-05-30 20:06:08 +05304867 unsigned i,current_active_slot;
4868 const char *current_slot_suffix;
4869
4870 if (!partition_multislot_is_supported())
4871 {
4872 fastboot_fail("Command not supported");
4873 return;
4874 }
4875
Rahul Shaharef7a5f8b2021-02-04 16:51:36 +05304876 if (target_virtual_ab_supported()) {
4877 if (GetSnapshotMergeStatus () == MERGING) {
4878 fastboot_fail ("Slot Change is not allowed in merging state");
4879 return;
4880 }
4881 }
4882
Mayank Grover351a75e2017-05-30 20:06:08 +05304883 if (arg)
4884 {
Mayank Grover6ccc1c92017-07-04 17:36:46 +05304885 p = strtok_r((char *)arg, ":", &sp);
Mayank Grover5eb5f692017-07-19 20:16:04 +05304886 if (p)
Mayank Grover351a75e2017-05-30 20:06:08 +05304887 {
4888 current_active_slot = partition_find_active_slot();
4889
4890 /* Check if trying to make curent slot active */
4891 current_slot_suffix = SUFFIX_SLOT(current_active_slot);
Mayank Grover6ccc1c92017-07-04 17:36:46 +05304892 current_slot_suffix = strtok_r((char *)current_slot_suffix,
4893 (char *)suffix_delimiter, &sp);
4894
Mayank Grover5eb5f692017-07-19 20:16:04 +05304895 if (current_slot_suffix &&
4896 !strncmp(p, current_slot_suffix, strlen(current_slot_suffix)))
Mayank Grover351a75e2017-05-30 20:06:08 +05304897 {
4898 fastboot_okay("Slot already set active");
4899 return;
4900 }
4901 else
4902 {
4903 for (i = 0; i < AB_SUPPORTED_SLOTS; i++)
4904 {
4905 current_slot_suffix = SUFFIX_SLOT(i);
Mayank Grover6ccc1c92017-07-04 17:36:46 +05304906 current_slot_suffix = strtok_r((char *)current_slot_suffix,
4907 (char *)suffix_delimiter, &sp);
Mayank Grover5eb5f692017-07-19 20:16:04 +05304908 if (current_slot_suffix &&
4909 !strncmp(p, current_slot_suffix, strlen(current_slot_suffix)))
Mayank Grover351a75e2017-05-30 20:06:08 +05304910 {
lijuang8b03e5f2019-07-12 18:16:19 +08004911 partition_switch_slots(current_active_slot, i, true);
Mayank Grover351a75e2017-05-30 20:06:08 +05304912 publish_getvar_multislot_vars();
4913 fastboot_okay("");
4914 return;
4915 }
4916 }
4917 }
4918 }
4919 }
4920 fastboot_fail("Invalid slot suffix.");
4921 return;
4922}
4923
Mayank Grover98c4c742019-04-25 17:21:37 +05304924#if DYNAMIC_PARTITION_SUPPORT
4925void cmd_reboot_fastboot(const char *arg, void *data, unsigned sz)
4926{
4927 dprintf(INFO, "rebooting the device - userspace fastboot\n");
4928 if (send_recovery_cmd(RECOVERY_BOOT_FASTBOOT_CMD)) {
4929 dprintf(CRITICAL, "ERROR: Failed to update recovery commands\n");
4930 fastboot_fail("Failed to update recovery command");
4931 return;
4932 }
4933 fastboot_okay("");
4934 reboot_device(REBOOT_MODE_UNKNOWN);
4935
4936 //shouldn't come here.
4937 dprintf(CRITICAL, "ERROR: Failed to reboot device\n");
4938 return;
4939}
4940
4941void cmd_reboot_recovery(const char *arg, void *data, unsigned sz)
4942{
4943 dprintf(INFO, "rebooting the device - recovery\n");
4944 if (send_recovery_cmd(RECOVERY_BOOT_RECOVERY_CMD)) {
4945 dprintf(CRITICAL, "ERROR: Failed to update recovery commands\n");
4946 fastboot_fail("Failed to update recovery command");
4947 return;
4948 }
4949 fastboot_okay("");
4950 reboot_device(REBOOT_MODE_UNKNOWN);
4951
4952 //shouldn't come here.
4953 dprintf(CRITICAL, "ERROR: Failed to reboot device\n");
4954 return;
4955}
Rahul Shaharef7a5f8b2021-02-04 16:51:36 +05304956
4957#ifdef VIRTUAL_AB_OTA
4958void CmdUpdateSnapshot(const char *arg, void *data, unsigned sz)
4959{
4960 char *command = NULL;
4961 const char *delim = ":";
4962 char *sp;
4963
4964 if (arg) {
4965 command = strtok_r((char *)arg, delim, &sp);
4966 if (command) {
4967 command++;
4968
4969 if(!strncmp (command, "merge", AsciiStrLen ("merge"))) {
4970 if (GetSnapshotMergeStatus () == MERGING) {
4971 cmd_reboot_fastboot(Arg, Data, Size);
4972 }
4973 FastbootOkay ("");
4974 return;
4975 }
4976 else if (!strncmp (Command, "cancel", AsciiStrLen ("cancel"))) {
4977 if(!device.is_unlocked) {
4978 fastboot_fail ("Snapshot Cancel is not allowed in Lock State");
4979 return;
4980 }
4981
4982 if (SetSnapshotMergeStatus (CANCELLED))
4983 {
4984 fastboot_fail("Failed to update snapshot state to cancel");
4985 return;
4986 }
4987
4988 //updating fbvar snapshot-merge-state
4989 snprintf(SnapshotMergeState, strlen(VabSnapshotMergeStatus[NONE_MERGE_STATUS]) + 1,
4990 "%s", VabSnapshotMergeStatus[NONE_MERGE_STATUS]);
4991 fastboot_okay("");
4992 return;
4993 }
4994 }
4995 }
4996 fastboot_fail("Invalid snapshot-update command");
4997 return;
4998}
4999#endif
Mayank Grover98c4c742019-04-25 17:21:37 +05305000#endif
5001
Chandan Uddaraju94183c02010-01-15 15:13:59 -08005002void cmd_reboot_bootloader(const char *arg, void *data, unsigned sz)
5003{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07005004 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08005005 fastboot_okay("");
5006 reboot_device(FASTBOOT_MODE);
5007}
5008
Ameya Thakur11cf1a62013-08-05 12:44:48 -07005009void cmd_oem_enable_charger_screen(const char *arg, void *data, unsigned size)
5010{
5011 dprintf(INFO, "Enabling charger screen check\n");
5012 device.charger_screen_enabled = 1;
5013 write_device_info(&device);
5014 fastboot_okay("");
5015}
5016
5017void cmd_oem_disable_charger_screen(const char *arg, void *data, unsigned size)
5018{
5019 dprintf(INFO, "Disabling charger screen check\n");
5020 device.charger_screen_enabled = 0;
5021 write_device_info(&device);
5022 fastboot_okay("");
5023}
5024
tracychui9d60fc52020-06-05 17:31:38 +08005025#if defined(ENABLE_PRODINFO_ACCESS)
5026void CmdOemEnableAdb(const char *arg, void *data, unsigned size)
5027{
5028 dprintf(INFO, "Enabling Adb\n");
5029 prod.is_adb_enabled = 1;
5030 write_prod_info(&prod);
5031 fastboot_okay("");
5032}
5033#endif
5034
lijuanga25d8bb2015-09-16 13:11:52 +08005035void cmd_oem_off_mode_charger(const char *arg, void *data, unsigned size)
5036{
5037 char *p = NULL;
5038 const char *delim = " \t\n\r";
Parth Dixit407f15b2016-01-07 14:47:37 +05305039 char *sp;
lijuanga25d8bb2015-09-16 13:11:52 +08005040
5041 if (arg) {
Parth Dixit407f15b2016-01-07 14:47:37 +05305042 p = strtok_r((char *)arg, delim, &sp);
lijuanga25d8bb2015-09-16 13:11:52 +08005043 if (p) {
5044 if (!strncmp(p, "0", 1)) {
5045 device.charger_screen_enabled = 0;
5046 } else if (!strncmp(p, "1", 1)) {
5047 device.charger_screen_enabled = 1;
5048 }
5049 }
5050 }
5051
5052 /* update charger_screen_enabled value for getvar
5053 * command
5054 */
5055 snprintf(charger_screen_enabled, MAX_RSP_SIZE, "%d",
5056 device.charger_screen_enabled);
5057
5058 write_device_info(&device);
5059 fastboot_okay("");
5060}
5061
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05305062void cmd_oem_select_display_panel(const char *arg, void *data, unsigned size)
5063{
5064 dprintf(INFO, "Selecting display panel %s\n", arg);
5065 if (arg)
5066 strlcpy(device.display_panel, arg,
5067 sizeof(device.display_panel));
5068 write_device_info(&device);
5069 fastboot_okay("");
5070}
5071
Shashank Mittal162244e2011-08-08 19:01:25 -07005072void cmd_oem_unlock(const char *arg, void *data, unsigned sz)
5073{
lijuang4ece1e72015-08-14 21:02:36 +08005074 set_device_unlock(UNLOCK, TRUE);
vijay kumarc65876c2015-04-24 13:29:16 +05305075}
5076
5077void cmd_oem_unlock_go(const char *arg, void *data, unsigned sz)
5078{
lijuang4ece1e72015-08-14 21:02:36 +08005079 if(!device.is_unlocked) {
vijay kumarc65876c2015-04-24 13:29:16 +05305080 if(!is_allow_unlock) {
5081 fastboot_fail("oem unlock is not allowed");
5082 return;
5083 }
5084
lijuang4ece1e72015-08-14 21:02:36 +08005085 set_device_unlock_value(UNLOCK, TRUE);
vijay kumarc65876c2015-04-24 13:29:16 +05305086
lijuang4ece1e72015-08-14 21:02:36 +08005087 /* wipe data */
vijay kumarc65876c2015-04-24 13:29:16 +05305088 struct recovery_message msg;
Kishor PK60a68212017-05-08 16:55:57 +05305089 memset(&msg, 0, sizeof(msg));
vijay kumarc65876c2015-04-24 13:29:16 +05305090 snprintf(msg.recovery, sizeof(msg.recovery), "recovery\n--wipe_data");
5091 write_misc(0, &msg, sizeof(msg));
5092
5093 fastboot_okay("");
5094 reboot_device(RECOVERY_MODE);
Shashank Mittald3e54dd2014-08-28 15:24:02 -07005095 }
5096 fastboot_okay("");
5097}
5098
Channagoud Kadabiad259832015-05-29 11:14:17 -07005099static int aboot_frp_unlock(char *pname, void *data, unsigned sz)
5100{
Mayank Grover70f8b0e2017-05-17 11:49:00 +05305101 int ret=1;
5102 bool authentication_success=false;
Channagoud Kadabiad259832015-05-29 11:14:17 -07005103
Mayank Grover70f8b0e2017-05-17 11:49:00 +05305104 /*
5105 Authentication method not implemented.
5106
5107 OEM to implement, authentication system which on successful validataion,
5108 calls write_allow_oem_unlock() with is_allow_unlock.
5109 */
5110#if 0
5111 authentication_success = oem_specific_auth_mthd();
5112#endif
5113
5114 if (authentication_success)
Channagoud Kadabiad259832015-05-29 11:14:17 -07005115 {
Mayank Grover70f8b0e2017-05-17 11:49:00 +05305116 is_allow_unlock = true;
5117 write_allow_oem_unlock(is_allow_unlock);
5118 ret = 0;
Channagoud Kadabiad259832015-05-29 11:14:17 -07005119 }
5120 return ret;
5121}
5122
Shashank Mittald3e54dd2014-08-28 15:24:02 -07005123void cmd_oem_lock(const char *arg, void *data, unsigned sz)
5124{
lijuang4ece1e72015-08-14 21:02:36 +08005125 set_device_unlock(UNLOCK, FALSE);
Shashank Mittal162244e2011-08-08 19:01:25 -07005126}
5127
Shashank Mittala0032282011-08-26 14:50:11 -07005128void cmd_oem_devinfo(const char *arg, void *data, unsigned sz)
5129{
lijuang511a2b52015-08-14 20:50:51 +08005130 char response[MAX_RSP_SIZE];
Ameya Thakur11cf1a62013-08-05 12:44:48 -07005131 snprintf(response, sizeof(response), "\tDevice tampered: %s", (device.is_tampered ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07005132 fastboot_info(response);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07005133 snprintf(response, sizeof(response), "\tDevice unlocked: %s", (device.is_unlocked ? "true" : "false"));
5134 fastboot_info(response);
Monika Singh292b3e92018-03-17 22:40:23 +05305135#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover912eaa62017-10-26 12:08:53 +05305136 if (VB_M <= target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +05305137 {
5138 snprintf(response, sizeof(response), "\tDevice critical unlocked: %s",
5139 (device.is_unlock_critical ? "true" : "false"));
5140 fastboot_info(response);
5141 }
Parth Dixitddbc7352015-10-18 03:13:31 +05305142#endif
Ameya Thakur11cf1a62013-08-05 12:44:48 -07005143 snprintf(response, sizeof(response), "\tCharger screen enabled: %s", (device.charger_screen_enabled ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07005144 fastboot_info(response);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05305145 snprintf(response, sizeof(response), "\tDisplay panel: %s", (device.display_panel));
5146 fastboot_info(response);
tracychui9d60fc52020-06-05 17:31:38 +08005147#if defined(ENABLE_PRODINFO_ACCESS)
5148 snprintf(response, sizeof(response), "\tAdb enabled: %s", prod.is_adb_enabled ? "true" : "false");
5149 fastboot_info(response);
5150#endif
Shashank Mittala0032282011-08-26 14:50:11 -07005151 fastboot_okay("");
5152}
5153
lijuang511a2b52015-08-14 20:50:51 +08005154void cmd_flashing_get_unlock_ability(const char *arg, void *data, unsigned sz)
5155{
5156 char response[MAX_RSP_SIZE];
5157 snprintf(response, sizeof(response), "\tget_unlock_ability: %d", is_allow_unlock);
5158 fastboot_info(response);
5159 fastboot_okay("");
5160}
5161
5162void cmd_flashing_lock_critical(const char *arg, void *data, unsigned sz)
5163{
lijuang4ece1e72015-08-14 21:02:36 +08005164 set_device_unlock(UNLOCK_CRITICAL, FALSE);
lijuang511a2b52015-08-14 20:50:51 +08005165}
5166
5167void cmd_flashing_unlock_critical(const char *arg, void *data, unsigned sz)
5168{
lijuang4ece1e72015-08-14 21:02:36 +08005169 set_device_unlock(UNLOCK_CRITICAL, TRUE);
lijuang511a2b52015-08-14 20:50:51 +08005170}
5171
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07005172void cmd_preflash(const char *arg, void *data, unsigned sz)
5173{
5174 fastboot_okay("");
5175}
5176
Mao Flynn7b379f32015-04-20 00:28:30 +08005177static uint8_t logo_header[LOGO_IMG_HEADER_SIZE];
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05305178
Mao Flynn7b379f32015-04-20 00:28:30 +08005179int splash_screen_check_header(logo_img_header *header)
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05305180{
Mao Flynn7b379f32015-04-20 00:28:30 +08005181 if (memcmp(header->magic, LOGO_IMG_MAGIC, 8))
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05305182 return -1;
Mao Flynn7b379f32015-04-20 00:28:30 +08005183 if (header->width == 0 || header->height == 0)
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05305184 return -1;
Ashish Bhimanpalliwar36f9f002021-04-14 13:50:52 +05305185 if (((header->blocks == 0) || (header->blocks > UINT_MAX/512))) {
Ashish Bhimanpalliwareab60c62020-10-09 16:31:11 +05305186 return -1;
5187 }
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05305188 return 0;
5189}
5190
Mao Flynn7b379f32015-04-20 00:28:30 +08005191int splash_screen_flash()
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07005192{
5193 struct ptentry *ptn;
5194 struct ptable *ptable;
Mao Flynn7b379f32015-04-20 00:28:30 +08005195 struct logo_img_header *header;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07005196 struct fbcon_config *fb_display = NULL;
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08005197
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05305198 ptable = flash_get_ptable();
5199 if (ptable == NULL) {
Mao Flynn7b379f32015-04-20 00:28:30 +08005200 dprintf(CRITICAL, "ERROR: Partition table not found\n");
5201 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05305202 }
Mao Flynn7b379f32015-04-20 00:28:30 +08005203
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05305204 ptn = ptable_find(ptable, "splash");
5205 if (ptn == NULL) {
5206 dprintf(CRITICAL, "ERROR: splash Partition not found\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08005207 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05305208 }
Mao Flynn7b379f32015-04-20 00:28:30 +08005209 if (flash_read(ptn, 0, (void *)logo_header, LOGO_IMG_HEADER_SIZE)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05305210 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08005211 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05305212 }
5213
Mao Flynn7b379f32015-04-20 00:28:30 +08005214 header = (struct logo_img_header *)logo_header;
5215 if (splash_screen_check_header(header)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05305216 dprintf(CRITICAL, "ERROR: Boot image header invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08005217 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05305218 }
5219
5220 fb_display = fbcon_display();
5221 if (fb_display) {
Ashish Bhimanpalliwareab60c62020-10-09 16:31:11 +05305222 if (header->type &&
5223 ((header->blocks * 512) <= (fb_display->width *
5224 fb_display->height * (fb_display->bpp / 8)))) {
raghavendra ambadas554f4fd2018-10-09 15:50:40 +05305225 /* RLE24 compressed data */
Mao Flynn7b379f32015-04-20 00:28:30 +08005226 uint8_t *base = (uint8_t *) fb_display->base + LOGO_IMG_OFFSET;
5227
5228 /* if the logo is full-screen size, remove "fbcon_clear()" */
5229 if ((header->width != fb_display->width)
5230 || (header->height != fb_display->height))
5231 fbcon_clear();
5232
5233 if (flash_read(ptn + LOGO_IMG_HEADER_SIZE, 0,
5234 (uint32_t *)base,
5235 (header->blocks * 512))) {
5236 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
5237 return -1;
5238 }
5239 fbcon_extract_to_screen(header, base);
5240 return 0;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07005241 }
Mao Flynn7b379f32015-04-20 00:28:30 +08005242
Veera Sundaram Sankaranc33416f2015-09-28 10:25:55 -07005243 if ((header->width > fb_display->width) || (header->height > fb_display->height)) {
5244 dprintf(CRITICAL, "Logo config greater than fb config. Fall back default logo\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08005245 return -1;
5246 }
5247
Vineet Bajaj99291ed2014-09-09 12:29:46 +05305248 uint8_t *base = (uint8_t *) fb_display->base;
Sachin Bhayare619e9e42017-05-15 13:10:31 +05305249 uint32_t fb_size = ROUNDUP(fb_display->width *
5250 fb_display->height *
5251 (fb_display->bpp / 8), 4096);
5252 uint32_t splash_size = ((((header->width * header->height *
5253 fb_display->bpp/8) + 511) >> 9) << 9);
5254
Ashish Bhimanpalliwar36f9f002021-04-14 13:50:52 +05305255 if ((header->height * header->width * (fb_display->bpp/8)) > (header->blocks * 512)) {
5256 dprintf(CRITICAL, "ERROR: Splash image size invalid\n");
5257 return -1;
5258 }
5259
Sachin Bhayare619e9e42017-05-15 13:10:31 +05305260 if (splash_size > fb_size) {
5261 dprintf(CRITICAL, "ERROR: Splash image size invalid\n");
5262 return -1;
5263 }
5264
Mao Flynn7b379f32015-04-20 00:28:30 +08005265 if (flash_read(ptn + LOGO_IMG_HEADER_SIZE, 0,
5266 (uint32_t *)base,
5267 ((((header->width * header->height * fb_display->bpp/8) + 511) >> 9) << 9))) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05305268 fbcon_clear();
Vineet Bajaj99291ed2014-09-09 12:29:46 +05305269 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08005270 return -1;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07005271 }
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05305272 }
5273
Mao Flynn7b379f32015-04-20 00:28:30 +08005274 return 0;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05305275}
5276
Mao Flynn7b379f32015-04-20 00:28:30 +08005277int splash_screen_mmc()
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05305278{
5279 int index = INVALID_PTN;
5280 unsigned long long ptn = 0;
5281 struct fbcon_config *fb_display = NULL;
Mao Flynn7b379f32015-04-20 00:28:30 +08005282 struct logo_img_header *header;
Mao Flynn1893a7f2015-06-03 12:03:36 +08005283 uint32_t blocksize, realsize, readsize;
5284 uint8_t *base;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05305285
5286 index = partition_get_index("splash");
5287 if (index == 0) {
5288 dprintf(CRITICAL, "ERROR: splash Partition table not found\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08005289 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05305290 }
5291
5292 ptn = partition_get_offset(index);
5293 if (ptn == 0) {
5294 dprintf(CRITICAL, "ERROR: splash Partition invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08005295 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05305296 }
5297
Mao Flynn1893a7f2015-06-03 12:03:36 +08005298 mmc_set_lun(partition_get_lun(index));
5299
5300 blocksize = mmc_get_device_blocksize();
5301 if (blocksize == 0) {
5302 dprintf(CRITICAL, "ERROR:splash Partition invalid blocksize\n");
5303 return -1;
5304 }
5305
5306 fb_display = fbcon_display();
Channagoud Kadabidaf10a62015-07-22 11:51:55 -07005307 if (!fb_display)
5308 {
5309 dprintf(CRITICAL, "ERROR: fb config is not allocated\n");
5310 return -1;
5311 }
5312
Mao Flynn1893a7f2015-06-03 12:03:36 +08005313 base = (uint8_t *) fb_display->base;
5314
jialongjhan2769d202020-05-22 18:25:56 +08005315 if (mmc_read(ptn , (uint32_t *)(base + LOGO_IMG_OFFSET), blocksize)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05305316 dprintf(CRITICAL, "ERROR: Cannot read splash image header\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08005317 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05305318 }
Mao Flynn1893a7f2015-06-03 12:03:36 +08005319 header = (struct logo_img_header *)(base + LOGO_IMG_OFFSET);
Mao Flynn7b379f32015-04-20 00:28:30 +08005320 if (splash_screen_check_header(header)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05305321 dprintf(CRITICAL, "ERROR: Splash image header invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08005322 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05305323 }
5324
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05305325 if (fb_display) {
Ashish Bhimanpalliwareab60c62020-10-09 16:31:11 +05305326 if (header->type &&
Ashish Bhimanpalliwarce39f482020-10-06 15:27:37 +05305327 (((UINT_MAX - LOGO_IMG_HEADER_SIZE) / 512) >= header->blocks) &&
raghavendra ambadas554f4fd2018-10-09 15:50:40 +05305328 ((header->blocks * 512) <= (fb_display->width *
5329 fb_display->height * (fb_display->bpp / 8)))) {
5330 /* 1 RLE24 compressed data */
Mao Flynn1893a7f2015-06-03 12:03:36 +08005331 base += LOGO_IMG_OFFSET;
Mao Flynn7b379f32015-04-20 00:28:30 +08005332
Mao Flynn1893a7f2015-06-03 12:03:36 +08005333 realsize = header->blocks * 512;
5334 readsize = ROUNDUP((realsize + LOGO_IMG_HEADER_SIZE), blocksize) - blocksize;
5335
5336 /* if the logo is not full-screen size, clean screen */
Mao Flynn7b379f32015-04-20 00:28:30 +08005337 if ((header->width != fb_display->width)
5338 || (header->height != fb_display->height))
5339 fbcon_clear();
5340
Sachin Bhayare619e9e42017-05-15 13:10:31 +05305341 uint32_t fb_size = ROUNDUP(fb_display->width *
5342 fb_display->height *
5343 (fb_display->bpp / 8), 4096);
5344
5345 if (readsize > fb_size) {
5346 dprintf(CRITICAL, "ERROR: Splash image size invalid\n");
5347 return -1;
5348 }
5349
jialongjhan2769d202020-05-22 18:25:56 +08005350 if (mmc_read(ptn + blocksize, (uint32_t *)(base + blocksize), readsize)) {
Mao Flynn7b379f32015-04-20 00:28:30 +08005351 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
5352 return -1;
5353 }
Mao Flynn7b379f32015-04-20 00:28:30 +08005354
Mao Flynn1893a7f2015-06-03 12:03:36 +08005355 fbcon_extract_to_screen(header, (base + LOGO_IMG_HEADER_SIZE));
5356 } else { /* 2 Raw BGR data */
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05305357
Veera Sundaram Sankaranc33416f2015-09-28 10:25:55 -07005358 if ((header->width > fb_display->width) || (header->height > fb_display->height)) {
5359 dprintf(CRITICAL, "Logo config greater than fb config. Fall back default logo\n");
Mao Flynn1893a7f2015-06-03 12:03:36 +08005360 return -1;
5361 }
5362
5363 realsize = header->width * header->height * fb_display->bpp / 8;
5364 readsize = ROUNDUP((realsize + LOGO_IMG_HEADER_SIZE), blocksize) - blocksize;
Ashish Bhimanpalliwar36f9f002021-04-14 13:50:52 +05305365 if (realsize > (header->blocks * 512)) {
5366 dprintf(CRITICAL, "Logo Size error\n");
5367 return -1;
5368 }
Mao Flynn1893a7f2015-06-03 12:03:36 +08005369
5370 if (blocksize == LOGO_IMG_HEADER_SIZE) { /* read the content directly */
Kishor PKee5c0a32018-03-06 16:49:46 +05305371 if (mmc_read((ptn + PLL_CODES_OFFSET + LOGO_IMG_HEADER_SIZE), (uint32_t *)base, readsize)) {
Mao Flynn1893a7f2015-06-03 12:03:36 +08005372 fbcon_clear();
5373 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
5374 return -1;
5375 }
5376 } else {
Kishor PKee5c0a32018-03-06 16:49:46 +05305377 if (mmc_read(ptn + PLL_CODES_OFFSET + blocksize ,
Mao Flynn1893a7f2015-06-03 12:03:36 +08005378 (uint32_t *)(base + LOGO_IMG_OFFSET + blocksize), readsize)) {
5379 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
5380 return -1;
5381 }
5382 memmove(base, (base + LOGO_IMG_OFFSET + LOGO_IMG_HEADER_SIZE), realsize);
5383 }
5384 }
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05305385 }
5386
Mao Flynn7b379f32015-04-20 00:28:30 +08005387 return 0;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05305388}
5389
Mao Flynn7b379f32015-04-20 00:28:30 +08005390int fetch_image_from_partition()
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05305391{
5392 if (target_is_emmc_boot()) {
5393 return splash_screen_mmc();
5394 } else {
5395 return splash_screen_flash();
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07005396 }
5397}
5398
Mayank Grover351a75e2017-05-30 20:06:08 +05305399void publish_getvar_multislot_vars()
5400{
5401 int i,count;
5402 static bool published = false;
5403 static char slot_count[MAX_RSP_SIZE];
5404 static struct ab_slot_info slot_info[AB_SUPPORTED_SLOTS];
5405 static char active_slot_suffix[MAX_RSP_SIZE];
5406 static char has_slot_pname[NUM_PARTITIONS][MAX_GET_VAR_NAME_SIZE];
5407 static char has_slot_reply[NUM_PARTITIONS][MAX_RSP_SIZE];
5408 const char *tmp;
5409 char tmpbuff[MAX_GET_VAR_NAME_SIZE];
Mayank Grover2bd84bb2017-06-20 11:06:07 +05305410 signed active_slt;
Mayank Grover351a75e2017-05-30 20:06:08 +05305411
5412 if (!published)
5413 {
5414 /* Update slot meta info */
5415 count = partition_fill_partition_meta(has_slot_pname, has_slot_reply,
5416 partition_get_partition_count());
5417 for(i=0; i<count; i++)
5418 {
5419 memset(tmpbuff, 0, MAX_GET_VAR_NAME_SIZE);
Mayank Grover2bd84bb2017-06-20 11:06:07 +05305420 snprintf(tmpbuff, MAX_GET_VAR_NAME_SIZE,"has-slot:%s",
5421 has_slot_pname[i]);
5422 strlcpy(has_slot_pname[i], tmpbuff, MAX_GET_VAR_NAME_SIZE);
Mayank Grover351a75e2017-05-30 20:06:08 +05305423 fastboot_publish(has_slot_pname[i], has_slot_reply[i]);
5424 }
5425
5426 for (i=0; i<AB_SUPPORTED_SLOTS; i++)
5427 {
5428 tmp = SUFFIX_SLOT(i);
Mayank Grover7b8a8f82017-10-27 13:07:59 +05305429 tmp++; // to remove "_" from slot_suffix.
Mayank Grover2bd84bb2017-06-20 11:06:07 +05305430 snprintf(slot_info[i].slot_is_unbootable, sizeof(slot_info[i].slot_is_unbootable),
5431 "slot-unbootable:%s", tmp);
5432 snprintf(slot_info[i].slot_is_active, sizeof(slot_info[i].slot_is_active),
5433 "slot-active:%s", tmp);
5434 snprintf(slot_info[i].slot_is_succesful, sizeof(slot_info[i].slot_is_succesful),
5435 "slot-success:%s", tmp);
5436 snprintf(slot_info[i].slot_retry_count, sizeof(slot_info[i].slot_retry_count),
5437 "slot-retry-count:%s", tmp);
Mayank Grover351a75e2017-05-30 20:06:08 +05305438 fastboot_publish(slot_info[i].slot_is_unbootable,
5439 slot_info[i].slot_is_unbootable_rsp);
5440 fastboot_publish(slot_info[i].slot_is_active,
5441 slot_info[i].slot_is_active_rsp);
5442 fastboot_publish(slot_info[i].slot_is_succesful,
5443 slot_info[i].slot_is_succesful_rsp);
5444 fastboot_publish(slot_info[i].slot_retry_count,
5445 slot_info[i].slot_retry_count_rsp);
5446 }
5447 fastboot_publish("current-slot", active_slot_suffix);
5448 snprintf(slot_count, sizeof(slot_count),"%d", AB_SUPPORTED_SLOTS);
5449 fastboot_publish("slot-count", slot_count);
5450 published = true;
5451 }
5452
Mayank Grover2bd84bb2017-06-20 11:06:07 +05305453 active_slt = partition_find_active_slot();
5454 if (active_slt != INVALID)
Mayank Grover7b8a8f82017-10-27 13:07:59 +05305455 {
5456 tmp = SUFFIX_SLOT(active_slt);
5457 tmp++; // to remove "_" from slot_suffix.
5458 snprintf(active_slot_suffix, sizeof(active_slot_suffix), "%s", tmp);
5459 }
Mayank Grover2bd84bb2017-06-20 11:06:07 +05305460 else
5461 strlcpy(active_slot_suffix, "INVALID", sizeof(active_slot_suffix));
5462
Mayank Grover351a75e2017-05-30 20:06:08 +05305463 /* Update partition meta information */
5464 partition_fill_slot_meta(slot_info);
5465 return;
5466}
5467
lijuang4ece1e72015-08-14 21:02:36 +08005468void get_product_name(unsigned char *buf)
5469{
5470 snprintf((char*)buf, MAX_RSP_SIZE, "%s", TARGET(BOARD));
5471 return;
5472}
5473
Mayank Grover7b8a8f82017-10-27 13:07:59 +05305474#if PRODUCT_IOT
5475void get_bootloader_version_iot(unsigned char *buf)
5476{
5477 if (buf != NULL)
5478 {
5479 strlcpy(buf, TARGET(BOARD), MAX_VERSION_LEN);
5480 strlcat(buf, "-", MAX_VERSION_LEN);
5481 strlcat(buf, PRODUCT_IOT_VERSION, MAX_VERSION_LEN);
5482 }
5483 return;
5484}
5485#endif
5486
lijuang4ece1e72015-08-14 21:02:36 +08005487void get_bootloader_version(unsigned char *buf)
5488{
jhchena8a15dd2020-04-24 15:45:57 +08005489 snprintf((char*)buf, MAX_RSP_SIZE, "%s", device.bootloader_version);
lijuang4ece1e72015-08-14 21:02:36 +08005490 return;
5491}
5492
5493void get_baseband_version(unsigned char *buf)
5494{
jhchena8a15dd2020-04-24 15:45:57 +08005495 snprintf((char*)buf, MAX_RSP_SIZE, "%s", device.radio_version);
lijuang4ece1e72015-08-14 21:02:36 +08005496 return;
5497}
5498
Monika Singh32a09f72018-03-14 13:08:29 +05305499bool is_device_locked_critical()
5500{
5501 return device.is_unlock_critical ? false:true;
5502}
5503
lijuang4ece1e72015-08-14 21:02:36 +08005504bool is_device_locked()
5505{
5506 return device.is_unlocked ? false:true;
5507}
5508
Monika Singh32a09f72018-03-14 13:08:29 +05305509bool is_verity_enforcing()
5510{
5511 return device.verity_mode ? true:false;
5512}
5513
Amol Jadi5edf3552013-07-23 14:15:34 -07005514/* register commands and variables for fastboot */
5515void aboot_fastboot_register_commands(void)
5516{
Sridhar Parasurame94e8152014-10-24 14:06:03 -07005517 int i;
lijuangf16461c2015-08-03 17:09:34 +08005518 char hw_platform_buf[MAX_RSP_SIZE];
Rahul Shaharee7baede2021-02-02 13:23:57 +05305519 VirtualAbMergeStatus SnapshotMergeStatus;
Amol Jadi5edf3552013-07-23 14:15:34 -07005520
Sridhar Parasurame94e8152014-10-24 14:06:03 -07005521 struct fastboot_cmd_desc cmd_list[] = {
lijuang511a2b52015-08-14 20:50:51 +08005522 /* By default the enabled list is empty. */
5523 {"", NULL},
5524 /* move commands enclosed within the below ifndef to here
5525 * if they need to be enabled in user build.
5526 */
Sridhar Parasurame94e8152014-10-24 14:06:03 -07005527#ifndef DISABLE_FASTBOOT_CMDS
lijuang511a2b52015-08-14 20:50:51 +08005528 /* Register the following commands only for non-user builds */
5529 {"flash:", cmd_flash},
5530 {"erase:", cmd_erase},
5531 {"boot", cmd_boot},
5532 {"continue", cmd_continue},
5533 {"reboot", cmd_reboot},
5534 {"reboot-bootloader", cmd_reboot_bootloader},
5535 {"oem unlock", cmd_oem_unlock},
5536 {"oem unlock-go", cmd_oem_unlock_go},
5537 {"oem lock", cmd_oem_lock},
5538 {"flashing unlock", cmd_oem_unlock},
5539 {"flashing lock", cmd_oem_lock},
5540 {"flashing lock_critical", cmd_flashing_lock_critical},
5541 {"flashing unlock_critical", cmd_flashing_unlock_critical},
5542 {"flashing get_unlock_ability", cmd_flashing_get_unlock_ability},
5543 {"oem device-info", cmd_oem_devinfo},
5544 {"preflash", cmd_preflash},
5545 {"oem enable-charger-screen", cmd_oem_enable_charger_screen},
5546 {"oem disable-charger-screen", cmd_oem_disable_charger_screen},
lijuanga25d8bb2015-09-16 13:11:52 +08005547 {"oem off-mode-charge", cmd_oem_off_mode_charger},
lijuang511a2b52015-08-14 20:50:51 +08005548 {"oem select-display-panel", cmd_oem_select_display_panel},
Mayank Grover6ccc1c92017-07-04 17:36:46 +05305549 {"set_active",cmd_set_active},
Mayank Grover98c4c742019-04-25 17:21:37 +05305550#if DYNAMIC_PARTITION_SUPPORT
5551 {"reboot-fastboot",cmd_reboot_fastboot},
5552 {"reboot-recovery",cmd_reboot_recovery},
5553#endif
Rahul Shaharef7a5f8b2021-02-04 16:51:36 +05305554#ifdef VIRTUAL_AB_OTA
5555 {"snapshot-update", CmdUpdateSnapshot},
5556#endif
Channagoud Kadabi736c4962015-08-21 11:56:52 -07005557#if UNITTEST_FW_SUPPORT
Channagoud Kadabid5ddf482015-09-28 10:31:35 -07005558 {"oem run-tests", cmd_oem_runtests},
Channagoud Kadabi736c4962015-08-21 11:56:52 -07005559#endif
Sridhar Parasurame94e8152014-10-24 14:06:03 -07005560#endif
tracychui9d60fc52020-06-05 17:31:38 +08005561#if defined(ENABLE_PRODINFO_ACCESS)
5562 {"oem adb_enable", CmdOemEnableAdb},
5563#endif
lijuang511a2b52015-08-14 20:50:51 +08005564 };
Sridhar Parasurame94e8152014-10-24 14:06:03 -07005565
5566 int fastboot_cmds_count = sizeof(cmd_list)/sizeof(cmd_list[0]);
5567 for (i = 1; i < fastboot_cmds_count; i++)
5568 fastboot_register(cmd_list[i].name,cmd_list[i].cb);
5569
Amol Jadi5edf3552013-07-23 14:15:34 -07005570 /* publish variables and their values */
5571 fastboot_publish("product", TARGET(BOARD));
5572 fastboot_publish("kernel", "lk");
5573 fastboot_publish("serialno", sn_buf);
5574
Mayank Grovera64dfbe2018-05-17 14:06:34 +05305575 /*publish hw-revision major(upper 16 bits) and minor(lower 16 bits)*/
5576 snprintf(soc_version_str, MAX_RSP_SIZE, "%x", board_soc_version());
5577 fastboot_publish("hw-revision", soc_version_str);
5578
Amol Jadi5edf3552013-07-23 14:15:34 -07005579 /*
5580 * partition info is supported only for emmc partitions
5581 * Calling this for NAND prints some error messages which
5582 * is harmless but misleading. Avoid calling this for NAND
5583 * devices.
5584 */
5585 if (target_is_emmc_boot())
Mayank Grover7b8a8f82017-10-27 13:07:59 +05305586 publish_getvar_partition_info(part_info, partition_get_partition_count());
Amol Jadi5edf3552013-07-23 14:15:34 -07005587
Mayank Grover351a75e2017-05-30 20:06:08 +05305588 if (partition_multislot_is_supported())
5589 publish_getvar_multislot_vars();
5590
Amol Jadi5edf3552013-07-23 14:15:34 -07005591 /* Max download size supported */
lijuang2fbed912019-07-12 14:15:05 +08005592#if !VERIFIED_BOOT_2
Ameya Thakur11cf1a62013-08-05 12:44:48 -07005593 snprintf(max_download_size, MAX_RSP_SIZE, "\t0x%x",
5594 target_get_max_flash_size());
lijuang2fbed912019-07-12 14:15:05 +08005595#else
5596 snprintf(max_download_size, MAX_RSP_SIZE, "\t0x%x",
5597 SUB_SALT_BUFF_OFFSET(target_get_max_flash_size()));
5598#endif
5599
Amol Jadi5edf3552013-07-23 14:15:34 -07005600 fastboot_publish("max-download-size", (const char *) max_download_size);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07005601 /* Is the charger screen check enabled */
5602 snprintf(charger_screen_enabled, MAX_RSP_SIZE, "%d",
5603 device.charger_screen_enabled);
5604 fastboot_publish("charger-screen-enabled",
5605 (const char *) charger_screen_enabled);
tracychui9d60fc52020-06-05 17:31:38 +08005606#if defined(ENABLE_PRODINFO_ACCESS)
5607 read_prod_info(&prod);
5608 snprintf(AdbEnable, MAX_RSP_SIZE, "%d",
5609 prod.is_adb_enabled);
5610 fastboot_publish("adb-enabled",
5611 (const char *) AdbEnable);
5612#endif
lijuanga25d8bb2015-09-16 13:11:52 +08005613 fastboot_publish("off-mode-charge", (const char *) charger_screen_enabled);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05305614 snprintf(panel_display_mode, MAX_RSP_SIZE, "%s",
5615 device.display_panel);
5616 fastboot_publish("display-panel",
5617 (const char *) panel_display_mode);
Mukesh Ojhabb6e7f32018-03-02 15:04:06 +05305618
5619 if (target_is_emmc_boot())
5620 {
5621 mmc_blocksize = mmc_get_device_blocksize();
5622 }
5623 else
5624 {
5625 mmc_blocksize = flash_block_size();
5626 }
5627 snprintf(block_size_string, MAX_RSP_SIZE, "0x%x", mmc_blocksize);
5628 fastboot_publish("erase-block-size", (const char *) block_size_string);
5629 fastboot_publish("logical-block-size", (const char *) block_size_string);
Mayank Grover7b8a8f82017-10-27 13:07:59 +05305630#if PRODUCT_IOT
5631 get_bootloader_version_iot(&bootloader_version_string);
5632 fastboot_publish("version-bootloader", (const char *) bootloader_version_string);
5633
5634 /* Version baseband is n/a for apq iot devices */
5635 fastboot_publish("version-baseband", "N/A");
Mayank Grover7b8a8f82017-10-27 13:07:59 +05305636#else
Channagoud Kadabiaafbbfc2015-03-04 17:52:56 -08005637 fastboot_publish("version-bootloader", (const char *) device.bootloader_version);
5638 fastboot_publish("version-baseband", (const char *) device.radio_version);
Mayank Grover7b8a8f82017-10-27 13:07:59 +05305639#endif
lijuangf16461c2015-08-03 17:09:34 +08005640 fastboot_publish("secure", is_secure_boot_enable()? "yes":"no");
Mayank Grover7b8a8f82017-10-27 13:07:59 +05305641 fastboot_publish("unlocked", device.is_unlocked ? "yes":"no");
lijuangf16461c2015-08-03 17:09:34 +08005642 smem_get_hw_platform_name((unsigned char *) hw_platform_buf, sizeof(hw_platform_buf));
5643 snprintf(get_variant, MAX_RSP_SIZE, "%s %s", hw_platform_buf,
5644 target_is_emmc_boot()? "eMMC":"UFS");
5645 fastboot_publish("variant", (const char *) get_variant);
lijuang65c5a822015-08-29 16:35:36 +08005646#if CHECK_BAT_VOLTAGE
lijuang102dfa92015-10-09 18:31:03 +08005647 update_battery_status();
lijuang65c5a822015-08-29 16:35:36 +08005648 fastboot_publish("battery-voltage", (const char *) battery_voltage);
lijuang102dfa92015-10-09 18:31:03 +08005649 fastboot_publish("battery-soc-ok", (const char *) battery_soc_ok);
lijuang65c5a822015-08-29 16:35:36 +08005650#endif
Mayank Grover98c4c742019-04-25 17:21:37 +05305651 if (target_dynamic_partition_supported())
5652 fastboot_publish("is-userspace", "no");
Rahul Shaharee7baede2021-02-02 13:23:57 +05305653
5654 if (target_virtual_ab_supported()) {
5655 SnapshotMergeStatus = GetSnapshotMergeStatus ();
5656
5657 switch (SnapshotMergeStatus) {
5658 case SNAPSHOTTED:
5659 SnapshotMergeStatus = SNAPSHOTTED;
5660 break;
5661 case MERGING:
5662 SnapshotMergeStatus = MERGING;
5663 break;
5664 default:
5665 SnapshotMergeStatus = NONE_MERGE_STATUS;
5666 break;
5667 }
5668
5669 snprintf(SnapshotMergeState,
5670 strlen(VabSnapshotMergeStatus[SnapshotMergeStatus]) + 1,
5671 "%s", VabSnapshotMergeStatus[SnapshotMergeStatus]);
5672 fastboot_publish("snapshot-update-state", SnapshotMergeState);
5673 }
Amol Jadi5edf3552013-07-23 14:15:34 -07005674}
5675
Brian Swetland9c4c0752009-01-25 16:23:50 -08005676void aboot_init(const struct app_descriptor *app)
5677{
Shashank Mittal4f99a882010-02-01 13:58:50 -08005678 unsigned reboot_mode = 0;
Mayank Grover351a75e2017-05-30 20:06:08 +05305679 int boot_err_type = 0;
5680 int boot_slot = INVALID;
jessicatseng43b1e352020-05-26 13:17:56 +08005681 int vbat = 0;
5682 char boot_vbat[MAX_RSP_SIZE];
Chandan Uddarajubedca152010-06-02 23:05:15 -07005683
lijuang39831732016-01-08 17:49:02 +08005684 /* Initialise wdog to catch early lk crashes */
5685#if WDOG_SUPPORT
5686 msm_wdog_init();
5687#endif
5688
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07005689 /* Setup page size information for nv storage */
Shashank Mittald8c42bf2010-06-09 15:44:28 -07005690 if (target_is_emmc_boot())
5691 {
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07005692 page_size = mmc_page_size();
Shashank Mittald8c42bf2010-06-09 15:44:28 -07005693 page_mask = page_size - 1;
Gaurav Nebhwani4d2389c2016-03-17 21:10:05 +05305694 mmc_blocksize = mmc_get_device_blocksize();
5695 mmc_blocksize_mask = mmc_blocksize - 1;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07005696 }
5697 else
5698 {
5699 page_size = flash_page_size();
5700 page_mask = page_size - 1;
5701 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07005702 ASSERT((MEMBASE + MEMSIZE) > MEMBASE);
5703
Ameya Thakur11cf1a62013-08-05 12:44:48 -07005704 read_device_info(&device);
tracychui9d60fc52020-06-05 17:31:38 +08005705#if defined(ENABLE_PRODINFO_ACCESS)
5706 read_prod_info(&prod);
5707#endif
vijay kumarc65876c2015-04-24 13:29:16 +05305708 read_allow_oem_unlock(&device);
Shashank Mittal162244e2011-08-08 19:01:25 -07005709
Mayank Grover351a75e2017-05-30 20:06:08 +05305710 /* Detect multi-slot support */
5711 if (partition_multislot_is_supported())
5712 {
5713 boot_slot = partition_find_active_slot();
5714 if (boot_slot == INVALID)
5715 {
5716 boot_into_fastboot = true;
5717 dprintf(INFO, "Active Slot: (INVALID)\n");
5718 }
5719 else
5720 {
5721 /* Setting the state of system to boot active slot */
5722 partition_mark_active_slot(boot_slot);
5723 dprintf(INFO, "Active Slot: (%s)\n", SUFFIX_SLOT(boot_slot));
5724 }
5725 }
5726
Greg Griscod6250552011-06-29 14:40:23 -07005727 target_serialno((unsigned char *) sn_buf);
tracychui9d60fc52020-06-05 17:31:38 +08005728#if defined(ENABLE_PRODINFO_ACCESS)
5729 dprintf(CRITICAL,"serial number: %s\n",sn_buf);
5730#else
Ajay Dudanib06c05f2011-05-12 14:46:10 -07005731 dprintf(SPEW,"serial number: %s\n",sn_buf);
tracychui9d60fc52020-06-05 17:31:38 +08005732#endif
5733#if defined(ENABLE_PRODINFO_ACCESS)
5734 read_prod_info(&prod);
5735 snprintf((char *)cust_sn_buf, PRODINFO_MAX_SSN_LEN + 1, "%s", prod.ssn);
5736 dprintf(CRITICAL,"customer serial number: %s\n", cust_sn_buf);
5737 snprintf((char *)factory_sn_buf, PRODINFO_MAX_ISN_LEN + 1, "%s", prod.isn);
5738 dprintf(CRITICAL,"factory serial number: %s\n", factory_sn_buf);
5739#endif
Dhaval Patel223ec952013-07-18 14:49:44 -07005740 memset(display_panel_buf, '\0', MAX_PANEL_BUF_SIZE);
5741
Matthew Qindefd5562014-07-11 18:02:40 +08005742 /*
5743 * Check power off reason if user force reset,
5744 * if yes phone will do normal boot.
5745 */
5746 if (is_user_force_reset())
5747 goto normal_boot;
5748
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07005749 /* Check if we should do something other than booting up */
Ameya Thakur0b9c2442013-05-10 13:22:56 -07005750 if (keys_get_state(KEY_VOLUMEUP) && keys_get_state(KEY_VOLUMEDOWN))
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07005751 {
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03005752 dprintf(ALWAYS,"dload mode key sequence detected\n");
lijuang395b5e62015-11-19 17:39:44 +08005753 reboot_device(EMERGENCY_DLOAD);
5754 dprintf(CRITICAL,"Failed to reboot into dload mode\n");
5755
Ameya Thakur0b9c2442013-05-10 13:22:56 -07005756 boot_into_fastboot = true;
5757 }
5758 if (!boot_into_fastboot)
5759 {
5760 if (keys_get_state(KEY_HOME) || keys_get_state(KEY_VOLUMEUP))
5761 boot_into_recovery = 1;
5762 if (!boot_into_recovery &&
5763 (keys_get_state(KEY_BACK) || keys_get_state(KEY_VOLUMEDOWN)))
Pavel Nedev5d91d412013-04-29 11:34:24 +03005764 boot_into_fastboot = true;
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07005765 }
Shashank Mittald8c42bf2010-06-09 15:44:28 -07005766 #if NO_KEYPAD_DRIVER
Kinson Chik0b1c8162011-08-31 16:31:57 -07005767 if (fastboot_trigger())
Pavel Nedev5d91d412013-04-29 11:34:24 +03005768 boot_into_fastboot = true;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07005769 #endif
Chandan Uddarajubedca152010-06-02 23:05:15 -07005770
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07005771#if USE_PON_REBOOT_REG
5772 reboot_mode = check_hard_reboot_mode();
5773#else
Ajay Dudani77421292010-10-27 19:34:06 -07005774 reboot_mode = check_reboot_mode();
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07005775#endif
5776 if (reboot_mode == RECOVERY_MODE)
5777 {
Ajay Dudani77421292010-10-27 19:34:06 -07005778 boot_into_recovery = 1;
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07005779 }
5780 else if(reboot_mode == FASTBOOT_MODE)
5781 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03005782 boot_into_fastboot = true;
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07005783 }
5784 else if(reboot_mode == ALARM_BOOT)
5785 {
Matthew Qind886f3c2014-01-17 16:52:01 +08005786 boot_reason_alarm = true;
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07005787 }
Monika Singhb0db4b82018-09-26 12:18:02 +05305788#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover912eaa62017-10-26 12:08:53 +05305789 else if (VB_M <= target_get_vb_version())
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07005790 {
Mayank Grover889be1b2017-09-12 20:12:23 +05305791 if (reboot_mode == DM_VERITY_ENFORCING)
5792 {
5793 device.verity_mode = 1;
5794 write_device_info(&device);
5795 }
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07005796#if ENABLE_VB_ATTEST
Mayank Grover889be1b2017-09-12 20:12:23 +05305797 else if (reboot_mode == DM_VERITY_EIO)
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07005798#else
Mayank Grover889be1b2017-09-12 20:12:23 +05305799 else if (reboot_mode == DM_VERITY_LOGGING)
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07005800#endif
Mayank Grover889be1b2017-09-12 20:12:23 +05305801 {
5802 device.verity_mode = 0;
5803 write_device_info(&device);
5804 }
5805 else if (reboot_mode == DM_VERITY_KEYSCLEAR)
5806 {
5807 if(send_delete_keys_to_tz())
5808 ASSERT(0);
5809 }
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07005810 }
Parth Dixitddbc7352015-10-18 03:13:31 +05305811#endif
Ajay Dudani77421292010-10-27 19:34:06 -07005812
jialongjhan2769d202020-05-22 18:25:56 +08005813 /* Display splash screen if enabled */
5814#if DISPLAY_SPLASH_SCREEN
5815#if NO_ALARM_DISPLAY
5816 if (!check_alarm_boot()) {
5817#endif
5818 dprintf(SPEW, "Display Init: Start\n");
5819#if DISPLAY_HDMI_PRIMARY
5820 if (!strlen(device.display_panel))
5821 strlcpy(device.display_panel, DISPLAY_PANEL_HDMI,
5822 sizeof(device.display_panel));
5823#endif
5824#if ENABLE_WBC
5825 /* Wait if the display shutdown is in progress */
5826 while(pm_app_display_shutdown_in_prgs());
5827 if (!pm_appsbl_display_init_done())
5828 target_display_init(device.display_panel);
5829 else
5830 display_image_on_screen();
5831#else
5832 target_display_init(device.display_panel);
5833#endif
5834 dprintf(SPEW, "Display Init: Done\n");
5835#if NO_ALARM_DISPLAY
5836 }
5837#endif
5838#endif
5839
5840
Matthew Qindefd5562014-07-11 18:02:40 +08005841normal_boot:
Pavel Nedev5d91d412013-04-29 11:34:24 +03005842 if (!boot_into_fastboot)
Shashank Mittald8c42bf2010-06-09 15:44:28 -07005843 {
jessicatseng43b1e352020-05-26 13:17:56 +08005844 if(!target_pause_for_battery_charge())
5845 {
5846 vbat = target_get_battery_voltage();
5847 snprintf(boot_vbat, MAX_RSP_SIZE, "%d", vbat);
5848 dprintf(CRITICAL,"battery_voltage: %s\n", boot_vbat);
5849 if(vbat < 3500000)
5850 {
5851 display_lowbattery_image_on_screen();
5852 //[Arima][8901][JialongJhan] Command mode reflash screen when low battery logo shown 20190516 Start
5853 msm_display_flush();
5854 //[Arima][8901][JialongJhan] Command mode reflash screen when low battery logo shown 20190516 End
5855 dprintf(CRITICAL,"Low battery, cannot boot up...\n");
5856 mdelay(3000);
5857 shutdown_device();
5858 }
5859 }
5860
Pavel Nedev5d91d412013-04-29 11:34:24 +03005861 if (target_is_emmc_boot())
Shashank Mittala0032282011-08-26 14:50:11 -07005862 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03005863 if(emmc_recovery_init())
5864 dprintf(ALWAYS,"error in emmc_recovery_init\n");
5865 if(target_use_signed_kernel())
Shashank Mittala0032282011-08-26 14:50:11 -07005866 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03005867 if((device.is_unlocked) || (device.is_tampered))
5868 {
5869 #ifdef TZ_TAMPER_FUSE
Monika Singh98257462018-06-06 11:28:49 +05305870 set_tamper_fuse_cmd(HLOS_IMG_TAMPER_FUSE);
Pavel Nedev5d91d412013-04-29 11:34:24 +03005871 #endif
5872 #if USE_PCOM_SECBOOT
5873 set_tamper_flag(device.is_tampered);
5874 #endif
5875 }
Shashank Mittala0032282011-08-26 14:50:11 -07005876 }
Amit Blay6281ebc2015-01-11 14:44:08 +02005877
Mayank Grover351a75e2017-05-30 20:06:08 +05305878retry_boot:
5879 /* Trying to boot active partition */
5880 if (partition_multislot_is_supported())
5881 {
5882 boot_slot = partition_find_boot_slot();
5883 partition_mark_active_slot(boot_slot);
5884 if (boot_slot == INVALID)
5885 goto fastboot;
5886 }
5887
5888 boot_err_type = boot_linux_from_mmc();
5889 switch (boot_err_type)
5890 {
5891 case ERR_INVALID_PAGE_SIZE:
5892 case ERR_DT_PARSE:
5893 case ERR_ABOOT_ADDR_OVERLAP:
Mayank Grover10cabbe2017-10-30 19:11:05 +05305894 case ERR_INVALID_BOOT_MAGIC:
Mayank Grover351a75e2017-05-30 20:06:08 +05305895 if(partition_multislot_is_supported())
Mayank Grover10cabbe2017-10-30 19:11:05 +05305896 {
5897 /*
5898 * Deactivate current slot, as it failed to
5899 * boot, and retry next slot.
5900 */
5901 partition_deactivate_slot(boot_slot);
Mayank Grover351a75e2017-05-30 20:06:08 +05305902 goto retry_boot;
Mayank Grover10cabbe2017-10-30 19:11:05 +05305903 }
Mayank Grover351a75e2017-05-30 20:06:08 +05305904 else
5905 break;
Mayank Grover351a75e2017-05-30 20:06:08 +05305906 default:
5907 break;
5908 /* going to fastboot menu */
5909 }
Shashank Mittala0032282011-08-26 14:50:11 -07005910 }
Pavel Nedev5d91d412013-04-29 11:34:24 +03005911 else
5912 {
5913 recovery_init();
5914 #if USE_PCOM_SECBOOT
5915 if((device.is_unlocked) || (device.is_tampered))
5916 set_tamper_flag(device.is_tampered);
5917 #endif
5918 boot_linux_from_flash();
5919 }
5920 dprintf(CRITICAL, "ERROR: Could not do normal boot. Reverting "
5921 "to fastboot mode.\n");
Shashank Mittald8c42bf2010-06-09 15:44:28 -07005922 }
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07005923
Mayank Grover351a75e2017-05-30 20:06:08 +05305924fastboot:
jialongjhan2769d202020-05-22 18:25:56 +08005925
5926 mdelay(1000);
5927
Amol Jadi5edf3552013-07-23 14:15:34 -07005928 /* We are here means regular boot did not happen. Start fastboot. */
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07005929
Amol Jadi5edf3552013-07-23 14:15:34 -07005930 /* register aboot specific fastboot commands */
5931 aboot_fastboot_register_commands();
Amol Jadi57abe4c2011-05-24 15:47:27 -07005932
Amol Jadi5edf3552013-07-23 14:15:34 -07005933 /* dump partition table for debug info */
Kinson Chikf1a43512011-07-14 11:28:39 -07005934 partition_dump();
Amol Jadi5edf3552013-07-23 14:15:34 -07005935
5936 /* initialize and start fastboot */
Saranya Chidura41b34532018-10-16 12:29:52 +05305937#if !VERIFIED_BOOT_2
Amol Jadi5edf3552013-07-23 14:15:34 -07005938 fastboot_init(target_get_scratch_address(), target_get_max_flash_size());
Saranya Chidura41b34532018-10-16 12:29:52 +05305939#else
5940 /* Add salt buffer offset at start of image address to copy VB salt */
5941 fastboot_init(ADD_SALT_BUFF_OFFSET(target_get_scratch_address()),
5942 SUB_SALT_BUFF_OFFSET(target_get_max_flash_size()));
5943#endif
lijuang4ece1e72015-08-14 21:02:36 +08005944#if FBCON_DISPLAY_MSG
lijuang9a7d3b92015-11-30 14:41:24 +08005945 display_fastboot_menu();
lijuang4ece1e72015-08-14 21:02:36 +08005946#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08005947}
5948
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07005949uint32_t get_page_size()
5950{
5951 return page_size;
5952}
5953
Amir Samuelov57a6fa22013-06-05 16:36:43 +03005954/*
5955 * Calculated and save hash (SHA256) for non-signed boot image.
5956 *
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07005957 * @param image_addr - Boot image address
5958 * @param image_size - Size of the boot image
Amir Samuelov57a6fa22013-06-05 16:36:43 +03005959 *
5960 * @return int - 0 on success, negative value on failure.
5961 */
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07005962static int aboot_save_boot_hash_mmc(uint32_t image_addr, uint32_t image_size)
Amir Samuelov57a6fa22013-06-05 16:36:43 +03005963{
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07005964 unsigned int digest[8];
5965#if IMAGE_VERIF_ALGO_SHA1
5966 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA1;
5967#else
5968 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA256;
5969#endif
Amir Samuelov57a6fa22013-06-05 16:36:43 +03005970
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07005971 target_crypto_init_params();
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -08005972 hash_find((unsigned char *) image_addr, image_size, (unsigned char *)&digest, auth_algo);
Amir Samuelov57a6fa22013-06-05 16:36:43 +03005973
5974 save_kernel_hash_cmd(digest);
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07005975 dprintf(INFO, "aboot_save_boot_hash_mmc: imagesize_actual size %d bytes.\n", (int) image_size);
Amir Samuelov57a6fa22013-06-05 16:36:43 +03005976
5977 return 0;
5978}
5979
Mayank Grover351a75e2017-05-30 20:06:08 +05305980
Brian Swetland9c4c0752009-01-25 16:23:50 -08005981APP_START(aboot)
5982 .init = aboot_init,
5983APP_END