blob: f499a62e4672c460c2c573eb8aa1eac32ce814a8 [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;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700106void write_device_info_mmc(device_info *dev);
107void write_device_info_flash(device_info *dev);
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -0700108static int aboot_save_boot_hash_mmc(uint32_t image_addr, uint32_t image_size);
Channagoud Kadabiad259832015-05-29 11:14:17 -0700109static int aboot_frp_unlock(char *pname, void *data, unsigned sz);
Kishor PK38ed93d2017-04-25 14:19:26 +0530110static inline uint64_t validate_partition_size();
Parth Dixit54ac3bb2017-03-07 15:52:48 +0530111bool pwr_key_is_pressed = false;
Hareesh Gauthamabf1e1e2017-04-12 18:09:24 +0530112static bool is_systemd_present=false;
Mayank Grover351a75e2017-05-30 20:06:08 +0530113static void publish_getvar_multislot_vars();
Sridhar Parasurame94e8152014-10-24 14:06:03 -0700114/* fastboot command function pointer */
115typedef void (*fastboot_cmd_fn) (const char *, void *, unsigned);
Monika Singh292b3e92018-03-17 22:40:23 +0530116bool get_perm_attr_status();
Sridhar Parasurame94e8152014-10-24 14:06:03 -0700117
118struct fastboot_cmd_desc {
119 char * name;
120 fastboot_cmd_fn cb;
121};
122
Subbaraman Narayanamurthyeb92bcc2010-07-20 14:32:46 -0700123#define EXPAND(NAME) #NAME
124#define TARGET(NAME) EXPAND(NAME)
Brian Swetland2defe162009-08-18 14:35:59 -0700125
Ajay Singh Parmara7865a82015-04-16 21:27:52 -0700126#define DISPLAY_PANEL_HDMI "hdmi"
127
Ajay Dudanicd01f9b2010-02-23 21:13:04 -0800128#ifdef MEMBASE
129#define EMMC_BOOT_IMG_HEADER_ADDR (0xFF000+(MEMBASE))
130#else
David Ng183a7422009-12-07 14:55:21 -0800131#define EMMC_BOOT_IMG_HEADER_ADDR 0xFF000
Ajay Dudanicd01f9b2010-02-23 21:13:04 -0800132#endif
133
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700134#ifndef MEMSIZE
135#define MEMSIZE 1024*1024
136#endif
137
138#define MAX_TAGS_SIZE 1024
Kishor PKee5c0a32018-03-06 16:49:46 +0530139#define PLL_CODES_OFFSET 4096
Kun Liang2f1601a2013-08-12 16:29:54 +0800140/* make 4096 as default size to ensure EFS,EXT4's erasing */
141#define DEFAULT_ERASE_SIZE 4096
Ujwal Patelc0b0a252015-08-16 14:05:35 -0700142#define MAX_PANEL_BUF_SIZE 196
Vijay Kumar Pendoti0b21f462016-05-02 17:09:18 +0530143#define FOOTER_SIZE 16384
Kun Liang2f1601a2013-08-12 16:29:54 +0800144
Dhaval Patelf83d73b2014-06-23 16:24:37 -0700145#define DISPLAY_DEFAULT_PREFIX "mdss_mdp"
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700146#define BOOT_DEV_MAX_LEN 64
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -0800147
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800148#define IS_ARM64(ptr) (ptr->magic_64 == KERNEL64_HDR_MAGIC) ? true : false
149
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -0700150#define ADD_OF(a, b) (UINT_MAX - b > a) ? (a + b) : UINT_MAX
151
Ameya Thakur10a33452016-06-13 14:24:26 -0700152//Size of the header that is used in case the boot image has
153//a uncompressed kernel + appended dtb
154#define PATCHED_KERNEL_HEADER_SIZE 20
155
156//String used to determine if the boot image has
157//a uncompressed kernel + appended dtb
158#define PATCHED_KERNEL_MAGIC "UNCOMPRESSED_IMG"
159
Sridhar Parasuram7bd4aaf2015-02-12 11:14:38 -0800160#if USE_BOOTDEV_CMDLINE
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700161static const char *emmc_cmdline = " androidboot.bootdevice=";
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700162#else
David Ng183a7422009-12-07 14:55:21 -0800163static const char *emmc_cmdline = " androidboot.emmc=true";
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700164#endif
Mayank Groverb716edf2019-05-08 16:06:55 +0530165static const char *dynamic_bootdev_cmdline =
166 " androidboot.boot_devices=soc/";
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800167static const char *usb_sn_cmdline = " androidboot.serialno=";
Pavel Nedev328ac822013-04-05 15:25:11 +0300168static const char *androidboot_mode = " androidboot.mode=";
Hareesh Gauthamabf1e1e2017-04-12 18:09:24 +0530169
170static const char *systemd_ffbm_mode = " systemd.unit=ffbm.target";
Matthew Qind886f3c2014-01-17 16:52:01 +0800171static const char *alarmboot_cmdline = " androidboot.alarmboot=true";
Pavel Nedev898298c2013-02-27 12:36:09 -0800172static const char *loglevel = " quiet";
Ajay Dudanica3a33c2011-11-18 08:31:40 -0800173static const char *battchg_pause = " androidboot.mode=charger";
Shashank Mittalcd98d472011-08-02 14:29:24 -0700174static const char *auth_kernel = " androidboot.authorized_kernel=true";
Pavel Nedev5614d222013-06-17 18:01:02 +0300175static const char *secondary_gpt_enable = " gpt";
Monika Singh292b3e92018-03-17 22:40:23 +0530176#ifdef MDTP_SUPPORT
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200177static const char *mdtp_activated_flag = " mdtp";
Monika Singh292b3e92018-03-17 22:40:23 +0530178#endif
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800179static const char *baseband_apq = " androidboot.baseband=apq";
180static const char *baseband_msm = " androidboot.baseband=msm";
181static const char *baseband_csfb = " androidboot.baseband=csfb";
182static const char *baseband_svlte2a = " androidboot.baseband=svlte2a";
Ajay Dudani403bc492011-09-30 16:17:21 -0700183static const char *baseband_mdm = " androidboot.baseband=mdm";
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800184static const char *baseband_mdm2 = " androidboot.baseband=mdm2";
Amol Jadi5c61a952012-05-04 17:05:35 -0700185static const char *baseband_sglte = " androidboot.baseband=sglte";
Amol Jadi2a15a272013-01-22 12:03:36 -0800186static const char *baseband_dsda = " androidboot.baseband=dsda";
187static const char *baseband_dsda2 = " androidboot.baseband=dsda2";
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800188static const char *baseband_sglte2 = " androidboot.baseband=sglte2";
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800189static const char *warmboot_cmdline = " qpnp-power-on.warm_boot=1";
Vijay Kumar Pendotib228cfc2016-06-13 20:15:23 +0530190static const char *baseband_apq_nowgr = " androidboot.baseband=baseband_apq_nowgr";
Mayank Grover351a75e2017-05-30 20:06:08 +0530191static const char *androidboot_slot_suffix = " androidboot.slot_suffix=";
192static const char *skip_ramfs = " skip_initramfs";
Mayank Grover466d6562018-05-10 14:52:20 +0530193
194#if HIBERNATION_SUPPORT
195static const char *resume = " resume=/dev/mmcblk0p";
196#endif
197
Sourabh Banerjee6c7153c2018-03-20 01:21:57 +0530198#ifdef INIT_BIN_LE
199static const char *sys_path_cmdline = " rootwait ro init="INIT_BIN_LE;
200#else
Mayank Grover3804be72017-06-22 11:59:23 +0530201static const char *sys_path_cmdline = " rootwait ro init=/init";
Sourabh Banerjee6c7153c2018-03-20 01:21:57 +0530202#endif
Sourabh Banerjee386b1322018-02-27 09:37:28 +0530203
204#if VERITY_LE
205static const char *verity_dev = " root=/dev/dm-0";
206static const char *verity_system_part = " dm=\"system";
207static const char *verity_params = " none ro,0 1 android-verity /dev/mmcblk0p";
208#else
Mayank Grover5384ed82018-05-09 12:09:24 +0530209static const char *sys_path = " root=/dev/mmcblk0p";
lijuang83ef4b22018-08-23 11:01:55 +0800210
211#define MAX_DTBO_IDX_STR 64
212static const char *android_boot_dtbo_idx = " androidboot.dtbo_idx=";
Parth Dixit1375d9e2019-07-08 20:56:13 +0530213
214#define MAX_DTB_IDX_STR MAX_DTBO_IDX_STR
215static const char *android_boot_dtb_idx = " androidboot.dtb_idx=";
Sourabh Banerjee386b1322018-02-27 09:37:28 +0530216#endif
Ajay Dudanid04110c2011-01-17 23:55:07 -0800217
michaellin6be1f392020-04-24 15:05:10 +0800218#include <platform/gpio.h>
219static const char *PCBA_STAGE_0 = " androidboot.pcbastage=EP0";
220static const char *PCBA_STAGE_1 = " androidboot.pcbastage=EP1";
221static const char *PCBA_STAGE_2 = " androidboot.pcbastage=EP2";
222static const char *PCBA_STAGE_3 = " androidboot.pcbastage=FP";
223static const char *PCBA_STAGE_4 = " androidboot.pcbastage=MP";
michaellinaf8857a2020-05-05 14:58:47 +0800224static const char *PCBA_STAGE_5 = " androidboot.pcbastage=MP-8903MB_001";
michaellin6be1f392020-04-24 15:05:10 +0800225static const char *PCBA_STAGE_F = " androidboot.pcbastage=Reserved";
226
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700227#if VERIFIED_BOOT
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700228static const char *verity_mode = " androidboot.veritymode=";
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700229static const char *verified_state= " androidboot.verifiedbootstate=";
Parth Dixita5715a02015-10-29 12:25:10 +0530230static const char *keymaster_v1= " androidboot.keymaster=1";
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700231//indexed based on enum values, green is 0 by default
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700232
233struct verified_boot_verity_mode vbvm[] =
234{
Channagoud Kadabi86b0c112016-03-16 19:23:16 -0700235#if ENABLE_VB_ATTEST
236 {false, "eio"},
237#else
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700238 {false, "logging"},
Channagoud Kadabi86b0c112016-03-16 19:23:16 -0700239#endif
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700240 {true, "enforcing"},
241};
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700242struct verified_boot_state_name vbsn[] =
243{
244 {GREEN, "green"},
245 {ORANGE, "orange"},
246 {YELLOW,"yellow"},
247 {RED,"red" },
248};
249#endif
Channagoud Kadabi86b0c112016-03-16 19:23:16 -0700250/*As per spec delay wait time before shutdown in Red state*/
251#define DELAY_WAIT 30000
Ashish Bhimanpalliwar8cb34fd2020-11-12 19:26:41 +0530252static unsigned page_size = BOARD_KERNEL_PAGESIZE;
253
254uint32_t kernel_hdr_page_size()
255{
256 return page_size;
257}
258
259
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700260static unsigned page_mask = 0;
Gaurav Nebhwani4d2389c2016-03-17 21:10:05 +0530261static unsigned mmc_blocksize = 0;
262static unsigned mmc_blocksize_mask = 0;
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700263static char ffbm_mode_string[FFBM_MODE_BUF_SIZE];
264static bool boot_into_ffbm;
vijay kumar870515d2015-08-31 16:37:24 +0530265static char *target_boot_params = NULL;
Matthew Qind886f3c2014-01-17 16:52:01 +0800266static bool boot_reason_alarm;
Channagoud Kadabi80a182b2015-03-11 17:04:23 -0700267static bool devinfo_present = true;
Channagoud Kadabi736c4962015-08-21 11:56:52 -0700268bool boot_into_fastboot = false;
Parth Dixit4097b622016-03-15 14:42:27 +0530269static uint32_t dt_size = 0;
Monika Singh292b3e92018-03-17 22:40:23 +0530270static char *vbcmdline;
271static bootinfo info = {0};
Mayank Grovere1ab96c2018-09-04 20:31:31 +0530272static void *recovery_dtbo_buf = NULL;
273static uint32_t recovery_dtbo_size = 0;
274
Shashank Mittalcd98d472011-08-02 14:29:24 -0700275/* Assuming unauthorized kernel image by default */
276static int auth_kernel_img = 0;
Monika Singh292b3e92018-03-17 22:40:23 +0530277static device_info device = {DEVICE_MAGIC,0,0,0,0,{0},{0},{0},1,{0},0,{0}};
278
jhchena8a15dd2020-04-24 15:45:57 +0800279#ifdef ENABLE_FUSE_CHECK
280static const char *fuse_blown = " androidboot.oem.color=red";
281static const char *fuse_not_blown = " androidboot.oem.color=brown";
282#endif
283
284#ifdef ENABLE_FUSE_CHECK
285 int is_fused = 0;
286#endif
287
vijay kumarc65876c2015-04-24 13:29:16 +0530288static bool is_allow_unlock = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -0700289
vijay kumarca2e6812015-07-08 20:28:25 +0530290static char frp_ptns[2][8] = {"config","frp"};
291
lijuang511a2b52015-08-14 20:50:51 +0800292static const char *critical_flash_allowed_ptn[] = {
293 "aboot",
294 "rpm",
295 "tz",
296 "sbl",
297 "sdi",
298 "sbl1",
299 "xbl",
300 "hyp",
301 "pmic",
302 "bootloader",
303 "devinfo",
304 "partition"};
305
Rahul Shaharef7a5f8b2021-02-04 16:51:36 +0530306static const char *VirtualAbCriticalPartitions[] = {
307 "misc",
308 "metadata",
309 "userdata"};
310
311static bool CheckVirtualAbCriticalPartition (const char *PartitionName);
312
Rahul Shaharee7baede2021-02-02 13:23:57 +0530313static const char *VabSnapshotMergeStatus[] = {
314 "none",
315 "unknown",
316 "snapshotted",
317 "merging",
318 "cancelled"};
319
Dima Zavin42168f22009-01-30 11:52:22 -0800320struct atag_ptbl_entry
321{
322 char name[16];
323 unsigned offset;
324 unsigned size;
325 unsigned flags;
326};
327
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700328/*
329 * Partition info, required to be published
330 * for fastboot
331 */
332struct getvar_partition_info {
Mayank Grover7b8a8f82017-10-27 13:07:59 +0530333 char part_name[MAX_GPT_NAME_SIZE]; /* Partition name */
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700334 char getvar_size[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for size */
335 char getvar_type[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for type */
336 char size_response[MAX_RSP_SIZE]; /* fastboot response for size */
337 char type_response[MAX_RSP_SIZE]; /* fastboot response for type */
338};
339
340/*
Mayank Grover7b8a8f82017-10-27 13:07:59 +0530341 * Update the part_type_known for known paritions types.
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700342 */
Mayank Groverd38fe012018-03-13 15:33:16 +0530343#define RAW_STR "raw"
Mayank Grover52cd10a2018-03-15 12:57:54 +0530344#define EXT_STR "ext4"
345#define F2FS_STR "f2fs"
346
347#define FS_SUPERBLOCK_OFFSET 0x400
348#define EXT_MAGIC 0xEF53
349#define EXT_MAGIC_OFFSET_SB 0x38
350#define F2FS_MAGIC 0xF2F52010 // F2FS Magic Number
351#define F2FS_MAGIC_OFFSET_SB 0x0
352
353typedef enum fs_signature_type {
354 EXT_FS_SIGNATURE = 1,
355 EXT_F2FS_SIGNATURE = 2,
356 NO_FS = -1
357} fs_signature_type;
358
Mayank Grover7b8a8f82017-10-27 13:07:59 +0530359struct getvar_partition_info part_info[NUM_PARTITIONS];
360struct getvar_partition_info part_type_known[] =
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700361{
Mayank Grover49920212018-02-09 18:15:55 +0530362 { "system" , "partition-size:", "partition-type:", "", "ext4" },
363 { "userdata" , "partition-size:", "partition-type:", "", "ext4" },
364 { "cache" , "partition-size:", "partition-type:", "", "ext4" },
365 { "recoveryfs" , "partition-size:", "partition-type:", "", "ext4" },
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700366};
367
368char max_download_size[MAX_RSP_SIZE];
Ameya Thakur11cf1a62013-08-05 12:44:48 -0700369char charger_screen_enabled[MAX_RSP_SIZE];
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800370char sn_buf[13];
Dhaval Patel223ec952013-07-18 14:49:44 -0700371char display_panel_buf[MAX_PANEL_BUF_SIZE];
Unnati Gandhi62c8ab82014-01-24 11:01:01 +0530372char panel_display_mode[MAX_RSP_SIZE];
Mayank Grovera64dfbe2018-05-17 14:06:34 +0530373char soc_version_str[MAX_RSP_SIZE];
Mayank Grover7b8a8f82017-10-27 13:07:59 +0530374char block_size_string[MAX_RSP_SIZE];
Rahul Shaharee7baede2021-02-02 13:23:57 +0530375static char SnapshotMergeState[MAX_RSP_SIZE];
Mukesh Ojhabb6e7f32018-03-02 15:04:06 +0530376#if PRODUCT_IOT
Mayank Grover7b8a8f82017-10-27 13:07:59 +0530377
378/* For IOT we are using custom version */
379#define PRODUCT_IOT_VERSION "IOT001"
380char bootloader_version_string[MAX_RSP_SIZE];
381#endif
lijuang102dfa92015-10-09 18:31:03 +0800382
383#if CHECK_BAT_VOLTAGE
lijuang65c5a822015-08-29 16:35:36 +0800384char battery_voltage[MAX_RSP_SIZE];
lijuang102dfa92015-10-09 18:31:03 +0800385char battery_soc_ok [MAX_RSP_SIZE];
386#endif
387
lijuangf16461c2015-08-03 17:09:34 +0800388char get_variant[MAX_RSP_SIZE];
Greg Griscod6250552011-06-29 14:40:23 -0700389
Greg Griscod2471ef2011-07-14 13:00:42 -0700390extern int emmc_recovery_init(void);
391
Kinson Chik0b1c8162011-08-31 16:31:57 -0700392#if NO_KEYPAD_DRIVER
393extern int fastboot_trigger(void);
394#endif
Greg Griscod2471ef2011-07-14 13:00:42 -0700395
Mayank Grovere1ab96c2018-09-04 20:31:31 +0530396static void update_ker_tags_rdisk_addr(boot_img_hdr *hdr, bool is_arm64)
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700397{
398 /* overwrite the destination of specified for the project */
Channagoud Kadabi7042fa32013-04-26 16:44:14 -0700399#ifdef ABOOT_IGNORE_BOOT_HEADER_ADDRS
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800400 if (is_arm64)
401 hdr->kernel_addr = ABOOT_FORCE_KERNEL64_ADDR;
402 else
403 hdr->kernel_addr = ABOOT_FORCE_KERNEL_ADDR;
Channagoud Kadabi7042fa32013-04-26 16:44:14 -0700404 hdr->ramdisk_addr = ABOOT_FORCE_RAMDISK_ADDR;
405 hdr->tags_addr = ABOOT_FORCE_TAGS_ADDR;
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700406#endif
407}
408
Dima Zavin42168f22009-01-30 11:52:22 -0800409static void ptentry_to_tag(unsigned **ptr, struct ptentry *ptn)
410{
411 struct atag_ptbl_entry atag_ptn;
412
413 memcpy(atag_ptn.name, ptn->name, 16);
414 atag_ptn.name[15] = '\0';
415 atag_ptn.offset = ptn->start;
416 atag_ptn.size = ptn->length;
417 atag_ptn.flags = ptn->flags;
418 memcpy(*ptr, &atag_ptn, sizeof(struct atag_ptbl_entry));
419 *ptr += sizeof(struct atag_ptbl_entry) / sizeof(unsigned);
420}
Mayank Grover9df84c02018-08-30 15:46:35 +0530421#ifdef VERIFIED_BOOT_2
422void load_vbmeta_image(void **vbmeta_image_buf, uint32_t *vbmeta_image_sz)
423{
424 int index = 0;
425 char *vbm_img_buf = NULL;
426 unsigned long long ptn = 0;
427 unsigned long long ptn_size = 0;
428
429 /* Immediately return if dtbo is not supported */
430 index = partition_get_index("vbmeta");
431 ptn = partition_get_offset(index);
432 if(!ptn)
433 {
434 dprintf(CRITICAL, "ERROR: vbmeta partition not found.\n");
435 return;
436 }
437
438 ptn_size = partition_get_size(index);
439 if (ptn_size > MAX_SUPPORTED_VBMETA_IMG_BUF)
440 {
441 dprintf(CRITICAL, "ERROR: vbmeta parition size is greater than supported.\n");
442 return;
443 }
444
445 vbm_img_buf = (char *)memalign(CACHE_LINE, ROUNDUP((uint32_t)ptn_size, CACHE_LINE));
446 if (!vbm_img_buf)
447 {
448 dprintf(CRITICAL, "ERROR: vbmeta unable to locate buffer\n");
449 return;
450 }
451
452 mmc_set_lun(partition_get_lun(index));
453 if (mmc_read(ptn, (uint32_t *)vbm_img_buf, (uint32_t)ptn_size))
454 {
455 dprintf(CRITICAL, "ERROR: vbmeta read failure\n");
456 free(vbm_img_buf);
457 return;
458 }
459
460 *vbmeta_image_buf = vbm_img_buf;
461 *vbmeta_image_sz = (uint32_t)ptn_size;
462 return;
463}
464#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -0800465
lijuang102dfa92015-10-09 18:31:03 +0800466#if CHECK_BAT_VOLTAGE
467void update_battery_status(void)
468{
469 snprintf(battery_voltage,MAX_RSP_SIZE, "%d",target_get_battery_voltage());
470 snprintf(battery_soc_ok ,MAX_RSP_SIZE, "%s",target_battery_soc_ok()? "yes":"no");
471}
472#endif
473
michaellin6be1f392020-04-24 15:05:10 +0800474uint32_t GetPcbaVariant(void)
475{
476 uint8_t GPIO_97=10;
477 uint8_t GPIO_98=10;
478 uint8_t GPIO_99=10;
479
480 uint8_t pcba_stage = 0;
481
482 // Config GPIO
483 gpio_tlmm_config(97, 0, GPIO_INPUT, GPIO_NO_PULL, GPIO_2MA, GPIO_ENABLE);
484 gpio_tlmm_config(98, 0, GPIO_INPUT, GPIO_NO_PULL, GPIO_2MA, GPIO_ENABLE);
485 gpio_tlmm_config(99, 0, GPIO_INPUT, GPIO_NO_PULL, GPIO_2MA, GPIO_ENABLE);
486
487 /* Wait for the gpio config to take effect - debounce time */
488 thread_sleep(10);
489
490 /* Get status of GPIO */
491 GPIO_97 = gpio_status(97);
492 GPIO_98 = gpio_status(98);
493 GPIO_99 = gpio_status(99);
494
495 pcba_stage = (GPIO_99 << 2) + (GPIO_98 << 1) + GPIO_97;
496 dprintf(ALWAYS, "pcba_stage status: %u\n", pcba_stage);
497 // GPIO_99 GPIO_98 GPIO_97
498 // 0 0 0 EP0
499 // 0 0 1 EP1
500 // 0 1 0 EP2
501 // 0 1 1 PP
michaellinaf8857a2020-05-05 14:58:47 +0800502 // 0 1 1 FP(8901MB-007)/PP
503 // 1 0 0 MP(8901MB-008)
504 // 1 0 1 MP-8903MB_001
michaellin6be1f392020-04-24 15:05:10 +0800505 return pcba_stage;
506}
507
Neeti Desaie245d492012-06-01 12:52:13 -0700508unsigned char *update_cmdline(const char * cmdline)
Brian Swetland9c4c0752009-01-25 16:23:50 -0800509{
David Ng183a7422009-12-07 14:55:21 -0800510 int cmdline_len = 0;
511 int have_cmdline = 0;
Amol Jadi168b7712012-03-06 16:15:00 -0800512 unsigned char *cmdline_final = NULL;
Neeti Desaie245d492012-06-01 12:52:13 -0700513 int pause_at_bootup = 0;
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800514 bool warm_boot = false;
Pavel Nedev5614d222013-06-17 18:01:02 +0300515 bool gpt_exists = partition_gpt_exists();
Joonwoo Park61112782013-10-02 19:50:39 -0700516 int have_target_boot_params = 0;
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700517 char *boot_dev_buf = NULL;
Monika Singh292b3e92018-03-17 22:40:23 +0530518#ifdef MDTP_SUPPORT
Mayank Grover351a75e2017-05-30 20:06:08 +0530519 bool is_mdtp_activated = 0;
Monika Singh292b3e92018-03-17 22:40:23 +0530520#endif
Mayank Grover351a75e2017-05-30 20:06:08 +0530521 int current_active_slot = INVALID;
Mayank Grover3804be72017-06-22 11:59:23 +0530522 int system_ptn_index = -1;
523 unsigned int lun = 0;
524 char lun_char_base = 'a';
Sourabh Banerjee386b1322018-02-27 09:37:28 +0530525#if VERITY_LE
526 int syspath_buflen = strlen(verity_dev)
527 + strlen(verity_system_part) + (sizeof(char) * 2) + 2
528 + strlen(verity_params) + sizeof(int) + 2;
529#else
lijuang83ef4b22018-08-23 11:01:55 +0800530 int syspath_buflen = strlen(sys_path) + sizeof(int) + 2; /*allocate buflen for largest possible string*/
531 char dtbo_idx_str[MAX_DTBO_IDX_STR] = "\0";
532 int dtbo_idx = INVALID_PTN;
Parth Dixit1375d9e2019-07-08 20:56:13 +0530533 char dtb_idx_str[MAX_DTB_IDX_STR] = "\0";
534 int dtb_idx = INVALID_PTN;
Sourabh Banerjee386b1322018-02-27 09:37:28 +0530535#endif
Mayank Grover3804be72017-06-22 11:59:23 +0530536 char syspath_buf[syspath_buflen];
Mayank Grover466d6562018-05-10 14:52:20 +0530537#if HIBERNATION_SUPPORT
538 int resume_buflen = strlen(resume) + sizeof(int) + 2;
539 char resume_buf[resume_buflen];
540 int swap_ptn_index = INVALID_PTN;
541#endif
542
Mayank Grover889be1b2017-09-12 20:12:23 +0530543#if VERIFIED_BOOT
544 uint32_t boot_state = RED;
545#endif
Hareesh Gauthamabf1e1e2017-04-12 18:09:24 +0530546
547#if USE_LE_SYSTEMD
548 is_systemd_present=true;
549#endif
550
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700551#if VERIFIED_BOOT
Mayank Grover912eaa62017-10-26 12:08:53 +0530552 if (VB_M <= target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +0530553 {
554 boot_state = boot_verify_get_state();
555 }
Parth Dixitddbc7352015-10-18 03:13:31 +0530556#endif
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700557
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200558#ifdef MDTP_SUPPORT
559 mdtp_activated(&is_mdtp_activated);
560#endif /* MDTP_SUPPORT */
Dima Zavin42168f22009-01-30 11:52:22 -0800561
Brian Swetland9c4c0752009-01-25 16:23:50 -0800562 if (cmdline && cmdline[0]) {
David Ng183a7422009-12-07 14:55:21 -0800563 cmdline_len = strlen(cmdline);
564 have_cmdline = 1;
565 }
566 if (target_is_emmc_boot()) {
567 cmdline_len += strlen(emmc_cmdline);
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700568 boot_dev_buf = (char *) malloc(sizeof(char) * BOOT_DEV_MAX_LEN);
Mayank Groverb716edf2019-05-08 16:06:55 +0530569 if (!boot_dev_buf) {
570 dprintf(CRITICAL, "ERROR: Failed to allocate boot_dev_buf\n");
571 } else {
lijuangd28587b2021-04-27 15:54:05 +0800572 platform_boot_dev_cmdline(boot_dev_buf, sizeof(char) * BOOT_DEV_MAX_LEN);
Mayank Groverb716edf2019-05-08 16:06:55 +0530573#if USE_BOOTDEV_CMDLINE
574 cmdline_len += strlen(boot_dev_buf);
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700575#endif
Mayank Groverb716edf2019-05-08 16:06:55 +0530576 if (target_dynamic_partition_supported()) {
577 cmdline_len += strlen(dynamic_bootdev_cmdline);
578 cmdline_len += strlen(boot_dev_buf);
579 }
580 }
David Ng183a7422009-12-07 14:55:21 -0800581 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800582
583 cmdline_len += strlen(usb_sn_cmdline);
584 cmdline_len += strlen(sn_buf);
585
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700586#if VERIFIED_BOOT
Mayank Grover912eaa62017-10-26 12:08:53 +0530587 if (VB_M <= target_get_vb_version())
Sridhar Parasuram7d8bb9a2015-09-23 18:21:19 -0700588 {
Mayank Grover889be1b2017-09-12 20:12:23 +0530589 cmdline_len += strlen(verified_state) + strlen(vbsn[boot_state].name);
590 if ((device.verity_mode != 0 ) && (device.verity_mode != 1))
591 {
592 dprintf(CRITICAL, "Devinfo paritition possibly corrupted!!!. Please erase devinfo partition to continue booting\n");
593 ASSERT(0);
594 }
595 cmdline_len += strlen(verity_mode) + strlen(vbvm[device.verity_mode].name);
596 cmdline_len += strlen(keymaster_v1);
Sridhar Parasuram7d8bb9a2015-09-23 18:21:19 -0700597 }
Parth Dixitddbc7352015-10-18 03:13:31 +0530598#endif
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700599
Monika Singh292b3e92018-03-17 22:40:23 +0530600
601 if (vbcmdline != NULL) {
602 dprintf(DEBUG, "UpdateCmdLine vbcmdline present len %d\n",
603 strlen(vbcmdline));
604 cmdline_len += strlen(vbcmdline);
605 }
606
Pavel Nedev5614d222013-06-17 18:01:02 +0300607 if (boot_into_recovery && gpt_exists)
608 cmdline_len += strlen(secondary_gpt_enable);
609
Monika Singh292b3e92018-03-17 22:40:23 +0530610#ifdef MDTP_SUPPORT
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200611 if(is_mdtp_activated)
612 cmdline_len += strlen(mdtp_activated_flag);
Monika Singh292b3e92018-03-17 22:40:23 +0530613#endif
Pavel Nedev328ac822013-04-05 15:25:11 +0300614 if (boot_into_ffbm) {
615 cmdline_len += strlen(androidboot_mode);
Hareesh Gauthamabf1e1e2017-04-12 18:09:24 +0530616
617 if(is_systemd_present)
618 cmdline_len += strlen(systemd_ffbm_mode);
619
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700620 cmdline_len += strlen(ffbm_mode_string);
Pavel Nedev898298c2013-02-27 12:36:09 -0800621 /* reduce kernel console messages to speed-up boot */
622 cmdline_len += strlen(loglevel);
Matthew Qind886f3c2014-01-17 16:52:01 +0800623 } else if (boot_reason_alarm) {
624 cmdline_len += strlen(alarmboot_cmdline);
Zhenhua Huang431dafa2015-06-30 16:13:37 +0800625 } else if ((target_build_variant_user() || device.charger_screen_enabled)
lijuang266b17d2018-08-17 18:53:42 +0800626 && target_pause_for_battery_charge() && !boot_into_recovery) {
David Ngf773dde2010-07-26 19:55:08 -0700627 pause_at_bootup = 1;
628 cmdline_len += strlen(battchg_pause);
629 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800630
Shashank Mittalcd98d472011-08-02 14:29:24 -0700631 if(target_use_signed_kernel() && auth_kernel_img) {
632 cmdline_len += strlen(auth_kernel);
633 }
634
Joonwoo Park61112782013-10-02 19:50:39 -0700635 if (get_target_boot_params(cmdline, boot_into_recovery ? "recoveryfs" :
636 "system",
vijay kumar870515d2015-08-31 16:37:24 +0530637 &target_boot_params) == 0) {
Joonwoo Park61112782013-10-02 19:50:39 -0700638 have_target_boot_params = 1;
639 cmdline_len += strlen(target_boot_params);
640 }
641
Ajay Dudanid04110c2011-01-17 23:55:07 -0800642 /* Determine correct androidboot.baseband to use */
643 switch(target_baseband())
644 {
645 case BASEBAND_APQ:
646 cmdline_len += strlen(baseband_apq);
647 break;
648
649 case BASEBAND_MSM:
650 cmdline_len += strlen(baseband_msm);
651 break;
652
653 case BASEBAND_CSFB:
654 cmdline_len += strlen(baseband_csfb);
655 break;
656
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800657 case BASEBAND_SVLTE2A:
658 cmdline_len += strlen(baseband_svlte2a);
Ajay Dudanid04110c2011-01-17 23:55:07 -0800659 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700660
661 case BASEBAND_MDM:
662 cmdline_len += strlen(baseband_mdm);
663 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700664
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800665 case BASEBAND_MDM2:
666 cmdline_len += strlen(baseband_mdm2);
667 break;
668
Amol Jadi5c61a952012-05-04 17:05:35 -0700669 case BASEBAND_SGLTE:
670 cmdline_len += strlen(baseband_sglte);
671 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530672
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800673 case BASEBAND_SGLTE2:
674 cmdline_len += strlen(baseband_sglte2);
675 break;
676
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530677 case BASEBAND_DSDA:
678 cmdline_len += strlen(baseband_dsda);
679 break;
Amol Jadi2a15a272013-01-22 12:03:36 -0800680
681 case BASEBAND_DSDA2:
682 cmdline_len += strlen(baseband_dsda2);
683 break;
Vijay Kumar Pendotib228cfc2016-06-13 20:15:23 +0530684 case BASEBAND_APQ_NOWGR:
685 cmdline_len += strlen(baseband_apq_nowgr);
686 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800687 }
688
michaellin6be1f392020-04-24 15:05:10 +0800689 switch(GetPcbaVariant())
690 {
691 case 0: // EP0
692 cmdline_len += strlen(PCBA_STAGE_0);
693 break;
694
695 case 1: // EP1
696 cmdline_len += strlen(PCBA_STAGE_1);
697 break;
698
699 case 2: // EP2
700 cmdline_len += strlen(PCBA_STAGE_2);
701 break;
702
michaellinaf8857a2020-05-05 14:58:47 +0800703 case 3: // FP(8901MB-007)/PP
michaellin6be1f392020-04-24 15:05:10 +0800704 cmdline_len += strlen(PCBA_STAGE_3);
705 break;
706
michaellinaf8857a2020-05-05 14:58:47 +0800707 case 4: // MP(8901MB-008)
michaellin6be1f392020-04-24 15:05:10 +0800708 cmdline_len += strlen(PCBA_STAGE_4);
709 break;
710
michaellinaf8857a2020-05-05 14:58:47 +0800711 case 5: // MP-8903MB_001
712 cmdline_len += strlen(PCBA_STAGE_5);
713 break;
714
michaellin6be1f392020-04-24 15:05:10 +0800715 default:// Reserved
716 cmdline_len += strlen(PCBA_STAGE_F);
717 break;
718 }
719
Vladislav Levenetz3cd45242017-04-05 08:58:07 +0300720#if ENABLE_DISPLAY
Lijuan Gao4a5b8322014-07-24 10:38:42 +0800721 if (cmdline) {
722 if ((strstr(cmdline, DISPLAY_DEFAULT_PREFIX) == NULL) &&
Padmanabhan Komandurubccbcdc2015-06-30 16:19:24 +0530723 target_display_panel_node(display_panel_buf,
724 MAX_PANEL_BUF_SIZE) &&
Lijuan Gao4a5b8322014-07-24 10:38:42 +0800725 strlen(display_panel_buf)) {
726 cmdline_len += strlen(display_panel_buf);
727 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700728 }
Vladislav Levenetz3cd45242017-04-05 08:58:07 +0300729#endif
Dhaval Patel223ec952013-07-18 14:49:44 -0700730
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800731 if (target_warm_boot()) {
732 warm_boot = true;
733 cmdline_len += strlen(warmboot_cmdline);
734 }
735
Mayank Grover5384ed82018-05-09 12:09:24 +0530736 if (target_uses_system_as_root() ||
737 partition_multislot_is_supported())
Mayank Grover351a75e2017-05-30 20:06:08 +0530738 {
739 current_active_slot = partition_find_active_slot();
740 cmdline_len += (strlen(androidboot_slot_suffix)+
741 strlen(SUFFIX_SLOT(current_active_slot)));
742
Mayank Grover3804be72017-06-22 11:59:23 +0530743 system_ptn_index = partition_get_index("system");
744 if (platform_boot_dev_isemmc())
745 {
Sourabh Banerjee386b1322018-02-27 09:37:28 +0530746#if VERITY_LE
747 /*
748 Condition 4: Verity and A/B both enabled
749 Eventual command line looks like:
750 ... androidboot.slot_suffix=<slot_suffix> ... rootfstype=ext4 ...
751 ... root=/dev/dm-0 dm="system_<slot_suffix> none ro,0 1 android-verity /dev/mmcblk0p<NN>"
752 */
753 snprintf(syspath_buf, syspath_buflen, " %s %s%s %s%d\"",
754 verity_dev,
755 verity_system_part, suffix_slot[current_active_slot],
756 verity_params, system_ptn_index + 1);
757#else
758 /*
759 Condition 5: A/B enabled, but verity disabled
760 Eventual command line looks like:
761 ... androidboot.slot_suffix=<slot_suffix> ... rootfstype=ext4 ...
762 ... root=/dev/mmcblk0p<NN> ...
763 */
764 snprintf(syspath_buf, syspath_buflen, " %s%d",
765 sys_path, system_ptn_index + 1);
766#endif
Mayank Grover3804be72017-06-22 11:59:23 +0530767 }
768 else
769 {
770 lun = partition_get_lun(system_ptn_index);
771 snprintf(syspath_buf, syspath_buflen, " root=/dev/sd%c%d",
772 lun_char_base + lun,
773 partition_get_index_in_lun("system", lun));
774 }
Mayank Grover351a75e2017-05-30 20:06:08 +0530775
Monika Singh292b3e92018-03-17 22:40:23 +0530776#ifndef VERIFIED_BOOT_2
Mayank Grover3804be72017-06-22 11:59:23 +0530777 cmdline_len += strlen(syspath_buf);
Monika Singh292b3e92018-03-17 22:40:23 +0530778#endif
Mayank Grover5384ed82018-05-09 12:09:24 +0530779 }
780
781 if (target_uses_system_as_root() ||
782 partition_multislot_is_supported())
783 {
784 cmdline_len += strlen(sys_path_cmdline);
Mayank Groverb716edf2019-05-08 16:06:55 +0530785
786 /* For dynamic partition, support skip skip_initramfs */
787 if (!target_dynamic_partition_supported() &&
788 !boot_into_recovery)
Mayank Grover351a75e2017-05-30 20:06:08 +0530789 cmdline_len += strlen(skip_ramfs);
790 }
791
Mayank Grover466d6562018-05-10 14:52:20 +0530792#if HIBERNATION_SUPPORT
793 if (platform_boot_dev_isemmc())
794 {
795 swap_ptn_index = partition_get_index("swap");
796 if (swap_ptn_index != INVALID_PTN)
797 {
798 snprintf(resume_buf, resume_buflen,
799 " %s%d", resume,
800 (swap_ptn_index + 1));
801 cmdline_len += strlen(resume_buf);
802 }
803 else
804 {
805 dprintf(INFO, "WARNING: swap partition not found\n");
806 }
807 }
808#endif
809
Channagoud Kadabi85c7ec32016-01-28 23:09:21 -0800810#if TARGET_CMDLINE_SUPPORT
811 char *target_cmdline_buf = malloc(TARGET_MAX_CMDLNBUF);
812 int target_cmd_line_len;
813 ASSERT(target_cmdline_buf);
814 target_cmd_line_len = target_update_cmdline(target_cmdline_buf);
815 cmdline_len += target_cmd_line_len;
816#endif
817
lijuang83ef4b22018-08-23 11:01:55 +0800818#if !VERITY_LE
819 dtbo_idx = get_dtbo_idx ();
820 if (dtbo_idx != INVALID_PTN) {
821 snprintf(dtbo_idx_str, sizeof(dtbo_idx_str), "%s%d",
822 android_boot_dtbo_idx, dtbo_idx);
823 cmdline_len += strlen (dtbo_idx_str);
824 }
Parth Dixit1375d9e2019-07-08 20:56:13 +0530825
826 dtb_idx = get_dtb_idx ();
827 if (dtb_idx != INVALID_PTN) {
828 snprintf(dtb_idx_str, sizeof(dtb_idx_str), "%s%d",
829 android_boot_dtb_idx, dtb_idx);
830 cmdline_len += strlen (dtb_idx_str);
831 }
lijuang83ef4b22018-08-23 11:01:55 +0800832#endif
833
jhchena8a15dd2020-04-24 15:45:57 +0800834#ifdef ENABLE_FUSE_CHECK
835 {
836 int hw_key_status = readl(0xA01D0);
837 is_fused = (hw_key_status & 0x2) >> 1;
838 dprintf(CRITICAL, "hw_key_status = 0x%x, is_fused=%d\n", hw_key_status, is_fused);
839 if (is_fused) {
840 cmdline_len += strlen(fuse_blown);
841 } else {
842 cmdline_len += strlen(fuse_not_blown);
843 }
844 }
845#endif
846
David Ng183a7422009-12-07 14:55:21 -0800847 if (cmdline_len > 0) {
848 const char *src;
Maria Yu52254c02014-07-04 16:14:54 +0800849 unsigned char *dst;
850
851 cmdline_final = (unsigned char*) malloc((cmdline_len + 4) & (~3));
852 ASSERT(cmdline_final != NULL);
vijay kumar287bb542015-09-29 13:01:52 +0530853 memset((void *)cmdline_final, 0, sizeof(*cmdline_final));
Maria Yu52254c02014-07-04 16:14:54 +0800854 dst = cmdline_final;
Neeti Desaie245d492012-06-01 12:52:13 -0700855
Amol Jadi168b7712012-03-06 16:15:00 -0800856 /* Save start ptr for debug print */
David Ng183a7422009-12-07 14:55:21 -0800857 if (have_cmdline) {
858 src = cmdline;
859 while ((*dst++ = *src++));
860 }
861 if (target_is_emmc_boot()) {
862 src = emmc_cmdline;
863 if (have_cmdline) --dst;
David Ngf773dde2010-07-26 19:55:08 -0700864 have_cmdline = 1;
865 while ((*dst++ = *src++));
Sridhar Parasuram7bd4aaf2015-02-12 11:14:38 -0800866#if USE_BOOTDEV_CMDLINE
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700867 src = boot_dev_buf;
Mayank Groverb716edf2019-05-08 16:06:55 +0530868 if (have_cmdline &&
869 boot_dev_buf) {
870 --dst;
871 while ((*dst++ = *src++));
872 }
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700873#endif
Mayank Groverb716edf2019-05-08 16:06:55 +0530874 /* Dynamic partition append boot_devices */
875 if (target_dynamic_partition_supported() &&
876 boot_dev_buf) {
877 src = dynamic_bootdev_cmdline;
878 if (have_cmdline) --dst;
879 while ((*dst++ = *src++));
880 src = boot_dev_buf;
881 if (have_cmdline) --dst;
882 while ((*dst++ = *src++));
883 }
David Ngf773dde2010-07-26 19:55:08 -0700884 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800885
michaellin6be1f392020-04-24 15:05:10 +0800886 switch(GetPcbaVariant())
887 {
888 case 0:
889 src = PCBA_STAGE_0;
890 if (have_cmdline) --dst;
891 while ((*dst++ = *src++));
892 break;
893 case 1:
894 src = PCBA_STAGE_1;
895 if (have_cmdline) --dst;
896 while ((*dst++ = *src++));
897 break;
898 case 2:
899 src = PCBA_STAGE_2;
900 if (have_cmdline) --dst;
901 while ((*dst++ = *src++));
902 break;
903 case 3:
904 src = PCBA_STAGE_3;
905 if (have_cmdline) --dst;
906 while ((*dst++ = *src++));
907 break;
908 case 4:
909 src = PCBA_STAGE_4;
910 if (have_cmdline) --dst;
911 while ((*dst++ = *src++));
912 break;
michaellinaf8857a2020-05-05 14:58:47 +0800913 case 5:
914 src = PCBA_STAGE_5;
915 if (have_cmdline) --dst;
916 while ((*dst++ = *src++));
917 break;
michaellin6be1f392020-04-24 15:05:10 +0800918 default:
919 src = PCBA_STAGE_F;
920 if (have_cmdline) --dst;
921 while ((*dst++ = *src++));
922 break;
923 }
924
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700925#if VERIFIED_BOOT
Mayank Grover912eaa62017-10-26 12:08:53 +0530926 if (VB_M <= target_get_vb_version())
Sridhar Parasuram7d8bb9a2015-09-23 18:21:19 -0700927 {
Mayank Grover889be1b2017-09-12 20:12:23 +0530928 src = verified_state;
929 if(have_cmdline) --dst;
930 have_cmdline = 1;
931 while ((*dst++ = *src++));
932 src = vbsn[boot_state].name;
933 if(have_cmdline) --dst;
934 while ((*dst++ = *src++));
935
936 if ((device.verity_mode != 0 ) && (device.verity_mode != 1))
937 {
938 dprintf(CRITICAL, "Devinfo paritition possibly corrupted!!!. Please erase devinfo partition to continue booting\n");
939 ASSERT(0);
940 }
941 src = verity_mode;
942 if(have_cmdline) --dst;
943 while ((*dst++ = *src++));
944 src = vbvm[device.verity_mode].name;
945 if(have_cmdline) -- dst;
946 while ((*dst++ = *src++));
947 src = keymaster_v1;
948 if(have_cmdline) --dst;
949 while ((*dst++ = *src++));
Sridhar Parasuram7d8bb9a2015-09-23 18:21:19 -0700950 }
Parth Dixitddbc7352015-10-18 03:13:31 +0530951#endif
Monika Singh292b3e92018-03-17 22:40:23 +0530952
953 if (vbcmdline != NULL) {
954 src = vbcmdline;
955 if (have_cmdline) --dst;
956 while ((*dst++ = *src++));
957 }
958
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800959 src = usb_sn_cmdline;
960 if (have_cmdline) --dst;
961 have_cmdline = 1;
962 while ((*dst++ = *src++));
963 src = sn_buf;
964 if (have_cmdline) --dst;
965 have_cmdline = 1;
966 while ((*dst++ = *src++));
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800967 if (warm_boot) {
968 if (have_cmdline) --dst;
969 src = warmboot_cmdline;
970 while ((*dst++ = *src++));
971 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800972
Pavel Nedev5614d222013-06-17 18:01:02 +0300973 if (boot_into_recovery && gpt_exists) {
974 src = secondary_gpt_enable;
975 if (have_cmdline) --dst;
976 while ((*dst++ = *src++));
977 }
Monika Singh292b3e92018-03-17 22:40:23 +0530978#ifdef MDTP_SUPPORT
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200979 if (is_mdtp_activated) {
980 src = mdtp_activated_flag;
981 if (have_cmdline) --dst;
982 while ((*dst++ = *src++));
983 }
Monika Singh292b3e92018-03-17 22:40:23 +0530984#endif
Pavel Nedev328ac822013-04-05 15:25:11 +0300985 if (boot_into_ffbm) {
986 src = androidboot_mode;
987 if (have_cmdline) --dst;
988 while ((*dst++ = *src++));
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700989 src = ffbm_mode_string;
Pavel Nedev328ac822013-04-05 15:25:11 +0300990 if (have_cmdline) --dst;
991 while ((*dst++ = *src++));
Hareesh Gauthamabf1e1e2017-04-12 18:09:24 +0530992
993 if(is_systemd_present) {
994 src = systemd_ffbm_mode;
995 if (have_cmdline) --dst;
996 while ((*dst++ = *src++));
997 }
998
Pavel Nedev898298c2013-02-27 12:36:09 -0800999 src = loglevel;
1000 if (have_cmdline) --dst;
1001 while ((*dst++ = *src++));
Matthew Qind886f3c2014-01-17 16:52:01 +08001002 } else if (boot_reason_alarm) {
1003 src = alarmboot_cmdline;
1004 if (have_cmdline) --dst;
1005 while ((*dst++ = *src++));
Pavel Nedev328ac822013-04-05 15:25:11 +03001006 } else if (pause_at_bootup) {
David Ngf773dde2010-07-26 19:55:08 -07001007 src = battchg_pause;
1008 if (have_cmdline) --dst;
David Ng183a7422009-12-07 14:55:21 -08001009 while ((*dst++ = *src++));
1010 }
Ajay Dudanid04110c2011-01-17 23:55:07 -08001011
Shashank Mittalcd98d472011-08-02 14:29:24 -07001012 if(target_use_signed_kernel() && auth_kernel_img) {
1013 src = auth_kernel;
1014 if (have_cmdline) --dst;
1015 while ((*dst++ = *src++));
1016 }
1017
Ajay Dudanid04110c2011-01-17 23:55:07 -08001018 switch(target_baseband())
1019 {
1020 case BASEBAND_APQ:
1021 src = baseband_apq;
1022 if (have_cmdline) --dst;
1023 while ((*dst++ = *src++));
1024 break;
1025
1026 case BASEBAND_MSM:
1027 src = baseband_msm;
1028 if (have_cmdline) --dst;
1029 while ((*dst++ = *src++));
1030 break;
1031
1032 case BASEBAND_CSFB:
1033 src = baseband_csfb;
1034 if (have_cmdline) --dst;
1035 while ((*dst++ = *src++));
1036 break;
1037
Ajay Dudani6cff85e2011-02-04 16:02:16 -08001038 case BASEBAND_SVLTE2A:
1039 src = baseband_svlte2a;
Ajay Dudanid04110c2011-01-17 23:55:07 -08001040 if (have_cmdline) --dst;
1041 while ((*dst++ = *src++));
1042 break;
Ajay Dudani403bc492011-09-30 16:17:21 -07001043
1044 case BASEBAND_MDM:
1045 src = baseband_mdm;
1046 if (have_cmdline) --dst;
1047 while ((*dst++ = *src++));
1048 break;
Amol Jadi5c61a952012-05-04 17:05:35 -07001049
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -08001050 case BASEBAND_MDM2:
1051 src = baseband_mdm2;
1052 if (have_cmdline) --dst;
1053 while ((*dst++ = *src++));
1054 break;
1055
Amol Jadi5c61a952012-05-04 17:05:35 -07001056 case BASEBAND_SGLTE:
1057 src = baseband_sglte;
1058 if (have_cmdline) --dst;
1059 while ((*dst++ = *src++));
1060 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +05301061
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -08001062 case BASEBAND_SGLTE2:
1063 src = baseband_sglte2;
1064 if (have_cmdline) --dst;
1065 while ((*dst++ = *src++));
1066 break;
1067
Channagoud Kadabi141f2982012-10-31 11:23:02 +05301068 case BASEBAND_DSDA:
1069 src = baseband_dsda;
1070 if (have_cmdline) --dst;
1071 while ((*dst++ = *src++));
1072 break;
Amol Jadi2a15a272013-01-22 12:03:36 -08001073
1074 case BASEBAND_DSDA2:
1075 src = baseband_dsda2;
1076 if (have_cmdline) --dst;
1077 while ((*dst++ = *src++));
1078 break;
Vijay Kumar Pendotib228cfc2016-06-13 20:15:23 +05301079 case BASEBAND_APQ_NOWGR:
1080 src = baseband_apq_nowgr;
1081 if (have_cmdline) --dst;
1082 while ((*dst++ = *src++));
1083 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -08001084 }
Dhaval Patel223ec952013-07-18 14:49:44 -07001085
1086 if (strlen(display_panel_buf)) {
Dhaval Patel223ec952013-07-18 14:49:44 -07001087 src = display_panel_buf;
1088 if (have_cmdline) --dst;
1089 while ((*dst++ = *src++));
1090 }
Joonwoo Park61112782013-10-02 19:50:39 -07001091
1092 if (have_target_boot_params) {
1093 if (have_cmdline) --dst;
1094 src = target_boot_params;
1095 while ((*dst++ = *src++));
vijay kumar870515d2015-08-31 16:37:24 +05301096 free(target_boot_params);
Joonwoo Park61112782013-10-02 19:50:39 -07001097 }
Channagoud Kadabi85c7ec32016-01-28 23:09:21 -08001098
Mayank Grover351a75e2017-05-30 20:06:08 +05301099 if (partition_multislot_is_supported() && have_cmdline)
1100 {
1101 src = androidboot_slot_suffix;
1102 --dst;
1103 while ((*dst++ = *src++));
1104 --dst;
1105 src = SUFFIX_SLOT(current_active_slot);
1106 while ((*dst++ = *src++));
Mayank Grover5384ed82018-05-09 12:09:24 +05301107 }
Mayank Grover351a75e2017-05-30 20:06:08 +05301108
Mayank Grover351a75e2017-05-30 20:06:08 +05301109
Mayank Grover5384ed82018-05-09 12:09:24 +05301110 /*
1111 * System-As-Root behaviour, system.img should contain both
1112 * system content and ramdisk content, and should be mounted at
1113 * root(a/b).
1114 * Apending skip_ramfs for non a/b builds which use, system as root.
1115 */
1116 if ((target_uses_system_as_root() ||
1117 partition_multislot_is_supported()) &&
1118 have_cmdline)
1119 {
Mayank Groverb716edf2019-05-08 16:06:55 +05301120 if (!target_dynamic_partition_supported() &&
1121 !boot_into_recovery)
Mayank Grover5384ed82018-05-09 12:09:24 +05301122 {
1123 src = skip_ramfs;
Mayank Grover351a75e2017-05-30 20:06:08 +05301124 --dst;
1125 while ((*dst++ = *src++));
Mayank Grover5384ed82018-05-09 12:09:24 +05301126 }
1127
1128 src = sys_path_cmdline;
1129 --dst;
1130 while ((*dst++ = *src++));
Mayank Grover3804be72017-06-22 11:59:23 +05301131
Monika Singh292b3e92018-03-17 22:40:23 +05301132#ifndef VERIFIED_BOOT_2
Mayank Grover5384ed82018-05-09 12:09:24 +05301133 src = syspath_buf;
1134 --dst;
1135 while ((*dst++ = *src++));
Monika Singh292b3e92018-03-17 22:40:23 +05301136#endif
Mayank Grover351a75e2017-05-30 20:06:08 +05301137 }
1138
Mayank Grover466d6562018-05-10 14:52:20 +05301139#if HIBERNATION_SUPPORT
1140 if (swap_ptn_index != INVALID_PTN)
1141 {
1142 src = resume_buf;
1143 --dst;
1144 while ((*dst++ = *src++));
1145 }
1146#endif
1147
Channagoud Kadabi85c7ec32016-01-28 23:09:21 -08001148#if TARGET_CMDLINE_SUPPORT
1149 if (target_cmdline_buf && target_cmd_line_len)
1150 {
1151 if (have_cmdline) --dst;
1152 src = target_cmdline_buf;
1153 while((*dst++ = *src++));
1154 free(target_cmdline_buf);
1155 }
1156#endif
lijuang83ef4b22018-08-23 11:01:55 +08001157
1158#if !VERITY_LE
1159 if (dtbo_idx != INVALID_PTN) {
1160 src = dtbo_idx_str;
1161 --dst;
1162 while ((*dst++ = *src++));
1163 }
Parth Dixit1375d9e2019-07-08 20:56:13 +05301164
1165 if (dtb_idx != INVALID_PTN) {
1166 src = dtb_idx_str;
1167 --dst;
1168 while ((*dst++ = *src++));
1169 }
lijuang83ef4b22018-08-23 11:01:55 +08001170#endif
jhchena8a15dd2020-04-24 15:45:57 +08001171
1172#ifdef ENABLE_FUSE_CHECK
1173 if (is_fused) {
1174 src = fuse_blown;
1175 if (have_cmdline) --dst;
1176 while ((*dst++ = *src++));
1177 } else {
1178 src = fuse_not_blown;
1179 if (have_cmdline) --dst;
1180 while ((*dst++ = *src++));
1181 }
1182#endif
Neeti Desaie245d492012-06-01 12:52:13 -07001183 }
Dhaval Patel223ec952013-07-18 14:49:44 -07001184
1185
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -07001186 if (boot_dev_buf)
1187 free(boot_dev_buf);
1188
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -08001189 if (cmdline_final)
1190 dprintf(INFO, "cmdline: %s\n", cmdline_final);
1191 else
1192 dprintf(INFO, "cmdline is NULL\n");
Neeti Desaie245d492012-06-01 12:52:13 -07001193 return cmdline_final;
1194}
1195
1196unsigned *atag_core(unsigned *ptr)
1197{
1198 /* CORE */
1199 *ptr++ = 2;
1200 *ptr++ = 0x54410001;
1201
1202 return ptr;
1203
1204}
1205
1206unsigned *atag_ramdisk(unsigned *ptr, void *ramdisk,
1207 unsigned ramdisk_size)
1208{
1209 if (ramdisk_size) {
1210 *ptr++ = 4;
1211 *ptr++ = 0x54420005;
1212 *ptr++ = (unsigned)ramdisk;
1213 *ptr++ = ramdisk_size;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001214 }
1215
Neeti Desaie245d492012-06-01 12:52:13 -07001216 return ptr;
1217}
1218
1219unsigned *atag_ptable(unsigned **ptr_addr)
1220{
1221 int i;
1222 struct ptable *ptable;
1223
1224 if ((ptable = flash_get_ptable()) && (ptable->count != 0)) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001225 *(*ptr_addr)++ = 2 + (ptable->count * (sizeof(struct atag_ptbl_entry) /
1226 sizeof(unsigned)));
Neeti Desaie245d492012-06-01 12:52:13 -07001227 *(*ptr_addr)++ = 0x4d534d70;
1228 for (i = 0; i < ptable->count; ++i)
1229 ptentry_to_tag(ptr_addr, ptable_get(ptable, i));
1230 }
1231
1232 return (*ptr_addr);
1233}
1234
1235unsigned *atag_cmdline(unsigned *ptr, const char *cmdline)
1236{
1237 int cmdline_length = 0;
1238 int n;
Neeti Desaie245d492012-06-01 12:52:13 -07001239 char *dest;
1240
Amol Jadi10c7d1c2013-01-25 13:24:29 -08001241 cmdline_length = strlen((const char*)cmdline);
Neeti Desaie245d492012-06-01 12:52:13 -07001242 n = (cmdline_length + 4) & (~3);
1243
1244 *ptr++ = (n / 4) + 2;
1245 *ptr++ = 0x54410009;
1246 dest = (char *) ptr;
Amol Jadi10c7d1c2013-01-25 13:24:29 -08001247 while ((*dest++ = *cmdline++));
Neeti Desaie245d492012-06-01 12:52:13 -07001248 ptr += (n / 4);
1249
1250 return ptr;
1251}
1252
1253unsigned *atag_end(unsigned *ptr)
1254{
Brian Swetland9c4c0752009-01-25 16:23:50 -08001255 /* END */
1256 *ptr++ = 0;
1257 *ptr++ = 0;
1258
Neeti Desaie245d492012-06-01 12:52:13 -07001259 return ptr;
1260}
1261
1262void generate_atags(unsigned *ptr, const char *cmdline,
1263 void *ramdisk, unsigned ramdisk_size)
1264{
vijay kumar21a37452015-12-29 16:23:21 +05301265 unsigned *orig_ptr = ptr;
Neeti Desaie245d492012-06-01 12:52:13 -07001266 ptr = atag_core(ptr);
1267 ptr = atag_ramdisk(ptr, ramdisk, ramdisk_size);
1268 ptr = target_atag_mem(ptr);
1269
1270 /* Skip NAND partition ATAGS for eMMC boot */
1271 if (!target_is_emmc_boot()){
1272 ptr = atag_ptable(&ptr);
1273 }
1274
vijay kumar21a37452015-12-29 16:23:21 +05301275 /*
1276 * Atags size filled till + cmdline size + 1 unsigned for 4-byte boundary + 4 unsigned
1277 * for atag identifier in atag_cmdline and atag_end should be with in MAX_TAGS_SIZE bytes
1278 */
Mayank Groverbc8a2ef2018-02-23 18:03:36 +05301279 if (!cmdline)
1280 return;
1281
vijay kumar21a37452015-12-29 16:23:21 +05301282 if (((ptr - orig_ptr) + strlen(cmdline) + 5 * sizeof(unsigned)) < MAX_TAGS_SIZE) {
1283 ptr = atag_cmdline(ptr, cmdline);
1284 ptr = atag_end(ptr);
1285 }
1286 else {
1287 dprintf(CRITICAL,"Crossing ATAGs Max size allowed\n");
1288 ASSERT(0);
1289 }
Neeti Desaie245d492012-06-01 12:52:13 -07001290}
1291
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001292typedef void entry_func_ptr(unsigned, unsigned, unsigned*);
Neeti Desaie245d492012-06-01 12:52:13 -07001293void boot_linux(void *kernel, unsigned *tags,
1294 const char *cmdline, unsigned machtype,
1295 void *ramdisk, unsigned ramdisk_size)
1296{
Amol Jadi10c7d1c2013-01-25 13:24:29 -08001297 unsigned char *final_cmdline;
Amol Jadib6be5c12012-11-14 13:39:51 -08001298#if DEVICE_TREE
Neeti Desai17379b82012-06-04 18:42:53 -07001299 int ret = 0;
Amol Jadib6be5c12012-11-14 13:39:51 -08001300#endif
1301
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001302 void (*entry)(unsigned, unsigned, unsigned*) = (entry_func_ptr*)(PA((addr_t)kernel));
Deepa Dinamani25a9f762012-11-30 15:57:15 -08001303 uint32_t tags_phys = PA((addr_t)tags);
vijay kumar1a50a642015-11-16 12:41:15 +05301304 struct kernel64_hdr *kptr = ((struct kernel64_hdr*)(PA((addr_t)kernel)));
Deepa Dinamani25a9f762012-11-30 15:57:15 -08001305
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301306 ramdisk = (void *)PA((addr_t)ramdisk);
Neeti Desaie245d492012-06-01 12:52:13 -07001307
Amol Jadi10c7d1c2013-01-25 13:24:29 -08001308 final_cmdline = update_cmdline((const char*)cmdline);
1309
Neeti Desai17379b82012-06-04 18:42:53 -07001310#if DEVICE_TREE
Amol Jadib6be5c12012-11-14 13:39:51 -08001311 dprintf(INFO, "Updating device tree: start\n");
1312
Neeti Desai17379b82012-06-04 18:42:53 -07001313 /* Update the Device Tree */
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301314 ret = update_device_tree((void *)tags,(const char *)final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -07001315 if(ret)
1316 {
1317 dprintf(CRITICAL, "ERROR: Updating Device Tree Failed \n");
1318 ASSERT(0);
1319 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001320 dprintf(INFO, "Updating device tree: done\n");
Neeti Desai17379b82012-06-04 18:42:53 -07001321#else
Neeti Desaie245d492012-06-01 12:52:13 -07001322 /* Generating the Atags */
Amol Jadi10c7d1c2013-01-25 13:24:29 -08001323 generate_atags(tags, final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -07001324#endif
Neeti Desaie245d492012-06-01 12:52:13 -07001325
Monika Singh0ec6db82019-07-18 16:57:58 +05301326#if VERIFIED_BOOT
Monika Singhb0db4b82018-09-26 12:18:02 +05301327 if (VB_M <= target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +05301328 {
1329 if (device.verity_mode == 0) {
lijuangbdd9bb42016-03-01 18:22:17 +08001330#if FBCON_DISPLAY_MSG
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07001331#if ENABLE_VB_ATTEST
Mayank Grover889be1b2017-09-12 20:12:23 +05301332 display_bootverify_menu(DISPLAY_MENU_EIO);
1333 wait_for_users_action();
1334 if(!pwr_key_is_pressed)
1335 shutdown_device();
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07001336#else
Mayank Grover889be1b2017-09-12 20:12:23 +05301337 display_bootverify_menu(DISPLAY_MENU_LOGGING);
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07001338#endif
Mayank Grover889be1b2017-09-12 20:12:23 +05301339 wait_for_users_action();
lijuangbdd9bb42016-03-01 18:22:17 +08001340#else
Mayank Grover889be1b2017-09-12 20:12:23 +05301341 dprintf(CRITICAL,
1342 "The dm-verity is not started in enforcing mode.\nWait for 5 seconds before proceeding\n");
1343 mdelay(5000);
lijuangbdd9bb42016-03-01 18:22:17 +08001344#endif
Mayank Grover889be1b2017-09-12 20:12:23 +05301345 }
lijuangbdd9bb42016-03-01 18:22:17 +08001346 }
lijuangbdd9bb42016-03-01 18:22:17 +08001347#endif
1348
1349#if VERIFIED_BOOT
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001350 /* Write protect the device info */
Channagoud Kadabi3bd9d1e2015-05-05 16:18:20 -07001351 if (!boot_into_recovery && target_build_variant_user() && devinfo_present && mmc_write_protect("devinfo", 1))
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001352 {
1353 dprintf(INFO, "Failed to write protect dev info\n");
1354 ASSERT(0);
1355 }
1356#endif
1357
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08001358 /* Turn off splash screen if enabled */
1359#if DISPLAY_SPLASH_SCREEN
1360 target_display_shutdown();
1361#endif
1362
Veera Sundaram Sankaran67ea0932015-09-25 10:09:30 -07001363 /* Perform target specific cleanup */
1364 target_uninit();
Monika Singh292b3e92018-03-17 22:40:23 +05301365 free_verified_boot_resource(&info);
1366 if (final_cmdline)
1367 free(final_cmdline);
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08001368
Deepa Dinamani33734bc2013-03-06 12:16:06 -08001369 dprintf(INFO, "booting linux @ %p, ramdisk @ %p (%d), tags/device tree @ %p\n",
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301370 entry, ramdisk, ramdisk_size, (void *)tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001371
1372 enter_critical_section();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001373
Amol Jadi4421e652011-06-16 15:00:48 -07001374 /* do any platform specific cleanup before kernel entry */
1375 platform_uninit();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001376
Brian Swetland9c4c0752009-01-25 16:23:50 -08001377 arch_disable_cache(UCACHE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001378
Amol Jadi504f9fe2012-08-16 13:56:48 -07001379#if ARM_WITH_MMU
Brian Swetland9c4c0752009-01-25 16:23:50 -08001380 arch_disable_mmu();
Amol Jadi504f9fe2012-08-16 13:56:48 -07001381#endif
Amol Jadi492d5a52013-03-15 16:12:34 -07001382 bs_set_timestamp(BS_KERNEL_ENTRY);
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001383
1384 if (IS_ARM64(kptr))
1385 /* Jump to a 64bit kernel */
1386 scm_elexec_call((paddr_t)kernel, tags_phys);
1387 else
1388 /* Jump to a 32bit kernel */
1389 entry(0, machtype, (unsigned*)tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001390}
1391
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001392/* Function to check if the memory address range falls within the aboot
1393 * boundaries.
1394 * start: Start of the memory region
1395 * size: Size of the memory region
1396 */
Vijay Kumar Pendoti9c002ad2016-03-09 13:52:45 +05301397int check_aboot_addr_range_overlap(uintptr_t start, uint32_t size)
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001398{
1399 /* Check for boundary conditions. */
Sundarajan Srinivasance2a0ea2013-12-16 17:02:56 -08001400 if ((UINT_MAX - start) < size)
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001401 return -1;
1402
1403 /* Check for memory overlap. */
1404 if ((start < MEMBASE) && ((start + size) <= MEMBASE))
1405 return 0;
Channagoud Kadabi94143912013-10-15 12:53:52 -07001406 else if (start >= (MEMBASE + MEMSIZE))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001407 return 0;
1408 else
1409 return -1;
1410}
1411
Mayank Grovere559cec2017-10-17 15:12:03 +05301412/* Function to check if the memory address range falls beyond ddr region.
1413 * start: Start of the memory region
1414 * size: Size of the memory region
1415 */
1416int check_ddr_addr_range_bound(uintptr_t start, uint32_t size)
1417{
1418 uintptr_t ddr_pa_start_addr = PA(get_ddr_start());
1419 uint64_t ddr_size = smem_get_ddr_size();
1420 uint64_t ddr_pa_end_addr = ddr_pa_start_addr + ddr_size;
1421 uintptr_t pa_start_addr = PA(start);
1422
1423 /* Check for boundary conditions. */
1424 if ((UINT_MAX - start) < size)
1425 return -1;
1426
1427 /* Check if memory range is beyond the ddr range. */
1428 if (pa_start_addr < ddr_pa_start_addr ||
1429 pa_start_addr >= (ddr_pa_end_addr) ||
1430 (pa_start_addr + size) > ddr_pa_end_addr)
1431 return -1;
1432 else
1433 return 0;
1434}
1435
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001436BUF_DMA_ALIGN(buf, BOOT_IMG_MAX_PAGE_SIZE); //Equal to max-supported pagesize
Dima Zavin214cc642009-01-26 11:16:21 -08001437
Mayank Groverdd080e82018-09-04 20:12:13 +05301438int getimage(void **image_buffer, uint32_t *imgsize,
1439 const char *imgname)
Monika Singh292b3e92018-03-17 22:40:23 +05301440{
Mayank Groverdd080e82018-09-04 20:12:13 +05301441 uint32_t loadedindex;
1442 if (image_buffer == NULL || imgsize == NULL ||
Monika Singh292b3e92018-03-17 22:40:23 +05301443 imgname == NULL) {
1444 dprintf(CRITICAL, "getimage: invalid parameters\n");
1445 return -1;
1446 }
Mayank Groverdd080e82018-09-04 20:12:13 +05301447 for (loadedindex = 0; loadedindex < info.num_loaded_images; loadedindex++) {
1448 if (!strncmp(info.images[loadedindex].name, imgname,
Monika Singh292b3e92018-03-17 22:40:23 +05301449 strlen(imgname))) {
Mayank Groverdd080e82018-09-04 20:12:13 +05301450 *image_buffer = info.images[loadedindex].image_buffer;
1451 *imgsize = info.images[loadedindex].imgsize;
Mayank Grover027a9412018-09-04 15:12:05 +05301452 dprintf(SPEW, "getimage(): Loaded image [%s|%d]\n",
1453 info.images[loadedindex].name,
1454 info.images[loadedindex].imgsize);
Monika Singh292b3e92018-03-17 22:40:23 +05301455 return 0;
1456 }
1457 }
1458 return -1;
1459}
1460
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001461static void verify_signed_bootimg(uint32_t bootimg_addr, uint32_t bootimg_size)
1462{
1463 int ret;
Channagoud Kadabia8c623f2015-01-13 14:48:48 -08001464
1465#if !VERIFIED_BOOT
Sundarajan Srinivasance54d6e2013-11-11 12:45:00 -08001466#if IMAGE_VERIF_ALGO_SHA1
1467 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA1;
1468#else
1469 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA256;
1470#endif
Channagoud Kadabia8c623f2015-01-13 14:48:48 -08001471#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001472
1473 /* Assume device is rooted at this time. */
1474 device.is_tampered = 1;
1475
1476 dprintf(INFO, "Authenticating boot image (%d): start\n", bootimg_size);
1477
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001478#if VERIFIED_BOOT
Monika Singh7d2fc272018-03-16 17:16:01 +05301479 uint32_t bootstate;
1480 if(boot_into_recovery &&
Mayank Grover68fbf0d2017-10-24 14:13:39 +05301481 (!partition_multislot_is_supported()))
Monika Singh7d2fc272018-03-16 17:16:01 +05301482 {
1483 ret = boot_verify_image((unsigned char *)bootimg_addr,
1484 bootimg_size, "/recovery", &bootstate);
1485 }
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001486 else
Monika Singh7d2fc272018-03-16 17:16:01 +05301487 {
1488 ret = boot_verify_image((unsigned char *)bootimg_addr,
1489 bootimg_size, "/boot", &bootstate);
1490 }
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001491 boot_verify_print_state();
1492#else
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001493 ret = image_verify((unsigned char *)bootimg_addr,
1494 (unsigned char *)(bootimg_addr + bootimg_size),
1495 bootimg_size,
Sundarajan Srinivasance54d6e2013-11-11 12:45:00 -08001496 auth_algo);
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001497#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001498 dprintf(INFO, "Authenticating boot image: done return value = %d\n", ret);
1499
1500 if (ret)
1501 {
1502 /* Authorized kernel */
1503 device.is_tampered = 0;
Sundarajan Srinivasan3fb21f12013-09-16 18:36:15 -07001504 auth_kernel_img = 1;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001505 }
1506
Amit Blay4aa292f2015-04-28 21:55:59 +03001507#ifdef MDTP_SUPPORT
1508 {
1509 /* Verify MDTP lock.
1510 * For boot & recovery partitions, use aboot's verification result.
1511 */
1512 mdtp_ext_partition_verification_t ext_partition;
1513 ext_partition.partition = boot_into_recovery ? MDTP_PARTITION_RECOVERY : MDTP_PARTITION_BOOT;
1514 ext_partition.integrity_state = device.is_tampered ? MDTP_PARTITION_STATE_INVALID : MDTP_PARTITION_STATE_VALID;
1515 ext_partition.page_size = 0; /* Not needed since already validated */
1516 ext_partition.image_addr = 0; /* Not needed since already validated */
1517 ext_partition.image_size = 0; /* Not needed since already validated */
1518 ext_partition.sig_avail = FALSE; /* Not needed since already validated */
1519 mdtp_fwlock_verify_lock(&ext_partition);
1520 }
1521#endif /* MDTP_SUPPORT */
1522
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001523#if USE_PCOM_SECBOOT
1524 set_tamper_flag(device.is_tampered);
1525#endif
1526
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001527#if VERIFIED_BOOT
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001528 switch(boot_verify_get_state())
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001529 {
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001530 case RED:
lijuanga40d6302015-07-20 20:10:13 +08001531#if FBCON_DISPLAY_MSG
lijuang9a7d3b92015-11-30 14:41:24 +08001532 display_bootverify_menu(DISPLAY_MENU_RED);
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07001533#if ENABLE_VB_ATTEST
1534 mdelay(DELAY_WAIT);
1535 shutdown_device();
1536#else
lijuanga40d6302015-07-20 20:10:13 +08001537 wait_for_users_action();
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07001538#endif
lijuanga40d6302015-07-20 20:10:13 +08001539#else
1540 dprintf(CRITICAL,
1541 "Your device has failed verification and may not work properly.\nWait for 5 seconds before proceeding\n");
1542 mdelay(5000);
1543#endif
1544
1545 break;
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001546 case YELLOW:
lijuanga40d6302015-07-20 20:10:13 +08001547#if FBCON_DISPLAY_MSG
lijuang9a7d3b92015-11-30 14:41:24 +08001548 display_bootverify_menu(DISPLAY_MENU_YELLOW);
lijuanga40d6302015-07-20 20:10:13 +08001549 wait_for_users_action();
1550#else
1551 dprintf(CRITICAL,
1552 "Your device has loaded a different operating system.\nWait for 5 seconds before proceeding\n");
1553 mdelay(5000);
1554#endif
1555 break;
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001556 default:
lijuanga40d6302015-07-20 20:10:13 +08001557 break;
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001558 }
1559#endif
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001560#if !VERIFIED_BOOT
Unnati Gandhi1be04752015-03-27 19:41:53 +05301561 if(device.is_tampered)
1562 {
1563 write_device_info_mmc(&device);
1564 #ifdef TZ_TAMPER_FUSE
Monika Singh98257462018-06-06 11:28:49 +05301565 set_tamper_fuse_cmd(HLOS_IMG_TAMPER_FUSE);
Unnati Gandhi1be04752015-03-27 19:41:53 +05301566 #endif
1567 #ifdef ASSERT_ON_TAMPER
1568 dprintf(CRITICAL, "Device is tampered. Asserting..\n");
1569 ASSERT(0);
1570 #endif
1571 }
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001572#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001573}
1574
Maunik Shah0f3c8ac2014-03-02 17:47:58 +05301575static bool check_format_bit()
1576{
1577 bool ret = false;
1578 int index;
1579 uint64_t offset;
1580 struct boot_selection_info *in = NULL;
1581 char *buf = NULL;
1582
1583 index = partition_get_index("bootselect");
1584 if (index == INVALID_PTN)
1585 {
1586 dprintf(INFO, "Unable to locate /bootselect partition\n");
1587 return ret;
1588 }
1589 offset = partition_get_offset(index);
1590 if(!offset)
1591 {
1592 dprintf(INFO, "partition /bootselect doesn't exist\n");
1593 return ret;
1594 }
1595 buf = (char *) memalign(CACHE_LINE, ROUNDUP(page_size, CACHE_LINE));
Mayank Grover48860402016-11-29 12:34:53 +05301596 mmc_set_lun(partition_get_lun(index));
Maunik Shah0f3c8ac2014-03-02 17:47:58 +05301597 ASSERT(buf);
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301598 if (mmc_read(offset, (uint32_t *)buf, page_size))
Maunik Shah0f3c8ac2014-03-02 17:47:58 +05301599 {
1600 dprintf(INFO, "mmc read failure /bootselect %d\n", page_size);
1601 free(buf);
1602 return ret;
1603 }
1604 in = (struct boot_selection_info *) buf;
1605 if ((in->signature == BOOTSELECT_SIGNATURE) &&
1606 (in->version == BOOTSELECT_VERSION)) {
1607 if ((in->state_info & BOOTSELECT_FORMAT) &&
1608 !(in->state_info & BOOTSELECT_FACTORY))
1609 ret = true;
1610 } else {
1611 dprintf(CRITICAL, "Signature: 0x%08x or version: 0x%08x mismatched of /bootselect\n",
1612 in->signature, in->version);
1613 ASSERT(0);
1614 }
1615 free(buf);
1616 return ret;
1617}
1618
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001619void boot_verifier_init()
1620{
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001621 uint32_t boot_state;
1622 /* Check if device unlock */
1623 if(device.is_unlocked)
1624 {
1625 boot_verify_send_event(DEV_UNLOCK);
1626 boot_verify_print_state();
1627 dprintf(CRITICAL, "Device is unlocked! Skipping verification...\n");
1628 return;
1629 }
1630 else
1631 {
1632 boot_verify_send_event(BOOT_INIT);
1633 }
1634
1635 /* Initialize keystore */
1636 boot_state = boot_verify_keystore_init();
1637 if(boot_state == YELLOW)
1638 {
1639 boot_verify_print_state();
1640 dprintf(CRITICAL, "Keystore verification failed! Continuing anyways...\n");
1641 }
1642}
1643
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301644/* Function to return recovery appended dtbo buffer info */
1645void get_recovery_dtbo_info(uint32_t *dtbo_size, void **dtbo_buf)
1646{
1647 *dtbo_size = recovery_dtbo_size;
1648 *dtbo_buf = recovery_dtbo_buf;
1649 return;
1650}
1651
Shashank Mittal23b8f422010-04-16 19:27:21 -07001652int boot_linux_from_mmc(void)
1653{
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301654 boot_img_hdr *hdr = (void*) buf;
1655 boot_img_hdr *uhdr;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001656 unsigned offset = 0;
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07001657 int rcode;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001658 unsigned long long ptn = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001659 int index = INVALID_PTN;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001660
Shashank Mittalcd98d472011-08-02 14:29:24 -07001661 unsigned char *image_addr = 0;
1662 unsigned kernel_actual;
1663 unsigned ramdisk_actual;
1664 unsigned imagesize_actual;
Neeti Desai465491e2012-07-31 12:53:35 -07001665 unsigned second_actual = 0;
Parth Dixit1375d9e2019-07-08 20:56:13 +05301666 void * image_buf = NULL;
Neeti Desai465491e2012-07-31 12:53:35 -07001667
Matthew Qin271927e2015-03-31 22:07:07 -04001668 unsigned int dtb_size = 0;
Parth Dixit1375d9e2019-07-08 20:56:13 +05301669 unsigned dtb_image_size = 0;
1670 uint32_t dtb_image_offset = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08001671 unsigned int out_len = 0;
1672 unsigned int out_avai_len = 0;
1673 unsigned char *out_addr = NULL;
1674 uint32_t dtb_offset = 0;
1675 unsigned char *kernel_start_addr = NULL;
1676 unsigned int kernel_size = 0;
Ameya Thakur10a33452016-06-13 14:24:26 -07001677 unsigned int patched_kernel_hdr_size = 0;
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301678 uint64_t image_size = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08001679 int rc;
Monika Singh292b3e92018-03-17 22:40:23 +05301680#if VERIFIED_BOOT_2
1681 int status;
Mayank Grover027a9412018-09-04 15:12:05 +05301682 void *dtbo_image_buf = NULL;
1683 uint32_t dtbo_image_sz = 0;
Mayank Grover9df84c02018-08-30 15:46:35 +05301684 void *vbmeta_image_buf = NULL;
1685 uint32_t vbmeta_image_sz = 0;
Monika Singh292b3e92018-03-17 22:40:23 +05301686#endif
Mayank Groverc93cad82017-10-03 12:23:45 +05301687 char *ptn_name = NULL;
Neeti Desai465491e2012-07-31 12:53:35 -07001688#if DEVICE_TREE
1689 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -07001690 struct dt_entry dt_entry;
Neeti Desai465491e2012-07-31 12:53:35 -07001691 unsigned dt_table_offset;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001692 uint32_t dt_actual;
Deepa Dinamani19648b42013-09-05 17:05:55 -07001693 uint32_t dt_hdr_size;
Matthew Qin271927e2015-03-31 22:07:07 -04001694 unsigned char *best_match_dt_addr = NULL;
Neeti Desai465491e2012-07-31 12:53:35 -07001695#endif
Matthew Qin49e51fa2015-02-09 10:40:45 +08001696 struct kernel64_hdr *kptr = NULL;
Mayank Grover351a75e2017-05-30 20:06:08 +05301697 int current_active_slot = INVALID;
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001698
Maunik Shah0f3c8ac2014-03-02 17:47:58 +05301699 if (check_format_bit())
1700 boot_into_recovery = 1;
1701
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07001702 if (!boot_into_recovery) {
1703 memset(ffbm_mode_string, '\0', sizeof(ffbm_mode_string));
1704 rcode = get_ffbm(ffbm_mode_string, sizeof(ffbm_mode_string));
1705 if (rcode <= 0) {
1706 boot_into_ffbm = false;
1707 if (rcode < 0)
1708 dprintf(CRITICAL,"failed to get ffbm cookie");
1709 } else
1710 boot_into_ffbm = true;
1711 } else
1712 boot_into_ffbm = false;
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301713 uhdr = (boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001714 if (!memcmp(uhdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1715 dprintf(INFO, "Unified boot method!\n");
1716 hdr = uhdr;
1717 goto unified_boot;
1718 }
Mayank Groverc93cad82017-10-03 12:23:45 +05301719
1720 /* For a/b recovery image code is on boot partition.
1721 If we support multislot, always use boot partition. */
1722 if (boot_into_recovery &&
Mayank Grover8ab4a762019-04-25 17:23:34 +05301723 ((!partition_multislot_is_supported()) ||
1724 (target_dynamic_partition_supported())))
Mayank Groverc93cad82017-10-03 12:23:45 +05301725 ptn_name = "recovery";
1726 else
1727 ptn_name = "boot";
1728
1729 index = partition_get_index(ptn_name);
1730 ptn = partition_get_offset(index);
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301731 image_size = partition_get_size(index);
1732 if(ptn == 0 || image_size == 0) {
Mayank Groverc93cad82017-10-03 12:23:45 +05301733 dprintf(CRITICAL, "ERROR: No %s partition found\n", ptn_name);
1734 return -1;
Kinson Chikf1a43512011-07-14 11:28:39 -07001735 }
Mayank Groverc93cad82017-10-03 12:23:45 +05301736
Channagoud Kadabief0547c2015-02-10 12:57:38 -08001737 /* Set Lun for boot & recovery partitions */
1738 mmc_set_lun(partition_get_lun(index));
Shashank Mittal23b8f422010-04-16 19:27:21 -07001739
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301740 if (mmc_read(ptn + offset, (uint32_t *) buf, page_size)) {
Shashank Mittal23b8f422010-04-16 19:27:21 -07001741 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
1742 return -1;
1743 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001744
1745 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001746 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Mayank Grover351a75e2017-05-30 20:06:08 +05301747 return ERR_INVALID_BOOT_MAGIC;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001748 }
1749
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07001750 if (hdr->page_size && (hdr->page_size != page_size)) {
vijay kumar2e21b3a2014-06-26 17:40:15 +05301751
1752 if (hdr->page_size > BOOT_IMG_MAX_PAGE_SIZE) {
1753 dprintf(CRITICAL, "ERROR: Invalid page size\n");
1754 return -1;
1755 }
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07001756 page_size = hdr->page_size;
1757 page_mask = page_size - 1;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001758 }
1759
Matthew Qin49e51fa2015-02-09 10:40:45 +08001760 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1761 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Kishor PKd8ddcad2017-07-27 13:53:57 +05301762 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001763
Matthew Qin49e51fa2015-02-09 10:40:45 +08001764 image_addr = (unsigned char *)target_get_scratch_address();
Mayank Grover6fd36792018-09-10 13:26:58 +05301765#if VERIFIED_BOOT_2
1766 /* Create hole in start of image for VB salt to copy */
1767 image_addr += SALT_BUFF_OFFSET;
1768#endif
Kishor PK9e91b592017-05-24 12:14:55 +05301769 memcpy(image_addr, (void *)buf, page_size);
1770
1771 /* ensure commandline is terminated */
1772 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
Matthew Qin49e51fa2015-02-09 10:40:45 +08001773
1774#if DEVICE_TREE
Parth Dixit4097b622016-03-15 14:42:27 +05301775#ifndef OSVERSION_IN_BOOTIMAGE
1776 dt_size = hdr->dt_size;
Mayank Grover5a502582018-09-12 11:24:49 +05301777#else
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301778 dprintf(INFO, "BootImage Header: %d\n", hdr->header_version);
Mayank Grover5a502582018-09-12 11:24:49 +05301779#endif
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301780
Parth Dixit4097b622016-03-15 14:42:27 +05301781 dt_actual = ROUND_TO_PAGE(dt_size, page_mask);
Kishor PKd8ddcad2017-07-27 13:53:57 +05301782 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 +05301783 dprintf(CRITICAL, "Integer overflow detected in bootimage header fields at %u in %s\n",__LINE__,__FILE__);
1784 return -1;
1785 }
Kishor PKd8ddcad2017-07-27 13:53:57 +05301786 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + second_actual + dt_actual);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001787#else
Kishor PKd8ddcad2017-07-27 13:53:57 +05301788 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 +05301789 dprintf(CRITICAL, "Integer overflow detected in bootimage header fields at %u in %s\n",__LINE__,__FILE__);
1790 return -1;
1791 }
Kishor PKd8ddcad2017-07-27 13:53:57 +05301792 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + second_actual);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001793#endif
Parth Dixit1375d9e2019-07-08 20:56:13 +05301794 dtb_image_size = hdr->kernel_size;
Matthew Qin49e51fa2015-02-09 10:40:45 +08001795
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301796#ifdef OSVERSION_IN_BOOTIMAGE
1797 /* If header version is ONE and booting into recovery,
1798 dtbo is appended with recovery image.
1799 Doing following:
1800 * Validating the recovery offset and size.
1801 * Extracting recovery dtbo to be used as dtbo.
1802 */
1803 if (boot_into_recovery &&
1804 hdr->header_version == BOOT_HEADER_VERSION_ONE)
1805 {
1806 struct boot_img_hdr_v1 *hdr1 =
1807 (struct boot_img_hdr_v1 *) (image_addr + sizeof(boot_img_hdr));
Mayank Grovera1a83c72018-09-24 11:23:15 +05301808 unsigned int recovery_dtbo_actual = 0;
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301809
Mayank Grovera1a83c72018-09-24 11:23:15 +05301810 recovery_dtbo_actual = ROUND_TO_PAGE(hdr1->recovery_dtbo_size, page_mask);
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301811 if ((hdr1->header_size !=
1812 sizeof(struct boot_img_hdr_v1) + sizeof(boot_img_hdr)))
1813 {
1814 dprintf(CRITICAL, "Invalid boot image header: %d\n", hdr1->header_size);
1815 return -1;
1816 }
1817
Mayank Grovera1a83c72018-09-24 11:23:15 +05301818 if (recovery_dtbo_actual > MAX_SUPPORTED_DTBO_IMG_BUF)
1819 {
1820 dprintf(CRITICAL, "Recovery Dtbo Size too big %x, Allowed size %x\n", recovery_dtbo_actual,
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301821 MAX_SUPPORTED_DTBO_IMG_BUF);
1822 return -1;
1823 }
1824
Mayank Grovera1a83c72018-09-24 11:23:15 +05301825 if (UINT_MAX < ((uint64_t)imagesize_actual + recovery_dtbo_actual))
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301826 {
1827 dprintf(CRITICAL, "Integer overflow detected in recoveryimage header fields at %u in %s\n",__LINE__,__FILE__);
1828 return -1;
1829 }
1830
Mayank Grovera1a83c72018-09-24 11:23:15 +05301831 if (UINT_MAX < (hdr1->recovery_dtbo_offset + recovery_dtbo_actual)) {
1832 dprintf(CRITICAL,
1833 "Integer overflow detected in recovery image header fields at %u in %s\n",__LINE__,__FILE__);
1834 return -1;
1835 }
1836
1837 if (hdr1->recovery_dtbo_offset + recovery_dtbo_actual > image_size)
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301838 {
1839 dprintf(CRITICAL, "Invalid recovery dtbo: Recovery Dtbo Offset=0x%llx,"
1840 " Recovery Dtbo Size=0x%x, Image Size=0x%llx\n",
1841 hdr1->recovery_dtbo_offset, recovery_dtbo_size, image_size);
1842 return -1;
1843 }
1844
1845 recovery_dtbo_buf = (void *)(hdr1->recovery_dtbo_offset + image_addr);
Mayank Grovera1a83c72018-09-24 11:23:15 +05301846 recovery_dtbo_size = recovery_dtbo_actual;
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301847 imagesize_actual += recovery_dtbo_size;
1848
1849 dprintf(SPEW, "Header version: %d\n", hdr->header_version);
1850 dprintf(SPEW, "Recovery Dtbo Size 0x%x\n", recovery_dtbo_size);
1851 dprintf(SPEW, "Recovery Dtbo Offset 0x%llx\n", hdr1->recovery_dtbo_offset);
1852
1853 }
Parth Dixit1375d9e2019-07-08 20:56:13 +05301854
1855 if ( hdr->header_version == BOOT_HEADER_VERSION_TWO) {
1856 struct boot_img_hdr_v1 *hdr1 =
1857 (struct boot_img_hdr_v1 *) (image_addr + sizeof(boot_img_hdr));
1858 struct boot_img_hdr_v2 *hdr2 = (struct boot_img_hdr_v2 *)
1859 (image_addr + sizeof(boot_img_hdr) +
1860 BOOT_IMAGE_HEADER_V2_OFFSET);
1861 unsigned int recovery_dtbo_actual = 0;
1862
lijuang0b17ba22019-09-04 14:20:47 +08001863 recovery_dtbo_actual =
1864 ROUND_TO_PAGE(hdr1->recovery_dtbo_size, page_mask);
1865 imagesize_actual += recovery_dtbo_actual;
Parth Dixit1375d9e2019-07-08 20:56:13 +05301866
1867 imagesize_actual += ROUND_TO_PAGE(hdr2->dtb_size, page_mask);
1868
1869
1870 dtb_image_offset = page_size + patched_kernel_hdr_size +
1871 kernel_actual + ramdisk_actual + second_actual +
1872 recovery_dtbo_actual;
1873
1874 dprintf(SPEW, "Header version: %d\n", hdr->header_version);
1875 dprintf(SPEW, "Dtb image offset 0x%x\n", dtb_image_offset);
1876 }
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301877#endif
1878
Parth Dixit1375d9e2019-07-08 20:56:13 +05301879
Saranya Chidurab1aaf232018-11-19 15:47:50 +05301880 /* Validate the boot/recovery image size is within the bounds of partition size */
1881 if (imagesize_actual > image_size) {
1882 dprintf(CRITICAL, "Image size is greater than partition size.\n");
1883 return -1;
1884 }
1885
Matthew Qin49e51fa2015-02-09 10:40:45 +08001886#if VERIFIED_BOOT
1887 boot_verifier_init();
1888#endif
1889
Mayank Grover1ceaee22019-04-01 17:54:32 +05301890#if VERIFIED_BOOT_2
1891 /* read full partition if device is unlocked */
1892 if (device.is_unlocked)
1893 imagesize_actual = image_size;
1894#endif
1895
Vijay Kumar Pendoti9c002ad2016-03-09 13:52:45 +05301896 if (check_aboot_addr_range_overlap((uintptr_t) image_addr, imagesize_actual))
Matthew Qin49e51fa2015-02-09 10:40:45 +08001897 {
1898 dprintf(CRITICAL, "Boot image buffer address overlaps with aboot addresses.\n");
1899 return -1;
1900 }
1901
Matthew Qinbb7923d2015-02-09 10:56:09 +08001902 /*
1903 * Update loading flow of bootimage to support compressed/uncompressed
1904 * bootimage on both 64bit and 32bit platform.
1905 * 1. Load bootimage from emmc partition onto DDR.
1906 * 2. Check if bootimage is gzip format. If yes, decompress compressed kernel
1907 * 3. Check kernel header and update kernel load addr for 64bit and 32bit
1908 * platform accordingly.
1909 * 4. Sanity Check on kernel_addr and ramdisk_addr and copy data.
1910 */
Mayank Grover351a75e2017-05-30 20:06:08 +05301911 if (partition_multislot_is_supported())
1912 {
1913 current_active_slot = partition_find_active_slot();
1914 dprintf(INFO, "Loading boot image (%d) active_slot(%s): start\n",
1915 imagesize_actual, SUFFIX_SLOT(current_active_slot));
1916 }
1917 else
1918 {
1919 dprintf(INFO, "Loading (%s) image (%d): start\n",
1920 (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual);
1921 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08001922 bs_set_timestamp(BS_KERNEL_LOAD_START);
1923
Gaurav Nebhwani43e2a462016-03-17 21:32:56 +05301924 if ((target_get_max_flash_size() - page_size) < imagesize_actual)
1925 {
1926 dprintf(CRITICAL, "booimage size is greater than DDR can hold\n");
1927 return -1;
1928 }
Kishor PK9e91b592017-05-24 12:14:55 +05301929 offset = page_size;
1930 /* Read image without signature and header*/
1931 if (mmc_read(ptn + offset, (void *)(image_addr + offset), imagesize_actual - page_size))
Matthew Qin49e51fa2015-02-09 10:40:45 +08001932 {
1933 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
1934 return -1;
1935 }
1936
Mayank Grover351a75e2017-05-30 20:06:08 +05301937 if (partition_multislot_is_supported())
1938 {
1939 dprintf(INFO, "Loading boot image (%d) active_slot(%s): done\n",
1940 imagesize_actual, SUFFIX_SLOT(current_active_slot));
1941 }
1942 else
1943 {
1944 dprintf(INFO, "Loading (%s) image (%d): done\n",
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001945 (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual);
1946
Mayank Grover351a75e2017-05-30 20:06:08 +05301947 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08001948 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
1949
1950 /* Authenticate Kernel */
1951 dprintf(INFO, "use_signed_kernel=%d, is_unlocked=%d, is_tampered=%d.\n",
1952 (int) target_use_signed_kernel(),
1953 device.is_unlocked,
1954 device.is_tampered);
Monika Singh292b3e92018-03-17 22:40:23 +05301955#if VERIFIED_BOOT_2
Mayank Grover1ceaee22019-04-01 17:54:32 +05301956 /* if device is unlocked skip reading signature, as full partition is read */
1957 if (!device.is_unlocked)
Monika Singh292b3e92018-03-17 22:40:23 +05301958 {
Mayank Grover1ceaee22019-04-01 17:54:32 +05301959 offset = imagesize_actual;
1960 if (check_aboot_addr_range_overlap((uintptr_t)image_addr + offset, page_size))
1961 {
1962 dprintf(CRITICAL, "Signature read buffer address overlaps with aboot addresses.\n");
1963 return -1;
1964 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08001965
Mayank Grover1ceaee22019-04-01 17:54:32 +05301966 /* Read signature */
1967 if(mmc_read(ptn + offset, (void *)(image_addr + offset), page_size))
1968 {
1969 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
1970 return -1;
1971 }
Monika Singh292b3e92018-03-17 22:40:23 +05301972 }
1973
Mayank Grover027a9412018-09-04 15:12:05 +05301974 /* load and validate dtbo partition */
1975 load_validate_dtbo_image(&dtbo_image_buf, &dtbo_image_sz);
1976
Mayank Grover9df84c02018-08-30 15:46:35 +05301977 /* load vbmeta partition */
1978 load_vbmeta_image(&vbmeta_image_buf, &vbmeta_image_sz);
1979
Monika Singh292b3e92018-03-17 22:40:23 +05301980 memset(&info, 0, sizeof(bootinfo));
Mayank Grover027a9412018-09-04 15:12:05 +05301981
1982 /* Pass loaded boot image passed */
Mayank Grover6fd36792018-09-10 13:26:58 +05301983 info.images[IMG_BOOT].image_buffer = SUB_SALT_BUFF_OFFSET(image_addr);
Mayank Grover027a9412018-09-04 15:12:05 +05301984 info.images[IMG_BOOT].imgsize = imagesize_actual;
1985 info.images[IMG_BOOT].name = ptn_name;
1986 ++info.num_loaded_images;
1987
1988 /* Pass loaded dtbo image */
1989 if (dtbo_image_buf != NULL) {
Mayank Grover6fd36792018-09-10 13:26:58 +05301990 info.images[IMG_DTBO].image_buffer =
1991 SUB_SALT_BUFF_OFFSET(dtbo_image_buf);
Mayank Grover027a9412018-09-04 15:12:05 +05301992 info.images[IMG_DTBO].imgsize = dtbo_image_sz;
1993 info.images[IMG_DTBO].name = "dtbo";
1994 ++info.num_loaded_images;
1995 }
1996
Mayank Grover9df84c02018-08-30 15:46:35 +05301997 /* Pass loaded vbmeta image */
1998 if (vbmeta_image_buf != NULL) {
1999 info.images[IMG_VBMETA].image_buffer = vbmeta_image_buf;
2000 info.images[IMG_VBMETA].imgsize = vbmeta_image_sz;
2001 info.images[IMG_VBMETA].name = "vbmeta";
2002 ++info.num_loaded_images;
2003 }
2004
Mayank Grovere1ab96c2018-09-04 20:31:31 +05302005 info.header_version = hdr->header_version;
Monika Singh292b3e92018-03-17 22:40:23 +05302006 info.multi_slot_boot = partition_multislot_is_supported();
2007 info.bootreason_alarm = boot_reason_alarm;
2008 info.bootinto_recovery = boot_into_recovery;
2009 status = load_image_and_auth(&info);
2010 if(status)
2011 return -1;
2012
2013 vbcmdline = info.vbcmdline;
Mayank Grover9df84c02018-08-30 15:46:35 +05302014
2015 /* Free the buffer allocated to vbmeta post verification */
Saranya Chidurab4933332018-10-15 17:30:06 +05302016 if (vbmeta_image_buf != NULL) {
2017 free(vbmeta_image_buf);
2018 --info.num_loaded_images;
2019 }
Monika Singh292b3e92018-03-17 22:40:23 +05302020#else
Channagoud Kadabi736c4962015-08-21 11:56:52 -07002021 /* Change the condition a little bit to include the test framework support.
2022 * We would never reach this point if device is in fastboot mode, even if we did
2023 * that means we are in test mode, so execute kernel authentication part for the
2024 * tests */
Parth Dixitcb0c6082015-12-30 15:01:13 +05302025 if((target_use_signed_kernel() && (!device.is_unlocked)) || is_test_mode_enabled())
Matthew Qin49e51fa2015-02-09 10:40:45 +08002026 {
2027 offset = imagesize_actual;
Vijay Kumar Pendoti9c002ad2016-03-09 13:52:45 +05302028 if (check_aboot_addr_range_overlap((uintptr_t)image_addr + offset, page_size))
Matthew Qin49e51fa2015-02-09 10:40:45 +08002029 {
2030 dprintf(CRITICAL, "Signature read buffer address overlaps with aboot addresses.\n");
2031 return -1;
2032 }
2033
2034 /* Read signature */
2035 if(mmc_read(ptn + offset, (void *)(image_addr + offset), page_size))
2036 {
2037 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
2038 return -1;
2039 }
2040
2041 verify_signed_bootimg((uint32_t)image_addr, imagesize_actual);
Channagoud Kadabi736c4962015-08-21 11:56:52 -07002042 /* The purpose of our test is done here */
Parth Dixitcb0c6082015-12-30 15:01:13 +05302043 if(is_test_mode_enabled() && auth_kernel_img)
Channagoud Kadabi736c4962015-08-21 11:56:52 -07002044 return 0;
Matthew Qin49e51fa2015-02-09 10:40:45 +08002045 } else {
2046 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
2047 #ifdef TZ_SAVE_KERNEL_HASH
2048 aboot_save_boot_hash_mmc((uint32_t) image_addr, imagesize_actual);
2049 #endif /* TZ_SAVE_KERNEL_HASH */
Amit Blay4aa292f2015-04-28 21:55:59 +03002050
2051#ifdef MDTP_SUPPORT
2052 {
2053 /* Verify MDTP lock.
2054 * For boot & recovery partitions, MDTP will use boot_verifier APIs,
2055 * since verification was skipped in aboot. The signature is not part of the loaded image.
2056 */
2057 mdtp_ext_partition_verification_t ext_partition;
2058 ext_partition.partition = boot_into_recovery ? MDTP_PARTITION_RECOVERY : MDTP_PARTITION_BOOT;
2059 ext_partition.integrity_state = MDTP_PARTITION_STATE_UNSET;
2060 ext_partition.page_size = page_size;
2061 ext_partition.image_addr = (uint32)image_addr;
2062 ext_partition.image_size = imagesize_actual;
2063 ext_partition.sig_avail = FALSE;
2064 mdtp_fwlock_verify_lock(&ext_partition);
2065 }
2066#endif /* MDTP_SUPPORT */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08002067 }
Monika Singh292b3e92018-03-17 22:40:23 +05302068#endif
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08002069
Sridhar Parasuram00bfedb2015-05-26 14:21:27 -07002070#if VERIFIED_BOOT
taozhang93088bd2016-11-16 15:50:46 +08002071 if((boot_verify_get_state() == ORANGE) && (!boot_into_ffbm))
Reut Zysmanba8a9d52016-02-18 11:44:04 +02002072 {
2073#if FBCON_DISPLAY_MSG
2074 display_bootverify_menu(DISPLAY_MENU_ORANGE);
2075 wait_for_users_action();
2076#else
2077 dprintf(CRITICAL,
2078 "Your device has been unlocked and can't be trusted.\nWait for 5 seconds before proceeding\n");
2079 mdelay(5000);
2080#endif
2081 }
2082#endif
2083
2084#if VERIFIED_BOOT
Monika Singh292b3e92018-03-17 22:40:23 +05302085 if (VB_M == target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +05302086 {
2087 /* set boot and system versions. */
2088 set_os_version((unsigned char *)image_addr);
2089 // send root of trust
2090 if(!send_rot_command((uint32_t)device.is_unlocked))
2091 ASSERT(0);
2092 }
Parth Dixitbc9b6492015-10-18 00:41:38 +05302093#endif
Channagoud Kadabia22144f2013-03-20 11:49:01 -07002094 /*
Matthew Qinbb7923d2015-02-09 10:56:09 +08002095 * Check if the kernel image is a gzip package. If yes, need to decompress it.
2096 * If not, continue booting.
2097 */
2098 if (is_gzip_package((unsigned char *)(image_addr + page_size), hdr->kernel_size))
2099 {
2100 out_addr = (unsigned char *)(image_addr + imagesize_actual + page_size);
2101 out_avai_len = target_get_max_flash_size() - imagesize_actual - page_size;
Mayank Grover027a9412018-09-04 15:12:05 +05302102#if VERIFIED_BOOT_2
2103 if (dtbo_image_sz)
2104 out_avai_len -= DTBO_IMG_BUF;
2105#endif
Matthew Qin0b15b322015-05-19 05:20:54 -04002106 dprintf(INFO, "decompressing kernel image: start\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08002107 rc = decompress((unsigned char *)(image_addr + page_size),
2108 hdr->kernel_size, out_addr, out_avai_len,
2109 &dtb_offset, &out_len);
2110 if (rc)
2111 {
Matthew Qin0b15b322015-05-19 05:20:54 -04002112 dprintf(CRITICAL, "decompressing kernel image failed!!!\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08002113 ASSERT(0);
2114 }
2115
Matthew Qin0b15b322015-05-19 05:20:54 -04002116 dprintf(INFO, "decompressing kernel image: done\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08002117 kptr = (struct kernel64_hdr *)out_addr;
2118 kernel_start_addr = out_addr;
2119 kernel_size = out_len;
2120 } else {
Ameya Thakur10a33452016-06-13 14:24:26 -07002121 dprintf(INFO, "Uncpmpressed kernel in use\n");
2122 if (!strncmp((char*)(image_addr + page_size),
2123 PATCHED_KERNEL_MAGIC,
2124 sizeof(PATCHED_KERNEL_MAGIC) - 1)) {
2125 dprintf(INFO, "Patched kernel detected\n");
2126 kptr = (struct kernel64_hdr *)(image_addr + page_size +
2127 PATCHED_KERNEL_HEADER_SIZE);
2128 //The size of the kernel is stored at start of kernel image + 16
2129 //The dtb would start just after the kernel
2130 dtb_offset = *((uint32_t*)((unsigned char*)
2131 (image_addr + page_size +
2132 sizeof(PATCHED_KERNEL_MAGIC) -
2133 1)));
2134 //The actual kernel starts after the 20 byte header.
2135 kernel_start_addr = (unsigned char*)(image_addr +
2136 page_size + PATCHED_KERNEL_HEADER_SIZE);
2137 kernel_size = hdr->kernel_size;
2138 patched_kernel_hdr_size = PATCHED_KERNEL_HEADER_SIZE;
2139 } else {
2140 dprintf(INFO, "Kernel image not patched..Unable to locate dt offset\n");
2141 kptr = (struct kernel64_hdr *)(image_addr + page_size);
2142 kernel_start_addr = (unsigned char *)(image_addr + page_size);
2143 kernel_size = hdr->kernel_size;
2144 }
Matthew Qinbb7923d2015-02-09 10:56:09 +08002145 }
2146
2147 /*
Channagoud Kadabia22144f2013-03-20 11:49:01 -07002148 * Update the kernel/ramdisk/tags address if the boot image header
2149 * has default values, these default values come from mkbootimg when
2150 * the boot image is flashed using fastboot flash:raw
2151 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08002152 update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr));
Channagoud Kadabia22144f2013-03-20 11:49:01 -07002153
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002154 /* Get virtual addresses since the hdr saves physical addresses. */
2155 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
2156 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
2157 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002158
Matthew Qinbb7923d2015-02-09 10:56:09 +08002159 kernel_size = ROUND_TO_PAGE(kernel_size, page_mask);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002160 /* Check if the addresses in the header are valid. */
Matthew Qinbb7923d2015-02-09 10:56:09 +08002161 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_size) ||
Mayank Grovere559cec2017-10-17 15:12:03 +05302162 check_ddr_addr_range_bound(hdr->kernel_addr, kernel_size) ||
2163 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual) ||
2164 check_ddr_addr_range_bound(hdr->ramdisk_addr, ramdisk_actual))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002165 {
Mayank Grovere559cec2017-10-17 15:12:03 +05302166 dprintf(CRITICAL, "kernel/ramdisk addresses are not valid.\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002167 return -1;
2168 }
2169
2170#ifndef DEVICE_TREE
Mayank Grovere559cec2017-10-17 15:12:03 +05302171 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE) ||
2172 check_ddr_addr_range_bound(hdr->tags_addr, MAX_TAGS_SIZE))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002173 {
Mayank Grovere559cec2017-10-17 15:12:03 +05302174 dprintf(CRITICAL, "Tags addresses are not valid.\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002175 return -1;
2176 }
2177#endif
2178
Matthew Qin49e51fa2015-02-09 10:40:45 +08002179 /* Move kernel, ramdisk and device tree to correct address */
Matthew Qinbb7923d2015-02-09 10:56:09 +08002180 memmove((void*) hdr->kernel_addr, kernel_start_addr, kernel_size);
Matthew Qin49e51fa2015-02-09 10:40:45 +08002181 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002182
Matthew Qin49e51fa2015-02-09 10:40:45 +08002183 #if DEVICE_TREE
Parth Dixit4097b622016-03-15 14:42:27 +05302184 if(dt_size) {
Matthew Qin49e51fa2015-02-09 10:40:45 +08002185 dt_table_offset = ((uint32_t)image_addr + page_size + kernel_actual + ramdisk_actual + second_actual);
2186 table = (struct dt_table*) dt_table_offset;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002187
Matthew Qin49e51fa2015-02-09 10:40:45 +08002188 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
2189 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
2190 return -1;
2191 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002192
P.V. Phani Kumar82916762016-03-09 09:20:19 +05302193 /* Its Error if, dt_hdr_size (table->num_entries * dt_entry size + Dev_Tree Header)
2194 goes beyound hdr->dt_size*/
Parth Dixit4097b622016-03-15 14:42:27 +05302195 if (dt_hdr_size > ROUND_TO_PAGE(dt_size,hdr->page_size)) {
P.V. Phani Kumar82916762016-03-09 09:20:19 +05302196 dprintf(CRITICAL, "ERROR: Invalid Device Tree size \n");
2197 return -1;
2198 }
2199
Matthew Qin49e51fa2015-02-09 10:40:45 +08002200 /* Find index of device tree within device tree table */
2201 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
2202 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
2203 return -1;
2204 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002205
P.V. Phani Kumar3c333202016-03-09 11:54:37 +05302206 if(dt_entry.offset > (UINT_MAX - dt_entry.size)) {
2207 dprintf(CRITICAL, "ERROR: Device tree contents are Invalid\n");
2208 return -1;
2209 }
2210
2211 /* Ensure we are not overshooting dt_size with the dt_entry selected */
Parth Dixit4097b622016-03-15 14:42:27 +05302212 if ((dt_entry.offset + dt_entry.size) > dt_size) {
P.V. Phani Kumar3c333202016-03-09 11:54:37 +05302213 dprintf(CRITICAL, "ERROR: Device tree contents are Invalid\n");
2214 return -1;
2215 }
2216
Matthew Qin271927e2015-03-31 22:07:07 -04002217 if (is_gzip_package((unsigned char *)dt_table_offset + dt_entry.offset, dt_entry.size))
2218 {
2219 unsigned int compressed_size = 0;
2220 out_addr += out_len;
2221 out_avai_len -= out_len;
Matthew Qin0b15b322015-05-19 05:20:54 -04002222 dprintf(INFO, "decompressing dtb: start\n");
Matthew Qin271927e2015-03-31 22:07:07 -04002223 rc = decompress((unsigned char *)dt_table_offset + dt_entry.offset,
2224 dt_entry.size, out_addr, out_avai_len,
2225 &compressed_size, &dtb_size);
2226 if (rc)
2227 {
Matthew Qin0b15b322015-05-19 05:20:54 -04002228 dprintf(CRITICAL, "decompressing dtb failed!!!\n");
Matthew Qin271927e2015-03-31 22:07:07 -04002229 ASSERT(0);
2230 }
2231
Matthew Qin0b15b322015-05-19 05:20:54 -04002232 dprintf(INFO, "decompressing dtb: done\n");
Matthew Qin271927e2015-03-31 22:07:07 -04002233 best_match_dt_addr = out_addr;
2234 } else {
2235 best_match_dt_addr = (unsigned char *)dt_table_offset + dt_entry.offset;
2236 dtb_size = dt_entry.size;
2237 }
2238
Matthew Qin49e51fa2015-02-09 10:40:45 +08002239 /* Validate and Read device device tree in the tags_addr */
Mayank Grovere559cec2017-10-17 15:12:03 +05302240 if (check_aboot_addr_range_overlap(hdr->tags_addr, dtb_size) ||
2241 check_ddr_addr_range_bound(hdr->tags_addr, dtb_size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002242 {
Mayank Grovere559cec2017-10-17 15:12:03 +05302243 dprintf(CRITICAL, "Device tree addresses are not valid\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002244 return -1;
2245 }
Shashank Mittal162244e2011-08-08 19:01:25 -07002246
Matthew Qin271927e2015-03-31 22:07:07 -04002247 memmove((void *)hdr->tags_addr, (char *)best_match_dt_addr, dtb_size);
Matthew Qin49e51fa2015-02-09 10:40:45 +08002248 } else {
2249 /* Validate the tags_addr */
Mayank Grovere559cec2017-10-17 15:12:03 +05302250 if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual) ||
2251 check_ddr_addr_range_bound(hdr->tags_addr, kernel_actual))
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07002252 {
Mayank Grovere559cec2017-10-17 15:12:03 +05302253 dprintf(CRITICAL, "Device tree addresses are not valid.\n");
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07002254 return -1;
2255 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08002256 /*
2257 * If appended dev tree is found, update the atags with
2258 * memory address to the DTB appended location on RAM.
2259 * Else update with the atags address in the kernel header
Mayank Grover1ed3e8b2018-09-19 15:16:50 +05302260 *
2261 * Make sure everything from scratch address is read before next step!
2262 * In case of dtbo, this API is going to read dtbo on scratch.
Matthew Qin49e51fa2015-02-09 10:40:45 +08002263 */
2264 void *dtb;
Parth Dixit1375d9e2019-07-08 20:56:13 +05302265 image_buf = (void*)(image_addr + page_size + patched_kernel_hdr_size);
2266
Nagireddy Annem0d60bb62019-08-16 19:16:00 +05302267#ifdef OSVERSION_IN_BOOTIMAGE
Parth Dixit1375d9e2019-07-08 20:56:13 +05302268 if ( hdr->header_version == BOOT_HEADER_VERSION_TWO) {
2269
2270 image_buf = (void*)(image_addr);
2271 dtb_offset = dtb_image_offset;
2272 dtb_image_size = imagesize_actual;
2273 }
Nagireddy Annem0d60bb62019-08-16 19:16:00 +05302274#endif
Parth Dixit1375d9e2019-07-08 20:56:13 +05302275
2276 dtb = dev_tree_appended(image_buf, dtb_image_size, dtb_offset,
Ameya Thakur10a33452016-06-13 14:24:26 -07002277 (void *)hdr->tags_addr);
Matthew Qin49e51fa2015-02-09 10:40:45 +08002278 if (!dtb) {
2279 dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07002280 return -1;
2281 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07002282 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08002283 #endif
Shashank Mittal23b8f422010-04-16 19:27:21 -07002284
Stanimir Varbanov69ec5462013-07-18 18:17:42 +03002285 if (boot_into_recovery && !device.is_unlocked && !device.is_tampered)
2286 target_load_ssd_keystore();
2287
Shashank Mittal23b8f422010-04-16 19:27:21 -07002288unified_boot:
Shashank Mittal23b8f422010-04-16 19:27:21 -07002289
Dima Zavin77e41f32013-03-06 16:10:43 -08002290 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07002291 (const char *)hdr->cmdline, board_machtype(),
Shashank Mittal23b8f422010-04-16 19:27:21 -07002292 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
2293
2294 return 0;
2295}
2296
Dima Zavin214cc642009-01-26 11:16:21 -08002297int boot_linux_from_flash(void)
2298{
Mayank Grovere1ab96c2018-09-04 20:31:31 +05302299 boot_img_hdr *hdr = (void*) buf;
Dima Zavin214cc642009-01-26 11:16:21 -08002300 struct ptentry *ptn;
2301 struct ptable *ptable;
2302 unsigned offset = 0;
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08002303
Shashank Mittalcd98d472011-08-02 14:29:24 -07002304 unsigned char *image_addr = 0;
2305 unsigned kernel_actual;
2306 unsigned ramdisk_actual;
2307 unsigned imagesize_actual;
vijay kumar8f53e362015-11-24 13:38:11 +05302308 unsigned second_actual = 0;
Shashank Mittalcd98d472011-08-02 14:29:24 -07002309
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07002310#if DEVICE_TREE
Monika Singh292b3e92018-03-17 22:40:23 +05302311 struct dt_table *table = NULL;
Joel Kingaa335dc2013-06-03 16:11:08 -07002312 struct dt_entry dt_entry;
vijay kumar8f53e362015-11-24 13:38:11 +05302313 unsigned dt_table_offset;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08002314 uint32_t dt_actual;
Monika Singh292b3e92018-03-17 22:40:23 +05302315 uint32_t dt_hdr_size = 0;
Vivek Kumar07bd0862018-04-10 14:19:23 +05302316 uint32_t dtb_offset = 0;
vijay kumar8f53e362015-11-24 13:38:11 +05302317 unsigned int dtb_size = 0;
2318 unsigned char *best_match_dt_addr = NULL;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07002319#endif
2320
David Ng183a7422009-12-07 14:55:21 -08002321 if (target_is_emmc_boot()) {
Mayank Grovere1ab96c2018-09-04 20:31:31 +05302322 hdr = (boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
David Ng183a7422009-12-07 14:55:21 -08002323 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
2324 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
2325 return -1;
2326 }
2327 goto continue_boot;
2328 }
2329
Dima Zavin214cc642009-01-26 11:16:21 -08002330 ptable = flash_get_ptable();
2331 if (ptable == NULL) {
2332 dprintf(CRITICAL, "ERROR: Partition table not found\n");
2333 return -1;
2334 }
2335
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08002336 if(!boot_into_recovery)
2337 {
2338 ptn = ptable_find(ptable, "boot");
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07002339
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08002340 if (ptn == NULL) {
2341 dprintf(CRITICAL, "ERROR: No boot partition found\n");
2342 return -1;
2343 }
2344 }
2345 else
2346 {
2347 ptn = ptable_find(ptable, "recovery");
2348 if (ptn == NULL) {
2349 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
2350 return -1;
2351 }
Dima Zavin214cc642009-01-26 11:16:21 -08002352 }
2353
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05302354 /* Read boot.img header from flash */
Shashank Mittaldcc2e352009-11-19 19:11:16 -08002355 if (flash_read(ptn, offset, buf, page_size)) {
Dima Zavin214cc642009-01-26 11:16:21 -08002356 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
2357 return -1;
2358 }
Dima Zavin214cc642009-01-26 11:16:21 -08002359
2360 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07002361 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Dima Zavin214cc642009-01-26 11:16:21 -08002362 return -1;
2363 }
2364
Shashank Mittaldcc2e352009-11-19 19:11:16 -08002365 if (hdr->page_size != page_size) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07002366 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 -08002367 return -1;
2368 }
2369
Kishor PK9e91b592017-05-24 12:14:55 +05302370 image_addr = (unsigned char *)target_get_scratch_address();
2371 memcpy(image_addr, (void *)buf, page_size);
vijay kumar287bb542015-09-29 13:01:52 +05302372
Channagoud Kadabia22144f2013-03-20 11:49:01 -07002373 /*
2374 * Update the kernel/ramdisk/tags address if the boot image header
2375 * has default values, these default values come from mkbootimg when
2376 * the boot image is flashed using fastboot flash:raw
2377 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08002378 update_ker_tags_rdisk_addr(hdr, false);
Channagoud Kadabia22144f2013-03-20 11:49:01 -07002379
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002380 /* Get virtual addresses since the hdr saves physical addresses. */
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002381 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
2382 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
2383 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
2384
2385 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
2386 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Mayank Grover032736f2017-07-14 20:34:51 +05302387 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002388
Kishor PK9e91b592017-05-24 12:14:55 +05302389 /* ensure commandline is terminated */
2390 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
2391
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002392 /* Check if the addresses in the header are valid. */
2393 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
Mayank Grovere559cec2017-10-17 15:12:03 +05302394 check_ddr_addr_range_bound(hdr->kernel_addr, kernel_actual) ||
2395 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual) ||
2396 check_ddr_addr_range_bound(hdr->ramdisk_addr, ramdisk_actual))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002397 {
Mayank Grovere559cec2017-10-17 15:12:03 +05302398 dprintf(CRITICAL, "kernel/ramdisk addresses are not valid.\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002399 return -1;
2400 }
2401
2402#ifndef DEVICE_TREE
Kishor PKd8ddcad2017-07-27 13:53:57 +05302403 if (UINT_MAX < ((uint64_t)kernel_actual + (uint64_t)ramdisk_actual+ (uint64_t)second_actual + page_size)) {
Mayank Grover032736f2017-07-14 20:34:51 +05302404 dprintf(CRITICAL, "Integer overflow detected in bootimage header fields\n");
2405 return -1;
2406 }
Kishor PKd8ddcad2017-07-27 13:53:57 +05302407 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + second_actual);
Mayank Grover032736f2017-07-14 20:34:51 +05302408
Mayank Grovere559cec2017-10-17 15:12:03 +05302409 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE) ||
2410 check_ddr_addr_range_bound(hdr->tags_addr, MAX_TAGS_SIZE))
Vijay Kumar Pendotid3ed20e2016-09-20 00:34:46 +05302411 {
Mayank Grovere559cec2017-10-17 15:12:03 +05302412 dprintf(CRITICAL, "Tags addresses are not valid.\n");
Vijay Kumar Pendotid3ed20e2016-09-20 00:34:46 +05302413 return -1;
2414 }
2415#else
2416
2417#ifndef OSVERSION_IN_BOOTIMAGE
2418 dt_size = hdr->dt_size;
2419#endif
Mayank Grover032736f2017-07-14 20:34:51 +05302420 dt_actual = ROUND_TO_PAGE(dt_size, page_mask);
Kishor PKd8ddcad2017-07-27 13:53:57 +05302421 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 +05302422 dprintf(CRITICAL, "Integer overflow detected in bootimage header fields\n");
2423 return -1;
2424 }
2425
Kishor PKd8ddcad2017-07-27 13:53:57 +05302426 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + second_actual + dt_actual);
Mayank Grover032736f2017-07-14 20:34:51 +05302427
Mayank Grovere559cec2017-10-17 15:12:03 +05302428 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_size) ||
2429 check_ddr_addr_range_bound(hdr->tags_addr, dt_size))
Mayank Grover032736f2017-07-14 20:34:51 +05302430 {
Mayank Grovere559cec2017-10-17 15:12:03 +05302431 dprintf(CRITICAL, "Device tree addresses are not valid.\n");
Mayank Grover032736f2017-07-14 20:34:51 +05302432 return -1;
2433 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002434#endif
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002435
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05302436 /* Read full boot.img from flash */
2437 dprintf(INFO, "Loading (%s) image (%d): start\n",
2438 (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual);
2439 bs_set_timestamp(BS_KERNEL_LOAD_START);
2440
2441 if (UINT_MAX - page_size < imagesize_actual)
2442 {
2443 dprintf(CRITICAL,"Integer overflow detected in bootimage header fields %u %s\n", __LINE__,__func__);
2444 return -1;
2445 }
2446
2447 /*Check the availability of RAM before reading boot image + max signature length from flash*/
2448 if (target_get_max_flash_size() < (imagesize_actual + page_size))
2449 {
2450 dprintf(CRITICAL, "bootimage size is greater than DDR can hold\n");
2451 return -1;
2452 }
2453
2454 offset = page_size;
2455 /* Read image without signature and header */
2456 if (flash_read(ptn, offset, (void *)(image_addr + offset), imagesize_actual - page_size))
2457 {
2458 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
2459 return -1;
2460 }
2461
2462 dprintf(INFO, "Loading (%s) image (%d): done\n",
2463 (!boot_into_recovery ? "boot" : "recovery"), imagesize_actual);
2464 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
2465
Shashank Mittalcd98d472011-08-02 14:29:24 -07002466 /* Authenticate Kernel */
Deepa Dinamani23b60d42013-06-24 18:10:52 -07002467 if(target_use_signed_kernel() && (!device.is_unlocked))
Shashank Mittalcd98d472011-08-02 14:29:24 -07002468 {
Shashank Mittalcd98d472011-08-02 14:29:24 -07002469 offset = imagesize_actual;
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05302470
Shashank Mittalcd98d472011-08-02 14:29:24 -07002471 /* Read signature */
2472 if (flash_read(ptn, offset, (void *)(image_addr + offset), page_size))
2473 {
2474 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002475 return -1;
Shashank Mittalcd98d472011-08-02 14:29:24 -07002476 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07002477
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05302478 verify_signed_bootimg((uint32_t)image_addr, imagesize_actual);
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05302479 }
2480 offset = page_size;
2481 if(hdr->second_size != 0) {
2482 if (UINT_MAX - offset < second_actual)
2483 {
2484 dprintf(CRITICAL, "ERROR: Integer overflow in boot image header %s\t%d\n",__func__,__LINE__);
2485 return -1;
vijay kumar8f53e362015-11-24 13:38:11 +05302486 }
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05302487 offset += second_actual;
2488 /* Second image loading not implemented. */
2489 ASSERT(0);
2490 }
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05302491 /* Move kernel and ramdisk to correct address */
2492 memmove((void*) hdr->kernel_addr, (char*) (image_addr + page_size), hdr->kernel_size);
2493 memmove((void*) hdr->ramdisk_addr, (char*) (image_addr + page_size + kernel_actual), hdr->ramdisk_size);
2494
2495#if DEVICE_TREE
2496 if(dt_size != 0) {
2497
2498 dt_table_offset = ((uint32_t)image_addr + page_size + kernel_actual + ramdisk_actual + second_actual);
2499
2500 table = (struct dt_table*) dt_table_offset;
2501
2502 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
2503 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
2504 return -1;
2505 }
2506
2507 /* Its Error if, dt_hdr_size (table->num_entries * dt_entry size + Dev_Tree Header)
2508 goes beyound hdr->dt_size*/
2509 if (dt_hdr_size > ROUND_TO_PAGE(dt_size,hdr->page_size)) {
2510 dprintf(CRITICAL, "ERROR: Invalid Device Tree size \n");
2511 return -1;
2512 }
2513
2514 /* Find index of device tree within device tree table */
2515 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
2516 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
2517 return -1;
2518 }
2519
2520 /* Validate and Read device device tree in the "tags_add */
2521 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size) ||
2522 check_ddr_addr_range_bound(hdr->tags_addr, dt_entry.size))
2523 {
2524 dprintf(CRITICAL, "Device tree addresses are not valid.\n");
2525 return -1;
2526 }
2527
2528 if(dt_entry.offset > (UINT_MAX - dt_entry.size)) {
2529 dprintf(CRITICAL, "ERROR: Device tree contents are Invalid\n");
2530 return -1;
2531 }
2532
2533 /* Ensure we are not overshooting dt_size with the dt_entry selected */
2534 if ((dt_entry.offset + dt_entry.size) > dt_size) {
2535 dprintf(CRITICAL, "ERROR: Device tree contents are Invalid\n");
2536 return -1;
2537 }
2538
2539 best_match_dt_addr = (unsigned char *)table + dt_entry.offset;
2540 dtb_size = dt_entry.size;
2541 memmove((void *)hdr->tags_addr, (char *)best_match_dt_addr, dtb_size);
Vivek Kumar07bd0862018-04-10 14:19:23 +05302542
2543 } else {
2544 /* Validate the tags_addr */
2545 if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual) ||
2546 check_ddr_addr_range_bound(hdr->tags_addr, kernel_actual))
2547 {
2548 dprintf(CRITICAL, "Device tree addresses are not valid.\n");
2549 return -1;
2550 }
2551 /*
2552 * If appended dev tree is found, update the atags with
2553 * memory address to the DTB appended location on RAM.
2554 * Else update with the atags address in the kernel header
Mayank Grover1ed3e8b2018-09-19 15:16:50 +05302555 *
2556 * Make sure everything from scratch address is read before next step!
2557 * In case of dtbo, this API is going to read dtbo on scratch.
Vivek Kumar07bd0862018-04-10 14:19:23 +05302558 */
2559 void *dtb = NULL;
2560 dtb = dev_tree_appended((void*)(image_addr + page_size ),hdr->kernel_size, dtb_offset, (void *)hdr->tags_addr);
2561 if (!dtb) {
2562 dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
2563 return -1;
2564 }
2565 }
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05302566#endif
2567 if(target_use_signed_kernel() && (!device.is_unlocked))
2568 {
Shashank Mittal162244e2011-08-08 19:01:25 -07002569 /* Make sure everything from scratch address is read before next step!*/
Shashank Mittala0032282011-08-26 14:50:11 -07002570 if(device.is_tampered)
Shashank Mittal162244e2011-08-08 19:01:25 -07002571 {
2572 write_device_info_flash(&device);
2573 }
Channagoud Kadabi5c86fe32012-02-16 10:58:48 +05302574#if USE_PCOM_SECBOOT
2575 set_tamper_flag(device.is_tampered);
2576#endif
Shashank Mittalcd98d472011-08-02 14:29:24 -07002577 }
David Ng183a7422009-12-07 14:55:21 -08002578continue_boot:
Dima Zavin214cc642009-01-26 11:16:21 -08002579
Dima Zavin214cc642009-01-26 11:16:21 -08002580 /* TODO: create/pass atags to kernel */
2581
Ajay Dudanie28a6072011-07-01 13:59:46 -07002582 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07002583 (const char *)hdr->cmdline, board_machtype(),
Dima Zavin214cc642009-01-26 11:16:21 -08002584 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
2585
2586 return 0;
2587}
Brian Swetland9c4c0752009-01-25 16:23:50 -08002588
Shashank Mittal162244e2011-08-08 19:01:25 -07002589void write_device_info_mmc(device_info *dev)
2590{
Shashank Mittal162244e2011-08-08 19:01:25 -07002591 unsigned long long ptn = 0;
2592 unsigned long long size;
2593 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002594 uint8_t lun = 0;
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002595 uint32_t ret = 0;
Mayank Groverddd348d2018-01-23 14:07:09 +05302596 uint32_t device_info_sz = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07002597
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002598 if (devinfo_present)
2599 index = partition_get_index("devinfo");
2600 else
2601 index = partition_get_index("aboot");
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07002602
Shashank Mittal162244e2011-08-08 19:01:25 -07002603 ptn = partition_get_offset(index);
2604 if(ptn == 0)
2605 {
2606 return;
2607 }
2608
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002609 lun = partition_get_lun(index);
2610 mmc_set_lun(lun);
2611
Shashank Mittal162244e2011-08-08 19:01:25 -07002612 size = partition_get_size(index);
2613
Mayank Groverddd348d2018-01-23 14:07:09 +05302614 device_info_sz = ROUND_TO_PAGE(sizeof(struct device_info),
2615 mmc_blocksize_mask);
2616 if (device_info_sz == UINT_MAX)
2617 {
2618 dprintf(CRITICAL, "ERROR: Incorrect blocksize of card\n");
2619 return;
2620 }
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07002621
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002622 if (devinfo_present)
Mayank Groverddd348d2018-01-23 14:07:09 +05302623 ret = mmc_write(ptn, device_info_sz, (void *)info_buf);
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002624 else
Mayank Groverddd348d2018-01-23 14:07:09 +05302625 ret = mmc_write((ptn + size - device_info_sz), device_info_sz, (void *)info_buf);
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002626 if (ret)
Shashank Mittal162244e2011-08-08 19:01:25 -07002627 {
2628 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
Sridhar Parasurame1a97922015-07-27 15:31:22 -07002629 ASSERT(0);
Shashank Mittal162244e2011-08-08 19:01:25 -07002630 }
2631}
2632
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002633void read_device_info_mmc(struct device_info *info)
Shashank Mittal162244e2011-08-08 19:01:25 -07002634{
Shashank Mittal162244e2011-08-08 19:01:25 -07002635 unsigned long long ptn = 0;
2636 unsigned long long size;
2637 int index = INVALID_PTN;
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002638 uint32_t ret = 0;
Mayank Groverddd348d2018-01-23 14:07:09 +05302639 uint32_t device_info_sz = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07002640
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002641 if ((index = partition_get_index("devinfo")) < 0)
2642 {
2643 devinfo_present = false;
2644 index = partition_get_index("aboot");
2645 }
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07002646
Shashank Mittal162244e2011-08-08 19:01:25 -07002647 ptn = partition_get_offset(index);
2648 if(ptn == 0)
2649 {
2650 return;
2651 }
2652
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07002653 mmc_set_lun(partition_get_lun(index));
2654
Shashank Mittal162244e2011-08-08 19:01:25 -07002655 size = partition_get_size(index);
2656
Mayank Groverddd348d2018-01-23 14:07:09 +05302657 device_info_sz = ROUND_TO_PAGE(sizeof(struct device_info),
2658 mmc_blocksize_mask);
2659 if (device_info_sz == UINT_MAX)
2660 {
2661 dprintf(CRITICAL, "ERROR: Incorrect blocksize of card\n");
2662 return;
2663 }
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07002664
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002665 if (devinfo_present)
Mayank Groverddd348d2018-01-23 14:07:09 +05302666 ret = mmc_read(ptn, (void *)info_buf, device_info_sz);
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002667 else
Mayank Groverddd348d2018-01-23 14:07:09 +05302668 ret = mmc_read((ptn + size - device_info_sz), (void *)info_buf, device_info_sz);
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002669 if (ret)
Shashank Mittal162244e2011-08-08 19:01:25 -07002670 {
2671 dprintf(CRITICAL, "ERROR: Cannot read device info\n");
Sridhar Parasurame1a97922015-07-27 15:31:22 -07002672 ASSERT(0);
Shashank Mittal162244e2011-08-08 19:01:25 -07002673 }
Shashank Mittal162244e2011-08-08 19:01:25 -07002674}
2675
2676void write_device_info_flash(device_info *dev)
2677{
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002678 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
Shashank Mittal162244e2011-08-08 19:01:25 -07002679 struct ptentry *ptn;
2680 struct ptable *ptable;
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002681 if(info == NULL)
2682 {
2683 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
2684 ASSERT(0);
2685 }
2686 info_buf = info;
Shashank Mittal162244e2011-08-08 19:01:25 -07002687 ptable = flash_get_ptable();
2688 if (ptable == NULL)
2689 {
2690 dprintf(CRITICAL, "ERROR: Partition table not found\n");
2691 return;
2692 }
2693
2694 ptn = ptable_find(ptable, "devinfo");
2695 if (ptn == NULL)
2696 {
Smita Ghosh670c8b82015-05-07 09:30:03 -07002697 dprintf(CRITICAL, "ERROR: No devinfo partition found\n");
Shashank Mittal162244e2011-08-08 19:01:25 -07002698 return;
2699 }
2700
Mayank Groverdedbc892017-10-24 13:41:34 +05302701 memset(info, 0, BOOT_IMG_MAX_PAGE_SIZE);
Shashank Mittal162244e2011-08-08 19:01:25 -07002702 memcpy(info, dev, sizeof(device_info));
2703
2704 if (flash_write(ptn, 0, (void *)info_buf, page_size))
2705 {
2706 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
2707 return;
2708 }
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002709 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07002710}
2711
vijay kumarc65876c2015-04-24 13:29:16 +05302712static int read_allow_oem_unlock(device_info *dev)
2713{
vijay kumarc65876c2015-04-24 13:29:16 +05302714 unsigned offset;
2715 int index;
2716 unsigned long long ptn;
2717 unsigned long long ptn_size;
2718 unsigned blocksize = mmc_get_device_blocksize();
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07002719 STACKBUF_DMA_ALIGN(buf, blocksize);
vijay kumarc65876c2015-04-24 13:29:16 +05302720
vijay kumarca2e6812015-07-08 20:28:25 +05302721 index = partition_get_index(frp_ptns[0]);
vijay kumarc65876c2015-04-24 13:29:16 +05302722 if (index == INVALID_PTN)
2723 {
vijay kumarca2e6812015-07-08 20:28:25 +05302724 index = partition_get_index(frp_ptns[1]);
2725 if (index == INVALID_PTN)
2726 {
2727 dprintf(CRITICAL, "Neither '%s' nor '%s' partition found\n", frp_ptns[0],frp_ptns[1]);
2728 return -1;
2729 }
vijay kumarc65876c2015-04-24 13:29:16 +05302730 }
2731
2732 ptn = partition_get_offset(index);
2733 ptn_size = partition_get_size(index);
2734 offset = ptn_size - blocksize;
2735
Mayank Grover48860402016-11-29 12:34:53 +05302736 /* Set Lun for partition */
2737 mmc_set_lun(partition_get_lun(index));
2738
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07002739 if (mmc_read(ptn + offset, (void *)buf, blocksize))
vijay kumarc65876c2015-04-24 13:29:16 +05302740 {
2741 dprintf(CRITICAL, "Reading MMC failed\n");
2742 return -1;
2743 }
2744
2745 /*is_allow_unlock is a bool value stored at the LSB of last byte*/
2746 is_allow_unlock = buf[blocksize-1] & 0x01;
2747 return 0;
2748}
2749
2750static int write_allow_oem_unlock(bool allow_unlock)
2751{
vijay kumarc65876c2015-04-24 13:29:16 +05302752 unsigned offset;
vijay kumarc65876c2015-04-24 13:29:16 +05302753 int index;
2754 unsigned long long ptn;
2755 unsigned long long ptn_size;
2756 unsigned blocksize = mmc_get_device_blocksize();
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07002757 STACKBUF_DMA_ALIGN(buf, blocksize);
vijay kumarc65876c2015-04-24 13:29:16 +05302758
vijay kumarca2e6812015-07-08 20:28:25 +05302759 index = partition_get_index(frp_ptns[0]);
vijay kumarc65876c2015-04-24 13:29:16 +05302760 if (index == INVALID_PTN)
2761 {
vijay kumarca2e6812015-07-08 20:28:25 +05302762 index = partition_get_index(frp_ptns[1]);
2763 if (index == INVALID_PTN)
2764 {
2765 dprintf(CRITICAL, "Neither '%s' nor '%s' partition found\n", frp_ptns[0],frp_ptns[1]);
2766 return -1;
2767 }
vijay kumarc65876c2015-04-24 13:29:16 +05302768 }
2769
2770 ptn = partition_get_offset(index);
2771 ptn_size = partition_get_size(index);
2772 offset = ptn_size - blocksize;
Mayank Grover48860402016-11-29 12:34:53 +05302773 mmc_set_lun(partition_get_lun(index));
vijay kumarc65876c2015-04-24 13:29:16 +05302774
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07002775 if (mmc_read(ptn + offset, (void *)buf, blocksize))
vijay kumarc65876c2015-04-24 13:29:16 +05302776 {
2777 dprintf(CRITICAL, "Reading MMC failed\n");
2778 return -1;
2779 }
2780
2781 /*is_allow_unlock is a bool value stored at the LSB of last byte*/
2782 buf[blocksize-1] = allow_unlock;
2783 if (mmc_write(ptn + offset, blocksize, buf))
2784 {
2785 dprintf(CRITICAL, "Writing MMC failed\n");
2786 return -1;
2787 }
2788
2789 return 0;
2790}
2791
Shashank Mittal162244e2011-08-08 19:01:25 -07002792void read_device_info_flash(device_info *dev)
2793{
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002794 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
Shashank Mittal162244e2011-08-08 19:01:25 -07002795 struct ptentry *ptn;
2796 struct ptable *ptable;
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002797 if(info == NULL)
2798 {
2799 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
2800 ASSERT(0);
2801 }
2802 info_buf = info;
Shashank Mittal162244e2011-08-08 19:01:25 -07002803 ptable = flash_get_ptable();
2804 if (ptable == NULL)
2805 {
2806 dprintf(CRITICAL, "ERROR: Partition table not found\n");
2807 return;
2808 }
2809
2810 ptn = ptable_find(ptable, "devinfo");
2811 if (ptn == NULL)
2812 {
Smita Ghosh670c8b82015-05-07 09:30:03 -07002813 dprintf(CRITICAL, "ERROR: No devinfo partition found\n");
Shashank Mittal162244e2011-08-08 19:01:25 -07002814 return;
2815 }
2816
2817 if (flash_read(ptn, 0, (void *)info_buf, page_size))
2818 {
2819 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
2820 return;
2821 }
2822
2823 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
2824 {
Shashank Mittal162244e2011-08-08 19:01:25 -07002825 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
2826 info->is_unlocked = 0;
Shashank Mittala0032282011-08-26 14:50:11 -07002827 info->is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07002828 write_device_info_flash(info);
2829 }
2830 memcpy(dev, info, sizeof(device_info));
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002831 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07002832}
2833
2834void write_device_info(device_info *dev)
2835{
2836 if(target_is_emmc_boot())
2837 {
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002838 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
2839 if(info == NULL)
2840 {
2841 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
2842 ASSERT(0);
2843 }
2844 info_buf = info;
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002845 memcpy(info, dev, sizeof(struct device_info));
2846
2847#if USE_RPMB_FOR_DEVINFO
Mayank Grover912eaa62017-10-26 12:08:53 +05302848 if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05302849 is_secure_boot_enable()) {
2850 if((write_device_info_rpmb((void*) info, PAGE_SIZE)) < 0)
2851 ASSERT(0);
Sridhar Parasurame1a97922015-07-27 15:31:22 -07002852 }
Channagoud Kadabic80cb492015-04-28 16:08:28 -07002853 else
2854 write_device_info_mmc(info);
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002855#else
2856 write_device_info_mmc(info);
2857#endif
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002858 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07002859 }
2860 else
2861 {
2862 write_device_info_flash(dev);
2863 }
2864}
2865
Monika Singh94316462018-03-15 18:39:01 +05302866int read_rollback_index(uint32_t loc, uint64_t *roll_back_index)
2867{
2868 if (!devinfo_present) {
2869 dprintf(CRITICAL, "DeviceInfo not initalized \n");
2870 return -EINVAL;
2871 }
2872 if (loc >= ARRAY_SIZE(device.rollback_index)) {
2873 dprintf(CRITICAL, "%s() Loc out of range index: %d, array len: %d\n",
2874 __func__, loc, ARRAY_SIZE(device.rollback_index));
2875 ASSERT(0);
2876 }
2877
2878 *roll_back_index = device.rollback_index[loc];
2879 return 0;
2880}
2881
2882int write_rollback_index(uint32_t loc, uint64_t roll_back_index)
2883{
2884 if (!devinfo_present) {
2885 dprintf(CRITICAL, "DeviceInfo not initalized \n");
2886 return -EINVAL;
2887 }
2888 if (loc >= ARRAY_SIZE(device.rollback_index)) {
2889 dprintf(CRITICAL, "%s() Loc out of range index: %d, array len: %d\n",
2890 __func__, loc, ARRAY_SIZE(device.rollback_index));
2891 ASSERT(0);
2892 }
2893
2894 device.rollback_index[loc] = roll_back_index;
2895 write_device_info(&device);
2896 return 0;
2897}
2898
Monika Singhb0fad822018-03-15 18:50:55 +05302899int store_userkey(uint8_t *user_key, uint32_t user_key_size)
2900{
2901 if (!devinfo_present) {
2902 dprintf(CRITICAL, "DeviceInfo not initalized \n");
2903 return -EINVAL;
2904 }
2905
2906 if (user_key_size > ARRAY_SIZE(device.user_public_key)) {
2907 dprintf(CRITICAL, "StoreUserKey, UserKeySize too large!\n");
2908 return -ENODEV;
2909 }
2910
2911 memcpy(device.user_public_key, user_key, user_key_size);
2912 device.user_public_key_length = user_key_size;
2913 write_device_info(&device);
2914 return 0;
2915}
2916
2917int erase_userkey()
2918{
2919 if (!devinfo_present) {
2920 dprintf(CRITICAL, "DeviceInfo not initalized \n");
2921 return -EINVAL;
2922 }
2923 memset(device.user_public_key, 0, ARRAY_SIZE(device.user_public_key));
2924 device.user_public_key_length = 0;
2925 write_device_info(&device);
2926 return 0;
2927}
2928
2929int get_userkey(uint8_t **user_key, uint32_t *user_key_size)
2930{
2931 if (!devinfo_present) {
2932 dprintf(CRITICAL, "DeviceInfo not initalized \n");
2933 return -EINVAL;
2934 }
2935 *user_key = device.user_public_key;
2936 *user_key_size = device.user_public_key_length;
2937 return 0;
2938}
2939
Shashank Mittal162244e2011-08-08 19:01:25 -07002940void read_device_info(device_info *dev)
2941{
2942 if(target_is_emmc_boot())
2943 {
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002944 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
2945 if(info == NULL)
2946 {
2947 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
2948 ASSERT(0);
2949 }
2950 info_buf = info;
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002951
2952#if USE_RPMB_FOR_DEVINFO
Mayank Grover912eaa62017-10-26 12:08:53 +05302953 if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05302954 is_secure_boot_enable()) {
2955 if((read_device_info_rpmb((void*) info, PAGE_SIZE)) < 0)
2956 ASSERT(0);
Sridhar Parasurame1a97922015-07-27 15:31:22 -07002957 }
Channagoud Kadabic80cb492015-04-28 16:08:28 -07002958 else
2959 read_device_info_mmc(info);
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002960#else
2961 read_device_info_mmc(info);
2962#endif
2963
2964 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
2965 {
2966 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
lijuang511a2b52015-08-14 20:50:51 +08002967 if (is_secure_boot_enable()) {
Channagoud Kadabi05f78ba2015-07-06 11:58:14 -07002968 info->is_unlocked = 0;
Monika Singh292b3e92018-03-17 22:40:23 +05302969#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover912eaa62017-10-26 12:08:53 +05302970 if (VB_M <= target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +05302971 info->is_unlock_critical = 0;
Parth Dixitddbc7352015-10-18 03:13:31 +05302972#endif
lijuang511a2b52015-08-14 20:50:51 +08002973 } else {
Channagoud Kadabi2fda4092015-07-07 13:34:11 -07002974 info->is_unlocked = 1;
Monika Singh292b3e92018-03-17 22:40:23 +05302975#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover912eaa62017-10-26 12:08:53 +05302976 if (VB_M <= target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +05302977 info->is_unlock_critical = 1;
Parth Dixitddbc7352015-10-18 03:13:31 +05302978#endif
lijuang511a2b52015-08-14 20:50:51 +08002979 }
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002980 info->is_tampered = 0;
2981 info->charger_screen_enabled = 0;
Monika Singh292b3e92018-03-17 22:40:23 +05302982#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover912eaa62017-10-26 12:08:53 +05302983 if (VB_M <= target_get_vb_version())
Monika Singh1c71a1c2019-12-03 12:12:48 +05302984 {
Mayank Grover889be1b2017-09-12 20:12:23 +05302985 info->verity_mode = 1; //enforcing by default
Monika Singh1c71a1c2019-12-03 12:12:48 +05302986 info->user_public_key_length = 0;
2987 memset(info->rollback_index, 0, sizeof(info->rollback_index));
2988 memset(info->user_public_key, 0, sizeof(info->user_public_key));
2989 }
Parth Dixitddbc7352015-10-18 03:13:31 +05302990#endif
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002991 write_device_info(info);
2992 }
jessicatseng8e7644c2020-04-22 11:13:20 +08002993
2994 info->charger_screen_enabled = 1;
2995 write_device_info(info);
2996
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002997 memcpy(dev, info, sizeof(device_info));
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002998 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07002999 }
3000 else
3001 {
3002 read_device_info_flash(dev);
3003 }
3004}
3005
3006void reset_device_info()
3007{
3008 dprintf(ALWAYS, "reset_device_info called.");
Shashank Mittala0032282011-08-26 14:50:11 -07003009 device.is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07003010 write_device_info(&device);
3011}
3012
3013void set_device_root()
3014{
3015 dprintf(ALWAYS, "set_device_root called.");
Shashank Mittala0032282011-08-26 14:50:11 -07003016 device.is_tampered = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -07003017 write_device_info(&device);
3018}
3019
lijuang4ece1e72015-08-14 21:02:36 +08003020/* set device unlock value
3021 * Must check FRP before call this function
3022 * Need to wipe data when unlock status changed
3023 * type 0: oem unlock
3024 * type 1: unlock critical
3025 * status 0: unlock as false
3026 * status 1: lock as true
3027 */
3028void set_device_unlock_value(int type, bool status)
lijuang21f12f52015-08-22 16:22:19 +08003029{
lijuang4ece1e72015-08-14 21:02:36 +08003030 if (type == UNLOCK)
3031 device.is_unlocked = status;
Monika Singh292b3e92018-03-17 22:40:23 +05303032#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover912eaa62017-10-26 12:08:53 +05303033 else if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05303034 type == UNLOCK_CRITICAL)
3035 device.is_unlock_critical = status;
Parth Dixitddbc7352015-10-18 03:13:31 +05303036#endif
lijuang4ece1e72015-08-14 21:02:36 +08003037 write_device_info(&device);
3038}
3039
3040static void set_device_unlock(int type, bool status)
3041{
3042 int is_unlocked = -1;
3043 char response[MAX_RSP_SIZE];
3044
3045 /* check device unlock status if it is as expected */
3046 if (type == UNLOCK)
3047 is_unlocked = device.is_unlocked;
Monika Singh292b3e92018-03-17 22:40:23 +05303048#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover912eaa62017-10-26 12:08:53 +05303049 if(VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05303050 type == UNLOCK_CRITICAL)
3051 {
3052 is_unlocked = device.is_unlock_critical;
3053 }
Parth Dixitddbc7352015-10-18 03:13:31 +05303054#endif
lijuang4ece1e72015-08-14 21:02:36 +08003055 if (is_unlocked == status) {
3056 snprintf(response, sizeof(response), "\tDevice already : %s", (status ? "unlocked!" : "locked!"));
3057 fastboot_info(response);
3058 fastboot_okay("");
3059 return;
3060 }
3061
3062 /* status is true, it means to unlock device */
lijuang07c5d6e2018-04-23 18:32:13 +08003063 if (status && !is_allow_unlock) {
3064 fastboot_fail("oem unlock is not allowed");
3065 return;
3066 }
lijuang21f12f52015-08-22 16:22:19 +08003067
ericlin4e397c42020-04-29 14:14:24 +08003068 if(target_build_variant_user()){
lijuang4ece1e72015-08-14 21:02:36 +08003069#if FBCON_DISPLAY_MSG
lijuang07c5d6e2018-04-23 18:32:13 +08003070 display_unlock_menu(type, status);
3071 fastboot_okay("");
3072 return;
lijuang4ece1e72015-08-14 21:02:36 +08003073#else
lijuang07c5d6e2018-04-23 18:32:13 +08003074 if (status && type == UNLOCK) {
3075 fastboot_fail("Need wipe userdata. Do 'fastboot oem unlock-go'");
3076 return;
lijuang21f12f52015-08-22 16:22:19 +08003077 }
lijuang07c5d6e2018-04-23 18:32:13 +08003078#endif
ericlin4e397c42020-04-29 14:14:24 +08003079 }
lijuang4ece1e72015-08-14 21:02:36 +08003080
3081 set_device_unlock_value(type, status);
3082
3083 /* wipe data */
3084 struct recovery_message msg;
Kishor PK60a68212017-05-08 16:55:57 +05303085 memset(&msg, 0, sizeof(msg));
lijuang4ece1e72015-08-14 21:02:36 +08003086 snprintf(msg.recovery, sizeof(msg.recovery), "recovery\n--wipe_data");
3087 write_misc(0, &msg, sizeof(msg));
3088
3089 fastboot_okay("");
ericlin4e397c42020-04-29 14:14:24 +08003090 //reboot_device(RECOVERY_MODE);
lijuang21f12f52015-08-22 16:22:19 +08003091}
3092
lijuang511a2b52015-08-14 20:50:51 +08003093static bool critical_flash_allowed(const char * entry)
3094{
3095 uint32_t i = 0;
3096 if (entry == NULL)
3097 return false;
3098
3099 for (i = 0; i < ARRAY_SIZE(critical_flash_allowed_ptn); i++) {
3100 if(!strcmp(entry, critical_flash_allowed_ptn[i]))
3101 return true;
3102 }
3103 return false;
Matthew Qin271927e2015-03-31 22:07:07 -04003104}
3105
3106#if DEVICE_TREE
Amol Jadicb524072012-08-09 16:40:18 -07003107int copy_dtb(uint8_t *boot_image_start, unsigned int scratch_offset)
3108{
3109 uint32 dt_image_offset = 0;
Amol Jadicb524072012-08-09 16:40:18 -07003110 uint32_t n;
Monika Singh292b3e92018-03-17 22:40:23 +05303111 struct dt_table *table = NULL;
Amol Jadicb524072012-08-09 16:40:18 -07003112 struct dt_entry dt_entry;
Monika Singh292b3e92018-03-17 22:40:23 +05303113 uint32_t dt_hdr_size = 0;
Amol Jadicb524072012-08-09 16:40:18 -07003114 unsigned int compressed_size = 0;
3115 unsigned int dtb_size = 0;
3116 unsigned int out_avai_len = 0;
3117 unsigned char *out_addr = NULL;
3118 unsigned char *best_match_dt_addr = NULL;
3119 int rc;
3120
Mayank Grovere1ab96c2018-09-04 20:31:31 +05303121 boot_img_hdr *hdr = (boot_img_hdr *) (boot_image_start);
Amol Jadicb524072012-08-09 16:40:18 -07003122
Parth Dixit4097b622016-03-15 14:42:27 +05303123#ifndef OSVERSION_IN_BOOTIMAGE
3124 dt_size = hdr->dt_size;
3125#endif
3126
3127 if(dt_size != 0) {
Amol Jadicb524072012-08-09 16:40:18 -07003128 /* add kernel offset */
3129 dt_image_offset += page_size;
3130 n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
3131 dt_image_offset += n;
3132
3133 /* add ramdisk offset */
3134 n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
3135 dt_image_offset += n;
3136
3137 /* add second offset */
3138 if(hdr->second_size != 0) {
3139 n = ROUND_TO_PAGE(hdr->second_size, page_mask);
3140 dt_image_offset += n;
3141 }
3142
3143 /* offset now point to start of dt.img */
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07003144 table = (struct dt_table*)(boot_image_start + dt_image_offset);
Amol Jadicb524072012-08-09 16:40:18 -07003145
Deepa Dinamani19648b42013-09-05 17:05:55 -07003146 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Amol Jadicb524072012-08-09 16:40:18 -07003147 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
3148 return -1;
3149 }
P.V. Phani Kumar82916762016-03-09 09:20:19 +05303150
3151 /* Its Error if, dt_hdr_size (table->num_entries * dt_entry size + Dev_Tree Header)
3152 goes beyound hdr->dt_size*/
Parth Dixit4097b622016-03-15 14:42:27 +05303153 if (dt_hdr_size > ROUND_TO_PAGE(dt_size,hdr->page_size)) {
P.V. Phani Kumar82916762016-03-09 09:20:19 +05303154 dprintf(CRITICAL, "ERROR: Invalid Device Tree size \n");
3155 return -1;
3156 }
3157
Joel Kingaa335dc2013-06-03 16:11:08 -07003158 /* Find index of device tree within device tree table */
3159 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Amol Jadicb524072012-08-09 16:40:18 -07003160 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
3161 return -1;
3162 }
3163
Matthew Qin271927e2015-03-31 22:07:07 -04003164 best_match_dt_addr = (unsigned char *)boot_image_start + dt_image_offset + dt_entry.offset;
3165 if (is_gzip_package(best_match_dt_addr, dt_entry.size))
3166 {
3167 out_addr = (unsigned char *)target_get_scratch_address() + scratch_offset;
3168 out_avai_len = target_get_max_flash_size() - scratch_offset;
Matthew Qin0b15b322015-05-19 05:20:54 -04003169 dprintf(INFO, "decompressing dtb: start\n");
Matthew Qin271927e2015-03-31 22:07:07 -04003170 rc = decompress(best_match_dt_addr,
3171 dt_entry.size, out_addr, out_avai_len,
3172 &compressed_size, &dtb_size);
3173 if (rc)
3174 {
Matthew Qin0b15b322015-05-19 05:20:54 -04003175 dprintf(CRITICAL, "decompressing dtb failed!!!\n");
Matthew Qin271927e2015-03-31 22:07:07 -04003176 ASSERT(0);
3177 }
3178
Matthew Qin0b15b322015-05-19 05:20:54 -04003179 dprintf(INFO, "decompressing dtb: done\n");
Matthew Qin271927e2015-03-31 22:07:07 -04003180 best_match_dt_addr = out_addr;
3181 } else {
3182 dtb_size = dt_entry.size;
3183 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07003184 /* Validate and Read device device tree in the "tags_add */
Mayank Grovere559cec2017-10-17 15:12:03 +05303185 if (check_aboot_addr_range_overlap(hdr->tags_addr, dtb_size) ||
3186 check_ddr_addr_range_bound(hdr->tags_addr, dtb_size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07003187 {
Mayank Grovere559cec2017-10-17 15:12:03 +05303188 dprintf(CRITICAL, "Device tree addresses are not valid.\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07003189 return -1;
3190 }
3191
Amol Jadicb524072012-08-09 16:40:18 -07003192 /* Read device device tree in the "tags_add */
Matthew Qin271927e2015-03-31 22:07:07 -04003193 memmove((void*) hdr->tags_addr, (void *)best_match_dt_addr, dtb_size);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07003194 } else
3195 return -1;
Amol Jadicb524072012-08-09 16:40:18 -07003196
3197 /* Everything looks fine. Return success. */
3198 return 0;
3199}
3200#endif
3201
Brian Swetland9c4c0752009-01-25 16:23:50 -08003202void cmd_boot(const char *arg, void *data, unsigned sz)
3203{
3204 unsigned kernel_actual;
3205 unsigned ramdisk_actual;
Kishor PKd8ddcad2017-07-27 13:53:57 +05303206 unsigned second_actual;
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07003207 uint32_t image_actual;
3208 uint32_t dt_actual = 0;
Mayank Grovere1ab96c2018-09-04 20:31:31 +05303209 boot_img_hdr *hdr = NULL;
Matthew Qinbb7923d2015-02-09 10:56:09 +08003210 struct kernel64_hdr *kptr = NULL;
Brian Swetland9c4c0752009-01-25 16:23:50 -08003211 char *ptr = ((char*) data);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07003212 int ret = 0;
3213 uint8_t dtb_copied = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08003214 unsigned int out_len = 0;
3215 unsigned int out_avai_len = 0;
3216 unsigned char *out_addr = NULL;
3217 uint32_t dtb_offset = 0;
3218 unsigned char *kernel_start_addr = NULL;
3219 unsigned int kernel_size = 0;
Matthew Qin271927e2015-03-31 22:07:07 -04003220 unsigned int scratch_offset = 0;
Saranya Chidurab4933332018-10-15 17:30:06 +05303221#if VERIFIED_BOOT_2
3222 void *dtbo_image_buf = NULL;
3223 uint32_t dtbo_image_sz = 0;
3224 void *vbmeta_image_buf = NULL;
3225 uint32_t vbmeta_image_sz = 0;
3226#endif
Monika Singh292b3e92018-03-17 22:40:23 +05303227#if !VERIFIED_BOOT_2
3228 uint32_t sig_actual = 0;
3229 uint32_t sig_size = 0;
3230#ifdef MDTP_SUPPORT
3231 static bool is_mdtp_activated = 0;
3232#endif /* MDTP_SUPPORT */
3233#endif
Matthew Qinbb7923d2015-02-09 10:56:09 +08003234
lijuang2008ff22016-03-07 17:56:27 +08003235#if FBCON_DISPLAY_MSG
3236 /* Exit keys' detection thread firstly */
3237 exit_menu_keys_detection();
3238#endif
3239
Monika Singh292b3e92018-03-17 22:40:23 +05303240#if VERIFIED_BOOT || VERIFIED_BOOT_2
Channagoud Kadabi6d5375e2015-06-23 17:15:42 -07003241 if(target_build_variant_user() && !device.is_unlocked)
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003242 {
3243 fastboot_fail("unlock device to use this command");
lijuang2008ff22016-03-07 17:56:27 +08003244 goto boot_failed;
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003245 }
3246#endif
3247
Brian Swetland9c4c0752009-01-25 16:23:50 -08003248 if (sz < sizeof(hdr)) {
3249 fastboot_fail("invalid bootimage header");
lijuang2008ff22016-03-07 17:56:27 +08003250 goto boot_failed;
Brian Swetland9c4c0752009-01-25 16:23:50 -08003251 }
3252
Mayank Grovere1ab96c2018-09-04 20:31:31 +05303253 hdr = (boot_img_hdr *)data;
Brian Swetland9c4c0752009-01-25 16:23:50 -08003254
3255 /* ensure commandline is terminated */
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07003256 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -08003257
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07003258 if(target_is_emmc_boot() && hdr->page_size) {
3259 page_size = hdr->page_size;
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07003260 page_mask = page_size - 1;
3261 }
3262
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07003263 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
3264 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Kishor PKd8ddcad2017-07-27 13:53:57 +05303265 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07003266#if DEVICE_TREE
Parth Dixit4097b622016-03-15 14:42:27 +05303267#ifndef OSVERSION_IN_BOOTIMAGE
3268 dt_size = hdr->dt_size;
3269#endif
3270 dt_actual = ROUND_TO_PAGE(dt_size, page_mask);
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07003271#endif
3272
3273 image_actual = ADD_OF(page_size, kernel_actual);
3274 image_actual = ADD_OF(image_actual, ramdisk_actual);
Kishor PKd8ddcad2017-07-27 13:53:57 +05303275 image_actual = ADD_OF(image_actual, second_actual);
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07003276 image_actual = ADD_OF(image_actual, dt_actual);
3277
Kishor PK5134b332017-05-09 17:50:08 +05303278 /* Checking to prevent oob access in read_der_message_length */
3279 if (image_actual > sz) {
3280 fastboot_fail("bootimage header fields are invalid");
3281 goto boot_failed;
3282 }
Saranya Chidurab4933332018-10-15 17:30:06 +05303283
Monika Singh292b3e92018-03-17 22:40:23 +05303284#if VERIFIED_BOOT_2
Mayank Grover1ceaee22019-04-01 17:54:32 +05303285 /* Pass size of boot partition, as imgsize, to avoid
3286 read fewer bytes error */
3287 image_actual = partition_get_size(partition_get_index("boot"));
Saranya Chidurab4933332018-10-15 17:30:06 +05303288
3289 /* load and validate dtbo partition */
3290 load_validate_dtbo_image(&dtbo_image_buf, &dtbo_image_sz);
3291
3292 /* load vbmeta partition */
3293 load_vbmeta_image(&vbmeta_image_buf, &vbmeta_image_sz);
3294
Monika Singh292b3e92018-03-17 22:40:23 +05303295 memset(&info, 0, sizeof(bootinfo));
Saranya Chidurab4933332018-10-15 17:30:06 +05303296
3297 info.images[IMG_BOOT].image_buffer = SUB_SALT_BUFF_OFFSET(data);
3298 info.images[IMG_BOOT].imgsize = image_actual;
3299 info.images[IMG_BOOT].name = "boot";
3300 ++info.num_loaded_images;
3301
3302 /* Pass loaded dtbo image */
3303 if (dtbo_image_buf != NULL) {
3304 info.images[IMG_DTBO].image_buffer = SUB_SALT_BUFF_OFFSET(dtbo_image_buf);
3305 info.images[IMG_DTBO].imgsize = dtbo_image_sz;
3306 info.images[IMG_DTBO].name = "dtbo";
3307 ++info.num_loaded_images;
3308 }
3309
3310 /* Pass loaded vbmeta image */
3311 if (vbmeta_image_buf != NULL) {
3312 info.images[IMG_VBMETA].image_buffer = vbmeta_image_buf;
3313 info.images[IMG_VBMETA].imgsize = vbmeta_image_sz;
3314 info.images[IMG_VBMETA].name = "vbmeta";
3315 ++info.num_loaded_images;
3316 }
3317
Monika Singh292b3e92018-03-17 22:40:23 +05303318 info.multi_slot_boot = partition_multislot_is_supported();
3319 if (load_image_and_auth(&info))
3320 goto boot_failed;
3321 vbcmdline = info.vbcmdline;
Saranya Chidurab4933332018-10-15 17:30:06 +05303322
3323 /* Free the buffer allocated to vbmeta post verification */
3324 if (vbmeta_image_buf != NULL) {
3325 free(vbmeta_image_buf);
3326 --info.num_loaded_images;
3327 }
Monika Singh292b3e92018-03-17 22:40:23 +05303328#else
Kishor PK5134b332017-05-09 17:50:08 +05303329 sig_size = sz - image_actual;
3330
P.V. Phani Kumareecfd822016-03-09 20:09:03 +05303331 if (target_use_signed_kernel() && (!device.is_unlocked)) {
Monika Singh292b3e92018-03-17 22:40:23 +05303332 unsigned chk;
P.V. Phani Kumareecfd822016-03-09 20:09:03 +05303333 /* Calculate the signature length from boot image */
3334 sig_actual = read_der_message_length(
Kishor PK5134b332017-05-09 17:50:08 +05303335 (unsigned char*)(data + image_actual), sig_size);
Monika Singh292b3e92018-03-17 22:40:23 +05303336 chk = ADD_OF(image_actual, sig_actual);
Channagoud Kadabi3d839012014-06-26 14:26:39 -07003337
Monika Singh292b3e92018-03-17 22:40:23 +05303338 if (chk > sz) {
Kishor PK5134b332017-05-09 17:50:08 +05303339 fastboot_fail("bootimage header fields are invalid");
3340 goto boot_failed;
3341 }
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07003342 }
3343
Sridhar Parasuram361e4cd2015-09-24 11:19:19 -07003344 // Initialize boot state before trying to verify boot.img
3345#if VERIFIED_BOOT
Channagoud Kadabi699466e2015-11-03 12:37:42 -08003346 boot_verifier_init();
Mayank Groverb337e932017-01-18 20:00:40 +05303347#endif
Channagoud Kadabibf6ce7d2015-09-17 13:25:35 -07003348 /* Handle overflow if the input image size is greater than
3349 * boot image buffer can hold
3350 */
Monika Singh292b3e92018-03-17 22:40:23 +05303351 if ((target_get_max_flash_size() - page_size) < image_actual)
Channagoud Kadabibf6ce7d2015-09-17 13:25:35 -07003352 {
3353 fastboot_fail("booimage: size is greater than boot image buffer can hold");
lijuang2008ff22016-03-07 17:56:27 +08003354 goto boot_failed;
Channagoud Kadabibf6ce7d2015-09-17 13:25:35 -07003355 }
Sridhar Parasuram361e4cd2015-09-24 11:19:19 -07003356
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07003357 /* Verify the boot image
3358 * device & page_size are initialized in aboot_init
3359 */
Sridhar Parasuram7ae9fbc2015-09-17 09:56:30 -07003360 if (target_use_signed_kernel() && (!device.is_unlocked)) {
Channagoud Kadabi3d839012014-06-26 14:26:39 -07003361 /* Pass size excluding signature size, otherwise we would try to
3362 * access signature beyond its length
3363 */
Monika Singh292b3e92018-03-17 22:40:23 +05303364 verify_signed_bootimg((uint32_t)data, image_actual);
Sridhar Parasuram7ae9fbc2015-09-17 09:56:30 -07003365 }
Amit Blay4aa292f2015-04-28 21:55:59 +03003366#ifdef MDTP_SUPPORT
3367 else
3368 {
Shay Nachmani062aa3f2015-05-17 17:28:44 +03003369 /* fastboot boot is not allowed when MDTP is activated */
Amit Blay4aa292f2015-04-28 21:55:59 +03003370 mdtp_ext_partition_verification_t ext_partition;
Amit Blay8a510302015-08-17 09:20:01 +03003371
3372 if (!is_mdtp_activated) {
3373 ext_partition.partition = MDTP_PARTITION_NONE;
3374 mdtp_fwlock_verify_lock(&ext_partition);
3375 }
Amit Blay4aa292f2015-04-28 21:55:59 +03003376 }
Shay Nachmani062aa3f2015-05-17 17:28:44 +03003377
Amir Kotzer7c768c02016-04-13 09:08:05 +03003378 /* If mdtp state cannot be validate, block fastboot boot*/
3379 if(mdtp_activated(&is_mdtp_activated)){
3380 dprintf(CRITICAL, "mdtp_activated cannot validate state.\n");
3381 dprintf(CRITICAL, "Can not proceed with fastboot boot command.\n");
3382 goto boot_failed;
3383 }
Shay Nachmani062aa3f2015-05-17 17:28:44 +03003384 if(is_mdtp_activated){
3385 dprintf(CRITICAL, "fastboot boot command is not available.\n");
lijuang2008ff22016-03-07 17:56:27 +08003386 goto boot_failed;
Shay Nachmani062aa3f2015-05-17 17:28:44 +03003387 }
Amit Blay4aa292f2015-04-28 21:55:59 +03003388#endif /* MDTP_SUPPORT */
Monika Singh292b3e92018-03-17 22:40:23 +05303389#endif /* VERIFIED_BOOT_2 else */
Amit Blay4aa292f2015-04-28 21:55:59 +03003390
Sridhar Parasuram361e4cd2015-09-24 11:19:19 -07003391#if VERIFIED_BOOT
Monika Singh292b3e92018-03-17 22:40:23 +05303392 if (VB_M == target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +05303393 {
3394 /* set boot and system versions. */
3395 set_os_version((unsigned char *)data);
3396 // send root of trust
3397 if(!send_rot_command((uint32_t)device.is_unlocked))
3398 ASSERT(0);
3399 }
Parth Dixitbc9b6492015-10-18 00:41:38 +05303400#endif
Channagoud Kadabia22144f2013-03-20 11:49:01 -07003401 /*
Matthew Qinbb7923d2015-02-09 10:56:09 +08003402 * Check if the kernel image is a gzip package. If yes, need to decompress it.
3403 * If not, continue booting.
3404 */
3405 if (is_gzip_package((unsigned char *)(data + page_size), hdr->kernel_size))
3406 {
3407 out_addr = (unsigned char *)target_get_scratch_address();
3408 out_addr = (unsigned char *)(out_addr + image_actual + page_size);
3409 out_avai_len = target_get_max_flash_size() - image_actual - page_size;
Saranya Chidurab4933332018-10-15 17:30:06 +05303410#if VERIFIED_BOOT_2
3411 if (dtbo_image_sz)
3412 out_avai_len -= DTBO_IMG_BUF;
3413#endif
Matthew Qin0b15b322015-05-19 05:20:54 -04003414 dprintf(INFO, "decompressing kernel image: start\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08003415 ret = decompress((unsigned char *)(ptr + page_size),
3416 hdr->kernel_size, out_addr, out_avai_len,
3417 &dtb_offset, &out_len);
3418 if (ret)
3419 {
Matthew Qin0b15b322015-05-19 05:20:54 -04003420 dprintf(CRITICAL, "decompressing image failed!!!\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08003421 ASSERT(0);
3422 }
3423
Matthew Qin0b15b322015-05-19 05:20:54 -04003424 dprintf(INFO, "decompressing kernel image: done\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08003425 kptr = (struct kernel64_hdr *)out_addr;
3426 kernel_start_addr = out_addr;
3427 kernel_size = out_len;
3428 } else {
3429 kptr = (struct kernel64_hdr*)((char *)data + page_size);
3430 kernel_start_addr = (unsigned char *)((char *)data + page_size);
3431 kernel_size = hdr->kernel_size;
3432 }
3433
3434 /*
Channagoud Kadabia22144f2013-03-20 11:49:01 -07003435 * Update the kernel/ramdisk/tags address if the boot image header
3436 * has default values, these default values come from mkbootimg when
3437 * the boot image is flashed using fastboot flash:raw
3438 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08003439 update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr));
Dima Zavin3cadfff2013-03-21 14:30:48 -07003440
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07003441 /* Get virtual addresses since the hdr saves physical addresses. */
3442 hdr->kernel_addr = VA(hdr->kernel_addr);
3443 hdr->ramdisk_addr = VA(hdr->ramdisk_addr);
3444 hdr->tags_addr = VA(hdr->tags_addr);
Brian Swetland9c4c0752009-01-25 16:23:50 -08003445
Matthew Qinbb7923d2015-02-09 10:56:09 +08003446 kernel_size = ROUND_TO_PAGE(kernel_size, page_mask);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07003447 /* Check if the addresses in the header are valid. */
Matthew Qinbb7923d2015-02-09 10:56:09 +08003448 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_size) ||
Mayank Grovere559cec2017-10-17 15:12:03 +05303449 check_ddr_addr_range_bound(hdr->kernel_addr, kernel_size) ||
3450 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual) ||
3451 check_ddr_addr_range_bound(hdr->ramdisk_addr, ramdisk_actual))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07003452 {
Mayank Grovere559cec2017-10-17 15:12:03 +05303453 dprintf(CRITICAL, "kernel/ramdisk addresses are not valid.\n");
lijuang2008ff22016-03-07 17:56:27 +08003454 goto boot_failed;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07003455 }
3456
Amol Jadicb524072012-08-09 16:40:18 -07003457#if DEVICE_TREE
Matthew Qin271927e2015-03-31 22:07:07 -04003458 scratch_offset = image_actual + page_size + out_len;
Amol Jadicb524072012-08-09 16:40:18 -07003459 /* find correct dtb and copy it to right location */
Matthew Qin271927e2015-03-31 22:07:07 -04003460 ret = copy_dtb(data, scratch_offset);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07003461
3462 dtb_copied = !ret ? 1 : 0;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07003463#else
Mayank Grovere559cec2017-10-17 15:12:03 +05303464 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE) ||
3465 check_ddr_addr_range_bound(hdr->tags_addr, MAX_TAGS_SIZE))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07003466 {
Mayank Grovere559cec2017-10-17 15:12:03 +05303467 dprintf(CRITICAL, "Tags addresses are not valid.\n");
lijuang2008ff22016-03-07 17:56:27 +08003468 goto boot_failed;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07003469 }
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07003470#endif
3471
3472 /* Load ramdisk & kernel */
3473 memmove((void*) hdr->ramdisk_addr, ptr + page_size + kernel_actual, hdr->ramdisk_size);
Matthew Qinbb7923d2015-02-09 10:56:09 +08003474 memmove((void*) hdr->kernel_addr, (char*) (kernel_start_addr), kernel_size);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07003475
3476#if DEVICE_TREE
Mayank Grovere559cec2017-10-17 15:12:03 +05303477 if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual) ||
3478 check_ddr_addr_range_bound(hdr->tags_addr, kernel_actual))
Matthew Qinbb7923d2015-02-09 10:56:09 +08003479 {
Mayank Grovere559cec2017-10-17 15:12:03 +05303480 dprintf(CRITICAL, "Tags addresses are not valid.\n");
lijuang2008ff22016-03-07 17:56:27 +08003481 goto boot_failed;
Matthew Qinbb7923d2015-02-09 10:56:09 +08003482 }
3483
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07003484 /*
3485 * If dtb is not found look for appended DTB in the kernel.
3486 * If appended dev tree is found, update the atags with
3487 * memory address to the DTB appended location on RAM.
3488 * Else update with the atags address in the kernel header
3489 */
3490 if (!dtb_copied) {
3491 void *dtb;
Matthew Qinbb7923d2015-02-09 10:56:09 +08003492 dtb = dev_tree_appended((void*)(ptr + page_size),
3493 hdr->kernel_size, dtb_offset,
Dima Zavine63e5572013-05-03 12:23:06 -07003494 (void *)hdr->tags_addr);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07003495 if (!dtb) {
3496 fastboot_fail("dtb not found");
lijuang2008ff22016-03-07 17:56:27 +08003497 goto boot_failed;
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07003498 }
Amol Jadicb524072012-08-09 16:40:18 -07003499 }
3500#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08003501
3502 fastboot_okay("");
Amol Jadi7c4316c2013-10-07 14:19:26 -07003503 fastboot_stop();
Brian Swetland9c4c0752009-01-25 16:23:50 -08003504
Dima Zavin77e41f32013-03-06 16:10:43 -08003505 boot_linux((void*) hdr->kernel_addr, (void*) hdr->tags_addr,
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07003506 (const char*) hdr->cmdline, board_machtype(),
3507 (void*) hdr->ramdisk_addr, hdr->ramdisk_size);
lijuang2008ff22016-03-07 17:56:27 +08003508
3509 /* fastboot already stop, it's no need to show fastboot menu */
3510 return;
3511boot_failed:
3512#if FBCON_DISPLAY_MSG
3513 /* revert to fastboot menu if boot failed */
3514 display_fastboot_menu();
3515#endif
3516 return;
Brian Swetland9c4c0752009-01-25 16:23:50 -08003517}
3518
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003519void cmd_erase_nand(const char *arg, void *data, unsigned sz)
Dima Zavin214cc642009-01-26 11:16:21 -08003520{
3521 struct ptentry *ptn;
3522 struct ptable *ptable;
3523
3524 ptable = flash_get_ptable();
3525 if (ptable == NULL) {
3526 fastboot_fail("partition table doesn't exist");
3527 return;
3528 }
3529
3530 ptn = ptable_find(ptable, arg);
3531 if (ptn == NULL) {
3532 fastboot_fail("unknown partition name");
3533 return;
3534 }
3535
Monika Singh292b3e92018-03-17 22:40:23 +05303536 if (!strncmp(arg, "avb_custom_key", strlen("avb_custom_key"))) {
3537 dprintf(INFO, "erasing avb_custom_key\n");
3538 if (erase_userkey()) {
3539 fastboot_fail("Erasing avb_custom_key failed");
3540 } else {
3541 fastboot_okay("");
3542 }
3543 return;
3544 }
3545
Dima Zavin214cc642009-01-26 11:16:21 -08003546 if (flash_erase(ptn)) {
3547 fastboot_fail("failed to erase partition");
3548 return;
3549 }
3550 fastboot_okay("");
3551}
3552
Bikas Gurungd48bd242010-09-04 19:54:32 -07003553
3554void cmd_erase_mmc(const char *arg, void *data, unsigned sz)
3555{
3556 unsigned long long ptn = 0;
Oliver Wangcee448d2013-10-22 18:40:13 +08003557 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07003558 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003559 uint8_t lun = 0;
Vijay Kumar Pendoti0b21f462016-05-02 17:09:18 +05303560 char *footer = NULL;
Rahul Shaharef7a5f8b2021-02-04 16:51:36 +05303561 char EraseResultStr[MAX_RSP_SIZE] = "";
3562 VirtualAbMergeStatus SnapshotMergeStatus;
Bikas Gurungd48bd242010-09-04 19:54:32 -07003563
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003564#if VERIFIED_BOOT
3565 if(!strcmp(arg, KEYSTORE_PTN_NAME))
3566 {
3567 if(!device.is_unlocked)
3568 {
3569 fastboot_fail("unlock device to erase keystore");
3570 return;
3571 }
3572 }
3573#endif
3574
Kinson Chikf1a43512011-07-14 11:28:39 -07003575 index = partition_get_index(arg);
3576 ptn = partition_get_offset(index);
Oliver Wangcee448d2013-10-22 18:40:13 +08003577 size = partition_get_size(index);
Neeti Desaica8c9602011-10-06 11:40:00 -07003578
Rahul Shaharef7a5f8b2021-02-04 16:51:36 +05303579 if (target_virtual_ab_supported()) {
3580 if (CheckVirtualAbCriticalPartition (arg)) {
3581 snprintf(EraseResultStr, MAX_RSP_SIZE,"Erase of %s is not allowed in %s state",
3582 arg, SnapshotMergeState);
3583 fastboot_fail(EraseResultStr);
3584 return;
3585 }
3586 SnapshotMergeStatus = GetSnapshotMergeStatus ();
3587 if (((SnapshotMergeStatus == MERGING) || (SnapshotMergeStatus == SNAPSHOTTED)) &&
3588 !strncmp (arg, "super", strlen ("super"))) {
3589
3590 if(SetSnapshotMergeStatus (CANCELLED))
3591 {
3592 fastboot_fail("Failed to update snapshot state to cancel");
3593 return;
3594 }
3595
3596 //updating fbvar snapshot-merge-state
3597 snprintf(SnapshotMergeState,strlen(VabSnapshotMergeStatus[NONE_MERGE_STATUS]) + 1,
3598 "%s", VabSnapshotMergeStatus[NONE_MERGE_STATUS]);
3599 }
3600 }
3601
Monika Singhc4778b72018-05-16 11:16:42 +05303602 if (!strncmp(arg, "avb_custom_key", strlen("avb_custom_key"))) {
3603 dprintf(INFO, "erasing avb_custom_key\n");
3604 if (erase_userkey()) {
3605 fastboot_fail("Erasing avb_custom_key failed");
3606 } else {
3607 fastboot_okay("");
3608 }
3609 return;
3610 }
3611
Kinson Chikf1a43512011-07-14 11:28:39 -07003612 if(ptn == 0) {
Neeti Desaica8c9602011-10-06 11:40:00 -07003613 fastboot_fail("Partition table doesn't exist\n");
Bikas Gurungd48bd242010-09-04 19:54:32 -07003614 return;
3615 }
Kun Liang2f1601a2013-08-12 16:29:54 +08003616
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003617 lun = partition_get_lun(index);
3618 mmc_set_lun(lun);
3619
Sridhar Parasuramd7957122015-02-27 11:33:40 -08003620 if (platform_boot_dev_isemmc())
3621 {
3622 if (mmc_erase_card(ptn, size)) {
3623 fastboot_fail("failed to erase partition\n");
3624 return;
3625 }
3626 } else {
3627 BUF_DMA_ALIGN(out, DEFAULT_ERASE_SIZE);
3628 size = partition_get_size(index);
3629 if (size > DEFAULT_ERASE_SIZE)
3630 size = DEFAULT_ERASE_SIZE;
Kun Liang2f1601a2013-08-12 16:29:54 +08003631
Sridhar Parasuramd7957122015-02-27 11:33:40 -08003632 /* Simple inefficient version of erase. Just writing
3633 0 in first several blocks */
3634 if (mmc_write(ptn , size, (unsigned int *)out)) {
3635 fastboot_fail("failed to erase partition");
3636 return;
3637 }
Vijay Kumar Pendoti0b21f462016-05-02 17:09:18 +05303638 /*Erase FDE metadata at the userdata footer*/
3639 if(!(strncmp(arg, "userdata", 8)))
3640 {
3641 footer = memalign(CACHE_LINE, FOOTER_SIZE);
3642 memset((void *)footer, 0, FOOTER_SIZE);
3643
3644 size = partition_get_size(index);
3645
3646 if (mmc_write((ptn + size) - FOOTER_SIZE , FOOTER_SIZE, (unsigned int *)footer)) {
3647 fastboot_fail("failed to erase userdata footer");
3648 free(footer);
3649 return;
3650 }
3651 free(footer);
3652 }
Bikas Gurungd48bd242010-09-04 19:54:32 -07003653 }
Monika Singh292b3e92018-03-17 22:40:23 +05303654#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover912eaa62017-10-26 12:08:53 +05303655 if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05303656 !(strncmp(arg, "userdata", 8)) &&
3657 send_delete_keys_to_tz())
Sridhar Parasuram32b30662015-07-10 13:33:22 -07003658 ASSERT(0);
3659#endif
Bikas Gurungd48bd242010-09-04 19:54:32 -07003660 fastboot_okay("");
3661}
3662
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003663void cmd_erase(const char *arg, void *data, unsigned sz)
3664{
Monika Singh292b3e92018-03-17 22:40:23 +05303665#if VERIFIED_BOOT || VERIFIED_BOOT_2
Channagoud Kadabi35297672015-06-13 11:09:49 -07003666 if (target_build_variant_user())
Channagoud Kadabice3cf422015-04-17 16:02:30 -07003667 {
Sridhar Parasuramc32d07d2015-07-12 10:57:48 -07003668 if(!device.is_unlocked)
Channagoud Kadabice3cf422015-04-17 16:02:30 -07003669 {
Channagoud Kadabi35297672015-06-13 11:09:49 -07003670 fastboot_fail("device is locked. Cannot erase");
Channagoud Kadabice3cf422015-04-17 16:02:30 -07003671 return;
3672 }
3673 }
3674#endif
3675
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003676 if(target_is_emmc_boot())
3677 cmd_erase_mmc(arg, data, sz);
3678 else
3679 cmd_erase_nand(arg, data, sz);
3680}
Bikas Gurungd48bd242010-09-04 19:54:32 -07003681
Mayank Grover11ff9692018-01-11 11:54:49 +05303682/* Get the size from partiton name */
3683static void get_partition_size(const char *arg, char *response)
3684{
3685 uint64_t ptn = 0;
3686 uint64_t size;
3687 int index = INVALID_PTN;
3688
3689 index = partition_get_index(arg);
3690
3691 if (index == INVALID_PTN)
3692 {
3693 dprintf(CRITICAL, "Invalid partition index\n");
3694 return;
3695 }
3696
3697 ptn = partition_get_offset(index);
3698
3699 if(!ptn)
3700 {
3701 dprintf(CRITICAL, "Invalid partition name %s\n", arg);
3702 return;
3703 }
3704
3705 size = partition_get_size(index);
3706
3707 snprintf(response, MAX_RSP_SIZE, "\t 0x%llx", size);
3708 return;
3709}
3710
Mayank Grover52cd10a2018-03-15 12:57:54 +05303711/* Function to check partition type of a partition*/
3712static fs_signature_type
3713check_partition_fs_signature(const char *arg)
3714{
3715 fs_signature_type ret = NO_FS;
3716 int index;
3717 unsigned long long ptn;
Mayank Grover399826a2018-08-27 12:15:15 +05303718 char *buffer = memalign(CACHE_LINE, mmc_blocksize);
3719 uint32_t sb_blk_offset = 0;
3720 char *sb_buffer = buffer;
3721
Mayank Grover52cd10a2018-03-15 12:57:54 +05303722 if (!sb_buffer)
3723 {
3724 dprintf(CRITICAL, "ERROR: Failed to allocate buffer for superblock\n");
3725 goto out;
3726 }
3727
3728 /* Read super block */
3729 if ((index = partition_get_index(arg)) < 0)
3730 {
3731 dprintf(CRITICAL, "ERROR: %s() doesn't exsit\n", arg);
3732 goto out;
3733 }
3734 ptn = partition_get_offset(index);
3735 mmc_set_lun(partition_get_lun(index));
Mayank Grover399826a2018-08-27 12:15:15 +05303736 sb_blk_offset = (FS_SUPERBLOCK_OFFSET/mmc_blocksize);
3737
3738 if(mmc_read(ptn + (sb_blk_offset * mmc_blocksize),
Mayank Grover52cd10a2018-03-15 12:57:54 +05303739 (void *)sb_buffer, mmc_blocksize))
3740 {
3741 dprintf(CRITICAL, "ERROR: Failed to read Superblock\n");
3742 goto out;
3743 }
3744
Mayank Grover399826a2018-08-27 12:15:15 +05303745 if (sb_blk_offset == 0)
3746 sb_buffer += FS_SUPERBLOCK_OFFSET;
3747
3748 if (*((uint16 *)(&sb_buffer[EXT_MAGIC_OFFSET_SB])) == (uint16)EXT_MAGIC)
Mayank Grover52cd10a2018-03-15 12:57:54 +05303749 {
3750 dprintf(SPEW, "%s() Found EXT FS\n", arg);
3751 ret = EXT_FS_SIGNATURE;
3752 }
Mayank Grover399826a2018-08-27 12:15:15 +05303753 else if (*((uint32 *)(&sb_buffer[F2FS_MAGIC_OFFSET_SB])) == F2FS_MAGIC)
Mayank Grover52cd10a2018-03-15 12:57:54 +05303754 {
3755 dprintf(SPEW, "%s() Found F2FS FS\n", arg);
3756 ret = EXT_F2FS_SIGNATURE;
3757 }
3758 else
3759 {
3760 dprintf(SPEW, "%s() Reverting to default 0x%x\n",
3761 arg, *((uint16 *)(&sb_buffer[EXT_MAGIC_OFFSET_SB])));
3762 ret = NO_FS;
3763 }
3764
3765out:
Mayank Grover399826a2018-08-27 12:15:15 +05303766 if(buffer)
3767 free(buffer);
Mayank Grover52cd10a2018-03-15 12:57:54 +05303768 return ret;
3769}
3770
Mayank Groverd38fe012018-03-13 15:33:16 +05303771/* Function to get partition type */
3772static void get_partition_type(const char *arg, char *response)
3773{
3774 uint n = 0;
Mayank Grover52cd10a2018-03-15 12:57:54 +05303775 fs_signature_type fs_signature;
Mayank Groverd38fe012018-03-13 15:33:16 +05303776
3777 if (arg == NULL ||
3778 response == NULL)
3779 {
3780 dprintf(CRITICAL, "Invalid input parameter\n");
3781 return;
3782 }
3783
3784 /* By default copy raw to response */
Mayank Grover0e559042018-04-11 17:49:30 +05303785 strlcpy(response, RAW_STR, MAX_RSP_SIZE);
Mayank Groverd38fe012018-03-13 15:33:16 +05303786
3787 /* Mark partiton type for known paritions only */
3788 for (n=0; n < ARRAY_SIZE(part_type_known); n++)
3789 {
3790 if (!strncmp(part_type_known[n].part_name, arg, strlen(arg)))
3791 {
Mayank Grover52cd10a2018-03-15 12:57:54 +05303792 /* Check partition for FS signature */
3793 fs_signature = check_partition_fs_signature(arg);
3794 switch (fs_signature)
3795 {
3796 case EXT_FS_SIGNATURE:
Mayank Grover0e559042018-04-11 17:49:30 +05303797 strlcpy(response, EXT_STR, MAX_RSP_SIZE);
Mayank Grover52cd10a2018-03-15 12:57:54 +05303798 break;
3799 case EXT_F2FS_SIGNATURE:
Mayank Grover0e559042018-04-11 17:49:30 +05303800 strlcpy(response, F2FS_STR, MAX_RSP_SIZE);
Mayank Grover52cd10a2018-03-15 12:57:54 +05303801 break;
3802 case NO_FS:
Mayank Grover0e559042018-04-11 17:49:30 +05303803 strlcpy(response, part_type_known[n].type_response, MAX_RSP_SIZE);
Mayank Grover52cd10a2018-03-15 12:57:54 +05303804 }
Mayank Groverd38fe012018-03-13 15:33:16 +05303805 }
3806 }
3807 return;
3808}
3809
Mayank Grover11ff9692018-01-11 11:54:49 +05303810/*
3811 * Publish the partition type & size info
3812 * fastboot getvar will publish the required information.
3813 * fastboot getvar partition_size:<partition_name>: partition size in hex
3814 * fastboot getvar partition_type:<partition_name>: partition type (ext/fat)
3815 */
3816static void publish_getvar_partition_info(struct getvar_partition_info *info, uint8_t num_parts)
3817{
Mayank Groverd38fe012018-03-13 15:33:16 +05303818 uint8_t i;
Mayank Grover11ff9692018-01-11 11:54:49 +05303819 static bool published = false;
3820 struct partition_entry *ptn_entry =
3821 partition_get_partition_entries();
3822 memset(info, 0, sizeof(struct getvar_partition_info)* num_parts);
3823
3824 for (i = 0; i < num_parts; i++) {
3825 strlcat(info[i].part_name, (const char *)ptn_entry[i].name, MAX_RSP_SIZE);
3826 strlcat(info[i].getvar_size, "partition-size:", MAX_GET_VAR_NAME_SIZE);
3827 strlcat(info[i].getvar_type, "partition-type:", MAX_GET_VAR_NAME_SIZE);
3828
Mayank Groverd38fe012018-03-13 15:33:16 +05303829 get_partition_type(info[i].part_name, info[i].type_response);
Mayank Grover11ff9692018-01-11 11:54:49 +05303830 get_partition_size(info[i].part_name, info[i].size_response);
Mayank Grover11ff9692018-01-11 11:54:49 +05303831 if (strlcat(info[i].getvar_size, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
3832 {
3833 dprintf(CRITICAL, "partition size name truncated\n");
3834 return;
3835 }
3836 if (strlcat(info[i].getvar_type, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
3837 {
3838 dprintf(CRITICAL, "partition type name truncated\n");
3839 return;
3840 }
3841
3842 if (!published)
3843 {
3844 /* publish partition size & type info */
3845 fastboot_publish((const char *) info[i].getvar_size, (const char *) info[i].size_response);
3846 fastboot_publish((const char *) info[i].getvar_type, (const char *) info[i].type_response);
3847 }
3848 }
3849 if (!published)
3850 published = true;
3851}
3852
3853
Ajay Dudani5c761132011-04-07 20:19:04 -07003854void cmd_flash_mmc_img(const char *arg, void *data, unsigned sz)
Shashank Mittal23b8f422010-04-16 19:27:21 -07003855{
3856 unsigned long long ptn = 0;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07003857 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07003858 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003859 char *token = NULL;
3860 char *pname = NULL;
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07003861 char *sp;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003862 uint8_t lun = 0;
3863 bool lun_set = false;
Mayank Grover351a75e2017-05-30 20:06:08 +05303864 int current_active_slot = INVALID;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07003865
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07003866 token = strtok_r((char *)arg, ":", &sp);
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003867 pname = token;
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07003868 token = strtok_r(NULL, ":", &sp);
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003869 if(token)
3870 {
3871 lun = atoi(token);
3872 mmc_set_lun(lun);
3873 lun_set = true;
3874 }
3875
Mao Jinlong226f33a2014-07-04 17:24:10 +08003876 if (pname)
Greg Grisco6e754772011-06-23 12:19:39 -07003877 {
Channagoud Kadabiad259832015-05-29 11:14:17 -07003878 if (!strncmp(pname, "frp-unlock", strlen("frp-unlock")))
3879 {
3880 if (!aboot_frp_unlock(pname, data, sz))
3881 {
3882 fastboot_info("FRP unlock successful");
3883 fastboot_okay("");
3884 }
3885 else
3886 fastboot_fail("Secret key is invalid, please update the bootloader with secret key");
3887
3888 return;
3889 }
3890
Mao Jinlong226f33a2014-07-04 17:24:10 +08003891 if (!strcmp(pname, "partition"))
3892 {
3893 dprintf(INFO, "Attempt to write partition image.\n");
3894 if (write_partition(sz, (unsigned char *) data)) {
3895 fastboot_fail("failed to write partition");
Greg Grisco6e754772011-06-23 12:19:39 -07003896 return;
3897 }
Mayank Grover11ff9692018-01-11 11:54:49 +05303898 /* Re-publish partition table */
3899 publish_getvar_partition_info(part_info, partition_get_partition_count());
Mayank Grover351a75e2017-05-30 20:06:08 +05303900
3901 /* Rescan partition table to ensure we have multislot support*/
3902 if (partition_scan_for_multislot())
3903 {
3904 current_active_slot = partition_find_active_slot();
3905 dprintf(INFO, "Multislot supported: Slot %s active",
3906 (SUFFIX_SLOT(current_active_slot)));
3907 }
3908 partition_mark_active_slot(current_active_slot);
Greg Grisco6e754772011-06-23 12:19:39 -07003909 }
Mao Jinlong226f33a2014-07-04 17:24:10 +08003910 else
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003911 {
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003912#if VERIFIED_BOOT
3913 if(!strcmp(pname, KEYSTORE_PTN_NAME))
3914 {
3915 if(!device.is_unlocked)
3916 {
3917 fastboot_fail("unlock device to flash keystore");
3918 return;
3919 }
Gaurav Nebhwanic7313cc2015-12-15 22:25:04 +05303920 if(!boot_verify_validate_keystore((unsigned char *)data,sz))
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003921 {
3922 fastboot_fail("image is not a keystore file");
3923 return;
3924 }
3925 }
3926#endif
Mao Jinlong226f33a2014-07-04 17:24:10 +08003927 index = partition_get_index(pname);
3928 ptn = partition_get_offset(index);
3929 if(ptn == 0) {
3930 fastboot_fail("partition table doesn't exist");
3931 return;
3932 }
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003933
Mayank Grover351a75e2017-05-30 20:06:08 +05303934 if (!strncmp(pname, "boot", strlen("boot"))
3935 || !strcmp(pname, "recovery"))
3936 {
Ashish Bhimanpalliwar84f7db92020-09-23 11:59:16 +05303937 if ((sz < BOOT_MAGIC_SIZE) || memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Mao Jinlong226f33a2014-07-04 17:24:10 +08003938 fastboot_fail("image is not a boot image");
3939 return;
3940 }
Mayank Grover351a75e2017-05-30 20:06:08 +05303941
3942 /* Reset multislot_partition attributes in case of flashing boot */
3943 if (partition_multislot_is_supported())
3944 {
3945 partition_reset_attributes(index);
3946 }
Mao Jinlong226f33a2014-07-04 17:24:10 +08003947 }
3948
3949 if(!lun_set)
3950 {
3951 lun = partition_get_lun(index);
3952 mmc_set_lun(lun);
3953 }
3954
3955 size = partition_get_size(index);
Gaurav Nebhwani4d2389c2016-03-17 21:10:05 +05303956 if (ROUND_TO_PAGE(sz, mmc_blocksize_mask) > size) {
Mao Jinlong226f33a2014-07-04 17:24:10 +08003957 fastboot_fail("size too large");
3958 return;
3959 }
3960 else if (mmc_write(ptn , sz, (unsigned int *)data)) {
3961 fastboot_fail("flash write failure");
3962 return;
3963 }
Greg Grisco6e754772011-06-23 12:19:39 -07003964 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07003965 }
3966 fastboot_okay("");
3967 return;
3968}
3969
Ajay Dudanide984792015-03-02 09:57:41 -08003970void cmd_flash_meta_img(const char *arg, void *data, unsigned sz)
3971{
3972 int i, images;
3973 meta_header_t *meta_header;
3974 img_header_entry_t *img_header_entry;
Parth Dixit3e2d3032016-03-04 17:11:52 +05303975 /*End of the image address*/
3976 uintptr_t data_end;
3977
3978 if( (UINT_MAX - sz) > (uintptr_t)data )
3979 data_end = (uintptr_t)data + sz;
3980 else
3981 {
3982 fastboot_fail("Cannot flash: image header corrupt");
3983 return;
3984 }
3985
Ashish Bhimanpalliwar21494e12020-10-07 13:24:03 +05303986 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 +05303987 {
3988 fastboot_fail("Cannot flash: image header corrupt");
3989 return;
3990 }
Ajay Dudanide984792015-03-02 09:57:41 -08003991
lijuang8ee21882016-04-19 16:57:11 +08003992 /* If device is locked:
3993 * Forbid to flash image to avoid the device to bypass the image
3994 * which with "any" name other than bootloader. Because it maybe
3995 * a meta package of all partitions.
3996 */
Monika Singh292b3e92018-03-17 22:40:23 +05303997#if VERIFIED_BOOT || VERIFIED_BOOT_2
lijuang8ee21882016-04-19 16:57:11 +08003998 if (target_build_variant_user()) {
3999 if (!device.is_unlocked) {
4000 fastboot_fail("Device is locked, meta image flashing is not allowed");
4001 return;
4002 }
Mayank Grover889be1b2017-09-12 20:12:23 +05304003
Mayank Grover912eaa62017-10-26 12:08:53 +05304004 if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05304005 !device.is_unlock_critical)
4006 {
lijuang8ee21882016-04-19 16:57:11 +08004007 fastboot_fail("Device is critical locked, Meta image flashing is not allowed");
4008 return;
4009 }
lijuang8ee21882016-04-19 16:57:11 +08004010 }
4011#endif
4012
Ajay Dudanide984792015-03-02 09:57:41 -08004013 meta_header = (meta_header_t*) data;
Ashish Bhimanpalliwar21494e12020-10-07 13:24:03 +05304014 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 +05304015 {
4016 fastboot_fail("Cannot flash: image header corrupt");
4017 return;
4018 }
Ajay Dudanide984792015-03-02 09:57:41 -08004019 img_header_entry = (img_header_entry_t*) (data+sizeof(meta_header_t));
4020
4021 images = meta_header->img_hdr_sz / sizeof(img_header_entry_t);
4022
4023 for (i=0; i<images; i++) {
4024
4025 if((img_header_entry[i].ptn_name == NULL) ||
4026 (img_header_entry[i].start_offset == 0) ||
4027 (img_header_entry[i].size == 0))
4028 break;
Kishor PK49831502017-04-21 17:55:43 +05304029 if ((UINT_MAX - img_header_entry[i].start_offset) < (uintptr_t)data) {
4030 fastboot_fail("Integer overflow detected in start_offset of img");
4031 break;
4032 }
4033 else if ((UINT_MAX - (img_header_entry[i].start_offset + (uintptr_t)data)) < img_header_entry[i].size) {
4034 fastboot_fail("Integer overflow detected in size of img");
4035 break;
4036 }
Parth Dixit3e2d3032016-03-04 17:11:52 +05304037 if( data_end < ((uintptr_t)data + img_header_entry[i].start_offset
4038 + img_header_entry[i].size) )
4039 {
4040 fastboot_fail("Cannot flash: image size mismatch");
4041 break;
4042 }
4043
Ajay Dudanide984792015-03-02 09:57:41 -08004044 cmd_flash_mmc_img(img_header_entry[i].ptn_name,
4045 (void *) data + img_header_entry[i].start_offset,
4046 img_header_entry[i].size);
4047 }
4048
Channagoud Kadabiaafbbfc2015-03-04 17:52:56 -08004049 if (!strncmp(arg, "bootloader", strlen("bootloader")))
4050 {
4051 strlcpy(device.bootloader_version, TARGET(BOARD), MAX_VERSION_LEN);
4052 strlcat(device.bootloader_version, "-", MAX_VERSION_LEN);
4053 strlcat(device.bootloader_version, meta_header->img_version, MAX_VERSION_LEN);
4054 }
4055 else
4056 {
4057 strlcpy(device.radio_version, TARGET(BOARD), MAX_VERSION_LEN);
4058 strlcat(device.radio_version, "-", MAX_VERSION_LEN);
4059 strlcat(device.radio_version, meta_header->img_version, MAX_VERSION_LEN);
4060 }
4061
4062 write_device_info(&device);
Ajay Dudanide984792015-03-02 09:57:41 -08004063 fastboot_okay("");
4064 return;
4065}
4066
Ajay Dudani5c761132011-04-07 20:19:04 -07004067void cmd_flash_mmc_sparse_img(const char *arg, void *data, unsigned sz)
4068{
4069 unsigned int chunk;
wufeng.jiang813dc352015-06-02 23:02:46 -04004070 uint64_t chunk_data_sz;
Channagoud Kadabif627cf82013-09-09 14:08:20 -07004071 uint32_t *fill_buf = NULL;
4072 uint32_t fill_val;
Mayank Grover4f50bba2017-07-19 18:17:08 +05304073 uint32_t blk_sz_actual = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07004074 sparse_header_t *sparse_header;
4075 chunk_header_t *chunk_header;
Ajay Dudaniab18f022011-05-12 14:39:22 -07004076 uint32_t total_blocks = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07004077 unsigned long long ptn = 0;
Channagoud Kadabi65b91002011-10-11 17:34:33 +05304078 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07004079 int index = INVALID_PTN;
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05304080 uint32_t i;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08004081 uint8_t lun = 0;
vijay kumar800255e2015-04-24 20:26:19 +05304082 /*End of the sparse image address*/
Parth Dixit64b1a482016-03-07 16:31:26 +05304083 uintptr_t data_end = (uintptr_t)data + sz;
Ajay Dudani5c761132011-04-07 20:19:04 -07004084
Kinson Chikf1a43512011-07-14 11:28:39 -07004085 index = partition_get_index(arg);
4086 ptn = partition_get_offset(index);
4087 if(ptn == 0) {
Ajay Dudani5c761132011-04-07 20:19:04 -07004088 fastboot_fail("partition table doesn't exist");
4089 return;
4090 }
4091
Channagoud Kadabi65b91002011-10-11 17:34:33 +05304092 size = partition_get_size(index);
Channagoud Kadabi65b91002011-10-11 17:34:33 +05304093
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08004094 lun = partition_get_lun(index);
4095 mmc_set_lun(lun);
4096
vijay kumar800255e2015-04-24 20:26:19 +05304097 if (sz < sizeof(sparse_header_t)) {
4098 fastboot_fail("size too low");
4099 return;
4100 }
4101
Ajay Dudani5c761132011-04-07 20:19:04 -07004102 /* Read and skip over sparse image header */
4103 sparse_header = (sparse_header_t *) data;
vijay kumar800255e2015-04-24 20:26:19 +05304104
Mayank Grover48bd9bb2017-07-19 12:04:16 +05304105 if (!sparse_header->blk_sz || (sparse_header->blk_sz % 4)){
4106 fastboot_fail("Invalid block size\n");
4107 return;
4108 }
4109
vijay kumar1321f342015-03-27 12:13:42 +05304110 if (((uint64_t)sparse_header->total_blks * (uint64_t)sparse_header->blk_sz) > size) {
Ajay Dudani876b3282012-12-21 14:12:17 -08004111 fastboot_fail("size too large");
4112 return;
4113 }
4114
vijay kumarde4fcf62015-04-23 13:05:49 +05304115 data += sizeof(sparse_header_t);
vijay kumar800255e2015-04-24 20:26:19 +05304116
Parth Dixit64b1a482016-03-07 16:31:26 +05304117 if (data_end < (uintptr_t)data) {
vijay kumar800255e2015-04-24 20:26:19 +05304118 fastboot_fail("buffer overreads occured due to invalid sparse header");
4119 return;
4120 }
4121
vijay kumarde4fcf62015-04-23 13:05:49 +05304122 if(sparse_header->file_hdr_sz != sizeof(sparse_header_t))
Ajay Dudani5c761132011-04-07 20:19:04 -07004123 {
vijay kumarde4fcf62015-04-23 13:05:49 +05304124 fastboot_fail("sparse header size mismatch");
4125 return;
Ajay Dudani5c761132011-04-07 20:19:04 -07004126 }
4127
Ajay Dudanib06c05f2011-05-12 14:46:10 -07004128 dprintf (SPEW, "=== Sparse Image Header ===\n");
4129 dprintf (SPEW, "magic: 0x%x\n", sparse_header->magic);
4130 dprintf (SPEW, "major_version: 0x%x\n", sparse_header->major_version);
4131 dprintf (SPEW, "minor_version: 0x%x\n", sparse_header->minor_version);
4132 dprintf (SPEW, "file_hdr_sz: %d\n", sparse_header->file_hdr_sz);
4133 dprintf (SPEW, "chunk_hdr_sz: %d\n", sparse_header->chunk_hdr_sz);
4134 dprintf (SPEW, "blk_sz: %d\n", sparse_header->blk_sz);
4135 dprintf (SPEW, "total_blks: %d\n", sparse_header->total_blks);
4136 dprintf (SPEW, "total_chunks: %d\n", sparse_header->total_chunks);
Ajay Dudani5c761132011-04-07 20:19:04 -07004137
4138 /* Start processing chunks */
4139 for (chunk=0; chunk<sparse_header->total_chunks; chunk++)
4140 {
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05304141 /* Make sure the total image size does not exceed the partition size */
4142 if(((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz) >= size) {
4143 fastboot_fail("size too large");
4144 return;
4145 }
Ajay Dudani5c761132011-04-07 20:19:04 -07004146 /* Read and skip over chunk header */
4147 chunk_header = (chunk_header_t *) data;
4148 data += sizeof(chunk_header_t);
4149
Parth Dixit64b1a482016-03-07 16:31:26 +05304150 if (data_end < (uintptr_t)data) {
vijay kumar800255e2015-04-24 20:26:19 +05304151 fastboot_fail("buffer overreads occured due to invalid sparse header");
4152 return;
4153 }
4154
Ajay Dudani5c761132011-04-07 20:19:04 -07004155 dprintf (SPEW, "=== Chunk Header ===\n");
4156 dprintf (SPEW, "chunk_type: 0x%x\n", chunk_header->chunk_type);
4157 dprintf (SPEW, "chunk_data_sz: 0x%x\n", chunk_header->chunk_sz);
4158 dprintf (SPEW, "total_size: 0x%x\n", chunk_header->total_sz);
4159
vijay kumar800255e2015-04-24 20:26:19 +05304160 if(sparse_header->chunk_hdr_sz != sizeof(chunk_header_t))
Ajay Dudani5c761132011-04-07 20:19:04 -07004161 {
vijay kumar800255e2015-04-24 20:26:19 +05304162 fastboot_fail("chunk header size mismatch");
4163 return;
Ajay Dudani5c761132011-04-07 20:19:04 -07004164 }
4165
wufeng.jiang813dc352015-06-02 23:02:46 -04004166 chunk_data_sz = (uint64_t)sparse_header->blk_sz * chunk_header->chunk_sz;
4167
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05304168 /* Make sure that the chunk size calculated from sparse image does not
4169 * exceed partition size
4170 */
4171 if ((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz + chunk_data_sz > size)
4172 {
4173 fastboot_fail("Chunk data size exceeds partition size");
4174 return;
4175 }
4176
Ajay Dudani5c761132011-04-07 20:19:04 -07004177 switch (chunk_header->chunk_type)
4178 {
4179 case CHUNK_TYPE_RAW:
wufeng.jiang813dc352015-06-02 23:02:46 -04004180 if((uint64_t)chunk_header->total_sz != ((uint64_t)sparse_header->chunk_hdr_sz +
Ajay Dudani5c761132011-04-07 20:19:04 -07004181 chunk_data_sz))
4182 {
4183 fastboot_fail("Bogus chunk size for chunk type Raw");
4184 return;
4185 }
4186
Parth Dixit64b1a482016-03-07 16:31:26 +05304187 if (data_end < (uintptr_t)data + chunk_data_sz) {
vijay kumar800255e2015-04-24 20:26:19 +05304188 fastboot_fail("buffer overreads occured due to invalid sparse header");
4189 return;
4190 }
4191
wufeng.jiang813dc352015-06-02 23:02:46 -04004192 /* chunk_header->total_sz is uint32,So chunk_data_sz is now less than 2^32
4193 otherwise it will return in the line above
4194 */
Ajay Dudaniab18f022011-05-12 14:39:22 -07004195 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
wufeng.jiang813dc352015-06-02 23:02:46 -04004196 (uint32_t)chunk_data_sz,
Ajay Dudaniab18f022011-05-12 14:39:22 -07004197 (unsigned int*)data))
Ajay Dudani5c761132011-04-07 20:19:04 -07004198 {
4199 fastboot_fail("flash write failure");
4200 return;
4201 }
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05304202 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
4203 fastboot_fail("Bogus size for RAW chunk type");
4204 return;
4205 }
Ajay Dudani5c761132011-04-07 20:19:04 -07004206 total_blocks += chunk_header->chunk_sz;
wufeng.jiang813dc352015-06-02 23:02:46 -04004207 data += (uint32_t)chunk_data_sz;
Ajay Dudani5c761132011-04-07 20:19:04 -07004208 break;
4209
Channagoud Kadabif627cf82013-09-09 14:08:20 -07004210 case CHUNK_TYPE_FILL:
4211 if(chunk_header->total_sz != (sparse_header->chunk_hdr_sz +
4212 sizeof(uint32_t)))
4213 {
4214 fastboot_fail("Bogus chunk size for chunk type FILL");
4215 return;
4216 }
4217
Mayank Grover4f50bba2017-07-19 18:17:08 +05304218 blk_sz_actual = ROUNDUP(sparse_header->blk_sz, CACHE_LINE);
4219 /* Integer overflow detected */
4220 if (blk_sz_actual < sparse_header->blk_sz)
4221 {
4222 fastboot_fail("Invalid block size");
4223 return;
4224 }
4225
4226 fill_buf = (uint32_t *)memalign(CACHE_LINE, blk_sz_actual);
Channagoud Kadabif627cf82013-09-09 14:08:20 -07004227 if (!fill_buf)
4228 {
4229 fastboot_fail("Malloc failed for: CHUNK_TYPE_FILL");
4230 return;
4231 }
4232
Parth Dixit64b1a482016-03-07 16:31:26 +05304233 if (data_end < (uintptr_t)data + sizeof(uint32_t)) {
vijay kumar800255e2015-04-24 20:26:19 +05304234 fastboot_fail("buffer overreads occured due to invalid sparse header");
Gaurav Nebhwani163cbf82016-03-17 21:21:27 +05304235 free(fill_buf);
vijay kumar800255e2015-04-24 20:26:19 +05304236 return;
4237 }
Channagoud Kadabif627cf82013-09-09 14:08:20 -07004238 fill_val = *(uint32_t *)data;
4239 data = (char *) data + sizeof(uint32_t);
Channagoud Kadabif627cf82013-09-09 14:08:20 -07004240
4241 for (i = 0; i < (sparse_header->blk_sz / sizeof(fill_val)); i++)
4242 {
4243 fill_buf[i] = fill_val;
4244 }
4245
Gaurav Nebhwanie94cbe12016-03-17 21:16:34 +05304246 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz))
4247 {
4248 fastboot_fail("bogus size for chunk FILL type");
4249 free(fill_buf);
4250 return;
4251 }
4252
wufeng.jiang813dc352015-06-02 23:02:46 -04004253 for (i = 0; i < chunk_header->chunk_sz; i++)
Channagoud Kadabif627cf82013-09-09 14:08:20 -07004254 {
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05304255 /* Make sure that the data written to partition does not exceed partition size */
4256 if ((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz + sparse_header->blk_sz > size)
4257 {
4258 fastboot_fail("Chunk data size for fill type exceeds partition size");
Gaurav Nebhwani163cbf82016-03-17 21:21:27 +05304259 free(fill_buf);
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05304260 return;
4261 }
4262
Channagoud Kadabif627cf82013-09-09 14:08:20 -07004263 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
4264 sparse_header->blk_sz,
4265 fill_buf))
4266 {
4267 fastboot_fail("flash write failure");
4268 free(fill_buf);
4269 return;
4270 }
4271
4272 total_blocks++;
4273 }
4274
4275 free(fill_buf);
4276 break;
4277
Ajay Dudani5c761132011-04-07 20:19:04 -07004278 case CHUNK_TYPE_DONT_CARE:
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05304279 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
4280 fastboot_fail("bogus size for chunk DONT CARE type");
4281 return;
4282 }
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07004283 total_blocks += chunk_header->chunk_sz;
4284 break;
4285
Ajay Dudani5c761132011-04-07 20:19:04 -07004286 case CHUNK_TYPE_CRC:
4287 if(chunk_header->total_sz != sparse_header->chunk_hdr_sz)
4288 {
wufeng.jiang813dc352015-06-02 23:02:46 -04004289 fastboot_fail("Bogus chunk size for chunk type CRC");
Ajay Dudani5c761132011-04-07 20:19:04 -07004290 return;
4291 }
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05304292 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
4293 fastboot_fail("bogus size for chunk CRC type");
4294 return;
4295 }
Ajay Dudani5c761132011-04-07 20:19:04 -07004296 total_blocks += chunk_header->chunk_sz;
Parth Dixit64b1a482016-03-07 16:31:26 +05304297 if ((uintptr_t)data > UINT_MAX - chunk_data_sz) {
vijay kumar800255e2015-04-24 20:26:19 +05304298 fastboot_fail("integer overflow occured");
4299 return;
4300 }
wufeng.jiang813dc352015-06-02 23:02:46 -04004301 data += (uint32_t)chunk_data_sz;
Parth Dixit64b1a482016-03-07 16:31:26 +05304302 if (data_end < (uintptr_t)data) {
vijay kumar800255e2015-04-24 20:26:19 +05304303 fastboot_fail("buffer overreads occured due to invalid sparse header");
4304 return;
4305 }
Ajay Dudani5c761132011-04-07 20:19:04 -07004306 break;
4307
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07004308 default:
Channagoud Kadabif627cf82013-09-09 14:08:20 -07004309 dprintf(CRITICAL, "Unkown chunk type: %x\n",chunk_header->chunk_type);
Ajay Dudani5c761132011-04-07 20:19:04 -07004310 fastboot_fail("Unknown chunk type");
4311 return;
4312 }
4313 }
4314
Ajay Dudani0c6927b2011-05-18 11:12:16 -07004315 dprintf(INFO, "Wrote %d blocks, expected to write %d blocks\n",
4316 total_blocks, sparse_header->total_blks);
4317
4318 if(total_blocks != sparse_header->total_blks)
4319 {
4320 fastboot_fail("sparse image write failure");
4321 }
Ajay Dudani5c761132011-04-07 20:19:04 -07004322
4323 fastboot_okay("");
4324 return;
4325}
4326
Rahul Shaharef7a5f8b2021-02-04 16:51:36 +05304327static bool CheckVirtualAbCriticalPartition (const char *PartitionName)
4328{
4329 VirtualAbMergeStatus SnapshotMergeStatus;
4330 uint32 Iter = 0;
4331
4332 SnapshotMergeStatus = GetSnapshotMergeStatus ();
4333 if ((SnapshotMergeStatus == MERGING || SnapshotMergeStatus == SNAPSHOTTED)) {
4334 for (Iter = 0; Iter < ARRAY_SIZE (VirtualAbCriticalPartitions); Iter++) {
4335 if (!strncmp (PartitionName, VirtualAbCriticalPartitions[Iter],
4336 strlen (VirtualAbCriticalPartitions[Iter])))
4337 return true;
4338 }
4339 }
4340 return false;
4341}
4342
Ajay Dudani5c761132011-04-07 20:19:04 -07004343void cmd_flash_mmc(const char *arg, void *data, unsigned sz)
4344{
4345 sparse_header_t *sparse_header;
Ajay Dudanide984792015-03-02 09:57:41 -08004346 meta_header_t *meta_header;
Rahul Shaharef7a5f8b2021-02-04 16:51:36 +05304347 VirtualAbMergeStatus SnapshotMergeStatus;
4348 char FlashResultStr[MAX_RSP_SIZE] = "";
Ajay Dudani5c761132011-04-07 20:19:04 -07004349
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07004350#ifdef SSD_ENABLE
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -08004351 /* 8 Byte Magic + 2048 Byte xml + Encrypted Data */
4352 unsigned int *magic_number = (unsigned int *) data;
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08004353 int ret=0;
4354 uint32 major_version=0;
4355 uint32 minor_version=0;
4356
4357 ret = scm_svc_version(&major_version,&minor_version);
4358 if(!ret)
4359 {
4360 if(major_version >= 2)
4361 {
Amir Samuelovbb65ce02013-05-05 12:20:18 +03004362 if( !strcmp(arg, "ssd") || !strcmp(arg, "tqs") )
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08004363 {
4364 ret = encrypt_scm((uint32 **) &data, &sz);
4365 if (ret != 0) {
4366 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
4367 return;
4368 }
4369
Amir Samuelovbb65ce02013-05-05 12:20:18 +03004370 /* Protect only for SSD */
4371 if (!strcmp(arg, "ssd")) {
4372 ret = scm_protect_keystore((uint32 *) data, sz);
4373 if (ret != 0) {
4374 dprintf(CRITICAL, "ERROR: scm_protect_keystore Failed\n");
4375 return;
4376 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08004377 }
4378 }
4379 else
4380 {
4381 ret = decrypt_scm_v2((uint32 **) &data, &sz);
4382 if(ret != 0)
4383 {
4384 dprintf(CRITICAL,"ERROR: Decryption Failure\n");
4385 return;
4386 }
4387 }
4388 }
4389 else
4390 {
4391 if (magic_number[0] == DECRYPT_MAGIC_0 &&
4392 magic_number[1] == DECRYPT_MAGIC_1)
4393 {
4394 ret = decrypt_scm((uint32 **) &data, &sz);
4395 if (ret != 0) {
4396 dprintf(CRITICAL, "ERROR: Invalid secure image\n");
4397 return;
4398 }
4399 }
4400 else if (magic_number[0] == ENCRYPT_MAGIC_0 &&
4401 magic_number[1] == ENCRYPT_MAGIC_1)
4402 {
4403 ret = encrypt_scm((uint32 **) &data, &sz);
4404 if (ret != 0) {
4405 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
4406 return;
4407 }
4408 }
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07004409 }
4410 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08004411 else
Neeti Desai127b9e02012-03-20 16:11:23 -07004412 {
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08004413 dprintf(CRITICAL,"INVALID SVC Version\n");
4414 return;
Neeti Desai127b9e02012-03-20 16:11:23 -07004415 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08004416#endif /* SSD_ENABLE */
Neeti Desai127b9e02012-03-20 16:11:23 -07004417
Monika Singh292b3e92018-03-17 22:40:23 +05304418#if VERIFIED_BOOT || VERIFIED_BOOT_2
Channagoud Kadabi35297672015-06-13 11:09:49 -07004419 if (target_build_variant_user())
Shashank Mittald3e54dd2014-08-28 15:24:02 -07004420 {
lijuang511a2b52015-08-14 20:50:51 +08004421 /* if device is locked:
4422 * common partition will not allow to be flashed
4423 * critical partition will allow to flash image.
4424 */
4425 if(!device.is_unlocked && !critical_flash_allowed(arg)) {
4426 fastboot_fail("Partition flashing is not allowed");
4427 return;
4428 }
Mayank Grover889be1b2017-09-12 20:12:23 +05304429
lijuang511a2b52015-08-14 20:50:51 +08004430 /* if device critical is locked:
4431 * common partition will allow to be flashed
4432 * critical partition will not allow to flash image.
4433 */
Mayank Grover912eaa62017-10-26 12:08:53 +05304434 if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05304435 !device.is_unlock_critical &&
4436 critical_flash_allowed(arg)) {
4437 fastboot_fail("Critical partition flashing is not allowed");
4438 return;
Shashank Mittald3e54dd2014-08-28 15:24:02 -07004439 }
4440 }
4441#endif
Rahul Shaharef7a5f8b2021-02-04 16:51:36 +05304442
4443 if (target_virtual_ab_supported())
4444 {
4445 if (CheckVirtualAbCriticalPartition (arg)) {
4446 snprintf(FlashResultStr, MAX_RSP_SIZE,"Flashing of %s is not allowed in %s state",
4447 arg, SnapshotMergeState);
4448 fastboot_fail(FlashResultStr);
4449 return;
4450 }
4451
4452 SnapshotMergeStatus = GetSnapshotMergeStatus ();
4453 if (((SnapshotMergeStatus == MERGING) || (SnapshotMergeStatus == SNAPSHOTTED)) &&
4454 !strncmp (arg, "super", strlen ("super"))) {
4455 if(SetSnapshotMergeStatus (CANCELLED))
4456 {
4457 fastboot_fail("Failed to update snapshot state to cancel");
4458 return;
4459 }
4460
4461 //updating fbvar snapshot-merge-state
4462 snprintf(SnapshotMergeState,strlen(VabSnapshotMergeStatus[NONE_MERGE_STATUS]) + 1,
4463 "%s", VabSnapshotMergeStatus[NONE_MERGE_STATUS]);
4464 }
4465 }
4466
Monika Singh292b3e92018-03-17 22:40:23 +05304467 if (!strncmp(arg, "avb_custom_key", strlen("avb_custom_key"))) {
4468 dprintf(INFO, "flashing avb_custom_key\n");
4469 if (store_userkey(data, sz)) {
4470 fastboot_fail("Flashing avb_custom_key failed");
4471 } else {
4472 fastboot_okay("");
4473 }
4474 return;
4475 }
Shashank Mittald3e54dd2014-08-28 15:24:02 -07004476
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07004477 sparse_header = (sparse_header_t *) data;
Ajay Dudanide984792015-03-02 09:57:41 -08004478 meta_header = (meta_header_t *) data;
4479 if (sparse_header->magic == SPARSE_HEADER_MAGIC)
Ajay Dudani5c761132011-04-07 20:19:04 -07004480 cmd_flash_mmc_sparse_img(arg, data, sz);
Ajay Dudanide984792015-03-02 09:57:41 -08004481 else if (meta_header->magic == META_HEADER_MAGIC)
4482 cmd_flash_meta_img(arg, data, sz);
4483 else
4484 cmd_flash_mmc_img(arg, data, sz);
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07004485
4486#if VERIFIED_BOOT
Mayank Grover912eaa62017-10-26 12:08:53 +05304487 if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05304488 (!strncmp(arg, "system", 6)) &&
4489 !device.verity_mode)
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07004490 // reset dm_verity mode to enforcing
4491 device.verity_mode = 1;
4492 write_device_info(&device);
Parth Dixitddbc7352015-10-18 03:13:31 +05304493#endif
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07004494
Ajay Dudani5c761132011-04-07 20:19:04 -07004495 return;
4496}
4497
Tanya Brokhman84eaaf62015-02-24 21:57:06 +02004498void cmd_updatevol(const char *vol_name, void *data, unsigned sz)
4499{
4500 struct ptentry *sys_ptn;
4501 struct ptable *ptable;
4502
4503 ptable = flash_get_ptable();
4504 if (ptable == NULL) {
4505 fastboot_fail("partition table doesn't exist");
4506 return;
4507 }
4508
4509 sys_ptn = ptable_find(ptable, "system");
4510 if (sys_ptn == NULL) {
4511 fastboot_fail("system partition not found");
4512 return;
4513 }
4514
4515 sz = ROUND_TO_PAGE(sz, page_mask);
4516 if (update_ubi_vol(sys_ptn, vol_name, data, sz))
4517 fastboot_fail("update_ubi_vol failed");
4518 else
4519 fastboot_okay("");
4520}
4521
Sridhar Parasurame94e8152014-10-24 14:06:03 -07004522void cmd_flash_nand(const char *arg, void *data, unsigned sz)
Dima Zavin214cc642009-01-26 11:16:21 -08004523{
4524 struct ptentry *ptn;
4525 struct ptable *ptable;
4526 unsigned extra = 0;
Gaurav Nebhwanid9730712016-05-06 14:28:29 +05304527 uint64_t partition_size = 0;
Vijay Kumar Pendotic6f22a72017-05-19 22:44:34 +05304528 unsigned bytes_to_round_page = 0;
4529 unsigned rounded_size = 0;
Dima Zavin214cc642009-01-26 11:16:21 -08004530
Kishor PK38ed93d2017-04-25 14:19:26 +05304531 if((uintptr_t)data > (UINT_MAX - sz)) {
4532 fastboot_fail("Cannot flash: image header corrupt");
4533 return;
4534 }
4535
Dima Zavin214cc642009-01-26 11:16:21 -08004536 ptable = flash_get_ptable();
4537 if (ptable == NULL) {
4538 fastboot_fail("partition table doesn't exist");
4539 return;
4540 }
4541
4542 ptn = ptable_find(ptable, arg);
4543 if (ptn == NULL) {
Tanya Brokhman84eaaf62015-02-24 21:57:06 +02004544 dprintf(INFO, "unknown partition name (%s). Trying updatevol\n",
4545 arg);
4546 cmd_updatevol(arg, data, sz);
Dima Zavin214cc642009-01-26 11:16:21 -08004547 return;
4548 }
4549
Monika Singh292b3e92018-03-17 22:40:23 +05304550 if (!strncmp(arg, "avb_custom_key", strlen("avb_custom_key"))) {
4551 dprintf(INFO, "flashing avb_custom_key\n");
4552 if (store_userkey(data, sz)) {
4553 fastboot_fail("Flashing avb_custom_key failed");
4554 } else {
4555 fastboot_okay("");
4556 }
4557 return;
4558 }
4559
Dima Zavin214cc642009-01-26 11:16:21 -08004560 if (!strcmp(ptn->name, "boot") || !strcmp(ptn->name, "recovery")) {
Kishor PK38ed93d2017-04-25 14:19:26 +05304561 if((sz > BOOT_MAGIC_SIZE) && (!memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE))) {
4562 dprintf(INFO, "Verified the BOOT_MAGIC in image header \n");
4563 } else {
4564 fastboot_fail("Image is not a boot image");
Dima Zavin214cc642009-01-26 11:16:21 -08004565 return;
4566 }
4567 }
4568
Amol Jadi5c61a952012-05-04 17:05:35 -07004569 if (!strcmp(ptn->name, "system")
Deepa Dinamani13e32c42012-03-12 14:34:17 -07004570 || !strcmp(ptn->name, "userdata")
4571 || !strcmp(ptn->name, "persist")
Sundarajan Srinivasanb063a852013-11-19 14:02:27 -08004572 || !strcmp(ptn->name, "recoveryfs")
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -08004573 || !strcmp(ptn->name, "modem"))
Tanya Brokhman1c94f1a2015-02-15 09:05:03 +02004574 extra = 1;
Kishor PK38ed93d2017-04-25 14:19:26 +05304575 else {
Vijay Kumar Pendotic6f22a72017-05-19 22:44:34 +05304576 rounded_size = ROUNDUP(sz, page_size);
4577 bytes_to_round_page = rounded_size - sz;
4578 if (bytes_to_round_page) {
4579 if (((uintptr_t)data + sz ) > (UINT_MAX - bytes_to_round_page)) {
4580 fastboot_fail("Integer overflow detected");
4581 return;
4582 }
4583 if (((uintptr_t)data + sz + bytes_to_round_page) >
4584 ((uintptr_t)target_get_scratch_address() + target_get_max_flash_size())) {
4585 fastboot_fail("Buffer size is not aligned to page_size");
4586 return;
4587 }
4588 else {
4589 memset(data + sz, 0, bytes_to_round_page);
4590 sz = rounded_size;
4591 }
Kishor PK38ed93d2017-04-25 14:19:26 +05304592 }
Gaurav Nebhwanid9730712016-05-06 14:28:29 +05304593 }
4594
Kishor PK38ed93d2017-04-25 14:19:26 +05304595 /*Checking partition_size for the possible integer overflow */
4596 partition_size = validate_partition_size(ptn);
4597
Gaurav Nebhwanid9730712016-05-06 14:28:29 +05304598 if (sz > partition_size) {
4599 fastboot_fail("Image size too large");
4600 return;
4601 }
4602
Dima Zavin214cc642009-01-26 11:16:21 -08004603 dprintf(INFO, "writing %d bytes to '%s'\n", sz, ptn->name);
Mayank Grover6cde9352017-06-06 18:45:23 +05304604 if ((sz > UBI_EC_HDR_SIZE) &&
4605 (!memcmp((void *)data, UBI_MAGIC, UBI_MAGIC_SIZE))) {
Tanya Brokhman1c94f1a2015-02-15 09:05:03 +02004606 if (flash_ubi_img(ptn, data, sz)) {
4607 fastboot_fail("flash write failure");
4608 return;
4609 }
4610 } else {
4611 if (flash_write(ptn, extra, data, sz)) {
4612 fastboot_fail("flash write failure");
4613 return;
4614 }
Dima Zavin214cc642009-01-26 11:16:21 -08004615 }
4616 dprintf(INFO, "partition '%s' updated\n", ptn->name);
4617 fastboot_okay("");
4618}
4619
Kishor PK38ed93d2017-04-25 14:19:26 +05304620
4621static inline uint64_t validate_partition_size(struct ptentry *ptn)
4622{
4623 if (ptn->length && flash_num_pages_per_blk() && page_size) {
4624 if ((ptn->length < ( UINT_MAX / flash_num_pages_per_blk())) && ((ptn->length * flash_num_pages_per_blk()) < ( UINT_MAX / page_size))) {
4625 return ptn->length * flash_num_pages_per_blk() * page_size;
4626 }
4627 }
4628 return 0;
4629}
4630
4631
Sridhar Parasurame94e8152014-10-24 14:06:03 -07004632void cmd_flash(const char *arg, void *data, unsigned sz)
4633{
4634 if(target_is_emmc_boot())
4635 cmd_flash_mmc(arg, data, sz);
4636 else
4637 cmd_flash_nand(arg, data, sz);
4638}
4639
Dima Zavin214cc642009-01-26 11:16:21 -08004640void cmd_continue(const char *arg, void *data, unsigned sz)
4641{
4642 fastboot_okay("");
Amol Jadi7c4316c2013-10-07 14:19:26 -07004643 fastboot_stop();
Shashank Mittald3e54dd2014-08-28 15:24:02 -07004644
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004645 if (target_is_emmc_boot())
4646 {
lijuanga40d6302015-07-20 20:10:13 +08004647#if FBCON_DISPLAY_MSG
lijuangde34d502016-02-26 16:04:50 +08004648 /* Exit keys' detection thread firstly */
4649 exit_menu_keys_detection();
lijuanga40d6302015-07-20 20:10:13 +08004650#endif
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004651 boot_linux_from_mmc();
4652 }
4653 else
4654 {
4655 boot_linux_from_flash();
4656 }
Dima Zavin214cc642009-01-26 11:16:21 -08004657}
4658
Chandan Uddaraju94183c02010-01-15 15:13:59 -08004659void cmd_reboot(const char *arg, void *data, unsigned sz)
4660{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004661 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08004662 fastboot_okay("");
4663 reboot_device(0);
4664}
4665
Mayank Grover351a75e2017-05-30 20:06:08 +05304666void cmd_set_active(const char *arg, void *data, unsigned sz)
4667{
Mayank Grover6ccc1c92017-07-04 17:36:46 +05304668 char *p, *sp = NULL;
Mayank Grover351a75e2017-05-30 20:06:08 +05304669 unsigned i,current_active_slot;
4670 const char *current_slot_suffix;
4671
4672 if (!partition_multislot_is_supported())
4673 {
4674 fastboot_fail("Command not supported");
4675 return;
4676 }
4677
Rahul Shaharef7a5f8b2021-02-04 16:51:36 +05304678 if (target_virtual_ab_supported()) {
4679 if (GetSnapshotMergeStatus () == MERGING) {
4680 fastboot_fail ("Slot Change is not allowed in merging state");
4681 return;
4682 }
4683 }
4684
Mayank Grover351a75e2017-05-30 20:06:08 +05304685 if (arg)
4686 {
Mayank Grover6ccc1c92017-07-04 17:36:46 +05304687 p = strtok_r((char *)arg, ":", &sp);
Mayank Grover5eb5f692017-07-19 20:16:04 +05304688 if (p)
Mayank Grover351a75e2017-05-30 20:06:08 +05304689 {
4690 current_active_slot = partition_find_active_slot();
4691
4692 /* Check if trying to make curent slot active */
4693 current_slot_suffix = SUFFIX_SLOT(current_active_slot);
Mayank Grover6ccc1c92017-07-04 17:36:46 +05304694 current_slot_suffix = strtok_r((char *)current_slot_suffix,
4695 (char *)suffix_delimiter, &sp);
4696
Mayank Grover5eb5f692017-07-19 20:16:04 +05304697 if (current_slot_suffix &&
4698 !strncmp(p, current_slot_suffix, strlen(current_slot_suffix)))
Mayank Grover351a75e2017-05-30 20:06:08 +05304699 {
4700 fastboot_okay("Slot already set active");
4701 return;
4702 }
4703 else
4704 {
4705 for (i = 0; i < AB_SUPPORTED_SLOTS; i++)
4706 {
4707 current_slot_suffix = SUFFIX_SLOT(i);
Mayank Grover6ccc1c92017-07-04 17:36:46 +05304708 current_slot_suffix = strtok_r((char *)current_slot_suffix,
4709 (char *)suffix_delimiter, &sp);
Mayank Grover5eb5f692017-07-19 20:16:04 +05304710 if (current_slot_suffix &&
4711 !strncmp(p, current_slot_suffix, strlen(current_slot_suffix)))
Mayank Grover351a75e2017-05-30 20:06:08 +05304712 {
lijuang8b03e5f2019-07-12 18:16:19 +08004713 partition_switch_slots(current_active_slot, i, true);
Mayank Grover351a75e2017-05-30 20:06:08 +05304714 publish_getvar_multislot_vars();
4715 fastboot_okay("");
4716 return;
4717 }
4718 }
4719 }
4720 }
4721 }
4722 fastboot_fail("Invalid slot suffix.");
4723 return;
4724}
4725
Mayank Grover98c4c742019-04-25 17:21:37 +05304726#if DYNAMIC_PARTITION_SUPPORT
4727void cmd_reboot_fastboot(const char *arg, void *data, unsigned sz)
4728{
4729 dprintf(INFO, "rebooting the device - userspace fastboot\n");
4730 if (send_recovery_cmd(RECOVERY_BOOT_FASTBOOT_CMD)) {
4731 dprintf(CRITICAL, "ERROR: Failed to update recovery commands\n");
4732 fastboot_fail("Failed to update recovery command");
4733 return;
4734 }
4735 fastboot_okay("");
4736 reboot_device(REBOOT_MODE_UNKNOWN);
4737
4738 //shouldn't come here.
4739 dprintf(CRITICAL, "ERROR: Failed to reboot device\n");
4740 return;
4741}
4742
4743void cmd_reboot_recovery(const char *arg, void *data, unsigned sz)
4744{
4745 dprintf(INFO, "rebooting the device - recovery\n");
4746 if (send_recovery_cmd(RECOVERY_BOOT_RECOVERY_CMD)) {
4747 dprintf(CRITICAL, "ERROR: Failed to update recovery commands\n");
4748 fastboot_fail("Failed to update recovery command");
4749 return;
4750 }
4751 fastboot_okay("");
4752 reboot_device(REBOOT_MODE_UNKNOWN);
4753
4754 //shouldn't come here.
4755 dprintf(CRITICAL, "ERROR: Failed to reboot device\n");
4756 return;
4757}
Rahul Shaharef7a5f8b2021-02-04 16:51:36 +05304758
4759#ifdef VIRTUAL_AB_OTA
4760void CmdUpdateSnapshot(const char *arg, void *data, unsigned sz)
4761{
4762 char *command = NULL;
4763 const char *delim = ":";
4764 char *sp;
4765
4766 if (arg) {
4767 command = strtok_r((char *)arg, delim, &sp);
4768 if (command) {
4769 command++;
4770
4771 if(!strncmp (command, "merge", AsciiStrLen ("merge"))) {
4772 if (GetSnapshotMergeStatus () == MERGING) {
4773 cmd_reboot_fastboot(Arg, Data, Size);
4774 }
4775 FastbootOkay ("");
4776 return;
4777 }
4778 else if (!strncmp (Command, "cancel", AsciiStrLen ("cancel"))) {
4779 if(!device.is_unlocked) {
4780 fastboot_fail ("Snapshot Cancel is not allowed in Lock State");
4781 return;
4782 }
4783
4784 if (SetSnapshotMergeStatus (CANCELLED))
4785 {
4786 fastboot_fail("Failed to update snapshot state to cancel");
4787 return;
4788 }
4789
4790 //updating fbvar snapshot-merge-state
4791 snprintf(SnapshotMergeState, strlen(VabSnapshotMergeStatus[NONE_MERGE_STATUS]) + 1,
4792 "%s", VabSnapshotMergeStatus[NONE_MERGE_STATUS]);
4793 fastboot_okay("");
4794 return;
4795 }
4796 }
4797 }
4798 fastboot_fail("Invalid snapshot-update command");
4799 return;
4800}
4801#endif
Mayank Grover98c4c742019-04-25 17:21:37 +05304802#endif
4803
Chandan Uddaraju94183c02010-01-15 15:13:59 -08004804void cmd_reboot_bootloader(const char *arg, void *data, unsigned sz)
4805{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004806 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08004807 fastboot_okay("");
4808 reboot_device(FASTBOOT_MODE);
4809}
4810
Ameya Thakur11cf1a62013-08-05 12:44:48 -07004811void cmd_oem_enable_charger_screen(const char *arg, void *data, unsigned size)
4812{
4813 dprintf(INFO, "Enabling charger screen check\n");
4814 device.charger_screen_enabled = 1;
4815 write_device_info(&device);
4816 fastboot_okay("");
4817}
4818
4819void cmd_oem_disable_charger_screen(const char *arg, void *data, unsigned size)
4820{
4821 dprintf(INFO, "Disabling charger screen check\n");
4822 device.charger_screen_enabled = 0;
4823 write_device_info(&device);
4824 fastboot_okay("");
4825}
4826
lijuanga25d8bb2015-09-16 13:11:52 +08004827void cmd_oem_off_mode_charger(const char *arg, void *data, unsigned size)
4828{
4829 char *p = NULL;
4830 const char *delim = " \t\n\r";
Parth Dixit407f15b2016-01-07 14:47:37 +05304831 char *sp;
lijuanga25d8bb2015-09-16 13:11:52 +08004832
4833 if (arg) {
Parth Dixit407f15b2016-01-07 14:47:37 +05304834 p = strtok_r((char *)arg, delim, &sp);
lijuanga25d8bb2015-09-16 13:11:52 +08004835 if (p) {
4836 if (!strncmp(p, "0", 1)) {
4837 device.charger_screen_enabled = 0;
4838 } else if (!strncmp(p, "1", 1)) {
4839 device.charger_screen_enabled = 1;
4840 }
4841 }
4842 }
4843
4844 /* update charger_screen_enabled value for getvar
4845 * command
4846 */
4847 snprintf(charger_screen_enabled, MAX_RSP_SIZE, "%d",
4848 device.charger_screen_enabled);
4849
4850 write_device_info(&device);
4851 fastboot_okay("");
4852}
4853
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05304854void cmd_oem_select_display_panel(const char *arg, void *data, unsigned size)
4855{
4856 dprintf(INFO, "Selecting display panel %s\n", arg);
4857 if (arg)
4858 strlcpy(device.display_panel, arg,
4859 sizeof(device.display_panel));
4860 write_device_info(&device);
4861 fastboot_okay("");
4862}
4863
Shashank Mittal162244e2011-08-08 19:01:25 -07004864void cmd_oem_unlock(const char *arg, void *data, unsigned sz)
4865{
lijuang4ece1e72015-08-14 21:02:36 +08004866 set_device_unlock(UNLOCK, TRUE);
vijay kumarc65876c2015-04-24 13:29:16 +05304867}
4868
4869void cmd_oem_unlock_go(const char *arg, void *data, unsigned sz)
4870{
lijuang4ece1e72015-08-14 21:02:36 +08004871 if(!device.is_unlocked) {
vijay kumarc65876c2015-04-24 13:29:16 +05304872 if(!is_allow_unlock) {
4873 fastboot_fail("oem unlock is not allowed");
4874 return;
4875 }
4876
lijuang4ece1e72015-08-14 21:02:36 +08004877 set_device_unlock_value(UNLOCK, TRUE);
vijay kumarc65876c2015-04-24 13:29:16 +05304878
lijuang4ece1e72015-08-14 21:02:36 +08004879 /* wipe data */
vijay kumarc65876c2015-04-24 13:29:16 +05304880 struct recovery_message msg;
Kishor PK60a68212017-05-08 16:55:57 +05304881 memset(&msg, 0, sizeof(msg));
vijay kumarc65876c2015-04-24 13:29:16 +05304882 snprintf(msg.recovery, sizeof(msg.recovery), "recovery\n--wipe_data");
4883 write_misc(0, &msg, sizeof(msg));
4884
4885 fastboot_okay("");
4886 reboot_device(RECOVERY_MODE);
Shashank Mittald3e54dd2014-08-28 15:24:02 -07004887 }
4888 fastboot_okay("");
4889}
4890
Channagoud Kadabiad259832015-05-29 11:14:17 -07004891static int aboot_frp_unlock(char *pname, void *data, unsigned sz)
4892{
Mayank Grover70f8b0e2017-05-17 11:49:00 +05304893 int ret=1;
4894 bool authentication_success=false;
Channagoud Kadabiad259832015-05-29 11:14:17 -07004895
Mayank Grover70f8b0e2017-05-17 11:49:00 +05304896 /*
4897 Authentication method not implemented.
4898
4899 OEM to implement, authentication system which on successful validataion,
4900 calls write_allow_oem_unlock() with is_allow_unlock.
4901 */
4902#if 0
4903 authentication_success = oem_specific_auth_mthd();
4904#endif
4905
4906 if (authentication_success)
Channagoud Kadabiad259832015-05-29 11:14:17 -07004907 {
Mayank Grover70f8b0e2017-05-17 11:49:00 +05304908 is_allow_unlock = true;
4909 write_allow_oem_unlock(is_allow_unlock);
4910 ret = 0;
Channagoud Kadabiad259832015-05-29 11:14:17 -07004911 }
4912 return ret;
4913}
4914
Shashank Mittald3e54dd2014-08-28 15:24:02 -07004915void cmd_oem_lock(const char *arg, void *data, unsigned sz)
4916{
lijuang4ece1e72015-08-14 21:02:36 +08004917 set_device_unlock(UNLOCK, FALSE);
Shashank Mittal162244e2011-08-08 19:01:25 -07004918}
4919
Shashank Mittala0032282011-08-26 14:50:11 -07004920void cmd_oem_devinfo(const char *arg, void *data, unsigned sz)
4921{
lijuang511a2b52015-08-14 20:50:51 +08004922 char response[MAX_RSP_SIZE];
Ameya Thakur11cf1a62013-08-05 12:44:48 -07004923 snprintf(response, sizeof(response), "\tDevice tampered: %s", (device.is_tampered ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07004924 fastboot_info(response);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07004925 snprintf(response, sizeof(response), "\tDevice unlocked: %s", (device.is_unlocked ? "true" : "false"));
4926 fastboot_info(response);
Monika Singh292b3e92018-03-17 22:40:23 +05304927#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover912eaa62017-10-26 12:08:53 +05304928 if (VB_M <= target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +05304929 {
4930 snprintf(response, sizeof(response), "\tDevice critical unlocked: %s",
4931 (device.is_unlock_critical ? "true" : "false"));
4932 fastboot_info(response);
4933 }
Parth Dixitddbc7352015-10-18 03:13:31 +05304934#endif
Ameya Thakur11cf1a62013-08-05 12:44:48 -07004935 snprintf(response, sizeof(response), "\tCharger screen enabled: %s", (device.charger_screen_enabled ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07004936 fastboot_info(response);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05304937 snprintf(response, sizeof(response), "\tDisplay panel: %s", (device.display_panel));
4938 fastboot_info(response);
Shashank Mittala0032282011-08-26 14:50:11 -07004939 fastboot_okay("");
4940}
4941
lijuang511a2b52015-08-14 20:50:51 +08004942void cmd_flashing_get_unlock_ability(const char *arg, void *data, unsigned sz)
4943{
4944 char response[MAX_RSP_SIZE];
4945 snprintf(response, sizeof(response), "\tget_unlock_ability: %d", is_allow_unlock);
4946 fastboot_info(response);
4947 fastboot_okay("");
4948}
4949
4950void cmd_flashing_lock_critical(const char *arg, void *data, unsigned sz)
4951{
lijuang4ece1e72015-08-14 21:02:36 +08004952 set_device_unlock(UNLOCK_CRITICAL, FALSE);
lijuang511a2b52015-08-14 20:50:51 +08004953}
4954
4955void cmd_flashing_unlock_critical(const char *arg, void *data, unsigned sz)
4956{
lijuang4ece1e72015-08-14 21:02:36 +08004957 set_device_unlock(UNLOCK_CRITICAL, TRUE);
lijuang511a2b52015-08-14 20:50:51 +08004958}
4959
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07004960void cmd_preflash(const char *arg, void *data, unsigned sz)
4961{
4962 fastboot_okay("");
4963}
4964
Mao Flynn7b379f32015-04-20 00:28:30 +08004965static uint8_t logo_header[LOGO_IMG_HEADER_SIZE];
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304966
Mao Flynn7b379f32015-04-20 00:28:30 +08004967int splash_screen_check_header(logo_img_header *header)
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304968{
Mao Flynn7b379f32015-04-20 00:28:30 +08004969 if (memcmp(header->magic, LOGO_IMG_MAGIC, 8))
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304970 return -1;
Mao Flynn7b379f32015-04-20 00:28:30 +08004971 if (header->width == 0 || header->height == 0)
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304972 return -1;
Ashish Bhimanpalliwar36f9f002021-04-14 13:50:52 +05304973 if (((header->blocks == 0) || (header->blocks > UINT_MAX/512))) {
Ashish Bhimanpalliwareab60c62020-10-09 16:31:11 +05304974 return -1;
4975 }
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304976 return 0;
4977}
4978
Mao Flynn7b379f32015-04-20 00:28:30 +08004979int splash_screen_flash()
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07004980{
4981 struct ptentry *ptn;
4982 struct ptable *ptable;
Mao Flynn7b379f32015-04-20 00:28:30 +08004983 struct logo_img_header *header;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07004984 struct fbcon_config *fb_display = NULL;
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08004985
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304986 ptable = flash_get_ptable();
4987 if (ptable == NULL) {
Mao Flynn7b379f32015-04-20 00:28:30 +08004988 dprintf(CRITICAL, "ERROR: Partition table not found\n");
4989 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304990 }
Mao Flynn7b379f32015-04-20 00:28:30 +08004991
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304992 ptn = ptable_find(ptable, "splash");
4993 if (ptn == NULL) {
4994 dprintf(CRITICAL, "ERROR: splash Partition not found\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08004995 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304996 }
Mao Flynn7b379f32015-04-20 00:28:30 +08004997 if (flash_read(ptn, 0, (void *)logo_header, LOGO_IMG_HEADER_SIZE)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304998 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08004999 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05305000 }
5001
Mao Flynn7b379f32015-04-20 00:28:30 +08005002 header = (struct logo_img_header *)logo_header;
5003 if (splash_screen_check_header(header)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05305004 dprintf(CRITICAL, "ERROR: Boot image header invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08005005 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05305006 }
5007
5008 fb_display = fbcon_display();
5009 if (fb_display) {
Ashish Bhimanpalliwareab60c62020-10-09 16:31:11 +05305010 if (header->type &&
5011 ((header->blocks * 512) <= (fb_display->width *
5012 fb_display->height * (fb_display->bpp / 8)))) {
raghavendra ambadas554f4fd2018-10-09 15:50:40 +05305013 /* RLE24 compressed data */
Mao Flynn7b379f32015-04-20 00:28:30 +08005014 uint8_t *base = (uint8_t *) fb_display->base + LOGO_IMG_OFFSET;
5015
5016 /* if the logo is full-screen size, remove "fbcon_clear()" */
5017 if ((header->width != fb_display->width)
5018 || (header->height != fb_display->height))
5019 fbcon_clear();
5020
5021 if (flash_read(ptn + LOGO_IMG_HEADER_SIZE, 0,
5022 (uint32_t *)base,
5023 (header->blocks * 512))) {
5024 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
5025 return -1;
5026 }
5027 fbcon_extract_to_screen(header, base);
5028 return 0;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07005029 }
Mao Flynn7b379f32015-04-20 00:28:30 +08005030
Veera Sundaram Sankaranc33416f2015-09-28 10:25:55 -07005031 if ((header->width > fb_display->width) || (header->height > fb_display->height)) {
5032 dprintf(CRITICAL, "Logo config greater than fb config. Fall back default logo\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08005033 return -1;
5034 }
5035
Vineet Bajaj99291ed2014-09-09 12:29:46 +05305036 uint8_t *base = (uint8_t *) fb_display->base;
Sachin Bhayare619e9e42017-05-15 13:10:31 +05305037 uint32_t fb_size = ROUNDUP(fb_display->width *
5038 fb_display->height *
5039 (fb_display->bpp / 8), 4096);
5040 uint32_t splash_size = ((((header->width * header->height *
5041 fb_display->bpp/8) + 511) >> 9) << 9);
5042
Ashish Bhimanpalliwar36f9f002021-04-14 13:50:52 +05305043 if ((header->height * header->width * (fb_display->bpp/8)) > (header->blocks * 512)) {
5044 dprintf(CRITICAL, "ERROR: Splash image size invalid\n");
5045 return -1;
5046 }
5047
Sachin Bhayare619e9e42017-05-15 13:10:31 +05305048 if (splash_size > fb_size) {
5049 dprintf(CRITICAL, "ERROR: Splash image size invalid\n");
5050 return -1;
5051 }
5052
Mao Flynn7b379f32015-04-20 00:28:30 +08005053 if (flash_read(ptn + LOGO_IMG_HEADER_SIZE, 0,
5054 (uint32_t *)base,
5055 ((((header->width * header->height * fb_display->bpp/8) + 511) >> 9) << 9))) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05305056 fbcon_clear();
Vineet Bajaj99291ed2014-09-09 12:29:46 +05305057 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08005058 return -1;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07005059 }
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05305060 }
5061
Mao Flynn7b379f32015-04-20 00:28:30 +08005062 return 0;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05305063}
5064
Mao Flynn7b379f32015-04-20 00:28:30 +08005065int splash_screen_mmc()
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05305066{
5067 int index = INVALID_PTN;
5068 unsigned long long ptn = 0;
5069 struct fbcon_config *fb_display = NULL;
Mao Flynn7b379f32015-04-20 00:28:30 +08005070 struct logo_img_header *header;
Mao Flynn1893a7f2015-06-03 12:03:36 +08005071 uint32_t blocksize, realsize, readsize;
5072 uint8_t *base;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05305073
5074 index = partition_get_index("splash");
5075 if (index == 0) {
5076 dprintf(CRITICAL, "ERROR: splash Partition table not found\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08005077 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05305078 }
5079
5080 ptn = partition_get_offset(index);
5081 if (ptn == 0) {
5082 dprintf(CRITICAL, "ERROR: splash Partition invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08005083 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05305084 }
5085
Mao Flynn1893a7f2015-06-03 12:03:36 +08005086 mmc_set_lun(partition_get_lun(index));
5087
5088 blocksize = mmc_get_device_blocksize();
5089 if (blocksize == 0) {
5090 dprintf(CRITICAL, "ERROR:splash Partition invalid blocksize\n");
5091 return -1;
5092 }
5093
5094 fb_display = fbcon_display();
Channagoud Kadabidaf10a62015-07-22 11:51:55 -07005095 if (!fb_display)
5096 {
5097 dprintf(CRITICAL, "ERROR: fb config is not allocated\n");
5098 return -1;
5099 }
5100
Mao Flynn1893a7f2015-06-03 12:03:36 +08005101 base = (uint8_t *) fb_display->base;
5102
jialongjhan2769d202020-05-22 18:25:56 +08005103 if (mmc_read(ptn , (uint32_t *)(base + LOGO_IMG_OFFSET), blocksize)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05305104 dprintf(CRITICAL, "ERROR: Cannot read splash image header\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08005105 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05305106 }
Mao Flynn1893a7f2015-06-03 12:03:36 +08005107 header = (struct logo_img_header *)(base + LOGO_IMG_OFFSET);
Mao Flynn7b379f32015-04-20 00:28:30 +08005108 if (splash_screen_check_header(header)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05305109 dprintf(CRITICAL, "ERROR: Splash image header invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08005110 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05305111 }
5112
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05305113 if (fb_display) {
Ashish Bhimanpalliwareab60c62020-10-09 16:31:11 +05305114 if (header->type &&
Ashish Bhimanpalliwarce39f482020-10-06 15:27:37 +05305115 (((UINT_MAX - LOGO_IMG_HEADER_SIZE) / 512) >= header->blocks) &&
raghavendra ambadas554f4fd2018-10-09 15:50:40 +05305116 ((header->blocks * 512) <= (fb_display->width *
5117 fb_display->height * (fb_display->bpp / 8)))) {
5118 /* 1 RLE24 compressed data */
Mao Flynn1893a7f2015-06-03 12:03:36 +08005119 base += LOGO_IMG_OFFSET;
Mao Flynn7b379f32015-04-20 00:28:30 +08005120
Mao Flynn1893a7f2015-06-03 12:03:36 +08005121 realsize = header->blocks * 512;
5122 readsize = ROUNDUP((realsize + LOGO_IMG_HEADER_SIZE), blocksize) - blocksize;
5123
5124 /* if the logo is not full-screen size, clean screen */
Mao Flynn7b379f32015-04-20 00:28:30 +08005125 if ((header->width != fb_display->width)
5126 || (header->height != fb_display->height))
5127 fbcon_clear();
5128
Sachin Bhayare619e9e42017-05-15 13:10:31 +05305129 uint32_t fb_size = ROUNDUP(fb_display->width *
5130 fb_display->height *
5131 (fb_display->bpp / 8), 4096);
5132
5133 if (readsize > fb_size) {
5134 dprintf(CRITICAL, "ERROR: Splash image size invalid\n");
5135 return -1;
5136 }
5137
jialongjhan2769d202020-05-22 18:25:56 +08005138 if (mmc_read(ptn + blocksize, (uint32_t *)(base + blocksize), readsize)) {
Mao Flynn7b379f32015-04-20 00:28:30 +08005139 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
5140 return -1;
5141 }
Mao Flynn7b379f32015-04-20 00:28:30 +08005142
Mao Flynn1893a7f2015-06-03 12:03:36 +08005143 fbcon_extract_to_screen(header, (base + LOGO_IMG_HEADER_SIZE));
5144 } else { /* 2 Raw BGR data */
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05305145
Veera Sundaram Sankaranc33416f2015-09-28 10:25:55 -07005146 if ((header->width > fb_display->width) || (header->height > fb_display->height)) {
5147 dprintf(CRITICAL, "Logo config greater than fb config. Fall back default logo\n");
Mao Flynn1893a7f2015-06-03 12:03:36 +08005148 return -1;
5149 }
5150
5151 realsize = header->width * header->height * fb_display->bpp / 8;
5152 readsize = ROUNDUP((realsize + LOGO_IMG_HEADER_SIZE), blocksize) - blocksize;
Ashish Bhimanpalliwar36f9f002021-04-14 13:50:52 +05305153 if (realsize > (header->blocks * 512)) {
5154 dprintf(CRITICAL, "Logo Size error\n");
5155 return -1;
5156 }
Mao Flynn1893a7f2015-06-03 12:03:36 +08005157
5158 if (blocksize == LOGO_IMG_HEADER_SIZE) { /* read the content directly */
Kishor PKee5c0a32018-03-06 16:49:46 +05305159 if (mmc_read((ptn + PLL_CODES_OFFSET + LOGO_IMG_HEADER_SIZE), (uint32_t *)base, readsize)) {
Mao Flynn1893a7f2015-06-03 12:03:36 +08005160 fbcon_clear();
5161 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
5162 return -1;
5163 }
5164 } else {
Kishor PKee5c0a32018-03-06 16:49:46 +05305165 if (mmc_read(ptn + PLL_CODES_OFFSET + blocksize ,
Mao Flynn1893a7f2015-06-03 12:03:36 +08005166 (uint32_t *)(base + LOGO_IMG_OFFSET + blocksize), readsize)) {
5167 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
5168 return -1;
5169 }
5170 memmove(base, (base + LOGO_IMG_OFFSET + LOGO_IMG_HEADER_SIZE), realsize);
5171 }
5172 }
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05305173 }
5174
Mao Flynn7b379f32015-04-20 00:28:30 +08005175 return 0;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05305176}
5177
Mao Flynn7b379f32015-04-20 00:28:30 +08005178int fetch_image_from_partition()
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05305179{
5180 if (target_is_emmc_boot()) {
5181 return splash_screen_mmc();
5182 } else {
5183 return splash_screen_flash();
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07005184 }
5185}
5186
Mayank Grover351a75e2017-05-30 20:06:08 +05305187void publish_getvar_multislot_vars()
5188{
5189 int i,count;
5190 static bool published = false;
5191 static char slot_count[MAX_RSP_SIZE];
5192 static struct ab_slot_info slot_info[AB_SUPPORTED_SLOTS];
5193 static char active_slot_suffix[MAX_RSP_SIZE];
5194 static char has_slot_pname[NUM_PARTITIONS][MAX_GET_VAR_NAME_SIZE];
5195 static char has_slot_reply[NUM_PARTITIONS][MAX_RSP_SIZE];
5196 const char *tmp;
5197 char tmpbuff[MAX_GET_VAR_NAME_SIZE];
Mayank Grover2bd84bb2017-06-20 11:06:07 +05305198 signed active_slt;
Mayank Grover351a75e2017-05-30 20:06:08 +05305199
5200 if (!published)
5201 {
5202 /* Update slot meta info */
5203 count = partition_fill_partition_meta(has_slot_pname, has_slot_reply,
5204 partition_get_partition_count());
5205 for(i=0; i<count; i++)
5206 {
5207 memset(tmpbuff, 0, MAX_GET_VAR_NAME_SIZE);
Mayank Grover2bd84bb2017-06-20 11:06:07 +05305208 snprintf(tmpbuff, MAX_GET_VAR_NAME_SIZE,"has-slot:%s",
5209 has_slot_pname[i]);
5210 strlcpy(has_slot_pname[i], tmpbuff, MAX_GET_VAR_NAME_SIZE);
Mayank Grover351a75e2017-05-30 20:06:08 +05305211 fastboot_publish(has_slot_pname[i], has_slot_reply[i]);
5212 }
5213
5214 for (i=0; i<AB_SUPPORTED_SLOTS; i++)
5215 {
5216 tmp = SUFFIX_SLOT(i);
Mayank Grover7b8a8f82017-10-27 13:07:59 +05305217 tmp++; // to remove "_" from slot_suffix.
Mayank Grover2bd84bb2017-06-20 11:06:07 +05305218 snprintf(slot_info[i].slot_is_unbootable, sizeof(slot_info[i].slot_is_unbootable),
5219 "slot-unbootable:%s", tmp);
5220 snprintf(slot_info[i].slot_is_active, sizeof(slot_info[i].slot_is_active),
5221 "slot-active:%s", tmp);
5222 snprintf(slot_info[i].slot_is_succesful, sizeof(slot_info[i].slot_is_succesful),
5223 "slot-success:%s", tmp);
5224 snprintf(slot_info[i].slot_retry_count, sizeof(slot_info[i].slot_retry_count),
5225 "slot-retry-count:%s", tmp);
Mayank Grover351a75e2017-05-30 20:06:08 +05305226 fastboot_publish(slot_info[i].slot_is_unbootable,
5227 slot_info[i].slot_is_unbootable_rsp);
5228 fastboot_publish(slot_info[i].slot_is_active,
5229 slot_info[i].slot_is_active_rsp);
5230 fastboot_publish(slot_info[i].slot_is_succesful,
5231 slot_info[i].slot_is_succesful_rsp);
5232 fastboot_publish(slot_info[i].slot_retry_count,
5233 slot_info[i].slot_retry_count_rsp);
5234 }
5235 fastboot_publish("current-slot", active_slot_suffix);
5236 snprintf(slot_count, sizeof(slot_count),"%d", AB_SUPPORTED_SLOTS);
5237 fastboot_publish("slot-count", slot_count);
5238 published = true;
5239 }
5240
Mayank Grover2bd84bb2017-06-20 11:06:07 +05305241 active_slt = partition_find_active_slot();
5242 if (active_slt != INVALID)
Mayank Grover7b8a8f82017-10-27 13:07:59 +05305243 {
5244 tmp = SUFFIX_SLOT(active_slt);
5245 tmp++; // to remove "_" from slot_suffix.
5246 snprintf(active_slot_suffix, sizeof(active_slot_suffix), "%s", tmp);
5247 }
Mayank Grover2bd84bb2017-06-20 11:06:07 +05305248 else
5249 strlcpy(active_slot_suffix, "INVALID", sizeof(active_slot_suffix));
5250
Mayank Grover351a75e2017-05-30 20:06:08 +05305251 /* Update partition meta information */
5252 partition_fill_slot_meta(slot_info);
5253 return;
5254}
5255
lijuang4ece1e72015-08-14 21:02:36 +08005256void get_product_name(unsigned char *buf)
5257{
5258 snprintf((char*)buf, MAX_RSP_SIZE, "%s", TARGET(BOARD));
5259 return;
5260}
5261
Mayank Grover7b8a8f82017-10-27 13:07:59 +05305262#if PRODUCT_IOT
5263void get_bootloader_version_iot(unsigned char *buf)
5264{
5265 if (buf != NULL)
5266 {
5267 strlcpy(buf, TARGET(BOARD), MAX_VERSION_LEN);
5268 strlcat(buf, "-", MAX_VERSION_LEN);
5269 strlcat(buf, PRODUCT_IOT_VERSION, MAX_VERSION_LEN);
5270 }
5271 return;
5272}
5273#endif
5274
lijuang4ece1e72015-08-14 21:02:36 +08005275void get_bootloader_version(unsigned char *buf)
5276{
jhchena8a15dd2020-04-24 15:45:57 +08005277 snprintf((char*)buf, MAX_RSP_SIZE, "%s", device.bootloader_version);
lijuang4ece1e72015-08-14 21:02:36 +08005278 return;
5279}
5280
5281void get_baseband_version(unsigned char *buf)
5282{
jhchena8a15dd2020-04-24 15:45:57 +08005283 snprintf((char*)buf, MAX_RSP_SIZE, "%s", device.radio_version);
lijuang4ece1e72015-08-14 21:02:36 +08005284 return;
5285}
5286
Monika Singh32a09f72018-03-14 13:08:29 +05305287bool is_device_locked_critical()
5288{
5289 return device.is_unlock_critical ? false:true;
5290}
5291
lijuang4ece1e72015-08-14 21:02:36 +08005292bool is_device_locked()
5293{
5294 return device.is_unlocked ? false:true;
5295}
5296
Monika Singh32a09f72018-03-14 13:08:29 +05305297bool is_verity_enforcing()
5298{
5299 return device.verity_mode ? true:false;
5300}
5301
Amol Jadi5edf3552013-07-23 14:15:34 -07005302/* register commands and variables for fastboot */
5303void aboot_fastboot_register_commands(void)
5304{
Sridhar Parasurame94e8152014-10-24 14:06:03 -07005305 int i;
lijuangf16461c2015-08-03 17:09:34 +08005306 char hw_platform_buf[MAX_RSP_SIZE];
Rahul Shaharee7baede2021-02-02 13:23:57 +05305307 VirtualAbMergeStatus SnapshotMergeStatus;
Amol Jadi5edf3552013-07-23 14:15:34 -07005308
Sridhar Parasurame94e8152014-10-24 14:06:03 -07005309 struct fastboot_cmd_desc cmd_list[] = {
lijuang511a2b52015-08-14 20:50:51 +08005310 /* By default the enabled list is empty. */
5311 {"", NULL},
5312 /* move commands enclosed within the below ifndef to here
5313 * if they need to be enabled in user build.
5314 */
Sridhar Parasurame94e8152014-10-24 14:06:03 -07005315#ifndef DISABLE_FASTBOOT_CMDS
lijuang511a2b52015-08-14 20:50:51 +08005316 /* Register the following commands only for non-user builds */
5317 {"flash:", cmd_flash},
5318 {"erase:", cmd_erase},
5319 {"boot", cmd_boot},
5320 {"continue", cmd_continue},
5321 {"reboot", cmd_reboot},
5322 {"reboot-bootloader", cmd_reboot_bootloader},
5323 {"oem unlock", cmd_oem_unlock},
5324 {"oem unlock-go", cmd_oem_unlock_go},
5325 {"oem lock", cmd_oem_lock},
5326 {"flashing unlock", cmd_oem_unlock},
5327 {"flashing lock", cmd_oem_lock},
5328 {"flashing lock_critical", cmd_flashing_lock_critical},
5329 {"flashing unlock_critical", cmd_flashing_unlock_critical},
5330 {"flashing get_unlock_ability", cmd_flashing_get_unlock_ability},
5331 {"oem device-info", cmd_oem_devinfo},
5332 {"preflash", cmd_preflash},
5333 {"oem enable-charger-screen", cmd_oem_enable_charger_screen},
5334 {"oem disable-charger-screen", cmd_oem_disable_charger_screen},
lijuanga25d8bb2015-09-16 13:11:52 +08005335 {"oem off-mode-charge", cmd_oem_off_mode_charger},
lijuang511a2b52015-08-14 20:50:51 +08005336 {"oem select-display-panel", cmd_oem_select_display_panel},
Mayank Grover6ccc1c92017-07-04 17:36:46 +05305337 {"set_active",cmd_set_active},
Mayank Grover98c4c742019-04-25 17:21:37 +05305338#if DYNAMIC_PARTITION_SUPPORT
5339 {"reboot-fastboot",cmd_reboot_fastboot},
5340 {"reboot-recovery",cmd_reboot_recovery},
5341#endif
Rahul Shaharef7a5f8b2021-02-04 16:51:36 +05305342#ifdef VIRTUAL_AB_OTA
5343 {"snapshot-update", CmdUpdateSnapshot},
5344#endif
Channagoud Kadabi736c4962015-08-21 11:56:52 -07005345#if UNITTEST_FW_SUPPORT
Channagoud Kadabid5ddf482015-09-28 10:31:35 -07005346 {"oem run-tests", cmd_oem_runtests},
Channagoud Kadabi736c4962015-08-21 11:56:52 -07005347#endif
Sridhar Parasurame94e8152014-10-24 14:06:03 -07005348#endif
lijuang511a2b52015-08-14 20:50:51 +08005349 };
Sridhar Parasurame94e8152014-10-24 14:06:03 -07005350
5351 int fastboot_cmds_count = sizeof(cmd_list)/sizeof(cmd_list[0]);
5352 for (i = 1; i < fastboot_cmds_count; i++)
5353 fastboot_register(cmd_list[i].name,cmd_list[i].cb);
5354
Amol Jadi5edf3552013-07-23 14:15:34 -07005355 /* publish variables and their values */
5356 fastboot_publish("product", TARGET(BOARD));
5357 fastboot_publish("kernel", "lk");
5358 fastboot_publish("serialno", sn_buf);
5359
Mayank Grovera64dfbe2018-05-17 14:06:34 +05305360 /*publish hw-revision major(upper 16 bits) and minor(lower 16 bits)*/
5361 snprintf(soc_version_str, MAX_RSP_SIZE, "%x", board_soc_version());
5362 fastboot_publish("hw-revision", soc_version_str);
5363
Amol Jadi5edf3552013-07-23 14:15:34 -07005364 /*
5365 * partition info is supported only for emmc partitions
5366 * Calling this for NAND prints some error messages which
5367 * is harmless but misleading. Avoid calling this for NAND
5368 * devices.
5369 */
5370 if (target_is_emmc_boot())
Mayank Grover7b8a8f82017-10-27 13:07:59 +05305371 publish_getvar_partition_info(part_info, partition_get_partition_count());
Amol Jadi5edf3552013-07-23 14:15:34 -07005372
Mayank Grover351a75e2017-05-30 20:06:08 +05305373 if (partition_multislot_is_supported())
5374 publish_getvar_multislot_vars();
5375
Amol Jadi5edf3552013-07-23 14:15:34 -07005376 /* Max download size supported */
lijuang2fbed912019-07-12 14:15:05 +08005377#if !VERIFIED_BOOT_2
Ameya Thakur11cf1a62013-08-05 12:44:48 -07005378 snprintf(max_download_size, MAX_RSP_SIZE, "\t0x%x",
5379 target_get_max_flash_size());
lijuang2fbed912019-07-12 14:15:05 +08005380#else
5381 snprintf(max_download_size, MAX_RSP_SIZE, "\t0x%x",
5382 SUB_SALT_BUFF_OFFSET(target_get_max_flash_size()));
5383#endif
5384
Amol Jadi5edf3552013-07-23 14:15:34 -07005385 fastboot_publish("max-download-size", (const char *) max_download_size);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07005386 /* Is the charger screen check enabled */
5387 snprintf(charger_screen_enabled, MAX_RSP_SIZE, "%d",
5388 device.charger_screen_enabled);
5389 fastboot_publish("charger-screen-enabled",
5390 (const char *) charger_screen_enabled);
lijuanga25d8bb2015-09-16 13:11:52 +08005391 fastboot_publish("off-mode-charge", (const char *) charger_screen_enabled);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05305392 snprintf(panel_display_mode, MAX_RSP_SIZE, "%s",
5393 device.display_panel);
5394 fastboot_publish("display-panel",
5395 (const char *) panel_display_mode);
Mukesh Ojhabb6e7f32018-03-02 15:04:06 +05305396
5397 if (target_is_emmc_boot())
5398 {
5399 mmc_blocksize = mmc_get_device_blocksize();
5400 }
5401 else
5402 {
5403 mmc_blocksize = flash_block_size();
5404 }
5405 snprintf(block_size_string, MAX_RSP_SIZE, "0x%x", mmc_blocksize);
5406 fastboot_publish("erase-block-size", (const char *) block_size_string);
5407 fastboot_publish("logical-block-size", (const char *) block_size_string);
Mayank Grover7b8a8f82017-10-27 13:07:59 +05305408#if PRODUCT_IOT
5409 get_bootloader_version_iot(&bootloader_version_string);
5410 fastboot_publish("version-bootloader", (const char *) bootloader_version_string);
5411
5412 /* Version baseband is n/a for apq iot devices */
5413 fastboot_publish("version-baseband", "N/A");
Mayank Grover7b8a8f82017-10-27 13:07:59 +05305414#else
Channagoud Kadabiaafbbfc2015-03-04 17:52:56 -08005415 fastboot_publish("version-bootloader", (const char *) device.bootloader_version);
5416 fastboot_publish("version-baseband", (const char *) device.radio_version);
Mayank Grover7b8a8f82017-10-27 13:07:59 +05305417#endif
lijuangf16461c2015-08-03 17:09:34 +08005418 fastboot_publish("secure", is_secure_boot_enable()? "yes":"no");
Mayank Grover7b8a8f82017-10-27 13:07:59 +05305419 fastboot_publish("unlocked", device.is_unlocked ? "yes":"no");
lijuangf16461c2015-08-03 17:09:34 +08005420 smem_get_hw_platform_name((unsigned char *) hw_platform_buf, sizeof(hw_platform_buf));
5421 snprintf(get_variant, MAX_RSP_SIZE, "%s %s", hw_platform_buf,
5422 target_is_emmc_boot()? "eMMC":"UFS");
5423 fastboot_publish("variant", (const char *) get_variant);
lijuang65c5a822015-08-29 16:35:36 +08005424#if CHECK_BAT_VOLTAGE
lijuang102dfa92015-10-09 18:31:03 +08005425 update_battery_status();
lijuang65c5a822015-08-29 16:35:36 +08005426 fastboot_publish("battery-voltage", (const char *) battery_voltage);
lijuang102dfa92015-10-09 18:31:03 +08005427 fastboot_publish("battery-soc-ok", (const char *) battery_soc_ok);
lijuang65c5a822015-08-29 16:35:36 +08005428#endif
Mayank Grover98c4c742019-04-25 17:21:37 +05305429 if (target_dynamic_partition_supported())
5430 fastboot_publish("is-userspace", "no");
Rahul Shaharee7baede2021-02-02 13:23:57 +05305431
5432 if (target_virtual_ab_supported()) {
5433 SnapshotMergeStatus = GetSnapshotMergeStatus ();
5434
5435 switch (SnapshotMergeStatus) {
5436 case SNAPSHOTTED:
5437 SnapshotMergeStatus = SNAPSHOTTED;
5438 break;
5439 case MERGING:
5440 SnapshotMergeStatus = MERGING;
5441 break;
5442 default:
5443 SnapshotMergeStatus = NONE_MERGE_STATUS;
5444 break;
5445 }
5446
5447 snprintf(SnapshotMergeState,
5448 strlen(VabSnapshotMergeStatus[SnapshotMergeStatus]) + 1,
5449 "%s", VabSnapshotMergeStatus[SnapshotMergeStatus]);
5450 fastboot_publish("snapshot-update-state", SnapshotMergeState);
5451 }
Amol Jadi5edf3552013-07-23 14:15:34 -07005452}
5453
Brian Swetland9c4c0752009-01-25 16:23:50 -08005454void aboot_init(const struct app_descriptor *app)
5455{
Shashank Mittal4f99a882010-02-01 13:58:50 -08005456 unsigned reboot_mode = 0;
Mayank Grover351a75e2017-05-30 20:06:08 +05305457 int boot_err_type = 0;
5458 int boot_slot = INVALID;
jessicatseng43b1e352020-05-26 13:17:56 +08005459 int vbat = 0;
5460 char boot_vbat[MAX_RSP_SIZE];
Chandan Uddarajubedca152010-06-02 23:05:15 -07005461
lijuang39831732016-01-08 17:49:02 +08005462 /* Initialise wdog to catch early lk crashes */
5463#if WDOG_SUPPORT
5464 msm_wdog_init();
5465#endif
5466
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07005467 /* Setup page size information for nv storage */
Shashank Mittald8c42bf2010-06-09 15:44:28 -07005468 if (target_is_emmc_boot())
5469 {
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07005470 page_size = mmc_page_size();
Shashank Mittald8c42bf2010-06-09 15:44:28 -07005471 page_mask = page_size - 1;
Gaurav Nebhwani4d2389c2016-03-17 21:10:05 +05305472 mmc_blocksize = mmc_get_device_blocksize();
5473 mmc_blocksize_mask = mmc_blocksize - 1;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07005474 }
5475 else
5476 {
5477 page_size = flash_page_size();
5478 page_mask = page_size - 1;
5479 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07005480 ASSERT((MEMBASE + MEMSIZE) > MEMBASE);
5481
Ameya Thakur11cf1a62013-08-05 12:44:48 -07005482 read_device_info(&device);
vijay kumarc65876c2015-04-24 13:29:16 +05305483 read_allow_oem_unlock(&device);
Shashank Mittal162244e2011-08-08 19:01:25 -07005484
Mayank Grover351a75e2017-05-30 20:06:08 +05305485 /* Detect multi-slot support */
5486 if (partition_multislot_is_supported())
5487 {
5488 boot_slot = partition_find_active_slot();
5489 if (boot_slot == INVALID)
5490 {
5491 boot_into_fastboot = true;
5492 dprintf(INFO, "Active Slot: (INVALID)\n");
5493 }
5494 else
5495 {
5496 /* Setting the state of system to boot active slot */
5497 partition_mark_active_slot(boot_slot);
5498 dprintf(INFO, "Active Slot: (%s)\n", SUFFIX_SLOT(boot_slot));
5499 }
5500 }
5501
Greg Griscod6250552011-06-29 14:40:23 -07005502 target_serialno((unsigned char *) sn_buf);
Ajay Dudanib06c05f2011-05-12 14:46:10 -07005503 dprintf(SPEW,"serial number: %s\n",sn_buf);
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -08005504
Dhaval Patel223ec952013-07-18 14:49:44 -07005505 memset(display_panel_buf, '\0', MAX_PANEL_BUF_SIZE);
5506
Matthew Qindefd5562014-07-11 18:02:40 +08005507 /*
5508 * Check power off reason if user force reset,
5509 * if yes phone will do normal boot.
5510 */
5511 if (is_user_force_reset())
5512 goto normal_boot;
5513
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07005514 /* Check if we should do something other than booting up */
Ameya Thakur0b9c2442013-05-10 13:22:56 -07005515 if (keys_get_state(KEY_VOLUMEUP) && keys_get_state(KEY_VOLUMEDOWN))
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07005516 {
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03005517 dprintf(ALWAYS,"dload mode key sequence detected\n");
lijuang395b5e62015-11-19 17:39:44 +08005518 reboot_device(EMERGENCY_DLOAD);
5519 dprintf(CRITICAL,"Failed to reboot into dload mode\n");
5520
Ameya Thakur0b9c2442013-05-10 13:22:56 -07005521 boot_into_fastboot = true;
5522 }
5523 if (!boot_into_fastboot)
5524 {
5525 if (keys_get_state(KEY_HOME) || keys_get_state(KEY_VOLUMEUP))
5526 boot_into_recovery = 1;
5527 if (!boot_into_recovery &&
5528 (keys_get_state(KEY_BACK) || keys_get_state(KEY_VOLUMEDOWN)))
Pavel Nedev5d91d412013-04-29 11:34:24 +03005529 boot_into_fastboot = true;
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07005530 }
Shashank Mittald8c42bf2010-06-09 15:44:28 -07005531 #if NO_KEYPAD_DRIVER
Kinson Chik0b1c8162011-08-31 16:31:57 -07005532 if (fastboot_trigger())
Pavel Nedev5d91d412013-04-29 11:34:24 +03005533 boot_into_fastboot = true;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07005534 #endif
Chandan Uddarajubedca152010-06-02 23:05:15 -07005535
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07005536#if USE_PON_REBOOT_REG
5537 reboot_mode = check_hard_reboot_mode();
5538#else
Ajay Dudani77421292010-10-27 19:34:06 -07005539 reboot_mode = check_reboot_mode();
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07005540#endif
5541 if (reboot_mode == RECOVERY_MODE)
5542 {
Ajay Dudani77421292010-10-27 19:34:06 -07005543 boot_into_recovery = 1;
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07005544 }
5545 else if(reboot_mode == FASTBOOT_MODE)
5546 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03005547 boot_into_fastboot = true;
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07005548 }
5549 else if(reboot_mode == ALARM_BOOT)
5550 {
Matthew Qind886f3c2014-01-17 16:52:01 +08005551 boot_reason_alarm = true;
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07005552 }
Monika Singhb0db4b82018-09-26 12:18:02 +05305553#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover912eaa62017-10-26 12:08:53 +05305554 else if (VB_M <= target_get_vb_version())
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07005555 {
Mayank Grover889be1b2017-09-12 20:12:23 +05305556 if (reboot_mode == DM_VERITY_ENFORCING)
5557 {
5558 device.verity_mode = 1;
5559 write_device_info(&device);
5560 }
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07005561#if ENABLE_VB_ATTEST
Mayank Grover889be1b2017-09-12 20:12:23 +05305562 else if (reboot_mode == DM_VERITY_EIO)
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07005563#else
Mayank Grover889be1b2017-09-12 20:12:23 +05305564 else if (reboot_mode == DM_VERITY_LOGGING)
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07005565#endif
Mayank Grover889be1b2017-09-12 20:12:23 +05305566 {
5567 device.verity_mode = 0;
5568 write_device_info(&device);
5569 }
5570 else if (reboot_mode == DM_VERITY_KEYSCLEAR)
5571 {
5572 if(send_delete_keys_to_tz())
5573 ASSERT(0);
5574 }
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07005575 }
Parth Dixitddbc7352015-10-18 03:13:31 +05305576#endif
Ajay Dudani77421292010-10-27 19:34:06 -07005577
jialongjhan2769d202020-05-22 18:25:56 +08005578 /* Display splash screen if enabled */
5579#if DISPLAY_SPLASH_SCREEN
5580#if NO_ALARM_DISPLAY
5581 if (!check_alarm_boot()) {
5582#endif
5583 dprintf(SPEW, "Display Init: Start\n");
5584#if DISPLAY_HDMI_PRIMARY
5585 if (!strlen(device.display_panel))
5586 strlcpy(device.display_panel, DISPLAY_PANEL_HDMI,
5587 sizeof(device.display_panel));
5588#endif
5589#if ENABLE_WBC
5590 /* Wait if the display shutdown is in progress */
5591 while(pm_app_display_shutdown_in_prgs());
5592 if (!pm_appsbl_display_init_done())
5593 target_display_init(device.display_panel);
5594 else
5595 display_image_on_screen();
5596#else
5597 target_display_init(device.display_panel);
5598#endif
5599 dprintf(SPEW, "Display Init: Done\n");
5600#if NO_ALARM_DISPLAY
5601 }
5602#endif
5603#endif
5604
5605
Matthew Qindefd5562014-07-11 18:02:40 +08005606normal_boot:
Pavel Nedev5d91d412013-04-29 11:34:24 +03005607 if (!boot_into_fastboot)
Shashank Mittald8c42bf2010-06-09 15:44:28 -07005608 {
jessicatseng43b1e352020-05-26 13:17:56 +08005609 if(!target_pause_for_battery_charge())
5610 {
5611 vbat = target_get_battery_voltage();
5612 snprintf(boot_vbat, MAX_RSP_SIZE, "%d", vbat);
5613 dprintf(CRITICAL,"battery_voltage: %s\n", boot_vbat);
5614 if(vbat < 3500000)
5615 {
5616 display_lowbattery_image_on_screen();
5617 //[Arima][8901][JialongJhan] Command mode reflash screen when low battery logo shown 20190516 Start
5618 msm_display_flush();
5619 //[Arima][8901][JialongJhan] Command mode reflash screen when low battery logo shown 20190516 End
5620 dprintf(CRITICAL,"Low battery, cannot boot up...\n");
5621 mdelay(3000);
5622 shutdown_device();
5623 }
5624 }
5625
Pavel Nedev5d91d412013-04-29 11:34:24 +03005626 if (target_is_emmc_boot())
Shashank Mittala0032282011-08-26 14:50:11 -07005627 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03005628 if(emmc_recovery_init())
5629 dprintf(ALWAYS,"error in emmc_recovery_init\n");
5630 if(target_use_signed_kernel())
Shashank Mittala0032282011-08-26 14:50:11 -07005631 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03005632 if((device.is_unlocked) || (device.is_tampered))
5633 {
5634 #ifdef TZ_TAMPER_FUSE
Monika Singh98257462018-06-06 11:28:49 +05305635 set_tamper_fuse_cmd(HLOS_IMG_TAMPER_FUSE);
Pavel Nedev5d91d412013-04-29 11:34:24 +03005636 #endif
5637 #if USE_PCOM_SECBOOT
5638 set_tamper_flag(device.is_tampered);
5639 #endif
5640 }
Shashank Mittala0032282011-08-26 14:50:11 -07005641 }
Amit Blay6281ebc2015-01-11 14:44:08 +02005642
Mayank Grover351a75e2017-05-30 20:06:08 +05305643retry_boot:
5644 /* Trying to boot active partition */
5645 if (partition_multislot_is_supported())
5646 {
5647 boot_slot = partition_find_boot_slot();
5648 partition_mark_active_slot(boot_slot);
5649 if (boot_slot == INVALID)
5650 goto fastboot;
5651 }
5652
5653 boot_err_type = boot_linux_from_mmc();
5654 switch (boot_err_type)
5655 {
5656 case ERR_INVALID_PAGE_SIZE:
5657 case ERR_DT_PARSE:
5658 case ERR_ABOOT_ADDR_OVERLAP:
Mayank Grover10cabbe2017-10-30 19:11:05 +05305659 case ERR_INVALID_BOOT_MAGIC:
Mayank Grover351a75e2017-05-30 20:06:08 +05305660 if(partition_multislot_is_supported())
Mayank Grover10cabbe2017-10-30 19:11:05 +05305661 {
5662 /*
5663 * Deactivate current slot, as it failed to
5664 * boot, and retry next slot.
5665 */
5666 partition_deactivate_slot(boot_slot);
Mayank Grover351a75e2017-05-30 20:06:08 +05305667 goto retry_boot;
Mayank Grover10cabbe2017-10-30 19:11:05 +05305668 }
Mayank Grover351a75e2017-05-30 20:06:08 +05305669 else
5670 break;
Mayank Grover351a75e2017-05-30 20:06:08 +05305671 default:
5672 break;
5673 /* going to fastboot menu */
5674 }
Shashank Mittala0032282011-08-26 14:50:11 -07005675 }
Pavel Nedev5d91d412013-04-29 11:34:24 +03005676 else
5677 {
5678 recovery_init();
5679 #if USE_PCOM_SECBOOT
5680 if((device.is_unlocked) || (device.is_tampered))
5681 set_tamper_flag(device.is_tampered);
5682 #endif
5683 boot_linux_from_flash();
5684 }
5685 dprintf(CRITICAL, "ERROR: Could not do normal boot. Reverting "
5686 "to fastboot mode.\n");
Shashank Mittald8c42bf2010-06-09 15:44:28 -07005687 }
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07005688
Mayank Grover351a75e2017-05-30 20:06:08 +05305689fastboot:
jialongjhan2769d202020-05-22 18:25:56 +08005690
5691 mdelay(1000);
5692
Amol Jadi5edf3552013-07-23 14:15:34 -07005693 /* We are here means regular boot did not happen. Start fastboot. */
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07005694
Amol Jadi5edf3552013-07-23 14:15:34 -07005695 /* register aboot specific fastboot commands */
5696 aboot_fastboot_register_commands();
Amol Jadi57abe4c2011-05-24 15:47:27 -07005697
Amol Jadi5edf3552013-07-23 14:15:34 -07005698 /* dump partition table for debug info */
Kinson Chikf1a43512011-07-14 11:28:39 -07005699 partition_dump();
Amol Jadi5edf3552013-07-23 14:15:34 -07005700
5701 /* initialize and start fastboot */
Saranya Chidura41b34532018-10-16 12:29:52 +05305702#if !VERIFIED_BOOT_2
Amol Jadi5edf3552013-07-23 14:15:34 -07005703 fastboot_init(target_get_scratch_address(), target_get_max_flash_size());
Saranya Chidura41b34532018-10-16 12:29:52 +05305704#else
5705 /* Add salt buffer offset at start of image address to copy VB salt */
5706 fastboot_init(ADD_SALT_BUFF_OFFSET(target_get_scratch_address()),
5707 SUB_SALT_BUFF_OFFSET(target_get_max_flash_size()));
5708#endif
lijuang4ece1e72015-08-14 21:02:36 +08005709#if FBCON_DISPLAY_MSG
lijuang9a7d3b92015-11-30 14:41:24 +08005710 display_fastboot_menu();
lijuang4ece1e72015-08-14 21:02:36 +08005711#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08005712}
5713
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07005714uint32_t get_page_size()
5715{
5716 return page_size;
5717}
5718
Amir Samuelov57a6fa22013-06-05 16:36:43 +03005719/*
5720 * Calculated and save hash (SHA256) for non-signed boot image.
5721 *
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07005722 * @param image_addr - Boot image address
5723 * @param image_size - Size of the boot image
Amir Samuelov57a6fa22013-06-05 16:36:43 +03005724 *
5725 * @return int - 0 on success, negative value on failure.
5726 */
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07005727static int aboot_save_boot_hash_mmc(uint32_t image_addr, uint32_t image_size)
Amir Samuelov57a6fa22013-06-05 16:36:43 +03005728{
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07005729 unsigned int digest[8];
5730#if IMAGE_VERIF_ALGO_SHA1
5731 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA1;
5732#else
5733 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA256;
5734#endif
Amir Samuelov57a6fa22013-06-05 16:36:43 +03005735
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07005736 target_crypto_init_params();
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -08005737 hash_find((unsigned char *) image_addr, image_size, (unsigned char *)&digest, auth_algo);
Amir Samuelov57a6fa22013-06-05 16:36:43 +03005738
5739 save_kernel_hash_cmd(digest);
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07005740 dprintf(INFO, "aboot_save_boot_hash_mmc: imagesize_actual size %d bytes.\n", (int) image_size);
Amir Samuelov57a6fa22013-06-05 16:36:43 +03005741
5742 return 0;
5743}
5744
Mayank Grover351a75e2017-05-30 20:06:08 +05305745
Brian Swetland9c4c0752009-01-25 16:23:50 -08005746APP_START(aboot)
5747 .init = aboot_init,
5748APP_END