blob: e194f857010e292daeaf39d0e8f433bd97c1591a [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
Mayank Groverdd080e82018-09-04 20:12:13 +05301178int getimage(void **image_buffer, uint32_t *imgsize,
1179 const char *imgname)
Monika Singh292b3e92018-03-17 22:40:23 +05301180{
Mayank Groverdd080e82018-09-04 20:12:13 +05301181 uint32_t loadedindex;
1182 if (image_buffer == NULL || imgsize == NULL ||
Monika Singh292b3e92018-03-17 22:40:23 +05301183 imgname == NULL) {
1184 dprintf(CRITICAL, "getimage: invalid parameters\n");
1185 return -1;
1186 }
Mayank Groverdd080e82018-09-04 20:12:13 +05301187 for (loadedindex = 0; loadedindex < info.num_loaded_images; loadedindex++) {
1188 if (!strncmp(info.images[loadedindex].name, imgname,
Monika Singh292b3e92018-03-17 22:40:23 +05301189 strlen(imgname))) {
Mayank Groverdd080e82018-09-04 20:12:13 +05301190 *image_buffer = info.images[loadedindex].image_buffer;
1191 *imgsize = info.images[loadedindex].imgsize;
Monika Singh292b3e92018-03-17 22:40:23 +05301192 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
Maunik Shah0f3c8ac2014-03-02 17:47:58 +05301312static bool check_format_bit()
1313{
1314 bool ret = false;
1315 int index;
1316 uint64_t offset;
1317 struct boot_selection_info *in = NULL;
1318 char *buf = NULL;
1319
1320 index = partition_get_index("bootselect");
1321 if (index == INVALID_PTN)
1322 {
1323 dprintf(INFO, "Unable to locate /bootselect partition\n");
1324 return ret;
1325 }
1326 offset = partition_get_offset(index);
1327 if(!offset)
1328 {
1329 dprintf(INFO, "partition /bootselect doesn't exist\n");
1330 return ret;
1331 }
1332 buf = (char *) memalign(CACHE_LINE, ROUNDUP(page_size, CACHE_LINE));
Mayank Grover48860402016-11-29 12:34:53 +05301333 mmc_set_lun(partition_get_lun(index));
Maunik Shah0f3c8ac2014-03-02 17:47:58 +05301334 ASSERT(buf);
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301335 if (mmc_read(offset, (uint32_t *)buf, page_size))
Maunik Shah0f3c8ac2014-03-02 17:47:58 +05301336 {
1337 dprintf(INFO, "mmc read failure /bootselect %d\n", page_size);
1338 free(buf);
1339 return ret;
1340 }
1341 in = (struct boot_selection_info *) buf;
1342 if ((in->signature == BOOTSELECT_SIGNATURE) &&
1343 (in->version == BOOTSELECT_VERSION)) {
1344 if ((in->state_info & BOOTSELECT_FORMAT) &&
1345 !(in->state_info & BOOTSELECT_FACTORY))
1346 ret = true;
1347 } else {
1348 dprintf(CRITICAL, "Signature: 0x%08x or version: 0x%08x mismatched of /bootselect\n",
1349 in->signature, in->version);
1350 ASSERT(0);
1351 }
1352 free(buf);
1353 return ret;
1354}
1355
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001356void boot_verifier_init()
1357{
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001358 uint32_t boot_state;
1359 /* Check if device unlock */
1360 if(device.is_unlocked)
1361 {
1362 boot_verify_send_event(DEV_UNLOCK);
1363 boot_verify_print_state();
1364 dprintf(CRITICAL, "Device is unlocked! Skipping verification...\n");
1365 return;
1366 }
1367 else
1368 {
1369 boot_verify_send_event(BOOT_INIT);
1370 }
1371
1372 /* Initialize keystore */
1373 boot_state = boot_verify_keystore_init();
1374 if(boot_state == YELLOW)
1375 {
1376 boot_verify_print_state();
1377 dprintf(CRITICAL, "Keystore verification failed! Continuing anyways...\n");
1378 }
1379}
1380
Shashank Mittal23b8f422010-04-16 19:27:21 -07001381int boot_linux_from_mmc(void)
1382{
1383 struct boot_img_hdr *hdr = (void*) buf;
1384 struct boot_img_hdr *uhdr;
1385 unsigned offset = 0;
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07001386 int rcode;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001387 unsigned long long ptn = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001388 int index = INVALID_PTN;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001389
Shashank Mittalcd98d472011-08-02 14:29:24 -07001390 unsigned char *image_addr = 0;
1391 unsigned kernel_actual;
1392 unsigned ramdisk_actual;
1393 unsigned imagesize_actual;
Neeti Desai465491e2012-07-31 12:53:35 -07001394 unsigned second_actual = 0;
Neeti Desai465491e2012-07-31 12:53:35 -07001395
Matthew Qin271927e2015-03-31 22:07:07 -04001396 unsigned int dtb_size = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08001397 unsigned int out_len = 0;
1398 unsigned int out_avai_len = 0;
1399 unsigned char *out_addr = NULL;
1400 uint32_t dtb_offset = 0;
1401 unsigned char *kernel_start_addr = NULL;
1402 unsigned int kernel_size = 0;
Ameya Thakur10a33452016-06-13 14:24:26 -07001403 unsigned int patched_kernel_hdr_size = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08001404 int rc;
Monika Singh292b3e92018-03-17 22:40:23 +05301405#if VERIFIED_BOOT_2
1406 int status;
1407#endif
Mayank Groverc93cad82017-10-03 12:23:45 +05301408 char *ptn_name = NULL;
Neeti Desai465491e2012-07-31 12:53:35 -07001409#if DEVICE_TREE
1410 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -07001411 struct dt_entry dt_entry;
Neeti Desai465491e2012-07-31 12:53:35 -07001412 unsigned dt_table_offset;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001413 uint32_t dt_actual;
Deepa Dinamani19648b42013-09-05 17:05:55 -07001414 uint32_t dt_hdr_size;
Matthew Qin271927e2015-03-31 22:07:07 -04001415 unsigned char *best_match_dt_addr = NULL;
Neeti Desai465491e2012-07-31 12:53:35 -07001416#endif
Matthew Qin49e51fa2015-02-09 10:40:45 +08001417 struct kernel64_hdr *kptr = NULL;
Mayank Grover351a75e2017-05-30 20:06:08 +05301418 int current_active_slot = INVALID;
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001419
Maunik Shah0f3c8ac2014-03-02 17:47:58 +05301420 if (check_format_bit())
1421 boot_into_recovery = 1;
1422
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07001423 if (!boot_into_recovery) {
1424 memset(ffbm_mode_string, '\0', sizeof(ffbm_mode_string));
1425 rcode = get_ffbm(ffbm_mode_string, sizeof(ffbm_mode_string));
1426 if (rcode <= 0) {
1427 boot_into_ffbm = false;
1428 if (rcode < 0)
1429 dprintf(CRITICAL,"failed to get ffbm cookie");
1430 } else
1431 boot_into_ffbm = true;
1432 } else
1433 boot_into_ffbm = false;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001434 uhdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
1435 if (!memcmp(uhdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1436 dprintf(INFO, "Unified boot method!\n");
1437 hdr = uhdr;
1438 goto unified_boot;
1439 }
Mayank Groverc93cad82017-10-03 12:23:45 +05301440
1441 /* For a/b recovery image code is on boot partition.
1442 If we support multislot, always use boot partition. */
1443 if (boot_into_recovery &&
1444 (!partition_multislot_is_supported()))
1445 ptn_name = "recovery";
1446 else
1447 ptn_name = "boot";
1448
1449 index = partition_get_index(ptn_name);
1450 ptn = partition_get_offset(index);
1451 if(ptn == 0) {
1452 dprintf(CRITICAL, "ERROR: No %s partition found\n", ptn_name);
1453 return -1;
Kinson Chikf1a43512011-07-14 11:28:39 -07001454 }
Mayank Groverc93cad82017-10-03 12:23:45 +05301455
Channagoud Kadabief0547c2015-02-10 12:57:38 -08001456 /* Set Lun for boot & recovery partitions */
1457 mmc_set_lun(partition_get_lun(index));
Shashank Mittal23b8f422010-04-16 19:27:21 -07001458
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301459 if (mmc_read(ptn + offset, (uint32_t *) buf, page_size)) {
Shashank Mittal23b8f422010-04-16 19:27:21 -07001460 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
1461 return -1;
1462 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001463
1464 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001465 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Mayank Grover351a75e2017-05-30 20:06:08 +05301466 return ERR_INVALID_BOOT_MAGIC;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001467 }
1468
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07001469 if (hdr->page_size && (hdr->page_size != page_size)) {
vijay kumar2e21b3a2014-06-26 17:40:15 +05301470
1471 if (hdr->page_size > BOOT_IMG_MAX_PAGE_SIZE) {
1472 dprintf(CRITICAL, "ERROR: Invalid page size\n");
1473 return -1;
1474 }
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07001475 page_size = hdr->page_size;
1476 page_mask = page_size - 1;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001477 }
1478
Matthew Qin49e51fa2015-02-09 10:40:45 +08001479 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1480 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Kishor PKd8ddcad2017-07-27 13:53:57 +05301481 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001482
Matthew Qin49e51fa2015-02-09 10:40:45 +08001483 image_addr = (unsigned char *)target_get_scratch_address();
Kishor PK9e91b592017-05-24 12:14:55 +05301484 memcpy(image_addr, (void *)buf, page_size);
1485
1486 /* ensure commandline is terminated */
1487 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
Matthew Qin49e51fa2015-02-09 10:40:45 +08001488
1489#if DEVICE_TREE
Parth Dixit4097b622016-03-15 14:42:27 +05301490#ifndef OSVERSION_IN_BOOTIMAGE
1491 dt_size = hdr->dt_size;
1492#endif
1493 dt_actual = ROUND_TO_PAGE(dt_size, page_mask);
Kishor PKd8ddcad2017-07-27 13:53:57 +05301494 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 +05301495 dprintf(CRITICAL, "Integer overflow detected in bootimage header fields at %u in %s\n",__LINE__,__FILE__);
1496 return -1;
1497 }
Kishor PKd8ddcad2017-07-27 13:53:57 +05301498 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + second_actual + dt_actual);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001499#else
Kishor PKd8ddcad2017-07-27 13:53:57 +05301500 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 +05301501 dprintf(CRITICAL, "Integer overflow detected in bootimage header fields at %u in %s\n",__LINE__,__FILE__);
1502 return -1;
1503 }
Kishor PKd8ddcad2017-07-27 13:53:57 +05301504 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + second_actual);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001505#endif
1506
1507#if VERIFIED_BOOT
1508 boot_verifier_init();
1509#endif
1510
Vijay Kumar Pendoti9c002ad2016-03-09 13:52:45 +05301511 if (check_aboot_addr_range_overlap((uintptr_t) image_addr, imagesize_actual))
Matthew Qin49e51fa2015-02-09 10:40:45 +08001512 {
1513 dprintf(CRITICAL, "Boot image buffer address overlaps with aboot addresses.\n");
1514 return -1;
1515 }
1516
Matthew Qinbb7923d2015-02-09 10:56:09 +08001517 /*
1518 * Update loading flow of bootimage to support compressed/uncompressed
1519 * bootimage on both 64bit and 32bit platform.
1520 * 1. Load bootimage from emmc partition onto DDR.
1521 * 2. Check if bootimage is gzip format. If yes, decompress compressed kernel
1522 * 3. Check kernel header and update kernel load addr for 64bit and 32bit
1523 * platform accordingly.
1524 * 4. Sanity Check on kernel_addr and ramdisk_addr and copy data.
1525 */
Mayank Grover351a75e2017-05-30 20:06:08 +05301526 if (partition_multislot_is_supported())
1527 {
1528 current_active_slot = partition_find_active_slot();
1529 dprintf(INFO, "Loading boot image (%d) active_slot(%s): start\n",
1530 imagesize_actual, SUFFIX_SLOT(current_active_slot));
1531 }
1532 else
1533 {
1534 dprintf(INFO, "Loading (%s) image (%d): start\n",
1535 (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual);
1536 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08001537 bs_set_timestamp(BS_KERNEL_LOAD_START);
1538
Gaurav Nebhwani43e2a462016-03-17 21:32:56 +05301539 if ((target_get_max_flash_size() - page_size) < imagesize_actual)
1540 {
1541 dprintf(CRITICAL, "booimage size is greater than DDR can hold\n");
1542 return -1;
1543 }
Kishor PK9e91b592017-05-24 12:14:55 +05301544 offset = page_size;
1545 /* Read image without signature and header*/
1546 if (mmc_read(ptn + offset, (void *)(image_addr + offset), imagesize_actual - page_size))
Matthew Qin49e51fa2015-02-09 10:40:45 +08001547 {
1548 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
1549 return -1;
1550 }
1551
Mayank Grover351a75e2017-05-30 20:06:08 +05301552 if (partition_multislot_is_supported())
1553 {
1554 dprintf(INFO, "Loading boot image (%d) active_slot(%s): done\n",
1555 imagesize_actual, SUFFIX_SLOT(current_active_slot));
1556 }
1557 else
1558 {
1559 dprintf(INFO, "Loading (%s) image (%d): done\n",
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001560 (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual);
1561
Mayank Grover351a75e2017-05-30 20:06:08 +05301562 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08001563 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
1564
1565 /* Authenticate Kernel */
1566 dprintf(INFO, "use_signed_kernel=%d, is_unlocked=%d, is_tampered=%d.\n",
1567 (int) target_use_signed_kernel(),
1568 device.is_unlocked,
1569 device.is_tampered);
Monika Singh292b3e92018-03-17 22:40:23 +05301570#if VERIFIED_BOOT_2
1571 offset = imagesize_actual;
1572 if (check_aboot_addr_range_overlap((uintptr_t)image_addr + offset, page_size))
1573 {
1574 dprintf(CRITICAL, "Signature read buffer address overlaps with aboot addresses.\n");
1575 return -1;
1576 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08001577
Monika Singh292b3e92018-03-17 22:40:23 +05301578 /* Read signature */
1579 if(mmc_read(ptn + offset, (void *)(image_addr + offset), page_size))
1580 {
1581 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
1582 return -1;
1583 }
1584
1585 memset(&info, 0, sizeof(bootinfo));
1586 info.images[0].image_buffer = image_addr;
1587 info.images[0].imgsize = imagesize_actual;
1588 info.images[0].name = "boot";
1589 info.num_loaded_images = 0;
1590 info.multi_slot_boot = partition_multislot_is_supported();
1591 info.bootreason_alarm = boot_reason_alarm;
1592 info.bootinto_recovery = boot_into_recovery;
1593 status = load_image_and_auth(&info);
1594 if(status)
1595 return -1;
1596
1597 vbcmdline = info.vbcmdline;
1598#else
Channagoud Kadabi736c4962015-08-21 11:56:52 -07001599 /* Change the condition a little bit to include the test framework support.
1600 * We would never reach this point if device is in fastboot mode, even if we did
1601 * that means we are in test mode, so execute kernel authentication part for the
1602 * tests */
Parth Dixitcb0c6082015-12-30 15:01:13 +05301603 if((target_use_signed_kernel() && (!device.is_unlocked)) || is_test_mode_enabled())
Matthew Qin49e51fa2015-02-09 10:40:45 +08001604 {
1605 offset = imagesize_actual;
Vijay Kumar Pendoti9c002ad2016-03-09 13:52:45 +05301606 if (check_aboot_addr_range_overlap((uintptr_t)image_addr + offset, page_size))
Matthew Qin49e51fa2015-02-09 10:40:45 +08001607 {
1608 dprintf(CRITICAL, "Signature read buffer address overlaps with aboot addresses.\n");
1609 return -1;
1610 }
1611
1612 /* Read signature */
1613 if(mmc_read(ptn + offset, (void *)(image_addr + offset), page_size))
1614 {
1615 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
1616 return -1;
1617 }
1618
1619 verify_signed_bootimg((uint32_t)image_addr, imagesize_actual);
Channagoud Kadabi736c4962015-08-21 11:56:52 -07001620 /* The purpose of our test is done here */
Parth Dixitcb0c6082015-12-30 15:01:13 +05301621 if(is_test_mode_enabled() && auth_kernel_img)
Channagoud Kadabi736c4962015-08-21 11:56:52 -07001622 return 0;
Matthew Qin49e51fa2015-02-09 10:40:45 +08001623 } else {
1624 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
1625 #ifdef TZ_SAVE_KERNEL_HASH
1626 aboot_save_boot_hash_mmc((uint32_t) image_addr, imagesize_actual);
1627 #endif /* TZ_SAVE_KERNEL_HASH */
Amit Blay4aa292f2015-04-28 21:55:59 +03001628
1629#ifdef MDTP_SUPPORT
1630 {
1631 /* Verify MDTP lock.
1632 * For boot & recovery partitions, MDTP will use boot_verifier APIs,
1633 * since verification was skipped in aboot. The signature is not part of the loaded image.
1634 */
1635 mdtp_ext_partition_verification_t ext_partition;
1636 ext_partition.partition = boot_into_recovery ? MDTP_PARTITION_RECOVERY : MDTP_PARTITION_BOOT;
1637 ext_partition.integrity_state = MDTP_PARTITION_STATE_UNSET;
1638 ext_partition.page_size = page_size;
1639 ext_partition.image_addr = (uint32)image_addr;
1640 ext_partition.image_size = imagesize_actual;
1641 ext_partition.sig_avail = FALSE;
1642 mdtp_fwlock_verify_lock(&ext_partition);
1643 }
1644#endif /* MDTP_SUPPORT */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001645 }
Monika Singh292b3e92018-03-17 22:40:23 +05301646#endif
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001647
Sridhar Parasuram00bfedb2015-05-26 14:21:27 -07001648#if VERIFIED_BOOT
taozhang93088bd2016-11-16 15:50:46 +08001649 if((boot_verify_get_state() == ORANGE) && (!boot_into_ffbm))
Reut Zysmanba8a9d52016-02-18 11:44:04 +02001650 {
1651#if FBCON_DISPLAY_MSG
1652 display_bootverify_menu(DISPLAY_MENU_ORANGE);
1653 wait_for_users_action();
1654#else
1655 dprintf(CRITICAL,
1656 "Your device has been unlocked and can't be trusted.\nWait for 5 seconds before proceeding\n");
1657 mdelay(5000);
1658#endif
1659 }
1660#endif
1661
1662#if VERIFIED_BOOT
Monika Singh292b3e92018-03-17 22:40:23 +05301663 if (VB_M == target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +05301664 {
1665 /* set boot and system versions. */
1666 set_os_version((unsigned char *)image_addr);
1667 // send root of trust
1668 if(!send_rot_command((uint32_t)device.is_unlocked))
1669 ASSERT(0);
1670 }
Parth Dixitbc9b6492015-10-18 00:41:38 +05301671#endif
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001672 /*
Matthew Qinbb7923d2015-02-09 10:56:09 +08001673 * Check if the kernel image is a gzip package. If yes, need to decompress it.
1674 * If not, continue booting.
1675 */
1676 if (is_gzip_package((unsigned char *)(image_addr + page_size), hdr->kernel_size))
1677 {
1678 out_addr = (unsigned char *)(image_addr + imagesize_actual + page_size);
1679 out_avai_len = target_get_max_flash_size() - imagesize_actual - page_size;
Matthew Qin0b15b322015-05-19 05:20:54 -04001680 dprintf(INFO, "decompressing kernel image: start\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001681 rc = decompress((unsigned char *)(image_addr + page_size),
1682 hdr->kernel_size, out_addr, out_avai_len,
1683 &dtb_offset, &out_len);
1684 if (rc)
1685 {
Matthew Qin0b15b322015-05-19 05:20:54 -04001686 dprintf(CRITICAL, "decompressing kernel image failed!!!\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001687 ASSERT(0);
1688 }
1689
Matthew Qin0b15b322015-05-19 05:20:54 -04001690 dprintf(INFO, "decompressing kernel image: done\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001691 kptr = (struct kernel64_hdr *)out_addr;
1692 kernel_start_addr = out_addr;
1693 kernel_size = out_len;
1694 } else {
Ameya Thakur10a33452016-06-13 14:24:26 -07001695 dprintf(INFO, "Uncpmpressed kernel in use\n");
1696 if (!strncmp((char*)(image_addr + page_size),
1697 PATCHED_KERNEL_MAGIC,
1698 sizeof(PATCHED_KERNEL_MAGIC) - 1)) {
1699 dprintf(INFO, "Patched kernel detected\n");
1700 kptr = (struct kernel64_hdr *)(image_addr + page_size +
1701 PATCHED_KERNEL_HEADER_SIZE);
1702 //The size of the kernel is stored at start of kernel image + 16
1703 //The dtb would start just after the kernel
1704 dtb_offset = *((uint32_t*)((unsigned char*)
1705 (image_addr + page_size +
1706 sizeof(PATCHED_KERNEL_MAGIC) -
1707 1)));
1708 //The actual kernel starts after the 20 byte header.
1709 kernel_start_addr = (unsigned char*)(image_addr +
1710 page_size + PATCHED_KERNEL_HEADER_SIZE);
1711 kernel_size = hdr->kernel_size;
1712 patched_kernel_hdr_size = PATCHED_KERNEL_HEADER_SIZE;
1713 } else {
1714 dprintf(INFO, "Kernel image not patched..Unable to locate dt offset\n");
1715 kptr = (struct kernel64_hdr *)(image_addr + page_size);
1716 kernel_start_addr = (unsigned char *)(image_addr + page_size);
1717 kernel_size = hdr->kernel_size;
1718 }
Matthew Qinbb7923d2015-02-09 10:56:09 +08001719 }
1720
1721 /*
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001722 * Update the kernel/ramdisk/tags address if the boot image header
1723 * has default values, these default values come from mkbootimg when
1724 * the boot image is flashed using fastboot flash:raw
1725 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001726 update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr));
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001727
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001728 /* Get virtual addresses since the hdr saves physical addresses. */
1729 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
1730 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
1731 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001732
Matthew Qinbb7923d2015-02-09 10:56:09 +08001733 kernel_size = ROUND_TO_PAGE(kernel_size, page_mask);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001734 /* Check if the addresses in the header are valid. */
Matthew Qinbb7923d2015-02-09 10:56:09 +08001735 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_size) ||
Mayank Grovere559cec2017-10-17 15:12:03 +05301736 check_ddr_addr_range_bound(hdr->kernel_addr, kernel_size) ||
1737 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual) ||
1738 check_ddr_addr_range_bound(hdr->ramdisk_addr, ramdisk_actual))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001739 {
Mayank Grovere559cec2017-10-17 15:12:03 +05301740 dprintf(CRITICAL, "kernel/ramdisk addresses are not valid.\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001741 return -1;
1742 }
1743
1744#ifndef DEVICE_TREE
Mayank Grovere559cec2017-10-17 15:12:03 +05301745 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE) ||
1746 check_ddr_addr_range_bound(hdr->tags_addr, MAX_TAGS_SIZE))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001747 {
Mayank Grovere559cec2017-10-17 15:12:03 +05301748 dprintf(CRITICAL, "Tags addresses are not valid.\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001749 return -1;
1750 }
1751#endif
1752
Matthew Qin49e51fa2015-02-09 10:40:45 +08001753 /* Move kernel, ramdisk and device tree to correct address */
Matthew Qinbb7923d2015-02-09 10:56:09 +08001754 memmove((void*) hdr->kernel_addr, kernel_start_addr, kernel_size);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001755 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001756
Matthew Qin49e51fa2015-02-09 10:40:45 +08001757 #if DEVICE_TREE
Parth Dixit4097b622016-03-15 14:42:27 +05301758 if(dt_size) {
Matthew Qin49e51fa2015-02-09 10:40:45 +08001759 dt_table_offset = ((uint32_t)image_addr + page_size + kernel_actual + ramdisk_actual + second_actual);
1760 table = (struct dt_table*) dt_table_offset;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001761
Matthew Qin49e51fa2015-02-09 10:40:45 +08001762 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
1763 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1764 return -1;
1765 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001766
P.V. Phani Kumar82916762016-03-09 09:20:19 +05301767 /* Its Error if, dt_hdr_size (table->num_entries * dt_entry size + Dev_Tree Header)
1768 goes beyound hdr->dt_size*/
Parth Dixit4097b622016-03-15 14:42:27 +05301769 if (dt_hdr_size > ROUND_TO_PAGE(dt_size,hdr->page_size)) {
P.V. Phani Kumar82916762016-03-09 09:20:19 +05301770 dprintf(CRITICAL, "ERROR: Invalid Device Tree size \n");
1771 return -1;
1772 }
1773
Matthew Qin49e51fa2015-02-09 10:40:45 +08001774 /* Find index of device tree within device tree table */
1775 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
1776 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1777 return -1;
1778 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001779
P.V. Phani Kumar3c333202016-03-09 11:54:37 +05301780 if(dt_entry.offset > (UINT_MAX - dt_entry.size)) {
1781 dprintf(CRITICAL, "ERROR: Device tree contents are Invalid\n");
1782 return -1;
1783 }
1784
1785 /* Ensure we are not overshooting dt_size with the dt_entry selected */
Parth Dixit4097b622016-03-15 14:42:27 +05301786 if ((dt_entry.offset + dt_entry.size) > dt_size) {
P.V. Phani Kumar3c333202016-03-09 11:54:37 +05301787 dprintf(CRITICAL, "ERROR: Device tree contents are Invalid\n");
1788 return -1;
1789 }
1790
Matthew Qin271927e2015-03-31 22:07:07 -04001791 if (is_gzip_package((unsigned char *)dt_table_offset + dt_entry.offset, dt_entry.size))
1792 {
1793 unsigned int compressed_size = 0;
1794 out_addr += out_len;
1795 out_avai_len -= out_len;
Matthew Qin0b15b322015-05-19 05:20:54 -04001796 dprintf(INFO, "decompressing dtb: start\n");
Matthew Qin271927e2015-03-31 22:07:07 -04001797 rc = decompress((unsigned char *)dt_table_offset + dt_entry.offset,
1798 dt_entry.size, out_addr, out_avai_len,
1799 &compressed_size, &dtb_size);
1800 if (rc)
1801 {
Matthew Qin0b15b322015-05-19 05:20:54 -04001802 dprintf(CRITICAL, "decompressing dtb failed!!!\n");
Matthew Qin271927e2015-03-31 22:07:07 -04001803 ASSERT(0);
1804 }
1805
Matthew Qin0b15b322015-05-19 05:20:54 -04001806 dprintf(INFO, "decompressing dtb: done\n");
Matthew Qin271927e2015-03-31 22:07:07 -04001807 best_match_dt_addr = out_addr;
1808 } else {
1809 best_match_dt_addr = (unsigned char *)dt_table_offset + dt_entry.offset;
1810 dtb_size = dt_entry.size;
1811 }
1812
Matthew Qin49e51fa2015-02-09 10:40:45 +08001813 /* Validate and Read device device tree in the tags_addr */
Mayank Grovere559cec2017-10-17 15:12:03 +05301814 if (check_aboot_addr_range_overlap(hdr->tags_addr, dtb_size) ||
1815 check_ddr_addr_range_bound(hdr->tags_addr, dtb_size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001816 {
Mayank Grovere559cec2017-10-17 15:12:03 +05301817 dprintf(CRITICAL, "Device tree addresses are not valid\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001818 return -1;
1819 }
Shashank Mittal162244e2011-08-08 19:01:25 -07001820
Matthew Qin271927e2015-03-31 22:07:07 -04001821 memmove((void *)hdr->tags_addr, (char *)best_match_dt_addr, dtb_size);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001822 } else {
1823 /* Validate the tags_addr */
Mayank Grovere559cec2017-10-17 15:12:03 +05301824 if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual) ||
1825 check_ddr_addr_range_bound(hdr->tags_addr, kernel_actual))
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001826 {
Mayank Grovere559cec2017-10-17 15:12:03 +05301827 dprintf(CRITICAL, "Device tree addresses are not valid.\n");
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001828 return -1;
1829 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08001830 /*
1831 * If appended dev tree is found, update the atags with
1832 * memory address to the DTB appended location on RAM.
1833 * Else update with the atags address in the kernel header
1834 */
1835 void *dtb;
Ameya Thakur10a33452016-06-13 14:24:26 -07001836 dtb = dev_tree_appended(
1837 (void*)(image_addr + page_size +
1838 patched_kernel_hdr_size),
1839 hdr->kernel_size, dtb_offset,
1840 (void *)hdr->tags_addr);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001841 if (!dtb) {
1842 dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001843 return -1;
1844 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07001845 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08001846 #endif
Shashank Mittal23b8f422010-04-16 19:27:21 -07001847
Stanimir Varbanov69ec5462013-07-18 18:17:42 +03001848 if (boot_into_recovery && !device.is_unlocked && !device.is_tampered)
1849 target_load_ssd_keystore();
1850
Shashank Mittal23b8f422010-04-16 19:27:21 -07001851unified_boot:
Shashank Mittal23b8f422010-04-16 19:27:21 -07001852
Dima Zavin77e41f32013-03-06 16:10:43 -08001853 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07001854 (const char *)hdr->cmdline, board_machtype(),
Shashank Mittal23b8f422010-04-16 19:27:21 -07001855 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
1856
1857 return 0;
1858}
1859
Dima Zavin214cc642009-01-26 11:16:21 -08001860int boot_linux_from_flash(void)
1861{
1862 struct boot_img_hdr *hdr = (void*) buf;
Dima Zavin214cc642009-01-26 11:16:21 -08001863 struct ptentry *ptn;
1864 struct ptable *ptable;
1865 unsigned offset = 0;
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001866
Shashank Mittalcd98d472011-08-02 14:29:24 -07001867 unsigned char *image_addr = 0;
1868 unsigned kernel_actual;
1869 unsigned ramdisk_actual;
1870 unsigned imagesize_actual;
vijay kumar8f53e362015-11-24 13:38:11 +05301871 unsigned second_actual = 0;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001872
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001873#if DEVICE_TREE
Monika Singh292b3e92018-03-17 22:40:23 +05301874 struct dt_table *table = NULL;
Joel Kingaa335dc2013-06-03 16:11:08 -07001875 struct dt_entry dt_entry;
vijay kumar8f53e362015-11-24 13:38:11 +05301876 unsigned dt_table_offset;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001877 uint32_t dt_actual;
Monika Singh292b3e92018-03-17 22:40:23 +05301878 uint32_t dt_hdr_size = 0;
Vivek Kumar07bd0862018-04-10 14:19:23 +05301879 uint32_t dtb_offset = 0;
vijay kumar8f53e362015-11-24 13:38:11 +05301880 unsigned int dtb_size = 0;
1881 unsigned char *best_match_dt_addr = NULL;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001882#endif
1883
David Ng183a7422009-12-07 14:55:21 -08001884 if (target_is_emmc_boot()) {
1885 hdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
1886 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1887 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
1888 return -1;
1889 }
1890 goto continue_boot;
1891 }
1892
Dima Zavin214cc642009-01-26 11:16:21 -08001893 ptable = flash_get_ptable();
1894 if (ptable == NULL) {
1895 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1896 return -1;
1897 }
1898
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001899 if(!boot_into_recovery)
1900 {
1901 ptn = ptable_find(ptable, "boot");
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001902
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001903 if (ptn == NULL) {
1904 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1905 return -1;
1906 }
1907 }
1908 else
1909 {
1910 ptn = ptable_find(ptable, "recovery");
1911 if (ptn == NULL) {
1912 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
1913 return -1;
1914 }
Dima Zavin214cc642009-01-26 11:16:21 -08001915 }
1916
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05301917 /* Read boot.img header from flash */
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001918 if (flash_read(ptn, offset, buf, page_size)) {
Dima Zavin214cc642009-01-26 11:16:21 -08001919 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
1920 return -1;
1921 }
Dima Zavin214cc642009-01-26 11:16:21 -08001922
1923 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001924 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Dima Zavin214cc642009-01-26 11:16:21 -08001925 return -1;
1926 }
1927
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001928 if (hdr->page_size != page_size) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001929 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 -08001930 return -1;
1931 }
1932
Kishor PK9e91b592017-05-24 12:14:55 +05301933 image_addr = (unsigned char *)target_get_scratch_address();
1934 memcpy(image_addr, (void *)buf, page_size);
vijay kumar287bb542015-09-29 13:01:52 +05301935
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001936 /*
1937 * Update the kernel/ramdisk/tags address if the boot image header
1938 * has default values, these default values come from mkbootimg when
1939 * the boot image is flashed using fastboot flash:raw
1940 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001941 update_ker_tags_rdisk_addr(hdr, false);
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001942
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001943 /* Get virtual addresses since the hdr saves physical addresses. */
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001944 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
1945 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
1946 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
1947
1948 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1949 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Mayank Grover032736f2017-07-14 20:34:51 +05301950 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001951
Kishor PK9e91b592017-05-24 12:14:55 +05301952 /* ensure commandline is terminated */
1953 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
1954
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001955 /* Check if the addresses in the header are valid. */
1956 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
Mayank Grovere559cec2017-10-17 15:12:03 +05301957 check_ddr_addr_range_bound(hdr->kernel_addr, kernel_actual) ||
1958 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual) ||
1959 check_ddr_addr_range_bound(hdr->ramdisk_addr, ramdisk_actual))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001960 {
Mayank Grovere559cec2017-10-17 15:12:03 +05301961 dprintf(CRITICAL, "kernel/ramdisk addresses are not valid.\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001962 return -1;
1963 }
1964
1965#ifndef DEVICE_TREE
Kishor PKd8ddcad2017-07-27 13:53:57 +05301966 if (UINT_MAX < ((uint64_t)kernel_actual + (uint64_t)ramdisk_actual+ (uint64_t)second_actual + page_size)) {
Mayank Grover032736f2017-07-14 20:34:51 +05301967 dprintf(CRITICAL, "Integer overflow detected in bootimage header fields\n");
1968 return -1;
1969 }
Kishor PKd8ddcad2017-07-27 13:53:57 +05301970 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + second_actual);
Mayank Grover032736f2017-07-14 20:34:51 +05301971
Mayank Grovere559cec2017-10-17 15:12:03 +05301972 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE) ||
1973 check_ddr_addr_range_bound(hdr->tags_addr, MAX_TAGS_SIZE))
Vijay Kumar Pendotid3ed20e2016-09-20 00:34:46 +05301974 {
Mayank Grovere559cec2017-10-17 15:12:03 +05301975 dprintf(CRITICAL, "Tags addresses are not valid.\n");
Vijay Kumar Pendotid3ed20e2016-09-20 00:34:46 +05301976 return -1;
1977 }
1978#else
1979
1980#ifndef OSVERSION_IN_BOOTIMAGE
1981 dt_size = hdr->dt_size;
1982#endif
Mayank Grover032736f2017-07-14 20:34:51 +05301983 dt_actual = ROUND_TO_PAGE(dt_size, page_mask);
Kishor PKd8ddcad2017-07-27 13:53:57 +05301984 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 +05301985 dprintf(CRITICAL, "Integer overflow detected in bootimage header fields\n");
1986 return -1;
1987 }
1988
Kishor PKd8ddcad2017-07-27 13:53:57 +05301989 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + second_actual + dt_actual);
Mayank Grover032736f2017-07-14 20:34:51 +05301990
Mayank Grovere559cec2017-10-17 15:12:03 +05301991 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_size) ||
1992 check_ddr_addr_range_bound(hdr->tags_addr, dt_size))
Mayank Grover032736f2017-07-14 20:34:51 +05301993 {
Mayank Grovere559cec2017-10-17 15:12:03 +05301994 dprintf(CRITICAL, "Device tree addresses are not valid.\n");
Mayank Grover032736f2017-07-14 20:34:51 +05301995 return -1;
1996 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001997#endif
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001998
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05301999 /* Read full boot.img from flash */
2000 dprintf(INFO, "Loading (%s) image (%d): start\n",
2001 (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual);
2002 bs_set_timestamp(BS_KERNEL_LOAD_START);
2003
2004 if (UINT_MAX - page_size < imagesize_actual)
2005 {
2006 dprintf(CRITICAL,"Integer overflow detected in bootimage header fields %u %s\n", __LINE__,__func__);
2007 return -1;
2008 }
2009
2010 /*Check the availability of RAM before reading boot image + max signature length from flash*/
2011 if (target_get_max_flash_size() < (imagesize_actual + page_size))
2012 {
2013 dprintf(CRITICAL, "bootimage size is greater than DDR can hold\n");
2014 return -1;
2015 }
2016
2017 offset = page_size;
2018 /* Read image without signature and header */
2019 if (flash_read(ptn, offset, (void *)(image_addr + offset), imagesize_actual - page_size))
2020 {
2021 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
2022 return -1;
2023 }
2024
2025 dprintf(INFO, "Loading (%s) image (%d): done\n",
2026 (!boot_into_recovery ? "boot" : "recovery"), imagesize_actual);
2027 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
2028
Shashank Mittalcd98d472011-08-02 14:29:24 -07002029 /* Authenticate Kernel */
Deepa Dinamani23b60d42013-06-24 18:10:52 -07002030 if(target_use_signed_kernel() && (!device.is_unlocked))
Shashank Mittalcd98d472011-08-02 14:29:24 -07002031 {
Shashank Mittalcd98d472011-08-02 14:29:24 -07002032 offset = imagesize_actual;
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05302033
Shashank Mittalcd98d472011-08-02 14:29:24 -07002034 /* Read signature */
2035 if (flash_read(ptn, offset, (void *)(image_addr + offset), page_size))
2036 {
2037 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002038 return -1;
Shashank Mittalcd98d472011-08-02 14:29:24 -07002039 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07002040
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05302041 verify_signed_bootimg((uint32_t)image_addr, imagesize_actual);
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05302042 }
2043 offset = page_size;
2044 if(hdr->second_size != 0) {
2045 if (UINT_MAX - offset < second_actual)
2046 {
2047 dprintf(CRITICAL, "ERROR: Integer overflow in boot image header %s\t%d\n",__func__,__LINE__);
2048 return -1;
vijay kumar8f53e362015-11-24 13:38:11 +05302049 }
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05302050 offset += second_actual;
2051 /* Second image loading not implemented. */
2052 ASSERT(0);
2053 }
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05302054 /* Move kernel and ramdisk to correct address */
2055 memmove((void*) hdr->kernel_addr, (char*) (image_addr + page_size), hdr->kernel_size);
2056 memmove((void*) hdr->ramdisk_addr, (char*) (image_addr + page_size + kernel_actual), hdr->ramdisk_size);
2057
2058#if DEVICE_TREE
2059 if(dt_size != 0) {
2060
2061 dt_table_offset = ((uint32_t)image_addr + page_size + kernel_actual + ramdisk_actual + second_actual);
2062
2063 table = (struct dt_table*) dt_table_offset;
2064
2065 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
2066 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
2067 return -1;
2068 }
2069
2070 /* Its Error if, dt_hdr_size (table->num_entries * dt_entry size + Dev_Tree Header)
2071 goes beyound hdr->dt_size*/
2072 if (dt_hdr_size > ROUND_TO_PAGE(dt_size,hdr->page_size)) {
2073 dprintf(CRITICAL, "ERROR: Invalid Device Tree size \n");
2074 return -1;
2075 }
2076
2077 /* Find index of device tree within device tree table */
2078 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
2079 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
2080 return -1;
2081 }
2082
2083 /* Validate and Read device device tree in the "tags_add */
2084 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size) ||
2085 check_ddr_addr_range_bound(hdr->tags_addr, dt_entry.size))
2086 {
2087 dprintf(CRITICAL, "Device tree addresses are not valid.\n");
2088 return -1;
2089 }
2090
2091 if(dt_entry.offset > (UINT_MAX - dt_entry.size)) {
2092 dprintf(CRITICAL, "ERROR: Device tree contents are Invalid\n");
2093 return -1;
2094 }
2095
2096 /* Ensure we are not overshooting dt_size with the dt_entry selected */
2097 if ((dt_entry.offset + dt_entry.size) > dt_size) {
2098 dprintf(CRITICAL, "ERROR: Device tree contents are Invalid\n");
2099 return -1;
2100 }
2101
2102 best_match_dt_addr = (unsigned char *)table + dt_entry.offset;
2103 dtb_size = dt_entry.size;
2104 memmove((void *)hdr->tags_addr, (char *)best_match_dt_addr, dtb_size);
Vivek Kumar07bd0862018-04-10 14:19:23 +05302105
2106 } else {
2107 /* Validate the tags_addr */
2108 if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual) ||
2109 check_ddr_addr_range_bound(hdr->tags_addr, kernel_actual))
2110 {
2111 dprintf(CRITICAL, "Device tree addresses are not valid.\n");
2112 return -1;
2113 }
2114 /*
2115 * If appended dev tree is found, update the atags with
2116 * memory address to the DTB appended location on RAM.
2117 * Else update with the atags address in the kernel header
2118 */
2119 void *dtb = NULL;
2120 dtb = dev_tree_appended((void*)(image_addr + page_size ),hdr->kernel_size, dtb_offset, (void *)hdr->tags_addr);
2121 if (!dtb) {
2122 dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
2123 return -1;
2124 }
2125 }
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05302126#endif
2127 if(target_use_signed_kernel() && (!device.is_unlocked))
2128 {
Shashank Mittal162244e2011-08-08 19:01:25 -07002129 /* Make sure everything from scratch address is read before next step!*/
Shashank Mittala0032282011-08-26 14:50:11 -07002130 if(device.is_tampered)
Shashank Mittal162244e2011-08-08 19:01:25 -07002131 {
2132 write_device_info_flash(&device);
2133 }
Channagoud Kadabi5c86fe32012-02-16 10:58:48 +05302134#if USE_PCOM_SECBOOT
2135 set_tamper_flag(device.is_tampered);
2136#endif
Shashank Mittalcd98d472011-08-02 14:29:24 -07002137 }
David Ng183a7422009-12-07 14:55:21 -08002138continue_boot:
Dima Zavin214cc642009-01-26 11:16:21 -08002139
Dima Zavin214cc642009-01-26 11:16:21 -08002140 /* TODO: create/pass atags to kernel */
2141
Ajay Dudanie28a6072011-07-01 13:59:46 -07002142 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07002143 (const char *)hdr->cmdline, board_machtype(),
Dima Zavin214cc642009-01-26 11:16:21 -08002144 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
2145
2146 return 0;
2147}
Brian Swetland9c4c0752009-01-25 16:23:50 -08002148
Shashank Mittal162244e2011-08-08 19:01:25 -07002149void write_device_info_mmc(device_info *dev)
2150{
Shashank Mittal162244e2011-08-08 19:01:25 -07002151 unsigned long long ptn = 0;
2152 unsigned long long size;
2153 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002154 uint8_t lun = 0;
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002155 uint32_t ret = 0;
Mayank Groverddd348d2018-01-23 14:07:09 +05302156 uint32_t device_info_sz = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07002157
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002158 if (devinfo_present)
2159 index = partition_get_index("devinfo");
2160 else
2161 index = partition_get_index("aboot");
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07002162
Shashank Mittal162244e2011-08-08 19:01:25 -07002163 ptn = partition_get_offset(index);
2164 if(ptn == 0)
2165 {
2166 return;
2167 }
2168
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002169 lun = partition_get_lun(index);
2170 mmc_set_lun(lun);
2171
Shashank Mittal162244e2011-08-08 19:01:25 -07002172 size = partition_get_size(index);
2173
Mayank Groverddd348d2018-01-23 14:07:09 +05302174 device_info_sz = ROUND_TO_PAGE(sizeof(struct device_info),
2175 mmc_blocksize_mask);
2176 if (device_info_sz == UINT_MAX)
2177 {
2178 dprintf(CRITICAL, "ERROR: Incorrect blocksize of card\n");
2179 return;
2180 }
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07002181
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002182 if (devinfo_present)
Mayank Groverddd348d2018-01-23 14:07:09 +05302183 ret = mmc_write(ptn, device_info_sz, (void *)info_buf);
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002184 else
Mayank Groverddd348d2018-01-23 14:07:09 +05302185 ret = mmc_write((ptn + size - device_info_sz), device_info_sz, (void *)info_buf);
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002186 if (ret)
Shashank Mittal162244e2011-08-08 19:01:25 -07002187 {
2188 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
Sridhar Parasurame1a97922015-07-27 15:31:22 -07002189 ASSERT(0);
Shashank Mittal162244e2011-08-08 19:01:25 -07002190 }
2191}
2192
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002193void read_device_info_mmc(struct device_info *info)
Shashank Mittal162244e2011-08-08 19:01:25 -07002194{
Shashank Mittal162244e2011-08-08 19:01:25 -07002195 unsigned long long ptn = 0;
2196 unsigned long long size;
2197 int index = INVALID_PTN;
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002198 uint32_t ret = 0;
Mayank Groverddd348d2018-01-23 14:07:09 +05302199 uint32_t device_info_sz = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07002200
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002201 if ((index = partition_get_index("devinfo")) < 0)
2202 {
2203 devinfo_present = false;
2204 index = partition_get_index("aboot");
2205 }
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07002206
Shashank Mittal162244e2011-08-08 19:01:25 -07002207 ptn = partition_get_offset(index);
2208 if(ptn == 0)
2209 {
2210 return;
2211 }
2212
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07002213 mmc_set_lun(partition_get_lun(index));
2214
Shashank Mittal162244e2011-08-08 19:01:25 -07002215 size = partition_get_size(index);
2216
Mayank Groverddd348d2018-01-23 14:07:09 +05302217 device_info_sz = ROUND_TO_PAGE(sizeof(struct device_info),
2218 mmc_blocksize_mask);
2219 if (device_info_sz == UINT_MAX)
2220 {
2221 dprintf(CRITICAL, "ERROR: Incorrect blocksize of card\n");
2222 return;
2223 }
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07002224
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002225 if (devinfo_present)
Mayank Groverddd348d2018-01-23 14:07:09 +05302226 ret = mmc_read(ptn, (void *)info_buf, device_info_sz);
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002227 else
Mayank Groverddd348d2018-01-23 14:07:09 +05302228 ret = mmc_read((ptn + size - device_info_sz), (void *)info_buf, device_info_sz);
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002229 if (ret)
Shashank Mittal162244e2011-08-08 19:01:25 -07002230 {
2231 dprintf(CRITICAL, "ERROR: Cannot read device info\n");
Sridhar Parasurame1a97922015-07-27 15:31:22 -07002232 ASSERT(0);
Shashank Mittal162244e2011-08-08 19:01:25 -07002233 }
Shashank Mittal162244e2011-08-08 19:01:25 -07002234}
2235
2236void write_device_info_flash(device_info *dev)
2237{
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002238 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
Shashank Mittal162244e2011-08-08 19:01:25 -07002239 struct ptentry *ptn;
2240 struct ptable *ptable;
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002241 if(info == NULL)
2242 {
2243 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
2244 ASSERT(0);
2245 }
2246 info_buf = info;
Shashank Mittal162244e2011-08-08 19:01:25 -07002247 ptable = flash_get_ptable();
2248 if (ptable == NULL)
2249 {
2250 dprintf(CRITICAL, "ERROR: Partition table not found\n");
2251 return;
2252 }
2253
2254 ptn = ptable_find(ptable, "devinfo");
2255 if (ptn == NULL)
2256 {
Smita Ghosh670c8b82015-05-07 09:30:03 -07002257 dprintf(CRITICAL, "ERROR: No devinfo partition found\n");
Shashank Mittal162244e2011-08-08 19:01:25 -07002258 return;
2259 }
2260
Mayank Groverdedbc892017-10-24 13:41:34 +05302261 memset(info, 0, BOOT_IMG_MAX_PAGE_SIZE);
Shashank Mittal162244e2011-08-08 19:01:25 -07002262 memcpy(info, dev, sizeof(device_info));
2263
2264 if (flash_write(ptn, 0, (void *)info_buf, page_size))
2265 {
2266 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
2267 return;
2268 }
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002269 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07002270}
2271
vijay kumarc65876c2015-04-24 13:29:16 +05302272static int read_allow_oem_unlock(device_info *dev)
2273{
vijay kumarc65876c2015-04-24 13:29:16 +05302274 unsigned offset;
2275 int index;
2276 unsigned long long ptn;
2277 unsigned long long ptn_size;
2278 unsigned blocksize = mmc_get_device_blocksize();
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07002279 STACKBUF_DMA_ALIGN(buf, blocksize);
vijay kumarc65876c2015-04-24 13:29:16 +05302280
vijay kumarca2e6812015-07-08 20:28:25 +05302281 index = partition_get_index(frp_ptns[0]);
vijay kumarc65876c2015-04-24 13:29:16 +05302282 if (index == INVALID_PTN)
2283 {
vijay kumarca2e6812015-07-08 20:28:25 +05302284 index = partition_get_index(frp_ptns[1]);
2285 if (index == INVALID_PTN)
2286 {
2287 dprintf(CRITICAL, "Neither '%s' nor '%s' partition found\n", frp_ptns[0],frp_ptns[1]);
2288 return -1;
2289 }
vijay kumarc65876c2015-04-24 13:29:16 +05302290 }
2291
2292 ptn = partition_get_offset(index);
2293 ptn_size = partition_get_size(index);
2294 offset = ptn_size - blocksize;
2295
Mayank Grover48860402016-11-29 12:34:53 +05302296 /* Set Lun for partition */
2297 mmc_set_lun(partition_get_lun(index));
2298
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07002299 if (mmc_read(ptn + offset, (void *)buf, blocksize))
vijay kumarc65876c2015-04-24 13:29:16 +05302300 {
2301 dprintf(CRITICAL, "Reading MMC failed\n");
2302 return -1;
2303 }
2304
2305 /*is_allow_unlock is a bool value stored at the LSB of last byte*/
2306 is_allow_unlock = buf[blocksize-1] & 0x01;
2307 return 0;
2308}
2309
2310static int write_allow_oem_unlock(bool allow_unlock)
2311{
vijay kumarc65876c2015-04-24 13:29:16 +05302312 unsigned offset;
vijay kumarc65876c2015-04-24 13:29:16 +05302313 int index;
2314 unsigned long long ptn;
2315 unsigned long long ptn_size;
2316 unsigned blocksize = mmc_get_device_blocksize();
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07002317 STACKBUF_DMA_ALIGN(buf, blocksize);
vijay kumarc65876c2015-04-24 13:29:16 +05302318
vijay kumarca2e6812015-07-08 20:28:25 +05302319 index = partition_get_index(frp_ptns[0]);
vijay kumarc65876c2015-04-24 13:29:16 +05302320 if (index == INVALID_PTN)
2321 {
vijay kumarca2e6812015-07-08 20:28:25 +05302322 index = partition_get_index(frp_ptns[1]);
2323 if (index == INVALID_PTN)
2324 {
2325 dprintf(CRITICAL, "Neither '%s' nor '%s' partition found\n", frp_ptns[0],frp_ptns[1]);
2326 return -1;
2327 }
vijay kumarc65876c2015-04-24 13:29:16 +05302328 }
2329
2330 ptn = partition_get_offset(index);
2331 ptn_size = partition_get_size(index);
2332 offset = ptn_size - blocksize;
Mayank Grover48860402016-11-29 12:34:53 +05302333 mmc_set_lun(partition_get_lun(index));
vijay kumarc65876c2015-04-24 13:29:16 +05302334
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07002335 if (mmc_read(ptn + offset, (void *)buf, blocksize))
vijay kumarc65876c2015-04-24 13:29:16 +05302336 {
2337 dprintf(CRITICAL, "Reading MMC failed\n");
2338 return -1;
2339 }
2340
2341 /*is_allow_unlock is a bool value stored at the LSB of last byte*/
2342 buf[blocksize-1] = allow_unlock;
2343 if (mmc_write(ptn + offset, blocksize, buf))
2344 {
2345 dprintf(CRITICAL, "Writing MMC failed\n");
2346 return -1;
2347 }
2348
2349 return 0;
2350}
2351
Shashank Mittal162244e2011-08-08 19:01:25 -07002352void read_device_info_flash(device_info *dev)
2353{
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002354 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
Shashank Mittal162244e2011-08-08 19:01:25 -07002355 struct ptentry *ptn;
2356 struct ptable *ptable;
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002357 if(info == NULL)
2358 {
2359 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
2360 ASSERT(0);
2361 }
2362 info_buf = info;
Shashank Mittal162244e2011-08-08 19:01:25 -07002363 ptable = flash_get_ptable();
2364 if (ptable == NULL)
2365 {
2366 dprintf(CRITICAL, "ERROR: Partition table not found\n");
2367 return;
2368 }
2369
2370 ptn = ptable_find(ptable, "devinfo");
2371 if (ptn == NULL)
2372 {
Smita Ghosh670c8b82015-05-07 09:30:03 -07002373 dprintf(CRITICAL, "ERROR: No devinfo partition found\n");
Shashank Mittal162244e2011-08-08 19:01:25 -07002374 return;
2375 }
2376
2377 if (flash_read(ptn, 0, (void *)info_buf, page_size))
2378 {
2379 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
2380 return;
2381 }
2382
2383 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
2384 {
Shashank Mittal162244e2011-08-08 19:01:25 -07002385 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
2386 info->is_unlocked = 0;
Shashank Mittala0032282011-08-26 14:50:11 -07002387 info->is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07002388 write_device_info_flash(info);
2389 }
2390 memcpy(dev, info, sizeof(device_info));
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002391 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07002392}
2393
2394void write_device_info(device_info *dev)
2395{
2396 if(target_is_emmc_boot())
2397 {
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002398 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
2399 if(info == NULL)
2400 {
2401 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
2402 ASSERT(0);
2403 }
2404 info_buf = info;
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002405 memcpy(info, dev, sizeof(struct device_info));
2406
2407#if USE_RPMB_FOR_DEVINFO
Mayank Grover912eaa62017-10-26 12:08:53 +05302408 if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05302409 is_secure_boot_enable()) {
2410 if((write_device_info_rpmb((void*) info, PAGE_SIZE)) < 0)
2411 ASSERT(0);
Sridhar Parasurame1a97922015-07-27 15:31:22 -07002412 }
Channagoud Kadabic80cb492015-04-28 16:08:28 -07002413 else
2414 write_device_info_mmc(info);
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002415#else
2416 write_device_info_mmc(info);
2417#endif
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002418 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07002419 }
2420 else
2421 {
2422 write_device_info_flash(dev);
2423 }
2424}
2425
Monika Singh94316462018-03-15 18:39:01 +05302426int read_rollback_index(uint32_t loc, uint64_t *roll_back_index)
2427{
2428 if (!devinfo_present) {
2429 dprintf(CRITICAL, "DeviceInfo not initalized \n");
2430 return -EINVAL;
2431 }
2432 if (loc >= ARRAY_SIZE(device.rollback_index)) {
2433 dprintf(CRITICAL, "%s() Loc out of range index: %d, array len: %d\n",
2434 __func__, loc, ARRAY_SIZE(device.rollback_index));
2435 ASSERT(0);
2436 }
2437
2438 *roll_back_index = device.rollback_index[loc];
2439 return 0;
2440}
2441
2442int write_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 device.rollback_index[loc] = roll_back_index;
2455 write_device_info(&device);
2456 return 0;
2457}
2458
Monika Singhb0fad822018-03-15 18:50:55 +05302459int store_userkey(uint8_t *user_key, uint32_t user_key_size)
2460{
2461 if (!devinfo_present) {
2462 dprintf(CRITICAL, "DeviceInfo not initalized \n");
2463 return -EINVAL;
2464 }
2465
2466 if (user_key_size > ARRAY_SIZE(device.user_public_key)) {
2467 dprintf(CRITICAL, "StoreUserKey, UserKeySize too large!\n");
2468 return -ENODEV;
2469 }
2470
2471 memcpy(device.user_public_key, user_key, user_key_size);
2472 device.user_public_key_length = user_key_size;
2473 write_device_info(&device);
2474 return 0;
2475}
2476
2477int erase_userkey()
2478{
2479 if (!devinfo_present) {
2480 dprintf(CRITICAL, "DeviceInfo not initalized \n");
2481 return -EINVAL;
2482 }
2483 memset(device.user_public_key, 0, ARRAY_SIZE(device.user_public_key));
2484 device.user_public_key_length = 0;
2485 write_device_info(&device);
2486 return 0;
2487}
2488
2489int get_userkey(uint8_t **user_key, uint32_t *user_key_size)
2490{
2491 if (!devinfo_present) {
2492 dprintf(CRITICAL, "DeviceInfo not initalized \n");
2493 return -EINVAL;
2494 }
2495 *user_key = device.user_public_key;
2496 *user_key_size = device.user_public_key_length;
2497 return 0;
2498}
2499
Shashank Mittal162244e2011-08-08 19:01:25 -07002500void read_device_info(device_info *dev)
2501{
2502 if(target_is_emmc_boot())
2503 {
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002504 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
2505 if(info == NULL)
2506 {
2507 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
2508 ASSERT(0);
2509 }
2510 info_buf = info;
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002511
2512#if USE_RPMB_FOR_DEVINFO
Mayank Grover912eaa62017-10-26 12:08:53 +05302513 if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05302514 is_secure_boot_enable()) {
2515 if((read_device_info_rpmb((void*) info, PAGE_SIZE)) < 0)
2516 ASSERT(0);
Sridhar Parasurame1a97922015-07-27 15:31:22 -07002517 }
Channagoud Kadabic80cb492015-04-28 16:08:28 -07002518 else
2519 read_device_info_mmc(info);
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002520#else
2521 read_device_info_mmc(info);
2522#endif
2523
2524 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
2525 {
2526 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
lijuang511a2b52015-08-14 20:50:51 +08002527 if (is_secure_boot_enable()) {
Channagoud Kadabi05f78ba2015-07-06 11:58:14 -07002528 info->is_unlocked = 0;
Monika Singh292b3e92018-03-17 22:40:23 +05302529#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover912eaa62017-10-26 12:08:53 +05302530 if (VB_M <= target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +05302531 info->is_unlock_critical = 0;
Parth Dixitddbc7352015-10-18 03:13:31 +05302532#endif
lijuang511a2b52015-08-14 20:50:51 +08002533 } else {
Channagoud Kadabi2fda4092015-07-07 13:34:11 -07002534 info->is_unlocked = 1;
Monika Singh292b3e92018-03-17 22:40:23 +05302535#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover912eaa62017-10-26 12:08:53 +05302536 if (VB_M <= target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +05302537 info->is_unlock_critical = 1;
Parth Dixitddbc7352015-10-18 03:13:31 +05302538#endif
lijuang511a2b52015-08-14 20:50:51 +08002539 }
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002540 info->is_tampered = 0;
2541 info->charger_screen_enabled = 0;
Monika Singh292b3e92018-03-17 22:40:23 +05302542#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover912eaa62017-10-26 12:08:53 +05302543 if (VB_M <= target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +05302544 info->verity_mode = 1; //enforcing by default
Parth Dixitddbc7352015-10-18 03:13:31 +05302545#endif
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002546 write_device_info(info);
2547 }
2548 memcpy(dev, info, sizeof(device_info));
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002549 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07002550 }
2551 else
2552 {
2553 read_device_info_flash(dev);
2554 }
2555}
2556
2557void reset_device_info()
2558{
2559 dprintf(ALWAYS, "reset_device_info called.");
Shashank Mittala0032282011-08-26 14:50:11 -07002560 device.is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07002561 write_device_info(&device);
2562}
2563
2564void set_device_root()
2565{
2566 dprintf(ALWAYS, "set_device_root called.");
Shashank Mittala0032282011-08-26 14:50:11 -07002567 device.is_tampered = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -07002568 write_device_info(&device);
2569}
2570
lijuang4ece1e72015-08-14 21:02:36 +08002571/* set device unlock value
2572 * Must check FRP before call this function
2573 * Need to wipe data when unlock status changed
2574 * type 0: oem unlock
2575 * type 1: unlock critical
2576 * status 0: unlock as false
2577 * status 1: lock as true
2578 */
2579void set_device_unlock_value(int type, bool status)
lijuang21f12f52015-08-22 16:22:19 +08002580{
lijuang4ece1e72015-08-14 21:02:36 +08002581 if (type == UNLOCK)
2582 device.is_unlocked = status;
Monika Singh292b3e92018-03-17 22:40:23 +05302583#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover912eaa62017-10-26 12:08:53 +05302584 else if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05302585 type == UNLOCK_CRITICAL)
2586 device.is_unlock_critical = status;
Parth Dixitddbc7352015-10-18 03:13:31 +05302587#endif
lijuang4ece1e72015-08-14 21:02:36 +08002588 write_device_info(&device);
2589}
2590
2591static void set_device_unlock(int type, bool status)
2592{
2593 int is_unlocked = -1;
2594 char response[MAX_RSP_SIZE];
2595
2596 /* check device unlock status if it is as expected */
2597 if (type == UNLOCK)
2598 is_unlocked = device.is_unlocked;
Monika Singh292b3e92018-03-17 22:40:23 +05302599#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover912eaa62017-10-26 12:08:53 +05302600 if(VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05302601 type == UNLOCK_CRITICAL)
2602 {
2603 is_unlocked = device.is_unlock_critical;
2604 }
Parth Dixitddbc7352015-10-18 03:13:31 +05302605#endif
lijuang4ece1e72015-08-14 21:02:36 +08002606 if (is_unlocked == status) {
2607 snprintf(response, sizeof(response), "\tDevice already : %s", (status ? "unlocked!" : "locked!"));
2608 fastboot_info(response);
2609 fastboot_okay("");
2610 return;
2611 }
2612
2613 /* status is true, it means to unlock device */
lijuang07c5d6e2018-04-23 18:32:13 +08002614 if (status && !is_allow_unlock) {
2615 fastboot_fail("oem unlock is not allowed");
2616 return;
2617 }
lijuang21f12f52015-08-22 16:22:19 +08002618
lijuang4ece1e72015-08-14 21:02:36 +08002619#if FBCON_DISPLAY_MSG
lijuang07c5d6e2018-04-23 18:32:13 +08002620 display_unlock_menu(type, status);
2621 fastboot_okay("");
2622 return;
lijuang4ece1e72015-08-14 21:02:36 +08002623#else
lijuang07c5d6e2018-04-23 18:32:13 +08002624 if (status && type == UNLOCK) {
2625 fastboot_fail("Need wipe userdata. Do 'fastboot oem unlock-go'");
2626 return;
lijuang21f12f52015-08-22 16:22:19 +08002627 }
lijuang07c5d6e2018-04-23 18:32:13 +08002628#endif
lijuang4ece1e72015-08-14 21:02:36 +08002629
2630 set_device_unlock_value(type, status);
2631
2632 /* wipe data */
2633 struct recovery_message msg;
Kishor PK60a68212017-05-08 16:55:57 +05302634 memset(&msg, 0, sizeof(msg));
lijuang4ece1e72015-08-14 21:02:36 +08002635 snprintf(msg.recovery, sizeof(msg.recovery), "recovery\n--wipe_data");
2636 write_misc(0, &msg, sizeof(msg));
2637
2638 fastboot_okay("");
2639 reboot_device(RECOVERY_MODE);
lijuang21f12f52015-08-22 16:22:19 +08002640}
2641
lijuang511a2b52015-08-14 20:50:51 +08002642static bool critical_flash_allowed(const char * entry)
2643{
2644 uint32_t i = 0;
2645 if (entry == NULL)
2646 return false;
2647
2648 for (i = 0; i < ARRAY_SIZE(critical_flash_allowed_ptn); i++) {
2649 if(!strcmp(entry, critical_flash_allowed_ptn[i]))
2650 return true;
2651 }
2652 return false;
Matthew Qin271927e2015-03-31 22:07:07 -04002653}
2654
2655#if DEVICE_TREE
Amol Jadicb524072012-08-09 16:40:18 -07002656int copy_dtb(uint8_t *boot_image_start, unsigned int scratch_offset)
2657{
2658 uint32 dt_image_offset = 0;
Amol Jadicb524072012-08-09 16:40:18 -07002659 uint32_t n;
Monika Singh292b3e92018-03-17 22:40:23 +05302660 struct dt_table *table = NULL;
Amol Jadicb524072012-08-09 16:40:18 -07002661 struct dt_entry dt_entry;
Monika Singh292b3e92018-03-17 22:40:23 +05302662 uint32_t dt_hdr_size = 0;
Amol Jadicb524072012-08-09 16:40:18 -07002663 unsigned int compressed_size = 0;
2664 unsigned int dtb_size = 0;
2665 unsigned int out_avai_len = 0;
2666 unsigned char *out_addr = NULL;
2667 unsigned char *best_match_dt_addr = NULL;
2668 int rc;
2669
2670 struct boot_img_hdr *hdr = (struct boot_img_hdr *) (boot_image_start);
2671
Parth Dixit4097b622016-03-15 14:42:27 +05302672#ifndef OSVERSION_IN_BOOTIMAGE
2673 dt_size = hdr->dt_size;
2674#endif
2675
2676 if(dt_size != 0) {
Amol Jadicb524072012-08-09 16:40:18 -07002677 /* add kernel offset */
2678 dt_image_offset += page_size;
2679 n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
2680 dt_image_offset += n;
2681
2682 /* add ramdisk offset */
2683 n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
2684 dt_image_offset += n;
2685
2686 /* add second offset */
2687 if(hdr->second_size != 0) {
2688 n = ROUND_TO_PAGE(hdr->second_size, page_mask);
2689 dt_image_offset += n;
2690 }
2691
2692 /* offset now point to start of dt.img */
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07002693 table = (struct dt_table*)(boot_image_start + dt_image_offset);
Amol Jadicb524072012-08-09 16:40:18 -07002694
Deepa Dinamani19648b42013-09-05 17:05:55 -07002695 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Amol Jadicb524072012-08-09 16:40:18 -07002696 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
2697 return -1;
2698 }
P.V. Phani Kumar82916762016-03-09 09:20:19 +05302699
2700 /* Its Error if, dt_hdr_size (table->num_entries * dt_entry size + Dev_Tree Header)
2701 goes beyound hdr->dt_size*/
Parth Dixit4097b622016-03-15 14:42:27 +05302702 if (dt_hdr_size > ROUND_TO_PAGE(dt_size,hdr->page_size)) {
P.V. Phani Kumar82916762016-03-09 09:20:19 +05302703 dprintf(CRITICAL, "ERROR: Invalid Device Tree size \n");
2704 return -1;
2705 }
2706
Joel Kingaa335dc2013-06-03 16:11:08 -07002707 /* Find index of device tree within device tree table */
2708 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Amol Jadicb524072012-08-09 16:40:18 -07002709 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
2710 return -1;
2711 }
2712
Matthew Qin271927e2015-03-31 22:07:07 -04002713 best_match_dt_addr = (unsigned char *)boot_image_start + dt_image_offset + dt_entry.offset;
2714 if (is_gzip_package(best_match_dt_addr, dt_entry.size))
2715 {
2716 out_addr = (unsigned char *)target_get_scratch_address() + scratch_offset;
2717 out_avai_len = target_get_max_flash_size() - scratch_offset;
Matthew Qin0b15b322015-05-19 05:20:54 -04002718 dprintf(INFO, "decompressing dtb: start\n");
Matthew Qin271927e2015-03-31 22:07:07 -04002719 rc = decompress(best_match_dt_addr,
2720 dt_entry.size, out_addr, out_avai_len,
2721 &compressed_size, &dtb_size);
2722 if (rc)
2723 {
Matthew Qin0b15b322015-05-19 05:20:54 -04002724 dprintf(CRITICAL, "decompressing dtb failed!!!\n");
Matthew Qin271927e2015-03-31 22:07:07 -04002725 ASSERT(0);
2726 }
2727
Matthew Qin0b15b322015-05-19 05:20:54 -04002728 dprintf(INFO, "decompressing dtb: done\n");
Matthew Qin271927e2015-03-31 22:07:07 -04002729 best_match_dt_addr = out_addr;
2730 } else {
2731 dtb_size = dt_entry.size;
2732 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002733 /* Validate and Read device device tree in the "tags_add */
Mayank Grovere559cec2017-10-17 15:12:03 +05302734 if (check_aboot_addr_range_overlap(hdr->tags_addr, dtb_size) ||
2735 check_ddr_addr_range_bound(hdr->tags_addr, dtb_size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002736 {
Mayank Grovere559cec2017-10-17 15:12:03 +05302737 dprintf(CRITICAL, "Device tree addresses are not valid.\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002738 return -1;
2739 }
2740
Amol Jadicb524072012-08-09 16:40:18 -07002741 /* Read device device tree in the "tags_add */
Matthew Qin271927e2015-03-31 22:07:07 -04002742 memmove((void*) hdr->tags_addr, (void *)best_match_dt_addr, dtb_size);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002743 } else
2744 return -1;
Amol Jadicb524072012-08-09 16:40:18 -07002745
2746 /* Everything looks fine. Return success. */
2747 return 0;
2748}
2749#endif
2750
Brian Swetland9c4c0752009-01-25 16:23:50 -08002751void cmd_boot(const char *arg, void *data, unsigned sz)
2752{
2753 unsigned kernel_actual;
2754 unsigned ramdisk_actual;
Kishor PKd8ddcad2017-07-27 13:53:57 +05302755 unsigned second_actual;
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002756 uint32_t image_actual;
2757 uint32_t dt_actual = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08002758 struct boot_img_hdr *hdr = NULL;
2759 struct kernel64_hdr *kptr = NULL;
Brian Swetland9c4c0752009-01-25 16:23:50 -08002760 char *ptr = ((char*) data);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002761 int ret = 0;
2762 uint8_t dtb_copied = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08002763 unsigned int out_len = 0;
2764 unsigned int out_avai_len = 0;
2765 unsigned char *out_addr = NULL;
2766 uint32_t dtb_offset = 0;
2767 unsigned char *kernel_start_addr = NULL;
2768 unsigned int kernel_size = 0;
Matthew Qin271927e2015-03-31 22:07:07 -04002769 unsigned int scratch_offset = 0;
Monika Singh292b3e92018-03-17 22:40:23 +05302770#if !VERIFIED_BOOT_2
2771 uint32_t sig_actual = 0;
2772 uint32_t sig_size = 0;
2773#ifdef MDTP_SUPPORT
2774 static bool is_mdtp_activated = 0;
2775#endif /* MDTP_SUPPORT */
2776#endif
Matthew Qinbb7923d2015-02-09 10:56:09 +08002777
lijuang2008ff22016-03-07 17:56:27 +08002778#if FBCON_DISPLAY_MSG
2779 /* Exit keys' detection thread firstly */
2780 exit_menu_keys_detection();
2781#endif
2782
Monika Singh292b3e92018-03-17 22:40:23 +05302783#if VERIFIED_BOOT || VERIFIED_BOOT_2
Channagoud Kadabi6d5375e2015-06-23 17:15:42 -07002784 if(target_build_variant_user() && !device.is_unlocked)
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002785 {
2786 fastboot_fail("unlock device to use this command");
lijuang2008ff22016-03-07 17:56:27 +08002787 goto boot_failed;
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002788 }
2789#endif
2790
Brian Swetland9c4c0752009-01-25 16:23:50 -08002791 if (sz < sizeof(hdr)) {
2792 fastboot_fail("invalid bootimage header");
lijuang2008ff22016-03-07 17:56:27 +08002793 goto boot_failed;
Brian Swetland9c4c0752009-01-25 16:23:50 -08002794 }
2795
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002796 hdr = (struct boot_img_hdr *)data;
Brian Swetland9c4c0752009-01-25 16:23:50 -08002797
2798 /* ensure commandline is terminated */
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002799 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -08002800
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002801 if(target_is_emmc_boot() && hdr->page_size) {
2802 page_size = hdr->page_size;
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07002803 page_mask = page_size - 1;
2804 }
2805
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002806 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
2807 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Kishor PKd8ddcad2017-07-27 13:53:57 +05302808 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002809#if DEVICE_TREE
Parth Dixit4097b622016-03-15 14:42:27 +05302810#ifndef OSVERSION_IN_BOOTIMAGE
2811 dt_size = hdr->dt_size;
2812#endif
2813 dt_actual = ROUND_TO_PAGE(dt_size, page_mask);
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002814#endif
2815
2816 image_actual = ADD_OF(page_size, kernel_actual);
2817 image_actual = ADD_OF(image_actual, ramdisk_actual);
Kishor PKd8ddcad2017-07-27 13:53:57 +05302818 image_actual = ADD_OF(image_actual, second_actual);
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002819 image_actual = ADD_OF(image_actual, dt_actual);
2820
Kishor PK5134b332017-05-09 17:50:08 +05302821 /* Checking to prevent oob access in read_der_message_length */
2822 if (image_actual > sz) {
2823 fastboot_fail("bootimage header fields are invalid");
2824 goto boot_failed;
2825 }
Monika Singh292b3e92018-03-17 22:40:23 +05302826#if VERIFIED_BOOT_2
2827 memset(&info, 0, sizeof(bootinfo));
2828 info.images[0].image_buffer = data;
2829 info.images[0].imgsize = image_actual;
2830 info.images[0].name = "boot";
2831 info.num_loaded_images = 1;
2832 info.multi_slot_boot = partition_multislot_is_supported();
2833 if (load_image_and_auth(&info))
2834 goto boot_failed;
2835 vbcmdline = info.vbcmdline;
2836#else
Kishor PK5134b332017-05-09 17:50:08 +05302837 sig_size = sz - image_actual;
2838
P.V. Phani Kumareecfd822016-03-09 20:09:03 +05302839 if (target_use_signed_kernel() && (!device.is_unlocked)) {
Monika Singh292b3e92018-03-17 22:40:23 +05302840 unsigned chk;
P.V. Phani Kumareecfd822016-03-09 20:09:03 +05302841 /* Calculate the signature length from boot image */
2842 sig_actual = read_der_message_length(
Kishor PK5134b332017-05-09 17:50:08 +05302843 (unsigned char*)(data + image_actual), sig_size);
Monika Singh292b3e92018-03-17 22:40:23 +05302844 chk = ADD_OF(image_actual, sig_actual);
Channagoud Kadabi3d839012014-06-26 14:26:39 -07002845
Monika Singh292b3e92018-03-17 22:40:23 +05302846 if (chk > sz) {
Kishor PK5134b332017-05-09 17:50:08 +05302847 fastboot_fail("bootimage header fields are invalid");
2848 goto boot_failed;
2849 }
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002850 }
2851
Sridhar Parasuram361e4cd2015-09-24 11:19:19 -07002852 // Initialize boot state before trying to verify boot.img
2853#if VERIFIED_BOOT
Channagoud Kadabi699466e2015-11-03 12:37:42 -08002854 boot_verifier_init();
Mayank Groverb337e932017-01-18 20:00:40 +05302855#endif
Channagoud Kadabibf6ce7d2015-09-17 13:25:35 -07002856 /* Handle overflow if the input image size is greater than
2857 * boot image buffer can hold
2858 */
Monika Singh292b3e92018-03-17 22:40:23 +05302859 if ((target_get_max_flash_size() - page_size) < image_actual)
Channagoud Kadabibf6ce7d2015-09-17 13:25:35 -07002860 {
2861 fastboot_fail("booimage: size is greater than boot image buffer can hold");
lijuang2008ff22016-03-07 17:56:27 +08002862 goto boot_failed;
Channagoud Kadabibf6ce7d2015-09-17 13:25:35 -07002863 }
Sridhar Parasuram361e4cd2015-09-24 11:19:19 -07002864
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002865 /* Verify the boot image
2866 * device & page_size are initialized in aboot_init
2867 */
Sridhar Parasuram7ae9fbc2015-09-17 09:56:30 -07002868 if (target_use_signed_kernel() && (!device.is_unlocked)) {
Channagoud Kadabi3d839012014-06-26 14:26:39 -07002869 /* Pass size excluding signature size, otherwise we would try to
2870 * access signature beyond its length
2871 */
Monika Singh292b3e92018-03-17 22:40:23 +05302872 verify_signed_bootimg((uint32_t)data, image_actual);
Sridhar Parasuram7ae9fbc2015-09-17 09:56:30 -07002873 }
Amit Blay4aa292f2015-04-28 21:55:59 +03002874#ifdef MDTP_SUPPORT
2875 else
2876 {
Shay Nachmani062aa3f2015-05-17 17:28:44 +03002877 /* fastboot boot is not allowed when MDTP is activated */
Amit Blay4aa292f2015-04-28 21:55:59 +03002878 mdtp_ext_partition_verification_t ext_partition;
Amit Blay8a510302015-08-17 09:20:01 +03002879
2880 if (!is_mdtp_activated) {
2881 ext_partition.partition = MDTP_PARTITION_NONE;
2882 mdtp_fwlock_verify_lock(&ext_partition);
2883 }
Amit Blay4aa292f2015-04-28 21:55:59 +03002884 }
Shay Nachmani062aa3f2015-05-17 17:28:44 +03002885
Amir Kotzer7c768c02016-04-13 09:08:05 +03002886 /* If mdtp state cannot be validate, block fastboot boot*/
2887 if(mdtp_activated(&is_mdtp_activated)){
2888 dprintf(CRITICAL, "mdtp_activated cannot validate state.\n");
2889 dprintf(CRITICAL, "Can not proceed with fastboot boot command.\n");
2890 goto boot_failed;
2891 }
Shay Nachmani062aa3f2015-05-17 17:28:44 +03002892 if(is_mdtp_activated){
2893 dprintf(CRITICAL, "fastboot boot command is not available.\n");
lijuang2008ff22016-03-07 17:56:27 +08002894 goto boot_failed;
Shay Nachmani062aa3f2015-05-17 17:28:44 +03002895 }
Amit Blay4aa292f2015-04-28 21:55:59 +03002896#endif /* MDTP_SUPPORT */
Monika Singh292b3e92018-03-17 22:40:23 +05302897#endif /* VERIFIED_BOOT_2 else */
Amit Blay4aa292f2015-04-28 21:55:59 +03002898
Sridhar Parasuram361e4cd2015-09-24 11:19:19 -07002899#if VERIFIED_BOOT
Monika Singh292b3e92018-03-17 22:40:23 +05302900 if (VB_M == target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +05302901 {
2902 /* set boot and system versions. */
2903 set_os_version((unsigned char *)data);
2904 // send root of trust
2905 if(!send_rot_command((uint32_t)device.is_unlocked))
2906 ASSERT(0);
2907 }
Parth Dixitbc9b6492015-10-18 00:41:38 +05302908#endif
Channagoud Kadabia22144f2013-03-20 11:49:01 -07002909 /*
Matthew Qinbb7923d2015-02-09 10:56:09 +08002910 * Check if the kernel image is a gzip package. If yes, need to decompress it.
2911 * If not, continue booting.
2912 */
2913 if (is_gzip_package((unsigned char *)(data + page_size), hdr->kernel_size))
2914 {
2915 out_addr = (unsigned char *)target_get_scratch_address();
2916 out_addr = (unsigned char *)(out_addr + image_actual + page_size);
2917 out_avai_len = target_get_max_flash_size() - image_actual - page_size;
Matthew Qin0b15b322015-05-19 05:20:54 -04002918 dprintf(INFO, "decompressing kernel image: start\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08002919 ret = decompress((unsigned char *)(ptr + page_size),
2920 hdr->kernel_size, out_addr, out_avai_len,
2921 &dtb_offset, &out_len);
2922 if (ret)
2923 {
Matthew Qin0b15b322015-05-19 05:20:54 -04002924 dprintf(CRITICAL, "decompressing image failed!!!\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08002925 ASSERT(0);
2926 }
2927
Matthew Qin0b15b322015-05-19 05:20:54 -04002928 dprintf(INFO, "decompressing kernel image: done\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08002929 kptr = (struct kernel64_hdr *)out_addr;
2930 kernel_start_addr = out_addr;
2931 kernel_size = out_len;
2932 } else {
2933 kptr = (struct kernel64_hdr*)((char *)data + page_size);
2934 kernel_start_addr = (unsigned char *)((char *)data + page_size);
2935 kernel_size = hdr->kernel_size;
2936 }
2937
2938 /*
Channagoud Kadabia22144f2013-03-20 11:49:01 -07002939 * Update the kernel/ramdisk/tags address if the boot image header
2940 * has default values, these default values come from mkbootimg when
2941 * the boot image is flashed using fastboot flash:raw
2942 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08002943 update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr));
Dima Zavin3cadfff2013-03-21 14:30:48 -07002944
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002945 /* Get virtual addresses since the hdr saves physical addresses. */
2946 hdr->kernel_addr = VA(hdr->kernel_addr);
2947 hdr->ramdisk_addr = VA(hdr->ramdisk_addr);
2948 hdr->tags_addr = VA(hdr->tags_addr);
Brian Swetland9c4c0752009-01-25 16:23:50 -08002949
Matthew Qinbb7923d2015-02-09 10:56:09 +08002950 kernel_size = ROUND_TO_PAGE(kernel_size, page_mask);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002951 /* Check if the addresses in the header are valid. */
Matthew Qinbb7923d2015-02-09 10:56:09 +08002952 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_size) ||
Mayank Grovere559cec2017-10-17 15:12:03 +05302953 check_ddr_addr_range_bound(hdr->kernel_addr, kernel_size) ||
2954 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual) ||
2955 check_ddr_addr_range_bound(hdr->ramdisk_addr, ramdisk_actual))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002956 {
Mayank Grovere559cec2017-10-17 15:12:03 +05302957 dprintf(CRITICAL, "kernel/ramdisk addresses are not valid.\n");
lijuang2008ff22016-03-07 17:56:27 +08002958 goto boot_failed;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002959 }
2960
Amol Jadicb524072012-08-09 16:40:18 -07002961#if DEVICE_TREE
Matthew Qin271927e2015-03-31 22:07:07 -04002962 scratch_offset = image_actual + page_size + out_len;
Amol Jadicb524072012-08-09 16:40:18 -07002963 /* find correct dtb and copy it to right location */
Matthew Qin271927e2015-03-31 22:07:07 -04002964 ret = copy_dtb(data, scratch_offset);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002965
2966 dtb_copied = !ret ? 1 : 0;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002967#else
Mayank Grovere559cec2017-10-17 15:12:03 +05302968 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE) ||
2969 check_ddr_addr_range_bound(hdr->tags_addr, MAX_TAGS_SIZE))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002970 {
Mayank Grovere559cec2017-10-17 15:12:03 +05302971 dprintf(CRITICAL, "Tags addresses are not valid.\n");
lijuang2008ff22016-03-07 17:56:27 +08002972 goto boot_failed;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002973 }
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002974#endif
2975
2976 /* Load ramdisk & kernel */
2977 memmove((void*) hdr->ramdisk_addr, ptr + page_size + kernel_actual, hdr->ramdisk_size);
Matthew Qinbb7923d2015-02-09 10:56:09 +08002978 memmove((void*) hdr->kernel_addr, (char*) (kernel_start_addr), kernel_size);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002979
2980#if DEVICE_TREE
Mayank Grovere559cec2017-10-17 15:12:03 +05302981 if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual) ||
2982 check_ddr_addr_range_bound(hdr->tags_addr, kernel_actual))
Matthew Qinbb7923d2015-02-09 10:56:09 +08002983 {
Mayank Grovere559cec2017-10-17 15:12:03 +05302984 dprintf(CRITICAL, "Tags addresses are not valid.\n");
lijuang2008ff22016-03-07 17:56:27 +08002985 goto boot_failed;
Matthew Qinbb7923d2015-02-09 10:56:09 +08002986 }
2987
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002988 /*
2989 * If dtb is not found look for appended DTB in the kernel.
2990 * If appended dev tree is found, update the atags with
2991 * memory address to the DTB appended location on RAM.
2992 * Else update with the atags address in the kernel header
2993 */
2994 if (!dtb_copied) {
2995 void *dtb;
Matthew Qinbb7923d2015-02-09 10:56:09 +08002996 dtb = dev_tree_appended((void*)(ptr + page_size),
2997 hdr->kernel_size, dtb_offset,
Dima Zavine63e5572013-05-03 12:23:06 -07002998 (void *)hdr->tags_addr);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002999 if (!dtb) {
3000 fastboot_fail("dtb not found");
lijuang2008ff22016-03-07 17:56:27 +08003001 goto boot_failed;
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07003002 }
Amol Jadicb524072012-08-09 16:40:18 -07003003 }
3004#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08003005
3006 fastboot_okay("");
Amol Jadi7c4316c2013-10-07 14:19:26 -07003007 fastboot_stop();
Brian Swetland9c4c0752009-01-25 16:23:50 -08003008
Dima Zavin77e41f32013-03-06 16:10:43 -08003009 boot_linux((void*) hdr->kernel_addr, (void*) hdr->tags_addr,
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07003010 (const char*) hdr->cmdline, board_machtype(),
3011 (void*) hdr->ramdisk_addr, hdr->ramdisk_size);
lijuang2008ff22016-03-07 17:56:27 +08003012
3013 /* fastboot already stop, it's no need to show fastboot menu */
3014 return;
3015boot_failed:
3016#if FBCON_DISPLAY_MSG
3017 /* revert to fastboot menu if boot failed */
3018 display_fastboot_menu();
3019#endif
3020 return;
Brian Swetland9c4c0752009-01-25 16:23:50 -08003021}
3022
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003023void cmd_erase_nand(const char *arg, void *data, unsigned sz)
Dima Zavin214cc642009-01-26 11:16:21 -08003024{
3025 struct ptentry *ptn;
3026 struct ptable *ptable;
3027
3028 ptable = flash_get_ptable();
3029 if (ptable == NULL) {
3030 fastboot_fail("partition table doesn't exist");
3031 return;
3032 }
3033
3034 ptn = ptable_find(ptable, arg);
3035 if (ptn == NULL) {
3036 fastboot_fail("unknown partition name");
3037 return;
3038 }
3039
Monika Singh292b3e92018-03-17 22:40:23 +05303040 if (!strncmp(arg, "avb_custom_key", strlen("avb_custom_key"))) {
3041 dprintf(INFO, "erasing avb_custom_key\n");
3042 if (erase_userkey()) {
3043 fastboot_fail("Erasing avb_custom_key failed");
3044 } else {
3045 fastboot_okay("");
3046 }
3047 return;
3048 }
3049
Dima Zavin214cc642009-01-26 11:16:21 -08003050 if (flash_erase(ptn)) {
3051 fastboot_fail("failed to erase partition");
3052 return;
3053 }
3054 fastboot_okay("");
3055}
3056
Bikas Gurungd48bd242010-09-04 19:54:32 -07003057
3058void cmd_erase_mmc(const char *arg, void *data, unsigned sz)
3059{
3060 unsigned long long ptn = 0;
Oliver Wangcee448d2013-10-22 18:40:13 +08003061 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07003062 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003063 uint8_t lun = 0;
Vijay Kumar Pendoti0b21f462016-05-02 17:09:18 +05303064 char *footer = NULL;
Bikas Gurungd48bd242010-09-04 19:54:32 -07003065
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003066#if VERIFIED_BOOT
3067 if(!strcmp(arg, KEYSTORE_PTN_NAME))
3068 {
3069 if(!device.is_unlocked)
3070 {
3071 fastboot_fail("unlock device to erase keystore");
3072 return;
3073 }
3074 }
3075#endif
3076
Kinson Chikf1a43512011-07-14 11:28:39 -07003077 index = partition_get_index(arg);
3078 ptn = partition_get_offset(index);
Oliver Wangcee448d2013-10-22 18:40:13 +08003079 size = partition_get_size(index);
Neeti Desaica8c9602011-10-06 11:40:00 -07003080
Monika Singhc4778b72018-05-16 11:16:42 +05303081 if (!strncmp(arg, "avb_custom_key", strlen("avb_custom_key"))) {
3082 dprintf(INFO, "erasing avb_custom_key\n");
3083 if (erase_userkey()) {
3084 fastboot_fail("Erasing avb_custom_key failed");
3085 } else {
3086 fastboot_okay("");
3087 }
3088 return;
3089 }
3090
Kinson Chikf1a43512011-07-14 11:28:39 -07003091 if(ptn == 0) {
Neeti Desaica8c9602011-10-06 11:40:00 -07003092 fastboot_fail("Partition table doesn't exist\n");
Bikas Gurungd48bd242010-09-04 19:54:32 -07003093 return;
3094 }
Kun Liang2f1601a2013-08-12 16:29:54 +08003095
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003096 lun = partition_get_lun(index);
3097 mmc_set_lun(lun);
3098
Sridhar Parasuramd7957122015-02-27 11:33:40 -08003099 if (platform_boot_dev_isemmc())
3100 {
3101 if (mmc_erase_card(ptn, size)) {
3102 fastboot_fail("failed to erase partition\n");
3103 return;
3104 }
3105 } else {
3106 BUF_DMA_ALIGN(out, DEFAULT_ERASE_SIZE);
3107 size = partition_get_size(index);
3108 if (size > DEFAULT_ERASE_SIZE)
3109 size = DEFAULT_ERASE_SIZE;
Kun Liang2f1601a2013-08-12 16:29:54 +08003110
Sridhar Parasuramd7957122015-02-27 11:33:40 -08003111 /* Simple inefficient version of erase. Just writing
3112 0 in first several blocks */
3113 if (mmc_write(ptn , size, (unsigned int *)out)) {
3114 fastboot_fail("failed to erase partition");
3115 return;
3116 }
Vijay Kumar Pendoti0b21f462016-05-02 17:09:18 +05303117 /*Erase FDE metadata at the userdata footer*/
3118 if(!(strncmp(arg, "userdata", 8)))
3119 {
3120 footer = memalign(CACHE_LINE, FOOTER_SIZE);
3121 memset((void *)footer, 0, FOOTER_SIZE);
3122
3123 size = partition_get_size(index);
3124
3125 if (mmc_write((ptn + size) - FOOTER_SIZE , FOOTER_SIZE, (unsigned int *)footer)) {
3126 fastboot_fail("failed to erase userdata footer");
3127 free(footer);
3128 return;
3129 }
3130 free(footer);
3131 }
Bikas Gurungd48bd242010-09-04 19:54:32 -07003132 }
Monika Singh292b3e92018-03-17 22:40:23 +05303133#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover912eaa62017-10-26 12:08:53 +05303134 if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05303135 !(strncmp(arg, "userdata", 8)) &&
3136 send_delete_keys_to_tz())
Sridhar Parasuram32b30662015-07-10 13:33:22 -07003137 ASSERT(0);
3138#endif
Bikas Gurungd48bd242010-09-04 19:54:32 -07003139 fastboot_okay("");
3140}
3141
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003142void cmd_erase(const char *arg, void *data, unsigned sz)
3143{
Monika Singh292b3e92018-03-17 22:40:23 +05303144#if VERIFIED_BOOT || VERIFIED_BOOT_2
Channagoud Kadabi35297672015-06-13 11:09:49 -07003145 if (target_build_variant_user())
Channagoud Kadabice3cf422015-04-17 16:02:30 -07003146 {
Sridhar Parasuramc32d07d2015-07-12 10:57:48 -07003147 if(!device.is_unlocked)
Channagoud Kadabice3cf422015-04-17 16:02:30 -07003148 {
Channagoud Kadabi35297672015-06-13 11:09:49 -07003149 fastboot_fail("device is locked. Cannot erase");
Channagoud Kadabice3cf422015-04-17 16:02:30 -07003150 return;
3151 }
3152 }
3153#endif
3154
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003155 if(target_is_emmc_boot())
3156 cmd_erase_mmc(arg, data, sz);
3157 else
3158 cmd_erase_nand(arg, data, sz);
3159}
Bikas Gurungd48bd242010-09-04 19:54:32 -07003160
Mayank Grover11ff9692018-01-11 11:54:49 +05303161/* Get the size from partiton name */
3162static void get_partition_size(const char *arg, char *response)
3163{
3164 uint64_t ptn = 0;
3165 uint64_t size;
3166 int index = INVALID_PTN;
3167
3168 index = partition_get_index(arg);
3169
3170 if (index == INVALID_PTN)
3171 {
3172 dprintf(CRITICAL, "Invalid partition index\n");
3173 return;
3174 }
3175
3176 ptn = partition_get_offset(index);
3177
3178 if(!ptn)
3179 {
3180 dprintf(CRITICAL, "Invalid partition name %s\n", arg);
3181 return;
3182 }
3183
3184 size = partition_get_size(index);
3185
3186 snprintf(response, MAX_RSP_SIZE, "\t 0x%llx", size);
3187 return;
3188}
3189
Mayank Grover52cd10a2018-03-15 12:57:54 +05303190/* Function to check partition type of a partition*/
3191static fs_signature_type
3192check_partition_fs_signature(const char *arg)
3193{
3194 fs_signature_type ret = NO_FS;
3195 int index;
3196 unsigned long long ptn;
Mayank Grover399826a2018-08-27 12:15:15 +05303197 char *buffer = memalign(CACHE_LINE, mmc_blocksize);
3198 uint32_t sb_blk_offset = 0;
3199 char *sb_buffer = buffer;
3200
Mayank Grover52cd10a2018-03-15 12:57:54 +05303201 if (!sb_buffer)
3202 {
3203 dprintf(CRITICAL, "ERROR: Failed to allocate buffer for superblock\n");
3204 goto out;
3205 }
3206
3207 /* Read super block */
3208 if ((index = partition_get_index(arg)) < 0)
3209 {
3210 dprintf(CRITICAL, "ERROR: %s() doesn't exsit\n", arg);
3211 goto out;
3212 }
3213 ptn = partition_get_offset(index);
3214 mmc_set_lun(partition_get_lun(index));
Mayank Grover399826a2018-08-27 12:15:15 +05303215 sb_blk_offset = (FS_SUPERBLOCK_OFFSET/mmc_blocksize);
3216
3217 if(mmc_read(ptn + (sb_blk_offset * mmc_blocksize),
Mayank Grover52cd10a2018-03-15 12:57:54 +05303218 (void *)sb_buffer, mmc_blocksize))
3219 {
3220 dprintf(CRITICAL, "ERROR: Failed to read Superblock\n");
3221 goto out;
3222 }
3223
Mayank Grover399826a2018-08-27 12:15:15 +05303224 if (sb_blk_offset == 0)
3225 sb_buffer += FS_SUPERBLOCK_OFFSET;
3226
3227 if (*((uint16 *)(&sb_buffer[EXT_MAGIC_OFFSET_SB])) == (uint16)EXT_MAGIC)
Mayank Grover52cd10a2018-03-15 12:57:54 +05303228 {
3229 dprintf(SPEW, "%s() Found EXT FS\n", arg);
3230 ret = EXT_FS_SIGNATURE;
3231 }
Mayank Grover399826a2018-08-27 12:15:15 +05303232 else if (*((uint32 *)(&sb_buffer[F2FS_MAGIC_OFFSET_SB])) == F2FS_MAGIC)
Mayank Grover52cd10a2018-03-15 12:57:54 +05303233 {
3234 dprintf(SPEW, "%s() Found F2FS FS\n", arg);
3235 ret = EXT_F2FS_SIGNATURE;
3236 }
3237 else
3238 {
3239 dprintf(SPEW, "%s() Reverting to default 0x%x\n",
3240 arg, *((uint16 *)(&sb_buffer[EXT_MAGIC_OFFSET_SB])));
3241 ret = NO_FS;
3242 }
3243
3244out:
Mayank Grover399826a2018-08-27 12:15:15 +05303245 if(buffer)
3246 free(buffer);
Mayank Grover52cd10a2018-03-15 12:57:54 +05303247 return ret;
3248}
3249
Mayank Groverd38fe012018-03-13 15:33:16 +05303250/* Function to get partition type */
3251static void get_partition_type(const char *arg, char *response)
3252{
3253 uint n = 0;
Mayank Grover52cd10a2018-03-15 12:57:54 +05303254 fs_signature_type fs_signature;
Mayank Groverd38fe012018-03-13 15:33:16 +05303255
3256 if (arg == NULL ||
3257 response == NULL)
3258 {
3259 dprintf(CRITICAL, "Invalid input parameter\n");
3260 return;
3261 }
3262
3263 /* By default copy raw to response */
Mayank Grover0e559042018-04-11 17:49:30 +05303264 strlcpy(response, RAW_STR, MAX_RSP_SIZE);
Mayank Groverd38fe012018-03-13 15:33:16 +05303265
3266 /* Mark partiton type for known paritions only */
3267 for (n=0; n < ARRAY_SIZE(part_type_known); n++)
3268 {
3269 if (!strncmp(part_type_known[n].part_name, arg, strlen(arg)))
3270 {
Mayank Grover52cd10a2018-03-15 12:57:54 +05303271 /* Check partition for FS signature */
3272 fs_signature = check_partition_fs_signature(arg);
3273 switch (fs_signature)
3274 {
3275 case EXT_FS_SIGNATURE:
Mayank Grover0e559042018-04-11 17:49:30 +05303276 strlcpy(response, EXT_STR, MAX_RSP_SIZE);
Mayank Grover52cd10a2018-03-15 12:57:54 +05303277 break;
3278 case EXT_F2FS_SIGNATURE:
Mayank Grover0e559042018-04-11 17:49:30 +05303279 strlcpy(response, F2FS_STR, MAX_RSP_SIZE);
Mayank Grover52cd10a2018-03-15 12:57:54 +05303280 break;
3281 case NO_FS:
Mayank Grover0e559042018-04-11 17:49:30 +05303282 strlcpy(response, part_type_known[n].type_response, MAX_RSP_SIZE);
Mayank Grover52cd10a2018-03-15 12:57:54 +05303283 }
Mayank Groverd38fe012018-03-13 15:33:16 +05303284 }
3285 }
3286 return;
3287}
3288
Mayank Grover11ff9692018-01-11 11:54:49 +05303289/*
3290 * Publish the partition type & size info
3291 * fastboot getvar will publish the required information.
3292 * fastboot getvar partition_size:<partition_name>: partition size in hex
3293 * fastboot getvar partition_type:<partition_name>: partition type (ext/fat)
3294 */
3295static void publish_getvar_partition_info(struct getvar_partition_info *info, uint8_t num_parts)
3296{
Mayank Groverd38fe012018-03-13 15:33:16 +05303297 uint8_t i;
Mayank Grover11ff9692018-01-11 11:54:49 +05303298 static bool published = false;
3299 struct partition_entry *ptn_entry =
3300 partition_get_partition_entries();
3301 memset(info, 0, sizeof(struct getvar_partition_info)* num_parts);
3302
3303 for (i = 0; i < num_parts; i++) {
3304 strlcat(info[i].part_name, (const char *)ptn_entry[i].name, MAX_RSP_SIZE);
3305 strlcat(info[i].getvar_size, "partition-size:", MAX_GET_VAR_NAME_SIZE);
3306 strlcat(info[i].getvar_type, "partition-type:", MAX_GET_VAR_NAME_SIZE);
3307
Mayank Groverd38fe012018-03-13 15:33:16 +05303308 get_partition_type(info[i].part_name, info[i].type_response);
Mayank Grover11ff9692018-01-11 11:54:49 +05303309 get_partition_size(info[i].part_name, info[i].size_response);
Mayank Grover11ff9692018-01-11 11:54:49 +05303310 if (strlcat(info[i].getvar_size, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
3311 {
3312 dprintf(CRITICAL, "partition size name truncated\n");
3313 return;
3314 }
3315 if (strlcat(info[i].getvar_type, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
3316 {
3317 dprintf(CRITICAL, "partition type name truncated\n");
3318 return;
3319 }
3320
3321 if (!published)
3322 {
3323 /* publish partition size & type info */
3324 fastboot_publish((const char *) info[i].getvar_size, (const char *) info[i].size_response);
3325 fastboot_publish((const char *) info[i].getvar_type, (const char *) info[i].type_response);
3326 }
3327 }
3328 if (!published)
3329 published = true;
3330}
3331
3332
Ajay Dudani5c761132011-04-07 20:19:04 -07003333void cmd_flash_mmc_img(const char *arg, void *data, unsigned sz)
Shashank Mittal23b8f422010-04-16 19:27:21 -07003334{
3335 unsigned long long ptn = 0;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07003336 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07003337 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003338 char *token = NULL;
3339 char *pname = NULL;
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07003340 char *sp;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003341 uint8_t lun = 0;
3342 bool lun_set = false;
Mayank Grover351a75e2017-05-30 20:06:08 +05303343 int current_active_slot = INVALID;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07003344
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07003345 token = strtok_r((char *)arg, ":", &sp);
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003346 pname = token;
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07003347 token = strtok_r(NULL, ":", &sp);
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003348 if(token)
3349 {
3350 lun = atoi(token);
3351 mmc_set_lun(lun);
3352 lun_set = true;
3353 }
3354
Mao Jinlong226f33a2014-07-04 17:24:10 +08003355 if (pname)
Greg Grisco6e754772011-06-23 12:19:39 -07003356 {
Channagoud Kadabiad259832015-05-29 11:14:17 -07003357 if (!strncmp(pname, "frp-unlock", strlen("frp-unlock")))
3358 {
3359 if (!aboot_frp_unlock(pname, data, sz))
3360 {
3361 fastboot_info("FRP unlock successful");
3362 fastboot_okay("");
3363 }
3364 else
3365 fastboot_fail("Secret key is invalid, please update the bootloader with secret key");
3366
3367 return;
3368 }
3369
Mao Jinlong226f33a2014-07-04 17:24:10 +08003370 if (!strcmp(pname, "partition"))
3371 {
3372 dprintf(INFO, "Attempt to write partition image.\n");
3373 if (write_partition(sz, (unsigned char *) data)) {
3374 fastboot_fail("failed to write partition");
Greg Grisco6e754772011-06-23 12:19:39 -07003375 return;
3376 }
Mayank Grover11ff9692018-01-11 11:54:49 +05303377 /* Re-publish partition table */
3378 publish_getvar_partition_info(part_info, partition_get_partition_count());
Mayank Grover351a75e2017-05-30 20:06:08 +05303379
3380 /* Rescan partition table to ensure we have multislot support*/
3381 if (partition_scan_for_multislot())
3382 {
3383 current_active_slot = partition_find_active_slot();
3384 dprintf(INFO, "Multislot supported: Slot %s active",
3385 (SUFFIX_SLOT(current_active_slot)));
3386 }
3387 partition_mark_active_slot(current_active_slot);
Greg Grisco6e754772011-06-23 12:19:39 -07003388 }
Mao Jinlong226f33a2014-07-04 17:24:10 +08003389 else
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003390 {
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003391#if VERIFIED_BOOT
3392 if(!strcmp(pname, KEYSTORE_PTN_NAME))
3393 {
3394 if(!device.is_unlocked)
3395 {
3396 fastboot_fail("unlock device to flash keystore");
3397 return;
3398 }
Gaurav Nebhwanic7313cc2015-12-15 22:25:04 +05303399 if(!boot_verify_validate_keystore((unsigned char *)data,sz))
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003400 {
3401 fastboot_fail("image is not a keystore file");
3402 return;
3403 }
3404 }
3405#endif
Mao Jinlong226f33a2014-07-04 17:24:10 +08003406 index = partition_get_index(pname);
3407 ptn = partition_get_offset(index);
3408 if(ptn == 0) {
3409 fastboot_fail("partition table doesn't exist");
3410 return;
3411 }
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003412
Mayank Grover351a75e2017-05-30 20:06:08 +05303413 if (!strncmp(pname, "boot", strlen("boot"))
3414 || !strcmp(pname, "recovery"))
3415 {
Mao Jinlong226f33a2014-07-04 17:24:10 +08003416 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
3417 fastboot_fail("image is not a boot image");
3418 return;
3419 }
Mayank Grover351a75e2017-05-30 20:06:08 +05303420
3421 /* Reset multislot_partition attributes in case of flashing boot */
3422 if (partition_multislot_is_supported())
3423 {
3424 partition_reset_attributes(index);
3425 }
Mao Jinlong226f33a2014-07-04 17:24:10 +08003426 }
3427
3428 if(!lun_set)
3429 {
3430 lun = partition_get_lun(index);
3431 mmc_set_lun(lun);
3432 }
3433
3434 size = partition_get_size(index);
Gaurav Nebhwani4d2389c2016-03-17 21:10:05 +05303435 if (ROUND_TO_PAGE(sz, mmc_blocksize_mask) > size) {
Mao Jinlong226f33a2014-07-04 17:24:10 +08003436 fastboot_fail("size too large");
3437 return;
3438 }
3439 else if (mmc_write(ptn , sz, (unsigned int *)data)) {
3440 fastboot_fail("flash write failure");
3441 return;
3442 }
Greg Grisco6e754772011-06-23 12:19:39 -07003443 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07003444 }
3445 fastboot_okay("");
3446 return;
3447}
3448
Ajay Dudanide984792015-03-02 09:57:41 -08003449void cmd_flash_meta_img(const char *arg, void *data, unsigned sz)
3450{
3451 int i, images;
3452 meta_header_t *meta_header;
3453 img_header_entry_t *img_header_entry;
Parth Dixit3e2d3032016-03-04 17:11:52 +05303454 /*End of the image address*/
3455 uintptr_t data_end;
3456
3457 if( (UINT_MAX - sz) > (uintptr_t)data )
3458 data_end = (uintptr_t)data + sz;
3459 else
3460 {
3461 fastboot_fail("Cannot flash: image header corrupt");
3462 return;
3463 }
3464
3465 if( data_end < ((uintptr_t)data + sizeof(meta_header_t)))
3466 {
3467 fastboot_fail("Cannot flash: image header corrupt");
3468 return;
3469 }
Ajay Dudanide984792015-03-02 09:57:41 -08003470
lijuang8ee21882016-04-19 16:57:11 +08003471 /* If device is locked:
3472 * Forbid to flash image to avoid the device to bypass the image
3473 * which with "any" name other than bootloader. Because it maybe
3474 * a meta package of all partitions.
3475 */
Monika Singh292b3e92018-03-17 22:40:23 +05303476#if VERIFIED_BOOT || VERIFIED_BOOT_2
lijuang8ee21882016-04-19 16:57:11 +08003477 if (target_build_variant_user()) {
3478 if (!device.is_unlocked) {
3479 fastboot_fail("Device is locked, meta image flashing is not allowed");
3480 return;
3481 }
Mayank Grover889be1b2017-09-12 20:12:23 +05303482
Mayank Grover912eaa62017-10-26 12:08:53 +05303483 if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05303484 !device.is_unlock_critical)
3485 {
lijuang8ee21882016-04-19 16:57:11 +08003486 fastboot_fail("Device is critical locked, Meta image flashing is not allowed");
3487 return;
3488 }
lijuang8ee21882016-04-19 16:57:11 +08003489 }
3490#endif
3491
Ajay Dudanide984792015-03-02 09:57:41 -08003492 meta_header = (meta_header_t*) data;
Parth Dixit3e2d3032016-03-04 17:11:52 +05303493 if( data_end < ((uintptr_t)data + meta_header->img_hdr_sz))
3494 {
3495 fastboot_fail("Cannot flash: image header corrupt");
3496 return;
3497 }
Ajay Dudanide984792015-03-02 09:57:41 -08003498 img_header_entry = (img_header_entry_t*) (data+sizeof(meta_header_t));
3499
3500 images = meta_header->img_hdr_sz / sizeof(img_header_entry_t);
3501
3502 for (i=0; i<images; i++) {
3503
3504 if((img_header_entry[i].ptn_name == NULL) ||
3505 (img_header_entry[i].start_offset == 0) ||
3506 (img_header_entry[i].size == 0))
3507 break;
Kishor PK49831502017-04-21 17:55:43 +05303508 if ((UINT_MAX - img_header_entry[i].start_offset) < (uintptr_t)data) {
3509 fastboot_fail("Integer overflow detected in start_offset of img");
3510 break;
3511 }
3512 else if ((UINT_MAX - (img_header_entry[i].start_offset + (uintptr_t)data)) < img_header_entry[i].size) {
3513 fastboot_fail("Integer overflow detected in size of img");
3514 break;
3515 }
Parth Dixit3e2d3032016-03-04 17:11:52 +05303516 if( data_end < ((uintptr_t)data + img_header_entry[i].start_offset
3517 + img_header_entry[i].size) )
3518 {
3519 fastboot_fail("Cannot flash: image size mismatch");
3520 break;
3521 }
3522
Ajay Dudanide984792015-03-02 09:57:41 -08003523 cmd_flash_mmc_img(img_header_entry[i].ptn_name,
3524 (void *) data + img_header_entry[i].start_offset,
3525 img_header_entry[i].size);
3526 }
3527
Channagoud Kadabiaafbbfc2015-03-04 17:52:56 -08003528 if (!strncmp(arg, "bootloader", strlen("bootloader")))
3529 {
3530 strlcpy(device.bootloader_version, TARGET(BOARD), MAX_VERSION_LEN);
3531 strlcat(device.bootloader_version, "-", MAX_VERSION_LEN);
3532 strlcat(device.bootloader_version, meta_header->img_version, MAX_VERSION_LEN);
3533 }
3534 else
3535 {
3536 strlcpy(device.radio_version, TARGET(BOARD), MAX_VERSION_LEN);
3537 strlcat(device.radio_version, "-", MAX_VERSION_LEN);
3538 strlcat(device.radio_version, meta_header->img_version, MAX_VERSION_LEN);
3539 }
3540
3541 write_device_info(&device);
Ajay Dudanide984792015-03-02 09:57:41 -08003542 fastboot_okay("");
3543 return;
3544}
3545
Ajay Dudani5c761132011-04-07 20:19:04 -07003546void cmd_flash_mmc_sparse_img(const char *arg, void *data, unsigned sz)
3547{
3548 unsigned int chunk;
wufeng.jiang813dc352015-06-02 23:02:46 -04003549 uint64_t chunk_data_sz;
Channagoud Kadabif627cf82013-09-09 14:08:20 -07003550 uint32_t *fill_buf = NULL;
3551 uint32_t fill_val;
Mayank Grover4f50bba2017-07-19 18:17:08 +05303552 uint32_t blk_sz_actual = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07003553 sparse_header_t *sparse_header;
3554 chunk_header_t *chunk_header;
Ajay Dudaniab18f022011-05-12 14:39:22 -07003555 uint32_t total_blocks = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07003556 unsigned long long ptn = 0;
Channagoud Kadabi65b91002011-10-11 17:34:33 +05303557 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07003558 int index = INVALID_PTN;
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05303559 uint32_t i;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003560 uint8_t lun = 0;
vijay kumar800255e2015-04-24 20:26:19 +05303561 /*End of the sparse image address*/
Parth Dixit64b1a482016-03-07 16:31:26 +05303562 uintptr_t data_end = (uintptr_t)data + sz;
Ajay Dudani5c761132011-04-07 20:19:04 -07003563
Kinson Chikf1a43512011-07-14 11:28:39 -07003564 index = partition_get_index(arg);
3565 ptn = partition_get_offset(index);
3566 if(ptn == 0) {
Ajay Dudani5c761132011-04-07 20:19:04 -07003567 fastboot_fail("partition table doesn't exist");
3568 return;
3569 }
3570
Channagoud Kadabi65b91002011-10-11 17:34:33 +05303571 size = partition_get_size(index);
Channagoud Kadabi65b91002011-10-11 17:34:33 +05303572
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003573 lun = partition_get_lun(index);
3574 mmc_set_lun(lun);
3575
vijay kumar800255e2015-04-24 20:26:19 +05303576 if (sz < sizeof(sparse_header_t)) {
3577 fastboot_fail("size too low");
3578 return;
3579 }
3580
Ajay Dudani5c761132011-04-07 20:19:04 -07003581 /* Read and skip over sparse image header */
3582 sparse_header = (sparse_header_t *) data;
vijay kumar800255e2015-04-24 20:26:19 +05303583
Mayank Grover48bd9bb2017-07-19 12:04:16 +05303584 if (!sparse_header->blk_sz || (sparse_header->blk_sz % 4)){
3585 fastboot_fail("Invalid block size\n");
3586 return;
3587 }
3588
vijay kumar1321f342015-03-27 12:13:42 +05303589 if (((uint64_t)sparse_header->total_blks * (uint64_t)sparse_header->blk_sz) > size) {
Ajay Dudani876b3282012-12-21 14:12:17 -08003590 fastboot_fail("size too large");
3591 return;
3592 }
3593
vijay kumarde4fcf62015-04-23 13:05:49 +05303594 data += sizeof(sparse_header_t);
vijay kumar800255e2015-04-24 20:26:19 +05303595
Parth Dixit64b1a482016-03-07 16:31:26 +05303596 if (data_end < (uintptr_t)data) {
vijay kumar800255e2015-04-24 20:26:19 +05303597 fastboot_fail("buffer overreads occured due to invalid sparse header");
3598 return;
3599 }
3600
vijay kumarde4fcf62015-04-23 13:05:49 +05303601 if(sparse_header->file_hdr_sz != sizeof(sparse_header_t))
Ajay Dudani5c761132011-04-07 20:19:04 -07003602 {
vijay kumarde4fcf62015-04-23 13:05:49 +05303603 fastboot_fail("sparse header size mismatch");
3604 return;
Ajay Dudani5c761132011-04-07 20:19:04 -07003605 }
3606
Ajay Dudanib06c05f2011-05-12 14:46:10 -07003607 dprintf (SPEW, "=== Sparse Image Header ===\n");
3608 dprintf (SPEW, "magic: 0x%x\n", sparse_header->magic);
3609 dprintf (SPEW, "major_version: 0x%x\n", sparse_header->major_version);
3610 dprintf (SPEW, "minor_version: 0x%x\n", sparse_header->minor_version);
3611 dprintf (SPEW, "file_hdr_sz: %d\n", sparse_header->file_hdr_sz);
3612 dprintf (SPEW, "chunk_hdr_sz: %d\n", sparse_header->chunk_hdr_sz);
3613 dprintf (SPEW, "blk_sz: %d\n", sparse_header->blk_sz);
3614 dprintf (SPEW, "total_blks: %d\n", sparse_header->total_blks);
3615 dprintf (SPEW, "total_chunks: %d\n", sparse_header->total_chunks);
Ajay Dudani5c761132011-04-07 20:19:04 -07003616
3617 /* Start processing chunks */
3618 for (chunk=0; chunk<sparse_header->total_chunks; chunk++)
3619 {
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05303620 /* Make sure the total image size does not exceed the partition size */
3621 if(((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz) >= size) {
3622 fastboot_fail("size too large");
3623 return;
3624 }
Ajay Dudani5c761132011-04-07 20:19:04 -07003625 /* Read and skip over chunk header */
3626 chunk_header = (chunk_header_t *) data;
3627 data += sizeof(chunk_header_t);
3628
Parth Dixit64b1a482016-03-07 16:31:26 +05303629 if (data_end < (uintptr_t)data) {
vijay kumar800255e2015-04-24 20:26:19 +05303630 fastboot_fail("buffer overreads occured due to invalid sparse header");
3631 return;
3632 }
3633
Ajay Dudani5c761132011-04-07 20:19:04 -07003634 dprintf (SPEW, "=== Chunk Header ===\n");
3635 dprintf (SPEW, "chunk_type: 0x%x\n", chunk_header->chunk_type);
3636 dprintf (SPEW, "chunk_data_sz: 0x%x\n", chunk_header->chunk_sz);
3637 dprintf (SPEW, "total_size: 0x%x\n", chunk_header->total_sz);
3638
vijay kumar800255e2015-04-24 20:26:19 +05303639 if(sparse_header->chunk_hdr_sz != sizeof(chunk_header_t))
Ajay Dudani5c761132011-04-07 20:19:04 -07003640 {
vijay kumar800255e2015-04-24 20:26:19 +05303641 fastboot_fail("chunk header size mismatch");
3642 return;
Ajay Dudani5c761132011-04-07 20:19:04 -07003643 }
3644
wufeng.jiang813dc352015-06-02 23:02:46 -04003645 chunk_data_sz = (uint64_t)sparse_header->blk_sz * chunk_header->chunk_sz;
3646
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05303647 /* Make sure that the chunk size calculated from sparse image does not
3648 * exceed partition size
3649 */
3650 if ((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz + chunk_data_sz > size)
3651 {
3652 fastboot_fail("Chunk data size exceeds partition size");
3653 return;
3654 }
3655
Ajay Dudani5c761132011-04-07 20:19:04 -07003656 switch (chunk_header->chunk_type)
3657 {
3658 case CHUNK_TYPE_RAW:
wufeng.jiang813dc352015-06-02 23:02:46 -04003659 if((uint64_t)chunk_header->total_sz != ((uint64_t)sparse_header->chunk_hdr_sz +
Ajay Dudani5c761132011-04-07 20:19:04 -07003660 chunk_data_sz))
3661 {
3662 fastboot_fail("Bogus chunk size for chunk type Raw");
3663 return;
3664 }
3665
Parth Dixit64b1a482016-03-07 16:31:26 +05303666 if (data_end < (uintptr_t)data + chunk_data_sz) {
vijay kumar800255e2015-04-24 20:26:19 +05303667 fastboot_fail("buffer overreads occured due to invalid sparse header");
3668 return;
3669 }
3670
wufeng.jiang813dc352015-06-02 23:02:46 -04003671 /* chunk_header->total_sz is uint32,So chunk_data_sz is now less than 2^32
3672 otherwise it will return in the line above
3673 */
Ajay Dudaniab18f022011-05-12 14:39:22 -07003674 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
wufeng.jiang813dc352015-06-02 23:02:46 -04003675 (uint32_t)chunk_data_sz,
Ajay Dudaniab18f022011-05-12 14:39:22 -07003676 (unsigned int*)data))
Ajay Dudani5c761132011-04-07 20:19:04 -07003677 {
3678 fastboot_fail("flash write failure");
3679 return;
3680 }
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05303681 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
3682 fastboot_fail("Bogus size for RAW chunk type");
3683 return;
3684 }
Ajay Dudani5c761132011-04-07 20:19:04 -07003685 total_blocks += chunk_header->chunk_sz;
wufeng.jiang813dc352015-06-02 23:02:46 -04003686 data += (uint32_t)chunk_data_sz;
Ajay Dudani5c761132011-04-07 20:19:04 -07003687 break;
3688
Channagoud Kadabif627cf82013-09-09 14:08:20 -07003689 case CHUNK_TYPE_FILL:
3690 if(chunk_header->total_sz != (sparse_header->chunk_hdr_sz +
3691 sizeof(uint32_t)))
3692 {
3693 fastboot_fail("Bogus chunk size for chunk type FILL");
3694 return;
3695 }
3696
Mayank Grover4f50bba2017-07-19 18:17:08 +05303697 blk_sz_actual = ROUNDUP(sparse_header->blk_sz, CACHE_LINE);
3698 /* Integer overflow detected */
3699 if (blk_sz_actual < sparse_header->blk_sz)
3700 {
3701 fastboot_fail("Invalid block size");
3702 return;
3703 }
3704
3705 fill_buf = (uint32_t *)memalign(CACHE_LINE, blk_sz_actual);
Channagoud Kadabif627cf82013-09-09 14:08:20 -07003706 if (!fill_buf)
3707 {
3708 fastboot_fail("Malloc failed for: CHUNK_TYPE_FILL");
3709 return;
3710 }
3711
Parth Dixit64b1a482016-03-07 16:31:26 +05303712 if (data_end < (uintptr_t)data + sizeof(uint32_t)) {
vijay kumar800255e2015-04-24 20:26:19 +05303713 fastboot_fail("buffer overreads occured due to invalid sparse header");
Gaurav Nebhwani163cbf82016-03-17 21:21:27 +05303714 free(fill_buf);
vijay kumar800255e2015-04-24 20:26:19 +05303715 return;
3716 }
Channagoud Kadabif627cf82013-09-09 14:08:20 -07003717 fill_val = *(uint32_t *)data;
3718 data = (char *) data + sizeof(uint32_t);
Channagoud Kadabif627cf82013-09-09 14:08:20 -07003719
3720 for (i = 0; i < (sparse_header->blk_sz / sizeof(fill_val)); i++)
3721 {
3722 fill_buf[i] = fill_val;
3723 }
3724
Gaurav Nebhwanie94cbe12016-03-17 21:16:34 +05303725 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz))
3726 {
3727 fastboot_fail("bogus size for chunk FILL type");
3728 free(fill_buf);
3729 return;
3730 }
3731
wufeng.jiang813dc352015-06-02 23:02:46 -04003732 for (i = 0; i < chunk_header->chunk_sz; i++)
Channagoud Kadabif627cf82013-09-09 14:08:20 -07003733 {
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05303734 /* Make sure that the data written to partition does not exceed partition size */
3735 if ((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz + sparse_header->blk_sz > size)
3736 {
3737 fastboot_fail("Chunk data size for fill type exceeds partition size");
Gaurav Nebhwani163cbf82016-03-17 21:21:27 +05303738 free(fill_buf);
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05303739 return;
3740 }
3741
Channagoud Kadabif627cf82013-09-09 14:08:20 -07003742 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
3743 sparse_header->blk_sz,
3744 fill_buf))
3745 {
3746 fastboot_fail("flash write failure");
3747 free(fill_buf);
3748 return;
3749 }
3750
3751 total_blocks++;
3752 }
3753
3754 free(fill_buf);
3755 break;
3756
Ajay Dudani5c761132011-04-07 20:19:04 -07003757 case CHUNK_TYPE_DONT_CARE:
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05303758 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
3759 fastboot_fail("bogus size for chunk DONT CARE type");
3760 return;
3761 }
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07003762 total_blocks += chunk_header->chunk_sz;
3763 break;
3764
Ajay Dudani5c761132011-04-07 20:19:04 -07003765 case CHUNK_TYPE_CRC:
3766 if(chunk_header->total_sz != sparse_header->chunk_hdr_sz)
3767 {
wufeng.jiang813dc352015-06-02 23:02:46 -04003768 fastboot_fail("Bogus chunk size for chunk type CRC");
Ajay Dudani5c761132011-04-07 20:19:04 -07003769 return;
3770 }
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05303771 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
3772 fastboot_fail("bogus size for chunk CRC type");
3773 return;
3774 }
Ajay Dudani5c761132011-04-07 20:19:04 -07003775 total_blocks += chunk_header->chunk_sz;
Parth Dixit64b1a482016-03-07 16:31:26 +05303776 if ((uintptr_t)data > UINT_MAX - chunk_data_sz) {
vijay kumar800255e2015-04-24 20:26:19 +05303777 fastboot_fail("integer overflow occured");
3778 return;
3779 }
wufeng.jiang813dc352015-06-02 23:02:46 -04003780 data += (uint32_t)chunk_data_sz;
Parth Dixit64b1a482016-03-07 16:31:26 +05303781 if (data_end < (uintptr_t)data) {
vijay kumar800255e2015-04-24 20:26:19 +05303782 fastboot_fail("buffer overreads occured due to invalid sparse header");
3783 return;
3784 }
Ajay Dudani5c761132011-04-07 20:19:04 -07003785 break;
3786
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07003787 default:
Channagoud Kadabif627cf82013-09-09 14:08:20 -07003788 dprintf(CRITICAL, "Unkown chunk type: %x\n",chunk_header->chunk_type);
Ajay Dudani5c761132011-04-07 20:19:04 -07003789 fastboot_fail("Unknown chunk type");
3790 return;
3791 }
3792 }
3793
Ajay Dudani0c6927b2011-05-18 11:12:16 -07003794 dprintf(INFO, "Wrote %d blocks, expected to write %d blocks\n",
3795 total_blocks, sparse_header->total_blks);
3796
3797 if(total_blocks != sparse_header->total_blks)
3798 {
3799 fastboot_fail("sparse image write failure");
3800 }
Ajay Dudani5c761132011-04-07 20:19:04 -07003801
3802 fastboot_okay("");
3803 return;
3804}
3805
3806void cmd_flash_mmc(const char *arg, void *data, unsigned sz)
3807{
3808 sparse_header_t *sparse_header;
Ajay Dudanide984792015-03-02 09:57:41 -08003809 meta_header_t *meta_header;
Ajay Dudani5c761132011-04-07 20:19:04 -07003810
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07003811#ifdef SSD_ENABLE
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -08003812 /* 8 Byte Magic + 2048 Byte xml + Encrypted Data */
3813 unsigned int *magic_number = (unsigned int *) data;
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08003814 int ret=0;
3815 uint32 major_version=0;
3816 uint32 minor_version=0;
3817
3818 ret = scm_svc_version(&major_version,&minor_version);
3819 if(!ret)
3820 {
3821 if(major_version >= 2)
3822 {
Amir Samuelovbb65ce02013-05-05 12:20:18 +03003823 if( !strcmp(arg, "ssd") || !strcmp(arg, "tqs") )
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08003824 {
3825 ret = encrypt_scm((uint32 **) &data, &sz);
3826 if (ret != 0) {
3827 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
3828 return;
3829 }
3830
Amir Samuelovbb65ce02013-05-05 12:20:18 +03003831 /* Protect only for SSD */
3832 if (!strcmp(arg, "ssd")) {
3833 ret = scm_protect_keystore((uint32 *) data, sz);
3834 if (ret != 0) {
3835 dprintf(CRITICAL, "ERROR: scm_protect_keystore Failed\n");
3836 return;
3837 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08003838 }
3839 }
3840 else
3841 {
3842 ret = decrypt_scm_v2((uint32 **) &data, &sz);
3843 if(ret != 0)
3844 {
3845 dprintf(CRITICAL,"ERROR: Decryption Failure\n");
3846 return;
3847 }
3848 }
3849 }
3850 else
3851 {
3852 if (magic_number[0] == DECRYPT_MAGIC_0 &&
3853 magic_number[1] == DECRYPT_MAGIC_1)
3854 {
3855 ret = decrypt_scm((uint32 **) &data, &sz);
3856 if (ret != 0) {
3857 dprintf(CRITICAL, "ERROR: Invalid secure image\n");
3858 return;
3859 }
3860 }
3861 else if (magic_number[0] == ENCRYPT_MAGIC_0 &&
3862 magic_number[1] == ENCRYPT_MAGIC_1)
3863 {
3864 ret = encrypt_scm((uint32 **) &data, &sz);
3865 if (ret != 0) {
3866 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
3867 return;
3868 }
3869 }
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07003870 }
3871 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08003872 else
Neeti Desai127b9e02012-03-20 16:11:23 -07003873 {
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08003874 dprintf(CRITICAL,"INVALID SVC Version\n");
3875 return;
Neeti Desai127b9e02012-03-20 16:11:23 -07003876 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08003877#endif /* SSD_ENABLE */
Neeti Desai127b9e02012-03-20 16:11:23 -07003878
Monika Singh292b3e92018-03-17 22:40:23 +05303879#if VERIFIED_BOOT || VERIFIED_BOOT_2
Channagoud Kadabi35297672015-06-13 11:09:49 -07003880 if (target_build_variant_user())
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003881 {
lijuang511a2b52015-08-14 20:50:51 +08003882 /* if device is locked:
3883 * common partition will not allow to be flashed
3884 * critical partition will allow to flash image.
3885 */
3886 if(!device.is_unlocked && !critical_flash_allowed(arg)) {
3887 fastboot_fail("Partition flashing is not allowed");
3888 return;
3889 }
Mayank Grover889be1b2017-09-12 20:12:23 +05303890
lijuang511a2b52015-08-14 20:50:51 +08003891 /* if device critical is locked:
3892 * common partition will allow to be flashed
3893 * critical partition will not allow to flash image.
3894 */
Mayank Grover912eaa62017-10-26 12:08:53 +05303895 if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05303896 !device.is_unlock_critical &&
3897 critical_flash_allowed(arg)) {
3898 fastboot_fail("Critical partition flashing is not allowed");
3899 return;
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003900 }
3901 }
3902#endif
Monika Singh292b3e92018-03-17 22:40:23 +05303903 if (!strncmp(arg, "avb_custom_key", strlen("avb_custom_key"))) {
3904 dprintf(INFO, "flashing avb_custom_key\n");
3905 if (store_userkey(data, sz)) {
3906 fastboot_fail("Flashing avb_custom_key failed");
3907 } else {
3908 fastboot_okay("");
3909 }
3910 return;
3911 }
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003912
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07003913 sparse_header = (sparse_header_t *) data;
Ajay Dudanide984792015-03-02 09:57:41 -08003914 meta_header = (meta_header_t *) data;
3915 if (sparse_header->magic == SPARSE_HEADER_MAGIC)
Ajay Dudani5c761132011-04-07 20:19:04 -07003916 cmd_flash_mmc_sparse_img(arg, data, sz);
Ajay Dudanide984792015-03-02 09:57:41 -08003917 else if (meta_header->magic == META_HEADER_MAGIC)
3918 cmd_flash_meta_img(arg, data, sz);
3919 else
3920 cmd_flash_mmc_img(arg, data, sz);
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07003921
3922#if VERIFIED_BOOT
Mayank Grover912eaa62017-10-26 12:08:53 +05303923 if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05303924 (!strncmp(arg, "system", 6)) &&
3925 !device.verity_mode)
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07003926 // reset dm_verity mode to enforcing
3927 device.verity_mode = 1;
3928 write_device_info(&device);
Parth Dixitddbc7352015-10-18 03:13:31 +05303929#endif
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07003930
Ajay Dudani5c761132011-04-07 20:19:04 -07003931 return;
3932}
3933
Tanya Brokhman84eaaf62015-02-24 21:57:06 +02003934void cmd_updatevol(const char *vol_name, void *data, unsigned sz)
3935{
3936 struct ptentry *sys_ptn;
3937 struct ptable *ptable;
3938
3939 ptable = flash_get_ptable();
3940 if (ptable == NULL) {
3941 fastboot_fail("partition table doesn't exist");
3942 return;
3943 }
3944
3945 sys_ptn = ptable_find(ptable, "system");
3946 if (sys_ptn == NULL) {
3947 fastboot_fail("system partition not found");
3948 return;
3949 }
3950
3951 sz = ROUND_TO_PAGE(sz, page_mask);
3952 if (update_ubi_vol(sys_ptn, vol_name, data, sz))
3953 fastboot_fail("update_ubi_vol failed");
3954 else
3955 fastboot_okay("");
3956}
3957
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003958void cmd_flash_nand(const char *arg, void *data, unsigned sz)
Dima Zavin214cc642009-01-26 11:16:21 -08003959{
3960 struct ptentry *ptn;
3961 struct ptable *ptable;
3962 unsigned extra = 0;
Gaurav Nebhwanid9730712016-05-06 14:28:29 +05303963 uint64_t partition_size = 0;
Vijay Kumar Pendotic6f22a72017-05-19 22:44:34 +05303964 unsigned bytes_to_round_page = 0;
3965 unsigned rounded_size = 0;
Dima Zavin214cc642009-01-26 11:16:21 -08003966
Kishor PK38ed93d2017-04-25 14:19:26 +05303967 if((uintptr_t)data > (UINT_MAX - sz)) {
3968 fastboot_fail("Cannot flash: image header corrupt");
3969 return;
3970 }
3971
Dima Zavin214cc642009-01-26 11:16:21 -08003972 ptable = flash_get_ptable();
3973 if (ptable == NULL) {
3974 fastboot_fail("partition table doesn't exist");
3975 return;
3976 }
3977
3978 ptn = ptable_find(ptable, arg);
3979 if (ptn == NULL) {
Tanya Brokhman84eaaf62015-02-24 21:57:06 +02003980 dprintf(INFO, "unknown partition name (%s). Trying updatevol\n",
3981 arg);
3982 cmd_updatevol(arg, data, sz);
Dima Zavin214cc642009-01-26 11:16:21 -08003983 return;
3984 }
3985
Monika Singh292b3e92018-03-17 22:40:23 +05303986 if (!strncmp(arg, "avb_custom_key", strlen("avb_custom_key"))) {
3987 dprintf(INFO, "flashing avb_custom_key\n");
3988 if (store_userkey(data, sz)) {
3989 fastboot_fail("Flashing avb_custom_key failed");
3990 } else {
3991 fastboot_okay("");
3992 }
3993 return;
3994 }
3995
Dima Zavin214cc642009-01-26 11:16:21 -08003996 if (!strcmp(ptn->name, "boot") || !strcmp(ptn->name, "recovery")) {
Kishor PK38ed93d2017-04-25 14:19:26 +05303997 if((sz > BOOT_MAGIC_SIZE) && (!memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE))) {
3998 dprintf(INFO, "Verified the BOOT_MAGIC in image header \n");
3999 } else {
4000 fastboot_fail("Image is not a boot image");
Dima Zavin214cc642009-01-26 11:16:21 -08004001 return;
4002 }
4003 }
4004
Amol Jadi5c61a952012-05-04 17:05:35 -07004005 if (!strcmp(ptn->name, "system")
Deepa Dinamani13e32c42012-03-12 14:34:17 -07004006 || !strcmp(ptn->name, "userdata")
4007 || !strcmp(ptn->name, "persist")
Sundarajan Srinivasanb063a852013-11-19 14:02:27 -08004008 || !strcmp(ptn->name, "recoveryfs")
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -08004009 || !strcmp(ptn->name, "modem"))
Tanya Brokhman1c94f1a2015-02-15 09:05:03 +02004010 extra = 1;
Kishor PK38ed93d2017-04-25 14:19:26 +05304011 else {
Vijay Kumar Pendotic6f22a72017-05-19 22:44:34 +05304012 rounded_size = ROUNDUP(sz, page_size);
4013 bytes_to_round_page = rounded_size - sz;
4014 if (bytes_to_round_page) {
4015 if (((uintptr_t)data + sz ) > (UINT_MAX - bytes_to_round_page)) {
4016 fastboot_fail("Integer overflow detected");
4017 return;
4018 }
4019 if (((uintptr_t)data + sz + bytes_to_round_page) >
4020 ((uintptr_t)target_get_scratch_address() + target_get_max_flash_size())) {
4021 fastboot_fail("Buffer size is not aligned to page_size");
4022 return;
4023 }
4024 else {
4025 memset(data + sz, 0, bytes_to_round_page);
4026 sz = rounded_size;
4027 }
Kishor PK38ed93d2017-04-25 14:19:26 +05304028 }
Gaurav Nebhwanid9730712016-05-06 14:28:29 +05304029 }
4030
Kishor PK38ed93d2017-04-25 14:19:26 +05304031 /*Checking partition_size for the possible integer overflow */
4032 partition_size = validate_partition_size(ptn);
4033
Gaurav Nebhwanid9730712016-05-06 14:28:29 +05304034 if (sz > partition_size) {
4035 fastboot_fail("Image size too large");
4036 return;
4037 }
4038
Dima Zavin214cc642009-01-26 11:16:21 -08004039 dprintf(INFO, "writing %d bytes to '%s'\n", sz, ptn->name);
Mayank Grover6cde9352017-06-06 18:45:23 +05304040 if ((sz > UBI_EC_HDR_SIZE) &&
4041 (!memcmp((void *)data, UBI_MAGIC, UBI_MAGIC_SIZE))) {
Tanya Brokhman1c94f1a2015-02-15 09:05:03 +02004042 if (flash_ubi_img(ptn, data, sz)) {
4043 fastboot_fail("flash write failure");
4044 return;
4045 }
4046 } else {
4047 if (flash_write(ptn, extra, data, sz)) {
4048 fastboot_fail("flash write failure");
4049 return;
4050 }
Dima Zavin214cc642009-01-26 11:16:21 -08004051 }
4052 dprintf(INFO, "partition '%s' updated\n", ptn->name);
4053 fastboot_okay("");
4054}
4055
Kishor PK38ed93d2017-04-25 14:19:26 +05304056
4057static inline uint64_t validate_partition_size(struct ptentry *ptn)
4058{
4059 if (ptn->length && flash_num_pages_per_blk() && page_size) {
4060 if ((ptn->length < ( UINT_MAX / flash_num_pages_per_blk())) && ((ptn->length * flash_num_pages_per_blk()) < ( UINT_MAX / page_size))) {
4061 return ptn->length * flash_num_pages_per_blk() * page_size;
4062 }
4063 }
4064 return 0;
4065}
4066
4067
Sridhar Parasurame94e8152014-10-24 14:06:03 -07004068void cmd_flash(const char *arg, void *data, unsigned sz)
4069{
4070 if(target_is_emmc_boot())
4071 cmd_flash_mmc(arg, data, sz);
4072 else
4073 cmd_flash_nand(arg, data, sz);
4074}
4075
Dima Zavin214cc642009-01-26 11:16:21 -08004076void cmd_continue(const char *arg, void *data, unsigned sz)
4077{
4078 fastboot_okay("");
Amol Jadi7c4316c2013-10-07 14:19:26 -07004079 fastboot_stop();
Shashank Mittald3e54dd2014-08-28 15:24:02 -07004080
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004081 if (target_is_emmc_boot())
4082 {
lijuanga40d6302015-07-20 20:10:13 +08004083#if FBCON_DISPLAY_MSG
lijuangde34d502016-02-26 16:04:50 +08004084 /* Exit keys' detection thread firstly */
4085 exit_menu_keys_detection();
lijuanga40d6302015-07-20 20:10:13 +08004086#endif
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004087 boot_linux_from_mmc();
4088 }
4089 else
4090 {
4091 boot_linux_from_flash();
4092 }
Dima Zavin214cc642009-01-26 11:16:21 -08004093}
4094
Chandan Uddaraju94183c02010-01-15 15:13:59 -08004095void cmd_reboot(const char *arg, void *data, unsigned sz)
4096{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004097 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08004098 fastboot_okay("");
4099 reboot_device(0);
4100}
4101
Mayank Grover351a75e2017-05-30 20:06:08 +05304102void cmd_set_active(const char *arg, void *data, unsigned sz)
4103{
Mayank Grover6ccc1c92017-07-04 17:36:46 +05304104 char *p, *sp = NULL;
Mayank Grover351a75e2017-05-30 20:06:08 +05304105 unsigned i,current_active_slot;
4106 const char *current_slot_suffix;
4107
4108 if (!partition_multislot_is_supported())
4109 {
4110 fastboot_fail("Command not supported");
4111 return;
4112 }
4113
4114 if (arg)
4115 {
Mayank Grover6ccc1c92017-07-04 17:36:46 +05304116 p = strtok_r((char *)arg, ":", &sp);
Mayank Grover5eb5f692017-07-19 20:16:04 +05304117 if (p)
Mayank Grover351a75e2017-05-30 20:06:08 +05304118 {
4119 current_active_slot = partition_find_active_slot();
4120
4121 /* Check if trying to make curent slot active */
4122 current_slot_suffix = SUFFIX_SLOT(current_active_slot);
Mayank Grover6ccc1c92017-07-04 17:36:46 +05304123 current_slot_suffix = strtok_r((char *)current_slot_suffix,
4124 (char *)suffix_delimiter, &sp);
4125
Mayank Grover5eb5f692017-07-19 20:16:04 +05304126 if (current_slot_suffix &&
4127 !strncmp(p, current_slot_suffix, strlen(current_slot_suffix)))
Mayank Grover351a75e2017-05-30 20:06:08 +05304128 {
4129 fastboot_okay("Slot already set active");
4130 return;
4131 }
4132 else
4133 {
4134 for (i = 0; i < AB_SUPPORTED_SLOTS; i++)
4135 {
4136 current_slot_suffix = SUFFIX_SLOT(i);
Mayank Grover6ccc1c92017-07-04 17:36:46 +05304137 current_slot_suffix = strtok_r((char *)current_slot_suffix,
4138 (char *)suffix_delimiter, &sp);
Mayank Grover5eb5f692017-07-19 20:16:04 +05304139 if (current_slot_suffix &&
4140 !strncmp(p, current_slot_suffix, strlen(current_slot_suffix)))
Mayank Grover351a75e2017-05-30 20:06:08 +05304141 {
4142 partition_switch_slots(current_active_slot, i);
4143 publish_getvar_multislot_vars();
4144 fastboot_okay("");
4145 return;
4146 }
4147 }
4148 }
4149 }
4150 }
4151 fastboot_fail("Invalid slot suffix.");
4152 return;
4153}
4154
Chandan Uddaraju94183c02010-01-15 15:13:59 -08004155void cmd_reboot_bootloader(const char *arg, void *data, unsigned sz)
4156{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004157 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08004158 fastboot_okay("");
4159 reboot_device(FASTBOOT_MODE);
4160}
4161
Ameya Thakur11cf1a62013-08-05 12:44:48 -07004162void cmd_oem_enable_charger_screen(const char *arg, void *data, unsigned size)
4163{
4164 dprintf(INFO, "Enabling charger screen check\n");
4165 device.charger_screen_enabled = 1;
4166 write_device_info(&device);
4167 fastboot_okay("");
4168}
4169
4170void cmd_oem_disable_charger_screen(const char *arg, void *data, unsigned size)
4171{
4172 dprintf(INFO, "Disabling charger screen check\n");
4173 device.charger_screen_enabled = 0;
4174 write_device_info(&device);
4175 fastboot_okay("");
4176}
4177
lijuanga25d8bb2015-09-16 13:11:52 +08004178void cmd_oem_off_mode_charger(const char *arg, void *data, unsigned size)
4179{
4180 char *p = NULL;
4181 const char *delim = " \t\n\r";
Parth Dixit407f15b2016-01-07 14:47:37 +05304182 char *sp;
lijuanga25d8bb2015-09-16 13:11:52 +08004183
4184 if (arg) {
Parth Dixit407f15b2016-01-07 14:47:37 +05304185 p = strtok_r((char *)arg, delim, &sp);
lijuanga25d8bb2015-09-16 13:11:52 +08004186 if (p) {
4187 if (!strncmp(p, "0", 1)) {
4188 device.charger_screen_enabled = 0;
4189 } else if (!strncmp(p, "1", 1)) {
4190 device.charger_screen_enabled = 1;
4191 }
4192 }
4193 }
4194
4195 /* update charger_screen_enabled value for getvar
4196 * command
4197 */
4198 snprintf(charger_screen_enabled, MAX_RSP_SIZE, "%d",
4199 device.charger_screen_enabled);
4200
4201 write_device_info(&device);
4202 fastboot_okay("");
4203}
4204
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05304205void cmd_oem_select_display_panel(const char *arg, void *data, unsigned size)
4206{
4207 dprintf(INFO, "Selecting display panel %s\n", arg);
4208 if (arg)
4209 strlcpy(device.display_panel, arg,
4210 sizeof(device.display_panel));
4211 write_device_info(&device);
4212 fastboot_okay("");
4213}
4214
Shashank Mittal162244e2011-08-08 19:01:25 -07004215void cmd_oem_unlock(const char *arg, void *data, unsigned sz)
4216{
lijuang4ece1e72015-08-14 21:02:36 +08004217 set_device_unlock(UNLOCK, TRUE);
vijay kumarc65876c2015-04-24 13:29:16 +05304218}
4219
4220void cmd_oem_unlock_go(const char *arg, void *data, unsigned sz)
4221{
lijuang4ece1e72015-08-14 21:02:36 +08004222 if(!device.is_unlocked) {
vijay kumarc65876c2015-04-24 13:29:16 +05304223 if(!is_allow_unlock) {
4224 fastboot_fail("oem unlock is not allowed");
4225 return;
4226 }
4227
lijuang4ece1e72015-08-14 21:02:36 +08004228 set_device_unlock_value(UNLOCK, TRUE);
vijay kumarc65876c2015-04-24 13:29:16 +05304229
lijuang4ece1e72015-08-14 21:02:36 +08004230 /* wipe data */
vijay kumarc65876c2015-04-24 13:29:16 +05304231 struct recovery_message msg;
Kishor PK60a68212017-05-08 16:55:57 +05304232 memset(&msg, 0, sizeof(msg));
vijay kumarc65876c2015-04-24 13:29:16 +05304233 snprintf(msg.recovery, sizeof(msg.recovery), "recovery\n--wipe_data");
4234 write_misc(0, &msg, sizeof(msg));
4235
4236 fastboot_okay("");
4237 reboot_device(RECOVERY_MODE);
Shashank Mittald3e54dd2014-08-28 15:24:02 -07004238 }
4239 fastboot_okay("");
4240}
4241
Channagoud Kadabiad259832015-05-29 11:14:17 -07004242static int aboot_frp_unlock(char *pname, void *data, unsigned sz)
4243{
Mayank Grover70f8b0e2017-05-17 11:49:00 +05304244 int ret=1;
4245 bool authentication_success=false;
Channagoud Kadabiad259832015-05-29 11:14:17 -07004246
Mayank Grover70f8b0e2017-05-17 11:49:00 +05304247 /*
4248 Authentication method not implemented.
4249
4250 OEM to implement, authentication system which on successful validataion,
4251 calls write_allow_oem_unlock() with is_allow_unlock.
4252 */
4253#if 0
4254 authentication_success = oem_specific_auth_mthd();
4255#endif
4256
4257 if (authentication_success)
Channagoud Kadabiad259832015-05-29 11:14:17 -07004258 {
Mayank Grover70f8b0e2017-05-17 11:49:00 +05304259 is_allow_unlock = true;
4260 write_allow_oem_unlock(is_allow_unlock);
4261 ret = 0;
Channagoud Kadabiad259832015-05-29 11:14:17 -07004262 }
4263 return ret;
4264}
4265
Shashank Mittald3e54dd2014-08-28 15:24:02 -07004266void cmd_oem_lock(const char *arg, void *data, unsigned sz)
4267{
lijuang4ece1e72015-08-14 21:02:36 +08004268 set_device_unlock(UNLOCK, FALSE);
Shashank Mittal162244e2011-08-08 19:01:25 -07004269}
4270
Shashank Mittala0032282011-08-26 14:50:11 -07004271void cmd_oem_devinfo(const char *arg, void *data, unsigned sz)
4272{
lijuang511a2b52015-08-14 20:50:51 +08004273 char response[MAX_RSP_SIZE];
Ameya Thakur11cf1a62013-08-05 12:44:48 -07004274 snprintf(response, sizeof(response), "\tDevice tampered: %s", (device.is_tampered ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07004275 fastboot_info(response);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07004276 snprintf(response, sizeof(response), "\tDevice unlocked: %s", (device.is_unlocked ? "true" : "false"));
4277 fastboot_info(response);
Monika Singh292b3e92018-03-17 22:40:23 +05304278#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover912eaa62017-10-26 12:08:53 +05304279 if (VB_M <= target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +05304280 {
4281 snprintf(response, sizeof(response), "\tDevice critical unlocked: %s",
4282 (device.is_unlock_critical ? "true" : "false"));
4283 fastboot_info(response);
4284 }
Parth Dixitddbc7352015-10-18 03:13:31 +05304285#endif
Ameya Thakur11cf1a62013-08-05 12:44:48 -07004286 snprintf(response, sizeof(response), "\tCharger screen enabled: %s", (device.charger_screen_enabled ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07004287 fastboot_info(response);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05304288 snprintf(response, sizeof(response), "\tDisplay panel: %s", (device.display_panel));
4289 fastboot_info(response);
Shashank Mittala0032282011-08-26 14:50:11 -07004290 fastboot_okay("");
4291}
4292
lijuang511a2b52015-08-14 20:50:51 +08004293void cmd_flashing_get_unlock_ability(const char *arg, void *data, unsigned sz)
4294{
4295 char response[MAX_RSP_SIZE];
4296 snprintf(response, sizeof(response), "\tget_unlock_ability: %d", is_allow_unlock);
4297 fastboot_info(response);
4298 fastboot_okay("");
4299}
4300
4301void cmd_flashing_lock_critical(const char *arg, void *data, unsigned sz)
4302{
lijuang4ece1e72015-08-14 21:02:36 +08004303 set_device_unlock(UNLOCK_CRITICAL, FALSE);
lijuang511a2b52015-08-14 20:50:51 +08004304}
4305
4306void cmd_flashing_unlock_critical(const char *arg, void *data, unsigned sz)
4307{
lijuang4ece1e72015-08-14 21:02:36 +08004308 set_device_unlock(UNLOCK_CRITICAL, TRUE);
lijuang511a2b52015-08-14 20:50:51 +08004309}
4310
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07004311void cmd_preflash(const char *arg, void *data, unsigned sz)
4312{
4313 fastboot_okay("");
4314}
4315
Mao Flynn7b379f32015-04-20 00:28:30 +08004316static uint8_t logo_header[LOGO_IMG_HEADER_SIZE];
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304317
Mao Flynn7b379f32015-04-20 00:28:30 +08004318int splash_screen_check_header(logo_img_header *header)
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304319{
Mao Flynn7b379f32015-04-20 00:28:30 +08004320 if (memcmp(header->magic, LOGO_IMG_MAGIC, 8))
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304321 return -1;
Mao Flynn7b379f32015-04-20 00:28:30 +08004322 if (header->width == 0 || header->height == 0)
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304323 return -1;
4324 return 0;
4325}
4326
Mao Flynn7b379f32015-04-20 00:28:30 +08004327int splash_screen_flash()
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07004328{
4329 struct ptentry *ptn;
4330 struct ptable *ptable;
Mao Flynn7b379f32015-04-20 00:28:30 +08004331 struct logo_img_header *header;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07004332 struct fbcon_config *fb_display = NULL;
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08004333
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304334 ptable = flash_get_ptable();
4335 if (ptable == NULL) {
Mao Flynn7b379f32015-04-20 00:28:30 +08004336 dprintf(CRITICAL, "ERROR: Partition table not found\n");
4337 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304338 }
Mao Flynn7b379f32015-04-20 00:28:30 +08004339
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304340 ptn = ptable_find(ptable, "splash");
4341 if (ptn == NULL) {
4342 dprintf(CRITICAL, "ERROR: splash Partition not found\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08004343 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304344 }
Mao Flynn7b379f32015-04-20 00:28:30 +08004345 if (flash_read(ptn, 0, (void *)logo_header, LOGO_IMG_HEADER_SIZE)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304346 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08004347 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304348 }
4349
Mao Flynn7b379f32015-04-20 00:28:30 +08004350 header = (struct logo_img_header *)logo_header;
4351 if (splash_screen_check_header(header)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304352 dprintf(CRITICAL, "ERROR: Boot image header invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08004353 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304354 }
4355
4356 fb_display = fbcon_display();
4357 if (fb_display) {
Mao Flynn7b379f32015-04-20 00:28:30 +08004358 if (header->type && (header->blocks != 0)) { // RLE24 compressed data
4359 uint8_t *base = (uint8_t *) fb_display->base + LOGO_IMG_OFFSET;
4360
4361 /* if the logo is full-screen size, remove "fbcon_clear()" */
4362 if ((header->width != fb_display->width)
4363 || (header->height != fb_display->height))
4364 fbcon_clear();
4365
4366 if (flash_read(ptn + LOGO_IMG_HEADER_SIZE, 0,
4367 (uint32_t *)base,
4368 (header->blocks * 512))) {
4369 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
4370 return -1;
4371 }
4372 fbcon_extract_to_screen(header, base);
4373 return 0;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07004374 }
Mao Flynn7b379f32015-04-20 00:28:30 +08004375
Veera Sundaram Sankaranc33416f2015-09-28 10:25:55 -07004376 if ((header->width > fb_display->width) || (header->height > fb_display->height)) {
4377 dprintf(CRITICAL, "Logo config greater than fb config. Fall back default logo\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08004378 return -1;
4379 }
4380
Vineet Bajaj99291ed2014-09-09 12:29:46 +05304381 uint8_t *base = (uint8_t *) fb_display->base;
Sachin Bhayare619e9e42017-05-15 13:10:31 +05304382 uint32_t fb_size = ROUNDUP(fb_display->width *
4383 fb_display->height *
4384 (fb_display->bpp / 8), 4096);
4385 uint32_t splash_size = ((((header->width * header->height *
4386 fb_display->bpp/8) + 511) >> 9) << 9);
4387
4388 if (splash_size > fb_size) {
4389 dprintf(CRITICAL, "ERROR: Splash image size invalid\n");
4390 return -1;
4391 }
4392
Mao Flynn7b379f32015-04-20 00:28:30 +08004393 if (flash_read(ptn + LOGO_IMG_HEADER_SIZE, 0,
4394 (uint32_t *)base,
4395 ((((header->width * header->height * fb_display->bpp/8) + 511) >> 9) << 9))) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304396 fbcon_clear();
Vineet Bajaj99291ed2014-09-09 12:29:46 +05304397 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08004398 return -1;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07004399 }
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304400 }
4401
Mao Flynn7b379f32015-04-20 00:28:30 +08004402 return 0;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304403}
4404
Mao Flynn7b379f32015-04-20 00:28:30 +08004405int splash_screen_mmc()
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304406{
4407 int index = INVALID_PTN;
4408 unsigned long long ptn = 0;
4409 struct fbcon_config *fb_display = NULL;
Mao Flynn7b379f32015-04-20 00:28:30 +08004410 struct logo_img_header *header;
Mao Flynn1893a7f2015-06-03 12:03:36 +08004411 uint32_t blocksize, realsize, readsize;
4412 uint8_t *base;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304413
4414 index = partition_get_index("splash");
4415 if (index == 0) {
4416 dprintf(CRITICAL, "ERROR: splash Partition table not found\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08004417 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304418 }
4419
4420 ptn = partition_get_offset(index);
4421 if (ptn == 0) {
4422 dprintf(CRITICAL, "ERROR: splash Partition invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08004423 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304424 }
4425
Mao Flynn1893a7f2015-06-03 12:03:36 +08004426 mmc_set_lun(partition_get_lun(index));
4427
4428 blocksize = mmc_get_device_blocksize();
4429 if (blocksize == 0) {
4430 dprintf(CRITICAL, "ERROR:splash Partition invalid blocksize\n");
4431 return -1;
4432 }
4433
4434 fb_display = fbcon_display();
Channagoud Kadabidaf10a62015-07-22 11:51:55 -07004435 if (!fb_display)
4436 {
4437 dprintf(CRITICAL, "ERROR: fb config is not allocated\n");
4438 return -1;
4439 }
4440
Mao Flynn1893a7f2015-06-03 12:03:36 +08004441 base = (uint8_t *) fb_display->base;
4442
Kishor PKee5c0a32018-03-06 16:49:46 +05304443 if (mmc_read(ptn + PLL_CODES_OFFSET, (uint32_t *)(base + LOGO_IMG_OFFSET), blocksize)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304444 dprintf(CRITICAL, "ERROR: Cannot read splash image header\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08004445 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304446 }
4447
Mao Flynn1893a7f2015-06-03 12:03:36 +08004448 header = (struct logo_img_header *)(base + LOGO_IMG_OFFSET);
Mao Flynn7b379f32015-04-20 00:28:30 +08004449 if (splash_screen_check_header(header)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304450 dprintf(CRITICAL, "ERROR: Splash image header invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08004451 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304452 }
4453
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304454 if (fb_display) {
Mao Flynn1893a7f2015-06-03 12:03:36 +08004455 if (header->type && (header->blocks != 0)) { /* 1 RLE24 compressed data */
4456 base += LOGO_IMG_OFFSET;
Mao Flynn7b379f32015-04-20 00:28:30 +08004457
Mao Flynn1893a7f2015-06-03 12:03:36 +08004458 realsize = header->blocks * 512;
4459 readsize = ROUNDUP((realsize + LOGO_IMG_HEADER_SIZE), blocksize) - blocksize;
4460
4461 /* if the logo is not full-screen size, clean screen */
Mao Flynn7b379f32015-04-20 00:28:30 +08004462 if ((header->width != fb_display->width)
4463 || (header->height != fb_display->height))
4464 fbcon_clear();
4465
Sachin Bhayare619e9e42017-05-15 13:10:31 +05304466 uint32_t fb_size = ROUNDUP(fb_display->width *
4467 fb_display->height *
4468 (fb_display->bpp / 8), 4096);
4469
4470 if (readsize > fb_size) {
4471 dprintf(CRITICAL, "ERROR: Splash image size invalid\n");
4472 return -1;
4473 }
4474
Kishor PKee5c0a32018-03-06 16:49:46 +05304475 if (mmc_read(ptn + PLL_CODES_OFFSET + blocksize, (uint32_t *)(base + blocksize), readsize)) {
Mao Flynn7b379f32015-04-20 00:28:30 +08004476 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
4477 return -1;
4478 }
Mao Flynn7b379f32015-04-20 00:28:30 +08004479
Mao Flynn1893a7f2015-06-03 12:03:36 +08004480 fbcon_extract_to_screen(header, (base + LOGO_IMG_HEADER_SIZE));
4481 } else { /* 2 Raw BGR data */
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304482
Veera Sundaram Sankaranc33416f2015-09-28 10:25:55 -07004483 if ((header->width > fb_display->width) || (header->height > fb_display->height)) {
4484 dprintf(CRITICAL, "Logo config greater than fb config. Fall back default logo\n");
Mao Flynn1893a7f2015-06-03 12:03:36 +08004485 return -1;
4486 }
4487
4488 realsize = header->width * header->height * fb_display->bpp / 8;
4489 readsize = ROUNDUP((realsize + LOGO_IMG_HEADER_SIZE), blocksize) - blocksize;
4490
4491 if (blocksize == LOGO_IMG_HEADER_SIZE) { /* read the content directly */
Kishor PKee5c0a32018-03-06 16:49:46 +05304492 if (mmc_read((ptn + PLL_CODES_OFFSET + LOGO_IMG_HEADER_SIZE), (uint32_t *)base, readsize)) {
Mao Flynn1893a7f2015-06-03 12:03:36 +08004493 fbcon_clear();
4494 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
4495 return -1;
4496 }
4497 } else {
Kishor PKee5c0a32018-03-06 16:49:46 +05304498 if (mmc_read(ptn + PLL_CODES_OFFSET + blocksize ,
Mao Flynn1893a7f2015-06-03 12:03:36 +08004499 (uint32_t *)(base + LOGO_IMG_OFFSET + blocksize), readsize)) {
4500 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
4501 return -1;
4502 }
4503 memmove(base, (base + LOGO_IMG_OFFSET + LOGO_IMG_HEADER_SIZE), realsize);
4504 }
4505 }
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304506 }
4507
Mao Flynn7b379f32015-04-20 00:28:30 +08004508 return 0;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304509}
4510
Mao Flynn7b379f32015-04-20 00:28:30 +08004511int fetch_image_from_partition()
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304512{
4513 if (target_is_emmc_boot()) {
4514 return splash_screen_mmc();
4515 } else {
4516 return splash_screen_flash();
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07004517 }
4518}
4519
Mayank Grover351a75e2017-05-30 20:06:08 +05304520void publish_getvar_multislot_vars()
4521{
4522 int i,count;
4523 static bool published = false;
4524 static char slot_count[MAX_RSP_SIZE];
4525 static struct ab_slot_info slot_info[AB_SUPPORTED_SLOTS];
4526 static char active_slot_suffix[MAX_RSP_SIZE];
4527 static char has_slot_pname[NUM_PARTITIONS][MAX_GET_VAR_NAME_SIZE];
4528 static char has_slot_reply[NUM_PARTITIONS][MAX_RSP_SIZE];
4529 const char *tmp;
4530 char tmpbuff[MAX_GET_VAR_NAME_SIZE];
Mayank Grover2bd84bb2017-06-20 11:06:07 +05304531 signed active_slt;
Mayank Grover351a75e2017-05-30 20:06:08 +05304532
4533 if (!published)
4534 {
4535 /* Update slot meta info */
4536 count = partition_fill_partition_meta(has_slot_pname, has_slot_reply,
4537 partition_get_partition_count());
4538 for(i=0; i<count; i++)
4539 {
4540 memset(tmpbuff, 0, MAX_GET_VAR_NAME_SIZE);
Mayank Grover2bd84bb2017-06-20 11:06:07 +05304541 snprintf(tmpbuff, MAX_GET_VAR_NAME_SIZE,"has-slot:%s",
4542 has_slot_pname[i]);
4543 strlcpy(has_slot_pname[i], tmpbuff, MAX_GET_VAR_NAME_SIZE);
Mayank Grover351a75e2017-05-30 20:06:08 +05304544 fastboot_publish(has_slot_pname[i], has_slot_reply[i]);
4545 }
4546
4547 for (i=0; i<AB_SUPPORTED_SLOTS; i++)
4548 {
4549 tmp = SUFFIX_SLOT(i);
Mayank Grover7b8a8f82017-10-27 13:07:59 +05304550 tmp++; // to remove "_" from slot_suffix.
Mayank Grover2bd84bb2017-06-20 11:06:07 +05304551 snprintf(slot_info[i].slot_is_unbootable, sizeof(slot_info[i].slot_is_unbootable),
4552 "slot-unbootable:%s", tmp);
4553 snprintf(slot_info[i].slot_is_active, sizeof(slot_info[i].slot_is_active),
4554 "slot-active:%s", tmp);
4555 snprintf(slot_info[i].slot_is_succesful, sizeof(slot_info[i].slot_is_succesful),
4556 "slot-success:%s", tmp);
4557 snprintf(slot_info[i].slot_retry_count, sizeof(slot_info[i].slot_retry_count),
4558 "slot-retry-count:%s", tmp);
Mayank Grover351a75e2017-05-30 20:06:08 +05304559 fastboot_publish(slot_info[i].slot_is_unbootable,
4560 slot_info[i].slot_is_unbootable_rsp);
4561 fastboot_publish(slot_info[i].slot_is_active,
4562 slot_info[i].slot_is_active_rsp);
4563 fastboot_publish(slot_info[i].slot_is_succesful,
4564 slot_info[i].slot_is_succesful_rsp);
4565 fastboot_publish(slot_info[i].slot_retry_count,
4566 slot_info[i].slot_retry_count_rsp);
4567 }
4568 fastboot_publish("current-slot", active_slot_suffix);
4569 snprintf(slot_count, sizeof(slot_count),"%d", AB_SUPPORTED_SLOTS);
4570 fastboot_publish("slot-count", slot_count);
4571 published = true;
4572 }
4573
Mayank Grover2bd84bb2017-06-20 11:06:07 +05304574 active_slt = partition_find_active_slot();
4575 if (active_slt != INVALID)
Mayank Grover7b8a8f82017-10-27 13:07:59 +05304576 {
4577 tmp = SUFFIX_SLOT(active_slt);
4578 tmp++; // to remove "_" from slot_suffix.
4579 snprintf(active_slot_suffix, sizeof(active_slot_suffix), "%s", tmp);
4580 }
Mayank Grover2bd84bb2017-06-20 11:06:07 +05304581 else
4582 strlcpy(active_slot_suffix, "INVALID", sizeof(active_slot_suffix));
4583
Mayank Grover351a75e2017-05-30 20:06:08 +05304584 /* Update partition meta information */
4585 partition_fill_slot_meta(slot_info);
4586 return;
4587}
4588
lijuang4ece1e72015-08-14 21:02:36 +08004589void get_product_name(unsigned char *buf)
4590{
4591 snprintf((char*)buf, MAX_RSP_SIZE, "%s", TARGET(BOARD));
4592 return;
4593}
4594
Mayank Grover7b8a8f82017-10-27 13:07:59 +05304595#if PRODUCT_IOT
4596void get_bootloader_version_iot(unsigned char *buf)
4597{
4598 if (buf != NULL)
4599 {
4600 strlcpy(buf, TARGET(BOARD), MAX_VERSION_LEN);
4601 strlcat(buf, "-", MAX_VERSION_LEN);
4602 strlcat(buf, PRODUCT_IOT_VERSION, MAX_VERSION_LEN);
4603 }
4604 return;
4605}
4606#endif
4607
lijuang4ece1e72015-08-14 21:02:36 +08004608void get_bootloader_version(unsigned char *buf)
4609{
4610 snprintf((char*)buf, MAX_RSP_SIZE, "%s", device.bootloader_version);
4611 return;
4612}
4613
4614void get_baseband_version(unsigned char *buf)
4615{
4616 snprintf((char*)buf, MAX_RSP_SIZE, "%s", device.radio_version);
4617 return;
4618}
4619
Monika Singh32a09f72018-03-14 13:08:29 +05304620bool is_device_locked_critical()
4621{
4622 return device.is_unlock_critical ? false:true;
4623}
4624
lijuang4ece1e72015-08-14 21:02:36 +08004625bool is_device_locked()
4626{
4627 return device.is_unlocked ? false:true;
4628}
4629
Monika Singh32a09f72018-03-14 13:08:29 +05304630bool is_verity_enforcing()
4631{
4632 return device.verity_mode ? true:false;
4633}
4634
Amol Jadi5edf3552013-07-23 14:15:34 -07004635/* register commands and variables for fastboot */
4636void aboot_fastboot_register_commands(void)
4637{
Sridhar Parasurame94e8152014-10-24 14:06:03 -07004638 int i;
lijuangf16461c2015-08-03 17:09:34 +08004639 char hw_platform_buf[MAX_RSP_SIZE];
Amol Jadi5edf3552013-07-23 14:15:34 -07004640
Sridhar Parasurame94e8152014-10-24 14:06:03 -07004641 struct fastboot_cmd_desc cmd_list[] = {
lijuang511a2b52015-08-14 20:50:51 +08004642 /* By default the enabled list is empty. */
4643 {"", NULL},
4644 /* move commands enclosed within the below ifndef to here
4645 * if they need to be enabled in user build.
4646 */
Sridhar Parasurame94e8152014-10-24 14:06:03 -07004647#ifndef DISABLE_FASTBOOT_CMDS
lijuang511a2b52015-08-14 20:50:51 +08004648 /* Register the following commands only for non-user builds */
4649 {"flash:", cmd_flash},
4650 {"erase:", cmd_erase},
4651 {"boot", cmd_boot},
4652 {"continue", cmd_continue},
4653 {"reboot", cmd_reboot},
4654 {"reboot-bootloader", cmd_reboot_bootloader},
4655 {"oem unlock", cmd_oem_unlock},
4656 {"oem unlock-go", cmd_oem_unlock_go},
4657 {"oem lock", cmd_oem_lock},
4658 {"flashing unlock", cmd_oem_unlock},
4659 {"flashing lock", cmd_oem_lock},
4660 {"flashing lock_critical", cmd_flashing_lock_critical},
4661 {"flashing unlock_critical", cmd_flashing_unlock_critical},
4662 {"flashing get_unlock_ability", cmd_flashing_get_unlock_ability},
4663 {"oem device-info", cmd_oem_devinfo},
4664 {"preflash", cmd_preflash},
4665 {"oem enable-charger-screen", cmd_oem_enable_charger_screen},
4666 {"oem disable-charger-screen", cmd_oem_disable_charger_screen},
lijuanga25d8bb2015-09-16 13:11:52 +08004667 {"oem off-mode-charge", cmd_oem_off_mode_charger},
lijuang511a2b52015-08-14 20:50:51 +08004668 {"oem select-display-panel", cmd_oem_select_display_panel},
Mayank Grover6ccc1c92017-07-04 17:36:46 +05304669 {"set_active",cmd_set_active},
Channagoud Kadabi736c4962015-08-21 11:56:52 -07004670#if UNITTEST_FW_SUPPORT
Channagoud Kadabid5ddf482015-09-28 10:31:35 -07004671 {"oem run-tests", cmd_oem_runtests},
Channagoud Kadabi736c4962015-08-21 11:56:52 -07004672#endif
Sridhar Parasurame94e8152014-10-24 14:06:03 -07004673#endif
lijuang511a2b52015-08-14 20:50:51 +08004674 };
Sridhar Parasurame94e8152014-10-24 14:06:03 -07004675
4676 int fastboot_cmds_count = sizeof(cmd_list)/sizeof(cmd_list[0]);
4677 for (i = 1; i < fastboot_cmds_count; i++)
4678 fastboot_register(cmd_list[i].name,cmd_list[i].cb);
4679
Amol Jadi5edf3552013-07-23 14:15:34 -07004680 /* publish variables and their values */
4681 fastboot_publish("product", TARGET(BOARD));
4682 fastboot_publish("kernel", "lk");
4683 fastboot_publish("serialno", sn_buf);
4684
Mayank Grovera64dfbe2018-05-17 14:06:34 +05304685 /*publish hw-revision major(upper 16 bits) and minor(lower 16 bits)*/
4686 snprintf(soc_version_str, MAX_RSP_SIZE, "%x", board_soc_version());
4687 fastboot_publish("hw-revision", soc_version_str);
4688
Amol Jadi5edf3552013-07-23 14:15:34 -07004689 /*
4690 * partition info is supported only for emmc partitions
4691 * Calling this for NAND prints some error messages which
4692 * is harmless but misleading. Avoid calling this for NAND
4693 * devices.
4694 */
4695 if (target_is_emmc_boot())
Mayank Grover7b8a8f82017-10-27 13:07:59 +05304696 publish_getvar_partition_info(part_info, partition_get_partition_count());
Amol Jadi5edf3552013-07-23 14:15:34 -07004697
Mayank Grover351a75e2017-05-30 20:06:08 +05304698 if (partition_multislot_is_supported())
4699 publish_getvar_multislot_vars();
4700
Amol Jadi5edf3552013-07-23 14:15:34 -07004701 /* Max download size supported */
Ameya Thakur11cf1a62013-08-05 12:44:48 -07004702 snprintf(max_download_size, MAX_RSP_SIZE, "\t0x%x",
4703 target_get_max_flash_size());
Amol Jadi5edf3552013-07-23 14:15:34 -07004704 fastboot_publish("max-download-size", (const char *) max_download_size);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07004705 /* Is the charger screen check enabled */
4706 snprintf(charger_screen_enabled, MAX_RSP_SIZE, "%d",
4707 device.charger_screen_enabled);
4708 fastboot_publish("charger-screen-enabled",
4709 (const char *) charger_screen_enabled);
lijuanga25d8bb2015-09-16 13:11:52 +08004710 fastboot_publish("off-mode-charge", (const char *) charger_screen_enabled);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05304711 snprintf(panel_display_mode, MAX_RSP_SIZE, "%s",
4712 device.display_panel);
4713 fastboot_publish("display-panel",
4714 (const char *) panel_display_mode);
Mukesh Ojhabb6e7f32018-03-02 15:04:06 +05304715
4716 if (target_is_emmc_boot())
4717 {
4718 mmc_blocksize = mmc_get_device_blocksize();
4719 }
4720 else
4721 {
4722 mmc_blocksize = flash_block_size();
4723 }
4724 snprintf(block_size_string, MAX_RSP_SIZE, "0x%x", mmc_blocksize);
4725 fastboot_publish("erase-block-size", (const char *) block_size_string);
4726 fastboot_publish("logical-block-size", (const char *) block_size_string);
Mayank Grover7b8a8f82017-10-27 13:07:59 +05304727#if PRODUCT_IOT
4728 get_bootloader_version_iot(&bootloader_version_string);
4729 fastboot_publish("version-bootloader", (const char *) bootloader_version_string);
4730
4731 /* Version baseband is n/a for apq iot devices */
4732 fastboot_publish("version-baseband", "N/A");
Mayank Grover7b8a8f82017-10-27 13:07:59 +05304733#else
Channagoud Kadabiaafbbfc2015-03-04 17:52:56 -08004734 fastboot_publish("version-bootloader", (const char *) device.bootloader_version);
4735 fastboot_publish("version-baseband", (const char *) device.radio_version);
Mayank Grover7b8a8f82017-10-27 13:07:59 +05304736#endif
lijuangf16461c2015-08-03 17:09:34 +08004737 fastboot_publish("secure", is_secure_boot_enable()? "yes":"no");
Mayank Grover7b8a8f82017-10-27 13:07:59 +05304738 fastboot_publish("unlocked", device.is_unlocked ? "yes":"no");
lijuangf16461c2015-08-03 17:09:34 +08004739 smem_get_hw_platform_name((unsigned char *) hw_platform_buf, sizeof(hw_platform_buf));
4740 snprintf(get_variant, MAX_RSP_SIZE, "%s %s", hw_platform_buf,
4741 target_is_emmc_boot()? "eMMC":"UFS");
4742 fastboot_publish("variant", (const char *) get_variant);
lijuang65c5a822015-08-29 16:35:36 +08004743#if CHECK_BAT_VOLTAGE
lijuang102dfa92015-10-09 18:31:03 +08004744 update_battery_status();
lijuang65c5a822015-08-29 16:35:36 +08004745 fastboot_publish("battery-voltage", (const char *) battery_voltage);
lijuang102dfa92015-10-09 18:31:03 +08004746 fastboot_publish("battery-soc-ok", (const char *) battery_soc_ok);
lijuang65c5a822015-08-29 16:35:36 +08004747#endif
Amol Jadi5edf3552013-07-23 14:15:34 -07004748}
4749
Brian Swetland9c4c0752009-01-25 16:23:50 -08004750void aboot_init(const struct app_descriptor *app)
4751{
Shashank Mittal4f99a882010-02-01 13:58:50 -08004752 unsigned reboot_mode = 0;
Mayank Grover351a75e2017-05-30 20:06:08 +05304753 int boot_err_type = 0;
4754 int boot_slot = INVALID;
Chandan Uddarajubedca152010-06-02 23:05:15 -07004755
lijuang39831732016-01-08 17:49:02 +08004756 /* Initialise wdog to catch early lk crashes */
4757#if WDOG_SUPPORT
4758 msm_wdog_init();
4759#endif
4760
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07004761 /* Setup page size information for nv storage */
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004762 if (target_is_emmc_boot())
4763 {
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07004764 page_size = mmc_page_size();
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004765 page_mask = page_size - 1;
Gaurav Nebhwani4d2389c2016-03-17 21:10:05 +05304766 mmc_blocksize = mmc_get_device_blocksize();
4767 mmc_blocksize_mask = mmc_blocksize - 1;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004768 }
4769 else
4770 {
4771 page_size = flash_page_size();
4772 page_mask = page_size - 1;
4773 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07004774 ASSERT((MEMBASE + MEMSIZE) > MEMBASE);
4775
Ameya Thakur11cf1a62013-08-05 12:44:48 -07004776 read_device_info(&device);
vijay kumarc65876c2015-04-24 13:29:16 +05304777 read_allow_oem_unlock(&device);
Shashank Mittal162244e2011-08-08 19:01:25 -07004778
Mayank Grover351a75e2017-05-30 20:06:08 +05304779 /* Detect multi-slot support */
4780 if (partition_multislot_is_supported())
4781 {
4782 boot_slot = partition_find_active_slot();
4783 if (boot_slot == INVALID)
4784 {
4785 boot_into_fastboot = true;
4786 dprintf(INFO, "Active Slot: (INVALID)\n");
4787 }
4788 else
4789 {
4790 /* Setting the state of system to boot active slot */
4791 partition_mark_active_slot(boot_slot);
4792 dprintf(INFO, "Active Slot: (%s)\n", SUFFIX_SLOT(boot_slot));
4793 }
4794 }
4795
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08004796 /* Display splash screen if enabled */
4797#if DISPLAY_SPLASH_SCREEN
lijuang99c02d82015-02-13 19:04:34 +08004798#if NO_ALARM_DISPLAY
4799 if (!check_alarm_boot()) {
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08004800#endif
lijuang99c02d82015-02-13 19:04:34 +08004801 dprintf(SPEW, "Display Init: Start\n");
Ajay Singh Parmara7865a82015-04-16 21:27:52 -07004802#if DISPLAY_HDMI_PRIMARY
4803 if (!strlen(device.display_panel))
4804 strlcpy(device.display_panel, DISPLAY_PANEL_HDMI,
4805 sizeof(device.display_panel));
4806#endif
Channagoud Kadabi90869ce2015-04-27 11:15:14 -07004807#if ENABLE_WBC
Channagoud Kadabid801ac72015-08-26 11:21:51 -07004808 /* Wait if the display shutdown is in progress */
4809 while(pm_app_display_shutdown_in_prgs());
Channagoud Kadabi90869ce2015-04-27 11:15:14 -07004810 if (!pm_appsbl_display_init_done())
4811 target_display_init(device.display_panel);
4812 else
4813 display_image_on_screen();
4814#else
lijuang99c02d82015-02-13 19:04:34 +08004815 target_display_init(device.display_panel);
Channagoud Kadabi90869ce2015-04-27 11:15:14 -07004816#endif
lijuang99c02d82015-02-13 19:04:34 +08004817 dprintf(SPEW, "Display Init: Done\n");
4818#if NO_ALARM_DISPLAY
4819 }
4820#endif
4821#endif
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08004822
Greg Griscod6250552011-06-29 14:40:23 -07004823 target_serialno((unsigned char *) sn_buf);
Ajay Dudanib06c05f2011-05-12 14:46:10 -07004824 dprintf(SPEW,"serial number: %s\n",sn_buf);
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -08004825
Dhaval Patel223ec952013-07-18 14:49:44 -07004826 memset(display_panel_buf, '\0', MAX_PANEL_BUF_SIZE);
4827
Matthew Qindefd5562014-07-11 18:02:40 +08004828 /*
4829 * Check power off reason if user force reset,
4830 * if yes phone will do normal boot.
4831 */
4832 if (is_user_force_reset())
4833 goto normal_boot;
4834
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07004835 /* Check if we should do something other than booting up */
Ameya Thakur0b9c2442013-05-10 13:22:56 -07004836 if (keys_get_state(KEY_VOLUMEUP) && keys_get_state(KEY_VOLUMEDOWN))
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07004837 {
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03004838 dprintf(ALWAYS,"dload mode key sequence detected\n");
lijuang395b5e62015-11-19 17:39:44 +08004839 reboot_device(EMERGENCY_DLOAD);
4840 dprintf(CRITICAL,"Failed to reboot into dload mode\n");
4841
Ameya Thakur0b9c2442013-05-10 13:22:56 -07004842 boot_into_fastboot = true;
4843 }
4844 if (!boot_into_fastboot)
4845 {
4846 if (keys_get_state(KEY_HOME) || keys_get_state(KEY_VOLUMEUP))
4847 boot_into_recovery = 1;
4848 if (!boot_into_recovery &&
4849 (keys_get_state(KEY_BACK) || keys_get_state(KEY_VOLUMEDOWN)))
Pavel Nedev5d91d412013-04-29 11:34:24 +03004850 boot_into_fastboot = true;
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07004851 }
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004852 #if NO_KEYPAD_DRIVER
Kinson Chik0b1c8162011-08-31 16:31:57 -07004853 if (fastboot_trigger())
Pavel Nedev5d91d412013-04-29 11:34:24 +03004854 boot_into_fastboot = true;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004855 #endif
Chandan Uddarajubedca152010-06-02 23:05:15 -07004856
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07004857#if USE_PON_REBOOT_REG
4858 reboot_mode = check_hard_reboot_mode();
4859#else
Ajay Dudani77421292010-10-27 19:34:06 -07004860 reboot_mode = check_reboot_mode();
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07004861#endif
4862 if (reboot_mode == RECOVERY_MODE)
4863 {
Ajay Dudani77421292010-10-27 19:34:06 -07004864 boot_into_recovery = 1;
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07004865 }
4866 else if(reboot_mode == FASTBOOT_MODE)
4867 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03004868 boot_into_fastboot = true;
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07004869 }
4870 else if(reboot_mode == ALARM_BOOT)
4871 {
Matthew Qind886f3c2014-01-17 16:52:01 +08004872 boot_reason_alarm = true;
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07004873 }
Parth Dixit207573a2015-10-27 17:26:21 +05304874#if VERIFIED_BOOT
Mayank Grover912eaa62017-10-26 12:08:53 +05304875 else if (VB_M <= target_get_vb_version())
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07004876 {
Mayank Grover889be1b2017-09-12 20:12:23 +05304877 if (reboot_mode == DM_VERITY_ENFORCING)
4878 {
4879 device.verity_mode = 1;
4880 write_device_info(&device);
4881 }
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07004882#if ENABLE_VB_ATTEST
Mayank Grover889be1b2017-09-12 20:12:23 +05304883 else if (reboot_mode == DM_VERITY_EIO)
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07004884#else
Mayank Grover889be1b2017-09-12 20:12:23 +05304885 else if (reboot_mode == DM_VERITY_LOGGING)
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07004886#endif
Mayank Grover889be1b2017-09-12 20:12:23 +05304887 {
4888 device.verity_mode = 0;
4889 write_device_info(&device);
4890 }
4891 else if (reboot_mode == DM_VERITY_KEYSCLEAR)
4892 {
4893 if(send_delete_keys_to_tz())
4894 ASSERT(0);
4895 }
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07004896 }
Parth Dixitddbc7352015-10-18 03:13:31 +05304897#endif
Ajay Dudani77421292010-10-27 19:34:06 -07004898
Matthew Qindefd5562014-07-11 18:02:40 +08004899normal_boot:
Pavel Nedev5d91d412013-04-29 11:34:24 +03004900 if (!boot_into_fastboot)
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004901 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03004902 if (target_is_emmc_boot())
Shashank Mittala0032282011-08-26 14:50:11 -07004903 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03004904 if(emmc_recovery_init())
4905 dprintf(ALWAYS,"error in emmc_recovery_init\n");
4906 if(target_use_signed_kernel())
Shashank Mittala0032282011-08-26 14:50:11 -07004907 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03004908 if((device.is_unlocked) || (device.is_tampered))
4909 {
4910 #ifdef TZ_TAMPER_FUSE
Monika Singh98257462018-06-06 11:28:49 +05304911 set_tamper_fuse_cmd(HLOS_IMG_TAMPER_FUSE);
Pavel Nedev5d91d412013-04-29 11:34:24 +03004912 #endif
4913 #if USE_PCOM_SECBOOT
4914 set_tamper_flag(device.is_tampered);
4915 #endif
4916 }
Shashank Mittala0032282011-08-26 14:50:11 -07004917 }
Amit Blay6281ebc2015-01-11 14:44:08 +02004918
Mayank Grover351a75e2017-05-30 20:06:08 +05304919retry_boot:
4920 /* Trying to boot active partition */
4921 if (partition_multislot_is_supported())
4922 {
4923 boot_slot = partition_find_boot_slot();
4924 partition_mark_active_slot(boot_slot);
4925 if (boot_slot == INVALID)
4926 goto fastboot;
4927 }
4928
4929 boot_err_type = boot_linux_from_mmc();
4930 switch (boot_err_type)
4931 {
4932 case ERR_INVALID_PAGE_SIZE:
4933 case ERR_DT_PARSE:
4934 case ERR_ABOOT_ADDR_OVERLAP:
Mayank Grover10cabbe2017-10-30 19:11:05 +05304935 case ERR_INVALID_BOOT_MAGIC:
Mayank Grover351a75e2017-05-30 20:06:08 +05304936 if(partition_multislot_is_supported())
Mayank Grover10cabbe2017-10-30 19:11:05 +05304937 {
4938 /*
4939 * Deactivate current slot, as it failed to
4940 * boot, and retry next slot.
4941 */
4942 partition_deactivate_slot(boot_slot);
Mayank Grover351a75e2017-05-30 20:06:08 +05304943 goto retry_boot;
Mayank Grover10cabbe2017-10-30 19:11:05 +05304944 }
Mayank Grover351a75e2017-05-30 20:06:08 +05304945 else
4946 break;
Mayank Grover351a75e2017-05-30 20:06:08 +05304947 default:
4948 break;
4949 /* going to fastboot menu */
4950 }
Shashank Mittala0032282011-08-26 14:50:11 -07004951 }
Pavel Nedev5d91d412013-04-29 11:34:24 +03004952 else
4953 {
4954 recovery_init();
4955 #if USE_PCOM_SECBOOT
4956 if((device.is_unlocked) || (device.is_tampered))
4957 set_tamper_flag(device.is_tampered);
4958 #endif
4959 boot_linux_from_flash();
4960 }
4961 dprintf(CRITICAL, "ERROR: Could not do normal boot. Reverting "
4962 "to fastboot mode.\n");
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004963 }
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07004964
Mayank Grover351a75e2017-05-30 20:06:08 +05304965fastboot:
Amol Jadi5edf3552013-07-23 14:15:34 -07004966 /* We are here means regular boot did not happen. Start fastboot. */
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07004967
Amol Jadi5edf3552013-07-23 14:15:34 -07004968 /* register aboot specific fastboot commands */
4969 aboot_fastboot_register_commands();
Amol Jadi57abe4c2011-05-24 15:47:27 -07004970
Amol Jadi5edf3552013-07-23 14:15:34 -07004971 /* dump partition table for debug info */
Kinson Chikf1a43512011-07-14 11:28:39 -07004972 partition_dump();
Amol Jadi5edf3552013-07-23 14:15:34 -07004973
4974 /* initialize and start fastboot */
4975 fastboot_init(target_get_scratch_address(), target_get_max_flash_size());
lijuang4ece1e72015-08-14 21:02:36 +08004976#if FBCON_DISPLAY_MSG
lijuang9a7d3b92015-11-30 14:41:24 +08004977 display_fastboot_menu();
lijuang4ece1e72015-08-14 21:02:36 +08004978#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08004979}
4980
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07004981uint32_t get_page_size()
4982{
4983 return page_size;
4984}
4985
Amir Samuelov57a6fa22013-06-05 16:36:43 +03004986/*
4987 * Calculated and save hash (SHA256) for non-signed boot image.
4988 *
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07004989 * @param image_addr - Boot image address
4990 * @param image_size - Size of the boot image
Amir Samuelov57a6fa22013-06-05 16:36:43 +03004991 *
4992 * @return int - 0 on success, negative value on failure.
4993 */
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07004994static int aboot_save_boot_hash_mmc(uint32_t image_addr, uint32_t image_size)
Amir Samuelov57a6fa22013-06-05 16:36:43 +03004995{
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07004996 unsigned int digest[8];
4997#if IMAGE_VERIF_ALGO_SHA1
4998 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA1;
4999#else
5000 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA256;
5001#endif
Amir Samuelov57a6fa22013-06-05 16:36:43 +03005002
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07005003 target_crypto_init_params();
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -08005004 hash_find((unsigned char *) image_addr, image_size, (unsigned char *)&digest, auth_algo);
Amir Samuelov57a6fa22013-06-05 16:36:43 +03005005
5006 save_kernel_hash_cmd(digest);
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07005007 dprintf(INFO, "aboot_save_boot_hash_mmc: imagesize_actual size %d bytes.\n", (int) image_size);
Amir Samuelov57a6fa22013-06-05 16:36:43 +03005008
5009 return 0;
5010}
5011
Mayank Grover351a75e2017-05-30 20:06:08 +05305012
Brian Swetland9c4c0752009-01-25 16:23:50 -08005013APP_START(aboot)
5014 .init = aboot_init,
5015APP_END