blob: cc7a90b662ee2cdc9c5badf85186c61aa21e84e9 [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
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700218#if VERIFIED_BOOT
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700219static const char *verity_mode = " androidboot.veritymode=";
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700220static const char *verified_state= " androidboot.verifiedbootstate=";
Parth Dixita5715a02015-10-29 12:25:10 +0530221static const char *keymaster_v1= " androidboot.keymaster=1";
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700222//indexed based on enum values, green is 0 by default
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700223
224struct verified_boot_verity_mode vbvm[] =
225{
Channagoud Kadabi86b0c112016-03-16 19:23:16 -0700226#if ENABLE_VB_ATTEST
227 {false, "eio"},
228#else
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700229 {false, "logging"},
Channagoud Kadabi86b0c112016-03-16 19:23:16 -0700230#endif
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700231 {true, "enforcing"},
232};
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700233struct verified_boot_state_name vbsn[] =
234{
235 {GREEN, "green"},
236 {ORANGE, "orange"},
237 {YELLOW,"yellow"},
238 {RED,"red" },
239};
240#endif
Channagoud Kadabi86b0c112016-03-16 19:23:16 -0700241/*As per spec delay wait time before shutdown in Red state*/
242#define DELAY_WAIT 30000
Ashish Bhimanpalliwar8cb34fd2020-11-12 19:26:41 +0530243static unsigned page_size = BOARD_KERNEL_PAGESIZE;
244
245uint32_t kernel_hdr_page_size()
246{
247 return page_size;
248}
249
250
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700251static unsigned page_mask = 0;
Gaurav Nebhwani4d2389c2016-03-17 21:10:05 +0530252static unsigned mmc_blocksize = 0;
253static unsigned mmc_blocksize_mask = 0;
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700254static char ffbm_mode_string[FFBM_MODE_BUF_SIZE];
255static bool boot_into_ffbm;
vijay kumar870515d2015-08-31 16:37:24 +0530256static char *target_boot_params = NULL;
Matthew Qind886f3c2014-01-17 16:52:01 +0800257static bool boot_reason_alarm;
Channagoud Kadabi80a182b2015-03-11 17:04:23 -0700258static bool devinfo_present = true;
Channagoud Kadabi736c4962015-08-21 11:56:52 -0700259bool boot_into_fastboot = false;
Parth Dixit4097b622016-03-15 14:42:27 +0530260static uint32_t dt_size = 0;
Monika Singh292b3e92018-03-17 22:40:23 +0530261static char *vbcmdline;
262static bootinfo info = {0};
Mayank Grovere1ab96c2018-09-04 20:31:31 +0530263static void *recovery_dtbo_buf = NULL;
264static uint32_t recovery_dtbo_size = 0;
265
Shashank Mittalcd98d472011-08-02 14:29:24 -0700266/* Assuming unauthorized kernel image by default */
267static int auth_kernel_img = 0;
Monika Singh292b3e92018-03-17 22:40:23 +0530268static device_info device = {DEVICE_MAGIC,0,0,0,0,{0},{0},{0},1,{0},0,{0}};
269
vijay kumarc65876c2015-04-24 13:29:16 +0530270static bool is_allow_unlock = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -0700271
vijay kumarca2e6812015-07-08 20:28:25 +0530272static char frp_ptns[2][8] = {"config","frp"};
273
lijuang511a2b52015-08-14 20:50:51 +0800274static const char *critical_flash_allowed_ptn[] = {
275 "aboot",
276 "rpm",
277 "tz",
278 "sbl",
279 "sdi",
280 "sbl1",
281 "xbl",
282 "hyp",
283 "pmic",
284 "bootloader",
285 "devinfo",
286 "partition"};
287
Rahul Shaharee7baede2021-02-02 13:23:57 +0530288static const char *VabSnapshotMergeStatus[] = {
289 "none",
290 "unknown",
291 "snapshotted",
292 "merging",
293 "cancelled"};
294
Dima Zavin42168f22009-01-30 11:52:22 -0800295struct atag_ptbl_entry
296{
297 char name[16];
298 unsigned offset;
299 unsigned size;
300 unsigned flags;
301};
302
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700303/*
304 * Partition info, required to be published
305 * for fastboot
306 */
307struct getvar_partition_info {
Mayank Grover7b8a8f82017-10-27 13:07:59 +0530308 char part_name[MAX_GPT_NAME_SIZE]; /* Partition name */
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700309 char getvar_size[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for size */
310 char getvar_type[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for type */
311 char size_response[MAX_RSP_SIZE]; /* fastboot response for size */
312 char type_response[MAX_RSP_SIZE]; /* fastboot response for type */
313};
314
315/*
Mayank Grover7b8a8f82017-10-27 13:07:59 +0530316 * Update the part_type_known for known paritions types.
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700317 */
Mayank Groverd38fe012018-03-13 15:33:16 +0530318#define RAW_STR "raw"
Mayank Grover52cd10a2018-03-15 12:57:54 +0530319#define EXT_STR "ext4"
320#define F2FS_STR "f2fs"
321
322#define FS_SUPERBLOCK_OFFSET 0x400
323#define EXT_MAGIC 0xEF53
324#define EXT_MAGIC_OFFSET_SB 0x38
325#define F2FS_MAGIC 0xF2F52010 // F2FS Magic Number
326#define F2FS_MAGIC_OFFSET_SB 0x0
327
328typedef enum fs_signature_type {
329 EXT_FS_SIGNATURE = 1,
330 EXT_F2FS_SIGNATURE = 2,
331 NO_FS = -1
332} fs_signature_type;
333
Mayank Grover7b8a8f82017-10-27 13:07:59 +0530334struct getvar_partition_info part_info[NUM_PARTITIONS];
335struct getvar_partition_info part_type_known[] =
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700336{
Mayank Grover49920212018-02-09 18:15:55 +0530337 { "system" , "partition-size:", "partition-type:", "", "ext4" },
338 { "userdata" , "partition-size:", "partition-type:", "", "ext4" },
339 { "cache" , "partition-size:", "partition-type:", "", "ext4" },
340 { "recoveryfs" , "partition-size:", "partition-type:", "", "ext4" },
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700341};
342
343char max_download_size[MAX_RSP_SIZE];
Ameya Thakur11cf1a62013-08-05 12:44:48 -0700344char charger_screen_enabled[MAX_RSP_SIZE];
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800345char sn_buf[13];
Dhaval Patel223ec952013-07-18 14:49:44 -0700346char display_panel_buf[MAX_PANEL_BUF_SIZE];
Unnati Gandhi62c8ab82014-01-24 11:01:01 +0530347char panel_display_mode[MAX_RSP_SIZE];
Mayank Grovera64dfbe2018-05-17 14:06:34 +0530348char soc_version_str[MAX_RSP_SIZE];
Mayank Grover7b8a8f82017-10-27 13:07:59 +0530349char block_size_string[MAX_RSP_SIZE];
Rahul Shaharee7baede2021-02-02 13:23:57 +0530350static char SnapshotMergeState[MAX_RSP_SIZE];
Mukesh Ojhabb6e7f32018-03-02 15:04:06 +0530351#if PRODUCT_IOT
Mayank Grover7b8a8f82017-10-27 13:07:59 +0530352
353/* For IOT we are using custom version */
354#define PRODUCT_IOT_VERSION "IOT001"
355char bootloader_version_string[MAX_RSP_SIZE];
356#endif
lijuang102dfa92015-10-09 18:31:03 +0800357
358#if CHECK_BAT_VOLTAGE
lijuang65c5a822015-08-29 16:35:36 +0800359char battery_voltage[MAX_RSP_SIZE];
lijuang102dfa92015-10-09 18:31:03 +0800360char battery_soc_ok [MAX_RSP_SIZE];
361#endif
362
lijuangf16461c2015-08-03 17:09:34 +0800363char get_variant[MAX_RSP_SIZE];
Greg Griscod6250552011-06-29 14:40:23 -0700364
Greg Griscod2471ef2011-07-14 13:00:42 -0700365extern int emmc_recovery_init(void);
366
Kinson Chik0b1c8162011-08-31 16:31:57 -0700367#if NO_KEYPAD_DRIVER
368extern int fastboot_trigger(void);
369#endif
Greg Griscod2471ef2011-07-14 13:00:42 -0700370
Mayank Grovere1ab96c2018-09-04 20:31:31 +0530371static void update_ker_tags_rdisk_addr(boot_img_hdr *hdr, bool is_arm64)
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700372{
373 /* overwrite the destination of specified for the project */
Channagoud Kadabi7042fa32013-04-26 16:44:14 -0700374#ifdef ABOOT_IGNORE_BOOT_HEADER_ADDRS
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800375 if (is_arm64)
376 hdr->kernel_addr = ABOOT_FORCE_KERNEL64_ADDR;
377 else
378 hdr->kernel_addr = ABOOT_FORCE_KERNEL_ADDR;
Channagoud Kadabi7042fa32013-04-26 16:44:14 -0700379 hdr->ramdisk_addr = ABOOT_FORCE_RAMDISK_ADDR;
380 hdr->tags_addr = ABOOT_FORCE_TAGS_ADDR;
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700381#endif
382}
383
Dima Zavin42168f22009-01-30 11:52:22 -0800384static void ptentry_to_tag(unsigned **ptr, struct ptentry *ptn)
385{
386 struct atag_ptbl_entry atag_ptn;
387
388 memcpy(atag_ptn.name, ptn->name, 16);
389 atag_ptn.name[15] = '\0';
390 atag_ptn.offset = ptn->start;
391 atag_ptn.size = ptn->length;
392 atag_ptn.flags = ptn->flags;
393 memcpy(*ptr, &atag_ptn, sizeof(struct atag_ptbl_entry));
394 *ptr += sizeof(struct atag_ptbl_entry) / sizeof(unsigned);
395}
Mayank Grover9df84c02018-08-30 15:46:35 +0530396#ifdef VERIFIED_BOOT_2
397void load_vbmeta_image(void **vbmeta_image_buf, uint32_t *vbmeta_image_sz)
398{
399 int index = 0;
400 char *vbm_img_buf = NULL;
401 unsigned long long ptn = 0;
402 unsigned long long ptn_size = 0;
403
404 /* Immediately return if dtbo is not supported */
405 index = partition_get_index("vbmeta");
406 ptn = partition_get_offset(index);
407 if(!ptn)
408 {
409 dprintf(CRITICAL, "ERROR: vbmeta partition not found.\n");
410 return;
411 }
412
413 ptn_size = partition_get_size(index);
414 if (ptn_size > MAX_SUPPORTED_VBMETA_IMG_BUF)
415 {
416 dprintf(CRITICAL, "ERROR: vbmeta parition size is greater than supported.\n");
417 return;
418 }
419
420 vbm_img_buf = (char *)memalign(CACHE_LINE, ROUNDUP((uint32_t)ptn_size, CACHE_LINE));
421 if (!vbm_img_buf)
422 {
423 dprintf(CRITICAL, "ERROR: vbmeta unable to locate buffer\n");
424 return;
425 }
426
427 mmc_set_lun(partition_get_lun(index));
428 if (mmc_read(ptn, (uint32_t *)vbm_img_buf, (uint32_t)ptn_size))
429 {
430 dprintf(CRITICAL, "ERROR: vbmeta read failure\n");
431 free(vbm_img_buf);
432 return;
433 }
434
435 *vbmeta_image_buf = vbm_img_buf;
436 *vbmeta_image_sz = (uint32_t)ptn_size;
437 return;
438}
439#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -0800440
lijuang102dfa92015-10-09 18:31:03 +0800441#if CHECK_BAT_VOLTAGE
442void update_battery_status(void)
443{
444 snprintf(battery_voltage,MAX_RSP_SIZE, "%d",target_get_battery_voltage());
445 snprintf(battery_soc_ok ,MAX_RSP_SIZE, "%s",target_battery_soc_ok()? "yes":"no");
446}
447#endif
448
Neeti Desaie245d492012-06-01 12:52:13 -0700449unsigned char *update_cmdline(const char * cmdline)
Brian Swetland9c4c0752009-01-25 16:23:50 -0800450{
David Ng183a7422009-12-07 14:55:21 -0800451 int cmdline_len = 0;
452 int have_cmdline = 0;
Amol Jadi168b7712012-03-06 16:15:00 -0800453 unsigned char *cmdline_final = NULL;
Neeti Desaie245d492012-06-01 12:52:13 -0700454 int pause_at_bootup = 0;
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800455 bool warm_boot = false;
Pavel Nedev5614d222013-06-17 18:01:02 +0300456 bool gpt_exists = partition_gpt_exists();
Joonwoo Park61112782013-10-02 19:50:39 -0700457 int have_target_boot_params = 0;
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700458 char *boot_dev_buf = NULL;
Monika Singh292b3e92018-03-17 22:40:23 +0530459#ifdef MDTP_SUPPORT
Mayank Grover351a75e2017-05-30 20:06:08 +0530460 bool is_mdtp_activated = 0;
Monika Singh292b3e92018-03-17 22:40:23 +0530461#endif
Mayank Grover351a75e2017-05-30 20:06:08 +0530462 int current_active_slot = INVALID;
Mayank Grover3804be72017-06-22 11:59:23 +0530463 int system_ptn_index = -1;
464 unsigned int lun = 0;
465 char lun_char_base = 'a';
Sourabh Banerjee386b1322018-02-27 09:37:28 +0530466#if VERITY_LE
467 int syspath_buflen = strlen(verity_dev)
468 + strlen(verity_system_part) + (sizeof(char) * 2) + 2
469 + strlen(verity_params) + sizeof(int) + 2;
470#else
lijuang83ef4b22018-08-23 11:01:55 +0800471 int syspath_buflen = strlen(sys_path) + sizeof(int) + 2; /*allocate buflen for largest possible string*/
472 char dtbo_idx_str[MAX_DTBO_IDX_STR] = "\0";
473 int dtbo_idx = INVALID_PTN;
Parth Dixit1375d9e2019-07-08 20:56:13 +0530474 char dtb_idx_str[MAX_DTB_IDX_STR] = "\0";
475 int dtb_idx = INVALID_PTN;
Sourabh Banerjee386b1322018-02-27 09:37:28 +0530476#endif
Mayank Grover3804be72017-06-22 11:59:23 +0530477 char syspath_buf[syspath_buflen];
Mayank Grover466d6562018-05-10 14:52:20 +0530478#if HIBERNATION_SUPPORT
479 int resume_buflen = strlen(resume) + sizeof(int) + 2;
480 char resume_buf[resume_buflen];
481 int swap_ptn_index = INVALID_PTN;
482#endif
483
Mayank Grover889be1b2017-09-12 20:12:23 +0530484#if VERIFIED_BOOT
485 uint32_t boot_state = RED;
486#endif
Hareesh Gauthamabf1e1e2017-04-12 18:09:24 +0530487
488#if USE_LE_SYSTEMD
489 is_systemd_present=true;
490#endif
491
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700492#if VERIFIED_BOOT
Mayank Grover912eaa62017-10-26 12:08:53 +0530493 if (VB_M <= target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +0530494 {
495 boot_state = boot_verify_get_state();
496 }
Parth Dixitddbc7352015-10-18 03:13:31 +0530497#endif
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700498
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200499#ifdef MDTP_SUPPORT
500 mdtp_activated(&is_mdtp_activated);
501#endif /* MDTP_SUPPORT */
Dima Zavin42168f22009-01-30 11:52:22 -0800502
Brian Swetland9c4c0752009-01-25 16:23:50 -0800503 if (cmdline && cmdline[0]) {
David Ng183a7422009-12-07 14:55:21 -0800504 cmdline_len = strlen(cmdline);
505 have_cmdline = 1;
506 }
507 if (target_is_emmc_boot()) {
508 cmdline_len += strlen(emmc_cmdline);
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700509 boot_dev_buf = (char *) malloc(sizeof(char) * BOOT_DEV_MAX_LEN);
Mayank Groverb716edf2019-05-08 16:06:55 +0530510 if (!boot_dev_buf) {
511 dprintf(CRITICAL, "ERROR: Failed to allocate boot_dev_buf\n");
512 } else {
513 platform_boot_dev_cmdline(boot_dev_buf);
514#if USE_BOOTDEV_CMDLINE
515 cmdline_len += strlen(boot_dev_buf);
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700516#endif
Mayank Groverb716edf2019-05-08 16:06:55 +0530517 if (target_dynamic_partition_supported()) {
518 cmdline_len += strlen(dynamic_bootdev_cmdline);
519 cmdline_len += strlen(boot_dev_buf);
520 }
521 }
David Ng183a7422009-12-07 14:55:21 -0800522 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800523
524 cmdline_len += strlen(usb_sn_cmdline);
525 cmdline_len += strlen(sn_buf);
526
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700527#if VERIFIED_BOOT
Mayank Grover912eaa62017-10-26 12:08:53 +0530528 if (VB_M <= target_get_vb_version())
Sridhar Parasuram7d8bb9a2015-09-23 18:21:19 -0700529 {
Mayank Grover889be1b2017-09-12 20:12:23 +0530530 cmdline_len += strlen(verified_state) + strlen(vbsn[boot_state].name);
531 if ((device.verity_mode != 0 ) && (device.verity_mode != 1))
532 {
533 dprintf(CRITICAL, "Devinfo paritition possibly corrupted!!!. Please erase devinfo partition to continue booting\n");
534 ASSERT(0);
535 }
536 cmdline_len += strlen(verity_mode) + strlen(vbvm[device.verity_mode].name);
537 cmdline_len += strlen(keymaster_v1);
Sridhar Parasuram7d8bb9a2015-09-23 18:21:19 -0700538 }
Parth Dixitddbc7352015-10-18 03:13:31 +0530539#endif
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700540
Monika Singh292b3e92018-03-17 22:40:23 +0530541
542 if (vbcmdline != NULL) {
543 dprintf(DEBUG, "UpdateCmdLine vbcmdline present len %d\n",
544 strlen(vbcmdline));
545 cmdline_len += strlen(vbcmdline);
546 }
547
Pavel Nedev5614d222013-06-17 18:01:02 +0300548 if (boot_into_recovery && gpt_exists)
549 cmdline_len += strlen(secondary_gpt_enable);
550
Monika Singh292b3e92018-03-17 22:40:23 +0530551#ifdef MDTP_SUPPORT
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200552 if(is_mdtp_activated)
553 cmdline_len += strlen(mdtp_activated_flag);
Monika Singh292b3e92018-03-17 22:40:23 +0530554#endif
Pavel Nedev328ac822013-04-05 15:25:11 +0300555 if (boot_into_ffbm) {
556 cmdline_len += strlen(androidboot_mode);
Hareesh Gauthamabf1e1e2017-04-12 18:09:24 +0530557
558 if(is_systemd_present)
559 cmdline_len += strlen(systemd_ffbm_mode);
560
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700561 cmdline_len += strlen(ffbm_mode_string);
Pavel Nedev898298c2013-02-27 12:36:09 -0800562 /* reduce kernel console messages to speed-up boot */
563 cmdline_len += strlen(loglevel);
Matthew Qind886f3c2014-01-17 16:52:01 +0800564 } else if (boot_reason_alarm) {
565 cmdline_len += strlen(alarmboot_cmdline);
Zhenhua Huang431dafa2015-06-30 16:13:37 +0800566 } else if ((target_build_variant_user() || device.charger_screen_enabled)
lijuang266b17d2018-08-17 18:53:42 +0800567 && target_pause_for_battery_charge() && !boot_into_recovery) {
David Ngf773dde2010-07-26 19:55:08 -0700568 pause_at_bootup = 1;
569 cmdline_len += strlen(battchg_pause);
570 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800571
Shashank Mittalcd98d472011-08-02 14:29:24 -0700572 if(target_use_signed_kernel() && auth_kernel_img) {
573 cmdline_len += strlen(auth_kernel);
574 }
575
Joonwoo Park61112782013-10-02 19:50:39 -0700576 if (get_target_boot_params(cmdline, boot_into_recovery ? "recoveryfs" :
577 "system",
vijay kumar870515d2015-08-31 16:37:24 +0530578 &target_boot_params) == 0) {
Joonwoo Park61112782013-10-02 19:50:39 -0700579 have_target_boot_params = 1;
580 cmdline_len += strlen(target_boot_params);
581 }
582
Ajay Dudanid04110c2011-01-17 23:55:07 -0800583 /* Determine correct androidboot.baseband to use */
584 switch(target_baseband())
585 {
586 case BASEBAND_APQ:
587 cmdline_len += strlen(baseband_apq);
588 break;
589
590 case BASEBAND_MSM:
591 cmdline_len += strlen(baseband_msm);
592 break;
593
594 case BASEBAND_CSFB:
595 cmdline_len += strlen(baseband_csfb);
596 break;
597
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800598 case BASEBAND_SVLTE2A:
599 cmdline_len += strlen(baseband_svlte2a);
Ajay Dudanid04110c2011-01-17 23:55:07 -0800600 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700601
602 case BASEBAND_MDM:
603 cmdline_len += strlen(baseband_mdm);
604 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700605
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800606 case BASEBAND_MDM2:
607 cmdline_len += strlen(baseband_mdm2);
608 break;
609
Amol Jadi5c61a952012-05-04 17:05:35 -0700610 case BASEBAND_SGLTE:
611 cmdline_len += strlen(baseband_sglte);
612 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530613
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800614 case BASEBAND_SGLTE2:
615 cmdline_len += strlen(baseband_sglte2);
616 break;
617
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530618 case BASEBAND_DSDA:
619 cmdline_len += strlen(baseband_dsda);
620 break;
Amol Jadi2a15a272013-01-22 12:03:36 -0800621
622 case BASEBAND_DSDA2:
623 cmdline_len += strlen(baseband_dsda2);
624 break;
Vijay Kumar Pendotib228cfc2016-06-13 20:15:23 +0530625 case BASEBAND_APQ_NOWGR:
626 cmdline_len += strlen(baseband_apq_nowgr);
627 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800628 }
629
Vladislav Levenetz3cd45242017-04-05 08:58:07 +0300630#if ENABLE_DISPLAY
Lijuan Gao4a5b8322014-07-24 10:38:42 +0800631 if (cmdline) {
632 if ((strstr(cmdline, DISPLAY_DEFAULT_PREFIX) == NULL) &&
Padmanabhan Komandurubccbcdc2015-06-30 16:19:24 +0530633 target_display_panel_node(display_panel_buf,
634 MAX_PANEL_BUF_SIZE) &&
Lijuan Gao4a5b8322014-07-24 10:38:42 +0800635 strlen(display_panel_buf)) {
636 cmdline_len += strlen(display_panel_buf);
637 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700638 }
Vladislav Levenetz3cd45242017-04-05 08:58:07 +0300639#endif
Dhaval Patel223ec952013-07-18 14:49:44 -0700640
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800641 if (target_warm_boot()) {
642 warm_boot = true;
643 cmdline_len += strlen(warmboot_cmdline);
644 }
645
Mayank Grover5384ed82018-05-09 12:09:24 +0530646 if (target_uses_system_as_root() ||
647 partition_multislot_is_supported())
Mayank Grover351a75e2017-05-30 20:06:08 +0530648 {
649 current_active_slot = partition_find_active_slot();
650 cmdline_len += (strlen(androidboot_slot_suffix)+
651 strlen(SUFFIX_SLOT(current_active_slot)));
652
Mayank Grover3804be72017-06-22 11:59:23 +0530653 system_ptn_index = partition_get_index("system");
654 if (platform_boot_dev_isemmc())
655 {
Sourabh Banerjee386b1322018-02-27 09:37:28 +0530656#if VERITY_LE
657 /*
658 Condition 4: Verity and A/B both enabled
659 Eventual command line looks like:
660 ... androidboot.slot_suffix=<slot_suffix> ... rootfstype=ext4 ...
661 ... root=/dev/dm-0 dm="system_<slot_suffix> none ro,0 1 android-verity /dev/mmcblk0p<NN>"
662 */
663 snprintf(syspath_buf, syspath_buflen, " %s %s%s %s%d\"",
664 verity_dev,
665 verity_system_part, suffix_slot[current_active_slot],
666 verity_params, system_ptn_index + 1);
667#else
668 /*
669 Condition 5: A/B enabled, but verity disabled
670 Eventual command line looks like:
671 ... androidboot.slot_suffix=<slot_suffix> ... rootfstype=ext4 ...
672 ... root=/dev/mmcblk0p<NN> ...
673 */
674 snprintf(syspath_buf, syspath_buflen, " %s%d",
675 sys_path, system_ptn_index + 1);
676#endif
Mayank Grover3804be72017-06-22 11:59:23 +0530677 }
678 else
679 {
680 lun = partition_get_lun(system_ptn_index);
681 snprintf(syspath_buf, syspath_buflen, " root=/dev/sd%c%d",
682 lun_char_base + lun,
683 partition_get_index_in_lun("system", lun));
684 }
Mayank Grover351a75e2017-05-30 20:06:08 +0530685
Monika Singh292b3e92018-03-17 22:40:23 +0530686#ifndef VERIFIED_BOOT_2
Mayank Grover3804be72017-06-22 11:59:23 +0530687 cmdline_len += strlen(syspath_buf);
Monika Singh292b3e92018-03-17 22:40:23 +0530688#endif
Mayank Grover5384ed82018-05-09 12:09:24 +0530689 }
690
691 if (target_uses_system_as_root() ||
692 partition_multislot_is_supported())
693 {
694 cmdline_len += strlen(sys_path_cmdline);
Mayank Groverb716edf2019-05-08 16:06:55 +0530695
696 /* For dynamic partition, support skip skip_initramfs */
697 if (!target_dynamic_partition_supported() &&
698 !boot_into_recovery)
Mayank Grover351a75e2017-05-30 20:06:08 +0530699 cmdline_len += strlen(skip_ramfs);
700 }
701
Mayank Grover466d6562018-05-10 14:52:20 +0530702#if HIBERNATION_SUPPORT
703 if (platform_boot_dev_isemmc())
704 {
705 swap_ptn_index = partition_get_index("swap");
706 if (swap_ptn_index != INVALID_PTN)
707 {
708 snprintf(resume_buf, resume_buflen,
709 " %s%d", resume,
710 (swap_ptn_index + 1));
711 cmdline_len += strlen(resume_buf);
712 }
713 else
714 {
715 dprintf(INFO, "WARNING: swap partition not found\n");
716 }
717 }
718#endif
719
Channagoud Kadabi85c7ec32016-01-28 23:09:21 -0800720#if TARGET_CMDLINE_SUPPORT
721 char *target_cmdline_buf = malloc(TARGET_MAX_CMDLNBUF);
722 int target_cmd_line_len;
723 ASSERT(target_cmdline_buf);
724 target_cmd_line_len = target_update_cmdline(target_cmdline_buf);
725 cmdline_len += target_cmd_line_len;
726#endif
727
lijuang83ef4b22018-08-23 11:01:55 +0800728#if !VERITY_LE
729 dtbo_idx = get_dtbo_idx ();
730 if (dtbo_idx != INVALID_PTN) {
731 snprintf(dtbo_idx_str, sizeof(dtbo_idx_str), "%s%d",
732 android_boot_dtbo_idx, dtbo_idx);
733 cmdline_len += strlen (dtbo_idx_str);
734 }
Parth Dixit1375d9e2019-07-08 20:56:13 +0530735
736 dtb_idx = get_dtb_idx ();
737 if (dtb_idx != INVALID_PTN) {
738 snprintf(dtb_idx_str, sizeof(dtb_idx_str), "%s%d",
739 android_boot_dtb_idx, dtb_idx);
740 cmdline_len += strlen (dtb_idx_str);
741 }
lijuang83ef4b22018-08-23 11:01:55 +0800742#endif
743
David Ng183a7422009-12-07 14:55:21 -0800744 if (cmdline_len > 0) {
745 const char *src;
Maria Yu52254c02014-07-04 16:14:54 +0800746 unsigned char *dst;
747
748 cmdline_final = (unsigned char*) malloc((cmdline_len + 4) & (~3));
749 ASSERT(cmdline_final != NULL);
vijay kumar287bb542015-09-29 13:01:52 +0530750 memset((void *)cmdline_final, 0, sizeof(*cmdline_final));
Maria Yu52254c02014-07-04 16:14:54 +0800751 dst = cmdline_final;
Neeti Desaie245d492012-06-01 12:52:13 -0700752
Amol Jadi168b7712012-03-06 16:15:00 -0800753 /* Save start ptr for debug print */
David Ng183a7422009-12-07 14:55:21 -0800754 if (have_cmdline) {
755 src = cmdline;
756 while ((*dst++ = *src++));
757 }
758 if (target_is_emmc_boot()) {
759 src = emmc_cmdline;
760 if (have_cmdline) --dst;
David Ngf773dde2010-07-26 19:55:08 -0700761 have_cmdline = 1;
762 while ((*dst++ = *src++));
Sridhar Parasuram7bd4aaf2015-02-12 11:14:38 -0800763#if USE_BOOTDEV_CMDLINE
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700764 src = boot_dev_buf;
Mayank Groverb716edf2019-05-08 16:06:55 +0530765 if (have_cmdline &&
766 boot_dev_buf) {
767 --dst;
768 while ((*dst++ = *src++));
769 }
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700770#endif
Mayank Groverb716edf2019-05-08 16:06:55 +0530771 /* Dynamic partition append boot_devices */
772 if (target_dynamic_partition_supported() &&
773 boot_dev_buf) {
774 src = dynamic_bootdev_cmdline;
775 if (have_cmdline) --dst;
776 while ((*dst++ = *src++));
777 src = boot_dev_buf;
778 if (have_cmdline) --dst;
779 while ((*dst++ = *src++));
780 }
David Ngf773dde2010-07-26 19:55:08 -0700781 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800782
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700783#if VERIFIED_BOOT
Mayank Grover912eaa62017-10-26 12:08:53 +0530784 if (VB_M <= target_get_vb_version())
Sridhar Parasuram7d8bb9a2015-09-23 18:21:19 -0700785 {
Mayank Grover889be1b2017-09-12 20:12:23 +0530786 src = verified_state;
787 if(have_cmdline) --dst;
788 have_cmdline = 1;
789 while ((*dst++ = *src++));
790 src = vbsn[boot_state].name;
791 if(have_cmdline) --dst;
792 while ((*dst++ = *src++));
793
794 if ((device.verity_mode != 0 ) && (device.verity_mode != 1))
795 {
796 dprintf(CRITICAL, "Devinfo paritition possibly corrupted!!!. Please erase devinfo partition to continue booting\n");
797 ASSERT(0);
798 }
799 src = verity_mode;
800 if(have_cmdline) --dst;
801 while ((*dst++ = *src++));
802 src = vbvm[device.verity_mode].name;
803 if(have_cmdline) -- dst;
804 while ((*dst++ = *src++));
805 src = keymaster_v1;
806 if(have_cmdline) --dst;
807 while ((*dst++ = *src++));
Sridhar Parasuram7d8bb9a2015-09-23 18:21:19 -0700808 }
Parth Dixitddbc7352015-10-18 03:13:31 +0530809#endif
Monika Singh292b3e92018-03-17 22:40:23 +0530810
811 if (vbcmdline != NULL) {
812 src = vbcmdline;
813 if (have_cmdline) --dst;
814 while ((*dst++ = *src++));
815 }
816
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800817 src = usb_sn_cmdline;
818 if (have_cmdline) --dst;
819 have_cmdline = 1;
820 while ((*dst++ = *src++));
821 src = sn_buf;
822 if (have_cmdline) --dst;
823 have_cmdline = 1;
824 while ((*dst++ = *src++));
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800825 if (warm_boot) {
826 if (have_cmdline) --dst;
827 src = warmboot_cmdline;
828 while ((*dst++ = *src++));
829 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800830
Pavel Nedev5614d222013-06-17 18:01:02 +0300831 if (boot_into_recovery && gpt_exists) {
832 src = secondary_gpt_enable;
833 if (have_cmdline) --dst;
834 while ((*dst++ = *src++));
835 }
Monika Singh292b3e92018-03-17 22:40:23 +0530836#ifdef MDTP_SUPPORT
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200837 if (is_mdtp_activated) {
838 src = mdtp_activated_flag;
839 if (have_cmdline) --dst;
840 while ((*dst++ = *src++));
841 }
Monika Singh292b3e92018-03-17 22:40:23 +0530842#endif
Pavel Nedev328ac822013-04-05 15:25:11 +0300843 if (boot_into_ffbm) {
844 src = androidboot_mode;
845 if (have_cmdline) --dst;
846 while ((*dst++ = *src++));
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700847 src = ffbm_mode_string;
Pavel Nedev328ac822013-04-05 15:25:11 +0300848 if (have_cmdline) --dst;
849 while ((*dst++ = *src++));
Hareesh Gauthamabf1e1e2017-04-12 18:09:24 +0530850
851 if(is_systemd_present) {
852 src = systemd_ffbm_mode;
853 if (have_cmdline) --dst;
854 while ((*dst++ = *src++));
855 }
856
Pavel Nedev898298c2013-02-27 12:36:09 -0800857 src = loglevel;
858 if (have_cmdline) --dst;
859 while ((*dst++ = *src++));
Matthew Qind886f3c2014-01-17 16:52:01 +0800860 } else if (boot_reason_alarm) {
861 src = alarmboot_cmdline;
862 if (have_cmdline) --dst;
863 while ((*dst++ = *src++));
Pavel Nedev328ac822013-04-05 15:25:11 +0300864 } else if (pause_at_bootup) {
David Ngf773dde2010-07-26 19:55:08 -0700865 src = battchg_pause;
866 if (have_cmdline) --dst;
David Ng183a7422009-12-07 14:55:21 -0800867 while ((*dst++ = *src++));
868 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800869
Shashank Mittalcd98d472011-08-02 14:29:24 -0700870 if(target_use_signed_kernel() && auth_kernel_img) {
871 src = auth_kernel;
872 if (have_cmdline) --dst;
873 while ((*dst++ = *src++));
874 }
875
Ajay Dudanid04110c2011-01-17 23:55:07 -0800876 switch(target_baseband())
877 {
878 case BASEBAND_APQ:
879 src = baseband_apq;
880 if (have_cmdline) --dst;
881 while ((*dst++ = *src++));
882 break;
883
884 case BASEBAND_MSM:
885 src = baseband_msm;
886 if (have_cmdline) --dst;
887 while ((*dst++ = *src++));
888 break;
889
890 case BASEBAND_CSFB:
891 src = baseband_csfb;
892 if (have_cmdline) --dst;
893 while ((*dst++ = *src++));
894 break;
895
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800896 case BASEBAND_SVLTE2A:
897 src = baseband_svlte2a;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800898 if (have_cmdline) --dst;
899 while ((*dst++ = *src++));
900 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700901
902 case BASEBAND_MDM:
903 src = baseband_mdm;
904 if (have_cmdline) --dst;
905 while ((*dst++ = *src++));
906 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700907
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800908 case BASEBAND_MDM2:
909 src = baseband_mdm2;
910 if (have_cmdline) --dst;
911 while ((*dst++ = *src++));
912 break;
913
Amol Jadi5c61a952012-05-04 17:05:35 -0700914 case BASEBAND_SGLTE:
915 src = baseband_sglte;
916 if (have_cmdline) --dst;
917 while ((*dst++ = *src++));
918 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530919
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800920 case BASEBAND_SGLTE2:
921 src = baseband_sglte2;
922 if (have_cmdline) --dst;
923 while ((*dst++ = *src++));
924 break;
925
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530926 case BASEBAND_DSDA:
927 src = baseband_dsda;
928 if (have_cmdline) --dst;
929 while ((*dst++ = *src++));
930 break;
Amol Jadi2a15a272013-01-22 12:03:36 -0800931
932 case BASEBAND_DSDA2:
933 src = baseband_dsda2;
934 if (have_cmdline) --dst;
935 while ((*dst++ = *src++));
936 break;
Vijay Kumar Pendotib228cfc2016-06-13 20:15:23 +0530937 case BASEBAND_APQ_NOWGR:
938 src = baseband_apq_nowgr;
939 if (have_cmdline) --dst;
940 while ((*dst++ = *src++));
941 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800942 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700943
944 if (strlen(display_panel_buf)) {
Dhaval Patel223ec952013-07-18 14:49:44 -0700945 src = display_panel_buf;
946 if (have_cmdline) --dst;
947 while ((*dst++ = *src++));
948 }
Joonwoo Park61112782013-10-02 19:50:39 -0700949
950 if (have_target_boot_params) {
951 if (have_cmdline) --dst;
952 src = target_boot_params;
953 while ((*dst++ = *src++));
vijay kumar870515d2015-08-31 16:37:24 +0530954 free(target_boot_params);
Joonwoo Park61112782013-10-02 19:50:39 -0700955 }
Channagoud Kadabi85c7ec32016-01-28 23:09:21 -0800956
Mayank Grover351a75e2017-05-30 20:06:08 +0530957 if (partition_multislot_is_supported() && have_cmdline)
958 {
959 src = androidboot_slot_suffix;
960 --dst;
961 while ((*dst++ = *src++));
962 --dst;
963 src = SUFFIX_SLOT(current_active_slot);
964 while ((*dst++ = *src++));
Mayank Grover5384ed82018-05-09 12:09:24 +0530965 }
Mayank Grover351a75e2017-05-30 20:06:08 +0530966
Mayank Grover351a75e2017-05-30 20:06:08 +0530967
Mayank Grover5384ed82018-05-09 12:09:24 +0530968 /*
969 * System-As-Root behaviour, system.img should contain both
970 * system content and ramdisk content, and should be mounted at
971 * root(a/b).
972 * Apending skip_ramfs for non a/b builds which use, system as root.
973 */
974 if ((target_uses_system_as_root() ||
975 partition_multislot_is_supported()) &&
976 have_cmdline)
977 {
Mayank Groverb716edf2019-05-08 16:06:55 +0530978 if (!target_dynamic_partition_supported() &&
979 !boot_into_recovery)
Mayank Grover5384ed82018-05-09 12:09:24 +0530980 {
981 src = skip_ramfs;
Mayank Grover351a75e2017-05-30 20:06:08 +0530982 --dst;
983 while ((*dst++ = *src++));
Mayank Grover5384ed82018-05-09 12:09:24 +0530984 }
985
986 src = sys_path_cmdline;
987 --dst;
988 while ((*dst++ = *src++));
Mayank Grover3804be72017-06-22 11:59:23 +0530989
Monika Singh292b3e92018-03-17 22:40:23 +0530990#ifndef VERIFIED_BOOT_2
Mayank Grover5384ed82018-05-09 12:09:24 +0530991 src = syspath_buf;
992 --dst;
993 while ((*dst++ = *src++));
Monika Singh292b3e92018-03-17 22:40:23 +0530994#endif
Mayank Grover351a75e2017-05-30 20:06:08 +0530995 }
996
Mayank Grover466d6562018-05-10 14:52:20 +0530997#if HIBERNATION_SUPPORT
998 if (swap_ptn_index != INVALID_PTN)
999 {
1000 src = resume_buf;
1001 --dst;
1002 while ((*dst++ = *src++));
1003 }
1004#endif
1005
Channagoud Kadabi85c7ec32016-01-28 23:09:21 -08001006#if TARGET_CMDLINE_SUPPORT
1007 if (target_cmdline_buf && target_cmd_line_len)
1008 {
1009 if (have_cmdline) --dst;
1010 src = target_cmdline_buf;
1011 while((*dst++ = *src++));
1012 free(target_cmdline_buf);
1013 }
1014#endif
lijuang83ef4b22018-08-23 11:01:55 +08001015
1016#if !VERITY_LE
1017 if (dtbo_idx != INVALID_PTN) {
1018 src = dtbo_idx_str;
1019 --dst;
1020 while ((*dst++ = *src++));
1021 }
Parth Dixit1375d9e2019-07-08 20:56:13 +05301022
1023 if (dtb_idx != INVALID_PTN) {
1024 src = dtb_idx_str;
1025 --dst;
1026 while ((*dst++ = *src++));
1027 }
lijuang83ef4b22018-08-23 11:01:55 +08001028#endif
Neeti Desaie245d492012-06-01 12:52:13 -07001029 }
Dhaval Patel223ec952013-07-18 14:49:44 -07001030
1031
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -07001032 if (boot_dev_buf)
1033 free(boot_dev_buf);
1034
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -08001035 if (cmdline_final)
1036 dprintf(INFO, "cmdline: %s\n", cmdline_final);
1037 else
1038 dprintf(INFO, "cmdline is NULL\n");
Neeti Desaie245d492012-06-01 12:52:13 -07001039 return cmdline_final;
1040}
1041
1042unsigned *atag_core(unsigned *ptr)
1043{
1044 /* CORE */
1045 *ptr++ = 2;
1046 *ptr++ = 0x54410001;
1047
1048 return ptr;
1049
1050}
1051
1052unsigned *atag_ramdisk(unsigned *ptr, void *ramdisk,
1053 unsigned ramdisk_size)
1054{
1055 if (ramdisk_size) {
1056 *ptr++ = 4;
1057 *ptr++ = 0x54420005;
1058 *ptr++ = (unsigned)ramdisk;
1059 *ptr++ = ramdisk_size;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001060 }
1061
Neeti Desaie245d492012-06-01 12:52:13 -07001062 return ptr;
1063}
1064
1065unsigned *atag_ptable(unsigned **ptr_addr)
1066{
1067 int i;
1068 struct ptable *ptable;
1069
1070 if ((ptable = flash_get_ptable()) && (ptable->count != 0)) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001071 *(*ptr_addr)++ = 2 + (ptable->count * (sizeof(struct atag_ptbl_entry) /
1072 sizeof(unsigned)));
Neeti Desaie245d492012-06-01 12:52:13 -07001073 *(*ptr_addr)++ = 0x4d534d70;
1074 for (i = 0; i < ptable->count; ++i)
1075 ptentry_to_tag(ptr_addr, ptable_get(ptable, i));
1076 }
1077
1078 return (*ptr_addr);
1079}
1080
1081unsigned *atag_cmdline(unsigned *ptr, const char *cmdline)
1082{
1083 int cmdline_length = 0;
1084 int n;
Neeti Desaie245d492012-06-01 12:52:13 -07001085 char *dest;
1086
Amol Jadi10c7d1c2013-01-25 13:24:29 -08001087 cmdline_length = strlen((const char*)cmdline);
Neeti Desaie245d492012-06-01 12:52:13 -07001088 n = (cmdline_length + 4) & (~3);
1089
1090 *ptr++ = (n / 4) + 2;
1091 *ptr++ = 0x54410009;
1092 dest = (char *) ptr;
Amol Jadi10c7d1c2013-01-25 13:24:29 -08001093 while ((*dest++ = *cmdline++));
Neeti Desaie245d492012-06-01 12:52:13 -07001094 ptr += (n / 4);
1095
1096 return ptr;
1097}
1098
1099unsigned *atag_end(unsigned *ptr)
1100{
Brian Swetland9c4c0752009-01-25 16:23:50 -08001101 /* END */
1102 *ptr++ = 0;
1103 *ptr++ = 0;
1104
Neeti Desaie245d492012-06-01 12:52:13 -07001105 return ptr;
1106}
1107
1108void generate_atags(unsigned *ptr, const char *cmdline,
1109 void *ramdisk, unsigned ramdisk_size)
1110{
vijay kumar21a37452015-12-29 16:23:21 +05301111 unsigned *orig_ptr = ptr;
Neeti Desaie245d492012-06-01 12:52:13 -07001112 ptr = atag_core(ptr);
1113 ptr = atag_ramdisk(ptr, ramdisk, ramdisk_size);
1114 ptr = target_atag_mem(ptr);
1115
1116 /* Skip NAND partition ATAGS for eMMC boot */
1117 if (!target_is_emmc_boot()){
1118 ptr = atag_ptable(&ptr);
1119 }
1120
vijay kumar21a37452015-12-29 16:23:21 +05301121 /*
1122 * Atags size filled till + cmdline size + 1 unsigned for 4-byte boundary + 4 unsigned
1123 * for atag identifier in atag_cmdline and atag_end should be with in MAX_TAGS_SIZE bytes
1124 */
Mayank Groverbc8a2ef2018-02-23 18:03:36 +05301125 if (!cmdline)
1126 return;
1127
vijay kumar21a37452015-12-29 16:23:21 +05301128 if (((ptr - orig_ptr) + strlen(cmdline) + 5 * sizeof(unsigned)) < MAX_TAGS_SIZE) {
1129 ptr = atag_cmdline(ptr, cmdline);
1130 ptr = atag_end(ptr);
1131 }
1132 else {
1133 dprintf(CRITICAL,"Crossing ATAGs Max size allowed\n");
1134 ASSERT(0);
1135 }
Neeti Desaie245d492012-06-01 12:52:13 -07001136}
1137
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001138typedef void entry_func_ptr(unsigned, unsigned, unsigned*);
Neeti Desaie245d492012-06-01 12:52:13 -07001139void boot_linux(void *kernel, unsigned *tags,
1140 const char *cmdline, unsigned machtype,
1141 void *ramdisk, unsigned ramdisk_size)
1142{
Amol Jadi10c7d1c2013-01-25 13:24:29 -08001143 unsigned char *final_cmdline;
Amol Jadib6be5c12012-11-14 13:39:51 -08001144#if DEVICE_TREE
Neeti Desai17379b82012-06-04 18:42:53 -07001145 int ret = 0;
Amol Jadib6be5c12012-11-14 13:39:51 -08001146#endif
1147
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001148 void (*entry)(unsigned, unsigned, unsigned*) = (entry_func_ptr*)(PA((addr_t)kernel));
Deepa Dinamani25a9f762012-11-30 15:57:15 -08001149 uint32_t tags_phys = PA((addr_t)tags);
vijay kumar1a50a642015-11-16 12:41:15 +05301150 struct kernel64_hdr *kptr = ((struct kernel64_hdr*)(PA((addr_t)kernel)));
Deepa Dinamani25a9f762012-11-30 15:57:15 -08001151
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301152 ramdisk = (void *)PA((addr_t)ramdisk);
Neeti Desaie245d492012-06-01 12:52:13 -07001153
Amol Jadi10c7d1c2013-01-25 13:24:29 -08001154 final_cmdline = update_cmdline((const char*)cmdline);
1155
Neeti Desai17379b82012-06-04 18:42:53 -07001156#if DEVICE_TREE
Amol Jadib6be5c12012-11-14 13:39:51 -08001157 dprintf(INFO, "Updating device tree: start\n");
1158
Neeti Desai17379b82012-06-04 18:42:53 -07001159 /* Update the Device Tree */
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301160 ret = update_device_tree((void *)tags,(const char *)final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -07001161 if(ret)
1162 {
1163 dprintf(CRITICAL, "ERROR: Updating Device Tree Failed \n");
1164 ASSERT(0);
1165 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001166 dprintf(INFO, "Updating device tree: done\n");
Neeti Desai17379b82012-06-04 18:42:53 -07001167#else
Neeti Desaie245d492012-06-01 12:52:13 -07001168 /* Generating the Atags */
Amol Jadi10c7d1c2013-01-25 13:24:29 -08001169 generate_atags(tags, final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -07001170#endif
Neeti Desaie245d492012-06-01 12:52:13 -07001171
Monika Singh0ec6db82019-07-18 16:57:58 +05301172#if VERIFIED_BOOT
Monika Singhb0db4b82018-09-26 12:18:02 +05301173 if (VB_M <= target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +05301174 {
1175 if (device.verity_mode == 0) {
lijuangbdd9bb42016-03-01 18:22:17 +08001176#if FBCON_DISPLAY_MSG
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07001177#if ENABLE_VB_ATTEST
Mayank Grover889be1b2017-09-12 20:12:23 +05301178 display_bootverify_menu(DISPLAY_MENU_EIO);
1179 wait_for_users_action();
1180 if(!pwr_key_is_pressed)
1181 shutdown_device();
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07001182#else
Mayank Grover889be1b2017-09-12 20:12:23 +05301183 display_bootverify_menu(DISPLAY_MENU_LOGGING);
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07001184#endif
Mayank Grover889be1b2017-09-12 20:12:23 +05301185 wait_for_users_action();
lijuangbdd9bb42016-03-01 18:22:17 +08001186#else
Mayank Grover889be1b2017-09-12 20:12:23 +05301187 dprintf(CRITICAL,
1188 "The dm-verity is not started in enforcing mode.\nWait for 5 seconds before proceeding\n");
1189 mdelay(5000);
lijuangbdd9bb42016-03-01 18:22:17 +08001190#endif
Mayank Grover889be1b2017-09-12 20:12:23 +05301191 }
lijuangbdd9bb42016-03-01 18:22:17 +08001192 }
lijuangbdd9bb42016-03-01 18:22:17 +08001193#endif
1194
1195#if VERIFIED_BOOT
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001196 /* Write protect the device info */
Channagoud Kadabi3bd9d1e2015-05-05 16:18:20 -07001197 if (!boot_into_recovery && target_build_variant_user() && devinfo_present && mmc_write_protect("devinfo", 1))
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001198 {
1199 dprintf(INFO, "Failed to write protect dev info\n");
1200 ASSERT(0);
1201 }
1202#endif
1203
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08001204 /* Turn off splash screen if enabled */
1205#if DISPLAY_SPLASH_SCREEN
1206 target_display_shutdown();
1207#endif
1208
Veera Sundaram Sankaran67ea0932015-09-25 10:09:30 -07001209 /* Perform target specific cleanup */
1210 target_uninit();
Monika Singh292b3e92018-03-17 22:40:23 +05301211 free_verified_boot_resource(&info);
1212 if (final_cmdline)
1213 free(final_cmdline);
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08001214
Deepa Dinamani33734bc2013-03-06 12:16:06 -08001215 dprintf(INFO, "booting linux @ %p, ramdisk @ %p (%d), tags/device tree @ %p\n",
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301216 entry, ramdisk, ramdisk_size, (void *)tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001217
1218 enter_critical_section();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001219
Amol Jadi4421e652011-06-16 15:00:48 -07001220 /* do any platform specific cleanup before kernel entry */
1221 platform_uninit();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001222
Brian Swetland9c4c0752009-01-25 16:23:50 -08001223 arch_disable_cache(UCACHE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001224
Amol Jadi504f9fe2012-08-16 13:56:48 -07001225#if ARM_WITH_MMU
Brian Swetland9c4c0752009-01-25 16:23:50 -08001226 arch_disable_mmu();
Amol Jadi504f9fe2012-08-16 13:56:48 -07001227#endif
Amol Jadi492d5a52013-03-15 16:12:34 -07001228 bs_set_timestamp(BS_KERNEL_ENTRY);
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001229
1230 if (IS_ARM64(kptr))
1231 /* Jump to a 64bit kernel */
1232 scm_elexec_call((paddr_t)kernel, tags_phys);
1233 else
1234 /* Jump to a 32bit kernel */
1235 entry(0, machtype, (unsigned*)tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001236}
1237
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001238/* Function to check if the memory address range falls within the aboot
1239 * boundaries.
1240 * start: Start of the memory region
1241 * size: Size of the memory region
1242 */
Vijay Kumar Pendoti9c002ad2016-03-09 13:52:45 +05301243int check_aboot_addr_range_overlap(uintptr_t start, uint32_t size)
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001244{
1245 /* Check for boundary conditions. */
Sundarajan Srinivasance2a0ea2013-12-16 17:02:56 -08001246 if ((UINT_MAX - start) < size)
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001247 return -1;
1248
1249 /* Check for memory overlap. */
1250 if ((start < MEMBASE) && ((start + size) <= MEMBASE))
1251 return 0;
Channagoud Kadabi94143912013-10-15 12:53:52 -07001252 else if (start >= (MEMBASE + MEMSIZE))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001253 return 0;
1254 else
1255 return -1;
1256}
1257
Mayank Grovere559cec2017-10-17 15:12:03 +05301258/* Function to check if the memory address range falls beyond ddr region.
1259 * start: Start of the memory region
1260 * size: Size of the memory region
1261 */
1262int check_ddr_addr_range_bound(uintptr_t start, uint32_t size)
1263{
1264 uintptr_t ddr_pa_start_addr = PA(get_ddr_start());
1265 uint64_t ddr_size = smem_get_ddr_size();
1266 uint64_t ddr_pa_end_addr = ddr_pa_start_addr + ddr_size;
1267 uintptr_t pa_start_addr = PA(start);
1268
1269 /* Check for boundary conditions. */
1270 if ((UINT_MAX - start) < size)
1271 return -1;
1272
1273 /* Check if memory range is beyond the ddr range. */
1274 if (pa_start_addr < ddr_pa_start_addr ||
1275 pa_start_addr >= (ddr_pa_end_addr) ||
1276 (pa_start_addr + size) > ddr_pa_end_addr)
1277 return -1;
1278 else
1279 return 0;
1280}
1281
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001282BUF_DMA_ALIGN(buf, BOOT_IMG_MAX_PAGE_SIZE); //Equal to max-supported pagesize
Dima Zavin214cc642009-01-26 11:16:21 -08001283
Mayank Groverdd080e82018-09-04 20:12:13 +05301284int getimage(void **image_buffer, uint32_t *imgsize,
1285 const char *imgname)
Monika Singh292b3e92018-03-17 22:40:23 +05301286{
Mayank Groverdd080e82018-09-04 20:12:13 +05301287 uint32_t loadedindex;
1288 if (image_buffer == NULL || imgsize == NULL ||
Monika Singh292b3e92018-03-17 22:40:23 +05301289 imgname == NULL) {
1290 dprintf(CRITICAL, "getimage: invalid parameters\n");
1291 return -1;
1292 }
Mayank Groverdd080e82018-09-04 20:12:13 +05301293 for (loadedindex = 0; loadedindex < info.num_loaded_images; loadedindex++) {
1294 if (!strncmp(info.images[loadedindex].name, imgname,
Monika Singh292b3e92018-03-17 22:40:23 +05301295 strlen(imgname))) {
Mayank Groverdd080e82018-09-04 20:12:13 +05301296 *image_buffer = info.images[loadedindex].image_buffer;
1297 *imgsize = info.images[loadedindex].imgsize;
Mayank Grover027a9412018-09-04 15:12:05 +05301298 dprintf(SPEW, "getimage(): Loaded image [%s|%d]\n",
1299 info.images[loadedindex].name,
1300 info.images[loadedindex].imgsize);
Monika Singh292b3e92018-03-17 22:40:23 +05301301 return 0;
1302 }
1303 }
1304 return -1;
1305}
1306
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001307static void verify_signed_bootimg(uint32_t bootimg_addr, uint32_t bootimg_size)
1308{
1309 int ret;
Channagoud Kadabia8c623f2015-01-13 14:48:48 -08001310
1311#if !VERIFIED_BOOT
Sundarajan Srinivasance54d6e2013-11-11 12:45:00 -08001312#if IMAGE_VERIF_ALGO_SHA1
1313 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA1;
1314#else
1315 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA256;
1316#endif
Channagoud Kadabia8c623f2015-01-13 14:48:48 -08001317#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001318
1319 /* Assume device is rooted at this time. */
1320 device.is_tampered = 1;
1321
1322 dprintf(INFO, "Authenticating boot image (%d): start\n", bootimg_size);
1323
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001324#if VERIFIED_BOOT
Monika Singh7d2fc272018-03-16 17:16:01 +05301325 uint32_t bootstate;
1326 if(boot_into_recovery &&
Mayank Grover68fbf0d2017-10-24 14:13:39 +05301327 (!partition_multislot_is_supported()))
Monika Singh7d2fc272018-03-16 17:16:01 +05301328 {
1329 ret = boot_verify_image((unsigned char *)bootimg_addr,
1330 bootimg_size, "/recovery", &bootstate);
1331 }
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001332 else
Monika Singh7d2fc272018-03-16 17:16:01 +05301333 {
1334 ret = boot_verify_image((unsigned char *)bootimg_addr,
1335 bootimg_size, "/boot", &bootstate);
1336 }
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001337 boot_verify_print_state();
1338#else
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001339 ret = image_verify((unsigned char *)bootimg_addr,
1340 (unsigned char *)(bootimg_addr + bootimg_size),
1341 bootimg_size,
Sundarajan Srinivasance54d6e2013-11-11 12:45:00 -08001342 auth_algo);
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001343#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001344 dprintf(INFO, "Authenticating boot image: done return value = %d\n", ret);
1345
1346 if (ret)
1347 {
1348 /* Authorized kernel */
1349 device.is_tampered = 0;
Sundarajan Srinivasan3fb21f12013-09-16 18:36:15 -07001350 auth_kernel_img = 1;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001351 }
1352
Amit Blay4aa292f2015-04-28 21:55:59 +03001353#ifdef MDTP_SUPPORT
1354 {
1355 /* Verify MDTP lock.
1356 * For boot & recovery partitions, use aboot's verification result.
1357 */
1358 mdtp_ext_partition_verification_t ext_partition;
1359 ext_partition.partition = boot_into_recovery ? MDTP_PARTITION_RECOVERY : MDTP_PARTITION_BOOT;
1360 ext_partition.integrity_state = device.is_tampered ? MDTP_PARTITION_STATE_INVALID : MDTP_PARTITION_STATE_VALID;
1361 ext_partition.page_size = 0; /* Not needed since already validated */
1362 ext_partition.image_addr = 0; /* Not needed since already validated */
1363 ext_partition.image_size = 0; /* Not needed since already validated */
1364 ext_partition.sig_avail = FALSE; /* Not needed since already validated */
1365 mdtp_fwlock_verify_lock(&ext_partition);
1366 }
1367#endif /* MDTP_SUPPORT */
1368
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001369#if USE_PCOM_SECBOOT
1370 set_tamper_flag(device.is_tampered);
1371#endif
1372
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001373#if VERIFIED_BOOT
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001374 switch(boot_verify_get_state())
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001375 {
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001376 case RED:
lijuanga40d6302015-07-20 20:10:13 +08001377#if FBCON_DISPLAY_MSG
lijuang9a7d3b92015-11-30 14:41:24 +08001378 display_bootverify_menu(DISPLAY_MENU_RED);
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07001379#if ENABLE_VB_ATTEST
1380 mdelay(DELAY_WAIT);
1381 shutdown_device();
1382#else
lijuanga40d6302015-07-20 20:10:13 +08001383 wait_for_users_action();
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07001384#endif
lijuanga40d6302015-07-20 20:10:13 +08001385#else
1386 dprintf(CRITICAL,
1387 "Your device has failed verification and may not work properly.\nWait for 5 seconds before proceeding\n");
1388 mdelay(5000);
1389#endif
1390
1391 break;
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001392 case YELLOW:
lijuanga40d6302015-07-20 20:10:13 +08001393#if FBCON_DISPLAY_MSG
lijuang9a7d3b92015-11-30 14:41:24 +08001394 display_bootverify_menu(DISPLAY_MENU_YELLOW);
lijuanga40d6302015-07-20 20:10:13 +08001395 wait_for_users_action();
1396#else
1397 dprintf(CRITICAL,
1398 "Your device has loaded a different operating system.\nWait for 5 seconds before proceeding\n");
1399 mdelay(5000);
1400#endif
1401 break;
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001402 default:
lijuanga40d6302015-07-20 20:10:13 +08001403 break;
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001404 }
1405#endif
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001406#if !VERIFIED_BOOT
Unnati Gandhi1be04752015-03-27 19:41:53 +05301407 if(device.is_tampered)
1408 {
1409 write_device_info_mmc(&device);
1410 #ifdef TZ_TAMPER_FUSE
Monika Singh98257462018-06-06 11:28:49 +05301411 set_tamper_fuse_cmd(HLOS_IMG_TAMPER_FUSE);
Unnati Gandhi1be04752015-03-27 19:41:53 +05301412 #endif
1413 #ifdef ASSERT_ON_TAMPER
1414 dprintf(CRITICAL, "Device is tampered. Asserting..\n");
1415 ASSERT(0);
1416 #endif
1417 }
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001418#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001419}
1420
Maunik Shah0f3c8ac2014-03-02 17:47:58 +05301421static bool check_format_bit()
1422{
1423 bool ret = false;
1424 int index;
1425 uint64_t offset;
1426 struct boot_selection_info *in = NULL;
1427 char *buf = NULL;
1428
1429 index = partition_get_index("bootselect");
1430 if (index == INVALID_PTN)
1431 {
1432 dprintf(INFO, "Unable to locate /bootselect partition\n");
1433 return ret;
1434 }
1435 offset = partition_get_offset(index);
1436 if(!offset)
1437 {
1438 dprintf(INFO, "partition /bootselect doesn't exist\n");
1439 return ret;
1440 }
1441 buf = (char *) memalign(CACHE_LINE, ROUNDUP(page_size, CACHE_LINE));
Mayank Grover48860402016-11-29 12:34:53 +05301442 mmc_set_lun(partition_get_lun(index));
Maunik Shah0f3c8ac2014-03-02 17:47:58 +05301443 ASSERT(buf);
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301444 if (mmc_read(offset, (uint32_t *)buf, page_size))
Maunik Shah0f3c8ac2014-03-02 17:47:58 +05301445 {
1446 dprintf(INFO, "mmc read failure /bootselect %d\n", page_size);
1447 free(buf);
1448 return ret;
1449 }
1450 in = (struct boot_selection_info *) buf;
1451 if ((in->signature == BOOTSELECT_SIGNATURE) &&
1452 (in->version == BOOTSELECT_VERSION)) {
1453 if ((in->state_info & BOOTSELECT_FORMAT) &&
1454 !(in->state_info & BOOTSELECT_FACTORY))
1455 ret = true;
1456 } else {
1457 dprintf(CRITICAL, "Signature: 0x%08x or version: 0x%08x mismatched of /bootselect\n",
1458 in->signature, in->version);
1459 ASSERT(0);
1460 }
1461 free(buf);
1462 return ret;
1463}
1464
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001465void boot_verifier_init()
1466{
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001467 uint32_t boot_state;
1468 /* Check if device unlock */
1469 if(device.is_unlocked)
1470 {
1471 boot_verify_send_event(DEV_UNLOCK);
1472 boot_verify_print_state();
1473 dprintf(CRITICAL, "Device is unlocked! Skipping verification...\n");
1474 return;
1475 }
1476 else
1477 {
1478 boot_verify_send_event(BOOT_INIT);
1479 }
1480
1481 /* Initialize keystore */
1482 boot_state = boot_verify_keystore_init();
1483 if(boot_state == YELLOW)
1484 {
1485 boot_verify_print_state();
1486 dprintf(CRITICAL, "Keystore verification failed! Continuing anyways...\n");
1487 }
1488}
1489
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301490/* Function to return recovery appended dtbo buffer info */
1491void get_recovery_dtbo_info(uint32_t *dtbo_size, void **dtbo_buf)
1492{
1493 *dtbo_size = recovery_dtbo_size;
1494 *dtbo_buf = recovery_dtbo_buf;
1495 return;
1496}
1497
Shashank Mittal23b8f422010-04-16 19:27:21 -07001498int boot_linux_from_mmc(void)
1499{
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301500 boot_img_hdr *hdr = (void*) buf;
1501 boot_img_hdr *uhdr;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001502 unsigned offset = 0;
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07001503 int rcode;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001504 unsigned long long ptn = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001505 int index = INVALID_PTN;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001506
Shashank Mittalcd98d472011-08-02 14:29:24 -07001507 unsigned char *image_addr = 0;
1508 unsigned kernel_actual;
1509 unsigned ramdisk_actual;
1510 unsigned imagesize_actual;
Neeti Desai465491e2012-07-31 12:53:35 -07001511 unsigned second_actual = 0;
Parth Dixit1375d9e2019-07-08 20:56:13 +05301512 void * image_buf = NULL;
Neeti Desai465491e2012-07-31 12:53:35 -07001513
Matthew Qin271927e2015-03-31 22:07:07 -04001514 unsigned int dtb_size = 0;
Parth Dixit1375d9e2019-07-08 20:56:13 +05301515 unsigned dtb_image_size = 0;
1516 uint32_t dtb_image_offset = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08001517 unsigned int out_len = 0;
1518 unsigned int out_avai_len = 0;
1519 unsigned char *out_addr = NULL;
1520 uint32_t dtb_offset = 0;
1521 unsigned char *kernel_start_addr = NULL;
1522 unsigned int kernel_size = 0;
Ameya Thakur10a33452016-06-13 14:24:26 -07001523 unsigned int patched_kernel_hdr_size = 0;
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301524 uint64_t image_size = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08001525 int rc;
Monika Singh292b3e92018-03-17 22:40:23 +05301526#if VERIFIED_BOOT_2
1527 int status;
Mayank Grover027a9412018-09-04 15:12:05 +05301528 void *dtbo_image_buf = NULL;
1529 uint32_t dtbo_image_sz = 0;
Mayank Grover9df84c02018-08-30 15:46:35 +05301530 void *vbmeta_image_buf = NULL;
1531 uint32_t vbmeta_image_sz = 0;
Monika Singh292b3e92018-03-17 22:40:23 +05301532#endif
Mayank Groverc93cad82017-10-03 12:23:45 +05301533 char *ptn_name = NULL;
Neeti Desai465491e2012-07-31 12:53:35 -07001534#if DEVICE_TREE
1535 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -07001536 struct dt_entry dt_entry;
Neeti Desai465491e2012-07-31 12:53:35 -07001537 unsigned dt_table_offset;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001538 uint32_t dt_actual;
Deepa Dinamani19648b42013-09-05 17:05:55 -07001539 uint32_t dt_hdr_size;
Matthew Qin271927e2015-03-31 22:07:07 -04001540 unsigned char *best_match_dt_addr = NULL;
Neeti Desai465491e2012-07-31 12:53:35 -07001541#endif
Matthew Qin49e51fa2015-02-09 10:40:45 +08001542 struct kernel64_hdr *kptr = NULL;
Mayank Grover351a75e2017-05-30 20:06:08 +05301543 int current_active_slot = INVALID;
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001544
Maunik Shah0f3c8ac2014-03-02 17:47:58 +05301545 if (check_format_bit())
1546 boot_into_recovery = 1;
1547
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07001548 if (!boot_into_recovery) {
1549 memset(ffbm_mode_string, '\0', sizeof(ffbm_mode_string));
1550 rcode = get_ffbm(ffbm_mode_string, sizeof(ffbm_mode_string));
1551 if (rcode <= 0) {
1552 boot_into_ffbm = false;
1553 if (rcode < 0)
1554 dprintf(CRITICAL,"failed to get ffbm cookie");
1555 } else
1556 boot_into_ffbm = true;
1557 } else
1558 boot_into_ffbm = false;
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301559 uhdr = (boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001560 if (!memcmp(uhdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1561 dprintf(INFO, "Unified boot method!\n");
1562 hdr = uhdr;
1563 goto unified_boot;
1564 }
Mayank Groverc93cad82017-10-03 12:23:45 +05301565
1566 /* For a/b recovery image code is on boot partition.
1567 If we support multislot, always use boot partition. */
1568 if (boot_into_recovery &&
Mayank Grover8ab4a762019-04-25 17:23:34 +05301569 ((!partition_multislot_is_supported()) ||
1570 (target_dynamic_partition_supported())))
Mayank Groverc93cad82017-10-03 12:23:45 +05301571 ptn_name = "recovery";
1572 else
1573 ptn_name = "boot";
1574
1575 index = partition_get_index(ptn_name);
1576 ptn = partition_get_offset(index);
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301577 image_size = partition_get_size(index);
1578 if(ptn == 0 || image_size == 0) {
Mayank Groverc93cad82017-10-03 12:23:45 +05301579 dprintf(CRITICAL, "ERROR: No %s partition found\n", ptn_name);
1580 return -1;
Kinson Chikf1a43512011-07-14 11:28:39 -07001581 }
Mayank Groverc93cad82017-10-03 12:23:45 +05301582
Channagoud Kadabief0547c2015-02-10 12:57:38 -08001583 /* Set Lun for boot & recovery partitions */
1584 mmc_set_lun(partition_get_lun(index));
Shashank Mittal23b8f422010-04-16 19:27:21 -07001585
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301586 if (mmc_read(ptn + offset, (uint32_t *) buf, page_size)) {
Shashank Mittal23b8f422010-04-16 19:27:21 -07001587 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
1588 return -1;
1589 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001590
1591 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001592 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Mayank Grover351a75e2017-05-30 20:06:08 +05301593 return ERR_INVALID_BOOT_MAGIC;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001594 }
1595
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07001596 if (hdr->page_size && (hdr->page_size != page_size)) {
vijay kumar2e21b3a2014-06-26 17:40:15 +05301597
1598 if (hdr->page_size > BOOT_IMG_MAX_PAGE_SIZE) {
1599 dprintf(CRITICAL, "ERROR: Invalid page size\n");
1600 return -1;
1601 }
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07001602 page_size = hdr->page_size;
1603 page_mask = page_size - 1;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001604 }
1605
Matthew Qin49e51fa2015-02-09 10:40:45 +08001606 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1607 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Kishor PKd8ddcad2017-07-27 13:53:57 +05301608 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001609
Matthew Qin49e51fa2015-02-09 10:40:45 +08001610 image_addr = (unsigned char *)target_get_scratch_address();
Mayank Grover6fd36792018-09-10 13:26:58 +05301611#if VERIFIED_BOOT_2
1612 /* Create hole in start of image for VB salt to copy */
1613 image_addr += SALT_BUFF_OFFSET;
1614#endif
Kishor PK9e91b592017-05-24 12:14:55 +05301615 memcpy(image_addr, (void *)buf, page_size);
1616
1617 /* ensure commandline is terminated */
1618 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
Matthew Qin49e51fa2015-02-09 10:40:45 +08001619
1620#if DEVICE_TREE
Parth Dixit4097b622016-03-15 14:42:27 +05301621#ifndef OSVERSION_IN_BOOTIMAGE
1622 dt_size = hdr->dt_size;
Mayank Grover5a502582018-09-12 11:24:49 +05301623#else
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301624 dprintf(INFO, "BootImage Header: %d\n", hdr->header_version);
Mayank Grover5a502582018-09-12 11:24:49 +05301625#endif
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301626
Parth Dixit4097b622016-03-15 14:42:27 +05301627 dt_actual = ROUND_TO_PAGE(dt_size, page_mask);
Kishor PKd8ddcad2017-07-27 13:53:57 +05301628 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 +05301629 dprintf(CRITICAL, "Integer overflow detected in bootimage header fields at %u in %s\n",__LINE__,__FILE__);
1630 return -1;
1631 }
Kishor PKd8ddcad2017-07-27 13:53:57 +05301632 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + second_actual + dt_actual);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001633#else
Kishor PKd8ddcad2017-07-27 13:53:57 +05301634 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 +05301635 dprintf(CRITICAL, "Integer overflow detected in bootimage header fields at %u in %s\n",__LINE__,__FILE__);
1636 return -1;
1637 }
Kishor PKd8ddcad2017-07-27 13:53:57 +05301638 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + second_actual);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001639#endif
Parth Dixit1375d9e2019-07-08 20:56:13 +05301640 dtb_image_size = hdr->kernel_size;
Matthew Qin49e51fa2015-02-09 10:40:45 +08001641
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301642#ifdef OSVERSION_IN_BOOTIMAGE
1643 /* If header version is ONE and booting into recovery,
1644 dtbo is appended with recovery image.
1645 Doing following:
1646 * Validating the recovery offset and size.
1647 * Extracting recovery dtbo to be used as dtbo.
1648 */
1649 if (boot_into_recovery &&
1650 hdr->header_version == BOOT_HEADER_VERSION_ONE)
1651 {
1652 struct boot_img_hdr_v1 *hdr1 =
1653 (struct boot_img_hdr_v1 *) (image_addr + sizeof(boot_img_hdr));
Mayank Grovera1a83c72018-09-24 11:23:15 +05301654 unsigned int recovery_dtbo_actual = 0;
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301655
Mayank Grovera1a83c72018-09-24 11:23:15 +05301656 recovery_dtbo_actual = ROUND_TO_PAGE(hdr1->recovery_dtbo_size, page_mask);
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301657 if ((hdr1->header_size !=
1658 sizeof(struct boot_img_hdr_v1) + sizeof(boot_img_hdr)))
1659 {
1660 dprintf(CRITICAL, "Invalid boot image header: %d\n", hdr1->header_size);
1661 return -1;
1662 }
1663
Mayank Grovera1a83c72018-09-24 11:23:15 +05301664 if (recovery_dtbo_actual > MAX_SUPPORTED_DTBO_IMG_BUF)
1665 {
1666 dprintf(CRITICAL, "Recovery Dtbo Size too big %x, Allowed size %x\n", recovery_dtbo_actual,
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301667 MAX_SUPPORTED_DTBO_IMG_BUF);
1668 return -1;
1669 }
1670
Mayank Grovera1a83c72018-09-24 11:23:15 +05301671 if (UINT_MAX < ((uint64_t)imagesize_actual + recovery_dtbo_actual))
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301672 {
1673 dprintf(CRITICAL, "Integer overflow detected in recoveryimage header fields at %u in %s\n",__LINE__,__FILE__);
1674 return -1;
1675 }
1676
Mayank Grovera1a83c72018-09-24 11:23:15 +05301677 if (UINT_MAX < (hdr1->recovery_dtbo_offset + recovery_dtbo_actual)) {
1678 dprintf(CRITICAL,
1679 "Integer overflow detected in recovery image header fields at %u in %s\n",__LINE__,__FILE__);
1680 return -1;
1681 }
1682
1683 if (hdr1->recovery_dtbo_offset + recovery_dtbo_actual > image_size)
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301684 {
1685 dprintf(CRITICAL, "Invalid recovery dtbo: Recovery Dtbo Offset=0x%llx,"
1686 " Recovery Dtbo Size=0x%x, Image Size=0x%llx\n",
1687 hdr1->recovery_dtbo_offset, recovery_dtbo_size, image_size);
1688 return -1;
1689 }
1690
1691 recovery_dtbo_buf = (void *)(hdr1->recovery_dtbo_offset + image_addr);
Mayank Grovera1a83c72018-09-24 11:23:15 +05301692 recovery_dtbo_size = recovery_dtbo_actual;
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301693 imagesize_actual += recovery_dtbo_size;
1694
1695 dprintf(SPEW, "Header version: %d\n", hdr->header_version);
1696 dprintf(SPEW, "Recovery Dtbo Size 0x%x\n", recovery_dtbo_size);
1697 dprintf(SPEW, "Recovery Dtbo Offset 0x%llx\n", hdr1->recovery_dtbo_offset);
1698
1699 }
Parth Dixit1375d9e2019-07-08 20:56:13 +05301700
1701 if ( hdr->header_version == BOOT_HEADER_VERSION_TWO) {
1702 struct boot_img_hdr_v1 *hdr1 =
1703 (struct boot_img_hdr_v1 *) (image_addr + sizeof(boot_img_hdr));
1704 struct boot_img_hdr_v2 *hdr2 = (struct boot_img_hdr_v2 *)
1705 (image_addr + sizeof(boot_img_hdr) +
1706 BOOT_IMAGE_HEADER_V2_OFFSET);
1707 unsigned int recovery_dtbo_actual = 0;
1708
lijuang0b17ba22019-09-04 14:20:47 +08001709 recovery_dtbo_actual =
1710 ROUND_TO_PAGE(hdr1->recovery_dtbo_size, page_mask);
1711 imagesize_actual += recovery_dtbo_actual;
Parth Dixit1375d9e2019-07-08 20:56:13 +05301712
1713 imagesize_actual += ROUND_TO_PAGE(hdr2->dtb_size, page_mask);
1714
1715
1716 dtb_image_offset = page_size + patched_kernel_hdr_size +
1717 kernel_actual + ramdisk_actual + second_actual +
1718 recovery_dtbo_actual;
1719
1720 dprintf(SPEW, "Header version: %d\n", hdr->header_version);
1721 dprintf(SPEW, "Dtb image offset 0x%x\n", dtb_image_offset);
1722 }
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301723#endif
1724
Parth Dixit1375d9e2019-07-08 20:56:13 +05301725
Saranya Chidurab1aaf232018-11-19 15:47:50 +05301726 /* Validate the boot/recovery image size is within the bounds of partition size */
1727 if (imagesize_actual > image_size) {
1728 dprintf(CRITICAL, "Image size is greater than partition size.\n");
1729 return -1;
1730 }
1731
Matthew Qin49e51fa2015-02-09 10:40:45 +08001732#if VERIFIED_BOOT
1733 boot_verifier_init();
1734#endif
1735
Mayank Grover1ceaee22019-04-01 17:54:32 +05301736#if VERIFIED_BOOT_2
1737 /* read full partition if device is unlocked */
1738 if (device.is_unlocked)
1739 imagesize_actual = image_size;
1740#endif
1741
Vijay Kumar Pendoti9c002ad2016-03-09 13:52:45 +05301742 if (check_aboot_addr_range_overlap((uintptr_t) image_addr, imagesize_actual))
Matthew Qin49e51fa2015-02-09 10:40:45 +08001743 {
1744 dprintf(CRITICAL, "Boot image buffer address overlaps with aboot addresses.\n");
1745 return -1;
1746 }
1747
Matthew Qinbb7923d2015-02-09 10:56:09 +08001748 /*
1749 * Update loading flow of bootimage to support compressed/uncompressed
1750 * bootimage on both 64bit and 32bit platform.
1751 * 1. Load bootimage from emmc partition onto DDR.
1752 * 2. Check if bootimage is gzip format. If yes, decompress compressed kernel
1753 * 3. Check kernel header and update kernel load addr for 64bit and 32bit
1754 * platform accordingly.
1755 * 4. Sanity Check on kernel_addr and ramdisk_addr and copy data.
1756 */
Mayank Grover351a75e2017-05-30 20:06:08 +05301757 if (partition_multislot_is_supported())
1758 {
1759 current_active_slot = partition_find_active_slot();
1760 dprintf(INFO, "Loading boot image (%d) active_slot(%s): start\n",
1761 imagesize_actual, SUFFIX_SLOT(current_active_slot));
1762 }
1763 else
1764 {
1765 dprintf(INFO, "Loading (%s) image (%d): start\n",
1766 (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual);
1767 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08001768 bs_set_timestamp(BS_KERNEL_LOAD_START);
1769
Gaurav Nebhwani43e2a462016-03-17 21:32:56 +05301770 if ((target_get_max_flash_size() - page_size) < imagesize_actual)
1771 {
1772 dprintf(CRITICAL, "booimage size is greater than DDR can hold\n");
1773 return -1;
1774 }
Kishor PK9e91b592017-05-24 12:14:55 +05301775 offset = page_size;
1776 /* Read image without signature and header*/
1777 if (mmc_read(ptn + offset, (void *)(image_addr + offset), imagesize_actual - page_size))
Matthew Qin49e51fa2015-02-09 10:40:45 +08001778 {
1779 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
1780 return -1;
1781 }
1782
Mayank Grover351a75e2017-05-30 20:06:08 +05301783 if (partition_multislot_is_supported())
1784 {
1785 dprintf(INFO, "Loading boot image (%d) active_slot(%s): done\n",
1786 imagesize_actual, SUFFIX_SLOT(current_active_slot));
1787 }
1788 else
1789 {
1790 dprintf(INFO, "Loading (%s) image (%d): done\n",
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001791 (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual);
1792
Mayank Grover351a75e2017-05-30 20:06:08 +05301793 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08001794 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
1795
1796 /* Authenticate Kernel */
1797 dprintf(INFO, "use_signed_kernel=%d, is_unlocked=%d, is_tampered=%d.\n",
1798 (int) target_use_signed_kernel(),
1799 device.is_unlocked,
1800 device.is_tampered);
Monika Singh292b3e92018-03-17 22:40:23 +05301801#if VERIFIED_BOOT_2
Mayank Grover1ceaee22019-04-01 17:54:32 +05301802 /* if device is unlocked skip reading signature, as full partition is read */
1803 if (!device.is_unlocked)
Monika Singh292b3e92018-03-17 22:40:23 +05301804 {
Mayank Grover1ceaee22019-04-01 17:54:32 +05301805 offset = imagesize_actual;
1806 if (check_aboot_addr_range_overlap((uintptr_t)image_addr + offset, page_size))
1807 {
1808 dprintf(CRITICAL, "Signature read buffer address overlaps with aboot addresses.\n");
1809 return -1;
1810 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08001811
Mayank Grover1ceaee22019-04-01 17:54:32 +05301812 /* Read signature */
1813 if(mmc_read(ptn + offset, (void *)(image_addr + offset), page_size))
1814 {
1815 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
1816 return -1;
1817 }
Monika Singh292b3e92018-03-17 22:40:23 +05301818 }
1819
Mayank Grover027a9412018-09-04 15:12:05 +05301820 /* load and validate dtbo partition */
1821 load_validate_dtbo_image(&dtbo_image_buf, &dtbo_image_sz);
1822
Mayank Grover9df84c02018-08-30 15:46:35 +05301823 /* load vbmeta partition */
1824 load_vbmeta_image(&vbmeta_image_buf, &vbmeta_image_sz);
1825
Monika Singh292b3e92018-03-17 22:40:23 +05301826 memset(&info, 0, sizeof(bootinfo));
Mayank Grover027a9412018-09-04 15:12:05 +05301827
1828 /* Pass loaded boot image passed */
Mayank Grover6fd36792018-09-10 13:26:58 +05301829 info.images[IMG_BOOT].image_buffer = SUB_SALT_BUFF_OFFSET(image_addr);
Mayank Grover027a9412018-09-04 15:12:05 +05301830 info.images[IMG_BOOT].imgsize = imagesize_actual;
1831 info.images[IMG_BOOT].name = ptn_name;
1832 ++info.num_loaded_images;
1833
1834 /* Pass loaded dtbo image */
1835 if (dtbo_image_buf != NULL) {
Mayank Grover6fd36792018-09-10 13:26:58 +05301836 info.images[IMG_DTBO].image_buffer =
1837 SUB_SALT_BUFF_OFFSET(dtbo_image_buf);
Mayank Grover027a9412018-09-04 15:12:05 +05301838 info.images[IMG_DTBO].imgsize = dtbo_image_sz;
1839 info.images[IMG_DTBO].name = "dtbo";
1840 ++info.num_loaded_images;
1841 }
1842
Mayank Grover9df84c02018-08-30 15:46:35 +05301843 /* Pass loaded vbmeta image */
1844 if (vbmeta_image_buf != NULL) {
1845 info.images[IMG_VBMETA].image_buffer = vbmeta_image_buf;
1846 info.images[IMG_VBMETA].imgsize = vbmeta_image_sz;
1847 info.images[IMG_VBMETA].name = "vbmeta";
1848 ++info.num_loaded_images;
1849 }
1850
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301851 info.header_version = hdr->header_version;
Monika Singh292b3e92018-03-17 22:40:23 +05301852 info.multi_slot_boot = partition_multislot_is_supported();
1853 info.bootreason_alarm = boot_reason_alarm;
1854 info.bootinto_recovery = boot_into_recovery;
1855 status = load_image_and_auth(&info);
1856 if(status)
1857 return -1;
1858
1859 vbcmdline = info.vbcmdline;
Mayank Grover9df84c02018-08-30 15:46:35 +05301860
1861 /* Free the buffer allocated to vbmeta post verification */
Saranya Chidurab4933332018-10-15 17:30:06 +05301862 if (vbmeta_image_buf != NULL) {
1863 free(vbmeta_image_buf);
1864 --info.num_loaded_images;
1865 }
Monika Singh292b3e92018-03-17 22:40:23 +05301866#else
Channagoud Kadabi736c4962015-08-21 11:56:52 -07001867 /* Change the condition a little bit to include the test framework support.
1868 * We would never reach this point if device is in fastboot mode, even if we did
1869 * that means we are in test mode, so execute kernel authentication part for the
1870 * tests */
Parth Dixitcb0c6082015-12-30 15:01:13 +05301871 if((target_use_signed_kernel() && (!device.is_unlocked)) || is_test_mode_enabled())
Matthew Qin49e51fa2015-02-09 10:40:45 +08001872 {
1873 offset = imagesize_actual;
Vijay Kumar Pendoti9c002ad2016-03-09 13:52:45 +05301874 if (check_aboot_addr_range_overlap((uintptr_t)image_addr + offset, page_size))
Matthew Qin49e51fa2015-02-09 10:40:45 +08001875 {
1876 dprintf(CRITICAL, "Signature read buffer address overlaps with aboot addresses.\n");
1877 return -1;
1878 }
1879
1880 /* Read signature */
1881 if(mmc_read(ptn + offset, (void *)(image_addr + offset), page_size))
1882 {
1883 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
1884 return -1;
1885 }
1886
1887 verify_signed_bootimg((uint32_t)image_addr, imagesize_actual);
Channagoud Kadabi736c4962015-08-21 11:56:52 -07001888 /* The purpose of our test is done here */
Parth Dixitcb0c6082015-12-30 15:01:13 +05301889 if(is_test_mode_enabled() && auth_kernel_img)
Channagoud Kadabi736c4962015-08-21 11:56:52 -07001890 return 0;
Matthew Qin49e51fa2015-02-09 10:40:45 +08001891 } else {
1892 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
1893 #ifdef TZ_SAVE_KERNEL_HASH
1894 aboot_save_boot_hash_mmc((uint32_t) image_addr, imagesize_actual);
1895 #endif /* TZ_SAVE_KERNEL_HASH */
Amit Blay4aa292f2015-04-28 21:55:59 +03001896
1897#ifdef MDTP_SUPPORT
1898 {
1899 /* Verify MDTP lock.
1900 * For boot & recovery partitions, MDTP will use boot_verifier APIs,
1901 * since verification was skipped in aboot. The signature is not part of the loaded image.
1902 */
1903 mdtp_ext_partition_verification_t ext_partition;
1904 ext_partition.partition = boot_into_recovery ? MDTP_PARTITION_RECOVERY : MDTP_PARTITION_BOOT;
1905 ext_partition.integrity_state = MDTP_PARTITION_STATE_UNSET;
1906 ext_partition.page_size = page_size;
1907 ext_partition.image_addr = (uint32)image_addr;
1908 ext_partition.image_size = imagesize_actual;
1909 ext_partition.sig_avail = FALSE;
1910 mdtp_fwlock_verify_lock(&ext_partition);
1911 }
1912#endif /* MDTP_SUPPORT */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001913 }
Monika Singh292b3e92018-03-17 22:40:23 +05301914#endif
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001915
Sridhar Parasuram00bfedb2015-05-26 14:21:27 -07001916#if VERIFIED_BOOT
taozhang93088bd2016-11-16 15:50:46 +08001917 if((boot_verify_get_state() == ORANGE) && (!boot_into_ffbm))
Reut Zysmanba8a9d52016-02-18 11:44:04 +02001918 {
1919#if FBCON_DISPLAY_MSG
1920 display_bootverify_menu(DISPLAY_MENU_ORANGE);
1921 wait_for_users_action();
1922#else
1923 dprintf(CRITICAL,
1924 "Your device has been unlocked and can't be trusted.\nWait for 5 seconds before proceeding\n");
1925 mdelay(5000);
1926#endif
1927 }
1928#endif
1929
1930#if VERIFIED_BOOT
Monika Singh292b3e92018-03-17 22:40:23 +05301931 if (VB_M == target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +05301932 {
1933 /* set boot and system versions. */
1934 set_os_version((unsigned char *)image_addr);
1935 // send root of trust
1936 if(!send_rot_command((uint32_t)device.is_unlocked))
1937 ASSERT(0);
1938 }
Parth Dixitbc9b6492015-10-18 00:41:38 +05301939#endif
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001940 /*
Matthew Qinbb7923d2015-02-09 10:56:09 +08001941 * Check if the kernel image is a gzip package. If yes, need to decompress it.
1942 * If not, continue booting.
1943 */
1944 if (is_gzip_package((unsigned char *)(image_addr + page_size), hdr->kernel_size))
1945 {
1946 out_addr = (unsigned char *)(image_addr + imagesize_actual + page_size);
1947 out_avai_len = target_get_max_flash_size() - imagesize_actual - page_size;
Mayank Grover027a9412018-09-04 15:12:05 +05301948#if VERIFIED_BOOT_2
1949 if (dtbo_image_sz)
1950 out_avai_len -= DTBO_IMG_BUF;
1951#endif
Matthew Qin0b15b322015-05-19 05:20:54 -04001952 dprintf(INFO, "decompressing kernel image: start\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001953 rc = decompress((unsigned char *)(image_addr + page_size),
1954 hdr->kernel_size, out_addr, out_avai_len,
1955 &dtb_offset, &out_len);
1956 if (rc)
1957 {
Matthew Qin0b15b322015-05-19 05:20:54 -04001958 dprintf(CRITICAL, "decompressing kernel image failed!!!\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001959 ASSERT(0);
1960 }
1961
Matthew Qin0b15b322015-05-19 05:20:54 -04001962 dprintf(INFO, "decompressing kernel image: done\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001963 kptr = (struct kernel64_hdr *)out_addr;
1964 kernel_start_addr = out_addr;
1965 kernel_size = out_len;
1966 } else {
Ameya Thakur10a33452016-06-13 14:24:26 -07001967 dprintf(INFO, "Uncpmpressed kernel in use\n");
1968 if (!strncmp((char*)(image_addr + page_size),
1969 PATCHED_KERNEL_MAGIC,
1970 sizeof(PATCHED_KERNEL_MAGIC) - 1)) {
1971 dprintf(INFO, "Patched kernel detected\n");
1972 kptr = (struct kernel64_hdr *)(image_addr + page_size +
1973 PATCHED_KERNEL_HEADER_SIZE);
1974 //The size of the kernel is stored at start of kernel image + 16
1975 //The dtb would start just after the kernel
1976 dtb_offset = *((uint32_t*)((unsigned char*)
1977 (image_addr + page_size +
1978 sizeof(PATCHED_KERNEL_MAGIC) -
1979 1)));
1980 //The actual kernel starts after the 20 byte header.
1981 kernel_start_addr = (unsigned char*)(image_addr +
1982 page_size + PATCHED_KERNEL_HEADER_SIZE);
1983 kernel_size = hdr->kernel_size;
1984 patched_kernel_hdr_size = PATCHED_KERNEL_HEADER_SIZE;
1985 } else {
1986 dprintf(INFO, "Kernel image not patched..Unable to locate dt offset\n");
1987 kptr = (struct kernel64_hdr *)(image_addr + page_size);
1988 kernel_start_addr = (unsigned char *)(image_addr + page_size);
1989 kernel_size = hdr->kernel_size;
1990 }
Matthew Qinbb7923d2015-02-09 10:56:09 +08001991 }
1992
1993 /*
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001994 * Update the kernel/ramdisk/tags address if the boot image header
1995 * has default values, these default values come from mkbootimg when
1996 * the boot image is flashed using fastboot flash:raw
1997 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001998 update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr));
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001999
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002000 /* Get virtual addresses since the hdr saves physical addresses. */
2001 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
2002 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
2003 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002004
Matthew Qinbb7923d2015-02-09 10:56:09 +08002005 kernel_size = ROUND_TO_PAGE(kernel_size, page_mask);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002006 /* Check if the addresses in the header are valid. */
Matthew Qinbb7923d2015-02-09 10:56:09 +08002007 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_size) ||
Mayank Grovere559cec2017-10-17 15:12:03 +05302008 check_ddr_addr_range_bound(hdr->kernel_addr, kernel_size) ||
2009 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual) ||
2010 check_ddr_addr_range_bound(hdr->ramdisk_addr, ramdisk_actual))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002011 {
Mayank Grovere559cec2017-10-17 15:12:03 +05302012 dprintf(CRITICAL, "kernel/ramdisk addresses are not valid.\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002013 return -1;
2014 }
2015
2016#ifndef DEVICE_TREE
Mayank Grovere559cec2017-10-17 15:12:03 +05302017 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE) ||
2018 check_ddr_addr_range_bound(hdr->tags_addr, MAX_TAGS_SIZE))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002019 {
Mayank Grovere559cec2017-10-17 15:12:03 +05302020 dprintf(CRITICAL, "Tags addresses are not valid.\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002021 return -1;
2022 }
2023#endif
2024
Matthew Qin49e51fa2015-02-09 10:40:45 +08002025 /* Move kernel, ramdisk and device tree to correct address */
Matthew Qinbb7923d2015-02-09 10:56:09 +08002026 memmove((void*) hdr->kernel_addr, kernel_start_addr, kernel_size);
Matthew Qin49e51fa2015-02-09 10:40:45 +08002027 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002028
Matthew Qin49e51fa2015-02-09 10:40:45 +08002029 #if DEVICE_TREE
Parth Dixit4097b622016-03-15 14:42:27 +05302030 if(dt_size) {
Matthew Qin49e51fa2015-02-09 10:40:45 +08002031 dt_table_offset = ((uint32_t)image_addr + page_size + kernel_actual + ramdisk_actual + second_actual);
2032 table = (struct dt_table*) dt_table_offset;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002033
Matthew Qin49e51fa2015-02-09 10:40:45 +08002034 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
2035 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
2036 return -1;
2037 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002038
P.V. Phani Kumar82916762016-03-09 09:20:19 +05302039 /* Its Error if, dt_hdr_size (table->num_entries * dt_entry size + Dev_Tree Header)
2040 goes beyound hdr->dt_size*/
Parth Dixit4097b622016-03-15 14:42:27 +05302041 if (dt_hdr_size > ROUND_TO_PAGE(dt_size,hdr->page_size)) {
P.V. Phani Kumar82916762016-03-09 09:20:19 +05302042 dprintf(CRITICAL, "ERROR: Invalid Device Tree size \n");
2043 return -1;
2044 }
2045
Matthew Qin49e51fa2015-02-09 10:40:45 +08002046 /* Find index of device tree within device tree table */
2047 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
2048 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
2049 return -1;
2050 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002051
P.V. Phani Kumar3c333202016-03-09 11:54:37 +05302052 if(dt_entry.offset > (UINT_MAX - dt_entry.size)) {
2053 dprintf(CRITICAL, "ERROR: Device tree contents are Invalid\n");
2054 return -1;
2055 }
2056
2057 /* Ensure we are not overshooting dt_size with the dt_entry selected */
Parth Dixit4097b622016-03-15 14:42:27 +05302058 if ((dt_entry.offset + dt_entry.size) > dt_size) {
P.V. Phani Kumar3c333202016-03-09 11:54:37 +05302059 dprintf(CRITICAL, "ERROR: Device tree contents are Invalid\n");
2060 return -1;
2061 }
2062
Matthew Qin271927e2015-03-31 22:07:07 -04002063 if (is_gzip_package((unsigned char *)dt_table_offset + dt_entry.offset, dt_entry.size))
2064 {
2065 unsigned int compressed_size = 0;
2066 out_addr += out_len;
2067 out_avai_len -= out_len;
Matthew Qin0b15b322015-05-19 05:20:54 -04002068 dprintf(INFO, "decompressing dtb: start\n");
Matthew Qin271927e2015-03-31 22:07:07 -04002069 rc = decompress((unsigned char *)dt_table_offset + dt_entry.offset,
2070 dt_entry.size, out_addr, out_avai_len,
2071 &compressed_size, &dtb_size);
2072 if (rc)
2073 {
Matthew Qin0b15b322015-05-19 05:20:54 -04002074 dprintf(CRITICAL, "decompressing dtb failed!!!\n");
Matthew Qin271927e2015-03-31 22:07:07 -04002075 ASSERT(0);
2076 }
2077
Matthew Qin0b15b322015-05-19 05:20:54 -04002078 dprintf(INFO, "decompressing dtb: done\n");
Matthew Qin271927e2015-03-31 22:07:07 -04002079 best_match_dt_addr = out_addr;
2080 } else {
2081 best_match_dt_addr = (unsigned char *)dt_table_offset + dt_entry.offset;
2082 dtb_size = dt_entry.size;
2083 }
2084
Matthew Qin49e51fa2015-02-09 10:40:45 +08002085 /* Validate and Read device device tree in the tags_addr */
Mayank Grovere559cec2017-10-17 15:12:03 +05302086 if (check_aboot_addr_range_overlap(hdr->tags_addr, dtb_size) ||
2087 check_ddr_addr_range_bound(hdr->tags_addr, dtb_size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002088 {
Mayank Grovere559cec2017-10-17 15:12:03 +05302089 dprintf(CRITICAL, "Device tree addresses are not valid\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002090 return -1;
2091 }
Shashank Mittal162244e2011-08-08 19:01:25 -07002092
Matthew Qin271927e2015-03-31 22:07:07 -04002093 memmove((void *)hdr->tags_addr, (char *)best_match_dt_addr, dtb_size);
Matthew Qin49e51fa2015-02-09 10:40:45 +08002094 } else {
2095 /* Validate the tags_addr */
Mayank Grovere559cec2017-10-17 15:12:03 +05302096 if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual) ||
2097 check_ddr_addr_range_bound(hdr->tags_addr, kernel_actual))
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07002098 {
Mayank Grovere559cec2017-10-17 15:12:03 +05302099 dprintf(CRITICAL, "Device tree addresses are not valid.\n");
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07002100 return -1;
2101 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08002102 /*
2103 * If appended dev tree is found, update the atags with
2104 * memory address to the DTB appended location on RAM.
2105 * Else update with the atags address in the kernel header
Mayank Grover1ed3e8b2018-09-19 15:16:50 +05302106 *
2107 * Make sure everything from scratch address is read before next step!
2108 * In case of dtbo, this API is going to read dtbo on scratch.
Matthew Qin49e51fa2015-02-09 10:40:45 +08002109 */
2110 void *dtb;
Parth Dixit1375d9e2019-07-08 20:56:13 +05302111 image_buf = (void*)(image_addr + page_size + patched_kernel_hdr_size);
2112
Nagireddy Annem0d60bb62019-08-16 19:16:00 +05302113#ifdef OSVERSION_IN_BOOTIMAGE
Parth Dixit1375d9e2019-07-08 20:56:13 +05302114 if ( hdr->header_version == BOOT_HEADER_VERSION_TWO) {
2115
2116 image_buf = (void*)(image_addr);
2117 dtb_offset = dtb_image_offset;
2118 dtb_image_size = imagesize_actual;
2119 }
Nagireddy Annem0d60bb62019-08-16 19:16:00 +05302120#endif
Parth Dixit1375d9e2019-07-08 20:56:13 +05302121
2122 dtb = dev_tree_appended(image_buf, dtb_image_size, dtb_offset,
Ameya Thakur10a33452016-06-13 14:24:26 -07002123 (void *)hdr->tags_addr);
Matthew Qin49e51fa2015-02-09 10:40:45 +08002124 if (!dtb) {
2125 dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07002126 return -1;
2127 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07002128 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08002129 #endif
Shashank Mittal23b8f422010-04-16 19:27:21 -07002130
Stanimir Varbanov69ec5462013-07-18 18:17:42 +03002131 if (boot_into_recovery && !device.is_unlocked && !device.is_tampered)
2132 target_load_ssd_keystore();
2133
Shashank Mittal23b8f422010-04-16 19:27:21 -07002134unified_boot:
Shashank Mittal23b8f422010-04-16 19:27:21 -07002135
Dima Zavin77e41f32013-03-06 16:10:43 -08002136 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07002137 (const char *)hdr->cmdline, board_machtype(),
Shashank Mittal23b8f422010-04-16 19:27:21 -07002138 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
2139
2140 return 0;
2141}
2142
Dima Zavin214cc642009-01-26 11:16:21 -08002143int boot_linux_from_flash(void)
2144{
Mayank Grovere1ab96c2018-09-04 20:31:31 +05302145 boot_img_hdr *hdr = (void*) buf;
Dima Zavin214cc642009-01-26 11:16:21 -08002146 struct ptentry *ptn;
2147 struct ptable *ptable;
2148 unsigned offset = 0;
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08002149
Shashank Mittalcd98d472011-08-02 14:29:24 -07002150 unsigned char *image_addr = 0;
2151 unsigned kernel_actual;
2152 unsigned ramdisk_actual;
2153 unsigned imagesize_actual;
vijay kumar8f53e362015-11-24 13:38:11 +05302154 unsigned second_actual = 0;
Shashank Mittalcd98d472011-08-02 14:29:24 -07002155
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07002156#if DEVICE_TREE
Monika Singh292b3e92018-03-17 22:40:23 +05302157 struct dt_table *table = NULL;
Joel Kingaa335dc2013-06-03 16:11:08 -07002158 struct dt_entry dt_entry;
vijay kumar8f53e362015-11-24 13:38:11 +05302159 unsigned dt_table_offset;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08002160 uint32_t dt_actual;
Monika Singh292b3e92018-03-17 22:40:23 +05302161 uint32_t dt_hdr_size = 0;
Vivek Kumar07bd0862018-04-10 14:19:23 +05302162 uint32_t dtb_offset = 0;
vijay kumar8f53e362015-11-24 13:38:11 +05302163 unsigned int dtb_size = 0;
2164 unsigned char *best_match_dt_addr = NULL;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07002165#endif
2166
David Ng183a7422009-12-07 14:55:21 -08002167 if (target_is_emmc_boot()) {
Mayank Grovere1ab96c2018-09-04 20:31:31 +05302168 hdr = (boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
David Ng183a7422009-12-07 14:55:21 -08002169 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
2170 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
2171 return -1;
2172 }
2173 goto continue_boot;
2174 }
2175
Dima Zavin214cc642009-01-26 11:16:21 -08002176 ptable = flash_get_ptable();
2177 if (ptable == NULL) {
2178 dprintf(CRITICAL, "ERROR: Partition table not found\n");
2179 return -1;
2180 }
2181
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08002182 if(!boot_into_recovery)
2183 {
2184 ptn = ptable_find(ptable, "boot");
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07002185
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08002186 if (ptn == NULL) {
2187 dprintf(CRITICAL, "ERROR: No boot partition found\n");
2188 return -1;
2189 }
2190 }
2191 else
2192 {
2193 ptn = ptable_find(ptable, "recovery");
2194 if (ptn == NULL) {
2195 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
2196 return -1;
2197 }
Dima Zavin214cc642009-01-26 11:16:21 -08002198 }
2199
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05302200 /* Read boot.img header from flash */
Shashank Mittaldcc2e352009-11-19 19:11:16 -08002201 if (flash_read(ptn, offset, buf, page_size)) {
Dima Zavin214cc642009-01-26 11:16:21 -08002202 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
2203 return -1;
2204 }
Dima Zavin214cc642009-01-26 11:16:21 -08002205
2206 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07002207 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Dima Zavin214cc642009-01-26 11:16:21 -08002208 return -1;
2209 }
2210
Shashank Mittaldcc2e352009-11-19 19:11:16 -08002211 if (hdr->page_size != page_size) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07002212 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 -08002213 return -1;
2214 }
2215
Kishor PK9e91b592017-05-24 12:14:55 +05302216 image_addr = (unsigned char *)target_get_scratch_address();
2217 memcpy(image_addr, (void *)buf, page_size);
vijay kumar287bb542015-09-29 13:01:52 +05302218
Channagoud Kadabia22144f2013-03-20 11:49:01 -07002219 /*
2220 * Update the kernel/ramdisk/tags address if the boot image header
2221 * has default values, these default values come from mkbootimg when
2222 * the boot image is flashed using fastboot flash:raw
2223 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08002224 update_ker_tags_rdisk_addr(hdr, false);
Channagoud Kadabia22144f2013-03-20 11:49:01 -07002225
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002226 /* Get virtual addresses since the hdr saves physical addresses. */
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002227 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
2228 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
2229 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
2230
2231 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
2232 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Mayank Grover032736f2017-07-14 20:34:51 +05302233 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002234
Kishor PK9e91b592017-05-24 12:14:55 +05302235 /* ensure commandline is terminated */
2236 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
2237
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002238 /* Check if the addresses in the header are valid. */
2239 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
Mayank Grovere559cec2017-10-17 15:12:03 +05302240 check_ddr_addr_range_bound(hdr->kernel_addr, kernel_actual) ||
2241 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual) ||
2242 check_ddr_addr_range_bound(hdr->ramdisk_addr, ramdisk_actual))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002243 {
Mayank Grovere559cec2017-10-17 15:12:03 +05302244 dprintf(CRITICAL, "kernel/ramdisk addresses are not valid.\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002245 return -1;
2246 }
2247
2248#ifndef DEVICE_TREE
Kishor PKd8ddcad2017-07-27 13:53:57 +05302249 if (UINT_MAX < ((uint64_t)kernel_actual + (uint64_t)ramdisk_actual+ (uint64_t)second_actual + page_size)) {
Mayank Grover032736f2017-07-14 20:34:51 +05302250 dprintf(CRITICAL, "Integer overflow detected in bootimage header fields\n");
2251 return -1;
2252 }
Kishor PKd8ddcad2017-07-27 13:53:57 +05302253 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + second_actual);
Mayank Grover032736f2017-07-14 20:34:51 +05302254
Mayank Grovere559cec2017-10-17 15:12:03 +05302255 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE) ||
2256 check_ddr_addr_range_bound(hdr->tags_addr, MAX_TAGS_SIZE))
Vijay Kumar Pendotid3ed20e2016-09-20 00:34:46 +05302257 {
Mayank Grovere559cec2017-10-17 15:12:03 +05302258 dprintf(CRITICAL, "Tags addresses are not valid.\n");
Vijay Kumar Pendotid3ed20e2016-09-20 00:34:46 +05302259 return -1;
2260 }
2261#else
2262
2263#ifndef OSVERSION_IN_BOOTIMAGE
2264 dt_size = hdr->dt_size;
2265#endif
Mayank Grover032736f2017-07-14 20:34:51 +05302266 dt_actual = ROUND_TO_PAGE(dt_size, page_mask);
Kishor PKd8ddcad2017-07-27 13:53:57 +05302267 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 +05302268 dprintf(CRITICAL, "Integer overflow detected in bootimage header fields\n");
2269 return -1;
2270 }
2271
Kishor PKd8ddcad2017-07-27 13:53:57 +05302272 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + second_actual + dt_actual);
Mayank Grover032736f2017-07-14 20:34:51 +05302273
Mayank Grovere559cec2017-10-17 15:12:03 +05302274 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_size) ||
2275 check_ddr_addr_range_bound(hdr->tags_addr, dt_size))
Mayank Grover032736f2017-07-14 20:34:51 +05302276 {
Mayank Grovere559cec2017-10-17 15:12:03 +05302277 dprintf(CRITICAL, "Device tree addresses are not valid.\n");
Mayank Grover032736f2017-07-14 20:34:51 +05302278 return -1;
2279 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002280#endif
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002281
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05302282 /* Read full boot.img from flash */
2283 dprintf(INFO, "Loading (%s) image (%d): start\n",
2284 (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual);
2285 bs_set_timestamp(BS_KERNEL_LOAD_START);
2286
2287 if (UINT_MAX - page_size < imagesize_actual)
2288 {
2289 dprintf(CRITICAL,"Integer overflow detected in bootimage header fields %u %s\n", __LINE__,__func__);
2290 return -1;
2291 }
2292
2293 /*Check the availability of RAM before reading boot image + max signature length from flash*/
2294 if (target_get_max_flash_size() < (imagesize_actual + page_size))
2295 {
2296 dprintf(CRITICAL, "bootimage size is greater than DDR can hold\n");
2297 return -1;
2298 }
2299
2300 offset = page_size;
2301 /* Read image without signature and header */
2302 if (flash_read(ptn, offset, (void *)(image_addr + offset), imagesize_actual - page_size))
2303 {
2304 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
2305 return -1;
2306 }
2307
2308 dprintf(INFO, "Loading (%s) image (%d): done\n",
2309 (!boot_into_recovery ? "boot" : "recovery"), imagesize_actual);
2310 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
2311
Shashank Mittalcd98d472011-08-02 14:29:24 -07002312 /* Authenticate Kernel */
Deepa Dinamani23b60d42013-06-24 18:10:52 -07002313 if(target_use_signed_kernel() && (!device.is_unlocked))
Shashank Mittalcd98d472011-08-02 14:29:24 -07002314 {
Shashank Mittalcd98d472011-08-02 14:29:24 -07002315 offset = imagesize_actual;
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05302316
Shashank Mittalcd98d472011-08-02 14:29:24 -07002317 /* Read signature */
2318 if (flash_read(ptn, offset, (void *)(image_addr + offset), page_size))
2319 {
2320 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002321 return -1;
Shashank Mittalcd98d472011-08-02 14:29:24 -07002322 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07002323
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05302324 verify_signed_bootimg((uint32_t)image_addr, imagesize_actual);
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05302325 }
2326 offset = page_size;
2327 if(hdr->second_size != 0) {
2328 if (UINT_MAX - offset < second_actual)
2329 {
2330 dprintf(CRITICAL, "ERROR: Integer overflow in boot image header %s\t%d\n",__func__,__LINE__);
2331 return -1;
vijay kumar8f53e362015-11-24 13:38:11 +05302332 }
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05302333 offset += second_actual;
2334 /* Second image loading not implemented. */
2335 ASSERT(0);
2336 }
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05302337 /* Move kernel and ramdisk to correct address */
2338 memmove((void*) hdr->kernel_addr, (char*) (image_addr + page_size), hdr->kernel_size);
2339 memmove((void*) hdr->ramdisk_addr, (char*) (image_addr + page_size + kernel_actual), hdr->ramdisk_size);
2340
2341#if DEVICE_TREE
2342 if(dt_size != 0) {
2343
2344 dt_table_offset = ((uint32_t)image_addr + page_size + kernel_actual + ramdisk_actual + second_actual);
2345
2346 table = (struct dt_table*) dt_table_offset;
2347
2348 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
2349 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
2350 return -1;
2351 }
2352
2353 /* Its Error if, dt_hdr_size (table->num_entries * dt_entry size + Dev_Tree Header)
2354 goes beyound hdr->dt_size*/
2355 if (dt_hdr_size > ROUND_TO_PAGE(dt_size,hdr->page_size)) {
2356 dprintf(CRITICAL, "ERROR: Invalid Device Tree size \n");
2357 return -1;
2358 }
2359
2360 /* Find index of device tree within device tree table */
2361 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
2362 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
2363 return -1;
2364 }
2365
2366 /* Validate and Read device device tree in the "tags_add */
2367 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size) ||
2368 check_ddr_addr_range_bound(hdr->tags_addr, dt_entry.size))
2369 {
2370 dprintf(CRITICAL, "Device tree addresses are not valid.\n");
2371 return -1;
2372 }
2373
2374 if(dt_entry.offset > (UINT_MAX - dt_entry.size)) {
2375 dprintf(CRITICAL, "ERROR: Device tree contents are Invalid\n");
2376 return -1;
2377 }
2378
2379 /* Ensure we are not overshooting dt_size with the dt_entry selected */
2380 if ((dt_entry.offset + dt_entry.size) > dt_size) {
2381 dprintf(CRITICAL, "ERROR: Device tree contents are Invalid\n");
2382 return -1;
2383 }
2384
2385 best_match_dt_addr = (unsigned char *)table + dt_entry.offset;
2386 dtb_size = dt_entry.size;
2387 memmove((void *)hdr->tags_addr, (char *)best_match_dt_addr, dtb_size);
Vivek Kumar07bd0862018-04-10 14:19:23 +05302388
2389 } else {
2390 /* Validate the tags_addr */
2391 if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual) ||
2392 check_ddr_addr_range_bound(hdr->tags_addr, kernel_actual))
2393 {
2394 dprintf(CRITICAL, "Device tree addresses are not valid.\n");
2395 return -1;
2396 }
2397 /*
2398 * If appended dev tree is found, update the atags with
2399 * memory address to the DTB appended location on RAM.
2400 * Else update with the atags address in the kernel header
Mayank Grover1ed3e8b2018-09-19 15:16:50 +05302401 *
2402 * Make sure everything from scratch address is read before next step!
2403 * In case of dtbo, this API is going to read dtbo on scratch.
Vivek Kumar07bd0862018-04-10 14:19:23 +05302404 */
2405 void *dtb = NULL;
2406 dtb = dev_tree_appended((void*)(image_addr + page_size ),hdr->kernel_size, dtb_offset, (void *)hdr->tags_addr);
2407 if (!dtb) {
2408 dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
2409 return -1;
2410 }
2411 }
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05302412#endif
2413 if(target_use_signed_kernel() && (!device.is_unlocked))
2414 {
Shashank Mittal162244e2011-08-08 19:01:25 -07002415 /* Make sure everything from scratch address is read before next step!*/
Shashank Mittala0032282011-08-26 14:50:11 -07002416 if(device.is_tampered)
Shashank Mittal162244e2011-08-08 19:01:25 -07002417 {
2418 write_device_info_flash(&device);
2419 }
Channagoud Kadabi5c86fe32012-02-16 10:58:48 +05302420#if USE_PCOM_SECBOOT
2421 set_tamper_flag(device.is_tampered);
2422#endif
Shashank Mittalcd98d472011-08-02 14:29:24 -07002423 }
David Ng183a7422009-12-07 14:55:21 -08002424continue_boot:
Dima Zavin214cc642009-01-26 11:16:21 -08002425
Dima Zavin214cc642009-01-26 11:16:21 -08002426 /* TODO: create/pass atags to kernel */
2427
Ajay Dudanie28a6072011-07-01 13:59:46 -07002428 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07002429 (const char *)hdr->cmdline, board_machtype(),
Dima Zavin214cc642009-01-26 11:16:21 -08002430 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
2431
2432 return 0;
2433}
Brian Swetland9c4c0752009-01-25 16:23:50 -08002434
Shashank Mittal162244e2011-08-08 19:01:25 -07002435void write_device_info_mmc(device_info *dev)
2436{
Shashank Mittal162244e2011-08-08 19:01:25 -07002437 unsigned long long ptn = 0;
2438 unsigned long long size;
2439 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002440 uint8_t lun = 0;
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002441 uint32_t ret = 0;
Mayank Groverddd348d2018-01-23 14:07:09 +05302442 uint32_t device_info_sz = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07002443
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002444 if (devinfo_present)
2445 index = partition_get_index("devinfo");
2446 else
2447 index = partition_get_index("aboot");
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07002448
Shashank Mittal162244e2011-08-08 19:01:25 -07002449 ptn = partition_get_offset(index);
2450 if(ptn == 0)
2451 {
2452 return;
2453 }
2454
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002455 lun = partition_get_lun(index);
2456 mmc_set_lun(lun);
2457
Shashank Mittal162244e2011-08-08 19:01:25 -07002458 size = partition_get_size(index);
2459
Mayank Groverddd348d2018-01-23 14:07:09 +05302460 device_info_sz = ROUND_TO_PAGE(sizeof(struct device_info),
2461 mmc_blocksize_mask);
2462 if (device_info_sz == UINT_MAX)
2463 {
2464 dprintf(CRITICAL, "ERROR: Incorrect blocksize of card\n");
2465 return;
2466 }
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07002467
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002468 if (devinfo_present)
Mayank Groverddd348d2018-01-23 14:07:09 +05302469 ret = mmc_write(ptn, device_info_sz, (void *)info_buf);
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002470 else
Mayank Groverddd348d2018-01-23 14:07:09 +05302471 ret = mmc_write((ptn + size - device_info_sz), device_info_sz, (void *)info_buf);
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002472 if (ret)
Shashank Mittal162244e2011-08-08 19:01:25 -07002473 {
2474 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
Sridhar Parasurame1a97922015-07-27 15:31:22 -07002475 ASSERT(0);
Shashank Mittal162244e2011-08-08 19:01:25 -07002476 }
2477}
2478
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002479void read_device_info_mmc(struct device_info *info)
Shashank Mittal162244e2011-08-08 19:01:25 -07002480{
Shashank Mittal162244e2011-08-08 19:01:25 -07002481 unsigned long long ptn = 0;
2482 unsigned long long size;
2483 int index = INVALID_PTN;
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002484 uint32_t ret = 0;
Mayank Groverddd348d2018-01-23 14:07:09 +05302485 uint32_t device_info_sz = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07002486
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002487 if ((index = partition_get_index("devinfo")) < 0)
2488 {
2489 devinfo_present = false;
2490 index = partition_get_index("aboot");
2491 }
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07002492
Shashank Mittal162244e2011-08-08 19:01:25 -07002493 ptn = partition_get_offset(index);
2494 if(ptn == 0)
2495 {
2496 return;
2497 }
2498
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07002499 mmc_set_lun(partition_get_lun(index));
2500
Shashank Mittal162244e2011-08-08 19:01:25 -07002501 size = partition_get_size(index);
2502
Mayank Groverddd348d2018-01-23 14:07:09 +05302503 device_info_sz = ROUND_TO_PAGE(sizeof(struct device_info),
2504 mmc_blocksize_mask);
2505 if (device_info_sz == UINT_MAX)
2506 {
2507 dprintf(CRITICAL, "ERROR: Incorrect blocksize of card\n");
2508 return;
2509 }
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07002510
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002511 if (devinfo_present)
Mayank Groverddd348d2018-01-23 14:07:09 +05302512 ret = mmc_read(ptn, (void *)info_buf, device_info_sz);
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002513 else
Mayank Groverddd348d2018-01-23 14:07:09 +05302514 ret = mmc_read((ptn + size - device_info_sz), (void *)info_buf, device_info_sz);
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002515 if (ret)
Shashank Mittal162244e2011-08-08 19:01:25 -07002516 {
2517 dprintf(CRITICAL, "ERROR: Cannot read device info\n");
Sridhar Parasurame1a97922015-07-27 15:31:22 -07002518 ASSERT(0);
Shashank Mittal162244e2011-08-08 19:01:25 -07002519 }
Shashank Mittal162244e2011-08-08 19:01:25 -07002520}
2521
2522void write_device_info_flash(device_info *dev)
2523{
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002524 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
Shashank Mittal162244e2011-08-08 19:01:25 -07002525 struct ptentry *ptn;
2526 struct ptable *ptable;
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002527 if(info == NULL)
2528 {
2529 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
2530 ASSERT(0);
2531 }
2532 info_buf = info;
Shashank Mittal162244e2011-08-08 19:01:25 -07002533 ptable = flash_get_ptable();
2534 if (ptable == NULL)
2535 {
2536 dprintf(CRITICAL, "ERROR: Partition table not found\n");
2537 return;
2538 }
2539
2540 ptn = ptable_find(ptable, "devinfo");
2541 if (ptn == NULL)
2542 {
Smita Ghosh670c8b82015-05-07 09:30:03 -07002543 dprintf(CRITICAL, "ERROR: No devinfo partition found\n");
Shashank Mittal162244e2011-08-08 19:01:25 -07002544 return;
2545 }
2546
Mayank Groverdedbc892017-10-24 13:41:34 +05302547 memset(info, 0, BOOT_IMG_MAX_PAGE_SIZE);
Shashank Mittal162244e2011-08-08 19:01:25 -07002548 memcpy(info, dev, sizeof(device_info));
2549
2550 if (flash_write(ptn, 0, (void *)info_buf, page_size))
2551 {
2552 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
2553 return;
2554 }
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002555 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07002556}
2557
vijay kumarc65876c2015-04-24 13:29:16 +05302558static int read_allow_oem_unlock(device_info *dev)
2559{
vijay kumarc65876c2015-04-24 13:29:16 +05302560 unsigned offset;
2561 int index;
2562 unsigned long long ptn;
2563 unsigned long long ptn_size;
2564 unsigned blocksize = mmc_get_device_blocksize();
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07002565 STACKBUF_DMA_ALIGN(buf, blocksize);
vijay kumarc65876c2015-04-24 13:29:16 +05302566
vijay kumarca2e6812015-07-08 20:28:25 +05302567 index = partition_get_index(frp_ptns[0]);
vijay kumarc65876c2015-04-24 13:29:16 +05302568 if (index == INVALID_PTN)
2569 {
vijay kumarca2e6812015-07-08 20:28:25 +05302570 index = partition_get_index(frp_ptns[1]);
2571 if (index == INVALID_PTN)
2572 {
2573 dprintf(CRITICAL, "Neither '%s' nor '%s' partition found\n", frp_ptns[0],frp_ptns[1]);
2574 return -1;
2575 }
vijay kumarc65876c2015-04-24 13:29:16 +05302576 }
2577
2578 ptn = partition_get_offset(index);
2579 ptn_size = partition_get_size(index);
2580 offset = ptn_size - blocksize;
2581
Mayank Grover48860402016-11-29 12:34:53 +05302582 /* Set Lun for partition */
2583 mmc_set_lun(partition_get_lun(index));
2584
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07002585 if (mmc_read(ptn + offset, (void *)buf, blocksize))
vijay kumarc65876c2015-04-24 13:29:16 +05302586 {
2587 dprintf(CRITICAL, "Reading MMC failed\n");
2588 return -1;
2589 }
2590
2591 /*is_allow_unlock is a bool value stored at the LSB of last byte*/
2592 is_allow_unlock = buf[blocksize-1] & 0x01;
2593 return 0;
2594}
2595
2596static int write_allow_oem_unlock(bool allow_unlock)
2597{
vijay kumarc65876c2015-04-24 13:29:16 +05302598 unsigned offset;
vijay kumarc65876c2015-04-24 13:29:16 +05302599 int index;
2600 unsigned long long ptn;
2601 unsigned long long ptn_size;
2602 unsigned blocksize = mmc_get_device_blocksize();
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07002603 STACKBUF_DMA_ALIGN(buf, blocksize);
vijay kumarc65876c2015-04-24 13:29:16 +05302604
vijay kumarca2e6812015-07-08 20:28:25 +05302605 index = partition_get_index(frp_ptns[0]);
vijay kumarc65876c2015-04-24 13:29:16 +05302606 if (index == INVALID_PTN)
2607 {
vijay kumarca2e6812015-07-08 20:28:25 +05302608 index = partition_get_index(frp_ptns[1]);
2609 if (index == INVALID_PTN)
2610 {
2611 dprintf(CRITICAL, "Neither '%s' nor '%s' partition found\n", frp_ptns[0],frp_ptns[1]);
2612 return -1;
2613 }
vijay kumarc65876c2015-04-24 13:29:16 +05302614 }
2615
2616 ptn = partition_get_offset(index);
2617 ptn_size = partition_get_size(index);
2618 offset = ptn_size - blocksize;
Mayank Grover48860402016-11-29 12:34:53 +05302619 mmc_set_lun(partition_get_lun(index));
vijay kumarc65876c2015-04-24 13:29:16 +05302620
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07002621 if (mmc_read(ptn + offset, (void *)buf, blocksize))
vijay kumarc65876c2015-04-24 13:29:16 +05302622 {
2623 dprintf(CRITICAL, "Reading MMC failed\n");
2624 return -1;
2625 }
2626
2627 /*is_allow_unlock is a bool value stored at the LSB of last byte*/
2628 buf[blocksize-1] = allow_unlock;
2629 if (mmc_write(ptn + offset, blocksize, buf))
2630 {
2631 dprintf(CRITICAL, "Writing MMC failed\n");
2632 return -1;
2633 }
2634
2635 return 0;
2636}
2637
Shashank Mittal162244e2011-08-08 19:01:25 -07002638void read_device_info_flash(device_info *dev)
2639{
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002640 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
Shashank Mittal162244e2011-08-08 19:01:25 -07002641 struct ptentry *ptn;
2642 struct ptable *ptable;
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002643 if(info == NULL)
2644 {
2645 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
2646 ASSERT(0);
2647 }
2648 info_buf = info;
Shashank Mittal162244e2011-08-08 19:01:25 -07002649 ptable = flash_get_ptable();
2650 if (ptable == NULL)
2651 {
2652 dprintf(CRITICAL, "ERROR: Partition table not found\n");
2653 return;
2654 }
2655
2656 ptn = ptable_find(ptable, "devinfo");
2657 if (ptn == NULL)
2658 {
Smita Ghosh670c8b82015-05-07 09:30:03 -07002659 dprintf(CRITICAL, "ERROR: No devinfo partition found\n");
Shashank Mittal162244e2011-08-08 19:01:25 -07002660 return;
2661 }
2662
2663 if (flash_read(ptn, 0, (void *)info_buf, page_size))
2664 {
2665 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
2666 return;
2667 }
2668
2669 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
2670 {
Shashank Mittal162244e2011-08-08 19:01:25 -07002671 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
2672 info->is_unlocked = 0;
Shashank Mittala0032282011-08-26 14:50:11 -07002673 info->is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07002674 write_device_info_flash(info);
2675 }
2676 memcpy(dev, info, sizeof(device_info));
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002677 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07002678}
2679
2680void write_device_info(device_info *dev)
2681{
2682 if(target_is_emmc_boot())
2683 {
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002684 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
2685 if(info == NULL)
2686 {
2687 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
2688 ASSERT(0);
2689 }
2690 info_buf = info;
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002691 memcpy(info, dev, sizeof(struct device_info));
2692
2693#if USE_RPMB_FOR_DEVINFO
Mayank Grover912eaa62017-10-26 12:08:53 +05302694 if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05302695 is_secure_boot_enable()) {
2696 if((write_device_info_rpmb((void*) info, PAGE_SIZE)) < 0)
2697 ASSERT(0);
Sridhar Parasurame1a97922015-07-27 15:31:22 -07002698 }
Channagoud Kadabic80cb492015-04-28 16:08:28 -07002699 else
2700 write_device_info_mmc(info);
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002701#else
2702 write_device_info_mmc(info);
2703#endif
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002704 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07002705 }
2706 else
2707 {
2708 write_device_info_flash(dev);
2709 }
2710}
2711
Monika Singh94316462018-03-15 18:39:01 +05302712int read_rollback_index(uint32_t loc, uint64_t *roll_back_index)
2713{
2714 if (!devinfo_present) {
2715 dprintf(CRITICAL, "DeviceInfo not initalized \n");
2716 return -EINVAL;
2717 }
2718 if (loc >= ARRAY_SIZE(device.rollback_index)) {
2719 dprintf(CRITICAL, "%s() Loc out of range index: %d, array len: %d\n",
2720 __func__, loc, ARRAY_SIZE(device.rollback_index));
2721 ASSERT(0);
2722 }
2723
2724 *roll_back_index = device.rollback_index[loc];
2725 return 0;
2726}
2727
2728int write_rollback_index(uint32_t loc, uint64_t roll_back_index)
2729{
2730 if (!devinfo_present) {
2731 dprintf(CRITICAL, "DeviceInfo not initalized \n");
2732 return -EINVAL;
2733 }
2734 if (loc >= ARRAY_SIZE(device.rollback_index)) {
2735 dprintf(CRITICAL, "%s() Loc out of range index: %d, array len: %d\n",
2736 __func__, loc, ARRAY_SIZE(device.rollback_index));
2737 ASSERT(0);
2738 }
2739
2740 device.rollback_index[loc] = roll_back_index;
2741 write_device_info(&device);
2742 return 0;
2743}
2744
Monika Singhb0fad822018-03-15 18:50:55 +05302745int store_userkey(uint8_t *user_key, uint32_t user_key_size)
2746{
2747 if (!devinfo_present) {
2748 dprintf(CRITICAL, "DeviceInfo not initalized \n");
2749 return -EINVAL;
2750 }
2751
2752 if (user_key_size > ARRAY_SIZE(device.user_public_key)) {
2753 dprintf(CRITICAL, "StoreUserKey, UserKeySize too large!\n");
2754 return -ENODEV;
2755 }
2756
2757 memcpy(device.user_public_key, user_key, user_key_size);
2758 device.user_public_key_length = user_key_size;
2759 write_device_info(&device);
2760 return 0;
2761}
2762
2763int erase_userkey()
2764{
2765 if (!devinfo_present) {
2766 dprintf(CRITICAL, "DeviceInfo not initalized \n");
2767 return -EINVAL;
2768 }
2769 memset(device.user_public_key, 0, ARRAY_SIZE(device.user_public_key));
2770 device.user_public_key_length = 0;
2771 write_device_info(&device);
2772 return 0;
2773}
2774
2775int get_userkey(uint8_t **user_key, uint32_t *user_key_size)
2776{
2777 if (!devinfo_present) {
2778 dprintf(CRITICAL, "DeviceInfo not initalized \n");
2779 return -EINVAL;
2780 }
2781 *user_key = device.user_public_key;
2782 *user_key_size = device.user_public_key_length;
2783 return 0;
2784}
2785
Shashank Mittal162244e2011-08-08 19:01:25 -07002786void read_device_info(device_info *dev)
2787{
2788 if(target_is_emmc_boot())
2789 {
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002790 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
2791 if(info == NULL)
2792 {
2793 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
2794 ASSERT(0);
2795 }
2796 info_buf = info;
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002797
2798#if USE_RPMB_FOR_DEVINFO
Mayank Grover912eaa62017-10-26 12:08:53 +05302799 if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05302800 is_secure_boot_enable()) {
2801 if((read_device_info_rpmb((void*) info, PAGE_SIZE)) < 0)
2802 ASSERT(0);
Sridhar Parasurame1a97922015-07-27 15:31:22 -07002803 }
Channagoud Kadabic80cb492015-04-28 16:08:28 -07002804 else
2805 read_device_info_mmc(info);
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002806#else
2807 read_device_info_mmc(info);
2808#endif
2809
2810 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
2811 {
2812 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
lijuang511a2b52015-08-14 20:50:51 +08002813 if (is_secure_boot_enable()) {
Channagoud Kadabi05f78ba2015-07-06 11:58:14 -07002814 info->is_unlocked = 0;
Monika Singh292b3e92018-03-17 22:40:23 +05302815#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover912eaa62017-10-26 12:08:53 +05302816 if (VB_M <= target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +05302817 info->is_unlock_critical = 0;
Parth Dixitddbc7352015-10-18 03:13:31 +05302818#endif
lijuang511a2b52015-08-14 20:50:51 +08002819 } else {
Channagoud Kadabi2fda4092015-07-07 13:34:11 -07002820 info->is_unlocked = 1;
Monika Singh292b3e92018-03-17 22:40:23 +05302821#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover912eaa62017-10-26 12:08:53 +05302822 if (VB_M <= target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +05302823 info->is_unlock_critical = 1;
Parth Dixitddbc7352015-10-18 03:13:31 +05302824#endif
lijuang511a2b52015-08-14 20:50:51 +08002825 }
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002826 info->is_tampered = 0;
2827 info->charger_screen_enabled = 0;
Monika Singh292b3e92018-03-17 22:40:23 +05302828#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover912eaa62017-10-26 12:08:53 +05302829 if (VB_M <= target_get_vb_version())
Monika Singh1c71a1c2019-12-03 12:12:48 +05302830 {
Mayank Grover889be1b2017-09-12 20:12:23 +05302831 info->verity_mode = 1; //enforcing by default
Monika Singh1c71a1c2019-12-03 12:12:48 +05302832 info->user_public_key_length = 0;
2833 memset(info->rollback_index, 0, sizeof(info->rollback_index));
2834 memset(info->user_public_key, 0, sizeof(info->user_public_key));
2835 }
Parth Dixitddbc7352015-10-18 03:13:31 +05302836#endif
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002837 write_device_info(info);
2838 }
2839 memcpy(dev, info, sizeof(device_info));
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002840 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07002841 }
2842 else
2843 {
2844 read_device_info_flash(dev);
2845 }
2846}
2847
2848void reset_device_info()
2849{
2850 dprintf(ALWAYS, "reset_device_info called.");
Shashank Mittala0032282011-08-26 14:50:11 -07002851 device.is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07002852 write_device_info(&device);
2853}
2854
2855void set_device_root()
2856{
2857 dprintf(ALWAYS, "set_device_root called.");
Shashank Mittala0032282011-08-26 14:50:11 -07002858 device.is_tampered = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -07002859 write_device_info(&device);
2860}
2861
lijuang4ece1e72015-08-14 21:02:36 +08002862/* set device unlock value
2863 * Must check FRP before call this function
2864 * Need to wipe data when unlock status changed
2865 * type 0: oem unlock
2866 * type 1: unlock critical
2867 * status 0: unlock as false
2868 * status 1: lock as true
2869 */
2870void set_device_unlock_value(int type, bool status)
lijuang21f12f52015-08-22 16:22:19 +08002871{
lijuang4ece1e72015-08-14 21:02:36 +08002872 if (type == UNLOCK)
2873 device.is_unlocked = status;
Monika Singh292b3e92018-03-17 22:40:23 +05302874#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover912eaa62017-10-26 12:08:53 +05302875 else if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05302876 type == UNLOCK_CRITICAL)
2877 device.is_unlock_critical = status;
Parth Dixitddbc7352015-10-18 03:13:31 +05302878#endif
lijuang4ece1e72015-08-14 21:02:36 +08002879 write_device_info(&device);
2880}
2881
2882static void set_device_unlock(int type, bool status)
2883{
2884 int is_unlocked = -1;
2885 char response[MAX_RSP_SIZE];
2886
2887 /* check device unlock status if it is as expected */
2888 if (type == UNLOCK)
2889 is_unlocked = device.is_unlocked;
Monika Singh292b3e92018-03-17 22:40:23 +05302890#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover912eaa62017-10-26 12:08:53 +05302891 if(VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05302892 type == UNLOCK_CRITICAL)
2893 {
2894 is_unlocked = device.is_unlock_critical;
2895 }
Parth Dixitddbc7352015-10-18 03:13:31 +05302896#endif
lijuang4ece1e72015-08-14 21:02:36 +08002897 if (is_unlocked == status) {
2898 snprintf(response, sizeof(response), "\tDevice already : %s", (status ? "unlocked!" : "locked!"));
2899 fastboot_info(response);
2900 fastboot_okay("");
2901 return;
2902 }
2903
2904 /* status is true, it means to unlock device */
lijuang07c5d6e2018-04-23 18:32:13 +08002905 if (status && !is_allow_unlock) {
2906 fastboot_fail("oem unlock is not allowed");
2907 return;
2908 }
lijuang21f12f52015-08-22 16:22:19 +08002909
lijuang4ece1e72015-08-14 21:02:36 +08002910#if FBCON_DISPLAY_MSG
lijuang07c5d6e2018-04-23 18:32:13 +08002911 display_unlock_menu(type, status);
2912 fastboot_okay("");
2913 return;
lijuang4ece1e72015-08-14 21:02:36 +08002914#else
lijuang07c5d6e2018-04-23 18:32:13 +08002915 if (status && type == UNLOCK) {
2916 fastboot_fail("Need wipe userdata. Do 'fastboot oem unlock-go'");
2917 return;
lijuang21f12f52015-08-22 16:22:19 +08002918 }
lijuang07c5d6e2018-04-23 18:32:13 +08002919#endif
lijuang4ece1e72015-08-14 21:02:36 +08002920
2921 set_device_unlock_value(type, status);
2922
2923 /* wipe data */
2924 struct recovery_message msg;
Kishor PK60a68212017-05-08 16:55:57 +05302925 memset(&msg, 0, sizeof(msg));
lijuang4ece1e72015-08-14 21:02:36 +08002926 snprintf(msg.recovery, sizeof(msg.recovery), "recovery\n--wipe_data");
2927 write_misc(0, &msg, sizeof(msg));
2928
2929 fastboot_okay("");
2930 reboot_device(RECOVERY_MODE);
lijuang21f12f52015-08-22 16:22:19 +08002931}
2932
lijuang511a2b52015-08-14 20:50:51 +08002933static bool critical_flash_allowed(const char * entry)
2934{
2935 uint32_t i = 0;
2936 if (entry == NULL)
2937 return false;
2938
2939 for (i = 0; i < ARRAY_SIZE(critical_flash_allowed_ptn); i++) {
2940 if(!strcmp(entry, critical_flash_allowed_ptn[i]))
2941 return true;
2942 }
2943 return false;
Matthew Qin271927e2015-03-31 22:07:07 -04002944}
2945
2946#if DEVICE_TREE
Amol Jadicb524072012-08-09 16:40:18 -07002947int copy_dtb(uint8_t *boot_image_start, unsigned int scratch_offset)
2948{
2949 uint32 dt_image_offset = 0;
Amol Jadicb524072012-08-09 16:40:18 -07002950 uint32_t n;
Monika Singh292b3e92018-03-17 22:40:23 +05302951 struct dt_table *table = NULL;
Amol Jadicb524072012-08-09 16:40:18 -07002952 struct dt_entry dt_entry;
Monika Singh292b3e92018-03-17 22:40:23 +05302953 uint32_t dt_hdr_size = 0;
Amol Jadicb524072012-08-09 16:40:18 -07002954 unsigned int compressed_size = 0;
2955 unsigned int dtb_size = 0;
2956 unsigned int out_avai_len = 0;
2957 unsigned char *out_addr = NULL;
2958 unsigned char *best_match_dt_addr = NULL;
2959 int rc;
2960
Mayank Grovere1ab96c2018-09-04 20:31:31 +05302961 boot_img_hdr *hdr = (boot_img_hdr *) (boot_image_start);
Amol Jadicb524072012-08-09 16:40:18 -07002962
Parth Dixit4097b622016-03-15 14:42:27 +05302963#ifndef OSVERSION_IN_BOOTIMAGE
2964 dt_size = hdr->dt_size;
2965#endif
2966
2967 if(dt_size != 0) {
Amol Jadicb524072012-08-09 16:40:18 -07002968 /* add kernel offset */
2969 dt_image_offset += page_size;
2970 n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
2971 dt_image_offset += n;
2972
2973 /* add ramdisk offset */
2974 n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
2975 dt_image_offset += n;
2976
2977 /* add second offset */
2978 if(hdr->second_size != 0) {
2979 n = ROUND_TO_PAGE(hdr->second_size, page_mask);
2980 dt_image_offset += n;
2981 }
2982
2983 /* offset now point to start of dt.img */
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07002984 table = (struct dt_table*)(boot_image_start + dt_image_offset);
Amol Jadicb524072012-08-09 16:40:18 -07002985
Deepa Dinamani19648b42013-09-05 17:05:55 -07002986 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Amol Jadicb524072012-08-09 16:40:18 -07002987 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
2988 return -1;
2989 }
P.V. Phani Kumar82916762016-03-09 09:20:19 +05302990
2991 /* Its Error if, dt_hdr_size (table->num_entries * dt_entry size + Dev_Tree Header)
2992 goes beyound hdr->dt_size*/
Parth Dixit4097b622016-03-15 14:42:27 +05302993 if (dt_hdr_size > ROUND_TO_PAGE(dt_size,hdr->page_size)) {
P.V. Phani Kumar82916762016-03-09 09:20:19 +05302994 dprintf(CRITICAL, "ERROR: Invalid Device Tree size \n");
2995 return -1;
2996 }
2997
Joel Kingaa335dc2013-06-03 16:11:08 -07002998 /* Find index of device tree within device tree table */
2999 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Amol Jadicb524072012-08-09 16:40:18 -07003000 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
3001 return -1;
3002 }
3003
Matthew Qin271927e2015-03-31 22:07:07 -04003004 best_match_dt_addr = (unsigned char *)boot_image_start + dt_image_offset + dt_entry.offset;
3005 if (is_gzip_package(best_match_dt_addr, dt_entry.size))
3006 {
3007 out_addr = (unsigned char *)target_get_scratch_address() + scratch_offset;
3008 out_avai_len = target_get_max_flash_size() - scratch_offset;
Matthew Qin0b15b322015-05-19 05:20:54 -04003009 dprintf(INFO, "decompressing dtb: start\n");
Matthew Qin271927e2015-03-31 22:07:07 -04003010 rc = decompress(best_match_dt_addr,
3011 dt_entry.size, out_addr, out_avai_len,
3012 &compressed_size, &dtb_size);
3013 if (rc)
3014 {
Matthew Qin0b15b322015-05-19 05:20:54 -04003015 dprintf(CRITICAL, "decompressing dtb failed!!!\n");
Matthew Qin271927e2015-03-31 22:07:07 -04003016 ASSERT(0);
3017 }
3018
Matthew Qin0b15b322015-05-19 05:20:54 -04003019 dprintf(INFO, "decompressing dtb: done\n");
Matthew Qin271927e2015-03-31 22:07:07 -04003020 best_match_dt_addr = out_addr;
3021 } else {
3022 dtb_size = dt_entry.size;
3023 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07003024 /* Validate and Read device device tree in the "tags_add */
Mayank Grovere559cec2017-10-17 15:12:03 +05303025 if (check_aboot_addr_range_overlap(hdr->tags_addr, dtb_size) ||
3026 check_ddr_addr_range_bound(hdr->tags_addr, dtb_size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07003027 {
Mayank Grovere559cec2017-10-17 15:12:03 +05303028 dprintf(CRITICAL, "Device tree addresses are not valid.\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07003029 return -1;
3030 }
3031
Amol Jadicb524072012-08-09 16:40:18 -07003032 /* Read device device tree in the "tags_add */
Matthew Qin271927e2015-03-31 22:07:07 -04003033 memmove((void*) hdr->tags_addr, (void *)best_match_dt_addr, dtb_size);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07003034 } else
3035 return -1;
Amol Jadicb524072012-08-09 16:40:18 -07003036
3037 /* Everything looks fine. Return success. */
3038 return 0;
3039}
3040#endif
3041
Brian Swetland9c4c0752009-01-25 16:23:50 -08003042void cmd_boot(const char *arg, void *data, unsigned sz)
3043{
3044 unsigned kernel_actual;
3045 unsigned ramdisk_actual;
Kishor PKd8ddcad2017-07-27 13:53:57 +05303046 unsigned second_actual;
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07003047 uint32_t image_actual;
3048 uint32_t dt_actual = 0;
Mayank Grovere1ab96c2018-09-04 20:31:31 +05303049 boot_img_hdr *hdr = NULL;
Matthew Qinbb7923d2015-02-09 10:56:09 +08003050 struct kernel64_hdr *kptr = NULL;
Brian Swetland9c4c0752009-01-25 16:23:50 -08003051 char *ptr = ((char*) data);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07003052 int ret = 0;
3053 uint8_t dtb_copied = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08003054 unsigned int out_len = 0;
3055 unsigned int out_avai_len = 0;
3056 unsigned char *out_addr = NULL;
3057 uint32_t dtb_offset = 0;
3058 unsigned char *kernel_start_addr = NULL;
3059 unsigned int kernel_size = 0;
Matthew Qin271927e2015-03-31 22:07:07 -04003060 unsigned int scratch_offset = 0;
Saranya Chidurab4933332018-10-15 17:30:06 +05303061#if VERIFIED_BOOT_2
3062 void *dtbo_image_buf = NULL;
3063 uint32_t dtbo_image_sz = 0;
3064 void *vbmeta_image_buf = NULL;
3065 uint32_t vbmeta_image_sz = 0;
3066#endif
Monika Singh292b3e92018-03-17 22:40:23 +05303067#if !VERIFIED_BOOT_2
3068 uint32_t sig_actual = 0;
3069 uint32_t sig_size = 0;
3070#ifdef MDTP_SUPPORT
3071 static bool is_mdtp_activated = 0;
3072#endif /* MDTP_SUPPORT */
3073#endif
Matthew Qinbb7923d2015-02-09 10:56:09 +08003074
lijuang2008ff22016-03-07 17:56:27 +08003075#if FBCON_DISPLAY_MSG
3076 /* Exit keys' detection thread firstly */
3077 exit_menu_keys_detection();
3078#endif
3079
Monika Singh292b3e92018-03-17 22:40:23 +05303080#if VERIFIED_BOOT || VERIFIED_BOOT_2
Channagoud Kadabi6d5375e2015-06-23 17:15:42 -07003081 if(target_build_variant_user() && !device.is_unlocked)
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003082 {
3083 fastboot_fail("unlock device to use this command");
lijuang2008ff22016-03-07 17:56:27 +08003084 goto boot_failed;
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003085 }
3086#endif
3087
Brian Swetland9c4c0752009-01-25 16:23:50 -08003088 if (sz < sizeof(hdr)) {
3089 fastboot_fail("invalid bootimage header");
lijuang2008ff22016-03-07 17:56:27 +08003090 goto boot_failed;
Brian Swetland9c4c0752009-01-25 16:23:50 -08003091 }
3092
Mayank Grovere1ab96c2018-09-04 20:31:31 +05303093 hdr = (boot_img_hdr *)data;
Brian Swetland9c4c0752009-01-25 16:23:50 -08003094
3095 /* ensure commandline is terminated */
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07003096 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -08003097
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07003098 if(target_is_emmc_boot() && hdr->page_size) {
3099 page_size = hdr->page_size;
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07003100 page_mask = page_size - 1;
3101 }
3102
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07003103 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
3104 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Kishor PKd8ddcad2017-07-27 13:53:57 +05303105 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07003106#if DEVICE_TREE
Parth Dixit4097b622016-03-15 14:42:27 +05303107#ifndef OSVERSION_IN_BOOTIMAGE
3108 dt_size = hdr->dt_size;
3109#endif
3110 dt_actual = ROUND_TO_PAGE(dt_size, page_mask);
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07003111#endif
3112
3113 image_actual = ADD_OF(page_size, kernel_actual);
3114 image_actual = ADD_OF(image_actual, ramdisk_actual);
Kishor PKd8ddcad2017-07-27 13:53:57 +05303115 image_actual = ADD_OF(image_actual, second_actual);
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07003116 image_actual = ADD_OF(image_actual, dt_actual);
3117
Kishor PK5134b332017-05-09 17:50:08 +05303118 /* Checking to prevent oob access in read_der_message_length */
3119 if (image_actual > sz) {
3120 fastboot_fail("bootimage header fields are invalid");
3121 goto boot_failed;
3122 }
Saranya Chidurab4933332018-10-15 17:30:06 +05303123
Monika Singh292b3e92018-03-17 22:40:23 +05303124#if VERIFIED_BOOT_2
Mayank Grover1ceaee22019-04-01 17:54:32 +05303125 /* Pass size of boot partition, as imgsize, to avoid
3126 read fewer bytes error */
3127 image_actual = partition_get_size(partition_get_index("boot"));
Saranya Chidurab4933332018-10-15 17:30:06 +05303128
3129 /* load and validate dtbo partition */
3130 load_validate_dtbo_image(&dtbo_image_buf, &dtbo_image_sz);
3131
3132 /* load vbmeta partition */
3133 load_vbmeta_image(&vbmeta_image_buf, &vbmeta_image_sz);
3134
Monika Singh292b3e92018-03-17 22:40:23 +05303135 memset(&info, 0, sizeof(bootinfo));
Saranya Chidurab4933332018-10-15 17:30:06 +05303136
3137 info.images[IMG_BOOT].image_buffer = SUB_SALT_BUFF_OFFSET(data);
3138 info.images[IMG_BOOT].imgsize = image_actual;
3139 info.images[IMG_BOOT].name = "boot";
3140 ++info.num_loaded_images;
3141
3142 /* Pass loaded dtbo image */
3143 if (dtbo_image_buf != NULL) {
3144 info.images[IMG_DTBO].image_buffer = SUB_SALT_BUFF_OFFSET(dtbo_image_buf);
3145 info.images[IMG_DTBO].imgsize = dtbo_image_sz;
3146 info.images[IMG_DTBO].name = "dtbo";
3147 ++info.num_loaded_images;
3148 }
3149
3150 /* Pass loaded vbmeta image */
3151 if (vbmeta_image_buf != NULL) {
3152 info.images[IMG_VBMETA].image_buffer = vbmeta_image_buf;
3153 info.images[IMG_VBMETA].imgsize = vbmeta_image_sz;
3154 info.images[IMG_VBMETA].name = "vbmeta";
3155 ++info.num_loaded_images;
3156 }
3157
Monika Singh292b3e92018-03-17 22:40:23 +05303158 info.multi_slot_boot = partition_multislot_is_supported();
3159 if (load_image_and_auth(&info))
3160 goto boot_failed;
3161 vbcmdline = info.vbcmdline;
Saranya Chidurab4933332018-10-15 17:30:06 +05303162
3163 /* Free the buffer allocated to vbmeta post verification */
3164 if (vbmeta_image_buf != NULL) {
3165 free(vbmeta_image_buf);
3166 --info.num_loaded_images;
3167 }
Monika Singh292b3e92018-03-17 22:40:23 +05303168#else
Kishor PK5134b332017-05-09 17:50:08 +05303169 sig_size = sz - image_actual;
3170
P.V. Phani Kumareecfd822016-03-09 20:09:03 +05303171 if (target_use_signed_kernel() && (!device.is_unlocked)) {
Monika Singh292b3e92018-03-17 22:40:23 +05303172 unsigned chk;
P.V. Phani Kumareecfd822016-03-09 20:09:03 +05303173 /* Calculate the signature length from boot image */
3174 sig_actual = read_der_message_length(
Kishor PK5134b332017-05-09 17:50:08 +05303175 (unsigned char*)(data + image_actual), sig_size);
Monika Singh292b3e92018-03-17 22:40:23 +05303176 chk = ADD_OF(image_actual, sig_actual);
Channagoud Kadabi3d839012014-06-26 14:26:39 -07003177
Monika Singh292b3e92018-03-17 22:40:23 +05303178 if (chk > sz) {
Kishor PK5134b332017-05-09 17:50:08 +05303179 fastboot_fail("bootimage header fields are invalid");
3180 goto boot_failed;
3181 }
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07003182 }
3183
Sridhar Parasuram361e4cd2015-09-24 11:19:19 -07003184 // Initialize boot state before trying to verify boot.img
3185#if VERIFIED_BOOT
Channagoud Kadabi699466e2015-11-03 12:37:42 -08003186 boot_verifier_init();
Mayank Groverb337e932017-01-18 20:00:40 +05303187#endif
Channagoud Kadabibf6ce7d2015-09-17 13:25:35 -07003188 /* Handle overflow if the input image size is greater than
3189 * boot image buffer can hold
3190 */
Monika Singh292b3e92018-03-17 22:40:23 +05303191 if ((target_get_max_flash_size() - page_size) < image_actual)
Channagoud Kadabibf6ce7d2015-09-17 13:25:35 -07003192 {
3193 fastboot_fail("booimage: size is greater than boot image buffer can hold");
lijuang2008ff22016-03-07 17:56:27 +08003194 goto boot_failed;
Channagoud Kadabibf6ce7d2015-09-17 13:25:35 -07003195 }
Sridhar Parasuram361e4cd2015-09-24 11:19:19 -07003196
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07003197 /* Verify the boot image
3198 * device & page_size are initialized in aboot_init
3199 */
Sridhar Parasuram7ae9fbc2015-09-17 09:56:30 -07003200 if (target_use_signed_kernel() && (!device.is_unlocked)) {
Channagoud Kadabi3d839012014-06-26 14:26:39 -07003201 /* Pass size excluding signature size, otherwise we would try to
3202 * access signature beyond its length
3203 */
Monika Singh292b3e92018-03-17 22:40:23 +05303204 verify_signed_bootimg((uint32_t)data, image_actual);
Sridhar Parasuram7ae9fbc2015-09-17 09:56:30 -07003205 }
Amit Blay4aa292f2015-04-28 21:55:59 +03003206#ifdef MDTP_SUPPORT
3207 else
3208 {
Shay Nachmani062aa3f2015-05-17 17:28:44 +03003209 /* fastboot boot is not allowed when MDTP is activated */
Amit Blay4aa292f2015-04-28 21:55:59 +03003210 mdtp_ext_partition_verification_t ext_partition;
Amit Blay8a510302015-08-17 09:20:01 +03003211
3212 if (!is_mdtp_activated) {
3213 ext_partition.partition = MDTP_PARTITION_NONE;
3214 mdtp_fwlock_verify_lock(&ext_partition);
3215 }
Amit Blay4aa292f2015-04-28 21:55:59 +03003216 }
Shay Nachmani062aa3f2015-05-17 17:28:44 +03003217
Amir Kotzer7c768c02016-04-13 09:08:05 +03003218 /* If mdtp state cannot be validate, block fastboot boot*/
3219 if(mdtp_activated(&is_mdtp_activated)){
3220 dprintf(CRITICAL, "mdtp_activated cannot validate state.\n");
3221 dprintf(CRITICAL, "Can not proceed with fastboot boot command.\n");
3222 goto boot_failed;
3223 }
Shay Nachmani062aa3f2015-05-17 17:28:44 +03003224 if(is_mdtp_activated){
3225 dprintf(CRITICAL, "fastboot boot command is not available.\n");
lijuang2008ff22016-03-07 17:56:27 +08003226 goto boot_failed;
Shay Nachmani062aa3f2015-05-17 17:28:44 +03003227 }
Amit Blay4aa292f2015-04-28 21:55:59 +03003228#endif /* MDTP_SUPPORT */
Monika Singh292b3e92018-03-17 22:40:23 +05303229#endif /* VERIFIED_BOOT_2 else */
Amit Blay4aa292f2015-04-28 21:55:59 +03003230
Sridhar Parasuram361e4cd2015-09-24 11:19:19 -07003231#if VERIFIED_BOOT
Monika Singh292b3e92018-03-17 22:40:23 +05303232 if (VB_M == target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +05303233 {
3234 /* set boot and system versions. */
3235 set_os_version((unsigned char *)data);
3236 // send root of trust
3237 if(!send_rot_command((uint32_t)device.is_unlocked))
3238 ASSERT(0);
3239 }
Parth Dixitbc9b6492015-10-18 00:41:38 +05303240#endif
Channagoud Kadabia22144f2013-03-20 11:49:01 -07003241 /*
Matthew Qinbb7923d2015-02-09 10:56:09 +08003242 * Check if the kernel image is a gzip package. If yes, need to decompress it.
3243 * If not, continue booting.
3244 */
3245 if (is_gzip_package((unsigned char *)(data + page_size), hdr->kernel_size))
3246 {
3247 out_addr = (unsigned char *)target_get_scratch_address();
3248 out_addr = (unsigned char *)(out_addr + image_actual + page_size);
3249 out_avai_len = target_get_max_flash_size() - image_actual - page_size;
Saranya Chidurab4933332018-10-15 17:30:06 +05303250#if VERIFIED_BOOT_2
3251 if (dtbo_image_sz)
3252 out_avai_len -= DTBO_IMG_BUF;
3253#endif
Matthew Qin0b15b322015-05-19 05:20:54 -04003254 dprintf(INFO, "decompressing kernel image: start\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08003255 ret = decompress((unsigned char *)(ptr + page_size),
3256 hdr->kernel_size, out_addr, out_avai_len,
3257 &dtb_offset, &out_len);
3258 if (ret)
3259 {
Matthew Qin0b15b322015-05-19 05:20:54 -04003260 dprintf(CRITICAL, "decompressing image failed!!!\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08003261 ASSERT(0);
3262 }
3263
Matthew Qin0b15b322015-05-19 05:20:54 -04003264 dprintf(INFO, "decompressing kernel image: done\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08003265 kptr = (struct kernel64_hdr *)out_addr;
3266 kernel_start_addr = out_addr;
3267 kernel_size = out_len;
3268 } else {
3269 kptr = (struct kernel64_hdr*)((char *)data + page_size);
3270 kernel_start_addr = (unsigned char *)((char *)data + page_size);
3271 kernel_size = hdr->kernel_size;
3272 }
3273
3274 /*
Channagoud Kadabia22144f2013-03-20 11:49:01 -07003275 * Update the kernel/ramdisk/tags address if the boot image header
3276 * has default values, these default values come from mkbootimg when
3277 * the boot image is flashed using fastboot flash:raw
3278 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08003279 update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr));
Dima Zavin3cadfff2013-03-21 14:30:48 -07003280
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07003281 /* Get virtual addresses since the hdr saves physical addresses. */
3282 hdr->kernel_addr = VA(hdr->kernel_addr);
3283 hdr->ramdisk_addr = VA(hdr->ramdisk_addr);
3284 hdr->tags_addr = VA(hdr->tags_addr);
Brian Swetland9c4c0752009-01-25 16:23:50 -08003285
Matthew Qinbb7923d2015-02-09 10:56:09 +08003286 kernel_size = ROUND_TO_PAGE(kernel_size, page_mask);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07003287 /* Check if the addresses in the header are valid. */
Matthew Qinbb7923d2015-02-09 10:56:09 +08003288 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_size) ||
Mayank Grovere559cec2017-10-17 15:12:03 +05303289 check_ddr_addr_range_bound(hdr->kernel_addr, kernel_size) ||
3290 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual) ||
3291 check_ddr_addr_range_bound(hdr->ramdisk_addr, ramdisk_actual))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07003292 {
Mayank Grovere559cec2017-10-17 15:12:03 +05303293 dprintf(CRITICAL, "kernel/ramdisk addresses are not valid.\n");
lijuang2008ff22016-03-07 17:56:27 +08003294 goto boot_failed;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07003295 }
3296
Amol Jadicb524072012-08-09 16:40:18 -07003297#if DEVICE_TREE
Matthew Qin271927e2015-03-31 22:07:07 -04003298 scratch_offset = image_actual + page_size + out_len;
Amol Jadicb524072012-08-09 16:40:18 -07003299 /* find correct dtb and copy it to right location */
Matthew Qin271927e2015-03-31 22:07:07 -04003300 ret = copy_dtb(data, scratch_offset);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07003301
3302 dtb_copied = !ret ? 1 : 0;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07003303#else
Mayank Grovere559cec2017-10-17 15:12:03 +05303304 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE) ||
3305 check_ddr_addr_range_bound(hdr->tags_addr, MAX_TAGS_SIZE))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07003306 {
Mayank Grovere559cec2017-10-17 15:12:03 +05303307 dprintf(CRITICAL, "Tags addresses are not valid.\n");
lijuang2008ff22016-03-07 17:56:27 +08003308 goto boot_failed;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07003309 }
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07003310#endif
3311
3312 /* Load ramdisk & kernel */
3313 memmove((void*) hdr->ramdisk_addr, ptr + page_size + kernel_actual, hdr->ramdisk_size);
Matthew Qinbb7923d2015-02-09 10:56:09 +08003314 memmove((void*) hdr->kernel_addr, (char*) (kernel_start_addr), kernel_size);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07003315
3316#if DEVICE_TREE
Mayank Grovere559cec2017-10-17 15:12:03 +05303317 if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual) ||
3318 check_ddr_addr_range_bound(hdr->tags_addr, kernel_actual))
Matthew Qinbb7923d2015-02-09 10:56:09 +08003319 {
Mayank Grovere559cec2017-10-17 15:12:03 +05303320 dprintf(CRITICAL, "Tags addresses are not valid.\n");
lijuang2008ff22016-03-07 17:56:27 +08003321 goto boot_failed;
Matthew Qinbb7923d2015-02-09 10:56:09 +08003322 }
3323
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07003324 /*
3325 * If dtb is not found look for appended DTB in the kernel.
3326 * If appended dev tree is found, update the atags with
3327 * memory address to the DTB appended location on RAM.
3328 * Else update with the atags address in the kernel header
3329 */
3330 if (!dtb_copied) {
3331 void *dtb;
Matthew Qinbb7923d2015-02-09 10:56:09 +08003332 dtb = dev_tree_appended((void*)(ptr + page_size),
3333 hdr->kernel_size, dtb_offset,
Dima Zavine63e5572013-05-03 12:23:06 -07003334 (void *)hdr->tags_addr);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07003335 if (!dtb) {
3336 fastboot_fail("dtb not found");
lijuang2008ff22016-03-07 17:56:27 +08003337 goto boot_failed;
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07003338 }
Amol Jadicb524072012-08-09 16:40:18 -07003339 }
3340#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08003341
3342 fastboot_okay("");
Amol Jadi7c4316c2013-10-07 14:19:26 -07003343 fastboot_stop();
Brian Swetland9c4c0752009-01-25 16:23:50 -08003344
Dima Zavin77e41f32013-03-06 16:10:43 -08003345 boot_linux((void*) hdr->kernel_addr, (void*) hdr->tags_addr,
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07003346 (const char*) hdr->cmdline, board_machtype(),
3347 (void*) hdr->ramdisk_addr, hdr->ramdisk_size);
lijuang2008ff22016-03-07 17:56:27 +08003348
3349 /* fastboot already stop, it's no need to show fastboot menu */
3350 return;
3351boot_failed:
3352#if FBCON_DISPLAY_MSG
3353 /* revert to fastboot menu if boot failed */
3354 display_fastboot_menu();
3355#endif
3356 return;
Brian Swetland9c4c0752009-01-25 16:23:50 -08003357}
3358
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003359void cmd_erase_nand(const char *arg, void *data, unsigned sz)
Dima Zavin214cc642009-01-26 11:16:21 -08003360{
3361 struct ptentry *ptn;
3362 struct ptable *ptable;
3363
3364 ptable = flash_get_ptable();
3365 if (ptable == NULL) {
3366 fastboot_fail("partition table doesn't exist");
3367 return;
3368 }
3369
3370 ptn = ptable_find(ptable, arg);
3371 if (ptn == NULL) {
3372 fastboot_fail("unknown partition name");
3373 return;
3374 }
3375
Monika Singh292b3e92018-03-17 22:40:23 +05303376 if (!strncmp(arg, "avb_custom_key", strlen("avb_custom_key"))) {
3377 dprintf(INFO, "erasing avb_custom_key\n");
3378 if (erase_userkey()) {
3379 fastboot_fail("Erasing avb_custom_key failed");
3380 } else {
3381 fastboot_okay("");
3382 }
3383 return;
3384 }
3385
Dima Zavin214cc642009-01-26 11:16:21 -08003386 if (flash_erase(ptn)) {
3387 fastboot_fail("failed to erase partition");
3388 return;
3389 }
3390 fastboot_okay("");
3391}
3392
Bikas Gurungd48bd242010-09-04 19:54:32 -07003393
3394void cmd_erase_mmc(const char *arg, void *data, unsigned sz)
3395{
3396 unsigned long long ptn = 0;
Oliver Wangcee448d2013-10-22 18:40:13 +08003397 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07003398 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003399 uint8_t lun = 0;
Vijay Kumar Pendoti0b21f462016-05-02 17:09:18 +05303400 char *footer = NULL;
Bikas Gurungd48bd242010-09-04 19:54:32 -07003401
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003402#if VERIFIED_BOOT
3403 if(!strcmp(arg, KEYSTORE_PTN_NAME))
3404 {
3405 if(!device.is_unlocked)
3406 {
3407 fastboot_fail("unlock device to erase keystore");
3408 return;
3409 }
3410 }
3411#endif
3412
Kinson Chikf1a43512011-07-14 11:28:39 -07003413 index = partition_get_index(arg);
3414 ptn = partition_get_offset(index);
Oliver Wangcee448d2013-10-22 18:40:13 +08003415 size = partition_get_size(index);
Neeti Desaica8c9602011-10-06 11:40:00 -07003416
Monika Singhc4778b72018-05-16 11:16:42 +05303417 if (!strncmp(arg, "avb_custom_key", strlen("avb_custom_key"))) {
3418 dprintf(INFO, "erasing avb_custom_key\n");
3419 if (erase_userkey()) {
3420 fastboot_fail("Erasing avb_custom_key failed");
3421 } else {
3422 fastboot_okay("");
3423 }
3424 return;
3425 }
3426
Kinson Chikf1a43512011-07-14 11:28:39 -07003427 if(ptn == 0) {
Neeti Desaica8c9602011-10-06 11:40:00 -07003428 fastboot_fail("Partition table doesn't exist\n");
Bikas Gurungd48bd242010-09-04 19:54:32 -07003429 return;
3430 }
Kun Liang2f1601a2013-08-12 16:29:54 +08003431
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003432 lun = partition_get_lun(index);
3433 mmc_set_lun(lun);
3434
Sridhar Parasuramd7957122015-02-27 11:33:40 -08003435 if (platform_boot_dev_isemmc())
3436 {
3437 if (mmc_erase_card(ptn, size)) {
3438 fastboot_fail("failed to erase partition\n");
3439 return;
3440 }
3441 } else {
3442 BUF_DMA_ALIGN(out, DEFAULT_ERASE_SIZE);
3443 size = partition_get_size(index);
3444 if (size > DEFAULT_ERASE_SIZE)
3445 size = DEFAULT_ERASE_SIZE;
Kun Liang2f1601a2013-08-12 16:29:54 +08003446
Sridhar Parasuramd7957122015-02-27 11:33:40 -08003447 /* Simple inefficient version of erase. Just writing
3448 0 in first several blocks */
3449 if (mmc_write(ptn , size, (unsigned int *)out)) {
3450 fastboot_fail("failed to erase partition");
3451 return;
3452 }
Vijay Kumar Pendoti0b21f462016-05-02 17:09:18 +05303453 /*Erase FDE metadata at the userdata footer*/
3454 if(!(strncmp(arg, "userdata", 8)))
3455 {
3456 footer = memalign(CACHE_LINE, FOOTER_SIZE);
3457 memset((void *)footer, 0, FOOTER_SIZE);
3458
3459 size = partition_get_size(index);
3460
3461 if (mmc_write((ptn + size) - FOOTER_SIZE , FOOTER_SIZE, (unsigned int *)footer)) {
3462 fastboot_fail("failed to erase userdata footer");
3463 free(footer);
3464 return;
3465 }
3466 free(footer);
3467 }
Bikas Gurungd48bd242010-09-04 19:54:32 -07003468 }
Monika Singh292b3e92018-03-17 22:40:23 +05303469#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover912eaa62017-10-26 12:08:53 +05303470 if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05303471 !(strncmp(arg, "userdata", 8)) &&
3472 send_delete_keys_to_tz())
Sridhar Parasuram32b30662015-07-10 13:33:22 -07003473 ASSERT(0);
3474#endif
Bikas Gurungd48bd242010-09-04 19:54:32 -07003475 fastboot_okay("");
3476}
3477
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003478void cmd_erase(const char *arg, void *data, unsigned sz)
3479{
Monika Singh292b3e92018-03-17 22:40:23 +05303480#if VERIFIED_BOOT || VERIFIED_BOOT_2
Channagoud Kadabi35297672015-06-13 11:09:49 -07003481 if (target_build_variant_user())
Channagoud Kadabice3cf422015-04-17 16:02:30 -07003482 {
Sridhar Parasuramc32d07d2015-07-12 10:57:48 -07003483 if(!device.is_unlocked)
Channagoud Kadabice3cf422015-04-17 16:02:30 -07003484 {
Channagoud Kadabi35297672015-06-13 11:09:49 -07003485 fastboot_fail("device is locked. Cannot erase");
Channagoud Kadabice3cf422015-04-17 16:02:30 -07003486 return;
3487 }
3488 }
3489#endif
3490
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003491 if(target_is_emmc_boot())
3492 cmd_erase_mmc(arg, data, sz);
3493 else
3494 cmd_erase_nand(arg, data, sz);
3495}
Bikas Gurungd48bd242010-09-04 19:54:32 -07003496
Mayank Grover11ff9692018-01-11 11:54:49 +05303497/* Get the size from partiton name */
3498static void get_partition_size(const char *arg, char *response)
3499{
3500 uint64_t ptn = 0;
3501 uint64_t size;
3502 int index = INVALID_PTN;
3503
3504 index = partition_get_index(arg);
3505
3506 if (index == INVALID_PTN)
3507 {
3508 dprintf(CRITICAL, "Invalid partition index\n");
3509 return;
3510 }
3511
3512 ptn = partition_get_offset(index);
3513
3514 if(!ptn)
3515 {
3516 dprintf(CRITICAL, "Invalid partition name %s\n", arg);
3517 return;
3518 }
3519
3520 size = partition_get_size(index);
3521
3522 snprintf(response, MAX_RSP_SIZE, "\t 0x%llx", size);
3523 return;
3524}
3525
Mayank Grover52cd10a2018-03-15 12:57:54 +05303526/* Function to check partition type of a partition*/
3527static fs_signature_type
3528check_partition_fs_signature(const char *arg)
3529{
3530 fs_signature_type ret = NO_FS;
3531 int index;
3532 unsigned long long ptn;
Mayank Grover399826a2018-08-27 12:15:15 +05303533 char *buffer = memalign(CACHE_LINE, mmc_blocksize);
3534 uint32_t sb_blk_offset = 0;
3535 char *sb_buffer = buffer;
3536
Mayank Grover52cd10a2018-03-15 12:57:54 +05303537 if (!sb_buffer)
3538 {
3539 dprintf(CRITICAL, "ERROR: Failed to allocate buffer for superblock\n");
3540 goto out;
3541 }
3542
3543 /* Read super block */
3544 if ((index = partition_get_index(arg)) < 0)
3545 {
3546 dprintf(CRITICAL, "ERROR: %s() doesn't exsit\n", arg);
3547 goto out;
3548 }
3549 ptn = partition_get_offset(index);
3550 mmc_set_lun(partition_get_lun(index));
Mayank Grover399826a2018-08-27 12:15:15 +05303551 sb_blk_offset = (FS_SUPERBLOCK_OFFSET/mmc_blocksize);
3552
3553 if(mmc_read(ptn + (sb_blk_offset * mmc_blocksize),
Mayank Grover52cd10a2018-03-15 12:57:54 +05303554 (void *)sb_buffer, mmc_blocksize))
3555 {
3556 dprintf(CRITICAL, "ERROR: Failed to read Superblock\n");
3557 goto out;
3558 }
3559
Mayank Grover399826a2018-08-27 12:15:15 +05303560 if (sb_blk_offset == 0)
3561 sb_buffer += FS_SUPERBLOCK_OFFSET;
3562
3563 if (*((uint16 *)(&sb_buffer[EXT_MAGIC_OFFSET_SB])) == (uint16)EXT_MAGIC)
Mayank Grover52cd10a2018-03-15 12:57:54 +05303564 {
3565 dprintf(SPEW, "%s() Found EXT FS\n", arg);
3566 ret = EXT_FS_SIGNATURE;
3567 }
Mayank Grover399826a2018-08-27 12:15:15 +05303568 else if (*((uint32 *)(&sb_buffer[F2FS_MAGIC_OFFSET_SB])) == F2FS_MAGIC)
Mayank Grover52cd10a2018-03-15 12:57:54 +05303569 {
3570 dprintf(SPEW, "%s() Found F2FS FS\n", arg);
3571 ret = EXT_F2FS_SIGNATURE;
3572 }
3573 else
3574 {
3575 dprintf(SPEW, "%s() Reverting to default 0x%x\n",
3576 arg, *((uint16 *)(&sb_buffer[EXT_MAGIC_OFFSET_SB])));
3577 ret = NO_FS;
3578 }
3579
3580out:
Mayank Grover399826a2018-08-27 12:15:15 +05303581 if(buffer)
3582 free(buffer);
Mayank Grover52cd10a2018-03-15 12:57:54 +05303583 return ret;
3584}
3585
Mayank Groverd38fe012018-03-13 15:33:16 +05303586/* Function to get partition type */
3587static void get_partition_type(const char *arg, char *response)
3588{
3589 uint n = 0;
Mayank Grover52cd10a2018-03-15 12:57:54 +05303590 fs_signature_type fs_signature;
Mayank Groverd38fe012018-03-13 15:33:16 +05303591
3592 if (arg == NULL ||
3593 response == NULL)
3594 {
3595 dprintf(CRITICAL, "Invalid input parameter\n");
3596 return;
3597 }
3598
3599 /* By default copy raw to response */
Mayank Grover0e559042018-04-11 17:49:30 +05303600 strlcpy(response, RAW_STR, MAX_RSP_SIZE);
Mayank Groverd38fe012018-03-13 15:33:16 +05303601
3602 /* Mark partiton type for known paritions only */
3603 for (n=0; n < ARRAY_SIZE(part_type_known); n++)
3604 {
3605 if (!strncmp(part_type_known[n].part_name, arg, strlen(arg)))
3606 {
Mayank Grover52cd10a2018-03-15 12:57:54 +05303607 /* Check partition for FS signature */
3608 fs_signature = check_partition_fs_signature(arg);
3609 switch (fs_signature)
3610 {
3611 case EXT_FS_SIGNATURE:
Mayank Grover0e559042018-04-11 17:49:30 +05303612 strlcpy(response, EXT_STR, MAX_RSP_SIZE);
Mayank Grover52cd10a2018-03-15 12:57:54 +05303613 break;
3614 case EXT_F2FS_SIGNATURE:
Mayank Grover0e559042018-04-11 17:49:30 +05303615 strlcpy(response, F2FS_STR, MAX_RSP_SIZE);
Mayank Grover52cd10a2018-03-15 12:57:54 +05303616 break;
3617 case NO_FS:
Mayank Grover0e559042018-04-11 17:49:30 +05303618 strlcpy(response, part_type_known[n].type_response, MAX_RSP_SIZE);
Mayank Grover52cd10a2018-03-15 12:57:54 +05303619 }
Mayank Groverd38fe012018-03-13 15:33:16 +05303620 }
3621 }
3622 return;
3623}
3624
Mayank Grover11ff9692018-01-11 11:54:49 +05303625/*
3626 * Publish the partition type & size info
3627 * fastboot getvar will publish the required information.
3628 * fastboot getvar partition_size:<partition_name>: partition size in hex
3629 * fastboot getvar partition_type:<partition_name>: partition type (ext/fat)
3630 */
3631static void publish_getvar_partition_info(struct getvar_partition_info *info, uint8_t num_parts)
3632{
Mayank Groverd38fe012018-03-13 15:33:16 +05303633 uint8_t i;
Mayank Grover11ff9692018-01-11 11:54:49 +05303634 static bool published = false;
3635 struct partition_entry *ptn_entry =
3636 partition_get_partition_entries();
3637 memset(info, 0, sizeof(struct getvar_partition_info)* num_parts);
3638
3639 for (i = 0; i < num_parts; i++) {
3640 strlcat(info[i].part_name, (const char *)ptn_entry[i].name, MAX_RSP_SIZE);
3641 strlcat(info[i].getvar_size, "partition-size:", MAX_GET_VAR_NAME_SIZE);
3642 strlcat(info[i].getvar_type, "partition-type:", MAX_GET_VAR_NAME_SIZE);
3643
Mayank Groverd38fe012018-03-13 15:33:16 +05303644 get_partition_type(info[i].part_name, info[i].type_response);
Mayank Grover11ff9692018-01-11 11:54:49 +05303645 get_partition_size(info[i].part_name, info[i].size_response);
Mayank Grover11ff9692018-01-11 11:54:49 +05303646 if (strlcat(info[i].getvar_size, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
3647 {
3648 dprintf(CRITICAL, "partition size name truncated\n");
3649 return;
3650 }
3651 if (strlcat(info[i].getvar_type, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
3652 {
3653 dprintf(CRITICAL, "partition type name truncated\n");
3654 return;
3655 }
3656
3657 if (!published)
3658 {
3659 /* publish partition size & type info */
3660 fastboot_publish((const char *) info[i].getvar_size, (const char *) info[i].size_response);
3661 fastboot_publish((const char *) info[i].getvar_type, (const char *) info[i].type_response);
3662 }
3663 }
3664 if (!published)
3665 published = true;
3666}
3667
3668
Ajay Dudani5c761132011-04-07 20:19:04 -07003669void cmd_flash_mmc_img(const char *arg, void *data, unsigned sz)
Shashank Mittal23b8f422010-04-16 19:27:21 -07003670{
3671 unsigned long long ptn = 0;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07003672 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07003673 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003674 char *token = NULL;
3675 char *pname = NULL;
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07003676 char *sp;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003677 uint8_t lun = 0;
3678 bool lun_set = false;
Mayank Grover351a75e2017-05-30 20:06:08 +05303679 int current_active_slot = INVALID;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07003680
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07003681 token = strtok_r((char *)arg, ":", &sp);
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003682 pname = token;
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07003683 token = strtok_r(NULL, ":", &sp);
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003684 if(token)
3685 {
3686 lun = atoi(token);
3687 mmc_set_lun(lun);
3688 lun_set = true;
3689 }
3690
Mao Jinlong226f33a2014-07-04 17:24:10 +08003691 if (pname)
Greg Grisco6e754772011-06-23 12:19:39 -07003692 {
Channagoud Kadabiad259832015-05-29 11:14:17 -07003693 if (!strncmp(pname, "frp-unlock", strlen("frp-unlock")))
3694 {
3695 if (!aboot_frp_unlock(pname, data, sz))
3696 {
3697 fastboot_info("FRP unlock successful");
3698 fastboot_okay("");
3699 }
3700 else
3701 fastboot_fail("Secret key is invalid, please update the bootloader with secret key");
3702
3703 return;
3704 }
3705
Mao Jinlong226f33a2014-07-04 17:24:10 +08003706 if (!strcmp(pname, "partition"))
3707 {
3708 dprintf(INFO, "Attempt to write partition image.\n");
3709 if (write_partition(sz, (unsigned char *) data)) {
3710 fastboot_fail("failed to write partition");
Greg Grisco6e754772011-06-23 12:19:39 -07003711 return;
3712 }
Mayank Grover11ff9692018-01-11 11:54:49 +05303713 /* Re-publish partition table */
3714 publish_getvar_partition_info(part_info, partition_get_partition_count());
Mayank Grover351a75e2017-05-30 20:06:08 +05303715
3716 /* Rescan partition table to ensure we have multislot support*/
3717 if (partition_scan_for_multislot())
3718 {
3719 current_active_slot = partition_find_active_slot();
3720 dprintf(INFO, "Multislot supported: Slot %s active",
3721 (SUFFIX_SLOT(current_active_slot)));
3722 }
3723 partition_mark_active_slot(current_active_slot);
Greg Grisco6e754772011-06-23 12:19:39 -07003724 }
Mao Jinlong226f33a2014-07-04 17:24:10 +08003725 else
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003726 {
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003727#if VERIFIED_BOOT
3728 if(!strcmp(pname, KEYSTORE_PTN_NAME))
3729 {
3730 if(!device.is_unlocked)
3731 {
3732 fastboot_fail("unlock device to flash keystore");
3733 return;
3734 }
Gaurav Nebhwanic7313cc2015-12-15 22:25:04 +05303735 if(!boot_verify_validate_keystore((unsigned char *)data,sz))
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003736 {
3737 fastboot_fail("image is not a keystore file");
3738 return;
3739 }
3740 }
3741#endif
Mao Jinlong226f33a2014-07-04 17:24:10 +08003742 index = partition_get_index(pname);
3743 ptn = partition_get_offset(index);
3744 if(ptn == 0) {
3745 fastboot_fail("partition table doesn't exist");
3746 return;
3747 }
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003748
Mayank Grover351a75e2017-05-30 20:06:08 +05303749 if (!strncmp(pname, "boot", strlen("boot"))
3750 || !strcmp(pname, "recovery"))
3751 {
Ashish Bhimanpalliwar84f7db92020-09-23 11:59:16 +05303752 if ((sz < BOOT_MAGIC_SIZE) || memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Mao Jinlong226f33a2014-07-04 17:24:10 +08003753 fastboot_fail("image is not a boot image");
3754 return;
3755 }
Mayank Grover351a75e2017-05-30 20:06:08 +05303756
3757 /* Reset multislot_partition attributes in case of flashing boot */
3758 if (partition_multislot_is_supported())
3759 {
3760 partition_reset_attributes(index);
3761 }
Mao Jinlong226f33a2014-07-04 17:24:10 +08003762 }
3763
3764 if(!lun_set)
3765 {
3766 lun = partition_get_lun(index);
3767 mmc_set_lun(lun);
3768 }
3769
3770 size = partition_get_size(index);
Gaurav Nebhwani4d2389c2016-03-17 21:10:05 +05303771 if (ROUND_TO_PAGE(sz, mmc_blocksize_mask) > size) {
Mao Jinlong226f33a2014-07-04 17:24:10 +08003772 fastboot_fail("size too large");
3773 return;
3774 }
3775 else if (mmc_write(ptn , sz, (unsigned int *)data)) {
3776 fastboot_fail("flash write failure");
3777 return;
3778 }
Greg Grisco6e754772011-06-23 12:19:39 -07003779 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07003780 }
3781 fastboot_okay("");
3782 return;
3783}
3784
Ajay Dudanide984792015-03-02 09:57:41 -08003785void cmd_flash_meta_img(const char *arg, void *data, unsigned sz)
3786{
3787 int i, images;
3788 meta_header_t *meta_header;
3789 img_header_entry_t *img_header_entry;
Parth Dixit3e2d3032016-03-04 17:11:52 +05303790 /*End of the image address*/
3791 uintptr_t data_end;
3792
3793 if( (UINT_MAX - sz) > (uintptr_t)data )
3794 data_end = (uintptr_t)data + sz;
3795 else
3796 {
3797 fastboot_fail("Cannot flash: image header corrupt");
3798 return;
3799 }
3800
Ashish Bhimanpalliwar21494e12020-10-07 13:24:03 +05303801 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 +05303802 {
3803 fastboot_fail("Cannot flash: image header corrupt");
3804 return;
3805 }
Ajay Dudanide984792015-03-02 09:57:41 -08003806
lijuang8ee21882016-04-19 16:57:11 +08003807 /* If device is locked:
3808 * Forbid to flash image to avoid the device to bypass the image
3809 * which with "any" name other than bootloader. Because it maybe
3810 * a meta package of all partitions.
3811 */
Monika Singh292b3e92018-03-17 22:40:23 +05303812#if VERIFIED_BOOT || VERIFIED_BOOT_2
lijuang8ee21882016-04-19 16:57:11 +08003813 if (target_build_variant_user()) {
3814 if (!device.is_unlocked) {
3815 fastboot_fail("Device is locked, meta image flashing is not allowed");
3816 return;
3817 }
Mayank Grover889be1b2017-09-12 20:12:23 +05303818
Mayank Grover912eaa62017-10-26 12:08:53 +05303819 if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05303820 !device.is_unlock_critical)
3821 {
lijuang8ee21882016-04-19 16:57:11 +08003822 fastboot_fail("Device is critical locked, Meta image flashing is not allowed");
3823 return;
3824 }
lijuang8ee21882016-04-19 16:57:11 +08003825 }
3826#endif
3827
Ajay Dudanide984792015-03-02 09:57:41 -08003828 meta_header = (meta_header_t*) data;
Ashish Bhimanpalliwar21494e12020-10-07 13:24:03 +05303829 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 +05303830 {
3831 fastboot_fail("Cannot flash: image header corrupt");
3832 return;
3833 }
Ajay Dudanide984792015-03-02 09:57:41 -08003834 img_header_entry = (img_header_entry_t*) (data+sizeof(meta_header_t));
3835
3836 images = meta_header->img_hdr_sz / sizeof(img_header_entry_t);
3837
3838 for (i=0; i<images; i++) {
3839
3840 if((img_header_entry[i].ptn_name == NULL) ||
3841 (img_header_entry[i].start_offset == 0) ||
3842 (img_header_entry[i].size == 0))
3843 break;
Kishor PK49831502017-04-21 17:55:43 +05303844 if ((UINT_MAX - img_header_entry[i].start_offset) < (uintptr_t)data) {
3845 fastboot_fail("Integer overflow detected in start_offset of img");
3846 break;
3847 }
3848 else if ((UINT_MAX - (img_header_entry[i].start_offset + (uintptr_t)data)) < img_header_entry[i].size) {
3849 fastboot_fail("Integer overflow detected in size of img");
3850 break;
3851 }
Parth Dixit3e2d3032016-03-04 17:11:52 +05303852 if( data_end < ((uintptr_t)data + img_header_entry[i].start_offset
3853 + img_header_entry[i].size) )
3854 {
3855 fastboot_fail("Cannot flash: image size mismatch");
3856 break;
3857 }
3858
Ajay Dudanide984792015-03-02 09:57:41 -08003859 cmd_flash_mmc_img(img_header_entry[i].ptn_name,
3860 (void *) data + img_header_entry[i].start_offset,
3861 img_header_entry[i].size);
3862 }
3863
Channagoud Kadabiaafbbfc2015-03-04 17:52:56 -08003864 if (!strncmp(arg, "bootloader", strlen("bootloader")))
3865 {
3866 strlcpy(device.bootloader_version, TARGET(BOARD), MAX_VERSION_LEN);
3867 strlcat(device.bootloader_version, "-", MAX_VERSION_LEN);
3868 strlcat(device.bootloader_version, meta_header->img_version, MAX_VERSION_LEN);
3869 }
3870 else
3871 {
3872 strlcpy(device.radio_version, TARGET(BOARD), MAX_VERSION_LEN);
3873 strlcat(device.radio_version, "-", MAX_VERSION_LEN);
3874 strlcat(device.radio_version, meta_header->img_version, MAX_VERSION_LEN);
3875 }
3876
3877 write_device_info(&device);
Ajay Dudanide984792015-03-02 09:57:41 -08003878 fastboot_okay("");
3879 return;
3880}
3881
Ajay Dudani5c761132011-04-07 20:19:04 -07003882void cmd_flash_mmc_sparse_img(const char *arg, void *data, unsigned sz)
3883{
3884 unsigned int chunk;
wufeng.jiang813dc352015-06-02 23:02:46 -04003885 uint64_t chunk_data_sz;
Channagoud Kadabif627cf82013-09-09 14:08:20 -07003886 uint32_t *fill_buf = NULL;
3887 uint32_t fill_val;
Mayank Grover4f50bba2017-07-19 18:17:08 +05303888 uint32_t blk_sz_actual = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07003889 sparse_header_t *sparse_header;
3890 chunk_header_t *chunk_header;
Ajay Dudaniab18f022011-05-12 14:39:22 -07003891 uint32_t total_blocks = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07003892 unsigned long long ptn = 0;
Channagoud Kadabi65b91002011-10-11 17:34:33 +05303893 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07003894 int index = INVALID_PTN;
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05303895 uint32_t i;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003896 uint8_t lun = 0;
vijay kumar800255e2015-04-24 20:26:19 +05303897 /*End of the sparse image address*/
Parth Dixit64b1a482016-03-07 16:31:26 +05303898 uintptr_t data_end = (uintptr_t)data + sz;
Ajay Dudani5c761132011-04-07 20:19:04 -07003899
Kinson Chikf1a43512011-07-14 11:28:39 -07003900 index = partition_get_index(arg);
3901 ptn = partition_get_offset(index);
3902 if(ptn == 0) {
Ajay Dudani5c761132011-04-07 20:19:04 -07003903 fastboot_fail("partition table doesn't exist");
3904 return;
3905 }
3906
Channagoud Kadabi65b91002011-10-11 17:34:33 +05303907 size = partition_get_size(index);
Channagoud Kadabi65b91002011-10-11 17:34:33 +05303908
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003909 lun = partition_get_lun(index);
3910 mmc_set_lun(lun);
3911
vijay kumar800255e2015-04-24 20:26:19 +05303912 if (sz < sizeof(sparse_header_t)) {
3913 fastboot_fail("size too low");
3914 return;
3915 }
3916
Ajay Dudani5c761132011-04-07 20:19:04 -07003917 /* Read and skip over sparse image header */
3918 sparse_header = (sparse_header_t *) data;
vijay kumar800255e2015-04-24 20:26:19 +05303919
Mayank Grover48bd9bb2017-07-19 12:04:16 +05303920 if (!sparse_header->blk_sz || (sparse_header->blk_sz % 4)){
3921 fastboot_fail("Invalid block size\n");
3922 return;
3923 }
3924
vijay kumar1321f342015-03-27 12:13:42 +05303925 if (((uint64_t)sparse_header->total_blks * (uint64_t)sparse_header->blk_sz) > size) {
Ajay Dudani876b3282012-12-21 14:12:17 -08003926 fastboot_fail("size too large");
3927 return;
3928 }
3929
vijay kumarde4fcf62015-04-23 13:05:49 +05303930 data += sizeof(sparse_header_t);
vijay kumar800255e2015-04-24 20:26:19 +05303931
Parth Dixit64b1a482016-03-07 16:31:26 +05303932 if (data_end < (uintptr_t)data) {
vijay kumar800255e2015-04-24 20:26:19 +05303933 fastboot_fail("buffer overreads occured due to invalid sparse header");
3934 return;
3935 }
3936
vijay kumarde4fcf62015-04-23 13:05:49 +05303937 if(sparse_header->file_hdr_sz != sizeof(sparse_header_t))
Ajay Dudani5c761132011-04-07 20:19:04 -07003938 {
vijay kumarde4fcf62015-04-23 13:05:49 +05303939 fastboot_fail("sparse header size mismatch");
3940 return;
Ajay Dudani5c761132011-04-07 20:19:04 -07003941 }
3942
Ajay Dudanib06c05f2011-05-12 14:46:10 -07003943 dprintf (SPEW, "=== Sparse Image Header ===\n");
3944 dprintf (SPEW, "magic: 0x%x\n", sparse_header->magic);
3945 dprintf (SPEW, "major_version: 0x%x\n", sparse_header->major_version);
3946 dprintf (SPEW, "minor_version: 0x%x\n", sparse_header->minor_version);
3947 dprintf (SPEW, "file_hdr_sz: %d\n", sparse_header->file_hdr_sz);
3948 dprintf (SPEW, "chunk_hdr_sz: %d\n", sparse_header->chunk_hdr_sz);
3949 dprintf (SPEW, "blk_sz: %d\n", sparse_header->blk_sz);
3950 dprintf (SPEW, "total_blks: %d\n", sparse_header->total_blks);
3951 dprintf (SPEW, "total_chunks: %d\n", sparse_header->total_chunks);
Ajay Dudani5c761132011-04-07 20:19:04 -07003952
3953 /* Start processing chunks */
3954 for (chunk=0; chunk<sparse_header->total_chunks; chunk++)
3955 {
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05303956 /* Make sure the total image size does not exceed the partition size */
3957 if(((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz) >= size) {
3958 fastboot_fail("size too large");
3959 return;
3960 }
Ajay Dudani5c761132011-04-07 20:19:04 -07003961 /* Read and skip over chunk header */
3962 chunk_header = (chunk_header_t *) data;
3963 data += sizeof(chunk_header_t);
3964
Parth Dixit64b1a482016-03-07 16:31:26 +05303965 if (data_end < (uintptr_t)data) {
vijay kumar800255e2015-04-24 20:26:19 +05303966 fastboot_fail("buffer overreads occured due to invalid sparse header");
3967 return;
3968 }
3969
Ajay Dudani5c761132011-04-07 20:19:04 -07003970 dprintf (SPEW, "=== Chunk Header ===\n");
3971 dprintf (SPEW, "chunk_type: 0x%x\n", chunk_header->chunk_type);
3972 dprintf (SPEW, "chunk_data_sz: 0x%x\n", chunk_header->chunk_sz);
3973 dprintf (SPEW, "total_size: 0x%x\n", chunk_header->total_sz);
3974
vijay kumar800255e2015-04-24 20:26:19 +05303975 if(sparse_header->chunk_hdr_sz != sizeof(chunk_header_t))
Ajay Dudani5c761132011-04-07 20:19:04 -07003976 {
vijay kumar800255e2015-04-24 20:26:19 +05303977 fastboot_fail("chunk header size mismatch");
3978 return;
Ajay Dudani5c761132011-04-07 20:19:04 -07003979 }
3980
wufeng.jiang813dc352015-06-02 23:02:46 -04003981 chunk_data_sz = (uint64_t)sparse_header->blk_sz * chunk_header->chunk_sz;
3982
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05303983 /* Make sure that the chunk size calculated from sparse image does not
3984 * exceed partition size
3985 */
3986 if ((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz + chunk_data_sz > size)
3987 {
3988 fastboot_fail("Chunk data size exceeds partition size");
3989 return;
3990 }
3991
Ajay Dudani5c761132011-04-07 20:19:04 -07003992 switch (chunk_header->chunk_type)
3993 {
3994 case CHUNK_TYPE_RAW:
wufeng.jiang813dc352015-06-02 23:02:46 -04003995 if((uint64_t)chunk_header->total_sz != ((uint64_t)sparse_header->chunk_hdr_sz +
Ajay Dudani5c761132011-04-07 20:19:04 -07003996 chunk_data_sz))
3997 {
3998 fastboot_fail("Bogus chunk size for chunk type Raw");
3999 return;
4000 }
4001
Parth Dixit64b1a482016-03-07 16:31:26 +05304002 if (data_end < (uintptr_t)data + chunk_data_sz) {
vijay kumar800255e2015-04-24 20:26:19 +05304003 fastboot_fail("buffer overreads occured due to invalid sparse header");
4004 return;
4005 }
4006
wufeng.jiang813dc352015-06-02 23:02:46 -04004007 /* chunk_header->total_sz is uint32,So chunk_data_sz is now less than 2^32
4008 otherwise it will return in the line above
4009 */
Ajay Dudaniab18f022011-05-12 14:39:22 -07004010 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
wufeng.jiang813dc352015-06-02 23:02:46 -04004011 (uint32_t)chunk_data_sz,
Ajay Dudaniab18f022011-05-12 14:39:22 -07004012 (unsigned int*)data))
Ajay Dudani5c761132011-04-07 20:19:04 -07004013 {
4014 fastboot_fail("flash write failure");
4015 return;
4016 }
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05304017 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
4018 fastboot_fail("Bogus size for RAW chunk type");
4019 return;
4020 }
Ajay Dudani5c761132011-04-07 20:19:04 -07004021 total_blocks += chunk_header->chunk_sz;
wufeng.jiang813dc352015-06-02 23:02:46 -04004022 data += (uint32_t)chunk_data_sz;
Ajay Dudani5c761132011-04-07 20:19:04 -07004023 break;
4024
Channagoud Kadabif627cf82013-09-09 14:08:20 -07004025 case CHUNK_TYPE_FILL:
4026 if(chunk_header->total_sz != (sparse_header->chunk_hdr_sz +
4027 sizeof(uint32_t)))
4028 {
4029 fastboot_fail("Bogus chunk size for chunk type FILL");
4030 return;
4031 }
4032
Mayank Grover4f50bba2017-07-19 18:17:08 +05304033 blk_sz_actual = ROUNDUP(sparse_header->blk_sz, CACHE_LINE);
4034 /* Integer overflow detected */
4035 if (blk_sz_actual < sparse_header->blk_sz)
4036 {
4037 fastboot_fail("Invalid block size");
4038 return;
4039 }
4040
4041 fill_buf = (uint32_t *)memalign(CACHE_LINE, blk_sz_actual);
Channagoud Kadabif627cf82013-09-09 14:08:20 -07004042 if (!fill_buf)
4043 {
4044 fastboot_fail("Malloc failed for: CHUNK_TYPE_FILL");
4045 return;
4046 }
4047
Parth Dixit64b1a482016-03-07 16:31:26 +05304048 if (data_end < (uintptr_t)data + sizeof(uint32_t)) {
vijay kumar800255e2015-04-24 20:26:19 +05304049 fastboot_fail("buffer overreads occured due to invalid sparse header");
Gaurav Nebhwani163cbf82016-03-17 21:21:27 +05304050 free(fill_buf);
vijay kumar800255e2015-04-24 20:26:19 +05304051 return;
4052 }
Channagoud Kadabif627cf82013-09-09 14:08:20 -07004053 fill_val = *(uint32_t *)data;
4054 data = (char *) data + sizeof(uint32_t);
Channagoud Kadabif627cf82013-09-09 14:08:20 -07004055
4056 for (i = 0; i < (sparse_header->blk_sz / sizeof(fill_val)); i++)
4057 {
4058 fill_buf[i] = fill_val;
4059 }
4060
Gaurav Nebhwanie94cbe12016-03-17 21:16:34 +05304061 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz))
4062 {
4063 fastboot_fail("bogus size for chunk FILL type");
4064 free(fill_buf);
4065 return;
4066 }
4067
wufeng.jiang813dc352015-06-02 23:02:46 -04004068 for (i = 0; i < chunk_header->chunk_sz; i++)
Channagoud Kadabif627cf82013-09-09 14:08:20 -07004069 {
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05304070 /* Make sure that the data written to partition does not exceed partition size */
4071 if ((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz + sparse_header->blk_sz > size)
4072 {
4073 fastboot_fail("Chunk data size for fill type exceeds partition size");
Gaurav Nebhwani163cbf82016-03-17 21:21:27 +05304074 free(fill_buf);
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05304075 return;
4076 }
4077
Channagoud Kadabif627cf82013-09-09 14:08:20 -07004078 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
4079 sparse_header->blk_sz,
4080 fill_buf))
4081 {
4082 fastboot_fail("flash write failure");
4083 free(fill_buf);
4084 return;
4085 }
4086
4087 total_blocks++;
4088 }
4089
4090 free(fill_buf);
4091 break;
4092
Ajay Dudani5c761132011-04-07 20:19:04 -07004093 case CHUNK_TYPE_DONT_CARE:
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05304094 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
4095 fastboot_fail("bogus size for chunk DONT CARE type");
4096 return;
4097 }
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07004098 total_blocks += chunk_header->chunk_sz;
4099 break;
4100
Ajay Dudani5c761132011-04-07 20:19:04 -07004101 case CHUNK_TYPE_CRC:
4102 if(chunk_header->total_sz != sparse_header->chunk_hdr_sz)
4103 {
wufeng.jiang813dc352015-06-02 23:02:46 -04004104 fastboot_fail("Bogus chunk size for chunk type CRC");
Ajay Dudani5c761132011-04-07 20:19:04 -07004105 return;
4106 }
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05304107 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
4108 fastboot_fail("bogus size for chunk CRC type");
4109 return;
4110 }
Ajay Dudani5c761132011-04-07 20:19:04 -07004111 total_blocks += chunk_header->chunk_sz;
Parth Dixit64b1a482016-03-07 16:31:26 +05304112 if ((uintptr_t)data > UINT_MAX - chunk_data_sz) {
vijay kumar800255e2015-04-24 20:26:19 +05304113 fastboot_fail("integer overflow occured");
4114 return;
4115 }
wufeng.jiang813dc352015-06-02 23:02:46 -04004116 data += (uint32_t)chunk_data_sz;
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 }
Ajay Dudani5c761132011-04-07 20:19:04 -07004121 break;
4122
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07004123 default:
Channagoud Kadabif627cf82013-09-09 14:08:20 -07004124 dprintf(CRITICAL, "Unkown chunk type: %x\n",chunk_header->chunk_type);
Ajay Dudani5c761132011-04-07 20:19:04 -07004125 fastboot_fail("Unknown chunk type");
4126 return;
4127 }
4128 }
4129
Ajay Dudani0c6927b2011-05-18 11:12:16 -07004130 dprintf(INFO, "Wrote %d blocks, expected to write %d blocks\n",
4131 total_blocks, sparse_header->total_blks);
4132
4133 if(total_blocks != sparse_header->total_blks)
4134 {
4135 fastboot_fail("sparse image write failure");
4136 }
Ajay Dudani5c761132011-04-07 20:19:04 -07004137
4138 fastboot_okay("");
4139 return;
4140}
4141
4142void cmd_flash_mmc(const char *arg, void *data, unsigned sz)
4143{
4144 sparse_header_t *sparse_header;
Ajay Dudanide984792015-03-02 09:57:41 -08004145 meta_header_t *meta_header;
Ajay Dudani5c761132011-04-07 20:19:04 -07004146
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07004147#ifdef SSD_ENABLE
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -08004148 /* 8 Byte Magic + 2048 Byte xml + Encrypted Data */
4149 unsigned int *magic_number = (unsigned int *) data;
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08004150 int ret=0;
4151 uint32 major_version=0;
4152 uint32 minor_version=0;
4153
4154 ret = scm_svc_version(&major_version,&minor_version);
4155 if(!ret)
4156 {
4157 if(major_version >= 2)
4158 {
Amir Samuelovbb65ce02013-05-05 12:20:18 +03004159 if( !strcmp(arg, "ssd") || !strcmp(arg, "tqs") )
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08004160 {
4161 ret = encrypt_scm((uint32 **) &data, &sz);
4162 if (ret != 0) {
4163 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
4164 return;
4165 }
4166
Amir Samuelovbb65ce02013-05-05 12:20:18 +03004167 /* Protect only for SSD */
4168 if (!strcmp(arg, "ssd")) {
4169 ret = scm_protect_keystore((uint32 *) data, sz);
4170 if (ret != 0) {
4171 dprintf(CRITICAL, "ERROR: scm_protect_keystore Failed\n");
4172 return;
4173 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08004174 }
4175 }
4176 else
4177 {
4178 ret = decrypt_scm_v2((uint32 **) &data, &sz);
4179 if(ret != 0)
4180 {
4181 dprintf(CRITICAL,"ERROR: Decryption Failure\n");
4182 return;
4183 }
4184 }
4185 }
4186 else
4187 {
4188 if (magic_number[0] == DECRYPT_MAGIC_0 &&
4189 magic_number[1] == DECRYPT_MAGIC_1)
4190 {
4191 ret = decrypt_scm((uint32 **) &data, &sz);
4192 if (ret != 0) {
4193 dprintf(CRITICAL, "ERROR: Invalid secure image\n");
4194 return;
4195 }
4196 }
4197 else if (magic_number[0] == ENCRYPT_MAGIC_0 &&
4198 magic_number[1] == ENCRYPT_MAGIC_1)
4199 {
4200 ret = encrypt_scm((uint32 **) &data, &sz);
4201 if (ret != 0) {
4202 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
4203 return;
4204 }
4205 }
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07004206 }
4207 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08004208 else
Neeti Desai127b9e02012-03-20 16:11:23 -07004209 {
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08004210 dprintf(CRITICAL,"INVALID SVC Version\n");
4211 return;
Neeti Desai127b9e02012-03-20 16:11:23 -07004212 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08004213#endif /* SSD_ENABLE */
Neeti Desai127b9e02012-03-20 16:11:23 -07004214
Monika Singh292b3e92018-03-17 22:40:23 +05304215#if VERIFIED_BOOT || VERIFIED_BOOT_2
Channagoud Kadabi35297672015-06-13 11:09:49 -07004216 if (target_build_variant_user())
Shashank Mittald3e54dd2014-08-28 15:24:02 -07004217 {
lijuang511a2b52015-08-14 20:50:51 +08004218 /* if device is locked:
4219 * common partition will not allow to be flashed
4220 * critical partition will allow to flash image.
4221 */
4222 if(!device.is_unlocked && !critical_flash_allowed(arg)) {
4223 fastboot_fail("Partition flashing is not allowed");
4224 return;
4225 }
Mayank Grover889be1b2017-09-12 20:12:23 +05304226
lijuang511a2b52015-08-14 20:50:51 +08004227 /* if device critical is locked:
4228 * common partition will allow to be flashed
4229 * critical partition will not allow to flash image.
4230 */
Mayank Grover912eaa62017-10-26 12:08:53 +05304231 if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05304232 !device.is_unlock_critical &&
4233 critical_flash_allowed(arg)) {
4234 fastboot_fail("Critical partition flashing is not allowed");
4235 return;
Shashank Mittald3e54dd2014-08-28 15:24:02 -07004236 }
4237 }
4238#endif
Monika Singh292b3e92018-03-17 22:40:23 +05304239 if (!strncmp(arg, "avb_custom_key", strlen("avb_custom_key"))) {
4240 dprintf(INFO, "flashing avb_custom_key\n");
4241 if (store_userkey(data, sz)) {
4242 fastboot_fail("Flashing avb_custom_key failed");
4243 } else {
4244 fastboot_okay("");
4245 }
4246 return;
4247 }
Shashank Mittald3e54dd2014-08-28 15:24:02 -07004248
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07004249 sparse_header = (sparse_header_t *) data;
Ajay Dudanide984792015-03-02 09:57:41 -08004250 meta_header = (meta_header_t *) data;
4251 if (sparse_header->magic == SPARSE_HEADER_MAGIC)
Ajay Dudani5c761132011-04-07 20:19:04 -07004252 cmd_flash_mmc_sparse_img(arg, data, sz);
Ajay Dudanide984792015-03-02 09:57:41 -08004253 else if (meta_header->magic == META_HEADER_MAGIC)
4254 cmd_flash_meta_img(arg, data, sz);
4255 else
4256 cmd_flash_mmc_img(arg, data, sz);
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07004257
4258#if VERIFIED_BOOT
Mayank Grover912eaa62017-10-26 12:08:53 +05304259 if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05304260 (!strncmp(arg, "system", 6)) &&
4261 !device.verity_mode)
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07004262 // reset dm_verity mode to enforcing
4263 device.verity_mode = 1;
4264 write_device_info(&device);
Parth Dixitddbc7352015-10-18 03:13:31 +05304265#endif
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07004266
Ajay Dudani5c761132011-04-07 20:19:04 -07004267 return;
4268}
4269
Tanya Brokhman84eaaf62015-02-24 21:57:06 +02004270void cmd_updatevol(const char *vol_name, void *data, unsigned sz)
4271{
4272 struct ptentry *sys_ptn;
4273 struct ptable *ptable;
4274
4275 ptable = flash_get_ptable();
4276 if (ptable == NULL) {
4277 fastboot_fail("partition table doesn't exist");
4278 return;
4279 }
4280
4281 sys_ptn = ptable_find(ptable, "system");
4282 if (sys_ptn == NULL) {
4283 fastboot_fail("system partition not found");
4284 return;
4285 }
4286
4287 sz = ROUND_TO_PAGE(sz, page_mask);
4288 if (update_ubi_vol(sys_ptn, vol_name, data, sz))
4289 fastboot_fail("update_ubi_vol failed");
4290 else
4291 fastboot_okay("");
4292}
4293
Sridhar Parasurame94e8152014-10-24 14:06:03 -07004294void cmd_flash_nand(const char *arg, void *data, unsigned sz)
Dima Zavin214cc642009-01-26 11:16:21 -08004295{
4296 struct ptentry *ptn;
4297 struct ptable *ptable;
4298 unsigned extra = 0;
Gaurav Nebhwanid9730712016-05-06 14:28:29 +05304299 uint64_t partition_size = 0;
Vijay Kumar Pendotic6f22a72017-05-19 22:44:34 +05304300 unsigned bytes_to_round_page = 0;
4301 unsigned rounded_size = 0;
Dima Zavin214cc642009-01-26 11:16:21 -08004302
Kishor PK38ed93d2017-04-25 14:19:26 +05304303 if((uintptr_t)data > (UINT_MAX - sz)) {
4304 fastboot_fail("Cannot flash: image header corrupt");
4305 return;
4306 }
4307
Dima Zavin214cc642009-01-26 11:16:21 -08004308 ptable = flash_get_ptable();
4309 if (ptable == NULL) {
4310 fastboot_fail("partition table doesn't exist");
4311 return;
4312 }
4313
4314 ptn = ptable_find(ptable, arg);
4315 if (ptn == NULL) {
Tanya Brokhman84eaaf62015-02-24 21:57:06 +02004316 dprintf(INFO, "unknown partition name (%s). Trying updatevol\n",
4317 arg);
4318 cmd_updatevol(arg, data, sz);
Dima Zavin214cc642009-01-26 11:16:21 -08004319 return;
4320 }
4321
Monika Singh292b3e92018-03-17 22:40:23 +05304322 if (!strncmp(arg, "avb_custom_key", strlen("avb_custom_key"))) {
4323 dprintf(INFO, "flashing avb_custom_key\n");
4324 if (store_userkey(data, sz)) {
4325 fastboot_fail("Flashing avb_custom_key failed");
4326 } else {
4327 fastboot_okay("");
4328 }
4329 return;
4330 }
4331
Dima Zavin214cc642009-01-26 11:16:21 -08004332 if (!strcmp(ptn->name, "boot") || !strcmp(ptn->name, "recovery")) {
Kishor PK38ed93d2017-04-25 14:19:26 +05304333 if((sz > BOOT_MAGIC_SIZE) && (!memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE))) {
4334 dprintf(INFO, "Verified the BOOT_MAGIC in image header \n");
4335 } else {
4336 fastboot_fail("Image is not a boot image");
Dima Zavin214cc642009-01-26 11:16:21 -08004337 return;
4338 }
4339 }
4340
Amol Jadi5c61a952012-05-04 17:05:35 -07004341 if (!strcmp(ptn->name, "system")
Deepa Dinamani13e32c42012-03-12 14:34:17 -07004342 || !strcmp(ptn->name, "userdata")
4343 || !strcmp(ptn->name, "persist")
Sundarajan Srinivasanb063a852013-11-19 14:02:27 -08004344 || !strcmp(ptn->name, "recoveryfs")
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -08004345 || !strcmp(ptn->name, "modem"))
Tanya Brokhman1c94f1a2015-02-15 09:05:03 +02004346 extra = 1;
Kishor PK38ed93d2017-04-25 14:19:26 +05304347 else {
Vijay Kumar Pendotic6f22a72017-05-19 22:44:34 +05304348 rounded_size = ROUNDUP(sz, page_size);
4349 bytes_to_round_page = rounded_size - sz;
4350 if (bytes_to_round_page) {
4351 if (((uintptr_t)data + sz ) > (UINT_MAX - bytes_to_round_page)) {
4352 fastboot_fail("Integer overflow detected");
4353 return;
4354 }
4355 if (((uintptr_t)data + sz + bytes_to_round_page) >
4356 ((uintptr_t)target_get_scratch_address() + target_get_max_flash_size())) {
4357 fastboot_fail("Buffer size is not aligned to page_size");
4358 return;
4359 }
4360 else {
4361 memset(data + sz, 0, bytes_to_round_page);
4362 sz = rounded_size;
4363 }
Kishor PK38ed93d2017-04-25 14:19:26 +05304364 }
Gaurav Nebhwanid9730712016-05-06 14:28:29 +05304365 }
4366
Kishor PK38ed93d2017-04-25 14:19:26 +05304367 /*Checking partition_size for the possible integer overflow */
4368 partition_size = validate_partition_size(ptn);
4369
Gaurav Nebhwanid9730712016-05-06 14:28:29 +05304370 if (sz > partition_size) {
4371 fastboot_fail("Image size too large");
4372 return;
4373 }
4374
Dima Zavin214cc642009-01-26 11:16:21 -08004375 dprintf(INFO, "writing %d bytes to '%s'\n", sz, ptn->name);
Mayank Grover6cde9352017-06-06 18:45:23 +05304376 if ((sz > UBI_EC_HDR_SIZE) &&
4377 (!memcmp((void *)data, UBI_MAGIC, UBI_MAGIC_SIZE))) {
Tanya Brokhman1c94f1a2015-02-15 09:05:03 +02004378 if (flash_ubi_img(ptn, data, sz)) {
4379 fastboot_fail("flash write failure");
4380 return;
4381 }
4382 } else {
4383 if (flash_write(ptn, extra, data, sz)) {
4384 fastboot_fail("flash write failure");
4385 return;
4386 }
Dima Zavin214cc642009-01-26 11:16:21 -08004387 }
4388 dprintf(INFO, "partition '%s' updated\n", ptn->name);
4389 fastboot_okay("");
4390}
4391
Kishor PK38ed93d2017-04-25 14:19:26 +05304392
4393static inline uint64_t validate_partition_size(struct ptentry *ptn)
4394{
4395 if (ptn->length && flash_num_pages_per_blk() && page_size) {
4396 if ((ptn->length < ( UINT_MAX / flash_num_pages_per_blk())) && ((ptn->length * flash_num_pages_per_blk()) < ( UINT_MAX / page_size))) {
4397 return ptn->length * flash_num_pages_per_blk() * page_size;
4398 }
4399 }
4400 return 0;
4401}
4402
4403
Sridhar Parasurame94e8152014-10-24 14:06:03 -07004404void cmd_flash(const char *arg, void *data, unsigned sz)
4405{
4406 if(target_is_emmc_boot())
4407 cmd_flash_mmc(arg, data, sz);
4408 else
4409 cmd_flash_nand(arg, data, sz);
4410}
4411
Dima Zavin214cc642009-01-26 11:16:21 -08004412void cmd_continue(const char *arg, void *data, unsigned sz)
4413{
4414 fastboot_okay("");
Amol Jadi7c4316c2013-10-07 14:19:26 -07004415 fastboot_stop();
Shashank Mittald3e54dd2014-08-28 15:24:02 -07004416
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004417 if (target_is_emmc_boot())
4418 {
lijuanga40d6302015-07-20 20:10:13 +08004419#if FBCON_DISPLAY_MSG
lijuangde34d502016-02-26 16:04:50 +08004420 /* Exit keys' detection thread firstly */
4421 exit_menu_keys_detection();
lijuanga40d6302015-07-20 20:10:13 +08004422#endif
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004423 boot_linux_from_mmc();
4424 }
4425 else
4426 {
4427 boot_linux_from_flash();
4428 }
Dima Zavin214cc642009-01-26 11:16:21 -08004429}
4430
Chandan Uddaraju94183c02010-01-15 15:13:59 -08004431void cmd_reboot(const char *arg, void *data, unsigned sz)
4432{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004433 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08004434 fastboot_okay("");
4435 reboot_device(0);
4436}
4437
Mayank Grover351a75e2017-05-30 20:06:08 +05304438void cmd_set_active(const char *arg, void *data, unsigned sz)
4439{
Mayank Grover6ccc1c92017-07-04 17:36:46 +05304440 char *p, *sp = NULL;
Mayank Grover351a75e2017-05-30 20:06:08 +05304441 unsigned i,current_active_slot;
4442 const char *current_slot_suffix;
4443
4444 if (!partition_multislot_is_supported())
4445 {
4446 fastboot_fail("Command not supported");
4447 return;
4448 }
4449
4450 if (arg)
4451 {
Mayank Grover6ccc1c92017-07-04 17:36:46 +05304452 p = strtok_r((char *)arg, ":", &sp);
Mayank Grover5eb5f692017-07-19 20:16:04 +05304453 if (p)
Mayank Grover351a75e2017-05-30 20:06:08 +05304454 {
4455 current_active_slot = partition_find_active_slot();
4456
4457 /* Check if trying to make curent slot active */
4458 current_slot_suffix = SUFFIX_SLOT(current_active_slot);
Mayank Grover6ccc1c92017-07-04 17:36:46 +05304459 current_slot_suffix = strtok_r((char *)current_slot_suffix,
4460 (char *)suffix_delimiter, &sp);
4461
Mayank Grover5eb5f692017-07-19 20:16:04 +05304462 if (current_slot_suffix &&
4463 !strncmp(p, current_slot_suffix, strlen(current_slot_suffix)))
Mayank Grover351a75e2017-05-30 20:06:08 +05304464 {
4465 fastboot_okay("Slot already set active");
4466 return;
4467 }
4468 else
4469 {
4470 for (i = 0; i < AB_SUPPORTED_SLOTS; i++)
4471 {
4472 current_slot_suffix = SUFFIX_SLOT(i);
Mayank Grover6ccc1c92017-07-04 17:36:46 +05304473 current_slot_suffix = strtok_r((char *)current_slot_suffix,
4474 (char *)suffix_delimiter, &sp);
Mayank Grover5eb5f692017-07-19 20:16:04 +05304475 if (current_slot_suffix &&
4476 !strncmp(p, current_slot_suffix, strlen(current_slot_suffix)))
Mayank Grover351a75e2017-05-30 20:06:08 +05304477 {
lijuang8b03e5f2019-07-12 18:16:19 +08004478 partition_switch_slots(current_active_slot, i, true);
Mayank Grover351a75e2017-05-30 20:06:08 +05304479 publish_getvar_multislot_vars();
4480 fastboot_okay("");
4481 return;
4482 }
4483 }
4484 }
4485 }
4486 }
4487 fastboot_fail("Invalid slot suffix.");
4488 return;
4489}
4490
Mayank Grover98c4c742019-04-25 17:21:37 +05304491#if DYNAMIC_PARTITION_SUPPORT
4492void cmd_reboot_fastboot(const char *arg, void *data, unsigned sz)
4493{
4494 dprintf(INFO, "rebooting the device - userspace fastboot\n");
4495 if (send_recovery_cmd(RECOVERY_BOOT_FASTBOOT_CMD)) {
4496 dprintf(CRITICAL, "ERROR: Failed to update recovery commands\n");
4497 fastboot_fail("Failed to update recovery command");
4498 return;
4499 }
4500 fastboot_okay("");
4501 reboot_device(REBOOT_MODE_UNKNOWN);
4502
4503 //shouldn't come here.
4504 dprintf(CRITICAL, "ERROR: Failed to reboot device\n");
4505 return;
4506}
4507
4508void cmd_reboot_recovery(const char *arg, void *data, unsigned sz)
4509{
4510 dprintf(INFO, "rebooting the device - recovery\n");
4511 if (send_recovery_cmd(RECOVERY_BOOT_RECOVERY_CMD)) {
4512 dprintf(CRITICAL, "ERROR: Failed to update recovery commands\n");
4513 fastboot_fail("Failed to update recovery command");
4514 return;
4515 }
4516 fastboot_okay("");
4517 reboot_device(REBOOT_MODE_UNKNOWN);
4518
4519 //shouldn't come here.
4520 dprintf(CRITICAL, "ERROR: Failed to reboot device\n");
4521 return;
4522}
4523#endif
4524
Chandan Uddaraju94183c02010-01-15 15:13:59 -08004525void cmd_reboot_bootloader(const char *arg, void *data, unsigned sz)
4526{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004527 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08004528 fastboot_okay("");
4529 reboot_device(FASTBOOT_MODE);
4530}
4531
Ameya Thakur11cf1a62013-08-05 12:44:48 -07004532void cmd_oem_enable_charger_screen(const char *arg, void *data, unsigned size)
4533{
4534 dprintf(INFO, "Enabling charger screen check\n");
4535 device.charger_screen_enabled = 1;
4536 write_device_info(&device);
4537 fastboot_okay("");
4538}
4539
4540void cmd_oem_disable_charger_screen(const char *arg, void *data, unsigned size)
4541{
4542 dprintf(INFO, "Disabling charger screen check\n");
4543 device.charger_screen_enabled = 0;
4544 write_device_info(&device);
4545 fastboot_okay("");
4546}
4547
lijuanga25d8bb2015-09-16 13:11:52 +08004548void cmd_oem_off_mode_charger(const char *arg, void *data, unsigned size)
4549{
4550 char *p = NULL;
4551 const char *delim = " \t\n\r";
Parth Dixit407f15b2016-01-07 14:47:37 +05304552 char *sp;
lijuanga25d8bb2015-09-16 13:11:52 +08004553
4554 if (arg) {
Parth Dixit407f15b2016-01-07 14:47:37 +05304555 p = strtok_r((char *)arg, delim, &sp);
lijuanga25d8bb2015-09-16 13:11:52 +08004556 if (p) {
4557 if (!strncmp(p, "0", 1)) {
4558 device.charger_screen_enabled = 0;
4559 } else if (!strncmp(p, "1", 1)) {
4560 device.charger_screen_enabled = 1;
4561 }
4562 }
4563 }
4564
4565 /* update charger_screen_enabled value for getvar
4566 * command
4567 */
4568 snprintf(charger_screen_enabled, MAX_RSP_SIZE, "%d",
4569 device.charger_screen_enabled);
4570
4571 write_device_info(&device);
4572 fastboot_okay("");
4573}
4574
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05304575void cmd_oem_select_display_panel(const char *arg, void *data, unsigned size)
4576{
4577 dprintf(INFO, "Selecting display panel %s\n", arg);
4578 if (arg)
4579 strlcpy(device.display_panel, arg,
4580 sizeof(device.display_panel));
4581 write_device_info(&device);
4582 fastboot_okay("");
4583}
4584
Shashank Mittal162244e2011-08-08 19:01:25 -07004585void cmd_oem_unlock(const char *arg, void *data, unsigned sz)
4586{
lijuang4ece1e72015-08-14 21:02:36 +08004587 set_device_unlock(UNLOCK, TRUE);
vijay kumarc65876c2015-04-24 13:29:16 +05304588}
4589
4590void cmd_oem_unlock_go(const char *arg, void *data, unsigned sz)
4591{
lijuang4ece1e72015-08-14 21:02:36 +08004592 if(!device.is_unlocked) {
vijay kumarc65876c2015-04-24 13:29:16 +05304593 if(!is_allow_unlock) {
4594 fastboot_fail("oem unlock is not allowed");
4595 return;
4596 }
4597
lijuang4ece1e72015-08-14 21:02:36 +08004598 set_device_unlock_value(UNLOCK, TRUE);
vijay kumarc65876c2015-04-24 13:29:16 +05304599
lijuang4ece1e72015-08-14 21:02:36 +08004600 /* wipe data */
vijay kumarc65876c2015-04-24 13:29:16 +05304601 struct recovery_message msg;
Kishor PK60a68212017-05-08 16:55:57 +05304602 memset(&msg, 0, sizeof(msg));
vijay kumarc65876c2015-04-24 13:29:16 +05304603 snprintf(msg.recovery, sizeof(msg.recovery), "recovery\n--wipe_data");
4604 write_misc(0, &msg, sizeof(msg));
4605
4606 fastboot_okay("");
4607 reboot_device(RECOVERY_MODE);
Shashank Mittald3e54dd2014-08-28 15:24:02 -07004608 }
4609 fastboot_okay("");
4610}
4611
Channagoud Kadabiad259832015-05-29 11:14:17 -07004612static int aboot_frp_unlock(char *pname, void *data, unsigned sz)
4613{
Mayank Grover70f8b0e2017-05-17 11:49:00 +05304614 int ret=1;
4615 bool authentication_success=false;
Channagoud Kadabiad259832015-05-29 11:14:17 -07004616
Mayank Grover70f8b0e2017-05-17 11:49:00 +05304617 /*
4618 Authentication method not implemented.
4619
4620 OEM to implement, authentication system which on successful validataion,
4621 calls write_allow_oem_unlock() with is_allow_unlock.
4622 */
4623#if 0
4624 authentication_success = oem_specific_auth_mthd();
4625#endif
4626
4627 if (authentication_success)
Channagoud Kadabiad259832015-05-29 11:14:17 -07004628 {
Mayank Grover70f8b0e2017-05-17 11:49:00 +05304629 is_allow_unlock = true;
4630 write_allow_oem_unlock(is_allow_unlock);
4631 ret = 0;
Channagoud Kadabiad259832015-05-29 11:14:17 -07004632 }
4633 return ret;
4634}
4635
Shashank Mittald3e54dd2014-08-28 15:24:02 -07004636void cmd_oem_lock(const char *arg, void *data, unsigned sz)
4637{
lijuang4ece1e72015-08-14 21:02:36 +08004638 set_device_unlock(UNLOCK, FALSE);
Shashank Mittal162244e2011-08-08 19:01:25 -07004639}
4640
Shashank Mittala0032282011-08-26 14:50:11 -07004641void cmd_oem_devinfo(const char *arg, void *data, unsigned sz)
4642{
lijuang511a2b52015-08-14 20:50:51 +08004643 char response[MAX_RSP_SIZE];
Ameya Thakur11cf1a62013-08-05 12:44:48 -07004644 snprintf(response, sizeof(response), "\tDevice tampered: %s", (device.is_tampered ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07004645 fastboot_info(response);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07004646 snprintf(response, sizeof(response), "\tDevice unlocked: %s", (device.is_unlocked ? "true" : "false"));
4647 fastboot_info(response);
Monika Singh292b3e92018-03-17 22:40:23 +05304648#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover912eaa62017-10-26 12:08:53 +05304649 if (VB_M <= target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +05304650 {
4651 snprintf(response, sizeof(response), "\tDevice critical unlocked: %s",
4652 (device.is_unlock_critical ? "true" : "false"));
4653 fastboot_info(response);
4654 }
Parth Dixitddbc7352015-10-18 03:13:31 +05304655#endif
Ameya Thakur11cf1a62013-08-05 12:44:48 -07004656 snprintf(response, sizeof(response), "\tCharger screen enabled: %s", (device.charger_screen_enabled ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07004657 fastboot_info(response);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05304658 snprintf(response, sizeof(response), "\tDisplay panel: %s", (device.display_panel));
4659 fastboot_info(response);
Shashank Mittala0032282011-08-26 14:50:11 -07004660 fastboot_okay("");
4661}
4662
lijuang511a2b52015-08-14 20:50:51 +08004663void cmd_flashing_get_unlock_ability(const char *arg, void *data, unsigned sz)
4664{
4665 char response[MAX_RSP_SIZE];
4666 snprintf(response, sizeof(response), "\tget_unlock_ability: %d", is_allow_unlock);
4667 fastboot_info(response);
4668 fastboot_okay("");
4669}
4670
4671void cmd_flashing_lock_critical(const char *arg, void *data, unsigned sz)
4672{
lijuang4ece1e72015-08-14 21:02:36 +08004673 set_device_unlock(UNLOCK_CRITICAL, FALSE);
lijuang511a2b52015-08-14 20:50:51 +08004674}
4675
4676void cmd_flashing_unlock_critical(const char *arg, void *data, unsigned sz)
4677{
lijuang4ece1e72015-08-14 21:02:36 +08004678 set_device_unlock(UNLOCK_CRITICAL, TRUE);
lijuang511a2b52015-08-14 20:50:51 +08004679}
4680
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07004681void cmd_preflash(const char *arg, void *data, unsigned sz)
4682{
4683 fastboot_okay("");
4684}
4685
Mao Flynn7b379f32015-04-20 00:28:30 +08004686static uint8_t logo_header[LOGO_IMG_HEADER_SIZE];
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304687
Mao Flynn7b379f32015-04-20 00:28:30 +08004688int splash_screen_check_header(logo_img_header *header)
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304689{
Mao Flynn7b379f32015-04-20 00:28:30 +08004690 if (memcmp(header->magic, LOGO_IMG_MAGIC, 8))
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304691 return -1;
Mao Flynn7b379f32015-04-20 00:28:30 +08004692 if (header->width == 0 || header->height == 0)
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304693 return -1;
Ashish Bhimanpalliwareab60c62020-10-09 16:31:11 +05304694 if ((UINT_MAX/512 >= header->blocks) && (header->blocks != 0)){
4695 if (header->blocks*512 < header->width * header->height)
4696 return -1;
4697 }
4698 else {
4699 return -1;
4700 }
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304701 return 0;
4702}
4703
Mao Flynn7b379f32015-04-20 00:28:30 +08004704int splash_screen_flash()
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07004705{
4706 struct ptentry *ptn;
4707 struct ptable *ptable;
Mao Flynn7b379f32015-04-20 00:28:30 +08004708 struct logo_img_header *header;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07004709 struct fbcon_config *fb_display = NULL;
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08004710
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304711 ptable = flash_get_ptable();
4712 if (ptable == NULL) {
Mao Flynn7b379f32015-04-20 00:28:30 +08004713 dprintf(CRITICAL, "ERROR: Partition table not found\n");
4714 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304715 }
Mao Flynn7b379f32015-04-20 00:28:30 +08004716
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304717 ptn = ptable_find(ptable, "splash");
4718 if (ptn == NULL) {
4719 dprintf(CRITICAL, "ERROR: splash Partition not found\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08004720 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304721 }
Mao Flynn7b379f32015-04-20 00:28:30 +08004722 if (flash_read(ptn, 0, (void *)logo_header, LOGO_IMG_HEADER_SIZE)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304723 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08004724 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304725 }
4726
Mao Flynn7b379f32015-04-20 00:28:30 +08004727 header = (struct logo_img_header *)logo_header;
4728 if (splash_screen_check_header(header)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304729 dprintf(CRITICAL, "ERROR: Boot image header invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08004730 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304731 }
4732
4733 fb_display = fbcon_display();
4734 if (fb_display) {
Ashish Bhimanpalliwareab60c62020-10-09 16:31:11 +05304735 if (header->type &&
4736 ((header->blocks * 512) <= (fb_display->width *
4737 fb_display->height * (fb_display->bpp / 8)))) {
raghavendra ambadas554f4fd2018-10-09 15:50:40 +05304738 /* RLE24 compressed data */
Mao Flynn7b379f32015-04-20 00:28:30 +08004739 uint8_t *base = (uint8_t *) fb_display->base + LOGO_IMG_OFFSET;
4740
4741 /* if the logo is full-screen size, remove "fbcon_clear()" */
4742 if ((header->width != fb_display->width)
4743 || (header->height != fb_display->height))
4744 fbcon_clear();
4745
4746 if (flash_read(ptn + LOGO_IMG_HEADER_SIZE, 0,
4747 (uint32_t *)base,
4748 (header->blocks * 512))) {
4749 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
4750 return -1;
4751 }
4752 fbcon_extract_to_screen(header, base);
4753 return 0;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07004754 }
Mao Flynn7b379f32015-04-20 00:28:30 +08004755
Veera Sundaram Sankaranc33416f2015-09-28 10:25:55 -07004756 if ((header->width > fb_display->width) || (header->height > fb_display->height)) {
4757 dprintf(CRITICAL, "Logo config greater than fb config. Fall back default logo\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08004758 return -1;
4759 }
4760
Vineet Bajaj99291ed2014-09-09 12:29:46 +05304761 uint8_t *base = (uint8_t *) fb_display->base;
Sachin Bhayare619e9e42017-05-15 13:10:31 +05304762 uint32_t fb_size = ROUNDUP(fb_display->width *
4763 fb_display->height *
4764 (fb_display->bpp / 8), 4096);
4765 uint32_t splash_size = ((((header->width * header->height *
4766 fb_display->bpp/8) + 511) >> 9) << 9);
4767
4768 if (splash_size > fb_size) {
4769 dprintf(CRITICAL, "ERROR: Splash image size invalid\n");
4770 return -1;
4771 }
4772
Mao Flynn7b379f32015-04-20 00:28:30 +08004773 if (flash_read(ptn + LOGO_IMG_HEADER_SIZE, 0,
4774 (uint32_t *)base,
4775 ((((header->width * header->height * fb_display->bpp/8) + 511) >> 9) << 9))) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304776 fbcon_clear();
Vineet Bajaj99291ed2014-09-09 12:29:46 +05304777 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08004778 return -1;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07004779 }
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304780 }
4781
Mao Flynn7b379f32015-04-20 00:28:30 +08004782 return 0;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304783}
4784
Mao Flynn7b379f32015-04-20 00:28:30 +08004785int splash_screen_mmc()
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304786{
4787 int index = INVALID_PTN;
4788 unsigned long long ptn = 0;
4789 struct fbcon_config *fb_display = NULL;
Mao Flynn7b379f32015-04-20 00:28:30 +08004790 struct logo_img_header *header;
Mao Flynn1893a7f2015-06-03 12:03:36 +08004791 uint32_t blocksize, realsize, readsize;
4792 uint8_t *base;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304793
4794 index = partition_get_index("splash");
4795 if (index == 0) {
4796 dprintf(CRITICAL, "ERROR: splash Partition table not found\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08004797 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304798 }
4799
4800 ptn = partition_get_offset(index);
4801 if (ptn == 0) {
4802 dprintf(CRITICAL, "ERROR: splash Partition invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08004803 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304804 }
4805
Mao Flynn1893a7f2015-06-03 12:03:36 +08004806 mmc_set_lun(partition_get_lun(index));
4807
4808 blocksize = mmc_get_device_blocksize();
4809 if (blocksize == 0) {
4810 dprintf(CRITICAL, "ERROR:splash Partition invalid blocksize\n");
4811 return -1;
4812 }
4813
4814 fb_display = fbcon_display();
Channagoud Kadabidaf10a62015-07-22 11:51:55 -07004815 if (!fb_display)
4816 {
4817 dprintf(CRITICAL, "ERROR: fb config is not allocated\n");
4818 return -1;
4819 }
4820
Mao Flynn1893a7f2015-06-03 12:03:36 +08004821 base = (uint8_t *) fb_display->base;
4822
Kishor PKee5c0a32018-03-06 16:49:46 +05304823 if (mmc_read(ptn + PLL_CODES_OFFSET, (uint32_t *)(base + LOGO_IMG_OFFSET), blocksize)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304824 dprintf(CRITICAL, "ERROR: Cannot read splash image header\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08004825 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304826 }
4827
Mao Flynn1893a7f2015-06-03 12:03:36 +08004828 header = (struct logo_img_header *)(base + LOGO_IMG_OFFSET);
Mao Flynn7b379f32015-04-20 00:28:30 +08004829 if (splash_screen_check_header(header)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304830 dprintf(CRITICAL, "ERROR: Splash image header invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08004831 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304832 }
4833
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304834 if (fb_display) {
Ashish Bhimanpalliwareab60c62020-10-09 16:31:11 +05304835 if (header->type &&
Ashish Bhimanpalliwarce39f482020-10-06 15:27:37 +05304836 (((UINT_MAX - LOGO_IMG_HEADER_SIZE) / 512) >= header->blocks) &&
raghavendra ambadas554f4fd2018-10-09 15:50:40 +05304837 ((header->blocks * 512) <= (fb_display->width *
4838 fb_display->height * (fb_display->bpp / 8)))) {
4839 /* 1 RLE24 compressed data */
Mao Flynn1893a7f2015-06-03 12:03:36 +08004840 base += LOGO_IMG_OFFSET;
Mao Flynn7b379f32015-04-20 00:28:30 +08004841
Mao Flynn1893a7f2015-06-03 12:03:36 +08004842 realsize = header->blocks * 512;
4843 readsize = ROUNDUP((realsize + LOGO_IMG_HEADER_SIZE), blocksize) - blocksize;
4844
4845 /* if the logo is not full-screen size, clean screen */
Mao Flynn7b379f32015-04-20 00:28:30 +08004846 if ((header->width != fb_display->width)
4847 || (header->height != fb_display->height))
4848 fbcon_clear();
4849
Sachin Bhayare619e9e42017-05-15 13:10:31 +05304850 uint32_t fb_size = ROUNDUP(fb_display->width *
4851 fb_display->height *
4852 (fb_display->bpp / 8), 4096);
4853
4854 if (readsize > fb_size) {
4855 dprintf(CRITICAL, "ERROR: Splash image size invalid\n");
4856 return -1;
4857 }
4858
Kishor PKee5c0a32018-03-06 16:49:46 +05304859 if (mmc_read(ptn + PLL_CODES_OFFSET + blocksize, (uint32_t *)(base + blocksize), readsize)) {
Mao Flynn7b379f32015-04-20 00:28:30 +08004860 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
4861 return -1;
4862 }
Mao Flynn7b379f32015-04-20 00:28:30 +08004863
Mao Flynn1893a7f2015-06-03 12:03:36 +08004864 fbcon_extract_to_screen(header, (base + LOGO_IMG_HEADER_SIZE));
4865 } else { /* 2 Raw BGR data */
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304866
Veera Sundaram Sankaranc33416f2015-09-28 10:25:55 -07004867 if ((header->width > fb_display->width) || (header->height > fb_display->height)) {
4868 dprintf(CRITICAL, "Logo config greater than fb config. Fall back default logo\n");
Mao Flynn1893a7f2015-06-03 12:03:36 +08004869 return -1;
4870 }
4871
4872 realsize = header->width * header->height * fb_display->bpp / 8;
4873 readsize = ROUNDUP((realsize + LOGO_IMG_HEADER_SIZE), blocksize) - blocksize;
4874
4875 if (blocksize == LOGO_IMG_HEADER_SIZE) { /* read the content directly */
Kishor PKee5c0a32018-03-06 16:49:46 +05304876 if (mmc_read((ptn + PLL_CODES_OFFSET + LOGO_IMG_HEADER_SIZE), (uint32_t *)base, readsize)) {
Mao Flynn1893a7f2015-06-03 12:03:36 +08004877 fbcon_clear();
4878 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
4879 return -1;
4880 }
4881 } else {
Kishor PKee5c0a32018-03-06 16:49:46 +05304882 if (mmc_read(ptn + PLL_CODES_OFFSET + blocksize ,
Mao Flynn1893a7f2015-06-03 12:03:36 +08004883 (uint32_t *)(base + LOGO_IMG_OFFSET + blocksize), readsize)) {
4884 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
4885 return -1;
4886 }
4887 memmove(base, (base + LOGO_IMG_OFFSET + LOGO_IMG_HEADER_SIZE), realsize);
4888 }
4889 }
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304890 }
4891
Mao Flynn7b379f32015-04-20 00:28:30 +08004892 return 0;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304893}
4894
Mao Flynn7b379f32015-04-20 00:28:30 +08004895int fetch_image_from_partition()
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304896{
4897 if (target_is_emmc_boot()) {
4898 return splash_screen_mmc();
4899 } else {
4900 return splash_screen_flash();
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07004901 }
4902}
4903
Mayank Grover351a75e2017-05-30 20:06:08 +05304904void publish_getvar_multislot_vars()
4905{
4906 int i,count;
4907 static bool published = false;
4908 static char slot_count[MAX_RSP_SIZE];
4909 static struct ab_slot_info slot_info[AB_SUPPORTED_SLOTS];
4910 static char active_slot_suffix[MAX_RSP_SIZE];
4911 static char has_slot_pname[NUM_PARTITIONS][MAX_GET_VAR_NAME_SIZE];
4912 static char has_slot_reply[NUM_PARTITIONS][MAX_RSP_SIZE];
4913 const char *tmp;
4914 char tmpbuff[MAX_GET_VAR_NAME_SIZE];
Mayank Grover2bd84bb2017-06-20 11:06:07 +05304915 signed active_slt;
Mayank Grover351a75e2017-05-30 20:06:08 +05304916
4917 if (!published)
4918 {
4919 /* Update slot meta info */
4920 count = partition_fill_partition_meta(has_slot_pname, has_slot_reply,
4921 partition_get_partition_count());
4922 for(i=0; i<count; i++)
4923 {
4924 memset(tmpbuff, 0, MAX_GET_VAR_NAME_SIZE);
Mayank Grover2bd84bb2017-06-20 11:06:07 +05304925 snprintf(tmpbuff, MAX_GET_VAR_NAME_SIZE,"has-slot:%s",
4926 has_slot_pname[i]);
4927 strlcpy(has_slot_pname[i], tmpbuff, MAX_GET_VAR_NAME_SIZE);
Mayank Grover351a75e2017-05-30 20:06:08 +05304928 fastboot_publish(has_slot_pname[i], has_slot_reply[i]);
4929 }
4930
4931 for (i=0; i<AB_SUPPORTED_SLOTS; i++)
4932 {
4933 tmp = SUFFIX_SLOT(i);
Mayank Grover7b8a8f82017-10-27 13:07:59 +05304934 tmp++; // to remove "_" from slot_suffix.
Mayank Grover2bd84bb2017-06-20 11:06:07 +05304935 snprintf(slot_info[i].slot_is_unbootable, sizeof(slot_info[i].slot_is_unbootable),
4936 "slot-unbootable:%s", tmp);
4937 snprintf(slot_info[i].slot_is_active, sizeof(slot_info[i].slot_is_active),
4938 "slot-active:%s", tmp);
4939 snprintf(slot_info[i].slot_is_succesful, sizeof(slot_info[i].slot_is_succesful),
4940 "slot-success:%s", tmp);
4941 snprintf(slot_info[i].slot_retry_count, sizeof(slot_info[i].slot_retry_count),
4942 "slot-retry-count:%s", tmp);
Mayank Grover351a75e2017-05-30 20:06:08 +05304943 fastboot_publish(slot_info[i].slot_is_unbootable,
4944 slot_info[i].slot_is_unbootable_rsp);
4945 fastboot_publish(slot_info[i].slot_is_active,
4946 slot_info[i].slot_is_active_rsp);
4947 fastboot_publish(slot_info[i].slot_is_succesful,
4948 slot_info[i].slot_is_succesful_rsp);
4949 fastboot_publish(slot_info[i].slot_retry_count,
4950 slot_info[i].slot_retry_count_rsp);
4951 }
4952 fastboot_publish("current-slot", active_slot_suffix);
4953 snprintf(slot_count, sizeof(slot_count),"%d", AB_SUPPORTED_SLOTS);
4954 fastboot_publish("slot-count", slot_count);
4955 published = true;
4956 }
4957
Mayank Grover2bd84bb2017-06-20 11:06:07 +05304958 active_slt = partition_find_active_slot();
4959 if (active_slt != INVALID)
Mayank Grover7b8a8f82017-10-27 13:07:59 +05304960 {
4961 tmp = SUFFIX_SLOT(active_slt);
4962 tmp++; // to remove "_" from slot_suffix.
4963 snprintf(active_slot_suffix, sizeof(active_slot_suffix), "%s", tmp);
4964 }
Mayank Grover2bd84bb2017-06-20 11:06:07 +05304965 else
4966 strlcpy(active_slot_suffix, "INVALID", sizeof(active_slot_suffix));
4967
Mayank Grover351a75e2017-05-30 20:06:08 +05304968 /* Update partition meta information */
4969 partition_fill_slot_meta(slot_info);
4970 return;
4971}
4972
lijuang4ece1e72015-08-14 21:02:36 +08004973void get_product_name(unsigned char *buf)
4974{
4975 snprintf((char*)buf, MAX_RSP_SIZE, "%s", TARGET(BOARD));
4976 return;
4977}
4978
Mayank Grover7b8a8f82017-10-27 13:07:59 +05304979#if PRODUCT_IOT
4980void get_bootloader_version_iot(unsigned char *buf)
4981{
4982 if (buf != NULL)
4983 {
4984 strlcpy(buf, TARGET(BOARD), MAX_VERSION_LEN);
4985 strlcat(buf, "-", MAX_VERSION_LEN);
4986 strlcat(buf, PRODUCT_IOT_VERSION, MAX_VERSION_LEN);
4987 }
4988 return;
4989}
4990#endif
4991
lijuang4ece1e72015-08-14 21:02:36 +08004992void get_bootloader_version(unsigned char *buf)
4993{
4994 snprintf((char*)buf, MAX_RSP_SIZE, "%s", device.bootloader_version);
4995 return;
4996}
4997
4998void get_baseband_version(unsigned char *buf)
4999{
5000 snprintf((char*)buf, MAX_RSP_SIZE, "%s", device.radio_version);
5001 return;
5002}
5003
Monika Singh32a09f72018-03-14 13:08:29 +05305004bool is_device_locked_critical()
5005{
5006 return device.is_unlock_critical ? false:true;
5007}
5008
lijuang4ece1e72015-08-14 21:02:36 +08005009bool is_device_locked()
5010{
5011 return device.is_unlocked ? false:true;
5012}
5013
Monika Singh32a09f72018-03-14 13:08:29 +05305014bool is_verity_enforcing()
5015{
5016 return device.verity_mode ? true:false;
5017}
5018
Amol Jadi5edf3552013-07-23 14:15:34 -07005019/* register commands and variables for fastboot */
5020void aboot_fastboot_register_commands(void)
5021{
Sridhar Parasurame94e8152014-10-24 14:06:03 -07005022 int i;
lijuangf16461c2015-08-03 17:09:34 +08005023 char hw_platform_buf[MAX_RSP_SIZE];
Rahul Shaharee7baede2021-02-02 13:23:57 +05305024 VirtualAbMergeStatus SnapshotMergeStatus;
Amol Jadi5edf3552013-07-23 14:15:34 -07005025
Sridhar Parasurame94e8152014-10-24 14:06:03 -07005026 struct fastboot_cmd_desc cmd_list[] = {
lijuang511a2b52015-08-14 20:50:51 +08005027 /* By default the enabled list is empty. */
5028 {"", NULL},
5029 /* move commands enclosed within the below ifndef to here
5030 * if they need to be enabled in user build.
5031 */
Sridhar Parasurame94e8152014-10-24 14:06:03 -07005032#ifndef DISABLE_FASTBOOT_CMDS
lijuang511a2b52015-08-14 20:50:51 +08005033 /* Register the following commands only for non-user builds */
5034 {"flash:", cmd_flash},
5035 {"erase:", cmd_erase},
5036 {"boot", cmd_boot},
5037 {"continue", cmd_continue},
5038 {"reboot", cmd_reboot},
5039 {"reboot-bootloader", cmd_reboot_bootloader},
5040 {"oem unlock", cmd_oem_unlock},
5041 {"oem unlock-go", cmd_oem_unlock_go},
5042 {"oem lock", cmd_oem_lock},
5043 {"flashing unlock", cmd_oem_unlock},
5044 {"flashing lock", cmd_oem_lock},
5045 {"flashing lock_critical", cmd_flashing_lock_critical},
5046 {"flashing unlock_critical", cmd_flashing_unlock_critical},
5047 {"flashing get_unlock_ability", cmd_flashing_get_unlock_ability},
5048 {"oem device-info", cmd_oem_devinfo},
5049 {"preflash", cmd_preflash},
5050 {"oem enable-charger-screen", cmd_oem_enable_charger_screen},
5051 {"oem disable-charger-screen", cmd_oem_disable_charger_screen},
lijuanga25d8bb2015-09-16 13:11:52 +08005052 {"oem off-mode-charge", cmd_oem_off_mode_charger},
lijuang511a2b52015-08-14 20:50:51 +08005053 {"oem select-display-panel", cmd_oem_select_display_panel},
Mayank Grover6ccc1c92017-07-04 17:36:46 +05305054 {"set_active",cmd_set_active},
Mayank Grover98c4c742019-04-25 17:21:37 +05305055#if DYNAMIC_PARTITION_SUPPORT
5056 {"reboot-fastboot",cmd_reboot_fastboot},
5057 {"reboot-recovery",cmd_reboot_recovery},
5058#endif
Channagoud Kadabi736c4962015-08-21 11:56:52 -07005059#if UNITTEST_FW_SUPPORT
Channagoud Kadabid5ddf482015-09-28 10:31:35 -07005060 {"oem run-tests", cmd_oem_runtests},
Channagoud Kadabi736c4962015-08-21 11:56:52 -07005061#endif
Sridhar Parasurame94e8152014-10-24 14:06:03 -07005062#endif
lijuang511a2b52015-08-14 20:50:51 +08005063 };
Sridhar Parasurame94e8152014-10-24 14:06:03 -07005064
5065 int fastboot_cmds_count = sizeof(cmd_list)/sizeof(cmd_list[0]);
5066 for (i = 1; i < fastboot_cmds_count; i++)
5067 fastboot_register(cmd_list[i].name,cmd_list[i].cb);
5068
Amol Jadi5edf3552013-07-23 14:15:34 -07005069 /* publish variables and their values */
5070 fastboot_publish("product", TARGET(BOARD));
5071 fastboot_publish("kernel", "lk");
5072 fastboot_publish("serialno", sn_buf);
5073
Mayank Grovera64dfbe2018-05-17 14:06:34 +05305074 /*publish hw-revision major(upper 16 bits) and minor(lower 16 bits)*/
5075 snprintf(soc_version_str, MAX_RSP_SIZE, "%x", board_soc_version());
5076 fastboot_publish("hw-revision", soc_version_str);
5077
Amol Jadi5edf3552013-07-23 14:15:34 -07005078 /*
5079 * partition info is supported only for emmc partitions
5080 * Calling this for NAND prints some error messages which
5081 * is harmless but misleading. Avoid calling this for NAND
5082 * devices.
5083 */
5084 if (target_is_emmc_boot())
Mayank Grover7b8a8f82017-10-27 13:07:59 +05305085 publish_getvar_partition_info(part_info, partition_get_partition_count());
Amol Jadi5edf3552013-07-23 14:15:34 -07005086
Mayank Grover351a75e2017-05-30 20:06:08 +05305087 if (partition_multislot_is_supported())
5088 publish_getvar_multislot_vars();
5089
Amol Jadi5edf3552013-07-23 14:15:34 -07005090 /* Max download size supported */
lijuang2fbed912019-07-12 14:15:05 +08005091#if !VERIFIED_BOOT_2
Ameya Thakur11cf1a62013-08-05 12:44:48 -07005092 snprintf(max_download_size, MAX_RSP_SIZE, "\t0x%x",
5093 target_get_max_flash_size());
lijuang2fbed912019-07-12 14:15:05 +08005094#else
5095 snprintf(max_download_size, MAX_RSP_SIZE, "\t0x%x",
5096 SUB_SALT_BUFF_OFFSET(target_get_max_flash_size()));
5097#endif
5098
Amol Jadi5edf3552013-07-23 14:15:34 -07005099 fastboot_publish("max-download-size", (const char *) max_download_size);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07005100 /* Is the charger screen check enabled */
5101 snprintf(charger_screen_enabled, MAX_RSP_SIZE, "%d",
5102 device.charger_screen_enabled);
5103 fastboot_publish("charger-screen-enabled",
5104 (const char *) charger_screen_enabled);
lijuanga25d8bb2015-09-16 13:11:52 +08005105 fastboot_publish("off-mode-charge", (const char *) charger_screen_enabled);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05305106 snprintf(panel_display_mode, MAX_RSP_SIZE, "%s",
5107 device.display_panel);
5108 fastboot_publish("display-panel",
5109 (const char *) panel_display_mode);
Mukesh Ojhabb6e7f32018-03-02 15:04:06 +05305110
5111 if (target_is_emmc_boot())
5112 {
5113 mmc_blocksize = mmc_get_device_blocksize();
5114 }
5115 else
5116 {
5117 mmc_blocksize = flash_block_size();
5118 }
5119 snprintf(block_size_string, MAX_RSP_SIZE, "0x%x", mmc_blocksize);
5120 fastboot_publish("erase-block-size", (const char *) block_size_string);
5121 fastboot_publish("logical-block-size", (const char *) block_size_string);
Mayank Grover7b8a8f82017-10-27 13:07:59 +05305122#if PRODUCT_IOT
5123 get_bootloader_version_iot(&bootloader_version_string);
5124 fastboot_publish("version-bootloader", (const char *) bootloader_version_string);
5125
5126 /* Version baseband is n/a for apq iot devices */
5127 fastboot_publish("version-baseband", "N/A");
Mayank Grover7b8a8f82017-10-27 13:07:59 +05305128#else
Channagoud Kadabiaafbbfc2015-03-04 17:52:56 -08005129 fastboot_publish("version-bootloader", (const char *) device.bootloader_version);
5130 fastboot_publish("version-baseband", (const char *) device.radio_version);
Mayank Grover7b8a8f82017-10-27 13:07:59 +05305131#endif
lijuangf16461c2015-08-03 17:09:34 +08005132 fastboot_publish("secure", is_secure_boot_enable()? "yes":"no");
Mayank Grover7b8a8f82017-10-27 13:07:59 +05305133 fastboot_publish("unlocked", device.is_unlocked ? "yes":"no");
lijuangf16461c2015-08-03 17:09:34 +08005134 smem_get_hw_platform_name((unsigned char *) hw_platform_buf, sizeof(hw_platform_buf));
5135 snprintf(get_variant, MAX_RSP_SIZE, "%s %s", hw_platform_buf,
5136 target_is_emmc_boot()? "eMMC":"UFS");
5137 fastboot_publish("variant", (const char *) get_variant);
lijuang65c5a822015-08-29 16:35:36 +08005138#if CHECK_BAT_VOLTAGE
lijuang102dfa92015-10-09 18:31:03 +08005139 update_battery_status();
lijuang65c5a822015-08-29 16:35:36 +08005140 fastboot_publish("battery-voltage", (const char *) battery_voltage);
lijuang102dfa92015-10-09 18:31:03 +08005141 fastboot_publish("battery-soc-ok", (const char *) battery_soc_ok);
lijuang65c5a822015-08-29 16:35:36 +08005142#endif
Mayank Grover98c4c742019-04-25 17:21:37 +05305143 if (target_dynamic_partition_supported())
5144 fastboot_publish("is-userspace", "no");
Rahul Shaharee7baede2021-02-02 13:23:57 +05305145
5146 if (target_virtual_ab_supported()) {
5147 SnapshotMergeStatus = GetSnapshotMergeStatus ();
5148
5149 switch (SnapshotMergeStatus) {
5150 case SNAPSHOTTED:
5151 SnapshotMergeStatus = SNAPSHOTTED;
5152 break;
5153 case MERGING:
5154 SnapshotMergeStatus = MERGING;
5155 break;
5156 default:
5157 SnapshotMergeStatus = NONE_MERGE_STATUS;
5158 break;
5159 }
5160
5161 snprintf(SnapshotMergeState,
5162 strlen(VabSnapshotMergeStatus[SnapshotMergeStatus]) + 1,
5163 "%s", VabSnapshotMergeStatus[SnapshotMergeStatus]);
5164 fastboot_publish("snapshot-update-state", SnapshotMergeState);
5165 }
Amol Jadi5edf3552013-07-23 14:15:34 -07005166}
5167
Brian Swetland9c4c0752009-01-25 16:23:50 -08005168void aboot_init(const struct app_descriptor *app)
5169{
Shashank Mittal4f99a882010-02-01 13:58:50 -08005170 unsigned reboot_mode = 0;
Mayank Grover351a75e2017-05-30 20:06:08 +05305171 int boot_err_type = 0;
5172 int boot_slot = INVALID;
Chandan Uddarajubedca152010-06-02 23:05:15 -07005173
lijuang39831732016-01-08 17:49:02 +08005174 /* Initialise wdog to catch early lk crashes */
5175#if WDOG_SUPPORT
5176 msm_wdog_init();
5177#endif
5178
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07005179 /* Setup page size information for nv storage */
Shashank Mittald8c42bf2010-06-09 15:44:28 -07005180 if (target_is_emmc_boot())
5181 {
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07005182 page_size = mmc_page_size();
Shashank Mittald8c42bf2010-06-09 15:44:28 -07005183 page_mask = page_size - 1;
Gaurav Nebhwani4d2389c2016-03-17 21:10:05 +05305184 mmc_blocksize = mmc_get_device_blocksize();
5185 mmc_blocksize_mask = mmc_blocksize - 1;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07005186 }
5187 else
5188 {
5189 page_size = flash_page_size();
5190 page_mask = page_size - 1;
5191 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07005192 ASSERT((MEMBASE + MEMSIZE) > MEMBASE);
5193
Ameya Thakur11cf1a62013-08-05 12:44:48 -07005194 read_device_info(&device);
vijay kumarc65876c2015-04-24 13:29:16 +05305195 read_allow_oem_unlock(&device);
Shashank Mittal162244e2011-08-08 19:01:25 -07005196
Mayank Grover351a75e2017-05-30 20:06:08 +05305197 /* Detect multi-slot support */
5198 if (partition_multislot_is_supported())
5199 {
5200 boot_slot = partition_find_active_slot();
5201 if (boot_slot == INVALID)
5202 {
5203 boot_into_fastboot = true;
5204 dprintf(INFO, "Active Slot: (INVALID)\n");
5205 }
5206 else
5207 {
5208 /* Setting the state of system to boot active slot */
5209 partition_mark_active_slot(boot_slot);
5210 dprintf(INFO, "Active Slot: (%s)\n", SUFFIX_SLOT(boot_slot));
5211 }
5212 }
5213
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08005214 /* Display splash screen if enabled */
5215#if DISPLAY_SPLASH_SCREEN
lijuang99c02d82015-02-13 19:04:34 +08005216#if NO_ALARM_DISPLAY
5217 if (!check_alarm_boot()) {
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08005218#endif
lijuang99c02d82015-02-13 19:04:34 +08005219 dprintf(SPEW, "Display Init: Start\n");
Ajay Singh Parmara7865a82015-04-16 21:27:52 -07005220#if DISPLAY_HDMI_PRIMARY
5221 if (!strlen(device.display_panel))
5222 strlcpy(device.display_panel, DISPLAY_PANEL_HDMI,
5223 sizeof(device.display_panel));
5224#endif
Channagoud Kadabi90869ce2015-04-27 11:15:14 -07005225#if ENABLE_WBC
Channagoud Kadabid801ac72015-08-26 11:21:51 -07005226 /* Wait if the display shutdown is in progress */
5227 while(pm_app_display_shutdown_in_prgs());
Channagoud Kadabi90869ce2015-04-27 11:15:14 -07005228 if (!pm_appsbl_display_init_done())
5229 target_display_init(device.display_panel);
5230 else
5231 display_image_on_screen();
5232#else
lijuang99c02d82015-02-13 19:04:34 +08005233 target_display_init(device.display_panel);
Channagoud Kadabi90869ce2015-04-27 11:15:14 -07005234#endif
lijuang99c02d82015-02-13 19:04:34 +08005235 dprintf(SPEW, "Display Init: Done\n");
5236#if NO_ALARM_DISPLAY
5237 }
5238#endif
5239#endif
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08005240
Greg Griscod6250552011-06-29 14:40:23 -07005241 target_serialno((unsigned char *) sn_buf);
Ajay Dudanib06c05f2011-05-12 14:46:10 -07005242 dprintf(SPEW,"serial number: %s\n",sn_buf);
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -08005243
Dhaval Patel223ec952013-07-18 14:49:44 -07005244 memset(display_panel_buf, '\0', MAX_PANEL_BUF_SIZE);
5245
Matthew Qindefd5562014-07-11 18:02:40 +08005246 /*
5247 * Check power off reason if user force reset,
5248 * if yes phone will do normal boot.
5249 */
5250 if (is_user_force_reset())
5251 goto normal_boot;
5252
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07005253 /* Check if we should do something other than booting up */
Ameya Thakur0b9c2442013-05-10 13:22:56 -07005254 if (keys_get_state(KEY_VOLUMEUP) && keys_get_state(KEY_VOLUMEDOWN))
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07005255 {
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03005256 dprintf(ALWAYS,"dload mode key sequence detected\n");
lijuang395b5e62015-11-19 17:39:44 +08005257 reboot_device(EMERGENCY_DLOAD);
5258 dprintf(CRITICAL,"Failed to reboot into dload mode\n");
5259
Ameya Thakur0b9c2442013-05-10 13:22:56 -07005260 boot_into_fastboot = true;
5261 }
5262 if (!boot_into_fastboot)
5263 {
5264 if (keys_get_state(KEY_HOME) || keys_get_state(KEY_VOLUMEUP))
5265 boot_into_recovery = 1;
5266 if (!boot_into_recovery &&
5267 (keys_get_state(KEY_BACK) || keys_get_state(KEY_VOLUMEDOWN)))
Pavel Nedev5d91d412013-04-29 11:34:24 +03005268 boot_into_fastboot = true;
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07005269 }
Shashank Mittald8c42bf2010-06-09 15:44:28 -07005270 #if NO_KEYPAD_DRIVER
Kinson Chik0b1c8162011-08-31 16:31:57 -07005271 if (fastboot_trigger())
Pavel Nedev5d91d412013-04-29 11:34:24 +03005272 boot_into_fastboot = true;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07005273 #endif
Chandan Uddarajubedca152010-06-02 23:05:15 -07005274
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07005275#if USE_PON_REBOOT_REG
5276 reboot_mode = check_hard_reboot_mode();
5277#else
Ajay Dudani77421292010-10-27 19:34:06 -07005278 reboot_mode = check_reboot_mode();
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07005279#endif
5280 if (reboot_mode == RECOVERY_MODE)
5281 {
Ajay Dudani77421292010-10-27 19:34:06 -07005282 boot_into_recovery = 1;
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07005283 }
5284 else if(reboot_mode == FASTBOOT_MODE)
5285 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03005286 boot_into_fastboot = true;
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07005287 }
5288 else if(reboot_mode == ALARM_BOOT)
5289 {
Matthew Qind886f3c2014-01-17 16:52:01 +08005290 boot_reason_alarm = true;
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07005291 }
Monika Singhb0db4b82018-09-26 12:18:02 +05305292#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover912eaa62017-10-26 12:08:53 +05305293 else if (VB_M <= target_get_vb_version())
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07005294 {
Mayank Grover889be1b2017-09-12 20:12:23 +05305295 if (reboot_mode == DM_VERITY_ENFORCING)
5296 {
5297 device.verity_mode = 1;
5298 write_device_info(&device);
5299 }
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07005300#if ENABLE_VB_ATTEST
Mayank Grover889be1b2017-09-12 20:12:23 +05305301 else if (reboot_mode == DM_VERITY_EIO)
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07005302#else
Mayank Grover889be1b2017-09-12 20:12:23 +05305303 else if (reboot_mode == DM_VERITY_LOGGING)
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07005304#endif
Mayank Grover889be1b2017-09-12 20:12:23 +05305305 {
5306 device.verity_mode = 0;
5307 write_device_info(&device);
5308 }
5309 else if (reboot_mode == DM_VERITY_KEYSCLEAR)
5310 {
5311 if(send_delete_keys_to_tz())
5312 ASSERT(0);
5313 }
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07005314 }
Parth Dixitddbc7352015-10-18 03:13:31 +05305315#endif
Ajay Dudani77421292010-10-27 19:34:06 -07005316
Matthew Qindefd5562014-07-11 18:02:40 +08005317normal_boot:
Pavel Nedev5d91d412013-04-29 11:34:24 +03005318 if (!boot_into_fastboot)
Shashank Mittald8c42bf2010-06-09 15:44:28 -07005319 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03005320 if (target_is_emmc_boot())
Shashank Mittala0032282011-08-26 14:50:11 -07005321 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03005322 if(emmc_recovery_init())
5323 dprintf(ALWAYS,"error in emmc_recovery_init\n");
5324 if(target_use_signed_kernel())
Shashank Mittala0032282011-08-26 14:50:11 -07005325 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03005326 if((device.is_unlocked) || (device.is_tampered))
5327 {
5328 #ifdef TZ_TAMPER_FUSE
Monika Singh98257462018-06-06 11:28:49 +05305329 set_tamper_fuse_cmd(HLOS_IMG_TAMPER_FUSE);
Pavel Nedev5d91d412013-04-29 11:34:24 +03005330 #endif
5331 #if USE_PCOM_SECBOOT
5332 set_tamper_flag(device.is_tampered);
5333 #endif
5334 }
Shashank Mittala0032282011-08-26 14:50:11 -07005335 }
Amit Blay6281ebc2015-01-11 14:44:08 +02005336
Mayank Grover351a75e2017-05-30 20:06:08 +05305337retry_boot:
5338 /* Trying to boot active partition */
5339 if (partition_multislot_is_supported())
5340 {
5341 boot_slot = partition_find_boot_slot();
5342 partition_mark_active_slot(boot_slot);
5343 if (boot_slot == INVALID)
5344 goto fastboot;
5345 }
5346
5347 boot_err_type = boot_linux_from_mmc();
5348 switch (boot_err_type)
5349 {
5350 case ERR_INVALID_PAGE_SIZE:
5351 case ERR_DT_PARSE:
5352 case ERR_ABOOT_ADDR_OVERLAP:
Mayank Grover10cabbe2017-10-30 19:11:05 +05305353 case ERR_INVALID_BOOT_MAGIC:
Mayank Grover351a75e2017-05-30 20:06:08 +05305354 if(partition_multislot_is_supported())
Mayank Grover10cabbe2017-10-30 19:11:05 +05305355 {
5356 /*
5357 * Deactivate current slot, as it failed to
5358 * boot, and retry next slot.
5359 */
5360 partition_deactivate_slot(boot_slot);
Mayank Grover351a75e2017-05-30 20:06:08 +05305361 goto retry_boot;
Mayank Grover10cabbe2017-10-30 19:11:05 +05305362 }
Mayank Grover351a75e2017-05-30 20:06:08 +05305363 else
5364 break;
Mayank Grover351a75e2017-05-30 20:06:08 +05305365 default:
5366 break;
5367 /* going to fastboot menu */
5368 }
Shashank Mittala0032282011-08-26 14:50:11 -07005369 }
Pavel Nedev5d91d412013-04-29 11:34:24 +03005370 else
5371 {
5372 recovery_init();
5373 #if USE_PCOM_SECBOOT
5374 if((device.is_unlocked) || (device.is_tampered))
5375 set_tamper_flag(device.is_tampered);
5376 #endif
5377 boot_linux_from_flash();
5378 }
5379 dprintf(CRITICAL, "ERROR: Could not do normal boot. Reverting "
5380 "to fastboot mode.\n");
Shashank Mittald8c42bf2010-06-09 15:44:28 -07005381 }
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07005382
Mayank Grover351a75e2017-05-30 20:06:08 +05305383fastboot:
Amol Jadi5edf3552013-07-23 14:15:34 -07005384 /* We are here means regular boot did not happen. Start fastboot. */
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07005385
Amol Jadi5edf3552013-07-23 14:15:34 -07005386 /* register aboot specific fastboot commands */
5387 aboot_fastboot_register_commands();
Amol Jadi57abe4c2011-05-24 15:47:27 -07005388
Amol Jadi5edf3552013-07-23 14:15:34 -07005389 /* dump partition table for debug info */
Kinson Chikf1a43512011-07-14 11:28:39 -07005390 partition_dump();
Amol Jadi5edf3552013-07-23 14:15:34 -07005391
5392 /* initialize and start fastboot */
Saranya Chidura41b34532018-10-16 12:29:52 +05305393#if !VERIFIED_BOOT_2
Amol Jadi5edf3552013-07-23 14:15:34 -07005394 fastboot_init(target_get_scratch_address(), target_get_max_flash_size());
Saranya Chidura41b34532018-10-16 12:29:52 +05305395#else
5396 /* Add salt buffer offset at start of image address to copy VB salt */
5397 fastboot_init(ADD_SALT_BUFF_OFFSET(target_get_scratch_address()),
5398 SUB_SALT_BUFF_OFFSET(target_get_max_flash_size()));
5399#endif
lijuang4ece1e72015-08-14 21:02:36 +08005400#if FBCON_DISPLAY_MSG
lijuang9a7d3b92015-11-30 14:41:24 +08005401 display_fastboot_menu();
lijuang4ece1e72015-08-14 21:02:36 +08005402#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08005403}
5404
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07005405uint32_t get_page_size()
5406{
5407 return page_size;
5408}
5409
Amir Samuelov57a6fa22013-06-05 16:36:43 +03005410/*
5411 * Calculated and save hash (SHA256) for non-signed boot image.
5412 *
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07005413 * @param image_addr - Boot image address
5414 * @param image_size - Size of the boot image
Amir Samuelov57a6fa22013-06-05 16:36:43 +03005415 *
5416 * @return int - 0 on success, negative value on failure.
5417 */
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07005418static int aboot_save_boot_hash_mmc(uint32_t image_addr, uint32_t image_size)
Amir Samuelov57a6fa22013-06-05 16:36:43 +03005419{
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07005420 unsigned int digest[8];
5421#if IMAGE_VERIF_ALGO_SHA1
5422 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA1;
5423#else
5424 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA256;
5425#endif
Amir Samuelov57a6fa22013-06-05 16:36:43 +03005426
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07005427 target_crypto_init_params();
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -08005428 hash_find((unsigned char *) image_addr, image_size, (unsigned char *)&digest, auth_algo);
Amir Samuelov57a6fa22013-06-05 16:36:43 +03005429
5430 save_kernel_hash_cmd(digest);
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07005431 dprintf(INFO, "aboot_save_boot_hash_mmc: imagesize_actual size %d bytes.\n", (int) image_size);
Amir Samuelov57a6fa22013-06-05 16:36:43 +03005432
5433 return 0;
5434}
5435
Mayank Grover351a75e2017-05-30 20:06:08 +05305436
Brian Swetland9c4c0752009-01-25 16:23:50 -08005437APP_START(aboot)
5438 .init = aboot_init,
5439APP_END