blob: 6987a56d4688a8c969f0b24f48a20d4b491027c1 [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
139
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;
vijay kumar8f53e362015-11-24 13:38:11 +05301816 unsigned int dtb_size = 0;
1817 unsigned char *best_match_dt_addr = NULL;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001818#endif
1819
David Ng183a7422009-12-07 14:55:21 -08001820 if (target_is_emmc_boot()) {
1821 hdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
1822 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1823 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
1824 return -1;
1825 }
1826 goto continue_boot;
1827 }
1828
Dima Zavin214cc642009-01-26 11:16:21 -08001829 ptable = flash_get_ptable();
1830 if (ptable == NULL) {
1831 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1832 return -1;
1833 }
1834
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001835 if(!boot_into_recovery)
1836 {
1837 ptn = ptable_find(ptable, "boot");
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001838
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001839 if (ptn == NULL) {
1840 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1841 return -1;
1842 }
1843 }
1844 else
1845 {
1846 ptn = ptable_find(ptable, "recovery");
1847 if (ptn == NULL) {
1848 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
1849 return -1;
1850 }
Dima Zavin214cc642009-01-26 11:16:21 -08001851 }
1852
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05301853 /* Read boot.img header from flash */
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001854 if (flash_read(ptn, offset, buf, page_size)) {
Dima Zavin214cc642009-01-26 11:16:21 -08001855 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
1856 return -1;
1857 }
Dima Zavin214cc642009-01-26 11:16:21 -08001858
1859 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001860 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Dima Zavin214cc642009-01-26 11:16:21 -08001861 return -1;
1862 }
1863
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001864 if (hdr->page_size != page_size) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001865 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 -08001866 return -1;
1867 }
1868
Kishor PK9e91b592017-05-24 12:14:55 +05301869 image_addr = (unsigned char *)target_get_scratch_address();
1870 memcpy(image_addr, (void *)buf, page_size);
vijay kumar287bb542015-09-29 13:01:52 +05301871
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001872 /*
1873 * Update the kernel/ramdisk/tags address if the boot image header
1874 * has default values, these default values come from mkbootimg when
1875 * the boot image is flashed using fastboot flash:raw
1876 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001877 update_ker_tags_rdisk_addr(hdr, false);
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001878
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001879 /* Get virtual addresses since the hdr saves physical addresses. */
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001880 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
1881 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
1882 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
1883
1884 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1885 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Mayank Grover032736f2017-07-14 20:34:51 +05301886 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001887
Kishor PK9e91b592017-05-24 12:14:55 +05301888 /* ensure commandline is terminated */
1889 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
1890
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001891 /* Check if the addresses in the header are valid. */
1892 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
Mayank Grovere559cec2017-10-17 15:12:03 +05301893 check_ddr_addr_range_bound(hdr->kernel_addr, kernel_actual) ||
1894 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual) ||
1895 check_ddr_addr_range_bound(hdr->ramdisk_addr, ramdisk_actual))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001896 {
Mayank Grovere559cec2017-10-17 15:12:03 +05301897 dprintf(CRITICAL, "kernel/ramdisk addresses are not valid.\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001898 return -1;
1899 }
1900
1901#ifndef DEVICE_TREE
Kishor PKd8ddcad2017-07-27 13:53:57 +05301902 if (UINT_MAX < ((uint64_t)kernel_actual + (uint64_t)ramdisk_actual+ (uint64_t)second_actual + page_size)) {
Mayank Grover032736f2017-07-14 20:34:51 +05301903 dprintf(CRITICAL, "Integer overflow detected in bootimage header fields\n");
1904 return -1;
1905 }
Kishor PKd8ddcad2017-07-27 13:53:57 +05301906 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + second_actual);
Mayank Grover032736f2017-07-14 20:34:51 +05301907
Mayank Grovere559cec2017-10-17 15:12:03 +05301908 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE) ||
1909 check_ddr_addr_range_bound(hdr->tags_addr, MAX_TAGS_SIZE))
Vijay Kumar Pendotid3ed20e2016-09-20 00:34:46 +05301910 {
Mayank Grovere559cec2017-10-17 15:12:03 +05301911 dprintf(CRITICAL, "Tags addresses are not valid.\n");
Vijay Kumar Pendotid3ed20e2016-09-20 00:34:46 +05301912 return -1;
1913 }
1914#else
1915
1916#ifndef OSVERSION_IN_BOOTIMAGE
1917 dt_size = hdr->dt_size;
1918#endif
Mayank Grover032736f2017-07-14 20:34:51 +05301919 dt_actual = ROUND_TO_PAGE(dt_size, page_mask);
Kishor PKd8ddcad2017-07-27 13:53:57 +05301920 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 +05301921 dprintf(CRITICAL, "Integer overflow detected in bootimage header fields\n");
1922 return -1;
1923 }
1924
Kishor PKd8ddcad2017-07-27 13:53:57 +05301925 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + second_actual + dt_actual);
Mayank Grover032736f2017-07-14 20:34:51 +05301926
Mayank Grovere559cec2017-10-17 15:12:03 +05301927 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_size) ||
1928 check_ddr_addr_range_bound(hdr->tags_addr, dt_size))
Mayank Grover032736f2017-07-14 20:34:51 +05301929 {
Mayank Grovere559cec2017-10-17 15:12:03 +05301930 dprintf(CRITICAL, "Device tree addresses are not valid.\n");
Mayank Grover032736f2017-07-14 20:34:51 +05301931 return -1;
1932 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001933#endif
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001934
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05301935 /* Read full boot.img from flash */
1936 dprintf(INFO, "Loading (%s) image (%d): start\n",
1937 (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual);
1938 bs_set_timestamp(BS_KERNEL_LOAD_START);
1939
1940 if (UINT_MAX - page_size < imagesize_actual)
1941 {
1942 dprintf(CRITICAL,"Integer overflow detected in bootimage header fields %u %s\n", __LINE__,__func__);
1943 return -1;
1944 }
1945
1946 /*Check the availability of RAM before reading boot image + max signature length from flash*/
1947 if (target_get_max_flash_size() < (imagesize_actual + page_size))
1948 {
1949 dprintf(CRITICAL, "bootimage size is greater than DDR can hold\n");
1950 return -1;
1951 }
1952
1953 offset = page_size;
1954 /* Read image without signature and header */
1955 if (flash_read(ptn, offset, (void *)(image_addr + offset), imagesize_actual - page_size))
1956 {
1957 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
1958 return -1;
1959 }
1960
1961 dprintf(INFO, "Loading (%s) image (%d): done\n",
1962 (!boot_into_recovery ? "boot" : "recovery"), imagesize_actual);
1963 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
1964
Shashank Mittalcd98d472011-08-02 14:29:24 -07001965 /* Authenticate Kernel */
Deepa Dinamani23b60d42013-06-24 18:10:52 -07001966 if(target_use_signed_kernel() && (!device.is_unlocked))
Shashank Mittalcd98d472011-08-02 14:29:24 -07001967 {
Shashank Mittalcd98d472011-08-02 14:29:24 -07001968 offset = imagesize_actual;
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05301969
Shashank Mittalcd98d472011-08-02 14:29:24 -07001970 /* Read signature */
1971 if (flash_read(ptn, offset, (void *)(image_addr + offset), page_size))
1972 {
1973 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001974 return -1;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001975 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07001976
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301977 verify_signed_bootimg((uint32_t)image_addr, imagesize_actual);
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05301978 }
1979 offset = page_size;
1980 if(hdr->second_size != 0) {
1981 if (UINT_MAX - offset < second_actual)
1982 {
1983 dprintf(CRITICAL, "ERROR: Integer overflow in boot image header %s\t%d\n",__func__,__LINE__);
1984 return -1;
vijay kumar8f53e362015-11-24 13:38:11 +05301985 }
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05301986 offset += second_actual;
1987 /* Second image loading not implemented. */
1988 ASSERT(0);
1989 }
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05301990 /* Move kernel and ramdisk to correct address */
1991 memmove((void*) hdr->kernel_addr, (char*) (image_addr + page_size), hdr->kernel_size);
1992 memmove((void*) hdr->ramdisk_addr, (char*) (image_addr + page_size + kernel_actual), hdr->ramdisk_size);
1993
1994#if DEVICE_TREE
1995 if(dt_size != 0) {
1996
1997 dt_table_offset = ((uint32_t)image_addr + page_size + kernel_actual + ramdisk_actual + second_actual);
1998
1999 table = (struct dt_table*) dt_table_offset;
2000
2001 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
2002 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
2003 return -1;
2004 }
2005
2006 /* Its Error if, dt_hdr_size (table->num_entries * dt_entry size + Dev_Tree Header)
2007 goes beyound hdr->dt_size*/
2008 if (dt_hdr_size > ROUND_TO_PAGE(dt_size,hdr->page_size)) {
2009 dprintf(CRITICAL, "ERROR: Invalid Device Tree size \n");
2010 return -1;
2011 }
2012
2013 /* Find index of device tree within device tree table */
2014 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
2015 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
2016 return -1;
2017 }
2018
2019 /* Validate and Read device device tree in the "tags_add */
2020 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size) ||
2021 check_ddr_addr_range_bound(hdr->tags_addr, dt_entry.size))
2022 {
2023 dprintf(CRITICAL, "Device tree addresses are not valid.\n");
2024 return -1;
2025 }
2026
2027 if(dt_entry.offset > (UINT_MAX - dt_entry.size)) {
2028 dprintf(CRITICAL, "ERROR: Device tree contents are Invalid\n");
2029 return -1;
2030 }
2031
2032 /* Ensure we are not overshooting dt_size with the dt_entry selected */
2033 if ((dt_entry.offset + dt_entry.size) > dt_size) {
2034 dprintf(CRITICAL, "ERROR: Device tree contents are Invalid\n");
2035 return -1;
2036 }
2037
2038 best_match_dt_addr = (unsigned char *)table + dt_entry.offset;
2039 dtb_size = dt_entry.size;
2040 memmove((void *)hdr->tags_addr, (char *)best_match_dt_addr, dtb_size);
2041 }
2042#endif
2043 if(target_use_signed_kernel() && (!device.is_unlocked))
2044 {
Shashank Mittal162244e2011-08-08 19:01:25 -07002045 /* Make sure everything from scratch address is read before next step!*/
Shashank Mittala0032282011-08-26 14:50:11 -07002046 if(device.is_tampered)
Shashank Mittal162244e2011-08-08 19:01:25 -07002047 {
2048 write_device_info_flash(&device);
2049 }
Channagoud Kadabi5c86fe32012-02-16 10:58:48 +05302050#if USE_PCOM_SECBOOT
2051 set_tamper_flag(device.is_tampered);
2052#endif
Shashank Mittalcd98d472011-08-02 14:29:24 -07002053 }
David Ng183a7422009-12-07 14:55:21 -08002054continue_boot:
Dima Zavin214cc642009-01-26 11:16:21 -08002055
Dima Zavin214cc642009-01-26 11:16:21 -08002056 /* TODO: create/pass atags to kernel */
2057
Ajay Dudanie28a6072011-07-01 13:59:46 -07002058 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07002059 (const char *)hdr->cmdline, board_machtype(),
Dima Zavin214cc642009-01-26 11:16:21 -08002060 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
2061
2062 return 0;
2063}
Brian Swetland9c4c0752009-01-25 16:23:50 -08002064
Shashank Mittal162244e2011-08-08 19:01:25 -07002065void write_device_info_mmc(device_info *dev)
2066{
Shashank Mittal162244e2011-08-08 19:01:25 -07002067 unsigned long long ptn = 0;
2068 unsigned long long size;
2069 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002070 uint8_t lun = 0;
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002071 uint32_t ret = 0;
Mayank Groverddd348d2018-01-23 14:07:09 +05302072 uint32_t device_info_sz = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07002073
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002074 if (devinfo_present)
2075 index = partition_get_index("devinfo");
2076 else
2077 index = partition_get_index("aboot");
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07002078
Shashank Mittal162244e2011-08-08 19:01:25 -07002079 ptn = partition_get_offset(index);
2080 if(ptn == 0)
2081 {
2082 return;
2083 }
2084
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002085 lun = partition_get_lun(index);
2086 mmc_set_lun(lun);
2087
Shashank Mittal162244e2011-08-08 19:01:25 -07002088 size = partition_get_size(index);
2089
Mayank Groverddd348d2018-01-23 14:07:09 +05302090 device_info_sz = ROUND_TO_PAGE(sizeof(struct device_info),
2091 mmc_blocksize_mask);
2092 if (device_info_sz == UINT_MAX)
2093 {
2094 dprintf(CRITICAL, "ERROR: Incorrect blocksize of card\n");
2095 return;
2096 }
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07002097
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002098 if (devinfo_present)
Mayank Groverddd348d2018-01-23 14:07:09 +05302099 ret = mmc_write(ptn, device_info_sz, (void *)info_buf);
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002100 else
Mayank Groverddd348d2018-01-23 14:07:09 +05302101 ret = mmc_write((ptn + size - device_info_sz), device_info_sz, (void *)info_buf);
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002102 if (ret)
Shashank Mittal162244e2011-08-08 19:01:25 -07002103 {
2104 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
Sridhar Parasurame1a97922015-07-27 15:31:22 -07002105 ASSERT(0);
Shashank Mittal162244e2011-08-08 19:01:25 -07002106 }
2107}
2108
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002109void read_device_info_mmc(struct device_info *info)
Shashank Mittal162244e2011-08-08 19:01:25 -07002110{
Shashank Mittal162244e2011-08-08 19:01:25 -07002111 unsigned long long ptn = 0;
2112 unsigned long long size;
2113 int index = INVALID_PTN;
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002114 uint32_t ret = 0;
Mayank Groverddd348d2018-01-23 14:07:09 +05302115 uint32_t device_info_sz = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07002116
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002117 if ((index = partition_get_index("devinfo")) < 0)
2118 {
2119 devinfo_present = false;
2120 index = partition_get_index("aboot");
2121 }
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07002122
Shashank Mittal162244e2011-08-08 19:01:25 -07002123 ptn = partition_get_offset(index);
2124 if(ptn == 0)
2125 {
2126 return;
2127 }
2128
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07002129 mmc_set_lun(partition_get_lun(index));
2130
Shashank Mittal162244e2011-08-08 19:01:25 -07002131 size = partition_get_size(index);
2132
Mayank Groverddd348d2018-01-23 14:07:09 +05302133 device_info_sz = ROUND_TO_PAGE(sizeof(struct device_info),
2134 mmc_blocksize_mask);
2135 if (device_info_sz == UINT_MAX)
2136 {
2137 dprintf(CRITICAL, "ERROR: Incorrect blocksize of card\n");
2138 return;
2139 }
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07002140
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002141 if (devinfo_present)
Mayank Groverddd348d2018-01-23 14:07:09 +05302142 ret = mmc_read(ptn, (void *)info_buf, device_info_sz);
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002143 else
Mayank Groverddd348d2018-01-23 14:07:09 +05302144 ret = mmc_read((ptn + size - device_info_sz), (void *)info_buf, device_info_sz);
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002145 if (ret)
Shashank Mittal162244e2011-08-08 19:01:25 -07002146 {
2147 dprintf(CRITICAL, "ERROR: Cannot read device info\n");
Sridhar Parasurame1a97922015-07-27 15:31:22 -07002148 ASSERT(0);
Shashank Mittal162244e2011-08-08 19:01:25 -07002149 }
Shashank Mittal162244e2011-08-08 19:01:25 -07002150}
2151
2152void write_device_info_flash(device_info *dev)
2153{
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002154 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
Shashank Mittal162244e2011-08-08 19:01:25 -07002155 struct ptentry *ptn;
2156 struct ptable *ptable;
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002157 if(info == NULL)
2158 {
2159 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
2160 ASSERT(0);
2161 }
2162 info_buf = info;
Shashank Mittal162244e2011-08-08 19:01:25 -07002163 ptable = flash_get_ptable();
2164 if (ptable == NULL)
2165 {
2166 dprintf(CRITICAL, "ERROR: Partition table not found\n");
2167 return;
2168 }
2169
2170 ptn = ptable_find(ptable, "devinfo");
2171 if (ptn == NULL)
2172 {
Smita Ghosh670c8b82015-05-07 09:30:03 -07002173 dprintf(CRITICAL, "ERROR: No devinfo partition found\n");
Shashank Mittal162244e2011-08-08 19:01:25 -07002174 return;
2175 }
2176
Mayank Groverdedbc892017-10-24 13:41:34 +05302177 memset(info, 0, BOOT_IMG_MAX_PAGE_SIZE);
Shashank Mittal162244e2011-08-08 19:01:25 -07002178 memcpy(info, dev, sizeof(device_info));
2179
2180 if (flash_write(ptn, 0, (void *)info_buf, page_size))
2181 {
2182 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
2183 return;
2184 }
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002185 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07002186}
2187
vijay kumarc65876c2015-04-24 13:29:16 +05302188static int read_allow_oem_unlock(device_info *dev)
2189{
vijay kumarc65876c2015-04-24 13:29:16 +05302190 unsigned offset;
2191 int index;
2192 unsigned long long ptn;
2193 unsigned long long ptn_size;
2194 unsigned blocksize = mmc_get_device_blocksize();
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07002195 STACKBUF_DMA_ALIGN(buf, blocksize);
vijay kumarc65876c2015-04-24 13:29:16 +05302196
vijay kumarca2e6812015-07-08 20:28:25 +05302197 index = partition_get_index(frp_ptns[0]);
vijay kumarc65876c2015-04-24 13:29:16 +05302198 if (index == INVALID_PTN)
2199 {
vijay kumarca2e6812015-07-08 20:28:25 +05302200 index = partition_get_index(frp_ptns[1]);
2201 if (index == INVALID_PTN)
2202 {
2203 dprintf(CRITICAL, "Neither '%s' nor '%s' partition found\n", frp_ptns[0],frp_ptns[1]);
2204 return -1;
2205 }
vijay kumarc65876c2015-04-24 13:29:16 +05302206 }
2207
2208 ptn = partition_get_offset(index);
2209 ptn_size = partition_get_size(index);
2210 offset = ptn_size - blocksize;
2211
Mayank Grover48860402016-11-29 12:34:53 +05302212 /* Set Lun for partition */
2213 mmc_set_lun(partition_get_lun(index));
2214
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07002215 if (mmc_read(ptn + offset, (void *)buf, blocksize))
vijay kumarc65876c2015-04-24 13:29:16 +05302216 {
2217 dprintf(CRITICAL, "Reading MMC failed\n");
2218 return -1;
2219 }
2220
2221 /*is_allow_unlock is a bool value stored at the LSB of last byte*/
2222 is_allow_unlock = buf[blocksize-1] & 0x01;
2223 return 0;
2224}
2225
2226static int write_allow_oem_unlock(bool allow_unlock)
2227{
vijay kumarc65876c2015-04-24 13:29:16 +05302228 unsigned offset;
vijay kumarc65876c2015-04-24 13:29:16 +05302229 int index;
2230 unsigned long long ptn;
2231 unsigned long long ptn_size;
2232 unsigned blocksize = mmc_get_device_blocksize();
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07002233 STACKBUF_DMA_ALIGN(buf, blocksize);
vijay kumarc65876c2015-04-24 13:29:16 +05302234
vijay kumarca2e6812015-07-08 20:28:25 +05302235 index = partition_get_index(frp_ptns[0]);
vijay kumarc65876c2015-04-24 13:29:16 +05302236 if (index == INVALID_PTN)
2237 {
vijay kumarca2e6812015-07-08 20:28:25 +05302238 index = partition_get_index(frp_ptns[1]);
2239 if (index == INVALID_PTN)
2240 {
2241 dprintf(CRITICAL, "Neither '%s' nor '%s' partition found\n", frp_ptns[0],frp_ptns[1]);
2242 return -1;
2243 }
vijay kumarc65876c2015-04-24 13:29:16 +05302244 }
2245
2246 ptn = partition_get_offset(index);
2247 ptn_size = partition_get_size(index);
2248 offset = ptn_size - blocksize;
Mayank Grover48860402016-11-29 12:34:53 +05302249 mmc_set_lun(partition_get_lun(index));
vijay kumarc65876c2015-04-24 13:29:16 +05302250
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07002251 if (mmc_read(ptn + offset, (void *)buf, blocksize))
vijay kumarc65876c2015-04-24 13:29:16 +05302252 {
2253 dprintf(CRITICAL, "Reading MMC failed\n");
2254 return -1;
2255 }
2256
2257 /*is_allow_unlock is a bool value stored at the LSB of last byte*/
2258 buf[blocksize-1] = allow_unlock;
2259 if (mmc_write(ptn + offset, blocksize, buf))
2260 {
2261 dprintf(CRITICAL, "Writing MMC failed\n");
2262 return -1;
2263 }
2264
2265 return 0;
2266}
2267
Shashank Mittal162244e2011-08-08 19:01:25 -07002268void read_device_info_flash(device_info *dev)
2269{
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002270 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
Shashank Mittal162244e2011-08-08 19:01:25 -07002271 struct ptentry *ptn;
2272 struct ptable *ptable;
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002273 if(info == NULL)
2274 {
2275 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
2276 ASSERT(0);
2277 }
2278 info_buf = info;
Shashank Mittal162244e2011-08-08 19:01:25 -07002279 ptable = flash_get_ptable();
2280 if (ptable == NULL)
2281 {
2282 dprintf(CRITICAL, "ERROR: Partition table not found\n");
2283 return;
2284 }
2285
2286 ptn = ptable_find(ptable, "devinfo");
2287 if (ptn == NULL)
2288 {
Smita Ghosh670c8b82015-05-07 09:30:03 -07002289 dprintf(CRITICAL, "ERROR: No devinfo partition found\n");
Shashank Mittal162244e2011-08-08 19:01:25 -07002290 return;
2291 }
2292
2293 if (flash_read(ptn, 0, (void *)info_buf, page_size))
2294 {
2295 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
2296 return;
2297 }
2298
2299 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
2300 {
Shashank Mittal162244e2011-08-08 19:01:25 -07002301 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
2302 info->is_unlocked = 0;
Shashank Mittala0032282011-08-26 14:50:11 -07002303 info->is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07002304 write_device_info_flash(info);
2305 }
2306 memcpy(dev, info, sizeof(device_info));
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002307 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07002308}
2309
2310void write_device_info(device_info *dev)
2311{
2312 if(target_is_emmc_boot())
2313 {
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002314 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
2315 if(info == NULL)
2316 {
2317 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
2318 ASSERT(0);
2319 }
2320 info_buf = info;
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002321 memcpy(info, dev, sizeof(struct device_info));
2322
2323#if USE_RPMB_FOR_DEVINFO
Mayank Grover912eaa62017-10-26 12:08:53 +05302324 if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05302325 is_secure_boot_enable()) {
2326 if((write_device_info_rpmb((void*) info, PAGE_SIZE)) < 0)
2327 ASSERT(0);
Sridhar Parasurame1a97922015-07-27 15:31:22 -07002328 }
Channagoud Kadabic80cb492015-04-28 16:08:28 -07002329 else
2330 write_device_info_mmc(info);
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002331#else
2332 write_device_info_mmc(info);
2333#endif
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002334 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07002335 }
2336 else
2337 {
2338 write_device_info_flash(dev);
2339 }
2340}
2341
Monika Singh94316462018-03-15 18:39:01 +05302342int read_rollback_index(uint32_t loc, uint64_t *roll_back_index)
2343{
2344 if (!devinfo_present) {
2345 dprintf(CRITICAL, "DeviceInfo not initalized \n");
2346 return -EINVAL;
2347 }
2348 if (loc >= ARRAY_SIZE(device.rollback_index)) {
2349 dprintf(CRITICAL, "%s() Loc out of range index: %d, array len: %d\n",
2350 __func__, loc, ARRAY_SIZE(device.rollback_index));
2351 ASSERT(0);
2352 }
2353
2354 *roll_back_index = device.rollback_index[loc];
2355 return 0;
2356}
2357
2358int write_rollback_index(uint32_t loc, uint64_t roll_back_index)
2359{
2360 if (!devinfo_present) {
2361 dprintf(CRITICAL, "DeviceInfo not initalized \n");
2362 return -EINVAL;
2363 }
2364 if (loc >= ARRAY_SIZE(device.rollback_index)) {
2365 dprintf(CRITICAL, "%s() Loc out of range index: %d, array len: %d\n",
2366 __func__, loc, ARRAY_SIZE(device.rollback_index));
2367 ASSERT(0);
2368 }
2369
2370 device.rollback_index[loc] = roll_back_index;
2371 write_device_info(&device);
2372 return 0;
2373}
2374
Monika Singhb0fad822018-03-15 18:50:55 +05302375int store_userkey(uint8_t *user_key, uint32_t user_key_size)
2376{
2377 if (!devinfo_present) {
2378 dprintf(CRITICAL, "DeviceInfo not initalized \n");
2379 return -EINVAL;
2380 }
2381
2382 if (user_key_size > ARRAY_SIZE(device.user_public_key)) {
2383 dprintf(CRITICAL, "StoreUserKey, UserKeySize too large!\n");
2384 return -ENODEV;
2385 }
2386
2387 memcpy(device.user_public_key, user_key, user_key_size);
2388 device.user_public_key_length = user_key_size;
2389 write_device_info(&device);
2390 return 0;
2391}
2392
2393int erase_userkey()
2394{
2395 if (!devinfo_present) {
2396 dprintf(CRITICAL, "DeviceInfo not initalized \n");
2397 return -EINVAL;
2398 }
2399 memset(device.user_public_key, 0, ARRAY_SIZE(device.user_public_key));
2400 device.user_public_key_length = 0;
2401 write_device_info(&device);
2402 return 0;
2403}
2404
2405int get_userkey(uint8_t **user_key, uint32_t *user_key_size)
2406{
2407 if (!devinfo_present) {
2408 dprintf(CRITICAL, "DeviceInfo not initalized \n");
2409 return -EINVAL;
2410 }
2411 *user_key = device.user_public_key;
2412 *user_key_size = device.user_public_key_length;
2413 return 0;
2414}
2415
Shashank Mittal162244e2011-08-08 19:01:25 -07002416void read_device_info(device_info *dev)
2417{
2418 if(target_is_emmc_boot())
2419 {
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002420 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
2421 if(info == NULL)
2422 {
2423 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
2424 ASSERT(0);
2425 }
2426 info_buf = info;
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002427
2428#if USE_RPMB_FOR_DEVINFO
Mayank Grover912eaa62017-10-26 12:08:53 +05302429 if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05302430 is_secure_boot_enable()) {
2431 if((read_device_info_rpmb((void*) info, PAGE_SIZE)) < 0)
2432 ASSERT(0);
Sridhar Parasurame1a97922015-07-27 15:31:22 -07002433 }
Channagoud Kadabic80cb492015-04-28 16:08:28 -07002434 else
2435 read_device_info_mmc(info);
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002436#else
2437 read_device_info_mmc(info);
2438#endif
2439
2440 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
2441 {
2442 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
lijuang511a2b52015-08-14 20:50:51 +08002443 if (is_secure_boot_enable()) {
Channagoud Kadabi05f78ba2015-07-06 11:58:14 -07002444 info->is_unlocked = 0;
Monika Singh292b3e92018-03-17 22:40:23 +05302445#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover912eaa62017-10-26 12:08:53 +05302446 if (VB_M <= target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +05302447 info->is_unlock_critical = 0;
Parth Dixitddbc7352015-10-18 03:13:31 +05302448#endif
lijuang511a2b52015-08-14 20:50:51 +08002449 } else {
Channagoud Kadabi2fda4092015-07-07 13:34:11 -07002450 info->is_unlocked = 1;
Monika Singh292b3e92018-03-17 22:40:23 +05302451#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover912eaa62017-10-26 12:08:53 +05302452 if (VB_M <= target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +05302453 info->is_unlock_critical = 1;
Parth Dixitddbc7352015-10-18 03:13:31 +05302454#endif
lijuang511a2b52015-08-14 20:50:51 +08002455 }
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002456 info->is_tampered = 0;
2457 info->charger_screen_enabled = 0;
Monika Singh292b3e92018-03-17 22:40:23 +05302458#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover912eaa62017-10-26 12:08:53 +05302459 if (VB_M <= target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +05302460 info->verity_mode = 1; //enforcing by default
Parth Dixitddbc7352015-10-18 03:13:31 +05302461#endif
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002462 write_device_info(info);
2463 }
2464 memcpy(dev, info, sizeof(device_info));
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002465 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07002466 }
2467 else
2468 {
2469 read_device_info_flash(dev);
2470 }
2471}
2472
2473void reset_device_info()
2474{
2475 dprintf(ALWAYS, "reset_device_info called.");
Shashank Mittala0032282011-08-26 14:50:11 -07002476 device.is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07002477 write_device_info(&device);
2478}
2479
2480void set_device_root()
2481{
2482 dprintf(ALWAYS, "set_device_root called.");
Shashank Mittala0032282011-08-26 14:50:11 -07002483 device.is_tampered = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -07002484 write_device_info(&device);
2485}
2486
lijuang4ece1e72015-08-14 21:02:36 +08002487/* set device unlock value
2488 * Must check FRP before call this function
2489 * Need to wipe data when unlock status changed
2490 * type 0: oem unlock
2491 * type 1: unlock critical
2492 * status 0: unlock as false
2493 * status 1: lock as true
2494 */
2495void set_device_unlock_value(int type, bool status)
lijuang21f12f52015-08-22 16:22:19 +08002496{
lijuang4ece1e72015-08-14 21:02:36 +08002497 if (type == UNLOCK)
2498 device.is_unlocked = status;
Monika Singh292b3e92018-03-17 22:40:23 +05302499#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover912eaa62017-10-26 12:08:53 +05302500 else if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05302501 type == UNLOCK_CRITICAL)
2502 device.is_unlock_critical = status;
Parth Dixitddbc7352015-10-18 03:13:31 +05302503#endif
lijuang4ece1e72015-08-14 21:02:36 +08002504 write_device_info(&device);
2505}
2506
2507static void set_device_unlock(int type, bool status)
2508{
2509 int is_unlocked = -1;
2510 char response[MAX_RSP_SIZE];
2511
2512 /* check device unlock status if it is as expected */
2513 if (type == UNLOCK)
2514 is_unlocked = device.is_unlocked;
Monika Singh292b3e92018-03-17 22:40:23 +05302515#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover912eaa62017-10-26 12:08:53 +05302516 if(VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05302517 type == UNLOCK_CRITICAL)
2518 {
2519 is_unlocked = device.is_unlock_critical;
2520 }
Parth Dixitddbc7352015-10-18 03:13:31 +05302521#endif
lijuang4ece1e72015-08-14 21:02:36 +08002522 if (is_unlocked == status) {
2523 snprintf(response, sizeof(response), "\tDevice already : %s", (status ? "unlocked!" : "locked!"));
2524 fastboot_info(response);
2525 fastboot_okay("");
2526 return;
2527 }
2528
2529 /* status is true, it means to unlock device */
2530 if (status) {
lijuang21f12f52015-08-22 16:22:19 +08002531 if(!is_allow_unlock) {
2532 fastboot_fail("oem unlock is not allowed");
2533 return;
2534 }
2535
lijuang4ece1e72015-08-14 21:02:36 +08002536#if FBCON_DISPLAY_MSG
lijuang9a7d3b92015-11-30 14:41:24 +08002537 display_unlock_menu(type);
lijuang4ece1e72015-08-14 21:02:36 +08002538 fastboot_okay("");
2539 return;
2540#else
2541 if (type == UNLOCK) {
2542 fastboot_fail("Need wipe userdata. Do 'fastboot oem unlock-go'");
2543 return;
2544 }
2545#endif
lijuang21f12f52015-08-22 16:22:19 +08002546 }
lijuang4ece1e72015-08-14 21:02:36 +08002547
2548 set_device_unlock_value(type, status);
2549
2550 /* wipe data */
2551 struct recovery_message msg;
Kishor PK60a68212017-05-08 16:55:57 +05302552 memset(&msg, 0, sizeof(msg));
lijuang4ece1e72015-08-14 21:02:36 +08002553 snprintf(msg.recovery, sizeof(msg.recovery), "recovery\n--wipe_data");
2554 write_misc(0, &msg, sizeof(msg));
2555
2556 fastboot_okay("");
2557 reboot_device(RECOVERY_MODE);
lijuang21f12f52015-08-22 16:22:19 +08002558}
2559
lijuang511a2b52015-08-14 20:50:51 +08002560static bool critical_flash_allowed(const char * entry)
2561{
2562 uint32_t i = 0;
2563 if (entry == NULL)
2564 return false;
2565
2566 for (i = 0; i < ARRAY_SIZE(critical_flash_allowed_ptn); i++) {
2567 if(!strcmp(entry, critical_flash_allowed_ptn[i]))
2568 return true;
2569 }
2570 return false;
Matthew Qin271927e2015-03-31 22:07:07 -04002571}
2572
2573#if DEVICE_TREE
Amol Jadicb524072012-08-09 16:40:18 -07002574int copy_dtb(uint8_t *boot_image_start, unsigned int scratch_offset)
2575{
2576 uint32 dt_image_offset = 0;
Amol Jadicb524072012-08-09 16:40:18 -07002577 uint32_t n;
Monika Singh292b3e92018-03-17 22:40:23 +05302578 struct dt_table *table = NULL;
Amol Jadicb524072012-08-09 16:40:18 -07002579 struct dt_entry dt_entry;
Monika Singh292b3e92018-03-17 22:40:23 +05302580 uint32_t dt_hdr_size = 0;
Amol Jadicb524072012-08-09 16:40:18 -07002581 unsigned int compressed_size = 0;
2582 unsigned int dtb_size = 0;
2583 unsigned int out_avai_len = 0;
2584 unsigned char *out_addr = NULL;
2585 unsigned char *best_match_dt_addr = NULL;
2586 int rc;
2587
2588 struct boot_img_hdr *hdr = (struct boot_img_hdr *) (boot_image_start);
2589
Parth Dixit4097b622016-03-15 14:42:27 +05302590#ifndef OSVERSION_IN_BOOTIMAGE
2591 dt_size = hdr->dt_size;
2592#endif
2593
2594 if(dt_size != 0) {
Amol Jadicb524072012-08-09 16:40:18 -07002595 /* add kernel offset */
2596 dt_image_offset += page_size;
2597 n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
2598 dt_image_offset += n;
2599
2600 /* add ramdisk offset */
2601 n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
2602 dt_image_offset += n;
2603
2604 /* add second offset */
2605 if(hdr->second_size != 0) {
2606 n = ROUND_TO_PAGE(hdr->second_size, page_mask);
2607 dt_image_offset += n;
2608 }
2609
2610 /* offset now point to start of dt.img */
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07002611 table = (struct dt_table*)(boot_image_start + dt_image_offset);
Amol Jadicb524072012-08-09 16:40:18 -07002612
Deepa Dinamani19648b42013-09-05 17:05:55 -07002613 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Amol Jadicb524072012-08-09 16:40:18 -07002614 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
2615 return -1;
2616 }
P.V. Phani Kumar82916762016-03-09 09:20:19 +05302617
2618 /* Its Error if, dt_hdr_size (table->num_entries * dt_entry size + Dev_Tree Header)
2619 goes beyound hdr->dt_size*/
Parth Dixit4097b622016-03-15 14:42:27 +05302620 if (dt_hdr_size > ROUND_TO_PAGE(dt_size,hdr->page_size)) {
P.V. Phani Kumar82916762016-03-09 09:20:19 +05302621 dprintf(CRITICAL, "ERROR: Invalid Device Tree size \n");
2622 return -1;
2623 }
2624
Joel Kingaa335dc2013-06-03 16:11:08 -07002625 /* Find index of device tree within device tree table */
2626 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Amol Jadicb524072012-08-09 16:40:18 -07002627 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
2628 return -1;
2629 }
2630
Matthew Qin271927e2015-03-31 22:07:07 -04002631 best_match_dt_addr = (unsigned char *)boot_image_start + dt_image_offset + dt_entry.offset;
2632 if (is_gzip_package(best_match_dt_addr, dt_entry.size))
2633 {
2634 out_addr = (unsigned char *)target_get_scratch_address() + scratch_offset;
2635 out_avai_len = target_get_max_flash_size() - scratch_offset;
Matthew Qin0b15b322015-05-19 05:20:54 -04002636 dprintf(INFO, "decompressing dtb: start\n");
Matthew Qin271927e2015-03-31 22:07:07 -04002637 rc = decompress(best_match_dt_addr,
2638 dt_entry.size, out_addr, out_avai_len,
2639 &compressed_size, &dtb_size);
2640 if (rc)
2641 {
Matthew Qin0b15b322015-05-19 05:20:54 -04002642 dprintf(CRITICAL, "decompressing dtb failed!!!\n");
Matthew Qin271927e2015-03-31 22:07:07 -04002643 ASSERT(0);
2644 }
2645
Matthew Qin0b15b322015-05-19 05:20:54 -04002646 dprintf(INFO, "decompressing dtb: done\n");
Matthew Qin271927e2015-03-31 22:07:07 -04002647 best_match_dt_addr = out_addr;
2648 } else {
2649 dtb_size = dt_entry.size;
2650 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002651 /* Validate and Read device device tree in the "tags_add */
Mayank Grovere559cec2017-10-17 15:12:03 +05302652 if (check_aboot_addr_range_overlap(hdr->tags_addr, dtb_size) ||
2653 check_ddr_addr_range_bound(hdr->tags_addr, dtb_size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002654 {
Mayank Grovere559cec2017-10-17 15:12:03 +05302655 dprintf(CRITICAL, "Device tree addresses are not valid.\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002656 return -1;
2657 }
2658
Amol Jadicb524072012-08-09 16:40:18 -07002659 /* Read device device tree in the "tags_add */
Matthew Qin271927e2015-03-31 22:07:07 -04002660 memmove((void*) hdr->tags_addr, (void *)best_match_dt_addr, dtb_size);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002661 } else
2662 return -1;
Amol Jadicb524072012-08-09 16:40:18 -07002663
2664 /* Everything looks fine. Return success. */
2665 return 0;
2666}
2667#endif
2668
Brian Swetland9c4c0752009-01-25 16:23:50 -08002669void cmd_boot(const char *arg, void *data, unsigned sz)
2670{
2671 unsigned kernel_actual;
2672 unsigned ramdisk_actual;
Kishor PKd8ddcad2017-07-27 13:53:57 +05302673 unsigned second_actual;
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002674 uint32_t image_actual;
2675 uint32_t dt_actual = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08002676 struct boot_img_hdr *hdr = NULL;
2677 struct kernel64_hdr *kptr = NULL;
Brian Swetland9c4c0752009-01-25 16:23:50 -08002678 char *ptr = ((char*) data);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002679 int ret = 0;
2680 uint8_t dtb_copied = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08002681 unsigned int out_len = 0;
2682 unsigned int out_avai_len = 0;
2683 unsigned char *out_addr = NULL;
2684 uint32_t dtb_offset = 0;
2685 unsigned char *kernel_start_addr = NULL;
2686 unsigned int kernel_size = 0;
Matthew Qin271927e2015-03-31 22:07:07 -04002687 unsigned int scratch_offset = 0;
Monika Singh292b3e92018-03-17 22:40:23 +05302688#if !VERIFIED_BOOT_2
2689 uint32_t sig_actual = 0;
2690 uint32_t sig_size = 0;
2691#ifdef MDTP_SUPPORT
2692 static bool is_mdtp_activated = 0;
2693#endif /* MDTP_SUPPORT */
2694#endif
Matthew Qinbb7923d2015-02-09 10:56:09 +08002695
lijuang2008ff22016-03-07 17:56:27 +08002696#if FBCON_DISPLAY_MSG
2697 /* Exit keys' detection thread firstly */
2698 exit_menu_keys_detection();
2699#endif
2700
Monika Singh292b3e92018-03-17 22:40:23 +05302701#if VERIFIED_BOOT || VERIFIED_BOOT_2
Channagoud Kadabi6d5375e2015-06-23 17:15:42 -07002702 if(target_build_variant_user() && !device.is_unlocked)
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002703 {
2704 fastboot_fail("unlock device to use this command");
lijuang2008ff22016-03-07 17:56:27 +08002705 goto boot_failed;
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002706 }
2707#endif
2708
Brian Swetland9c4c0752009-01-25 16:23:50 -08002709 if (sz < sizeof(hdr)) {
2710 fastboot_fail("invalid bootimage header");
lijuang2008ff22016-03-07 17:56:27 +08002711 goto boot_failed;
Brian Swetland9c4c0752009-01-25 16:23:50 -08002712 }
2713
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002714 hdr = (struct boot_img_hdr *)data;
Brian Swetland9c4c0752009-01-25 16:23:50 -08002715
2716 /* ensure commandline is terminated */
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002717 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -08002718
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002719 if(target_is_emmc_boot() && hdr->page_size) {
2720 page_size = hdr->page_size;
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07002721 page_mask = page_size - 1;
2722 }
2723
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002724 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
2725 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Kishor PKd8ddcad2017-07-27 13:53:57 +05302726 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002727#if DEVICE_TREE
Parth Dixit4097b622016-03-15 14:42:27 +05302728#ifndef OSVERSION_IN_BOOTIMAGE
2729 dt_size = hdr->dt_size;
2730#endif
2731 dt_actual = ROUND_TO_PAGE(dt_size, page_mask);
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002732#endif
2733
2734 image_actual = ADD_OF(page_size, kernel_actual);
2735 image_actual = ADD_OF(image_actual, ramdisk_actual);
Kishor PKd8ddcad2017-07-27 13:53:57 +05302736 image_actual = ADD_OF(image_actual, second_actual);
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002737 image_actual = ADD_OF(image_actual, dt_actual);
2738
Kishor PK5134b332017-05-09 17:50:08 +05302739 /* Checking to prevent oob access in read_der_message_length */
2740 if (image_actual > sz) {
2741 fastboot_fail("bootimage header fields are invalid");
2742 goto boot_failed;
2743 }
Monika Singh292b3e92018-03-17 22:40:23 +05302744#if VERIFIED_BOOT_2
2745 memset(&info, 0, sizeof(bootinfo));
2746 info.images[0].image_buffer = data;
2747 info.images[0].imgsize = image_actual;
2748 info.images[0].name = "boot";
2749 info.num_loaded_images = 1;
2750 info.multi_slot_boot = partition_multislot_is_supported();
2751 if (load_image_and_auth(&info))
2752 goto boot_failed;
2753 vbcmdline = info.vbcmdline;
2754#else
Kishor PK5134b332017-05-09 17:50:08 +05302755 sig_size = sz - image_actual;
2756
P.V. Phani Kumareecfd822016-03-09 20:09:03 +05302757 if (target_use_signed_kernel() && (!device.is_unlocked)) {
Monika Singh292b3e92018-03-17 22:40:23 +05302758 unsigned chk;
P.V. Phani Kumareecfd822016-03-09 20:09:03 +05302759 /* Calculate the signature length from boot image */
2760 sig_actual = read_der_message_length(
Kishor PK5134b332017-05-09 17:50:08 +05302761 (unsigned char*)(data + image_actual), sig_size);
Monika Singh292b3e92018-03-17 22:40:23 +05302762 chk = ADD_OF(image_actual, sig_actual);
Channagoud Kadabi3d839012014-06-26 14:26:39 -07002763
Monika Singh292b3e92018-03-17 22:40:23 +05302764 if (chk > sz) {
Kishor PK5134b332017-05-09 17:50:08 +05302765 fastboot_fail("bootimage header fields are invalid");
2766 goto boot_failed;
2767 }
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002768 }
2769
Sridhar Parasuram361e4cd2015-09-24 11:19:19 -07002770 // Initialize boot state before trying to verify boot.img
2771#if VERIFIED_BOOT
Channagoud Kadabi699466e2015-11-03 12:37:42 -08002772 boot_verifier_init();
Mayank Groverb337e932017-01-18 20:00:40 +05302773#endif
Channagoud Kadabibf6ce7d2015-09-17 13:25:35 -07002774 /* Handle overflow if the input image size is greater than
2775 * boot image buffer can hold
2776 */
Monika Singh292b3e92018-03-17 22:40:23 +05302777 if ((target_get_max_flash_size() - page_size) < image_actual)
Channagoud Kadabibf6ce7d2015-09-17 13:25:35 -07002778 {
2779 fastboot_fail("booimage: size is greater than boot image buffer can hold");
lijuang2008ff22016-03-07 17:56:27 +08002780 goto boot_failed;
Channagoud Kadabibf6ce7d2015-09-17 13:25:35 -07002781 }
Sridhar Parasuram361e4cd2015-09-24 11:19:19 -07002782
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002783 /* Verify the boot image
2784 * device & page_size are initialized in aboot_init
2785 */
Sridhar Parasuram7ae9fbc2015-09-17 09:56:30 -07002786 if (target_use_signed_kernel() && (!device.is_unlocked)) {
Channagoud Kadabi3d839012014-06-26 14:26:39 -07002787 /* Pass size excluding signature size, otherwise we would try to
2788 * access signature beyond its length
2789 */
Monika Singh292b3e92018-03-17 22:40:23 +05302790 verify_signed_bootimg((uint32_t)data, image_actual);
Sridhar Parasuram7ae9fbc2015-09-17 09:56:30 -07002791 }
Amit Blay4aa292f2015-04-28 21:55:59 +03002792#ifdef MDTP_SUPPORT
2793 else
2794 {
Shay Nachmani062aa3f2015-05-17 17:28:44 +03002795 /* fastboot boot is not allowed when MDTP is activated */
Amit Blay4aa292f2015-04-28 21:55:59 +03002796 mdtp_ext_partition_verification_t ext_partition;
Amit Blay8a510302015-08-17 09:20:01 +03002797
2798 if (!is_mdtp_activated) {
2799 ext_partition.partition = MDTP_PARTITION_NONE;
2800 mdtp_fwlock_verify_lock(&ext_partition);
2801 }
Amit Blay4aa292f2015-04-28 21:55:59 +03002802 }
Shay Nachmani062aa3f2015-05-17 17:28:44 +03002803
Amir Kotzer7c768c02016-04-13 09:08:05 +03002804 /* If mdtp state cannot be validate, block fastboot boot*/
2805 if(mdtp_activated(&is_mdtp_activated)){
2806 dprintf(CRITICAL, "mdtp_activated cannot validate state.\n");
2807 dprintf(CRITICAL, "Can not proceed with fastboot boot command.\n");
2808 goto boot_failed;
2809 }
Shay Nachmani062aa3f2015-05-17 17:28:44 +03002810 if(is_mdtp_activated){
2811 dprintf(CRITICAL, "fastboot boot command is not available.\n");
lijuang2008ff22016-03-07 17:56:27 +08002812 goto boot_failed;
Shay Nachmani062aa3f2015-05-17 17:28:44 +03002813 }
Amit Blay4aa292f2015-04-28 21:55:59 +03002814#endif /* MDTP_SUPPORT */
Monika Singh292b3e92018-03-17 22:40:23 +05302815#endif /* VERIFIED_BOOT_2 else */
Amit Blay4aa292f2015-04-28 21:55:59 +03002816
Sridhar Parasuram361e4cd2015-09-24 11:19:19 -07002817#if VERIFIED_BOOT
Monika Singh292b3e92018-03-17 22:40:23 +05302818 if (VB_M == target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +05302819 {
2820 /* set boot and system versions. */
2821 set_os_version((unsigned char *)data);
2822 // send root of trust
2823 if(!send_rot_command((uint32_t)device.is_unlocked))
2824 ASSERT(0);
2825 }
Parth Dixitbc9b6492015-10-18 00:41:38 +05302826#endif
Channagoud Kadabia22144f2013-03-20 11:49:01 -07002827 /*
Matthew Qinbb7923d2015-02-09 10:56:09 +08002828 * Check if the kernel image is a gzip package. If yes, need to decompress it.
2829 * If not, continue booting.
2830 */
2831 if (is_gzip_package((unsigned char *)(data + page_size), hdr->kernel_size))
2832 {
2833 out_addr = (unsigned char *)target_get_scratch_address();
2834 out_addr = (unsigned char *)(out_addr + image_actual + page_size);
2835 out_avai_len = target_get_max_flash_size() - image_actual - page_size;
Matthew Qin0b15b322015-05-19 05:20:54 -04002836 dprintf(INFO, "decompressing kernel image: start\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08002837 ret = decompress((unsigned char *)(ptr + page_size),
2838 hdr->kernel_size, out_addr, out_avai_len,
2839 &dtb_offset, &out_len);
2840 if (ret)
2841 {
Matthew Qin0b15b322015-05-19 05:20:54 -04002842 dprintf(CRITICAL, "decompressing image failed!!!\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08002843 ASSERT(0);
2844 }
2845
Matthew Qin0b15b322015-05-19 05:20:54 -04002846 dprintf(INFO, "decompressing kernel image: done\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08002847 kptr = (struct kernel64_hdr *)out_addr;
2848 kernel_start_addr = out_addr;
2849 kernel_size = out_len;
2850 } else {
2851 kptr = (struct kernel64_hdr*)((char *)data + page_size);
2852 kernel_start_addr = (unsigned char *)((char *)data + page_size);
2853 kernel_size = hdr->kernel_size;
2854 }
2855
2856 /*
Channagoud Kadabia22144f2013-03-20 11:49:01 -07002857 * Update the kernel/ramdisk/tags address if the boot image header
2858 * has default values, these default values come from mkbootimg when
2859 * the boot image is flashed using fastboot flash:raw
2860 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08002861 update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr));
Dima Zavin3cadfff2013-03-21 14:30:48 -07002862
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002863 /* Get virtual addresses since the hdr saves physical addresses. */
2864 hdr->kernel_addr = VA(hdr->kernel_addr);
2865 hdr->ramdisk_addr = VA(hdr->ramdisk_addr);
2866 hdr->tags_addr = VA(hdr->tags_addr);
Brian Swetland9c4c0752009-01-25 16:23:50 -08002867
Matthew Qinbb7923d2015-02-09 10:56:09 +08002868 kernel_size = ROUND_TO_PAGE(kernel_size, page_mask);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002869 /* Check if the addresses in the header are valid. */
Matthew Qinbb7923d2015-02-09 10:56:09 +08002870 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_size) ||
Mayank Grovere559cec2017-10-17 15:12:03 +05302871 check_ddr_addr_range_bound(hdr->kernel_addr, kernel_size) ||
2872 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual) ||
2873 check_ddr_addr_range_bound(hdr->ramdisk_addr, ramdisk_actual))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002874 {
Mayank Grovere559cec2017-10-17 15:12:03 +05302875 dprintf(CRITICAL, "kernel/ramdisk addresses are not valid.\n");
lijuang2008ff22016-03-07 17:56:27 +08002876 goto boot_failed;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002877 }
2878
Amol Jadicb524072012-08-09 16:40:18 -07002879#if DEVICE_TREE
Matthew Qin271927e2015-03-31 22:07:07 -04002880 scratch_offset = image_actual + page_size + out_len;
Amol Jadicb524072012-08-09 16:40:18 -07002881 /* find correct dtb and copy it to right location */
Matthew Qin271927e2015-03-31 22:07:07 -04002882 ret = copy_dtb(data, scratch_offset);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002883
2884 dtb_copied = !ret ? 1 : 0;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002885#else
Mayank Grovere559cec2017-10-17 15:12:03 +05302886 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE) ||
2887 check_ddr_addr_range_bound(hdr->tags_addr, MAX_TAGS_SIZE))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002888 {
Mayank Grovere559cec2017-10-17 15:12:03 +05302889 dprintf(CRITICAL, "Tags addresses are not valid.\n");
lijuang2008ff22016-03-07 17:56:27 +08002890 goto boot_failed;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002891 }
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002892#endif
2893
2894 /* Load ramdisk & kernel */
2895 memmove((void*) hdr->ramdisk_addr, ptr + page_size + kernel_actual, hdr->ramdisk_size);
Matthew Qinbb7923d2015-02-09 10:56:09 +08002896 memmove((void*) hdr->kernel_addr, (char*) (kernel_start_addr), kernel_size);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002897
2898#if DEVICE_TREE
Mayank Grovere559cec2017-10-17 15:12:03 +05302899 if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual) ||
2900 check_ddr_addr_range_bound(hdr->tags_addr, kernel_actual))
Matthew Qinbb7923d2015-02-09 10:56:09 +08002901 {
Mayank Grovere559cec2017-10-17 15:12:03 +05302902 dprintf(CRITICAL, "Tags addresses are not valid.\n");
lijuang2008ff22016-03-07 17:56:27 +08002903 goto boot_failed;
Matthew Qinbb7923d2015-02-09 10:56:09 +08002904 }
2905
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002906 /*
2907 * If dtb is not found look for appended DTB in the kernel.
2908 * If appended dev tree is found, update the atags with
2909 * memory address to the DTB appended location on RAM.
2910 * Else update with the atags address in the kernel header
2911 */
2912 if (!dtb_copied) {
2913 void *dtb;
Matthew Qinbb7923d2015-02-09 10:56:09 +08002914 dtb = dev_tree_appended((void*)(ptr + page_size),
2915 hdr->kernel_size, dtb_offset,
Dima Zavine63e5572013-05-03 12:23:06 -07002916 (void *)hdr->tags_addr);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002917 if (!dtb) {
2918 fastboot_fail("dtb not found");
lijuang2008ff22016-03-07 17:56:27 +08002919 goto boot_failed;
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002920 }
Amol Jadicb524072012-08-09 16:40:18 -07002921 }
2922#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08002923
2924 fastboot_okay("");
Amol Jadi7c4316c2013-10-07 14:19:26 -07002925 fastboot_stop();
Brian Swetland9c4c0752009-01-25 16:23:50 -08002926
Dima Zavin77e41f32013-03-06 16:10:43 -08002927 boot_linux((void*) hdr->kernel_addr, (void*) hdr->tags_addr,
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002928 (const char*) hdr->cmdline, board_machtype(),
2929 (void*) hdr->ramdisk_addr, hdr->ramdisk_size);
lijuang2008ff22016-03-07 17:56:27 +08002930
2931 /* fastboot already stop, it's no need to show fastboot menu */
2932 return;
2933boot_failed:
2934#if FBCON_DISPLAY_MSG
2935 /* revert to fastboot menu if boot failed */
2936 display_fastboot_menu();
2937#endif
2938 return;
Brian Swetland9c4c0752009-01-25 16:23:50 -08002939}
2940
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002941void cmd_erase_nand(const char *arg, void *data, unsigned sz)
Dima Zavin214cc642009-01-26 11:16:21 -08002942{
2943 struct ptentry *ptn;
2944 struct ptable *ptable;
2945
2946 ptable = flash_get_ptable();
2947 if (ptable == NULL) {
2948 fastboot_fail("partition table doesn't exist");
2949 return;
2950 }
2951
2952 ptn = ptable_find(ptable, arg);
2953 if (ptn == NULL) {
2954 fastboot_fail("unknown partition name");
2955 return;
2956 }
2957
Monika Singh292b3e92018-03-17 22:40:23 +05302958 if (!strncmp(arg, "avb_custom_key", strlen("avb_custom_key"))) {
2959 dprintf(INFO, "erasing avb_custom_key\n");
2960 if (erase_userkey()) {
2961 fastboot_fail("Erasing avb_custom_key failed");
2962 } else {
2963 fastboot_okay("");
2964 }
2965 return;
2966 }
2967
Dima Zavin214cc642009-01-26 11:16:21 -08002968 if (flash_erase(ptn)) {
2969 fastboot_fail("failed to erase partition");
2970 return;
2971 }
2972 fastboot_okay("");
2973}
2974
Bikas Gurungd48bd242010-09-04 19:54:32 -07002975
2976void cmd_erase_mmc(const char *arg, void *data, unsigned sz)
2977{
2978 unsigned long long ptn = 0;
Oliver Wangcee448d2013-10-22 18:40:13 +08002979 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07002980 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002981 uint8_t lun = 0;
Vijay Kumar Pendoti0b21f462016-05-02 17:09:18 +05302982 char *footer = NULL;
Bikas Gurungd48bd242010-09-04 19:54:32 -07002983
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002984#if VERIFIED_BOOT
2985 if(!strcmp(arg, KEYSTORE_PTN_NAME))
2986 {
2987 if(!device.is_unlocked)
2988 {
2989 fastboot_fail("unlock device to erase keystore");
2990 return;
2991 }
2992 }
2993#endif
2994
Kinson Chikf1a43512011-07-14 11:28:39 -07002995 index = partition_get_index(arg);
2996 ptn = partition_get_offset(index);
Oliver Wangcee448d2013-10-22 18:40:13 +08002997 size = partition_get_size(index);
Neeti Desaica8c9602011-10-06 11:40:00 -07002998
Kinson Chikf1a43512011-07-14 11:28:39 -07002999 if(ptn == 0) {
Neeti Desaica8c9602011-10-06 11:40:00 -07003000 fastboot_fail("Partition table doesn't exist\n");
Bikas Gurungd48bd242010-09-04 19:54:32 -07003001 return;
3002 }
Kun Liang2f1601a2013-08-12 16:29:54 +08003003
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003004 lun = partition_get_lun(index);
3005 mmc_set_lun(lun);
3006
Monika Singh292b3e92018-03-17 22:40:23 +05303007 if (!strncmp(arg, "avb_custom_key", strlen("avb_custom_key"))) {
3008 dprintf(INFO, "erasing avb_custom_key\n");
3009 if (erase_userkey()) {
3010 fastboot_fail("Erasing avb_custom_key failed");
3011 } else {
3012 fastboot_okay("");
3013 }
3014 return;
3015 }
Sridhar Parasuramd7957122015-02-27 11:33:40 -08003016 if (platform_boot_dev_isemmc())
3017 {
3018 if (mmc_erase_card(ptn, size)) {
3019 fastboot_fail("failed to erase partition\n");
3020 return;
3021 }
3022 } else {
3023 BUF_DMA_ALIGN(out, DEFAULT_ERASE_SIZE);
3024 size = partition_get_size(index);
3025 if (size > DEFAULT_ERASE_SIZE)
3026 size = DEFAULT_ERASE_SIZE;
Kun Liang2f1601a2013-08-12 16:29:54 +08003027
Sridhar Parasuramd7957122015-02-27 11:33:40 -08003028 /* Simple inefficient version of erase. Just writing
3029 0 in first several blocks */
3030 if (mmc_write(ptn , size, (unsigned int *)out)) {
3031 fastboot_fail("failed to erase partition");
3032 return;
3033 }
Vijay Kumar Pendoti0b21f462016-05-02 17:09:18 +05303034 /*Erase FDE metadata at the userdata footer*/
3035 if(!(strncmp(arg, "userdata", 8)))
3036 {
3037 footer = memalign(CACHE_LINE, FOOTER_SIZE);
3038 memset((void *)footer, 0, FOOTER_SIZE);
3039
3040 size = partition_get_size(index);
3041
3042 if (mmc_write((ptn + size) - FOOTER_SIZE , FOOTER_SIZE, (unsigned int *)footer)) {
3043 fastboot_fail("failed to erase userdata footer");
3044 free(footer);
3045 return;
3046 }
3047 free(footer);
3048 }
Bikas Gurungd48bd242010-09-04 19:54:32 -07003049 }
Monika Singh292b3e92018-03-17 22:40:23 +05303050#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover912eaa62017-10-26 12:08:53 +05303051 if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05303052 !(strncmp(arg, "userdata", 8)) &&
3053 send_delete_keys_to_tz())
Sridhar Parasuram32b30662015-07-10 13:33:22 -07003054 ASSERT(0);
3055#endif
Bikas Gurungd48bd242010-09-04 19:54:32 -07003056 fastboot_okay("");
3057}
3058
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003059void cmd_erase(const char *arg, void *data, unsigned sz)
3060{
Monika Singh292b3e92018-03-17 22:40:23 +05303061#if VERIFIED_BOOT || VERIFIED_BOOT_2
Channagoud Kadabi35297672015-06-13 11:09:49 -07003062 if (target_build_variant_user())
Channagoud Kadabice3cf422015-04-17 16:02:30 -07003063 {
Sridhar Parasuramc32d07d2015-07-12 10:57:48 -07003064 if(!device.is_unlocked)
Channagoud Kadabice3cf422015-04-17 16:02:30 -07003065 {
Channagoud Kadabi35297672015-06-13 11:09:49 -07003066 fastboot_fail("device is locked. Cannot erase");
Channagoud Kadabice3cf422015-04-17 16:02:30 -07003067 return;
3068 }
3069 }
3070#endif
3071
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003072 if(target_is_emmc_boot())
3073 cmd_erase_mmc(arg, data, sz);
3074 else
3075 cmd_erase_nand(arg, data, sz);
3076}
Bikas Gurungd48bd242010-09-04 19:54:32 -07003077
Mayank Grover11ff9692018-01-11 11:54:49 +05303078/* Get the size from partiton name */
3079static void get_partition_size(const char *arg, char *response)
3080{
3081 uint64_t ptn = 0;
3082 uint64_t size;
3083 int index = INVALID_PTN;
3084
3085 index = partition_get_index(arg);
3086
3087 if (index == INVALID_PTN)
3088 {
3089 dprintf(CRITICAL, "Invalid partition index\n");
3090 return;
3091 }
3092
3093 ptn = partition_get_offset(index);
3094
3095 if(!ptn)
3096 {
3097 dprintf(CRITICAL, "Invalid partition name %s\n", arg);
3098 return;
3099 }
3100
3101 size = partition_get_size(index);
3102
3103 snprintf(response, MAX_RSP_SIZE, "\t 0x%llx", size);
3104 return;
3105}
3106
Mayank Grover52cd10a2018-03-15 12:57:54 +05303107/* Function to check partition type of a partition*/
3108static fs_signature_type
3109check_partition_fs_signature(const char *arg)
3110{
3111 fs_signature_type ret = NO_FS;
3112 int index;
3113 unsigned long long ptn;
3114 char *sb_buffer = malloc(mmc_blocksize);
3115 if (!sb_buffer)
3116 {
3117 dprintf(CRITICAL, "ERROR: Failed to allocate buffer for superblock\n");
3118 goto out;
3119 }
3120
3121 /* Read super block */
3122 if ((index = partition_get_index(arg)) < 0)
3123 {
3124 dprintf(CRITICAL, "ERROR: %s() doesn't exsit\n", arg);
3125 goto out;
3126 }
3127 ptn = partition_get_offset(index);
3128 mmc_set_lun(partition_get_lun(index));
3129 if(mmc_read(ptn + FS_SUPERBLOCK_OFFSET,
3130 (void *)sb_buffer, mmc_blocksize))
3131 {
3132 dprintf(CRITICAL, "ERROR: Failed to read Superblock\n");
3133 goto out;
3134 }
3135
3136 if (*((uint16 *)(&sb_buffer[EXT_MAGIC_OFFSET_SB]))
3137 == (uint16)EXT_MAGIC)
3138 {
3139 dprintf(SPEW, "%s() Found EXT FS\n", arg);
3140 ret = EXT_FS_SIGNATURE;
3141 }
3142 else if (*((uint32 *)(&sb_buffer[F2FS_MAGIC_OFFSET_SB]))
3143 == F2FS_MAGIC)
3144 {
3145 dprintf(SPEW, "%s() Found F2FS FS\n", arg);
3146 ret = EXT_F2FS_SIGNATURE;
3147 }
3148 else
3149 {
3150 dprintf(SPEW, "%s() Reverting to default 0x%x\n",
3151 arg, *((uint16 *)(&sb_buffer[EXT_MAGIC_OFFSET_SB])));
3152 ret = NO_FS;
3153 }
3154
3155out:
3156 if(sb_buffer)
3157 free(sb_buffer);
3158 return ret;
3159}
3160
Mayank Groverd38fe012018-03-13 15:33:16 +05303161/* Function to get partition type */
3162static void get_partition_type(const char *arg, char *response)
3163{
3164 uint n = 0;
Mayank Grover52cd10a2018-03-15 12:57:54 +05303165 fs_signature_type fs_signature;
Mayank Groverd38fe012018-03-13 15:33:16 +05303166
3167 if (arg == NULL ||
3168 response == NULL)
3169 {
3170 dprintf(CRITICAL, "Invalid input parameter\n");
3171 return;
3172 }
3173
3174 /* By default copy raw to response */
3175 strncpy(response, RAW_STR, strlen(RAW_STR));
3176
3177 /* Mark partiton type for known paritions only */
3178 for (n=0; n < ARRAY_SIZE(part_type_known); n++)
3179 {
3180 if (!strncmp(part_type_known[n].part_name, arg, strlen(arg)))
3181 {
Mayank Grover52cd10a2018-03-15 12:57:54 +05303182 /* Check partition for FS signature */
3183 fs_signature = check_partition_fs_signature(arg);
3184 switch (fs_signature)
3185 {
3186 case EXT_FS_SIGNATURE:
3187 strncpy(response, EXT_STR, strlen(EXT_STR));
3188 break;
3189 case EXT_F2FS_SIGNATURE:
3190 strncpy(response, F2FS_STR, strlen(F2FS_STR));
3191 break;
3192 case NO_FS:
3193 strncpy(response, part_type_known[n].type_response, MAX_RSP_SIZE);
3194 }
Mayank Groverd38fe012018-03-13 15:33:16 +05303195 }
3196 }
3197 return;
3198}
3199
Mayank Grover11ff9692018-01-11 11:54:49 +05303200/*
3201 * Publish the partition type & size info
3202 * fastboot getvar will publish the required information.
3203 * fastboot getvar partition_size:<partition_name>: partition size in hex
3204 * fastboot getvar partition_type:<partition_name>: partition type (ext/fat)
3205 */
3206static void publish_getvar_partition_info(struct getvar_partition_info *info, uint8_t num_parts)
3207{
Mayank Groverd38fe012018-03-13 15:33:16 +05303208 uint8_t i;
Mayank Grover11ff9692018-01-11 11:54:49 +05303209 static bool published = false;
3210 struct partition_entry *ptn_entry =
3211 partition_get_partition_entries();
3212 memset(info, 0, sizeof(struct getvar_partition_info)* num_parts);
3213
3214 for (i = 0; i < num_parts; i++) {
3215 strlcat(info[i].part_name, (const char *)ptn_entry[i].name, MAX_RSP_SIZE);
3216 strlcat(info[i].getvar_size, "partition-size:", MAX_GET_VAR_NAME_SIZE);
3217 strlcat(info[i].getvar_type, "partition-type:", MAX_GET_VAR_NAME_SIZE);
3218
Mayank Groverd38fe012018-03-13 15:33:16 +05303219 get_partition_type(info[i].part_name, info[i].type_response);
Mayank Grover11ff9692018-01-11 11:54:49 +05303220 get_partition_size(info[i].part_name, info[i].size_response);
Mayank Grover11ff9692018-01-11 11:54:49 +05303221 if (strlcat(info[i].getvar_size, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
3222 {
3223 dprintf(CRITICAL, "partition size name truncated\n");
3224 return;
3225 }
3226 if (strlcat(info[i].getvar_type, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
3227 {
3228 dprintf(CRITICAL, "partition type name truncated\n");
3229 return;
3230 }
3231
3232 if (!published)
3233 {
3234 /* publish partition size & type info */
3235 fastboot_publish((const char *) info[i].getvar_size, (const char *) info[i].size_response);
3236 fastboot_publish((const char *) info[i].getvar_type, (const char *) info[i].type_response);
3237 }
3238 }
3239 if (!published)
3240 published = true;
3241}
3242
3243
Ajay Dudani5c761132011-04-07 20:19:04 -07003244void cmd_flash_mmc_img(const char *arg, void *data, unsigned sz)
Shashank Mittal23b8f422010-04-16 19:27:21 -07003245{
3246 unsigned long long ptn = 0;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07003247 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07003248 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003249 char *token = NULL;
3250 char *pname = NULL;
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07003251 char *sp;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003252 uint8_t lun = 0;
3253 bool lun_set = false;
Mayank Grover351a75e2017-05-30 20:06:08 +05303254 int current_active_slot = INVALID;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07003255
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07003256 token = strtok_r((char *)arg, ":", &sp);
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003257 pname = token;
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07003258 token = strtok_r(NULL, ":", &sp);
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003259 if(token)
3260 {
3261 lun = atoi(token);
3262 mmc_set_lun(lun);
3263 lun_set = true;
3264 }
3265
Mao Jinlong226f33a2014-07-04 17:24:10 +08003266 if (pname)
Greg Grisco6e754772011-06-23 12:19:39 -07003267 {
Channagoud Kadabiad259832015-05-29 11:14:17 -07003268 if (!strncmp(pname, "frp-unlock", strlen("frp-unlock")))
3269 {
3270 if (!aboot_frp_unlock(pname, data, sz))
3271 {
3272 fastboot_info("FRP unlock successful");
3273 fastboot_okay("");
3274 }
3275 else
3276 fastboot_fail("Secret key is invalid, please update the bootloader with secret key");
3277
3278 return;
3279 }
3280
Mao Jinlong226f33a2014-07-04 17:24:10 +08003281 if (!strcmp(pname, "partition"))
3282 {
3283 dprintf(INFO, "Attempt to write partition image.\n");
3284 if (write_partition(sz, (unsigned char *) data)) {
3285 fastboot_fail("failed to write partition");
Greg Grisco6e754772011-06-23 12:19:39 -07003286 return;
3287 }
Mayank Grover11ff9692018-01-11 11:54:49 +05303288 /* Re-publish partition table */
3289 publish_getvar_partition_info(part_info, partition_get_partition_count());
Mayank Grover351a75e2017-05-30 20:06:08 +05303290
3291 /* Rescan partition table to ensure we have multislot support*/
3292 if (partition_scan_for_multislot())
3293 {
3294 current_active_slot = partition_find_active_slot();
3295 dprintf(INFO, "Multislot supported: Slot %s active",
3296 (SUFFIX_SLOT(current_active_slot)));
3297 }
3298 partition_mark_active_slot(current_active_slot);
Greg Grisco6e754772011-06-23 12:19:39 -07003299 }
Mao Jinlong226f33a2014-07-04 17:24:10 +08003300 else
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003301 {
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003302#if VERIFIED_BOOT
3303 if(!strcmp(pname, KEYSTORE_PTN_NAME))
3304 {
3305 if(!device.is_unlocked)
3306 {
3307 fastboot_fail("unlock device to flash keystore");
3308 return;
3309 }
Gaurav Nebhwanic7313cc2015-12-15 22:25:04 +05303310 if(!boot_verify_validate_keystore((unsigned char *)data,sz))
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003311 {
3312 fastboot_fail("image is not a keystore file");
3313 return;
3314 }
3315 }
3316#endif
Mao Jinlong226f33a2014-07-04 17:24:10 +08003317 index = partition_get_index(pname);
3318 ptn = partition_get_offset(index);
3319 if(ptn == 0) {
3320 fastboot_fail("partition table doesn't exist");
3321 return;
3322 }
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003323
Mayank Grover351a75e2017-05-30 20:06:08 +05303324 if (!strncmp(pname, "boot", strlen("boot"))
3325 || !strcmp(pname, "recovery"))
3326 {
Mao Jinlong226f33a2014-07-04 17:24:10 +08003327 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
3328 fastboot_fail("image is not a boot image");
3329 return;
3330 }
Mayank Grover351a75e2017-05-30 20:06:08 +05303331
3332 /* Reset multislot_partition attributes in case of flashing boot */
3333 if (partition_multislot_is_supported())
3334 {
3335 partition_reset_attributes(index);
3336 }
Mao Jinlong226f33a2014-07-04 17:24:10 +08003337 }
3338
3339 if(!lun_set)
3340 {
3341 lun = partition_get_lun(index);
3342 mmc_set_lun(lun);
3343 }
3344
3345 size = partition_get_size(index);
Gaurav Nebhwani4d2389c2016-03-17 21:10:05 +05303346 if (ROUND_TO_PAGE(sz, mmc_blocksize_mask) > size) {
Mao Jinlong226f33a2014-07-04 17:24:10 +08003347 fastboot_fail("size too large");
3348 return;
3349 }
3350 else if (mmc_write(ptn , sz, (unsigned int *)data)) {
3351 fastboot_fail("flash write failure");
3352 return;
3353 }
Greg Grisco6e754772011-06-23 12:19:39 -07003354 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07003355 }
3356 fastboot_okay("");
3357 return;
3358}
3359
Ajay Dudanide984792015-03-02 09:57:41 -08003360void cmd_flash_meta_img(const char *arg, void *data, unsigned sz)
3361{
3362 int i, images;
3363 meta_header_t *meta_header;
3364 img_header_entry_t *img_header_entry;
Parth Dixit3e2d3032016-03-04 17:11:52 +05303365 /*End of the image address*/
3366 uintptr_t data_end;
3367
3368 if( (UINT_MAX - sz) > (uintptr_t)data )
3369 data_end = (uintptr_t)data + sz;
3370 else
3371 {
3372 fastboot_fail("Cannot flash: image header corrupt");
3373 return;
3374 }
3375
3376 if( data_end < ((uintptr_t)data + sizeof(meta_header_t)))
3377 {
3378 fastboot_fail("Cannot flash: image header corrupt");
3379 return;
3380 }
Ajay Dudanide984792015-03-02 09:57:41 -08003381
lijuang8ee21882016-04-19 16:57:11 +08003382 /* If device is locked:
3383 * Forbid to flash image to avoid the device to bypass the image
3384 * which with "any" name other than bootloader. Because it maybe
3385 * a meta package of all partitions.
3386 */
Monika Singh292b3e92018-03-17 22:40:23 +05303387#if VERIFIED_BOOT || VERIFIED_BOOT_2
lijuang8ee21882016-04-19 16:57:11 +08003388 if (target_build_variant_user()) {
3389 if (!device.is_unlocked) {
3390 fastboot_fail("Device is locked, meta image flashing is not allowed");
3391 return;
3392 }
Mayank Grover889be1b2017-09-12 20:12:23 +05303393
Mayank Grover912eaa62017-10-26 12:08:53 +05303394 if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05303395 !device.is_unlock_critical)
3396 {
lijuang8ee21882016-04-19 16:57:11 +08003397 fastboot_fail("Device is critical locked, Meta image flashing is not allowed");
3398 return;
3399 }
lijuang8ee21882016-04-19 16:57:11 +08003400 }
3401#endif
3402
Ajay Dudanide984792015-03-02 09:57:41 -08003403 meta_header = (meta_header_t*) data;
Parth Dixit3e2d3032016-03-04 17:11:52 +05303404 if( data_end < ((uintptr_t)data + meta_header->img_hdr_sz))
3405 {
3406 fastboot_fail("Cannot flash: image header corrupt");
3407 return;
3408 }
Ajay Dudanide984792015-03-02 09:57:41 -08003409 img_header_entry = (img_header_entry_t*) (data+sizeof(meta_header_t));
3410
3411 images = meta_header->img_hdr_sz / sizeof(img_header_entry_t);
3412
3413 for (i=0; i<images; i++) {
3414
3415 if((img_header_entry[i].ptn_name == NULL) ||
3416 (img_header_entry[i].start_offset == 0) ||
3417 (img_header_entry[i].size == 0))
3418 break;
Kishor PK49831502017-04-21 17:55:43 +05303419 if ((UINT_MAX - img_header_entry[i].start_offset) < (uintptr_t)data) {
3420 fastboot_fail("Integer overflow detected in start_offset of img");
3421 break;
3422 }
3423 else if ((UINT_MAX - (img_header_entry[i].start_offset + (uintptr_t)data)) < img_header_entry[i].size) {
3424 fastboot_fail("Integer overflow detected in size of img");
3425 break;
3426 }
Parth Dixit3e2d3032016-03-04 17:11:52 +05303427 if( data_end < ((uintptr_t)data + img_header_entry[i].start_offset
3428 + img_header_entry[i].size) )
3429 {
3430 fastboot_fail("Cannot flash: image size mismatch");
3431 break;
3432 }
3433
Ajay Dudanide984792015-03-02 09:57:41 -08003434 cmd_flash_mmc_img(img_header_entry[i].ptn_name,
3435 (void *) data + img_header_entry[i].start_offset,
3436 img_header_entry[i].size);
3437 }
3438
Channagoud Kadabiaafbbfc2015-03-04 17:52:56 -08003439 if (!strncmp(arg, "bootloader", strlen("bootloader")))
3440 {
3441 strlcpy(device.bootloader_version, TARGET(BOARD), MAX_VERSION_LEN);
3442 strlcat(device.bootloader_version, "-", MAX_VERSION_LEN);
3443 strlcat(device.bootloader_version, meta_header->img_version, MAX_VERSION_LEN);
3444 }
3445 else
3446 {
3447 strlcpy(device.radio_version, TARGET(BOARD), MAX_VERSION_LEN);
3448 strlcat(device.radio_version, "-", MAX_VERSION_LEN);
3449 strlcat(device.radio_version, meta_header->img_version, MAX_VERSION_LEN);
3450 }
3451
3452 write_device_info(&device);
Ajay Dudanide984792015-03-02 09:57:41 -08003453 fastboot_okay("");
3454 return;
3455}
3456
Ajay Dudani5c761132011-04-07 20:19:04 -07003457void cmd_flash_mmc_sparse_img(const char *arg, void *data, unsigned sz)
3458{
3459 unsigned int chunk;
wufeng.jiang813dc352015-06-02 23:02:46 -04003460 uint64_t chunk_data_sz;
Channagoud Kadabif627cf82013-09-09 14:08:20 -07003461 uint32_t *fill_buf = NULL;
3462 uint32_t fill_val;
Mayank Grover4f50bba2017-07-19 18:17:08 +05303463 uint32_t blk_sz_actual = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07003464 sparse_header_t *sparse_header;
3465 chunk_header_t *chunk_header;
Ajay Dudaniab18f022011-05-12 14:39:22 -07003466 uint32_t total_blocks = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07003467 unsigned long long ptn = 0;
Channagoud Kadabi65b91002011-10-11 17:34:33 +05303468 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07003469 int index = INVALID_PTN;
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05303470 uint32_t i;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003471 uint8_t lun = 0;
vijay kumar800255e2015-04-24 20:26:19 +05303472 /*End of the sparse image address*/
Parth Dixit64b1a482016-03-07 16:31:26 +05303473 uintptr_t data_end = (uintptr_t)data + sz;
Ajay Dudani5c761132011-04-07 20:19:04 -07003474
Kinson Chikf1a43512011-07-14 11:28:39 -07003475 index = partition_get_index(arg);
3476 ptn = partition_get_offset(index);
3477 if(ptn == 0) {
Ajay Dudani5c761132011-04-07 20:19:04 -07003478 fastboot_fail("partition table doesn't exist");
3479 return;
3480 }
3481
Channagoud Kadabi65b91002011-10-11 17:34:33 +05303482 size = partition_get_size(index);
Channagoud Kadabi65b91002011-10-11 17:34:33 +05303483
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003484 lun = partition_get_lun(index);
3485 mmc_set_lun(lun);
3486
vijay kumar800255e2015-04-24 20:26:19 +05303487 if (sz < sizeof(sparse_header_t)) {
3488 fastboot_fail("size too low");
3489 return;
3490 }
3491
Ajay Dudani5c761132011-04-07 20:19:04 -07003492 /* Read and skip over sparse image header */
3493 sparse_header = (sparse_header_t *) data;
vijay kumar800255e2015-04-24 20:26:19 +05303494
Mayank Grover48bd9bb2017-07-19 12:04:16 +05303495 if (!sparse_header->blk_sz || (sparse_header->blk_sz % 4)){
3496 fastboot_fail("Invalid block size\n");
3497 return;
3498 }
3499
vijay kumar1321f342015-03-27 12:13:42 +05303500 if (((uint64_t)sparse_header->total_blks * (uint64_t)sparse_header->blk_sz) > size) {
Ajay Dudani876b3282012-12-21 14:12:17 -08003501 fastboot_fail("size too large");
3502 return;
3503 }
3504
vijay kumarde4fcf62015-04-23 13:05:49 +05303505 data += sizeof(sparse_header_t);
vijay kumar800255e2015-04-24 20:26:19 +05303506
Parth Dixit64b1a482016-03-07 16:31:26 +05303507 if (data_end < (uintptr_t)data) {
vijay kumar800255e2015-04-24 20:26:19 +05303508 fastboot_fail("buffer overreads occured due to invalid sparse header");
3509 return;
3510 }
3511
vijay kumarde4fcf62015-04-23 13:05:49 +05303512 if(sparse_header->file_hdr_sz != sizeof(sparse_header_t))
Ajay Dudani5c761132011-04-07 20:19:04 -07003513 {
vijay kumarde4fcf62015-04-23 13:05:49 +05303514 fastboot_fail("sparse header size mismatch");
3515 return;
Ajay Dudani5c761132011-04-07 20:19:04 -07003516 }
3517
Ajay Dudanib06c05f2011-05-12 14:46:10 -07003518 dprintf (SPEW, "=== Sparse Image Header ===\n");
3519 dprintf (SPEW, "magic: 0x%x\n", sparse_header->magic);
3520 dprintf (SPEW, "major_version: 0x%x\n", sparse_header->major_version);
3521 dprintf (SPEW, "minor_version: 0x%x\n", sparse_header->minor_version);
3522 dprintf (SPEW, "file_hdr_sz: %d\n", sparse_header->file_hdr_sz);
3523 dprintf (SPEW, "chunk_hdr_sz: %d\n", sparse_header->chunk_hdr_sz);
3524 dprintf (SPEW, "blk_sz: %d\n", sparse_header->blk_sz);
3525 dprintf (SPEW, "total_blks: %d\n", sparse_header->total_blks);
3526 dprintf (SPEW, "total_chunks: %d\n", sparse_header->total_chunks);
Ajay Dudani5c761132011-04-07 20:19:04 -07003527
3528 /* Start processing chunks */
3529 for (chunk=0; chunk<sparse_header->total_chunks; chunk++)
3530 {
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05303531 /* Make sure the total image size does not exceed the partition size */
3532 if(((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz) >= size) {
3533 fastboot_fail("size too large");
3534 return;
3535 }
Ajay Dudani5c761132011-04-07 20:19:04 -07003536 /* Read and skip over chunk header */
3537 chunk_header = (chunk_header_t *) data;
3538 data += sizeof(chunk_header_t);
3539
Parth Dixit64b1a482016-03-07 16:31:26 +05303540 if (data_end < (uintptr_t)data) {
vijay kumar800255e2015-04-24 20:26:19 +05303541 fastboot_fail("buffer overreads occured due to invalid sparse header");
3542 return;
3543 }
3544
Ajay Dudani5c761132011-04-07 20:19:04 -07003545 dprintf (SPEW, "=== Chunk Header ===\n");
3546 dprintf (SPEW, "chunk_type: 0x%x\n", chunk_header->chunk_type);
3547 dprintf (SPEW, "chunk_data_sz: 0x%x\n", chunk_header->chunk_sz);
3548 dprintf (SPEW, "total_size: 0x%x\n", chunk_header->total_sz);
3549
vijay kumar800255e2015-04-24 20:26:19 +05303550 if(sparse_header->chunk_hdr_sz != sizeof(chunk_header_t))
Ajay Dudani5c761132011-04-07 20:19:04 -07003551 {
vijay kumar800255e2015-04-24 20:26:19 +05303552 fastboot_fail("chunk header size mismatch");
3553 return;
Ajay Dudani5c761132011-04-07 20:19:04 -07003554 }
3555
wufeng.jiang813dc352015-06-02 23:02:46 -04003556 chunk_data_sz = (uint64_t)sparse_header->blk_sz * chunk_header->chunk_sz;
3557
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05303558 /* Make sure that the chunk size calculated from sparse image does not
3559 * exceed partition size
3560 */
3561 if ((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz + chunk_data_sz > size)
3562 {
3563 fastboot_fail("Chunk data size exceeds partition size");
3564 return;
3565 }
3566
Ajay Dudani5c761132011-04-07 20:19:04 -07003567 switch (chunk_header->chunk_type)
3568 {
3569 case CHUNK_TYPE_RAW:
wufeng.jiang813dc352015-06-02 23:02:46 -04003570 if((uint64_t)chunk_header->total_sz != ((uint64_t)sparse_header->chunk_hdr_sz +
Ajay Dudani5c761132011-04-07 20:19:04 -07003571 chunk_data_sz))
3572 {
3573 fastboot_fail("Bogus chunk size for chunk type Raw");
3574 return;
3575 }
3576
Parth Dixit64b1a482016-03-07 16:31:26 +05303577 if (data_end < (uintptr_t)data + chunk_data_sz) {
vijay kumar800255e2015-04-24 20:26:19 +05303578 fastboot_fail("buffer overreads occured due to invalid sparse header");
3579 return;
3580 }
3581
wufeng.jiang813dc352015-06-02 23:02:46 -04003582 /* chunk_header->total_sz is uint32,So chunk_data_sz is now less than 2^32
3583 otherwise it will return in the line above
3584 */
Ajay Dudaniab18f022011-05-12 14:39:22 -07003585 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
wufeng.jiang813dc352015-06-02 23:02:46 -04003586 (uint32_t)chunk_data_sz,
Ajay Dudaniab18f022011-05-12 14:39:22 -07003587 (unsigned int*)data))
Ajay Dudani5c761132011-04-07 20:19:04 -07003588 {
3589 fastboot_fail("flash write failure");
3590 return;
3591 }
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05303592 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
3593 fastboot_fail("Bogus size for RAW chunk type");
3594 return;
3595 }
Ajay Dudani5c761132011-04-07 20:19:04 -07003596 total_blocks += chunk_header->chunk_sz;
wufeng.jiang813dc352015-06-02 23:02:46 -04003597 data += (uint32_t)chunk_data_sz;
Ajay Dudani5c761132011-04-07 20:19:04 -07003598 break;
3599
Channagoud Kadabif627cf82013-09-09 14:08:20 -07003600 case CHUNK_TYPE_FILL:
3601 if(chunk_header->total_sz != (sparse_header->chunk_hdr_sz +
3602 sizeof(uint32_t)))
3603 {
3604 fastboot_fail("Bogus chunk size for chunk type FILL");
3605 return;
3606 }
3607
Mayank Grover4f50bba2017-07-19 18:17:08 +05303608 blk_sz_actual = ROUNDUP(sparse_header->blk_sz, CACHE_LINE);
3609 /* Integer overflow detected */
3610 if (blk_sz_actual < sparse_header->blk_sz)
3611 {
3612 fastboot_fail("Invalid block size");
3613 return;
3614 }
3615
3616 fill_buf = (uint32_t *)memalign(CACHE_LINE, blk_sz_actual);
Channagoud Kadabif627cf82013-09-09 14:08:20 -07003617 if (!fill_buf)
3618 {
3619 fastboot_fail("Malloc failed for: CHUNK_TYPE_FILL");
3620 return;
3621 }
3622
Parth Dixit64b1a482016-03-07 16:31:26 +05303623 if (data_end < (uintptr_t)data + sizeof(uint32_t)) {
vijay kumar800255e2015-04-24 20:26:19 +05303624 fastboot_fail("buffer overreads occured due to invalid sparse header");
Gaurav Nebhwani163cbf82016-03-17 21:21:27 +05303625 free(fill_buf);
vijay kumar800255e2015-04-24 20:26:19 +05303626 return;
3627 }
Channagoud Kadabif627cf82013-09-09 14:08:20 -07003628 fill_val = *(uint32_t *)data;
3629 data = (char *) data + sizeof(uint32_t);
Channagoud Kadabif627cf82013-09-09 14:08:20 -07003630
3631 for (i = 0; i < (sparse_header->blk_sz / sizeof(fill_val)); i++)
3632 {
3633 fill_buf[i] = fill_val;
3634 }
3635
Gaurav Nebhwanie94cbe12016-03-17 21:16:34 +05303636 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz))
3637 {
3638 fastboot_fail("bogus size for chunk FILL type");
3639 free(fill_buf);
3640 return;
3641 }
3642
wufeng.jiang813dc352015-06-02 23:02:46 -04003643 for (i = 0; i < chunk_header->chunk_sz; i++)
Channagoud Kadabif627cf82013-09-09 14:08:20 -07003644 {
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05303645 /* Make sure that the data written to partition does not exceed partition size */
3646 if ((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz + sparse_header->blk_sz > size)
3647 {
3648 fastboot_fail("Chunk data size for fill type exceeds partition size");
Gaurav Nebhwani163cbf82016-03-17 21:21:27 +05303649 free(fill_buf);
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05303650 return;
3651 }
3652
Channagoud Kadabif627cf82013-09-09 14:08:20 -07003653 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
3654 sparse_header->blk_sz,
3655 fill_buf))
3656 {
3657 fastboot_fail("flash write failure");
3658 free(fill_buf);
3659 return;
3660 }
3661
3662 total_blocks++;
3663 }
3664
3665 free(fill_buf);
3666 break;
3667
Ajay Dudani5c761132011-04-07 20:19:04 -07003668 case CHUNK_TYPE_DONT_CARE:
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05303669 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
3670 fastboot_fail("bogus size for chunk DONT CARE type");
3671 return;
3672 }
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07003673 total_blocks += chunk_header->chunk_sz;
3674 break;
3675
Ajay Dudani5c761132011-04-07 20:19:04 -07003676 case CHUNK_TYPE_CRC:
3677 if(chunk_header->total_sz != sparse_header->chunk_hdr_sz)
3678 {
wufeng.jiang813dc352015-06-02 23:02:46 -04003679 fastboot_fail("Bogus chunk size for chunk type CRC");
Ajay Dudani5c761132011-04-07 20:19:04 -07003680 return;
3681 }
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05303682 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
3683 fastboot_fail("bogus size for chunk CRC type");
3684 return;
3685 }
Ajay Dudani5c761132011-04-07 20:19:04 -07003686 total_blocks += chunk_header->chunk_sz;
Parth Dixit64b1a482016-03-07 16:31:26 +05303687 if ((uintptr_t)data > UINT_MAX - chunk_data_sz) {
vijay kumar800255e2015-04-24 20:26:19 +05303688 fastboot_fail("integer overflow occured");
3689 return;
3690 }
wufeng.jiang813dc352015-06-02 23:02:46 -04003691 data += (uint32_t)chunk_data_sz;
Parth Dixit64b1a482016-03-07 16:31:26 +05303692 if (data_end < (uintptr_t)data) {
vijay kumar800255e2015-04-24 20:26:19 +05303693 fastboot_fail("buffer overreads occured due to invalid sparse header");
3694 return;
3695 }
Ajay Dudani5c761132011-04-07 20:19:04 -07003696 break;
3697
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07003698 default:
Channagoud Kadabif627cf82013-09-09 14:08:20 -07003699 dprintf(CRITICAL, "Unkown chunk type: %x\n",chunk_header->chunk_type);
Ajay Dudani5c761132011-04-07 20:19:04 -07003700 fastboot_fail("Unknown chunk type");
3701 return;
3702 }
3703 }
3704
Ajay Dudani0c6927b2011-05-18 11:12:16 -07003705 dprintf(INFO, "Wrote %d blocks, expected to write %d blocks\n",
3706 total_blocks, sparse_header->total_blks);
3707
3708 if(total_blocks != sparse_header->total_blks)
3709 {
3710 fastboot_fail("sparse image write failure");
3711 }
Ajay Dudani5c761132011-04-07 20:19:04 -07003712
3713 fastboot_okay("");
3714 return;
3715}
3716
3717void cmd_flash_mmc(const char *arg, void *data, unsigned sz)
3718{
3719 sparse_header_t *sparse_header;
Ajay Dudanide984792015-03-02 09:57:41 -08003720 meta_header_t *meta_header;
Ajay Dudani5c761132011-04-07 20:19:04 -07003721
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07003722#ifdef SSD_ENABLE
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -08003723 /* 8 Byte Magic + 2048 Byte xml + Encrypted Data */
3724 unsigned int *magic_number = (unsigned int *) data;
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08003725 int ret=0;
3726 uint32 major_version=0;
3727 uint32 minor_version=0;
3728
3729 ret = scm_svc_version(&major_version,&minor_version);
3730 if(!ret)
3731 {
3732 if(major_version >= 2)
3733 {
Amir Samuelovbb65ce02013-05-05 12:20:18 +03003734 if( !strcmp(arg, "ssd") || !strcmp(arg, "tqs") )
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08003735 {
3736 ret = encrypt_scm((uint32 **) &data, &sz);
3737 if (ret != 0) {
3738 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
3739 return;
3740 }
3741
Amir Samuelovbb65ce02013-05-05 12:20:18 +03003742 /* Protect only for SSD */
3743 if (!strcmp(arg, "ssd")) {
3744 ret = scm_protect_keystore((uint32 *) data, sz);
3745 if (ret != 0) {
3746 dprintf(CRITICAL, "ERROR: scm_protect_keystore Failed\n");
3747 return;
3748 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08003749 }
3750 }
3751 else
3752 {
3753 ret = decrypt_scm_v2((uint32 **) &data, &sz);
3754 if(ret != 0)
3755 {
3756 dprintf(CRITICAL,"ERROR: Decryption Failure\n");
3757 return;
3758 }
3759 }
3760 }
3761 else
3762 {
3763 if (magic_number[0] == DECRYPT_MAGIC_0 &&
3764 magic_number[1] == DECRYPT_MAGIC_1)
3765 {
3766 ret = decrypt_scm((uint32 **) &data, &sz);
3767 if (ret != 0) {
3768 dprintf(CRITICAL, "ERROR: Invalid secure image\n");
3769 return;
3770 }
3771 }
3772 else if (magic_number[0] == ENCRYPT_MAGIC_0 &&
3773 magic_number[1] == ENCRYPT_MAGIC_1)
3774 {
3775 ret = encrypt_scm((uint32 **) &data, &sz);
3776 if (ret != 0) {
3777 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
3778 return;
3779 }
3780 }
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07003781 }
3782 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08003783 else
Neeti Desai127b9e02012-03-20 16:11:23 -07003784 {
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08003785 dprintf(CRITICAL,"INVALID SVC Version\n");
3786 return;
Neeti Desai127b9e02012-03-20 16:11:23 -07003787 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08003788#endif /* SSD_ENABLE */
Neeti Desai127b9e02012-03-20 16:11:23 -07003789
Monika Singh292b3e92018-03-17 22:40:23 +05303790#if VERIFIED_BOOT || VERIFIED_BOOT_2
Channagoud Kadabi35297672015-06-13 11:09:49 -07003791 if (target_build_variant_user())
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003792 {
lijuang511a2b52015-08-14 20:50:51 +08003793 /* if device is locked:
3794 * common partition will not allow to be flashed
3795 * critical partition will allow to flash image.
3796 */
3797 if(!device.is_unlocked && !critical_flash_allowed(arg)) {
3798 fastboot_fail("Partition flashing is not allowed");
3799 return;
3800 }
Mayank Grover889be1b2017-09-12 20:12:23 +05303801
lijuang511a2b52015-08-14 20:50:51 +08003802 /* if device critical is locked:
3803 * common partition will allow to be flashed
3804 * critical partition will not allow to flash image.
3805 */
Mayank Grover912eaa62017-10-26 12:08:53 +05303806 if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05303807 !device.is_unlock_critical &&
3808 critical_flash_allowed(arg)) {
3809 fastboot_fail("Critical partition flashing is not allowed");
3810 return;
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003811 }
3812 }
3813#endif
Monika Singh292b3e92018-03-17 22:40:23 +05303814 if (!strncmp(arg, "avb_custom_key", strlen("avb_custom_key"))) {
3815 dprintf(INFO, "flashing avb_custom_key\n");
3816 if (store_userkey(data, sz)) {
3817 fastboot_fail("Flashing avb_custom_key failed");
3818 } else {
3819 fastboot_okay("");
3820 }
3821 return;
3822 }
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003823
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07003824 sparse_header = (sparse_header_t *) data;
Ajay Dudanide984792015-03-02 09:57:41 -08003825 meta_header = (meta_header_t *) data;
3826 if (sparse_header->magic == SPARSE_HEADER_MAGIC)
Ajay Dudani5c761132011-04-07 20:19:04 -07003827 cmd_flash_mmc_sparse_img(arg, data, sz);
Ajay Dudanide984792015-03-02 09:57:41 -08003828 else if (meta_header->magic == META_HEADER_MAGIC)
3829 cmd_flash_meta_img(arg, data, sz);
3830 else
3831 cmd_flash_mmc_img(arg, data, sz);
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07003832
3833#if VERIFIED_BOOT
Mayank Grover912eaa62017-10-26 12:08:53 +05303834 if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05303835 (!strncmp(arg, "system", 6)) &&
3836 !device.verity_mode)
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07003837 // reset dm_verity mode to enforcing
3838 device.verity_mode = 1;
3839 write_device_info(&device);
Parth Dixitddbc7352015-10-18 03:13:31 +05303840#endif
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07003841
Ajay Dudani5c761132011-04-07 20:19:04 -07003842 return;
3843}
3844
Tanya Brokhman84eaaf62015-02-24 21:57:06 +02003845void cmd_updatevol(const char *vol_name, void *data, unsigned sz)
3846{
3847 struct ptentry *sys_ptn;
3848 struct ptable *ptable;
3849
3850 ptable = flash_get_ptable();
3851 if (ptable == NULL) {
3852 fastboot_fail("partition table doesn't exist");
3853 return;
3854 }
3855
3856 sys_ptn = ptable_find(ptable, "system");
3857 if (sys_ptn == NULL) {
3858 fastboot_fail("system partition not found");
3859 return;
3860 }
3861
3862 sz = ROUND_TO_PAGE(sz, page_mask);
3863 if (update_ubi_vol(sys_ptn, vol_name, data, sz))
3864 fastboot_fail("update_ubi_vol failed");
3865 else
3866 fastboot_okay("");
3867}
3868
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003869void cmd_flash_nand(const char *arg, void *data, unsigned sz)
Dima Zavin214cc642009-01-26 11:16:21 -08003870{
3871 struct ptentry *ptn;
3872 struct ptable *ptable;
3873 unsigned extra = 0;
Gaurav Nebhwanid9730712016-05-06 14:28:29 +05303874 uint64_t partition_size = 0;
Vijay Kumar Pendotic6f22a72017-05-19 22:44:34 +05303875 unsigned bytes_to_round_page = 0;
3876 unsigned rounded_size = 0;
Dima Zavin214cc642009-01-26 11:16:21 -08003877
Kishor PK38ed93d2017-04-25 14:19:26 +05303878 if((uintptr_t)data > (UINT_MAX - sz)) {
3879 fastboot_fail("Cannot flash: image header corrupt");
3880 return;
3881 }
3882
Dima Zavin214cc642009-01-26 11:16:21 -08003883 ptable = flash_get_ptable();
3884 if (ptable == NULL) {
3885 fastboot_fail("partition table doesn't exist");
3886 return;
3887 }
3888
3889 ptn = ptable_find(ptable, arg);
3890 if (ptn == NULL) {
Tanya Brokhman84eaaf62015-02-24 21:57:06 +02003891 dprintf(INFO, "unknown partition name (%s). Trying updatevol\n",
3892 arg);
3893 cmd_updatevol(arg, data, sz);
Dima Zavin214cc642009-01-26 11:16:21 -08003894 return;
3895 }
3896
Monika Singh292b3e92018-03-17 22:40:23 +05303897 if (!strncmp(arg, "avb_custom_key", strlen("avb_custom_key"))) {
3898 dprintf(INFO, "flashing avb_custom_key\n");
3899 if (store_userkey(data, sz)) {
3900 fastboot_fail("Flashing avb_custom_key failed");
3901 } else {
3902 fastboot_okay("");
3903 }
3904 return;
3905 }
3906
Dima Zavin214cc642009-01-26 11:16:21 -08003907 if (!strcmp(ptn->name, "boot") || !strcmp(ptn->name, "recovery")) {
Kishor PK38ed93d2017-04-25 14:19:26 +05303908 if((sz > BOOT_MAGIC_SIZE) && (!memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE))) {
3909 dprintf(INFO, "Verified the BOOT_MAGIC in image header \n");
3910 } else {
3911 fastboot_fail("Image is not a boot image");
Dima Zavin214cc642009-01-26 11:16:21 -08003912 return;
3913 }
3914 }
3915
Amol Jadi5c61a952012-05-04 17:05:35 -07003916 if (!strcmp(ptn->name, "system")
Deepa Dinamani13e32c42012-03-12 14:34:17 -07003917 || !strcmp(ptn->name, "userdata")
3918 || !strcmp(ptn->name, "persist")
Sundarajan Srinivasanb063a852013-11-19 14:02:27 -08003919 || !strcmp(ptn->name, "recoveryfs")
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -08003920 || !strcmp(ptn->name, "modem"))
Tanya Brokhman1c94f1a2015-02-15 09:05:03 +02003921 extra = 1;
Kishor PK38ed93d2017-04-25 14:19:26 +05303922 else {
Vijay Kumar Pendotic6f22a72017-05-19 22:44:34 +05303923 rounded_size = ROUNDUP(sz, page_size);
3924 bytes_to_round_page = rounded_size - sz;
3925 if (bytes_to_round_page) {
3926 if (((uintptr_t)data + sz ) > (UINT_MAX - bytes_to_round_page)) {
3927 fastboot_fail("Integer overflow detected");
3928 return;
3929 }
3930 if (((uintptr_t)data + sz + bytes_to_round_page) >
3931 ((uintptr_t)target_get_scratch_address() + target_get_max_flash_size())) {
3932 fastboot_fail("Buffer size is not aligned to page_size");
3933 return;
3934 }
3935 else {
3936 memset(data + sz, 0, bytes_to_round_page);
3937 sz = rounded_size;
3938 }
Kishor PK38ed93d2017-04-25 14:19:26 +05303939 }
Gaurav Nebhwanid9730712016-05-06 14:28:29 +05303940 }
3941
Kishor PK38ed93d2017-04-25 14:19:26 +05303942 /*Checking partition_size for the possible integer overflow */
3943 partition_size = validate_partition_size(ptn);
3944
Gaurav Nebhwanid9730712016-05-06 14:28:29 +05303945 if (sz > partition_size) {
3946 fastboot_fail("Image size too large");
3947 return;
3948 }
3949
Dima Zavin214cc642009-01-26 11:16:21 -08003950 dprintf(INFO, "writing %d bytes to '%s'\n", sz, ptn->name);
Mayank Grover6cde9352017-06-06 18:45:23 +05303951 if ((sz > UBI_EC_HDR_SIZE) &&
3952 (!memcmp((void *)data, UBI_MAGIC, UBI_MAGIC_SIZE))) {
Tanya Brokhman1c94f1a2015-02-15 09:05:03 +02003953 if (flash_ubi_img(ptn, data, sz)) {
3954 fastboot_fail("flash write failure");
3955 return;
3956 }
3957 } else {
3958 if (flash_write(ptn, extra, data, sz)) {
3959 fastboot_fail("flash write failure");
3960 return;
3961 }
Dima Zavin214cc642009-01-26 11:16:21 -08003962 }
3963 dprintf(INFO, "partition '%s' updated\n", ptn->name);
3964 fastboot_okay("");
3965}
3966
Kishor PK38ed93d2017-04-25 14:19:26 +05303967
3968static inline uint64_t validate_partition_size(struct ptentry *ptn)
3969{
3970 if (ptn->length && flash_num_pages_per_blk() && page_size) {
3971 if ((ptn->length < ( UINT_MAX / flash_num_pages_per_blk())) && ((ptn->length * flash_num_pages_per_blk()) < ( UINT_MAX / page_size))) {
3972 return ptn->length * flash_num_pages_per_blk() * page_size;
3973 }
3974 }
3975 return 0;
3976}
3977
3978
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003979void cmd_flash(const char *arg, void *data, unsigned sz)
3980{
3981 if(target_is_emmc_boot())
3982 cmd_flash_mmc(arg, data, sz);
3983 else
3984 cmd_flash_nand(arg, data, sz);
3985}
3986
Dima Zavin214cc642009-01-26 11:16:21 -08003987void cmd_continue(const char *arg, void *data, unsigned sz)
3988{
3989 fastboot_okay("");
Amol Jadi7c4316c2013-10-07 14:19:26 -07003990 fastboot_stop();
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003991
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003992 if (target_is_emmc_boot())
3993 {
lijuanga40d6302015-07-20 20:10:13 +08003994#if FBCON_DISPLAY_MSG
lijuangde34d502016-02-26 16:04:50 +08003995 /* Exit keys' detection thread firstly */
3996 exit_menu_keys_detection();
lijuanga40d6302015-07-20 20:10:13 +08003997#endif
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003998 boot_linux_from_mmc();
3999 }
4000 else
4001 {
4002 boot_linux_from_flash();
4003 }
Dima Zavin214cc642009-01-26 11:16:21 -08004004}
4005
Chandan Uddaraju94183c02010-01-15 15:13:59 -08004006void cmd_reboot(const char *arg, void *data, unsigned sz)
4007{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004008 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08004009 fastboot_okay("");
4010 reboot_device(0);
4011}
4012
Mayank Grover351a75e2017-05-30 20:06:08 +05304013void cmd_set_active(const char *arg, void *data, unsigned sz)
4014{
Mayank Grover6ccc1c92017-07-04 17:36:46 +05304015 char *p, *sp = NULL;
Mayank Grover351a75e2017-05-30 20:06:08 +05304016 unsigned i,current_active_slot;
4017 const char *current_slot_suffix;
4018
4019 if (!partition_multislot_is_supported())
4020 {
4021 fastboot_fail("Command not supported");
4022 return;
4023 }
4024
4025 if (arg)
4026 {
Mayank Grover6ccc1c92017-07-04 17:36:46 +05304027 p = strtok_r((char *)arg, ":", &sp);
Mayank Grover5eb5f692017-07-19 20:16:04 +05304028 if (p)
Mayank Grover351a75e2017-05-30 20:06:08 +05304029 {
4030 current_active_slot = partition_find_active_slot();
4031
4032 /* Check if trying to make curent slot active */
4033 current_slot_suffix = SUFFIX_SLOT(current_active_slot);
Mayank Grover6ccc1c92017-07-04 17:36:46 +05304034 current_slot_suffix = strtok_r((char *)current_slot_suffix,
4035 (char *)suffix_delimiter, &sp);
4036
Mayank Grover5eb5f692017-07-19 20:16:04 +05304037 if (current_slot_suffix &&
4038 !strncmp(p, current_slot_suffix, strlen(current_slot_suffix)))
Mayank Grover351a75e2017-05-30 20:06:08 +05304039 {
4040 fastboot_okay("Slot already set active");
4041 return;
4042 }
4043 else
4044 {
4045 for (i = 0; i < AB_SUPPORTED_SLOTS; i++)
4046 {
4047 current_slot_suffix = SUFFIX_SLOT(i);
Mayank Grover6ccc1c92017-07-04 17:36:46 +05304048 current_slot_suffix = strtok_r((char *)current_slot_suffix,
4049 (char *)suffix_delimiter, &sp);
Mayank Grover5eb5f692017-07-19 20:16:04 +05304050 if (current_slot_suffix &&
4051 !strncmp(p, current_slot_suffix, strlen(current_slot_suffix)))
Mayank Grover351a75e2017-05-30 20:06:08 +05304052 {
4053 partition_switch_slots(current_active_slot, i);
4054 publish_getvar_multislot_vars();
4055 fastboot_okay("");
4056 return;
4057 }
4058 }
4059 }
4060 }
4061 }
4062 fastboot_fail("Invalid slot suffix.");
4063 return;
4064}
4065
Chandan Uddaraju94183c02010-01-15 15:13:59 -08004066void cmd_reboot_bootloader(const char *arg, void *data, unsigned sz)
4067{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004068 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08004069 fastboot_okay("");
4070 reboot_device(FASTBOOT_MODE);
4071}
4072
Ameya Thakur11cf1a62013-08-05 12:44:48 -07004073void cmd_oem_enable_charger_screen(const char *arg, void *data, unsigned size)
4074{
4075 dprintf(INFO, "Enabling charger screen check\n");
4076 device.charger_screen_enabled = 1;
4077 write_device_info(&device);
4078 fastboot_okay("");
4079}
4080
4081void cmd_oem_disable_charger_screen(const char *arg, void *data, unsigned size)
4082{
4083 dprintf(INFO, "Disabling charger screen check\n");
4084 device.charger_screen_enabled = 0;
4085 write_device_info(&device);
4086 fastboot_okay("");
4087}
4088
lijuanga25d8bb2015-09-16 13:11:52 +08004089void cmd_oem_off_mode_charger(const char *arg, void *data, unsigned size)
4090{
4091 char *p = NULL;
4092 const char *delim = " \t\n\r";
Parth Dixit407f15b2016-01-07 14:47:37 +05304093 char *sp;
lijuanga25d8bb2015-09-16 13:11:52 +08004094
4095 if (arg) {
Parth Dixit407f15b2016-01-07 14:47:37 +05304096 p = strtok_r((char *)arg, delim, &sp);
lijuanga25d8bb2015-09-16 13:11:52 +08004097 if (p) {
4098 if (!strncmp(p, "0", 1)) {
4099 device.charger_screen_enabled = 0;
4100 } else if (!strncmp(p, "1", 1)) {
4101 device.charger_screen_enabled = 1;
4102 }
4103 }
4104 }
4105
4106 /* update charger_screen_enabled value for getvar
4107 * command
4108 */
4109 snprintf(charger_screen_enabled, MAX_RSP_SIZE, "%d",
4110 device.charger_screen_enabled);
4111
4112 write_device_info(&device);
4113 fastboot_okay("");
4114}
4115
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05304116void cmd_oem_select_display_panel(const char *arg, void *data, unsigned size)
4117{
4118 dprintf(INFO, "Selecting display panel %s\n", arg);
4119 if (arg)
4120 strlcpy(device.display_panel, arg,
4121 sizeof(device.display_panel));
4122 write_device_info(&device);
4123 fastboot_okay("");
4124}
4125
Shashank Mittal162244e2011-08-08 19:01:25 -07004126void cmd_oem_unlock(const char *arg, void *data, unsigned sz)
4127{
lijuang4ece1e72015-08-14 21:02:36 +08004128 set_device_unlock(UNLOCK, TRUE);
vijay kumarc65876c2015-04-24 13:29:16 +05304129}
4130
4131void cmd_oem_unlock_go(const char *arg, void *data, unsigned sz)
4132{
lijuang4ece1e72015-08-14 21:02:36 +08004133 if(!device.is_unlocked) {
vijay kumarc65876c2015-04-24 13:29:16 +05304134 if(!is_allow_unlock) {
4135 fastboot_fail("oem unlock is not allowed");
4136 return;
4137 }
4138
lijuang4ece1e72015-08-14 21:02:36 +08004139 set_device_unlock_value(UNLOCK, TRUE);
vijay kumarc65876c2015-04-24 13:29:16 +05304140
lijuang4ece1e72015-08-14 21:02:36 +08004141 /* wipe data */
vijay kumarc65876c2015-04-24 13:29:16 +05304142 struct recovery_message msg;
Kishor PK60a68212017-05-08 16:55:57 +05304143 memset(&msg, 0, sizeof(msg));
vijay kumarc65876c2015-04-24 13:29:16 +05304144 snprintf(msg.recovery, sizeof(msg.recovery), "recovery\n--wipe_data");
4145 write_misc(0, &msg, sizeof(msg));
4146
4147 fastboot_okay("");
4148 reboot_device(RECOVERY_MODE);
Shashank Mittald3e54dd2014-08-28 15:24:02 -07004149 }
4150 fastboot_okay("");
4151}
4152
Channagoud Kadabiad259832015-05-29 11:14:17 -07004153static int aboot_frp_unlock(char *pname, void *data, unsigned sz)
4154{
Mayank Grover70f8b0e2017-05-17 11:49:00 +05304155 int ret=1;
4156 bool authentication_success=false;
Channagoud Kadabiad259832015-05-29 11:14:17 -07004157
Mayank Grover70f8b0e2017-05-17 11:49:00 +05304158 /*
4159 Authentication method not implemented.
4160
4161 OEM to implement, authentication system which on successful validataion,
4162 calls write_allow_oem_unlock() with is_allow_unlock.
4163 */
4164#if 0
4165 authentication_success = oem_specific_auth_mthd();
4166#endif
4167
4168 if (authentication_success)
Channagoud Kadabiad259832015-05-29 11:14:17 -07004169 {
Mayank Grover70f8b0e2017-05-17 11:49:00 +05304170 is_allow_unlock = true;
4171 write_allow_oem_unlock(is_allow_unlock);
4172 ret = 0;
Channagoud Kadabiad259832015-05-29 11:14:17 -07004173 }
4174 return ret;
4175}
4176
Shashank Mittald3e54dd2014-08-28 15:24:02 -07004177void cmd_oem_lock(const char *arg, void *data, unsigned sz)
4178{
lijuang4ece1e72015-08-14 21:02:36 +08004179 set_device_unlock(UNLOCK, FALSE);
Shashank Mittal162244e2011-08-08 19:01:25 -07004180}
4181
Shashank Mittala0032282011-08-26 14:50:11 -07004182void cmd_oem_devinfo(const char *arg, void *data, unsigned sz)
4183{
lijuang511a2b52015-08-14 20:50:51 +08004184 char response[MAX_RSP_SIZE];
Ameya Thakur11cf1a62013-08-05 12:44:48 -07004185 snprintf(response, sizeof(response), "\tDevice tampered: %s", (device.is_tampered ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07004186 fastboot_info(response);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07004187 snprintf(response, sizeof(response), "\tDevice unlocked: %s", (device.is_unlocked ? "true" : "false"));
4188 fastboot_info(response);
Monika Singh292b3e92018-03-17 22:40:23 +05304189#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover912eaa62017-10-26 12:08:53 +05304190 if (VB_M <= target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +05304191 {
4192 snprintf(response, sizeof(response), "\tDevice critical unlocked: %s",
4193 (device.is_unlock_critical ? "true" : "false"));
4194 fastboot_info(response);
4195 }
Parth Dixitddbc7352015-10-18 03:13:31 +05304196#endif
Ameya Thakur11cf1a62013-08-05 12:44:48 -07004197 snprintf(response, sizeof(response), "\tCharger screen enabled: %s", (device.charger_screen_enabled ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07004198 fastboot_info(response);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05304199 snprintf(response, sizeof(response), "\tDisplay panel: %s", (device.display_panel));
4200 fastboot_info(response);
Shashank Mittala0032282011-08-26 14:50:11 -07004201 fastboot_okay("");
4202}
4203
lijuang511a2b52015-08-14 20:50:51 +08004204void cmd_flashing_get_unlock_ability(const char *arg, void *data, unsigned sz)
4205{
4206 char response[MAX_RSP_SIZE];
4207 snprintf(response, sizeof(response), "\tget_unlock_ability: %d", is_allow_unlock);
4208 fastboot_info(response);
4209 fastboot_okay("");
4210}
4211
4212void cmd_flashing_lock_critical(const char *arg, void *data, unsigned sz)
4213{
lijuang4ece1e72015-08-14 21:02:36 +08004214 set_device_unlock(UNLOCK_CRITICAL, FALSE);
lijuang511a2b52015-08-14 20:50:51 +08004215}
4216
4217void cmd_flashing_unlock_critical(const char *arg, void *data, unsigned sz)
4218{
lijuang4ece1e72015-08-14 21:02:36 +08004219 set_device_unlock(UNLOCK_CRITICAL, TRUE);
lijuang511a2b52015-08-14 20:50:51 +08004220}
4221
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07004222void cmd_preflash(const char *arg, void *data, unsigned sz)
4223{
4224 fastboot_okay("");
4225}
4226
Mao Flynn7b379f32015-04-20 00:28:30 +08004227static uint8_t logo_header[LOGO_IMG_HEADER_SIZE];
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304228
Mao Flynn7b379f32015-04-20 00:28:30 +08004229int splash_screen_check_header(logo_img_header *header)
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304230{
Mao Flynn7b379f32015-04-20 00:28:30 +08004231 if (memcmp(header->magic, LOGO_IMG_MAGIC, 8))
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304232 return -1;
Mao Flynn7b379f32015-04-20 00:28:30 +08004233 if (header->width == 0 || header->height == 0)
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304234 return -1;
4235 return 0;
4236}
4237
Mao Flynn7b379f32015-04-20 00:28:30 +08004238int splash_screen_flash()
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07004239{
4240 struct ptentry *ptn;
4241 struct ptable *ptable;
Mao Flynn7b379f32015-04-20 00:28:30 +08004242 struct logo_img_header *header;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07004243 struct fbcon_config *fb_display = NULL;
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08004244
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304245 ptable = flash_get_ptable();
4246 if (ptable == NULL) {
Mao Flynn7b379f32015-04-20 00:28:30 +08004247 dprintf(CRITICAL, "ERROR: Partition table not found\n");
4248 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304249 }
Mao Flynn7b379f32015-04-20 00:28:30 +08004250
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304251 ptn = ptable_find(ptable, "splash");
4252 if (ptn == NULL) {
4253 dprintf(CRITICAL, "ERROR: splash Partition not found\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08004254 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304255 }
Mao Flynn7b379f32015-04-20 00:28:30 +08004256 if (flash_read(ptn, 0, (void *)logo_header, LOGO_IMG_HEADER_SIZE)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304257 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08004258 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304259 }
4260
Mao Flynn7b379f32015-04-20 00:28:30 +08004261 header = (struct logo_img_header *)logo_header;
4262 if (splash_screen_check_header(header)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304263 dprintf(CRITICAL, "ERROR: Boot image header invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08004264 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304265 }
4266
4267 fb_display = fbcon_display();
4268 if (fb_display) {
Mao Flynn7b379f32015-04-20 00:28:30 +08004269 if (header->type && (header->blocks != 0)) { // RLE24 compressed data
4270 uint8_t *base = (uint8_t *) fb_display->base + LOGO_IMG_OFFSET;
4271
4272 /* if the logo is full-screen size, remove "fbcon_clear()" */
4273 if ((header->width != fb_display->width)
4274 || (header->height != fb_display->height))
4275 fbcon_clear();
4276
4277 if (flash_read(ptn + LOGO_IMG_HEADER_SIZE, 0,
4278 (uint32_t *)base,
4279 (header->blocks * 512))) {
4280 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
4281 return -1;
4282 }
4283 fbcon_extract_to_screen(header, base);
4284 return 0;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07004285 }
Mao Flynn7b379f32015-04-20 00:28:30 +08004286
Veera Sundaram Sankaranc33416f2015-09-28 10:25:55 -07004287 if ((header->width > fb_display->width) || (header->height > fb_display->height)) {
4288 dprintf(CRITICAL, "Logo config greater than fb config. Fall back default logo\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08004289 return -1;
4290 }
4291
Vineet Bajaj99291ed2014-09-09 12:29:46 +05304292 uint8_t *base = (uint8_t *) fb_display->base;
Sachin Bhayare619e9e42017-05-15 13:10:31 +05304293 uint32_t fb_size = ROUNDUP(fb_display->width *
4294 fb_display->height *
4295 (fb_display->bpp / 8), 4096);
4296 uint32_t splash_size = ((((header->width * header->height *
4297 fb_display->bpp/8) + 511) >> 9) << 9);
4298
4299 if (splash_size > fb_size) {
4300 dprintf(CRITICAL, "ERROR: Splash image size invalid\n");
4301 return -1;
4302 }
4303
Mao Flynn7b379f32015-04-20 00:28:30 +08004304 if (flash_read(ptn + LOGO_IMG_HEADER_SIZE, 0,
4305 (uint32_t *)base,
4306 ((((header->width * header->height * fb_display->bpp/8) + 511) >> 9) << 9))) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304307 fbcon_clear();
Vineet Bajaj99291ed2014-09-09 12:29:46 +05304308 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08004309 return -1;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07004310 }
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304311 }
4312
Mao Flynn7b379f32015-04-20 00:28:30 +08004313 return 0;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304314}
4315
Mao Flynn7b379f32015-04-20 00:28:30 +08004316int splash_screen_mmc()
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304317{
4318 int index = INVALID_PTN;
4319 unsigned long long ptn = 0;
4320 struct fbcon_config *fb_display = NULL;
Mao Flynn7b379f32015-04-20 00:28:30 +08004321 struct logo_img_header *header;
Mao Flynn1893a7f2015-06-03 12:03:36 +08004322 uint32_t blocksize, realsize, readsize;
4323 uint8_t *base;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304324
4325 index = partition_get_index("splash");
4326 if (index == 0) {
4327 dprintf(CRITICAL, "ERROR: splash Partition table not found\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08004328 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304329 }
4330
4331 ptn = partition_get_offset(index);
4332 if (ptn == 0) {
4333 dprintf(CRITICAL, "ERROR: splash Partition invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08004334 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304335 }
4336
Mao Flynn1893a7f2015-06-03 12:03:36 +08004337 mmc_set_lun(partition_get_lun(index));
4338
4339 blocksize = mmc_get_device_blocksize();
4340 if (blocksize == 0) {
4341 dprintf(CRITICAL, "ERROR:splash Partition invalid blocksize\n");
4342 return -1;
4343 }
4344
4345 fb_display = fbcon_display();
Channagoud Kadabidaf10a62015-07-22 11:51:55 -07004346 if (!fb_display)
4347 {
4348 dprintf(CRITICAL, "ERROR: fb config is not allocated\n");
4349 return -1;
4350 }
4351
Mao Flynn1893a7f2015-06-03 12:03:36 +08004352 base = (uint8_t *) fb_display->base;
4353
4354 if (mmc_read(ptn, (uint32_t *)(base + LOGO_IMG_OFFSET), blocksize)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304355 dprintf(CRITICAL, "ERROR: Cannot read splash image header\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08004356 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304357 }
4358
Mao Flynn1893a7f2015-06-03 12:03:36 +08004359 header = (struct logo_img_header *)(base + LOGO_IMG_OFFSET);
Mao Flynn7b379f32015-04-20 00:28:30 +08004360 if (splash_screen_check_header(header)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304361 dprintf(CRITICAL, "ERROR: Splash image header invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08004362 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304363 }
4364
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304365 if (fb_display) {
Mao Flynn1893a7f2015-06-03 12:03:36 +08004366 if (header->type && (header->blocks != 0)) { /* 1 RLE24 compressed data */
4367 base += LOGO_IMG_OFFSET;
Mao Flynn7b379f32015-04-20 00:28:30 +08004368
Mao Flynn1893a7f2015-06-03 12:03:36 +08004369 realsize = header->blocks * 512;
4370 readsize = ROUNDUP((realsize + LOGO_IMG_HEADER_SIZE), blocksize) - blocksize;
4371
4372 /* if the logo is not full-screen size, clean screen */
Mao Flynn7b379f32015-04-20 00:28:30 +08004373 if ((header->width != fb_display->width)
4374 || (header->height != fb_display->height))
4375 fbcon_clear();
4376
Sachin Bhayare619e9e42017-05-15 13:10:31 +05304377 uint32_t fb_size = ROUNDUP(fb_display->width *
4378 fb_display->height *
4379 (fb_display->bpp / 8), 4096);
4380
4381 if (readsize > fb_size) {
4382 dprintf(CRITICAL, "ERROR: Splash image size invalid\n");
4383 return -1;
4384 }
4385
Mao Flynn1893a7f2015-06-03 12:03:36 +08004386 if (mmc_read(ptn + blocksize, (uint32_t *)(base + blocksize), readsize)) {
Mao Flynn7b379f32015-04-20 00:28:30 +08004387 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
4388 return -1;
4389 }
Mao Flynn7b379f32015-04-20 00:28:30 +08004390
Mao Flynn1893a7f2015-06-03 12:03:36 +08004391 fbcon_extract_to_screen(header, (base + LOGO_IMG_HEADER_SIZE));
4392 } else { /* 2 Raw BGR data */
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304393
Veera Sundaram Sankaranc33416f2015-09-28 10:25:55 -07004394 if ((header->width > fb_display->width) || (header->height > fb_display->height)) {
4395 dprintf(CRITICAL, "Logo config greater than fb config. Fall back default logo\n");
Mao Flynn1893a7f2015-06-03 12:03:36 +08004396 return -1;
4397 }
4398
4399 realsize = header->width * header->height * fb_display->bpp / 8;
4400 readsize = ROUNDUP((realsize + LOGO_IMG_HEADER_SIZE), blocksize) - blocksize;
4401
4402 if (blocksize == LOGO_IMG_HEADER_SIZE) { /* read the content directly */
4403 if (mmc_read((ptn + LOGO_IMG_HEADER_SIZE), (uint32_t *)base, readsize)) {
4404 fbcon_clear();
4405 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
4406 return -1;
4407 }
4408 } else {
4409 if (mmc_read(ptn + blocksize ,
4410 (uint32_t *)(base + LOGO_IMG_OFFSET + blocksize), readsize)) {
4411 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
4412 return -1;
4413 }
4414 memmove(base, (base + LOGO_IMG_OFFSET + LOGO_IMG_HEADER_SIZE), realsize);
4415 }
4416 }
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304417 }
4418
Mao Flynn7b379f32015-04-20 00:28:30 +08004419 return 0;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304420}
4421
Mao Flynn7b379f32015-04-20 00:28:30 +08004422int fetch_image_from_partition()
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304423{
4424 if (target_is_emmc_boot()) {
4425 return splash_screen_mmc();
4426 } else {
4427 return splash_screen_flash();
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07004428 }
4429}
4430
Mayank Grover351a75e2017-05-30 20:06:08 +05304431void publish_getvar_multislot_vars()
4432{
4433 int i,count;
4434 static bool published = false;
4435 static char slot_count[MAX_RSP_SIZE];
4436 static struct ab_slot_info slot_info[AB_SUPPORTED_SLOTS];
4437 static char active_slot_suffix[MAX_RSP_SIZE];
4438 static char has_slot_pname[NUM_PARTITIONS][MAX_GET_VAR_NAME_SIZE];
4439 static char has_slot_reply[NUM_PARTITIONS][MAX_RSP_SIZE];
4440 const char *tmp;
4441 char tmpbuff[MAX_GET_VAR_NAME_SIZE];
Mayank Grover2bd84bb2017-06-20 11:06:07 +05304442 signed active_slt;
Mayank Grover351a75e2017-05-30 20:06:08 +05304443
4444 if (!published)
4445 {
4446 /* Update slot meta info */
4447 count = partition_fill_partition_meta(has_slot_pname, has_slot_reply,
4448 partition_get_partition_count());
4449 for(i=0; i<count; i++)
4450 {
4451 memset(tmpbuff, 0, MAX_GET_VAR_NAME_SIZE);
Mayank Grover2bd84bb2017-06-20 11:06:07 +05304452 snprintf(tmpbuff, MAX_GET_VAR_NAME_SIZE,"has-slot:%s",
4453 has_slot_pname[i]);
4454 strlcpy(has_slot_pname[i], tmpbuff, MAX_GET_VAR_NAME_SIZE);
Mayank Grover351a75e2017-05-30 20:06:08 +05304455 fastboot_publish(has_slot_pname[i], has_slot_reply[i]);
4456 }
4457
4458 for (i=0; i<AB_SUPPORTED_SLOTS; i++)
4459 {
4460 tmp = SUFFIX_SLOT(i);
Mayank Grover7b8a8f82017-10-27 13:07:59 +05304461 tmp++; // to remove "_" from slot_suffix.
Mayank Grover2bd84bb2017-06-20 11:06:07 +05304462 snprintf(slot_info[i].slot_is_unbootable, sizeof(slot_info[i].slot_is_unbootable),
4463 "slot-unbootable:%s", tmp);
4464 snprintf(slot_info[i].slot_is_active, sizeof(slot_info[i].slot_is_active),
4465 "slot-active:%s", tmp);
4466 snprintf(slot_info[i].slot_is_succesful, sizeof(slot_info[i].slot_is_succesful),
4467 "slot-success:%s", tmp);
4468 snprintf(slot_info[i].slot_retry_count, sizeof(slot_info[i].slot_retry_count),
4469 "slot-retry-count:%s", tmp);
Mayank Grover351a75e2017-05-30 20:06:08 +05304470 fastboot_publish(slot_info[i].slot_is_unbootable,
4471 slot_info[i].slot_is_unbootable_rsp);
4472 fastboot_publish(slot_info[i].slot_is_active,
4473 slot_info[i].slot_is_active_rsp);
4474 fastboot_publish(slot_info[i].slot_is_succesful,
4475 slot_info[i].slot_is_succesful_rsp);
4476 fastboot_publish(slot_info[i].slot_retry_count,
4477 slot_info[i].slot_retry_count_rsp);
4478 }
4479 fastboot_publish("current-slot", active_slot_suffix);
4480 snprintf(slot_count, sizeof(slot_count),"%d", AB_SUPPORTED_SLOTS);
4481 fastboot_publish("slot-count", slot_count);
4482 published = true;
4483 }
4484
Mayank Grover2bd84bb2017-06-20 11:06:07 +05304485 active_slt = partition_find_active_slot();
4486 if (active_slt != INVALID)
Mayank Grover7b8a8f82017-10-27 13:07:59 +05304487 {
4488 tmp = SUFFIX_SLOT(active_slt);
4489 tmp++; // to remove "_" from slot_suffix.
4490 snprintf(active_slot_suffix, sizeof(active_slot_suffix), "%s", tmp);
4491 }
Mayank Grover2bd84bb2017-06-20 11:06:07 +05304492 else
4493 strlcpy(active_slot_suffix, "INVALID", sizeof(active_slot_suffix));
4494
Mayank Grover351a75e2017-05-30 20:06:08 +05304495 /* Update partition meta information */
4496 partition_fill_slot_meta(slot_info);
4497 return;
4498}
4499
lijuang4ece1e72015-08-14 21:02:36 +08004500void get_product_name(unsigned char *buf)
4501{
4502 snprintf((char*)buf, MAX_RSP_SIZE, "%s", TARGET(BOARD));
4503 return;
4504}
4505
Mayank Grover7b8a8f82017-10-27 13:07:59 +05304506#if PRODUCT_IOT
4507void get_bootloader_version_iot(unsigned char *buf)
4508{
4509 if (buf != NULL)
4510 {
4511 strlcpy(buf, TARGET(BOARD), MAX_VERSION_LEN);
4512 strlcat(buf, "-", MAX_VERSION_LEN);
4513 strlcat(buf, PRODUCT_IOT_VERSION, MAX_VERSION_LEN);
4514 }
4515 return;
4516}
4517#endif
4518
lijuang4ece1e72015-08-14 21:02:36 +08004519void get_bootloader_version(unsigned char *buf)
4520{
4521 snprintf((char*)buf, MAX_RSP_SIZE, "%s", device.bootloader_version);
4522 return;
4523}
4524
4525void get_baseband_version(unsigned char *buf)
4526{
4527 snprintf((char*)buf, MAX_RSP_SIZE, "%s", device.radio_version);
4528 return;
4529}
4530
Monika Singh32a09f72018-03-14 13:08:29 +05304531bool is_device_locked_critical()
4532{
4533 return device.is_unlock_critical ? false:true;
4534}
4535
lijuang4ece1e72015-08-14 21:02:36 +08004536bool is_device_locked()
4537{
4538 return device.is_unlocked ? false:true;
4539}
4540
Monika Singh32a09f72018-03-14 13:08:29 +05304541bool is_verity_enforcing()
4542{
4543 return device.verity_mode ? true:false;
4544}
4545
Amol Jadi5edf3552013-07-23 14:15:34 -07004546/* register commands and variables for fastboot */
4547void aboot_fastboot_register_commands(void)
4548{
Sridhar Parasurame94e8152014-10-24 14:06:03 -07004549 int i;
lijuangf16461c2015-08-03 17:09:34 +08004550 char hw_platform_buf[MAX_RSP_SIZE];
Amol Jadi5edf3552013-07-23 14:15:34 -07004551
Sridhar Parasurame94e8152014-10-24 14:06:03 -07004552 struct fastboot_cmd_desc cmd_list[] = {
lijuang511a2b52015-08-14 20:50:51 +08004553 /* By default the enabled list is empty. */
4554 {"", NULL},
4555 /* move commands enclosed within the below ifndef to here
4556 * if they need to be enabled in user build.
4557 */
Sridhar Parasurame94e8152014-10-24 14:06:03 -07004558#ifndef DISABLE_FASTBOOT_CMDS
lijuang511a2b52015-08-14 20:50:51 +08004559 /* Register the following commands only for non-user builds */
4560 {"flash:", cmd_flash},
4561 {"erase:", cmd_erase},
4562 {"boot", cmd_boot},
4563 {"continue", cmd_continue},
4564 {"reboot", cmd_reboot},
4565 {"reboot-bootloader", cmd_reboot_bootloader},
4566 {"oem unlock", cmd_oem_unlock},
4567 {"oem unlock-go", cmd_oem_unlock_go},
4568 {"oem lock", cmd_oem_lock},
4569 {"flashing unlock", cmd_oem_unlock},
4570 {"flashing lock", cmd_oem_lock},
4571 {"flashing lock_critical", cmd_flashing_lock_critical},
4572 {"flashing unlock_critical", cmd_flashing_unlock_critical},
4573 {"flashing get_unlock_ability", cmd_flashing_get_unlock_ability},
4574 {"oem device-info", cmd_oem_devinfo},
4575 {"preflash", cmd_preflash},
4576 {"oem enable-charger-screen", cmd_oem_enable_charger_screen},
4577 {"oem disable-charger-screen", cmd_oem_disable_charger_screen},
lijuanga25d8bb2015-09-16 13:11:52 +08004578 {"oem off-mode-charge", cmd_oem_off_mode_charger},
lijuang511a2b52015-08-14 20:50:51 +08004579 {"oem select-display-panel", cmd_oem_select_display_panel},
Mayank Grover6ccc1c92017-07-04 17:36:46 +05304580 {"set_active",cmd_set_active},
Channagoud Kadabi736c4962015-08-21 11:56:52 -07004581#if UNITTEST_FW_SUPPORT
Channagoud Kadabid5ddf482015-09-28 10:31:35 -07004582 {"oem run-tests", cmd_oem_runtests},
Channagoud Kadabi736c4962015-08-21 11:56:52 -07004583#endif
Sridhar Parasurame94e8152014-10-24 14:06:03 -07004584#endif
lijuang511a2b52015-08-14 20:50:51 +08004585 };
Sridhar Parasurame94e8152014-10-24 14:06:03 -07004586
4587 int fastboot_cmds_count = sizeof(cmd_list)/sizeof(cmd_list[0]);
4588 for (i = 1; i < fastboot_cmds_count; i++)
4589 fastboot_register(cmd_list[i].name,cmd_list[i].cb);
4590
Amol Jadi5edf3552013-07-23 14:15:34 -07004591 /* publish variables and their values */
4592 fastboot_publish("product", TARGET(BOARD));
4593 fastboot_publish("kernel", "lk");
4594 fastboot_publish("serialno", sn_buf);
4595
4596 /*
4597 * partition info is supported only for emmc partitions
4598 * Calling this for NAND prints some error messages which
4599 * is harmless but misleading. Avoid calling this for NAND
4600 * devices.
4601 */
4602 if (target_is_emmc_boot())
Mayank Grover7b8a8f82017-10-27 13:07:59 +05304603 publish_getvar_partition_info(part_info, partition_get_partition_count());
Amol Jadi5edf3552013-07-23 14:15:34 -07004604
Mayank Grover351a75e2017-05-30 20:06:08 +05304605 if (partition_multislot_is_supported())
4606 publish_getvar_multislot_vars();
4607
Amol Jadi5edf3552013-07-23 14:15:34 -07004608 /* Max download size supported */
Ameya Thakur11cf1a62013-08-05 12:44:48 -07004609 snprintf(max_download_size, MAX_RSP_SIZE, "\t0x%x",
4610 target_get_max_flash_size());
Amol Jadi5edf3552013-07-23 14:15:34 -07004611 fastboot_publish("max-download-size", (const char *) max_download_size);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07004612 /* Is the charger screen check enabled */
4613 snprintf(charger_screen_enabled, MAX_RSP_SIZE, "%d",
4614 device.charger_screen_enabled);
4615 fastboot_publish("charger-screen-enabled",
4616 (const char *) charger_screen_enabled);
lijuanga25d8bb2015-09-16 13:11:52 +08004617 fastboot_publish("off-mode-charge", (const char *) charger_screen_enabled);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05304618 snprintf(panel_display_mode, MAX_RSP_SIZE, "%s",
4619 device.display_panel);
4620 fastboot_publish("display-panel",
4621 (const char *) panel_display_mode);
Mayank Grover7b8a8f82017-10-27 13:07:59 +05304622#if PRODUCT_IOT
4623 get_bootloader_version_iot(&bootloader_version_string);
4624 fastboot_publish("version-bootloader", (const char *) bootloader_version_string);
4625
4626 /* Version baseband is n/a for apq iot devices */
4627 fastboot_publish("version-baseband", "N/A");
4628
4629 /* IOT targets support only mmc target */
4630 snprintf(block_size_string, MAX_RSP_SIZE, "0x%x", mmc_get_device_blocksize());
4631 fastboot_publish("erase-block-size", (const char *) block_size_string);
4632 fastboot_publish("logical-block-size", (const char *) block_size_string);
4633#else
Channagoud Kadabiaafbbfc2015-03-04 17:52:56 -08004634 fastboot_publish("version-bootloader", (const char *) device.bootloader_version);
4635 fastboot_publish("version-baseband", (const char *) device.radio_version);
Mayank Grover7b8a8f82017-10-27 13:07:59 +05304636#endif
lijuangf16461c2015-08-03 17:09:34 +08004637 fastboot_publish("secure", is_secure_boot_enable()? "yes":"no");
Mayank Grover7b8a8f82017-10-27 13:07:59 +05304638 fastboot_publish("unlocked", device.is_unlocked ? "yes":"no");
lijuangf16461c2015-08-03 17:09:34 +08004639 smem_get_hw_platform_name((unsigned char *) hw_platform_buf, sizeof(hw_platform_buf));
4640 snprintf(get_variant, MAX_RSP_SIZE, "%s %s", hw_platform_buf,
4641 target_is_emmc_boot()? "eMMC":"UFS");
4642 fastboot_publish("variant", (const char *) get_variant);
lijuang65c5a822015-08-29 16:35:36 +08004643#if CHECK_BAT_VOLTAGE
lijuang102dfa92015-10-09 18:31:03 +08004644 update_battery_status();
lijuang65c5a822015-08-29 16:35:36 +08004645 fastboot_publish("battery-voltage", (const char *) battery_voltage);
lijuang102dfa92015-10-09 18:31:03 +08004646 fastboot_publish("battery-soc-ok", (const char *) battery_soc_ok);
lijuang65c5a822015-08-29 16:35:36 +08004647#endif
Amol Jadi5edf3552013-07-23 14:15:34 -07004648}
4649
Brian Swetland9c4c0752009-01-25 16:23:50 -08004650void aboot_init(const struct app_descriptor *app)
4651{
Shashank Mittal4f99a882010-02-01 13:58:50 -08004652 unsigned reboot_mode = 0;
Mayank Grover351a75e2017-05-30 20:06:08 +05304653 int boot_err_type = 0;
4654 int boot_slot = INVALID;
Chandan Uddarajubedca152010-06-02 23:05:15 -07004655
lijuang39831732016-01-08 17:49:02 +08004656 /* Initialise wdog to catch early lk crashes */
4657#if WDOG_SUPPORT
4658 msm_wdog_init();
4659#endif
4660
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07004661 /* Setup page size information for nv storage */
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004662 if (target_is_emmc_boot())
4663 {
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07004664 page_size = mmc_page_size();
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004665 page_mask = page_size - 1;
Gaurav Nebhwani4d2389c2016-03-17 21:10:05 +05304666 mmc_blocksize = mmc_get_device_blocksize();
4667 mmc_blocksize_mask = mmc_blocksize - 1;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004668 }
4669 else
4670 {
4671 page_size = flash_page_size();
4672 page_mask = page_size - 1;
4673 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07004674 ASSERT((MEMBASE + MEMSIZE) > MEMBASE);
4675
Ameya Thakur11cf1a62013-08-05 12:44:48 -07004676 read_device_info(&device);
vijay kumarc65876c2015-04-24 13:29:16 +05304677 read_allow_oem_unlock(&device);
Shashank Mittal162244e2011-08-08 19:01:25 -07004678
Mayank Grover351a75e2017-05-30 20:06:08 +05304679 /* Detect multi-slot support */
4680 if (partition_multislot_is_supported())
4681 {
4682 boot_slot = partition_find_active_slot();
4683 if (boot_slot == INVALID)
4684 {
4685 boot_into_fastboot = true;
4686 dprintf(INFO, "Active Slot: (INVALID)\n");
4687 }
4688 else
4689 {
4690 /* Setting the state of system to boot active slot */
4691 partition_mark_active_slot(boot_slot);
4692 dprintf(INFO, "Active Slot: (%s)\n", SUFFIX_SLOT(boot_slot));
4693 }
4694 }
4695
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08004696 /* Display splash screen if enabled */
4697#if DISPLAY_SPLASH_SCREEN
lijuang99c02d82015-02-13 19:04:34 +08004698#if NO_ALARM_DISPLAY
4699 if (!check_alarm_boot()) {
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08004700#endif
lijuang99c02d82015-02-13 19:04:34 +08004701 dprintf(SPEW, "Display Init: Start\n");
Ajay Singh Parmara7865a82015-04-16 21:27:52 -07004702#if DISPLAY_HDMI_PRIMARY
4703 if (!strlen(device.display_panel))
4704 strlcpy(device.display_panel, DISPLAY_PANEL_HDMI,
4705 sizeof(device.display_panel));
4706#endif
Channagoud Kadabi90869ce2015-04-27 11:15:14 -07004707#if ENABLE_WBC
Channagoud Kadabid801ac72015-08-26 11:21:51 -07004708 /* Wait if the display shutdown is in progress */
4709 while(pm_app_display_shutdown_in_prgs());
Channagoud Kadabi90869ce2015-04-27 11:15:14 -07004710 if (!pm_appsbl_display_init_done())
4711 target_display_init(device.display_panel);
4712 else
4713 display_image_on_screen();
4714#else
lijuang99c02d82015-02-13 19:04:34 +08004715 target_display_init(device.display_panel);
Channagoud Kadabi90869ce2015-04-27 11:15:14 -07004716#endif
lijuang99c02d82015-02-13 19:04:34 +08004717 dprintf(SPEW, "Display Init: Done\n");
4718#if NO_ALARM_DISPLAY
4719 }
4720#endif
4721#endif
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08004722
Greg Griscod6250552011-06-29 14:40:23 -07004723 target_serialno((unsigned char *) sn_buf);
Ajay Dudanib06c05f2011-05-12 14:46:10 -07004724 dprintf(SPEW,"serial number: %s\n",sn_buf);
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -08004725
Dhaval Patel223ec952013-07-18 14:49:44 -07004726 memset(display_panel_buf, '\0', MAX_PANEL_BUF_SIZE);
4727
Matthew Qindefd5562014-07-11 18:02:40 +08004728 /*
4729 * Check power off reason if user force reset,
4730 * if yes phone will do normal boot.
4731 */
4732 if (is_user_force_reset())
4733 goto normal_boot;
4734
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07004735 /* Check if we should do something other than booting up */
Ameya Thakur0b9c2442013-05-10 13:22:56 -07004736 if (keys_get_state(KEY_VOLUMEUP) && keys_get_state(KEY_VOLUMEDOWN))
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07004737 {
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03004738 dprintf(ALWAYS,"dload mode key sequence detected\n");
lijuang395b5e62015-11-19 17:39:44 +08004739 reboot_device(EMERGENCY_DLOAD);
4740 dprintf(CRITICAL,"Failed to reboot into dload mode\n");
4741
Ameya Thakur0b9c2442013-05-10 13:22:56 -07004742 boot_into_fastboot = true;
4743 }
4744 if (!boot_into_fastboot)
4745 {
4746 if (keys_get_state(KEY_HOME) || keys_get_state(KEY_VOLUMEUP))
4747 boot_into_recovery = 1;
4748 if (!boot_into_recovery &&
4749 (keys_get_state(KEY_BACK) || keys_get_state(KEY_VOLUMEDOWN)))
Pavel Nedev5d91d412013-04-29 11:34:24 +03004750 boot_into_fastboot = true;
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07004751 }
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004752 #if NO_KEYPAD_DRIVER
Kinson Chik0b1c8162011-08-31 16:31:57 -07004753 if (fastboot_trigger())
Pavel Nedev5d91d412013-04-29 11:34:24 +03004754 boot_into_fastboot = true;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004755 #endif
Chandan Uddarajubedca152010-06-02 23:05:15 -07004756
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07004757#if USE_PON_REBOOT_REG
4758 reboot_mode = check_hard_reboot_mode();
4759#else
Ajay Dudani77421292010-10-27 19:34:06 -07004760 reboot_mode = check_reboot_mode();
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07004761#endif
4762 if (reboot_mode == RECOVERY_MODE)
4763 {
Ajay Dudani77421292010-10-27 19:34:06 -07004764 boot_into_recovery = 1;
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07004765 }
4766 else if(reboot_mode == FASTBOOT_MODE)
4767 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03004768 boot_into_fastboot = true;
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07004769 }
4770 else if(reboot_mode == ALARM_BOOT)
4771 {
Matthew Qind886f3c2014-01-17 16:52:01 +08004772 boot_reason_alarm = true;
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07004773 }
Parth Dixit207573a2015-10-27 17:26:21 +05304774#if VERIFIED_BOOT
Mayank Grover912eaa62017-10-26 12:08:53 +05304775 else if (VB_M <= target_get_vb_version())
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07004776 {
Mayank Grover889be1b2017-09-12 20:12:23 +05304777 if (reboot_mode == DM_VERITY_ENFORCING)
4778 {
4779 device.verity_mode = 1;
4780 write_device_info(&device);
4781 }
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07004782#if ENABLE_VB_ATTEST
Mayank Grover889be1b2017-09-12 20:12:23 +05304783 else if (reboot_mode == DM_VERITY_EIO)
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07004784#else
Mayank Grover889be1b2017-09-12 20:12:23 +05304785 else if (reboot_mode == DM_VERITY_LOGGING)
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07004786#endif
Mayank Grover889be1b2017-09-12 20:12:23 +05304787 {
4788 device.verity_mode = 0;
4789 write_device_info(&device);
4790 }
4791 else if (reboot_mode == DM_VERITY_KEYSCLEAR)
4792 {
4793 if(send_delete_keys_to_tz())
4794 ASSERT(0);
4795 }
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07004796 }
Parth Dixitddbc7352015-10-18 03:13:31 +05304797#endif
Ajay Dudani77421292010-10-27 19:34:06 -07004798
Matthew Qindefd5562014-07-11 18:02:40 +08004799normal_boot:
Pavel Nedev5d91d412013-04-29 11:34:24 +03004800 if (!boot_into_fastboot)
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004801 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03004802 if (target_is_emmc_boot())
Shashank Mittala0032282011-08-26 14:50:11 -07004803 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03004804 if(emmc_recovery_init())
4805 dprintf(ALWAYS,"error in emmc_recovery_init\n");
4806 if(target_use_signed_kernel())
Shashank Mittala0032282011-08-26 14:50:11 -07004807 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03004808 if((device.is_unlocked) || (device.is_tampered))
4809 {
4810 #ifdef TZ_TAMPER_FUSE
4811 set_tamper_fuse_cmd();
4812 #endif
4813 #if USE_PCOM_SECBOOT
4814 set_tamper_flag(device.is_tampered);
4815 #endif
4816 }
Shashank Mittala0032282011-08-26 14:50:11 -07004817 }
Amit Blay6281ebc2015-01-11 14:44:08 +02004818
Mayank Grover351a75e2017-05-30 20:06:08 +05304819retry_boot:
4820 /* Trying to boot active partition */
4821 if (partition_multislot_is_supported())
4822 {
4823 boot_slot = partition_find_boot_slot();
4824 partition_mark_active_slot(boot_slot);
4825 if (boot_slot == INVALID)
4826 goto fastboot;
4827 }
4828
4829 boot_err_type = boot_linux_from_mmc();
4830 switch (boot_err_type)
4831 {
4832 case ERR_INVALID_PAGE_SIZE:
4833 case ERR_DT_PARSE:
4834 case ERR_ABOOT_ADDR_OVERLAP:
Mayank Grover10cabbe2017-10-30 19:11:05 +05304835 case ERR_INVALID_BOOT_MAGIC:
Mayank Grover351a75e2017-05-30 20:06:08 +05304836 if(partition_multislot_is_supported())
Mayank Grover10cabbe2017-10-30 19:11:05 +05304837 {
4838 /*
4839 * Deactivate current slot, as it failed to
4840 * boot, and retry next slot.
4841 */
4842 partition_deactivate_slot(boot_slot);
Mayank Grover351a75e2017-05-30 20:06:08 +05304843 goto retry_boot;
Mayank Grover10cabbe2017-10-30 19:11:05 +05304844 }
Mayank Grover351a75e2017-05-30 20:06:08 +05304845 else
4846 break;
Mayank Grover351a75e2017-05-30 20:06:08 +05304847 default:
4848 break;
4849 /* going to fastboot menu */
4850 }
Shashank Mittala0032282011-08-26 14:50:11 -07004851 }
Pavel Nedev5d91d412013-04-29 11:34:24 +03004852 else
4853 {
4854 recovery_init();
4855 #if USE_PCOM_SECBOOT
4856 if((device.is_unlocked) || (device.is_tampered))
4857 set_tamper_flag(device.is_tampered);
4858 #endif
4859 boot_linux_from_flash();
4860 }
4861 dprintf(CRITICAL, "ERROR: Could not do normal boot. Reverting "
4862 "to fastboot mode.\n");
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004863 }
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07004864
Mayank Grover351a75e2017-05-30 20:06:08 +05304865fastboot:
Amol Jadi5edf3552013-07-23 14:15:34 -07004866 /* We are here means regular boot did not happen. Start fastboot. */
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07004867
Amol Jadi5edf3552013-07-23 14:15:34 -07004868 /* register aboot specific fastboot commands */
4869 aboot_fastboot_register_commands();
Amol Jadi57abe4c2011-05-24 15:47:27 -07004870
Amol Jadi5edf3552013-07-23 14:15:34 -07004871 /* dump partition table for debug info */
Kinson Chikf1a43512011-07-14 11:28:39 -07004872 partition_dump();
Amol Jadi5edf3552013-07-23 14:15:34 -07004873
4874 /* initialize and start fastboot */
4875 fastboot_init(target_get_scratch_address(), target_get_max_flash_size());
lijuang4ece1e72015-08-14 21:02:36 +08004876#if FBCON_DISPLAY_MSG
lijuang9a7d3b92015-11-30 14:41:24 +08004877 display_fastboot_menu();
lijuang4ece1e72015-08-14 21:02:36 +08004878#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08004879}
4880
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07004881uint32_t get_page_size()
4882{
4883 return page_size;
4884}
4885
Amir Samuelov57a6fa22013-06-05 16:36:43 +03004886/*
4887 * Calculated and save hash (SHA256) for non-signed boot image.
4888 *
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07004889 * @param image_addr - Boot image address
4890 * @param image_size - Size of the boot image
Amir Samuelov57a6fa22013-06-05 16:36:43 +03004891 *
4892 * @return int - 0 on success, negative value on failure.
4893 */
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07004894static int aboot_save_boot_hash_mmc(uint32_t image_addr, uint32_t image_size)
Amir Samuelov57a6fa22013-06-05 16:36:43 +03004895{
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07004896 unsigned int digest[8];
4897#if IMAGE_VERIF_ALGO_SHA1
4898 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA1;
4899#else
4900 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA256;
4901#endif
Amir Samuelov57a6fa22013-06-05 16:36:43 +03004902
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07004903 target_crypto_init_params();
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -08004904 hash_find((unsigned char *) image_addr, image_size, (unsigned char *)&digest, auth_algo);
Amir Samuelov57a6fa22013-06-05 16:36:43 +03004905
4906 save_kernel_hash_cmd(digest);
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07004907 dprintf(INFO, "aboot_save_boot_hash_mmc: imagesize_actual size %d bytes.\n", (int) image_size);
Amir Samuelov57a6fa22013-06-05 16:36:43 +03004908
4909 return 0;
4910}
4911
Mayank Grover351a75e2017-05-30 20:06:08 +05304912
Brian Swetland9c4c0752009-01-25 16:23:50 -08004913APP_START(aboot)
4914 .init = aboot_init,
4915APP_END