blob: 31b5315d946c37070e08bdb91f1a5898c6832a1f [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";
Sourabh Banerjee6c7153c2018-03-20 01:21:57 +0530191#ifdef INIT_BIN_LE
192static const char *sys_path_cmdline = " rootwait ro init="INIT_BIN_LE;
193#else
Mayank Grover3804be72017-06-22 11:59:23 +0530194static const char *sys_path_cmdline = " rootwait ro init=/init";
Sourabh Banerjee6c7153c2018-03-20 01:21:57 +0530195#endif
Sourabh Banerjee386b1322018-02-27 09:37:28 +0530196
197#if VERITY_LE
198static const char *verity_dev = " root=/dev/dm-0";
199static const char *verity_system_part = " dm=\"system";
200static const char *verity_params = " none ro,0 1 android-verity /dev/mmcblk0p";
201#else
Mayank Grover3804be72017-06-22 11:59:23 +0530202static const char *sys_path = " root=/dev/mmcblk0p";
Sourabh Banerjee386b1322018-02-27 09:37:28 +0530203#endif
Ajay Dudanid04110c2011-01-17 23:55:07 -0800204
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700205#if VERIFIED_BOOT
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700206static const char *verity_mode = " androidboot.veritymode=";
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700207static const char *verified_state= " androidboot.verifiedbootstate=";
Parth Dixita5715a02015-10-29 12:25:10 +0530208static const char *keymaster_v1= " androidboot.keymaster=1";
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700209//indexed based on enum values, green is 0 by default
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700210
211struct verified_boot_verity_mode vbvm[] =
212{
Channagoud Kadabi86b0c112016-03-16 19:23:16 -0700213#if ENABLE_VB_ATTEST
214 {false, "eio"},
215#else
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700216 {false, "logging"},
Channagoud Kadabi86b0c112016-03-16 19:23:16 -0700217#endif
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700218 {true, "enforcing"},
219};
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700220struct verified_boot_state_name vbsn[] =
221{
222 {GREEN, "green"},
223 {ORANGE, "orange"},
224 {YELLOW,"yellow"},
225 {RED,"red" },
226};
227#endif
Channagoud Kadabi86b0c112016-03-16 19:23:16 -0700228/*As per spec delay wait time before shutdown in Red state*/
229#define DELAY_WAIT 30000
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700230static unsigned page_size = 0;
231static unsigned page_mask = 0;
Gaurav Nebhwani4d2389c2016-03-17 21:10:05 +0530232static unsigned mmc_blocksize = 0;
233static unsigned mmc_blocksize_mask = 0;
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700234static char ffbm_mode_string[FFBM_MODE_BUF_SIZE];
235static bool boot_into_ffbm;
vijay kumar870515d2015-08-31 16:37:24 +0530236static char *target_boot_params = NULL;
Matthew Qind886f3c2014-01-17 16:52:01 +0800237static bool boot_reason_alarm;
Channagoud Kadabi80a182b2015-03-11 17:04:23 -0700238static bool devinfo_present = true;
Channagoud Kadabi736c4962015-08-21 11:56:52 -0700239bool boot_into_fastboot = false;
Parth Dixit4097b622016-03-15 14:42:27 +0530240static uint32_t dt_size = 0;
Monika Singh292b3e92018-03-17 22:40:23 +0530241static char *vbcmdline;
242static bootinfo info = {0};
Shashank Mittalcd98d472011-08-02 14:29:24 -0700243/* Assuming unauthorized kernel image by default */
244static int auth_kernel_img = 0;
Monika Singh292b3e92018-03-17 22:40:23 +0530245static device_info device = {DEVICE_MAGIC,0,0,0,0,{0},{0},{0},1,{0},0,{0}};
246
vijay kumarc65876c2015-04-24 13:29:16 +0530247static bool is_allow_unlock = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -0700248
vijay kumarca2e6812015-07-08 20:28:25 +0530249static char frp_ptns[2][8] = {"config","frp"};
250
lijuang511a2b52015-08-14 20:50:51 +0800251static const char *critical_flash_allowed_ptn[] = {
252 "aboot",
253 "rpm",
254 "tz",
255 "sbl",
256 "sdi",
257 "sbl1",
258 "xbl",
259 "hyp",
260 "pmic",
261 "bootloader",
262 "devinfo",
263 "partition"};
264
Dima Zavin42168f22009-01-30 11:52:22 -0800265struct atag_ptbl_entry
266{
267 char name[16];
268 unsigned offset;
269 unsigned size;
270 unsigned flags;
271};
272
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700273/*
274 * Partition info, required to be published
275 * for fastboot
276 */
277struct getvar_partition_info {
Mayank Grover7b8a8f82017-10-27 13:07:59 +0530278 char part_name[MAX_GPT_NAME_SIZE]; /* Partition name */
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700279 char getvar_size[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for size */
280 char getvar_type[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for type */
281 char size_response[MAX_RSP_SIZE]; /* fastboot response for size */
282 char type_response[MAX_RSP_SIZE]; /* fastboot response for type */
283};
284
285/*
Mayank Grover7b8a8f82017-10-27 13:07:59 +0530286 * Update the part_type_known for known paritions types.
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700287 */
Mayank Groverd38fe012018-03-13 15:33:16 +0530288#define RAW_STR "raw"
Mayank Grover52cd10a2018-03-15 12:57:54 +0530289#define EXT_STR "ext4"
290#define F2FS_STR "f2fs"
291
292#define FS_SUPERBLOCK_OFFSET 0x400
293#define EXT_MAGIC 0xEF53
294#define EXT_MAGIC_OFFSET_SB 0x38
295#define F2FS_MAGIC 0xF2F52010 // F2FS Magic Number
296#define F2FS_MAGIC_OFFSET_SB 0x0
297
298typedef enum fs_signature_type {
299 EXT_FS_SIGNATURE = 1,
300 EXT_F2FS_SIGNATURE = 2,
301 NO_FS = -1
302} fs_signature_type;
303
Mayank Grover7b8a8f82017-10-27 13:07:59 +0530304struct getvar_partition_info part_info[NUM_PARTITIONS];
305struct getvar_partition_info part_type_known[] =
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700306{
Mayank Grover49920212018-02-09 18:15:55 +0530307 { "system" , "partition-size:", "partition-type:", "", "ext4" },
308 { "userdata" , "partition-size:", "partition-type:", "", "ext4" },
309 { "cache" , "partition-size:", "partition-type:", "", "ext4" },
310 { "recoveryfs" , "partition-size:", "partition-type:", "", "ext4" },
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700311};
312
313char max_download_size[MAX_RSP_SIZE];
Ameya Thakur11cf1a62013-08-05 12:44:48 -0700314char charger_screen_enabled[MAX_RSP_SIZE];
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800315char sn_buf[13];
Dhaval Patel223ec952013-07-18 14:49:44 -0700316char display_panel_buf[MAX_PANEL_BUF_SIZE];
Unnati Gandhi62c8ab82014-01-24 11:01:01 +0530317char panel_display_mode[MAX_RSP_SIZE];
Mayank Grover7b8a8f82017-10-27 13:07:59 +0530318#if PRODUCT_IOT
319char block_size_string[MAX_RSP_SIZE];
320
321/* For IOT we are using custom version */
322#define PRODUCT_IOT_VERSION "IOT001"
323char bootloader_version_string[MAX_RSP_SIZE];
324#endif
lijuang102dfa92015-10-09 18:31:03 +0800325
326#if CHECK_BAT_VOLTAGE
lijuang65c5a822015-08-29 16:35:36 +0800327char battery_voltage[MAX_RSP_SIZE];
lijuang102dfa92015-10-09 18:31:03 +0800328char battery_soc_ok [MAX_RSP_SIZE];
329#endif
330
lijuangf16461c2015-08-03 17:09:34 +0800331char get_variant[MAX_RSP_SIZE];
Greg Griscod6250552011-06-29 14:40:23 -0700332
Greg Griscod2471ef2011-07-14 13:00:42 -0700333extern int emmc_recovery_init(void);
334
Kinson Chik0b1c8162011-08-31 16:31:57 -0700335#if NO_KEYPAD_DRIVER
336extern int fastboot_trigger(void);
337#endif
Greg Griscod2471ef2011-07-14 13:00:42 -0700338
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800339static void update_ker_tags_rdisk_addr(struct boot_img_hdr *hdr, bool is_arm64)
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700340{
341 /* overwrite the destination of specified for the project */
Channagoud Kadabi7042fa32013-04-26 16:44:14 -0700342#ifdef ABOOT_IGNORE_BOOT_HEADER_ADDRS
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800343 if (is_arm64)
344 hdr->kernel_addr = ABOOT_FORCE_KERNEL64_ADDR;
345 else
346 hdr->kernel_addr = ABOOT_FORCE_KERNEL_ADDR;
Channagoud Kadabi7042fa32013-04-26 16:44:14 -0700347 hdr->ramdisk_addr = ABOOT_FORCE_RAMDISK_ADDR;
348 hdr->tags_addr = ABOOT_FORCE_TAGS_ADDR;
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700349#endif
350}
351
Dima Zavin42168f22009-01-30 11:52:22 -0800352static void ptentry_to_tag(unsigned **ptr, struct ptentry *ptn)
353{
354 struct atag_ptbl_entry atag_ptn;
355
356 memcpy(atag_ptn.name, ptn->name, 16);
357 atag_ptn.name[15] = '\0';
358 atag_ptn.offset = ptn->start;
359 atag_ptn.size = ptn->length;
360 atag_ptn.flags = ptn->flags;
361 memcpy(*ptr, &atag_ptn, sizeof(struct atag_ptbl_entry));
362 *ptr += sizeof(struct atag_ptbl_entry) / sizeof(unsigned);
363}
Brian Swetland9c4c0752009-01-25 16:23:50 -0800364
lijuang102dfa92015-10-09 18:31:03 +0800365#if CHECK_BAT_VOLTAGE
366void update_battery_status(void)
367{
368 snprintf(battery_voltage,MAX_RSP_SIZE, "%d",target_get_battery_voltage());
369 snprintf(battery_soc_ok ,MAX_RSP_SIZE, "%s",target_battery_soc_ok()? "yes":"no");
370}
371#endif
372
Neeti Desaie245d492012-06-01 12:52:13 -0700373unsigned char *update_cmdline(const char * cmdline)
Brian Swetland9c4c0752009-01-25 16:23:50 -0800374{
David Ng183a7422009-12-07 14:55:21 -0800375 int cmdline_len = 0;
376 int have_cmdline = 0;
Amol Jadi168b7712012-03-06 16:15:00 -0800377 unsigned char *cmdline_final = NULL;
Neeti Desaie245d492012-06-01 12:52:13 -0700378 int pause_at_bootup = 0;
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800379 bool warm_boot = false;
Pavel Nedev5614d222013-06-17 18:01:02 +0300380 bool gpt_exists = partition_gpt_exists();
Joonwoo Park61112782013-10-02 19:50:39 -0700381 int have_target_boot_params = 0;
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700382 char *boot_dev_buf = NULL;
Monika Singh292b3e92018-03-17 22:40:23 +0530383#ifdef MDTP_SUPPORT
Mayank Grover351a75e2017-05-30 20:06:08 +0530384 bool is_mdtp_activated = 0;
Monika Singh292b3e92018-03-17 22:40:23 +0530385#endif
Mayank Grover351a75e2017-05-30 20:06:08 +0530386 int current_active_slot = INVALID;
Mayank Grover3804be72017-06-22 11:59:23 +0530387 int system_ptn_index = -1;
388 unsigned int lun = 0;
389 char lun_char_base = 'a';
Sourabh Banerjee386b1322018-02-27 09:37:28 +0530390#if VERITY_LE
391 int syspath_buflen = strlen(verity_dev)
392 + strlen(verity_system_part) + (sizeof(char) * 2) + 2
393 + strlen(verity_params) + sizeof(int) + 2;
394#else
395 int syspath_buflen = strlen(sys_path) + sizeof(int) + 2; /*allocate buflen for largest possible string*/
396#endif
Mayank Grover3804be72017-06-22 11:59:23 +0530397 char syspath_buf[syspath_buflen];
Mayank Grover889be1b2017-09-12 20:12:23 +0530398#if VERIFIED_BOOT
399 uint32_t boot_state = RED;
400#endif
Hareesh Gauthamabf1e1e2017-04-12 18:09:24 +0530401
402#if USE_LE_SYSTEMD
403 is_systemd_present=true;
404#endif
405
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700406#if VERIFIED_BOOT
Mayank Grover912eaa62017-10-26 12:08:53 +0530407 if (VB_M <= target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +0530408 {
409 boot_state = boot_verify_get_state();
410 }
Parth Dixitddbc7352015-10-18 03:13:31 +0530411#endif
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700412
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200413#ifdef MDTP_SUPPORT
414 mdtp_activated(&is_mdtp_activated);
415#endif /* MDTP_SUPPORT */
Dima Zavin42168f22009-01-30 11:52:22 -0800416
Brian Swetland9c4c0752009-01-25 16:23:50 -0800417 if (cmdline && cmdline[0]) {
David Ng183a7422009-12-07 14:55:21 -0800418 cmdline_len = strlen(cmdline);
419 have_cmdline = 1;
420 }
421 if (target_is_emmc_boot()) {
422 cmdline_len += strlen(emmc_cmdline);
Sridhar Parasuram7bd4aaf2015-02-12 11:14:38 -0800423#if USE_BOOTDEV_CMDLINE
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700424 boot_dev_buf = (char *) malloc(sizeof(char) * BOOT_DEV_MAX_LEN);
425 ASSERT(boot_dev_buf);
426 platform_boot_dev_cmdline(boot_dev_buf);
427 cmdline_len += strlen(boot_dev_buf);
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700428#endif
David Ng183a7422009-12-07 14:55:21 -0800429 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800430
431 cmdline_len += strlen(usb_sn_cmdline);
432 cmdline_len += strlen(sn_buf);
433
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700434#if VERIFIED_BOOT
Mayank Grover912eaa62017-10-26 12:08:53 +0530435 if (VB_M <= target_get_vb_version())
Sridhar Parasuram7d8bb9a2015-09-23 18:21:19 -0700436 {
Mayank Grover889be1b2017-09-12 20:12:23 +0530437 cmdline_len += strlen(verified_state) + strlen(vbsn[boot_state].name);
438 if ((device.verity_mode != 0 ) && (device.verity_mode != 1))
439 {
440 dprintf(CRITICAL, "Devinfo paritition possibly corrupted!!!. Please erase devinfo partition to continue booting\n");
441 ASSERT(0);
442 }
443 cmdline_len += strlen(verity_mode) + strlen(vbvm[device.verity_mode].name);
444 cmdline_len += strlen(keymaster_v1);
Sridhar Parasuram7d8bb9a2015-09-23 18:21:19 -0700445 }
Parth Dixitddbc7352015-10-18 03:13:31 +0530446#endif
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700447
Monika Singh292b3e92018-03-17 22:40:23 +0530448
449 if (vbcmdline != NULL) {
450 dprintf(DEBUG, "UpdateCmdLine vbcmdline present len %d\n",
451 strlen(vbcmdline));
452 cmdline_len += strlen(vbcmdline);
453 }
454
Pavel Nedev5614d222013-06-17 18:01:02 +0300455 if (boot_into_recovery && gpt_exists)
456 cmdline_len += strlen(secondary_gpt_enable);
457
Monika Singh292b3e92018-03-17 22:40:23 +0530458#ifdef MDTP_SUPPORT
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200459 if(is_mdtp_activated)
460 cmdline_len += strlen(mdtp_activated_flag);
Monika Singh292b3e92018-03-17 22:40:23 +0530461#endif
Pavel Nedev328ac822013-04-05 15:25:11 +0300462 if (boot_into_ffbm) {
463 cmdline_len += strlen(androidboot_mode);
Hareesh Gauthamabf1e1e2017-04-12 18:09:24 +0530464
465 if(is_systemd_present)
466 cmdline_len += strlen(systemd_ffbm_mode);
467
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700468 cmdline_len += strlen(ffbm_mode_string);
Pavel Nedev898298c2013-02-27 12:36:09 -0800469 /* reduce kernel console messages to speed-up boot */
470 cmdline_len += strlen(loglevel);
Matthew Qind886f3c2014-01-17 16:52:01 +0800471 } else if (boot_reason_alarm) {
472 cmdline_len += strlen(alarmboot_cmdline);
Zhenhua Huang431dafa2015-06-30 16:13:37 +0800473 } else if ((target_build_variant_user() || device.charger_screen_enabled)
474 && target_pause_for_battery_charge()) {
David Ngf773dde2010-07-26 19:55:08 -0700475 pause_at_bootup = 1;
476 cmdline_len += strlen(battchg_pause);
477 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800478
Shashank Mittalcd98d472011-08-02 14:29:24 -0700479 if(target_use_signed_kernel() && auth_kernel_img) {
480 cmdline_len += strlen(auth_kernel);
481 }
482
Joonwoo Park61112782013-10-02 19:50:39 -0700483 if (get_target_boot_params(cmdline, boot_into_recovery ? "recoveryfs" :
484 "system",
vijay kumar870515d2015-08-31 16:37:24 +0530485 &target_boot_params) == 0) {
Joonwoo Park61112782013-10-02 19:50:39 -0700486 have_target_boot_params = 1;
487 cmdline_len += strlen(target_boot_params);
488 }
489
Ajay Dudanid04110c2011-01-17 23:55:07 -0800490 /* Determine correct androidboot.baseband to use */
491 switch(target_baseband())
492 {
493 case BASEBAND_APQ:
494 cmdline_len += strlen(baseband_apq);
495 break;
496
497 case BASEBAND_MSM:
498 cmdline_len += strlen(baseband_msm);
499 break;
500
501 case BASEBAND_CSFB:
502 cmdline_len += strlen(baseband_csfb);
503 break;
504
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800505 case BASEBAND_SVLTE2A:
506 cmdline_len += strlen(baseband_svlte2a);
Ajay Dudanid04110c2011-01-17 23:55:07 -0800507 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700508
509 case BASEBAND_MDM:
510 cmdline_len += strlen(baseband_mdm);
511 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700512
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800513 case BASEBAND_MDM2:
514 cmdline_len += strlen(baseband_mdm2);
515 break;
516
Amol Jadi5c61a952012-05-04 17:05:35 -0700517 case BASEBAND_SGLTE:
518 cmdline_len += strlen(baseband_sglte);
519 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530520
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800521 case BASEBAND_SGLTE2:
522 cmdline_len += strlen(baseband_sglte2);
523 break;
524
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530525 case BASEBAND_DSDA:
526 cmdline_len += strlen(baseband_dsda);
527 break;
Amol Jadi2a15a272013-01-22 12:03:36 -0800528
529 case BASEBAND_DSDA2:
530 cmdline_len += strlen(baseband_dsda2);
531 break;
Vijay Kumar Pendotib228cfc2016-06-13 20:15:23 +0530532 case BASEBAND_APQ_NOWGR:
533 cmdline_len += strlen(baseband_apq_nowgr);
534 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800535 }
536
Vladislav Levenetz3cd45242017-04-05 08:58:07 +0300537#if ENABLE_DISPLAY
Lijuan Gao4a5b8322014-07-24 10:38:42 +0800538 if (cmdline) {
539 if ((strstr(cmdline, DISPLAY_DEFAULT_PREFIX) == NULL) &&
Padmanabhan Komandurubccbcdc2015-06-30 16:19:24 +0530540 target_display_panel_node(display_panel_buf,
541 MAX_PANEL_BUF_SIZE) &&
Lijuan Gao4a5b8322014-07-24 10:38:42 +0800542 strlen(display_panel_buf)) {
543 cmdline_len += strlen(display_panel_buf);
544 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700545 }
Vladislav Levenetz3cd45242017-04-05 08:58:07 +0300546#endif
Dhaval Patel223ec952013-07-18 14:49:44 -0700547
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800548 if (target_warm_boot()) {
549 warm_boot = true;
550 cmdline_len += strlen(warmboot_cmdline);
551 }
552
Mayank Grover351a75e2017-05-30 20:06:08 +0530553 if (partition_multislot_is_supported())
554 {
555 current_active_slot = partition_find_active_slot();
556 cmdline_len += (strlen(androidboot_slot_suffix)+
557 strlen(SUFFIX_SLOT(current_active_slot)));
558
Mayank Grover3804be72017-06-22 11:59:23 +0530559 system_ptn_index = partition_get_index("system");
560 if (platform_boot_dev_isemmc())
561 {
Sourabh Banerjee386b1322018-02-27 09:37:28 +0530562#if VERITY_LE
563 /*
564 Condition 4: Verity and A/B both enabled
565 Eventual command line looks like:
566 ... androidboot.slot_suffix=<slot_suffix> ... rootfstype=ext4 ...
567 ... root=/dev/dm-0 dm="system_<slot_suffix> none ro,0 1 android-verity /dev/mmcblk0p<NN>"
568 */
569 snprintf(syspath_buf, syspath_buflen, " %s %s%s %s%d\"",
570 verity_dev,
571 verity_system_part, suffix_slot[current_active_slot],
572 verity_params, system_ptn_index + 1);
573#else
574 /*
575 Condition 5: A/B enabled, but verity disabled
576 Eventual command line looks like:
577 ... androidboot.slot_suffix=<slot_suffix> ... rootfstype=ext4 ...
578 ... root=/dev/mmcblk0p<NN> ...
579 */
580 snprintf(syspath_buf, syspath_buflen, " %s%d",
581 sys_path, system_ptn_index + 1);
582#endif
Mayank Grover3804be72017-06-22 11:59:23 +0530583 }
584 else
585 {
586 lun = partition_get_lun(system_ptn_index);
587 snprintf(syspath_buf, syspath_buflen, " root=/dev/sd%c%d",
588 lun_char_base + lun,
589 partition_get_index_in_lun("system", lun));
590 }
Mayank Grover351a75e2017-05-30 20:06:08 +0530591
Mayank Grover3804be72017-06-22 11:59:23 +0530592 cmdline_len += strlen(sys_path_cmdline);
Monika Singh292b3e92018-03-17 22:40:23 +0530593#ifndef VERIFIED_BOOT_2
Mayank Grover3804be72017-06-22 11:59:23 +0530594 cmdline_len += strlen(syspath_buf);
Monika Singh292b3e92018-03-17 22:40:23 +0530595#endif
Mayank Grover351a75e2017-05-30 20:06:08 +0530596 if (!boot_into_recovery)
597 cmdline_len += strlen(skip_ramfs);
598 }
599
Channagoud Kadabi85c7ec32016-01-28 23:09:21 -0800600#if TARGET_CMDLINE_SUPPORT
601 char *target_cmdline_buf = malloc(TARGET_MAX_CMDLNBUF);
602 int target_cmd_line_len;
603 ASSERT(target_cmdline_buf);
604 target_cmd_line_len = target_update_cmdline(target_cmdline_buf);
605 cmdline_len += target_cmd_line_len;
606#endif
607
David Ng183a7422009-12-07 14:55:21 -0800608 if (cmdline_len > 0) {
609 const char *src;
Maria Yu52254c02014-07-04 16:14:54 +0800610 unsigned char *dst;
611
612 cmdline_final = (unsigned char*) malloc((cmdline_len + 4) & (~3));
613 ASSERT(cmdline_final != NULL);
vijay kumar287bb542015-09-29 13:01:52 +0530614 memset((void *)cmdline_final, 0, sizeof(*cmdline_final));
Maria Yu52254c02014-07-04 16:14:54 +0800615 dst = cmdline_final;
Neeti Desaie245d492012-06-01 12:52:13 -0700616
Amol Jadi168b7712012-03-06 16:15:00 -0800617 /* Save start ptr for debug print */
David Ng183a7422009-12-07 14:55:21 -0800618 if (have_cmdline) {
619 src = cmdline;
620 while ((*dst++ = *src++));
621 }
622 if (target_is_emmc_boot()) {
623 src = emmc_cmdline;
624 if (have_cmdline) --dst;
David Ngf773dde2010-07-26 19:55:08 -0700625 have_cmdline = 1;
626 while ((*dst++ = *src++));
Sridhar Parasuram7bd4aaf2015-02-12 11:14:38 -0800627#if USE_BOOTDEV_CMDLINE
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700628 src = boot_dev_buf;
629 if (have_cmdline) --dst;
630 while ((*dst++ = *src++));
631#endif
David Ngf773dde2010-07-26 19:55:08 -0700632 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800633
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700634#if VERIFIED_BOOT
Mayank Grover912eaa62017-10-26 12:08:53 +0530635 if (VB_M <= target_get_vb_version())
Sridhar Parasuram7d8bb9a2015-09-23 18:21:19 -0700636 {
Mayank Grover889be1b2017-09-12 20:12:23 +0530637 src = verified_state;
638 if(have_cmdline) --dst;
639 have_cmdline = 1;
640 while ((*dst++ = *src++));
641 src = vbsn[boot_state].name;
642 if(have_cmdline) --dst;
643 while ((*dst++ = *src++));
644
645 if ((device.verity_mode != 0 ) && (device.verity_mode != 1))
646 {
647 dprintf(CRITICAL, "Devinfo paritition possibly corrupted!!!. Please erase devinfo partition to continue booting\n");
648 ASSERT(0);
649 }
650 src = verity_mode;
651 if(have_cmdline) --dst;
652 while ((*dst++ = *src++));
653 src = vbvm[device.verity_mode].name;
654 if(have_cmdline) -- dst;
655 while ((*dst++ = *src++));
656 src = keymaster_v1;
657 if(have_cmdline) --dst;
658 while ((*dst++ = *src++));
Sridhar Parasuram7d8bb9a2015-09-23 18:21:19 -0700659 }
Parth Dixitddbc7352015-10-18 03:13:31 +0530660#endif
Monika Singh292b3e92018-03-17 22:40:23 +0530661
662 if (vbcmdline != NULL) {
663 src = vbcmdline;
664 if (have_cmdline) --dst;
665 while ((*dst++ = *src++));
666 }
667
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800668 src = usb_sn_cmdline;
669 if (have_cmdline) --dst;
670 have_cmdline = 1;
671 while ((*dst++ = *src++));
672 src = sn_buf;
673 if (have_cmdline) --dst;
674 have_cmdline = 1;
675 while ((*dst++ = *src++));
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800676 if (warm_boot) {
677 if (have_cmdline) --dst;
678 src = warmboot_cmdline;
679 while ((*dst++ = *src++));
680 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800681
Pavel Nedev5614d222013-06-17 18:01:02 +0300682 if (boot_into_recovery && gpt_exists) {
683 src = secondary_gpt_enable;
684 if (have_cmdline) --dst;
685 while ((*dst++ = *src++));
686 }
Monika Singh292b3e92018-03-17 22:40:23 +0530687#ifdef MDTP_SUPPORT
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200688 if (is_mdtp_activated) {
689 src = mdtp_activated_flag;
690 if (have_cmdline) --dst;
691 while ((*dst++ = *src++));
692 }
Monika Singh292b3e92018-03-17 22:40:23 +0530693#endif
Pavel Nedev328ac822013-04-05 15:25:11 +0300694 if (boot_into_ffbm) {
695 src = androidboot_mode;
696 if (have_cmdline) --dst;
697 while ((*dst++ = *src++));
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700698 src = ffbm_mode_string;
Pavel Nedev328ac822013-04-05 15:25:11 +0300699 if (have_cmdline) --dst;
700 while ((*dst++ = *src++));
Hareesh Gauthamabf1e1e2017-04-12 18:09:24 +0530701
702 if(is_systemd_present) {
703 src = systemd_ffbm_mode;
704 if (have_cmdline) --dst;
705 while ((*dst++ = *src++));
706 }
707
Pavel Nedev898298c2013-02-27 12:36:09 -0800708 src = loglevel;
709 if (have_cmdline) --dst;
710 while ((*dst++ = *src++));
Matthew Qind886f3c2014-01-17 16:52:01 +0800711 } else if (boot_reason_alarm) {
712 src = alarmboot_cmdline;
713 if (have_cmdline) --dst;
714 while ((*dst++ = *src++));
Pavel Nedev328ac822013-04-05 15:25:11 +0300715 } else if (pause_at_bootup) {
David Ngf773dde2010-07-26 19:55:08 -0700716 src = battchg_pause;
717 if (have_cmdline) --dst;
David Ng183a7422009-12-07 14:55:21 -0800718 while ((*dst++ = *src++));
719 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800720
Shashank Mittalcd98d472011-08-02 14:29:24 -0700721 if(target_use_signed_kernel() && auth_kernel_img) {
722 src = auth_kernel;
723 if (have_cmdline) --dst;
724 while ((*dst++ = *src++));
725 }
726
Ajay Dudanid04110c2011-01-17 23:55:07 -0800727 switch(target_baseband())
728 {
729 case BASEBAND_APQ:
730 src = baseband_apq;
731 if (have_cmdline) --dst;
732 while ((*dst++ = *src++));
733 break;
734
735 case BASEBAND_MSM:
736 src = baseband_msm;
737 if (have_cmdline) --dst;
738 while ((*dst++ = *src++));
739 break;
740
741 case BASEBAND_CSFB:
742 src = baseband_csfb;
743 if (have_cmdline) --dst;
744 while ((*dst++ = *src++));
745 break;
746
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800747 case BASEBAND_SVLTE2A:
748 src = baseband_svlte2a;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800749 if (have_cmdline) --dst;
750 while ((*dst++ = *src++));
751 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700752
753 case BASEBAND_MDM:
754 src = baseband_mdm;
755 if (have_cmdline) --dst;
756 while ((*dst++ = *src++));
757 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700758
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800759 case BASEBAND_MDM2:
760 src = baseband_mdm2;
761 if (have_cmdline) --dst;
762 while ((*dst++ = *src++));
763 break;
764
Amol Jadi5c61a952012-05-04 17:05:35 -0700765 case BASEBAND_SGLTE:
766 src = baseband_sglte;
767 if (have_cmdline) --dst;
768 while ((*dst++ = *src++));
769 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530770
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800771 case BASEBAND_SGLTE2:
772 src = baseband_sglte2;
773 if (have_cmdline) --dst;
774 while ((*dst++ = *src++));
775 break;
776
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530777 case BASEBAND_DSDA:
778 src = baseband_dsda;
779 if (have_cmdline) --dst;
780 while ((*dst++ = *src++));
781 break;
Amol Jadi2a15a272013-01-22 12:03:36 -0800782
783 case BASEBAND_DSDA2:
784 src = baseband_dsda2;
785 if (have_cmdline) --dst;
786 while ((*dst++ = *src++));
787 break;
Vijay Kumar Pendotib228cfc2016-06-13 20:15:23 +0530788 case BASEBAND_APQ_NOWGR:
789 src = baseband_apq_nowgr;
790 if (have_cmdline) --dst;
791 while ((*dst++ = *src++));
792 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800793 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700794
795 if (strlen(display_panel_buf)) {
Dhaval Patel223ec952013-07-18 14:49:44 -0700796 src = display_panel_buf;
797 if (have_cmdline) --dst;
798 while ((*dst++ = *src++));
799 }
Joonwoo Park61112782013-10-02 19:50:39 -0700800
801 if (have_target_boot_params) {
802 if (have_cmdline) --dst;
803 src = target_boot_params;
804 while ((*dst++ = *src++));
vijay kumar870515d2015-08-31 16:37:24 +0530805 free(target_boot_params);
Joonwoo Park61112782013-10-02 19:50:39 -0700806 }
Channagoud Kadabi85c7ec32016-01-28 23:09:21 -0800807
Mayank Grover351a75e2017-05-30 20:06:08 +0530808 if (partition_multislot_is_supported() && have_cmdline)
809 {
810 src = androidboot_slot_suffix;
811 --dst;
812 while ((*dst++ = *src++));
813 --dst;
814 src = SUFFIX_SLOT(current_active_slot);
815 while ((*dst++ = *src++));
816
817 if (!boot_into_recovery)
818 {
819 src = skip_ramfs;
820 --dst;
821 while ((*dst++ = *src++));
822 }
823
824 src = sys_path_cmdline;
825 --dst;
826 while ((*dst++ = *src++));
Mayank Grover3804be72017-06-22 11:59:23 +0530827
Monika Singh292b3e92018-03-17 22:40:23 +0530828#ifndef VERIFIED_BOOT_2
Mayank Grover3804be72017-06-22 11:59:23 +0530829 src = syspath_buf;
830 --dst;
831 while ((*dst++ = *src++));
Monika Singh292b3e92018-03-17 22:40:23 +0530832#endif
Mayank Grover351a75e2017-05-30 20:06:08 +0530833 }
834
Channagoud Kadabi85c7ec32016-01-28 23:09:21 -0800835#if TARGET_CMDLINE_SUPPORT
836 if (target_cmdline_buf && target_cmd_line_len)
837 {
838 if (have_cmdline) --dst;
839 src = target_cmdline_buf;
840 while((*dst++ = *src++));
841 free(target_cmdline_buf);
842 }
843#endif
Neeti Desaie245d492012-06-01 12:52:13 -0700844 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700845
846
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700847 if (boot_dev_buf)
848 free(boot_dev_buf);
849
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -0800850 if (cmdline_final)
851 dprintf(INFO, "cmdline: %s\n", cmdline_final);
852 else
853 dprintf(INFO, "cmdline is NULL\n");
Neeti Desaie245d492012-06-01 12:52:13 -0700854 return cmdline_final;
855}
856
857unsigned *atag_core(unsigned *ptr)
858{
859 /* CORE */
860 *ptr++ = 2;
861 *ptr++ = 0x54410001;
862
863 return ptr;
864
865}
866
867unsigned *atag_ramdisk(unsigned *ptr, void *ramdisk,
868 unsigned ramdisk_size)
869{
870 if (ramdisk_size) {
871 *ptr++ = 4;
872 *ptr++ = 0x54420005;
873 *ptr++ = (unsigned)ramdisk;
874 *ptr++ = ramdisk_size;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800875 }
876
Neeti Desaie245d492012-06-01 12:52:13 -0700877 return ptr;
878}
879
880unsigned *atag_ptable(unsigned **ptr_addr)
881{
882 int i;
883 struct ptable *ptable;
884
885 if ((ptable = flash_get_ptable()) && (ptable->count != 0)) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700886 *(*ptr_addr)++ = 2 + (ptable->count * (sizeof(struct atag_ptbl_entry) /
887 sizeof(unsigned)));
Neeti Desaie245d492012-06-01 12:52:13 -0700888 *(*ptr_addr)++ = 0x4d534d70;
889 for (i = 0; i < ptable->count; ++i)
890 ptentry_to_tag(ptr_addr, ptable_get(ptable, i));
891 }
892
893 return (*ptr_addr);
894}
895
896unsigned *atag_cmdline(unsigned *ptr, const char *cmdline)
897{
898 int cmdline_length = 0;
899 int n;
Neeti Desaie245d492012-06-01 12:52:13 -0700900 char *dest;
901
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800902 cmdline_length = strlen((const char*)cmdline);
Neeti Desaie245d492012-06-01 12:52:13 -0700903 n = (cmdline_length + 4) & (~3);
904
905 *ptr++ = (n / 4) + 2;
906 *ptr++ = 0x54410009;
907 dest = (char *) ptr;
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800908 while ((*dest++ = *cmdline++));
Neeti Desaie245d492012-06-01 12:52:13 -0700909 ptr += (n / 4);
910
911 return ptr;
912}
913
914unsigned *atag_end(unsigned *ptr)
915{
Brian Swetland9c4c0752009-01-25 16:23:50 -0800916 /* END */
917 *ptr++ = 0;
918 *ptr++ = 0;
919
Neeti Desaie245d492012-06-01 12:52:13 -0700920 return ptr;
921}
922
923void generate_atags(unsigned *ptr, const char *cmdline,
924 void *ramdisk, unsigned ramdisk_size)
925{
vijay kumar21a37452015-12-29 16:23:21 +0530926 unsigned *orig_ptr = ptr;
Neeti Desaie245d492012-06-01 12:52:13 -0700927 ptr = atag_core(ptr);
928 ptr = atag_ramdisk(ptr, ramdisk, ramdisk_size);
929 ptr = target_atag_mem(ptr);
930
931 /* Skip NAND partition ATAGS for eMMC boot */
932 if (!target_is_emmc_boot()){
933 ptr = atag_ptable(&ptr);
934 }
935
vijay kumar21a37452015-12-29 16:23:21 +0530936 /*
937 * Atags size filled till + cmdline size + 1 unsigned for 4-byte boundary + 4 unsigned
938 * for atag identifier in atag_cmdline and atag_end should be with in MAX_TAGS_SIZE bytes
939 */
Mayank Groverbc8a2ef2018-02-23 18:03:36 +0530940 if (!cmdline)
941 return;
942
vijay kumar21a37452015-12-29 16:23:21 +0530943 if (((ptr - orig_ptr) + strlen(cmdline) + 5 * sizeof(unsigned)) < MAX_TAGS_SIZE) {
944 ptr = atag_cmdline(ptr, cmdline);
945 ptr = atag_end(ptr);
946 }
947 else {
948 dprintf(CRITICAL,"Crossing ATAGs Max size allowed\n");
949 ASSERT(0);
950 }
Neeti Desaie245d492012-06-01 12:52:13 -0700951}
952
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700953typedef void entry_func_ptr(unsigned, unsigned, unsigned*);
Neeti Desaie245d492012-06-01 12:52:13 -0700954void boot_linux(void *kernel, unsigned *tags,
955 const char *cmdline, unsigned machtype,
956 void *ramdisk, unsigned ramdisk_size)
957{
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800958 unsigned char *final_cmdline;
Amol Jadib6be5c12012-11-14 13:39:51 -0800959#if DEVICE_TREE
Neeti Desai17379b82012-06-04 18:42:53 -0700960 int ret = 0;
Amol Jadib6be5c12012-11-14 13:39:51 -0800961#endif
962
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700963 void (*entry)(unsigned, unsigned, unsigned*) = (entry_func_ptr*)(PA((addr_t)kernel));
Deepa Dinamani25a9f762012-11-30 15:57:15 -0800964 uint32_t tags_phys = PA((addr_t)tags);
vijay kumar1a50a642015-11-16 12:41:15 +0530965 struct kernel64_hdr *kptr = ((struct kernel64_hdr*)(PA((addr_t)kernel)));
Deepa Dinamani25a9f762012-11-30 15:57:15 -0800966
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530967 ramdisk = (void *)PA((addr_t)ramdisk);
Neeti Desaie245d492012-06-01 12:52:13 -0700968
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800969 final_cmdline = update_cmdline((const char*)cmdline);
970
Neeti Desai17379b82012-06-04 18:42:53 -0700971#if DEVICE_TREE
Amol Jadib6be5c12012-11-14 13:39:51 -0800972 dprintf(INFO, "Updating device tree: start\n");
973
Neeti Desai17379b82012-06-04 18:42:53 -0700974 /* Update the Device Tree */
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530975 ret = update_device_tree((void *)tags,(const char *)final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -0700976 if(ret)
977 {
978 dprintf(CRITICAL, "ERROR: Updating Device Tree Failed \n");
979 ASSERT(0);
980 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800981 dprintf(INFO, "Updating device tree: done\n");
Neeti Desai17379b82012-06-04 18:42:53 -0700982#else
Neeti Desaie245d492012-06-01 12:52:13 -0700983 /* Generating the Atags */
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800984 generate_atags(tags, final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -0700985#endif
Neeti Desaie245d492012-06-01 12:52:13 -0700986
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -0700987#if VERIFIED_BOOT
Monika Singh292b3e92018-03-17 22:40:23 +0530988 if (VB_M == target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +0530989 {
990 if (device.verity_mode == 0) {
lijuangbdd9bb42016-03-01 18:22:17 +0800991#if FBCON_DISPLAY_MSG
Channagoud Kadabi86b0c112016-03-16 19:23:16 -0700992#if ENABLE_VB_ATTEST
Mayank Grover889be1b2017-09-12 20:12:23 +0530993 display_bootverify_menu(DISPLAY_MENU_EIO);
994 wait_for_users_action();
995 if(!pwr_key_is_pressed)
996 shutdown_device();
Channagoud Kadabi86b0c112016-03-16 19:23:16 -0700997#else
Mayank Grover889be1b2017-09-12 20:12:23 +0530998 display_bootverify_menu(DISPLAY_MENU_LOGGING);
Channagoud Kadabi86b0c112016-03-16 19:23:16 -0700999#endif
Mayank Grover889be1b2017-09-12 20:12:23 +05301000 wait_for_users_action();
lijuangbdd9bb42016-03-01 18:22:17 +08001001#else
Mayank Grover889be1b2017-09-12 20:12:23 +05301002 dprintf(CRITICAL,
1003 "The dm-verity is not started in enforcing mode.\nWait for 5 seconds before proceeding\n");
1004 mdelay(5000);
lijuangbdd9bb42016-03-01 18:22:17 +08001005#endif
Mayank Grover889be1b2017-09-12 20:12:23 +05301006 }
lijuangbdd9bb42016-03-01 18:22:17 +08001007 }
lijuangbdd9bb42016-03-01 18:22:17 +08001008#endif
1009
1010#if VERIFIED_BOOT
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001011 /* Write protect the device info */
Channagoud Kadabi3bd9d1e2015-05-05 16:18:20 -07001012 if (!boot_into_recovery && target_build_variant_user() && devinfo_present && mmc_write_protect("devinfo", 1))
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001013 {
1014 dprintf(INFO, "Failed to write protect dev info\n");
1015 ASSERT(0);
1016 }
1017#endif
1018
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08001019 /* Turn off splash screen if enabled */
1020#if DISPLAY_SPLASH_SCREEN
1021 target_display_shutdown();
1022#endif
1023
Veera Sundaram Sankaran67ea0932015-09-25 10:09:30 -07001024 /* Perform target specific cleanup */
1025 target_uninit();
Monika Singh292b3e92018-03-17 22:40:23 +05301026 free_verified_boot_resource(&info);
1027 if (final_cmdline)
1028 free(final_cmdline);
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08001029
Deepa Dinamani33734bc2013-03-06 12:16:06 -08001030 dprintf(INFO, "booting linux @ %p, ramdisk @ %p (%d), tags/device tree @ %p\n",
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301031 entry, ramdisk, ramdisk_size, (void *)tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001032
1033 enter_critical_section();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001034
Amol Jadi4421e652011-06-16 15:00:48 -07001035 /* do any platform specific cleanup before kernel entry */
1036 platform_uninit();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001037
Brian Swetland9c4c0752009-01-25 16:23:50 -08001038 arch_disable_cache(UCACHE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001039
Amol Jadi504f9fe2012-08-16 13:56:48 -07001040#if ARM_WITH_MMU
Brian Swetland9c4c0752009-01-25 16:23:50 -08001041 arch_disable_mmu();
Amol Jadi504f9fe2012-08-16 13:56:48 -07001042#endif
Amol Jadi492d5a52013-03-15 16:12:34 -07001043 bs_set_timestamp(BS_KERNEL_ENTRY);
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001044
1045 if (IS_ARM64(kptr))
1046 /* Jump to a 64bit kernel */
1047 scm_elexec_call((paddr_t)kernel, tags_phys);
1048 else
1049 /* Jump to a 32bit kernel */
1050 entry(0, machtype, (unsigned*)tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001051}
1052
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001053/* Function to check if the memory address range falls within the aboot
1054 * boundaries.
1055 * start: Start of the memory region
1056 * size: Size of the memory region
1057 */
Vijay Kumar Pendoti9c002ad2016-03-09 13:52:45 +05301058int check_aboot_addr_range_overlap(uintptr_t start, uint32_t size)
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001059{
1060 /* Check for boundary conditions. */
Sundarajan Srinivasance2a0ea2013-12-16 17:02:56 -08001061 if ((UINT_MAX - start) < size)
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001062 return -1;
1063
1064 /* Check for memory overlap. */
1065 if ((start < MEMBASE) && ((start + size) <= MEMBASE))
1066 return 0;
Channagoud Kadabi94143912013-10-15 12:53:52 -07001067 else if (start >= (MEMBASE + MEMSIZE))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001068 return 0;
1069 else
1070 return -1;
1071}
1072
Mayank Grovere559cec2017-10-17 15:12:03 +05301073/* Function to check if the memory address range falls beyond ddr region.
1074 * start: Start of the memory region
1075 * size: Size of the memory region
1076 */
1077int check_ddr_addr_range_bound(uintptr_t start, uint32_t size)
1078{
1079 uintptr_t ddr_pa_start_addr = PA(get_ddr_start());
1080 uint64_t ddr_size = smem_get_ddr_size();
1081 uint64_t ddr_pa_end_addr = ddr_pa_start_addr + ddr_size;
1082 uintptr_t pa_start_addr = PA(start);
1083
1084 /* Check for boundary conditions. */
1085 if ((UINT_MAX - start) < size)
1086 return -1;
1087
1088 /* Check if memory range is beyond the ddr range. */
1089 if (pa_start_addr < ddr_pa_start_addr ||
1090 pa_start_addr >= (ddr_pa_end_addr) ||
1091 (pa_start_addr + size) > ddr_pa_end_addr)
1092 return -1;
1093 else
1094 return 0;
1095}
1096
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001097BUF_DMA_ALIGN(buf, BOOT_IMG_MAX_PAGE_SIZE); //Equal to max-supported pagesize
Dima Zavin214cc642009-01-26 11:16:21 -08001098
Monika Singh292b3e92018-03-17 22:40:23 +05301099int getimage(const bootinfo *info, void **image_buffer, uint32_t *imgsize,
1100 char *imgname)
1101{
1102 if (info == NULL || image_buffer == NULL || imgsize == NULL ||
1103 imgname == NULL) {
1104 dprintf(CRITICAL, "getimage: invalid parameters\n");
1105 return -1;
1106 }
1107
1108 for (uint32_t loadedindex = 0; loadedindex < info->num_loaded_images; loadedindex++) {
1109 if (!strncmp(info->images[loadedindex].name, imgname,
1110 strlen(imgname))) {
1111 *image_buffer = info->images[loadedindex].image_buffer;
1112 *imgsize = info->images[loadedindex].imgsize;
1113 return 0;
1114 }
1115 }
1116 return -1;
1117}
1118
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001119static void verify_signed_bootimg(uint32_t bootimg_addr, uint32_t bootimg_size)
1120{
1121 int ret;
Channagoud Kadabia8c623f2015-01-13 14:48:48 -08001122
1123#if !VERIFIED_BOOT
Sundarajan Srinivasance54d6e2013-11-11 12:45:00 -08001124#if IMAGE_VERIF_ALGO_SHA1
1125 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA1;
1126#else
1127 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA256;
1128#endif
Channagoud Kadabia8c623f2015-01-13 14:48:48 -08001129#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001130
1131 /* Assume device is rooted at this time. */
1132 device.is_tampered = 1;
1133
1134 dprintf(INFO, "Authenticating boot image (%d): start\n", bootimg_size);
1135
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001136#if VERIFIED_BOOT
Monika Singh7d2fc272018-03-16 17:16:01 +05301137 uint32_t bootstate;
1138 if(boot_into_recovery &&
Mayank Grover68fbf0d2017-10-24 14:13:39 +05301139 (!partition_multislot_is_supported()))
Monika Singh7d2fc272018-03-16 17:16:01 +05301140 {
1141 ret = boot_verify_image((unsigned char *)bootimg_addr,
1142 bootimg_size, "/recovery", &bootstate);
1143 }
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001144 else
Monika Singh7d2fc272018-03-16 17:16:01 +05301145 {
1146 ret = boot_verify_image((unsigned char *)bootimg_addr,
1147 bootimg_size, "/boot", &bootstate);
1148 }
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001149 boot_verify_print_state();
1150#else
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001151 ret = image_verify((unsigned char *)bootimg_addr,
1152 (unsigned char *)(bootimg_addr + bootimg_size),
1153 bootimg_size,
Sundarajan Srinivasance54d6e2013-11-11 12:45:00 -08001154 auth_algo);
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001155#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001156 dprintf(INFO, "Authenticating boot image: done return value = %d\n", ret);
1157
1158 if (ret)
1159 {
1160 /* Authorized kernel */
1161 device.is_tampered = 0;
Sundarajan Srinivasan3fb21f12013-09-16 18:36:15 -07001162 auth_kernel_img = 1;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001163 }
1164
Amit Blay4aa292f2015-04-28 21:55:59 +03001165#ifdef MDTP_SUPPORT
1166 {
1167 /* Verify MDTP lock.
1168 * For boot & recovery partitions, use aboot's verification result.
1169 */
1170 mdtp_ext_partition_verification_t ext_partition;
1171 ext_partition.partition = boot_into_recovery ? MDTP_PARTITION_RECOVERY : MDTP_PARTITION_BOOT;
1172 ext_partition.integrity_state = device.is_tampered ? MDTP_PARTITION_STATE_INVALID : MDTP_PARTITION_STATE_VALID;
1173 ext_partition.page_size = 0; /* Not needed since already validated */
1174 ext_partition.image_addr = 0; /* Not needed since already validated */
1175 ext_partition.image_size = 0; /* Not needed since already validated */
1176 ext_partition.sig_avail = FALSE; /* Not needed since already validated */
1177 mdtp_fwlock_verify_lock(&ext_partition);
1178 }
1179#endif /* MDTP_SUPPORT */
1180
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001181#if USE_PCOM_SECBOOT
1182 set_tamper_flag(device.is_tampered);
1183#endif
1184
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001185#if VERIFIED_BOOT
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001186 switch(boot_verify_get_state())
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001187 {
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001188 case RED:
lijuanga40d6302015-07-20 20:10:13 +08001189#if FBCON_DISPLAY_MSG
lijuang9a7d3b92015-11-30 14:41:24 +08001190 display_bootverify_menu(DISPLAY_MENU_RED);
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07001191#if ENABLE_VB_ATTEST
1192 mdelay(DELAY_WAIT);
1193 shutdown_device();
1194#else
lijuanga40d6302015-07-20 20:10:13 +08001195 wait_for_users_action();
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07001196#endif
lijuanga40d6302015-07-20 20:10:13 +08001197#else
1198 dprintf(CRITICAL,
1199 "Your device has failed verification and may not work properly.\nWait for 5 seconds before proceeding\n");
1200 mdelay(5000);
1201#endif
1202
1203 break;
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001204 case YELLOW:
lijuanga40d6302015-07-20 20:10:13 +08001205#if FBCON_DISPLAY_MSG
lijuang9a7d3b92015-11-30 14:41:24 +08001206 display_bootverify_menu(DISPLAY_MENU_YELLOW);
lijuanga40d6302015-07-20 20:10:13 +08001207 wait_for_users_action();
1208#else
1209 dprintf(CRITICAL,
1210 "Your device has loaded a different operating system.\nWait for 5 seconds before proceeding\n");
1211 mdelay(5000);
1212#endif
1213 break;
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001214 default:
lijuanga40d6302015-07-20 20:10:13 +08001215 break;
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001216 }
1217#endif
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001218#if !VERIFIED_BOOT
Unnati Gandhi1be04752015-03-27 19:41:53 +05301219 if(device.is_tampered)
1220 {
1221 write_device_info_mmc(&device);
1222 #ifdef TZ_TAMPER_FUSE
1223 set_tamper_fuse_cmd();
1224 #endif
1225 #ifdef ASSERT_ON_TAMPER
1226 dprintf(CRITICAL, "Device is tampered. Asserting..\n");
1227 ASSERT(0);
1228 #endif
1229 }
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001230#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001231}
1232
Monika Singh292b3e92018-03-17 22:40:23 +05301233int get_boot_image_info(void **image_buffer, uint32_t *imgsize,char *imgname)
1234{
1235 if (image_buffer == NULL || imgsize == NULL || imgname == NULL) {
1236 dprintf(CRITICAL, "get_boot_image_info: invalid parameters\n");
1237 return -1;
1238 }
1239
1240 if (!strncmp(info.images[0].name, imgname,
1241 strlen(imgname))) {
1242 *image_buffer = info.images[0].image_buffer;
1243 *imgsize = info.images[0].imgsize;
1244 return 0;
1245 }
1246 return -1;
1247}
1248
Maunik Shah0f3c8ac2014-03-02 17:47:58 +05301249static bool check_format_bit()
1250{
1251 bool ret = false;
1252 int index;
1253 uint64_t offset;
1254 struct boot_selection_info *in = NULL;
1255 char *buf = NULL;
1256
1257 index = partition_get_index("bootselect");
1258 if (index == INVALID_PTN)
1259 {
1260 dprintf(INFO, "Unable to locate /bootselect partition\n");
1261 return ret;
1262 }
1263 offset = partition_get_offset(index);
1264 if(!offset)
1265 {
1266 dprintf(INFO, "partition /bootselect doesn't exist\n");
1267 return ret;
1268 }
1269 buf = (char *) memalign(CACHE_LINE, ROUNDUP(page_size, CACHE_LINE));
Mayank Grover48860402016-11-29 12:34:53 +05301270 mmc_set_lun(partition_get_lun(index));
Maunik Shah0f3c8ac2014-03-02 17:47:58 +05301271 ASSERT(buf);
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301272 if (mmc_read(offset, (uint32_t *)buf, page_size))
Maunik Shah0f3c8ac2014-03-02 17:47:58 +05301273 {
1274 dprintf(INFO, "mmc read failure /bootselect %d\n", page_size);
1275 free(buf);
1276 return ret;
1277 }
1278 in = (struct boot_selection_info *) buf;
1279 if ((in->signature == BOOTSELECT_SIGNATURE) &&
1280 (in->version == BOOTSELECT_VERSION)) {
1281 if ((in->state_info & BOOTSELECT_FORMAT) &&
1282 !(in->state_info & BOOTSELECT_FACTORY))
1283 ret = true;
1284 } else {
1285 dprintf(CRITICAL, "Signature: 0x%08x or version: 0x%08x mismatched of /bootselect\n",
1286 in->signature, in->version);
1287 ASSERT(0);
1288 }
1289 free(buf);
1290 return ret;
1291}
1292
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001293void boot_verifier_init()
1294{
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001295 uint32_t boot_state;
1296 /* Check if device unlock */
1297 if(device.is_unlocked)
1298 {
1299 boot_verify_send_event(DEV_UNLOCK);
1300 boot_verify_print_state();
1301 dprintf(CRITICAL, "Device is unlocked! Skipping verification...\n");
1302 return;
1303 }
1304 else
1305 {
1306 boot_verify_send_event(BOOT_INIT);
1307 }
1308
1309 /* Initialize keystore */
1310 boot_state = boot_verify_keystore_init();
1311 if(boot_state == YELLOW)
1312 {
1313 boot_verify_print_state();
1314 dprintf(CRITICAL, "Keystore verification failed! Continuing anyways...\n");
1315 }
1316}
1317
Shashank Mittal23b8f422010-04-16 19:27:21 -07001318int boot_linux_from_mmc(void)
1319{
1320 struct boot_img_hdr *hdr = (void*) buf;
1321 struct boot_img_hdr *uhdr;
1322 unsigned offset = 0;
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07001323 int rcode;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001324 unsigned long long ptn = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001325 int index = INVALID_PTN;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001326
Shashank Mittalcd98d472011-08-02 14:29:24 -07001327 unsigned char *image_addr = 0;
1328 unsigned kernel_actual;
1329 unsigned ramdisk_actual;
1330 unsigned imagesize_actual;
Neeti Desai465491e2012-07-31 12:53:35 -07001331 unsigned second_actual = 0;
Neeti Desai465491e2012-07-31 12:53:35 -07001332
Matthew Qin271927e2015-03-31 22:07:07 -04001333 unsigned int dtb_size = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08001334 unsigned int out_len = 0;
1335 unsigned int out_avai_len = 0;
1336 unsigned char *out_addr = NULL;
1337 uint32_t dtb_offset = 0;
1338 unsigned char *kernel_start_addr = NULL;
1339 unsigned int kernel_size = 0;
Ameya Thakur10a33452016-06-13 14:24:26 -07001340 unsigned int patched_kernel_hdr_size = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08001341 int rc;
Monika Singh292b3e92018-03-17 22:40:23 +05301342#if VERIFIED_BOOT_2
1343 int status;
1344#endif
Mayank Groverc93cad82017-10-03 12:23:45 +05301345 char *ptn_name = NULL;
Neeti Desai465491e2012-07-31 12:53:35 -07001346#if DEVICE_TREE
1347 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -07001348 struct dt_entry dt_entry;
Neeti Desai465491e2012-07-31 12:53:35 -07001349 unsigned dt_table_offset;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001350 uint32_t dt_actual;
Deepa Dinamani19648b42013-09-05 17:05:55 -07001351 uint32_t dt_hdr_size;
Matthew Qin271927e2015-03-31 22:07:07 -04001352 unsigned char *best_match_dt_addr = NULL;
Neeti Desai465491e2012-07-31 12:53:35 -07001353#endif
Matthew Qin49e51fa2015-02-09 10:40:45 +08001354 struct kernel64_hdr *kptr = NULL;
Mayank Grover351a75e2017-05-30 20:06:08 +05301355 int current_active_slot = INVALID;
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001356
Maunik Shah0f3c8ac2014-03-02 17:47:58 +05301357 if (check_format_bit())
1358 boot_into_recovery = 1;
1359
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07001360 if (!boot_into_recovery) {
1361 memset(ffbm_mode_string, '\0', sizeof(ffbm_mode_string));
1362 rcode = get_ffbm(ffbm_mode_string, sizeof(ffbm_mode_string));
1363 if (rcode <= 0) {
1364 boot_into_ffbm = false;
1365 if (rcode < 0)
1366 dprintf(CRITICAL,"failed to get ffbm cookie");
1367 } else
1368 boot_into_ffbm = true;
1369 } else
1370 boot_into_ffbm = false;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001371 uhdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
1372 if (!memcmp(uhdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1373 dprintf(INFO, "Unified boot method!\n");
1374 hdr = uhdr;
1375 goto unified_boot;
1376 }
Mayank Groverc93cad82017-10-03 12:23:45 +05301377
1378 /* For a/b recovery image code is on boot partition.
1379 If we support multislot, always use boot partition. */
1380 if (boot_into_recovery &&
1381 (!partition_multislot_is_supported()))
1382 ptn_name = "recovery";
1383 else
1384 ptn_name = "boot";
1385
1386 index = partition_get_index(ptn_name);
1387 ptn = partition_get_offset(index);
1388 if(ptn == 0) {
1389 dprintf(CRITICAL, "ERROR: No %s partition found\n", ptn_name);
1390 return -1;
Kinson Chikf1a43512011-07-14 11:28:39 -07001391 }
Mayank Groverc93cad82017-10-03 12:23:45 +05301392
Channagoud Kadabief0547c2015-02-10 12:57:38 -08001393 /* Set Lun for boot & recovery partitions */
1394 mmc_set_lun(partition_get_lun(index));
Shashank Mittal23b8f422010-04-16 19:27:21 -07001395
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301396 if (mmc_read(ptn + offset, (uint32_t *) buf, page_size)) {
Shashank Mittal23b8f422010-04-16 19:27:21 -07001397 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
1398 return -1;
1399 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001400
1401 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001402 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Mayank Grover351a75e2017-05-30 20:06:08 +05301403 return ERR_INVALID_BOOT_MAGIC;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001404 }
1405
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07001406 if (hdr->page_size && (hdr->page_size != page_size)) {
vijay kumar2e21b3a2014-06-26 17:40:15 +05301407
1408 if (hdr->page_size > BOOT_IMG_MAX_PAGE_SIZE) {
1409 dprintf(CRITICAL, "ERROR: Invalid page size\n");
1410 return -1;
1411 }
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07001412 page_size = hdr->page_size;
1413 page_mask = page_size - 1;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001414 }
1415
Matthew Qin49e51fa2015-02-09 10:40:45 +08001416 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1417 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Kishor PKd8ddcad2017-07-27 13:53:57 +05301418 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001419
Matthew Qin49e51fa2015-02-09 10:40:45 +08001420 image_addr = (unsigned char *)target_get_scratch_address();
Kishor PK9e91b592017-05-24 12:14:55 +05301421 memcpy(image_addr, (void *)buf, page_size);
1422
1423 /* ensure commandline is terminated */
1424 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
Matthew Qin49e51fa2015-02-09 10:40:45 +08001425
1426#if DEVICE_TREE
Parth Dixit4097b622016-03-15 14:42:27 +05301427#ifndef OSVERSION_IN_BOOTIMAGE
1428 dt_size = hdr->dt_size;
1429#endif
1430 dt_actual = ROUND_TO_PAGE(dt_size, page_mask);
Kishor PKd8ddcad2017-07-27 13:53:57 +05301431 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 +05301432 dprintf(CRITICAL, "Integer overflow detected in bootimage header fields at %u in %s\n",__LINE__,__FILE__);
1433 return -1;
1434 }
Kishor PKd8ddcad2017-07-27 13:53:57 +05301435 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + second_actual + dt_actual);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001436#else
Kishor PKd8ddcad2017-07-27 13:53:57 +05301437 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 +05301438 dprintf(CRITICAL, "Integer overflow detected in bootimage header fields at %u in %s\n",__LINE__,__FILE__);
1439 return -1;
1440 }
Kishor PKd8ddcad2017-07-27 13:53:57 +05301441 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + second_actual);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001442#endif
1443
1444#if VERIFIED_BOOT
1445 boot_verifier_init();
1446#endif
1447
Vijay Kumar Pendoti9c002ad2016-03-09 13:52:45 +05301448 if (check_aboot_addr_range_overlap((uintptr_t) image_addr, imagesize_actual))
Matthew Qin49e51fa2015-02-09 10:40:45 +08001449 {
1450 dprintf(CRITICAL, "Boot image buffer address overlaps with aboot addresses.\n");
1451 return -1;
1452 }
1453
Matthew Qinbb7923d2015-02-09 10:56:09 +08001454 /*
1455 * Update loading flow of bootimage to support compressed/uncompressed
1456 * bootimage on both 64bit and 32bit platform.
1457 * 1. Load bootimage from emmc partition onto DDR.
1458 * 2. Check if bootimage is gzip format. If yes, decompress compressed kernel
1459 * 3. Check kernel header and update kernel load addr for 64bit and 32bit
1460 * platform accordingly.
1461 * 4. Sanity Check on kernel_addr and ramdisk_addr and copy data.
1462 */
Mayank Grover351a75e2017-05-30 20:06:08 +05301463 if (partition_multislot_is_supported())
1464 {
1465 current_active_slot = partition_find_active_slot();
1466 dprintf(INFO, "Loading boot image (%d) active_slot(%s): start\n",
1467 imagesize_actual, SUFFIX_SLOT(current_active_slot));
1468 }
1469 else
1470 {
1471 dprintf(INFO, "Loading (%s) image (%d): start\n",
1472 (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual);
1473 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08001474 bs_set_timestamp(BS_KERNEL_LOAD_START);
1475
Gaurav Nebhwani43e2a462016-03-17 21:32:56 +05301476 if ((target_get_max_flash_size() - page_size) < imagesize_actual)
1477 {
1478 dprintf(CRITICAL, "booimage size is greater than DDR can hold\n");
1479 return -1;
1480 }
Kishor PK9e91b592017-05-24 12:14:55 +05301481 offset = page_size;
1482 /* Read image without signature and header*/
1483 if (mmc_read(ptn + offset, (void *)(image_addr + offset), imagesize_actual - page_size))
Matthew Qin49e51fa2015-02-09 10:40:45 +08001484 {
1485 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
1486 return -1;
1487 }
1488
Mayank Grover351a75e2017-05-30 20:06:08 +05301489 if (partition_multislot_is_supported())
1490 {
1491 dprintf(INFO, "Loading boot image (%d) active_slot(%s): done\n",
1492 imagesize_actual, SUFFIX_SLOT(current_active_slot));
1493 }
1494 else
1495 {
1496 dprintf(INFO, "Loading (%s) image (%d): done\n",
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001497 (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual);
1498
Mayank Grover351a75e2017-05-30 20:06:08 +05301499 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08001500 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
1501
1502 /* Authenticate Kernel */
1503 dprintf(INFO, "use_signed_kernel=%d, is_unlocked=%d, is_tampered=%d.\n",
1504 (int) target_use_signed_kernel(),
1505 device.is_unlocked,
1506 device.is_tampered);
Monika Singh292b3e92018-03-17 22:40:23 +05301507#if VERIFIED_BOOT_2
1508 offset = imagesize_actual;
1509 if (check_aboot_addr_range_overlap((uintptr_t)image_addr + offset, page_size))
1510 {
1511 dprintf(CRITICAL, "Signature read buffer address overlaps with aboot addresses.\n");
1512 return -1;
1513 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08001514
Monika Singh292b3e92018-03-17 22:40:23 +05301515 /* Read signature */
1516 if(mmc_read(ptn + offset, (void *)(image_addr + offset), page_size))
1517 {
1518 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
1519 return -1;
1520 }
1521
1522 memset(&info, 0, sizeof(bootinfo));
1523 info.images[0].image_buffer = image_addr;
1524 info.images[0].imgsize = imagesize_actual;
1525 info.images[0].name = "boot";
1526 info.num_loaded_images = 0;
1527 info.multi_slot_boot = partition_multislot_is_supported();
1528 info.bootreason_alarm = boot_reason_alarm;
1529 info.bootinto_recovery = boot_into_recovery;
1530 status = load_image_and_auth(&info);
1531 if(status)
1532 return -1;
1533
1534 vbcmdline = info.vbcmdline;
1535#else
Channagoud Kadabi736c4962015-08-21 11:56:52 -07001536 /* Change the condition a little bit to include the test framework support.
1537 * We would never reach this point if device is in fastboot mode, even if we did
1538 * that means we are in test mode, so execute kernel authentication part for the
1539 * tests */
Parth Dixitcb0c6082015-12-30 15:01:13 +05301540 if((target_use_signed_kernel() && (!device.is_unlocked)) || is_test_mode_enabled())
Matthew Qin49e51fa2015-02-09 10:40:45 +08001541 {
1542 offset = imagesize_actual;
Vijay Kumar Pendoti9c002ad2016-03-09 13:52:45 +05301543 if (check_aboot_addr_range_overlap((uintptr_t)image_addr + offset, page_size))
Matthew Qin49e51fa2015-02-09 10:40:45 +08001544 {
1545 dprintf(CRITICAL, "Signature read buffer address overlaps with aboot addresses.\n");
1546 return -1;
1547 }
1548
1549 /* Read signature */
1550 if(mmc_read(ptn + offset, (void *)(image_addr + offset), page_size))
1551 {
1552 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
1553 return -1;
1554 }
1555
1556 verify_signed_bootimg((uint32_t)image_addr, imagesize_actual);
Channagoud Kadabi736c4962015-08-21 11:56:52 -07001557 /* The purpose of our test is done here */
Parth Dixitcb0c6082015-12-30 15:01:13 +05301558 if(is_test_mode_enabled() && auth_kernel_img)
Channagoud Kadabi736c4962015-08-21 11:56:52 -07001559 return 0;
Matthew Qin49e51fa2015-02-09 10:40:45 +08001560 } else {
1561 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
1562 #ifdef TZ_SAVE_KERNEL_HASH
1563 aboot_save_boot_hash_mmc((uint32_t) image_addr, imagesize_actual);
1564 #endif /* TZ_SAVE_KERNEL_HASH */
Amit Blay4aa292f2015-04-28 21:55:59 +03001565
1566#ifdef MDTP_SUPPORT
1567 {
1568 /* Verify MDTP lock.
1569 * For boot & recovery partitions, MDTP will use boot_verifier APIs,
1570 * since verification was skipped in aboot. The signature is not part of the loaded image.
1571 */
1572 mdtp_ext_partition_verification_t ext_partition;
1573 ext_partition.partition = boot_into_recovery ? MDTP_PARTITION_RECOVERY : MDTP_PARTITION_BOOT;
1574 ext_partition.integrity_state = MDTP_PARTITION_STATE_UNSET;
1575 ext_partition.page_size = page_size;
1576 ext_partition.image_addr = (uint32)image_addr;
1577 ext_partition.image_size = imagesize_actual;
1578 ext_partition.sig_avail = FALSE;
1579 mdtp_fwlock_verify_lock(&ext_partition);
1580 }
1581#endif /* MDTP_SUPPORT */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001582 }
Monika Singh292b3e92018-03-17 22:40:23 +05301583#endif
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001584
Sridhar Parasuram00bfedb2015-05-26 14:21:27 -07001585#if VERIFIED_BOOT
taozhang93088bd2016-11-16 15:50:46 +08001586 if((boot_verify_get_state() == ORANGE) && (!boot_into_ffbm))
Reut Zysmanba8a9d52016-02-18 11:44:04 +02001587 {
1588#if FBCON_DISPLAY_MSG
1589 display_bootverify_menu(DISPLAY_MENU_ORANGE);
1590 wait_for_users_action();
1591#else
1592 dprintf(CRITICAL,
1593 "Your device has been unlocked and can't be trusted.\nWait for 5 seconds before proceeding\n");
1594 mdelay(5000);
1595#endif
1596 }
1597#endif
1598
1599#if VERIFIED_BOOT
Monika Singh292b3e92018-03-17 22:40:23 +05301600 if (VB_M == target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +05301601 {
1602 /* set boot and system versions. */
1603 set_os_version((unsigned char *)image_addr);
1604 // send root of trust
1605 if(!send_rot_command((uint32_t)device.is_unlocked))
1606 ASSERT(0);
1607 }
Parth Dixitbc9b6492015-10-18 00:41:38 +05301608#endif
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001609 /*
Matthew Qinbb7923d2015-02-09 10:56:09 +08001610 * Check if the kernel image is a gzip package. If yes, need to decompress it.
1611 * If not, continue booting.
1612 */
1613 if (is_gzip_package((unsigned char *)(image_addr + page_size), hdr->kernel_size))
1614 {
1615 out_addr = (unsigned char *)(image_addr + imagesize_actual + page_size);
1616 out_avai_len = target_get_max_flash_size() - imagesize_actual - page_size;
Matthew Qin0b15b322015-05-19 05:20:54 -04001617 dprintf(INFO, "decompressing kernel image: start\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001618 rc = decompress((unsigned char *)(image_addr + page_size),
1619 hdr->kernel_size, out_addr, out_avai_len,
1620 &dtb_offset, &out_len);
1621 if (rc)
1622 {
Matthew Qin0b15b322015-05-19 05:20:54 -04001623 dprintf(CRITICAL, "decompressing kernel image failed!!!\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001624 ASSERT(0);
1625 }
1626
Matthew Qin0b15b322015-05-19 05:20:54 -04001627 dprintf(INFO, "decompressing kernel image: done\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001628 kptr = (struct kernel64_hdr *)out_addr;
1629 kernel_start_addr = out_addr;
1630 kernel_size = out_len;
1631 } else {
Ameya Thakur10a33452016-06-13 14:24:26 -07001632 dprintf(INFO, "Uncpmpressed kernel in use\n");
1633 if (!strncmp((char*)(image_addr + page_size),
1634 PATCHED_KERNEL_MAGIC,
1635 sizeof(PATCHED_KERNEL_MAGIC) - 1)) {
1636 dprintf(INFO, "Patched kernel detected\n");
1637 kptr = (struct kernel64_hdr *)(image_addr + page_size +
1638 PATCHED_KERNEL_HEADER_SIZE);
1639 //The size of the kernel is stored at start of kernel image + 16
1640 //The dtb would start just after the kernel
1641 dtb_offset = *((uint32_t*)((unsigned char*)
1642 (image_addr + page_size +
1643 sizeof(PATCHED_KERNEL_MAGIC) -
1644 1)));
1645 //The actual kernel starts after the 20 byte header.
1646 kernel_start_addr = (unsigned char*)(image_addr +
1647 page_size + PATCHED_KERNEL_HEADER_SIZE);
1648 kernel_size = hdr->kernel_size;
1649 patched_kernel_hdr_size = PATCHED_KERNEL_HEADER_SIZE;
1650 } else {
1651 dprintf(INFO, "Kernel image not patched..Unable to locate dt offset\n");
1652 kptr = (struct kernel64_hdr *)(image_addr + page_size);
1653 kernel_start_addr = (unsigned char *)(image_addr + page_size);
1654 kernel_size = hdr->kernel_size;
1655 }
Matthew Qinbb7923d2015-02-09 10:56:09 +08001656 }
1657
1658 /*
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001659 * Update the kernel/ramdisk/tags address if the boot image header
1660 * has default values, these default values come from mkbootimg when
1661 * the boot image is flashed using fastboot flash:raw
1662 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001663 update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr));
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001664
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001665 /* Get virtual addresses since the hdr saves physical addresses. */
1666 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
1667 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
1668 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001669
Matthew Qinbb7923d2015-02-09 10:56:09 +08001670 kernel_size = ROUND_TO_PAGE(kernel_size, page_mask);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001671 /* Check if the addresses in the header are valid. */
Matthew Qinbb7923d2015-02-09 10:56:09 +08001672 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_size) ||
Mayank Grovere559cec2017-10-17 15:12:03 +05301673 check_ddr_addr_range_bound(hdr->kernel_addr, kernel_size) ||
1674 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual) ||
1675 check_ddr_addr_range_bound(hdr->ramdisk_addr, ramdisk_actual))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001676 {
Mayank Grovere559cec2017-10-17 15:12:03 +05301677 dprintf(CRITICAL, "kernel/ramdisk addresses are not valid.\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001678 return -1;
1679 }
1680
1681#ifndef DEVICE_TREE
Mayank Grovere559cec2017-10-17 15:12:03 +05301682 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE) ||
1683 check_ddr_addr_range_bound(hdr->tags_addr, MAX_TAGS_SIZE))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001684 {
Mayank Grovere559cec2017-10-17 15:12:03 +05301685 dprintf(CRITICAL, "Tags addresses are not valid.\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001686 return -1;
1687 }
1688#endif
1689
Matthew Qin49e51fa2015-02-09 10:40:45 +08001690 /* Move kernel, ramdisk and device tree to correct address */
Matthew Qinbb7923d2015-02-09 10:56:09 +08001691 memmove((void*) hdr->kernel_addr, kernel_start_addr, kernel_size);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001692 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001693
Matthew Qin49e51fa2015-02-09 10:40:45 +08001694 #if DEVICE_TREE
Parth Dixit4097b622016-03-15 14:42:27 +05301695 if(dt_size) {
Matthew Qin49e51fa2015-02-09 10:40:45 +08001696 dt_table_offset = ((uint32_t)image_addr + page_size + kernel_actual + ramdisk_actual + second_actual);
1697 table = (struct dt_table*) dt_table_offset;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001698
Matthew Qin49e51fa2015-02-09 10:40:45 +08001699 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
1700 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1701 return -1;
1702 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001703
P.V. Phani Kumar82916762016-03-09 09:20:19 +05301704 /* Its Error if, dt_hdr_size (table->num_entries * dt_entry size + Dev_Tree Header)
1705 goes beyound hdr->dt_size*/
Parth Dixit4097b622016-03-15 14:42:27 +05301706 if (dt_hdr_size > ROUND_TO_PAGE(dt_size,hdr->page_size)) {
P.V. Phani Kumar82916762016-03-09 09:20:19 +05301707 dprintf(CRITICAL, "ERROR: Invalid Device Tree size \n");
1708 return -1;
1709 }
1710
Matthew Qin49e51fa2015-02-09 10:40:45 +08001711 /* Find index of device tree within device tree table */
1712 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
1713 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1714 return -1;
1715 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001716
P.V. Phani Kumar3c333202016-03-09 11:54:37 +05301717 if(dt_entry.offset > (UINT_MAX - dt_entry.size)) {
1718 dprintf(CRITICAL, "ERROR: Device tree contents are Invalid\n");
1719 return -1;
1720 }
1721
1722 /* Ensure we are not overshooting dt_size with the dt_entry selected */
Parth Dixit4097b622016-03-15 14:42:27 +05301723 if ((dt_entry.offset + dt_entry.size) > dt_size) {
P.V. Phani Kumar3c333202016-03-09 11:54:37 +05301724 dprintf(CRITICAL, "ERROR: Device tree contents are Invalid\n");
1725 return -1;
1726 }
1727
Matthew Qin271927e2015-03-31 22:07:07 -04001728 if (is_gzip_package((unsigned char *)dt_table_offset + dt_entry.offset, dt_entry.size))
1729 {
1730 unsigned int compressed_size = 0;
1731 out_addr += out_len;
1732 out_avai_len -= out_len;
Matthew Qin0b15b322015-05-19 05:20:54 -04001733 dprintf(INFO, "decompressing dtb: start\n");
Matthew Qin271927e2015-03-31 22:07:07 -04001734 rc = decompress((unsigned char *)dt_table_offset + dt_entry.offset,
1735 dt_entry.size, out_addr, out_avai_len,
1736 &compressed_size, &dtb_size);
1737 if (rc)
1738 {
Matthew Qin0b15b322015-05-19 05:20:54 -04001739 dprintf(CRITICAL, "decompressing dtb failed!!!\n");
Matthew Qin271927e2015-03-31 22:07:07 -04001740 ASSERT(0);
1741 }
1742
Matthew Qin0b15b322015-05-19 05:20:54 -04001743 dprintf(INFO, "decompressing dtb: done\n");
Matthew Qin271927e2015-03-31 22:07:07 -04001744 best_match_dt_addr = out_addr;
1745 } else {
1746 best_match_dt_addr = (unsigned char *)dt_table_offset + dt_entry.offset;
1747 dtb_size = dt_entry.size;
1748 }
1749
Matthew Qin49e51fa2015-02-09 10:40:45 +08001750 /* Validate and Read device device tree in the tags_addr */
Mayank Grovere559cec2017-10-17 15:12:03 +05301751 if (check_aboot_addr_range_overlap(hdr->tags_addr, dtb_size) ||
1752 check_ddr_addr_range_bound(hdr->tags_addr, dtb_size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001753 {
Mayank Grovere559cec2017-10-17 15:12:03 +05301754 dprintf(CRITICAL, "Device tree addresses are not valid\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001755 return -1;
1756 }
Shashank Mittal162244e2011-08-08 19:01:25 -07001757
Matthew Qin271927e2015-03-31 22:07:07 -04001758 memmove((void *)hdr->tags_addr, (char *)best_match_dt_addr, dtb_size);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001759 } else {
1760 /* Validate the tags_addr */
Mayank Grovere559cec2017-10-17 15:12:03 +05301761 if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual) ||
1762 check_ddr_addr_range_bound(hdr->tags_addr, kernel_actual))
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001763 {
Mayank Grovere559cec2017-10-17 15:12:03 +05301764 dprintf(CRITICAL, "Device tree addresses are not valid.\n");
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001765 return -1;
1766 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08001767 /*
1768 * If appended dev tree is found, update the atags with
1769 * memory address to the DTB appended location on RAM.
1770 * Else update with the atags address in the kernel header
1771 */
1772 void *dtb;
Ameya Thakur10a33452016-06-13 14:24:26 -07001773 dtb = dev_tree_appended(
1774 (void*)(image_addr + page_size +
1775 patched_kernel_hdr_size),
1776 hdr->kernel_size, dtb_offset,
1777 (void *)hdr->tags_addr);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001778 if (!dtb) {
1779 dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001780 return -1;
1781 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07001782 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08001783 #endif
Shashank Mittal23b8f422010-04-16 19:27:21 -07001784
Stanimir Varbanov69ec5462013-07-18 18:17:42 +03001785 if (boot_into_recovery && !device.is_unlocked && !device.is_tampered)
1786 target_load_ssd_keystore();
1787
Shashank Mittal23b8f422010-04-16 19:27:21 -07001788unified_boot:
Shashank Mittal23b8f422010-04-16 19:27:21 -07001789
Dima Zavin77e41f32013-03-06 16:10:43 -08001790 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07001791 (const char *)hdr->cmdline, board_machtype(),
Shashank Mittal23b8f422010-04-16 19:27:21 -07001792 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
1793
1794 return 0;
1795}
1796
Dima Zavin214cc642009-01-26 11:16:21 -08001797int boot_linux_from_flash(void)
1798{
1799 struct boot_img_hdr *hdr = (void*) buf;
Dima Zavin214cc642009-01-26 11:16:21 -08001800 struct ptentry *ptn;
1801 struct ptable *ptable;
1802 unsigned offset = 0;
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001803
Shashank Mittalcd98d472011-08-02 14:29:24 -07001804 unsigned char *image_addr = 0;
1805 unsigned kernel_actual;
1806 unsigned ramdisk_actual;
1807 unsigned imagesize_actual;
vijay kumar8f53e362015-11-24 13:38:11 +05301808 unsigned second_actual = 0;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001809
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001810#if DEVICE_TREE
Monika Singh292b3e92018-03-17 22:40:23 +05301811 struct dt_table *table = NULL;
Joel Kingaa335dc2013-06-03 16:11:08 -07001812 struct dt_entry dt_entry;
vijay kumar8f53e362015-11-24 13:38:11 +05301813 unsigned dt_table_offset;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001814 uint32_t dt_actual;
Monika Singh292b3e92018-03-17 22:40:23 +05301815 uint32_t dt_hdr_size = 0;
Vivek Kumar07bd0862018-04-10 14:19:23 +05301816 uint32_t dtb_offset = 0;
vijay kumar8f53e362015-11-24 13:38:11 +05301817 unsigned int dtb_size = 0;
1818 unsigned char *best_match_dt_addr = NULL;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001819#endif
1820
David Ng183a7422009-12-07 14:55:21 -08001821 if (target_is_emmc_boot()) {
1822 hdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
1823 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1824 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
1825 return -1;
1826 }
1827 goto continue_boot;
1828 }
1829
Dima Zavin214cc642009-01-26 11:16:21 -08001830 ptable = flash_get_ptable();
1831 if (ptable == NULL) {
1832 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1833 return -1;
1834 }
1835
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001836 if(!boot_into_recovery)
1837 {
1838 ptn = ptable_find(ptable, "boot");
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001839
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001840 if (ptn == NULL) {
1841 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1842 return -1;
1843 }
1844 }
1845 else
1846 {
1847 ptn = ptable_find(ptable, "recovery");
1848 if (ptn == NULL) {
1849 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
1850 return -1;
1851 }
Dima Zavin214cc642009-01-26 11:16:21 -08001852 }
1853
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05301854 /* Read boot.img header from flash */
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001855 if (flash_read(ptn, offset, buf, page_size)) {
Dima Zavin214cc642009-01-26 11:16:21 -08001856 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
1857 return -1;
1858 }
Dima Zavin214cc642009-01-26 11:16:21 -08001859
1860 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001861 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Dima Zavin214cc642009-01-26 11:16:21 -08001862 return -1;
1863 }
1864
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001865 if (hdr->page_size != page_size) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001866 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 -08001867 return -1;
1868 }
1869
Kishor PK9e91b592017-05-24 12:14:55 +05301870 image_addr = (unsigned char *)target_get_scratch_address();
1871 memcpy(image_addr, (void *)buf, page_size);
vijay kumar287bb542015-09-29 13:01:52 +05301872
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001873 /*
1874 * Update the kernel/ramdisk/tags address if the boot image header
1875 * has default values, these default values come from mkbootimg when
1876 * the boot image is flashed using fastboot flash:raw
1877 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001878 update_ker_tags_rdisk_addr(hdr, false);
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001879
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001880 /* Get virtual addresses since the hdr saves physical addresses. */
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001881 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
1882 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
1883 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
1884
1885 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1886 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Mayank Grover032736f2017-07-14 20:34:51 +05301887 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001888
Kishor PK9e91b592017-05-24 12:14:55 +05301889 /* ensure commandline is terminated */
1890 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
1891
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001892 /* Check if the addresses in the header are valid. */
1893 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
Mayank Grovere559cec2017-10-17 15:12:03 +05301894 check_ddr_addr_range_bound(hdr->kernel_addr, kernel_actual) ||
1895 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual) ||
1896 check_ddr_addr_range_bound(hdr->ramdisk_addr, ramdisk_actual))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001897 {
Mayank Grovere559cec2017-10-17 15:12:03 +05301898 dprintf(CRITICAL, "kernel/ramdisk addresses are not valid.\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001899 return -1;
1900 }
1901
1902#ifndef DEVICE_TREE
Kishor PKd8ddcad2017-07-27 13:53:57 +05301903 if (UINT_MAX < ((uint64_t)kernel_actual + (uint64_t)ramdisk_actual+ (uint64_t)second_actual + page_size)) {
Mayank Grover032736f2017-07-14 20:34:51 +05301904 dprintf(CRITICAL, "Integer overflow detected in bootimage header fields\n");
1905 return -1;
1906 }
Kishor PKd8ddcad2017-07-27 13:53:57 +05301907 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + second_actual);
Mayank Grover032736f2017-07-14 20:34:51 +05301908
Mayank Grovere559cec2017-10-17 15:12:03 +05301909 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE) ||
1910 check_ddr_addr_range_bound(hdr->tags_addr, MAX_TAGS_SIZE))
Vijay Kumar Pendotid3ed20e2016-09-20 00:34:46 +05301911 {
Mayank Grovere559cec2017-10-17 15:12:03 +05301912 dprintf(CRITICAL, "Tags addresses are not valid.\n");
Vijay Kumar Pendotid3ed20e2016-09-20 00:34:46 +05301913 return -1;
1914 }
1915#else
1916
1917#ifndef OSVERSION_IN_BOOTIMAGE
1918 dt_size = hdr->dt_size;
1919#endif
Mayank Grover032736f2017-07-14 20:34:51 +05301920 dt_actual = ROUND_TO_PAGE(dt_size, page_mask);
Kishor PKd8ddcad2017-07-27 13:53:57 +05301921 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 +05301922 dprintf(CRITICAL, "Integer overflow detected in bootimage header fields\n");
1923 return -1;
1924 }
1925
Kishor PKd8ddcad2017-07-27 13:53:57 +05301926 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + second_actual + dt_actual);
Mayank Grover032736f2017-07-14 20:34:51 +05301927
Mayank Grovere559cec2017-10-17 15:12:03 +05301928 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_size) ||
1929 check_ddr_addr_range_bound(hdr->tags_addr, dt_size))
Mayank Grover032736f2017-07-14 20:34:51 +05301930 {
Mayank Grovere559cec2017-10-17 15:12:03 +05301931 dprintf(CRITICAL, "Device tree addresses are not valid.\n");
Mayank Grover032736f2017-07-14 20:34:51 +05301932 return -1;
1933 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001934#endif
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001935
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05301936 /* Read full boot.img from flash */
1937 dprintf(INFO, "Loading (%s) image (%d): start\n",
1938 (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual);
1939 bs_set_timestamp(BS_KERNEL_LOAD_START);
1940
1941 if (UINT_MAX - page_size < imagesize_actual)
1942 {
1943 dprintf(CRITICAL,"Integer overflow detected in bootimage header fields %u %s\n", __LINE__,__func__);
1944 return -1;
1945 }
1946
1947 /*Check the availability of RAM before reading boot image + max signature length from flash*/
1948 if (target_get_max_flash_size() < (imagesize_actual + page_size))
1949 {
1950 dprintf(CRITICAL, "bootimage size is greater than DDR can hold\n");
1951 return -1;
1952 }
1953
1954 offset = page_size;
1955 /* Read image without signature and header */
1956 if (flash_read(ptn, offset, (void *)(image_addr + offset), imagesize_actual - page_size))
1957 {
1958 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
1959 return -1;
1960 }
1961
1962 dprintf(INFO, "Loading (%s) image (%d): done\n",
1963 (!boot_into_recovery ? "boot" : "recovery"), imagesize_actual);
1964 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
1965
Shashank Mittalcd98d472011-08-02 14:29:24 -07001966 /* Authenticate Kernel */
Deepa Dinamani23b60d42013-06-24 18:10:52 -07001967 if(target_use_signed_kernel() && (!device.is_unlocked))
Shashank Mittalcd98d472011-08-02 14:29:24 -07001968 {
Shashank Mittalcd98d472011-08-02 14:29:24 -07001969 offset = imagesize_actual;
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05301970
Shashank Mittalcd98d472011-08-02 14:29:24 -07001971 /* Read signature */
1972 if (flash_read(ptn, offset, (void *)(image_addr + offset), page_size))
1973 {
1974 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001975 return -1;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001976 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07001977
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301978 verify_signed_bootimg((uint32_t)image_addr, imagesize_actual);
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05301979 }
1980 offset = page_size;
1981 if(hdr->second_size != 0) {
1982 if (UINT_MAX - offset < second_actual)
1983 {
1984 dprintf(CRITICAL, "ERROR: Integer overflow in boot image header %s\t%d\n",__func__,__LINE__);
1985 return -1;
vijay kumar8f53e362015-11-24 13:38:11 +05301986 }
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05301987 offset += second_actual;
1988 /* Second image loading not implemented. */
1989 ASSERT(0);
1990 }
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05301991 /* Move kernel and ramdisk to correct address */
1992 memmove((void*) hdr->kernel_addr, (char*) (image_addr + page_size), hdr->kernel_size);
1993 memmove((void*) hdr->ramdisk_addr, (char*) (image_addr + page_size + kernel_actual), hdr->ramdisk_size);
1994
1995#if DEVICE_TREE
1996 if(dt_size != 0) {
1997
1998 dt_table_offset = ((uint32_t)image_addr + page_size + kernel_actual + ramdisk_actual + second_actual);
1999
2000 table = (struct dt_table*) dt_table_offset;
2001
2002 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
2003 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
2004 return -1;
2005 }
2006
2007 /* Its Error if, dt_hdr_size (table->num_entries * dt_entry size + Dev_Tree Header)
2008 goes beyound hdr->dt_size*/
2009 if (dt_hdr_size > ROUND_TO_PAGE(dt_size,hdr->page_size)) {
2010 dprintf(CRITICAL, "ERROR: Invalid Device Tree size \n");
2011 return -1;
2012 }
2013
2014 /* Find index of device tree within device tree table */
2015 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
2016 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
2017 return -1;
2018 }
2019
2020 /* Validate and Read device device tree in the "tags_add */
2021 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size) ||
2022 check_ddr_addr_range_bound(hdr->tags_addr, dt_entry.size))
2023 {
2024 dprintf(CRITICAL, "Device tree addresses are not valid.\n");
2025 return -1;
2026 }
2027
2028 if(dt_entry.offset > (UINT_MAX - dt_entry.size)) {
2029 dprintf(CRITICAL, "ERROR: Device tree contents are Invalid\n");
2030 return -1;
2031 }
2032
2033 /* Ensure we are not overshooting dt_size with the dt_entry selected */
2034 if ((dt_entry.offset + dt_entry.size) > dt_size) {
2035 dprintf(CRITICAL, "ERROR: Device tree contents are Invalid\n");
2036 return -1;
2037 }
2038
2039 best_match_dt_addr = (unsigned char *)table + dt_entry.offset;
2040 dtb_size = dt_entry.size;
2041 memmove((void *)hdr->tags_addr, (char *)best_match_dt_addr, dtb_size);
Vivek Kumar07bd0862018-04-10 14:19:23 +05302042
2043 } else {
2044 /* Validate the tags_addr */
2045 if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual) ||
2046 check_ddr_addr_range_bound(hdr->tags_addr, kernel_actual))
2047 {
2048 dprintf(CRITICAL, "Device tree addresses are not valid.\n");
2049 return -1;
2050 }
2051 /*
2052 * If appended dev tree is found, update the atags with
2053 * memory address to the DTB appended location on RAM.
2054 * Else update with the atags address in the kernel header
2055 */
2056 void *dtb = NULL;
2057 dtb = dev_tree_appended((void*)(image_addr + page_size ),hdr->kernel_size, dtb_offset, (void *)hdr->tags_addr);
2058 if (!dtb) {
2059 dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
2060 return -1;
2061 }
2062 }
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05302063#endif
2064 if(target_use_signed_kernel() && (!device.is_unlocked))
2065 {
Shashank Mittal162244e2011-08-08 19:01:25 -07002066 /* Make sure everything from scratch address is read before next step!*/
Shashank Mittala0032282011-08-26 14:50:11 -07002067 if(device.is_tampered)
Shashank Mittal162244e2011-08-08 19:01:25 -07002068 {
2069 write_device_info_flash(&device);
2070 }
Channagoud Kadabi5c86fe32012-02-16 10:58:48 +05302071#if USE_PCOM_SECBOOT
2072 set_tamper_flag(device.is_tampered);
2073#endif
Shashank Mittalcd98d472011-08-02 14:29:24 -07002074 }
David Ng183a7422009-12-07 14:55:21 -08002075continue_boot:
Dima Zavin214cc642009-01-26 11:16:21 -08002076
Dima Zavin214cc642009-01-26 11:16:21 -08002077 /* TODO: create/pass atags to kernel */
2078
Ajay Dudanie28a6072011-07-01 13:59:46 -07002079 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07002080 (const char *)hdr->cmdline, board_machtype(),
Dima Zavin214cc642009-01-26 11:16:21 -08002081 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
2082
2083 return 0;
2084}
Brian Swetland9c4c0752009-01-25 16:23:50 -08002085
Shashank Mittal162244e2011-08-08 19:01:25 -07002086void write_device_info_mmc(device_info *dev)
2087{
Shashank Mittal162244e2011-08-08 19:01:25 -07002088 unsigned long long ptn = 0;
2089 unsigned long long size;
2090 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002091 uint8_t lun = 0;
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002092 uint32_t ret = 0;
Mayank Groverddd348d2018-01-23 14:07:09 +05302093 uint32_t device_info_sz = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07002094
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002095 if (devinfo_present)
2096 index = partition_get_index("devinfo");
2097 else
2098 index = partition_get_index("aboot");
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07002099
Shashank Mittal162244e2011-08-08 19:01:25 -07002100 ptn = partition_get_offset(index);
2101 if(ptn == 0)
2102 {
2103 return;
2104 }
2105
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002106 lun = partition_get_lun(index);
2107 mmc_set_lun(lun);
2108
Shashank Mittal162244e2011-08-08 19:01:25 -07002109 size = partition_get_size(index);
2110
Mayank Groverddd348d2018-01-23 14:07:09 +05302111 device_info_sz = ROUND_TO_PAGE(sizeof(struct device_info),
2112 mmc_blocksize_mask);
2113 if (device_info_sz == UINT_MAX)
2114 {
2115 dprintf(CRITICAL, "ERROR: Incorrect blocksize of card\n");
2116 return;
2117 }
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07002118
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002119 if (devinfo_present)
Mayank Groverddd348d2018-01-23 14:07:09 +05302120 ret = mmc_write(ptn, device_info_sz, (void *)info_buf);
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002121 else
Mayank Groverddd348d2018-01-23 14:07:09 +05302122 ret = mmc_write((ptn + size - device_info_sz), device_info_sz, (void *)info_buf);
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002123 if (ret)
Shashank Mittal162244e2011-08-08 19:01:25 -07002124 {
2125 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
Sridhar Parasurame1a97922015-07-27 15:31:22 -07002126 ASSERT(0);
Shashank Mittal162244e2011-08-08 19:01:25 -07002127 }
2128}
2129
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002130void read_device_info_mmc(struct device_info *info)
Shashank Mittal162244e2011-08-08 19:01:25 -07002131{
Shashank Mittal162244e2011-08-08 19:01:25 -07002132 unsigned long long ptn = 0;
2133 unsigned long long size;
2134 int index = INVALID_PTN;
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002135 uint32_t ret = 0;
Mayank Groverddd348d2018-01-23 14:07:09 +05302136 uint32_t device_info_sz = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07002137
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002138 if ((index = partition_get_index("devinfo")) < 0)
2139 {
2140 devinfo_present = false;
2141 index = partition_get_index("aboot");
2142 }
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07002143
Shashank Mittal162244e2011-08-08 19:01:25 -07002144 ptn = partition_get_offset(index);
2145 if(ptn == 0)
2146 {
2147 return;
2148 }
2149
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07002150 mmc_set_lun(partition_get_lun(index));
2151
Shashank Mittal162244e2011-08-08 19:01:25 -07002152 size = partition_get_size(index);
2153
Mayank Groverddd348d2018-01-23 14:07:09 +05302154 device_info_sz = ROUND_TO_PAGE(sizeof(struct device_info),
2155 mmc_blocksize_mask);
2156 if (device_info_sz == UINT_MAX)
2157 {
2158 dprintf(CRITICAL, "ERROR: Incorrect blocksize of card\n");
2159 return;
2160 }
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07002161
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002162 if (devinfo_present)
Mayank Groverddd348d2018-01-23 14:07:09 +05302163 ret = mmc_read(ptn, (void *)info_buf, device_info_sz);
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002164 else
Mayank Groverddd348d2018-01-23 14:07:09 +05302165 ret = mmc_read((ptn + size - device_info_sz), (void *)info_buf, device_info_sz);
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002166 if (ret)
Shashank Mittal162244e2011-08-08 19:01:25 -07002167 {
2168 dprintf(CRITICAL, "ERROR: Cannot read device info\n");
Sridhar Parasurame1a97922015-07-27 15:31:22 -07002169 ASSERT(0);
Shashank Mittal162244e2011-08-08 19:01:25 -07002170 }
Shashank Mittal162244e2011-08-08 19:01:25 -07002171}
2172
2173void write_device_info_flash(device_info *dev)
2174{
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002175 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
Shashank Mittal162244e2011-08-08 19:01:25 -07002176 struct ptentry *ptn;
2177 struct ptable *ptable;
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002178 if(info == NULL)
2179 {
2180 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
2181 ASSERT(0);
2182 }
2183 info_buf = info;
Shashank Mittal162244e2011-08-08 19:01:25 -07002184 ptable = flash_get_ptable();
2185 if (ptable == NULL)
2186 {
2187 dprintf(CRITICAL, "ERROR: Partition table not found\n");
2188 return;
2189 }
2190
2191 ptn = ptable_find(ptable, "devinfo");
2192 if (ptn == NULL)
2193 {
Smita Ghosh670c8b82015-05-07 09:30:03 -07002194 dprintf(CRITICAL, "ERROR: No devinfo partition found\n");
Shashank Mittal162244e2011-08-08 19:01:25 -07002195 return;
2196 }
2197
Mayank Groverdedbc892017-10-24 13:41:34 +05302198 memset(info, 0, BOOT_IMG_MAX_PAGE_SIZE);
Shashank Mittal162244e2011-08-08 19:01:25 -07002199 memcpy(info, dev, sizeof(device_info));
2200
2201 if (flash_write(ptn, 0, (void *)info_buf, page_size))
2202 {
2203 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
2204 return;
2205 }
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002206 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07002207}
2208
vijay kumarc65876c2015-04-24 13:29:16 +05302209static int read_allow_oem_unlock(device_info *dev)
2210{
vijay kumarc65876c2015-04-24 13:29:16 +05302211 unsigned offset;
2212 int index;
2213 unsigned long long ptn;
2214 unsigned long long ptn_size;
2215 unsigned blocksize = mmc_get_device_blocksize();
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07002216 STACKBUF_DMA_ALIGN(buf, blocksize);
vijay kumarc65876c2015-04-24 13:29:16 +05302217
vijay kumarca2e6812015-07-08 20:28:25 +05302218 index = partition_get_index(frp_ptns[0]);
vijay kumarc65876c2015-04-24 13:29:16 +05302219 if (index == INVALID_PTN)
2220 {
vijay kumarca2e6812015-07-08 20:28:25 +05302221 index = partition_get_index(frp_ptns[1]);
2222 if (index == INVALID_PTN)
2223 {
2224 dprintf(CRITICAL, "Neither '%s' nor '%s' partition found\n", frp_ptns[0],frp_ptns[1]);
2225 return -1;
2226 }
vijay kumarc65876c2015-04-24 13:29:16 +05302227 }
2228
2229 ptn = partition_get_offset(index);
2230 ptn_size = partition_get_size(index);
2231 offset = ptn_size - blocksize;
2232
Mayank Grover48860402016-11-29 12:34:53 +05302233 /* Set Lun for partition */
2234 mmc_set_lun(partition_get_lun(index));
2235
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07002236 if (mmc_read(ptn + offset, (void *)buf, blocksize))
vijay kumarc65876c2015-04-24 13:29:16 +05302237 {
2238 dprintf(CRITICAL, "Reading MMC failed\n");
2239 return -1;
2240 }
2241
2242 /*is_allow_unlock is a bool value stored at the LSB of last byte*/
2243 is_allow_unlock = buf[blocksize-1] & 0x01;
2244 return 0;
2245}
2246
2247static int write_allow_oem_unlock(bool allow_unlock)
2248{
vijay kumarc65876c2015-04-24 13:29:16 +05302249 unsigned offset;
vijay kumarc65876c2015-04-24 13:29:16 +05302250 int index;
2251 unsigned long long ptn;
2252 unsigned long long ptn_size;
2253 unsigned blocksize = mmc_get_device_blocksize();
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07002254 STACKBUF_DMA_ALIGN(buf, blocksize);
vijay kumarc65876c2015-04-24 13:29:16 +05302255
vijay kumarca2e6812015-07-08 20:28:25 +05302256 index = partition_get_index(frp_ptns[0]);
vijay kumarc65876c2015-04-24 13:29:16 +05302257 if (index == INVALID_PTN)
2258 {
vijay kumarca2e6812015-07-08 20:28:25 +05302259 index = partition_get_index(frp_ptns[1]);
2260 if (index == INVALID_PTN)
2261 {
2262 dprintf(CRITICAL, "Neither '%s' nor '%s' partition found\n", frp_ptns[0],frp_ptns[1]);
2263 return -1;
2264 }
vijay kumarc65876c2015-04-24 13:29:16 +05302265 }
2266
2267 ptn = partition_get_offset(index);
2268 ptn_size = partition_get_size(index);
2269 offset = ptn_size - blocksize;
Mayank Grover48860402016-11-29 12:34:53 +05302270 mmc_set_lun(partition_get_lun(index));
vijay kumarc65876c2015-04-24 13:29:16 +05302271
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07002272 if (mmc_read(ptn + offset, (void *)buf, blocksize))
vijay kumarc65876c2015-04-24 13:29:16 +05302273 {
2274 dprintf(CRITICAL, "Reading MMC failed\n");
2275 return -1;
2276 }
2277
2278 /*is_allow_unlock is a bool value stored at the LSB of last byte*/
2279 buf[blocksize-1] = allow_unlock;
2280 if (mmc_write(ptn + offset, blocksize, buf))
2281 {
2282 dprintf(CRITICAL, "Writing MMC failed\n");
2283 return -1;
2284 }
2285
2286 return 0;
2287}
2288
Shashank Mittal162244e2011-08-08 19:01:25 -07002289void read_device_info_flash(device_info *dev)
2290{
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002291 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
Shashank Mittal162244e2011-08-08 19:01:25 -07002292 struct ptentry *ptn;
2293 struct ptable *ptable;
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002294 if(info == NULL)
2295 {
2296 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
2297 ASSERT(0);
2298 }
2299 info_buf = info;
Shashank Mittal162244e2011-08-08 19:01:25 -07002300 ptable = flash_get_ptable();
2301 if (ptable == NULL)
2302 {
2303 dprintf(CRITICAL, "ERROR: Partition table not found\n");
2304 return;
2305 }
2306
2307 ptn = ptable_find(ptable, "devinfo");
2308 if (ptn == NULL)
2309 {
Smita Ghosh670c8b82015-05-07 09:30:03 -07002310 dprintf(CRITICAL, "ERROR: No devinfo partition found\n");
Shashank Mittal162244e2011-08-08 19:01:25 -07002311 return;
2312 }
2313
2314 if (flash_read(ptn, 0, (void *)info_buf, page_size))
2315 {
2316 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
2317 return;
2318 }
2319
2320 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
2321 {
Shashank Mittal162244e2011-08-08 19:01:25 -07002322 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
2323 info->is_unlocked = 0;
Shashank Mittala0032282011-08-26 14:50:11 -07002324 info->is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07002325 write_device_info_flash(info);
2326 }
2327 memcpy(dev, info, sizeof(device_info));
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002328 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07002329}
2330
2331void write_device_info(device_info *dev)
2332{
2333 if(target_is_emmc_boot())
2334 {
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002335 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
2336 if(info == NULL)
2337 {
2338 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
2339 ASSERT(0);
2340 }
2341 info_buf = info;
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002342 memcpy(info, dev, sizeof(struct device_info));
2343
2344#if USE_RPMB_FOR_DEVINFO
Mayank Grover912eaa62017-10-26 12:08:53 +05302345 if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05302346 is_secure_boot_enable()) {
2347 if((write_device_info_rpmb((void*) info, PAGE_SIZE)) < 0)
2348 ASSERT(0);
Sridhar Parasurame1a97922015-07-27 15:31:22 -07002349 }
Channagoud Kadabic80cb492015-04-28 16:08:28 -07002350 else
2351 write_device_info_mmc(info);
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002352#else
2353 write_device_info_mmc(info);
2354#endif
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002355 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07002356 }
2357 else
2358 {
2359 write_device_info_flash(dev);
2360 }
2361}
2362
Monika Singh94316462018-03-15 18:39:01 +05302363int read_rollback_index(uint32_t loc, uint64_t *roll_back_index)
2364{
2365 if (!devinfo_present) {
2366 dprintf(CRITICAL, "DeviceInfo not initalized \n");
2367 return -EINVAL;
2368 }
2369 if (loc >= ARRAY_SIZE(device.rollback_index)) {
2370 dprintf(CRITICAL, "%s() Loc out of range index: %d, array len: %d\n",
2371 __func__, loc, ARRAY_SIZE(device.rollback_index));
2372 ASSERT(0);
2373 }
2374
2375 *roll_back_index = device.rollback_index[loc];
2376 return 0;
2377}
2378
2379int write_rollback_index(uint32_t loc, uint64_t roll_back_index)
2380{
2381 if (!devinfo_present) {
2382 dprintf(CRITICAL, "DeviceInfo not initalized \n");
2383 return -EINVAL;
2384 }
2385 if (loc >= ARRAY_SIZE(device.rollback_index)) {
2386 dprintf(CRITICAL, "%s() Loc out of range index: %d, array len: %d\n",
2387 __func__, loc, ARRAY_SIZE(device.rollback_index));
2388 ASSERT(0);
2389 }
2390
2391 device.rollback_index[loc] = roll_back_index;
2392 write_device_info(&device);
2393 return 0;
2394}
2395
Monika Singhb0fad822018-03-15 18:50:55 +05302396int store_userkey(uint8_t *user_key, uint32_t user_key_size)
2397{
2398 if (!devinfo_present) {
2399 dprintf(CRITICAL, "DeviceInfo not initalized \n");
2400 return -EINVAL;
2401 }
2402
2403 if (user_key_size > ARRAY_SIZE(device.user_public_key)) {
2404 dprintf(CRITICAL, "StoreUserKey, UserKeySize too large!\n");
2405 return -ENODEV;
2406 }
2407
2408 memcpy(device.user_public_key, user_key, user_key_size);
2409 device.user_public_key_length = user_key_size;
2410 write_device_info(&device);
2411 return 0;
2412}
2413
2414int erase_userkey()
2415{
2416 if (!devinfo_present) {
2417 dprintf(CRITICAL, "DeviceInfo not initalized \n");
2418 return -EINVAL;
2419 }
2420 memset(device.user_public_key, 0, ARRAY_SIZE(device.user_public_key));
2421 device.user_public_key_length = 0;
2422 write_device_info(&device);
2423 return 0;
2424}
2425
2426int get_userkey(uint8_t **user_key, uint32_t *user_key_size)
2427{
2428 if (!devinfo_present) {
2429 dprintf(CRITICAL, "DeviceInfo not initalized \n");
2430 return -EINVAL;
2431 }
2432 *user_key = device.user_public_key;
2433 *user_key_size = device.user_public_key_length;
2434 return 0;
2435}
2436
Shashank Mittal162244e2011-08-08 19:01:25 -07002437void read_device_info(device_info *dev)
2438{
2439 if(target_is_emmc_boot())
2440 {
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002441 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
2442 if(info == NULL)
2443 {
2444 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
2445 ASSERT(0);
2446 }
2447 info_buf = info;
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002448
2449#if USE_RPMB_FOR_DEVINFO
Mayank Grover912eaa62017-10-26 12:08:53 +05302450 if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05302451 is_secure_boot_enable()) {
2452 if((read_device_info_rpmb((void*) info, PAGE_SIZE)) < 0)
2453 ASSERT(0);
Sridhar Parasurame1a97922015-07-27 15:31:22 -07002454 }
Channagoud Kadabic80cb492015-04-28 16:08:28 -07002455 else
2456 read_device_info_mmc(info);
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002457#else
2458 read_device_info_mmc(info);
2459#endif
2460
2461 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
2462 {
2463 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
lijuang511a2b52015-08-14 20:50:51 +08002464 if (is_secure_boot_enable()) {
Channagoud Kadabi05f78ba2015-07-06 11:58:14 -07002465 info->is_unlocked = 0;
Monika Singh292b3e92018-03-17 22:40:23 +05302466#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover912eaa62017-10-26 12:08:53 +05302467 if (VB_M <= target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +05302468 info->is_unlock_critical = 0;
Parth Dixitddbc7352015-10-18 03:13:31 +05302469#endif
lijuang511a2b52015-08-14 20:50:51 +08002470 } else {
Channagoud Kadabi2fda4092015-07-07 13:34:11 -07002471 info->is_unlocked = 1;
Monika Singh292b3e92018-03-17 22:40:23 +05302472#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover912eaa62017-10-26 12:08:53 +05302473 if (VB_M <= target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +05302474 info->is_unlock_critical = 1;
Parth Dixitddbc7352015-10-18 03:13:31 +05302475#endif
lijuang511a2b52015-08-14 20:50:51 +08002476 }
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002477 info->is_tampered = 0;
2478 info->charger_screen_enabled = 0;
Monika Singh292b3e92018-03-17 22:40:23 +05302479#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover912eaa62017-10-26 12:08:53 +05302480 if (VB_M <= target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +05302481 info->verity_mode = 1; //enforcing by default
Parth Dixitddbc7352015-10-18 03:13:31 +05302482#endif
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002483 write_device_info(info);
2484 }
2485 memcpy(dev, info, sizeof(device_info));
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002486 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07002487 }
2488 else
2489 {
2490 read_device_info_flash(dev);
2491 }
2492}
2493
2494void reset_device_info()
2495{
2496 dprintf(ALWAYS, "reset_device_info called.");
Shashank Mittala0032282011-08-26 14:50:11 -07002497 device.is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07002498 write_device_info(&device);
2499}
2500
2501void set_device_root()
2502{
2503 dprintf(ALWAYS, "set_device_root called.");
Shashank Mittala0032282011-08-26 14:50:11 -07002504 device.is_tampered = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -07002505 write_device_info(&device);
2506}
2507
lijuang4ece1e72015-08-14 21:02:36 +08002508/* set device unlock value
2509 * Must check FRP before call this function
2510 * Need to wipe data when unlock status changed
2511 * type 0: oem unlock
2512 * type 1: unlock critical
2513 * status 0: unlock as false
2514 * status 1: lock as true
2515 */
2516void set_device_unlock_value(int type, bool status)
lijuang21f12f52015-08-22 16:22:19 +08002517{
lijuang4ece1e72015-08-14 21:02:36 +08002518 if (type == UNLOCK)
2519 device.is_unlocked = status;
Monika Singh292b3e92018-03-17 22:40:23 +05302520#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover912eaa62017-10-26 12:08:53 +05302521 else if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05302522 type == UNLOCK_CRITICAL)
2523 device.is_unlock_critical = status;
Parth Dixitddbc7352015-10-18 03:13:31 +05302524#endif
lijuang4ece1e72015-08-14 21:02:36 +08002525 write_device_info(&device);
2526}
2527
2528static void set_device_unlock(int type, bool status)
2529{
2530 int is_unlocked = -1;
2531 char response[MAX_RSP_SIZE];
2532
2533 /* check device unlock status if it is as expected */
2534 if (type == UNLOCK)
2535 is_unlocked = device.is_unlocked;
Monika Singh292b3e92018-03-17 22:40:23 +05302536#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover912eaa62017-10-26 12:08:53 +05302537 if(VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05302538 type == UNLOCK_CRITICAL)
2539 {
2540 is_unlocked = device.is_unlock_critical;
2541 }
Parth Dixitddbc7352015-10-18 03:13:31 +05302542#endif
lijuang4ece1e72015-08-14 21:02:36 +08002543 if (is_unlocked == status) {
2544 snprintf(response, sizeof(response), "\tDevice already : %s", (status ? "unlocked!" : "locked!"));
2545 fastboot_info(response);
2546 fastboot_okay("");
2547 return;
2548 }
2549
2550 /* status is true, it means to unlock device */
lijuang07c5d6e2018-04-23 18:32:13 +08002551 if (status && !is_allow_unlock) {
2552 fastboot_fail("oem unlock is not allowed");
2553 return;
2554 }
lijuang21f12f52015-08-22 16:22:19 +08002555
lijuang4ece1e72015-08-14 21:02:36 +08002556#if FBCON_DISPLAY_MSG
lijuang07c5d6e2018-04-23 18:32:13 +08002557 display_unlock_menu(type, status);
2558 fastboot_okay("");
2559 return;
lijuang4ece1e72015-08-14 21:02:36 +08002560#else
lijuang07c5d6e2018-04-23 18:32:13 +08002561 if (status && type == UNLOCK) {
2562 fastboot_fail("Need wipe userdata. Do 'fastboot oem unlock-go'");
2563 return;
lijuang21f12f52015-08-22 16:22:19 +08002564 }
lijuang07c5d6e2018-04-23 18:32:13 +08002565#endif
lijuang4ece1e72015-08-14 21:02:36 +08002566
2567 set_device_unlock_value(type, status);
2568
2569 /* wipe data */
2570 struct recovery_message msg;
Kishor PK60a68212017-05-08 16:55:57 +05302571 memset(&msg, 0, sizeof(msg));
lijuang4ece1e72015-08-14 21:02:36 +08002572 snprintf(msg.recovery, sizeof(msg.recovery), "recovery\n--wipe_data");
2573 write_misc(0, &msg, sizeof(msg));
2574
2575 fastboot_okay("");
2576 reboot_device(RECOVERY_MODE);
lijuang21f12f52015-08-22 16:22:19 +08002577}
2578
lijuang511a2b52015-08-14 20:50:51 +08002579static bool critical_flash_allowed(const char * entry)
2580{
2581 uint32_t i = 0;
2582 if (entry == NULL)
2583 return false;
2584
2585 for (i = 0; i < ARRAY_SIZE(critical_flash_allowed_ptn); i++) {
2586 if(!strcmp(entry, critical_flash_allowed_ptn[i]))
2587 return true;
2588 }
2589 return false;
Matthew Qin271927e2015-03-31 22:07:07 -04002590}
2591
2592#if DEVICE_TREE
Amol Jadicb524072012-08-09 16:40:18 -07002593int copy_dtb(uint8_t *boot_image_start, unsigned int scratch_offset)
2594{
2595 uint32 dt_image_offset = 0;
Amol Jadicb524072012-08-09 16:40:18 -07002596 uint32_t n;
Monika Singh292b3e92018-03-17 22:40:23 +05302597 struct dt_table *table = NULL;
Amol Jadicb524072012-08-09 16:40:18 -07002598 struct dt_entry dt_entry;
Monika Singh292b3e92018-03-17 22:40:23 +05302599 uint32_t dt_hdr_size = 0;
Amol Jadicb524072012-08-09 16:40:18 -07002600 unsigned int compressed_size = 0;
2601 unsigned int dtb_size = 0;
2602 unsigned int out_avai_len = 0;
2603 unsigned char *out_addr = NULL;
2604 unsigned char *best_match_dt_addr = NULL;
2605 int rc;
2606
2607 struct boot_img_hdr *hdr = (struct boot_img_hdr *) (boot_image_start);
2608
Parth Dixit4097b622016-03-15 14:42:27 +05302609#ifndef OSVERSION_IN_BOOTIMAGE
2610 dt_size = hdr->dt_size;
2611#endif
2612
2613 if(dt_size != 0) {
Amol Jadicb524072012-08-09 16:40:18 -07002614 /* add kernel offset */
2615 dt_image_offset += page_size;
2616 n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
2617 dt_image_offset += n;
2618
2619 /* add ramdisk offset */
2620 n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
2621 dt_image_offset += n;
2622
2623 /* add second offset */
2624 if(hdr->second_size != 0) {
2625 n = ROUND_TO_PAGE(hdr->second_size, page_mask);
2626 dt_image_offset += n;
2627 }
2628
2629 /* offset now point to start of dt.img */
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07002630 table = (struct dt_table*)(boot_image_start + dt_image_offset);
Amol Jadicb524072012-08-09 16:40:18 -07002631
Deepa Dinamani19648b42013-09-05 17:05:55 -07002632 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Amol Jadicb524072012-08-09 16:40:18 -07002633 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
2634 return -1;
2635 }
P.V. Phani Kumar82916762016-03-09 09:20:19 +05302636
2637 /* Its Error if, dt_hdr_size (table->num_entries * dt_entry size + Dev_Tree Header)
2638 goes beyound hdr->dt_size*/
Parth Dixit4097b622016-03-15 14:42:27 +05302639 if (dt_hdr_size > ROUND_TO_PAGE(dt_size,hdr->page_size)) {
P.V. Phani Kumar82916762016-03-09 09:20:19 +05302640 dprintf(CRITICAL, "ERROR: Invalid Device Tree size \n");
2641 return -1;
2642 }
2643
Joel Kingaa335dc2013-06-03 16:11:08 -07002644 /* Find index of device tree within device tree table */
2645 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Amol Jadicb524072012-08-09 16:40:18 -07002646 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
2647 return -1;
2648 }
2649
Matthew Qin271927e2015-03-31 22:07:07 -04002650 best_match_dt_addr = (unsigned char *)boot_image_start + dt_image_offset + dt_entry.offset;
2651 if (is_gzip_package(best_match_dt_addr, dt_entry.size))
2652 {
2653 out_addr = (unsigned char *)target_get_scratch_address() + scratch_offset;
2654 out_avai_len = target_get_max_flash_size() - scratch_offset;
Matthew Qin0b15b322015-05-19 05:20:54 -04002655 dprintf(INFO, "decompressing dtb: start\n");
Matthew Qin271927e2015-03-31 22:07:07 -04002656 rc = decompress(best_match_dt_addr,
2657 dt_entry.size, out_addr, out_avai_len,
2658 &compressed_size, &dtb_size);
2659 if (rc)
2660 {
Matthew Qin0b15b322015-05-19 05:20:54 -04002661 dprintf(CRITICAL, "decompressing dtb failed!!!\n");
Matthew Qin271927e2015-03-31 22:07:07 -04002662 ASSERT(0);
2663 }
2664
Matthew Qin0b15b322015-05-19 05:20:54 -04002665 dprintf(INFO, "decompressing dtb: done\n");
Matthew Qin271927e2015-03-31 22:07:07 -04002666 best_match_dt_addr = out_addr;
2667 } else {
2668 dtb_size = dt_entry.size;
2669 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002670 /* Validate and Read device device tree in the "tags_add */
Mayank Grovere559cec2017-10-17 15:12:03 +05302671 if (check_aboot_addr_range_overlap(hdr->tags_addr, dtb_size) ||
2672 check_ddr_addr_range_bound(hdr->tags_addr, dtb_size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002673 {
Mayank Grovere559cec2017-10-17 15:12:03 +05302674 dprintf(CRITICAL, "Device tree addresses are not valid.\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002675 return -1;
2676 }
2677
Amol Jadicb524072012-08-09 16:40:18 -07002678 /* Read device device tree in the "tags_add */
Matthew Qin271927e2015-03-31 22:07:07 -04002679 memmove((void*) hdr->tags_addr, (void *)best_match_dt_addr, dtb_size);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002680 } else
2681 return -1;
Amol Jadicb524072012-08-09 16:40:18 -07002682
2683 /* Everything looks fine. Return success. */
2684 return 0;
2685}
2686#endif
2687
Brian Swetland9c4c0752009-01-25 16:23:50 -08002688void cmd_boot(const char *arg, void *data, unsigned sz)
2689{
2690 unsigned kernel_actual;
2691 unsigned ramdisk_actual;
Kishor PKd8ddcad2017-07-27 13:53:57 +05302692 unsigned second_actual;
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002693 uint32_t image_actual;
2694 uint32_t dt_actual = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08002695 struct boot_img_hdr *hdr = NULL;
2696 struct kernel64_hdr *kptr = NULL;
Brian Swetland9c4c0752009-01-25 16:23:50 -08002697 char *ptr = ((char*) data);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002698 int ret = 0;
2699 uint8_t dtb_copied = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08002700 unsigned int out_len = 0;
2701 unsigned int out_avai_len = 0;
2702 unsigned char *out_addr = NULL;
2703 uint32_t dtb_offset = 0;
2704 unsigned char *kernel_start_addr = NULL;
2705 unsigned int kernel_size = 0;
Matthew Qin271927e2015-03-31 22:07:07 -04002706 unsigned int scratch_offset = 0;
Monika Singh292b3e92018-03-17 22:40:23 +05302707#if !VERIFIED_BOOT_2
2708 uint32_t sig_actual = 0;
2709 uint32_t sig_size = 0;
2710#ifdef MDTP_SUPPORT
2711 static bool is_mdtp_activated = 0;
2712#endif /* MDTP_SUPPORT */
2713#endif
Matthew Qinbb7923d2015-02-09 10:56:09 +08002714
lijuang2008ff22016-03-07 17:56:27 +08002715#if FBCON_DISPLAY_MSG
2716 /* Exit keys' detection thread firstly */
2717 exit_menu_keys_detection();
2718#endif
2719
Monika Singh292b3e92018-03-17 22:40:23 +05302720#if VERIFIED_BOOT || VERIFIED_BOOT_2
Channagoud Kadabi6d5375e2015-06-23 17:15:42 -07002721 if(target_build_variant_user() && !device.is_unlocked)
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002722 {
2723 fastboot_fail("unlock device to use this command");
lijuang2008ff22016-03-07 17:56:27 +08002724 goto boot_failed;
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002725 }
2726#endif
2727
Brian Swetland9c4c0752009-01-25 16:23:50 -08002728 if (sz < sizeof(hdr)) {
2729 fastboot_fail("invalid bootimage header");
lijuang2008ff22016-03-07 17:56:27 +08002730 goto boot_failed;
Brian Swetland9c4c0752009-01-25 16:23:50 -08002731 }
2732
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002733 hdr = (struct boot_img_hdr *)data;
Brian Swetland9c4c0752009-01-25 16:23:50 -08002734
2735 /* ensure commandline is terminated */
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002736 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -08002737
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002738 if(target_is_emmc_boot() && hdr->page_size) {
2739 page_size = hdr->page_size;
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07002740 page_mask = page_size - 1;
2741 }
2742
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002743 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
2744 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Kishor PKd8ddcad2017-07-27 13:53:57 +05302745 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002746#if DEVICE_TREE
Parth Dixit4097b622016-03-15 14:42:27 +05302747#ifndef OSVERSION_IN_BOOTIMAGE
2748 dt_size = hdr->dt_size;
2749#endif
2750 dt_actual = ROUND_TO_PAGE(dt_size, page_mask);
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002751#endif
2752
2753 image_actual = ADD_OF(page_size, kernel_actual);
2754 image_actual = ADD_OF(image_actual, ramdisk_actual);
Kishor PKd8ddcad2017-07-27 13:53:57 +05302755 image_actual = ADD_OF(image_actual, second_actual);
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002756 image_actual = ADD_OF(image_actual, dt_actual);
2757
Kishor PK5134b332017-05-09 17:50:08 +05302758 /* Checking to prevent oob access in read_der_message_length */
2759 if (image_actual > sz) {
2760 fastboot_fail("bootimage header fields are invalid");
2761 goto boot_failed;
2762 }
Monika Singh292b3e92018-03-17 22:40:23 +05302763#if VERIFIED_BOOT_2
2764 memset(&info, 0, sizeof(bootinfo));
2765 info.images[0].image_buffer = data;
2766 info.images[0].imgsize = image_actual;
2767 info.images[0].name = "boot";
2768 info.num_loaded_images = 1;
2769 info.multi_slot_boot = partition_multislot_is_supported();
2770 if (load_image_and_auth(&info))
2771 goto boot_failed;
2772 vbcmdline = info.vbcmdline;
2773#else
Kishor PK5134b332017-05-09 17:50:08 +05302774 sig_size = sz - image_actual;
2775
P.V. Phani Kumareecfd822016-03-09 20:09:03 +05302776 if (target_use_signed_kernel() && (!device.is_unlocked)) {
Monika Singh292b3e92018-03-17 22:40:23 +05302777 unsigned chk;
P.V. Phani Kumareecfd822016-03-09 20:09:03 +05302778 /* Calculate the signature length from boot image */
2779 sig_actual = read_der_message_length(
Kishor PK5134b332017-05-09 17:50:08 +05302780 (unsigned char*)(data + image_actual), sig_size);
Monika Singh292b3e92018-03-17 22:40:23 +05302781 chk = ADD_OF(image_actual, sig_actual);
Channagoud Kadabi3d839012014-06-26 14:26:39 -07002782
Monika Singh292b3e92018-03-17 22:40:23 +05302783 if (chk > sz) {
Kishor PK5134b332017-05-09 17:50:08 +05302784 fastboot_fail("bootimage header fields are invalid");
2785 goto boot_failed;
2786 }
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002787 }
2788
Sridhar Parasuram361e4cd2015-09-24 11:19:19 -07002789 // Initialize boot state before trying to verify boot.img
2790#if VERIFIED_BOOT
Channagoud Kadabi699466e2015-11-03 12:37:42 -08002791 boot_verifier_init();
Mayank Groverb337e932017-01-18 20:00:40 +05302792#endif
Channagoud Kadabibf6ce7d2015-09-17 13:25:35 -07002793 /* Handle overflow if the input image size is greater than
2794 * boot image buffer can hold
2795 */
Monika Singh292b3e92018-03-17 22:40:23 +05302796 if ((target_get_max_flash_size() - page_size) < image_actual)
Channagoud Kadabibf6ce7d2015-09-17 13:25:35 -07002797 {
2798 fastboot_fail("booimage: size is greater than boot image buffer can hold");
lijuang2008ff22016-03-07 17:56:27 +08002799 goto boot_failed;
Channagoud Kadabibf6ce7d2015-09-17 13:25:35 -07002800 }
Sridhar Parasuram361e4cd2015-09-24 11:19:19 -07002801
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002802 /* Verify the boot image
2803 * device & page_size are initialized in aboot_init
2804 */
Sridhar Parasuram7ae9fbc2015-09-17 09:56:30 -07002805 if (target_use_signed_kernel() && (!device.is_unlocked)) {
Channagoud Kadabi3d839012014-06-26 14:26:39 -07002806 /* Pass size excluding signature size, otherwise we would try to
2807 * access signature beyond its length
2808 */
Monika Singh292b3e92018-03-17 22:40:23 +05302809 verify_signed_bootimg((uint32_t)data, image_actual);
Sridhar Parasuram7ae9fbc2015-09-17 09:56:30 -07002810 }
Amit Blay4aa292f2015-04-28 21:55:59 +03002811#ifdef MDTP_SUPPORT
2812 else
2813 {
Shay Nachmani062aa3f2015-05-17 17:28:44 +03002814 /* fastboot boot is not allowed when MDTP is activated */
Amit Blay4aa292f2015-04-28 21:55:59 +03002815 mdtp_ext_partition_verification_t ext_partition;
Amit Blay8a510302015-08-17 09:20:01 +03002816
2817 if (!is_mdtp_activated) {
2818 ext_partition.partition = MDTP_PARTITION_NONE;
2819 mdtp_fwlock_verify_lock(&ext_partition);
2820 }
Amit Blay4aa292f2015-04-28 21:55:59 +03002821 }
Shay Nachmani062aa3f2015-05-17 17:28:44 +03002822
Amir Kotzer7c768c02016-04-13 09:08:05 +03002823 /* If mdtp state cannot be validate, block fastboot boot*/
2824 if(mdtp_activated(&is_mdtp_activated)){
2825 dprintf(CRITICAL, "mdtp_activated cannot validate state.\n");
2826 dprintf(CRITICAL, "Can not proceed with fastboot boot command.\n");
2827 goto boot_failed;
2828 }
Shay Nachmani062aa3f2015-05-17 17:28:44 +03002829 if(is_mdtp_activated){
2830 dprintf(CRITICAL, "fastboot boot command is not available.\n");
lijuang2008ff22016-03-07 17:56:27 +08002831 goto boot_failed;
Shay Nachmani062aa3f2015-05-17 17:28:44 +03002832 }
Amit Blay4aa292f2015-04-28 21:55:59 +03002833#endif /* MDTP_SUPPORT */
Monika Singh292b3e92018-03-17 22:40:23 +05302834#endif /* VERIFIED_BOOT_2 else */
Amit Blay4aa292f2015-04-28 21:55:59 +03002835
Sridhar Parasuram361e4cd2015-09-24 11:19:19 -07002836#if VERIFIED_BOOT
Monika Singh292b3e92018-03-17 22:40:23 +05302837 if (VB_M == target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +05302838 {
2839 /* set boot and system versions. */
2840 set_os_version((unsigned char *)data);
2841 // send root of trust
2842 if(!send_rot_command((uint32_t)device.is_unlocked))
2843 ASSERT(0);
2844 }
Parth Dixitbc9b6492015-10-18 00:41:38 +05302845#endif
Channagoud Kadabia22144f2013-03-20 11:49:01 -07002846 /*
Matthew Qinbb7923d2015-02-09 10:56:09 +08002847 * Check if the kernel image is a gzip package. If yes, need to decompress it.
2848 * If not, continue booting.
2849 */
2850 if (is_gzip_package((unsigned char *)(data + page_size), hdr->kernel_size))
2851 {
2852 out_addr = (unsigned char *)target_get_scratch_address();
2853 out_addr = (unsigned char *)(out_addr + image_actual + page_size);
2854 out_avai_len = target_get_max_flash_size() - image_actual - page_size;
Matthew Qin0b15b322015-05-19 05:20:54 -04002855 dprintf(INFO, "decompressing kernel image: start\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08002856 ret = decompress((unsigned char *)(ptr + page_size),
2857 hdr->kernel_size, out_addr, out_avai_len,
2858 &dtb_offset, &out_len);
2859 if (ret)
2860 {
Matthew Qin0b15b322015-05-19 05:20:54 -04002861 dprintf(CRITICAL, "decompressing image failed!!!\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08002862 ASSERT(0);
2863 }
2864
Matthew Qin0b15b322015-05-19 05:20:54 -04002865 dprintf(INFO, "decompressing kernel image: done\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08002866 kptr = (struct kernel64_hdr *)out_addr;
2867 kernel_start_addr = out_addr;
2868 kernel_size = out_len;
2869 } else {
2870 kptr = (struct kernel64_hdr*)((char *)data + page_size);
2871 kernel_start_addr = (unsigned char *)((char *)data + page_size);
2872 kernel_size = hdr->kernel_size;
2873 }
2874
2875 /*
Channagoud Kadabia22144f2013-03-20 11:49:01 -07002876 * Update the kernel/ramdisk/tags address if the boot image header
2877 * has default values, these default values come from mkbootimg when
2878 * the boot image is flashed using fastboot flash:raw
2879 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08002880 update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr));
Dima Zavin3cadfff2013-03-21 14:30:48 -07002881
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002882 /* Get virtual addresses since the hdr saves physical addresses. */
2883 hdr->kernel_addr = VA(hdr->kernel_addr);
2884 hdr->ramdisk_addr = VA(hdr->ramdisk_addr);
2885 hdr->tags_addr = VA(hdr->tags_addr);
Brian Swetland9c4c0752009-01-25 16:23:50 -08002886
Matthew Qinbb7923d2015-02-09 10:56:09 +08002887 kernel_size = ROUND_TO_PAGE(kernel_size, page_mask);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002888 /* Check if the addresses in the header are valid. */
Matthew Qinbb7923d2015-02-09 10:56:09 +08002889 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_size) ||
Mayank Grovere559cec2017-10-17 15:12:03 +05302890 check_ddr_addr_range_bound(hdr->kernel_addr, kernel_size) ||
2891 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual) ||
2892 check_ddr_addr_range_bound(hdr->ramdisk_addr, ramdisk_actual))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002893 {
Mayank Grovere559cec2017-10-17 15:12:03 +05302894 dprintf(CRITICAL, "kernel/ramdisk addresses are not valid.\n");
lijuang2008ff22016-03-07 17:56:27 +08002895 goto boot_failed;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002896 }
2897
Amol Jadicb524072012-08-09 16:40:18 -07002898#if DEVICE_TREE
Matthew Qin271927e2015-03-31 22:07:07 -04002899 scratch_offset = image_actual + page_size + out_len;
Amol Jadicb524072012-08-09 16:40:18 -07002900 /* find correct dtb and copy it to right location */
Matthew Qin271927e2015-03-31 22:07:07 -04002901 ret = copy_dtb(data, scratch_offset);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002902
2903 dtb_copied = !ret ? 1 : 0;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002904#else
Mayank Grovere559cec2017-10-17 15:12:03 +05302905 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE) ||
2906 check_ddr_addr_range_bound(hdr->tags_addr, MAX_TAGS_SIZE))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002907 {
Mayank Grovere559cec2017-10-17 15:12:03 +05302908 dprintf(CRITICAL, "Tags addresses are not valid.\n");
lijuang2008ff22016-03-07 17:56:27 +08002909 goto boot_failed;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002910 }
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002911#endif
2912
2913 /* Load ramdisk & kernel */
2914 memmove((void*) hdr->ramdisk_addr, ptr + page_size + kernel_actual, hdr->ramdisk_size);
Matthew Qinbb7923d2015-02-09 10:56:09 +08002915 memmove((void*) hdr->kernel_addr, (char*) (kernel_start_addr), kernel_size);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002916
2917#if DEVICE_TREE
Mayank Grovere559cec2017-10-17 15:12:03 +05302918 if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual) ||
2919 check_ddr_addr_range_bound(hdr->tags_addr, kernel_actual))
Matthew Qinbb7923d2015-02-09 10:56:09 +08002920 {
Mayank Grovere559cec2017-10-17 15:12:03 +05302921 dprintf(CRITICAL, "Tags addresses are not valid.\n");
lijuang2008ff22016-03-07 17:56:27 +08002922 goto boot_failed;
Matthew Qinbb7923d2015-02-09 10:56:09 +08002923 }
2924
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002925 /*
2926 * If dtb is not found look for appended DTB in the kernel.
2927 * If appended dev tree is found, update the atags with
2928 * memory address to the DTB appended location on RAM.
2929 * Else update with the atags address in the kernel header
2930 */
2931 if (!dtb_copied) {
2932 void *dtb;
Matthew Qinbb7923d2015-02-09 10:56:09 +08002933 dtb = dev_tree_appended((void*)(ptr + page_size),
2934 hdr->kernel_size, dtb_offset,
Dima Zavine63e5572013-05-03 12:23:06 -07002935 (void *)hdr->tags_addr);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002936 if (!dtb) {
2937 fastboot_fail("dtb not found");
lijuang2008ff22016-03-07 17:56:27 +08002938 goto boot_failed;
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002939 }
Amol Jadicb524072012-08-09 16:40:18 -07002940 }
2941#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08002942
2943 fastboot_okay("");
Amol Jadi7c4316c2013-10-07 14:19:26 -07002944 fastboot_stop();
Brian Swetland9c4c0752009-01-25 16:23:50 -08002945
Dima Zavin77e41f32013-03-06 16:10:43 -08002946 boot_linux((void*) hdr->kernel_addr, (void*) hdr->tags_addr,
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002947 (const char*) hdr->cmdline, board_machtype(),
2948 (void*) hdr->ramdisk_addr, hdr->ramdisk_size);
lijuang2008ff22016-03-07 17:56:27 +08002949
2950 /* fastboot already stop, it's no need to show fastboot menu */
2951 return;
2952boot_failed:
2953#if FBCON_DISPLAY_MSG
2954 /* revert to fastboot menu if boot failed */
2955 display_fastboot_menu();
2956#endif
2957 return;
Brian Swetland9c4c0752009-01-25 16:23:50 -08002958}
2959
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002960void cmd_erase_nand(const char *arg, void *data, unsigned sz)
Dima Zavin214cc642009-01-26 11:16:21 -08002961{
2962 struct ptentry *ptn;
2963 struct ptable *ptable;
2964
2965 ptable = flash_get_ptable();
2966 if (ptable == NULL) {
2967 fastboot_fail("partition table doesn't exist");
2968 return;
2969 }
2970
2971 ptn = ptable_find(ptable, arg);
2972 if (ptn == NULL) {
2973 fastboot_fail("unknown partition name");
2974 return;
2975 }
2976
Monika Singh292b3e92018-03-17 22:40:23 +05302977 if (!strncmp(arg, "avb_custom_key", strlen("avb_custom_key"))) {
2978 dprintf(INFO, "erasing avb_custom_key\n");
2979 if (erase_userkey()) {
2980 fastboot_fail("Erasing avb_custom_key failed");
2981 } else {
2982 fastboot_okay("");
2983 }
2984 return;
2985 }
2986
Dima Zavin214cc642009-01-26 11:16:21 -08002987 if (flash_erase(ptn)) {
2988 fastboot_fail("failed to erase partition");
2989 return;
2990 }
2991 fastboot_okay("");
2992}
2993
Bikas Gurungd48bd242010-09-04 19:54:32 -07002994
2995void cmd_erase_mmc(const char *arg, void *data, unsigned sz)
2996{
2997 unsigned long long ptn = 0;
Oliver Wangcee448d2013-10-22 18:40:13 +08002998 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07002999 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003000 uint8_t lun = 0;
Vijay Kumar Pendoti0b21f462016-05-02 17:09:18 +05303001 char *footer = NULL;
Bikas Gurungd48bd242010-09-04 19:54:32 -07003002
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003003#if VERIFIED_BOOT
3004 if(!strcmp(arg, KEYSTORE_PTN_NAME))
3005 {
3006 if(!device.is_unlocked)
3007 {
3008 fastboot_fail("unlock device to erase keystore");
3009 return;
3010 }
3011 }
3012#endif
3013
Kinson Chikf1a43512011-07-14 11:28:39 -07003014 index = partition_get_index(arg);
3015 ptn = partition_get_offset(index);
Oliver Wangcee448d2013-10-22 18:40:13 +08003016 size = partition_get_size(index);
Neeti Desaica8c9602011-10-06 11:40:00 -07003017
Kinson Chikf1a43512011-07-14 11:28:39 -07003018 if(ptn == 0) {
Neeti Desaica8c9602011-10-06 11:40:00 -07003019 fastboot_fail("Partition table doesn't exist\n");
Bikas Gurungd48bd242010-09-04 19:54:32 -07003020 return;
3021 }
Kun Liang2f1601a2013-08-12 16:29:54 +08003022
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003023 lun = partition_get_lun(index);
3024 mmc_set_lun(lun);
3025
Monika Singh292b3e92018-03-17 22:40:23 +05303026 if (!strncmp(arg, "avb_custom_key", strlen("avb_custom_key"))) {
3027 dprintf(INFO, "erasing avb_custom_key\n");
3028 if (erase_userkey()) {
3029 fastboot_fail("Erasing avb_custom_key failed");
3030 } else {
3031 fastboot_okay("");
3032 }
3033 return;
3034 }
Sridhar Parasuramd7957122015-02-27 11:33:40 -08003035 if (platform_boot_dev_isemmc())
3036 {
3037 if (mmc_erase_card(ptn, size)) {
3038 fastboot_fail("failed to erase partition\n");
3039 return;
3040 }
3041 } else {
3042 BUF_DMA_ALIGN(out, DEFAULT_ERASE_SIZE);
3043 size = partition_get_size(index);
3044 if (size > DEFAULT_ERASE_SIZE)
3045 size = DEFAULT_ERASE_SIZE;
Kun Liang2f1601a2013-08-12 16:29:54 +08003046
Sridhar Parasuramd7957122015-02-27 11:33:40 -08003047 /* Simple inefficient version of erase. Just writing
3048 0 in first several blocks */
3049 if (mmc_write(ptn , size, (unsigned int *)out)) {
3050 fastboot_fail("failed to erase partition");
3051 return;
3052 }
Vijay Kumar Pendoti0b21f462016-05-02 17:09:18 +05303053 /*Erase FDE metadata at the userdata footer*/
3054 if(!(strncmp(arg, "userdata", 8)))
3055 {
3056 footer = memalign(CACHE_LINE, FOOTER_SIZE);
3057 memset((void *)footer, 0, FOOTER_SIZE);
3058
3059 size = partition_get_size(index);
3060
3061 if (mmc_write((ptn + size) - FOOTER_SIZE , FOOTER_SIZE, (unsigned int *)footer)) {
3062 fastboot_fail("failed to erase userdata footer");
3063 free(footer);
3064 return;
3065 }
3066 free(footer);
3067 }
Bikas Gurungd48bd242010-09-04 19:54:32 -07003068 }
Monika Singh292b3e92018-03-17 22:40:23 +05303069#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover912eaa62017-10-26 12:08:53 +05303070 if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05303071 !(strncmp(arg, "userdata", 8)) &&
3072 send_delete_keys_to_tz())
Sridhar Parasuram32b30662015-07-10 13:33:22 -07003073 ASSERT(0);
3074#endif
Bikas Gurungd48bd242010-09-04 19:54:32 -07003075 fastboot_okay("");
3076}
3077
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003078void cmd_erase(const char *arg, void *data, unsigned sz)
3079{
Monika Singh292b3e92018-03-17 22:40:23 +05303080#if VERIFIED_BOOT || VERIFIED_BOOT_2
Channagoud Kadabi35297672015-06-13 11:09:49 -07003081 if (target_build_variant_user())
Channagoud Kadabice3cf422015-04-17 16:02:30 -07003082 {
Sridhar Parasuramc32d07d2015-07-12 10:57:48 -07003083 if(!device.is_unlocked)
Channagoud Kadabice3cf422015-04-17 16:02:30 -07003084 {
Channagoud Kadabi35297672015-06-13 11:09:49 -07003085 fastboot_fail("device is locked. Cannot erase");
Channagoud Kadabice3cf422015-04-17 16:02:30 -07003086 return;
3087 }
3088 }
3089#endif
3090
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003091 if(target_is_emmc_boot())
3092 cmd_erase_mmc(arg, data, sz);
3093 else
3094 cmd_erase_nand(arg, data, sz);
3095}
Bikas Gurungd48bd242010-09-04 19:54:32 -07003096
Mayank Grover11ff9692018-01-11 11:54:49 +05303097/* Get the size from partiton name */
3098static void get_partition_size(const char *arg, char *response)
3099{
3100 uint64_t ptn = 0;
3101 uint64_t size;
3102 int index = INVALID_PTN;
3103
3104 index = partition_get_index(arg);
3105
3106 if (index == INVALID_PTN)
3107 {
3108 dprintf(CRITICAL, "Invalid partition index\n");
3109 return;
3110 }
3111
3112 ptn = partition_get_offset(index);
3113
3114 if(!ptn)
3115 {
3116 dprintf(CRITICAL, "Invalid partition name %s\n", arg);
3117 return;
3118 }
3119
3120 size = partition_get_size(index);
3121
3122 snprintf(response, MAX_RSP_SIZE, "\t 0x%llx", size);
3123 return;
3124}
3125
Mayank Grover52cd10a2018-03-15 12:57:54 +05303126/* Function to check partition type of a partition*/
3127static fs_signature_type
3128check_partition_fs_signature(const char *arg)
3129{
3130 fs_signature_type ret = NO_FS;
3131 int index;
3132 unsigned long long ptn;
3133 char *sb_buffer = malloc(mmc_blocksize);
3134 if (!sb_buffer)
3135 {
3136 dprintf(CRITICAL, "ERROR: Failed to allocate buffer for superblock\n");
3137 goto out;
3138 }
3139
3140 /* Read super block */
3141 if ((index = partition_get_index(arg)) < 0)
3142 {
3143 dprintf(CRITICAL, "ERROR: %s() doesn't exsit\n", arg);
3144 goto out;
3145 }
3146 ptn = partition_get_offset(index);
3147 mmc_set_lun(partition_get_lun(index));
3148 if(mmc_read(ptn + FS_SUPERBLOCK_OFFSET,
3149 (void *)sb_buffer, mmc_blocksize))
3150 {
3151 dprintf(CRITICAL, "ERROR: Failed to read Superblock\n");
3152 goto out;
3153 }
3154
3155 if (*((uint16 *)(&sb_buffer[EXT_MAGIC_OFFSET_SB]))
3156 == (uint16)EXT_MAGIC)
3157 {
3158 dprintf(SPEW, "%s() Found EXT FS\n", arg);
3159 ret = EXT_FS_SIGNATURE;
3160 }
3161 else if (*((uint32 *)(&sb_buffer[F2FS_MAGIC_OFFSET_SB]))
3162 == F2FS_MAGIC)
3163 {
3164 dprintf(SPEW, "%s() Found F2FS FS\n", arg);
3165 ret = EXT_F2FS_SIGNATURE;
3166 }
3167 else
3168 {
3169 dprintf(SPEW, "%s() Reverting to default 0x%x\n",
3170 arg, *((uint16 *)(&sb_buffer[EXT_MAGIC_OFFSET_SB])));
3171 ret = NO_FS;
3172 }
3173
3174out:
3175 if(sb_buffer)
3176 free(sb_buffer);
3177 return ret;
3178}
3179
Mayank Groverd38fe012018-03-13 15:33:16 +05303180/* Function to get partition type */
3181static void get_partition_type(const char *arg, char *response)
3182{
3183 uint n = 0;
Mayank Grover52cd10a2018-03-15 12:57:54 +05303184 fs_signature_type fs_signature;
Mayank Groverd38fe012018-03-13 15:33:16 +05303185
3186 if (arg == NULL ||
3187 response == NULL)
3188 {
3189 dprintf(CRITICAL, "Invalid input parameter\n");
3190 return;
3191 }
3192
3193 /* By default copy raw to response */
Mayank Grover0e559042018-04-11 17:49:30 +05303194 strlcpy(response, RAW_STR, MAX_RSP_SIZE);
Mayank Groverd38fe012018-03-13 15:33:16 +05303195
3196 /* Mark partiton type for known paritions only */
3197 for (n=0; n < ARRAY_SIZE(part_type_known); n++)
3198 {
3199 if (!strncmp(part_type_known[n].part_name, arg, strlen(arg)))
3200 {
Mayank Grover52cd10a2018-03-15 12:57:54 +05303201 /* Check partition for FS signature */
3202 fs_signature = check_partition_fs_signature(arg);
3203 switch (fs_signature)
3204 {
3205 case EXT_FS_SIGNATURE:
Mayank Grover0e559042018-04-11 17:49:30 +05303206 strlcpy(response, EXT_STR, MAX_RSP_SIZE);
Mayank Grover52cd10a2018-03-15 12:57:54 +05303207 break;
3208 case EXT_F2FS_SIGNATURE:
Mayank Grover0e559042018-04-11 17:49:30 +05303209 strlcpy(response, F2FS_STR, MAX_RSP_SIZE);
Mayank Grover52cd10a2018-03-15 12:57:54 +05303210 break;
3211 case NO_FS:
Mayank Grover0e559042018-04-11 17:49:30 +05303212 strlcpy(response, part_type_known[n].type_response, MAX_RSP_SIZE);
Mayank Grover52cd10a2018-03-15 12:57:54 +05303213 }
Mayank Groverd38fe012018-03-13 15:33:16 +05303214 }
3215 }
3216 return;
3217}
3218
Mayank Grover11ff9692018-01-11 11:54:49 +05303219/*
3220 * Publish the partition type & size info
3221 * fastboot getvar will publish the required information.
3222 * fastboot getvar partition_size:<partition_name>: partition size in hex
3223 * fastboot getvar partition_type:<partition_name>: partition type (ext/fat)
3224 */
3225static void publish_getvar_partition_info(struct getvar_partition_info *info, uint8_t num_parts)
3226{
Mayank Groverd38fe012018-03-13 15:33:16 +05303227 uint8_t i;
Mayank Grover11ff9692018-01-11 11:54:49 +05303228 static bool published = false;
3229 struct partition_entry *ptn_entry =
3230 partition_get_partition_entries();
3231 memset(info, 0, sizeof(struct getvar_partition_info)* num_parts);
3232
3233 for (i = 0; i < num_parts; i++) {
3234 strlcat(info[i].part_name, (const char *)ptn_entry[i].name, MAX_RSP_SIZE);
3235 strlcat(info[i].getvar_size, "partition-size:", MAX_GET_VAR_NAME_SIZE);
3236 strlcat(info[i].getvar_type, "partition-type:", MAX_GET_VAR_NAME_SIZE);
3237
Mayank Groverd38fe012018-03-13 15:33:16 +05303238 get_partition_type(info[i].part_name, info[i].type_response);
Mayank Grover11ff9692018-01-11 11:54:49 +05303239 get_partition_size(info[i].part_name, info[i].size_response);
Mayank Grover11ff9692018-01-11 11:54:49 +05303240 if (strlcat(info[i].getvar_size, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
3241 {
3242 dprintf(CRITICAL, "partition size name truncated\n");
3243 return;
3244 }
3245 if (strlcat(info[i].getvar_type, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
3246 {
3247 dprintf(CRITICAL, "partition type name truncated\n");
3248 return;
3249 }
3250
3251 if (!published)
3252 {
3253 /* publish partition size & type info */
3254 fastboot_publish((const char *) info[i].getvar_size, (const char *) info[i].size_response);
3255 fastboot_publish((const char *) info[i].getvar_type, (const char *) info[i].type_response);
3256 }
3257 }
3258 if (!published)
3259 published = true;
3260}
3261
3262
Ajay Dudani5c761132011-04-07 20:19:04 -07003263void cmd_flash_mmc_img(const char *arg, void *data, unsigned sz)
Shashank Mittal23b8f422010-04-16 19:27:21 -07003264{
3265 unsigned long long ptn = 0;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07003266 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07003267 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003268 char *token = NULL;
3269 char *pname = NULL;
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07003270 char *sp;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003271 uint8_t lun = 0;
3272 bool lun_set = false;
Mayank Grover351a75e2017-05-30 20:06:08 +05303273 int current_active_slot = INVALID;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07003274
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07003275 token = strtok_r((char *)arg, ":", &sp);
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003276 pname = token;
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07003277 token = strtok_r(NULL, ":", &sp);
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003278 if(token)
3279 {
3280 lun = atoi(token);
3281 mmc_set_lun(lun);
3282 lun_set = true;
3283 }
3284
Mao Jinlong226f33a2014-07-04 17:24:10 +08003285 if (pname)
Greg Grisco6e754772011-06-23 12:19:39 -07003286 {
Channagoud Kadabiad259832015-05-29 11:14:17 -07003287 if (!strncmp(pname, "frp-unlock", strlen("frp-unlock")))
3288 {
3289 if (!aboot_frp_unlock(pname, data, sz))
3290 {
3291 fastboot_info("FRP unlock successful");
3292 fastboot_okay("");
3293 }
3294 else
3295 fastboot_fail("Secret key is invalid, please update the bootloader with secret key");
3296
3297 return;
3298 }
3299
Mao Jinlong226f33a2014-07-04 17:24:10 +08003300 if (!strcmp(pname, "partition"))
3301 {
3302 dprintf(INFO, "Attempt to write partition image.\n");
3303 if (write_partition(sz, (unsigned char *) data)) {
3304 fastboot_fail("failed to write partition");
Greg Grisco6e754772011-06-23 12:19:39 -07003305 return;
3306 }
Mayank Grover11ff9692018-01-11 11:54:49 +05303307 /* Re-publish partition table */
3308 publish_getvar_partition_info(part_info, partition_get_partition_count());
Mayank Grover351a75e2017-05-30 20:06:08 +05303309
3310 /* Rescan partition table to ensure we have multislot support*/
3311 if (partition_scan_for_multislot())
3312 {
3313 current_active_slot = partition_find_active_slot();
3314 dprintf(INFO, "Multislot supported: Slot %s active",
3315 (SUFFIX_SLOT(current_active_slot)));
3316 }
3317 partition_mark_active_slot(current_active_slot);
Greg Grisco6e754772011-06-23 12:19:39 -07003318 }
Mao Jinlong226f33a2014-07-04 17:24:10 +08003319 else
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003320 {
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003321#if VERIFIED_BOOT
3322 if(!strcmp(pname, KEYSTORE_PTN_NAME))
3323 {
3324 if(!device.is_unlocked)
3325 {
3326 fastboot_fail("unlock device to flash keystore");
3327 return;
3328 }
Gaurav Nebhwanic7313cc2015-12-15 22:25:04 +05303329 if(!boot_verify_validate_keystore((unsigned char *)data,sz))
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003330 {
3331 fastboot_fail("image is not a keystore file");
3332 return;
3333 }
3334 }
3335#endif
Mao Jinlong226f33a2014-07-04 17:24:10 +08003336 index = partition_get_index(pname);
3337 ptn = partition_get_offset(index);
3338 if(ptn == 0) {
3339 fastboot_fail("partition table doesn't exist");
3340 return;
3341 }
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003342
Mayank Grover351a75e2017-05-30 20:06:08 +05303343 if (!strncmp(pname, "boot", strlen("boot"))
3344 || !strcmp(pname, "recovery"))
3345 {
Mao Jinlong226f33a2014-07-04 17:24:10 +08003346 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
3347 fastboot_fail("image is not a boot image");
3348 return;
3349 }
Mayank Grover351a75e2017-05-30 20:06:08 +05303350
3351 /* Reset multislot_partition attributes in case of flashing boot */
3352 if (partition_multislot_is_supported())
3353 {
3354 partition_reset_attributes(index);
3355 }
Mao Jinlong226f33a2014-07-04 17:24:10 +08003356 }
3357
3358 if(!lun_set)
3359 {
3360 lun = partition_get_lun(index);
3361 mmc_set_lun(lun);
3362 }
3363
3364 size = partition_get_size(index);
Gaurav Nebhwani4d2389c2016-03-17 21:10:05 +05303365 if (ROUND_TO_PAGE(sz, mmc_blocksize_mask) > size) {
Mao Jinlong226f33a2014-07-04 17:24:10 +08003366 fastboot_fail("size too large");
3367 return;
3368 }
3369 else if (mmc_write(ptn , sz, (unsigned int *)data)) {
3370 fastboot_fail("flash write failure");
3371 return;
3372 }
Greg Grisco6e754772011-06-23 12:19:39 -07003373 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07003374 }
3375 fastboot_okay("");
3376 return;
3377}
3378
Ajay Dudanide984792015-03-02 09:57:41 -08003379void cmd_flash_meta_img(const char *arg, void *data, unsigned sz)
3380{
3381 int i, images;
3382 meta_header_t *meta_header;
3383 img_header_entry_t *img_header_entry;
Parth Dixit3e2d3032016-03-04 17:11:52 +05303384 /*End of the image address*/
3385 uintptr_t data_end;
3386
3387 if( (UINT_MAX - sz) > (uintptr_t)data )
3388 data_end = (uintptr_t)data + sz;
3389 else
3390 {
3391 fastboot_fail("Cannot flash: image header corrupt");
3392 return;
3393 }
3394
3395 if( data_end < ((uintptr_t)data + sizeof(meta_header_t)))
3396 {
3397 fastboot_fail("Cannot flash: image header corrupt");
3398 return;
3399 }
Ajay Dudanide984792015-03-02 09:57:41 -08003400
lijuang8ee21882016-04-19 16:57:11 +08003401 /* If device is locked:
3402 * Forbid to flash image to avoid the device to bypass the image
3403 * which with "any" name other than bootloader. Because it maybe
3404 * a meta package of all partitions.
3405 */
Monika Singh292b3e92018-03-17 22:40:23 +05303406#if VERIFIED_BOOT || VERIFIED_BOOT_2
lijuang8ee21882016-04-19 16:57:11 +08003407 if (target_build_variant_user()) {
3408 if (!device.is_unlocked) {
3409 fastboot_fail("Device is locked, meta image flashing is not allowed");
3410 return;
3411 }
Mayank Grover889be1b2017-09-12 20:12:23 +05303412
Mayank Grover912eaa62017-10-26 12:08:53 +05303413 if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05303414 !device.is_unlock_critical)
3415 {
lijuang8ee21882016-04-19 16:57:11 +08003416 fastboot_fail("Device is critical locked, Meta image flashing is not allowed");
3417 return;
3418 }
lijuang8ee21882016-04-19 16:57:11 +08003419 }
3420#endif
3421
Ajay Dudanide984792015-03-02 09:57:41 -08003422 meta_header = (meta_header_t*) data;
Parth Dixit3e2d3032016-03-04 17:11:52 +05303423 if( data_end < ((uintptr_t)data + meta_header->img_hdr_sz))
3424 {
3425 fastboot_fail("Cannot flash: image header corrupt");
3426 return;
3427 }
Ajay Dudanide984792015-03-02 09:57:41 -08003428 img_header_entry = (img_header_entry_t*) (data+sizeof(meta_header_t));
3429
3430 images = meta_header->img_hdr_sz / sizeof(img_header_entry_t);
3431
3432 for (i=0; i<images; i++) {
3433
3434 if((img_header_entry[i].ptn_name == NULL) ||
3435 (img_header_entry[i].start_offset == 0) ||
3436 (img_header_entry[i].size == 0))
3437 break;
Kishor PK49831502017-04-21 17:55:43 +05303438 if ((UINT_MAX - img_header_entry[i].start_offset) < (uintptr_t)data) {
3439 fastboot_fail("Integer overflow detected in start_offset of img");
3440 break;
3441 }
3442 else if ((UINT_MAX - (img_header_entry[i].start_offset + (uintptr_t)data)) < img_header_entry[i].size) {
3443 fastboot_fail("Integer overflow detected in size of img");
3444 break;
3445 }
Parth Dixit3e2d3032016-03-04 17:11:52 +05303446 if( data_end < ((uintptr_t)data + img_header_entry[i].start_offset
3447 + img_header_entry[i].size) )
3448 {
3449 fastboot_fail("Cannot flash: image size mismatch");
3450 break;
3451 }
3452
Ajay Dudanide984792015-03-02 09:57:41 -08003453 cmd_flash_mmc_img(img_header_entry[i].ptn_name,
3454 (void *) data + img_header_entry[i].start_offset,
3455 img_header_entry[i].size);
3456 }
3457
Channagoud Kadabiaafbbfc2015-03-04 17:52:56 -08003458 if (!strncmp(arg, "bootloader", strlen("bootloader")))
3459 {
3460 strlcpy(device.bootloader_version, TARGET(BOARD), MAX_VERSION_LEN);
3461 strlcat(device.bootloader_version, "-", MAX_VERSION_LEN);
3462 strlcat(device.bootloader_version, meta_header->img_version, MAX_VERSION_LEN);
3463 }
3464 else
3465 {
3466 strlcpy(device.radio_version, TARGET(BOARD), MAX_VERSION_LEN);
3467 strlcat(device.radio_version, "-", MAX_VERSION_LEN);
3468 strlcat(device.radio_version, meta_header->img_version, MAX_VERSION_LEN);
3469 }
3470
3471 write_device_info(&device);
Ajay Dudanide984792015-03-02 09:57:41 -08003472 fastboot_okay("");
3473 return;
3474}
3475
Ajay Dudani5c761132011-04-07 20:19:04 -07003476void cmd_flash_mmc_sparse_img(const char *arg, void *data, unsigned sz)
3477{
3478 unsigned int chunk;
wufeng.jiang813dc352015-06-02 23:02:46 -04003479 uint64_t chunk_data_sz;
Channagoud Kadabif627cf82013-09-09 14:08:20 -07003480 uint32_t *fill_buf = NULL;
3481 uint32_t fill_val;
Mayank Grover4f50bba2017-07-19 18:17:08 +05303482 uint32_t blk_sz_actual = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07003483 sparse_header_t *sparse_header;
3484 chunk_header_t *chunk_header;
Ajay Dudaniab18f022011-05-12 14:39:22 -07003485 uint32_t total_blocks = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07003486 unsigned long long ptn = 0;
Channagoud Kadabi65b91002011-10-11 17:34:33 +05303487 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07003488 int index = INVALID_PTN;
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05303489 uint32_t i;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003490 uint8_t lun = 0;
vijay kumar800255e2015-04-24 20:26:19 +05303491 /*End of the sparse image address*/
Parth Dixit64b1a482016-03-07 16:31:26 +05303492 uintptr_t data_end = (uintptr_t)data + sz;
Ajay Dudani5c761132011-04-07 20:19:04 -07003493
Kinson Chikf1a43512011-07-14 11:28:39 -07003494 index = partition_get_index(arg);
3495 ptn = partition_get_offset(index);
3496 if(ptn == 0) {
Ajay Dudani5c761132011-04-07 20:19:04 -07003497 fastboot_fail("partition table doesn't exist");
3498 return;
3499 }
3500
Channagoud Kadabi65b91002011-10-11 17:34:33 +05303501 size = partition_get_size(index);
Channagoud Kadabi65b91002011-10-11 17:34:33 +05303502
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003503 lun = partition_get_lun(index);
3504 mmc_set_lun(lun);
3505
vijay kumar800255e2015-04-24 20:26:19 +05303506 if (sz < sizeof(sparse_header_t)) {
3507 fastboot_fail("size too low");
3508 return;
3509 }
3510
Ajay Dudani5c761132011-04-07 20:19:04 -07003511 /* Read and skip over sparse image header */
3512 sparse_header = (sparse_header_t *) data;
vijay kumar800255e2015-04-24 20:26:19 +05303513
Mayank Grover48bd9bb2017-07-19 12:04:16 +05303514 if (!sparse_header->blk_sz || (sparse_header->blk_sz % 4)){
3515 fastboot_fail("Invalid block size\n");
3516 return;
3517 }
3518
vijay kumar1321f342015-03-27 12:13:42 +05303519 if (((uint64_t)sparse_header->total_blks * (uint64_t)sparse_header->blk_sz) > size) {
Ajay Dudani876b3282012-12-21 14:12:17 -08003520 fastboot_fail("size too large");
3521 return;
3522 }
3523
vijay kumarde4fcf62015-04-23 13:05:49 +05303524 data += sizeof(sparse_header_t);
vijay kumar800255e2015-04-24 20:26:19 +05303525
Parth Dixit64b1a482016-03-07 16:31:26 +05303526 if (data_end < (uintptr_t)data) {
vijay kumar800255e2015-04-24 20:26:19 +05303527 fastboot_fail("buffer overreads occured due to invalid sparse header");
3528 return;
3529 }
3530
vijay kumarde4fcf62015-04-23 13:05:49 +05303531 if(sparse_header->file_hdr_sz != sizeof(sparse_header_t))
Ajay Dudani5c761132011-04-07 20:19:04 -07003532 {
vijay kumarde4fcf62015-04-23 13:05:49 +05303533 fastboot_fail("sparse header size mismatch");
3534 return;
Ajay Dudani5c761132011-04-07 20:19:04 -07003535 }
3536
Ajay Dudanib06c05f2011-05-12 14:46:10 -07003537 dprintf (SPEW, "=== Sparse Image Header ===\n");
3538 dprintf (SPEW, "magic: 0x%x\n", sparse_header->magic);
3539 dprintf (SPEW, "major_version: 0x%x\n", sparse_header->major_version);
3540 dprintf (SPEW, "minor_version: 0x%x\n", sparse_header->minor_version);
3541 dprintf (SPEW, "file_hdr_sz: %d\n", sparse_header->file_hdr_sz);
3542 dprintf (SPEW, "chunk_hdr_sz: %d\n", sparse_header->chunk_hdr_sz);
3543 dprintf (SPEW, "blk_sz: %d\n", sparse_header->blk_sz);
3544 dprintf (SPEW, "total_blks: %d\n", sparse_header->total_blks);
3545 dprintf (SPEW, "total_chunks: %d\n", sparse_header->total_chunks);
Ajay Dudani5c761132011-04-07 20:19:04 -07003546
3547 /* Start processing chunks */
3548 for (chunk=0; chunk<sparse_header->total_chunks; chunk++)
3549 {
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05303550 /* Make sure the total image size does not exceed the partition size */
3551 if(((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz) >= size) {
3552 fastboot_fail("size too large");
3553 return;
3554 }
Ajay Dudani5c761132011-04-07 20:19:04 -07003555 /* Read and skip over chunk header */
3556 chunk_header = (chunk_header_t *) data;
3557 data += sizeof(chunk_header_t);
3558
Parth Dixit64b1a482016-03-07 16:31:26 +05303559 if (data_end < (uintptr_t)data) {
vijay kumar800255e2015-04-24 20:26:19 +05303560 fastboot_fail("buffer overreads occured due to invalid sparse header");
3561 return;
3562 }
3563
Ajay Dudani5c761132011-04-07 20:19:04 -07003564 dprintf (SPEW, "=== Chunk Header ===\n");
3565 dprintf (SPEW, "chunk_type: 0x%x\n", chunk_header->chunk_type);
3566 dprintf (SPEW, "chunk_data_sz: 0x%x\n", chunk_header->chunk_sz);
3567 dprintf (SPEW, "total_size: 0x%x\n", chunk_header->total_sz);
3568
vijay kumar800255e2015-04-24 20:26:19 +05303569 if(sparse_header->chunk_hdr_sz != sizeof(chunk_header_t))
Ajay Dudani5c761132011-04-07 20:19:04 -07003570 {
vijay kumar800255e2015-04-24 20:26:19 +05303571 fastboot_fail("chunk header size mismatch");
3572 return;
Ajay Dudani5c761132011-04-07 20:19:04 -07003573 }
3574
wufeng.jiang813dc352015-06-02 23:02:46 -04003575 chunk_data_sz = (uint64_t)sparse_header->blk_sz * chunk_header->chunk_sz;
3576
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05303577 /* Make sure that the chunk size calculated from sparse image does not
3578 * exceed partition size
3579 */
3580 if ((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz + chunk_data_sz > size)
3581 {
3582 fastboot_fail("Chunk data size exceeds partition size");
3583 return;
3584 }
3585
Ajay Dudani5c761132011-04-07 20:19:04 -07003586 switch (chunk_header->chunk_type)
3587 {
3588 case CHUNK_TYPE_RAW:
wufeng.jiang813dc352015-06-02 23:02:46 -04003589 if((uint64_t)chunk_header->total_sz != ((uint64_t)sparse_header->chunk_hdr_sz +
Ajay Dudani5c761132011-04-07 20:19:04 -07003590 chunk_data_sz))
3591 {
3592 fastboot_fail("Bogus chunk size for chunk type Raw");
3593 return;
3594 }
3595
Parth Dixit64b1a482016-03-07 16:31:26 +05303596 if (data_end < (uintptr_t)data + chunk_data_sz) {
vijay kumar800255e2015-04-24 20:26:19 +05303597 fastboot_fail("buffer overreads occured due to invalid sparse header");
3598 return;
3599 }
3600
wufeng.jiang813dc352015-06-02 23:02:46 -04003601 /* chunk_header->total_sz is uint32,So chunk_data_sz is now less than 2^32
3602 otherwise it will return in the line above
3603 */
Ajay Dudaniab18f022011-05-12 14:39:22 -07003604 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
wufeng.jiang813dc352015-06-02 23:02:46 -04003605 (uint32_t)chunk_data_sz,
Ajay Dudaniab18f022011-05-12 14:39:22 -07003606 (unsigned int*)data))
Ajay Dudani5c761132011-04-07 20:19:04 -07003607 {
3608 fastboot_fail("flash write failure");
3609 return;
3610 }
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05303611 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
3612 fastboot_fail("Bogus size for RAW chunk type");
3613 return;
3614 }
Ajay Dudani5c761132011-04-07 20:19:04 -07003615 total_blocks += chunk_header->chunk_sz;
wufeng.jiang813dc352015-06-02 23:02:46 -04003616 data += (uint32_t)chunk_data_sz;
Ajay Dudani5c761132011-04-07 20:19:04 -07003617 break;
3618
Channagoud Kadabif627cf82013-09-09 14:08:20 -07003619 case CHUNK_TYPE_FILL:
3620 if(chunk_header->total_sz != (sparse_header->chunk_hdr_sz +
3621 sizeof(uint32_t)))
3622 {
3623 fastboot_fail("Bogus chunk size for chunk type FILL");
3624 return;
3625 }
3626
Mayank Grover4f50bba2017-07-19 18:17:08 +05303627 blk_sz_actual = ROUNDUP(sparse_header->blk_sz, CACHE_LINE);
3628 /* Integer overflow detected */
3629 if (blk_sz_actual < sparse_header->blk_sz)
3630 {
3631 fastboot_fail("Invalid block size");
3632 return;
3633 }
3634
3635 fill_buf = (uint32_t *)memalign(CACHE_LINE, blk_sz_actual);
Channagoud Kadabif627cf82013-09-09 14:08:20 -07003636 if (!fill_buf)
3637 {
3638 fastboot_fail("Malloc failed for: CHUNK_TYPE_FILL");
3639 return;
3640 }
3641
Parth Dixit64b1a482016-03-07 16:31:26 +05303642 if (data_end < (uintptr_t)data + sizeof(uint32_t)) {
vijay kumar800255e2015-04-24 20:26:19 +05303643 fastboot_fail("buffer overreads occured due to invalid sparse header");
Gaurav Nebhwani163cbf82016-03-17 21:21:27 +05303644 free(fill_buf);
vijay kumar800255e2015-04-24 20:26:19 +05303645 return;
3646 }
Channagoud Kadabif627cf82013-09-09 14:08:20 -07003647 fill_val = *(uint32_t *)data;
3648 data = (char *) data + sizeof(uint32_t);
Channagoud Kadabif627cf82013-09-09 14:08:20 -07003649
3650 for (i = 0; i < (sparse_header->blk_sz / sizeof(fill_val)); i++)
3651 {
3652 fill_buf[i] = fill_val;
3653 }
3654
Gaurav Nebhwanie94cbe12016-03-17 21:16:34 +05303655 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz))
3656 {
3657 fastboot_fail("bogus size for chunk FILL type");
3658 free(fill_buf);
3659 return;
3660 }
3661
wufeng.jiang813dc352015-06-02 23:02:46 -04003662 for (i = 0; i < chunk_header->chunk_sz; i++)
Channagoud Kadabif627cf82013-09-09 14:08:20 -07003663 {
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05303664 /* Make sure that the data written to partition does not exceed partition size */
3665 if ((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz + sparse_header->blk_sz > size)
3666 {
3667 fastboot_fail("Chunk data size for fill type exceeds partition size");
Gaurav Nebhwani163cbf82016-03-17 21:21:27 +05303668 free(fill_buf);
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05303669 return;
3670 }
3671
Channagoud Kadabif627cf82013-09-09 14:08:20 -07003672 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
3673 sparse_header->blk_sz,
3674 fill_buf))
3675 {
3676 fastboot_fail("flash write failure");
3677 free(fill_buf);
3678 return;
3679 }
3680
3681 total_blocks++;
3682 }
3683
3684 free(fill_buf);
3685 break;
3686
Ajay Dudani5c761132011-04-07 20:19:04 -07003687 case CHUNK_TYPE_DONT_CARE:
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05303688 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
3689 fastboot_fail("bogus size for chunk DONT CARE type");
3690 return;
3691 }
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07003692 total_blocks += chunk_header->chunk_sz;
3693 break;
3694
Ajay Dudani5c761132011-04-07 20:19:04 -07003695 case CHUNK_TYPE_CRC:
3696 if(chunk_header->total_sz != sparse_header->chunk_hdr_sz)
3697 {
wufeng.jiang813dc352015-06-02 23:02:46 -04003698 fastboot_fail("Bogus chunk size for chunk type CRC");
Ajay Dudani5c761132011-04-07 20:19:04 -07003699 return;
3700 }
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05303701 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
3702 fastboot_fail("bogus size for chunk CRC type");
3703 return;
3704 }
Ajay Dudani5c761132011-04-07 20:19:04 -07003705 total_blocks += chunk_header->chunk_sz;
Parth Dixit64b1a482016-03-07 16:31:26 +05303706 if ((uintptr_t)data > UINT_MAX - chunk_data_sz) {
vijay kumar800255e2015-04-24 20:26:19 +05303707 fastboot_fail("integer overflow occured");
3708 return;
3709 }
wufeng.jiang813dc352015-06-02 23:02:46 -04003710 data += (uint32_t)chunk_data_sz;
Parth Dixit64b1a482016-03-07 16:31:26 +05303711 if (data_end < (uintptr_t)data) {
vijay kumar800255e2015-04-24 20:26:19 +05303712 fastboot_fail("buffer overreads occured due to invalid sparse header");
3713 return;
3714 }
Ajay Dudani5c761132011-04-07 20:19:04 -07003715 break;
3716
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07003717 default:
Channagoud Kadabif627cf82013-09-09 14:08:20 -07003718 dprintf(CRITICAL, "Unkown chunk type: %x\n",chunk_header->chunk_type);
Ajay Dudani5c761132011-04-07 20:19:04 -07003719 fastboot_fail("Unknown chunk type");
3720 return;
3721 }
3722 }
3723
Ajay Dudani0c6927b2011-05-18 11:12:16 -07003724 dprintf(INFO, "Wrote %d blocks, expected to write %d blocks\n",
3725 total_blocks, sparse_header->total_blks);
3726
3727 if(total_blocks != sparse_header->total_blks)
3728 {
3729 fastboot_fail("sparse image write failure");
3730 }
Ajay Dudani5c761132011-04-07 20:19:04 -07003731
3732 fastboot_okay("");
3733 return;
3734}
3735
3736void cmd_flash_mmc(const char *arg, void *data, unsigned sz)
3737{
3738 sparse_header_t *sparse_header;
Ajay Dudanide984792015-03-02 09:57:41 -08003739 meta_header_t *meta_header;
Ajay Dudani5c761132011-04-07 20:19:04 -07003740
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07003741#ifdef SSD_ENABLE
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -08003742 /* 8 Byte Magic + 2048 Byte xml + Encrypted Data */
3743 unsigned int *magic_number = (unsigned int *) data;
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08003744 int ret=0;
3745 uint32 major_version=0;
3746 uint32 minor_version=0;
3747
3748 ret = scm_svc_version(&major_version,&minor_version);
3749 if(!ret)
3750 {
3751 if(major_version >= 2)
3752 {
Amir Samuelovbb65ce02013-05-05 12:20:18 +03003753 if( !strcmp(arg, "ssd") || !strcmp(arg, "tqs") )
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08003754 {
3755 ret = encrypt_scm((uint32 **) &data, &sz);
3756 if (ret != 0) {
3757 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
3758 return;
3759 }
3760
Amir Samuelovbb65ce02013-05-05 12:20:18 +03003761 /* Protect only for SSD */
3762 if (!strcmp(arg, "ssd")) {
3763 ret = scm_protect_keystore((uint32 *) data, sz);
3764 if (ret != 0) {
3765 dprintf(CRITICAL, "ERROR: scm_protect_keystore Failed\n");
3766 return;
3767 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08003768 }
3769 }
3770 else
3771 {
3772 ret = decrypt_scm_v2((uint32 **) &data, &sz);
3773 if(ret != 0)
3774 {
3775 dprintf(CRITICAL,"ERROR: Decryption Failure\n");
3776 return;
3777 }
3778 }
3779 }
3780 else
3781 {
3782 if (magic_number[0] == DECRYPT_MAGIC_0 &&
3783 magic_number[1] == DECRYPT_MAGIC_1)
3784 {
3785 ret = decrypt_scm((uint32 **) &data, &sz);
3786 if (ret != 0) {
3787 dprintf(CRITICAL, "ERROR: Invalid secure image\n");
3788 return;
3789 }
3790 }
3791 else if (magic_number[0] == ENCRYPT_MAGIC_0 &&
3792 magic_number[1] == ENCRYPT_MAGIC_1)
3793 {
3794 ret = encrypt_scm((uint32 **) &data, &sz);
3795 if (ret != 0) {
3796 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
3797 return;
3798 }
3799 }
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07003800 }
3801 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08003802 else
Neeti Desai127b9e02012-03-20 16:11:23 -07003803 {
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08003804 dprintf(CRITICAL,"INVALID SVC Version\n");
3805 return;
Neeti Desai127b9e02012-03-20 16:11:23 -07003806 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08003807#endif /* SSD_ENABLE */
Neeti Desai127b9e02012-03-20 16:11:23 -07003808
Monika Singh292b3e92018-03-17 22:40:23 +05303809#if VERIFIED_BOOT || VERIFIED_BOOT_2
Channagoud Kadabi35297672015-06-13 11:09:49 -07003810 if (target_build_variant_user())
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003811 {
lijuang511a2b52015-08-14 20:50:51 +08003812 /* if device is locked:
3813 * common partition will not allow to be flashed
3814 * critical partition will allow to flash image.
3815 */
3816 if(!device.is_unlocked && !critical_flash_allowed(arg)) {
3817 fastboot_fail("Partition flashing is not allowed");
3818 return;
3819 }
Mayank Grover889be1b2017-09-12 20:12:23 +05303820
lijuang511a2b52015-08-14 20:50:51 +08003821 /* if device critical is locked:
3822 * common partition will allow to be flashed
3823 * critical partition will not allow to flash image.
3824 */
Mayank Grover912eaa62017-10-26 12:08:53 +05303825 if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05303826 !device.is_unlock_critical &&
3827 critical_flash_allowed(arg)) {
3828 fastboot_fail("Critical partition flashing is not allowed");
3829 return;
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003830 }
3831 }
3832#endif
Monika Singh292b3e92018-03-17 22:40:23 +05303833 if (!strncmp(arg, "avb_custom_key", strlen("avb_custom_key"))) {
3834 dprintf(INFO, "flashing avb_custom_key\n");
3835 if (store_userkey(data, sz)) {
3836 fastboot_fail("Flashing avb_custom_key failed");
3837 } else {
3838 fastboot_okay("");
3839 }
3840 return;
3841 }
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003842
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07003843 sparse_header = (sparse_header_t *) data;
Ajay Dudanide984792015-03-02 09:57:41 -08003844 meta_header = (meta_header_t *) data;
3845 if (sparse_header->magic == SPARSE_HEADER_MAGIC)
Ajay Dudani5c761132011-04-07 20:19:04 -07003846 cmd_flash_mmc_sparse_img(arg, data, sz);
Ajay Dudanide984792015-03-02 09:57:41 -08003847 else if (meta_header->magic == META_HEADER_MAGIC)
3848 cmd_flash_meta_img(arg, data, sz);
3849 else
3850 cmd_flash_mmc_img(arg, data, sz);
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07003851
3852#if VERIFIED_BOOT
Mayank Grover912eaa62017-10-26 12:08:53 +05303853 if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05303854 (!strncmp(arg, "system", 6)) &&
3855 !device.verity_mode)
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07003856 // reset dm_verity mode to enforcing
3857 device.verity_mode = 1;
3858 write_device_info(&device);
Parth Dixitddbc7352015-10-18 03:13:31 +05303859#endif
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07003860
Ajay Dudani5c761132011-04-07 20:19:04 -07003861 return;
3862}
3863
Tanya Brokhman84eaaf62015-02-24 21:57:06 +02003864void cmd_updatevol(const char *vol_name, void *data, unsigned sz)
3865{
3866 struct ptentry *sys_ptn;
3867 struct ptable *ptable;
3868
3869 ptable = flash_get_ptable();
3870 if (ptable == NULL) {
3871 fastboot_fail("partition table doesn't exist");
3872 return;
3873 }
3874
3875 sys_ptn = ptable_find(ptable, "system");
3876 if (sys_ptn == NULL) {
3877 fastboot_fail("system partition not found");
3878 return;
3879 }
3880
3881 sz = ROUND_TO_PAGE(sz, page_mask);
3882 if (update_ubi_vol(sys_ptn, vol_name, data, sz))
3883 fastboot_fail("update_ubi_vol failed");
3884 else
3885 fastboot_okay("");
3886}
3887
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003888void cmd_flash_nand(const char *arg, void *data, unsigned sz)
Dima Zavin214cc642009-01-26 11:16:21 -08003889{
3890 struct ptentry *ptn;
3891 struct ptable *ptable;
3892 unsigned extra = 0;
Gaurav Nebhwanid9730712016-05-06 14:28:29 +05303893 uint64_t partition_size = 0;
Vijay Kumar Pendotic6f22a72017-05-19 22:44:34 +05303894 unsigned bytes_to_round_page = 0;
3895 unsigned rounded_size = 0;
Dima Zavin214cc642009-01-26 11:16:21 -08003896
Kishor PK38ed93d2017-04-25 14:19:26 +05303897 if((uintptr_t)data > (UINT_MAX - sz)) {
3898 fastboot_fail("Cannot flash: image header corrupt");
3899 return;
3900 }
3901
Dima Zavin214cc642009-01-26 11:16:21 -08003902 ptable = flash_get_ptable();
3903 if (ptable == NULL) {
3904 fastboot_fail("partition table doesn't exist");
3905 return;
3906 }
3907
3908 ptn = ptable_find(ptable, arg);
3909 if (ptn == NULL) {
Tanya Brokhman84eaaf62015-02-24 21:57:06 +02003910 dprintf(INFO, "unknown partition name (%s). Trying updatevol\n",
3911 arg);
3912 cmd_updatevol(arg, data, sz);
Dima Zavin214cc642009-01-26 11:16:21 -08003913 return;
3914 }
3915
Monika Singh292b3e92018-03-17 22:40:23 +05303916 if (!strncmp(arg, "avb_custom_key", strlen("avb_custom_key"))) {
3917 dprintf(INFO, "flashing avb_custom_key\n");
3918 if (store_userkey(data, sz)) {
3919 fastboot_fail("Flashing avb_custom_key failed");
3920 } else {
3921 fastboot_okay("");
3922 }
3923 return;
3924 }
3925
Dima Zavin214cc642009-01-26 11:16:21 -08003926 if (!strcmp(ptn->name, "boot") || !strcmp(ptn->name, "recovery")) {
Kishor PK38ed93d2017-04-25 14:19:26 +05303927 if((sz > BOOT_MAGIC_SIZE) && (!memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE))) {
3928 dprintf(INFO, "Verified the BOOT_MAGIC in image header \n");
3929 } else {
3930 fastboot_fail("Image is not a boot image");
Dima Zavin214cc642009-01-26 11:16:21 -08003931 return;
3932 }
3933 }
3934
Amol Jadi5c61a952012-05-04 17:05:35 -07003935 if (!strcmp(ptn->name, "system")
Deepa Dinamani13e32c42012-03-12 14:34:17 -07003936 || !strcmp(ptn->name, "userdata")
3937 || !strcmp(ptn->name, "persist")
Sundarajan Srinivasanb063a852013-11-19 14:02:27 -08003938 || !strcmp(ptn->name, "recoveryfs")
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -08003939 || !strcmp(ptn->name, "modem"))
Tanya Brokhman1c94f1a2015-02-15 09:05:03 +02003940 extra = 1;
Kishor PK38ed93d2017-04-25 14:19:26 +05303941 else {
Vijay Kumar Pendotic6f22a72017-05-19 22:44:34 +05303942 rounded_size = ROUNDUP(sz, page_size);
3943 bytes_to_round_page = rounded_size - sz;
3944 if (bytes_to_round_page) {
3945 if (((uintptr_t)data + sz ) > (UINT_MAX - bytes_to_round_page)) {
3946 fastboot_fail("Integer overflow detected");
3947 return;
3948 }
3949 if (((uintptr_t)data + sz + bytes_to_round_page) >
3950 ((uintptr_t)target_get_scratch_address() + target_get_max_flash_size())) {
3951 fastboot_fail("Buffer size is not aligned to page_size");
3952 return;
3953 }
3954 else {
3955 memset(data + sz, 0, bytes_to_round_page);
3956 sz = rounded_size;
3957 }
Kishor PK38ed93d2017-04-25 14:19:26 +05303958 }
Gaurav Nebhwanid9730712016-05-06 14:28:29 +05303959 }
3960
Kishor PK38ed93d2017-04-25 14:19:26 +05303961 /*Checking partition_size for the possible integer overflow */
3962 partition_size = validate_partition_size(ptn);
3963
Gaurav Nebhwanid9730712016-05-06 14:28:29 +05303964 if (sz > partition_size) {
3965 fastboot_fail("Image size too large");
3966 return;
3967 }
3968
Dima Zavin214cc642009-01-26 11:16:21 -08003969 dprintf(INFO, "writing %d bytes to '%s'\n", sz, ptn->name);
Mayank Grover6cde9352017-06-06 18:45:23 +05303970 if ((sz > UBI_EC_HDR_SIZE) &&
3971 (!memcmp((void *)data, UBI_MAGIC, UBI_MAGIC_SIZE))) {
Tanya Brokhman1c94f1a2015-02-15 09:05:03 +02003972 if (flash_ubi_img(ptn, data, sz)) {
3973 fastboot_fail("flash write failure");
3974 return;
3975 }
3976 } else {
3977 if (flash_write(ptn, extra, data, sz)) {
3978 fastboot_fail("flash write failure");
3979 return;
3980 }
Dima Zavin214cc642009-01-26 11:16:21 -08003981 }
3982 dprintf(INFO, "partition '%s' updated\n", ptn->name);
3983 fastboot_okay("");
3984}
3985
Kishor PK38ed93d2017-04-25 14:19:26 +05303986
3987static inline uint64_t validate_partition_size(struct ptentry *ptn)
3988{
3989 if (ptn->length && flash_num_pages_per_blk() && page_size) {
3990 if ((ptn->length < ( UINT_MAX / flash_num_pages_per_blk())) && ((ptn->length * flash_num_pages_per_blk()) < ( UINT_MAX / page_size))) {
3991 return ptn->length * flash_num_pages_per_blk() * page_size;
3992 }
3993 }
3994 return 0;
3995}
3996
3997
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003998void cmd_flash(const char *arg, void *data, unsigned sz)
3999{
4000 if(target_is_emmc_boot())
4001 cmd_flash_mmc(arg, data, sz);
4002 else
4003 cmd_flash_nand(arg, data, sz);
4004}
4005
Dima Zavin214cc642009-01-26 11:16:21 -08004006void cmd_continue(const char *arg, void *data, unsigned sz)
4007{
4008 fastboot_okay("");
Amol Jadi7c4316c2013-10-07 14:19:26 -07004009 fastboot_stop();
Shashank Mittald3e54dd2014-08-28 15:24:02 -07004010
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004011 if (target_is_emmc_boot())
4012 {
lijuanga40d6302015-07-20 20:10:13 +08004013#if FBCON_DISPLAY_MSG
lijuangde34d502016-02-26 16:04:50 +08004014 /* Exit keys' detection thread firstly */
4015 exit_menu_keys_detection();
lijuanga40d6302015-07-20 20:10:13 +08004016#endif
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004017 boot_linux_from_mmc();
4018 }
4019 else
4020 {
4021 boot_linux_from_flash();
4022 }
Dima Zavin214cc642009-01-26 11:16:21 -08004023}
4024
Chandan Uddaraju94183c02010-01-15 15:13:59 -08004025void cmd_reboot(const char *arg, void *data, unsigned sz)
4026{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004027 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08004028 fastboot_okay("");
4029 reboot_device(0);
4030}
4031
Mayank Grover351a75e2017-05-30 20:06:08 +05304032void cmd_set_active(const char *arg, void *data, unsigned sz)
4033{
Mayank Grover6ccc1c92017-07-04 17:36:46 +05304034 char *p, *sp = NULL;
Mayank Grover351a75e2017-05-30 20:06:08 +05304035 unsigned i,current_active_slot;
4036 const char *current_slot_suffix;
4037
4038 if (!partition_multislot_is_supported())
4039 {
4040 fastboot_fail("Command not supported");
4041 return;
4042 }
4043
4044 if (arg)
4045 {
Mayank Grover6ccc1c92017-07-04 17:36:46 +05304046 p = strtok_r((char *)arg, ":", &sp);
Mayank Grover5eb5f692017-07-19 20:16:04 +05304047 if (p)
Mayank Grover351a75e2017-05-30 20:06:08 +05304048 {
4049 current_active_slot = partition_find_active_slot();
4050
4051 /* Check if trying to make curent slot active */
4052 current_slot_suffix = SUFFIX_SLOT(current_active_slot);
Mayank Grover6ccc1c92017-07-04 17:36:46 +05304053 current_slot_suffix = strtok_r((char *)current_slot_suffix,
4054 (char *)suffix_delimiter, &sp);
4055
Mayank Grover5eb5f692017-07-19 20:16:04 +05304056 if (current_slot_suffix &&
4057 !strncmp(p, current_slot_suffix, strlen(current_slot_suffix)))
Mayank Grover351a75e2017-05-30 20:06:08 +05304058 {
4059 fastboot_okay("Slot already set active");
4060 return;
4061 }
4062 else
4063 {
4064 for (i = 0; i < AB_SUPPORTED_SLOTS; i++)
4065 {
4066 current_slot_suffix = SUFFIX_SLOT(i);
Mayank Grover6ccc1c92017-07-04 17:36:46 +05304067 current_slot_suffix = strtok_r((char *)current_slot_suffix,
4068 (char *)suffix_delimiter, &sp);
Mayank Grover5eb5f692017-07-19 20:16:04 +05304069 if (current_slot_suffix &&
4070 !strncmp(p, current_slot_suffix, strlen(current_slot_suffix)))
Mayank Grover351a75e2017-05-30 20:06:08 +05304071 {
4072 partition_switch_slots(current_active_slot, i);
4073 publish_getvar_multislot_vars();
4074 fastboot_okay("");
4075 return;
4076 }
4077 }
4078 }
4079 }
4080 }
4081 fastboot_fail("Invalid slot suffix.");
4082 return;
4083}
4084
Chandan Uddaraju94183c02010-01-15 15:13:59 -08004085void cmd_reboot_bootloader(const char *arg, void *data, unsigned sz)
4086{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004087 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08004088 fastboot_okay("");
4089 reboot_device(FASTBOOT_MODE);
4090}
4091
Ameya Thakur11cf1a62013-08-05 12:44:48 -07004092void cmd_oem_enable_charger_screen(const char *arg, void *data, unsigned size)
4093{
4094 dprintf(INFO, "Enabling charger screen check\n");
4095 device.charger_screen_enabled = 1;
4096 write_device_info(&device);
4097 fastboot_okay("");
4098}
4099
4100void cmd_oem_disable_charger_screen(const char *arg, void *data, unsigned size)
4101{
4102 dprintf(INFO, "Disabling charger screen check\n");
4103 device.charger_screen_enabled = 0;
4104 write_device_info(&device);
4105 fastboot_okay("");
4106}
4107
lijuanga25d8bb2015-09-16 13:11:52 +08004108void cmd_oem_off_mode_charger(const char *arg, void *data, unsigned size)
4109{
4110 char *p = NULL;
4111 const char *delim = " \t\n\r";
Parth Dixit407f15b2016-01-07 14:47:37 +05304112 char *sp;
lijuanga25d8bb2015-09-16 13:11:52 +08004113
4114 if (arg) {
Parth Dixit407f15b2016-01-07 14:47:37 +05304115 p = strtok_r((char *)arg, delim, &sp);
lijuanga25d8bb2015-09-16 13:11:52 +08004116 if (p) {
4117 if (!strncmp(p, "0", 1)) {
4118 device.charger_screen_enabled = 0;
4119 } else if (!strncmp(p, "1", 1)) {
4120 device.charger_screen_enabled = 1;
4121 }
4122 }
4123 }
4124
4125 /* update charger_screen_enabled value for getvar
4126 * command
4127 */
4128 snprintf(charger_screen_enabled, MAX_RSP_SIZE, "%d",
4129 device.charger_screen_enabled);
4130
4131 write_device_info(&device);
4132 fastboot_okay("");
4133}
4134
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05304135void cmd_oem_select_display_panel(const char *arg, void *data, unsigned size)
4136{
4137 dprintf(INFO, "Selecting display panel %s\n", arg);
4138 if (arg)
4139 strlcpy(device.display_panel, arg,
4140 sizeof(device.display_panel));
4141 write_device_info(&device);
4142 fastboot_okay("");
4143}
4144
Shashank Mittal162244e2011-08-08 19:01:25 -07004145void cmd_oem_unlock(const char *arg, void *data, unsigned sz)
4146{
lijuang4ece1e72015-08-14 21:02:36 +08004147 set_device_unlock(UNLOCK, TRUE);
vijay kumarc65876c2015-04-24 13:29:16 +05304148}
4149
4150void cmd_oem_unlock_go(const char *arg, void *data, unsigned sz)
4151{
lijuang4ece1e72015-08-14 21:02:36 +08004152 if(!device.is_unlocked) {
vijay kumarc65876c2015-04-24 13:29:16 +05304153 if(!is_allow_unlock) {
4154 fastboot_fail("oem unlock is not allowed");
4155 return;
4156 }
4157
lijuang4ece1e72015-08-14 21:02:36 +08004158 set_device_unlock_value(UNLOCK, TRUE);
vijay kumarc65876c2015-04-24 13:29:16 +05304159
lijuang4ece1e72015-08-14 21:02:36 +08004160 /* wipe data */
vijay kumarc65876c2015-04-24 13:29:16 +05304161 struct recovery_message msg;
Kishor PK60a68212017-05-08 16:55:57 +05304162 memset(&msg, 0, sizeof(msg));
vijay kumarc65876c2015-04-24 13:29:16 +05304163 snprintf(msg.recovery, sizeof(msg.recovery), "recovery\n--wipe_data");
4164 write_misc(0, &msg, sizeof(msg));
4165
4166 fastboot_okay("");
4167 reboot_device(RECOVERY_MODE);
Shashank Mittald3e54dd2014-08-28 15:24:02 -07004168 }
4169 fastboot_okay("");
4170}
4171
Channagoud Kadabiad259832015-05-29 11:14:17 -07004172static int aboot_frp_unlock(char *pname, void *data, unsigned sz)
4173{
Mayank Grover70f8b0e2017-05-17 11:49:00 +05304174 int ret=1;
4175 bool authentication_success=false;
Channagoud Kadabiad259832015-05-29 11:14:17 -07004176
Mayank Grover70f8b0e2017-05-17 11:49:00 +05304177 /*
4178 Authentication method not implemented.
4179
4180 OEM to implement, authentication system which on successful validataion,
4181 calls write_allow_oem_unlock() with is_allow_unlock.
4182 */
4183#if 0
4184 authentication_success = oem_specific_auth_mthd();
4185#endif
4186
4187 if (authentication_success)
Channagoud Kadabiad259832015-05-29 11:14:17 -07004188 {
Mayank Grover70f8b0e2017-05-17 11:49:00 +05304189 is_allow_unlock = true;
4190 write_allow_oem_unlock(is_allow_unlock);
4191 ret = 0;
Channagoud Kadabiad259832015-05-29 11:14:17 -07004192 }
4193 return ret;
4194}
4195
Shashank Mittald3e54dd2014-08-28 15:24:02 -07004196void cmd_oem_lock(const char *arg, void *data, unsigned sz)
4197{
lijuang4ece1e72015-08-14 21:02:36 +08004198 set_device_unlock(UNLOCK, FALSE);
Shashank Mittal162244e2011-08-08 19:01:25 -07004199}
4200
Shashank Mittala0032282011-08-26 14:50:11 -07004201void cmd_oem_devinfo(const char *arg, void *data, unsigned sz)
4202{
lijuang511a2b52015-08-14 20:50:51 +08004203 char response[MAX_RSP_SIZE];
Ameya Thakur11cf1a62013-08-05 12:44:48 -07004204 snprintf(response, sizeof(response), "\tDevice tampered: %s", (device.is_tampered ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07004205 fastboot_info(response);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07004206 snprintf(response, sizeof(response), "\tDevice unlocked: %s", (device.is_unlocked ? "true" : "false"));
4207 fastboot_info(response);
Monika Singh292b3e92018-03-17 22:40:23 +05304208#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover912eaa62017-10-26 12:08:53 +05304209 if (VB_M <= target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +05304210 {
4211 snprintf(response, sizeof(response), "\tDevice critical unlocked: %s",
4212 (device.is_unlock_critical ? "true" : "false"));
4213 fastboot_info(response);
4214 }
Parth Dixitddbc7352015-10-18 03:13:31 +05304215#endif
Ameya Thakur11cf1a62013-08-05 12:44:48 -07004216 snprintf(response, sizeof(response), "\tCharger screen enabled: %s", (device.charger_screen_enabled ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07004217 fastboot_info(response);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05304218 snprintf(response, sizeof(response), "\tDisplay panel: %s", (device.display_panel));
4219 fastboot_info(response);
Shashank Mittala0032282011-08-26 14:50:11 -07004220 fastboot_okay("");
4221}
4222
lijuang511a2b52015-08-14 20:50:51 +08004223void cmd_flashing_get_unlock_ability(const char *arg, void *data, unsigned sz)
4224{
4225 char response[MAX_RSP_SIZE];
4226 snprintf(response, sizeof(response), "\tget_unlock_ability: %d", is_allow_unlock);
4227 fastboot_info(response);
4228 fastboot_okay("");
4229}
4230
4231void cmd_flashing_lock_critical(const char *arg, void *data, unsigned sz)
4232{
lijuang4ece1e72015-08-14 21:02:36 +08004233 set_device_unlock(UNLOCK_CRITICAL, FALSE);
lijuang511a2b52015-08-14 20:50:51 +08004234}
4235
4236void cmd_flashing_unlock_critical(const char *arg, void *data, unsigned sz)
4237{
lijuang4ece1e72015-08-14 21:02:36 +08004238 set_device_unlock(UNLOCK_CRITICAL, TRUE);
lijuang511a2b52015-08-14 20:50:51 +08004239}
4240
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07004241void cmd_preflash(const char *arg, void *data, unsigned sz)
4242{
4243 fastboot_okay("");
4244}
4245
Mao Flynn7b379f32015-04-20 00:28:30 +08004246static uint8_t logo_header[LOGO_IMG_HEADER_SIZE];
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304247
Mao Flynn7b379f32015-04-20 00:28:30 +08004248int splash_screen_check_header(logo_img_header *header)
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304249{
Mao Flynn7b379f32015-04-20 00:28:30 +08004250 if (memcmp(header->magic, LOGO_IMG_MAGIC, 8))
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304251 return -1;
Mao Flynn7b379f32015-04-20 00:28:30 +08004252 if (header->width == 0 || header->height == 0)
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304253 return -1;
4254 return 0;
4255}
4256
Mao Flynn7b379f32015-04-20 00:28:30 +08004257int splash_screen_flash()
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07004258{
4259 struct ptentry *ptn;
4260 struct ptable *ptable;
Mao Flynn7b379f32015-04-20 00:28:30 +08004261 struct logo_img_header *header;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07004262 struct fbcon_config *fb_display = NULL;
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08004263
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304264 ptable = flash_get_ptable();
4265 if (ptable == NULL) {
Mao Flynn7b379f32015-04-20 00:28:30 +08004266 dprintf(CRITICAL, "ERROR: Partition table not found\n");
4267 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304268 }
Mao Flynn7b379f32015-04-20 00:28:30 +08004269
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304270 ptn = ptable_find(ptable, "splash");
4271 if (ptn == NULL) {
4272 dprintf(CRITICAL, "ERROR: splash Partition not found\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08004273 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304274 }
Mao Flynn7b379f32015-04-20 00:28:30 +08004275 if (flash_read(ptn, 0, (void *)logo_header, LOGO_IMG_HEADER_SIZE)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304276 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08004277 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304278 }
4279
Mao Flynn7b379f32015-04-20 00:28:30 +08004280 header = (struct logo_img_header *)logo_header;
4281 if (splash_screen_check_header(header)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304282 dprintf(CRITICAL, "ERROR: Boot image header invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08004283 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304284 }
4285
4286 fb_display = fbcon_display();
4287 if (fb_display) {
Mao Flynn7b379f32015-04-20 00:28:30 +08004288 if (header->type && (header->blocks != 0)) { // RLE24 compressed data
4289 uint8_t *base = (uint8_t *) fb_display->base + LOGO_IMG_OFFSET;
4290
4291 /* if the logo is full-screen size, remove "fbcon_clear()" */
4292 if ((header->width != fb_display->width)
4293 || (header->height != fb_display->height))
4294 fbcon_clear();
4295
4296 if (flash_read(ptn + LOGO_IMG_HEADER_SIZE, 0,
4297 (uint32_t *)base,
4298 (header->blocks * 512))) {
4299 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
4300 return -1;
4301 }
4302 fbcon_extract_to_screen(header, base);
4303 return 0;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07004304 }
Mao Flynn7b379f32015-04-20 00:28:30 +08004305
Veera Sundaram Sankaranc33416f2015-09-28 10:25:55 -07004306 if ((header->width > fb_display->width) || (header->height > fb_display->height)) {
4307 dprintf(CRITICAL, "Logo config greater than fb config. Fall back default logo\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08004308 return -1;
4309 }
4310
Vineet Bajaj99291ed2014-09-09 12:29:46 +05304311 uint8_t *base = (uint8_t *) fb_display->base;
Sachin Bhayare619e9e42017-05-15 13:10:31 +05304312 uint32_t fb_size = ROUNDUP(fb_display->width *
4313 fb_display->height *
4314 (fb_display->bpp / 8), 4096);
4315 uint32_t splash_size = ((((header->width * header->height *
4316 fb_display->bpp/8) + 511) >> 9) << 9);
4317
4318 if (splash_size > fb_size) {
4319 dprintf(CRITICAL, "ERROR: Splash image size invalid\n");
4320 return -1;
4321 }
4322
Mao Flynn7b379f32015-04-20 00:28:30 +08004323 if (flash_read(ptn + LOGO_IMG_HEADER_SIZE, 0,
4324 (uint32_t *)base,
4325 ((((header->width * header->height * fb_display->bpp/8) + 511) >> 9) << 9))) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304326 fbcon_clear();
Vineet Bajaj99291ed2014-09-09 12:29:46 +05304327 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08004328 return -1;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07004329 }
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304330 }
4331
Mao Flynn7b379f32015-04-20 00:28:30 +08004332 return 0;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304333}
4334
Mao Flynn7b379f32015-04-20 00:28:30 +08004335int splash_screen_mmc()
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304336{
4337 int index = INVALID_PTN;
4338 unsigned long long ptn = 0;
4339 struct fbcon_config *fb_display = NULL;
Mao Flynn7b379f32015-04-20 00:28:30 +08004340 struct logo_img_header *header;
Mao Flynn1893a7f2015-06-03 12:03:36 +08004341 uint32_t blocksize, realsize, readsize;
4342 uint8_t *base;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304343
4344 index = partition_get_index("splash");
4345 if (index == 0) {
4346 dprintf(CRITICAL, "ERROR: splash Partition table not found\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08004347 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304348 }
4349
4350 ptn = partition_get_offset(index);
4351 if (ptn == 0) {
4352 dprintf(CRITICAL, "ERROR: splash Partition invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08004353 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304354 }
4355
Mao Flynn1893a7f2015-06-03 12:03:36 +08004356 mmc_set_lun(partition_get_lun(index));
4357
4358 blocksize = mmc_get_device_blocksize();
4359 if (blocksize == 0) {
4360 dprintf(CRITICAL, "ERROR:splash Partition invalid blocksize\n");
4361 return -1;
4362 }
4363
4364 fb_display = fbcon_display();
Channagoud Kadabidaf10a62015-07-22 11:51:55 -07004365 if (!fb_display)
4366 {
4367 dprintf(CRITICAL, "ERROR: fb config is not allocated\n");
4368 return -1;
4369 }
4370
Mao Flynn1893a7f2015-06-03 12:03:36 +08004371 base = (uint8_t *) fb_display->base;
4372
Kishor PKee5c0a32018-03-06 16:49:46 +05304373 if (mmc_read(ptn + PLL_CODES_OFFSET, (uint32_t *)(base + LOGO_IMG_OFFSET), blocksize)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304374 dprintf(CRITICAL, "ERROR: Cannot read splash image header\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08004375 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304376 }
4377
Mao Flynn1893a7f2015-06-03 12:03:36 +08004378 header = (struct logo_img_header *)(base + LOGO_IMG_OFFSET);
Mao Flynn7b379f32015-04-20 00:28:30 +08004379 if (splash_screen_check_header(header)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304380 dprintf(CRITICAL, "ERROR: Splash image header invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08004381 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304382 }
4383
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304384 if (fb_display) {
Mao Flynn1893a7f2015-06-03 12:03:36 +08004385 if (header->type && (header->blocks != 0)) { /* 1 RLE24 compressed data */
4386 base += LOGO_IMG_OFFSET;
Mao Flynn7b379f32015-04-20 00:28:30 +08004387
Mao Flynn1893a7f2015-06-03 12:03:36 +08004388 realsize = header->blocks * 512;
4389 readsize = ROUNDUP((realsize + LOGO_IMG_HEADER_SIZE), blocksize) - blocksize;
4390
4391 /* if the logo is not full-screen size, clean screen */
Mao Flynn7b379f32015-04-20 00:28:30 +08004392 if ((header->width != fb_display->width)
4393 || (header->height != fb_display->height))
4394 fbcon_clear();
4395
Sachin Bhayare619e9e42017-05-15 13:10:31 +05304396 uint32_t fb_size = ROUNDUP(fb_display->width *
4397 fb_display->height *
4398 (fb_display->bpp / 8), 4096);
4399
4400 if (readsize > fb_size) {
4401 dprintf(CRITICAL, "ERROR: Splash image size invalid\n");
4402 return -1;
4403 }
4404
Kishor PKee5c0a32018-03-06 16:49:46 +05304405 if (mmc_read(ptn + PLL_CODES_OFFSET + blocksize, (uint32_t *)(base + blocksize), readsize)) {
Mao Flynn7b379f32015-04-20 00:28:30 +08004406 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
4407 return -1;
4408 }
Mao Flynn7b379f32015-04-20 00:28:30 +08004409
Mao Flynn1893a7f2015-06-03 12:03:36 +08004410 fbcon_extract_to_screen(header, (base + LOGO_IMG_HEADER_SIZE));
4411 } else { /* 2 Raw BGR data */
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304412
Veera Sundaram Sankaranc33416f2015-09-28 10:25:55 -07004413 if ((header->width > fb_display->width) || (header->height > fb_display->height)) {
4414 dprintf(CRITICAL, "Logo config greater than fb config. Fall back default logo\n");
Mao Flynn1893a7f2015-06-03 12:03:36 +08004415 return -1;
4416 }
4417
4418 realsize = header->width * header->height * fb_display->bpp / 8;
4419 readsize = ROUNDUP((realsize + LOGO_IMG_HEADER_SIZE), blocksize) - blocksize;
4420
4421 if (blocksize == LOGO_IMG_HEADER_SIZE) { /* read the content directly */
Kishor PKee5c0a32018-03-06 16:49:46 +05304422 if (mmc_read((ptn + PLL_CODES_OFFSET + LOGO_IMG_HEADER_SIZE), (uint32_t *)base, readsize)) {
Mao Flynn1893a7f2015-06-03 12:03:36 +08004423 fbcon_clear();
4424 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
4425 return -1;
4426 }
4427 } else {
Kishor PKee5c0a32018-03-06 16:49:46 +05304428 if (mmc_read(ptn + PLL_CODES_OFFSET + blocksize ,
Mao Flynn1893a7f2015-06-03 12:03:36 +08004429 (uint32_t *)(base + LOGO_IMG_OFFSET + blocksize), readsize)) {
4430 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
4431 return -1;
4432 }
4433 memmove(base, (base + LOGO_IMG_OFFSET + LOGO_IMG_HEADER_SIZE), realsize);
4434 }
4435 }
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304436 }
4437
Mao Flynn7b379f32015-04-20 00:28:30 +08004438 return 0;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304439}
4440
Mao Flynn7b379f32015-04-20 00:28:30 +08004441int fetch_image_from_partition()
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304442{
4443 if (target_is_emmc_boot()) {
4444 return splash_screen_mmc();
4445 } else {
4446 return splash_screen_flash();
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07004447 }
4448}
4449
Mayank Grover351a75e2017-05-30 20:06:08 +05304450void publish_getvar_multislot_vars()
4451{
4452 int i,count;
4453 static bool published = false;
4454 static char slot_count[MAX_RSP_SIZE];
4455 static struct ab_slot_info slot_info[AB_SUPPORTED_SLOTS];
4456 static char active_slot_suffix[MAX_RSP_SIZE];
4457 static char has_slot_pname[NUM_PARTITIONS][MAX_GET_VAR_NAME_SIZE];
4458 static char has_slot_reply[NUM_PARTITIONS][MAX_RSP_SIZE];
4459 const char *tmp;
4460 char tmpbuff[MAX_GET_VAR_NAME_SIZE];
Mayank Grover2bd84bb2017-06-20 11:06:07 +05304461 signed active_slt;
Mayank Grover351a75e2017-05-30 20:06:08 +05304462
4463 if (!published)
4464 {
4465 /* Update slot meta info */
4466 count = partition_fill_partition_meta(has_slot_pname, has_slot_reply,
4467 partition_get_partition_count());
4468 for(i=0; i<count; i++)
4469 {
4470 memset(tmpbuff, 0, MAX_GET_VAR_NAME_SIZE);
Mayank Grover2bd84bb2017-06-20 11:06:07 +05304471 snprintf(tmpbuff, MAX_GET_VAR_NAME_SIZE,"has-slot:%s",
4472 has_slot_pname[i]);
4473 strlcpy(has_slot_pname[i], tmpbuff, MAX_GET_VAR_NAME_SIZE);
Mayank Grover351a75e2017-05-30 20:06:08 +05304474 fastboot_publish(has_slot_pname[i], has_slot_reply[i]);
4475 }
4476
4477 for (i=0; i<AB_SUPPORTED_SLOTS; i++)
4478 {
4479 tmp = SUFFIX_SLOT(i);
Mayank Grover7b8a8f82017-10-27 13:07:59 +05304480 tmp++; // to remove "_" from slot_suffix.
Mayank Grover2bd84bb2017-06-20 11:06:07 +05304481 snprintf(slot_info[i].slot_is_unbootable, sizeof(slot_info[i].slot_is_unbootable),
4482 "slot-unbootable:%s", tmp);
4483 snprintf(slot_info[i].slot_is_active, sizeof(slot_info[i].slot_is_active),
4484 "slot-active:%s", tmp);
4485 snprintf(slot_info[i].slot_is_succesful, sizeof(slot_info[i].slot_is_succesful),
4486 "slot-success:%s", tmp);
4487 snprintf(slot_info[i].slot_retry_count, sizeof(slot_info[i].slot_retry_count),
4488 "slot-retry-count:%s", tmp);
Mayank Grover351a75e2017-05-30 20:06:08 +05304489 fastboot_publish(slot_info[i].slot_is_unbootable,
4490 slot_info[i].slot_is_unbootable_rsp);
4491 fastboot_publish(slot_info[i].slot_is_active,
4492 slot_info[i].slot_is_active_rsp);
4493 fastboot_publish(slot_info[i].slot_is_succesful,
4494 slot_info[i].slot_is_succesful_rsp);
4495 fastboot_publish(slot_info[i].slot_retry_count,
4496 slot_info[i].slot_retry_count_rsp);
4497 }
4498 fastboot_publish("current-slot", active_slot_suffix);
4499 snprintf(slot_count, sizeof(slot_count),"%d", AB_SUPPORTED_SLOTS);
4500 fastboot_publish("slot-count", slot_count);
4501 published = true;
4502 }
4503
Mayank Grover2bd84bb2017-06-20 11:06:07 +05304504 active_slt = partition_find_active_slot();
4505 if (active_slt != INVALID)
Mayank Grover7b8a8f82017-10-27 13:07:59 +05304506 {
4507 tmp = SUFFIX_SLOT(active_slt);
4508 tmp++; // to remove "_" from slot_suffix.
4509 snprintf(active_slot_suffix, sizeof(active_slot_suffix), "%s", tmp);
4510 }
Mayank Grover2bd84bb2017-06-20 11:06:07 +05304511 else
4512 strlcpy(active_slot_suffix, "INVALID", sizeof(active_slot_suffix));
4513
Mayank Grover351a75e2017-05-30 20:06:08 +05304514 /* Update partition meta information */
4515 partition_fill_slot_meta(slot_info);
4516 return;
4517}
4518
lijuang4ece1e72015-08-14 21:02:36 +08004519void get_product_name(unsigned char *buf)
4520{
4521 snprintf((char*)buf, MAX_RSP_SIZE, "%s", TARGET(BOARD));
4522 return;
4523}
4524
Mayank Grover7b8a8f82017-10-27 13:07:59 +05304525#if PRODUCT_IOT
4526void get_bootloader_version_iot(unsigned char *buf)
4527{
4528 if (buf != NULL)
4529 {
4530 strlcpy(buf, TARGET(BOARD), MAX_VERSION_LEN);
4531 strlcat(buf, "-", MAX_VERSION_LEN);
4532 strlcat(buf, PRODUCT_IOT_VERSION, MAX_VERSION_LEN);
4533 }
4534 return;
4535}
4536#endif
4537
lijuang4ece1e72015-08-14 21:02:36 +08004538void get_bootloader_version(unsigned char *buf)
4539{
4540 snprintf((char*)buf, MAX_RSP_SIZE, "%s", device.bootloader_version);
4541 return;
4542}
4543
4544void get_baseband_version(unsigned char *buf)
4545{
4546 snprintf((char*)buf, MAX_RSP_SIZE, "%s", device.radio_version);
4547 return;
4548}
4549
Monika Singh32a09f72018-03-14 13:08:29 +05304550bool is_device_locked_critical()
4551{
4552 return device.is_unlock_critical ? false:true;
4553}
4554
lijuang4ece1e72015-08-14 21:02:36 +08004555bool is_device_locked()
4556{
4557 return device.is_unlocked ? false:true;
4558}
4559
Monika Singh32a09f72018-03-14 13:08:29 +05304560bool is_verity_enforcing()
4561{
4562 return device.verity_mode ? true:false;
4563}
4564
Amol Jadi5edf3552013-07-23 14:15:34 -07004565/* register commands and variables for fastboot */
4566void aboot_fastboot_register_commands(void)
4567{
Sridhar Parasurame94e8152014-10-24 14:06:03 -07004568 int i;
lijuangf16461c2015-08-03 17:09:34 +08004569 char hw_platform_buf[MAX_RSP_SIZE];
Amol Jadi5edf3552013-07-23 14:15:34 -07004570
Sridhar Parasurame94e8152014-10-24 14:06:03 -07004571 struct fastboot_cmd_desc cmd_list[] = {
lijuang511a2b52015-08-14 20:50:51 +08004572 /* By default the enabled list is empty. */
4573 {"", NULL},
4574 /* move commands enclosed within the below ifndef to here
4575 * if they need to be enabled in user build.
4576 */
Sridhar Parasurame94e8152014-10-24 14:06:03 -07004577#ifndef DISABLE_FASTBOOT_CMDS
lijuang511a2b52015-08-14 20:50:51 +08004578 /* Register the following commands only for non-user builds */
4579 {"flash:", cmd_flash},
4580 {"erase:", cmd_erase},
4581 {"boot", cmd_boot},
4582 {"continue", cmd_continue},
4583 {"reboot", cmd_reboot},
4584 {"reboot-bootloader", cmd_reboot_bootloader},
4585 {"oem unlock", cmd_oem_unlock},
4586 {"oem unlock-go", cmd_oem_unlock_go},
4587 {"oem lock", cmd_oem_lock},
4588 {"flashing unlock", cmd_oem_unlock},
4589 {"flashing lock", cmd_oem_lock},
4590 {"flashing lock_critical", cmd_flashing_lock_critical},
4591 {"flashing unlock_critical", cmd_flashing_unlock_critical},
4592 {"flashing get_unlock_ability", cmd_flashing_get_unlock_ability},
4593 {"oem device-info", cmd_oem_devinfo},
4594 {"preflash", cmd_preflash},
4595 {"oem enable-charger-screen", cmd_oem_enable_charger_screen},
4596 {"oem disable-charger-screen", cmd_oem_disable_charger_screen},
lijuanga25d8bb2015-09-16 13:11:52 +08004597 {"oem off-mode-charge", cmd_oem_off_mode_charger},
lijuang511a2b52015-08-14 20:50:51 +08004598 {"oem select-display-panel", cmd_oem_select_display_panel},
Mayank Grover6ccc1c92017-07-04 17:36:46 +05304599 {"set_active",cmd_set_active},
Channagoud Kadabi736c4962015-08-21 11:56:52 -07004600#if UNITTEST_FW_SUPPORT
Channagoud Kadabid5ddf482015-09-28 10:31:35 -07004601 {"oem run-tests", cmd_oem_runtests},
Channagoud Kadabi736c4962015-08-21 11:56:52 -07004602#endif
Sridhar Parasurame94e8152014-10-24 14:06:03 -07004603#endif
lijuang511a2b52015-08-14 20:50:51 +08004604 };
Sridhar Parasurame94e8152014-10-24 14:06:03 -07004605
4606 int fastboot_cmds_count = sizeof(cmd_list)/sizeof(cmd_list[0]);
4607 for (i = 1; i < fastboot_cmds_count; i++)
4608 fastboot_register(cmd_list[i].name,cmd_list[i].cb);
4609
Amol Jadi5edf3552013-07-23 14:15:34 -07004610 /* publish variables and their values */
4611 fastboot_publish("product", TARGET(BOARD));
4612 fastboot_publish("kernel", "lk");
4613 fastboot_publish("serialno", sn_buf);
4614
4615 /*
4616 * partition info is supported only for emmc partitions
4617 * Calling this for NAND prints some error messages which
4618 * is harmless but misleading. Avoid calling this for NAND
4619 * devices.
4620 */
4621 if (target_is_emmc_boot())
Mayank Grover7b8a8f82017-10-27 13:07:59 +05304622 publish_getvar_partition_info(part_info, partition_get_partition_count());
Amol Jadi5edf3552013-07-23 14:15:34 -07004623
Mayank Grover351a75e2017-05-30 20:06:08 +05304624 if (partition_multislot_is_supported())
4625 publish_getvar_multislot_vars();
4626
Amol Jadi5edf3552013-07-23 14:15:34 -07004627 /* Max download size supported */
Ameya Thakur11cf1a62013-08-05 12:44:48 -07004628 snprintf(max_download_size, MAX_RSP_SIZE, "\t0x%x",
4629 target_get_max_flash_size());
Amol Jadi5edf3552013-07-23 14:15:34 -07004630 fastboot_publish("max-download-size", (const char *) max_download_size);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07004631 /* Is the charger screen check enabled */
4632 snprintf(charger_screen_enabled, MAX_RSP_SIZE, "%d",
4633 device.charger_screen_enabled);
4634 fastboot_publish("charger-screen-enabled",
4635 (const char *) charger_screen_enabled);
lijuanga25d8bb2015-09-16 13:11:52 +08004636 fastboot_publish("off-mode-charge", (const char *) charger_screen_enabled);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05304637 snprintf(panel_display_mode, MAX_RSP_SIZE, "%s",
4638 device.display_panel);
4639 fastboot_publish("display-panel",
4640 (const char *) panel_display_mode);
Mayank Grover7b8a8f82017-10-27 13:07:59 +05304641#if PRODUCT_IOT
4642 get_bootloader_version_iot(&bootloader_version_string);
4643 fastboot_publish("version-bootloader", (const char *) bootloader_version_string);
4644
4645 /* Version baseband is n/a for apq iot devices */
4646 fastboot_publish("version-baseband", "N/A");
4647
4648 /* IOT targets support only mmc target */
4649 snprintf(block_size_string, MAX_RSP_SIZE, "0x%x", mmc_get_device_blocksize());
4650 fastboot_publish("erase-block-size", (const char *) block_size_string);
4651 fastboot_publish("logical-block-size", (const char *) block_size_string);
4652#else
Channagoud Kadabiaafbbfc2015-03-04 17:52:56 -08004653 fastboot_publish("version-bootloader", (const char *) device.bootloader_version);
4654 fastboot_publish("version-baseband", (const char *) device.radio_version);
Mayank Grover7b8a8f82017-10-27 13:07:59 +05304655#endif
lijuangf16461c2015-08-03 17:09:34 +08004656 fastboot_publish("secure", is_secure_boot_enable()? "yes":"no");
Mayank Grover7b8a8f82017-10-27 13:07:59 +05304657 fastboot_publish("unlocked", device.is_unlocked ? "yes":"no");
lijuangf16461c2015-08-03 17:09:34 +08004658 smem_get_hw_platform_name((unsigned char *) hw_platform_buf, sizeof(hw_platform_buf));
4659 snprintf(get_variant, MAX_RSP_SIZE, "%s %s", hw_platform_buf,
4660 target_is_emmc_boot()? "eMMC":"UFS");
4661 fastboot_publish("variant", (const char *) get_variant);
lijuang65c5a822015-08-29 16:35:36 +08004662#if CHECK_BAT_VOLTAGE
lijuang102dfa92015-10-09 18:31:03 +08004663 update_battery_status();
lijuang65c5a822015-08-29 16:35:36 +08004664 fastboot_publish("battery-voltage", (const char *) battery_voltage);
lijuang102dfa92015-10-09 18:31:03 +08004665 fastboot_publish("battery-soc-ok", (const char *) battery_soc_ok);
lijuang65c5a822015-08-29 16:35:36 +08004666#endif
Amol Jadi5edf3552013-07-23 14:15:34 -07004667}
4668
Brian Swetland9c4c0752009-01-25 16:23:50 -08004669void aboot_init(const struct app_descriptor *app)
4670{
Shashank Mittal4f99a882010-02-01 13:58:50 -08004671 unsigned reboot_mode = 0;
Mayank Grover351a75e2017-05-30 20:06:08 +05304672 int boot_err_type = 0;
4673 int boot_slot = INVALID;
Chandan Uddarajubedca152010-06-02 23:05:15 -07004674
lijuang39831732016-01-08 17:49:02 +08004675 /* Initialise wdog to catch early lk crashes */
4676#if WDOG_SUPPORT
4677 msm_wdog_init();
4678#endif
4679
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07004680 /* Setup page size information for nv storage */
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004681 if (target_is_emmc_boot())
4682 {
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07004683 page_size = mmc_page_size();
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004684 page_mask = page_size - 1;
Gaurav Nebhwani4d2389c2016-03-17 21:10:05 +05304685 mmc_blocksize = mmc_get_device_blocksize();
4686 mmc_blocksize_mask = mmc_blocksize - 1;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004687 }
4688 else
4689 {
4690 page_size = flash_page_size();
4691 page_mask = page_size - 1;
4692 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07004693 ASSERT((MEMBASE + MEMSIZE) > MEMBASE);
4694
Ameya Thakur11cf1a62013-08-05 12:44:48 -07004695 read_device_info(&device);
vijay kumarc65876c2015-04-24 13:29:16 +05304696 read_allow_oem_unlock(&device);
Shashank Mittal162244e2011-08-08 19:01:25 -07004697
Mayank Grover351a75e2017-05-30 20:06:08 +05304698 /* Detect multi-slot support */
4699 if (partition_multislot_is_supported())
4700 {
4701 boot_slot = partition_find_active_slot();
4702 if (boot_slot == INVALID)
4703 {
4704 boot_into_fastboot = true;
4705 dprintf(INFO, "Active Slot: (INVALID)\n");
4706 }
4707 else
4708 {
4709 /* Setting the state of system to boot active slot */
4710 partition_mark_active_slot(boot_slot);
4711 dprintf(INFO, "Active Slot: (%s)\n", SUFFIX_SLOT(boot_slot));
4712 }
4713 }
4714
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08004715 /* Display splash screen if enabled */
4716#if DISPLAY_SPLASH_SCREEN
lijuang99c02d82015-02-13 19:04:34 +08004717#if NO_ALARM_DISPLAY
4718 if (!check_alarm_boot()) {
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08004719#endif
lijuang99c02d82015-02-13 19:04:34 +08004720 dprintf(SPEW, "Display Init: Start\n");
Ajay Singh Parmara7865a82015-04-16 21:27:52 -07004721#if DISPLAY_HDMI_PRIMARY
4722 if (!strlen(device.display_panel))
4723 strlcpy(device.display_panel, DISPLAY_PANEL_HDMI,
4724 sizeof(device.display_panel));
4725#endif
Channagoud Kadabi90869ce2015-04-27 11:15:14 -07004726#if ENABLE_WBC
Channagoud Kadabid801ac72015-08-26 11:21:51 -07004727 /* Wait if the display shutdown is in progress */
4728 while(pm_app_display_shutdown_in_prgs());
Channagoud Kadabi90869ce2015-04-27 11:15:14 -07004729 if (!pm_appsbl_display_init_done())
4730 target_display_init(device.display_panel);
4731 else
4732 display_image_on_screen();
4733#else
lijuang99c02d82015-02-13 19:04:34 +08004734 target_display_init(device.display_panel);
Channagoud Kadabi90869ce2015-04-27 11:15:14 -07004735#endif
lijuang99c02d82015-02-13 19:04:34 +08004736 dprintf(SPEW, "Display Init: Done\n");
4737#if NO_ALARM_DISPLAY
4738 }
4739#endif
4740#endif
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08004741
Greg Griscod6250552011-06-29 14:40:23 -07004742 target_serialno((unsigned char *) sn_buf);
Ajay Dudanib06c05f2011-05-12 14:46:10 -07004743 dprintf(SPEW,"serial number: %s\n",sn_buf);
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -08004744
Dhaval Patel223ec952013-07-18 14:49:44 -07004745 memset(display_panel_buf, '\0', MAX_PANEL_BUF_SIZE);
4746
Matthew Qindefd5562014-07-11 18:02:40 +08004747 /*
4748 * Check power off reason if user force reset,
4749 * if yes phone will do normal boot.
4750 */
4751 if (is_user_force_reset())
4752 goto normal_boot;
4753
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07004754 /* Check if we should do something other than booting up */
Ameya Thakur0b9c2442013-05-10 13:22:56 -07004755 if (keys_get_state(KEY_VOLUMEUP) && keys_get_state(KEY_VOLUMEDOWN))
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07004756 {
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03004757 dprintf(ALWAYS,"dload mode key sequence detected\n");
lijuang395b5e62015-11-19 17:39:44 +08004758 reboot_device(EMERGENCY_DLOAD);
4759 dprintf(CRITICAL,"Failed to reboot into dload mode\n");
4760
Ameya Thakur0b9c2442013-05-10 13:22:56 -07004761 boot_into_fastboot = true;
4762 }
4763 if (!boot_into_fastboot)
4764 {
4765 if (keys_get_state(KEY_HOME) || keys_get_state(KEY_VOLUMEUP))
4766 boot_into_recovery = 1;
4767 if (!boot_into_recovery &&
4768 (keys_get_state(KEY_BACK) || keys_get_state(KEY_VOLUMEDOWN)))
Pavel Nedev5d91d412013-04-29 11:34:24 +03004769 boot_into_fastboot = true;
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07004770 }
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004771 #if NO_KEYPAD_DRIVER
Kinson Chik0b1c8162011-08-31 16:31:57 -07004772 if (fastboot_trigger())
Pavel Nedev5d91d412013-04-29 11:34:24 +03004773 boot_into_fastboot = true;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004774 #endif
Chandan Uddarajubedca152010-06-02 23:05:15 -07004775
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07004776#if USE_PON_REBOOT_REG
4777 reboot_mode = check_hard_reboot_mode();
4778#else
Ajay Dudani77421292010-10-27 19:34:06 -07004779 reboot_mode = check_reboot_mode();
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07004780#endif
4781 if (reboot_mode == RECOVERY_MODE)
4782 {
Ajay Dudani77421292010-10-27 19:34:06 -07004783 boot_into_recovery = 1;
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07004784 }
4785 else if(reboot_mode == FASTBOOT_MODE)
4786 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03004787 boot_into_fastboot = true;
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07004788 }
4789 else if(reboot_mode == ALARM_BOOT)
4790 {
Matthew Qind886f3c2014-01-17 16:52:01 +08004791 boot_reason_alarm = true;
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07004792 }
Parth Dixit207573a2015-10-27 17:26:21 +05304793#if VERIFIED_BOOT
Mayank Grover912eaa62017-10-26 12:08:53 +05304794 else if (VB_M <= target_get_vb_version())
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07004795 {
Mayank Grover889be1b2017-09-12 20:12:23 +05304796 if (reboot_mode == DM_VERITY_ENFORCING)
4797 {
4798 device.verity_mode = 1;
4799 write_device_info(&device);
4800 }
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07004801#if ENABLE_VB_ATTEST
Mayank Grover889be1b2017-09-12 20:12:23 +05304802 else if (reboot_mode == DM_VERITY_EIO)
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07004803#else
Mayank Grover889be1b2017-09-12 20:12:23 +05304804 else if (reboot_mode == DM_VERITY_LOGGING)
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07004805#endif
Mayank Grover889be1b2017-09-12 20:12:23 +05304806 {
4807 device.verity_mode = 0;
4808 write_device_info(&device);
4809 }
4810 else if (reboot_mode == DM_VERITY_KEYSCLEAR)
4811 {
4812 if(send_delete_keys_to_tz())
4813 ASSERT(0);
4814 }
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07004815 }
Parth Dixitddbc7352015-10-18 03:13:31 +05304816#endif
Ajay Dudani77421292010-10-27 19:34:06 -07004817
Matthew Qindefd5562014-07-11 18:02:40 +08004818normal_boot:
Pavel Nedev5d91d412013-04-29 11:34:24 +03004819 if (!boot_into_fastboot)
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004820 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03004821 if (target_is_emmc_boot())
Shashank Mittala0032282011-08-26 14:50:11 -07004822 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03004823 if(emmc_recovery_init())
4824 dprintf(ALWAYS,"error in emmc_recovery_init\n");
4825 if(target_use_signed_kernel())
Shashank Mittala0032282011-08-26 14:50:11 -07004826 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03004827 if((device.is_unlocked) || (device.is_tampered))
4828 {
4829 #ifdef TZ_TAMPER_FUSE
4830 set_tamper_fuse_cmd();
4831 #endif
4832 #if USE_PCOM_SECBOOT
4833 set_tamper_flag(device.is_tampered);
4834 #endif
4835 }
Shashank Mittala0032282011-08-26 14:50:11 -07004836 }
Amit Blay6281ebc2015-01-11 14:44:08 +02004837
Mayank Grover351a75e2017-05-30 20:06:08 +05304838retry_boot:
4839 /* Trying to boot active partition */
4840 if (partition_multislot_is_supported())
4841 {
4842 boot_slot = partition_find_boot_slot();
4843 partition_mark_active_slot(boot_slot);
4844 if (boot_slot == INVALID)
4845 goto fastboot;
4846 }
4847
4848 boot_err_type = boot_linux_from_mmc();
4849 switch (boot_err_type)
4850 {
4851 case ERR_INVALID_PAGE_SIZE:
4852 case ERR_DT_PARSE:
4853 case ERR_ABOOT_ADDR_OVERLAP:
Mayank Grover10cabbe2017-10-30 19:11:05 +05304854 case ERR_INVALID_BOOT_MAGIC:
Mayank Grover351a75e2017-05-30 20:06:08 +05304855 if(partition_multislot_is_supported())
Mayank Grover10cabbe2017-10-30 19:11:05 +05304856 {
4857 /*
4858 * Deactivate current slot, as it failed to
4859 * boot, and retry next slot.
4860 */
4861 partition_deactivate_slot(boot_slot);
Mayank Grover351a75e2017-05-30 20:06:08 +05304862 goto retry_boot;
Mayank Grover10cabbe2017-10-30 19:11:05 +05304863 }
Mayank Grover351a75e2017-05-30 20:06:08 +05304864 else
4865 break;
Mayank Grover351a75e2017-05-30 20:06:08 +05304866 default:
4867 break;
4868 /* going to fastboot menu */
4869 }
Shashank Mittala0032282011-08-26 14:50:11 -07004870 }
Pavel Nedev5d91d412013-04-29 11:34:24 +03004871 else
4872 {
4873 recovery_init();
4874 #if USE_PCOM_SECBOOT
4875 if((device.is_unlocked) || (device.is_tampered))
4876 set_tamper_flag(device.is_tampered);
4877 #endif
4878 boot_linux_from_flash();
4879 }
4880 dprintf(CRITICAL, "ERROR: Could not do normal boot. Reverting "
4881 "to fastboot mode.\n");
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004882 }
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07004883
Mayank Grover351a75e2017-05-30 20:06:08 +05304884fastboot:
Amol Jadi5edf3552013-07-23 14:15:34 -07004885 /* We are here means regular boot did not happen. Start fastboot. */
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07004886
Amol Jadi5edf3552013-07-23 14:15:34 -07004887 /* register aboot specific fastboot commands */
4888 aboot_fastboot_register_commands();
Amol Jadi57abe4c2011-05-24 15:47:27 -07004889
Amol Jadi5edf3552013-07-23 14:15:34 -07004890 /* dump partition table for debug info */
Kinson Chikf1a43512011-07-14 11:28:39 -07004891 partition_dump();
Amol Jadi5edf3552013-07-23 14:15:34 -07004892
4893 /* initialize and start fastboot */
4894 fastboot_init(target_get_scratch_address(), target_get_max_flash_size());
lijuang4ece1e72015-08-14 21:02:36 +08004895#if FBCON_DISPLAY_MSG
lijuang9a7d3b92015-11-30 14:41:24 +08004896 display_fastboot_menu();
lijuang4ece1e72015-08-14 21:02:36 +08004897#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08004898}
4899
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07004900uint32_t get_page_size()
4901{
4902 return page_size;
4903}
4904
Amir Samuelov57a6fa22013-06-05 16:36:43 +03004905/*
4906 * Calculated and save hash (SHA256) for non-signed boot image.
4907 *
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07004908 * @param image_addr - Boot image address
4909 * @param image_size - Size of the boot image
Amir Samuelov57a6fa22013-06-05 16:36:43 +03004910 *
4911 * @return int - 0 on success, negative value on failure.
4912 */
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07004913static int aboot_save_boot_hash_mmc(uint32_t image_addr, uint32_t image_size)
Amir Samuelov57a6fa22013-06-05 16:36:43 +03004914{
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07004915 unsigned int digest[8];
4916#if IMAGE_VERIF_ALGO_SHA1
4917 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA1;
4918#else
4919 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA256;
4920#endif
Amir Samuelov57a6fa22013-06-05 16:36:43 +03004921
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07004922 target_crypto_init_params();
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -08004923 hash_find((unsigned char *) image_addr, image_size, (unsigned char *)&digest, auth_algo);
Amir Samuelov57a6fa22013-06-05 16:36:43 +03004924
4925 save_kernel_hash_cmd(digest);
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07004926 dprintf(INFO, "aboot_save_boot_hash_mmc: imagesize_actual size %d bytes.\n", (int) image_size);
Amir Samuelov57a6fa22013-06-05 16:36:43 +03004927
4928 return 0;
4929}
4930
Mayank Grover351a75e2017-05-30 20:06:08 +05304931
Brian Swetland9c4c0752009-01-25 16:23:50 -08004932APP_START(aboot)
4933 .init = aboot_init,
4934APP_END