blob: 534db14a50c9487425362881578e8c1d183dc533 [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};
Mayank Grovere1ab96c2018-09-04 20:31:31 +0530251static void *recovery_dtbo_buf = NULL;
252static uint32_t recovery_dtbo_size = 0;
253
Shashank Mittalcd98d472011-08-02 14:29:24 -0700254/* Assuming unauthorized kernel image by default */
255static int auth_kernel_img = 0;
Monika Singh292b3e92018-03-17 22:40:23 +0530256static device_info device = {DEVICE_MAGIC,0,0,0,0,{0},{0},{0},1,{0},0,{0}};
257
vijay kumarc65876c2015-04-24 13:29:16 +0530258static bool is_allow_unlock = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -0700259
vijay kumarca2e6812015-07-08 20:28:25 +0530260static char frp_ptns[2][8] = {"config","frp"};
261
lijuang511a2b52015-08-14 20:50:51 +0800262static const char *critical_flash_allowed_ptn[] = {
263 "aboot",
264 "rpm",
265 "tz",
266 "sbl",
267 "sdi",
268 "sbl1",
269 "xbl",
270 "hyp",
271 "pmic",
272 "bootloader",
273 "devinfo",
274 "partition"};
275
Dima Zavin42168f22009-01-30 11:52:22 -0800276struct atag_ptbl_entry
277{
278 char name[16];
279 unsigned offset;
280 unsigned size;
281 unsigned flags;
282};
283
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700284/*
285 * Partition info, required to be published
286 * for fastboot
287 */
288struct getvar_partition_info {
Mayank Grover7b8a8f82017-10-27 13:07:59 +0530289 char part_name[MAX_GPT_NAME_SIZE]; /* Partition name */
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700290 char getvar_size[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for size */
291 char getvar_type[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for type */
292 char size_response[MAX_RSP_SIZE]; /* fastboot response for size */
293 char type_response[MAX_RSP_SIZE]; /* fastboot response for type */
294};
295
296/*
Mayank Grover7b8a8f82017-10-27 13:07:59 +0530297 * Update the part_type_known for known paritions types.
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700298 */
Mayank Groverd38fe012018-03-13 15:33:16 +0530299#define RAW_STR "raw"
Mayank Grover52cd10a2018-03-15 12:57:54 +0530300#define EXT_STR "ext4"
301#define F2FS_STR "f2fs"
302
303#define FS_SUPERBLOCK_OFFSET 0x400
304#define EXT_MAGIC 0xEF53
305#define EXT_MAGIC_OFFSET_SB 0x38
306#define F2FS_MAGIC 0xF2F52010 // F2FS Magic Number
307#define F2FS_MAGIC_OFFSET_SB 0x0
308
309typedef enum fs_signature_type {
310 EXT_FS_SIGNATURE = 1,
311 EXT_F2FS_SIGNATURE = 2,
312 NO_FS = -1
313} fs_signature_type;
314
Mayank Grover7b8a8f82017-10-27 13:07:59 +0530315struct getvar_partition_info part_info[NUM_PARTITIONS];
316struct getvar_partition_info part_type_known[] =
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700317{
Mayank Grover49920212018-02-09 18:15:55 +0530318 { "system" , "partition-size:", "partition-type:", "", "ext4" },
319 { "userdata" , "partition-size:", "partition-type:", "", "ext4" },
320 { "cache" , "partition-size:", "partition-type:", "", "ext4" },
321 { "recoveryfs" , "partition-size:", "partition-type:", "", "ext4" },
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700322};
323
324char max_download_size[MAX_RSP_SIZE];
Ameya Thakur11cf1a62013-08-05 12:44:48 -0700325char charger_screen_enabled[MAX_RSP_SIZE];
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800326char sn_buf[13];
Dhaval Patel223ec952013-07-18 14:49:44 -0700327char display_panel_buf[MAX_PANEL_BUF_SIZE];
Unnati Gandhi62c8ab82014-01-24 11:01:01 +0530328char panel_display_mode[MAX_RSP_SIZE];
Mayank Grovera64dfbe2018-05-17 14:06:34 +0530329char soc_version_str[MAX_RSP_SIZE];
Mayank Grover7b8a8f82017-10-27 13:07:59 +0530330char block_size_string[MAX_RSP_SIZE];
Mukesh Ojhabb6e7f32018-03-02 15:04:06 +0530331#if PRODUCT_IOT
Mayank Grover7b8a8f82017-10-27 13:07:59 +0530332
333/* For IOT we are using custom version */
334#define PRODUCT_IOT_VERSION "IOT001"
335char bootloader_version_string[MAX_RSP_SIZE];
336#endif
lijuang102dfa92015-10-09 18:31:03 +0800337
338#if CHECK_BAT_VOLTAGE
lijuang65c5a822015-08-29 16:35:36 +0800339char battery_voltage[MAX_RSP_SIZE];
lijuang102dfa92015-10-09 18:31:03 +0800340char battery_soc_ok [MAX_RSP_SIZE];
341#endif
342
lijuangf16461c2015-08-03 17:09:34 +0800343char get_variant[MAX_RSP_SIZE];
Greg Griscod6250552011-06-29 14:40:23 -0700344
Greg Griscod2471ef2011-07-14 13:00:42 -0700345extern int emmc_recovery_init(void);
346
Kinson Chik0b1c8162011-08-31 16:31:57 -0700347#if NO_KEYPAD_DRIVER
348extern int fastboot_trigger(void);
349#endif
Greg Griscod2471ef2011-07-14 13:00:42 -0700350
Mayank Grovere1ab96c2018-09-04 20:31:31 +0530351static void update_ker_tags_rdisk_addr(boot_img_hdr *hdr, bool is_arm64)
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700352{
353 /* overwrite the destination of specified for the project */
Channagoud Kadabi7042fa32013-04-26 16:44:14 -0700354#ifdef ABOOT_IGNORE_BOOT_HEADER_ADDRS
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800355 if (is_arm64)
356 hdr->kernel_addr = ABOOT_FORCE_KERNEL64_ADDR;
357 else
358 hdr->kernel_addr = ABOOT_FORCE_KERNEL_ADDR;
Channagoud Kadabi7042fa32013-04-26 16:44:14 -0700359 hdr->ramdisk_addr = ABOOT_FORCE_RAMDISK_ADDR;
360 hdr->tags_addr = ABOOT_FORCE_TAGS_ADDR;
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700361#endif
362}
363
Dima Zavin42168f22009-01-30 11:52:22 -0800364static void ptentry_to_tag(unsigned **ptr, struct ptentry *ptn)
365{
366 struct atag_ptbl_entry atag_ptn;
367
368 memcpy(atag_ptn.name, ptn->name, 16);
369 atag_ptn.name[15] = '\0';
370 atag_ptn.offset = ptn->start;
371 atag_ptn.size = ptn->length;
372 atag_ptn.flags = ptn->flags;
373 memcpy(*ptr, &atag_ptn, sizeof(struct atag_ptbl_entry));
374 *ptr += sizeof(struct atag_ptbl_entry) / sizeof(unsigned);
375}
Mayank Grover9df84c02018-08-30 15:46:35 +0530376#ifdef VERIFIED_BOOT_2
377void load_vbmeta_image(void **vbmeta_image_buf, uint32_t *vbmeta_image_sz)
378{
379 int index = 0;
380 char *vbm_img_buf = NULL;
381 unsigned long long ptn = 0;
382 unsigned long long ptn_size = 0;
383
384 /* Immediately return if dtbo is not supported */
385 index = partition_get_index("vbmeta");
386 ptn = partition_get_offset(index);
387 if(!ptn)
388 {
389 dprintf(CRITICAL, "ERROR: vbmeta partition not found.\n");
390 return;
391 }
392
393 ptn_size = partition_get_size(index);
394 if (ptn_size > MAX_SUPPORTED_VBMETA_IMG_BUF)
395 {
396 dprintf(CRITICAL, "ERROR: vbmeta parition size is greater than supported.\n");
397 return;
398 }
399
400 vbm_img_buf = (char *)memalign(CACHE_LINE, ROUNDUP((uint32_t)ptn_size, CACHE_LINE));
401 if (!vbm_img_buf)
402 {
403 dprintf(CRITICAL, "ERROR: vbmeta unable to locate buffer\n");
404 return;
405 }
406
407 mmc_set_lun(partition_get_lun(index));
408 if (mmc_read(ptn, (uint32_t *)vbm_img_buf, (uint32_t)ptn_size))
409 {
410 dprintf(CRITICAL, "ERROR: vbmeta read failure\n");
411 free(vbm_img_buf);
412 return;
413 }
414
415 *vbmeta_image_buf = vbm_img_buf;
416 *vbmeta_image_sz = (uint32_t)ptn_size;
417 return;
418}
419#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -0800420
lijuang102dfa92015-10-09 18:31:03 +0800421#if CHECK_BAT_VOLTAGE
422void update_battery_status(void)
423{
424 snprintf(battery_voltage,MAX_RSP_SIZE, "%d",target_get_battery_voltage());
425 snprintf(battery_soc_ok ,MAX_RSP_SIZE, "%s",target_battery_soc_ok()? "yes":"no");
426}
427#endif
428
Neeti Desaie245d492012-06-01 12:52:13 -0700429unsigned char *update_cmdline(const char * cmdline)
Brian Swetland9c4c0752009-01-25 16:23:50 -0800430{
David Ng183a7422009-12-07 14:55:21 -0800431 int cmdline_len = 0;
432 int have_cmdline = 0;
Amol Jadi168b7712012-03-06 16:15:00 -0800433 unsigned char *cmdline_final = NULL;
Neeti Desaie245d492012-06-01 12:52:13 -0700434 int pause_at_bootup = 0;
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800435 bool warm_boot = false;
Pavel Nedev5614d222013-06-17 18:01:02 +0300436 bool gpt_exists = partition_gpt_exists();
Joonwoo Park61112782013-10-02 19:50:39 -0700437 int have_target_boot_params = 0;
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700438 char *boot_dev_buf = NULL;
Monika Singh292b3e92018-03-17 22:40:23 +0530439#ifdef MDTP_SUPPORT
Mayank Grover351a75e2017-05-30 20:06:08 +0530440 bool is_mdtp_activated = 0;
Monika Singh292b3e92018-03-17 22:40:23 +0530441#endif
Mayank Grover351a75e2017-05-30 20:06:08 +0530442 int current_active_slot = INVALID;
Mayank Grover3804be72017-06-22 11:59:23 +0530443 int system_ptn_index = -1;
444 unsigned int lun = 0;
445 char lun_char_base = 'a';
Sourabh Banerjee386b1322018-02-27 09:37:28 +0530446#if VERITY_LE
447 int syspath_buflen = strlen(verity_dev)
448 + strlen(verity_system_part) + (sizeof(char) * 2) + 2
449 + strlen(verity_params) + sizeof(int) + 2;
450#else
lijuang83ef4b22018-08-23 11:01:55 +0800451 int syspath_buflen = strlen(sys_path) + sizeof(int) + 2; /*allocate buflen for largest possible string*/
452 char dtbo_idx_str[MAX_DTBO_IDX_STR] = "\0";
453 int dtbo_idx = INVALID_PTN;
Sourabh Banerjee386b1322018-02-27 09:37:28 +0530454#endif
Mayank Grover3804be72017-06-22 11:59:23 +0530455 char syspath_buf[syspath_buflen];
Mayank Grover466d6562018-05-10 14:52:20 +0530456#if HIBERNATION_SUPPORT
457 int resume_buflen = strlen(resume) + sizeof(int) + 2;
458 char resume_buf[resume_buflen];
459 int swap_ptn_index = INVALID_PTN;
460#endif
461
Mayank Grover889be1b2017-09-12 20:12:23 +0530462#if VERIFIED_BOOT
463 uint32_t boot_state = RED;
464#endif
Hareesh Gauthamabf1e1e2017-04-12 18:09:24 +0530465
466#if USE_LE_SYSTEMD
467 is_systemd_present=true;
468#endif
469
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700470#if VERIFIED_BOOT
Mayank Grover912eaa62017-10-26 12:08:53 +0530471 if (VB_M <= target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +0530472 {
473 boot_state = boot_verify_get_state();
474 }
Parth Dixitddbc7352015-10-18 03:13:31 +0530475#endif
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700476
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200477#ifdef MDTP_SUPPORT
478 mdtp_activated(&is_mdtp_activated);
479#endif /* MDTP_SUPPORT */
Dima Zavin42168f22009-01-30 11:52:22 -0800480
Brian Swetland9c4c0752009-01-25 16:23:50 -0800481 if (cmdline && cmdline[0]) {
David Ng183a7422009-12-07 14:55:21 -0800482 cmdline_len = strlen(cmdline);
483 have_cmdline = 1;
484 }
485 if (target_is_emmc_boot()) {
486 cmdline_len += strlen(emmc_cmdline);
Sridhar Parasuram7bd4aaf2015-02-12 11:14:38 -0800487#if USE_BOOTDEV_CMDLINE
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700488 boot_dev_buf = (char *) malloc(sizeof(char) * BOOT_DEV_MAX_LEN);
489 ASSERT(boot_dev_buf);
490 platform_boot_dev_cmdline(boot_dev_buf);
491 cmdline_len += strlen(boot_dev_buf);
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700492#endif
David Ng183a7422009-12-07 14:55:21 -0800493 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800494
495 cmdline_len += strlen(usb_sn_cmdline);
496 cmdline_len += strlen(sn_buf);
497
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700498#if VERIFIED_BOOT
Mayank Grover912eaa62017-10-26 12:08:53 +0530499 if (VB_M <= target_get_vb_version())
Sridhar Parasuram7d8bb9a2015-09-23 18:21:19 -0700500 {
Mayank Grover889be1b2017-09-12 20:12:23 +0530501 cmdline_len += strlen(verified_state) + strlen(vbsn[boot_state].name);
502 if ((device.verity_mode != 0 ) && (device.verity_mode != 1))
503 {
504 dprintf(CRITICAL, "Devinfo paritition possibly corrupted!!!. Please erase devinfo partition to continue booting\n");
505 ASSERT(0);
506 }
507 cmdline_len += strlen(verity_mode) + strlen(vbvm[device.verity_mode].name);
508 cmdline_len += strlen(keymaster_v1);
Sridhar Parasuram7d8bb9a2015-09-23 18:21:19 -0700509 }
Parth Dixitddbc7352015-10-18 03:13:31 +0530510#endif
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700511
Monika Singh292b3e92018-03-17 22:40:23 +0530512
513 if (vbcmdline != NULL) {
514 dprintf(DEBUG, "UpdateCmdLine vbcmdline present len %d\n",
515 strlen(vbcmdline));
516 cmdline_len += strlen(vbcmdline);
517 }
518
Pavel Nedev5614d222013-06-17 18:01:02 +0300519 if (boot_into_recovery && gpt_exists)
520 cmdline_len += strlen(secondary_gpt_enable);
521
Monika Singh292b3e92018-03-17 22:40:23 +0530522#ifdef MDTP_SUPPORT
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200523 if(is_mdtp_activated)
524 cmdline_len += strlen(mdtp_activated_flag);
Monika Singh292b3e92018-03-17 22:40:23 +0530525#endif
Pavel Nedev328ac822013-04-05 15:25:11 +0300526 if (boot_into_ffbm) {
527 cmdline_len += strlen(androidboot_mode);
Hareesh Gauthamabf1e1e2017-04-12 18:09:24 +0530528
529 if(is_systemd_present)
530 cmdline_len += strlen(systemd_ffbm_mode);
531
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700532 cmdline_len += strlen(ffbm_mode_string);
Pavel Nedev898298c2013-02-27 12:36:09 -0800533 /* reduce kernel console messages to speed-up boot */
534 cmdline_len += strlen(loglevel);
Matthew Qind886f3c2014-01-17 16:52:01 +0800535 } else if (boot_reason_alarm) {
536 cmdline_len += strlen(alarmboot_cmdline);
Zhenhua Huang431dafa2015-06-30 16:13:37 +0800537 } else if ((target_build_variant_user() || device.charger_screen_enabled)
lijuang266b17d2018-08-17 18:53:42 +0800538 && target_pause_for_battery_charge() && !boot_into_recovery) {
David Ngf773dde2010-07-26 19:55:08 -0700539 pause_at_bootup = 1;
540 cmdline_len += strlen(battchg_pause);
541 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800542
Shashank Mittalcd98d472011-08-02 14:29:24 -0700543 if(target_use_signed_kernel() && auth_kernel_img) {
544 cmdline_len += strlen(auth_kernel);
545 }
546
Joonwoo Park61112782013-10-02 19:50:39 -0700547 if (get_target_boot_params(cmdline, boot_into_recovery ? "recoveryfs" :
548 "system",
vijay kumar870515d2015-08-31 16:37:24 +0530549 &target_boot_params) == 0) {
Joonwoo Park61112782013-10-02 19:50:39 -0700550 have_target_boot_params = 1;
551 cmdline_len += strlen(target_boot_params);
552 }
553
Ajay Dudanid04110c2011-01-17 23:55:07 -0800554 /* Determine correct androidboot.baseband to use */
555 switch(target_baseband())
556 {
557 case BASEBAND_APQ:
558 cmdline_len += strlen(baseband_apq);
559 break;
560
561 case BASEBAND_MSM:
562 cmdline_len += strlen(baseband_msm);
563 break;
564
565 case BASEBAND_CSFB:
566 cmdline_len += strlen(baseband_csfb);
567 break;
568
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800569 case BASEBAND_SVLTE2A:
570 cmdline_len += strlen(baseband_svlte2a);
Ajay Dudanid04110c2011-01-17 23:55:07 -0800571 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700572
573 case BASEBAND_MDM:
574 cmdline_len += strlen(baseband_mdm);
575 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700576
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800577 case BASEBAND_MDM2:
578 cmdline_len += strlen(baseband_mdm2);
579 break;
580
Amol Jadi5c61a952012-05-04 17:05:35 -0700581 case BASEBAND_SGLTE:
582 cmdline_len += strlen(baseband_sglte);
583 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530584
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800585 case BASEBAND_SGLTE2:
586 cmdline_len += strlen(baseband_sglte2);
587 break;
588
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530589 case BASEBAND_DSDA:
590 cmdline_len += strlen(baseband_dsda);
591 break;
Amol Jadi2a15a272013-01-22 12:03:36 -0800592
593 case BASEBAND_DSDA2:
594 cmdline_len += strlen(baseband_dsda2);
595 break;
Vijay Kumar Pendotib228cfc2016-06-13 20:15:23 +0530596 case BASEBAND_APQ_NOWGR:
597 cmdline_len += strlen(baseband_apq_nowgr);
598 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800599 }
600
Vladislav Levenetz3cd45242017-04-05 08:58:07 +0300601#if ENABLE_DISPLAY
Lijuan Gao4a5b8322014-07-24 10:38:42 +0800602 if (cmdline) {
603 if ((strstr(cmdline, DISPLAY_DEFAULT_PREFIX) == NULL) &&
Padmanabhan Komandurubccbcdc2015-06-30 16:19:24 +0530604 target_display_panel_node(display_panel_buf,
605 MAX_PANEL_BUF_SIZE) &&
Lijuan Gao4a5b8322014-07-24 10:38:42 +0800606 strlen(display_panel_buf)) {
607 cmdline_len += strlen(display_panel_buf);
608 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700609 }
Vladislav Levenetz3cd45242017-04-05 08:58:07 +0300610#endif
Dhaval Patel223ec952013-07-18 14:49:44 -0700611
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800612 if (target_warm_boot()) {
613 warm_boot = true;
614 cmdline_len += strlen(warmboot_cmdline);
615 }
616
Mayank Grover5384ed82018-05-09 12:09:24 +0530617 if (target_uses_system_as_root() ||
618 partition_multislot_is_supported())
Mayank Grover351a75e2017-05-30 20:06:08 +0530619 {
620 current_active_slot = partition_find_active_slot();
621 cmdline_len += (strlen(androidboot_slot_suffix)+
622 strlen(SUFFIX_SLOT(current_active_slot)));
623
Mayank Grover3804be72017-06-22 11:59:23 +0530624 system_ptn_index = partition_get_index("system");
625 if (platform_boot_dev_isemmc())
626 {
Sourabh Banerjee386b1322018-02-27 09:37:28 +0530627#if VERITY_LE
628 /*
629 Condition 4: Verity and A/B both enabled
630 Eventual command line looks like:
631 ... androidboot.slot_suffix=<slot_suffix> ... rootfstype=ext4 ...
632 ... root=/dev/dm-0 dm="system_<slot_suffix> none ro,0 1 android-verity /dev/mmcblk0p<NN>"
633 */
634 snprintf(syspath_buf, syspath_buflen, " %s %s%s %s%d\"",
635 verity_dev,
636 verity_system_part, suffix_slot[current_active_slot],
637 verity_params, system_ptn_index + 1);
638#else
639 /*
640 Condition 5: A/B enabled, but verity disabled
641 Eventual command line looks like:
642 ... androidboot.slot_suffix=<slot_suffix> ... rootfstype=ext4 ...
643 ... root=/dev/mmcblk0p<NN> ...
644 */
645 snprintf(syspath_buf, syspath_buflen, " %s%d",
646 sys_path, system_ptn_index + 1);
647#endif
Mayank Grover3804be72017-06-22 11:59:23 +0530648 }
649 else
650 {
651 lun = partition_get_lun(system_ptn_index);
652 snprintf(syspath_buf, syspath_buflen, " root=/dev/sd%c%d",
653 lun_char_base + lun,
654 partition_get_index_in_lun("system", lun));
655 }
Mayank Grover351a75e2017-05-30 20:06:08 +0530656
Monika Singh292b3e92018-03-17 22:40:23 +0530657#ifndef VERIFIED_BOOT_2
Mayank Grover3804be72017-06-22 11:59:23 +0530658 cmdline_len += strlen(syspath_buf);
Monika Singh292b3e92018-03-17 22:40:23 +0530659#endif
Mayank Grover5384ed82018-05-09 12:09:24 +0530660 }
661
662 if (target_uses_system_as_root() ||
663 partition_multislot_is_supported())
664 {
665 cmdline_len += strlen(sys_path_cmdline);
Mayank Grover351a75e2017-05-30 20:06:08 +0530666 if (!boot_into_recovery)
667 cmdline_len += strlen(skip_ramfs);
668 }
669
Mayank Grover466d6562018-05-10 14:52:20 +0530670#if HIBERNATION_SUPPORT
671 if (platform_boot_dev_isemmc())
672 {
673 swap_ptn_index = partition_get_index("swap");
674 if (swap_ptn_index != INVALID_PTN)
675 {
676 snprintf(resume_buf, resume_buflen,
677 " %s%d", resume,
678 (swap_ptn_index + 1));
679 cmdline_len += strlen(resume_buf);
680 }
681 else
682 {
683 dprintf(INFO, "WARNING: swap partition not found\n");
684 }
685 }
686#endif
687
Channagoud Kadabi85c7ec32016-01-28 23:09:21 -0800688#if TARGET_CMDLINE_SUPPORT
689 char *target_cmdline_buf = malloc(TARGET_MAX_CMDLNBUF);
690 int target_cmd_line_len;
691 ASSERT(target_cmdline_buf);
692 target_cmd_line_len = target_update_cmdline(target_cmdline_buf);
693 cmdline_len += target_cmd_line_len;
694#endif
695
lijuang83ef4b22018-08-23 11:01:55 +0800696#if !VERITY_LE
697 dtbo_idx = get_dtbo_idx ();
698 if (dtbo_idx != INVALID_PTN) {
699 snprintf(dtbo_idx_str, sizeof(dtbo_idx_str), "%s%d",
700 android_boot_dtbo_idx, dtbo_idx);
701 cmdline_len += strlen (dtbo_idx_str);
702 }
703#endif
704
David Ng183a7422009-12-07 14:55:21 -0800705 if (cmdline_len > 0) {
706 const char *src;
Maria Yu52254c02014-07-04 16:14:54 +0800707 unsigned char *dst;
708
709 cmdline_final = (unsigned char*) malloc((cmdline_len + 4) & (~3));
710 ASSERT(cmdline_final != NULL);
vijay kumar287bb542015-09-29 13:01:52 +0530711 memset((void *)cmdline_final, 0, sizeof(*cmdline_final));
Maria Yu52254c02014-07-04 16:14:54 +0800712 dst = cmdline_final;
Neeti Desaie245d492012-06-01 12:52:13 -0700713
Amol Jadi168b7712012-03-06 16:15:00 -0800714 /* Save start ptr for debug print */
David Ng183a7422009-12-07 14:55:21 -0800715 if (have_cmdline) {
716 src = cmdline;
717 while ((*dst++ = *src++));
718 }
719 if (target_is_emmc_boot()) {
720 src = emmc_cmdline;
721 if (have_cmdline) --dst;
David Ngf773dde2010-07-26 19:55:08 -0700722 have_cmdline = 1;
723 while ((*dst++ = *src++));
Sridhar Parasuram7bd4aaf2015-02-12 11:14:38 -0800724#if USE_BOOTDEV_CMDLINE
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700725 src = boot_dev_buf;
726 if (have_cmdline) --dst;
727 while ((*dst++ = *src++));
728#endif
David Ngf773dde2010-07-26 19:55:08 -0700729 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800730
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700731#if VERIFIED_BOOT
Mayank Grover912eaa62017-10-26 12:08:53 +0530732 if (VB_M <= target_get_vb_version())
Sridhar Parasuram7d8bb9a2015-09-23 18:21:19 -0700733 {
Mayank Grover889be1b2017-09-12 20:12:23 +0530734 src = verified_state;
735 if(have_cmdline) --dst;
736 have_cmdline = 1;
737 while ((*dst++ = *src++));
738 src = vbsn[boot_state].name;
739 if(have_cmdline) --dst;
740 while ((*dst++ = *src++));
741
742 if ((device.verity_mode != 0 ) && (device.verity_mode != 1))
743 {
744 dprintf(CRITICAL, "Devinfo paritition possibly corrupted!!!. Please erase devinfo partition to continue booting\n");
745 ASSERT(0);
746 }
747 src = verity_mode;
748 if(have_cmdline) --dst;
749 while ((*dst++ = *src++));
750 src = vbvm[device.verity_mode].name;
751 if(have_cmdline) -- dst;
752 while ((*dst++ = *src++));
753 src = keymaster_v1;
754 if(have_cmdline) --dst;
755 while ((*dst++ = *src++));
Sridhar Parasuram7d8bb9a2015-09-23 18:21:19 -0700756 }
Parth Dixitddbc7352015-10-18 03:13:31 +0530757#endif
Monika Singh292b3e92018-03-17 22:40:23 +0530758
759 if (vbcmdline != NULL) {
760 src = vbcmdline;
761 if (have_cmdline) --dst;
762 while ((*dst++ = *src++));
763 }
764
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800765 src = usb_sn_cmdline;
766 if (have_cmdline) --dst;
767 have_cmdline = 1;
768 while ((*dst++ = *src++));
769 src = sn_buf;
770 if (have_cmdline) --dst;
771 have_cmdline = 1;
772 while ((*dst++ = *src++));
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800773 if (warm_boot) {
774 if (have_cmdline) --dst;
775 src = warmboot_cmdline;
776 while ((*dst++ = *src++));
777 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800778
Pavel Nedev5614d222013-06-17 18:01:02 +0300779 if (boot_into_recovery && gpt_exists) {
780 src = secondary_gpt_enable;
781 if (have_cmdline) --dst;
782 while ((*dst++ = *src++));
783 }
Monika Singh292b3e92018-03-17 22:40:23 +0530784#ifdef MDTP_SUPPORT
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200785 if (is_mdtp_activated) {
786 src = mdtp_activated_flag;
787 if (have_cmdline) --dst;
788 while ((*dst++ = *src++));
789 }
Monika Singh292b3e92018-03-17 22:40:23 +0530790#endif
Pavel Nedev328ac822013-04-05 15:25:11 +0300791 if (boot_into_ffbm) {
792 src = androidboot_mode;
793 if (have_cmdline) --dst;
794 while ((*dst++ = *src++));
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700795 src = ffbm_mode_string;
Pavel Nedev328ac822013-04-05 15:25:11 +0300796 if (have_cmdline) --dst;
797 while ((*dst++ = *src++));
Hareesh Gauthamabf1e1e2017-04-12 18:09:24 +0530798
799 if(is_systemd_present) {
800 src = systemd_ffbm_mode;
801 if (have_cmdline) --dst;
802 while ((*dst++ = *src++));
803 }
804
Pavel Nedev898298c2013-02-27 12:36:09 -0800805 src = loglevel;
806 if (have_cmdline) --dst;
807 while ((*dst++ = *src++));
Matthew Qind886f3c2014-01-17 16:52:01 +0800808 } else if (boot_reason_alarm) {
809 src = alarmboot_cmdline;
810 if (have_cmdline) --dst;
811 while ((*dst++ = *src++));
Pavel Nedev328ac822013-04-05 15:25:11 +0300812 } else if (pause_at_bootup) {
David Ngf773dde2010-07-26 19:55:08 -0700813 src = battchg_pause;
814 if (have_cmdline) --dst;
David Ng183a7422009-12-07 14:55:21 -0800815 while ((*dst++ = *src++));
816 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800817
Shashank Mittalcd98d472011-08-02 14:29:24 -0700818 if(target_use_signed_kernel() && auth_kernel_img) {
819 src = auth_kernel;
820 if (have_cmdline) --dst;
821 while ((*dst++ = *src++));
822 }
823
Ajay Dudanid04110c2011-01-17 23:55:07 -0800824 switch(target_baseband())
825 {
826 case BASEBAND_APQ:
827 src = baseband_apq;
828 if (have_cmdline) --dst;
829 while ((*dst++ = *src++));
830 break;
831
832 case BASEBAND_MSM:
833 src = baseband_msm;
834 if (have_cmdline) --dst;
835 while ((*dst++ = *src++));
836 break;
837
838 case BASEBAND_CSFB:
839 src = baseband_csfb;
840 if (have_cmdline) --dst;
841 while ((*dst++ = *src++));
842 break;
843
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800844 case BASEBAND_SVLTE2A:
845 src = baseband_svlte2a;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800846 if (have_cmdline) --dst;
847 while ((*dst++ = *src++));
848 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700849
850 case BASEBAND_MDM:
851 src = baseband_mdm;
852 if (have_cmdline) --dst;
853 while ((*dst++ = *src++));
854 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700855
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800856 case BASEBAND_MDM2:
857 src = baseband_mdm2;
858 if (have_cmdline) --dst;
859 while ((*dst++ = *src++));
860 break;
861
Amol Jadi5c61a952012-05-04 17:05:35 -0700862 case BASEBAND_SGLTE:
863 src = baseband_sglte;
864 if (have_cmdline) --dst;
865 while ((*dst++ = *src++));
866 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530867
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800868 case BASEBAND_SGLTE2:
869 src = baseband_sglte2;
870 if (have_cmdline) --dst;
871 while ((*dst++ = *src++));
872 break;
873
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530874 case BASEBAND_DSDA:
875 src = baseband_dsda;
876 if (have_cmdline) --dst;
877 while ((*dst++ = *src++));
878 break;
Amol Jadi2a15a272013-01-22 12:03:36 -0800879
880 case BASEBAND_DSDA2:
881 src = baseband_dsda2;
882 if (have_cmdline) --dst;
883 while ((*dst++ = *src++));
884 break;
Vijay Kumar Pendotib228cfc2016-06-13 20:15:23 +0530885 case BASEBAND_APQ_NOWGR:
886 src = baseband_apq_nowgr;
887 if (have_cmdline) --dst;
888 while ((*dst++ = *src++));
889 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800890 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700891
892 if (strlen(display_panel_buf)) {
Dhaval Patel223ec952013-07-18 14:49:44 -0700893 src = display_panel_buf;
894 if (have_cmdline) --dst;
895 while ((*dst++ = *src++));
896 }
Joonwoo Park61112782013-10-02 19:50:39 -0700897
898 if (have_target_boot_params) {
899 if (have_cmdline) --dst;
900 src = target_boot_params;
901 while ((*dst++ = *src++));
vijay kumar870515d2015-08-31 16:37:24 +0530902 free(target_boot_params);
Joonwoo Park61112782013-10-02 19:50:39 -0700903 }
Channagoud Kadabi85c7ec32016-01-28 23:09:21 -0800904
Mayank Grover351a75e2017-05-30 20:06:08 +0530905 if (partition_multislot_is_supported() && have_cmdline)
906 {
907 src = androidboot_slot_suffix;
908 --dst;
909 while ((*dst++ = *src++));
910 --dst;
911 src = SUFFIX_SLOT(current_active_slot);
912 while ((*dst++ = *src++));
Mayank Grover5384ed82018-05-09 12:09:24 +0530913 }
Mayank Grover351a75e2017-05-30 20:06:08 +0530914
Mayank Grover351a75e2017-05-30 20:06:08 +0530915
Mayank Grover5384ed82018-05-09 12:09:24 +0530916 /*
917 * System-As-Root behaviour, system.img should contain both
918 * system content and ramdisk content, and should be mounted at
919 * root(a/b).
920 * Apending skip_ramfs for non a/b builds which use, system as root.
921 */
922 if ((target_uses_system_as_root() ||
923 partition_multislot_is_supported()) &&
924 have_cmdline)
925 {
926 if (!boot_into_recovery)
927 {
928 src = skip_ramfs;
Mayank Grover351a75e2017-05-30 20:06:08 +0530929 --dst;
930 while ((*dst++ = *src++));
Mayank Grover5384ed82018-05-09 12:09:24 +0530931 }
932
933 src = sys_path_cmdline;
934 --dst;
935 while ((*dst++ = *src++));
Mayank Grover3804be72017-06-22 11:59:23 +0530936
Monika Singh292b3e92018-03-17 22:40:23 +0530937#ifndef VERIFIED_BOOT_2
Mayank Grover5384ed82018-05-09 12:09:24 +0530938 src = syspath_buf;
939 --dst;
940 while ((*dst++ = *src++));
Monika Singh292b3e92018-03-17 22:40:23 +0530941#endif
Mayank Grover351a75e2017-05-30 20:06:08 +0530942 }
943
Mayank Grover466d6562018-05-10 14:52:20 +0530944#if HIBERNATION_SUPPORT
945 if (swap_ptn_index != INVALID_PTN)
946 {
947 src = resume_buf;
948 --dst;
949 while ((*dst++ = *src++));
950 }
951#endif
952
Channagoud Kadabi85c7ec32016-01-28 23:09:21 -0800953#if TARGET_CMDLINE_SUPPORT
954 if (target_cmdline_buf && target_cmd_line_len)
955 {
956 if (have_cmdline) --dst;
957 src = target_cmdline_buf;
958 while((*dst++ = *src++));
959 free(target_cmdline_buf);
960 }
961#endif
lijuang83ef4b22018-08-23 11:01:55 +0800962
963#if !VERITY_LE
964 if (dtbo_idx != INVALID_PTN) {
965 src = dtbo_idx_str;
966 --dst;
967 while ((*dst++ = *src++));
968 }
969#endif
Neeti Desaie245d492012-06-01 12:52:13 -0700970 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700971
972
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700973 if (boot_dev_buf)
974 free(boot_dev_buf);
975
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -0800976 if (cmdline_final)
977 dprintf(INFO, "cmdline: %s\n", cmdline_final);
978 else
979 dprintf(INFO, "cmdline is NULL\n");
Neeti Desaie245d492012-06-01 12:52:13 -0700980 return cmdline_final;
981}
982
983unsigned *atag_core(unsigned *ptr)
984{
985 /* CORE */
986 *ptr++ = 2;
987 *ptr++ = 0x54410001;
988
989 return ptr;
990
991}
992
993unsigned *atag_ramdisk(unsigned *ptr, void *ramdisk,
994 unsigned ramdisk_size)
995{
996 if (ramdisk_size) {
997 *ptr++ = 4;
998 *ptr++ = 0x54420005;
999 *ptr++ = (unsigned)ramdisk;
1000 *ptr++ = ramdisk_size;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001001 }
1002
Neeti Desaie245d492012-06-01 12:52:13 -07001003 return ptr;
1004}
1005
1006unsigned *atag_ptable(unsigned **ptr_addr)
1007{
1008 int i;
1009 struct ptable *ptable;
1010
1011 if ((ptable = flash_get_ptable()) && (ptable->count != 0)) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001012 *(*ptr_addr)++ = 2 + (ptable->count * (sizeof(struct atag_ptbl_entry) /
1013 sizeof(unsigned)));
Neeti Desaie245d492012-06-01 12:52:13 -07001014 *(*ptr_addr)++ = 0x4d534d70;
1015 for (i = 0; i < ptable->count; ++i)
1016 ptentry_to_tag(ptr_addr, ptable_get(ptable, i));
1017 }
1018
1019 return (*ptr_addr);
1020}
1021
1022unsigned *atag_cmdline(unsigned *ptr, const char *cmdline)
1023{
1024 int cmdline_length = 0;
1025 int n;
Neeti Desaie245d492012-06-01 12:52:13 -07001026 char *dest;
1027
Amol Jadi10c7d1c2013-01-25 13:24:29 -08001028 cmdline_length = strlen((const char*)cmdline);
Neeti Desaie245d492012-06-01 12:52:13 -07001029 n = (cmdline_length + 4) & (~3);
1030
1031 *ptr++ = (n / 4) + 2;
1032 *ptr++ = 0x54410009;
1033 dest = (char *) ptr;
Amol Jadi10c7d1c2013-01-25 13:24:29 -08001034 while ((*dest++ = *cmdline++));
Neeti Desaie245d492012-06-01 12:52:13 -07001035 ptr += (n / 4);
1036
1037 return ptr;
1038}
1039
1040unsigned *atag_end(unsigned *ptr)
1041{
Brian Swetland9c4c0752009-01-25 16:23:50 -08001042 /* END */
1043 *ptr++ = 0;
1044 *ptr++ = 0;
1045
Neeti Desaie245d492012-06-01 12:52:13 -07001046 return ptr;
1047}
1048
1049void generate_atags(unsigned *ptr, const char *cmdline,
1050 void *ramdisk, unsigned ramdisk_size)
1051{
vijay kumar21a37452015-12-29 16:23:21 +05301052 unsigned *orig_ptr = ptr;
Neeti Desaie245d492012-06-01 12:52:13 -07001053 ptr = atag_core(ptr);
1054 ptr = atag_ramdisk(ptr, ramdisk, ramdisk_size);
1055 ptr = target_atag_mem(ptr);
1056
1057 /* Skip NAND partition ATAGS for eMMC boot */
1058 if (!target_is_emmc_boot()){
1059 ptr = atag_ptable(&ptr);
1060 }
1061
vijay kumar21a37452015-12-29 16:23:21 +05301062 /*
1063 * Atags size filled till + cmdline size + 1 unsigned for 4-byte boundary + 4 unsigned
1064 * for atag identifier in atag_cmdline and atag_end should be with in MAX_TAGS_SIZE bytes
1065 */
Mayank Groverbc8a2ef2018-02-23 18:03:36 +05301066 if (!cmdline)
1067 return;
1068
vijay kumar21a37452015-12-29 16:23:21 +05301069 if (((ptr - orig_ptr) + strlen(cmdline) + 5 * sizeof(unsigned)) < MAX_TAGS_SIZE) {
1070 ptr = atag_cmdline(ptr, cmdline);
1071 ptr = atag_end(ptr);
1072 }
1073 else {
1074 dprintf(CRITICAL,"Crossing ATAGs Max size allowed\n");
1075 ASSERT(0);
1076 }
Neeti Desaie245d492012-06-01 12:52:13 -07001077}
1078
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001079typedef void entry_func_ptr(unsigned, unsigned, unsigned*);
Neeti Desaie245d492012-06-01 12:52:13 -07001080void boot_linux(void *kernel, unsigned *tags,
1081 const char *cmdline, unsigned machtype,
1082 void *ramdisk, unsigned ramdisk_size)
1083{
Amol Jadi10c7d1c2013-01-25 13:24:29 -08001084 unsigned char *final_cmdline;
Amol Jadib6be5c12012-11-14 13:39:51 -08001085#if DEVICE_TREE
Neeti Desai17379b82012-06-04 18:42:53 -07001086 int ret = 0;
Amol Jadib6be5c12012-11-14 13:39:51 -08001087#endif
1088
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001089 void (*entry)(unsigned, unsigned, unsigned*) = (entry_func_ptr*)(PA((addr_t)kernel));
Deepa Dinamani25a9f762012-11-30 15:57:15 -08001090 uint32_t tags_phys = PA((addr_t)tags);
vijay kumar1a50a642015-11-16 12:41:15 +05301091 struct kernel64_hdr *kptr = ((struct kernel64_hdr*)(PA((addr_t)kernel)));
Deepa Dinamani25a9f762012-11-30 15:57:15 -08001092
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301093 ramdisk = (void *)PA((addr_t)ramdisk);
Neeti Desaie245d492012-06-01 12:52:13 -07001094
Amol Jadi10c7d1c2013-01-25 13:24:29 -08001095 final_cmdline = update_cmdline((const char*)cmdline);
1096
Neeti Desai17379b82012-06-04 18:42:53 -07001097#if DEVICE_TREE
Amol Jadib6be5c12012-11-14 13:39:51 -08001098 dprintf(INFO, "Updating device tree: start\n");
1099
Neeti Desai17379b82012-06-04 18:42:53 -07001100 /* Update the Device Tree */
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301101 ret = update_device_tree((void *)tags,(const char *)final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -07001102 if(ret)
1103 {
1104 dprintf(CRITICAL, "ERROR: Updating Device Tree Failed \n");
1105 ASSERT(0);
1106 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001107 dprintf(INFO, "Updating device tree: done\n");
Neeti Desai17379b82012-06-04 18:42:53 -07001108#else
Neeti Desaie245d492012-06-01 12:52:13 -07001109 /* Generating the Atags */
Amol Jadi10c7d1c2013-01-25 13:24:29 -08001110 generate_atags(tags, final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -07001111#endif
Neeti Desaie245d492012-06-01 12:52:13 -07001112
Monika Singhb0db4b82018-09-26 12:18:02 +05301113#if VERIFIED_BOOT || VERIFIED_BOOT_2
1114 if (VB_M <= target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +05301115 {
1116 if (device.verity_mode == 0) {
lijuangbdd9bb42016-03-01 18:22:17 +08001117#if FBCON_DISPLAY_MSG
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07001118#if ENABLE_VB_ATTEST
Mayank Grover889be1b2017-09-12 20:12:23 +05301119 display_bootverify_menu(DISPLAY_MENU_EIO);
1120 wait_for_users_action();
1121 if(!pwr_key_is_pressed)
1122 shutdown_device();
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07001123#else
Mayank Grover889be1b2017-09-12 20:12:23 +05301124 display_bootverify_menu(DISPLAY_MENU_LOGGING);
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07001125#endif
Mayank Grover889be1b2017-09-12 20:12:23 +05301126 wait_for_users_action();
lijuangbdd9bb42016-03-01 18:22:17 +08001127#else
Mayank Grover889be1b2017-09-12 20:12:23 +05301128 dprintf(CRITICAL,
1129 "The dm-verity is not started in enforcing mode.\nWait for 5 seconds before proceeding\n");
1130 mdelay(5000);
lijuangbdd9bb42016-03-01 18:22:17 +08001131#endif
Mayank Grover889be1b2017-09-12 20:12:23 +05301132 }
lijuangbdd9bb42016-03-01 18:22:17 +08001133 }
lijuangbdd9bb42016-03-01 18:22:17 +08001134#endif
1135
1136#if VERIFIED_BOOT
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001137 /* Write protect the device info */
Channagoud Kadabi3bd9d1e2015-05-05 16:18:20 -07001138 if (!boot_into_recovery && target_build_variant_user() && devinfo_present && mmc_write_protect("devinfo", 1))
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001139 {
1140 dprintf(INFO, "Failed to write protect dev info\n");
1141 ASSERT(0);
1142 }
1143#endif
1144
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08001145 /* Turn off splash screen if enabled */
1146#if DISPLAY_SPLASH_SCREEN
1147 target_display_shutdown();
1148#endif
1149
Veera Sundaram Sankaran67ea0932015-09-25 10:09:30 -07001150 /* Perform target specific cleanup */
1151 target_uninit();
Monika Singh292b3e92018-03-17 22:40:23 +05301152 free_verified_boot_resource(&info);
1153 if (final_cmdline)
1154 free(final_cmdline);
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08001155
Deepa Dinamani33734bc2013-03-06 12:16:06 -08001156 dprintf(INFO, "booting linux @ %p, ramdisk @ %p (%d), tags/device tree @ %p\n",
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301157 entry, ramdisk, ramdisk_size, (void *)tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001158
1159 enter_critical_section();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001160
Amol Jadi4421e652011-06-16 15:00:48 -07001161 /* do any platform specific cleanup before kernel entry */
1162 platform_uninit();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001163
Brian Swetland9c4c0752009-01-25 16:23:50 -08001164 arch_disable_cache(UCACHE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001165
Amol Jadi504f9fe2012-08-16 13:56:48 -07001166#if ARM_WITH_MMU
Brian Swetland9c4c0752009-01-25 16:23:50 -08001167 arch_disable_mmu();
Amol Jadi504f9fe2012-08-16 13:56:48 -07001168#endif
Amol Jadi492d5a52013-03-15 16:12:34 -07001169 bs_set_timestamp(BS_KERNEL_ENTRY);
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001170
1171 if (IS_ARM64(kptr))
1172 /* Jump to a 64bit kernel */
1173 scm_elexec_call((paddr_t)kernel, tags_phys);
1174 else
1175 /* Jump to a 32bit kernel */
1176 entry(0, machtype, (unsigned*)tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001177}
1178
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001179/* Function to check if the memory address range falls within the aboot
1180 * boundaries.
1181 * start: Start of the memory region
1182 * size: Size of the memory region
1183 */
Vijay Kumar Pendoti9c002ad2016-03-09 13:52:45 +05301184int check_aboot_addr_range_overlap(uintptr_t start, uint32_t size)
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001185{
1186 /* Check for boundary conditions. */
Sundarajan Srinivasance2a0ea2013-12-16 17:02:56 -08001187 if ((UINT_MAX - start) < size)
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001188 return -1;
1189
1190 /* Check for memory overlap. */
1191 if ((start < MEMBASE) && ((start + size) <= MEMBASE))
1192 return 0;
Channagoud Kadabi94143912013-10-15 12:53:52 -07001193 else if (start >= (MEMBASE + MEMSIZE))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001194 return 0;
1195 else
1196 return -1;
1197}
1198
Mayank Grovere559cec2017-10-17 15:12:03 +05301199/* Function to check if the memory address range falls beyond ddr region.
1200 * start: Start of the memory region
1201 * size: Size of the memory region
1202 */
1203int check_ddr_addr_range_bound(uintptr_t start, uint32_t size)
1204{
1205 uintptr_t ddr_pa_start_addr = PA(get_ddr_start());
1206 uint64_t ddr_size = smem_get_ddr_size();
1207 uint64_t ddr_pa_end_addr = ddr_pa_start_addr + ddr_size;
1208 uintptr_t pa_start_addr = PA(start);
1209
1210 /* Check for boundary conditions. */
1211 if ((UINT_MAX - start) < size)
1212 return -1;
1213
1214 /* Check if memory range is beyond the ddr range. */
1215 if (pa_start_addr < ddr_pa_start_addr ||
1216 pa_start_addr >= (ddr_pa_end_addr) ||
1217 (pa_start_addr + size) > ddr_pa_end_addr)
1218 return -1;
1219 else
1220 return 0;
1221}
1222
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001223BUF_DMA_ALIGN(buf, BOOT_IMG_MAX_PAGE_SIZE); //Equal to max-supported pagesize
Dima Zavin214cc642009-01-26 11:16:21 -08001224
Mayank Groverdd080e82018-09-04 20:12:13 +05301225int getimage(void **image_buffer, uint32_t *imgsize,
1226 const char *imgname)
Monika Singh292b3e92018-03-17 22:40:23 +05301227{
Mayank Groverdd080e82018-09-04 20:12:13 +05301228 uint32_t loadedindex;
1229 if (image_buffer == NULL || imgsize == NULL ||
Monika Singh292b3e92018-03-17 22:40:23 +05301230 imgname == NULL) {
1231 dprintf(CRITICAL, "getimage: invalid parameters\n");
1232 return -1;
1233 }
Mayank Groverdd080e82018-09-04 20:12:13 +05301234 for (loadedindex = 0; loadedindex < info.num_loaded_images; loadedindex++) {
1235 if (!strncmp(info.images[loadedindex].name, imgname,
Monika Singh292b3e92018-03-17 22:40:23 +05301236 strlen(imgname))) {
Mayank Groverdd080e82018-09-04 20:12:13 +05301237 *image_buffer = info.images[loadedindex].image_buffer;
1238 *imgsize = info.images[loadedindex].imgsize;
Mayank Grover027a9412018-09-04 15:12:05 +05301239 dprintf(SPEW, "getimage(): Loaded image [%s|%d]\n",
1240 info.images[loadedindex].name,
1241 info.images[loadedindex].imgsize);
Monika Singh292b3e92018-03-17 22:40:23 +05301242 return 0;
1243 }
1244 }
1245 return -1;
1246}
1247
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001248static void verify_signed_bootimg(uint32_t bootimg_addr, uint32_t bootimg_size)
1249{
1250 int ret;
Channagoud Kadabia8c623f2015-01-13 14:48:48 -08001251
1252#if !VERIFIED_BOOT
Sundarajan Srinivasance54d6e2013-11-11 12:45:00 -08001253#if IMAGE_VERIF_ALGO_SHA1
1254 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA1;
1255#else
1256 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA256;
1257#endif
Channagoud Kadabia8c623f2015-01-13 14:48:48 -08001258#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001259
1260 /* Assume device is rooted at this time. */
1261 device.is_tampered = 1;
1262
1263 dprintf(INFO, "Authenticating boot image (%d): start\n", bootimg_size);
1264
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001265#if VERIFIED_BOOT
Monika Singh7d2fc272018-03-16 17:16:01 +05301266 uint32_t bootstate;
1267 if(boot_into_recovery &&
Mayank Grover68fbf0d2017-10-24 14:13:39 +05301268 (!partition_multislot_is_supported()))
Monika Singh7d2fc272018-03-16 17:16:01 +05301269 {
1270 ret = boot_verify_image((unsigned char *)bootimg_addr,
1271 bootimg_size, "/recovery", &bootstate);
1272 }
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001273 else
Monika Singh7d2fc272018-03-16 17:16:01 +05301274 {
1275 ret = boot_verify_image((unsigned char *)bootimg_addr,
1276 bootimg_size, "/boot", &bootstate);
1277 }
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001278 boot_verify_print_state();
1279#else
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001280 ret = image_verify((unsigned char *)bootimg_addr,
1281 (unsigned char *)(bootimg_addr + bootimg_size),
1282 bootimg_size,
Sundarajan Srinivasance54d6e2013-11-11 12:45:00 -08001283 auth_algo);
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001284#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001285 dprintf(INFO, "Authenticating boot image: done return value = %d\n", ret);
1286
1287 if (ret)
1288 {
1289 /* Authorized kernel */
1290 device.is_tampered = 0;
Sundarajan Srinivasan3fb21f12013-09-16 18:36:15 -07001291 auth_kernel_img = 1;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001292 }
1293
Amit Blay4aa292f2015-04-28 21:55:59 +03001294#ifdef MDTP_SUPPORT
1295 {
1296 /* Verify MDTP lock.
1297 * For boot & recovery partitions, use aboot's verification result.
1298 */
1299 mdtp_ext_partition_verification_t ext_partition;
1300 ext_partition.partition = boot_into_recovery ? MDTP_PARTITION_RECOVERY : MDTP_PARTITION_BOOT;
1301 ext_partition.integrity_state = device.is_tampered ? MDTP_PARTITION_STATE_INVALID : MDTP_PARTITION_STATE_VALID;
1302 ext_partition.page_size = 0; /* Not needed since already validated */
1303 ext_partition.image_addr = 0; /* Not needed since already validated */
1304 ext_partition.image_size = 0; /* Not needed since already validated */
1305 ext_partition.sig_avail = FALSE; /* Not needed since already validated */
1306 mdtp_fwlock_verify_lock(&ext_partition);
1307 }
1308#endif /* MDTP_SUPPORT */
1309
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001310#if USE_PCOM_SECBOOT
1311 set_tamper_flag(device.is_tampered);
1312#endif
1313
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001314#if VERIFIED_BOOT
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001315 switch(boot_verify_get_state())
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001316 {
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001317 case RED:
lijuanga40d6302015-07-20 20:10:13 +08001318#if FBCON_DISPLAY_MSG
lijuang9a7d3b92015-11-30 14:41:24 +08001319 display_bootverify_menu(DISPLAY_MENU_RED);
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07001320#if ENABLE_VB_ATTEST
1321 mdelay(DELAY_WAIT);
1322 shutdown_device();
1323#else
lijuanga40d6302015-07-20 20:10:13 +08001324 wait_for_users_action();
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07001325#endif
lijuanga40d6302015-07-20 20:10:13 +08001326#else
1327 dprintf(CRITICAL,
1328 "Your device has failed verification and may not work properly.\nWait for 5 seconds before proceeding\n");
1329 mdelay(5000);
1330#endif
1331
1332 break;
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001333 case YELLOW:
lijuanga40d6302015-07-20 20:10:13 +08001334#if FBCON_DISPLAY_MSG
lijuang9a7d3b92015-11-30 14:41:24 +08001335 display_bootverify_menu(DISPLAY_MENU_YELLOW);
lijuanga40d6302015-07-20 20:10:13 +08001336 wait_for_users_action();
1337#else
1338 dprintf(CRITICAL,
1339 "Your device has loaded a different operating system.\nWait for 5 seconds before proceeding\n");
1340 mdelay(5000);
1341#endif
1342 break;
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001343 default:
lijuanga40d6302015-07-20 20:10:13 +08001344 break;
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001345 }
1346#endif
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001347#if !VERIFIED_BOOT
Unnati Gandhi1be04752015-03-27 19:41:53 +05301348 if(device.is_tampered)
1349 {
1350 write_device_info_mmc(&device);
1351 #ifdef TZ_TAMPER_FUSE
Monika Singh98257462018-06-06 11:28:49 +05301352 set_tamper_fuse_cmd(HLOS_IMG_TAMPER_FUSE);
Unnati Gandhi1be04752015-03-27 19:41:53 +05301353 #endif
1354 #ifdef ASSERT_ON_TAMPER
1355 dprintf(CRITICAL, "Device is tampered. Asserting..\n");
1356 ASSERT(0);
1357 #endif
1358 }
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001359#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001360}
1361
Maunik Shah0f3c8ac2014-03-02 17:47:58 +05301362static bool check_format_bit()
1363{
1364 bool ret = false;
1365 int index;
1366 uint64_t offset;
1367 struct boot_selection_info *in = NULL;
1368 char *buf = NULL;
1369
1370 index = partition_get_index("bootselect");
1371 if (index == INVALID_PTN)
1372 {
1373 dprintf(INFO, "Unable to locate /bootselect partition\n");
1374 return ret;
1375 }
1376 offset = partition_get_offset(index);
1377 if(!offset)
1378 {
1379 dprintf(INFO, "partition /bootselect doesn't exist\n");
1380 return ret;
1381 }
1382 buf = (char *) memalign(CACHE_LINE, ROUNDUP(page_size, CACHE_LINE));
Mayank Grover48860402016-11-29 12:34:53 +05301383 mmc_set_lun(partition_get_lun(index));
Maunik Shah0f3c8ac2014-03-02 17:47:58 +05301384 ASSERT(buf);
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301385 if (mmc_read(offset, (uint32_t *)buf, page_size))
Maunik Shah0f3c8ac2014-03-02 17:47:58 +05301386 {
1387 dprintf(INFO, "mmc read failure /bootselect %d\n", page_size);
1388 free(buf);
1389 return ret;
1390 }
1391 in = (struct boot_selection_info *) buf;
1392 if ((in->signature == BOOTSELECT_SIGNATURE) &&
1393 (in->version == BOOTSELECT_VERSION)) {
1394 if ((in->state_info & BOOTSELECT_FORMAT) &&
1395 !(in->state_info & BOOTSELECT_FACTORY))
1396 ret = true;
1397 } else {
1398 dprintf(CRITICAL, "Signature: 0x%08x or version: 0x%08x mismatched of /bootselect\n",
1399 in->signature, in->version);
1400 ASSERT(0);
1401 }
1402 free(buf);
1403 return ret;
1404}
1405
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001406void boot_verifier_init()
1407{
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001408 uint32_t boot_state;
1409 /* Check if device unlock */
1410 if(device.is_unlocked)
1411 {
1412 boot_verify_send_event(DEV_UNLOCK);
1413 boot_verify_print_state();
1414 dprintf(CRITICAL, "Device is unlocked! Skipping verification...\n");
1415 return;
1416 }
1417 else
1418 {
1419 boot_verify_send_event(BOOT_INIT);
1420 }
1421
1422 /* Initialize keystore */
1423 boot_state = boot_verify_keystore_init();
1424 if(boot_state == YELLOW)
1425 {
1426 boot_verify_print_state();
1427 dprintf(CRITICAL, "Keystore verification failed! Continuing anyways...\n");
1428 }
1429}
1430
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301431/* Function to return recovery appended dtbo buffer info */
1432void get_recovery_dtbo_info(uint32_t *dtbo_size, void **dtbo_buf)
1433{
1434 *dtbo_size = recovery_dtbo_size;
1435 *dtbo_buf = recovery_dtbo_buf;
1436 return;
1437}
1438
Shashank Mittal23b8f422010-04-16 19:27:21 -07001439int boot_linux_from_mmc(void)
1440{
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301441 boot_img_hdr *hdr = (void*) buf;
1442 boot_img_hdr *uhdr;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001443 unsigned offset = 0;
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07001444 int rcode;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001445 unsigned long long ptn = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001446 int index = INVALID_PTN;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001447
Shashank Mittalcd98d472011-08-02 14:29:24 -07001448 unsigned char *image_addr = 0;
1449 unsigned kernel_actual;
1450 unsigned ramdisk_actual;
1451 unsigned imagesize_actual;
Neeti Desai465491e2012-07-31 12:53:35 -07001452 unsigned second_actual = 0;
Neeti Desai465491e2012-07-31 12:53:35 -07001453
Matthew Qin271927e2015-03-31 22:07:07 -04001454 unsigned int dtb_size = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08001455 unsigned int out_len = 0;
1456 unsigned int out_avai_len = 0;
1457 unsigned char *out_addr = NULL;
1458 uint32_t dtb_offset = 0;
1459 unsigned char *kernel_start_addr = NULL;
1460 unsigned int kernel_size = 0;
Ameya Thakur10a33452016-06-13 14:24:26 -07001461 unsigned int patched_kernel_hdr_size = 0;
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301462 uint64_t image_size = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08001463 int rc;
Monika Singh292b3e92018-03-17 22:40:23 +05301464#if VERIFIED_BOOT_2
1465 int status;
Mayank Grover027a9412018-09-04 15:12:05 +05301466 void *dtbo_image_buf = NULL;
1467 uint32_t dtbo_image_sz = 0;
Mayank Grover9df84c02018-08-30 15:46:35 +05301468 void *vbmeta_image_buf = NULL;
1469 uint32_t vbmeta_image_sz = 0;
Monika Singh292b3e92018-03-17 22:40:23 +05301470#endif
Mayank Groverc93cad82017-10-03 12:23:45 +05301471 char *ptn_name = NULL;
Neeti Desai465491e2012-07-31 12:53:35 -07001472#if DEVICE_TREE
1473 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -07001474 struct dt_entry dt_entry;
Neeti Desai465491e2012-07-31 12:53:35 -07001475 unsigned dt_table_offset;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001476 uint32_t dt_actual;
Deepa Dinamani19648b42013-09-05 17:05:55 -07001477 uint32_t dt_hdr_size;
Matthew Qin271927e2015-03-31 22:07:07 -04001478 unsigned char *best_match_dt_addr = NULL;
Neeti Desai465491e2012-07-31 12:53:35 -07001479#endif
Matthew Qin49e51fa2015-02-09 10:40:45 +08001480 struct kernel64_hdr *kptr = NULL;
Mayank Grover351a75e2017-05-30 20:06:08 +05301481 int current_active_slot = INVALID;
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001482
Maunik Shah0f3c8ac2014-03-02 17:47:58 +05301483 if (check_format_bit())
1484 boot_into_recovery = 1;
1485
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07001486 if (!boot_into_recovery) {
1487 memset(ffbm_mode_string, '\0', sizeof(ffbm_mode_string));
1488 rcode = get_ffbm(ffbm_mode_string, sizeof(ffbm_mode_string));
1489 if (rcode <= 0) {
1490 boot_into_ffbm = false;
1491 if (rcode < 0)
1492 dprintf(CRITICAL,"failed to get ffbm cookie");
1493 } else
1494 boot_into_ffbm = true;
1495 } else
1496 boot_into_ffbm = false;
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301497 uhdr = (boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001498 if (!memcmp(uhdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1499 dprintf(INFO, "Unified boot method!\n");
1500 hdr = uhdr;
1501 goto unified_boot;
1502 }
Mayank Groverc93cad82017-10-03 12:23:45 +05301503
1504 /* For a/b recovery image code is on boot partition.
1505 If we support multislot, always use boot partition. */
1506 if (boot_into_recovery &&
1507 (!partition_multislot_is_supported()))
1508 ptn_name = "recovery";
1509 else
1510 ptn_name = "boot";
1511
1512 index = partition_get_index(ptn_name);
1513 ptn = partition_get_offset(index);
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301514 image_size = partition_get_size(index);
1515 if(ptn == 0 || image_size == 0) {
Mayank Groverc93cad82017-10-03 12:23:45 +05301516 dprintf(CRITICAL, "ERROR: No %s partition found\n", ptn_name);
1517 return -1;
Kinson Chikf1a43512011-07-14 11:28:39 -07001518 }
Mayank Groverc93cad82017-10-03 12:23:45 +05301519
Channagoud Kadabief0547c2015-02-10 12:57:38 -08001520 /* Set Lun for boot & recovery partitions */
1521 mmc_set_lun(partition_get_lun(index));
Shashank Mittal23b8f422010-04-16 19:27:21 -07001522
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301523 if (mmc_read(ptn + offset, (uint32_t *) buf, page_size)) {
Shashank Mittal23b8f422010-04-16 19:27:21 -07001524 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
1525 return -1;
1526 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001527
1528 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001529 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Mayank Grover351a75e2017-05-30 20:06:08 +05301530 return ERR_INVALID_BOOT_MAGIC;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001531 }
1532
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07001533 if (hdr->page_size && (hdr->page_size != page_size)) {
vijay kumar2e21b3a2014-06-26 17:40:15 +05301534
1535 if (hdr->page_size > BOOT_IMG_MAX_PAGE_SIZE) {
1536 dprintf(CRITICAL, "ERROR: Invalid page size\n");
1537 return -1;
1538 }
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07001539 page_size = hdr->page_size;
1540 page_mask = page_size - 1;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001541 }
1542
Matthew Qin49e51fa2015-02-09 10:40:45 +08001543 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1544 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Kishor PKd8ddcad2017-07-27 13:53:57 +05301545 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001546
Matthew Qin49e51fa2015-02-09 10:40:45 +08001547 image_addr = (unsigned char *)target_get_scratch_address();
Mayank Grover6fd36792018-09-10 13:26:58 +05301548#if VERIFIED_BOOT_2
1549 /* Create hole in start of image for VB salt to copy */
1550 image_addr += SALT_BUFF_OFFSET;
1551#endif
Kishor PK9e91b592017-05-24 12:14:55 +05301552 memcpy(image_addr, (void *)buf, page_size);
1553
1554 /* ensure commandline is terminated */
1555 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
Matthew Qin49e51fa2015-02-09 10:40:45 +08001556
1557#if DEVICE_TREE
Parth Dixit4097b622016-03-15 14:42:27 +05301558#ifndef OSVERSION_IN_BOOTIMAGE
1559 dt_size = hdr->dt_size;
Mayank Grover5a502582018-09-12 11:24:49 +05301560#else
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301561 dprintf(INFO, "BootImage Header: %d\n", hdr->header_version);
Mayank Grover5a502582018-09-12 11:24:49 +05301562#endif
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301563
Parth Dixit4097b622016-03-15 14:42:27 +05301564 dt_actual = ROUND_TO_PAGE(dt_size, page_mask);
Kishor PKd8ddcad2017-07-27 13:53:57 +05301565 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 +05301566 dprintf(CRITICAL, "Integer overflow detected in bootimage header fields at %u in %s\n",__LINE__,__FILE__);
1567 return -1;
1568 }
Kishor PKd8ddcad2017-07-27 13:53:57 +05301569 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + second_actual + dt_actual);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001570#else
Kishor PKd8ddcad2017-07-27 13:53:57 +05301571 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 +05301572 dprintf(CRITICAL, "Integer overflow detected in bootimage header fields at %u in %s\n",__LINE__,__FILE__);
1573 return -1;
1574 }
Kishor PKd8ddcad2017-07-27 13:53:57 +05301575 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + second_actual);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001576#endif
1577
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301578#ifdef OSVERSION_IN_BOOTIMAGE
1579 /* If header version is ONE and booting into recovery,
1580 dtbo is appended with recovery image.
1581 Doing following:
1582 * Validating the recovery offset and size.
1583 * Extracting recovery dtbo to be used as dtbo.
1584 */
1585 if (boot_into_recovery &&
1586 hdr->header_version == BOOT_HEADER_VERSION_ONE)
1587 {
1588 struct boot_img_hdr_v1 *hdr1 =
1589 (struct boot_img_hdr_v1 *) (image_addr + sizeof(boot_img_hdr));
Mayank Grovera1a83c72018-09-24 11:23:15 +05301590 unsigned int recovery_dtbo_actual = 0;
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301591
Mayank Grovera1a83c72018-09-24 11:23:15 +05301592 recovery_dtbo_actual = ROUND_TO_PAGE(hdr1->recovery_dtbo_size, page_mask);
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301593 if ((hdr1->header_size !=
1594 sizeof(struct boot_img_hdr_v1) + sizeof(boot_img_hdr)))
1595 {
1596 dprintf(CRITICAL, "Invalid boot image header: %d\n", hdr1->header_size);
1597 return -1;
1598 }
1599
Mayank Grovera1a83c72018-09-24 11:23:15 +05301600 if (recovery_dtbo_actual > MAX_SUPPORTED_DTBO_IMG_BUF)
1601 {
1602 dprintf(CRITICAL, "Recovery Dtbo Size too big %x, Allowed size %x\n", recovery_dtbo_actual,
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301603 MAX_SUPPORTED_DTBO_IMG_BUF);
1604 return -1;
1605 }
1606
Mayank Grovera1a83c72018-09-24 11:23:15 +05301607 if (UINT_MAX < ((uint64_t)imagesize_actual + recovery_dtbo_actual))
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301608 {
1609 dprintf(CRITICAL, "Integer overflow detected in recoveryimage header fields at %u in %s\n",__LINE__,__FILE__);
1610 return -1;
1611 }
1612
Mayank Grovera1a83c72018-09-24 11:23:15 +05301613 if (UINT_MAX < (hdr1->recovery_dtbo_offset + recovery_dtbo_actual)) {
1614 dprintf(CRITICAL,
1615 "Integer overflow detected in recovery image header fields at %u in %s\n",__LINE__,__FILE__);
1616 return -1;
1617 }
1618
1619 if (hdr1->recovery_dtbo_offset + recovery_dtbo_actual > image_size)
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301620 {
1621 dprintf(CRITICAL, "Invalid recovery dtbo: Recovery Dtbo Offset=0x%llx,"
1622 " Recovery Dtbo Size=0x%x, Image Size=0x%llx\n",
1623 hdr1->recovery_dtbo_offset, recovery_dtbo_size, image_size);
1624 return -1;
1625 }
1626
1627 recovery_dtbo_buf = (void *)(hdr1->recovery_dtbo_offset + image_addr);
Mayank Grovera1a83c72018-09-24 11:23:15 +05301628 recovery_dtbo_size = recovery_dtbo_actual;
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301629 imagesize_actual += recovery_dtbo_size;
1630
1631 dprintf(SPEW, "Header version: %d\n", hdr->header_version);
1632 dprintf(SPEW, "Recovery Dtbo Size 0x%x\n", recovery_dtbo_size);
1633 dprintf(SPEW, "Recovery Dtbo Offset 0x%llx\n", hdr1->recovery_dtbo_offset);
1634
1635 }
1636#endif
1637
Saranya Chidurab1aaf232018-11-19 15:47:50 +05301638 /* Validate the boot/recovery image size is within the bounds of partition size */
1639 if (imagesize_actual > image_size) {
1640 dprintf(CRITICAL, "Image size is greater than partition size.\n");
1641 return -1;
1642 }
1643
Matthew Qin49e51fa2015-02-09 10:40:45 +08001644#if VERIFIED_BOOT
1645 boot_verifier_init();
1646#endif
1647
Mayank Grover1ceaee22019-04-01 17:54:32 +05301648#if VERIFIED_BOOT_2
1649 /* read full partition if device is unlocked */
1650 if (device.is_unlocked)
1651 imagesize_actual = image_size;
1652#endif
1653
Vijay Kumar Pendoti9c002ad2016-03-09 13:52:45 +05301654 if (check_aboot_addr_range_overlap((uintptr_t) image_addr, imagesize_actual))
Matthew Qin49e51fa2015-02-09 10:40:45 +08001655 {
1656 dprintf(CRITICAL, "Boot image buffer address overlaps with aboot addresses.\n");
1657 return -1;
1658 }
1659
Matthew Qinbb7923d2015-02-09 10:56:09 +08001660 /*
1661 * Update loading flow of bootimage to support compressed/uncompressed
1662 * bootimage on both 64bit and 32bit platform.
1663 * 1. Load bootimage from emmc partition onto DDR.
1664 * 2. Check if bootimage is gzip format. If yes, decompress compressed kernel
1665 * 3. Check kernel header and update kernel load addr for 64bit and 32bit
1666 * platform accordingly.
1667 * 4. Sanity Check on kernel_addr and ramdisk_addr and copy data.
1668 */
Mayank Grover351a75e2017-05-30 20:06:08 +05301669 if (partition_multislot_is_supported())
1670 {
1671 current_active_slot = partition_find_active_slot();
1672 dprintf(INFO, "Loading boot image (%d) active_slot(%s): start\n",
1673 imagesize_actual, SUFFIX_SLOT(current_active_slot));
1674 }
1675 else
1676 {
1677 dprintf(INFO, "Loading (%s) image (%d): start\n",
1678 (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual);
1679 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08001680 bs_set_timestamp(BS_KERNEL_LOAD_START);
1681
Gaurav Nebhwani43e2a462016-03-17 21:32:56 +05301682 if ((target_get_max_flash_size() - page_size) < imagesize_actual)
1683 {
1684 dprintf(CRITICAL, "booimage size is greater than DDR can hold\n");
1685 return -1;
1686 }
Kishor PK9e91b592017-05-24 12:14:55 +05301687 offset = page_size;
1688 /* Read image without signature and header*/
1689 if (mmc_read(ptn + offset, (void *)(image_addr + offset), imagesize_actual - page_size))
Matthew Qin49e51fa2015-02-09 10:40:45 +08001690 {
1691 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
1692 return -1;
1693 }
1694
Mayank Grover351a75e2017-05-30 20:06:08 +05301695 if (partition_multislot_is_supported())
1696 {
1697 dprintf(INFO, "Loading boot image (%d) active_slot(%s): done\n",
1698 imagesize_actual, SUFFIX_SLOT(current_active_slot));
1699 }
1700 else
1701 {
1702 dprintf(INFO, "Loading (%s) image (%d): done\n",
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001703 (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual);
1704
Mayank Grover351a75e2017-05-30 20:06:08 +05301705 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08001706 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
1707
1708 /* Authenticate Kernel */
1709 dprintf(INFO, "use_signed_kernel=%d, is_unlocked=%d, is_tampered=%d.\n",
1710 (int) target_use_signed_kernel(),
1711 device.is_unlocked,
1712 device.is_tampered);
Monika Singh292b3e92018-03-17 22:40:23 +05301713#if VERIFIED_BOOT_2
Mayank Grover1ceaee22019-04-01 17:54:32 +05301714 /* if device is unlocked skip reading signature, as full partition is read */
1715 if (!device.is_unlocked)
Monika Singh292b3e92018-03-17 22:40:23 +05301716 {
Mayank Grover1ceaee22019-04-01 17:54:32 +05301717 offset = imagesize_actual;
1718 if (check_aboot_addr_range_overlap((uintptr_t)image_addr + offset, page_size))
1719 {
1720 dprintf(CRITICAL, "Signature read buffer address overlaps with aboot addresses.\n");
1721 return -1;
1722 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08001723
Mayank Grover1ceaee22019-04-01 17:54:32 +05301724 /* Read signature */
1725 if(mmc_read(ptn + offset, (void *)(image_addr + offset), page_size))
1726 {
1727 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
1728 return -1;
1729 }
Monika Singh292b3e92018-03-17 22:40:23 +05301730 }
1731
Mayank Grover027a9412018-09-04 15:12:05 +05301732 /* load and validate dtbo partition */
1733 load_validate_dtbo_image(&dtbo_image_buf, &dtbo_image_sz);
1734
Mayank Grover9df84c02018-08-30 15:46:35 +05301735 /* load vbmeta partition */
1736 load_vbmeta_image(&vbmeta_image_buf, &vbmeta_image_sz);
1737
Monika Singh292b3e92018-03-17 22:40:23 +05301738 memset(&info, 0, sizeof(bootinfo));
Mayank Grover027a9412018-09-04 15:12:05 +05301739
1740 /* Pass loaded boot image passed */
Mayank Grover6fd36792018-09-10 13:26:58 +05301741 info.images[IMG_BOOT].image_buffer = SUB_SALT_BUFF_OFFSET(image_addr);
Mayank Grover027a9412018-09-04 15:12:05 +05301742 info.images[IMG_BOOT].imgsize = imagesize_actual;
1743 info.images[IMG_BOOT].name = ptn_name;
1744 ++info.num_loaded_images;
1745
1746 /* Pass loaded dtbo image */
1747 if (dtbo_image_buf != NULL) {
Mayank Grover6fd36792018-09-10 13:26:58 +05301748 info.images[IMG_DTBO].image_buffer =
1749 SUB_SALT_BUFF_OFFSET(dtbo_image_buf);
Mayank Grover027a9412018-09-04 15:12:05 +05301750 info.images[IMG_DTBO].imgsize = dtbo_image_sz;
1751 info.images[IMG_DTBO].name = "dtbo";
1752 ++info.num_loaded_images;
1753 }
1754
Mayank Grover9df84c02018-08-30 15:46:35 +05301755 /* Pass loaded vbmeta image */
1756 if (vbmeta_image_buf != NULL) {
1757 info.images[IMG_VBMETA].image_buffer = vbmeta_image_buf;
1758 info.images[IMG_VBMETA].imgsize = vbmeta_image_sz;
1759 info.images[IMG_VBMETA].name = "vbmeta";
1760 ++info.num_loaded_images;
1761 }
1762
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301763 info.header_version = hdr->header_version;
Monika Singh292b3e92018-03-17 22:40:23 +05301764 info.multi_slot_boot = partition_multislot_is_supported();
1765 info.bootreason_alarm = boot_reason_alarm;
1766 info.bootinto_recovery = boot_into_recovery;
1767 status = load_image_and_auth(&info);
1768 if(status)
1769 return -1;
1770
1771 vbcmdline = info.vbcmdline;
Mayank Grover9df84c02018-08-30 15:46:35 +05301772
1773 /* Free the buffer allocated to vbmeta post verification */
Saranya Chidurab4933332018-10-15 17:30:06 +05301774 if (vbmeta_image_buf != NULL) {
1775 free(vbmeta_image_buf);
1776 --info.num_loaded_images;
1777 }
Monika Singh292b3e92018-03-17 22:40:23 +05301778#else
Channagoud Kadabi736c4962015-08-21 11:56:52 -07001779 /* Change the condition a little bit to include the test framework support.
1780 * We would never reach this point if device is in fastboot mode, even if we did
1781 * that means we are in test mode, so execute kernel authentication part for the
1782 * tests */
Parth Dixitcb0c6082015-12-30 15:01:13 +05301783 if((target_use_signed_kernel() && (!device.is_unlocked)) || is_test_mode_enabled())
Matthew Qin49e51fa2015-02-09 10:40:45 +08001784 {
1785 offset = imagesize_actual;
Vijay Kumar Pendoti9c002ad2016-03-09 13:52:45 +05301786 if (check_aboot_addr_range_overlap((uintptr_t)image_addr + offset, page_size))
Matthew Qin49e51fa2015-02-09 10:40:45 +08001787 {
1788 dprintf(CRITICAL, "Signature read buffer address overlaps with aboot addresses.\n");
1789 return -1;
1790 }
1791
1792 /* Read signature */
1793 if(mmc_read(ptn + offset, (void *)(image_addr + offset), page_size))
1794 {
1795 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
1796 return -1;
1797 }
1798
1799 verify_signed_bootimg((uint32_t)image_addr, imagesize_actual);
Channagoud Kadabi736c4962015-08-21 11:56:52 -07001800 /* The purpose of our test is done here */
Parth Dixitcb0c6082015-12-30 15:01:13 +05301801 if(is_test_mode_enabled() && auth_kernel_img)
Channagoud Kadabi736c4962015-08-21 11:56:52 -07001802 return 0;
Matthew Qin49e51fa2015-02-09 10:40:45 +08001803 } else {
1804 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
1805 #ifdef TZ_SAVE_KERNEL_HASH
1806 aboot_save_boot_hash_mmc((uint32_t) image_addr, imagesize_actual);
1807 #endif /* TZ_SAVE_KERNEL_HASH */
Amit Blay4aa292f2015-04-28 21:55:59 +03001808
1809#ifdef MDTP_SUPPORT
1810 {
1811 /* Verify MDTP lock.
1812 * For boot & recovery partitions, MDTP will use boot_verifier APIs,
1813 * since verification was skipped in aboot. The signature is not part of the loaded image.
1814 */
1815 mdtp_ext_partition_verification_t ext_partition;
1816 ext_partition.partition = boot_into_recovery ? MDTP_PARTITION_RECOVERY : MDTP_PARTITION_BOOT;
1817 ext_partition.integrity_state = MDTP_PARTITION_STATE_UNSET;
1818 ext_partition.page_size = page_size;
1819 ext_partition.image_addr = (uint32)image_addr;
1820 ext_partition.image_size = imagesize_actual;
1821 ext_partition.sig_avail = FALSE;
1822 mdtp_fwlock_verify_lock(&ext_partition);
1823 }
1824#endif /* MDTP_SUPPORT */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001825 }
Monika Singh292b3e92018-03-17 22:40:23 +05301826#endif
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001827
Sridhar Parasuram00bfedb2015-05-26 14:21:27 -07001828#if VERIFIED_BOOT
taozhang93088bd2016-11-16 15:50:46 +08001829 if((boot_verify_get_state() == ORANGE) && (!boot_into_ffbm))
Reut Zysmanba8a9d52016-02-18 11:44:04 +02001830 {
1831#if FBCON_DISPLAY_MSG
1832 display_bootverify_menu(DISPLAY_MENU_ORANGE);
1833 wait_for_users_action();
1834#else
1835 dprintf(CRITICAL,
1836 "Your device has been unlocked and can't be trusted.\nWait for 5 seconds before proceeding\n");
1837 mdelay(5000);
1838#endif
1839 }
1840#endif
1841
1842#if VERIFIED_BOOT
Monika Singh292b3e92018-03-17 22:40:23 +05301843 if (VB_M == target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +05301844 {
1845 /* set boot and system versions. */
1846 set_os_version((unsigned char *)image_addr);
1847 // send root of trust
1848 if(!send_rot_command((uint32_t)device.is_unlocked))
1849 ASSERT(0);
1850 }
Parth Dixitbc9b6492015-10-18 00:41:38 +05301851#endif
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001852 /*
Matthew Qinbb7923d2015-02-09 10:56:09 +08001853 * Check if the kernel image is a gzip package. If yes, need to decompress it.
1854 * If not, continue booting.
1855 */
1856 if (is_gzip_package((unsigned char *)(image_addr + page_size), hdr->kernel_size))
1857 {
1858 out_addr = (unsigned char *)(image_addr + imagesize_actual + page_size);
1859 out_avai_len = target_get_max_flash_size() - imagesize_actual - page_size;
Mayank Grover027a9412018-09-04 15:12:05 +05301860#if VERIFIED_BOOT_2
1861 if (dtbo_image_sz)
1862 out_avai_len -= DTBO_IMG_BUF;
1863#endif
Matthew Qin0b15b322015-05-19 05:20:54 -04001864 dprintf(INFO, "decompressing kernel image: start\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001865 rc = decompress((unsigned char *)(image_addr + page_size),
1866 hdr->kernel_size, out_addr, out_avai_len,
1867 &dtb_offset, &out_len);
1868 if (rc)
1869 {
Matthew Qin0b15b322015-05-19 05:20:54 -04001870 dprintf(CRITICAL, "decompressing kernel image failed!!!\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001871 ASSERT(0);
1872 }
1873
Matthew Qin0b15b322015-05-19 05:20:54 -04001874 dprintf(INFO, "decompressing kernel image: done\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001875 kptr = (struct kernel64_hdr *)out_addr;
1876 kernel_start_addr = out_addr;
1877 kernel_size = out_len;
1878 } else {
Ameya Thakur10a33452016-06-13 14:24:26 -07001879 dprintf(INFO, "Uncpmpressed kernel in use\n");
1880 if (!strncmp((char*)(image_addr + page_size),
1881 PATCHED_KERNEL_MAGIC,
1882 sizeof(PATCHED_KERNEL_MAGIC) - 1)) {
1883 dprintf(INFO, "Patched kernel detected\n");
1884 kptr = (struct kernel64_hdr *)(image_addr + page_size +
1885 PATCHED_KERNEL_HEADER_SIZE);
1886 //The size of the kernel is stored at start of kernel image + 16
1887 //The dtb would start just after the kernel
1888 dtb_offset = *((uint32_t*)((unsigned char*)
1889 (image_addr + page_size +
1890 sizeof(PATCHED_KERNEL_MAGIC) -
1891 1)));
1892 //The actual kernel starts after the 20 byte header.
1893 kernel_start_addr = (unsigned char*)(image_addr +
1894 page_size + PATCHED_KERNEL_HEADER_SIZE);
1895 kernel_size = hdr->kernel_size;
1896 patched_kernel_hdr_size = PATCHED_KERNEL_HEADER_SIZE;
1897 } else {
1898 dprintf(INFO, "Kernel image not patched..Unable to locate dt offset\n");
1899 kptr = (struct kernel64_hdr *)(image_addr + page_size);
1900 kernel_start_addr = (unsigned char *)(image_addr + page_size);
1901 kernel_size = hdr->kernel_size;
1902 }
Matthew Qinbb7923d2015-02-09 10:56:09 +08001903 }
1904
1905 /*
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001906 * Update the kernel/ramdisk/tags address if the boot image header
1907 * has default values, these default values come from mkbootimg when
1908 * the boot image is flashed using fastboot flash:raw
1909 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001910 update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr));
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001911
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001912 /* Get virtual addresses since the hdr saves physical addresses. */
1913 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
1914 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
1915 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001916
Matthew Qinbb7923d2015-02-09 10:56:09 +08001917 kernel_size = ROUND_TO_PAGE(kernel_size, page_mask);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001918 /* Check if the addresses in the header are valid. */
Matthew Qinbb7923d2015-02-09 10:56:09 +08001919 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_size) ||
Mayank Grovere559cec2017-10-17 15:12:03 +05301920 check_ddr_addr_range_bound(hdr->kernel_addr, kernel_size) ||
1921 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual) ||
1922 check_ddr_addr_range_bound(hdr->ramdisk_addr, ramdisk_actual))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001923 {
Mayank Grovere559cec2017-10-17 15:12:03 +05301924 dprintf(CRITICAL, "kernel/ramdisk addresses are not valid.\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001925 return -1;
1926 }
1927
1928#ifndef DEVICE_TREE
Mayank Grovere559cec2017-10-17 15:12:03 +05301929 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE) ||
1930 check_ddr_addr_range_bound(hdr->tags_addr, MAX_TAGS_SIZE))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001931 {
Mayank Grovere559cec2017-10-17 15:12:03 +05301932 dprintf(CRITICAL, "Tags addresses are not valid.\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001933 return -1;
1934 }
1935#endif
1936
Matthew Qin49e51fa2015-02-09 10:40:45 +08001937 /* Move kernel, ramdisk and device tree to correct address */
Matthew Qinbb7923d2015-02-09 10:56:09 +08001938 memmove((void*) hdr->kernel_addr, kernel_start_addr, kernel_size);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001939 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001940
Matthew Qin49e51fa2015-02-09 10:40:45 +08001941 #if DEVICE_TREE
Parth Dixit4097b622016-03-15 14:42:27 +05301942 if(dt_size) {
Matthew Qin49e51fa2015-02-09 10:40:45 +08001943 dt_table_offset = ((uint32_t)image_addr + page_size + kernel_actual + ramdisk_actual + second_actual);
1944 table = (struct dt_table*) dt_table_offset;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001945
Matthew Qin49e51fa2015-02-09 10:40:45 +08001946 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
1947 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1948 return -1;
1949 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001950
P.V. Phani Kumar82916762016-03-09 09:20:19 +05301951 /* Its Error if, dt_hdr_size (table->num_entries * dt_entry size + Dev_Tree Header)
1952 goes beyound hdr->dt_size*/
Parth Dixit4097b622016-03-15 14:42:27 +05301953 if (dt_hdr_size > ROUND_TO_PAGE(dt_size,hdr->page_size)) {
P.V. Phani Kumar82916762016-03-09 09:20:19 +05301954 dprintf(CRITICAL, "ERROR: Invalid Device Tree size \n");
1955 return -1;
1956 }
1957
Matthew Qin49e51fa2015-02-09 10:40:45 +08001958 /* Find index of device tree within device tree table */
1959 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
1960 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1961 return -1;
1962 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001963
P.V. Phani Kumar3c333202016-03-09 11:54:37 +05301964 if(dt_entry.offset > (UINT_MAX - dt_entry.size)) {
1965 dprintf(CRITICAL, "ERROR: Device tree contents are Invalid\n");
1966 return -1;
1967 }
1968
1969 /* Ensure we are not overshooting dt_size with the dt_entry selected */
Parth Dixit4097b622016-03-15 14:42:27 +05301970 if ((dt_entry.offset + dt_entry.size) > dt_size) {
P.V. Phani Kumar3c333202016-03-09 11:54:37 +05301971 dprintf(CRITICAL, "ERROR: Device tree contents are Invalid\n");
1972 return -1;
1973 }
1974
Matthew Qin271927e2015-03-31 22:07:07 -04001975 if (is_gzip_package((unsigned char *)dt_table_offset + dt_entry.offset, dt_entry.size))
1976 {
1977 unsigned int compressed_size = 0;
1978 out_addr += out_len;
1979 out_avai_len -= out_len;
Matthew Qin0b15b322015-05-19 05:20:54 -04001980 dprintf(INFO, "decompressing dtb: start\n");
Matthew Qin271927e2015-03-31 22:07:07 -04001981 rc = decompress((unsigned char *)dt_table_offset + dt_entry.offset,
1982 dt_entry.size, out_addr, out_avai_len,
1983 &compressed_size, &dtb_size);
1984 if (rc)
1985 {
Matthew Qin0b15b322015-05-19 05:20:54 -04001986 dprintf(CRITICAL, "decompressing dtb failed!!!\n");
Matthew Qin271927e2015-03-31 22:07:07 -04001987 ASSERT(0);
1988 }
1989
Matthew Qin0b15b322015-05-19 05:20:54 -04001990 dprintf(INFO, "decompressing dtb: done\n");
Matthew Qin271927e2015-03-31 22:07:07 -04001991 best_match_dt_addr = out_addr;
1992 } else {
1993 best_match_dt_addr = (unsigned char *)dt_table_offset + dt_entry.offset;
1994 dtb_size = dt_entry.size;
1995 }
1996
Matthew Qin49e51fa2015-02-09 10:40:45 +08001997 /* Validate and Read device device tree in the tags_addr */
Mayank Grovere559cec2017-10-17 15:12:03 +05301998 if (check_aboot_addr_range_overlap(hdr->tags_addr, dtb_size) ||
1999 check_ddr_addr_range_bound(hdr->tags_addr, dtb_size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002000 {
Mayank Grovere559cec2017-10-17 15:12:03 +05302001 dprintf(CRITICAL, "Device tree addresses are not valid\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002002 return -1;
2003 }
Shashank Mittal162244e2011-08-08 19:01:25 -07002004
Matthew Qin271927e2015-03-31 22:07:07 -04002005 memmove((void *)hdr->tags_addr, (char *)best_match_dt_addr, dtb_size);
Matthew Qin49e51fa2015-02-09 10:40:45 +08002006 } else {
2007 /* Validate the tags_addr */
Mayank Grovere559cec2017-10-17 15:12:03 +05302008 if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual) ||
2009 check_ddr_addr_range_bound(hdr->tags_addr, kernel_actual))
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07002010 {
Mayank Grovere559cec2017-10-17 15:12:03 +05302011 dprintf(CRITICAL, "Device tree addresses are not valid.\n");
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07002012 return -1;
2013 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08002014 /*
2015 * If appended dev tree is found, update the atags with
2016 * memory address to the DTB appended location on RAM.
2017 * Else update with the atags address in the kernel header
Mayank Grover1ed3e8b2018-09-19 15:16:50 +05302018 *
2019 * Make sure everything from scratch address is read before next step!
2020 * In case of dtbo, this API is going to read dtbo on scratch.
Matthew Qin49e51fa2015-02-09 10:40:45 +08002021 */
2022 void *dtb;
Ameya Thakur10a33452016-06-13 14:24:26 -07002023 dtb = dev_tree_appended(
2024 (void*)(image_addr + page_size +
2025 patched_kernel_hdr_size),
2026 hdr->kernel_size, dtb_offset,
2027 (void *)hdr->tags_addr);
Matthew Qin49e51fa2015-02-09 10:40:45 +08002028 if (!dtb) {
2029 dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07002030 return -1;
2031 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07002032 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08002033 #endif
Shashank Mittal23b8f422010-04-16 19:27:21 -07002034
Stanimir Varbanov69ec5462013-07-18 18:17:42 +03002035 if (boot_into_recovery && !device.is_unlocked && !device.is_tampered)
2036 target_load_ssd_keystore();
2037
Shashank Mittal23b8f422010-04-16 19:27:21 -07002038unified_boot:
Shashank Mittal23b8f422010-04-16 19:27:21 -07002039
Dima Zavin77e41f32013-03-06 16:10:43 -08002040 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07002041 (const char *)hdr->cmdline, board_machtype(),
Shashank Mittal23b8f422010-04-16 19:27:21 -07002042 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
2043
2044 return 0;
2045}
2046
Dima Zavin214cc642009-01-26 11:16:21 -08002047int boot_linux_from_flash(void)
2048{
Mayank Grovere1ab96c2018-09-04 20:31:31 +05302049 boot_img_hdr *hdr = (void*) buf;
Dima Zavin214cc642009-01-26 11:16:21 -08002050 struct ptentry *ptn;
2051 struct ptable *ptable;
2052 unsigned offset = 0;
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08002053
Shashank Mittalcd98d472011-08-02 14:29:24 -07002054 unsigned char *image_addr = 0;
2055 unsigned kernel_actual;
2056 unsigned ramdisk_actual;
2057 unsigned imagesize_actual;
vijay kumar8f53e362015-11-24 13:38:11 +05302058 unsigned second_actual = 0;
Shashank Mittalcd98d472011-08-02 14:29:24 -07002059
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07002060#if DEVICE_TREE
Monika Singh292b3e92018-03-17 22:40:23 +05302061 struct dt_table *table = NULL;
Joel Kingaa335dc2013-06-03 16:11:08 -07002062 struct dt_entry dt_entry;
vijay kumar8f53e362015-11-24 13:38:11 +05302063 unsigned dt_table_offset;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08002064 uint32_t dt_actual;
Monika Singh292b3e92018-03-17 22:40:23 +05302065 uint32_t dt_hdr_size = 0;
Vivek Kumar07bd0862018-04-10 14:19:23 +05302066 uint32_t dtb_offset = 0;
vijay kumar8f53e362015-11-24 13:38:11 +05302067 unsigned int dtb_size = 0;
2068 unsigned char *best_match_dt_addr = NULL;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07002069#endif
2070
David Ng183a7422009-12-07 14:55:21 -08002071 if (target_is_emmc_boot()) {
Mayank Grovere1ab96c2018-09-04 20:31:31 +05302072 hdr = (boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
David Ng183a7422009-12-07 14:55:21 -08002073 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
2074 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
2075 return -1;
2076 }
2077 goto continue_boot;
2078 }
2079
Dima Zavin214cc642009-01-26 11:16:21 -08002080 ptable = flash_get_ptable();
2081 if (ptable == NULL) {
2082 dprintf(CRITICAL, "ERROR: Partition table not found\n");
2083 return -1;
2084 }
2085
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08002086 if(!boot_into_recovery)
2087 {
2088 ptn = ptable_find(ptable, "boot");
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07002089
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08002090 if (ptn == NULL) {
2091 dprintf(CRITICAL, "ERROR: No boot partition found\n");
2092 return -1;
2093 }
2094 }
2095 else
2096 {
2097 ptn = ptable_find(ptable, "recovery");
2098 if (ptn == NULL) {
2099 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
2100 return -1;
2101 }
Dima Zavin214cc642009-01-26 11:16:21 -08002102 }
2103
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05302104 /* Read boot.img header from flash */
Shashank Mittaldcc2e352009-11-19 19:11:16 -08002105 if (flash_read(ptn, offset, buf, page_size)) {
Dima Zavin214cc642009-01-26 11:16:21 -08002106 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
2107 return -1;
2108 }
Dima Zavin214cc642009-01-26 11:16:21 -08002109
2110 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07002111 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Dima Zavin214cc642009-01-26 11:16:21 -08002112 return -1;
2113 }
2114
Shashank Mittaldcc2e352009-11-19 19:11:16 -08002115 if (hdr->page_size != page_size) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07002116 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 -08002117 return -1;
2118 }
2119
Kishor PK9e91b592017-05-24 12:14:55 +05302120 image_addr = (unsigned char *)target_get_scratch_address();
2121 memcpy(image_addr, (void *)buf, page_size);
vijay kumar287bb542015-09-29 13:01:52 +05302122
Channagoud Kadabia22144f2013-03-20 11:49:01 -07002123 /*
2124 * Update the kernel/ramdisk/tags address if the boot image header
2125 * has default values, these default values come from mkbootimg when
2126 * the boot image is flashed using fastboot flash:raw
2127 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08002128 update_ker_tags_rdisk_addr(hdr, false);
Channagoud Kadabia22144f2013-03-20 11:49:01 -07002129
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002130 /* Get virtual addresses since the hdr saves physical addresses. */
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002131 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
2132 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
2133 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
2134
2135 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
2136 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Mayank Grover032736f2017-07-14 20:34:51 +05302137 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002138
Kishor PK9e91b592017-05-24 12:14:55 +05302139 /* ensure commandline is terminated */
2140 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
2141
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002142 /* Check if the addresses in the header are valid. */
2143 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
Mayank Grovere559cec2017-10-17 15:12:03 +05302144 check_ddr_addr_range_bound(hdr->kernel_addr, kernel_actual) ||
2145 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual) ||
2146 check_ddr_addr_range_bound(hdr->ramdisk_addr, ramdisk_actual))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002147 {
Mayank Grovere559cec2017-10-17 15:12:03 +05302148 dprintf(CRITICAL, "kernel/ramdisk addresses are not valid.\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002149 return -1;
2150 }
2151
2152#ifndef DEVICE_TREE
Kishor PKd8ddcad2017-07-27 13:53:57 +05302153 if (UINT_MAX < ((uint64_t)kernel_actual + (uint64_t)ramdisk_actual+ (uint64_t)second_actual + page_size)) {
Mayank Grover032736f2017-07-14 20:34:51 +05302154 dprintf(CRITICAL, "Integer overflow detected in bootimage header fields\n");
2155 return -1;
2156 }
Kishor PKd8ddcad2017-07-27 13:53:57 +05302157 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + second_actual);
Mayank Grover032736f2017-07-14 20:34:51 +05302158
Mayank Grovere559cec2017-10-17 15:12:03 +05302159 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE) ||
2160 check_ddr_addr_range_bound(hdr->tags_addr, MAX_TAGS_SIZE))
Vijay Kumar Pendotid3ed20e2016-09-20 00:34:46 +05302161 {
Mayank Grovere559cec2017-10-17 15:12:03 +05302162 dprintf(CRITICAL, "Tags addresses are not valid.\n");
Vijay Kumar Pendotid3ed20e2016-09-20 00:34:46 +05302163 return -1;
2164 }
2165#else
2166
2167#ifndef OSVERSION_IN_BOOTIMAGE
2168 dt_size = hdr->dt_size;
2169#endif
Mayank Grover032736f2017-07-14 20:34:51 +05302170 dt_actual = ROUND_TO_PAGE(dt_size, page_mask);
Kishor PKd8ddcad2017-07-27 13:53:57 +05302171 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 +05302172 dprintf(CRITICAL, "Integer overflow detected in bootimage header fields\n");
2173 return -1;
2174 }
2175
Kishor PKd8ddcad2017-07-27 13:53:57 +05302176 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + second_actual + dt_actual);
Mayank Grover032736f2017-07-14 20:34:51 +05302177
Mayank Grovere559cec2017-10-17 15:12:03 +05302178 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_size) ||
2179 check_ddr_addr_range_bound(hdr->tags_addr, dt_size))
Mayank Grover032736f2017-07-14 20:34:51 +05302180 {
Mayank Grovere559cec2017-10-17 15:12:03 +05302181 dprintf(CRITICAL, "Device tree addresses are not valid.\n");
Mayank Grover032736f2017-07-14 20:34:51 +05302182 return -1;
2183 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002184#endif
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002185
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05302186 /* Read full boot.img from flash */
2187 dprintf(INFO, "Loading (%s) image (%d): start\n",
2188 (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual);
2189 bs_set_timestamp(BS_KERNEL_LOAD_START);
2190
2191 if (UINT_MAX - page_size < imagesize_actual)
2192 {
2193 dprintf(CRITICAL,"Integer overflow detected in bootimage header fields %u %s\n", __LINE__,__func__);
2194 return -1;
2195 }
2196
2197 /*Check the availability of RAM before reading boot image + max signature length from flash*/
2198 if (target_get_max_flash_size() < (imagesize_actual + page_size))
2199 {
2200 dprintf(CRITICAL, "bootimage size is greater than DDR can hold\n");
2201 return -1;
2202 }
2203
2204 offset = page_size;
2205 /* Read image without signature and header */
2206 if (flash_read(ptn, offset, (void *)(image_addr + offset), imagesize_actual - page_size))
2207 {
2208 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
2209 return -1;
2210 }
2211
2212 dprintf(INFO, "Loading (%s) image (%d): done\n",
2213 (!boot_into_recovery ? "boot" : "recovery"), imagesize_actual);
2214 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
2215
Shashank Mittalcd98d472011-08-02 14:29:24 -07002216 /* Authenticate Kernel */
Deepa Dinamani23b60d42013-06-24 18:10:52 -07002217 if(target_use_signed_kernel() && (!device.is_unlocked))
Shashank Mittalcd98d472011-08-02 14:29:24 -07002218 {
Shashank Mittalcd98d472011-08-02 14:29:24 -07002219 offset = imagesize_actual;
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05302220
Shashank Mittalcd98d472011-08-02 14:29:24 -07002221 /* Read signature */
2222 if (flash_read(ptn, offset, (void *)(image_addr + offset), page_size))
2223 {
2224 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002225 return -1;
Shashank Mittalcd98d472011-08-02 14:29:24 -07002226 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07002227
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05302228 verify_signed_bootimg((uint32_t)image_addr, imagesize_actual);
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05302229 }
2230 offset = page_size;
2231 if(hdr->second_size != 0) {
2232 if (UINT_MAX - offset < second_actual)
2233 {
2234 dprintf(CRITICAL, "ERROR: Integer overflow in boot image header %s\t%d\n",__func__,__LINE__);
2235 return -1;
vijay kumar8f53e362015-11-24 13:38:11 +05302236 }
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05302237 offset += second_actual;
2238 /* Second image loading not implemented. */
2239 ASSERT(0);
2240 }
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05302241 /* Move kernel and ramdisk to correct address */
2242 memmove((void*) hdr->kernel_addr, (char*) (image_addr + page_size), hdr->kernel_size);
2243 memmove((void*) hdr->ramdisk_addr, (char*) (image_addr + page_size + kernel_actual), hdr->ramdisk_size);
2244
2245#if DEVICE_TREE
2246 if(dt_size != 0) {
2247
2248 dt_table_offset = ((uint32_t)image_addr + page_size + kernel_actual + ramdisk_actual + second_actual);
2249
2250 table = (struct dt_table*) dt_table_offset;
2251
2252 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
2253 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
2254 return -1;
2255 }
2256
2257 /* Its Error if, dt_hdr_size (table->num_entries * dt_entry size + Dev_Tree Header)
2258 goes beyound hdr->dt_size*/
2259 if (dt_hdr_size > ROUND_TO_PAGE(dt_size,hdr->page_size)) {
2260 dprintf(CRITICAL, "ERROR: Invalid Device Tree size \n");
2261 return -1;
2262 }
2263
2264 /* Find index of device tree within device tree table */
2265 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
2266 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
2267 return -1;
2268 }
2269
2270 /* Validate and Read device device tree in the "tags_add */
2271 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size) ||
2272 check_ddr_addr_range_bound(hdr->tags_addr, dt_entry.size))
2273 {
2274 dprintf(CRITICAL, "Device tree addresses are not valid.\n");
2275 return -1;
2276 }
2277
2278 if(dt_entry.offset > (UINT_MAX - dt_entry.size)) {
2279 dprintf(CRITICAL, "ERROR: Device tree contents are Invalid\n");
2280 return -1;
2281 }
2282
2283 /* Ensure we are not overshooting dt_size with the dt_entry selected */
2284 if ((dt_entry.offset + dt_entry.size) > dt_size) {
2285 dprintf(CRITICAL, "ERROR: Device tree contents are Invalid\n");
2286 return -1;
2287 }
2288
2289 best_match_dt_addr = (unsigned char *)table + dt_entry.offset;
2290 dtb_size = dt_entry.size;
2291 memmove((void *)hdr->tags_addr, (char *)best_match_dt_addr, dtb_size);
Vivek Kumar07bd0862018-04-10 14:19:23 +05302292
2293 } else {
2294 /* Validate the tags_addr */
2295 if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual) ||
2296 check_ddr_addr_range_bound(hdr->tags_addr, kernel_actual))
2297 {
2298 dprintf(CRITICAL, "Device tree addresses are not valid.\n");
2299 return -1;
2300 }
2301 /*
2302 * If appended dev tree is found, update the atags with
2303 * memory address to the DTB appended location on RAM.
2304 * Else update with the atags address in the kernel header
Mayank Grover1ed3e8b2018-09-19 15:16:50 +05302305 *
2306 * Make sure everything from scratch address is read before next step!
2307 * In case of dtbo, this API is going to read dtbo on scratch.
Vivek Kumar07bd0862018-04-10 14:19:23 +05302308 */
2309 void *dtb = NULL;
2310 dtb = dev_tree_appended((void*)(image_addr + page_size ),hdr->kernel_size, dtb_offset, (void *)hdr->tags_addr);
2311 if (!dtb) {
2312 dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
2313 return -1;
2314 }
2315 }
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05302316#endif
2317 if(target_use_signed_kernel() && (!device.is_unlocked))
2318 {
Shashank Mittal162244e2011-08-08 19:01:25 -07002319 /* Make sure everything from scratch address is read before next step!*/
Shashank Mittala0032282011-08-26 14:50:11 -07002320 if(device.is_tampered)
Shashank Mittal162244e2011-08-08 19:01:25 -07002321 {
2322 write_device_info_flash(&device);
2323 }
Channagoud Kadabi5c86fe32012-02-16 10:58:48 +05302324#if USE_PCOM_SECBOOT
2325 set_tamper_flag(device.is_tampered);
2326#endif
Shashank Mittalcd98d472011-08-02 14:29:24 -07002327 }
David Ng183a7422009-12-07 14:55:21 -08002328continue_boot:
Dima Zavin214cc642009-01-26 11:16:21 -08002329
Dima Zavin214cc642009-01-26 11:16:21 -08002330 /* TODO: create/pass atags to kernel */
2331
Ajay Dudanie28a6072011-07-01 13:59:46 -07002332 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07002333 (const char *)hdr->cmdline, board_machtype(),
Dima Zavin214cc642009-01-26 11:16:21 -08002334 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
2335
2336 return 0;
2337}
Brian Swetland9c4c0752009-01-25 16:23:50 -08002338
Shashank Mittal162244e2011-08-08 19:01:25 -07002339void write_device_info_mmc(device_info *dev)
2340{
Shashank Mittal162244e2011-08-08 19:01:25 -07002341 unsigned long long ptn = 0;
2342 unsigned long long size;
2343 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002344 uint8_t lun = 0;
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002345 uint32_t ret = 0;
Mayank Groverddd348d2018-01-23 14:07:09 +05302346 uint32_t device_info_sz = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07002347
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002348 if (devinfo_present)
2349 index = partition_get_index("devinfo");
2350 else
2351 index = partition_get_index("aboot");
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07002352
Shashank Mittal162244e2011-08-08 19:01:25 -07002353 ptn = partition_get_offset(index);
2354 if(ptn == 0)
2355 {
2356 return;
2357 }
2358
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002359 lun = partition_get_lun(index);
2360 mmc_set_lun(lun);
2361
Shashank Mittal162244e2011-08-08 19:01:25 -07002362 size = partition_get_size(index);
2363
Mayank Groverddd348d2018-01-23 14:07:09 +05302364 device_info_sz = ROUND_TO_PAGE(sizeof(struct device_info),
2365 mmc_blocksize_mask);
2366 if (device_info_sz == UINT_MAX)
2367 {
2368 dprintf(CRITICAL, "ERROR: Incorrect blocksize of card\n");
2369 return;
2370 }
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07002371
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002372 if (devinfo_present)
Mayank Groverddd348d2018-01-23 14:07:09 +05302373 ret = mmc_write(ptn, device_info_sz, (void *)info_buf);
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002374 else
Mayank Groverddd348d2018-01-23 14:07:09 +05302375 ret = mmc_write((ptn + size - device_info_sz), device_info_sz, (void *)info_buf);
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002376 if (ret)
Shashank Mittal162244e2011-08-08 19:01:25 -07002377 {
2378 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
Sridhar Parasurame1a97922015-07-27 15:31:22 -07002379 ASSERT(0);
Shashank Mittal162244e2011-08-08 19:01:25 -07002380 }
2381}
2382
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002383void read_device_info_mmc(struct device_info *info)
Shashank Mittal162244e2011-08-08 19:01:25 -07002384{
Shashank Mittal162244e2011-08-08 19:01:25 -07002385 unsigned long long ptn = 0;
2386 unsigned long long size;
2387 int index = INVALID_PTN;
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002388 uint32_t ret = 0;
Mayank Groverddd348d2018-01-23 14:07:09 +05302389 uint32_t device_info_sz = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07002390
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002391 if ((index = partition_get_index("devinfo")) < 0)
2392 {
2393 devinfo_present = false;
2394 index = partition_get_index("aboot");
2395 }
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07002396
Shashank Mittal162244e2011-08-08 19:01:25 -07002397 ptn = partition_get_offset(index);
2398 if(ptn == 0)
2399 {
2400 return;
2401 }
2402
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07002403 mmc_set_lun(partition_get_lun(index));
2404
Shashank Mittal162244e2011-08-08 19:01:25 -07002405 size = partition_get_size(index);
2406
Mayank Groverddd348d2018-01-23 14:07:09 +05302407 device_info_sz = ROUND_TO_PAGE(sizeof(struct device_info),
2408 mmc_blocksize_mask);
2409 if (device_info_sz == UINT_MAX)
2410 {
2411 dprintf(CRITICAL, "ERROR: Incorrect blocksize of card\n");
2412 return;
2413 }
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07002414
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002415 if (devinfo_present)
Mayank Groverddd348d2018-01-23 14:07:09 +05302416 ret = mmc_read(ptn, (void *)info_buf, device_info_sz);
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002417 else
Mayank Groverddd348d2018-01-23 14:07:09 +05302418 ret = mmc_read((ptn + size - device_info_sz), (void *)info_buf, device_info_sz);
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002419 if (ret)
Shashank Mittal162244e2011-08-08 19:01:25 -07002420 {
2421 dprintf(CRITICAL, "ERROR: Cannot read device info\n");
Sridhar Parasurame1a97922015-07-27 15:31:22 -07002422 ASSERT(0);
Shashank Mittal162244e2011-08-08 19:01:25 -07002423 }
Shashank Mittal162244e2011-08-08 19:01:25 -07002424}
2425
2426void write_device_info_flash(device_info *dev)
2427{
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002428 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
Shashank Mittal162244e2011-08-08 19:01:25 -07002429 struct ptentry *ptn;
2430 struct ptable *ptable;
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002431 if(info == NULL)
2432 {
2433 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
2434 ASSERT(0);
2435 }
2436 info_buf = info;
Shashank Mittal162244e2011-08-08 19:01:25 -07002437 ptable = flash_get_ptable();
2438 if (ptable == NULL)
2439 {
2440 dprintf(CRITICAL, "ERROR: Partition table not found\n");
2441 return;
2442 }
2443
2444 ptn = ptable_find(ptable, "devinfo");
2445 if (ptn == NULL)
2446 {
Smita Ghosh670c8b82015-05-07 09:30:03 -07002447 dprintf(CRITICAL, "ERROR: No devinfo partition found\n");
Shashank Mittal162244e2011-08-08 19:01:25 -07002448 return;
2449 }
2450
Mayank Groverdedbc892017-10-24 13:41:34 +05302451 memset(info, 0, BOOT_IMG_MAX_PAGE_SIZE);
Shashank Mittal162244e2011-08-08 19:01:25 -07002452 memcpy(info, dev, sizeof(device_info));
2453
2454 if (flash_write(ptn, 0, (void *)info_buf, page_size))
2455 {
2456 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
2457 return;
2458 }
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002459 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07002460}
2461
vijay kumarc65876c2015-04-24 13:29:16 +05302462static int read_allow_oem_unlock(device_info *dev)
2463{
vijay kumarc65876c2015-04-24 13:29:16 +05302464 unsigned offset;
2465 int index;
2466 unsigned long long ptn;
2467 unsigned long long ptn_size;
2468 unsigned blocksize = mmc_get_device_blocksize();
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07002469 STACKBUF_DMA_ALIGN(buf, blocksize);
vijay kumarc65876c2015-04-24 13:29:16 +05302470
vijay kumarca2e6812015-07-08 20:28:25 +05302471 index = partition_get_index(frp_ptns[0]);
vijay kumarc65876c2015-04-24 13:29:16 +05302472 if (index == INVALID_PTN)
2473 {
vijay kumarca2e6812015-07-08 20:28:25 +05302474 index = partition_get_index(frp_ptns[1]);
2475 if (index == INVALID_PTN)
2476 {
2477 dprintf(CRITICAL, "Neither '%s' nor '%s' partition found\n", frp_ptns[0],frp_ptns[1]);
2478 return -1;
2479 }
vijay kumarc65876c2015-04-24 13:29:16 +05302480 }
2481
2482 ptn = partition_get_offset(index);
2483 ptn_size = partition_get_size(index);
2484 offset = ptn_size - blocksize;
2485
Mayank Grover48860402016-11-29 12:34:53 +05302486 /* Set Lun for partition */
2487 mmc_set_lun(partition_get_lun(index));
2488
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07002489 if (mmc_read(ptn + offset, (void *)buf, blocksize))
vijay kumarc65876c2015-04-24 13:29:16 +05302490 {
2491 dprintf(CRITICAL, "Reading MMC failed\n");
2492 return -1;
2493 }
2494
2495 /*is_allow_unlock is a bool value stored at the LSB of last byte*/
2496 is_allow_unlock = buf[blocksize-1] & 0x01;
2497 return 0;
2498}
2499
2500static int write_allow_oem_unlock(bool allow_unlock)
2501{
vijay kumarc65876c2015-04-24 13:29:16 +05302502 unsigned offset;
vijay kumarc65876c2015-04-24 13:29:16 +05302503 int index;
2504 unsigned long long ptn;
2505 unsigned long long ptn_size;
2506 unsigned blocksize = mmc_get_device_blocksize();
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07002507 STACKBUF_DMA_ALIGN(buf, blocksize);
vijay kumarc65876c2015-04-24 13:29:16 +05302508
vijay kumarca2e6812015-07-08 20:28:25 +05302509 index = partition_get_index(frp_ptns[0]);
vijay kumarc65876c2015-04-24 13:29:16 +05302510 if (index == INVALID_PTN)
2511 {
vijay kumarca2e6812015-07-08 20:28:25 +05302512 index = partition_get_index(frp_ptns[1]);
2513 if (index == INVALID_PTN)
2514 {
2515 dprintf(CRITICAL, "Neither '%s' nor '%s' partition found\n", frp_ptns[0],frp_ptns[1]);
2516 return -1;
2517 }
vijay kumarc65876c2015-04-24 13:29:16 +05302518 }
2519
2520 ptn = partition_get_offset(index);
2521 ptn_size = partition_get_size(index);
2522 offset = ptn_size - blocksize;
Mayank Grover48860402016-11-29 12:34:53 +05302523 mmc_set_lun(partition_get_lun(index));
vijay kumarc65876c2015-04-24 13:29:16 +05302524
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07002525 if (mmc_read(ptn + offset, (void *)buf, blocksize))
vijay kumarc65876c2015-04-24 13:29:16 +05302526 {
2527 dprintf(CRITICAL, "Reading MMC failed\n");
2528 return -1;
2529 }
2530
2531 /*is_allow_unlock is a bool value stored at the LSB of last byte*/
2532 buf[blocksize-1] = allow_unlock;
2533 if (mmc_write(ptn + offset, blocksize, buf))
2534 {
2535 dprintf(CRITICAL, "Writing MMC failed\n");
2536 return -1;
2537 }
2538
2539 return 0;
2540}
2541
Shashank Mittal162244e2011-08-08 19:01:25 -07002542void read_device_info_flash(device_info *dev)
2543{
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002544 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
Shashank Mittal162244e2011-08-08 19:01:25 -07002545 struct ptentry *ptn;
2546 struct ptable *ptable;
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002547 if(info == NULL)
2548 {
2549 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
2550 ASSERT(0);
2551 }
2552 info_buf = info;
Shashank Mittal162244e2011-08-08 19:01:25 -07002553 ptable = flash_get_ptable();
2554 if (ptable == NULL)
2555 {
2556 dprintf(CRITICAL, "ERROR: Partition table not found\n");
2557 return;
2558 }
2559
2560 ptn = ptable_find(ptable, "devinfo");
2561 if (ptn == NULL)
2562 {
Smita Ghosh670c8b82015-05-07 09:30:03 -07002563 dprintf(CRITICAL, "ERROR: No devinfo partition found\n");
Shashank Mittal162244e2011-08-08 19:01:25 -07002564 return;
2565 }
2566
2567 if (flash_read(ptn, 0, (void *)info_buf, page_size))
2568 {
2569 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
2570 return;
2571 }
2572
2573 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
2574 {
Shashank Mittal162244e2011-08-08 19:01:25 -07002575 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
2576 info->is_unlocked = 0;
Shashank Mittala0032282011-08-26 14:50:11 -07002577 info->is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07002578 write_device_info_flash(info);
2579 }
2580 memcpy(dev, info, sizeof(device_info));
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002581 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07002582}
2583
2584void write_device_info(device_info *dev)
2585{
2586 if(target_is_emmc_boot())
2587 {
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002588 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
2589 if(info == NULL)
2590 {
2591 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
2592 ASSERT(0);
2593 }
2594 info_buf = info;
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002595 memcpy(info, dev, sizeof(struct device_info));
2596
2597#if USE_RPMB_FOR_DEVINFO
Mayank Grover912eaa62017-10-26 12:08:53 +05302598 if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05302599 is_secure_boot_enable()) {
2600 if((write_device_info_rpmb((void*) info, PAGE_SIZE)) < 0)
2601 ASSERT(0);
Sridhar Parasurame1a97922015-07-27 15:31:22 -07002602 }
Channagoud Kadabic80cb492015-04-28 16:08:28 -07002603 else
2604 write_device_info_mmc(info);
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002605#else
2606 write_device_info_mmc(info);
2607#endif
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002608 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07002609 }
2610 else
2611 {
2612 write_device_info_flash(dev);
2613 }
2614}
2615
Monika Singh94316462018-03-15 18:39:01 +05302616int read_rollback_index(uint32_t loc, uint64_t *roll_back_index)
2617{
2618 if (!devinfo_present) {
2619 dprintf(CRITICAL, "DeviceInfo not initalized \n");
2620 return -EINVAL;
2621 }
2622 if (loc >= ARRAY_SIZE(device.rollback_index)) {
2623 dprintf(CRITICAL, "%s() Loc out of range index: %d, array len: %d\n",
2624 __func__, loc, ARRAY_SIZE(device.rollback_index));
2625 ASSERT(0);
2626 }
2627
2628 *roll_back_index = device.rollback_index[loc];
2629 return 0;
2630}
2631
2632int write_rollback_index(uint32_t loc, uint64_t roll_back_index)
2633{
2634 if (!devinfo_present) {
2635 dprintf(CRITICAL, "DeviceInfo not initalized \n");
2636 return -EINVAL;
2637 }
2638 if (loc >= ARRAY_SIZE(device.rollback_index)) {
2639 dprintf(CRITICAL, "%s() Loc out of range index: %d, array len: %d\n",
2640 __func__, loc, ARRAY_SIZE(device.rollback_index));
2641 ASSERT(0);
2642 }
2643
2644 device.rollback_index[loc] = roll_back_index;
2645 write_device_info(&device);
2646 return 0;
2647}
2648
Monika Singhb0fad822018-03-15 18:50:55 +05302649int store_userkey(uint8_t *user_key, uint32_t user_key_size)
2650{
2651 if (!devinfo_present) {
2652 dprintf(CRITICAL, "DeviceInfo not initalized \n");
2653 return -EINVAL;
2654 }
2655
2656 if (user_key_size > ARRAY_SIZE(device.user_public_key)) {
2657 dprintf(CRITICAL, "StoreUserKey, UserKeySize too large!\n");
2658 return -ENODEV;
2659 }
2660
2661 memcpy(device.user_public_key, user_key, user_key_size);
2662 device.user_public_key_length = user_key_size;
2663 write_device_info(&device);
2664 return 0;
2665}
2666
2667int erase_userkey()
2668{
2669 if (!devinfo_present) {
2670 dprintf(CRITICAL, "DeviceInfo not initalized \n");
2671 return -EINVAL;
2672 }
2673 memset(device.user_public_key, 0, ARRAY_SIZE(device.user_public_key));
2674 device.user_public_key_length = 0;
2675 write_device_info(&device);
2676 return 0;
2677}
2678
2679int get_userkey(uint8_t **user_key, uint32_t *user_key_size)
2680{
2681 if (!devinfo_present) {
2682 dprintf(CRITICAL, "DeviceInfo not initalized \n");
2683 return -EINVAL;
2684 }
2685 *user_key = device.user_public_key;
2686 *user_key_size = device.user_public_key_length;
2687 return 0;
2688}
2689
Shashank Mittal162244e2011-08-08 19:01:25 -07002690void read_device_info(device_info *dev)
2691{
2692 if(target_is_emmc_boot())
2693 {
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002694 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
2695 if(info == NULL)
2696 {
2697 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
2698 ASSERT(0);
2699 }
2700 info_buf = info;
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002701
2702#if USE_RPMB_FOR_DEVINFO
Mayank Grover912eaa62017-10-26 12:08:53 +05302703 if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05302704 is_secure_boot_enable()) {
2705 if((read_device_info_rpmb((void*) info, PAGE_SIZE)) < 0)
2706 ASSERT(0);
Sridhar Parasurame1a97922015-07-27 15:31:22 -07002707 }
Channagoud Kadabic80cb492015-04-28 16:08:28 -07002708 else
2709 read_device_info_mmc(info);
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002710#else
2711 read_device_info_mmc(info);
2712#endif
2713
2714 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
2715 {
2716 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
lijuang511a2b52015-08-14 20:50:51 +08002717 if (is_secure_boot_enable()) {
Channagoud Kadabi05f78ba2015-07-06 11:58:14 -07002718 info->is_unlocked = 0;
Monika Singh292b3e92018-03-17 22:40:23 +05302719#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover912eaa62017-10-26 12:08:53 +05302720 if (VB_M <= target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +05302721 info->is_unlock_critical = 0;
Parth Dixitddbc7352015-10-18 03:13:31 +05302722#endif
lijuang511a2b52015-08-14 20:50:51 +08002723 } else {
Channagoud Kadabi2fda4092015-07-07 13:34:11 -07002724 info->is_unlocked = 1;
Monika Singh292b3e92018-03-17 22:40:23 +05302725#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover912eaa62017-10-26 12:08:53 +05302726 if (VB_M <= target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +05302727 info->is_unlock_critical = 1;
Parth Dixitddbc7352015-10-18 03:13:31 +05302728#endif
lijuang511a2b52015-08-14 20:50:51 +08002729 }
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002730 info->is_tampered = 0;
2731 info->charger_screen_enabled = 0;
Monika Singh292b3e92018-03-17 22:40:23 +05302732#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover912eaa62017-10-26 12:08:53 +05302733 if (VB_M <= target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +05302734 info->verity_mode = 1; //enforcing by default
Parth Dixitddbc7352015-10-18 03:13:31 +05302735#endif
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002736 write_device_info(info);
2737 }
2738 memcpy(dev, info, sizeof(device_info));
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002739 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07002740 }
2741 else
2742 {
2743 read_device_info_flash(dev);
2744 }
2745}
2746
2747void reset_device_info()
2748{
2749 dprintf(ALWAYS, "reset_device_info called.");
Shashank Mittala0032282011-08-26 14:50:11 -07002750 device.is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07002751 write_device_info(&device);
2752}
2753
2754void set_device_root()
2755{
2756 dprintf(ALWAYS, "set_device_root called.");
Shashank Mittala0032282011-08-26 14:50:11 -07002757 device.is_tampered = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -07002758 write_device_info(&device);
2759}
2760
lijuang4ece1e72015-08-14 21:02:36 +08002761/* set device unlock value
2762 * Must check FRP before call this function
2763 * Need to wipe data when unlock status changed
2764 * type 0: oem unlock
2765 * type 1: unlock critical
2766 * status 0: unlock as false
2767 * status 1: lock as true
2768 */
2769void set_device_unlock_value(int type, bool status)
lijuang21f12f52015-08-22 16:22:19 +08002770{
lijuang4ece1e72015-08-14 21:02:36 +08002771 if (type == UNLOCK)
2772 device.is_unlocked = status;
Monika Singh292b3e92018-03-17 22:40:23 +05302773#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover912eaa62017-10-26 12:08:53 +05302774 else if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05302775 type == UNLOCK_CRITICAL)
2776 device.is_unlock_critical = status;
Parth Dixitddbc7352015-10-18 03:13:31 +05302777#endif
lijuang4ece1e72015-08-14 21:02:36 +08002778 write_device_info(&device);
2779}
2780
2781static void set_device_unlock(int type, bool status)
2782{
2783 int is_unlocked = -1;
2784 char response[MAX_RSP_SIZE];
2785
2786 /* check device unlock status if it is as expected */
2787 if (type == UNLOCK)
2788 is_unlocked = device.is_unlocked;
Monika Singh292b3e92018-03-17 22:40:23 +05302789#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover912eaa62017-10-26 12:08:53 +05302790 if(VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05302791 type == UNLOCK_CRITICAL)
2792 {
2793 is_unlocked = device.is_unlock_critical;
2794 }
Parth Dixitddbc7352015-10-18 03:13:31 +05302795#endif
lijuang4ece1e72015-08-14 21:02:36 +08002796 if (is_unlocked == status) {
2797 snprintf(response, sizeof(response), "\tDevice already : %s", (status ? "unlocked!" : "locked!"));
2798 fastboot_info(response);
2799 fastboot_okay("");
2800 return;
2801 }
2802
2803 /* status is true, it means to unlock device */
lijuang07c5d6e2018-04-23 18:32:13 +08002804 if (status && !is_allow_unlock) {
2805 fastboot_fail("oem unlock is not allowed");
2806 return;
2807 }
lijuang21f12f52015-08-22 16:22:19 +08002808
lijuang4ece1e72015-08-14 21:02:36 +08002809#if FBCON_DISPLAY_MSG
lijuang07c5d6e2018-04-23 18:32:13 +08002810 display_unlock_menu(type, status);
2811 fastboot_okay("");
2812 return;
lijuang4ece1e72015-08-14 21:02:36 +08002813#else
lijuang07c5d6e2018-04-23 18:32:13 +08002814 if (status && type == UNLOCK) {
2815 fastboot_fail("Need wipe userdata. Do 'fastboot oem unlock-go'");
2816 return;
lijuang21f12f52015-08-22 16:22:19 +08002817 }
lijuang07c5d6e2018-04-23 18:32:13 +08002818#endif
lijuang4ece1e72015-08-14 21:02:36 +08002819
2820 set_device_unlock_value(type, status);
2821
2822 /* wipe data */
2823 struct recovery_message msg;
Kishor PK60a68212017-05-08 16:55:57 +05302824 memset(&msg, 0, sizeof(msg));
lijuang4ece1e72015-08-14 21:02:36 +08002825 snprintf(msg.recovery, sizeof(msg.recovery), "recovery\n--wipe_data");
2826 write_misc(0, &msg, sizeof(msg));
2827
2828 fastboot_okay("");
2829 reboot_device(RECOVERY_MODE);
lijuang21f12f52015-08-22 16:22:19 +08002830}
2831
lijuang511a2b52015-08-14 20:50:51 +08002832static bool critical_flash_allowed(const char * entry)
2833{
2834 uint32_t i = 0;
2835 if (entry == NULL)
2836 return false;
2837
2838 for (i = 0; i < ARRAY_SIZE(critical_flash_allowed_ptn); i++) {
2839 if(!strcmp(entry, critical_flash_allowed_ptn[i]))
2840 return true;
2841 }
2842 return false;
Matthew Qin271927e2015-03-31 22:07:07 -04002843}
2844
2845#if DEVICE_TREE
Amol Jadicb524072012-08-09 16:40:18 -07002846int copy_dtb(uint8_t *boot_image_start, unsigned int scratch_offset)
2847{
2848 uint32 dt_image_offset = 0;
Amol Jadicb524072012-08-09 16:40:18 -07002849 uint32_t n;
Monika Singh292b3e92018-03-17 22:40:23 +05302850 struct dt_table *table = NULL;
Amol Jadicb524072012-08-09 16:40:18 -07002851 struct dt_entry dt_entry;
Monika Singh292b3e92018-03-17 22:40:23 +05302852 uint32_t dt_hdr_size = 0;
Amol Jadicb524072012-08-09 16:40:18 -07002853 unsigned int compressed_size = 0;
2854 unsigned int dtb_size = 0;
2855 unsigned int out_avai_len = 0;
2856 unsigned char *out_addr = NULL;
2857 unsigned char *best_match_dt_addr = NULL;
2858 int rc;
2859
Mayank Grovere1ab96c2018-09-04 20:31:31 +05302860 boot_img_hdr *hdr = (boot_img_hdr *) (boot_image_start);
Amol Jadicb524072012-08-09 16:40:18 -07002861
Parth Dixit4097b622016-03-15 14:42:27 +05302862#ifndef OSVERSION_IN_BOOTIMAGE
2863 dt_size = hdr->dt_size;
2864#endif
2865
2866 if(dt_size != 0) {
Amol Jadicb524072012-08-09 16:40:18 -07002867 /* add kernel offset */
2868 dt_image_offset += page_size;
2869 n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
2870 dt_image_offset += n;
2871
2872 /* add ramdisk offset */
2873 n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
2874 dt_image_offset += n;
2875
2876 /* add second offset */
2877 if(hdr->second_size != 0) {
2878 n = ROUND_TO_PAGE(hdr->second_size, page_mask);
2879 dt_image_offset += n;
2880 }
2881
2882 /* offset now point to start of dt.img */
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07002883 table = (struct dt_table*)(boot_image_start + dt_image_offset);
Amol Jadicb524072012-08-09 16:40:18 -07002884
Deepa Dinamani19648b42013-09-05 17:05:55 -07002885 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Amol Jadicb524072012-08-09 16:40:18 -07002886 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
2887 return -1;
2888 }
P.V. Phani Kumar82916762016-03-09 09:20:19 +05302889
2890 /* Its Error if, dt_hdr_size (table->num_entries * dt_entry size + Dev_Tree Header)
2891 goes beyound hdr->dt_size*/
Parth Dixit4097b622016-03-15 14:42:27 +05302892 if (dt_hdr_size > ROUND_TO_PAGE(dt_size,hdr->page_size)) {
P.V. Phani Kumar82916762016-03-09 09:20:19 +05302893 dprintf(CRITICAL, "ERROR: Invalid Device Tree size \n");
2894 return -1;
2895 }
2896
Joel Kingaa335dc2013-06-03 16:11:08 -07002897 /* Find index of device tree within device tree table */
2898 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Amol Jadicb524072012-08-09 16:40:18 -07002899 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
2900 return -1;
2901 }
2902
Matthew Qin271927e2015-03-31 22:07:07 -04002903 best_match_dt_addr = (unsigned char *)boot_image_start + dt_image_offset + dt_entry.offset;
2904 if (is_gzip_package(best_match_dt_addr, dt_entry.size))
2905 {
2906 out_addr = (unsigned char *)target_get_scratch_address() + scratch_offset;
2907 out_avai_len = target_get_max_flash_size() - scratch_offset;
Matthew Qin0b15b322015-05-19 05:20:54 -04002908 dprintf(INFO, "decompressing dtb: start\n");
Matthew Qin271927e2015-03-31 22:07:07 -04002909 rc = decompress(best_match_dt_addr,
2910 dt_entry.size, out_addr, out_avai_len,
2911 &compressed_size, &dtb_size);
2912 if (rc)
2913 {
Matthew Qin0b15b322015-05-19 05:20:54 -04002914 dprintf(CRITICAL, "decompressing dtb failed!!!\n");
Matthew Qin271927e2015-03-31 22:07:07 -04002915 ASSERT(0);
2916 }
2917
Matthew Qin0b15b322015-05-19 05:20:54 -04002918 dprintf(INFO, "decompressing dtb: done\n");
Matthew Qin271927e2015-03-31 22:07:07 -04002919 best_match_dt_addr = out_addr;
2920 } else {
2921 dtb_size = dt_entry.size;
2922 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002923 /* Validate and Read device device tree in the "tags_add */
Mayank Grovere559cec2017-10-17 15:12:03 +05302924 if (check_aboot_addr_range_overlap(hdr->tags_addr, dtb_size) ||
2925 check_ddr_addr_range_bound(hdr->tags_addr, dtb_size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002926 {
Mayank Grovere559cec2017-10-17 15:12:03 +05302927 dprintf(CRITICAL, "Device tree addresses are not valid.\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002928 return -1;
2929 }
2930
Amol Jadicb524072012-08-09 16:40:18 -07002931 /* Read device device tree in the "tags_add */
Matthew Qin271927e2015-03-31 22:07:07 -04002932 memmove((void*) hdr->tags_addr, (void *)best_match_dt_addr, dtb_size);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002933 } else
2934 return -1;
Amol Jadicb524072012-08-09 16:40:18 -07002935
2936 /* Everything looks fine. Return success. */
2937 return 0;
2938}
2939#endif
2940
Brian Swetland9c4c0752009-01-25 16:23:50 -08002941void cmd_boot(const char *arg, void *data, unsigned sz)
2942{
2943 unsigned kernel_actual;
2944 unsigned ramdisk_actual;
Kishor PKd8ddcad2017-07-27 13:53:57 +05302945 unsigned second_actual;
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002946 uint32_t image_actual;
2947 uint32_t dt_actual = 0;
Mayank Grovere1ab96c2018-09-04 20:31:31 +05302948 boot_img_hdr *hdr = NULL;
Matthew Qinbb7923d2015-02-09 10:56:09 +08002949 struct kernel64_hdr *kptr = NULL;
Brian Swetland9c4c0752009-01-25 16:23:50 -08002950 char *ptr = ((char*) data);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002951 int ret = 0;
2952 uint8_t dtb_copied = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08002953 unsigned int out_len = 0;
2954 unsigned int out_avai_len = 0;
2955 unsigned char *out_addr = NULL;
2956 uint32_t dtb_offset = 0;
2957 unsigned char *kernel_start_addr = NULL;
2958 unsigned int kernel_size = 0;
Matthew Qin271927e2015-03-31 22:07:07 -04002959 unsigned int scratch_offset = 0;
Saranya Chidurab4933332018-10-15 17:30:06 +05302960#if VERIFIED_BOOT_2
2961 void *dtbo_image_buf = NULL;
2962 uint32_t dtbo_image_sz = 0;
2963 void *vbmeta_image_buf = NULL;
2964 uint32_t vbmeta_image_sz = 0;
2965#endif
Monika Singh292b3e92018-03-17 22:40:23 +05302966#if !VERIFIED_BOOT_2
2967 uint32_t sig_actual = 0;
2968 uint32_t sig_size = 0;
2969#ifdef MDTP_SUPPORT
2970 static bool is_mdtp_activated = 0;
2971#endif /* MDTP_SUPPORT */
2972#endif
Matthew Qinbb7923d2015-02-09 10:56:09 +08002973
lijuang2008ff22016-03-07 17:56:27 +08002974#if FBCON_DISPLAY_MSG
2975 /* Exit keys' detection thread firstly */
2976 exit_menu_keys_detection();
2977#endif
2978
Monika Singh292b3e92018-03-17 22:40:23 +05302979#if VERIFIED_BOOT || VERIFIED_BOOT_2
Channagoud Kadabi6d5375e2015-06-23 17:15:42 -07002980 if(target_build_variant_user() && !device.is_unlocked)
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002981 {
2982 fastboot_fail("unlock device to use this command");
lijuang2008ff22016-03-07 17:56:27 +08002983 goto boot_failed;
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002984 }
2985#endif
2986
Brian Swetland9c4c0752009-01-25 16:23:50 -08002987 if (sz < sizeof(hdr)) {
2988 fastboot_fail("invalid bootimage header");
lijuang2008ff22016-03-07 17:56:27 +08002989 goto boot_failed;
Brian Swetland9c4c0752009-01-25 16:23:50 -08002990 }
2991
Mayank Grovere1ab96c2018-09-04 20:31:31 +05302992 hdr = (boot_img_hdr *)data;
Brian Swetland9c4c0752009-01-25 16:23:50 -08002993
2994 /* ensure commandline is terminated */
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002995 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -08002996
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002997 if(target_is_emmc_boot() && hdr->page_size) {
2998 page_size = hdr->page_size;
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07002999 page_mask = page_size - 1;
3000 }
3001
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07003002 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
3003 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Kishor PKd8ddcad2017-07-27 13:53:57 +05303004 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07003005#if DEVICE_TREE
Parth Dixit4097b622016-03-15 14:42:27 +05303006#ifndef OSVERSION_IN_BOOTIMAGE
3007 dt_size = hdr->dt_size;
3008#endif
3009 dt_actual = ROUND_TO_PAGE(dt_size, page_mask);
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07003010#endif
3011
3012 image_actual = ADD_OF(page_size, kernel_actual);
3013 image_actual = ADD_OF(image_actual, ramdisk_actual);
Kishor PKd8ddcad2017-07-27 13:53:57 +05303014 image_actual = ADD_OF(image_actual, second_actual);
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07003015 image_actual = ADD_OF(image_actual, dt_actual);
3016
Kishor PK5134b332017-05-09 17:50:08 +05303017 /* Checking to prevent oob access in read_der_message_length */
3018 if (image_actual > sz) {
3019 fastboot_fail("bootimage header fields are invalid");
3020 goto boot_failed;
3021 }
Saranya Chidurab4933332018-10-15 17:30:06 +05303022
Monika Singh292b3e92018-03-17 22:40:23 +05303023#if VERIFIED_BOOT_2
Mayank Grover1ceaee22019-04-01 17:54:32 +05303024 /* Pass size of boot partition, as imgsize, to avoid
3025 read fewer bytes error */
3026 image_actual = partition_get_size(partition_get_index("boot"));
Saranya Chidurab4933332018-10-15 17:30:06 +05303027
3028 /* load and validate dtbo partition */
3029 load_validate_dtbo_image(&dtbo_image_buf, &dtbo_image_sz);
3030
3031 /* load vbmeta partition */
3032 load_vbmeta_image(&vbmeta_image_buf, &vbmeta_image_sz);
3033
Monika Singh292b3e92018-03-17 22:40:23 +05303034 memset(&info, 0, sizeof(bootinfo));
Saranya Chidurab4933332018-10-15 17:30:06 +05303035
3036 info.images[IMG_BOOT].image_buffer = SUB_SALT_BUFF_OFFSET(data);
3037 info.images[IMG_BOOT].imgsize = image_actual;
3038 info.images[IMG_BOOT].name = "boot";
3039 ++info.num_loaded_images;
3040
3041 /* Pass loaded dtbo image */
3042 if (dtbo_image_buf != NULL) {
3043 info.images[IMG_DTBO].image_buffer = SUB_SALT_BUFF_OFFSET(dtbo_image_buf);
3044 info.images[IMG_DTBO].imgsize = dtbo_image_sz;
3045 info.images[IMG_DTBO].name = "dtbo";
3046 ++info.num_loaded_images;
3047 }
3048
3049 /* Pass loaded vbmeta image */
3050 if (vbmeta_image_buf != NULL) {
3051 info.images[IMG_VBMETA].image_buffer = vbmeta_image_buf;
3052 info.images[IMG_VBMETA].imgsize = vbmeta_image_sz;
3053 info.images[IMG_VBMETA].name = "vbmeta";
3054 ++info.num_loaded_images;
3055 }
3056
Monika Singh292b3e92018-03-17 22:40:23 +05303057 info.multi_slot_boot = partition_multislot_is_supported();
3058 if (load_image_and_auth(&info))
3059 goto boot_failed;
3060 vbcmdline = info.vbcmdline;
Saranya Chidurab4933332018-10-15 17:30:06 +05303061
3062 /* Free the buffer allocated to vbmeta post verification */
3063 if (vbmeta_image_buf != NULL) {
3064 free(vbmeta_image_buf);
3065 --info.num_loaded_images;
3066 }
Monika Singh292b3e92018-03-17 22:40:23 +05303067#else
Kishor PK5134b332017-05-09 17:50:08 +05303068 sig_size = sz - image_actual;
3069
P.V. Phani Kumareecfd822016-03-09 20:09:03 +05303070 if (target_use_signed_kernel() && (!device.is_unlocked)) {
Monika Singh292b3e92018-03-17 22:40:23 +05303071 unsigned chk;
P.V. Phani Kumareecfd822016-03-09 20:09:03 +05303072 /* Calculate the signature length from boot image */
3073 sig_actual = read_der_message_length(
Kishor PK5134b332017-05-09 17:50:08 +05303074 (unsigned char*)(data + image_actual), sig_size);
Monika Singh292b3e92018-03-17 22:40:23 +05303075 chk = ADD_OF(image_actual, sig_actual);
Channagoud Kadabi3d839012014-06-26 14:26:39 -07003076
Monika Singh292b3e92018-03-17 22:40:23 +05303077 if (chk > sz) {
Kishor PK5134b332017-05-09 17:50:08 +05303078 fastboot_fail("bootimage header fields are invalid");
3079 goto boot_failed;
3080 }
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07003081 }
3082
Sridhar Parasuram361e4cd2015-09-24 11:19:19 -07003083 // Initialize boot state before trying to verify boot.img
3084#if VERIFIED_BOOT
Channagoud Kadabi699466e2015-11-03 12:37:42 -08003085 boot_verifier_init();
Mayank Groverb337e932017-01-18 20:00:40 +05303086#endif
Channagoud Kadabibf6ce7d2015-09-17 13:25:35 -07003087 /* Handle overflow if the input image size is greater than
3088 * boot image buffer can hold
3089 */
Monika Singh292b3e92018-03-17 22:40:23 +05303090 if ((target_get_max_flash_size() - page_size) < image_actual)
Channagoud Kadabibf6ce7d2015-09-17 13:25:35 -07003091 {
3092 fastboot_fail("booimage: size is greater than boot image buffer can hold");
lijuang2008ff22016-03-07 17:56:27 +08003093 goto boot_failed;
Channagoud Kadabibf6ce7d2015-09-17 13:25:35 -07003094 }
Sridhar Parasuram361e4cd2015-09-24 11:19:19 -07003095
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07003096 /* Verify the boot image
3097 * device & page_size are initialized in aboot_init
3098 */
Sridhar Parasuram7ae9fbc2015-09-17 09:56:30 -07003099 if (target_use_signed_kernel() && (!device.is_unlocked)) {
Channagoud Kadabi3d839012014-06-26 14:26:39 -07003100 /* Pass size excluding signature size, otherwise we would try to
3101 * access signature beyond its length
3102 */
Monika Singh292b3e92018-03-17 22:40:23 +05303103 verify_signed_bootimg((uint32_t)data, image_actual);
Sridhar Parasuram7ae9fbc2015-09-17 09:56:30 -07003104 }
Amit Blay4aa292f2015-04-28 21:55:59 +03003105#ifdef MDTP_SUPPORT
3106 else
3107 {
Shay Nachmani062aa3f2015-05-17 17:28:44 +03003108 /* fastboot boot is not allowed when MDTP is activated */
Amit Blay4aa292f2015-04-28 21:55:59 +03003109 mdtp_ext_partition_verification_t ext_partition;
Amit Blay8a510302015-08-17 09:20:01 +03003110
3111 if (!is_mdtp_activated) {
3112 ext_partition.partition = MDTP_PARTITION_NONE;
3113 mdtp_fwlock_verify_lock(&ext_partition);
3114 }
Amit Blay4aa292f2015-04-28 21:55:59 +03003115 }
Shay Nachmani062aa3f2015-05-17 17:28:44 +03003116
Amir Kotzer7c768c02016-04-13 09:08:05 +03003117 /* If mdtp state cannot be validate, block fastboot boot*/
3118 if(mdtp_activated(&is_mdtp_activated)){
3119 dprintf(CRITICAL, "mdtp_activated cannot validate state.\n");
3120 dprintf(CRITICAL, "Can not proceed with fastboot boot command.\n");
3121 goto boot_failed;
3122 }
Shay Nachmani062aa3f2015-05-17 17:28:44 +03003123 if(is_mdtp_activated){
3124 dprintf(CRITICAL, "fastboot boot command is not available.\n");
lijuang2008ff22016-03-07 17:56:27 +08003125 goto boot_failed;
Shay Nachmani062aa3f2015-05-17 17:28:44 +03003126 }
Amit Blay4aa292f2015-04-28 21:55:59 +03003127#endif /* MDTP_SUPPORT */
Monika Singh292b3e92018-03-17 22:40:23 +05303128#endif /* VERIFIED_BOOT_2 else */
Amit Blay4aa292f2015-04-28 21:55:59 +03003129
Sridhar Parasuram361e4cd2015-09-24 11:19:19 -07003130#if VERIFIED_BOOT
Monika Singh292b3e92018-03-17 22:40:23 +05303131 if (VB_M == target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +05303132 {
3133 /* set boot and system versions. */
3134 set_os_version((unsigned char *)data);
3135 // send root of trust
3136 if(!send_rot_command((uint32_t)device.is_unlocked))
3137 ASSERT(0);
3138 }
Parth Dixitbc9b6492015-10-18 00:41:38 +05303139#endif
Channagoud Kadabia22144f2013-03-20 11:49:01 -07003140 /*
Matthew Qinbb7923d2015-02-09 10:56:09 +08003141 * Check if the kernel image is a gzip package. If yes, need to decompress it.
3142 * If not, continue booting.
3143 */
3144 if (is_gzip_package((unsigned char *)(data + page_size), hdr->kernel_size))
3145 {
3146 out_addr = (unsigned char *)target_get_scratch_address();
3147 out_addr = (unsigned char *)(out_addr + image_actual + page_size);
3148 out_avai_len = target_get_max_flash_size() - image_actual - page_size;
Saranya Chidurab4933332018-10-15 17:30:06 +05303149#if VERIFIED_BOOT_2
3150 if (dtbo_image_sz)
3151 out_avai_len -= DTBO_IMG_BUF;
3152#endif
Matthew Qin0b15b322015-05-19 05:20:54 -04003153 dprintf(INFO, "decompressing kernel image: start\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08003154 ret = decompress((unsigned char *)(ptr + page_size),
3155 hdr->kernel_size, out_addr, out_avai_len,
3156 &dtb_offset, &out_len);
3157 if (ret)
3158 {
Matthew Qin0b15b322015-05-19 05:20:54 -04003159 dprintf(CRITICAL, "decompressing image failed!!!\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08003160 ASSERT(0);
3161 }
3162
Matthew Qin0b15b322015-05-19 05:20:54 -04003163 dprintf(INFO, "decompressing kernel image: done\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08003164 kptr = (struct kernel64_hdr *)out_addr;
3165 kernel_start_addr = out_addr;
3166 kernel_size = out_len;
3167 } else {
3168 kptr = (struct kernel64_hdr*)((char *)data + page_size);
3169 kernel_start_addr = (unsigned char *)((char *)data + page_size);
3170 kernel_size = hdr->kernel_size;
3171 }
3172
3173 /*
Channagoud Kadabia22144f2013-03-20 11:49:01 -07003174 * Update the kernel/ramdisk/tags address if the boot image header
3175 * has default values, these default values come from mkbootimg when
3176 * the boot image is flashed using fastboot flash:raw
3177 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08003178 update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr));
Dima Zavin3cadfff2013-03-21 14:30:48 -07003179
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07003180 /* Get virtual addresses since the hdr saves physical addresses. */
3181 hdr->kernel_addr = VA(hdr->kernel_addr);
3182 hdr->ramdisk_addr = VA(hdr->ramdisk_addr);
3183 hdr->tags_addr = VA(hdr->tags_addr);
Brian Swetland9c4c0752009-01-25 16:23:50 -08003184
Matthew Qinbb7923d2015-02-09 10:56:09 +08003185 kernel_size = ROUND_TO_PAGE(kernel_size, page_mask);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07003186 /* Check if the addresses in the header are valid. */
Matthew Qinbb7923d2015-02-09 10:56:09 +08003187 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_size) ||
Mayank Grovere559cec2017-10-17 15:12:03 +05303188 check_ddr_addr_range_bound(hdr->kernel_addr, kernel_size) ||
3189 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual) ||
3190 check_ddr_addr_range_bound(hdr->ramdisk_addr, ramdisk_actual))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07003191 {
Mayank Grovere559cec2017-10-17 15:12:03 +05303192 dprintf(CRITICAL, "kernel/ramdisk addresses are not valid.\n");
lijuang2008ff22016-03-07 17:56:27 +08003193 goto boot_failed;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07003194 }
3195
Amol Jadicb524072012-08-09 16:40:18 -07003196#if DEVICE_TREE
Matthew Qin271927e2015-03-31 22:07:07 -04003197 scratch_offset = image_actual + page_size + out_len;
Amol Jadicb524072012-08-09 16:40:18 -07003198 /* find correct dtb and copy it to right location */
Matthew Qin271927e2015-03-31 22:07:07 -04003199 ret = copy_dtb(data, scratch_offset);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07003200
3201 dtb_copied = !ret ? 1 : 0;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07003202#else
Mayank Grovere559cec2017-10-17 15:12:03 +05303203 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE) ||
3204 check_ddr_addr_range_bound(hdr->tags_addr, MAX_TAGS_SIZE))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07003205 {
Mayank Grovere559cec2017-10-17 15:12:03 +05303206 dprintf(CRITICAL, "Tags addresses are not valid.\n");
lijuang2008ff22016-03-07 17:56:27 +08003207 goto boot_failed;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07003208 }
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07003209#endif
3210
3211 /* Load ramdisk & kernel */
3212 memmove((void*) hdr->ramdisk_addr, ptr + page_size + kernel_actual, hdr->ramdisk_size);
Matthew Qinbb7923d2015-02-09 10:56:09 +08003213 memmove((void*) hdr->kernel_addr, (char*) (kernel_start_addr), kernel_size);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07003214
3215#if DEVICE_TREE
Mayank Grovere559cec2017-10-17 15:12:03 +05303216 if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual) ||
3217 check_ddr_addr_range_bound(hdr->tags_addr, kernel_actual))
Matthew Qinbb7923d2015-02-09 10:56:09 +08003218 {
Mayank Grovere559cec2017-10-17 15:12:03 +05303219 dprintf(CRITICAL, "Tags addresses are not valid.\n");
lijuang2008ff22016-03-07 17:56:27 +08003220 goto boot_failed;
Matthew Qinbb7923d2015-02-09 10:56:09 +08003221 }
3222
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07003223 /*
3224 * If dtb is not found look for appended DTB in the kernel.
3225 * If appended dev tree is found, update the atags with
3226 * memory address to the DTB appended location on RAM.
3227 * Else update with the atags address in the kernel header
3228 */
3229 if (!dtb_copied) {
3230 void *dtb;
Matthew Qinbb7923d2015-02-09 10:56:09 +08003231 dtb = dev_tree_appended((void*)(ptr + page_size),
3232 hdr->kernel_size, dtb_offset,
Dima Zavine63e5572013-05-03 12:23:06 -07003233 (void *)hdr->tags_addr);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07003234 if (!dtb) {
3235 fastboot_fail("dtb not found");
lijuang2008ff22016-03-07 17:56:27 +08003236 goto boot_failed;
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07003237 }
Amol Jadicb524072012-08-09 16:40:18 -07003238 }
3239#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08003240
3241 fastboot_okay("");
Amol Jadi7c4316c2013-10-07 14:19:26 -07003242 fastboot_stop();
Brian Swetland9c4c0752009-01-25 16:23:50 -08003243
Dima Zavin77e41f32013-03-06 16:10:43 -08003244 boot_linux((void*) hdr->kernel_addr, (void*) hdr->tags_addr,
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07003245 (const char*) hdr->cmdline, board_machtype(),
3246 (void*) hdr->ramdisk_addr, hdr->ramdisk_size);
lijuang2008ff22016-03-07 17:56:27 +08003247
3248 /* fastboot already stop, it's no need to show fastboot menu */
3249 return;
3250boot_failed:
3251#if FBCON_DISPLAY_MSG
3252 /* revert to fastboot menu if boot failed */
3253 display_fastboot_menu();
3254#endif
3255 return;
Brian Swetland9c4c0752009-01-25 16:23:50 -08003256}
3257
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003258void cmd_erase_nand(const char *arg, void *data, unsigned sz)
Dima Zavin214cc642009-01-26 11:16:21 -08003259{
3260 struct ptentry *ptn;
3261 struct ptable *ptable;
3262
3263 ptable = flash_get_ptable();
3264 if (ptable == NULL) {
3265 fastboot_fail("partition table doesn't exist");
3266 return;
3267 }
3268
3269 ptn = ptable_find(ptable, arg);
3270 if (ptn == NULL) {
3271 fastboot_fail("unknown partition name");
3272 return;
3273 }
3274
Monika Singh292b3e92018-03-17 22:40:23 +05303275 if (!strncmp(arg, "avb_custom_key", strlen("avb_custom_key"))) {
3276 dprintf(INFO, "erasing avb_custom_key\n");
3277 if (erase_userkey()) {
3278 fastboot_fail("Erasing avb_custom_key failed");
3279 } else {
3280 fastboot_okay("");
3281 }
3282 return;
3283 }
3284
Dima Zavin214cc642009-01-26 11:16:21 -08003285 if (flash_erase(ptn)) {
3286 fastboot_fail("failed to erase partition");
3287 return;
3288 }
3289 fastboot_okay("");
3290}
3291
Bikas Gurungd48bd242010-09-04 19:54:32 -07003292
3293void cmd_erase_mmc(const char *arg, void *data, unsigned sz)
3294{
3295 unsigned long long ptn = 0;
Oliver Wangcee448d2013-10-22 18:40:13 +08003296 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07003297 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003298 uint8_t lun = 0;
Vijay Kumar Pendoti0b21f462016-05-02 17:09:18 +05303299 char *footer = NULL;
Bikas Gurungd48bd242010-09-04 19:54:32 -07003300
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003301#if VERIFIED_BOOT
3302 if(!strcmp(arg, KEYSTORE_PTN_NAME))
3303 {
3304 if(!device.is_unlocked)
3305 {
3306 fastboot_fail("unlock device to erase keystore");
3307 return;
3308 }
3309 }
3310#endif
3311
Kinson Chikf1a43512011-07-14 11:28:39 -07003312 index = partition_get_index(arg);
3313 ptn = partition_get_offset(index);
Oliver Wangcee448d2013-10-22 18:40:13 +08003314 size = partition_get_size(index);
Neeti Desaica8c9602011-10-06 11:40:00 -07003315
Monika Singhc4778b72018-05-16 11:16:42 +05303316 if (!strncmp(arg, "avb_custom_key", strlen("avb_custom_key"))) {
3317 dprintf(INFO, "erasing avb_custom_key\n");
3318 if (erase_userkey()) {
3319 fastboot_fail("Erasing avb_custom_key failed");
3320 } else {
3321 fastboot_okay("");
3322 }
3323 return;
3324 }
3325
Kinson Chikf1a43512011-07-14 11:28:39 -07003326 if(ptn == 0) {
Neeti Desaica8c9602011-10-06 11:40:00 -07003327 fastboot_fail("Partition table doesn't exist\n");
Bikas Gurungd48bd242010-09-04 19:54:32 -07003328 return;
3329 }
Kun Liang2f1601a2013-08-12 16:29:54 +08003330
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003331 lun = partition_get_lun(index);
3332 mmc_set_lun(lun);
3333
Sridhar Parasuramd7957122015-02-27 11:33:40 -08003334 if (platform_boot_dev_isemmc())
3335 {
3336 if (mmc_erase_card(ptn, size)) {
3337 fastboot_fail("failed to erase partition\n");
3338 return;
3339 }
3340 } else {
3341 BUF_DMA_ALIGN(out, DEFAULT_ERASE_SIZE);
3342 size = partition_get_size(index);
3343 if (size > DEFAULT_ERASE_SIZE)
3344 size = DEFAULT_ERASE_SIZE;
Kun Liang2f1601a2013-08-12 16:29:54 +08003345
Sridhar Parasuramd7957122015-02-27 11:33:40 -08003346 /* Simple inefficient version of erase. Just writing
3347 0 in first several blocks */
3348 if (mmc_write(ptn , size, (unsigned int *)out)) {
3349 fastboot_fail("failed to erase partition");
3350 return;
3351 }
Vijay Kumar Pendoti0b21f462016-05-02 17:09:18 +05303352 /*Erase FDE metadata at the userdata footer*/
3353 if(!(strncmp(arg, "userdata", 8)))
3354 {
3355 footer = memalign(CACHE_LINE, FOOTER_SIZE);
3356 memset((void *)footer, 0, FOOTER_SIZE);
3357
3358 size = partition_get_size(index);
3359
3360 if (mmc_write((ptn + size) - FOOTER_SIZE , FOOTER_SIZE, (unsigned int *)footer)) {
3361 fastboot_fail("failed to erase userdata footer");
3362 free(footer);
3363 return;
3364 }
3365 free(footer);
3366 }
Bikas Gurungd48bd242010-09-04 19:54:32 -07003367 }
Monika Singh292b3e92018-03-17 22:40:23 +05303368#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover912eaa62017-10-26 12:08:53 +05303369 if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05303370 !(strncmp(arg, "userdata", 8)) &&
3371 send_delete_keys_to_tz())
Sridhar Parasuram32b30662015-07-10 13:33:22 -07003372 ASSERT(0);
3373#endif
Bikas Gurungd48bd242010-09-04 19:54:32 -07003374 fastboot_okay("");
3375}
3376
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003377void cmd_erase(const char *arg, void *data, unsigned sz)
3378{
Monika Singh292b3e92018-03-17 22:40:23 +05303379#if VERIFIED_BOOT || VERIFIED_BOOT_2
Channagoud Kadabi35297672015-06-13 11:09:49 -07003380 if (target_build_variant_user())
Channagoud Kadabice3cf422015-04-17 16:02:30 -07003381 {
Sridhar Parasuramc32d07d2015-07-12 10:57:48 -07003382 if(!device.is_unlocked)
Channagoud Kadabice3cf422015-04-17 16:02:30 -07003383 {
Channagoud Kadabi35297672015-06-13 11:09:49 -07003384 fastboot_fail("device is locked. Cannot erase");
Channagoud Kadabice3cf422015-04-17 16:02:30 -07003385 return;
3386 }
3387 }
3388#endif
3389
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003390 if(target_is_emmc_boot())
3391 cmd_erase_mmc(arg, data, sz);
3392 else
3393 cmd_erase_nand(arg, data, sz);
3394}
Bikas Gurungd48bd242010-09-04 19:54:32 -07003395
Mayank Grover11ff9692018-01-11 11:54:49 +05303396/* Get the size from partiton name */
3397static void get_partition_size(const char *arg, char *response)
3398{
3399 uint64_t ptn = 0;
3400 uint64_t size;
3401 int index = INVALID_PTN;
3402
3403 index = partition_get_index(arg);
3404
3405 if (index == INVALID_PTN)
3406 {
3407 dprintf(CRITICAL, "Invalid partition index\n");
3408 return;
3409 }
3410
3411 ptn = partition_get_offset(index);
3412
3413 if(!ptn)
3414 {
3415 dprintf(CRITICAL, "Invalid partition name %s\n", arg);
3416 return;
3417 }
3418
3419 size = partition_get_size(index);
3420
3421 snprintf(response, MAX_RSP_SIZE, "\t 0x%llx", size);
3422 return;
3423}
3424
Mayank Grover52cd10a2018-03-15 12:57:54 +05303425/* Function to check partition type of a partition*/
3426static fs_signature_type
3427check_partition_fs_signature(const char *arg)
3428{
3429 fs_signature_type ret = NO_FS;
3430 int index;
3431 unsigned long long ptn;
Mayank Grover399826a2018-08-27 12:15:15 +05303432 char *buffer = memalign(CACHE_LINE, mmc_blocksize);
3433 uint32_t sb_blk_offset = 0;
3434 char *sb_buffer = buffer;
3435
Mayank Grover52cd10a2018-03-15 12:57:54 +05303436 if (!sb_buffer)
3437 {
3438 dprintf(CRITICAL, "ERROR: Failed to allocate buffer for superblock\n");
3439 goto out;
3440 }
3441
3442 /* Read super block */
3443 if ((index = partition_get_index(arg)) < 0)
3444 {
3445 dprintf(CRITICAL, "ERROR: %s() doesn't exsit\n", arg);
3446 goto out;
3447 }
3448 ptn = partition_get_offset(index);
3449 mmc_set_lun(partition_get_lun(index));
Mayank Grover399826a2018-08-27 12:15:15 +05303450 sb_blk_offset = (FS_SUPERBLOCK_OFFSET/mmc_blocksize);
3451
3452 if(mmc_read(ptn + (sb_blk_offset * mmc_blocksize),
Mayank Grover52cd10a2018-03-15 12:57:54 +05303453 (void *)sb_buffer, mmc_blocksize))
3454 {
3455 dprintf(CRITICAL, "ERROR: Failed to read Superblock\n");
3456 goto out;
3457 }
3458
Mayank Grover399826a2018-08-27 12:15:15 +05303459 if (sb_blk_offset == 0)
3460 sb_buffer += FS_SUPERBLOCK_OFFSET;
3461
3462 if (*((uint16 *)(&sb_buffer[EXT_MAGIC_OFFSET_SB])) == (uint16)EXT_MAGIC)
Mayank Grover52cd10a2018-03-15 12:57:54 +05303463 {
3464 dprintf(SPEW, "%s() Found EXT FS\n", arg);
3465 ret = EXT_FS_SIGNATURE;
3466 }
Mayank Grover399826a2018-08-27 12:15:15 +05303467 else if (*((uint32 *)(&sb_buffer[F2FS_MAGIC_OFFSET_SB])) == F2FS_MAGIC)
Mayank Grover52cd10a2018-03-15 12:57:54 +05303468 {
3469 dprintf(SPEW, "%s() Found F2FS FS\n", arg);
3470 ret = EXT_F2FS_SIGNATURE;
3471 }
3472 else
3473 {
3474 dprintf(SPEW, "%s() Reverting to default 0x%x\n",
3475 arg, *((uint16 *)(&sb_buffer[EXT_MAGIC_OFFSET_SB])));
3476 ret = NO_FS;
3477 }
3478
3479out:
Mayank Grover399826a2018-08-27 12:15:15 +05303480 if(buffer)
3481 free(buffer);
Mayank Grover52cd10a2018-03-15 12:57:54 +05303482 return ret;
3483}
3484
Mayank Groverd38fe012018-03-13 15:33:16 +05303485/* Function to get partition type */
3486static void get_partition_type(const char *arg, char *response)
3487{
3488 uint n = 0;
Mayank Grover52cd10a2018-03-15 12:57:54 +05303489 fs_signature_type fs_signature;
Mayank Groverd38fe012018-03-13 15:33:16 +05303490
3491 if (arg == NULL ||
3492 response == NULL)
3493 {
3494 dprintf(CRITICAL, "Invalid input parameter\n");
3495 return;
3496 }
3497
3498 /* By default copy raw to response */
Mayank Grover0e559042018-04-11 17:49:30 +05303499 strlcpy(response, RAW_STR, MAX_RSP_SIZE);
Mayank Groverd38fe012018-03-13 15:33:16 +05303500
3501 /* Mark partiton type for known paritions only */
3502 for (n=0; n < ARRAY_SIZE(part_type_known); n++)
3503 {
3504 if (!strncmp(part_type_known[n].part_name, arg, strlen(arg)))
3505 {
Mayank Grover52cd10a2018-03-15 12:57:54 +05303506 /* Check partition for FS signature */
3507 fs_signature = check_partition_fs_signature(arg);
3508 switch (fs_signature)
3509 {
3510 case EXT_FS_SIGNATURE:
Mayank Grover0e559042018-04-11 17:49:30 +05303511 strlcpy(response, EXT_STR, MAX_RSP_SIZE);
Mayank Grover52cd10a2018-03-15 12:57:54 +05303512 break;
3513 case EXT_F2FS_SIGNATURE:
Mayank Grover0e559042018-04-11 17:49:30 +05303514 strlcpy(response, F2FS_STR, MAX_RSP_SIZE);
Mayank Grover52cd10a2018-03-15 12:57:54 +05303515 break;
3516 case NO_FS:
Mayank Grover0e559042018-04-11 17:49:30 +05303517 strlcpy(response, part_type_known[n].type_response, MAX_RSP_SIZE);
Mayank Grover52cd10a2018-03-15 12:57:54 +05303518 }
Mayank Groverd38fe012018-03-13 15:33:16 +05303519 }
3520 }
3521 return;
3522}
3523
Mayank Grover11ff9692018-01-11 11:54:49 +05303524/*
3525 * Publish the partition type & size info
3526 * fastboot getvar will publish the required information.
3527 * fastboot getvar partition_size:<partition_name>: partition size in hex
3528 * fastboot getvar partition_type:<partition_name>: partition type (ext/fat)
3529 */
3530static void publish_getvar_partition_info(struct getvar_partition_info *info, uint8_t num_parts)
3531{
Mayank Groverd38fe012018-03-13 15:33:16 +05303532 uint8_t i;
Mayank Grover11ff9692018-01-11 11:54:49 +05303533 static bool published = false;
3534 struct partition_entry *ptn_entry =
3535 partition_get_partition_entries();
3536 memset(info, 0, sizeof(struct getvar_partition_info)* num_parts);
3537
3538 for (i = 0; i < num_parts; i++) {
3539 strlcat(info[i].part_name, (const char *)ptn_entry[i].name, MAX_RSP_SIZE);
3540 strlcat(info[i].getvar_size, "partition-size:", MAX_GET_VAR_NAME_SIZE);
3541 strlcat(info[i].getvar_type, "partition-type:", MAX_GET_VAR_NAME_SIZE);
3542
Mayank Groverd38fe012018-03-13 15:33:16 +05303543 get_partition_type(info[i].part_name, info[i].type_response);
Mayank Grover11ff9692018-01-11 11:54:49 +05303544 get_partition_size(info[i].part_name, info[i].size_response);
Mayank Grover11ff9692018-01-11 11:54:49 +05303545 if (strlcat(info[i].getvar_size, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
3546 {
3547 dprintf(CRITICAL, "partition size name truncated\n");
3548 return;
3549 }
3550 if (strlcat(info[i].getvar_type, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
3551 {
3552 dprintf(CRITICAL, "partition type name truncated\n");
3553 return;
3554 }
3555
3556 if (!published)
3557 {
3558 /* publish partition size & type info */
3559 fastboot_publish((const char *) info[i].getvar_size, (const char *) info[i].size_response);
3560 fastboot_publish((const char *) info[i].getvar_type, (const char *) info[i].type_response);
3561 }
3562 }
3563 if (!published)
3564 published = true;
3565}
3566
3567
Ajay Dudani5c761132011-04-07 20:19:04 -07003568void cmd_flash_mmc_img(const char *arg, void *data, unsigned sz)
Shashank Mittal23b8f422010-04-16 19:27:21 -07003569{
3570 unsigned long long ptn = 0;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07003571 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07003572 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003573 char *token = NULL;
3574 char *pname = NULL;
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07003575 char *sp;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003576 uint8_t lun = 0;
3577 bool lun_set = false;
Mayank Grover351a75e2017-05-30 20:06:08 +05303578 int current_active_slot = INVALID;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07003579
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07003580 token = strtok_r((char *)arg, ":", &sp);
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003581 pname = token;
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07003582 token = strtok_r(NULL, ":", &sp);
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003583 if(token)
3584 {
3585 lun = atoi(token);
3586 mmc_set_lun(lun);
3587 lun_set = true;
3588 }
3589
Mao Jinlong226f33a2014-07-04 17:24:10 +08003590 if (pname)
Greg Grisco6e754772011-06-23 12:19:39 -07003591 {
Channagoud Kadabiad259832015-05-29 11:14:17 -07003592 if (!strncmp(pname, "frp-unlock", strlen("frp-unlock")))
3593 {
3594 if (!aboot_frp_unlock(pname, data, sz))
3595 {
3596 fastboot_info("FRP unlock successful");
3597 fastboot_okay("");
3598 }
3599 else
3600 fastboot_fail("Secret key is invalid, please update the bootloader with secret key");
3601
3602 return;
3603 }
3604
Mao Jinlong226f33a2014-07-04 17:24:10 +08003605 if (!strcmp(pname, "partition"))
3606 {
3607 dprintf(INFO, "Attempt to write partition image.\n");
3608 if (write_partition(sz, (unsigned char *) data)) {
3609 fastboot_fail("failed to write partition");
Greg Grisco6e754772011-06-23 12:19:39 -07003610 return;
3611 }
Mayank Grover11ff9692018-01-11 11:54:49 +05303612 /* Re-publish partition table */
3613 publish_getvar_partition_info(part_info, partition_get_partition_count());
Mayank Grover351a75e2017-05-30 20:06:08 +05303614
3615 /* Rescan partition table to ensure we have multislot support*/
3616 if (partition_scan_for_multislot())
3617 {
3618 current_active_slot = partition_find_active_slot();
3619 dprintf(INFO, "Multislot supported: Slot %s active",
3620 (SUFFIX_SLOT(current_active_slot)));
3621 }
3622 partition_mark_active_slot(current_active_slot);
Greg Grisco6e754772011-06-23 12:19:39 -07003623 }
Mao Jinlong226f33a2014-07-04 17:24:10 +08003624 else
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003625 {
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003626#if VERIFIED_BOOT
3627 if(!strcmp(pname, KEYSTORE_PTN_NAME))
3628 {
3629 if(!device.is_unlocked)
3630 {
3631 fastboot_fail("unlock device to flash keystore");
3632 return;
3633 }
Gaurav Nebhwanic7313cc2015-12-15 22:25:04 +05303634 if(!boot_verify_validate_keystore((unsigned char *)data,sz))
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003635 {
3636 fastboot_fail("image is not a keystore file");
3637 return;
3638 }
3639 }
3640#endif
Mao Jinlong226f33a2014-07-04 17:24:10 +08003641 index = partition_get_index(pname);
3642 ptn = partition_get_offset(index);
3643 if(ptn == 0) {
3644 fastboot_fail("partition table doesn't exist");
3645 return;
3646 }
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003647
Mayank Grover351a75e2017-05-30 20:06:08 +05303648 if (!strncmp(pname, "boot", strlen("boot"))
3649 || !strcmp(pname, "recovery"))
3650 {
Mao Jinlong226f33a2014-07-04 17:24:10 +08003651 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
3652 fastboot_fail("image is not a boot image");
3653 return;
3654 }
Mayank Grover351a75e2017-05-30 20:06:08 +05303655
3656 /* Reset multislot_partition attributes in case of flashing boot */
3657 if (partition_multislot_is_supported())
3658 {
3659 partition_reset_attributes(index);
3660 }
Mao Jinlong226f33a2014-07-04 17:24:10 +08003661 }
3662
3663 if(!lun_set)
3664 {
3665 lun = partition_get_lun(index);
3666 mmc_set_lun(lun);
3667 }
3668
3669 size = partition_get_size(index);
Gaurav Nebhwani4d2389c2016-03-17 21:10:05 +05303670 if (ROUND_TO_PAGE(sz, mmc_blocksize_mask) > size) {
Mao Jinlong226f33a2014-07-04 17:24:10 +08003671 fastboot_fail("size too large");
3672 return;
3673 }
3674 else if (mmc_write(ptn , sz, (unsigned int *)data)) {
3675 fastboot_fail("flash write failure");
3676 return;
3677 }
Greg Grisco6e754772011-06-23 12:19:39 -07003678 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07003679 }
3680 fastboot_okay("");
3681 return;
3682}
3683
Ajay Dudanide984792015-03-02 09:57:41 -08003684void cmd_flash_meta_img(const char *arg, void *data, unsigned sz)
3685{
3686 int i, images;
3687 meta_header_t *meta_header;
3688 img_header_entry_t *img_header_entry;
Parth Dixit3e2d3032016-03-04 17:11:52 +05303689 /*End of the image address*/
3690 uintptr_t data_end;
3691
3692 if( (UINT_MAX - sz) > (uintptr_t)data )
3693 data_end = (uintptr_t)data + sz;
3694 else
3695 {
3696 fastboot_fail("Cannot flash: image header corrupt");
3697 return;
3698 }
3699
3700 if( data_end < ((uintptr_t)data + sizeof(meta_header_t)))
3701 {
3702 fastboot_fail("Cannot flash: image header corrupt");
3703 return;
3704 }
Ajay Dudanide984792015-03-02 09:57:41 -08003705
lijuang8ee21882016-04-19 16:57:11 +08003706 /* If device is locked:
3707 * Forbid to flash image to avoid the device to bypass the image
3708 * which with "any" name other than bootloader. Because it maybe
3709 * a meta package of all partitions.
3710 */
Monika Singh292b3e92018-03-17 22:40:23 +05303711#if VERIFIED_BOOT || VERIFIED_BOOT_2
lijuang8ee21882016-04-19 16:57:11 +08003712 if (target_build_variant_user()) {
3713 if (!device.is_unlocked) {
3714 fastboot_fail("Device is locked, meta image flashing is not allowed");
3715 return;
3716 }
Mayank Grover889be1b2017-09-12 20:12:23 +05303717
Mayank Grover912eaa62017-10-26 12:08:53 +05303718 if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05303719 !device.is_unlock_critical)
3720 {
lijuang8ee21882016-04-19 16:57:11 +08003721 fastboot_fail("Device is critical locked, Meta image flashing is not allowed");
3722 return;
3723 }
lijuang8ee21882016-04-19 16:57:11 +08003724 }
3725#endif
3726
Ajay Dudanide984792015-03-02 09:57:41 -08003727 meta_header = (meta_header_t*) data;
Parth Dixit3e2d3032016-03-04 17:11:52 +05303728 if( data_end < ((uintptr_t)data + meta_header->img_hdr_sz))
3729 {
3730 fastboot_fail("Cannot flash: image header corrupt");
3731 return;
3732 }
Ajay Dudanide984792015-03-02 09:57:41 -08003733 img_header_entry = (img_header_entry_t*) (data+sizeof(meta_header_t));
3734
3735 images = meta_header->img_hdr_sz / sizeof(img_header_entry_t);
3736
3737 for (i=0; i<images; i++) {
3738
3739 if((img_header_entry[i].ptn_name == NULL) ||
3740 (img_header_entry[i].start_offset == 0) ||
3741 (img_header_entry[i].size == 0))
3742 break;
Kishor PK49831502017-04-21 17:55:43 +05303743 if ((UINT_MAX - img_header_entry[i].start_offset) < (uintptr_t)data) {
3744 fastboot_fail("Integer overflow detected in start_offset of img");
3745 break;
3746 }
3747 else if ((UINT_MAX - (img_header_entry[i].start_offset + (uintptr_t)data)) < img_header_entry[i].size) {
3748 fastboot_fail("Integer overflow detected in size of img");
3749 break;
3750 }
Parth Dixit3e2d3032016-03-04 17:11:52 +05303751 if( data_end < ((uintptr_t)data + img_header_entry[i].start_offset
3752 + img_header_entry[i].size) )
3753 {
3754 fastboot_fail("Cannot flash: image size mismatch");
3755 break;
3756 }
3757
Ajay Dudanide984792015-03-02 09:57:41 -08003758 cmd_flash_mmc_img(img_header_entry[i].ptn_name,
3759 (void *) data + img_header_entry[i].start_offset,
3760 img_header_entry[i].size);
3761 }
3762
Channagoud Kadabiaafbbfc2015-03-04 17:52:56 -08003763 if (!strncmp(arg, "bootloader", strlen("bootloader")))
3764 {
3765 strlcpy(device.bootloader_version, TARGET(BOARD), MAX_VERSION_LEN);
3766 strlcat(device.bootloader_version, "-", MAX_VERSION_LEN);
3767 strlcat(device.bootloader_version, meta_header->img_version, MAX_VERSION_LEN);
3768 }
3769 else
3770 {
3771 strlcpy(device.radio_version, TARGET(BOARD), MAX_VERSION_LEN);
3772 strlcat(device.radio_version, "-", MAX_VERSION_LEN);
3773 strlcat(device.radio_version, meta_header->img_version, MAX_VERSION_LEN);
3774 }
3775
3776 write_device_info(&device);
Ajay Dudanide984792015-03-02 09:57:41 -08003777 fastboot_okay("");
3778 return;
3779}
3780
Ajay Dudani5c761132011-04-07 20:19:04 -07003781void cmd_flash_mmc_sparse_img(const char *arg, void *data, unsigned sz)
3782{
3783 unsigned int chunk;
wufeng.jiang813dc352015-06-02 23:02:46 -04003784 uint64_t chunk_data_sz;
Channagoud Kadabif627cf82013-09-09 14:08:20 -07003785 uint32_t *fill_buf = NULL;
3786 uint32_t fill_val;
Mayank Grover4f50bba2017-07-19 18:17:08 +05303787 uint32_t blk_sz_actual = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07003788 sparse_header_t *sparse_header;
3789 chunk_header_t *chunk_header;
Ajay Dudaniab18f022011-05-12 14:39:22 -07003790 uint32_t total_blocks = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07003791 unsigned long long ptn = 0;
Channagoud Kadabi65b91002011-10-11 17:34:33 +05303792 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07003793 int index = INVALID_PTN;
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05303794 uint32_t i;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003795 uint8_t lun = 0;
vijay kumar800255e2015-04-24 20:26:19 +05303796 /*End of the sparse image address*/
Parth Dixit64b1a482016-03-07 16:31:26 +05303797 uintptr_t data_end = (uintptr_t)data + sz;
Ajay Dudani5c761132011-04-07 20:19:04 -07003798
Kinson Chikf1a43512011-07-14 11:28:39 -07003799 index = partition_get_index(arg);
3800 ptn = partition_get_offset(index);
3801 if(ptn == 0) {
Ajay Dudani5c761132011-04-07 20:19:04 -07003802 fastboot_fail("partition table doesn't exist");
3803 return;
3804 }
3805
Channagoud Kadabi65b91002011-10-11 17:34:33 +05303806 size = partition_get_size(index);
Channagoud Kadabi65b91002011-10-11 17:34:33 +05303807
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003808 lun = partition_get_lun(index);
3809 mmc_set_lun(lun);
3810
vijay kumar800255e2015-04-24 20:26:19 +05303811 if (sz < sizeof(sparse_header_t)) {
3812 fastboot_fail("size too low");
3813 return;
3814 }
3815
Ajay Dudani5c761132011-04-07 20:19:04 -07003816 /* Read and skip over sparse image header */
3817 sparse_header = (sparse_header_t *) data;
vijay kumar800255e2015-04-24 20:26:19 +05303818
Mayank Grover48bd9bb2017-07-19 12:04:16 +05303819 if (!sparse_header->blk_sz || (sparse_header->blk_sz % 4)){
3820 fastboot_fail("Invalid block size\n");
3821 return;
3822 }
3823
vijay kumar1321f342015-03-27 12:13:42 +05303824 if (((uint64_t)sparse_header->total_blks * (uint64_t)sparse_header->blk_sz) > size) {
Ajay Dudani876b3282012-12-21 14:12:17 -08003825 fastboot_fail("size too large");
3826 return;
3827 }
3828
vijay kumarde4fcf62015-04-23 13:05:49 +05303829 data += sizeof(sparse_header_t);
vijay kumar800255e2015-04-24 20:26:19 +05303830
Parth Dixit64b1a482016-03-07 16:31:26 +05303831 if (data_end < (uintptr_t)data) {
vijay kumar800255e2015-04-24 20:26:19 +05303832 fastboot_fail("buffer overreads occured due to invalid sparse header");
3833 return;
3834 }
3835
vijay kumarde4fcf62015-04-23 13:05:49 +05303836 if(sparse_header->file_hdr_sz != sizeof(sparse_header_t))
Ajay Dudani5c761132011-04-07 20:19:04 -07003837 {
vijay kumarde4fcf62015-04-23 13:05:49 +05303838 fastboot_fail("sparse header size mismatch");
3839 return;
Ajay Dudani5c761132011-04-07 20:19:04 -07003840 }
3841
Ajay Dudanib06c05f2011-05-12 14:46:10 -07003842 dprintf (SPEW, "=== Sparse Image Header ===\n");
3843 dprintf (SPEW, "magic: 0x%x\n", sparse_header->magic);
3844 dprintf (SPEW, "major_version: 0x%x\n", sparse_header->major_version);
3845 dprintf (SPEW, "minor_version: 0x%x\n", sparse_header->minor_version);
3846 dprintf (SPEW, "file_hdr_sz: %d\n", sparse_header->file_hdr_sz);
3847 dprintf (SPEW, "chunk_hdr_sz: %d\n", sparse_header->chunk_hdr_sz);
3848 dprintf (SPEW, "blk_sz: %d\n", sparse_header->blk_sz);
3849 dprintf (SPEW, "total_blks: %d\n", sparse_header->total_blks);
3850 dprintf (SPEW, "total_chunks: %d\n", sparse_header->total_chunks);
Ajay Dudani5c761132011-04-07 20:19:04 -07003851
3852 /* Start processing chunks */
3853 for (chunk=0; chunk<sparse_header->total_chunks; chunk++)
3854 {
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05303855 /* Make sure the total image size does not exceed the partition size */
3856 if(((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz) >= size) {
3857 fastboot_fail("size too large");
3858 return;
3859 }
Ajay Dudani5c761132011-04-07 20:19:04 -07003860 /* Read and skip over chunk header */
3861 chunk_header = (chunk_header_t *) data;
3862 data += sizeof(chunk_header_t);
3863
Parth Dixit64b1a482016-03-07 16:31:26 +05303864 if (data_end < (uintptr_t)data) {
vijay kumar800255e2015-04-24 20:26:19 +05303865 fastboot_fail("buffer overreads occured due to invalid sparse header");
3866 return;
3867 }
3868
Ajay Dudani5c761132011-04-07 20:19:04 -07003869 dprintf (SPEW, "=== Chunk Header ===\n");
3870 dprintf (SPEW, "chunk_type: 0x%x\n", chunk_header->chunk_type);
3871 dprintf (SPEW, "chunk_data_sz: 0x%x\n", chunk_header->chunk_sz);
3872 dprintf (SPEW, "total_size: 0x%x\n", chunk_header->total_sz);
3873
vijay kumar800255e2015-04-24 20:26:19 +05303874 if(sparse_header->chunk_hdr_sz != sizeof(chunk_header_t))
Ajay Dudani5c761132011-04-07 20:19:04 -07003875 {
vijay kumar800255e2015-04-24 20:26:19 +05303876 fastboot_fail("chunk header size mismatch");
3877 return;
Ajay Dudani5c761132011-04-07 20:19:04 -07003878 }
3879
wufeng.jiang813dc352015-06-02 23:02:46 -04003880 chunk_data_sz = (uint64_t)sparse_header->blk_sz * chunk_header->chunk_sz;
3881
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05303882 /* Make sure that the chunk size calculated from sparse image does not
3883 * exceed partition size
3884 */
3885 if ((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz + chunk_data_sz > size)
3886 {
3887 fastboot_fail("Chunk data size exceeds partition size");
3888 return;
3889 }
3890
Ajay Dudani5c761132011-04-07 20:19:04 -07003891 switch (chunk_header->chunk_type)
3892 {
3893 case CHUNK_TYPE_RAW:
wufeng.jiang813dc352015-06-02 23:02:46 -04003894 if((uint64_t)chunk_header->total_sz != ((uint64_t)sparse_header->chunk_hdr_sz +
Ajay Dudani5c761132011-04-07 20:19:04 -07003895 chunk_data_sz))
3896 {
3897 fastboot_fail("Bogus chunk size for chunk type Raw");
3898 return;
3899 }
3900
Parth Dixit64b1a482016-03-07 16:31:26 +05303901 if (data_end < (uintptr_t)data + chunk_data_sz) {
vijay kumar800255e2015-04-24 20:26:19 +05303902 fastboot_fail("buffer overreads occured due to invalid sparse header");
3903 return;
3904 }
3905
wufeng.jiang813dc352015-06-02 23:02:46 -04003906 /* chunk_header->total_sz is uint32,So chunk_data_sz is now less than 2^32
3907 otherwise it will return in the line above
3908 */
Ajay Dudaniab18f022011-05-12 14:39:22 -07003909 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
wufeng.jiang813dc352015-06-02 23:02:46 -04003910 (uint32_t)chunk_data_sz,
Ajay Dudaniab18f022011-05-12 14:39:22 -07003911 (unsigned int*)data))
Ajay Dudani5c761132011-04-07 20:19:04 -07003912 {
3913 fastboot_fail("flash write failure");
3914 return;
3915 }
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05303916 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
3917 fastboot_fail("Bogus size for RAW chunk type");
3918 return;
3919 }
Ajay Dudani5c761132011-04-07 20:19:04 -07003920 total_blocks += chunk_header->chunk_sz;
wufeng.jiang813dc352015-06-02 23:02:46 -04003921 data += (uint32_t)chunk_data_sz;
Ajay Dudani5c761132011-04-07 20:19:04 -07003922 break;
3923
Channagoud Kadabif627cf82013-09-09 14:08:20 -07003924 case CHUNK_TYPE_FILL:
3925 if(chunk_header->total_sz != (sparse_header->chunk_hdr_sz +
3926 sizeof(uint32_t)))
3927 {
3928 fastboot_fail("Bogus chunk size for chunk type FILL");
3929 return;
3930 }
3931
Mayank Grover4f50bba2017-07-19 18:17:08 +05303932 blk_sz_actual = ROUNDUP(sparse_header->blk_sz, CACHE_LINE);
3933 /* Integer overflow detected */
3934 if (blk_sz_actual < sparse_header->blk_sz)
3935 {
3936 fastboot_fail("Invalid block size");
3937 return;
3938 }
3939
3940 fill_buf = (uint32_t *)memalign(CACHE_LINE, blk_sz_actual);
Channagoud Kadabif627cf82013-09-09 14:08:20 -07003941 if (!fill_buf)
3942 {
3943 fastboot_fail("Malloc failed for: CHUNK_TYPE_FILL");
3944 return;
3945 }
3946
Parth Dixit64b1a482016-03-07 16:31:26 +05303947 if (data_end < (uintptr_t)data + sizeof(uint32_t)) {
vijay kumar800255e2015-04-24 20:26:19 +05303948 fastboot_fail("buffer overreads occured due to invalid sparse header");
Gaurav Nebhwani163cbf82016-03-17 21:21:27 +05303949 free(fill_buf);
vijay kumar800255e2015-04-24 20:26:19 +05303950 return;
3951 }
Channagoud Kadabif627cf82013-09-09 14:08:20 -07003952 fill_val = *(uint32_t *)data;
3953 data = (char *) data + sizeof(uint32_t);
Channagoud Kadabif627cf82013-09-09 14:08:20 -07003954
3955 for (i = 0; i < (sparse_header->blk_sz / sizeof(fill_val)); i++)
3956 {
3957 fill_buf[i] = fill_val;
3958 }
3959
Gaurav Nebhwanie94cbe12016-03-17 21:16:34 +05303960 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz))
3961 {
3962 fastboot_fail("bogus size for chunk FILL type");
3963 free(fill_buf);
3964 return;
3965 }
3966
wufeng.jiang813dc352015-06-02 23:02:46 -04003967 for (i = 0; i < chunk_header->chunk_sz; i++)
Channagoud Kadabif627cf82013-09-09 14:08:20 -07003968 {
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05303969 /* Make sure that the data written to partition does not exceed partition size */
3970 if ((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz + sparse_header->blk_sz > size)
3971 {
3972 fastboot_fail("Chunk data size for fill type exceeds partition size");
Gaurav Nebhwani163cbf82016-03-17 21:21:27 +05303973 free(fill_buf);
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05303974 return;
3975 }
3976
Channagoud Kadabif627cf82013-09-09 14:08:20 -07003977 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
3978 sparse_header->blk_sz,
3979 fill_buf))
3980 {
3981 fastboot_fail("flash write failure");
3982 free(fill_buf);
3983 return;
3984 }
3985
3986 total_blocks++;
3987 }
3988
3989 free(fill_buf);
3990 break;
3991
Ajay Dudani5c761132011-04-07 20:19:04 -07003992 case CHUNK_TYPE_DONT_CARE:
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05303993 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
3994 fastboot_fail("bogus size for chunk DONT CARE type");
3995 return;
3996 }
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07003997 total_blocks += chunk_header->chunk_sz;
3998 break;
3999
Ajay Dudani5c761132011-04-07 20:19:04 -07004000 case CHUNK_TYPE_CRC:
4001 if(chunk_header->total_sz != sparse_header->chunk_hdr_sz)
4002 {
wufeng.jiang813dc352015-06-02 23:02:46 -04004003 fastboot_fail("Bogus chunk size for chunk type CRC");
Ajay Dudani5c761132011-04-07 20:19:04 -07004004 return;
4005 }
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05304006 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
4007 fastboot_fail("bogus size for chunk CRC type");
4008 return;
4009 }
Ajay Dudani5c761132011-04-07 20:19:04 -07004010 total_blocks += chunk_header->chunk_sz;
Parth Dixit64b1a482016-03-07 16:31:26 +05304011 if ((uintptr_t)data > UINT_MAX - chunk_data_sz) {
vijay kumar800255e2015-04-24 20:26:19 +05304012 fastboot_fail("integer overflow occured");
4013 return;
4014 }
wufeng.jiang813dc352015-06-02 23:02:46 -04004015 data += (uint32_t)chunk_data_sz;
Parth Dixit64b1a482016-03-07 16:31:26 +05304016 if (data_end < (uintptr_t)data) {
vijay kumar800255e2015-04-24 20:26:19 +05304017 fastboot_fail("buffer overreads occured due to invalid sparse header");
4018 return;
4019 }
Ajay Dudani5c761132011-04-07 20:19:04 -07004020 break;
4021
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07004022 default:
Channagoud Kadabif627cf82013-09-09 14:08:20 -07004023 dprintf(CRITICAL, "Unkown chunk type: %x\n",chunk_header->chunk_type);
Ajay Dudani5c761132011-04-07 20:19:04 -07004024 fastboot_fail("Unknown chunk type");
4025 return;
4026 }
4027 }
4028
Ajay Dudani0c6927b2011-05-18 11:12:16 -07004029 dprintf(INFO, "Wrote %d blocks, expected to write %d blocks\n",
4030 total_blocks, sparse_header->total_blks);
4031
4032 if(total_blocks != sparse_header->total_blks)
4033 {
4034 fastboot_fail("sparse image write failure");
4035 }
Ajay Dudani5c761132011-04-07 20:19:04 -07004036
4037 fastboot_okay("");
4038 return;
4039}
4040
4041void cmd_flash_mmc(const char *arg, void *data, unsigned sz)
4042{
4043 sparse_header_t *sparse_header;
Ajay Dudanide984792015-03-02 09:57:41 -08004044 meta_header_t *meta_header;
Ajay Dudani5c761132011-04-07 20:19:04 -07004045
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07004046#ifdef SSD_ENABLE
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -08004047 /* 8 Byte Magic + 2048 Byte xml + Encrypted Data */
4048 unsigned int *magic_number = (unsigned int *) data;
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08004049 int ret=0;
4050 uint32 major_version=0;
4051 uint32 minor_version=0;
4052
4053 ret = scm_svc_version(&major_version,&minor_version);
4054 if(!ret)
4055 {
4056 if(major_version >= 2)
4057 {
Amir Samuelovbb65ce02013-05-05 12:20:18 +03004058 if( !strcmp(arg, "ssd") || !strcmp(arg, "tqs") )
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08004059 {
4060 ret = encrypt_scm((uint32 **) &data, &sz);
4061 if (ret != 0) {
4062 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
4063 return;
4064 }
4065
Amir Samuelovbb65ce02013-05-05 12:20:18 +03004066 /* Protect only for SSD */
4067 if (!strcmp(arg, "ssd")) {
4068 ret = scm_protect_keystore((uint32 *) data, sz);
4069 if (ret != 0) {
4070 dprintf(CRITICAL, "ERROR: scm_protect_keystore Failed\n");
4071 return;
4072 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08004073 }
4074 }
4075 else
4076 {
4077 ret = decrypt_scm_v2((uint32 **) &data, &sz);
4078 if(ret != 0)
4079 {
4080 dprintf(CRITICAL,"ERROR: Decryption Failure\n");
4081 return;
4082 }
4083 }
4084 }
4085 else
4086 {
4087 if (magic_number[0] == DECRYPT_MAGIC_0 &&
4088 magic_number[1] == DECRYPT_MAGIC_1)
4089 {
4090 ret = decrypt_scm((uint32 **) &data, &sz);
4091 if (ret != 0) {
4092 dprintf(CRITICAL, "ERROR: Invalid secure image\n");
4093 return;
4094 }
4095 }
4096 else if (magic_number[0] == ENCRYPT_MAGIC_0 &&
4097 magic_number[1] == ENCRYPT_MAGIC_1)
4098 {
4099 ret = encrypt_scm((uint32 **) &data, &sz);
4100 if (ret != 0) {
4101 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
4102 return;
4103 }
4104 }
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07004105 }
4106 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08004107 else
Neeti Desai127b9e02012-03-20 16:11:23 -07004108 {
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08004109 dprintf(CRITICAL,"INVALID SVC Version\n");
4110 return;
Neeti Desai127b9e02012-03-20 16:11:23 -07004111 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08004112#endif /* SSD_ENABLE */
Neeti Desai127b9e02012-03-20 16:11:23 -07004113
Monika Singh292b3e92018-03-17 22:40:23 +05304114#if VERIFIED_BOOT || VERIFIED_BOOT_2
Channagoud Kadabi35297672015-06-13 11:09:49 -07004115 if (target_build_variant_user())
Shashank Mittald3e54dd2014-08-28 15:24:02 -07004116 {
lijuang511a2b52015-08-14 20:50:51 +08004117 /* if device is locked:
4118 * common partition will not allow to be flashed
4119 * critical partition will allow to flash image.
4120 */
4121 if(!device.is_unlocked && !critical_flash_allowed(arg)) {
4122 fastboot_fail("Partition flashing is not allowed");
4123 return;
4124 }
Mayank Grover889be1b2017-09-12 20:12:23 +05304125
lijuang511a2b52015-08-14 20:50:51 +08004126 /* if device critical is locked:
4127 * common partition will allow to be flashed
4128 * critical partition will not allow to flash image.
4129 */
Mayank Grover912eaa62017-10-26 12:08:53 +05304130 if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05304131 !device.is_unlock_critical &&
4132 critical_flash_allowed(arg)) {
4133 fastboot_fail("Critical partition flashing is not allowed");
4134 return;
Shashank Mittald3e54dd2014-08-28 15:24:02 -07004135 }
4136 }
4137#endif
Monika Singh292b3e92018-03-17 22:40:23 +05304138 if (!strncmp(arg, "avb_custom_key", strlen("avb_custom_key"))) {
4139 dprintf(INFO, "flashing avb_custom_key\n");
4140 if (store_userkey(data, sz)) {
4141 fastboot_fail("Flashing avb_custom_key failed");
4142 } else {
4143 fastboot_okay("");
4144 }
4145 return;
4146 }
Shashank Mittald3e54dd2014-08-28 15:24:02 -07004147
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07004148 sparse_header = (sparse_header_t *) data;
Ajay Dudanide984792015-03-02 09:57:41 -08004149 meta_header = (meta_header_t *) data;
4150 if (sparse_header->magic == SPARSE_HEADER_MAGIC)
Ajay Dudani5c761132011-04-07 20:19:04 -07004151 cmd_flash_mmc_sparse_img(arg, data, sz);
Ajay Dudanide984792015-03-02 09:57:41 -08004152 else if (meta_header->magic == META_HEADER_MAGIC)
4153 cmd_flash_meta_img(arg, data, sz);
4154 else
4155 cmd_flash_mmc_img(arg, data, sz);
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07004156
4157#if VERIFIED_BOOT
Mayank Grover912eaa62017-10-26 12:08:53 +05304158 if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05304159 (!strncmp(arg, "system", 6)) &&
4160 !device.verity_mode)
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07004161 // reset dm_verity mode to enforcing
4162 device.verity_mode = 1;
4163 write_device_info(&device);
Parth Dixitddbc7352015-10-18 03:13:31 +05304164#endif
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07004165
Ajay Dudani5c761132011-04-07 20:19:04 -07004166 return;
4167}
4168
Tanya Brokhman84eaaf62015-02-24 21:57:06 +02004169void cmd_updatevol(const char *vol_name, void *data, unsigned sz)
4170{
4171 struct ptentry *sys_ptn;
4172 struct ptable *ptable;
4173
4174 ptable = flash_get_ptable();
4175 if (ptable == NULL) {
4176 fastboot_fail("partition table doesn't exist");
4177 return;
4178 }
4179
4180 sys_ptn = ptable_find(ptable, "system");
4181 if (sys_ptn == NULL) {
4182 fastboot_fail("system partition not found");
4183 return;
4184 }
4185
4186 sz = ROUND_TO_PAGE(sz, page_mask);
4187 if (update_ubi_vol(sys_ptn, vol_name, data, sz))
4188 fastboot_fail("update_ubi_vol failed");
4189 else
4190 fastboot_okay("");
4191}
4192
Sridhar Parasurame94e8152014-10-24 14:06:03 -07004193void cmd_flash_nand(const char *arg, void *data, unsigned sz)
Dima Zavin214cc642009-01-26 11:16:21 -08004194{
4195 struct ptentry *ptn;
4196 struct ptable *ptable;
4197 unsigned extra = 0;
Gaurav Nebhwanid9730712016-05-06 14:28:29 +05304198 uint64_t partition_size = 0;
Vijay Kumar Pendotic6f22a72017-05-19 22:44:34 +05304199 unsigned bytes_to_round_page = 0;
4200 unsigned rounded_size = 0;
Dima Zavin214cc642009-01-26 11:16:21 -08004201
Kishor PK38ed93d2017-04-25 14:19:26 +05304202 if((uintptr_t)data > (UINT_MAX - sz)) {
4203 fastboot_fail("Cannot flash: image header corrupt");
4204 return;
4205 }
4206
Dima Zavin214cc642009-01-26 11:16:21 -08004207 ptable = flash_get_ptable();
4208 if (ptable == NULL) {
4209 fastboot_fail("partition table doesn't exist");
4210 return;
4211 }
4212
4213 ptn = ptable_find(ptable, arg);
4214 if (ptn == NULL) {
Tanya Brokhman84eaaf62015-02-24 21:57:06 +02004215 dprintf(INFO, "unknown partition name (%s). Trying updatevol\n",
4216 arg);
4217 cmd_updatevol(arg, data, sz);
Dima Zavin214cc642009-01-26 11:16:21 -08004218 return;
4219 }
4220
Monika Singh292b3e92018-03-17 22:40:23 +05304221 if (!strncmp(arg, "avb_custom_key", strlen("avb_custom_key"))) {
4222 dprintf(INFO, "flashing avb_custom_key\n");
4223 if (store_userkey(data, sz)) {
4224 fastboot_fail("Flashing avb_custom_key failed");
4225 } else {
4226 fastboot_okay("");
4227 }
4228 return;
4229 }
4230
Dima Zavin214cc642009-01-26 11:16:21 -08004231 if (!strcmp(ptn->name, "boot") || !strcmp(ptn->name, "recovery")) {
Kishor PK38ed93d2017-04-25 14:19:26 +05304232 if((sz > BOOT_MAGIC_SIZE) && (!memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE))) {
4233 dprintf(INFO, "Verified the BOOT_MAGIC in image header \n");
4234 } else {
4235 fastboot_fail("Image is not a boot image");
Dima Zavin214cc642009-01-26 11:16:21 -08004236 return;
4237 }
4238 }
4239
Amol Jadi5c61a952012-05-04 17:05:35 -07004240 if (!strcmp(ptn->name, "system")
Deepa Dinamani13e32c42012-03-12 14:34:17 -07004241 || !strcmp(ptn->name, "userdata")
4242 || !strcmp(ptn->name, "persist")
Sundarajan Srinivasanb063a852013-11-19 14:02:27 -08004243 || !strcmp(ptn->name, "recoveryfs")
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -08004244 || !strcmp(ptn->name, "modem"))
Tanya Brokhman1c94f1a2015-02-15 09:05:03 +02004245 extra = 1;
Kishor PK38ed93d2017-04-25 14:19:26 +05304246 else {
Vijay Kumar Pendotic6f22a72017-05-19 22:44:34 +05304247 rounded_size = ROUNDUP(sz, page_size);
4248 bytes_to_round_page = rounded_size - sz;
4249 if (bytes_to_round_page) {
4250 if (((uintptr_t)data + sz ) > (UINT_MAX - bytes_to_round_page)) {
4251 fastboot_fail("Integer overflow detected");
4252 return;
4253 }
4254 if (((uintptr_t)data + sz + bytes_to_round_page) >
4255 ((uintptr_t)target_get_scratch_address() + target_get_max_flash_size())) {
4256 fastboot_fail("Buffer size is not aligned to page_size");
4257 return;
4258 }
4259 else {
4260 memset(data + sz, 0, bytes_to_round_page);
4261 sz = rounded_size;
4262 }
Kishor PK38ed93d2017-04-25 14:19:26 +05304263 }
Gaurav Nebhwanid9730712016-05-06 14:28:29 +05304264 }
4265
Kishor PK38ed93d2017-04-25 14:19:26 +05304266 /*Checking partition_size for the possible integer overflow */
4267 partition_size = validate_partition_size(ptn);
4268
Gaurav Nebhwanid9730712016-05-06 14:28:29 +05304269 if (sz > partition_size) {
4270 fastboot_fail("Image size too large");
4271 return;
4272 }
4273
Dima Zavin214cc642009-01-26 11:16:21 -08004274 dprintf(INFO, "writing %d bytes to '%s'\n", sz, ptn->name);
Mayank Grover6cde9352017-06-06 18:45:23 +05304275 if ((sz > UBI_EC_HDR_SIZE) &&
4276 (!memcmp((void *)data, UBI_MAGIC, UBI_MAGIC_SIZE))) {
Tanya Brokhman1c94f1a2015-02-15 09:05:03 +02004277 if (flash_ubi_img(ptn, data, sz)) {
4278 fastboot_fail("flash write failure");
4279 return;
4280 }
4281 } else {
4282 if (flash_write(ptn, extra, data, sz)) {
4283 fastboot_fail("flash write failure");
4284 return;
4285 }
Dima Zavin214cc642009-01-26 11:16:21 -08004286 }
4287 dprintf(INFO, "partition '%s' updated\n", ptn->name);
4288 fastboot_okay("");
4289}
4290
Kishor PK38ed93d2017-04-25 14:19:26 +05304291
4292static inline uint64_t validate_partition_size(struct ptentry *ptn)
4293{
4294 if (ptn->length && flash_num_pages_per_blk() && page_size) {
4295 if ((ptn->length < ( UINT_MAX / flash_num_pages_per_blk())) && ((ptn->length * flash_num_pages_per_blk()) < ( UINT_MAX / page_size))) {
4296 return ptn->length * flash_num_pages_per_blk() * page_size;
4297 }
4298 }
4299 return 0;
4300}
4301
4302
Sridhar Parasurame94e8152014-10-24 14:06:03 -07004303void cmd_flash(const char *arg, void *data, unsigned sz)
4304{
4305 if(target_is_emmc_boot())
4306 cmd_flash_mmc(arg, data, sz);
4307 else
4308 cmd_flash_nand(arg, data, sz);
4309}
4310
Dima Zavin214cc642009-01-26 11:16:21 -08004311void cmd_continue(const char *arg, void *data, unsigned sz)
4312{
4313 fastboot_okay("");
Amol Jadi7c4316c2013-10-07 14:19:26 -07004314 fastboot_stop();
Shashank Mittald3e54dd2014-08-28 15:24:02 -07004315
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004316 if (target_is_emmc_boot())
4317 {
lijuanga40d6302015-07-20 20:10:13 +08004318#if FBCON_DISPLAY_MSG
lijuangde34d502016-02-26 16:04:50 +08004319 /* Exit keys' detection thread firstly */
4320 exit_menu_keys_detection();
lijuanga40d6302015-07-20 20:10:13 +08004321#endif
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004322 boot_linux_from_mmc();
4323 }
4324 else
4325 {
4326 boot_linux_from_flash();
4327 }
Dima Zavin214cc642009-01-26 11:16:21 -08004328}
4329
Chandan Uddaraju94183c02010-01-15 15:13:59 -08004330void cmd_reboot(const char *arg, void *data, unsigned sz)
4331{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004332 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08004333 fastboot_okay("");
4334 reboot_device(0);
4335}
4336
Mayank Grover351a75e2017-05-30 20:06:08 +05304337void cmd_set_active(const char *arg, void *data, unsigned sz)
4338{
Mayank Grover6ccc1c92017-07-04 17:36:46 +05304339 char *p, *sp = NULL;
Mayank Grover351a75e2017-05-30 20:06:08 +05304340 unsigned i,current_active_slot;
4341 const char *current_slot_suffix;
4342
4343 if (!partition_multislot_is_supported())
4344 {
4345 fastboot_fail("Command not supported");
4346 return;
4347 }
4348
4349 if (arg)
4350 {
Mayank Grover6ccc1c92017-07-04 17:36:46 +05304351 p = strtok_r((char *)arg, ":", &sp);
Mayank Grover5eb5f692017-07-19 20:16:04 +05304352 if (p)
Mayank Grover351a75e2017-05-30 20:06:08 +05304353 {
4354 current_active_slot = partition_find_active_slot();
4355
4356 /* Check if trying to make curent slot active */
4357 current_slot_suffix = SUFFIX_SLOT(current_active_slot);
Mayank Grover6ccc1c92017-07-04 17:36:46 +05304358 current_slot_suffix = strtok_r((char *)current_slot_suffix,
4359 (char *)suffix_delimiter, &sp);
4360
Mayank Grover5eb5f692017-07-19 20:16:04 +05304361 if (current_slot_suffix &&
4362 !strncmp(p, current_slot_suffix, strlen(current_slot_suffix)))
Mayank Grover351a75e2017-05-30 20:06:08 +05304363 {
4364 fastboot_okay("Slot already set active");
4365 return;
4366 }
4367 else
4368 {
4369 for (i = 0; i < AB_SUPPORTED_SLOTS; i++)
4370 {
4371 current_slot_suffix = SUFFIX_SLOT(i);
Mayank Grover6ccc1c92017-07-04 17:36:46 +05304372 current_slot_suffix = strtok_r((char *)current_slot_suffix,
4373 (char *)suffix_delimiter, &sp);
Mayank Grover5eb5f692017-07-19 20:16:04 +05304374 if (current_slot_suffix &&
4375 !strncmp(p, current_slot_suffix, strlen(current_slot_suffix)))
Mayank Grover351a75e2017-05-30 20:06:08 +05304376 {
4377 partition_switch_slots(current_active_slot, i);
4378 publish_getvar_multislot_vars();
4379 fastboot_okay("");
4380 return;
4381 }
4382 }
4383 }
4384 }
4385 }
4386 fastboot_fail("Invalid slot suffix.");
4387 return;
4388}
4389
Chandan Uddaraju94183c02010-01-15 15:13:59 -08004390void cmd_reboot_bootloader(const char *arg, void *data, unsigned sz)
4391{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004392 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08004393 fastboot_okay("");
4394 reboot_device(FASTBOOT_MODE);
4395}
4396
Ameya Thakur11cf1a62013-08-05 12:44:48 -07004397void cmd_oem_enable_charger_screen(const char *arg, void *data, unsigned size)
4398{
4399 dprintf(INFO, "Enabling charger screen check\n");
4400 device.charger_screen_enabled = 1;
4401 write_device_info(&device);
4402 fastboot_okay("");
4403}
4404
4405void cmd_oem_disable_charger_screen(const char *arg, void *data, unsigned size)
4406{
4407 dprintf(INFO, "Disabling charger screen check\n");
4408 device.charger_screen_enabled = 0;
4409 write_device_info(&device);
4410 fastboot_okay("");
4411}
4412
lijuanga25d8bb2015-09-16 13:11:52 +08004413void cmd_oem_off_mode_charger(const char *arg, void *data, unsigned size)
4414{
4415 char *p = NULL;
4416 const char *delim = " \t\n\r";
Parth Dixit407f15b2016-01-07 14:47:37 +05304417 char *sp;
lijuanga25d8bb2015-09-16 13:11:52 +08004418
4419 if (arg) {
Parth Dixit407f15b2016-01-07 14:47:37 +05304420 p = strtok_r((char *)arg, delim, &sp);
lijuanga25d8bb2015-09-16 13:11:52 +08004421 if (p) {
4422 if (!strncmp(p, "0", 1)) {
4423 device.charger_screen_enabled = 0;
4424 } else if (!strncmp(p, "1", 1)) {
4425 device.charger_screen_enabled = 1;
4426 }
4427 }
4428 }
4429
4430 /* update charger_screen_enabled value for getvar
4431 * command
4432 */
4433 snprintf(charger_screen_enabled, MAX_RSP_SIZE, "%d",
4434 device.charger_screen_enabled);
4435
4436 write_device_info(&device);
4437 fastboot_okay("");
4438}
4439
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05304440void cmd_oem_select_display_panel(const char *arg, void *data, unsigned size)
4441{
4442 dprintf(INFO, "Selecting display panel %s\n", arg);
4443 if (arg)
4444 strlcpy(device.display_panel, arg,
4445 sizeof(device.display_panel));
4446 write_device_info(&device);
4447 fastboot_okay("");
4448}
4449
Shashank Mittal162244e2011-08-08 19:01:25 -07004450void cmd_oem_unlock(const char *arg, void *data, unsigned sz)
4451{
lijuang4ece1e72015-08-14 21:02:36 +08004452 set_device_unlock(UNLOCK, TRUE);
vijay kumarc65876c2015-04-24 13:29:16 +05304453}
4454
4455void cmd_oem_unlock_go(const char *arg, void *data, unsigned sz)
4456{
lijuang4ece1e72015-08-14 21:02:36 +08004457 if(!device.is_unlocked) {
vijay kumarc65876c2015-04-24 13:29:16 +05304458 if(!is_allow_unlock) {
4459 fastboot_fail("oem unlock is not allowed");
4460 return;
4461 }
4462
lijuang4ece1e72015-08-14 21:02:36 +08004463 set_device_unlock_value(UNLOCK, TRUE);
vijay kumarc65876c2015-04-24 13:29:16 +05304464
lijuang4ece1e72015-08-14 21:02:36 +08004465 /* wipe data */
vijay kumarc65876c2015-04-24 13:29:16 +05304466 struct recovery_message msg;
Kishor PK60a68212017-05-08 16:55:57 +05304467 memset(&msg, 0, sizeof(msg));
vijay kumarc65876c2015-04-24 13:29:16 +05304468 snprintf(msg.recovery, sizeof(msg.recovery), "recovery\n--wipe_data");
4469 write_misc(0, &msg, sizeof(msg));
4470
4471 fastboot_okay("");
4472 reboot_device(RECOVERY_MODE);
Shashank Mittald3e54dd2014-08-28 15:24:02 -07004473 }
4474 fastboot_okay("");
4475}
4476
Channagoud Kadabiad259832015-05-29 11:14:17 -07004477static int aboot_frp_unlock(char *pname, void *data, unsigned sz)
4478{
Mayank Grover70f8b0e2017-05-17 11:49:00 +05304479 int ret=1;
4480 bool authentication_success=false;
Channagoud Kadabiad259832015-05-29 11:14:17 -07004481
Mayank Grover70f8b0e2017-05-17 11:49:00 +05304482 /*
4483 Authentication method not implemented.
4484
4485 OEM to implement, authentication system which on successful validataion,
4486 calls write_allow_oem_unlock() with is_allow_unlock.
4487 */
4488#if 0
4489 authentication_success = oem_specific_auth_mthd();
4490#endif
4491
4492 if (authentication_success)
Channagoud Kadabiad259832015-05-29 11:14:17 -07004493 {
Mayank Grover70f8b0e2017-05-17 11:49:00 +05304494 is_allow_unlock = true;
4495 write_allow_oem_unlock(is_allow_unlock);
4496 ret = 0;
Channagoud Kadabiad259832015-05-29 11:14:17 -07004497 }
4498 return ret;
4499}
4500
Shashank Mittald3e54dd2014-08-28 15:24:02 -07004501void cmd_oem_lock(const char *arg, void *data, unsigned sz)
4502{
lijuang4ece1e72015-08-14 21:02:36 +08004503 set_device_unlock(UNLOCK, FALSE);
Shashank Mittal162244e2011-08-08 19:01:25 -07004504}
4505
Shashank Mittala0032282011-08-26 14:50:11 -07004506void cmd_oem_devinfo(const char *arg, void *data, unsigned sz)
4507{
lijuang511a2b52015-08-14 20:50:51 +08004508 char response[MAX_RSP_SIZE];
Ameya Thakur11cf1a62013-08-05 12:44:48 -07004509 snprintf(response, sizeof(response), "\tDevice tampered: %s", (device.is_tampered ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07004510 fastboot_info(response);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07004511 snprintf(response, sizeof(response), "\tDevice unlocked: %s", (device.is_unlocked ? "true" : "false"));
4512 fastboot_info(response);
Monika Singh292b3e92018-03-17 22:40:23 +05304513#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover912eaa62017-10-26 12:08:53 +05304514 if (VB_M <= target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +05304515 {
4516 snprintf(response, sizeof(response), "\tDevice critical unlocked: %s",
4517 (device.is_unlock_critical ? "true" : "false"));
4518 fastboot_info(response);
4519 }
Parth Dixitddbc7352015-10-18 03:13:31 +05304520#endif
Ameya Thakur11cf1a62013-08-05 12:44:48 -07004521 snprintf(response, sizeof(response), "\tCharger screen enabled: %s", (device.charger_screen_enabled ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07004522 fastboot_info(response);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05304523 snprintf(response, sizeof(response), "\tDisplay panel: %s", (device.display_panel));
4524 fastboot_info(response);
Shashank Mittala0032282011-08-26 14:50:11 -07004525 fastboot_okay("");
4526}
4527
lijuang511a2b52015-08-14 20:50:51 +08004528void cmd_flashing_get_unlock_ability(const char *arg, void *data, unsigned sz)
4529{
4530 char response[MAX_RSP_SIZE];
4531 snprintf(response, sizeof(response), "\tget_unlock_ability: %d", is_allow_unlock);
4532 fastboot_info(response);
4533 fastboot_okay("");
4534}
4535
4536void cmd_flashing_lock_critical(const char *arg, void *data, unsigned sz)
4537{
lijuang4ece1e72015-08-14 21:02:36 +08004538 set_device_unlock(UNLOCK_CRITICAL, FALSE);
lijuang511a2b52015-08-14 20:50:51 +08004539}
4540
4541void cmd_flashing_unlock_critical(const char *arg, void *data, unsigned sz)
4542{
lijuang4ece1e72015-08-14 21:02:36 +08004543 set_device_unlock(UNLOCK_CRITICAL, TRUE);
lijuang511a2b52015-08-14 20:50:51 +08004544}
4545
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07004546void cmd_preflash(const char *arg, void *data, unsigned sz)
4547{
4548 fastboot_okay("");
4549}
4550
Mao Flynn7b379f32015-04-20 00:28:30 +08004551static uint8_t logo_header[LOGO_IMG_HEADER_SIZE];
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304552
Mao Flynn7b379f32015-04-20 00:28:30 +08004553int splash_screen_check_header(logo_img_header *header)
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304554{
Mao Flynn7b379f32015-04-20 00:28:30 +08004555 if (memcmp(header->magic, LOGO_IMG_MAGIC, 8))
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304556 return -1;
Mao Flynn7b379f32015-04-20 00:28:30 +08004557 if (header->width == 0 || header->height == 0)
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304558 return -1;
4559 return 0;
4560}
4561
Mao Flynn7b379f32015-04-20 00:28:30 +08004562int splash_screen_flash()
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07004563{
4564 struct ptentry *ptn;
4565 struct ptable *ptable;
Mao Flynn7b379f32015-04-20 00:28:30 +08004566 struct logo_img_header *header;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07004567 struct fbcon_config *fb_display = NULL;
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08004568
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304569 ptable = flash_get_ptable();
4570 if (ptable == NULL) {
Mao Flynn7b379f32015-04-20 00:28:30 +08004571 dprintf(CRITICAL, "ERROR: Partition table not found\n");
4572 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304573 }
Mao Flynn7b379f32015-04-20 00:28:30 +08004574
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304575 ptn = ptable_find(ptable, "splash");
4576 if (ptn == NULL) {
4577 dprintf(CRITICAL, "ERROR: splash Partition not found\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08004578 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304579 }
Mao Flynn7b379f32015-04-20 00:28:30 +08004580 if (flash_read(ptn, 0, (void *)logo_header, LOGO_IMG_HEADER_SIZE)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304581 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08004582 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304583 }
4584
Mao Flynn7b379f32015-04-20 00:28:30 +08004585 header = (struct logo_img_header *)logo_header;
4586 if (splash_screen_check_header(header)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304587 dprintf(CRITICAL, "ERROR: Boot image header invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08004588 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304589 }
4590
4591 fb_display = fbcon_display();
4592 if (fb_display) {
raghavendra ambadas554f4fd2018-10-09 15:50:40 +05304593 if (header->type && (header->blocks != 0) &&
4594 (UINT_MAX >= header->blocks * 512) &&
4595 ((header->blocks * 512) <= (fb_display->width *
4596 fb_display->height * (fb_display->bpp / 8)))) {
4597 /* RLE24 compressed data */
Mao Flynn7b379f32015-04-20 00:28:30 +08004598 uint8_t *base = (uint8_t *) fb_display->base + LOGO_IMG_OFFSET;
4599
4600 /* if the logo is full-screen size, remove "fbcon_clear()" */
4601 if ((header->width != fb_display->width)
4602 || (header->height != fb_display->height))
4603 fbcon_clear();
4604
4605 if (flash_read(ptn + LOGO_IMG_HEADER_SIZE, 0,
4606 (uint32_t *)base,
4607 (header->blocks * 512))) {
4608 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
4609 return -1;
4610 }
4611 fbcon_extract_to_screen(header, base);
4612 return 0;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07004613 }
Mao Flynn7b379f32015-04-20 00:28:30 +08004614
Veera Sundaram Sankaranc33416f2015-09-28 10:25:55 -07004615 if ((header->width > fb_display->width) || (header->height > fb_display->height)) {
4616 dprintf(CRITICAL, "Logo config greater than fb config. Fall back default logo\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08004617 return -1;
4618 }
4619
Vineet Bajaj99291ed2014-09-09 12:29:46 +05304620 uint8_t *base = (uint8_t *) fb_display->base;
Sachin Bhayare619e9e42017-05-15 13:10:31 +05304621 uint32_t fb_size = ROUNDUP(fb_display->width *
4622 fb_display->height *
4623 (fb_display->bpp / 8), 4096);
4624 uint32_t splash_size = ((((header->width * header->height *
4625 fb_display->bpp/8) + 511) >> 9) << 9);
4626
4627 if (splash_size > fb_size) {
4628 dprintf(CRITICAL, "ERROR: Splash image size invalid\n");
4629 return -1;
4630 }
4631
Mao Flynn7b379f32015-04-20 00:28:30 +08004632 if (flash_read(ptn + LOGO_IMG_HEADER_SIZE, 0,
4633 (uint32_t *)base,
4634 ((((header->width * header->height * fb_display->bpp/8) + 511) >> 9) << 9))) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304635 fbcon_clear();
Vineet Bajaj99291ed2014-09-09 12:29:46 +05304636 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08004637 return -1;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07004638 }
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304639 }
4640
Mao Flynn7b379f32015-04-20 00:28:30 +08004641 return 0;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304642}
4643
Mao Flynn7b379f32015-04-20 00:28:30 +08004644int splash_screen_mmc()
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304645{
4646 int index = INVALID_PTN;
4647 unsigned long long ptn = 0;
4648 struct fbcon_config *fb_display = NULL;
Mao Flynn7b379f32015-04-20 00:28:30 +08004649 struct logo_img_header *header;
Mao Flynn1893a7f2015-06-03 12:03:36 +08004650 uint32_t blocksize, realsize, readsize;
4651 uint8_t *base;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304652
4653 index = partition_get_index("splash");
4654 if (index == 0) {
4655 dprintf(CRITICAL, "ERROR: splash Partition table not found\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08004656 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304657 }
4658
4659 ptn = partition_get_offset(index);
4660 if (ptn == 0) {
4661 dprintf(CRITICAL, "ERROR: splash Partition invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08004662 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304663 }
4664
Mao Flynn1893a7f2015-06-03 12:03:36 +08004665 mmc_set_lun(partition_get_lun(index));
4666
4667 blocksize = mmc_get_device_blocksize();
4668 if (blocksize == 0) {
4669 dprintf(CRITICAL, "ERROR:splash Partition invalid blocksize\n");
4670 return -1;
4671 }
4672
4673 fb_display = fbcon_display();
Channagoud Kadabidaf10a62015-07-22 11:51:55 -07004674 if (!fb_display)
4675 {
4676 dprintf(CRITICAL, "ERROR: fb config is not allocated\n");
4677 return -1;
4678 }
4679
Mao Flynn1893a7f2015-06-03 12:03:36 +08004680 base = (uint8_t *) fb_display->base;
4681
Kishor PKee5c0a32018-03-06 16:49:46 +05304682 if (mmc_read(ptn + PLL_CODES_OFFSET, (uint32_t *)(base + LOGO_IMG_OFFSET), blocksize)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304683 dprintf(CRITICAL, "ERROR: Cannot read splash image header\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08004684 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304685 }
4686
Mao Flynn1893a7f2015-06-03 12:03:36 +08004687 header = (struct logo_img_header *)(base + LOGO_IMG_OFFSET);
Mao Flynn7b379f32015-04-20 00:28:30 +08004688 if (splash_screen_check_header(header)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304689 dprintf(CRITICAL, "ERROR: Splash image header invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08004690 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304691 }
4692
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304693 if (fb_display) {
raghavendra ambadas554f4fd2018-10-09 15:50:40 +05304694 if (header->type && (header->blocks != 0) &&
4695 (UINT_MAX >= header->blocks * 512 + LOGO_IMG_HEADER_SIZE) &&
4696 ((header->blocks * 512) <= (fb_display->width *
4697 fb_display->height * (fb_display->bpp / 8)))) {
4698 /* 1 RLE24 compressed data */
Mao Flynn1893a7f2015-06-03 12:03:36 +08004699 base += LOGO_IMG_OFFSET;
Mao Flynn7b379f32015-04-20 00:28:30 +08004700
Mao Flynn1893a7f2015-06-03 12:03:36 +08004701 realsize = header->blocks * 512;
4702 readsize = ROUNDUP((realsize + LOGO_IMG_HEADER_SIZE), blocksize) - blocksize;
4703
4704 /* if the logo is not full-screen size, clean screen */
Mao Flynn7b379f32015-04-20 00:28:30 +08004705 if ((header->width != fb_display->width)
4706 || (header->height != fb_display->height))
4707 fbcon_clear();
4708
Sachin Bhayare619e9e42017-05-15 13:10:31 +05304709 uint32_t fb_size = ROUNDUP(fb_display->width *
4710 fb_display->height *
4711 (fb_display->bpp / 8), 4096);
4712
4713 if (readsize > fb_size) {
4714 dprintf(CRITICAL, "ERROR: Splash image size invalid\n");
4715 return -1;
4716 }
4717
Kishor PKee5c0a32018-03-06 16:49:46 +05304718 if (mmc_read(ptn + PLL_CODES_OFFSET + blocksize, (uint32_t *)(base + blocksize), readsize)) {
Mao Flynn7b379f32015-04-20 00:28:30 +08004719 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
4720 return -1;
4721 }
Mao Flynn7b379f32015-04-20 00:28:30 +08004722
Mao Flynn1893a7f2015-06-03 12:03:36 +08004723 fbcon_extract_to_screen(header, (base + LOGO_IMG_HEADER_SIZE));
4724 } else { /* 2 Raw BGR data */
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304725
Veera Sundaram Sankaranc33416f2015-09-28 10:25:55 -07004726 if ((header->width > fb_display->width) || (header->height > fb_display->height)) {
4727 dprintf(CRITICAL, "Logo config greater than fb config. Fall back default logo\n");
Mao Flynn1893a7f2015-06-03 12:03:36 +08004728 return -1;
4729 }
4730
4731 realsize = header->width * header->height * fb_display->bpp / 8;
4732 readsize = ROUNDUP((realsize + LOGO_IMG_HEADER_SIZE), blocksize) - blocksize;
4733
4734 if (blocksize == LOGO_IMG_HEADER_SIZE) { /* read the content directly */
Kishor PKee5c0a32018-03-06 16:49:46 +05304735 if (mmc_read((ptn + PLL_CODES_OFFSET + LOGO_IMG_HEADER_SIZE), (uint32_t *)base, readsize)) {
Mao Flynn1893a7f2015-06-03 12:03:36 +08004736 fbcon_clear();
4737 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
4738 return -1;
4739 }
4740 } else {
Kishor PKee5c0a32018-03-06 16:49:46 +05304741 if (mmc_read(ptn + PLL_CODES_OFFSET + blocksize ,
Mao Flynn1893a7f2015-06-03 12:03:36 +08004742 (uint32_t *)(base + LOGO_IMG_OFFSET + blocksize), readsize)) {
4743 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
4744 return -1;
4745 }
4746 memmove(base, (base + LOGO_IMG_OFFSET + LOGO_IMG_HEADER_SIZE), realsize);
4747 }
4748 }
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304749 }
4750
Mao Flynn7b379f32015-04-20 00:28:30 +08004751 return 0;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304752}
4753
Mao Flynn7b379f32015-04-20 00:28:30 +08004754int fetch_image_from_partition()
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304755{
4756 if (target_is_emmc_boot()) {
4757 return splash_screen_mmc();
4758 } else {
4759 return splash_screen_flash();
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07004760 }
4761}
4762
Mayank Grover351a75e2017-05-30 20:06:08 +05304763void publish_getvar_multislot_vars()
4764{
4765 int i,count;
4766 static bool published = false;
4767 static char slot_count[MAX_RSP_SIZE];
4768 static struct ab_slot_info slot_info[AB_SUPPORTED_SLOTS];
4769 static char active_slot_suffix[MAX_RSP_SIZE];
4770 static char has_slot_pname[NUM_PARTITIONS][MAX_GET_VAR_NAME_SIZE];
4771 static char has_slot_reply[NUM_PARTITIONS][MAX_RSP_SIZE];
4772 const char *tmp;
4773 char tmpbuff[MAX_GET_VAR_NAME_SIZE];
Mayank Grover2bd84bb2017-06-20 11:06:07 +05304774 signed active_slt;
Mayank Grover351a75e2017-05-30 20:06:08 +05304775
4776 if (!published)
4777 {
4778 /* Update slot meta info */
4779 count = partition_fill_partition_meta(has_slot_pname, has_slot_reply,
4780 partition_get_partition_count());
4781 for(i=0; i<count; i++)
4782 {
4783 memset(tmpbuff, 0, MAX_GET_VAR_NAME_SIZE);
Mayank Grover2bd84bb2017-06-20 11:06:07 +05304784 snprintf(tmpbuff, MAX_GET_VAR_NAME_SIZE,"has-slot:%s",
4785 has_slot_pname[i]);
4786 strlcpy(has_slot_pname[i], tmpbuff, MAX_GET_VAR_NAME_SIZE);
Mayank Grover351a75e2017-05-30 20:06:08 +05304787 fastboot_publish(has_slot_pname[i], has_slot_reply[i]);
4788 }
4789
4790 for (i=0; i<AB_SUPPORTED_SLOTS; i++)
4791 {
4792 tmp = SUFFIX_SLOT(i);
Mayank Grover7b8a8f82017-10-27 13:07:59 +05304793 tmp++; // to remove "_" from slot_suffix.
Mayank Grover2bd84bb2017-06-20 11:06:07 +05304794 snprintf(slot_info[i].slot_is_unbootable, sizeof(slot_info[i].slot_is_unbootable),
4795 "slot-unbootable:%s", tmp);
4796 snprintf(slot_info[i].slot_is_active, sizeof(slot_info[i].slot_is_active),
4797 "slot-active:%s", tmp);
4798 snprintf(slot_info[i].slot_is_succesful, sizeof(slot_info[i].slot_is_succesful),
4799 "slot-success:%s", tmp);
4800 snprintf(slot_info[i].slot_retry_count, sizeof(slot_info[i].slot_retry_count),
4801 "slot-retry-count:%s", tmp);
Mayank Grover351a75e2017-05-30 20:06:08 +05304802 fastboot_publish(slot_info[i].slot_is_unbootable,
4803 slot_info[i].slot_is_unbootable_rsp);
4804 fastboot_publish(slot_info[i].slot_is_active,
4805 slot_info[i].slot_is_active_rsp);
4806 fastboot_publish(slot_info[i].slot_is_succesful,
4807 slot_info[i].slot_is_succesful_rsp);
4808 fastboot_publish(slot_info[i].slot_retry_count,
4809 slot_info[i].slot_retry_count_rsp);
4810 }
4811 fastboot_publish("current-slot", active_slot_suffix);
4812 snprintf(slot_count, sizeof(slot_count),"%d", AB_SUPPORTED_SLOTS);
4813 fastboot_publish("slot-count", slot_count);
4814 published = true;
4815 }
4816
Mayank Grover2bd84bb2017-06-20 11:06:07 +05304817 active_slt = partition_find_active_slot();
4818 if (active_slt != INVALID)
Mayank Grover7b8a8f82017-10-27 13:07:59 +05304819 {
4820 tmp = SUFFIX_SLOT(active_slt);
4821 tmp++; // to remove "_" from slot_suffix.
4822 snprintf(active_slot_suffix, sizeof(active_slot_suffix), "%s", tmp);
4823 }
Mayank Grover2bd84bb2017-06-20 11:06:07 +05304824 else
4825 strlcpy(active_slot_suffix, "INVALID", sizeof(active_slot_suffix));
4826
Mayank Grover351a75e2017-05-30 20:06:08 +05304827 /* Update partition meta information */
4828 partition_fill_slot_meta(slot_info);
4829 return;
4830}
4831
lijuang4ece1e72015-08-14 21:02:36 +08004832void get_product_name(unsigned char *buf)
4833{
4834 snprintf((char*)buf, MAX_RSP_SIZE, "%s", TARGET(BOARD));
4835 return;
4836}
4837
Mayank Grover7b8a8f82017-10-27 13:07:59 +05304838#if PRODUCT_IOT
4839void get_bootloader_version_iot(unsigned char *buf)
4840{
4841 if (buf != NULL)
4842 {
4843 strlcpy(buf, TARGET(BOARD), MAX_VERSION_LEN);
4844 strlcat(buf, "-", MAX_VERSION_LEN);
4845 strlcat(buf, PRODUCT_IOT_VERSION, MAX_VERSION_LEN);
4846 }
4847 return;
4848}
4849#endif
4850
lijuang4ece1e72015-08-14 21:02:36 +08004851void get_bootloader_version(unsigned char *buf)
4852{
4853 snprintf((char*)buf, MAX_RSP_SIZE, "%s", device.bootloader_version);
4854 return;
4855}
4856
4857void get_baseband_version(unsigned char *buf)
4858{
4859 snprintf((char*)buf, MAX_RSP_SIZE, "%s", device.radio_version);
4860 return;
4861}
4862
Monika Singh32a09f72018-03-14 13:08:29 +05304863bool is_device_locked_critical()
4864{
4865 return device.is_unlock_critical ? false:true;
4866}
4867
lijuang4ece1e72015-08-14 21:02:36 +08004868bool is_device_locked()
4869{
4870 return device.is_unlocked ? false:true;
4871}
4872
Monika Singh32a09f72018-03-14 13:08:29 +05304873bool is_verity_enforcing()
4874{
4875 return device.verity_mode ? true:false;
4876}
4877
Amol Jadi5edf3552013-07-23 14:15:34 -07004878/* register commands and variables for fastboot */
4879void aboot_fastboot_register_commands(void)
4880{
Sridhar Parasurame94e8152014-10-24 14:06:03 -07004881 int i;
lijuangf16461c2015-08-03 17:09:34 +08004882 char hw_platform_buf[MAX_RSP_SIZE];
Amol Jadi5edf3552013-07-23 14:15:34 -07004883
Sridhar Parasurame94e8152014-10-24 14:06:03 -07004884 struct fastboot_cmd_desc cmd_list[] = {
lijuang511a2b52015-08-14 20:50:51 +08004885 /* By default the enabled list is empty. */
4886 {"", NULL},
4887 /* move commands enclosed within the below ifndef to here
4888 * if they need to be enabled in user build.
4889 */
Sridhar Parasurame94e8152014-10-24 14:06:03 -07004890#ifndef DISABLE_FASTBOOT_CMDS
lijuang511a2b52015-08-14 20:50:51 +08004891 /* Register the following commands only for non-user builds */
4892 {"flash:", cmd_flash},
4893 {"erase:", cmd_erase},
4894 {"boot", cmd_boot},
4895 {"continue", cmd_continue},
4896 {"reboot", cmd_reboot},
4897 {"reboot-bootloader", cmd_reboot_bootloader},
4898 {"oem unlock", cmd_oem_unlock},
4899 {"oem unlock-go", cmd_oem_unlock_go},
4900 {"oem lock", cmd_oem_lock},
4901 {"flashing unlock", cmd_oem_unlock},
4902 {"flashing lock", cmd_oem_lock},
4903 {"flashing lock_critical", cmd_flashing_lock_critical},
4904 {"flashing unlock_critical", cmd_flashing_unlock_critical},
4905 {"flashing get_unlock_ability", cmd_flashing_get_unlock_ability},
4906 {"oem device-info", cmd_oem_devinfo},
4907 {"preflash", cmd_preflash},
4908 {"oem enable-charger-screen", cmd_oem_enable_charger_screen},
4909 {"oem disable-charger-screen", cmd_oem_disable_charger_screen},
lijuanga25d8bb2015-09-16 13:11:52 +08004910 {"oem off-mode-charge", cmd_oem_off_mode_charger},
lijuang511a2b52015-08-14 20:50:51 +08004911 {"oem select-display-panel", cmd_oem_select_display_panel},
Mayank Grover6ccc1c92017-07-04 17:36:46 +05304912 {"set_active",cmd_set_active},
Channagoud Kadabi736c4962015-08-21 11:56:52 -07004913#if UNITTEST_FW_SUPPORT
Channagoud Kadabid5ddf482015-09-28 10:31:35 -07004914 {"oem run-tests", cmd_oem_runtests},
Channagoud Kadabi736c4962015-08-21 11:56:52 -07004915#endif
Sridhar Parasurame94e8152014-10-24 14:06:03 -07004916#endif
lijuang511a2b52015-08-14 20:50:51 +08004917 };
Sridhar Parasurame94e8152014-10-24 14:06:03 -07004918
4919 int fastboot_cmds_count = sizeof(cmd_list)/sizeof(cmd_list[0]);
4920 for (i = 1; i < fastboot_cmds_count; i++)
4921 fastboot_register(cmd_list[i].name,cmd_list[i].cb);
4922
Amol Jadi5edf3552013-07-23 14:15:34 -07004923 /* publish variables and their values */
4924 fastboot_publish("product", TARGET(BOARD));
4925 fastboot_publish("kernel", "lk");
4926 fastboot_publish("serialno", sn_buf);
4927
Mayank Grovera64dfbe2018-05-17 14:06:34 +05304928 /*publish hw-revision major(upper 16 bits) and minor(lower 16 bits)*/
4929 snprintf(soc_version_str, MAX_RSP_SIZE, "%x", board_soc_version());
4930 fastboot_publish("hw-revision", soc_version_str);
4931
Amol Jadi5edf3552013-07-23 14:15:34 -07004932 /*
4933 * partition info is supported only for emmc partitions
4934 * Calling this for NAND prints some error messages which
4935 * is harmless but misleading. Avoid calling this for NAND
4936 * devices.
4937 */
4938 if (target_is_emmc_boot())
Mayank Grover7b8a8f82017-10-27 13:07:59 +05304939 publish_getvar_partition_info(part_info, partition_get_partition_count());
Amol Jadi5edf3552013-07-23 14:15:34 -07004940
Mayank Grover351a75e2017-05-30 20:06:08 +05304941 if (partition_multislot_is_supported())
4942 publish_getvar_multislot_vars();
4943
Amol Jadi5edf3552013-07-23 14:15:34 -07004944 /* Max download size supported */
Ameya Thakur11cf1a62013-08-05 12:44:48 -07004945 snprintf(max_download_size, MAX_RSP_SIZE, "\t0x%x",
4946 target_get_max_flash_size());
Amol Jadi5edf3552013-07-23 14:15:34 -07004947 fastboot_publish("max-download-size", (const char *) max_download_size);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07004948 /* Is the charger screen check enabled */
4949 snprintf(charger_screen_enabled, MAX_RSP_SIZE, "%d",
4950 device.charger_screen_enabled);
4951 fastboot_publish("charger-screen-enabled",
4952 (const char *) charger_screen_enabled);
lijuanga25d8bb2015-09-16 13:11:52 +08004953 fastboot_publish("off-mode-charge", (const char *) charger_screen_enabled);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05304954 snprintf(panel_display_mode, MAX_RSP_SIZE, "%s",
4955 device.display_panel);
4956 fastboot_publish("display-panel",
4957 (const char *) panel_display_mode);
Mukesh Ojhabb6e7f32018-03-02 15:04:06 +05304958
4959 if (target_is_emmc_boot())
4960 {
4961 mmc_blocksize = mmc_get_device_blocksize();
4962 }
4963 else
4964 {
4965 mmc_blocksize = flash_block_size();
4966 }
4967 snprintf(block_size_string, MAX_RSP_SIZE, "0x%x", mmc_blocksize);
4968 fastboot_publish("erase-block-size", (const char *) block_size_string);
4969 fastboot_publish("logical-block-size", (const char *) block_size_string);
Mayank Grover7b8a8f82017-10-27 13:07:59 +05304970#if PRODUCT_IOT
4971 get_bootloader_version_iot(&bootloader_version_string);
4972 fastboot_publish("version-bootloader", (const char *) bootloader_version_string);
4973
4974 /* Version baseband is n/a for apq iot devices */
4975 fastboot_publish("version-baseband", "N/A");
Mayank Grover7b8a8f82017-10-27 13:07:59 +05304976#else
Channagoud Kadabiaafbbfc2015-03-04 17:52:56 -08004977 fastboot_publish("version-bootloader", (const char *) device.bootloader_version);
4978 fastboot_publish("version-baseband", (const char *) device.radio_version);
Mayank Grover7b8a8f82017-10-27 13:07:59 +05304979#endif
lijuangf16461c2015-08-03 17:09:34 +08004980 fastboot_publish("secure", is_secure_boot_enable()? "yes":"no");
Mayank Grover7b8a8f82017-10-27 13:07:59 +05304981 fastboot_publish("unlocked", device.is_unlocked ? "yes":"no");
lijuangf16461c2015-08-03 17:09:34 +08004982 smem_get_hw_platform_name((unsigned char *) hw_platform_buf, sizeof(hw_platform_buf));
4983 snprintf(get_variant, MAX_RSP_SIZE, "%s %s", hw_platform_buf,
4984 target_is_emmc_boot()? "eMMC":"UFS");
4985 fastboot_publish("variant", (const char *) get_variant);
lijuang65c5a822015-08-29 16:35:36 +08004986#if CHECK_BAT_VOLTAGE
lijuang102dfa92015-10-09 18:31:03 +08004987 update_battery_status();
lijuang65c5a822015-08-29 16:35:36 +08004988 fastboot_publish("battery-voltage", (const char *) battery_voltage);
lijuang102dfa92015-10-09 18:31:03 +08004989 fastboot_publish("battery-soc-ok", (const char *) battery_soc_ok);
lijuang65c5a822015-08-29 16:35:36 +08004990#endif
Amol Jadi5edf3552013-07-23 14:15:34 -07004991}
4992
Brian Swetland9c4c0752009-01-25 16:23:50 -08004993void aboot_init(const struct app_descriptor *app)
4994{
Shashank Mittal4f99a882010-02-01 13:58:50 -08004995 unsigned reboot_mode = 0;
Mayank Grover351a75e2017-05-30 20:06:08 +05304996 int boot_err_type = 0;
4997 int boot_slot = INVALID;
Chandan Uddarajubedca152010-06-02 23:05:15 -07004998
lijuang39831732016-01-08 17:49:02 +08004999 /* Initialise wdog to catch early lk crashes */
5000#if WDOG_SUPPORT
5001 msm_wdog_init();
5002#endif
5003
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07005004 /* Setup page size information for nv storage */
Shashank Mittald8c42bf2010-06-09 15:44:28 -07005005 if (target_is_emmc_boot())
5006 {
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07005007 page_size = mmc_page_size();
Shashank Mittald8c42bf2010-06-09 15:44:28 -07005008 page_mask = page_size - 1;
Gaurav Nebhwani4d2389c2016-03-17 21:10:05 +05305009 mmc_blocksize = mmc_get_device_blocksize();
5010 mmc_blocksize_mask = mmc_blocksize - 1;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07005011 }
5012 else
5013 {
5014 page_size = flash_page_size();
5015 page_mask = page_size - 1;
5016 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07005017 ASSERT((MEMBASE + MEMSIZE) > MEMBASE);
5018
Ameya Thakur11cf1a62013-08-05 12:44:48 -07005019 read_device_info(&device);
vijay kumarc65876c2015-04-24 13:29:16 +05305020 read_allow_oem_unlock(&device);
Shashank Mittal162244e2011-08-08 19:01:25 -07005021
Mayank Grover351a75e2017-05-30 20:06:08 +05305022 /* Detect multi-slot support */
5023 if (partition_multislot_is_supported())
5024 {
5025 boot_slot = partition_find_active_slot();
5026 if (boot_slot == INVALID)
5027 {
5028 boot_into_fastboot = true;
5029 dprintf(INFO, "Active Slot: (INVALID)\n");
5030 }
5031 else
5032 {
5033 /* Setting the state of system to boot active slot */
5034 partition_mark_active_slot(boot_slot);
5035 dprintf(INFO, "Active Slot: (%s)\n", SUFFIX_SLOT(boot_slot));
5036 }
5037 }
5038
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08005039 /* Display splash screen if enabled */
5040#if DISPLAY_SPLASH_SCREEN
lijuang99c02d82015-02-13 19:04:34 +08005041#if NO_ALARM_DISPLAY
5042 if (!check_alarm_boot()) {
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08005043#endif
lijuang99c02d82015-02-13 19:04:34 +08005044 dprintf(SPEW, "Display Init: Start\n");
Ajay Singh Parmara7865a82015-04-16 21:27:52 -07005045#if DISPLAY_HDMI_PRIMARY
5046 if (!strlen(device.display_panel))
5047 strlcpy(device.display_panel, DISPLAY_PANEL_HDMI,
5048 sizeof(device.display_panel));
5049#endif
Channagoud Kadabi90869ce2015-04-27 11:15:14 -07005050#if ENABLE_WBC
Channagoud Kadabid801ac72015-08-26 11:21:51 -07005051 /* Wait if the display shutdown is in progress */
5052 while(pm_app_display_shutdown_in_prgs());
Channagoud Kadabi90869ce2015-04-27 11:15:14 -07005053 if (!pm_appsbl_display_init_done())
5054 target_display_init(device.display_panel);
5055 else
5056 display_image_on_screen();
5057#else
lijuang99c02d82015-02-13 19:04:34 +08005058 target_display_init(device.display_panel);
Channagoud Kadabi90869ce2015-04-27 11:15:14 -07005059#endif
lijuang99c02d82015-02-13 19:04:34 +08005060 dprintf(SPEW, "Display Init: Done\n");
5061#if NO_ALARM_DISPLAY
5062 }
5063#endif
5064#endif
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08005065
Greg Griscod6250552011-06-29 14:40:23 -07005066 target_serialno((unsigned char *) sn_buf);
Ajay Dudanib06c05f2011-05-12 14:46:10 -07005067 dprintf(SPEW,"serial number: %s\n",sn_buf);
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -08005068
Dhaval Patel223ec952013-07-18 14:49:44 -07005069 memset(display_panel_buf, '\0', MAX_PANEL_BUF_SIZE);
5070
Matthew Qindefd5562014-07-11 18:02:40 +08005071 /*
5072 * Check power off reason if user force reset,
5073 * if yes phone will do normal boot.
5074 */
5075 if (is_user_force_reset())
5076 goto normal_boot;
5077
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07005078 /* Check if we should do something other than booting up */
Ameya Thakur0b9c2442013-05-10 13:22:56 -07005079 if (keys_get_state(KEY_VOLUMEUP) && keys_get_state(KEY_VOLUMEDOWN))
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07005080 {
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03005081 dprintf(ALWAYS,"dload mode key sequence detected\n");
lijuang395b5e62015-11-19 17:39:44 +08005082 reboot_device(EMERGENCY_DLOAD);
5083 dprintf(CRITICAL,"Failed to reboot into dload mode\n");
5084
Ameya Thakur0b9c2442013-05-10 13:22:56 -07005085 boot_into_fastboot = true;
5086 }
5087 if (!boot_into_fastboot)
5088 {
5089 if (keys_get_state(KEY_HOME) || keys_get_state(KEY_VOLUMEUP))
5090 boot_into_recovery = 1;
5091 if (!boot_into_recovery &&
5092 (keys_get_state(KEY_BACK) || keys_get_state(KEY_VOLUMEDOWN)))
Pavel Nedev5d91d412013-04-29 11:34:24 +03005093 boot_into_fastboot = true;
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07005094 }
Shashank Mittald8c42bf2010-06-09 15:44:28 -07005095 #if NO_KEYPAD_DRIVER
Kinson Chik0b1c8162011-08-31 16:31:57 -07005096 if (fastboot_trigger())
Pavel Nedev5d91d412013-04-29 11:34:24 +03005097 boot_into_fastboot = true;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07005098 #endif
Chandan Uddarajubedca152010-06-02 23:05:15 -07005099
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07005100#if USE_PON_REBOOT_REG
5101 reboot_mode = check_hard_reboot_mode();
5102#else
Ajay Dudani77421292010-10-27 19:34:06 -07005103 reboot_mode = check_reboot_mode();
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07005104#endif
5105 if (reboot_mode == RECOVERY_MODE)
5106 {
Ajay Dudani77421292010-10-27 19:34:06 -07005107 boot_into_recovery = 1;
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07005108 }
5109 else if(reboot_mode == FASTBOOT_MODE)
5110 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03005111 boot_into_fastboot = true;
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07005112 }
5113 else if(reboot_mode == ALARM_BOOT)
5114 {
Matthew Qind886f3c2014-01-17 16:52:01 +08005115 boot_reason_alarm = true;
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07005116 }
Monika Singhb0db4b82018-09-26 12:18:02 +05305117#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover912eaa62017-10-26 12:08:53 +05305118 else if (VB_M <= target_get_vb_version())
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07005119 {
Mayank Grover889be1b2017-09-12 20:12:23 +05305120 if (reboot_mode == DM_VERITY_ENFORCING)
5121 {
5122 device.verity_mode = 1;
5123 write_device_info(&device);
5124 }
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07005125#if ENABLE_VB_ATTEST
Mayank Grover889be1b2017-09-12 20:12:23 +05305126 else if (reboot_mode == DM_VERITY_EIO)
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07005127#else
Mayank Grover889be1b2017-09-12 20:12:23 +05305128 else if (reboot_mode == DM_VERITY_LOGGING)
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07005129#endif
Mayank Grover889be1b2017-09-12 20:12:23 +05305130 {
5131 device.verity_mode = 0;
5132 write_device_info(&device);
5133 }
5134 else if (reboot_mode == DM_VERITY_KEYSCLEAR)
5135 {
5136 if(send_delete_keys_to_tz())
5137 ASSERT(0);
5138 }
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07005139 }
Parth Dixitddbc7352015-10-18 03:13:31 +05305140#endif
Ajay Dudani77421292010-10-27 19:34:06 -07005141
Matthew Qindefd5562014-07-11 18:02:40 +08005142normal_boot:
Pavel Nedev5d91d412013-04-29 11:34:24 +03005143 if (!boot_into_fastboot)
Shashank Mittald8c42bf2010-06-09 15:44:28 -07005144 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03005145 if (target_is_emmc_boot())
Shashank Mittala0032282011-08-26 14:50:11 -07005146 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03005147 if(emmc_recovery_init())
5148 dprintf(ALWAYS,"error in emmc_recovery_init\n");
5149 if(target_use_signed_kernel())
Shashank Mittala0032282011-08-26 14:50:11 -07005150 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03005151 if((device.is_unlocked) || (device.is_tampered))
5152 {
5153 #ifdef TZ_TAMPER_FUSE
Monika Singh98257462018-06-06 11:28:49 +05305154 set_tamper_fuse_cmd(HLOS_IMG_TAMPER_FUSE);
Pavel Nedev5d91d412013-04-29 11:34:24 +03005155 #endif
5156 #if USE_PCOM_SECBOOT
5157 set_tamper_flag(device.is_tampered);
5158 #endif
5159 }
Shashank Mittala0032282011-08-26 14:50:11 -07005160 }
Amit Blay6281ebc2015-01-11 14:44:08 +02005161
Mayank Grover351a75e2017-05-30 20:06:08 +05305162retry_boot:
5163 /* Trying to boot active partition */
5164 if (partition_multislot_is_supported())
5165 {
5166 boot_slot = partition_find_boot_slot();
5167 partition_mark_active_slot(boot_slot);
5168 if (boot_slot == INVALID)
5169 goto fastboot;
5170 }
5171
5172 boot_err_type = boot_linux_from_mmc();
5173 switch (boot_err_type)
5174 {
5175 case ERR_INVALID_PAGE_SIZE:
5176 case ERR_DT_PARSE:
5177 case ERR_ABOOT_ADDR_OVERLAP:
Mayank Grover10cabbe2017-10-30 19:11:05 +05305178 case ERR_INVALID_BOOT_MAGIC:
Mayank Grover351a75e2017-05-30 20:06:08 +05305179 if(partition_multislot_is_supported())
Mayank Grover10cabbe2017-10-30 19:11:05 +05305180 {
5181 /*
5182 * Deactivate current slot, as it failed to
5183 * boot, and retry next slot.
5184 */
5185 partition_deactivate_slot(boot_slot);
Mayank Grover351a75e2017-05-30 20:06:08 +05305186 goto retry_boot;
Mayank Grover10cabbe2017-10-30 19:11:05 +05305187 }
Mayank Grover351a75e2017-05-30 20:06:08 +05305188 else
5189 break;
Mayank Grover351a75e2017-05-30 20:06:08 +05305190 default:
5191 break;
5192 /* going to fastboot menu */
5193 }
Shashank Mittala0032282011-08-26 14:50:11 -07005194 }
Pavel Nedev5d91d412013-04-29 11:34:24 +03005195 else
5196 {
5197 recovery_init();
5198 #if USE_PCOM_SECBOOT
5199 if((device.is_unlocked) || (device.is_tampered))
5200 set_tamper_flag(device.is_tampered);
5201 #endif
5202 boot_linux_from_flash();
5203 }
5204 dprintf(CRITICAL, "ERROR: Could not do normal boot. Reverting "
5205 "to fastboot mode.\n");
Shashank Mittald8c42bf2010-06-09 15:44:28 -07005206 }
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07005207
Mayank Grover351a75e2017-05-30 20:06:08 +05305208fastboot:
Amol Jadi5edf3552013-07-23 14:15:34 -07005209 /* We are here means regular boot did not happen. Start fastboot. */
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07005210
Amol Jadi5edf3552013-07-23 14:15:34 -07005211 /* register aboot specific fastboot commands */
5212 aboot_fastboot_register_commands();
Amol Jadi57abe4c2011-05-24 15:47:27 -07005213
Amol Jadi5edf3552013-07-23 14:15:34 -07005214 /* dump partition table for debug info */
Kinson Chikf1a43512011-07-14 11:28:39 -07005215 partition_dump();
Amol Jadi5edf3552013-07-23 14:15:34 -07005216
5217 /* initialize and start fastboot */
Saranya Chidura41b34532018-10-16 12:29:52 +05305218#if !VERIFIED_BOOT_2
Amol Jadi5edf3552013-07-23 14:15:34 -07005219 fastboot_init(target_get_scratch_address(), target_get_max_flash_size());
Saranya Chidura41b34532018-10-16 12:29:52 +05305220#else
5221 /* Add salt buffer offset at start of image address to copy VB salt */
5222 fastboot_init(ADD_SALT_BUFF_OFFSET(target_get_scratch_address()),
5223 SUB_SALT_BUFF_OFFSET(target_get_max_flash_size()));
5224#endif
lijuang4ece1e72015-08-14 21:02:36 +08005225#if FBCON_DISPLAY_MSG
lijuang9a7d3b92015-11-30 14:41:24 +08005226 display_fastboot_menu();
lijuang4ece1e72015-08-14 21:02:36 +08005227#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08005228}
5229
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07005230uint32_t get_page_size()
5231{
5232 return page_size;
5233}
5234
Amir Samuelov57a6fa22013-06-05 16:36:43 +03005235/*
5236 * Calculated and save hash (SHA256) for non-signed boot image.
5237 *
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07005238 * @param image_addr - Boot image address
5239 * @param image_size - Size of the boot image
Amir Samuelov57a6fa22013-06-05 16:36:43 +03005240 *
5241 * @return int - 0 on success, negative value on failure.
5242 */
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07005243static int aboot_save_boot_hash_mmc(uint32_t image_addr, uint32_t image_size)
Amir Samuelov57a6fa22013-06-05 16:36:43 +03005244{
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07005245 unsigned int digest[8];
5246#if IMAGE_VERIF_ALGO_SHA1
5247 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA1;
5248#else
5249 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA256;
5250#endif
Amir Samuelov57a6fa22013-06-05 16:36:43 +03005251
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07005252 target_crypto_init_params();
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -08005253 hash_find((unsigned char *) image_addr, image_size, (unsigned char *)&digest, auth_algo);
Amir Samuelov57a6fa22013-06-05 16:36:43 +03005254
5255 save_kernel_hash_cmd(digest);
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07005256 dprintf(INFO, "aboot_save_boot_hash_mmc: imagesize_actual size %d bytes.\n", (int) image_size);
Amir Samuelov57a6fa22013-06-05 16:36:43 +03005257
5258 return 0;
5259}
5260
Mayank Grover351a75e2017-05-30 20:06:08 +05305261
Brian Swetland9c4c0752009-01-25 16:23:50 -08005262APP_START(aboot)
5263 .init = aboot_init,
5264APP_END