blob: e39bad0d5fa07e116c18028b546f158b1c67eef0 [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
Mayank Grover68fbf0d2017-10-24 14:13:39 +05301133 char *ptn_name = NULL;
1134 if (boot_into_recovery &&
1135 (!partition_multislot_is_supported()))
1136 ptn_name = "/recovery";
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001137 else
Mayank Grover68fbf0d2017-10-24 14:13:39 +05301138 ptn_name = "/boot";
1139
1140 ret = boot_verify_image((unsigned char *)bootimg_addr,
1141 bootimg_size, ptn_name);
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001142 boot_verify_print_state();
1143#else
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001144 ret = image_verify((unsigned char *)bootimg_addr,
1145 (unsigned char *)(bootimg_addr + bootimg_size),
1146 bootimg_size,
Sundarajan Srinivasance54d6e2013-11-11 12:45:00 -08001147 auth_algo);
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001148#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001149 dprintf(INFO, "Authenticating boot image: done return value = %d\n", ret);
1150
1151 if (ret)
1152 {
1153 /* Authorized kernel */
1154 device.is_tampered = 0;
Sundarajan Srinivasan3fb21f12013-09-16 18:36:15 -07001155 auth_kernel_img = 1;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001156 }
1157
Amit Blay4aa292f2015-04-28 21:55:59 +03001158#ifdef MDTP_SUPPORT
1159 {
1160 /* Verify MDTP lock.
1161 * For boot & recovery partitions, use aboot's verification result.
1162 */
1163 mdtp_ext_partition_verification_t ext_partition;
1164 ext_partition.partition = boot_into_recovery ? MDTP_PARTITION_RECOVERY : MDTP_PARTITION_BOOT;
1165 ext_partition.integrity_state = device.is_tampered ? MDTP_PARTITION_STATE_INVALID : MDTP_PARTITION_STATE_VALID;
1166 ext_partition.page_size = 0; /* Not needed since already validated */
1167 ext_partition.image_addr = 0; /* Not needed since already validated */
1168 ext_partition.image_size = 0; /* Not needed since already validated */
1169 ext_partition.sig_avail = FALSE; /* Not needed since already validated */
1170 mdtp_fwlock_verify_lock(&ext_partition);
1171 }
1172#endif /* MDTP_SUPPORT */
1173
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001174#if USE_PCOM_SECBOOT
1175 set_tamper_flag(device.is_tampered);
1176#endif
1177
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001178#if VERIFIED_BOOT
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001179 switch(boot_verify_get_state())
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001180 {
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001181 case RED:
lijuanga40d6302015-07-20 20:10:13 +08001182#if FBCON_DISPLAY_MSG
lijuang9a7d3b92015-11-30 14:41:24 +08001183 display_bootverify_menu(DISPLAY_MENU_RED);
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07001184#if ENABLE_VB_ATTEST
1185 mdelay(DELAY_WAIT);
1186 shutdown_device();
1187#else
lijuanga40d6302015-07-20 20:10:13 +08001188 wait_for_users_action();
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07001189#endif
lijuanga40d6302015-07-20 20:10:13 +08001190#else
1191 dprintf(CRITICAL,
1192 "Your device has failed verification and may not work properly.\nWait for 5 seconds before proceeding\n");
1193 mdelay(5000);
1194#endif
1195
1196 break;
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001197 case YELLOW:
lijuanga40d6302015-07-20 20:10:13 +08001198#if FBCON_DISPLAY_MSG
lijuang9a7d3b92015-11-30 14:41:24 +08001199 display_bootverify_menu(DISPLAY_MENU_YELLOW);
lijuanga40d6302015-07-20 20:10:13 +08001200 wait_for_users_action();
1201#else
1202 dprintf(CRITICAL,
1203 "Your device has loaded a different operating system.\nWait for 5 seconds before proceeding\n");
1204 mdelay(5000);
1205#endif
1206 break;
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001207 default:
lijuanga40d6302015-07-20 20:10:13 +08001208 break;
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001209 }
1210#endif
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001211#if !VERIFIED_BOOT
Unnati Gandhi1be04752015-03-27 19:41:53 +05301212 if(device.is_tampered)
1213 {
1214 write_device_info_mmc(&device);
1215 #ifdef TZ_TAMPER_FUSE
1216 set_tamper_fuse_cmd();
1217 #endif
1218 #ifdef ASSERT_ON_TAMPER
1219 dprintf(CRITICAL, "Device is tampered. Asserting..\n");
1220 ASSERT(0);
1221 #endif
1222 }
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001223#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001224}
1225
Monika Singh292b3e92018-03-17 22:40:23 +05301226int get_boot_image_info(void **image_buffer, uint32_t *imgsize,char *imgname)
1227{
1228 if (image_buffer == NULL || imgsize == NULL || imgname == NULL) {
1229 dprintf(CRITICAL, "get_boot_image_info: invalid parameters\n");
1230 return -1;
1231 }
1232
1233 if (!strncmp(info.images[0].name, imgname,
1234 strlen(imgname))) {
1235 *image_buffer = info.images[0].image_buffer;
1236 *imgsize = info.images[0].imgsize;
1237 return 0;
1238 }
1239 return -1;
1240}
1241
Maunik Shah0f3c8ac2014-03-02 17:47:58 +05301242static bool check_format_bit()
1243{
1244 bool ret = false;
1245 int index;
1246 uint64_t offset;
1247 struct boot_selection_info *in = NULL;
1248 char *buf = NULL;
1249
1250 index = partition_get_index("bootselect");
1251 if (index == INVALID_PTN)
1252 {
1253 dprintf(INFO, "Unable to locate /bootselect partition\n");
1254 return ret;
1255 }
1256 offset = partition_get_offset(index);
1257 if(!offset)
1258 {
1259 dprintf(INFO, "partition /bootselect doesn't exist\n");
1260 return ret;
1261 }
1262 buf = (char *) memalign(CACHE_LINE, ROUNDUP(page_size, CACHE_LINE));
Mayank Grover48860402016-11-29 12:34:53 +05301263 mmc_set_lun(partition_get_lun(index));
Maunik Shah0f3c8ac2014-03-02 17:47:58 +05301264 ASSERT(buf);
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301265 if (mmc_read(offset, (uint32_t *)buf, page_size))
Maunik Shah0f3c8ac2014-03-02 17:47:58 +05301266 {
1267 dprintf(INFO, "mmc read failure /bootselect %d\n", page_size);
1268 free(buf);
1269 return ret;
1270 }
1271 in = (struct boot_selection_info *) buf;
1272 if ((in->signature == BOOTSELECT_SIGNATURE) &&
1273 (in->version == BOOTSELECT_VERSION)) {
1274 if ((in->state_info & BOOTSELECT_FORMAT) &&
1275 !(in->state_info & BOOTSELECT_FACTORY))
1276 ret = true;
1277 } else {
1278 dprintf(CRITICAL, "Signature: 0x%08x or version: 0x%08x mismatched of /bootselect\n",
1279 in->signature, in->version);
1280 ASSERT(0);
1281 }
1282 free(buf);
1283 return ret;
1284}
1285
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001286void boot_verifier_init()
1287{
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001288 uint32_t boot_state;
1289 /* Check if device unlock */
1290 if(device.is_unlocked)
1291 {
1292 boot_verify_send_event(DEV_UNLOCK);
1293 boot_verify_print_state();
1294 dprintf(CRITICAL, "Device is unlocked! Skipping verification...\n");
1295 return;
1296 }
1297 else
1298 {
1299 boot_verify_send_event(BOOT_INIT);
1300 }
1301
1302 /* Initialize keystore */
1303 boot_state = boot_verify_keystore_init();
1304 if(boot_state == YELLOW)
1305 {
1306 boot_verify_print_state();
1307 dprintf(CRITICAL, "Keystore verification failed! Continuing anyways...\n");
1308 }
1309}
1310
Shashank Mittal23b8f422010-04-16 19:27:21 -07001311int boot_linux_from_mmc(void)
1312{
1313 struct boot_img_hdr *hdr = (void*) buf;
1314 struct boot_img_hdr *uhdr;
1315 unsigned offset = 0;
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07001316 int rcode;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001317 unsigned long long ptn = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001318 int index = INVALID_PTN;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001319
Shashank Mittalcd98d472011-08-02 14:29:24 -07001320 unsigned char *image_addr = 0;
1321 unsigned kernel_actual;
1322 unsigned ramdisk_actual;
1323 unsigned imagesize_actual;
Neeti Desai465491e2012-07-31 12:53:35 -07001324 unsigned second_actual = 0;
Neeti Desai465491e2012-07-31 12:53:35 -07001325
Matthew Qin271927e2015-03-31 22:07:07 -04001326 unsigned int dtb_size = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08001327 unsigned int out_len = 0;
1328 unsigned int out_avai_len = 0;
1329 unsigned char *out_addr = NULL;
1330 uint32_t dtb_offset = 0;
1331 unsigned char *kernel_start_addr = NULL;
1332 unsigned int kernel_size = 0;
Ameya Thakur10a33452016-06-13 14:24:26 -07001333 unsigned int patched_kernel_hdr_size = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08001334 int rc;
Monika Singh292b3e92018-03-17 22:40:23 +05301335#if VERIFIED_BOOT_2
1336 int status;
1337#endif
Mayank Groverc93cad82017-10-03 12:23:45 +05301338 char *ptn_name = NULL;
Neeti Desai465491e2012-07-31 12:53:35 -07001339#if DEVICE_TREE
1340 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -07001341 struct dt_entry dt_entry;
Neeti Desai465491e2012-07-31 12:53:35 -07001342 unsigned dt_table_offset;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001343 uint32_t dt_actual;
Deepa Dinamani19648b42013-09-05 17:05:55 -07001344 uint32_t dt_hdr_size;
Matthew Qin271927e2015-03-31 22:07:07 -04001345 unsigned char *best_match_dt_addr = NULL;
Neeti Desai465491e2012-07-31 12:53:35 -07001346#endif
Matthew Qin49e51fa2015-02-09 10:40:45 +08001347 struct kernel64_hdr *kptr = NULL;
Mayank Grover351a75e2017-05-30 20:06:08 +05301348 int current_active_slot = INVALID;
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001349
Maunik Shah0f3c8ac2014-03-02 17:47:58 +05301350 if (check_format_bit())
1351 boot_into_recovery = 1;
1352
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07001353 if (!boot_into_recovery) {
1354 memset(ffbm_mode_string, '\0', sizeof(ffbm_mode_string));
1355 rcode = get_ffbm(ffbm_mode_string, sizeof(ffbm_mode_string));
1356 if (rcode <= 0) {
1357 boot_into_ffbm = false;
1358 if (rcode < 0)
1359 dprintf(CRITICAL,"failed to get ffbm cookie");
1360 } else
1361 boot_into_ffbm = true;
1362 } else
1363 boot_into_ffbm = false;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001364 uhdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
1365 if (!memcmp(uhdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1366 dprintf(INFO, "Unified boot method!\n");
1367 hdr = uhdr;
1368 goto unified_boot;
1369 }
Mayank Groverc93cad82017-10-03 12:23:45 +05301370
1371 /* For a/b recovery image code is on boot partition.
1372 If we support multislot, always use boot partition. */
1373 if (boot_into_recovery &&
1374 (!partition_multislot_is_supported()))
1375 ptn_name = "recovery";
1376 else
1377 ptn_name = "boot";
1378
1379 index = partition_get_index(ptn_name);
1380 ptn = partition_get_offset(index);
1381 if(ptn == 0) {
1382 dprintf(CRITICAL, "ERROR: No %s partition found\n", ptn_name);
1383 return -1;
Kinson Chikf1a43512011-07-14 11:28:39 -07001384 }
Mayank Groverc93cad82017-10-03 12:23:45 +05301385
Channagoud Kadabief0547c2015-02-10 12:57:38 -08001386 /* Set Lun for boot & recovery partitions */
1387 mmc_set_lun(partition_get_lun(index));
Shashank Mittal23b8f422010-04-16 19:27:21 -07001388
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301389 if (mmc_read(ptn + offset, (uint32_t *) buf, page_size)) {
Shashank Mittal23b8f422010-04-16 19:27:21 -07001390 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
1391 return -1;
1392 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001393
1394 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001395 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Mayank Grover351a75e2017-05-30 20:06:08 +05301396 return ERR_INVALID_BOOT_MAGIC;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001397 }
1398
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07001399 if (hdr->page_size && (hdr->page_size != page_size)) {
vijay kumar2e21b3a2014-06-26 17:40:15 +05301400
1401 if (hdr->page_size > BOOT_IMG_MAX_PAGE_SIZE) {
1402 dprintf(CRITICAL, "ERROR: Invalid page size\n");
1403 return -1;
1404 }
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07001405 page_size = hdr->page_size;
1406 page_mask = page_size - 1;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001407 }
1408
Matthew Qin49e51fa2015-02-09 10:40:45 +08001409 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1410 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Kishor PKd8ddcad2017-07-27 13:53:57 +05301411 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001412
Matthew Qin49e51fa2015-02-09 10:40:45 +08001413 image_addr = (unsigned char *)target_get_scratch_address();
Kishor PK9e91b592017-05-24 12:14:55 +05301414 memcpy(image_addr, (void *)buf, page_size);
1415
1416 /* ensure commandline is terminated */
1417 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
Matthew Qin49e51fa2015-02-09 10:40:45 +08001418
1419#if DEVICE_TREE
Parth Dixit4097b622016-03-15 14:42:27 +05301420#ifndef OSVERSION_IN_BOOTIMAGE
1421 dt_size = hdr->dt_size;
1422#endif
1423 dt_actual = ROUND_TO_PAGE(dt_size, page_mask);
Kishor PKd8ddcad2017-07-27 13:53:57 +05301424 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 +05301425 dprintf(CRITICAL, "Integer overflow detected in bootimage header fields at %u in %s\n",__LINE__,__FILE__);
1426 return -1;
1427 }
Kishor PKd8ddcad2017-07-27 13:53:57 +05301428 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + second_actual + dt_actual);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001429#else
Kishor PKd8ddcad2017-07-27 13:53:57 +05301430 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 +05301431 dprintf(CRITICAL, "Integer overflow detected in bootimage header fields at %u in %s\n",__LINE__,__FILE__);
1432 return -1;
1433 }
Kishor PKd8ddcad2017-07-27 13:53:57 +05301434 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + second_actual);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001435#endif
1436
1437#if VERIFIED_BOOT
1438 boot_verifier_init();
1439#endif
1440
Vijay Kumar Pendoti9c002ad2016-03-09 13:52:45 +05301441 if (check_aboot_addr_range_overlap((uintptr_t) image_addr, imagesize_actual))
Matthew Qin49e51fa2015-02-09 10:40:45 +08001442 {
1443 dprintf(CRITICAL, "Boot image buffer address overlaps with aboot addresses.\n");
1444 return -1;
1445 }
1446
Matthew Qinbb7923d2015-02-09 10:56:09 +08001447 /*
1448 * Update loading flow of bootimage to support compressed/uncompressed
1449 * bootimage on both 64bit and 32bit platform.
1450 * 1. Load bootimage from emmc partition onto DDR.
1451 * 2. Check if bootimage is gzip format. If yes, decompress compressed kernel
1452 * 3. Check kernel header and update kernel load addr for 64bit and 32bit
1453 * platform accordingly.
1454 * 4. Sanity Check on kernel_addr and ramdisk_addr and copy data.
1455 */
Mayank Grover351a75e2017-05-30 20:06:08 +05301456 if (partition_multislot_is_supported())
1457 {
1458 current_active_slot = partition_find_active_slot();
1459 dprintf(INFO, "Loading boot image (%d) active_slot(%s): start\n",
1460 imagesize_actual, SUFFIX_SLOT(current_active_slot));
1461 }
1462 else
1463 {
1464 dprintf(INFO, "Loading (%s) image (%d): start\n",
1465 (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual);
1466 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08001467 bs_set_timestamp(BS_KERNEL_LOAD_START);
1468
Gaurav Nebhwani43e2a462016-03-17 21:32:56 +05301469 if ((target_get_max_flash_size() - page_size) < imagesize_actual)
1470 {
1471 dprintf(CRITICAL, "booimage size is greater than DDR can hold\n");
1472 return -1;
1473 }
Kishor PK9e91b592017-05-24 12:14:55 +05301474 offset = page_size;
1475 /* Read image without signature and header*/
1476 if (mmc_read(ptn + offset, (void *)(image_addr + offset), imagesize_actual - page_size))
Matthew Qin49e51fa2015-02-09 10:40:45 +08001477 {
1478 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
1479 return -1;
1480 }
1481
Mayank Grover351a75e2017-05-30 20:06:08 +05301482 if (partition_multislot_is_supported())
1483 {
1484 dprintf(INFO, "Loading boot image (%d) active_slot(%s): done\n",
1485 imagesize_actual, SUFFIX_SLOT(current_active_slot));
1486 }
1487 else
1488 {
1489 dprintf(INFO, "Loading (%s) image (%d): done\n",
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001490 (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual);
1491
Mayank Grover351a75e2017-05-30 20:06:08 +05301492 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08001493 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
1494
1495 /* Authenticate Kernel */
1496 dprintf(INFO, "use_signed_kernel=%d, is_unlocked=%d, is_tampered=%d.\n",
1497 (int) target_use_signed_kernel(),
1498 device.is_unlocked,
1499 device.is_tampered);
Monika Singh292b3e92018-03-17 22:40:23 +05301500#if VERIFIED_BOOT_2
1501 offset = imagesize_actual;
1502 if (check_aboot_addr_range_overlap((uintptr_t)image_addr + offset, page_size))
1503 {
1504 dprintf(CRITICAL, "Signature read buffer address overlaps with aboot addresses.\n");
1505 return -1;
1506 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08001507
Monika Singh292b3e92018-03-17 22:40:23 +05301508 /* Read signature */
1509 if(mmc_read(ptn + offset, (void *)(image_addr + offset), page_size))
1510 {
1511 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
1512 return -1;
1513 }
1514
1515 memset(&info, 0, sizeof(bootinfo));
1516 info.images[0].image_buffer = image_addr;
1517 info.images[0].imgsize = imagesize_actual;
1518 info.images[0].name = "boot";
1519 info.num_loaded_images = 0;
1520 info.multi_slot_boot = partition_multislot_is_supported();
1521 info.bootreason_alarm = boot_reason_alarm;
1522 info.bootinto_recovery = boot_into_recovery;
1523 status = load_image_and_auth(&info);
1524 if(status)
1525 return -1;
1526
1527 vbcmdline = info.vbcmdline;
1528#else
Channagoud Kadabi736c4962015-08-21 11:56:52 -07001529 /* Change the condition a little bit to include the test framework support.
1530 * We would never reach this point if device is in fastboot mode, even if we did
1531 * that means we are in test mode, so execute kernel authentication part for the
1532 * tests */
Parth Dixitcb0c6082015-12-30 15:01:13 +05301533 if((target_use_signed_kernel() && (!device.is_unlocked)) || is_test_mode_enabled())
Matthew Qin49e51fa2015-02-09 10:40:45 +08001534 {
1535 offset = imagesize_actual;
Vijay Kumar Pendoti9c002ad2016-03-09 13:52:45 +05301536 if (check_aboot_addr_range_overlap((uintptr_t)image_addr + offset, page_size))
Matthew Qin49e51fa2015-02-09 10:40:45 +08001537 {
1538 dprintf(CRITICAL, "Signature read buffer address overlaps with aboot addresses.\n");
1539 return -1;
1540 }
1541
1542 /* Read signature */
1543 if(mmc_read(ptn + offset, (void *)(image_addr + offset), page_size))
1544 {
1545 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
1546 return -1;
1547 }
1548
1549 verify_signed_bootimg((uint32_t)image_addr, imagesize_actual);
Channagoud Kadabi736c4962015-08-21 11:56:52 -07001550 /* The purpose of our test is done here */
Parth Dixitcb0c6082015-12-30 15:01:13 +05301551 if(is_test_mode_enabled() && auth_kernel_img)
Channagoud Kadabi736c4962015-08-21 11:56:52 -07001552 return 0;
Matthew Qin49e51fa2015-02-09 10:40:45 +08001553 } else {
1554 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
1555 #ifdef TZ_SAVE_KERNEL_HASH
1556 aboot_save_boot_hash_mmc((uint32_t) image_addr, imagesize_actual);
1557 #endif /* TZ_SAVE_KERNEL_HASH */
Amit Blay4aa292f2015-04-28 21:55:59 +03001558
1559#ifdef MDTP_SUPPORT
1560 {
1561 /* Verify MDTP lock.
1562 * For boot & recovery partitions, MDTP will use boot_verifier APIs,
1563 * since verification was skipped in aboot. The signature is not part of the loaded image.
1564 */
1565 mdtp_ext_partition_verification_t ext_partition;
1566 ext_partition.partition = boot_into_recovery ? MDTP_PARTITION_RECOVERY : MDTP_PARTITION_BOOT;
1567 ext_partition.integrity_state = MDTP_PARTITION_STATE_UNSET;
1568 ext_partition.page_size = page_size;
1569 ext_partition.image_addr = (uint32)image_addr;
1570 ext_partition.image_size = imagesize_actual;
1571 ext_partition.sig_avail = FALSE;
1572 mdtp_fwlock_verify_lock(&ext_partition);
1573 }
1574#endif /* MDTP_SUPPORT */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001575 }
Monika Singh292b3e92018-03-17 22:40:23 +05301576#endif
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001577
Sridhar Parasuram00bfedb2015-05-26 14:21:27 -07001578#if VERIFIED_BOOT
taozhang93088bd2016-11-16 15:50:46 +08001579 if((boot_verify_get_state() == ORANGE) && (!boot_into_ffbm))
Reut Zysmanba8a9d52016-02-18 11:44:04 +02001580 {
1581#if FBCON_DISPLAY_MSG
1582 display_bootverify_menu(DISPLAY_MENU_ORANGE);
1583 wait_for_users_action();
1584#else
1585 dprintf(CRITICAL,
1586 "Your device has been unlocked and can't be trusted.\nWait for 5 seconds before proceeding\n");
1587 mdelay(5000);
1588#endif
1589 }
1590#endif
1591
1592#if VERIFIED_BOOT
Monika Singh292b3e92018-03-17 22:40:23 +05301593 if (VB_M == target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +05301594 {
1595 /* set boot and system versions. */
1596 set_os_version((unsigned char *)image_addr);
1597 // send root of trust
1598 if(!send_rot_command((uint32_t)device.is_unlocked))
1599 ASSERT(0);
1600 }
Parth Dixitbc9b6492015-10-18 00:41:38 +05301601#endif
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001602 /*
Matthew Qinbb7923d2015-02-09 10:56:09 +08001603 * Check if the kernel image is a gzip package. If yes, need to decompress it.
1604 * If not, continue booting.
1605 */
1606 if (is_gzip_package((unsigned char *)(image_addr + page_size), hdr->kernel_size))
1607 {
1608 out_addr = (unsigned char *)(image_addr + imagesize_actual + page_size);
1609 out_avai_len = target_get_max_flash_size() - imagesize_actual - page_size;
Matthew Qin0b15b322015-05-19 05:20:54 -04001610 dprintf(INFO, "decompressing kernel image: start\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001611 rc = decompress((unsigned char *)(image_addr + page_size),
1612 hdr->kernel_size, out_addr, out_avai_len,
1613 &dtb_offset, &out_len);
1614 if (rc)
1615 {
Matthew Qin0b15b322015-05-19 05:20:54 -04001616 dprintf(CRITICAL, "decompressing kernel image failed!!!\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001617 ASSERT(0);
1618 }
1619
Matthew Qin0b15b322015-05-19 05:20:54 -04001620 dprintf(INFO, "decompressing kernel image: done\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001621 kptr = (struct kernel64_hdr *)out_addr;
1622 kernel_start_addr = out_addr;
1623 kernel_size = out_len;
1624 } else {
Ameya Thakur10a33452016-06-13 14:24:26 -07001625 dprintf(INFO, "Uncpmpressed kernel in use\n");
1626 if (!strncmp((char*)(image_addr + page_size),
1627 PATCHED_KERNEL_MAGIC,
1628 sizeof(PATCHED_KERNEL_MAGIC) - 1)) {
1629 dprintf(INFO, "Patched kernel detected\n");
1630 kptr = (struct kernel64_hdr *)(image_addr + page_size +
1631 PATCHED_KERNEL_HEADER_SIZE);
1632 //The size of the kernel is stored at start of kernel image + 16
1633 //The dtb would start just after the kernel
1634 dtb_offset = *((uint32_t*)((unsigned char*)
1635 (image_addr + page_size +
1636 sizeof(PATCHED_KERNEL_MAGIC) -
1637 1)));
1638 //The actual kernel starts after the 20 byte header.
1639 kernel_start_addr = (unsigned char*)(image_addr +
1640 page_size + PATCHED_KERNEL_HEADER_SIZE);
1641 kernel_size = hdr->kernel_size;
1642 patched_kernel_hdr_size = PATCHED_KERNEL_HEADER_SIZE;
1643 } else {
1644 dprintf(INFO, "Kernel image not patched..Unable to locate dt offset\n");
1645 kptr = (struct kernel64_hdr *)(image_addr + page_size);
1646 kernel_start_addr = (unsigned char *)(image_addr + page_size);
1647 kernel_size = hdr->kernel_size;
1648 }
Matthew Qinbb7923d2015-02-09 10:56:09 +08001649 }
1650
1651 /*
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001652 * Update the kernel/ramdisk/tags address if the boot image header
1653 * has default values, these default values come from mkbootimg when
1654 * the boot image is flashed using fastboot flash:raw
1655 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001656 update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr));
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001657
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001658 /* Get virtual addresses since the hdr saves physical addresses. */
1659 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
1660 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
1661 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001662
Matthew Qinbb7923d2015-02-09 10:56:09 +08001663 kernel_size = ROUND_TO_PAGE(kernel_size, page_mask);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001664 /* Check if the addresses in the header are valid. */
Matthew Qinbb7923d2015-02-09 10:56:09 +08001665 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_size) ||
Mayank Grovere559cec2017-10-17 15:12:03 +05301666 check_ddr_addr_range_bound(hdr->kernel_addr, kernel_size) ||
1667 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual) ||
1668 check_ddr_addr_range_bound(hdr->ramdisk_addr, ramdisk_actual))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001669 {
Mayank Grovere559cec2017-10-17 15:12:03 +05301670 dprintf(CRITICAL, "kernel/ramdisk addresses are not valid.\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001671 return -1;
1672 }
1673
1674#ifndef DEVICE_TREE
Mayank Grovere559cec2017-10-17 15:12:03 +05301675 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE) ||
1676 check_ddr_addr_range_bound(hdr->tags_addr, MAX_TAGS_SIZE))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001677 {
Mayank Grovere559cec2017-10-17 15:12:03 +05301678 dprintf(CRITICAL, "Tags addresses are not valid.\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001679 return -1;
1680 }
1681#endif
1682
Matthew Qin49e51fa2015-02-09 10:40:45 +08001683 /* Move kernel, ramdisk and device tree to correct address */
Matthew Qinbb7923d2015-02-09 10:56:09 +08001684 memmove((void*) hdr->kernel_addr, kernel_start_addr, kernel_size);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001685 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001686
Matthew Qin49e51fa2015-02-09 10:40:45 +08001687 #if DEVICE_TREE
Parth Dixit4097b622016-03-15 14:42:27 +05301688 if(dt_size) {
Matthew Qin49e51fa2015-02-09 10:40:45 +08001689 dt_table_offset = ((uint32_t)image_addr + page_size + kernel_actual + ramdisk_actual + second_actual);
1690 table = (struct dt_table*) dt_table_offset;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001691
Matthew Qin49e51fa2015-02-09 10:40:45 +08001692 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
1693 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1694 return -1;
1695 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001696
P.V. Phani Kumar82916762016-03-09 09:20:19 +05301697 /* Its Error if, dt_hdr_size (table->num_entries * dt_entry size + Dev_Tree Header)
1698 goes beyound hdr->dt_size*/
Parth Dixit4097b622016-03-15 14:42:27 +05301699 if (dt_hdr_size > ROUND_TO_PAGE(dt_size,hdr->page_size)) {
P.V. Phani Kumar82916762016-03-09 09:20:19 +05301700 dprintf(CRITICAL, "ERROR: Invalid Device Tree size \n");
1701 return -1;
1702 }
1703
Matthew Qin49e51fa2015-02-09 10:40:45 +08001704 /* Find index of device tree within device tree table */
1705 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
1706 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1707 return -1;
1708 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001709
P.V. Phani Kumar3c333202016-03-09 11:54:37 +05301710 if(dt_entry.offset > (UINT_MAX - dt_entry.size)) {
1711 dprintf(CRITICAL, "ERROR: Device tree contents are Invalid\n");
1712 return -1;
1713 }
1714
1715 /* Ensure we are not overshooting dt_size with the dt_entry selected */
Parth Dixit4097b622016-03-15 14:42:27 +05301716 if ((dt_entry.offset + dt_entry.size) > dt_size) {
P.V. Phani Kumar3c333202016-03-09 11:54:37 +05301717 dprintf(CRITICAL, "ERROR: Device tree contents are Invalid\n");
1718 return -1;
1719 }
1720
Matthew Qin271927e2015-03-31 22:07:07 -04001721 if (is_gzip_package((unsigned char *)dt_table_offset + dt_entry.offset, dt_entry.size))
1722 {
1723 unsigned int compressed_size = 0;
1724 out_addr += out_len;
1725 out_avai_len -= out_len;
Matthew Qin0b15b322015-05-19 05:20:54 -04001726 dprintf(INFO, "decompressing dtb: start\n");
Matthew Qin271927e2015-03-31 22:07:07 -04001727 rc = decompress((unsigned char *)dt_table_offset + dt_entry.offset,
1728 dt_entry.size, out_addr, out_avai_len,
1729 &compressed_size, &dtb_size);
1730 if (rc)
1731 {
Matthew Qin0b15b322015-05-19 05:20:54 -04001732 dprintf(CRITICAL, "decompressing dtb failed!!!\n");
Matthew Qin271927e2015-03-31 22:07:07 -04001733 ASSERT(0);
1734 }
1735
Matthew Qin0b15b322015-05-19 05:20:54 -04001736 dprintf(INFO, "decompressing dtb: done\n");
Matthew Qin271927e2015-03-31 22:07:07 -04001737 best_match_dt_addr = out_addr;
1738 } else {
1739 best_match_dt_addr = (unsigned char *)dt_table_offset + dt_entry.offset;
1740 dtb_size = dt_entry.size;
1741 }
1742
Matthew Qin49e51fa2015-02-09 10:40:45 +08001743 /* Validate and Read device device tree in the tags_addr */
Mayank Grovere559cec2017-10-17 15:12:03 +05301744 if (check_aboot_addr_range_overlap(hdr->tags_addr, dtb_size) ||
1745 check_ddr_addr_range_bound(hdr->tags_addr, dtb_size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001746 {
Mayank Grovere559cec2017-10-17 15:12:03 +05301747 dprintf(CRITICAL, "Device tree addresses are not valid\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001748 return -1;
1749 }
Shashank Mittal162244e2011-08-08 19:01:25 -07001750
Matthew Qin271927e2015-03-31 22:07:07 -04001751 memmove((void *)hdr->tags_addr, (char *)best_match_dt_addr, dtb_size);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001752 } else {
1753 /* Validate the tags_addr */
Mayank Grovere559cec2017-10-17 15:12:03 +05301754 if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual) ||
1755 check_ddr_addr_range_bound(hdr->tags_addr, kernel_actual))
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001756 {
Mayank Grovere559cec2017-10-17 15:12:03 +05301757 dprintf(CRITICAL, "Device tree addresses are not valid.\n");
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001758 return -1;
1759 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08001760 /*
1761 * If appended dev tree is found, update the atags with
1762 * memory address to the DTB appended location on RAM.
1763 * Else update with the atags address in the kernel header
1764 */
1765 void *dtb;
Ameya Thakur10a33452016-06-13 14:24:26 -07001766 dtb = dev_tree_appended(
1767 (void*)(image_addr + page_size +
1768 patched_kernel_hdr_size),
1769 hdr->kernel_size, dtb_offset,
1770 (void *)hdr->tags_addr);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001771 if (!dtb) {
1772 dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001773 return -1;
1774 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07001775 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08001776 #endif
Shashank Mittal23b8f422010-04-16 19:27:21 -07001777
Stanimir Varbanov69ec5462013-07-18 18:17:42 +03001778 if (boot_into_recovery && !device.is_unlocked && !device.is_tampered)
1779 target_load_ssd_keystore();
1780
Shashank Mittal23b8f422010-04-16 19:27:21 -07001781unified_boot:
Shashank Mittal23b8f422010-04-16 19:27:21 -07001782
Dima Zavin77e41f32013-03-06 16:10:43 -08001783 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07001784 (const char *)hdr->cmdline, board_machtype(),
Shashank Mittal23b8f422010-04-16 19:27:21 -07001785 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
1786
1787 return 0;
1788}
1789
Dima Zavin214cc642009-01-26 11:16:21 -08001790int boot_linux_from_flash(void)
1791{
1792 struct boot_img_hdr *hdr = (void*) buf;
Dima Zavin214cc642009-01-26 11:16:21 -08001793 struct ptentry *ptn;
1794 struct ptable *ptable;
1795 unsigned offset = 0;
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001796
Shashank Mittalcd98d472011-08-02 14:29:24 -07001797 unsigned char *image_addr = 0;
1798 unsigned kernel_actual;
1799 unsigned ramdisk_actual;
1800 unsigned imagesize_actual;
vijay kumar8f53e362015-11-24 13:38:11 +05301801 unsigned second_actual = 0;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001802
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001803#if DEVICE_TREE
Monika Singh292b3e92018-03-17 22:40:23 +05301804 struct dt_table *table = NULL;
Joel Kingaa335dc2013-06-03 16:11:08 -07001805 struct dt_entry dt_entry;
vijay kumar8f53e362015-11-24 13:38:11 +05301806 unsigned dt_table_offset;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001807 uint32_t dt_actual;
Monika Singh292b3e92018-03-17 22:40:23 +05301808 uint32_t dt_hdr_size = 0;
vijay kumar8f53e362015-11-24 13:38:11 +05301809 unsigned int dtb_size = 0;
1810 unsigned char *best_match_dt_addr = NULL;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001811#endif
1812
David Ng183a7422009-12-07 14:55:21 -08001813 if (target_is_emmc_boot()) {
1814 hdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
1815 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1816 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
1817 return -1;
1818 }
1819 goto continue_boot;
1820 }
1821
Dima Zavin214cc642009-01-26 11:16:21 -08001822 ptable = flash_get_ptable();
1823 if (ptable == NULL) {
1824 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1825 return -1;
1826 }
1827
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001828 if(!boot_into_recovery)
1829 {
1830 ptn = ptable_find(ptable, "boot");
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001831
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001832 if (ptn == NULL) {
1833 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1834 return -1;
1835 }
1836 }
1837 else
1838 {
1839 ptn = ptable_find(ptable, "recovery");
1840 if (ptn == NULL) {
1841 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
1842 return -1;
1843 }
Dima Zavin214cc642009-01-26 11:16:21 -08001844 }
1845
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05301846 /* Read boot.img header from flash */
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001847 if (flash_read(ptn, offset, buf, page_size)) {
Dima Zavin214cc642009-01-26 11:16:21 -08001848 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
1849 return -1;
1850 }
Dima Zavin214cc642009-01-26 11:16:21 -08001851
1852 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001853 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Dima Zavin214cc642009-01-26 11:16:21 -08001854 return -1;
1855 }
1856
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001857 if (hdr->page_size != page_size) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001858 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 -08001859 return -1;
1860 }
1861
Kishor PK9e91b592017-05-24 12:14:55 +05301862 image_addr = (unsigned char *)target_get_scratch_address();
1863 memcpy(image_addr, (void *)buf, page_size);
vijay kumar287bb542015-09-29 13:01:52 +05301864
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001865 /*
1866 * Update the kernel/ramdisk/tags address if the boot image header
1867 * has default values, these default values come from mkbootimg when
1868 * the boot image is flashed using fastboot flash:raw
1869 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001870 update_ker_tags_rdisk_addr(hdr, false);
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001871
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001872 /* Get virtual addresses since the hdr saves physical addresses. */
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001873 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
1874 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
1875 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
1876
1877 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1878 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Mayank Grover032736f2017-07-14 20:34:51 +05301879 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001880
Kishor PK9e91b592017-05-24 12:14:55 +05301881 /* ensure commandline is terminated */
1882 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
1883
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001884 /* Check if the addresses in the header are valid. */
1885 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
Mayank Grovere559cec2017-10-17 15:12:03 +05301886 check_ddr_addr_range_bound(hdr->kernel_addr, kernel_actual) ||
1887 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual) ||
1888 check_ddr_addr_range_bound(hdr->ramdisk_addr, ramdisk_actual))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001889 {
Mayank Grovere559cec2017-10-17 15:12:03 +05301890 dprintf(CRITICAL, "kernel/ramdisk addresses are not valid.\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001891 return -1;
1892 }
1893
1894#ifndef DEVICE_TREE
Kishor PKd8ddcad2017-07-27 13:53:57 +05301895 if (UINT_MAX < ((uint64_t)kernel_actual + (uint64_t)ramdisk_actual+ (uint64_t)second_actual + page_size)) {
Mayank Grover032736f2017-07-14 20:34:51 +05301896 dprintf(CRITICAL, "Integer overflow detected in bootimage header fields\n");
1897 return -1;
1898 }
Kishor PKd8ddcad2017-07-27 13:53:57 +05301899 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + second_actual);
Mayank Grover032736f2017-07-14 20:34:51 +05301900
Mayank Grovere559cec2017-10-17 15:12:03 +05301901 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE) ||
1902 check_ddr_addr_range_bound(hdr->tags_addr, MAX_TAGS_SIZE))
Vijay Kumar Pendotid3ed20e2016-09-20 00:34:46 +05301903 {
Mayank Grovere559cec2017-10-17 15:12:03 +05301904 dprintf(CRITICAL, "Tags addresses are not valid.\n");
Vijay Kumar Pendotid3ed20e2016-09-20 00:34:46 +05301905 return -1;
1906 }
1907#else
1908
1909#ifndef OSVERSION_IN_BOOTIMAGE
1910 dt_size = hdr->dt_size;
1911#endif
Mayank Grover032736f2017-07-14 20:34:51 +05301912 dt_actual = ROUND_TO_PAGE(dt_size, page_mask);
Kishor PKd8ddcad2017-07-27 13:53:57 +05301913 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 +05301914 dprintf(CRITICAL, "Integer overflow detected in bootimage header fields\n");
1915 return -1;
1916 }
1917
Kishor PKd8ddcad2017-07-27 13:53:57 +05301918 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + second_actual + dt_actual);
Mayank Grover032736f2017-07-14 20:34:51 +05301919
Mayank Grovere559cec2017-10-17 15:12:03 +05301920 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_size) ||
1921 check_ddr_addr_range_bound(hdr->tags_addr, dt_size))
Mayank Grover032736f2017-07-14 20:34:51 +05301922 {
Mayank Grovere559cec2017-10-17 15:12:03 +05301923 dprintf(CRITICAL, "Device tree addresses are not valid.\n");
Mayank Grover032736f2017-07-14 20:34:51 +05301924 return -1;
1925 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001926#endif
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001927
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05301928 /* Read full boot.img from flash */
1929 dprintf(INFO, "Loading (%s) image (%d): start\n",
1930 (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual);
1931 bs_set_timestamp(BS_KERNEL_LOAD_START);
1932
1933 if (UINT_MAX - page_size < imagesize_actual)
1934 {
1935 dprintf(CRITICAL,"Integer overflow detected in bootimage header fields %u %s\n", __LINE__,__func__);
1936 return -1;
1937 }
1938
1939 /*Check the availability of RAM before reading boot image + max signature length from flash*/
1940 if (target_get_max_flash_size() < (imagesize_actual + page_size))
1941 {
1942 dprintf(CRITICAL, "bootimage size is greater than DDR can hold\n");
1943 return -1;
1944 }
1945
1946 offset = page_size;
1947 /* Read image without signature and header */
1948 if (flash_read(ptn, offset, (void *)(image_addr + offset), imagesize_actual - page_size))
1949 {
1950 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
1951 return -1;
1952 }
1953
1954 dprintf(INFO, "Loading (%s) image (%d): done\n",
1955 (!boot_into_recovery ? "boot" : "recovery"), imagesize_actual);
1956 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
1957
Shashank Mittalcd98d472011-08-02 14:29:24 -07001958 /* Authenticate Kernel */
Deepa Dinamani23b60d42013-06-24 18:10:52 -07001959 if(target_use_signed_kernel() && (!device.is_unlocked))
Shashank Mittalcd98d472011-08-02 14:29:24 -07001960 {
Shashank Mittalcd98d472011-08-02 14:29:24 -07001961 offset = imagesize_actual;
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05301962
Shashank Mittalcd98d472011-08-02 14:29:24 -07001963 /* Read signature */
1964 if (flash_read(ptn, offset, (void *)(image_addr + offset), page_size))
1965 {
1966 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001967 return -1;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001968 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07001969
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301970 verify_signed_bootimg((uint32_t)image_addr, imagesize_actual);
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05301971 }
1972 offset = page_size;
1973 if(hdr->second_size != 0) {
1974 if (UINT_MAX - offset < second_actual)
1975 {
1976 dprintf(CRITICAL, "ERROR: Integer overflow in boot image header %s\t%d\n",__func__,__LINE__);
1977 return -1;
vijay kumar8f53e362015-11-24 13:38:11 +05301978 }
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05301979 offset += second_actual;
1980 /* Second image loading not implemented. */
1981 ASSERT(0);
1982 }
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05301983 /* Move kernel and ramdisk to correct address */
1984 memmove((void*) hdr->kernel_addr, (char*) (image_addr + page_size), hdr->kernel_size);
1985 memmove((void*) hdr->ramdisk_addr, (char*) (image_addr + page_size + kernel_actual), hdr->ramdisk_size);
1986
1987#if DEVICE_TREE
1988 if(dt_size != 0) {
1989
1990 dt_table_offset = ((uint32_t)image_addr + page_size + kernel_actual + ramdisk_actual + second_actual);
1991
1992 table = (struct dt_table*) dt_table_offset;
1993
1994 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
1995 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1996 return -1;
1997 }
1998
1999 /* Its Error if, dt_hdr_size (table->num_entries * dt_entry size + Dev_Tree Header)
2000 goes beyound hdr->dt_size*/
2001 if (dt_hdr_size > ROUND_TO_PAGE(dt_size,hdr->page_size)) {
2002 dprintf(CRITICAL, "ERROR: Invalid Device Tree size \n");
2003 return -1;
2004 }
2005
2006 /* Find index of device tree within device tree table */
2007 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
2008 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
2009 return -1;
2010 }
2011
2012 /* Validate and Read device device tree in the "tags_add */
2013 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size) ||
2014 check_ddr_addr_range_bound(hdr->tags_addr, dt_entry.size))
2015 {
2016 dprintf(CRITICAL, "Device tree addresses are not valid.\n");
2017 return -1;
2018 }
2019
2020 if(dt_entry.offset > (UINT_MAX - dt_entry.size)) {
2021 dprintf(CRITICAL, "ERROR: Device tree contents are Invalid\n");
2022 return -1;
2023 }
2024
2025 /* Ensure we are not overshooting dt_size with the dt_entry selected */
2026 if ((dt_entry.offset + dt_entry.size) > dt_size) {
2027 dprintf(CRITICAL, "ERROR: Device tree contents are Invalid\n");
2028 return -1;
2029 }
2030
2031 best_match_dt_addr = (unsigned char *)table + dt_entry.offset;
2032 dtb_size = dt_entry.size;
2033 memmove((void *)hdr->tags_addr, (char *)best_match_dt_addr, dtb_size);
2034 }
2035#endif
2036 if(target_use_signed_kernel() && (!device.is_unlocked))
2037 {
Shashank Mittal162244e2011-08-08 19:01:25 -07002038 /* Make sure everything from scratch address is read before next step!*/
Shashank Mittala0032282011-08-26 14:50:11 -07002039 if(device.is_tampered)
Shashank Mittal162244e2011-08-08 19:01:25 -07002040 {
2041 write_device_info_flash(&device);
2042 }
Channagoud Kadabi5c86fe32012-02-16 10:58:48 +05302043#if USE_PCOM_SECBOOT
2044 set_tamper_flag(device.is_tampered);
2045#endif
Shashank Mittalcd98d472011-08-02 14:29:24 -07002046 }
David Ng183a7422009-12-07 14:55:21 -08002047continue_boot:
Dima Zavin214cc642009-01-26 11:16:21 -08002048
Dima Zavin214cc642009-01-26 11:16:21 -08002049 /* TODO: create/pass atags to kernel */
2050
Ajay Dudanie28a6072011-07-01 13:59:46 -07002051 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07002052 (const char *)hdr->cmdline, board_machtype(),
Dima Zavin214cc642009-01-26 11:16:21 -08002053 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
2054
2055 return 0;
2056}
Brian Swetland9c4c0752009-01-25 16:23:50 -08002057
Shashank Mittal162244e2011-08-08 19:01:25 -07002058void write_device_info_mmc(device_info *dev)
2059{
Shashank Mittal162244e2011-08-08 19:01:25 -07002060 unsigned long long ptn = 0;
2061 unsigned long long size;
2062 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002063 uint8_t lun = 0;
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002064 uint32_t ret = 0;
Mayank Groverddd348d2018-01-23 14:07:09 +05302065 uint32_t device_info_sz = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07002066
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002067 if (devinfo_present)
2068 index = partition_get_index("devinfo");
2069 else
2070 index = partition_get_index("aboot");
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07002071
Shashank Mittal162244e2011-08-08 19:01:25 -07002072 ptn = partition_get_offset(index);
2073 if(ptn == 0)
2074 {
2075 return;
2076 }
2077
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002078 lun = partition_get_lun(index);
2079 mmc_set_lun(lun);
2080
Shashank Mittal162244e2011-08-08 19:01:25 -07002081 size = partition_get_size(index);
2082
Mayank Groverddd348d2018-01-23 14:07:09 +05302083 device_info_sz = ROUND_TO_PAGE(sizeof(struct device_info),
2084 mmc_blocksize_mask);
2085 if (device_info_sz == UINT_MAX)
2086 {
2087 dprintf(CRITICAL, "ERROR: Incorrect blocksize of card\n");
2088 return;
2089 }
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07002090
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002091 if (devinfo_present)
Mayank Groverddd348d2018-01-23 14:07:09 +05302092 ret = mmc_write(ptn, device_info_sz, (void *)info_buf);
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002093 else
Mayank Groverddd348d2018-01-23 14:07:09 +05302094 ret = mmc_write((ptn + size - device_info_sz), device_info_sz, (void *)info_buf);
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002095 if (ret)
Shashank Mittal162244e2011-08-08 19:01:25 -07002096 {
2097 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
Sridhar Parasurame1a97922015-07-27 15:31:22 -07002098 ASSERT(0);
Shashank Mittal162244e2011-08-08 19:01:25 -07002099 }
2100}
2101
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002102void read_device_info_mmc(struct device_info *info)
Shashank Mittal162244e2011-08-08 19:01:25 -07002103{
Shashank Mittal162244e2011-08-08 19:01:25 -07002104 unsigned long long ptn = 0;
2105 unsigned long long size;
2106 int index = INVALID_PTN;
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002107 uint32_t ret = 0;
Mayank Groverddd348d2018-01-23 14:07:09 +05302108 uint32_t device_info_sz = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07002109
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002110 if ((index = partition_get_index("devinfo")) < 0)
2111 {
2112 devinfo_present = false;
2113 index = partition_get_index("aboot");
2114 }
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07002115
Shashank Mittal162244e2011-08-08 19:01:25 -07002116 ptn = partition_get_offset(index);
2117 if(ptn == 0)
2118 {
2119 return;
2120 }
2121
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07002122 mmc_set_lun(partition_get_lun(index));
2123
Shashank Mittal162244e2011-08-08 19:01:25 -07002124 size = partition_get_size(index);
2125
Mayank Groverddd348d2018-01-23 14:07:09 +05302126 device_info_sz = ROUND_TO_PAGE(sizeof(struct device_info),
2127 mmc_blocksize_mask);
2128 if (device_info_sz == UINT_MAX)
2129 {
2130 dprintf(CRITICAL, "ERROR: Incorrect blocksize of card\n");
2131 return;
2132 }
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07002133
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002134 if (devinfo_present)
Mayank Groverddd348d2018-01-23 14:07:09 +05302135 ret = mmc_read(ptn, (void *)info_buf, device_info_sz);
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002136 else
Mayank Groverddd348d2018-01-23 14:07:09 +05302137 ret = mmc_read((ptn + size - device_info_sz), (void *)info_buf, device_info_sz);
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002138 if (ret)
Shashank Mittal162244e2011-08-08 19:01:25 -07002139 {
2140 dprintf(CRITICAL, "ERROR: Cannot read device info\n");
Sridhar Parasurame1a97922015-07-27 15:31:22 -07002141 ASSERT(0);
Shashank Mittal162244e2011-08-08 19:01:25 -07002142 }
Shashank Mittal162244e2011-08-08 19:01:25 -07002143}
2144
2145void write_device_info_flash(device_info *dev)
2146{
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002147 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
Shashank Mittal162244e2011-08-08 19:01:25 -07002148 struct ptentry *ptn;
2149 struct ptable *ptable;
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002150 if(info == NULL)
2151 {
2152 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
2153 ASSERT(0);
2154 }
2155 info_buf = info;
Shashank Mittal162244e2011-08-08 19:01:25 -07002156 ptable = flash_get_ptable();
2157 if (ptable == NULL)
2158 {
2159 dprintf(CRITICAL, "ERROR: Partition table not found\n");
2160 return;
2161 }
2162
2163 ptn = ptable_find(ptable, "devinfo");
2164 if (ptn == NULL)
2165 {
Smita Ghosh670c8b82015-05-07 09:30:03 -07002166 dprintf(CRITICAL, "ERROR: No devinfo partition found\n");
Shashank Mittal162244e2011-08-08 19:01:25 -07002167 return;
2168 }
2169
Mayank Groverdedbc892017-10-24 13:41:34 +05302170 memset(info, 0, BOOT_IMG_MAX_PAGE_SIZE);
Shashank Mittal162244e2011-08-08 19:01:25 -07002171 memcpy(info, dev, sizeof(device_info));
2172
2173 if (flash_write(ptn, 0, (void *)info_buf, page_size))
2174 {
2175 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
2176 return;
2177 }
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002178 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07002179}
2180
vijay kumarc65876c2015-04-24 13:29:16 +05302181static int read_allow_oem_unlock(device_info *dev)
2182{
vijay kumarc65876c2015-04-24 13:29:16 +05302183 unsigned offset;
2184 int index;
2185 unsigned long long ptn;
2186 unsigned long long ptn_size;
2187 unsigned blocksize = mmc_get_device_blocksize();
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07002188 STACKBUF_DMA_ALIGN(buf, blocksize);
vijay kumarc65876c2015-04-24 13:29:16 +05302189
vijay kumarca2e6812015-07-08 20:28:25 +05302190 index = partition_get_index(frp_ptns[0]);
vijay kumarc65876c2015-04-24 13:29:16 +05302191 if (index == INVALID_PTN)
2192 {
vijay kumarca2e6812015-07-08 20:28:25 +05302193 index = partition_get_index(frp_ptns[1]);
2194 if (index == INVALID_PTN)
2195 {
2196 dprintf(CRITICAL, "Neither '%s' nor '%s' partition found\n", frp_ptns[0],frp_ptns[1]);
2197 return -1;
2198 }
vijay kumarc65876c2015-04-24 13:29:16 +05302199 }
2200
2201 ptn = partition_get_offset(index);
2202 ptn_size = partition_get_size(index);
2203 offset = ptn_size - blocksize;
2204
Mayank Grover48860402016-11-29 12:34:53 +05302205 /* Set Lun for partition */
2206 mmc_set_lun(partition_get_lun(index));
2207
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07002208 if (mmc_read(ptn + offset, (void *)buf, blocksize))
vijay kumarc65876c2015-04-24 13:29:16 +05302209 {
2210 dprintf(CRITICAL, "Reading MMC failed\n");
2211 return -1;
2212 }
2213
2214 /*is_allow_unlock is a bool value stored at the LSB of last byte*/
2215 is_allow_unlock = buf[blocksize-1] & 0x01;
2216 return 0;
2217}
2218
2219static int write_allow_oem_unlock(bool allow_unlock)
2220{
vijay kumarc65876c2015-04-24 13:29:16 +05302221 unsigned offset;
vijay kumarc65876c2015-04-24 13:29:16 +05302222 int index;
2223 unsigned long long ptn;
2224 unsigned long long ptn_size;
2225 unsigned blocksize = mmc_get_device_blocksize();
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07002226 STACKBUF_DMA_ALIGN(buf, blocksize);
vijay kumarc65876c2015-04-24 13:29:16 +05302227
vijay kumarca2e6812015-07-08 20:28:25 +05302228 index = partition_get_index(frp_ptns[0]);
vijay kumarc65876c2015-04-24 13:29:16 +05302229 if (index == INVALID_PTN)
2230 {
vijay kumarca2e6812015-07-08 20:28:25 +05302231 index = partition_get_index(frp_ptns[1]);
2232 if (index == INVALID_PTN)
2233 {
2234 dprintf(CRITICAL, "Neither '%s' nor '%s' partition found\n", frp_ptns[0],frp_ptns[1]);
2235 return -1;
2236 }
vijay kumarc65876c2015-04-24 13:29:16 +05302237 }
2238
2239 ptn = partition_get_offset(index);
2240 ptn_size = partition_get_size(index);
2241 offset = ptn_size - blocksize;
Mayank Grover48860402016-11-29 12:34:53 +05302242 mmc_set_lun(partition_get_lun(index));
vijay kumarc65876c2015-04-24 13:29:16 +05302243
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07002244 if (mmc_read(ptn + offset, (void *)buf, blocksize))
vijay kumarc65876c2015-04-24 13:29:16 +05302245 {
2246 dprintf(CRITICAL, "Reading MMC failed\n");
2247 return -1;
2248 }
2249
2250 /*is_allow_unlock is a bool value stored at the LSB of last byte*/
2251 buf[blocksize-1] = allow_unlock;
2252 if (mmc_write(ptn + offset, blocksize, buf))
2253 {
2254 dprintf(CRITICAL, "Writing MMC failed\n");
2255 return -1;
2256 }
2257
2258 return 0;
2259}
2260
Shashank Mittal162244e2011-08-08 19:01:25 -07002261void read_device_info_flash(device_info *dev)
2262{
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002263 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
Shashank Mittal162244e2011-08-08 19:01:25 -07002264 struct ptentry *ptn;
2265 struct ptable *ptable;
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002266 if(info == NULL)
2267 {
2268 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
2269 ASSERT(0);
2270 }
2271 info_buf = info;
Shashank Mittal162244e2011-08-08 19:01:25 -07002272 ptable = flash_get_ptable();
2273 if (ptable == NULL)
2274 {
2275 dprintf(CRITICAL, "ERROR: Partition table not found\n");
2276 return;
2277 }
2278
2279 ptn = ptable_find(ptable, "devinfo");
2280 if (ptn == NULL)
2281 {
Smita Ghosh670c8b82015-05-07 09:30:03 -07002282 dprintf(CRITICAL, "ERROR: No devinfo partition found\n");
Shashank Mittal162244e2011-08-08 19:01:25 -07002283 return;
2284 }
2285
2286 if (flash_read(ptn, 0, (void *)info_buf, page_size))
2287 {
2288 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
2289 return;
2290 }
2291
2292 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
2293 {
Shashank Mittal162244e2011-08-08 19:01:25 -07002294 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
2295 info->is_unlocked = 0;
Shashank Mittala0032282011-08-26 14:50:11 -07002296 info->is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07002297 write_device_info_flash(info);
2298 }
2299 memcpy(dev, info, sizeof(device_info));
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002300 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07002301}
2302
2303void write_device_info(device_info *dev)
2304{
2305 if(target_is_emmc_boot())
2306 {
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002307 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
2308 if(info == NULL)
2309 {
2310 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
2311 ASSERT(0);
2312 }
2313 info_buf = info;
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002314 memcpy(info, dev, sizeof(struct device_info));
2315
2316#if USE_RPMB_FOR_DEVINFO
Mayank Grover912eaa62017-10-26 12:08:53 +05302317 if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05302318 is_secure_boot_enable()) {
2319 if((write_device_info_rpmb((void*) info, PAGE_SIZE)) < 0)
2320 ASSERT(0);
Sridhar Parasurame1a97922015-07-27 15:31:22 -07002321 }
Channagoud Kadabic80cb492015-04-28 16:08:28 -07002322 else
2323 write_device_info_mmc(info);
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002324#else
2325 write_device_info_mmc(info);
2326#endif
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002327 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07002328 }
2329 else
2330 {
2331 write_device_info_flash(dev);
2332 }
2333}
2334
2335void read_device_info(device_info *dev)
2336{
2337 if(target_is_emmc_boot())
2338 {
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002339 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
2340 if(info == NULL)
2341 {
2342 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
2343 ASSERT(0);
2344 }
2345 info_buf = info;
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002346
2347#if USE_RPMB_FOR_DEVINFO
Mayank Grover912eaa62017-10-26 12:08:53 +05302348 if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05302349 is_secure_boot_enable()) {
2350 if((read_device_info_rpmb((void*) info, PAGE_SIZE)) < 0)
2351 ASSERT(0);
Sridhar Parasurame1a97922015-07-27 15:31:22 -07002352 }
Channagoud Kadabic80cb492015-04-28 16:08:28 -07002353 else
2354 read_device_info_mmc(info);
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002355#else
2356 read_device_info_mmc(info);
2357#endif
2358
2359 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
2360 {
2361 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
lijuang511a2b52015-08-14 20:50:51 +08002362 if (is_secure_boot_enable()) {
Channagoud Kadabi05f78ba2015-07-06 11:58:14 -07002363 info->is_unlocked = 0;
Monika Singh292b3e92018-03-17 22:40:23 +05302364#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover912eaa62017-10-26 12:08:53 +05302365 if (VB_M <= target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +05302366 info->is_unlock_critical = 0;
Parth Dixitddbc7352015-10-18 03:13:31 +05302367#endif
lijuang511a2b52015-08-14 20:50:51 +08002368 } else {
Channagoud Kadabi2fda4092015-07-07 13:34:11 -07002369 info->is_unlocked = 1;
Monika Singh292b3e92018-03-17 22:40:23 +05302370#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover912eaa62017-10-26 12:08:53 +05302371 if (VB_M <= target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +05302372 info->is_unlock_critical = 1;
Parth Dixitddbc7352015-10-18 03:13:31 +05302373#endif
lijuang511a2b52015-08-14 20:50:51 +08002374 }
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002375 info->is_tampered = 0;
2376 info->charger_screen_enabled = 0;
Monika Singh292b3e92018-03-17 22:40:23 +05302377#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover912eaa62017-10-26 12:08:53 +05302378 if (VB_M <= target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +05302379 info->verity_mode = 1; //enforcing by default
Parth Dixitddbc7352015-10-18 03:13:31 +05302380#endif
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002381 write_device_info(info);
2382 }
2383 memcpy(dev, info, sizeof(device_info));
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002384 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07002385 }
2386 else
2387 {
2388 read_device_info_flash(dev);
2389 }
2390}
2391
2392void reset_device_info()
2393{
2394 dprintf(ALWAYS, "reset_device_info called.");
Shashank Mittala0032282011-08-26 14:50:11 -07002395 device.is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07002396 write_device_info(&device);
2397}
2398
2399void set_device_root()
2400{
2401 dprintf(ALWAYS, "set_device_root called.");
Shashank Mittala0032282011-08-26 14:50:11 -07002402 device.is_tampered = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -07002403 write_device_info(&device);
2404}
2405
lijuang4ece1e72015-08-14 21:02:36 +08002406/* set device unlock value
2407 * Must check FRP before call this function
2408 * Need to wipe data when unlock status changed
2409 * type 0: oem unlock
2410 * type 1: unlock critical
2411 * status 0: unlock as false
2412 * status 1: lock as true
2413 */
2414void set_device_unlock_value(int type, bool status)
lijuang21f12f52015-08-22 16:22:19 +08002415{
lijuang4ece1e72015-08-14 21:02:36 +08002416 if (type == UNLOCK)
2417 device.is_unlocked = status;
Monika Singh292b3e92018-03-17 22:40:23 +05302418#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover912eaa62017-10-26 12:08:53 +05302419 else if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05302420 type == UNLOCK_CRITICAL)
2421 device.is_unlock_critical = status;
Parth Dixitddbc7352015-10-18 03:13:31 +05302422#endif
lijuang4ece1e72015-08-14 21:02:36 +08002423 write_device_info(&device);
2424}
2425
2426static void set_device_unlock(int type, bool status)
2427{
2428 int is_unlocked = -1;
2429 char response[MAX_RSP_SIZE];
2430
2431 /* check device unlock status if it is as expected */
2432 if (type == UNLOCK)
2433 is_unlocked = device.is_unlocked;
Monika Singh292b3e92018-03-17 22:40:23 +05302434#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover912eaa62017-10-26 12:08:53 +05302435 if(VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05302436 type == UNLOCK_CRITICAL)
2437 {
2438 is_unlocked = device.is_unlock_critical;
2439 }
Parth Dixitddbc7352015-10-18 03:13:31 +05302440#endif
lijuang4ece1e72015-08-14 21:02:36 +08002441 if (is_unlocked == status) {
2442 snprintf(response, sizeof(response), "\tDevice already : %s", (status ? "unlocked!" : "locked!"));
2443 fastboot_info(response);
2444 fastboot_okay("");
2445 return;
2446 }
2447
2448 /* status is true, it means to unlock device */
2449 if (status) {
lijuang21f12f52015-08-22 16:22:19 +08002450 if(!is_allow_unlock) {
2451 fastboot_fail("oem unlock is not allowed");
2452 return;
2453 }
2454
lijuang4ece1e72015-08-14 21:02:36 +08002455#if FBCON_DISPLAY_MSG
lijuang9a7d3b92015-11-30 14:41:24 +08002456 display_unlock_menu(type);
lijuang4ece1e72015-08-14 21:02:36 +08002457 fastboot_okay("");
2458 return;
2459#else
2460 if (type == UNLOCK) {
2461 fastboot_fail("Need wipe userdata. Do 'fastboot oem unlock-go'");
2462 return;
2463 }
2464#endif
lijuang21f12f52015-08-22 16:22:19 +08002465 }
lijuang4ece1e72015-08-14 21:02:36 +08002466
2467 set_device_unlock_value(type, status);
2468
2469 /* wipe data */
2470 struct recovery_message msg;
Kishor PK60a68212017-05-08 16:55:57 +05302471 memset(&msg, 0, sizeof(msg));
lijuang4ece1e72015-08-14 21:02:36 +08002472 snprintf(msg.recovery, sizeof(msg.recovery), "recovery\n--wipe_data");
2473 write_misc(0, &msg, sizeof(msg));
2474
2475 fastboot_okay("");
2476 reboot_device(RECOVERY_MODE);
lijuang21f12f52015-08-22 16:22:19 +08002477}
2478
lijuang511a2b52015-08-14 20:50:51 +08002479static bool critical_flash_allowed(const char * entry)
2480{
2481 uint32_t i = 0;
2482 if (entry == NULL)
2483 return false;
2484
2485 for (i = 0; i < ARRAY_SIZE(critical_flash_allowed_ptn); i++) {
2486 if(!strcmp(entry, critical_flash_allowed_ptn[i]))
2487 return true;
2488 }
2489 return false;
Matthew Qin271927e2015-03-31 22:07:07 -04002490}
2491
2492#if DEVICE_TREE
Amol Jadicb524072012-08-09 16:40:18 -07002493int copy_dtb(uint8_t *boot_image_start, unsigned int scratch_offset)
2494{
2495 uint32 dt_image_offset = 0;
Amol Jadicb524072012-08-09 16:40:18 -07002496 uint32_t n;
Monika Singh292b3e92018-03-17 22:40:23 +05302497 struct dt_table *table = NULL;
Amol Jadicb524072012-08-09 16:40:18 -07002498 struct dt_entry dt_entry;
Monika Singh292b3e92018-03-17 22:40:23 +05302499 uint32_t dt_hdr_size = 0;
Amol Jadicb524072012-08-09 16:40:18 -07002500 unsigned int compressed_size = 0;
2501 unsigned int dtb_size = 0;
2502 unsigned int out_avai_len = 0;
2503 unsigned char *out_addr = NULL;
2504 unsigned char *best_match_dt_addr = NULL;
2505 int rc;
2506
2507 struct boot_img_hdr *hdr = (struct boot_img_hdr *) (boot_image_start);
2508
Parth Dixit4097b622016-03-15 14:42:27 +05302509#ifndef OSVERSION_IN_BOOTIMAGE
2510 dt_size = hdr->dt_size;
2511#endif
2512
2513 if(dt_size != 0) {
Amol Jadicb524072012-08-09 16:40:18 -07002514 /* add kernel offset */
2515 dt_image_offset += page_size;
2516 n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
2517 dt_image_offset += n;
2518
2519 /* add ramdisk offset */
2520 n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
2521 dt_image_offset += n;
2522
2523 /* add second offset */
2524 if(hdr->second_size != 0) {
2525 n = ROUND_TO_PAGE(hdr->second_size, page_mask);
2526 dt_image_offset += n;
2527 }
2528
2529 /* offset now point to start of dt.img */
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07002530 table = (struct dt_table*)(boot_image_start + dt_image_offset);
Amol Jadicb524072012-08-09 16:40:18 -07002531
Deepa Dinamani19648b42013-09-05 17:05:55 -07002532 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Amol Jadicb524072012-08-09 16:40:18 -07002533 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
2534 return -1;
2535 }
P.V. Phani Kumar82916762016-03-09 09:20:19 +05302536
2537 /* Its Error if, dt_hdr_size (table->num_entries * dt_entry size + Dev_Tree Header)
2538 goes beyound hdr->dt_size*/
Parth Dixit4097b622016-03-15 14:42:27 +05302539 if (dt_hdr_size > ROUND_TO_PAGE(dt_size,hdr->page_size)) {
P.V. Phani Kumar82916762016-03-09 09:20:19 +05302540 dprintf(CRITICAL, "ERROR: Invalid Device Tree size \n");
2541 return -1;
2542 }
2543
Joel Kingaa335dc2013-06-03 16:11:08 -07002544 /* Find index of device tree within device tree table */
2545 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Amol Jadicb524072012-08-09 16:40:18 -07002546 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
2547 return -1;
2548 }
2549
Matthew Qin271927e2015-03-31 22:07:07 -04002550 best_match_dt_addr = (unsigned char *)boot_image_start + dt_image_offset + dt_entry.offset;
2551 if (is_gzip_package(best_match_dt_addr, dt_entry.size))
2552 {
2553 out_addr = (unsigned char *)target_get_scratch_address() + scratch_offset;
2554 out_avai_len = target_get_max_flash_size() - scratch_offset;
Matthew Qin0b15b322015-05-19 05:20:54 -04002555 dprintf(INFO, "decompressing dtb: start\n");
Matthew Qin271927e2015-03-31 22:07:07 -04002556 rc = decompress(best_match_dt_addr,
2557 dt_entry.size, out_addr, out_avai_len,
2558 &compressed_size, &dtb_size);
2559 if (rc)
2560 {
Matthew Qin0b15b322015-05-19 05:20:54 -04002561 dprintf(CRITICAL, "decompressing dtb failed!!!\n");
Matthew Qin271927e2015-03-31 22:07:07 -04002562 ASSERT(0);
2563 }
2564
Matthew Qin0b15b322015-05-19 05:20:54 -04002565 dprintf(INFO, "decompressing dtb: done\n");
Matthew Qin271927e2015-03-31 22:07:07 -04002566 best_match_dt_addr = out_addr;
2567 } else {
2568 dtb_size = dt_entry.size;
2569 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002570 /* Validate and Read device device tree in the "tags_add */
Mayank Grovere559cec2017-10-17 15:12:03 +05302571 if (check_aboot_addr_range_overlap(hdr->tags_addr, dtb_size) ||
2572 check_ddr_addr_range_bound(hdr->tags_addr, dtb_size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002573 {
Mayank Grovere559cec2017-10-17 15:12:03 +05302574 dprintf(CRITICAL, "Device tree addresses are not valid.\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002575 return -1;
2576 }
2577
Amol Jadicb524072012-08-09 16:40:18 -07002578 /* Read device device tree in the "tags_add */
Matthew Qin271927e2015-03-31 22:07:07 -04002579 memmove((void*) hdr->tags_addr, (void *)best_match_dt_addr, dtb_size);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002580 } else
2581 return -1;
Amol Jadicb524072012-08-09 16:40:18 -07002582
2583 /* Everything looks fine. Return success. */
2584 return 0;
2585}
2586#endif
2587
Brian Swetland9c4c0752009-01-25 16:23:50 -08002588void cmd_boot(const char *arg, void *data, unsigned sz)
2589{
2590 unsigned kernel_actual;
2591 unsigned ramdisk_actual;
Kishor PKd8ddcad2017-07-27 13:53:57 +05302592 unsigned second_actual;
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002593 uint32_t image_actual;
2594 uint32_t dt_actual = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08002595 struct boot_img_hdr *hdr = NULL;
2596 struct kernel64_hdr *kptr = NULL;
Brian Swetland9c4c0752009-01-25 16:23:50 -08002597 char *ptr = ((char*) data);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002598 int ret = 0;
2599 uint8_t dtb_copied = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08002600 unsigned int out_len = 0;
2601 unsigned int out_avai_len = 0;
2602 unsigned char *out_addr = NULL;
2603 uint32_t dtb_offset = 0;
2604 unsigned char *kernel_start_addr = NULL;
2605 unsigned int kernel_size = 0;
Matthew Qin271927e2015-03-31 22:07:07 -04002606 unsigned int scratch_offset = 0;
Monika Singh292b3e92018-03-17 22:40:23 +05302607#if !VERIFIED_BOOT_2
2608 uint32_t sig_actual = 0;
2609 uint32_t sig_size = 0;
2610#ifdef MDTP_SUPPORT
2611 static bool is_mdtp_activated = 0;
2612#endif /* MDTP_SUPPORT */
2613#endif
Matthew Qinbb7923d2015-02-09 10:56:09 +08002614
lijuang2008ff22016-03-07 17:56:27 +08002615#if FBCON_DISPLAY_MSG
2616 /* Exit keys' detection thread firstly */
2617 exit_menu_keys_detection();
2618#endif
2619
Monika Singh292b3e92018-03-17 22:40:23 +05302620#if VERIFIED_BOOT || VERIFIED_BOOT_2
Channagoud Kadabi6d5375e2015-06-23 17:15:42 -07002621 if(target_build_variant_user() && !device.is_unlocked)
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002622 {
2623 fastboot_fail("unlock device to use this command");
lijuang2008ff22016-03-07 17:56:27 +08002624 goto boot_failed;
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002625 }
2626#endif
2627
Brian Swetland9c4c0752009-01-25 16:23:50 -08002628 if (sz < sizeof(hdr)) {
2629 fastboot_fail("invalid bootimage header");
lijuang2008ff22016-03-07 17:56:27 +08002630 goto boot_failed;
Brian Swetland9c4c0752009-01-25 16:23:50 -08002631 }
2632
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002633 hdr = (struct boot_img_hdr *)data;
Brian Swetland9c4c0752009-01-25 16:23:50 -08002634
2635 /* ensure commandline is terminated */
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002636 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -08002637
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002638 if(target_is_emmc_boot() && hdr->page_size) {
2639 page_size = hdr->page_size;
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07002640 page_mask = page_size - 1;
2641 }
2642
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002643 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
2644 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Kishor PKd8ddcad2017-07-27 13:53:57 +05302645 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002646#if DEVICE_TREE
Parth Dixit4097b622016-03-15 14:42:27 +05302647#ifndef OSVERSION_IN_BOOTIMAGE
2648 dt_size = hdr->dt_size;
2649#endif
2650 dt_actual = ROUND_TO_PAGE(dt_size, page_mask);
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002651#endif
2652
2653 image_actual = ADD_OF(page_size, kernel_actual);
2654 image_actual = ADD_OF(image_actual, ramdisk_actual);
Kishor PKd8ddcad2017-07-27 13:53:57 +05302655 image_actual = ADD_OF(image_actual, second_actual);
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002656 image_actual = ADD_OF(image_actual, dt_actual);
2657
Kishor PK5134b332017-05-09 17:50:08 +05302658 /* Checking to prevent oob access in read_der_message_length */
2659 if (image_actual > sz) {
2660 fastboot_fail("bootimage header fields are invalid");
2661 goto boot_failed;
2662 }
Monika Singh292b3e92018-03-17 22:40:23 +05302663#if VERIFIED_BOOT_2
2664 memset(&info, 0, sizeof(bootinfo));
2665 info.images[0].image_buffer = data;
2666 info.images[0].imgsize = image_actual;
2667 info.images[0].name = "boot";
2668 info.num_loaded_images = 1;
2669 info.multi_slot_boot = partition_multislot_is_supported();
2670 if (load_image_and_auth(&info))
2671 goto boot_failed;
2672 vbcmdline = info.vbcmdline;
2673#else
Kishor PK5134b332017-05-09 17:50:08 +05302674 sig_size = sz - image_actual;
2675
P.V. Phani Kumareecfd822016-03-09 20:09:03 +05302676 if (target_use_signed_kernel() && (!device.is_unlocked)) {
Monika Singh292b3e92018-03-17 22:40:23 +05302677 unsigned chk;
P.V. Phani Kumareecfd822016-03-09 20:09:03 +05302678 /* Calculate the signature length from boot image */
2679 sig_actual = read_der_message_length(
Kishor PK5134b332017-05-09 17:50:08 +05302680 (unsigned char*)(data + image_actual), sig_size);
Monika Singh292b3e92018-03-17 22:40:23 +05302681 chk = ADD_OF(image_actual, sig_actual);
Channagoud Kadabi3d839012014-06-26 14:26:39 -07002682
Monika Singh292b3e92018-03-17 22:40:23 +05302683 if (chk > sz) {
Kishor PK5134b332017-05-09 17:50:08 +05302684 fastboot_fail("bootimage header fields are invalid");
2685 goto boot_failed;
2686 }
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002687 }
2688
Sridhar Parasuram361e4cd2015-09-24 11:19:19 -07002689 // Initialize boot state before trying to verify boot.img
2690#if VERIFIED_BOOT
Channagoud Kadabi699466e2015-11-03 12:37:42 -08002691 boot_verifier_init();
Mayank Groverb337e932017-01-18 20:00:40 +05302692#endif
Channagoud Kadabibf6ce7d2015-09-17 13:25:35 -07002693 /* Handle overflow if the input image size is greater than
2694 * boot image buffer can hold
2695 */
Monika Singh292b3e92018-03-17 22:40:23 +05302696 if ((target_get_max_flash_size() - page_size) < image_actual)
Channagoud Kadabibf6ce7d2015-09-17 13:25:35 -07002697 {
2698 fastboot_fail("booimage: size is greater than boot image buffer can hold");
lijuang2008ff22016-03-07 17:56:27 +08002699 goto boot_failed;
Channagoud Kadabibf6ce7d2015-09-17 13:25:35 -07002700 }
Sridhar Parasuram361e4cd2015-09-24 11:19:19 -07002701
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002702 /* Verify the boot image
2703 * device & page_size are initialized in aboot_init
2704 */
Sridhar Parasuram7ae9fbc2015-09-17 09:56:30 -07002705 if (target_use_signed_kernel() && (!device.is_unlocked)) {
Channagoud Kadabi3d839012014-06-26 14:26:39 -07002706 /* Pass size excluding signature size, otherwise we would try to
2707 * access signature beyond its length
2708 */
Monika Singh292b3e92018-03-17 22:40:23 +05302709 verify_signed_bootimg((uint32_t)data, image_actual);
Sridhar Parasuram7ae9fbc2015-09-17 09:56:30 -07002710 }
Amit Blay4aa292f2015-04-28 21:55:59 +03002711#ifdef MDTP_SUPPORT
2712 else
2713 {
Shay Nachmani062aa3f2015-05-17 17:28:44 +03002714 /* fastboot boot is not allowed when MDTP is activated */
Amit Blay4aa292f2015-04-28 21:55:59 +03002715 mdtp_ext_partition_verification_t ext_partition;
Amit Blay8a510302015-08-17 09:20:01 +03002716
2717 if (!is_mdtp_activated) {
2718 ext_partition.partition = MDTP_PARTITION_NONE;
2719 mdtp_fwlock_verify_lock(&ext_partition);
2720 }
Amit Blay4aa292f2015-04-28 21:55:59 +03002721 }
Shay Nachmani062aa3f2015-05-17 17:28:44 +03002722
Amir Kotzer7c768c02016-04-13 09:08:05 +03002723 /* If mdtp state cannot be validate, block fastboot boot*/
2724 if(mdtp_activated(&is_mdtp_activated)){
2725 dprintf(CRITICAL, "mdtp_activated cannot validate state.\n");
2726 dprintf(CRITICAL, "Can not proceed with fastboot boot command.\n");
2727 goto boot_failed;
2728 }
Shay Nachmani062aa3f2015-05-17 17:28:44 +03002729 if(is_mdtp_activated){
2730 dprintf(CRITICAL, "fastboot boot command is not available.\n");
lijuang2008ff22016-03-07 17:56:27 +08002731 goto boot_failed;
Shay Nachmani062aa3f2015-05-17 17:28:44 +03002732 }
Amit Blay4aa292f2015-04-28 21:55:59 +03002733#endif /* MDTP_SUPPORT */
Monika Singh292b3e92018-03-17 22:40:23 +05302734#endif /* VERIFIED_BOOT_2 else */
Amit Blay4aa292f2015-04-28 21:55:59 +03002735
Sridhar Parasuram361e4cd2015-09-24 11:19:19 -07002736#if VERIFIED_BOOT
Monika Singh292b3e92018-03-17 22:40:23 +05302737 if (VB_M == target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +05302738 {
2739 /* set boot and system versions. */
2740 set_os_version((unsigned char *)data);
2741 // send root of trust
2742 if(!send_rot_command((uint32_t)device.is_unlocked))
2743 ASSERT(0);
2744 }
Parth Dixitbc9b6492015-10-18 00:41:38 +05302745#endif
Channagoud Kadabia22144f2013-03-20 11:49:01 -07002746 /*
Matthew Qinbb7923d2015-02-09 10:56:09 +08002747 * Check if the kernel image is a gzip package. If yes, need to decompress it.
2748 * If not, continue booting.
2749 */
2750 if (is_gzip_package((unsigned char *)(data + page_size), hdr->kernel_size))
2751 {
2752 out_addr = (unsigned char *)target_get_scratch_address();
2753 out_addr = (unsigned char *)(out_addr + image_actual + page_size);
2754 out_avai_len = target_get_max_flash_size() - image_actual - page_size;
Matthew Qin0b15b322015-05-19 05:20:54 -04002755 dprintf(INFO, "decompressing kernel image: start\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08002756 ret = decompress((unsigned char *)(ptr + page_size),
2757 hdr->kernel_size, out_addr, out_avai_len,
2758 &dtb_offset, &out_len);
2759 if (ret)
2760 {
Matthew Qin0b15b322015-05-19 05:20:54 -04002761 dprintf(CRITICAL, "decompressing image failed!!!\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08002762 ASSERT(0);
2763 }
2764
Matthew Qin0b15b322015-05-19 05:20:54 -04002765 dprintf(INFO, "decompressing kernel image: done\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08002766 kptr = (struct kernel64_hdr *)out_addr;
2767 kernel_start_addr = out_addr;
2768 kernel_size = out_len;
2769 } else {
2770 kptr = (struct kernel64_hdr*)((char *)data + page_size);
2771 kernel_start_addr = (unsigned char *)((char *)data + page_size);
2772 kernel_size = hdr->kernel_size;
2773 }
2774
2775 /*
Channagoud Kadabia22144f2013-03-20 11:49:01 -07002776 * Update the kernel/ramdisk/tags address if the boot image header
2777 * has default values, these default values come from mkbootimg when
2778 * the boot image is flashed using fastboot flash:raw
2779 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08002780 update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr));
Dima Zavin3cadfff2013-03-21 14:30:48 -07002781
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002782 /* Get virtual addresses since the hdr saves physical addresses. */
2783 hdr->kernel_addr = VA(hdr->kernel_addr);
2784 hdr->ramdisk_addr = VA(hdr->ramdisk_addr);
2785 hdr->tags_addr = VA(hdr->tags_addr);
Brian Swetland9c4c0752009-01-25 16:23:50 -08002786
Matthew Qinbb7923d2015-02-09 10:56:09 +08002787 kernel_size = ROUND_TO_PAGE(kernel_size, page_mask);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002788 /* Check if the addresses in the header are valid. */
Matthew Qinbb7923d2015-02-09 10:56:09 +08002789 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_size) ||
Mayank Grovere559cec2017-10-17 15:12:03 +05302790 check_ddr_addr_range_bound(hdr->kernel_addr, kernel_size) ||
2791 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual) ||
2792 check_ddr_addr_range_bound(hdr->ramdisk_addr, ramdisk_actual))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002793 {
Mayank Grovere559cec2017-10-17 15:12:03 +05302794 dprintf(CRITICAL, "kernel/ramdisk addresses are not valid.\n");
lijuang2008ff22016-03-07 17:56:27 +08002795 goto boot_failed;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002796 }
2797
Amol Jadicb524072012-08-09 16:40:18 -07002798#if DEVICE_TREE
Matthew Qin271927e2015-03-31 22:07:07 -04002799 scratch_offset = image_actual + page_size + out_len;
Amol Jadicb524072012-08-09 16:40:18 -07002800 /* find correct dtb and copy it to right location */
Matthew Qin271927e2015-03-31 22:07:07 -04002801 ret = copy_dtb(data, scratch_offset);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002802
2803 dtb_copied = !ret ? 1 : 0;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002804#else
Mayank Grovere559cec2017-10-17 15:12:03 +05302805 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE) ||
2806 check_ddr_addr_range_bound(hdr->tags_addr, MAX_TAGS_SIZE))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002807 {
Mayank Grovere559cec2017-10-17 15:12:03 +05302808 dprintf(CRITICAL, "Tags addresses are not valid.\n");
lijuang2008ff22016-03-07 17:56:27 +08002809 goto boot_failed;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002810 }
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002811#endif
2812
2813 /* Load ramdisk & kernel */
2814 memmove((void*) hdr->ramdisk_addr, ptr + page_size + kernel_actual, hdr->ramdisk_size);
Matthew Qinbb7923d2015-02-09 10:56:09 +08002815 memmove((void*) hdr->kernel_addr, (char*) (kernel_start_addr), kernel_size);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002816
2817#if DEVICE_TREE
Mayank Grovere559cec2017-10-17 15:12:03 +05302818 if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual) ||
2819 check_ddr_addr_range_bound(hdr->tags_addr, kernel_actual))
Matthew Qinbb7923d2015-02-09 10:56:09 +08002820 {
Mayank Grovere559cec2017-10-17 15:12:03 +05302821 dprintf(CRITICAL, "Tags addresses are not valid.\n");
lijuang2008ff22016-03-07 17:56:27 +08002822 goto boot_failed;
Matthew Qinbb7923d2015-02-09 10:56:09 +08002823 }
2824
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002825 /*
2826 * If dtb is not found look for appended DTB in the kernel.
2827 * If appended dev tree is found, update the atags with
2828 * memory address to the DTB appended location on RAM.
2829 * Else update with the atags address in the kernel header
2830 */
2831 if (!dtb_copied) {
2832 void *dtb;
Matthew Qinbb7923d2015-02-09 10:56:09 +08002833 dtb = dev_tree_appended((void*)(ptr + page_size),
2834 hdr->kernel_size, dtb_offset,
Dima Zavine63e5572013-05-03 12:23:06 -07002835 (void *)hdr->tags_addr);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002836 if (!dtb) {
2837 fastboot_fail("dtb not found");
lijuang2008ff22016-03-07 17:56:27 +08002838 goto boot_failed;
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002839 }
Amol Jadicb524072012-08-09 16:40:18 -07002840 }
2841#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08002842
2843 fastboot_okay("");
Amol Jadi7c4316c2013-10-07 14:19:26 -07002844 fastboot_stop();
Brian Swetland9c4c0752009-01-25 16:23:50 -08002845
Dima Zavin77e41f32013-03-06 16:10:43 -08002846 boot_linux((void*) hdr->kernel_addr, (void*) hdr->tags_addr,
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002847 (const char*) hdr->cmdline, board_machtype(),
2848 (void*) hdr->ramdisk_addr, hdr->ramdisk_size);
lijuang2008ff22016-03-07 17:56:27 +08002849
2850 /* fastboot already stop, it's no need to show fastboot menu */
2851 return;
2852boot_failed:
2853#if FBCON_DISPLAY_MSG
2854 /* revert to fastboot menu if boot failed */
2855 display_fastboot_menu();
2856#endif
2857 return;
Brian Swetland9c4c0752009-01-25 16:23:50 -08002858}
2859
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002860void cmd_erase_nand(const char *arg, void *data, unsigned sz)
Dima Zavin214cc642009-01-26 11:16:21 -08002861{
2862 struct ptentry *ptn;
2863 struct ptable *ptable;
2864
2865 ptable = flash_get_ptable();
2866 if (ptable == NULL) {
2867 fastboot_fail("partition table doesn't exist");
2868 return;
2869 }
2870
2871 ptn = ptable_find(ptable, arg);
2872 if (ptn == NULL) {
2873 fastboot_fail("unknown partition name");
2874 return;
2875 }
2876
Monika Singh292b3e92018-03-17 22:40:23 +05302877 if (!strncmp(arg, "avb_custom_key", strlen("avb_custom_key"))) {
2878 dprintf(INFO, "erasing avb_custom_key\n");
2879 if (erase_userkey()) {
2880 fastboot_fail("Erasing avb_custom_key failed");
2881 } else {
2882 fastboot_okay("");
2883 }
2884 return;
2885 }
2886
Dima Zavin214cc642009-01-26 11:16:21 -08002887 if (flash_erase(ptn)) {
2888 fastboot_fail("failed to erase partition");
2889 return;
2890 }
2891 fastboot_okay("");
2892}
2893
Bikas Gurungd48bd242010-09-04 19:54:32 -07002894
2895void cmd_erase_mmc(const char *arg, void *data, unsigned sz)
2896{
2897 unsigned long long ptn = 0;
Oliver Wangcee448d2013-10-22 18:40:13 +08002898 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07002899 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002900 uint8_t lun = 0;
Vijay Kumar Pendoti0b21f462016-05-02 17:09:18 +05302901 char *footer = NULL;
Bikas Gurungd48bd242010-09-04 19:54:32 -07002902
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002903#if VERIFIED_BOOT
2904 if(!strcmp(arg, KEYSTORE_PTN_NAME))
2905 {
2906 if(!device.is_unlocked)
2907 {
2908 fastboot_fail("unlock device to erase keystore");
2909 return;
2910 }
2911 }
2912#endif
2913
Kinson Chikf1a43512011-07-14 11:28:39 -07002914 index = partition_get_index(arg);
2915 ptn = partition_get_offset(index);
Oliver Wangcee448d2013-10-22 18:40:13 +08002916 size = partition_get_size(index);
Neeti Desaica8c9602011-10-06 11:40:00 -07002917
Kinson Chikf1a43512011-07-14 11:28:39 -07002918 if(ptn == 0) {
Neeti Desaica8c9602011-10-06 11:40:00 -07002919 fastboot_fail("Partition table doesn't exist\n");
Bikas Gurungd48bd242010-09-04 19:54:32 -07002920 return;
2921 }
Kun Liang2f1601a2013-08-12 16:29:54 +08002922
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002923 lun = partition_get_lun(index);
2924 mmc_set_lun(lun);
2925
Monika Singh292b3e92018-03-17 22:40:23 +05302926 if (!strncmp(arg, "avb_custom_key", strlen("avb_custom_key"))) {
2927 dprintf(INFO, "erasing avb_custom_key\n");
2928 if (erase_userkey()) {
2929 fastboot_fail("Erasing avb_custom_key failed");
2930 } else {
2931 fastboot_okay("");
2932 }
2933 return;
2934 }
Sridhar Parasuramd7957122015-02-27 11:33:40 -08002935 if (platform_boot_dev_isemmc())
2936 {
2937 if (mmc_erase_card(ptn, size)) {
2938 fastboot_fail("failed to erase partition\n");
2939 return;
2940 }
2941 } else {
2942 BUF_DMA_ALIGN(out, DEFAULT_ERASE_SIZE);
2943 size = partition_get_size(index);
2944 if (size > DEFAULT_ERASE_SIZE)
2945 size = DEFAULT_ERASE_SIZE;
Kun Liang2f1601a2013-08-12 16:29:54 +08002946
Sridhar Parasuramd7957122015-02-27 11:33:40 -08002947 /* Simple inefficient version of erase. Just writing
2948 0 in first several blocks */
2949 if (mmc_write(ptn , size, (unsigned int *)out)) {
2950 fastboot_fail("failed to erase partition");
2951 return;
2952 }
Vijay Kumar Pendoti0b21f462016-05-02 17:09:18 +05302953 /*Erase FDE metadata at the userdata footer*/
2954 if(!(strncmp(arg, "userdata", 8)))
2955 {
2956 footer = memalign(CACHE_LINE, FOOTER_SIZE);
2957 memset((void *)footer, 0, FOOTER_SIZE);
2958
2959 size = partition_get_size(index);
2960
2961 if (mmc_write((ptn + size) - FOOTER_SIZE , FOOTER_SIZE, (unsigned int *)footer)) {
2962 fastboot_fail("failed to erase userdata footer");
2963 free(footer);
2964 return;
2965 }
2966 free(footer);
2967 }
Bikas Gurungd48bd242010-09-04 19:54:32 -07002968 }
Monika Singh292b3e92018-03-17 22:40:23 +05302969#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover912eaa62017-10-26 12:08:53 +05302970 if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05302971 !(strncmp(arg, "userdata", 8)) &&
2972 send_delete_keys_to_tz())
Sridhar Parasuram32b30662015-07-10 13:33:22 -07002973 ASSERT(0);
2974#endif
Bikas Gurungd48bd242010-09-04 19:54:32 -07002975 fastboot_okay("");
2976}
2977
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002978void cmd_erase(const char *arg, void *data, unsigned sz)
2979{
Monika Singh292b3e92018-03-17 22:40:23 +05302980#if VERIFIED_BOOT || VERIFIED_BOOT_2
Channagoud Kadabi35297672015-06-13 11:09:49 -07002981 if (target_build_variant_user())
Channagoud Kadabice3cf422015-04-17 16:02:30 -07002982 {
Sridhar Parasuramc32d07d2015-07-12 10:57:48 -07002983 if(!device.is_unlocked)
Channagoud Kadabice3cf422015-04-17 16:02:30 -07002984 {
Channagoud Kadabi35297672015-06-13 11:09:49 -07002985 fastboot_fail("device is locked. Cannot erase");
Channagoud Kadabice3cf422015-04-17 16:02:30 -07002986 return;
2987 }
2988 }
2989#endif
2990
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002991 if(target_is_emmc_boot())
2992 cmd_erase_mmc(arg, data, sz);
2993 else
2994 cmd_erase_nand(arg, data, sz);
2995}
Bikas Gurungd48bd242010-09-04 19:54:32 -07002996
Mayank Grover11ff9692018-01-11 11:54:49 +05302997/* Get the size from partiton name */
2998static void get_partition_size(const char *arg, char *response)
2999{
3000 uint64_t ptn = 0;
3001 uint64_t size;
3002 int index = INVALID_PTN;
3003
3004 index = partition_get_index(arg);
3005
3006 if (index == INVALID_PTN)
3007 {
3008 dprintf(CRITICAL, "Invalid partition index\n");
3009 return;
3010 }
3011
3012 ptn = partition_get_offset(index);
3013
3014 if(!ptn)
3015 {
3016 dprintf(CRITICAL, "Invalid partition name %s\n", arg);
3017 return;
3018 }
3019
3020 size = partition_get_size(index);
3021
3022 snprintf(response, MAX_RSP_SIZE, "\t 0x%llx", size);
3023 return;
3024}
3025
Mayank Grover52cd10a2018-03-15 12:57:54 +05303026/* Function to check partition type of a partition*/
3027static fs_signature_type
3028check_partition_fs_signature(const char *arg)
3029{
3030 fs_signature_type ret = NO_FS;
3031 int index;
3032 unsigned long long ptn;
3033 char *sb_buffer = malloc(mmc_blocksize);
3034 if (!sb_buffer)
3035 {
3036 dprintf(CRITICAL, "ERROR: Failed to allocate buffer for superblock\n");
3037 goto out;
3038 }
3039
3040 /* Read super block */
3041 if ((index = partition_get_index(arg)) < 0)
3042 {
3043 dprintf(CRITICAL, "ERROR: %s() doesn't exsit\n", arg);
3044 goto out;
3045 }
3046 ptn = partition_get_offset(index);
3047 mmc_set_lun(partition_get_lun(index));
3048 if(mmc_read(ptn + FS_SUPERBLOCK_OFFSET,
3049 (void *)sb_buffer, mmc_blocksize))
3050 {
3051 dprintf(CRITICAL, "ERROR: Failed to read Superblock\n");
3052 goto out;
3053 }
3054
3055 if (*((uint16 *)(&sb_buffer[EXT_MAGIC_OFFSET_SB]))
3056 == (uint16)EXT_MAGIC)
3057 {
3058 dprintf(SPEW, "%s() Found EXT FS\n", arg);
3059 ret = EXT_FS_SIGNATURE;
3060 }
3061 else if (*((uint32 *)(&sb_buffer[F2FS_MAGIC_OFFSET_SB]))
3062 == F2FS_MAGIC)
3063 {
3064 dprintf(SPEW, "%s() Found F2FS FS\n", arg);
3065 ret = EXT_F2FS_SIGNATURE;
3066 }
3067 else
3068 {
3069 dprintf(SPEW, "%s() Reverting to default 0x%x\n",
3070 arg, *((uint16 *)(&sb_buffer[EXT_MAGIC_OFFSET_SB])));
3071 ret = NO_FS;
3072 }
3073
3074out:
3075 if(sb_buffer)
3076 free(sb_buffer);
3077 return ret;
3078}
3079
Mayank Groverd38fe012018-03-13 15:33:16 +05303080/* Function to get partition type */
3081static void get_partition_type(const char *arg, char *response)
3082{
3083 uint n = 0;
Mayank Grover52cd10a2018-03-15 12:57:54 +05303084 fs_signature_type fs_signature;
Mayank Groverd38fe012018-03-13 15:33:16 +05303085
3086 if (arg == NULL ||
3087 response == NULL)
3088 {
3089 dprintf(CRITICAL, "Invalid input parameter\n");
3090 return;
3091 }
3092
3093 /* By default copy raw to response */
3094 strncpy(response, RAW_STR, strlen(RAW_STR));
3095
3096 /* Mark partiton type for known paritions only */
3097 for (n=0; n < ARRAY_SIZE(part_type_known); n++)
3098 {
3099 if (!strncmp(part_type_known[n].part_name, arg, strlen(arg)))
3100 {
Mayank Grover52cd10a2018-03-15 12:57:54 +05303101 /* Check partition for FS signature */
3102 fs_signature = check_partition_fs_signature(arg);
3103 switch (fs_signature)
3104 {
3105 case EXT_FS_SIGNATURE:
3106 strncpy(response, EXT_STR, strlen(EXT_STR));
3107 break;
3108 case EXT_F2FS_SIGNATURE:
3109 strncpy(response, F2FS_STR, strlen(F2FS_STR));
3110 break;
3111 case NO_FS:
3112 strncpy(response, part_type_known[n].type_response, MAX_RSP_SIZE);
3113 }
Mayank Groverd38fe012018-03-13 15:33:16 +05303114 }
3115 }
3116 return;
3117}
3118
Mayank Grover11ff9692018-01-11 11:54:49 +05303119/*
3120 * Publish the partition type & size info
3121 * fastboot getvar will publish the required information.
3122 * fastboot getvar partition_size:<partition_name>: partition size in hex
3123 * fastboot getvar partition_type:<partition_name>: partition type (ext/fat)
3124 */
3125static void publish_getvar_partition_info(struct getvar_partition_info *info, uint8_t num_parts)
3126{
Mayank Groverd38fe012018-03-13 15:33:16 +05303127 uint8_t i;
Mayank Grover11ff9692018-01-11 11:54:49 +05303128 static bool published = false;
3129 struct partition_entry *ptn_entry =
3130 partition_get_partition_entries();
3131 memset(info, 0, sizeof(struct getvar_partition_info)* num_parts);
3132
3133 for (i = 0; i < num_parts; i++) {
3134 strlcat(info[i].part_name, (const char *)ptn_entry[i].name, MAX_RSP_SIZE);
3135 strlcat(info[i].getvar_size, "partition-size:", MAX_GET_VAR_NAME_SIZE);
3136 strlcat(info[i].getvar_type, "partition-type:", MAX_GET_VAR_NAME_SIZE);
3137
Mayank Groverd38fe012018-03-13 15:33:16 +05303138 get_partition_type(info[i].part_name, info[i].type_response);
Mayank Grover11ff9692018-01-11 11:54:49 +05303139 get_partition_size(info[i].part_name, info[i].size_response);
Mayank Grover11ff9692018-01-11 11:54:49 +05303140 if (strlcat(info[i].getvar_size, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
3141 {
3142 dprintf(CRITICAL, "partition size name truncated\n");
3143 return;
3144 }
3145 if (strlcat(info[i].getvar_type, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
3146 {
3147 dprintf(CRITICAL, "partition type name truncated\n");
3148 return;
3149 }
3150
3151 if (!published)
3152 {
3153 /* publish partition size & type info */
3154 fastboot_publish((const char *) info[i].getvar_size, (const char *) info[i].size_response);
3155 fastboot_publish((const char *) info[i].getvar_type, (const char *) info[i].type_response);
3156 }
3157 }
3158 if (!published)
3159 published = true;
3160}
3161
3162
Ajay Dudani5c761132011-04-07 20:19:04 -07003163void cmd_flash_mmc_img(const char *arg, void *data, unsigned sz)
Shashank Mittal23b8f422010-04-16 19:27:21 -07003164{
3165 unsigned long long ptn = 0;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07003166 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07003167 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003168 char *token = NULL;
3169 char *pname = NULL;
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07003170 char *sp;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003171 uint8_t lun = 0;
3172 bool lun_set = false;
Mayank Grover351a75e2017-05-30 20:06:08 +05303173 int current_active_slot = INVALID;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07003174
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07003175 token = strtok_r((char *)arg, ":", &sp);
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003176 pname = token;
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07003177 token = strtok_r(NULL, ":", &sp);
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003178 if(token)
3179 {
3180 lun = atoi(token);
3181 mmc_set_lun(lun);
3182 lun_set = true;
3183 }
3184
Mao Jinlong226f33a2014-07-04 17:24:10 +08003185 if (pname)
Greg Grisco6e754772011-06-23 12:19:39 -07003186 {
Channagoud Kadabiad259832015-05-29 11:14:17 -07003187 if (!strncmp(pname, "frp-unlock", strlen("frp-unlock")))
3188 {
3189 if (!aboot_frp_unlock(pname, data, sz))
3190 {
3191 fastboot_info("FRP unlock successful");
3192 fastboot_okay("");
3193 }
3194 else
3195 fastboot_fail("Secret key is invalid, please update the bootloader with secret key");
3196
3197 return;
3198 }
3199
Mao Jinlong226f33a2014-07-04 17:24:10 +08003200 if (!strcmp(pname, "partition"))
3201 {
3202 dprintf(INFO, "Attempt to write partition image.\n");
3203 if (write_partition(sz, (unsigned char *) data)) {
3204 fastboot_fail("failed to write partition");
Greg Grisco6e754772011-06-23 12:19:39 -07003205 return;
3206 }
Mayank Grover11ff9692018-01-11 11:54:49 +05303207 /* Re-publish partition table */
3208 publish_getvar_partition_info(part_info, partition_get_partition_count());
Mayank Grover351a75e2017-05-30 20:06:08 +05303209
3210 /* Rescan partition table to ensure we have multislot support*/
3211 if (partition_scan_for_multislot())
3212 {
3213 current_active_slot = partition_find_active_slot();
3214 dprintf(INFO, "Multislot supported: Slot %s active",
3215 (SUFFIX_SLOT(current_active_slot)));
3216 }
3217 partition_mark_active_slot(current_active_slot);
Greg Grisco6e754772011-06-23 12:19:39 -07003218 }
Mao Jinlong226f33a2014-07-04 17:24:10 +08003219 else
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003220 {
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003221#if VERIFIED_BOOT
3222 if(!strcmp(pname, KEYSTORE_PTN_NAME))
3223 {
3224 if(!device.is_unlocked)
3225 {
3226 fastboot_fail("unlock device to flash keystore");
3227 return;
3228 }
Gaurav Nebhwanic7313cc2015-12-15 22:25:04 +05303229 if(!boot_verify_validate_keystore((unsigned char *)data,sz))
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003230 {
3231 fastboot_fail("image is not a keystore file");
3232 return;
3233 }
3234 }
3235#endif
Mao Jinlong226f33a2014-07-04 17:24:10 +08003236 index = partition_get_index(pname);
3237 ptn = partition_get_offset(index);
3238 if(ptn == 0) {
3239 fastboot_fail("partition table doesn't exist");
3240 return;
3241 }
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003242
Mayank Grover351a75e2017-05-30 20:06:08 +05303243 if (!strncmp(pname, "boot", strlen("boot"))
3244 || !strcmp(pname, "recovery"))
3245 {
Mao Jinlong226f33a2014-07-04 17:24:10 +08003246 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
3247 fastboot_fail("image is not a boot image");
3248 return;
3249 }
Mayank Grover351a75e2017-05-30 20:06:08 +05303250
3251 /* Reset multislot_partition attributes in case of flashing boot */
3252 if (partition_multislot_is_supported())
3253 {
3254 partition_reset_attributes(index);
3255 }
Mao Jinlong226f33a2014-07-04 17:24:10 +08003256 }
3257
3258 if(!lun_set)
3259 {
3260 lun = partition_get_lun(index);
3261 mmc_set_lun(lun);
3262 }
3263
3264 size = partition_get_size(index);
Gaurav Nebhwani4d2389c2016-03-17 21:10:05 +05303265 if (ROUND_TO_PAGE(sz, mmc_blocksize_mask) > size) {
Mao Jinlong226f33a2014-07-04 17:24:10 +08003266 fastboot_fail("size too large");
3267 return;
3268 }
3269 else if (mmc_write(ptn , sz, (unsigned int *)data)) {
3270 fastboot_fail("flash write failure");
3271 return;
3272 }
Greg Grisco6e754772011-06-23 12:19:39 -07003273 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07003274 }
3275 fastboot_okay("");
3276 return;
3277}
3278
Ajay Dudanide984792015-03-02 09:57:41 -08003279void cmd_flash_meta_img(const char *arg, void *data, unsigned sz)
3280{
3281 int i, images;
3282 meta_header_t *meta_header;
3283 img_header_entry_t *img_header_entry;
Parth Dixit3e2d3032016-03-04 17:11:52 +05303284 /*End of the image address*/
3285 uintptr_t data_end;
3286
3287 if( (UINT_MAX - sz) > (uintptr_t)data )
3288 data_end = (uintptr_t)data + sz;
3289 else
3290 {
3291 fastboot_fail("Cannot flash: image header corrupt");
3292 return;
3293 }
3294
3295 if( data_end < ((uintptr_t)data + sizeof(meta_header_t)))
3296 {
3297 fastboot_fail("Cannot flash: image header corrupt");
3298 return;
3299 }
Ajay Dudanide984792015-03-02 09:57:41 -08003300
lijuang8ee21882016-04-19 16:57:11 +08003301 /* If device is locked:
3302 * Forbid to flash image to avoid the device to bypass the image
3303 * which with "any" name other than bootloader. Because it maybe
3304 * a meta package of all partitions.
3305 */
Monika Singh292b3e92018-03-17 22:40:23 +05303306#if VERIFIED_BOOT || VERIFIED_BOOT_2
lijuang8ee21882016-04-19 16:57:11 +08003307 if (target_build_variant_user()) {
3308 if (!device.is_unlocked) {
3309 fastboot_fail("Device is locked, meta image flashing is not allowed");
3310 return;
3311 }
Mayank Grover889be1b2017-09-12 20:12:23 +05303312
Mayank Grover912eaa62017-10-26 12:08:53 +05303313 if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05303314 !device.is_unlock_critical)
3315 {
lijuang8ee21882016-04-19 16:57:11 +08003316 fastboot_fail("Device is critical locked, Meta image flashing is not allowed");
3317 return;
3318 }
lijuang8ee21882016-04-19 16:57:11 +08003319 }
3320#endif
3321
Ajay Dudanide984792015-03-02 09:57:41 -08003322 meta_header = (meta_header_t*) data;
Parth Dixit3e2d3032016-03-04 17:11:52 +05303323 if( data_end < ((uintptr_t)data + meta_header->img_hdr_sz))
3324 {
3325 fastboot_fail("Cannot flash: image header corrupt");
3326 return;
3327 }
Ajay Dudanide984792015-03-02 09:57:41 -08003328 img_header_entry = (img_header_entry_t*) (data+sizeof(meta_header_t));
3329
3330 images = meta_header->img_hdr_sz / sizeof(img_header_entry_t);
3331
3332 for (i=0; i<images; i++) {
3333
3334 if((img_header_entry[i].ptn_name == NULL) ||
3335 (img_header_entry[i].start_offset == 0) ||
3336 (img_header_entry[i].size == 0))
3337 break;
Kishor PK49831502017-04-21 17:55:43 +05303338 if ((UINT_MAX - img_header_entry[i].start_offset) < (uintptr_t)data) {
3339 fastboot_fail("Integer overflow detected in start_offset of img");
3340 break;
3341 }
3342 else if ((UINT_MAX - (img_header_entry[i].start_offset + (uintptr_t)data)) < img_header_entry[i].size) {
3343 fastboot_fail("Integer overflow detected in size of img");
3344 break;
3345 }
Parth Dixit3e2d3032016-03-04 17:11:52 +05303346 if( data_end < ((uintptr_t)data + img_header_entry[i].start_offset
3347 + img_header_entry[i].size) )
3348 {
3349 fastboot_fail("Cannot flash: image size mismatch");
3350 break;
3351 }
3352
Ajay Dudanide984792015-03-02 09:57:41 -08003353 cmd_flash_mmc_img(img_header_entry[i].ptn_name,
3354 (void *) data + img_header_entry[i].start_offset,
3355 img_header_entry[i].size);
3356 }
3357
Channagoud Kadabiaafbbfc2015-03-04 17:52:56 -08003358 if (!strncmp(arg, "bootloader", strlen("bootloader")))
3359 {
3360 strlcpy(device.bootloader_version, TARGET(BOARD), MAX_VERSION_LEN);
3361 strlcat(device.bootloader_version, "-", MAX_VERSION_LEN);
3362 strlcat(device.bootloader_version, meta_header->img_version, MAX_VERSION_LEN);
3363 }
3364 else
3365 {
3366 strlcpy(device.radio_version, TARGET(BOARD), MAX_VERSION_LEN);
3367 strlcat(device.radio_version, "-", MAX_VERSION_LEN);
3368 strlcat(device.radio_version, meta_header->img_version, MAX_VERSION_LEN);
3369 }
3370
3371 write_device_info(&device);
Ajay Dudanide984792015-03-02 09:57:41 -08003372 fastboot_okay("");
3373 return;
3374}
3375
Ajay Dudani5c761132011-04-07 20:19:04 -07003376void cmd_flash_mmc_sparse_img(const char *arg, void *data, unsigned sz)
3377{
3378 unsigned int chunk;
wufeng.jiang813dc352015-06-02 23:02:46 -04003379 uint64_t chunk_data_sz;
Channagoud Kadabif627cf82013-09-09 14:08:20 -07003380 uint32_t *fill_buf = NULL;
3381 uint32_t fill_val;
Mayank Grover4f50bba2017-07-19 18:17:08 +05303382 uint32_t blk_sz_actual = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07003383 sparse_header_t *sparse_header;
3384 chunk_header_t *chunk_header;
Ajay Dudaniab18f022011-05-12 14:39:22 -07003385 uint32_t total_blocks = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07003386 unsigned long long ptn = 0;
Channagoud Kadabi65b91002011-10-11 17:34:33 +05303387 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07003388 int index = INVALID_PTN;
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05303389 uint32_t i;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003390 uint8_t lun = 0;
vijay kumar800255e2015-04-24 20:26:19 +05303391 /*End of the sparse image address*/
Parth Dixit64b1a482016-03-07 16:31:26 +05303392 uintptr_t data_end = (uintptr_t)data + sz;
Ajay Dudani5c761132011-04-07 20:19:04 -07003393
Kinson Chikf1a43512011-07-14 11:28:39 -07003394 index = partition_get_index(arg);
3395 ptn = partition_get_offset(index);
3396 if(ptn == 0) {
Ajay Dudani5c761132011-04-07 20:19:04 -07003397 fastboot_fail("partition table doesn't exist");
3398 return;
3399 }
3400
Channagoud Kadabi65b91002011-10-11 17:34:33 +05303401 size = partition_get_size(index);
Channagoud Kadabi65b91002011-10-11 17:34:33 +05303402
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003403 lun = partition_get_lun(index);
3404 mmc_set_lun(lun);
3405
vijay kumar800255e2015-04-24 20:26:19 +05303406 if (sz < sizeof(sparse_header_t)) {
3407 fastboot_fail("size too low");
3408 return;
3409 }
3410
Ajay Dudani5c761132011-04-07 20:19:04 -07003411 /* Read and skip over sparse image header */
3412 sparse_header = (sparse_header_t *) data;
vijay kumar800255e2015-04-24 20:26:19 +05303413
Mayank Grover48bd9bb2017-07-19 12:04:16 +05303414 if (!sparse_header->blk_sz || (sparse_header->blk_sz % 4)){
3415 fastboot_fail("Invalid block size\n");
3416 return;
3417 }
3418
vijay kumar1321f342015-03-27 12:13:42 +05303419 if (((uint64_t)sparse_header->total_blks * (uint64_t)sparse_header->blk_sz) > size) {
Ajay Dudani876b3282012-12-21 14:12:17 -08003420 fastboot_fail("size too large");
3421 return;
3422 }
3423
vijay kumarde4fcf62015-04-23 13:05:49 +05303424 data += sizeof(sparse_header_t);
vijay kumar800255e2015-04-24 20:26:19 +05303425
Parth Dixit64b1a482016-03-07 16:31:26 +05303426 if (data_end < (uintptr_t)data) {
vijay kumar800255e2015-04-24 20:26:19 +05303427 fastboot_fail("buffer overreads occured due to invalid sparse header");
3428 return;
3429 }
3430
vijay kumarde4fcf62015-04-23 13:05:49 +05303431 if(sparse_header->file_hdr_sz != sizeof(sparse_header_t))
Ajay Dudani5c761132011-04-07 20:19:04 -07003432 {
vijay kumarde4fcf62015-04-23 13:05:49 +05303433 fastboot_fail("sparse header size mismatch");
3434 return;
Ajay Dudani5c761132011-04-07 20:19:04 -07003435 }
3436
Ajay Dudanib06c05f2011-05-12 14:46:10 -07003437 dprintf (SPEW, "=== Sparse Image Header ===\n");
3438 dprintf (SPEW, "magic: 0x%x\n", sparse_header->magic);
3439 dprintf (SPEW, "major_version: 0x%x\n", sparse_header->major_version);
3440 dprintf (SPEW, "minor_version: 0x%x\n", sparse_header->minor_version);
3441 dprintf (SPEW, "file_hdr_sz: %d\n", sparse_header->file_hdr_sz);
3442 dprintf (SPEW, "chunk_hdr_sz: %d\n", sparse_header->chunk_hdr_sz);
3443 dprintf (SPEW, "blk_sz: %d\n", sparse_header->blk_sz);
3444 dprintf (SPEW, "total_blks: %d\n", sparse_header->total_blks);
3445 dprintf (SPEW, "total_chunks: %d\n", sparse_header->total_chunks);
Ajay Dudani5c761132011-04-07 20:19:04 -07003446
3447 /* Start processing chunks */
3448 for (chunk=0; chunk<sparse_header->total_chunks; chunk++)
3449 {
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05303450 /* Make sure the total image size does not exceed the partition size */
3451 if(((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz) >= size) {
3452 fastboot_fail("size too large");
3453 return;
3454 }
Ajay Dudani5c761132011-04-07 20:19:04 -07003455 /* Read and skip over chunk header */
3456 chunk_header = (chunk_header_t *) data;
3457 data += sizeof(chunk_header_t);
3458
Parth Dixit64b1a482016-03-07 16:31:26 +05303459 if (data_end < (uintptr_t)data) {
vijay kumar800255e2015-04-24 20:26:19 +05303460 fastboot_fail("buffer overreads occured due to invalid sparse header");
3461 return;
3462 }
3463
Ajay Dudani5c761132011-04-07 20:19:04 -07003464 dprintf (SPEW, "=== Chunk Header ===\n");
3465 dprintf (SPEW, "chunk_type: 0x%x\n", chunk_header->chunk_type);
3466 dprintf (SPEW, "chunk_data_sz: 0x%x\n", chunk_header->chunk_sz);
3467 dprintf (SPEW, "total_size: 0x%x\n", chunk_header->total_sz);
3468
vijay kumar800255e2015-04-24 20:26:19 +05303469 if(sparse_header->chunk_hdr_sz != sizeof(chunk_header_t))
Ajay Dudani5c761132011-04-07 20:19:04 -07003470 {
vijay kumar800255e2015-04-24 20:26:19 +05303471 fastboot_fail("chunk header size mismatch");
3472 return;
Ajay Dudani5c761132011-04-07 20:19:04 -07003473 }
3474
wufeng.jiang813dc352015-06-02 23:02:46 -04003475 chunk_data_sz = (uint64_t)sparse_header->blk_sz * chunk_header->chunk_sz;
3476
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05303477 /* Make sure that the chunk size calculated from sparse image does not
3478 * exceed partition size
3479 */
3480 if ((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz + chunk_data_sz > size)
3481 {
3482 fastboot_fail("Chunk data size exceeds partition size");
3483 return;
3484 }
3485
Ajay Dudani5c761132011-04-07 20:19:04 -07003486 switch (chunk_header->chunk_type)
3487 {
3488 case CHUNK_TYPE_RAW:
wufeng.jiang813dc352015-06-02 23:02:46 -04003489 if((uint64_t)chunk_header->total_sz != ((uint64_t)sparse_header->chunk_hdr_sz +
Ajay Dudani5c761132011-04-07 20:19:04 -07003490 chunk_data_sz))
3491 {
3492 fastboot_fail("Bogus chunk size for chunk type Raw");
3493 return;
3494 }
3495
Parth Dixit64b1a482016-03-07 16:31:26 +05303496 if (data_end < (uintptr_t)data + chunk_data_sz) {
vijay kumar800255e2015-04-24 20:26:19 +05303497 fastboot_fail("buffer overreads occured due to invalid sparse header");
3498 return;
3499 }
3500
wufeng.jiang813dc352015-06-02 23:02:46 -04003501 /* chunk_header->total_sz is uint32,So chunk_data_sz is now less than 2^32
3502 otherwise it will return in the line above
3503 */
Ajay Dudaniab18f022011-05-12 14:39:22 -07003504 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
wufeng.jiang813dc352015-06-02 23:02:46 -04003505 (uint32_t)chunk_data_sz,
Ajay Dudaniab18f022011-05-12 14:39:22 -07003506 (unsigned int*)data))
Ajay Dudani5c761132011-04-07 20:19:04 -07003507 {
3508 fastboot_fail("flash write failure");
3509 return;
3510 }
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05303511 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
3512 fastboot_fail("Bogus size for RAW chunk type");
3513 return;
3514 }
Ajay Dudani5c761132011-04-07 20:19:04 -07003515 total_blocks += chunk_header->chunk_sz;
wufeng.jiang813dc352015-06-02 23:02:46 -04003516 data += (uint32_t)chunk_data_sz;
Ajay Dudani5c761132011-04-07 20:19:04 -07003517 break;
3518
Channagoud Kadabif627cf82013-09-09 14:08:20 -07003519 case CHUNK_TYPE_FILL:
3520 if(chunk_header->total_sz != (sparse_header->chunk_hdr_sz +
3521 sizeof(uint32_t)))
3522 {
3523 fastboot_fail("Bogus chunk size for chunk type FILL");
3524 return;
3525 }
3526
Mayank Grover4f50bba2017-07-19 18:17:08 +05303527 blk_sz_actual = ROUNDUP(sparse_header->blk_sz, CACHE_LINE);
3528 /* Integer overflow detected */
3529 if (blk_sz_actual < sparse_header->blk_sz)
3530 {
3531 fastboot_fail("Invalid block size");
3532 return;
3533 }
3534
3535 fill_buf = (uint32_t *)memalign(CACHE_LINE, blk_sz_actual);
Channagoud Kadabif627cf82013-09-09 14:08:20 -07003536 if (!fill_buf)
3537 {
3538 fastboot_fail("Malloc failed for: CHUNK_TYPE_FILL");
3539 return;
3540 }
3541
Parth Dixit64b1a482016-03-07 16:31:26 +05303542 if (data_end < (uintptr_t)data + sizeof(uint32_t)) {
vijay kumar800255e2015-04-24 20:26:19 +05303543 fastboot_fail("buffer overreads occured due to invalid sparse header");
Gaurav Nebhwani163cbf82016-03-17 21:21:27 +05303544 free(fill_buf);
vijay kumar800255e2015-04-24 20:26:19 +05303545 return;
3546 }
Channagoud Kadabif627cf82013-09-09 14:08:20 -07003547 fill_val = *(uint32_t *)data;
3548 data = (char *) data + sizeof(uint32_t);
Channagoud Kadabif627cf82013-09-09 14:08:20 -07003549
3550 for (i = 0; i < (sparse_header->blk_sz / sizeof(fill_val)); i++)
3551 {
3552 fill_buf[i] = fill_val;
3553 }
3554
Gaurav Nebhwanie94cbe12016-03-17 21:16:34 +05303555 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz))
3556 {
3557 fastboot_fail("bogus size for chunk FILL type");
3558 free(fill_buf);
3559 return;
3560 }
3561
wufeng.jiang813dc352015-06-02 23:02:46 -04003562 for (i = 0; i < chunk_header->chunk_sz; i++)
Channagoud Kadabif627cf82013-09-09 14:08:20 -07003563 {
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05303564 /* Make sure that the data written to partition does not exceed partition size */
3565 if ((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz + sparse_header->blk_sz > size)
3566 {
3567 fastboot_fail("Chunk data size for fill type exceeds partition size");
Gaurav Nebhwani163cbf82016-03-17 21:21:27 +05303568 free(fill_buf);
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05303569 return;
3570 }
3571
Channagoud Kadabif627cf82013-09-09 14:08:20 -07003572 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
3573 sparse_header->blk_sz,
3574 fill_buf))
3575 {
3576 fastboot_fail("flash write failure");
3577 free(fill_buf);
3578 return;
3579 }
3580
3581 total_blocks++;
3582 }
3583
3584 free(fill_buf);
3585 break;
3586
Ajay Dudani5c761132011-04-07 20:19:04 -07003587 case CHUNK_TYPE_DONT_CARE:
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05303588 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
3589 fastboot_fail("bogus size for chunk DONT CARE type");
3590 return;
3591 }
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07003592 total_blocks += chunk_header->chunk_sz;
3593 break;
3594
Ajay Dudani5c761132011-04-07 20:19:04 -07003595 case CHUNK_TYPE_CRC:
3596 if(chunk_header->total_sz != sparse_header->chunk_hdr_sz)
3597 {
wufeng.jiang813dc352015-06-02 23:02:46 -04003598 fastboot_fail("Bogus chunk size for chunk type CRC");
Ajay Dudani5c761132011-04-07 20:19:04 -07003599 return;
3600 }
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05303601 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
3602 fastboot_fail("bogus size for chunk CRC type");
3603 return;
3604 }
Ajay Dudani5c761132011-04-07 20:19:04 -07003605 total_blocks += chunk_header->chunk_sz;
Parth Dixit64b1a482016-03-07 16:31:26 +05303606 if ((uintptr_t)data > UINT_MAX - chunk_data_sz) {
vijay kumar800255e2015-04-24 20:26:19 +05303607 fastboot_fail("integer overflow occured");
3608 return;
3609 }
wufeng.jiang813dc352015-06-02 23:02:46 -04003610 data += (uint32_t)chunk_data_sz;
Parth Dixit64b1a482016-03-07 16:31:26 +05303611 if (data_end < (uintptr_t)data) {
vijay kumar800255e2015-04-24 20:26:19 +05303612 fastboot_fail("buffer overreads occured due to invalid sparse header");
3613 return;
3614 }
Ajay Dudani5c761132011-04-07 20:19:04 -07003615 break;
3616
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07003617 default:
Channagoud Kadabif627cf82013-09-09 14:08:20 -07003618 dprintf(CRITICAL, "Unkown chunk type: %x\n",chunk_header->chunk_type);
Ajay Dudani5c761132011-04-07 20:19:04 -07003619 fastboot_fail("Unknown chunk type");
3620 return;
3621 }
3622 }
3623
Ajay Dudani0c6927b2011-05-18 11:12:16 -07003624 dprintf(INFO, "Wrote %d blocks, expected to write %d blocks\n",
3625 total_blocks, sparse_header->total_blks);
3626
3627 if(total_blocks != sparse_header->total_blks)
3628 {
3629 fastboot_fail("sparse image write failure");
3630 }
Ajay Dudani5c761132011-04-07 20:19:04 -07003631
3632 fastboot_okay("");
3633 return;
3634}
3635
3636void cmd_flash_mmc(const char *arg, void *data, unsigned sz)
3637{
3638 sparse_header_t *sparse_header;
Ajay Dudanide984792015-03-02 09:57:41 -08003639 meta_header_t *meta_header;
Ajay Dudani5c761132011-04-07 20:19:04 -07003640
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07003641#ifdef SSD_ENABLE
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -08003642 /* 8 Byte Magic + 2048 Byte xml + Encrypted Data */
3643 unsigned int *magic_number = (unsigned int *) data;
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08003644 int ret=0;
3645 uint32 major_version=0;
3646 uint32 minor_version=0;
3647
3648 ret = scm_svc_version(&major_version,&minor_version);
3649 if(!ret)
3650 {
3651 if(major_version >= 2)
3652 {
Amir Samuelovbb65ce02013-05-05 12:20:18 +03003653 if( !strcmp(arg, "ssd") || !strcmp(arg, "tqs") )
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08003654 {
3655 ret = encrypt_scm((uint32 **) &data, &sz);
3656 if (ret != 0) {
3657 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
3658 return;
3659 }
3660
Amir Samuelovbb65ce02013-05-05 12:20:18 +03003661 /* Protect only for SSD */
3662 if (!strcmp(arg, "ssd")) {
3663 ret = scm_protect_keystore((uint32 *) data, sz);
3664 if (ret != 0) {
3665 dprintf(CRITICAL, "ERROR: scm_protect_keystore Failed\n");
3666 return;
3667 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08003668 }
3669 }
3670 else
3671 {
3672 ret = decrypt_scm_v2((uint32 **) &data, &sz);
3673 if(ret != 0)
3674 {
3675 dprintf(CRITICAL,"ERROR: Decryption Failure\n");
3676 return;
3677 }
3678 }
3679 }
3680 else
3681 {
3682 if (magic_number[0] == DECRYPT_MAGIC_0 &&
3683 magic_number[1] == DECRYPT_MAGIC_1)
3684 {
3685 ret = decrypt_scm((uint32 **) &data, &sz);
3686 if (ret != 0) {
3687 dprintf(CRITICAL, "ERROR: Invalid secure image\n");
3688 return;
3689 }
3690 }
3691 else if (magic_number[0] == ENCRYPT_MAGIC_0 &&
3692 magic_number[1] == ENCRYPT_MAGIC_1)
3693 {
3694 ret = encrypt_scm((uint32 **) &data, &sz);
3695 if (ret != 0) {
3696 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
3697 return;
3698 }
3699 }
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07003700 }
3701 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08003702 else
Neeti Desai127b9e02012-03-20 16:11:23 -07003703 {
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08003704 dprintf(CRITICAL,"INVALID SVC Version\n");
3705 return;
Neeti Desai127b9e02012-03-20 16:11:23 -07003706 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08003707#endif /* SSD_ENABLE */
Neeti Desai127b9e02012-03-20 16:11:23 -07003708
Monika Singh292b3e92018-03-17 22:40:23 +05303709#if VERIFIED_BOOT || VERIFIED_BOOT_2
Channagoud Kadabi35297672015-06-13 11:09:49 -07003710 if (target_build_variant_user())
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003711 {
lijuang511a2b52015-08-14 20:50:51 +08003712 /* if device is locked:
3713 * common partition will not allow to be flashed
3714 * critical partition will allow to flash image.
3715 */
3716 if(!device.is_unlocked && !critical_flash_allowed(arg)) {
3717 fastboot_fail("Partition flashing is not allowed");
3718 return;
3719 }
Mayank Grover889be1b2017-09-12 20:12:23 +05303720
lijuang511a2b52015-08-14 20:50:51 +08003721 /* if device critical is locked:
3722 * common partition will allow to be flashed
3723 * critical partition will not allow to flash image.
3724 */
Mayank Grover912eaa62017-10-26 12:08:53 +05303725 if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05303726 !device.is_unlock_critical &&
3727 critical_flash_allowed(arg)) {
3728 fastboot_fail("Critical partition flashing is not allowed");
3729 return;
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003730 }
3731 }
3732#endif
Monika Singh292b3e92018-03-17 22:40:23 +05303733 if (!strncmp(arg, "avb_custom_key", strlen("avb_custom_key"))) {
3734 dprintf(INFO, "flashing avb_custom_key\n");
3735 if (store_userkey(data, sz)) {
3736 fastboot_fail("Flashing avb_custom_key failed");
3737 } else {
3738 fastboot_okay("");
3739 }
3740 return;
3741 }
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003742
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07003743 sparse_header = (sparse_header_t *) data;
Ajay Dudanide984792015-03-02 09:57:41 -08003744 meta_header = (meta_header_t *) data;
3745 if (sparse_header->magic == SPARSE_HEADER_MAGIC)
Ajay Dudani5c761132011-04-07 20:19:04 -07003746 cmd_flash_mmc_sparse_img(arg, data, sz);
Ajay Dudanide984792015-03-02 09:57:41 -08003747 else if (meta_header->magic == META_HEADER_MAGIC)
3748 cmd_flash_meta_img(arg, data, sz);
3749 else
3750 cmd_flash_mmc_img(arg, data, sz);
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07003751
3752#if VERIFIED_BOOT
Mayank Grover912eaa62017-10-26 12:08:53 +05303753 if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05303754 (!strncmp(arg, "system", 6)) &&
3755 !device.verity_mode)
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07003756 // reset dm_verity mode to enforcing
3757 device.verity_mode = 1;
3758 write_device_info(&device);
Parth Dixitddbc7352015-10-18 03:13:31 +05303759#endif
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07003760
Ajay Dudani5c761132011-04-07 20:19:04 -07003761 return;
3762}
3763
Tanya Brokhman84eaaf62015-02-24 21:57:06 +02003764void cmd_updatevol(const char *vol_name, void *data, unsigned sz)
3765{
3766 struct ptentry *sys_ptn;
3767 struct ptable *ptable;
3768
3769 ptable = flash_get_ptable();
3770 if (ptable == NULL) {
3771 fastboot_fail("partition table doesn't exist");
3772 return;
3773 }
3774
3775 sys_ptn = ptable_find(ptable, "system");
3776 if (sys_ptn == NULL) {
3777 fastboot_fail("system partition not found");
3778 return;
3779 }
3780
3781 sz = ROUND_TO_PAGE(sz, page_mask);
3782 if (update_ubi_vol(sys_ptn, vol_name, data, sz))
3783 fastboot_fail("update_ubi_vol failed");
3784 else
3785 fastboot_okay("");
3786}
3787
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003788void cmd_flash_nand(const char *arg, void *data, unsigned sz)
Dima Zavin214cc642009-01-26 11:16:21 -08003789{
3790 struct ptentry *ptn;
3791 struct ptable *ptable;
3792 unsigned extra = 0;
Gaurav Nebhwanid9730712016-05-06 14:28:29 +05303793 uint64_t partition_size = 0;
Vijay Kumar Pendotic6f22a72017-05-19 22:44:34 +05303794 unsigned bytes_to_round_page = 0;
3795 unsigned rounded_size = 0;
Dima Zavin214cc642009-01-26 11:16:21 -08003796
Kishor PK38ed93d2017-04-25 14:19:26 +05303797 if((uintptr_t)data > (UINT_MAX - sz)) {
3798 fastboot_fail("Cannot flash: image header corrupt");
3799 return;
3800 }
3801
Dima Zavin214cc642009-01-26 11:16:21 -08003802 ptable = flash_get_ptable();
3803 if (ptable == NULL) {
3804 fastboot_fail("partition table doesn't exist");
3805 return;
3806 }
3807
3808 ptn = ptable_find(ptable, arg);
3809 if (ptn == NULL) {
Tanya Brokhman84eaaf62015-02-24 21:57:06 +02003810 dprintf(INFO, "unknown partition name (%s). Trying updatevol\n",
3811 arg);
3812 cmd_updatevol(arg, data, sz);
Dima Zavin214cc642009-01-26 11:16:21 -08003813 return;
3814 }
3815
Monika Singh292b3e92018-03-17 22:40:23 +05303816 if (!strncmp(arg, "avb_custom_key", strlen("avb_custom_key"))) {
3817 dprintf(INFO, "flashing avb_custom_key\n");
3818 if (store_userkey(data, sz)) {
3819 fastboot_fail("Flashing avb_custom_key failed");
3820 } else {
3821 fastboot_okay("");
3822 }
3823 return;
3824 }
3825
Dima Zavin214cc642009-01-26 11:16:21 -08003826 if (!strcmp(ptn->name, "boot") || !strcmp(ptn->name, "recovery")) {
Kishor PK38ed93d2017-04-25 14:19:26 +05303827 if((sz > BOOT_MAGIC_SIZE) && (!memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE))) {
3828 dprintf(INFO, "Verified the BOOT_MAGIC in image header \n");
3829 } else {
3830 fastboot_fail("Image is not a boot image");
Dima Zavin214cc642009-01-26 11:16:21 -08003831 return;
3832 }
3833 }
3834
Amol Jadi5c61a952012-05-04 17:05:35 -07003835 if (!strcmp(ptn->name, "system")
Deepa Dinamani13e32c42012-03-12 14:34:17 -07003836 || !strcmp(ptn->name, "userdata")
3837 || !strcmp(ptn->name, "persist")
Sundarajan Srinivasanb063a852013-11-19 14:02:27 -08003838 || !strcmp(ptn->name, "recoveryfs")
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -08003839 || !strcmp(ptn->name, "modem"))
Tanya Brokhman1c94f1a2015-02-15 09:05:03 +02003840 extra = 1;
Kishor PK38ed93d2017-04-25 14:19:26 +05303841 else {
Vijay Kumar Pendotic6f22a72017-05-19 22:44:34 +05303842 rounded_size = ROUNDUP(sz, page_size);
3843 bytes_to_round_page = rounded_size - sz;
3844 if (bytes_to_round_page) {
3845 if (((uintptr_t)data + sz ) > (UINT_MAX - bytes_to_round_page)) {
3846 fastboot_fail("Integer overflow detected");
3847 return;
3848 }
3849 if (((uintptr_t)data + sz + bytes_to_round_page) >
3850 ((uintptr_t)target_get_scratch_address() + target_get_max_flash_size())) {
3851 fastboot_fail("Buffer size is not aligned to page_size");
3852 return;
3853 }
3854 else {
3855 memset(data + sz, 0, bytes_to_round_page);
3856 sz = rounded_size;
3857 }
Kishor PK38ed93d2017-04-25 14:19:26 +05303858 }
Gaurav Nebhwanid9730712016-05-06 14:28:29 +05303859 }
3860
Kishor PK38ed93d2017-04-25 14:19:26 +05303861 /*Checking partition_size for the possible integer overflow */
3862 partition_size = validate_partition_size(ptn);
3863
Gaurav Nebhwanid9730712016-05-06 14:28:29 +05303864 if (sz > partition_size) {
3865 fastboot_fail("Image size too large");
3866 return;
3867 }
3868
Dima Zavin214cc642009-01-26 11:16:21 -08003869 dprintf(INFO, "writing %d bytes to '%s'\n", sz, ptn->name);
Mayank Grover6cde9352017-06-06 18:45:23 +05303870 if ((sz > UBI_EC_HDR_SIZE) &&
3871 (!memcmp((void *)data, UBI_MAGIC, UBI_MAGIC_SIZE))) {
Tanya Brokhman1c94f1a2015-02-15 09:05:03 +02003872 if (flash_ubi_img(ptn, data, sz)) {
3873 fastboot_fail("flash write failure");
3874 return;
3875 }
3876 } else {
3877 if (flash_write(ptn, extra, data, sz)) {
3878 fastboot_fail("flash write failure");
3879 return;
3880 }
Dima Zavin214cc642009-01-26 11:16:21 -08003881 }
3882 dprintf(INFO, "partition '%s' updated\n", ptn->name);
3883 fastboot_okay("");
3884}
3885
Kishor PK38ed93d2017-04-25 14:19:26 +05303886
3887static inline uint64_t validate_partition_size(struct ptentry *ptn)
3888{
3889 if (ptn->length && flash_num_pages_per_blk() && page_size) {
3890 if ((ptn->length < ( UINT_MAX / flash_num_pages_per_blk())) && ((ptn->length * flash_num_pages_per_blk()) < ( UINT_MAX / page_size))) {
3891 return ptn->length * flash_num_pages_per_blk() * page_size;
3892 }
3893 }
3894 return 0;
3895}
3896
3897
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003898void cmd_flash(const char *arg, void *data, unsigned sz)
3899{
3900 if(target_is_emmc_boot())
3901 cmd_flash_mmc(arg, data, sz);
3902 else
3903 cmd_flash_nand(arg, data, sz);
3904}
3905
Dima Zavin214cc642009-01-26 11:16:21 -08003906void cmd_continue(const char *arg, void *data, unsigned sz)
3907{
3908 fastboot_okay("");
Amol Jadi7c4316c2013-10-07 14:19:26 -07003909 fastboot_stop();
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003910
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003911 if (target_is_emmc_boot())
3912 {
lijuanga40d6302015-07-20 20:10:13 +08003913#if FBCON_DISPLAY_MSG
lijuangde34d502016-02-26 16:04:50 +08003914 /* Exit keys' detection thread firstly */
3915 exit_menu_keys_detection();
lijuanga40d6302015-07-20 20:10:13 +08003916#endif
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003917 boot_linux_from_mmc();
3918 }
3919 else
3920 {
3921 boot_linux_from_flash();
3922 }
Dima Zavin214cc642009-01-26 11:16:21 -08003923}
3924
Chandan Uddaraju94183c02010-01-15 15:13:59 -08003925void cmd_reboot(const char *arg, void *data, unsigned sz)
3926{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003927 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08003928 fastboot_okay("");
3929 reboot_device(0);
3930}
3931
Mayank Grover351a75e2017-05-30 20:06:08 +05303932void cmd_set_active(const char *arg, void *data, unsigned sz)
3933{
Mayank Grover6ccc1c92017-07-04 17:36:46 +05303934 char *p, *sp = NULL;
Mayank Grover351a75e2017-05-30 20:06:08 +05303935 unsigned i,current_active_slot;
3936 const char *current_slot_suffix;
3937
3938 if (!partition_multislot_is_supported())
3939 {
3940 fastboot_fail("Command not supported");
3941 return;
3942 }
3943
3944 if (arg)
3945 {
Mayank Grover6ccc1c92017-07-04 17:36:46 +05303946 p = strtok_r((char *)arg, ":", &sp);
Mayank Grover5eb5f692017-07-19 20:16:04 +05303947 if (p)
Mayank Grover351a75e2017-05-30 20:06:08 +05303948 {
3949 current_active_slot = partition_find_active_slot();
3950
3951 /* Check if trying to make curent slot active */
3952 current_slot_suffix = SUFFIX_SLOT(current_active_slot);
Mayank Grover6ccc1c92017-07-04 17:36:46 +05303953 current_slot_suffix = strtok_r((char *)current_slot_suffix,
3954 (char *)suffix_delimiter, &sp);
3955
Mayank Grover5eb5f692017-07-19 20:16:04 +05303956 if (current_slot_suffix &&
3957 !strncmp(p, current_slot_suffix, strlen(current_slot_suffix)))
Mayank Grover351a75e2017-05-30 20:06:08 +05303958 {
3959 fastboot_okay("Slot already set active");
3960 return;
3961 }
3962 else
3963 {
3964 for (i = 0; i < AB_SUPPORTED_SLOTS; i++)
3965 {
3966 current_slot_suffix = SUFFIX_SLOT(i);
Mayank Grover6ccc1c92017-07-04 17:36:46 +05303967 current_slot_suffix = strtok_r((char *)current_slot_suffix,
3968 (char *)suffix_delimiter, &sp);
Mayank Grover5eb5f692017-07-19 20:16:04 +05303969 if (current_slot_suffix &&
3970 !strncmp(p, current_slot_suffix, strlen(current_slot_suffix)))
Mayank Grover351a75e2017-05-30 20:06:08 +05303971 {
3972 partition_switch_slots(current_active_slot, i);
3973 publish_getvar_multislot_vars();
3974 fastboot_okay("");
3975 return;
3976 }
3977 }
3978 }
3979 }
3980 }
3981 fastboot_fail("Invalid slot suffix.");
3982 return;
3983}
3984
Chandan Uddaraju94183c02010-01-15 15:13:59 -08003985void cmd_reboot_bootloader(const char *arg, void *data, unsigned sz)
3986{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003987 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08003988 fastboot_okay("");
3989 reboot_device(FASTBOOT_MODE);
3990}
3991
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003992void cmd_oem_enable_charger_screen(const char *arg, void *data, unsigned size)
3993{
3994 dprintf(INFO, "Enabling charger screen check\n");
3995 device.charger_screen_enabled = 1;
3996 write_device_info(&device);
3997 fastboot_okay("");
3998}
3999
4000void cmd_oem_disable_charger_screen(const char *arg, void *data, unsigned size)
4001{
4002 dprintf(INFO, "Disabling charger screen check\n");
4003 device.charger_screen_enabled = 0;
4004 write_device_info(&device);
4005 fastboot_okay("");
4006}
4007
lijuanga25d8bb2015-09-16 13:11:52 +08004008void cmd_oem_off_mode_charger(const char *arg, void *data, unsigned size)
4009{
4010 char *p = NULL;
4011 const char *delim = " \t\n\r";
Parth Dixit407f15b2016-01-07 14:47:37 +05304012 char *sp;
lijuanga25d8bb2015-09-16 13:11:52 +08004013
4014 if (arg) {
Parth Dixit407f15b2016-01-07 14:47:37 +05304015 p = strtok_r((char *)arg, delim, &sp);
lijuanga25d8bb2015-09-16 13:11:52 +08004016 if (p) {
4017 if (!strncmp(p, "0", 1)) {
4018 device.charger_screen_enabled = 0;
4019 } else if (!strncmp(p, "1", 1)) {
4020 device.charger_screen_enabled = 1;
4021 }
4022 }
4023 }
4024
4025 /* update charger_screen_enabled value for getvar
4026 * command
4027 */
4028 snprintf(charger_screen_enabled, MAX_RSP_SIZE, "%d",
4029 device.charger_screen_enabled);
4030
4031 write_device_info(&device);
4032 fastboot_okay("");
4033}
4034
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05304035void cmd_oem_select_display_panel(const char *arg, void *data, unsigned size)
4036{
4037 dprintf(INFO, "Selecting display panel %s\n", arg);
4038 if (arg)
4039 strlcpy(device.display_panel, arg,
4040 sizeof(device.display_panel));
4041 write_device_info(&device);
4042 fastboot_okay("");
4043}
4044
Shashank Mittal162244e2011-08-08 19:01:25 -07004045void cmd_oem_unlock(const char *arg, void *data, unsigned sz)
4046{
lijuang4ece1e72015-08-14 21:02:36 +08004047 set_device_unlock(UNLOCK, TRUE);
vijay kumarc65876c2015-04-24 13:29:16 +05304048}
4049
4050void cmd_oem_unlock_go(const char *arg, void *data, unsigned sz)
4051{
lijuang4ece1e72015-08-14 21:02:36 +08004052 if(!device.is_unlocked) {
vijay kumarc65876c2015-04-24 13:29:16 +05304053 if(!is_allow_unlock) {
4054 fastboot_fail("oem unlock is not allowed");
4055 return;
4056 }
4057
lijuang4ece1e72015-08-14 21:02:36 +08004058 set_device_unlock_value(UNLOCK, TRUE);
vijay kumarc65876c2015-04-24 13:29:16 +05304059
lijuang4ece1e72015-08-14 21:02:36 +08004060 /* wipe data */
vijay kumarc65876c2015-04-24 13:29:16 +05304061 struct recovery_message msg;
Kishor PK60a68212017-05-08 16:55:57 +05304062 memset(&msg, 0, sizeof(msg));
vijay kumarc65876c2015-04-24 13:29:16 +05304063 snprintf(msg.recovery, sizeof(msg.recovery), "recovery\n--wipe_data");
4064 write_misc(0, &msg, sizeof(msg));
4065
4066 fastboot_okay("");
4067 reboot_device(RECOVERY_MODE);
Shashank Mittald3e54dd2014-08-28 15:24:02 -07004068 }
4069 fastboot_okay("");
4070}
4071
Channagoud Kadabiad259832015-05-29 11:14:17 -07004072static int aboot_frp_unlock(char *pname, void *data, unsigned sz)
4073{
Mayank Grover70f8b0e2017-05-17 11:49:00 +05304074 int ret=1;
4075 bool authentication_success=false;
Channagoud Kadabiad259832015-05-29 11:14:17 -07004076
Mayank Grover70f8b0e2017-05-17 11:49:00 +05304077 /*
4078 Authentication method not implemented.
4079
4080 OEM to implement, authentication system which on successful validataion,
4081 calls write_allow_oem_unlock() with is_allow_unlock.
4082 */
4083#if 0
4084 authentication_success = oem_specific_auth_mthd();
4085#endif
4086
4087 if (authentication_success)
Channagoud Kadabiad259832015-05-29 11:14:17 -07004088 {
Mayank Grover70f8b0e2017-05-17 11:49:00 +05304089 is_allow_unlock = true;
4090 write_allow_oem_unlock(is_allow_unlock);
4091 ret = 0;
Channagoud Kadabiad259832015-05-29 11:14:17 -07004092 }
4093 return ret;
4094}
4095
Shashank Mittald3e54dd2014-08-28 15:24:02 -07004096void cmd_oem_lock(const char *arg, void *data, unsigned sz)
4097{
lijuang4ece1e72015-08-14 21:02:36 +08004098 set_device_unlock(UNLOCK, FALSE);
Shashank Mittal162244e2011-08-08 19:01:25 -07004099}
4100
Shashank Mittala0032282011-08-26 14:50:11 -07004101void cmd_oem_devinfo(const char *arg, void *data, unsigned sz)
4102{
lijuang511a2b52015-08-14 20:50:51 +08004103 char response[MAX_RSP_SIZE];
Ameya Thakur11cf1a62013-08-05 12:44:48 -07004104 snprintf(response, sizeof(response), "\tDevice tampered: %s", (device.is_tampered ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07004105 fastboot_info(response);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07004106 snprintf(response, sizeof(response), "\tDevice unlocked: %s", (device.is_unlocked ? "true" : "false"));
4107 fastboot_info(response);
Monika Singh292b3e92018-03-17 22:40:23 +05304108#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover912eaa62017-10-26 12:08:53 +05304109 if (VB_M <= target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +05304110 {
4111 snprintf(response, sizeof(response), "\tDevice critical unlocked: %s",
4112 (device.is_unlock_critical ? "true" : "false"));
4113 fastboot_info(response);
4114 }
Parth Dixitddbc7352015-10-18 03:13:31 +05304115#endif
Ameya Thakur11cf1a62013-08-05 12:44:48 -07004116 snprintf(response, sizeof(response), "\tCharger screen enabled: %s", (device.charger_screen_enabled ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07004117 fastboot_info(response);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05304118 snprintf(response, sizeof(response), "\tDisplay panel: %s", (device.display_panel));
4119 fastboot_info(response);
Shashank Mittala0032282011-08-26 14:50:11 -07004120 fastboot_okay("");
4121}
4122
lijuang511a2b52015-08-14 20:50:51 +08004123void cmd_flashing_get_unlock_ability(const char *arg, void *data, unsigned sz)
4124{
4125 char response[MAX_RSP_SIZE];
4126 snprintf(response, sizeof(response), "\tget_unlock_ability: %d", is_allow_unlock);
4127 fastboot_info(response);
4128 fastboot_okay("");
4129}
4130
4131void cmd_flashing_lock_critical(const char *arg, void *data, unsigned sz)
4132{
lijuang4ece1e72015-08-14 21:02:36 +08004133 set_device_unlock(UNLOCK_CRITICAL, FALSE);
lijuang511a2b52015-08-14 20:50:51 +08004134}
4135
4136void cmd_flashing_unlock_critical(const char *arg, void *data, unsigned sz)
4137{
lijuang4ece1e72015-08-14 21:02:36 +08004138 set_device_unlock(UNLOCK_CRITICAL, TRUE);
lijuang511a2b52015-08-14 20:50:51 +08004139}
4140
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07004141void cmd_preflash(const char *arg, void *data, unsigned sz)
4142{
4143 fastboot_okay("");
4144}
4145
Mao Flynn7b379f32015-04-20 00:28:30 +08004146static uint8_t logo_header[LOGO_IMG_HEADER_SIZE];
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304147
Mao Flynn7b379f32015-04-20 00:28:30 +08004148int splash_screen_check_header(logo_img_header *header)
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304149{
Mao Flynn7b379f32015-04-20 00:28:30 +08004150 if (memcmp(header->magic, LOGO_IMG_MAGIC, 8))
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304151 return -1;
Mao Flynn7b379f32015-04-20 00:28:30 +08004152 if (header->width == 0 || header->height == 0)
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304153 return -1;
4154 return 0;
4155}
4156
Mao Flynn7b379f32015-04-20 00:28:30 +08004157int splash_screen_flash()
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07004158{
4159 struct ptentry *ptn;
4160 struct ptable *ptable;
Mao Flynn7b379f32015-04-20 00:28:30 +08004161 struct logo_img_header *header;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07004162 struct fbcon_config *fb_display = NULL;
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08004163
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304164 ptable = flash_get_ptable();
4165 if (ptable == NULL) {
Mao Flynn7b379f32015-04-20 00:28:30 +08004166 dprintf(CRITICAL, "ERROR: Partition table not found\n");
4167 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304168 }
Mao Flynn7b379f32015-04-20 00:28:30 +08004169
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304170 ptn = ptable_find(ptable, "splash");
4171 if (ptn == NULL) {
4172 dprintf(CRITICAL, "ERROR: splash Partition not found\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08004173 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304174 }
Mao Flynn7b379f32015-04-20 00:28:30 +08004175 if (flash_read(ptn, 0, (void *)logo_header, LOGO_IMG_HEADER_SIZE)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304176 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08004177 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304178 }
4179
Mao Flynn7b379f32015-04-20 00:28:30 +08004180 header = (struct logo_img_header *)logo_header;
4181 if (splash_screen_check_header(header)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304182 dprintf(CRITICAL, "ERROR: Boot image header invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08004183 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304184 }
4185
4186 fb_display = fbcon_display();
4187 if (fb_display) {
Mao Flynn7b379f32015-04-20 00:28:30 +08004188 if (header->type && (header->blocks != 0)) { // RLE24 compressed data
4189 uint8_t *base = (uint8_t *) fb_display->base + LOGO_IMG_OFFSET;
4190
4191 /* if the logo is full-screen size, remove "fbcon_clear()" */
4192 if ((header->width != fb_display->width)
4193 || (header->height != fb_display->height))
4194 fbcon_clear();
4195
4196 if (flash_read(ptn + LOGO_IMG_HEADER_SIZE, 0,
4197 (uint32_t *)base,
4198 (header->blocks * 512))) {
4199 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
4200 return -1;
4201 }
4202 fbcon_extract_to_screen(header, base);
4203 return 0;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07004204 }
Mao Flynn7b379f32015-04-20 00:28:30 +08004205
Veera Sundaram Sankaranc33416f2015-09-28 10:25:55 -07004206 if ((header->width > fb_display->width) || (header->height > fb_display->height)) {
4207 dprintf(CRITICAL, "Logo config greater than fb config. Fall back default logo\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08004208 return -1;
4209 }
4210
Vineet Bajaj99291ed2014-09-09 12:29:46 +05304211 uint8_t *base = (uint8_t *) fb_display->base;
Sachin Bhayare619e9e42017-05-15 13:10:31 +05304212 uint32_t fb_size = ROUNDUP(fb_display->width *
4213 fb_display->height *
4214 (fb_display->bpp / 8), 4096);
4215 uint32_t splash_size = ((((header->width * header->height *
4216 fb_display->bpp/8) + 511) >> 9) << 9);
4217
4218 if (splash_size > fb_size) {
4219 dprintf(CRITICAL, "ERROR: Splash image size invalid\n");
4220 return -1;
4221 }
4222
Mao Flynn7b379f32015-04-20 00:28:30 +08004223 if (flash_read(ptn + LOGO_IMG_HEADER_SIZE, 0,
4224 (uint32_t *)base,
4225 ((((header->width * header->height * fb_display->bpp/8) + 511) >> 9) << 9))) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304226 fbcon_clear();
Vineet Bajaj99291ed2014-09-09 12:29:46 +05304227 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08004228 return -1;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07004229 }
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304230 }
4231
Mao Flynn7b379f32015-04-20 00:28:30 +08004232 return 0;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304233}
4234
Mao Flynn7b379f32015-04-20 00:28:30 +08004235int splash_screen_mmc()
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304236{
4237 int index = INVALID_PTN;
4238 unsigned long long ptn = 0;
4239 struct fbcon_config *fb_display = NULL;
Mao Flynn7b379f32015-04-20 00:28:30 +08004240 struct logo_img_header *header;
Mao Flynn1893a7f2015-06-03 12:03:36 +08004241 uint32_t blocksize, realsize, readsize;
4242 uint8_t *base;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304243
4244 index = partition_get_index("splash");
4245 if (index == 0) {
4246 dprintf(CRITICAL, "ERROR: splash Partition table not found\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08004247 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304248 }
4249
4250 ptn = partition_get_offset(index);
4251 if (ptn == 0) {
4252 dprintf(CRITICAL, "ERROR: splash Partition invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08004253 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304254 }
4255
Mao Flynn1893a7f2015-06-03 12:03:36 +08004256 mmc_set_lun(partition_get_lun(index));
4257
4258 blocksize = mmc_get_device_blocksize();
4259 if (blocksize == 0) {
4260 dprintf(CRITICAL, "ERROR:splash Partition invalid blocksize\n");
4261 return -1;
4262 }
4263
4264 fb_display = fbcon_display();
Channagoud Kadabidaf10a62015-07-22 11:51:55 -07004265 if (!fb_display)
4266 {
4267 dprintf(CRITICAL, "ERROR: fb config is not allocated\n");
4268 return -1;
4269 }
4270
Mao Flynn1893a7f2015-06-03 12:03:36 +08004271 base = (uint8_t *) fb_display->base;
4272
4273 if (mmc_read(ptn, (uint32_t *)(base + LOGO_IMG_OFFSET), blocksize)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304274 dprintf(CRITICAL, "ERROR: Cannot read splash image header\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08004275 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304276 }
4277
Mao Flynn1893a7f2015-06-03 12:03:36 +08004278 header = (struct logo_img_header *)(base + LOGO_IMG_OFFSET);
Mao Flynn7b379f32015-04-20 00:28:30 +08004279 if (splash_screen_check_header(header)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304280 dprintf(CRITICAL, "ERROR: Splash image header invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08004281 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304282 }
4283
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304284 if (fb_display) {
Mao Flynn1893a7f2015-06-03 12:03:36 +08004285 if (header->type && (header->blocks != 0)) { /* 1 RLE24 compressed data */
4286 base += LOGO_IMG_OFFSET;
Mao Flynn7b379f32015-04-20 00:28:30 +08004287
Mao Flynn1893a7f2015-06-03 12:03:36 +08004288 realsize = header->blocks * 512;
4289 readsize = ROUNDUP((realsize + LOGO_IMG_HEADER_SIZE), blocksize) - blocksize;
4290
4291 /* if the logo is not full-screen size, clean screen */
Mao Flynn7b379f32015-04-20 00:28:30 +08004292 if ((header->width != fb_display->width)
4293 || (header->height != fb_display->height))
4294 fbcon_clear();
4295
Sachin Bhayare619e9e42017-05-15 13:10:31 +05304296 uint32_t fb_size = ROUNDUP(fb_display->width *
4297 fb_display->height *
4298 (fb_display->bpp / 8), 4096);
4299
4300 if (readsize > fb_size) {
4301 dprintf(CRITICAL, "ERROR: Splash image size invalid\n");
4302 return -1;
4303 }
4304
Mao Flynn1893a7f2015-06-03 12:03:36 +08004305 if (mmc_read(ptn + blocksize, (uint32_t *)(base + blocksize), readsize)) {
Mao Flynn7b379f32015-04-20 00:28:30 +08004306 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
4307 return -1;
4308 }
Mao Flynn7b379f32015-04-20 00:28:30 +08004309
Mao Flynn1893a7f2015-06-03 12:03:36 +08004310 fbcon_extract_to_screen(header, (base + LOGO_IMG_HEADER_SIZE));
4311 } else { /* 2 Raw BGR data */
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304312
Veera Sundaram Sankaranc33416f2015-09-28 10:25:55 -07004313 if ((header->width > fb_display->width) || (header->height > fb_display->height)) {
4314 dprintf(CRITICAL, "Logo config greater than fb config. Fall back default logo\n");
Mao Flynn1893a7f2015-06-03 12:03:36 +08004315 return -1;
4316 }
4317
4318 realsize = header->width * header->height * fb_display->bpp / 8;
4319 readsize = ROUNDUP((realsize + LOGO_IMG_HEADER_SIZE), blocksize) - blocksize;
4320
4321 if (blocksize == LOGO_IMG_HEADER_SIZE) { /* read the content directly */
4322 if (mmc_read((ptn + LOGO_IMG_HEADER_SIZE), (uint32_t *)base, readsize)) {
4323 fbcon_clear();
4324 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
4325 return -1;
4326 }
4327 } else {
4328 if (mmc_read(ptn + blocksize ,
4329 (uint32_t *)(base + LOGO_IMG_OFFSET + blocksize), readsize)) {
4330 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
4331 return -1;
4332 }
4333 memmove(base, (base + LOGO_IMG_OFFSET + LOGO_IMG_HEADER_SIZE), realsize);
4334 }
4335 }
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304336 }
4337
Mao Flynn7b379f32015-04-20 00:28:30 +08004338 return 0;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304339}
4340
Mao Flynn7b379f32015-04-20 00:28:30 +08004341int fetch_image_from_partition()
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304342{
4343 if (target_is_emmc_boot()) {
4344 return splash_screen_mmc();
4345 } else {
4346 return splash_screen_flash();
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07004347 }
4348}
4349
Mayank Grover351a75e2017-05-30 20:06:08 +05304350void publish_getvar_multislot_vars()
4351{
4352 int i,count;
4353 static bool published = false;
4354 static char slot_count[MAX_RSP_SIZE];
4355 static struct ab_slot_info slot_info[AB_SUPPORTED_SLOTS];
4356 static char active_slot_suffix[MAX_RSP_SIZE];
4357 static char has_slot_pname[NUM_PARTITIONS][MAX_GET_VAR_NAME_SIZE];
4358 static char has_slot_reply[NUM_PARTITIONS][MAX_RSP_SIZE];
4359 const char *tmp;
4360 char tmpbuff[MAX_GET_VAR_NAME_SIZE];
Mayank Grover2bd84bb2017-06-20 11:06:07 +05304361 signed active_slt;
Mayank Grover351a75e2017-05-30 20:06:08 +05304362
4363 if (!published)
4364 {
4365 /* Update slot meta info */
4366 count = partition_fill_partition_meta(has_slot_pname, has_slot_reply,
4367 partition_get_partition_count());
4368 for(i=0; i<count; i++)
4369 {
4370 memset(tmpbuff, 0, MAX_GET_VAR_NAME_SIZE);
Mayank Grover2bd84bb2017-06-20 11:06:07 +05304371 snprintf(tmpbuff, MAX_GET_VAR_NAME_SIZE,"has-slot:%s",
4372 has_slot_pname[i]);
4373 strlcpy(has_slot_pname[i], tmpbuff, MAX_GET_VAR_NAME_SIZE);
Mayank Grover351a75e2017-05-30 20:06:08 +05304374 fastboot_publish(has_slot_pname[i], has_slot_reply[i]);
4375 }
4376
4377 for (i=0; i<AB_SUPPORTED_SLOTS; i++)
4378 {
4379 tmp = SUFFIX_SLOT(i);
Mayank Grover7b8a8f82017-10-27 13:07:59 +05304380 tmp++; // to remove "_" from slot_suffix.
Mayank Grover2bd84bb2017-06-20 11:06:07 +05304381 snprintf(slot_info[i].slot_is_unbootable, sizeof(slot_info[i].slot_is_unbootable),
4382 "slot-unbootable:%s", tmp);
4383 snprintf(slot_info[i].slot_is_active, sizeof(slot_info[i].slot_is_active),
4384 "slot-active:%s", tmp);
4385 snprintf(slot_info[i].slot_is_succesful, sizeof(slot_info[i].slot_is_succesful),
4386 "slot-success:%s", tmp);
4387 snprintf(slot_info[i].slot_retry_count, sizeof(slot_info[i].slot_retry_count),
4388 "slot-retry-count:%s", tmp);
Mayank Grover351a75e2017-05-30 20:06:08 +05304389 fastboot_publish(slot_info[i].slot_is_unbootable,
4390 slot_info[i].slot_is_unbootable_rsp);
4391 fastboot_publish(slot_info[i].slot_is_active,
4392 slot_info[i].slot_is_active_rsp);
4393 fastboot_publish(slot_info[i].slot_is_succesful,
4394 slot_info[i].slot_is_succesful_rsp);
4395 fastboot_publish(slot_info[i].slot_retry_count,
4396 slot_info[i].slot_retry_count_rsp);
4397 }
4398 fastboot_publish("current-slot", active_slot_suffix);
4399 snprintf(slot_count, sizeof(slot_count),"%d", AB_SUPPORTED_SLOTS);
4400 fastboot_publish("slot-count", slot_count);
4401 published = true;
4402 }
4403
Mayank Grover2bd84bb2017-06-20 11:06:07 +05304404 active_slt = partition_find_active_slot();
4405 if (active_slt != INVALID)
Mayank Grover7b8a8f82017-10-27 13:07:59 +05304406 {
4407 tmp = SUFFIX_SLOT(active_slt);
4408 tmp++; // to remove "_" from slot_suffix.
4409 snprintf(active_slot_suffix, sizeof(active_slot_suffix), "%s", tmp);
4410 }
Mayank Grover2bd84bb2017-06-20 11:06:07 +05304411 else
4412 strlcpy(active_slot_suffix, "INVALID", sizeof(active_slot_suffix));
4413
Mayank Grover351a75e2017-05-30 20:06:08 +05304414 /* Update partition meta information */
4415 partition_fill_slot_meta(slot_info);
4416 return;
4417}
4418
lijuang4ece1e72015-08-14 21:02:36 +08004419void get_product_name(unsigned char *buf)
4420{
4421 snprintf((char*)buf, MAX_RSP_SIZE, "%s", TARGET(BOARD));
4422 return;
4423}
4424
Mayank Grover7b8a8f82017-10-27 13:07:59 +05304425#if PRODUCT_IOT
4426void get_bootloader_version_iot(unsigned char *buf)
4427{
4428 if (buf != NULL)
4429 {
4430 strlcpy(buf, TARGET(BOARD), MAX_VERSION_LEN);
4431 strlcat(buf, "-", MAX_VERSION_LEN);
4432 strlcat(buf, PRODUCT_IOT_VERSION, MAX_VERSION_LEN);
4433 }
4434 return;
4435}
4436#endif
4437
lijuang4ece1e72015-08-14 21:02:36 +08004438void get_bootloader_version(unsigned char *buf)
4439{
4440 snprintf((char*)buf, MAX_RSP_SIZE, "%s", device.bootloader_version);
4441 return;
4442}
4443
4444void get_baseband_version(unsigned char *buf)
4445{
4446 snprintf((char*)buf, MAX_RSP_SIZE, "%s", device.radio_version);
4447 return;
4448}
4449
4450bool is_device_locked()
4451{
4452 return device.is_unlocked ? false:true;
4453}
4454
Amol Jadi5edf3552013-07-23 14:15:34 -07004455/* register commands and variables for fastboot */
4456void aboot_fastboot_register_commands(void)
4457{
Sridhar Parasurame94e8152014-10-24 14:06:03 -07004458 int i;
lijuangf16461c2015-08-03 17:09:34 +08004459 char hw_platform_buf[MAX_RSP_SIZE];
Amol Jadi5edf3552013-07-23 14:15:34 -07004460
Sridhar Parasurame94e8152014-10-24 14:06:03 -07004461 struct fastboot_cmd_desc cmd_list[] = {
lijuang511a2b52015-08-14 20:50:51 +08004462 /* By default the enabled list is empty. */
4463 {"", NULL},
4464 /* move commands enclosed within the below ifndef to here
4465 * if they need to be enabled in user build.
4466 */
Sridhar Parasurame94e8152014-10-24 14:06:03 -07004467#ifndef DISABLE_FASTBOOT_CMDS
lijuang511a2b52015-08-14 20:50:51 +08004468 /* Register the following commands only for non-user builds */
4469 {"flash:", cmd_flash},
4470 {"erase:", cmd_erase},
4471 {"boot", cmd_boot},
4472 {"continue", cmd_continue},
4473 {"reboot", cmd_reboot},
4474 {"reboot-bootloader", cmd_reboot_bootloader},
4475 {"oem unlock", cmd_oem_unlock},
4476 {"oem unlock-go", cmd_oem_unlock_go},
4477 {"oem lock", cmd_oem_lock},
4478 {"flashing unlock", cmd_oem_unlock},
4479 {"flashing lock", cmd_oem_lock},
4480 {"flashing lock_critical", cmd_flashing_lock_critical},
4481 {"flashing unlock_critical", cmd_flashing_unlock_critical},
4482 {"flashing get_unlock_ability", cmd_flashing_get_unlock_ability},
4483 {"oem device-info", cmd_oem_devinfo},
4484 {"preflash", cmd_preflash},
4485 {"oem enable-charger-screen", cmd_oem_enable_charger_screen},
4486 {"oem disable-charger-screen", cmd_oem_disable_charger_screen},
lijuanga25d8bb2015-09-16 13:11:52 +08004487 {"oem off-mode-charge", cmd_oem_off_mode_charger},
lijuang511a2b52015-08-14 20:50:51 +08004488 {"oem select-display-panel", cmd_oem_select_display_panel},
Mayank Grover6ccc1c92017-07-04 17:36:46 +05304489 {"set_active",cmd_set_active},
Channagoud Kadabi736c4962015-08-21 11:56:52 -07004490#if UNITTEST_FW_SUPPORT
Channagoud Kadabid5ddf482015-09-28 10:31:35 -07004491 {"oem run-tests", cmd_oem_runtests},
Channagoud Kadabi736c4962015-08-21 11:56:52 -07004492#endif
Sridhar Parasurame94e8152014-10-24 14:06:03 -07004493#endif
lijuang511a2b52015-08-14 20:50:51 +08004494 };
Sridhar Parasurame94e8152014-10-24 14:06:03 -07004495
4496 int fastboot_cmds_count = sizeof(cmd_list)/sizeof(cmd_list[0]);
4497 for (i = 1; i < fastboot_cmds_count; i++)
4498 fastboot_register(cmd_list[i].name,cmd_list[i].cb);
4499
Amol Jadi5edf3552013-07-23 14:15:34 -07004500 /* publish variables and their values */
4501 fastboot_publish("product", TARGET(BOARD));
4502 fastboot_publish("kernel", "lk");
4503 fastboot_publish("serialno", sn_buf);
4504
4505 /*
4506 * partition info is supported only for emmc partitions
4507 * Calling this for NAND prints some error messages which
4508 * is harmless but misleading. Avoid calling this for NAND
4509 * devices.
4510 */
4511 if (target_is_emmc_boot())
Mayank Grover7b8a8f82017-10-27 13:07:59 +05304512 publish_getvar_partition_info(part_info, partition_get_partition_count());
Amol Jadi5edf3552013-07-23 14:15:34 -07004513
Mayank Grover351a75e2017-05-30 20:06:08 +05304514 if (partition_multislot_is_supported())
4515 publish_getvar_multislot_vars();
4516
Amol Jadi5edf3552013-07-23 14:15:34 -07004517 /* Max download size supported */
Ameya Thakur11cf1a62013-08-05 12:44:48 -07004518 snprintf(max_download_size, MAX_RSP_SIZE, "\t0x%x",
4519 target_get_max_flash_size());
Amol Jadi5edf3552013-07-23 14:15:34 -07004520 fastboot_publish("max-download-size", (const char *) max_download_size);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07004521 /* Is the charger screen check enabled */
4522 snprintf(charger_screen_enabled, MAX_RSP_SIZE, "%d",
4523 device.charger_screen_enabled);
4524 fastboot_publish("charger-screen-enabled",
4525 (const char *) charger_screen_enabled);
lijuanga25d8bb2015-09-16 13:11:52 +08004526 fastboot_publish("off-mode-charge", (const char *) charger_screen_enabled);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05304527 snprintf(panel_display_mode, MAX_RSP_SIZE, "%s",
4528 device.display_panel);
4529 fastboot_publish("display-panel",
4530 (const char *) panel_display_mode);
Mayank Grover7b8a8f82017-10-27 13:07:59 +05304531#if PRODUCT_IOT
4532 get_bootloader_version_iot(&bootloader_version_string);
4533 fastboot_publish("version-bootloader", (const char *) bootloader_version_string);
4534
4535 /* Version baseband is n/a for apq iot devices */
4536 fastboot_publish("version-baseband", "N/A");
4537
4538 /* IOT targets support only mmc target */
4539 snprintf(block_size_string, MAX_RSP_SIZE, "0x%x", mmc_get_device_blocksize());
4540 fastboot_publish("erase-block-size", (const char *) block_size_string);
4541 fastboot_publish("logical-block-size", (const char *) block_size_string);
4542#else
Channagoud Kadabiaafbbfc2015-03-04 17:52:56 -08004543 fastboot_publish("version-bootloader", (const char *) device.bootloader_version);
4544 fastboot_publish("version-baseband", (const char *) device.radio_version);
Mayank Grover7b8a8f82017-10-27 13:07:59 +05304545#endif
lijuangf16461c2015-08-03 17:09:34 +08004546 fastboot_publish("secure", is_secure_boot_enable()? "yes":"no");
Mayank Grover7b8a8f82017-10-27 13:07:59 +05304547 fastboot_publish("unlocked", device.is_unlocked ? "yes":"no");
lijuangf16461c2015-08-03 17:09:34 +08004548 smem_get_hw_platform_name((unsigned char *) hw_platform_buf, sizeof(hw_platform_buf));
4549 snprintf(get_variant, MAX_RSP_SIZE, "%s %s", hw_platform_buf,
4550 target_is_emmc_boot()? "eMMC":"UFS");
4551 fastboot_publish("variant", (const char *) get_variant);
lijuang65c5a822015-08-29 16:35:36 +08004552#if CHECK_BAT_VOLTAGE
lijuang102dfa92015-10-09 18:31:03 +08004553 update_battery_status();
lijuang65c5a822015-08-29 16:35:36 +08004554 fastboot_publish("battery-voltage", (const char *) battery_voltage);
lijuang102dfa92015-10-09 18:31:03 +08004555 fastboot_publish("battery-soc-ok", (const char *) battery_soc_ok);
lijuang65c5a822015-08-29 16:35:36 +08004556#endif
Amol Jadi5edf3552013-07-23 14:15:34 -07004557}
4558
Brian Swetland9c4c0752009-01-25 16:23:50 -08004559void aboot_init(const struct app_descriptor *app)
4560{
Shashank Mittal4f99a882010-02-01 13:58:50 -08004561 unsigned reboot_mode = 0;
Mayank Grover351a75e2017-05-30 20:06:08 +05304562 int boot_err_type = 0;
4563 int boot_slot = INVALID;
Chandan Uddarajubedca152010-06-02 23:05:15 -07004564
lijuang39831732016-01-08 17:49:02 +08004565 /* Initialise wdog to catch early lk crashes */
4566#if WDOG_SUPPORT
4567 msm_wdog_init();
4568#endif
4569
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07004570 /* Setup page size information for nv storage */
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004571 if (target_is_emmc_boot())
4572 {
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07004573 page_size = mmc_page_size();
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004574 page_mask = page_size - 1;
Gaurav Nebhwani4d2389c2016-03-17 21:10:05 +05304575 mmc_blocksize = mmc_get_device_blocksize();
4576 mmc_blocksize_mask = mmc_blocksize - 1;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004577 }
4578 else
4579 {
4580 page_size = flash_page_size();
4581 page_mask = page_size - 1;
4582 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07004583 ASSERT((MEMBASE + MEMSIZE) > MEMBASE);
4584
Ameya Thakur11cf1a62013-08-05 12:44:48 -07004585 read_device_info(&device);
vijay kumarc65876c2015-04-24 13:29:16 +05304586 read_allow_oem_unlock(&device);
Shashank Mittal162244e2011-08-08 19:01:25 -07004587
Mayank Grover351a75e2017-05-30 20:06:08 +05304588 /* Detect multi-slot support */
4589 if (partition_multislot_is_supported())
4590 {
4591 boot_slot = partition_find_active_slot();
4592 if (boot_slot == INVALID)
4593 {
4594 boot_into_fastboot = true;
4595 dprintf(INFO, "Active Slot: (INVALID)\n");
4596 }
4597 else
4598 {
4599 /* Setting the state of system to boot active slot */
4600 partition_mark_active_slot(boot_slot);
4601 dprintf(INFO, "Active Slot: (%s)\n", SUFFIX_SLOT(boot_slot));
4602 }
4603 }
4604
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08004605 /* Display splash screen if enabled */
4606#if DISPLAY_SPLASH_SCREEN
lijuang99c02d82015-02-13 19:04:34 +08004607#if NO_ALARM_DISPLAY
4608 if (!check_alarm_boot()) {
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08004609#endif
lijuang99c02d82015-02-13 19:04:34 +08004610 dprintf(SPEW, "Display Init: Start\n");
Ajay Singh Parmara7865a82015-04-16 21:27:52 -07004611#if DISPLAY_HDMI_PRIMARY
4612 if (!strlen(device.display_panel))
4613 strlcpy(device.display_panel, DISPLAY_PANEL_HDMI,
4614 sizeof(device.display_panel));
4615#endif
Channagoud Kadabi90869ce2015-04-27 11:15:14 -07004616#if ENABLE_WBC
Channagoud Kadabid801ac72015-08-26 11:21:51 -07004617 /* Wait if the display shutdown is in progress */
4618 while(pm_app_display_shutdown_in_prgs());
Channagoud Kadabi90869ce2015-04-27 11:15:14 -07004619 if (!pm_appsbl_display_init_done())
4620 target_display_init(device.display_panel);
4621 else
4622 display_image_on_screen();
4623#else
lijuang99c02d82015-02-13 19:04:34 +08004624 target_display_init(device.display_panel);
Channagoud Kadabi90869ce2015-04-27 11:15:14 -07004625#endif
lijuang99c02d82015-02-13 19:04:34 +08004626 dprintf(SPEW, "Display Init: Done\n");
4627#if NO_ALARM_DISPLAY
4628 }
4629#endif
4630#endif
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08004631
Greg Griscod6250552011-06-29 14:40:23 -07004632 target_serialno((unsigned char *) sn_buf);
Ajay Dudanib06c05f2011-05-12 14:46:10 -07004633 dprintf(SPEW,"serial number: %s\n",sn_buf);
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -08004634
Dhaval Patel223ec952013-07-18 14:49:44 -07004635 memset(display_panel_buf, '\0', MAX_PANEL_BUF_SIZE);
4636
Matthew Qindefd5562014-07-11 18:02:40 +08004637 /*
4638 * Check power off reason if user force reset,
4639 * if yes phone will do normal boot.
4640 */
4641 if (is_user_force_reset())
4642 goto normal_boot;
4643
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07004644 /* Check if we should do something other than booting up */
Ameya Thakur0b9c2442013-05-10 13:22:56 -07004645 if (keys_get_state(KEY_VOLUMEUP) && keys_get_state(KEY_VOLUMEDOWN))
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07004646 {
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03004647 dprintf(ALWAYS,"dload mode key sequence detected\n");
lijuang395b5e62015-11-19 17:39:44 +08004648 reboot_device(EMERGENCY_DLOAD);
4649 dprintf(CRITICAL,"Failed to reboot into dload mode\n");
4650
Ameya Thakur0b9c2442013-05-10 13:22:56 -07004651 boot_into_fastboot = true;
4652 }
4653 if (!boot_into_fastboot)
4654 {
4655 if (keys_get_state(KEY_HOME) || keys_get_state(KEY_VOLUMEUP))
4656 boot_into_recovery = 1;
4657 if (!boot_into_recovery &&
4658 (keys_get_state(KEY_BACK) || keys_get_state(KEY_VOLUMEDOWN)))
Pavel Nedev5d91d412013-04-29 11:34:24 +03004659 boot_into_fastboot = true;
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07004660 }
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004661 #if NO_KEYPAD_DRIVER
Kinson Chik0b1c8162011-08-31 16:31:57 -07004662 if (fastboot_trigger())
Pavel Nedev5d91d412013-04-29 11:34:24 +03004663 boot_into_fastboot = true;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004664 #endif
Chandan Uddarajubedca152010-06-02 23:05:15 -07004665
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07004666#if USE_PON_REBOOT_REG
4667 reboot_mode = check_hard_reboot_mode();
4668#else
Ajay Dudani77421292010-10-27 19:34:06 -07004669 reboot_mode = check_reboot_mode();
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07004670#endif
4671 if (reboot_mode == RECOVERY_MODE)
4672 {
Ajay Dudani77421292010-10-27 19:34:06 -07004673 boot_into_recovery = 1;
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07004674 }
4675 else if(reboot_mode == FASTBOOT_MODE)
4676 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03004677 boot_into_fastboot = true;
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07004678 }
4679 else if(reboot_mode == ALARM_BOOT)
4680 {
Matthew Qind886f3c2014-01-17 16:52:01 +08004681 boot_reason_alarm = true;
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07004682 }
Parth Dixit207573a2015-10-27 17:26:21 +05304683#if VERIFIED_BOOT
Mayank Grover912eaa62017-10-26 12:08:53 +05304684 else if (VB_M <= target_get_vb_version())
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07004685 {
Mayank Grover889be1b2017-09-12 20:12:23 +05304686 if (reboot_mode == DM_VERITY_ENFORCING)
4687 {
4688 device.verity_mode = 1;
4689 write_device_info(&device);
4690 }
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07004691#if ENABLE_VB_ATTEST
Mayank Grover889be1b2017-09-12 20:12:23 +05304692 else if (reboot_mode == DM_VERITY_EIO)
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07004693#else
Mayank Grover889be1b2017-09-12 20:12:23 +05304694 else if (reboot_mode == DM_VERITY_LOGGING)
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07004695#endif
Mayank Grover889be1b2017-09-12 20:12:23 +05304696 {
4697 device.verity_mode = 0;
4698 write_device_info(&device);
4699 }
4700 else if (reboot_mode == DM_VERITY_KEYSCLEAR)
4701 {
4702 if(send_delete_keys_to_tz())
4703 ASSERT(0);
4704 }
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07004705 }
Parth Dixitddbc7352015-10-18 03:13:31 +05304706#endif
Ajay Dudani77421292010-10-27 19:34:06 -07004707
Matthew Qindefd5562014-07-11 18:02:40 +08004708normal_boot:
Pavel Nedev5d91d412013-04-29 11:34:24 +03004709 if (!boot_into_fastboot)
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004710 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03004711 if (target_is_emmc_boot())
Shashank Mittala0032282011-08-26 14:50:11 -07004712 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03004713 if(emmc_recovery_init())
4714 dprintf(ALWAYS,"error in emmc_recovery_init\n");
4715 if(target_use_signed_kernel())
Shashank Mittala0032282011-08-26 14:50:11 -07004716 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03004717 if((device.is_unlocked) || (device.is_tampered))
4718 {
4719 #ifdef TZ_TAMPER_FUSE
4720 set_tamper_fuse_cmd();
4721 #endif
4722 #if USE_PCOM_SECBOOT
4723 set_tamper_flag(device.is_tampered);
4724 #endif
4725 }
Shashank Mittala0032282011-08-26 14:50:11 -07004726 }
Amit Blay6281ebc2015-01-11 14:44:08 +02004727
Mayank Grover351a75e2017-05-30 20:06:08 +05304728retry_boot:
4729 /* Trying to boot active partition */
4730 if (partition_multislot_is_supported())
4731 {
4732 boot_slot = partition_find_boot_slot();
4733 partition_mark_active_slot(boot_slot);
4734 if (boot_slot == INVALID)
4735 goto fastboot;
4736 }
4737
4738 boot_err_type = boot_linux_from_mmc();
4739 switch (boot_err_type)
4740 {
4741 case ERR_INVALID_PAGE_SIZE:
4742 case ERR_DT_PARSE:
4743 case ERR_ABOOT_ADDR_OVERLAP:
Mayank Grover10cabbe2017-10-30 19:11:05 +05304744 case ERR_INVALID_BOOT_MAGIC:
Mayank Grover351a75e2017-05-30 20:06:08 +05304745 if(partition_multislot_is_supported())
Mayank Grover10cabbe2017-10-30 19:11:05 +05304746 {
4747 /*
4748 * Deactivate current slot, as it failed to
4749 * boot, and retry next slot.
4750 */
4751 partition_deactivate_slot(boot_slot);
Mayank Grover351a75e2017-05-30 20:06:08 +05304752 goto retry_boot;
Mayank Grover10cabbe2017-10-30 19:11:05 +05304753 }
Mayank Grover351a75e2017-05-30 20:06:08 +05304754 else
4755 break;
Mayank Grover351a75e2017-05-30 20:06:08 +05304756 default:
4757 break;
4758 /* going to fastboot menu */
4759 }
Shashank Mittala0032282011-08-26 14:50:11 -07004760 }
Pavel Nedev5d91d412013-04-29 11:34:24 +03004761 else
4762 {
4763 recovery_init();
4764 #if USE_PCOM_SECBOOT
4765 if((device.is_unlocked) || (device.is_tampered))
4766 set_tamper_flag(device.is_tampered);
4767 #endif
4768 boot_linux_from_flash();
4769 }
4770 dprintf(CRITICAL, "ERROR: Could not do normal boot. Reverting "
4771 "to fastboot mode.\n");
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004772 }
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07004773
Mayank Grover351a75e2017-05-30 20:06:08 +05304774fastboot:
Amol Jadi5edf3552013-07-23 14:15:34 -07004775 /* We are here means regular boot did not happen. Start fastboot. */
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07004776
Amol Jadi5edf3552013-07-23 14:15:34 -07004777 /* register aboot specific fastboot commands */
4778 aboot_fastboot_register_commands();
Amol Jadi57abe4c2011-05-24 15:47:27 -07004779
Amol Jadi5edf3552013-07-23 14:15:34 -07004780 /* dump partition table for debug info */
Kinson Chikf1a43512011-07-14 11:28:39 -07004781 partition_dump();
Amol Jadi5edf3552013-07-23 14:15:34 -07004782
4783 /* initialize and start fastboot */
4784 fastboot_init(target_get_scratch_address(), target_get_max_flash_size());
lijuang4ece1e72015-08-14 21:02:36 +08004785#if FBCON_DISPLAY_MSG
lijuang9a7d3b92015-11-30 14:41:24 +08004786 display_fastboot_menu();
lijuang4ece1e72015-08-14 21:02:36 +08004787#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08004788}
4789
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07004790uint32_t get_page_size()
4791{
4792 return page_size;
4793}
4794
Amir Samuelov57a6fa22013-06-05 16:36:43 +03004795/*
4796 * Calculated and save hash (SHA256) for non-signed boot image.
4797 *
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07004798 * @param image_addr - Boot image address
4799 * @param image_size - Size of the boot image
Amir Samuelov57a6fa22013-06-05 16:36:43 +03004800 *
4801 * @return int - 0 on success, negative value on failure.
4802 */
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07004803static int aboot_save_boot_hash_mmc(uint32_t image_addr, uint32_t image_size)
Amir Samuelov57a6fa22013-06-05 16:36:43 +03004804{
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07004805 unsigned int digest[8];
4806#if IMAGE_VERIF_ALGO_SHA1
4807 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA1;
4808#else
4809 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA256;
4810#endif
Amir Samuelov57a6fa22013-06-05 16:36:43 +03004811
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07004812 target_crypto_init_params();
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -08004813 hash_find((unsigned char *) image_addr, image_size, (unsigned char *)&digest, auth_algo);
Amir Samuelov57a6fa22013-06-05 16:36:43 +03004814
4815 save_kernel_hash_cmd(digest);
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07004816 dprintf(INFO, "aboot_save_boot_hash_mmc: imagesize_actual size %d bytes.\n", (int) image_size);
Amir Samuelov57a6fa22013-06-05 16:36:43 +03004817
4818 return 0;
4819}
4820
Mayank Grover351a75e2017-05-30 20:06:08 +05304821
Brian Swetland9c4c0752009-01-25 16:23:50 -08004822APP_START(aboot)
4823 .init = aboot_init,
4824APP_END