blob: 62cc67a5afbd5f5d0ad852a0c36e308b09816805 [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";
lijuang83ef4b22018-08-23 11:01:55 +0800208
209#define MAX_DTBO_IDX_STR 64
210static const char *android_boot_dtbo_idx = " androidboot.dtbo_idx=";
Sourabh Banerjee386b1322018-02-27 09:37:28 +0530211#endif
Ajay Dudanid04110c2011-01-17 23:55:07 -0800212
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700213#if VERIFIED_BOOT
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700214static const char *verity_mode = " androidboot.veritymode=";
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700215static const char *verified_state= " androidboot.verifiedbootstate=";
Parth Dixita5715a02015-10-29 12:25:10 +0530216static const char *keymaster_v1= " androidboot.keymaster=1";
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700217//indexed based on enum values, green is 0 by default
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700218
219struct verified_boot_verity_mode vbvm[] =
220{
Channagoud Kadabi86b0c112016-03-16 19:23:16 -0700221#if ENABLE_VB_ATTEST
222 {false, "eio"},
223#else
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700224 {false, "logging"},
Channagoud Kadabi86b0c112016-03-16 19:23:16 -0700225#endif
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700226 {true, "enforcing"},
227};
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700228struct verified_boot_state_name vbsn[] =
229{
230 {GREEN, "green"},
231 {ORANGE, "orange"},
232 {YELLOW,"yellow"},
233 {RED,"red" },
234};
235#endif
Channagoud Kadabi86b0c112016-03-16 19:23:16 -0700236/*As per spec delay wait time before shutdown in Red state*/
237#define DELAY_WAIT 30000
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700238static unsigned page_size = 0;
239static unsigned page_mask = 0;
Gaurav Nebhwani4d2389c2016-03-17 21:10:05 +0530240static unsigned mmc_blocksize = 0;
241static unsigned mmc_blocksize_mask = 0;
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700242static char ffbm_mode_string[FFBM_MODE_BUF_SIZE];
243static bool boot_into_ffbm;
vijay kumar870515d2015-08-31 16:37:24 +0530244static char *target_boot_params = NULL;
Matthew Qind886f3c2014-01-17 16:52:01 +0800245static bool boot_reason_alarm;
Channagoud Kadabi80a182b2015-03-11 17:04:23 -0700246static bool devinfo_present = true;
Channagoud Kadabi736c4962015-08-21 11:56:52 -0700247bool boot_into_fastboot = false;
Parth Dixit4097b622016-03-15 14:42:27 +0530248static uint32_t dt_size = 0;
Monika Singh292b3e92018-03-17 22:40:23 +0530249static char *vbcmdline;
250static bootinfo info = {0};
Shashank Mittalcd98d472011-08-02 14:29:24 -0700251/* Assuming unauthorized kernel image by default */
252static int auth_kernel_img = 0;
Monika Singh292b3e92018-03-17 22:40:23 +0530253static device_info device = {DEVICE_MAGIC,0,0,0,0,{0},{0},{0},1,{0},0,{0}};
254
vijay kumarc65876c2015-04-24 13:29:16 +0530255static bool is_allow_unlock = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -0700256
vijay kumarca2e6812015-07-08 20:28:25 +0530257static char frp_ptns[2][8] = {"config","frp"};
258
lijuang511a2b52015-08-14 20:50:51 +0800259static const char *critical_flash_allowed_ptn[] = {
260 "aboot",
261 "rpm",
262 "tz",
263 "sbl",
264 "sdi",
265 "sbl1",
266 "xbl",
267 "hyp",
268 "pmic",
269 "bootloader",
270 "devinfo",
271 "partition"};
272
Dima Zavin42168f22009-01-30 11:52:22 -0800273struct atag_ptbl_entry
274{
275 char name[16];
276 unsigned offset;
277 unsigned size;
278 unsigned flags;
279};
280
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700281/*
282 * Partition info, required to be published
283 * for fastboot
284 */
285struct getvar_partition_info {
Mayank Grover7b8a8f82017-10-27 13:07:59 +0530286 char part_name[MAX_GPT_NAME_SIZE]; /* Partition name */
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700287 char getvar_size[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for size */
288 char getvar_type[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for type */
289 char size_response[MAX_RSP_SIZE]; /* fastboot response for size */
290 char type_response[MAX_RSP_SIZE]; /* fastboot response for type */
291};
292
293/*
Mayank Grover7b8a8f82017-10-27 13:07:59 +0530294 * Update the part_type_known for known paritions types.
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700295 */
Mayank Groverd38fe012018-03-13 15:33:16 +0530296#define RAW_STR "raw"
Mayank Grover52cd10a2018-03-15 12:57:54 +0530297#define EXT_STR "ext4"
298#define F2FS_STR "f2fs"
299
300#define FS_SUPERBLOCK_OFFSET 0x400
301#define EXT_MAGIC 0xEF53
302#define EXT_MAGIC_OFFSET_SB 0x38
303#define F2FS_MAGIC 0xF2F52010 // F2FS Magic Number
304#define F2FS_MAGIC_OFFSET_SB 0x0
305
306typedef enum fs_signature_type {
307 EXT_FS_SIGNATURE = 1,
308 EXT_F2FS_SIGNATURE = 2,
309 NO_FS = -1
310} fs_signature_type;
311
Mayank Grover7b8a8f82017-10-27 13:07:59 +0530312struct getvar_partition_info part_info[NUM_PARTITIONS];
313struct getvar_partition_info part_type_known[] =
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700314{
Mayank Grover49920212018-02-09 18:15:55 +0530315 { "system" , "partition-size:", "partition-type:", "", "ext4" },
316 { "userdata" , "partition-size:", "partition-type:", "", "ext4" },
317 { "cache" , "partition-size:", "partition-type:", "", "ext4" },
318 { "recoveryfs" , "partition-size:", "partition-type:", "", "ext4" },
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700319};
320
321char max_download_size[MAX_RSP_SIZE];
Ameya Thakur11cf1a62013-08-05 12:44:48 -0700322char charger_screen_enabled[MAX_RSP_SIZE];
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800323char sn_buf[13];
Dhaval Patel223ec952013-07-18 14:49:44 -0700324char display_panel_buf[MAX_PANEL_BUF_SIZE];
Unnati Gandhi62c8ab82014-01-24 11:01:01 +0530325char panel_display_mode[MAX_RSP_SIZE];
Mayank Grovera64dfbe2018-05-17 14:06:34 +0530326char soc_version_str[MAX_RSP_SIZE];
Mayank Grover7b8a8f82017-10-27 13:07:59 +0530327char block_size_string[MAX_RSP_SIZE];
Mukesh Ojhabb6e7f32018-03-02 15:04:06 +0530328#if PRODUCT_IOT
Mayank Grover7b8a8f82017-10-27 13:07:59 +0530329
330/* For IOT we are using custom version */
331#define PRODUCT_IOT_VERSION "IOT001"
332char bootloader_version_string[MAX_RSP_SIZE];
333#endif
lijuang102dfa92015-10-09 18:31:03 +0800334
335#if CHECK_BAT_VOLTAGE
lijuang65c5a822015-08-29 16:35:36 +0800336char battery_voltage[MAX_RSP_SIZE];
lijuang102dfa92015-10-09 18:31:03 +0800337char battery_soc_ok [MAX_RSP_SIZE];
338#endif
339
lijuangf16461c2015-08-03 17:09:34 +0800340char get_variant[MAX_RSP_SIZE];
Greg Griscod6250552011-06-29 14:40:23 -0700341
Greg Griscod2471ef2011-07-14 13:00:42 -0700342extern int emmc_recovery_init(void);
343
Kinson Chik0b1c8162011-08-31 16:31:57 -0700344#if NO_KEYPAD_DRIVER
345extern int fastboot_trigger(void);
346#endif
Greg Griscod2471ef2011-07-14 13:00:42 -0700347
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800348static void update_ker_tags_rdisk_addr(struct boot_img_hdr *hdr, bool is_arm64)
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700349{
350 /* overwrite the destination of specified for the project */
Channagoud Kadabi7042fa32013-04-26 16:44:14 -0700351#ifdef ABOOT_IGNORE_BOOT_HEADER_ADDRS
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800352 if (is_arm64)
353 hdr->kernel_addr = ABOOT_FORCE_KERNEL64_ADDR;
354 else
355 hdr->kernel_addr = ABOOT_FORCE_KERNEL_ADDR;
Channagoud Kadabi7042fa32013-04-26 16:44:14 -0700356 hdr->ramdisk_addr = ABOOT_FORCE_RAMDISK_ADDR;
357 hdr->tags_addr = ABOOT_FORCE_TAGS_ADDR;
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700358#endif
359}
360
Dima Zavin42168f22009-01-30 11:52:22 -0800361static void ptentry_to_tag(unsigned **ptr, struct ptentry *ptn)
362{
363 struct atag_ptbl_entry atag_ptn;
364
365 memcpy(atag_ptn.name, ptn->name, 16);
366 atag_ptn.name[15] = '\0';
367 atag_ptn.offset = ptn->start;
368 atag_ptn.size = ptn->length;
369 atag_ptn.flags = ptn->flags;
370 memcpy(*ptr, &atag_ptn, sizeof(struct atag_ptbl_entry));
371 *ptr += sizeof(struct atag_ptbl_entry) / sizeof(unsigned);
372}
Brian Swetland9c4c0752009-01-25 16:23:50 -0800373
lijuang102dfa92015-10-09 18:31:03 +0800374#if CHECK_BAT_VOLTAGE
375void update_battery_status(void)
376{
377 snprintf(battery_voltage,MAX_RSP_SIZE, "%d",target_get_battery_voltage());
378 snprintf(battery_soc_ok ,MAX_RSP_SIZE, "%s",target_battery_soc_ok()? "yes":"no");
379}
380#endif
381
Neeti Desaie245d492012-06-01 12:52:13 -0700382unsigned char *update_cmdline(const char * cmdline)
Brian Swetland9c4c0752009-01-25 16:23:50 -0800383{
David Ng183a7422009-12-07 14:55:21 -0800384 int cmdline_len = 0;
385 int have_cmdline = 0;
Amol Jadi168b7712012-03-06 16:15:00 -0800386 unsigned char *cmdline_final = NULL;
Neeti Desaie245d492012-06-01 12:52:13 -0700387 int pause_at_bootup = 0;
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800388 bool warm_boot = false;
Pavel Nedev5614d222013-06-17 18:01:02 +0300389 bool gpt_exists = partition_gpt_exists();
Joonwoo Park61112782013-10-02 19:50:39 -0700390 int have_target_boot_params = 0;
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700391 char *boot_dev_buf = NULL;
Monika Singh292b3e92018-03-17 22:40:23 +0530392#ifdef MDTP_SUPPORT
Mayank Grover351a75e2017-05-30 20:06:08 +0530393 bool is_mdtp_activated = 0;
Monika Singh292b3e92018-03-17 22:40:23 +0530394#endif
Mayank Grover351a75e2017-05-30 20:06:08 +0530395 int current_active_slot = INVALID;
Mayank Grover3804be72017-06-22 11:59:23 +0530396 int system_ptn_index = -1;
397 unsigned int lun = 0;
398 char lun_char_base = 'a';
Sourabh Banerjee386b1322018-02-27 09:37:28 +0530399#if VERITY_LE
400 int syspath_buflen = strlen(verity_dev)
401 + strlen(verity_system_part) + (sizeof(char) * 2) + 2
402 + strlen(verity_params) + sizeof(int) + 2;
403#else
lijuang83ef4b22018-08-23 11:01:55 +0800404 int syspath_buflen = strlen(sys_path) + sizeof(int) + 2; /*allocate buflen for largest possible string*/
405 char dtbo_idx_str[MAX_DTBO_IDX_STR] = "\0";
406 int dtbo_idx = INVALID_PTN;
Sourabh Banerjee386b1322018-02-27 09:37:28 +0530407#endif
Mayank Grover3804be72017-06-22 11:59:23 +0530408 char syspath_buf[syspath_buflen];
Mayank Grover466d6562018-05-10 14:52:20 +0530409#if HIBERNATION_SUPPORT
410 int resume_buflen = strlen(resume) + sizeof(int) + 2;
411 char resume_buf[resume_buflen];
412 int swap_ptn_index = INVALID_PTN;
413#endif
414
Mayank Grover889be1b2017-09-12 20:12:23 +0530415#if VERIFIED_BOOT
416 uint32_t boot_state = RED;
417#endif
Hareesh Gauthamabf1e1e2017-04-12 18:09:24 +0530418
419#if USE_LE_SYSTEMD
420 is_systemd_present=true;
421#endif
422
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700423#if VERIFIED_BOOT
Mayank Grover912eaa62017-10-26 12:08:53 +0530424 if (VB_M <= target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +0530425 {
426 boot_state = boot_verify_get_state();
427 }
Parth Dixitddbc7352015-10-18 03:13:31 +0530428#endif
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700429
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200430#ifdef MDTP_SUPPORT
431 mdtp_activated(&is_mdtp_activated);
432#endif /* MDTP_SUPPORT */
Dima Zavin42168f22009-01-30 11:52:22 -0800433
Brian Swetland9c4c0752009-01-25 16:23:50 -0800434 if (cmdline && cmdline[0]) {
David Ng183a7422009-12-07 14:55:21 -0800435 cmdline_len = strlen(cmdline);
436 have_cmdline = 1;
437 }
438 if (target_is_emmc_boot()) {
439 cmdline_len += strlen(emmc_cmdline);
Sridhar Parasuram7bd4aaf2015-02-12 11:14:38 -0800440#if USE_BOOTDEV_CMDLINE
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700441 boot_dev_buf = (char *) malloc(sizeof(char) * BOOT_DEV_MAX_LEN);
442 ASSERT(boot_dev_buf);
443 platform_boot_dev_cmdline(boot_dev_buf);
444 cmdline_len += strlen(boot_dev_buf);
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700445#endif
David Ng183a7422009-12-07 14:55:21 -0800446 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800447
448 cmdline_len += strlen(usb_sn_cmdline);
449 cmdline_len += strlen(sn_buf);
450
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700451#if VERIFIED_BOOT
Mayank Grover912eaa62017-10-26 12:08:53 +0530452 if (VB_M <= target_get_vb_version())
Sridhar Parasuram7d8bb9a2015-09-23 18:21:19 -0700453 {
Mayank Grover889be1b2017-09-12 20:12:23 +0530454 cmdline_len += strlen(verified_state) + strlen(vbsn[boot_state].name);
455 if ((device.verity_mode != 0 ) && (device.verity_mode != 1))
456 {
457 dprintf(CRITICAL, "Devinfo paritition possibly corrupted!!!. Please erase devinfo partition to continue booting\n");
458 ASSERT(0);
459 }
460 cmdline_len += strlen(verity_mode) + strlen(vbvm[device.verity_mode].name);
461 cmdline_len += strlen(keymaster_v1);
Sridhar Parasuram7d8bb9a2015-09-23 18:21:19 -0700462 }
Parth Dixitddbc7352015-10-18 03:13:31 +0530463#endif
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700464
Monika Singh292b3e92018-03-17 22:40:23 +0530465
466 if (vbcmdline != NULL) {
467 dprintf(DEBUG, "UpdateCmdLine vbcmdline present len %d\n",
468 strlen(vbcmdline));
469 cmdline_len += strlen(vbcmdline);
470 }
471
Pavel Nedev5614d222013-06-17 18:01:02 +0300472 if (boot_into_recovery && gpt_exists)
473 cmdline_len += strlen(secondary_gpt_enable);
474
Monika Singh292b3e92018-03-17 22:40:23 +0530475#ifdef MDTP_SUPPORT
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200476 if(is_mdtp_activated)
477 cmdline_len += strlen(mdtp_activated_flag);
Monika Singh292b3e92018-03-17 22:40:23 +0530478#endif
Pavel Nedev328ac822013-04-05 15:25:11 +0300479 if (boot_into_ffbm) {
480 cmdline_len += strlen(androidboot_mode);
Hareesh Gauthamabf1e1e2017-04-12 18:09:24 +0530481
482 if(is_systemd_present)
483 cmdline_len += strlen(systemd_ffbm_mode);
484
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700485 cmdline_len += strlen(ffbm_mode_string);
Pavel Nedev898298c2013-02-27 12:36:09 -0800486 /* reduce kernel console messages to speed-up boot */
487 cmdline_len += strlen(loglevel);
Matthew Qind886f3c2014-01-17 16:52:01 +0800488 } else if (boot_reason_alarm) {
489 cmdline_len += strlen(alarmboot_cmdline);
Zhenhua Huang431dafa2015-06-30 16:13:37 +0800490 } else if ((target_build_variant_user() || device.charger_screen_enabled)
lijuang266b17d2018-08-17 18:53:42 +0800491 && target_pause_for_battery_charge() && !boot_into_recovery) {
David Ngf773dde2010-07-26 19:55:08 -0700492 pause_at_bootup = 1;
493 cmdline_len += strlen(battchg_pause);
494 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800495
Shashank Mittalcd98d472011-08-02 14:29:24 -0700496 if(target_use_signed_kernel() && auth_kernel_img) {
497 cmdline_len += strlen(auth_kernel);
498 }
499
Joonwoo Park61112782013-10-02 19:50:39 -0700500 if (get_target_boot_params(cmdline, boot_into_recovery ? "recoveryfs" :
501 "system",
vijay kumar870515d2015-08-31 16:37:24 +0530502 &target_boot_params) == 0) {
Joonwoo Park61112782013-10-02 19:50:39 -0700503 have_target_boot_params = 1;
504 cmdline_len += strlen(target_boot_params);
505 }
506
Ajay Dudanid04110c2011-01-17 23:55:07 -0800507 /* Determine correct androidboot.baseband to use */
508 switch(target_baseband())
509 {
510 case BASEBAND_APQ:
511 cmdline_len += strlen(baseband_apq);
512 break;
513
514 case BASEBAND_MSM:
515 cmdline_len += strlen(baseband_msm);
516 break;
517
518 case BASEBAND_CSFB:
519 cmdline_len += strlen(baseband_csfb);
520 break;
521
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800522 case BASEBAND_SVLTE2A:
523 cmdline_len += strlen(baseband_svlte2a);
Ajay Dudanid04110c2011-01-17 23:55:07 -0800524 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700525
526 case BASEBAND_MDM:
527 cmdline_len += strlen(baseband_mdm);
528 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700529
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800530 case BASEBAND_MDM2:
531 cmdline_len += strlen(baseband_mdm2);
532 break;
533
Amol Jadi5c61a952012-05-04 17:05:35 -0700534 case BASEBAND_SGLTE:
535 cmdline_len += strlen(baseband_sglte);
536 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530537
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800538 case BASEBAND_SGLTE2:
539 cmdline_len += strlen(baseband_sglte2);
540 break;
541
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530542 case BASEBAND_DSDA:
543 cmdline_len += strlen(baseband_dsda);
544 break;
Amol Jadi2a15a272013-01-22 12:03:36 -0800545
546 case BASEBAND_DSDA2:
547 cmdline_len += strlen(baseband_dsda2);
548 break;
Vijay Kumar Pendotib228cfc2016-06-13 20:15:23 +0530549 case BASEBAND_APQ_NOWGR:
550 cmdline_len += strlen(baseband_apq_nowgr);
551 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800552 }
553
Vladislav Levenetz3cd45242017-04-05 08:58:07 +0300554#if ENABLE_DISPLAY
Lijuan Gao4a5b8322014-07-24 10:38:42 +0800555 if (cmdline) {
556 if ((strstr(cmdline, DISPLAY_DEFAULT_PREFIX) == NULL) &&
Padmanabhan Komandurubccbcdc2015-06-30 16:19:24 +0530557 target_display_panel_node(display_panel_buf,
558 MAX_PANEL_BUF_SIZE) &&
Lijuan Gao4a5b8322014-07-24 10:38:42 +0800559 strlen(display_panel_buf)) {
560 cmdline_len += strlen(display_panel_buf);
561 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700562 }
Vladislav Levenetz3cd45242017-04-05 08:58:07 +0300563#endif
Dhaval Patel223ec952013-07-18 14:49:44 -0700564
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800565 if (target_warm_boot()) {
566 warm_boot = true;
567 cmdline_len += strlen(warmboot_cmdline);
568 }
569
Mayank Grover5384ed82018-05-09 12:09:24 +0530570 if (target_uses_system_as_root() ||
571 partition_multislot_is_supported())
Mayank Grover351a75e2017-05-30 20:06:08 +0530572 {
573 current_active_slot = partition_find_active_slot();
574 cmdline_len += (strlen(androidboot_slot_suffix)+
575 strlen(SUFFIX_SLOT(current_active_slot)));
576
Mayank Grover3804be72017-06-22 11:59:23 +0530577 system_ptn_index = partition_get_index("system");
578 if (platform_boot_dev_isemmc())
579 {
Sourabh Banerjee386b1322018-02-27 09:37:28 +0530580#if VERITY_LE
581 /*
582 Condition 4: Verity and A/B both enabled
583 Eventual command line looks like:
584 ... androidboot.slot_suffix=<slot_suffix> ... rootfstype=ext4 ...
585 ... root=/dev/dm-0 dm="system_<slot_suffix> none ro,0 1 android-verity /dev/mmcblk0p<NN>"
586 */
587 snprintf(syspath_buf, syspath_buflen, " %s %s%s %s%d\"",
588 verity_dev,
589 verity_system_part, suffix_slot[current_active_slot],
590 verity_params, system_ptn_index + 1);
591#else
592 /*
593 Condition 5: A/B enabled, but verity disabled
594 Eventual command line looks like:
595 ... androidboot.slot_suffix=<slot_suffix> ... rootfstype=ext4 ...
596 ... root=/dev/mmcblk0p<NN> ...
597 */
598 snprintf(syspath_buf, syspath_buflen, " %s%d",
599 sys_path, system_ptn_index + 1);
600#endif
Mayank Grover3804be72017-06-22 11:59:23 +0530601 }
602 else
603 {
604 lun = partition_get_lun(system_ptn_index);
605 snprintf(syspath_buf, syspath_buflen, " root=/dev/sd%c%d",
606 lun_char_base + lun,
607 partition_get_index_in_lun("system", lun));
608 }
Mayank Grover351a75e2017-05-30 20:06:08 +0530609
Monika Singh292b3e92018-03-17 22:40:23 +0530610#ifndef VERIFIED_BOOT_2
Mayank Grover3804be72017-06-22 11:59:23 +0530611 cmdline_len += strlen(syspath_buf);
Monika Singh292b3e92018-03-17 22:40:23 +0530612#endif
Mayank Grover5384ed82018-05-09 12:09:24 +0530613 }
614
615 if (target_uses_system_as_root() ||
616 partition_multislot_is_supported())
617 {
618 cmdline_len += strlen(sys_path_cmdline);
Mayank Grover351a75e2017-05-30 20:06:08 +0530619 if (!boot_into_recovery)
620 cmdline_len += strlen(skip_ramfs);
621 }
622
Mayank Grover466d6562018-05-10 14:52:20 +0530623#if HIBERNATION_SUPPORT
624 if (platform_boot_dev_isemmc())
625 {
626 swap_ptn_index = partition_get_index("swap");
627 if (swap_ptn_index != INVALID_PTN)
628 {
629 snprintf(resume_buf, resume_buflen,
630 " %s%d", resume,
631 (swap_ptn_index + 1));
632 cmdline_len += strlen(resume_buf);
633 }
634 else
635 {
636 dprintf(INFO, "WARNING: swap partition not found\n");
637 }
638 }
639#endif
640
Channagoud Kadabi85c7ec32016-01-28 23:09:21 -0800641#if TARGET_CMDLINE_SUPPORT
642 char *target_cmdline_buf = malloc(TARGET_MAX_CMDLNBUF);
643 int target_cmd_line_len;
644 ASSERT(target_cmdline_buf);
645 target_cmd_line_len = target_update_cmdline(target_cmdline_buf);
646 cmdline_len += target_cmd_line_len;
647#endif
648
lijuang83ef4b22018-08-23 11:01:55 +0800649#if !VERITY_LE
650 dtbo_idx = get_dtbo_idx ();
651 if (dtbo_idx != INVALID_PTN) {
652 snprintf(dtbo_idx_str, sizeof(dtbo_idx_str), "%s%d",
653 android_boot_dtbo_idx, dtbo_idx);
654 cmdline_len += strlen (dtbo_idx_str);
655 }
656#endif
657
David Ng183a7422009-12-07 14:55:21 -0800658 if (cmdline_len > 0) {
659 const char *src;
Maria Yu52254c02014-07-04 16:14:54 +0800660 unsigned char *dst;
661
662 cmdline_final = (unsigned char*) malloc((cmdline_len + 4) & (~3));
663 ASSERT(cmdline_final != NULL);
vijay kumar287bb542015-09-29 13:01:52 +0530664 memset((void *)cmdline_final, 0, sizeof(*cmdline_final));
Maria Yu52254c02014-07-04 16:14:54 +0800665 dst = cmdline_final;
Neeti Desaie245d492012-06-01 12:52:13 -0700666
Amol Jadi168b7712012-03-06 16:15:00 -0800667 /* Save start ptr for debug print */
David Ng183a7422009-12-07 14:55:21 -0800668 if (have_cmdline) {
669 src = cmdline;
670 while ((*dst++ = *src++));
671 }
672 if (target_is_emmc_boot()) {
673 src = emmc_cmdline;
674 if (have_cmdline) --dst;
David Ngf773dde2010-07-26 19:55:08 -0700675 have_cmdline = 1;
676 while ((*dst++ = *src++));
Sridhar Parasuram7bd4aaf2015-02-12 11:14:38 -0800677#if USE_BOOTDEV_CMDLINE
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700678 src = boot_dev_buf;
679 if (have_cmdline) --dst;
680 while ((*dst++ = *src++));
681#endif
David Ngf773dde2010-07-26 19:55:08 -0700682 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800683
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700684#if VERIFIED_BOOT
Mayank Grover912eaa62017-10-26 12:08:53 +0530685 if (VB_M <= target_get_vb_version())
Sridhar Parasuram7d8bb9a2015-09-23 18:21:19 -0700686 {
Mayank Grover889be1b2017-09-12 20:12:23 +0530687 src = verified_state;
688 if(have_cmdline) --dst;
689 have_cmdline = 1;
690 while ((*dst++ = *src++));
691 src = vbsn[boot_state].name;
692 if(have_cmdline) --dst;
693 while ((*dst++ = *src++));
694
695 if ((device.verity_mode != 0 ) && (device.verity_mode != 1))
696 {
697 dprintf(CRITICAL, "Devinfo paritition possibly corrupted!!!. Please erase devinfo partition to continue booting\n");
698 ASSERT(0);
699 }
700 src = verity_mode;
701 if(have_cmdline) --dst;
702 while ((*dst++ = *src++));
703 src = vbvm[device.verity_mode].name;
704 if(have_cmdline) -- dst;
705 while ((*dst++ = *src++));
706 src = keymaster_v1;
707 if(have_cmdline) --dst;
708 while ((*dst++ = *src++));
Sridhar Parasuram7d8bb9a2015-09-23 18:21:19 -0700709 }
Parth Dixitddbc7352015-10-18 03:13:31 +0530710#endif
Monika Singh292b3e92018-03-17 22:40:23 +0530711
712 if (vbcmdline != NULL) {
713 src = vbcmdline;
714 if (have_cmdline) --dst;
715 while ((*dst++ = *src++));
716 }
717
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800718 src = usb_sn_cmdline;
719 if (have_cmdline) --dst;
720 have_cmdline = 1;
721 while ((*dst++ = *src++));
722 src = sn_buf;
723 if (have_cmdline) --dst;
724 have_cmdline = 1;
725 while ((*dst++ = *src++));
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800726 if (warm_boot) {
727 if (have_cmdline) --dst;
728 src = warmboot_cmdline;
729 while ((*dst++ = *src++));
730 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800731
Pavel Nedev5614d222013-06-17 18:01:02 +0300732 if (boot_into_recovery && gpt_exists) {
733 src = secondary_gpt_enable;
734 if (have_cmdline) --dst;
735 while ((*dst++ = *src++));
736 }
Monika Singh292b3e92018-03-17 22:40:23 +0530737#ifdef MDTP_SUPPORT
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200738 if (is_mdtp_activated) {
739 src = mdtp_activated_flag;
740 if (have_cmdline) --dst;
741 while ((*dst++ = *src++));
742 }
Monika Singh292b3e92018-03-17 22:40:23 +0530743#endif
Pavel Nedev328ac822013-04-05 15:25:11 +0300744 if (boot_into_ffbm) {
745 src = androidboot_mode;
746 if (have_cmdline) --dst;
747 while ((*dst++ = *src++));
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700748 src = ffbm_mode_string;
Pavel Nedev328ac822013-04-05 15:25:11 +0300749 if (have_cmdline) --dst;
750 while ((*dst++ = *src++));
Hareesh Gauthamabf1e1e2017-04-12 18:09:24 +0530751
752 if(is_systemd_present) {
753 src = systemd_ffbm_mode;
754 if (have_cmdline) --dst;
755 while ((*dst++ = *src++));
756 }
757
Pavel Nedev898298c2013-02-27 12:36:09 -0800758 src = loglevel;
759 if (have_cmdline) --dst;
760 while ((*dst++ = *src++));
Matthew Qind886f3c2014-01-17 16:52:01 +0800761 } else if (boot_reason_alarm) {
762 src = alarmboot_cmdline;
763 if (have_cmdline) --dst;
764 while ((*dst++ = *src++));
Pavel Nedev328ac822013-04-05 15:25:11 +0300765 } else if (pause_at_bootup) {
David Ngf773dde2010-07-26 19:55:08 -0700766 src = battchg_pause;
767 if (have_cmdline) --dst;
David Ng183a7422009-12-07 14:55:21 -0800768 while ((*dst++ = *src++));
769 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800770
Shashank Mittalcd98d472011-08-02 14:29:24 -0700771 if(target_use_signed_kernel() && auth_kernel_img) {
772 src = auth_kernel;
773 if (have_cmdline) --dst;
774 while ((*dst++ = *src++));
775 }
776
Ajay Dudanid04110c2011-01-17 23:55:07 -0800777 switch(target_baseband())
778 {
779 case BASEBAND_APQ:
780 src = baseband_apq;
781 if (have_cmdline) --dst;
782 while ((*dst++ = *src++));
783 break;
784
785 case BASEBAND_MSM:
786 src = baseband_msm;
787 if (have_cmdline) --dst;
788 while ((*dst++ = *src++));
789 break;
790
791 case BASEBAND_CSFB:
792 src = baseband_csfb;
793 if (have_cmdline) --dst;
794 while ((*dst++ = *src++));
795 break;
796
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800797 case BASEBAND_SVLTE2A:
798 src = baseband_svlte2a;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800799 if (have_cmdline) --dst;
800 while ((*dst++ = *src++));
801 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700802
803 case BASEBAND_MDM:
804 src = baseband_mdm;
805 if (have_cmdline) --dst;
806 while ((*dst++ = *src++));
807 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700808
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800809 case BASEBAND_MDM2:
810 src = baseband_mdm2;
811 if (have_cmdline) --dst;
812 while ((*dst++ = *src++));
813 break;
814
Amol Jadi5c61a952012-05-04 17:05:35 -0700815 case BASEBAND_SGLTE:
816 src = baseband_sglte;
817 if (have_cmdline) --dst;
818 while ((*dst++ = *src++));
819 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530820
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800821 case BASEBAND_SGLTE2:
822 src = baseband_sglte2;
823 if (have_cmdline) --dst;
824 while ((*dst++ = *src++));
825 break;
826
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530827 case BASEBAND_DSDA:
828 src = baseband_dsda;
829 if (have_cmdline) --dst;
830 while ((*dst++ = *src++));
831 break;
Amol Jadi2a15a272013-01-22 12:03:36 -0800832
833 case BASEBAND_DSDA2:
834 src = baseband_dsda2;
835 if (have_cmdline) --dst;
836 while ((*dst++ = *src++));
837 break;
Vijay Kumar Pendotib228cfc2016-06-13 20:15:23 +0530838 case BASEBAND_APQ_NOWGR:
839 src = baseband_apq_nowgr;
840 if (have_cmdline) --dst;
841 while ((*dst++ = *src++));
842 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800843 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700844
845 if (strlen(display_panel_buf)) {
Dhaval Patel223ec952013-07-18 14:49:44 -0700846 src = display_panel_buf;
847 if (have_cmdline) --dst;
848 while ((*dst++ = *src++));
849 }
Joonwoo Park61112782013-10-02 19:50:39 -0700850
851 if (have_target_boot_params) {
852 if (have_cmdline) --dst;
853 src = target_boot_params;
854 while ((*dst++ = *src++));
vijay kumar870515d2015-08-31 16:37:24 +0530855 free(target_boot_params);
Joonwoo Park61112782013-10-02 19:50:39 -0700856 }
Channagoud Kadabi85c7ec32016-01-28 23:09:21 -0800857
Mayank Grover351a75e2017-05-30 20:06:08 +0530858 if (partition_multislot_is_supported() && have_cmdline)
859 {
860 src = androidboot_slot_suffix;
861 --dst;
862 while ((*dst++ = *src++));
863 --dst;
864 src = SUFFIX_SLOT(current_active_slot);
865 while ((*dst++ = *src++));
Mayank Grover5384ed82018-05-09 12:09:24 +0530866 }
Mayank Grover351a75e2017-05-30 20:06:08 +0530867
Mayank Grover351a75e2017-05-30 20:06:08 +0530868
Mayank Grover5384ed82018-05-09 12:09:24 +0530869 /*
870 * System-As-Root behaviour, system.img should contain both
871 * system content and ramdisk content, and should be mounted at
872 * root(a/b).
873 * Apending skip_ramfs for non a/b builds which use, system as root.
874 */
875 if ((target_uses_system_as_root() ||
876 partition_multislot_is_supported()) &&
877 have_cmdline)
878 {
879 if (!boot_into_recovery)
880 {
881 src = skip_ramfs;
Mayank Grover351a75e2017-05-30 20:06:08 +0530882 --dst;
883 while ((*dst++ = *src++));
Mayank Grover5384ed82018-05-09 12:09:24 +0530884 }
885
886 src = sys_path_cmdline;
887 --dst;
888 while ((*dst++ = *src++));
Mayank Grover3804be72017-06-22 11:59:23 +0530889
Monika Singh292b3e92018-03-17 22:40:23 +0530890#ifndef VERIFIED_BOOT_2
Mayank Grover5384ed82018-05-09 12:09:24 +0530891 src = syspath_buf;
892 --dst;
893 while ((*dst++ = *src++));
Monika Singh292b3e92018-03-17 22:40:23 +0530894#endif
Mayank Grover351a75e2017-05-30 20:06:08 +0530895 }
896
Mayank Grover466d6562018-05-10 14:52:20 +0530897#if HIBERNATION_SUPPORT
898 if (swap_ptn_index != INVALID_PTN)
899 {
900 src = resume_buf;
901 --dst;
902 while ((*dst++ = *src++));
903 }
904#endif
905
Channagoud Kadabi85c7ec32016-01-28 23:09:21 -0800906#if TARGET_CMDLINE_SUPPORT
907 if (target_cmdline_buf && target_cmd_line_len)
908 {
909 if (have_cmdline) --dst;
910 src = target_cmdline_buf;
911 while((*dst++ = *src++));
912 free(target_cmdline_buf);
913 }
914#endif
lijuang83ef4b22018-08-23 11:01:55 +0800915
916#if !VERITY_LE
917 if (dtbo_idx != INVALID_PTN) {
918 src = dtbo_idx_str;
919 --dst;
920 while ((*dst++ = *src++));
921 }
922#endif
Neeti Desaie245d492012-06-01 12:52:13 -0700923 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700924
925
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700926 if (boot_dev_buf)
927 free(boot_dev_buf);
928
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -0800929 if (cmdline_final)
930 dprintf(INFO, "cmdline: %s\n", cmdline_final);
931 else
932 dprintf(INFO, "cmdline is NULL\n");
Neeti Desaie245d492012-06-01 12:52:13 -0700933 return cmdline_final;
934}
935
936unsigned *atag_core(unsigned *ptr)
937{
938 /* CORE */
939 *ptr++ = 2;
940 *ptr++ = 0x54410001;
941
942 return ptr;
943
944}
945
946unsigned *atag_ramdisk(unsigned *ptr, void *ramdisk,
947 unsigned ramdisk_size)
948{
949 if (ramdisk_size) {
950 *ptr++ = 4;
951 *ptr++ = 0x54420005;
952 *ptr++ = (unsigned)ramdisk;
953 *ptr++ = ramdisk_size;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800954 }
955
Neeti Desaie245d492012-06-01 12:52:13 -0700956 return ptr;
957}
958
959unsigned *atag_ptable(unsigned **ptr_addr)
960{
961 int i;
962 struct ptable *ptable;
963
964 if ((ptable = flash_get_ptable()) && (ptable->count != 0)) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700965 *(*ptr_addr)++ = 2 + (ptable->count * (sizeof(struct atag_ptbl_entry) /
966 sizeof(unsigned)));
Neeti Desaie245d492012-06-01 12:52:13 -0700967 *(*ptr_addr)++ = 0x4d534d70;
968 for (i = 0; i < ptable->count; ++i)
969 ptentry_to_tag(ptr_addr, ptable_get(ptable, i));
970 }
971
972 return (*ptr_addr);
973}
974
975unsigned *atag_cmdline(unsigned *ptr, const char *cmdline)
976{
977 int cmdline_length = 0;
978 int n;
Neeti Desaie245d492012-06-01 12:52:13 -0700979 char *dest;
980
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800981 cmdline_length = strlen((const char*)cmdline);
Neeti Desaie245d492012-06-01 12:52:13 -0700982 n = (cmdline_length + 4) & (~3);
983
984 *ptr++ = (n / 4) + 2;
985 *ptr++ = 0x54410009;
986 dest = (char *) ptr;
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800987 while ((*dest++ = *cmdline++));
Neeti Desaie245d492012-06-01 12:52:13 -0700988 ptr += (n / 4);
989
990 return ptr;
991}
992
993unsigned *atag_end(unsigned *ptr)
994{
Brian Swetland9c4c0752009-01-25 16:23:50 -0800995 /* END */
996 *ptr++ = 0;
997 *ptr++ = 0;
998
Neeti Desaie245d492012-06-01 12:52:13 -0700999 return ptr;
1000}
1001
1002void generate_atags(unsigned *ptr, const char *cmdline,
1003 void *ramdisk, unsigned ramdisk_size)
1004{
vijay kumar21a37452015-12-29 16:23:21 +05301005 unsigned *orig_ptr = ptr;
Neeti Desaie245d492012-06-01 12:52:13 -07001006 ptr = atag_core(ptr);
1007 ptr = atag_ramdisk(ptr, ramdisk, ramdisk_size);
1008 ptr = target_atag_mem(ptr);
1009
1010 /* Skip NAND partition ATAGS for eMMC boot */
1011 if (!target_is_emmc_boot()){
1012 ptr = atag_ptable(&ptr);
1013 }
1014
vijay kumar21a37452015-12-29 16:23:21 +05301015 /*
1016 * Atags size filled till + cmdline size + 1 unsigned for 4-byte boundary + 4 unsigned
1017 * for atag identifier in atag_cmdline and atag_end should be with in MAX_TAGS_SIZE bytes
1018 */
Mayank Groverbc8a2ef2018-02-23 18:03:36 +05301019 if (!cmdline)
1020 return;
1021
vijay kumar21a37452015-12-29 16:23:21 +05301022 if (((ptr - orig_ptr) + strlen(cmdline) + 5 * sizeof(unsigned)) < MAX_TAGS_SIZE) {
1023 ptr = atag_cmdline(ptr, cmdline);
1024 ptr = atag_end(ptr);
1025 }
1026 else {
1027 dprintf(CRITICAL,"Crossing ATAGs Max size allowed\n");
1028 ASSERT(0);
1029 }
Neeti Desaie245d492012-06-01 12:52:13 -07001030}
1031
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001032typedef void entry_func_ptr(unsigned, unsigned, unsigned*);
Neeti Desaie245d492012-06-01 12:52:13 -07001033void boot_linux(void *kernel, unsigned *tags,
1034 const char *cmdline, unsigned machtype,
1035 void *ramdisk, unsigned ramdisk_size)
1036{
Amol Jadi10c7d1c2013-01-25 13:24:29 -08001037 unsigned char *final_cmdline;
Amol Jadib6be5c12012-11-14 13:39:51 -08001038#if DEVICE_TREE
Neeti Desai17379b82012-06-04 18:42:53 -07001039 int ret = 0;
Amol Jadib6be5c12012-11-14 13:39:51 -08001040#endif
1041
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001042 void (*entry)(unsigned, unsigned, unsigned*) = (entry_func_ptr*)(PA((addr_t)kernel));
Deepa Dinamani25a9f762012-11-30 15:57:15 -08001043 uint32_t tags_phys = PA((addr_t)tags);
vijay kumar1a50a642015-11-16 12:41:15 +05301044 struct kernel64_hdr *kptr = ((struct kernel64_hdr*)(PA((addr_t)kernel)));
Deepa Dinamani25a9f762012-11-30 15:57:15 -08001045
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301046 ramdisk = (void *)PA((addr_t)ramdisk);
Neeti Desaie245d492012-06-01 12:52:13 -07001047
Amol Jadi10c7d1c2013-01-25 13:24:29 -08001048 final_cmdline = update_cmdline((const char*)cmdline);
1049
Neeti Desai17379b82012-06-04 18:42:53 -07001050#if DEVICE_TREE
Amol Jadib6be5c12012-11-14 13:39:51 -08001051 dprintf(INFO, "Updating device tree: start\n");
1052
Neeti Desai17379b82012-06-04 18:42:53 -07001053 /* Update the Device Tree */
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301054 ret = update_device_tree((void *)tags,(const char *)final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -07001055 if(ret)
1056 {
1057 dprintf(CRITICAL, "ERROR: Updating Device Tree Failed \n");
1058 ASSERT(0);
1059 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001060 dprintf(INFO, "Updating device tree: done\n");
Neeti Desai17379b82012-06-04 18:42:53 -07001061#else
Neeti Desaie245d492012-06-01 12:52:13 -07001062 /* Generating the Atags */
Amol Jadi10c7d1c2013-01-25 13:24:29 -08001063 generate_atags(tags, final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -07001064#endif
Neeti Desaie245d492012-06-01 12:52:13 -07001065
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001066#if VERIFIED_BOOT
Monika Singh292b3e92018-03-17 22:40:23 +05301067 if (VB_M == target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +05301068 {
1069 if (device.verity_mode == 0) {
lijuangbdd9bb42016-03-01 18:22:17 +08001070#if FBCON_DISPLAY_MSG
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07001071#if ENABLE_VB_ATTEST
Mayank Grover889be1b2017-09-12 20:12:23 +05301072 display_bootverify_menu(DISPLAY_MENU_EIO);
1073 wait_for_users_action();
1074 if(!pwr_key_is_pressed)
1075 shutdown_device();
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07001076#else
Mayank Grover889be1b2017-09-12 20:12:23 +05301077 display_bootverify_menu(DISPLAY_MENU_LOGGING);
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07001078#endif
Mayank Grover889be1b2017-09-12 20:12:23 +05301079 wait_for_users_action();
lijuangbdd9bb42016-03-01 18:22:17 +08001080#else
Mayank Grover889be1b2017-09-12 20:12:23 +05301081 dprintf(CRITICAL,
1082 "The dm-verity is not started in enforcing mode.\nWait for 5 seconds before proceeding\n");
1083 mdelay(5000);
lijuangbdd9bb42016-03-01 18:22:17 +08001084#endif
Mayank Grover889be1b2017-09-12 20:12:23 +05301085 }
lijuangbdd9bb42016-03-01 18:22:17 +08001086 }
lijuangbdd9bb42016-03-01 18:22:17 +08001087#endif
1088
1089#if VERIFIED_BOOT
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001090 /* Write protect the device info */
Channagoud Kadabi3bd9d1e2015-05-05 16:18:20 -07001091 if (!boot_into_recovery && target_build_variant_user() && devinfo_present && mmc_write_protect("devinfo", 1))
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001092 {
1093 dprintf(INFO, "Failed to write protect dev info\n");
1094 ASSERT(0);
1095 }
1096#endif
1097
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08001098 /* Turn off splash screen if enabled */
1099#if DISPLAY_SPLASH_SCREEN
1100 target_display_shutdown();
1101#endif
1102
Veera Sundaram Sankaran67ea0932015-09-25 10:09:30 -07001103 /* Perform target specific cleanup */
1104 target_uninit();
Monika Singh292b3e92018-03-17 22:40:23 +05301105 free_verified_boot_resource(&info);
1106 if (final_cmdline)
1107 free(final_cmdline);
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08001108
Deepa Dinamani33734bc2013-03-06 12:16:06 -08001109 dprintf(INFO, "booting linux @ %p, ramdisk @ %p (%d), tags/device tree @ %p\n",
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301110 entry, ramdisk, ramdisk_size, (void *)tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001111
1112 enter_critical_section();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001113
Amol Jadi4421e652011-06-16 15:00:48 -07001114 /* do any platform specific cleanup before kernel entry */
1115 platform_uninit();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001116
Brian Swetland9c4c0752009-01-25 16:23:50 -08001117 arch_disable_cache(UCACHE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001118
Amol Jadi504f9fe2012-08-16 13:56:48 -07001119#if ARM_WITH_MMU
Brian Swetland9c4c0752009-01-25 16:23:50 -08001120 arch_disable_mmu();
Amol Jadi504f9fe2012-08-16 13:56:48 -07001121#endif
Amol Jadi492d5a52013-03-15 16:12:34 -07001122 bs_set_timestamp(BS_KERNEL_ENTRY);
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001123
1124 if (IS_ARM64(kptr))
1125 /* Jump to a 64bit kernel */
1126 scm_elexec_call((paddr_t)kernel, tags_phys);
1127 else
1128 /* Jump to a 32bit kernel */
1129 entry(0, machtype, (unsigned*)tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001130}
1131
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001132/* Function to check if the memory address range falls within the aboot
1133 * boundaries.
1134 * start: Start of the memory region
1135 * size: Size of the memory region
1136 */
Vijay Kumar Pendoti9c002ad2016-03-09 13:52:45 +05301137int check_aboot_addr_range_overlap(uintptr_t start, uint32_t size)
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001138{
1139 /* Check for boundary conditions. */
Sundarajan Srinivasance2a0ea2013-12-16 17:02:56 -08001140 if ((UINT_MAX - start) < size)
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001141 return -1;
1142
1143 /* Check for memory overlap. */
1144 if ((start < MEMBASE) && ((start + size) <= MEMBASE))
1145 return 0;
Channagoud Kadabi94143912013-10-15 12:53:52 -07001146 else if (start >= (MEMBASE + MEMSIZE))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001147 return 0;
1148 else
1149 return -1;
1150}
1151
Mayank Grovere559cec2017-10-17 15:12:03 +05301152/* Function to check if the memory address range falls beyond ddr region.
1153 * start: Start of the memory region
1154 * size: Size of the memory region
1155 */
1156int check_ddr_addr_range_bound(uintptr_t start, uint32_t size)
1157{
1158 uintptr_t ddr_pa_start_addr = PA(get_ddr_start());
1159 uint64_t ddr_size = smem_get_ddr_size();
1160 uint64_t ddr_pa_end_addr = ddr_pa_start_addr + ddr_size;
1161 uintptr_t pa_start_addr = PA(start);
1162
1163 /* Check for boundary conditions. */
1164 if ((UINT_MAX - start) < size)
1165 return -1;
1166
1167 /* Check if memory range is beyond the ddr range. */
1168 if (pa_start_addr < ddr_pa_start_addr ||
1169 pa_start_addr >= (ddr_pa_end_addr) ||
1170 (pa_start_addr + size) > ddr_pa_end_addr)
1171 return -1;
1172 else
1173 return 0;
1174}
1175
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001176BUF_DMA_ALIGN(buf, BOOT_IMG_MAX_PAGE_SIZE); //Equal to max-supported pagesize
Dima Zavin214cc642009-01-26 11:16:21 -08001177
Monika Singh292b3e92018-03-17 22:40:23 +05301178int getimage(const bootinfo *info, void **image_buffer, uint32_t *imgsize,
1179 char *imgname)
1180{
1181 if (info == NULL || image_buffer == NULL || imgsize == NULL ||
1182 imgname == NULL) {
1183 dprintf(CRITICAL, "getimage: invalid parameters\n");
1184 return -1;
1185 }
1186
1187 for (uint32_t loadedindex = 0; loadedindex < info->num_loaded_images; loadedindex++) {
1188 if (!strncmp(info->images[loadedindex].name, imgname,
1189 strlen(imgname))) {
1190 *image_buffer = info->images[loadedindex].image_buffer;
1191 *imgsize = info->images[loadedindex].imgsize;
1192 return 0;
1193 }
1194 }
1195 return -1;
1196}
1197
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001198static void verify_signed_bootimg(uint32_t bootimg_addr, uint32_t bootimg_size)
1199{
1200 int ret;
Channagoud Kadabia8c623f2015-01-13 14:48:48 -08001201
1202#if !VERIFIED_BOOT
Sundarajan Srinivasance54d6e2013-11-11 12:45:00 -08001203#if IMAGE_VERIF_ALGO_SHA1
1204 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA1;
1205#else
1206 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA256;
1207#endif
Channagoud Kadabia8c623f2015-01-13 14:48:48 -08001208#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001209
1210 /* Assume device is rooted at this time. */
1211 device.is_tampered = 1;
1212
1213 dprintf(INFO, "Authenticating boot image (%d): start\n", bootimg_size);
1214
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001215#if VERIFIED_BOOT
Monika Singh7d2fc272018-03-16 17:16:01 +05301216 uint32_t bootstate;
1217 if(boot_into_recovery &&
Mayank Grover68fbf0d2017-10-24 14:13:39 +05301218 (!partition_multislot_is_supported()))
Monika Singh7d2fc272018-03-16 17:16:01 +05301219 {
1220 ret = boot_verify_image((unsigned char *)bootimg_addr,
1221 bootimg_size, "/recovery", &bootstate);
1222 }
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001223 else
Monika Singh7d2fc272018-03-16 17:16:01 +05301224 {
1225 ret = boot_verify_image((unsigned char *)bootimg_addr,
1226 bootimg_size, "/boot", &bootstate);
1227 }
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001228 boot_verify_print_state();
1229#else
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001230 ret = image_verify((unsigned char *)bootimg_addr,
1231 (unsigned char *)(bootimg_addr + bootimg_size),
1232 bootimg_size,
Sundarajan Srinivasance54d6e2013-11-11 12:45:00 -08001233 auth_algo);
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001234#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001235 dprintf(INFO, "Authenticating boot image: done return value = %d\n", ret);
1236
1237 if (ret)
1238 {
1239 /* Authorized kernel */
1240 device.is_tampered = 0;
Sundarajan Srinivasan3fb21f12013-09-16 18:36:15 -07001241 auth_kernel_img = 1;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001242 }
1243
Amit Blay4aa292f2015-04-28 21:55:59 +03001244#ifdef MDTP_SUPPORT
1245 {
1246 /* Verify MDTP lock.
1247 * For boot & recovery partitions, use aboot's verification result.
1248 */
1249 mdtp_ext_partition_verification_t ext_partition;
1250 ext_partition.partition = boot_into_recovery ? MDTP_PARTITION_RECOVERY : MDTP_PARTITION_BOOT;
1251 ext_partition.integrity_state = device.is_tampered ? MDTP_PARTITION_STATE_INVALID : MDTP_PARTITION_STATE_VALID;
1252 ext_partition.page_size = 0; /* Not needed since already validated */
1253 ext_partition.image_addr = 0; /* Not needed since already validated */
1254 ext_partition.image_size = 0; /* Not needed since already validated */
1255 ext_partition.sig_avail = FALSE; /* Not needed since already validated */
1256 mdtp_fwlock_verify_lock(&ext_partition);
1257 }
1258#endif /* MDTP_SUPPORT */
1259
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001260#if USE_PCOM_SECBOOT
1261 set_tamper_flag(device.is_tampered);
1262#endif
1263
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001264#if VERIFIED_BOOT
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001265 switch(boot_verify_get_state())
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001266 {
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001267 case RED:
lijuanga40d6302015-07-20 20:10:13 +08001268#if FBCON_DISPLAY_MSG
lijuang9a7d3b92015-11-30 14:41:24 +08001269 display_bootverify_menu(DISPLAY_MENU_RED);
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07001270#if ENABLE_VB_ATTEST
1271 mdelay(DELAY_WAIT);
1272 shutdown_device();
1273#else
lijuanga40d6302015-07-20 20:10:13 +08001274 wait_for_users_action();
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07001275#endif
lijuanga40d6302015-07-20 20:10:13 +08001276#else
1277 dprintf(CRITICAL,
1278 "Your device has failed verification and may not work properly.\nWait for 5 seconds before proceeding\n");
1279 mdelay(5000);
1280#endif
1281
1282 break;
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001283 case YELLOW:
lijuanga40d6302015-07-20 20:10:13 +08001284#if FBCON_DISPLAY_MSG
lijuang9a7d3b92015-11-30 14:41:24 +08001285 display_bootverify_menu(DISPLAY_MENU_YELLOW);
lijuanga40d6302015-07-20 20:10:13 +08001286 wait_for_users_action();
1287#else
1288 dprintf(CRITICAL,
1289 "Your device has loaded a different operating system.\nWait for 5 seconds before proceeding\n");
1290 mdelay(5000);
1291#endif
1292 break;
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001293 default:
lijuanga40d6302015-07-20 20:10:13 +08001294 break;
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001295 }
1296#endif
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001297#if !VERIFIED_BOOT
Unnati Gandhi1be04752015-03-27 19:41:53 +05301298 if(device.is_tampered)
1299 {
1300 write_device_info_mmc(&device);
1301 #ifdef TZ_TAMPER_FUSE
Monika Singh98257462018-06-06 11:28:49 +05301302 set_tamper_fuse_cmd(HLOS_IMG_TAMPER_FUSE);
Unnati Gandhi1be04752015-03-27 19:41:53 +05301303 #endif
1304 #ifdef ASSERT_ON_TAMPER
1305 dprintf(CRITICAL, "Device is tampered. Asserting..\n");
1306 ASSERT(0);
1307 #endif
1308 }
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001309#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001310}
1311
Monika Singh292b3e92018-03-17 22:40:23 +05301312int get_boot_image_info(void **image_buffer, uint32_t *imgsize,char *imgname)
1313{
1314 if (image_buffer == NULL || imgsize == NULL || imgname == NULL) {
1315 dprintf(CRITICAL, "get_boot_image_info: invalid parameters\n");
1316 return -1;
1317 }
1318
1319 if (!strncmp(info.images[0].name, imgname,
1320 strlen(imgname))) {
1321 *image_buffer = info.images[0].image_buffer;
1322 *imgsize = info.images[0].imgsize;
1323 return 0;
1324 }
1325 return -1;
1326}
1327
Maunik Shah0f3c8ac2014-03-02 17:47:58 +05301328static bool check_format_bit()
1329{
1330 bool ret = false;
1331 int index;
1332 uint64_t offset;
1333 struct boot_selection_info *in = NULL;
1334 char *buf = NULL;
1335
1336 index = partition_get_index("bootselect");
1337 if (index == INVALID_PTN)
1338 {
1339 dprintf(INFO, "Unable to locate /bootselect partition\n");
1340 return ret;
1341 }
1342 offset = partition_get_offset(index);
1343 if(!offset)
1344 {
1345 dprintf(INFO, "partition /bootselect doesn't exist\n");
1346 return ret;
1347 }
1348 buf = (char *) memalign(CACHE_LINE, ROUNDUP(page_size, CACHE_LINE));
Mayank Grover48860402016-11-29 12:34:53 +05301349 mmc_set_lun(partition_get_lun(index));
Maunik Shah0f3c8ac2014-03-02 17:47:58 +05301350 ASSERT(buf);
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301351 if (mmc_read(offset, (uint32_t *)buf, page_size))
Maunik Shah0f3c8ac2014-03-02 17:47:58 +05301352 {
1353 dprintf(INFO, "mmc read failure /bootselect %d\n", page_size);
1354 free(buf);
1355 return ret;
1356 }
1357 in = (struct boot_selection_info *) buf;
1358 if ((in->signature == BOOTSELECT_SIGNATURE) &&
1359 (in->version == BOOTSELECT_VERSION)) {
1360 if ((in->state_info & BOOTSELECT_FORMAT) &&
1361 !(in->state_info & BOOTSELECT_FACTORY))
1362 ret = true;
1363 } else {
1364 dprintf(CRITICAL, "Signature: 0x%08x or version: 0x%08x mismatched of /bootselect\n",
1365 in->signature, in->version);
1366 ASSERT(0);
1367 }
1368 free(buf);
1369 return ret;
1370}
1371
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001372void boot_verifier_init()
1373{
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001374 uint32_t boot_state;
1375 /* Check if device unlock */
1376 if(device.is_unlocked)
1377 {
1378 boot_verify_send_event(DEV_UNLOCK);
1379 boot_verify_print_state();
1380 dprintf(CRITICAL, "Device is unlocked! Skipping verification...\n");
1381 return;
1382 }
1383 else
1384 {
1385 boot_verify_send_event(BOOT_INIT);
1386 }
1387
1388 /* Initialize keystore */
1389 boot_state = boot_verify_keystore_init();
1390 if(boot_state == YELLOW)
1391 {
1392 boot_verify_print_state();
1393 dprintf(CRITICAL, "Keystore verification failed! Continuing anyways...\n");
1394 }
1395}
1396
Shashank Mittal23b8f422010-04-16 19:27:21 -07001397int boot_linux_from_mmc(void)
1398{
1399 struct boot_img_hdr *hdr = (void*) buf;
1400 struct boot_img_hdr *uhdr;
1401 unsigned offset = 0;
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07001402 int rcode;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001403 unsigned long long ptn = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001404 int index = INVALID_PTN;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001405
Shashank Mittalcd98d472011-08-02 14:29:24 -07001406 unsigned char *image_addr = 0;
1407 unsigned kernel_actual;
1408 unsigned ramdisk_actual;
1409 unsigned imagesize_actual;
Neeti Desai465491e2012-07-31 12:53:35 -07001410 unsigned second_actual = 0;
Neeti Desai465491e2012-07-31 12:53:35 -07001411
Matthew Qin271927e2015-03-31 22:07:07 -04001412 unsigned int dtb_size = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08001413 unsigned int out_len = 0;
1414 unsigned int out_avai_len = 0;
1415 unsigned char *out_addr = NULL;
1416 uint32_t dtb_offset = 0;
1417 unsigned char *kernel_start_addr = NULL;
1418 unsigned int kernel_size = 0;
Ameya Thakur10a33452016-06-13 14:24:26 -07001419 unsigned int patched_kernel_hdr_size = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08001420 int rc;
Monika Singh292b3e92018-03-17 22:40:23 +05301421#if VERIFIED_BOOT_2
1422 int status;
1423#endif
Mayank Groverc93cad82017-10-03 12:23:45 +05301424 char *ptn_name = NULL;
Neeti Desai465491e2012-07-31 12:53:35 -07001425#if DEVICE_TREE
1426 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -07001427 struct dt_entry dt_entry;
Neeti Desai465491e2012-07-31 12:53:35 -07001428 unsigned dt_table_offset;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001429 uint32_t dt_actual;
Deepa Dinamani19648b42013-09-05 17:05:55 -07001430 uint32_t dt_hdr_size;
Matthew Qin271927e2015-03-31 22:07:07 -04001431 unsigned char *best_match_dt_addr = NULL;
Neeti Desai465491e2012-07-31 12:53:35 -07001432#endif
Matthew Qin49e51fa2015-02-09 10:40:45 +08001433 struct kernel64_hdr *kptr = NULL;
Mayank Grover351a75e2017-05-30 20:06:08 +05301434 int current_active_slot = INVALID;
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001435
Maunik Shah0f3c8ac2014-03-02 17:47:58 +05301436 if (check_format_bit())
1437 boot_into_recovery = 1;
1438
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07001439 if (!boot_into_recovery) {
1440 memset(ffbm_mode_string, '\0', sizeof(ffbm_mode_string));
1441 rcode = get_ffbm(ffbm_mode_string, sizeof(ffbm_mode_string));
1442 if (rcode <= 0) {
1443 boot_into_ffbm = false;
1444 if (rcode < 0)
1445 dprintf(CRITICAL,"failed to get ffbm cookie");
1446 } else
1447 boot_into_ffbm = true;
1448 } else
1449 boot_into_ffbm = false;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001450 uhdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
1451 if (!memcmp(uhdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1452 dprintf(INFO, "Unified boot method!\n");
1453 hdr = uhdr;
1454 goto unified_boot;
1455 }
Mayank Groverc93cad82017-10-03 12:23:45 +05301456
1457 /* For a/b recovery image code is on boot partition.
1458 If we support multislot, always use boot partition. */
1459 if (boot_into_recovery &&
1460 (!partition_multislot_is_supported()))
1461 ptn_name = "recovery";
1462 else
1463 ptn_name = "boot";
1464
1465 index = partition_get_index(ptn_name);
1466 ptn = partition_get_offset(index);
1467 if(ptn == 0) {
1468 dprintf(CRITICAL, "ERROR: No %s partition found\n", ptn_name);
1469 return -1;
Kinson Chikf1a43512011-07-14 11:28:39 -07001470 }
Mayank Groverc93cad82017-10-03 12:23:45 +05301471
Channagoud Kadabief0547c2015-02-10 12:57:38 -08001472 /* Set Lun for boot & recovery partitions */
1473 mmc_set_lun(partition_get_lun(index));
Shashank Mittal23b8f422010-04-16 19:27:21 -07001474
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301475 if (mmc_read(ptn + offset, (uint32_t *) buf, page_size)) {
Shashank Mittal23b8f422010-04-16 19:27:21 -07001476 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
1477 return -1;
1478 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001479
1480 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001481 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Mayank Grover351a75e2017-05-30 20:06:08 +05301482 return ERR_INVALID_BOOT_MAGIC;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001483 }
1484
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07001485 if (hdr->page_size && (hdr->page_size != page_size)) {
vijay kumar2e21b3a2014-06-26 17:40:15 +05301486
1487 if (hdr->page_size > BOOT_IMG_MAX_PAGE_SIZE) {
1488 dprintf(CRITICAL, "ERROR: Invalid page size\n");
1489 return -1;
1490 }
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07001491 page_size = hdr->page_size;
1492 page_mask = page_size - 1;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001493 }
1494
Matthew Qin49e51fa2015-02-09 10:40:45 +08001495 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1496 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Kishor PKd8ddcad2017-07-27 13:53:57 +05301497 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001498
Matthew Qin49e51fa2015-02-09 10:40:45 +08001499 image_addr = (unsigned char *)target_get_scratch_address();
Kishor PK9e91b592017-05-24 12:14:55 +05301500 memcpy(image_addr, (void *)buf, page_size);
1501
1502 /* ensure commandline is terminated */
1503 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
Matthew Qin49e51fa2015-02-09 10:40:45 +08001504
1505#if DEVICE_TREE
Parth Dixit4097b622016-03-15 14:42:27 +05301506#ifndef OSVERSION_IN_BOOTIMAGE
1507 dt_size = hdr->dt_size;
1508#endif
1509 dt_actual = ROUND_TO_PAGE(dt_size, page_mask);
Kishor PKd8ddcad2017-07-27 13:53:57 +05301510 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 +05301511 dprintf(CRITICAL, "Integer overflow detected in bootimage header fields at %u in %s\n",__LINE__,__FILE__);
1512 return -1;
1513 }
Kishor PKd8ddcad2017-07-27 13:53:57 +05301514 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + second_actual + dt_actual);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001515#else
Kishor PKd8ddcad2017-07-27 13:53:57 +05301516 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 +05301517 dprintf(CRITICAL, "Integer overflow detected in bootimage header fields at %u in %s\n",__LINE__,__FILE__);
1518 return -1;
1519 }
Kishor PKd8ddcad2017-07-27 13:53:57 +05301520 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + second_actual);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001521#endif
1522
1523#if VERIFIED_BOOT
1524 boot_verifier_init();
1525#endif
1526
Vijay Kumar Pendoti9c002ad2016-03-09 13:52:45 +05301527 if (check_aboot_addr_range_overlap((uintptr_t) image_addr, imagesize_actual))
Matthew Qin49e51fa2015-02-09 10:40:45 +08001528 {
1529 dprintf(CRITICAL, "Boot image buffer address overlaps with aboot addresses.\n");
1530 return -1;
1531 }
1532
Matthew Qinbb7923d2015-02-09 10:56:09 +08001533 /*
1534 * Update loading flow of bootimage to support compressed/uncompressed
1535 * bootimage on both 64bit and 32bit platform.
1536 * 1. Load bootimage from emmc partition onto DDR.
1537 * 2. Check if bootimage is gzip format. If yes, decompress compressed kernel
1538 * 3. Check kernel header and update kernel load addr for 64bit and 32bit
1539 * platform accordingly.
1540 * 4. Sanity Check on kernel_addr and ramdisk_addr and copy data.
1541 */
Mayank Grover351a75e2017-05-30 20:06:08 +05301542 if (partition_multislot_is_supported())
1543 {
1544 current_active_slot = partition_find_active_slot();
1545 dprintf(INFO, "Loading boot image (%d) active_slot(%s): start\n",
1546 imagesize_actual, SUFFIX_SLOT(current_active_slot));
1547 }
1548 else
1549 {
1550 dprintf(INFO, "Loading (%s) image (%d): start\n",
1551 (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual);
1552 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08001553 bs_set_timestamp(BS_KERNEL_LOAD_START);
1554
Gaurav Nebhwani43e2a462016-03-17 21:32:56 +05301555 if ((target_get_max_flash_size() - page_size) < imagesize_actual)
1556 {
1557 dprintf(CRITICAL, "booimage size is greater than DDR can hold\n");
1558 return -1;
1559 }
Kishor PK9e91b592017-05-24 12:14:55 +05301560 offset = page_size;
1561 /* Read image without signature and header*/
1562 if (mmc_read(ptn + offset, (void *)(image_addr + offset), imagesize_actual - page_size))
Matthew Qin49e51fa2015-02-09 10:40:45 +08001563 {
1564 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
1565 return -1;
1566 }
1567
Mayank Grover351a75e2017-05-30 20:06:08 +05301568 if (partition_multislot_is_supported())
1569 {
1570 dprintf(INFO, "Loading boot image (%d) active_slot(%s): done\n",
1571 imagesize_actual, SUFFIX_SLOT(current_active_slot));
1572 }
1573 else
1574 {
1575 dprintf(INFO, "Loading (%s) image (%d): done\n",
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001576 (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual);
1577
Mayank Grover351a75e2017-05-30 20:06:08 +05301578 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08001579 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
1580
1581 /* Authenticate Kernel */
1582 dprintf(INFO, "use_signed_kernel=%d, is_unlocked=%d, is_tampered=%d.\n",
1583 (int) target_use_signed_kernel(),
1584 device.is_unlocked,
1585 device.is_tampered);
Monika Singh292b3e92018-03-17 22:40:23 +05301586#if VERIFIED_BOOT_2
1587 offset = imagesize_actual;
1588 if (check_aboot_addr_range_overlap((uintptr_t)image_addr + offset, page_size))
1589 {
1590 dprintf(CRITICAL, "Signature read buffer address overlaps with aboot addresses.\n");
1591 return -1;
1592 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08001593
Monika Singh292b3e92018-03-17 22:40:23 +05301594 /* Read signature */
1595 if(mmc_read(ptn + offset, (void *)(image_addr + offset), page_size))
1596 {
1597 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
1598 return -1;
1599 }
1600
1601 memset(&info, 0, sizeof(bootinfo));
1602 info.images[0].image_buffer = image_addr;
1603 info.images[0].imgsize = imagesize_actual;
1604 info.images[0].name = "boot";
1605 info.num_loaded_images = 0;
1606 info.multi_slot_boot = partition_multislot_is_supported();
1607 info.bootreason_alarm = boot_reason_alarm;
1608 info.bootinto_recovery = boot_into_recovery;
1609 status = load_image_and_auth(&info);
1610 if(status)
1611 return -1;
1612
1613 vbcmdline = info.vbcmdline;
1614#else
Channagoud Kadabi736c4962015-08-21 11:56:52 -07001615 /* Change the condition a little bit to include the test framework support.
1616 * We would never reach this point if device is in fastboot mode, even if we did
1617 * that means we are in test mode, so execute kernel authentication part for the
1618 * tests */
Parth Dixitcb0c6082015-12-30 15:01:13 +05301619 if((target_use_signed_kernel() && (!device.is_unlocked)) || is_test_mode_enabled())
Matthew Qin49e51fa2015-02-09 10:40:45 +08001620 {
1621 offset = imagesize_actual;
Vijay Kumar Pendoti9c002ad2016-03-09 13:52:45 +05301622 if (check_aboot_addr_range_overlap((uintptr_t)image_addr + offset, page_size))
Matthew Qin49e51fa2015-02-09 10:40:45 +08001623 {
1624 dprintf(CRITICAL, "Signature read buffer address overlaps with aboot addresses.\n");
1625 return -1;
1626 }
1627
1628 /* Read signature */
1629 if(mmc_read(ptn + offset, (void *)(image_addr + offset), page_size))
1630 {
1631 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
1632 return -1;
1633 }
1634
1635 verify_signed_bootimg((uint32_t)image_addr, imagesize_actual);
Channagoud Kadabi736c4962015-08-21 11:56:52 -07001636 /* The purpose of our test is done here */
Parth Dixitcb0c6082015-12-30 15:01:13 +05301637 if(is_test_mode_enabled() && auth_kernel_img)
Channagoud Kadabi736c4962015-08-21 11:56:52 -07001638 return 0;
Matthew Qin49e51fa2015-02-09 10:40:45 +08001639 } else {
1640 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
1641 #ifdef TZ_SAVE_KERNEL_HASH
1642 aboot_save_boot_hash_mmc((uint32_t) image_addr, imagesize_actual);
1643 #endif /* TZ_SAVE_KERNEL_HASH */
Amit Blay4aa292f2015-04-28 21:55:59 +03001644
1645#ifdef MDTP_SUPPORT
1646 {
1647 /* Verify MDTP lock.
1648 * For boot & recovery partitions, MDTP will use boot_verifier APIs,
1649 * since verification was skipped in aboot. The signature is not part of the loaded image.
1650 */
1651 mdtp_ext_partition_verification_t ext_partition;
1652 ext_partition.partition = boot_into_recovery ? MDTP_PARTITION_RECOVERY : MDTP_PARTITION_BOOT;
1653 ext_partition.integrity_state = MDTP_PARTITION_STATE_UNSET;
1654 ext_partition.page_size = page_size;
1655 ext_partition.image_addr = (uint32)image_addr;
1656 ext_partition.image_size = imagesize_actual;
1657 ext_partition.sig_avail = FALSE;
1658 mdtp_fwlock_verify_lock(&ext_partition);
1659 }
1660#endif /* MDTP_SUPPORT */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001661 }
Monika Singh292b3e92018-03-17 22:40:23 +05301662#endif
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001663
Sridhar Parasuram00bfedb2015-05-26 14:21:27 -07001664#if VERIFIED_BOOT
taozhang93088bd2016-11-16 15:50:46 +08001665 if((boot_verify_get_state() == ORANGE) && (!boot_into_ffbm))
Reut Zysmanba8a9d52016-02-18 11:44:04 +02001666 {
1667#if FBCON_DISPLAY_MSG
1668 display_bootverify_menu(DISPLAY_MENU_ORANGE);
1669 wait_for_users_action();
1670#else
1671 dprintf(CRITICAL,
1672 "Your device has been unlocked and can't be trusted.\nWait for 5 seconds before proceeding\n");
1673 mdelay(5000);
1674#endif
1675 }
1676#endif
1677
1678#if VERIFIED_BOOT
Monika Singh292b3e92018-03-17 22:40:23 +05301679 if (VB_M == target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +05301680 {
1681 /* set boot and system versions. */
1682 set_os_version((unsigned char *)image_addr);
1683 // send root of trust
1684 if(!send_rot_command((uint32_t)device.is_unlocked))
1685 ASSERT(0);
1686 }
Parth Dixitbc9b6492015-10-18 00:41:38 +05301687#endif
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001688 /*
Matthew Qinbb7923d2015-02-09 10:56:09 +08001689 * Check if the kernel image is a gzip package. If yes, need to decompress it.
1690 * If not, continue booting.
1691 */
1692 if (is_gzip_package((unsigned char *)(image_addr + page_size), hdr->kernel_size))
1693 {
1694 out_addr = (unsigned char *)(image_addr + imagesize_actual + page_size);
1695 out_avai_len = target_get_max_flash_size() - imagesize_actual - page_size;
Matthew Qin0b15b322015-05-19 05:20:54 -04001696 dprintf(INFO, "decompressing kernel image: start\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001697 rc = decompress((unsigned char *)(image_addr + page_size),
1698 hdr->kernel_size, out_addr, out_avai_len,
1699 &dtb_offset, &out_len);
1700 if (rc)
1701 {
Matthew Qin0b15b322015-05-19 05:20:54 -04001702 dprintf(CRITICAL, "decompressing kernel image failed!!!\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001703 ASSERT(0);
1704 }
1705
Matthew Qin0b15b322015-05-19 05:20:54 -04001706 dprintf(INFO, "decompressing kernel image: done\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001707 kptr = (struct kernel64_hdr *)out_addr;
1708 kernel_start_addr = out_addr;
1709 kernel_size = out_len;
1710 } else {
Ameya Thakur10a33452016-06-13 14:24:26 -07001711 dprintf(INFO, "Uncpmpressed kernel in use\n");
1712 if (!strncmp((char*)(image_addr + page_size),
1713 PATCHED_KERNEL_MAGIC,
1714 sizeof(PATCHED_KERNEL_MAGIC) - 1)) {
1715 dprintf(INFO, "Patched kernel detected\n");
1716 kptr = (struct kernel64_hdr *)(image_addr + page_size +
1717 PATCHED_KERNEL_HEADER_SIZE);
1718 //The size of the kernel is stored at start of kernel image + 16
1719 //The dtb would start just after the kernel
1720 dtb_offset = *((uint32_t*)((unsigned char*)
1721 (image_addr + page_size +
1722 sizeof(PATCHED_KERNEL_MAGIC) -
1723 1)));
1724 //The actual kernel starts after the 20 byte header.
1725 kernel_start_addr = (unsigned char*)(image_addr +
1726 page_size + PATCHED_KERNEL_HEADER_SIZE);
1727 kernel_size = hdr->kernel_size;
1728 patched_kernel_hdr_size = PATCHED_KERNEL_HEADER_SIZE;
1729 } else {
1730 dprintf(INFO, "Kernel image not patched..Unable to locate dt offset\n");
1731 kptr = (struct kernel64_hdr *)(image_addr + page_size);
1732 kernel_start_addr = (unsigned char *)(image_addr + page_size);
1733 kernel_size = hdr->kernel_size;
1734 }
Matthew Qinbb7923d2015-02-09 10:56:09 +08001735 }
1736
1737 /*
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001738 * Update the kernel/ramdisk/tags address if the boot image header
1739 * has default values, these default values come from mkbootimg when
1740 * the boot image is flashed using fastboot flash:raw
1741 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001742 update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr));
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001743
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001744 /* Get virtual addresses since the hdr saves physical addresses. */
1745 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
1746 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
1747 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001748
Matthew Qinbb7923d2015-02-09 10:56:09 +08001749 kernel_size = ROUND_TO_PAGE(kernel_size, page_mask);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001750 /* Check if the addresses in the header are valid. */
Matthew Qinbb7923d2015-02-09 10:56:09 +08001751 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_size) ||
Mayank Grovere559cec2017-10-17 15:12:03 +05301752 check_ddr_addr_range_bound(hdr->kernel_addr, kernel_size) ||
1753 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual) ||
1754 check_ddr_addr_range_bound(hdr->ramdisk_addr, ramdisk_actual))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001755 {
Mayank Grovere559cec2017-10-17 15:12:03 +05301756 dprintf(CRITICAL, "kernel/ramdisk addresses are not valid.\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001757 return -1;
1758 }
1759
1760#ifndef DEVICE_TREE
Mayank Grovere559cec2017-10-17 15:12:03 +05301761 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE) ||
1762 check_ddr_addr_range_bound(hdr->tags_addr, MAX_TAGS_SIZE))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001763 {
Mayank Grovere559cec2017-10-17 15:12:03 +05301764 dprintf(CRITICAL, "Tags addresses are not valid.\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001765 return -1;
1766 }
1767#endif
1768
Matthew Qin49e51fa2015-02-09 10:40:45 +08001769 /* Move kernel, ramdisk and device tree to correct address */
Matthew Qinbb7923d2015-02-09 10:56:09 +08001770 memmove((void*) hdr->kernel_addr, kernel_start_addr, kernel_size);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001771 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001772
Matthew Qin49e51fa2015-02-09 10:40:45 +08001773 #if DEVICE_TREE
Parth Dixit4097b622016-03-15 14:42:27 +05301774 if(dt_size) {
Matthew Qin49e51fa2015-02-09 10:40:45 +08001775 dt_table_offset = ((uint32_t)image_addr + page_size + kernel_actual + ramdisk_actual + second_actual);
1776 table = (struct dt_table*) dt_table_offset;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001777
Matthew Qin49e51fa2015-02-09 10:40:45 +08001778 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
1779 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1780 return -1;
1781 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001782
P.V. Phani Kumar82916762016-03-09 09:20:19 +05301783 /* Its Error if, dt_hdr_size (table->num_entries * dt_entry size + Dev_Tree Header)
1784 goes beyound hdr->dt_size*/
Parth Dixit4097b622016-03-15 14:42:27 +05301785 if (dt_hdr_size > ROUND_TO_PAGE(dt_size,hdr->page_size)) {
P.V. Phani Kumar82916762016-03-09 09:20:19 +05301786 dprintf(CRITICAL, "ERROR: Invalid Device Tree size \n");
1787 return -1;
1788 }
1789
Matthew Qin49e51fa2015-02-09 10:40:45 +08001790 /* Find index of device tree within device tree table */
1791 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
1792 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1793 return -1;
1794 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001795
P.V. Phani Kumar3c333202016-03-09 11:54:37 +05301796 if(dt_entry.offset > (UINT_MAX - dt_entry.size)) {
1797 dprintf(CRITICAL, "ERROR: Device tree contents are Invalid\n");
1798 return -1;
1799 }
1800
1801 /* Ensure we are not overshooting dt_size with the dt_entry selected */
Parth Dixit4097b622016-03-15 14:42:27 +05301802 if ((dt_entry.offset + dt_entry.size) > dt_size) {
P.V. Phani Kumar3c333202016-03-09 11:54:37 +05301803 dprintf(CRITICAL, "ERROR: Device tree contents are Invalid\n");
1804 return -1;
1805 }
1806
Matthew Qin271927e2015-03-31 22:07:07 -04001807 if (is_gzip_package((unsigned char *)dt_table_offset + dt_entry.offset, dt_entry.size))
1808 {
1809 unsigned int compressed_size = 0;
1810 out_addr += out_len;
1811 out_avai_len -= out_len;
Matthew Qin0b15b322015-05-19 05:20:54 -04001812 dprintf(INFO, "decompressing dtb: start\n");
Matthew Qin271927e2015-03-31 22:07:07 -04001813 rc = decompress((unsigned char *)dt_table_offset + dt_entry.offset,
1814 dt_entry.size, out_addr, out_avai_len,
1815 &compressed_size, &dtb_size);
1816 if (rc)
1817 {
Matthew Qin0b15b322015-05-19 05:20:54 -04001818 dprintf(CRITICAL, "decompressing dtb failed!!!\n");
Matthew Qin271927e2015-03-31 22:07:07 -04001819 ASSERT(0);
1820 }
1821
Matthew Qin0b15b322015-05-19 05:20:54 -04001822 dprintf(INFO, "decompressing dtb: done\n");
Matthew Qin271927e2015-03-31 22:07:07 -04001823 best_match_dt_addr = out_addr;
1824 } else {
1825 best_match_dt_addr = (unsigned char *)dt_table_offset + dt_entry.offset;
1826 dtb_size = dt_entry.size;
1827 }
1828
Matthew Qin49e51fa2015-02-09 10:40:45 +08001829 /* Validate and Read device device tree in the tags_addr */
Mayank Grovere559cec2017-10-17 15:12:03 +05301830 if (check_aboot_addr_range_overlap(hdr->tags_addr, dtb_size) ||
1831 check_ddr_addr_range_bound(hdr->tags_addr, dtb_size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001832 {
Mayank Grovere559cec2017-10-17 15:12:03 +05301833 dprintf(CRITICAL, "Device tree addresses are not valid\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001834 return -1;
1835 }
Shashank Mittal162244e2011-08-08 19:01:25 -07001836
Matthew Qin271927e2015-03-31 22:07:07 -04001837 memmove((void *)hdr->tags_addr, (char *)best_match_dt_addr, dtb_size);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001838 } else {
1839 /* Validate the tags_addr */
Mayank Grovere559cec2017-10-17 15:12:03 +05301840 if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual) ||
1841 check_ddr_addr_range_bound(hdr->tags_addr, kernel_actual))
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001842 {
Mayank Grovere559cec2017-10-17 15:12:03 +05301843 dprintf(CRITICAL, "Device tree addresses are not valid.\n");
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001844 return -1;
1845 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08001846 /*
1847 * If appended dev tree is found, update the atags with
1848 * memory address to the DTB appended location on RAM.
1849 * Else update with the atags address in the kernel header
1850 */
1851 void *dtb;
Ameya Thakur10a33452016-06-13 14:24:26 -07001852 dtb = dev_tree_appended(
1853 (void*)(image_addr + page_size +
1854 patched_kernel_hdr_size),
1855 hdr->kernel_size, dtb_offset,
1856 (void *)hdr->tags_addr);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001857 if (!dtb) {
1858 dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001859 return -1;
1860 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07001861 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08001862 #endif
Shashank Mittal23b8f422010-04-16 19:27:21 -07001863
Stanimir Varbanov69ec5462013-07-18 18:17:42 +03001864 if (boot_into_recovery && !device.is_unlocked && !device.is_tampered)
1865 target_load_ssd_keystore();
1866
Shashank Mittal23b8f422010-04-16 19:27:21 -07001867unified_boot:
Shashank Mittal23b8f422010-04-16 19:27:21 -07001868
Dima Zavin77e41f32013-03-06 16:10:43 -08001869 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07001870 (const char *)hdr->cmdline, board_machtype(),
Shashank Mittal23b8f422010-04-16 19:27:21 -07001871 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
1872
1873 return 0;
1874}
1875
Dima Zavin214cc642009-01-26 11:16:21 -08001876int boot_linux_from_flash(void)
1877{
1878 struct boot_img_hdr *hdr = (void*) buf;
Dima Zavin214cc642009-01-26 11:16:21 -08001879 struct ptentry *ptn;
1880 struct ptable *ptable;
1881 unsigned offset = 0;
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001882
Shashank Mittalcd98d472011-08-02 14:29:24 -07001883 unsigned char *image_addr = 0;
1884 unsigned kernel_actual;
1885 unsigned ramdisk_actual;
1886 unsigned imagesize_actual;
vijay kumar8f53e362015-11-24 13:38:11 +05301887 unsigned second_actual = 0;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001888
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001889#if DEVICE_TREE
Monika Singh292b3e92018-03-17 22:40:23 +05301890 struct dt_table *table = NULL;
Joel Kingaa335dc2013-06-03 16:11:08 -07001891 struct dt_entry dt_entry;
vijay kumar8f53e362015-11-24 13:38:11 +05301892 unsigned dt_table_offset;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001893 uint32_t dt_actual;
Monika Singh292b3e92018-03-17 22:40:23 +05301894 uint32_t dt_hdr_size = 0;
Vivek Kumar07bd0862018-04-10 14:19:23 +05301895 uint32_t dtb_offset = 0;
vijay kumar8f53e362015-11-24 13:38:11 +05301896 unsigned int dtb_size = 0;
1897 unsigned char *best_match_dt_addr = NULL;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001898#endif
1899
David Ng183a7422009-12-07 14:55:21 -08001900 if (target_is_emmc_boot()) {
1901 hdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
1902 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1903 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
1904 return -1;
1905 }
1906 goto continue_boot;
1907 }
1908
Dima Zavin214cc642009-01-26 11:16:21 -08001909 ptable = flash_get_ptable();
1910 if (ptable == NULL) {
1911 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1912 return -1;
1913 }
1914
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001915 if(!boot_into_recovery)
1916 {
1917 ptn = ptable_find(ptable, "boot");
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001918
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001919 if (ptn == NULL) {
1920 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1921 return -1;
1922 }
1923 }
1924 else
1925 {
1926 ptn = ptable_find(ptable, "recovery");
1927 if (ptn == NULL) {
1928 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
1929 return -1;
1930 }
Dima Zavin214cc642009-01-26 11:16:21 -08001931 }
1932
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05301933 /* Read boot.img header from flash */
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001934 if (flash_read(ptn, offset, buf, page_size)) {
Dima Zavin214cc642009-01-26 11:16:21 -08001935 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
1936 return -1;
1937 }
Dima Zavin214cc642009-01-26 11:16:21 -08001938
1939 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001940 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Dima Zavin214cc642009-01-26 11:16:21 -08001941 return -1;
1942 }
1943
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001944 if (hdr->page_size != page_size) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001945 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 -08001946 return -1;
1947 }
1948
Kishor PK9e91b592017-05-24 12:14:55 +05301949 image_addr = (unsigned char *)target_get_scratch_address();
1950 memcpy(image_addr, (void *)buf, page_size);
vijay kumar287bb542015-09-29 13:01:52 +05301951
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001952 /*
1953 * Update the kernel/ramdisk/tags address if the boot image header
1954 * has default values, these default values come from mkbootimg when
1955 * the boot image is flashed using fastboot flash:raw
1956 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001957 update_ker_tags_rdisk_addr(hdr, false);
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001958
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001959 /* Get virtual addresses since the hdr saves physical addresses. */
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001960 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
1961 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
1962 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
1963
1964 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1965 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Mayank Grover032736f2017-07-14 20:34:51 +05301966 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001967
Kishor PK9e91b592017-05-24 12:14:55 +05301968 /* ensure commandline is terminated */
1969 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
1970
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001971 /* Check if the addresses in the header are valid. */
1972 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
Mayank Grovere559cec2017-10-17 15:12:03 +05301973 check_ddr_addr_range_bound(hdr->kernel_addr, kernel_actual) ||
1974 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual) ||
1975 check_ddr_addr_range_bound(hdr->ramdisk_addr, ramdisk_actual))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001976 {
Mayank Grovere559cec2017-10-17 15:12:03 +05301977 dprintf(CRITICAL, "kernel/ramdisk addresses are not valid.\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001978 return -1;
1979 }
1980
1981#ifndef DEVICE_TREE
Kishor PKd8ddcad2017-07-27 13:53:57 +05301982 if (UINT_MAX < ((uint64_t)kernel_actual + (uint64_t)ramdisk_actual+ (uint64_t)second_actual + page_size)) {
Mayank Grover032736f2017-07-14 20:34:51 +05301983 dprintf(CRITICAL, "Integer overflow detected in bootimage header fields\n");
1984 return -1;
1985 }
Kishor PKd8ddcad2017-07-27 13:53:57 +05301986 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + second_actual);
Mayank Grover032736f2017-07-14 20:34:51 +05301987
Mayank Grovere559cec2017-10-17 15:12:03 +05301988 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE) ||
1989 check_ddr_addr_range_bound(hdr->tags_addr, MAX_TAGS_SIZE))
Vijay Kumar Pendotid3ed20e2016-09-20 00:34:46 +05301990 {
Mayank Grovere559cec2017-10-17 15:12:03 +05301991 dprintf(CRITICAL, "Tags addresses are not valid.\n");
Vijay Kumar Pendotid3ed20e2016-09-20 00:34:46 +05301992 return -1;
1993 }
1994#else
1995
1996#ifndef OSVERSION_IN_BOOTIMAGE
1997 dt_size = hdr->dt_size;
1998#endif
Mayank Grover032736f2017-07-14 20:34:51 +05301999 dt_actual = ROUND_TO_PAGE(dt_size, page_mask);
Kishor PKd8ddcad2017-07-27 13:53:57 +05302000 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 +05302001 dprintf(CRITICAL, "Integer overflow detected in bootimage header fields\n");
2002 return -1;
2003 }
2004
Kishor PKd8ddcad2017-07-27 13:53:57 +05302005 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + second_actual + dt_actual);
Mayank Grover032736f2017-07-14 20:34:51 +05302006
Mayank Grovere559cec2017-10-17 15:12:03 +05302007 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_size) ||
2008 check_ddr_addr_range_bound(hdr->tags_addr, dt_size))
Mayank Grover032736f2017-07-14 20:34:51 +05302009 {
Mayank Grovere559cec2017-10-17 15:12:03 +05302010 dprintf(CRITICAL, "Device tree addresses are not valid.\n");
Mayank Grover032736f2017-07-14 20:34:51 +05302011 return -1;
2012 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002013#endif
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002014
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05302015 /* Read full boot.img from flash */
2016 dprintf(INFO, "Loading (%s) image (%d): start\n",
2017 (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual);
2018 bs_set_timestamp(BS_KERNEL_LOAD_START);
2019
2020 if (UINT_MAX - page_size < imagesize_actual)
2021 {
2022 dprintf(CRITICAL,"Integer overflow detected in bootimage header fields %u %s\n", __LINE__,__func__);
2023 return -1;
2024 }
2025
2026 /*Check the availability of RAM before reading boot image + max signature length from flash*/
2027 if (target_get_max_flash_size() < (imagesize_actual + page_size))
2028 {
2029 dprintf(CRITICAL, "bootimage size is greater than DDR can hold\n");
2030 return -1;
2031 }
2032
2033 offset = page_size;
2034 /* Read image without signature and header */
2035 if (flash_read(ptn, offset, (void *)(image_addr + offset), imagesize_actual - page_size))
2036 {
2037 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
2038 return -1;
2039 }
2040
2041 dprintf(INFO, "Loading (%s) image (%d): done\n",
2042 (!boot_into_recovery ? "boot" : "recovery"), imagesize_actual);
2043 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
2044
Shashank Mittalcd98d472011-08-02 14:29:24 -07002045 /* Authenticate Kernel */
Deepa Dinamani23b60d42013-06-24 18:10:52 -07002046 if(target_use_signed_kernel() && (!device.is_unlocked))
Shashank Mittalcd98d472011-08-02 14:29:24 -07002047 {
Shashank Mittalcd98d472011-08-02 14:29:24 -07002048 offset = imagesize_actual;
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05302049
Shashank Mittalcd98d472011-08-02 14:29:24 -07002050 /* Read signature */
2051 if (flash_read(ptn, offset, (void *)(image_addr + offset), page_size))
2052 {
2053 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002054 return -1;
Shashank Mittalcd98d472011-08-02 14:29:24 -07002055 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07002056
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05302057 verify_signed_bootimg((uint32_t)image_addr, imagesize_actual);
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05302058 }
2059 offset = page_size;
2060 if(hdr->second_size != 0) {
2061 if (UINT_MAX - offset < second_actual)
2062 {
2063 dprintf(CRITICAL, "ERROR: Integer overflow in boot image header %s\t%d\n",__func__,__LINE__);
2064 return -1;
vijay kumar8f53e362015-11-24 13:38:11 +05302065 }
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05302066 offset += second_actual;
2067 /* Second image loading not implemented. */
2068 ASSERT(0);
2069 }
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05302070 /* Move kernel and ramdisk to correct address */
2071 memmove((void*) hdr->kernel_addr, (char*) (image_addr + page_size), hdr->kernel_size);
2072 memmove((void*) hdr->ramdisk_addr, (char*) (image_addr + page_size + kernel_actual), hdr->ramdisk_size);
2073
2074#if DEVICE_TREE
2075 if(dt_size != 0) {
2076
2077 dt_table_offset = ((uint32_t)image_addr + page_size + kernel_actual + ramdisk_actual + second_actual);
2078
2079 table = (struct dt_table*) dt_table_offset;
2080
2081 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
2082 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
2083 return -1;
2084 }
2085
2086 /* Its Error if, dt_hdr_size (table->num_entries * dt_entry size + Dev_Tree Header)
2087 goes beyound hdr->dt_size*/
2088 if (dt_hdr_size > ROUND_TO_PAGE(dt_size,hdr->page_size)) {
2089 dprintf(CRITICAL, "ERROR: Invalid Device Tree size \n");
2090 return -1;
2091 }
2092
2093 /* Find index of device tree within device tree table */
2094 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
2095 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
2096 return -1;
2097 }
2098
2099 /* Validate and Read device device tree in the "tags_add */
2100 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size) ||
2101 check_ddr_addr_range_bound(hdr->tags_addr, dt_entry.size))
2102 {
2103 dprintf(CRITICAL, "Device tree addresses are not valid.\n");
2104 return -1;
2105 }
2106
2107 if(dt_entry.offset > (UINT_MAX - dt_entry.size)) {
2108 dprintf(CRITICAL, "ERROR: Device tree contents are Invalid\n");
2109 return -1;
2110 }
2111
2112 /* Ensure we are not overshooting dt_size with the dt_entry selected */
2113 if ((dt_entry.offset + dt_entry.size) > dt_size) {
2114 dprintf(CRITICAL, "ERROR: Device tree contents are Invalid\n");
2115 return -1;
2116 }
2117
2118 best_match_dt_addr = (unsigned char *)table + dt_entry.offset;
2119 dtb_size = dt_entry.size;
2120 memmove((void *)hdr->tags_addr, (char *)best_match_dt_addr, dtb_size);
Vivek Kumar07bd0862018-04-10 14:19:23 +05302121
2122 } else {
2123 /* Validate the tags_addr */
2124 if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual) ||
2125 check_ddr_addr_range_bound(hdr->tags_addr, kernel_actual))
2126 {
2127 dprintf(CRITICAL, "Device tree addresses are not valid.\n");
2128 return -1;
2129 }
2130 /*
2131 * If appended dev tree is found, update the atags with
2132 * memory address to the DTB appended location on RAM.
2133 * Else update with the atags address in the kernel header
2134 */
2135 void *dtb = NULL;
2136 dtb = dev_tree_appended((void*)(image_addr + page_size ),hdr->kernel_size, dtb_offset, (void *)hdr->tags_addr);
2137 if (!dtb) {
2138 dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
2139 return -1;
2140 }
2141 }
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05302142#endif
2143 if(target_use_signed_kernel() && (!device.is_unlocked))
2144 {
Shashank Mittal162244e2011-08-08 19:01:25 -07002145 /* Make sure everything from scratch address is read before next step!*/
Shashank Mittala0032282011-08-26 14:50:11 -07002146 if(device.is_tampered)
Shashank Mittal162244e2011-08-08 19:01:25 -07002147 {
2148 write_device_info_flash(&device);
2149 }
Channagoud Kadabi5c86fe32012-02-16 10:58:48 +05302150#if USE_PCOM_SECBOOT
2151 set_tamper_flag(device.is_tampered);
2152#endif
Shashank Mittalcd98d472011-08-02 14:29:24 -07002153 }
David Ng183a7422009-12-07 14:55:21 -08002154continue_boot:
Dima Zavin214cc642009-01-26 11:16:21 -08002155
Dima Zavin214cc642009-01-26 11:16:21 -08002156 /* TODO: create/pass atags to kernel */
2157
Ajay Dudanie28a6072011-07-01 13:59:46 -07002158 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07002159 (const char *)hdr->cmdline, board_machtype(),
Dima Zavin214cc642009-01-26 11:16:21 -08002160 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
2161
2162 return 0;
2163}
Brian Swetland9c4c0752009-01-25 16:23:50 -08002164
Shashank Mittal162244e2011-08-08 19:01:25 -07002165void write_device_info_mmc(device_info *dev)
2166{
Shashank Mittal162244e2011-08-08 19:01:25 -07002167 unsigned long long ptn = 0;
2168 unsigned long long size;
2169 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002170 uint8_t lun = 0;
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002171 uint32_t ret = 0;
Mayank Groverddd348d2018-01-23 14:07:09 +05302172 uint32_t device_info_sz = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07002173
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002174 if (devinfo_present)
2175 index = partition_get_index("devinfo");
2176 else
2177 index = partition_get_index("aboot");
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07002178
Shashank Mittal162244e2011-08-08 19:01:25 -07002179 ptn = partition_get_offset(index);
2180 if(ptn == 0)
2181 {
2182 return;
2183 }
2184
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002185 lun = partition_get_lun(index);
2186 mmc_set_lun(lun);
2187
Shashank Mittal162244e2011-08-08 19:01:25 -07002188 size = partition_get_size(index);
2189
Mayank Groverddd348d2018-01-23 14:07:09 +05302190 device_info_sz = ROUND_TO_PAGE(sizeof(struct device_info),
2191 mmc_blocksize_mask);
2192 if (device_info_sz == UINT_MAX)
2193 {
2194 dprintf(CRITICAL, "ERROR: Incorrect blocksize of card\n");
2195 return;
2196 }
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07002197
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002198 if (devinfo_present)
Mayank Groverddd348d2018-01-23 14:07:09 +05302199 ret = mmc_write(ptn, device_info_sz, (void *)info_buf);
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002200 else
Mayank Groverddd348d2018-01-23 14:07:09 +05302201 ret = mmc_write((ptn + size - device_info_sz), device_info_sz, (void *)info_buf);
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002202 if (ret)
Shashank Mittal162244e2011-08-08 19:01:25 -07002203 {
2204 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
Sridhar Parasurame1a97922015-07-27 15:31:22 -07002205 ASSERT(0);
Shashank Mittal162244e2011-08-08 19:01:25 -07002206 }
2207}
2208
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002209void read_device_info_mmc(struct device_info *info)
Shashank Mittal162244e2011-08-08 19:01:25 -07002210{
Shashank Mittal162244e2011-08-08 19:01:25 -07002211 unsigned long long ptn = 0;
2212 unsigned long long size;
2213 int index = INVALID_PTN;
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002214 uint32_t ret = 0;
Mayank Groverddd348d2018-01-23 14:07:09 +05302215 uint32_t device_info_sz = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07002216
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002217 if ((index = partition_get_index("devinfo")) < 0)
2218 {
2219 devinfo_present = false;
2220 index = partition_get_index("aboot");
2221 }
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07002222
Shashank Mittal162244e2011-08-08 19:01:25 -07002223 ptn = partition_get_offset(index);
2224 if(ptn == 0)
2225 {
2226 return;
2227 }
2228
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07002229 mmc_set_lun(partition_get_lun(index));
2230
Shashank Mittal162244e2011-08-08 19:01:25 -07002231 size = partition_get_size(index);
2232
Mayank Groverddd348d2018-01-23 14:07:09 +05302233 device_info_sz = ROUND_TO_PAGE(sizeof(struct device_info),
2234 mmc_blocksize_mask);
2235 if (device_info_sz == UINT_MAX)
2236 {
2237 dprintf(CRITICAL, "ERROR: Incorrect blocksize of card\n");
2238 return;
2239 }
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07002240
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002241 if (devinfo_present)
Mayank Groverddd348d2018-01-23 14:07:09 +05302242 ret = mmc_read(ptn, (void *)info_buf, device_info_sz);
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002243 else
Mayank Groverddd348d2018-01-23 14:07:09 +05302244 ret = mmc_read((ptn + size - device_info_sz), (void *)info_buf, device_info_sz);
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002245 if (ret)
Shashank Mittal162244e2011-08-08 19:01:25 -07002246 {
2247 dprintf(CRITICAL, "ERROR: Cannot read device info\n");
Sridhar Parasurame1a97922015-07-27 15:31:22 -07002248 ASSERT(0);
Shashank Mittal162244e2011-08-08 19:01:25 -07002249 }
Shashank Mittal162244e2011-08-08 19:01:25 -07002250}
2251
2252void write_device_info_flash(device_info *dev)
2253{
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002254 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
Shashank Mittal162244e2011-08-08 19:01:25 -07002255 struct ptentry *ptn;
2256 struct ptable *ptable;
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002257 if(info == NULL)
2258 {
2259 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
2260 ASSERT(0);
2261 }
2262 info_buf = info;
Shashank Mittal162244e2011-08-08 19:01:25 -07002263 ptable = flash_get_ptable();
2264 if (ptable == NULL)
2265 {
2266 dprintf(CRITICAL, "ERROR: Partition table not found\n");
2267 return;
2268 }
2269
2270 ptn = ptable_find(ptable, "devinfo");
2271 if (ptn == NULL)
2272 {
Smita Ghosh670c8b82015-05-07 09:30:03 -07002273 dprintf(CRITICAL, "ERROR: No devinfo partition found\n");
Shashank Mittal162244e2011-08-08 19:01:25 -07002274 return;
2275 }
2276
Mayank Groverdedbc892017-10-24 13:41:34 +05302277 memset(info, 0, BOOT_IMG_MAX_PAGE_SIZE);
Shashank Mittal162244e2011-08-08 19:01:25 -07002278 memcpy(info, dev, sizeof(device_info));
2279
2280 if (flash_write(ptn, 0, (void *)info_buf, page_size))
2281 {
2282 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
2283 return;
2284 }
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002285 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07002286}
2287
vijay kumarc65876c2015-04-24 13:29:16 +05302288static int read_allow_oem_unlock(device_info *dev)
2289{
vijay kumarc65876c2015-04-24 13:29:16 +05302290 unsigned offset;
2291 int index;
2292 unsigned long long ptn;
2293 unsigned long long ptn_size;
2294 unsigned blocksize = mmc_get_device_blocksize();
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07002295 STACKBUF_DMA_ALIGN(buf, blocksize);
vijay kumarc65876c2015-04-24 13:29:16 +05302296
vijay kumarca2e6812015-07-08 20:28:25 +05302297 index = partition_get_index(frp_ptns[0]);
vijay kumarc65876c2015-04-24 13:29:16 +05302298 if (index == INVALID_PTN)
2299 {
vijay kumarca2e6812015-07-08 20:28:25 +05302300 index = partition_get_index(frp_ptns[1]);
2301 if (index == INVALID_PTN)
2302 {
2303 dprintf(CRITICAL, "Neither '%s' nor '%s' partition found\n", frp_ptns[0],frp_ptns[1]);
2304 return -1;
2305 }
vijay kumarc65876c2015-04-24 13:29:16 +05302306 }
2307
2308 ptn = partition_get_offset(index);
2309 ptn_size = partition_get_size(index);
2310 offset = ptn_size - blocksize;
2311
Mayank Grover48860402016-11-29 12:34:53 +05302312 /* Set Lun for partition */
2313 mmc_set_lun(partition_get_lun(index));
2314
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07002315 if (mmc_read(ptn + offset, (void *)buf, blocksize))
vijay kumarc65876c2015-04-24 13:29:16 +05302316 {
2317 dprintf(CRITICAL, "Reading MMC failed\n");
2318 return -1;
2319 }
2320
2321 /*is_allow_unlock is a bool value stored at the LSB of last byte*/
2322 is_allow_unlock = buf[blocksize-1] & 0x01;
2323 return 0;
2324}
2325
2326static int write_allow_oem_unlock(bool allow_unlock)
2327{
vijay kumarc65876c2015-04-24 13:29:16 +05302328 unsigned offset;
vijay kumarc65876c2015-04-24 13:29:16 +05302329 int index;
2330 unsigned long long ptn;
2331 unsigned long long ptn_size;
2332 unsigned blocksize = mmc_get_device_blocksize();
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07002333 STACKBUF_DMA_ALIGN(buf, blocksize);
vijay kumarc65876c2015-04-24 13:29:16 +05302334
vijay kumarca2e6812015-07-08 20:28:25 +05302335 index = partition_get_index(frp_ptns[0]);
vijay kumarc65876c2015-04-24 13:29:16 +05302336 if (index == INVALID_PTN)
2337 {
vijay kumarca2e6812015-07-08 20:28:25 +05302338 index = partition_get_index(frp_ptns[1]);
2339 if (index == INVALID_PTN)
2340 {
2341 dprintf(CRITICAL, "Neither '%s' nor '%s' partition found\n", frp_ptns[0],frp_ptns[1]);
2342 return -1;
2343 }
vijay kumarc65876c2015-04-24 13:29:16 +05302344 }
2345
2346 ptn = partition_get_offset(index);
2347 ptn_size = partition_get_size(index);
2348 offset = ptn_size - blocksize;
Mayank Grover48860402016-11-29 12:34:53 +05302349 mmc_set_lun(partition_get_lun(index));
vijay kumarc65876c2015-04-24 13:29:16 +05302350
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07002351 if (mmc_read(ptn + offset, (void *)buf, blocksize))
vijay kumarc65876c2015-04-24 13:29:16 +05302352 {
2353 dprintf(CRITICAL, "Reading MMC failed\n");
2354 return -1;
2355 }
2356
2357 /*is_allow_unlock is a bool value stored at the LSB of last byte*/
2358 buf[blocksize-1] = allow_unlock;
2359 if (mmc_write(ptn + offset, blocksize, buf))
2360 {
2361 dprintf(CRITICAL, "Writing MMC failed\n");
2362 return -1;
2363 }
2364
2365 return 0;
2366}
2367
Shashank Mittal162244e2011-08-08 19:01:25 -07002368void read_device_info_flash(device_info *dev)
2369{
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002370 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
Shashank Mittal162244e2011-08-08 19:01:25 -07002371 struct ptentry *ptn;
2372 struct ptable *ptable;
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002373 if(info == NULL)
2374 {
2375 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
2376 ASSERT(0);
2377 }
2378 info_buf = info;
Shashank Mittal162244e2011-08-08 19:01:25 -07002379 ptable = flash_get_ptable();
2380 if (ptable == NULL)
2381 {
2382 dprintf(CRITICAL, "ERROR: Partition table not found\n");
2383 return;
2384 }
2385
2386 ptn = ptable_find(ptable, "devinfo");
2387 if (ptn == NULL)
2388 {
Smita Ghosh670c8b82015-05-07 09:30:03 -07002389 dprintf(CRITICAL, "ERROR: No devinfo partition found\n");
Shashank Mittal162244e2011-08-08 19:01:25 -07002390 return;
2391 }
2392
2393 if (flash_read(ptn, 0, (void *)info_buf, page_size))
2394 {
2395 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
2396 return;
2397 }
2398
2399 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
2400 {
Shashank Mittal162244e2011-08-08 19:01:25 -07002401 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
2402 info->is_unlocked = 0;
Shashank Mittala0032282011-08-26 14:50:11 -07002403 info->is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07002404 write_device_info_flash(info);
2405 }
2406 memcpy(dev, info, sizeof(device_info));
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002407 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07002408}
2409
2410void write_device_info(device_info *dev)
2411{
2412 if(target_is_emmc_boot())
2413 {
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002414 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
2415 if(info == NULL)
2416 {
2417 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
2418 ASSERT(0);
2419 }
2420 info_buf = info;
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002421 memcpy(info, dev, sizeof(struct device_info));
2422
2423#if USE_RPMB_FOR_DEVINFO
Mayank Grover912eaa62017-10-26 12:08:53 +05302424 if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05302425 is_secure_boot_enable()) {
2426 if((write_device_info_rpmb((void*) info, PAGE_SIZE)) < 0)
2427 ASSERT(0);
Sridhar Parasurame1a97922015-07-27 15:31:22 -07002428 }
Channagoud Kadabic80cb492015-04-28 16:08:28 -07002429 else
2430 write_device_info_mmc(info);
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002431#else
2432 write_device_info_mmc(info);
2433#endif
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002434 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07002435 }
2436 else
2437 {
2438 write_device_info_flash(dev);
2439 }
2440}
2441
Monika Singh94316462018-03-15 18:39:01 +05302442int read_rollback_index(uint32_t loc, uint64_t *roll_back_index)
2443{
2444 if (!devinfo_present) {
2445 dprintf(CRITICAL, "DeviceInfo not initalized \n");
2446 return -EINVAL;
2447 }
2448 if (loc >= ARRAY_SIZE(device.rollback_index)) {
2449 dprintf(CRITICAL, "%s() Loc out of range index: %d, array len: %d\n",
2450 __func__, loc, ARRAY_SIZE(device.rollback_index));
2451 ASSERT(0);
2452 }
2453
2454 *roll_back_index = device.rollback_index[loc];
2455 return 0;
2456}
2457
2458int write_rollback_index(uint32_t loc, uint64_t roll_back_index)
2459{
2460 if (!devinfo_present) {
2461 dprintf(CRITICAL, "DeviceInfo not initalized \n");
2462 return -EINVAL;
2463 }
2464 if (loc >= ARRAY_SIZE(device.rollback_index)) {
2465 dprintf(CRITICAL, "%s() Loc out of range index: %d, array len: %d\n",
2466 __func__, loc, ARRAY_SIZE(device.rollback_index));
2467 ASSERT(0);
2468 }
2469
2470 device.rollback_index[loc] = roll_back_index;
2471 write_device_info(&device);
2472 return 0;
2473}
2474
Monika Singhb0fad822018-03-15 18:50:55 +05302475int store_userkey(uint8_t *user_key, uint32_t user_key_size)
2476{
2477 if (!devinfo_present) {
2478 dprintf(CRITICAL, "DeviceInfo not initalized \n");
2479 return -EINVAL;
2480 }
2481
2482 if (user_key_size > ARRAY_SIZE(device.user_public_key)) {
2483 dprintf(CRITICAL, "StoreUserKey, UserKeySize too large!\n");
2484 return -ENODEV;
2485 }
2486
2487 memcpy(device.user_public_key, user_key, user_key_size);
2488 device.user_public_key_length = user_key_size;
2489 write_device_info(&device);
2490 return 0;
2491}
2492
2493int erase_userkey()
2494{
2495 if (!devinfo_present) {
2496 dprintf(CRITICAL, "DeviceInfo not initalized \n");
2497 return -EINVAL;
2498 }
2499 memset(device.user_public_key, 0, ARRAY_SIZE(device.user_public_key));
2500 device.user_public_key_length = 0;
2501 write_device_info(&device);
2502 return 0;
2503}
2504
2505int get_userkey(uint8_t **user_key, uint32_t *user_key_size)
2506{
2507 if (!devinfo_present) {
2508 dprintf(CRITICAL, "DeviceInfo not initalized \n");
2509 return -EINVAL;
2510 }
2511 *user_key = device.user_public_key;
2512 *user_key_size = device.user_public_key_length;
2513 return 0;
2514}
2515
Shashank Mittal162244e2011-08-08 19:01:25 -07002516void read_device_info(device_info *dev)
2517{
2518 if(target_is_emmc_boot())
2519 {
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002520 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
2521 if(info == NULL)
2522 {
2523 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
2524 ASSERT(0);
2525 }
2526 info_buf = info;
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002527
2528#if USE_RPMB_FOR_DEVINFO
Mayank Grover912eaa62017-10-26 12:08:53 +05302529 if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05302530 is_secure_boot_enable()) {
2531 if((read_device_info_rpmb((void*) info, PAGE_SIZE)) < 0)
2532 ASSERT(0);
Sridhar Parasurame1a97922015-07-27 15:31:22 -07002533 }
Channagoud Kadabic80cb492015-04-28 16:08:28 -07002534 else
2535 read_device_info_mmc(info);
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002536#else
2537 read_device_info_mmc(info);
2538#endif
2539
2540 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
2541 {
2542 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
lijuang511a2b52015-08-14 20:50:51 +08002543 if (is_secure_boot_enable()) {
Channagoud Kadabi05f78ba2015-07-06 11:58:14 -07002544 info->is_unlocked = 0;
Monika Singh292b3e92018-03-17 22:40:23 +05302545#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover912eaa62017-10-26 12:08:53 +05302546 if (VB_M <= target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +05302547 info->is_unlock_critical = 0;
Parth Dixitddbc7352015-10-18 03:13:31 +05302548#endif
lijuang511a2b52015-08-14 20:50:51 +08002549 } else {
Channagoud Kadabi2fda4092015-07-07 13:34:11 -07002550 info->is_unlocked = 1;
Monika Singh292b3e92018-03-17 22:40:23 +05302551#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover912eaa62017-10-26 12:08:53 +05302552 if (VB_M <= target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +05302553 info->is_unlock_critical = 1;
Parth Dixitddbc7352015-10-18 03:13:31 +05302554#endif
lijuang511a2b52015-08-14 20:50:51 +08002555 }
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002556 info->is_tampered = 0;
2557 info->charger_screen_enabled = 0;
Monika Singh292b3e92018-03-17 22:40:23 +05302558#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover912eaa62017-10-26 12:08:53 +05302559 if (VB_M <= target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +05302560 info->verity_mode = 1; //enforcing by default
Parth Dixitddbc7352015-10-18 03:13:31 +05302561#endif
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002562 write_device_info(info);
2563 }
2564 memcpy(dev, info, sizeof(device_info));
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002565 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07002566 }
2567 else
2568 {
2569 read_device_info_flash(dev);
2570 }
2571}
2572
2573void reset_device_info()
2574{
2575 dprintf(ALWAYS, "reset_device_info called.");
Shashank Mittala0032282011-08-26 14:50:11 -07002576 device.is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07002577 write_device_info(&device);
2578}
2579
2580void set_device_root()
2581{
2582 dprintf(ALWAYS, "set_device_root called.");
Shashank Mittala0032282011-08-26 14:50:11 -07002583 device.is_tampered = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -07002584 write_device_info(&device);
2585}
2586
lijuang4ece1e72015-08-14 21:02:36 +08002587/* set device unlock value
2588 * Must check FRP before call this function
2589 * Need to wipe data when unlock status changed
2590 * type 0: oem unlock
2591 * type 1: unlock critical
2592 * status 0: unlock as false
2593 * status 1: lock as true
2594 */
2595void set_device_unlock_value(int type, bool status)
lijuang21f12f52015-08-22 16:22:19 +08002596{
lijuang4ece1e72015-08-14 21:02:36 +08002597 if (type == UNLOCK)
2598 device.is_unlocked = status;
Monika Singh292b3e92018-03-17 22:40:23 +05302599#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover912eaa62017-10-26 12:08:53 +05302600 else if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05302601 type == UNLOCK_CRITICAL)
2602 device.is_unlock_critical = status;
Parth Dixitddbc7352015-10-18 03:13:31 +05302603#endif
lijuang4ece1e72015-08-14 21:02:36 +08002604 write_device_info(&device);
2605}
2606
2607static void set_device_unlock(int type, bool status)
2608{
2609 int is_unlocked = -1;
2610 char response[MAX_RSP_SIZE];
2611
2612 /* check device unlock status if it is as expected */
2613 if (type == UNLOCK)
2614 is_unlocked = device.is_unlocked;
Monika Singh292b3e92018-03-17 22:40:23 +05302615#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover912eaa62017-10-26 12:08:53 +05302616 if(VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05302617 type == UNLOCK_CRITICAL)
2618 {
2619 is_unlocked = device.is_unlock_critical;
2620 }
Parth Dixitddbc7352015-10-18 03:13:31 +05302621#endif
lijuang4ece1e72015-08-14 21:02:36 +08002622 if (is_unlocked == status) {
2623 snprintf(response, sizeof(response), "\tDevice already : %s", (status ? "unlocked!" : "locked!"));
2624 fastboot_info(response);
2625 fastboot_okay("");
2626 return;
2627 }
2628
2629 /* status is true, it means to unlock device */
lijuang07c5d6e2018-04-23 18:32:13 +08002630 if (status && !is_allow_unlock) {
2631 fastboot_fail("oem unlock is not allowed");
2632 return;
2633 }
lijuang21f12f52015-08-22 16:22:19 +08002634
lijuang4ece1e72015-08-14 21:02:36 +08002635#if FBCON_DISPLAY_MSG
lijuang07c5d6e2018-04-23 18:32:13 +08002636 display_unlock_menu(type, status);
2637 fastboot_okay("");
2638 return;
lijuang4ece1e72015-08-14 21:02:36 +08002639#else
lijuang07c5d6e2018-04-23 18:32:13 +08002640 if (status && type == UNLOCK) {
2641 fastboot_fail("Need wipe userdata. Do 'fastboot oem unlock-go'");
2642 return;
lijuang21f12f52015-08-22 16:22:19 +08002643 }
lijuang07c5d6e2018-04-23 18:32:13 +08002644#endif
lijuang4ece1e72015-08-14 21:02:36 +08002645
2646 set_device_unlock_value(type, status);
2647
2648 /* wipe data */
2649 struct recovery_message msg;
Kishor PK60a68212017-05-08 16:55:57 +05302650 memset(&msg, 0, sizeof(msg));
lijuang4ece1e72015-08-14 21:02:36 +08002651 snprintf(msg.recovery, sizeof(msg.recovery), "recovery\n--wipe_data");
2652 write_misc(0, &msg, sizeof(msg));
2653
2654 fastboot_okay("");
2655 reboot_device(RECOVERY_MODE);
lijuang21f12f52015-08-22 16:22:19 +08002656}
2657
lijuang511a2b52015-08-14 20:50:51 +08002658static bool critical_flash_allowed(const char * entry)
2659{
2660 uint32_t i = 0;
2661 if (entry == NULL)
2662 return false;
2663
2664 for (i = 0; i < ARRAY_SIZE(critical_flash_allowed_ptn); i++) {
2665 if(!strcmp(entry, critical_flash_allowed_ptn[i]))
2666 return true;
2667 }
2668 return false;
Matthew Qin271927e2015-03-31 22:07:07 -04002669}
2670
2671#if DEVICE_TREE
Amol Jadicb524072012-08-09 16:40:18 -07002672int copy_dtb(uint8_t *boot_image_start, unsigned int scratch_offset)
2673{
2674 uint32 dt_image_offset = 0;
Amol Jadicb524072012-08-09 16:40:18 -07002675 uint32_t n;
Monika Singh292b3e92018-03-17 22:40:23 +05302676 struct dt_table *table = NULL;
Amol Jadicb524072012-08-09 16:40:18 -07002677 struct dt_entry dt_entry;
Monika Singh292b3e92018-03-17 22:40:23 +05302678 uint32_t dt_hdr_size = 0;
Amol Jadicb524072012-08-09 16:40:18 -07002679 unsigned int compressed_size = 0;
2680 unsigned int dtb_size = 0;
2681 unsigned int out_avai_len = 0;
2682 unsigned char *out_addr = NULL;
2683 unsigned char *best_match_dt_addr = NULL;
2684 int rc;
2685
2686 struct boot_img_hdr *hdr = (struct boot_img_hdr *) (boot_image_start);
2687
Parth Dixit4097b622016-03-15 14:42:27 +05302688#ifndef OSVERSION_IN_BOOTIMAGE
2689 dt_size = hdr->dt_size;
2690#endif
2691
2692 if(dt_size != 0) {
Amol Jadicb524072012-08-09 16:40:18 -07002693 /* add kernel offset */
2694 dt_image_offset += page_size;
2695 n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
2696 dt_image_offset += n;
2697
2698 /* add ramdisk offset */
2699 n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
2700 dt_image_offset += n;
2701
2702 /* add second offset */
2703 if(hdr->second_size != 0) {
2704 n = ROUND_TO_PAGE(hdr->second_size, page_mask);
2705 dt_image_offset += n;
2706 }
2707
2708 /* offset now point to start of dt.img */
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07002709 table = (struct dt_table*)(boot_image_start + dt_image_offset);
Amol Jadicb524072012-08-09 16:40:18 -07002710
Deepa Dinamani19648b42013-09-05 17:05:55 -07002711 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Amol Jadicb524072012-08-09 16:40:18 -07002712 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
2713 return -1;
2714 }
P.V. Phani Kumar82916762016-03-09 09:20:19 +05302715
2716 /* Its Error if, dt_hdr_size (table->num_entries * dt_entry size + Dev_Tree Header)
2717 goes beyound hdr->dt_size*/
Parth Dixit4097b622016-03-15 14:42:27 +05302718 if (dt_hdr_size > ROUND_TO_PAGE(dt_size,hdr->page_size)) {
P.V. Phani Kumar82916762016-03-09 09:20:19 +05302719 dprintf(CRITICAL, "ERROR: Invalid Device Tree size \n");
2720 return -1;
2721 }
2722
Joel Kingaa335dc2013-06-03 16:11:08 -07002723 /* Find index of device tree within device tree table */
2724 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Amol Jadicb524072012-08-09 16:40:18 -07002725 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
2726 return -1;
2727 }
2728
Matthew Qin271927e2015-03-31 22:07:07 -04002729 best_match_dt_addr = (unsigned char *)boot_image_start + dt_image_offset + dt_entry.offset;
2730 if (is_gzip_package(best_match_dt_addr, dt_entry.size))
2731 {
2732 out_addr = (unsigned char *)target_get_scratch_address() + scratch_offset;
2733 out_avai_len = target_get_max_flash_size() - scratch_offset;
Matthew Qin0b15b322015-05-19 05:20:54 -04002734 dprintf(INFO, "decompressing dtb: start\n");
Matthew Qin271927e2015-03-31 22:07:07 -04002735 rc = decompress(best_match_dt_addr,
2736 dt_entry.size, out_addr, out_avai_len,
2737 &compressed_size, &dtb_size);
2738 if (rc)
2739 {
Matthew Qin0b15b322015-05-19 05:20:54 -04002740 dprintf(CRITICAL, "decompressing dtb failed!!!\n");
Matthew Qin271927e2015-03-31 22:07:07 -04002741 ASSERT(0);
2742 }
2743
Matthew Qin0b15b322015-05-19 05:20:54 -04002744 dprintf(INFO, "decompressing dtb: done\n");
Matthew Qin271927e2015-03-31 22:07:07 -04002745 best_match_dt_addr = out_addr;
2746 } else {
2747 dtb_size = dt_entry.size;
2748 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002749 /* Validate and Read device device tree in the "tags_add */
Mayank Grovere559cec2017-10-17 15:12:03 +05302750 if (check_aboot_addr_range_overlap(hdr->tags_addr, dtb_size) ||
2751 check_ddr_addr_range_bound(hdr->tags_addr, dtb_size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002752 {
Mayank Grovere559cec2017-10-17 15:12:03 +05302753 dprintf(CRITICAL, "Device tree addresses are not valid.\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002754 return -1;
2755 }
2756
Amol Jadicb524072012-08-09 16:40:18 -07002757 /* Read device device tree in the "tags_add */
Matthew Qin271927e2015-03-31 22:07:07 -04002758 memmove((void*) hdr->tags_addr, (void *)best_match_dt_addr, dtb_size);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002759 } else
2760 return -1;
Amol Jadicb524072012-08-09 16:40:18 -07002761
2762 /* Everything looks fine. Return success. */
2763 return 0;
2764}
2765#endif
2766
Brian Swetland9c4c0752009-01-25 16:23:50 -08002767void cmd_boot(const char *arg, void *data, unsigned sz)
2768{
2769 unsigned kernel_actual;
2770 unsigned ramdisk_actual;
Kishor PKd8ddcad2017-07-27 13:53:57 +05302771 unsigned second_actual;
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002772 uint32_t image_actual;
2773 uint32_t dt_actual = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08002774 struct boot_img_hdr *hdr = NULL;
2775 struct kernel64_hdr *kptr = NULL;
Brian Swetland9c4c0752009-01-25 16:23:50 -08002776 char *ptr = ((char*) data);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002777 int ret = 0;
2778 uint8_t dtb_copied = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08002779 unsigned int out_len = 0;
2780 unsigned int out_avai_len = 0;
2781 unsigned char *out_addr = NULL;
2782 uint32_t dtb_offset = 0;
2783 unsigned char *kernel_start_addr = NULL;
2784 unsigned int kernel_size = 0;
Matthew Qin271927e2015-03-31 22:07:07 -04002785 unsigned int scratch_offset = 0;
Monika Singh292b3e92018-03-17 22:40:23 +05302786#if !VERIFIED_BOOT_2
2787 uint32_t sig_actual = 0;
2788 uint32_t sig_size = 0;
2789#ifdef MDTP_SUPPORT
2790 static bool is_mdtp_activated = 0;
2791#endif /* MDTP_SUPPORT */
2792#endif
Matthew Qinbb7923d2015-02-09 10:56:09 +08002793
lijuang2008ff22016-03-07 17:56:27 +08002794#if FBCON_DISPLAY_MSG
2795 /* Exit keys' detection thread firstly */
2796 exit_menu_keys_detection();
2797#endif
2798
Monika Singh292b3e92018-03-17 22:40:23 +05302799#if VERIFIED_BOOT || VERIFIED_BOOT_2
Channagoud Kadabi6d5375e2015-06-23 17:15:42 -07002800 if(target_build_variant_user() && !device.is_unlocked)
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002801 {
2802 fastboot_fail("unlock device to use this command");
lijuang2008ff22016-03-07 17:56:27 +08002803 goto boot_failed;
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002804 }
2805#endif
2806
Brian Swetland9c4c0752009-01-25 16:23:50 -08002807 if (sz < sizeof(hdr)) {
2808 fastboot_fail("invalid bootimage header");
lijuang2008ff22016-03-07 17:56:27 +08002809 goto boot_failed;
Brian Swetland9c4c0752009-01-25 16:23:50 -08002810 }
2811
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002812 hdr = (struct boot_img_hdr *)data;
Brian Swetland9c4c0752009-01-25 16:23:50 -08002813
2814 /* ensure commandline is terminated */
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002815 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -08002816
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002817 if(target_is_emmc_boot() && hdr->page_size) {
2818 page_size = hdr->page_size;
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07002819 page_mask = page_size - 1;
2820 }
2821
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002822 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
2823 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Kishor PKd8ddcad2017-07-27 13:53:57 +05302824 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002825#if DEVICE_TREE
Parth Dixit4097b622016-03-15 14:42:27 +05302826#ifndef OSVERSION_IN_BOOTIMAGE
2827 dt_size = hdr->dt_size;
2828#endif
2829 dt_actual = ROUND_TO_PAGE(dt_size, page_mask);
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002830#endif
2831
2832 image_actual = ADD_OF(page_size, kernel_actual);
2833 image_actual = ADD_OF(image_actual, ramdisk_actual);
Kishor PKd8ddcad2017-07-27 13:53:57 +05302834 image_actual = ADD_OF(image_actual, second_actual);
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002835 image_actual = ADD_OF(image_actual, dt_actual);
2836
Kishor PK5134b332017-05-09 17:50:08 +05302837 /* Checking to prevent oob access in read_der_message_length */
2838 if (image_actual > sz) {
2839 fastboot_fail("bootimage header fields are invalid");
2840 goto boot_failed;
2841 }
Monika Singh292b3e92018-03-17 22:40:23 +05302842#if VERIFIED_BOOT_2
2843 memset(&info, 0, sizeof(bootinfo));
2844 info.images[0].image_buffer = data;
2845 info.images[0].imgsize = image_actual;
2846 info.images[0].name = "boot";
2847 info.num_loaded_images = 1;
2848 info.multi_slot_boot = partition_multislot_is_supported();
2849 if (load_image_and_auth(&info))
2850 goto boot_failed;
2851 vbcmdline = info.vbcmdline;
2852#else
Kishor PK5134b332017-05-09 17:50:08 +05302853 sig_size = sz - image_actual;
2854
P.V. Phani Kumareecfd822016-03-09 20:09:03 +05302855 if (target_use_signed_kernel() && (!device.is_unlocked)) {
Monika Singh292b3e92018-03-17 22:40:23 +05302856 unsigned chk;
P.V. Phani Kumareecfd822016-03-09 20:09:03 +05302857 /* Calculate the signature length from boot image */
2858 sig_actual = read_der_message_length(
Kishor PK5134b332017-05-09 17:50:08 +05302859 (unsigned char*)(data + image_actual), sig_size);
Monika Singh292b3e92018-03-17 22:40:23 +05302860 chk = ADD_OF(image_actual, sig_actual);
Channagoud Kadabi3d839012014-06-26 14:26:39 -07002861
Monika Singh292b3e92018-03-17 22:40:23 +05302862 if (chk > sz) {
Kishor PK5134b332017-05-09 17:50:08 +05302863 fastboot_fail("bootimage header fields are invalid");
2864 goto boot_failed;
2865 }
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002866 }
2867
Sridhar Parasuram361e4cd2015-09-24 11:19:19 -07002868 // Initialize boot state before trying to verify boot.img
2869#if VERIFIED_BOOT
Channagoud Kadabi699466e2015-11-03 12:37:42 -08002870 boot_verifier_init();
Mayank Groverb337e932017-01-18 20:00:40 +05302871#endif
Channagoud Kadabibf6ce7d2015-09-17 13:25:35 -07002872 /* Handle overflow if the input image size is greater than
2873 * boot image buffer can hold
2874 */
Monika Singh292b3e92018-03-17 22:40:23 +05302875 if ((target_get_max_flash_size() - page_size) < image_actual)
Channagoud Kadabibf6ce7d2015-09-17 13:25:35 -07002876 {
2877 fastboot_fail("booimage: size is greater than boot image buffer can hold");
lijuang2008ff22016-03-07 17:56:27 +08002878 goto boot_failed;
Channagoud Kadabibf6ce7d2015-09-17 13:25:35 -07002879 }
Sridhar Parasuram361e4cd2015-09-24 11:19:19 -07002880
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002881 /* Verify the boot image
2882 * device & page_size are initialized in aboot_init
2883 */
Sridhar Parasuram7ae9fbc2015-09-17 09:56:30 -07002884 if (target_use_signed_kernel() && (!device.is_unlocked)) {
Channagoud Kadabi3d839012014-06-26 14:26:39 -07002885 /* Pass size excluding signature size, otherwise we would try to
2886 * access signature beyond its length
2887 */
Monika Singh292b3e92018-03-17 22:40:23 +05302888 verify_signed_bootimg((uint32_t)data, image_actual);
Sridhar Parasuram7ae9fbc2015-09-17 09:56:30 -07002889 }
Amit Blay4aa292f2015-04-28 21:55:59 +03002890#ifdef MDTP_SUPPORT
2891 else
2892 {
Shay Nachmani062aa3f2015-05-17 17:28:44 +03002893 /* fastboot boot is not allowed when MDTP is activated */
Amit Blay4aa292f2015-04-28 21:55:59 +03002894 mdtp_ext_partition_verification_t ext_partition;
Amit Blay8a510302015-08-17 09:20:01 +03002895
2896 if (!is_mdtp_activated) {
2897 ext_partition.partition = MDTP_PARTITION_NONE;
2898 mdtp_fwlock_verify_lock(&ext_partition);
2899 }
Amit Blay4aa292f2015-04-28 21:55:59 +03002900 }
Shay Nachmani062aa3f2015-05-17 17:28:44 +03002901
Amir Kotzer7c768c02016-04-13 09:08:05 +03002902 /* If mdtp state cannot be validate, block fastboot boot*/
2903 if(mdtp_activated(&is_mdtp_activated)){
2904 dprintf(CRITICAL, "mdtp_activated cannot validate state.\n");
2905 dprintf(CRITICAL, "Can not proceed with fastboot boot command.\n");
2906 goto boot_failed;
2907 }
Shay Nachmani062aa3f2015-05-17 17:28:44 +03002908 if(is_mdtp_activated){
2909 dprintf(CRITICAL, "fastboot boot command is not available.\n");
lijuang2008ff22016-03-07 17:56:27 +08002910 goto boot_failed;
Shay Nachmani062aa3f2015-05-17 17:28:44 +03002911 }
Amit Blay4aa292f2015-04-28 21:55:59 +03002912#endif /* MDTP_SUPPORT */
Monika Singh292b3e92018-03-17 22:40:23 +05302913#endif /* VERIFIED_BOOT_2 else */
Amit Blay4aa292f2015-04-28 21:55:59 +03002914
Sridhar Parasuram361e4cd2015-09-24 11:19:19 -07002915#if VERIFIED_BOOT
Monika Singh292b3e92018-03-17 22:40:23 +05302916 if (VB_M == target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +05302917 {
2918 /* set boot and system versions. */
2919 set_os_version((unsigned char *)data);
2920 // send root of trust
2921 if(!send_rot_command((uint32_t)device.is_unlocked))
2922 ASSERT(0);
2923 }
Parth Dixitbc9b6492015-10-18 00:41:38 +05302924#endif
Channagoud Kadabia22144f2013-03-20 11:49:01 -07002925 /*
Matthew Qinbb7923d2015-02-09 10:56:09 +08002926 * Check if the kernel image is a gzip package. If yes, need to decompress it.
2927 * If not, continue booting.
2928 */
2929 if (is_gzip_package((unsigned char *)(data + page_size), hdr->kernel_size))
2930 {
2931 out_addr = (unsigned char *)target_get_scratch_address();
2932 out_addr = (unsigned char *)(out_addr + image_actual + page_size);
2933 out_avai_len = target_get_max_flash_size() - image_actual - page_size;
Matthew Qin0b15b322015-05-19 05:20:54 -04002934 dprintf(INFO, "decompressing kernel image: start\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08002935 ret = decompress((unsigned char *)(ptr + page_size),
2936 hdr->kernel_size, out_addr, out_avai_len,
2937 &dtb_offset, &out_len);
2938 if (ret)
2939 {
Matthew Qin0b15b322015-05-19 05:20:54 -04002940 dprintf(CRITICAL, "decompressing image failed!!!\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08002941 ASSERT(0);
2942 }
2943
Matthew Qin0b15b322015-05-19 05:20:54 -04002944 dprintf(INFO, "decompressing kernel image: done\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08002945 kptr = (struct kernel64_hdr *)out_addr;
2946 kernel_start_addr = out_addr;
2947 kernel_size = out_len;
2948 } else {
2949 kptr = (struct kernel64_hdr*)((char *)data + page_size);
2950 kernel_start_addr = (unsigned char *)((char *)data + page_size);
2951 kernel_size = hdr->kernel_size;
2952 }
2953
2954 /*
Channagoud Kadabia22144f2013-03-20 11:49:01 -07002955 * Update the kernel/ramdisk/tags address if the boot image header
2956 * has default values, these default values come from mkbootimg when
2957 * the boot image is flashed using fastboot flash:raw
2958 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08002959 update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr));
Dima Zavin3cadfff2013-03-21 14:30:48 -07002960
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002961 /* Get virtual addresses since the hdr saves physical addresses. */
2962 hdr->kernel_addr = VA(hdr->kernel_addr);
2963 hdr->ramdisk_addr = VA(hdr->ramdisk_addr);
2964 hdr->tags_addr = VA(hdr->tags_addr);
Brian Swetland9c4c0752009-01-25 16:23:50 -08002965
Matthew Qinbb7923d2015-02-09 10:56:09 +08002966 kernel_size = ROUND_TO_PAGE(kernel_size, page_mask);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002967 /* Check if the addresses in the header are valid. */
Matthew Qinbb7923d2015-02-09 10:56:09 +08002968 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_size) ||
Mayank Grovere559cec2017-10-17 15:12:03 +05302969 check_ddr_addr_range_bound(hdr->kernel_addr, kernel_size) ||
2970 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual) ||
2971 check_ddr_addr_range_bound(hdr->ramdisk_addr, ramdisk_actual))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002972 {
Mayank Grovere559cec2017-10-17 15:12:03 +05302973 dprintf(CRITICAL, "kernel/ramdisk addresses are not valid.\n");
lijuang2008ff22016-03-07 17:56:27 +08002974 goto boot_failed;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002975 }
2976
Amol Jadicb524072012-08-09 16:40:18 -07002977#if DEVICE_TREE
Matthew Qin271927e2015-03-31 22:07:07 -04002978 scratch_offset = image_actual + page_size + out_len;
Amol Jadicb524072012-08-09 16:40:18 -07002979 /* find correct dtb and copy it to right location */
Matthew Qin271927e2015-03-31 22:07:07 -04002980 ret = copy_dtb(data, scratch_offset);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002981
2982 dtb_copied = !ret ? 1 : 0;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002983#else
Mayank Grovere559cec2017-10-17 15:12:03 +05302984 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE) ||
2985 check_ddr_addr_range_bound(hdr->tags_addr, MAX_TAGS_SIZE))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002986 {
Mayank Grovere559cec2017-10-17 15:12:03 +05302987 dprintf(CRITICAL, "Tags addresses are not valid.\n");
lijuang2008ff22016-03-07 17:56:27 +08002988 goto boot_failed;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002989 }
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002990#endif
2991
2992 /* Load ramdisk & kernel */
2993 memmove((void*) hdr->ramdisk_addr, ptr + page_size + kernel_actual, hdr->ramdisk_size);
Matthew Qinbb7923d2015-02-09 10:56:09 +08002994 memmove((void*) hdr->kernel_addr, (char*) (kernel_start_addr), kernel_size);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002995
2996#if DEVICE_TREE
Mayank Grovere559cec2017-10-17 15:12:03 +05302997 if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual) ||
2998 check_ddr_addr_range_bound(hdr->tags_addr, kernel_actual))
Matthew Qinbb7923d2015-02-09 10:56:09 +08002999 {
Mayank Grovere559cec2017-10-17 15:12:03 +05303000 dprintf(CRITICAL, "Tags addresses are not valid.\n");
lijuang2008ff22016-03-07 17:56:27 +08003001 goto boot_failed;
Matthew Qinbb7923d2015-02-09 10:56:09 +08003002 }
3003
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07003004 /*
3005 * If dtb is not found look for appended DTB in the kernel.
3006 * If appended dev tree is found, update the atags with
3007 * memory address to the DTB appended location on RAM.
3008 * Else update with the atags address in the kernel header
3009 */
3010 if (!dtb_copied) {
3011 void *dtb;
Matthew Qinbb7923d2015-02-09 10:56:09 +08003012 dtb = dev_tree_appended((void*)(ptr + page_size),
3013 hdr->kernel_size, dtb_offset,
Dima Zavine63e5572013-05-03 12:23:06 -07003014 (void *)hdr->tags_addr);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07003015 if (!dtb) {
3016 fastboot_fail("dtb not found");
lijuang2008ff22016-03-07 17:56:27 +08003017 goto boot_failed;
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07003018 }
Amol Jadicb524072012-08-09 16:40:18 -07003019 }
3020#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08003021
3022 fastboot_okay("");
Amol Jadi7c4316c2013-10-07 14:19:26 -07003023 fastboot_stop();
Brian Swetland9c4c0752009-01-25 16:23:50 -08003024
Dima Zavin77e41f32013-03-06 16:10:43 -08003025 boot_linux((void*) hdr->kernel_addr, (void*) hdr->tags_addr,
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07003026 (const char*) hdr->cmdline, board_machtype(),
3027 (void*) hdr->ramdisk_addr, hdr->ramdisk_size);
lijuang2008ff22016-03-07 17:56:27 +08003028
3029 /* fastboot already stop, it's no need to show fastboot menu */
3030 return;
3031boot_failed:
3032#if FBCON_DISPLAY_MSG
3033 /* revert to fastboot menu if boot failed */
3034 display_fastboot_menu();
3035#endif
3036 return;
Brian Swetland9c4c0752009-01-25 16:23:50 -08003037}
3038
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003039void cmd_erase_nand(const char *arg, void *data, unsigned sz)
Dima Zavin214cc642009-01-26 11:16:21 -08003040{
3041 struct ptentry *ptn;
3042 struct ptable *ptable;
3043
3044 ptable = flash_get_ptable();
3045 if (ptable == NULL) {
3046 fastboot_fail("partition table doesn't exist");
3047 return;
3048 }
3049
3050 ptn = ptable_find(ptable, arg);
3051 if (ptn == NULL) {
3052 fastboot_fail("unknown partition name");
3053 return;
3054 }
3055
Monika Singh292b3e92018-03-17 22:40:23 +05303056 if (!strncmp(arg, "avb_custom_key", strlen("avb_custom_key"))) {
3057 dprintf(INFO, "erasing avb_custom_key\n");
3058 if (erase_userkey()) {
3059 fastboot_fail("Erasing avb_custom_key failed");
3060 } else {
3061 fastboot_okay("");
3062 }
3063 return;
3064 }
3065
Dima Zavin214cc642009-01-26 11:16:21 -08003066 if (flash_erase(ptn)) {
3067 fastboot_fail("failed to erase partition");
3068 return;
3069 }
3070 fastboot_okay("");
3071}
3072
Bikas Gurungd48bd242010-09-04 19:54:32 -07003073
3074void cmd_erase_mmc(const char *arg, void *data, unsigned sz)
3075{
3076 unsigned long long ptn = 0;
Oliver Wangcee448d2013-10-22 18:40:13 +08003077 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07003078 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003079 uint8_t lun = 0;
Vijay Kumar Pendoti0b21f462016-05-02 17:09:18 +05303080 char *footer = NULL;
Bikas Gurungd48bd242010-09-04 19:54:32 -07003081
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003082#if VERIFIED_BOOT
3083 if(!strcmp(arg, KEYSTORE_PTN_NAME))
3084 {
3085 if(!device.is_unlocked)
3086 {
3087 fastboot_fail("unlock device to erase keystore");
3088 return;
3089 }
3090 }
3091#endif
3092
Kinson Chikf1a43512011-07-14 11:28:39 -07003093 index = partition_get_index(arg);
3094 ptn = partition_get_offset(index);
Oliver Wangcee448d2013-10-22 18:40:13 +08003095 size = partition_get_size(index);
Neeti Desaica8c9602011-10-06 11:40:00 -07003096
Monika Singhc4778b72018-05-16 11:16:42 +05303097 if (!strncmp(arg, "avb_custom_key", strlen("avb_custom_key"))) {
3098 dprintf(INFO, "erasing avb_custom_key\n");
3099 if (erase_userkey()) {
3100 fastboot_fail("Erasing avb_custom_key failed");
3101 } else {
3102 fastboot_okay("");
3103 }
3104 return;
3105 }
3106
Kinson Chikf1a43512011-07-14 11:28:39 -07003107 if(ptn == 0) {
Neeti Desaica8c9602011-10-06 11:40:00 -07003108 fastboot_fail("Partition table doesn't exist\n");
Bikas Gurungd48bd242010-09-04 19:54:32 -07003109 return;
3110 }
Kun Liang2f1601a2013-08-12 16:29:54 +08003111
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003112 lun = partition_get_lun(index);
3113 mmc_set_lun(lun);
3114
Sridhar Parasuramd7957122015-02-27 11:33:40 -08003115 if (platform_boot_dev_isemmc())
3116 {
3117 if (mmc_erase_card(ptn, size)) {
3118 fastboot_fail("failed to erase partition\n");
3119 return;
3120 }
3121 } else {
3122 BUF_DMA_ALIGN(out, DEFAULT_ERASE_SIZE);
3123 size = partition_get_size(index);
3124 if (size > DEFAULT_ERASE_SIZE)
3125 size = DEFAULT_ERASE_SIZE;
Kun Liang2f1601a2013-08-12 16:29:54 +08003126
Sridhar Parasuramd7957122015-02-27 11:33:40 -08003127 /* Simple inefficient version of erase. Just writing
3128 0 in first several blocks */
3129 if (mmc_write(ptn , size, (unsigned int *)out)) {
3130 fastboot_fail("failed to erase partition");
3131 return;
3132 }
Vijay Kumar Pendoti0b21f462016-05-02 17:09:18 +05303133 /*Erase FDE metadata at the userdata footer*/
3134 if(!(strncmp(arg, "userdata", 8)))
3135 {
3136 footer = memalign(CACHE_LINE, FOOTER_SIZE);
3137 memset((void *)footer, 0, FOOTER_SIZE);
3138
3139 size = partition_get_size(index);
3140
3141 if (mmc_write((ptn + size) - FOOTER_SIZE , FOOTER_SIZE, (unsigned int *)footer)) {
3142 fastboot_fail("failed to erase userdata footer");
3143 free(footer);
3144 return;
3145 }
3146 free(footer);
3147 }
Bikas Gurungd48bd242010-09-04 19:54:32 -07003148 }
Monika Singh292b3e92018-03-17 22:40:23 +05303149#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover912eaa62017-10-26 12:08:53 +05303150 if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05303151 !(strncmp(arg, "userdata", 8)) &&
3152 send_delete_keys_to_tz())
Sridhar Parasuram32b30662015-07-10 13:33:22 -07003153 ASSERT(0);
3154#endif
Bikas Gurungd48bd242010-09-04 19:54:32 -07003155 fastboot_okay("");
3156}
3157
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003158void cmd_erase(const char *arg, void *data, unsigned sz)
3159{
Monika Singh292b3e92018-03-17 22:40:23 +05303160#if VERIFIED_BOOT || VERIFIED_BOOT_2
Channagoud Kadabi35297672015-06-13 11:09:49 -07003161 if (target_build_variant_user())
Channagoud Kadabice3cf422015-04-17 16:02:30 -07003162 {
Sridhar Parasuramc32d07d2015-07-12 10:57:48 -07003163 if(!device.is_unlocked)
Channagoud Kadabice3cf422015-04-17 16:02:30 -07003164 {
Channagoud Kadabi35297672015-06-13 11:09:49 -07003165 fastboot_fail("device is locked. Cannot erase");
Channagoud Kadabice3cf422015-04-17 16:02:30 -07003166 return;
3167 }
3168 }
3169#endif
3170
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003171 if(target_is_emmc_boot())
3172 cmd_erase_mmc(arg, data, sz);
3173 else
3174 cmd_erase_nand(arg, data, sz);
3175}
Bikas Gurungd48bd242010-09-04 19:54:32 -07003176
Mayank Grover11ff9692018-01-11 11:54:49 +05303177/* Get the size from partiton name */
3178static void get_partition_size(const char *arg, char *response)
3179{
3180 uint64_t ptn = 0;
3181 uint64_t size;
3182 int index = INVALID_PTN;
3183
3184 index = partition_get_index(arg);
3185
3186 if (index == INVALID_PTN)
3187 {
3188 dprintf(CRITICAL, "Invalid partition index\n");
3189 return;
3190 }
3191
3192 ptn = partition_get_offset(index);
3193
3194 if(!ptn)
3195 {
3196 dprintf(CRITICAL, "Invalid partition name %s\n", arg);
3197 return;
3198 }
3199
3200 size = partition_get_size(index);
3201
3202 snprintf(response, MAX_RSP_SIZE, "\t 0x%llx", size);
3203 return;
3204}
3205
Mayank Grover52cd10a2018-03-15 12:57:54 +05303206/* Function to check partition type of a partition*/
3207static fs_signature_type
3208check_partition_fs_signature(const char *arg)
3209{
3210 fs_signature_type ret = NO_FS;
3211 int index;
3212 unsigned long long ptn;
lijuangc2676752018-05-15 13:52:36 +08003213 char *sb_buffer = memalign(CACHE_LINE, mmc_blocksize);
Mayank Grover52cd10a2018-03-15 12:57:54 +05303214 if (!sb_buffer)
3215 {
3216 dprintf(CRITICAL, "ERROR: Failed to allocate buffer for superblock\n");
3217 goto out;
3218 }
3219
3220 /* Read super block */
3221 if ((index = partition_get_index(arg)) < 0)
3222 {
3223 dprintf(CRITICAL, "ERROR: %s() doesn't exsit\n", arg);
3224 goto out;
3225 }
3226 ptn = partition_get_offset(index);
3227 mmc_set_lun(partition_get_lun(index));
3228 if(mmc_read(ptn + FS_SUPERBLOCK_OFFSET,
3229 (void *)sb_buffer, mmc_blocksize))
3230 {
3231 dprintf(CRITICAL, "ERROR: Failed to read Superblock\n");
3232 goto out;
3233 }
3234
3235 if (*((uint16 *)(&sb_buffer[EXT_MAGIC_OFFSET_SB]))
3236 == (uint16)EXT_MAGIC)
3237 {
3238 dprintf(SPEW, "%s() Found EXT FS\n", arg);
3239 ret = EXT_FS_SIGNATURE;
3240 }
3241 else if (*((uint32 *)(&sb_buffer[F2FS_MAGIC_OFFSET_SB]))
3242 == F2FS_MAGIC)
3243 {
3244 dprintf(SPEW, "%s() Found F2FS FS\n", arg);
3245 ret = EXT_F2FS_SIGNATURE;
3246 }
3247 else
3248 {
3249 dprintf(SPEW, "%s() Reverting to default 0x%x\n",
3250 arg, *((uint16 *)(&sb_buffer[EXT_MAGIC_OFFSET_SB])));
3251 ret = NO_FS;
3252 }
3253
3254out:
3255 if(sb_buffer)
3256 free(sb_buffer);
3257 return ret;
3258}
3259
Mayank Groverd38fe012018-03-13 15:33:16 +05303260/* Function to get partition type */
3261static void get_partition_type(const char *arg, char *response)
3262{
3263 uint n = 0;
Mayank Grover52cd10a2018-03-15 12:57:54 +05303264 fs_signature_type fs_signature;
Mayank Groverd38fe012018-03-13 15:33:16 +05303265
3266 if (arg == NULL ||
3267 response == NULL)
3268 {
3269 dprintf(CRITICAL, "Invalid input parameter\n");
3270 return;
3271 }
3272
3273 /* By default copy raw to response */
Mayank Grover0e559042018-04-11 17:49:30 +05303274 strlcpy(response, RAW_STR, MAX_RSP_SIZE);
Mayank Groverd38fe012018-03-13 15:33:16 +05303275
3276 /* Mark partiton type for known paritions only */
3277 for (n=0; n < ARRAY_SIZE(part_type_known); n++)
3278 {
3279 if (!strncmp(part_type_known[n].part_name, arg, strlen(arg)))
3280 {
Mayank Grover52cd10a2018-03-15 12:57:54 +05303281 /* Check partition for FS signature */
3282 fs_signature = check_partition_fs_signature(arg);
3283 switch (fs_signature)
3284 {
3285 case EXT_FS_SIGNATURE:
Mayank Grover0e559042018-04-11 17:49:30 +05303286 strlcpy(response, EXT_STR, MAX_RSP_SIZE);
Mayank Grover52cd10a2018-03-15 12:57:54 +05303287 break;
3288 case EXT_F2FS_SIGNATURE:
Mayank Grover0e559042018-04-11 17:49:30 +05303289 strlcpy(response, F2FS_STR, MAX_RSP_SIZE);
Mayank Grover52cd10a2018-03-15 12:57:54 +05303290 break;
3291 case NO_FS:
Mayank Grover0e559042018-04-11 17:49:30 +05303292 strlcpy(response, part_type_known[n].type_response, MAX_RSP_SIZE);
Mayank Grover52cd10a2018-03-15 12:57:54 +05303293 }
Mayank Groverd38fe012018-03-13 15:33:16 +05303294 }
3295 }
3296 return;
3297}
3298
Mayank Grover11ff9692018-01-11 11:54:49 +05303299/*
3300 * Publish the partition type & size info
3301 * fastboot getvar will publish the required information.
3302 * fastboot getvar partition_size:<partition_name>: partition size in hex
3303 * fastboot getvar partition_type:<partition_name>: partition type (ext/fat)
3304 */
3305static void publish_getvar_partition_info(struct getvar_partition_info *info, uint8_t num_parts)
3306{
Mayank Groverd38fe012018-03-13 15:33:16 +05303307 uint8_t i;
Mayank Grover11ff9692018-01-11 11:54:49 +05303308 static bool published = false;
3309 struct partition_entry *ptn_entry =
3310 partition_get_partition_entries();
3311 memset(info, 0, sizeof(struct getvar_partition_info)* num_parts);
3312
3313 for (i = 0; i < num_parts; i++) {
3314 strlcat(info[i].part_name, (const char *)ptn_entry[i].name, MAX_RSP_SIZE);
3315 strlcat(info[i].getvar_size, "partition-size:", MAX_GET_VAR_NAME_SIZE);
3316 strlcat(info[i].getvar_type, "partition-type:", MAX_GET_VAR_NAME_SIZE);
3317
Mayank Groverd38fe012018-03-13 15:33:16 +05303318 get_partition_type(info[i].part_name, info[i].type_response);
Mayank Grover11ff9692018-01-11 11:54:49 +05303319 get_partition_size(info[i].part_name, info[i].size_response);
Mayank Grover11ff9692018-01-11 11:54:49 +05303320 if (strlcat(info[i].getvar_size, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
3321 {
3322 dprintf(CRITICAL, "partition size name truncated\n");
3323 return;
3324 }
3325 if (strlcat(info[i].getvar_type, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
3326 {
3327 dprintf(CRITICAL, "partition type name truncated\n");
3328 return;
3329 }
3330
3331 if (!published)
3332 {
3333 /* publish partition size & type info */
3334 fastboot_publish((const char *) info[i].getvar_size, (const char *) info[i].size_response);
3335 fastboot_publish((const char *) info[i].getvar_type, (const char *) info[i].type_response);
3336 }
3337 }
3338 if (!published)
3339 published = true;
3340}
3341
3342
Ajay Dudani5c761132011-04-07 20:19:04 -07003343void cmd_flash_mmc_img(const char *arg, void *data, unsigned sz)
Shashank Mittal23b8f422010-04-16 19:27:21 -07003344{
3345 unsigned long long ptn = 0;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07003346 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07003347 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003348 char *token = NULL;
3349 char *pname = NULL;
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07003350 char *sp;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003351 uint8_t lun = 0;
3352 bool lun_set = false;
Mayank Grover351a75e2017-05-30 20:06:08 +05303353 int current_active_slot = INVALID;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07003354
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07003355 token = strtok_r((char *)arg, ":", &sp);
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003356 pname = token;
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07003357 token = strtok_r(NULL, ":", &sp);
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003358 if(token)
3359 {
3360 lun = atoi(token);
3361 mmc_set_lun(lun);
3362 lun_set = true;
3363 }
3364
Mao Jinlong226f33a2014-07-04 17:24:10 +08003365 if (pname)
Greg Grisco6e754772011-06-23 12:19:39 -07003366 {
Channagoud Kadabiad259832015-05-29 11:14:17 -07003367 if (!strncmp(pname, "frp-unlock", strlen("frp-unlock")))
3368 {
3369 if (!aboot_frp_unlock(pname, data, sz))
3370 {
3371 fastboot_info("FRP unlock successful");
3372 fastboot_okay("");
3373 }
3374 else
3375 fastboot_fail("Secret key is invalid, please update the bootloader with secret key");
3376
3377 return;
3378 }
3379
Mao Jinlong226f33a2014-07-04 17:24:10 +08003380 if (!strcmp(pname, "partition"))
3381 {
3382 dprintf(INFO, "Attempt to write partition image.\n");
3383 if (write_partition(sz, (unsigned char *) data)) {
3384 fastboot_fail("failed to write partition");
Greg Grisco6e754772011-06-23 12:19:39 -07003385 return;
3386 }
Mayank Grover11ff9692018-01-11 11:54:49 +05303387 /* Re-publish partition table */
3388 publish_getvar_partition_info(part_info, partition_get_partition_count());
Mayank Grover351a75e2017-05-30 20:06:08 +05303389
3390 /* Rescan partition table to ensure we have multislot support*/
3391 if (partition_scan_for_multislot())
3392 {
3393 current_active_slot = partition_find_active_slot();
3394 dprintf(INFO, "Multislot supported: Slot %s active",
3395 (SUFFIX_SLOT(current_active_slot)));
3396 }
3397 partition_mark_active_slot(current_active_slot);
Greg Grisco6e754772011-06-23 12:19:39 -07003398 }
Mao Jinlong226f33a2014-07-04 17:24:10 +08003399 else
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003400 {
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003401#if VERIFIED_BOOT
3402 if(!strcmp(pname, KEYSTORE_PTN_NAME))
3403 {
3404 if(!device.is_unlocked)
3405 {
3406 fastboot_fail("unlock device to flash keystore");
3407 return;
3408 }
Gaurav Nebhwanic7313cc2015-12-15 22:25:04 +05303409 if(!boot_verify_validate_keystore((unsigned char *)data,sz))
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003410 {
3411 fastboot_fail("image is not a keystore file");
3412 return;
3413 }
3414 }
3415#endif
Mao Jinlong226f33a2014-07-04 17:24:10 +08003416 index = partition_get_index(pname);
3417 ptn = partition_get_offset(index);
3418 if(ptn == 0) {
3419 fastboot_fail("partition table doesn't exist");
3420 return;
3421 }
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003422
Mayank Grover351a75e2017-05-30 20:06:08 +05303423 if (!strncmp(pname, "boot", strlen("boot"))
3424 || !strcmp(pname, "recovery"))
3425 {
Mao Jinlong226f33a2014-07-04 17:24:10 +08003426 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
3427 fastboot_fail("image is not a boot image");
3428 return;
3429 }
Mayank Grover351a75e2017-05-30 20:06:08 +05303430
3431 /* Reset multislot_partition attributes in case of flashing boot */
3432 if (partition_multislot_is_supported())
3433 {
3434 partition_reset_attributes(index);
3435 }
Mao Jinlong226f33a2014-07-04 17:24:10 +08003436 }
3437
3438 if(!lun_set)
3439 {
3440 lun = partition_get_lun(index);
3441 mmc_set_lun(lun);
3442 }
3443
3444 size = partition_get_size(index);
Gaurav Nebhwani4d2389c2016-03-17 21:10:05 +05303445 if (ROUND_TO_PAGE(sz, mmc_blocksize_mask) > size) {
Mao Jinlong226f33a2014-07-04 17:24:10 +08003446 fastboot_fail("size too large");
3447 return;
3448 }
3449 else if (mmc_write(ptn , sz, (unsigned int *)data)) {
3450 fastboot_fail("flash write failure");
3451 return;
3452 }
Greg Grisco6e754772011-06-23 12:19:39 -07003453 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07003454 }
3455 fastboot_okay("");
3456 return;
3457}
3458
Ajay Dudanide984792015-03-02 09:57:41 -08003459void cmd_flash_meta_img(const char *arg, void *data, unsigned sz)
3460{
3461 int i, images;
3462 meta_header_t *meta_header;
3463 img_header_entry_t *img_header_entry;
Parth Dixit3e2d3032016-03-04 17:11:52 +05303464 /*End of the image address*/
3465 uintptr_t data_end;
3466
3467 if( (UINT_MAX - sz) > (uintptr_t)data )
3468 data_end = (uintptr_t)data + sz;
3469 else
3470 {
3471 fastboot_fail("Cannot flash: image header corrupt");
3472 return;
3473 }
3474
3475 if( data_end < ((uintptr_t)data + sizeof(meta_header_t)))
3476 {
3477 fastboot_fail("Cannot flash: image header corrupt");
3478 return;
3479 }
Ajay Dudanide984792015-03-02 09:57:41 -08003480
lijuang8ee21882016-04-19 16:57:11 +08003481 /* If device is locked:
3482 * Forbid to flash image to avoid the device to bypass the image
3483 * which with "any" name other than bootloader. Because it maybe
3484 * a meta package of all partitions.
3485 */
Monika Singh292b3e92018-03-17 22:40:23 +05303486#if VERIFIED_BOOT || VERIFIED_BOOT_2
lijuang8ee21882016-04-19 16:57:11 +08003487 if (target_build_variant_user()) {
3488 if (!device.is_unlocked) {
3489 fastboot_fail("Device is locked, meta image flashing is not allowed");
3490 return;
3491 }
Mayank Grover889be1b2017-09-12 20:12:23 +05303492
Mayank Grover912eaa62017-10-26 12:08:53 +05303493 if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05303494 !device.is_unlock_critical)
3495 {
lijuang8ee21882016-04-19 16:57:11 +08003496 fastboot_fail("Device is critical locked, Meta image flashing is not allowed");
3497 return;
3498 }
lijuang8ee21882016-04-19 16:57:11 +08003499 }
3500#endif
3501
Ajay Dudanide984792015-03-02 09:57:41 -08003502 meta_header = (meta_header_t*) data;
Parth Dixit3e2d3032016-03-04 17:11:52 +05303503 if( data_end < ((uintptr_t)data + meta_header->img_hdr_sz))
3504 {
3505 fastboot_fail("Cannot flash: image header corrupt");
3506 return;
3507 }
Ajay Dudanide984792015-03-02 09:57:41 -08003508 img_header_entry = (img_header_entry_t*) (data+sizeof(meta_header_t));
3509
3510 images = meta_header->img_hdr_sz / sizeof(img_header_entry_t);
3511
3512 for (i=0; i<images; i++) {
3513
3514 if((img_header_entry[i].ptn_name == NULL) ||
3515 (img_header_entry[i].start_offset == 0) ||
3516 (img_header_entry[i].size == 0))
3517 break;
Kishor PK49831502017-04-21 17:55:43 +05303518 if ((UINT_MAX - img_header_entry[i].start_offset) < (uintptr_t)data) {
3519 fastboot_fail("Integer overflow detected in start_offset of img");
3520 break;
3521 }
3522 else if ((UINT_MAX - (img_header_entry[i].start_offset + (uintptr_t)data)) < img_header_entry[i].size) {
3523 fastboot_fail("Integer overflow detected in size of img");
3524 break;
3525 }
Parth Dixit3e2d3032016-03-04 17:11:52 +05303526 if( data_end < ((uintptr_t)data + img_header_entry[i].start_offset
3527 + img_header_entry[i].size) )
3528 {
3529 fastboot_fail("Cannot flash: image size mismatch");
3530 break;
3531 }
3532
Ajay Dudanide984792015-03-02 09:57:41 -08003533 cmd_flash_mmc_img(img_header_entry[i].ptn_name,
3534 (void *) data + img_header_entry[i].start_offset,
3535 img_header_entry[i].size);
3536 }
3537
Channagoud Kadabiaafbbfc2015-03-04 17:52:56 -08003538 if (!strncmp(arg, "bootloader", strlen("bootloader")))
3539 {
3540 strlcpy(device.bootloader_version, TARGET(BOARD), MAX_VERSION_LEN);
3541 strlcat(device.bootloader_version, "-", MAX_VERSION_LEN);
3542 strlcat(device.bootloader_version, meta_header->img_version, MAX_VERSION_LEN);
3543 }
3544 else
3545 {
3546 strlcpy(device.radio_version, TARGET(BOARD), MAX_VERSION_LEN);
3547 strlcat(device.radio_version, "-", MAX_VERSION_LEN);
3548 strlcat(device.radio_version, meta_header->img_version, MAX_VERSION_LEN);
3549 }
3550
3551 write_device_info(&device);
Ajay Dudanide984792015-03-02 09:57:41 -08003552 fastboot_okay("");
3553 return;
3554}
3555
Ajay Dudani5c761132011-04-07 20:19:04 -07003556void cmd_flash_mmc_sparse_img(const char *arg, void *data, unsigned sz)
3557{
3558 unsigned int chunk;
wufeng.jiang813dc352015-06-02 23:02:46 -04003559 uint64_t chunk_data_sz;
Channagoud Kadabif627cf82013-09-09 14:08:20 -07003560 uint32_t *fill_buf = NULL;
3561 uint32_t fill_val;
Mayank Grover4f50bba2017-07-19 18:17:08 +05303562 uint32_t blk_sz_actual = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07003563 sparse_header_t *sparse_header;
3564 chunk_header_t *chunk_header;
Ajay Dudaniab18f022011-05-12 14:39:22 -07003565 uint32_t total_blocks = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07003566 unsigned long long ptn = 0;
Channagoud Kadabi65b91002011-10-11 17:34:33 +05303567 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07003568 int index = INVALID_PTN;
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05303569 uint32_t i;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003570 uint8_t lun = 0;
vijay kumar800255e2015-04-24 20:26:19 +05303571 /*End of the sparse image address*/
Parth Dixit64b1a482016-03-07 16:31:26 +05303572 uintptr_t data_end = (uintptr_t)data + sz;
Ajay Dudani5c761132011-04-07 20:19:04 -07003573
Kinson Chikf1a43512011-07-14 11:28:39 -07003574 index = partition_get_index(arg);
3575 ptn = partition_get_offset(index);
3576 if(ptn == 0) {
Ajay Dudani5c761132011-04-07 20:19:04 -07003577 fastboot_fail("partition table doesn't exist");
3578 return;
3579 }
3580
Channagoud Kadabi65b91002011-10-11 17:34:33 +05303581 size = partition_get_size(index);
Channagoud Kadabi65b91002011-10-11 17:34:33 +05303582
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003583 lun = partition_get_lun(index);
3584 mmc_set_lun(lun);
3585
vijay kumar800255e2015-04-24 20:26:19 +05303586 if (sz < sizeof(sparse_header_t)) {
3587 fastboot_fail("size too low");
3588 return;
3589 }
3590
Ajay Dudani5c761132011-04-07 20:19:04 -07003591 /* Read and skip over sparse image header */
3592 sparse_header = (sparse_header_t *) data;
vijay kumar800255e2015-04-24 20:26:19 +05303593
Mayank Grover48bd9bb2017-07-19 12:04:16 +05303594 if (!sparse_header->blk_sz || (sparse_header->blk_sz % 4)){
3595 fastboot_fail("Invalid block size\n");
3596 return;
3597 }
3598
vijay kumar1321f342015-03-27 12:13:42 +05303599 if (((uint64_t)sparse_header->total_blks * (uint64_t)sparse_header->blk_sz) > size) {
Ajay Dudani876b3282012-12-21 14:12:17 -08003600 fastboot_fail("size too large");
3601 return;
3602 }
3603
vijay kumarde4fcf62015-04-23 13:05:49 +05303604 data += sizeof(sparse_header_t);
vijay kumar800255e2015-04-24 20:26:19 +05303605
Parth Dixit64b1a482016-03-07 16:31:26 +05303606 if (data_end < (uintptr_t)data) {
vijay kumar800255e2015-04-24 20:26:19 +05303607 fastboot_fail("buffer overreads occured due to invalid sparse header");
3608 return;
3609 }
3610
vijay kumarde4fcf62015-04-23 13:05:49 +05303611 if(sparse_header->file_hdr_sz != sizeof(sparse_header_t))
Ajay Dudani5c761132011-04-07 20:19:04 -07003612 {
vijay kumarde4fcf62015-04-23 13:05:49 +05303613 fastboot_fail("sparse header size mismatch");
3614 return;
Ajay Dudani5c761132011-04-07 20:19:04 -07003615 }
3616
Ajay Dudanib06c05f2011-05-12 14:46:10 -07003617 dprintf (SPEW, "=== Sparse Image Header ===\n");
3618 dprintf (SPEW, "magic: 0x%x\n", sparse_header->magic);
3619 dprintf (SPEW, "major_version: 0x%x\n", sparse_header->major_version);
3620 dprintf (SPEW, "minor_version: 0x%x\n", sparse_header->minor_version);
3621 dprintf (SPEW, "file_hdr_sz: %d\n", sparse_header->file_hdr_sz);
3622 dprintf (SPEW, "chunk_hdr_sz: %d\n", sparse_header->chunk_hdr_sz);
3623 dprintf (SPEW, "blk_sz: %d\n", sparse_header->blk_sz);
3624 dprintf (SPEW, "total_blks: %d\n", sparse_header->total_blks);
3625 dprintf (SPEW, "total_chunks: %d\n", sparse_header->total_chunks);
Ajay Dudani5c761132011-04-07 20:19:04 -07003626
3627 /* Start processing chunks */
3628 for (chunk=0; chunk<sparse_header->total_chunks; chunk++)
3629 {
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05303630 /* Make sure the total image size does not exceed the partition size */
3631 if(((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz) >= size) {
3632 fastboot_fail("size too large");
3633 return;
3634 }
Ajay Dudani5c761132011-04-07 20:19:04 -07003635 /* Read and skip over chunk header */
3636 chunk_header = (chunk_header_t *) data;
3637 data += sizeof(chunk_header_t);
3638
Parth Dixit64b1a482016-03-07 16:31:26 +05303639 if (data_end < (uintptr_t)data) {
vijay kumar800255e2015-04-24 20:26:19 +05303640 fastboot_fail("buffer overreads occured due to invalid sparse header");
3641 return;
3642 }
3643
Ajay Dudani5c761132011-04-07 20:19:04 -07003644 dprintf (SPEW, "=== Chunk Header ===\n");
3645 dprintf (SPEW, "chunk_type: 0x%x\n", chunk_header->chunk_type);
3646 dprintf (SPEW, "chunk_data_sz: 0x%x\n", chunk_header->chunk_sz);
3647 dprintf (SPEW, "total_size: 0x%x\n", chunk_header->total_sz);
3648
vijay kumar800255e2015-04-24 20:26:19 +05303649 if(sparse_header->chunk_hdr_sz != sizeof(chunk_header_t))
Ajay Dudani5c761132011-04-07 20:19:04 -07003650 {
vijay kumar800255e2015-04-24 20:26:19 +05303651 fastboot_fail("chunk header size mismatch");
3652 return;
Ajay Dudani5c761132011-04-07 20:19:04 -07003653 }
3654
wufeng.jiang813dc352015-06-02 23:02:46 -04003655 chunk_data_sz = (uint64_t)sparse_header->blk_sz * chunk_header->chunk_sz;
3656
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05303657 /* Make sure that the chunk size calculated from sparse image does not
3658 * exceed partition size
3659 */
3660 if ((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz + chunk_data_sz > size)
3661 {
3662 fastboot_fail("Chunk data size exceeds partition size");
3663 return;
3664 }
3665
Ajay Dudani5c761132011-04-07 20:19:04 -07003666 switch (chunk_header->chunk_type)
3667 {
3668 case CHUNK_TYPE_RAW:
wufeng.jiang813dc352015-06-02 23:02:46 -04003669 if((uint64_t)chunk_header->total_sz != ((uint64_t)sparse_header->chunk_hdr_sz +
Ajay Dudani5c761132011-04-07 20:19:04 -07003670 chunk_data_sz))
3671 {
3672 fastboot_fail("Bogus chunk size for chunk type Raw");
3673 return;
3674 }
3675
Parth Dixit64b1a482016-03-07 16:31:26 +05303676 if (data_end < (uintptr_t)data + chunk_data_sz) {
vijay kumar800255e2015-04-24 20:26:19 +05303677 fastboot_fail("buffer overreads occured due to invalid sparse header");
3678 return;
3679 }
3680
wufeng.jiang813dc352015-06-02 23:02:46 -04003681 /* chunk_header->total_sz is uint32,So chunk_data_sz is now less than 2^32
3682 otherwise it will return in the line above
3683 */
Ajay Dudaniab18f022011-05-12 14:39:22 -07003684 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
wufeng.jiang813dc352015-06-02 23:02:46 -04003685 (uint32_t)chunk_data_sz,
Ajay Dudaniab18f022011-05-12 14:39:22 -07003686 (unsigned int*)data))
Ajay Dudani5c761132011-04-07 20:19:04 -07003687 {
3688 fastboot_fail("flash write failure");
3689 return;
3690 }
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05303691 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
3692 fastboot_fail("Bogus size for RAW chunk type");
3693 return;
3694 }
Ajay Dudani5c761132011-04-07 20:19:04 -07003695 total_blocks += chunk_header->chunk_sz;
wufeng.jiang813dc352015-06-02 23:02:46 -04003696 data += (uint32_t)chunk_data_sz;
Ajay Dudani5c761132011-04-07 20:19:04 -07003697 break;
3698
Channagoud Kadabif627cf82013-09-09 14:08:20 -07003699 case CHUNK_TYPE_FILL:
3700 if(chunk_header->total_sz != (sparse_header->chunk_hdr_sz +
3701 sizeof(uint32_t)))
3702 {
3703 fastboot_fail("Bogus chunk size for chunk type FILL");
3704 return;
3705 }
3706
Mayank Grover4f50bba2017-07-19 18:17:08 +05303707 blk_sz_actual = ROUNDUP(sparse_header->blk_sz, CACHE_LINE);
3708 /* Integer overflow detected */
3709 if (blk_sz_actual < sparse_header->blk_sz)
3710 {
3711 fastboot_fail("Invalid block size");
3712 return;
3713 }
3714
3715 fill_buf = (uint32_t *)memalign(CACHE_LINE, blk_sz_actual);
Channagoud Kadabif627cf82013-09-09 14:08:20 -07003716 if (!fill_buf)
3717 {
3718 fastboot_fail("Malloc failed for: CHUNK_TYPE_FILL");
3719 return;
3720 }
3721
Parth Dixit64b1a482016-03-07 16:31:26 +05303722 if (data_end < (uintptr_t)data + sizeof(uint32_t)) {
vijay kumar800255e2015-04-24 20:26:19 +05303723 fastboot_fail("buffer overreads occured due to invalid sparse header");
Gaurav Nebhwani163cbf82016-03-17 21:21:27 +05303724 free(fill_buf);
vijay kumar800255e2015-04-24 20:26:19 +05303725 return;
3726 }
Channagoud Kadabif627cf82013-09-09 14:08:20 -07003727 fill_val = *(uint32_t *)data;
3728 data = (char *) data + sizeof(uint32_t);
Channagoud Kadabif627cf82013-09-09 14:08:20 -07003729
3730 for (i = 0; i < (sparse_header->blk_sz / sizeof(fill_val)); i++)
3731 {
3732 fill_buf[i] = fill_val;
3733 }
3734
Gaurav Nebhwanie94cbe12016-03-17 21:16:34 +05303735 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz))
3736 {
3737 fastboot_fail("bogus size for chunk FILL type");
3738 free(fill_buf);
3739 return;
3740 }
3741
wufeng.jiang813dc352015-06-02 23:02:46 -04003742 for (i = 0; i < chunk_header->chunk_sz; i++)
Channagoud Kadabif627cf82013-09-09 14:08:20 -07003743 {
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05303744 /* Make sure that the data written to partition does not exceed partition size */
3745 if ((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz + sparse_header->blk_sz > size)
3746 {
3747 fastboot_fail("Chunk data size for fill type exceeds partition size");
Gaurav Nebhwani163cbf82016-03-17 21:21:27 +05303748 free(fill_buf);
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05303749 return;
3750 }
3751
Channagoud Kadabif627cf82013-09-09 14:08:20 -07003752 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
3753 sparse_header->blk_sz,
3754 fill_buf))
3755 {
3756 fastboot_fail("flash write failure");
3757 free(fill_buf);
3758 return;
3759 }
3760
3761 total_blocks++;
3762 }
3763
3764 free(fill_buf);
3765 break;
3766
Ajay Dudani5c761132011-04-07 20:19:04 -07003767 case CHUNK_TYPE_DONT_CARE:
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05303768 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
3769 fastboot_fail("bogus size for chunk DONT CARE type");
3770 return;
3771 }
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07003772 total_blocks += chunk_header->chunk_sz;
3773 break;
3774
Ajay Dudani5c761132011-04-07 20:19:04 -07003775 case CHUNK_TYPE_CRC:
3776 if(chunk_header->total_sz != sparse_header->chunk_hdr_sz)
3777 {
wufeng.jiang813dc352015-06-02 23:02:46 -04003778 fastboot_fail("Bogus chunk size for chunk type CRC");
Ajay Dudani5c761132011-04-07 20:19:04 -07003779 return;
3780 }
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05303781 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
3782 fastboot_fail("bogus size for chunk CRC type");
3783 return;
3784 }
Ajay Dudani5c761132011-04-07 20:19:04 -07003785 total_blocks += chunk_header->chunk_sz;
Parth Dixit64b1a482016-03-07 16:31:26 +05303786 if ((uintptr_t)data > UINT_MAX - chunk_data_sz) {
vijay kumar800255e2015-04-24 20:26:19 +05303787 fastboot_fail("integer overflow occured");
3788 return;
3789 }
wufeng.jiang813dc352015-06-02 23:02:46 -04003790 data += (uint32_t)chunk_data_sz;
Parth Dixit64b1a482016-03-07 16:31:26 +05303791 if (data_end < (uintptr_t)data) {
vijay kumar800255e2015-04-24 20:26:19 +05303792 fastboot_fail("buffer overreads occured due to invalid sparse header");
3793 return;
3794 }
Ajay Dudani5c761132011-04-07 20:19:04 -07003795 break;
3796
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07003797 default:
Channagoud Kadabif627cf82013-09-09 14:08:20 -07003798 dprintf(CRITICAL, "Unkown chunk type: %x\n",chunk_header->chunk_type);
Ajay Dudani5c761132011-04-07 20:19:04 -07003799 fastboot_fail("Unknown chunk type");
3800 return;
3801 }
3802 }
3803
Ajay Dudani0c6927b2011-05-18 11:12:16 -07003804 dprintf(INFO, "Wrote %d blocks, expected to write %d blocks\n",
3805 total_blocks, sparse_header->total_blks);
3806
3807 if(total_blocks != sparse_header->total_blks)
3808 {
3809 fastboot_fail("sparse image write failure");
3810 }
Ajay Dudani5c761132011-04-07 20:19:04 -07003811
3812 fastboot_okay("");
3813 return;
3814}
3815
3816void cmd_flash_mmc(const char *arg, void *data, unsigned sz)
3817{
3818 sparse_header_t *sparse_header;
Ajay Dudanide984792015-03-02 09:57:41 -08003819 meta_header_t *meta_header;
Ajay Dudani5c761132011-04-07 20:19:04 -07003820
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07003821#ifdef SSD_ENABLE
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -08003822 /* 8 Byte Magic + 2048 Byte xml + Encrypted Data */
3823 unsigned int *magic_number = (unsigned int *) data;
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08003824 int ret=0;
3825 uint32 major_version=0;
3826 uint32 minor_version=0;
3827
3828 ret = scm_svc_version(&major_version,&minor_version);
3829 if(!ret)
3830 {
3831 if(major_version >= 2)
3832 {
Amir Samuelovbb65ce02013-05-05 12:20:18 +03003833 if( !strcmp(arg, "ssd") || !strcmp(arg, "tqs") )
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08003834 {
3835 ret = encrypt_scm((uint32 **) &data, &sz);
3836 if (ret != 0) {
3837 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
3838 return;
3839 }
3840
Amir Samuelovbb65ce02013-05-05 12:20:18 +03003841 /* Protect only for SSD */
3842 if (!strcmp(arg, "ssd")) {
3843 ret = scm_protect_keystore((uint32 *) data, sz);
3844 if (ret != 0) {
3845 dprintf(CRITICAL, "ERROR: scm_protect_keystore Failed\n");
3846 return;
3847 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08003848 }
3849 }
3850 else
3851 {
3852 ret = decrypt_scm_v2((uint32 **) &data, &sz);
3853 if(ret != 0)
3854 {
3855 dprintf(CRITICAL,"ERROR: Decryption Failure\n");
3856 return;
3857 }
3858 }
3859 }
3860 else
3861 {
3862 if (magic_number[0] == DECRYPT_MAGIC_0 &&
3863 magic_number[1] == DECRYPT_MAGIC_1)
3864 {
3865 ret = decrypt_scm((uint32 **) &data, &sz);
3866 if (ret != 0) {
3867 dprintf(CRITICAL, "ERROR: Invalid secure image\n");
3868 return;
3869 }
3870 }
3871 else if (magic_number[0] == ENCRYPT_MAGIC_0 &&
3872 magic_number[1] == ENCRYPT_MAGIC_1)
3873 {
3874 ret = encrypt_scm((uint32 **) &data, &sz);
3875 if (ret != 0) {
3876 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
3877 return;
3878 }
3879 }
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07003880 }
3881 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08003882 else
Neeti Desai127b9e02012-03-20 16:11:23 -07003883 {
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08003884 dprintf(CRITICAL,"INVALID SVC Version\n");
3885 return;
Neeti Desai127b9e02012-03-20 16:11:23 -07003886 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08003887#endif /* SSD_ENABLE */
Neeti Desai127b9e02012-03-20 16:11:23 -07003888
Monika Singh292b3e92018-03-17 22:40:23 +05303889#if VERIFIED_BOOT || VERIFIED_BOOT_2
Channagoud Kadabi35297672015-06-13 11:09:49 -07003890 if (target_build_variant_user())
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003891 {
lijuang511a2b52015-08-14 20:50:51 +08003892 /* if device is locked:
3893 * common partition will not allow to be flashed
3894 * critical partition will allow to flash image.
3895 */
3896 if(!device.is_unlocked && !critical_flash_allowed(arg)) {
3897 fastboot_fail("Partition flashing is not allowed");
3898 return;
3899 }
Mayank Grover889be1b2017-09-12 20:12:23 +05303900
lijuang511a2b52015-08-14 20:50:51 +08003901 /* if device critical is locked:
3902 * common partition will allow to be flashed
3903 * critical partition will not allow to flash image.
3904 */
Mayank Grover912eaa62017-10-26 12:08:53 +05303905 if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05303906 !device.is_unlock_critical &&
3907 critical_flash_allowed(arg)) {
3908 fastboot_fail("Critical partition flashing is not allowed");
3909 return;
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003910 }
3911 }
3912#endif
Monika Singh292b3e92018-03-17 22:40:23 +05303913 if (!strncmp(arg, "avb_custom_key", strlen("avb_custom_key"))) {
3914 dprintf(INFO, "flashing avb_custom_key\n");
3915 if (store_userkey(data, sz)) {
3916 fastboot_fail("Flashing avb_custom_key failed");
3917 } else {
3918 fastboot_okay("");
3919 }
3920 return;
3921 }
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003922
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07003923 sparse_header = (sparse_header_t *) data;
Ajay Dudanide984792015-03-02 09:57:41 -08003924 meta_header = (meta_header_t *) data;
3925 if (sparse_header->magic == SPARSE_HEADER_MAGIC)
Ajay Dudani5c761132011-04-07 20:19:04 -07003926 cmd_flash_mmc_sparse_img(arg, data, sz);
Ajay Dudanide984792015-03-02 09:57:41 -08003927 else if (meta_header->magic == META_HEADER_MAGIC)
3928 cmd_flash_meta_img(arg, data, sz);
3929 else
3930 cmd_flash_mmc_img(arg, data, sz);
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07003931
3932#if VERIFIED_BOOT
Mayank Grover912eaa62017-10-26 12:08:53 +05303933 if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05303934 (!strncmp(arg, "system", 6)) &&
3935 !device.verity_mode)
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07003936 // reset dm_verity mode to enforcing
3937 device.verity_mode = 1;
3938 write_device_info(&device);
Parth Dixitddbc7352015-10-18 03:13:31 +05303939#endif
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07003940
Ajay Dudani5c761132011-04-07 20:19:04 -07003941 return;
3942}
3943
Tanya Brokhman84eaaf62015-02-24 21:57:06 +02003944void cmd_updatevol(const char *vol_name, void *data, unsigned sz)
3945{
3946 struct ptentry *sys_ptn;
3947 struct ptable *ptable;
3948
3949 ptable = flash_get_ptable();
3950 if (ptable == NULL) {
3951 fastboot_fail("partition table doesn't exist");
3952 return;
3953 }
3954
3955 sys_ptn = ptable_find(ptable, "system");
3956 if (sys_ptn == NULL) {
3957 fastboot_fail("system partition not found");
3958 return;
3959 }
3960
3961 sz = ROUND_TO_PAGE(sz, page_mask);
3962 if (update_ubi_vol(sys_ptn, vol_name, data, sz))
3963 fastboot_fail("update_ubi_vol failed");
3964 else
3965 fastboot_okay("");
3966}
3967
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003968void cmd_flash_nand(const char *arg, void *data, unsigned sz)
Dima Zavin214cc642009-01-26 11:16:21 -08003969{
3970 struct ptentry *ptn;
3971 struct ptable *ptable;
3972 unsigned extra = 0;
Gaurav Nebhwanid9730712016-05-06 14:28:29 +05303973 uint64_t partition_size = 0;
Vijay Kumar Pendotic6f22a72017-05-19 22:44:34 +05303974 unsigned bytes_to_round_page = 0;
3975 unsigned rounded_size = 0;
Dima Zavin214cc642009-01-26 11:16:21 -08003976
Kishor PK38ed93d2017-04-25 14:19:26 +05303977 if((uintptr_t)data > (UINT_MAX - sz)) {
3978 fastboot_fail("Cannot flash: image header corrupt");
3979 return;
3980 }
3981
Dima Zavin214cc642009-01-26 11:16:21 -08003982 ptable = flash_get_ptable();
3983 if (ptable == NULL) {
3984 fastboot_fail("partition table doesn't exist");
3985 return;
3986 }
3987
3988 ptn = ptable_find(ptable, arg);
3989 if (ptn == NULL) {
Tanya Brokhman84eaaf62015-02-24 21:57:06 +02003990 dprintf(INFO, "unknown partition name (%s). Trying updatevol\n",
3991 arg);
3992 cmd_updatevol(arg, data, sz);
Dima Zavin214cc642009-01-26 11:16:21 -08003993 return;
3994 }
3995
Monika Singh292b3e92018-03-17 22:40:23 +05303996 if (!strncmp(arg, "avb_custom_key", strlen("avb_custom_key"))) {
3997 dprintf(INFO, "flashing avb_custom_key\n");
3998 if (store_userkey(data, sz)) {
3999 fastboot_fail("Flashing avb_custom_key failed");
4000 } else {
4001 fastboot_okay("");
4002 }
4003 return;
4004 }
4005
Dima Zavin214cc642009-01-26 11:16:21 -08004006 if (!strcmp(ptn->name, "boot") || !strcmp(ptn->name, "recovery")) {
Kishor PK38ed93d2017-04-25 14:19:26 +05304007 if((sz > BOOT_MAGIC_SIZE) && (!memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE))) {
4008 dprintf(INFO, "Verified the BOOT_MAGIC in image header \n");
4009 } else {
4010 fastboot_fail("Image is not a boot image");
Dima Zavin214cc642009-01-26 11:16:21 -08004011 return;
4012 }
4013 }
4014
Amol Jadi5c61a952012-05-04 17:05:35 -07004015 if (!strcmp(ptn->name, "system")
Deepa Dinamani13e32c42012-03-12 14:34:17 -07004016 || !strcmp(ptn->name, "userdata")
4017 || !strcmp(ptn->name, "persist")
Sundarajan Srinivasanb063a852013-11-19 14:02:27 -08004018 || !strcmp(ptn->name, "recoveryfs")
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -08004019 || !strcmp(ptn->name, "modem"))
Tanya Brokhman1c94f1a2015-02-15 09:05:03 +02004020 extra = 1;
Kishor PK38ed93d2017-04-25 14:19:26 +05304021 else {
Vijay Kumar Pendotic6f22a72017-05-19 22:44:34 +05304022 rounded_size = ROUNDUP(sz, page_size);
4023 bytes_to_round_page = rounded_size - sz;
4024 if (bytes_to_round_page) {
4025 if (((uintptr_t)data + sz ) > (UINT_MAX - bytes_to_round_page)) {
4026 fastboot_fail("Integer overflow detected");
4027 return;
4028 }
4029 if (((uintptr_t)data + sz + bytes_to_round_page) >
4030 ((uintptr_t)target_get_scratch_address() + target_get_max_flash_size())) {
4031 fastboot_fail("Buffer size is not aligned to page_size");
4032 return;
4033 }
4034 else {
4035 memset(data + sz, 0, bytes_to_round_page);
4036 sz = rounded_size;
4037 }
Kishor PK38ed93d2017-04-25 14:19:26 +05304038 }
Gaurav Nebhwanid9730712016-05-06 14:28:29 +05304039 }
4040
Kishor PK38ed93d2017-04-25 14:19:26 +05304041 /*Checking partition_size for the possible integer overflow */
4042 partition_size = validate_partition_size(ptn);
4043
Gaurav Nebhwanid9730712016-05-06 14:28:29 +05304044 if (sz > partition_size) {
4045 fastboot_fail("Image size too large");
4046 return;
4047 }
4048
Dima Zavin214cc642009-01-26 11:16:21 -08004049 dprintf(INFO, "writing %d bytes to '%s'\n", sz, ptn->name);
Mayank Grover6cde9352017-06-06 18:45:23 +05304050 if ((sz > UBI_EC_HDR_SIZE) &&
4051 (!memcmp((void *)data, UBI_MAGIC, UBI_MAGIC_SIZE))) {
Tanya Brokhman1c94f1a2015-02-15 09:05:03 +02004052 if (flash_ubi_img(ptn, data, sz)) {
4053 fastboot_fail("flash write failure");
4054 return;
4055 }
4056 } else {
4057 if (flash_write(ptn, extra, data, sz)) {
4058 fastboot_fail("flash write failure");
4059 return;
4060 }
Dima Zavin214cc642009-01-26 11:16:21 -08004061 }
4062 dprintf(INFO, "partition '%s' updated\n", ptn->name);
4063 fastboot_okay("");
4064}
4065
Kishor PK38ed93d2017-04-25 14:19:26 +05304066
4067static inline uint64_t validate_partition_size(struct ptentry *ptn)
4068{
4069 if (ptn->length && flash_num_pages_per_blk() && page_size) {
4070 if ((ptn->length < ( UINT_MAX / flash_num_pages_per_blk())) && ((ptn->length * flash_num_pages_per_blk()) < ( UINT_MAX / page_size))) {
4071 return ptn->length * flash_num_pages_per_blk() * page_size;
4072 }
4073 }
4074 return 0;
4075}
4076
4077
Sridhar Parasurame94e8152014-10-24 14:06:03 -07004078void cmd_flash(const char *arg, void *data, unsigned sz)
4079{
4080 if(target_is_emmc_boot())
4081 cmd_flash_mmc(arg, data, sz);
4082 else
4083 cmd_flash_nand(arg, data, sz);
4084}
4085
Dima Zavin214cc642009-01-26 11:16:21 -08004086void cmd_continue(const char *arg, void *data, unsigned sz)
4087{
4088 fastboot_okay("");
Amol Jadi7c4316c2013-10-07 14:19:26 -07004089 fastboot_stop();
Shashank Mittald3e54dd2014-08-28 15:24:02 -07004090
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004091 if (target_is_emmc_boot())
4092 {
lijuanga40d6302015-07-20 20:10:13 +08004093#if FBCON_DISPLAY_MSG
lijuangde34d502016-02-26 16:04:50 +08004094 /* Exit keys' detection thread firstly */
4095 exit_menu_keys_detection();
lijuanga40d6302015-07-20 20:10:13 +08004096#endif
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004097 boot_linux_from_mmc();
4098 }
4099 else
4100 {
4101 boot_linux_from_flash();
4102 }
Dima Zavin214cc642009-01-26 11:16:21 -08004103}
4104
Chandan Uddaraju94183c02010-01-15 15:13:59 -08004105void cmd_reboot(const char *arg, void *data, unsigned sz)
4106{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004107 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08004108 fastboot_okay("");
4109 reboot_device(0);
4110}
4111
Mayank Grover351a75e2017-05-30 20:06:08 +05304112void cmd_set_active(const char *arg, void *data, unsigned sz)
4113{
Mayank Grover6ccc1c92017-07-04 17:36:46 +05304114 char *p, *sp = NULL;
Mayank Grover351a75e2017-05-30 20:06:08 +05304115 unsigned i,current_active_slot;
4116 const char *current_slot_suffix;
4117
4118 if (!partition_multislot_is_supported())
4119 {
4120 fastboot_fail("Command not supported");
4121 return;
4122 }
4123
4124 if (arg)
4125 {
Mayank Grover6ccc1c92017-07-04 17:36:46 +05304126 p = strtok_r((char *)arg, ":", &sp);
Mayank Grover5eb5f692017-07-19 20:16:04 +05304127 if (p)
Mayank Grover351a75e2017-05-30 20:06:08 +05304128 {
4129 current_active_slot = partition_find_active_slot();
4130
4131 /* Check if trying to make curent slot active */
4132 current_slot_suffix = SUFFIX_SLOT(current_active_slot);
Mayank Grover6ccc1c92017-07-04 17:36:46 +05304133 current_slot_suffix = strtok_r((char *)current_slot_suffix,
4134 (char *)suffix_delimiter, &sp);
4135
Mayank Grover5eb5f692017-07-19 20:16:04 +05304136 if (current_slot_suffix &&
4137 !strncmp(p, current_slot_suffix, strlen(current_slot_suffix)))
Mayank Grover351a75e2017-05-30 20:06:08 +05304138 {
4139 fastboot_okay("Slot already set active");
4140 return;
4141 }
4142 else
4143 {
4144 for (i = 0; i < AB_SUPPORTED_SLOTS; i++)
4145 {
4146 current_slot_suffix = SUFFIX_SLOT(i);
Mayank Grover6ccc1c92017-07-04 17:36:46 +05304147 current_slot_suffix = strtok_r((char *)current_slot_suffix,
4148 (char *)suffix_delimiter, &sp);
Mayank Grover5eb5f692017-07-19 20:16:04 +05304149 if (current_slot_suffix &&
4150 !strncmp(p, current_slot_suffix, strlen(current_slot_suffix)))
Mayank Grover351a75e2017-05-30 20:06:08 +05304151 {
4152 partition_switch_slots(current_active_slot, i);
4153 publish_getvar_multislot_vars();
4154 fastboot_okay("");
4155 return;
4156 }
4157 }
4158 }
4159 }
4160 }
4161 fastboot_fail("Invalid slot suffix.");
4162 return;
4163}
4164
Chandan Uddaraju94183c02010-01-15 15:13:59 -08004165void cmd_reboot_bootloader(const char *arg, void *data, unsigned sz)
4166{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004167 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08004168 fastboot_okay("");
4169 reboot_device(FASTBOOT_MODE);
4170}
4171
Ameya Thakur11cf1a62013-08-05 12:44:48 -07004172void cmd_oem_enable_charger_screen(const char *arg, void *data, unsigned size)
4173{
4174 dprintf(INFO, "Enabling charger screen check\n");
4175 device.charger_screen_enabled = 1;
4176 write_device_info(&device);
4177 fastboot_okay("");
4178}
4179
4180void cmd_oem_disable_charger_screen(const char *arg, void *data, unsigned size)
4181{
4182 dprintf(INFO, "Disabling charger screen check\n");
4183 device.charger_screen_enabled = 0;
4184 write_device_info(&device);
4185 fastboot_okay("");
4186}
4187
lijuanga25d8bb2015-09-16 13:11:52 +08004188void cmd_oem_off_mode_charger(const char *arg, void *data, unsigned size)
4189{
4190 char *p = NULL;
4191 const char *delim = " \t\n\r";
Parth Dixit407f15b2016-01-07 14:47:37 +05304192 char *sp;
lijuanga25d8bb2015-09-16 13:11:52 +08004193
4194 if (arg) {
Parth Dixit407f15b2016-01-07 14:47:37 +05304195 p = strtok_r((char *)arg, delim, &sp);
lijuanga25d8bb2015-09-16 13:11:52 +08004196 if (p) {
4197 if (!strncmp(p, "0", 1)) {
4198 device.charger_screen_enabled = 0;
4199 } else if (!strncmp(p, "1", 1)) {
4200 device.charger_screen_enabled = 1;
4201 }
4202 }
4203 }
4204
4205 /* update charger_screen_enabled value for getvar
4206 * command
4207 */
4208 snprintf(charger_screen_enabled, MAX_RSP_SIZE, "%d",
4209 device.charger_screen_enabled);
4210
4211 write_device_info(&device);
4212 fastboot_okay("");
4213}
4214
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05304215void cmd_oem_select_display_panel(const char *arg, void *data, unsigned size)
4216{
4217 dprintf(INFO, "Selecting display panel %s\n", arg);
4218 if (arg)
4219 strlcpy(device.display_panel, arg,
4220 sizeof(device.display_panel));
4221 write_device_info(&device);
4222 fastboot_okay("");
4223}
4224
Shashank Mittal162244e2011-08-08 19:01:25 -07004225void cmd_oem_unlock(const char *arg, void *data, unsigned sz)
4226{
lijuang4ece1e72015-08-14 21:02:36 +08004227 set_device_unlock(UNLOCK, TRUE);
vijay kumarc65876c2015-04-24 13:29:16 +05304228}
4229
4230void cmd_oem_unlock_go(const char *arg, void *data, unsigned sz)
4231{
lijuang4ece1e72015-08-14 21:02:36 +08004232 if(!device.is_unlocked) {
vijay kumarc65876c2015-04-24 13:29:16 +05304233 if(!is_allow_unlock) {
4234 fastboot_fail("oem unlock is not allowed");
4235 return;
4236 }
4237
lijuang4ece1e72015-08-14 21:02:36 +08004238 set_device_unlock_value(UNLOCK, TRUE);
vijay kumarc65876c2015-04-24 13:29:16 +05304239
lijuang4ece1e72015-08-14 21:02:36 +08004240 /* wipe data */
vijay kumarc65876c2015-04-24 13:29:16 +05304241 struct recovery_message msg;
Kishor PK60a68212017-05-08 16:55:57 +05304242 memset(&msg, 0, sizeof(msg));
vijay kumarc65876c2015-04-24 13:29:16 +05304243 snprintf(msg.recovery, sizeof(msg.recovery), "recovery\n--wipe_data");
4244 write_misc(0, &msg, sizeof(msg));
4245
4246 fastboot_okay("");
4247 reboot_device(RECOVERY_MODE);
Shashank Mittald3e54dd2014-08-28 15:24:02 -07004248 }
4249 fastboot_okay("");
4250}
4251
Channagoud Kadabiad259832015-05-29 11:14:17 -07004252static int aboot_frp_unlock(char *pname, void *data, unsigned sz)
4253{
Mayank Grover70f8b0e2017-05-17 11:49:00 +05304254 int ret=1;
4255 bool authentication_success=false;
Channagoud Kadabiad259832015-05-29 11:14:17 -07004256
Mayank Grover70f8b0e2017-05-17 11:49:00 +05304257 /*
4258 Authentication method not implemented.
4259
4260 OEM to implement, authentication system which on successful validataion,
4261 calls write_allow_oem_unlock() with is_allow_unlock.
4262 */
4263#if 0
4264 authentication_success = oem_specific_auth_mthd();
4265#endif
4266
4267 if (authentication_success)
Channagoud Kadabiad259832015-05-29 11:14:17 -07004268 {
Mayank Grover70f8b0e2017-05-17 11:49:00 +05304269 is_allow_unlock = true;
4270 write_allow_oem_unlock(is_allow_unlock);
4271 ret = 0;
Channagoud Kadabiad259832015-05-29 11:14:17 -07004272 }
4273 return ret;
4274}
4275
Shashank Mittald3e54dd2014-08-28 15:24:02 -07004276void cmd_oem_lock(const char *arg, void *data, unsigned sz)
4277{
lijuang4ece1e72015-08-14 21:02:36 +08004278 set_device_unlock(UNLOCK, FALSE);
Shashank Mittal162244e2011-08-08 19:01:25 -07004279}
4280
Shashank Mittala0032282011-08-26 14:50:11 -07004281void cmd_oem_devinfo(const char *arg, void *data, unsigned sz)
4282{
lijuang511a2b52015-08-14 20:50:51 +08004283 char response[MAX_RSP_SIZE];
Ameya Thakur11cf1a62013-08-05 12:44:48 -07004284 snprintf(response, sizeof(response), "\tDevice tampered: %s", (device.is_tampered ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07004285 fastboot_info(response);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07004286 snprintf(response, sizeof(response), "\tDevice unlocked: %s", (device.is_unlocked ? "true" : "false"));
4287 fastboot_info(response);
Monika Singh292b3e92018-03-17 22:40:23 +05304288#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover912eaa62017-10-26 12:08:53 +05304289 if (VB_M <= target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +05304290 {
4291 snprintf(response, sizeof(response), "\tDevice critical unlocked: %s",
4292 (device.is_unlock_critical ? "true" : "false"));
4293 fastboot_info(response);
4294 }
Parth Dixitddbc7352015-10-18 03:13:31 +05304295#endif
Ameya Thakur11cf1a62013-08-05 12:44:48 -07004296 snprintf(response, sizeof(response), "\tCharger screen enabled: %s", (device.charger_screen_enabled ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07004297 fastboot_info(response);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05304298 snprintf(response, sizeof(response), "\tDisplay panel: %s", (device.display_panel));
4299 fastboot_info(response);
Shashank Mittala0032282011-08-26 14:50:11 -07004300 fastboot_okay("");
4301}
4302
lijuang511a2b52015-08-14 20:50:51 +08004303void cmd_flashing_get_unlock_ability(const char *arg, void *data, unsigned sz)
4304{
4305 char response[MAX_RSP_SIZE];
4306 snprintf(response, sizeof(response), "\tget_unlock_ability: %d", is_allow_unlock);
4307 fastboot_info(response);
4308 fastboot_okay("");
4309}
4310
4311void cmd_flashing_lock_critical(const char *arg, void *data, unsigned sz)
4312{
lijuang4ece1e72015-08-14 21:02:36 +08004313 set_device_unlock(UNLOCK_CRITICAL, FALSE);
lijuang511a2b52015-08-14 20:50:51 +08004314}
4315
4316void cmd_flashing_unlock_critical(const char *arg, void *data, unsigned sz)
4317{
lijuang4ece1e72015-08-14 21:02:36 +08004318 set_device_unlock(UNLOCK_CRITICAL, TRUE);
lijuang511a2b52015-08-14 20:50:51 +08004319}
4320
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07004321void cmd_preflash(const char *arg, void *data, unsigned sz)
4322{
4323 fastboot_okay("");
4324}
4325
Mao Flynn7b379f32015-04-20 00:28:30 +08004326static uint8_t logo_header[LOGO_IMG_HEADER_SIZE];
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304327
Mao Flynn7b379f32015-04-20 00:28:30 +08004328int splash_screen_check_header(logo_img_header *header)
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304329{
Mao Flynn7b379f32015-04-20 00:28:30 +08004330 if (memcmp(header->magic, LOGO_IMG_MAGIC, 8))
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304331 return -1;
Mao Flynn7b379f32015-04-20 00:28:30 +08004332 if (header->width == 0 || header->height == 0)
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304333 return -1;
4334 return 0;
4335}
4336
Mao Flynn7b379f32015-04-20 00:28:30 +08004337int splash_screen_flash()
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07004338{
4339 struct ptentry *ptn;
4340 struct ptable *ptable;
Mao Flynn7b379f32015-04-20 00:28:30 +08004341 struct logo_img_header *header;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07004342 struct fbcon_config *fb_display = NULL;
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08004343
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304344 ptable = flash_get_ptable();
4345 if (ptable == NULL) {
Mao Flynn7b379f32015-04-20 00:28:30 +08004346 dprintf(CRITICAL, "ERROR: Partition table not found\n");
4347 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304348 }
Mao Flynn7b379f32015-04-20 00:28:30 +08004349
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304350 ptn = ptable_find(ptable, "splash");
4351 if (ptn == NULL) {
4352 dprintf(CRITICAL, "ERROR: splash Partition not found\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08004353 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304354 }
Mao Flynn7b379f32015-04-20 00:28:30 +08004355 if (flash_read(ptn, 0, (void *)logo_header, LOGO_IMG_HEADER_SIZE)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304356 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08004357 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304358 }
4359
Mao Flynn7b379f32015-04-20 00:28:30 +08004360 header = (struct logo_img_header *)logo_header;
4361 if (splash_screen_check_header(header)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304362 dprintf(CRITICAL, "ERROR: Boot image header invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08004363 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304364 }
4365
4366 fb_display = fbcon_display();
4367 if (fb_display) {
Mao Flynn7b379f32015-04-20 00:28:30 +08004368 if (header->type && (header->blocks != 0)) { // RLE24 compressed data
4369 uint8_t *base = (uint8_t *) fb_display->base + LOGO_IMG_OFFSET;
4370
4371 /* if the logo is full-screen size, remove "fbcon_clear()" */
4372 if ((header->width != fb_display->width)
4373 || (header->height != fb_display->height))
4374 fbcon_clear();
4375
4376 if (flash_read(ptn + LOGO_IMG_HEADER_SIZE, 0,
4377 (uint32_t *)base,
4378 (header->blocks * 512))) {
4379 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
4380 return -1;
4381 }
4382 fbcon_extract_to_screen(header, base);
4383 return 0;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07004384 }
Mao Flynn7b379f32015-04-20 00:28:30 +08004385
Veera Sundaram Sankaranc33416f2015-09-28 10:25:55 -07004386 if ((header->width > fb_display->width) || (header->height > fb_display->height)) {
4387 dprintf(CRITICAL, "Logo config greater than fb config. Fall back default logo\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08004388 return -1;
4389 }
4390
Vineet Bajaj99291ed2014-09-09 12:29:46 +05304391 uint8_t *base = (uint8_t *) fb_display->base;
Sachin Bhayare619e9e42017-05-15 13:10:31 +05304392 uint32_t fb_size = ROUNDUP(fb_display->width *
4393 fb_display->height *
4394 (fb_display->bpp / 8), 4096);
4395 uint32_t splash_size = ((((header->width * header->height *
4396 fb_display->bpp/8) + 511) >> 9) << 9);
4397
4398 if (splash_size > fb_size) {
4399 dprintf(CRITICAL, "ERROR: Splash image size invalid\n");
4400 return -1;
4401 }
4402
Mao Flynn7b379f32015-04-20 00:28:30 +08004403 if (flash_read(ptn + LOGO_IMG_HEADER_SIZE, 0,
4404 (uint32_t *)base,
4405 ((((header->width * header->height * fb_display->bpp/8) + 511) >> 9) << 9))) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304406 fbcon_clear();
Vineet Bajaj99291ed2014-09-09 12:29:46 +05304407 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08004408 return -1;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07004409 }
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304410 }
4411
Mao Flynn7b379f32015-04-20 00:28:30 +08004412 return 0;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304413}
4414
Mao Flynn7b379f32015-04-20 00:28:30 +08004415int splash_screen_mmc()
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304416{
4417 int index = INVALID_PTN;
4418 unsigned long long ptn = 0;
4419 struct fbcon_config *fb_display = NULL;
Mao Flynn7b379f32015-04-20 00:28:30 +08004420 struct logo_img_header *header;
Mao Flynn1893a7f2015-06-03 12:03:36 +08004421 uint32_t blocksize, realsize, readsize;
4422 uint8_t *base;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304423
4424 index = partition_get_index("splash");
4425 if (index == 0) {
4426 dprintf(CRITICAL, "ERROR: splash Partition table not found\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08004427 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304428 }
4429
4430 ptn = partition_get_offset(index);
4431 if (ptn == 0) {
4432 dprintf(CRITICAL, "ERROR: splash Partition invalid\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 mmc_set_lun(partition_get_lun(index));
4437
4438 blocksize = mmc_get_device_blocksize();
4439 if (blocksize == 0) {
4440 dprintf(CRITICAL, "ERROR:splash Partition invalid blocksize\n");
4441 return -1;
4442 }
4443
4444 fb_display = fbcon_display();
Channagoud Kadabidaf10a62015-07-22 11:51:55 -07004445 if (!fb_display)
4446 {
4447 dprintf(CRITICAL, "ERROR: fb config is not allocated\n");
4448 return -1;
4449 }
4450
Mao Flynn1893a7f2015-06-03 12:03:36 +08004451 base = (uint8_t *) fb_display->base;
4452
Kishor PKee5c0a32018-03-06 16:49:46 +05304453 if (mmc_read(ptn + PLL_CODES_OFFSET, (uint32_t *)(base + LOGO_IMG_OFFSET), blocksize)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304454 dprintf(CRITICAL, "ERROR: Cannot read splash image header\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08004455 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304456 }
4457
Mao Flynn1893a7f2015-06-03 12:03:36 +08004458 header = (struct logo_img_header *)(base + LOGO_IMG_OFFSET);
Mao Flynn7b379f32015-04-20 00:28:30 +08004459 if (splash_screen_check_header(header)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304460 dprintf(CRITICAL, "ERROR: Splash image header invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08004461 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304462 }
4463
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304464 if (fb_display) {
Mao Flynn1893a7f2015-06-03 12:03:36 +08004465 if (header->type && (header->blocks != 0)) { /* 1 RLE24 compressed data */
4466 base += LOGO_IMG_OFFSET;
Mao Flynn7b379f32015-04-20 00:28:30 +08004467
Mao Flynn1893a7f2015-06-03 12:03:36 +08004468 realsize = header->blocks * 512;
4469 readsize = ROUNDUP((realsize + LOGO_IMG_HEADER_SIZE), blocksize) - blocksize;
4470
4471 /* if the logo is not full-screen size, clean screen */
Mao Flynn7b379f32015-04-20 00:28:30 +08004472 if ((header->width != fb_display->width)
4473 || (header->height != fb_display->height))
4474 fbcon_clear();
4475
Sachin Bhayare619e9e42017-05-15 13:10:31 +05304476 uint32_t fb_size = ROUNDUP(fb_display->width *
4477 fb_display->height *
4478 (fb_display->bpp / 8), 4096);
4479
4480 if (readsize > fb_size) {
4481 dprintf(CRITICAL, "ERROR: Splash image size invalid\n");
4482 return -1;
4483 }
4484
Kishor PKee5c0a32018-03-06 16:49:46 +05304485 if (mmc_read(ptn + PLL_CODES_OFFSET + blocksize, (uint32_t *)(base + blocksize), readsize)) {
Mao Flynn7b379f32015-04-20 00:28:30 +08004486 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
4487 return -1;
4488 }
Mao Flynn7b379f32015-04-20 00:28:30 +08004489
Mao Flynn1893a7f2015-06-03 12:03:36 +08004490 fbcon_extract_to_screen(header, (base + LOGO_IMG_HEADER_SIZE));
4491 } else { /* 2 Raw BGR data */
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304492
Veera Sundaram Sankaranc33416f2015-09-28 10:25:55 -07004493 if ((header->width > fb_display->width) || (header->height > fb_display->height)) {
4494 dprintf(CRITICAL, "Logo config greater than fb config. Fall back default logo\n");
Mao Flynn1893a7f2015-06-03 12:03:36 +08004495 return -1;
4496 }
4497
4498 realsize = header->width * header->height * fb_display->bpp / 8;
4499 readsize = ROUNDUP((realsize + LOGO_IMG_HEADER_SIZE), blocksize) - blocksize;
4500
4501 if (blocksize == LOGO_IMG_HEADER_SIZE) { /* read the content directly */
Kishor PKee5c0a32018-03-06 16:49:46 +05304502 if (mmc_read((ptn + PLL_CODES_OFFSET + LOGO_IMG_HEADER_SIZE), (uint32_t *)base, readsize)) {
Mao Flynn1893a7f2015-06-03 12:03:36 +08004503 fbcon_clear();
4504 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
4505 return -1;
4506 }
4507 } else {
Kishor PKee5c0a32018-03-06 16:49:46 +05304508 if (mmc_read(ptn + PLL_CODES_OFFSET + blocksize ,
Mao Flynn1893a7f2015-06-03 12:03:36 +08004509 (uint32_t *)(base + LOGO_IMG_OFFSET + blocksize), readsize)) {
4510 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
4511 return -1;
4512 }
4513 memmove(base, (base + LOGO_IMG_OFFSET + LOGO_IMG_HEADER_SIZE), realsize);
4514 }
4515 }
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304516 }
4517
Mao Flynn7b379f32015-04-20 00:28:30 +08004518 return 0;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304519}
4520
Mao Flynn7b379f32015-04-20 00:28:30 +08004521int fetch_image_from_partition()
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304522{
4523 if (target_is_emmc_boot()) {
4524 return splash_screen_mmc();
4525 } else {
4526 return splash_screen_flash();
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07004527 }
4528}
4529
Mayank Grover351a75e2017-05-30 20:06:08 +05304530void publish_getvar_multislot_vars()
4531{
4532 int i,count;
4533 static bool published = false;
4534 static char slot_count[MAX_RSP_SIZE];
4535 static struct ab_slot_info slot_info[AB_SUPPORTED_SLOTS];
4536 static char active_slot_suffix[MAX_RSP_SIZE];
4537 static char has_slot_pname[NUM_PARTITIONS][MAX_GET_VAR_NAME_SIZE];
4538 static char has_slot_reply[NUM_PARTITIONS][MAX_RSP_SIZE];
4539 const char *tmp;
4540 char tmpbuff[MAX_GET_VAR_NAME_SIZE];
Mayank Grover2bd84bb2017-06-20 11:06:07 +05304541 signed active_slt;
Mayank Grover351a75e2017-05-30 20:06:08 +05304542
4543 if (!published)
4544 {
4545 /* Update slot meta info */
4546 count = partition_fill_partition_meta(has_slot_pname, has_slot_reply,
4547 partition_get_partition_count());
4548 for(i=0; i<count; i++)
4549 {
4550 memset(tmpbuff, 0, MAX_GET_VAR_NAME_SIZE);
Mayank Grover2bd84bb2017-06-20 11:06:07 +05304551 snprintf(tmpbuff, MAX_GET_VAR_NAME_SIZE,"has-slot:%s",
4552 has_slot_pname[i]);
4553 strlcpy(has_slot_pname[i], tmpbuff, MAX_GET_VAR_NAME_SIZE);
Mayank Grover351a75e2017-05-30 20:06:08 +05304554 fastboot_publish(has_slot_pname[i], has_slot_reply[i]);
4555 }
4556
4557 for (i=0; i<AB_SUPPORTED_SLOTS; i++)
4558 {
4559 tmp = SUFFIX_SLOT(i);
Mayank Grover7b8a8f82017-10-27 13:07:59 +05304560 tmp++; // to remove "_" from slot_suffix.
Mayank Grover2bd84bb2017-06-20 11:06:07 +05304561 snprintf(slot_info[i].slot_is_unbootable, sizeof(slot_info[i].slot_is_unbootable),
4562 "slot-unbootable:%s", tmp);
4563 snprintf(slot_info[i].slot_is_active, sizeof(slot_info[i].slot_is_active),
4564 "slot-active:%s", tmp);
4565 snprintf(slot_info[i].slot_is_succesful, sizeof(slot_info[i].slot_is_succesful),
4566 "slot-success:%s", tmp);
4567 snprintf(slot_info[i].slot_retry_count, sizeof(slot_info[i].slot_retry_count),
4568 "slot-retry-count:%s", tmp);
Mayank Grover351a75e2017-05-30 20:06:08 +05304569 fastboot_publish(slot_info[i].slot_is_unbootable,
4570 slot_info[i].slot_is_unbootable_rsp);
4571 fastboot_publish(slot_info[i].slot_is_active,
4572 slot_info[i].slot_is_active_rsp);
4573 fastboot_publish(slot_info[i].slot_is_succesful,
4574 slot_info[i].slot_is_succesful_rsp);
4575 fastboot_publish(slot_info[i].slot_retry_count,
4576 slot_info[i].slot_retry_count_rsp);
4577 }
4578 fastboot_publish("current-slot", active_slot_suffix);
4579 snprintf(slot_count, sizeof(slot_count),"%d", AB_SUPPORTED_SLOTS);
4580 fastboot_publish("slot-count", slot_count);
4581 published = true;
4582 }
4583
Mayank Grover2bd84bb2017-06-20 11:06:07 +05304584 active_slt = partition_find_active_slot();
4585 if (active_slt != INVALID)
Mayank Grover7b8a8f82017-10-27 13:07:59 +05304586 {
4587 tmp = SUFFIX_SLOT(active_slt);
4588 tmp++; // to remove "_" from slot_suffix.
4589 snprintf(active_slot_suffix, sizeof(active_slot_suffix), "%s", tmp);
4590 }
Mayank Grover2bd84bb2017-06-20 11:06:07 +05304591 else
4592 strlcpy(active_slot_suffix, "INVALID", sizeof(active_slot_suffix));
4593
Mayank Grover351a75e2017-05-30 20:06:08 +05304594 /* Update partition meta information */
4595 partition_fill_slot_meta(slot_info);
4596 return;
4597}
4598
lijuang4ece1e72015-08-14 21:02:36 +08004599void get_product_name(unsigned char *buf)
4600{
4601 snprintf((char*)buf, MAX_RSP_SIZE, "%s", TARGET(BOARD));
4602 return;
4603}
4604
Mayank Grover7b8a8f82017-10-27 13:07:59 +05304605#if PRODUCT_IOT
4606void get_bootloader_version_iot(unsigned char *buf)
4607{
4608 if (buf != NULL)
4609 {
4610 strlcpy(buf, TARGET(BOARD), MAX_VERSION_LEN);
4611 strlcat(buf, "-", MAX_VERSION_LEN);
4612 strlcat(buf, PRODUCT_IOT_VERSION, MAX_VERSION_LEN);
4613 }
4614 return;
4615}
4616#endif
4617
lijuang4ece1e72015-08-14 21:02:36 +08004618void get_bootloader_version(unsigned char *buf)
4619{
4620 snprintf((char*)buf, MAX_RSP_SIZE, "%s", device.bootloader_version);
4621 return;
4622}
4623
4624void get_baseband_version(unsigned char *buf)
4625{
4626 snprintf((char*)buf, MAX_RSP_SIZE, "%s", device.radio_version);
4627 return;
4628}
4629
Monika Singh32a09f72018-03-14 13:08:29 +05304630bool is_device_locked_critical()
4631{
4632 return device.is_unlock_critical ? false:true;
4633}
4634
lijuang4ece1e72015-08-14 21:02:36 +08004635bool is_device_locked()
4636{
4637 return device.is_unlocked ? false:true;
4638}
4639
Monika Singh32a09f72018-03-14 13:08:29 +05304640bool is_verity_enforcing()
4641{
4642 return device.verity_mode ? true:false;
4643}
4644
Amol Jadi5edf3552013-07-23 14:15:34 -07004645/* register commands and variables for fastboot */
4646void aboot_fastboot_register_commands(void)
4647{
Sridhar Parasurame94e8152014-10-24 14:06:03 -07004648 int i;
lijuangf16461c2015-08-03 17:09:34 +08004649 char hw_platform_buf[MAX_RSP_SIZE];
Amol Jadi5edf3552013-07-23 14:15:34 -07004650
Sridhar Parasurame94e8152014-10-24 14:06:03 -07004651 struct fastboot_cmd_desc cmd_list[] = {
lijuang511a2b52015-08-14 20:50:51 +08004652 /* By default the enabled list is empty. */
4653 {"", NULL},
4654 /* move commands enclosed within the below ifndef to here
4655 * if they need to be enabled in user build.
4656 */
Sridhar Parasurame94e8152014-10-24 14:06:03 -07004657#ifndef DISABLE_FASTBOOT_CMDS
lijuang511a2b52015-08-14 20:50:51 +08004658 /* Register the following commands only for non-user builds */
4659 {"flash:", cmd_flash},
4660 {"erase:", cmd_erase},
4661 {"boot", cmd_boot},
4662 {"continue", cmd_continue},
4663 {"reboot", cmd_reboot},
4664 {"reboot-bootloader", cmd_reboot_bootloader},
4665 {"oem unlock", cmd_oem_unlock},
4666 {"oem unlock-go", cmd_oem_unlock_go},
4667 {"oem lock", cmd_oem_lock},
4668 {"flashing unlock", cmd_oem_unlock},
4669 {"flashing lock", cmd_oem_lock},
4670 {"flashing lock_critical", cmd_flashing_lock_critical},
4671 {"flashing unlock_critical", cmd_flashing_unlock_critical},
4672 {"flashing get_unlock_ability", cmd_flashing_get_unlock_ability},
4673 {"oem device-info", cmd_oem_devinfo},
4674 {"preflash", cmd_preflash},
4675 {"oem enable-charger-screen", cmd_oem_enable_charger_screen},
4676 {"oem disable-charger-screen", cmd_oem_disable_charger_screen},
lijuanga25d8bb2015-09-16 13:11:52 +08004677 {"oem off-mode-charge", cmd_oem_off_mode_charger},
lijuang511a2b52015-08-14 20:50:51 +08004678 {"oem select-display-panel", cmd_oem_select_display_panel},
Mayank Grover6ccc1c92017-07-04 17:36:46 +05304679 {"set_active",cmd_set_active},
Channagoud Kadabi736c4962015-08-21 11:56:52 -07004680#if UNITTEST_FW_SUPPORT
Channagoud Kadabid5ddf482015-09-28 10:31:35 -07004681 {"oem run-tests", cmd_oem_runtests},
Channagoud Kadabi736c4962015-08-21 11:56:52 -07004682#endif
Sridhar Parasurame94e8152014-10-24 14:06:03 -07004683#endif
lijuang511a2b52015-08-14 20:50:51 +08004684 };
Sridhar Parasurame94e8152014-10-24 14:06:03 -07004685
4686 int fastboot_cmds_count = sizeof(cmd_list)/sizeof(cmd_list[0]);
4687 for (i = 1; i < fastboot_cmds_count; i++)
4688 fastboot_register(cmd_list[i].name,cmd_list[i].cb);
4689
Amol Jadi5edf3552013-07-23 14:15:34 -07004690 /* publish variables and their values */
4691 fastboot_publish("product", TARGET(BOARD));
4692 fastboot_publish("kernel", "lk");
4693 fastboot_publish("serialno", sn_buf);
4694
Mayank Grovera64dfbe2018-05-17 14:06:34 +05304695 /*publish hw-revision major(upper 16 bits) and minor(lower 16 bits)*/
4696 snprintf(soc_version_str, MAX_RSP_SIZE, "%x", board_soc_version());
4697 fastboot_publish("hw-revision", soc_version_str);
4698
Amol Jadi5edf3552013-07-23 14:15:34 -07004699 /*
4700 * partition info is supported only for emmc partitions
4701 * Calling this for NAND prints some error messages which
4702 * is harmless but misleading. Avoid calling this for NAND
4703 * devices.
4704 */
4705 if (target_is_emmc_boot())
Mayank Grover7b8a8f82017-10-27 13:07:59 +05304706 publish_getvar_partition_info(part_info, partition_get_partition_count());
Amol Jadi5edf3552013-07-23 14:15:34 -07004707
Mayank Grover351a75e2017-05-30 20:06:08 +05304708 if (partition_multislot_is_supported())
4709 publish_getvar_multislot_vars();
4710
Amol Jadi5edf3552013-07-23 14:15:34 -07004711 /* Max download size supported */
Ameya Thakur11cf1a62013-08-05 12:44:48 -07004712 snprintf(max_download_size, MAX_RSP_SIZE, "\t0x%x",
4713 target_get_max_flash_size());
Amol Jadi5edf3552013-07-23 14:15:34 -07004714 fastboot_publish("max-download-size", (const char *) max_download_size);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07004715 /* Is the charger screen check enabled */
4716 snprintf(charger_screen_enabled, MAX_RSP_SIZE, "%d",
4717 device.charger_screen_enabled);
4718 fastboot_publish("charger-screen-enabled",
4719 (const char *) charger_screen_enabled);
lijuanga25d8bb2015-09-16 13:11:52 +08004720 fastboot_publish("off-mode-charge", (const char *) charger_screen_enabled);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05304721 snprintf(panel_display_mode, MAX_RSP_SIZE, "%s",
4722 device.display_panel);
4723 fastboot_publish("display-panel",
4724 (const char *) panel_display_mode);
Mukesh Ojhabb6e7f32018-03-02 15:04:06 +05304725
4726 if (target_is_emmc_boot())
4727 {
4728 mmc_blocksize = mmc_get_device_blocksize();
4729 }
4730 else
4731 {
4732 mmc_blocksize = flash_block_size();
4733 }
4734 snprintf(block_size_string, MAX_RSP_SIZE, "0x%x", mmc_blocksize);
4735 fastboot_publish("erase-block-size", (const char *) block_size_string);
4736 fastboot_publish("logical-block-size", (const char *) block_size_string);
Mayank Grover7b8a8f82017-10-27 13:07:59 +05304737#if PRODUCT_IOT
4738 get_bootloader_version_iot(&bootloader_version_string);
4739 fastboot_publish("version-bootloader", (const char *) bootloader_version_string);
4740
4741 /* Version baseband is n/a for apq iot devices */
4742 fastboot_publish("version-baseband", "N/A");
Mayank Grover7b8a8f82017-10-27 13:07:59 +05304743#else
Channagoud Kadabiaafbbfc2015-03-04 17:52:56 -08004744 fastboot_publish("version-bootloader", (const char *) device.bootloader_version);
4745 fastboot_publish("version-baseband", (const char *) device.radio_version);
Mayank Grover7b8a8f82017-10-27 13:07:59 +05304746#endif
lijuangf16461c2015-08-03 17:09:34 +08004747 fastboot_publish("secure", is_secure_boot_enable()? "yes":"no");
Mayank Grover7b8a8f82017-10-27 13:07:59 +05304748 fastboot_publish("unlocked", device.is_unlocked ? "yes":"no");
lijuangf16461c2015-08-03 17:09:34 +08004749 smem_get_hw_platform_name((unsigned char *) hw_platform_buf, sizeof(hw_platform_buf));
4750 snprintf(get_variant, MAX_RSP_SIZE, "%s %s", hw_platform_buf,
4751 target_is_emmc_boot()? "eMMC":"UFS");
4752 fastboot_publish("variant", (const char *) get_variant);
lijuang65c5a822015-08-29 16:35:36 +08004753#if CHECK_BAT_VOLTAGE
lijuang102dfa92015-10-09 18:31:03 +08004754 update_battery_status();
lijuang65c5a822015-08-29 16:35:36 +08004755 fastboot_publish("battery-voltage", (const char *) battery_voltage);
lijuang102dfa92015-10-09 18:31:03 +08004756 fastboot_publish("battery-soc-ok", (const char *) battery_soc_ok);
lijuang65c5a822015-08-29 16:35:36 +08004757#endif
Amol Jadi5edf3552013-07-23 14:15:34 -07004758}
4759
Brian Swetland9c4c0752009-01-25 16:23:50 -08004760void aboot_init(const struct app_descriptor *app)
4761{
Shashank Mittal4f99a882010-02-01 13:58:50 -08004762 unsigned reboot_mode = 0;
Mayank Grover351a75e2017-05-30 20:06:08 +05304763 int boot_err_type = 0;
4764 int boot_slot = INVALID;
Chandan Uddarajubedca152010-06-02 23:05:15 -07004765
lijuang39831732016-01-08 17:49:02 +08004766 /* Initialise wdog to catch early lk crashes */
4767#if WDOG_SUPPORT
4768 msm_wdog_init();
4769#endif
4770
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07004771 /* Setup page size information for nv storage */
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004772 if (target_is_emmc_boot())
4773 {
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07004774 page_size = mmc_page_size();
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004775 page_mask = page_size - 1;
Gaurav Nebhwani4d2389c2016-03-17 21:10:05 +05304776 mmc_blocksize = mmc_get_device_blocksize();
4777 mmc_blocksize_mask = mmc_blocksize - 1;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004778 }
4779 else
4780 {
4781 page_size = flash_page_size();
4782 page_mask = page_size - 1;
4783 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07004784 ASSERT((MEMBASE + MEMSIZE) > MEMBASE);
4785
Ameya Thakur11cf1a62013-08-05 12:44:48 -07004786 read_device_info(&device);
vijay kumarc65876c2015-04-24 13:29:16 +05304787 read_allow_oem_unlock(&device);
Shashank Mittal162244e2011-08-08 19:01:25 -07004788
Mayank Grover351a75e2017-05-30 20:06:08 +05304789 /* Detect multi-slot support */
4790 if (partition_multislot_is_supported())
4791 {
4792 boot_slot = partition_find_active_slot();
4793 if (boot_slot == INVALID)
4794 {
4795 boot_into_fastboot = true;
4796 dprintf(INFO, "Active Slot: (INVALID)\n");
4797 }
4798 else
4799 {
4800 /* Setting the state of system to boot active slot */
4801 partition_mark_active_slot(boot_slot);
4802 dprintf(INFO, "Active Slot: (%s)\n", SUFFIX_SLOT(boot_slot));
4803 }
4804 }
4805
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08004806 /* Display splash screen if enabled */
4807#if DISPLAY_SPLASH_SCREEN
lijuang99c02d82015-02-13 19:04:34 +08004808#if NO_ALARM_DISPLAY
4809 if (!check_alarm_boot()) {
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08004810#endif
lijuang99c02d82015-02-13 19:04:34 +08004811 dprintf(SPEW, "Display Init: Start\n");
Ajay Singh Parmara7865a82015-04-16 21:27:52 -07004812#if DISPLAY_HDMI_PRIMARY
4813 if (!strlen(device.display_panel))
4814 strlcpy(device.display_panel, DISPLAY_PANEL_HDMI,
4815 sizeof(device.display_panel));
4816#endif
Channagoud Kadabi90869ce2015-04-27 11:15:14 -07004817#if ENABLE_WBC
Channagoud Kadabid801ac72015-08-26 11:21:51 -07004818 /* Wait if the display shutdown is in progress */
4819 while(pm_app_display_shutdown_in_prgs());
Channagoud Kadabi90869ce2015-04-27 11:15:14 -07004820 if (!pm_appsbl_display_init_done())
4821 target_display_init(device.display_panel);
4822 else
4823 display_image_on_screen();
4824#else
lijuang99c02d82015-02-13 19:04:34 +08004825 target_display_init(device.display_panel);
Channagoud Kadabi90869ce2015-04-27 11:15:14 -07004826#endif
lijuang99c02d82015-02-13 19:04:34 +08004827 dprintf(SPEW, "Display Init: Done\n");
4828#if NO_ALARM_DISPLAY
4829 }
4830#endif
4831#endif
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08004832
Greg Griscod6250552011-06-29 14:40:23 -07004833 target_serialno((unsigned char *) sn_buf);
Ajay Dudanib06c05f2011-05-12 14:46:10 -07004834 dprintf(SPEW,"serial number: %s\n",sn_buf);
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -08004835
Dhaval Patel223ec952013-07-18 14:49:44 -07004836 memset(display_panel_buf, '\0', MAX_PANEL_BUF_SIZE);
4837
Matthew Qindefd5562014-07-11 18:02:40 +08004838 /*
4839 * Check power off reason if user force reset,
4840 * if yes phone will do normal boot.
4841 */
4842 if (is_user_force_reset())
4843 goto normal_boot;
4844
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07004845 /* Check if we should do something other than booting up */
Ameya Thakur0b9c2442013-05-10 13:22:56 -07004846 if (keys_get_state(KEY_VOLUMEUP) && keys_get_state(KEY_VOLUMEDOWN))
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07004847 {
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03004848 dprintf(ALWAYS,"dload mode key sequence detected\n");
lijuang395b5e62015-11-19 17:39:44 +08004849 reboot_device(EMERGENCY_DLOAD);
4850 dprintf(CRITICAL,"Failed to reboot into dload mode\n");
4851
Ameya Thakur0b9c2442013-05-10 13:22:56 -07004852 boot_into_fastboot = true;
4853 }
4854 if (!boot_into_fastboot)
4855 {
4856 if (keys_get_state(KEY_HOME) || keys_get_state(KEY_VOLUMEUP))
4857 boot_into_recovery = 1;
4858 if (!boot_into_recovery &&
4859 (keys_get_state(KEY_BACK) || keys_get_state(KEY_VOLUMEDOWN)))
Pavel Nedev5d91d412013-04-29 11:34:24 +03004860 boot_into_fastboot = true;
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07004861 }
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004862 #if NO_KEYPAD_DRIVER
Kinson Chik0b1c8162011-08-31 16:31:57 -07004863 if (fastboot_trigger())
Pavel Nedev5d91d412013-04-29 11:34:24 +03004864 boot_into_fastboot = true;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004865 #endif
Chandan Uddarajubedca152010-06-02 23:05:15 -07004866
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07004867#if USE_PON_REBOOT_REG
4868 reboot_mode = check_hard_reboot_mode();
4869#else
Ajay Dudani77421292010-10-27 19:34:06 -07004870 reboot_mode = check_reboot_mode();
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07004871#endif
4872 if (reboot_mode == RECOVERY_MODE)
4873 {
Ajay Dudani77421292010-10-27 19:34:06 -07004874 boot_into_recovery = 1;
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07004875 }
4876 else if(reboot_mode == FASTBOOT_MODE)
4877 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03004878 boot_into_fastboot = true;
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07004879 }
4880 else if(reboot_mode == ALARM_BOOT)
4881 {
Matthew Qind886f3c2014-01-17 16:52:01 +08004882 boot_reason_alarm = true;
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07004883 }
Parth Dixit207573a2015-10-27 17:26:21 +05304884#if VERIFIED_BOOT
Mayank Grover912eaa62017-10-26 12:08:53 +05304885 else if (VB_M <= target_get_vb_version())
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07004886 {
Mayank Grover889be1b2017-09-12 20:12:23 +05304887 if (reboot_mode == DM_VERITY_ENFORCING)
4888 {
4889 device.verity_mode = 1;
4890 write_device_info(&device);
4891 }
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07004892#if ENABLE_VB_ATTEST
Mayank Grover889be1b2017-09-12 20:12:23 +05304893 else if (reboot_mode == DM_VERITY_EIO)
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07004894#else
Mayank Grover889be1b2017-09-12 20:12:23 +05304895 else if (reboot_mode == DM_VERITY_LOGGING)
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07004896#endif
Mayank Grover889be1b2017-09-12 20:12:23 +05304897 {
4898 device.verity_mode = 0;
4899 write_device_info(&device);
4900 }
4901 else if (reboot_mode == DM_VERITY_KEYSCLEAR)
4902 {
4903 if(send_delete_keys_to_tz())
4904 ASSERT(0);
4905 }
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07004906 }
Parth Dixitddbc7352015-10-18 03:13:31 +05304907#endif
Ajay Dudani77421292010-10-27 19:34:06 -07004908
Matthew Qindefd5562014-07-11 18:02:40 +08004909normal_boot:
Pavel Nedev5d91d412013-04-29 11:34:24 +03004910 if (!boot_into_fastboot)
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004911 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03004912 if (target_is_emmc_boot())
Shashank Mittala0032282011-08-26 14:50:11 -07004913 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03004914 if(emmc_recovery_init())
4915 dprintf(ALWAYS,"error in emmc_recovery_init\n");
4916 if(target_use_signed_kernel())
Shashank Mittala0032282011-08-26 14:50:11 -07004917 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03004918 if((device.is_unlocked) || (device.is_tampered))
4919 {
4920 #ifdef TZ_TAMPER_FUSE
Monika Singh98257462018-06-06 11:28:49 +05304921 set_tamper_fuse_cmd(HLOS_IMG_TAMPER_FUSE);
Pavel Nedev5d91d412013-04-29 11:34:24 +03004922 #endif
4923 #if USE_PCOM_SECBOOT
4924 set_tamper_flag(device.is_tampered);
4925 #endif
4926 }
Shashank Mittala0032282011-08-26 14:50:11 -07004927 }
Amit Blay6281ebc2015-01-11 14:44:08 +02004928
Mayank Grover351a75e2017-05-30 20:06:08 +05304929retry_boot:
4930 /* Trying to boot active partition */
4931 if (partition_multislot_is_supported())
4932 {
4933 boot_slot = partition_find_boot_slot();
4934 partition_mark_active_slot(boot_slot);
4935 if (boot_slot == INVALID)
4936 goto fastboot;
4937 }
4938
4939 boot_err_type = boot_linux_from_mmc();
4940 switch (boot_err_type)
4941 {
4942 case ERR_INVALID_PAGE_SIZE:
4943 case ERR_DT_PARSE:
4944 case ERR_ABOOT_ADDR_OVERLAP:
Mayank Grover10cabbe2017-10-30 19:11:05 +05304945 case ERR_INVALID_BOOT_MAGIC:
Mayank Grover351a75e2017-05-30 20:06:08 +05304946 if(partition_multislot_is_supported())
Mayank Grover10cabbe2017-10-30 19:11:05 +05304947 {
4948 /*
4949 * Deactivate current slot, as it failed to
4950 * boot, and retry next slot.
4951 */
4952 partition_deactivate_slot(boot_slot);
Mayank Grover351a75e2017-05-30 20:06:08 +05304953 goto retry_boot;
Mayank Grover10cabbe2017-10-30 19:11:05 +05304954 }
Mayank Grover351a75e2017-05-30 20:06:08 +05304955 else
4956 break;
Mayank Grover351a75e2017-05-30 20:06:08 +05304957 default:
4958 break;
4959 /* going to fastboot menu */
4960 }
Shashank Mittala0032282011-08-26 14:50:11 -07004961 }
Pavel Nedev5d91d412013-04-29 11:34:24 +03004962 else
4963 {
4964 recovery_init();
4965 #if USE_PCOM_SECBOOT
4966 if((device.is_unlocked) || (device.is_tampered))
4967 set_tamper_flag(device.is_tampered);
4968 #endif
4969 boot_linux_from_flash();
4970 }
4971 dprintf(CRITICAL, "ERROR: Could not do normal boot. Reverting "
4972 "to fastboot mode.\n");
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004973 }
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07004974
Mayank Grover351a75e2017-05-30 20:06:08 +05304975fastboot:
Amol Jadi5edf3552013-07-23 14:15:34 -07004976 /* We are here means regular boot did not happen. Start fastboot. */
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07004977
Amol Jadi5edf3552013-07-23 14:15:34 -07004978 /* register aboot specific fastboot commands */
4979 aboot_fastboot_register_commands();
Amol Jadi57abe4c2011-05-24 15:47:27 -07004980
Amol Jadi5edf3552013-07-23 14:15:34 -07004981 /* dump partition table for debug info */
Kinson Chikf1a43512011-07-14 11:28:39 -07004982 partition_dump();
Amol Jadi5edf3552013-07-23 14:15:34 -07004983
4984 /* initialize and start fastboot */
4985 fastboot_init(target_get_scratch_address(), target_get_max_flash_size());
lijuang4ece1e72015-08-14 21:02:36 +08004986#if FBCON_DISPLAY_MSG
lijuang9a7d3b92015-11-30 14:41:24 +08004987 display_fastboot_menu();
lijuang4ece1e72015-08-14 21:02:36 +08004988#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08004989}
4990
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07004991uint32_t get_page_size()
4992{
4993 return page_size;
4994}
4995
Amir Samuelov57a6fa22013-06-05 16:36:43 +03004996/*
4997 * Calculated and save hash (SHA256) for non-signed boot image.
4998 *
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07004999 * @param image_addr - Boot image address
5000 * @param image_size - Size of the boot image
Amir Samuelov57a6fa22013-06-05 16:36:43 +03005001 *
5002 * @return int - 0 on success, negative value on failure.
5003 */
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07005004static int aboot_save_boot_hash_mmc(uint32_t image_addr, uint32_t image_size)
Amir Samuelov57a6fa22013-06-05 16:36:43 +03005005{
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07005006 unsigned int digest[8];
5007#if IMAGE_VERIF_ALGO_SHA1
5008 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA1;
5009#else
5010 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA256;
5011#endif
Amir Samuelov57a6fa22013-06-05 16:36:43 +03005012
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07005013 target_crypto_init_params();
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -08005014 hash_find((unsigned char *) image_addr, image_size, (unsigned char *)&digest, auth_algo);
Amir Samuelov57a6fa22013-06-05 16:36:43 +03005015
5016 save_kernel_hash_cmd(digest);
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07005017 dprintf(INFO, "aboot_save_boot_hash_mmc: imagesize_actual size %d bytes.\n", (int) image_size);
Amir Samuelov57a6fa22013-06-05 16:36:43 +03005018
5019 return 0;
5020}
5021
Mayank Grover351a75e2017-05-30 20:06:08 +05305022
Brian Swetland9c4c0752009-01-25 16:23:50 -08005023APP_START(aboot)
5024 .init = aboot_init,
5025APP_END