blob: f7ec3c84c0da983bdc4f51f8a683d043d91b5e24 [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
jhchen7a504d12020-04-24 15:45:57 +0800263/*[Arima_8910][jhchen] add fuse check property 20181031 begin*/
264#ifdef ENABLE_FUSE_CHECK
265static const char *fuse_blown = " androidboot.oem.color=red";
266static const char *fuse_not_blown = " androidboot.oem.color=brown";
267#endif
268/*[Arima_8910][jhchen] 20181031 end*/
269
270/*[Arima_8910][jhchen] add fuse check property 20181031 begin*/
271#ifdef ENABLE_FUSE_CHECK
272 int is_fused = 0;
273#endif
274/*[Arima_8910][jhchen] 20181031 end*/
275
vijay kumarc65876c2015-04-24 13:29:16 +0530276static bool is_allow_unlock = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -0700277
vijay kumarca2e6812015-07-08 20:28:25 +0530278static char frp_ptns[2][8] = {"config","frp"};
279
lijuang511a2b52015-08-14 20:50:51 +0800280static const char *critical_flash_allowed_ptn[] = {
281 "aboot",
282 "rpm",
283 "tz",
284 "sbl",
285 "sdi",
286 "sbl1",
287 "xbl",
288 "hyp",
289 "pmic",
290 "bootloader",
291 "devinfo",
292 "partition"};
293
Dima Zavin42168f22009-01-30 11:52:22 -0800294struct atag_ptbl_entry
295{
296 char name[16];
297 unsigned offset;
298 unsigned size;
299 unsigned flags;
300};
301
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700302/*
303 * Partition info, required to be published
304 * for fastboot
305 */
306struct getvar_partition_info {
Mayank Grover7b8a8f82017-10-27 13:07:59 +0530307 char part_name[MAX_GPT_NAME_SIZE]; /* Partition name */
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700308 char getvar_size[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for size */
309 char getvar_type[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for type */
310 char size_response[MAX_RSP_SIZE]; /* fastboot response for size */
311 char type_response[MAX_RSP_SIZE]; /* fastboot response for type */
312};
313
314/*
Mayank Grover7b8a8f82017-10-27 13:07:59 +0530315 * Update the part_type_known for known paritions types.
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700316 */
Mayank Groverd38fe012018-03-13 15:33:16 +0530317#define RAW_STR "raw"
Mayank Grover52cd10a2018-03-15 12:57:54 +0530318#define EXT_STR "ext4"
319#define F2FS_STR "f2fs"
320
321#define FS_SUPERBLOCK_OFFSET 0x400
322#define EXT_MAGIC 0xEF53
323#define EXT_MAGIC_OFFSET_SB 0x38
324#define F2FS_MAGIC 0xF2F52010 // F2FS Magic Number
325#define F2FS_MAGIC_OFFSET_SB 0x0
326
327typedef enum fs_signature_type {
328 EXT_FS_SIGNATURE = 1,
329 EXT_F2FS_SIGNATURE = 2,
330 NO_FS = -1
331} fs_signature_type;
332
Mayank Grover7b8a8f82017-10-27 13:07:59 +0530333struct getvar_partition_info part_info[NUM_PARTITIONS];
334struct getvar_partition_info part_type_known[] =
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700335{
Mayank Grover49920212018-02-09 18:15:55 +0530336 { "system" , "partition-size:", "partition-type:", "", "ext4" },
337 { "userdata" , "partition-size:", "partition-type:", "", "ext4" },
338 { "cache" , "partition-size:", "partition-type:", "", "ext4" },
339 { "recoveryfs" , "partition-size:", "partition-type:", "", "ext4" },
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700340};
341
342char max_download_size[MAX_RSP_SIZE];
Ameya Thakur11cf1a62013-08-05 12:44:48 -0700343char charger_screen_enabled[MAX_RSP_SIZE];
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800344char sn_buf[13];
Dhaval Patel223ec952013-07-18 14:49:44 -0700345char display_panel_buf[MAX_PANEL_BUF_SIZE];
Unnati Gandhi62c8ab82014-01-24 11:01:01 +0530346char panel_display_mode[MAX_RSP_SIZE];
Mayank Grovera64dfbe2018-05-17 14:06:34 +0530347char soc_version_str[MAX_RSP_SIZE];
Mayank Grover7b8a8f82017-10-27 13:07:59 +0530348char block_size_string[MAX_RSP_SIZE];
Mukesh Ojhabb6e7f32018-03-02 15:04:06 +0530349#if PRODUCT_IOT
Mayank Grover7b8a8f82017-10-27 13:07:59 +0530350
351/* For IOT we are using custom version */
352#define PRODUCT_IOT_VERSION "IOT001"
353char bootloader_version_string[MAX_RSP_SIZE];
354#endif
lijuang102dfa92015-10-09 18:31:03 +0800355
356#if CHECK_BAT_VOLTAGE
lijuang65c5a822015-08-29 16:35:36 +0800357char battery_voltage[MAX_RSP_SIZE];
lijuang102dfa92015-10-09 18:31:03 +0800358char battery_soc_ok [MAX_RSP_SIZE];
359#endif
360
lijuangf16461c2015-08-03 17:09:34 +0800361char get_variant[MAX_RSP_SIZE];
Greg Griscod6250552011-06-29 14:40:23 -0700362
Greg Griscod2471ef2011-07-14 13:00:42 -0700363extern int emmc_recovery_init(void);
364
Kinson Chik0b1c8162011-08-31 16:31:57 -0700365#if NO_KEYPAD_DRIVER
366extern int fastboot_trigger(void);
367#endif
Greg Griscod2471ef2011-07-14 13:00:42 -0700368
Mayank Grovere1ab96c2018-09-04 20:31:31 +0530369static void update_ker_tags_rdisk_addr(boot_img_hdr *hdr, bool is_arm64)
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700370{
371 /* overwrite the destination of specified for the project */
Channagoud Kadabi7042fa32013-04-26 16:44:14 -0700372#ifdef ABOOT_IGNORE_BOOT_HEADER_ADDRS
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800373 if (is_arm64)
374 hdr->kernel_addr = ABOOT_FORCE_KERNEL64_ADDR;
375 else
376 hdr->kernel_addr = ABOOT_FORCE_KERNEL_ADDR;
Channagoud Kadabi7042fa32013-04-26 16:44:14 -0700377 hdr->ramdisk_addr = ABOOT_FORCE_RAMDISK_ADDR;
378 hdr->tags_addr = ABOOT_FORCE_TAGS_ADDR;
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700379#endif
380}
381
Dima Zavin42168f22009-01-30 11:52:22 -0800382static void ptentry_to_tag(unsigned **ptr, struct ptentry *ptn)
383{
384 struct atag_ptbl_entry atag_ptn;
385
386 memcpy(atag_ptn.name, ptn->name, 16);
387 atag_ptn.name[15] = '\0';
388 atag_ptn.offset = ptn->start;
389 atag_ptn.size = ptn->length;
390 atag_ptn.flags = ptn->flags;
391 memcpy(*ptr, &atag_ptn, sizeof(struct atag_ptbl_entry));
392 *ptr += sizeof(struct atag_ptbl_entry) / sizeof(unsigned);
393}
Mayank Grover9df84c02018-08-30 15:46:35 +0530394#ifdef VERIFIED_BOOT_2
395void load_vbmeta_image(void **vbmeta_image_buf, uint32_t *vbmeta_image_sz)
396{
397 int index = 0;
398 char *vbm_img_buf = NULL;
399 unsigned long long ptn = 0;
400 unsigned long long ptn_size = 0;
401
402 /* Immediately return if dtbo is not supported */
403 index = partition_get_index("vbmeta");
404 ptn = partition_get_offset(index);
405 if(!ptn)
406 {
407 dprintf(CRITICAL, "ERROR: vbmeta partition not found.\n");
408 return;
409 }
410
411 ptn_size = partition_get_size(index);
412 if (ptn_size > MAX_SUPPORTED_VBMETA_IMG_BUF)
413 {
414 dprintf(CRITICAL, "ERROR: vbmeta parition size is greater than supported.\n");
415 return;
416 }
417
418 vbm_img_buf = (char *)memalign(CACHE_LINE, ROUNDUP((uint32_t)ptn_size, CACHE_LINE));
419 if (!vbm_img_buf)
420 {
421 dprintf(CRITICAL, "ERROR: vbmeta unable to locate buffer\n");
422 return;
423 }
424
425 mmc_set_lun(partition_get_lun(index));
426 if (mmc_read(ptn, (uint32_t *)vbm_img_buf, (uint32_t)ptn_size))
427 {
428 dprintf(CRITICAL, "ERROR: vbmeta read failure\n");
429 free(vbm_img_buf);
430 return;
431 }
432
433 *vbmeta_image_buf = vbm_img_buf;
434 *vbmeta_image_sz = (uint32_t)ptn_size;
435 return;
436}
437#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -0800438
lijuang102dfa92015-10-09 18:31:03 +0800439#if CHECK_BAT_VOLTAGE
440void update_battery_status(void)
441{
442 snprintf(battery_voltage,MAX_RSP_SIZE, "%d",target_get_battery_voltage());
443 snprintf(battery_soc_ok ,MAX_RSP_SIZE, "%s",target_battery_soc_ok()? "yes":"no");
444}
445#endif
446
Neeti Desaie245d492012-06-01 12:52:13 -0700447unsigned char *update_cmdline(const char * cmdline)
Brian Swetland9c4c0752009-01-25 16:23:50 -0800448{
David Ng183a7422009-12-07 14:55:21 -0800449 int cmdline_len = 0;
450 int have_cmdline = 0;
Amol Jadi168b7712012-03-06 16:15:00 -0800451 unsigned char *cmdline_final = NULL;
Neeti Desaie245d492012-06-01 12:52:13 -0700452 int pause_at_bootup = 0;
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800453 bool warm_boot = false;
Pavel Nedev5614d222013-06-17 18:01:02 +0300454 bool gpt_exists = partition_gpt_exists();
Joonwoo Park61112782013-10-02 19:50:39 -0700455 int have_target_boot_params = 0;
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700456 char *boot_dev_buf = NULL;
Monika Singh292b3e92018-03-17 22:40:23 +0530457#ifdef MDTP_SUPPORT
Mayank Grover351a75e2017-05-30 20:06:08 +0530458 bool is_mdtp_activated = 0;
Monika Singh292b3e92018-03-17 22:40:23 +0530459#endif
Mayank Grover351a75e2017-05-30 20:06:08 +0530460 int current_active_slot = INVALID;
Mayank Grover3804be72017-06-22 11:59:23 +0530461 int system_ptn_index = -1;
462 unsigned int lun = 0;
463 char lun_char_base = 'a';
Sourabh Banerjee386b1322018-02-27 09:37:28 +0530464#if VERITY_LE
465 int syspath_buflen = strlen(verity_dev)
466 + strlen(verity_system_part) + (sizeof(char) * 2) + 2
467 + strlen(verity_params) + sizeof(int) + 2;
468#else
lijuang83ef4b22018-08-23 11:01:55 +0800469 int syspath_buflen = strlen(sys_path) + sizeof(int) + 2; /*allocate buflen for largest possible string*/
470 char dtbo_idx_str[MAX_DTBO_IDX_STR] = "\0";
471 int dtbo_idx = INVALID_PTN;
Parth Dixit1375d9e2019-07-08 20:56:13 +0530472 char dtb_idx_str[MAX_DTB_IDX_STR] = "\0";
473 int dtb_idx = INVALID_PTN;
Sourabh Banerjee386b1322018-02-27 09:37:28 +0530474#endif
Mayank Grover3804be72017-06-22 11:59:23 +0530475 char syspath_buf[syspath_buflen];
Mayank Grover466d6562018-05-10 14:52:20 +0530476#if HIBERNATION_SUPPORT
477 int resume_buflen = strlen(resume) + sizeof(int) + 2;
478 char resume_buf[resume_buflen];
479 int swap_ptn_index = INVALID_PTN;
480#endif
481
Mayank Grover889be1b2017-09-12 20:12:23 +0530482#if VERIFIED_BOOT
483 uint32_t boot_state = RED;
484#endif
Hareesh Gauthamabf1e1e2017-04-12 18:09:24 +0530485
486#if USE_LE_SYSTEMD
487 is_systemd_present=true;
488#endif
489
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700490#if VERIFIED_BOOT
Mayank Grover912eaa62017-10-26 12:08:53 +0530491 if (VB_M <= target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +0530492 {
493 boot_state = boot_verify_get_state();
494 }
Parth Dixitddbc7352015-10-18 03:13:31 +0530495#endif
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700496
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200497#ifdef MDTP_SUPPORT
498 mdtp_activated(&is_mdtp_activated);
499#endif /* MDTP_SUPPORT */
Dima Zavin42168f22009-01-30 11:52:22 -0800500
Brian Swetland9c4c0752009-01-25 16:23:50 -0800501 if (cmdline && cmdline[0]) {
David Ng183a7422009-12-07 14:55:21 -0800502 cmdline_len = strlen(cmdline);
503 have_cmdline = 1;
504 }
505 if (target_is_emmc_boot()) {
506 cmdline_len += strlen(emmc_cmdline);
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700507 boot_dev_buf = (char *) malloc(sizeof(char) * BOOT_DEV_MAX_LEN);
Mayank Groverb716edf2019-05-08 16:06:55 +0530508 if (!boot_dev_buf) {
509 dprintf(CRITICAL, "ERROR: Failed to allocate boot_dev_buf\n");
510 } else {
511 platform_boot_dev_cmdline(boot_dev_buf);
512#if USE_BOOTDEV_CMDLINE
513 cmdline_len += strlen(boot_dev_buf);
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700514#endif
Mayank Groverb716edf2019-05-08 16:06:55 +0530515 if (target_dynamic_partition_supported()) {
516 cmdline_len += strlen(dynamic_bootdev_cmdline);
517 cmdline_len += strlen(boot_dev_buf);
518 }
519 }
David Ng183a7422009-12-07 14:55:21 -0800520 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800521
522 cmdline_len += strlen(usb_sn_cmdline);
523 cmdline_len += strlen(sn_buf);
524
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700525#if VERIFIED_BOOT
Mayank Grover912eaa62017-10-26 12:08:53 +0530526 if (VB_M <= target_get_vb_version())
Sridhar Parasuram7d8bb9a2015-09-23 18:21:19 -0700527 {
Mayank Grover889be1b2017-09-12 20:12:23 +0530528 cmdline_len += strlen(verified_state) + strlen(vbsn[boot_state].name);
529 if ((device.verity_mode != 0 ) && (device.verity_mode != 1))
530 {
531 dprintf(CRITICAL, "Devinfo paritition possibly corrupted!!!. Please erase devinfo partition to continue booting\n");
532 ASSERT(0);
533 }
534 cmdline_len += strlen(verity_mode) + strlen(vbvm[device.verity_mode].name);
535 cmdline_len += strlen(keymaster_v1);
Sridhar Parasuram7d8bb9a2015-09-23 18:21:19 -0700536 }
Parth Dixitddbc7352015-10-18 03:13:31 +0530537#endif
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700538
Monika Singh292b3e92018-03-17 22:40:23 +0530539
540 if (vbcmdline != NULL) {
541 dprintf(DEBUG, "UpdateCmdLine vbcmdline present len %d\n",
542 strlen(vbcmdline));
543 cmdline_len += strlen(vbcmdline);
544 }
545
Pavel Nedev5614d222013-06-17 18:01:02 +0300546 if (boot_into_recovery && gpt_exists)
547 cmdline_len += strlen(secondary_gpt_enable);
548
Monika Singh292b3e92018-03-17 22:40:23 +0530549#ifdef MDTP_SUPPORT
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200550 if(is_mdtp_activated)
551 cmdline_len += strlen(mdtp_activated_flag);
Monika Singh292b3e92018-03-17 22:40:23 +0530552#endif
Pavel Nedev328ac822013-04-05 15:25:11 +0300553 if (boot_into_ffbm) {
554 cmdline_len += strlen(androidboot_mode);
Hareesh Gauthamabf1e1e2017-04-12 18:09:24 +0530555
556 if(is_systemd_present)
557 cmdline_len += strlen(systemd_ffbm_mode);
558
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700559 cmdline_len += strlen(ffbm_mode_string);
Pavel Nedev898298c2013-02-27 12:36:09 -0800560 /* reduce kernel console messages to speed-up boot */
561 cmdline_len += strlen(loglevel);
Matthew Qind886f3c2014-01-17 16:52:01 +0800562 } else if (boot_reason_alarm) {
563 cmdline_len += strlen(alarmboot_cmdline);
Zhenhua Huang431dafa2015-06-30 16:13:37 +0800564 } else if ((target_build_variant_user() || device.charger_screen_enabled)
lijuang266b17d2018-08-17 18:53:42 +0800565 && target_pause_for_battery_charge() && !boot_into_recovery) {
David Ngf773dde2010-07-26 19:55:08 -0700566 pause_at_bootup = 1;
567 cmdline_len += strlen(battchg_pause);
568 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800569
Shashank Mittalcd98d472011-08-02 14:29:24 -0700570 if(target_use_signed_kernel() && auth_kernel_img) {
571 cmdline_len += strlen(auth_kernel);
572 }
573
Joonwoo Park61112782013-10-02 19:50:39 -0700574 if (get_target_boot_params(cmdline, boot_into_recovery ? "recoveryfs" :
575 "system",
vijay kumar870515d2015-08-31 16:37:24 +0530576 &target_boot_params) == 0) {
Joonwoo Park61112782013-10-02 19:50:39 -0700577 have_target_boot_params = 1;
578 cmdline_len += strlen(target_boot_params);
579 }
580
Ajay Dudanid04110c2011-01-17 23:55:07 -0800581 /* Determine correct androidboot.baseband to use */
582 switch(target_baseband())
583 {
584 case BASEBAND_APQ:
585 cmdline_len += strlen(baseband_apq);
586 break;
587
588 case BASEBAND_MSM:
589 cmdline_len += strlen(baseband_msm);
590 break;
591
592 case BASEBAND_CSFB:
593 cmdline_len += strlen(baseband_csfb);
594 break;
595
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800596 case BASEBAND_SVLTE2A:
597 cmdline_len += strlen(baseband_svlte2a);
Ajay Dudanid04110c2011-01-17 23:55:07 -0800598 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700599
600 case BASEBAND_MDM:
601 cmdline_len += strlen(baseband_mdm);
602 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700603
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800604 case BASEBAND_MDM2:
605 cmdline_len += strlen(baseband_mdm2);
606 break;
607
Amol Jadi5c61a952012-05-04 17:05:35 -0700608 case BASEBAND_SGLTE:
609 cmdline_len += strlen(baseband_sglte);
610 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530611
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800612 case BASEBAND_SGLTE2:
613 cmdline_len += strlen(baseband_sglte2);
614 break;
615
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530616 case BASEBAND_DSDA:
617 cmdline_len += strlen(baseband_dsda);
618 break;
Amol Jadi2a15a272013-01-22 12:03:36 -0800619
620 case BASEBAND_DSDA2:
621 cmdline_len += strlen(baseband_dsda2);
622 break;
Vijay Kumar Pendotib228cfc2016-06-13 20:15:23 +0530623 case BASEBAND_APQ_NOWGR:
624 cmdline_len += strlen(baseband_apq_nowgr);
625 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800626 }
627
Vladislav Levenetz3cd45242017-04-05 08:58:07 +0300628#if ENABLE_DISPLAY
Lijuan Gao4a5b8322014-07-24 10:38:42 +0800629 if (cmdline) {
630 if ((strstr(cmdline, DISPLAY_DEFAULT_PREFIX) == NULL) &&
Padmanabhan Komandurubccbcdc2015-06-30 16:19:24 +0530631 target_display_panel_node(display_panel_buf,
632 MAX_PANEL_BUF_SIZE) &&
Lijuan Gao4a5b8322014-07-24 10:38:42 +0800633 strlen(display_panel_buf)) {
634 cmdline_len += strlen(display_panel_buf);
635 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700636 }
Vladislav Levenetz3cd45242017-04-05 08:58:07 +0300637#endif
Dhaval Patel223ec952013-07-18 14:49:44 -0700638
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800639 if (target_warm_boot()) {
640 warm_boot = true;
641 cmdline_len += strlen(warmboot_cmdline);
642 }
643
Mayank Grover5384ed82018-05-09 12:09:24 +0530644 if (target_uses_system_as_root() ||
645 partition_multislot_is_supported())
Mayank Grover351a75e2017-05-30 20:06:08 +0530646 {
647 current_active_slot = partition_find_active_slot();
648 cmdline_len += (strlen(androidboot_slot_suffix)+
649 strlen(SUFFIX_SLOT(current_active_slot)));
650
Mayank Grover3804be72017-06-22 11:59:23 +0530651 system_ptn_index = partition_get_index("system");
652 if (platform_boot_dev_isemmc())
653 {
Sourabh Banerjee386b1322018-02-27 09:37:28 +0530654#if VERITY_LE
655 /*
656 Condition 4: Verity and A/B both enabled
657 Eventual command line looks like:
658 ... androidboot.slot_suffix=<slot_suffix> ... rootfstype=ext4 ...
659 ... root=/dev/dm-0 dm="system_<slot_suffix> none ro,0 1 android-verity /dev/mmcblk0p<NN>"
660 */
661 snprintf(syspath_buf, syspath_buflen, " %s %s%s %s%d\"",
662 verity_dev,
663 verity_system_part, suffix_slot[current_active_slot],
664 verity_params, system_ptn_index + 1);
665#else
666 /*
667 Condition 5: A/B enabled, but verity disabled
668 Eventual command line looks like:
669 ... androidboot.slot_suffix=<slot_suffix> ... rootfstype=ext4 ...
670 ... root=/dev/mmcblk0p<NN> ...
671 */
672 snprintf(syspath_buf, syspath_buflen, " %s%d",
673 sys_path, system_ptn_index + 1);
674#endif
Mayank Grover3804be72017-06-22 11:59:23 +0530675 }
676 else
677 {
678 lun = partition_get_lun(system_ptn_index);
679 snprintf(syspath_buf, syspath_buflen, " root=/dev/sd%c%d",
680 lun_char_base + lun,
681 partition_get_index_in_lun("system", lun));
682 }
Mayank Grover351a75e2017-05-30 20:06:08 +0530683
Monika Singh292b3e92018-03-17 22:40:23 +0530684#ifndef VERIFIED_BOOT_2
Mayank Grover3804be72017-06-22 11:59:23 +0530685 cmdline_len += strlen(syspath_buf);
Monika Singh292b3e92018-03-17 22:40:23 +0530686#endif
Mayank Grover5384ed82018-05-09 12:09:24 +0530687 }
688
689 if (target_uses_system_as_root() ||
690 partition_multislot_is_supported())
691 {
692 cmdline_len += strlen(sys_path_cmdline);
Mayank Groverb716edf2019-05-08 16:06:55 +0530693
694 /* For dynamic partition, support skip skip_initramfs */
695 if (!target_dynamic_partition_supported() &&
696 !boot_into_recovery)
Mayank Grover351a75e2017-05-30 20:06:08 +0530697 cmdline_len += strlen(skip_ramfs);
698 }
699
Mayank Grover466d6562018-05-10 14:52:20 +0530700#if HIBERNATION_SUPPORT
701 if (platform_boot_dev_isemmc())
702 {
703 swap_ptn_index = partition_get_index("swap");
704 if (swap_ptn_index != INVALID_PTN)
705 {
706 snprintf(resume_buf, resume_buflen,
707 " %s%d", resume,
708 (swap_ptn_index + 1));
709 cmdline_len += strlen(resume_buf);
710 }
711 else
712 {
713 dprintf(INFO, "WARNING: swap partition not found\n");
714 }
715 }
716#endif
717
Channagoud Kadabi85c7ec32016-01-28 23:09:21 -0800718#if TARGET_CMDLINE_SUPPORT
719 char *target_cmdline_buf = malloc(TARGET_MAX_CMDLNBUF);
720 int target_cmd_line_len;
721 ASSERT(target_cmdline_buf);
722 target_cmd_line_len = target_update_cmdline(target_cmdline_buf);
723 cmdline_len += target_cmd_line_len;
724#endif
725
lijuang83ef4b22018-08-23 11:01:55 +0800726#if !VERITY_LE
727 dtbo_idx = get_dtbo_idx ();
728 if (dtbo_idx != INVALID_PTN) {
729 snprintf(dtbo_idx_str, sizeof(dtbo_idx_str), "%s%d",
730 android_boot_dtbo_idx, dtbo_idx);
731 cmdline_len += strlen (dtbo_idx_str);
732 }
Parth Dixit1375d9e2019-07-08 20:56:13 +0530733
734 dtb_idx = get_dtb_idx ();
735 if (dtb_idx != INVALID_PTN) {
736 snprintf(dtb_idx_str, sizeof(dtb_idx_str), "%s%d",
737 android_boot_dtb_idx, dtb_idx);
738 cmdline_len += strlen (dtb_idx_str);
739 }
lijuang83ef4b22018-08-23 11:01:55 +0800740#endif
741
jhchen7a504d12020-04-24 15:45:57 +0800742/*[Arima_8910][jhchen] add fuse check property 20181031 begin*/
743#ifdef ENABLE_FUSE_CHECK
744 {
745 int hw_key_status = readl(0xA01D0);
746 is_fused = (hw_key_status & 0x2) >> 1;
747 dprintf(CRITICAL, "hw_key_status = 0x%x, is_fused=%d\n", hw_key_status, is_fused);
748 if (is_fused) {
749 cmdline_len += strlen(fuse_blown);
750 } else {
751 cmdline_len += strlen(fuse_not_blown);
752 }
753 }
754#endif
755/*[Arima_8910][jhchen] 20181031 end*/
756
David Ng183a7422009-12-07 14:55:21 -0800757 if (cmdline_len > 0) {
758 const char *src;
Maria Yu52254c02014-07-04 16:14:54 +0800759 unsigned char *dst;
760
761 cmdline_final = (unsigned char*) malloc((cmdline_len + 4) & (~3));
762 ASSERT(cmdline_final != NULL);
vijay kumar287bb542015-09-29 13:01:52 +0530763 memset((void *)cmdline_final, 0, sizeof(*cmdline_final));
Maria Yu52254c02014-07-04 16:14:54 +0800764 dst = cmdline_final;
Neeti Desaie245d492012-06-01 12:52:13 -0700765
Amol Jadi168b7712012-03-06 16:15:00 -0800766 /* Save start ptr for debug print */
David Ng183a7422009-12-07 14:55:21 -0800767 if (have_cmdline) {
768 src = cmdline;
769 while ((*dst++ = *src++));
770 }
771 if (target_is_emmc_boot()) {
772 src = emmc_cmdline;
773 if (have_cmdline) --dst;
David Ngf773dde2010-07-26 19:55:08 -0700774 have_cmdline = 1;
775 while ((*dst++ = *src++));
Sridhar Parasuram7bd4aaf2015-02-12 11:14:38 -0800776#if USE_BOOTDEV_CMDLINE
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700777 src = boot_dev_buf;
Mayank Groverb716edf2019-05-08 16:06:55 +0530778 if (have_cmdline &&
779 boot_dev_buf) {
780 --dst;
781 while ((*dst++ = *src++));
782 }
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700783#endif
Mayank Groverb716edf2019-05-08 16:06:55 +0530784 /* Dynamic partition append boot_devices */
785 if (target_dynamic_partition_supported() &&
786 boot_dev_buf) {
787 src = dynamic_bootdev_cmdline;
788 if (have_cmdline) --dst;
789 while ((*dst++ = *src++));
790 src = boot_dev_buf;
791 if (have_cmdline) --dst;
792 while ((*dst++ = *src++));
793 }
David Ngf773dde2010-07-26 19:55:08 -0700794 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800795
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700796#if VERIFIED_BOOT
Mayank Grover912eaa62017-10-26 12:08:53 +0530797 if (VB_M <= target_get_vb_version())
Sridhar Parasuram7d8bb9a2015-09-23 18:21:19 -0700798 {
Mayank Grover889be1b2017-09-12 20:12:23 +0530799 src = verified_state;
800 if(have_cmdline) --dst;
801 have_cmdline = 1;
802 while ((*dst++ = *src++));
803 src = vbsn[boot_state].name;
804 if(have_cmdline) --dst;
805 while ((*dst++ = *src++));
806
807 if ((device.verity_mode != 0 ) && (device.verity_mode != 1))
808 {
809 dprintf(CRITICAL, "Devinfo paritition possibly corrupted!!!. Please erase devinfo partition to continue booting\n");
810 ASSERT(0);
811 }
812 src = verity_mode;
813 if(have_cmdline) --dst;
814 while ((*dst++ = *src++));
815 src = vbvm[device.verity_mode].name;
816 if(have_cmdline) -- dst;
817 while ((*dst++ = *src++));
818 src = keymaster_v1;
819 if(have_cmdline) --dst;
820 while ((*dst++ = *src++));
Sridhar Parasuram7d8bb9a2015-09-23 18:21:19 -0700821 }
Parth Dixitddbc7352015-10-18 03:13:31 +0530822#endif
Monika Singh292b3e92018-03-17 22:40:23 +0530823
824 if (vbcmdline != NULL) {
825 src = vbcmdline;
826 if (have_cmdline) --dst;
827 while ((*dst++ = *src++));
828 }
829
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800830 src = usb_sn_cmdline;
831 if (have_cmdline) --dst;
832 have_cmdline = 1;
833 while ((*dst++ = *src++));
834 src = sn_buf;
835 if (have_cmdline) --dst;
836 have_cmdline = 1;
837 while ((*dst++ = *src++));
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800838 if (warm_boot) {
839 if (have_cmdline) --dst;
840 src = warmboot_cmdline;
841 while ((*dst++ = *src++));
842 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800843
Pavel Nedev5614d222013-06-17 18:01:02 +0300844 if (boot_into_recovery && gpt_exists) {
845 src = secondary_gpt_enable;
846 if (have_cmdline) --dst;
847 while ((*dst++ = *src++));
848 }
Monika Singh292b3e92018-03-17 22:40:23 +0530849#ifdef MDTP_SUPPORT
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200850 if (is_mdtp_activated) {
851 src = mdtp_activated_flag;
852 if (have_cmdline) --dst;
853 while ((*dst++ = *src++));
854 }
Monika Singh292b3e92018-03-17 22:40:23 +0530855#endif
Pavel Nedev328ac822013-04-05 15:25:11 +0300856 if (boot_into_ffbm) {
857 src = androidboot_mode;
858 if (have_cmdline) --dst;
859 while ((*dst++ = *src++));
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700860 src = ffbm_mode_string;
Pavel Nedev328ac822013-04-05 15:25:11 +0300861 if (have_cmdline) --dst;
862 while ((*dst++ = *src++));
Hareesh Gauthamabf1e1e2017-04-12 18:09:24 +0530863
864 if(is_systemd_present) {
865 src = systemd_ffbm_mode;
866 if (have_cmdline) --dst;
867 while ((*dst++ = *src++));
868 }
869
Pavel Nedev898298c2013-02-27 12:36:09 -0800870 src = loglevel;
871 if (have_cmdline) --dst;
872 while ((*dst++ = *src++));
Matthew Qind886f3c2014-01-17 16:52:01 +0800873 } else if (boot_reason_alarm) {
874 src = alarmboot_cmdline;
875 if (have_cmdline) --dst;
876 while ((*dst++ = *src++));
Pavel Nedev328ac822013-04-05 15:25:11 +0300877 } else if (pause_at_bootup) {
David Ngf773dde2010-07-26 19:55:08 -0700878 src = battchg_pause;
879 if (have_cmdline) --dst;
David Ng183a7422009-12-07 14:55:21 -0800880 while ((*dst++ = *src++));
881 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800882
Shashank Mittalcd98d472011-08-02 14:29:24 -0700883 if(target_use_signed_kernel() && auth_kernel_img) {
884 src = auth_kernel;
885 if (have_cmdline) --dst;
886 while ((*dst++ = *src++));
887 }
888
Ajay Dudanid04110c2011-01-17 23:55:07 -0800889 switch(target_baseband())
890 {
891 case BASEBAND_APQ:
892 src = baseband_apq;
893 if (have_cmdline) --dst;
894 while ((*dst++ = *src++));
895 break;
896
897 case BASEBAND_MSM:
898 src = baseband_msm;
899 if (have_cmdline) --dst;
900 while ((*dst++ = *src++));
901 break;
902
903 case BASEBAND_CSFB:
904 src = baseband_csfb;
905 if (have_cmdline) --dst;
906 while ((*dst++ = *src++));
907 break;
908
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800909 case BASEBAND_SVLTE2A:
910 src = baseband_svlte2a;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800911 if (have_cmdline) --dst;
912 while ((*dst++ = *src++));
913 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700914
915 case BASEBAND_MDM:
916 src = baseband_mdm;
917 if (have_cmdline) --dst;
918 while ((*dst++ = *src++));
919 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700920
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800921 case BASEBAND_MDM2:
922 src = baseband_mdm2;
923 if (have_cmdline) --dst;
924 while ((*dst++ = *src++));
925 break;
926
Amol Jadi5c61a952012-05-04 17:05:35 -0700927 case BASEBAND_SGLTE:
928 src = baseband_sglte;
929 if (have_cmdline) --dst;
930 while ((*dst++ = *src++));
931 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530932
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800933 case BASEBAND_SGLTE2:
934 src = baseband_sglte2;
935 if (have_cmdline) --dst;
936 while ((*dst++ = *src++));
937 break;
938
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530939 case BASEBAND_DSDA:
940 src = baseband_dsda;
941 if (have_cmdline) --dst;
942 while ((*dst++ = *src++));
943 break;
Amol Jadi2a15a272013-01-22 12:03:36 -0800944
945 case BASEBAND_DSDA2:
946 src = baseband_dsda2;
947 if (have_cmdline) --dst;
948 while ((*dst++ = *src++));
949 break;
Vijay Kumar Pendotib228cfc2016-06-13 20:15:23 +0530950 case BASEBAND_APQ_NOWGR:
951 src = baseband_apq_nowgr;
952 if (have_cmdline) --dst;
953 while ((*dst++ = *src++));
954 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800955 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700956
957 if (strlen(display_panel_buf)) {
Dhaval Patel223ec952013-07-18 14:49:44 -0700958 src = display_panel_buf;
959 if (have_cmdline) --dst;
960 while ((*dst++ = *src++));
961 }
Joonwoo Park61112782013-10-02 19:50:39 -0700962
963 if (have_target_boot_params) {
964 if (have_cmdline) --dst;
965 src = target_boot_params;
966 while ((*dst++ = *src++));
vijay kumar870515d2015-08-31 16:37:24 +0530967 free(target_boot_params);
Joonwoo Park61112782013-10-02 19:50:39 -0700968 }
Channagoud Kadabi85c7ec32016-01-28 23:09:21 -0800969
Mayank Grover351a75e2017-05-30 20:06:08 +0530970 if (partition_multislot_is_supported() && have_cmdline)
971 {
972 src = androidboot_slot_suffix;
973 --dst;
974 while ((*dst++ = *src++));
975 --dst;
976 src = SUFFIX_SLOT(current_active_slot);
977 while ((*dst++ = *src++));
Mayank Grover5384ed82018-05-09 12:09:24 +0530978 }
Mayank Grover351a75e2017-05-30 20:06:08 +0530979
Mayank Grover351a75e2017-05-30 20:06:08 +0530980
Mayank Grover5384ed82018-05-09 12:09:24 +0530981 /*
982 * System-As-Root behaviour, system.img should contain both
983 * system content and ramdisk content, and should be mounted at
984 * root(a/b).
985 * Apending skip_ramfs for non a/b builds which use, system as root.
986 */
987 if ((target_uses_system_as_root() ||
988 partition_multislot_is_supported()) &&
989 have_cmdline)
990 {
Mayank Groverb716edf2019-05-08 16:06:55 +0530991 if (!target_dynamic_partition_supported() &&
992 !boot_into_recovery)
Mayank Grover5384ed82018-05-09 12:09:24 +0530993 {
994 src = skip_ramfs;
Mayank Grover351a75e2017-05-30 20:06:08 +0530995 --dst;
996 while ((*dst++ = *src++));
Mayank Grover5384ed82018-05-09 12:09:24 +0530997 }
998
999 src = sys_path_cmdline;
1000 --dst;
1001 while ((*dst++ = *src++));
Mayank Grover3804be72017-06-22 11:59:23 +05301002
Monika Singh292b3e92018-03-17 22:40:23 +05301003#ifndef VERIFIED_BOOT_2
Mayank Grover5384ed82018-05-09 12:09:24 +05301004 src = syspath_buf;
1005 --dst;
1006 while ((*dst++ = *src++));
Monika Singh292b3e92018-03-17 22:40:23 +05301007#endif
Mayank Grover351a75e2017-05-30 20:06:08 +05301008 }
1009
Mayank Grover466d6562018-05-10 14:52:20 +05301010#if HIBERNATION_SUPPORT
1011 if (swap_ptn_index != INVALID_PTN)
1012 {
1013 src = resume_buf;
1014 --dst;
1015 while ((*dst++ = *src++));
1016 }
1017#endif
1018
Channagoud Kadabi85c7ec32016-01-28 23:09:21 -08001019#if TARGET_CMDLINE_SUPPORT
1020 if (target_cmdline_buf && target_cmd_line_len)
1021 {
1022 if (have_cmdline) --dst;
1023 src = target_cmdline_buf;
1024 while((*dst++ = *src++));
1025 free(target_cmdline_buf);
1026 }
1027#endif
lijuang83ef4b22018-08-23 11:01:55 +08001028
1029#if !VERITY_LE
1030 if (dtbo_idx != INVALID_PTN) {
1031 src = dtbo_idx_str;
1032 --dst;
1033 while ((*dst++ = *src++));
1034 }
Parth Dixit1375d9e2019-07-08 20:56:13 +05301035
1036 if (dtb_idx != INVALID_PTN) {
1037 src = dtb_idx_str;
1038 --dst;
1039 while ((*dst++ = *src++));
1040 }
lijuang83ef4b22018-08-23 11:01:55 +08001041#endif
jhchen7a504d12020-04-24 15:45:57 +08001042
1043/*[Arima_8910][jhchen] add fuse check property 20181031 begin*/
1044#ifdef ENABLE_FUSE_CHECK
1045 if (is_fused) {
1046 src = fuse_blown;
1047 if (have_cmdline) --dst;
1048 while ((*dst++ = *src++));
1049 } else {
1050 src = fuse_not_blown;
1051 if (have_cmdline) --dst;
1052 while ((*dst++ = *src++));
1053 }
1054#endif
1055/*[Arima_8910][jhchen] 20181031 end*/
Neeti Desaie245d492012-06-01 12:52:13 -07001056 }
Dhaval Patel223ec952013-07-18 14:49:44 -07001057
1058
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -07001059 if (boot_dev_buf)
1060 free(boot_dev_buf);
1061
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -08001062 if (cmdline_final)
1063 dprintf(INFO, "cmdline: %s\n", cmdline_final);
1064 else
1065 dprintf(INFO, "cmdline is NULL\n");
Neeti Desaie245d492012-06-01 12:52:13 -07001066 return cmdline_final;
1067}
1068
1069unsigned *atag_core(unsigned *ptr)
1070{
1071 /* CORE */
1072 *ptr++ = 2;
1073 *ptr++ = 0x54410001;
1074
1075 return ptr;
1076
1077}
1078
1079unsigned *atag_ramdisk(unsigned *ptr, void *ramdisk,
1080 unsigned ramdisk_size)
1081{
1082 if (ramdisk_size) {
1083 *ptr++ = 4;
1084 *ptr++ = 0x54420005;
1085 *ptr++ = (unsigned)ramdisk;
1086 *ptr++ = ramdisk_size;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001087 }
1088
Neeti Desaie245d492012-06-01 12:52:13 -07001089 return ptr;
1090}
1091
1092unsigned *atag_ptable(unsigned **ptr_addr)
1093{
1094 int i;
1095 struct ptable *ptable;
1096
1097 if ((ptable = flash_get_ptable()) && (ptable->count != 0)) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001098 *(*ptr_addr)++ = 2 + (ptable->count * (sizeof(struct atag_ptbl_entry) /
1099 sizeof(unsigned)));
Neeti Desaie245d492012-06-01 12:52:13 -07001100 *(*ptr_addr)++ = 0x4d534d70;
1101 for (i = 0; i < ptable->count; ++i)
1102 ptentry_to_tag(ptr_addr, ptable_get(ptable, i));
1103 }
1104
1105 return (*ptr_addr);
1106}
1107
1108unsigned *atag_cmdline(unsigned *ptr, const char *cmdline)
1109{
1110 int cmdline_length = 0;
1111 int n;
Neeti Desaie245d492012-06-01 12:52:13 -07001112 char *dest;
1113
Amol Jadi10c7d1c2013-01-25 13:24:29 -08001114 cmdline_length = strlen((const char*)cmdline);
Neeti Desaie245d492012-06-01 12:52:13 -07001115 n = (cmdline_length + 4) & (~3);
1116
1117 *ptr++ = (n / 4) + 2;
1118 *ptr++ = 0x54410009;
1119 dest = (char *) ptr;
Amol Jadi10c7d1c2013-01-25 13:24:29 -08001120 while ((*dest++ = *cmdline++));
Neeti Desaie245d492012-06-01 12:52:13 -07001121 ptr += (n / 4);
1122
1123 return ptr;
1124}
1125
1126unsigned *atag_end(unsigned *ptr)
1127{
Brian Swetland9c4c0752009-01-25 16:23:50 -08001128 /* END */
1129 *ptr++ = 0;
1130 *ptr++ = 0;
1131
Neeti Desaie245d492012-06-01 12:52:13 -07001132 return ptr;
1133}
1134
1135void generate_atags(unsigned *ptr, const char *cmdline,
1136 void *ramdisk, unsigned ramdisk_size)
1137{
vijay kumar21a37452015-12-29 16:23:21 +05301138 unsigned *orig_ptr = ptr;
Neeti Desaie245d492012-06-01 12:52:13 -07001139 ptr = atag_core(ptr);
1140 ptr = atag_ramdisk(ptr, ramdisk, ramdisk_size);
1141 ptr = target_atag_mem(ptr);
1142
1143 /* Skip NAND partition ATAGS for eMMC boot */
1144 if (!target_is_emmc_boot()){
1145 ptr = atag_ptable(&ptr);
1146 }
1147
vijay kumar21a37452015-12-29 16:23:21 +05301148 /*
1149 * Atags size filled till + cmdline size + 1 unsigned for 4-byte boundary + 4 unsigned
1150 * for atag identifier in atag_cmdline and atag_end should be with in MAX_TAGS_SIZE bytes
1151 */
Mayank Groverbc8a2ef2018-02-23 18:03:36 +05301152 if (!cmdline)
1153 return;
1154
vijay kumar21a37452015-12-29 16:23:21 +05301155 if (((ptr - orig_ptr) + strlen(cmdline) + 5 * sizeof(unsigned)) < MAX_TAGS_SIZE) {
1156 ptr = atag_cmdline(ptr, cmdline);
1157 ptr = atag_end(ptr);
1158 }
1159 else {
1160 dprintf(CRITICAL,"Crossing ATAGs Max size allowed\n");
1161 ASSERT(0);
1162 }
Neeti Desaie245d492012-06-01 12:52:13 -07001163}
1164
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001165typedef void entry_func_ptr(unsigned, unsigned, unsigned*);
Neeti Desaie245d492012-06-01 12:52:13 -07001166void boot_linux(void *kernel, unsigned *tags,
1167 const char *cmdline, unsigned machtype,
1168 void *ramdisk, unsigned ramdisk_size)
1169{
Amol Jadi10c7d1c2013-01-25 13:24:29 -08001170 unsigned char *final_cmdline;
Amol Jadib6be5c12012-11-14 13:39:51 -08001171#if DEVICE_TREE
Neeti Desai17379b82012-06-04 18:42:53 -07001172 int ret = 0;
Amol Jadib6be5c12012-11-14 13:39:51 -08001173#endif
1174
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001175 void (*entry)(unsigned, unsigned, unsigned*) = (entry_func_ptr*)(PA((addr_t)kernel));
Deepa Dinamani25a9f762012-11-30 15:57:15 -08001176 uint32_t tags_phys = PA((addr_t)tags);
vijay kumar1a50a642015-11-16 12:41:15 +05301177 struct kernel64_hdr *kptr = ((struct kernel64_hdr*)(PA((addr_t)kernel)));
Deepa Dinamani25a9f762012-11-30 15:57:15 -08001178
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301179 ramdisk = (void *)PA((addr_t)ramdisk);
Neeti Desaie245d492012-06-01 12:52:13 -07001180
Amol Jadi10c7d1c2013-01-25 13:24:29 -08001181 final_cmdline = update_cmdline((const char*)cmdline);
1182
Neeti Desai17379b82012-06-04 18:42:53 -07001183#if DEVICE_TREE
Amol Jadib6be5c12012-11-14 13:39:51 -08001184 dprintf(INFO, "Updating device tree: start\n");
1185
Neeti Desai17379b82012-06-04 18:42:53 -07001186 /* Update the Device Tree */
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301187 ret = update_device_tree((void *)tags,(const char *)final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -07001188 if(ret)
1189 {
1190 dprintf(CRITICAL, "ERROR: Updating Device Tree Failed \n");
1191 ASSERT(0);
1192 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001193 dprintf(INFO, "Updating device tree: done\n");
Neeti Desai17379b82012-06-04 18:42:53 -07001194#else
Neeti Desaie245d492012-06-01 12:52:13 -07001195 /* Generating the Atags */
Amol Jadi10c7d1c2013-01-25 13:24:29 -08001196 generate_atags(tags, final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -07001197#endif
Neeti Desaie245d492012-06-01 12:52:13 -07001198
Monika Singh0ec6db82019-07-18 16:57:58 +05301199#if VERIFIED_BOOT
Monika Singhb0db4b82018-09-26 12:18:02 +05301200 if (VB_M <= target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +05301201 {
1202 if (device.verity_mode == 0) {
lijuangbdd9bb42016-03-01 18:22:17 +08001203#if FBCON_DISPLAY_MSG
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07001204#if ENABLE_VB_ATTEST
Mayank Grover889be1b2017-09-12 20:12:23 +05301205 display_bootverify_menu(DISPLAY_MENU_EIO);
1206 wait_for_users_action();
1207 if(!pwr_key_is_pressed)
1208 shutdown_device();
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07001209#else
Mayank Grover889be1b2017-09-12 20:12:23 +05301210 display_bootverify_menu(DISPLAY_MENU_LOGGING);
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07001211#endif
Mayank Grover889be1b2017-09-12 20:12:23 +05301212 wait_for_users_action();
lijuangbdd9bb42016-03-01 18:22:17 +08001213#else
Mayank Grover889be1b2017-09-12 20:12:23 +05301214 dprintf(CRITICAL,
1215 "The dm-verity is not started in enforcing mode.\nWait for 5 seconds before proceeding\n");
1216 mdelay(5000);
lijuangbdd9bb42016-03-01 18:22:17 +08001217#endif
Mayank Grover889be1b2017-09-12 20:12:23 +05301218 }
lijuangbdd9bb42016-03-01 18:22:17 +08001219 }
lijuangbdd9bb42016-03-01 18:22:17 +08001220#endif
1221
1222#if VERIFIED_BOOT
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001223 /* Write protect the device info */
Channagoud Kadabi3bd9d1e2015-05-05 16:18:20 -07001224 if (!boot_into_recovery && target_build_variant_user() && devinfo_present && mmc_write_protect("devinfo", 1))
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001225 {
1226 dprintf(INFO, "Failed to write protect dev info\n");
1227 ASSERT(0);
1228 }
1229#endif
1230
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08001231 /* Turn off splash screen if enabled */
1232#if DISPLAY_SPLASH_SCREEN
1233 target_display_shutdown();
1234#endif
1235
Veera Sundaram Sankaran67ea0932015-09-25 10:09:30 -07001236 /* Perform target specific cleanup */
1237 target_uninit();
Monika Singh292b3e92018-03-17 22:40:23 +05301238 free_verified_boot_resource(&info);
1239 if (final_cmdline)
1240 free(final_cmdline);
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08001241
Deepa Dinamani33734bc2013-03-06 12:16:06 -08001242 dprintf(INFO, "booting linux @ %p, ramdisk @ %p (%d), tags/device tree @ %p\n",
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301243 entry, ramdisk, ramdisk_size, (void *)tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001244
1245 enter_critical_section();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001246
Amol Jadi4421e652011-06-16 15:00:48 -07001247 /* do any platform specific cleanup before kernel entry */
1248 platform_uninit();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001249
Brian Swetland9c4c0752009-01-25 16:23:50 -08001250 arch_disable_cache(UCACHE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001251
Amol Jadi504f9fe2012-08-16 13:56:48 -07001252#if ARM_WITH_MMU
Brian Swetland9c4c0752009-01-25 16:23:50 -08001253 arch_disable_mmu();
Amol Jadi504f9fe2012-08-16 13:56:48 -07001254#endif
Amol Jadi492d5a52013-03-15 16:12:34 -07001255 bs_set_timestamp(BS_KERNEL_ENTRY);
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001256
1257 if (IS_ARM64(kptr))
1258 /* Jump to a 64bit kernel */
1259 scm_elexec_call((paddr_t)kernel, tags_phys);
1260 else
1261 /* Jump to a 32bit kernel */
1262 entry(0, machtype, (unsigned*)tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001263}
1264
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001265/* Function to check if the memory address range falls within the aboot
1266 * boundaries.
1267 * start: Start of the memory region
1268 * size: Size of the memory region
1269 */
Vijay Kumar Pendoti9c002ad2016-03-09 13:52:45 +05301270int check_aboot_addr_range_overlap(uintptr_t start, uint32_t size)
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001271{
1272 /* Check for boundary conditions. */
Sundarajan Srinivasance2a0ea2013-12-16 17:02:56 -08001273 if ((UINT_MAX - start) < size)
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001274 return -1;
1275
1276 /* Check for memory overlap. */
1277 if ((start < MEMBASE) && ((start + size) <= MEMBASE))
1278 return 0;
Channagoud Kadabi94143912013-10-15 12:53:52 -07001279 else if (start >= (MEMBASE + MEMSIZE))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001280 return 0;
1281 else
1282 return -1;
1283}
1284
Mayank Grovere559cec2017-10-17 15:12:03 +05301285/* Function to check if the memory address range falls beyond ddr region.
1286 * start: Start of the memory region
1287 * size: Size of the memory region
1288 */
1289int check_ddr_addr_range_bound(uintptr_t start, uint32_t size)
1290{
1291 uintptr_t ddr_pa_start_addr = PA(get_ddr_start());
1292 uint64_t ddr_size = smem_get_ddr_size();
1293 uint64_t ddr_pa_end_addr = ddr_pa_start_addr + ddr_size;
1294 uintptr_t pa_start_addr = PA(start);
1295
1296 /* Check for boundary conditions. */
1297 if ((UINT_MAX - start) < size)
1298 return -1;
1299
1300 /* Check if memory range is beyond the ddr range. */
1301 if (pa_start_addr < ddr_pa_start_addr ||
1302 pa_start_addr >= (ddr_pa_end_addr) ||
1303 (pa_start_addr + size) > ddr_pa_end_addr)
1304 return -1;
1305 else
1306 return 0;
1307}
1308
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001309BUF_DMA_ALIGN(buf, BOOT_IMG_MAX_PAGE_SIZE); //Equal to max-supported pagesize
Dima Zavin214cc642009-01-26 11:16:21 -08001310
Mayank Groverdd080e82018-09-04 20:12:13 +05301311int getimage(void **image_buffer, uint32_t *imgsize,
1312 const char *imgname)
Monika Singh292b3e92018-03-17 22:40:23 +05301313{
Mayank Groverdd080e82018-09-04 20:12:13 +05301314 uint32_t loadedindex;
1315 if (image_buffer == NULL || imgsize == NULL ||
Monika Singh292b3e92018-03-17 22:40:23 +05301316 imgname == NULL) {
1317 dprintf(CRITICAL, "getimage: invalid parameters\n");
1318 return -1;
1319 }
Mayank Groverdd080e82018-09-04 20:12:13 +05301320 for (loadedindex = 0; loadedindex < info.num_loaded_images; loadedindex++) {
1321 if (!strncmp(info.images[loadedindex].name, imgname,
Monika Singh292b3e92018-03-17 22:40:23 +05301322 strlen(imgname))) {
Mayank Groverdd080e82018-09-04 20:12:13 +05301323 *image_buffer = info.images[loadedindex].image_buffer;
1324 *imgsize = info.images[loadedindex].imgsize;
Mayank Grover027a9412018-09-04 15:12:05 +05301325 dprintf(SPEW, "getimage(): Loaded image [%s|%d]\n",
1326 info.images[loadedindex].name,
1327 info.images[loadedindex].imgsize);
Monika Singh292b3e92018-03-17 22:40:23 +05301328 return 0;
1329 }
1330 }
1331 return -1;
1332}
1333
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001334static void verify_signed_bootimg(uint32_t bootimg_addr, uint32_t bootimg_size)
1335{
1336 int ret;
Channagoud Kadabia8c623f2015-01-13 14:48:48 -08001337
1338#if !VERIFIED_BOOT
Sundarajan Srinivasance54d6e2013-11-11 12:45:00 -08001339#if IMAGE_VERIF_ALGO_SHA1
1340 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA1;
1341#else
1342 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA256;
1343#endif
Channagoud Kadabia8c623f2015-01-13 14:48:48 -08001344#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001345
1346 /* Assume device is rooted at this time. */
1347 device.is_tampered = 1;
1348
1349 dprintf(INFO, "Authenticating boot image (%d): start\n", bootimg_size);
1350
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001351#if VERIFIED_BOOT
Monika Singh7d2fc272018-03-16 17:16:01 +05301352 uint32_t bootstate;
1353 if(boot_into_recovery &&
Mayank Grover68fbf0d2017-10-24 14:13:39 +05301354 (!partition_multislot_is_supported()))
Monika Singh7d2fc272018-03-16 17:16:01 +05301355 {
1356 ret = boot_verify_image((unsigned char *)bootimg_addr,
1357 bootimg_size, "/recovery", &bootstate);
1358 }
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001359 else
Monika Singh7d2fc272018-03-16 17:16:01 +05301360 {
1361 ret = boot_verify_image((unsigned char *)bootimg_addr,
1362 bootimg_size, "/boot", &bootstate);
1363 }
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001364 boot_verify_print_state();
1365#else
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001366 ret = image_verify((unsigned char *)bootimg_addr,
1367 (unsigned char *)(bootimg_addr + bootimg_size),
1368 bootimg_size,
Sundarajan Srinivasance54d6e2013-11-11 12:45:00 -08001369 auth_algo);
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001370#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001371 dprintf(INFO, "Authenticating boot image: done return value = %d\n", ret);
1372
1373 if (ret)
1374 {
1375 /* Authorized kernel */
1376 device.is_tampered = 0;
Sundarajan Srinivasan3fb21f12013-09-16 18:36:15 -07001377 auth_kernel_img = 1;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001378 }
1379
Amit Blay4aa292f2015-04-28 21:55:59 +03001380#ifdef MDTP_SUPPORT
1381 {
1382 /* Verify MDTP lock.
1383 * For boot & recovery partitions, use aboot's verification result.
1384 */
1385 mdtp_ext_partition_verification_t ext_partition;
1386 ext_partition.partition = boot_into_recovery ? MDTP_PARTITION_RECOVERY : MDTP_PARTITION_BOOT;
1387 ext_partition.integrity_state = device.is_tampered ? MDTP_PARTITION_STATE_INVALID : MDTP_PARTITION_STATE_VALID;
1388 ext_partition.page_size = 0; /* Not needed since already validated */
1389 ext_partition.image_addr = 0; /* Not needed since already validated */
1390 ext_partition.image_size = 0; /* Not needed since already validated */
1391 ext_partition.sig_avail = FALSE; /* Not needed since already validated */
1392 mdtp_fwlock_verify_lock(&ext_partition);
1393 }
1394#endif /* MDTP_SUPPORT */
1395
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001396#if USE_PCOM_SECBOOT
1397 set_tamper_flag(device.is_tampered);
1398#endif
1399
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001400#if VERIFIED_BOOT
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001401 switch(boot_verify_get_state())
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001402 {
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001403 case RED:
lijuanga40d6302015-07-20 20:10:13 +08001404#if FBCON_DISPLAY_MSG
lijuang9a7d3b92015-11-30 14:41:24 +08001405 display_bootverify_menu(DISPLAY_MENU_RED);
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07001406#if ENABLE_VB_ATTEST
1407 mdelay(DELAY_WAIT);
1408 shutdown_device();
1409#else
lijuanga40d6302015-07-20 20:10:13 +08001410 wait_for_users_action();
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07001411#endif
lijuanga40d6302015-07-20 20:10:13 +08001412#else
1413 dprintf(CRITICAL,
1414 "Your device has failed verification and may not work properly.\nWait for 5 seconds before proceeding\n");
1415 mdelay(5000);
1416#endif
1417
1418 break;
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001419 case YELLOW:
lijuanga40d6302015-07-20 20:10:13 +08001420#if FBCON_DISPLAY_MSG
lijuang9a7d3b92015-11-30 14:41:24 +08001421 display_bootverify_menu(DISPLAY_MENU_YELLOW);
lijuanga40d6302015-07-20 20:10:13 +08001422 wait_for_users_action();
1423#else
1424 dprintf(CRITICAL,
1425 "Your device has loaded a different operating system.\nWait for 5 seconds before proceeding\n");
1426 mdelay(5000);
1427#endif
1428 break;
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001429 default:
lijuanga40d6302015-07-20 20:10:13 +08001430 break;
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001431 }
1432#endif
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001433#if !VERIFIED_BOOT
Unnati Gandhi1be04752015-03-27 19:41:53 +05301434 if(device.is_tampered)
1435 {
1436 write_device_info_mmc(&device);
1437 #ifdef TZ_TAMPER_FUSE
Monika Singh98257462018-06-06 11:28:49 +05301438 set_tamper_fuse_cmd(HLOS_IMG_TAMPER_FUSE);
Unnati Gandhi1be04752015-03-27 19:41:53 +05301439 #endif
1440 #ifdef ASSERT_ON_TAMPER
1441 dprintf(CRITICAL, "Device is tampered. Asserting..\n");
1442 ASSERT(0);
1443 #endif
1444 }
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001445#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001446}
1447
Maunik Shah0f3c8ac2014-03-02 17:47:58 +05301448static bool check_format_bit()
1449{
1450 bool ret = false;
1451 int index;
1452 uint64_t offset;
1453 struct boot_selection_info *in = NULL;
1454 char *buf = NULL;
1455
1456 index = partition_get_index("bootselect");
1457 if (index == INVALID_PTN)
1458 {
1459 dprintf(INFO, "Unable to locate /bootselect partition\n");
1460 return ret;
1461 }
1462 offset = partition_get_offset(index);
1463 if(!offset)
1464 {
1465 dprintf(INFO, "partition /bootselect doesn't exist\n");
1466 return ret;
1467 }
1468 buf = (char *) memalign(CACHE_LINE, ROUNDUP(page_size, CACHE_LINE));
Mayank Grover48860402016-11-29 12:34:53 +05301469 mmc_set_lun(partition_get_lun(index));
Maunik Shah0f3c8ac2014-03-02 17:47:58 +05301470 ASSERT(buf);
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301471 if (mmc_read(offset, (uint32_t *)buf, page_size))
Maunik Shah0f3c8ac2014-03-02 17:47:58 +05301472 {
1473 dprintf(INFO, "mmc read failure /bootselect %d\n", page_size);
1474 free(buf);
1475 return ret;
1476 }
1477 in = (struct boot_selection_info *) buf;
1478 if ((in->signature == BOOTSELECT_SIGNATURE) &&
1479 (in->version == BOOTSELECT_VERSION)) {
1480 if ((in->state_info & BOOTSELECT_FORMAT) &&
1481 !(in->state_info & BOOTSELECT_FACTORY))
1482 ret = true;
1483 } else {
1484 dprintf(CRITICAL, "Signature: 0x%08x or version: 0x%08x mismatched of /bootselect\n",
1485 in->signature, in->version);
1486 ASSERT(0);
1487 }
1488 free(buf);
1489 return ret;
1490}
1491
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001492void boot_verifier_init()
1493{
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001494 uint32_t boot_state;
1495 /* Check if device unlock */
1496 if(device.is_unlocked)
1497 {
1498 boot_verify_send_event(DEV_UNLOCK);
1499 boot_verify_print_state();
1500 dprintf(CRITICAL, "Device is unlocked! Skipping verification...\n");
1501 return;
1502 }
1503 else
1504 {
1505 boot_verify_send_event(BOOT_INIT);
1506 }
1507
1508 /* Initialize keystore */
1509 boot_state = boot_verify_keystore_init();
1510 if(boot_state == YELLOW)
1511 {
1512 boot_verify_print_state();
1513 dprintf(CRITICAL, "Keystore verification failed! Continuing anyways...\n");
1514 }
1515}
1516
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301517/* Function to return recovery appended dtbo buffer info */
1518void get_recovery_dtbo_info(uint32_t *dtbo_size, void **dtbo_buf)
1519{
1520 *dtbo_size = recovery_dtbo_size;
1521 *dtbo_buf = recovery_dtbo_buf;
1522 return;
1523}
1524
Shashank Mittal23b8f422010-04-16 19:27:21 -07001525int boot_linux_from_mmc(void)
1526{
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301527 boot_img_hdr *hdr = (void*) buf;
1528 boot_img_hdr *uhdr;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001529 unsigned offset = 0;
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07001530 int rcode;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001531 unsigned long long ptn = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001532 int index = INVALID_PTN;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001533
Shashank Mittalcd98d472011-08-02 14:29:24 -07001534 unsigned char *image_addr = 0;
1535 unsigned kernel_actual;
1536 unsigned ramdisk_actual;
1537 unsigned imagesize_actual;
Neeti Desai465491e2012-07-31 12:53:35 -07001538 unsigned second_actual = 0;
Parth Dixit1375d9e2019-07-08 20:56:13 +05301539 void * image_buf = NULL;
Neeti Desai465491e2012-07-31 12:53:35 -07001540
Matthew Qin271927e2015-03-31 22:07:07 -04001541 unsigned int dtb_size = 0;
Parth Dixit1375d9e2019-07-08 20:56:13 +05301542 unsigned dtb_image_size = 0;
1543 uint32_t dtb_image_offset = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08001544 unsigned int out_len = 0;
1545 unsigned int out_avai_len = 0;
1546 unsigned char *out_addr = NULL;
1547 uint32_t dtb_offset = 0;
1548 unsigned char *kernel_start_addr = NULL;
1549 unsigned int kernel_size = 0;
Ameya Thakur10a33452016-06-13 14:24:26 -07001550 unsigned int patched_kernel_hdr_size = 0;
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301551 uint64_t image_size = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08001552 int rc;
Monika Singh292b3e92018-03-17 22:40:23 +05301553#if VERIFIED_BOOT_2
1554 int status;
Mayank Grover027a9412018-09-04 15:12:05 +05301555 void *dtbo_image_buf = NULL;
1556 uint32_t dtbo_image_sz = 0;
Mayank Grover9df84c02018-08-30 15:46:35 +05301557 void *vbmeta_image_buf = NULL;
1558 uint32_t vbmeta_image_sz = 0;
Monika Singh292b3e92018-03-17 22:40:23 +05301559#endif
Mayank Groverc93cad82017-10-03 12:23:45 +05301560 char *ptn_name = NULL;
Neeti Desai465491e2012-07-31 12:53:35 -07001561#if DEVICE_TREE
1562 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -07001563 struct dt_entry dt_entry;
Neeti Desai465491e2012-07-31 12:53:35 -07001564 unsigned dt_table_offset;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001565 uint32_t dt_actual;
Deepa Dinamani19648b42013-09-05 17:05:55 -07001566 uint32_t dt_hdr_size;
Matthew Qin271927e2015-03-31 22:07:07 -04001567 unsigned char *best_match_dt_addr = NULL;
Neeti Desai465491e2012-07-31 12:53:35 -07001568#endif
Matthew Qin49e51fa2015-02-09 10:40:45 +08001569 struct kernel64_hdr *kptr = NULL;
Mayank Grover351a75e2017-05-30 20:06:08 +05301570 int current_active_slot = INVALID;
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001571
Maunik Shah0f3c8ac2014-03-02 17:47:58 +05301572 if (check_format_bit())
1573 boot_into_recovery = 1;
1574
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07001575 if (!boot_into_recovery) {
1576 memset(ffbm_mode_string, '\0', sizeof(ffbm_mode_string));
1577 rcode = get_ffbm(ffbm_mode_string, sizeof(ffbm_mode_string));
1578 if (rcode <= 0) {
1579 boot_into_ffbm = false;
1580 if (rcode < 0)
1581 dprintf(CRITICAL,"failed to get ffbm cookie");
1582 } else
1583 boot_into_ffbm = true;
1584 } else
1585 boot_into_ffbm = false;
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301586 uhdr = (boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001587 if (!memcmp(uhdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1588 dprintf(INFO, "Unified boot method!\n");
1589 hdr = uhdr;
1590 goto unified_boot;
1591 }
Mayank Groverc93cad82017-10-03 12:23:45 +05301592
1593 /* For a/b recovery image code is on boot partition.
1594 If we support multislot, always use boot partition. */
1595 if (boot_into_recovery &&
Mayank Grover8ab4a762019-04-25 17:23:34 +05301596 ((!partition_multislot_is_supported()) ||
1597 (target_dynamic_partition_supported())))
Mayank Groverc93cad82017-10-03 12:23:45 +05301598 ptn_name = "recovery";
1599 else
1600 ptn_name = "boot";
1601
1602 index = partition_get_index(ptn_name);
1603 ptn = partition_get_offset(index);
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301604 image_size = partition_get_size(index);
1605 if(ptn == 0 || image_size == 0) {
Mayank Groverc93cad82017-10-03 12:23:45 +05301606 dprintf(CRITICAL, "ERROR: No %s partition found\n", ptn_name);
1607 return -1;
Kinson Chikf1a43512011-07-14 11:28:39 -07001608 }
Mayank Groverc93cad82017-10-03 12:23:45 +05301609
Channagoud Kadabief0547c2015-02-10 12:57:38 -08001610 /* Set Lun for boot & recovery partitions */
1611 mmc_set_lun(partition_get_lun(index));
Shashank Mittal23b8f422010-04-16 19:27:21 -07001612
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301613 if (mmc_read(ptn + offset, (uint32_t *) buf, page_size)) {
Shashank Mittal23b8f422010-04-16 19:27:21 -07001614 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
1615 return -1;
1616 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001617
1618 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001619 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Mayank Grover351a75e2017-05-30 20:06:08 +05301620 return ERR_INVALID_BOOT_MAGIC;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001621 }
1622
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07001623 if (hdr->page_size && (hdr->page_size != page_size)) {
vijay kumar2e21b3a2014-06-26 17:40:15 +05301624
1625 if (hdr->page_size > BOOT_IMG_MAX_PAGE_SIZE) {
1626 dprintf(CRITICAL, "ERROR: Invalid page size\n");
1627 return -1;
1628 }
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07001629 page_size = hdr->page_size;
1630 page_mask = page_size - 1;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001631 }
1632
Matthew Qin49e51fa2015-02-09 10:40:45 +08001633 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1634 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Kishor PKd8ddcad2017-07-27 13:53:57 +05301635 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001636
Matthew Qin49e51fa2015-02-09 10:40:45 +08001637 image_addr = (unsigned char *)target_get_scratch_address();
Mayank Grover6fd36792018-09-10 13:26:58 +05301638#if VERIFIED_BOOT_2
1639 /* Create hole in start of image for VB salt to copy */
1640 image_addr += SALT_BUFF_OFFSET;
1641#endif
Kishor PK9e91b592017-05-24 12:14:55 +05301642 memcpy(image_addr, (void *)buf, page_size);
1643
1644 /* ensure commandline is terminated */
1645 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
Matthew Qin49e51fa2015-02-09 10:40:45 +08001646
1647#if DEVICE_TREE
Parth Dixit4097b622016-03-15 14:42:27 +05301648#ifndef OSVERSION_IN_BOOTIMAGE
1649 dt_size = hdr->dt_size;
Mayank Grover5a502582018-09-12 11:24:49 +05301650#else
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301651 dprintf(INFO, "BootImage Header: %d\n", hdr->header_version);
Mayank Grover5a502582018-09-12 11:24:49 +05301652#endif
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301653
Parth Dixit4097b622016-03-15 14:42:27 +05301654 dt_actual = ROUND_TO_PAGE(dt_size, page_mask);
Kishor PKd8ddcad2017-07-27 13:53:57 +05301655 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 +05301656 dprintf(CRITICAL, "Integer overflow detected in bootimage header fields at %u in %s\n",__LINE__,__FILE__);
1657 return -1;
1658 }
Kishor PKd8ddcad2017-07-27 13:53:57 +05301659 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + second_actual + dt_actual);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001660#else
Kishor PKd8ddcad2017-07-27 13:53:57 +05301661 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 +05301662 dprintf(CRITICAL, "Integer overflow detected in bootimage header fields at %u in %s\n",__LINE__,__FILE__);
1663 return -1;
1664 }
Kishor PKd8ddcad2017-07-27 13:53:57 +05301665 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + second_actual);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001666#endif
Parth Dixit1375d9e2019-07-08 20:56:13 +05301667 dtb_image_size = hdr->kernel_size;
Matthew Qin49e51fa2015-02-09 10:40:45 +08001668
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301669#ifdef OSVERSION_IN_BOOTIMAGE
1670 /* If header version is ONE and booting into recovery,
1671 dtbo is appended with recovery image.
1672 Doing following:
1673 * Validating the recovery offset and size.
1674 * Extracting recovery dtbo to be used as dtbo.
1675 */
1676 if (boot_into_recovery &&
1677 hdr->header_version == BOOT_HEADER_VERSION_ONE)
1678 {
1679 struct boot_img_hdr_v1 *hdr1 =
1680 (struct boot_img_hdr_v1 *) (image_addr + sizeof(boot_img_hdr));
Mayank Grovera1a83c72018-09-24 11:23:15 +05301681 unsigned int recovery_dtbo_actual = 0;
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301682
Mayank Grovera1a83c72018-09-24 11:23:15 +05301683 recovery_dtbo_actual = ROUND_TO_PAGE(hdr1->recovery_dtbo_size, page_mask);
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301684 if ((hdr1->header_size !=
1685 sizeof(struct boot_img_hdr_v1) + sizeof(boot_img_hdr)))
1686 {
1687 dprintf(CRITICAL, "Invalid boot image header: %d\n", hdr1->header_size);
1688 return -1;
1689 }
1690
Mayank Grovera1a83c72018-09-24 11:23:15 +05301691 if (recovery_dtbo_actual > MAX_SUPPORTED_DTBO_IMG_BUF)
1692 {
1693 dprintf(CRITICAL, "Recovery Dtbo Size too big %x, Allowed size %x\n", recovery_dtbo_actual,
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301694 MAX_SUPPORTED_DTBO_IMG_BUF);
1695 return -1;
1696 }
1697
Mayank Grovera1a83c72018-09-24 11:23:15 +05301698 if (UINT_MAX < ((uint64_t)imagesize_actual + recovery_dtbo_actual))
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301699 {
1700 dprintf(CRITICAL, "Integer overflow detected in recoveryimage header fields at %u in %s\n",__LINE__,__FILE__);
1701 return -1;
1702 }
1703
Mayank Grovera1a83c72018-09-24 11:23:15 +05301704 if (UINT_MAX < (hdr1->recovery_dtbo_offset + recovery_dtbo_actual)) {
1705 dprintf(CRITICAL,
1706 "Integer overflow detected in recovery image header fields at %u in %s\n",__LINE__,__FILE__);
1707 return -1;
1708 }
1709
1710 if (hdr1->recovery_dtbo_offset + recovery_dtbo_actual > image_size)
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301711 {
1712 dprintf(CRITICAL, "Invalid recovery dtbo: Recovery Dtbo Offset=0x%llx,"
1713 " Recovery Dtbo Size=0x%x, Image Size=0x%llx\n",
1714 hdr1->recovery_dtbo_offset, recovery_dtbo_size, image_size);
1715 return -1;
1716 }
1717
1718 recovery_dtbo_buf = (void *)(hdr1->recovery_dtbo_offset + image_addr);
Mayank Grovera1a83c72018-09-24 11:23:15 +05301719 recovery_dtbo_size = recovery_dtbo_actual;
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301720 imagesize_actual += recovery_dtbo_size;
1721
1722 dprintf(SPEW, "Header version: %d\n", hdr->header_version);
1723 dprintf(SPEW, "Recovery Dtbo Size 0x%x\n", recovery_dtbo_size);
1724 dprintf(SPEW, "Recovery Dtbo Offset 0x%llx\n", hdr1->recovery_dtbo_offset);
1725
1726 }
Parth Dixit1375d9e2019-07-08 20:56:13 +05301727
1728 if ( hdr->header_version == BOOT_HEADER_VERSION_TWO) {
1729 struct boot_img_hdr_v1 *hdr1 =
1730 (struct boot_img_hdr_v1 *) (image_addr + sizeof(boot_img_hdr));
1731 struct boot_img_hdr_v2 *hdr2 = (struct boot_img_hdr_v2 *)
1732 (image_addr + sizeof(boot_img_hdr) +
1733 BOOT_IMAGE_HEADER_V2_OFFSET);
1734 unsigned int recovery_dtbo_actual = 0;
1735
lijuang0b17ba22019-09-04 14:20:47 +08001736 recovery_dtbo_actual =
1737 ROUND_TO_PAGE(hdr1->recovery_dtbo_size, page_mask);
1738 imagesize_actual += recovery_dtbo_actual;
Parth Dixit1375d9e2019-07-08 20:56:13 +05301739
1740 imagesize_actual += ROUND_TO_PAGE(hdr2->dtb_size, page_mask);
1741
1742
1743 dtb_image_offset = page_size + patched_kernel_hdr_size +
1744 kernel_actual + ramdisk_actual + second_actual +
1745 recovery_dtbo_actual;
1746
1747 dprintf(SPEW, "Header version: %d\n", hdr->header_version);
1748 dprintf(SPEW, "Dtb image offset 0x%x\n", dtb_image_offset);
1749 }
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301750#endif
1751
Parth Dixit1375d9e2019-07-08 20:56:13 +05301752
Saranya Chidurab1aaf232018-11-19 15:47:50 +05301753 /* Validate the boot/recovery image size is within the bounds of partition size */
1754 if (imagesize_actual > image_size) {
1755 dprintf(CRITICAL, "Image size is greater than partition size.\n");
1756 return -1;
1757 }
1758
Matthew Qin49e51fa2015-02-09 10:40:45 +08001759#if VERIFIED_BOOT
1760 boot_verifier_init();
1761#endif
1762
Mayank Grover1ceaee22019-04-01 17:54:32 +05301763#if VERIFIED_BOOT_2
1764 /* read full partition if device is unlocked */
1765 if (device.is_unlocked)
1766 imagesize_actual = image_size;
1767#endif
1768
Vijay Kumar Pendoti9c002ad2016-03-09 13:52:45 +05301769 if (check_aboot_addr_range_overlap((uintptr_t) image_addr, imagesize_actual))
Matthew Qin49e51fa2015-02-09 10:40:45 +08001770 {
1771 dprintf(CRITICAL, "Boot image buffer address overlaps with aboot addresses.\n");
1772 return -1;
1773 }
1774
Matthew Qinbb7923d2015-02-09 10:56:09 +08001775 /*
1776 * Update loading flow of bootimage to support compressed/uncompressed
1777 * bootimage on both 64bit and 32bit platform.
1778 * 1. Load bootimage from emmc partition onto DDR.
1779 * 2. Check if bootimage is gzip format. If yes, decompress compressed kernel
1780 * 3. Check kernel header and update kernel load addr for 64bit and 32bit
1781 * platform accordingly.
1782 * 4. Sanity Check on kernel_addr and ramdisk_addr and copy data.
1783 */
Mayank Grover351a75e2017-05-30 20:06:08 +05301784 if (partition_multislot_is_supported())
1785 {
1786 current_active_slot = partition_find_active_slot();
1787 dprintf(INFO, "Loading boot image (%d) active_slot(%s): start\n",
1788 imagesize_actual, SUFFIX_SLOT(current_active_slot));
1789 }
1790 else
1791 {
1792 dprintf(INFO, "Loading (%s) image (%d): start\n",
1793 (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual);
1794 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08001795 bs_set_timestamp(BS_KERNEL_LOAD_START);
1796
Gaurav Nebhwani43e2a462016-03-17 21:32:56 +05301797 if ((target_get_max_flash_size() - page_size) < imagesize_actual)
1798 {
1799 dprintf(CRITICAL, "booimage size is greater than DDR can hold\n");
1800 return -1;
1801 }
Kishor PK9e91b592017-05-24 12:14:55 +05301802 offset = page_size;
1803 /* Read image without signature and header*/
1804 if (mmc_read(ptn + offset, (void *)(image_addr + offset), imagesize_actual - page_size))
Matthew Qin49e51fa2015-02-09 10:40:45 +08001805 {
1806 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
1807 return -1;
1808 }
1809
Mayank Grover351a75e2017-05-30 20:06:08 +05301810 if (partition_multislot_is_supported())
1811 {
1812 dprintf(INFO, "Loading boot image (%d) active_slot(%s): done\n",
1813 imagesize_actual, SUFFIX_SLOT(current_active_slot));
1814 }
1815 else
1816 {
1817 dprintf(INFO, "Loading (%s) image (%d): done\n",
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001818 (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual);
1819
Mayank Grover351a75e2017-05-30 20:06:08 +05301820 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08001821 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
1822
1823 /* Authenticate Kernel */
1824 dprintf(INFO, "use_signed_kernel=%d, is_unlocked=%d, is_tampered=%d.\n",
1825 (int) target_use_signed_kernel(),
1826 device.is_unlocked,
1827 device.is_tampered);
Monika Singh292b3e92018-03-17 22:40:23 +05301828#if VERIFIED_BOOT_2
Mayank Grover1ceaee22019-04-01 17:54:32 +05301829 /* if device is unlocked skip reading signature, as full partition is read */
1830 if (!device.is_unlocked)
Monika Singh292b3e92018-03-17 22:40:23 +05301831 {
Mayank Grover1ceaee22019-04-01 17:54:32 +05301832 offset = imagesize_actual;
1833 if (check_aboot_addr_range_overlap((uintptr_t)image_addr + offset, page_size))
1834 {
1835 dprintf(CRITICAL, "Signature read buffer address overlaps with aboot addresses.\n");
1836 return -1;
1837 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08001838
Mayank Grover1ceaee22019-04-01 17:54:32 +05301839 /* Read signature */
1840 if(mmc_read(ptn + offset, (void *)(image_addr + offset), page_size))
1841 {
1842 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
1843 return -1;
1844 }
Monika Singh292b3e92018-03-17 22:40:23 +05301845 }
1846
Mayank Grover027a9412018-09-04 15:12:05 +05301847 /* load and validate dtbo partition */
1848 load_validate_dtbo_image(&dtbo_image_buf, &dtbo_image_sz);
1849
Mayank Grover9df84c02018-08-30 15:46:35 +05301850 /* load vbmeta partition */
1851 load_vbmeta_image(&vbmeta_image_buf, &vbmeta_image_sz);
1852
Monika Singh292b3e92018-03-17 22:40:23 +05301853 memset(&info, 0, sizeof(bootinfo));
Mayank Grover027a9412018-09-04 15:12:05 +05301854
1855 /* Pass loaded boot image passed */
Mayank Grover6fd36792018-09-10 13:26:58 +05301856 info.images[IMG_BOOT].image_buffer = SUB_SALT_BUFF_OFFSET(image_addr);
Mayank Grover027a9412018-09-04 15:12:05 +05301857 info.images[IMG_BOOT].imgsize = imagesize_actual;
1858 info.images[IMG_BOOT].name = ptn_name;
1859 ++info.num_loaded_images;
1860
1861 /* Pass loaded dtbo image */
1862 if (dtbo_image_buf != NULL) {
Mayank Grover6fd36792018-09-10 13:26:58 +05301863 info.images[IMG_DTBO].image_buffer =
1864 SUB_SALT_BUFF_OFFSET(dtbo_image_buf);
Mayank Grover027a9412018-09-04 15:12:05 +05301865 info.images[IMG_DTBO].imgsize = dtbo_image_sz;
1866 info.images[IMG_DTBO].name = "dtbo";
1867 ++info.num_loaded_images;
1868 }
1869
Mayank Grover9df84c02018-08-30 15:46:35 +05301870 /* Pass loaded vbmeta image */
1871 if (vbmeta_image_buf != NULL) {
1872 info.images[IMG_VBMETA].image_buffer = vbmeta_image_buf;
1873 info.images[IMG_VBMETA].imgsize = vbmeta_image_sz;
1874 info.images[IMG_VBMETA].name = "vbmeta";
1875 ++info.num_loaded_images;
1876 }
1877
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301878 info.header_version = hdr->header_version;
Monika Singh292b3e92018-03-17 22:40:23 +05301879 info.multi_slot_boot = partition_multislot_is_supported();
1880 info.bootreason_alarm = boot_reason_alarm;
1881 info.bootinto_recovery = boot_into_recovery;
1882 status = load_image_and_auth(&info);
1883 if(status)
1884 return -1;
1885
1886 vbcmdline = info.vbcmdline;
Mayank Grover9df84c02018-08-30 15:46:35 +05301887
1888 /* Free the buffer allocated to vbmeta post verification */
Saranya Chidurab4933332018-10-15 17:30:06 +05301889 if (vbmeta_image_buf != NULL) {
1890 free(vbmeta_image_buf);
1891 --info.num_loaded_images;
1892 }
Monika Singh292b3e92018-03-17 22:40:23 +05301893#else
Channagoud Kadabi736c4962015-08-21 11:56:52 -07001894 /* Change the condition a little bit to include the test framework support.
1895 * We would never reach this point if device is in fastboot mode, even if we did
1896 * that means we are in test mode, so execute kernel authentication part for the
1897 * tests */
Parth Dixitcb0c6082015-12-30 15:01:13 +05301898 if((target_use_signed_kernel() && (!device.is_unlocked)) || is_test_mode_enabled())
Matthew Qin49e51fa2015-02-09 10:40:45 +08001899 {
1900 offset = imagesize_actual;
Vijay Kumar Pendoti9c002ad2016-03-09 13:52:45 +05301901 if (check_aboot_addr_range_overlap((uintptr_t)image_addr + offset, page_size))
Matthew Qin49e51fa2015-02-09 10:40:45 +08001902 {
1903 dprintf(CRITICAL, "Signature read buffer address overlaps with aboot addresses.\n");
1904 return -1;
1905 }
1906
1907 /* Read signature */
1908 if(mmc_read(ptn + offset, (void *)(image_addr + offset), page_size))
1909 {
1910 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
1911 return -1;
1912 }
1913
1914 verify_signed_bootimg((uint32_t)image_addr, imagesize_actual);
Channagoud Kadabi736c4962015-08-21 11:56:52 -07001915 /* The purpose of our test is done here */
Parth Dixitcb0c6082015-12-30 15:01:13 +05301916 if(is_test_mode_enabled() && auth_kernel_img)
Channagoud Kadabi736c4962015-08-21 11:56:52 -07001917 return 0;
Matthew Qin49e51fa2015-02-09 10:40:45 +08001918 } else {
1919 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
1920 #ifdef TZ_SAVE_KERNEL_HASH
1921 aboot_save_boot_hash_mmc((uint32_t) image_addr, imagesize_actual);
1922 #endif /* TZ_SAVE_KERNEL_HASH */
Amit Blay4aa292f2015-04-28 21:55:59 +03001923
1924#ifdef MDTP_SUPPORT
1925 {
1926 /* Verify MDTP lock.
1927 * For boot & recovery partitions, MDTP will use boot_verifier APIs,
1928 * since verification was skipped in aboot. The signature is not part of the loaded image.
1929 */
1930 mdtp_ext_partition_verification_t ext_partition;
1931 ext_partition.partition = boot_into_recovery ? MDTP_PARTITION_RECOVERY : MDTP_PARTITION_BOOT;
1932 ext_partition.integrity_state = MDTP_PARTITION_STATE_UNSET;
1933 ext_partition.page_size = page_size;
1934 ext_partition.image_addr = (uint32)image_addr;
1935 ext_partition.image_size = imagesize_actual;
1936 ext_partition.sig_avail = FALSE;
1937 mdtp_fwlock_verify_lock(&ext_partition);
1938 }
1939#endif /* MDTP_SUPPORT */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001940 }
Monika Singh292b3e92018-03-17 22:40:23 +05301941#endif
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001942
Sridhar Parasuram00bfedb2015-05-26 14:21:27 -07001943#if VERIFIED_BOOT
taozhang93088bd2016-11-16 15:50:46 +08001944 if((boot_verify_get_state() == ORANGE) && (!boot_into_ffbm))
Reut Zysmanba8a9d52016-02-18 11:44:04 +02001945 {
1946#if FBCON_DISPLAY_MSG
1947 display_bootverify_menu(DISPLAY_MENU_ORANGE);
1948 wait_for_users_action();
1949#else
1950 dprintf(CRITICAL,
1951 "Your device has been unlocked and can't be trusted.\nWait for 5 seconds before proceeding\n");
1952 mdelay(5000);
1953#endif
1954 }
1955#endif
1956
1957#if VERIFIED_BOOT
Monika Singh292b3e92018-03-17 22:40:23 +05301958 if (VB_M == target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +05301959 {
1960 /* set boot and system versions. */
1961 set_os_version((unsigned char *)image_addr);
1962 // send root of trust
1963 if(!send_rot_command((uint32_t)device.is_unlocked))
1964 ASSERT(0);
1965 }
Parth Dixitbc9b6492015-10-18 00:41:38 +05301966#endif
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001967 /*
Matthew Qinbb7923d2015-02-09 10:56:09 +08001968 * Check if the kernel image is a gzip package. If yes, need to decompress it.
1969 * If not, continue booting.
1970 */
1971 if (is_gzip_package((unsigned char *)(image_addr + page_size), hdr->kernel_size))
1972 {
1973 out_addr = (unsigned char *)(image_addr + imagesize_actual + page_size);
1974 out_avai_len = target_get_max_flash_size() - imagesize_actual - page_size;
Mayank Grover027a9412018-09-04 15:12:05 +05301975#if VERIFIED_BOOT_2
1976 if (dtbo_image_sz)
1977 out_avai_len -= DTBO_IMG_BUF;
1978#endif
Matthew Qin0b15b322015-05-19 05:20:54 -04001979 dprintf(INFO, "decompressing kernel image: start\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001980 rc = decompress((unsigned char *)(image_addr + page_size),
1981 hdr->kernel_size, out_addr, out_avai_len,
1982 &dtb_offset, &out_len);
1983 if (rc)
1984 {
Matthew Qin0b15b322015-05-19 05:20:54 -04001985 dprintf(CRITICAL, "decompressing kernel image failed!!!\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001986 ASSERT(0);
1987 }
1988
Matthew Qin0b15b322015-05-19 05:20:54 -04001989 dprintf(INFO, "decompressing kernel image: done\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001990 kptr = (struct kernel64_hdr *)out_addr;
1991 kernel_start_addr = out_addr;
1992 kernel_size = out_len;
1993 } else {
Ameya Thakur10a33452016-06-13 14:24:26 -07001994 dprintf(INFO, "Uncpmpressed kernel in use\n");
1995 if (!strncmp((char*)(image_addr + page_size),
1996 PATCHED_KERNEL_MAGIC,
1997 sizeof(PATCHED_KERNEL_MAGIC) - 1)) {
1998 dprintf(INFO, "Patched kernel detected\n");
1999 kptr = (struct kernel64_hdr *)(image_addr + page_size +
2000 PATCHED_KERNEL_HEADER_SIZE);
2001 //The size of the kernel is stored at start of kernel image + 16
2002 //The dtb would start just after the kernel
2003 dtb_offset = *((uint32_t*)((unsigned char*)
2004 (image_addr + page_size +
2005 sizeof(PATCHED_KERNEL_MAGIC) -
2006 1)));
2007 //The actual kernel starts after the 20 byte header.
2008 kernel_start_addr = (unsigned char*)(image_addr +
2009 page_size + PATCHED_KERNEL_HEADER_SIZE);
2010 kernel_size = hdr->kernel_size;
2011 patched_kernel_hdr_size = PATCHED_KERNEL_HEADER_SIZE;
2012 } else {
2013 dprintf(INFO, "Kernel image not patched..Unable to locate dt offset\n");
2014 kptr = (struct kernel64_hdr *)(image_addr + page_size);
2015 kernel_start_addr = (unsigned char *)(image_addr + page_size);
2016 kernel_size = hdr->kernel_size;
2017 }
Matthew Qinbb7923d2015-02-09 10:56:09 +08002018 }
2019
2020 /*
Channagoud Kadabia22144f2013-03-20 11:49:01 -07002021 * Update the kernel/ramdisk/tags address if the boot image header
2022 * has default values, these default values come from mkbootimg when
2023 * the boot image is flashed using fastboot flash:raw
2024 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08002025 update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr));
Channagoud Kadabia22144f2013-03-20 11:49:01 -07002026
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002027 /* Get virtual addresses since the hdr saves physical addresses. */
2028 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
2029 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
2030 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002031
Matthew Qinbb7923d2015-02-09 10:56:09 +08002032 kernel_size = ROUND_TO_PAGE(kernel_size, page_mask);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002033 /* Check if the addresses in the header are valid. */
Matthew Qinbb7923d2015-02-09 10:56:09 +08002034 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_size) ||
Mayank Grovere559cec2017-10-17 15:12:03 +05302035 check_ddr_addr_range_bound(hdr->kernel_addr, kernel_size) ||
2036 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual) ||
2037 check_ddr_addr_range_bound(hdr->ramdisk_addr, ramdisk_actual))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002038 {
Mayank Grovere559cec2017-10-17 15:12:03 +05302039 dprintf(CRITICAL, "kernel/ramdisk addresses are not valid.\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002040 return -1;
2041 }
2042
2043#ifndef DEVICE_TREE
Mayank Grovere559cec2017-10-17 15:12:03 +05302044 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE) ||
2045 check_ddr_addr_range_bound(hdr->tags_addr, MAX_TAGS_SIZE))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002046 {
Mayank Grovere559cec2017-10-17 15:12:03 +05302047 dprintf(CRITICAL, "Tags addresses are not valid.\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002048 return -1;
2049 }
2050#endif
2051
Matthew Qin49e51fa2015-02-09 10:40:45 +08002052 /* Move kernel, ramdisk and device tree to correct address */
Matthew Qinbb7923d2015-02-09 10:56:09 +08002053 memmove((void*) hdr->kernel_addr, kernel_start_addr, kernel_size);
Matthew Qin49e51fa2015-02-09 10:40:45 +08002054 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002055
Matthew Qin49e51fa2015-02-09 10:40:45 +08002056 #if DEVICE_TREE
Parth Dixit4097b622016-03-15 14:42:27 +05302057 if(dt_size) {
Matthew Qin49e51fa2015-02-09 10:40:45 +08002058 dt_table_offset = ((uint32_t)image_addr + page_size + kernel_actual + ramdisk_actual + second_actual);
2059 table = (struct dt_table*) dt_table_offset;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002060
Matthew Qin49e51fa2015-02-09 10:40:45 +08002061 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
2062 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
2063 return -1;
2064 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002065
P.V. Phani Kumar82916762016-03-09 09:20:19 +05302066 /* Its Error if, dt_hdr_size (table->num_entries * dt_entry size + Dev_Tree Header)
2067 goes beyound hdr->dt_size*/
Parth Dixit4097b622016-03-15 14:42:27 +05302068 if (dt_hdr_size > ROUND_TO_PAGE(dt_size,hdr->page_size)) {
P.V. Phani Kumar82916762016-03-09 09:20:19 +05302069 dprintf(CRITICAL, "ERROR: Invalid Device Tree size \n");
2070 return -1;
2071 }
2072
Matthew Qin49e51fa2015-02-09 10:40:45 +08002073 /* Find index of device tree within device tree table */
2074 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
2075 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
2076 return -1;
2077 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002078
P.V. Phani Kumar3c333202016-03-09 11:54:37 +05302079 if(dt_entry.offset > (UINT_MAX - dt_entry.size)) {
2080 dprintf(CRITICAL, "ERROR: Device tree contents are Invalid\n");
2081 return -1;
2082 }
2083
2084 /* Ensure we are not overshooting dt_size with the dt_entry selected */
Parth Dixit4097b622016-03-15 14:42:27 +05302085 if ((dt_entry.offset + dt_entry.size) > dt_size) {
P.V. Phani Kumar3c333202016-03-09 11:54:37 +05302086 dprintf(CRITICAL, "ERROR: Device tree contents are Invalid\n");
2087 return -1;
2088 }
2089
Matthew Qin271927e2015-03-31 22:07:07 -04002090 if (is_gzip_package((unsigned char *)dt_table_offset + dt_entry.offset, dt_entry.size))
2091 {
2092 unsigned int compressed_size = 0;
2093 out_addr += out_len;
2094 out_avai_len -= out_len;
Matthew Qin0b15b322015-05-19 05:20:54 -04002095 dprintf(INFO, "decompressing dtb: start\n");
Matthew Qin271927e2015-03-31 22:07:07 -04002096 rc = decompress((unsigned char *)dt_table_offset + dt_entry.offset,
2097 dt_entry.size, out_addr, out_avai_len,
2098 &compressed_size, &dtb_size);
2099 if (rc)
2100 {
Matthew Qin0b15b322015-05-19 05:20:54 -04002101 dprintf(CRITICAL, "decompressing dtb failed!!!\n");
Matthew Qin271927e2015-03-31 22:07:07 -04002102 ASSERT(0);
2103 }
2104
Matthew Qin0b15b322015-05-19 05:20:54 -04002105 dprintf(INFO, "decompressing dtb: done\n");
Matthew Qin271927e2015-03-31 22:07:07 -04002106 best_match_dt_addr = out_addr;
2107 } else {
2108 best_match_dt_addr = (unsigned char *)dt_table_offset + dt_entry.offset;
2109 dtb_size = dt_entry.size;
2110 }
2111
Matthew Qin49e51fa2015-02-09 10:40:45 +08002112 /* Validate and Read device device tree in the tags_addr */
Mayank Grovere559cec2017-10-17 15:12:03 +05302113 if (check_aboot_addr_range_overlap(hdr->tags_addr, dtb_size) ||
2114 check_ddr_addr_range_bound(hdr->tags_addr, dtb_size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002115 {
Mayank Grovere559cec2017-10-17 15:12:03 +05302116 dprintf(CRITICAL, "Device tree addresses are not valid\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002117 return -1;
2118 }
Shashank Mittal162244e2011-08-08 19:01:25 -07002119
Matthew Qin271927e2015-03-31 22:07:07 -04002120 memmove((void *)hdr->tags_addr, (char *)best_match_dt_addr, dtb_size);
Matthew Qin49e51fa2015-02-09 10:40:45 +08002121 } else {
2122 /* Validate the tags_addr */
Mayank Grovere559cec2017-10-17 15:12:03 +05302123 if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual) ||
2124 check_ddr_addr_range_bound(hdr->tags_addr, kernel_actual))
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07002125 {
Mayank Grovere559cec2017-10-17 15:12:03 +05302126 dprintf(CRITICAL, "Device tree addresses are not valid.\n");
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07002127 return -1;
2128 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08002129 /*
2130 * If appended dev tree is found, update the atags with
2131 * memory address to the DTB appended location on RAM.
2132 * Else update with the atags address in the kernel header
Mayank Grover1ed3e8b2018-09-19 15:16:50 +05302133 *
2134 * Make sure everything from scratch address is read before next step!
2135 * In case of dtbo, this API is going to read dtbo on scratch.
Matthew Qin49e51fa2015-02-09 10:40:45 +08002136 */
2137 void *dtb;
Parth Dixit1375d9e2019-07-08 20:56:13 +05302138 image_buf = (void*)(image_addr + page_size + patched_kernel_hdr_size);
2139
Nagireddy Annem0d60bb62019-08-16 19:16:00 +05302140#ifdef OSVERSION_IN_BOOTIMAGE
Parth Dixit1375d9e2019-07-08 20:56:13 +05302141 if ( hdr->header_version == BOOT_HEADER_VERSION_TWO) {
2142
2143 image_buf = (void*)(image_addr);
2144 dtb_offset = dtb_image_offset;
2145 dtb_image_size = imagesize_actual;
2146 }
Nagireddy Annem0d60bb62019-08-16 19:16:00 +05302147#endif
Parth Dixit1375d9e2019-07-08 20:56:13 +05302148
2149 dtb = dev_tree_appended(image_buf, dtb_image_size, dtb_offset,
Ameya Thakur10a33452016-06-13 14:24:26 -07002150 (void *)hdr->tags_addr);
Matthew Qin49e51fa2015-02-09 10:40:45 +08002151 if (!dtb) {
2152 dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07002153 return -1;
2154 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07002155 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08002156 #endif
Shashank Mittal23b8f422010-04-16 19:27:21 -07002157
Stanimir Varbanov69ec5462013-07-18 18:17:42 +03002158 if (boot_into_recovery && !device.is_unlocked && !device.is_tampered)
2159 target_load_ssd_keystore();
2160
Shashank Mittal23b8f422010-04-16 19:27:21 -07002161unified_boot:
Shashank Mittal23b8f422010-04-16 19:27:21 -07002162
Dima Zavin77e41f32013-03-06 16:10:43 -08002163 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07002164 (const char *)hdr->cmdline, board_machtype(),
Shashank Mittal23b8f422010-04-16 19:27:21 -07002165 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
2166
2167 return 0;
2168}
2169
Dima Zavin214cc642009-01-26 11:16:21 -08002170int boot_linux_from_flash(void)
2171{
Mayank Grovere1ab96c2018-09-04 20:31:31 +05302172 boot_img_hdr *hdr = (void*) buf;
Dima Zavin214cc642009-01-26 11:16:21 -08002173 struct ptentry *ptn;
2174 struct ptable *ptable;
2175 unsigned offset = 0;
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08002176
Shashank Mittalcd98d472011-08-02 14:29:24 -07002177 unsigned char *image_addr = 0;
2178 unsigned kernel_actual;
2179 unsigned ramdisk_actual;
2180 unsigned imagesize_actual;
vijay kumar8f53e362015-11-24 13:38:11 +05302181 unsigned second_actual = 0;
Shashank Mittalcd98d472011-08-02 14:29:24 -07002182
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07002183#if DEVICE_TREE
Monika Singh292b3e92018-03-17 22:40:23 +05302184 struct dt_table *table = NULL;
Joel Kingaa335dc2013-06-03 16:11:08 -07002185 struct dt_entry dt_entry;
vijay kumar8f53e362015-11-24 13:38:11 +05302186 unsigned dt_table_offset;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08002187 uint32_t dt_actual;
Monika Singh292b3e92018-03-17 22:40:23 +05302188 uint32_t dt_hdr_size = 0;
Vivek Kumar07bd0862018-04-10 14:19:23 +05302189 uint32_t dtb_offset = 0;
vijay kumar8f53e362015-11-24 13:38:11 +05302190 unsigned int dtb_size = 0;
2191 unsigned char *best_match_dt_addr = NULL;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07002192#endif
2193
David Ng183a7422009-12-07 14:55:21 -08002194 if (target_is_emmc_boot()) {
Mayank Grovere1ab96c2018-09-04 20:31:31 +05302195 hdr = (boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
David Ng183a7422009-12-07 14:55:21 -08002196 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
2197 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
2198 return -1;
2199 }
2200 goto continue_boot;
2201 }
2202
Dima Zavin214cc642009-01-26 11:16:21 -08002203 ptable = flash_get_ptable();
2204 if (ptable == NULL) {
2205 dprintf(CRITICAL, "ERROR: Partition table not found\n");
2206 return -1;
2207 }
2208
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08002209 if(!boot_into_recovery)
2210 {
2211 ptn = ptable_find(ptable, "boot");
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07002212
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08002213 if (ptn == NULL) {
2214 dprintf(CRITICAL, "ERROR: No boot partition found\n");
2215 return -1;
2216 }
2217 }
2218 else
2219 {
2220 ptn = ptable_find(ptable, "recovery");
2221 if (ptn == NULL) {
2222 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
2223 return -1;
2224 }
Dima Zavin214cc642009-01-26 11:16:21 -08002225 }
2226
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05302227 /* Read boot.img header from flash */
Shashank Mittaldcc2e352009-11-19 19:11:16 -08002228 if (flash_read(ptn, offset, buf, page_size)) {
Dima Zavin214cc642009-01-26 11:16:21 -08002229 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
2230 return -1;
2231 }
Dima Zavin214cc642009-01-26 11:16:21 -08002232
2233 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07002234 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Dima Zavin214cc642009-01-26 11:16:21 -08002235 return -1;
2236 }
2237
Shashank Mittaldcc2e352009-11-19 19:11:16 -08002238 if (hdr->page_size != page_size) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07002239 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 -08002240 return -1;
2241 }
2242
Kishor PK9e91b592017-05-24 12:14:55 +05302243 image_addr = (unsigned char *)target_get_scratch_address();
2244 memcpy(image_addr, (void *)buf, page_size);
vijay kumar287bb542015-09-29 13:01:52 +05302245
Channagoud Kadabia22144f2013-03-20 11:49:01 -07002246 /*
2247 * Update the kernel/ramdisk/tags address if the boot image header
2248 * has default values, these default values come from mkbootimg when
2249 * the boot image is flashed using fastboot flash:raw
2250 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08002251 update_ker_tags_rdisk_addr(hdr, false);
Channagoud Kadabia22144f2013-03-20 11:49:01 -07002252
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002253 /* Get virtual addresses since the hdr saves physical addresses. */
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002254 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
2255 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
2256 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
2257
2258 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
2259 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Mayank Grover032736f2017-07-14 20:34:51 +05302260 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002261
Kishor PK9e91b592017-05-24 12:14:55 +05302262 /* ensure commandline is terminated */
2263 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
2264
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002265 /* Check if the addresses in the header are valid. */
2266 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
Mayank Grovere559cec2017-10-17 15:12:03 +05302267 check_ddr_addr_range_bound(hdr->kernel_addr, kernel_actual) ||
2268 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual) ||
2269 check_ddr_addr_range_bound(hdr->ramdisk_addr, ramdisk_actual))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002270 {
Mayank Grovere559cec2017-10-17 15:12:03 +05302271 dprintf(CRITICAL, "kernel/ramdisk addresses are not valid.\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002272 return -1;
2273 }
2274
2275#ifndef DEVICE_TREE
Kishor PKd8ddcad2017-07-27 13:53:57 +05302276 if (UINT_MAX < ((uint64_t)kernel_actual + (uint64_t)ramdisk_actual+ (uint64_t)second_actual + page_size)) {
Mayank Grover032736f2017-07-14 20:34:51 +05302277 dprintf(CRITICAL, "Integer overflow detected in bootimage header fields\n");
2278 return -1;
2279 }
Kishor PKd8ddcad2017-07-27 13:53:57 +05302280 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + second_actual);
Mayank Grover032736f2017-07-14 20:34:51 +05302281
Mayank Grovere559cec2017-10-17 15:12:03 +05302282 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE) ||
2283 check_ddr_addr_range_bound(hdr->tags_addr, MAX_TAGS_SIZE))
Vijay Kumar Pendotid3ed20e2016-09-20 00:34:46 +05302284 {
Mayank Grovere559cec2017-10-17 15:12:03 +05302285 dprintf(CRITICAL, "Tags addresses are not valid.\n");
Vijay Kumar Pendotid3ed20e2016-09-20 00:34:46 +05302286 return -1;
2287 }
2288#else
2289
2290#ifndef OSVERSION_IN_BOOTIMAGE
2291 dt_size = hdr->dt_size;
2292#endif
Mayank Grover032736f2017-07-14 20:34:51 +05302293 dt_actual = ROUND_TO_PAGE(dt_size, page_mask);
Kishor PKd8ddcad2017-07-27 13:53:57 +05302294 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 +05302295 dprintf(CRITICAL, "Integer overflow detected in bootimage header fields\n");
2296 return -1;
2297 }
2298
Kishor PKd8ddcad2017-07-27 13:53:57 +05302299 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + second_actual + dt_actual);
Mayank Grover032736f2017-07-14 20:34:51 +05302300
Mayank Grovere559cec2017-10-17 15:12:03 +05302301 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_size) ||
2302 check_ddr_addr_range_bound(hdr->tags_addr, dt_size))
Mayank Grover032736f2017-07-14 20:34:51 +05302303 {
Mayank Grovere559cec2017-10-17 15:12:03 +05302304 dprintf(CRITICAL, "Device tree addresses are not valid.\n");
Mayank Grover032736f2017-07-14 20:34:51 +05302305 return -1;
2306 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002307#endif
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002308
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05302309 /* Read full boot.img from flash */
2310 dprintf(INFO, "Loading (%s) image (%d): start\n",
2311 (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual);
2312 bs_set_timestamp(BS_KERNEL_LOAD_START);
2313
2314 if (UINT_MAX - page_size < imagesize_actual)
2315 {
2316 dprintf(CRITICAL,"Integer overflow detected in bootimage header fields %u %s\n", __LINE__,__func__);
2317 return -1;
2318 }
2319
2320 /*Check the availability of RAM before reading boot image + max signature length from flash*/
2321 if (target_get_max_flash_size() < (imagesize_actual + page_size))
2322 {
2323 dprintf(CRITICAL, "bootimage size is greater than DDR can hold\n");
2324 return -1;
2325 }
2326
2327 offset = page_size;
2328 /* Read image without signature and header */
2329 if (flash_read(ptn, offset, (void *)(image_addr + offset), imagesize_actual - page_size))
2330 {
2331 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
2332 return -1;
2333 }
2334
2335 dprintf(INFO, "Loading (%s) image (%d): done\n",
2336 (!boot_into_recovery ? "boot" : "recovery"), imagesize_actual);
2337 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
2338
Shashank Mittalcd98d472011-08-02 14:29:24 -07002339 /* Authenticate Kernel */
Deepa Dinamani23b60d42013-06-24 18:10:52 -07002340 if(target_use_signed_kernel() && (!device.is_unlocked))
Shashank Mittalcd98d472011-08-02 14:29:24 -07002341 {
Shashank Mittalcd98d472011-08-02 14:29:24 -07002342 offset = imagesize_actual;
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05302343
Shashank Mittalcd98d472011-08-02 14:29:24 -07002344 /* Read signature */
2345 if (flash_read(ptn, offset, (void *)(image_addr + offset), page_size))
2346 {
2347 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002348 return -1;
Shashank Mittalcd98d472011-08-02 14:29:24 -07002349 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07002350
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05302351 verify_signed_bootimg((uint32_t)image_addr, imagesize_actual);
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05302352 }
2353 offset = page_size;
2354 if(hdr->second_size != 0) {
2355 if (UINT_MAX - offset < second_actual)
2356 {
2357 dprintf(CRITICAL, "ERROR: Integer overflow in boot image header %s\t%d\n",__func__,__LINE__);
2358 return -1;
vijay kumar8f53e362015-11-24 13:38:11 +05302359 }
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05302360 offset += second_actual;
2361 /* Second image loading not implemented. */
2362 ASSERT(0);
2363 }
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05302364 /* Move kernel and ramdisk to correct address */
2365 memmove((void*) hdr->kernel_addr, (char*) (image_addr + page_size), hdr->kernel_size);
2366 memmove((void*) hdr->ramdisk_addr, (char*) (image_addr + page_size + kernel_actual), hdr->ramdisk_size);
2367
2368#if DEVICE_TREE
2369 if(dt_size != 0) {
2370
2371 dt_table_offset = ((uint32_t)image_addr + page_size + kernel_actual + ramdisk_actual + second_actual);
2372
2373 table = (struct dt_table*) dt_table_offset;
2374
2375 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
2376 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
2377 return -1;
2378 }
2379
2380 /* Its Error if, dt_hdr_size (table->num_entries * dt_entry size + Dev_Tree Header)
2381 goes beyound hdr->dt_size*/
2382 if (dt_hdr_size > ROUND_TO_PAGE(dt_size,hdr->page_size)) {
2383 dprintf(CRITICAL, "ERROR: Invalid Device Tree size \n");
2384 return -1;
2385 }
2386
2387 /* Find index of device tree within device tree table */
2388 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
2389 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
2390 return -1;
2391 }
2392
2393 /* Validate and Read device device tree in the "tags_add */
2394 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size) ||
2395 check_ddr_addr_range_bound(hdr->tags_addr, dt_entry.size))
2396 {
2397 dprintf(CRITICAL, "Device tree addresses are not valid.\n");
2398 return -1;
2399 }
2400
2401 if(dt_entry.offset > (UINT_MAX - dt_entry.size)) {
2402 dprintf(CRITICAL, "ERROR: Device tree contents are Invalid\n");
2403 return -1;
2404 }
2405
2406 /* Ensure we are not overshooting dt_size with the dt_entry selected */
2407 if ((dt_entry.offset + dt_entry.size) > dt_size) {
2408 dprintf(CRITICAL, "ERROR: Device tree contents are Invalid\n");
2409 return -1;
2410 }
2411
2412 best_match_dt_addr = (unsigned char *)table + dt_entry.offset;
2413 dtb_size = dt_entry.size;
2414 memmove((void *)hdr->tags_addr, (char *)best_match_dt_addr, dtb_size);
Vivek Kumar07bd0862018-04-10 14:19:23 +05302415
2416 } else {
2417 /* Validate the tags_addr */
2418 if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual) ||
2419 check_ddr_addr_range_bound(hdr->tags_addr, kernel_actual))
2420 {
2421 dprintf(CRITICAL, "Device tree addresses are not valid.\n");
2422 return -1;
2423 }
2424 /*
2425 * If appended dev tree is found, update the atags with
2426 * memory address to the DTB appended location on RAM.
2427 * Else update with the atags address in the kernel header
Mayank Grover1ed3e8b2018-09-19 15:16:50 +05302428 *
2429 * Make sure everything from scratch address is read before next step!
2430 * In case of dtbo, this API is going to read dtbo on scratch.
Vivek Kumar07bd0862018-04-10 14:19:23 +05302431 */
2432 void *dtb = NULL;
2433 dtb = dev_tree_appended((void*)(image_addr + page_size ),hdr->kernel_size, dtb_offset, (void *)hdr->tags_addr);
2434 if (!dtb) {
2435 dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
2436 return -1;
2437 }
2438 }
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05302439#endif
2440 if(target_use_signed_kernel() && (!device.is_unlocked))
2441 {
Shashank Mittal162244e2011-08-08 19:01:25 -07002442 /* Make sure everything from scratch address is read before next step!*/
Shashank Mittala0032282011-08-26 14:50:11 -07002443 if(device.is_tampered)
Shashank Mittal162244e2011-08-08 19:01:25 -07002444 {
2445 write_device_info_flash(&device);
2446 }
Channagoud Kadabi5c86fe32012-02-16 10:58:48 +05302447#if USE_PCOM_SECBOOT
2448 set_tamper_flag(device.is_tampered);
2449#endif
Shashank Mittalcd98d472011-08-02 14:29:24 -07002450 }
David Ng183a7422009-12-07 14:55:21 -08002451continue_boot:
Dima Zavin214cc642009-01-26 11:16:21 -08002452
Dima Zavin214cc642009-01-26 11:16:21 -08002453 /* TODO: create/pass atags to kernel */
2454
Ajay Dudanie28a6072011-07-01 13:59:46 -07002455 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07002456 (const char *)hdr->cmdline, board_machtype(),
Dima Zavin214cc642009-01-26 11:16:21 -08002457 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
2458
2459 return 0;
2460}
Brian Swetland9c4c0752009-01-25 16:23:50 -08002461
Shashank Mittal162244e2011-08-08 19:01:25 -07002462void write_device_info_mmc(device_info *dev)
2463{
Shashank Mittal162244e2011-08-08 19:01:25 -07002464 unsigned long long ptn = 0;
2465 unsigned long long size;
2466 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002467 uint8_t lun = 0;
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002468 uint32_t ret = 0;
Mayank Groverddd348d2018-01-23 14:07:09 +05302469 uint32_t device_info_sz = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07002470
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002471 if (devinfo_present)
2472 index = partition_get_index("devinfo");
2473 else
2474 index = partition_get_index("aboot");
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07002475
Shashank Mittal162244e2011-08-08 19:01:25 -07002476 ptn = partition_get_offset(index);
2477 if(ptn == 0)
2478 {
2479 return;
2480 }
2481
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002482 lun = partition_get_lun(index);
2483 mmc_set_lun(lun);
2484
Shashank Mittal162244e2011-08-08 19:01:25 -07002485 size = partition_get_size(index);
2486
Mayank Groverddd348d2018-01-23 14:07:09 +05302487 device_info_sz = ROUND_TO_PAGE(sizeof(struct device_info),
2488 mmc_blocksize_mask);
2489 if (device_info_sz == UINT_MAX)
2490 {
2491 dprintf(CRITICAL, "ERROR: Incorrect blocksize of card\n");
2492 return;
2493 }
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07002494
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002495 if (devinfo_present)
Mayank Groverddd348d2018-01-23 14:07:09 +05302496 ret = mmc_write(ptn, device_info_sz, (void *)info_buf);
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002497 else
Mayank Groverddd348d2018-01-23 14:07:09 +05302498 ret = mmc_write((ptn + size - device_info_sz), device_info_sz, (void *)info_buf);
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002499 if (ret)
Shashank Mittal162244e2011-08-08 19:01:25 -07002500 {
2501 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
Sridhar Parasurame1a97922015-07-27 15:31:22 -07002502 ASSERT(0);
Shashank Mittal162244e2011-08-08 19:01:25 -07002503 }
2504}
2505
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002506void read_device_info_mmc(struct device_info *info)
Shashank Mittal162244e2011-08-08 19:01:25 -07002507{
Shashank Mittal162244e2011-08-08 19:01:25 -07002508 unsigned long long ptn = 0;
2509 unsigned long long size;
2510 int index = INVALID_PTN;
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002511 uint32_t ret = 0;
Mayank Groverddd348d2018-01-23 14:07:09 +05302512 uint32_t device_info_sz = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07002513
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002514 if ((index = partition_get_index("devinfo")) < 0)
2515 {
2516 devinfo_present = false;
2517 index = partition_get_index("aboot");
2518 }
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07002519
Shashank Mittal162244e2011-08-08 19:01:25 -07002520 ptn = partition_get_offset(index);
2521 if(ptn == 0)
2522 {
2523 return;
2524 }
2525
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07002526 mmc_set_lun(partition_get_lun(index));
2527
Shashank Mittal162244e2011-08-08 19:01:25 -07002528 size = partition_get_size(index);
2529
Mayank Groverddd348d2018-01-23 14:07:09 +05302530 device_info_sz = ROUND_TO_PAGE(sizeof(struct device_info),
2531 mmc_blocksize_mask);
2532 if (device_info_sz == UINT_MAX)
2533 {
2534 dprintf(CRITICAL, "ERROR: Incorrect blocksize of card\n");
2535 return;
2536 }
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07002537
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002538 if (devinfo_present)
Mayank Groverddd348d2018-01-23 14:07:09 +05302539 ret = mmc_read(ptn, (void *)info_buf, device_info_sz);
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002540 else
Mayank Groverddd348d2018-01-23 14:07:09 +05302541 ret = mmc_read((ptn + size - device_info_sz), (void *)info_buf, device_info_sz);
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002542 if (ret)
Shashank Mittal162244e2011-08-08 19:01:25 -07002543 {
2544 dprintf(CRITICAL, "ERROR: Cannot read device info\n");
Sridhar Parasurame1a97922015-07-27 15:31:22 -07002545 ASSERT(0);
Shashank Mittal162244e2011-08-08 19:01:25 -07002546 }
Shashank Mittal162244e2011-08-08 19:01:25 -07002547}
2548
2549void write_device_info_flash(device_info *dev)
2550{
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002551 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
Shashank Mittal162244e2011-08-08 19:01:25 -07002552 struct ptentry *ptn;
2553 struct ptable *ptable;
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002554 if(info == NULL)
2555 {
2556 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
2557 ASSERT(0);
2558 }
2559 info_buf = info;
Shashank Mittal162244e2011-08-08 19:01:25 -07002560 ptable = flash_get_ptable();
2561 if (ptable == NULL)
2562 {
2563 dprintf(CRITICAL, "ERROR: Partition table not found\n");
2564 return;
2565 }
2566
2567 ptn = ptable_find(ptable, "devinfo");
2568 if (ptn == NULL)
2569 {
Smita Ghosh670c8b82015-05-07 09:30:03 -07002570 dprintf(CRITICAL, "ERROR: No devinfo partition found\n");
Shashank Mittal162244e2011-08-08 19:01:25 -07002571 return;
2572 }
2573
Mayank Groverdedbc892017-10-24 13:41:34 +05302574 memset(info, 0, BOOT_IMG_MAX_PAGE_SIZE);
Shashank Mittal162244e2011-08-08 19:01:25 -07002575 memcpy(info, dev, sizeof(device_info));
2576
2577 if (flash_write(ptn, 0, (void *)info_buf, page_size))
2578 {
2579 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
2580 return;
2581 }
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002582 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07002583}
2584
vijay kumarc65876c2015-04-24 13:29:16 +05302585static int read_allow_oem_unlock(device_info *dev)
2586{
vijay kumarc65876c2015-04-24 13:29:16 +05302587 unsigned offset;
2588 int index;
2589 unsigned long long ptn;
2590 unsigned long long ptn_size;
2591 unsigned blocksize = mmc_get_device_blocksize();
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07002592 STACKBUF_DMA_ALIGN(buf, blocksize);
vijay kumarc65876c2015-04-24 13:29:16 +05302593
vijay kumarca2e6812015-07-08 20:28:25 +05302594 index = partition_get_index(frp_ptns[0]);
vijay kumarc65876c2015-04-24 13:29:16 +05302595 if (index == INVALID_PTN)
2596 {
vijay kumarca2e6812015-07-08 20:28:25 +05302597 index = partition_get_index(frp_ptns[1]);
2598 if (index == INVALID_PTN)
2599 {
2600 dprintf(CRITICAL, "Neither '%s' nor '%s' partition found\n", frp_ptns[0],frp_ptns[1]);
2601 return -1;
2602 }
vijay kumarc65876c2015-04-24 13:29:16 +05302603 }
2604
2605 ptn = partition_get_offset(index);
2606 ptn_size = partition_get_size(index);
2607 offset = ptn_size - blocksize;
2608
Mayank Grover48860402016-11-29 12:34:53 +05302609 /* Set Lun for partition */
2610 mmc_set_lun(partition_get_lun(index));
2611
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07002612 if (mmc_read(ptn + offset, (void *)buf, blocksize))
vijay kumarc65876c2015-04-24 13:29:16 +05302613 {
2614 dprintf(CRITICAL, "Reading MMC failed\n");
2615 return -1;
2616 }
2617
2618 /*is_allow_unlock is a bool value stored at the LSB of last byte*/
2619 is_allow_unlock = buf[blocksize-1] & 0x01;
2620 return 0;
2621}
2622
2623static int write_allow_oem_unlock(bool allow_unlock)
2624{
vijay kumarc65876c2015-04-24 13:29:16 +05302625 unsigned offset;
vijay kumarc65876c2015-04-24 13:29:16 +05302626 int index;
2627 unsigned long long ptn;
2628 unsigned long long ptn_size;
2629 unsigned blocksize = mmc_get_device_blocksize();
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07002630 STACKBUF_DMA_ALIGN(buf, blocksize);
vijay kumarc65876c2015-04-24 13:29:16 +05302631
vijay kumarca2e6812015-07-08 20:28:25 +05302632 index = partition_get_index(frp_ptns[0]);
vijay kumarc65876c2015-04-24 13:29:16 +05302633 if (index == INVALID_PTN)
2634 {
vijay kumarca2e6812015-07-08 20:28:25 +05302635 index = partition_get_index(frp_ptns[1]);
2636 if (index == INVALID_PTN)
2637 {
2638 dprintf(CRITICAL, "Neither '%s' nor '%s' partition found\n", frp_ptns[0],frp_ptns[1]);
2639 return -1;
2640 }
vijay kumarc65876c2015-04-24 13:29:16 +05302641 }
2642
2643 ptn = partition_get_offset(index);
2644 ptn_size = partition_get_size(index);
2645 offset = ptn_size - blocksize;
Mayank Grover48860402016-11-29 12:34:53 +05302646 mmc_set_lun(partition_get_lun(index));
vijay kumarc65876c2015-04-24 13:29:16 +05302647
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07002648 if (mmc_read(ptn + offset, (void *)buf, blocksize))
vijay kumarc65876c2015-04-24 13:29:16 +05302649 {
2650 dprintf(CRITICAL, "Reading MMC failed\n");
2651 return -1;
2652 }
2653
2654 /*is_allow_unlock is a bool value stored at the LSB of last byte*/
2655 buf[blocksize-1] = allow_unlock;
2656 if (mmc_write(ptn + offset, blocksize, buf))
2657 {
2658 dprintf(CRITICAL, "Writing MMC failed\n");
2659 return -1;
2660 }
2661
2662 return 0;
2663}
2664
Shashank Mittal162244e2011-08-08 19:01:25 -07002665void read_device_info_flash(device_info *dev)
2666{
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002667 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
Shashank Mittal162244e2011-08-08 19:01:25 -07002668 struct ptentry *ptn;
2669 struct ptable *ptable;
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002670 if(info == NULL)
2671 {
2672 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
2673 ASSERT(0);
2674 }
2675 info_buf = info;
Shashank Mittal162244e2011-08-08 19:01:25 -07002676 ptable = flash_get_ptable();
2677 if (ptable == NULL)
2678 {
2679 dprintf(CRITICAL, "ERROR: Partition table not found\n");
2680 return;
2681 }
2682
2683 ptn = ptable_find(ptable, "devinfo");
2684 if (ptn == NULL)
2685 {
Smita Ghosh670c8b82015-05-07 09:30:03 -07002686 dprintf(CRITICAL, "ERROR: No devinfo partition found\n");
Shashank Mittal162244e2011-08-08 19:01:25 -07002687 return;
2688 }
2689
2690 if (flash_read(ptn, 0, (void *)info_buf, page_size))
2691 {
2692 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
2693 return;
2694 }
2695
2696 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
2697 {
Shashank Mittal162244e2011-08-08 19:01:25 -07002698 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
2699 info->is_unlocked = 0;
Shashank Mittala0032282011-08-26 14:50:11 -07002700 info->is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07002701 write_device_info_flash(info);
2702 }
2703 memcpy(dev, info, sizeof(device_info));
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002704 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07002705}
2706
2707void write_device_info(device_info *dev)
2708{
2709 if(target_is_emmc_boot())
2710 {
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002711 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
2712 if(info == NULL)
2713 {
2714 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
2715 ASSERT(0);
2716 }
2717 info_buf = info;
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002718 memcpy(info, dev, sizeof(struct device_info));
2719
2720#if USE_RPMB_FOR_DEVINFO
Mayank Grover912eaa62017-10-26 12:08:53 +05302721 if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05302722 is_secure_boot_enable()) {
2723 if((write_device_info_rpmb((void*) info, PAGE_SIZE)) < 0)
2724 ASSERT(0);
Sridhar Parasurame1a97922015-07-27 15:31:22 -07002725 }
Channagoud Kadabic80cb492015-04-28 16:08:28 -07002726 else
2727 write_device_info_mmc(info);
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002728#else
2729 write_device_info_mmc(info);
2730#endif
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002731 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07002732 }
2733 else
2734 {
2735 write_device_info_flash(dev);
2736 }
2737}
2738
Monika Singh94316462018-03-15 18:39:01 +05302739int read_rollback_index(uint32_t loc, uint64_t *roll_back_index)
2740{
2741 if (!devinfo_present) {
2742 dprintf(CRITICAL, "DeviceInfo not initalized \n");
2743 return -EINVAL;
2744 }
2745 if (loc >= ARRAY_SIZE(device.rollback_index)) {
2746 dprintf(CRITICAL, "%s() Loc out of range index: %d, array len: %d\n",
2747 __func__, loc, ARRAY_SIZE(device.rollback_index));
2748 ASSERT(0);
2749 }
2750
2751 *roll_back_index = device.rollback_index[loc];
2752 return 0;
2753}
2754
2755int write_rollback_index(uint32_t loc, uint64_t roll_back_index)
2756{
2757 if (!devinfo_present) {
2758 dprintf(CRITICAL, "DeviceInfo not initalized \n");
2759 return -EINVAL;
2760 }
2761 if (loc >= ARRAY_SIZE(device.rollback_index)) {
2762 dprintf(CRITICAL, "%s() Loc out of range index: %d, array len: %d\n",
2763 __func__, loc, ARRAY_SIZE(device.rollback_index));
2764 ASSERT(0);
2765 }
2766
2767 device.rollback_index[loc] = roll_back_index;
2768 write_device_info(&device);
2769 return 0;
2770}
2771
Monika Singhb0fad822018-03-15 18:50:55 +05302772int store_userkey(uint8_t *user_key, uint32_t user_key_size)
2773{
2774 if (!devinfo_present) {
2775 dprintf(CRITICAL, "DeviceInfo not initalized \n");
2776 return -EINVAL;
2777 }
2778
2779 if (user_key_size > ARRAY_SIZE(device.user_public_key)) {
2780 dprintf(CRITICAL, "StoreUserKey, UserKeySize too large!\n");
2781 return -ENODEV;
2782 }
2783
2784 memcpy(device.user_public_key, user_key, user_key_size);
2785 device.user_public_key_length = user_key_size;
2786 write_device_info(&device);
2787 return 0;
2788}
2789
2790int erase_userkey()
2791{
2792 if (!devinfo_present) {
2793 dprintf(CRITICAL, "DeviceInfo not initalized \n");
2794 return -EINVAL;
2795 }
2796 memset(device.user_public_key, 0, ARRAY_SIZE(device.user_public_key));
2797 device.user_public_key_length = 0;
2798 write_device_info(&device);
2799 return 0;
2800}
2801
2802int get_userkey(uint8_t **user_key, uint32_t *user_key_size)
2803{
2804 if (!devinfo_present) {
2805 dprintf(CRITICAL, "DeviceInfo not initalized \n");
2806 return -EINVAL;
2807 }
2808 *user_key = device.user_public_key;
2809 *user_key_size = device.user_public_key_length;
2810 return 0;
2811}
2812
Shashank Mittal162244e2011-08-08 19:01:25 -07002813void read_device_info(device_info *dev)
2814{
2815 if(target_is_emmc_boot())
2816 {
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002817 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
2818 if(info == NULL)
2819 {
2820 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
2821 ASSERT(0);
2822 }
2823 info_buf = info;
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002824
2825#if USE_RPMB_FOR_DEVINFO
Mayank Grover912eaa62017-10-26 12:08:53 +05302826 if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05302827 is_secure_boot_enable()) {
2828 if((read_device_info_rpmb((void*) info, PAGE_SIZE)) < 0)
2829 ASSERT(0);
Sridhar Parasurame1a97922015-07-27 15:31:22 -07002830 }
Channagoud Kadabic80cb492015-04-28 16:08:28 -07002831 else
2832 read_device_info_mmc(info);
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002833#else
2834 read_device_info_mmc(info);
2835#endif
2836
2837 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
2838 {
2839 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
lijuang511a2b52015-08-14 20:50:51 +08002840 if (is_secure_boot_enable()) {
Channagoud Kadabi05f78ba2015-07-06 11:58:14 -07002841 info->is_unlocked = 0;
Monika Singh292b3e92018-03-17 22:40:23 +05302842#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover912eaa62017-10-26 12:08:53 +05302843 if (VB_M <= target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +05302844 info->is_unlock_critical = 0;
Parth Dixitddbc7352015-10-18 03:13:31 +05302845#endif
lijuang511a2b52015-08-14 20:50:51 +08002846 } else {
Channagoud Kadabi2fda4092015-07-07 13:34:11 -07002847 info->is_unlocked = 1;
Monika Singh292b3e92018-03-17 22:40:23 +05302848#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover912eaa62017-10-26 12:08:53 +05302849 if (VB_M <= target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +05302850 info->is_unlock_critical = 1;
Parth Dixitddbc7352015-10-18 03:13:31 +05302851#endif
lijuang511a2b52015-08-14 20:50:51 +08002852 }
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002853 info->is_tampered = 0;
2854 info->charger_screen_enabled = 0;
Monika Singh292b3e92018-03-17 22:40:23 +05302855#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover912eaa62017-10-26 12:08:53 +05302856 if (VB_M <= target_get_vb_version())
Monika Singh1c71a1c2019-12-03 12:12:48 +05302857 {
Mayank Grover889be1b2017-09-12 20:12:23 +05302858 info->verity_mode = 1; //enforcing by default
Monika Singh1c71a1c2019-12-03 12:12:48 +05302859 info->user_public_key_length = 0;
2860 memset(info->rollback_index, 0, sizeof(info->rollback_index));
2861 memset(info->user_public_key, 0, sizeof(info->user_public_key));
2862 }
Parth Dixitddbc7352015-10-18 03:13:31 +05302863#endif
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002864 write_device_info(info);
2865 }
jessicatseng1b9da342020-04-22 11:13:20 +08002866
2867//<2020/04/22-JessicaTseng, Enable off-charging
2868 info->charger_screen_enabled = 1;
2869 write_device_info(info);
2870//>2020/04/22-JessicaTseng
2871
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002872 memcpy(dev, info, sizeof(device_info));
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002873 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07002874 }
2875 else
2876 {
2877 read_device_info_flash(dev);
2878 }
2879}
2880
2881void reset_device_info()
2882{
2883 dprintf(ALWAYS, "reset_device_info called.");
Shashank Mittala0032282011-08-26 14:50:11 -07002884 device.is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07002885 write_device_info(&device);
2886}
2887
2888void set_device_root()
2889{
2890 dprintf(ALWAYS, "set_device_root called.");
Shashank Mittala0032282011-08-26 14:50:11 -07002891 device.is_tampered = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -07002892 write_device_info(&device);
2893}
2894
lijuang4ece1e72015-08-14 21:02:36 +08002895/* set device unlock value
2896 * Must check FRP before call this function
2897 * Need to wipe data when unlock status changed
2898 * type 0: oem unlock
2899 * type 1: unlock critical
2900 * status 0: unlock as false
2901 * status 1: lock as true
2902 */
2903void set_device_unlock_value(int type, bool status)
lijuang21f12f52015-08-22 16:22:19 +08002904{
lijuang4ece1e72015-08-14 21:02:36 +08002905 if (type == UNLOCK)
2906 device.is_unlocked = status;
Monika Singh292b3e92018-03-17 22:40:23 +05302907#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover912eaa62017-10-26 12:08:53 +05302908 else if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05302909 type == UNLOCK_CRITICAL)
2910 device.is_unlock_critical = status;
Parth Dixitddbc7352015-10-18 03:13:31 +05302911#endif
lijuang4ece1e72015-08-14 21:02:36 +08002912 write_device_info(&device);
2913}
2914
2915static void set_device_unlock(int type, bool status)
2916{
2917 int is_unlocked = -1;
2918 char response[MAX_RSP_SIZE];
2919
2920 /* check device unlock status if it is as expected */
2921 if (type == UNLOCK)
2922 is_unlocked = device.is_unlocked;
Monika Singh292b3e92018-03-17 22:40:23 +05302923#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover912eaa62017-10-26 12:08:53 +05302924 if(VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05302925 type == UNLOCK_CRITICAL)
2926 {
2927 is_unlocked = device.is_unlock_critical;
2928 }
Parth Dixitddbc7352015-10-18 03:13:31 +05302929#endif
lijuang4ece1e72015-08-14 21:02:36 +08002930 if (is_unlocked == status) {
2931 snprintf(response, sizeof(response), "\tDevice already : %s", (status ? "unlocked!" : "locked!"));
2932 fastboot_info(response);
2933 fastboot_okay("");
2934 return;
2935 }
2936
2937 /* status is true, it means to unlock device */
lijuang07c5d6e2018-04-23 18:32:13 +08002938 if (status && !is_allow_unlock) {
2939 fastboot_fail("oem unlock is not allowed");
2940 return;
2941 }
lijuang21f12f52015-08-22 16:22:19 +08002942
lijuang4ece1e72015-08-14 21:02:36 +08002943#if FBCON_DISPLAY_MSG
lijuang07c5d6e2018-04-23 18:32:13 +08002944 display_unlock_menu(type, status);
2945 fastboot_okay("");
2946 return;
lijuang4ece1e72015-08-14 21:02:36 +08002947#else
lijuang07c5d6e2018-04-23 18:32:13 +08002948 if (status && type == UNLOCK) {
2949 fastboot_fail("Need wipe userdata. Do 'fastboot oem unlock-go'");
2950 return;
lijuang21f12f52015-08-22 16:22:19 +08002951 }
lijuang07c5d6e2018-04-23 18:32:13 +08002952#endif
lijuang4ece1e72015-08-14 21:02:36 +08002953
2954 set_device_unlock_value(type, status);
2955
2956 /* wipe data */
2957 struct recovery_message msg;
Kishor PK60a68212017-05-08 16:55:57 +05302958 memset(&msg, 0, sizeof(msg));
lijuang4ece1e72015-08-14 21:02:36 +08002959 snprintf(msg.recovery, sizeof(msg.recovery), "recovery\n--wipe_data");
2960 write_misc(0, &msg, sizeof(msg));
2961
2962 fastboot_okay("");
2963 reboot_device(RECOVERY_MODE);
lijuang21f12f52015-08-22 16:22:19 +08002964}
2965
lijuang511a2b52015-08-14 20:50:51 +08002966static bool critical_flash_allowed(const char * entry)
2967{
2968 uint32_t i = 0;
2969 if (entry == NULL)
2970 return false;
2971
2972 for (i = 0; i < ARRAY_SIZE(critical_flash_allowed_ptn); i++) {
2973 if(!strcmp(entry, critical_flash_allowed_ptn[i]))
2974 return true;
2975 }
2976 return false;
Matthew Qin271927e2015-03-31 22:07:07 -04002977}
2978
2979#if DEVICE_TREE
Amol Jadicb524072012-08-09 16:40:18 -07002980int copy_dtb(uint8_t *boot_image_start, unsigned int scratch_offset)
2981{
2982 uint32 dt_image_offset = 0;
Amol Jadicb524072012-08-09 16:40:18 -07002983 uint32_t n;
Monika Singh292b3e92018-03-17 22:40:23 +05302984 struct dt_table *table = NULL;
Amol Jadicb524072012-08-09 16:40:18 -07002985 struct dt_entry dt_entry;
Monika Singh292b3e92018-03-17 22:40:23 +05302986 uint32_t dt_hdr_size = 0;
Amol Jadicb524072012-08-09 16:40:18 -07002987 unsigned int compressed_size = 0;
2988 unsigned int dtb_size = 0;
2989 unsigned int out_avai_len = 0;
2990 unsigned char *out_addr = NULL;
2991 unsigned char *best_match_dt_addr = NULL;
2992 int rc;
2993
Mayank Grovere1ab96c2018-09-04 20:31:31 +05302994 boot_img_hdr *hdr = (boot_img_hdr *) (boot_image_start);
Amol Jadicb524072012-08-09 16:40:18 -07002995
Parth Dixit4097b622016-03-15 14:42:27 +05302996#ifndef OSVERSION_IN_BOOTIMAGE
2997 dt_size = hdr->dt_size;
2998#endif
2999
3000 if(dt_size != 0) {
Amol Jadicb524072012-08-09 16:40:18 -07003001 /* add kernel offset */
3002 dt_image_offset += page_size;
3003 n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
3004 dt_image_offset += n;
3005
3006 /* add ramdisk offset */
3007 n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
3008 dt_image_offset += n;
3009
3010 /* add second offset */
3011 if(hdr->second_size != 0) {
3012 n = ROUND_TO_PAGE(hdr->second_size, page_mask);
3013 dt_image_offset += n;
3014 }
3015
3016 /* offset now point to start of dt.img */
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07003017 table = (struct dt_table*)(boot_image_start + dt_image_offset);
Amol Jadicb524072012-08-09 16:40:18 -07003018
Deepa Dinamani19648b42013-09-05 17:05:55 -07003019 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Amol Jadicb524072012-08-09 16:40:18 -07003020 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
3021 return -1;
3022 }
P.V. Phani Kumar82916762016-03-09 09:20:19 +05303023
3024 /* Its Error if, dt_hdr_size (table->num_entries * dt_entry size + Dev_Tree Header)
3025 goes beyound hdr->dt_size*/
Parth Dixit4097b622016-03-15 14:42:27 +05303026 if (dt_hdr_size > ROUND_TO_PAGE(dt_size,hdr->page_size)) {
P.V. Phani Kumar82916762016-03-09 09:20:19 +05303027 dprintf(CRITICAL, "ERROR: Invalid Device Tree size \n");
3028 return -1;
3029 }
3030
Joel Kingaa335dc2013-06-03 16:11:08 -07003031 /* Find index of device tree within device tree table */
3032 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Amol Jadicb524072012-08-09 16:40:18 -07003033 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
3034 return -1;
3035 }
3036
Matthew Qin271927e2015-03-31 22:07:07 -04003037 best_match_dt_addr = (unsigned char *)boot_image_start + dt_image_offset + dt_entry.offset;
3038 if (is_gzip_package(best_match_dt_addr, dt_entry.size))
3039 {
3040 out_addr = (unsigned char *)target_get_scratch_address() + scratch_offset;
3041 out_avai_len = target_get_max_flash_size() - scratch_offset;
Matthew Qin0b15b322015-05-19 05:20:54 -04003042 dprintf(INFO, "decompressing dtb: start\n");
Matthew Qin271927e2015-03-31 22:07:07 -04003043 rc = decompress(best_match_dt_addr,
3044 dt_entry.size, out_addr, out_avai_len,
3045 &compressed_size, &dtb_size);
3046 if (rc)
3047 {
Matthew Qin0b15b322015-05-19 05:20:54 -04003048 dprintf(CRITICAL, "decompressing dtb failed!!!\n");
Matthew Qin271927e2015-03-31 22:07:07 -04003049 ASSERT(0);
3050 }
3051
Matthew Qin0b15b322015-05-19 05:20:54 -04003052 dprintf(INFO, "decompressing dtb: done\n");
Matthew Qin271927e2015-03-31 22:07:07 -04003053 best_match_dt_addr = out_addr;
3054 } else {
3055 dtb_size = dt_entry.size;
3056 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07003057 /* Validate and Read device device tree in the "tags_add */
Mayank Grovere559cec2017-10-17 15:12:03 +05303058 if (check_aboot_addr_range_overlap(hdr->tags_addr, dtb_size) ||
3059 check_ddr_addr_range_bound(hdr->tags_addr, dtb_size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07003060 {
Mayank Grovere559cec2017-10-17 15:12:03 +05303061 dprintf(CRITICAL, "Device tree addresses are not valid.\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07003062 return -1;
3063 }
3064
Amol Jadicb524072012-08-09 16:40:18 -07003065 /* Read device device tree in the "tags_add */
Matthew Qin271927e2015-03-31 22:07:07 -04003066 memmove((void*) hdr->tags_addr, (void *)best_match_dt_addr, dtb_size);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07003067 } else
3068 return -1;
Amol Jadicb524072012-08-09 16:40:18 -07003069
3070 /* Everything looks fine. Return success. */
3071 return 0;
3072}
3073#endif
3074
Brian Swetland9c4c0752009-01-25 16:23:50 -08003075void cmd_boot(const char *arg, void *data, unsigned sz)
3076{
3077 unsigned kernel_actual;
3078 unsigned ramdisk_actual;
Kishor PKd8ddcad2017-07-27 13:53:57 +05303079 unsigned second_actual;
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07003080 uint32_t image_actual;
3081 uint32_t dt_actual = 0;
Mayank Grovere1ab96c2018-09-04 20:31:31 +05303082 boot_img_hdr *hdr = NULL;
Matthew Qinbb7923d2015-02-09 10:56:09 +08003083 struct kernel64_hdr *kptr = NULL;
Brian Swetland9c4c0752009-01-25 16:23:50 -08003084 char *ptr = ((char*) data);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07003085 int ret = 0;
3086 uint8_t dtb_copied = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08003087 unsigned int out_len = 0;
3088 unsigned int out_avai_len = 0;
3089 unsigned char *out_addr = NULL;
3090 uint32_t dtb_offset = 0;
3091 unsigned char *kernel_start_addr = NULL;
3092 unsigned int kernel_size = 0;
Matthew Qin271927e2015-03-31 22:07:07 -04003093 unsigned int scratch_offset = 0;
Saranya Chidurab4933332018-10-15 17:30:06 +05303094#if VERIFIED_BOOT_2
3095 void *dtbo_image_buf = NULL;
3096 uint32_t dtbo_image_sz = 0;
3097 void *vbmeta_image_buf = NULL;
3098 uint32_t vbmeta_image_sz = 0;
3099#endif
Monika Singh292b3e92018-03-17 22:40:23 +05303100#if !VERIFIED_BOOT_2
3101 uint32_t sig_actual = 0;
3102 uint32_t sig_size = 0;
3103#ifdef MDTP_SUPPORT
3104 static bool is_mdtp_activated = 0;
3105#endif /* MDTP_SUPPORT */
3106#endif
Matthew Qinbb7923d2015-02-09 10:56:09 +08003107
lijuang2008ff22016-03-07 17:56:27 +08003108#if FBCON_DISPLAY_MSG
3109 /* Exit keys' detection thread firstly */
3110 exit_menu_keys_detection();
3111#endif
3112
Monika Singh292b3e92018-03-17 22:40:23 +05303113#if VERIFIED_BOOT || VERIFIED_BOOT_2
Channagoud Kadabi6d5375e2015-06-23 17:15:42 -07003114 if(target_build_variant_user() && !device.is_unlocked)
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003115 {
3116 fastboot_fail("unlock device to use this command");
lijuang2008ff22016-03-07 17:56:27 +08003117 goto boot_failed;
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003118 }
3119#endif
3120
Brian Swetland9c4c0752009-01-25 16:23:50 -08003121 if (sz < sizeof(hdr)) {
3122 fastboot_fail("invalid bootimage header");
lijuang2008ff22016-03-07 17:56:27 +08003123 goto boot_failed;
Brian Swetland9c4c0752009-01-25 16:23:50 -08003124 }
3125
Mayank Grovere1ab96c2018-09-04 20:31:31 +05303126 hdr = (boot_img_hdr *)data;
Brian Swetland9c4c0752009-01-25 16:23:50 -08003127
3128 /* ensure commandline is terminated */
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07003129 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -08003130
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07003131 if(target_is_emmc_boot() && hdr->page_size) {
3132 page_size = hdr->page_size;
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07003133 page_mask = page_size - 1;
3134 }
3135
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07003136 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
3137 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Kishor PKd8ddcad2017-07-27 13:53:57 +05303138 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07003139#if DEVICE_TREE
Parth Dixit4097b622016-03-15 14:42:27 +05303140#ifndef OSVERSION_IN_BOOTIMAGE
3141 dt_size = hdr->dt_size;
3142#endif
3143 dt_actual = ROUND_TO_PAGE(dt_size, page_mask);
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07003144#endif
3145
3146 image_actual = ADD_OF(page_size, kernel_actual);
3147 image_actual = ADD_OF(image_actual, ramdisk_actual);
Kishor PKd8ddcad2017-07-27 13:53:57 +05303148 image_actual = ADD_OF(image_actual, second_actual);
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07003149 image_actual = ADD_OF(image_actual, dt_actual);
3150
Kishor PK5134b332017-05-09 17:50:08 +05303151 /* Checking to prevent oob access in read_der_message_length */
3152 if (image_actual > sz) {
3153 fastboot_fail("bootimage header fields are invalid");
3154 goto boot_failed;
3155 }
Saranya Chidurab4933332018-10-15 17:30:06 +05303156
Monika Singh292b3e92018-03-17 22:40:23 +05303157#if VERIFIED_BOOT_2
Mayank Grover1ceaee22019-04-01 17:54:32 +05303158 /* Pass size of boot partition, as imgsize, to avoid
3159 read fewer bytes error */
3160 image_actual = partition_get_size(partition_get_index("boot"));
Saranya Chidurab4933332018-10-15 17:30:06 +05303161
3162 /* load and validate dtbo partition */
3163 load_validate_dtbo_image(&dtbo_image_buf, &dtbo_image_sz);
3164
3165 /* load vbmeta partition */
3166 load_vbmeta_image(&vbmeta_image_buf, &vbmeta_image_sz);
3167
Monika Singh292b3e92018-03-17 22:40:23 +05303168 memset(&info, 0, sizeof(bootinfo));
Saranya Chidurab4933332018-10-15 17:30:06 +05303169
3170 info.images[IMG_BOOT].image_buffer = SUB_SALT_BUFF_OFFSET(data);
3171 info.images[IMG_BOOT].imgsize = image_actual;
3172 info.images[IMG_BOOT].name = "boot";
3173 ++info.num_loaded_images;
3174
3175 /* Pass loaded dtbo image */
3176 if (dtbo_image_buf != NULL) {
3177 info.images[IMG_DTBO].image_buffer = SUB_SALT_BUFF_OFFSET(dtbo_image_buf);
3178 info.images[IMG_DTBO].imgsize = dtbo_image_sz;
3179 info.images[IMG_DTBO].name = "dtbo";
3180 ++info.num_loaded_images;
3181 }
3182
3183 /* Pass loaded vbmeta image */
3184 if (vbmeta_image_buf != NULL) {
3185 info.images[IMG_VBMETA].image_buffer = vbmeta_image_buf;
3186 info.images[IMG_VBMETA].imgsize = vbmeta_image_sz;
3187 info.images[IMG_VBMETA].name = "vbmeta";
3188 ++info.num_loaded_images;
3189 }
3190
Monika Singh292b3e92018-03-17 22:40:23 +05303191 info.multi_slot_boot = partition_multislot_is_supported();
3192 if (load_image_and_auth(&info))
3193 goto boot_failed;
3194 vbcmdline = info.vbcmdline;
Saranya Chidurab4933332018-10-15 17:30:06 +05303195
3196 /* Free the buffer allocated to vbmeta post verification */
3197 if (vbmeta_image_buf != NULL) {
3198 free(vbmeta_image_buf);
3199 --info.num_loaded_images;
3200 }
Monika Singh292b3e92018-03-17 22:40:23 +05303201#else
Kishor PK5134b332017-05-09 17:50:08 +05303202 sig_size = sz - image_actual;
3203
P.V. Phani Kumareecfd822016-03-09 20:09:03 +05303204 if (target_use_signed_kernel() && (!device.is_unlocked)) {
Monika Singh292b3e92018-03-17 22:40:23 +05303205 unsigned chk;
P.V. Phani Kumareecfd822016-03-09 20:09:03 +05303206 /* Calculate the signature length from boot image */
3207 sig_actual = read_der_message_length(
Kishor PK5134b332017-05-09 17:50:08 +05303208 (unsigned char*)(data + image_actual), sig_size);
Monika Singh292b3e92018-03-17 22:40:23 +05303209 chk = ADD_OF(image_actual, sig_actual);
Channagoud Kadabi3d839012014-06-26 14:26:39 -07003210
Monika Singh292b3e92018-03-17 22:40:23 +05303211 if (chk > sz) {
Kishor PK5134b332017-05-09 17:50:08 +05303212 fastboot_fail("bootimage header fields are invalid");
3213 goto boot_failed;
3214 }
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07003215 }
3216
Sridhar Parasuram361e4cd2015-09-24 11:19:19 -07003217 // Initialize boot state before trying to verify boot.img
3218#if VERIFIED_BOOT
Channagoud Kadabi699466e2015-11-03 12:37:42 -08003219 boot_verifier_init();
Mayank Groverb337e932017-01-18 20:00:40 +05303220#endif
Channagoud Kadabibf6ce7d2015-09-17 13:25:35 -07003221 /* Handle overflow if the input image size is greater than
3222 * boot image buffer can hold
3223 */
Monika Singh292b3e92018-03-17 22:40:23 +05303224 if ((target_get_max_flash_size() - page_size) < image_actual)
Channagoud Kadabibf6ce7d2015-09-17 13:25:35 -07003225 {
3226 fastboot_fail("booimage: size is greater than boot image buffer can hold");
lijuang2008ff22016-03-07 17:56:27 +08003227 goto boot_failed;
Channagoud Kadabibf6ce7d2015-09-17 13:25:35 -07003228 }
Sridhar Parasuram361e4cd2015-09-24 11:19:19 -07003229
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07003230 /* Verify the boot image
3231 * device & page_size are initialized in aboot_init
3232 */
Sridhar Parasuram7ae9fbc2015-09-17 09:56:30 -07003233 if (target_use_signed_kernel() && (!device.is_unlocked)) {
Channagoud Kadabi3d839012014-06-26 14:26:39 -07003234 /* Pass size excluding signature size, otherwise we would try to
3235 * access signature beyond its length
3236 */
Monika Singh292b3e92018-03-17 22:40:23 +05303237 verify_signed_bootimg((uint32_t)data, image_actual);
Sridhar Parasuram7ae9fbc2015-09-17 09:56:30 -07003238 }
Amit Blay4aa292f2015-04-28 21:55:59 +03003239#ifdef MDTP_SUPPORT
3240 else
3241 {
Shay Nachmani062aa3f2015-05-17 17:28:44 +03003242 /* fastboot boot is not allowed when MDTP is activated */
Amit Blay4aa292f2015-04-28 21:55:59 +03003243 mdtp_ext_partition_verification_t ext_partition;
Amit Blay8a510302015-08-17 09:20:01 +03003244
3245 if (!is_mdtp_activated) {
3246 ext_partition.partition = MDTP_PARTITION_NONE;
3247 mdtp_fwlock_verify_lock(&ext_partition);
3248 }
Amit Blay4aa292f2015-04-28 21:55:59 +03003249 }
Shay Nachmani062aa3f2015-05-17 17:28:44 +03003250
Amir Kotzer7c768c02016-04-13 09:08:05 +03003251 /* If mdtp state cannot be validate, block fastboot boot*/
3252 if(mdtp_activated(&is_mdtp_activated)){
3253 dprintf(CRITICAL, "mdtp_activated cannot validate state.\n");
3254 dprintf(CRITICAL, "Can not proceed with fastboot boot command.\n");
3255 goto boot_failed;
3256 }
Shay Nachmani062aa3f2015-05-17 17:28:44 +03003257 if(is_mdtp_activated){
3258 dprintf(CRITICAL, "fastboot boot command is not available.\n");
lijuang2008ff22016-03-07 17:56:27 +08003259 goto boot_failed;
Shay Nachmani062aa3f2015-05-17 17:28:44 +03003260 }
Amit Blay4aa292f2015-04-28 21:55:59 +03003261#endif /* MDTP_SUPPORT */
Monika Singh292b3e92018-03-17 22:40:23 +05303262#endif /* VERIFIED_BOOT_2 else */
Amit Blay4aa292f2015-04-28 21:55:59 +03003263
Sridhar Parasuram361e4cd2015-09-24 11:19:19 -07003264#if VERIFIED_BOOT
Monika Singh292b3e92018-03-17 22:40:23 +05303265 if (VB_M == target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +05303266 {
3267 /* set boot and system versions. */
3268 set_os_version((unsigned char *)data);
3269 // send root of trust
3270 if(!send_rot_command((uint32_t)device.is_unlocked))
3271 ASSERT(0);
3272 }
Parth Dixitbc9b6492015-10-18 00:41:38 +05303273#endif
Channagoud Kadabia22144f2013-03-20 11:49:01 -07003274 /*
Matthew Qinbb7923d2015-02-09 10:56:09 +08003275 * Check if the kernel image is a gzip package. If yes, need to decompress it.
3276 * If not, continue booting.
3277 */
3278 if (is_gzip_package((unsigned char *)(data + page_size), hdr->kernel_size))
3279 {
3280 out_addr = (unsigned char *)target_get_scratch_address();
3281 out_addr = (unsigned char *)(out_addr + image_actual + page_size);
3282 out_avai_len = target_get_max_flash_size() - image_actual - page_size;
Saranya Chidurab4933332018-10-15 17:30:06 +05303283#if VERIFIED_BOOT_2
3284 if (dtbo_image_sz)
3285 out_avai_len -= DTBO_IMG_BUF;
3286#endif
Matthew Qin0b15b322015-05-19 05:20:54 -04003287 dprintf(INFO, "decompressing kernel image: start\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08003288 ret = decompress((unsigned char *)(ptr + page_size),
3289 hdr->kernel_size, out_addr, out_avai_len,
3290 &dtb_offset, &out_len);
3291 if (ret)
3292 {
Matthew Qin0b15b322015-05-19 05:20:54 -04003293 dprintf(CRITICAL, "decompressing image failed!!!\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08003294 ASSERT(0);
3295 }
3296
Matthew Qin0b15b322015-05-19 05:20:54 -04003297 dprintf(INFO, "decompressing kernel image: done\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08003298 kptr = (struct kernel64_hdr *)out_addr;
3299 kernel_start_addr = out_addr;
3300 kernel_size = out_len;
3301 } else {
3302 kptr = (struct kernel64_hdr*)((char *)data + page_size);
3303 kernel_start_addr = (unsigned char *)((char *)data + page_size);
3304 kernel_size = hdr->kernel_size;
3305 }
3306
3307 /*
Channagoud Kadabia22144f2013-03-20 11:49:01 -07003308 * Update the kernel/ramdisk/tags address if the boot image header
3309 * has default values, these default values come from mkbootimg when
3310 * the boot image is flashed using fastboot flash:raw
3311 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08003312 update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr));
Dima Zavin3cadfff2013-03-21 14:30:48 -07003313
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07003314 /* Get virtual addresses since the hdr saves physical addresses. */
3315 hdr->kernel_addr = VA(hdr->kernel_addr);
3316 hdr->ramdisk_addr = VA(hdr->ramdisk_addr);
3317 hdr->tags_addr = VA(hdr->tags_addr);
Brian Swetland9c4c0752009-01-25 16:23:50 -08003318
Matthew Qinbb7923d2015-02-09 10:56:09 +08003319 kernel_size = ROUND_TO_PAGE(kernel_size, page_mask);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07003320 /* Check if the addresses in the header are valid. */
Matthew Qinbb7923d2015-02-09 10:56:09 +08003321 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_size) ||
Mayank Grovere559cec2017-10-17 15:12:03 +05303322 check_ddr_addr_range_bound(hdr->kernel_addr, kernel_size) ||
3323 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual) ||
3324 check_ddr_addr_range_bound(hdr->ramdisk_addr, ramdisk_actual))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07003325 {
Mayank Grovere559cec2017-10-17 15:12:03 +05303326 dprintf(CRITICAL, "kernel/ramdisk addresses are not valid.\n");
lijuang2008ff22016-03-07 17:56:27 +08003327 goto boot_failed;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07003328 }
3329
Amol Jadicb524072012-08-09 16:40:18 -07003330#if DEVICE_TREE
Matthew Qin271927e2015-03-31 22:07:07 -04003331 scratch_offset = image_actual + page_size + out_len;
Amol Jadicb524072012-08-09 16:40:18 -07003332 /* find correct dtb and copy it to right location */
Matthew Qin271927e2015-03-31 22:07:07 -04003333 ret = copy_dtb(data, scratch_offset);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07003334
3335 dtb_copied = !ret ? 1 : 0;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07003336#else
Mayank Grovere559cec2017-10-17 15:12:03 +05303337 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE) ||
3338 check_ddr_addr_range_bound(hdr->tags_addr, MAX_TAGS_SIZE))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07003339 {
Mayank Grovere559cec2017-10-17 15:12:03 +05303340 dprintf(CRITICAL, "Tags addresses are not valid.\n");
lijuang2008ff22016-03-07 17:56:27 +08003341 goto boot_failed;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07003342 }
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07003343#endif
3344
3345 /* Load ramdisk & kernel */
3346 memmove((void*) hdr->ramdisk_addr, ptr + page_size + kernel_actual, hdr->ramdisk_size);
Matthew Qinbb7923d2015-02-09 10:56:09 +08003347 memmove((void*) hdr->kernel_addr, (char*) (kernel_start_addr), kernel_size);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07003348
3349#if DEVICE_TREE
Mayank Grovere559cec2017-10-17 15:12:03 +05303350 if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual) ||
3351 check_ddr_addr_range_bound(hdr->tags_addr, kernel_actual))
Matthew Qinbb7923d2015-02-09 10:56:09 +08003352 {
Mayank Grovere559cec2017-10-17 15:12:03 +05303353 dprintf(CRITICAL, "Tags addresses are not valid.\n");
lijuang2008ff22016-03-07 17:56:27 +08003354 goto boot_failed;
Matthew Qinbb7923d2015-02-09 10:56:09 +08003355 }
3356
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07003357 /*
3358 * If dtb is not found look for appended DTB in the kernel.
3359 * If appended dev tree is found, update the atags with
3360 * memory address to the DTB appended location on RAM.
3361 * Else update with the atags address in the kernel header
3362 */
3363 if (!dtb_copied) {
3364 void *dtb;
Matthew Qinbb7923d2015-02-09 10:56:09 +08003365 dtb = dev_tree_appended((void*)(ptr + page_size),
3366 hdr->kernel_size, dtb_offset,
Dima Zavine63e5572013-05-03 12:23:06 -07003367 (void *)hdr->tags_addr);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07003368 if (!dtb) {
3369 fastboot_fail("dtb not found");
lijuang2008ff22016-03-07 17:56:27 +08003370 goto boot_failed;
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07003371 }
Amol Jadicb524072012-08-09 16:40:18 -07003372 }
3373#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08003374
3375 fastboot_okay("");
Amol Jadi7c4316c2013-10-07 14:19:26 -07003376 fastboot_stop();
Brian Swetland9c4c0752009-01-25 16:23:50 -08003377
Dima Zavin77e41f32013-03-06 16:10:43 -08003378 boot_linux((void*) hdr->kernel_addr, (void*) hdr->tags_addr,
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07003379 (const char*) hdr->cmdline, board_machtype(),
3380 (void*) hdr->ramdisk_addr, hdr->ramdisk_size);
lijuang2008ff22016-03-07 17:56:27 +08003381
3382 /* fastboot already stop, it's no need to show fastboot menu */
3383 return;
3384boot_failed:
3385#if FBCON_DISPLAY_MSG
3386 /* revert to fastboot menu if boot failed */
3387 display_fastboot_menu();
3388#endif
3389 return;
Brian Swetland9c4c0752009-01-25 16:23:50 -08003390}
3391
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003392void cmd_erase_nand(const char *arg, void *data, unsigned sz)
Dima Zavin214cc642009-01-26 11:16:21 -08003393{
3394 struct ptentry *ptn;
3395 struct ptable *ptable;
3396
3397 ptable = flash_get_ptable();
3398 if (ptable == NULL) {
3399 fastboot_fail("partition table doesn't exist");
3400 return;
3401 }
3402
3403 ptn = ptable_find(ptable, arg);
3404 if (ptn == NULL) {
3405 fastboot_fail("unknown partition name");
3406 return;
3407 }
3408
Monika Singh292b3e92018-03-17 22:40:23 +05303409 if (!strncmp(arg, "avb_custom_key", strlen("avb_custom_key"))) {
3410 dprintf(INFO, "erasing avb_custom_key\n");
3411 if (erase_userkey()) {
3412 fastboot_fail("Erasing avb_custom_key failed");
3413 } else {
3414 fastboot_okay("");
3415 }
3416 return;
3417 }
3418
Dima Zavin214cc642009-01-26 11:16:21 -08003419 if (flash_erase(ptn)) {
3420 fastboot_fail("failed to erase partition");
3421 return;
3422 }
3423 fastboot_okay("");
3424}
3425
Bikas Gurungd48bd242010-09-04 19:54:32 -07003426
3427void cmd_erase_mmc(const char *arg, void *data, unsigned sz)
3428{
3429 unsigned long long ptn = 0;
Oliver Wangcee448d2013-10-22 18:40:13 +08003430 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07003431 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003432 uint8_t lun = 0;
Vijay Kumar Pendoti0b21f462016-05-02 17:09:18 +05303433 char *footer = NULL;
Bikas Gurungd48bd242010-09-04 19:54:32 -07003434
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003435#if VERIFIED_BOOT
3436 if(!strcmp(arg, KEYSTORE_PTN_NAME))
3437 {
3438 if(!device.is_unlocked)
3439 {
3440 fastboot_fail("unlock device to erase keystore");
3441 return;
3442 }
3443 }
3444#endif
3445
Kinson Chikf1a43512011-07-14 11:28:39 -07003446 index = partition_get_index(arg);
3447 ptn = partition_get_offset(index);
Oliver Wangcee448d2013-10-22 18:40:13 +08003448 size = partition_get_size(index);
Neeti Desaica8c9602011-10-06 11:40:00 -07003449
Monika Singhc4778b72018-05-16 11:16:42 +05303450 if (!strncmp(arg, "avb_custom_key", strlen("avb_custom_key"))) {
3451 dprintf(INFO, "erasing avb_custom_key\n");
3452 if (erase_userkey()) {
3453 fastboot_fail("Erasing avb_custom_key failed");
3454 } else {
3455 fastboot_okay("");
3456 }
3457 return;
3458 }
3459
Kinson Chikf1a43512011-07-14 11:28:39 -07003460 if(ptn == 0) {
Neeti Desaica8c9602011-10-06 11:40:00 -07003461 fastboot_fail("Partition table doesn't exist\n");
Bikas Gurungd48bd242010-09-04 19:54:32 -07003462 return;
3463 }
Kun Liang2f1601a2013-08-12 16:29:54 +08003464
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003465 lun = partition_get_lun(index);
3466 mmc_set_lun(lun);
3467
Sridhar Parasuramd7957122015-02-27 11:33:40 -08003468 if (platform_boot_dev_isemmc())
3469 {
3470 if (mmc_erase_card(ptn, size)) {
3471 fastboot_fail("failed to erase partition\n");
3472 return;
3473 }
3474 } else {
3475 BUF_DMA_ALIGN(out, DEFAULT_ERASE_SIZE);
3476 size = partition_get_size(index);
3477 if (size > DEFAULT_ERASE_SIZE)
3478 size = DEFAULT_ERASE_SIZE;
Kun Liang2f1601a2013-08-12 16:29:54 +08003479
Sridhar Parasuramd7957122015-02-27 11:33:40 -08003480 /* Simple inefficient version of erase. Just writing
3481 0 in first several blocks */
3482 if (mmc_write(ptn , size, (unsigned int *)out)) {
3483 fastboot_fail("failed to erase partition");
3484 return;
3485 }
Vijay Kumar Pendoti0b21f462016-05-02 17:09:18 +05303486 /*Erase FDE metadata at the userdata footer*/
3487 if(!(strncmp(arg, "userdata", 8)))
3488 {
3489 footer = memalign(CACHE_LINE, FOOTER_SIZE);
3490 memset((void *)footer, 0, FOOTER_SIZE);
3491
3492 size = partition_get_size(index);
3493
3494 if (mmc_write((ptn + size) - FOOTER_SIZE , FOOTER_SIZE, (unsigned int *)footer)) {
3495 fastboot_fail("failed to erase userdata footer");
3496 free(footer);
3497 return;
3498 }
3499 free(footer);
3500 }
Bikas Gurungd48bd242010-09-04 19:54:32 -07003501 }
Monika Singh292b3e92018-03-17 22:40:23 +05303502#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover912eaa62017-10-26 12:08:53 +05303503 if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05303504 !(strncmp(arg, "userdata", 8)) &&
3505 send_delete_keys_to_tz())
Sridhar Parasuram32b30662015-07-10 13:33:22 -07003506 ASSERT(0);
3507#endif
Bikas Gurungd48bd242010-09-04 19:54:32 -07003508 fastboot_okay("");
3509}
3510
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003511void cmd_erase(const char *arg, void *data, unsigned sz)
3512{
Monika Singh292b3e92018-03-17 22:40:23 +05303513#if VERIFIED_BOOT || VERIFIED_BOOT_2
Channagoud Kadabi35297672015-06-13 11:09:49 -07003514 if (target_build_variant_user())
Channagoud Kadabice3cf422015-04-17 16:02:30 -07003515 {
Sridhar Parasuramc32d07d2015-07-12 10:57:48 -07003516 if(!device.is_unlocked)
Channagoud Kadabice3cf422015-04-17 16:02:30 -07003517 {
Channagoud Kadabi35297672015-06-13 11:09:49 -07003518 fastboot_fail("device is locked. Cannot erase");
Channagoud Kadabice3cf422015-04-17 16:02:30 -07003519 return;
3520 }
3521 }
3522#endif
3523
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003524 if(target_is_emmc_boot())
3525 cmd_erase_mmc(arg, data, sz);
3526 else
3527 cmd_erase_nand(arg, data, sz);
3528}
Bikas Gurungd48bd242010-09-04 19:54:32 -07003529
Mayank Grover11ff9692018-01-11 11:54:49 +05303530/* Get the size from partiton name */
3531static void get_partition_size(const char *arg, char *response)
3532{
3533 uint64_t ptn = 0;
3534 uint64_t size;
3535 int index = INVALID_PTN;
3536
3537 index = partition_get_index(arg);
3538
3539 if (index == INVALID_PTN)
3540 {
3541 dprintf(CRITICAL, "Invalid partition index\n");
3542 return;
3543 }
3544
3545 ptn = partition_get_offset(index);
3546
3547 if(!ptn)
3548 {
3549 dprintf(CRITICAL, "Invalid partition name %s\n", arg);
3550 return;
3551 }
3552
3553 size = partition_get_size(index);
3554
3555 snprintf(response, MAX_RSP_SIZE, "\t 0x%llx", size);
3556 return;
3557}
3558
Mayank Grover52cd10a2018-03-15 12:57:54 +05303559/* Function to check partition type of a partition*/
3560static fs_signature_type
3561check_partition_fs_signature(const char *arg)
3562{
3563 fs_signature_type ret = NO_FS;
3564 int index;
3565 unsigned long long ptn;
Mayank Grover399826a2018-08-27 12:15:15 +05303566 char *buffer = memalign(CACHE_LINE, mmc_blocksize);
3567 uint32_t sb_blk_offset = 0;
3568 char *sb_buffer = buffer;
3569
Mayank Grover52cd10a2018-03-15 12:57:54 +05303570 if (!sb_buffer)
3571 {
3572 dprintf(CRITICAL, "ERROR: Failed to allocate buffer for superblock\n");
3573 goto out;
3574 }
3575
3576 /* Read super block */
3577 if ((index = partition_get_index(arg)) < 0)
3578 {
3579 dprintf(CRITICAL, "ERROR: %s() doesn't exsit\n", arg);
3580 goto out;
3581 }
3582 ptn = partition_get_offset(index);
3583 mmc_set_lun(partition_get_lun(index));
Mayank Grover399826a2018-08-27 12:15:15 +05303584 sb_blk_offset = (FS_SUPERBLOCK_OFFSET/mmc_blocksize);
3585
3586 if(mmc_read(ptn + (sb_blk_offset * mmc_blocksize),
Mayank Grover52cd10a2018-03-15 12:57:54 +05303587 (void *)sb_buffer, mmc_blocksize))
3588 {
3589 dprintf(CRITICAL, "ERROR: Failed to read Superblock\n");
3590 goto out;
3591 }
3592
Mayank Grover399826a2018-08-27 12:15:15 +05303593 if (sb_blk_offset == 0)
3594 sb_buffer += FS_SUPERBLOCK_OFFSET;
3595
3596 if (*((uint16 *)(&sb_buffer[EXT_MAGIC_OFFSET_SB])) == (uint16)EXT_MAGIC)
Mayank Grover52cd10a2018-03-15 12:57:54 +05303597 {
3598 dprintf(SPEW, "%s() Found EXT FS\n", arg);
3599 ret = EXT_FS_SIGNATURE;
3600 }
Mayank Grover399826a2018-08-27 12:15:15 +05303601 else if (*((uint32 *)(&sb_buffer[F2FS_MAGIC_OFFSET_SB])) == F2FS_MAGIC)
Mayank Grover52cd10a2018-03-15 12:57:54 +05303602 {
3603 dprintf(SPEW, "%s() Found F2FS FS\n", arg);
3604 ret = EXT_F2FS_SIGNATURE;
3605 }
3606 else
3607 {
3608 dprintf(SPEW, "%s() Reverting to default 0x%x\n",
3609 arg, *((uint16 *)(&sb_buffer[EXT_MAGIC_OFFSET_SB])));
3610 ret = NO_FS;
3611 }
3612
3613out:
Mayank Grover399826a2018-08-27 12:15:15 +05303614 if(buffer)
3615 free(buffer);
Mayank Grover52cd10a2018-03-15 12:57:54 +05303616 return ret;
3617}
3618
Mayank Groverd38fe012018-03-13 15:33:16 +05303619/* Function to get partition type */
3620static void get_partition_type(const char *arg, char *response)
3621{
3622 uint n = 0;
Mayank Grover52cd10a2018-03-15 12:57:54 +05303623 fs_signature_type fs_signature;
Mayank Groverd38fe012018-03-13 15:33:16 +05303624
3625 if (arg == NULL ||
3626 response == NULL)
3627 {
3628 dprintf(CRITICAL, "Invalid input parameter\n");
3629 return;
3630 }
3631
3632 /* By default copy raw to response */
Mayank Grover0e559042018-04-11 17:49:30 +05303633 strlcpy(response, RAW_STR, MAX_RSP_SIZE);
Mayank Groverd38fe012018-03-13 15:33:16 +05303634
3635 /* Mark partiton type for known paritions only */
3636 for (n=0; n < ARRAY_SIZE(part_type_known); n++)
3637 {
3638 if (!strncmp(part_type_known[n].part_name, arg, strlen(arg)))
3639 {
Mayank Grover52cd10a2018-03-15 12:57:54 +05303640 /* Check partition for FS signature */
3641 fs_signature = check_partition_fs_signature(arg);
3642 switch (fs_signature)
3643 {
3644 case EXT_FS_SIGNATURE:
Mayank Grover0e559042018-04-11 17:49:30 +05303645 strlcpy(response, EXT_STR, MAX_RSP_SIZE);
Mayank Grover52cd10a2018-03-15 12:57:54 +05303646 break;
3647 case EXT_F2FS_SIGNATURE:
Mayank Grover0e559042018-04-11 17:49:30 +05303648 strlcpy(response, F2FS_STR, MAX_RSP_SIZE);
Mayank Grover52cd10a2018-03-15 12:57:54 +05303649 break;
3650 case NO_FS:
Mayank Grover0e559042018-04-11 17:49:30 +05303651 strlcpy(response, part_type_known[n].type_response, MAX_RSP_SIZE);
Mayank Grover52cd10a2018-03-15 12:57:54 +05303652 }
Mayank Groverd38fe012018-03-13 15:33:16 +05303653 }
3654 }
3655 return;
3656}
3657
Mayank Grover11ff9692018-01-11 11:54:49 +05303658/*
3659 * Publish the partition type & size info
3660 * fastboot getvar will publish the required information.
3661 * fastboot getvar partition_size:<partition_name>: partition size in hex
3662 * fastboot getvar partition_type:<partition_name>: partition type (ext/fat)
3663 */
3664static void publish_getvar_partition_info(struct getvar_partition_info *info, uint8_t num_parts)
3665{
Mayank Groverd38fe012018-03-13 15:33:16 +05303666 uint8_t i;
Mayank Grover11ff9692018-01-11 11:54:49 +05303667 static bool published = false;
3668 struct partition_entry *ptn_entry =
3669 partition_get_partition_entries();
3670 memset(info, 0, sizeof(struct getvar_partition_info)* num_parts);
3671
3672 for (i = 0; i < num_parts; i++) {
3673 strlcat(info[i].part_name, (const char *)ptn_entry[i].name, MAX_RSP_SIZE);
3674 strlcat(info[i].getvar_size, "partition-size:", MAX_GET_VAR_NAME_SIZE);
3675 strlcat(info[i].getvar_type, "partition-type:", MAX_GET_VAR_NAME_SIZE);
3676
Mayank Groverd38fe012018-03-13 15:33:16 +05303677 get_partition_type(info[i].part_name, info[i].type_response);
Mayank Grover11ff9692018-01-11 11:54:49 +05303678 get_partition_size(info[i].part_name, info[i].size_response);
Mayank Grover11ff9692018-01-11 11:54:49 +05303679 if (strlcat(info[i].getvar_size, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
3680 {
3681 dprintf(CRITICAL, "partition size name truncated\n");
3682 return;
3683 }
3684 if (strlcat(info[i].getvar_type, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
3685 {
3686 dprintf(CRITICAL, "partition type name truncated\n");
3687 return;
3688 }
3689
3690 if (!published)
3691 {
3692 /* publish partition size & type info */
3693 fastboot_publish((const char *) info[i].getvar_size, (const char *) info[i].size_response);
3694 fastboot_publish((const char *) info[i].getvar_type, (const char *) info[i].type_response);
3695 }
3696 }
3697 if (!published)
3698 published = true;
3699}
3700
3701
Ajay Dudani5c761132011-04-07 20:19:04 -07003702void cmd_flash_mmc_img(const char *arg, void *data, unsigned sz)
Shashank Mittal23b8f422010-04-16 19:27:21 -07003703{
3704 unsigned long long ptn = 0;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07003705 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07003706 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003707 char *token = NULL;
3708 char *pname = NULL;
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07003709 char *sp;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003710 uint8_t lun = 0;
3711 bool lun_set = false;
Mayank Grover351a75e2017-05-30 20:06:08 +05303712 int current_active_slot = INVALID;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07003713
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07003714 token = strtok_r((char *)arg, ":", &sp);
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003715 pname = token;
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07003716 token = strtok_r(NULL, ":", &sp);
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003717 if(token)
3718 {
3719 lun = atoi(token);
3720 mmc_set_lun(lun);
3721 lun_set = true;
3722 }
3723
Mao Jinlong226f33a2014-07-04 17:24:10 +08003724 if (pname)
Greg Grisco6e754772011-06-23 12:19:39 -07003725 {
Channagoud Kadabiad259832015-05-29 11:14:17 -07003726 if (!strncmp(pname, "frp-unlock", strlen("frp-unlock")))
3727 {
3728 if (!aboot_frp_unlock(pname, data, sz))
3729 {
3730 fastboot_info("FRP unlock successful");
3731 fastboot_okay("");
3732 }
3733 else
3734 fastboot_fail("Secret key is invalid, please update the bootloader with secret key");
3735
3736 return;
3737 }
3738
Mao Jinlong226f33a2014-07-04 17:24:10 +08003739 if (!strcmp(pname, "partition"))
3740 {
3741 dprintf(INFO, "Attempt to write partition image.\n");
3742 if (write_partition(sz, (unsigned char *) data)) {
3743 fastboot_fail("failed to write partition");
Greg Grisco6e754772011-06-23 12:19:39 -07003744 return;
3745 }
Mayank Grover11ff9692018-01-11 11:54:49 +05303746 /* Re-publish partition table */
3747 publish_getvar_partition_info(part_info, partition_get_partition_count());
Mayank Grover351a75e2017-05-30 20:06:08 +05303748
3749 /* Rescan partition table to ensure we have multislot support*/
3750 if (partition_scan_for_multislot())
3751 {
3752 current_active_slot = partition_find_active_slot();
3753 dprintf(INFO, "Multislot supported: Slot %s active",
3754 (SUFFIX_SLOT(current_active_slot)));
3755 }
3756 partition_mark_active_slot(current_active_slot);
Greg Grisco6e754772011-06-23 12:19:39 -07003757 }
Mao Jinlong226f33a2014-07-04 17:24:10 +08003758 else
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003759 {
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003760#if VERIFIED_BOOT
3761 if(!strcmp(pname, KEYSTORE_PTN_NAME))
3762 {
3763 if(!device.is_unlocked)
3764 {
3765 fastboot_fail("unlock device to flash keystore");
3766 return;
3767 }
Gaurav Nebhwanic7313cc2015-12-15 22:25:04 +05303768 if(!boot_verify_validate_keystore((unsigned char *)data,sz))
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003769 {
3770 fastboot_fail("image is not a keystore file");
3771 return;
3772 }
3773 }
3774#endif
Mao Jinlong226f33a2014-07-04 17:24:10 +08003775 index = partition_get_index(pname);
3776 ptn = partition_get_offset(index);
3777 if(ptn == 0) {
3778 fastboot_fail("partition table doesn't exist");
3779 return;
3780 }
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003781
Mayank Grover351a75e2017-05-30 20:06:08 +05303782 if (!strncmp(pname, "boot", strlen("boot"))
3783 || !strcmp(pname, "recovery"))
3784 {
Mao Jinlong226f33a2014-07-04 17:24:10 +08003785 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
3786 fastboot_fail("image is not a boot image");
3787 return;
3788 }
Mayank Grover351a75e2017-05-30 20:06:08 +05303789
3790 /* Reset multislot_partition attributes in case of flashing boot */
3791 if (partition_multislot_is_supported())
3792 {
3793 partition_reset_attributes(index);
3794 }
Mao Jinlong226f33a2014-07-04 17:24:10 +08003795 }
3796
3797 if(!lun_set)
3798 {
3799 lun = partition_get_lun(index);
3800 mmc_set_lun(lun);
3801 }
3802
3803 size = partition_get_size(index);
Gaurav Nebhwani4d2389c2016-03-17 21:10:05 +05303804 if (ROUND_TO_PAGE(sz, mmc_blocksize_mask) > size) {
Mao Jinlong226f33a2014-07-04 17:24:10 +08003805 fastboot_fail("size too large");
3806 return;
3807 }
3808 else if (mmc_write(ptn , sz, (unsigned int *)data)) {
3809 fastboot_fail("flash write failure");
3810 return;
3811 }
Greg Grisco6e754772011-06-23 12:19:39 -07003812 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07003813 }
3814 fastboot_okay("");
3815 return;
3816}
3817
Ajay Dudanide984792015-03-02 09:57:41 -08003818void cmd_flash_meta_img(const char *arg, void *data, unsigned sz)
3819{
3820 int i, images;
3821 meta_header_t *meta_header;
3822 img_header_entry_t *img_header_entry;
Parth Dixit3e2d3032016-03-04 17:11:52 +05303823 /*End of the image address*/
3824 uintptr_t data_end;
3825
3826 if( (UINT_MAX - sz) > (uintptr_t)data )
3827 data_end = (uintptr_t)data + sz;
3828 else
3829 {
3830 fastboot_fail("Cannot flash: image header corrupt");
3831 return;
3832 }
3833
3834 if( data_end < ((uintptr_t)data + sizeof(meta_header_t)))
3835 {
3836 fastboot_fail("Cannot flash: image header corrupt");
3837 return;
3838 }
Ajay Dudanide984792015-03-02 09:57:41 -08003839
lijuang8ee21882016-04-19 16:57:11 +08003840 /* If device is locked:
3841 * Forbid to flash image to avoid the device to bypass the image
3842 * which with "any" name other than bootloader. Because it maybe
3843 * a meta package of all partitions.
3844 */
Monika Singh292b3e92018-03-17 22:40:23 +05303845#if VERIFIED_BOOT || VERIFIED_BOOT_2
lijuang8ee21882016-04-19 16:57:11 +08003846 if (target_build_variant_user()) {
3847 if (!device.is_unlocked) {
3848 fastboot_fail("Device is locked, meta image flashing is not allowed");
3849 return;
3850 }
Mayank Grover889be1b2017-09-12 20:12:23 +05303851
Mayank Grover912eaa62017-10-26 12:08:53 +05303852 if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05303853 !device.is_unlock_critical)
3854 {
lijuang8ee21882016-04-19 16:57:11 +08003855 fastboot_fail("Device is critical locked, Meta image flashing is not allowed");
3856 return;
3857 }
lijuang8ee21882016-04-19 16:57:11 +08003858 }
3859#endif
3860
Ajay Dudanide984792015-03-02 09:57:41 -08003861 meta_header = (meta_header_t*) data;
Parth Dixit3e2d3032016-03-04 17:11:52 +05303862 if( data_end < ((uintptr_t)data + meta_header->img_hdr_sz))
3863 {
3864 fastboot_fail("Cannot flash: image header corrupt");
3865 return;
3866 }
Ajay Dudanide984792015-03-02 09:57:41 -08003867 img_header_entry = (img_header_entry_t*) (data+sizeof(meta_header_t));
3868
3869 images = meta_header->img_hdr_sz / sizeof(img_header_entry_t);
3870
3871 for (i=0; i<images; i++) {
3872
3873 if((img_header_entry[i].ptn_name == NULL) ||
3874 (img_header_entry[i].start_offset == 0) ||
3875 (img_header_entry[i].size == 0))
3876 break;
Kishor PK49831502017-04-21 17:55:43 +05303877 if ((UINT_MAX - img_header_entry[i].start_offset) < (uintptr_t)data) {
3878 fastboot_fail("Integer overflow detected in start_offset of img");
3879 break;
3880 }
3881 else if ((UINT_MAX - (img_header_entry[i].start_offset + (uintptr_t)data)) < img_header_entry[i].size) {
3882 fastboot_fail("Integer overflow detected in size of img");
3883 break;
3884 }
Parth Dixit3e2d3032016-03-04 17:11:52 +05303885 if( data_end < ((uintptr_t)data + img_header_entry[i].start_offset
3886 + img_header_entry[i].size) )
3887 {
3888 fastboot_fail("Cannot flash: image size mismatch");
3889 break;
3890 }
3891
Ajay Dudanide984792015-03-02 09:57:41 -08003892 cmd_flash_mmc_img(img_header_entry[i].ptn_name,
3893 (void *) data + img_header_entry[i].start_offset,
3894 img_header_entry[i].size);
3895 }
3896
Channagoud Kadabiaafbbfc2015-03-04 17:52:56 -08003897 if (!strncmp(arg, "bootloader", strlen("bootloader")))
3898 {
3899 strlcpy(device.bootloader_version, TARGET(BOARD), MAX_VERSION_LEN);
3900 strlcat(device.bootloader_version, "-", MAX_VERSION_LEN);
3901 strlcat(device.bootloader_version, meta_header->img_version, MAX_VERSION_LEN);
3902 }
3903 else
3904 {
3905 strlcpy(device.radio_version, TARGET(BOARD), MAX_VERSION_LEN);
3906 strlcat(device.radio_version, "-", MAX_VERSION_LEN);
3907 strlcat(device.radio_version, meta_header->img_version, MAX_VERSION_LEN);
3908 }
3909
3910 write_device_info(&device);
Ajay Dudanide984792015-03-02 09:57:41 -08003911 fastboot_okay("");
3912 return;
3913}
3914
Ajay Dudani5c761132011-04-07 20:19:04 -07003915void cmd_flash_mmc_sparse_img(const char *arg, void *data, unsigned sz)
3916{
3917 unsigned int chunk;
wufeng.jiang813dc352015-06-02 23:02:46 -04003918 uint64_t chunk_data_sz;
Channagoud Kadabif627cf82013-09-09 14:08:20 -07003919 uint32_t *fill_buf = NULL;
3920 uint32_t fill_val;
Mayank Grover4f50bba2017-07-19 18:17:08 +05303921 uint32_t blk_sz_actual = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07003922 sparse_header_t *sparse_header;
3923 chunk_header_t *chunk_header;
Ajay Dudaniab18f022011-05-12 14:39:22 -07003924 uint32_t total_blocks = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07003925 unsigned long long ptn = 0;
Channagoud Kadabi65b91002011-10-11 17:34:33 +05303926 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07003927 int index = INVALID_PTN;
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05303928 uint32_t i;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003929 uint8_t lun = 0;
vijay kumar800255e2015-04-24 20:26:19 +05303930 /*End of the sparse image address*/
Parth Dixit64b1a482016-03-07 16:31:26 +05303931 uintptr_t data_end = (uintptr_t)data + sz;
Ajay Dudani5c761132011-04-07 20:19:04 -07003932
Kinson Chikf1a43512011-07-14 11:28:39 -07003933 index = partition_get_index(arg);
3934 ptn = partition_get_offset(index);
3935 if(ptn == 0) {
Ajay Dudani5c761132011-04-07 20:19:04 -07003936 fastboot_fail("partition table doesn't exist");
3937 return;
3938 }
3939
Channagoud Kadabi65b91002011-10-11 17:34:33 +05303940 size = partition_get_size(index);
Channagoud Kadabi65b91002011-10-11 17:34:33 +05303941
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003942 lun = partition_get_lun(index);
3943 mmc_set_lun(lun);
3944
vijay kumar800255e2015-04-24 20:26:19 +05303945 if (sz < sizeof(sparse_header_t)) {
3946 fastboot_fail("size too low");
3947 return;
3948 }
3949
Ajay Dudani5c761132011-04-07 20:19:04 -07003950 /* Read and skip over sparse image header */
3951 sparse_header = (sparse_header_t *) data;
vijay kumar800255e2015-04-24 20:26:19 +05303952
Mayank Grover48bd9bb2017-07-19 12:04:16 +05303953 if (!sparse_header->blk_sz || (sparse_header->blk_sz % 4)){
3954 fastboot_fail("Invalid block size\n");
3955 return;
3956 }
3957
vijay kumar1321f342015-03-27 12:13:42 +05303958 if (((uint64_t)sparse_header->total_blks * (uint64_t)sparse_header->blk_sz) > size) {
Ajay Dudani876b3282012-12-21 14:12:17 -08003959 fastboot_fail("size too large");
3960 return;
3961 }
3962
vijay kumarde4fcf62015-04-23 13:05:49 +05303963 data += sizeof(sparse_header_t);
vijay kumar800255e2015-04-24 20:26:19 +05303964
Parth Dixit64b1a482016-03-07 16:31:26 +05303965 if (data_end < (uintptr_t)data) {
vijay kumar800255e2015-04-24 20:26:19 +05303966 fastboot_fail("buffer overreads occured due to invalid sparse header");
3967 return;
3968 }
3969
vijay kumarde4fcf62015-04-23 13:05:49 +05303970 if(sparse_header->file_hdr_sz != sizeof(sparse_header_t))
Ajay Dudani5c761132011-04-07 20:19:04 -07003971 {
vijay kumarde4fcf62015-04-23 13:05:49 +05303972 fastboot_fail("sparse header size mismatch");
3973 return;
Ajay Dudani5c761132011-04-07 20:19:04 -07003974 }
3975
Ajay Dudanib06c05f2011-05-12 14:46:10 -07003976 dprintf (SPEW, "=== Sparse Image Header ===\n");
3977 dprintf (SPEW, "magic: 0x%x\n", sparse_header->magic);
3978 dprintf (SPEW, "major_version: 0x%x\n", sparse_header->major_version);
3979 dprintf (SPEW, "minor_version: 0x%x\n", sparse_header->minor_version);
3980 dprintf (SPEW, "file_hdr_sz: %d\n", sparse_header->file_hdr_sz);
3981 dprintf (SPEW, "chunk_hdr_sz: %d\n", sparse_header->chunk_hdr_sz);
3982 dprintf (SPEW, "blk_sz: %d\n", sparse_header->blk_sz);
3983 dprintf (SPEW, "total_blks: %d\n", sparse_header->total_blks);
3984 dprintf (SPEW, "total_chunks: %d\n", sparse_header->total_chunks);
Ajay Dudani5c761132011-04-07 20:19:04 -07003985
3986 /* Start processing chunks */
3987 for (chunk=0; chunk<sparse_header->total_chunks; chunk++)
3988 {
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05303989 /* Make sure the total image size does not exceed the partition size */
3990 if(((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz) >= size) {
3991 fastboot_fail("size too large");
3992 return;
3993 }
Ajay Dudani5c761132011-04-07 20:19:04 -07003994 /* Read and skip over chunk header */
3995 chunk_header = (chunk_header_t *) data;
3996 data += sizeof(chunk_header_t);
3997
Parth Dixit64b1a482016-03-07 16:31:26 +05303998 if (data_end < (uintptr_t)data) {
vijay kumar800255e2015-04-24 20:26:19 +05303999 fastboot_fail("buffer overreads occured due to invalid sparse header");
4000 return;
4001 }
4002
Ajay Dudani5c761132011-04-07 20:19:04 -07004003 dprintf (SPEW, "=== Chunk Header ===\n");
4004 dprintf (SPEW, "chunk_type: 0x%x\n", chunk_header->chunk_type);
4005 dprintf (SPEW, "chunk_data_sz: 0x%x\n", chunk_header->chunk_sz);
4006 dprintf (SPEW, "total_size: 0x%x\n", chunk_header->total_sz);
4007
vijay kumar800255e2015-04-24 20:26:19 +05304008 if(sparse_header->chunk_hdr_sz != sizeof(chunk_header_t))
Ajay Dudani5c761132011-04-07 20:19:04 -07004009 {
vijay kumar800255e2015-04-24 20:26:19 +05304010 fastboot_fail("chunk header size mismatch");
4011 return;
Ajay Dudani5c761132011-04-07 20:19:04 -07004012 }
4013
wufeng.jiang813dc352015-06-02 23:02:46 -04004014 chunk_data_sz = (uint64_t)sparse_header->blk_sz * chunk_header->chunk_sz;
4015
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05304016 /* Make sure that the chunk size calculated from sparse image does not
4017 * exceed partition size
4018 */
4019 if ((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz + chunk_data_sz > size)
4020 {
4021 fastboot_fail("Chunk data size exceeds partition size");
4022 return;
4023 }
4024
Ajay Dudani5c761132011-04-07 20:19:04 -07004025 switch (chunk_header->chunk_type)
4026 {
4027 case CHUNK_TYPE_RAW:
wufeng.jiang813dc352015-06-02 23:02:46 -04004028 if((uint64_t)chunk_header->total_sz != ((uint64_t)sparse_header->chunk_hdr_sz +
Ajay Dudani5c761132011-04-07 20:19:04 -07004029 chunk_data_sz))
4030 {
4031 fastboot_fail("Bogus chunk size for chunk type Raw");
4032 return;
4033 }
4034
Parth Dixit64b1a482016-03-07 16:31:26 +05304035 if (data_end < (uintptr_t)data + chunk_data_sz) {
vijay kumar800255e2015-04-24 20:26:19 +05304036 fastboot_fail("buffer overreads occured due to invalid sparse header");
4037 return;
4038 }
4039
wufeng.jiang813dc352015-06-02 23:02:46 -04004040 /* chunk_header->total_sz is uint32,So chunk_data_sz is now less than 2^32
4041 otherwise it will return in the line above
4042 */
Ajay Dudaniab18f022011-05-12 14:39:22 -07004043 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
wufeng.jiang813dc352015-06-02 23:02:46 -04004044 (uint32_t)chunk_data_sz,
Ajay Dudaniab18f022011-05-12 14:39:22 -07004045 (unsigned int*)data))
Ajay Dudani5c761132011-04-07 20:19:04 -07004046 {
4047 fastboot_fail("flash write failure");
4048 return;
4049 }
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05304050 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
4051 fastboot_fail("Bogus size for RAW chunk type");
4052 return;
4053 }
Ajay Dudani5c761132011-04-07 20:19:04 -07004054 total_blocks += chunk_header->chunk_sz;
wufeng.jiang813dc352015-06-02 23:02:46 -04004055 data += (uint32_t)chunk_data_sz;
Ajay Dudani5c761132011-04-07 20:19:04 -07004056 break;
4057
Channagoud Kadabif627cf82013-09-09 14:08:20 -07004058 case CHUNK_TYPE_FILL:
4059 if(chunk_header->total_sz != (sparse_header->chunk_hdr_sz +
4060 sizeof(uint32_t)))
4061 {
4062 fastboot_fail("Bogus chunk size for chunk type FILL");
4063 return;
4064 }
4065
Mayank Grover4f50bba2017-07-19 18:17:08 +05304066 blk_sz_actual = ROUNDUP(sparse_header->blk_sz, CACHE_LINE);
4067 /* Integer overflow detected */
4068 if (blk_sz_actual < sparse_header->blk_sz)
4069 {
4070 fastboot_fail("Invalid block size");
4071 return;
4072 }
4073
4074 fill_buf = (uint32_t *)memalign(CACHE_LINE, blk_sz_actual);
Channagoud Kadabif627cf82013-09-09 14:08:20 -07004075 if (!fill_buf)
4076 {
4077 fastboot_fail("Malloc failed for: CHUNK_TYPE_FILL");
4078 return;
4079 }
4080
Parth Dixit64b1a482016-03-07 16:31:26 +05304081 if (data_end < (uintptr_t)data + sizeof(uint32_t)) {
vijay kumar800255e2015-04-24 20:26:19 +05304082 fastboot_fail("buffer overreads occured due to invalid sparse header");
Gaurav Nebhwani163cbf82016-03-17 21:21:27 +05304083 free(fill_buf);
vijay kumar800255e2015-04-24 20:26:19 +05304084 return;
4085 }
Channagoud Kadabif627cf82013-09-09 14:08:20 -07004086 fill_val = *(uint32_t *)data;
4087 data = (char *) data + sizeof(uint32_t);
Channagoud Kadabif627cf82013-09-09 14:08:20 -07004088
4089 for (i = 0; i < (sparse_header->blk_sz / sizeof(fill_val)); i++)
4090 {
4091 fill_buf[i] = fill_val;
4092 }
4093
Gaurav Nebhwanie94cbe12016-03-17 21:16:34 +05304094 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz))
4095 {
4096 fastboot_fail("bogus size for chunk FILL type");
4097 free(fill_buf);
4098 return;
4099 }
4100
wufeng.jiang813dc352015-06-02 23:02:46 -04004101 for (i = 0; i < chunk_header->chunk_sz; i++)
Channagoud Kadabif627cf82013-09-09 14:08:20 -07004102 {
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05304103 /* Make sure that the data written to partition does not exceed partition size */
4104 if ((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz + sparse_header->blk_sz > size)
4105 {
4106 fastboot_fail("Chunk data size for fill type exceeds partition size");
Gaurav Nebhwani163cbf82016-03-17 21:21:27 +05304107 free(fill_buf);
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05304108 return;
4109 }
4110
Channagoud Kadabif627cf82013-09-09 14:08:20 -07004111 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
4112 sparse_header->blk_sz,
4113 fill_buf))
4114 {
4115 fastboot_fail("flash write failure");
4116 free(fill_buf);
4117 return;
4118 }
4119
4120 total_blocks++;
4121 }
4122
4123 free(fill_buf);
4124 break;
4125
Ajay Dudani5c761132011-04-07 20:19:04 -07004126 case CHUNK_TYPE_DONT_CARE:
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05304127 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
4128 fastboot_fail("bogus size for chunk DONT CARE type");
4129 return;
4130 }
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07004131 total_blocks += chunk_header->chunk_sz;
4132 break;
4133
Ajay Dudani5c761132011-04-07 20:19:04 -07004134 case CHUNK_TYPE_CRC:
4135 if(chunk_header->total_sz != sparse_header->chunk_hdr_sz)
4136 {
wufeng.jiang813dc352015-06-02 23:02:46 -04004137 fastboot_fail("Bogus chunk size for chunk type CRC");
Ajay Dudani5c761132011-04-07 20:19:04 -07004138 return;
4139 }
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05304140 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
4141 fastboot_fail("bogus size for chunk CRC type");
4142 return;
4143 }
Ajay Dudani5c761132011-04-07 20:19:04 -07004144 total_blocks += chunk_header->chunk_sz;
Parth Dixit64b1a482016-03-07 16:31:26 +05304145 if ((uintptr_t)data > UINT_MAX - chunk_data_sz) {
vijay kumar800255e2015-04-24 20:26:19 +05304146 fastboot_fail("integer overflow occured");
4147 return;
4148 }
wufeng.jiang813dc352015-06-02 23:02:46 -04004149 data += (uint32_t)chunk_data_sz;
Parth Dixit64b1a482016-03-07 16:31:26 +05304150 if (data_end < (uintptr_t)data) {
vijay kumar800255e2015-04-24 20:26:19 +05304151 fastboot_fail("buffer overreads occured due to invalid sparse header");
4152 return;
4153 }
Ajay Dudani5c761132011-04-07 20:19:04 -07004154 break;
4155
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07004156 default:
Channagoud Kadabif627cf82013-09-09 14:08:20 -07004157 dprintf(CRITICAL, "Unkown chunk type: %x\n",chunk_header->chunk_type);
Ajay Dudani5c761132011-04-07 20:19:04 -07004158 fastboot_fail("Unknown chunk type");
4159 return;
4160 }
4161 }
4162
Ajay Dudani0c6927b2011-05-18 11:12:16 -07004163 dprintf(INFO, "Wrote %d blocks, expected to write %d blocks\n",
4164 total_blocks, sparse_header->total_blks);
4165
4166 if(total_blocks != sparse_header->total_blks)
4167 {
4168 fastboot_fail("sparse image write failure");
4169 }
Ajay Dudani5c761132011-04-07 20:19:04 -07004170
4171 fastboot_okay("");
4172 return;
4173}
4174
4175void cmd_flash_mmc(const char *arg, void *data, unsigned sz)
4176{
4177 sparse_header_t *sparse_header;
Ajay Dudanide984792015-03-02 09:57:41 -08004178 meta_header_t *meta_header;
Ajay Dudani5c761132011-04-07 20:19:04 -07004179
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07004180#ifdef SSD_ENABLE
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -08004181 /* 8 Byte Magic + 2048 Byte xml + Encrypted Data */
4182 unsigned int *magic_number = (unsigned int *) data;
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08004183 int ret=0;
4184 uint32 major_version=0;
4185 uint32 minor_version=0;
4186
4187 ret = scm_svc_version(&major_version,&minor_version);
4188 if(!ret)
4189 {
4190 if(major_version >= 2)
4191 {
Amir Samuelovbb65ce02013-05-05 12:20:18 +03004192 if( !strcmp(arg, "ssd") || !strcmp(arg, "tqs") )
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08004193 {
4194 ret = encrypt_scm((uint32 **) &data, &sz);
4195 if (ret != 0) {
4196 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
4197 return;
4198 }
4199
Amir Samuelovbb65ce02013-05-05 12:20:18 +03004200 /* Protect only for SSD */
4201 if (!strcmp(arg, "ssd")) {
4202 ret = scm_protect_keystore((uint32 *) data, sz);
4203 if (ret != 0) {
4204 dprintf(CRITICAL, "ERROR: scm_protect_keystore Failed\n");
4205 return;
4206 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08004207 }
4208 }
4209 else
4210 {
4211 ret = decrypt_scm_v2((uint32 **) &data, &sz);
4212 if(ret != 0)
4213 {
4214 dprintf(CRITICAL,"ERROR: Decryption Failure\n");
4215 return;
4216 }
4217 }
4218 }
4219 else
4220 {
4221 if (magic_number[0] == DECRYPT_MAGIC_0 &&
4222 magic_number[1] == DECRYPT_MAGIC_1)
4223 {
4224 ret = decrypt_scm((uint32 **) &data, &sz);
4225 if (ret != 0) {
4226 dprintf(CRITICAL, "ERROR: Invalid secure image\n");
4227 return;
4228 }
4229 }
4230 else if (magic_number[0] == ENCRYPT_MAGIC_0 &&
4231 magic_number[1] == ENCRYPT_MAGIC_1)
4232 {
4233 ret = encrypt_scm((uint32 **) &data, &sz);
4234 if (ret != 0) {
4235 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
4236 return;
4237 }
4238 }
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07004239 }
4240 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08004241 else
Neeti Desai127b9e02012-03-20 16:11:23 -07004242 {
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08004243 dprintf(CRITICAL,"INVALID SVC Version\n");
4244 return;
Neeti Desai127b9e02012-03-20 16:11:23 -07004245 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08004246#endif /* SSD_ENABLE */
Neeti Desai127b9e02012-03-20 16:11:23 -07004247
Monika Singh292b3e92018-03-17 22:40:23 +05304248#if VERIFIED_BOOT || VERIFIED_BOOT_2
Channagoud Kadabi35297672015-06-13 11:09:49 -07004249 if (target_build_variant_user())
Shashank Mittald3e54dd2014-08-28 15:24:02 -07004250 {
lijuang511a2b52015-08-14 20:50:51 +08004251 /* if device is locked:
4252 * common partition will not allow to be flashed
4253 * critical partition will allow to flash image.
4254 */
4255 if(!device.is_unlocked && !critical_flash_allowed(arg)) {
4256 fastboot_fail("Partition flashing is not allowed");
4257 return;
4258 }
Mayank Grover889be1b2017-09-12 20:12:23 +05304259
lijuang511a2b52015-08-14 20:50:51 +08004260 /* if device critical is locked:
4261 * common partition will allow to be flashed
4262 * critical partition will not allow to flash image.
4263 */
Mayank Grover912eaa62017-10-26 12:08:53 +05304264 if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05304265 !device.is_unlock_critical &&
4266 critical_flash_allowed(arg)) {
4267 fastboot_fail("Critical partition flashing is not allowed");
4268 return;
Shashank Mittald3e54dd2014-08-28 15:24:02 -07004269 }
4270 }
4271#endif
Monika Singh292b3e92018-03-17 22:40:23 +05304272 if (!strncmp(arg, "avb_custom_key", strlen("avb_custom_key"))) {
4273 dprintf(INFO, "flashing avb_custom_key\n");
4274 if (store_userkey(data, sz)) {
4275 fastboot_fail("Flashing avb_custom_key failed");
4276 } else {
4277 fastboot_okay("");
4278 }
4279 return;
4280 }
Shashank Mittald3e54dd2014-08-28 15:24:02 -07004281
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07004282 sparse_header = (sparse_header_t *) data;
Ajay Dudanide984792015-03-02 09:57:41 -08004283 meta_header = (meta_header_t *) data;
4284 if (sparse_header->magic == SPARSE_HEADER_MAGIC)
Ajay Dudani5c761132011-04-07 20:19:04 -07004285 cmd_flash_mmc_sparse_img(arg, data, sz);
Ajay Dudanide984792015-03-02 09:57:41 -08004286 else if (meta_header->magic == META_HEADER_MAGIC)
4287 cmd_flash_meta_img(arg, data, sz);
4288 else
4289 cmd_flash_mmc_img(arg, data, sz);
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07004290
4291#if VERIFIED_BOOT
Mayank Grover912eaa62017-10-26 12:08:53 +05304292 if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05304293 (!strncmp(arg, "system", 6)) &&
4294 !device.verity_mode)
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07004295 // reset dm_verity mode to enforcing
4296 device.verity_mode = 1;
4297 write_device_info(&device);
Parth Dixitddbc7352015-10-18 03:13:31 +05304298#endif
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07004299
Ajay Dudani5c761132011-04-07 20:19:04 -07004300 return;
4301}
4302
Tanya Brokhman84eaaf62015-02-24 21:57:06 +02004303void cmd_updatevol(const char *vol_name, void *data, unsigned sz)
4304{
4305 struct ptentry *sys_ptn;
4306 struct ptable *ptable;
4307
4308 ptable = flash_get_ptable();
4309 if (ptable == NULL) {
4310 fastboot_fail("partition table doesn't exist");
4311 return;
4312 }
4313
4314 sys_ptn = ptable_find(ptable, "system");
4315 if (sys_ptn == NULL) {
4316 fastboot_fail("system partition not found");
4317 return;
4318 }
4319
4320 sz = ROUND_TO_PAGE(sz, page_mask);
4321 if (update_ubi_vol(sys_ptn, vol_name, data, sz))
4322 fastboot_fail("update_ubi_vol failed");
4323 else
4324 fastboot_okay("");
4325}
4326
Sridhar Parasurame94e8152014-10-24 14:06:03 -07004327void cmd_flash_nand(const char *arg, void *data, unsigned sz)
Dima Zavin214cc642009-01-26 11:16:21 -08004328{
4329 struct ptentry *ptn;
4330 struct ptable *ptable;
4331 unsigned extra = 0;
Gaurav Nebhwanid9730712016-05-06 14:28:29 +05304332 uint64_t partition_size = 0;
Vijay Kumar Pendotic6f22a72017-05-19 22:44:34 +05304333 unsigned bytes_to_round_page = 0;
4334 unsigned rounded_size = 0;
Dima Zavin214cc642009-01-26 11:16:21 -08004335
Kishor PK38ed93d2017-04-25 14:19:26 +05304336 if((uintptr_t)data > (UINT_MAX - sz)) {
4337 fastboot_fail("Cannot flash: image header corrupt");
4338 return;
4339 }
4340
Dima Zavin214cc642009-01-26 11:16:21 -08004341 ptable = flash_get_ptable();
4342 if (ptable == NULL) {
4343 fastboot_fail("partition table doesn't exist");
4344 return;
4345 }
4346
4347 ptn = ptable_find(ptable, arg);
4348 if (ptn == NULL) {
Tanya Brokhman84eaaf62015-02-24 21:57:06 +02004349 dprintf(INFO, "unknown partition name (%s). Trying updatevol\n",
4350 arg);
4351 cmd_updatevol(arg, data, sz);
Dima Zavin214cc642009-01-26 11:16:21 -08004352 return;
4353 }
4354
Monika Singh292b3e92018-03-17 22:40:23 +05304355 if (!strncmp(arg, "avb_custom_key", strlen("avb_custom_key"))) {
4356 dprintf(INFO, "flashing avb_custom_key\n");
4357 if (store_userkey(data, sz)) {
4358 fastboot_fail("Flashing avb_custom_key failed");
4359 } else {
4360 fastboot_okay("");
4361 }
4362 return;
4363 }
4364
Dima Zavin214cc642009-01-26 11:16:21 -08004365 if (!strcmp(ptn->name, "boot") || !strcmp(ptn->name, "recovery")) {
Kishor PK38ed93d2017-04-25 14:19:26 +05304366 if((sz > BOOT_MAGIC_SIZE) && (!memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE))) {
4367 dprintf(INFO, "Verified the BOOT_MAGIC in image header \n");
4368 } else {
4369 fastboot_fail("Image is not a boot image");
Dima Zavin214cc642009-01-26 11:16:21 -08004370 return;
4371 }
4372 }
4373
Amol Jadi5c61a952012-05-04 17:05:35 -07004374 if (!strcmp(ptn->name, "system")
Deepa Dinamani13e32c42012-03-12 14:34:17 -07004375 || !strcmp(ptn->name, "userdata")
4376 || !strcmp(ptn->name, "persist")
Sundarajan Srinivasanb063a852013-11-19 14:02:27 -08004377 || !strcmp(ptn->name, "recoveryfs")
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -08004378 || !strcmp(ptn->name, "modem"))
Tanya Brokhman1c94f1a2015-02-15 09:05:03 +02004379 extra = 1;
Kishor PK38ed93d2017-04-25 14:19:26 +05304380 else {
Vijay Kumar Pendotic6f22a72017-05-19 22:44:34 +05304381 rounded_size = ROUNDUP(sz, page_size);
4382 bytes_to_round_page = rounded_size - sz;
4383 if (bytes_to_round_page) {
4384 if (((uintptr_t)data + sz ) > (UINT_MAX - bytes_to_round_page)) {
4385 fastboot_fail("Integer overflow detected");
4386 return;
4387 }
4388 if (((uintptr_t)data + sz + bytes_to_round_page) >
4389 ((uintptr_t)target_get_scratch_address() + target_get_max_flash_size())) {
4390 fastboot_fail("Buffer size is not aligned to page_size");
4391 return;
4392 }
4393 else {
4394 memset(data + sz, 0, bytes_to_round_page);
4395 sz = rounded_size;
4396 }
Kishor PK38ed93d2017-04-25 14:19:26 +05304397 }
Gaurav Nebhwanid9730712016-05-06 14:28:29 +05304398 }
4399
Kishor PK38ed93d2017-04-25 14:19:26 +05304400 /*Checking partition_size for the possible integer overflow */
4401 partition_size = validate_partition_size(ptn);
4402
Gaurav Nebhwanid9730712016-05-06 14:28:29 +05304403 if (sz > partition_size) {
4404 fastboot_fail("Image size too large");
4405 return;
4406 }
4407
Dima Zavin214cc642009-01-26 11:16:21 -08004408 dprintf(INFO, "writing %d bytes to '%s'\n", sz, ptn->name);
Mayank Grover6cde9352017-06-06 18:45:23 +05304409 if ((sz > UBI_EC_HDR_SIZE) &&
4410 (!memcmp((void *)data, UBI_MAGIC, UBI_MAGIC_SIZE))) {
Tanya Brokhman1c94f1a2015-02-15 09:05:03 +02004411 if (flash_ubi_img(ptn, data, sz)) {
4412 fastboot_fail("flash write failure");
4413 return;
4414 }
4415 } else {
4416 if (flash_write(ptn, extra, data, sz)) {
4417 fastboot_fail("flash write failure");
4418 return;
4419 }
Dima Zavin214cc642009-01-26 11:16:21 -08004420 }
4421 dprintf(INFO, "partition '%s' updated\n", ptn->name);
4422 fastboot_okay("");
4423}
4424
Kishor PK38ed93d2017-04-25 14:19:26 +05304425
4426static inline uint64_t validate_partition_size(struct ptentry *ptn)
4427{
4428 if (ptn->length && flash_num_pages_per_blk() && page_size) {
4429 if ((ptn->length < ( UINT_MAX / flash_num_pages_per_blk())) && ((ptn->length * flash_num_pages_per_blk()) < ( UINT_MAX / page_size))) {
4430 return ptn->length * flash_num_pages_per_blk() * page_size;
4431 }
4432 }
4433 return 0;
4434}
4435
4436
Sridhar Parasurame94e8152014-10-24 14:06:03 -07004437void cmd_flash(const char *arg, void *data, unsigned sz)
4438{
4439 if(target_is_emmc_boot())
4440 cmd_flash_mmc(arg, data, sz);
4441 else
4442 cmd_flash_nand(arg, data, sz);
4443}
4444
Dima Zavin214cc642009-01-26 11:16:21 -08004445void cmd_continue(const char *arg, void *data, unsigned sz)
4446{
4447 fastboot_okay("");
Amol Jadi7c4316c2013-10-07 14:19:26 -07004448 fastboot_stop();
Shashank Mittald3e54dd2014-08-28 15:24:02 -07004449
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004450 if (target_is_emmc_boot())
4451 {
lijuanga40d6302015-07-20 20:10:13 +08004452#if FBCON_DISPLAY_MSG
lijuangde34d502016-02-26 16:04:50 +08004453 /* Exit keys' detection thread firstly */
4454 exit_menu_keys_detection();
lijuanga40d6302015-07-20 20:10:13 +08004455#endif
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004456 boot_linux_from_mmc();
4457 }
4458 else
4459 {
4460 boot_linux_from_flash();
4461 }
Dima Zavin214cc642009-01-26 11:16:21 -08004462}
4463
Chandan Uddaraju94183c02010-01-15 15:13:59 -08004464void cmd_reboot(const char *arg, void *data, unsigned sz)
4465{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004466 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08004467 fastboot_okay("");
4468 reboot_device(0);
4469}
4470
Mayank Grover351a75e2017-05-30 20:06:08 +05304471void cmd_set_active(const char *arg, void *data, unsigned sz)
4472{
Mayank Grover6ccc1c92017-07-04 17:36:46 +05304473 char *p, *sp = NULL;
Mayank Grover351a75e2017-05-30 20:06:08 +05304474 unsigned i,current_active_slot;
4475 const char *current_slot_suffix;
4476
4477 if (!partition_multislot_is_supported())
4478 {
4479 fastboot_fail("Command not supported");
4480 return;
4481 }
4482
4483 if (arg)
4484 {
Mayank Grover6ccc1c92017-07-04 17:36:46 +05304485 p = strtok_r((char *)arg, ":", &sp);
Mayank Grover5eb5f692017-07-19 20:16:04 +05304486 if (p)
Mayank Grover351a75e2017-05-30 20:06:08 +05304487 {
4488 current_active_slot = partition_find_active_slot();
4489
4490 /* Check if trying to make curent slot active */
4491 current_slot_suffix = SUFFIX_SLOT(current_active_slot);
Mayank Grover6ccc1c92017-07-04 17:36:46 +05304492 current_slot_suffix = strtok_r((char *)current_slot_suffix,
4493 (char *)suffix_delimiter, &sp);
4494
Mayank Grover5eb5f692017-07-19 20:16:04 +05304495 if (current_slot_suffix &&
4496 !strncmp(p, current_slot_suffix, strlen(current_slot_suffix)))
Mayank Grover351a75e2017-05-30 20:06:08 +05304497 {
4498 fastboot_okay("Slot already set active");
4499 return;
4500 }
4501 else
4502 {
4503 for (i = 0; i < AB_SUPPORTED_SLOTS; i++)
4504 {
4505 current_slot_suffix = SUFFIX_SLOT(i);
Mayank Grover6ccc1c92017-07-04 17:36:46 +05304506 current_slot_suffix = strtok_r((char *)current_slot_suffix,
4507 (char *)suffix_delimiter, &sp);
Mayank Grover5eb5f692017-07-19 20:16:04 +05304508 if (current_slot_suffix &&
4509 !strncmp(p, current_slot_suffix, strlen(current_slot_suffix)))
Mayank Grover351a75e2017-05-30 20:06:08 +05304510 {
lijuang8b03e5f2019-07-12 18:16:19 +08004511 partition_switch_slots(current_active_slot, i, true);
Mayank Grover351a75e2017-05-30 20:06:08 +05304512 publish_getvar_multislot_vars();
4513 fastboot_okay("");
4514 return;
4515 }
4516 }
4517 }
4518 }
4519 }
4520 fastboot_fail("Invalid slot suffix.");
4521 return;
4522}
4523
Mayank Grover98c4c742019-04-25 17:21:37 +05304524#if DYNAMIC_PARTITION_SUPPORT
4525void cmd_reboot_fastboot(const char *arg, void *data, unsigned sz)
4526{
4527 dprintf(INFO, "rebooting the device - userspace fastboot\n");
4528 if (send_recovery_cmd(RECOVERY_BOOT_FASTBOOT_CMD)) {
4529 dprintf(CRITICAL, "ERROR: Failed to update recovery commands\n");
4530 fastboot_fail("Failed to update recovery command");
4531 return;
4532 }
4533 fastboot_okay("");
4534 reboot_device(REBOOT_MODE_UNKNOWN);
4535
4536 //shouldn't come here.
4537 dprintf(CRITICAL, "ERROR: Failed to reboot device\n");
4538 return;
4539}
4540
4541void cmd_reboot_recovery(const char *arg, void *data, unsigned sz)
4542{
4543 dprintf(INFO, "rebooting the device - recovery\n");
4544 if (send_recovery_cmd(RECOVERY_BOOT_RECOVERY_CMD)) {
4545 dprintf(CRITICAL, "ERROR: Failed to update recovery commands\n");
4546 fastboot_fail("Failed to update recovery command");
4547 return;
4548 }
4549 fastboot_okay("");
4550 reboot_device(REBOOT_MODE_UNKNOWN);
4551
4552 //shouldn't come here.
4553 dprintf(CRITICAL, "ERROR: Failed to reboot device\n");
4554 return;
4555}
4556#endif
4557
Chandan Uddaraju94183c02010-01-15 15:13:59 -08004558void cmd_reboot_bootloader(const char *arg, void *data, unsigned sz)
4559{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004560 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08004561 fastboot_okay("");
4562 reboot_device(FASTBOOT_MODE);
4563}
4564
Ameya Thakur11cf1a62013-08-05 12:44:48 -07004565void cmd_oem_enable_charger_screen(const char *arg, void *data, unsigned size)
4566{
4567 dprintf(INFO, "Enabling charger screen check\n");
4568 device.charger_screen_enabled = 1;
4569 write_device_info(&device);
4570 fastboot_okay("");
4571}
4572
4573void cmd_oem_disable_charger_screen(const char *arg, void *data, unsigned size)
4574{
4575 dprintf(INFO, "Disabling charger screen check\n");
4576 device.charger_screen_enabled = 0;
4577 write_device_info(&device);
4578 fastboot_okay("");
4579}
4580
lijuanga25d8bb2015-09-16 13:11:52 +08004581void cmd_oem_off_mode_charger(const char *arg, void *data, unsigned size)
4582{
4583 char *p = NULL;
4584 const char *delim = " \t\n\r";
Parth Dixit407f15b2016-01-07 14:47:37 +05304585 char *sp;
lijuanga25d8bb2015-09-16 13:11:52 +08004586
4587 if (arg) {
Parth Dixit407f15b2016-01-07 14:47:37 +05304588 p = strtok_r((char *)arg, delim, &sp);
lijuanga25d8bb2015-09-16 13:11:52 +08004589 if (p) {
4590 if (!strncmp(p, "0", 1)) {
4591 device.charger_screen_enabled = 0;
4592 } else if (!strncmp(p, "1", 1)) {
4593 device.charger_screen_enabled = 1;
4594 }
4595 }
4596 }
4597
4598 /* update charger_screen_enabled value for getvar
4599 * command
4600 */
4601 snprintf(charger_screen_enabled, MAX_RSP_SIZE, "%d",
4602 device.charger_screen_enabled);
4603
4604 write_device_info(&device);
4605 fastboot_okay("");
4606}
4607
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05304608void cmd_oem_select_display_panel(const char *arg, void *data, unsigned size)
4609{
4610 dprintf(INFO, "Selecting display panel %s\n", arg);
4611 if (arg)
4612 strlcpy(device.display_panel, arg,
4613 sizeof(device.display_panel));
4614 write_device_info(&device);
4615 fastboot_okay("");
4616}
4617
Shashank Mittal162244e2011-08-08 19:01:25 -07004618void cmd_oem_unlock(const char *arg, void *data, unsigned sz)
4619{
lijuang4ece1e72015-08-14 21:02:36 +08004620 set_device_unlock(UNLOCK, TRUE);
vijay kumarc65876c2015-04-24 13:29:16 +05304621}
4622
4623void cmd_oem_unlock_go(const char *arg, void *data, unsigned sz)
4624{
lijuang4ece1e72015-08-14 21:02:36 +08004625 if(!device.is_unlocked) {
vijay kumarc65876c2015-04-24 13:29:16 +05304626 if(!is_allow_unlock) {
4627 fastboot_fail("oem unlock is not allowed");
4628 return;
4629 }
4630
lijuang4ece1e72015-08-14 21:02:36 +08004631 set_device_unlock_value(UNLOCK, TRUE);
vijay kumarc65876c2015-04-24 13:29:16 +05304632
lijuang4ece1e72015-08-14 21:02:36 +08004633 /* wipe data */
vijay kumarc65876c2015-04-24 13:29:16 +05304634 struct recovery_message msg;
Kishor PK60a68212017-05-08 16:55:57 +05304635 memset(&msg, 0, sizeof(msg));
vijay kumarc65876c2015-04-24 13:29:16 +05304636 snprintf(msg.recovery, sizeof(msg.recovery), "recovery\n--wipe_data");
4637 write_misc(0, &msg, sizeof(msg));
4638
4639 fastboot_okay("");
4640 reboot_device(RECOVERY_MODE);
Shashank Mittald3e54dd2014-08-28 15:24:02 -07004641 }
4642 fastboot_okay("");
4643}
4644
Channagoud Kadabiad259832015-05-29 11:14:17 -07004645static int aboot_frp_unlock(char *pname, void *data, unsigned sz)
4646{
Mayank Grover70f8b0e2017-05-17 11:49:00 +05304647 int ret=1;
4648 bool authentication_success=false;
Channagoud Kadabiad259832015-05-29 11:14:17 -07004649
Mayank Grover70f8b0e2017-05-17 11:49:00 +05304650 /*
4651 Authentication method not implemented.
4652
4653 OEM to implement, authentication system which on successful validataion,
4654 calls write_allow_oem_unlock() with is_allow_unlock.
4655 */
4656#if 0
4657 authentication_success = oem_specific_auth_mthd();
4658#endif
4659
4660 if (authentication_success)
Channagoud Kadabiad259832015-05-29 11:14:17 -07004661 {
Mayank Grover70f8b0e2017-05-17 11:49:00 +05304662 is_allow_unlock = true;
4663 write_allow_oem_unlock(is_allow_unlock);
4664 ret = 0;
Channagoud Kadabiad259832015-05-29 11:14:17 -07004665 }
4666 return ret;
4667}
4668
Shashank Mittald3e54dd2014-08-28 15:24:02 -07004669void cmd_oem_lock(const char *arg, void *data, unsigned sz)
4670{
lijuang4ece1e72015-08-14 21:02:36 +08004671 set_device_unlock(UNLOCK, FALSE);
Shashank Mittal162244e2011-08-08 19:01:25 -07004672}
4673
Shashank Mittala0032282011-08-26 14:50:11 -07004674void cmd_oem_devinfo(const char *arg, void *data, unsigned sz)
4675{
lijuang511a2b52015-08-14 20:50:51 +08004676 char response[MAX_RSP_SIZE];
Ameya Thakur11cf1a62013-08-05 12:44:48 -07004677 snprintf(response, sizeof(response), "\tDevice tampered: %s", (device.is_tampered ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07004678 fastboot_info(response);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07004679 snprintf(response, sizeof(response), "\tDevice unlocked: %s", (device.is_unlocked ? "true" : "false"));
4680 fastboot_info(response);
Monika Singh292b3e92018-03-17 22:40:23 +05304681#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover912eaa62017-10-26 12:08:53 +05304682 if (VB_M <= target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +05304683 {
4684 snprintf(response, sizeof(response), "\tDevice critical unlocked: %s",
4685 (device.is_unlock_critical ? "true" : "false"));
4686 fastboot_info(response);
4687 }
Parth Dixitddbc7352015-10-18 03:13:31 +05304688#endif
Ameya Thakur11cf1a62013-08-05 12:44:48 -07004689 snprintf(response, sizeof(response), "\tCharger screen enabled: %s", (device.charger_screen_enabled ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07004690 fastboot_info(response);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05304691 snprintf(response, sizeof(response), "\tDisplay panel: %s", (device.display_panel));
4692 fastboot_info(response);
Shashank Mittala0032282011-08-26 14:50:11 -07004693 fastboot_okay("");
4694}
4695
lijuang511a2b52015-08-14 20:50:51 +08004696void cmd_flashing_get_unlock_ability(const char *arg, void *data, unsigned sz)
4697{
4698 char response[MAX_RSP_SIZE];
4699 snprintf(response, sizeof(response), "\tget_unlock_ability: %d", is_allow_unlock);
4700 fastboot_info(response);
4701 fastboot_okay("");
4702}
4703
4704void cmd_flashing_lock_critical(const char *arg, void *data, unsigned sz)
4705{
lijuang4ece1e72015-08-14 21:02:36 +08004706 set_device_unlock(UNLOCK_CRITICAL, FALSE);
lijuang511a2b52015-08-14 20:50:51 +08004707}
4708
4709void cmd_flashing_unlock_critical(const char *arg, void *data, unsigned sz)
4710{
lijuang4ece1e72015-08-14 21:02:36 +08004711 set_device_unlock(UNLOCK_CRITICAL, TRUE);
lijuang511a2b52015-08-14 20:50:51 +08004712}
4713
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07004714void cmd_preflash(const char *arg, void *data, unsigned sz)
4715{
4716 fastboot_okay("");
4717}
4718
Mao Flynn7b379f32015-04-20 00:28:30 +08004719static uint8_t logo_header[LOGO_IMG_HEADER_SIZE];
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304720
Mao Flynn7b379f32015-04-20 00:28:30 +08004721int splash_screen_check_header(logo_img_header *header)
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304722{
Mao Flynn7b379f32015-04-20 00:28:30 +08004723 if (memcmp(header->magic, LOGO_IMG_MAGIC, 8))
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304724 return -1;
Mao Flynn7b379f32015-04-20 00:28:30 +08004725 if (header->width == 0 || header->height == 0)
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304726 return -1;
4727 return 0;
4728}
4729
Mao Flynn7b379f32015-04-20 00:28:30 +08004730int splash_screen_flash()
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07004731{
4732 struct ptentry *ptn;
4733 struct ptable *ptable;
Mao Flynn7b379f32015-04-20 00:28:30 +08004734 struct logo_img_header *header;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07004735 struct fbcon_config *fb_display = NULL;
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08004736
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304737 ptable = flash_get_ptable();
4738 if (ptable == NULL) {
Mao Flynn7b379f32015-04-20 00:28:30 +08004739 dprintf(CRITICAL, "ERROR: Partition table not found\n");
4740 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304741 }
Mao Flynn7b379f32015-04-20 00:28:30 +08004742
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304743 ptn = ptable_find(ptable, "splash");
4744 if (ptn == NULL) {
4745 dprintf(CRITICAL, "ERROR: splash Partition not found\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08004746 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304747 }
Mao Flynn7b379f32015-04-20 00:28:30 +08004748 if (flash_read(ptn, 0, (void *)logo_header, LOGO_IMG_HEADER_SIZE)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304749 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08004750 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304751 }
4752
Mao Flynn7b379f32015-04-20 00:28:30 +08004753 header = (struct logo_img_header *)logo_header;
4754 if (splash_screen_check_header(header)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304755 dprintf(CRITICAL, "ERROR: Boot image header invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08004756 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304757 }
4758
4759 fb_display = fbcon_display();
4760 if (fb_display) {
raghavendra ambadas554f4fd2018-10-09 15:50:40 +05304761 if (header->type && (header->blocks != 0) &&
4762 (UINT_MAX >= header->blocks * 512) &&
4763 ((header->blocks * 512) <= (fb_display->width *
4764 fb_display->height * (fb_display->bpp / 8)))) {
4765 /* RLE24 compressed data */
Mao Flynn7b379f32015-04-20 00:28:30 +08004766 uint8_t *base = (uint8_t *) fb_display->base + LOGO_IMG_OFFSET;
4767
4768 /* if the logo is full-screen size, remove "fbcon_clear()" */
4769 if ((header->width != fb_display->width)
4770 || (header->height != fb_display->height))
4771 fbcon_clear();
4772
4773 if (flash_read(ptn + LOGO_IMG_HEADER_SIZE, 0,
4774 (uint32_t *)base,
4775 (header->blocks * 512))) {
4776 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
4777 return -1;
4778 }
4779 fbcon_extract_to_screen(header, base);
4780 return 0;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07004781 }
Mao Flynn7b379f32015-04-20 00:28:30 +08004782
Veera Sundaram Sankaranc33416f2015-09-28 10:25:55 -07004783 if ((header->width > fb_display->width) || (header->height > fb_display->height)) {
4784 dprintf(CRITICAL, "Logo config greater than fb config. Fall back default logo\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08004785 return -1;
4786 }
4787
Vineet Bajaj99291ed2014-09-09 12:29:46 +05304788 uint8_t *base = (uint8_t *) fb_display->base;
Sachin Bhayare619e9e42017-05-15 13:10:31 +05304789 uint32_t fb_size = ROUNDUP(fb_display->width *
4790 fb_display->height *
4791 (fb_display->bpp / 8), 4096);
4792 uint32_t splash_size = ((((header->width * header->height *
4793 fb_display->bpp/8) + 511) >> 9) << 9);
4794
4795 if (splash_size > fb_size) {
4796 dprintf(CRITICAL, "ERROR: Splash image size invalid\n");
4797 return -1;
4798 }
4799
Mao Flynn7b379f32015-04-20 00:28:30 +08004800 if (flash_read(ptn + LOGO_IMG_HEADER_SIZE, 0,
4801 (uint32_t *)base,
4802 ((((header->width * header->height * fb_display->bpp/8) + 511) >> 9) << 9))) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304803 fbcon_clear();
Vineet Bajaj99291ed2014-09-09 12:29:46 +05304804 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08004805 return -1;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07004806 }
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304807 }
4808
Mao Flynn7b379f32015-04-20 00:28:30 +08004809 return 0;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304810}
4811
Mao Flynn7b379f32015-04-20 00:28:30 +08004812int splash_screen_mmc()
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304813{
4814 int index = INVALID_PTN;
4815 unsigned long long ptn = 0;
4816 struct fbcon_config *fb_display = NULL;
Mao Flynn7b379f32015-04-20 00:28:30 +08004817 struct logo_img_header *header;
Mao Flynn1893a7f2015-06-03 12:03:36 +08004818 uint32_t blocksize, realsize, readsize;
4819 uint8_t *base;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304820
4821 index = partition_get_index("splash");
4822 if (index == 0) {
4823 dprintf(CRITICAL, "ERROR: splash Partition table not found\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08004824 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304825 }
4826
4827 ptn = partition_get_offset(index);
4828 if (ptn == 0) {
4829 dprintf(CRITICAL, "ERROR: splash Partition invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08004830 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304831 }
4832
Mao Flynn1893a7f2015-06-03 12:03:36 +08004833 mmc_set_lun(partition_get_lun(index));
4834
4835 blocksize = mmc_get_device_blocksize();
4836 if (blocksize == 0) {
4837 dprintf(CRITICAL, "ERROR:splash Partition invalid blocksize\n");
4838 return -1;
4839 }
4840
4841 fb_display = fbcon_display();
Channagoud Kadabidaf10a62015-07-22 11:51:55 -07004842 if (!fb_display)
4843 {
4844 dprintf(CRITICAL, "ERROR: fb config is not allocated\n");
4845 return -1;
4846 }
4847
Mao Flynn1893a7f2015-06-03 12:03:36 +08004848 base = (uint8_t *) fb_display->base;
4849
Kishor PKee5c0a32018-03-06 16:49:46 +05304850 if (mmc_read(ptn + PLL_CODES_OFFSET, (uint32_t *)(base + LOGO_IMG_OFFSET), blocksize)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304851 dprintf(CRITICAL, "ERROR: Cannot read splash image header\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08004852 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304853 }
4854
Mao Flynn1893a7f2015-06-03 12:03:36 +08004855 header = (struct logo_img_header *)(base + LOGO_IMG_OFFSET);
Mao Flynn7b379f32015-04-20 00:28:30 +08004856 if (splash_screen_check_header(header)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304857 dprintf(CRITICAL, "ERROR: Splash image header invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08004858 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304859 }
4860
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304861 if (fb_display) {
raghavendra ambadas554f4fd2018-10-09 15:50:40 +05304862 if (header->type && (header->blocks != 0) &&
4863 (UINT_MAX >= header->blocks * 512 + LOGO_IMG_HEADER_SIZE) &&
4864 ((header->blocks * 512) <= (fb_display->width *
4865 fb_display->height * (fb_display->bpp / 8)))) {
4866 /* 1 RLE24 compressed data */
Mao Flynn1893a7f2015-06-03 12:03:36 +08004867 base += LOGO_IMG_OFFSET;
Mao Flynn7b379f32015-04-20 00:28:30 +08004868
Mao Flynn1893a7f2015-06-03 12:03:36 +08004869 realsize = header->blocks * 512;
4870 readsize = ROUNDUP((realsize + LOGO_IMG_HEADER_SIZE), blocksize) - blocksize;
4871
4872 /* if the logo is not full-screen size, clean screen */
Mao Flynn7b379f32015-04-20 00:28:30 +08004873 if ((header->width != fb_display->width)
4874 || (header->height != fb_display->height))
4875 fbcon_clear();
4876
Sachin Bhayare619e9e42017-05-15 13:10:31 +05304877 uint32_t fb_size = ROUNDUP(fb_display->width *
4878 fb_display->height *
4879 (fb_display->bpp / 8), 4096);
4880
4881 if (readsize > fb_size) {
4882 dprintf(CRITICAL, "ERROR: Splash image size invalid\n");
4883 return -1;
4884 }
4885
Kishor PKee5c0a32018-03-06 16:49:46 +05304886 if (mmc_read(ptn + PLL_CODES_OFFSET + blocksize, (uint32_t *)(base + blocksize), readsize)) {
Mao Flynn7b379f32015-04-20 00:28:30 +08004887 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
4888 return -1;
4889 }
Mao Flynn7b379f32015-04-20 00:28:30 +08004890
Mao Flynn1893a7f2015-06-03 12:03:36 +08004891 fbcon_extract_to_screen(header, (base + LOGO_IMG_HEADER_SIZE));
4892 } else { /* 2 Raw BGR data */
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304893
Veera Sundaram Sankaranc33416f2015-09-28 10:25:55 -07004894 if ((header->width > fb_display->width) || (header->height > fb_display->height)) {
4895 dprintf(CRITICAL, "Logo config greater than fb config. Fall back default logo\n");
Mao Flynn1893a7f2015-06-03 12:03:36 +08004896 return -1;
4897 }
4898
4899 realsize = header->width * header->height * fb_display->bpp / 8;
4900 readsize = ROUNDUP((realsize + LOGO_IMG_HEADER_SIZE), blocksize) - blocksize;
4901
4902 if (blocksize == LOGO_IMG_HEADER_SIZE) { /* read the content directly */
Kishor PKee5c0a32018-03-06 16:49:46 +05304903 if (mmc_read((ptn + PLL_CODES_OFFSET + LOGO_IMG_HEADER_SIZE), (uint32_t *)base, readsize)) {
Mao Flynn1893a7f2015-06-03 12:03:36 +08004904 fbcon_clear();
4905 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
4906 return -1;
4907 }
4908 } else {
Kishor PKee5c0a32018-03-06 16:49:46 +05304909 if (mmc_read(ptn + PLL_CODES_OFFSET + blocksize ,
Mao Flynn1893a7f2015-06-03 12:03:36 +08004910 (uint32_t *)(base + LOGO_IMG_OFFSET + blocksize), readsize)) {
4911 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
4912 return -1;
4913 }
4914 memmove(base, (base + LOGO_IMG_OFFSET + LOGO_IMG_HEADER_SIZE), realsize);
4915 }
4916 }
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304917 }
4918
Mao Flynn7b379f32015-04-20 00:28:30 +08004919 return 0;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304920}
4921
Mao Flynn7b379f32015-04-20 00:28:30 +08004922int fetch_image_from_partition()
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304923{
4924 if (target_is_emmc_boot()) {
4925 return splash_screen_mmc();
4926 } else {
4927 return splash_screen_flash();
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07004928 }
4929}
4930
Mayank Grover351a75e2017-05-30 20:06:08 +05304931void publish_getvar_multislot_vars()
4932{
4933 int i,count;
4934 static bool published = false;
4935 static char slot_count[MAX_RSP_SIZE];
4936 static struct ab_slot_info slot_info[AB_SUPPORTED_SLOTS];
4937 static char active_slot_suffix[MAX_RSP_SIZE];
4938 static char has_slot_pname[NUM_PARTITIONS][MAX_GET_VAR_NAME_SIZE];
4939 static char has_slot_reply[NUM_PARTITIONS][MAX_RSP_SIZE];
4940 const char *tmp;
4941 char tmpbuff[MAX_GET_VAR_NAME_SIZE];
Mayank Grover2bd84bb2017-06-20 11:06:07 +05304942 signed active_slt;
Mayank Grover351a75e2017-05-30 20:06:08 +05304943
4944 if (!published)
4945 {
4946 /* Update slot meta info */
4947 count = partition_fill_partition_meta(has_slot_pname, has_slot_reply,
4948 partition_get_partition_count());
4949 for(i=0; i<count; i++)
4950 {
4951 memset(tmpbuff, 0, MAX_GET_VAR_NAME_SIZE);
Mayank Grover2bd84bb2017-06-20 11:06:07 +05304952 snprintf(tmpbuff, MAX_GET_VAR_NAME_SIZE,"has-slot:%s",
4953 has_slot_pname[i]);
4954 strlcpy(has_slot_pname[i], tmpbuff, MAX_GET_VAR_NAME_SIZE);
Mayank Grover351a75e2017-05-30 20:06:08 +05304955 fastboot_publish(has_slot_pname[i], has_slot_reply[i]);
4956 }
4957
4958 for (i=0; i<AB_SUPPORTED_SLOTS; i++)
4959 {
4960 tmp = SUFFIX_SLOT(i);
Mayank Grover7b8a8f82017-10-27 13:07:59 +05304961 tmp++; // to remove "_" from slot_suffix.
Mayank Grover2bd84bb2017-06-20 11:06:07 +05304962 snprintf(slot_info[i].slot_is_unbootable, sizeof(slot_info[i].slot_is_unbootable),
4963 "slot-unbootable:%s", tmp);
4964 snprintf(slot_info[i].slot_is_active, sizeof(slot_info[i].slot_is_active),
4965 "slot-active:%s", tmp);
4966 snprintf(slot_info[i].slot_is_succesful, sizeof(slot_info[i].slot_is_succesful),
4967 "slot-success:%s", tmp);
4968 snprintf(slot_info[i].slot_retry_count, sizeof(slot_info[i].slot_retry_count),
4969 "slot-retry-count:%s", tmp);
Mayank Grover351a75e2017-05-30 20:06:08 +05304970 fastboot_publish(slot_info[i].slot_is_unbootable,
4971 slot_info[i].slot_is_unbootable_rsp);
4972 fastboot_publish(slot_info[i].slot_is_active,
4973 slot_info[i].slot_is_active_rsp);
4974 fastboot_publish(slot_info[i].slot_is_succesful,
4975 slot_info[i].slot_is_succesful_rsp);
4976 fastboot_publish(slot_info[i].slot_retry_count,
4977 slot_info[i].slot_retry_count_rsp);
4978 }
4979 fastboot_publish("current-slot", active_slot_suffix);
4980 snprintf(slot_count, sizeof(slot_count),"%d", AB_SUPPORTED_SLOTS);
4981 fastboot_publish("slot-count", slot_count);
4982 published = true;
4983 }
4984
Mayank Grover2bd84bb2017-06-20 11:06:07 +05304985 active_slt = partition_find_active_slot();
4986 if (active_slt != INVALID)
Mayank Grover7b8a8f82017-10-27 13:07:59 +05304987 {
4988 tmp = SUFFIX_SLOT(active_slt);
4989 tmp++; // to remove "_" from slot_suffix.
4990 snprintf(active_slot_suffix, sizeof(active_slot_suffix), "%s", tmp);
4991 }
Mayank Grover2bd84bb2017-06-20 11:06:07 +05304992 else
4993 strlcpy(active_slot_suffix, "INVALID", sizeof(active_slot_suffix));
4994
Mayank Grover351a75e2017-05-30 20:06:08 +05304995 /* Update partition meta information */
4996 partition_fill_slot_meta(slot_info);
4997 return;
4998}
4999
lijuang4ece1e72015-08-14 21:02:36 +08005000void get_product_name(unsigned char *buf)
5001{
5002 snprintf((char*)buf, MAX_RSP_SIZE, "%s", TARGET(BOARD));
5003 return;
5004}
5005
Mayank Grover7b8a8f82017-10-27 13:07:59 +05305006#if PRODUCT_IOT
5007void get_bootloader_version_iot(unsigned char *buf)
5008{
5009 if (buf != NULL)
5010 {
5011 strlcpy(buf, TARGET(BOARD), MAX_VERSION_LEN);
5012 strlcat(buf, "-", MAX_VERSION_LEN);
5013 strlcat(buf, PRODUCT_IOT_VERSION, MAX_VERSION_LEN);
5014 }
5015 return;
5016}
5017#endif
5018
lijuang4ece1e72015-08-14 21:02:36 +08005019void get_bootloader_version(unsigned char *buf)
5020{
jhchen7a504d12020-04-24 15:45:57 +08005021 snprintf((char*)buf, MAX_RSP_SIZE, "%s", device.bootloader_version);
lijuang4ece1e72015-08-14 21:02:36 +08005022 return;
5023}
5024
5025void get_baseband_version(unsigned char *buf)
5026{
jhchen7a504d12020-04-24 15:45:57 +08005027 snprintf((char*)buf, MAX_RSP_SIZE, "%s", device.radio_version);
lijuang4ece1e72015-08-14 21:02:36 +08005028 return;
5029}
5030
Monika Singh32a09f72018-03-14 13:08:29 +05305031bool is_device_locked_critical()
5032{
5033 return device.is_unlock_critical ? false:true;
5034}
5035
lijuang4ece1e72015-08-14 21:02:36 +08005036bool is_device_locked()
5037{
5038 return device.is_unlocked ? false:true;
5039}
5040
Monika Singh32a09f72018-03-14 13:08:29 +05305041bool is_verity_enforcing()
5042{
5043 return device.verity_mode ? true:false;
5044}
5045
Amol Jadi5edf3552013-07-23 14:15:34 -07005046/* register commands and variables for fastboot */
5047void aboot_fastboot_register_commands(void)
5048{
Sridhar Parasurame94e8152014-10-24 14:06:03 -07005049 int i;
lijuangf16461c2015-08-03 17:09:34 +08005050 char hw_platform_buf[MAX_RSP_SIZE];
Amol Jadi5edf3552013-07-23 14:15:34 -07005051
Sridhar Parasurame94e8152014-10-24 14:06:03 -07005052 struct fastboot_cmd_desc cmd_list[] = {
lijuang511a2b52015-08-14 20:50:51 +08005053 /* By default the enabled list is empty. */
5054 {"", NULL},
5055 /* move commands enclosed within the below ifndef to here
5056 * if they need to be enabled in user build.
5057 */
Sridhar Parasurame94e8152014-10-24 14:06:03 -07005058#ifndef DISABLE_FASTBOOT_CMDS
lijuang511a2b52015-08-14 20:50:51 +08005059 /* Register the following commands only for non-user builds */
5060 {"flash:", cmd_flash},
5061 {"erase:", cmd_erase},
5062 {"boot", cmd_boot},
5063 {"continue", cmd_continue},
5064 {"reboot", cmd_reboot},
5065 {"reboot-bootloader", cmd_reboot_bootloader},
5066 {"oem unlock", cmd_oem_unlock},
5067 {"oem unlock-go", cmd_oem_unlock_go},
5068 {"oem lock", cmd_oem_lock},
5069 {"flashing unlock", cmd_oem_unlock},
5070 {"flashing lock", cmd_oem_lock},
5071 {"flashing lock_critical", cmd_flashing_lock_critical},
5072 {"flashing unlock_critical", cmd_flashing_unlock_critical},
5073 {"flashing get_unlock_ability", cmd_flashing_get_unlock_ability},
5074 {"oem device-info", cmd_oem_devinfo},
5075 {"preflash", cmd_preflash},
5076 {"oem enable-charger-screen", cmd_oem_enable_charger_screen},
5077 {"oem disable-charger-screen", cmd_oem_disable_charger_screen},
lijuanga25d8bb2015-09-16 13:11:52 +08005078 {"oem off-mode-charge", cmd_oem_off_mode_charger},
lijuang511a2b52015-08-14 20:50:51 +08005079 {"oem select-display-panel", cmd_oem_select_display_panel},
Mayank Grover6ccc1c92017-07-04 17:36:46 +05305080 {"set_active",cmd_set_active},
Mayank Grover98c4c742019-04-25 17:21:37 +05305081#if DYNAMIC_PARTITION_SUPPORT
5082 {"reboot-fastboot",cmd_reboot_fastboot},
5083 {"reboot-recovery",cmd_reboot_recovery},
5084#endif
Channagoud Kadabi736c4962015-08-21 11:56:52 -07005085#if UNITTEST_FW_SUPPORT
Channagoud Kadabid5ddf482015-09-28 10:31:35 -07005086 {"oem run-tests", cmd_oem_runtests},
Channagoud Kadabi736c4962015-08-21 11:56:52 -07005087#endif
Sridhar Parasurame94e8152014-10-24 14:06:03 -07005088#endif
lijuang511a2b52015-08-14 20:50:51 +08005089 };
Sridhar Parasurame94e8152014-10-24 14:06:03 -07005090
5091 int fastboot_cmds_count = sizeof(cmd_list)/sizeof(cmd_list[0]);
5092 for (i = 1; i < fastboot_cmds_count; i++)
5093 fastboot_register(cmd_list[i].name,cmd_list[i].cb);
5094
Amol Jadi5edf3552013-07-23 14:15:34 -07005095 /* publish variables and their values */
5096 fastboot_publish("product", TARGET(BOARD));
5097 fastboot_publish("kernel", "lk");
5098 fastboot_publish("serialno", sn_buf);
5099
Mayank Grovera64dfbe2018-05-17 14:06:34 +05305100 /*publish hw-revision major(upper 16 bits) and minor(lower 16 bits)*/
5101 snprintf(soc_version_str, MAX_RSP_SIZE, "%x", board_soc_version());
5102 fastboot_publish("hw-revision", soc_version_str);
5103
Amol Jadi5edf3552013-07-23 14:15:34 -07005104 /*
5105 * partition info is supported only for emmc partitions
5106 * Calling this for NAND prints some error messages which
5107 * is harmless but misleading. Avoid calling this for NAND
5108 * devices.
5109 */
5110 if (target_is_emmc_boot())
Mayank Grover7b8a8f82017-10-27 13:07:59 +05305111 publish_getvar_partition_info(part_info, partition_get_partition_count());
Amol Jadi5edf3552013-07-23 14:15:34 -07005112
Mayank Grover351a75e2017-05-30 20:06:08 +05305113 if (partition_multislot_is_supported())
5114 publish_getvar_multislot_vars();
5115
Amol Jadi5edf3552013-07-23 14:15:34 -07005116 /* Max download size supported */
lijuang2fbed912019-07-12 14:15:05 +08005117#if !VERIFIED_BOOT_2
Ameya Thakur11cf1a62013-08-05 12:44:48 -07005118 snprintf(max_download_size, MAX_RSP_SIZE, "\t0x%x",
5119 target_get_max_flash_size());
lijuang2fbed912019-07-12 14:15:05 +08005120#else
5121 snprintf(max_download_size, MAX_RSP_SIZE, "\t0x%x",
5122 SUB_SALT_BUFF_OFFSET(target_get_max_flash_size()));
5123#endif
5124
Amol Jadi5edf3552013-07-23 14:15:34 -07005125 fastboot_publish("max-download-size", (const char *) max_download_size);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07005126 /* Is the charger screen check enabled */
5127 snprintf(charger_screen_enabled, MAX_RSP_SIZE, "%d",
5128 device.charger_screen_enabled);
5129 fastboot_publish("charger-screen-enabled",
5130 (const char *) charger_screen_enabled);
lijuanga25d8bb2015-09-16 13:11:52 +08005131 fastboot_publish("off-mode-charge", (const char *) charger_screen_enabled);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05305132 snprintf(panel_display_mode, MAX_RSP_SIZE, "%s",
5133 device.display_panel);
5134 fastboot_publish("display-panel",
5135 (const char *) panel_display_mode);
Mukesh Ojhabb6e7f32018-03-02 15:04:06 +05305136
5137 if (target_is_emmc_boot())
5138 {
5139 mmc_blocksize = mmc_get_device_blocksize();
5140 }
5141 else
5142 {
5143 mmc_blocksize = flash_block_size();
5144 }
5145 snprintf(block_size_string, MAX_RSP_SIZE, "0x%x", mmc_blocksize);
5146 fastboot_publish("erase-block-size", (const char *) block_size_string);
5147 fastboot_publish("logical-block-size", (const char *) block_size_string);
Mayank Grover7b8a8f82017-10-27 13:07:59 +05305148#if PRODUCT_IOT
5149 get_bootloader_version_iot(&bootloader_version_string);
5150 fastboot_publish("version-bootloader", (const char *) bootloader_version_string);
5151
5152 /* Version baseband is n/a for apq iot devices */
5153 fastboot_publish("version-baseband", "N/A");
Mayank Grover7b8a8f82017-10-27 13:07:59 +05305154#else
Channagoud Kadabiaafbbfc2015-03-04 17:52:56 -08005155 fastboot_publish("version-bootloader", (const char *) device.bootloader_version);
5156 fastboot_publish("version-baseband", (const char *) device.radio_version);
Mayank Grover7b8a8f82017-10-27 13:07:59 +05305157#endif
lijuangf16461c2015-08-03 17:09:34 +08005158 fastboot_publish("secure", is_secure_boot_enable()? "yes":"no");
Mayank Grover7b8a8f82017-10-27 13:07:59 +05305159 fastboot_publish("unlocked", device.is_unlocked ? "yes":"no");
lijuangf16461c2015-08-03 17:09:34 +08005160 smem_get_hw_platform_name((unsigned char *) hw_platform_buf, sizeof(hw_platform_buf));
5161 snprintf(get_variant, MAX_RSP_SIZE, "%s %s", hw_platform_buf,
5162 target_is_emmc_boot()? "eMMC":"UFS");
5163 fastboot_publish("variant", (const char *) get_variant);
lijuang65c5a822015-08-29 16:35:36 +08005164#if CHECK_BAT_VOLTAGE
lijuang102dfa92015-10-09 18:31:03 +08005165 update_battery_status();
lijuang65c5a822015-08-29 16:35:36 +08005166 fastboot_publish("battery-voltage", (const char *) battery_voltage);
lijuang102dfa92015-10-09 18:31:03 +08005167 fastboot_publish("battery-soc-ok", (const char *) battery_soc_ok);
lijuang65c5a822015-08-29 16:35:36 +08005168#endif
Mayank Grover98c4c742019-04-25 17:21:37 +05305169 if (target_dynamic_partition_supported())
5170 fastboot_publish("is-userspace", "no");
Amol Jadi5edf3552013-07-23 14:15:34 -07005171}
5172
Brian Swetland9c4c0752009-01-25 16:23:50 -08005173void aboot_init(const struct app_descriptor *app)
5174{
Shashank Mittal4f99a882010-02-01 13:58:50 -08005175 unsigned reboot_mode = 0;
Mayank Grover351a75e2017-05-30 20:06:08 +05305176 int boot_err_type = 0;
5177 int boot_slot = INVALID;
Chandan Uddarajubedca152010-06-02 23:05:15 -07005178
lijuang39831732016-01-08 17:49:02 +08005179 /* Initialise wdog to catch early lk crashes */
5180#if WDOG_SUPPORT
5181 msm_wdog_init();
5182#endif
5183
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07005184 /* Setup page size information for nv storage */
Shashank Mittald8c42bf2010-06-09 15:44:28 -07005185 if (target_is_emmc_boot())
5186 {
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07005187 page_size = mmc_page_size();
Shashank Mittald8c42bf2010-06-09 15:44:28 -07005188 page_mask = page_size - 1;
Gaurav Nebhwani4d2389c2016-03-17 21:10:05 +05305189 mmc_blocksize = mmc_get_device_blocksize();
5190 mmc_blocksize_mask = mmc_blocksize - 1;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07005191 }
5192 else
5193 {
5194 page_size = flash_page_size();
5195 page_mask = page_size - 1;
5196 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07005197 ASSERT((MEMBASE + MEMSIZE) > MEMBASE);
5198
Ameya Thakur11cf1a62013-08-05 12:44:48 -07005199 read_device_info(&device);
vijay kumarc65876c2015-04-24 13:29:16 +05305200 read_allow_oem_unlock(&device);
Shashank Mittal162244e2011-08-08 19:01:25 -07005201
Mayank Grover351a75e2017-05-30 20:06:08 +05305202 /* Detect multi-slot support */
5203 if (partition_multislot_is_supported())
5204 {
5205 boot_slot = partition_find_active_slot();
5206 if (boot_slot == INVALID)
5207 {
5208 boot_into_fastboot = true;
5209 dprintf(INFO, "Active Slot: (INVALID)\n");
5210 }
5211 else
5212 {
5213 /* Setting the state of system to boot active slot */
5214 partition_mark_active_slot(boot_slot);
5215 dprintf(INFO, "Active Slot: (%s)\n", SUFFIX_SLOT(boot_slot));
5216 }
5217 }
5218
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08005219 /* Display splash screen if enabled */
5220#if DISPLAY_SPLASH_SCREEN
lijuang99c02d82015-02-13 19:04:34 +08005221#if NO_ALARM_DISPLAY
5222 if (!check_alarm_boot()) {
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08005223#endif
lijuang99c02d82015-02-13 19:04:34 +08005224 dprintf(SPEW, "Display Init: Start\n");
Ajay Singh Parmara7865a82015-04-16 21:27:52 -07005225#if DISPLAY_HDMI_PRIMARY
5226 if (!strlen(device.display_panel))
5227 strlcpy(device.display_panel, DISPLAY_PANEL_HDMI,
5228 sizeof(device.display_panel));
5229#endif
Channagoud Kadabi90869ce2015-04-27 11:15:14 -07005230#if ENABLE_WBC
Channagoud Kadabid801ac72015-08-26 11:21:51 -07005231 /* Wait if the display shutdown is in progress */
5232 while(pm_app_display_shutdown_in_prgs());
Channagoud Kadabi90869ce2015-04-27 11:15:14 -07005233 if (!pm_appsbl_display_init_done())
5234 target_display_init(device.display_panel);
5235 else
5236 display_image_on_screen();
5237#else
lijuang99c02d82015-02-13 19:04:34 +08005238 target_display_init(device.display_panel);
Channagoud Kadabi90869ce2015-04-27 11:15:14 -07005239#endif
lijuang99c02d82015-02-13 19:04:34 +08005240 dprintf(SPEW, "Display Init: Done\n");
5241#if NO_ALARM_DISPLAY
5242 }
5243#endif
5244#endif
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08005245
Greg Griscod6250552011-06-29 14:40:23 -07005246 target_serialno((unsigned char *) sn_buf);
Ajay Dudanib06c05f2011-05-12 14:46:10 -07005247 dprintf(SPEW,"serial number: %s\n",sn_buf);
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -08005248
Dhaval Patel223ec952013-07-18 14:49:44 -07005249 memset(display_panel_buf, '\0', MAX_PANEL_BUF_SIZE);
5250
Matthew Qindefd5562014-07-11 18:02:40 +08005251 /*
5252 * Check power off reason if user force reset,
5253 * if yes phone will do normal boot.
5254 */
5255 if (is_user_force_reset())
5256 goto normal_boot;
5257
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07005258 /* Check if we should do something other than booting up */
Ameya Thakur0b9c2442013-05-10 13:22:56 -07005259 if (keys_get_state(KEY_VOLUMEUP) && keys_get_state(KEY_VOLUMEDOWN))
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07005260 {
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03005261 dprintf(ALWAYS,"dload mode key sequence detected\n");
lijuang395b5e62015-11-19 17:39:44 +08005262 reboot_device(EMERGENCY_DLOAD);
5263 dprintf(CRITICAL,"Failed to reboot into dload mode\n");
5264
Ameya Thakur0b9c2442013-05-10 13:22:56 -07005265 boot_into_fastboot = true;
5266 }
5267 if (!boot_into_fastboot)
5268 {
5269 if (keys_get_state(KEY_HOME) || keys_get_state(KEY_VOLUMEUP))
5270 boot_into_recovery = 1;
5271 if (!boot_into_recovery &&
5272 (keys_get_state(KEY_BACK) || keys_get_state(KEY_VOLUMEDOWN)))
Pavel Nedev5d91d412013-04-29 11:34:24 +03005273 boot_into_fastboot = true;
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07005274 }
Shashank Mittald8c42bf2010-06-09 15:44:28 -07005275 #if NO_KEYPAD_DRIVER
Kinson Chik0b1c8162011-08-31 16:31:57 -07005276 if (fastboot_trigger())
Pavel Nedev5d91d412013-04-29 11:34:24 +03005277 boot_into_fastboot = true;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07005278 #endif
Chandan Uddarajubedca152010-06-02 23:05:15 -07005279
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07005280#if USE_PON_REBOOT_REG
5281 reboot_mode = check_hard_reboot_mode();
5282#else
Ajay Dudani77421292010-10-27 19:34:06 -07005283 reboot_mode = check_reboot_mode();
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07005284#endif
5285 if (reboot_mode == RECOVERY_MODE)
5286 {
Ajay Dudani77421292010-10-27 19:34:06 -07005287 boot_into_recovery = 1;
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07005288 }
5289 else if(reboot_mode == FASTBOOT_MODE)
5290 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03005291 boot_into_fastboot = true;
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07005292 }
5293 else if(reboot_mode == ALARM_BOOT)
5294 {
Matthew Qind886f3c2014-01-17 16:52:01 +08005295 boot_reason_alarm = true;
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07005296 }
Monika Singhb0db4b82018-09-26 12:18:02 +05305297#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover912eaa62017-10-26 12:08:53 +05305298 else if (VB_M <= target_get_vb_version())
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07005299 {
Mayank Grover889be1b2017-09-12 20:12:23 +05305300 if (reboot_mode == DM_VERITY_ENFORCING)
5301 {
5302 device.verity_mode = 1;
5303 write_device_info(&device);
5304 }
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07005305#if ENABLE_VB_ATTEST
Mayank Grover889be1b2017-09-12 20:12:23 +05305306 else if (reboot_mode == DM_VERITY_EIO)
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07005307#else
Mayank Grover889be1b2017-09-12 20:12:23 +05305308 else if (reboot_mode == DM_VERITY_LOGGING)
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07005309#endif
Mayank Grover889be1b2017-09-12 20:12:23 +05305310 {
5311 device.verity_mode = 0;
5312 write_device_info(&device);
5313 }
5314 else if (reboot_mode == DM_VERITY_KEYSCLEAR)
5315 {
5316 if(send_delete_keys_to_tz())
5317 ASSERT(0);
5318 }
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07005319 }
Parth Dixitddbc7352015-10-18 03:13:31 +05305320#endif
Ajay Dudani77421292010-10-27 19:34:06 -07005321
Matthew Qindefd5562014-07-11 18:02:40 +08005322normal_boot:
Pavel Nedev5d91d412013-04-29 11:34:24 +03005323 if (!boot_into_fastboot)
Shashank Mittald8c42bf2010-06-09 15:44:28 -07005324 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03005325 if (target_is_emmc_boot())
Shashank Mittala0032282011-08-26 14:50:11 -07005326 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03005327 if(emmc_recovery_init())
5328 dprintf(ALWAYS,"error in emmc_recovery_init\n");
5329 if(target_use_signed_kernel())
Shashank Mittala0032282011-08-26 14:50:11 -07005330 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03005331 if((device.is_unlocked) || (device.is_tampered))
5332 {
5333 #ifdef TZ_TAMPER_FUSE
Monika Singh98257462018-06-06 11:28:49 +05305334 set_tamper_fuse_cmd(HLOS_IMG_TAMPER_FUSE);
Pavel Nedev5d91d412013-04-29 11:34:24 +03005335 #endif
5336 #if USE_PCOM_SECBOOT
5337 set_tamper_flag(device.is_tampered);
5338 #endif
5339 }
Shashank Mittala0032282011-08-26 14:50:11 -07005340 }
Amit Blay6281ebc2015-01-11 14:44:08 +02005341
Mayank Grover351a75e2017-05-30 20:06:08 +05305342retry_boot:
5343 /* Trying to boot active partition */
5344 if (partition_multislot_is_supported())
5345 {
5346 boot_slot = partition_find_boot_slot();
5347 partition_mark_active_slot(boot_slot);
5348 if (boot_slot == INVALID)
5349 goto fastboot;
5350 }
5351
5352 boot_err_type = boot_linux_from_mmc();
5353 switch (boot_err_type)
5354 {
5355 case ERR_INVALID_PAGE_SIZE:
5356 case ERR_DT_PARSE:
5357 case ERR_ABOOT_ADDR_OVERLAP:
Mayank Grover10cabbe2017-10-30 19:11:05 +05305358 case ERR_INVALID_BOOT_MAGIC:
Mayank Grover351a75e2017-05-30 20:06:08 +05305359 if(partition_multislot_is_supported())
Mayank Grover10cabbe2017-10-30 19:11:05 +05305360 {
5361 /*
5362 * Deactivate current slot, as it failed to
5363 * boot, and retry next slot.
5364 */
5365 partition_deactivate_slot(boot_slot);
Mayank Grover351a75e2017-05-30 20:06:08 +05305366 goto retry_boot;
Mayank Grover10cabbe2017-10-30 19:11:05 +05305367 }
Mayank Grover351a75e2017-05-30 20:06:08 +05305368 else
5369 break;
Mayank Grover351a75e2017-05-30 20:06:08 +05305370 default:
5371 break;
5372 /* going to fastboot menu */
5373 }
Shashank Mittala0032282011-08-26 14:50:11 -07005374 }
Pavel Nedev5d91d412013-04-29 11:34:24 +03005375 else
5376 {
5377 recovery_init();
5378 #if USE_PCOM_SECBOOT
5379 if((device.is_unlocked) || (device.is_tampered))
5380 set_tamper_flag(device.is_tampered);
5381 #endif
5382 boot_linux_from_flash();
5383 }
5384 dprintf(CRITICAL, "ERROR: Could not do normal boot. Reverting "
5385 "to fastboot mode.\n");
Shashank Mittald8c42bf2010-06-09 15:44:28 -07005386 }
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07005387
Mayank Grover351a75e2017-05-30 20:06:08 +05305388fastboot:
Amol Jadi5edf3552013-07-23 14:15:34 -07005389 /* We are here means regular boot did not happen. Start fastboot. */
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07005390
Amol Jadi5edf3552013-07-23 14:15:34 -07005391 /* register aboot specific fastboot commands */
5392 aboot_fastboot_register_commands();
Amol Jadi57abe4c2011-05-24 15:47:27 -07005393
Amol Jadi5edf3552013-07-23 14:15:34 -07005394 /* dump partition table for debug info */
Kinson Chikf1a43512011-07-14 11:28:39 -07005395 partition_dump();
Amol Jadi5edf3552013-07-23 14:15:34 -07005396
5397 /* initialize and start fastboot */
Saranya Chidura41b34532018-10-16 12:29:52 +05305398#if !VERIFIED_BOOT_2
Amol Jadi5edf3552013-07-23 14:15:34 -07005399 fastboot_init(target_get_scratch_address(), target_get_max_flash_size());
Saranya Chidura41b34532018-10-16 12:29:52 +05305400#else
5401 /* Add salt buffer offset at start of image address to copy VB salt */
5402 fastboot_init(ADD_SALT_BUFF_OFFSET(target_get_scratch_address()),
5403 SUB_SALT_BUFF_OFFSET(target_get_max_flash_size()));
5404#endif
lijuang4ece1e72015-08-14 21:02:36 +08005405#if FBCON_DISPLAY_MSG
lijuang9a7d3b92015-11-30 14:41:24 +08005406 display_fastboot_menu();
lijuang4ece1e72015-08-14 21:02:36 +08005407#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08005408}
5409
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07005410uint32_t get_page_size()
5411{
5412 return page_size;
5413}
5414
Amir Samuelov57a6fa22013-06-05 16:36:43 +03005415/*
5416 * Calculated and save hash (SHA256) for non-signed boot image.
5417 *
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07005418 * @param image_addr - Boot image address
5419 * @param image_size - Size of the boot image
Amir Samuelov57a6fa22013-06-05 16:36:43 +03005420 *
5421 * @return int - 0 on success, negative value on failure.
5422 */
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07005423static int aboot_save_boot_hash_mmc(uint32_t image_addr, uint32_t image_size)
Amir Samuelov57a6fa22013-06-05 16:36:43 +03005424{
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07005425 unsigned int digest[8];
5426#if IMAGE_VERIF_ALGO_SHA1
5427 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA1;
5428#else
5429 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA256;
5430#endif
Amir Samuelov57a6fa22013-06-05 16:36:43 +03005431
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07005432 target_crypto_init_params();
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -08005433 hash_find((unsigned char *) image_addr, image_size, (unsigned char *)&digest, auth_algo);
Amir Samuelov57a6fa22013-06-05 16:36:43 +03005434
5435 save_kernel_hash_cmd(digest);
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07005436 dprintf(INFO, "aboot_save_boot_hash_mmc: imagesize_actual size %d bytes.\n", (int) image_size);
Amir Samuelov57a6fa22013-06-05 16:36:43 +03005437
5438 return 0;
5439}
5440
Mayank Grover351a75e2017-05-30 20:06:08 +05305441
Brian Swetland9c4c0752009-01-25 16:23:50 -08005442APP_START(aboot)
5443 .init = aboot_init,
5444APP_END