blob: 51bdf85149ed28f7f5744bbde89f5f7cafbe4ac2 [file] [log] [blame]
Brian Swetland9c4c0752009-01-25 16:23:50 -08001/*
2 * Copyright (c) 2009, Google Inc.
3 * All rights reserved.
4 *
Mayank Grover98c4c742019-04-25 17:21:37 +05305 * Copyright (c) 2009-2019, The Linux Foundation. All rights reserved.
Brian Swetland9c4c0752009-01-25 16:23:50 -08006 *
Chandan Uddaraju5fa471a2009-12-02 17:31:34 -08007 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070014 * * Neither the name of The Linux Foundation nor
Chandan Uddaraju5fa471a2009-12-02 17:31:34 -080015 * the names of its contributors may be used to endorse or promote
16 * products derived from this software without specific prior written
17 * permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22 * NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 *
Brian Swetland9c4c0752009-01-25 16:23:50 -080031 */
32
33#include <app.h>
34#include <debug.h>
35#include <arch/arm.h>
Brian Swetland9c4c0752009-01-25 16:23:50 -080036#include <string.h>
Channagoud Kadabi132ff552013-04-19 14:34:44 -070037#include <stdlib.h>
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -070038#include <limits.h>
Brian Swetland9c4c0752009-01-25 16:23:50 -080039#include <kernel/thread.h>
40#include <arch/ops.h>
41
Dima Zavin214cc642009-01-26 11:16:21 -080042#include <dev/flash.h>
Tanya Brokhman1c94f1a2015-02-15 09:05:03 +020043#include <dev/flash-ubi.h>
Dima Zavin214cc642009-01-26 11:16:21 -080044#include <lib/ptable.h>
Dima Zavinb4283602009-01-26 16:36:57 -080045#include <dev/keys.h>
Shashank Mittal4f99a882010-02-01 13:58:50 -080046#include <dev/fbcon.h>
Ajay Dudanid04110c2011-01-17 23:55:07 -080047#include <baseband.h>
Greg Griscod6250552011-06-29 14:40:23 -070048#include <target.h>
49#include <mmc.h>
Kinson Chikf1a43512011-07-14 11:28:39 -070050#include <partition_parser.h>
Mayank Grover351a75e2017-05-30 20:06:08 +053051#include <ab_partition_parser.h>
Monika Singh292b3e92018-03-17 22:40:23 +053052#include <verifiedboot.h>
Greg Griscod6250552011-06-29 14:40:23 -070053#include <platform.h>
Shashank Mittalcd98d472011-08-02 14:29:24 -070054#include <crypto_hash.h>
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070055#include <malloc.h>
Amol Jadi492d5a52013-03-15 16:12:34 -070056#include <boot_stats.h>
Amir Samuelov57a6fa22013-06-05 16:36:43 +030057#include <sha.h>
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -070058#include <platform/iomap.h>
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -070059#include <boot_device.h>
Shashank Mittald3e54dd2014-08-28 15:24:02 -070060#include <boot_verifier.h>
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +053061#include <image_verify.h>
Matthew Qinbb7923d2015-02-09 10:56:09 +080062#include <decompress.h>
Unnati Gandhi17b3bfc2015-05-11 12:58:16 +053063#include <platform/timer.h>
lijuang511a2b52015-08-14 20:50:51 +080064#include <sys/types.h>
Channagoud Kadabi036c6052015-02-09 15:19:59 -080065#if USE_RPMB_FOR_DEVINFO
66#include <rpmb.h>
67#endif
Dima Zavin214cc642009-01-26 11:16:21 -080068
Channagoud Kadabi90869ce2015-04-27 11:15:14 -070069#if ENABLE_WBC
Umang Chheda4c140de2019-12-19 14:30:38 +053070#include <pm_smbchg_common.h>
Channagoud Kadabi90869ce2015-04-27 11:15:14 -070071#endif
72
Neeti Desai17379b82012-06-04 18:42:53 -070073#if DEVICE_TREE
74#include <libfdt.h>
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070075#include <dev_tree.h>
Neeti Desai17379b82012-06-04 18:42:53 -070076#endif
77
Aparna Mallavarapu118ccae2015-06-03 13:47:11 +053078#if WDOG_SUPPORT
79#include <wdog.h>
80#endif
81
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -070082#include <reboot.h>
Shashank Mittalcd98d472011-08-02 14:29:24 -070083#include "image_verify.h"
Shashank Mittal024c0332010-02-03 11:44:00 -080084#include "recovery.h"
Brian Swetland9c4c0752009-01-25 16:23:50 -080085#include "bootimg.h"
86#include "fastboot.h"
Ajay Dudani5c761132011-04-07 20:19:04 -070087#include "sparse_format.h"
Ajay Dudanide984792015-03-02 09:57:41 -080088#include "meta_format.h"
Greg Grisco6e754772011-06-23 12:19:39 -070089#include "mmc.h"
Shashank Mittal162244e2011-08-08 19:01:25 -070090#include "devinfo.h"
Neeti Desai465491e2012-07-31 12:53:35 -070091#include "board.h"
Shashank Mittal162244e2011-08-08 19:01:25 -070092#include "scm.h"
Amit Blay6281ebc2015-01-11 14:44:08 +020093#include "mdtp.h"
Sridhar Parasuram32b30662015-07-10 13:33:22 -070094#include "secapp_loader.h"
lijuanga40d6302015-07-20 20:10:13 +080095#include <menu_keys_detect.h>
96#include <display_menu.h>
Channagoud Kadabi736c4962015-08-21 11:56:52 -070097#include "fastboot_test.h"
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
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700243static unsigned page_size = 0;
244static unsigned page_mask = 0;
Gaurav Nebhwani4d2389c2016-03-17 21:10:05 +0530245static unsigned mmc_blocksize = 0;
246static unsigned mmc_blocksize_mask = 0;
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700247static char ffbm_mode_string[FFBM_MODE_BUF_SIZE];
248static bool boot_into_ffbm;
vijay kumar870515d2015-08-31 16:37:24 +0530249static char *target_boot_params = NULL;
Matthew Qind886f3c2014-01-17 16:52:01 +0800250static bool boot_reason_alarm;
Channagoud Kadabi80a182b2015-03-11 17:04:23 -0700251static bool devinfo_present = true;
Channagoud Kadabi736c4962015-08-21 11:56:52 -0700252bool boot_into_fastboot = false;
Parth Dixit4097b622016-03-15 14:42:27 +0530253static uint32_t dt_size = 0;
Monika Singh292b3e92018-03-17 22:40:23 +0530254static char *vbcmdline;
255static bootinfo info = {0};
Mayank Grovere1ab96c2018-09-04 20:31:31 +0530256static void *recovery_dtbo_buf = NULL;
257static uint32_t recovery_dtbo_size = 0;
258
Shashank Mittalcd98d472011-08-02 14:29:24 -0700259/* Assuming unauthorized kernel image by default */
260static int auth_kernel_img = 0;
Monika Singh292b3e92018-03-17 22:40:23 +0530261static device_info device = {DEVICE_MAGIC,0,0,0,0,{0},{0},{0},1,{0},0,{0}};
262
vijay kumarc65876c2015-04-24 13:29:16 +0530263static bool is_allow_unlock = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -0700264
vijay kumarca2e6812015-07-08 20:28:25 +0530265static char frp_ptns[2][8] = {"config","frp"};
266
lijuang511a2b52015-08-14 20:50:51 +0800267static const char *critical_flash_allowed_ptn[] = {
268 "aboot",
269 "rpm",
270 "tz",
271 "sbl",
272 "sdi",
273 "sbl1",
274 "xbl",
275 "hyp",
276 "pmic",
277 "bootloader",
278 "devinfo",
279 "partition"};
280
Dima Zavin42168f22009-01-30 11:52:22 -0800281struct atag_ptbl_entry
282{
283 char name[16];
284 unsigned offset;
285 unsigned size;
286 unsigned flags;
287};
288
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700289/*
290 * Partition info, required to be published
291 * for fastboot
292 */
293struct getvar_partition_info {
Mayank Grover7b8a8f82017-10-27 13:07:59 +0530294 char part_name[MAX_GPT_NAME_SIZE]; /* Partition name */
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700295 char getvar_size[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for size */
296 char getvar_type[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for type */
297 char size_response[MAX_RSP_SIZE]; /* fastboot response for size */
298 char type_response[MAX_RSP_SIZE]; /* fastboot response for type */
299};
300
301/*
Mayank Grover7b8a8f82017-10-27 13:07:59 +0530302 * Update the part_type_known for known paritions types.
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700303 */
Mayank Groverd38fe012018-03-13 15:33:16 +0530304#define RAW_STR "raw"
Mayank Grover52cd10a2018-03-15 12:57:54 +0530305#define EXT_STR "ext4"
306#define F2FS_STR "f2fs"
307
308#define FS_SUPERBLOCK_OFFSET 0x400
309#define EXT_MAGIC 0xEF53
310#define EXT_MAGIC_OFFSET_SB 0x38
311#define F2FS_MAGIC 0xF2F52010 // F2FS Magic Number
312#define F2FS_MAGIC_OFFSET_SB 0x0
313
314typedef enum fs_signature_type {
315 EXT_FS_SIGNATURE = 1,
316 EXT_F2FS_SIGNATURE = 2,
317 NO_FS = -1
318} fs_signature_type;
319
Mayank Grover7b8a8f82017-10-27 13:07:59 +0530320struct getvar_partition_info part_info[NUM_PARTITIONS];
321struct getvar_partition_info part_type_known[] =
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700322{
Mayank Grover49920212018-02-09 18:15:55 +0530323 { "system" , "partition-size:", "partition-type:", "", "ext4" },
324 { "userdata" , "partition-size:", "partition-type:", "", "ext4" },
325 { "cache" , "partition-size:", "partition-type:", "", "ext4" },
326 { "recoveryfs" , "partition-size:", "partition-type:", "", "ext4" },
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700327};
328
329char max_download_size[MAX_RSP_SIZE];
Ameya Thakur11cf1a62013-08-05 12:44:48 -0700330char charger_screen_enabled[MAX_RSP_SIZE];
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800331char sn_buf[13];
Dhaval Patel223ec952013-07-18 14:49:44 -0700332char display_panel_buf[MAX_PANEL_BUF_SIZE];
Unnati Gandhi62c8ab82014-01-24 11:01:01 +0530333char panel_display_mode[MAX_RSP_SIZE];
Mayank Grovera64dfbe2018-05-17 14:06:34 +0530334char soc_version_str[MAX_RSP_SIZE];
Mayank Grover7b8a8f82017-10-27 13:07:59 +0530335char block_size_string[MAX_RSP_SIZE];
Mukesh Ojhabb6e7f32018-03-02 15:04:06 +0530336#if PRODUCT_IOT
Mayank Grover7b8a8f82017-10-27 13:07:59 +0530337
338/* For IOT we are using custom version */
339#define PRODUCT_IOT_VERSION "IOT001"
340char bootloader_version_string[MAX_RSP_SIZE];
341#endif
lijuang102dfa92015-10-09 18:31:03 +0800342
343#if CHECK_BAT_VOLTAGE
lijuang65c5a822015-08-29 16:35:36 +0800344char battery_voltage[MAX_RSP_SIZE];
lijuang102dfa92015-10-09 18:31:03 +0800345char battery_soc_ok [MAX_RSP_SIZE];
346#endif
347
lijuangf16461c2015-08-03 17:09:34 +0800348char get_variant[MAX_RSP_SIZE];
Greg Griscod6250552011-06-29 14:40:23 -0700349
Greg Griscod2471ef2011-07-14 13:00:42 -0700350extern int emmc_recovery_init(void);
351
Kinson Chik0b1c8162011-08-31 16:31:57 -0700352#if NO_KEYPAD_DRIVER
353extern int fastboot_trigger(void);
354#endif
Greg Griscod2471ef2011-07-14 13:00:42 -0700355
Mayank Grovere1ab96c2018-09-04 20:31:31 +0530356static void update_ker_tags_rdisk_addr(boot_img_hdr *hdr, bool is_arm64)
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700357{
358 /* overwrite the destination of specified for the project */
Channagoud Kadabi7042fa32013-04-26 16:44:14 -0700359#ifdef ABOOT_IGNORE_BOOT_HEADER_ADDRS
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800360 if (is_arm64)
361 hdr->kernel_addr = ABOOT_FORCE_KERNEL64_ADDR;
362 else
363 hdr->kernel_addr = ABOOT_FORCE_KERNEL_ADDR;
Channagoud Kadabi7042fa32013-04-26 16:44:14 -0700364 hdr->ramdisk_addr = ABOOT_FORCE_RAMDISK_ADDR;
365 hdr->tags_addr = ABOOT_FORCE_TAGS_ADDR;
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700366#endif
367}
368
Dima Zavin42168f22009-01-30 11:52:22 -0800369static void ptentry_to_tag(unsigned **ptr, struct ptentry *ptn)
370{
371 struct atag_ptbl_entry atag_ptn;
372
373 memcpy(atag_ptn.name, ptn->name, 16);
374 atag_ptn.name[15] = '\0';
375 atag_ptn.offset = ptn->start;
376 atag_ptn.size = ptn->length;
377 atag_ptn.flags = ptn->flags;
378 memcpy(*ptr, &atag_ptn, sizeof(struct atag_ptbl_entry));
379 *ptr += sizeof(struct atag_ptbl_entry) / sizeof(unsigned);
380}
Mayank Grover9df84c02018-08-30 15:46:35 +0530381#ifdef VERIFIED_BOOT_2
382void load_vbmeta_image(void **vbmeta_image_buf, uint32_t *vbmeta_image_sz)
383{
384 int index = 0;
385 char *vbm_img_buf = NULL;
386 unsigned long long ptn = 0;
387 unsigned long long ptn_size = 0;
388
389 /* Immediately return if dtbo is not supported */
390 index = partition_get_index("vbmeta");
391 ptn = partition_get_offset(index);
392 if(!ptn)
393 {
394 dprintf(CRITICAL, "ERROR: vbmeta partition not found.\n");
395 return;
396 }
397
398 ptn_size = partition_get_size(index);
399 if (ptn_size > MAX_SUPPORTED_VBMETA_IMG_BUF)
400 {
401 dprintf(CRITICAL, "ERROR: vbmeta parition size is greater than supported.\n");
402 return;
403 }
404
405 vbm_img_buf = (char *)memalign(CACHE_LINE, ROUNDUP((uint32_t)ptn_size, CACHE_LINE));
406 if (!vbm_img_buf)
407 {
408 dprintf(CRITICAL, "ERROR: vbmeta unable to locate buffer\n");
409 return;
410 }
411
412 mmc_set_lun(partition_get_lun(index));
413 if (mmc_read(ptn, (uint32_t *)vbm_img_buf, (uint32_t)ptn_size))
414 {
415 dprintf(CRITICAL, "ERROR: vbmeta read failure\n");
416 free(vbm_img_buf);
417 return;
418 }
419
420 *vbmeta_image_buf = vbm_img_buf;
421 *vbmeta_image_sz = (uint32_t)ptn_size;
422 return;
423}
424#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -0800425
lijuang102dfa92015-10-09 18:31:03 +0800426#if CHECK_BAT_VOLTAGE
427void update_battery_status(void)
428{
429 snprintf(battery_voltage,MAX_RSP_SIZE, "%d",target_get_battery_voltage());
430 snprintf(battery_soc_ok ,MAX_RSP_SIZE, "%s",target_battery_soc_ok()? "yes":"no");
431}
432#endif
433
Neeti Desaie245d492012-06-01 12:52:13 -0700434unsigned char *update_cmdline(const char * cmdline)
Brian Swetland9c4c0752009-01-25 16:23:50 -0800435{
David Ng183a7422009-12-07 14:55:21 -0800436 int cmdline_len = 0;
437 int have_cmdline = 0;
Amol Jadi168b7712012-03-06 16:15:00 -0800438 unsigned char *cmdline_final = NULL;
Neeti Desaie245d492012-06-01 12:52:13 -0700439 int pause_at_bootup = 0;
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800440 bool warm_boot = false;
Pavel Nedev5614d222013-06-17 18:01:02 +0300441 bool gpt_exists = partition_gpt_exists();
Joonwoo Park61112782013-10-02 19:50:39 -0700442 int have_target_boot_params = 0;
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700443 char *boot_dev_buf = NULL;
Monika Singh292b3e92018-03-17 22:40:23 +0530444#ifdef MDTP_SUPPORT
Mayank Grover351a75e2017-05-30 20:06:08 +0530445 bool is_mdtp_activated = 0;
Monika Singh292b3e92018-03-17 22:40:23 +0530446#endif
Mayank Grover351a75e2017-05-30 20:06:08 +0530447 int current_active_slot = INVALID;
Mayank Grover3804be72017-06-22 11:59:23 +0530448 int system_ptn_index = -1;
449 unsigned int lun = 0;
450 char lun_char_base = 'a';
Sourabh Banerjee386b1322018-02-27 09:37:28 +0530451#if VERITY_LE
452 int syspath_buflen = strlen(verity_dev)
453 + strlen(verity_system_part) + (sizeof(char) * 2) + 2
454 + strlen(verity_params) + sizeof(int) + 2;
455#else
lijuang83ef4b22018-08-23 11:01:55 +0800456 int syspath_buflen = strlen(sys_path) + sizeof(int) + 2; /*allocate buflen for largest possible string*/
457 char dtbo_idx_str[MAX_DTBO_IDX_STR] = "\0";
458 int dtbo_idx = INVALID_PTN;
Parth Dixit1375d9e2019-07-08 20:56:13 +0530459 char dtb_idx_str[MAX_DTB_IDX_STR] = "\0";
460 int dtb_idx = INVALID_PTN;
Sourabh Banerjee386b1322018-02-27 09:37:28 +0530461#endif
Mayank Grover3804be72017-06-22 11:59:23 +0530462 char syspath_buf[syspath_buflen];
Mayank Grover466d6562018-05-10 14:52:20 +0530463#if HIBERNATION_SUPPORT
464 int resume_buflen = strlen(resume) + sizeof(int) + 2;
465 char resume_buf[resume_buflen];
466 int swap_ptn_index = INVALID_PTN;
467#endif
468
Mayank Grover889be1b2017-09-12 20:12:23 +0530469#if VERIFIED_BOOT
470 uint32_t boot_state = RED;
471#endif
Hareesh Gauthamabf1e1e2017-04-12 18:09:24 +0530472
473#if USE_LE_SYSTEMD
474 is_systemd_present=true;
475#endif
476
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700477#if VERIFIED_BOOT
Mayank Grover912eaa62017-10-26 12:08:53 +0530478 if (VB_M <= target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +0530479 {
480 boot_state = boot_verify_get_state();
481 }
Parth Dixitddbc7352015-10-18 03:13:31 +0530482#endif
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700483
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200484#ifdef MDTP_SUPPORT
485 mdtp_activated(&is_mdtp_activated);
486#endif /* MDTP_SUPPORT */
Dima Zavin42168f22009-01-30 11:52:22 -0800487
Brian Swetland9c4c0752009-01-25 16:23:50 -0800488 if (cmdline && cmdline[0]) {
David Ng183a7422009-12-07 14:55:21 -0800489 cmdline_len = strlen(cmdline);
490 have_cmdline = 1;
491 }
492 if (target_is_emmc_boot()) {
493 cmdline_len += strlen(emmc_cmdline);
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700494 boot_dev_buf = (char *) malloc(sizeof(char) * BOOT_DEV_MAX_LEN);
Mayank Groverb716edf2019-05-08 16:06:55 +0530495 if (!boot_dev_buf) {
496 dprintf(CRITICAL, "ERROR: Failed to allocate boot_dev_buf\n");
497 } else {
498 platform_boot_dev_cmdline(boot_dev_buf);
499#if USE_BOOTDEV_CMDLINE
500 cmdline_len += strlen(boot_dev_buf);
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700501#endif
Mayank Groverb716edf2019-05-08 16:06:55 +0530502 if (target_dynamic_partition_supported()) {
503 cmdline_len += strlen(dynamic_bootdev_cmdline);
504 cmdline_len += strlen(boot_dev_buf);
505 }
506 }
David Ng183a7422009-12-07 14:55:21 -0800507 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800508
509 cmdline_len += strlen(usb_sn_cmdline);
510 cmdline_len += strlen(sn_buf);
511
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700512#if VERIFIED_BOOT
Mayank Grover912eaa62017-10-26 12:08:53 +0530513 if (VB_M <= target_get_vb_version())
Sridhar Parasuram7d8bb9a2015-09-23 18:21:19 -0700514 {
Mayank Grover889be1b2017-09-12 20:12:23 +0530515 cmdline_len += strlen(verified_state) + strlen(vbsn[boot_state].name);
516 if ((device.verity_mode != 0 ) && (device.verity_mode != 1))
517 {
518 dprintf(CRITICAL, "Devinfo paritition possibly corrupted!!!. Please erase devinfo partition to continue booting\n");
519 ASSERT(0);
520 }
521 cmdline_len += strlen(verity_mode) + strlen(vbvm[device.verity_mode].name);
522 cmdline_len += strlen(keymaster_v1);
Sridhar Parasuram7d8bb9a2015-09-23 18:21:19 -0700523 }
Parth Dixitddbc7352015-10-18 03:13:31 +0530524#endif
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700525
Monika Singh292b3e92018-03-17 22:40:23 +0530526
527 if (vbcmdline != NULL) {
528 dprintf(DEBUG, "UpdateCmdLine vbcmdline present len %d\n",
529 strlen(vbcmdline));
530 cmdline_len += strlen(vbcmdline);
531 }
532
Pavel Nedev5614d222013-06-17 18:01:02 +0300533 if (boot_into_recovery && gpt_exists)
534 cmdline_len += strlen(secondary_gpt_enable);
535
Monika Singh292b3e92018-03-17 22:40:23 +0530536#ifdef MDTP_SUPPORT
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200537 if(is_mdtp_activated)
538 cmdline_len += strlen(mdtp_activated_flag);
Monika Singh292b3e92018-03-17 22:40:23 +0530539#endif
Pavel Nedev328ac822013-04-05 15:25:11 +0300540 if (boot_into_ffbm) {
541 cmdline_len += strlen(androidboot_mode);
Hareesh Gauthamabf1e1e2017-04-12 18:09:24 +0530542
543 if(is_systemd_present)
544 cmdline_len += strlen(systemd_ffbm_mode);
545
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700546 cmdline_len += strlen(ffbm_mode_string);
Pavel Nedev898298c2013-02-27 12:36:09 -0800547 /* reduce kernel console messages to speed-up boot */
548 cmdline_len += strlen(loglevel);
Matthew Qind886f3c2014-01-17 16:52:01 +0800549 } else if (boot_reason_alarm) {
550 cmdline_len += strlen(alarmboot_cmdline);
Zhenhua Huang431dafa2015-06-30 16:13:37 +0800551 } else if ((target_build_variant_user() || device.charger_screen_enabled)
lijuang266b17d2018-08-17 18:53:42 +0800552 && target_pause_for_battery_charge() && !boot_into_recovery) {
David Ngf773dde2010-07-26 19:55:08 -0700553 pause_at_bootup = 1;
554 cmdline_len += strlen(battchg_pause);
555 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800556
Shashank Mittalcd98d472011-08-02 14:29:24 -0700557 if(target_use_signed_kernel() && auth_kernel_img) {
558 cmdline_len += strlen(auth_kernel);
559 }
560
Joonwoo Park61112782013-10-02 19:50:39 -0700561 if (get_target_boot_params(cmdline, boot_into_recovery ? "recoveryfs" :
562 "system",
vijay kumar870515d2015-08-31 16:37:24 +0530563 &target_boot_params) == 0) {
Joonwoo Park61112782013-10-02 19:50:39 -0700564 have_target_boot_params = 1;
565 cmdline_len += strlen(target_boot_params);
566 }
567
Ajay Dudanid04110c2011-01-17 23:55:07 -0800568 /* Determine correct androidboot.baseband to use */
569 switch(target_baseband())
570 {
571 case BASEBAND_APQ:
572 cmdline_len += strlen(baseband_apq);
573 break;
574
575 case BASEBAND_MSM:
576 cmdline_len += strlen(baseband_msm);
577 break;
578
579 case BASEBAND_CSFB:
580 cmdline_len += strlen(baseband_csfb);
581 break;
582
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800583 case BASEBAND_SVLTE2A:
584 cmdline_len += strlen(baseband_svlte2a);
Ajay Dudanid04110c2011-01-17 23:55:07 -0800585 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700586
587 case BASEBAND_MDM:
588 cmdline_len += strlen(baseband_mdm);
589 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700590
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800591 case BASEBAND_MDM2:
592 cmdline_len += strlen(baseband_mdm2);
593 break;
594
Amol Jadi5c61a952012-05-04 17:05:35 -0700595 case BASEBAND_SGLTE:
596 cmdline_len += strlen(baseband_sglte);
597 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530598
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800599 case BASEBAND_SGLTE2:
600 cmdline_len += strlen(baseband_sglte2);
601 break;
602
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530603 case BASEBAND_DSDA:
604 cmdline_len += strlen(baseband_dsda);
605 break;
Amol Jadi2a15a272013-01-22 12:03:36 -0800606
607 case BASEBAND_DSDA2:
608 cmdline_len += strlen(baseband_dsda2);
609 break;
Vijay Kumar Pendotib228cfc2016-06-13 20:15:23 +0530610 case BASEBAND_APQ_NOWGR:
611 cmdline_len += strlen(baseband_apq_nowgr);
612 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800613 }
614
Vladislav Levenetz3cd45242017-04-05 08:58:07 +0300615#if ENABLE_DISPLAY
Lijuan Gao4a5b8322014-07-24 10:38:42 +0800616 if (cmdline) {
617 if ((strstr(cmdline, DISPLAY_DEFAULT_PREFIX) == NULL) &&
Padmanabhan Komandurubccbcdc2015-06-30 16:19:24 +0530618 target_display_panel_node(display_panel_buf,
619 MAX_PANEL_BUF_SIZE) &&
Lijuan Gao4a5b8322014-07-24 10:38:42 +0800620 strlen(display_panel_buf)) {
621 cmdline_len += strlen(display_panel_buf);
622 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700623 }
Vladislav Levenetz3cd45242017-04-05 08:58:07 +0300624#endif
Dhaval Patel223ec952013-07-18 14:49:44 -0700625
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800626 if (target_warm_boot()) {
627 warm_boot = true;
628 cmdline_len += strlen(warmboot_cmdline);
629 }
630
Mayank Grover5384ed82018-05-09 12:09:24 +0530631 if (target_uses_system_as_root() ||
632 partition_multislot_is_supported())
Mayank Grover351a75e2017-05-30 20:06:08 +0530633 {
634 current_active_slot = partition_find_active_slot();
635 cmdline_len += (strlen(androidboot_slot_suffix)+
636 strlen(SUFFIX_SLOT(current_active_slot)));
637
Mayank Grover3804be72017-06-22 11:59:23 +0530638 system_ptn_index = partition_get_index("system");
639 if (platform_boot_dev_isemmc())
640 {
Sourabh Banerjee386b1322018-02-27 09:37:28 +0530641#if VERITY_LE
642 /*
643 Condition 4: Verity and A/B both enabled
644 Eventual command line looks like:
645 ... androidboot.slot_suffix=<slot_suffix> ... rootfstype=ext4 ...
646 ... root=/dev/dm-0 dm="system_<slot_suffix> none ro,0 1 android-verity /dev/mmcblk0p<NN>"
647 */
648 snprintf(syspath_buf, syspath_buflen, " %s %s%s %s%d\"",
649 verity_dev,
650 verity_system_part, suffix_slot[current_active_slot],
651 verity_params, system_ptn_index + 1);
652#else
653 /*
654 Condition 5: A/B enabled, but verity disabled
655 Eventual command line looks like:
656 ... androidboot.slot_suffix=<slot_suffix> ... rootfstype=ext4 ...
657 ... root=/dev/mmcblk0p<NN> ...
658 */
659 snprintf(syspath_buf, syspath_buflen, " %s%d",
660 sys_path, system_ptn_index + 1);
661#endif
Mayank Grover3804be72017-06-22 11:59:23 +0530662 }
663 else
664 {
665 lun = partition_get_lun(system_ptn_index);
666 snprintf(syspath_buf, syspath_buflen, " root=/dev/sd%c%d",
667 lun_char_base + lun,
668 partition_get_index_in_lun("system", lun));
669 }
Mayank Grover351a75e2017-05-30 20:06:08 +0530670
Monika Singh292b3e92018-03-17 22:40:23 +0530671#ifndef VERIFIED_BOOT_2
Mayank Grover3804be72017-06-22 11:59:23 +0530672 cmdline_len += strlen(syspath_buf);
Monika Singh292b3e92018-03-17 22:40:23 +0530673#endif
Mayank Grover5384ed82018-05-09 12:09:24 +0530674 }
675
676 if (target_uses_system_as_root() ||
677 partition_multislot_is_supported())
678 {
679 cmdline_len += strlen(sys_path_cmdline);
Mayank Groverb716edf2019-05-08 16:06:55 +0530680
681 /* For dynamic partition, support skip skip_initramfs */
682 if (!target_dynamic_partition_supported() &&
683 !boot_into_recovery)
Mayank Grover351a75e2017-05-30 20:06:08 +0530684 cmdline_len += strlen(skip_ramfs);
685 }
686
Mayank Grover466d6562018-05-10 14:52:20 +0530687#if HIBERNATION_SUPPORT
688 if (platform_boot_dev_isemmc())
689 {
690 swap_ptn_index = partition_get_index("swap");
691 if (swap_ptn_index != INVALID_PTN)
692 {
693 snprintf(resume_buf, resume_buflen,
694 " %s%d", resume,
695 (swap_ptn_index + 1));
696 cmdline_len += strlen(resume_buf);
697 }
698 else
699 {
700 dprintf(INFO, "WARNING: swap partition not found\n");
701 }
702 }
703#endif
704
Channagoud Kadabi85c7ec32016-01-28 23:09:21 -0800705#if TARGET_CMDLINE_SUPPORT
706 char *target_cmdline_buf = malloc(TARGET_MAX_CMDLNBUF);
707 int target_cmd_line_len;
708 ASSERT(target_cmdline_buf);
709 target_cmd_line_len = target_update_cmdline(target_cmdline_buf);
710 cmdline_len += target_cmd_line_len;
711#endif
712
lijuang83ef4b22018-08-23 11:01:55 +0800713#if !VERITY_LE
714 dtbo_idx = get_dtbo_idx ();
715 if (dtbo_idx != INVALID_PTN) {
716 snprintf(dtbo_idx_str, sizeof(dtbo_idx_str), "%s%d",
717 android_boot_dtbo_idx, dtbo_idx);
718 cmdline_len += strlen (dtbo_idx_str);
719 }
Parth Dixit1375d9e2019-07-08 20:56:13 +0530720
721 dtb_idx = get_dtb_idx ();
722 if (dtb_idx != INVALID_PTN) {
723 snprintf(dtb_idx_str, sizeof(dtb_idx_str), "%s%d",
724 android_boot_dtb_idx, dtb_idx);
725 cmdline_len += strlen (dtb_idx_str);
726 }
lijuang83ef4b22018-08-23 11:01:55 +0800727#endif
728
David Ng183a7422009-12-07 14:55:21 -0800729 if (cmdline_len > 0) {
730 const char *src;
Maria Yu52254c02014-07-04 16:14:54 +0800731 unsigned char *dst;
732
733 cmdline_final = (unsigned char*) malloc((cmdline_len + 4) & (~3));
734 ASSERT(cmdline_final != NULL);
vijay kumar287bb542015-09-29 13:01:52 +0530735 memset((void *)cmdline_final, 0, sizeof(*cmdline_final));
Maria Yu52254c02014-07-04 16:14:54 +0800736 dst = cmdline_final;
Neeti Desaie245d492012-06-01 12:52:13 -0700737
Amol Jadi168b7712012-03-06 16:15:00 -0800738 /* Save start ptr for debug print */
David Ng183a7422009-12-07 14:55:21 -0800739 if (have_cmdline) {
740 src = cmdline;
741 while ((*dst++ = *src++));
742 }
743 if (target_is_emmc_boot()) {
744 src = emmc_cmdline;
745 if (have_cmdline) --dst;
David Ngf773dde2010-07-26 19:55:08 -0700746 have_cmdline = 1;
747 while ((*dst++ = *src++));
Sridhar Parasuram7bd4aaf2015-02-12 11:14:38 -0800748#if USE_BOOTDEV_CMDLINE
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700749 src = boot_dev_buf;
Mayank Groverb716edf2019-05-08 16:06:55 +0530750 if (have_cmdline &&
751 boot_dev_buf) {
752 --dst;
753 while ((*dst++ = *src++));
754 }
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700755#endif
Mayank Groverb716edf2019-05-08 16:06:55 +0530756 /* Dynamic partition append boot_devices */
757 if (target_dynamic_partition_supported() &&
758 boot_dev_buf) {
759 src = dynamic_bootdev_cmdline;
760 if (have_cmdline) --dst;
761 while ((*dst++ = *src++));
762 src = boot_dev_buf;
763 if (have_cmdline) --dst;
764 while ((*dst++ = *src++));
765 }
David Ngf773dde2010-07-26 19:55:08 -0700766 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800767
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700768#if VERIFIED_BOOT
Mayank Grover912eaa62017-10-26 12:08:53 +0530769 if (VB_M <= target_get_vb_version())
Sridhar Parasuram7d8bb9a2015-09-23 18:21:19 -0700770 {
Mayank Grover889be1b2017-09-12 20:12:23 +0530771 src = verified_state;
772 if(have_cmdline) --dst;
773 have_cmdline = 1;
774 while ((*dst++ = *src++));
775 src = vbsn[boot_state].name;
776 if(have_cmdline) --dst;
777 while ((*dst++ = *src++));
778
779 if ((device.verity_mode != 0 ) && (device.verity_mode != 1))
780 {
781 dprintf(CRITICAL, "Devinfo paritition possibly corrupted!!!. Please erase devinfo partition to continue booting\n");
782 ASSERT(0);
783 }
784 src = verity_mode;
785 if(have_cmdline) --dst;
786 while ((*dst++ = *src++));
787 src = vbvm[device.verity_mode].name;
788 if(have_cmdline) -- dst;
789 while ((*dst++ = *src++));
790 src = keymaster_v1;
791 if(have_cmdline) --dst;
792 while ((*dst++ = *src++));
Sridhar Parasuram7d8bb9a2015-09-23 18:21:19 -0700793 }
Parth Dixitddbc7352015-10-18 03:13:31 +0530794#endif
Monika Singh292b3e92018-03-17 22:40:23 +0530795
796 if (vbcmdline != NULL) {
797 src = vbcmdline;
798 if (have_cmdline) --dst;
799 while ((*dst++ = *src++));
800 }
801
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800802 src = usb_sn_cmdline;
803 if (have_cmdline) --dst;
804 have_cmdline = 1;
805 while ((*dst++ = *src++));
806 src = sn_buf;
807 if (have_cmdline) --dst;
808 have_cmdline = 1;
809 while ((*dst++ = *src++));
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800810 if (warm_boot) {
811 if (have_cmdline) --dst;
812 src = warmboot_cmdline;
813 while ((*dst++ = *src++));
814 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800815
Pavel Nedev5614d222013-06-17 18:01:02 +0300816 if (boot_into_recovery && gpt_exists) {
817 src = secondary_gpt_enable;
818 if (have_cmdline) --dst;
819 while ((*dst++ = *src++));
820 }
Monika Singh292b3e92018-03-17 22:40:23 +0530821#ifdef MDTP_SUPPORT
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200822 if (is_mdtp_activated) {
823 src = mdtp_activated_flag;
824 if (have_cmdline) --dst;
825 while ((*dst++ = *src++));
826 }
Monika Singh292b3e92018-03-17 22:40:23 +0530827#endif
Pavel Nedev328ac822013-04-05 15:25:11 +0300828 if (boot_into_ffbm) {
829 src = androidboot_mode;
830 if (have_cmdline) --dst;
831 while ((*dst++ = *src++));
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700832 src = ffbm_mode_string;
Pavel Nedev328ac822013-04-05 15:25:11 +0300833 if (have_cmdline) --dst;
834 while ((*dst++ = *src++));
Hareesh Gauthamabf1e1e2017-04-12 18:09:24 +0530835
836 if(is_systemd_present) {
837 src = systemd_ffbm_mode;
838 if (have_cmdline) --dst;
839 while ((*dst++ = *src++));
840 }
841
Pavel Nedev898298c2013-02-27 12:36:09 -0800842 src = loglevel;
843 if (have_cmdline) --dst;
844 while ((*dst++ = *src++));
Matthew Qind886f3c2014-01-17 16:52:01 +0800845 } else if (boot_reason_alarm) {
846 src = alarmboot_cmdline;
847 if (have_cmdline) --dst;
848 while ((*dst++ = *src++));
Pavel Nedev328ac822013-04-05 15:25:11 +0300849 } else if (pause_at_bootup) {
David Ngf773dde2010-07-26 19:55:08 -0700850 src = battchg_pause;
851 if (have_cmdline) --dst;
David Ng183a7422009-12-07 14:55:21 -0800852 while ((*dst++ = *src++));
853 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800854
Shashank Mittalcd98d472011-08-02 14:29:24 -0700855 if(target_use_signed_kernel() && auth_kernel_img) {
856 src = auth_kernel;
857 if (have_cmdline) --dst;
858 while ((*dst++ = *src++));
859 }
860
Ajay Dudanid04110c2011-01-17 23:55:07 -0800861 switch(target_baseband())
862 {
863 case BASEBAND_APQ:
864 src = baseband_apq;
865 if (have_cmdline) --dst;
866 while ((*dst++ = *src++));
867 break;
868
869 case BASEBAND_MSM:
870 src = baseband_msm;
871 if (have_cmdline) --dst;
872 while ((*dst++ = *src++));
873 break;
874
875 case BASEBAND_CSFB:
876 src = baseband_csfb;
877 if (have_cmdline) --dst;
878 while ((*dst++ = *src++));
879 break;
880
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800881 case BASEBAND_SVLTE2A:
882 src = baseband_svlte2a;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800883 if (have_cmdline) --dst;
884 while ((*dst++ = *src++));
885 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700886
887 case BASEBAND_MDM:
888 src = baseband_mdm;
889 if (have_cmdline) --dst;
890 while ((*dst++ = *src++));
891 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700892
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800893 case BASEBAND_MDM2:
894 src = baseband_mdm2;
895 if (have_cmdline) --dst;
896 while ((*dst++ = *src++));
897 break;
898
Amol Jadi5c61a952012-05-04 17:05:35 -0700899 case BASEBAND_SGLTE:
900 src = baseband_sglte;
901 if (have_cmdline) --dst;
902 while ((*dst++ = *src++));
903 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530904
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800905 case BASEBAND_SGLTE2:
906 src = baseband_sglte2;
907 if (have_cmdline) --dst;
908 while ((*dst++ = *src++));
909 break;
910
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530911 case BASEBAND_DSDA:
912 src = baseband_dsda;
913 if (have_cmdline) --dst;
914 while ((*dst++ = *src++));
915 break;
Amol Jadi2a15a272013-01-22 12:03:36 -0800916
917 case BASEBAND_DSDA2:
918 src = baseband_dsda2;
919 if (have_cmdline) --dst;
920 while ((*dst++ = *src++));
921 break;
Vijay Kumar Pendotib228cfc2016-06-13 20:15:23 +0530922 case BASEBAND_APQ_NOWGR:
923 src = baseband_apq_nowgr;
924 if (have_cmdline) --dst;
925 while ((*dst++ = *src++));
926 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800927 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700928
929 if (strlen(display_panel_buf)) {
Dhaval Patel223ec952013-07-18 14:49:44 -0700930 src = display_panel_buf;
931 if (have_cmdline) --dst;
932 while ((*dst++ = *src++));
933 }
Joonwoo Park61112782013-10-02 19:50:39 -0700934
935 if (have_target_boot_params) {
936 if (have_cmdline) --dst;
937 src = target_boot_params;
938 while ((*dst++ = *src++));
vijay kumar870515d2015-08-31 16:37:24 +0530939 free(target_boot_params);
Joonwoo Park61112782013-10-02 19:50:39 -0700940 }
Channagoud Kadabi85c7ec32016-01-28 23:09:21 -0800941
Mayank Grover351a75e2017-05-30 20:06:08 +0530942 if (partition_multislot_is_supported() && have_cmdline)
943 {
944 src = androidboot_slot_suffix;
945 --dst;
946 while ((*dst++ = *src++));
947 --dst;
948 src = SUFFIX_SLOT(current_active_slot);
949 while ((*dst++ = *src++));
Mayank Grover5384ed82018-05-09 12:09:24 +0530950 }
Mayank Grover351a75e2017-05-30 20:06:08 +0530951
Mayank Grover351a75e2017-05-30 20:06:08 +0530952
Mayank Grover5384ed82018-05-09 12:09:24 +0530953 /*
954 * System-As-Root behaviour, system.img should contain both
955 * system content and ramdisk content, and should be mounted at
956 * root(a/b).
957 * Apending skip_ramfs for non a/b builds which use, system as root.
958 */
959 if ((target_uses_system_as_root() ||
960 partition_multislot_is_supported()) &&
961 have_cmdline)
962 {
Mayank Groverb716edf2019-05-08 16:06:55 +0530963 if (!target_dynamic_partition_supported() &&
964 !boot_into_recovery)
Mayank Grover5384ed82018-05-09 12:09:24 +0530965 {
966 src = skip_ramfs;
Mayank Grover351a75e2017-05-30 20:06:08 +0530967 --dst;
968 while ((*dst++ = *src++));
Mayank Grover5384ed82018-05-09 12:09:24 +0530969 }
970
971 src = sys_path_cmdline;
972 --dst;
973 while ((*dst++ = *src++));
Mayank Grover3804be72017-06-22 11:59:23 +0530974
Monika Singh292b3e92018-03-17 22:40:23 +0530975#ifndef VERIFIED_BOOT_2
Mayank Grover5384ed82018-05-09 12:09:24 +0530976 src = syspath_buf;
977 --dst;
978 while ((*dst++ = *src++));
Monika Singh292b3e92018-03-17 22:40:23 +0530979#endif
Mayank Grover351a75e2017-05-30 20:06:08 +0530980 }
981
Mayank Grover466d6562018-05-10 14:52:20 +0530982#if HIBERNATION_SUPPORT
983 if (swap_ptn_index != INVALID_PTN)
984 {
985 src = resume_buf;
986 --dst;
987 while ((*dst++ = *src++));
988 }
989#endif
990
Channagoud Kadabi85c7ec32016-01-28 23:09:21 -0800991#if TARGET_CMDLINE_SUPPORT
992 if (target_cmdline_buf && target_cmd_line_len)
993 {
994 if (have_cmdline) --dst;
995 src = target_cmdline_buf;
996 while((*dst++ = *src++));
997 free(target_cmdline_buf);
998 }
999#endif
lijuang83ef4b22018-08-23 11:01:55 +08001000
1001#if !VERITY_LE
1002 if (dtbo_idx != INVALID_PTN) {
1003 src = dtbo_idx_str;
1004 --dst;
1005 while ((*dst++ = *src++));
1006 }
Parth Dixit1375d9e2019-07-08 20:56:13 +05301007
1008 if (dtb_idx != INVALID_PTN) {
1009 src = dtb_idx_str;
1010 --dst;
1011 while ((*dst++ = *src++));
1012 }
lijuang83ef4b22018-08-23 11:01:55 +08001013#endif
Neeti Desaie245d492012-06-01 12:52:13 -07001014 }
Dhaval Patel223ec952013-07-18 14:49:44 -07001015
1016
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -07001017 if (boot_dev_buf)
1018 free(boot_dev_buf);
1019
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -08001020 if (cmdline_final)
1021 dprintf(INFO, "cmdline: %s\n", cmdline_final);
1022 else
1023 dprintf(INFO, "cmdline is NULL\n");
Neeti Desaie245d492012-06-01 12:52:13 -07001024 return cmdline_final;
1025}
1026
1027unsigned *atag_core(unsigned *ptr)
1028{
1029 /* CORE */
1030 *ptr++ = 2;
1031 *ptr++ = 0x54410001;
1032
1033 return ptr;
1034
1035}
1036
1037unsigned *atag_ramdisk(unsigned *ptr, void *ramdisk,
1038 unsigned ramdisk_size)
1039{
1040 if (ramdisk_size) {
1041 *ptr++ = 4;
1042 *ptr++ = 0x54420005;
1043 *ptr++ = (unsigned)ramdisk;
1044 *ptr++ = ramdisk_size;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001045 }
1046
Neeti Desaie245d492012-06-01 12:52:13 -07001047 return ptr;
1048}
1049
1050unsigned *atag_ptable(unsigned **ptr_addr)
1051{
1052 int i;
1053 struct ptable *ptable;
1054
1055 if ((ptable = flash_get_ptable()) && (ptable->count != 0)) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001056 *(*ptr_addr)++ = 2 + (ptable->count * (sizeof(struct atag_ptbl_entry) /
1057 sizeof(unsigned)));
Neeti Desaie245d492012-06-01 12:52:13 -07001058 *(*ptr_addr)++ = 0x4d534d70;
1059 for (i = 0; i < ptable->count; ++i)
1060 ptentry_to_tag(ptr_addr, ptable_get(ptable, i));
1061 }
1062
1063 return (*ptr_addr);
1064}
1065
1066unsigned *atag_cmdline(unsigned *ptr, const char *cmdline)
1067{
1068 int cmdline_length = 0;
1069 int n;
Neeti Desaie245d492012-06-01 12:52:13 -07001070 char *dest;
1071
Amol Jadi10c7d1c2013-01-25 13:24:29 -08001072 cmdline_length = strlen((const char*)cmdline);
Neeti Desaie245d492012-06-01 12:52:13 -07001073 n = (cmdline_length + 4) & (~3);
1074
1075 *ptr++ = (n / 4) + 2;
1076 *ptr++ = 0x54410009;
1077 dest = (char *) ptr;
Amol Jadi10c7d1c2013-01-25 13:24:29 -08001078 while ((*dest++ = *cmdline++));
Neeti Desaie245d492012-06-01 12:52:13 -07001079 ptr += (n / 4);
1080
1081 return ptr;
1082}
1083
1084unsigned *atag_end(unsigned *ptr)
1085{
Brian Swetland9c4c0752009-01-25 16:23:50 -08001086 /* END */
1087 *ptr++ = 0;
1088 *ptr++ = 0;
1089
Neeti Desaie245d492012-06-01 12:52:13 -07001090 return ptr;
1091}
1092
1093void generate_atags(unsigned *ptr, const char *cmdline,
1094 void *ramdisk, unsigned ramdisk_size)
1095{
vijay kumar21a37452015-12-29 16:23:21 +05301096 unsigned *orig_ptr = ptr;
Neeti Desaie245d492012-06-01 12:52:13 -07001097 ptr = atag_core(ptr);
1098 ptr = atag_ramdisk(ptr, ramdisk, ramdisk_size);
1099 ptr = target_atag_mem(ptr);
1100
1101 /* Skip NAND partition ATAGS for eMMC boot */
1102 if (!target_is_emmc_boot()){
1103 ptr = atag_ptable(&ptr);
1104 }
1105
vijay kumar21a37452015-12-29 16:23:21 +05301106 /*
1107 * Atags size filled till + cmdline size + 1 unsigned for 4-byte boundary + 4 unsigned
1108 * for atag identifier in atag_cmdline and atag_end should be with in MAX_TAGS_SIZE bytes
1109 */
Mayank Groverbc8a2ef2018-02-23 18:03:36 +05301110 if (!cmdline)
1111 return;
1112
vijay kumar21a37452015-12-29 16:23:21 +05301113 if (((ptr - orig_ptr) + strlen(cmdline) + 5 * sizeof(unsigned)) < MAX_TAGS_SIZE) {
1114 ptr = atag_cmdline(ptr, cmdline);
1115 ptr = atag_end(ptr);
1116 }
1117 else {
1118 dprintf(CRITICAL,"Crossing ATAGs Max size allowed\n");
1119 ASSERT(0);
1120 }
Neeti Desaie245d492012-06-01 12:52:13 -07001121}
1122
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001123typedef void entry_func_ptr(unsigned, unsigned, unsigned*);
Neeti Desaie245d492012-06-01 12:52:13 -07001124void boot_linux(void *kernel, unsigned *tags,
1125 const char *cmdline, unsigned machtype,
1126 void *ramdisk, unsigned ramdisk_size)
1127{
Amol Jadi10c7d1c2013-01-25 13:24:29 -08001128 unsigned char *final_cmdline;
Amol Jadib6be5c12012-11-14 13:39:51 -08001129#if DEVICE_TREE
Neeti Desai17379b82012-06-04 18:42:53 -07001130 int ret = 0;
Amol Jadib6be5c12012-11-14 13:39:51 -08001131#endif
1132
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001133 void (*entry)(unsigned, unsigned, unsigned*) = (entry_func_ptr*)(PA((addr_t)kernel));
Deepa Dinamani25a9f762012-11-30 15:57:15 -08001134 uint32_t tags_phys = PA((addr_t)tags);
vijay kumar1a50a642015-11-16 12:41:15 +05301135 struct kernel64_hdr *kptr = ((struct kernel64_hdr*)(PA((addr_t)kernel)));
Deepa Dinamani25a9f762012-11-30 15:57:15 -08001136
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301137 ramdisk = (void *)PA((addr_t)ramdisk);
Neeti Desaie245d492012-06-01 12:52:13 -07001138
Amol Jadi10c7d1c2013-01-25 13:24:29 -08001139 final_cmdline = update_cmdline((const char*)cmdline);
1140
Neeti Desai17379b82012-06-04 18:42:53 -07001141#if DEVICE_TREE
Amol Jadib6be5c12012-11-14 13:39:51 -08001142 dprintf(INFO, "Updating device tree: start\n");
1143
Neeti Desai17379b82012-06-04 18:42:53 -07001144 /* Update the Device Tree */
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301145 ret = update_device_tree((void *)tags,(const char *)final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -07001146 if(ret)
1147 {
1148 dprintf(CRITICAL, "ERROR: Updating Device Tree Failed \n");
1149 ASSERT(0);
1150 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001151 dprintf(INFO, "Updating device tree: done\n");
Neeti Desai17379b82012-06-04 18:42:53 -07001152#else
Neeti Desaie245d492012-06-01 12:52:13 -07001153 /* Generating the Atags */
Amol Jadi10c7d1c2013-01-25 13:24:29 -08001154 generate_atags(tags, final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -07001155#endif
Neeti Desaie245d492012-06-01 12:52:13 -07001156
Monika Singh0ec6db82019-07-18 16:57:58 +05301157#if VERIFIED_BOOT
Monika Singhb0db4b82018-09-26 12:18:02 +05301158 if (VB_M <= target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +05301159 {
1160 if (device.verity_mode == 0) {
lijuangbdd9bb42016-03-01 18:22:17 +08001161#if FBCON_DISPLAY_MSG
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07001162#if ENABLE_VB_ATTEST
Mayank Grover889be1b2017-09-12 20:12:23 +05301163 display_bootverify_menu(DISPLAY_MENU_EIO);
1164 wait_for_users_action();
1165 if(!pwr_key_is_pressed)
1166 shutdown_device();
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07001167#else
Mayank Grover889be1b2017-09-12 20:12:23 +05301168 display_bootverify_menu(DISPLAY_MENU_LOGGING);
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07001169#endif
Mayank Grover889be1b2017-09-12 20:12:23 +05301170 wait_for_users_action();
lijuangbdd9bb42016-03-01 18:22:17 +08001171#else
Mayank Grover889be1b2017-09-12 20:12:23 +05301172 dprintf(CRITICAL,
1173 "The dm-verity is not started in enforcing mode.\nWait for 5 seconds before proceeding\n");
1174 mdelay(5000);
lijuangbdd9bb42016-03-01 18:22:17 +08001175#endif
Mayank Grover889be1b2017-09-12 20:12:23 +05301176 }
lijuangbdd9bb42016-03-01 18:22:17 +08001177 }
lijuangbdd9bb42016-03-01 18:22:17 +08001178#endif
1179
1180#if VERIFIED_BOOT
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001181 /* Write protect the device info */
Channagoud Kadabi3bd9d1e2015-05-05 16:18:20 -07001182 if (!boot_into_recovery && target_build_variant_user() && devinfo_present && mmc_write_protect("devinfo", 1))
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001183 {
1184 dprintf(INFO, "Failed to write protect dev info\n");
1185 ASSERT(0);
1186 }
1187#endif
1188
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08001189 /* Turn off splash screen if enabled */
1190#if DISPLAY_SPLASH_SCREEN
1191 target_display_shutdown();
1192#endif
1193
Veera Sundaram Sankaran67ea0932015-09-25 10:09:30 -07001194 /* Perform target specific cleanup */
1195 target_uninit();
Monika Singh292b3e92018-03-17 22:40:23 +05301196 free_verified_boot_resource(&info);
1197 if (final_cmdline)
1198 free(final_cmdline);
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08001199
Deepa Dinamani33734bc2013-03-06 12:16:06 -08001200 dprintf(INFO, "booting linux @ %p, ramdisk @ %p (%d), tags/device tree @ %p\n",
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301201 entry, ramdisk, ramdisk_size, (void *)tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001202
1203 enter_critical_section();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001204
Amol Jadi4421e652011-06-16 15:00:48 -07001205 /* do any platform specific cleanup before kernel entry */
1206 platform_uninit();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001207
Brian Swetland9c4c0752009-01-25 16:23:50 -08001208 arch_disable_cache(UCACHE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001209
Amol Jadi504f9fe2012-08-16 13:56:48 -07001210#if ARM_WITH_MMU
Brian Swetland9c4c0752009-01-25 16:23:50 -08001211 arch_disable_mmu();
Amol Jadi504f9fe2012-08-16 13:56:48 -07001212#endif
Amol Jadi492d5a52013-03-15 16:12:34 -07001213 bs_set_timestamp(BS_KERNEL_ENTRY);
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001214
1215 if (IS_ARM64(kptr))
1216 /* Jump to a 64bit kernel */
1217 scm_elexec_call((paddr_t)kernel, tags_phys);
1218 else
1219 /* Jump to a 32bit kernel */
1220 entry(0, machtype, (unsigned*)tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001221}
1222
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001223/* Function to check if the memory address range falls within the aboot
1224 * boundaries.
1225 * start: Start of the memory region
1226 * size: Size of the memory region
1227 */
Vijay Kumar Pendoti9c002ad2016-03-09 13:52:45 +05301228int check_aboot_addr_range_overlap(uintptr_t start, uint32_t size)
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001229{
1230 /* Check for boundary conditions. */
Sundarajan Srinivasance2a0ea2013-12-16 17:02:56 -08001231 if ((UINT_MAX - start) < size)
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001232 return -1;
1233
1234 /* Check for memory overlap. */
1235 if ((start < MEMBASE) && ((start + size) <= MEMBASE))
1236 return 0;
Channagoud Kadabi94143912013-10-15 12:53:52 -07001237 else if (start >= (MEMBASE + MEMSIZE))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001238 return 0;
1239 else
1240 return -1;
1241}
1242
Mayank Grovere559cec2017-10-17 15:12:03 +05301243/* Function to check if the memory address range falls beyond ddr region.
1244 * start: Start of the memory region
1245 * size: Size of the memory region
1246 */
1247int check_ddr_addr_range_bound(uintptr_t start, uint32_t size)
1248{
1249 uintptr_t ddr_pa_start_addr = PA(get_ddr_start());
1250 uint64_t ddr_size = smem_get_ddr_size();
1251 uint64_t ddr_pa_end_addr = ddr_pa_start_addr + ddr_size;
1252 uintptr_t pa_start_addr = PA(start);
1253
1254 /* Check for boundary conditions. */
1255 if ((UINT_MAX - start) < size)
1256 return -1;
1257
1258 /* Check if memory range is beyond the ddr range. */
1259 if (pa_start_addr < ddr_pa_start_addr ||
1260 pa_start_addr >= (ddr_pa_end_addr) ||
1261 (pa_start_addr + size) > ddr_pa_end_addr)
1262 return -1;
1263 else
1264 return 0;
1265}
1266
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001267BUF_DMA_ALIGN(buf, BOOT_IMG_MAX_PAGE_SIZE); //Equal to max-supported pagesize
Dima Zavin214cc642009-01-26 11:16:21 -08001268
Mayank Groverdd080e82018-09-04 20:12:13 +05301269int getimage(void **image_buffer, uint32_t *imgsize,
1270 const char *imgname)
Monika Singh292b3e92018-03-17 22:40:23 +05301271{
Mayank Groverdd080e82018-09-04 20:12:13 +05301272 uint32_t loadedindex;
1273 if (image_buffer == NULL || imgsize == NULL ||
Monika Singh292b3e92018-03-17 22:40:23 +05301274 imgname == NULL) {
1275 dprintf(CRITICAL, "getimage: invalid parameters\n");
1276 return -1;
1277 }
Mayank Groverdd080e82018-09-04 20:12:13 +05301278 for (loadedindex = 0; loadedindex < info.num_loaded_images; loadedindex++) {
1279 if (!strncmp(info.images[loadedindex].name, imgname,
Monika Singh292b3e92018-03-17 22:40:23 +05301280 strlen(imgname))) {
Mayank Groverdd080e82018-09-04 20:12:13 +05301281 *image_buffer = info.images[loadedindex].image_buffer;
1282 *imgsize = info.images[loadedindex].imgsize;
Mayank Grover027a9412018-09-04 15:12:05 +05301283 dprintf(SPEW, "getimage(): Loaded image [%s|%d]\n",
1284 info.images[loadedindex].name,
1285 info.images[loadedindex].imgsize);
Monika Singh292b3e92018-03-17 22:40:23 +05301286 return 0;
1287 }
1288 }
1289 return -1;
1290}
1291
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001292static void verify_signed_bootimg(uint32_t bootimg_addr, uint32_t bootimg_size)
1293{
1294 int ret;
Channagoud Kadabia8c623f2015-01-13 14:48:48 -08001295
1296#if !VERIFIED_BOOT
Sundarajan Srinivasance54d6e2013-11-11 12:45:00 -08001297#if IMAGE_VERIF_ALGO_SHA1
1298 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA1;
1299#else
1300 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA256;
1301#endif
Channagoud Kadabia8c623f2015-01-13 14:48:48 -08001302#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001303
1304 /* Assume device is rooted at this time. */
1305 device.is_tampered = 1;
1306
1307 dprintf(INFO, "Authenticating boot image (%d): start\n", bootimg_size);
1308
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001309#if VERIFIED_BOOT
Monika Singh7d2fc272018-03-16 17:16:01 +05301310 uint32_t bootstate;
1311 if(boot_into_recovery &&
Mayank Grover68fbf0d2017-10-24 14:13:39 +05301312 (!partition_multislot_is_supported()))
Monika Singh7d2fc272018-03-16 17:16:01 +05301313 {
1314 ret = boot_verify_image((unsigned char *)bootimg_addr,
1315 bootimg_size, "/recovery", &bootstate);
1316 }
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001317 else
Monika Singh7d2fc272018-03-16 17:16:01 +05301318 {
1319 ret = boot_verify_image((unsigned char *)bootimg_addr,
1320 bootimg_size, "/boot", &bootstate);
1321 }
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001322 boot_verify_print_state();
1323#else
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001324 ret = image_verify((unsigned char *)bootimg_addr,
1325 (unsigned char *)(bootimg_addr + bootimg_size),
1326 bootimg_size,
Sundarajan Srinivasance54d6e2013-11-11 12:45:00 -08001327 auth_algo);
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001328#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001329 dprintf(INFO, "Authenticating boot image: done return value = %d\n", ret);
1330
1331 if (ret)
1332 {
1333 /* Authorized kernel */
1334 device.is_tampered = 0;
Sundarajan Srinivasan3fb21f12013-09-16 18:36:15 -07001335 auth_kernel_img = 1;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001336 }
1337
Amit Blay4aa292f2015-04-28 21:55:59 +03001338#ifdef MDTP_SUPPORT
1339 {
1340 /* Verify MDTP lock.
1341 * For boot & recovery partitions, use aboot's verification result.
1342 */
1343 mdtp_ext_partition_verification_t ext_partition;
1344 ext_partition.partition = boot_into_recovery ? MDTP_PARTITION_RECOVERY : MDTP_PARTITION_BOOT;
1345 ext_partition.integrity_state = device.is_tampered ? MDTP_PARTITION_STATE_INVALID : MDTP_PARTITION_STATE_VALID;
1346 ext_partition.page_size = 0; /* Not needed since already validated */
1347 ext_partition.image_addr = 0; /* Not needed since already validated */
1348 ext_partition.image_size = 0; /* Not needed since already validated */
1349 ext_partition.sig_avail = FALSE; /* Not needed since already validated */
1350 mdtp_fwlock_verify_lock(&ext_partition);
1351 }
1352#endif /* MDTP_SUPPORT */
1353
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001354#if USE_PCOM_SECBOOT
1355 set_tamper_flag(device.is_tampered);
1356#endif
1357
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001358#if VERIFIED_BOOT
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001359 switch(boot_verify_get_state())
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001360 {
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001361 case RED:
lijuanga40d6302015-07-20 20:10:13 +08001362#if FBCON_DISPLAY_MSG
lijuang9a7d3b92015-11-30 14:41:24 +08001363 display_bootverify_menu(DISPLAY_MENU_RED);
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07001364#if ENABLE_VB_ATTEST
1365 mdelay(DELAY_WAIT);
1366 shutdown_device();
1367#else
lijuanga40d6302015-07-20 20:10:13 +08001368 wait_for_users_action();
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07001369#endif
lijuanga40d6302015-07-20 20:10:13 +08001370#else
1371 dprintf(CRITICAL,
1372 "Your device has failed verification and may not work properly.\nWait for 5 seconds before proceeding\n");
1373 mdelay(5000);
1374#endif
1375
1376 break;
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001377 case YELLOW:
lijuanga40d6302015-07-20 20:10:13 +08001378#if FBCON_DISPLAY_MSG
lijuang9a7d3b92015-11-30 14:41:24 +08001379 display_bootverify_menu(DISPLAY_MENU_YELLOW);
lijuanga40d6302015-07-20 20:10:13 +08001380 wait_for_users_action();
1381#else
1382 dprintf(CRITICAL,
1383 "Your device has loaded a different operating system.\nWait for 5 seconds before proceeding\n");
1384 mdelay(5000);
1385#endif
1386 break;
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001387 default:
lijuanga40d6302015-07-20 20:10:13 +08001388 break;
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001389 }
1390#endif
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001391#if !VERIFIED_BOOT
Unnati Gandhi1be04752015-03-27 19:41:53 +05301392 if(device.is_tampered)
1393 {
1394 write_device_info_mmc(&device);
1395 #ifdef TZ_TAMPER_FUSE
Monika Singh98257462018-06-06 11:28:49 +05301396 set_tamper_fuse_cmd(HLOS_IMG_TAMPER_FUSE);
Unnati Gandhi1be04752015-03-27 19:41:53 +05301397 #endif
1398 #ifdef ASSERT_ON_TAMPER
1399 dprintf(CRITICAL, "Device is tampered. Asserting..\n");
1400 ASSERT(0);
1401 #endif
1402 }
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001403#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001404}
1405
Maunik Shah0f3c8ac2014-03-02 17:47:58 +05301406static bool check_format_bit()
1407{
1408 bool ret = false;
1409 int index;
1410 uint64_t offset;
1411 struct boot_selection_info *in = NULL;
1412 char *buf = NULL;
1413
1414 index = partition_get_index("bootselect");
1415 if (index == INVALID_PTN)
1416 {
1417 dprintf(INFO, "Unable to locate /bootselect partition\n");
1418 return ret;
1419 }
1420 offset = partition_get_offset(index);
1421 if(!offset)
1422 {
1423 dprintf(INFO, "partition /bootselect doesn't exist\n");
1424 return ret;
1425 }
1426 buf = (char *) memalign(CACHE_LINE, ROUNDUP(page_size, CACHE_LINE));
Mayank Grover48860402016-11-29 12:34:53 +05301427 mmc_set_lun(partition_get_lun(index));
Maunik Shah0f3c8ac2014-03-02 17:47:58 +05301428 ASSERT(buf);
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301429 if (mmc_read(offset, (uint32_t *)buf, page_size))
Maunik Shah0f3c8ac2014-03-02 17:47:58 +05301430 {
1431 dprintf(INFO, "mmc read failure /bootselect %d\n", page_size);
1432 free(buf);
1433 return ret;
1434 }
1435 in = (struct boot_selection_info *) buf;
1436 if ((in->signature == BOOTSELECT_SIGNATURE) &&
1437 (in->version == BOOTSELECT_VERSION)) {
1438 if ((in->state_info & BOOTSELECT_FORMAT) &&
1439 !(in->state_info & BOOTSELECT_FACTORY))
1440 ret = true;
1441 } else {
1442 dprintf(CRITICAL, "Signature: 0x%08x or version: 0x%08x mismatched of /bootselect\n",
1443 in->signature, in->version);
1444 ASSERT(0);
1445 }
1446 free(buf);
1447 return ret;
1448}
1449
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001450void boot_verifier_init()
1451{
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001452 uint32_t boot_state;
1453 /* Check if device unlock */
1454 if(device.is_unlocked)
1455 {
1456 boot_verify_send_event(DEV_UNLOCK);
1457 boot_verify_print_state();
1458 dprintf(CRITICAL, "Device is unlocked! Skipping verification...\n");
1459 return;
1460 }
1461 else
1462 {
1463 boot_verify_send_event(BOOT_INIT);
1464 }
1465
1466 /* Initialize keystore */
1467 boot_state = boot_verify_keystore_init();
1468 if(boot_state == YELLOW)
1469 {
1470 boot_verify_print_state();
1471 dprintf(CRITICAL, "Keystore verification failed! Continuing anyways...\n");
1472 }
1473}
1474
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301475/* Function to return recovery appended dtbo buffer info */
1476void get_recovery_dtbo_info(uint32_t *dtbo_size, void **dtbo_buf)
1477{
1478 *dtbo_size = recovery_dtbo_size;
1479 *dtbo_buf = recovery_dtbo_buf;
1480 return;
1481}
1482
Shashank Mittal23b8f422010-04-16 19:27:21 -07001483int boot_linux_from_mmc(void)
1484{
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301485 boot_img_hdr *hdr = (void*) buf;
1486 boot_img_hdr *uhdr;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001487 unsigned offset = 0;
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07001488 int rcode;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001489 unsigned long long ptn = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001490 int index = INVALID_PTN;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001491
Shashank Mittalcd98d472011-08-02 14:29:24 -07001492 unsigned char *image_addr = 0;
1493 unsigned kernel_actual;
1494 unsigned ramdisk_actual;
1495 unsigned imagesize_actual;
Neeti Desai465491e2012-07-31 12:53:35 -07001496 unsigned second_actual = 0;
Parth Dixit1375d9e2019-07-08 20:56:13 +05301497 void * image_buf = NULL;
Neeti Desai465491e2012-07-31 12:53:35 -07001498
Matthew Qin271927e2015-03-31 22:07:07 -04001499 unsigned int dtb_size = 0;
Parth Dixit1375d9e2019-07-08 20:56:13 +05301500 unsigned dtb_image_size = 0;
1501 uint32_t dtb_image_offset = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08001502 unsigned int out_len = 0;
1503 unsigned int out_avai_len = 0;
1504 unsigned char *out_addr = NULL;
1505 uint32_t dtb_offset = 0;
1506 unsigned char *kernel_start_addr = NULL;
1507 unsigned int kernel_size = 0;
Ameya Thakur10a33452016-06-13 14:24:26 -07001508 unsigned int patched_kernel_hdr_size = 0;
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301509 uint64_t image_size = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08001510 int rc;
Monika Singh292b3e92018-03-17 22:40:23 +05301511#if VERIFIED_BOOT_2
1512 int status;
Mayank Grover027a9412018-09-04 15:12:05 +05301513 void *dtbo_image_buf = NULL;
1514 uint32_t dtbo_image_sz = 0;
Mayank Grover9df84c02018-08-30 15:46:35 +05301515 void *vbmeta_image_buf = NULL;
1516 uint32_t vbmeta_image_sz = 0;
Monika Singh292b3e92018-03-17 22:40:23 +05301517#endif
Mayank Groverc93cad82017-10-03 12:23:45 +05301518 char *ptn_name = NULL;
Neeti Desai465491e2012-07-31 12:53:35 -07001519#if DEVICE_TREE
1520 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -07001521 struct dt_entry dt_entry;
Neeti Desai465491e2012-07-31 12:53:35 -07001522 unsigned dt_table_offset;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001523 uint32_t dt_actual;
Deepa Dinamani19648b42013-09-05 17:05:55 -07001524 uint32_t dt_hdr_size;
Matthew Qin271927e2015-03-31 22:07:07 -04001525 unsigned char *best_match_dt_addr = NULL;
Neeti Desai465491e2012-07-31 12:53:35 -07001526#endif
Matthew Qin49e51fa2015-02-09 10:40:45 +08001527 struct kernel64_hdr *kptr = NULL;
Mayank Grover351a75e2017-05-30 20:06:08 +05301528 int current_active_slot = INVALID;
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001529
Maunik Shah0f3c8ac2014-03-02 17:47:58 +05301530 if (check_format_bit())
1531 boot_into_recovery = 1;
1532
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07001533 if (!boot_into_recovery) {
1534 memset(ffbm_mode_string, '\0', sizeof(ffbm_mode_string));
1535 rcode = get_ffbm(ffbm_mode_string, sizeof(ffbm_mode_string));
1536 if (rcode <= 0) {
1537 boot_into_ffbm = false;
1538 if (rcode < 0)
1539 dprintf(CRITICAL,"failed to get ffbm cookie");
1540 } else
1541 boot_into_ffbm = true;
1542 } else
1543 boot_into_ffbm = false;
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301544 uhdr = (boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001545 if (!memcmp(uhdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1546 dprintf(INFO, "Unified boot method!\n");
1547 hdr = uhdr;
1548 goto unified_boot;
1549 }
Mayank Groverc93cad82017-10-03 12:23:45 +05301550
1551 /* For a/b recovery image code is on boot partition.
1552 If we support multislot, always use boot partition. */
1553 if (boot_into_recovery &&
Mayank Grover8ab4a762019-04-25 17:23:34 +05301554 ((!partition_multislot_is_supported()) ||
1555 (target_dynamic_partition_supported())))
Mayank Groverc93cad82017-10-03 12:23:45 +05301556 ptn_name = "recovery";
1557 else
1558 ptn_name = "boot";
1559
1560 index = partition_get_index(ptn_name);
1561 ptn = partition_get_offset(index);
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301562 image_size = partition_get_size(index);
1563 if(ptn == 0 || image_size == 0) {
Mayank Groverc93cad82017-10-03 12:23:45 +05301564 dprintf(CRITICAL, "ERROR: No %s partition found\n", ptn_name);
1565 return -1;
Kinson Chikf1a43512011-07-14 11:28:39 -07001566 }
Mayank Groverc93cad82017-10-03 12:23:45 +05301567
Channagoud Kadabief0547c2015-02-10 12:57:38 -08001568 /* Set Lun for boot & recovery partitions */
1569 mmc_set_lun(partition_get_lun(index));
Shashank Mittal23b8f422010-04-16 19:27:21 -07001570
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301571 if (mmc_read(ptn + offset, (uint32_t *) buf, page_size)) {
Shashank Mittal23b8f422010-04-16 19:27:21 -07001572 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
1573 return -1;
1574 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001575
1576 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001577 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Mayank Grover351a75e2017-05-30 20:06:08 +05301578 return ERR_INVALID_BOOT_MAGIC;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001579 }
1580
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07001581 if (hdr->page_size && (hdr->page_size != page_size)) {
vijay kumar2e21b3a2014-06-26 17:40:15 +05301582
1583 if (hdr->page_size > BOOT_IMG_MAX_PAGE_SIZE) {
1584 dprintf(CRITICAL, "ERROR: Invalid page size\n");
1585 return -1;
1586 }
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07001587 page_size = hdr->page_size;
1588 page_mask = page_size - 1;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001589 }
1590
Matthew Qin49e51fa2015-02-09 10:40:45 +08001591 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1592 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Kishor PKd8ddcad2017-07-27 13:53:57 +05301593 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001594
Matthew Qin49e51fa2015-02-09 10:40:45 +08001595 image_addr = (unsigned char *)target_get_scratch_address();
Mayank Grover6fd36792018-09-10 13:26:58 +05301596#if VERIFIED_BOOT_2
1597 /* Create hole in start of image for VB salt to copy */
1598 image_addr += SALT_BUFF_OFFSET;
1599#endif
Kishor PK9e91b592017-05-24 12:14:55 +05301600 memcpy(image_addr, (void *)buf, page_size);
1601
1602 /* ensure commandline is terminated */
1603 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
Matthew Qin49e51fa2015-02-09 10:40:45 +08001604
1605#if DEVICE_TREE
Parth Dixit4097b622016-03-15 14:42:27 +05301606#ifndef OSVERSION_IN_BOOTIMAGE
1607 dt_size = hdr->dt_size;
Mayank Grover5a502582018-09-12 11:24:49 +05301608#else
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301609 dprintf(INFO, "BootImage Header: %d\n", hdr->header_version);
Mayank Grover5a502582018-09-12 11:24:49 +05301610#endif
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301611
Parth Dixit4097b622016-03-15 14:42:27 +05301612 dt_actual = ROUND_TO_PAGE(dt_size, page_mask);
Kishor PKd8ddcad2017-07-27 13:53:57 +05301613 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 +05301614 dprintf(CRITICAL, "Integer overflow detected in bootimage header fields at %u in %s\n",__LINE__,__FILE__);
1615 return -1;
1616 }
Kishor PKd8ddcad2017-07-27 13:53:57 +05301617 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + second_actual + dt_actual);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001618#else
Kishor PKd8ddcad2017-07-27 13:53:57 +05301619 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 +05301620 dprintf(CRITICAL, "Integer overflow detected in bootimage header fields at %u in %s\n",__LINE__,__FILE__);
1621 return -1;
1622 }
Kishor PKd8ddcad2017-07-27 13:53:57 +05301623 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + second_actual);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001624#endif
Parth Dixit1375d9e2019-07-08 20:56:13 +05301625 dtb_image_size = hdr->kernel_size;
Matthew Qin49e51fa2015-02-09 10:40:45 +08001626
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301627#ifdef OSVERSION_IN_BOOTIMAGE
1628 /* If header version is ONE and booting into recovery,
1629 dtbo is appended with recovery image.
1630 Doing following:
1631 * Validating the recovery offset and size.
1632 * Extracting recovery dtbo to be used as dtbo.
1633 */
1634 if (boot_into_recovery &&
1635 hdr->header_version == BOOT_HEADER_VERSION_ONE)
1636 {
1637 struct boot_img_hdr_v1 *hdr1 =
1638 (struct boot_img_hdr_v1 *) (image_addr + sizeof(boot_img_hdr));
Mayank Grovera1a83c72018-09-24 11:23:15 +05301639 unsigned int recovery_dtbo_actual = 0;
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301640
Mayank Grovera1a83c72018-09-24 11:23:15 +05301641 recovery_dtbo_actual = ROUND_TO_PAGE(hdr1->recovery_dtbo_size, page_mask);
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301642 if ((hdr1->header_size !=
1643 sizeof(struct boot_img_hdr_v1) + sizeof(boot_img_hdr)))
1644 {
1645 dprintf(CRITICAL, "Invalid boot image header: %d\n", hdr1->header_size);
1646 return -1;
1647 }
1648
Mayank Grovera1a83c72018-09-24 11:23:15 +05301649 if (recovery_dtbo_actual > MAX_SUPPORTED_DTBO_IMG_BUF)
1650 {
1651 dprintf(CRITICAL, "Recovery Dtbo Size too big %x, Allowed size %x\n", recovery_dtbo_actual,
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301652 MAX_SUPPORTED_DTBO_IMG_BUF);
1653 return -1;
1654 }
1655
Mayank Grovera1a83c72018-09-24 11:23:15 +05301656 if (UINT_MAX < ((uint64_t)imagesize_actual + recovery_dtbo_actual))
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301657 {
1658 dprintf(CRITICAL, "Integer overflow detected in recoveryimage header fields at %u in %s\n",__LINE__,__FILE__);
1659 return -1;
1660 }
1661
Mayank Grovera1a83c72018-09-24 11:23:15 +05301662 if (UINT_MAX < (hdr1->recovery_dtbo_offset + recovery_dtbo_actual)) {
1663 dprintf(CRITICAL,
1664 "Integer overflow detected in recovery image header fields at %u in %s\n",__LINE__,__FILE__);
1665 return -1;
1666 }
1667
1668 if (hdr1->recovery_dtbo_offset + recovery_dtbo_actual > image_size)
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301669 {
1670 dprintf(CRITICAL, "Invalid recovery dtbo: Recovery Dtbo Offset=0x%llx,"
1671 " Recovery Dtbo Size=0x%x, Image Size=0x%llx\n",
1672 hdr1->recovery_dtbo_offset, recovery_dtbo_size, image_size);
1673 return -1;
1674 }
1675
1676 recovery_dtbo_buf = (void *)(hdr1->recovery_dtbo_offset + image_addr);
Mayank Grovera1a83c72018-09-24 11:23:15 +05301677 recovery_dtbo_size = recovery_dtbo_actual;
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301678 imagesize_actual += recovery_dtbo_size;
1679
1680 dprintf(SPEW, "Header version: %d\n", hdr->header_version);
1681 dprintf(SPEW, "Recovery Dtbo Size 0x%x\n", recovery_dtbo_size);
1682 dprintf(SPEW, "Recovery Dtbo Offset 0x%llx\n", hdr1->recovery_dtbo_offset);
1683
1684 }
Parth Dixit1375d9e2019-07-08 20:56:13 +05301685
1686 if ( hdr->header_version == BOOT_HEADER_VERSION_TWO) {
1687 struct boot_img_hdr_v1 *hdr1 =
1688 (struct boot_img_hdr_v1 *) (image_addr + sizeof(boot_img_hdr));
1689 struct boot_img_hdr_v2 *hdr2 = (struct boot_img_hdr_v2 *)
1690 (image_addr + sizeof(boot_img_hdr) +
1691 BOOT_IMAGE_HEADER_V2_OFFSET);
1692 unsigned int recovery_dtbo_actual = 0;
1693
lijuang0b17ba22019-09-04 14:20:47 +08001694 recovery_dtbo_actual =
1695 ROUND_TO_PAGE(hdr1->recovery_dtbo_size, page_mask);
1696 imagesize_actual += recovery_dtbo_actual;
Parth Dixit1375d9e2019-07-08 20:56:13 +05301697
1698 imagesize_actual += ROUND_TO_PAGE(hdr2->dtb_size, page_mask);
1699
1700
1701 dtb_image_offset = page_size + patched_kernel_hdr_size +
1702 kernel_actual + ramdisk_actual + second_actual +
1703 recovery_dtbo_actual;
1704
1705 dprintf(SPEW, "Header version: %d\n", hdr->header_version);
1706 dprintf(SPEW, "Dtb image offset 0x%x\n", dtb_image_offset);
1707 }
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301708#endif
1709
Parth Dixit1375d9e2019-07-08 20:56:13 +05301710
Saranya Chidurab1aaf232018-11-19 15:47:50 +05301711 /* Validate the boot/recovery image size is within the bounds of partition size */
1712 if (imagesize_actual > image_size) {
1713 dprintf(CRITICAL, "Image size is greater than partition size.\n");
1714 return -1;
1715 }
1716
Matthew Qin49e51fa2015-02-09 10:40:45 +08001717#if VERIFIED_BOOT
1718 boot_verifier_init();
1719#endif
1720
Mayank Grover1ceaee22019-04-01 17:54:32 +05301721#if VERIFIED_BOOT_2
1722 /* read full partition if device is unlocked */
1723 if (device.is_unlocked)
1724 imagesize_actual = image_size;
1725#endif
1726
Vijay Kumar Pendoti9c002ad2016-03-09 13:52:45 +05301727 if (check_aboot_addr_range_overlap((uintptr_t) image_addr, imagesize_actual))
Matthew Qin49e51fa2015-02-09 10:40:45 +08001728 {
1729 dprintf(CRITICAL, "Boot image buffer address overlaps with aboot addresses.\n");
1730 return -1;
1731 }
1732
Matthew Qinbb7923d2015-02-09 10:56:09 +08001733 /*
1734 * Update loading flow of bootimage to support compressed/uncompressed
1735 * bootimage on both 64bit and 32bit platform.
1736 * 1. Load bootimage from emmc partition onto DDR.
1737 * 2. Check if bootimage is gzip format. If yes, decompress compressed kernel
1738 * 3. Check kernel header and update kernel load addr for 64bit and 32bit
1739 * platform accordingly.
1740 * 4. Sanity Check on kernel_addr and ramdisk_addr and copy data.
1741 */
Mayank Grover351a75e2017-05-30 20:06:08 +05301742 if (partition_multislot_is_supported())
1743 {
1744 current_active_slot = partition_find_active_slot();
1745 dprintf(INFO, "Loading boot image (%d) active_slot(%s): start\n",
1746 imagesize_actual, SUFFIX_SLOT(current_active_slot));
1747 }
1748 else
1749 {
1750 dprintf(INFO, "Loading (%s) image (%d): start\n",
1751 (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual);
1752 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08001753 bs_set_timestamp(BS_KERNEL_LOAD_START);
1754
Gaurav Nebhwani43e2a462016-03-17 21:32:56 +05301755 if ((target_get_max_flash_size() - page_size) < imagesize_actual)
1756 {
1757 dprintf(CRITICAL, "booimage size is greater than DDR can hold\n");
1758 return -1;
1759 }
Kishor PK9e91b592017-05-24 12:14:55 +05301760 offset = page_size;
1761 /* Read image without signature and header*/
1762 if (mmc_read(ptn + offset, (void *)(image_addr + offset), imagesize_actual - page_size))
Matthew Qin49e51fa2015-02-09 10:40:45 +08001763 {
1764 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
1765 return -1;
1766 }
1767
Mayank Grover351a75e2017-05-30 20:06:08 +05301768 if (partition_multislot_is_supported())
1769 {
1770 dprintf(INFO, "Loading boot image (%d) active_slot(%s): done\n",
1771 imagesize_actual, SUFFIX_SLOT(current_active_slot));
1772 }
1773 else
1774 {
1775 dprintf(INFO, "Loading (%s) image (%d): done\n",
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001776 (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual);
1777
Mayank Grover351a75e2017-05-30 20:06:08 +05301778 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08001779 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
1780
1781 /* Authenticate Kernel */
1782 dprintf(INFO, "use_signed_kernel=%d, is_unlocked=%d, is_tampered=%d.\n",
1783 (int) target_use_signed_kernel(),
1784 device.is_unlocked,
1785 device.is_tampered);
Monika Singh292b3e92018-03-17 22:40:23 +05301786#if VERIFIED_BOOT_2
Mayank Grover1ceaee22019-04-01 17:54:32 +05301787 /* if device is unlocked skip reading signature, as full partition is read */
1788 if (!device.is_unlocked)
Monika Singh292b3e92018-03-17 22:40:23 +05301789 {
Mayank Grover1ceaee22019-04-01 17:54:32 +05301790 offset = imagesize_actual;
1791 if (check_aboot_addr_range_overlap((uintptr_t)image_addr + offset, page_size))
1792 {
1793 dprintf(CRITICAL, "Signature read buffer address overlaps with aboot addresses.\n");
1794 return -1;
1795 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08001796
Mayank Grover1ceaee22019-04-01 17:54:32 +05301797 /* Read signature */
1798 if(mmc_read(ptn + offset, (void *)(image_addr + offset), page_size))
1799 {
1800 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
1801 return -1;
1802 }
Monika Singh292b3e92018-03-17 22:40:23 +05301803 }
1804
Mayank Grover027a9412018-09-04 15:12:05 +05301805 /* load and validate dtbo partition */
1806 load_validate_dtbo_image(&dtbo_image_buf, &dtbo_image_sz);
1807
Mayank Grover9df84c02018-08-30 15:46:35 +05301808 /* load vbmeta partition */
1809 load_vbmeta_image(&vbmeta_image_buf, &vbmeta_image_sz);
1810
Monika Singh292b3e92018-03-17 22:40:23 +05301811 memset(&info, 0, sizeof(bootinfo));
Mayank Grover027a9412018-09-04 15:12:05 +05301812
1813 /* Pass loaded boot image passed */
Mayank Grover6fd36792018-09-10 13:26:58 +05301814 info.images[IMG_BOOT].image_buffer = SUB_SALT_BUFF_OFFSET(image_addr);
Mayank Grover027a9412018-09-04 15:12:05 +05301815 info.images[IMG_BOOT].imgsize = imagesize_actual;
1816 info.images[IMG_BOOT].name = ptn_name;
1817 ++info.num_loaded_images;
1818
1819 /* Pass loaded dtbo image */
1820 if (dtbo_image_buf != NULL) {
Mayank Grover6fd36792018-09-10 13:26:58 +05301821 info.images[IMG_DTBO].image_buffer =
1822 SUB_SALT_BUFF_OFFSET(dtbo_image_buf);
Mayank Grover027a9412018-09-04 15:12:05 +05301823 info.images[IMG_DTBO].imgsize = dtbo_image_sz;
1824 info.images[IMG_DTBO].name = "dtbo";
1825 ++info.num_loaded_images;
1826 }
1827
Mayank Grover9df84c02018-08-30 15:46:35 +05301828 /* Pass loaded vbmeta image */
1829 if (vbmeta_image_buf != NULL) {
1830 info.images[IMG_VBMETA].image_buffer = vbmeta_image_buf;
1831 info.images[IMG_VBMETA].imgsize = vbmeta_image_sz;
1832 info.images[IMG_VBMETA].name = "vbmeta";
1833 ++info.num_loaded_images;
1834 }
1835
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301836 info.header_version = hdr->header_version;
Monika Singh292b3e92018-03-17 22:40:23 +05301837 info.multi_slot_boot = partition_multislot_is_supported();
1838 info.bootreason_alarm = boot_reason_alarm;
1839 info.bootinto_recovery = boot_into_recovery;
1840 status = load_image_and_auth(&info);
1841 if(status)
1842 return -1;
1843
1844 vbcmdline = info.vbcmdline;
Mayank Grover9df84c02018-08-30 15:46:35 +05301845
1846 /* Free the buffer allocated to vbmeta post verification */
Saranya Chidurab4933332018-10-15 17:30:06 +05301847 if (vbmeta_image_buf != NULL) {
1848 free(vbmeta_image_buf);
1849 --info.num_loaded_images;
1850 }
Monika Singh292b3e92018-03-17 22:40:23 +05301851#else
Channagoud Kadabi736c4962015-08-21 11:56:52 -07001852 /* Change the condition a little bit to include the test framework support.
1853 * We would never reach this point if device is in fastboot mode, even if we did
1854 * that means we are in test mode, so execute kernel authentication part for the
1855 * tests */
Parth Dixitcb0c6082015-12-30 15:01:13 +05301856 if((target_use_signed_kernel() && (!device.is_unlocked)) || is_test_mode_enabled())
Matthew Qin49e51fa2015-02-09 10:40:45 +08001857 {
1858 offset = imagesize_actual;
Vijay Kumar Pendoti9c002ad2016-03-09 13:52:45 +05301859 if (check_aboot_addr_range_overlap((uintptr_t)image_addr + offset, page_size))
Matthew Qin49e51fa2015-02-09 10:40:45 +08001860 {
1861 dprintf(CRITICAL, "Signature read buffer address overlaps with aboot addresses.\n");
1862 return -1;
1863 }
1864
1865 /* Read signature */
1866 if(mmc_read(ptn + offset, (void *)(image_addr + offset), page_size))
1867 {
1868 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
1869 return -1;
1870 }
1871
1872 verify_signed_bootimg((uint32_t)image_addr, imagesize_actual);
Channagoud Kadabi736c4962015-08-21 11:56:52 -07001873 /* The purpose of our test is done here */
Parth Dixitcb0c6082015-12-30 15:01:13 +05301874 if(is_test_mode_enabled() && auth_kernel_img)
Channagoud Kadabi736c4962015-08-21 11:56:52 -07001875 return 0;
Matthew Qin49e51fa2015-02-09 10:40:45 +08001876 } else {
1877 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
1878 #ifdef TZ_SAVE_KERNEL_HASH
1879 aboot_save_boot_hash_mmc((uint32_t) image_addr, imagesize_actual);
1880 #endif /* TZ_SAVE_KERNEL_HASH */
Amit Blay4aa292f2015-04-28 21:55:59 +03001881
1882#ifdef MDTP_SUPPORT
1883 {
1884 /* Verify MDTP lock.
1885 * For boot & recovery partitions, MDTP will use boot_verifier APIs,
1886 * since verification was skipped in aboot. The signature is not part of the loaded image.
1887 */
1888 mdtp_ext_partition_verification_t ext_partition;
1889 ext_partition.partition = boot_into_recovery ? MDTP_PARTITION_RECOVERY : MDTP_PARTITION_BOOT;
1890 ext_partition.integrity_state = MDTP_PARTITION_STATE_UNSET;
1891 ext_partition.page_size = page_size;
1892 ext_partition.image_addr = (uint32)image_addr;
1893 ext_partition.image_size = imagesize_actual;
1894 ext_partition.sig_avail = FALSE;
1895 mdtp_fwlock_verify_lock(&ext_partition);
1896 }
1897#endif /* MDTP_SUPPORT */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001898 }
Monika Singh292b3e92018-03-17 22:40:23 +05301899#endif
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001900
Sridhar Parasuram00bfedb2015-05-26 14:21:27 -07001901#if VERIFIED_BOOT
taozhang93088bd2016-11-16 15:50:46 +08001902 if((boot_verify_get_state() == ORANGE) && (!boot_into_ffbm))
Reut Zysmanba8a9d52016-02-18 11:44:04 +02001903 {
1904#if FBCON_DISPLAY_MSG
1905 display_bootverify_menu(DISPLAY_MENU_ORANGE);
1906 wait_for_users_action();
1907#else
1908 dprintf(CRITICAL,
1909 "Your device has been unlocked and can't be trusted.\nWait for 5 seconds before proceeding\n");
1910 mdelay(5000);
1911#endif
1912 }
1913#endif
1914
1915#if VERIFIED_BOOT
Monika Singh292b3e92018-03-17 22:40:23 +05301916 if (VB_M == target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +05301917 {
1918 /* set boot and system versions. */
1919 set_os_version((unsigned char *)image_addr);
1920 // send root of trust
1921 if(!send_rot_command((uint32_t)device.is_unlocked))
1922 ASSERT(0);
1923 }
Parth Dixitbc9b6492015-10-18 00:41:38 +05301924#endif
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001925 /*
Matthew Qinbb7923d2015-02-09 10:56:09 +08001926 * Check if the kernel image is a gzip package. If yes, need to decompress it.
1927 * If not, continue booting.
1928 */
1929 if (is_gzip_package((unsigned char *)(image_addr + page_size), hdr->kernel_size))
1930 {
1931 out_addr = (unsigned char *)(image_addr + imagesize_actual + page_size);
1932 out_avai_len = target_get_max_flash_size() - imagesize_actual - page_size;
Mayank Grover027a9412018-09-04 15:12:05 +05301933#if VERIFIED_BOOT_2
1934 if (dtbo_image_sz)
1935 out_avai_len -= DTBO_IMG_BUF;
1936#endif
Matthew Qin0b15b322015-05-19 05:20:54 -04001937 dprintf(INFO, "decompressing kernel image: start\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001938 rc = decompress((unsigned char *)(image_addr + page_size),
1939 hdr->kernel_size, out_addr, out_avai_len,
1940 &dtb_offset, &out_len);
1941 if (rc)
1942 {
Matthew Qin0b15b322015-05-19 05:20:54 -04001943 dprintf(CRITICAL, "decompressing kernel image failed!!!\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001944 ASSERT(0);
1945 }
1946
Matthew Qin0b15b322015-05-19 05:20:54 -04001947 dprintf(INFO, "decompressing kernel image: done\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001948 kptr = (struct kernel64_hdr *)out_addr;
1949 kernel_start_addr = out_addr;
1950 kernel_size = out_len;
1951 } else {
Ameya Thakur10a33452016-06-13 14:24:26 -07001952 dprintf(INFO, "Uncpmpressed kernel in use\n");
1953 if (!strncmp((char*)(image_addr + page_size),
1954 PATCHED_KERNEL_MAGIC,
1955 sizeof(PATCHED_KERNEL_MAGIC) - 1)) {
1956 dprintf(INFO, "Patched kernel detected\n");
1957 kptr = (struct kernel64_hdr *)(image_addr + page_size +
1958 PATCHED_KERNEL_HEADER_SIZE);
1959 //The size of the kernel is stored at start of kernel image + 16
1960 //The dtb would start just after the kernel
1961 dtb_offset = *((uint32_t*)((unsigned char*)
1962 (image_addr + page_size +
1963 sizeof(PATCHED_KERNEL_MAGIC) -
1964 1)));
1965 //The actual kernel starts after the 20 byte header.
1966 kernel_start_addr = (unsigned char*)(image_addr +
1967 page_size + PATCHED_KERNEL_HEADER_SIZE);
1968 kernel_size = hdr->kernel_size;
1969 patched_kernel_hdr_size = PATCHED_KERNEL_HEADER_SIZE;
1970 } else {
1971 dprintf(INFO, "Kernel image not patched..Unable to locate dt offset\n");
1972 kptr = (struct kernel64_hdr *)(image_addr + page_size);
1973 kernel_start_addr = (unsigned char *)(image_addr + page_size);
1974 kernel_size = hdr->kernel_size;
1975 }
Matthew Qinbb7923d2015-02-09 10:56:09 +08001976 }
1977
1978 /*
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001979 * Update the kernel/ramdisk/tags address if the boot image header
1980 * has default values, these default values come from mkbootimg when
1981 * the boot image is flashed using fastboot flash:raw
1982 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001983 update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr));
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001984
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001985 /* Get virtual addresses since the hdr saves physical addresses. */
1986 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
1987 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
1988 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001989
Matthew Qinbb7923d2015-02-09 10:56:09 +08001990 kernel_size = ROUND_TO_PAGE(kernel_size, page_mask);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001991 /* Check if the addresses in the header are valid. */
Matthew Qinbb7923d2015-02-09 10:56:09 +08001992 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_size) ||
Mayank Grovere559cec2017-10-17 15:12:03 +05301993 check_ddr_addr_range_bound(hdr->kernel_addr, kernel_size) ||
1994 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual) ||
1995 check_ddr_addr_range_bound(hdr->ramdisk_addr, ramdisk_actual))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001996 {
Mayank Grovere559cec2017-10-17 15:12:03 +05301997 dprintf(CRITICAL, "kernel/ramdisk addresses are not valid.\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001998 return -1;
1999 }
2000
2001#ifndef DEVICE_TREE
Mayank Grovere559cec2017-10-17 15:12:03 +05302002 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE) ||
2003 check_ddr_addr_range_bound(hdr->tags_addr, MAX_TAGS_SIZE))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002004 {
Mayank Grovere559cec2017-10-17 15:12:03 +05302005 dprintf(CRITICAL, "Tags addresses are not valid.\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002006 return -1;
2007 }
2008#endif
2009
Matthew Qin49e51fa2015-02-09 10:40:45 +08002010 /* Move kernel, ramdisk and device tree to correct address */
Matthew Qinbb7923d2015-02-09 10:56:09 +08002011 memmove((void*) hdr->kernel_addr, kernel_start_addr, kernel_size);
Matthew Qin49e51fa2015-02-09 10:40:45 +08002012 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002013
Matthew Qin49e51fa2015-02-09 10:40:45 +08002014 #if DEVICE_TREE
Parth Dixit4097b622016-03-15 14:42:27 +05302015 if(dt_size) {
Matthew Qin49e51fa2015-02-09 10:40:45 +08002016 dt_table_offset = ((uint32_t)image_addr + page_size + kernel_actual + ramdisk_actual + second_actual);
2017 table = (struct dt_table*) dt_table_offset;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002018
Matthew Qin49e51fa2015-02-09 10:40:45 +08002019 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
2020 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
2021 return -1;
2022 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002023
P.V. Phani Kumar82916762016-03-09 09:20:19 +05302024 /* Its Error if, dt_hdr_size (table->num_entries * dt_entry size + Dev_Tree Header)
2025 goes beyound hdr->dt_size*/
Parth Dixit4097b622016-03-15 14:42:27 +05302026 if (dt_hdr_size > ROUND_TO_PAGE(dt_size,hdr->page_size)) {
P.V. Phani Kumar82916762016-03-09 09:20:19 +05302027 dprintf(CRITICAL, "ERROR: Invalid Device Tree size \n");
2028 return -1;
2029 }
2030
Matthew Qin49e51fa2015-02-09 10:40:45 +08002031 /* Find index of device tree within device tree table */
2032 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
2033 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
2034 return -1;
2035 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002036
P.V. Phani Kumar3c333202016-03-09 11:54:37 +05302037 if(dt_entry.offset > (UINT_MAX - dt_entry.size)) {
2038 dprintf(CRITICAL, "ERROR: Device tree contents are Invalid\n");
2039 return -1;
2040 }
2041
2042 /* Ensure we are not overshooting dt_size with the dt_entry selected */
Parth Dixit4097b622016-03-15 14:42:27 +05302043 if ((dt_entry.offset + dt_entry.size) > dt_size) {
P.V. Phani Kumar3c333202016-03-09 11:54:37 +05302044 dprintf(CRITICAL, "ERROR: Device tree contents are Invalid\n");
2045 return -1;
2046 }
2047
Matthew Qin271927e2015-03-31 22:07:07 -04002048 if (is_gzip_package((unsigned char *)dt_table_offset + dt_entry.offset, dt_entry.size))
2049 {
2050 unsigned int compressed_size = 0;
2051 out_addr += out_len;
2052 out_avai_len -= out_len;
Matthew Qin0b15b322015-05-19 05:20:54 -04002053 dprintf(INFO, "decompressing dtb: start\n");
Matthew Qin271927e2015-03-31 22:07:07 -04002054 rc = decompress((unsigned char *)dt_table_offset + dt_entry.offset,
2055 dt_entry.size, out_addr, out_avai_len,
2056 &compressed_size, &dtb_size);
2057 if (rc)
2058 {
Matthew Qin0b15b322015-05-19 05:20:54 -04002059 dprintf(CRITICAL, "decompressing dtb failed!!!\n");
Matthew Qin271927e2015-03-31 22:07:07 -04002060 ASSERT(0);
2061 }
2062
Matthew Qin0b15b322015-05-19 05:20:54 -04002063 dprintf(INFO, "decompressing dtb: done\n");
Matthew Qin271927e2015-03-31 22:07:07 -04002064 best_match_dt_addr = out_addr;
2065 } else {
2066 best_match_dt_addr = (unsigned char *)dt_table_offset + dt_entry.offset;
2067 dtb_size = dt_entry.size;
2068 }
2069
Matthew Qin49e51fa2015-02-09 10:40:45 +08002070 /* Validate and Read device device tree in the tags_addr */
Mayank Grovere559cec2017-10-17 15:12:03 +05302071 if (check_aboot_addr_range_overlap(hdr->tags_addr, dtb_size) ||
2072 check_ddr_addr_range_bound(hdr->tags_addr, dtb_size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002073 {
Mayank Grovere559cec2017-10-17 15:12:03 +05302074 dprintf(CRITICAL, "Device tree addresses are not valid\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002075 return -1;
2076 }
Shashank Mittal162244e2011-08-08 19:01:25 -07002077
Matthew Qin271927e2015-03-31 22:07:07 -04002078 memmove((void *)hdr->tags_addr, (char *)best_match_dt_addr, dtb_size);
Matthew Qin49e51fa2015-02-09 10:40:45 +08002079 } else {
2080 /* Validate the tags_addr */
Mayank Grovere559cec2017-10-17 15:12:03 +05302081 if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual) ||
2082 check_ddr_addr_range_bound(hdr->tags_addr, kernel_actual))
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07002083 {
Mayank Grovere559cec2017-10-17 15:12:03 +05302084 dprintf(CRITICAL, "Device tree addresses are not valid.\n");
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07002085 return -1;
2086 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08002087 /*
2088 * If appended dev tree is found, update the atags with
2089 * memory address to the DTB appended location on RAM.
2090 * Else update with the atags address in the kernel header
Mayank Grover1ed3e8b2018-09-19 15:16:50 +05302091 *
2092 * Make sure everything from scratch address is read before next step!
2093 * In case of dtbo, this API is going to read dtbo on scratch.
Matthew Qin49e51fa2015-02-09 10:40:45 +08002094 */
2095 void *dtb;
Parth Dixit1375d9e2019-07-08 20:56:13 +05302096 image_buf = (void*)(image_addr + page_size + patched_kernel_hdr_size);
2097
Nagireddy Annem0d60bb62019-08-16 19:16:00 +05302098#ifdef OSVERSION_IN_BOOTIMAGE
Parth Dixit1375d9e2019-07-08 20:56:13 +05302099 if ( hdr->header_version == BOOT_HEADER_VERSION_TWO) {
2100
2101 image_buf = (void*)(image_addr);
2102 dtb_offset = dtb_image_offset;
2103 dtb_image_size = imagesize_actual;
2104 }
Nagireddy Annem0d60bb62019-08-16 19:16:00 +05302105#endif
Parth Dixit1375d9e2019-07-08 20:56:13 +05302106
2107 dtb = dev_tree_appended(image_buf, dtb_image_size, dtb_offset,
Ameya Thakur10a33452016-06-13 14:24:26 -07002108 (void *)hdr->tags_addr);
Matthew Qin49e51fa2015-02-09 10:40:45 +08002109 if (!dtb) {
2110 dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07002111 return -1;
2112 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07002113 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08002114 #endif
Shashank Mittal23b8f422010-04-16 19:27:21 -07002115
Stanimir Varbanov69ec5462013-07-18 18:17:42 +03002116 if (boot_into_recovery && !device.is_unlocked && !device.is_tampered)
2117 target_load_ssd_keystore();
2118
Shashank Mittal23b8f422010-04-16 19:27:21 -07002119unified_boot:
Shashank Mittal23b8f422010-04-16 19:27:21 -07002120
Dima Zavin77e41f32013-03-06 16:10:43 -08002121 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07002122 (const char *)hdr->cmdline, board_machtype(),
Shashank Mittal23b8f422010-04-16 19:27:21 -07002123 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
2124
2125 return 0;
2126}
2127
Dima Zavin214cc642009-01-26 11:16:21 -08002128int boot_linux_from_flash(void)
2129{
Mayank Grovere1ab96c2018-09-04 20:31:31 +05302130 boot_img_hdr *hdr = (void*) buf;
Dima Zavin214cc642009-01-26 11:16:21 -08002131 struct ptentry *ptn;
2132 struct ptable *ptable;
2133 unsigned offset = 0;
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08002134
Shashank Mittalcd98d472011-08-02 14:29:24 -07002135 unsigned char *image_addr = 0;
2136 unsigned kernel_actual;
2137 unsigned ramdisk_actual;
2138 unsigned imagesize_actual;
vijay kumar8f53e362015-11-24 13:38:11 +05302139 unsigned second_actual = 0;
Shashank Mittalcd98d472011-08-02 14:29:24 -07002140
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07002141#if DEVICE_TREE
Monika Singh292b3e92018-03-17 22:40:23 +05302142 struct dt_table *table = NULL;
Joel Kingaa335dc2013-06-03 16:11:08 -07002143 struct dt_entry dt_entry;
vijay kumar8f53e362015-11-24 13:38:11 +05302144 unsigned dt_table_offset;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08002145 uint32_t dt_actual;
Monika Singh292b3e92018-03-17 22:40:23 +05302146 uint32_t dt_hdr_size = 0;
Vivek Kumar07bd0862018-04-10 14:19:23 +05302147 uint32_t dtb_offset = 0;
vijay kumar8f53e362015-11-24 13:38:11 +05302148 unsigned int dtb_size = 0;
2149 unsigned char *best_match_dt_addr = NULL;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07002150#endif
2151
David Ng183a7422009-12-07 14:55:21 -08002152 if (target_is_emmc_boot()) {
Mayank Grovere1ab96c2018-09-04 20:31:31 +05302153 hdr = (boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
David Ng183a7422009-12-07 14:55:21 -08002154 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
2155 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
2156 return -1;
2157 }
2158 goto continue_boot;
2159 }
2160
Dima Zavin214cc642009-01-26 11:16:21 -08002161 ptable = flash_get_ptable();
2162 if (ptable == NULL) {
2163 dprintf(CRITICAL, "ERROR: Partition table not found\n");
2164 return -1;
2165 }
2166
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08002167 if(!boot_into_recovery)
2168 {
2169 ptn = ptable_find(ptable, "boot");
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07002170
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08002171 if (ptn == NULL) {
2172 dprintf(CRITICAL, "ERROR: No boot partition found\n");
2173 return -1;
2174 }
2175 }
2176 else
2177 {
2178 ptn = ptable_find(ptable, "recovery");
2179 if (ptn == NULL) {
2180 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
2181 return -1;
2182 }
Dima Zavin214cc642009-01-26 11:16:21 -08002183 }
2184
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05302185 /* Read boot.img header from flash */
Shashank Mittaldcc2e352009-11-19 19:11:16 -08002186 if (flash_read(ptn, offset, buf, page_size)) {
Dima Zavin214cc642009-01-26 11:16:21 -08002187 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
2188 return -1;
2189 }
Dima Zavin214cc642009-01-26 11:16:21 -08002190
2191 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07002192 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Dima Zavin214cc642009-01-26 11:16:21 -08002193 return -1;
2194 }
2195
Shashank Mittaldcc2e352009-11-19 19:11:16 -08002196 if (hdr->page_size != page_size) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07002197 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 -08002198 return -1;
2199 }
2200
Kishor PK9e91b592017-05-24 12:14:55 +05302201 image_addr = (unsigned char *)target_get_scratch_address();
2202 memcpy(image_addr, (void *)buf, page_size);
vijay kumar287bb542015-09-29 13:01:52 +05302203
Channagoud Kadabia22144f2013-03-20 11:49:01 -07002204 /*
2205 * Update the kernel/ramdisk/tags address if the boot image header
2206 * has default values, these default values come from mkbootimg when
2207 * the boot image is flashed using fastboot flash:raw
2208 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08002209 update_ker_tags_rdisk_addr(hdr, false);
Channagoud Kadabia22144f2013-03-20 11:49:01 -07002210
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002211 /* Get virtual addresses since the hdr saves physical addresses. */
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002212 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
2213 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
2214 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
2215
2216 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
2217 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Mayank Grover032736f2017-07-14 20:34:51 +05302218 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002219
Kishor PK9e91b592017-05-24 12:14:55 +05302220 /* ensure commandline is terminated */
2221 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
2222
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002223 /* Check if the addresses in the header are valid. */
2224 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
Mayank Grovere559cec2017-10-17 15:12:03 +05302225 check_ddr_addr_range_bound(hdr->kernel_addr, kernel_actual) ||
2226 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual) ||
2227 check_ddr_addr_range_bound(hdr->ramdisk_addr, ramdisk_actual))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002228 {
Mayank Grovere559cec2017-10-17 15:12:03 +05302229 dprintf(CRITICAL, "kernel/ramdisk addresses are not valid.\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002230 return -1;
2231 }
2232
2233#ifndef DEVICE_TREE
Kishor PKd8ddcad2017-07-27 13:53:57 +05302234 if (UINT_MAX < ((uint64_t)kernel_actual + (uint64_t)ramdisk_actual+ (uint64_t)second_actual + page_size)) {
Mayank Grover032736f2017-07-14 20:34:51 +05302235 dprintf(CRITICAL, "Integer overflow detected in bootimage header fields\n");
2236 return -1;
2237 }
Kishor PKd8ddcad2017-07-27 13:53:57 +05302238 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + second_actual);
Mayank Grover032736f2017-07-14 20:34:51 +05302239
Mayank Grovere559cec2017-10-17 15:12:03 +05302240 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE) ||
2241 check_ddr_addr_range_bound(hdr->tags_addr, MAX_TAGS_SIZE))
Vijay Kumar Pendotid3ed20e2016-09-20 00:34:46 +05302242 {
Mayank Grovere559cec2017-10-17 15:12:03 +05302243 dprintf(CRITICAL, "Tags addresses are not valid.\n");
Vijay Kumar Pendotid3ed20e2016-09-20 00:34:46 +05302244 return -1;
2245 }
2246#else
2247
2248#ifndef OSVERSION_IN_BOOTIMAGE
2249 dt_size = hdr->dt_size;
2250#endif
Mayank Grover032736f2017-07-14 20:34:51 +05302251 dt_actual = ROUND_TO_PAGE(dt_size, page_mask);
Kishor PKd8ddcad2017-07-27 13:53:57 +05302252 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 +05302253 dprintf(CRITICAL, "Integer overflow detected in bootimage header fields\n");
2254 return -1;
2255 }
2256
Kishor PKd8ddcad2017-07-27 13:53:57 +05302257 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + second_actual + dt_actual);
Mayank Grover032736f2017-07-14 20:34:51 +05302258
Mayank Grovere559cec2017-10-17 15:12:03 +05302259 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_size) ||
2260 check_ddr_addr_range_bound(hdr->tags_addr, dt_size))
Mayank Grover032736f2017-07-14 20:34:51 +05302261 {
Mayank Grovere559cec2017-10-17 15:12:03 +05302262 dprintf(CRITICAL, "Device tree addresses are not valid.\n");
Mayank Grover032736f2017-07-14 20:34:51 +05302263 return -1;
2264 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002265#endif
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002266
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05302267 /* Read full boot.img from flash */
2268 dprintf(INFO, "Loading (%s) image (%d): start\n",
2269 (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual);
2270 bs_set_timestamp(BS_KERNEL_LOAD_START);
2271
2272 if (UINT_MAX - page_size < imagesize_actual)
2273 {
2274 dprintf(CRITICAL,"Integer overflow detected in bootimage header fields %u %s\n", __LINE__,__func__);
2275 return -1;
2276 }
2277
2278 /*Check the availability of RAM before reading boot image + max signature length from flash*/
2279 if (target_get_max_flash_size() < (imagesize_actual + page_size))
2280 {
2281 dprintf(CRITICAL, "bootimage size is greater than DDR can hold\n");
2282 return -1;
2283 }
2284
2285 offset = page_size;
2286 /* Read image without signature and header */
2287 if (flash_read(ptn, offset, (void *)(image_addr + offset), imagesize_actual - page_size))
2288 {
2289 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
2290 return -1;
2291 }
2292
2293 dprintf(INFO, "Loading (%s) image (%d): done\n",
2294 (!boot_into_recovery ? "boot" : "recovery"), imagesize_actual);
2295 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
2296
Shashank Mittalcd98d472011-08-02 14:29:24 -07002297 /* Authenticate Kernel */
Deepa Dinamani23b60d42013-06-24 18:10:52 -07002298 if(target_use_signed_kernel() && (!device.is_unlocked))
Shashank Mittalcd98d472011-08-02 14:29:24 -07002299 {
Shashank Mittalcd98d472011-08-02 14:29:24 -07002300 offset = imagesize_actual;
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05302301
Shashank Mittalcd98d472011-08-02 14:29:24 -07002302 /* Read signature */
2303 if (flash_read(ptn, offset, (void *)(image_addr + offset), page_size))
2304 {
2305 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002306 return -1;
Shashank Mittalcd98d472011-08-02 14:29:24 -07002307 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07002308
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05302309 verify_signed_bootimg((uint32_t)image_addr, imagesize_actual);
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05302310 }
2311 offset = page_size;
2312 if(hdr->second_size != 0) {
2313 if (UINT_MAX - offset < second_actual)
2314 {
2315 dprintf(CRITICAL, "ERROR: Integer overflow in boot image header %s\t%d\n",__func__,__LINE__);
2316 return -1;
vijay kumar8f53e362015-11-24 13:38:11 +05302317 }
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05302318 offset += second_actual;
2319 /* Second image loading not implemented. */
2320 ASSERT(0);
2321 }
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05302322 /* Move kernel and ramdisk to correct address */
2323 memmove((void*) hdr->kernel_addr, (char*) (image_addr + page_size), hdr->kernel_size);
2324 memmove((void*) hdr->ramdisk_addr, (char*) (image_addr + page_size + kernel_actual), hdr->ramdisk_size);
2325
2326#if DEVICE_TREE
2327 if(dt_size != 0) {
2328
2329 dt_table_offset = ((uint32_t)image_addr + page_size + kernel_actual + ramdisk_actual + second_actual);
2330
2331 table = (struct dt_table*) dt_table_offset;
2332
2333 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
2334 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
2335 return -1;
2336 }
2337
2338 /* Its Error if, dt_hdr_size (table->num_entries * dt_entry size + Dev_Tree Header)
2339 goes beyound hdr->dt_size*/
2340 if (dt_hdr_size > ROUND_TO_PAGE(dt_size,hdr->page_size)) {
2341 dprintf(CRITICAL, "ERROR: Invalid Device Tree size \n");
2342 return -1;
2343 }
2344
2345 /* Find index of device tree within device tree table */
2346 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
2347 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
2348 return -1;
2349 }
2350
2351 /* Validate and Read device device tree in the "tags_add */
2352 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size) ||
2353 check_ddr_addr_range_bound(hdr->tags_addr, dt_entry.size))
2354 {
2355 dprintf(CRITICAL, "Device tree addresses are not valid.\n");
2356 return -1;
2357 }
2358
2359 if(dt_entry.offset > (UINT_MAX - dt_entry.size)) {
2360 dprintf(CRITICAL, "ERROR: Device tree contents are Invalid\n");
2361 return -1;
2362 }
2363
2364 /* Ensure we are not overshooting dt_size with the dt_entry selected */
2365 if ((dt_entry.offset + dt_entry.size) > dt_size) {
2366 dprintf(CRITICAL, "ERROR: Device tree contents are Invalid\n");
2367 return -1;
2368 }
2369
2370 best_match_dt_addr = (unsigned char *)table + dt_entry.offset;
2371 dtb_size = dt_entry.size;
2372 memmove((void *)hdr->tags_addr, (char *)best_match_dt_addr, dtb_size);
Vivek Kumar07bd0862018-04-10 14:19:23 +05302373
2374 } else {
2375 /* Validate the tags_addr */
2376 if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual) ||
2377 check_ddr_addr_range_bound(hdr->tags_addr, kernel_actual))
2378 {
2379 dprintf(CRITICAL, "Device tree addresses are not valid.\n");
2380 return -1;
2381 }
2382 /*
2383 * If appended dev tree is found, update the atags with
2384 * memory address to the DTB appended location on RAM.
2385 * Else update with the atags address in the kernel header
Mayank Grover1ed3e8b2018-09-19 15:16:50 +05302386 *
2387 * Make sure everything from scratch address is read before next step!
2388 * In case of dtbo, this API is going to read dtbo on scratch.
Vivek Kumar07bd0862018-04-10 14:19:23 +05302389 */
2390 void *dtb = NULL;
2391 dtb = dev_tree_appended((void*)(image_addr + page_size ),hdr->kernel_size, dtb_offset, (void *)hdr->tags_addr);
2392 if (!dtb) {
2393 dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
2394 return -1;
2395 }
2396 }
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05302397#endif
2398 if(target_use_signed_kernel() && (!device.is_unlocked))
2399 {
Shashank Mittal162244e2011-08-08 19:01:25 -07002400 /* Make sure everything from scratch address is read before next step!*/
Shashank Mittala0032282011-08-26 14:50:11 -07002401 if(device.is_tampered)
Shashank Mittal162244e2011-08-08 19:01:25 -07002402 {
2403 write_device_info_flash(&device);
2404 }
Channagoud Kadabi5c86fe32012-02-16 10:58:48 +05302405#if USE_PCOM_SECBOOT
2406 set_tamper_flag(device.is_tampered);
2407#endif
Shashank Mittalcd98d472011-08-02 14:29:24 -07002408 }
David Ng183a7422009-12-07 14:55:21 -08002409continue_boot:
Dima Zavin214cc642009-01-26 11:16:21 -08002410
Dima Zavin214cc642009-01-26 11:16:21 -08002411 /* TODO: create/pass atags to kernel */
2412
Ajay Dudanie28a6072011-07-01 13:59:46 -07002413 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07002414 (const char *)hdr->cmdline, board_machtype(),
Dima Zavin214cc642009-01-26 11:16:21 -08002415 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
2416
2417 return 0;
2418}
Brian Swetland9c4c0752009-01-25 16:23:50 -08002419
Shashank Mittal162244e2011-08-08 19:01:25 -07002420void write_device_info_mmc(device_info *dev)
2421{
Shashank Mittal162244e2011-08-08 19:01:25 -07002422 unsigned long long ptn = 0;
2423 unsigned long long size;
2424 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002425 uint8_t lun = 0;
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002426 uint32_t ret = 0;
Mayank Groverddd348d2018-01-23 14:07:09 +05302427 uint32_t device_info_sz = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07002428
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002429 if (devinfo_present)
2430 index = partition_get_index("devinfo");
2431 else
2432 index = partition_get_index("aboot");
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07002433
Shashank Mittal162244e2011-08-08 19:01:25 -07002434 ptn = partition_get_offset(index);
2435 if(ptn == 0)
2436 {
2437 return;
2438 }
2439
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002440 lun = partition_get_lun(index);
2441 mmc_set_lun(lun);
2442
Shashank Mittal162244e2011-08-08 19:01:25 -07002443 size = partition_get_size(index);
2444
Mayank Groverddd348d2018-01-23 14:07:09 +05302445 device_info_sz = ROUND_TO_PAGE(sizeof(struct device_info),
2446 mmc_blocksize_mask);
2447 if (device_info_sz == UINT_MAX)
2448 {
2449 dprintf(CRITICAL, "ERROR: Incorrect blocksize of card\n");
2450 return;
2451 }
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07002452
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002453 if (devinfo_present)
Mayank Groverddd348d2018-01-23 14:07:09 +05302454 ret = mmc_write(ptn, device_info_sz, (void *)info_buf);
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002455 else
Mayank Groverddd348d2018-01-23 14:07:09 +05302456 ret = mmc_write((ptn + size - device_info_sz), device_info_sz, (void *)info_buf);
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002457 if (ret)
Shashank Mittal162244e2011-08-08 19:01:25 -07002458 {
2459 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
Sridhar Parasurame1a97922015-07-27 15:31:22 -07002460 ASSERT(0);
Shashank Mittal162244e2011-08-08 19:01:25 -07002461 }
2462}
2463
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002464void read_device_info_mmc(struct device_info *info)
Shashank Mittal162244e2011-08-08 19:01:25 -07002465{
Shashank Mittal162244e2011-08-08 19:01:25 -07002466 unsigned long long ptn = 0;
2467 unsigned long long size;
2468 int index = INVALID_PTN;
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002469 uint32_t ret = 0;
Mayank Groverddd348d2018-01-23 14:07:09 +05302470 uint32_t device_info_sz = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07002471
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002472 if ((index = partition_get_index("devinfo")) < 0)
2473 {
2474 devinfo_present = false;
2475 index = partition_get_index("aboot");
2476 }
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07002477
Shashank Mittal162244e2011-08-08 19:01:25 -07002478 ptn = partition_get_offset(index);
2479 if(ptn == 0)
2480 {
2481 return;
2482 }
2483
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07002484 mmc_set_lun(partition_get_lun(index));
2485
Shashank Mittal162244e2011-08-08 19:01:25 -07002486 size = partition_get_size(index);
2487
Mayank Groverddd348d2018-01-23 14:07:09 +05302488 device_info_sz = ROUND_TO_PAGE(sizeof(struct device_info),
2489 mmc_blocksize_mask);
2490 if (device_info_sz == UINT_MAX)
2491 {
2492 dprintf(CRITICAL, "ERROR: Incorrect blocksize of card\n");
2493 return;
2494 }
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07002495
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002496 if (devinfo_present)
Mayank Groverddd348d2018-01-23 14:07:09 +05302497 ret = mmc_read(ptn, (void *)info_buf, device_info_sz);
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002498 else
Mayank Groverddd348d2018-01-23 14:07:09 +05302499 ret = mmc_read((ptn + size - device_info_sz), (void *)info_buf, device_info_sz);
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002500 if (ret)
Shashank Mittal162244e2011-08-08 19:01:25 -07002501 {
2502 dprintf(CRITICAL, "ERROR: Cannot read device info\n");
Sridhar Parasurame1a97922015-07-27 15:31:22 -07002503 ASSERT(0);
Shashank Mittal162244e2011-08-08 19:01:25 -07002504 }
Shashank Mittal162244e2011-08-08 19:01:25 -07002505}
2506
2507void write_device_info_flash(device_info *dev)
2508{
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002509 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
Shashank Mittal162244e2011-08-08 19:01:25 -07002510 struct ptentry *ptn;
2511 struct ptable *ptable;
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002512 if(info == NULL)
2513 {
2514 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
2515 ASSERT(0);
2516 }
2517 info_buf = info;
Shashank Mittal162244e2011-08-08 19:01:25 -07002518 ptable = flash_get_ptable();
2519 if (ptable == NULL)
2520 {
2521 dprintf(CRITICAL, "ERROR: Partition table not found\n");
2522 return;
2523 }
2524
2525 ptn = ptable_find(ptable, "devinfo");
2526 if (ptn == NULL)
2527 {
Smita Ghosh670c8b82015-05-07 09:30:03 -07002528 dprintf(CRITICAL, "ERROR: No devinfo partition found\n");
Shashank Mittal162244e2011-08-08 19:01:25 -07002529 return;
2530 }
2531
Mayank Groverdedbc892017-10-24 13:41:34 +05302532 memset(info, 0, BOOT_IMG_MAX_PAGE_SIZE);
Shashank Mittal162244e2011-08-08 19:01:25 -07002533 memcpy(info, dev, sizeof(device_info));
2534
2535 if (flash_write(ptn, 0, (void *)info_buf, page_size))
2536 {
2537 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
2538 return;
2539 }
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002540 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07002541}
2542
vijay kumarc65876c2015-04-24 13:29:16 +05302543static int read_allow_oem_unlock(device_info *dev)
2544{
vijay kumarc65876c2015-04-24 13:29:16 +05302545 unsigned offset;
2546 int index;
2547 unsigned long long ptn;
2548 unsigned long long ptn_size;
2549 unsigned blocksize = mmc_get_device_blocksize();
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07002550 STACKBUF_DMA_ALIGN(buf, blocksize);
vijay kumarc65876c2015-04-24 13:29:16 +05302551
vijay kumarca2e6812015-07-08 20:28:25 +05302552 index = partition_get_index(frp_ptns[0]);
vijay kumarc65876c2015-04-24 13:29:16 +05302553 if (index == INVALID_PTN)
2554 {
vijay kumarca2e6812015-07-08 20:28:25 +05302555 index = partition_get_index(frp_ptns[1]);
2556 if (index == INVALID_PTN)
2557 {
2558 dprintf(CRITICAL, "Neither '%s' nor '%s' partition found\n", frp_ptns[0],frp_ptns[1]);
2559 return -1;
2560 }
vijay kumarc65876c2015-04-24 13:29:16 +05302561 }
2562
2563 ptn = partition_get_offset(index);
2564 ptn_size = partition_get_size(index);
2565 offset = ptn_size - blocksize;
2566
Mayank Grover48860402016-11-29 12:34:53 +05302567 /* Set Lun for partition */
2568 mmc_set_lun(partition_get_lun(index));
2569
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07002570 if (mmc_read(ptn + offset, (void *)buf, blocksize))
vijay kumarc65876c2015-04-24 13:29:16 +05302571 {
2572 dprintf(CRITICAL, "Reading MMC failed\n");
2573 return -1;
2574 }
2575
2576 /*is_allow_unlock is a bool value stored at the LSB of last byte*/
2577 is_allow_unlock = buf[blocksize-1] & 0x01;
2578 return 0;
2579}
2580
2581static int write_allow_oem_unlock(bool allow_unlock)
2582{
vijay kumarc65876c2015-04-24 13:29:16 +05302583 unsigned offset;
vijay kumarc65876c2015-04-24 13:29:16 +05302584 int index;
2585 unsigned long long ptn;
2586 unsigned long long ptn_size;
2587 unsigned blocksize = mmc_get_device_blocksize();
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07002588 STACKBUF_DMA_ALIGN(buf, blocksize);
vijay kumarc65876c2015-04-24 13:29:16 +05302589
vijay kumarca2e6812015-07-08 20:28:25 +05302590 index = partition_get_index(frp_ptns[0]);
vijay kumarc65876c2015-04-24 13:29:16 +05302591 if (index == INVALID_PTN)
2592 {
vijay kumarca2e6812015-07-08 20:28:25 +05302593 index = partition_get_index(frp_ptns[1]);
2594 if (index == INVALID_PTN)
2595 {
2596 dprintf(CRITICAL, "Neither '%s' nor '%s' partition found\n", frp_ptns[0],frp_ptns[1]);
2597 return -1;
2598 }
vijay kumarc65876c2015-04-24 13:29:16 +05302599 }
2600
2601 ptn = partition_get_offset(index);
2602 ptn_size = partition_get_size(index);
2603 offset = ptn_size - blocksize;
Mayank Grover48860402016-11-29 12:34:53 +05302604 mmc_set_lun(partition_get_lun(index));
vijay kumarc65876c2015-04-24 13:29:16 +05302605
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07002606 if (mmc_read(ptn + offset, (void *)buf, blocksize))
vijay kumarc65876c2015-04-24 13:29:16 +05302607 {
2608 dprintf(CRITICAL, "Reading MMC failed\n");
2609 return -1;
2610 }
2611
2612 /*is_allow_unlock is a bool value stored at the LSB of last byte*/
2613 buf[blocksize-1] = allow_unlock;
2614 if (mmc_write(ptn + offset, blocksize, buf))
2615 {
2616 dprintf(CRITICAL, "Writing MMC failed\n");
2617 return -1;
2618 }
2619
2620 return 0;
2621}
2622
Shashank Mittal162244e2011-08-08 19:01:25 -07002623void read_device_info_flash(device_info *dev)
2624{
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002625 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
Shashank Mittal162244e2011-08-08 19:01:25 -07002626 struct ptentry *ptn;
2627 struct ptable *ptable;
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002628 if(info == NULL)
2629 {
2630 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
2631 ASSERT(0);
2632 }
2633 info_buf = info;
Shashank Mittal162244e2011-08-08 19:01:25 -07002634 ptable = flash_get_ptable();
2635 if (ptable == NULL)
2636 {
2637 dprintf(CRITICAL, "ERROR: Partition table not found\n");
2638 return;
2639 }
2640
2641 ptn = ptable_find(ptable, "devinfo");
2642 if (ptn == NULL)
2643 {
Smita Ghosh670c8b82015-05-07 09:30:03 -07002644 dprintf(CRITICAL, "ERROR: No devinfo partition found\n");
Shashank Mittal162244e2011-08-08 19:01:25 -07002645 return;
2646 }
2647
2648 if (flash_read(ptn, 0, (void *)info_buf, page_size))
2649 {
2650 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
2651 return;
2652 }
2653
2654 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
2655 {
Shashank Mittal162244e2011-08-08 19:01:25 -07002656 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
2657 info->is_unlocked = 0;
Shashank Mittala0032282011-08-26 14:50:11 -07002658 info->is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07002659 write_device_info_flash(info);
2660 }
2661 memcpy(dev, info, sizeof(device_info));
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002662 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07002663}
2664
2665void write_device_info(device_info *dev)
2666{
2667 if(target_is_emmc_boot())
2668 {
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002669 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
2670 if(info == NULL)
2671 {
2672 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
2673 ASSERT(0);
2674 }
2675 info_buf = info;
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002676 memcpy(info, dev, sizeof(struct device_info));
2677
2678#if USE_RPMB_FOR_DEVINFO
Mayank Grover912eaa62017-10-26 12:08:53 +05302679 if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05302680 is_secure_boot_enable()) {
2681 if((write_device_info_rpmb((void*) info, PAGE_SIZE)) < 0)
2682 ASSERT(0);
Sridhar Parasurame1a97922015-07-27 15:31:22 -07002683 }
Channagoud Kadabic80cb492015-04-28 16:08:28 -07002684 else
2685 write_device_info_mmc(info);
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002686#else
2687 write_device_info_mmc(info);
2688#endif
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002689 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07002690 }
2691 else
2692 {
2693 write_device_info_flash(dev);
2694 }
2695}
2696
Monika Singh94316462018-03-15 18:39:01 +05302697int read_rollback_index(uint32_t loc, uint64_t *roll_back_index)
2698{
2699 if (!devinfo_present) {
2700 dprintf(CRITICAL, "DeviceInfo not initalized \n");
2701 return -EINVAL;
2702 }
2703 if (loc >= ARRAY_SIZE(device.rollback_index)) {
2704 dprintf(CRITICAL, "%s() Loc out of range index: %d, array len: %d\n",
2705 __func__, loc, ARRAY_SIZE(device.rollback_index));
2706 ASSERT(0);
2707 }
2708
2709 *roll_back_index = device.rollback_index[loc];
2710 return 0;
2711}
2712
2713int write_rollback_index(uint32_t loc, uint64_t roll_back_index)
2714{
2715 if (!devinfo_present) {
2716 dprintf(CRITICAL, "DeviceInfo not initalized \n");
2717 return -EINVAL;
2718 }
2719 if (loc >= ARRAY_SIZE(device.rollback_index)) {
2720 dprintf(CRITICAL, "%s() Loc out of range index: %d, array len: %d\n",
2721 __func__, loc, ARRAY_SIZE(device.rollback_index));
2722 ASSERT(0);
2723 }
2724
2725 device.rollback_index[loc] = roll_back_index;
2726 write_device_info(&device);
2727 return 0;
2728}
2729
Monika Singhb0fad822018-03-15 18:50:55 +05302730int store_userkey(uint8_t *user_key, uint32_t user_key_size)
2731{
2732 if (!devinfo_present) {
2733 dprintf(CRITICAL, "DeviceInfo not initalized \n");
2734 return -EINVAL;
2735 }
2736
2737 if (user_key_size > ARRAY_SIZE(device.user_public_key)) {
2738 dprintf(CRITICAL, "StoreUserKey, UserKeySize too large!\n");
2739 return -ENODEV;
2740 }
2741
2742 memcpy(device.user_public_key, user_key, user_key_size);
2743 device.user_public_key_length = user_key_size;
2744 write_device_info(&device);
2745 return 0;
2746}
2747
2748int erase_userkey()
2749{
2750 if (!devinfo_present) {
2751 dprintf(CRITICAL, "DeviceInfo not initalized \n");
2752 return -EINVAL;
2753 }
2754 memset(device.user_public_key, 0, ARRAY_SIZE(device.user_public_key));
2755 device.user_public_key_length = 0;
2756 write_device_info(&device);
2757 return 0;
2758}
2759
2760int get_userkey(uint8_t **user_key, uint32_t *user_key_size)
2761{
2762 if (!devinfo_present) {
2763 dprintf(CRITICAL, "DeviceInfo not initalized \n");
2764 return -EINVAL;
2765 }
2766 *user_key = device.user_public_key;
2767 *user_key_size = device.user_public_key_length;
2768 return 0;
2769}
2770
Shashank Mittal162244e2011-08-08 19:01:25 -07002771void read_device_info(device_info *dev)
2772{
2773 if(target_is_emmc_boot())
2774 {
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002775 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
2776 if(info == NULL)
2777 {
2778 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
2779 ASSERT(0);
2780 }
2781 info_buf = info;
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002782
2783#if USE_RPMB_FOR_DEVINFO
Mayank Grover912eaa62017-10-26 12:08:53 +05302784 if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05302785 is_secure_boot_enable()) {
2786 if((read_device_info_rpmb((void*) info, PAGE_SIZE)) < 0)
2787 ASSERT(0);
Sridhar Parasurame1a97922015-07-27 15:31:22 -07002788 }
Channagoud Kadabic80cb492015-04-28 16:08:28 -07002789 else
2790 read_device_info_mmc(info);
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002791#else
2792 read_device_info_mmc(info);
2793#endif
2794
2795 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
2796 {
2797 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
lijuang511a2b52015-08-14 20:50:51 +08002798 if (is_secure_boot_enable()) {
Channagoud Kadabi05f78ba2015-07-06 11:58:14 -07002799 info->is_unlocked = 0;
Monika Singh292b3e92018-03-17 22:40:23 +05302800#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover912eaa62017-10-26 12:08:53 +05302801 if (VB_M <= target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +05302802 info->is_unlock_critical = 0;
Parth Dixitddbc7352015-10-18 03:13:31 +05302803#endif
lijuang511a2b52015-08-14 20:50:51 +08002804 } else {
Channagoud Kadabi2fda4092015-07-07 13:34:11 -07002805 info->is_unlocked = 1;
Monika Singh292b3e92018-03-17 22:40:23 +05302806#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover912eaa62017-10-26 12:08:53 +05302807 if (VB_M <= target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +05302808 info->is_unlock_critical = 1;
Parth Dixitddbc7352015-10-18 03:13:31 +05302809#endif
lijuang511a2b52015-08-14 20:50:51 +08002810 }
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002811 info->is_tampered = 0;
2812 info->charger_screen_enabled = 0;
Monika Singh292b3e92018-03-17 22:40:23 +05302813#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover912eaa62017-10-26 12:08:53 +05302814 if (VB_M <= target_get_vb_version())
Monika Singh1c71a1c2019-12-03 12:12:48 +05302815 {
Mayank Grover889be1b2017-09-12 20:12:23 +05302816 info->verity_mode = 1; //enforcing by default
Monika Singh1c71a1c2019-12-03 12:12:48 +05302817 info->user_public_key_length = 0;
2818 memset(info->rollback_index, 0, sizeof(info->rollback_index));
2819 memset(info->user_public_key, 0, sizeof(info->user_public_key));
2820 }
Parth Dixitddbc7352015-10-18 03:13:31 +05302821#endif
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002822 write_device_info(info);
2823 }
2824 memcpy(dev, info, sizeof(device_info));
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002825 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07002826 }
2827 else
2828 {
2829 read_device_info_flash(dev);
2830 }
2831}
2832
2833void reset_device_info()
2834{
2835 dprintf(ALWAYS, "reset_device_info called.");
Shashank Mittala0032282011-08-26 14:50:11 -07002836 device.is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07002837 write_device_info(&device);
2838}
2839
2840void set_device_root()
2841{
2842 dprintf(ALWAYS, "set_device_root called.");
Shashank Mittala0032282011-08-26 14:50:11 -07002843 device.is_tampered = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -07002844 write_device_info(&device);
2845}
2846
lijuang4ece1e72015-08-14 21:02:36 +08002847/* set device unlock value
2848 * Must check FRP before call this function
2849 * Need to wipe data when unlock status changed
2850 * type 0: oem unlock
2851 * type 1: unlock critical
2852 * status 0: unlock as false
2853 * status 1: lock as true
2854 */
2855void set_device_unlock_value(int type, bool status)
lijuang21f12f52015-08-22 16:22:19 +08002856{
lijuang4ece1e72015-08-14 21:02:36 +08002857 if (type == UNLOCK)
2858 device.is_unlocked = status;
Monika Singh292b3e92018-03-17 22:40:23 +05302859#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover912eaa62017-10-26 12:08:53 +05302860 else if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05302861 type == UNLOCK_CRITICAL)
2862 device.is_unlock_critical = status;
Parth Dixitddbc7352015-10-18 03:13:31 +05302863#endif
lijuang4ece1e72015-08-14 21:02:36 +08002864 write_device_info(&device);
2865}
2866
2867static void set_device_unlock(int type, bool status)
2868{
2869 int is_unlocked = -1;
2870 char response[MAX_RSP_SIZE];
2871
2872 /* check device unlock status if it is as expected */
2873 if (type == UNLOCK)
2874 is_unlocked = device.is_unlocked;
Monika Singh292b3e92018-03-17 22:40:23 +05302875#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover912eaa62017-10-26 12:08:53 +05302876 if(VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05302877 type == UNLOCK_CRITICAL)
2878 {
2879 is_unlocked = device.is_unlock_critical;
2880 }
Parth Dixitddbc7352015-10-18 03:13:31 +05302881#endif
lijuang4ece1e72015-08-14 21:02:36 +08002882 if (is_unlocked == status) {
2883 snprintf(response, sizeof(response), "\tDevice already : %s", (status ? "unlocked!" : "locked!"));
2884 fastboot_info(response);
2885 fastboot_okay("");
2886 return;
2887 }
2888
2889 /* status is true, it means to unlock device */
lijuang07c5d6e2018-04-23 18:32:13 +08002890 if (status && !is_allow_unlock) {
2891 fastboot_fail("oem unlock is not allowed");
2892 return;
2893 }
lijuang21f12f52015-08-22 16:22:19 +08002894
lijuang4ece1e72015-08-14 21:02:36 +08002895#if FBCON_DISPLAY_MSG
lijuang07c5d6e2018-04-23 18:32:13 +08002896 display_unlock_menu(type, status);
2897 fastboot_okay("");
2898 return;
lijuang4ece1e72015-08-14 21:02:36 +08002899#else
lijuang07c5d6e2018-04-23 18:32:13 +08002900 if (status && type == UNLOCK) {
2901 fastboot_fail("Need wipe userdata. Do 'fastboot oem unlock-go'");
2902 return;
lijuang21f12f52015-08-22 16:22:19 +08002903 }
lijuang07c5d6e2018-04-23 18:32:13 +08002904#endif
lijuang4ece1e72015-08-14 21:02:36 +08002905
2906 set_device_unlock_value(type, status);
2907
2908 /* wipe data */
2909 struct recovery_message msg;
Kishor PK60a68212017-05-08 16:55:57 +05302910 memset(&msg, 0, sizeof(msg));
lijuang4ece1e72015-08-14 21:02:36 +08002911 snprintf(msg.recovery, sizeof(msg.recovery), "recovery\n--wipe_data");
2912 write_misc(0, &msg, sizeof(msg));
2913
2914 fastboot_okay("");
2915 reboot_device(RECOVERY_MODE);
lijuang21f12f52015-08-22 16:22:19 +08002916}
2917
lijuang511a2b52015-08-14 20:50:51 +08002918static bool critical_flash_allowed(const char * entry)
2919{
2920 uint32_t i = 0;
2921 if (entry == NULL)
2922 return false;
2923
2924 for (i = 0; i < ARRAY_SIZE(critical_flash_allowed_ptn); i++) {
2925 if(!strcmp(entry, critical_flash_allowed_ptn[i]))
2926 return true;
2927 }
2928 return false;
Matthew Qin271927e2015-03-31 22:07:07 -04002929}
2930
2931#if DEVICE_TREE
Amol Jadicb524072012-08-09 16:40:18 -07002932int copy_dtb(uint8_t *boot_image_start, unsigned int scratch_offset)
2933{
2934 uint32 dt_image_offset = 0;
Amol Jadicb524072012-08-09 16:40:18 -07002935 uint32_t n;
Monika Singh292b3e92018-03-17 22:40:23 +05302936 struct dt_table *table = NULL;
Amol Jadicb524072012-08-09 16:40:18 -07002937 struct dt_entry dt_entry;
Monika Singh292b3e92018-03-17 22:40:23 +05302938 uint32_t dt_hdr_size = 0;
Amol Jadicb524072012-08-09 16:40:18 -07002939 unsigned int compressed_size = 0;
2940 unsigned int dtb_size = 0;
2941 unsigned int out_avai_len = 0;
2942 unsigned char *out_addr = NULL;
2943 unsigned char *best_match_dt_addr = NULL;
2944 int rc;
2945
Mayank Grovere1ab96c2018-09-04 20:31:31 +05302946 boot_img_hdr *hdr = (boot_img_hdr *) (boot_image_start);
Amol Jadicb524072012-08-09 16:40:18 -07002947
Parth Dixit4097b622016-03-15 14:42:27 +05302948#ifndef OSVERSION_IN_BOOTIMAGE
2949 dt_size = hdr->dt_size;
2950#endif
2951
2952 if(dt_size != 0) {
Amol Jadicb524072012-08-09 16:40:18 -07002953 /* add kernel offset */
2954 dt_image_offset += page_size;
2955 n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
2956 dt_image_offset += n;
2957
2958 /* add ramdisk offset */
2959 n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
2960 dt_image_offset += n;
2961
2962 /* add second offset */
2963 if(hdr->second_size != 0) {
2964 n = ROUND_TO_PAGE(hdr->second_size, page_mask);
2965 dt_image_offset += n;
2966 }
2967
2968 /* offset now point to start of dt.img */
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07002969 table = (struct dt_table*)(boot_image_start + dt_image_offset);
Amol Jadicb524072012-08-09 16:40:18 -07002970
Deepa Dinamani19648b42013-09-05 17:05:55 -07002971 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Amol Jadicb524072012-08-09 16:40:18 -07002972 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
2973 return -1;
2974 }
P.V. Phani Kumar82916762016-03-09 09:20:19 +05302975
2976 /* Its Error if, dt_hdr_size (table->num_entries * dt_entry size + Dev_Tree Header)
2977 goes beyound hdr->dt_size*/
Parth Dixit4097b622016-03-15 14:42:27 +05302978 if (dt_hdr_size > ROUND_TO_PAGE(dt_size,hdr->page_size)) {
P.V. Phani Kumar82916762016-03-09 09:20:19 +05302979 dprintf(CRITICAL, "ERROR: Invalid Device Tree size \n");
2980 return -1;
2981 }
2982
Joel Kingaa335dc2013-06-03 16:11:08 -07002983 /* Find index of device tree within device tree table */
2984 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Amol Jadicb524072012-08-09 16:40:18 -07002985 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
2986 return -1;
2987 }
2988
Matthew Qin271927e2015-03-31 22:07:07 -04002989 best_match_dt_addr = (unsigned char *)boot_image_start + dt_image_offset + dt_entry.offset;
2990 if (is_gzip_package(best_match_dt_addr, dt_entry.size))
2991 {
2992 out_addr = (unsigned char *)target_get_scratch_address() + scratch_offset;
2993 out_avai_len = target_get_max_flash_size() - scratch_offset;
Matthew Qin0b15b322015-05-19 05:20:54 -04002994 dprintf(INFO, "decompressing dtb: start\n");
Matthew Qin271927e2015-03-31 22:07:07 -04002995 rc = decompress(best_match_dt_addr,
2996 dt_entry.size, out_addr, out_avai_len,
2997 &compressed_size, &dtb_size);
2998 if (rc)
2999 {
Matthew Qin0b15b322015-05-19 05:20:54 -04003000 dprintf(CRITICAL, "decompressing dtb failed!!!\n");
Matthew Qin271927e2015-03-31 22:07:07 -04003001 ASSERT(0);
3002 }
3003
Matthew Qin0b15b322015-05-19 05:20:54 -04003004 dprintf(INFO, "decompressing dtb: done\n");
Matthew Qin271927e2015-03-31 22:07:07 -04003005 best_match_dt_addr = out_addr;
3006 } else {
3007 dtb_size = dt_entry.size;
3008 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07003009 /* Validate and Read device device tree in the "tags_add */
Mayank Grovere559cec2017-10-17 15:12:03 +05303010 if (check_aboot_addr_range_overlap(hdr->tags_addr, dtb_size) ||
3011 check_ddr_addr_range_bound(hdr->tags_addr, dtb_size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07003012 {
Mayank Grovere559cec2017-10-17 15:12:03 +05303013 dprintf(CRITICAL, "Device tree addresses are not valid.\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07003014 return -1;
3015 }
3016
Amol Jadicb524072012-08-09 16:40:18 -07003017 /* Read device device tree in the "tags_add */
Matthew Qin271927e2015-03-31 22:07:07 -04003018 memmove((void*) hdr->tags_addr, (void *)best_match_dt_addr, dtb_size);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07003019 } else
3020 return -1;
Amol Jadicb524072012-08-09 16:40:18 -07003021
3022 /* Everything looks fine. Return success. */
3023 return 0;
3024}
3025#endif
3026
Brian Swetland9c4c0752009-01-25 16:23:50 -08003027void cmd_boot(const char *arg, void *data, unsigned sz)
3028{
3029 unsigned kernel_actual;
3030 unsigned ramdisk_actual;
Kishor PKd8ddcad2017-07-27 13:53:57 +05303031 unsigned second_actual;
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07003032 uint32_t image_actual;
3033 uint32_t dt_actual = 0;
Mayank Grovere1ab96c2018-09-04 20:31:31 +05303034 boot_img_hdr *hdr = NULL;
Matthew Qinbb7923d2015-02-09 10:56:09 +08003035 struct kernel64_hdr *kptr = NULL;
Brian Swetland9c4c0752009-01-25 16:23:50 -08003036 char *ptr = ((char*) data);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07003037 int ret = 0;
3038 uint8_t dtb_copied = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08003039 unsigned int out_len = 0;
3040 unsigned int out_avai_len = 0;
3041 unsigned char *out_addr = NULL;
3042 uint32_t dtb_offset = 0;
3043 unsigned char *kernel_start_addr = NULL;
3044 unsigned int kernel_size = 0;
Matthew Qin271927e2015-03-31 22:07:07 -04003045 unsigned int scratch_offset = 0;
Saranya Chidurab4933332018-10-15 17:30:06 +05303046#if VERIFIED_BOOT_2
3047 void *dtbo_image_buf = NULL;
3048 uint32_t dtbo_image_sz = 0;
3049 void *vbmeta_image_buf = NULL;
3050 uint32_t vbmeta_image_sz = 0;
3051#endif
Monika Singh292b3e92018-03-17 22:40:23 +05303052#if !VERIFIED_BOOT_2
3053 uint32_t sig_actual = 0;
3054 uint32_t sig_size = 0;
3055#ifdef MDTP_SUPPORT
3056 static bool is_mdtp_activated = 0;
3057#endif /* MDTP_SUPPORT */
3058#endif
Matthew Qinbb7923d2015-02-09 10:56:09 +08003059
lijuang2008ff22016-03-07 17:56:27 +08003060#if FBCON_DISPLAY_MSG
3061 /* Exit keys' detection thread firstly */
3062 exit_menu_keys_detection();
3063#endif
3064
Monika Singh292b3e92018-03-17 22:40:23 +05303065#if VERIFIED_BOOT || VERIFIED_BOOT_2
Channagoud Kadabi6d5375e2015-06-23 17:15:42 -07003066 if(target_build_variant_user() && !device.is_unlocked)
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003067 {
3068 fastboot_fail("unlock device to use this command");
lijuang2008ff22016-03-07 17:56:27 +08003069 goto boot_failed;
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003070 }
3071#endif
3072
Brian Swetland9c4c0752009-01-25 16:23:50 -08003073 if (sz < sizeof(hdr)) {
3074 fastboot_fail("invalid bootimage header");
lijuang2008ff22016-03-07 17:56:27 +08003075 goto boot_failed;
Brian Swetland9c4c0752009-01-25 16:23:50 -08003076 }
3077
Mayank Grovere1ab96c2018-09-04 20:31:31 +05303078 hdr = (boot_img_hdr *)data;
Brian Swetland9c4c0752009-01-25 16:23:50 -08003079
3080 /* ensure commandline is terminated */
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07003081 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -08003082
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07003083 if(target_is_emmc_boot() && hdr->page_size) {
3084 page_size = hdr->page_size;
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07003085 page_mask = page_size - 1;
3086 }
3087
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07003088 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
3089 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Kishor PKd8ddcad2017-07-27 13:53:57 +05303090 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07003091#if DEVICE_TREE
Parth Dixit4097b622016-03-15 14:42:27 +05303092#ifndef OSVERSION_IN_BOOTIMAGE
3093 dt_size = hdr->dt_size;
3094#endif
3095 dt_actual = ROUND_TO_PAGE(dt_size, page_mask);
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07003096#endif
3097
3098 image_actual = ADD_OF(page_size, kernel_actual);
3099 image_actual = ADD_OF(image_actual, ramdisk_actual);
Kishor PKd8ddcad2017-07-27 13:53:57 +05303100 image_actual = ADD_OF(image_actual, second_actual);
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07003101 image_actual = ADD_OF(image_actual, dt_actual);
3102
Kishor PK5134b332017-05-09 17:50:08 +05303103 /* Checking to prevent oob access in read_der_message_length */
3104 if (image_actual > sz) {
3105 fastboot_fail("bootimage header fields are invalid");
3106 goto boot_failed;
3107 }
Saranya Chidurab4933332018-10-15 17:30:06 +05303108
Monika Singh292b3e92018-03-17 22:40:23 +05303109#if VERIFIED_BOOT_2
Mayank Grover1ceaee22019-04-01 17:54:32 +05303110 /* Pass size of boot partition, as imgsize, to avoid
3111 read fewer bytes error */
3112 image_actual = partition_get_size(partition_get_index("boot"));
Saranya Chidurab4933332018-10-15 17:30:06 +05303113
3114 /* load and validate dtbo partition */
3115 load_validate_dtbo_image(&dtbo_image_buf, &dtbo_image_sz);
3116
3117 /* load vbmeta partition */
3118 load_vbmeta_image(&vbmeta_image_buf, &vbmeta_image_sz);
3119
Monika Singh292b3e92018-03-17 22:40:23 +05303120 memset(&info, 0, sizeof(bootinfo));
Saranya Chidurab4933332018-10-15 17:30:06 +05303121
3122 info.images[IMG_BOOT].image_buffer = SUB_SALT_BUFF_OFFSET(data);
3123 info.images[IMG_BOOT].imgsize = image_actual;
3124 info.images[IMG_BOOT].name = "boot";
3125 ++info.num_loaded_images;
3126
3127 /* Pass loaded dtbo image */
3128 if (dtbo_image_buf != NULL) {
3129 info.images[IMG_DTBO].image_buffer = SUB_SALT_BUFF_OFFSET(dtbo_image_buf);
3130 info.images[IMG_DTBO].imgsize = dtbo_image_sz;
3131 info.images[IMG_DTBO].name = "dtbo";
3132 ++info.num_loaded_images;
3133 }
3134
3135 /* Pass loaded vbmeta image */
3136 if (vbmeta_image_buf != NULL) {
3137 info.images[IMG_VBMETA].image_buffer = vbmeta_image_buf;
3138 info.images[IMG_VBMETA].imgsize = vbmeta_image_sz;
3139 info.images[IMG_VBMETA].name = "vbmeta";
3140 ++info.num_loaded_images;
3141 }
3142
Monika Singh292b3e92018-03-17 22:40:23 +05303143 info.multi_slot_boot = partition_multislot_is_supported();
3144 if (load_image_and_auth(&info))
3145 goto boot_failed;
3146 vbcmdline = info.vbcmdline;
Saranya Chidurab4933332018-10-15 17:30:06 +05303147
3148 /* Free the buffer allocated to vbmeta post verification */
3149 if (vbmeta_image_buf != NULL) {
3150 free(vbmeta_image_buf);
3151 --info.num_loaded_images;
3152 }
Monika Singh292b3e92018-03-17 22:40:23 +05303153#else
Kishor PK5134b332017-05-09 17:50:08 +05303154 sig_size = sz - image_actual;
3155
P.V. Phani Kumareecfd822016-03-09 20:09:03 +05303156 if (target_use_signed_kernel() && (!device.is_unlocked)) {
Monika Singh292b3e92018-03-17 22:40:23 +05303157 unsigned chk;
P.V. Phani Kumareecfd822016-03-09 20:09:03 +05303158 /* Calculate the signature length from boot image */
3159 sig_actual = read_der_message_length(
Kishor PK5134b332017-05-09 17:50:08 +05303160 (unsigned char*)(data + image_actual), sig_size);
Monika Singh292b3e92018-03-17 22:40:23 +05303161 chk = ADD_OF(image_actual, sig_actual);
Channagoud Kadabi3d839012014-06-26 14:26:39 -07003162
Monika Singh292b3e92018-03-17 22:40:23 +05303163 if (chk > sz) {
Kishor PK5134b332017-05-09 17:50:08 +05303164 fastboot_fail("bootimage header fields are invalid");
3165 goto boot_failed;
3166 }
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07003167 }
3168
Sridhar Parasuram361e4cd2015-09-24 11:19:19 -07003169 // Initialize boot state before trying to verify boot.img
3170#if VERIFIED_BOOT
Channagoud Kadabi699466e2015-11-03 12:37:42 -08003171 boot_verifier_init();
Mayank Groverb337e932017-01-18 20:00:40 +05303172#endif
Channagoud Kadabibf6ce7d2015-09-17 13:25:35 -07003173 /* Handle overflow if the input image size is greater than
3174 * boot image buffer can hold
3175 */
Monika Singh292b3e92018-03-17 22:40:23 +05303176 if ((target_get_max_flash_size() - page_size) < image_actual)
Channagoud Kadabibf6ce7d2015-09-17 13:25:35 -07003177 {
3178 fastboot_fail("booimage: size is greater than boot image buffer can hold");
lijuang2008ff22016-03-07 17:56:27 +08003179 goto boot_failed;
Channagoud Kadabibf6ce7d2015-09-17 13:25:35 -07003180 }
Sridhar Parasuram361e4cd2015-09-24 11:19:19 -07003181
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07003182 /* Verify the boot image
3183 * device & page_size are initialized in aboot_init
3184 */
Sridhar Parasuram7ae9fbc2015-09-17 09:56:30 -07003185 if (target_use_signed_kernel() && (!device.is_unlocked)) {
Channagoud Kadabi3d839012014-06-26 14:26:39 -07003186 /* Pass size excluding signature size, otherwise we would try to
3187 * access signature beyond its length
3188 */
Monika Singh292b3e92018-03-17 22:40:23 +05303189 verify_signed_bootimg((uint32_t)data, image_actual);
Sridhar Parasuram7ae9fbc2015-09-17 09:56:30 -07003190 }
Amit Blay4aa292f2015-04-28 21:55:59 +03003191#ifdef MDTP_SUPPORT
3192 else
3193 {
Shay Nachmani062aa3f2015-05-17 17:28:44 +03003194 /* fastboot boot is not allowed when MDTP is activated */
Amit Blay4aa292f2015-04-28 21:55:59 +03003195 mdtp_ext_partition_verification_t ext_partition;
Amit Blay8a510302015-08-17 09:20:01 +03003196
3197 if (!is_mdtp_activated) {
3198 ext_partition.partition = MDTP_PARTITION_NONE;
3199 mdtp_fwlock_verify_lock(&ext_partition);
3200 }
Amit Blay4aa292f2015-04-28 21:55:59 +03003201 }
Shay Nachmani062aa3f2015-05-17 17:28:44 +03003202
Amir Kotzer7c768c02016-04-13 09:08:05 +03003203 /* If mdtp state cannot be validate, block fastboot boot*/
3204 if(mdtp_activated(&is_mdtp_activated)){
3205 dprintf(CRITICAL, "mdtp_activated cannot validate state.\n");
3206 dprintf(CRITICAL, "Can not proceed with fastboot boot command.\n");
3207 goto boot_failed;
3208 }
Shay Nachmani062aa3f2015-05-17 17:28:44 +03003209 if(is_mdtp_activated){
3210 dprintf(CRITICAL, "fastboot boot command is not available.\n");
lijuang2008ff22016-03-07 17:56:27 +08003211 goto boot_failed;
Shay Nachmani062aa3f2015-05-17 17:28:44 +03003212 }
Amit Blay4aa292f2015-04-28 21:55:59 +03003213#endif /* MDTP_SUPPORT */
Monika Singh292b3e92018-03-17 22:40:23 +05303214#endif /* VERIFIED_BOOT_2 else */
Amit Blay4aa292f2015-04-28 21:55:59 +03003215
Sridhar Parasuram361e4cd2015-09-24 11:19:19 -07003216#if VERIFIED_BOOT
Monika Singh292b3e92018-03-17 22:40:23 +05303217 if (VB_M == target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +05303218 {
3219 /* set boot and system versions. */
3220 set_os_version((unsigned char *)data);
3221 // send root of trust
3222 if(!send_rot_command((uint32_t)device.is_unlocked))
3223 ASSERT(0);
3224 }
Parth Dixitbc9b6492015-10-18 00:41:38 +05303225#endif
Channagoud Kadabia22144f2013-03-20 11:49:01 -07003226 /*
Matthew Qinbb7923d2015-02-09 10:56:09 +08003227 * Check if the kernel image is a gzip package. If yes, need to decompress it.
3228 * If not, continue booting.
3229 */
3230 if (is_gzip_package((unsigned char *)(data + page_size), hdr->kernel_size))
3231 {
3232 out_addr = (unsigned char *)target_get_scratch_address();
3233 out_addr = (unsigned char *)(out_addr + image_actual + page_size);
3234 out_avai_len = target_get_max_flash_size() - image_actual - page_size;
Saranya Chidurab4933332018-10-15 17:30:06 +05303235#if VERIFIED_BOOT_2
3236 if (dtbo_image_sz)
3237 out_avai_len -= DTBO_IMG_BUF;
3238#endif
Matthew Qin0b15b322015-05-19 05:20:54 -04003239 dprintf(INFO, "decompressing kernel image: start\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08003240 ret = decompress((unsigned char *)(ptr + page_size),
3241 hdr->kernel_size, out_addr, out_avai_len,
3242 &dtb_offset, &out_len);
3243 if (ret)
3244 {
Matthew Qin0b15b322015-05-19 05:20:54 -04003245 dprintf(CRITICAL, "decompressing image failed!!!\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08003246 ASSERT(0);
3247 }
3248
Matthew Qin0b15b322015-05-19 05:20:54 -04003249 dprintf(INFO, "decompressing kernel image: done\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08003250 kptr = (struct kernel64_hdr *)out_addr;
3251 kernel_start_addr = out_addr;
3252 kernel_size = out_len;
3253 } else {
3254 kptr = (struct kernel64_hdr*)((char *)data + page_size);
3255 kernel_start_addr = (unsigned char *)((char *)data + page_size);
3256 kernel_size = hdr->kernel_size;
3257 }
3258
3259 /*
Channagoud Kadabia22144f2013-03-20 11:49:01 -07003260 * Update the kernel/ramdisk/tags address if the boot image header
3261 * has default values, these default values come from mkbootimg when
3262 * the boot image is flashed using fastboot flash:raw
3263 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08003264 update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr));
Dima Zavin3cadfff2013-03-21 14:30:48 -07003265
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07003266 /* Get virtual addresses since the hdr saves physical addresses. */
3267 hdr->kernel_addr = VA(hdr->kernel_addr);
3268 hdr->ramdisk_addr = VA(hdr->ramdisk_addr);
3269 hdr->tags_addr = VA(hdr->tags_addr);
Brian Swetland9c4c0752009-01-25 16:23:50 -08003270
Matthew Qinbb7923d2015-02-09 10:56:09 +08003271 kernel_size = ROUND_TO_PAGE(kernel_size, page_mask);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07003272 /* Check if the addresses in the header are valid. */
Matthew Qinbb7923d2015-02-09 10:56:09 +08003273 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_size) ||
Mayank Grovere559cec2017-10-17 15:12:03 +05303274 check_ddr_addr_range_bound(hdr->kernel_addr, kernel_size) ||
3275 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual) ||
3276 check_ddr_addr_range_bound(hdr->ramdisk_addr, ramdisk_actual))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07003277 {
Mayank Grovere559cec2017-10-17 15:12:03 +05303278 dprintf(CRITICAL, "kernel/ramdisk addresses are not valid.\n");
lijuang2008ff22016-03-07 17:56:27 +08003279 goto boot_failed;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07003280 }
3281
Amol Jadicb524072012-08-09 16:40:18 -07003282#if DEVICE_TREE
Matthew Qin271927e2015-03-31 22:07:07 -04003283 scratch_offset = image_actual + page_size + out_len;
Amol Jadicb524072012-08-09 16:40:18 -07003284 /* find correct dtb and copy it to right location */
Matthew Qin271927e2015-03-31 22:07:07 -04003285 ret = copy_dtb(data, scratch_offset);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07003286
3287 dtb_copied = !ret ? 1 : 0;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07003288#else
Mayank Grovere559cec2017-10-17 15:12:03 +05303289 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE) ||
3290 check_ddr_addr_range_bound(hdr->tags_addr, MAX_TAGS_SIZE))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07003291 {
Mayank Grovere559cec2017-10-17 15:12:03 +05303292 dprintf(CRITICAL, "Tags addresses are not valid.\n");
lijuang2008ff22016-03-07 17:56:27 +08003293 goto boot_failed;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07003294 }
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07003295#endif
3296
3297 /* Load ramdisk & kernel */
3298 memmove((void*) hdr->ramdisk_addr, ptr + page_size + kernel_actual, hdr->ramdisk_size);
Matthew Qinbb7923d2015-02-09 10:56:09 +08003299 memmove((void*) hdr->kernel_addr, (char*) (kernel_start_addr), kernel_size);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07003300
3301#if DEVICE_TREE
Mayank Grovere559cec2017-10-17 15:12:03 +05303302 if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual) ||
3303 check_ddr_addr_range_bound(hdr->tags_addr, kernel_actual))
Matthew Qinbb7923d2015-02-09 10:56:09 +08003304 {
Mayank Grovere559cec2017-10-17 15:12:03 +05303305 dprintf(CRITICAL, "Tags addresses are not valid.\n");
lijuang2008ff22016-03-07 17:56:27 +08003306 goto boot_failed;
Matthew Qinbb7923d2015-02-09 10:56:09 +08003307 }
3308
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07003309 /*
3310 * If dtb is not found look for appended DTB in the kernel.
3311 * If appended dev tree is found, update the atags with
3312 * memory address to the DTB appended location on RAM.
3313 * Else update with the atags address in the kernel header
3314 */
3315 if (!dtb_copied) {
3316 void *dtb;
Matthew Qinbb7923d2015-02-09 10:56:09 +08003317 dtb = dev_tree_appended((void*)(ptr + page_size),
3318 hdr->kernel_size, dtb_offset,
Dima Zavine63e5572013-05-03 12:23:06 -07003319 (void *)hdr->tags_addr);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07003320 if (!dtb) {
3321 fastboot_fail("dtb not found");
lijuang2008ff22016-03-07 17:56:27 +08003322 goto boot_failed;
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07003323 }
Amol Jadicb524072012-08-09 16:40:18 -07003324 }
3325#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08003326
3327 fastboot_okay("");
Amol Jadi7c4316c2013-10-07 14:19:26 -07003328 fastboot_stop();
Brian Swetland9c4c0752009-01-25 16:23:50 -08003329
Dima Zavin77e41f32013-03-06 16:10:43 -08003330 boot_linux((void*) hdr->kernel_addr, (void*) hdr->tags_addr,
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07003331 (const char*) hdr->cmdline, board_machtype(),
3332 (void*) hdr->ramdisk_addr, hdr->ramdisk_size);
lijuang2008ff22016-03-07 17:56:27 +08003333
3334 /* fastboot already stop, it's no need to show fastboot menu */
3335 return;
3336boot_failed:
3337#if FBCON_DISPLAY_MSG
3338 /* revert to fastboot menu if boot failed */
3339 display_fastboot_menu();
3340#endif
3341 return;
Brian Swetland9c4c0752009-01-25 16:23:50 -08003342}
3343
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003344void cmd_erase_nand(const char *arg, void *data, unsigned sz)
Dima Zavin214cc642009-01-26 11:16:21 -08003345{
3346 struct ptentry *ptn;
3347 struct ptable *ptable;
3348
3349 ptable = flash_get_ptable();
3350 if (ptable == NULL) {
3351 fastboot_fail("partition table doesn't exist");
3352 return;
3353 }
3354
3355 ptn = ptable_find(ptable, arg);
3356 if (ptn == NULL) {
3357 fastboot_fail("unknown partition name");
3358 return;
3359 }
3360
Monika Singh292b3e92018-03-17 22:40:23 +05303361 if (!strncmp(arg, "avb_custom_key", strlen("avb_custom_key"))) {
3362 dprintf(INFO, "erasing avb_custom_key\n");
3363 if (erase_userkey()) {
3364 fastboot_fail("Erasing avb_custom_key failed");
3365 } else {
3366 fastboot_okay("");
3367 }
3368 return;
3369 }
3370
Dima Zavin214cc642009-01-26 11:16:21 -08003371 if (flash_erase(ptn)) {
3372 fastboot_fail("failed to erase partition");
3373 return;
3374 }
3375 fastboot_okay("");
3376}
3377
Bikas Gurungd48bd242010-09-04 19:54:32 -07003378
3379void cmd_erase_mmc(const char *arg, void *data, unsigned sz)
3380{
3381 unsigned long long ptn = 0;
Oliver Wangcee448d2013-10-22 18:40:13 +08003382 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07003383 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003384 uint8_t lun = 0;
Vijay Kumar Pendoti0b21f462016-05-02 17:09:18 +05303385 char *footer = NULL;
Bikas Gurungd48bd242010-09-04 19:54:32 -07003386
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003387#if VERIFIED_BOOT
3388 if(!strcmp(arg, KEYSTORE_PTN_NAME))
3389 {
3390 if(!device.is_unlocked)
3391 {
3392 fastboot_fail("unlock device to erase keystore");
3393 return;
3394 }
3395 }
3396#endif
3397
Kinson Chikf1a43512011-07-14 11:28:39 -07003398 index = partition_get_index(arg);
3399 ptn = partition_get_offset(index);
Oliver Wangcee448d2013-10-22 18:40:13 +08003400 size = partition_get_size(index);
Neeti Desaica8c9602011-10-06 11:40:00 -07003401
Monika Singhc4778b72018-05-16 11:16:42 +05303402 if (!strncmp(arg, "avb_custom_key", strlen("avb_custom_key"))) {
3403 dprintf(INFO, "erasing avb_custom_key\n");
3404 if (erase_userkey()) {
3405 fastboot_fail("Erasing avb_custom_key failed");
3406 } else {
3407 fastboot_okay("");
3408 }
3409 return;
3410 }
3411
Kinson Chikf1a43512011-07-14 11:28:39 -07003412 if(ptn == 0) {
Neeti Desaica8c9602011-10-06 11:40:00 -07003413 fastboot_fail("Partition table doesn't exist\n");
Bikas Gurungd48bd242010-09-04 19:54:32 -07003414 return;
3415 }
Kun Liang2f1601a2013-08-12 16:29:54 +08003416
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003417 lun = partition_get_lun(index);
3418 mmc_set_lun(lun);
3419
Sridhar Parasuramd7957122015-02-27 11:33:40 -08003420 if (platform_boot_dev_isemmc())
3421 {
3422 if (mmc_erase_card(ptn, size)) {
3423 fastboot_fail("failed to erase partition\n");
3424 return;
3425 }
3426 } else {
3427 BUF_DMA_ALIGN(out, DEFAULT_ERASE_SIZE);
3428 size = partition_get_size(index);
3429 if (size > DEFAULT_ERASE_SIZE)
3430 size = DEFAULT_ERASE_SIZE;
Kun Liang2f1601a2013-08-12 16:29:54 +08003431
Sridhar Parasuramd7957122015-02-27 11:33:40 -08003432 /* Simple inefficient version of erase. Just writing
3433 0 in first several blocks */
3434 if (mmc_write(ptn , size, (unsigned int *)out)) {
3435 fastboot_fail("failed to erase partition");
3436 return;
3437 }
Vijay Kumar Pendoti0b21f462016-05-02 17:09:18 +05303438 /*Erase FDE metadata at the userdata footer*/
3439 if(!(strncmp(arg, "userdata", 8)))
3440 {
3441 footer = memalign(CACHE_LINE, FOOTER_SIZE);
3442 memset((void *)footer, 0, FOOTER_SIZE);
3443
3444 size = partition_get_size(index);
3445
3446 if (mmc_write((ptn + size) - FOOTER_SIZE , FOOTER_SIZE, (unsigned int *)footer)) {
3447 fastboot_fail("failed to erase userdata footer");
3448 free(footer);
3449 return;
3450 }
3451 free(footer);
3452 }
Bikas Gurungd48bd242010-09-04 19:54:32 -07003453 }
Monika Singh292b3e92018-03-17 22:40:23 +05303454#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover912eaa62017-10-26 12:08:53 +05303455 if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05303456 !(strncmp(arg, "userdata", 8)) &&
3457 send_delete_keys_to_tz())
Sridhar Parasuram32b30662015-07-10 13:33:22 -07003458 ASSERT(0);
3459#endif
Bikas Gurungd48bd242010-09-04 19:54:32 -07003460 fastboot_okay("");
3461}
3462
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003463void cmd_erase(const char *arg, void *data, unsigned sz)
3464{
Monika Singh292b3e92018-03-17 22:40:23 +05303465#if VERIFIED_BOOT || VERIFIED_BOOT_2
Channagoud Kadabi35297672015-06-13 11:09:49 -07003466 if (target_build_variant_user())
Channagoud Kadabice3cf422015-04-17 16:02:30 -07003467 {
Sridhar Parasuramc32d07d2015-07-12 10:57:48 -07003468 if(!device.is_unlocked)
Channagoud Kadabice3cf422015-04-17 16:02:30 -07003469 {
Channagoud Kadabi35297672015-06-13 11:09:49 -07003470 fastboot_fail("device is locked. Cannot erase");
Channagoud Kadabice3cf422015-04-17 16:02:30 -07003471 return;
3472 }
3473 }
3474#endif
3475
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003476 if(target_is_emmc_boot())
3477 cmd_erase_mmc(arg, data, sz);
3478 else
3479 cmd_erase_nand(arg, data, sz);
3480}
Bikas Gurungd48bd242010-09-04 19:54:32 -07003481
Mayank Grover11ff9692018-01-11 11:54:49 +05303482/* Get the size from partiton name */
3483static void get_partition_size(const char *arg, char *response)
3484{
3485 uint64_t ptn = 0;
3486 uint64_t size;
3487 int index = INVALID_PTN;
3488
3489 index = partition_get_index(arg);
3490
3491 if (index == INVALID_PTN)
3492 {
3493 dprintf(CRITICAL, "Invalid partition index\n");
3494 return;
3495 }
3496
3497 ptn = partition_get_offset(index);
3498
3499 if(!ptn)
3500 {
3501 dprintf(CRITICAL, "Invalid partition name %s\n", arg);
3502 return;
3503 }
3504
3505 size = partition_get_size(index);
3506
3507 snprintf(response, MAX_RSP_SIZE, "\t 0x%llx", size);
3508 return;
3509}
3510
Mayank Grover52cd10a2018-03-15 12:57:54 +05303511/* Function to check partition type of a partition*/
3512static fs_signature_type
3513check_partition_fs_signature(const char *arg)
3514{
3515 fs_signature_type ret = NO_FS;
3516 int index;
3517 unsigned long long ptn;
Mayank Grover399826a2018-08-27 12:15:15 +05303518 char *buffer = memalign(CACHE_LINE, mmc_blocksize);
3519 uint32_t sb_blk_offset = 0;
3520 char *sb_buffer = buffer;
3521
Mayank Grover52cd10a2018-03-15 12:57:54 +05303522 if (!sb_buffer)
3523 {
3524 dprintf(CRITICAL, "ERROR: Failed to allocate buffer for superblock\n");
3525 goto out;
3526 }
3527
3528 /* Read super block */
3529 if ((index = partition_get_index(arg)) < 0)
3530 {
3531 dprintf(CRITICAL, "ERROR: %s() doesn't exsit\n", arg);
3532 goto out;
3533 }
3534 ptn = partition_get_offset(index);
3535 mmc_set_lun(partition_get_lun(index));
Mayank Grover399826a2018-08-27 12:15:15 +05303536 sb_blk_offset = (FS_SUPERBLOCK_OFFSET/mmc_blocksize);
3537
3538 if(mmc_read(ptn + (sb_blk_offset * mmc_blocksize),
Mayank Grover52cd10a2018-03-15 12:57:54 +05303539 (void *)sb_buffer, mmc_blocksize))
3540 {
3541 dprintf(CRITICAL, "ERROR: Failed to read Superblock\n");
3542 goto out;
3543 }
3544
Mayank Grover399826a2018-08-27 12:15:15 +05303545 if (sb_blk_offset == 0)
3546 sb_buffer += FS_SUPERBLOCK_OFFSET;
3547
3548 if (*((uint16 *)(&sb_buffer[EXT_MAGIC_OFFSET_SB])) == (uint16)EXT_MAGIC)
Mayank Grover52cd10a2018-03-15 12:57:54 +05303549 {
3550 dprintf(SPEW, "%s() Found EXT FS\n", arg);
3551 ret = EXT_FS_SIGNATURE;
3552 }
Mayank Grover399826a2018-08-27 12:15:15 +05303553 else if (*((uint32 *)(&sb_buffer[F2FS_MAGIC_OFFSET_SB])) == F2FS_MAGIC)
Mayank Grover52cd10a2018-03-15 12:57:54 +05303554 {
3555 dprintf(SPEW, "%s() Found F2FS FS\n", arg);
3556 ret = EXT_F2FS_SIGNATURE;
3557 }
3558 else
3559 {
3560 dprintf(SPEW, "%s() Reverting to default 0x%x\n",
3561 arg, *((uint16 *)(&sb_buffer[EXT_MAGIC_OFFSET_SB])));
3562 ret = NO_FS;
3563 }
3564
3565out:
Mayank Grover399826a2018-08-27 12:15:15 +05303566 if(buffer)
3567 free(buffer);
Mayank Grover52cd10a2018-03-15 12:57:54 +05303568 return ret;
3569}
3570
Mayank Groverd38fe012018-03-13 15:33:16 +05303571/* Function to get partition type */
3572static void get_partition_type(const char *arg, char *response)
3573{
3574 uint n = 0;
Mayank Grover52cd10a2018-03-15 12:57:54 +05303575 fs_signature_type fs_signature;
Mayank Groverd38fe012018-03-13 15:33:16 +05303576
3577 if (arg == NULL ||
3578 response == NULL)
3579 {
3580 dprintf(CRITICAL, "Invalid input parameter\n");
3581 return;
3582 }
3583
3584 /* By default copy raw to response */
Mayank Grover0e559042018-04-11 17:49:30 +05303585 strlcpy(response, RAW_STR, MAX_RSP_SIZE);
Mayank Groverd38fe012018-03-13 15:33:16 +05303586
3587 /* Mark partiton type for known paritions only */
3588 for (n=0; n < ARRAY_SIZE(part_type_known); n++)
3589 {
3590 if (!strncmp(part_type_known[n].part_name, arg, strlen(arg)))
3591 {
Mayank Grover52cd10a2018-03-15 12:57:54 +05303592 /* Check partition for FS signature */
3593 fs_signature = check_partition_fs_signature(arg);
3594 switch (fs_signature)
3595 {
3596 case EXT_FS_SIGNATURE:
Mayank Grover0e559042018-04-11 17:49:30 +05303597 strlcpy(response, EXT_STR, MAX_RSP_SIZE);
Mayank Grover52cd10a2018-03-15 12:57:54 +05303598 break;
3599 case EXT_F2FS_SIGNATURE:
Mayank Grover0e559042018-04-11 17:49:30 +05303600 strlcpy(response, F2FS_STR, MAX_RSP_SIZE);
Mayank Grover52cd10a2018-03-15 12:57:54 +05303601 break;
3602 case NO_FS:
Mayank Grover0e559042018-04-11 17:49:30 +05303603 strlcpy(response, part_type_known[n].type_response, MAX_RSP_SIZE);
Mayank Grover52cd10a2018-03-15 12:57:54 +05303604 }
Mayank Groverd38fe012018-03-13 15:33:16 +05303605 }
3606 }
3607 return;
3608}
3609
Mayank Grover11ff9692018-01-11 11:54:49 +05303610/*
3611 * Publish the partition type & size info
3612 * fastboot getvar will publish the required information.
3613 * fastboot getvar partition_size:<partition_name>: partition size in hex
3614 * fastboot getvar partition_type:<partition_name>: partition type (ext/fat)
3615 */
3616static void publish_getvar_partition_info(struct getvar_partition_info *info, uint8_t num_parts)
3617{
Mayank Groverd38fe012018-03-13 15:33:16 +05303618 uint8_t i;
Mayank Grover11ff9692018-01-11 11:54:49 +05303619 static bool published = false;
3620 struct partition_entry *ptn_entry =
3621 partition_get_partition_entries();
3622 memset(info, 0, sizeof(struct getvar_partition_info)* num_parts);
3623
3624 for (i = 0; i < num_parts; i++) {
3625 strlcat(info[i].part_name, (const char *)ptn_entry[i].name, MAX_RSP_SIZE);
3626 strlcat(info[i].getvar_size, "partition-size:", MAX_GET_VAR_NAME_SIZE);
3627 strlcat(info[i].getvar_type, "partition-type:", MAX_GET_VAR_NAME_SIZE);
3628
Mayank Groverd38fe012018-03-13 15:33:16 +05303629 get_partition_type(info[i].part_name, info[i].type_response);
Mayank Grover11ff9692018-01-11 11:54:49 +05303630 get_partition_size(info[i].part_name, info[i].size_response);
Mayank Grover11ff9692018-01-11 11:54:49 +05303631 if (strlcat(info[i].getvar_size, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
3632 {
3633 dprintf(CRITICAL, "partition size name truncated\n");
3634 return;
3635 }
3636 if (strlcat(info[i].getvar_type, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
3637 {
3638 dprintf(CRITICAL, "partition type name truncated\n");
3639 return;
3640 }
3641
3642 if (!published)
3643 {
3644 /* publish partition size & type info */
3645 fastboot_publish((const char *) info[i].getvar_size, (const char *) info[i].size_response);
3646 fastboot_publish((const char *) info[i].getvar_type, (const char *) info[i].type_response);
3647 }
3648 }
3649 if (!published)
3650 published = true;
3651}
3652
3653
Ajay Dudani5c761132011-04-07 20:19:04 -07003654void cmd_flash_mmc_img(const char *arg, void *data, unsigned sz)
Shashank Mittal23b8f422010-04-16 19:27:21 -07003655{
3656 unsigned long long ptn = 0;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07003657 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07003658 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003659 char *token = NULL;
3660 char *pname = NULL;
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07003661 char *sp;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003662 uint8_t lun = 0;
3663 bool lun_set = false;
Mayank Grover351a75e2017-05-30 20:06:08 +05303664 int current_active_slot = INVALID;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07003665
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07003666 token = strtok_r((char *)arg, ":", &sp);
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003667 pname = token;
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07003668 token = strtok_r(NULL, ":", &sp);
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003669 if(token)
3670 {
3671 lun = atoi(token);
3672 mmc_set_lun(lun);
3673 lun_set = true;
3674 }
3675
Mao Jinlong226f33a2014-07-04 17:24:10 +08003676 if (pname)
Greg Grisco6e754772011-06-23 12:19:39 -07003677 {
Channagoud Kadabiad259832015-05-29 11:14:17 -07003678 if (!strncmp(pname, "frp-unlock", strlen("frp-unlock")))
3679 {
3680 if (!aboot_frp_unlock(pname, data, sz))
3681 {
3682 fastboot_info("FRP unlock successful");
3683 fastboot_okay("");
3684 }
3685 else
3686 fastboot_fail("Secret key is invalid, please update the bootloader with secret key");
3687
3688 return;
3689 }
3690
Mao Jinlong226f33a2014-07-04 17:24:10 +08003691 if (!strcmp(pname, "partition"))
3692 {
3693 dprintf(INFO, "Attempt to write partition image.\n");
3694 if (write_partition(sz, (unsigned char *) data)) {
3695 fastboot_fail("failed to write partition");
Greg Grisco6e754772011-06-23 12:19:39 -07003696 return;
3697 }
Mayank Grover11ff9692018-01-11 11:54:49 +05303698 /* Re-publish partition table */
3699 publish_getvar_partition_info(part_info, partition_get_partition_count());
Mayank Grover351a75e2017-05-30 20:06:08 +05303700
3701 /* Rescan partition table to ensure we have multislot support*/
3702 if (partition_scan_for_multislot())
3703 {
3704 current_active_slot = partition_find_active_slot();
3705 dprintf(INFO, "Multislot supported: Slot %s active",
3706 (SUFFIX_SLOT(current_active_slot)));
3707 }
3708 partition_mark_active_slot(current_active_slot);
Greg Grisco6e754772011-06-23 12:19:39 -07003709 }
Mao Jinlong226f33a2014-07-04 17:24:10 +08003710 else
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003711 {
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003712#if VERIFIED_BOOT
3713 if(!strcmp(pname, KEYSTORE_PTN_NAME))
3714 {
3715 if(!device.is_unlocked)
3716 {
3717 fastboot_fail("unlock device to flash keystore");
3718 return;
3719 }
Gaurav Nebhwanic7313cc2015-12-15 22:25:04 +05303720 if(!boot_verify_validate_keystore((unsigned char *)data,sz))
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003721 {
3722 fastboot_fail("image is not a keystore file");
3723 return;
3724 }
3725 }
3726#endif
Mao Jinlong226f33a2014-07-04 17:24:10 +08003727 index = partition_get_index(pname);
3728 ptn = partition_get_offset(index);
3729 if(ptn == 0) {
3730 fastboot_fail("partition table doesn't exist");
3731 return;
3732 }
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003733
Mayank Grover351a75e2017-05-30 20:06:08 +05303734 if (!strncmp(pname, "boot", strlen("boot"))
3735 || !strcmp(pname, "recovery"))
3736 {
Mao Jinlong226f33a2014-07-04 17:24:10 +08003737 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
3738 fastboot_fail("image is not a boot image");
3739 return;
3740 }
Mayank Grover351a75e2017-05-30 20:06:08 +05303741
3742 /* Reset multislot_partition attributes in case of flashing boot */
3743 if (partition_multislot_is_supported())
3744 {
3745 partition_reset_attributes(index);
3746 }
Mao Jinlong226f33a2014-07-04 17:24:10 +08003747 }
3748
3749 if(!lun_set)
3750 {
3751 lun = partition_get_lun(index);
3752 mmc_set_lun(lun);
3753 }
3754
3755 size = partition_get_size(index);
Gaurav Nebhwani4d2389c2016-03-17 21:10:05 +05303756 if (ROUND_TO_PAGE(sz, mmc_blocksize_mask) > size) {
Mao Jinlong226f33a2014-07-04 17:24:10 +08003757 fastboot_fail("size too large");
3758 return;
3759 }
3760 else if (mmc_write(ptn , sz, (unsigned int *)data)) {
3761 fastboot_fail("flash write failure");
3762 return;
3763 }
Greg Grisco6e754772011-06-23 12:19:39 -07003764 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07003765 }
3766 fastboot_okay("");
3767 return;
3768}
3769
Ajay Dudanide984792015-03-02 09:57:41 -08003770void cmd_flash_meta_img(const char *arg, void *data, unsigned sz)
3771{
3772 int i, images;
3773 meta_header_t *meta_header;
3774 img_header_entry_t *img_header_entry;
Parth Dixit3e2d3032016-03-04 17:11:52 +05303775 /*End of the image address*/
3776 uintptr_t data_end;
3777
3778 if( (UINT_MAX - sz) > (uintptr_t)data )
3779 data_end = (uintptr_t)data + sz;
3780 else
3781 {
3782 fastboot_fail("Cannot flash: image header corrupt");
3783 return;
3784 }
3785
3786 if( data_end < ((uintptr_t)data + sizeof(meta_header_t)))
3787 {
3788 fastboot_fail("Cannot flash: image header corrupt");
3789 return;
3790 }
Ajay Dudanide984792015-03-02 09:57:41 -08003791
lijuang8ee21882016-04-19 16:57:11 +08003792 /* If device is locked:
3793 * Forbid to flash image to avoid the device to bypass the image
3794 * which with "any" name other than bootloader. Because it maybe
3795 * a meta package of all partitions.
3796 */
Monika Singh292b3e92018-03-17 22:40:23 +05303797#if VERIFIED_BOOT || VERIFIED_BOOT_2
lijuang8ee21882016-04-19 16:57:11 +08003798 if (target_build_variant_user()) {
3799 if (!device.is_unlocked) {
3800 fastboot_fail("Device is locked, meta image flashing is not allowed");
3801 return;
3802 }
Mayank Grover889be1b2017-09-12 20:12:23 +05303803
Mayank Grover912eaa62017-10-26 12:08:53 +05303804 if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05303805 !device.is_unlock_critical)
3806 {
lijuang8ee21882016-04-19 16:57:11 +08003807 fastboot_fail("Device is critical locked, Meta image flashing is not allowed");
3808 return;
3809 }
lijuang8ee21882016-04-19 16:57:11 +08003810 }
3811#endif
3812
Ajay Dudanide984792015-03-02 09:57:41 -08003813 meta_header = (meta_header_t*) data;
Parth Dixit3e2d3032016-03-04 17:11:52 +05303814 if( data_end < ((uintptr_t)data + meta_header->img_hdr_sz))
3815 {
3816 fastboot_fail("Cannot flash: image header corrupt");
3817 return;
3818 }
Ajay Dudanide984792015-03-02 09:57:41 -08003819 img_header_entry = (img_header_entry_t*) (data+sizeof(meta_header_t));
3820
3821 images = meta_header->img_hdr_sz / sizeof(img_header_entry_t);
3822
3823 for (i=0; i<images; i++) {
3824
3825 if((img_header_entry[i].ptn_name == NULL) ||
3826 (img_header_entry[i].start_offset == 0) ||
3827 (img_header_entry[i].size == 0))
3828 break;
Kishor PK49831502017-04-21 17:55:43 +05303829 if ((UINT_MAX - img_header_entry[i].start_offset) < (uintptr_t)data) {
3830 fastboot_fail("Integer overflow detected in start_offset of img");
3831 break;
3832 }
3833 else if ((UINT_MAX - (img_header_entry[i].start_offset + (uintptr_t)data)) < img_header_entry[i].size) {
3834 fastboot_fail("Integer overflow detected in size of img");
3835 break;
3836 }
Parth Dixit3e2d3032016-03-04 17:11:52 +05303837 if( data_end < ((uintptr_t)data + img_header_entry[i].start_offset
3838 + img_header_entry[i].size) )
3839 {
3840 fastboot_fail("Cannot flash: image size mismatch");
3841 break;
3842 }
3843
Ajay Dudanide984792015-03-02 09:57:41 -08003844 cmd_flash_mmc_img(img_header_entry[i].ptn_name,
3845 (void *) data + img_header_entry[i].start_offset,
3846 img_header_entry[i].size);
3847 }
3848
Channagoud Kadabiaafbbfc2015-03-04 17:52:56 -08003849 if (!strncmp(arg, "bootloader", strlen("bootloader")))
3850 {
3851 strlcpy(device.bootloader_version, TARGET(BOARD), MAX_VERSION_LEN);
3852 strlcat(device.bootloader_version, "-", MAX_VERSION_LEN);
3853 strlcat(device.bootloader_version, meta_header->img_version, MAX_VERSION_LEN);
3854 }
3855 else
3856 {
3857 strlcpy(device.radio_version, TARGET(BOARD), MAX_VERSION_LEN);
3858 strlcat(device.radio_version, "-", MAX_VERSION_LEN);
3859 strlcat(device.radio_version, meta_header->img_version, MAX_VERSION_LEN);
3860 }
3861
3862 write_device_info(&device);
Ajay Dudanide984792015-03-02 09:57:41 -08003863 fastboot_okay("");
3864 return;
3865}
3866
Ajay Dudani5c761132011-04-07 20:19:04 -07003867void cmd_flash_mmc_sparse_img(const char *arg, void *data, unsigned sz)
3868{
3869 unsigned int chunk;
wufeng.jiang813dc352015-06-02 23:02:46 -04003870 uint64_t chunk_data_sz;
Channagoud Kadabif627cf82013-09-09 14:08:20 -07003871 uint32_t *fill_buf = NULL;
3872 uint32_t fill_val;
Mayank Grover4f50bba2017-07-19 18:17:08 +05303873 uint32_t blk_sz_actual = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07003874 sparse_header_t *sparse_header;
3875 chunk_header_t *chunk_header;
Ajay Dudaniab18f022011-05-12 14:39:22 -07003876 uint32_t total_blocks = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07003877 unsigned long long ptn = 0;
Channagoud Kadabi65b91002011-10-11 17:34:33 +05303878 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07003879 int index = INVALID_PTN;
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05303880 uint32_t i;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003881 uint8_t lun = 0;
vijay kumar800255e2015-04-24 20:26:19 +05303882 /*End of the sparse image address*/
Parth Dixit64b1a482016-03-07 16:31:26 +05303883 uintptr_t data_end = (uintptr_t)data + sz;
Ajay Dudani5c761132011-04-07 20:19:04 -07003884
Kinson Chikf1a43512011-07-14 11:28:39 -07003885 index = partition_get_index(arg);
3886 ptn = partition_get_offset(index);
3887 if(ptn == 0) {
Ajay Dudani5c761132011-04-07 20:19:04 -07003888 fastboot_fail("partition table doesn't exist");
3889 return;
3890 }
3891
Channagoud Kadabi65b91002011-10-11 17:34:33 +05303892 size = partition_get_size(index);
Channagoud Kadabi65b91002011-10-11 17:34:33 +05303893
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003894 lun = partition_get_lun(index);
3895 mmc_set_lun(lun);
3896
vijay kumar800255e2015-04-24 20:26:19 +05303897 if (sz < sizeof(sparse_header_t)) {
3898 fastboot_fail("size too low");
3899 return;
3900 }
3901
Ajay Dudani5c761132011-04-07 20:19:04 -07003902 /* Read and skip over sparse image header */
3903 sparse_header = (sparse_header_t *) data;
vijay kumar800255e2015-04-24 20:26:19 +05303904
Mayank Grover48bd9bb2017-07-19 12:04:16 +05303905 if (!sparse_header->blk_sz || (sparse_header->blk_sz % 4)){
3906 fastboot_fail("Invalid block size\n");
3907 return;
3908 }
3909
vijay kumar1321f342015-03-27 12:13:42 +05303910 if (((uint64_t)sparse_header->total_blks * (uint64_t)sparse_header->blk_sz) > size) {
Ajay Dudani876b3282012-12-21 14:12:17 -08003911 fastboot_fail("size too large");
3912 return;
3913 }
3914
vijay kumarde4fcf62015-04-23 13:05:49 +05303915 data += sizeof(sparse_header_t);
vijay kumar800255e2015-04-24 20:26:19 +05303916
Parth Dixit64b1a482016-03-07 16:31:26 +05303917 if (data_end < (uintptr_t)data) {
vijay kumar800255e2015-04-24 20:26:19 +05303918 fastboot_fail("buffer overreads occured due to invalid sparse header");
3919 return;
3920 }
3921
vijay kumarde4fcf62015-04-23 13:05:49 +05303922 if(sparse_header->file_hdr_sz != sizeof(sparse_header_t))
Ajay Dudani5c761132011-04-07 20:19:04 -07003923 {
vijay kumarde4fcf62015-04-23 13:05:49 +05303924 fastboot_fail("sparse header size mismatch");
3925 return;
Ajay Dudani5c761132011-04-07 20:19:04 -07003926 }
3927
Ajay Dudanib06c05f2011-05-12 14:46:10 -07003928 dprintf (SPEW, "=== Sparse Image Header ===\n");
3929 dprintf (SPEW, "magic: 0x%x\n", sparse_header->magic);
3930 dprintf (SPEW, "major_version: 0x%x\n", sparse_header->major_version);
3931 dprintf (SPEW, "minor_version: 0x%x\n", sparse_header->minor_version);
3932 dprintf (SPEW, "file_hdr_sz: %d\n", sparse_header->file_hdr_sz);
3933 dprintf (SPEW, "chunk_hdr_sz: %d\n", sparse_header->chunk_hdr_sz);
3934 dprintf (SPEW, "blk_sz: %d\n", sparse_header->blk_sz);
3935 dprintf (SPEW, "total_blks: %d\n", sparse_header->total_blks);
3936 dprintf (SPEW, "total_chunks: %d\n", sparse_header->total_chunks);
Ajay Dudani5c761132011-04-07 20:19:04 -07003937
3938 /* Start processing chunks */
3939 for (chunk=0; chunk<sparse_header->total_chunks; chunk++)
3940 {
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05303941 /* Make sure the total image size does not exceed the partition size */
3942 if(((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz) >= size) {
3943 fastboot_fail("size too large");
3944 return;
3945 }
Ajay Dudani5c761132011-04-07 20:19:04 -07003946 /* Read and skip over chunk header */
3947 chunk_header = (chunk_header_t *) data;
3948 data += sizeof(chunk_header_t);
3949
Parth Dixit64b1a482016-03-07 16:31:26 +05303950 if (data_end < (uintptr_t)data) {
vijay kumar800255e2015-04-24 20:26:19 +05303951 fastboot_fail("buffer overreads occured due to invalid sparse header");
3952 return;
3953 }
3954
Ajay Dudani5c761132011-04-07 20:19:04 -07003955 dprintf (SPEW, "=== Chunk Header ===\n");
3956 dprintf (SPEW, "chunk_type: 0x%x\n", chunk_header->chunk_type);
3957 dprintf (SPEW, "chunk_data_sz: 0x%x\n", chunk_header->chunk_sz);
3958 dprintf (SPEW, "total_size: 0x%x\n", chunk_header->total_sz);
3959
vijay kumar800255e2015-04-24 20:26:19 +05303960 if(sparse_header->chunk_hdr_sz != sizeof(chunk_header_t))
Ajay Dudani5c761132011-04-07 20:19:04 -07003961 {
vijay kumar800255e2015-04-24 20:26:19 +05303962 fastboot_fail("chunk header size mismatch");
3963 return;
Ajay Dudani5c761132011-04-07 20:19:04 -07003964 }
3965
wufeng.jiang813dc352015-06-02 23:02:46 -04003966 chunk_data_sz = (uint64_t)sparse_header->blk_sz * chunk_header->chunk_sz;
3967
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05303968 /* Make sure that the chunk size calculated from sparse image does not
3969 * exceed partition size
3970 */
3971 if ((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz + chunk_data_sz > size)
3972 {
3973 fastboot_fail("Chunk data size exceeds partition size");
3974 return;
3975 }
3976
Ajay Dudani5c761132011-04-07 20:19:04 -07003977 switch (chunk_header->chunk_type)
3978 {
3979 case CHUNK_TYPE_RAW:
wufeng.jiang813dc352015-06-02 23:02:46 -04003980 if((uint64_t)chunk_header->total_sz != ((uint64_t)sparse_header->chunk_hdr_sz +
Ajay Dudani5c761132011-04-07 20:19:04 -07003981 chunk_data_sz))
3982 {
3983 fastboot_fail("Bogus chunk size for chunk type Raw");
3984 return;
3985 }
3986
Parth Dixit64b1a482016-03-07 16:31:26 +05303987 if (data_end < (uintptr_t)data + chunk_data_sz) {
vijay kumar800255e2015-04-24 20:26:19 +05303988 fastboot_fail("buffer overreads occured due to invalid sparse header");
3989 return;
3990 }
3991
wufeng.jiang813dc352015-06-02 23:02:46 -04003992 /* chunk_header->total_sz is uint32,So chunk_data_sz is now less than 2^32
3993 otherwise it will return in the line above
3994 */
Ajay Dudaniab18f022011-05-12 14:39:22 -07003995 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
wufeng.jiang813dc352015-06-02 23:02:46 -04003996 (uint32_t)chunk_data_sz,
Ajay Dudaniab18f022011-05-12 14:39:22 -07003997 (unsigned int*)data))
Ajay Dudani5c761132011-04-07 20:19:04 -07003998 {
3999 fastboot_fail("flash write failure");
4000 return;
4001 }
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05304002 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
4003 fastboot_fail("Bogus size for RAW chunk type");
4004 return;
4005 }
Ajay Dudani5c761132011-04-07 20:19:04 -07004006 total_blocks += chunk_header->chunk_sz;
wufeng.jiang813dc352015-06-02 23:02:46 -04004007 data += (uint32_t)chunk_data_sz;
Ajay Dudani5c761132011-04-07 20:19:04 -07004008 break;
4009
Channagoud Kadabif627cf82013-09-09 14:08:20 -07004010 case CHUNK_TYPE_FILL:
4011 if(chunk_header->total_sz != (sparse_header->chunk_hdr_sz +
4012 sizeof(uint32_t)))
4013 {
4014 fastboot_fail("Bogus chunk size for chunk type FILL");
4015 return;
4016 }
4017
Mayank Grover4f50bba2017-07-19 18:17:08 +05304018 blk_sz_actual = ROUNDUP(sparse_header->blk_sz, CACHE_LINE);
4019 /* Integer overflow detected */
4020 if (blk_sz_actual < sparse_header->blk_sz)
4021 {
4022 fastboot_fail("Invalid block size");
4023 return;
4024 }
4025
4026 fill_buf = (uint32_t *)memalign(CACHE_LINE, blk_sz_actual);
Channagoud Kadabif627cf82013-09-09 14:08:20 -07004027 if (!fill_buf)
4028 {
4029 fastboot_fail("Malloc failed for: CHUNK_TYPE_FILL");
4030 return;
4031 }
4032
Parth Dixit64b1a482016-03-07 16:31:26 +05304033 if (data_end < (uintptr_t)data + sizeof(uint32_t)) {
vijay kumar800255e2015-04-24 20:26:19 +05304034 fastboot_fail("buffer overreads occured due to invalid sparse header");
Gaurav Nebhwani163cbf82016-03-17 21:21:27 +05304035 free(fill_buf);
vijay kumar800255e2015-04-24 20:26:19 +05304036 return;
4037 }
Channagoud Kadabif627cf82013-09-09 14:08:20 -07004038 fill_val = *(uint32_t *)data;
4039 data = (char *) data + sizeof(uint32_t);
Channagoud Kadabif627cf82013-09-09 14:08:20 -07004040
4041 for (i = 0; i < (sparse_header->blk_sz / sizeof(fill_val)); i++)
4042 {
4043 fill_buf[i] = fill_val;
4044 }
4045
Gaurav Nebhwanie94cbe12016-03-17 21:16:34 +05304046 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz))
4047 {
4048 fastboot_fail("bogus size for chunk FILL type");
4049 free(fill_buf);
4050 return;
4051 }
4052
wufeng.jiang813dc352015-06-02 23:02:46 -04004053 for (i = 0; i < chunk_header->chunk_sz; i++)
Channagoud Kadabif627cf82013-09-09 14:08:20 -07004054 {
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05304055 /* Make sure that the data written to partition does not exceed partition size */
4056 if ((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz + sparse_header->blk_sz > size)
4057 {
4058 fastboot_fail("Chunk data size for fill type exceeds partition size");
Gaurav Nebhwani163cbf82016-03-17 21:21:27 +05304059 free(fill_buf);
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05304060 return;
4061 }
4062
Channagoud Kadabif627cf82013-09-09 14:08:20 -07004063 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
4064 sparse_header->blk_sz,
4065 fill_buf))
4066 {
4067 fastboot_fail("flash write failure");
4068 free(fill_buf);
4069 return;
4070 }
4071
4072 total_blocks++;
4073 }
4074
4075 free(fill_buf);
4076 break;
4077
Ajay Dudani5c761132011-04-07 20:19:04 -07004078 case CHUNK_TYPE_DONT_CARE:
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05304079 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
4080 fastboot_fail("bogus size for chunk DONT CARE type");
4081 return;
4082 }
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07004083 total_blocks += chunk_header->chunk_sz;
4084 break;
4085
Ajay Dudani5c761132011-04-07 20:19:04 -07004086 case CHUNK_TYPE_CRC:
4087 if(chunk_header->total_sz != sparse_header->chunk_hdr_sz)
4088 {
wufeng.jiang813dc352015-06-02 23:02:46 -04004089 fastboot_fail("Bogus chunk size for chunk type CRC");
Ajay Dudani5c761132011-04-07 20:19:04 -07004090 return;
4091 }
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05304092 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
4093 fastboot_fail("bogus size for chunk CRC type");
4094 return;
4095 }
Ajay Dudani5c761132011-04-07 20:19:04 -07004096 total_blocks += chunk_header->chunk_sz;
Parth Dixit64b1a482016-03-07 16:31:26 +05304097 if ((uintptr_t)data > UINT_MAX - chunk_data_sz) {
vijay kumar800255e2015-04-24 20:26:19 +05304098 fastboot_fail("integer overflow occured");
4099 return;
4100 }
wufeng.jiang813dc352015-06-02 23:02:46 -04004101 data += (uint32_t)chunk_data_sz;
Parth Dixit64b1a482016-03-07 16:31:26 +05304102 if (data_end < (uintptr_t)data) {
vijay kumar800255e2015-04-24 20:26:19 +05304103 fastboot_fail("buffer overreads occured due to invalid sparse header");
4104 return;
4105 }
Ajay Dudani5c761132011-04-07 20:19:04 -07004106 break;
4107
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07004108 default:
Channagoud Kadabif627cf82013-09-09 14:08:20 -07004109 dprintf(CRITICAL, "Unkown chunk type: %x\n",chunk_header->chunk_type);
Ajay Dudani5c761132011-04-07 20:19:04 -07004110 fastboot_fail("Unknown chunk type");
4111 return;
4112 }
4113 }
4114
Ajay Dudani0c6927b2011-05-18 11:12:16 -07004115 dprintf(INFO, "Wrote %d blocks, expected to write %d blocks\n",
4116 total_blocks, sparse_header->total_blks);
4117
4118 if(total_blocks != sparse_header->total_blks)
4119 {
4120 fastboot_fail("sparse image write failure");
4121 }
Ajay Dudani5c761132011-04-07 20:19:04 -07004122
4123 fastboot_okay("");
4124 return;
4125}
4126
4127void cmd_flash_mmc(const char *arg, void *data, unsigned sz)
4128{
4129 sparse_header_t *sparse_header;
Ajay Dudanide984792015-03-02 09:57:41 -08004130 meta_header_t *meta_header;
Ajay Dudani5c761132011-04-07 20:19:04 -07004131
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07004132#ifdef SSD_ENABLE
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -08004133 /* 8 Byte Magic + 2048 Byte xml + Encrypted Data */
4134 unsigned int *magic_number = (unsigned int *) data;
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08004135 int ret=0;
4136 uint32 major_version=0;
4137 uint32 minor_version=0;
4138
4139 ret = scm_svc_version(&major_version,&minor_version);
4140 if(!ret)
4141 {
4142 if(major_version >= 2)
4143 {
Amir Samuelovbb65ce02013-05-05 12:20:18 +03004144 if( !strcmp(arg, "ssd") || !strcmp(arg, "tqs") )
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08004145 {
4146 ret = encrypt_scm((uint32 **) &data, &sz);
4147 if (ret != 0) {
4148 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
4149 return;
4150 }
4151
Amir Samuelovbb65ce02013-05-05 12:20:18 +03004152 /* Protect only for SSD */
4153 if (!strcmp(arg, "ssd")) {
4154 ret = scm_protect_keystore((uint32 *) data, sz);
4155 if (ret != 0) {
4156 dprintf(CRITICAL, "ERROR: scm_protect_keystore Failed\n");
4157 return;
4158 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08004159 }
4160 }
4161 else
4162 {
4163 ret = decrypt_scm_v2((uint32 **) &data, &sz);
4164 if(ret != 0)
4165 {
4166 dprintf(CRITICAL,"ERROR: Decryption Failure\n");
4167 return;
4168 }
4169 }
4170 }
4171 else
4172 {
4173 if (magic_number[0] == DECRYPT_MAGIC_0 &&
4174 magic_number[1] == DECRYPT_MAGIC_1)
4175 {
4176 ret = decrypt_scm((uint32 **) &data, &sz);
4177 if (ret != 0) {
4178 dprintf(CRITICAL, "ERROR: Invalid secure image\n");
4179 return;
4180 }
4181 }
4182 else if (magic_number[0] == ENCRYPT_MAGIC_0 &&
4183 magic_number[1] == ENCRYPT_MAGIC_1)
4184 {
4185 ret = encrypt_scm((uint32 **) &data, &sz);
4186 if (ret != 0) {
4187 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
4188 return;
4189 }
4190 }
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07004191 }
4192 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08004193 else
Neeti Desai127b9e02012-03-20 16:11:23 -07004194 {
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08004195 dprintf(CRITICAL,"INVALID SVC Version\n");
4196 return;
Neeti Desai127b9e02012-03-20 16:11:23 -07004197 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08004198#endif /* SSD_ENABLE */
Neeti Desai127b9e02012-03-20 16:11:23 -07004199
Monika Singh292b3e92018-03-17 22:40:23 +05304200#if VERIFIED_BOOT || VERIFIED_BOOT_2
Channagoud Kadabi35297672015-06-13 11:09:49 -07004201 if (target_build_variant_user())
Shashank Mittald3e54dd2014-08-28 15:24:02 -07004202 {
lijuang511a2b52015-08-14 20:50:51 +08004203 /* if device is locked:
4204 * common partition will not allow to be flashed
4205 * critical partition will allow to flash image.
4206 */
4207 if(!device.is_unlocked && !critical_flash_allowed(arg)) {
4208 fastboot_fail("Partition flashing is not allowed");
4209 return;
4210 }
Mayank Grover889be1b2017-09-12 20:12:23 +05304211
lijuang511a2b52015-08-14 20:50:51 +08004212 /* if device critical is locked:
4213 * common partition will allow to be flashed
4214 * critical partition will not allow to flash image.
4215 */
Mayank Grover912eaa62017-10-26 12:08:53 +05304216 if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05304217 !device.is_unlock_critical &&
4218 critical_flash_allowed(arg)) {
4219 fastboot_fail("Critical partition flashing is not allowed");
4220 return;
Shashank Mittald3e54dd2014-08-28 15:24:02 -07004221 }
4222 }
4223#endif
Monika Singh292b3e92018-03-17 22:40:23 +05304224 if (!strncmp(arg, "avb_custom_key", strlen("avb_custom_key"))) {
4225 dprintf(INFO, "flashing avb_custom_key\n");
4226 if (store_userkey(data, sz)) {
4227 fastboot_fail("Flashing avb_custom_key failed");
4228 } else {
4229 fastboot_okay("");
4230 }
4231 return;
4232 }
Shashank Mittald3e54dd2014-08-28 15:24:02 -07004233
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07004234 sparse_header = (sparse_header_t *) data;
Ajay Dudanide984792015-03-02 09:57:41 -08004235 meta_header = (meta_header_t *) data;
4236 if (sparse_header->magic == SPARSE_HEADER_MAGIC)
Ajay Dudani5c761132011-04-07 20:19:04 -07004237 cmd_flash_mmc_sparse_img(arg, data, sz);
Ajay Dudanide984792015-03-02 09:57:41 -08004238 else if (meta_header->magic == META_HEADER_MAGIC)
4239 cmd_flash_meta_img(arg, data, sz);
4240 else
4241 cmd_flash_mmc_img(arg, data, sz);
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07004242
4243#if VERIFIED_BOOT
Mayank Grover912eaa62017-10-26 12:08:53 +05304244 if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05304245 (!strncmp(arg, "system", 6)) &&
4246 !device.verity_mode)
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07004247 // reset dm_verity mode to enforcing
4248 device.verity_mode = 1;
4249 write_device_info(&device);
Parth Dixitddbc7352015-10-18 03:13:31 +05304250#endif
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07004251
Ajay Dudani5c761132011-04-07 20:19:04 -07004252 return;
4253}
4254
Tanya Brokhman84eaaf62015-02-24 21:57:06 +02004255void cmd_updatevol(const char *vol_name, void *data, unsigned sz)
4256{
4257 struct ptentry *sys_ptn;
4258 struct ptable *ptable;
4259
4260 ptable = flash_get_ptable();
4261 if (ptable == NULL) {
4262 fastboot_fail("partition table doesn't exist");
4263 return;
4264 }
4265
4266 sys_ptn = ptable_find(ptable, "system");
4267 if (sys_ptn == NULL) {
4268 fastboot_fail("system partition not found");
4269 return;
4270 }
4271
4272 sz = ROUND_TO_PAGE(sz, page_mask);
4273 if (update_ubi_vol(sys_ptn, vol_name, data, sz))
4274 fastboot_fail("update_ubi_vol failed");
4275 else
4276 fastboot_okay("");
4277}
4278
Sridhar Parasurame94e8152014-10-24 14:06:03 -07004279void cmd_flash_nand(const char *arg, void *data, unsigned sz)
Dima Zavin214cc642009-01-26 11:16:21 -08004280{
4281 struct ptentry *ptn;
4282 struct ptable *ptable;
4283 unsigned extra = 0;
Gaurav Nebhwanid9730712016-05-06 14:28:29 +05304284 uint64_t partition_size = 0;
Vijay Kumar Pendotic6f22a72017-05-19 22:44:34 +05304285 unsigned bytes_to_round_page = 0;
4286 unsigned rounded_size = 0;
Dima Zavin214cc642009-01-26 11:16:21 -08004287
Kishor PK38ed93d2017-04-25 14:19:26 +05304288 if((uintptr_t)data > (UINT_MAX - sz)) {
4289 fastboot_fail("Cannot flash: image header corrupt");
4290 return;
4291 }
4292
Dima Zavin214cc642009-01-26 11:16:21 -08004293 ptable = flash_get_ptable();
4294 if (ptable == NULL) {
4295 fastboot_fail("partition table doesn't exist");
4296 return;
4297 }
4298
4299 ptn = ptable_find(ptable, arg);
4300 if (ptn == NULL) {
Tanya Brokhman84eaaf62015-02-24 21:57:06 +02004301 dprintf(INFO, "unknown partition name (%s). Trying updatevol\n",
4302 arg);
4303 cmd_updatevol(arg, data, sz);
Dima Zavin214cc642009-01-26 11:16:21 -08004304 return;
4305 }
4306
Monika Singh292b3e92018-03-17 22:40:23 +05304307 if (!strncmp(arg, "avb_custom_key", strlen("avb_custom_key"))) {
4308 dprintf(INFO, "flashing avb_custom_key\n");
4309 if (store_userkey(data, sz)) {
4310 fastboot_fail("Flashing avb_custom_key failed");
4311 } else {
4312 fastboot_okay("");
4313 }
4314 return;
4315 }
4316
Dima Zavin214cc642009-01-26 11:16:21 -08004317 if (!strcmp(ptn->name, "boot") || !strcmp(ptn->name, "recovery")) {
Kishor PK38ed93d2017-04-25 14:19:26 +05304318 if((sz > BOOT_MAGIC_SIZE) && (!memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE))) {
4319 dprintf(INFO, "Verified the BOOT_MAGIC in image header \n");
4320 } else {
4321 fastboot_fail("Image is not a boot image");
Dima Zavin214cc642009-01-26 11:16:21 -08004322 return;
4323 }
4324 }
4325
Amol Jadi5c61a952012-05-04 17:05:35 -07004326 if (!strcmp(ptn->name, "system")
Deepa Dinamani13e32c42012-03-12 14:34:17 -07004327 || !strcmp(ptn->name, "userdata")
4328 || !strcmp(ptn->name, "persist")
Sundarajan Srinivasanb063a852013-11-19 14:02:27 -08004329 || !strcmp(ptn->name, "recoveryfs")
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -08004330 || !strcmp(ptn->name, "modem"))
Tanya Brokhman1c94f1a2015-02-15 09:05:03 +02004331 extra = 1;
Kishor PK38ed93d2017-04-25 14:19:26 +05304332 else {
Vijay Kumar Pendotic6f22a72017-05-19 22:44:34 +05304333 rounded_size = ROUNDUP(sz, page_size);
4334 bytes_to_round_page = rounded_size - sz;
4335 if (bytes_to_round_page) {
4336 if (((uintptr_t)data + sz ) > (UINT_MAX - bytes_to_round_page)) {
4337 fastboot_fail("Integer overflow detected");
4338 return;
4339 }
4340 if (((uintptr_t)data + sz + bytes_to_round_page) >
4341 ((uintptr_t)target_get_scratch_address() + target_get_max_flash_size())) {
4342 fastboot_fail("Buffer size is not aligned to page_size");
4343 return;
4344 }
4345 else {
4346 memset(data + sz, 0, bytes_to_round_page);
4347 sz = rounded_size;
4348 }
Kishor PK38ed93d2017-04-25 14:19:26 +05304349 }
Gaurav Nebhwanid9730712016-05-06 14:28:29 +05304350 }
4351
Kishor PK38ed93d2017-04-25 14:19:26 +05304352 /*Checking partition_size for the possible integer overflow */
4353 partition_size = validate_partition_size(ptn);
4354
Gaurav Nebhwanid9730712016-05-06 14:28:29 +05304355 if (sz > partition_size) {
4356 fastboot_fail("Image size too large");
4357 return;
4358 }
4359
Dima Zavin214cc642009-01-26 11:16:21 -08004360 dprintf(INFO, "writing %d bytes to '%s'\n", sz, ptn->name);
Mayank Grover6cde9352017-06-06 18:45:23 +05304361 if ((sz > UBI_EC_HDR_SIZE) &&
4362 (!memcmp((void *)data, UBI_MAGIC, UBI_MAGIC_SIZE))) {
Tanya Brokhman1c94f1a2015-02-15 09:05:03 +02004363 if (flash_ubi_img(ptn, data, sz)) {
4364 fastboot_fail("flash write failure");
4365 return;
4366 }
4367 } else {
4368 if (flash_write(ptn, extra, data, sz)) {
4369 fastboot_fail("flash write failure");
4370 return;
4371 }
Dima Zavin214cc642009-01-26 11:16:21 -08004372 }
4373 dprintf(INFO, "partition '%s' updated\n", ptn->name);
4374 fastboot_okay("");
4375}
4376
Kishor PK38ed93d2017-04-25 14:19:26 +05304377
4378static inline uint64_t validate_partition_size(struct ptentry *ptn)
4379{
4380 if (ptn->length && flash_num_pages_per_blk() && page_size) {
4381 if ((ptn->length < ( UINT_MAX / flash_num_pages_per_blk())) && ((ptn->length * flash_num_pages_per_blk()) < ( UINT_MAX / page_size))) {
4382 return ptn->length * flash_num_pages_per_blk() * page_size;
4383 }
4384 }
4385 return 0;
4386}
4387
4388
Sridhar Parasurame94e8152014-10-24 14:06:03 -07004389void cmd_flash(const char *arg, void *data, unsigned sz)
4390{
4391 if(target_is_emmc_boot())
4392 cmd_flash_mmc(arg, data, sz);
4393 else
4394 cmd_flash_nand(arg, data, sz);
4395}
4396
Dima Zavin214cc642009-01-26 11:16:21 -08004397void cmd_continue(const char *arg, void *data, unsigned sz)
4398{
4399 fastboot_okay("");
Amol Jadi7c4316c2013-10-07 14:19:26 -07004400 fastboot_stop();
Shashank Mittald3e54dd2014-08-28 15:24:02 -07004401
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004402 if (target_is_emmc_boot())
4403 {
lijuanga40d6302015-07-20 20:10:13 +08004404#if FBCON_DISPLAY_MSG
lijuangde34d502016-02-26 16:04:50 +08004405 /* Exit keys' detection thread firstly */
4406 exit_menu_keys_detection();
lijuanga40d6302015-07-20 20:10:13 +08004407#endif
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004408 boot_linux_from_mmc();
4409 }
4410 else
4411 {
4412 boot_linux_from_flash();
4413 }
Dima Zavin214cc642009-01-26 11:16:21 -08004414}
4415
Chandan Uddaraju94183c02010-01-15 15:13:59 -08004416void cmd_reboot(const char *arg, void *data, unsigned sz)
4417{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004418 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08004419 fastboot_okay("");
4420 reboot_device(0);
4421}
4422
Mayank Grover351a75e2017-05-30 20:06:08 +05304423void cmd_set_active(const char *arg, void *data, unsigned sz)
4424{
Mayank Grover6ccc1c92017-07-04 17:36:46 +05304425 char *p, *sp = NULL;
Mayank Grover351a75e2017-05-30 20:06:08 +05304426 unsigned i,current_active_slot;
4427 const char *current_slot_suffix;
4428
4429 if (!partition_multislot_is_supported())
4430 {
4431 fastboot_fail("Command not supported");
4432 return;
4433 }
4434
4435 if (arg)
4436 {
Mayank Grover6ccc1c92017-07-04 17:36:46 +05304437 p = strtok_r((char *)arg, ":", &sp);
Mayank Grover5eb5f692017-07-19 20:16:04 +05304438 if (p)
Mayank Grover351a75e2017-05-30 20:06:08 +05304439 {
4440 current_active_slot = partition_find_active_slot();
4441
4442 /* Check if trying to make curent slot active */
4443 current_slot_suffix = SUFFIX_SLOT(current_active_slot);
Mayank Grover6ccc1c92017-07-04 17:36:46 +05304444 current_slot_suffix = strtok_r((char *)current_slot_suffix,
4445 (char *)suffix_delimiter, &sp);
4446
Mayank Grover5eb5f692017-07-19 20:16:04 +05304447 if (current_slot_suffix &&
4448 !strncmp(p, current_slot_suffix, strlen(current_slot_suffix)))
Mayank Grover351a75e2017-05-30 20:06:08 +05304449 {
4450 fastboot_okay("Slot already set active");
4451 return;
4452 }
4453 else
4454 {
4455 for (i = 0; i < AB_SUPPORTED_SLOTS; i++)
4456 {
4457 current_slot_suffix = SUFFIX_SLOT(i);
Mayank Grover6ccc1c92017-07-04 17:36:46 +05304458 current_slot_suffix = strtok_r((char *)current_slot_suffix,
4459 (char *)suffix_delimiter, &sp);
Mayank Grover5eb5f692017-07-19 20:16:04 +05304460 if (current_slot_suffix &&
4461 !strncmp(p, current_slot_suffix, strlen(current_slot_suffix)))
Mayank Grover351a75e2017-05-30 20:06:08 +05304462 {
lijuang8b03e5f2019-07-12 18:16:19 +08004463 partition_switch_slots(current_active_slot, i, true);
Mayank Grover351a75e2017-05-30 20:06:08 +05304464 publish_getvar_multislot_vars();
4465 fastboot_okay("");
4466 return;
4467 }
4468 }
4469 }
4470 }
4471 }
4472 fastboot_fail("Invalid slot suffix.");
4473 return;
4474}
4475
Mayank Grover98c4c742019-04-25 17:21:37 +05304476#if DYNAMIC_PARTITION_SUPPORT
4477void cmd_reboot_fastboot(const char *arg, void *data, unsigned sz)
4478{
4479 dprintf(INFO, "rebooting the device - userspace fastboot\n");
4480 if (send_recovery_cmd(RECOVERY_BOOT_FASTBOOT_CMD)) {
4481 dprintf(CRITICAL, "ERROR: Failed to update recovery commands\n");
4482 fastboot_fail("Failed to update recovery command");
4483 return;
4484 }
4485 fastboot_okay("");
4486 reboot_device(REBOOT_MODE_UNKNOWN);
4487
4488 //shouldn't come here.
4489 dprintf(CRITICAL, "ERROR: Failed to reboot device\n");
4490 return;
4491}
4492
4493void cmd_reboot_recovery(const char *arg, void *data, unsigned sz)
4494{
4495 dprintf(INFO, "rebooting the device - recovery\n");
4496 if (send_recovery_cmd(RECOVERY_BOOT_RECOVERY_CMD)) {
4497 dprintf(CRITICAL, "ERROR: Failed to update recovery commands\n");
4498 fastboot_fail("Failed to update recovery command");
4499 return;
4500 }
4501 fastboot_okay("");
4502 reboot_device(REBOOT_MODE_UNKNOWN);
4503
4504 //shouldn't come here.
4505 dprintf(CRITICAL, "ERROR: Failed to reboot device\n");
4506 return;
4507}
4508#endif
4509
Chandan Uddaraju94183c02010-01-15 15:13:59 -08004510void cmd_reboot_bootloader(const char *arg, void *data, unsigned sz)
4511{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004512 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08004513 fastboot_okay("");
4514 reboot_device(FASTBOOT_MODE);
4515}
4516
Ameya Thakur11cf1a62013-08-05 12:44:48 -07004517void cmd_oem_enable_charger_screen(const char *arg, void *data, unsigned size)
4518{
4519 dprintf(INFO, "Enabling charger screen check\n");
4520 device.charger_screen_enabled = 1;
4521 write_device_info(&device);
4522 fastboot_okay("");
4523}
4524
4525void cmd_oem_disable_charger_screen(const char *arg, void *data, unsigned size)
4526{
4527 dprintf(INFO, "Disabling charger screen check\n");
4528 device.charger_screen_enabled = 0;
4529 write_device_info(&device);
4530 fastboot_okay("");
4531}
4532
lijuanga25d8bb2015-09-16 13:11:52 +08004533void cmd_oem_off_mode_charger(const char *arg, void *data, unsigned size)
4534{
4535 char *p = NULL;
4536 const char *delim = " \t\n\r";
Parth Dixit407f15b2016-01-07 14:47:37 +05304537 char *sp;
lijuanga25d8bb2015-09-16 13:11:52 +08004538
4539 if (arg) {
Parth Dixit407f15b2016-01-07 14:47:37 +05304540 p = strtok_r((char *)arg, delim, &sp);
lijuanga25d8bb2015-09-16 13:11:52 +08004541 if (p) {
4542 if (!strncmp(p, "0", 1)) {
4543 device.charger_screen_enabled = 0;
4544 } else if (!strncmp(p, "1", 1)) {
4545 device.charger_screen_enabled = 1;
4546 }
4547 }
4548 }
4549
4550 /* update charger_screen_enabled value for getvar
4551 * command
4552 */
4553 snprintf(charger_screen_enabled, MAX_RSP_SIZE, "%d",
4554 device.charger_screen_enabled);
4555
4556 write_device_info(&device);
4557 fastboot_okay("");
4558}
4559
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05304560void cmd_oem_select_display_panel(const char *arg, void *data, unsigned size)
4561{
4562 dprintf(INFO, "Selecting display panel %s\n", arg);
4563 if (arg)
4564 strlcpy(device.display_panel, arg,
4565 sizeof(device.display_panel));
4566 write_device_info(&device);
4567 fastboot_okay("");
4568}
4569
Shashank Mittal162244e2011-08-08 19:01:25 -07004570void cmd_oem_unlock(const char *arg, void *data, unsigned sz)
4571{
lijuang4ece1e72015-08-14 21:02:36 +08004572 set_device_unlock(UNLOCK, TRUE);
vijay kumarc65876c2015-04-24 13:29:16 +05304573}
4574
4575void cmd_oem_unlock_go(const char *arg, void *data, unsigned sz)
4576{
lijuang4ece1e72015-08-14 21:02:36 +08004577 if(!device.is_unlocked) {
vijay kumarc65876c2015-04-24 13:29:16 +05304578 if(!is_allow_unlock) {
4579 fastboot_fail("oem unlock is not allowed");
4580 return;
4581 }
4582
lijuang4ece1e72015-08-14 21:02:36 +08004583 set_device_unlock_value(UNLOCK, TRUE);
vijay kumarc65876c2015-04-24 13:29:16 +05304584
lijuang4ece1e72015-08-14 21:02:36 +08004585 /* wipe data */
vijay kumarc65876c2015-04-24 13:29:16 +05304586 struct recovery_message msg;
Kishor PK60a68212017-05-08 16:55:57 +05304587 memset(&msg, 0, sizeof(msg));
vijay kumarc65876c2015-04-24 13:29:16 +05304588 snprintf(msg.recovery, sizeof(msg.recovery), "recovery\n--wipe_data");
4589 write_misc(0, &msg, sizeof(msg));
4590
4591 fastboot_okay("");
4592 reboot_device(RECOVERY_MODE);
Shashank Mittald3e54dd2014-08-28 15:24:02 -07004593 }
4594 fastboot_okay("");
4595}
4596
Channagoud Kadabiad259832015-05-29 11:14:17 -07004597static int aboot_frp_unlock(char *pname, void *data, unsigned sz)
4598{
Mayank Grover70f8b0e2017-05-17 11:49:00 +05304599 int ret=1;
4600 bool authentication_success=false;
Channagoud Kadabiad259832015-05-29 11:14:17 -07004601
Mayank Grover70f8b0e2017-05-17 11:49:00 +05304602 /*
4603 Authentication method not implemented.
4604
4605 OEM to implement, authentication system which on successful validataion,
4606 calls write_allow_oem_unlock() with is_allow_unlock.
4607 */
4608#if 0
4609 authentication_success = oem_specific_auth_mthd();
4610#endif
4611
4612 if (authentication_success)
Channagoud Kadabiad259832015-05-29 11:14:17 -07004613 {
Mayank Grover70f8b0e2017-05-17 11:49:00 +05304614 is_allow_unlock = true;
4615 write_allow_oem_unlock(is_allow_unlock);
4616 ret = 0;
Channagoud Kadabiad259832015-05-29 11:14:17 -07004617 }
4618 return ret;
4619}
4620
Shashank Mittald3e54dd2014-08-28 15:24:02 -07004621void cmd_oem_lock(const char *arg, void *data, unsigned sz)
4622{
lijuang4ece1e72015-08-14 21:02:36 +08004623 set_device_unlock(UNLOCK, FALSE);
Shashank Mittal162244e2011-08-08 19:01:25 -07004624}
4625
Shashank Mittala0032282011-08-26 14:50:11 -07004626void cmd_oem_devinfo(const char *arg, void *data, unsigned sz)
4627{
lijuang511a2b52015-08-14 20:50:51 +08004628 char response[MAX_RSP_SIZE];
Ameya Thakur11cf1a62013-08-05 12:44:48 -07004629 snprintf(response, sizeof(response), "\tDevice tampered: %s", (device.is_tampered ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07004630 fastboot_info(response);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07004631 snprintf(response, sizeof(response), "\tDevice unlocked: %s", (device.is_unlocked ? "true" : "false"));
4632 fastboot_info(response);
Monika Singh292b3e92018-03-17 22:40:23 +05304633#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover912eaa62017-10-26 12:08:53 +05304634 if (VB_M <= target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +05304635 {
4636 snprintf(response, sizeof(response), "\tDevice critical unlocked: %s",
4637 (device.is_unlock_critical ? "true" : "false"));
4638 fastboot_info(response);
4639 }
Parth Dixitddbc7352015-10-18 03:13:31 +05304640#endif
Ameya Thakur11cf1a62013-08-05 12:44:48 -07004641 snprintf(response, sizeof(response), "\tCharger screen enabled: %s", (device.charger_screen_enabled ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07004642 fastboot_info(response);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05304643 snprintf(response, sizeof(response), "\tDisplay panel: %s", (device.display_panel));
4644 fastboot_info(response);
Shashank Mittala0032282011-08-26 14:50:11 -07004645 fastboot_okay("");
4646}
4647
lijuang511a2b52015-08-14 20:50:51 +08004648void cmd_flashing_get_unlock_ability(const char *arg, void *data, unsigned sz)
4649{
4650 char response[MAX_RSP_SIZE];
4651 snprintf(response, sizeof(response), "\tget_unlock_ability: %d", is_allow_unlock);
4652 fastboot_info(response);
4653 fastboot_okay("");
4654}
4655
4656void cmd_flashing_lock_critical(const char *arg, void *data, unsigned sz)
4657{
lijuang4ece1e72015-08-14 21:02:36 +08004658 set_device_unlock(UNLOCK_CRITICAL, FALSE);
lijuang511a2b52015-08-14 20:50:51 +08004659}
4660
4661void cmd_flashing_unlock_critical(const char *arg, void *data, unsigned sz)
4662{
lijuang4ece1e72015-08-14 21:02:36 +08004663 set_device_unlock(UNLOCK_CRITICAL, TRUE);
lijuang511a2b52015-08-14 20:50:51 +08004664}
4665
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07004666void cmd_preflash(const char *arg, void *data, unsigned sz)
4667{
4668 fastboot_okay("");
4669}
4670
Mao Flynn7b379f32015-04-20 00:28:30 +08004671static uint8_t logo_header[LOGO_IMG_HEADER_SIZE];
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304672
Mao Flynn7b379f32015-04-20 00:28:30 +08004673int splash_screen_check_header(logo_img_header *header)
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304674{
Mao Flynn7b379f32015-04-20 00:28:30 +08004675 if (memcmp(header->magic, LOGO_IMG_MAGIC, 8))
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304676 return -1;
Mao Flynn7b379f32015-04-20 00:28:30 +08004677 if (header->width == 0 || header->height == 0)
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304678 return -1;
4679 return 0;
4680}
4681
Mao Flynn7b379f32015-04-20 00:28:30 +08004682int splash_screen_flash()
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07004683{
4684 struct ptentry *ptn;
4685 struct ptable *ptable;
Mao Flynn7b379f32015-04-20 00:28:30 +08004686 struct logo_img_header *header;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07004687 struct fbcon_config *fb_display = NULL;
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08004688
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304689 ptable = flash_get_ptable();
4690 if (ptable == NULL) {
Mao Flynn7b379f32015-04-20 00:28:30 +08004691 dprintf(CRITICAL, "ERROR: Partition table not found\n");
4692 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304693 }
Mao Flynn7b379f32015-04-20 00:28:30 +08004694
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304695 ptn = ptable_find(ptable, "splash");
4696 if (ptn == NULL) {
4697 dprintf(CRITICAL, "ERROR: splash Partition not found\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08004698 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304699 }
Mao Flynn7b379f32015-04-20 00:28:30 +08004700 if (flash_read(ptn, 0, (void *)logo_header, LOGO_IMG_HEADER_SIZE)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304701 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08004702 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304703 }
4704
Mao Flynn7b379f32015-04-20 00:28:30 +08004705 header = (struct logo_img_header *)logo_header;
4706 if (splash_screen_check_header(header)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304707 dprintf(CRITICAL, "ERROR: Boot image header invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08004708 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304709 }
4710
4711 fb_display = fbcon_display();
4712 if (fb_display) {
raghavendra ambadas554f4fd2018-10-09 15:50:40 +05304713 if (header->type && (header->blocks != 0) &&
4714 (UINT_MAX >= header->blocks * 512) &&
4715 ((header->blocks * 512) <= (fb_display->width *
4716 fb_display->height * (fb_display->bpp / 8)))) {
4717 /* RLE24 compressed data */
Mao Flynn7b379f32015-04-20 00:28:30 +08004718 uint8_t *base = (uint8_t *) fb_display->base + LOGO_IMG_OFFSET;
4719
4720 /* if the logo is full-screen size, remove "fbcon_clear()" */
4721 if ((header->width != fb_display->width)
4722 || (header->height != fb_display->height))
4723 fbcon_clear();
4724
4725 if (flash_read(ptn + LOGO_IMG_HEADER_SIZE, 0,
4726 (uint32_t *)base,
4727 (header->blocks * 512))) {
4728 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
4729 return -1;
4730 }
4731 fbcon_extract_to_screen(header, base);
4732 return 0;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07004733 }
Mao Flynn7b379f32015-04-20 00:28:30 +08004734
Veera Sundaram Sankaranc33416f2015-09-28 10:25:55 -07004735 if ((header->width > fb_display->width) || (header->height > fb_display->height)) {
4736 dprintf(CRITICAL, "Logo config greater than fb config. Fall back default logo\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08004737 return -1;
4738 }
4739
Vineet Bajaj99291ed2014-09-09 12:29:46 +05304740 uint8_t *base = (uint8_t *) fb_display->base;
Sachin Bhayare619e9e42017-05-15 13:10:31 +05304741 uint32_t fb_size = ROUNDUP(fb_display->width *
4742 fb_display->height *
4743 (fb_display->bpp / 8), 4096);
4744 uint32_t splash_size = ((((header->width * header->height *
4745 fb_display->bpp/8) + 511) >> 9) << 9);
4746
4747 if (splash_size > fb_size) {
4748 dprintf(CRITICAL, "ERROR: Splash image size invalid\n");
4749 return -1;
4750 }
4751
Mao Flynn7b379f32015-04-20 00:28:30 +08004752 if (flash_read(ptn + LOGO_IMG_HEADER_SIZE, 0,
4753 (uint32_t *)base,
4754 ((((header->width * header->height * fb_display->bpp/8) + 511) >> 9) << 9))) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304755 fbcon_clear();
Vineet Bajaj99291ed2014-09-09 12:29:46 +05304756 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08004757 return -1;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07004758 }
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304759 }
4760
Mao Flynn7b379f32015-04-20 00:28:30 +08004761 return 0;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304762}
4763
Mao Flynn7b379f32015-04-20 00:28:30 +08004764int splash_screen_mmc()
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304765{
4766 int index = INVALID_PTN;
4767 unsigned long long ptn = 0;
4768 struct fbcon_config *fb_display = NULL;
Mao Flynn7b379f32015-04-20 00:28:30 +08004769 struct logo_img_header *header;
Mao Flynn1893a7f2015-06-03 12:03:36 +08004770 uint32_t blocksize, realsize, readsize;
4771 uint8_t *base;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304772
4773 index = partition_get_index("splash");
4774 if (index == 0) {
4775 dprintf(CRITICAL, "ERROR: splash Partition table not found\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08004776 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304777 }
4778
4779 ptn = partition_get_offset(index);
4780 if (ptn == 0) {
4781 dprintf(CRITICAL, "ERROR: splash Partition invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08004782 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304783 }
4784
Mao Flynn1893a7f2015-06-03 12:03:36 +08004785 mmc_set_lun(partition_get_lun(index));
4786
4787 blocksize = mmc_get_device_blocksize();
4788 if (blocksize == 0) {
4789 dprintf(CRITICAL, "ERROR:splash Partition invalid blocksize\n");
4790 return -1;
4791 }
4792
4793 fb_display = fbcon_display();
Channagoud Kadabidaf10a62015-07-22 11:51:55 -07004794 if (!fb_display)
4795 {
4796 dprintf(CRITICAL, "ERROR: fb config is not allocated\n");
4797 return -1;
4798 }
4799
Mao Flynn1893a7f2015-06-03 12:03:36 +08004800 base = (uint8_t *) fb_display->base;
4801
Kishor PKee5c0a32018-03-06 16:49:46 +05304802 if (mmc_read(ptn + PLL_CODES_OFFSET, (uint32_t *)(base + LOGO_IMG_OFFSET), blocksize)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304803 dprintf(CRITICAL, "ERROR: Cannot read splash image header\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08004804 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304805 }
4806
Mao Flynn1893a7f2015-06-03 12:03:36 +08004807 header = (struct logo_img_header *)(base + LOGO_IMG_OFFSET);
Mao Flynn7b379f32015-04-20 00:28:30 +08004808 if (splash_screen_check_header(header)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304809 dprintf(CRITICAL, "ERROR: Splash image header invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08004810 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304811 }
4812
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304813 if (fb_display) {
raghavendra ambadas554f4fd2018-10-09 15:50:40 +05304814 if (header->type && (header->blocks != 0) &&
4815 (UINT_MAX >= header->blocks * 512 + LOGO_IMG_HEADER_SIZE) &&
4816 ((header->blocks * 512) <= (fb_display->width *
4817 fb_display->height * (fb_display->bpp / 8)))) {
4818 /* 1 RLE24 compressed data */
Mao Flynn1893a7f2015-06-03 12:03:36 +08004819 base += LOGO_IMG_OFFSET;
Mao Flynn7b379f32015-04-20 00:28:30 +08004820
Mao Flynn1893a7f2015-06-03 12:03:36 +08004821 realsize = header->blocks * 512;
4822 readsize = ROUNDUP((realsize + LOGO_IMG_HEADER_SIZE), blocksize) - blocksize;
4823
4824 /* if the logo is not full-screen size, clean screen */
Mao Flynn7b379f32015-04-20 00:28:30 +08004825 if ((header->width != fb_display->width)
4826 || (header->height != fb_display->height))
4827 fbcon_clear();
4828
Sachin Bhayare619e9e42017-05-15 13:10:31 +05304829 uint32_t fb_size = ROUNDUP(fb_display->width *
4830 fb_display->height *
4831 (fb_display->bpp / 8), 4096);
4832
4833 if (readsize > fb_size) {
4834 dprintf(CRITICAL, "ERROR: Splash image size invalid\n");
4835 return -1;
4836 }
4837
Kishor PKee5c0a32018-03-06 16:49:46 +05304838 if (mmc_read(ptn + PLL_CODES_OFFSET + blocksize, (uint32_t *)(base + blocksize), readsize)) {
Mao Flynn7b379f32015-04-20 00:28:30 +08004839 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
4840 return -1;
4841 }
Mao Flynn7b379f32015-04-20 00:28:30 +08004842
Mao Flynn1893a7f2015-06-03 12:03:36 +08004843 fbcon_extract_to_screen(header, (base + LOGO_IMG_HEADER_SIZE));
4844 } else { /* 2 Raw BGR data */
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304845
Veera Sundaram Sankaranc33416f2015-09-28 10:25:55 -07004846 if ((header->width > fb_display->width) || (header->height > fb_display->height)) {
4847 dprintf(CRITICAL, "Logo config greater than fb config. Fall back default logo\n");
Mao Flynn1893a7f2015-06-03 12:03:36 +08004848 return -1;
4849 }
4850
4851 realsize = header->width * header->height * fb_display->bpp / 8;
4852 readsize = ROUNDUP((realsize + LOGO_IMG_HEADER_SIZE), blocksize) - blocksize;
4853
4854 if (blocksize == LOGO_IMG_HEADER_SIZE) { /* read the content directly */
Kishor PKee5c0a32018-03-06 16:49:46 +05304855 if (mmc_read((ptn + PLL_CODES_OFFSET + LOGO_IMG_HEADER_SIZE), (uint32_t *)base, readsize)) {
Mao Flynn1893a7f2015-06-03 12:03:36 +08004856 fbcon_clear();
4857 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
4858 return -1;
4859 }
4860 } else {
Kishor PKee5c0a32018-03-06 16:49:46 +05304861 if (mmc_read(ptn + PLL_CODES_OFFSET + blocksize ,
Mao Flynn1893a7f2015-06-03 12:03:36 +08004862 (uint32_t *)(base + LOGO_IMG_OFFSET + blocksize), readsize)) {
4863 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
4864 return -1;
4865 }
4866 memmove(base, (base + LOGO_IMG_OFFSET + LOGO_IMG_HEADER_SIZE), realsize);
4867 }
4868 }
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304869 }
4870
Mao Flynn7b379f32015-04-20 00:28:30 +08004871 return 0;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304872}
4873
Mao Flynn7b379f32015-04-20 00:28:30 +08004874int fetch_image_from_partition()
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304875{
4876 if (target_is_emmc_boot()) {
4877 return splash_screen_mmc();
4878 } else {
4879 return splash_screen_flash();
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07004880 }
4881}
4882
Mayank Grover351a75e2017-05-30 20:06:08 +05304883void publish_getvar_multislot_vars()
4884{
4885 int i,count;
4886 static bool published = false;
4887 static char slot_count[MAX_RSP_SIZE];
4888 static struct ab_slot_info slot_info[AB_SUPPORTED_SLOTS];
4889 static char active_slot_suffix[MAX_RSP_SIZE];
4890 static char has_slot_pname[NUM_PARTITIONS][MAX_GET_VAR_NAME_SIZE];
4891 static char has_slot_reply[NUM_PARTITIONS][MAX_RSP_SIZE];
4892 const char *tmp;
4893 char tmpbuff[MAX_GET_VAR_NAME_SIZE];
Mayank Grover2bd84bb2017-06-20 11:06:07 +05304894 signed active_slt;
Mayank Grover351a75e2017-05-30 20:06:08 +05304895
4896 if (!published)
4897 {
4898 /* Update slot meta info */
4899 count = partition_fill_partition_meta(has_slot_pname, has_slot_reply,
4900 partition_get_partition_count());
4901 for(i=0; i<count; i++)
4902 {
4903 memset(tmpbuff, 0, MAX_GET_VAR_NAME_SIZE);
Mayank Grover2bd84bb2017-06-20 11:06:07 +05304904 snprintf(tmpbuff, MAX_GET_VAR_NAME_SIZE,"has-slot:%s",
4905 has_slot_pname[i]);
4906 strlcpy(has_slot_pname[i], tmpbuff, MAX_GET_VAR_NAME_SIZE);
Mayank Grover351a75e2017-05-30 20:06:08 +05304907 fastboot_publish(has_slot_pname[i], has_slot_reply[i]);
4908 }
4909
4910 for (i=0; i<AB_SUPPORTED_SLOTS; i++)
4911 {
4912 tmp = SUFFIX_SLOT(i);
Mayank Grover7b8a8f82017-10-27 13:07:59 +05304913 tmp++; // to remove "_" from slot_suffix.
Mayank Grover2bd84bb2017-06-20 11:06:07 +05304914 snprintf(slot_info[i].slot_is_unbootable, sizeof(slot_info[i].slot_is_unbootable),
4915 "slot-unbootable:%s", tmp);
4916 snprintf(slot_info[i].slot_is_active, sizeof(slot_info[i].slot_is_active),
4917 "slot-active:%s", tmp);
4918 snprintf(slot_info[i].slot_is_succesful, sizeof(slot_info[i].slot_is_succesful),
4919 "slot-success:%s", tmp);
4920 snprintf(slot_info[i].slot_retry_count, sizeof(slot_info[i].slot_retry_count),
4921 "slot-retry-count:%s", tmp);
Mayank Grover351a75e2017-05-30 20:06:08 +05304922 fastboot_publish(slot_info[i].slot_is_unbootable,
4923 slot_info[i].slot_is_unbootable_rsp);
4924 fastboot_publish(slot_info[i].slot_is_active,
4925 slot_info[i].slot_is_active_rsp);
4926 fastboot_publish(slot_info[i].slot_is_succesful,
4927 slot_info[i].slot_is_succesful_rsp);
4928 fastboot_publish(slot_info[i].slot_retry_count,
4929 slot_info[i].slot_retry_count_rsp);
4930 }
4931 fastboot_publish("current-slot", active_slot_suffix);
4932 snprintf(slot_count, sizeof(slot_count),"%d", AB_SUPPORTED_SLOTS);
4933 fastboot_publish("slot-count", slot_count);
4934 published = true;
4935 }
4936
Mayank Grover2bd84bb2017-06-20 11:06:07 +05304937 active_slt = partition_find_active_slot();
4938 if (active_slt != INVALID)
Mayank Grover7b8a8f82017-10-27 13:07:59 +05304939 {
4940 tmp = SUFFIX_SLOT(active_slt);
4941 tmp++; // to remove "_" from slot_suffix.
4942 snprintf(active_slot_suffix, sizeof(active_slot_suffix), "%s", tmp);
4943 }
Mayank Grover2bd84bb2017-06-20 11:06:07 +05304944 else
4945 strlcpy(active_slot_suffix, "INVALID", sizeof(active_slot_suffix));
4946
Mayank Grover351a75e2017-05-30 20:06:08 +05304947 /* Update partition meta information */
4948 partition_fill_slot_meta(slot_info);
4949 return;
4950}
4951
lijuang4ece1e72015-08-14 21:02:36 +08004952void get_product_name(unsigned char *buf)
4953{
4954 snprintf((char*)buf, MAX_RSP_SIZE, "%s", TARGET(BOARD));
4955 return;
4956}
4957
Mayank Grover7b8a8f82017-10-27 13:07:59 +05304958#if PRODUCT_IOT
4959void get_bootloader_version_iot(unsigned char *buf)
4960{
4961 if (buf != NULL)
4962 {
4963 strlcpy(buf, TARGET(BOARD), MAX_VERSION_LEN);
4964 strlcat(buf, "-", MAX_VERSION_LEN);
4965 strlcat(buf, PRODUCT_IOT_VERSION, MAX_VERSION_LEN);
4966 }
4967 return;
4968}
4969#endif
4970
lijuang4ece1e72015-08-14 21:02:36 +08004971void get_bootloader_version(unsigned char *buf)
4972{
4973 snprintf((char*)buf, MAX_RSP_SIZE, "%s", device.bootloader_version);
4974 return;
4975}
4976
4977void get_baseband_version(unsigned char *buf)
4978{
4979 snprintf((char*)buf, MAX_RSP_SIZE, "%s", device.radio_version);
4980 return;
4981}
4982
Monika Singh32a09f72018-03-14 13:08:29 +05304983bool is_device_locked_critical()
4984{
4985 return device.is_unlock_critical ? false:true;
4986}
4987
lijuang4ece1e72015-08-14 21:02:36 +08004988bool is_device_locked()
4989{
4990 return device.is_unlocked ? false:true;
4991}
4992
Monika Singh32a09f72018-03-14 13:08:29 +05304993bool is_verity_enforcing()
4994{
4995 return device.verity_mode ? true:false;
4996}
4997
Amol Jadi5edf3552013-07-23 14:15:34 -07004998/* register commands and variables for fastboot */
4999void aboot_fastboot_register_commands(void)
5000{
Sridhar Parasurame94e8152014-10-24 14:06:03 -07005001 int i;
lijuangf16461c2015-08-03 17:09:34 +08005002 char hw_platform_buf[MAX_RSP_SIZE];
Amol Jadi5edf3552013-07-23 14:15:34 -07005003
Sridhar Parasurame94e8152014-10-24 14:06:03 -07005004 struct fastboot_cmd_desc cmd_list[] = {
lijuang511a2b52015-08-14 20:50:51 +08005005 /* By default the enabled list is empty. */
5006 {"", NULL},
5007 /* move commands enclosed within the below ifndef to here
5008 * if they need to be enabled in user build.
5009 */
Sridhar Parasurame94e8152014-10-24 14:06:03 -07005010#ifndef DISABLE_FASTBOOT_CMDS
lijuang511a2b52015-08-14 20:50:51 +08005011 /* Register the following commands only for non-user builds */
5012 {"flash:", cmd_flash},
5013 {"erase:", cmd_erase},
5014 {"boot", cmd_boot},
5015 {"continue", cmd_continue},
5016 {"reboot", cmd_reboot},
5017 {"reboot-bootloader", cmd_reboot_bootloader},
5018 {"oem unlock", cmd_oem_unlock},
5019 {"oem unlock-go", cmd_oem_unlock_go},
5020 {"oem lock", cmd_oem_lock},
5021 {"flashing unlock", cmd_oem_unlock},
5022 {"flashing lock", cmd_oem_lock},
5023 {"flashing lock_critical", cmd_flashing_lock_critical},
5024 {"flashing unlock_critical", cmd_flashing_unlock_critical},
5025 {"flashing get_unlock_ability", cmd_flashing_get_unlock_ability},
5026 {"oem device-info", cmd_oem_devinfo},
5027 {"preflash", cmd_preflash},
5028 {"oem enable-charger-screen", cmd_oem_enable_charger_screen},
5029 {"oem disable-charger-screen", cmd_oem_disable_charger_screen},
lijuanga25d8bb2015-09-16 13:11:52 +08005030 {"oem off-mode-charge", cmd_oem_off_mode_charger},
lijuang511a2b52015-08-14 20:50:51 +08005031 {"oem select-display-panel", cmd_oem_select_display_panel},
Mayank Grover6ccc1c92017-07-04 17:36:46 +05305032 {"set_active",cmd_set_active},
Mayank Grover98c4c742019-04-25 17:21:37 +05305033#if DYNAMIC_PARTITION_SUPPORT
5034 {"reboot-fastboot",cmd_reboot_fastboot},
5035 {"reboot-recovery",cmd_reboot_recovery},
5036#endif
Channagoud Kadabi736c4962015-08-21 11:56:52 -07005037#if UNITTEST_FW_SUPPORT
Channagoud Kadabid5ddf482015-09-28 10:31:35 -07005038 {"oem run-tests", cmd_oem_runtests},
Channagoud Kadabi736c4962015-08-21 11:56:52 -07005039#endif
Sridhar Parasurame94e8152014-10-24 14:06:03 -07005040#endif
lijuang511a2b52015-08-14 20:50:51 +08005041 };
Sridhar Parasurame94e8152014-10-24 14:06:03 -07005042
5043 int fastboot_cmds_count = sizeof(cmd_list)/sizeof(cmd_list[0]);
5044 for (i = 1; i < fastboot_cmds_count; i++)
5045 fastboot_register(cmd_list[i].name,cmd_list[i].cb);
5046
Amol Jadi5edf3552013-07-23 14:15:34 -07005047 /* publish variables and their values */
5048 fastboot_publish("product", TARGET(BOARD));
5049 fastboot_publish("kernel", "lk");
5050 fastboot_publish("serialno", sn_buf);
5051
Mayank Grovera64dfbe2018-05-17 14:06:34 +05305052 /*publish hw-revision major(upper 16 bits) and minor(lower 16 bits)*/
5053 snprintf(soc_version_str, MAX_RSP_SIZE, "%x", board_soc_version());
5054 fastboot_publish("hw-revision", soc_version_str);
5055
Amol Jadi5edf3552013-07-23 14:15:34 -07005056 /*
5057 * partition info is supported only for emmc partitions
5058 * Calling this for NAND prints some error messages which
5059 * is harmless but misleading. Avoid calling this for NAND
5060 * devices.
5061 */
5062 if (target_is_emmc_boot())
Mayank Grover7b8a8f82017-10-27 13:07:59 +05305063 publish_getvar_partition_info(part_info, partition_get_partition_count());
Amol Jadi5edf3552013-07-23 14:15:34 -07005064
Mayank Grover351a75e2017-05-30 20:06:08 +05305065 if (partition_multislot_is_supported())
5066 publish_getvar_multislot_vars();
5067
Amol Jadi5edf3552013-07-23 14:15:34 -07005068 /* Max download size supported */
lijuang2fbed912019-07-12 14:15:05 +08005069#if !VERIFIED_BOOT_2
Ameya Thakur11cf1a62013-08-05 12:44:48 -07005070 snprintf(max_download_size, MAX_RSP_SIZE, "\t0x%x",
5071 target_get_max_flash_size());
lijuang2fbed912019-07-12 14:15:05 +08005072#else
5073 snprintf(max_download_size, MAX_RSP_SIZE, "\t0x%x",
5074 SUB_SALT_BUFF_OFFSET(target_get_max_flash_size()));
5075#endif
5076
Amol Jadi5edf3552013-07-23 14:15:34 -07005077 fastboot_publish("max-download-size", (const char *) max_download_size);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07005078 /* Is the charger screen check enabled */
5079 snprintf(charger_screen_enabled, MAX_RSP_SIZE, "%d",
5080 device.charger_screen_enabled);
5081 fastboot_publish("charger-screen-enabled",
5082 (const char *) charger_screen_enabled);
lijuanga25d8bb2015-09-16 13:11:52 +08005083 fastboot_publish("off-mode-charge", (const char *) charger_screen_enabled);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05305084 snprintf(panel_display_mode, MAX_RSP_SIZE, "%s",
5085 device.display_panel);
5086 fastboot_publish("display-panel",
5087 (const char *) panel_display_mode);
Mukesh Ojhabb6e7f32018-03-02 15:04:06 +05305088
5089 if (target_is_emmc_boot())
5090 {
5091 mmc_blocksize = mmc_get_device_blocksize();
5092 }
5093 else
5094 {
5095 mmc_blocksize = flash_block_size();
5096 }
5097 snprintf(block_size_string, MAX_RSP_SIZE, "0x%x", mmc_blocksize);
5098 fastboot_publish("erase-block-size", (const char *) block_size_string);
5099 fastboot_publish("logical-block-size", (const char *) block_size_string);
Mayank Grover7b8a8f82017-10-27 13:07:59 +05305100#if PRODUCT_IOT
5101 get_bootloader_version_iot(&bootloader_version_string);
5102 fastboot_publish("version-bootloader", (const char *) bootloader_version_string);
5103
5104 /* Version baseband is n/a for apq iot devices */
5105 fastboot_publish("version-baseband", "N/A");
Mayank Grover7b8a8f82017-10-27 13:07:59 +05305106#else
Channagoud Kadabiaafbbfc2015-03-04 17:52:56 -08005107 fastboot_publish("version-bootloader", (const char *) device.bootloader_version);
5108 fastboot_publish("version-baseband", (const char *) device.radio_version);
Mayank Grover7b8a8f82017-10-27 13:07:59 +05305109#endif
lijuangf16461c2015-08-03 17:09:34 +08005110 fastboot_publish("secure", is_secure_boot_enable()? "yes":"no");
Mayank Grover7b8a8f82017-10-27 13:07:59 +05305111 fastboot_publish("unlocked", device.is_unlocked ? "yes":"no");
lijuangf16461c2015-08-03 17:09:34 +08005112 smem_get_hw_platform_name((unsigned char *) hw_platform_buf, sizeof(hw_platform_buf));
5113 snprintf(get_variant, MAX_RSP_SIZE, "%s %s", hw_platform_buf,
5114 target_is_emmc_boot()? "eMMC":"UFS");
5115 fastboot_publish("variant", (const char *) get_variant);
lijuang65c5a822015-08-29 16:35:36 +08005116#if CHECK_BAT_VOLTAGE
lijuang102dfa92015-10-09 18:31:03 +08005117 update_battery_status();
lijuang65c5a822015-08-29 16:35:36 +08005118 fastboot_publish("battery-voltage", (const char *) battery_voltage);
lijuang102dfa92015-10-09 18:31:03 +08005119 fastboot_publish("battery-soc-ok", (const char *) battery_soc_ok);
lijuang65c5a822015-08-29 16:35:36 +08005120#endif
Mayank Grover98c4c742019-04-25 17:21:37 +05305121 if (target_dynamic_partition_supported())
5122 fastboot_publish("is-userspace", "no");
Amol Jadi5edf3552013-07-23 14:15:34 -07005123}
5124
Brian Swetland9c4c0752009-01-25 16:23:50 -08005125void aboot_init(const struct app_descriptor *app)
5126{
Shashank Mittal4f99a882010-02-01 13:58:50 -08005127 unsigned reboot_mode = 0;
Mayank Grover351a75e2017-05-30 20:06:08 +05305128 int boot_err_type = 0;
5129 int boot_slot = INVALID;
Chandan Uddarajubedca152010-06-02 23:05:15 -07005130
lijuang39831732016-01-08 17:49:02 +08005131 /* Initialise wdog to catch early lk crashes */
5132#if WDOG_SUPPORT
5133 msm_wdog_init();
5134#endif
5135
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07005136 /* Setup page size information for nv storage */
Shashank Mittald8c42bf2010-06-09 15:44:28 -07005137 if (target_is_emmc_boot())
5138 {
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07005139 page_size = mmc_page_size();
Shashank Mittald8c42bf2010-06-09 15:44:28 -07005140 page_mask = page_size - 1;
Gaurav Nebhwani4d2389c2016-03-17 21:10:05 +05305141 mmc_blocksize = mmc_get_device_blocksize();
5142 mmc_blocksize_mask = mmc_blocksize - 1;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07005143 }
5144 else
5145 {
5146 page_size = flash_page_size();
5147 page_mask = page_size - 1;
5148 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07005149 ASSERT((MEMBASE + MEMSIZE) > MEMBASE);
5150
Ameya Thakur11cf1a62013-08-05 12:44:48 -07005151 read_device_info(&device);
vijay kumarc65876c2015-04-24 13:29:16 +05305152 read_allow_oem_unlock(&device);
Shashank Mittal162244e2011-08-08 19:01:25 -07005153
Mayank Grover351a75e2017-05-30 20:06:08 +05305154 /* Detect multi-slot support */
5155 if (partition_multislot_is_supported())
5156 {
5157 boot_slot = partition_find_active_slot();
5158 if (boot_slot == INVALID)
5159 {
5160 boot_into_fastboot = true;
5161 dprintf(INFO, "Active Slot: (INVALID)\n");
5162 }
5163 else
5164 {
5165 /* Setting the state of system to boot active slot */
5166 partition_mark_active_slot(boot_slot);
5167 dprintf(INFO, "Active Slot: (%s)\n", SUFFIX_SLOT(boot_slot));
5168 }
5169 }
5170
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08005171 /* Display splash screen if enabled */
5172#if DISPLAY_SPLASH_SCREEN
lijuang99c02d82015-02-13 19:04:34 +08005173#if NO_ALARM_DISPLAY
5174 if (!check_alarm_boot()) {
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08005175#endif
lijuang99c02d82015-02-13 19:04:34 +08005176 dprintf(SPEW, "Display Init: Start\n");
Ajay Singh Parmara7865a82015-04-16 21:27:52 -07005177#if DISPLAY_HDMI_PRIMARY
5178 if (!strlen(device.display_panel))
5179 strlcpy(device.display_panel, DISPLAY_PANEL_HDMI,
5180 sizeof(device.display_panel));
5181#endif
Channagoud Kadabi90869ce2015-04-27 11:15:14 -07005182#if ENABLE_WBC
Channagoud Kadabid801ac72015-08-26 11:21:51 -07005183 /* Wait if the display shutdown is in progress */
5184 while(pm_app_display_shutdown_in_prgs());
Channagoud Kadabi90869ce2015-04-27 11:15:14 -07005185 if (!pm_appsbl_display_init_done())
5186 target_display_init(device.display_panel);
5187 else
5188 display_image_on_screen();
5189#else
lijuang99c02d82015-02-13 19:04:34 +08005190 target_display_init(device.display_panel);
Channagoud Kadabi90869ce2015-04-27 11:15:14 -07005191#endif
lijuang99c02d82015-02-13 19:04:34 +08005192 dprintf(SPEW, "Display Init: Done\n");
5193#if NO_ALARM_DISPLAY
5194 }
5195#endif
5196#endif
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08005197
Greg Griscod6250552011-06-29 14:40:23 -07005198 target_serialno((unsigned char *) sn_buf);
Ajay Dudanib06c05f2011-05-12 14:46:10 -07005199 dprintf(SPEW,"serial number: %s\n",sn_buf);
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -08005200
Dhaval Patel223ec952013-07-18 14:49:44 -07005201 memset(display_panel_buf, '\0', MAX_PANEL_BUF_SIZE);
5202
Matthew Qindefd5562014-07-11 18:02:40 +08005203 /*
5204 * Check power off reason if user force reset,
5205 * if yes phone will do normal boot.
5206 */
5207 if (is_user_force_reset())
5208 goto normal_boot;
5209
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07005210 /* Check if we should do something other than booting up */
Ameya Thakur0b9c2442013-05-10 13:22:56 -07005211 if (keys_get_state(KEY_VOLUMEUP) && keys_get_state(KEY_VOLUMEDOWN))
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07005212 {
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03005213 dprintf(ALWAYS,"dload mode key sequence detected\n");
lijuang395b5e62015-11-19 17:39:44 +08005214 reboot_device(EMERGENCY_DLOAD);
5215 dprintf(CRITICAL,"Failed to reboot into dload mode\n");
5216
Ameya Thakur0b9c2442013-05-10 13:22:56 -07005217 boot_into_fastboot = true;
5218 }
5219 if (!boot_into_fastboot)
5220 {
5221 if (keys_get_state(KEY_HOME) || keys_get_state(KEY_VOLUMEUP))
5222 boot_into_recovery = 1;
5223 if (!boot_into_recovery &&
5224 (keys_get_state(KEY_BACK) || keys_get_state(KEY_VOLUMEDOWN)))
Pavel Nedev5d91d412013-04-29 11:34:24 +03005225 boot_into_fastboot = true;
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07005226 }
Shashank Mittald8c42bf2010-06-09 15:44:28 -07005227 #if NO_KEYPAD_DRIVER
Kinson Chik0b1c8162011-08-31 16:31:57 -07005228 if (fastboot_trigger())
Pavel Nedev5d91d412013-04-29 11:34:24 +03005229 boot_into_fastboot = true;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07005230 #endif
Chandan Uddarajubedca152010-06-02 23:05:15 -07005231
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07005232#if USE_PON_REBOOT_REG
5233 reboot_mode = check_hard_reboot_mode();
5234#else
Ajay Dudani77421292010-10-27 19:34:06 -07005235 reboot_mode = check_reboot_mode();
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07005236#endif
5237 if (reboot_mode == RECOVERY_MODE)
5238 {
Ajay Dudani77421292010-10-27 19:34:06 -07005239 boot_into_recovery = 1;
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07005240 }
5241 else if(reboot_mode == FASTBOOT_MODE)
5242 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03005243 boot_into_fastboot = true;
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07005244 }
5245 else if(reboot_mode == ALARM_BOOT)
5246 {
Matthew Qind886f3c2014-01-17 16:52:01 +08005247 boot_reason_alarm = true;
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07005248 }
Monika Singhb0db4b82018-09-26 12:18:02 +05305249#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover912eaa62017-10-26 12:08:53 +05305250 else if (VB_M <= target_get_vb_version())
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07005251 {
Mayank Grover889be1b2017-09-12 20:12:23 +05305252 if (reboot_mode == DM_VERITY_ENFORCING)
5253 {
5254 device.verity_mode = 1;
5255 write_device_info(&device);
5256 }
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07005257#if ENABLE_VB_ATTEST
Mayank Grover889be1b2017-09-12 20:12:23 +05305258 else if (reboot_mode == DM_VERITY_EIO)
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07005259#else
Mayank Grover889be1b2017-09-12 20:12:23 +05305260 else if (reboot_mode == DM_VERITY_LOGGING)
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07005261#endif
Mayank Grover889be1b2017-09-12 20:12:23 +05305262 {
5263 device.verity_mode = 0;
5264 write_device_info(&device);
5265 }
5266 else if (reboot_mode == DM_VERITY_KEYSCLEAR)
5267 {
5268 if(send_delete_keys_to_tz())
5269 ASSERT(0);
5270 }
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07005271 }
Parth Dixitddbc7352015-10-18 03:13:31 +05305272#endif
Ajay Dudani77421292010-10-27 19:34:06 -07005273
Matthew Qindefd5562014-07-11 18:02:40 +08005274normal_boot:
Pavel Nedev5d91d412013-04-29 11:34:24 +03005275 if (!boot_into_fastboot)
Shashank Mittald8c42bf2010-06-09 15:44:28 -07005276 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03005277 if (target_is_emmc_boot())
Shashank Mittala0032282011-08-26 14:50:11 -07005278 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03005279 if(emmc_recovery_init())
5280 dprintf(ALWAYS,"error in emmc_recovery_init\n");
5281 if(target_use_signed_kernel())
Shashank Mittala0032282011-08-26 14:50:11 -07005282 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03005283 if((device.is_unlocked) || (device.is_tampered))
5284 {
5285 #ifdef TZ_TAMPER_FUSE
Monika Singh98257462018-06-06 11:28:49 +05305286 set_tamper_fuse_cmd(HLOS_IMG_TAMPER_FUSE);
Pavel Nedev5d91d412013-04-29 11:34:24 +03005287 #endif
5288 #if USE_PCOM_SECBOOT
5289 set_tamper_flag(device.is_tampered);
5290 #endif
5291 }
Shashank Mittala0032282011-08-26 14:50:11 -07005292 }
Amit Blay6281ebc2015-01-11 14:44:08 +02005293
Mayank Grover351a75e2017-05-30 20:06:08 +05305294retry_boot:
5295 /* Trying to boot active partition */
5296 if (partition_multislot_is_supported())
5297 {
5298 boot_slot = partition_find_boot_slot();
5299 partition_mark_active_slot(boot_slot);
5300 if (boot_slot == INVALID)
5301 goto fastboot;
5302 }
5303
5304 boot_err_type = boot_linux_from_mmc();
5305 switch (boot_err_type)
5306 {
5307 case ERR_INVALID_PAGE_SIZE:
5308 case ERR_DT_PARSE:
5309 case ERR_ABOOT_ADDR_OVERLAP:
Mayank Grover10cabbe2017-10-30 19:11:05 +05305310 case ERR_INVALID_BOOT_MAGIC:
Mayank Grover351a75e2017-05-30 20:06:08 +05305311 if(partition_multislot_is_supported())
Mayank Grover10cabbe2017-10-30 19:11:05 +05305312 {
5313 /*
5314 * Deactivate current slot, as it failed to
5315 * boot, and retry next slot.
5316 */
5317 partition_deactivate_slot(boot_slot);
Mayank Grover351a75e2017-05-30 20:06:08 +05305318 goto retry_boot;
Mayank Grover10cabbe2017-10-30 19:11:05 +05305319 }
Mayank Grover351a75e2017-05-30 20:06:08 +05305320 else
5321 break;
Mayank Grover351a75e2017-05-30 20:06:08 +05305322 default:
5323 break;
5324 /* going to fastboot menu */
5325 }
Shashank Mittala0032282011-08-26 14:50:11 -07005326 }
Pavel Nedev5d91d412013-04-29 11:34:24 +03005327 else
5328 {
5329 recovery_init();
5330 #if USE_PCOM_SECBOOT
5331 if((device.is_unlocked) || (device.is_tampered))
5332 set_tamper_flag(device.is_tampered);
5333 #endif
5334 boot_linux_from_flash();
5335 }
5336 dprintf(CRITICAL, "ERROR: Could not do normal boot. Reverting "
5337 "to fastboot mode.\n");
Shashank Mittald8c42bf2010-06-09 15:44:28 -07005338 }
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07005339
Mayank Grover351a75e2017-05-30 20:06:08 +05305340fastboot:
Amol Jadi5edf3552013-07-23 14:15:34 -07005341 /* We are here means regular boot did not happen. Start fastboot. */
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07005342
Amol Jadi5edf3552013-07-23 14:15:34 -07005343 /* register aboot specific fastboot commands */
5344 aboot_fastboot_register_commands();
Amol Jadi57abe4c2011-05-24 15:47:27 -07005345
Amol Jadi5edf3552013-07-23 14:15:34 -07005346 /* dump partition table for debug info */
Kinson Chikf1a43512011-07-14 11:28:39 -07005347 partition_dump();
Amol Jadi5edf3552013-07-23 14:15:34 -07005348
5349 /* initialize and start fastboot */
Saranya Chidura41b34532018-10-16 12:29:52 +05305350#if !VERIFIED_BOOT_2
Amol Jadi5edf3552013-07-23 14:15:34 -07005351 fastboot_init(target_get_scratch_address(), target_get_max_flash_size());
Saranya Chidura41b34532018-10-16 12:29:52 +05305352#else
5353 /* Add salt buffer offset at start of image address to copy VB salt */
5354 fastboot_init(ADD_SALT_BUFF_OFFSET(target_get_scratch_address()),
5355 SUB_SALT_BUFF_OFFSET(target_get_max_flash_size()));
5356#endif
lijuang4ece1e72015-08-14 21:02:36 +08005357#if FBCON_DISPLAY_MSG
lijuang9a7d3b92015-11-30 14:41:24 +08005358 display_fastboot_menu();
lijuang4ece1e72015-08-14 21:02:36 +08005359#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08005360}
5361
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07005362uint32_t get_page_size()
5363{
5364 return page_size;
5365}
5366
Amir Samuelov57a6fa22013-06-05 16:36:43 +03005367/*
5368 * Calculated and save hash (SHA256) for non-signed boot image.
5369 *
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07005370 * @param image_addr - Boot image address
5371 * @param image_size - Size of the boot image
Amir Samuelov57a6fa22013-06-05 16:36:43 +03005372 *
5373 * @return int - 0 on success, negative value on failure.
5374 */
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07005375static int aboot_save_boot_hash_mmc(uint32_t image_addr, uint32_t image_size)
Amir Samuelov57a6fa22013-06-05 16:36:43 +03005376{
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07005377 unsigned int digest[8];
5378#if IMAGE_VERIF_ALGO_SHA1
5379 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA1;
5380#else
5381 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA256;
5382#endif
Amir Samuelov57a6fa22013-06-05 16:36:43 +03005383
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07005384 target_crypto_init_params();
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -08005385 hash_find((unsigned char *) image_addr, image_size, (unsigned char *)&digest, auth_algo);
Amir Samuelov57a6fa22013-06-05 16:36:43 +03005386
5387 save_kernel_hash_cmd(digest);
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07005388 dprintf(INFO, "aboot_save_boot_hash_mmc: imagesize_actual size %d bytes.\n", (int) image_size);
Amir Samuelov57a6fa22013-06-05 16:36:43 +03005389
5390 return 0;
5391}
5392
Mayank Grover351a75e2017-05-30 20:06:08 +05305393
Brian Swetland9c4c0752009-01-25 16:23:50 -08005394APP_START(aboot)
5395 .init = aboot_init,
5396APP_END