blob: e4154acc50d4a3680b703a83fbafa1406571a008 [file] [log] [blame]
Brian Swetland9c4c0752009-01-25 16:23:50 -08001/*
2 * Copyright (c) 2009, Google Inc.
3 * All rights reserved.
4 *
Mayank Grover11ff9692018-01-11 11:54:49 +05305 * Copyright (c) 2009-2018, 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
70#include <pm_app_smbchg.h>
71#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
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800165static const char *usb_sn_cmdline = " androidboot.serialno=";
Pavel Nedev328ac822013-04-05 15:25:11 +0300166static const char *androidboot_mode = " androidboot.mode=";
Hareesh Gauthamabf1e1e2017-04-12 18:09:24 +0530167
168static const char *systemd_ffbm_mode = " systemd.unit=ffbm.target";
Matthew Qind886f3c2014-01-17 16:52:01 +0800169static const char *alarmboot_cmdline = " androidboot.alarmboot=true";
Pavel Nedev898298c2013-02-27 12:36:09 -0800170static const char *loglevel = " quiet";
Ajay Dudanica3a33c2011-11-18 08:31:40 -0800171static const char *battchg_pause = " androidboot.mode=charger";
Shashank Mittalcd98d472011-08-02 14:29:24 -0700172static const char *auth_kernel = " androidboot.authorized_kernel=true";
Pavel Nedev5614d222013-06-17 18:01:02 +0300173static const char *secondary_gpt_enable = " gpt";
Monika Singh292b3e92018-03-17 22:40:23 +0530174#ifdef MDTP_SUPPORT
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200175static const char *mdtp_activated_flag = " mdtp";
Monika Singh292b3e92018-03-17 22:40:23 +0530176#endif
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800177static const char *baseband_apq = " androidboot.baseband=apq";
178static const char *baseband_msm = " androidboot.baseband=msm";
179static const char *baseband_csfb = " androidboot.baseband=csfb";
180static const char *baseband_svlte2a = " androidboot.baseband=svlte2a";
Ajay Dudani403bc492011-09-30 16:17:21 -0700181static const char *baseband_mdm = " androidboot.baseband=mdm";
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800182static const char *baseband_mdm2 = " androidboot.baseband=mdm2";
Amol Jadi5c61a952012-05-04 17:05:35 -0700183static const char *baseband_sglte = " androidboot.baseband=sglte";
Amol Jadi2a15a272013-01-22 12:03:36 -0800184static const char *baseband_dsda = " androidboot.baseband=dsda";
185static const char *baseband_dsda2 = " androidboot.baseband=dsda2";
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800186static const char *baseband_sglte2 = " androidboot.baseband=sglte2";
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800187static const char *warmboot_cmdline = " qpnp-power-on.warm_boot=1";
Vijay Kumar Pendotib228cfc2016-06-13 20:15:23 +0530188static const char *baseband_apq_nowgr = " androidboot.baseband=baseband_apq_nowgr";
Mayank Grover351a75e2017-05-30 20:06:08 +0530189static const char *androidboot_slot_suffix = " androidboot.slot_suffix=";
190static const char *skip_ramfs = " skip_initramfs";
Mayank Grover466d6562018-05-10 14:52:20 +0530191
192#if HIBERNATION_SUPPORT
193static const char *resume = " resume=/dev/mmcblk0p";
194#endif
195
Sourabh Banerjee6c7153c2018-03-20 01:21:57 +0530196#ifdef INIT_BIN_LE
197static const char *sys_path_cmdline = " rootwait ro init="INIT_BIN_LE;
198#else
Mayank Grover3804be72017-06-22 11:59:23 +0530199static const char *sys_path_cmdline = " rootwait ro init=/init";
Sourabh Banerjee6c7153c2018-03-20 01:21:57 +0530200#endif
Sourabh Banerjee386b1322018-02-27 09:37:28 +0530201
202#if VERITY_LE
203static const char *verity_dev = " root=/dev/dm-0";
204static const char *verity_system_part = " dm=\"system";
205static const char *verity_params = " none ro,0 1 android-verity /dev/mmcblk0p";
206#else
Mayank Grover5384ed82018-05-09 12:09:24 +0530207static const char *sys_path = " root=/dev/mmcblk0p";
Sourabh Banerjee386b1322018-02-27 09:37:28 +0530208#endif
Ajay Dudanid04110c2011-01-17 23:55:07 -0800209
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700210#if VERIFIED_BOOT
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700211static const char *verity_mode = " androidboot.veritymode=";
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700212static const char *verified_state= " androidboot.verifiedbootstate=";
Parth Dixita5715a02015-10-29 12:25:10 +0530213static const char *keymaster_v1= " androidboot.keymaster=1";
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700214//indexed based on enum values, green is 0 by default
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700215
216struct verified_boot_verity_mode vbvm[] =
217{
Channagoud Kadabi86b0c112016-03-16 19:23:16 -0700218#if ENABLE_VB_ATTEST
219 {false, "eio"},
220#else
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700221 {false, "logging"},
Channagoud Kadabi86b0c112016-03-16 19:23:16 -0700222#endif
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700223 {true, "enforcing"},
224};
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700225struct verified_boot_state_name vbsn[] =
226{
227 {GREEN, "green"},
228 {ORANGE, "orange"},
229 {YELLOW,"yellow"},
230 {RED,"red" },
231};
232#endif
Channagoud Kadabi86b0c112016-03-16 19:23:16 -0700233/*As per spec delay wait time before shutdown in Red state*/
234#define DELAY_WAIT 30000
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700235static unsigned page_size = 0;
236static unsigned page_mask = 0;
Gaurav Nebhwani4d2389c2016-03-17 21:10:05 +0530237static unsigned mmc_blocksize = 0;
238static unsigned mmc_blocksize_mask = 0;
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700239static char ffbm_mode_string[FFBM_MODE_BUF_SIZE];
240static bool boot_into_ffbm;
vijay kumar870515d2015-08-31 16:37:24 +0530241static char *target_boot_params = NULL;
Matthew Qind886f3c2014-01-17 16:52:01 +0800242static bool boot_reason_alarm;
Channagoud Kadabi80a182b2015-03-11 17:04:23 -0700243static bool devinfo_present = true;
Channagoud Kadabi736c4962015-08-21 11:56:52 -0700244bool boot_into_fastboot = false;
Parth Dixit4097b622016-03-15 14:42:27 +0530245static uint32_t dt_size = 0;
Monika Singh292b3e92018-03-17 22:40:23 +0530246static char *vbcmdline;
247static bootinfo info = {0};
Shashank Mittalcd98d472011-08-02 14:29:24 -0700248/* Assuming unauthorized kernel image by default */
249static int auth_kernel_img = 0;
Monika Singh292b3e92018-03-17 22:40:23 +0530250static device_info device = {DEVICE_MAGIC,0,0,0,0,{0},{0},{0},1,{0},0,{0}};
251
vijay kumarc65876c2015-04-24 13:29:16 +0530252static bool is_allow_unlock = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -0700253
vijay kumarca2e6812015-07-08 20:28:25 +0530254static char frp_ptns[2][8] = {"config","frp"};
255
lijuang511a2b52015-08-14 20:50:51 +0800256static const char *critical_flash_allowed_ptn[] = {
257 "aboot",
258 "rpm",
259 "tz",
260 "sbl",
261 "sdi",
262 "sbl1",
263 "xbl",
264 "hyp",
265 "pmic",
266 "bootloader",
267 "devinfo",
268 "partition"};
269
Dima Zavin42168f22009-01-30 11:52:22 -0800270struct atag_ptbl_entry
271{
272 char name[16];
273 unsigned offset;
274 unsigned size;
275 unsigned flags;
276};
277
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700278/*
279 * Partition info, required to be published
280 * for fastboot
281 */
282struct getvar_partition_info {
Mayank Grover7b8a8f82017-10-27 13:07:59 +0530283 char part_name[MAX_GPT_NAME_SIZE]; /* Partition name */
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700284 char getvar_size[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for size */
285 char getvar_type[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for type */
286 char size_response[MAX_RSP_SIZE]; /* fastboot response for size */
287 char type_response[MAX_RSP_SIZE]; /* fastboot response for type */
288};
289
290/*
Mayank Grover7b8a8f82017-10-27 13:07:59 +0530291 * Update the part_type_known for known paritions types.
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700292 */
Mayank Groverd38fe012018-03-13 15:33:16 +0530293#define RAW_STR "raw"
Mayank Grover52cd10a2018-03-15 12:57:54 +0530294#define EXT_STR "ext4"
295#define F2FS_STR "f2fs"
296
297#define FS_SUPERBLOCK_OFFSET 0x400
298#define EXT_MAGIC 0xEF53
299#define EXT_MAGIC_OFFSET_SB 0x38
300#define F2FS_MAGIC 0xF2F52010 // F2FS Magic Number
301#define F2FS_MAGIC_OFFSET_SB 0x0
302
303typedef enum fs_signature_type {
304 EXT_FS_SIGNATURE = 1,
305 EXT_F2FS_SIGNATURE = 2,
306 NO_FS = -1
307} fs_signature_type;
308
Mayank Grover7b8a8f82017-10-27 13:07:59 +0530309struct getvar_partition_info part_info[NUM_PARTITIONS];
310struct getvar_partition_info part_type_known[] =
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700311{
Mayank Grover49920212018-02-09 18:15:55 +0530312 { "system" , "partition-size:", "partition-type:", "", "ext4" },
313 { "userdata" , "partition-size:", "partition-type:", "", "ext4" },
314 { "cache" , "partition-size:", "partition-type:", "", "ext4" },
315 { "recoveryfs" , "partition-size:", "partition-type:", "", "ext4" },
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700316};
317
318char max_download_size[MAX_RSP_SIZE];
Ameya Thakur11cf1a62013-08-05 12:44:48 -0700319char charger_screen_enabled[MAX_RSP_SIZE];
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800320char sn_buf[13];
Dhaval Patel223ec952013-07-18 14:49:44 -0700321char display_panel_buf[MAX_PANEL_BUF_SIZE];
Unnati Gandhi62c8ab82014-01-24 11:01:01 +0530322char panel_display_mode[MAX_RSP_SIZE];
Mayank Grovera64dfbe2018-05-17 14:06:34 +0530323char soc_version_str[MAX_RSP_SIZE];
Mayank Grover7b8a8f82017-10-27 13:07:59 +0530324char block_size_string[MAX_RSP_SIZE];
Mukesh Ojhabb6e7f32018-03-02 15:04:06 +0530325#if PRODUCT_IOT
Mayank Grover7b8a8f82017-10-27 13:07:59 +0530326
327/* For IOT we are using custom version */
328#define PRODUCT_IOT_VERSION "IOT001"
329char bootloader_version_string[MAX_RSP_SIZE];
330#endif
lijuang102dfa92015-10-09 18:31:03 +0800331
332#if CHECK_BAT_VOLTAGE
lijuang65c5a822015-08-29 16:35:36 +0800333char battery_voltage[MAX_RSP_SIZE];
lijuang102dfa92015-10-09 18:31:03 +0800334char battery_soc_ok [MAX_RSP_SIZE];
335#endif
336
lijuangf16461c2015-08-03 17:09:34 +0800337char get_variant[MAX_RSP_SIZE];
Greg Griscod6250552011-06-29 14:40:23 -0700338
Greg Griscod2471ef2011-07-14 13:00:42 -0700339extern int emmc_recovery_init(void);
340
Kinson Chik0b1c8162011-08-31 16:31:57 -0700341#if NO_KEYPAD_DRIVER
342extern int fastboot_trigger(void);
343#endif
Greg Griscod2471ef2011-07-14 13:00:42 -0700344
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800345static void update_ker_tags_rdisk_addr(struct boot_img_hdr *hdr, bool is_arm64)
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700346{
347 /* overwrite the destination of specified for the project */
Channagoud Kadabi7042fa32013-04-26 16:44:14 -0700348#ifdef ABOOT_IGNORE_BOOT_HEADER_ADDRS
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800349 if (is_arm64)
350 hdr->kernel_addr = ABOOT_FORCE_KERNEL64_ADDR;
351 else
352 hdr->kernel_addr = ABOOT_FORCE_KERNEL_ADDR;
Channagoud Kadabi7042fa32013-04-26 16:44:14 -0700353 hdr->ramdisk_addr = ABOOT_FORCE_RAMDISK_ADDR;
354 hdr->tags_addr = ABOOT_FORCE_TAGS_ADDR;
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700355#endif
356}
357
Dima Zavin42168f22009-01-30 11:52:22 -0800358static void ptentry_to_tag(unsigned **ptr, struct ptentry *ptn)
359{
360 struct atag_ptbl_entry atag_ptn;
361
362 memcpy(atag_ptn.name, ptn->name, 16);
363 atag_ptn.name[15] = '\0';
364 atag_ptn.offset = ptn->start;
365 atag_ptn.size = ptn->length;
366 atag_ptn.flags = ptn->flags;
367 memcpy(*ptr, &atag_ptn, sizeof(struct atag_ptbl_entry));
368 *ptr += sizeof(struct atag_ptbl_entry) / sizeof(unsigned);
369}
Brian Swetland9c4c0752009-01-25 16:23:50 -0800370
lijuang102dfa92015-10-09 18:31:03 +0800371#if CHECK_BAT_VOLTAGE
372void update_battery_status(void)
373{
374 snprintf(battery_voltage,MAX_RSP_SIZE, "%d",target_get_battery_voltage());
375 snprintf(battery_soc_ok ,MAX_RSP_SIZE, "%s",target_battery_soc_ok()? "yes":"no");
376}
377#endif
378
Neeti Desaie245d492012-06-01 12:52:13 -0700379unsigned char *update_cmdline(const char * cmdline)
Brian Swetland9c4c0752009-01-25 16:23:50 -0800380{
David Ng183a7422009-12-07 14:55:21 -0800381 int cmdline_len = 0;
382 int have_cmdline = 0;
Amol Jadi168b7712012-03-06 16:15:00 -0800383 unsigned char *cmdline_final = NULL;
Neeti Desaie245d492012-06-01 12:52:13 -0700384 int pause_at_bootup = 0;
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800385 bool warm_boot = false;
Pavel Nedev5614d222013-06-17 18:01:02 +0300386 bool gpt_exists = partition_gpt_exists();
Joonwoo Park61112782013-10-02 19:50:39 -0700387 int have_target_boot_params = 0;
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700388 char *boot_dev_buf = NULL;
Monika Singh292b3e92018-03-17 22:40:23 +0530389#ifdef MDTP_SUPPORT
Mayank Grover351a75e2017-05-30 20:06:08 +0530390 bool is_mdtp_activated = 0;
Monika Singh292b3e92018-03-17 22:40:23 +0530391#endif
Mayank Grover351a75e2017-05-30 20:06:08 +0530392 int current_active_slot = INVALID;
Mayank Grover3804be72017-06-22 11:59:23 +0530393 int system_ptn_index = -1;
394 unsigned int lun = 0;
395 char lun_char_base = 'a';
Sourabh Banerjee386b1322018-02-27 09:37:28 +0530396#if VERITY_LE
397 int syspath_buflen = strlen(verity_dev)
398 + strlen(verity_system_part) + (sizeof(char) * 2) + 2
399 + strlen(verity_params) + sizeof(int) + 2;
400#else
401 int syspath_buflen = strlen(sys_path) + sizeof(int) + 2; /*allocate buflen for largest possible string*/
402#endif
Mayank Grover3804be72017-06-22 11:59:23 +0530403 char syspath_buf[syspath_buflen];
Mayank Grover466d6562018-05-10 14:52:20 +0530404#if HIBERNATION_SUPPORT
405 int resume_buflen = strlen(resume) + sizeof(int) + 2;
406 char resume_buf[resume_buflen];
407 int swap_ptn_index = INVALID_PTN;
408#endif
409
Mayank Grover889be1b2017-09-12 20:12:23 +0530410#if VERIFIED_BOOT
411 uint32_t boot_state = RED;
412#endif
Hareesh Gauthamabf1e1e2017-04-12 18:09:24 +0530413
414#if USE_LE_SYSTEMD
415 is_systemd_present=true;
416#endif
417
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700418#if VERIFIED_BOOT
Mayank Grover912eaa62017-10-26 12:08:53 +0530419 if (VB_M <= target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +0530420 {
421 boot_state = boot_verify_get_state();
422 }
Parth Dixitddbc7352015-10-18 03:13:31 +0530423#endif
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700424
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200425#ifdef MDTP_SUPPORT
426 mdtp_activated(&is_mdtp_activated);
427#endif /* MDTP_SUPPORT */
Dima Zavin42168f22009-01-30 11:52:22 -0800428
Brian Swetland9c4c0752009-01-25 16:23:50 -0800429 if (cmdline && cmdline[0]) {
David Ng183a7422009-12-07 14:55:21 -0800430 cmdline_len = strlen(cmdline);
431 have_cmdline = 1;
432 }
433 if (target_is_emmc_boot()) {
434 cmdline_len += strlen(emmc_cmdline);
Sridhar Parasuram7bd4aaf2015-02-12 11:14:38 -0800435#if USE_BOOTDEV_CMDLINE
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700436 boot_dev_buf = (char *) malloc(sizeof(char) * BOOT_DEV_MAX_LEN);
437 ASSERT(boot_dev_buf);
438 platform_boot_dev_cmdline(boot_dev_buf);
439 cmdline_len += strlen(boot_dev_buf);
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700440#endif
David Ng183a7422009-12-07 14:55:21 -0800441 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800442
443 cmdline_len += strlen(usb_sn_cmdline);
444 cmdline_len += strlen(sn_buf);
445
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700446#if VERIFIED_BOOT
Mayank Grover912eaa62017-10-26 12:08:53 +0530447 if (VB_M <= target_get_vb_version())
Sridhar Parasuram7d8bb9a2015-09-23 18:21:19 -0700448 {
Mayank Grover889be1b2017-09-12 20:12:23 +0530449 cmdline_len += strlen(verified_state) + strlen(vbsn[boot_state].name);
450 if ((device.verity_mode != 0 ) && (device.verity_mode != 1))
451 {
452 dprintf(CRITICAL, "Devinfo paritition possibly corrupted!!!. Please erase devinfo partition to continue booting\n");
453 ASSERT(0);
454 }
455 cmdline_len += strlen(verity_mode) + strlen(vbvm[device.verity_mode].name);
456 cmdline_len += strlen(keymaster_v1);
Sridhar Parasuram7d8bb9a2015-09-23 18:21:19 -0700457 }
Parth Dixitddbc7352015-10-18 03:13:31 +0530458#endif
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700459
Monika Singh292b3e92018-03-17 22:40:23 +0530460
461 if (vbcmdline != NULL) {
462 dprintf(DEBUG, "UpdateCmdLine vbcmdline present len %d\n",
463 strlen(vbcmdline));
464 cmdline_len += strlen(vbcmdline);
465 }
466
Pavel Nedev5614d222013-06-17 18:01:02 +0300467 if (boot_into_recovery && gpt_exists)
468 cmdline_len += strlen(secondary_gpt_enable);
469
Monika Singh292b3e92018-03-17 22:40:23 +0530470#ifdef MDTP_SUPPORT
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200471 if(is_mdtp_activated)
472 cmdline_len += strlen(mdtp_activated_flag);
Monika Singh292b3e92018-03-17 22:40:23 +0530473#endif
Pavel Nedev328ac822013-04-05 15:25:11 +0300474 if (boot_into_ffbm) {
475 cmdline_len += strlen(androidboot_mode);
Hareesh Gauthamabf1e1e2017-04-12 18:09:24 +0530476
477 if(is_systemd_present)
478 cmdline_len += strlen(systemd_ffbm_mode);
479
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700480 cmdline_len += strlen(ffbm_mode_string);
Pavel Nedev898298c2013-02-27 12:36:09 -0800481 /* reduce kernel console messages to speed-up boot */
482 cmdline_len += strlen(loglevel);
Matthew Qind886f3c2014-01-17 16:52:01 +0800483 } else if (boot_reason_alarm) {
484 cmdline_len += strlen(alarmboot_cmdline);
Zhenhua Huang431dafa2015-06-30 16:13:37 +0800485 } else if ((target_build_variant_user() || device.charger_screen_enabled)
lijuang266b17d2018-08-17 18:53:42 +0800486 && target_pause_for_battery_charge() && !boot_into_recovery) {
David Ngf773dde2010-07-26 19:55:08 -0700487 pause_at_bootup = 1;
488 cmdline_len += strlen(battchg_pause);
489 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800490
Shashank Mittalcd98d472011-08-02 14:29:24 -0700491 if(target_use_signed_kernel() && auth_kernel_img) {
492 cmdline_len += strlen(auth_kernel);
493 }
494
Joonwoo Park61112782013-10-02 19:50:39 -0700495 if (get_target_boot_params(cmdline, boot_into_recovery ? "recoveryfs" :
496 "system",
vijay kumar870515d2015-08-31 16:37:24 +0530497 &target_boot_params) == 0) {
Joonwoo Park61112782013-10-02 19:50:39 -0700498 have_target_boot_params = 1;
499 cmdline_len += strlen(target_boot_params);
500 }
501
Ajay Dudanid04110c2011-01-17 23:55:07 -0800502 /* Determine correct androidboot.baseband to use */
503 switch(target_baseband())
504 {
505 case BASEBAND_APQ:
506 cmdline_len += strlen(baseband_apq);
507 break;
508
509 case BASEBAND_MSM:
510 cmdline_len += strlen(baseband_msm);
511 break;
512
513 case BASEBAND_CSFB:
514 cmdline_len += strlen(baseband_csfb);
515 break;
516
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800517 case BASEBAND_SVLTE2A:
518 cmdline_len += strlen(baseband_svlte2a);
Ajay Dudanid04110c2011-01-17 23:55:07 -0800519 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700520
521 case BASEBAND_MDM:
522 cmdline_len += strlen(baseband_mdm);
523 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700524
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800525 case BASEBAND_MDM2:
526 cmdline_len += strlen(baseband_mdm2);
527 break;
528
Amol Jadi5c61a952012-05-04 17:05:35 -0700529 case BASEBAND_SGLTE:
530 cmdline_len += strlen(baseband_sglte);
531 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530532
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800533 case BASEBAND_SGLTE2:
534 cmdline_len += strlen(baseband_sglte2);
535 break;
536
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530537 case BASEBAND_DSDA:
538 cmdline_len += strlen(baseband_dsda);
539 break;
Amol Jadi2a15a272013-01-22 12:03:36 -0800540
541 case BASEBAND_DSDA2:
542 cmdline_len += strlen(baseband_dsda2);
543 break;
Vijay Kumar Pendotib228cfc2016-06-13 20:15:23 +0530544 case BASEBAND_APQ_NOWGR:
545 cmdline_len += strlen(baseband_apq_nowgr);
546 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800547 }
548
Vladislav Levenetz3cd45242017-04-05 08:58:07 +0300549#if ENABLE_DISPLAY
Lijuan Gao4a5b8322014-07-24 10:38:42 +0800550 if (cmdline) {
551 if ((strstr(cmdline, DISPLAY_DEFAULT_PREFIX) == NULL) &&
Padmanabhan Komandurubccbcdc2015-06-30 16:19:24 +0530552 target_display_panel_node(display_panel_buf,
553 MAX_PANEL_BUF_SIZE) &&
Lijuan Gao4a5b8322014-07-24 10:38:42 +0800554 strlen(display_panel_buf)) {
555 cmdline_len += strlen(display_panel_buf);
556 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700557 }
Vladislav Levenetz3cd45242017-04-05 08:58:07 +0300558#endif
Dhaval Patel223ec952013-07-18 14:49:44 -0700559
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800560 if (target_warm_boot()) {
561 warm_boot = true;
562 cmdline_len += strlen(warmboot_cmdline);
563 }
564
Mayank Grover5384ed82018-05-09 12:09:24 +0530565 if (target_uses_system_as_root() ||
566 partition_multislot_is_supported())
Mayank Grover351a75e2017-05-30 20:06:08 +0530567 {
568 current_active_slot = partition_find_active_slot();
569 cmdline_len += (strlen(androidboot_slot_suffix)+
570 strlen(SUFFIX_SLOT(current_active_slot)));
571
Mayank Grover3804be72017-06-22 11:59:23 +0530572 system_ptn_index = partition_get_index("system");
573 if (platform_boot_dev_isemmc())
574 {
Sourabh Banerjee386b1322018-02-27 09:37:28 +0530575#if VERITY_LE
576 /*
577 Condition 4: Verity and A/B both enabled
578 Eventual command line looks like:
579 ... androidboot.slot_suffix=<slot_suffix> ... rootfstype=ext4 ...
580 ... root=/dev/dm-0 dm="system_<slot_suffix> none ro,0 1 android-verity /dev/mmcblk0p<NN>"
581 */
582 snprintf(syspath_buf, syspath_buflen, " %s %s%s %s%d\"",
583 verity_dev,
584 verity_system_part, suffix_slot[current_active_slot],
585 verity_params, system_ptn_index + 1);
586#else
587 /*
588 Condition 5: A/B enabled, but verity disabled
589 Eventual command line looks like:
590 ... androidboot.slot_suffix=<slot_suffix> ... rootfstype=ext4 ...
591 ... root=/dev/mmcblk0p<NN> ...
592 */
593 snprintf(syspath_buf, syspath_buflen, " %s%d",
594 sys_path, system_ptn_index + 1);
595#endif
Mayank Grover3804be72017-06-22 11:59:23 +0530596 }
597 else
598 {
599 lun = partition_get_lun(system_ptn_index);
600 snprintf(syspath_buf, syspath_buflen, " root=/dev/sd%c%d",
601 lun_char_base + lun,
602 partition_get_index_in_lun("system", lun));
603 }
Mayank Grover351a75e2017-05-30 20:06:08 +0530604
Monika Singh292b3e92018-03-17 22:40:23 +0530605#ifndef VERIFIED_BOOT_2
Mayank Grover3804be72017-06-22 11:59:23 +0530606 cmdline_len += strlen(syspath_buf);
Monika Singh292b3e92018-03-17 22:40:23 +0530607#endif
Mayank Grover5384ed82018-05-09 12:09:24 +0530608 }
609
610 if (target_uses_system_as_root() ||
611 partition_multislot_is_supported())
612 {
613 cmdline_len += strlen(sys_path_cmdline);
Mayank Grover351a75e2017-05-30 20:06:08 +0530614 if (!boot_into_recovery)
615 cmdline_len += strlen(skip_ramfs);
616 }
617
Mayank Grover466d6562018-05-10 14:52:20 +0530618#if HIBERNATION_SUPPORT
619 if (platform_boot_dev_isemmc())
620 {
621 swap_ptn_index = partition_get_index("swap");
622 if (swap_ptn_index != INVALID_PTN)
623 {
624 snprintf(resume_buf, resume_buflen,
625 " %s%d", resume,
626 (swap_ptn_index + 1));
627 cmdline_len += strlen(resume_buf);
628 }
629 else
630 {
631 dprintf(INFO, "WARNING: swap partition not found\n");
632 }
633 }
634#endif
635
Channagoud Kadabi85c7ec32016-01-28 23:09:21 -0800636#if TARGET_CMDLINE_SUPPORT
637 char *target_cmdline_buf = malloc(TARGET_MAX_CMDLNBUF);
638 int target_cmd_line_len;
639 ASSERT(target_cmdline_buf);
640 target_cmd_line_len = target_update_cmdline(target_cmdline_buf);
641 cmdline_len += target_cmd_line_len;
642#endif
643
David Ng183a7422009-12-07 14:55:21 -0800644 if (cmdline_len > 0) {
645 const char *src;
Maria Yu52254c02014-07-04 16:14:54 +0800646 unsigned char *dst;
647
648 cmdline_final = (unsigned char*) malloc((cmdline_len + 4) & (~3));
649 ASSERT(cmdline_final != NULL);
vijay kumar287bb542015-09-29 13:01:52 +0530650 memset((void *)cmdline_final, 0, sizeof(*cmdline_final));
Maria Yu52254c02014-07-04 16:14:54 +0800651 dst = cmdline_final;
Neeti Desaie245d492012-06-01 12:52:13 -0700652
Amol Jadi168b7712012-03-06 16:15:00 -0800653 /* Save start ptr for debug print */
David Ng183a7422009-12-07 14:55:21 -0800654 if (have_cmdline) {
655 src = cmdline;
656 while ((*dst++ = *src++));
657 }
658 if (target_is_emmc_boot()) {
659 src = emmc_cmdline;
660 if (have_cmdline) --dst;
David Ngf773dde2010-07-26 19:55:08 -0700661 have_cmdline = 1;
662 while ((*dst++ = *src++));
Sridhar Parasuram7bd4aaf2015-02-12 11:14:38 -0800663#if USE_BOOTDEV_CMDLINE
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700664 src = boot_dev_buf;
665 if (have_cmdline) --dst;
666 while ((*dst++ = *src++));
667#endif
David Ngf773dde2010-07-26 19:55:08 -0700668 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800669
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700670#if VERIFIED_BOOT
Mayank Grover912eaa62017-10-26 12:08:53 +0530671 if (VB_M <= target_get_vb_version())
Sridhar Parasuram7d8bb9a2015-09-23 18:21:19 -0700672 {
Mayank Grover889be1b2017-09-12 20:12:23 +0530673 src = verified_state;
674 if(have_cmdline) --dst;
675 have_cmdline = 1;
676 while ((*dst++ = *src++));
677 src = vbsn[boot_state].name;
678 if(have_cmdline) --dst;
679 while ((*dst++ = *src++));
680
681 if ((device.verity_mode != 0 ) && (device.verity_mode != 1))
682 {
683 dprintf(CRITICAL, "Devinfo paritition possibly corrupted!!!. Please erase devinfo partition to continue booting\n");
684 ASSERT(0);
685 }
686 src = verity_mode;
687 if(have_cmdline) --dst;
688 while ((*dst++ = *src++));
689 src = vbvm[device.verity_mode].name;
690 if(have_cmdline) -- dst;
691 while ((*dst++ = *src++));
692 src = keymaster_v1;
693 if(have_cmdline) --dst;
694 while ((*dst++ = *src++));
Sridhar Parasuram7d8bb9a2015-09-23 18:21:19 -0700695 }
Parth Dixitddbc7352015-10-18 03:13:31 +0530696#endif
Monika Singh292b3e92018-03-17 22:40:23 +0530697
698 if (vbcmdline != NULL) {
699 src = vbcmdline;
700 if (have_cmdline) --dst;
701 while ((*dst++ = *src++));
702 }
703
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800704 src = usb_sn_cmdline;
705 if (have_cmdline) --dst;
706 have_cmdline = 1;
707 while ((*dst++ = *src++));
708 src = sn_buf;
709 if (have_cmdline) --dst;
710 have_cmdline = 1;
711 while ((*dst++ = *src++));
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800712 if (warm_boot) {
713 if (have_cmdline) --dst;
714 src = warmboot_cmdline;
715 while ((*dst++ = *src++));
716 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800717
Pavel Nedev5614d222013-06-17 18:01:02 +0300718 if (boot_into_recovery && gpt_exists) {
719 src = secondary_gpt_enable;
720 if (have_cmdline) --dst;
721 while ((*dst++ = *src++));
722 }
Monika Singh292b3e92018-03-17 22:40:23 +0530723#ifdef MDTP_SUPPORT
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200724 if (is_mdtp_activated) {
725 src = mdtp_activated_flag;
726 if (have_cmdline) --dst;
727 while ((*dst++ = *src++));
728 }
Monika Singh292b3e92018-03-17 22:40:23 +0530729#endif
Pavel Nedev328ac822013-04-05 15:25:11 +0300730 if (boot_into_ffbm) {
731 src = androidboot_mode;
732 if (have_cmdline) --dst;
733 while ((*dst++ = *src++));
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700734 src = ffbm_mode_string;
Pavel Nedev328ac822013-04-05 15:25:11 +0300735 if (have_cmdline) --dst;
736 while ((*dst++ = *src++));
Hareesh Gauthamabf1e1e2017-04-12 18:09:24 +0530737
738 if(is_systemd_present) {
739 src = systemd_ffbm_mode;
740 if (have_cmdline) --dst;
741 while ((*dst++ = *src++));
742 }
743
Pavel Nedev898298c2013-02-27 12:36:09 -0800744 src = loglevel;
745 if (have_cmdline) --dst;
746 while ((*dst++ = *src++));
Matthew Qind886f3c2014-01-17 16:52:01 +0800747 } else if (boot_reason_alarm) {
748 src = alarmboot_cmdline;
749 if (have_cmdline) --dst;
750 while ((*dst++ = *src++));
Pavel Nedev328ac822013-04-05 15:25:11 +0300751 } else if (pause_at_bootup) {
David Ngf773dde2010-07-26 19:55:08 -0700752 src = battchg_pause;
753 if (have_cmdline) --dst;
David Ng183a7422009-12-07 14:55:21 -0800754 while ((*dst++ = *src++));
755 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800756
Shashank Mittalcd98d472011-08-02 14:29:24 -0700757 if(target_use_signed_kernel() && auth_kernel_img) {
758 src = auth_kernel;
759 if (have_cmdline) --dst;
760 while ((*dst++ = *src++));
761 }
762
Ajay Dudanid04110c2011-01-17 23:55:07 -0800763 switch(target_baseband())
764 {
765 case BASEBAND_APQ:
766 src = baseband_apq;
767 if (have_cmdline) --dst;
768 while ((*dst++ = *src++));
769 break;
770
771 case BASEBAND_MSM:
772 src = baseband_msm;
773 if (have_cmdline) --dst;
774 while ((*dst++ = *src++));
775 break;
776
777 case BASEBAND_CSFB:
778 src = baseband_csfb;
779 if (have_cmdline) --dst;
780 while ((*dst++ = *src++));
781 break;
782
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800783 case BASEBAND_SVLTE2A:
784 src = baseband_svlte2a;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800785 if (have_cmdline) --dst;
786 while ((*dst++ = *src++));
787 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700788
789 case BASEBAND_MDM:
790 src = baseband_mdm;
791 if (have_cmdline) --dst;
792 while ((*dst++ = *src++));
793 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700794
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800795 case BASEBAND_MDM2:
796 src = baseband_mdm2;
797 if (have_cmdline) --dst;
798 while ((*dst++ = *src++));
799 break;
800
Amol Jadi5c61a952012-05-04 17:05:35 -0700801 case BASEBAND_SGLTE:
802 src = baseband_sglte;
803 if (have_cmdline) --dst;
804 while ((*dst++ = *src++));
805 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530806
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800807 case BASEBAND_SGLTE2:
808 src = baseband_sglte2;
809 if (have_cmdline) --dst;
810 while ((*dst++ = *src++));
811 break;
812
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530813 case BASEBAND_DSDA:
814 src = baseband_dsda;
815 if (have_cmdline) --dst;
816 while ((*dst++ = *src++));
817 break;
Amol Jadi2a15a272013-01-22 12:03:36 -0800818
819 case BASEBAND_DSDA2:
820 src = baseband_dsda2;
821 if (have_cmdline) --dst;
822 while ((*dst++ = *src++));
823 break;
Vijay Kumar Pendotib228cfc2016-06-13 20:15:23 +0530824 case BASEBAND_APQ_NOWGR:
825 src = baseband_apq_nowgr;
826 if (have_cmdline) --dst;
827 while ((*dst++ = *src++));
828 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800829 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700830
831 if (strlen(display_panel_buf)) {
Dhaval Patel223ec952013-07-18 14:49:44 -0700832 src = display_panel_buf;
833 if (have_cmdline) --dst;
834 while ((*dst++ = *src++));
835 }
Joonwoo Park61112782013-10-02 19:50:39 -0700836
837 if (have_target_boot_params) {
838 if (have_cmdline) --dst;
839 src = target_boot_params;
840 while ((*dst++ = *src++));
vijay kumar870515d2015-08-31 16:37:24 +0530841 free(target_boot_params);
Joonwoo Park61112782013-10-02 19:50:39 -0700842 }
Channagoud Kadabi85c7ec32016-01-28 23:09:21 -0800843
Mayank Grover351a75e2017-05-30 20:06:08 +0530844 if (partition_multislot_is_supported() && have_cmdline)
845 {
846 src = androidboot_slot_suffix;
847 --dst;
848 while ((*dst++ = *src++));
849 --dst;
850 src = SUFFIX_SLOT(current_active_slot);
851 while ((*dst++ = *src++));
Mayank Grover5384ed82018-05-09 12:09:24 +0530852 }
Mayank Grover351a75e2017-05-30 20:06:08 +0530853
Mayank Grover351a75e2017-05-30 20:06:08 +0530854
Mayank Grover5384ed82018-05-09 12:09:24 +0530855 /*
856 * System-As-Root behaviour, system.img should contain both
857 * system content and ramdisk content, and should be mounted at
858 * root(a/b).
859 * Apending skip_ramfs for non a/b builds which use, system as root.
860 */
861 if ((target_uses_system_as_root() ||
862 partition_multislot_is_supported()) &&
863 have_cmdline)
864 {
865 if (!boot_into_recovery)
866 {
867 src = skip_ramfs;
Mayank Grover351a75e2017-05-30 20:06:08 +0530868 --dst;
869 while ((*dst++ = *src++));
Mayank Grover5384ed82018-05-09 12:09:24 +0530870 }
871
872 src = sys_path_cmdline;
873 --dst;
874 while ((*dst++ = *src++));
Mayank Grover3804be72017-06-22 11:59:23 +0530875
Monika Singh292b3e92018-03-17 22:40:23 +0530876#ifndef VERIFIED_BOOT_2
Mayank Grover5384ed82018-05-09 12:09:24 +0530877 src = syspath_buf;
878 --dst;
879 while ((*dst++ = *src++));
Monika Singh292b3e92018-03-17 22:40:23 +0530880#endif
Mayank Grover351a75e2017-05-30 20:06:08 +0530881 }
882
Mayank Grover466d6562018-05-10 14:52:20 +0530883#if HIBERNATION_SUPPORT
884 if (swap_ptn_index != INVALID_PTN)
885 {
886 src = resume_buf;
887 --dst;
888 while ((*dst++ = *src++));
889 }
890#endif
891
Channagoud Kadabi85c7ec32016-01-28 23:09:21 -0800892#if TARGET_CMDLINE_SUPPORT
893 if (target_cmdline_buf && target_cmd_line_len)
894 {
895 if (have_cmdline) --dst;
896 src = target_cmdline_buf;
897 while((*dst++ = *src++));
898 free(target_cmdline_buf);
899 }
900#endif
Neeti Desaie245d492012-06-01 12:52:13 -0700901 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700902
903
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700904 if (boot_dev_buf)
905 free(boot_dev_buf);
906
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -0800907 if (cmdline_final)
908 dprintf(INFO, "cmdline: %s\n", cmdline_final);
909 else
910 dprintf(INFO, "cmdline is NULL\n");
Neeti Desaie245d492012-06-01 12:52:13 -0700911 return cmdline_final;
912}
913
914unsigned *atag_core(unsigned *ptr)
915{
916 /* CORE */
917 *ptr++ = 2;
918 *ptr++ = 0x54410001;
919
920 return ptr;
921
922}
923
924unsigned *atag_ramdisk(unsigned *ptr, void *ramdisk,
925 unsigned ramdisk_size)
926{
927 if (ramdisk_size) {
928 *ptr++ = 4;
929 *ptr++ = 0x54420005;
930 *ptr++ = (unsigned)ramdisk;
931 *ptr++ = ramdisk_size;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800932 }
933
Neeti Desaie245d492012-06-01 12:52:13 -0700934 return ptr;
935}
936
937unsigned *atag_ptable(unsigned **ptr_addr)
938{
939 int i;
940 struct ptable *ptable;
941
942 if ((ptable = flash_get_ptable()) && (ptable->count != 0)) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700943 *(*ptr_addr)++ = 2 + (ptable->count * (sizeof(struct atag_ptbl_entry) /
944 sizeof(unsigned)));
Neeti Desaie245d492012-06-01 12:52:13 -0700945 *(*ptr_addr)++ = 0x4d534d70;
946 for (i = 0; i < ptable->count; ++i)
947 ptentry_to_tag(ptr_addr, ptable_get(ptable, i));
948 }
949
950 return (*ptr_addr);
951}
952
953unsigned *atag_cmdline(unsigned *ptr, const char *cmdline)
954{
955 int cmdline_length = 0;
956 int n;
Neeti Desaie245d492012-06-01 12:52:13 -0700957 char *dest;
958
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800959 cmdline_length = strlen((const char*)cmdline);
Neeti Desaie245d492012-06-01 12:52:13 -0700960 n = (cmdline_length + 4) & (~3);
961
962 *ptr++ = (n / 4) + 2;
963 *ptr++ = 0x54410009;
964 dest = (char *) ptr;
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800965 while ((*dest++ = *cmdline++));
Neeti Desaie245d492012-06-01 12:52:13 -0700966 ptr += (n / 4);
967
968 return ptr;
969}
970
971unsigned *atag_end(unsigned *ptr)
972{
Brian Swetland9c4c0752009-01-25 16:23:50 -0800973 /* END */
974 *ptr++ = 0;
975 *ptr++ = 0;
976
Neeti Desaie245d492012-06-01 12:52:13 -0700977 return ptr;
978}
979
980void generate_atags(unsigned *ptr, const char *cmdline,
981 void *ramdisk, unsigned ramdisk_size)
982{
vijay kumar21a37452015-12-29 16:23:21 +0530983 unsigned *orig_ptr = ptr;
Neeti Desaie245d492012-06-01 12:52:13 -0700984 ptr = atag_core(ptr);
985 ptr = atag_ramdisk(ptr, ramdisk, ramdisk_size);
986 ptr = target_atag_mem(ptr);
987
988 /* Skip NAND partition ATAGS for eMMC boot */
989 if (!target_is_emmc_boot()){
990 ptr = atag_ptable(&ptr);
991 }
992
vijay kumar21a37452015-12-29 16:23:21 +0530993 /*
994 * Atags size filled till + cmdline size + 1 unsigned for 4-byte boundary + 4 unsigned
995 * for atag identifier in atag_cmdline and atag_end should be with in MAX_TAGS_SIZE bytes
996 */
Mayank Groverbc8a2ef2018-02-23 18:03:36 +0530997 if (!cmdline)
998 return;
999
vijay kumar21a37452015-12-29 16:23:21 +05301000 if (((ptr - orig_ptr) + strlen(cmdline) + 5 * sizeof(unsigned)) < MAX_TAGS_SIZE) {
1001 ptr = atag_cmdline(ptr, cmdline);
1002 ptr = atag_end(ptr);
1003 }
1004 else {
1005 dprintf(CRITICAL,"Crossing ATAGs Max size allowed\n");
1006 ASSERT(0);
1007 }
Neeti Desaie245d492012-06-01 12:52:13 -07001008}
1009
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001010typedef void entry_func_ptr(unsigned, unsigned, unsigned*);
Neeti Desaie245d492012-06-01 12:52:13 -07001011void boot_linux(void *kernel, unsigned *tags,
1012 const char *cmdline, unsigned machtype,
1013 void *ramdisk, unsigned ramdisk_size)
1014{
Amol Jadi10c7d1c2013-01-25 13:24:29 -08001015 unsigned char *final_cmdline;
Amol Jadib6be5c12012-11-14 13:39:51 -08001016#if DEVICE_TREE
Neeti Desai17379b82012-06-04 18:42:53 -07001017 int ret = 0;
Amol Jadib6be5c12012-11-14 13:39:51 -08001018#endif
1019
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001020 void (*entry)(unsigned, unsigned, unsigned*) = (entry_func_ptr*)(PA((addr_t)kernel));
Deepa Dinamani25a9f762012-11-30 15:57:15 -08001021 uint32_t tags_phys = PA((addr_t)tags);
vijay kumar1a50a642015-11-16 12:41:15 +05301022 struct kernel64_hdr *kptr = ((struct kernel64_hdr*)(PA((addr_t)kernel)));
Deepa Dinamani25a9f762012-11-30 15:57:15 -08001023
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301024 ramdisk = (void *)PA((addr_t)ramdisk);
Neeti Desaie245d492012-06-01 12:52:13 -07001025
Amol Jadi10c7d1c2013-01-25 13:24:29 -08001026 final_cmdline = update_cmdline((const char*)cmdline);
1027
Neeti Desai17379b82012-06-04 18:42:53 -07001028#if DEVICE_TREE
Amol Jadib6be5c12012-11-14 13:39:51 -08001029 dprintf(INFO, "Updating device tree: start\n");
1030
Neeti Desai17379b82012-06-04 18:42:53 -07001031 /* Update the Device Tree */
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301032 ret = update_device_tree((void *)tags,(const char *)final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -07001033 if(ret)
1034 {
1035 dprintf(CRITICAL, "ERROR: Updating Device Tree Failed \n");
1036 ASSERT(0);
1037 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001038 dprintf(INFO, "Updating device tree: done\n");
Neeti Desai17379b82012-06-04 18:42:53 -07001039#else
Neeti Desaie245d492012-06-01 12:52:13 -07001040 /* Generating the Atags */
Amol Jadi10c7d1c2013-01-25 13:24:29 -08001041 generate_atags(tags, final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -07001042#endif
Neeti Desaie245d492012-06-01 12:52:13 -07001043
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001044#if VERIFIED_BOOT
Monika Singh292b3e92018-03-17 22:40:23 +05301045 if (VB_M == target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +05301046 {
1047 if (device.verity_mode == 0) {
lijuangbdd9bb42016-03-01 18:22:17 +08001048#if FBCON_DISPLAY_MSG
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07001049#if ENABLE_VB_ATTEST
Mayank Grover889be1b2017-09-12 20:12:23 +05301050 display_bootverify_menu(DISPLAY_MENU_EIO);
1051 wait_for_users_action();
1052 if(!pwr_key_is_pressed)
1053 shutdown_device();
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07001054#else
Mayank Grover889be1b2017-09-12 20:12:23 +05301055 display_bootverify_menu(DISPLAY_MENU_LOGGING);
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07001056#endif
Mayank Grover889be1b2017-09-12 20:12:23 +05301057 wait_for_users_action();
lijuangbdd9bb42016-03-01 18:22:17 +08001058#else
Mayank Grover889be1b2017-09-12 20:12:23 +05301059 dprintf(CRITICAL,
1060 "The dm-verity is not started in enforcing mode.\nWait for 5 seconds before proceeding\n");
1061 mdelay(5000);
lijuangbdd9bb42016-03-01 18:22:17 +08001062#endif
Mayank Grover889be1b2017-09-12 20:12:23 +05301063 }
lijuangbdd9bb42016-03-01 18:22:17 +08001064 }
lijuangbdd9bb42016-03-01 18:22:17 +08001065#endif
1066
1067#if VERIFIED_BOOT
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001068 /* Write protect the device info */
Channagoud Kadabi3bd9d1e2015-05-05 16:18:20 -07001069 if (!boot_into_recovery && target_build_variant_user() && devinfo_present && mmc_write_protect("devinfo", 1))
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001070 {
1071 dprintf(INFO, "Failed to write protect dev info\n");
1072 ASSERT(0);
1073 }
1074#endif
1075
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08001076 /* Turn off splash screen if enabled */
1077#if DISPLAY_SPLASH_SCREEN
1078 target_display_shutdown();
1079#endif
1080
Veera Sundaram Sankaran67ea0932015-09-25 10:09:30 -07001081 /* Perform target specific cleanup */
1082 target_uninit();
Monika Singh292b3e92018-03-17 22:40:23 +05301083 free_verified_boot_resource(&info);
1084 if (final_cmdline)
1085 free(final_cmdline);
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08001086
Deepa Dinamani33734bc2013-03-06 12:16:06 -08001087 dprintf(INFO, "booting linux @ %p, ramdisk @ %p (%d), tags/device tree @ %p\n",
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301088 entry, ramdisk, ramdisk_size, (void *)tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001089
1090 enter_critical_section();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001091
Amol Jadi4421e652011-06-16 15:00:48 -07001092 /* do any platform specific cleanup before kernel entry */
1093 platform_uninit();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001094
Brian Swetland9c4c0752009-01-25 16:23:50 -08001095 arch_disable_cache(UCACHE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001096
Amol Jadi504f9fe2012-08-16 13:56:48 -07001097#if ARM_WITH_MMU
Brian Swetland9c4c0752009-01-25 16:23:50 -08001098 arch_disable_mmu();
Amol Jadi504f9fe2012-08-16 13:56:48 -07001099#endif
Amol Jadi492d5a52013-03-15 16:12:34 -07001100 bs_set_timestamp(BS_KERNEL_ENTRY);
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001101
1102 if (IS_ARM64(kptr))
1103 /* Jump to a 64bit kernel */
1104 scm_elexec_call((paddr_t)kernel, tags_phys);
1105 else
1106 /* Jump to a 32bit kernel */
1107 entry(0, machtype, (unsigned*)tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001108}
1109
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001110/* Function to check if the memory address range falls within the aboot
1111 * boundaries.
1112 * start: Start of the memory region
1113 * size: Size of the memory region
1114 */
Vijay Kumar Pendoti9c002ad2016-03-09 13:52:45 +05301115int check_aboot_addr_range_overlap(uintptr_t start, uint32_t size)
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001116{
1117 /* Check for boundary conditions. */
Sundarajan Srinivasance2a0ea2013-12-16 17:02:56 -08001118 if ((UINT_MAX - start) < size)
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001119 return -1;
1120
1121 /* Check for memory overlap. */
1122 if ((start < MEMBASE) && ((start + size) <= MEMBASE))
1123 return 0;
Channagoud Kadabi94143912013-10-15 12:53:52 -07001124 else if (start >= (MEMBASE + MEMSIZE))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001125 return 0;
1126 else
1127 return -1;
1128}
1129
Mayank Grovere559cec2017-10-17 15:12:03 +05301130/* Function to check if the memory address range falls beyond ddr region.
1131 * start: Start of the memory region
1132 * size: Size of the memory region
1133 */
1134int check_ddr_addr_range_bound(uintptr_t start, uint32_t size)
1135{
1136 uintptr_t ddr_pa_start_addr = PA(get_ddr_start());
1137 uint64_t ddr_size = smem_get_ddr_size();
1138 uint64_t ddr_pa_end_addr = ddr_pa_start_addr + ddr_size;
1139 uintptr_t pa_start_addr = PA(start);
1140
1141 /* Check for boundary conditions. */
1142 if ((UINT_MAX - start) < size)
1143 return -1;
1144
1145 /* Check if memory range is beyond the ddr range. */
1146 if (pa_start_addr < ddr_pa_start_addr ||
1147 pa_start_addr >= (ddr_pa_end_addr) ||
1148 (pa_start_addr + size) > ddr_pa_end_addr)
1149 return -1;
1150 else
1151 return 0;
1152}
1153
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001154BUF_DMA_ALIGN(buf, BOOT_IMG_MAX_PAGE_SIZE); //Equal to max-supported pagesize
Dima Zavin214cc642009-01-26 11:16:21 -08001155
Monika Singh292b3e92018-03-17 22:40:23 +05301156int getimage(const bootinfo *info, void **image_buffer, uint32_t *imgsize,
1157 char *imgname)
1158{
1159 if (info == NULL || image_buffer == NULL || imgsize == NULL ||
1160 imgname == NULL) {
1161 dprintf(CRITICAL, "getimage: invalid parameters\n");
1162 return -1;
1163 }
1164
1165 for (uint32_t loadedindex = 0; loadedindex < info->num_loaded_images; loadedindex++) {
1166 if (!strncmp(info->images[loadedindex].name, imgname,
1167 strlen(imgname))) {
1168 *image_buffer = info->images[loadedindex].image_buffer;
1169 *imgsize = info->images[loadedindex].imgsize;
1170 return 0;
1171 }
1172 }
1173 return -1;
1174}
1175
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001176static void verify_signed_bootimg(uint32_t bootimg_addr, uint32_t bootimg_size)
1177{
1178 int ret;
Channagoud Kadabia8c623f2015-01-13 14:48:48 -08001179
1180#if !VERIFIED_BOOT
Sundarajan Srinivasance54d6e2013-11-11 12:45:00 -08001181#if IMAGE_VERIF_ALGO_SHA1
1182 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA1;
1183#else
1184 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA256;
1185#endif
Channagoud Kadabia8c623f2015-01-13 14:48:48 -08001186#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001187
1188 /* Assume device is rooted at this time. */
1189 device.is_tampered = 1;
1190
1191 dprintf(INFO, "Authenticating boot image (%d): start\n", bootimg_size);
1192
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001193#if VERIFIED_BOOT
Monika Singh7d2fc272018-03-16 17:16:01 +05301194 uint32_t bootstate;
1195 if(boot_into_recovery &&
Mayank Grover68fbf0d2017-10-24 14:13:39 +05301196 (!partition_multislot_is_supported()))
Monika Singh7d2fc272018-03-16 17:16:01 +05301197 {
1198 ret = boot_verify_image((unsigned char *)bootimg_addr,
1199 bootimg_size, "/recovery", &bootstate);
1200 }
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001201 else
Monika Singh7d2fc272018-03-16 17:16:01 +05301202 {
1203 ret = boot_verify_image((unsigned char *)bootimg_addr,
1204 bootimg_size, "/boot", &bootstate);
1205 }
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001206 boot_verify_print_state();
1207#else
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001208 ret = image_verify((unsigned char *)bootimg_addr,
1209 (unsigned char *)(bootimg_addr + bootimg_size),
1210 bootimg_size,
Sundarajan Srinivasance54d6e2013-11-11 12:45:00 -08001211 auth_algo);
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001212#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001213 dprintf(INFO, "Authenticating boot image: done return value = %d\n", ret);
1214
1215 if (ret)
1216 {
1217 /* Authorized kernel */
1218 device.is_tampered = 0;
Sundarajan Srinivasan3fb21f12013-09-16 18:36:15 -07001219 auth_kernel_img = 1;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001220 }
1221
Amit Blay4aa292f2015-04-28 21:55:59 +03001222#ifdef MDTP_SUPPORT
1223 {
1224 /* Verify MDTP lock.
1225 * For boot & recovery partitions, use aboot's verification result.
1226 */
1227 mdtp_ext_partition_verification_t ext_partition;
1228 ext_partition.partition = boot_into_recovery ? MDTP_PARTITION_RECOVERY : MDTP_PARTITION_BOOT;
1229 ext_partition.integrity_state = device.is_tampered ? MDTP_PARTITION_STATE_INVALID : MDTP_PARTITION_STATE_VALID;
1230 ext_partition.page_size = 0; /* Not needed since already validated */
1231 ext_partition.image_addr = 0; /* Not needed since already validated */
1232 ext_partition.image_size = 0; /* Not needed since already validated */
1233 ext_partition.sig_avail = FALSE; /* Not needed since already validated */
1234 mdtp_fwlock_verify_lock(&ext_partition);
1235 }
1236#endif /* MDTP_SUPPORT */
1237
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001238#if USE_PCOM_SECBOOT
1239 set_tamper_flag(device.is_tampered);
1240#endif
1241
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001242#if VERIFIED_BOOT
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001243 switch(boot_verify_get_state())
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001244 {
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001245 case RED:
lijuanga40d6302015-07-20 20:10:13 +08001246#if FBCON_DISPLAY_MSG
lijuang9a7d3b92015-11-30 14:41:24 +08001247 display_bootverify_menu(DISPLAY_MENU_RED);
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07001248#if ENABLE_VB_ATTEST
1249 mdelay(DELAY_WAIT);
1250 shutdown_device();
1251#else
lijuanga40d6302015-07-20 20:10:13 +08001252 wait_for_users_action();
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07001253#endif
lijuanga40d6302015-07-20 20:10:13 +08001254#else
1255 dprintf(CRITICAL,
1256 "Your device has failed verification and may not work properly.\nWait for 5 seconds before proceeding\n");
1257 mdelay(5000);
1258#endif
1259
1260 break;
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001261 case YELLOW:
lijuanga40d6302015-07-20 20:10:13 +08001262#if FBCON_DISPLAY_MSG
lijuang9a7d3b92015-11-30 14:41:24 +08001263 display_bootverify_menu(DISPLAY_MENU_YELLOW);
lijuanga40d6302015-07-20 20:10:13 +08001264 wait_for_users_action();
1265#else
1266 dprintf(CRITICAL,
1267 "Your device has loaded a different operating system.\nWait for 5 seconds before proceeding\n");
1268 mdelay(5000);
1269#endif
1270 break;
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001271 default:
lijuanga40d6302015-07-20 20:10:13 +08001272 break;
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001273 }
1274#endif
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001275#if !VERIFIED_BOOT
Unnati Gandhi1be04752015-03-27 19:41:53 +05301276 if(device.is_tampered)
1277 {
1278 write_device_info_mmc(&device);
1279 #ifdef TZ_TAMPER_FUSE
Monika Singh98257462018-06-06 11:28:49 +05301280 set_tamper_fuse_cmd(HLOS_IMG_TAMPER_FUSE);
Unnati Gandhi1be04752015-03-27 19:41:53 +05301281 #endif
1282 #ifdef ASSERT_ON_TAMPER
1283 dprintf(CRITICAL, "Device is tampered. Asserting..\n");
1284 ASSERT(0);
1285 #endif
1286 }
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001287#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001288}
1289
Monika Singh292b3e92018-03-17 22:40:23 +05301290int get_boot_image_info(void **image_buffer, uint32_t *imgsize,char *imgname)
1291{
1292 if (image_buffer == NULL || imgsize == NULL || imgname == NULL) {
1293 dprintf(CRITICAL, "get_boot_image_info: invalid parameters\n");
1294 return -1;
1295 }
1296
1297 if (!strncmp(info.images[0].name, imgname,
1298 strlen(imgname))) {
1299 *image_buffer = info.images[0].image_buffer;
1300 *imgsize = info.images[0].imgsize;
1301 return 0;
1302 }
1303 return -1;
1304}
1305
Maunik Shah0f3c8ac2014-03-02 17:47:58 +05301306static bool check_format_bit()
1307{
1308 bool ret = false;
1309 int index;
1310 uint64_t offset;
1311 struct boot_selection_info *in = NULL;
1312 char *buf = NULL;
1313
1314 index = partition_get_index("bootselect");
1315 if (index == INVALID_PTN)
1316 {
1317 dprintf(INFO, "Unable to locate /bootselect partition\n");
1318 return ret;
1319 }
1320 offset = partition_get_offset(index);
1321 if(!offset)
1322 {
1323 dprintf(INFO, "partition /bootselect doesn't exist\n");
1324 return ret;
1325 }
1326 buf = (char *) memalign(CACHE_LINE, ROUNDUP(page_size, CACHE_LINE));
Mayank Grover48860402016-11-29 12:34:53 +05301327 mmc_set_lun(partition_get_lun(index));
Maunik Shah0f3c8ac2014-03-02 17:47:58 +05301328 ASSERT(buf);
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301329 if (mmc_read(offset, (uint32_t *)buf, page_size))
Maunik Shah0f3c8ac2014-03-02 17:47:58 +05301330 {
1331 dprintf(INFO, "mmc read failure /bootselect %d\n", page_size);
1332 free(buf);
1333 return ret;
1334 }
1335 in = (struct boot_selection_info *) buf;
1336 if ((in->signature == BOOTSELECT_SIGNATURE) &&
1337 (in->version == BOOTSELECT_VERSION)) {
1338 if ((in->state_info & BOOTSELECT_FORMAT) &&
1339 !(in->state_info & BOOTSELECT_FACTORY))
1340 ret = true;
1341 } else {
1342 dprintf(CRITICAL, "Signature: 0x%08x or version: 0x%08x mismatched of /bootselect\n",
1343 in->signature, in->version);
1344 ASSERT(0);
1345 }
1346 free(buf);
1347 return ret;
1348}
1349
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001350void boot_verifier_init()
1351{
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001352 uint32_t boot_state;
1353 /* Check if device unlock */
1354 if(device.is_unlocked)
1355 {
1356 boot_verify_send_event(DEV_UNLOCK);
1357 boot_verify_print_state();
1358 dprintf(CRITICAL, "Device is unlocked! Skipping verification...\n");
1359 return;
1360 }
1361 else
1362 {
1363 boot_verify_send_event(BOOT_INIT);
1364 }
1365
1366 /* Initialize keystore */
1367 boot_state = boot_verify_keystore_init();
1368 if(boot_state == YELLOW)
1369 {
1370 boot_verify_print_state();
1371 dprintf(CRITICAL, "Keystore verification failed! Continuing anyways...\n");
1372 }
1373}
1374
Shashank Mittal23b8f422010-04-16 19:27:21 -07001375int boot_linux_from_mmc(void)
1376{
1377 struct boot_img_hdr *hdr = (void*) buf;
1378 struct boot_img_hdr *uhdr;
1379 unsigned offset = 0;
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07001380 int rcode;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001381 unsigned long long ptn = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001382 int index = INVALID_PTN;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001383
Shashank Mittalcd98d472011-08-02 14:29:24 -07001384 unsigned char *image_addr = 0;
1385 unsigned kernel_actual;
1386 unsigned ramdisk_actual;
1387 unsigned imagesize_actual;
Neeti Desai465491e2012-07-31 12:53:35 -07001388 unsigned second_actual = 0;
Neeti Desai465491e2012-07-31 12:53:35 -07001389
Matthew Qin271927e2015-03-31 22:07:07 -04001390 unsigned int dtb_size = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08001391 unsigned int out_len = 0;
1392 unsigned int out_avai_len = 0;
1393 unsigned char *out_addr = NULL;
1394 uint32_t dtb_offset = 0;
1395 unsigned char *kernel_start_addr = NULL;
1396 unsigned int kernel_size = 0;
Ameya Thakur10a33452016-06-13 14:24:26 -07001397 unsigned int patched_kernel_hdr_size = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08001398 int rc;
Monika Singh292b3e92018-03-17 22:40:23 +05301399#if VERIFIED_BOOT_2
1400 int status;
1401#endif
Mayank Groverc93cad82017-10-03 12:23:45 +05301402 char *ptn_name = NULL;
Neeti Desai465491e2012-07-31 12:53:35 -07001403#if DEVICE_TREE
1404 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -07001405 struct dt_entry dt_entry;
Neeti Desai465491e2012-07-31 12:53:35 -07001406 unsigned dt_table_offset;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001407 uint32_t dt_actual;
Deepa Dinamani19648b42013-09-05 17:05:55 -07001408 uint32_t dt_hdr_size;
Matthew Qin271927e2015-03-31 22:07:07 -04001409 unsigned char *best_match_dt_addr = NULL;
Neeti Desai465491e2012-07-31 12:53:35 -07001410#endif
Matthew Qin49e51fa2015-02-09 10:40:45 +08001411 struct kernel64_hdr *kptr = NULL;
Mayank Grover351a75e2017-05-30 20:06:08 +05301412 int current_active_slot = INVALID;
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001413
Maunik Shah0f3c8ac2014-03-02 17:47:58 +05301414 if (check_format_bit())
1415 boot_into_recovery = 1;
1416
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07001417 if (!boot_into_recovery) {
1418 memset(ffbm_mode_string, '\0', sizeof(ffbm_mode_string));
1419 rcode = get_ffbm(ffbm_mode_string, sizeof(ffbm_mode_string));
1420 if (rcode <= 0) {
1421 boot_into_ffbm = false;
1422 if (rcode < 0)
1423 dprintf(CRITICAL,"failed to get ffbm cookie");
1424 } else
1425 boot_into_ffbm = true;
1426 } else
1427 boot_into_ffbm = false;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001428 uhdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
1429 if (!memcmp(uhdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1430 dprintf(INFO, "Unified boot method!\n");
1431 hdr = uhdr;
1432 goto unified_boot;
1433 }
Mayank Groverc93cad82017-10-03 12:23:45 +05301434
1435 /* For a/b recovery image code is on boot partition.
1436 If we support multislot, always use boot partition. */
1437 if (boot_into_recovery &&
1438 (!partition_multislot_is_supported()))
1439 ptn_name = "recovery";
1440 else
1441 ptn_name = "boot";
1442
1443 index = partition_get_index(ptn_name);
1444 ptn = partition_get_offset(index);
1445 if(ptn == 0) {
1446 dprintf(CRITICAL, "ERROR: No %s partition found\n", ptn_name);
1447 return -1;
Kinson Chikf1a43512011-07-14 11:28:39 -07001448 }
Mayank Groverc93cad82017-10-03 12:23:45 +05301449
Channagoud Kadabief0547c2015-02-10 12:57:38 -08001450 /* Set Lun for boot & recovery partitions */
1451 mmc_set_lun(partition_get_lun(index));
Shashank Mittal23b8f422010-04-16 19:27:21 -07001452
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301453 if (mmc_read(ptn + offset, (uint32_t *) buf, page_size)) {
Shashank Mittal23b8f422010-04-16 19:27:21 -07001454 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
1455 return -1;
1456 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001457
1458 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001459 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Mayank Grover351a75e2017-05-30 20:06:08 +05301460 return ERR_INVALID_BOOT_MAGIC;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001461 }
1462
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07001463 if (hdr->page_size && (hdr->page_size != page_size)) {
vijay kumar2e21b3a2014-06-26 17:40:15 +05301464
1465 if (hdr->page_size > BOOT_IMG_MAX_PAGE_SIZE) {
1466 dprintf(CRITICAL, "ERROR: Invalid page size\n");
1467 return -1;
1468 }
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07001469 page_size = hdr->page_size;
1470 page_mask = page_size - 1;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001471 }
1472
Matthew Qin49e51fa2015-02-09 10:40:45 +08001473 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1474 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Kishor PKd8ddcad2017-07-27 13:53:57 +05301475 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001476
Matthew Qin49e51fa2015-02-09 10:40:45 +08001477 image_addr = (unsigned char *)target_get_scratch_address();
Kishor PK9e91b592017-05-24 12:14:55 +05301478 memcpy(image_addr, (void *)buf, page_size);
1479
1480 /* ensure commandline is terminated */
1481 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
Matthew Qin49e51fa2015-02-09 10:40:45 +08001482
1483#if DEVICE_TREE
Parth Dixit4097b622016-03-15 14:42:27 +05301484#ifndef OSVERSION_IN_BOOTIMAGE
1485 dt_size = hdr->dt_size;
1486#endif
1487 dt_actual = ROUND_TO_PAGE(dt_size, page_mask);
Kishor PKd8ddcad2017-07-27 13:53:57 +05301488 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 +05301489 dprintf(CRITICAL, "Integer overflow detected in bootimage header fields at %u in %s\n",__LINE__,__FILE__);
1490 return -1;
1491 }
Kishor PKd8ddcad2017-07-27 13:53:57 +05301492 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + second_actual + dt_actual);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001493#else
Kishor PKd8ddcad2017-07-27 13:53:57 +05301494 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 +05301495 dprintf(CRITICAL, "Integer overflow detected in bootimage header fields at %u in %s\n",__LINE__,__FILE__);
1496 return -1;
1497 }
Kishor PKd8ddcad2017-07-27 13:53:57 +05301498 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + second_actual);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001499#endif
1500
1501#if VERIFIED_BOOT
1502 boot_verifier_init();
1503#endif
1504
Vijay Kumar Pendoti9c002ad2016-03-09 13:52:45 +05301505 if (check_aboot_addr_range_overlap((uintptr_t) image_addr, imagesize_actual))
Matthew Qin49e51fa2015-02-09 10:40:45 +08001506 {
1507 dprintf(CRITICAL, "Boot image buffer address overlaps with aboot addresses.\n");
1508 return -1;
1509 }
1510
Matthew Qinbb7923d2015-02-09 10:56:09 +08001511 /*
1512 * Update loading flow of bootimage to support compressed/uncompressed
1513 * bootimage on both 64bit and 32bit platform.
1514 * 1. Load bootimage from emmc partition onto DDR.
1515 * 2. Check if bootimage is gzip format. If yes, decompress compressed kernel
1516 * 3. Check kernel header and update kernel load addr for 64bit and 32bit
1517 * platform accordingly.
1518 * 4. Sanity Check on kernel_addr and ramdisk_addr and copy data.
1519 */
Mayank Grover351a75e2017-05-30 20:06:08 +05301520 if (partition_multislot_is_supported())
1521 {
1522 current_active_slot = partition_find_active_slot();
1523 dprintf(INFO, "Loading boot image (%d) active_slot(%s): start\n",
1524 imagesize_actual, SUFFIX_SLOT(current_active_slot));
1525 }
1526 else
1527 {
1528 dprintf(INFO, "Loading (%s) image (%d): start\n",
1529 (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual);
1530 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08001531 bs_set_timestamp(BS_KERNEL_LOAD_START);
1532
Gaurav Nebhwani43e2a462016-03-17 21:32:56 +05301533 if ((target_get_max_flash_size() - page_size) < imagesize_actual)
1534 {
1535 dprintf(CRITICAL, "booimage size is greater than DDR can hold\n");
1536 return -1;
1537 }
Kishor PK9e91b592017-05-24 12:14:55 +05301538 offset = page_size;
1539 /* Read image without signature and header*/
1540 if (mmc_read(ptn + offset, (void *)(image_addr + offset), imagesize_actual - page_size))
Matthew Qin49e51fa2015-02-09 10:40:45 +08001541 {
1542 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
1543 return -1;
1544 }
1545
Mayank Grover351a75e2017-05-30 20:06:08 +05301546 if (partition_multislot_is_supported())
1547 {
1548 dprintf(INFO, "Loading boot image (%d) active_slot(%s): done\n",
1549 imagesize_actual, SUFFIX_SLOT(current_active_slot));
1550 }
1551 else
1552 {
1553 dprintf(INFO, "Loading (%s) image (%d): done\n",
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001554 (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual);
1555
Mayank Grover351a75e2017-05-30 20:06:08 +05301556 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08001557 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
1558
1559 /* Authenticate Kernel */
1560 dprintf(INFO, "use_signed_kernel=%d, is_unlocked=%d, is_tampered=%d.\n",
1561 (int) target_use_signed_kernel(),
1562 device.is_unlocked,
1563 device.is_tampered);
Monika Singh292b3e92018-03-17 22:40:23 +05301564#if VERIFIED_BOOT_2
1565 offset = imagesize_actual;
1566 if (check_aboot_addr_range_overlap((uintptr_t)image_addr + offset, page_size))
1567 {
1568 dprintf(CRITICAL, "Signature read buffer address overlaps with aboot addresses.\n");
1569 return -1;
1570 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08001571
Monika Singh292b3e92018-03-17 22:40:23 +05301572 /* Read signature */
1573 if(mmc_read(ptn + offset, (void *)(image_addr + offset), page_size))
1574 {
1575 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
1576 return -1;
1577 }
1578
1579 memset(&info, 0, sizeof(bootinfo));
1580 info.images[0].image_buffer = image_addr;
1581 info.images[0].imgsize = imagesize_actual;
1582 info.images[0].name = "boot";
1583 info.num_loaded_images = 0;
1584 info.multi_slot_boot = partition_multislot_is_supported();
1585 info.bootreason_alarm = boot_reason_alarm;
1586 info.bootinto_recovery = boot_into_recovery;
1587 status = load_image_and_auth(&info);
1588 if(status)
1589 return -1;
1590
1591 vbcmdline = info.vbcmdline;
1592#else
Channagoud Kadabi736c4962015-08-21 11:56:52 -07001593 /* Change the condition a little bit to include the test framework support.
1594 * We would never reach this point if device is in fastboot mode, even if we did
1595 * that means we are in test mode, so execute kernel authentication part for the
1596 * tests */
Parth Dixitcb0c6082015-12-30 15:01:13 +05301597 if((target_use_signed_kernel() && (!device.is_unlocked)) || is_test_mode_enabled())
Matthew Qin49e51fa2015-02-09 10:40:45 +08001598 {
1599 offset = imagesize_actual;
Vijay Kumar Pendoti9c002ad2016-03-09 13:52:45 +05301600 if (check_aboot_addr_range_overlap((uintptr_t)image_addr + offset, page_size))
Matthew Qin49e51fa2015-02-09 10:40:45 +08001601 {
1602 dprintf(CRITICAL, "Signature read buffer address overlaps with aboot addresses.\n");
1603 return -1;
1604 }
1605
1606 /* Read signature */
1607 if(mmc_read(ptn + offset, (void *)(image_addr + offset), page_size))
1608 {
1609 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
1610 return -1;
1611 }
1612
1613 verify_signed_bootimg((uint32_t)image_addr, imagesize_actual);
Channagoud Kadabi736c4962015-08-21 11:56:52 -07001614 /* The purpose of our test is done here */
Parth Dixitcb0c6082015-12-30 15:01:13 +05301615 if(is_test_mode_enabled() && auth_kernel_img)
Channagoud Kadabi736c4962015-08-21 11:56:52 -07001616 return 0;
Matthew Qin49e51fa2015-02-09 10:40:45 +08001617 } else {
1618 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
1619 #ifdef TZ_SAVE_KERNEL_HASH
1620 aboot_save_boot_hash_mmc((uint32_t) image_addr, imagesize_actual);
1621 #endif /* TZ_SAVE_KERNEL_HASH */
Amit Blay4aa292f2015-04-28 21:55:59 +03001622
1623#ifdef MDTP_SUPPORT
1624 {
1625 /* Verify MDTP lock.
1626 * For boot & recovery partitions, MDTP will use boot_verifier APIs,
1627 * since verification was skipped in aboot. The signature is not part of the loaded image.
1628 */
1629 mdtp_ext_partition_verification_t ext_partition;
1630 ext_partition.partition = boot_into_recovery ? MDTP_PARTITION_RECOVERY : MDTP_PARTITION_BOOT;
1631 ext_partition.integrity_state = MDTP_PARTITION_STATE_UNSET;
1632 ext_partition.page_size = page_size;
1633 ext_partition.image_addr = (uint32)image_addr;
1634 ext_partition.image_size = imagesize_actual;
1635 ext_partition.sig_avail = FALSE;
1636 mdtp_fwlock_verify_lock(&ext_partition);
1637 }
1638#endif /* MDTP_SUPPORT */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001639 }
Monika Singh292b3e92018-03-17 22:40:23 +05301640#endif
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001641
Sridhar Parasuram00bfedb2015-05-26 14:21:27 -07001642#if VERIFIED_BOOT
taozhang93088bd2016-11-16 15:50:46 +08001643 if((boot_verify_get_state() == ORANGE) && (!boot_into_ffbm))
Reut Zysmanba8a9d52016-02-18 11:44:04 +02001644 {
1645#if FBCON_DISPLAY_MSG
1646 display_bootverify_menu(DISPLAY_MENU_ORANGE);
1647 wait_for_users_action();
1648#else
1649 dprintf(CRITICAL,
1650 "Your device has been unlocked and can't be trusted.\nWait for 5 seconds before proceeding\n");
1651 mdelay(5000);
1652#endif
1653 }
1654#endif
1655
1656#if VERIFIED_BOOT
Monika Singh292b3e92018-03-17 22:40:23 +05301657 if (VB_M == target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +05301658 {
1659 /* set boot and system versions. */
1660 set_os_version((unsigned char *)image_addr);
1661 // send root of trust
1662 if(!send_rot_command((uint32_t)device.is_unlocked))
1663 ASSERT(0);
1664 }
Parth Dixitbc9b6492015-10-18 00:41:38 +05301665#endif
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001666 /*
Matthew Qinbb7923d2015-02-09 10:56:09 +08001667 * Check if the kernel image is a gzip package. If yes, need to decompress it.
1668 * If not, continue booting.
1669 */
1670 if (is_gzip_package((unsigned char *)(image_addr + page_size), hdr->kernel_size))
1671 {
1672 out_addr = (unsigned char *)(image_addr + imagesize_actual + page_size);
1673 out_avai_len = target_get_max_flash_size() - imagesize_actual - page_size;
Matthew Qin0b15b322015-05-19 05:20:54 -04001674 dprintf(INFO, "decompressing kernel image: start\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001675 rc = decompress((unsigned char *)(image_addr + page_size),
1676 hdr->kernel_size, out_addr, out_avai_len,
1677 &dtb_offset, &out_len);
1678 if (rc)
1679 {
Matthew Qin0b15b322015-05-19 05:20:54 -04001680 dprintf(CRITICAL, "decompressing kernel image failed!!!\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001681 ASSERT(0);
1682 }
1683
Matthew Qin0b15b322015-05-19 05:20:54 -04001684 dprintf(INFO, "decompressing kernel image: done\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001685 kptr = (struct kernel64_hdr *)out_addr;
1686 kernel_start_addr = out_addr;
1687 kernel_size = out_len;
1688 } else {
Ameya Thakur10a33452016-06-13 14:24:26 -07001689 dprintf(INFO, "Uncpmpressed kernel in use\n");
1690 if (!strncmp((char*)(image_addr + page_size),
1691 PATCHED_KERNEL_MAGIC,
1692 sizeof(PATCHED_KERNEL_MAGIC) - 1)) {
1693 dprintf(INFO, "Patched kernel detected\n");
1694 kptr = (struct kernel64_hdr *)(image_addr + page_size +
1695 PATCHED_KERNEL_HEADER_SIZE);
1696 //The size of the kernel is stored at start of kernel image + 16
1697 //The dtb would start just after the kernel
1698 dtb_offset = *((uint32_t*)((unsigned char*)
1699 (image_addr + page_size +
1700 sizeof(PATCHED_KERNEL_MAGIC) -
1701 1)));
1702 //The actual kernel starts after the 20 byte header.
1703 kernel_start_addr = (unsigned char*)(image_addr +
1704 page_size + PATCHED_KERNEL_HEADER_SIZE);
1705 kernel_size = hdr->kernel_size;
1706 patched_kernel_hdr_size = PATCHED_KERNEL_HEADER_SIZE;
1707 } else {
1708 dprintf(INFO, "Kernel image not patched..Unable to locate dt offset\n");
1709 kptr = (struct kernel64_hdr *)(image_addr + page_size);
1710 kernel_start_addr = (unsigned char *)(image_addr + page_size);
1711 kernel_size = hdr->kernel_size;
1712 }
Matthew Qinbb7923d2015-02-09 10:56:09 +08001713 }
1714
1715 /*
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001716 * Update the kernel/ramdisk/tags address if the boot image header
1717 * has default values, these default values come from mkbootimg when
1718 * the boot image is flashed using fastboot flash:raw
1719 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001720 update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr));
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001721
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001722 /* Get virtual addresses since the hdr saves physical addresses. */
1723 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
1724 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
1725 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001726
Matthew Qinbb7923d2015-02-09 10:56:09 +08001727 kernel_size = ROUND_TO_PAGE(kernel_size, page_mask);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001728 /* Check if the addresses in the header are valid. */
Matthew Qinbb7923d2015-02-09 10:56:09 +08001729 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_size) ||
Mayank Grovere559cec2017-10-17 15:12:03 +05301730 check_ddr_addr_range_bound(hdr->kernel_addr, kernel_size) ||
1731 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual) ||
1732 check_ddr_addr_range_bound(hdr->ramdisk_addr, ramdisk_actual))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001733 {
Mayank Grovere559cec2017-10-17 15:12:03 +05301734 dprintf(CRITICAL, "kernel/ramdisk addresses are not valid.\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001735 return -1;
1736 }
1737
1738#ifndef DEVICE_TREE
Mayank Grovere559cec2017-10-17 15:12:03 +05301739 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE) ||
1740 check_ddr_addr_range_bound(hdr->tags_addr, MAX_TAGS_SIZE))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001741 {
Mayank Grovere559cec2017-10-17 15:12:03 +05301742 dprintf(CRITICAL, "Tags addresses are not valid.\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001743 return -1;
1744 }
1745#endif
1746
Matthew Qin49e51fa2015-02-09 10:40:45 +08001747 /* Move kernel, ramdisk and device tree to correct address */
Matthew Qinbb7923d2015-02-09 10:56:09 +08001748 memmove((void*) hdr->kernel_addr, kernel_start_addr, kernel_size);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001749 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001750
Matthew Qin49e51fa2015-02-09 10:40:45 +08001751 #if DEVICE_TREE
Parth Dixit4097b622016-03-15 14:42:27 +05301752 if(dt_size) {
Matthew Qin49e51fa2015-02-09 10:40:45 +08001753 dt_table_offset = ((uint32_t)image_addr + page_size + kernel_actual + ramdisk_actual + second_actual);
1754 table = (struct dt_table*) dt_table_offset;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001755
Matthew Qin49e51fa2015-02-09 10:40:45 +08001756 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
1757 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1758 return -1;
1759 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001760
P.V. Phani Kumar82916762016-03-09 09:20:19 +05301761 /* Its Error if, dt_hdr_size (table->num_entries * dt_entry size + Dev_Tree Header)
1762 goes beyound hdr->dt_size*/
Parth Dixit4097b622016-03-15 14:42:27 +05301763 if (dt_hdr_size > ROUND_TO_PAGE(dt_size,hdr->page_size)) {
P.V. Phani Kumar82916762016-03-09 09:20:19 +05301764 dprintf(CRITICAL, "ERROR: Invalid Device Tree size \n");
1765 return -1;
1766 }
1767
Matthew Qin49e51fa2015-02-09 10:40:45 +08001768 /* Find index of device tree within device tree table */
1769 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
1770 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1771 return -1;
1772 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001773
P.V. Phani Kumar3c333202016-03-09 11:54:37 +05301774 if(dt_entry.offset > (UINT_MAX - dt_entry.size)) {
1775 dprintf(CRITICAL, "ERROR: Device tree contents are Invalid\n");
1776 return -1;
1777 }
1778
1779 /* Ensure we are not overshooting dt_size with the dt_entry selected */
Parth Dixit4097b622016-03-15 14:42:27 +05301780 if ((dt_entry.offset + dt_entry.size) > dt_size) {
P.V. Phani Kumar3c333202016-03-09 11:54:37 +05301781 dprintf(CRITICAL, "ERROR: Device tree contents are Invalid\n");
1782 return -1;
1783 }
1784
Matthew Qin271927e2015-03-31 22:07:07 -04001785 if (is_gzip_package((unsigned char *)dt_table_offset + dt_entry.offset, dt_entry.size))
1786 {
1787 unsigned int compressed_size = 0;
1788 out_addr += out_len;
1789 out_avai_len -= out_len;
Matthew Qin0b15b322015-05-19 05:20:54 -04001790 dprintf(INFO, "decompressing dtb: start\n");
Matthew Qin271927e2015-03-31 22:07:07 -04001791 rc = decompress((unsigned char *)dt_table_offset + dt_entry.offset,
1792 dt_entry.size, out_addr, out_avai_len,
1793 &compressed_size, &dtb_size);
1794 if (rc)
1795 {
Matthew Qin0b15b322015-05-19 05:20:54 -04001796 dprintf(CRITICAL, "decompressing dtb failed!!!\n");
Matthew Qin271927e2015-03-31 22:07:07 -04001797 ASSERT(0);
1798 }
1799
Matthew Qin0b15b322015-05-19 05:20:54 -04001800 dprintf(INFO, "decompressing dtb: done\n");
Matthew Qin271927e2015-03-31 22:07:07 -04001801 best_match_dt_addr = out_addr;
1802 } else {
1803 best_match_dt_addr = (unsigned char *)dt_table_offset + dt_entry.offset;
1804 dtb_size = dt_entry.size;
1805 }
1806
Matthew Qin49e51fa2015-02-09 10:40:45 +08001807 /* Validate and Read device device tree in the tags_addr */
Mayank Grovere559cec2017-10-17 15:12:03 +05301808 if (check_aboot_addr_range_overlap(hdr->tags_addr, dtb_size) ||
1809 check_ddr_addr_range_bound(hdr->tags_addr, dtb_size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001810 {
Mayank Grovere559cec2017-10-17 15:12:03 +05301811 dprintf(CRITICAL, "Device tree addresses are not valid\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001812 return -1;
1813 }
Shashank Mittal162244e2011-08-08 19:01:25 -07001814
Matthew Qin271927e2015-03-31 22:07:07 -04001815 memmove((void *)hdr->tags_addr, (char *)best_match_dt_addr, dtb_size);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001816 } else {
1817 /* Validate the tags_addr */
Mayank Grovere559cec2017-10-17 15:12:03 +05301818 if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual) ||
1819 check_ddr_addr_range_bound(hdr->tags_addr, kernel_actual))
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001820 {
Mayank Grovere559cec2017-10-17 15:12:03 +05301821 dprintf(CRITICAL, "Device tree addresses are not valid.\n");
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001822 return -1;
1823 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08001824 /*
1825 * If appended dev tree is found, update the atags with
1826 * memory address to the DTB appended location on RAM.
1827 * Else update with the atags address in the kernel header
1828 */
1829 void *dtb;
Ameya Thakur10a33452016-06-13 14:24:26 -07001830 dtb = dev_tree_appended(
1831 (void*)(image_addr + page_size +
1832 patched_kernel_hdr_size),
1833 hdr->kernel_size, dtb_offset,
1834 (void *)hdr->tags_addr);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001835 if (!dtb) {
1836 dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001837 return -1;
1838 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07001839 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08001840 #endif
Shashank Mittal23b8f422010-04-16 19:27:21 -07001841
Stanimir Varbanov69ec5462013-07-18 18:17:42 +03001842 if (boot_into_recovery && !device.is_unlocked && !device.is_tampered)
1843 target_load_ssd_keystore();
1844
Shashank Mittal23b8f422010-04-16 19:27:21 -07001845unified_boot:
Shashank Mittal23b8f422010-04-16 19:27:21 -07001846
Dima Zavin77e41f32013-03-06 16:10:43 -08001847 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07001848 (const char *)hdr->cmdline, board_machtype(),
Shashank Mittal23b8f422010-04-16 19:27:21 -07001849 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
1850
1851 return 0;
1852}
1853
Dima Zavin214cc642009-01-26 11:16:21 -08001854int boot_linux_from_flash(void)
1855{
1856 struct boot_img_hdr *hdr = (void*) buf;
Dima Zavin214cc642009-01-26 11:16:21 -08001857 struct ptentry *ptn;
1858 struct ptable *ptable;
1859 unsigned offset = 0;
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001860
Shashank Mittalcd98d472011-08-02 14:29:24 -07001861 unsigned char *image_addr = 0;
1862 unsigned kernel_actual;
1863 unsigned ramdisk_actual;
1864 unsigned imagesize_actual;
vijay kumar8f53e362015-11-24 13:38:11 +05301865 unsigned second_actual = 0;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001866
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001867#if DEVICE_TREE
Monika Singh292b3e92018-03-17 22:40:23 +05301868 struct dt_table *table = NULL;
Joel Kingaa335dc2013-06-03 16:11:08 -07001869 struct dt_entry dt_entry;
vijay kumar8f53e362015-11-24 13:38:11 +05301870 unsigned dt_table_offset;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001871 uint32_t dt_actual;
Monika Singh292b3e92018-03-17 22:40:23 +05301872 uint32_t dt_hdr_size = 0;
Vivek Kumar07bd0862018-04-10 14:19:23 +05301873 uint32_t dtb_offset = 0;
vijay kumar8f53e362015-11-24 13:38:11 +05301874 unsigned int dtb_size = 0;
1875 unsigned char *best_match_dt_addr = NULL;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001876#endif
1877
David Ng183a7422009-12-07 14:55:21 -08001878 if (target_is_emmc_boot()) {
1879 hdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
1880 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1881 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
1882 return -1;
1883 }
1884 goto continue_boot;
1885 }
1886
Dima Zavin214cc642009-01-26 11:16:21 -08001887 ptable = flash_get_ptable();
1888 if (ptable == NULL) {
1889 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1890 return -1;
1891 }
1892
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001893 if(!boot_into_recovery)
1894 {
1895 ptn = ptable_find(ptable, "boot");
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001896
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001897 if (ptn == NULL) {
1898 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1899 return -1;
1900 }
1901 }
1902 else
1903 {
1904 ptn = ptable_find(ptable, "recovery");
1905 if (ptn == NULL) {
1906 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
1907 return -1;
1908 }
Dima Zavin214cc642009-01-26 11:16:21 -08001909 }
1910
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05301911 /* Read boot.img header from flash */
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001912 if (flash_read(ptn, offset, buf, page_size)) {
Dima Zavin214cc642009-01-26 11:16:21 -08001913 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
1914 return -1;
1915 }
Dima Zavin214cc642009-01-26 11:16:21 -08001916
1917 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001918 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Dima Zavin214cc642009-01-26 11:16:21 -08001919 return -1;
1920 }
1921
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001922 if (hdr->page_size != page_size) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001923 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 -08001924 return -1;
1925 }
1926
Kishor PK9e91b592017-05-24 12:14:55 +05301927 image_addr = (unsigned char *)target_get_scratch_address();
1928 memcpy(image_addr, (void *)buf, page_size);
vijay kumar287bb542015-09-29 13:01:52 +05301929
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001930 /*
1931 * Update the kernel/ramdisk/tags address if the boot image header
1932 * has default values, these default values come from mkbootimg when
1933 * the boot image is flashed using fastboot flash:raw
1934 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001935 update_ker_tags_rdisk_addr(hdr, false);
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001936
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001937 /* Get virtual addresses since the hdr saves physical addresses. */
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001938 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
1939 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
1940 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
1941
1942 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1943 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Mayank Grover032736f2017-07-14 20:34:51 +05301944 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001945
Kishor PK9e91b592017-05-24 12:14:55 +05301946 /* ensure commandline is terminated */
1947 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
1948
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001949 /* Check if the addresses in the header are valid. */
1950 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
Mayank Grovere559cec2017-10-17 15:12:03 +05301951 check_ddr_addr_range_bound(hdr->kernel_addr, kernel_actual) ||
1952 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual) ||
1953 check_ddr_addr_range_bound(hdr->ramdisk_addr, ramdisk_actual))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001954 {
Mayank Grovere559cec2017-10-17 15:12:03 +05301955 dprintf(CRITICAL, "kernel/ramdisk addresses are not valid.\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001956 return -1;
1957 }
1958
1959#ifndef DEVICE_TREE
Kishor PKd8ddcad2017-07-27 13:53:57 +05301960 if (UINT_MAX < ((uint64_t)kernel_actual + (uint64_t)ramdisk_actual+ (uint64_t)second_actual + page_size)) {
Mayank Grover032736f2017-07-14 20:34:51 +05301961 dprintf(CRITICAL, "Integer overflow detected in bootimage header fields\n");
1962 return -1;
1963 }
Kishor PKd8ddcad2017-07-27 13:53:57 +05301964 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + second_actual);
Mayank Grover032736f2017-07-14 20:34:51 +05301965
Mayank Grovere559cec2017-10-17 15:12:03 +05301966 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE) ||
1967 check_ddr_addr_range_bound(hdr->tags_addr, MAX_TAGS_SIZE))
Vijay Kumar Pendotid3ed20e2016-09-20 00:34:46 +05301968 {
Mayank Grovere559cec2017-10-17 15:12:03 +05301969 dprintf(CRITICAL, "Tags addresses are not valid.\n");
Vijay Kumar Pendotid3ed20e2016-09-20 00:34:46 +05301970 return -1;
1971 }
1972#else
1973
1974#ifndef OSVERSION_IN_BOOTIMAGE
1975 dt_size = hdr->dt_size;
1976#endif
Mayank Grover032736f2017-07-14 20:34:51 +05301977 dt_actual = ROUND_TO_PAGE(dt_size, page_mask);
Kishor PKd8ddcad2017-07-27 13:53:57 +05301978 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 +05301979 dprintf(CRITICAL, "Integer overflow detected in bootimage header fields\n");
1980 return -1;
1981 }
1982
Kishor PKd8ddcad2017-07-27 13:53:57 +05301983 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + second_actual + dt_actual);
Mayank Grover032736f2017-07-14 20:34:51 +05301984
Mayank Grovere559cec2017-10-17 15:12:03 +05301985 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_size) ||
1986 check_ddr_addr_range_bound(hdr->tags_addr, dt_size))
Mayank Grover032736f2017-07-14 20:34:51 +05301987 {
Mayank Grovere559cec2017-10-17 15:12:03 +05301988 dprintf(CRITICAL, "Device tree addresses are not valid.\n");
Mayank Grover032736f2017-07-14 20:34:51 +05301989 return -1;
1990 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001991#endif
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001992
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05301993 /* Read full boot.img from flash */
1994 dprintf(INFO, "Loading (%s) image (%d): start\n",
1995 (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual);
1996 bs_set_timestamp(BS_KERNEL_LOAD_START);
1997
1998 if (UINT_MAX - page_size < imagesize_actual)
1999 {
2000 dprintf(CRITICAL,"Integer overflow detected in bootimage header fields %u %s\n", __LINE__,__func__);
2001 return -1;
2002 }
2003
2004 /*Check the availability of RAM before reading boot image + max signature length from flash*/
2005 if (target_get_max_flash_size() < (imagesize_actual + page_size))
2006 {
2007 dprintf(CRITICAL, "bootimage size is greater than DDR can hold\n");
2008 return -1;
2009 }
2010
2011 offset = page_size;
2012 /* Read image without signature and header */
2013 if (flash_read(ptn, offset, (void *)(image_addr + offset), imagesize_actual - page_size))
2014 {
2015 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
2016 return -1;
2017 }
2018
2019 dprintf(INFO, "Loading (%s) image (%d): done\n",
2020 (!boot_into_recovery ? "boot" : "recovery"), imagesize_actual);
2021 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
2022
Shashank Mittalcd98d472011-08-02 14:29:24 -07002023 /* Authenticate Kernel */
Deepa Dinamani23b60d42013-06-24 18:10:52 -07002024 if(target_use_signed_kernel() && (!device.is_unlocked))
Shashank Mittalcd98d472011-08-02 14:29:24 -07002025 {
Shashank Mittalcd98d472011-08-02 14:29:24 -07002026 offset = imagesize_actual;
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05302027
Shashank Mittalcd98d472011-08-02 14:29:24 -07002028 /* Read signature */
2029 if (flash_read(ptn, offset, (void *)(image_addr + offset), page_size))
2030 {
2031 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002032 return -1;
Shashank Mittalcd98d472011-08-02 14:29:24 -07002033 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07002034
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05302035 verify_signed_bootimg((uint32_t)image_addr, imagesize_actual);
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05302036 }
2037 offset = page_size;
2038 if(hdr->second_size != 0) {
2039 if (UINT_MAX - offset < second_actual)
2040 {
2041 dprintf(CRITICAL, "ERROR: Integer overflow in boot image header %s\t%d\n",__func__,__LINE__);
2042 return -1;
vijay kumar8f53e362015-11-24 13:38:11 +05302043 }
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05302044 offset += second_actual;
2045 /* Second image loading not implemented. */
2046 ASSERT(0);
2047 }
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05302048 /* Move kernel and ramdisk to correct address */
2049 memmove((void*) hdr->kernel_addr, (char*) (image_addr + page_size), hdr->kernel_size);
2050 memmove((void*) hdr->ramdisk_addr, (char*) (image_addr + page_size + kernel_actual), hdr->ramdisk_size);
2051
2052#if DEVICE_TREE
2053 if(dt_size != 0) {
2054
2055 dt_table_offset = ((uint32_t)image_addr + page_size + kernel_actual + ramdisk_actual + second_actual);
2056
2057 table = (struct dt_table*) dt_table_offset;
2058
2059 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
2060 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
2061 return -1;
2062 }
2063
2064 /* Its Error if, dt_hdr_size (table->num_entries * dt_entry size + Dev_Tree Header)
2065 goes beyound hdr->dt_size*/
2066 if (dt_hdr_size > ROUND_TO_PAGE(dt_size,hdr->page_size)) {
2067 dprintf(CRITICAL, "ERROR: Invalid Device Tree size \n");
2068 return -1;
2069 }
2070
2071 /* Find index of device tree within device tree table */
2072 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
2073 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
2074 return -1;
2075 }
2076
2077 /* Validate and Read device device tree in the "tags_add */
2078 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size) ||
2079 check_ddr_addr_range_bound(hdr->tags_addr, dt_entry.size))
2080 {
2081 dprintf(CRITICAL, "Device tree addresses are not valid.\n");
2082 return -1;
2083 }
2084
2085 if(dt_entry.offset > (UINT_MAX - dt_entry.size)) {
2086 dprintf(CRITICAL, "ERROR: Device tree contents are Invalid\n");
2087 return -1;
2088 }
2089
2090 /* Ensure we are not overshooting dt_size with the dt_entry selected */
2091 if ((dt_entry.offset + dt_entry.size) > dt_size) {
2092 dprintf(CRITICAL, "ERROR: Device tree contents are Invalid\n");
2093 return -1;
2094 }
2095
2096 best_match_dt_addr = (unsigned char *)table + dt_entry.offset;
2097 dtb_size = dt_entry.size;
2098 memmove((void *)hdr->tags_addr, (char *)best_match_dt_addr, dtb_size);
Vivek Kumar07bd0862018-04-10 14:19:23 +05302099
2100 } else {
2101 /* Validate the tags_addr */
2102 if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual) ||
2103 check_ddr_addr_range_bound(hdr->tags_addr, kernel_actual))
2104 {
2105 dprintf(CRITICAL, "Device tree addresses are not valid.\n");
2106 return -1;
2107 }
2108 /*
2109 * If appended dev tree is found, update the atags with
2110 * memory address to the DTB appended location on RAM.
2111 * Else update with the atags address in the kernel header
2112 */
2113 void *dtb = NULL;
2114 dtb = dev_tree_appended((void*)(image_addr + page_size ),hdr->kernel_size, dtb_offset, (void *)hdr->tags_addr);
2115 if (!dtb) {
2116 dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
2117 return -1;
2118 }
2119 }
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05302120#endif
2121 if(target_use_signed_kernel() && (!device.is_unlocked))
2122 {
Shashank Mittal162244e2011-08-08 19:01:25 -07002123 /* Make sure everything from scratch address is read before next step!*/
Shashank Mittala0032282011-08-26 14:50:11 -07002124 if(device.is_tampered)
Shashank Mittal162244e2011-08-08 19:01:25 -07002125 {
2126 write_device_info_flash(&device);
2127 }
Channagoud Kadabi5c86fe32012-02-16 10:58:48 +05302128#if USE_PCOM_SECBOOT
2129 set_tamper_flag(device.is_tampered);
2130#endif
Shashank Mittalcd98d472011-08-02 14:29:24 -07002131 }
David Ng183a7422009-12-07 14:55:21 -08002132continue_boot:
Dima Zavin214cc642009-01-26 11:16:21 -08002133
Dima Zavin214cc642009-01-26 11:16:21 -08002134 /* TODO: create/pass atags to kernel */
2135
Ajay Dudanie28a6072011-07-01 13:59:46 -07002136 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07002137 (const char *)hdr->cmdline, board_machtype(),
Dima Zavin214cc642009-01-26 11:16:21 -08002138 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
2139
2140 return 0;
2141}
Brian Swetland9c4c0752009-01-25 16:23:50 -08002142
Shashank Mittal162244e2011-08-08 19:01:25 -07002143void write_device_info_mmc(device_info *dev)
2144{
Shashank Mittal162244e2011-08-08 19:01:25 -07002145 unsigned long long ptn = 0;
2146 unsigned long long size;
2147 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002148 uint8_t lun = 0;
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002149 uint32_t ret = 0;
Mayank Groverddd348d2018-01-23 14:07:09 +05302150 uint32_t device_info_sz = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07002151
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002152 if (devinfo_present)
2153 index = partition_get_index("devinfo");
2154 else
2155 index = partition_get_index("aboot");
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07002156
Shashank Mittal162244e2011-08-08 19:01:25 -07002157 ptn = partition_get_offset(index);
2158 if(ptn == 0)
2159 {
2160 return;
2161 }
2162
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002163 lun = partition_get_lun(index);
2164 mmc_set_lun(lun);
2165
Shashank Mittal162244e2011-08-08 19:01:25 -07002166 size = partition_get_size(index);
2167
Mayank Groverddd348d2018-01-23 14:07:09 +05302168 device_info_sz = ROUND_TO_PAGE(sizeof(struct device_info),
2169 mmc_blocksize_mask);
2170 if (device_info_sz == UINT_MAX)
2171 {
2172 dprintf(CRITICAL, "ERROR: Incorrect blocksize of card\n");
2173 return;
2174 }
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07002175
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002176 if (devinfo_present)
Mayank Groverddd348d2018-01-23 14:07:09 +05302177 ret = mmc_write(ptn, device_info_sz, (void *)info_buf);
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002178 else
Mayank Groverddd348d2018-01-23 14:07:09 +05302179 ret = mmc_write((ptn + size - device_info_sz), device_info_sz, (void *)info_buf);
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002180 if (ret)
Shashank Mittal162244e2011-08-08 19:01:25 -07002181 {
2182 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
Sridhar Parasurame1a97922015-07-27 15:31:22 -07002183 ASSERT(0);
Shashank Mittal162244e2011-08-08 19:01:25 -07002184 }
2185}
2186
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002187void read_device_info_mmc(struct device_info *info)
Shashank Mittal162244e2011-08-08 19:01:25 -07002188{
Shashank Mittal162244e2011-08-08 19:01:25 -07002189 unsigned long long ptn = 0;
2190 unsigned long long size;
2191 int index = INVALID_PTN;
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002192 uint32_t ret = 0;
Mayank Groverddd348d2018-01-23 14:07:09 +05302193 uint32_t device_info_sz = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07002194
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002195 if ((index = partition_get_index("devinfo")) < 0)
2196 {
2197 devinfo_present = false;
2198 index = partition_get_index("aboot");
2199 }
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07002200
Shashank Mittal162244e2011-08-08 19:01:25 -07002201 ptn = partition_get_offset(index);
2202 if(ptn == 0)
2203 {
2204 return;
2205 }
2206
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07002207 mmc_set_lun(partition_get_lun(index));
2208
Shashank Mittal162244e2011-08-08 19:01:25 -07002209 size = partition_get_size(index);
2210
Mayank Groverddd348d2018-01-23 14:07:09 +05302211 device_info_sz = ROUND_TO_PAGE(sizeof(struct device_info),
2212 mmc_blocksize_mask);
2213 if (device_info_sz == UINT_MAX)
2214 {
2215 dprintf(CRITICAL, "ERROR: Incorrect blocksize of card\n");
2216 return;
2217 }
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07002218
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002219 if (devinfo_present)
Mayank Groverddd348d2018-01-23 14:07:09 +05302220 ret = mmc_read(ptn, (void *)info_buf, device_info_sz);
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002221 else
Mayank Groverddd348d2018-01-23 14:07:09 +05302222 ret = mmc_read((ptn + size - device_info_sz), (void *)info_buf, device_info_sz);
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002223 if (ret)
Shashank Mittal162244e2011-08-08 19:01:25 -07002224 {
2225 dprintf(CRITICAL, "ERROR: Cannot read device info\n");
Sridhar Parasurame1a97922015-07-27 15:31:22 -07002226 ASSERT(0);
Shashank Mittal162244e2011-08-08 19:01:25 -07002227 }
Shashank Mittal162244e2011-08-08 19:01:25 -07002228}
2229
2230void write_device_info_flash(device_info *dev)
2231{
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002232 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
Shashank Mittal162244e2011-08-08 19:01:25 -07002233 struct ptentry *ptn;
2234 struct ptable *ptable;
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002235 if(info == NULL)
2236 {
2237 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
2238 ASSERT(0);
2239 }
2240 info_buf = info;
Shashank Mittal162244e2011-08-08 19:01:25 -07002241 ptable = flash_get_ptable();
2242 if (ptable == NULL)
2243 {
2244 dprintf(CRITICAL, "ERROR: Partition table not found\n");
2245 return;
2246 }
2247
2248 ptn = ptable_find(ptable, "devinfo");
2249 if (ptn == NULL)
2250 {
Smita Ghosh670c8b82015-05-07 09:30:03 -07002251 dprintf(CRITICAL, "ERROR: No devinfo partition found\n");
Shashank Mittal162244e2011-08-08 19:01:25 -07002252 return;
2253 }
2254
Mayank Groverdedbc892017-10-24 13:41:34 +05302255 memset(info, 0, BOOT_IMG_MAX_PAGE_SIZE);
Shashank Mittal162244e2011-08-08 19:01:25 -07002256 memcpy(info, dev, sizeof(device_info));
2257
2258 if (flash_write(ptn, 0, (void *)info_buf, page_size))
2259 {
2260 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
2261 return;
2262 }
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002263 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07002264}
2265
vijay kumarc65876c2015-04-24 13:29:16 +05302266static int read_allow_oem_unlock(device_info *dev)
2267{
vijay kumarc65876c2015-04-24 13:29:16 +05302268 unsigned offset;
2269 int index;
2270 unsigned long long ptn;
2271 unsigned long long ptn_size;
2272 unsigned blocksize = mmc_get_device_blocksize();
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07002273 STACKBUF_DMA_ALIGN(buf, blocksize);
vijay kumarc65876c2015-04-24 13:29:16 +05302274
vijay kumarca2e6812015-07-08 20:28:25 +05302275 index = partition_get_index(frp_ptns[0]);
vijay kumarc65876c2015-04-24 13:29:16 +05302276 if (index == INVALID_PTN)
2277 {
vijay kumarca2e6812015-07-08 20:28:25 +05302278 index = partition_get_index(frp_ptns[1]);
2279 if (index == INVALID_PTN)
2280 {
2281 dprintf(CRITICAL, "Neither '%s' nor '%s' partition found\n", frp_ptns[0],frp_ptns[1]);
2282 return -1;
2283 }
vijay kumarc65876c2015-04-24 13:29:16 +05302284 }
2285
2286 ptn = partition_get_offset(index);
2287 ptn_size = partition_get_size(index);
2288 offset = ptn_size - blocksize;
2289
Mayank Grover48860402016-11-29 12:34:53 +05302290 /* Set Lun for partition */
2291 mmc_set_lun(partition_get_lun(index));
2292
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07002293 if (mmc_read(ptn + offset, (void *)buf, blocksize))
vijay kumarc65876c2015-04-24 13:29:16 +05302294 {
2295 dprintf(CRITICAL, "Reading MMC failed\n");
2296 return -1;
2297 }
2298
2299 /*is_allow_unlock is a bool value stored at the LSB of last byte*/
2300 is_allow_unlock = buf[blocksize-1] & 0x01;
2301 return 0;
2302}
2303
2304static int write_allow_oem_unlock(bool allow_unlock)
2305{
vijay kumarc65876c2015-04-24 13:29:16 +05302306 unsigned offset;
vijay kumarc65876c2015-04-24 13:29:16 +05302307 int index;
2308 unsigned long long ptn;
2309 unsigned long long ptn_size;
2310 unsigned blocksize = mmc_get_device_blocksize();
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07002311 STACKBUF_DMA_ALIGN(buf, blocksize);
vijay kumarc65876c2015-04-24 13:29:16 +05302312
vijay kumarca2e6812015-07-08 20:28:25 +05302313 index = partition_get_index(frp_ptns[0]);
vijay kumarc65876c2015-04-24 13:29:16 +05302314 if (index == INVALID_PTN)
2315 {
vijay kumarca2e6812015-07-08 20:28:25 +05302316 index = partition_get_index(frp_ptns[1]);
2317 if (index == INVALID_PTN)
2318 {
2319 dprintf(CRITICAL, "Neither '%s' nor '%s' partition found\n", frp_ptns[0],frp_ptns[1]);
2320 return -1;
2321 }
vijay kumarc65876c2015-04-24 13:29:16 +05302322 }
2323
2324 ptn = partition_get_offset(index);
2325 ptn_size = partition_get_size(index);
2326 offset = ptn_size - blocksize;
Mayank Grover48860402016-11-29 12:34:53 +05302327 mmc_set_lun(partition_get_lun(index));
vijay kumarc65876c2015-04-24 13:29:16 +05302328
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07002329 if (mmc_read(ptn + offset, (void *)buf, blocksize))
vijay kumarc65876c2015-04-24 13:29:16 +05302330 {
2331 dprintf(CRITICAL, "Reading MMC failed\n");
2332 return -1;
2333 }
2334
2335 /*is_allow_unlock is a bool value stored at the LSB of last byte*/
2336 buf[blocksize-1] = allow_unlock;
2337 if (mmc_write(ptn + offset, blocksize, buf))
2338 {
2339 dprintf(CRITICAL, "Writing MMC failed\n");
2340 return -1;
2341 }
2342
2343 return 0;
2344}
2345
Shashank Mittal162244e2011-08-08 19:01:25 -07002346void read_device_info_flash(device_info *dev)
2347{
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002348 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
Shashank Mittal162244e2011-08-08 19:01:25 -07002349 struct ptentry *ptn;
2350 struct ptable *ptable;
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002351 if(info == NULL)
2352 {
2353 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
2354 ASSERT(0);
2355 }
2356 info_buf = info;
Shashank Mittal162244e2011-08-08 19:01:25 -07002357 ptable = flash_get_ptable();
2358 if (ptable == NULL)
2359 {
2360 dprintf(CRITICAL, "ERROR: Partition table not found\n");
2361 return;
2362 }
2363
2364 ptn = ptable_find(ptable, "devinfo");
2365 if (ptn == NULL)
2366 {
Smita Ghosh670c8b82015-05-07 09:30:03 -07002367 dprintf(CRITICAL, "ERROR: No devinfo partition found\n");
Shashank Mittal162244e2011-08-08 19:01:25 -07002368 return;
2369 }
2370
2371 if (flash_read(ptn, 0, (void *)info_buf, page_size))
2372 {
2373 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
2374 return;
2375 }
2376
2377 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
2378 {
Shashank Mittal162244e2011-08-08 19:01:25 -07002379 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
2380 info->is_unlocked = 0;
Shashank Mittala0032282011-08-26 14:50:11 -07002381 info->is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07002382 write_device_info_flash(info);
2383 }
2384 memcpy(dev, info, sizeof(device_info));
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002385 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07002386}
2387
2388void write_device_info(device_info *dev)
2389{
2390 if(target_is_emmc_boot())
2391 {
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002392 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
2393 if(info == NULL)
2394 {
2395 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
2396 ASSERT(0);
2397 }
2398 info_buf = info;
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002399 memcpy(info, dev, sizeof(struct device_info));
2400
2401#if USE_RPMB_FOR_DEVINFO
Mayank Grover912eaa62017-10-26 12:08:53 +05302402 if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05302403 is_secure_boot_enable()) {
2404 if((write_device_info_rpmb((void*) info, PAGE_SIZE)) < 0)
2405 ASSERT(0);
Sridhar Parasurame1a97922015-07-27 15:31:22 -07002406 }
Channagoud Kadabic80cb492015-04-28 16:08:28 -07002407 else
2408 write_device_info_mmc(info);
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002409#else
2410 write_device_info_mmc(info);
2411#endif
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002412 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07002413 }
2414 else
2415 {
2416 write_device_info_flash(dev);
2417 }
2418}
2419
Monika Singh94316462018-03-15 18:39:01 +05302420int read_rollback_index(uint32_t loc, uint64_t *roll_back_index)
2421{
2422 if (!devinfo_present) {
2423 dprintf(CRITICAL, "DeviceInfo not initalized \n");
2424 return -EINVAL;
2425 }
2426 if (loc >= ARRAY_SIZE(device.rollback_index)) {
2427 dprintf(CRITICAL, "%s() Loc out of range index: %d, array len: %d\n",
2428 __func__, loc, ARRAY_SIZE(device.rollback_index));
2429 ASSERT(0);
2430 }
2431
2432 *roll_back_index = device.rollback_index[loc];
2433 return 0;
2434}
2435
2436int write_rollback_index(uint32_t loc, uint64_t roll_back_index)
2437{
2438 if (!devinfo_present) {
2439 dprintf(CRITICAL, "DeviceInfo not initalized \n");
2440 return -EINVAL;
2441 }
2442 if (loc >= ARRAY_SIZE(device.rollback_index)) {
2443 dprintf(CRITICAL, "%s() Loc out of range index: %d, array len: %d\n",
2444 __func__, loc, ARRAY_SIZE(device.rollback_index));
2445 ASSERT(0);
2446 }
2447
2448 device.rollback_index[loc] = roll_back_index;
2449 write_device_info(&device);
2450 return 0;
2451}
2452
Monika Singhb0fad822018-03-15 18:50:55 +05302453int store_userkey(uint8_t *user_key, uint32_t user_key_size)
2454{
2455 if (!devinfo_present) {
2456 dprintf(CRITICAL, "DeviceInfo not initalized \n");
2457 return -EINVAL;
2458 }
2459
2460 if (user_key_size > ARRAY_SIZE(device.user_public_key)) {
2461 dprintf(CRITICAL, "StoreUserKey, UserKeySize too large!\n");
2462 return -ENODEV;
2463 }
2464
2465 memcpy(device.user_public_key, user_key, user_key_size);
2466 device.user_public_key_length = user_key_size;
2467 write_device_info(&device);
2468 return 0;
2469}
2470
2471int erase_userkey()
2472{
2473 if (!devinfo_present) {
2474 dprintf(CRITICAL, "DeviceInfo not initalized \n");
2475 return -EINVAL;
2476 }
2477 memset(device.user_public_key, 0, ARRAY_SIZE(device.user_public_key));
2478 device.user_public_key_length = 0;
2479 write_device_info(&device);
2480 return 0;
2481}
2482
2483int get_userkey(uint8_t **user_key, uint32_t *user_key_size)
2484{
2485 if (!devinfo_present) {
2486 dprintf(CRITICAL, "DeviceInfo not initalized \n");
2487 return -EINVAL;
2488 }
2489 *user_key = device.user_public_key;
2490 *user_key_size = device.user_public_key_length;
2491 return 0;
2492}
2493
Shashank Mittal162244e2011-08-08 19:01:25 -07002494void read_device_info(device_info *dev)
2495{
2496 if(target_is_emmc_boot())
2497 {
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002498 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
2499 if(info == NULL)
2500 {
2501 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
2502 ASSERT(0);
2503 }
2504 info_buf = info;
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002505
2506#if USE_RPMB_FOR_DEVINFO
Mayank Grover912eaa62017-10-26 12:08:53 +05302507 if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05302508 is_secure_boot_enable()) {
2509 if((read_device_info_rpmb((void*) info, PAGE_SIZE)) < 0)
2510 ASSERT(0);
Sridhar Parasurame1a97922015-07-27 15:31:22 -07002511 }
Channagoud Kadabic80cb492015-04-28 16:08:28 -07002512 else
2513 read_device_info_mmc(info);
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002514#else
2515 read_device_info_mmc(info);
2516#endif
2517
2518 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
2519 {
2520 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
lijuang511a2b52015-08-14 20:50:51 +08002521 if (is_secure_boot_enable()) {
Channagoud Kadabi05f78ba2015-07-06 11:58:14 -07002522 info->is_unlocked = 0;
Monika Singh292b3e92018-03-17 22:40:23 +05302523#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover912eaa62017-10-26 12:08:53 +05302524 if (VB_M <= target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +05302525 info->is_unlock_critical = 0;
Parth Dixitddbc7352015-10-18 03:13:31 +05302526#endif
lijuang511a2b52015-08-14 20:50:51 +08002527 } else {
Channagoud Kadabi2fda4092015-07-07 13:34:11 -07002528 info->is_unlocked = 1;
Monika Singh292b3e92018-03-17 22:40:23 +05302529#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover912eaa62017-10-26 12:08:53 +05302530 if (VB_M <= target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +05302531 info->is_unlock_critical = 1;
Parth Dixitddbc7352015-10-18 03:13:31 +05302532#endif
lijuang511a2b52015-08-14 20:50:51 +08002533 }
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002534 info->is_tampered = 0;
2535 info->charger_screen_enabled = 0;
Monika Singh292b3e92018-03-17 22:40:23 +05302536#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover912eaa62017-10-26 12:08:53 +05302537 if (VB_M <= target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +05302538 info->verity_mode = 1; //enforcing by default
Parth Dixitddbc7352015-10-18 03:13:31 +05302539#endif
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002540 write_device_info(info);
2541 }
2542 memcpy(dev, info, sizeof(device_info));
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002543 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07002544 }
2545 else
2546 {
2547 read_device_info_flash(dev);
2548 }
2549}
2550
2551void reset_device_info()
2552{
2553 dprintf(ALWAYS, "reset_device_info called.");
Shashank Mittala0032282011-08-26 14:50:11 -07002554 device.is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07002555 write_device_info(&device);
2556}
2557
2558void set_device_root()
2559{
2560 dprintf(ALWAYS, "set_device_root called.");
Shashank Mittala0032282011-08-26 14:50:11 -07002561 device.is_tampered = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -07002562 write_device_info(&device);
2563}
2564
lijuang4ece1e72015-08-14 21:02:36 +08002565/* set device unlock value
2566 * Must check FRP before call this function
2567 * Need to wipe data when unlock status changed
2568 * type 0: oem unlock
2569 * type 1: unlock critical
2570 * status 0: unlock as false
2571 * status 1: lock as true
2572 */
2573void set_device_unlock_value(int type, bool status)
lijuang21f12f52015-08-22 16:22:19 +08002574{
lijuang4ece1e72015-08-14 21:02:36 +08002575 if (type == UNLOCK)
2576 device.is_unlocked = status;
Monika Singh292b3e92018-03-17 22:40:23 +05302577#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover912eaa62017-10-26 12:08:53 +05302578 else if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05302579 type == UNLOCK_CRITICAL)
2580 device.is_unlock_critical = status;
Parth Dixitddbc7352015-10-18 03:13:31 +05302581#endif
lijuang4ece1e72015-08-14 21:02:36 +08002582 write_device_info(&device);
2583}
2584
2585static void set_device_unlock(int type, bool status)
2586{
2587 int is_unlocked = -1;
2588 char response[MAX_RSP_SIZE];
2589
2590 /* check device unlock status if it is as expected */
2591 if (type == UNLOCK)
2592 is_unlocked = device.is_unlocked;
Monika Singh292b3e92018-03-17 22:40:23 +05302593#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover912eaa62017-10-26 12:08:53 +05302594 if(VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05302595 type == UNLOCK_CRITICAL)
2596 {
2597 is_unlocked = device.is_unlock_critical;
2598 }
Parth Dixitddbc7352015-10-18 03:13:31 +05302599#endif
lijuang4ece1e72015-08-14 21:02:36 +08002600 if (is_unlocked == status) {
2601 snprintf(response, sizeof(response), "\tDevice already : %s", (status ? "unlocked!" : "locked!"));
2602 fastboot_info(response);
2603 fastboot_okay("");
2604 return;
2605 }
2606
2607 /* status is true, it means to unlock device */
lijuang07c5d6e2018-04-23 18:32:13 +08002608 if (status && !is_allow_unlock) {
2609 fastboot_fail("oem unlock is not allowed");
2610 return;
2611 }
lijuang21f12f52015-08-22 16:22:19 +08002612
lijuang4ece1e72015-08-14 21:02:36 +08002613#if FBCON_DISPLAY_MSG
lijuang07c5d6e2018-04-23 18:32:13 +08002614 display_unlock_menu(type, status);
2615 fastboot_okay("");
2616 return;
lijuang4ece1e72015-08-14 21:02:36 +08002617#else
lijuang07c5d6e2018-04-23 18:32:13 +08002618 if (status && type == UNLOCK) {
2619 fastboot_fail("Need wipe userdata. Do 'fastboot oem unlock-go'");
2620 return;
lijuang21f12f52015-08-22 16:22:19 +08002621 }
lijuang07c5d6e2018-04-23 18:32:13 +08002622#endif
lijuang4ece1e72015-08-14 21:02:36 +08002623
2624 set_device_unlock_value(type, status);
2625
2626 /* wipe data */
2627 struct recovery_message msg;
Kishor PK60a68212017-05-08 16:55:57 +05302628 memset(&msg, 0, sizeof(msg));
lijuang4ece1e72015-08-14 21:02:36 +08002629 snprintf(msg.recovery, sizeof(msg.recovery), "recovery\n--wipe_data");
2630 write_misc(0, &msg, sizeof(msg));
2631
2632 fastboot_okay("");
2633 reboot_device(RECOVERY_MODE);
lijuang21f12f52015-08-22 16:22:19 +08002634}
2635
lijuang511a2b52015-08-14 20:50:51 +08002636static bool critical_flash_allowed(const char * entry)
2637{
2638 uint32_t i = 0;
2639 if (entry == NULL)
2640 return false;
2641
2642 for (i = 0; i < ARRAY_SIZE(critical_flash_allowed_ptn); i++) {
2643 if(!strcmp(entry, critical_flash_allowed_ptn[i]))
2644 return true;
2645 }
2646 return false;
Matthew Qin271927e2015-03-31 22:07:07 -04002647}
2648
2649#if DEVICE_TREE
Amol Jadicb524072012-08-09 16:40:18 -07002650int copy_dtb(uint8_t *boot_image_start, unsigned int scratch_offset)
2651{
2652 uint32 dt_image_offset = 0;
Amol Jadicb524072012-08-09 16:40:18 -07002653 uint32_t n;
Monika Singh292b3e92018-03-17 22:40:23 +05302654 struct dt_table *table = NULL;
Amol Jadicb524072012-08-09 16:40:18 -07002655 struct dt_entry dt_entry;
Monika Singh292b3e92018-03-17 22:40:23 +05302656 uint32_t dt_hdr_size = 0;
Amol Jadicb524072012-08-09 16:40:18 -07002657 unsigned int compressed_size = 0;
2658 unsigned int dtb_size = 0;
2659 unsigned int out_avai_len = 0;
2660 unsigned char *out_addr = NULL;
2661 unsigned char *best_match_dt_addr = NULL;
2662 int rc;
2663
2664 struct boot_img_hdr *hdr = (struct boot_img_hdr *) (boot_image_start);
2665
Parth Dixit4097b622016-03-15 14:42:27 +05302666#ifndef OSVERSION_IN_BOOTIMAGE
2667 dt_size = hdr->dt_size;
2668#endif
2669
2670 if(dt_size != 0) {
Amol Jadicb524072012-08-09 16:40:18 -07002671 /* add kernel offset */
2672 dt_image_offset += page_size;
2673 n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
2674 dt_image_offset += n;
2675
2676 /* add ramdisk offset */
2677 n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
2678 dt_image_offset += n;
2679
2680 /* add second offset */
2681 if(hdr->second_size != 0) {
2682 n = ROUND_TO_PAGE(hdr->second_size, page_mask);
2683 dt_image_offset += n;
2684 }
2685
2686 /* offset now point to start of dt.img */
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07002687 table = (struct dt_table*)(boot_image_start + dt_image_offset);
Amol Jadicb524072012-08-09 16:40:18 -07002688
Deepa Dinamani19648b42013-09-05 17:05:55 -07002689 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Amol Jadicb524072012-08-09 16:40:18 -07002690 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
2691 return -1;
2692 }
P.V. Phani Kumar82916762016-03-09 09:20:19 +05302693
2694 /* Its Error if, dt_hdr_size (table->num_entries * dt_entry size + Dev_Tree Header)
2695 goes beyound hdr->dt_size*/
Parth Dixit4097b622016-03-15 14:42:27 +05302696 if (dt_hdr_size > ROUND_TO_PAGE(dt_size,hdr->page_size)) {
P.V. Phani Kumar82916762016-03-09 09:20:19 +05302697 dprintf(CRITICAL, "ERROR: Invalid Device Tree size \n");
2698 return -1;
2699 }
2700
Joel Kingaa335dc2013-06-03 16:11:08 -07002701 /* Find index of device tree within device tree table */
2702 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Amol Jadicb524072012-08-09 16:40:18 -07002703 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
2704 return -1;
2705 }
2706
Matthew Qin271927e2015-03-31 22:07:07 -04002707 best_match_dt_addr = (unsigned char *)boot_image_start + dt_image_offset + dt_entry.offset;
2708 if (is_gzip_package(best_match_dt_addr, dt_entry.size))
2709 {
2710 out_addr = (unsigned char *)target_get_scratch_address() + scratch_offset;
2711 out_avai_len = target_get_max_flash_size() - scratch_offset;
Matthew Qin0b15b322015-05-19 05:20:54 -04002712 dprintf(INFO, "decompressing dtb: start\n");
Matthew Qin271927e2015-03-31 22:07:07 -04002713 rc = decompress(best_match_dt_addr,
2714 dt_entry.size, out_addr, out_avai_len,
2715 &compressed_size, &dtb_size);
2716 if (rc)
2717 {
Matthew Qin0b15b322015-05-19 05:20:54 -04002718 dprintf(CRITICAL, "decompressing dtb failed!!!\n");
Matthew Qin271927e2015-03-31 22:07:07 -04002719 ASSERT(0);
2720 }
2721
Matthew Qin0b15b322015-05-19 05:20:54 -04002722 dprintf(INFO, "decompressing dtb: done\n");
Matthew Qin271927e2015-03-31 22:07:07 -04002723 best_match_dt_addr = out_addr;
2724 } else {
2725 dtb_size = dt_entry.size;
2726 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002727 /* Validate and Read device device tree in the "tags_add */
Mayank Grovere559cec2017-10-17 15:12:03 +05302728 if (check_aboot_addr_range_overlap(hdr->tags_addr, dtb_size) ||
2729 check_ddr_addr_range_bound(hdr->tags_addr, dtb_size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002730 {
Mayank Grovere559cec2017-10-17 15:12:03 +05302731 dprintf(CRITICAL, "Device tree addresses are not valid.\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002732 return -1;
2733 }
2734
Amol Jadicb524072012-08-09 16:40:18 -07002735 /* Read device device tree in the "tags_add */
Matthew Qin271927e2015-03-31 22:07:07 -04002736 memmove((void*) hdr->tags_addr, (void *)best_match_dt_addr, dtb_size);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002737 } else
2738 return -1;
Amol Jadicb524072012-08-09 16:40:18 -07002739
2740 /* Everything looks fine. Return success. */
2741 return 0;
2742}
2743#endif
2744
Brian Swetland9c4c0752009-01-25 16:23:50 -08002745void cmd_boot(const char *arg, void *data, unsigned sz)
2746{
2747 unsigned kernel_actual;
2748 unsigned ramdisk_actual;
Kishor PKd8ddcad2017-07-27 13:53:57 +05302749 unsigned second_actual;
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002750 uint32_t image_actual;
2751 uint32_t dt_actual = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08002752 struct boot_img_hdr *hdr = NULL;
2753 struct kernel64_hdr *kptr = NULL;
Brian Swetland9c4c0752009-01-25 16:23:50 -08002754 char *ptr = ((char*) data);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002755 int ret = 0;
2756 uint8_t dtb_copied = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08002757 unsigned int out_len = 0;
2758 unsigned int out_avai_len = 0;
2759 unsigned char *out_addr = NULL;
2760 uint32_t dtb_offset = 0;
2761 unsigned char *kernel_start_addr = NULL;
2762 unsigned int kernel_size = 0;
Matthew Qin271927e2015-03-31 22:07:07 -04002763 unsigned int scratch_offset = 0;
Monika Singh292b3e92018-03-17 22:40:23 +05302764#if !VERIFIED_BOOT_2
2765 uint32_t sig_actual = 0;
2766 uint32_t sig_size = 0;
2767#ifdef MDTP_SUPPORT
2768 static bool is_mdtp_activated = 0;
2769#endif /* MDTP_SUPPORT */
2770#endif
Matthew Qinbb7923d2015-02-09 10:56:09 +08002771
lijuang2008ff22016-03-07 17:56:27 +08002772#if FBCON_DISPLAY_MSG
2773 /* Exit keys' detection thread firstly */
2774 exit_menu_keys_detection();
2775#endif
2776
Monika Singh292b3e92018-03-17 22:40:23 +05302777#if VERIFIED_BOOT || VERIFIED_BOOT_2
Channagoud Kadabi6d5375e2015-06-23 17:15:42 -07002778 if(target_build_variant_user() && !device.is_unlocked)
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002779 {
2780 fastboot_fail("unlock device to use this command");
lijuang2008ff22016-03-07 17:56:27 +08002781 goto boot_failed;
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002782 }
2783#endif
2784
Brian Swetland9c4c0752009-01-25 16:23:50 -08002785 if (sz < sizeof(hdr)) {
2786 fastboot_fail("invalid bootimage header");
lijuang2008ff22016-03-07 17:56:27 +08002787 goto boot_failed;
Brian Swetland9c4c0752009-01-25 16:23:50 -08002788 }
2789
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002790 hdr = (struct boot_img_hdr *)data;
Brian Swetland9c4c0752009-01-25 16:23:50 -08002791
2792 /* ensure commandline is terminated */
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002793 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -08002794
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002795 if(target_is_emmc_boot() && hdr->page_size) {
2796 page_size = hdr->page_size;
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07002797 page_mask = page_size - 1;
2798 }
2799
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002800 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
2801 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Kishor PKd8ddcad2017-07-27 13:53:57 +05302802 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002803#if DEVICE_TREE
Parth Dixit4097b622016-03-15 14:42:27 +05302804#ifndef OSVERSION_IN_BOOTIMAGE
2805 dt_size = hdr->dt_size;
2806#endif
2807 dt_actual = ROUND_TO_PAGE(dt_size, page_mask);
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002808#endif
2809
2810 image_actual = ADD_OF(page_size, kernel_actual);
2811 image_actual = ADD_OF(image_actual, ramdisk_actual);
Kishor PKd8ddcad2017-07-27 13:53:57 +05302812 image_actual = ADD_OF(image_actual, second_actual);
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002813 image_actual = ADD_OF(image_actual, dt_actual);
2814
Kishor PK5134b332017-05-09 17:50:08 +05302815 /* Checking to prevent oob access in read_der_message_length */
2816 if (image_actual > sz) {
2817 fastboot_fail("bootimage header fields are invalid");
2818 goto boot_failed;
2819 }
Monika Singh292b3e92018-03-17 22:40:23 +05302820#if VERIFIED_BOOT_2
2821 memset(&info, 0, sizeof(bootinfo));
2822 info.images[0].image_buffer = data;
2823 info.images[0].imgsize = image_actual;
2824 info.images[0].name = "boot";
2825 info.num_loaded_images = 1;
2826 info.multi_slot_boot = partition_multislot_is_supported();
2827 if (load_image_and_auth(&info))
2828 goto boot_failed;
2829 vbcmdline = info.vbcmdline;
2830#else
Kishor PK5134b332017-05-09 17:50:08 +05302831 sig_size = sz - image_actual;
2832
P.V. Phani Kumareecfd822016-03-09 20:09:03 +05302833 if (target_use_signed_kernel() && (!device.is_unlocked)) {
Monika Singh292b3e92018-03-17 22:40:23 +05302834 unsigned chk;
P.V. Phani Kumareecfd822016-03-09 20:09:03 +05302835 /* Calculate the signature length from boot image */
2836 sig_actual = read_der_message_length(
Kishor PK5134b332017-05-09 17:50:08 +05302837 (unsigned char*)(data + image_actual), sig_size);
Monika Singh292b3e92018-03-17 22:40:23 +05302838 chk = ADD_OF(image_actual, sig_actual);
Channagoud Kadabi3d839012014-06-26 14:26:39 -07002839
Monika Singh292b3e92018-03-17 22:40:23 +05302840 if (chk > sz) {
Kishor PK5134b332017-05-09 17:50:08 +05302841 fastboot_fail("bootimage header fields are invalid");
2842 goto boot_failed;
2843 }
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002844 }
2845
Sridhar Parasuram361e4cd2015-09-24 11:19:19 -07002846 // Initialize boot state before trying to verify boot.img
2847#if VERIFIED_BOOT
Channagoud Kadabi699466e2015-11-03 12:37:42 -08002848 boot_verifier_init();
Mayank Groverb337e932017-01-18 20:00:40 +05302849#endif
Channagoud Kadabibf6ce7d2015-09-17 13:25:35 -07002850 /* Handle overflow if the input image size is greater than
2851 * boot image buffer can hold
2852 */
Monika Singh292b3e92018-03-17 22:40:23 +05302853 if ((target_get_max_flash_size() - page_size) < image_actual)
Channagoud Kadabibf6ce7d2015-09-17 13:25:35 -07002854 {
2855 fastboot_fail("booimage: size is greater than boot image buffer can hold");
lijuang2008ff22016-03-07 17:56:27 +08002856 goto boot_failed;
Channagoud Kadabibf6ce7d2015-09-17 13:25:35 -07002857 }
Sridhar Parasuram361e4cd2015-09-24 11:19:19 -07002858
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002859 /* Verify the boot image
2860 * device & page_size are initialized in aboot_init
2861 */
Sridhar Parasuram7ae9fbc2015-09-17 09:56:30 -07002862 if (target_use_signed_kernel() && (!device.is_unlocked)) {
Channagoud Kadabi3d839012014-06-26 14:26:39 -07002863 /* Pass size excluding signature size, otherwise we would try to
2864 * access signature beyond its length
2865 */
Monika Singh292b3e92018-03-17 22:40:23 +05302866 verify_signed_bootimg((uint32_t)data, image_actual);
Sridhar Parasuram7ae9fbc2015-09-17 09:56:30 -07002867 }
Amit Blay4aa292f2015-04-28 21:55:59 +03002868#ifdef MDTP_SUPPORT
2869 else
2870 {
Shay Nachmani062aa3f2015-05-17 17:28:44 +03002871 /* fastboot boot is not allowed when MDTP is activated */
Amit Blay4aa292f2015-04-28 21:55:59 +03002872 mdtp_ext_partition_verification_t ext_partition;
Amit Blay8a510302015-08-17 09:20:01 +03002873
2874 if (!is_mdtp_activated) {
2875 ext_partition.partition = MDTP_PARTITION_NONE;
2876 mdtp_fwlock_verify_lock(&ext_partition);
2877 }
Amit Blay4aa292f2015-04-28 21:55:59 +03002878 }
Shay Nachmani062aa3f2015-05-17 17:28:44 +03002879
Amir Kotzer7c768c02016-04-13 09:08:05 +03002880 /* If mdtp state cannot be validate, block fastboot boot*/
2881 if(mdtp_activated(&is_mdtp_activated)){
2882 dprintf(CRITICAL, "mdtp_activated cannot validate state.\n");
2883 dprintf(CRITICAL, "Can not proceed with fastboot boot command.\n");
2884 goto boot_failed;
2885 }
Shay Nachmani062aa3f2015-05-17 17:28:44 +03002886 if(is_mdtp_activated){
2887 dprintf(CRITICAL, "fastboot boot command is not available.\n");
lijuang2008ff22016-03-07 17:56:27 +08002888 goto boot_failed;
Shay Nachmani062aa3f2015-05-17 17:28:44 +03002889 }
Amit Blay4aa292f2015-04-28 21:55:59 +03002890#endif /* MDTP_SUPPORT */
Monika Singh292b3e92018-03-17 22:40:23 +05302891#endif /* VERIFIED_BOOT_2 else */
Amit Blay4aa292f2015-04-28 21:55:59 +03002892
Sridhar Parasuram361e4cd2015-09-24 11:19:19 -07002893#if VERIFIED_BOOT
Monika Singh292b3e92018-03-17 22:40:23 +05302894 if (VB_M == target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +05302895 {
2896 /* set boot and system versions. */
2897 set_os_version((unsigned char *)data);
2898 // send root of trust
2899 if(!send_rot_command((uint32_t)device.is_unlocked))
2900 ASSERT(0);
2901 }
Parth Dixitbc9b6492015-10-18 00:41:38 +05302902#endif
Channagoud Kadabia22144f2013-03-20 11:49:01 -07002903 /*
Matthew Qinbb7923d2015-02-09 10:56:09 +08002904 * Check if the kernel image is a gzip package. If yes, need to decompress it.
2905 * If not, continue booting.
2906 */
2907 if (is_gzip_package((unsigned char *)(data + page_size), hdr->kernel_size))
2908 {
2909 out_addr = (unsigned char *)target_get_scratch_address();
2910 out_addr = (unsigned char *)(out_addr + image_actual + page_size);
2911 out_avai_len = target_get_max_flash_size() - image_actual - page_size;
Matthew Qin0b15b322015-05-19 05:20:54 -04002912 dprintf(INFO, "decompressing kernel image: start\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08002913 ret = decompress((unsigned char *)(ptr + page_size),
2914 hdr->kernel_size, out_addr, out_avai_len,
2915 &dtb_offset, &out_len);
2916 if (ret)
2917 {
Matthew Qin0b15b322015-05-19 05:20:54 -04002918 dprintf(CRITICAL, "decompressing image failed!!!\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08002919 ASSERT(0);
2920 }
2921
Matthew Qin0b15b322015-05-19 05:20:54 -04002922 dprintf(INFO, "decompressing kernel image: done\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08002923 kptr = (struct kernel64_hdr *)out_addr;
2924 kernel_start_addr = out_addr;
2925 kernel_size = out_len;
2926 } else {
2927 kptr = (struct kernel64_hdr*)((char *)data + page_size);
2928 kernel_start_addr = (unsigned char *)((char *)data + page_size);
2929 kernel_size = hdr->kernel_size;
2930 }
2931
2932 /*
Channagoud Kadabia22144f2013-03-20 11:49:01 -07002933 * Update the kernel/ramdisk/tags address if the boot image header
2934 * has default values, these default values come from mkbootimg when
2935 * the boot image is flashed using fastboot flash:raw
2936 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08002937 update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr));
Dima Zavin3cadfff2013-03-21 14:30:48 -07002938
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002939 /* Get virtual addresses since the hdr saves physical addresses. */
2940 hdr->kernel_addr = VA(hdr->kernel_addr);
2941 hdr->ramdisk_addr = VA(hdr->ramdisk_addr);
2942 hdr->tags_addr = VA(hdr->tags_addr);
Brian Swetland9c4c0752009-01-25 16:23:50 -08002943
Matthew Qinbb7923d2015-02-09 10:56:09 +08002944 kernel_size = ROUND_TO_PAGE(kernel_size, page_mask);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002945 /* Check if the addresses in the header are valid. */
Matthew Qinbb7923d2015-02-09 10:56:09 +08002946 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_size) ||
Mayank Grovere559cec2017-10-17 15:12:03 +05302947 check_ddr_addr_range_bound(hdr->kernel_addr, kernel_size) ||
2948 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual) ||
2949 check_ddr_addr_range_bound(hdr->ramdisk_addr, ramdisk_actual))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002950 {
Mayank Grovere559cec2017-10-17 15:12:03 +05302951 dprintf(CRITICAL, "kernel/ramdisk addresses are not valid.\n");
lijuang2008ff22016-03-07 17:56:27 +08002952 goto boot_failed;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002953 }
2954
Amol Jadicb524072012-08-09 16:40:18 -07002955#if DEVICE_TREE
Matthew Qin271927e2015-03-31 22:07:07 -04002956 scratch_offset = image_actual + page_size + out_len;
Amol Jadicb524072012-08-09 16:40:18 -07002957 /* find correct dtb and copy it to right location */
Matthew Qin271927e2015-03-31 22:07:07 -04002958 ret = copy_dtb(data, scratch_offset);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002959
2960 dtb_copied = !ret ? 1 : 0;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002961#else
Mayank Grovere559cec2017-10-17 15:12:03 +05302962 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE) ||
2963 check_ddr_addr_range_bound(hdr->tags_addr, MAX_TAGS_SIZE))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002964 {
Mayank Grovere559cec2017-10-17 15:12:03 +05302965 dprintf(CRITICAL, "Tags addresses are not valid.\n");
lijuang2008ff22016-03-07 17:56:27 +08002966 goto boot_failed;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002967 }
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002968#endif
2969
2970 /* Load ramdisk & kernel */
2971 memmove((void*) hdr->ramdisk_addr, ptr + page_size + kernel_actual, hdr->ramdisk_size);
Matthew Qinbb7923d2015-02-09 10:56:09 +08002972 memmove((void*) hdr->kernel_addr, (char*) (kernel_start_addr), kernel_size);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002973
2974#if DEVICE_TREE
Mayank Grovere559cec2017-10-17 15:12:03 +05302975 if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual) ||
2976 check_ddr_addr_range_bound(hdr->tags_addr, kernel_actual))
Matthew Qinbb7923d2015-02-09 10:56:09 +08002977 {
Mayank Grovere559cec2017-10-17 15:12:03 +05302978 dprintf(CRITICAL, "Tags addresses are not valid.\n");
lijuang2008ff22016-03-07 17:56:27 +08002979 goto boot_failed;
Matthew Qinbb7923d2015-02-09 10:56:09 +08002980 }
2981
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002982 /*
2983 * If dtb is not found look for appended DTB in the kernel.
2984 * If appended dev tree is found, update the atags with
2985 * memory address to the DTB appended location on RAM.
2986 * Else update with the atags address in the kernel header
2987 */
2988 if (!dtb_copied) {
2989 void *dtb;
Matthew Qinbb7923d2015-02-09 10:56:09 +08002990 dtb = dev_tree_appended((void*)(ptr + page_size),
2991 hdr->kernel_size, dtb_offset,
Dima Zavine63e5572013-05-03 12:23:06 -07002992 (void *)hdr->tags_addr);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002993 if (!dtb) {
2994 fastboot_fail("dtb not found");
lijuang2008ff22016-03-07 17:56:27 +08002995 goto boot_failed;
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002996 }
Amol Jadicb524072012-08-09 16:40:18 -07002997 }
2998#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08002999
3000 fastboot_okay("");
Amol Jadi7c4316c2013-10-07 14:19:26 -07003001 fastboot_stop();
Brian Swetland9c4c0752009-01-25 16:23:50 -08003002
Dima Zavin77e41f32013-03-06 16:10:43 -08003003 boot_linux((void*) hdr->kernel_addr, (void*) hdr->tags_addr,
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07003004 (const char*) hdr->cmdline, board_machtype(),
3005 (void*) hdr->ramdisk_addr, hdr->ramdisk_size);
lijuang2008ff22016-03-07 17:56:27 +08003006
3007 /* fastboot already stop, it's no need to show fastboot menu */
3008 return;
3009boot_failed:
3010#if FBCON_DISPLAY_MSG
3011 /* revert to fastboot menu if boot failed */
3012 display_fastboot_menu();
3013#endif
3014 return;
Brian Swetland9c4c0752009-01-25 16:23:50 -08003015}
3016
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003017void cmd_erase_nand(const char *arg, void *data, unsigned sz)
Dima Zavin214cc642009-01-26 11:16:21 -08003018{
3019 struct ptentry *ptn;
3020 struct ptable *ptable;
3021
3022 ptable = flash_get_ptable();
3023 if (ptable == NULL) {
3024 fastboot_fail("partition table doesn't exist");
3025 return;
3026 }
3027
3028 ptn = ptable_find(ptable, arg);
3029 if (ptn == NULL) {
3030 fastboot_fail("unknown partition name");
3031 return;
3032 }
3033
Monika Singh292b3e92018-03-17 22:40:23 +05303034 if (!strncmp(arg, "avb_custom_key", strlen("avb_custom_key"))) {
3035 dprintf(INFO, "erasing avb_custom_key\n");
3036 if (erase_userkey()) {
3037 fastboot_fail("Erasing avb_custom_key failed");
3038 } else {
3039 fastboot_okay("");
3040 }
3041 return;
3042 }
3043
Dima Zavin214cc642009-01-26 11:16:21 -08003044 if (flash_erase(ptn)) {
3045 fastboot_fail("failed to erase partition");
3046 return;
3047 }
3048 fastboot_okay("");
3049}
3050
Bikas Gurungd48bd242010-09-04 19:54:32 -07003051
3052void cmd_erase_mmc(const char *arg, void *data, unsigned sz)
3053{
3054 unsigned long long ptn = 0;
Oliver Wangcee448d2013-10-22 18:40:13 +08003055 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07003056 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003057 uint8_t lun = 0;
Vijay Kumar Pendoti0b21f462016-05-02 17:09:18 +05303058 char *footer = NULL;
Bikas Gurungd48bd242010-09-04 19:54:32 -07003059
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003060#if VERIFIED_BOOT
3061 if(!strcmp(arg, KEYSTORE_PTN_NAME))
3062 {
3063 if(!device.is_unlocked)
3064 {
3065 fastboot_fail("unlock device to erase keystore");
3066 return;
3067 }
3068 }
3069#endif
3070
Kinson Chikf1a43512011-07-14 11:28:39 -07003071 index = partition_get_index(arg);
3072 ptn = partition_get_offset(index);
Oliver Wangcee448d2013-10-22 18:40:13 +08003073 size = partition_get_size(index);
Neeti Desaica8c9602011-10-06 11:40:00 -07003074
Monika Singhc4778b72018-05-16 11:16:42 +05303075 if (!strncmp(arg, "avb_custom_key", strlen("avb_custom_key"))) {
3076 dprintf(INFO, "erasing avb_custom_key\n");
3077 if (erase_userkey()) {
3078 fastboot_fail("Erasing avb_custom_key failed");
3079 } else {
3080 fastboot_okay("");
3081 }
3082 return;
3083 }
3084
Kinson Chikf1a43512011-07-14 11:28:39 -07003085 if(ptn == 0) {
Neeti Desaica8c9602011-10-06 11:40:00 -07003086 fastboot_fail("Partition table doesn't exist\n");
Bikas Gurungd48bd242010-09-04 19:54:32 -07003087 return;
3088 }
Kun Liang2f1601a2013-08-12 16:29:54 +08003089
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003090 lun = partition_get_lun(index);
3091 mmc_set_lun(lun);
3092
Sridhar Parasuramd7957122015-02-27 11:33:40 -08003093 if (platform_boot_dev_isemmc())
3094 {
3095 if (mmc_erase_card(ptn, size)) {
3096 fastboot_fail("failed to erase partition\n");
3097 return;
3098 }
3099 } else {
3100 BUF_DMA_ALIGN(out, DEFAULT_ERASE_SIZE);
3101 size = partition_get_size(index);
3102 if (size > DEFAULT_ERASE_SIZE)
3103 size = DEFAULT_ERASE_SIZE;
Kun Liang2f1601a2013-08-12 16:29:54 +08003104
Sridhar Parasuramd7957122015-02-27 11:33:40 -08003105 /* Simple inefficient version of erase. Just writing
3106 0 in first several blocks */
3107 if (mmc_write(ptn , size, (unsigned int *)out)) {
3108 fastboot_fail("failed to erase partition");
3109 return;
3110 }
Vijay Kumar Pendoti0b21f462016-05-02 17:09:18 +05303111 /*Erase FDE metadata at the userdata footer*/
3112 if(!(strncmp(arg, "userdata", 8)))
3113 {
3114 footer = memalign(CACHE_LINE, FOOTER_SIZE);
3115 memset((void *)footer, 0, FOOTER_SIZE);
3116
3117 size = partition_get_size(index);
3118
3119 if (mmc_write((ptn + size) - FOOTER_SIZE , FOOTER_SIZE, (unsigned int *)footer)) {
3120 fastboot_fail("failed to erase userdata footer");
3121 free(footer);
3122 return;
3123 }
3124 free(footer);
3125 }
Bikas Gurungd48bd242010-09-04 19:54:32 -07003126 }
Monika Singh292b3e92018-03-17 22:40:23 +05303127#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover912eaa62017-10-26 12:08:53 +05303128 if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05303129 !(strncmp(arg, "userdata", 8)) &&
3130 send_delete_keys_to_tz())
Sridhar Parasuram32b30662015-07-10 13:33:22 -07003131 ASSERT(0);
3132#endif
Bikas Gurungd48bd242010-09-04 19:54:32 -07003133 fastboot_okay("");
3134}
3135
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003136void cmd_erase(const char *arg, void *data, unsigned sz)
3137{
Monika Singh292b3e92018-03-17 22:40:23 +05303138#if VERIFIED_BOOT || VERIFIED_BOOT_2
Channagoud Kadabi35297672015-06-13 11:09:49 -07003139 if (target_build_variant_user())
Channagoud Kadabice3cf422015-04-17 16:02:30 -07003140 {
Sridhar Parasuramc32d07d2015-07-12 10:57:48 -07003141 if(!device.is_unlocked)
Channagoud Kadabice3cf422015-04-17 16:02:30 -07003142 {
Channagoud Kadabi35297672015-06-13 11:09:49 -07003143 fastboot_fail("device is locked. Cannot erase");
Channagoud Kadabice3cf422015-04-17 16:02:30 -07003144 return;
3145 }
3146 }
3147#endif
3148
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003149 if(target_is_emmc_boot())
3150 cmd_erase_mmc(arg, data, sz);
3151 else
3152 cmd_erase_nand(arg, data, sz);
3153}
Bikas Gurungd48bd242010-09-04 19:54:32 -07003154
Mayank Grover11ff9692018-01-11 11:54:49 +05303155/* Get the size from partiton name */
3156static void get_partition_size(const char *arg, char *response)
3157{
3158 uint64_t ptn = 0;
3159 uint64_t size;
3160 int index = INVALID_PTN;
3161
3162 index = partition_get_index(arg);
3163
3164 if (index == INVALID_PTN)
3165 {
3166 dprintf(CRITICAL, "Invalid partition index\n");
3167 return;
3168 }
3169
3170 ptn = partition_get_offset(index);
3171
3172 if(!ptn)
3173 {
3174 dprintf(CRITICAL, "Invalid partition name %s\n", arg);
3175 return;
3176 }
3177
3178 size = partition_get_size(index);
3179
3180 snprintf(response, MAX_RSP_SIZE, "\t 0x%llx", size);
3181 return;
3182}
3183
Mayank Grover52cd10a2018-03-15 12:57:54 +05303184/* Function to check partition type of a partition*/
3185static fs_signature_type
3186check_partition_fs_signature(const char *arg)
3187{
3188 fs_signature_type ret = NO_FS;
3189 int index;
3190 unsigned long long ptn;
lijuangc2676752018-05-15 13:52:36 +08003191 char *sb_buffer = memalign(CACHE_LINE, mmc_blocksize);
Mayank Grover52cd10a2018-03-15 12:57:54 +05303192 if (!sb_buffer)
3193 {
3194 dprintf(CRITICAL, "ERROR: Failed to allocate buffer for superblock\n");
3195 goto out;
3196 }
3197
3198 /* Read super block */
3199 if ((index = partition_get_index(arg)) < 0)
3200 {
3201 dprintf(CRITICAL, "ERROR: %s() doesn't exsit\n", arg);
3202 goto out;
3203 }
3204 ptn = partition_get_offset(index);
3205 mmc_set_lun(partition_get_lun(index));
3206 if(mmc_read(ptn + FS_SUPERBLOCK_OFFSET,
3207 (void *)sb_buffer, mmc_blocksize))
3208 {
3209 dprintf(CRITICAL, "ERROR: Failed to read Superblock\n");
3210 goto out;
3211 }
3212
3213 if (*((uint16 *)(&sb_buffer[EXT_MAGIC_OFFSET_SB]))
3214 == (uint16)EXT_MAGIC)
3215 {
3216 dprintf(SPEW, "%s() Found EXT FS\n", arg);
3217 ret = EXT_FS_SIGNATURE;
3218 }
3219 else if (*((uint32 *)(&sb_buffer[F2FS_MAGIC_OFFSET_SB]))
3220 == F2FS_MAGIC)
3221 {
3222 dprintf(SPEW, "%s() Found F2FS FS\n", arg);
3223 ret = EXT_F2FS_SIGNATURE;
3224 }
3225 else
3226 {
3227 dprintf(SPEW, "%s() Reverting to default 0x%x\n",
3228 arg, *((uint16 *)(&sb_buffer[EXT_MAGIC_OFFSET_SB])));
3229 ret = NO_FS;
3230 }
3231
3232out:
3233 if(sb_buffer)
3234 free(sb_buffer);
3235 return ret;
3236}
3237
Mayank Groverd38fe012018-03-13 15:33:16 +05303238/* Function to get partition type */
3239static void get_partition_type(const char *arg, char *response)
3240{
3241 uint n = 0;
Mayank Grover52cd10a2018-03-15 12:57:54 +05303242 fs_signature_type fs_signature;
Mayank Groverd38fe012018-03-13 15:33:16 +05303243
3244 if (arg == NULL ||
3245 response == NULL)
3246 {
3247 dprintf(CRITICAL, "Invalid input parameter\n");
3248 return;
3249 }
3250
3251 /* By default copy raw to response */
Mayank Grover0e559042018-04-11 17:49:30 +05303252 strlcpy(response, RAW_STR, MAX_RSP_SIZE);
Mayank Groverd38fe012018-03-13 15:33:16 +05303253
3254 /* Mark partiton type for known paritions only */
3255 for (n=0; n < ARRAY_SIZE(part_type_known); n++)
3256 {
3257 if (!strncmp(part_type_known[n].part_name, arg, strlen(arg)))
3258 {
Mayank Grover52cd10a2018-03-15 12:57:54 +05303259 /* Check partition for FS signature */
3260 fs_signature = check_partition_fs_signature(arg);
3261 switch (fs_signature)
3262 {
3263 case EXT_FS_SIGNATURE:
Mayank Grover0e559042018-04-11 17:49:30 +05303264 strlcpy(response, EXT_STR, MAX_RSP_SIZE);
Mayank Grover52cd10a2018-03-15 12:57:54 +05303265 break;
3266 case EXT_F2FS_SIGNATURE:
Mayank Grover0e559042018-04-11 17:49:30 +05303267 strlcpy(response, F2FS_STR, MAX_RSP_SIZE);
Mayank Grover52cd10a2018-03-15 12:57:54 +05303268 break;
3269 case NO_FS:
Mayank Grover0e559042018-04-11 17:49:30 +05303270 strlcpy(response, part_type_known[n].type_response, MAX_RSP_SIZE);
Mayank Grover52cd10a2018-03-15 12:57:54 +05303271 }
Mayank Groverd38fe012018-03-13 15:33:16 +05303272 }
3273 }
3274 return;
3275}
3276
Mayank Grover11ff9692018-01-11 11:54:49 +05303277/*
3278 * Publish the partition type & size info
3279 * fastboot getvar will publish the required information.
3280 * fastboot getvar partition_size:<partition_name>: partition size in hex
3281 * fastboot getvar partition_type:<partition_name>: partition type (ext/fat)
3282 */
3283static void publish_getvar_partition_info(struct getvar_partition_info *info, uint8_t num_parts)
3284{
Mayank Groverd38fe012018-03-13 15:33:16 +05303285 uint8_t i;
Mayank Grover11ff9692018-01-11 11:54:49 +05303286 static bool published = false;
3287 struct partition_entry *ptn_entry =
3288 partition_get_partition_entries();
3289 memset(info, 0, sizeof(struct getvar_partition_info)* num_parts);
3290
3291 for (i = 0; i < num_parts; i++) {
3292 strlcat(info[i].part_name, (const char *)ptn_entry[i].name, MAX_RSP_SIZE);
3293 strlcat(info[i].getvar_size, "partition-size:", MAX_GET_VAR_NAME_SIZE);
3294 strlcat(info[i].getvar_type, "partition-type:", MAX_GET_VAR_NAME_SIZE);
3295
Mayank Groverd38fe012018-03-13 15:33:16 +05303296 get_partition_type(info[i].part_name, info[i].type_response);
Mayank Grover11ff9692018-01-11 11:54:49 +05303297 get_partition_size(info[i].part_name, info[i].size_response);
Mayank Grover11ff9692018-01-11 11:54:49 +05303298 if (strlcat(info[i].getvar_size, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
3299 {
3300 dprintf(CRITICAL, "partition size name truncated\n");
3301 return;
3302 }
3303 if (strlcat(info[i].getvar_type, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
3304 {
3305 dprintf(CRITICAL, "partition type name truncated\n");
3306 return;
3307 }
3308
3309 if (!published)
3310 {
3311 /* publish partition size & type info */
3312 fastboot_publish((const char *) info[i].getvar_size, (const char *) info[i].size_response);
3313 fastboot_publish((const char *) info[i].getvar_type, (const char *) info[i].type_response);
3314 }
3315 }
3316 if (!published)
3317 published = true;
3318}
3319
3320
Ajay Dudani5c761132011-04-07 20:19:04 -07003321void cmd_flash_mmc_img(const char *arg, void *data, unsigned sz)
Shashank Mittal23b8f422010-04-16 19:27:21 -07003322{
3323 unsigned long long ptn = 0;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07003324 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07003325 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003326 char *token = NULL;
3327 char *pname = NULL;
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07003328 char *sp;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003329 uint8_t lun = 0;
3330 bool lun_set = false;
Mayank Grover351a75e2017-05-30 20:06:08 +05303331 int current_active_slot = INVALID;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07003332
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07003333 token = strtok_r((char *)arg, ":", &sp);
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003334 pname = token;
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07003335 token = strtok_r(NULL, ":", &sp);
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003336 if(token)
3337 {
3338 lun = atoi(token);
3339 mmc_set_lun(lun);
3340 lun_set = true;
3341 }
3342
Mao Jinlong226f33a2014-07-04 17:24:10 +08003343 if (pname)
Greg Grisco6e754772011-06-23 12:19:39 -07003344 {
Channagoud Kadabiad259832015-05-29 11:14:17 -07003345 if (!strncmp(pname, "frp-unlock", strlen("frp-unlock")))
3346 {
3347 if (!aboot_frp_unlock(pname, data, sz))
3348 {
3349 fastboot_info("FRP unlock successful");
3350 fastboot_okay("");
3351 }
3352 else
3353 fastboot_fail("Secret key is invalid, please update the bootloader with secret key");
3354
3355 return;
3356 }
3357
Mao Jinlong226f33a2014-07-04 17:24:10 +08003358 if (!strcmp(pname, "partition"))
3359 {
3360 dprintf(INFO, "Attempt to write partition image.\n");
3361 if (write_partition(sz, (unsigned char *) data)) {
3362 fastboot_fail("failed to write partition");
Greg Grisco6e754772011-06-23 12:19:39 -07003363 return;
3364 }
Mayank Grover11ff9692018-01-11 11:54:49 +05303365 /* Re-publish partition table */
3366 publish_getvar_partition_info(part_info, partition_get_partition_count());
Mayank Grover351a75e2017-05-30 20:06:08 +05303367
3368 /* Rescan partition table to ensure we have multislot support*/
3369 if (partition_scan_for_multislot())
3370 {
3371 current_active_slot = partition_find_active_slot();
3372 dprintf(INFO, "Multislot supported: Slot %s active",
3373 (SUFFIX_SLOT(current_active_slot)));
3374 }
3375 partition_mark_active_slot(current_active_slot);
Greg Grisco6e754772011-06-23 12:19:39 -07003376 }
Mao Jinlong226f33a2014-07-04 17:24:10 +08003377 else
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003378 {
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003379#if VERIFIED_BOOT
3380 if(!strcmp(pname, KEYSTORE_PTN_NAME))
3381 {
3382 if(!device.is_unlocked)
3383 {
3384 fastboot_fail("unlock device to flash keystore");
3385 return;
3386 }
Gaurav Nebhwanic7313cc2015-12-15 22:25:04 +05303387 if(!boot_verify_validate_keystore((unsigned char *)data,sz))
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003388 {
3389 fastboot_fail("image is not a keystore file");
3390 return;
3391 }
3392 }
3393#endif
Mao Jinlong226f33a2014-07-04 17:24:10 +08003394 index = partition_get_index(pname);
3395 ptn = partition_get_offset(index);
3396 if(ptn == 0) {
3397 fastboot_fail("partition table doesn't exist");
3398 return;
3399 }
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003400
Mayank Grover351a75e2017-05-30 20:06:08 +05303401 if (!strncmp(pname, "boot", strlen("boot"))
3402 || !strcmp(pname, "recovery"))
3403 {
Mao Jinlong226f33a2014-07-04 17:24:10 +08003404 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
3405 fastboot_fail("image is not a boot image");
3406 return;
3407 }
Mayank Grover351a75e2017-05-30 20:06:08 +05303408
3409 /* Reset multislot_partition attributes in case of flashing boot */
3410 if (partition_multislot_is_supported())
3411 {
3412 partition_reset_attributes(index);
3413 }
Mao Jinlong226f33a2014-07-04 17:24:10 +08003414 }
3415
3416 if(!lun_set)
3417 {
3418 lun = partition_get_lun(index);
3419 mmc_set_lun(lun);
3420 }
3421
3422 size = partition_get_size(index);
Gaurav Nebhwani4d2389c2016-03-17 21:10:05 +05303423 if (ROUND_TO_PAGE(sz, mmc_blocksize_mask) > size) {
Mao Jinlong226f33a2014-07-04 17:24:10 +08003424 fastboot_fail("size too large");
3425 return;
3426 }
3427 else if (mmc_write(ptn , sz, (unsigned int *)data)) {
3428 fastboot_fail("flash write failure");
3429 return;
3430 }
Greg Grisco6e754772011-06-23 12:19:39 -07003431 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07003432 }
3433 fastboot_okay("");
3434 return;
3435}
3436
Ajay Dudanide984792015-03-02 09:57:41 -08003437void cmd_flash_meta_img(const char *arg, void *data, unsigned sz)
3438{
3439 int i, images;
3440 meta_header_t *meta_header;
3441 img_header_entry_t *img_header_entry;
Parth Dixit3e2d3032016-03-04 17:11:52 +05303442 /*End of the image address*/
3443 uintptr_t data_end;
3444
3445 if( (UINT_MAX - sz) > (uintptr_t)data )
3446 data_end = (uintptr_t)data + sz;
3447 else
3448 {
3449 fastboot_fail("Cannot flash: image header corrupt");
3450 return;
3451 }
3452
3453 if( data_end < ((uintptr_t)data + sizeof(meta_header_t)))
3454 {
3455 fastboot_fail("Cannot flash: image header corrupt");
3456 return;
3457 }
Ajay Dudanide984792015-03-02 09:57:41 -08003458
lijuang8ee21882016-04-19 16:57:11 +08003459 /* If device is locked:
3460 * Forbid to flash image to avoid the device to bypass the image
3461 * which with "any" name other than bootloader. Because it maybe
3462 * a meta package of all partitions.
3463 */
Monika Singh292b3e92018-03-17 22:40:23 +05303464#if VERIFIED_BOOT || VERIFIED_BOOT_2
lijuang8ee21882016-04-19 16:57:11 +08003465 if (target_build_variant_user()) {
3466 if (!device.is_unlocked) {
3467 fastboot_fail("Device is locked, meta image flashing is not allowed");
3468 return;
3469 }
Mayank Grover889be1b2017-09-12 20:12:23 +05303470
Mayank Grover912eaa62017-10-26 12:08:53 +05303471 if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05303472 !device.is_unlock_critical)
3473 {
lijuang8ee21882016-04-19 16:57:11 +08003474 fastboot_fail("Device is critical locked, Meta image flashing is not allowed");
3475 return;
3476 }
lijuang8ee21882016-04-19 16:57:11 +08003477 }
3478#endif
3479
Ajay Dudanide984792015-03-02 09:57:41 -08003480 meta_header = (meta_header_t*) data;
Parth Dixit3e2d3032016-03-04 17:11:52 +05303481 if( data_end < ((uintptr_t)data + meta_header->img_hdr_sz))
3482 {
3483 fastboot_fail("Cannot flash: image header corrupt");
3484 return;
3485 }
Ajay Dudanide984792015-03-02 09:57:41 -08003486 img_header_entry = (img_header_entry_t*) (data+sizeof(meta_header_t));
3487
3488 images = meta_header->img_hdr_sz / sizeof(img_header_entry_t);
3489
3490 for (i=0; i<images; i++) {
3491
3492 if((img_header_entry[i].ptn_name == NULL) ||
3493 (img_header_entry[i].start_offset == 0) ||
3494 (img_header_entry[i].size == 0))
3495 break;
Kishor PK49831502017-04-21 17:55:43 +05303496 if ((UINT_MAX - img_header_entry[i].start_offset) < (uintptr_t)data) {
3497 fastboot_fail("Integer overflow detected in start_offset of img");
3498 break;
3499 }
3500 else if ((UINT_MAX - (img_header_entry[i].start_offset + (uintptr_t)data)) < img_header_entry[i].size) {
3501 fastboot_fail("Integer overflow detected in size of img");
3502 break;
3503 }
Parth Dixit3e2d3032016-03-04 17:11:52 +05303504 if( data_end < ((uintptr_t)data + img_header_entry[i].start_offset
3505 + img_header_entry[i].size) )
3506 {
3507 fastboot_fail("Cannot flash: image size mismatch");
3508 break;
3509 }
3510
Ajay Dudanide984792015-03-02 09:57:41 -08003511 cmd_flash_mmc_img(img_header_entry[i].ptn_name,
3512 (void *) data + img_header_entry[i].start_offset,
3513 img_header_entry[i].size);
3514 }
3515
Channagoud Kadabiaafbbfc2015-03-04 17:52:56 -08003516 if (!strncmp(arg, "bootloader", strlen("bootloader")))
3517 {
3518 strlcpy(device.bootloader_version, TARGET(BOARD), MAX_VERSION_LEN);
3519 strlcat(device.bootloader_version, "-", MAX_VERSION_LEN);
3520 strlcat(device.bootloader_version, meta_header->img_version, MAX_VERSION_LEN);
3521 }
3522 else
3523 {
3524 strlcpy(device.radio_version, TARGET(BOARD), MAX_VERSION_LEN);
3525 strlcat(device.radio_version, "-", MAX_VERSION_LEN);
3526 strlcat(device.radio_version, meta_header->img_version, MAX_VERSION_LEN);
3527 }
3528
3529 write_device_info(&device);
Ajay Dudanide984792015-03-02 09:57:41 -08003530 fastboot_okay("");
3531 return;
3532}
3533
Ajay Dudani5c761132011-04-07 20:19:04 -07003534void cmd_flash_mmc_sparse_img(const char *arg, void *data, unsigned sz)
3535{
3536 unsigned int chunk;
wufeng.jiang813dc352015-06-02 23:02:46 -04003537 uint64_t chunk_data_sz;
Channagoud Kadabif627cf82013-09-09 14:08:20 -07003538 uint32_t *fill_buf = NULL;
3539 uint32_t fill_val;
Mayank Grover4f50bba2017-07-19 18:17:08 +05303540 uint32_t blk_sz_actual = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07003541 sparse_header_t *sparse_header;
3542 chunk_header_t *chunk_header;
Ajay Dudaniab18f022011-05-12 14:39:22 -07003543 uint32_t total_blocks = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07003544 unsigned long long ptn = 0;
Channagoud Kadabi65b91002011-10-11 17:34:33 +05303545 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07003546 int index = INVALID_PTN;
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05303547 uint32_t i;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003548 uint8_t lun = 0;
vijay kumar800255e2015-04-24 20:26:19 +05303549 /*End of the sparse image address*/
Parth Dixit64b1a482016-03-07 16:31:26 +05303550 uintptr_t data_end = (uintptr_t)data + sz;
Ajay Dudani5c761132011-04-07 20:19:04 -07003551
Kinson Chikf1a43512011-07-14 11:28:39 -07003552 index = partition_get_index(arg);
3553 ptn = partition_get_offset(index);
3554 if(ptn == 0) {
Ajay Dudani5c761132011-04-07 20:19:04 -07003555 fastboot_fail("partition table doesn't exist");
3556 return;
3557 }
3558
Channagoud Kadabi65b91002011-10-11 17:34:33 +05303559 size = partition_get_size(index);
Channagoud Kadabi65b91002011-10-11 17:34:33 +05303560
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003561 lun = partition_get_lun(index);
3562 mmc_set_lun(lun);
3563
vijay kumar800255e2015-04-24 20:26:19 +05303564 if (sz < sizeof(sparse_header_t)) {
3565 fastboot_fail("size too low");
3566 return;
3567 }
3568
Ajay Dudani5c761132011-04-07 20:19:04 -07003569 /* Read and skip over sparse image header */
3570 sparse_header = (sparse_header_t *) data;
vijay kumar800255e2015-04-24 20:26:19 +05303571
Mayank Grover48bd9bb2017-07-19 12:04:16 +05303572 if (!sparse_header->blk_sz || (sparse_header->blk_sz % 4)){
3573 fastboot_fail("Invalid block size\n");
3574 return;
3575 }
3576
vijay kumar1321f342015-03-27 12:13:42 +05303577 if (((uint64_t)sparse_header->total_blks * (uint64_t)sparse_header->blk_sz) > size) {
Ajay Dudani876b3282012-12-21 14:12:17 -08003578 fastboot_fail("size too large");
3579 return;
3580 }
3581
vijay kumarde4fcf62015-04-23 13:05:49 +05303582 data += sizeof(sparse_header_t);
vijay kumar800255e2015-04-24 20:26:19 +05303583
Parth Dixit64b1a482016-03-07 16:31:26 +05303584 if (data_end < (uintptr_t)data) {
vijay kumar800255e2015-04-24 20:26:19 +05303585 fastboot_fail("buffer overreads occured due to invalid sparse header");
3586 return;
3587 }
3588
vijay kumarde4fcf62015-04-23 13:05:49 +05303589 if(sparse_header->file_hdr_sz != sizeof(sparse_header_t))
Ajay Dudani5c761132011-04-07 20:19:04 -07003590 {
vijay kumarde4fcf62015-04-23 13:05:49 +05303591 fastboot_fail("sparse header size mismatch");
3592 return;
Ajay Dudani5c761132011-04-07 20:19:04 -07003593 }
3594
Ajay Dudanib06c05f2011-05-12 14:46:10 -07003595 dprintf (SPEW, "=== Sparse Image Header ===\n");
3596 dprintf (SPEW, "magic: 0x%x\n", sparse_header->magic);
3597 dprintf (SPEW, "major_version: 0x%x\n", sparse_header->major_version);
3598 dprintf (SPEW, "minor_version: 0x%x\n", sparse_header->minor_version);
3599 dprintf (SPEW, "file_hdr_sz: %d\n", sparse_header->file_hdr_sz);
3600 dprintf (SPEW, "chunk_hdr_sz: %d\n", sparse_header->chunk_hdr_sz);
3601 dprintf (SPEW, "blk_sz: %d\n", sparse_header->blk_sz);
3602 dprintf (SPEW, "total_blks: %d\n", sparse_header->total_blks);
3603 dprintf (SPEW, "total_chunks: %d\n", sparse_header->total_chunks);
Ajay Dudani5c761132011-04-07 20:19:04 -07003604
3605 /* Start processing chunks */
3606 for (chunk=0; chunk<sparse_header->total_chunks; chunk++)
3607 {
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05303608 /* Make sure the total image size does not exceed the partition size */
3609 if(((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz) >= size) {
3610 fastboot_fail("size too large");
3611 return;
3612 }
Ajay Dudani5c761132011-04-07 20:19:04 -07003613 /* Read and skip over chunk header */
3614 chunk_header = (chunk_header_t *) data;
3615 data += sizeof(chunk_header_t);
3616
Parth Dixit64b1a482016-03-07 16:31:26 +05303617 if (data_end < (uintptr_t)data) {
vijay kumar800255e2015-04-24 20:26:19 +05303618 fastboot_fail("buffer overreads occured due to invalid sparse header");
3619 return;
3620 }
3621
Ajay Dudani5c761132011-04-07 20:19:04 -07003622 dprintf (SPEW, "=== Chunk Header ===\n");
3623 dprintf (SPEW, "chunk_type: 0x%x\n", chunk_header->chunk_type);
3624 dprintf (SPEW, "chunk_data_sz: 0x%x\n", chunk_header->chunk_sz);
3625 dprintf (SPEW, "total_size: 0x%x\n", chunk_header->total_sz);
3626
vijay kumar800255e2015-04-24 20:26:19 +05303627 if(sparse_header->chunk_hdr_sz != sizeof(chunk_header_t))
Ajay Dudani5c761132011-04-07 20:19:04 -07003628 {
vijay kumar800255e2015-04-24 20:26:19 +05303629 fastboot_fail("chunk header size mismatch");
3630 return;
Ajay Dudani5c761132011-04-07 20:19:04 -07003631 }
3632
wufeng.jiang813dc352015-06-02 23:02:46 -04003633 chunk_data_sz = (uint64_t)sparse_header->blk_sz * chunk_header->chunk_sz;
3634
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05303635 /* Make sure that the chunk size calculated from sparse image does not
3636 * exceed partition size
3637 */
3638 if ((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz + chunk_data_sz > size)
3639 {
3640 fastboot_fail("Chunk data size exceeds partition size");
3641 return;
3642 }
3643
Ajay Dudani5c761132011-04-07 20:19:04 -07003644 switch (chunk_header->chunk_type)
3645 {
3646 case CHUNK_TYPE_RAW:
wufeng.jiang813dc352015-06-02 23:02:46 -04003647 if((uint64_t)chunk_header->total_sz != ((uint64_t)sparse_header->chunk_hdr_sz +
Ajay Dudani5c761132011-04-07 20:19:04 -07003648 chunk_data_sz))
3649 {
3650 fastboot_fail("Bogus chunk size for chunk type Raw");
3651 return;
3652 }
3653
Parth Dixit64b1a482016-03-07 16:31:26 +05303654 if (data_end < (uintptr_t)data + chunk_data_sz) {
vijay kumar800255e2015-04-24 20:26:19 +05303655 fastboot_fail("buffer overreads occured due to invalid sparse header");
3656 return;
3657 }
3658
wufeng.jiang813dc352015-06-02 23:02:46 -04003659 /* chunk_header->total_sz is uint32,So chunk_data_sz is now less than 2^32
3660 otherwise it will return in the line above
3661 */
Ajay Dudaniab18f022011-05-12 14:39:22 -07003662 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
wufeng.jiang813dc352015-06-02 23:02:46 -04003663 (uint32_t)chunk_data_sz,
Ajay Dudaniab18f022011-05-12 14:39:22 -07003664 (unsigned int*)data))
Ajay Dudani5c761132011-04-07 20:19:04 -07003665 {
3666 fastboot_fail("flash write failure");
3667 return;
3668 }
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05303669 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
3670 fastboot_fail("Bogus size for RAW chunk type");
3671 return;
3672 }
Ajay Dudani5c761132011-04-07 20:19:04 -07003673 total_blocks += chunk_header->chunk_sz;
wufeng.jiang813dc352015-06-02 23:02:46 -04003674 data += (uint32_t)chunk_data_sz;
Ajay Dudani5c761132011-04-07 20:19:04 -07003675 break;
3676
Channagoud Kadabif627cf82013-09-09 14:08:20 -07003677 case CHUNK_TYPE_FILL:
3678 if(chunk_header->total_sz != (sparse_header->chunk_hdr_sz +
3679 sizeof(uint32_t)))
3680 {
3681 fastboot_fail("Bogus chunk size for chunk type FILL");
3682 return;
3683 }
3684
Mayank Grover4f50bba2017-07-19 18:17:08 +05303685 blk_sz_actual = ROUNDUP(sparse_header->blk_sz, CACHE_LINE);
3686 /* Integer overflow detected */
3687 if (blk_sz_actual < sparse_header->blk_sz)
3688 {
3689 fastboot_fail("Invalid block size");
3690 return;
3691 }
3692
3693 fill_buf = (uint32_t *)memalign(CACHE_LINE, blk_sz_actual);
Channagoud Kadabif627cf82013-09-09 14:08:20 -07003694 if (!fill_buf)
3695 {
3696 fastboot_fail("Malloc failed for: CHUNK_TYPE_FILL");
3697 return;
3698 }
3699
Parth Dixit64b1a482016-03-07 16:31:26 +05303700 if (data_end < (uintptr_t)data + sizeof(uint32_t)) {
vijay kumar800255e2015-04-24 20:26:19 +05303701 fastboot_fail("buffer overreads occured due to invalid sparse header");
Gaurav Nebhwani163cbf82016-03-17 21:21:27 +05303702 free(fill_buf);
vijay kumar800255e2015-04-24 20:26:19 +05303703 return;
3704 }
Channagoud Kadabif627cf82013-09-09 14:08:20 -07003705 fill_val = *(uint32_t *)data;
3706 data = (char *) data + sizeof(uint32_t);
Channagoud Kadabif627cf82013-09-09 14:08:20 -07003707
3708 for (i = 0; i < (sparse_header->blk_sz / sizeof(fill_val)); i++)
3709 {
3710 fill_buf[i] = fill_val;
3711 }
3712
Gaurav Nebhwanie94cbe12016-03-17 21:16:34 +05303713 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz))
3714 {
3715 fastboot_fail("bogus size for chunk FILL type");
3716 free(fill_buf);
3717 return;
3718 }
3719
wufeng.jiang813dc352015-06-02 23:02:46 -04003720 for (i = 0; i < chunk_header->chunk_sz; i++)
Channagoud Kadabif627cf82013-09-09 14:08:20 -07003721 {
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05303722 /* Make sure that the data written to partition does not exceed partition size */
3723 if ((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz + sparse_header->blk_sz > size)
3724 {
3725 fastboot_fail("Chunk data size for fill type exceeds partition size");
Gaurav Nebhwani163cbf82016-03-17 21:21:27 +05303726 free(fill_buf);
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05303727 return;
3728 }
3729
Channagoud Kadabif627cf82013-09-09 14:08:20 -07003730 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
3731 sparse_header->blk_sz,
3732 fill_buf))
3733 {
3734 fastboot_fail("flash write failure");
3735 free(fill_buf);
3736 return;
3737 }
3738
3739 total_blocks++;
3740 }
3741
3742 free(fill_buf);
3743 break;
3744
Ajay Dudani5c761132011-04-07 20:19:04 -07003745 case CHUNK_TYPE_DONT_CARE:
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05303746 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
3747 fastboot_fail("bogus size for chunk DONT CARE type");
3748 return;
3749 }
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07003750 total_blocks += chunk_header->chunk_sz;
3751 break;
3752
Ajay Dudani5c761132011-04-07 20:19:04 -07003753 case CHUNK_TYPE_CRC:
3754 if(chunk_header->total_sz != sparse_header->chunk_hdr_sz)
3755 {
wufeng.jiang813dc352015-06-02 23:02:46 -04003756 fastboot_fail("Bogus chunk size for chunk type CRC");
Ajay Dudani5c761132011-04-07 20:19:04 -07003757 return;
3758 }
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05303759 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
3760 fastboot_fail("bogus size for chunk CRC type");
3761 return;
3762 }
Ajay Dudani5c761132011-04-07 20:19:04 -07003763 total_blocks += chunk_header->chunk_sz;
Parth Dixit64b1a482016-03-07 16:31:26 +05303764 if ((uintptr_t)data > UINT_MAX - chunk_data_sz) {
vijay kumar800255e2015-04-24 20:26:19 +05303765 fastboot_fail("integer overflow occured");
3766 return;
3767 }
wufeng.jiang813dc352015-06-02 23:02:46 -04003768 data += (uint32_t)chunk_data_sz;
Parth Dixit64b1a482016-03-07 16:31:26 +05303769 if (data_end < (uintptr_t)data) {
vijay kumar800255e2015-04-24 20:26:19 +05303770 fastboot_fail("buffer overreads occured due to invalid sparse header");
3771 return;
3772 }
Ajay Dudani5c761132011-04-07 20:19:04 -07003773 break;
3774
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07003775 default:
Channagoud Kadabif627cf82013-09-09 14:08:20 -07003776 dprintf(CRITICAL, "Unkown chunk type: %x\n",chunk_header->chunk_type);
Ajay Dudani5c761132011-04-07 20:19:04 -07003777 fastboot_fail("Unknown chunk type");
3778 return;
3779 }
3780 }
3781
Ajay Dudani0c6927b2011-05-18 11:12:16 -07003782 dprintf(INFO, "Wrote %d blocks, expected to write %d blocks\n",
3783 total_blocks, sparse_header->total_blks);
3784
3785 if(total_blocks != sparse_header->total_blks)
3786 {
3787 fastboot_fail("sparse image write failure");
3788 }
Ajay Dudani5c761132011-04-07 20:19:04 -07003789
3790 fastboot_okay("");
3791 return;
3792}
3793
3794void cmd_flash_mmc(const char *arg, void *data, unsigned sz)
3795{
3796 sparse_header_t *sparse_header;
Ajay Dudanide984792015-03-02 09:57:41 -08003797 meta_header_t *meta_header;
Ajay Dudani5c761132011-04-07 20:19:04 -07003798
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07003799#ifdef SSD_ENABLE
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -08003800 /* 8 Byte Magic + 2048 Byte xml + Encrypted Data */
3801 unsigned int *magic_number = (unsigned int *) data;
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08003802 int ret=0;
3803 uint32 major_version=0;
3804 uint32 minor_version=0;
3805
3806 ret = scm_svc_version(&major_version,&minor_version);
3807 if(!ret)
3808 {
3809 if(major_version >= 2)
3810 {
Amir Samuelovbb65ce02013-05-05 12:20:18 +03003811 if( !strcmp(arg, "ssd") || !strcmp(arg, "tqs") )
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08003812 {
3813 ret = encrypt_scm((uint32 **) &data, &sz);
3814 if (ret != 0) {
3815 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
3816 return;
3817 }
3818
Amir Samuelovbb65ce02013-05-05 12:20:18 +03003819 /* Protect only for SSD */
3820 if (!strcmp(arg, "ssd")) {
3821 ret = scm_protect_keystore((uint32 *) data, sz);
3822 if (ret != 0) {
3823 dprintf(CRITICAL, "ERROR: scm_protect_keystore Failed\n");
3824 return;
3825 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08003826 }
3827 }
3828 else
3829 {
3830 ret = decrypt_scm_v2((uint32 **) &data, &sz);
3831 if(ret != 0)
3832 {
3833 dprintf(CRITICAL,"ERROR: Decryption Failure\n");
3834 return;
3835 }
3836 }
3837 }
3838 else
3839 {
3840 if (magic_number[0] == DECRYPT_MAGIC_0 &&
3841 magic_number[1] == DECRYPT_MAGIC_1)
3842 {
3843 ret = decrypt_scm((uint32 **) &data, &sz);
3844 if (ret != 0) {
3845 dprintf(CRITICAL, "ERROR: Invalid secure image\n");
3846 return;
3847 }
3848 }
3849 else if (magic_number[0] == ENCRYPT_MAGIC_0 &&
3850 magic_number[1] == ENCRYPT_MAGIC_1)
3851 {
3852 ret = encrypt_scm((uint32 **) &data, &sz);
3853 if (ret != 0) {
3854 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
3855 return;
3856 }
3857 }
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07003858 }
3859 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08003860 else
Neeti Desai127b9e02012-03-20 16:11:23 -07003861 {
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08003862 dprintf(CRITICAL,"INVALID SVC Version\n");
3863 return;
Neeti Desai127b9e02012-03-20 16:11:23 -07003864 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08003865#endif /* SSD_ENABLE */
Neeti Desai127b9e02012-03-20 16:11:23 -07003866
Monika Singh292b3e92018-03-17 22:40:23 +05303867#if VERIFIED_BOOT || VERIFIED_BOOT_2
Channagoud Kadabi35297672015-06-13 11:09:49 -07003868 if (target_build_variant_user())
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003869 {
lijuang511a2b52015-08-14 20:50:51 +08003870 /* if device is locked:
3871 * common partition will not allow to be flashed
3872 * critical partition will allow to flash image.
3873 */
3874 if(!device.is_unlocked && !critical_flash_allowed(arg)) {
3875 fastboot_fail("Partition flashing is not allowed");
3876 return;
3877 }
Mayank Grover889be1b2017-09-12 20:12:23 +05303878
lijuang511a2b52015-08-14 20:50:51 +08003879 /* if device critical is locked:
3880 * common partition will allow to be flashed
3881 * critical partition will not allow to flash image.
3882 */
Mayank Grover912eaa62017-10-26 12:08:53 +05303883 if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05303884 !device.is_unlock_critical &&
3885 critical_flash_allowed(arg)) {
3886 fastboot_fail("Critical partition flashing is not allowed");
3887 return;
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003888 }
3889 }
3890#endif
Monika Singh292b3e92018-03-17 22:40:23 +05303891 if (!strncmp(arg, "avb_custom_key", strlen("avb_custom_key"))) {
3892 dprintf(INFO, "flashing avb_custom_key\n");
3893 if (store_userkey(data, sz)) {
3894 fastboot_fail("Flashing avb_custom_key failed");
3895 } else {
3896 fastboot_okay("");
3897 }
3898 return;
3899 }
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003900
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07003901 sparse_header = (sparse_header_t *) data;
Ajay Dudanide984792015-03-02 09:57:41 -08003902 meta_header = (meta_header_t *) data;
3903 if (sparse_header->magic == SPARSE_HEADER_MAGIC)
Ajay Dudani5c761132011-04-07 20:19:04 -07003904 cmd_flash_mmc_sparse_img(arg, data, sz);
Ajay Dudanide984792015-03-02 09:57:41 -08003905 else if (meta_header->magic == META_HEADER_MAGIC)
3906 cmd_flash_meta_img(arg, data, sz);
3907 else
3908 cmd_flash_mmc_img(arg, data, sz);
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07003909
3910#if VERIFIED_BOOT
Mayank Grover912eaa62017-10-26 12:08:53 +05303911 if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05303912 (!strncmp(arg, "system", 6)) &&
3913 !device.verity_mode)
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07003914 // reset dm_verity mode to enforcing
3915 device.verity_mode = 1;
3916 write_device_info(&device);
Parth Dixitddbc7352015-10-18 03:13:31 +05303917#endif
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07003918
Ajay Dudani5c761132011-04-07 20:19:04 -07003919 return;
3920}
3921
Tanya Brokhman84eaaf62015-02-24 21:57:06 +02003922void cmd_updatevol(const char *vol_name, void *data, unsigned sz)
3923{
3924 struct ptentry *sys_ptn;
3925 struct ptable *ptable;
3926
3927 ptable = flash_get_ptable();
3928 if (ptable == NULL) {
3929 fastboot_fail("partition table doesn't exist");
3930 return;
3931 }
3932
3933 sys_ptn = ptable_find(ptable, "system");
3934 if (sys_ptn == NULL) {
3935 fastboot_fail("system partition not found");
3936 return;
3937 }
3938
3939 sz = ROUND_TO_PAGE(sz, page_mask);
3940 if (update_ubi_vol(sys_ptn, vol_name, data, sz))
3941 fastboot_fail("update_ubi_vol failed");
3942 else
3943 fastboot_okay("");
3944}
3945
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003946void cmd_flash_nand(const char *arg, void *data, unsigned sz)
Dima Zavin214cc642009-01-26 11:16:21 -08003947{
3948 struct ptentry *ptn;
3949 struct ptable *ptable;
3950 unsigned extra = 0;
Gaurav Nebhwanid9730712016-05-06 14:28:29 +05303951 uint64_t partition_size = 0;
Vijay Kumar Pendotic6f22a72017-05-19 22:44:34 +05303952 unsigned bytes_to_round_page = 0;
3953 unsigned rounded_size = 0;
Dima Zavin214cc642009-01-26 11:16:21 -08003954
Kishor PK38ed93d2017-04-25 14:19:26 +05303955 if((uintptr_t)data > (UINT_MAX - sz)) {
3956 fastboot_fail("Cannot flash: image header corrupt");
3957 return;
3958 }
3959
Dima Zavin214cc642009-01-26 11:16:21 -08003960 ptable = flash_get_ptable();
3961 if (ptable == NULL) {
3962 fastboot_fail("partition table doesn't exist");
3963 return;
3964 }
3965
3966 ptn = ptable_find(ptable, arg);
3967 if (ptn == NULL) {
Tanya Brokhman84eaaf62015-02-24 21:57:06 +02003968 dprintf(INFO, "unknown partition name (%s). Trying updatevol\n",
3969 arg);
3970 cmd_updatevol(arg, data, sz);
Dima Zavin214cc642009-01-26 11:16:21 -08003971 return;
3972 }
3973
Monika Singh292b3e92018-03-17 22:40:23 +05303974 if (!strncmp(arg, "avb_custom_key", strlen("avb_custom_key"))) {
3975 dprintf(INFO, "flashing avb_custom_key\n");
3976 if (store_userkey(data, sz)) {
3977 fastboot_fail("Flashing avb_custom_key failed");
3978 } else {
3979 fastboot_okay("");
3980 }
3981 return;
3982 }
3983
Dima Zavin214cc642009-01-26 11:16:21 -08003984 if (!strcmp(ptn->name, "boot") || !strcmp(ptn->name, "recovery")) {
Kishor PK38ed93d2017-04-25 14:19:26 +05303985 if((sz > BOOT_MAGIC_SIZE) && (!memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE))) {
3986 dprintf(INFO, "Verified the BOOT_MAGIC in image header \n");
3987 } else {
3988 fastboot_fail("Image is not a boot image");
Dima Zavin214cc642009-01-26 11:16:21 -08003989 return;
3990 }
3991 }
3992
Amol Jadi5c61a952012-05-04 17:05:35 -07003993 if (!strcmp(ptn->name, "system")
Deepa Dinamani13e32c42012-03-12 14:34:17 -07003994 || !strcmp(ptn->name, "userdata")
3995 || !strcmp(ptn->name, "persist")
Sundarajan Srinivasanb063a852013-11-19 14:02:27 -08003996 || !strcmp(ptn->name, "recoveryfs")
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -08003997 || !strcmp(ptn->name, "modem"))
Tanya Brokhman1c94f1a2015-02-15 09:05:03 +02003998 extra = 1;
Kishor PK38ed93d2017-04-25 14:19:26 +05303999 else {
Vijay Kumar Pendotic6f22a72017-05-19 22:44:34 +05304000 rounded_size = ROUNDUP(sz, page_size);
4001 bytes_to_round_page = rounded_size - sz;
4002 if (bytes_to_round_page) {
4003 if (((uintptr_t)data + sz ) > (UINT_MAX - bytes_to_round_page)) {
4004 fastboot_fail("Integer overflow detected");
4005 return;
4006 }
4007 if (((uintptr_t)data + sz + bytes_to_round_page) >
4008 ((uintptr_t)target_get_scratch_address() + target_get_max_flash_size())) {
4009 fastboot_fail("Buffer size is not aligned to page_size");
4010 return;
4011 }
4012 else {
4013 memset(data + sz, 0, bytes_to_round_page);
4014 sz = rounded_size;
4015 }
Kishor PK38ed93d2017-04-25 14:19:26 +05304016 }
Gaurav Nebhwanid9730712016-05-06 14:28:29 +05304017 }
4018
Kishor PK38ed93d2017-04-25 14:19:26 +05304019 /*Checking partition_size for the possible integer overflow */
4020 partition_size = validate_partition_size(ptn);
4021
Gaurav Nebhwanid9730712016-05-06 14:28:29 +05304022 if (sz > partition_size) {
4023 fastboot_fail("Image size too large");
4024 return;
4025 }
4026
Dima Zavin214cc642009-01-26 11:16:21 -08004027 dprintf(INFO, "writing %d bytes to '%s'\n", sz, ptn->name);
Mayank Grover6cde9352017-06-06 18:45:23 +05304028 if ((sz > UBI_EC_HDR_SIZE) &&
4029 (!memcmp((void *)data, UBI_MAGIC, UBI_MAGIC_SIZE))) {
Tanya Brokhman1c94f1a2015-02-15 09:05:03 +02004030 if (flash_ubi_img(ptn, data, sz)) {
4031 fastboot_fail("flash write failure");
4032 return;
4033 }
4034 } else {
4035 if (flash_write(ptn, extra, data, sz)) {
4036 fastboot_fail("flash write failure");
4037 return;
4038 }
Dima Zavin214cc642009-01-26 11:16:21 -08004039 }
4040 dprintf(INFO, "partition '%s' updated\n", ptn->name);
4041 fastboot_okay("");
4042}
4043
Kishor PK38ed93d2017-04-25 14:19:26 +05304044
4045static inline uint64_t validate_partition_size(struct ptentry *ptn)
4046{
4047 if (ptn->length && flash_num_pages_per_blk() && page_size) {
4048 if ((ptn->length < ( UINT_MAX / flash_num_pages_per_blk())) && ((ptn->length * flash_num_pages_per_blk()) < ( UINT_MAX / page_size))) {
4049 return ptn->length * flash_num_pages_per_blk() * page_size;
4050 }
4051 }
4052 return 0;
4053}
4054
4055
Sridhar Parasurame94e8152014-10-24 14:06:03 -07004056void cmd_flash(const char *arg, void *data, unsigned sz)
4057{
4058 if(target_is_emmc_boot())
4059 cmd_flash_mmc(arg, data, sz);
4060 else
4061 cmd_flash_nand(arg, data, sz);
4062}
4063
Dima Zavin214cc642009-01-26 11:16:21 -08004064void cmd_continue(const char *arg, void *data, unsigned sz)
4065{
4066 fastboot_okay("");
Amol Jadi7c4316c2013-10-07 14:19:26 -07004067 fastboot_stop();
Shashank Mittald3e54dd2014-08-28 15:24:02 -07004068
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004069 if (target_is_emmc_boot())
4070 {
lijuanga40d6302015-07-20 20:10:13 +08004071#if FBCON_DISPLAY_MSG
lijuangde34d502016-02-26 16:04:50 +08004072 /* Exit keys' detection thread firstly */
4073 exit_menu_keys_detection();
lijuanga40d6302015-07-20 20:10:13 +08004074#endif
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004075 boot_linux_from_mmc();
4076 }
4077 else
4078 {
4079 boot_linux_from_flash();
4080 }
Dima Zavin214cc642009-01-26 11:16:21 -08004081}
4082
Chandan Uddaraju94183c02010-01-15 15:13:59 -08004083void cmd_reboot(const char *arg, void *data, unsigned sz)
4084{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004085 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08004086 fastboot_okay("");
4087 reboot_device(0);
4088}
4089
Mayank Grover351a75e2017-05-30 20:06:08 +05304090void cmd_set_active(const char *arg, void *data, unsigned sz)
4091{
Mayank Grover6ccc1c92017-07-04 17:36:46 +05304092 char *p, *sp = NULL;
Mayank Grover351a75e2017-05-30 20:06:08 +05304093 unsigned i,current_active_slot;
4094 const char *current_slot_suffix;
4095
4096 if (!partition_multislot_is_supported())
4097 {
4098 fastboot_fail("Command not supported");
4099 return;
4100 }
4101
4102 if (arg)
4103 {
Mayank Grover6ccc1c92017-07-04 17:36:46 +05304104 p = strtok_r((char *)arg, ":", &sp);
Mayank Grover5eb5f692017-07-19 20:16:04 +05304105 if (p)
Mayank Grover351a75e2017-05-30 20:06:08 +05304106 {
4107 current_active_slot = partition_find_active_slot();
4108
4109 /* Check if trying to make curent slot active */
4110 current_slot_suffix = SUFFIX_SLOT(current_active_slot);
Mayank Grover6ccc1c92017-07-04 17:36:46 +05304111 current_slot_suffix = strtok_r((char *)current_slot_suffix,
4112 (char *)suffix_delimiter, &sp);
4113
Mayank Grover5eb5f692017-07-19 20:16:04 +05304114 if (current_slot_suffix &&
4115 !strncmp(p, current_slot_suffix, strlen(current_slot_suffix)))
Mayank Grover351a75e2017-05-30 20:06:08 +05304116 {
4117 fastboot_okay("Slot already set active");
4118 return;
4119 }
4120 else
4121 {
4122 for (i = 0; i < AB_SUPPORTED_SLOTS; i++)
4123 {
4124 current_slot_suffix = SUFFIX_SLOT(i);
Mayank Grover6ccc1c92017-07-04 17:36:46 +05304125 current_slot_suffix = strtok_r((char *)current_slot_suffix,
4126 (char *)suffix_delimiter, &sp);
Mayank Grover5eb5f692017-07-19 20:16:04 +05304127 if (current_slot_suffix &&
4128 !strncmp(p, current_slot_suffix, strlen(current_slot_suffix)))
Mayank Grover351a75e2017-05-30 20:06:08 +05304129 {
4130 partition_switch_slots(current_active_slot, i);
4131 publish_getvar_multislot_vars();
4132 fastboot_okay("");
4133 return;
4134 }
4135 }
4136 }
4137 }
4138 }
4139 fastboot_fail("Invalid slot suffix.");
4140 return;
4141}
4142
Chandan Uddaraju94183c02010-01-15 15:13:59 -08004143void cmd_reboot_bootloader(const char *arg, void *data, unsigned sz)
4144{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004145 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08004146 fastboot_okay("");
4147 reboot_device(FASTBOOT_MODE);
4148}
4149
Ameya Thakur11cf1a62013-08-05 12:44:48 -07004150void cmd_oem_enable_charger_screen(const char *arg, void *data, unsigned size)
4151{
4152 dprintf(INFO, "Enabling charger screen check\n");
4153 device.charger_screen_enabled = 1;
4154 write_device_info(&device);
4155 fastboot_okay("");
4156}
4157
4158void cmd_oem_disable_charger_screen(const char *arg, void *data, unsigned size)
4159{
4160 dprintf(INFO, "Disabling charger screen check\n");
4161 device.charger_screen_enabled = 0;
4162 write_device_info(&device);
4163 fastboot_okay("");
4164}
4165
lijuanga25d8bb2015-09-16 13:11:52 +08004166void cmd_oem_off_mode_charger(const char *arg, void *data, unsigned size)
4167{
4168 char *p = NULL;
4169 const char *delim = " \t\n\r";
Parth Dixit407f15b2016-01-07 14:47:37 +05304170 char *sp;
lijuanga25d8bb2015-09-16 13:11:52 +08004171
4172 if (arg) {
Parth Dixit407f15b2016-01-07 14:47:37 +05304173 p = strtok_r((char *)arg, delim, &sp);
lijuanga25d8bb2015-09-16 13:11:52 +08004174 if (p) {
4175 if (!strncmp(p, "0", 1)) {
4176 device.charger_screen_enabled = 0;
4177 } else if (!strncmp(p, "1", 1)) {
4178 device.charger_screen_enabled = 1;
4179 }
4180 }
4181 }
4182
4183 /* update charger_screen_enabled value for getvar
4184 * command
4185 */
4186 snprintf(charger_screen_enabled, MAX_RSP_SIZE, "%d",
4187 device.charger_screen_enabled);
4188
4189 write_device_info(&device);
4190 fastboot_okay("");
4191}
4192
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05304193void cmd_oem_select_display_panel(const char *arg, void *data, unsigned size)
4194{
4195 dprintf(INFO, "Selecting display panel %s\n", arg);
4196 if (arg)
4197 strlcpy(device.display_panel, arg,
4198 sizeof(device.display_panel));
4199 write_device_info(&device);
4200 fastboot_okay("");
4201}
4202
Shashank Mittal162244e2011-08-08 19:01:25 -07004203void cmd_oem_unlock(const char *arg, void *data, unsigned sz)
4204{
lijuang4ece1e72015-08-14 21:02:36 +08004205 set_device_unlock(UNLOCK, TRUE);
vijay kumarc65876c2015-04-24 13:29:16 +05304206}
4207
4208void cmd_oem_unlock_go(const char *arg, void *data, unsigned sz)
4209{
lijuang4ece1e72015-08-14 21:02:36 +08004210 if(!device.is_unlocked) {
vijay kumarc65876c2015-04-24 13:29:16 +05304211 if(!is_allow_unlock) {
4212 fastboot_fail("oem unlock is not allowed");
4213 return;
4214 }
4215
lijuang4ece1e72015-08-14 21:02:36 +08004216 set_device_unlock_value(UNLOCK, TRUE);
vijay kumarc65876c2015-04-24 13:29:16 +05304217
lijuang4ece1e72015-08-14 21:02:36 +08004218 /* wipe data */
vijay kumarc65876c2015-04-24 13:29:16 +05304219 struct recovery_message msg;
Kishor PK60a68212017-05-08 16:55:57 +05304220 memset(&msg, 0, sizeof(msg));
vijay kumarc65876c2015-04-24 13:29:16 +05304221 snprintf(msg.recovery, sizeof(msg.recovery), "recovery\n--wipe_data");
4222 write_misc(0, &msg, sizeof(msg));
4223
4224 fastboot_okay("");
4225 reboot_device(RECOVERY_MODE);
Shashank Mittald3e54dd2014-08-28 15:24:02 -07004226 }
4227 fastboot_okay("");
4228}
4229
Channagoud Kadabiad259832015-05-29 11:14:17 -07004230static int aboot_frp_unlock(char *pname, void *data, unsigned sz)
4231{
Mayank Grover70f8b0e2017-05-17 11:49:00 +05304232 int ret=1;
4233 bool authentication_success=false;
Channagoud Kadabiad259832015-05-29 11:14:17 -07004234
Mayank Grover70f8b0e2017-05-17 11:49:00 +05304235 /*
4236 Authentication method not implemented.
4237
4238 OEM to implement, authentication system which on successful validataion,
4239 calls write_allow_oem_unlock() with is_allow_unlock.
4240 */
4241#if 0
4242 authentication_success = oem_specific_auth_mthd();
4243#endif
4244
4245 if (authentication_success)
Channagoud Kadabiad259832015-05-29 11:14:17 -07004246 {
Mayank Grover70f8b0e2017-05-17 11:49:00 +05304247 is_allow_unlock = true;
4248 write_allow_oem_unlock(is_allow_unlock);
4249 ret = 0;
Channagoud Kadabiad259832015-05-29 11:14:17 -07004250 }
4251 return ret;
4252}
4253
Shashank Mittald3e54dd2014-08-28 15:24:02 -07004254void cmd_oem_lock(const char *arg, void *data, unsigned sz)
4255{
lijuang4ece1e72015-08-14 21:02:36 +08004256 set_device_unlock(UNLOCK, FALSE);
Shashank Mittal162244e2011-08-08 19:01:25 -07004257}
4258
Shashank Mittala0032282011-08-26 14:50:11 -07004259void cmd_oem_devinfo(const char *arg, void *data, unsigned sz)
4260{
lijuang511a2b52015-08-14 20:50:51 +08004261 char response[MAX_RSP_SIZE];
Ameya Thakur11cf1a62013-08-05 12:44:48 -07004262 snprintf(response, sizeof(response), "\tDevice tampered: %s", (device.is_tampered ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07004263 fastboot_info(response);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07004264 snprintf(response, sizeof(response), "\tDevice unlocked: %s", (device.is_unlocked ? "true" : "false"));
4265 fastboot_info(response);
Monika Singh292b3e92018-03-17 22:40:23 +05304266#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover912eaa62017-10-26 12:08:53 +05304267 if (VB_M <= target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +05304268 {
4269 snprintf(response, sizeof(response), "\tDevice critical unlocked: %s",
4270 (device.is_unlock_critical ? "true" : "false"));
4271 fastboot_info(response);
4272 }
Parth Dixitddbc7352015-10-18 03:13:31 +05304273#endif
Ameya Thakur11cf1a62013-08-05 12:44:48 -07004274 snprintf(response, sizeof(response), "\tCharger screen enabled: %s", (device.charger_screen_enabled ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07004275 fastboot_info(response);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05304276 snprintf(response, sizeof(response), "\tDisplay panel: %s", (device.display_panel));
4277 fastboot_info(response);
Shashank Mittala0032282011-08-26 14:50:11 -07004278 fastboot_okay("");
4279}
4280
lijuang511a2b52015-08-14 20:50:51 +08004281void cmd_flashing_get_unlock_ability(const char *arg, void *data, unsigned sz)
4282{
4283 char response[MAX_RSP_SIZE];
4284 snprintf(response, sizeof(response), "\tget_unlock_ability: %d", is_allow_unlock);
4285 fastboot_info(response);
4286 fastboot_okay("");
4287}
4288
4289void cmd_flashing_lock_critical(const char *arg, void *data, unsigned sz)
4290{
lijuang4ece1e72015-08-14 21:02:36 +08004291 set_device_unlock(UNLOCK_CRITICAL, FALSE);
lijuang511a2b52015-08-14 20:50:51 +08004292}
4293
4294void cmd_flashing_unlock_critical(const char *arg, void *data, unsigned sz)
4295{
lijuang4ece1e72015-08-14 21:02:36 +08004296 set_device_unlock(UNLOCK_CRITICAL, TRUE);
lijuang511a2b52015-08-14 20:50:51 +08004297}
4298
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07004299void cmd_preflash(const char *arg, void *data, unsigned sz)
4300{
4301 fastboot_okay("");
4302}
4303
Mao Flynn7b379f32015-04-20 00:28:30 +08004304static uint8_t logo_header[LOGO_IMG_HEADER_SIZE];
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304305
Mao Flynn7b379f32015-04-20 00:28:30 +08004306int splash_screen_check_header(logo_img_header *header)
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304307{
Mao Flynn7b379f32015-04-20 00:28:30 +08004308 if (memcmp(header->magic, LOGO_IMG_MAGIC, 8))
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304309 return -1;
Mao Flynn7b379f32015-04-20 00:28:30 +08004310 if (header->width == 0 || header->height == 0)
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304311 return -1;
4312 return 0;
4313}
4314
Mao Flynn7b379f32015-04-20 00:28:30 +08004315int splash_screen_flash()
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07004316{
4317 struct ptentry *ptn;
4318 struct ptable *ptable;
Mao Flynn7b379f32015-04-20 00:28:30 +08004319 struct logo_img_header *header;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07004320 struct fbcon_config *fb_display = NULL;
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08004321
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304322 ptable = flash_get_ptable();
4323 if (ptable == NULL) {
Mao Flynn7b379f32015-04-20 00:28:30 +08004324 dprintf(CRITICAL, "ERROR: Partition table not found\n");
4325 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304326 }
Mao Flynn7b379f32015-04-20 00:28:30 +08004327
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304328 ptn = ptable_find(ptable, "splash");
4329 if (ptn == NULL) {
4330 dprintf(CRITICAL, "ERROR: splash Partition not found\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08004331 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304332 }
Mao Flynn7b379f32015-04-20 00:28:30 +08004333 if (flash_read(ptn, 0, (void *)logo_header, LOGO_IMG_HEADER_SIZE)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304334 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08004335 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304336 }
4337
Mao Flynn7b379f32015-04-20 00:28:30 +08004338 header = (struct logo_img_header *)logo_header;
4339 if (splash_screen_check_header(header)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304340 dprintf(CRITICAL, "ERROR: Boot image header invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08004341 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304342 }
4343
4344 fb_display = fbcon_display();
4345 if (fb_display) {
Mao Flynn7b379f32015-04-20 00:28:30 +08004346 if (header->type && (header->blocks != 0)) { // RLE24 compressed data
4347 uint8_t *base = (uint8_t *) fb_display->base + LOGO_IMG_OFFSET;
4348
4349 /* if the logo is full-screen size, remove "fbcon_clear()" */
4350 if ((header->width != fb_display->width)
4351 || (header->height != fb_display->height))
4352 fbcon_clear();
4353
4354 if (flash_read(ptn + LOGO_IMG_HEADER_SIZE, 0,
4355 (uint32_t *)base,
4356 (header->blocks * 512))) {
4357 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
4358 return -1;
4359 }
4360 fbcon_extract_to_screen(header, base);
4361 return 0;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07004362 }
Mao Flynn7b379f32015-04-20 00:28:30 +08004363
Veera Sundaram Sankaranc33416f2015-09-28 10:25:55 -07004364 if ((header->width > fb_display->width) || (header->height > fb_display->height)) {
4365 dprintf(CRITICAL, "Logo config greater than fb config. Fall back default logo\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08004366 return -1;
4367 }
4368
Vineet Bajaj99291ed2014-09-09 12:29:46 +05304369 uint8_t *base = (uint8_t *) fb_display->base;
Sachin Bhayare619e9e42017-05-15 13:10:31 +05304370 uint32_t fb_size = ROUNDUP(fb_display->width *
4371 fb_display->height *
4372 (fb_display->bpp / 8), 4096);
4373 uint32_t splash_size = ((((header->width * header->height *
4374 fb_display->bpp/8) + 511) >> 9) << 9);
4375
4376 if (splash_size > fb_size) {
4377 dprintf(CRITICAL, "ERROR: Splash image size invalid\n");
4378 return -1;
4379 }
4380
Mao Flynn7b379f32015-04-20 00:28:30 +08004381 if (flash_read(ptn + LOGO_IMG_HEADER_SIZE, 0,
4382 (uint32_t *)base,
4383 ((((header->width * header->height * fb_display->bpp/8) + 511) >> 9) << 9))) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304384 fbcon_clear();
Vineet Bajaj99291ed2014-09-09 12:29:46 +05304385 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08004386 return -1;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07004387 }
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304388 }
4389
Mao Flynn7b379f32015-04-20 00:28:30 +08004390 return 0;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304391}
4392
Mao Flynn7b379f32015-04-20 00:28:30 +08004393int splash_screen_mmc()
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304394{
4395 int index = INVALID_PTN;
4396 unsigned long long ptn = 0;
4397 struct fbcon_config *fb_display = NULL;
Mao Flynn7b379f32015-04-20 00:28:30 +08004398 struct logo_img_header *header;
Mao Flynn1893a7f2015-06-03 12:03:36 +08004399 uint32_t blocksize, realsize, readsize;
4400 uint8_t *base;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304401
4402 index = partition_get_index("splash");
4403 if (index == 0) {
4404 dprintf(CRITICAL, "ERROR: splash Partition table not found\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08004405 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304406 }
4407
4408 ptn = partition_get_offset(index);
4409 if (ptn == 0) {
4410 dprintf(CRITICAL, "ERROR: splash Partition invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08004411 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304412 }
4413
Mao Flynn1893a7f2015-06-03 12:03:36 +08004414 mmc_set_lun(partition_get_lun(index));
4415
4416 blocksize = mmc_get_device_blocksize();
4417 if (blocksize == 0) {
4418 dprintf(CRITICAL, "ERROR:splash Partition invalid blocksize\n");
4419 return -1;
4420 }
4421
4422 fb_display = fbcon_display();
Channagoud Kadabidaf10a62015-07-22 11:51:55 -07004423 if (!fb_display)
4424 {
4425 dprintf(CRITICAL, "ERROR: fb config is not allocated\n");
4426 return -1;
4427 }
4428
Mao Flynn1893a7f2015-06-03 12:03:36 +08004429 base = (uint8_t *) fb_display->base;
4430
Kishor PKee5c0a32018-03-06 16:49:46 +05304431 if (mmc_read(ptn + PLL_CODES_OFFSET, (uint32_t *)(base + LOGO_IMG_OFFSET), blocksize)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304432 dprintf(CRITICAL, "ERROR: Cannot read splash image header\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08004433 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304434 }
4435
Mao Flynn1893a7f2015-06-03 12:03:36 +08004436 header = (struct logo_img_header *)(base + LOGO_IMG_OFFSET);
Mao Flynn7b379f32015-04-20 00:28:30 +08004437 if (splash_screen_check_header(header)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304438 dprintf(CRITICAL, "ERROR: Splash image header invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08004439 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304440 }
4441
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304442 if (fb_display) {
Mao Flynn1893a7f2015-06-03 12:03:36 +08004443 if (header->type && (header->blocks != 0)) { /* 1 RLE24 compressed data */
4444 base += LOGO_IMG_OFFSET;
Mao Flynn7b379f32015-04-20 00:28:30 +08004445
Mao Flynn1893a7f2015-06-03 12:03:36 +08004446 realsize = header->blocks * 512;
4447 readsize = ROUNDUP((realsize + LOGO_IMG_HEADER_SIZE), blocksize) - blocksize;
4448
4449 /* if the logo is not full-screen size, clean screen */
Mao Flynn7b379f32015-04-20 00:28:30 +08004450 if ((header->width != fb_display->width)
4451 || (header->height != fb_display->height))
4452 fbcon_clear();
4453
Sachin Bhayare619e9e42017-05-15 13:10:31 +05304454 uint32_t fb_size = ROUNDUP(fb_display->width *
4455 fb_display->height *
4456 (fb_display->bpp / 8), 4096);
4457
4458 if (readsize > fb_size) {
4459 dprintf(CRITICAL, "ERROR: Splash image size invalid\n");
4460 return -1;
4461 }
4462
Kishor PKee5c0a32018-03-06 16:49:46 +05304463 if (mmc_read(ptn + PLL_CODES_OFFSET + blocksize, (uint32_t *)(base + blocksize), readsize)) {
Mao Flynn7b379f32015-04-20 00:28:30 +08004464 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
4465 return -1;
4466 }
Mao Flynn7b379f32015-04-20 00:28:30 +08004467
Mao Flynn1893a7f2015-06-03 12:03:36 +08004468 fbcon_extract_to_screen(header, (base + LOGO_IMG_HEADER_SIZE));
4469 } else { /* 2 Raw BGR data */
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304470
Veera Sundaram Sankaranc33416f2015-09-28 10:25:55 -07004471 if ((header->width > fb_display->width) || (header->height > fb_display->height)) {
4472 dprintf(CRITICAL, "Logo config greater than fb config. Fall back default logo\n");
Mao Flynn1893a7f2015-06-03 12:03:36 +08004473 return -1;
4474 }
4475
4476 realsize = header->width * header->height * fb_display->bpp / 8;
4477 readsize = ROUNDUP((realsize + LOGO_IMG_HEADER_SIZE), blocksize) - blocksize;
4478
4479 if (blocksize == LOGO_IMG_HEADER_SIZE) { /* read the content directly */
Kishor PKee5c0a32018-03-06 16:49:46 +05304480 if (mmc_read((ptn + PLL_CODES_OFFSET + LOGO_IMG_HEADER_SIZE), (uint32_t *)base, readsize)) {
Mao Flynn1893a7f2015-06-03 12:03:36 +08004481 fbcon_clear();
4482 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
4483 return -1;
4484 }
4485 } else {
Kishor PKee5c0a32018-03-06 16:49:46 +05304486 if (mmc_read(ptn + PLL_CODES_OFFSET + blocksize ,
Mao Flynn1893a7f2015-06-03 12:03:36 +08004487 (uint32_t *)(base + LOGO_IMG_OFFSET + blocksize), readsize)) {
4488 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
4489 return -1;
4490 }
4491 memmove(base, (base + LOGO_IMG_OFFSET + LOGO_IMG_HEADER_SIZE), realsize);
4492 }
4493 }
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304494 }
4495
Mao Flynn7b379f32015-04-20 00:28:30 +08004496 return 0;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304497}
4498
Mao Flynn7b379f32015-04-20 00:28:30 +08004499int fetch_image_from_partition()
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304500{
4501 if (target_is_emmc_boot()) {
4502 return splash_screen_mmc();
4503 } else {
4504 return splash_screen_flash();
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07004505 }
4506}
4507
Mayank Grover351a75e2017-05-30 20:06:08 +05304508void publish_getvar_multislot_vars()
4509{
4510 int i,count;
4511 static bool published = false;
4512 static char slot_count[MAX_RSP_SIZE];
4513 static struct ab_slot_info slot_info[AB_SUPPORTED_SLOTS];
4514 static char active_slot_suffix[MAX_RSP_SIZE];
4515 static char has_slot_pname[NUM_PARTITIONS][MAX_GET_VAR_NAME_SIZE];
4516 static char has_slot_reply[NUM_PARTITIONS][MAX_RSP_SIZE];
4517 const char *tmp;
4518 char tmpbuff[MAX_GET_VAR_NAME_SIZE];
Mayank Grover2bd84bb2017-06-20 11:06:07 +05304519 signed active_slt;
Mayank Grover351a75e2017-05-30 20:06:08 +05304520
4521 if (!published)
4522 {
4523 /* Update slot meta info */
4524 count = partition_fill_partition_meta(has_slot_pname, has_slot_reply,
4525 partition_get_partition_count());
4526 for(i=0; i<count; i++)
4527 {
4528 memset(tmpbuff, 0, MAX_GET_VAR_NAME_SIZE);
Mayank Grover2bd84bb2017-06-20 11:06:07 +05304529 snprintf(tmpbuff, MAX_GET_VAR_NAME_SIZE,"has-slot:%s",
4530 has_slot_pname[i]);
4531 strlcpy(has_slot_pname[i], tmpbuff, MAX_GET_VAR_NAME_SIZE);
Mayank Grover351a75e2017-05-30 20:06:08 +05304532 fastboot_publish(has_slot_pname[i], has_slot_reply[i]);
4533 }
4534
4535 for (i=0; i<AB_SUPPORTED_SLOTS; i++)
4536 {
4537 tmp = SUFFIX_SLOT(i);
Mayank Grover7b8a8f82017-10-27 13:07:59 +05304538 tmp++; // to remove "_" from slot_suffix.
Mayank Grover2bd84bb2017-06-20 11:06:07 +05304539 snprintf(slot_info[i].slot_is_unbootable, sizeof(slot_info[i].slot_is_unbootable),
4540 "slot-unbootable:%s", tmp);
4541 snprintf(slot_info[i].slot_is_active, sizeof(slot_info[i].slot_is_active),
4542 "slot-active:%s", tmp);
4543 snprintf(slot_info[i].slot_is_succesful, sizeof(slot_info[i].slot_is_succesful),
4544 "slot-success:%s", tmp);
4545 snprintf(slot_info[i].slot_retry_count, sizeof(slot_info[i].slot_retry_count),
4546 "slot-retry-count:%s", tmp);
Mayank Grover351a75e2017-05-30 20:06:08 +05304547 fastboot_publish(slot_info[i].slot_is_unbootable,
4548 slot_info[i].slot_is_unbootable_rsp);
4549 fastboot_publish(slot_info[i].slot_is_active,
4550 slot_info[i].slot_is_active_rsp);
4551 fastboot_publish(slot_info[i].slot_is_succesful,
4552 slot_info[i].slot_is_succesful_rsp);
4553 fastboot_publish(slot_info[i].slot_retry_count,
4554 slot_info[i].slot_retry_count_rsp);
4555 }
4556 fastboot_publish("current-slot", active_slot_suffix);
4557 snprintf(slot_count, sizeof(slot_count),"%d", AB_SUPPORTED_SLOTS);
4558 fastboot_publish("slot-count", slot_count);
4559 published = true;
4560 }
4561
Mayank Grover2bd84bb2017-06-20 11:06:07 +05304562 active_slt = partition_find_active_slot();
4563 if (active_slt != INVALID)
Mayank Grover7b8a8f82017-10-27 13:07:59 +05304564 {
4565 tmp = SUFFIX_SLOT(active_slt);
4566 tmp++; // to remove "_" from slot_suffix.
4567 snprintf(active_slot_suffix, sizeof(active_slot_suffix), "%s", tmp);
4568 }
Mayank Grover2bd84bb2017-06-20 11:06:07 +05304569 else
4570 strlcpy(active_slot_suffix, "INVALID", sizeof(active_slot_suffix));
4571
Mayank Grover351a75e2017-05-30 20:06:08 +05304572 /* Update partition meta information */
4573 partition_fill_slot_meta(slot_info);
4574 return;
4575}
4576
lijuang4ece1e72015-08-14 21:02:36 +08004577void get_product_name(unsigned char *buf)
4578{
4579 snprintf((char*)buf, MAX_RSP_SIZE, "%s", TARGET(BOARD));
4580 return;
4581}
4582
Mayank Grover7b8a8f82017-10-27 13:07:59 +05304583#if PRODUCT_IOT
4584void get_bootloader_version_iot(unsigned char *buf)
4585{
4586 if (buf != NULL)
4587 {
4588 strlcpy(buf, TARGET(BOARD), MAX_VERSION_LEN);
4589 strlcat(buf, "-", MAX_VERSION_LEN);
4590 strlcat(buf, PRODUCT_IOT_VERSION, MAX_VERSION_LEN);
4591 }
4592 return;
4593}
4594#endif
4595
lijuang4ece1e72015-08-14 21:02:36 +08004596void get_bootloader_version(unsigned char *buf)
4597{
4598 snprintf((char*)buf, MAX_RSP_SIZE, "%s", device.bootloader_version);
4599 return;
4600}
4601
4602void get_baseband_version(unsigned char *buf)
4603{
4604 snprintf((char*)buf, MAX_RSP_SIZE, "%s", device.radio_version);
4605 return;
4606}
4607
Monika Singh32a09f72018-03-14 13:08:29 +05304608bool is_device_locked_critical()
4609{
4610 return device.is_unlock_critical ? false:true;
4611}
4612
lijuang4ece1e72015-08-14 21:02:36 +08004613bool is_device_locked()
4614{
4615 return device.is_unlocked ? false:true;
4616}
4617
Monika Singh32a09f72018-03-14 13:08:29 +05304618bool is_verity_enforcing()
4619{
4620 return device.verity_mode ? true:false;
4621}
4622
Amol Jadi5edf3552013-07-23 14:15:34 -07004623/* register commands and variables for fastboot */
4624void aboot_fastboot_register_commands(void)
4625{
Sridhar Parasurame94e8152014-10-24 14:06:03 -07004626 int i;
lijuangf16461c2015-08-03 17:09:34 +08004627 char hw_platform_buf[MAX_RSP_SIZE];
Amol Jadi5edf3552013-07-23 14:15:34 -07004628
Sridhar Parasurame94e8152014-10-24 14:06:03 -07004629 struct fastboot_cmd_desc cmd_list[] = {
lijuang511a2b52015-08-14 20:50:51 +08004630 /* By default the enabled list is empty. */
4631 {"", NULL},
4632 /* move commands enclosed within the below ifndef to here
4633 * if they need to be enabled in user build.
4634 */
Sridhar Parasurame94e8152014-10-24 14:06:03 -07004635#ifndef DISABLE_FASTBOOT_CMDS
lijuang511a2b52015-08-14 20:50:51 +08004636 /* Register the following commands only for non-user builds */
4637 {"flash:", cmd_flash},
4638 {"erase:", cmd_erase},
4639 {"boot", cmd_boot},
4640 {"continue", cmd_continue},
4641 {"reboot", cmd_reboot},
4642 {"reboot-bootloader", cmd_reboot_bootloader},
4643 {"oem unlock", cmd_oem_unlock},
4644 {"oem unlock-go", cmd_oem_unlock_go},
4645 {"oem lock", cmd_oem_lock},
4646 {"flashing unlock", cmd_oem_unlock},
4647 {"flashing lock", cmd_oem_lock},
4648 {"flashing lock_critical", cmd_flashing_lock_critical},
4649 {"flashing unlock_critical", cmd_flashing_unlock_critical},
4650 {"flashing get_unlock_ability", cmd_flashing_get_unlock_ability},
4651 {"oem device-info", cmd_oem_devinfo},
4652 {"preflash", cmd_preflash},
4653 {"oem enable-charger-screen", cmd_oem_enable_charger_screen},
4654 {"oem disable-charger-screen", cmd_oem_disable_charger_screen},
lijuanga25d8bb2015-09-16 13:11:52 +08004655 {"oem off-mode-charge", cmd_oem_off_mode_charger},
lijuang511a2b52015-08-14 20:50:51 +08004656 {"oem select-display-panel", cmd_oem_select_display_panel},
Mayank Grover6ccc1c92017-07-04 17:36:46 +05304657 {"set_active",cmd_set_active},
Channagoud Kadabi736c4962015-08-21 11:56:52 -07004658#if UNITTEST_FW_SUPPORT
Channagoud Kadabid5ddf482015-09-28 10:31:35 -07004659 {"oem run-tests", cmd_oem_runtests},
Channagoud Kadabi736c4962015-08-21 11:56:52 -07004660#endif
Sridhar Parasurame94e8152014-10-24 14:06:03 -07004661#endif
lijuang511a2b52015-08-14 20:50:51 +08004662 };
Sridhar Parasurame94e8152014-10-24 14:06:03 -07004663
4664 int fastboot_cmds_count = sizeof(cmd_list)/sizeof(cmd_list[0]);
4665 for (i = 1; i < fastboot_cmds_count; i++)
4666 fastboot_register(cmd_list[i].name,cmd_list[i].cb);
4667
Amol Jadi5edf3552013-07-23 14:15:34 -07004668 /* publish variables and their values */
4669 fastboot_publish("product", TARGET(BOARD));
4670 fastboot_publish("kernel", "lk");
4671 fastboot_publish("serialno", sn_buf);
4672
Mayank Grovera64dfbe2018-05-17 14:06:34 +05304673 /*publish hw-revision major(upper 16 bits) and minor(lower 16 bits)*/
4674 snprintf(soc_version_str, MAX_RSP_SIZE, "%x", board_soc_version());
4675 fastboot_publish("hw-revision", soc_version_str);
4676
Amol Jadi5edf3552013-07-23 14:15:34 -07004677 /*
4678 * partition info is supported only for emmc partitions
4679 * Calling this for NAND prints some error messages which
4680 * is harmless but misleading. Avoid calling this for NAND
4681 * devices.
4682 */
4683 if (target_is_emmc_boot())
Mayank Grover7b8a8f82017-10-27 13:07:59 +05304684 publish_getvar_partition_info(part_info, partition_get_partition_count());
Amol Jadi5edf3552013-07-23 14:15:34 -07004685
Mayank Grover351a75e2017-05-30 20:06:08 +05304686 if (partition_multislot_is_supported())
4687 publish_getvar_multislot_vars();
4688
Amol Jadi5edf3552013-07-23 14:15:34 -07004689 /* Max download size supported */
Ameya Thakur11cf1a62013-08-05 12:44:48 -07004690 snprintf(max_download_size, MAX_RSP_SIZE, "\t0x%x",
4691 target_get_max_flash_size());
Amol Jadi5edf3552013-07-23 14:15:34 -07004692 fastboot_publish("max-download-size", (const char *) max_download_size);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07004693 /* Is the charger screen check enabled */
4694 snprintf(charger_screen_enabled, MAX_RSP_SIZE, "%d",
4695 device.charger_screen_enabled);
4696 fastboot_publish("charger-screen-enabled",
4697 (const char *) charger_screen_enabled);
lijuanga25d8bb2015-09-16 13:11:52 +08004698 fastboot_publish("off-mode-charge", (const char *) charger_screen_enabled);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05304699 snprintf(panel_display_mode, MAX_RSP_SIZE, "%s",
4700 device.display_panel);
4701 fastboot_publish("display-panel",
4702 (const char *) panel_display_mode);
Mukesh Ojhabb6e7f32018-03-02 15:04:06 +05304703
4704 if (target_is_emmc_boot())
4705 {
4706 mmc_blocksize = mmc_get_device_blocksize();
4707 }
4708 else
4709 {
4710 mmc_blocksize = flash_block_size();
4711 }
4712 snprintf(block_size_string, MAX_RSP_SIZE, "0x%x", mmc_blocksize);
4713 fastboot_publish("erase-block-size", (const char *) block_size_string);
4714 fastboot_publish("logical-block-size", (const char *) block_size_string);
Mayank Grover7b8a8f82017-10-27 13:07:59 +05304715#if PRODUCT_IOT
4716 get_bootloader_version_iot(&bootloader_version_string);
4717 fastboot_publish("version-bootloader", (const char *) bootloader_version_string);
4718
4719 /* Version baseband is n/a for apq iot devices */
4720 fastboot_publish("version-baseband", "N/A");
Mayank Grover7b8a8f82017-10-27 13:07:59 +05304721#else
Channagoud Kadabiaafbbfc2015-03-04 17:52:56 -08004722 fastboot_publish("version-bootloader", (const char *) device.bootloader_version);
4723 fastboot_publish("version-baseband", (const char *) device.radio_version);
Mayank Grover7b8a8f82017-10-27 13:07:59 +05304724#endif
lijuangf16461c2015-08-03 17:09:34 +08004725 fastboot_publish("secure", is_secure_boot_enable()? "yes":"no");
Mayank Grover7b8a8f82017-10-27 13:07:59 +05304726 fastboot_publish("unlocked", device.is_unlocked ? "yes":"no");
lijuangf16461c2015-08-03 17:09:34 +08004727 smem_get_hw_platform_name((unsigned char *) hw_platform_buf, sizeof(hw_platform_buf));
4728 snprintf(get_variant, MAX_RSP_SIZE, "%s %s", hw_platform_buf,
4729 target_is_emmc_boot()? "eMMC":"UFS");
4730 fastboot_publish("variant", (const char *) get_variant);
lijuang65c5a822015-08-29 16:35:36 +08004731#if CHECK_BAT_VOLTAGE
lijuang102dfa92015-10-09 18:31:03 +08004732 update_battery_status();
lijuang65c5a822015-08-29 16:35:36 +08004733 fastboot_publish("battery-voltage", (const char *) battery_voltage);
lijuang102dfa92015-10-09 18:31:03 +08004734 fastboot_publish("battery-soc-ok", (const char *) battery_soc_ok);
lijuang65c5a822015-08-29 16:35:36 +08004735#endif
Amol Jadi5edf3552013-07-23 14:15:34 -07004736}
4737
Brian Swetland9c4c0752009-01-25 16:23:50 -08004738void aboot_init(const struct app_descriptor *app)
4739{
Shashank Mittal4f99a882010-02-01 13:58:50 -08004740 unsigned reboot_mode = 0;
Mayank Grover351a75e2017-05-30 20:06:08 +05304741 int boot_err_type = 0;
4742 int boot_slot = INVALID;
Chandan Uddarajubedca152010-06-02 23:05:15 -07004743
lijuang39831732016-01-08 17:49:02 +08004744 /* Initialise wdog to catch early lk crashes */
4745#if WDOG_SUPPORT
4746 msm_wdog_init();
4747#endif
4748
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07004749 /* Setup page size information for nv storage */
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004750 if (target_is_emmc_boot())
4751 {
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07004752 page_size = mmc_page_size();
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004753 page_mask = page_size - 1;
Gaurav Nebhwani4d2389c2016-03-17 21:10:05 +05304754 mmc_blocksize = mmc_get_device_blocksize();
4755 mmc_blocksize_mask = mmc_blocksize - 1;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004756 }
4757 else
4758 {
4759 page_size = flash_page_size();
4760 page_mask = page_size - 1;
4761 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07004762 ASSERT((MEMBASE + MEMSIZE) > MEMBASE);
4763
Ameya Thakur11cf1a62013-08-05 12:44:48 -07004764 read_device_info(&device);
vijay kumarc65876c2015-04-24 13:29:16 +05304765 read_allow_oem_unlock(&device);
Shashank Mittal162244e2011-08-08 19:01:25 -07004766
Mayank Grover351a75e2017-05-30 20:06:08 +05304767 /* Detect multi-slot support */
4768 if (partition_multislot_is_supported())
4769 {
4770 boot_slot = partition_find_active_slot();
4771 if (boot_slot == INVALID)
4772 {
4773 boot_into_fastboot = true;
4774 dprintf(INFO, "Active Slot: (INVALID)\n");
4775 }
4776 else
4777 {
4778 /* Setting the state of system to boot active slot */
4779 partition_mark_active_slot(boot_slot);
4780 dprintf(INFO, "Active Slot: (%s)\n", SUFFIX_SLOT(boot_slot));
4781 }
4782 }
4783
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08004784 /* Display splash screen if enabled */
4785#if DISPLAY_SPLASH_SCREEN
lijuang99c02d82015-02-13 19:04:34 +08004786#if NO_ALARM_DISPLAY
4787 if (!check_alarm_boot()) {
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08004788#endif
lijuang99c02d82015-02-13 19:04:34 +08004789 dprintf(SPEW, "Display Init: Start\n");
Ajay Singh Parmara7865a82015-04-16 21:27:52 -07004790#if DISPLAY_HDMI_PRIMARY
4791 if (!strlen(device.display_panel))
4792 strlcpy(device.display_panel, DISPLAY_PANEL_HDMI,
4793 sizeof(device.display_panel));
4794#endif
Channagoud Kadabi90869ce2015-04-27 11:15:14 -07004795#if ENABLE_WBC
Channagoud Kadabid801ac72015-08-26 11:21:51 -07004796 /* Wait if the display shutdown is in progress */
4797 while(pm_app_display_shutdown_in_prgs());
Channagoud Kadabi90869ce2015-04-27 11:15:14 -07004798 if (!pm_appsbl_display_init_done())
4799 target_display_init(device.display_panel);
4800 else
4801 display_image_on_screen();
4802#else
lijuang99c02d82015-02-13 19:04:34 +08004803 target_display_init(device.display_panel);
Channagoud Kadabi90869ce2015-04-27 11:15:14 -07004804#endif
lijuang99c02d82015-02-13 19:04:34 +08004805 dprintf(SPEW, "Display Init: Done\n");
4806#if NO_ALARM_DISPLAY
4807 }
4808#endif
4809#endif
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08004810
Greg Griscod6250552011-06-29 14:40:23 -07004811 target_serialno((unsigned char *) sn_buf);
Ajay Dudanib06c05f2011-05-12 14:46:10 -07004812 dprintf(SPEW,"serial number: %s\n",sn_buf);
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -08004813
Dhaval Patel223ec952013-07-18 14:49:44 -07004814 memset(display_panel_buf, '\0', MAX_PANEL_BUF_SIZE);
4815
Matthew Qindefd5562014-07-11 18:02:40 +08004816 /*
4817 * Check power off reason if user force reset,
4818 * if yes phone will do normal boot.
4819 */
4820 if (is_user_force_reset())
4821 goto normal_boot;
4822
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07004823 /* Check if we should do something other than booting up */
Ameya Thakur0b9c2442013-05-10 13:22:56 -07004824 if (keys_get_state(KEY_VOLUMEUP) && keys_get_state(KEY_VOLUMEDOWN))
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07004825 {
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03004826 dprintf(ALWAYS,"dload mode key sequence detected\n");
lijuang395b5e62015-11-19 17:39:44 +08004827 reboot_device(EMERGENCY_DLOAD);
4828 dprintf(CRITICAL,"Failed to reboot into dload mode\n");
4829
Ameya Thakur0b9c2442013-05-10 13:22:56 -07004830 boot_into_fastboot = true;
4831 }
4832 if (!boot_into_fastboot)
4833 {
4834 if (keys_get_state(KEY_HOME) || keys_get_state(KEY_VOLUMEUP))
4835 boot_into_recovery = 1;
4836 if (!boot_into_recovery &&
4837 (keys_get_state(KEY_BACK) || keys_get_state(KEY_VOLUMEDOWN)))
Pavel Nedev5d91d412013-04-29 11:34:24 +03004838 boot_into_fastboot = true;
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07004839 }
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004840 #if NO_KEYPAD_DRIVER
Kinson Chik0b1c8162011-08-31 16:31:57 -07004841 if (fastboot_trigger())
Pavel Nedev5d91d412013-04-29 11:34:24 +03004842 boot_into_fastboot = true;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004843 #endif
Chandan Uddarajubedca152010-06-02 23:05:15 -07004844
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07004845#if USE_PON_REBOOT_REG
4846 reboot_mode = check_hard_reboot_mode();
4847#else
Ajay Dudani77421292010-10-27 19:34:06 -07004848 reboot_mode = check_reboot_mode();
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07004849#endif
4850 if (reboot_mode == RECOVERY_MODE)
4851 {
Ajay Dudani77421292010-10-27 19:34:06 -07004852 boot_into_recovery = 1;
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07004853 }
4854 else if(reboot_mode == FASTBOOT_MODE)
4855 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03004856 boot_into_fastboot = true;
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07004857 }
4858 else if(reboot_mode == ALARM_BOOT)
4859 {
Matthew Qind886f3c2014-01-17 16:52:01 +08004860 boot_reason_alarm = true;
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07004861 }
Parth Dixit207573a2015-10-27 17:26:21 +05304862#if VERIFIED_BOOT
Mayank Grover912eaa62017-10-26 12:08:53 +05304863 else if (VB_M <= target_get_vb_version())
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07004864 {
Mayank Grover889be1b2017-09-12 20:12:23 +05304865 if (reboot_mode == DM_VERITY_ENFORCING)
4866 {
4867 device.verity_mode = 1;
4868 write_device_info(&device);
4869 }
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07004870#if ENABLE_VB_ATTEST
Mayank Grover889be1b2017-09-12 20:12:23 +05304871 else if (reboot_mode == DM_VERITY_EIO)
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07004872#else
Mayank Grover889be1b2017-09-12 20:12:23 +05304873 else if (reboot_mode == DM_VERITY_LOGGING)
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07004874#endif
Mayank Grover889be1b2017-09-12 20:12:23 +05304875 {
4876 device.verity_mode = 0;
4877 write_device_info(&device);
4878 }
4879 else if (reboot_mode == DM_VERITY_KEYSCLEAR)
4880 {
4881 if(send_delete_keys_to_tz())
4882 ASSERT(0);
4883 }
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07004884 }
Parth Dixitddbc7352015-10-18 03:13:31 +05304885#endif
Ajay Dudani77421292010-10-27 19:34:06 -07004886
Matthew Qindefd5562014-07-11 18:02:40 +08004887normal_boot:
Pavel Nedev5d91d412013-04-29 11:34:24 +03004888 if (!boot_into_fastboot)
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004889 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03004890 if (target_is_emmc_boot())
Shashank Mittala0032282011-08-26 14:50:11 -07004891 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03004892 if(emmc_recovery_init())
4893 dprintf(ALWAYS,"error in emmc_recovery_init\n");
4894 if(target_use_signed_kernel())
Shashank Mittala0032282011-08-26 14:50:11 -07004895 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03004896 if((device.is_unlocked) || (device.is_tampered))
4897 {
4898 #ifdef TZ_TAMPER_FUSE
Monika Singh98257462018-06-06 11:28:49 +05304899 set_tamper_fuse_cmd(HLOS_IMG_TAMPER_FUSE);
Pavel Nedev5d91d412013-04-29 11:34:24 +03004900 #endif
4901 #if USE_PCOM_SECBOOT
4902 set_tamper_flag(device.is_tampered);
4903 #endif
4904 }
Shashank Mittala0032282011-08-26 14:50:11 -07004905 }
Amit Blay6281ebc2015-01-11 14:44:08 +02004906
Mayank Grover351a75e2017-05-30 20:06:08 +05304907retry_boot:
4908 /* Trying to boot active partition */
4909 if (partition_multislot_is_supported())
4910 {
4911 boot_slot = partition_find_boot_slot();
4912 partition_mark_active_slot(boot_slot);
4913 if (boot_slot == INVALID)
4914 goto fastboot;
4915 }
4916
4917 boot_err_type = boot_linux_from_mmc();
4918 switch (boot_err_type)
4919 {
4920 case ERR_INVALID_PAGE_SIZE:
4921 case ERR_DT_PARSE:
4922 case ERR_ABOOT_ADDR_OVERLAP:
Mayank Grover10cabbe2017-10-30 19:11:05 +05304923 case ERR_INVALID_BOOT_MAGIC:
Mayank Grover351a75e2017-05-30 20:06:08 +05304924 if(partition_multislot_is_supported())
Mayank Grover10cabbe2017-10-30 19:11:05 +05304925 {
4926 /*
4927 * Deactivate current slot, as it failed to
4928 * boot, and retry next slot.
4929 */
4930 partition_deactivate_slot(boot_slot);
Mayank Grover351a75e2017-05-30 20:06:08 +05304931 goto retry_boot;
Mayank Grover10cabbe2017-10-30 19:11:05 +05304932 }
Mayank Grover351a75e2017-05-30 20:06:08 +05304933 else
4934 break;
Mayank Grover351a75e2017-05-30 20:06:08 +05304935 default:
4936 break;
4937 /* going to fastboot menu */
4938 }
Shashank Mittala0032282011-08-26 14:50:11 -07004939 }
Pavel Nedev5d91d412013-04-29 11:34:24 +03004940 else
4941 {
4942 recovery_init();
4943 #if USE_PCOM_SECBOOT
4944 if((device.is_unlocked) || (device.is_tampered))
4945 set_tamper_flag(device.is_tampered);
4946 #endif
4947 boot_linux_from_flash();
4948 }
4949 dprintf(CRITICAL, "ERROR: Could not do normal boot. Reverting "
4950 "to fastboot mode.\n");
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004951 }
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07004952
Mayank Grover351a75e2017-05-30 20:06:08 +05304953fastboot:
Amol Jadi5edf3552013-07-23 14:15:34 -07004954 /* We are here means regular boot did not happen. Start fastboot. */
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07004955
Amol Jadi5edf3552013-07-23 14:15:34 -07004956 /* register aboot specific fastboot commands */
4957 aboot_fastboot_register_commands();
Amol Jadi57abe4c2011-05-24 15:47:27 -07004958
Amol Jadi5edf3552013-07-23 14:15:34 -07004959 /* dump partition table for debug info */
Kinson Chikf1a43512011-07-14 11:28:39 -07004960 partition_dump();
Amol Jadi5edf3552013-07-23 14:15:34 -07004961
4962 /* initialize and start fastboot */
4963 fastboot_init(target_get_scratch_address(), target_get_max_flash_size());
lijuang4ece1e72015-08-14 21:02:36 +08004964#if FBCON_DISPLAY_MSG
lijuang9a7d3b92015-11-30 14:41:24 +08004965 display_fastboot_menu();
lijuang4ece1e72015-08-14 21:02:36 +08004966#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08004967}
4968
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07004969uint32_t get_page_size()
4970{
4971 return page_size;
4972}
4973
Amir Samuelov57a6fa22013-06-05 16:36:43 +03004974/*
4975 * Calculated and save hash (SHA256) for non-signed boot image.
4976 *
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07004977 * @param image_addr - Boot image address
4978 * @param image_size - Size of the boot image
Amir Samuelov57a6fa22013-06-05 16:36:43 +03004979 *
4980 * @return int - 0 on success, negative value on failure.
4981 */
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07004982static int aboot_save_boot_hash_mmc(uint32_t image_addr, uint32_t image_size)
Amir Samuelov57a6fa22013-06-05 16:36:43 +03004983{
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07004984 unsigned int digest[8];
4985#if IMAGE_VERIF_ALGO_SHA1
4986 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA1;
4987#else
4988 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA256;
4989#endif
Amir Samuelov57a6fa22013-06-05 16:36:43 +03004990
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07004991 target_crypto_init_params();
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -08004992 hash_find((unsigned char *) image_addr, image_size, (unsigned char *)&digest, auth_algo);
Amir Samuelov57a6fa22013-06-05 16:36:43 +03004993
4994 save_kernel_hash_cmd(digest);
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07004995 dprintf(INFO, "aboot_save_boot_hash_mmc: imagesize_actual size %d bytes.\n", (int) image_size);
Amir Samuelov57a6fa22013-06-05 16:36:43 +03004996
4997 return 0;
4998}
4999
Mayank Grover351a75e2017-05-30 20:06:08 +05305000
Brian Swetland9c4c0752009-01-25 16:23:50 -08005001APP_START(aboot)
5002 .init = aboot_init,
5003APP_END