blob: 85f5b2940f9fa7c65df639da534980081d282260 [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";
Mayank Grover3804be72017-06-22 11:59:23 +0530191static const char *sys_path_cmdline = " rootwait ro init=/init";
Sourabh Banerjee386b1322018-02-27 09:37:28 +0530192
193#if VERITY_LE
194static const char *verity_dev = " root=/dev/dm-0";
195static const char *verity_system_part = " dm=\"system";
196static const char *verity_params = " none ro,0 1 android-verity /dev/mmcblk0p";
197#else
Mayank Grover3804be72017-06-22 11:59:23 +0530198static const char *sys_path = " root=/dev/mmcblk0p";
Sourabh Banerjee386b1322018-02-27 09:37:28 +0530199#endif
Ajay Dudanid04110c2011-01-17 23:55:07 -0800200
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700201#if VERIFIED_BOOT
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700202static const char *verity_mode = " androidboot.veritymode=";
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700203static const char *verified_state= " androidboot.verifiedbootstate=";
Parth Dixita5715a02015-10-29 12:25:10 +0530204static const char *keymaster_v1= " androidboot.keymaster=1";
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700205//indexed based on enum values, green is 0 by default
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700206
207struct verified_boot_verity_mode vbvm[] =
208{
Channagoud Kadabi86b0c112016-03-16 19:23:16 -0700209#if ENABLE_VB_ATTEST
210 {false, "eio"},
211#else
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700212 {false, "logging"},
Channagoud Kadabi86b0c112016-03-16 19:23:16 -0700213#endif
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700214 {true, "enforcing"},
215};
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700216struct verified_boot_state_name vbsn[] =
217{
218 {GREEN, "green"},
219 {ORANGE, "orange"},
220 {YELLOW,"yellow"},
221 {RED,"red" },
222};
223#endif
Channagoud Kadabi86b0c112016-03-16 19:23:16 -0700224/*As per spec delay wait time before shutdown in Red state*/
225#define DELAY_WAIT 30000
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700226static unsigned page_size = 0;
227static unsigned page_mask = 0;
Gaurav Nebhwani4d2389c2016-03-17 21:10:05 +0530228static unsigned mmc_blocksize = 0;
229static unsigned mmc_blocksize_mask = 0;
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700230static char ffbm_mode_string[FFBM_MODE_BUF_SIZE];
231static bool boot_into_ffbm;
vijay kumar870515d2015-08-31 16:37:24 +0530232static char *target_boot_params = NULL;
Matthew Qind886f3c2014-01-17 16:52:01 +0800233static bool boot_reason_alarm;
Channagoud Kadabi80a182b2015-03-11 17:04:23 -0700234static bool devinfo_present = true;
Channagoud Kadabi736c4962015-08-21 11:56:52 -0700235bool boot_into_fastboot = false;
Parth Dixit4097b622016-03-15 14:42:27 +0530236static uint32_t dt_size = 0;
Monika Singh292b3e92018-03-17 22:40:23 +0530237static char *vbcmdline;
238static bootinfo info = {0};
Shashank Mittalcd98d472011-08-02 14:29:24 -0700239/* Assuming unauthorized kernel image by default */
240static int auth_kernel_img = 0;
Monika Singh292b3e92018-03-17 22:40:23 +0530241static device_info device = {DEVICE_MAGIC,0,0,0,0,{0},{0},{0},1,{0},0,{0}};
242
vijay kumarc65876c2015-04-24 13:29:16 +0530243static bool is_allow_unlock = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -0700244
vijay kumarca2e6812015-07-08 20:28:25 +0530245static char frp_ptns[2][8] = {"config","frp"};
246
lijuang511a2b52015-08-14 20:50:51 +0800247static const char *critical_flash_allowed_ptn[] = {
248 "aboot",
249 "rpm",
250 "tz",
251 "sbl",
252 "sdi",
253 "sbl1",
254 "xbl",
255 "hyp",
256 "pmic",
257 "bootloader",
258 "devinfo",
259 "partition"};
260
Dima Zavin42168f22009-01-30 11:52:22 -0800261struct atag_ptbl_entry
262{
263 char name[16];
264 unsigned offset;
265 unsigned size;
266 unsigned flags;
267};
268
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700269/*
270 * Partition info, required to be published
271 * for fastboot
272 */
273struct getvar_partition_info {
Mayank Grover7b8a8f82017-10-27 13:07:59 +0530274 char part_name[MAX_GPT_NAME_SIZE]; /* Partition name */
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700275 char getvar_size[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for size */
276 char getvar_type[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for type */
277 char size_response[MAX_RSP_SIZE]; /* fastboot response for size */
278 char type_response[MAX_RSP_SIZE]; /* fastboot response for type */
279};
280
281/*
Mayank Grover7b8a8f82017-10-27 13:07:59 +0530282 * Update the part_type_known for known paritions types.
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700283 */
Mayank Groverd38fe012018-03-13 15:33:16 +0530284#define RAW_STR "raw"
Mayank Grover52cd10a2018-03-15 12:57:54 +0530285#define EXT_STR "ext4"
286#define F2FS_STR "f2fs"
287
288#define FS_SUPERBLOCK_OFFSET 0x400
289#define EXT_MAGIC 0xEF53
290#define EXT_MAGIC_OFFSET_SB 0x38
291#define F2FS_MAGIC 0xF2F52010 // F2FS Magic Number
292#define F2FS_MAGIC_OFFSET_SB 0x0
293
294typedef enum fs_signature_type {
295 EXT_FS_SIGNATURE = 1,
296 EXT_F2FS_SIGNATURE = 2,
297 NO_FS = -1
298} fs_signature_type;
299
Mayank Grover7b8a8f82017-10-27 13:07:59 +0530300struct getvar_partition_info part_info[NUM_PARTITIONS];
301struct getvar_partition_info part_type_known[] =
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700302{
Mayank Grover49920212018-02-09 18:15:55 +0530303 { "system" , "partition-size:", "partition-type:", "", "ext4" },
304 { "userdata" , "partition-size:", "partition-type:", "", "ext4" },
305 { "cache" , "partition-size:", "partition-type:", "", "ext4" },
306 { "recoveryfs" , "partition-size:", "partition-type:", "", "ext4" },
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700307};
308
309char max_download_size[MAX_RSP_SIZE];
Ameya Thakur11cf1a62013-08-05 12:44:48 -0700310char charger_screen_enabled[MAX_RSP_SIZE];
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800311char sn_buf[13];
Dhaval Patel223ec952013-07-18 14:49:44 -0700312char display_panel_buf[MAX_PANEL_BUF_SIZE];
Unnati Gandhi62c8ab82014-01-24 11:01:01 +0530313char panel_display_mode[MAX_RSP_SIZE];
Mayank Grover7b8a8f82017-10-27 13:07:59 +0530314#if PRODUCT_IOT
315char block_size_string[MAX_RSP_SIZE];
316
317/* For IOT we are using custom version */
318#define PRODUCT_IOT_VERSION "IOT001"
319char bootloader_version_string[MAX_RSP_SIZE];
320#endif
lijuang102dfa92015-10-09 18:31:03 +0800321
322#if CHECK_BAT_VOLTAGE
lijuang65c5a822015-08-29 16:35:36 +0800323char battery_voltage[MAX_RSP_SIZE];
lijuang102dfa92015-10-09 18:31:03 +0800324char battery_soc_ok [MAX_RSP_SIZE];
325#endif
326
lijuangf16461c2015-08-03 17:09:34 +0800327char get_variant[MAX_RSP_SIZE];
Greg Griscod6250552011-06-29 14:40:23 -0700328
Greg Griscod2471ef2011-07-14 13:00:42 -0700329extern int emmc_recovery_init(void);
330
Kinson Chik0b1c8162011-08-31 16:31:57 -0700331#if NO_KEYPAD_DRIVER
332extern int fastboot_trigger(void);
333#endif
Greg Griscod2471ef2011-07-14 13:00:42 -0700334
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800335static void update_ker_tags_rdisk_addr(struct boot_img_hdr *hdr, bool is_arm64)
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700336{
337 /* overwrite the destination of specified for the project */
Channagoud Kadabi7042fa32013-04-26 16:44:14 -0700338#ifdef ABOOT_IGNORE_BOOT_HEADER_ADDRS
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800339 if (is_arm64)
340 hdr->kernel_addr = ABOOT_FORCE_KERNEL64_ADDR;
341 else
342 hdr->kernel_addr = ABOOT_FORCE_KERNEL_ADDR;
Channagoud Kadabi7042fa32013-04-26 16:44:14 -0700343 hdr->ramdisk_addr = ABOOT_FORCE_RAMDISK_ADDR;
344 hdr->tags_addr = ABOOT_FORCE_TAGS_ADDR;
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700345#endif
346}
347
Dima Zavin42168f22009-01-30 11:52:22 -0800348static void ptentry_to_tag(unsigned **ptr, struct ptentry *ptn)
349{
350 struct atag_ptbl_entry atag_ptn;
351
352 memcpy(atag_ptn.name, ptn->name, 16);
353 atag_ptn.name[15] = '\0';
354 atag_ptn.offset = ptn->start;
355 atag_ptn.size = ptn->length;
356 atag_ptn.flags = ptn->flags;
357 memcpy(*ptr, &atag_ptn, sizeof(struct atag_ptbl_entry));
358 *ptr += sizeof(struct atag_ptbl_entry) / sizeof(unsigned);
359}
Brian Swetland9c4c0752009-01-25 16:23:50 -0800360
lijuang102dfa92015-10-09 18:31:03 +0800361#if CHECK_BAT_VOLTAGE
362void update_battery_status(void)
363{
364 snprintf(battery_voltage,MAX_RSP_SIZE, "%d",target_get_battery_voltage());
365 snprintf(battery_soc_ok ,MAX_RSP_SIZE, "%s",target_battery_soc_ok()? "yes":"no");
366}
367#endif
368
Neeti Desaie245d492012-06-01 12:52:13 -0700369unsigned char *update_cmdline(const char * cmdline)
Brian Swetland9c4c0752009-01-25 16:23:50 -0800370{
David Ng183a7422009-12-07 14:55:21 -0800371 int cmdline_len = 0;
372 int have_cmdline = 0;
Amol Jadi168b7712012-03-06 16:15:00 -0800373 unsigned char *cmdline_final = NULL;
Neeti Desaie245d492012-06-01 12:52:13 -0700374 int pause_at_bootup = 0;
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800375 bool warm_boot = false;
Pavel Nedev5614d222013-06-17 18:01:02 +0300376 bool gpt_exists = partition_gpt_exists();
Joonwoo Park61112782013-10-02 19:50:39 -0700377 int have_target_boot_params = 0;
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700378 char *boot_dev_buf = NULL;
Monika Singh292b3e92018-03-17 22:40:23 +0530379#ifdef MDTP_SUPPORT
Mayank Grover351a75e2017-05-30 20:06:08 +0530380 bool is_mdtp_activated = 0;
Monika Singh292b3e92018-03-17 22:40:23 +0530381#endif
Mayank Grover351a75e2017-05-30 20:06:08 +0530382 int current_active_slot = INVALID;
Mayank Grover3804be72017-06-22 11:59:23 +0530383 int system_ptn_index = -1;
384 unsigned int lun = 0;
385 char lun_char_base = 'a';
Sourabh Banerjee386b1322018-02-27 09:37:28 +0530386#if VERITY_LE
387 int syspath_buflen = strlen(verity_dev)
388 + strlen(verity_system_part) + (sizeof(char) * 2) + 2
389 + strlen(verity_params) + sizeof(int) + 2;
390#else
391 int syspath_buflen = strlen(sys_path) + sizeof(int) + 2; /*allocate buflen for largest possible string*/
392#endif
Mayank Grover3804be72017-06-22 11:59:23 +0530393 char syspath_buf[syspath_buflen];
Mayank Grover889be1b2017-09-12 20:12:23 +0530394#if VERIFIED_BOOT
395 uint32_t boot_state = RED;
396#endif
Hareesh Gauthamabf1e1e2017-04-12 18:09:24 +0530397
398#if USE_LE_SYSTEMD
399 is_systemd_present=true;
400#endif
401
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700402#if VERIFIED_BOOT
Mayank Grover912eaa62017-10-26 12:08:53 +0530403 if (VB_M <= target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +0530404 {
405 boot_state = boot_verify_get_state();
406 }
Parth Dixitddbc7352015-10-18 03:13:31 +0530407#endif
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700408
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200409#ifdef MDTP_SUPPORT
410 mdtp_activated(&is_mdtp_activated);
411#endif /* MDTP_SUPPORT */
Dima Zavin42168f22009-01-30 11:52:22 -0800412
Brian Swetland9c4c0752009-01-25 16:23:50 -0800413 if (cmdline && cmdline[0]) {
David Ng183a7422009-12-07 14:55:21 -0800414 cmdline_len = strlen(cmdline);
415 have_cmdline = 1;
416 }
417 if (target_is_emmc_boot()) {
418 cmdline_len += strlen(emmc_cmdline);
Sridhar Parasuram7bd4aaf2015-02-12 11:14:38 -0800419#if USE_BOOTDEV_CMDLINE
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700420 boot_dev_buf = (char *) malloc(sizeof(char) * BOOT_DEV_MAX_LEN);
421 ASSERT(boot_dev_buf);
422 platform_boot_dev_cmdline(boot_dev_buf);
423 cmdline_len += strlen(boot_dev_buf);
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700424#endif
David Ng183a7422009-12-07 14:55:21 -0800425 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800426
427 cmdline_len += strlen(usb_sn_cmdline);
428 cmdline_len += strlen(sn_buf);
429
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700430#if VERIFIED_BOOT
Mayank Grover912eaa62017-10-26 12:08:53 +0530431 if (VB_M <= target_get_vb_version())
Sridhar Parasuram7d8bb9a2015-09-23 18:21:19 -0700432 {
Mayank Grover889be1b2017-09-12 20:12:23 +0530433 cmdline_len += strlen(verified_state) + strlen(vbsn[boot_state].name);
434 if ((device.verity_mode != 0 ) && (device.verity_mode != 1))
435 {
436 dprintf(CRITICAL, "Devinfo paritition possibly corrupted!!!. Please erase devinfo partition to continue booting\n");
437 ASSERT(0);
438 }
439 cmdline_len += strlen(verity_mode) + strlen(vbvm[device.verity_mode].name);
440 cmdline_len += strlen(keymaster_v1);
Sridhar Parasuram7d8bb9a2015-09-23 18:21:19 -0700441 }
Parth Dixitddbc7352015-10-18 03:13:31 +0530442#endif
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700443
Monika Singh292b3e92018-03-17 22:40:23 +0530444
445 if (vbcmdline != NULL) {
446 dprintf(DEBUG, "UpdateCmdLine vbcmdline present len %d\n",
447 strlen(vbcmdline));
448 cmdline_len += strlen(vbcmdline);
449 }
450
Pavel Nedev5614d222013-06-17 18:01:02 +0300451 if (boot_into_recovery && gpt_exists)
452 cmdline_len += strlen(secondary_gpt_enable);
453
Monika Singh292b3e92018-03-17 22:40:23 +0530454#ifdef MDTP_SUPPORT
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200455 if(is_mdtp_activated)
456 cmdline_len += strlen(mdtp_activated_flag);
Monika Singh292b3e92018-03-17 22:40:23 +0530457#endif
Pavel Nedev328ac822013-04-05 15:25:11 +0300458 if (boot_into_ffbm) {
459 cmdline_len += strlen(androidboot_mode);
Hareesh Gauthamabf1e1e2017-04-12 18:09:24 +0530460
461 if(is_systemd_present)
462 cmdline_len += strlen(systemd_ffbm_mode);
463
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700464 cmdline_len += strlen(ffbm_mode_string);
Pavel Nedev898298c2013-02-27 12:36:09 -0800465 /* reduce kernel console messages to speed-up boot */
466 cmdline_len += strlen(loglevel);
Matthew Qind886f3c2014-01-17 16:52:01 +0800467 } else if (boot_reason_alarm) {
468 cmdline_len += strlen(alarmboot_cmdline);
Zhenhua Huang431dafa2015-06-30 16:13:37 +0800469 } else if ((target_build_variant_user() || device.charger_screen_enabled)
470 && target_pause_for_battery_charge()) {
David Ngf773dde2010-07-26 19:55:08 -0700471 pause_at_bootup = 1;
472 cmdline_len += strlen(battchg_pause);
473 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800474
Shashank Mittalcd98d472011-08-02 14:29:24 -0700475 if(target_use_signed_kernel() && auth_kernel_img) {
476 cmdline_len += strlen(auth_kernel);
477 }
478
Joonwoo Park61112782013-10-02 19:50:39 -0700479 if (get_target_boot_params(cmdline, boot_into_recovery ? "recoveryfs" :
480 "system",
vijay kumar870515d2015-08-31 16:37:24 +0530481 &target_boot_params) == 0) {
Joonwoo Park61112782013-10-02 19:50:39 -0700482 have_target_boot_params = 1;
483 cmdline_len += strlen(target_boot_params);
484 }
485
Ajay Dudanid04110c2011-01-17 23:55:07 -0800486 /* Determine correct androidboot.baseband to use */
487 switch(target_baseband())
488 {
489 case BASEBAND_APQ:
490 cmdline_len += strlen(baseband_apq);
491 break;
492
493 case BASEBAND_MSM:
494 cmdline_len += strlen(baseband_msm);
495 break;
496
497 case BASEBAND_CSFB:
498 cmdline_len += strlen(baseband_csfb);
499 break;
500
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800501 case BASEBAND_SVLTE2A:
502 cmdline_len += strlen(baseband_svlte2a);
Ajay Dudanid04110c2011-01-17 23:55:07 -0800503 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700504
505 case BASEBAND_MDM:
506 cmdline_len += strlen(baseband_mdm);
507 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700508
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800509 case BASEBAND_MDM2:
510 cmdline_len += strlen(baseband_mdm2);
511 break;
512
Amol Jadi5c61a952012-05-04 17:05:35 -0700513 case BASEBAND_SGLTE:
514 cmdline_len += strlen(baseband_sglte);
515 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530516
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800517 case BASEBAND_SGLTE2:
518 cmdline_len += strlen(baseband_sglte2);
519 break;
520
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530521 case BASEBAND_DSDA:
522 cmdline_len += strlen(baseband_dsda);
523 break;
Amol Jadi2a15a272013-01-22 12:03:36 -0800524
525 case BASEBAND_DSDA2:
526 cmdline_len += strlen(baseband_dsda2);
527 break;
Vijay Kumar Pendotib228cfc2016-06-13 20:15:23 +0530528 case BASEBAND_APQ_NOWGR:
529 cmdline_len += strlen(baseband_apq_nowgr);
530 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800531 }
532
Vladislav Levenetz3cd45242017-04-05 08:58:07 +0300533#if ENABLE_DISPLAY
Lijuan Gao4a5b8322014-07-24 10:38:42 +0800534 if (cmdline) {
535 if ((strstr(cmdline, DISPLAY_DEFAULT_PREFIX) == NULL) &&
Padmanabhan Komandurubccbcdc2015-06-30 16:19:24 +0530536 target_display_panel_node(display_panel_buf,
537 MAX_PANEL_BUF_SIZE) &&
Lijuan Gao4a5b8322014-07-24 10:38:42 +0800538 strlen(display_panel_buf)) {
539 cmdline_len += strlen(display_panel_buf);
540 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700541 }
Vladislav Levenetz3cd45242017-04-05 08:58:07 +0300542#endif
Dhaval Patel223ec952013-07-18 14:49:44 -0700543
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800544 if (target_warm_boot()) {
545 warm_boot = true;
546 cmdline_len += strlen(warmboot_cmdline);
547 }
548
Mayank Grover351a75e2017-05-30 20:06:08 +0530549 if (partition_multislot_is_supported())
550 {
551 current_active_slot = partition_find_active_slot();
552 cmdline_len += (strlen(androidboot_slot_suffix)+
553 strlen(SUFFIX_SLOT(current_active_slot)));
554
Mayank Grover3804be72017-06-22 11:59:23 +0530555 system_ptn_index = partition_get_index("system");
556 if (platform_boot_dev_isemmc())
557 {
Sourabh Banerjee386b1322018-02-27 09:37:28 +0530558#if VERITY_LE
559 /*
560 Condition 4: Verity and A/B both enabled
561 Eventual command line looks like:
562 ... androidboot.slot_suffix=<slot_suffix> ... rootfstype=ext4 ...
563 ... root=/dev/dm-0 dm="system_<slot_suffix> none ro,0 1 android-verity /dev/mmcblk0p<NN>"
564 */
565 snprintf(syspath_buf, syspath_buflen, " %s %s%s %s%d\"",
566 verity_dev,
567 verity_system_part, suffix_slot[current_active_slot],
568 verity_params, system_ptn_index + 1);
569#else
570 /*
571 Condition 5: A/B enabled, but verity disabled
572 Eventual command line looks like:
573 ... androidboot.slot_suffix=<slot_suffix> ... rootfstype=ext4 ...
574 ... root=/dev/mmcblk0p<NN> ...
575 */
576 snprintf(syspath_buf, syspath_buflen, " %s%d",
577 sys_path, system_ptn_index + 1);
578#endif
Mayank Grover3804be72017-06-22 11:59:23 +0530579 }
580 else
581 {
582 lun = partition_get_lun(system_ptn_index);
583 snprintf(syspath_buf, syspath_buflen, " root=/dev/sd%c%d",
584 lun_char_base + lun,
585 partition_get_index_in_lun("system", lun));
586 }
Mayank Grover351a75e2017-05-30 20:06:08 +0530587
Mayank Grover3804be72017-06-22 11:59:23 +0530588 cmdline_len += strlen(sys_path_cmdline);
Monika Singh292b3e92018-03-17 22:40:23 +0530589#ifndef VERIFIED_BOOT_2
Mayank Grover3804be72017-06-22 11:59:23 +0530590 cmdline_len += strlen(syspath_buf);
Monika Singh292b3e92018-03-17 22:40:23 +0530591#endif
Mayank Grover351a75e2017-05-30 20:06:08 +0530592 if (!boot_into_recovery)
593 cmdline_len += strlen(skip_ramfs);
594 }
595
Channagoud Kadabi85c7ec32016-01-28 23:09:21 -0800596#if TARGET_CMDLINE_SUPPORT
597 char *target_cmdline_buf = malloc(TARGET_MAX_CMDLNBUF);
598 int target_cmd_line_len;
599 ASSERT(target_cmdline_buf);
600 target_cmd_line_len = target_update_cmdline(target_cmdline_buf);
601 cmdline_len += target_cmd_line_len;
602#endif
603
David Ng183a7422009-12-07 14:55:21 -0800604 if (cmdline_len > 0) {
605 const char *src;
Maria Yu52254c02014-07-04 16:14:54 +0800606 unsigned char *dst;
607
608 cmdline_final = (unsigned char*) malloc((cmdline_len + 4) & (~3));
609 ASSERT(cmdline_final != NULL);
vijay kumar287bb542015-09-29 13:01:52 +0530610 memset((void *)cmdline_final, 0, sizeof(*cmdline_final));
Maria Yu52254c02014-07-04 16:14:54 +0800611 dst = cmdline_final;
Neeti Desaie245d492012-06-01 12:52:13 -0700612
Amol Jadi168b7712012-03-06 16:15:00 -0800613 /* Save start ptr for debug print */
David Ng183a7422009-12-07 14:55:21 -0800614 if (have_cmdline) {
615 src = cmdline;
616 while ((*dst++ = *src++));
617 }
618 if (target_is_emmc_boot()) {
619 src = emmc_cmdline;
620 if (have_cmdline) --dst;
David Ngf773dde2010-07-26 19:55:08 -0700621 have_cmdline = 1;
622 while ((*dst++ = *src++));
Sridhar Parasuram7bd4aaf2015-02-12 11:14:38 -0800623#if USE_BOOTDEV_CMDLINE
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700624 src = boot_dev_buf;
625 if (have_cmdline) --dst;
626 while ((*dst++ = *src++));
627#endif
David Ngf773dde2010-07-26 19:55:08 -0700628 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800629
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700630#if VERIFIED_BOOT
Mayank Grover912eaa62017-10-26 12:08:53 +0530631 if (VB_M <= target_get_vb_version())
Sridhar Parasuram7d8bb9a2015-09-23 18:21:19 -0700632 {
Mayank Grover889be1b2017-09-12 20:12:23 +0530633 src = verified_state;
634 if(have_cmdline) --dst;
635 have_cmdline = 1;
636 while ((*dst++ = *src++));
637 src = vbsn[boot_state].name;
638 if(have_cmdline) --dst;
639 while ((*dst++ = *src++));
640
641 if ((device.verity_mode != 0 ) && (device.verity_mode != 1))
642 {
643 dprintf(CRITICAL, "Devinfo paritition possibly corrupted!!!. Please erase devinfo partition to continue booting\n");
644 ASSERT(0);
645 }
646 src = verity_mode;
647 if(have_cmdline) --dst;
648 while ((*dst++ = *src++));
649 src = vbvm[device.verity_mode].name;
650 if(have_cmdline) -- dst;
651 while ((*dst++ = *src++));
652 src = keymaster_v1;
653 if(have_cmdline) --dst;
654 while ((*dst++ = *src++));
Sridhar Parasuram7d8bb9a2015-09-23 18:21:19 -0700655 }
Parth Dixitddbc7352015-10-18 03:13:31 +0530656#endif
Monika Singh292b3e92018-03-17 22:40:23 +0530657
658 if (vbcmdline != NULL) {
659 src = vbcmdline;
660 if (have_cmdline) --dst;
661 while ((*dst++ = *src++));
662 }
663
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800664 src = usb_sn_cmdline;
665 if (have_cmdline) --dst;
666 have_cmdline = 1;
667 while ((*dst++ = *src++));
668 src = sn_buf;
669 if (have_cmdline) --dst;
670 have_cmdline = 1;
671 while ((*dst++ = *src++));
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800672 if (warm_boot) {
673 if (have_cmdline) --dst;
674 src = warmboot_cmdline;
675 while ((*dst++ = *src++));
676 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800677
Pavel Nedev5614d222013-06-17 18:01:02 +0300678 if (boot_into_recovery && gpt_exists) {
679 src = secondary_gpt_enable;
680 if (have_cmdline) --dst;
681 while ((*dst++ = *src++));
682 }
Monika Singh292b3e92018-03-17 22:40:23 +0530683#ifdef MDTP_SUPPORT
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200684 if (is_mdtp_activated) {
685 src = mdtp_activated_flag;
686 if (have_cmdline) --dst;
687 while ((*dst++ = *src++));
688 }
Monika Singh292b3e92018-03-17 22:40:23 +0530689#endif
Pavel Nedev328ac822013-04-05 15:25:11 +0300690 if (boot_into_ffbm) {
691 src = androidboot_mode;
692 if (have_cmdline) --dst;
693 while ((*dst++ = *src++));
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700694 src = ffbm_mode_string;
Pavel Nedev328ac822013-04-05 15:25:11 +0300695 if (have_cmdline) --dst;
696 while ((*dst++ = *src++));
Hareesh Gauthamabf1e1e2017-04-12 18:09:24 +0530697
698 if(is_systemd_present) {
699 src = systemd_ffbm_mode;
700 if (have_cmdline) --dst;
701 while ((*dst++ = *src++));
702 }
703
Pavel Nedev898298c2013-02-27 12:36:09 -0800704 src = loglevel;
705 if (have_cmdline) --dst;
706 while ((*dst++ = *src++));
Matthew Qind886f3c2014-01-17 16:52:01 +0800707 } else if (boot_reason_alarm) {
708 src = alarmboot_cmdline;
709 if (have_cmdline) --dst;
710 while ((*dst++ = *src++));
Pavel Nedev328ac822013-04-05 15:25:11 +0300711 } else if (pause_at_bootup) {
David Ngf773dde2010-07-26 19:55:08 -0700712 src = battchg_pause;
713 if (have_cmdline) --dst;
David Ng183a7422009-12-07 14:55:21 -0800714 while ((*dst++ = *src++));
715 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800716
Shashank Mittalcd98d472011-08-02 14:29:24 -0700717 if(target_use_signed_kernel() && auth_kernel_img) {
718 src = auth_kernel;
719 if (have_cmdline) --dst;
720 while ((*dst++ = *src++));
721 }
722
Ajay Dudanid04110c2011-01-17 23:55:07 -0800723 switch(target_baseband())
724 {
725 case BASEBAND_APQ:
726 src = baseband_apq;
727 if (have_cmdline) --dst;
728 while ((*dst++ = *src++));
729 break;
730
731 case BASEBAND_MSM:
732 src = baseband_msm;
733 if (have_cmdline) --dst;
734 while ((*dst++ = *src++));
735 break;
736
737 case BASEBAND_CSFB:
738 src = baseband_csfb;
739 if (have_cmdline) --dst;
740 while ((*dst++ = *src++));
741 break;
742
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800743 case BASEBAND_SVLTE2A:
744 src = baseband_svlte2a;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800745 if (have_cmdline) --dst;
746 while ((*dst++ = *src++));
747 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700748
749 case BASEBAND_MDM:
750 src = baseband_mdm;
751 if (have_cmdline) --dst;
752 while ((*dst++ = *src++));
753 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700754
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800755 case BASEBAND_MDM2:
756 src = baseband_mdm2;
757 if (have_cmdline) --dst;
758 while ((*dst++ = *src++));
759 break;
760
Amol Jadi5c61a952012-05-04 17:05:35 -0700761 case BASEBAND_SGLTE:
762 src = baseband_sglte;
763 if (have_cmdline) --dst;
764 while ((*dst++ = *src++));
765 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530766
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800767 case BASEBAND_SGLTE2:
768 src = baseband_sglte2;
769 if (have_cmdline) --dst;
770 while ((*dst++ = *src++));
771 break;
772
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530773 case BASEBAND_DSDA:
774 src = baseband_dsda;
775 if (have_cmdline) --dst;
776 while ((*dst++ = *src++));
777 break;
Amol Jadi2a15a272013-01-22 12:03:36 -0800778
779 case BASEBAND_DSDA2:
780 src = baseband_dsda2;
781 if (have_cmdline) --dst;
782 while ((*dst++ = *src++));
783 break;
Vijay Kumar Pendotib228cfc2016-06-13 20:15:23 +0530784 case BASEBAND_APQ_NOWGR:
785 src = baseband_apq_nowgr;
786 if (have_cmdline) --dst;
787 while ((*dst++ = *src++));
788 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800789 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700790
791 if (strlen(display_panel_buf)) {
Dhaval Patel223ec952013-07-18 14:49:44 -0700792 src = display_panel_buf;
793 if (have_cmdline) --dst;
794 while ((*dst++ = *src++));
795 }
Joonwoo Park61112782013-10-02 19:50:39 -0700796
797 if (have_target_boot_params) {
798 if (have_cmdline) --dst;
799 src = target_boot_params;
800 while ((*dst++ = *src++));
vijay kumar870515d2015-08-31 16:37:24 +0530801 free(target_boot_params);
Joonwoo Park61112782013-10-02 19:50:39 -0700802 }
Channagoud Kadabi85c7ec32016-01-28 23:09:21 -0800803
Mayank Grover351a75e2017-05-30 20:06:08 +0530804 if (partition_multislot_is_supported() && have_cmdline)
805 {
806 src = androidboot_slot_suffix;
807 --dst;
808 while ((*dst++ = *src++));
809 --dst;
810 src = SUFFIX_SLOT(current_active_slot);
811 while ((*dst++ = *src++));
812
813 if (!boot_into_recovery)
814 {
815 src = skip_ramfs;
816 --dst;
817 while ((*dst++ = *src++));
818 }
819
820 src = sys_path_cmdline;
821 --dst;
822 while ((*dst++ = *src++));
Mayank Grover3804be72017-06-22 11:59:23 +0530823
Monika Singh292b3e92018-03-17 22:40:23 +0530824#ifndef VERIFIED_BOOT_2
Mayank Grover3804be72017-06-22 11:59:23 +0530825 src = syspath_buf;
826 --dst;
827 while ((*dst++ = *src++));
Monika Singh292b3e92018-03-17 22:40:23 +0530828#endif
Mayank Grover351a75e2017-05-30 20:06:08 +0530829 }
830
Channagoud Kadabi85c7ec32016-01-28 23:09:21 -0800831#if TARGET_CMDLINE_SUPPORT
832 if (target_cmdline_buf && target_cmd_line_len)
833 {
834 if (have_cmdline) --dst;
835 src = target_cmdline_buf;
836 while((*dst++ = *src++));
837 free(target_cmdline_buf);
838 }
839#endif
Neeti Desaie245d492012-06-01 12:52:13 -0700840 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700841
842
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700843 if (boot_dev_buf)
844 free(boot_dev_buf);
845
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -0800846 if (cmdline_final)
847 dprintf(INFO, "cmdline: %s\n", cmdline_final);
848 else
849 dprintf(INFO, "cmdline is NULL\n");
Neeti Desaie245d492012-06-01 12:52:13 -0700850 return cmdline_final;
851}
852
853unsigned *atag_core(unsigned *ptr)
854{
855 /* CORE */
856 *ptr++ = 2;
857 *ptr++ = 0x54410001;
858
859 return ptr;
860
861}
862
863unsigned *atag_ramdisk(unsigned *ptr, void *ramdisk,
864 unsigned ramdisk_size)
865{
866 if (ramdisk_size) {
867 *ptr++ = 4;
868 *ptr++ = 0x54420005;
869 *ptr++ = (unsigned)ramdisk;
870 *ptr++ = ramdisk_size;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800871 }
872
Neeti Desaie245d492012-06-01 12:52:13 -0700873 return ptr;
874}
875
876unsigned *atag_ptable(unsigned **ptr_addr)
877{
878 int i;
879 struct ptable *ptable;
880
881 if ((ptable = flash_get_ptable()) && (ptable->count != 0)) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700882 *(*ptr_addr)++ = 2 + (ptable->count * (sizeof(struct atag_ptbl_entry) /
883 sizeof(unsigned)));
Neeti Desaie245d492012-06-01 12:52:13 -0700884 *(*ptr_addr)++ = 0x4d534d70;
885 for (i = 0; i < ptable->count; ++i)
886 ptentry_to_tag(ptr_addr, ptable_get(ptable, i));
887 }
888
889 return (*ptr_addr);
890}
891
892unsigned *atag_cmdline(unsigned *ptr, const char *cmdline)
893{
894 int cmdline_length = 0;
895 int n;
Neeti Desaie245d492012-06-01 12:52:13 -0700896 char *dest;
897
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800898 cmdline_length = strlen((const char*)cmdline);
Neeti Desaie245d492012-06-01 12:52:13 -0700899 n = (cmdline_length + 4) & (~3);
900
901 *ptr++ = (n / 4) + 2;
902 *ptr++ = 0x54410009;
903 dest = (char *) ptr;
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800904 while ((*dest++ = *cmdline++));
Neeti Desaie245d492012-06-01 12:52:13 -0700905 ptr += (n / 4);
906
907 return ptr;
908}
909
910unsigned *atag_end(unsigned *ptr)
911{
Brian Swetland9c4c0752009-01-25 16:23:50 -0800912 /* END */
913 *ptr++ = 0;
914 *ptr++ = 0;
915
Neeti Desaie245d492012-06-01 12:52:13 -0700916 return ptr;
917}
918
919void generate_atags(unsigned *ptr, const char *cmdline,
920 void *ramdisk, unsigned ramdisk_size)
921{
vijay kumar21a37452015-12-29 16:23:21 +0530922 unsigned *orig_ptr = ptr;
Neeti Desaie245d492012-06-01 12:52:13 -0700923 ptr = atag_core(ptr);
924 ptr = atag_ramdisk(ptr, ramdisk, ramdisk_size);
925 ptr = target_atag_mem(ptr);
926
927 /* Skip NAND partition ATAGS for eMMC boot */
928 if (!target_is_emmc_boot()){
929 ptr = atag_ptable(&ptr);
930 }
931
vijay kumar21a37452015-12-29 16:23:21 +0530932 /*
933 * Atags size filled till + cmdline size + 1 unsigned for 4-byte boundary + 4 unsigned
934 * for atag identifier in atag_cmdline and atag_end should be with in MAX_TAGS_SIZE bytes
935 */
Mayank Groverbc8a2ef2018-02-23 18:03:36 +0530936 if (!cmdline)
937 return;
938
vijay kumar21a37452015-12-29 16:23:21 +0530939 if (((ptr - orig_ptr) + strlen(cmdline) + 5 * sizeof(unsigned)) < MAX_TAGS_SIZE) {
940 ptr = atag_cmdline(ptr, cmdline);
941 ptr = atag_end(ptr);
942 }
943 else {
944 dprintf(CRITICAL,"Crossing ATAGs Max size allowed\n");
945 ASSERT(0);
946 }
Neeti Desaie245d492012-06-01 12:52:13 -0700947}
948
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700949typedef void entry_func_ptr(unsigned, unsigned, unsigned*);
Neeti Desaie245d492012-06-01 12:52:13 -0700950void boot_linux(void *kernel, unsigned *tags,
951 const char *cmdline, unsigned machtype,
952 void *ramdisk, unsigned ramdisk_size)
953{
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800954 unsigned char *final_cmdline;
Amol Jadib6be5c12012-11-14 13:39:51 -0800955#if DEVICE_TREE
Neeti Desai17379b82012-06-04 18:42:53 -0700956 int ret = 0;
Amol Jadib6be5c12012-11-14 13:39:51 -0800957#endif
958
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700959 void (*entry)(unsigned, unsigned, unsigned*) = (entry_func_ptr*)(PA((addr_t)kernel));
Deepa Dinamani25a9f762012-11-30 15:57:15 -0800960 uint32_t tags_phys = PA((addr_t)tags);
vijay kumar1a50a642015-11-16 12:41:15 +0530961 struct kernel64_hdr *kptr = ((struct kernel64_hdr*)(PA((addr_t)kernel)));
Deepa Dinamani25a9f762012-11-30 15:57:15 -0800962
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530963 ramdisk = (void *)PA((addr_t)ramdisk);
Neeti Desaie245d492012-06-01 12:52:13 -0700964
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800965 final_cmdline = update_cmdline((const char*)cmdline);
966
Neeti Desai17379b82012-06-04 18:42:53 -0700967#if DEVICE_TREE
Amol Jadib6be5c12012-11-14 13:39:51 -0800968 dprintf(INFO, "Updating device tree: start\n");
969
Neeti Desai17379b82012-06-04 18:42:53 -0700970 /* Update the Device Tree */
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530971 ret = update_device_tree((void *)tags,(const char *)final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -0700972 if(ret)
973 {
974 dprintf(CRITICAL, "ERROR: Updating Device Tree Failed \n");
975 ASSERT(0);
976 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800977 dprintf(INFO, "Updating device tree: done\n");
Neeti Desai17379b82012-06-04 18:42:53 -0700978#else
Neeti Desaie245d492012-06-01 12:52:13 -0700979 /* Generating the Atags */
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800980 generate_atags(tags, final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -0700981#endif
Neeti Desaie245d492012-06-01 12:52:13 -0700982
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -0700983#if VERIFIED_BOOT
Monika Singh292b3e92018-03-17 22:40:23 +0530984 if (VB_M == target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +0530985 {
986 if (device.verity_mode == 0) {
lijuangbdd9bb42016-03-01 18:22:17 +0800987#if FBCON_DISPLAY_MSG
Channagoud Kadabi86b0c112016-03-16 19:23:16 -0700988#if ENABLE_VB_ATTEST
Mayank Grover889be1b2017-09-12 20:12:23 +0530989 display_bootverify_menu(DISPLAY_MENU_EIO);
990 wait_for_users_action();
991 if(!pwr_key_is_pressed)
992 shutdown_device();
Channagoud Kadabi86b0c112016-03-16 19:23:16 -0700993#else
Mayank Grover889be1b2017-09-12 20:12:23 +0530994 display_bootverify_menu(DISPLAY_MENU_LOGGING);
Channagoud Kadabi86b0c112016-03-16 19:23:16 -0700995#endif
Mayank Grover889be1b2017-09-12 20:12:23 +0530996 wait_for_users_action();
lijuangbdd9bb42016-03-01 18:22:17 +0800997#else
Mayank Grover889be1b2017-09-12 20:12:23 +0530998 dprintf(CRITICAL,
999 "The dm-verity is not started in enforcing mode.\nWait for 5 seconds before proceeding\n");
1000 mdelay(5000);
lijuangbdd9bb42016-03-01 18:22:17 +08001001#endif
Mayank Grover889be1b2017-09-12 20:12:23 +05301002 }
lijuangbdd9bb42016-03-01 18:22:17 +08001003 }
lijuangbdd9bb42016-03-01 18:22:17 +08001004#endif
1005
1006#if VERIFIED_BOOT
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001007 /* Write protect the device info */
Channagoud Kadabi3bd9d1e2015-05-05 16:18:20 -07001008 if (!boot_into_recovery && target_build_variant_user() && devinfo_present && mmc_write_protect("devinfo", 1))
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001009 {
1010 dprintf(INFO, "Failed to write protect dev info\n");
1011 ASSERT(0);
1012 }
1013#endif
1014
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08001015 /* Turn off splash screen if enabled */
1016#if DISPLAY_SPLASH_SCREEN
1017 target_display_shutdown();
1018#endif
1019
Veera Sundaram Sankaran67ea0932015-09-25 10:09:30 -07001020 /* Perform target specific cleanup */
1021 target_uninit();
Monika Singh292b3e92018-03-17 22:40:23 +05301022 free_verified_boot_resource(&info);
1023 if (final_cmdline)
1024 free(final_cmdline);
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08001025
Deepa Dinamani33734bc2013-03-06 12:16:06 -08001026 dprintf(INFO, "booting linux @ %p, ramdisk @ %p (%d), tags/device tree @ %p\n",
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301027 entry, ramdisk, ramdisk_size, (void *)tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001028
1029 enter_critical_section();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001030
Amol Jadi4421e652011-06-16 15:00:48 -07001031 /* do any platform specific cleanup before kernel entry */
1032 platform_uninit();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001033
Brian Swetland9c4c0752009-01-25 16:23:50 -08001034 arch_disable_cache(UCACHE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001035
Amol Jadi504f9fe2012-08-16 13:56:48 -07001036#if ARM_WITH_MMU
Brian Swetland9c4c0752009-01-25 16:23:50 -08001037 arch_disable_mmu();
Amol Jadi504f9fe2012-08-16 13:56:48 -07001038#endif
Amol Jadi492d5a52013-03-15 16:12:34 -07001039 bs_set_timestamp(BS_KERNEL_ENTRY);
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001040
1041 if (IS_ARM64(kptr))
1042 /* Jump to a 64bit kernel */
1043 scm_elexec_call((paddr_t)kernel, tags_phys);
1044 else
1045 /* Jump to a 32bit kernel */
1046 entry(0, machtype, (unsigned*)tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001047}
1048
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001049/* Function to check if the memory address range falls within the aboot
1050 * boundaries.
1051 * start: Start of the memory region
1052 * size: Size of the memory region
1053 */
Vijay Kumar Pendoti9c002ad2016-03-09 13:52:45 +05301054int check_aboot_addr_range_overlap(uintptr_t start, uint32_t size)
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001055{
1056 /* Check for boundary conditions. */
Sundarajan Srinivasance2a0ea2013-12-16 17:02:56 -08001057 if ((UINT_MAX - start) < size)
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001058 return -1;
1059
1060 /* Check for memory overlap. */
1061 if ((start < MEMBASE) && ((start + size) <= MEMBASE))
1062 return 0;
Channagoud Kadabi94143912013-10-15 12:53:52 -07001063 else if (start >= (MEMBASE + MEMSIZE))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001064 return 0;
1065 else
1066 return -1;
1067}
1068
Mayank Grovere559cec2017-10-17 15:12:03 +05301069/* Function to check if the memory address range falls beyond ddr region.
1070 * start: Start of the memory region
1071 * size: Size of the memory region
1072 */
1073int check_ddr_addr_range_bound(uintptr_t start, uint32_t size)
1074{
1075 uintptr_t ddr_pa_start_addr = PA(get_ddr_start());
1076 uint64_t ddr_size = smem_get_ddr_size();
1077 uint64_t ddr_pa_end_addr = ddr_pa_start_addr + ddr_size;
1078 uintptr_t pa_start_addr = PA(start);
1079
1080 /* Check for boundary conditions. */
1081 if ((UINT_MAX - start) < size)
1082 return -1;
1083
1084 /* Check if memory range is beyond the ddr range. */
1085 if (pa_start_addr < ddr_pa_start_addr ||
1086 pa_start_addr >= (ddr_pa_end_addr) ||
1087 (pa_start_addr + size) > ddr_pa_end_addr)
1088 return -1;
1089 else
1090 return 0;
1091}
1092
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001093BUF_DMA_ALIGN(buf, BOOT_IMG_MAX_PAGE_SIZE); //Equal to max-supported pagesize
Dima Zavin214cc642009-01-26 11:16:21 -08001094
Monika Singh292b3e92018-03-17 22:40:23 +05301095int getimage(const bootinfo *info, void **image_buffer, uint32_t *imgsize,
1096 char *imgname)
1097{
1098 if (info == NULL || image_buffer == NULL || imgsize == NULL ||
1099 imgname == NULL) {
1100 dprintf(CRITICAL, "getimage: invalid parameters\n");
1101 return -1;
1102 }
1103
1104 for (uint32_t loadedindex = 0; loadedindex < info->num_loaded_images; loadedindex++) {
1105 if (!strncmp(info->images[loadedindex].name, imgname,
1106 strlen(imgname))) {
1107 *image_buffer = info->images[loadedindex].image_buffer;
1108 *imgsize = info->images[loadedindex].imgsize;
1109 return 0;
1110 }
1111 }
1112 return -1;
1113}
1114
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001115static void verify_signed_bootimg(uint32_t bootimg_addr, uint32_t bootimg_size)
1116{
1117 int ret;
Channagoud Kadabia8c623f2015-01-13 14:48:48 -08001118
1119#if !VERIFIED_BOOT
Sundarajan Srinivasance54d6e2013-11-11 12:45:00 -08001120#if IMAGE_VERIF_ALGO_SHA1
1121 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA1;
1122#else
1123 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA256;
1124#endif
Channagoud Kadabia8c623f2015-01-13 14:48:48 -08001125#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001126
1127 /* Assume device is rooted at this time. */
1128 device.is_tampered = 1;
1129
1130 dprintf(INFO, "Authenticating boot image (%d): start\n", bootimg_size);
1131
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001132#if VERIFIED_BOOT
Monika Singh7d2fc272018-03-16 17:16:01 +05301133 uint32_t bootstate;
1134 if(boot_into_recovery &&
Mayank Grover68fbf0d2017-10-24 14:13:39 +05301135 (!partition_multislot_is_supported()))
Monika Singh7d2fc272018-03-16 17:16:01 +05301136 {
1137 ret = boot_verify_image((unsigned char *)bootimg_addr,
1138 bootimg_size, "/recovery", &bootstate);
1139 }
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001140 else
Monika Singh7d2fc272018-03-16 17:16:01 +05301141 {
1142 ret = boot_verify_image((unsigned char *)bootimg_addr,
1143 bootimg_size, "/boot", &bootstate);
1144 }
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001145 boot_verify_print_state();
1146#else
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001147 ret = image_verify((unsigned char *)bootimg_addr,
1148 (unsigned char *)(bootimg_addr + bootimg_size),
1149 bootimg_size,
Sundarajan Srinivasance54d6e2013-11-11 12:45:00 -08001150 auth_algo);
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001151#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001152 dprintf(INFO, "Authenticating boot image: done return value = %d\n", ret);
1153
1154 if (ret)
1155 {
1156 /* Authorized kernel */
1157 device.is_tampered = 0;
Sundarajan Srinivasan3fb21f12013-09-16 18:36:15 -07001158 auth_kernel_img = 1;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001159 }
1160
Amit Blay4aa292f2015-04-28 21:55:59 +03001161#ifdef MDTP_SUPPORT
1162 {
1163 /* Verify MDTP lock.
1164 * For boot & recovery partitions, use aboot's verification result.
1165 */
1166 mdtp_ext_partition_verification_t ext_partition;
1167 ext_partition.partition = boot_into_recovery ? MDTP_PARTITION_RECOVERY : MDTP_PARTITION_BOOT;
1168 ext_partition.integrity_state = device.is_tampered ? MDTP_PARTITION_STATE_INVALID : MDTP_PARTITION_STATE_VALID;
1169 ext_partition.page_size = 0; /* Not needed since already validated */
1170 ext_partition.image_addr = 0; /* Not needed since already validated */
1171 ext_partition.image_size = 0; /* Not needed since already validated */
1172 ext_partition.sig_avail = FALSE; /* Not needed since already validated */
1173 mdtp_fwlock_verify_lock(&ext_partition);
1174 }
1175#endif /* MDTP_SUPPORT */
1176
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001177#if USE_PCOM_SECBOOT
1178 set_tamper_flag(device.is_tampered);
1179#endif
1180
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001181#if VERIFIED_BOOT
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001182 switch(boot_verify_get_state())
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001183 {
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001184 case RED:
lijuanga40d6302015-07-20 20:10:13 +08001185#if FBCON_DISPLAY_MSG
lijuang9a7d3b92015-11-30 14:41:24 +08001186 display_bootverify_menu(DISPLAY_MENU_RED);
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07001187#if ENABLE_VB_ATTEST
1188 mdelay(DELAY_WAIT);
1189 shutdown_device();
1190#else
lijuanga40d6302015-07-20 20:10:13 +08001191 wait_for_users_action();
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07001192#endif
lijuanga40d6302015-07-20 20:10:13 +08001193#else
1194 dprintf(CRITICAL,
1195 "Your device has failed verification and may not work properly.\nWait for 5 seconds before proceeding\n");
1196 mdelay(5000);
1197#endif
1198
1199 break;
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001200 case YELLOW:
lijuanga40d6302015-07-20 20:10:13 +08001201#if FBCON_DISPLAY_MSG
lijuang9a7d3b92015-11-30 14:41:24 +08001202 display_bootverify_menu(DISPLAY_MENU_YELLOW);
lijuanga40d6302015-07-20 20:10:13 +08001203 wait_for_users_action();
1204#else
1205 dprintf(CRITICAL,
1206 "Your device has loaded a different operating system.\nWait for 5 seconds before proceeding\n");
1207 mdelay(5000);
1208#endif
1209 break;
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001210 default:
lijuanga40d6302015-07-20 20:10:13 +08001211 break;
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001212 }
1213#endif
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001214#if !VERIFIED_BOOT
Unnati Gandhi1be04752015-03-27 19:41:53 +05301215 if(device.is_tampered)
1216 {
1217 write_device_info_mmc(&device);
1218 #ifdef TZ_TAMPER_FUSE
1219 set_tamper_fuse_cmd();
1220 #endif
1221 #ifdef ASSERT_ON_TAMPER
1222 dprintf(CRITICAL, "Device is tampered. Asserting..\n");
1223 ASSERT(0);
1224 #endif
1225 }
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001226#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001227}
1228
Monika Singh292b3e92018-03-17 22:40:23 +05301229int get_boot_image_info(void **image_buffer, uint32_t *imgsize,char *imgname)
1230{
1231 if (image_buffer == NULL || imgsize == NULL || imgname == NULL) {
1232 dprintf(CRITICAL, "get_boot_image_info: invalid parameters\n");
1233 return -1;
1234 }
1235
1236 if (!strncmp(info.images[0].name, imgname,
1237 strlen(imgname))) {
1238 *image_buffer = info.images[0].image_buffer;
1239 *imgsize = info.images[0].imgsize;
1240 return 0;
1241 }
1242 return -1;
1243}
1244
Maunik Shah0f3c8ac2014-03-02 17:47:58 +05301245static bool check_format_bit()
1246{
1247 bool ret = false;
1248 int index;
1249 uint64_t offset;
1250 struct boot_selection_info *in = NULL;
1251 char *buf = NULL;
1252
1253 index = partition_get_index("bootselect");
1254 if (index == INVALID_PTN)
1255 {
1256 dprintf(INFO, "Unable to locate /bootselect partition\n");
1257 return ret;
1258 }
1259 offset = partition_get_offset(index);
1260 if(!offset)
1261 {
1262 dprintf(INFO, "partition /bootselect doesn't exist\n");
1263 return ret;
1264 }
1265 buf = (char *) memalign(CACHE_LINE, ROUNDUP(page_size, CACHE_LINE));
Mayank Grover48860402016-11-29 12:34:53 +05301266 mmc_set_lun(partition_get_lun(index));
Maunik Shah0f3c8ac2014-03-02 17:47:58 +05301267 ASSERT(buf);
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301268 if (mmc_read(offset, (uint32_t *)buf, page_size))
Maunik Shah0f3c8ac2014-03-02 17:47:58 +05301269 {
1270 dprintf(INFO, "mmc read failure /bootselect %d\n", page_size);
1271 free(buf);
1272 return ret;
1273 }
1274 in = (struct boot_selection_info *) buf;
1275 if ((in->signature == BOOTSELECT_SIGNATURE) &&
1276 (in->version == BOOTSELECT_VERSION)) {
1277 if ((in->state_info & BOOTSELECT_FORMAT) &&
1278 !(in->state_info & BOOTSELECT_FACTORY))
1279 ret = true;
1280 } else {
1281 dprintf(CRITICAL, "Signature: 0x%08x or version: 0x%08x mismatched of /bootselect\n",
1282 in->signature, in->version);
1283 ASSERT(0);
1284 }
1285 free(buf);
1286 return ret;
1287}
1288
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001289void boot_verifier_init()
1290{
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001291 uint32_t boot_state;
1292 /* Check if device unlock */
1293 if(device.is_unlocked)
1294 {
1295 boot_verify_send_event(DEV_UNLOCK);
1296 boot_verify_print_state();
1297 dprintf(CRITICAL, "Device is unlocked! Skipping verification...\n");
1298 return;
1299 }
1300 else
1301 {
1302 boot_verify_send_event(BOOT_INIT);
1303 }
1304
1305 /* Initialize keystore */
1306 boot_state = boot_verify_keystore_init();
1307 if(boot_state == YELLOW)
1308 {
1309 boot_verify_print_state();
1310 dprintf(CRITICAL, "Keystore verification failed! Continuing anyways...\n");
1311 }
1312}
1313
Shashank Mittal23b8f422010-04-16 19:27:21 -07001314int boot_linux_from_mmc(void)
1315{
1316 struct boot_img_hdr *hdr = (void*) buf;
1317 struct boot_img_hdr *uhdr;
1318 unsigned offset = 0;
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07001319 int rcode;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001320 unsigned long long ptn = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001321 int index = INVALID_PTN;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001322
Shashank Mittalcd98d472011-08-02 14:29:24 -07001323 unsigned char *image_addr = 0;
1324 unsigned kernel_actual;
1325 unsigned ramdisk_actual;
1326 unsigned imagesize_actual;
Neeti Desai465491e2012-07-31 12:53:35 -07001327 unsigned second_actual = 0;
Neeti Desai465491e2012-07-31 12:53:35 -07001328
Matthew Qin271927e2015-03-31 22:07:07 -04001329 unsigned int dtb_size = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08001330 unsigned int out_len = 0;
1331 unsigned int out_avai_len = 0;
1332 unsigned char *out_addr = NULL;
1333 uint32_t dtb_offset = 0;
1334 unsigned char *kernel_start_addr = NULL;
1335 unsigned int kernel_size = 0;
Ameya Thakur10a33452016-06-13 14:24:26 -07001336 unsigned int patched_kernel_hdr_size = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08001337 int rc;
Monika Singh292b3e92018-03-17 22:40:23 +05301338#if VERIFIED_BOOT_2
1339 int status;
1340#endif
Mayank Groverc93cad82017-10-03 12:23:45 +05301341 char *ptn_name = NULL;
Neeti Desai465491e2012-07-31 12:53:35 -07001342#if DEVICE_TREE
1343 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -07001344 struct dt_entry dt_entry;
Neeti Desai465491e2012-07-31 12:53:35 -07001345 unsigned dt_table_offset;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001346 uint32_t dt_actual;
Deepa Dinamani19648b42013-09-05 17:05:55 -07001347 uint32_t dt_hdr_size;
Matthew Qin271927e2015-03-31 22:07:07 -04001348 unsigned char *best_match_dt_addr = NULL;
Neeti Desai465491e2012-07-31 12:53:35 -07001349#endif
Matthew Qin49e51fa2015-02-09 10:40:45 +08001350 struct kernel64_hdr *kptr = NULL;
Mayank Grover351a75e2017-05-30 20:06:08 +05301351 int current_active_slot = INVALID;
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001352
Maunik Shah0f3c8ac2014-03-02 17:47:58 +05301353 if (check_format_bit())
1354 boot_into_recovery = 1;
1355
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07001356 if (!boot_into_recovery) {
1357 memset(ffbm_mode_string, '\0', sizeof(ffbm_mode_string));
1358 rcode = get_ffbm(ffbm_mode_string, sizeof(ffbm_mode_string));
1359 if (rcode <= 0) {
1360 boot_into_ffbm = false;
1361 if (rcode < 0)
1362 dprintf(CRITICAL,"failed to get ffbm cookie");
1363 } else
1364 boot_into_ffbm = true;
1365 } else
1366 boot_into_ffbm = false;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001367 uhdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
1368 if (!memcmp(uhdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1369 dprintf(INFO, "Unified boot method!\n");
1370 hdr = uhdr;
1371 goto unified_boot;
1372 }
Mayank Groverc93cad82017-10-03 12:23:45 +05301373
1374 /* For a/b recovery image code is on boot partition.
1375 If we support multislot, always use boot partition. */
1376 if (boot_into_recovery &&
1377 (!partition_multislot_is_supported()))
1378 ptn_name = "recovery";
1379 else
1380 ptn_name = "boot";
1381
1382 index = partition_get_index(ptn_name);
1383 ptn = partition_get_offset(index);
1384 if(ptn == 0) {
1385 dprintf(CRITICAL, "ERROR: No %s partition found\n", ptn_name);
1386 return -1;
Kinson Chikf1a43512011-07-14 11:28:39 -07001387 }
Mayank Groverc93cad82017-10-03 12:23:45 +05301388
Channagoud Kadabief0547c2015-02-10 12:57:38 -08001389 /* Set Lun for boot & recovery partitions */
1390 mmc_set_lun(partition_get_lun(index));
Shashank Mittal23b8f422010-04-16 19:27:21 -07001391
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301392 if (mmc_read(ptn + offset, (uint32_t *) buf, page_size)) {
Shashank Mittal23b8f422010-04-16 19:27:21 -07001393 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
1394 return -1;
1395 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001396
1397 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001398 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Mayank Grover351a75e2017-05-30 20:06:08 +05301399 return ERR_INVALID_BOOT_MAGIC;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001400 }
1401
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07001402 if (hdr->page_size && (hdr->page_size != page_size)) {
vijay kumar2e21b3a2014-06-26 17:40:15 +05301403
1404 if (hdr->page_size > BOOT_IMG_MAX_PAGE_SIZE) {
1405 dprintf(CRITICAL, "ERROR: Invalid page size\n");
1406 return -1;
1407 }
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07001408 page_size = hdr->page_size;
1409 page_mask = page_size - 1;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001410 }
1411
Matthew Qin49e51fa2015-02-09 10:40:45 +08001412 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1413 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Kishor PKd8ddcad2017-07-27 13:53:57 +05301414 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001415
Matthew Qin49e51fa2015-02-09 10:40:45 +08001416 image_addr = (unsigned char *)target_get_scratch_address();
Kishor PK9e91b592017-05-24 12:14:55 +05301417 memcpy(image_addr, (void *)buf, page_size);
1418
1419 /* ensure commandline is terminated */
1420 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
Matthew Qin49e51fa2015-02-09 10:40:45 +08001421
1422#if DEVICE_TREE
Parth Dixit4097b622016-03-15 14:42:27 +05301423#ifndef OSVERSION_IN_BOOTIMAGE
1424 dt_size = hdr->dt_size;
1425#endif
1426 dt_actual = ROUND_TO_PAGE(dt_size, page_mask);
Kishor PKd8ddcad2017-07-27 13:53:57 +05301427 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 +05301428 dprintf(CRITICAL, "Integer overflow detected in bootimage header fields at %u in %s\n",__LINE__,__FILE__);
1429 return -1;
1430 }
Kishor PKd8ddcad2017-07-27 13:53:57 +05301431 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + second_actual + dt_actual);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001432#else
Kishor PKd8ddcad2017-07-27 13:53:57 +05301433 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 +05301434 dprintf(CRITICAL, "Integer overflow detected in bootimage header fields at %u in %s\n",__LINE__,__FILE__);
1435 return -1;
1436 }
Kishor PKd8ddcad2017-07-27 13:53:57 +05301437 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + second_actual);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001438#endif
1439
1440#if VERIFIED_BOOT
1441 boot_verifier_init();
1442#endif
1443
Vijay Kumar Pendoti9c002ad2016-03-09 13:52:45 +05301444 if (check_aboot_addr_range_overlap((uintptr_t) image_addr, imagesize_actual))
Matthew Qin49e51fa2015-02-09 10:40:45 +08001445 {
1446 dprintf(CRITICAL, "Boot image buffer address overlaps with aboot addresses.\n");
1447 return -1;
1448 }
1449
Matthew Qinbb7923d2015-02-09 10:56:09 +08001450 /*
1451 * Update loading flow of bootimage to support compressed/uncompressed
1452 * bootimage on both 64bit and 32bit platform.
1453 * 1. Load bootimage from emmc partition onto DDR.
1454 * 2. Check if bootimage is gzip format. If yes, decompress compressed kernel
1455 * 3. Check kernel header and update kernel load addr for 64bit and 32bit
1456 * platform accordingly.
1457 * 4. Sanity Check on kernel_addr and ramdisk_addr and copy data.
1458 */
Mayank Grover351a75e2017-05-30 20:06:08 +05301459 if (partition_multislot_is_supported())
1460 {
1461 current_active_slot = partition_find_active_slot();
1462 dprintf(INFO, "Loading boot image (%d) active_slot(%s): start\n",
1463 imagesize_actual, SUFFIX_SLOT(current_active_slot));
1464 }
1465 else
1466 {
1467 dprintf(INFO, "Loading (%s) image (%d): start\n",
1468 (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual);
1469 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08001470 bs_set_timestamp(BS_KERNEL_LOAD_START);
1471
Gaurav Nebhwani43e2a462016-03-17 21:32:56 +05301472 if ((target_get_max_flash_size() - page_size) < imagesize_actual)
1473 {
1474 dprintf(CRITICAL, "booimage size is greater than DDR can hold\n");
1475 return -1;
1476 }
Kishor PK9e91b592017-05-24 12:14:55 +05301477 offset = page_size;
1478 /* Read image without signature and header*/
1479 if (mmc_read(ptn + offset, (void *)(image_addr + offset), imagesize_actual - page_size))
Matthew Qin49e51fa2015-02-09 10:40:45 +08001480 {
1481 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
1482 return -1;
1483 }
1484
Mayank Grover351a75e2017-05-30 20:06:08 +05301485 if (partition_multislot_is_supported())
1486 {
1487 dprintf(INFO, "Loading boot image (%d) active_slot(%s): done\n",
1488 imagesize_actual, SUFFIX_SLOT(current_active_slot));
1489 }
1490 else
1491 {
1492 dprintf(INFO, "Loading (%s) image (%d): done\n",
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001493 (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual);
1494
Mayank Grover351a75e2017-05-30 20:06:08 +05301495 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08001496 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
1497
1498 /* Authenticate Kernel */
1499 dprintf(INFO, "use_signed_kernel=%d, is_unlocked=%d, is_tampered=%d.\n",
1500 (int) target_use_signed_kernel(),
1501 device.is_unlocked,
1502 device.is_tampered);
Monika Singh292b3e92018-03-17 22:40:23 +05301503#if VERIFIED_BOOT_2
1504 offset = imagesize_actual;
1505 if (check_aboot_addr_range_overlap((uintptr_t)image_addr + offset, page_size))
1506 {
1507 dprintf(CRITICAL, "Signature read buffer address overlaps with aboot addresses.\n");
1508 return -1;
1509 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08001510
Monika Singh292b3e92018-03-17 22:40:23 +05301511 /* Read signature */
1512 if(mmc_read(ptn + offset, (void *)(image_addr + offset), page_size))
1513 {
1514 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
1515 return -1;
1516 }
1517
1518 memset(&info, 0, sizeof(bootinfo));
1519 info.images[0].image_buffer = image_addr;
1520 info.images[0].imgsize = imagesize_actual;
1521 info.images[0].name = "boot";
1522 info.num_loaded_images = 0;
1523 info.multi_slot_boot = partition_multislot_is_supported();
1524 info.bootreason_alarm = boot_reason_alarm;
1525 info.bootinto_recovery = boot_into_recovery;
1526 status = load_image_and_auth(&info);
1527 if(status)
1528 return -1;
1529
1530 vbcmdline = info.vbcmdline;
1531#else
Channagoud Kadabi736c4962015-08-21 11:56:52 -07001532 /* Change the condition a little bit to include the test framework support.
1533 * We would never reach this point if device is in fastboot mode, even if we did
1534 * that means we are in test mode, so execute kernel authentication part for the
1535 * tests */
Parth Dixitcb0c6082015-12-30 15:01:13 +05301536 if((target_use_signed_kernel() && (!device.is_unlocked)) || is_test_mode_enabled())
Matthew Qin49e51fa2015-02-09 10:40:45 +08001537 {
1538 offset = imagesize_actual;
Vijay Kumar Pendoti9c002ad2016-03-09 13:52:45 +05301539 if (check_aboot_addr_range_overlap((uintptr_t)image_addr + offset, page_size))
Matthew Qin49e51fa2015-02-09 10:40:45 +08001540 {
1541 dprintf(CRITICAL, "Signature read buffer address overlaps with aboot addresses.\n");
1542 return -1;
1543 }
1544
1545 /* Read signature */
1546 if(mmc_read(ptn + offset, (void *)(image_addr + offset), page_size))
1547 {
1548 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
1549 return -1;
1550 }
1551
1552 verify_signed_bootimg((uint32_t)image_addr, imagesize_actual);
Channagoud Kadabi736c4962015-08-21 11:56:52 -07001553 /* The purpose of our test is done here */
Parth Dixitcb0c6082015-12-30 15:01:13 +05301554 if(is_test_mode_enabled() && auth_kernel_img)
Channagoud Kadabi736c4962015-08-21 11:56:52 -07001555 return 0;
Matthew Qin49e51fa2015-02-09 10:40:45 +08001556 } else {
1557 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
1558 #ifdef TZ_SAVE_KERNEL_HASH
1559 aboot_save_boot_hash_mmc((uint32_t) image_addr, imagesize_actual);
1560 #endif /* TZ_SAVE_KERNEL_HASH */
Amit Blay4aa292f2015-04-28 21:55:59 +03001561
1562#ifdef MDTP_SUPPORT
1563 {
1564 /* Verify MDTP lock.
1565 * For boot & recovery partitions, MDTP will use boot_verifier APIs,
1566 * since verification was skipped in aboot. The signature is not part of the loaded image.
1567 */
1568 mdtp_ext_partition_verification_t ext_partition;
1569 ext_partition.partition = boot_into_recovery ? MDTP_PARTITION_RECOVERY : MDTP_PARTITION_BOOT;
1570 ext_partition.integrity_state = MDTP_PARTITION_STATE_UNSET;
1571 ext_partition.page_size = page_size;
1572 ext_partition.image_addr = (uint32)image_addr;
1573 ext_partition.image_size = imagesize_actual;
1574 ext_partition.sig_avail = FALSE;
1575 mdtp_fwlock_verify_lock(&ext_partition);
1576 }
1577#endif /* MDTP_SUPPORT */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001578 }
Monika Singh292b3e92018-03-17 22:40:23 +05301579#endif
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001580
Sridhar Parasuram00bfedb2015-05-26 14:21:27 -07001581#if VERIFIED_BOOT
taozhang93088bd2016-11-16 15:50:46 +08001582 if((boot_verify_get_state() == ORANGE) && (!boot_into_ffbm))
Reut Zysmanba8a9d52016-02-18 11:44:04 +02001583 {
1584#if FBCON_DISPLAY_MSG
1585 display_bootverify_menu(DISPLAY_MENU_ORANGE);
1586 wait_for_users_action();
1587#else
1588 dprintf(CRITICAL,
1589 "Your device has been unlocked and can't be trusted.\nWait for 5 seconds before proceeding\n");
1590 mdelay(5000);
1591#endif
1592 }
1593#endif
1594
1595#if VERIFIED_BOOT
Monika Singh292b3e92018-03-17 22:40:23 +05301596 if (VB_M == target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +05301597 {
1598 /* set boot and system versions. */
1599 set_os_version((unsigned char *)image_addr);
1600 // send root of trust
1601 if(!send_rot_command((uint32_t)device.is_unlocked))
1602 ASSERT(0);
1603 }
Parth Dixitbc9b6492015-10-18 00:41:38 +05301604#endif
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001605 /*
Matthew Qinbb7923d2015-02-09 10:56:09 +08001606 * Check if the kernel image is a gzip package. If yes, need to decompress it.
1607 * If not, continue booting.
1608 */
1609 if (is_gzip_package((unsigned char *)(image_addr + page_size), hdr->kernel_size))
1610 {
1611 out_addr = (unsigned char *)(image_addr + imagesize_actual + page_size);
1612 out_avai_len = target_get_max_flash_size() - imagesize_actual - page_size;
Matthew Qin0b15b322015-05-19 05:20:54 -04001613 dprintf(INFO, "decompressing kernel image: start\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001614 rc = decompress((unsigned char *)(image_addr + page_size),
1615 hdr->kernel_size, out_addr, out_avai_len,
1616 &dtb_offset, &out_len);
1617 if (rc)
1618 {
Matthew Qin0b15b322015-05-19 05:20:54 -04001619 dprintf(CRITICAL, "decompressing kernel image failed!!!\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001620 ASSERT(0);
1621 }
1622
Matthew Qin0b15b322015-05-19 05:20:54 -04001623 dprintf(INFO, "decompressing kernel image: done\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001624 kptr = (struct kernel64_hdr *)out_addr;
1625 kernel_start_addr = out_addr;
1626 kernel_size = out_len;
1627 } else {
Ameya Thakur10a33452016-06-13 14:24:26 -07001628 dprintf(INFO, "Uncpmpressed kernel in use\n");
1629 if (!strncmp((char*)(image_addr + page_size),
1630 PATCHED_KERNEL_MAGIC,
1631 sizeof(PATCHED_KERNEL_MAGIC) - 1)) {
1632 dprintf(INFO, "Patched kernel detected\n");
1633 kptr = (struct kernel64_hdr *)(image_addr + page_size +
1634 PATCHED_KERNEL_HEADER_SIZE);
1635 //The size of the kernel is stored at start of kernel image + 16
1636 //The dtb would start just after the kernel
1637 dtb_offset = *((uint32_t*)((unsigned char*)
1638 (image_addr + page_size +
1639 sizeof(PATCHED_KERNEL_MAGIC) -
1640 1)));
1641 //The actual kernel starts after the 20 byte header.
1642 kernel_start_addr = (unsigned char*)(image_addr +
1643 page_size + PATCHED_KERNEL_HEADER_SIZE);
1644 kernel_size = hdr->kernel_size;
1645 patched_kernel_hdr_size = PATCHED_KERNEL_HEADER_SIZE;
1646 } else {
1647 dprintf(INFO, "Kernel image not patched..Unable to locate dt offset\n");
1648 kptr = (struct kernel64_hdr *)(image_addr + page_size);
1649 kernel_start_addr = (unsigned char *)(image_addr + page_size);
1650 kernel_size = hdr->kernel_size;
1651 }
Matthew Qinbb7923d2015-02-09 10:56:09 +08001652 }
1653
1654 /*
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001655 * Update the kernel/ramdisk/tags address if the boot image header
1656 * has default values, these default values come from mkbootimg when
1657 * the boot image is flashed using fastboot flash:raw
1658 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001659 update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr));
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001660
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001661 /* Get virtual addresses since the hdr saves physical addresses. */
1662 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
1663 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
1664 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001665
Matthew Qinbb7923d2015-02-09 10:56:09 +08001666 kernel_size = ROUND_TO_PAGE(kernel_size, page_mask);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001667 /* Check if the addresses in the header are valid. */
Matthew Qinbb7923d2015-02-09 10:56:09 +08001668 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_size) ||
Mayank Grovere559cec2017-10-17 15:12:03 +05301669 check_ddr_addr_range_bound(hdr->kernel_addr, kernel_size) ||
1670 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual) ||
1671 check_ddr_addr_range_bound(hdr->ramdisk_addr, ramdisk_actual))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001672 {
Mayank Grovere559cec2017-10-17 15:12:03 +05301673 dprintf(CRITICAL, "kernel/ramdisk addresses are not valid.\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001674 return -1;
1675 }
1676
1677#ifndef DEVICE_TREE
Mayank Grovere559cec2017-10-17 15:12:03 +05301678 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE) ||
1679 check_ddr_addr_range_bound(hdr->tags_addr, MAX_TAGS_SIZE))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001680 {
Mayank Grovere559cec2017-10-17 15:12:03 +05301681 dprintf(CRITICAL, "Tags addresses are not valid.\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001682 return -1;
1683 }
1684#endif
1685
Matthew Qin49e51fa2015-02-09 10:40:45 +08001686 /* Move kernel, ramdisk and device tree to correct address */
Matthew Qinbb7923d2015-02-09 10:56:09 +08001687 memmove((void*) hdr->kernel_addr, kernel_start_addr, kernel_size);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001688 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001689
Matthew Qin49e51fa2015-02-09 10:40:45 +08001690 #if DEVICE_TREE
Parth Dixit4097b622016-03-15 14:42:27 +05301691 if(dt_size) {
Matthew Qin49e51fa2015-02-09 10:40:45 +08001692 dt_table_offset = ((uint32_t)image_addr + page_size + kernel_actual + ramdisk_actual + second_actual);
1693 table = (struct dt_table*) dt_table_offset;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001694
Matthew Qin49e51fa2015-02-09 10:40:45 +08001695 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
1696 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1697 return -1;
1698 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001699
P.V. Phani Kumar82916762016-03-09 09:20:19 +05301700 /* Its Error if, dt_hdr_size (table->num_entries * dt_entry size + Dev_Tree Header)
1701 goes beyound hdr->dt_size*/
Parth Dixit4097b622016-03-15 14:42:27 +05301702 if (dt_hdr_size > ROUND_TO_PAGE(dt_size,hdr->page_size)) {
P.V. Phani Kumar82916762016-03-09 09:20:19 +05301703 dprintf(CRITICAL, "ERROR: Invalid Device Tree size \n");
1704 return -1;
1705 }
1706
Matthew Qin49e51fa2015-02-09 10:40:45 +08001707 /* Find index of device tree within device tree table */
1708 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
1709 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1710 return -1;
1711 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001712
P.V. Phani Kumar3c333202016-03-09 11:54:37 +05301713 if(dt_entry.offset > (UINT_MAX - dt_entry.size)) {
1714 dprintf(CRITICAL, "ERROR: Device tree contents are Invalid\n");
1715 return -1;
1716 }
1717
1718 /* Ensure we are not overshooting dt_size with the dt_entry selected */
Parth Dixit4097b622016-03-15 14:42:27 +05301719 if ((dt_entry.offset + dt_entry.size) > dt_size) {
P.V. Phani Kumar3c333202016-03-09 11:54:37 +05301720 dprintf(CRITICAL, "ERROR: Device tree contents are Invalid\n");
1721 return -1;
1722 }
1723
Matthew Qin271927e2015-03-31 22:07:07 -04001724 if (is_gzip_package((unsigned char *)dt_table_offset + dt_entry.offset, dt_entry.size))
1725 {
1726 unsigned int compressed_size = 0;
1727 out_addr += out_len;
1728 out_avai_len -= out_len;
Matthew Qin0b15b322015-05-19 05:20:54 -04001729 dprintf(INFO, "decompressing dtb: start\n");
Matthew Qin271927e2015-03-31 22:07:07 -04001730 rc = decompress((unsigned char *)dt_table_offset + dt_entry.offset,
1731 dt_entry.size, out_addr, out_avai_len,
1732 &compressed_size, &dtb_size);
1733 if (rc)
1734 {
Matthew Qin0b15b322015-05-19 05:20:54 -04001735 dprintf(CRITICAL, "decompressing dtb failed!!!\n");
Matthew Qin271927e2015-03-31 22:07:07 -04001736 ASSERT(0);
1737 }
1738
Matthew Qin0b15b322015-05-19 05:20:54 -04001739 dprintf(INFO, "decompressing dtb: done\n");
Matthew Qin271927e2015-03-31 22:07:07 -04001740 best_match_dt_addr = out_addr;
1741 } else {
1742 best_match_dt_addr = (unsigned char *)dt_table_offset + dt_entry.offset;
1743 dtb_size = dt_entry.size;
1744 }
1745
Matthew Qin49e51fa2015-02-09 10:40:45 +08001746 /* Validate and Read device device tree in the tags_addr */
Mayank Grovere559cec2017-10-17 15:12:03 +05301747 if (check_aboot_addr_range_overlap(hdr->tags_addr, dtb_size) ||
1748 check_ddr_addr_range_bound(hdr->tags_addr, dtb_size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001749 {
Mayank Grovere559cec2017-10-17 15:12:03 +05301750 dprintf(CRITICAL, "Device tree addresses are not valid\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001751 return -1;
1752 }
Shashank Mittal162244e2011-08-08 19:01:25 -07001753
Matthew Qin271927e2015-03-31 22:07:07 -04001754 memmove((void *)hdr->tags_addr, (char *)best_match_dt_addr, dtb_size);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001755 } else {
1756 /* Validate the tags_addr */
Mayank Grovere559cec2017-10-17 15:12:03 +05301757 if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual) ||
1758 check_ddr_addr_range_bound(hdr->tags_addr, kernel_actual))
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001759 {
Mayank Grovere559cec2017-10-17 15:12:03 +05301760 dprintf(CRITICAL, "Device tree addresses are not valid.\n");
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001761 return -1;
1762 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08001763 /*
1764 * If appended dev tree is found, update the atags with
1765 * memory address to the DTB appended location on RAM.
1766 * Else update with the atags address in the kernel header
1767 */
1768 void *dtb;
Ameya Thakur10a33452016-06-13 14:24:26 -07001769 dtb = dev_tree_appended(
1770 (void*)(image_addr + page_size +
1771 patched_kernel_hdr_size),
1772 hdr->kernel_size, dtb_offset,
1773 (void *)hdr->tags_addr);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001774 if (!dtb) {
1775 dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001776 return -1;
1777 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07001778 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08001779 #endif
Shashank Mittal23b8f422010-04-16 19:27:21 -07001780
Stanimir Varbanov69ec5462013-07-18 18:17:42 +03001781 if (boot_into_recovery && !device.is_unlocked && !device.is_tampered)
1782 target_load_ssd_keystore();
1783
Shashank Mittal23b8f422010-04-16 19:27:21 -07001784unified_boot:
Shashank Mittal23b8f422010-04-16 19:27:21 -07001785
Dima Zavin77e41f32013-03-06 16:10:43 -08001786 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07001787 (const char *)hdr->cmdline, board_machtype(),
Shashank Mittal23b8f422010-04-16 19:27:21 -07001788 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
1789
1790 return 0;
1791}
1792
Dima Zavin214cc642009-01-26 11:16:21 -08001793int boot_linux_from_flash(void)
1794{
1795 struct boot_img_hdr *hdr = (void*) buf;
Dima Zavin214cc642009-01-26 11:16:21 -08001796 struct ptentry *ptn;
1797 struct ptable *ptable;
1798 unsigned offset = 0;
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001799
Shashank Mittalcd98d472011-08-02 14:29:24 -07001800 unsigned char *image_addr = 0;
1801 unsigned kernel_actual;
1802 unsigned ramdisk_actual;
1803 unsigned imagesize_actual;
vijay kumar8f53e362015-11-24 13:38:11 +05301804 unsigned second_actual = 0;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001805
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001806#if DEVICE_TREE
Monika Singh292b3e92018-03-17 22:40:23 +05301807 struct dt_table *table = NULL;
Joel Kingaa335dc2013-06-03 16:11:08 -07001808 struct dt_entry dt_entry;
vijay kumar8f53e362015-11-24 13:38:11 +05301809 unsigned dt_table_offset;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001810 uint32_t dt_actual;
Monika Singh292b3e92018-03-17 22:40:23 +05301811 uint32_t dt_hdr_size = 0;
vijay kumar8f53e362015-11-24 13:38:11 +05301812 unsigned int dtb_size = 0;
1813 unsigned char *best_match_dt_addr = NULL;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001814#endif
1815
David Ng183a7422009-12-07 14:55:21 -08001816 if (target_is_emmc_boot()) {
1817 hdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
1818 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1819 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
1820 return -1;
1821 }
1822 goto continue_boot;
1823 }
1824
Dima Zavin214cc642009-01-26 11:16:21 -08001825 ptable = flash_get_ptable();
1826 if (ptable == NULL) {
1827 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1828 return -1;
1829 }
1830
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001831 if(!boot_into_recovery)
1832 {
1833 ptn = ptable_find(ptable, "boot");
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001834
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001835 if (ptn == NULL) {
1836 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1837 return -1;
1838 }
1839 }
1840 else
1841 {
1842 ptn = ptable_find(ptable, "recovery");
1843 if (ptn == NULL) {
1844 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
1845 return -1;
1846 }
Dima Zavin214cc642009-01-26 11:16:21 -08001847 }
1848
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05301849 /* Read boot.img header from flash */
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001850 if (flash_read(ptn, offset, buf, page_size)) {
Dima Zavin214cc642009-01-26 11:16:21 -08001851 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
1852 return -1;
1853 }
Dima Zavin214cc642009-01-26 11:16:21 -08001854
1855 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001856 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Dima Zavin214cc642009-01-26 11:16:21 -08001857 return -1;
1858 }
1859
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001860 if (hdr->page_size != page_size) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001861 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 -08001862 return -1;
1863 }
1864
Kishor PK9e91b592017-05-24 12:14:55 +05301865 image_addr = (unsigned char *)target_get_scratch_address();
1866 memcpy(image_addr, (void *)buf, page_size);
vijay kumar287bb542015-09-29 13:01:52 +05301867
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001868 /*
1869 * Update the kernel/ramdisk/tags address if the boot image header
1870 * has default values, these default values come from mkbootimg when
1871 * the boot image is flashed using fastboot flash:raw
1872 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001873 update_ker_tags_rdisk_addr(hdr, false);
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001874
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001875 /* Get virtual addresses since the hdr saves physical addresses. */
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001876 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
1877 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
1878 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
1879
1880 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1881 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Mayank Grover032736f2017-07-14 20:34:51 +05301882 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001883
Kishor PK9e91b592017-05-24 12:14:55 +05301884 /* ensure commandline is terminated */
1885 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
1886
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001887 /* Check if the addresses in the header are valid. */
1888 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
Mayank Grovere559cec2017-10-17 15:12:03 +05301889 check_ddr_addr_range_bound(hdr->kernel_addr, kernel_actual) ||
1890 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual) ||
1891 check_ddr_addr_range_bound(hdr->ramdisk_addr, ramdisk_actual))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001892 {
Mayank Grovere559cec2017-10-17 15:12:03 +05301893 dprintf(CRITICAL, "kernel/ramdisk addresses are not valid.\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001894 return -1;
1895 }
1896
1897#ifndef DEVICE_TREE
Kishor PKd8ddcad2017-07-27 13:53:57 +05301898 if (UINT_MAX < ((uint64_t)kernel_actual + (uint64_t)ramdisk_actual+ (uint64_t)second_actual + page_size)) {
Mayank Grover032736f2017-07-14 20:34:51 +05301899 dprintf(CRITICAL, "Integer overflow detected in bootimage header fields\n");
1900 return -1;
1901 }
Kishor PKd8ddcad2017-07-27 13:53:57 +05301902 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + second_actual);
Mayank Grover032736f2017-07-14 20:34:51 +05301903
Mayank Grovere559cec2017-10-17 15:12:03 +05301904 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE) ||
1905 check_ddr_addr_range_bound(hdr->tags_addr, MAX_TAGS_SIZE))
Vijay Kumar Pendotid3ed20e2016-09-20 00:34:46 +05301906 {
Mayank Grovere559cec2017-10-17 15:12:03 +05301907 dprintf(CRITICAL, "Tags addresses are not valid.\n");
Vijay Kumar Pendotid3ed20e2016-09-20 00:34:46 +05301908 return -1;
1909 }
1910#else
1911
1912#ifndef OSVERSION_IN_BOOTIMAGE
1913 dt_size = hdr->dt_size;
1914#endif
Mayank Grover032736f2017-07-14 20:34:51 +05301915 dt_actual = ROUND_TO_PAGE(dt_size, page_mask);
Kishor PKd8ddcad2017-07-27 13:53:57 +05301916 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 +05301917 dprintf(CRITICAL, "Integer overflow detected in bootimage header fields\n");
1918 return -1;
1919 }
1920
Kishor PKd8ddcad2017-07-27 13:53:57 +05301921 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + second_actual + dt_actual);
Mayank Grover032736f2017-07-14 20:34:51 +05301922
Mayank Grovere559cec2017-10-17 15:12:03 +05301923 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_size) ||
1924 check_ddr_addr_range_bound(hdr->tags_addr, dt_size))
Mayank Grover032736f2017-07-14 20:34:51 +05301925 {
Mayank Grovere559cec2017-10-17 15:12:03 +05301926 dprintf(CRITICAL, "Device tree addresses are not valid.\n");
Mayank Grover032736f2017-07-14 20:34:51 +05301927 return -1;
1928 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001929#endif
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001930
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05301931 /* Read full boot.img from flash */
1932 dprintf(INFO, "Loading (%s) image (%d): start\n",
1933 (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual);
1934 bs_set_timestamp(BS_KERNEL_LOAD_START);
1935
1936 if (UINT_MAX - page_size < imagesize_actual)
1937 {
1938 dprintf(CRITICAL,"Integer overflow detected in bootimage header fields %u %s\n", __LINE__,__func__);
1939 return -1;
1940 }
1941
1942 /*Check the availability of RAM before reading boot image + max signature length from flash*/
1943 if (target_get_max_flash_size() < (imagesize_actual + page_size))
1944 {
1945 dprintf(CRITICAL, "bootimage size is greater than DDR can hold\n");
1946 return -1;
1947 }
1948
1949 offset = page_size;
1950 /* Read image without signature and header */
1951 if (flash_read(ptn, offset, (void *)(image_addr + offset), imagesize_actual - page_size))
1952 {
1953 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
1954 return -1;
1955 }
1956
1957 dprintf(INFO, "Loading (%s) image (%d): done\n",
1958 (!boot_into_recovery ? "boot" : "recovery"), imagesize_actual);
1959 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
1960
Shashank Mittalcd98d472011-08-02 14:29:24 -07001961 /* Authenticate Kernel */
Deepa Dinamani23b60d42013-06-24 18:10:52 -07001962 if(target_use_signed_kernel() && (!device.is_unlocked))
Shashank Mittalcd98d472011-08-02 14:29:24 -07001963 {
Shashank Mittalcd98d472011-08-02 14:29:24 -07001964 offset = imagesize_actual;
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05301965
Shashank Mittalcd98d472011-08-02 14:29:24 -07001966 /* Read signature */
1967 if (flash_read(ptn, offset, (void *)(image_addr + offset), page_size))
1968 {
1969 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001970 return -1;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001971 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07001972
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301973 verify_signed_bootimg((uint32_t)image_addr, imagesize_actual);
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05301974 }
1975 offset = page_size;
1976 if(hdr->second_size != 0) {
1977 if (UINT_MAX - offset < second_actual)
1978 {
1979 dprintf(CRITICAL, "ERROR: Integer overflow in boot image header %s\t%d\n",__func__,__LINE__);
1980 return -1;
vijay kumar8f53e362015-11-24 13:38:11 +05301981 }
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05301982 offset += second_actual;
1983 /* Second image loading not implemented. */
1984 ASSERT(0);
1985 }
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05301986 /* Move kernel and ramdisk to correct address */
1987 memmove((void*) hdr->kernel_addr, (char*) (image_addr + page_size), hdr->kernel_size);
1988 memmove((void*) hdr->ramdisk_addr, (char*) (image_addr + page_size + kernel_actual), hdr->ramdisk_size);
1989
1990#if DEVICE_TREE
1991 if(dt_size != 0) {
1992
1993 dt_table_offset = ((uint32_t)image_addr + page_size + kernel_actual + ramdisk_actual + second_actual);
1994
1995 table = (struct dt_table*) dt_table_offset;
1996
1997 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
1998 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1999 return -1;
2000 }
2001
2002 /* Its Error if, dt_hdr_size (table->num_entries * dt_entry size + Dev_Tree Header)
2003 goes beyound hdr->dt_size*/
2004 if (dt_hdr_size > ROUND_TO_PAGE(dt_size,hdr->page_size)) {
2005 dprintf(CRITICAL, "ERROR: Invalid Device Tree size \n");
2006 return -1;
2007 }
2008
2009 /* Find index of device tree within device tree table */
2010 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
2011 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
2012 return -1;
2013 }
2014
2015 /* Validate and Read device device tree in the "tags_add */
2016 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size) ||
2017 check_ddr_addr_range_bound(hdr->tags_addr, dt_entry.size))
2018 {
2019 dprintf(CRITICAL, "Device tree addresses are not valid.\n");
2020 return -1;
2021 }
2022
2023 if(dt_entry.offset > (UINT_MAX - dt_entry.size)) {
2024 dprintf(CRITICAL, "ERROR: Device tree contents are Invalid\n");
2025 return -1;
2026 }
2027
2028 /* Ensure we are not overshooting dt_size with the dt_entry selected */
2029 if ((dt_entry.offset + dt_entry.size) > dt_size) {
2030 dprintf(CRITICAL, "ERROR: Device tree contents are Invalid\n");
2031 return -1;
2032 }
2033
2034 best_match_dt_addr = (unsigned char *)table + dt_entry.offset;
2035 dtb_size = dt_entry.size;
2036 memmove((void *)hdr->tags_addr, (char *)best_match_dt_addr, dtb_size);
2037 }
2038#endif
2039 if(target_use_signed_kernel() && (!device.is_unlocked))
2040 {
Shashank Mittal162244e2011-08-08 19:01:25 -07002041 /* Make sure everything from scratch address is read before next step!*/
Shashank Mittala0032282011-08-26 14:50:11 -07002042 if(device.is_tampered)
Shashank Mittal162244e2011-08-08 19:01:25 -07002043 {
2044 write_device_info_flash(&device);
2045 }
Channagoud Kadabi5c86fe32012-02-16 10:58:48 +05302046#if USE_PCOM_SECBOOT
2047 set_tamper_flag(device.is_tampered);
2048#endif
Shashank Mittalcd98d472011-08-02 14:29:24 -07002049 }
David Ng183a7422009-12-07 14:55:21 -08002050continue_boot:
Dima Zavin214cc642009-01-26 11:16:21 -08002051
Dima Zavin214cc642009-01-26 11:16:21 -08002052 /* TODO: create/pass atags to kernel */
2053
Ajay Dudanie28a6072011-07-01 13:59:46 -07002054 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07002055 (const char *)hdr->cmdline, board_machtype(),
Dima Zavin214cc642009-01-26 11:16:21 -08002056 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
2057
2058 return 0;
2059}
Brian Swetland9c4c0752009-01-25 16:23:50 -08002060
Shashank Mittal162244e2011-08-08 19:01:25 -07002061void write_device_info_mmc(device_info *dev)
2062{
Shashank Mittal162244e2011-08-08 19:01:25 -07002063 unsigned long long ptn = 0;
2064 unsigned long long size;
2065 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002066 uint8_t lun = 0;
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002067 uint32_t ret = 0;
Mayank Groverddd348d2018-01-23 14:07:09 +05302068 uint32_t device_info_sz = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07002069
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002070 if (devinfo_present)
2071 index = partition_get_index("devinfo");
2072 else
2073 index = partition_get_index("aboot");
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07002074
Shashank Mittal162244e2011-08-08 19:01:25 -07002075 ptn = partition_get_offset(index);
2076 if(ptn == 0)
2077 {
2078 return;
2079 }
2080
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002081 lun = partition_get_lun(index);
2082 mmc_set_lun(lun);
2083
Shashank Mittal162244e2011-08-08 19:01:25 -07002084 size = partition_get_size(index);
2085
Mayank Groverddd348d2018-01-23 14:07:09 +05302086 device_info_sz = ROUND_TO_PAGE(sizeof(struct device_info),
2087 mmc_blocksize_mask);
2088 if (device_info_sz == UINT_MAX)
2089 {
2090 dprintf(CRITICAL, "ERROR: Incorrect blocksize of card\n");
2091 return;
2092 }
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07002093
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002094 if (devinfo_present)
Mayank Groverddd348d2018-01-23 14:07:09 +05302095 ret = mmc_write(ptn, device_info_sz, (void *)info_buf);
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002096 else
Mayank Groverddd348d2018-01-23 14:07:09 +05302097 ret = mmc_write((ptn + size - device_info_sz), device_info_sz, (void *)info_buf);
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002098 if (ret)
Shashank Mittal162244e2011-08-08 19:01:25 -07002099 {
2100 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
Sridhar Parasurame1a97922015-07-27 15:31:22 -07002101 ASSERT(0);
Shashank Mittal162244e2011-08-08 19:01:25 -07002102 }
2103}
2104
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002105void read_device_info_mmc(struct device_info *info)
Shashank Mittal162244e2011-08-08 19:01:25 -07002106{
Shashank Mittal162244e2011-08-08 19:01:25 -07002107 unsigned long long ptn = 0;
2108 unsigned long long size;
2109 int index = INVALID_PTN;
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002110 uint32_t ret = 0;
Mayank Groverddd348d2018-01-23 14:07:09 +05302111 uint32_t device_info_sz = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07002112
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002113 if ((index = partition_get_index("devinfo")) < 0)
2114 {
2115 devinfo_present = false;
2116 index = partition_get_index("aboot");
2117 }
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07002118
Shashank Mittal162244e2011-08-08 19:01:25 -07002119 ptn = partition_get_offset(index);
2120 if(ptn == 0)
2121 {
2122 return;
2123 }
2124
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07002125 mmc_set_lun(partition_get_lun(index));
2126
Shashank Mittal162244e2011-08-08 19:01:25 -07002127 size = partition_get_size(index);
2128
Mayank Groverddd348d2018-01-23 14:07:09 +05302129 device_info_sz = ROUND_TO_PAGE(sizeof(struct device_info),
2130 mmc_blocksize_mask);
2131 if (device_info_sz == UINT_MAX)
2132 {
2133 dprintf(CRITICAL, "ERROR: Incorrect blocksize of card\n");
2134 return;
2135 }
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07002136
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002137 if (devinfo_present)
Mayank Groverddd348d2018-01-23 14:07:09 +05302138 ret = mmc_read(ptn, (void *)info_buf, device_info_sz);
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002139 else
Mayank Groverddd348d2018-01-23 14:07:09 +05302140 ret = mmc_read((ptn + size - device_info_sz), (void *)info_buf, device_info_sz);
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002141 if (ret)
Shashank Mittal162244e2011-08-08 19:01:25 -07002142 {
2143 dprintf(CRITICAL, "ERROR: Cannot read device info\n");
Sridhar Parasurame1a97922015-07-27 15:31:22 -07002144 ASSERT(0);
Shashank Mittal162244e2011-08-08 19:01:25 -07002145 }
Shashank Mittal162244e2011-08-08 19:01:25 -07002146}
2147
2148void write_device_info_flash(device_info *dev)
2149{
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002150 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
Shashank Mittal162244e2011-08-08 19:01:25 -07002151 struct ptentry *ptn;
2152 struct ptable *ptable;
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002153 if(info == NULL)
2154 {
2155 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
2156 ASSERT(0);
2157 }
2158 info_buf = info;
Shashank Mittal162244e2011-08-08 19:01:25 -07002159 ptable = flash_get_ptable();
2160 if (ptable == NULL)
2161 {
2162 dprintf(CRITICAL, "ERROR: Partition table not found\n");
2163 return;
2164 }
2165
2166 ptn = ptable_find(ptable, "devinfo");
2167 if (ptn == NULL)
2168 {
Smita Ghosh670c8b82015-05-07 09:30:03 -07002169 dprintf(CRITICAL, "ERROR: No devinfo partition found\n");
Shashank Mittal162244e2011-08-08 19:01:25 -07002170 return;
2171 }
2172
Mayank Groverdedbc892017-10-24 13:41:34 +05302173 memset(info, 0, BOOT_IMG_MAX_PAGE_SIZE);
Shashank Mittal162244e2011-08-08 19:01:25 -07002174 memcpy(info, dev, sizeof(device_info));
2175
2176 if (flash_write(ptn, 0, (void *)info_buf, page_size))
2177 {
2178 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
2179 return;
2180 }
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002181 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07002182}
2183
vijay kumarc65876c2015-04-24 13:29:16 +05302184static int read_allow_oem_unlock(device_info *dev)
2185{
vijay kumarc65876c2015-04-24 13:29:16 +05302186 unsigned offset;
2187 int index;
2188 unsigned long long ptn;
2189 unsigned long long ptn_size;
2190 unsigned blocksize = mmc_get_device_blocksize();
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07002191 STACKBUF_DMA_ALIGN(buf, blocksize);
vijay kumarc65876c2015-04-24 13:29:16 +05302192
vijay kumarca2e6812015-07-08 20:28:25 +05302193 index = partition_get_index(frp_ptns[0]);
vijay kumarc65876c2015-04-24 13:29:16 +05302194 if (index == INVALID_PTN)
2195 {
vijay kumarca2e6812015-07-08 20:28:25 +05302196 index = partition_get_index(frp_ptns[1]);
2197 if (index == INVALID_PTN)
2198 {
2199 dprintf(CRITICAL, "Neither '%s' nor '%s' partition found\n", frp_ptns[0],frp_ptns[1]);
2200 return -1;
2201 }
vijay kumarc65876c2015-04-24 13:29:16 +05302202 }
2203
2204 ptn = partition_get_offset(index);
2205 ptn_size = partition_get_size(index);
2206 offset = ptn_size - blocksize;
2207
Mayank Grover48860402016-11-29 12:34:53 +05302208 /* Set Lun for partition */
2209 mmc_set_lun(partition_get_lun(index));
2210
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07002211 if (mmc_read(ptn + offset, (void *)buf, blocksize))
vijay kumarc65876c2015-04-24 13:29:16 +05302212 {
2213 dprintf(CRITICAL, "Reading MMC failed\n");
2214 return -1;
2215 }
2216
2217 /*is_allow_unlock is a bool value stored at the LSB of last byte*/
2218 is_allow_unlock = buf[blocksize-1] & 0x01;
2219 return 0;
2220}
2221
2222static int write_allow_oem_unlock(bool allow_unlock)
2223{
vijay kumarc65876c2015-04-24 13:29:16 +05302224 unsigned offset;
vijay kumarc65876c2015-04-24 13:29:16 +05302225 int index;
2226 unsigned long long ptn;
2227 unsigned long long ptn_size;
2228 unsigned blocksize = mmc_get_device_blocksize();
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07002229 STACKBUF_DMA_ALIGN(buf, blocksize);
vijay kumarc65876c2015-04-24 13:29:16 +05302230
vijay kumarca2e6812015-07-08 20:28:25 +05302231 index = partition_get_index(frp_ptns[0]);
vijay kumarc65876c2015-04-24 13:29:16 +05302232 if (index == INVALID_PTN)
2233 {
vijay kumarca2e6812015-07-08 20:28:25 +05302234 index = partition_get_index(frp_ptns[1]);
2235 if (index == INVALID_PTN)
2236 {
2237 dprintf(CRITICAL, "Neither '%s' nor '%s' partition found\n", frp_ptns[0],frp_ptns[1]);
2238 return -1;
2239 }
vijay kumarc65876c2015-04-24 13:29:16 +05302240 }
2241
2242 ptn = partition_get_offset(index);
2243 ptn_size = partition_get_size(index);
2244 offset = ptn_size - blocksize;
Mayank Grover48860402016-11-29 12:34:53 +05302245 mmc_set_lun(partition_get_lun(index));
vijay kumarc65876c2015-04-24 13:29:16 +05302246
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07002247 if (mmc_read(ptn + offset, (void *)buf, blocksize))
vijay kumarc65876c2015-04-24 13:29:16 +05302248 {
2249 dprintf(CRITICAL, "Reading MMC failed\n");
2250 return -1;
2251 }
2252
2253 /*is_allow_unlock is a bool value stored at the LSB of last byte*/
2254 buf[blocksize-1] = allow_unlock;
2255 if (mmc_write(ptn + offset, blocksize, buf))
2256 {
2257 dprintf(CRITICAL, "Writing MMC failed\n");
2258 return -1;
2259 }
2260
2261 return 0;
2262}
2263
Shashank Mittal162244e2011-08-08 19:01:25 -07002264void read_device_info_flash(device_info *dev)
2265{
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002266 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
Shashank Mittal162244e2011-08-08 19:01:25 -07002267 struct ptentry *ptn;
2268 struct ptable *ptable;
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002269 if(info == NULL)
2270 {
2271 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
2272 ASSERT(0);
2273 }
2274 info_buf = info;
Shashank Mittal162244e2011-08-08 19:01:25 -07002275 ptable = flash_get_ptable();
2276 if (ptable == NULL)
2277 {
2278 dprintf(CRITICAL, "ERROR: Partition table not found\n");
2279 return;
2280 }
2281
2282 ptn = ptable_find(ptable, "devinfo");
2283 if (ptn == NULL)
2284 {
Smita Ghosh670c8b82015-05-07 09:30:03 -07002285 dprintf(CRITICAL, "ERROR: No devinfo partition found\n");
Shashank Mittal162244e2011-08-08 19:01:25 -07002286 return;
2287 }
2288
2289 if (flash_read(ptn, 0, (void *)info_buf, page_size))
2290 {
2291 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
2292 return;
2293 }
2294
2295 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
2296 {
Shashank Mittal162244e2011-08-08 19:01:25 -07002297 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
2298 info->is_unlocked = 0;
Shashank Mittala0032282011-08-26 14:50:11 -07002299 info->is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07002300 write_device_info_flash(info);
2301 }
2302 memcpy(dev, info, sizeof(device_info));
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002303 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07002304}
2305
2306void write_device_info(device_info *dev)
2307{
2308 if(target_is_emmc_boot())
2309 {
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002310 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
2311 if(info == NULL)
2312 {
2313 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
2314 ASSERT(0);
2315 }
2316 info_buf = info;
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002317 memcpy(info, dev, sizeof(struct device_info));
2318
2319#if USE_RPMB_FOR_DEVINFO
Mayank Grover912eaa62017-10-26 12:08:53 +05302320 if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05302321 is_secure_boot_enable()) {
2322 if((write_device_info_rpmb((void*) info, PAGE_SIZE)) < 0)
2323 ASSERT(0);
Sridhar Parasurame1a97922015-07-27 15:31:22 -07002324 }
Channagoud Kadabic80cb492015-04-28 16:08:28 -07002325 else
2326 write_device_info_mmc(info);
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002327#else
2328 write_device_info_mmc(info);
2329#endif
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002330 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07002331 }
2332 else
2333 {
2334 write_device_info_flash(dev);
2335 }
2336}
2337
2338void read_device_info(device_info *dev)
2339{
2340 if(target_is_emmc_boot())
2341 {
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002342 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
2343 if(info == NULL)
2344 {
2345 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
2346 ASSERT(0);
2347 }
2348 info_buf = info;
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002349
2350#if USE_RPMB_FOR_DEVINFO
Mayank Grover912eaa62017-10-26 12:08:53 +05302351 if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05302352 is_secure_boot_enable()) {
2353 if((read_device_info_rpmb((void*) info, PAGE_SIZE)) < 0)
2354 ASSERT(0);
Sridhar Parasurame1a97922015-07-27 15:31:22 -07002355 }
Channagoud Kadabic80cb492015-04-28 16:08:28 -07002356 else
2357 read_device_info_mmc(info);
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002358#else
2359 read_device_info_mmc(info);
2360#endif
2361
2362 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
2363 {
2364 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
lijuang511a2b52015-08-14 20:50:51 +08002365 if (is_secure_boot_enable()) {
Channagoud Kadabi05f78ba2015-07-06 11:58:14 -07002366 info->is_unlocked = 0;
Monika Singh292b3e92018-03-17 22:40:23 +05302367#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover912eaa62017-10-26 12:08:53 +05302368 if (VB_M <= target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +05302369 info->is_unlock_critical = 0;
Parth Dixitddbc7352015-10-18 03:13:31 +05302370#endif
lijuang511a2b52015-08-14 20:50:51 +08002371 } else {
Channagoud Kadabi2fda4092015-07-07 13:34:11 -07002372 info->is_unlocked = 1;
Monika Singh292b3e92018-03-17 22:40:23 +05302373#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover912eaa62017-10-26 12:08:53 +05302374 if (VB_M <= target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +05302375 info->is_unlock_critical = 1;
Parth Dixitddbc7352015-10-18 03:13:31 +05302376#endif
lijuang511a2b52015-08-14 20:50:51 +08002377 }
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002378 info->is_tampered = 0;
2379 info->charger_screen_enabled = 0;
Monika Singh292b3e92018-03-17 22:40:23 +05302380#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover912eaa62017-10-26 12:08:53 +05302381 if (VB_M <= target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +05302382 info->verity_mode = 1; //enforcing by default
Parth Dixitddbc7352015-10-18 03:13:31 +05302383#endif
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002384 write_device_info(info);
2385 }
2386 memcpy(dev, info, sizeof(device_info));
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002387 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07002388 }
2389 else
2390 {
2391 read_device_info_flash(dev);
2392 }
2393}
2394
2395void reset_device_info()
2396{
2397 dprintf(ALWAYS, "reset_device_info called.");
Shashank Mittala0032282011-08-26 14:50:11 -07002398 device.is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07002399 write_device_info(&device);
2400}
2401
2402void set_device_root()
2403{
2404 dprintf(ALWAYS, "set_device_root called.");
Shashank Mittala0032282011-08-26 14:50:11 -07002405 device.is_tampered = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -07002406 write_device_info(&device);
2407}
2408
lijuang4ece1e72015-08-14 21:02:36 +08002409/* set device unlock value
2410 * Must check FRP before call this function
2411 * Need to wipe data when unlock status changed
2412 * type 0: oem unlock
2413 * type 1: unlock critical
2414 * status 0: unlock as false
2415 * status 1: lock as true
2416 */
2417void set_device_unlock_value(int type, bool status)
lijuang21f12f52015-08-22 16:22:19 +08002418{
lijuang4ece1e72015-08-14 21:02:36 +08002419 if (type == UNLOCK)
2420 device.is_unlocked = status;
Monika Singh292b3e92018-03-17 22:40:23 +05302421#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover912eaa62017-10-26 12:08:53 +05302422 else if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05302423 type == UNLOCK_CRITICAL)
2424 device.is_unlock_critical = status;
Parth Dixitddbc7352015-10-18 03:13:31 +05302425#endif
lijuang4ece1e72015-08-14 21:02:36 +08002426 write_device_info(&device);
2427}
2428
2429static void set_device_unlock(int type, bool status)
2430{
2431 int is_unlocked = -1;
2432 char response[MAX_RSP_SIZE];
2433
2434 /* check device unlock status if it is as expected */
2435 if (type == UNLOCK)
2436 is_unlocked = device.is_unlocked;
Monika Singh292b3e92018-03-17 22:40:23 +05302437#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover912eaa62017-10-26 12:08:53 +05302438 if(VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05302439 type == UNLOCK_CRITICAL)
2440 {
2441 is_unlocked = device.is_unlock_critical;
2442 }
Parth Dixitddbc7352015-10-18 03:13:31 +05302443#endif
lijuang4ece1e72015-08-14 21:02:36 +08002444 if (is_unlocked == status) {
2445 snprintf(response, sizeof(response), "\tDevice already : %s", (status ? "unlocked!" : "locked!"));
2446 fastboot_info(response);
2447 fastboot_okay("");
2448 return;
2449 }
2450
2451 /* status is true, it means to unlock device */
2452 if (status) {
lijuang21f12f52015-08-22 16:22:19 +08002453 if(!is_allow_unlock) {
2454 fastboot_fail("oem unlock is not allowed");
2455 return;
2456 }
2457
lijuang4ece1e72015-08-14 21:02:36 +08002458#if FBCON_DISPLAY_MSG
lijuang9a7d3b92015-11-30 14:41:24 +08002459 display_unlock_menu(type);
lijuang4ece1e72015-08-14 21:02:36 +08002460 fastboot_okay("");
2461 return;
2462#else
2463 if (type == UNLOCK) {
2464 fastboot_fail("Need wipe userdata. Do 'fastboot oem unlock-go'");
2465 return;
2466 }
2467#endif
lijuang21f12f52015-08-22 16:22:19 +08002468 }
lijuang4ece1e72015-08-14 21:02:36 +08002469
2470 set_device_unlock_value(type, status);
2471
2472 /* wipe data */
2473 struct recovery_message msg;
Kishor PK60a68212017-05-08 16:55:57 +05302474 memset(&msg, 0, sizeof(msg));
lijuang4ece1e72015-08-14 21:02:36 +08002475 snprintf(msg.recovery, sizeof(msg.recovery), "recovery\n--wipe_data");
2476 write_misc(0, &msg, sizeof(msg));
2477
2478 fastboot_okay("");
2479 reboot_device(RECOVERY_MODE);
lijuang21f12f52015-08-22 16:22:19 +08002480}
2481
lijuang511a2b52015-08-14 20:50:51 +08002482static bool critical_flash_allowed(const char * entry)
2483{
2484 uint32_t i = 0;
2485 if (entry == NULL)
2486 return false;
2487
2488 for (i = 0; i < ARRAY_SIZE(critical_flash_allowed_ptn); i++) {
2489 if(!strcmp(entry, critical_flash_allowed_ptn[i]))
2490 return true;
2491 }
2492 return false;
Matthew Qin271927e2015-03-31 22:07:07 -04002493}
2494
2495#if DEVICE_TREE
Amol Jadicb524072012-08-09 16:40:18 -07002496int copy_dtb(uint8_t *boot_image_start, unsigned int scratch_offset)
2497{
2498 uint32 dt_image_offset = 0;
Amol Jadicb524072012-08-09 16:40:18 -07002499 uint32_t n;
Monika Singh292b3e92018-03-17 22:40:23 +05302500 struct dt_table *table = NULL;
Amol Jadicb524072012-08-09 16:40:18 -07002501 struct dt_entry dt_entry;
Monika Singh292b3e92018-03-17 22:40:23 +05302502 uint32_t dt_hdr_size = 0;
Amol Jadicb524072012-08-09 16:40:18 -07002503 unsigned int compressed_size = 0;
2504 unsigned int dtb_size = 0;
2505 unsigned int out_avai_len = 0;
2506 unsigned char *out_addr = NULL;
2507 unsigned char *best_match_dt_addr = NULL;
2508 int rc;
2509
2510 struct boot_img_hdr *hdr = (struct boot_img_hdr *) (boot_image_start);
2511
Parth Dixit4097b622016-03-15 14:42:27 +05302512#ifndef OSVERSION_IN_BOOTIMAGE
2513 dt_size = hdr->dt_size;
2514#endif
2515
2516 if(dt_size != 0) {
Amol Jadicb524072012-08-09 16:40:18 -07002517 /* add kernel offset */
2518 dt_image_offset += page_size;
2519 n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
2520 dt_image_offset += n;
2521
2522 /* add ramdisk offset */
2523 n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
2524 dt_image_offset += n;
2525
2526 /* add second offset */
2527 if(hdr->second_size != 0) {
2528 n = ROUND_TO_PAGE(hdr->second_size, page_mask);
2529 dt_image_offset += n;
2530 }
2531
2532 /* offset now point to start of dt.img */
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07002533 table = (struct dt_table*)(boot_image_start + dt_image_offset);
Amol Jadicb524072012-08-09 16:40:18 -07002534
Deepa Dinamani19648b42013-09-05 17:05:55 -07002535 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Amol Jadicb524072012-08-09 16:40:18 -07002536 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
2537 return -1;
2538 }
P.V. Phani Kumar82916762016-03-09 09:20:19 +05302539
2540 /* Its Error if, dt_hdr_size (table->num_entries * dt_entry size + Dev_Tree Header)
2541 goes beyound hdr->dt_size*/
Parth Dixit4097b622016-03-15 14:42:27 +05302542 if (dt_hdr_size > ROUND_TO_PAGE(dt_size,hdr->page_size)) {
P.V. Phani Kumar82916762016-03-09 09:20:19 +05302543 dprintf(CRITICAL, "ERROR: Invalid Device Tree size \n");
2544 return -1;
2545 }
2546
Joel Kingaa335dc2013-06-03 16:11:08 -07002547 /* Find index of device tree within device tree table */
2548 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Amol Jadicb524072012-08-09 16:40:18 -07002549 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
2550 return -1;
2551 }
2552
Matthew Qin271927e2015-03-31 22:07:07 -04002553 best_match_dt_addr = (unsigned char *)boot_image_start + dt_image_offset + dt_entry.offset;
2554 if (is_gzip_package(best_match_dt_addr, dt_entry.size))
2555 {
2556 out_addr = (unsigned char *)target_get_scratch_address() + scratch_offset;
2557 out_avai_len = target_get_max_flash_size() - scratch_offset;
Matthew Qin0b15b322015-05-19 05:20:54 -04002558 dprintf(INFO, "decompressing dtb: start\n");
Matthew Qin271927e2015-03-31 22:07:07 -04002559 rc = decompress(best_match_dt_addr,
2560 dt_entry.size, out_addr, out_avai_len,
2561 &compressed_size, &dtb_size);
2562 if (rc)
2563 {
Matthew Qin0b15b322015-05-19 05:20:54 -04002564 dprintf(CRITICAL, "decompressing dtb failed!!!\n");
Matthew Qin271927e2015-03-31 22:07:07 -04002565 ASSERT(0);
2566 }
2567
Matthew Qin0b15b322015-05-19 05:20:54 -04002568 dprintf(INFO, "decompressing dtb: done\n");
Matthew Qin271927e2015-03-31 22:07:07 -04002569 best_match_dt_addr = out_addr;
2570 } else {
2571 dtb_size = dt_entry.size;
2572 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002573 /* Validate and Read device device tree in the "tags_add */
Mayank Grovere559cec2017-10-17 15:12:03 +05302574 if (check_aboot_addr_range_overlap(hdr->tags_addr, dtb_size) ||
2575 check_ddr_addr_range_bound(hdr->tags_addr, dtb_size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002576 {
Mayank Grovere559cec2017-10-17 15:12:03 +05302577 dprintf(CRITICAL, "Device tree addresses are not valid.\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002578 return -1;
2579 }
2580
Amol Jadicb524072012-08-09 16:40:18 -07002581 /* Read device device tree in the "tags_add */
Matthew Qin271927e2015-03-31 22:07:07 -04002582 memmove((void*) hdr->tags_addr, (void *)best_match_dt_addr, dtb_size);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002583 } else
2584 return -1;
Amol Jadicb524072012-08-09 16:40:18 -07002585
2586 /* Everything looks fine. Return success. */
2587 return 0;
2588}
2589#endif
2590
Brian Swetland9c4c0752009-01-25 16:23:50 -08002591void cmd_boot(const char *arg, void *data, unsigned sz)
2592{
2593 unsigned kernel_actual;
2594 unsigned ramdisk_actual;
Kishor PKd8ddcad2017-07-27 13:53:57 +05302595 unsigned second_actual;
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002596 uint32_t image_actual;
2597 uint32_t dt_actual = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08002598 struct boot_img_hdr *hdr = NULL;
2599 struct kernel64_hdr *kptr = NULL;
Brian Swetland9c4c0752009-01-25 16:23:50 -08002600 char *ptr = ((char*) data);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002601 int ret = 0;
2602 uint8_t dtb_copied = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08002603 unsigned int out_len = 0;
2604 unsigned int out_avai_len = 0;
2605 unsigned char *out_addr = NULL;
2606 uint32_t dtb_offset = 0;
2607 unsigned char *kernel_start_addr = NULL;
2608 unsigned int kernel_size = 0;
Matthew Qin271927e2015-03-31 22:07:07 -04002609 unsigned int scratch_offset = 0;
Monika Singh292b3e92018-03-17 22:40:23 +05302610#if !VERIFIED_BOOT_2
2611 uint32_t sig_actual = 0;
2612 uint32_t sig_size = 0;
2613#ifdef MDTP_SUPPORT
2614 static bool is_mdtp_activated = 0;
2615#endif /* MDTP_SUPPORT */
2616#endif
Matthew Qinbb7923d2015-02-09 10:56:09 +08002617
lijuang2008ff22016-03-07 17:56:27 +08002618#if FBCON_DISPLAY_MSG
2619 /* Exit keys' detection thread firstly */
2620 exit_menu_keys_detection();
2621#endif
2622
Monika Singh292b3e92018-03-17 22:40:23 +05302623#if VERIFIED_BOOT || VERIFIED_BOOT_2
Channagoud Kadabi6d5375e2015-06-23 17:15:42 -07002624 if(target_build_variant_user() && !device.is_unlocked)
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002625 {
2626 fastboot_fail("unlock device to use this command");
lijuang2008ff22016-03-07 17:56:27 +08002627 goto boot_failed;
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002628 }
2629#endif
2630
Brian Swetland9c4c0752009-01-25 16:23:50 -08002631 if (sz < sizeof(hdr)) {
2632 fastboot_fail("invalid bootimage header");
lijuang2008ff22016-03-07 17:56:27 +08002633 goto boot_failed;
Brian Swetland9c4c0752009-01-25 16:23:50 -08002634 }
2635
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002636 hdr = (struct boot_img_hdr *)data;
Brian Swetland9c4c0752009-01-25 16:23:50 -08002637
2638 /* ensure commandline is terminated */
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002639 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -08002640
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002641 if(target_is_emmc_boot() && hdr->page_size) {
2642 page_size = hdr->page_size;
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07002643 page_mask = page_size - 1;
2644 }
2645
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002646 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
2647 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Kishor PKd8ddcad2017-07-27 13:53:57 +05302648 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002649#if DEVICE_TREE
Parth Dixit4097b622016-03-15 14:42:27 +05302650#ifndef OSVERSION_IN_BOOTIMAGE
2651 dt_size = hdr->dt_size;
2652#endif
2653 dt_actual = ROUND_TO_PAGE(dt_size, page_mask);
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002654#endif
2655
2656 image_actual = ADD_OF(page_size, kernel_actual);
2657 image_actual = ADD_OF(image_actual, ramdisk_actual);
Kishor PKd8ddcad2017-07-27 13:53:57 +05302658 image_actual = ADD_OF(image_actual, second_actual);
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002659 image_actual = ADD_OF(image_actual, dt_actual);
2660
Kishor PK5134b332017-05-09 17:50:08 +05302661 /* Checking to prevent oob access in read_der_message_length */
2662 if (image_actual > sz) {
2663 fastboot_fail("bootimage header fields are invalid");
2664 goto boot_failed;
2665 }
Monika Singh292b3e92018-03-17 22:40:23 +05302666#if VERIFIED_BOOT_2
2667 memset(&info, 0, sizeof(bootinfo));
2668 info.images[0].image_buffer = data;
2669 info.images[0].imgsize = image_actual;
2670 info.images[0].name = "boot";
2671 info.num_loaded_images = 1;
2672 info.multi_slot_boot = partition_multislot_is_supported();
2673 if (load_image_and_auth(&info))
2674 goto boot_failed;
2675 vbcmdline = info.vbcmdline;
2676#else
Kishor PK5134b332017-05-09 17:50:08 +05302677 sig_size = sz - image_actual;
2678
P.V. Phani Kumareecfd822016-03-09 20:09:03 +05302679 if (target_use_signed_kernel() && (!device.is_unlocked)) {
Monika Singh292b3e92018-03-17 22:40:23 +05302680 unsigned chk;
P.V. Phani Kumareecfd822016-03-09 20:09:03 +05302681 /* Calculate the signature length from boot image */
2682 sig_actual = read_der_message_length(
Kishor PK5134b332017-05-09 17:50:08 +05302683 (unsigned char*)(data + image_actual), sig_size);
Monika Singh292b3e92018-03-17 22:40:23 +05302684 chk = ADD_OF(image_actual, sig_actual);
Channagoud Kadabi3d839012014-06-26 14:26:39 -07002685
Monika Singh292b3e92018-03-17 22:40:23 +05302686 if (chk > sz) {
Kishor PK5134b332017-05-09 17:50:08 +05302687 fastboot_fail("bootimage header fields are invalid");
2688 goto boot_failed;
2689 }
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002690 }
2691
Sridhar Parasuram361e4cd2015-09-24 11:19:19 -07002692 // Initialize boot state before trying to verify boot.img
2693#if VERIFIED_BOOT
Channagoud Kadabi699466e2015-11-03 12:37:42 -08002694 boot_verifier_init();
Mayank Groverb337e932017-01-18 20:00:40 +05302695#endif
Channagoud Kadabibf6ce7d2015-09-17 13:25:35 -07002696 /* Handle overflow if the input image size is greater than
2697 * boot image buffer can hold
2698 */
Monika Singh292b3e92018-03-17 22:40:23 +05302699 if ((target_get_max_flash_size() - page_size) < image_actual)
Channagoud Kadabibf6ce7d2015-09-17 13:25:35 -07002700 {
2701 fastboot_fail("booimage: size is greater than boot image buffer can hold");
lijuang2008ff22016-03-07 17:56:27 +08002702 goto boot_failed;
Channagoud Kadabibf6ce7d2015-09-17 13:25:35 -07002703 }
Sridhar Parasuram361e4cd2015-09-24 11:19:19 -07002704
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002705 /* Verify the boot image
2706 * device & page_size are initialized in aboot_init
2707 */
Sridhar Parasuram7ae9fbc2015-09-17 09:56:30 -07002708 if (target_use_signed_kernel() && (!device.is_unlocked)) {
Channagoud Kadabi3d839012014-06-26 14:26:39 -07002709 /* Pass size excluding signature size, otherwise we would try to
2710 * access signature beyond its length
2711 */
Monika Singh292b3e92018-03-17 22:40:23 +05302712 verify_signed_bootimg((uint32_t)data, image_actual);
Sridhar Parasuram7ae9fbc2015-09-17 09:56:30 -07002713 }
Amit Blay4aa292f2015-04-28 21:55:59 +03002714#ifdef MDTP_SUPPORT
2715 else
2716 {
Shay Nachmani062aa3f2015-05-17 17:28:44 +03002717 /* fastboot boot is not allowed when MDTP is activated */
Amit Blay4aa292f2015-04-28 21:55:59 +03002718 mdtp_ext_partition_verification_t ext_partition;
Amit Blay8a510302015-08-17 09:20:01 +03002719
2720 if (!is_mdtp_activated) {
2721 ext_partition.partition = MDTP_PARTITION_NONE;
2722 mdtp_fwlock_verify_lock(&ext_partition);
2723 }
Amit Blay4aa292f2015-04-28 21:55:59 +03002724 }
Shay Nachmani062aa3f2015-05-17 17:28:44 +03002725
Amir Kotzer7c768c02016-04-13 09:08:05 +03002726 /* If mdtp state cannot be validate, block fastboot boot*/
2727 if(mdtp_activated(&is_mdtp_activated)){
2728 dprintf(CRITICAL, "mdtp_activated cannot validate state.\n");
2729 dprintf(CRITICAL, "Can not proceed with fastboot boot command.\n");
2730 goto boot_failed;
2731 }
Shay Nachmani062aa3f2015-05-17 17:28:44 +03002732 if(is_mdtp_activated){
2733 dprintf(CRITICAL, "fastboot boot command is not available.\n");
lijuang2008ff22016-03-07 17:56:27 +08002734 goto boot_failed;
Shay Nachmani062aa3f2015-05-17 17:28:44 +03002735 }
Amit Blay4aa292f2015-04-28 21:55:59 +03002736#endif /* MDTP_SUPPORT */
Monika Singh292b3e92018-03-17 22:40:23 +05302737#endif /* VERIFIED_BOOT_2 else */
Amit Blay4aa292f2015-04-28 21:55:59 +03002738
Sridhar Parasuram361e4cd2015-09-24 11:19:19 -07002739#if VERIFIED_BOOT
Monika Singh292b3e92018-03-17 22:40:23 +05302740 if (VB_M == target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +05302741 {
2742 /* set boot and system versions. */
2743 set_os_version((unsigned char *)data);
2744 // send root of trust
2745 if(!send_rot_command((uint32_t)device.is_unlocked))
2746 ASSERT(0);
2747 }
Parth Dixitbc9b6492015-10-18 00:41:38 +05302748#endif
Channagoud Kadabia22144f2013-03-20 11:49:01 -07002749 /*
Matthew Qinbb7923d2015-02-09 10:56:09 +08002750 * Check if the kernel image is a gzip package. If yes, need to decompress it.
2751 * If not, continue booting.
2752 */
2753 if (is_gzip_package((unsigned char *)(data + page_size), hdr->kernel_size))
2754 {
2755 out_addr = (unsigned char *)target_get_scratch_address();
2756 out_addr = (unsigned char *)(out_addr + image_actual + page_size);
2757 out_avai_len = target_get_max_flash_size() - image_actual - page_size;
Matthew Qin0b15b322015-05-19 05:20:54 -04002758 dprintf(INFO, "decompressing kernel image: start\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08002759 ret = decompress((unsigned char *)(ptr + page_size),
2760 hdr->kernel_size, out_addr, out_avai_len,
2761 &dtb_offset, &out_len);
2762 if (ret)
2763 {
Matthew Qin0b15b322015-05-19 05:20:54 -04002764 dprintf(CRITICAL, "decompressing image failed!!!\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08002765 ASSERT(0);
2766 }
2767
Matthew Qin0b15b322015-05-19 05:20:54 -04002768 dprintf(INFO, "decompressing kernel image: done\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08002769 kptr = (struct kernel64_hdr *)out_addr;
2770 kernel_start_addr = out_addr;
2771 kernel_size = out_len;
2772 } else {
2773 kptr = (struct kernel64_hdr*)((char *)data + page_size);
2774 kernel_start_addr = (unsigned char *)((char *)data + page_size);
2775 kernel_size = hdr->kernel_size;
2776 }
2777
2778 /*
Channagoud Kadabia22144f2013-03-20 11:49:01 -07002779 * Update the kernel/ramdisk/tags address if the boot image header
2780 * has default values, these default values come from mkbootimg when
2781 * the boot image is flashed using fastboot flash:raw
2782 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08002783 update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr));
Dima Zavin3cadfff2013-03-21 14:30:48 -07002784
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002785 /* Get virtual addresses since the hdr saves physical addresses. */
2786 hdr->kernel_addr = VA(hdr->kernel_addr);
2787 hdr->ramdisk_addr = VA(hdr->ramdisk_addr);
2788 hdr->tags_addr = VA(hdr->tags_addr);
Brian Swetland9c4c0752009-01-25 16:23:50 -08002789
Matthew Qinbb7923d2015-02-09 10:56:09 +08002790 kernel_size = ROUND_TO_PAGE(kernel_size, page_mask);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002791 /* Check if the addresses in the header are valid. */
Matthew Qinbb7923d2015-02-09 10:56:09 +08002792 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_size) ||
Mayank Grovere559cec2017-10-17 15:12:03 +05302793 check_ddr_addr_range_bound(hdr->kernel_addr, kernel_size) ||
2794 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual) ||
2795 check_ddr_addr_range_bound(hdr->ramdisk_addr, ramdisk_actual))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002796 {
Mayank Grovere559cec2017-10-17 15:12:03 +05302797 dprintf(CRITICAL, "kernel/ramdisk addresses are not valid.\n");
lijuang2008ff22016-03-07 17:56:27 +08002798 goto boot_failed;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002799 }
2800
Amol Jadicb524072012-08-09 16:40:18 -07002801#if DEVICE_TREE
Matthew Qin271927e2015-03-31 22:07:07 -04002802 scratch_offset = image_actual + page_size + out_len;
Amol Jadicb524072012-08-09 16:40:18 -07002803 /* find correct dtb and copy it to right location */
Matthew Qin271927e2015-03-31 22:07:07 -04002804 ret = copy_dtb(data, scratch_offset);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002805
2806 dtb_copied = !ret ? 1 : 0;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002807#else
Mayank Grovere559cec2017-10-17 15:12:03 +05302808 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE) ||
2809 check_ddr_addr_range_bound(hdr->tags_addr, MAX_TAGS_SIZE))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002810 {
Mayank Grovere559cec2017-10-17 15:12:03 +05302811 dprintf(CRITICAL, "Tags addresses are not valid.\n");
lijuang2008ff22016-03-07 17:56:27 +08002812 goto boot_failed;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002813 }
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002814#endif
2815
2816 /* Load ramdisk & kernel */
2817 memmove((void*) hdr->ramdisk_addr, ptr + page_size + kernel_actual, hdr->ramdisk_size);
Matthew Qinbb7923d2015-02-09 10:56:09 +08002818 memmove((void*) hdr->kernel_addr, (char*) (kernel_start_addr), kernel_size);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002819
2820#if DEVICE_TREE
Mayank Grovere559cec2017-10-17 15:12:03 +05302821 if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual) ||
2822 check_ddr_addr_range_bound(hdr->tags_addr, kernel_actual))
Matthew Qinbb7923d2015-02-09 10:56:09 +08002823 {
Mayank Grovere559cec2017-10-17 15:12:03 +05302824 dprintf(CRITICAL, "Tags addresses are not valid.\n");
lijuang2008ff22016-03-07 17:56:27 +08002825 goto boot_failed;
Matthew Qinbb7923d2015-02-09 10:56:09 +08002826 }
2827
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002828 /*
2829 * If dtb is not found look for appended DTB in the kernel.
2830 * If appended dev tree is found, update the atags with
2831 * memory address to the DTB appended location on RAM.
2832 * Else update with the atags address in the kernel header
2833 */
2834 if (!dtb_copied) {
2835 void *dtb;
Matthew Qinbb7923d2015-02-09 10:56:09 +08002836 dtb = dev_tree_appended((void*)(ptr + page_size),
2837 hdr->kernel_size, dtb_offset,
Dima Zavine63e5572013-05-03 12:23:06 -07002838 (void *)hdr->tags_addr);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002839 if (!dtb) {
2840 fastboot_fail("dtb not found");
lijuang2008ff22016-03-07 17:56:27 +08002841 goto boot_failed;
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002842 }
Amol Jadicb524072012-08-09 16:40:18 -07002843 }
2844#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08002845
2846 fastboot_okay("");
Amol Jadi7c4316c2013-10-07 14:19:26 -07002847 fastboot_stop();
Brian Swetland9c4c0752009-01-25 16:23:50 -08002848
Dima Zavin77e41f32013-03-06 16:10:43 -08002849 boot_linux((void*) hdr->kernel_addr, (void*) hdr->tags_addr,
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002850 (const char*) hdr->cmdline, board_machtype(),
2851 (void*) hdr->ramdisk_addr, hdr->ramdisk_size);
lijuang2008ff22016-03-07 17:56:27 +08002852
2853 /* fastboot already stop, it's no need to show fastboot menu */
2854 return;
2855boot_failed:
2856#if FBCON_DISPLAY_MSG
2857 /* revert to fastboot menu if boot failed */
2858 display_fastboot_menu();
2859#endif
2860 return;
Brian Swetland9c4c0752009-01-25 16:23:50 -08002861}
2862
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002863void cmd_erase_nand(const char *arg, void *data, unsigned sz)
Dima Zavin214cc642009-01-26 11:16:21 -08002864{
2865 struct ptentry *ptn;
2866 struct ptable *ptable;
2867
2868 ptable = flash_get_ptable();
2869 if (ptable == NULL) {
2870 fastboot_fail("partition table doesn't exist");
2871 return;
2872 }
2873
2874 ptn = ptable_find(ptable, arg);
2875 if (ptn == NULL) {
2876 fastboot_fail("unknown partition name");
2877 return;
2878 }
2879
Monika Singh292b3e92018-03-17 22:40:23 +05302880 if (!strncmp(arg, "avb_custom_key", strlen("avb_custom_key"))) {
2881 dprintf(INFO, "erasing avb_custom_key\n");
2882 if (erase_userkey()) {
2883 fastboot_fail("Erasing avb_custom_key failed");
2884 } else {
2885 fastboot_okay("");
2886 }
2887 return;
2888 }
2889
Dima Zavin214cc642009-01-26 11:16:21 -08002890 if (flash_erase(ptn)) {
2891 fastboot_fail("failed to erase partition");
2892 return;
2893 }
2894 fastboot_okay("");
2895}
2896
Bikas Gurungd48bd242010-09-04 19:54:32 -07002897
2898void cmd_erase_mmc(const char *arg, void *data, unsigned sz)
2899{
2900 unsigned long long ptn = 0;
Oliver Wangcee448d2013-10-22 18:40:13 +08002901 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07002902 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002903 uint8_t lun = 0;
Vijay Kumar Pendoti0b21f462016-05-02 17:09:18 +05302904 char *footer = NULL;
Bikas Gurungd48bd242010-09-04 19:54:32 -07002905
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002906#if VERIFIED_BOOT
2907 if(!strcmp(arg, KEYSTORE_PTN_NAME))
2908 {
2909 if(!device.is_unlocked)
2910 {
2911 fastboot_fail("unlock device to erase keystore");
2912 return;
2913 }
2914 }
2915#endif
2916
Kinson Chikf1a43512011-07-14 11:28:39 -07002917 index = partition_get_index(arg);
2918 ptn = partition_get_offset(index);
Oliver Wangcee448d2013-10-22 18:40:13 +08002919 size = partition_get_size(index);
Neeti Desaica8c9602011-10-06 11:40:00 -07002920
Kinson Chikf1a43512011-07-14 11:28:39 -07002921 if(ptn == 0) {
Neeti Desaica8c9602011-10-06 11:40:00 -07002922 fastboot_fail("Partition table doesn't exist\n");
Bikas Gurungd48bd242010-09-04 19:54:32 -07002923 return;
2924 }
Kun Liang2f1601a2013-08-12 16:29:54 +08002925
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002926 lun = partition_get_lun(index);
2927 mmc_set_lun(lun);
2928
Monika Singh292b3e92018-03-17 22:40:23 +05302929 if (!strncmp(arg, "avb_custom_key", strlen("avb_custom_key"))) {
2930 dprintf(INFO, "erasing avb_custom_key\n");
2931 if (erase_userkey()) {
2932 fastboot_fail("Erasing avb_custom_key failed");
2933 } else {
2934 fastboot_okay("");
2935 }
2936 return;
2937 }
Sridhar Parasuramd7957122015-02-27 11:33:40 -08002938 if (platform_boot_dev_isemmc())
2939 {
2940 if (mmc_erase_card(ptn, size)) {
2941 fastboot_fail("failed to erase partition\n");
2942 return;
2943 }
2944 } else {
2945 BUF_DMA_ALIGN(out, DEFAULT_ERASE_SIZE);
2946 size = partition_get_size(index);
2947 if (size > DEFAULT_ERASE_SIZE)
2948 size = DEFAULT_ERASE_SIZE;
Kun Liang2f1601a2013-08-12 16:29:54 +08002949
Sridhar Parasuramd7957122015-02-27 11:33:40 -08002950 /* Simple inefficient version of erase. Just writing
2951 0 in first several blocks */
2952 if (mmc_write(ptn , size, (unsigned int *)out)) {
2953 fastboot_fail("failed to erase partition");
2954 return;
2955 }
Vijay Kumar Pendoti0b21f462016-05-02 17:09:18 +05302956 /*Erase FDE metadata at the userdata footer*/
2957 if(!(strncmp(arg, "userdata", 8)))
2958 {
2959 footer = memalign(CACHE_LINE, FOOTER_SIZE);
2960 memset((void *)footer, 0, FOOTER_SIZE);
2961
2962 size = partition_get_size(index);
2963
2964 if (mmc_write((ptn + size) - FOOTER_SIZE , FOOTER_SIZE, (unsigned int *)footer)) {
2965 fastboot_fail("failed to erase userdata footer");
2966 free(footer);
2967 return;
2968 }
2969 free(footer);
2970 }
Bikas Gurungd48bd242010-09-04 19:54:32 -07002971 }
Monika Singh292b3e92018-03-17 22:40:23 +05302972#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover912eaa62017-10-26 12:08:53 +05302973 if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05302974 !(strncmp(arg, "userdata", 8)) &&
2975 send_delete_keys_to_tz())
Sridhar Parasuram32b30662015-07-10 13:33:22 -07002976 ASSERT(0);
2977#endif
Bikas Gurungd48bd242010-09-04 19:54:32 -07002978 fastboot_okay("");
2979}
2980
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002981void cmd_erase(const char *arg, void *data, unsigned sz)
2982{
Monika Singh292b3e92018-03-17 22:40:23 +05302983#if VERIFIED_BOOT || VERIFIED_BOOT_2
Channagoud Kadabi35297672015-06-13 11:09:49 -07002984 if (target_build_variant_user())
Channagoud Kadabice3cf422015-04-17 16:02:30 -07002985 {
Sridhar Parasuramc32d07d2015-07-12 10:57:48 -07002986 if(!device.is_unlocked)
Channagoud Kadabice3cf422015-04-17 16:02:30 -07002987 {
Channagoud Kadabi35297672015-06-13 11:09:49 -07002988 fastboot_fail("device is locked. Cannot erase");
Channagoud Kadabice3cf422015-04-17 16:02:30 -07002989 return;
2990 }
2991 }
2992#endif
2993
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002994 if(target_is_emmc_boot())
2995 cmd_erase_mmc(arg, data, sz);
2996 else
2997 cmd_erase_nand(arg, data, sz);
2998}
Bikas Gurungd48bd242010-09-04 19:54:32 -07002999
Mayank Grover11ff9692018-01-11 11:54:49 +05303000/* Get the size from partiton name */
3001static void get_partition_size(const char *arg, char *response)
3002{
3003 uint64_t ptn = 0;
3004 uint64_t size;
3005 int index = INVALID_PTN;
3006
3007 index = partition_get_index(arg);
3008
3009 if (index == INVALID_PTN)
3010 {
3011 dprintf(CRITICAL, "Invalid partition index\n");
3012 return;
3013 }
3014
3015 ptn = partition_get_offset(index);
3016
3017 if(!ptn)
3018 {
3019 dprintf(CRITICAL, "Invalid partition name %s\n", arg);
3020 return;
3021 }
3022
3023 size = partition_get_size(index);
3024
3025 snprintf(response, MAX_RSP_SIZE, "\t 0x%llx", size);
3026 return;
3027}
3028
Mayank Grover52cd10a2018-03-15 12:57:54 +05303029/* Function to check partition type of a partition*/
3030static fs_signature_type
3031check_partition_fs_signature(const char *arg)
3032{
3033 fs_signature_type ret = NO_FS;
3034 int index;
3035 unsigned long long ptn;
3036 char *sb_buffer = malloc(mmc_blocksize);
3037 if (!sb_buffer)
3038 {
3039 dprintf(CRITICAL, "ERROR: Failed to allocate buffer for superblock\n");
3040 goto out;
3041 }
3042
3043 /* Read super block */
3044 if ((index = partition_get_index(arg)) < 0)
3045 {
3046 dprintf(CRITICAL, "ERROR: %s() doesn't exsit\n", arg);
3047 goto out;
3048 }
3049 ptn = partition_get_offset(index);
3050 mmc_set_lun(partition_get_lun(index));
3051 if(mmc_read(ptn + FS_SUPERBLOCK_OFFSET,
3052 (void *)sb_buffer, mmc_blocksize))
3053 {
3054 dprintf(CRITICAL, "ERROR: Failed to read Superblock\n");
3055 goto out;
3056 }
3057
3058 if (*((uint16 *)(&sb_buffer[EXT_MAGIC_OFFSET_SB]))
3059 == (uint16)EXT_MAGIC)
3060 {
3061 dprintf(SPEW, "%s() Found EXT FS\n", arg);
3062 ret = EXT_FS_SIGNATURE;
3063 }
3064 else if (*((uint32 *)(&sb_buffer[F2FS_MAGIC_OFFSET_SB]))
3065 == F2FS_MAGIC)
3066 {
3067 dprintf(SPEW, "%s() Found F2FS FS\n", arg);
3068 ret = EXT_F2FS_SIGNATURE;
3069 }
3070 else
3071 {
3072 dprintf(SPEW, "%s() Reverting to default 0x%x\n",
3073 arg, *((uint16 *)(&sb_buffer[EXT_MAGIC_OFFSET_SB])));
3074 ret = NO_FS;
3075 }
3076
3077out:
3078 if(sb_buffer)
3079 free(sb_buffer);
3080 return ret;
3081}
3082
Mayank Groverd38fe012018-03-13 15:33:16 +05303083/* Function to get partition type */
3084static void get_partition_type(const char *arg, char *response)
3085{
3086 uint n = 0;
Mayank Grover52cd10a2018-03-15 12:57:54 +05303087 fs_signature_type fs_signature;
Mayank Groverd38fe012018-03-13 15:33:16 +05303088
3089 if (arg == NULL ||
3090 response == NULL)
3091 {
3092 dprintf(CRITICAL, "Invalid input parameter\n");
3093 return;
3094 }
3095
3096 /* By default copy raw to response */
3097 strncpy(response, RAW_STR, strlen(RAW_STR));
3098
3099 /* Mark partiton type for known paritions only */
3100 for (n=0; n < ARRAY_SIZE(part_type_known); n++)
3101 {
3102 if (!strncmp(part_type_known[n].part_name, arg, strlen(arg)))
3103 {
Mayank Grover52cd10a2018-03-15 12:57:54 +05303104 /* Check partition for FS signature */
3105 fs_signature = check_partition_fs_signature(arg);
3106 switch (fs_signature)
3107 {
3108 case EXT_FS_SIGNATURE:
3109 strncpy(response, EXT_STR, strlen(EXT_STR));
3110 break;
3111 case EXT_F2FS_SIGNATURE:
3112 strncpy(response, F2FS_STR, strlen(F2FS_STR));
3113 break;
3114 case NO_FS:
3115 strncpy(response, part_type_known[n].type_response, MAX_RSP_SIZE);
3116 }
Mayank Groverd38fe012018-03-13 15:33:16 +05303117 }
3118 }
3119 return;
3120}
3121
Mayank Grover11ff9692018-01-11 11:54:49 +05303122/*
3123 * Publish the partition type & size info
3124 * fastboot getvar will publish the required information.
3125 * fastboot getvar partition_size:<partition_name>: partition size in hex
3126 * fastboot getvar partition_type:<partition_name>: partition type (ext/fat)
3127 */
3128static void publish_getvar_partition_info(struct getvar_partition_info *info, uint8_t num_parts)
3129{
Mayank Groverd38fe012018-03-13 15:33:16 +05303130 uint8_t i;
Mayank Grover11ff9692018-01-11 11:54:49 +05303131 static bool published = false;
3132 struct partition_entry *ptn_entry =
3133 partition_get_partition_entries();
3134 memset(info, 0, sizeof(struct getvar_partition_info)* num_parts);
3135
3136 for (i = 0; i < num_parts; i++) {
3137 strlcat(info[i].part_name, (const char *)ptn_entry[i].name, MAX_RSP_SIZE);
3138 strlcat(info[i].getvar_size, "partition-size:", MAX_GET_VAR_NAME_SIZE);
3139 strlcat(info[i].getvar_type, "partition-type:", MAX_GET_VAR_NAME_SIZE);
3140
Mayank Groverd38fe012018-03-13 15:33:16 +05303141 get_partition_type(info[i].part_name, info[i].type_response);
Mayank Grover11ff9692018-01-11 11:54:49 +05303142 get_partition_size(info[i].part_name, info[i].size_response);
Mayank Grover11ff9692018-01-11 11:54:49 +05303143 if (strlcat(info[i].getvar_size, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
3144 {
3145 dprintf(CRITICAL, "partition size name truncated\n");
3146 return;
3147 }
3148 if (strlcat(info[i].getvar_type, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
3149 {
3150 dprintf(CRITICAL, "partition type name truncated\n");
3151 return;
3152 }
3153
3154 if (!published)
3155 {
3156 /* publish partition size & type info */
3157 fastboot_publish((const char *) info[i].getvar_size, (const char *) info[i].size_response);
3158 fastboot_publish((const char *) info[i].getvar_type, (const char *) info[i].type_response);
3159 }
3160 }
3161 if (!published)
3162 published = true;
3163}
3164
3165
Ajay Dudani5c761132011-04-07 20:19:04 -07003166void cmd_flash_mmc_img(const char *arg, void *data, unsigned sz)
Shashank Mittal23b8f422010-04-16 19:27:21 -07003167{
3168 unsigned long long ptn = 0;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07003169 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07003170 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003171 char *token = NULL;
3172 char *pname = NULL;
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07003173 char *sp;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003174 uint8_t lun = 0;
3175 bool lun_set = false;
Mayank Grover351a75e2017-05-30 20:06:08 +05303176 int current_active_slot = INVALID;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07003177
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07003178 token = strtok_r((char *)arg, ":", &sp);
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003179 pname = token;
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07003180 token = strtok_r(NULL, ":", &sp);
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003181 if(token)
3182 {
3183 lun = atoi(token);
3184 mmc_set_lun(lun);
3185 lun_set = true;
3186 }
3187
Mao Jinlong226f33a2014-07-04 17:24:10 +08003188 if (pname)
Greg Grisco6e754772011-06-23 12:19:39 -07003189 {
Channagoud Kadabiad259832015-05-29 11:14:17 -07003190 if (!strncmp(pname, "frp-unlock", strlen("frp-unlock")))
3191 {
3192 if (!aboot_frp_unlock(pname, data, sz))
3193 {
3194 fastboot_info("FRP unlock successful");
3195 fastboot_okay("");
3196 }
3197 else
3198 fastboot_fail("Secret key is invalid, please update the bootloader with secret key");
3199
3200 return;
3201 }
3202
Mao Jinlong226f33a2014-07-04 17:24:10 +08003203 if (!strcmp(pname, "partition"))
3204 {
3205 dprintf(INFO, "Attempt to write partition image.\n");
3206 if (write_partition(sz, (unsigned char *) data)) {
3207 fastboot_fail("failed to write partition");
Greg Grisco6e754772011-06-23 12:19:39 -07003208 return;
3209 }
Mayank Grover11ff9692018-01-11 11:54:49 +05303210 /* Re-publish partition table */
3211 publish_getvar_partition_info(part_info, partition_get_partition_count());
Mayank Grover351a75e2017-05-30 20:06:08 +05303212
3213 /* Rescan partition table to ensure we have multislot support*/
3214 if (partition_scan_for_multislot())
3215 {
3216 current_active_slot = partition_find_active_slot();
3217 dprintf(INFO, "Multislot supported: Slot %s active",
3218 (SUFFIX_SLOT(current_active_slot)));
3219 }
3220 partition_mark_active_slot(current_active_slot);
Greg Grisco6e754772011-06-23 12:19:39 -07003221 }
Mao Jinlong226f33a2014-07-04 17:24:10 +08003222 else
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003223 {
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003224#if VERIFIED_BOOT
3225 if(!strcmp(pname, KEYSTORE_PTN_NAME))
3226 {
3227 if(!device.is_unlocked)
3228 {
3229 fastboot_fail("unlock device to flash keystore");
3230 return;
3231 }
Gaurav Nebhwanic7313cc2015-12-15 22:25:04 +05303232 if(!boot_verify_validate_keystore((unsigned char *)data,sz))
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003233 {
3234 fastboot_fail("image is not a keystore file");
3235 return;
3236 }
3237 }
3238#endif
Mao Jinlong226f33a2014-07-04 17:24:10 +08003239 index = partition_get_index(pname);
3240 ptn = partition_get_offset(index);
3241 if(ptn == 0) {
3242 fastboot_fail("partition table doesn't exist");
3243 return;
3244 }
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003245
Mayank Grover351a75e2017-05-30 20:06:08 +05303246 if (!strncmp(pname, "boot", strlen("boot"))
3247 || !strcmp(pname, "recovery"))
3248 {
Mao Jinlong226f33a2014-07-04 17:24:10 +08003249 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
3250 fastboot_fail("image is not a boot image");
3251 return;
3252 }
Mayank Grover351a75e2017-05-30 20:06:08 +05303253
3254 /* Reset multislot_partition attributes in case of flashing boot */
3255 if (partition_multislot_is_supported())
3256 {
3257 partition_reset_attributes(index);
3258 }
Mao Jinlong226f33a2014-07-04 17:24:10 +08003259 }
3260
3261 if(!lun_set)
3262 {
3263 lun = partition_get_lun(index);
3264 mmc_set_lun(lun);
3265 }
3266
3267 size = partition_get_size(index);
Gaurav Nebhwani4d2389c2016-03-17 21:10:05 +05303268 if (ROUND_TO_PAGE(sz, mmc_blocksize_mask) > size) {
Mao Jinlong226f33a2014-07-04 17:24:10 +08003269 fastboot_fail("size too large");
3270 return;
3271 }
3272 else if (mmc_write(ptn , sz, (unsigned int *)data)) {
3273 fastboot_fail("flash write failure");
3274 return;
3275 }
Greg Grisco6e754772011-06-23 12:19:39 -07003276 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07003277 }
3278 fastboot_okay("");
3279 return;
3280}
3281
Ajay Dudanide984792015-03-02 09:57:41 -08003282void cmd_flash_meta_img(const char *arg, void *data, unsigned sz)
3283{
3284 int i, images;
3285 meta_header_t *meta_header;
3286 img_header_entry_t *img_header_entry;
Parth Dixit3e2d3032016-03-04 17:11:52 +05303287 /*End of the image address*/
3288 uintptr_t data_end;
3289
3290 if( (UINT_MAX - sz) > (uintptr_t)data )
3291 data_end = (uintptr_t)data + sz;
3292 else
3293 {
3294 fastboot_fail("Cannot flash: image header corrupt");
3295 return;
3296 }
3297
3298 if( data_end < ((uintptr_t)data + sizeof(meta_header_t)))
3299 {
3300 fastboot_fail("Cannot flash: image header corrupt");
3301 return;
3302 }
Ajay Dudanide984792015-03-02 09:57:41 -08003303
lijuang8ee21882016-04-19 16:57:11 +08003304 /* If device is locked:
3305 * Forbid to flash image to avoid the device to bypass the image
3306 * which with "any" name other than bootloader. Because it maybe
3307 * a meta package of all partitions.
3308 */
Monika Singh292b3e92018-03-17 22:40:23 +05303309#if VERIFIED_BOOT || VERIFIED_BOOT_2
lijuang8ee21882016-04-19 16:57:11 +08003310 if (target_build_variant_user()) {
3311 if (!device.is_unlocked) {
3312 fastboot_fail("Device is locked, meta image flashing is not allowed");
3313 return;
3314 }
Mayank Grover889be1b2017-09-12 20:12:23 +05303315
Mayank Grover912eaa62017-10-26 12:08:53 +05303316 if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05303317 !device.is_unlock_critical)
3318 {
lijuang8ee21882016-04-19 16:57:11 +08003319 fastboot_fail("Device is critical locked, Meta image flashing is not allowed");
3320 return;
3321 }
lijuang8ee21882016-04-19 16:57:11 +08003322 }
3323#endif
3324
Ajay Dudanide984792015-03-02 09:57:41 -08003325 meta_header = (meta_header_t*) data;
Parth Dixit3e2d3032016-03-04 17:11:52 +05303326 if( data_end < ((uintptr_t)data + meta_header->img_hdr_sz))
3327 {
3328 fastboot_fail("Cannot flash: image header corrupt");
3329 return;
3330 }
Ajay Dudanide984792015-03-02 09:57:41 -08003331 img_header_entry = (img_header_entry_t*) (data+sizeof(meta_header_t));
3332
3333 images = meta_header->img_hdr_sz / sizeof(img_header_entry_t);
3334
3335 for (i=0; i<images; i++) {
3336
3337 if((img_header_entry[i].ptn_name == NULL) ||
3338 (img_header_entry[i].start_offset == 0) ||
3339 (img_header_entry[i].size == 0))
3340 break;
Kishor PK49831502017-04-21 17:55:43 +05303341 if ((UINT_MAX - img_header_entry[i].start_offset) < (uintptr_t)data) {
3342 fastboot_fail("Integer overflow detected in start_offset of img");
3343 break;
3344 }
3345 else if ((UINT_MAX - (img_header_entry[i].start_offset + (uintptr_t)data)) < img_header_entry[i].size) {
3346 fastboot_fail("Integer overflow detected in size of img");
3347 break;
3348 }
Parth Dixit3e2d3032016-03-04 17:11:52 +05303349 if( data_end < ((uintptr_t)data + img_header_entry[i].start_offset
3350 + img_header_entry[i].size) )
3351 {
3352 fastboot_fail("Cannot flash: image size mismatch");
3353 break;
3354 }
3355
Ajay Dudanide984792015-03-02 09:57:41 -08003356 cmd_flash_mmc_img(img_header_entry[i].ptn_name,
3357 (void *) data + img_header_entry[i].start_offset,
3358 img_header_entry[i].size);
3359 }
3360
Channagoud Kadabiaafbbfc2015-03-04 17:52:56 -08003361 if (!strncmp(arg, "bootloader", strlen("bootloader")))
3362 {
3363 strlcpy(device.bootloader_version, TARGET(BOARD), MAX_VERSION_LEN);
3364 strlcat(device.bootloader_version, "-", MAX_VERSION_LEN);
3365 strlcat(device.bootloader_version, meta_header->img_version, MAX_VERSION_LEN);
3366 }
3367 else
3368 {
3369 strlcpy(device.radio_version, TARGET(BOARD), MAX_VERSION_LEN);
3370 strlcat(device.radio_version, "-", MAX_VERSION_LEN);
3371 strlcat(device.radio_version, meta_header->img_version, MAX_VERSION_LEN);
3372 }
3373
3374 write_device_info(&device);
Ajay Dudanide984792015-03-02 09:57:41 -08003375 fastboot_okay("");
3376 return;
3377}
3378
Ajay Dudani5c761132011-04-07 20:19:04 -07003379void cmd_flash_mmc_sparse_img(const char *arg, void *data, unsigned sz)
3380{
3381 unsigned int chunk;
wufeng.jiang813dc352015-06-02 23:02:46 -04003382 uint64_t chunk_data_sz;
Channagoud Kadabif627cf82013-09-09 14:08:20 -07003383 uint32_t *fill_buf = NULL;
3384 uint32_t fill_val;
Mayank Grover4f50bba2017-07-19 18:17:08 +05303385 uint32_t blk_sz_actual = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07003386 sparse_header_t *sparse_header;
3387 chunk_header_t *chunk_header;
Ajay Dudaniab18f022011-05-12 14:39:22 -07003388 uint32_t total_blocks = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07003389 unsigned long long ptn = 0;
Channagoud Kadabi65b91002011-10-11 17:34:33 +05303390 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07003391 int index = INVALID_PTN;
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05303392 uint32_t i;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003393 uint8_t lun = 0;
vijay kumar800255e2015-04-24 20:26:19 +05303394 /*End of the sparse image address*/
Parth Dixit64b1a482016-03-07 16:31:26 +05303395 uintptr_t data_end = (uintptr_t)data + sz;
Ajay Dudani5c761132011-04-07 20:19:04 -07003396
Kinson Chikf1a43512011-07-14 11:28:39 -07003397 index = partition_get_index(arg);
3398 ptn = partition_get_offset(index);
3399 if(ptn == 0) {
Ajay Dudani5c761132011-04-07 20:19:04 -07003400 fastboot_fail("partition table doesn't exist");
3401 return;
3402 }
3403
Channagoud Kadabi65b91002011-10-11 17:34:33 +05303404 size = partition_get_size(index);
Channagoud Kadabi65b91002011-10-11 17:34:33 +05303405
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003406 lun = partition_get_lun(index);
3407 mmc_set_lun(lun);
3408
vijay kumar800255e2015-04-24 20:26:19 +05303409 if (sz < sizeof(sparse_header_t)) {
3410 fastboot_fail("size too low");
3411 return;
3412 }
3413
Ajay Dudani5c761132011-04-07 20:19:04 -07003414 /* Read and skip over sparse image header */
3415 sparse_header = (sparse_header_t *) data;
vijay kumar800255e2015-04-24 20:26:19 +05303416
Mayank Grover48bd9bb2017-07-19 12:04:16 +05303417 if (!sparse_header->blk_sz || (sparse_header->blk_sz % 4)){
3418 fastboot_fail("Invalid block size\n");
3419 return;
3420 }
3421
vijay kumar1321f342015-03-27 12:13:42 +05303422 if (((uint64_t)sparse_header->total_blks * (uint64_t)sparse_header->blk_sz) > size) {
Ajay Dudani876b3282012-12-21 14:12:17 -08003423 fastboot_fail("size too large");
3424 return;
3425 }
3426
vijay kumarde4fcf62015-04-23 13:05:49 +05303427 data += sizeof(sparse_header_t);
vijay kumar800255e2015-04-24 20:26:19 +05303428
Parth Dixit64b1a482016-03-07 16:31:26 +05303429 if (data_end < (uintptr_t)data) {
vijay kumar800255e2015-04-24 20:26:19 +05303430 fastboot_fail("buffer overreads occured due to invalid sparse header");
3431 return;
3432 }
3433
vijay kumarde4fcf62015-04-23 13:05:49 +05303434 if(sparse_header->file_hdr_sz != sizeof(sparse_header_t))
Ajay Dudani5c761132011-04-07 20:19:04 -07003435 {
vijay kumarde4fcf62015-04-23 13:05:49 +05303436 fastboot_fail("sparse header size mismatch");
3437 return;
Ajay Dudani5c761132011-04-07 20:19:04 -07003438 }
3439
Ajay Dudanib06c05f2011-05-12 14:46:10 -07003440 dprintf (SPEW, "=== Sparse Image Header ===\n");
3441 dprintf (SPEW, "magic: 0x%x\n", sparse_header->magic);
3442 dprintf (SPEW, "major_version: 0x%x\n", sparse_header->major_version);
3443 dprintf (SPEW, "minor_version: 0x%x\n", sparse_header->minor_version);
3444 dprintf (SPEW, "file_hdr_sz: %d\n", sparse_header->file_hdr_sz);
3445 dprintf (SPEW, "chunk_hdr_sz: %d\n", sparse_header->chunk_hdr_sz);
3446 dprintf (SPEW, "blk_sz: %d\n", sparse_header->blk_sz);
3447 dprintf (SPEW, "total_blks: %d\n", sparse_header->total_blks);
3448 dprintf (SPEW, "total_chunks: %d\n", sparse_header->total_chunks);
Ajay Dudani5c761132011-04-07 20:19:04 -07003449
3450 /* Start processing chunks */
3451 for (chunk=0; chunk<sparse_header->total_chunks; chunk++)
3452 {
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05303453 /* Make sure the total image size does not exceed the partition size */
3454 if(((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz) >= size) {
3455 fastboot_fail("size too large");
3456 return;
3457 }
Ajay Dudani5c761132011-04-07 20:19:04 -07003458 /* Read and skip over chunk header */
3459 chunk_header = (chunk_header_t *) data;
3460 data += sizeof(chunk_header_t);
3461
Parth Dixit64b1a482016-03-07 16:31:26 +05303462 if (data_end < (uintptr_t)data) {
vijay kumar800255e2015-04-24 20:26:19 +05303463 fastboot_fail("buffer overreads occured due to invalid sparse header");
3464 return;
3465 }
3466
Ajay Dudani5c761132011-04-07 20:19:04 -07003467 dprintf (SPEW, "=== Chunk Header ===\n");
3468 dprintf (SPEW, "chunk_type: 0x%x\n", chunk_header->chunk_type);
3469 dprintf (SPEW, "chunk_data_sz: 0x%x\n", chunk_header->chunk_sz);
3470 dprintf (SPEW, "total_size: 0x%x\n", chunk_header->total_sz);
3471
vijay kumar800255e2015-04-24 20:26:19 +05303472 if(sparse_header->chunk_hdr_sz != sizeof(chunk_header_t))
Ajay Dudani5c761132011-04-07 20:19:04 -07003473 {
vijay kumar800255e2015-04-24 20:26:19 +05303474 fastboot_fail("chunk header size mismatch");
3475 return;
Ajay Dudani5c761132011-04-07 20:19:04 -07003476 }
3477
wufeng.jiang813dc352015-06-02 23:02:46 -04003478 chunk_data_sz = (uint64_t)sparse_header->blk_sz * chunk_header->chunk_sz;
3479
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05303480 /* Make sure that the chunk size calculated from sparse image does not
3481 * exceed partition size
3482 */
3483 if ((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz + chunk_data_sz > size)
3484 {
3485 fastboot_fail("Chunk data size exceeds partition size");
3486 return;
3487 }
3488
Ajay Dudani5c761132011-04-07 20:19:04 -07003489 switch (chunk_header->chunk_type)
3490 {
3491 case CHUNK_TYPE_RAW:
wufeng.jiang813dc352015-06-02 23:02:46 -04003492 if((uint64_t)chunk_header->total_sz != ((uint64_t)sparse_header->chunk_hdr_sz +
Ajay Dudani5c761132011-04-07 20:19:04 -07003493 chunk_data_sz))
3494 {
3495 fastboot_fail("Bogus chunk size for chunk type Raw");
3496 return;
3497 }
3498
Parth Dixit64b1a482016-03-07 16:31:26 +05303499 if (data_end < (uintptr_t)data + chunk_data_sz) {
vijay kumar800255e2015-04-24 20:26:19 +05303500 fastboot_fail("buffer overreads occured due to invalid sparse header");
3501 return;
3502 }
3503
wufeng.jiang813dc352015-06-02 23:02:46 -04003504 /* chunk_header->total_sz is uint32,So chunk_data_sz is now less than 2^32
3505 otherwise it will return in the line above
3506 */
Ajay Dudaniab18f022011-05-12 14:39:22 -07003507 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
wufeng.jiang813dc352015-06-02 23:02:46 -04003508 (uint32_t)chunk_data_sz,
Ajay Dudaniab18f022011-05-12 14:39:22 -07003509 (unsigned int*)data))
Ajay Dudani5c761132011-04-07 20:19:04 -07003510 {
3511 fastboot_fail("flash write failure");
3512 return;
3513 }
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05303514 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
3515 fastboot_fail("Bogus size for RAW chunk type");
3516 return;
3517 }
Ajay Dudani5c761132011-04-07 20:19:04 -07003518 total_blocks += chunk_header->chunk_sz;
wufeng.jiang813dc352015-06-02 23:02:46 -04003519 data += (uint32_t)chunk_data_sz;
Ajay Dudani5c761132011-04-07 20:19:04 -07003520 break;
3521
Channagoud Kadabif627cf82013-09-09 14:08:20 -07003522 case CHUNK_TYPE_FILL:
3523 if(chunk_header->total_sz != (sparse_header->chunk_hdr_sz +
3524 sizeof(uint32_t)))
3525 {
3526 fastboot_fail("Bogus chunk size for chunk type FILL");
3527 return;
3528 }
3529
Mayank Grover4f50bba2017-07-19 18:17:08 +05303530 blk_sz_actual = ROUNDUP(sparse_header->blk_sz, CACHE_LINE);
3531 /* Integer overflow detected */
3532 if (blk_sz_actual < sparse_header->blk_sz)
3533 {
3534 fastboot_fail("Invalid block size");
3535 return;
3536 }
3537
3538 fill_buf = (uint32_t *)memalign(CACHE_LINE, blk_sz_actual);
Channagoud Kadabif627cf82013-09-09 14:08:20 -07003539 if (!fill_buf)
3540 {
3541 fastboot_fail("Malloc failed for: CHUNK_TYPE_FILL");
3542 return;
3543 }
3544
Parth Dixit64b1a482016-03-07 16:31:26 +05303545 if (data_end < (uintptr_t)data + sizeof(uint32_t)) {
vijay kumar800255e2015-04-24 20:26:19 +05303546 fastboot_fail("buffer overreads occured due to invalid sparse header");
Gaurav Nebhwani163cbf82016-03-17 21:21:27 +05303547 free(fill_buf);
vijay kumar800255e2015-04-24 20:26:19 +05303548 return;
3549 }
Channagoud Kadabif627cf82013-09-09 14:08:20 -07003550 fill_val = *(uint32_t *)data;
3551 data = (char *) data + sizeof(uint32_t);
Channagoud Kadabif627cf82013-09-09 14:08:20 -07003552
3553 for (i = 0; i < (sparse_header->blk_sz / sizeof(fill_val)); i++)
3554 {
3555 fill_buf[i] = fill_val;
3556 }
3557
Gaurav Nebhwanie94cbe12016-03-17 21:16:34 +05303558 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz))
3559 {
3560 fastboot_fail("bogus size for chunk FILL type");
3561 free(fill_buf);
3562 return;
3563 }
3564
wufeng.jiang813dc352015-06-02 23:02:46 -04003565 for (i = 0; i < chunk_header->chunk_sz; i++)
Channagoud Kadabif627cf82013-09-09 14:08:20 -07003566 {
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05303567 /* Make sure that the data written to partition does not exceed partition size */
3568 if ((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz + sparse_header->blk_sz > size)
3569 {
3570 fastboot_fail("Chunk data size for fill type exceeds partition size");
Gaurav Nebhwani163cbf82016-03-17 21:21:27 +05303571 free(fill_buf);
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05303572 return;
3573 }
3574
Channagoud Kadabif627cf82013-09-09 14:08:20 -07003575 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
3576 sparse_header->blk_sz,
3577 fill_buf))
3578 {
3579 fastboot_fail("flash write failure");
3580 free(fill_buf);
3581 return;
3582 }
3583
3584 total_blocks++;
3585 }
3586
3587 free(fill_buf);
3588 break;
3589
Ajay Dudani5c761132011-04-07 20:19:04 -07003590 case CHUNK_TYPE_DONT_CARE:
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05303591 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
3592 fastboot_fail("bogus size for chunk DONT CARE type");
3593 return;
3594 }
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07003595 total_blocks += chunk_header->chunk_sz;
3596 break;
3597
Ajay Dudani5c761132011-04-07 20:19:04 -07003598 case CHUNK_TYPE_CRC:
3599 if(chunk_header->total_sz != sparse_header->chunk_hdr_sz)
3600 {
wufeng.jiang813dc352015-06-02 23:02:46 -04003601 fastboot_fail("Bogus chunk size for chunk type CRC");
Ajay Dudani5c761132011-04-07 20:19:04 -07003602 return;
3603 }
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05303604 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
3605 fastboot_fail("bogus size for chunk CRC type");
3606 return;
3607 }
Ajay Dudani5c761132011-04-07 20:19:04 -07003608 total_blocks += chunk_header->chunk_sz;
Parth Dixit64b1a482016-03-07 16:31:26 +05303609 if ((uintptr_t)data > UINT_MAX - chunk_data_sz) {
vijay kumar800255e2015-04-24 20:26:19 +05303610 fastboot_fail("integer overflow occured");
3611 return;
3612 }
wufeng.jiang813dc352015-06-02 23:02:46 -04003613 data += (uint32_t)chunk_data_sz;
Parth Dixit64b1a482016-03-07 16:31:26 +05303614 if (data_end < (uintptr_t)data) {
vijay kumar800255e2015-04-24 20:26:19 +05303615 fastboot_fail("buffer overreads occured due to invalid sparse header");
3616 return;
3617 }
Ajay Dudani5c761132011-04-07 20:19:04 -07003618 break;
3619
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07003620 default:
Channagoud Kadabif627cf82013-09-09 14:08:20 -07003621 dprintf(CRITICAL, "Unkown chunk type: %x\n",chunk_header->chunk_type);
Ajay Dudani5c761132011-04-07 20:19:04 -07003622 fastboot_fail("Unknown chunk type");
3623 return;
3624 }
3625 }
3626
Ajay Dudani0c6927b2011-05-18 11:12:16 -07003627 dprintf(INFO, "Wrote %d blocks, expected to write %d blocks\n",
3628 total_blocks, sparse_header->total_blks);
3629
3630 if(total_blocks != sparse_header->total_blks)
3631 {
3632 fastboot_fail("sparse image write failure");
3633 }
Ajay Dudani5c761132011-04-07 20:19:04 -07003634
3635 fastboot_okay("");
3636 return;
3637}
3638
3639void cmd_flash_mmc(const char *arg, void *data, unsigned sz)
3640{
3641 sparse_header_t *sparse_header;
Ajay Dudanide984792015-03-02 09:57:41 -08003642 meta_header_t *meta_header;
Ajay Dudani5c761132011-04-07 20:19:04 -07003643
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07003644#ifdef SSD_ENABLE
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -08003645 /* 8 Byte Magic + 2048 Byte xml + Encrypted Data */
3646 unsigned int *magic_number = (unsigned int *) data;
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08003647 int ret=0;
3648 uint32 major_version=0;
3649 uint32 minor_version=0;
3650
3651 ret = scm_svc_version(&major_version,&minor_version);
3652 if(!ret)
3653 {
3654 if(major_version >= 2)
3655 {
Amir Samuelovbb65ce02013-05-05 12:20:18 +03003656 if( !strcmp(arg, "ssd") || !strcmp(arg, "tqs") )
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08003657 {
3658 ret = encrypt_scm((uint32 **) &data, &sz);
3659 if (ret != 0) {
3660 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
3661 return;
3662 }
3663
Amir Samuelovbb65ce02013-05-05 12:20:18 +03003664 /* Protect only for SSD */
3665 if (!strcmp(arg, "ssd")) {
3666 ret = scm_protect_keystore((uint32 *) data, sz);
3667 if (ret != 0) {
3668 dprintf(CRITICAL, "ERROR: scm_protect_keystore Failed\n");
3669 return;
3670 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08003671 }
3672 }
3673 else
3674 {
3675 ret = decrypt_scm_v2((uint32 **) &data, &sz);
3676 if(ret != 0)
3677 {
3678 dprintf(CRITICAL,"ERROR: Decryption Failure\n");
3679 return;
3680 }
3681 }
3682 }
3683 else
3684 {
3685 if (magic_number[0] == DECRYPT_MAGIC_0 &&
3686 magic_number[1] == DECRYPT_MAGIC_1)
3687 {
3688 ret = decrypt_scm((uint32 **) &data, &sz);
3689 if (ret != 0) {
3690 dprintf(CRITICAL, "ERROR: Invalid secure image\n");
3691 return;
3692 }
3693 }
3694 else if (magic_number[0] == ENCRYPT_MAGIC_0 &&
3695 magic_number[1] == ENCRYPT_MAGIC_1)
3696 {
3697 ret = encrypt_scm((uint32 **) &data, &sz);
3698 if (ret != 0) {
3699 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
3700 return;
3701 }
3702 }
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07003703 }
3704 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08003705 else
Neeti Desai127b9e02012-03-20 16:11:23 -07003706 {
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08003707 dprintf(CRITICAL,"INVALID SVC Version\n");
3708 return;
Neeti Desai127b9e02012-03-20 16:11:23 -07003709 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08003710#endif /* SSD_ENABLE */
Neeti Desai127b9e02012-03-20 16:11:23 -07003711
Monika Singh292b3e92018-03-17 22:40:23 +05303712#if VERIFIED_BOOT || VERIFIED_BOOT_2
Channagoud Kadabi35297672015-06-13 11:09:49 -07003713 if (target_build_variant_user())
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003714 {
lijuang511a2b52015-08-14 20:50:51 +08003715 /* if device is locked:
3716 * common partition will not allow to be flashed
3717 * critical partition will allow to flash image.
3718 */
3719 if(!device.is_unlocked && !critical_flash_allowed(arg)) {
3720 fastboot_fail("Partition flashing is not allowed");
3721 return;
3722 }
Mayank Grover889be1b2017-09-12 20:12:23 +05303723
lijuang511a2b52015-08-14 20:50:51 +08003724 /* if device critical is locked:
3725 * common partition will allow to be flashed
3726 * critical partition will not allow to flash image.
3727 */
Mayank Grover912eaa62017-10-26 12:08:53 +05303728 if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05303729 !device.is_unlock_critical &&
3730 critical_flash_allowed(arg)) {
3731 fastboot_fail("Critical partition flashing is not allowed");
3732 return;
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003733 }
3734 }
3735#endif
Monika Singh292b3e92018-03-17 22:40:23 +05303736 if (!strncmp(arg, "avb_custom_key", strlen("avb_custom_key"))) {
3737 dprintf(INFO, "flashing avb_custom_key\n");
3738 if (store_userkey(data, sz)) {
3739 fastboot_fail("Flashing avb_custom_key failed");
3740 } else {
3741 fastboot_okay("");
3742 }
3743 return;
3744 }
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003745
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07003746 sparse_header = (sparse_header_t *) data;
Ajay Dudanide984792015-03-02 09:57:41 -08003747 meta_header = (meta_header_t *) data;
3748 if (sparse_header->magic == SPARSE_HEADER_MAGIC)
Ajay Dudani5c761132011-04-07 20:19:04 -07003749 cmd_flash_mmc_sparse_img(arg, data, sz);
Ajay Dudanide984792015-03-02 09:57:41 -08003750 else if (meta_header->magic == META_HEADER_MAGIC)
3751 cmd_flash_meta_img(arg, data, sz);
3752 else
3753 cmd_flash_mmc_img(arg, data, sz);
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07003754
3755#if VERIFIED_BOOT
Mayank Grover912eaa62017-10-26 12:08:53 +05303756 if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05303757 (!strncmp(arg, "system", 6)) &&
3758 !device.verity_mode)
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07003759 // reset dm_verity mode to enforcing
3760 device.verity_mode = 1;
3761 write_device_info(&device);
Parth Dixitddbc7352015-10-18 03:13:31 +05303762#endif
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07003763
Ajay Dudani5c761132011-04-07 20:19:04 -07003764 return;
3765}
3766
Tanya Brokhman84eaaf62015-02-24 21:57:06 +02003767void cmd_updatevol(const char *vol_name, void *data, unsigned sz)
3768{
3769 struct ptentry *sys_ptn;
3770 struct ptable *ptable;
3771
3772 ptable = flash_get_ptable();
3773 if (ptable == NULL) {
3774 fastboot_fail("partition table doesn't exist");
3775 return;
3776 }
3777
3778 sys_ptn = ptable_find(ptable, "system");
3779 if (sys_ptn == NULL) {
3780 fastboot_fail("system partition not found");
3781 return;
3782 }
3783
3784 sz = ROUND_TO_PAGE(sz, page_mask);
3785 if (update_ubi_vol(sys_ptn, vol_name, data, sz))
3786 fastboot_fail("update_ubi_vol failed");
3787 else
3788 fastboot_okay("");
3789}
3790
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003791void cmd_flash_nand(const char *arg, void *data, unsigned sz)
Dima Zavin214cc642009-01-26 11:16:21 -08003792{
3793 struct ptentry *ptn;
3794 struct ptable *ptable;
3795 unsigned extra = 0;
Gaurav Nebhwanid9730712016-05-06 14:28:29 +05303796 uint64_t partition_size = 0;
Vijay Kumar Pendotic6f22a72017-05-19 22:44:34 +05303797 unsigned bytes_to_round_page = 0;
3798 unsigned rounded_size = 0;
Dima Zavin214cc642009-01-26 11:16:21 -08003799
Kishor PK38ed93d2017-04-25 14:19:26 +05303800 if((uintptr_t)data > (UINT_MAX - sz)) {
3801 fastboot_fail("Cannot flash: image header corrupt");
3802 return;
3803 }
3804
Dima Zavin214cc642009-01-26 11:16:21 -08003805 ptable = flash_get_ptable();
3806 if (ptable == NULL) {
3807 fastboot_fail("partition table doesn't exist");
3808 return;
3809 }
3810
3811 ptn = ptable_find(ptable, arg);
3812 if (ptn == NULL) {
Tanya Brokhman84eaaf62015-02-24 21:57:06 +02003813 dprintf(INFO, "unknown partition name (%s). Trying updatevol\n",
3814 arg);
3815 cmd_updatevol(arg, data, sz);
Dima Zavin214cc642009-01-26 11:16:21 -08003816 return;
3817 }
3818
Monika Singh292b3e92018-03-17 22:40:23 +05303819 if (!strncmp(arg, "avb_custom_key", strlen("avb_custom_key"))) {
3820 dprintf(INFO, "flashing avb_custom_key\n");
3821 if (store_userkey(data, sz)) {
3822 fastboot_fail("Flashing avb_custom_key failed");
3823 } else {
3824 fastboot_okay("");
3825 }
3826 return;
3827 }
3828
Dima Zavin214cc642009-01-26 11:16:21 -08003829 if (!strcmp(ptn->name, "boot") || !strcmp(ptn->name, "recovery")) {
Kishor PK38ed93d2017-04-25 14:19:26 +05303830 if((sz > BOOT_MAGIC_SIZE) && (!memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE))) {
3831 dprintf(INFO, "Verified the BOOT_MAGIC in image header \n");
3832 } else {
3833 fastboot_fail("Image is not a boot image");
Dima Zavin214cc642009-01-26 11:16:21 -08003834 return;
3835 }
3836 }
3837
Amol Jadi5c61a952012-05-04 17:05:35 -07003838 if (!strcmp(ptn->name, "system")
Deepa Dinamani13e32c42012-03-12 14:34:17 -07003839 || !strcmp(ptn->name, "userdata")
3840 || !strcmp(ptn->name, "persist")
Sundarajan Srinivasanb063a852013-11-19 14:02:27 -08003841 || !strcmp(ptn->name, "recoveryfs")
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -08003842 || !strcmp(ptn->name, "modem"))
Tanya Brokhman1c94f1a2015-02-15 09:05:03 +02003843 extra = 1;
Kishor PK38ed93d2017-04-25 14:19:26 +05303844 else {
Vijay Kumar Pendotic6f22a72017-05-19 22:44:34 +05303845 rounded_size = ROUNDUP(sz, page_size);
3846 bytes_to_round_page = rounded_size - sz;
3847 if (bytes_to_round_page) {
3848 if (((uintptr_t)data + sz ) > (UINT_MAX - bytes_to_round_page)) {
3849 fastboot_fail("Integer overflow detected");
3850 return;
3851 }
3852 if (((uintptr_t)data + sz + bytes_to_round_page) >
3853 ((uintptr_t)target_get_scratch_address() + target_get_max_flash_size())) {
3854 fastboot_fail("Buffer size is not aligned to page_size");
3855 return;
3856 }
3857 else {
3858 memset(data + sz, 0, bytes_to_round_page);
3859 sz = rounded_size;
3860 }
Kishor PK38ed93d2017-04-25 14:19:26 +05303861 }
Gaurav Nebhwanid9730712016-05-06 14:28:29 +05303862 }
3863
Kishor PK38ed93d2017-04-25 14:19:26 +05303864 /*Checking partition_size for the possible integer overflow */
3865 partition_size = validate_partition_size(ptn);
3866
Gaurav Nebhwanid9730712016-05-06 14:28:29 +05303867 if (sz > partition_size) {
3868 fastboot_fail("Image size too large");
3869 return;
3870 }
3871
Dima Zavin214cc642009-01-26 11:16:21 -08003872 dprintf(INFO, "writing %d bytes to '%s'\n", sz, ptn->name);
Mayank Grover6cde9352017-06-06 18:45:23 +05303873 if ((sz > UBI_EC_HDR_SIZE) &&
3874 (!memcmp((void *)data, UBI_MAGIC, UBI_MAGIC_SIZE))) {
Tanya Brokhman1c94f1a2015-02-15 09:05:03 +02003875 if (flash_ubi_img(ptn, data, sz)) {
3876 fastboot_fail("flash write failure");
3877 return;
3878 }
3879 } else {
3880 if (flash_write(ptn, extra, data, sz)) {
3881 fastboot_fail("flash write failure");
3882 return;
3883 }
Dima Zavin214cc642009-01-26 11:16:21 -08003884 }
3885 dprintf(INFO, "partition '%s' updated\n", ptn->name);
3886 fastboot_okay("");
3887}
3888
Kishor PK38ed93d2017-04-25 14:19:26 +05303889
3890static inline uint64_t validate_partition_size(struct ptentry *ptn)
3891{
3892 if (ptn->length && flash_num_pages_per_blk() && page_size) {
3893 if ((ptn->length < ( UINT_MAX / flash_num_pages_per_blk())) && ((ptn->length * flash_num_pages_per_blk()) < ( UINT_MAX / page_size))) {
3894 return ptn->length * flash_num_pages_per_blk() * page_size;
3895 }
3896 }
3897 return 0;
3898}
3899
3900
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003901void cmd_flash(const char *arg, void *data, unsigned sz)
3902{
3903 if(target_is_emmc_boot())
3904 cmd_flash_mmc(arg, data, sz);
3905 else
3906 cmd_flash_nand(arg, data, sz);
3907}
3908
Dima Zavin214cc642009-01-26 11:16:21 -08003909void cmd_continue(const char *arg, void *data, unsigned sz)
3910{
3911 fastboot_okay("");
Amol Jadi7c4316c2013-10-07 14:19:26 -07003912 fastboot_stop();
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003913
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003914 if (target_is_emmc_boot())
3915 {
lijuanga40d6302015-07-20 20:10:13 +08003916#if FBCON_DISPLAY_MSG
lijuangde34d502016-02-26 16:04:50 +08003917 /* Exit keys' detection thread firstly */
3918 exit_menu_keys_detection();
lijuanga40d6302015-07-20 20:10:13 +08003919#endif
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003920 boot_linux_from_mmc();
3921 }
3922 else
3923 {
3924 boot_linux_from_flash();
3925 }
Dima Zavin214cc642009-01-26 11:16:21 -08003926}
3927
Chandan Uddaraju94183c02010-01-15 15:13:59 -08003928void cmd_reboot(const char *arg, void *data, unsigned sz)
3929{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003930 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08003931 fastboot_okay("");
3932 reboot_device(0);
3933}
3934
Mayank Grover351a75e2017-05-30 20:06:08 +05303935void cmd_set_active(const char *arg, void *data, unsigned sz)
3936{
Mayank Grover6ccc1c92017-07-04 17:36:46 +05303937 char *p, *sp = NULL;
Mayank Grover351a75e2017-05-30 20:06:08 +05303938 unsigned i,current_active_slot;
3939 const char *current_slot_suffix;
3940
3941 if (!partition_multislot_is_supported())
3942 {
3943 fastboot_fail("Command not supported");
3944 return;
3945 }
3946
3947 if (arg)
3948 {
Mayank Grover6ccc1c92017-07-04 17:36:46 +05303949 p = strtok_r((char *)arg, ":", &sp);
Mayank Grover5eb5f692017-07-19 20:16:04 +05303950 if (p)
Mayank Grover351a75e2017-05-30 20:06:08 +05303951 {
3952 current_active_slot = partition_find_active_slot();
3953
3954 /* Check if trying to make curent slot active */
3955 current_slot_suffix = SUFFIX_SLOT(current_active_slot);
Mayank Grover6ccc1c92017-07-04 17:36:46 +05303956 current_slot_suffix = strtok_r((char *)current_slot_suffix,
3957 (char *)suffix_delimiter, &sp);
3958
Mayank Grover5eb5f692017-07-19 20:16:04 +05303959 if (current_slot_suffix &&
3960 !strncmp(p, current_slot_suffix, strlen(current_slot_suffix)))
Mayank Grover351a75e2017-05-30 20:06:08 +05303961 {
3962 fastboot_okay("Slot already set active");
3963 return;
3964 }
3965 else
3966 {
3967 for (i = 0; i < AB_SUPPORTED_SLOTS; i++)
3968 {
3969 current_slot_suffix = SUFFIX_SLOT(i);
Mayank Grover6ccc1c92017-07-04 17:36:46 +05303970 current_slot_suffix = strtok_r((char *)current_slot_suffix,
3971 (char *)suffix_delimiter, &sp);
Mayank Grover5eb5f692017-07-19 20:16:04 +05303972 if (current_slot_suffix &&
3973 !strncmp(p, current_slot_suffix, strlen(current_slot_suffix)))
Mayank Grover351a75e2017-05-30 20:06:08 +05303974 {
3975 partition_switch_slots(current_active_slot, i);
3976 publish_getvar_multislot_vars();
3977 fastboot_okay("");
3978 return;
3979 }
3980 }
3981 }
3982 }
3983 }
3984 fastboot_fail("Invalid slot suffix.");
3985 return;
3986}
3987
Chandan Uddaraju94183c02010-01-15 15:13:59 -08003988void cmd_reboot_bootloader(const char *arg, void *data, unsigned sz)
3989{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003990 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08003991 fastboot_okay("");
3992 reboot_device(FASTBOOT_MODE);
3993}
3994
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003995void cmd_oem_enable_charger_screen(const char *arg, void *data, unsigned size)
3996{
3997 dprintf(INFO, "Enabling charger screen check\n");
3998 device.charger_screen_enabled = 1;
3999 write_device_info(&device);
4000 fastboot_okay("");
4001}
4002
4003void cmd_oem_disable_charger_screen(const char *arg, void *data, unsigned size)
4004{
4005 dprintf(INFO, "Disabling charger screen check\n");
4006 device.charger_screen_enabled = 0;
4007 write_device_info(&device);
4008 fastboot_okay("");
4009}
4010
lijuanga25d8bb2015-09-16 13:11:52 +08004011void cmd_oem_off_mode_charger(const char *arg, void *data, unsigned size)
4012{
4013 char *p = NULL;
4014 const char *delim = " \t\n\r";
Parth Dixit407f15b2016-01-07 14:47:37 +05304015 char *sp;
lijuanga25d8bb2015-09-16 13:11:52 +08004016
4017 if (arg) {
Parth Dixit407f15b2016-01-07 14:47:37 +05304018 p = strtok_r((char *)arg, delim, &sp);
lijuanga25d8bb2015-09-16 13:11:52 +08004019 if (p) {
4020 if (!strncmp(p, "0", 1)) {
4021 device.charger_screen_enabled = 0;
4022 } else if (!strncmp(p, "1", 1)) {
4023 device.charger_screen_enabled = 1;
4024 }
4025 }
4026 }
4027
4028 /* update charger_screen_enabled value for getvar
4029 * command
4030 */
4031 snprintf(charger_screen_enabled, MAX_RSP_SIZE, "%d",
4032 device.charger_screen_enabled);
4033
4034 write_device_info(&device);
4035 fastboot_okay("");
4036}
4037
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05304038void cmd_oem_select_display_panel(const char *arg, void *data, unsigned size)
4039{
4040 dprintf(INFO, "Selecting display panel %s\n", arg);
4041 if (arg)
4042 strlcpy(device.display_panel, arg,
4043 sizeof(device.display_panel));
4044 write_device_info(&device);
4045 fastboot_okay("");
4046}
4047
Shashank Mittal162244e2011-08-08 19:01:25 -07004048void cmd_oem_unlock(const char *arg, void *data, unsigned sz)
4049{
lijuang4ece1e72015-08-14 21:02:36 +08004050 set_device_unlock(UNLOCK, TRUE);
vijay kumarc65876c2015-04-24 13:29:16 +05304051}
4052
4053void cmd_oem_unlock_go(const char *arg, void *data, unsigned sz)
4054{
lijuang4ece1e72015-08-14 21:02:36 +08004055 if(!device.is_unlocked) {
vijay kumarc65876c2015-04-24 13:29:16 +05304056 if(!is_allow_unlock) {
4057 fastboot_fail("oem unlock is not allowed");
4058 return;
4059 }
4060
lijuang4ece1e72015-08-14 21:02:36 +08004061 set_device_unlock_value(UNLOCK, TRUE);
vijay kumarc65876c2015-04-24 13:29:16 +05304062
lijuang4ece1e72015-08-14 21:02:36 +08004063 /* wipe data */
vijay kumarc65876c2015-04-24 13:29:16 +05304064 struct recovery_message msg;
Kishor PK60a68212017-05-08 16:55:57 +05304065 memset(&msg, 0, sizeof(msg));
vijay kumarc65876c2015-04-24 13:29:16 +05304066 snprintf(msg.recovery, sizeof(msg.recovery), "recovery\n--wipe_data");
4067 write_misc(0, &msg, sizeof(msg));
4068
4069 fastboot_okay("");
4070 reboot_device(RECOVERY_MODE);
Shashank Mittald3e54dd2014-08-28 15:24:02 -07004071 }
4072 fastboot_okay("");
4073}
4074
Channagoud Kadabiad259832015-05-29 11:14:17 -07004075static int aboot_frp_unlock(char *pname, void *data, unsigned sz)
4076{
Mayank Grover70f8b0e2017-05-17 11:49:00 +05304077 int ret=1;
4078 bool authentication_success=false;
Channagoud Kadabiad259832015-05-29 11:14:17 -07004079
Mayank Grover70f8b0e2017-05-17 11:49:00 +05304080 /*
4081 Authentication method not implemented.
4082
4083 OEM to implement, authentication system which on successful validataion,
4084 calls write_allow_oem_unlock() with is_allow_unlock.
4085 */
4086#if 0
4087 authentication_success = oem_specific_auth_mthd();
4088#endif
4089
4090 if (authentication_success)
Channagoud Kadabiad259832015-05-29 11:14:17 -07004091 {
Mayank Grover70f8b0e2017-05-17 11:49:00 +05304092 is_allow_unlock = true;
4093 write_allow_oem_unlock(is_allow_unlock);
4094 ret = 0;
Channagoud Kadabiad259832015-05-29 11:14:17 -07004095 }
4096 return ret;
4097}
4098
Shashank Mittald3e54dd2014-08-28 15:24:02 -07004099void cmd_oem_lock(const char *arg, void *data, unsigned sz)
4100{
lijuang4ece1e72015-08-14 21:02:36 +08004101 set_device_unlock(UNLOCK, FALSE);
Shashank Mittal162244e2011-08-08 19:01:25 -07004102}
4103
Shashank Mittala0032282011-08-26 14:50:11 -07004104void cmd_oem_devinfo(const char *arg, void *data, unsigned sz)
4105{
lijuang511a2b52015-08-14 20:50:51 +08004106 char response[MAX_RSP_SIZE];
Ameya Thakur11cf1a62013-08-05 12:44:48 -07004107 snprintf(response, sizeof(response), "\tDevice tampered: %s", (device.is_tampered ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07004108 fastboot_info(response);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07004109 snprintf(response, sizeof(response), "\tDevice unlocked: %s", (device.is_unlocked ? "true" : "false"));
4110 fastboot_info(response);
Monika Singh292b3e92018-03-17 22:40:23 +05304111#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover912eaa62017-10-26 12:08:53 +05304112 if (VB_M <= target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +05304113 {
4114 snprintf(response, sizeof(response), "\tDevice critical unlocked: %s",
4115 (device.is_unlock_critical ? "true" : "false"));
4116 fastboot_info(response);
4117 }
Parth Dixitddbc7352015-10-18 03:13:31 +05304118#endif
Ameya Thakur11cf1a62013-08-05 12:44:48 -07004119 snprintf(response, sizeof(response), "\tCharger screen enabled: %s", (device.charger_screen_enabled ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07004120 fastboot_info(response);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05304121 snprintf(response, sizeof(response), "\tDisplay panel: %s", (device.display_panel));
4122 fastboot_info(response);
Shashank Mittala0032282011-08-26 14:50:11 -07004123 fastboot_okay("");
4124}
4125
lijuang511a2b52015-08-14 20:50:51 +08004126void cmd_flashing_get_unlock_ability(const char *arg, void *data, unsigned sz)
4127{
4128 char response[MAX_RSP_SIZE];
4129 snprintf(response, sizeof(response), "\tget_unlock_ability: %d", is_allow_unlock);
4130 fastboot_info(response);
4131 fastboot_okay("");
4132}
4133
4134void cmd_flashing_lock_critical(const char *arg, void *data, unsigned sz)
4135{
lijuang4ece1e72015-08-14 21:02:36 +08004136 set_device_unlock(UNLOCK_CRITICAL, FALSE);
lijuang511a2b52015-08-14 20:50:51 +08004137}
4138
4139void cmd_flashing_unlock_critical(const char *arg, void *data, unsigned sz)
4140{
lijuang4ece1e72015-08-14 21:02:36 +08004141 set_device_unlock(UNLOCK_CRITICAL, TRUE);
lijuang511a2b52015-08-14 20:50:51 +08004142}
4143
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07004144void cmd_preflash(const char *arg, void *data, unsigned sz)
4145{
4146 fastboot_okay("");
4147}
4148
Mao Flynn7b379f32015-04-20 00:28:30 +08004149static uint8_t logo_header[LOGO_IMG_HEADER_SIZE];
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304150
Mao Flynn7b379f32015-04-20 00:28:30 +08004151int splash_screen_check_header(logo_img_header *header)
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304152{
Mao Flynn7b379f32015-04-20 00:28:30 +08004153 if (memcmp(header->magic, LOGO_IMG_MAGIC, 8))
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304154 return -1;
Mao Flynn7b379f32015-04-20 00:28:30 +08004155 if (header->width == 0 || header->height == 0)
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304156 return -1;
4157 return 0;
4158}
4159
Mao Flynn7b379f32015-04-20 00:28:30 +08004160int splash_screen_flash()
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07004161{
4162 struct ptentry *ptn;
4163 struct ptable *ptable;
Mao Flynn7b379f32015-04-20 00:28:30 +08004164 struct logo_img_header *header;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07004165 struct fbcon_config *fb_display = NULL;
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08004166
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304167 ptable = flash_get_ptable();
4168 if (ptable == NULL) {
Mao Flynn7b379f32015-04-20 00:28:30 +08004169 dprintf(CRITICAL, "ERROR: Partition table not found\n");
4170 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304171 }
Mao Flynn7b379f32015-04-20 00:28:30 +08004172
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304173 ptn = ptable_find(ptable, "splash");
4174 if (ptn == NULL) {
4175 dprintf(CRITICAL, "ERROR: splash Partition not found\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08004176 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304177 }
Mao Flynn7b379f32015-04-20 00:28:30 +08004178 if (flash_read(ptn, 0, (void *)logo_header, LOGO_IMG_HEADER_SIZE)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304179 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08004180 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304181 }
4182
Mao Flynn7b379f32015-04-20 00:28:30 +08004183 header = (struct logo_img_header *)logo_header;
4184 if (splash_screen_check_header(header)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304185 dprintf(CRITICAL, "ERROR: Boot image header invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08004186 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304187 }
4188
4189 fb_display = fbcon_display();
4190 if (fb_display) {
Mao Flynn7b379f32015-04-20 00:28:30 +08004191 if (header->type && (header->blocks != 0)) { // RLE24 compressed data
4192 uint8_t *base = (uint8_t *) fb_display->base + LOGO_IMG_OFFSET;
4193
4194 /* if the logo is full-screen size, remove "fbcon_clear()" */
4195 if ((header->width != fb_display->width)
4196 || (header->height != fb_display->height))
4197 fbcon_clear();
4198
4199 if (flash_read(ptn + LOGO_IMG_HEADER_SIZE, 0,
4200 (uint32_t *)base,
4201 (header->blocks * 512))) {
4202 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
4203 return -1;
4204 }
4205 fbcon_extract_to_screen(header, base);
4206 return 0;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07004207 }
Mao Flynn7b379f32015-04-20 00:28:30 +08004208
Veera Sundaram Sankaranc33416f2015-09-28 10:25:55 -07004209 if ((header->width > fb_display->width) || (header->height > fb_display->height)) {
4210 dprintf(CRITICAL, "Logo config greater than fb config. Fall back default logo\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08004211 return -1;
4212 }
4213
Vineet Bajaj99291ed2014-09-09 12:29:46 +05304214 uint8_t *base = (uint8_t *) fb_display->base;
Sachin Bhayare619e9e42017-05-15 13:10:31 +05304215 uint32_t fb_size = ROUNDUP(fb_display->width *
4216 fb_display->height *
4217 (fb_display->bpp / 8), 4096);
4218 uint32_t splash_size = ((((header->width * header->height *
4219 fb_display->bpp/8) + 511) >> 9) << 9);
4220
4221 if (splash_size > fb_size) {
4222 dprintf(CRITICAL, "ERROR: Splash image size invalid\n");
4223 return -1;
4224 }
4225
Mao Flynn7b379f32015-04-20 00:28:30 +08004226 if (flash_read(ptn + LOGO_IMG_HEADER_SIZE, 0,
4227 (uint32_t *)base,
4228 ((((header->width * header->height * fb_display->bpp/8) + 511) >> 9) << 9))) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304229 fbcon_clear();
Vineet Bajaj99291ed2014-09-09 12:29:46 +05304230 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08004231 return -1;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07004232 }
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304233 }
4234
Mao Flynn7b379f32015-04-20 00:28:30 +08004235 return 0;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304236}
4237
Mao Flynn7b379f32015-04-20 00:28:30 +08004238int splash_screen_mmc()
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304239{
4240 int index = INVALID_PTN;
4241 unsigned long long ptn = 0;
4242 struct fbcon_config *fb_display = NULL;
Mao Flynn7b379f32015-04-20 00:28:30 +08004243 struct logo_img_header *header;
Mao Flynn1893a7f2015-06-03 12:03:36 +08004244 uint32_t blocksize, realsize, readsize;
4245 uint8_t *base;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304246
4247 index = partition_get_index("splash");
4248 if (index == 0) {
4249 dprintf(CRITICAL, "ERROR: splash Partition table not found\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08004250 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304251 }
4252
4253 ptn = partition_get_offset(index);
4254 if (ptn == 0) {
4255 dprintf(CRITICAL, "ERROR: splash Partition invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08004256 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304257 }
4258
Mao Flynn1893a7f2015-06-03 12:03:36 +08004259 mmc_set_lun(partition_get_lun(index));
4260
4261 blocksize = mmc_get_device_blocksize();
4262 if (blocksize == 0) {
4263 dprintf(CRITICAL, "ERROR:splash Partition invalid blocksize\n");
4264 return -1;
4265 }
4266
4267 fb_display = fbcon_display();
Channagoud Kadabidaf10a62015-07-22 11:51:55 -07004268 if (!fb_display)
4269 {
4270 dprintf(CRITICAL, "ERROR: fb config is not allocated\n");
4271 return -1;
4272 }
4273
Mao Flynn1893a7f2015-06-03 12:03:36 +08004274 base = (uint8_t *) fb_display->base;
4275
4276 if (mmc_read(ptn, (uint32_t *)(base + LOGO_IMG_OFFSET), blocksize)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304277 dprintf(CRITICAL, "ERROR: Cannot read splash image header\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08004278 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304279 }
4280
Mao Flynn1893a7f2015-06-03 12:03:36 +08004281 header = (struct logo_img_header *)(base + LOGO_IMG_OFFSET);
Mao Flynn7b379f32015-04-20 00:28:30 +08004282 if (splash_screen_check_header(header)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304283 dprintf(CRITICAL, "ERROR: Splash image header invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08004284 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304285 }
4286
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304287 if (fb_display) {
Mao Flynn1893a7f2015-06-03 12:03:36 +08004288 if (header->type && (header->blocks != 0)) { /* 1 RLE24 compressed data */
4289 base += LOGO_IMG_OFFSET;
Mao Flynn7b379f32015-04-20 00:28:30 +08004290
Mao Flynn1893a7f2015-06-03 12:03:36 +08004291 realsize = header->blocks * 512;
4292 readsize = ROUNDUP((realsize + LOGO_IMG_HEADER_SIZE), blocksize) - blocksize;
4293
4294 /* if the logo is not full-screen size, clean screen */
Mao Flynn7b379f32015-04-20 00:28:30 +08004295 if ((header->width != fb_display->width)
4296 || (header->height != fb_display->height))
4297 fbcon_clear();
4298
Sachin Bhayare619e9e42017-05-15 13:10:31 +05304299 uint32_t fb_size = ROUNDUP(fb_display->width *
4300 fb_display->height *
4301 (fb_display->bpp / 8), 4096);
4302
4303 if (readsize > fb_size) {
4304 dprintf(CRITICAL, "ERROR: Splash image size invalid\n");
4305 return -1;
4306 }
4307
Mao Flynn1893a7f2015-06-03 12:03:36 +08004308 if (mmc_read(ptn + blocksize, (uint32_t *)(base + blocksize), readsize)) {
Mao Flynn7b379f32015-04-20 00:28:30 +08004309 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
4310 return -1;
4311 }
Mao Flynn7b379f32015-04-20 00:28:30 +08004312
Mao Flynn1893a7f2015-06-03 12:03:36 +08004313 fbcon_extract_to_screen(header, (base + LOGO_IMG_HEADER_SIZE));
4314 } else { /* 2 Raw BGR data */
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304315
Veera Sundaram Sankaranc33416f2015-09-28 10:25:55 -07004316 if ((header->width > fb_display->width) || (header->height > fb_display->height)) {
4317 dprintf(CRITICAL, "Logo config greater than fb config. Fall back default logo\n");
Mao Flynn1893a7f2015-06-03 12:03:36 +08004318 return -1;
4319 }
4320
4321 realsize = header->width * header->height * fb_display->bpp / 8;
4322 readsize = ROUNDUP((realsize + LOGO_IMG_HEADER_SIZE), blocksize) - blocksize;
4323
4324 if (blocksize == LOGO_IMG_HEADER_SIZE) { /* read the content directly */
4325 if (mmc_read((ptn + LOGO_IMG_HEADER_SIZE), (uint32_t *)base, readsize)) {
4326 fbcon_clear();
4327 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
4328 return -1;
4329 }
4330 } else {
4331 if (mmc_read(ptn + blocksize ,
4332 (uint32_t *)(base + LOGO_IMG_OFFSET + blocksize), readsize)) {
4333 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
4334 return -1;
4335 }
4336 memmove(base, (base + LOGO_IMG_OFFSET + LOGO_IMG_HEADER_SIZE), realsize);
4337 }
4338 }
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304339 }
4340
Mao Flynn7b379f32015-04-20 00:28:30 +08004341 return 0;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304342}
4343
Mao Flynn7b379f32015-04-20 00:28:30 +08004344int fetch_image_from_partition()
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304345{
4346 if (target_is_emmc_boot()) {
4347 return splash_screen_mmc();
4348 } else {
4349 return splash_screen_flash();
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07004350 }
4351}
4352
Mayank Grover351a75e2017-05-30 20:06:08 +05304353void publish_getvar_multislot_vars()
4354{
4355 int i,count;
4356 static bool published = false;
4357 static char slot_count[MAX_RSP_SIZE];
4358 static struct ab_slot_info slot_info[AB_SUPPORTED_SLOTS];
4359 static char active_slot_suffix[MAX_RSP_SIZE];
4360 static char has_slot_pname[NUM_PARTITIONS][MAX_GET_VAR_NAME_SIZE];
4361 static char has_slot_reply[NUM_PARTITIONS][MAX_RSP_SIZE];
4362 const char *tmp;
4363 char tmpbuff[MAX_GET_VAR_NAME_SIZE];
Mayank Grover2bd84bb2017-06-20 11:06:07 +05304364 signed active_slt;
Mayank Grover351a75e2017-05-30 20:06:08 +05304365
4366 if (!published)
4367 {
4368 /* Update slot meta info */
4369 count = partition_fill_partition_meta(has_slot_pname, has_slot_reply,
4370 partition_get_partition_count());
4371 for(i=0; i<count; i++)
4372 {
4373 memset(tmpbuff, 0, MAX_GET_VAR_NAME_SIZE);
Mayank Grover2bd84bb2017-06-20 11:06:07 +05304374 snprintf(tmpbuff, MAX_GET_VAR_NAME_SIZE,"has-slot:%s",
4375 has_slot_pname[i]);
4376 strlcpy(has_slot_pname[i], tmpbuff, MAX_GET_VAR_NAME_SIZE);
Mayank Grover351a75e2017-05-30 20:06:08 +05304377 fastboot_publish(has_slot_pname[i], has_slot_reply[i]);
4378 }
4379
4380 for (i=0; i<AB_SUPPORTED_SLOTS; i++)
4381 {
4382 tmp = SUFFIX_SLOT(i);
Mayank Grover7b8a8f82017-10-27 13:07:59 +05304383 tmp++; // to remove "_" from slot_suffix.
Mayank Grover2bd84bb2017-06-20 11:06:07 +05304384 snprintf(slot_info[i].slot_is_unbootable, sizeof(slot_info[i].slot_is_unbootable),
4385 "slot-unbootable:%s", tmp);
4386 snprintf(slot_info[i].slot_is_active, sizeof(slot_info[i].slot_is_active),
4387 "slot-active:%s", tmp);
4388 snprintf(slot_info[i].slot_is_succesful, sizeof(slot_info[i].slot_is_succesful),
4389 "slot-success:%s", tmp);
4390 snprintf(slot_info[i].slot_retry_count, sizeof(slot_info[i].slot_retry_count),
4391 "slot-retry-count:%s", tmp);
Mayank Grover351a75e2017-05-30 20:06:08 +05304392 fastboot_publish(slot_info[i].slot_is_unbootable,
4393 slot_info[i].slot_is_unbootable_rsp);
4394 fastboot_publish(slot_info[i].slot_is_active,
4395 slot_info[i].slot_is_active_rsp);
4396 fastboot_publish(slot_info[i].slot_is_succesful,
4397 slot_info[i].slot_is_succesful_rsp);
4398 fastboot_publish(slot_info[i].slot_retry_count,
4399 slot_info[i].slot_retry_count_rsp);
4400 }
4401 fastboot_publish("current-slot", active_slot_suffix);
4402 snprintf(slot_count, sizeof(slot_count),"%d", AB_SUPPORTED_SLOTS);
4403 fastboot_publish("slot-count", slot_count);
4404 published = true;
4405 }
4406
Mayank Grover2bd84bb2017-06-20 11:06:07 +05304407 active_slt = partition_find_active_slot();
4408 if (active_slt != INVALID)
Mayank Grover7b8a8f82017-10-27 13:07:59 +05304409 {
4410 tmp = SUFFIX_SLOT(active_slt);
4411 tmp++; // to remove "_" from slot_suffix.
4412 snprintf(active_slot_suffix, sizeof(active_slot_suffix), "%s", tmp);
4413 }
Mayank Grover2bd84bb2017-06-20 11:06:07 +05304414 else
4415 strlcpy(active_slot_suffix, "INVALID", sizeof(active_slot_suffix));
4416
Mayank Grover351a75e2017-05-30 20:06:08 +05304417 /* Update partition meta information */
4418 partition_fill_slot_meta(slot_info);
4419 return;
4420}
4421
lijuang4ece1e72015-08-14 21:02:36 +08004422void get_product_name(unsigned char *buf)
4423{
4424 snprintf((char*)buf, MAX_RSP_SIZE, "%s", TARGET(BOARD));
4425 return;
4426}
4427
Mayank Grover7b8a8f82017-10-27 13:07:59 +05304428#if PRODUCT_IOT
4429void get_bootloader_version_iot(unsigned char *buf)
4430{
4431 if (buf != NULL)
4432 {
4433 strlcpy(buf, TARGET(BOARD), MAX_VERSION_LEN);
4434 strlcat(buf, "-", MAX_VERSION_LEN);
4435 strlcat(buf, PRODUCT_IOT_VERSION, MAX_VERSION_LEN);
4436 }
4437 return;
4438}
4439#endif
4440
lijuang4ece1e72015-08-14 21:02:36 +08004441void get_bootloader_version(unsigned char *buf)
4442{
4443 snprintf((char*)buf, MAX_RSP_SIZE, "%s", device.bootloader_version);
4444 return;
4445}
4446
4447void get_baseband_version(unsigned char *buf)
4448{
4449 snprintf((char*)buf, MAX_RSP_SIZE, "%s", device.radio_version);
4450 return;
4451}
4452
4453bool is_device_locked()
4454{
4455 return device.is_unlocked ? false:true;
4456}
4457
Amol Jadi5edf3552013-07-23 14:15:34 -07004458/* register commands and variables for fastboot */
4459void aboot_fastboot_register_commands(void)
4460{
Sridhar Parasurame94e8152014-10-24 14:06:03 -07004461 int i;
lijuangf16461c2015-08-03 17:09:34 +08004462 char hw_platform_buf[MAX_RSP_SIZE];
Amol Jadi5edf3552013-07-23 14:15:34 -07004463
Sridhar Parasurame94e8152014-10-24 14:06:03 -07004464 struct fastboot_cmd_desc cmd_list[] = {
lijuang511a2b52015-08-14 20:50:51 +08004465 /* By default the enabled list is empty. */
4466 {"", NULL},
4467 /* move commands enclosed within the below ifndef to here
4468 * if they need to be enabled in user build.
4469 */
Sridhar Parasurame94e8152014-10-24 14:06:03 -07004470#ifndef DISABLE_FASTBOOT_CMDS
lijuang511a2b52015-08-14 20:50:51 +08004471 /* Register the following commands only for non-user builds */
4472 {"flash:", cmd_flash},
4473 {"erase:", cmd_erase},
4474 {"boot", cmd_boot},
4475 {"continue", cmd_continue},
4476 {"reboot", cmd_reboot},
4477 {"reboot-bootloader", cmd_reboot_bootloader},
4478 {"oem unlock", cmd_oem_unlock},
4479 {"oem unlock-go", cmd_oem_unlock_go},
4480 {"oem lock", cmd_oem_lock},
4481 {"flashing unlock", cmd_oem_unlock},
4482 {"flashing lock", cmd_oem_lock},
4483 {"flashing lock_critical", cmd_flashing_lock_critical},
4484 {"flashing unlock_critical", cmd_flashing_unlock_critical},
4485 {"flashing get_unlock_ability", cmd_flashing_get_unlock_ability},
4486 {"oem device-info", cmd_oem_devinfo},
4487 {"preflash", cmd_preflash},
4488 {"oem enable-charger-screen", cmd_oem_enable_charger_screen},
4489 {"oem disable-charger-screen", cmd_oem_disable_charger_screen},
lijuanga25d8bb2015-09-16 13:11:52 +08004490 {"oem off-mode-charge", cmd_oem_off_mode_charger},
lijuang511a2b52015-08-14 20:50:51 +08004491 {"oem select-display-panel", cmd_oem_select_display_panel},
Mayank Grover6ccc1c92017-07-04 17:36:46 +05304492 {"set_active",cmd_set_active},
Channagoud Kadabi736c4962015-08-21 11:56:52 -07004493#if UNITTEST_FW_SUPPORT
Channagoud Kadabid5ddf482015-09-28 10:31:35 -07004494 {"oem run-tests", cmd_oem_runtests},
Channagoud Kadabi736c4962015-08-21 11:56:52 -07004495#endif
Sridhar Parasurame94e8152014-10-24 14:06:03 -07004496#endif
lijuang511a2b52015-08-14 20:50:51 +08004497 };
Sridhar Parasurame94e8152014-10-24 14:06:03 -07004498
4499 int fastboot_cmds_count = sizeof(cmd_list)/sizeof(cmd_list[0]);
4500 for (i = 1; i < fastboot_cmds_count; i++)
4501 fastboot_register(cmd_list[i].name,cmd_list[i].cb);
4502
Amol Jadi5edf3552013-07-23 14:15:34 -07004503 /* publish variables and their values */
4504 fastboot_publish("product", TARGET(BOARD));
4505 fastboot_publish("kernel", "lk");
4506 fastboot_publish("serialno", sn_buf);
4507
4508 /*
4509 * partition info is supported only for emmc partitions
4510 * Calling this for NAND prints some error messages which
4511 * is harmless but misleading. Avoid calling this for NAND
4512 * devices.
4513 */
4514 if (target_is_emmc_boot())
Mayank Grover7b8a8f82017-10-27 13:07:59 +05304515 publish_getvar_partition_info(part_info, partition_get_partition_count());
Amol Jadi5edf3552013-07-23 14:15:34 -07004516
Mayank Grover351a75e2017-05-30 20:06:08 +05304517 if (partition_multislot_is_supported())
4518 publish_getvar_multislot_vars();
4519
Amol Jadi5edf3552013-07-23 14:15:34 -07004520 /* Max download size supported */
Ameya Thakur11cf1a62013-08-05 12:44:48 -07004521 snprintf(max_download_size, MAX_RSP_SIZE, "\t0x%x",
4522 target_get_max_flash_size());
Amol Jadi5edf3552013-07-23 14:15:34 -07004523 fastboot_publish("max-download-size", (const char *) max_download_size);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07004524 /* Is the charger screen check enabled */
4525 snprintf(charger_screen_enabled, MAX_RSP_SIZE, "%d",
4526 device.charger_screen_enabled);
4527 fastboot_publish("charger-screen-enabled",
4528 (const char *) charger_screen_enabled);
lijuanga25d8bb2015-09-16 13:11:52 +08004529 fastboot_publish("off-mode-charge", (const char *) charger_screen_enabled);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05304530 snprintf(panel_display_mode, MAX_RSP_SIZE, "%s",
4531 device.display_panel);
4532 fastboot_publish("display-panel",
4533 (const char *) panel_display_mode);
Mayank Grover7b8a8f82017-10-27 13:07:59 +05304534#if PRODUCT_IOT
4535 get_bootloader_version_iot(&bootloader_version_string);
4536 fastboot_publish("version-bootloader", (const char *) bootloader_version_string);
4537
4538 /* Version baseband is n/a for apq iot devices */
4539 fastboot_publish("version-baseband", "N/A");
4540
4541 /* IOT targets support only mmc target */
4542 snprintf(block_size_string, MAX_RSP_SIZE, "0x%x", mmc_get_device_blocksize());
4543 fastboot_publish("erase-block-size", (const char *) block_size_string);
4544 fastboot_publish("logical-block-size", (const char *) block_size_string);
4545#else
Channagoud Kadabiaafbbfc2015-03-04 17:52:56 -08004546 fastboot_publish("version-bootloader", (const char *) device.bootloader_version);
4547 fastboot_publish("version-baseband", (const char *) device.radio_version);
Mayank Grover7b8a8f82017-10-27 13:07:59 +05304548#endif
lijuangf16461c2015-08-03 17:09:34 +08004549 fastboot_publish("secure", is_secure_boot_enable()? "yes":"no");
Mayank Grover7b8a8f82017-10-27 13:07:59 +05304550 fastboot_publish("unlocked", device.is_unlocked ? "yes":"no");
lijuangf16461c2015-08-03 17:09:34 +08004551 smem_get_hw_platform_name((unsigned char *) hw_platform_buf, sizeof(hw_platform_buf));
4552 snprintf(get_variant, MAX_RSP_SIZE, "%s %s", hw_platform_buf,
4553 target_is_emmc_boot()? "eMMC":"UFS");
4554 fastboot_publish("variant", (const char *) get_variant);
lijuang65c5a822015-08-29 16:35:36 +08004555#if CHECK_BAT_VOLTAGE
lijuang102dfa92015-10-09 18:31:03 +08004556 update_battery_status();
lijuang65c5a822015-08-29 16:35:36 +08004557 fastboot_publish("battery-voltage", (const char *) battery_voltage);
lijuang102dfa92015-10-09 18:31:03 +08004558 fastboot_publish("battery-soc-ok", (const char *) battery_soc_ok);
lijuang65c5a822015-08-29 16:35:36 +08004559#endif
Amol Jadi5edf3552013-07-23 14:15:34 -07004560}
4561
Brian Swetland9c4c0752009-01-25 16:23:50 -08004562void aboot_init(const struct app_descriptor *app)
4563{
Shashank Mittal4f99a882010-02-01 13:58:50 -08004564 unsigned reboot_mode = 0;
Mayank Grover351a75e2017-05-30 20:06:08 +05304565 int boot_err_type = 0;
4566 int boot_slot = INVALID;
Chandan Uddarajubedca152010-06-02 23:05:15 -07004567
lijuang39831732016-01-08 17:49:02 +08004568 /* Initialise wdog to catch early lk crashes */
4569#if WDOG_SUPPORT
4570 msm_wdog_init();
4571#endif
4572
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07004573 /* Setup page size information for nv storage */
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004574 if (target_is_emmc_boot())
4575 {
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07004576 page_size = mmc_page_size();
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004577 page_mask = page_size - 1;
Gaurav Nebhwani4d2389c2016-03-17 21:10:05 +05304578 mmc_blocksize = mmc_get_device_blocksize();
4579 mmc_blocksize_mask = mmc_blocksize - 1;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004580 }
4581 else
4582 {
4583 page_size = flash_page_size();
4584 page_mask = page_size - 1;
4585 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07004586 ASSERT((MEMBASE + MEMSIZE) > MEMBASE);
4587
Ameya Thakur11cf1a62013-08-05 12:44:48 -07004588 read_device_info(&device);
vijay kumarc65876c2015-04-24 13:29:16 +05304589 read_allow_oem_unlock(&device);
Shashank Mittal162244e2011-08-08 19:01:25 -07004590
Mayank Grover351a75e2017-05-30 20:06:08 +05304591 /* Detect multi-slot support */
4592 if (partition_multislot_is_supported())
4593 {
4594 boot_slot = partition_find_active_slot();
4595 if (boot_slot == INVALID)
4596 {
4597 boot_into_fastboot = true;
4598 dprintf(INFO, "Active Slot: (INVALID)\n");
4599 }
4600 else
4601 {
4602 /* Setting the state of system to boot active slot */
4603 partition_mark_active_slot(boot_slot);
4604 dprintf(INFO, "Active Slot: (%s)\n", SUFFIX_SLOT(boot_slot));
4605 }
4606 }
4607
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08004608 /* Display splash screen if enabled */
4609#if DISPLAY_SPLASH_SCREEN
lijuang99c02d82015-02-13 19:04:34 +08004610#if NO_ALARM_DISPLAY
4611 if (!check_alarm_boot()) {
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08004612#endif
lijuang99c02d82015-02-13 19:04:34 +08004613 dprintf(SPEW, "Display Init: Start\n");
Ajay Singh Parmara7865a82015-04-16 21:27:52 -07004614#if DISPLAY_HDMI_PRIMARY
4615 if (!strlen(device.display_panel))
4616 strlcpy(device.display_panel, DISPLAY_PANEL_HDMI,
4617 sizeof(device.display_panel));
4618#endif
Channagoud Kadabi90869ce2015-04-27 11:15:14 -07004619#if ENABLE_WBC
Channagoud Kadabid801ac72015-08-26 11:21:51 -07004620 /* Wait if the display shutdown is in progress */
4621 while(pm_app_display_shutdown_in_prgs());
Channagoud Kadabi90869ce2015-04-27 11:15:14 -07004622 if (!pm_appsbl_display_init_done())
4623 target_display_init(device.display_panel);
4624 else
4625 display_image_on_screen();
4626#else
lijuang99c02d82015-02-13 19:04:34 +08004627 target_display_init(device.display_panel);
Channagoud Kadabi90869ce2015-04-27 11:15:14 -07004628#endif
lijuang99c02d82015-02-13 19:04:34 +08004629 dprintf(SPEW, "Display Init: Done\n");
4630#if NO_ALARM_DISPLAY
4631 }
4632#endif
4633#endif
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08004634
Greg Griscod6250552011-06-29 14:40:23 -07004635 target_serialno((unsigned char *) sn_buf);
Ajay Dudanib06c05f2011-05-12 14:46:10 -07004636 dprintf(SPEW,"serial number: %s\n",sn_buf);
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -08004637
Dhaval Patel223ec952013-07-18 14:49:44 -07004638 memset(display_panel_buf, '\0', MAX_PANEL_BUF_SIZE);
4639
Matthew Qindefd5562014-07-11 18:02:40 +08004640 /*
4641 * Check power off reason if user force reset,
4642 * if yes phone will do normal boot.
4643 */
4644 if (is_user_force_reset())
4645 goto normal_boot;
4646
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07004647 /* Check if we should do something other than booting up */
Ameya Thakur0b9c2442013-05-10 13:22:56 -07004648 if (keys_get_state(KEY_VOLUMEUP) && keys_get_state(KEY_VOLUMEDOWN))
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07004649 {
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03004650 dprintf(ALWAYS,"dload mode key sequence detected\n");
lijuang395b5e62015-11-19 17:39:44 +08004651 reboot_device(EMERGENCY_DLOAD);
4652 dprintf(CRITICAL,"Failed to reboot into dload mode\n");
4653
Ameya Thakur0b9c2442013-05-10 13:22:56 -07004654 boot_into_fastboot = true;
4655 }
4656 if (!boot_into_fastboot)
4657 {
4658 if (keys_get_state(KEY_HOME) || keys_get_state(KEY_VOLUMEUP))
4659 boot_into_recovery = 1;
4660 if (!boot_into_recovery &&
4661 (keys_get_state(KEY_BACK) || keys_get_state(KEY_VOLUMEDOWN)))
Pavel Nedev5d91d412013-04-29 11:34:24 +03004662 boot_into_fastboot = true;
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07004663 }
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004664 #if NO_KEYPAD_DRIVER
Kinson Chik0b1c8162011-08-31 16:31:57 -07004665 if (fastboot_trigger())
Pavel Nedev5d91d412013-04-29 11:34:24 +03004666 boot_into_fastboot = true;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004667 #endif
Chandan Uddarajubedca152010-06-02 23:05:15 -07004668
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07004669#if USE_PON_REBOOT_REG
4670 reboot_mode = check_hard_reboot_mode();
4671#else
Ajay Dudani77421292010-10-27 19:34:06 -07004672 reboot_mode = check_reboot_mode();
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07004673#endif
4674 if (reboot_mode == RECOVERY_MODE)
4675 {
Ajay Dudani77421292010-10-27 19:34:06 -07004676 boot_into_recovery = 1;
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07004677 }
4678 else if(reboot_mode == FASTBOOT_MODE)
4679 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03004680 boot_into_fastboot = true;
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07004681 }
4682 else if(reboot_mode == ALARM_BOOT)
4683 {
Matthew Qind886f3c2014-01-17 16:52:01 +08004684 boot_reason_alarm = true;
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07004685 }
Parth Dixit207573a2015-10-27 17:26:21 +05304686#if VERIFIED_BOOT
Mayank Grover912eaa62017-10-26 12:08:53 +05304687 else if (VB_M <= target_get_vb_version())
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07004688 {
Mayank Grover889be1b2017-09-12 20:12:23 +05304689 if (reboot_mode == DM_VERITY_ENFORCING)
4690 {
4691 device.verity_mode = 1;
4692 write_device_info(&device);
4693 }
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07004694#if ENABLE_VB_ATTEST
Mayank Grover889be1b2017-09-12 20:12:23 +05304695 else if (reboot_mode == DM_VERITY_EIO)
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07004696#else
Mayank Grover889be1b2017-09-12 20:12:23 +05304697 else if (reboot_mode == DM_VERITY_LOGGING)
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07004698#endif
Mayank Grover889be1b2017-09-12 20:12:23 +05304699 {
4700 device.verity_mode = 0;
4701 write_device_info(&device);
4702 }
4703 else if (reboot_mode == DM_VERITY_KEYSCLEAR)
4704 {
4705 if(send_delete_keys_to_tz())
4706 ASSERT(0);
4707 }
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07004708 }
Parth Dixitddbc7352015-10-18 03:13:31 +05304709#endif
Ajay Dudani77421292010-10-27 19:34:06 -07004710
Matthew Qindefd5562014-07-11 18:02:40 +08004711normal_boot:
Pavel Nedev5d91d412013-04-29 11:34:24 +03004712 if (!boot_into_fastboot)
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004713 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03004714 if (target_is_emmc_boot())
Shashank Mittala0032282011-08-26 14:50:11 -07004715 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03004716 if(emmc_recovery_init())
4717 dprintf(ALWAYS,"error in emmc_recovery_init\n");
4718 if(target_use_signed_kernel())
Shashank Mittala0032282011-08-26 14:50:11 -07004719 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03004720 if((device.is_unlocked) || (device.is_tampered))
4721 {
4722 #ifdef TZ_TAMPER_FUSE
4723 set_tamper_fuse_cmd();
4724 #endif
4725 #if USE_PCOM_SECBOOT
4726 set_tamper_flag(device.is_tampered);
4727 #endif
4728 }
Shashank Mittala0032282011-08-26 14:50:11 -07004729 }
Amit Blay6281ebc2015-01-11 14:44:08 +02004730
Mayank Grover351a75e2017-05-30 20:06:08 +05304731retry_boot:
4732 /* Trying to boot active partition */
4733 if (partition_multislot_is_supported())
4734 {
4735 boot_slot = partition_find_boot_slot();
4736 partition_mark_active_slot(boot_slot);
4737 if (boot_slot == INVALID)
4738 goto fastboot;
4739 }
4740
4741 boot_err_type = boot_linux_from_mmc();
4742 switch (boot_err_type)
4743 {
4744 case ERR_INVALID_PAGE_SIZE:
4745 case ERR_DT_PARSE:
4746 case ERR_ABOOT_ADDR_OVERLAP:
Mayank Grover10cabbe2017-10-30 19:11:05 +05304747 case ERR_INVALID_BOOT_MAGIC:
Mayank Grover351a75e2017-05-30 20:06:08 +05304748 if(partition_multislot_is_supported())
Mayank Grover10cabbe2017-10-30 19:11:05 +05304749 {
4750 /*
4751 * Deactivate current slot, as it failed to
4752 * boot, and retry next slot.
4753 */
4754 partition_deactivate_slot(boot_slot);
Mayank Grover351a75e2017-05-30 20:06:08 +05304755 goto retry_boot;
Mayank Grover10cabbe2017-10-30 19:11:05 +05304756 }
Mayank Grover351a75e2017-05-30 20:06:08 +05304757 else
4758 break;
Mayank Grover351a75e2017-05-30 20:06:08 +05304759 default:
4760 break;
4761 /* going to fastboot menu */
4762 }
Shashank Mittala0032282011-08-26 14:50:11 -07004763 }
Pavel Nedev5d91d412013-04-29 11:34:24 +03004764 else
4765 {
4766 recovery_init();
4767 #if USE_PCOM_SECBOOT
4768 if((device.is_unlocked) || (device.is_tampered))
4769 set_tamper_flag(device.is_tampered);
4770 #endif
4771 boot_linux_from_flash();
4772 }
4773 dprintf(CRITICAL, "ERROR: Could not do normal boot. Reverting "
4774 "to fastboot mode.\n");
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004775 }
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07004776
Mayank Grover351a75e2017-05-30 20:06:08 +05304777fastboot:
Amol Jadi5edf3552013-07-23 14:15:34 -07004778 /* We are here means regular boot did not happen. Start fastboot. */
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07004779
Amol Jadi5edf3552013-07-23 14:15:34 -07004780 /* register aboot specific fastboot commands */
4781 aboot_fastboot_register_commands();
Amol Jadi57abe4c2011-05-24 15:47:27 -07004782
Amol Jadi5edf3552013-07-23 14:15:34 -07004783 /* dump partition table for debug info */
Kinson Chikf1a43512011-07-14 11:28:39 -07004784 partition_dump();
Amol Jadi5edf3552013-07-23 14:15:34 -07004785
4786 /* initialize and start fastboot */
4787 fastboot_init(target_get_scratch_address(), target_get_max_flash_size());
lijuang4ece1e72015-08-14 21:02:36 +08004788#if FBCON_DISPLAY_MSG
lijuang9a7d3b92015-11-30 14:41:24 +08004789 display_fastboot_menu();
lijuang4ece1e72015-08-14 21:02:36 +08004790#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08004791}
4792
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07004793uint32_t get_page_size()
4794{
4795 return page_size;
4796}
4797
Amir Samuelov57a6fa22013-06-05 16:36:43 +03004798/*
4799 * Calculated and save hash (SHA256) for non-signed boot image.
4800 *
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07004801 * @param image_addr - Boot image address
4802 * @param image_size - Size of the boot image
Amir Samuelov57a6fa22013-06-05 16:36:43 +03004803 *
4804 * @return int - 0 on success, negative value on failure.
4805 */
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07004806static int aboot_save_boot_hash_mmc(uint32_t image_addr, uint32_t image_size)
Amir Samuelov57a6fa22013-06-05 16:36:43 +03004807{
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07004808 unsigned int digest[8];
4809#if IMAGE_VERIF_ALGO_SHA1
4810 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA1;
4811#else
4812 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA256;
4813#endif
Amir Samuelov57a6fa22013-06-05 16:36:43 +03004814
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07004815 target_crypto_init_params();
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -08004816 hash_find((unsigned char *) image_addr, image_size, (unsigned char *)&digest, auth_algo);
Amir Samuelov57a6fa22013-06-05 16:36:43 +03004817
4818 save_kernel_hash_cmd(digest);
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07004819 dprintf(INFO, "aboot_save_boot_hash_mmc: imagesize_actual size %d bytes.\n", (int) image_size);
Amir Samuelov57a6fa22013-06-05 16:36:43 +03004820
4821 return 0;
4822}
4823
Mayank Grover351a75e2017-05-30 20:06:08 +05304824
Brian Swetland9c4c0752009-01-25 16:23:50 -08004825APP_START(aboot)
4826 .init = aboot_init,
4827APP_END