blob: 5024edceee61c9214da9439c196f45953be532dc [file] [log] [blame]
Brian Swetland9c4c0752009-01-25 16:23:50 -08001/*
2 * Copyright (c) 2009, Google Inc.
3 * All rights reserved.
4 *
Mayank Grover11ff9692018-01-11 11:54:49 +05305 * Copyright (c) 2009-2018, The Linux Foundation. All rights reserved.
Brian Swetland9c4c0752009-01-25 16:23:50 -08006 *
Chandan Uddaraju5fa471a2009-12-02 17:31:34 -08007 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070014 * * Neither the name of The Linux Foundation nor
Chandan Uddaraju5fa471a2009-12-02 17:31:34 -080015 * the names of its contributors may be used to endorse or promote
16 * products derived from this software without specific prior written
17 * permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22 * NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 *
Brian Swetland9c4c0752009-01-25 16:23:50 -080031 */
32
33#include <app.h>
34#include <debug.h>
35#include <arch/arm.h>
Brian Swetland9c4c0752009-01-25 16:23:50 -080036#include <string.h>
Channagoud Kadabi132ff552013-04-19 14:34:44 -070037#include <stdlib.h>
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -070038#include <limits.h>
Brian Swetland9c4c0752009-01-25 16:23:50 -080039#include <kernel/thread.h>
40#include <arch/ops.h>
41
Dima Zavin214cc642009-01-26 11:16:21 -080042#include <dev/flash.h>
Tanya Brokhman1c94f1a2015-02-15 09:05:03 +020043#include <dev/flash-ubi.h>
Dima Zavin214cc642009-01-26 11:16:21 -080044#include <lib/ptable.h>
Dima Zavinb4283602009-01-26 16:36:57 -080045#include <dev/keys.h>
Shashank Mittal4f99a882010-02-01 13:58:50 -080046#include <dev/fbcon.h>
Ajay Dudanid04110c2011-01-17 23:55:07 -080047#include <baseband.h>
Greg Griscod6250552011-06-29 14:40:23 -070048#include <target.h>
49#include <mmc.h>
Kinson Chikf1a43512011-07-14 11:28:39 -070050#include <partition_parser.h>
Mayank Grover351a75e2017-05-30 20:06:08 +053051#include <ab_partition_parser.h>
Monika Singh292b3e92018-03-17 22:40:23 +053052#include <verifiedboot.h>
Greg Griscod6250552011-06-29 14:40:23 -070053#include <platform.h>
Shashank Mittalcd98d472011-08-02 14:29:24 -070054#include <crypto_hash.h>
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070055#include <malloc.h>
Amol Jadi492d5a52013-03-15 16:12:34 -070056#include <boot_stats.h>
Amir Samuelov57a6fa22013-06-05 16:36:43 +030057#include <sha.h>
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -070058#include <platform/iomap.h>
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -070059#include <boot_device.h>
Shashank Mittald3e54dd2014-08-28 15:24:02 -070060#include <boot_verifier.h>
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +053061#include <image_verify.h>
Matthew Qinbb7923d2015-02-09 10:56:09 +080062#include <decompress.h>
Unnati Gandhi17b3bfc2015-05-11 12:58:16 +053063#include <platform/timer.h>
lijuang511a2b52015-08-14 20:50:51 +080064#include <sys/types.h>
Channagoud Kadabi036c6052015-02-09 15:19:59 -080065#if USE_RPMB_FOR_DEVINFO
66#include <rpmb.h>
67#endif
Dima Zavin214cc642009-01-26 11:16:21 -080068
Channagoud Kadabi90869ce2015-04-27 11:15:14 -070069#if ENABLE_WBC
70#include <pm_app_smbchg.h>
71#endif
72
Neeti Desai17379b82012-06-04 18:42:53 -070073#if DEVICE_TREE
74#include <libfdt.h>
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070075#include <dev_tree.h>
Neeti Desai17379b82012-06-04 18:42:53 -070076#endif
77
Aparna Mallavarapu118ccae2015-06-03 13:47:11 +053078#if WDOG_SUPPORT
79#include <wdog.h>
80#endif
81
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -070082#include <reboot.h>
Shashank Mittalcd98d472011-08-02 14:29:24 -070083#include "image_verify.h"
Shashank Mittal024c0332010-02-03 11:44:00 -080084#include "recovery.h"
Brian Swetland9c4c0752009-01-25 16:23:50 -080085#include "bootimg.h"
86#include "fastboot.h"
Ajay Dudani5c761132011-04-07 20:19:04 -070087#include "sparse_format.h"
Ajay Dudanide984792015-03-02 09:57:41 -080088#include "meta_format.h"
Greg Grisco6e754772011-06-23 12:19:39 -070089#include "mmc.h"
Shashank Mittal162244e2011-08-08 19:01:25 -070090#include "devinfo.h"
Neeti Desai465491e2012-07-31 12:53:35 -070091#include "board.h"
Shashank Mittal162244e2011-08-08 19:01:25 -070092#include "scm.h"
Amit Blay6281ebc2015-01-11 14:44:08 +020093#include "mdtp.h"
Sridhar Parasuram32b30662015-07-10 13:33:22 -070094#include "secapp_loader.h"
lijuanga40d6302015-07-20 20:10:13 +080095#include <menu_keys_detect.h>
96#include <display_menu.h>
Channagoud Kadabi736c4962015-08-21 11:56:52 -070097#include "fastboot_test.h"
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -070098
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070099extern bool target_use_signed_kernel(void);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700100extern void platform_uninit(void);
Channagoud Kadabi33defe22013-06-18 18:35:40 -0700101extern void target_uninit(void);
Joonwoo Park61112782013-10-02 19:50:39 -0700102extern int get_target_boot_params(const char *cmdline, const char *part,
vijay kumar870515d2015-08-31 16:37:24 +0530103 char **buf);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700104
Sridhar Parasuram7e16d172015-07-05 11:35:23 -0700105void *info_buf;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700106void write_device_info_mmc(device_info *dev);
107void write_device_info_flash(device_info *dev);
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -0700108static int aboot_save_boot_hash_mmc(uint32_t image_addr, uint32_t image_size);
Channagoud Kadabiad259832015-05-29 11:14:17 -0700109static int aboot_frp_unlock(char *pname, void *data, unsigned sz);
Kishor PK38ed93d2017-04-25 14:19:26 +0530110static inline uint64_t validate_partition_size();
Parth Dixit54ac3bb2017-03-07 15:52:48 +0530111bool pwr_key_is_pressed = false;
Hareesh Gauthamabf1e1e2017-04-12 18:09:24 +0530112static bool is_systemd_present=false;
Mayank Grover351a75e2017-05-30 20:06:08 +0530113static void publish_getvar_multislot_vars();
Sridhar Parasurame94e8152014-10-24 14:06:03 -0700114/* fastboot command function pointer */
115typedef void (*fastboot_cmd_fn) (const char *, void *, unsigned);
Monika Singh292b3e92018-03-17 22:40:23 +0530116bool get_perm_attr_status();
Sridhar Parasurame94e8152014-10-24 14:06:03 -0700117
118struct fastboot_cmd_desc {
119 char * name;
120 fastboot_cmd_fn cb;
121};
122
Subbaraman Narayanamurthyeb92bcc2010-07-20 14:32:46 -0700123#define EXPAND(NAME) #NAME
124#define TARGET(NAME) EXPAND(NAME)
Brian Swetland2defe162009-08-18 14:35:59 -0700125
Ajay Singh Parmara7865a82015-04-16 21:27:52 -0700126#define DISPLAY_PANEL_HDMI "hdmi"
127
Ajay Dudanicd01f9b2010-02-23 21:13:04 -0800128#ifdef MEMBASE
129#define EMMC_BOOT_IMG_HEADER_ADDR (0xFF000+(MEMBASE))
130#else
David Ng183a7422009-12-07 14:55:21 -0800131#define EMMC_BOOT_IMG_HEADER_ADDR 0xFF000
Ajay Dudanicd01f9b2010-02-23 21:13:04 -0800132#endif
133
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700134#ifndef MEMSIZE
135#define MEMSIZE 1024*1024
136#endif
137
138#define MAX_TAGS_SIZE 1024
139
Kun Liang2f1601a2013-08-12 16:29:54 +0800140/* make 4096 as default size to ensure EFS,EXT4's erasing */
141#define DEFAULT_ERASE_SIZE 4096
Ujwal Patelc0b0a252015-08-16 14:05:35 -0700142#define MAX_PANEL_BUF_SIZE 196
Vijay Kumar Pendoti0b21f462016-05-02 17:09:18 +0530143#define FOOTER_SIZE 16384
Kun Liang2f1601a2013-08-12 16:29:54 +0800144
Dhaval Patelf83d73b2014-06-23 16:24:37 -0700145#define DISPLAY_DEFAULT_PREFIX "mdss_mdp"
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700146#define BOOT_DEV_MAX_LEN 64
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -0800147
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800148#define IS_ARM64(ptr) (ptr->magic_64 == KERNEL64_HDR_MAGIC) ? true : false
149
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -0700150#define ADD_OF(a, b) (UINT_MAX - b > a) ? (a + b) : UINT_MAX
151
Ameya Thakur10a33452016-06-13 14:24:26 -0700152//Size of the header that is used in case the boot image has
153//a uncompressed kernel + appended dtb
154#define PATCHED_KERNEL_HEADER_SIZE 20
155
156//String used to determine if the boot image has
157//a uncompressed kernel + appended dtb
158#define PATCHED_KERNEL_MAGIC "UNCOMPRESSED_IMG"
159
Sridhar Parasuram7bd4aaf2015-02-12 11:14:38 -0800160#if USE_BOOTDEV_CMDLINE
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700161static const char *emmc_cmdline = " androidboot.bootdevice=";
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700162#else
David Ng183a7422009-12-07 14:55:21 -0800163static const char *emmc_cmdline = " androidboot.emmc=true";
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700164#endif
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800165static const char *usb_sn_cmdline = " androidboot.serialno=";
Pavel Nedev328ac822013-04-05 15:25:11 +0300166static const char *androidboot_mode = " androidboot.mode=";
Hareesh Gauthamabf1e1e2017-04-12 18:09:24 +0530167
168static const char *systemd_ffbm_mode = " systemd.unit=ffbm.target";
Matthew Qind886f3c2014-01-17 16:52:01 +0800169static const char *alarmboot_cmdline = " androidboot.alarmboot=true";
Pavel Nedev898298c2013-02-27 12:36:09 -0800170static const char *loglevel = " quiet";
Ajay Dudanica3a33c2011-11-18 08:31:40 -0800171static const char *battchg_pause = " androidboot.mode=charger";
Shashank Mittalcd98d472011-08-02 14:29:24 -0700172static const char *auth_kernel = " androidboot.authorized_kernel=true";
Pavel Nedev5614d222013-06-17 18:01:02 +0300173static const char *secondary_gpt_enable = " gpt";
Monika Singh292b3e92018-03-17 22:40:23 +0530174#ifdef MDTP_SUPPORT
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200175static const char *mdtp_activated_flag = " mdtp";
Monika Singh292b3e92018-03-17 22:40:23 +0530176#endif
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800177static const char *baseband_apq = " androidboot.baseband=apq";
178static const char *baseband_msm = " androidboot.baseband=msm";
179static const char *baseband_csfb = " androidboot.baseband=csfb";
180static const char *baseband_svlte2a = " androidboot.baseband=svlte2a";
Ajay Dudani403bc492011-09-30 16:17:21 -0700181static const char *baseband_mdm = " androidboot.baseband=mdm";
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800182static const char *baseband_mdm2 = " androidboot.baseband=mdm2";
Amol Jadi5c61a952012-05-04 17:05:35 -0700183static const char *baseband_sglte = " androidboot.baseband=sglte";
Amol Jadi2a15a272013-01-22 12:03:36 -0800184static const char *baseband_dsda = " androidboot.baseband=dsda";
185static const char *baseband_dsda2 = " androidboot.baseband=dsda2";
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800186static const char *baseband_sglte2 = " androidboot.baseband=sglte2";
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800187static const char *warmboot_cmdline = " qpnp-power-on.warm_boot=1";
Vijay Kumar Pendotib228cfc2016-06-13 20:15:23 +0530188static const char *baseband_apq_nowgr = " androidboot.baseband=baseband_apq_nowgr";
Mayank Grover351a75e2017-05-30 20:06:08 +0530189static const char *androidboot_slot_suffix = " androidboot.slot_suffix=";
190static const char *skip_ramfs = " skip_initramfs";
Mayank Grover3804be72017-06-22 11:59:23 +0530191static const char *sys_path_cmdline = " rootwait ro init=/init";
Sourabh Banerjee386b1322018-02-27 09:37:28 +0530192
193#if VERITY_LE
194static const char *verity_dev = " root=/dev/dm-0";
195static const char *verity_system_part = " dm=\"system";
196static const char *verity_params = " none ro,0 1 android-verity /dev/mmcblk0p";
197#else
Mayank Grover3804be72017-06-22 11:59:23 +0530198static const char *sys_path = " root=/dev/mmcblk0p";
Sourabh Banerjee386b1322018-02-27 09:37:28 +0530199#endif
Ajay Dudanid04110c2011-01-17 23:55:07 -0800200
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700201#if VERIFIED_BOOT
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700202static const char *verity_mode = " androidboot.veritymode=";
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700203static const char *verified_state= " androidboot.verifiedbootstate=";
Parth Dixita5715a02015-10-29 12:25:10 +0530204static const char *keymaster_v1= " androidboot.keymaster=1";
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700205//indexed based on enum values, green is 0 by default
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700206
207struct verified_boot_verity_mode vbvm[] =
208{
Channagoud Kadabi86b0c112016-03-16 19:23:16 -0700209#if ENABLE_VB_ATTEST
210 {false, "eio"},
211#else
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700212 {false, "logging"},
Channagoud Kadabi86b0c112016-03-16 19:23:16 -0700213#endif
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700214 {true, "enforcing"},
215};
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700216struct verified_boot_state_name vbsn[] =
217{
218 {GREEN, "green"},
219 {ORANGE, "orange"},
220 {YELLOW,"yellow"},
221 {RED,"red" },
222};
223#endif
Channagoud Kadabi86b0c112016-03-16 19:23:16 -0700224/*As per spec delay wait time before shutdown in Red state*/
225#define DELAY_WAIT 30000
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700226static unsigned page_size = 0;
227static unsigned page_mask = 0;
Gaurav Nebhwani4d2389c2016-03-17 21:10:05 +0530228static unsigned mmc_blocksize = 0;
229static unsigned mmc_blocksize_mask = 0;
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700230static char ffbm_mode_string[FFBM_MODE_BUF_SIZE];
231static bool boot_into_ffbm;
vijay kumar870515d2015-08-31 16:37:24 +0530232static char *target_boot_params = NULL;
Matthew Qind886f3c2014-01-17 16:52:01 +0800233static bool boot_reason_alarm;
Channagoud Kadabi80a182b2015-03-11 17:04:23 -0700234static bool devinfo_present = true;
Channagoud Kadabi736c4962015-08-21 11:56:52 -0700235bool boot_into_fastboot = false;
Parth Dixit4097b622016-03-15 14:42:27 +0530236static uint32_t dt_size = 0;
Monika Singh292b3e92018-03-17 22:40:23 +0530237static char *vbcmdline;
238static bootinfo info = {0};
Shashank Mittalcd98d472011-08-02 14:29:24 -0700239/* Assuming unauthorized kernel image by default */
240static int auth_kernel_img = 0;
Monika Singh292b3e92018-03-17 22:40:23 +0530241static device_info device = {DEVICE_MAGIC,0,0,0,0,{0},{0},{0},1,{0},0,{0}};
242
vijay kumarc65876c2015-04-24 13:29:16 +0530243static bool is_allow_unlock = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -0700244
vijay kumarca2e6812015-07-08 20:28:25 +0530245static char frp_ptns[2][8] = {"config","frp"};
246
lijuang511a2b52015-08-14 20:50:51 +0800247static const char *critical_flash_allowed_ptn[] = {
248 "aboot",
249 "rpm",
250 "tz",
251 "sbl",
252 "sdi",
253 "sbl1",
254 "xbl",
255 "hyp",
256 "pmic",
257 "bootloader",
258 "devinfo",
259 "partition"};
260
Dima Zavin42168f22009-01-30 11:52:22 -0800261struct atag_ptbl_entry
262{
263 char name[16];
264 unsigned offset;
265 unsigned size;
266 unsigned flags;
267};
268
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700269/*
270 * Partition info, required to be published
271 * for fastboot
272 */
273struct getvar_partition_info {
Mayank Grover7b8a8f82017-10-27 13:07:59 +0530274 char part_name[MAX_GPT_NAME_SIZE]; /* Partition name */
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700275 char getvar_size[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for size */
276 char getvar_type[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for type */
277 char size_response[MAX_RSP_SIZE]; /* fastboot response for size */
278 char type_response[MAX_RSP_SIZE]; /* fastboot response for type */
279};
280
281/*
Mayank Grover7b8a8f82017-10-27 13:07:59 +0530282 * Update the part_type_known for known paritions types.
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700283 */
Mayank Groverd38fe012018-03-13 15:33:16 +0530284#define RAW_STR "raw"
Mayank Grover52cd10a2018-03-15 12:57:54 +0530285#define EXT_STR "ext4"
286#define F2FS_STR "f2fs"
287
288#define FS_SUPERBLOCK_OFFSET 0x400
289#define EXT_MAGIC 0xEF53
290#define EXT_MAGIC_OFFSET_SB 0x38
291#define F2FS_MAGIC 0xF2F52010 // F2FS Magic Number
292#define F2FS_MAGIC_OFFSET_SB 0x0
293
294typedef enum fs_signature_type {
295 EXT_FS_SIGNATURE = 1,
296 EXT_F2FS_SIGNATURE = 2,
297 NO_FS = -1
298} fs_signature_type;
299
Mayank Grover7b8a8f82017-10-27 13:07:59 +0530300struct getvar_partition_info part_info[NUM_PARTITIONS];
301struct getvar_partition_info part_type_known[] =
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700302{
Mayank Grover49920212018-02-09 18:15:55 +0530303 { "system" , "partition-size:", "partition-type:", "", "ext4" },
304 { "userdata" , "partition-size:", "partition-type:", "", "ext4" },
305 { "cache" , "partition-size:", "partition-type:", "", "ext4" },
306 { "recoveryfs" , "partition-size:", "partition-type:", "", "ext4" },
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700307};
308
309char max_download_size[MAX_RSP_SIZE];
Ameya Thakur11cf1a62013-08-05 12:44:48 -0700310char charger_screen_enabled[MAX_RSP_SIZE];
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800311char sn_buf[13];
Dhaval Patel223ec952013-07-18 14:49:44 -0700312char display_panel_buf[MAX_PANEL_BUF_SIZE];
Unnati Gandhi62c8ab82014-01-24 11:01:01 +0530313char panel_display_mode[MAX_RSP_SIZE];
Mayank Grover7b8a8f82017-10-27 13:07:59 +0530314#if PRODUCT_IOT
315char block_size_string[MAX_RSP_SIZE];
316
317/* For IOT we are using custom version */
318#define PRODUCT_IOT_VERSION "IOT001"
319char bootloader_version_string[MAX_RSP_SIZE];
320#endif
lijuang102dfa92015-10-09 18:31:03 +0800321
322#if CHECK_BAT_VOLTAGE
lijuang65c5a822015-08-29 16:35:36 +0800323char battery_voltage[MAX_RSP_SIZE];
lijuang102dfa92015-10-09 18:31:03 +0800324char battery_soc_ok [MAX_RSP_SIZE];
325#endif
326
lijuangf16461c2015-08-03 17:09:34 +0800327char get_variant[MAX_RSP_SIZE];
Greg Griscod6250552011-06-29 14:40:23 -0700328
Greg Griscod2471ef2011-07-14 13:00:42 -0700329extern int emmc_recovery_init(void);
330
Kinson Chik0b1c8162011-08-31 16:31:57 -0700331#if NO_KEYPAD_DRIVER
332extern int fastboot_trigger(void);
333#endif
Greg Griscod2471ef2011-07-14 13:00:42 -0700334
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800335static void update_ker_tags_rdisk_addr(struct boot_img_hdr *hdr, bool is_arm64)
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700336{
337 /* overwrite the destination of specified for the project */
Channagoud Kadabi7042fa32013-04-26 16:44:14 -0700338#ifdef ABOOT_IGNORE_BOOT_HEADER_ADDRS
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800339 if (is_arm64)
340 hdr->kernel_addr = ABOOT_FORCE_KERNEL64_ADDR;
341 else
342 hdr->kernel_addr = ABOOT_FORCE_KERNEL_ADDR;
Channagoud Kadabi7042fa32013-04-26 16:44:14 -0700343 hdr->ramdisk_addr = ABOOT_FORCE_RAMDISK_ADDR;
344 hdr->tags_addr = ABOOT_FORCE_TAGS_ADDR;
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700345#endif
346}
347
Dima Zavin42168f22009-01-30 11:52:22 -0800348static void ptentry_to_tag(unsigned **ptr, struct ptentry *ptn)
349{
350 struct atag_ptbl_entry atag_ptn;
351
352 memcpy(atag_ptn.name, ptn->name, 16);
353 atag_ptn.name[15] = '\0';
354 atag_ptn.offset = ptn->start;
355 atag_ptn.size = ptn->length;
356 atag_ptn.flags = ptn->flags;
357 memcpy(*ptr, &atag_ptn, sizeof(struct atag_ptbl_entry));
358 *ptr += sizeof(struct atag_ptbl_entry) / sizeof(unsigned);
359}
Brian Swetland9c4c0752009-01-25 16:23:50 -0800360
lijuang102dfa92015-10-09 18:31:03 +0800361#if CHECK_BAT_VOLTAGE
362void update_battery_status(void)
363{
364 snprintf(battery_voltage,MAX_RSP_SIZE, "%d",target_get_battery_voltage());
365 snprintf(battery_soc_ok ,MAX_RSP_SIZE, "%s",target_battery_soc_ok()? "yes":"no");
366}
367#endif
368
Neeti Desaie245d492012-06-01 12:52:13 -0700369unsigned char *update_cmdline(const char * cmdline)
Brian Swetland9c4c0752009-01-25 16:23:50 -0800370{
David Ng183a7422009-12-07 14:55:21 -0800371 int cmdline_len = 0;
372 int have_cmdline = 0;
Amol Jadi168b7712012-03-06 16:15:00 -0800373 unsigned char *cmdline_final = NULL;
Neeti Desaie245d492012-06-01 12:52:13 -0700374 int pause_at_bootup = 0;
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800375 bool warm_boot = false;
Pavel Nedev5614d222013-06-17 18:01:02 +0300376 bool gpt_exists = partition_gpt_exists();
Joonwoo Park61112782013-10-02 19:50:39 -0700377 int have_target_boot_params = 0;
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700378 char *boot_dev_buf = NULL;
Monika Singh292b3e92018-03-17 22:40:23 +0530379#ifdef MDTP_SUPPORT
Mayank Grover351a75e2017-05-30 20:06:08 +0530380 bool is_mdtp_activated = 0;
Monika Singh292b3e92018-03-17 22:40:23 +0530381#endif
Mayank Grover351a75e2017-05-30 20:06:08 +0530382 int current_active_slot = INVALID;
Mayank Grover3804be72017-06-22 11:59:23 +0530383 int system_ptn_index = -1;
384 unsigned int lun = 0;
385 char lun_char_base = 'a';
Sourabh Banerjee386b1322018-02-27 09:37:28 +0530386#if VERITY_LE
387 int syspath_buflen = strlen(verity_dev)
388 + strlen(verity_system_part) + (sizeof(char) * 2) + 2
389 + strlen(verity_params) + sizeof(int) + 2;
390#else
391 int syspath_buflen = strlen(sys_path) + sizeof(int) + 2; /*allocate buflen for largest possible string*/
392#endif
Mayank Grover3804be72017-06-22 11:59:23 +0530393 char syspath_buf[syspath_buflen];
Mayank Grover889be1b2017-09-12 20:12:23 +0530394#if VERIFIED_BOOT
395 uint32_t boot_state = RED;
396#endif
Hareesh Gauthamabf1e1e2017-04-12 18:09:24 +0530397
398#if USE_LE_SYSTEMD
399 is_systemd_present=true;
400#endif
401
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700402#if VERIFIED_BOOT
Mayank Grover912eaa62017-10-26 12:08:53 +0530403 if (VB_M <= target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +0530404 {
405 boot_state = boot_verify_get_state();
406 }
Parth Dixitddbc7352015-10-18 03:13:31 +0530407#endif
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700408
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200409#ifdef MDTP_SUPPORT
410 mdtp_activated(&is_mdtp_activated);
411#endif /* MDTP_SUPPORT */
Dima Zavin42168f22009-01-30 11:52:22 -0800412
Brian Swetland9c4c0752009-01-25 16:23:50 -0800413 if (cmdline && cmdline[0]) {
David Ng183a7422009-12-07 14:55:21 -0800414 cmdline_len = strlen(cmdline);
415 have_cmdline = 1;
416 }
417 if (target_is_emmc_boot()) {
418 cmdline_len += strlen(emmc_cmdline);
Sridhar Parasuram7bd4aaf2015-02-12 11:14:38 -0800419#if USE_BOOTDEV_CMDLINE
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700420 boot_dev_buf = (char *) malloc(sizeof(char) * BOOT_DEV_MAX_LEN);
421 ASSERT(boot_dev_buf);
422 platform_boot_dev_cmdline(boot_dev_buf);
423 cmdline_len += strlen(boot_dev_buf);
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700424#endif
David Ng183a7422009-12-07 14:55:21 -0800425 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800426
427 cmdline_len += strlen(usb_sn_cmdline);
428 cmdline_len += strlen(sn_buf);
429
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700430#if VERIFIED_BOOT
Mayank Grover912eaa62017-10-26 12:08:53 +0530431 if (VB_M <= target_get_vb_version())
Sridhar Parasuram7d8bb9a2015-09-23 18:21:19 -0700432 {
Mayank Grover889be1b2017-09-12 20:12:23 +0530433 cmdline_len += strlen(verified_state) + strlen(vbsn[boot_state].name);
434 if ((device.verity_mode != 0 ) && (device.verity_mode != 1))
435 {
436 dprintf(CRITICAL, "Devinfo paritition possibly corrupted!!!. Please erase devinfo partition to continue booting\n");
437 ASSERT(0);
438 }
439 cmdline_len += strlen(verity_mode) + strlen(vbvm[device.verity_mode].name);
440 cmdline_len += strlen(keymaster_v1);
Sridhar Parasuram7d8bb9a2015-09-23 18:21:19 -0700441 }
Parth Dixitddbc7352015-10-18 03:13:31 +0530442#endif
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700443
Monika Singh292b3e92018-03-17 22:40:23 +0530444
445 if (vbcmdline != NULL) {
446 dprintf(DEBUG, "UpdateCmdLine vbcmdline present len %d\n",
447 strlen(vbcmdline));
448 cmdline_len += strlen(vbcmdline);
449 }
450
Pavel Nedev5614d222013-06-17 18:01:02 +0300451 if (boot_into_recovery && gpt_exists)
452 cmdline_len += strlen(secondary_gpt_enable);
453
Monika Singh292b3e92018-03-17 22:40:23 +0530454#ifdef MDTP_SUPPORT
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200455 if(is_mdtp_activated)
456 cmdline_len += strlen(mdtp_activated_flag);
Monika Singh292b3e92018-03-17 22:40:23 +0530457#endif
Pavel Nedev328ac822013-04-05 15:25:11 +0300458 if (boot_into_ffbm) {
459 cmdline_len += strlen(androidboot_mode);
Hareesh Gauthamabf1e1e2017-04-12 18:09:24 +0530460
461 if(is_systemd_present)
462 cmdline_len += strlen(systemd_ffbm_mode);
463
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700464 cmdline_len += strlen(ffbm_mode_string);
Pavel Nedev898298c2013-02-27 12:36:09 -0800465 /* reduce kernel console messages to speed-up boot */
466 cmdline_len += strlen(loglevel);
Matthew Qind886f3c2014-01-17 16:52:01 +0800467 } else if (boot_reason_alarm) {
468 cmdline_len += strlen(alarmboot_cmdline);
Zhenhua Huang431dafa2015-06-30 16:13:37 +0800469 } else if ((target_build_variant_user() || device.charger_screen_enabled)
470 && target_pause_for_battery_charge()) {
David Ngf773dde2010-07-26 19:55:08 -0700471 pause_at_bootup = 1;
472 cmdline_len += strlen(battchg_pause);
473 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800474
Shashank Mittalcd98d472011-08-02 14:29:24 -0700475 if(target_use_signed_kernel() && auth_kernel_img) {
476 cmdline_len += strlen(auth_kernel);
477 }
478
Joonwoo Park61112782013-10-02 19:50:39 -0700479 if (get_target_boot_params(cmdline, boot_into_recovery ? "recoveryfs" :
480 "system",
vijay kumar870515d2015-08-31 16:37:24 +0530481 &target_boot_params) == 0) {
Joonwoo Park61112782013-10-02 19:50:39 -0700482 have_target_boot_params = 1;
483 cmdline_len += strlen(target_boot_params);
484 }
485
Ajay Dudanid04110c2011-01-17 23:55:07 -0800486 /* Determine correct androidboot.baseband to use */
487 switch(target_baseband())
488 {
489 case BASEBAND_APQ:
490 cmdline_len += strlen(baseband_apq);
491 break;
492
493 case BASEBAND_MSM:
494 cmdline_len += strlen(baseband_msm);
495 break;
496
497 case BASEBAND_CSFB:
498 cmdline_len += strlen(baseband_csfb);
499 break;
500
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800501 case BASEBAND_SVLTE2A:
502 cmdline_len += strlen(baseband_svlte2a);
Ajay Dudanid04110c2011-01-17 23:55:07 -0800503 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700504
505 case BASEBAND_MDM:
506 cmdline_len += strlen(baseband_mdm);
507 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700508
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800509 case BASEBAND_MDM2:
510 cmdline_len += strlen(baseband_mdm2);
511 break;
512
Amol Jadi5c61a952012-05-04 17:05:35 -0700513 case BASEBAND_SGLTE:
514 cmdline_len += strlen(baseband_sglte);
515 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530516
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800517 case BASEBAND_SGLTE2:
518 cmdline_len += strlen(baseband_sglte2);
519 break;
520
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530521 case BASEBAND_DSDA:
522 cmdline_len += strlen(baseband_dsda);
523 break;
Amol Jadi2a15a272013-01-22 12:03:36 -0800524
525 case BASEBAND_DSDA2:
526 cmdline_len += strlen(baseband_dsda2);
527 break;
Vijay Kumar Pendotib228cfc2016-06-13 20:15:23 +0530528 case BASEBAND_APQ_NOWGR:
529 cmdline_len += strlen(baseband_apq_nowgr);
530 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800531 }
532
Vladislav Levenetz3cd45242017-04-05 08:58:07 +0300533#if ENABLE_DISPLAY
Lijuan Gao4a5b8322014-07-24 10:38:42 +0800534 if (cmdline) {
535 if ((strstr(cmdline, DISPLAY_DEFAULT_PREFIX) == NULL) &&
Padmanabhan Komandurubccbcdc2015-06-30 16:19:24 +0530536 target_display_panel_node(display_panel_buf,
537 MAX_PANEL_BUF_SIZE) &&
Lijuan Gao4a5b8322014-07-24 10:38:42 +0800538 strlen(display_panel_buf)) {
539 cmdline_len += strlen(display_panel_buf);
540 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700541 }
Vladislav Levenetz3cd45242017-04-05 08:58:07 +0300542#endif
Dhaval Patel223ec952013-07-18 14:49:44 -0700543
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800544 if (target_warm_boot()) {
545 warm_boot = true;
546 cmdline_len += strlen(warmboot_cmdline);
547 }
548
Mayank Grover351a75e2017-05-30 20:06:08 +0530549 if (partition_multislot_is_supported())
550 {
551 current_active_slot = partition_find_active_slot();
552 cmdline_len += (strlen(androidboot_slot_suffix)+
553 strlen(SUFFIX_SLOT(current_active_slot)));
554
Mayank Grover3804be72017-06-22 11:59:23 +0530555 system_ptn_index = partition_get_index("system");
556 if (platform_boot_dev_isemmc())
557 {
Sourabh Banerjee386b1322018-02-27 09:37:28 +0530558#if VERITY_LE
559 /*
560 Condition 4: Verity and A/B both enabled
561 Eventual command line looks like:
562 ... androidboot.slot_suffix=<slot_suffix> ... rootfstype=ext4 ...
563 ... root=/dev/dm-0 dm="system_<slot_suffix> none ro,0 1 android-verity /dev/mmcblk0p<NN>"
564 */
565 snprintf(syspath_buf, syspath_buflen, " %s %s%s %s%d\"",
566 verity_dev,
567 verity_system_part, suffix_slot[current_active_slot],
568 verity_params, system_ptn_index + 1);
569#else
570 /*
571 Condition 5: A/B enabled, but verity disabled
572 Eventual command line looks like:
573 ... androidboot.slot_suffix=<slot_suffix> ... rootfstype=ext4 ...
574 ... root=/dev/mmcblk0p<NN> ...
575 */
576 snprintf(syspath_buf, syspath_buflen, " %s%d",
577 sys_path, system_ptn_index + 1);
578#endif
Mayank Grover3804be72017-06-22 11:59:23 +0530579 }
580 else
581 {
582 lun = partition_get_lun(system_ptn_index);
583 snprintf(syspath_buf, syspath_buflen, " root=/dev/sd%c%d",
584 lun_char_base + lun,
585 partition_get_index_in_lun("system", lun));
586 }
Mayank Grover351a75e2017-05-30 20:06:08 +0530587
Mayank Grover3804be72017-06-22 11:59:23 +0530588 cmdline_len += strlen(sys_path_cmdline);
Monika Singh292b3e92018-03-17 22:40:23 +0530589#ifndef VERIFIED_BOOT_2
Mayank Grover3804be72017-06-22 11:59:23 +0530590 cmdline_len += strlen(syspath_buf);
Monika Singh292b3e92018-03-17 22:40:23 +0530591#endif
Mayank Grover351a75e2017-05-30 20:06:08 +0530592 if (!boot_into_recovery)
593 cmdline_len += strlen(skip_ramfs);
594 }
595
Channagoud Kadabi85c7ec32016-01-28 23:09:21 -0800596#if TARGET_CMDLINE_SUPPORT
597 char *target_cmdline_buf = malloc(TARGET_MAX_CMDLNBUF);
598 int target_cmd_line_len;
599 ASSERT(target_cmdline_buf);
600 target_cmd_line_len = target_update_cmdline(target_cmdline_buf);
601 cmdline_len += target_cmd_line_len;
602#endif
603
David Ng183a7422009-12-07 14:55:21 -0800604 if (cmdline_len > 0) {
605 const char *src;
Maria Yu52254c02014-07-04 16:14:54 +0800606 unsigned char *dst;
607
608 cmdline_final = (unsigned char*) malloc((cmdline_len + 4) & (~3));
609 ASSERT(cmdline_final != NULL);
vijay kumar287bb542015-09-29 13:01:52 +0530610 memset((void *)cmdline_final, 0, sizeof(*cmdline_final));
Maria Yu52254c02014-07-04 16:14:54 +0800611 dst = cmdline_final;
Neeti Desaie245d492012-06-01 12:52:13 -0700612
Amol Jadi168b7712012-03-06 16:15:00 -0800613 /* Save start ptr for debug print */
David Ng183a7422009-12-07 14:55:21 -0800614 if (have_cmdline) {
615 src = cmdline;
616 while ((*dst++ = *src++));
617 }
618 if (target_is_emmc_boot()) {
619 src = emmc_cmdline;
620 if (have_cmdline) --dst;
David Ngf773dde2010-07-26 19:55:08 -0700621 have_cmdline = 1;
622 while ((*dst++ = *src++));
Sridhar Parasuram7bd4aaf2015-02-12 11:14:38 -0800623#if USE_BOOTDEV_CMDLINE
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700624 src = boot_dev_buf;
625 if (have_cmdline) --dst;
626 while ((*dst++ = *src++));
627#endif
David Ngf773dde2010-07-26 19:55:08 -0700628 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800629
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700630#if VERIFIED_BOOT
Mayank Grover912eaa62017-10-26 12:08:53 +0530631 if (VB_M <= target_get_vb_version())
Sridhar Parasuram7d8bb9a2015-09-23 18:21:19 -0700632 {
Mayank Grover889be1b2017-09-12 20:12:23 +0530633 src = verified_state;
634 if(have_cmdline) --dst;
635 have_cmdline = 1;
636 while ((*dst++ = *src++));
637 src = vbsn[boot_state].name;
638 if(have_cmdline) --dst;
639 while ((*dst++ = *src++));
640
641 if ((device.verity_mode != 0 ) && (device.verity_mode != 1))
642 {
643 dprintf(CRITICAL, "Devinfo paritition possibly corrupted!!!. Please erase devinfo partition to continue booting\n");
644 ASSERT(0);
645 }
646 src = verity_mode;
647 if(have_cmdline) --dst;
648 while ((*dst++ = *src++));
649 src = vbvm[device.verity_mode].name;
650 if(have_cmdline) -- dst;
651 while ((*dst++ = *src++));
652 src = keymaster_v1;
653 if(have_cmdline) --dst;
654 while ((*dst++ = *src++));
Sridhar Parasuram7d8bb9a2015-09-23 18:21:19 -0700655 }
Parth Dixitddbc7352015-10-18 03:13:31 +0530656#endif
Monika Singh292b3e92018-03-17 22:40:23 +0530657
658 if (vbcmdline != NULL) {
659 src = vbcmdline;
660 if (have_cmdline) --dst;
661 while ((*dst++ = *src++));
662 }
663
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800664 src = usb_sn_cmdline;
665 if (have_cmdline) --dst;
666 have_cmdline = 1;
667 while ((*dst++ = *src++));
668 src = sn_buf;
669 if (have_cmdline) --dst;
670 have_cmdline = 1;
671 while ((*dst++ = *src++));
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800672 if (warm_boot) {
673 if (have_cmdline) --dst;
674 src = warmboot_cmdline;
675 while ((*dst++ = *src++));
676 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800677
Pavel Nedev5614d222013-06-17 18:01:02 +0300678 if (boot_into_recovery && gpt_exists) {
679 src = secondary_gpt_enable;
680 if (have_cmdline) --dst;
681 while ((*dst++ = *src++));
682 }
Monika Singh292b3e92018-03-17 22:40:23 +0530683#ifdef MDTP_SUPPORT
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200684 if (is_mdtp_activated) {
685 src = mdtp_activated_flag;
686 if (have_cmdline) --dst;
687 while ((*dst++ = *src++));
688 }
Monika Singh292b3e92018-03-17 22:40:23 +0530689#endif
Pavel Nedev328ac822013-04-05 15:25:11 +0300690 if (boot_into_ffbm) {
691 src = androidboot_mode;
692 if (have_cmdline) --dst;
693 while ((*dst++ = *src++));
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700694 src = ffbm_mode_string;
Pavel Nedev328ac822013-04-05 15:25:11 +0300695 if (have_cmdline) --dst;
696 while ((*dst++ = *src++));
Hareesh Gauthamabf1e1e2017-04-12 18:09:24 +0530697
698 if(is_systemd_present) {
699 src = systemd_ffbm_mode;
700 if (have_cmdline) --dst;
701 while ((*dst++ = *src++));
702 }
703
Pavel Nedev898298c2013-02-27 12:36:09 -0800704 src = loglevel;
705 if (have_cmdline) --dst;
706 while ((*dst++ = *src++));
Matthew Qind886f3c2014-01-17 16:52:01 +0800707 } else if (boot_reason_alarm) {
708 src = alarmboot_cmdline;
709 if (have_cmdline) --dst;
710 while ((*dst++ = *src++));
Pavel Nedev328ac822013-04-05 15:25:11 +0300711 } else if (pause_at_bootup) {
David Ngf773dde2010-07-26 19:55:08 -0700712 src = battchg_pause;
713 if (have_cmdline) --dst;
David Ng183a7422009-12-07 14:55:21 -0800714 while ((*dst++ = *src++));
715 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800716
Shashank Mittalcd98d472011-08-02 14:29:24 -0700717 if(target_use_signed_kernel() && auth_kernel_img) {
718 src = auth_kernel;
719 if (have_cmdline) --dst;
720 while ((*dst++ = *src++));
721 }
722
Ajay Dudanid04110c2011-01-17 23:55:07 -0800723 switch(target_baseband())
724 {
725 case BASEBAND_APQ:
726 src = baseband_apq;
727 if (have_cmdline) --dst;
728 while ((*dst++ = *src++));
729 break;
730
731 case BASEBAND_MSM:
732 src = baseband_msm;
733 if (have_cmdline) --dst;
734 while ((*dst++ = *src++));
735 break;
736
737 case BASEBAND_CSFB:
738 src = baseband_csfb;
739 if (have_cmdline) --dst;
740 while ((*dst++ = *src++));
741 break;
742
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800743 case BASEBAND_SVLTE2A:
744 src = baseband_svlte2a;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800745 if (have_cmdline) --dst;
746 while ((*dst++ = *src++));
747 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700748
749 case BASEBAND_MDM:
750 src = baseband_mdm;
751 if (have_cmdline) --dst;
752 while ((*dst++ = *src++));
753 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700754
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800755 case BASEBAND_MDM2:
756 src = baseband_mdm2;
757 if (have_cmdline) --dst;
758 while ((*dst++ = *src++));
759 break;
760
Amol Jadi5c61a952012-05-04 17:05:35 -0700761 case BASEBAND_SGLTE:
762 src = baseband_sglte;
763 if (have_cmdline) --dst;
764 while ((*dst++ = *src++));
765 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530766
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800767 case BASEBAND_SGLTE2:
768 src = baseband_sglte2;
769 if (have_cmdline) --dst;
770 while ((*dst++ = *src++));
771 break;
772
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530773 case BASEBAND_DSDA:
774 src = baseband_dsda;
775 if (have_cmdline) --dst;
776 while ((*dst++ = *src++));
777 break;
Amol Jadi2a15a272013-01-22 12:03:36 -0800778
779 case BASEBAND_DSDA2:
780 src = baseband_dsda2;
781 if (have_cmdline) --dst;
782 while ((*dst++ = *src++));
783 break;
Vijay Kumar Pendotib228cfc2016-06-13 20:15:23 +0530784 case BASEBAND_APQ_NOWGR:
785 src = baseband_apq_nowgr;
786 if (have_cmdline) --dst;
787 while ((*dst++ = *src++));
788 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800789 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700790
791 if (strlen(display_panel_buf)) {
Dhaval Patel223ec952013-07-18 14:49:44 -0700792 src = display_panel_buf;
793 if (have_cmdline) --dst;
794 while ((*dst++ = *src++));
795 }
Joonwoo Park61112782013-10-02 19:50:39 -0700796
797 if (have_target_boot_params) {
798 if (have_cmdline) --dst;
799 src = target_boot_params;
800 while ((*dst++ = *src++));
vijay kumar870515d2015-08-31 16:37:24 +0530801 free(target_boot_params);
Joonwoo Park61112782013-10-02 19:50:39 -0700802 }
Channagoud Kadabi85c7ec32016-01-28 23:09:21 -0800803
Mayank Grover351a75e2017-05-30 20:06:08 +0530804 if (partition_multislot_is_supported() && have_cmdline)
805 {
806 src = androidboot_slot_suffix;
807 --dst;
808 while ((*dst++ = *src++));
809 --dst;
810 src = SUFFIX_SLOT(current_active_slot);
811 while ((*dst++ = *src++));
812
813 if (!boot_into_recovery)
814 {
815 src = skip_ramfs;
816 --dst;
817 while ((*dst++ = *src++));
818 }
819
820 src = sys_path_cmdline;
821 --dst;
822 while ((*dst++ = *src++));
Mayank Grover3804be72017-06-22 11:59:23 +0530823
Monika Singh292b3e92018-03-17 22:40:23 +0530824#ifndef VERIFIED_BOOT_2
Mayank Grover3804be72017-06-22 11:59:23 +0530825 src = syspath_buf;
826 --dst;
827 while ((*dst++ = *src++));
Monika Singh292b3e92018-03-17 22:40:23 +0530828#endif
Mayank Grover351a75e2017-05-30 20:06:08 +0530829 }
830
Channagoud Kadabi85c7ec32016-01-28 23:09:21 -0800831#if TARGET_CMDLINE_SUPPORT
832 if (target_cmdline_buf && target_cmd_line_len)
833 {
834 if (have_cmdline) --dst;
835 src = target_cmdline_buf;
836 while((*dst++ = *src++));
837 free(target_cmdline_buf);
838 }
839#endif
Neeti Desaie245d492012-06-01 12:52:13 -0700840 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700841
842
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700843 if (boot_dev_buf)
844 free(boot_dev_buf);
845
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -0800846 if (cmdline_final)
847 dprintf(INFO, "cmdline: %s\n", cmdline_final);
848 else
849 dprintf(INFO, "cmdline is NULL\n");
Neeti Desaie245d492012-06-01 12:52:13 -0700850 return cmdline_final;
851}
852
853unsigned *atag_core(unsigned *ptr)
854{
855 /* CORE */
856 *ptr++ = 2;
857 *ptr++ = 0x54410001;
858
859 return ptr;
860
861}
862
863unsigned *atag_ramdisk(unsigned *ptr, void *ramdisk,
864 unsigned ramdisk_size)
865{
866 if (ramdisk_size) {
867 *ptr++ = 4;
868 *ptr++ = 0x54420005;
869 *ptr++ = (unsigned)ramdisk;
870 *ptr++ = ramdisk_size;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800871 }
872
Neeti Desaie245d492012-06-01 12:52:13 -0700873 return ptr;
874}
875
876unsigned *atag_ptable(unsigned **ptr_addr)
877{
878 int i;
879 struct ptable *ptable;
880
881 if ((ptable = flash_get_ptable()) && (ptable->count != 0)) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700882 *(*ptr_addr)++ = 2 + (ptable->count * (sizeof(struct atag_ptbl_entry) /
883 sizeof(unsigned)));
Neeti Desaie245d492012-06-01 12:52:13 -0700884 *(*ptr_addr)++ = 0x4d534d70;
885 for (i = 0; i < ptable->count; ++i)
886 ptentry_to_tag(ptr_addr, ptable_get(ptable, i));
887 }
888
889 return (*ptr_addr);
890}
891
892unsigned *atag_cmdline(unsigned *ptr, const char *cmdline)
893{
894 int cmdline_length = 0;
895 int n;
Neeti Desaie245d492012-06-01 12:52:13 -0700896 char *dest;
897
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800898 cmdline_length = strlen((const char*)cmdline);
Neeti Desaie245d492012-06-01 12:52:13 -0700899 n = (cmdline_length + 4) & (~3);
900
901 *ptr++ = (n / 4) + 2;
902 *ptr++ = 0x54410009;
903 dest = (char *) ptr;
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800904 while ((*dest++ = *cmdline++));
Neeti Desaie245d492012-06-01 12:52:13 -0700905 ptr += (n / 4);
906
907 return ptr;
908}
909
910unsigned *atag_end(unsigned *ptr)
911{
Brian Swetland9c4c0752009-01-25 16:23:50 -0800912 /* END */
913 *ptr++ = 0;
914 *ptr++ = 0;
915
Neeti Desaie245d492012-06-01 12:52:13 -0700916 return ptr;
917}
918
919void generate_atags(unsigned *ptr, const char *cmdline,
920 void *ramdisk, unsigned ramdisk_size)
921{
vijay kumar21a37452015-12-29 16:23:21 +0530922 unsigned *orig_ptr = ptr;
Neeti Desaie245d492012-06-01 12:52:13 -0700923 ptr = atag_core(ptr);
924 ptr = atag_ramdisk(ptr, ramdisk, ramdisk_size);
925 ptr = target_atag_mem(ptr);
926
927 /* Skip NAND partition ATAGS for eMMC boot */
928 if (!target_is_emmc_boot()){
929 ptr = atag_ptable(&ptr);
930 }
931
vijay kumar21a37452015-12-29 16:23:21 +0530932 /*
933 * Atags size filled till + cmdline size + 1 unsigned for 4-byte boundary + 4 unsigned
934 * for atag identifier in atag_cmdline and atag_end should be with in MAX_TAGS_SIZE bytes
935 */
Mayank Groverbc8a2ef2018-02-23 18:03:36 +0530936 if (!cmdline)
937 return;
938
vijay kumar21a37452015-12-29 16:23:21 +0530939 if (((ptr - orig_ptr) + strlen(cmdline) + 5 * sizeof(unsigned)) < MAX_TAGS_SIZE) {
940 ptr = atag_cmdline(ptr, cmdline);
941 ptr = atag_end(ptr);
942 }
943 else {
944 dprintf(CRITICAL,"Crossing ATAGs Max size allowed\n");
945 ASSERT(0);
946 }
Neeti Desaie245d492012-06-01 12:52:13 -0700947}
948
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700949typedef void entry_func_ptr(unsigned, unsigned, unsigned*);
Neeti Desaie245d492012-06-01 12:52:13 -0700950void boot_linux(void *kernel, unsigned *tags,
951 const char *cmdline, unsigned machtype,
952 void *ramdisk, unsigned ramdisk_size)
953{
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800954 unsigned char *final_cmdline;
Amol Jadib6be5c12012-11-14 13:39:51 -0800955#if DEVICE_TREE
Neeti Desai17379b82012-06-04 18:42:53 -0700956 int ret = 0;
Amol Jadib6be5c12012-11-14 13:39:51 -0800957#endif
958
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700959 void (*entry)(unsigned, unsigned, unsigned*) = (entry_func_ptr*)(PA((addr_t)kernel));
Deepa Dinamani25a9f762012-11-30 15:57:15 -0800960 uint32_t tags_phys = PA((addr_t)tags);
vijay kumar1a50a642015-11-16 12:41:15 +0530961 struct kernel64_hdr *kptr = ((struct kernel64_hdr*)(PA((addr_t)kernel)));
Deepa Dinamani25a9f762012-11-30 15:57:15 -0800962
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530963 ramdisk = (void *)PA((addr_t)ramdisk);
Neeti Desaie245d492012-06-01 12:52:13 -0700964
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800965 final_cmdline = update_cmdline((const char*)cmdline);
966
Neeti Desai17379b82012-06-04 18:42:53 -0700967#if DEVICE_TREE
Amol Jadib6be5c12012-11-14 13:39:51 -0800968 dprintf(INFO, "Updating device tree: start\n");
969
Neeti Desai17379b82012-06-04 18:42:53 -0700970 /* Update the Device Tree */
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530971 ret = update_device_tree((void *)tags,(const char *)final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -0700972 if(ret)
973 {
974 dprintf(CRITICAL, "ERROR: Updating Device Tree Failed \n");
975 ASSERT(0);
976 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800977 dprintf(INFO, "Updating device tree: done\n");
Neeti Desai17379b82012-06-04 18:42:53 -0700978#else
Neeti Desaie245d492012-06-01 12:52:13 -0700979 /* Generating the Atags */
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800980 generate_atags(tags, final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -0700981#endif
Neeti Desaie245d492012-06-01 12:52:13 -0700982
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -0700983#if VERIFIED_BOOT
Monika Singh292b3e92018-03-17 22:40:23 +0530984 if (VB_M == target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +0530985 {
986 if (device.verity_mode == 0) {
lijuangbdd9bb42016-03-01 18:22:17 +0800987#if FBCON_DISPLAY_MSG
Channagoud Kadabi86b0c112016-03-16 19:23:16 -0700988#if ENABLE_VB_ATTEST
Mayank Grover889be1b2017-09-12 20:12:23 +0530989 display_bootverify_menu(DISPLAY_MENU_EIO);
990 wait_for_users_action();
991 if(!pwr_key_is_pressed)
992 shutdown_device();
Channagoud Kadabi86b0c112016-03-16 19:23:16 -0700993#else
Mayank Grover889be1b2017-09-12 20:12:23 +0530994 display_bootverify_menu(DISPLAY_MENU_LOGGING);
Channagoud Kadabi86b0c112016-03-16 19:23:16 -0700995#endif
Mayank Grover889be1b2017-09-12 20:12:23 +0530996 wait_for_users_action();
lijuangbdd9bb42016-03-01 18:22:17 +0800997#else
Mayank Grover889be1b2017-09-12 20:12:23 +0530998 dprintf(CRITICAL,
999 "The dm-verity is not started in enforcing mode.\nWait for 5 seconds before proceeding\n");
1000 mdelay(5000);
lijuangbdd9bb42016-03-01 18:22:17 +08001001#endif
Mayank Grover889be1b2017-09-12 20:12:23 +05301002 }
lijuangbdd9bb42016-03-01 18:22:17 +08001003 }
lijuangbdd9bb42016-03-01 18:22:17 +08001004#endif
1005
1006#if VERIFIED_BOOT
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001007 /* Write protect the device info */
Channagoud Kadabi3bd9d1e2015-05-05 16:18:20 -07001008 if (!boot_into_recovery && target_build_variant_user() && devinfo_present && mmc_write_protect("devinfo", 1))
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001009 {
1010 dprintf(INFO, "Failed to write protect dev info\n");
1011 ASSERT(0);
1012 }
1013#endif
1014
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08001015 /* Turn off splash screen if enabled */
1016#if DISPLAY_SPLASH_SCREEN
1017 target_display_shutdown();
1018#endif
1019
Veera Sundaram Sankaran67ea0932015-09-25 10:09:30 -07001020 /* Perform target specific cleanup */
1021 target_uninit();
Monika Singh292b3e92018-03-17 22:40:23 +05301022 free_verified_boot_resource(&info);
1023 if (final_cmdline)
1024 free(final_cmdline);
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08001025
Deepa Dinamani33734bc2013-03-06 12:16:06 -08001026 dprintf(INFO, "booting linux @ %p, ramdisk @ %p (%d), tags/device tree @ %p\n",
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301027 entry, ramdisk, ramdisk_size, (void *)tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001028
1029 enter_critical_section();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001030
Amol Jadi4421e652011-06-16 15:00:48 -07001031 /* do any platform specific cleanup before kernel entry */
1032 platform_uninit();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001033
Brian Swetland9c4c0752009-01-25 16:23:50 -08001034 arch_disable_cache(UCACHE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001035
Amol Jadi504f9fe2012-08-16 13:56:48 -07001036#if ARM_WITH_MMU
Brian Swetland9c4c0752009-01-25 16:23:50 -08001037 arch_disable_mmu();
Amol Jadi504f9fe2012-08-16 13:56:48 -07001038#endif
Amol Jadi492d5a52013-03-15 16:12:34 -07001039 bs_set_timestamp(BS_KERNEL_ENTRY);
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001040
1041 if (IS_ARM64(kptr))
1042 /* Jump to a 64bit kernel */
1043 scm_elexec_call((paddr_t)kernel, tags_phys);
1044 else
1045 /* Jump to a 32bit kernel */
1046 entry(0, machtype, (unsigned*)tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001047}
1048
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001049/* Function to check if the memory address range falls within the aboot
1050 * boundaries.
1051 * start: Start of the memory region
1052 * size: Size of the memory region
1053 */
Vijay Kumar Pendoti9c002ad2016-03-09 13:52:45 +05301054int check_aboot_addr_range_overlap(uintptr_t start, uint32_t size)
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001055{
1056 /* Check for boundary conditions. */
Sundarajan Srinivasance2a0ea2013-12-16 17:02:56 -08001057 if ((UINT_MAX - start) < size)
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001058 return -1;
1059
1060 /* Check for memory overlap. */
1061 if ((start < MEMBASE) && ((start + size) <= MEMBASE))
1062 return 0;
Channagoud Kadabi94143912013-10-15 12:53:52 -07001063 else if (start >= (MEMBASE + MEMSIZE))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001064 return 0;
1065 else
1066 return -1;
1067}
1068
Mayank Grovere559cec2017-10-17 15:12:03 +05301069/* Function to check if the memory address range falls beyond ddr region.
1070 * start: Start of the memory region
1071 * size: Size of the memory region
1072 */
1073int check_ddr_addr_range_bound(uintptr_t start, uint32_t size)
1074{
1075 uintptr_t ddr_pa_start_addr = PA(get_ddr_start());
1076 uint64_t ddr_size = smem_get_ddr_size();
1077 uint64_t ddr_pa_end_addr = ddr_pa_start_addr + ddr_size;
1078 uintptr_t pa_start_addr = PA(start);
1079
1080 /* Check for boundary conditions. */
1081 if ((UINT_MAX - start) < size)
1082 return -1;
1083
1084 /* Check if memory range is beyond the ddr range. */
1085 if (pa_start_addr < ddr_pa_start_addr ||
1086 pa_start_addr >= (ddr_pa_end_addr) ||
1087 (pa_start_addr + size) > ddr_pa_end_addr)
1088 return -1;
1089 else
1090 return 0;
1091}
1092
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001093BUF_DMA_ALIGN(buf, BOOT_IMG_MAX_PAGE_SIZE); //Equal to max-supported pagesize
Dima Zavin214cc642009-01-26 11:16:21 -08001094
Monika Singh292b3e92018-03-17 22:40:23 +05301095int getimage(const bootinfo *info, void **image_buffer, uint32_t *imgsize,
1096 char *imgname)
1097{
1098 if (info == NULL || image_buffer == NULL || imgsize == NULL ||
1099 imgname == NULL) {
1100 dprintf(CRITICAL, "getimage: invalid parameters\n");
1101 return -1;
1102 }
1103
1104 for (uint32_t loadedindex = 0; loadedindex < info->num_loaded_images; loadedindex++) {
1105 if (!strncmp(info->images[loadedindex].name, imgname,
1106 strlen(imgname))) {
1107 *image_buffer = info->images[loadedindex].image_buffer;
1108 *imgsize = info->images[loadedindex].imgsize;
1109 return 0;
1110 }
1111 }
1112 return -1;
1113}
1114
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001115static void verify_signed_bootimg(uint32_t bootimg_addr, uint32_t bootimg_size)
1116{
1117 int ret;
Channagoud Kadabia8c623f2015-01-13 14:48:48 -08001118
1119#if !VERIFIED_BOOT
Sundarajan Srinivasance54d6e2013-11-11 12:45:00 -08001120#if IMAGE_VERIF_ALGO_SHA1
1121 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA1;
1122#else
1123 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA256;
1124#endif
Channagoud Kadabia8c623f2015-01-13 14:48:48 -08001125#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001126
1127 /* Assume device is rooted at this time. */
1128 device.is_tampered = 1;
1129
1130 dprintf(INFO, "Authenticating boot image (%d): start\n", bootimg_size);
1131
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001132#if VERIFIED_BOOT
Monika Singh7d2fc272018-03-16 17:16:01 +05301133 uint32_t bootstate;
1134 if(boot_into_recovery &&
Mayank Grover68fbf0d2017-10-24 14:13:39 +05301135 (!partition_multislot_is_supported()))
Monika Singh7d2fc272018-03-16 17:16:01 +05301136 {
1137 ret = boot_verify_image((unsigned char *)bootimg_addr,
1138 bootimg_size, "/recovery", &bootstate);
1139 }
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001140 else
Monika Singh7d2fc272018-03-16 17:16:01 +05301141 {
1142 ret = boot_verify_image((unsigned char *)bootimg_addr,
1143 bootimg_size, "/boot", &bootstate);
1144 }
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001145 boot_verify_print_state();
1146#else
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001147 ret = image_verify((unsigned char *)bootimg_addr,
1148 (unsigned char *)(bootimg_addr + bootimg_size),
1149 bootimg_size,
Sundarajan Srinivasance54d6e2013-11-11 12:45:00 -08001150 auth_algo);
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001151#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001152 dprintf(INFO, "Authenticating boot image: done return value = %d\n", ret);
1153
1154 if (ret)
1155 {
1156 /* Authorized kernel */
1157 device.is_tampered = 0;
Sundarajan Srinivasan3fb21f12013-09-16 18:36:15 -07001158 auth_kernel_img = 1;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001159 }
1160
Amit Blay4aa292f2015-04-28 21:55:59 +03001161#ifdef MDTP_SUPPORT
1162 {
1163 /* Verify MDTP lock.
1164 * For boot & recovery partitions, use aboot's verification result.
1165 */
1166 mdtp_ext_partition_verification_t ext_partition;
1167 ext_partition.partition = boot_into_recovery ? MDTP_PARTITION_RECOVERY : MDTP_PARTITION_BOOT;
1168 ext_partition.integrity_state = device.is_tampered ? MDTP_PARTITION_STATE_INVALID : MDTP_PARTITION_STATE_VALID;
1169 ext_partition.page_size = 0; /* Not needed since already validated */
1170 ext_partition.image_addr = 0; /* Not needed since already validated */
1171 ext_partition.image_size = 0; /* Not needed since already validated */
1172 ext_partition.sig_avail = FALSE; /* Not needed since already validated */
1173 mdtp_fwlock_verify_lock(&ext_partition);
1174 }
1175#endif /* MDTP_SUPPORT */
1176
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001177#if USE_PCOM_SECBOOT
1178 set_tamper_flag(device.is_tampered);
1179#endif
1180
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001181#if VERIFIED_BOOT
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001182 switch(boot_verify_get_state())
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001183 {
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001184 case RED:
lijuanga40d6302015-07-20 20:10:13 +08001185#if FBCON_DISPLAY_MSG
lijuang9a7d3b92015-11-30 14:41:24 +08001186 display_bootverify_menu(DISPLAY_MENU_RED);
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07001187#if ENABLE_VB_ATTEST
1188 mdelay(DELAY_WAIT);
1189 shutdown_device();
1190#else
lijuanga40d6302015-07-20 20:10:13 +08001191 wait_for_users_action();
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07001192#endif
lijuanga40d6302015-07-20 20:10:13 +08001193#else
1194 dprintf(CRITICAL,
1195 "Your device has failed verification and may not work properly.\nWait for 5 seconds before proceeding\n");
1196 mdelay(5000);
1197#endif
1198
1199 break;
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001200 case YELLOW:
lijuanga40d6302015-07-20 20:10:13 +08001201#if FBCON_DISPLAY_MSG
lijuang9a7d3b92015-11-30 14:41:24 +08001202 display_bootverify_menu(DISPLAY_MENU_YELLOW);
lijuanga40d6302015-07-20 20:10:13 +08001203 wait_for_users_action();
1204#else
1205 dprintf(CRITICAL,
1206 "Your device has loaded a different operating system.\nWait for 5 seconds before proceeding\n");
1207 mdelay(5000);
1208#endif
1209 break;
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001210 default:
lijuanga40d6302015-07-20 20:10:13 +08001211 break;
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001212 }
1213#endif
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001214#if !VERIFIED_BOOT
Unnati Gandhi1be04752015-03-27 19:41:53 +05301215 if(device.is_tampered)
1216 {
1217 write_device_info_mmc(&device);
1218 #ifdef TZ_TAMPER_FUSE
1219 set_tamper_fuse_cmd();
1220 #endif
1221 #ifdef ASSERT_ON_TAMPER
1222 dprintf(CRITICAL, "Device is tampered. Asserting..\n");
1223 ASSERT(0);
1224 #endif
1225 }
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001226#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001227}
1228
Monika Singh292b3e92018-03-17 22:40:23 +05301229int get_boot_image_info(void **image_buffer, uint32_t *imgsize,char *imgname)
1230{
1231 if (image_buffer == NULL || imgsize == NULL || imgname == NULL) {
1232 dprintf(CRITICAL, "get_boot_image_info: invalid parameters\n");
1233 return -1;
1234 }
1235
1236 if (!strncmp(info.images[0].name, imgname,
1237 strlen(imgname))) {
1238 *image_buffer = info.images[0].image_buffer;
1239 *imgsize = info.images[0].imgsize;
1240 return 0;
1241 }
1242 return -1;
1243}
1244
Maunik Shah0f3c8ac2014-03-02 17:47:58 +05301245static bool check_format_bit()
1246{
1247 bool ret = false;
1248 int index;
1249 uint64_t offset;
1250 struct boot_selection_info *in = NULL;
1251 char *buf = NULL;
1252
1253 index = partition_get_index("bootselect");
1254 if (index == INVALID_PTN)
1255 {
1256 dprintf(INFO, "Unable to locate /bootselect partition\n");
1257 return ret;
1258 }
1259 offset = partition_get_offset(index);
1260 if(!offset)
1261 {
1262 dprintf(INFO, "partition /bootselect doesn't exist\n");
1263 return ret;
1264 }
1265 buf = (char *) memalign(CACHE_LINE, ROUNDUP(page_size, CACHE_LINE));
Mayank Grover48860402016-11-29 12:34:53 +05301266 mmc_set_lun(partition_get_lun(index));
Maunik Shah0f3c8ac2014-03-02 17:47:58 +05301267 ASSERT(buf);
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301268 if (mmc_read(offset, (uint32_t *)buf, page_size))
Maunik Shah0f3c8ac2014-03-02 17:47:58 +05301269 {
1270 dprintf(INFO, "mmc read failure /bootselect %d\n", page_size);
1271 free(buf);
1272 return ret;
1273 }
1274 in = (struct boot_selection_info *) buf;
1275 if ((in->signature == BOOTSELECT_SIGNATURE) &&
1276 (in->version == BOOTSELECT_VERSION)) {
1277 if ((in->state_info & BOOTSELECT_FORMAT) &&
1278 !(in->state_info & BOOTSELECT_FACTORY))
1279 ret = true;
1280 } else {
1281 dprintf(CRITICAL, "Signature: 0x%08x or version: 0x%08x mismatched of /bootselect\n",
1282 in->signature, in->version);
1283 ASSERT(0);
1284 }
1285 free(buf);
1286 return ret;
1287}
1288
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001289void boot_verifier_init()
1290{
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001291 uint32_t boot_state;
1292 /* Check if device unlock */
1293 if(device.is_unlocked)
1294 {
1295 boot_verify_send_event(DEV_UNLOCK);
1296 boot_verify_print_state();
1297 dprintf(CRITICAL, "Device is unlocked! Skipping verification...\n");
1298 return;
1299 }
1300 else
1301 {
1302 boot_verify_send_event(BOOT_INIT);
1303 }
1304
1305 /* Initialize keystore */
1306 boot_state = boot_verify_keystore_init();
1307 if(boot_state == YELLOW)
1308 {
1309 boot_verify_print_state();
1310 dprintf(CRITICAL, "Keystore verification failed! Continuing anyways...\n");
1311 }
1312}
1313
Shashank Mittal23b8f422010-04-16 19:27:21 -07001314int boot_linux_from_mmc(void)
1315{
1316 struct boot_img_hdr *hdr = (void*) buf;
1317 struct boot_img_hdr *uhdr;
1318 unsigned offset = 0;
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07001319 int rcode;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001320 unsigned long long ptn = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001321 int index = INVALID_PTN;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001322
Shashank Mittalcd98d472011-08-02 14:29:24 -07001323 unsigned char *image_addr = 0;
1324 unsigned kernel_actual;
1325 unsigned ramdisk_actual;
1326 unsigned imagesize_actual;
Neeti Desai465491e2012-07-31 12:53:35 -07001327 unsigned second_actual = 0;
Neeti Desai465491e2012-07-31 12:53:35 -07001328
Matthew Qin271927e2015-03-31 22:07:07 -04001329 unsigned int dtb_size = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08001330 unsigned int out_len = 0;
1331 unsigned int out_avai_len = 0;
1332 unsigned char *out_addr = NULL;
1333 uint32_t dtb_offset = 0;
1334 unsigned char *kernel_start_addr = NULL;
1335 unsigned int kernel_size = 0;
Ameya Thakur10a33452016-06-13 14:24:26 -07001336 unsigned int patched_kernel_hdr_size = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08001337 int rc;
Monika Singh292b3e92018-03-17 22:40:23 +05301338#if VERIFIED_BOOT_2
1339 int status;
1340#endif
Mayank Groverc93cad82017-10-03 12:23:45 +05301341 char *ptn_name = NULL;
Neeti Desai465491e2012-07-31 12:53:35 -07001342#if DEVICE_TREE
1343 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -07001344 struct dt_entry dt_entry;
Neeti Desai465491e2012-07-31 12:53:35 -07001345 unsigned dt_table_offset;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001346 uint32_t dt_actual;
Deepa Dinamani19648b42013-09-05 17:05:55 -07001347 uint32_t dt_hdr_size;
Matthew Qin271927e2015-03-31 22:07:07 -04001348 unsigned char *best_match_dt_addr = NULL;
Neeti Desai465491e2012-07-31 12:53:35 -07001349#endif
Matthew Qin49e51fa2015-02-09 10:40:45 +08001350 struct kernel64_hdr *kptr = NULL;
Mayank Grover351a75e2017-05-30 20:06:08 +05301351 int current_active_slot = INVALID;
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001352
Maunik Shah0f3c8ac2014-03-02 17:47:58 +05301353 if (check_format_bit())
1354 boot_into_recovery = 1;
1355
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07001356 if (!boot_into_recovery) {
1357 memset(ffbm_mode_string, '\0', sizeof(ffbm_mode_string));
1358 rcode = get_ffbm(ffbm_mode_string, sizeof(ffbm_mode_string));
1359 if (rcode <= 0) {
1360 boot_into_ffbm = false;
1361 if (rcode < 0)
1362 dprintf(CRITICAL,"failed to get ffbm cookie");
1363 } else
1364 boot_into_ffbm = true;
1365 } else
1366 boot_into_ffbm = false;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001367 uhdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
1368 if (!memcmp(uhdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1369 dprintf(INFO, "Unified boot method!\n");
1370 hdr = uhdr;
1371 goto unified_boot;
1372 }
Mayank Groverc93cad82017-10-03 12:23:45 +05301373
1374 /* For a/b recovery image code is on boot partition.
1375 If we support multislot, always use boot partition. */
1376 if (boot_into_recovery &&
1377 (!partition_multislot_is_supported()))
1378 ptn_name = "recovery";
1379 else
1380 ptn_name = "boot";
1381
1382 index = partition_get_index(ptn_name);
1383 ptn = partition_get_offset(index);
1384 if(ptn == 0) {
1385 dprintf(CRITICAL, "ERROR: No %s partition found\n", ptn_name);
1386 return -1;
Kinson Chikf1a43512011-07-14 11:28:39 -07001387 }
Mayank Groverc93cad82017-10-03 12:23:45 +05301388
Channagoud Kadabief0547c2015-02-10 12:57:38 -08001389 /* Set Lun for boot & recovery partitions */
1390 mmc_set_lun(partition_get_lun(index));
Shashank Mittal23b8f422010-04-16 19:27:21 -07001391
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301392 if (mmc_read(ptn + offset, (uint32_t *) buf, page_size)) {
Shashank Mittal23b8f422010-04-16 19:27:21 -07001393 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
1394 return -1;
1395 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001396
1397 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001398 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Mayank Grover351a75e2017-05-30 20:06:08 +05301399 return ERR_INVALID_BOOT_MAGIC;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001400 }
1401
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07001402 if (hdr->page_size && (hdr->page_size != page_size)) {
vijay kumar2e21b3a2014-06-26 17:40:15 +05301403
1404 if (hdr->page_size > BOOT_IMG_MAX_PAGE_SIZE) {
1405 dprintf(CRITICAL, "ERROR: Invalid page size\n");
1406 return -1;
1407 }
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07001408 page_size = hdr->page_size;
1409 page_mask = page_size - 1;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001410 }
1411
Matthew Qin49e51fa2015-02-09 10:40:45 +08001412 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1413 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Kishor PKd8ddcad2017-07-27 13:53:57 +05301414 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001415
Matthew Qin49e51fa2015-02-09 10:40:45 +08001416 image_addr = (unsigned char *)target_get_scratch_address();
Kishor PK9e91b592017-05-24 12:14:55 +05301417 memcpy(image_addr, (void *)buf, page_size);
1418
1419 /* ensure commandline is terminated */
1420 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
Matthew Qin49e51fa2015-02-09 10:40:45 +08001421
1422#if DEVICE_TREE
Parth Dixit4097b622016-03-15 14:42:27 +05301423#ifndef OSVERSION_IN_BOOTIMAGE
1424 dt_size = hdr->dt_size;
1425#endif
1426 dt_actual = ROUND_TO_PAGE(dt_size, page_mask);
Kishor PKd8ddcad2017-07-27 13:53:57 +05301427 if (UINT_MAX < ((uint64_t)kernel_actual + (uint64_t)ramdisk_actual+ (uint64_t)second_actual + (uint64_t)dt_actual + page_size)) {
Vijay Kumar Pendoti208f9622016-06-09 19:34:01 +05301428 dprintf(CRITICAL, "Integer overflow detected in bootimage header fields at %u in %s\n",__LINE__,__FILE__);
1429 return -1;
1430 }
Kishor PKd8ddcad2017-07-27 13:53:57 +05301431 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + second_actual + dt_actual);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001432#else
Kishor PKd8ddcad2017-07-27 13:53:57 +05301433 if (UINT_MAX < ((uint64_t)kernel_actual + (uint64_t)ramdisk_actual + (uint64_t)second_actual + page_size)) {
Vijay Kumar Pendoti208f9622016-06-09 19:34:01 +05301434 dprintf(CRITICAL, "Integer overflow detected in bootimage header fields at %u in %s\n",__LINE__,__FILE__);
1435 return -1;
1436 }
Kishor PKd8ddcad2017-07-27 13:53:57 +05301437 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + second_actual);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001438#endif
1439
1440#if VERIFIED_BOOT
1441 boot_verifier_init();
1442#endif
1443
Vijay Kumar Pendoti9c002ad2016-03-09 13:52:45 +05301444 if (check_aboot_addr_range_overlap((uintptr_t) image_addr, imagesize_actual))
Matthew Qin49e51fa2015-02-09 10:40:45 +08001445 {
1446 dprintf(CRITICAL, "Boot image buffer address overlaps with aboot addresses.\n");
1447 return -1;
1448 }
1449
Matthew Qinbb7923d2015-02-09 10:56:09 +08001450 /*
1451 * Update loading flow of bootimage to support compressed/uncompressed
1452 * bootimage on both 64bit and 32bit platform.
1453 * 1. Load bootimage from emmc partition onto DDR.
1454 * 2. Check if bootimage is gzip format. If yes, decompress compressed kernel
1455 * 3. Check kernel header and update kernel load addr for 64bit and 32bit
1456 * platform accordingly.
1457 * 4. Sanity Check on kernel_addr and ramdisk_addr and copy data.
1458 */
Mayank Grover351a75e2017-05-30 20:06:08 +05301459 if (partition_multislot_is_supported())
1460 {
1461 current_active_slot = partition_find_active_slot();
1462 dprintf(INFO, "Loading boot image (%d) active_slot(%s): start\n",
1463 imagesize_actual, SUFFIX_SLOT(current_active_slot));
1464 }
1465 else
1466 {
1467 dprintf(INFO, "Loading (%s) image (%d): start\n",
1468 (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual);
1469 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08001470 bs_set_timestamp(BS_KERNEL_LOAD_START);
1471
Gaurav Nebhwani43e2a462016-03-17 21:32:56 +05301472 if ((target_get_max_flash_size() - page_size) < imagesize_actual)
1473 {
1474 dprintf(CRITICAL, "booimage size is greater than DDR can hold\n");
1475 return -1;
1476 }
Kishor PK9e91b592017-05-24 12:14:55 +05301477 offset = page_size;
1478 /* Read image without signature and header*/
1479 if (mmc_read(ptn + offset, (void *)(image_addr + offset), imagesize_actual - page_size))
Matthew Qin49e51fa2015-02-09 10:40:45 +08001480 {
1481 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
1482 return -1;
1483 }
1484
Mayank Grover351a75e2017-05-30 20:06:08 +05301485 if (partition_multislot_is_supported())
1486 {
1487 dprintf(INFO, "Loading boot image (%d) active_slot(%s): done\n",
1488 imagesize_actual, SUFFIX_SLOT(current_active_slot));
1489 }
1490 else
1491 {
1492 dprintf(INFO, "Loading (%s) image (%d): done\n",
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001493 (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual);
1494
Mayank Grover351a75e2017-05-30 20:06:08 +05301495 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08001496 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
1497
1498 /* Authenticate Kernel */
1499 dprintf(INFO, "use_signed_kernel=%d, is_unlocked=%d, is_tampered=%d.\n",
1500 (int) target_use_signed_kernel(),
1501 device.is_unlocked,
1502 device.is_tampered);
Monika Singh292b3e92018-03-17 22:40:23 +05301503#if VERIFIED_BOOT_2
1504 offset = imagesize_actual;
1505 if (check_aboot_addr_range_overlap((uintptr_t)image_addr + offset, page_size))
1506 {
1507 dprintf(CRITICAL, "Signature read buffer address overlaps with aboot addresses.\n");
1508 return -1;
1509 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08001510
Monika Singh292b3e92018-03-17 22:40:23 +05301511 /* Read signature */
1512 if(mmc_read(ptn + offset, (void *)(image_addr + offset), page_size))
1513 {
1514 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
1515 return -1;
1516 }
1517
1518 memset(&info, 0, sizeof(bootinfo));
1519 info.images[0].image_buffer = image_addr;
1520 info.images[0].imgsize = imagesize_actual;
1521 info.images[0].name = "boot";
1522 info.num_loaded_images = 0;
1523 info.multi_slot_boot = partition_multislot_is_supported();
1524 info.bootreason_alarm = boot_reason_alarm;
1525 info.bootinto_recovery = boot_into_recovery;
1526 status = load_image_and_auth(&info);
1527 if(status)
1528 return -1;
1529
1530 vbcmdline = info.vbcmdline;
1531#else
Channagoud Kadabi736c4962015-08-21 11:56:52 -07001532 /* Change the condition a little bit to include the test framework support.
1533 * We would never reach this point if device is in fastboot mode, even if we did
1534 * that means we are in test mode, so execute kernel authentication part for the
1535 * tests */
Parth Dixitcb0c6082015-12-30 15:01:13 +05301536 if((target_use_signed_kernel() && (!device.is_unlocked)) || is_test_mode_enabled())
Matthew Qin49e51fa2015-02-09 10:40:45 +08001537 {
1538 offset = imagesize_actual;
Vijay Kumar Pendoti9c002ad2016-03-09 13:52:45 +05301539 if (check_aboot_addr_range_overlap((uintptr_t)image_addr + offset, page_size))
Matthew Qin49e51fa2015-02-09 10:40:45 +08001540 {
1541 dprintf(CRITICAL, "Signature read buffer address overlaps with aboot addresses.\n");
1542 return -1;
1543 }
1544
1545 /* Read signature */
1546 if(mmc_read(ptn + offset, (void *)(image_addr + offset), page_size))
1547 {
1548 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
1549 return -1;
1550 }
1551
1552 verify_signed_bootimg((uint32_t)image_addr, imagesize_actual);
Channagoud Kadabi736c4962015-08-21 11:56:52 -07001553 /* The purpose of our test is done here */
Parth Dixitcb0c6082015-12-30 15:01:13 +05301554 if(is_test_mode_enabled() && auth_kernel_img)
Channagoud Kadabi736c4962015-08-21 11:56:52 -07001555 return 0;
Matthew Qin49e51fa2015-02-09 10:40:45 +08001556 } else {
1557 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
1558 #ifdef TZ_SAVE_KERNEL_HASH
1559 aboot_save_boot_hash_mmc((uint32_t) image_addr, imagesize_actual);
1560 #endif /* TZ_SAVE_KERNEL_HASH */
Amit Blay4aa292f2015-04-28 21:55:59 +03001561
1562#ifdef MDTP_SUPPORT
1563 {
1564 /* Verify MDTP lock.
1565 * For boot & recovery partitions, MDTP will use boot_verifier APIs,
1566 * since verification was skipped in aboot. The signature is not part of the loaded image.
1567 */
1568 mdtp_ext_partition_verification_t ext_partition;
1569 ext_partition.partition = boot_into_recovery ? MDTP_PARTITION_RECOVERY : MDTP_PARTITION_BOOT;
1570 ext_partition.integrity_state = MDTP_PARTITION_STATE_UNSET;
1571 ext_partition.page_size = page_size;
1572 ext_partition.image_addr = (uint32)image_addr;
1573 ext_partition.image_size = imagesize_actual;
1574 ext_partition.sig_avail = FALSE;
1575 mdtp_fwlock_verify_lock(&ext_partition);
1576 }
1577#endif /* MDTP_SUPPORT */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001578 }
Monika Singh292b3e92018-03-17 22:40:23 +05301579#endif
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001580
Sridhar Parasuram00bfedb2015-05-26 14:21:27 -07001581#if VERIFIED_BOOT
taozhang93088bd2016-11-16 15:50:46 +08001582 if((boot_verify_get_state() == ORANGE) && (!boot_into_ffbm))
Reut Zysmanba8a9d52016-02-18 11:44:04 +02001583 {
1584#if FBCON_DISPLAY_MSG
1585 display_bootverify_menu(DISPLAY_MENU_ORANGE);
1586 wait_for_users_action();
1587#else
1588 dprintf(CRITICAL,
1589 "Your device has been unlocked and can't be trusted.\nWait for 5 seconds before proceeding\n");
1590 mdelay(5000);
1591#endif
1592 }
1593#endif
1594
1595#if VERIFIED_BOOT
Monika Singh292b3e92018-03-17 22:40:23 +05301596 if (VB_M == target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +05301597 {
1598 /* set boot and system versions. */
1599 set_os_version((unsigned char *)image_addr);
1600 // send root of trust
1601 if(!send_rot_command((uint32_t)device.is_unlocked))
1602 ASSERT(0);
1603 }
Parth Dixitbc9b6492015-10-18 00:41:38 +05301604#endif
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001605 /*
Matthew Qinbb7923d2015-02-09 10:56:09 +08001606 * Check if the kernel image is a gzip package. If yes, need to decompress it.
1607 * If not, continue booting.
1608 */
1609 if (is_gzip_package((unsigned char *)(image_addr + page_size), hdr->kernel_size))
1610 {
1611 out_addr = (unsigned char *)(image_addr + imagesize_actual + page_size);
1612 out_avai_len = target_get_max_flash_size() - imagesize_actual - page_size;
Matthew Qin0b15b322015-05-19 05:20:54 -04001613 dprintf(INFO, "decompressing kernel image: start\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001614 rc = decompress((unsigned char *)(image_addr + page_size),
1615 hdr->kernel_size, out_addr, out_avai_len,
1616 &dtb_offset, &out_len);
1617 if (rc)
1618 {
Matthew Qin0b15b322015-05-19 05:20:54 -04001619 dprintf(CRITICAL, "decompressing kernel image failed!!!\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001620 ASSERT(0);
1621 }
1622
Matthew Qin0b15b322015-05-19 05:20:54 -04001623 dprintf(INFO, "decompressing kernel image: done\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001624 kptr = (struct kernel64_hdr *)out_addr;
1625 kernel_start_addr = out_addr;
1626 kernel_size = out_len;
1627 } else {
Ameya Thakur10a33452016-06-13 14:24:26 -07001628 dprintf(INFO, "Uncpmpressed kernel in use\n");
1629 if (!strncmp((char*)(image_addr + page_size),
1630 PATCHED_KERNEL_MAGIC,
1631 sizeof(PATCHED_KERNEL_MAGIC) - 1)) {
1632 dprintf(INFO, "Patched kernel detected\n");
1633 kptr = (struct kernel64_hdr *)(image_addr + page_size +
1634 PATCHED_KERNEL_HEADER_SIZE);
1635 //The size of the kernel is stored at start of kernel image + 16
1636 //The dtb would start just after the kernel
1637 dtb_offset = *((uint32_t*)((unsigned char*)
1638 (image_addr + page_size +
1639 sizeof(PATCHED_KERNEL_MAGIC) -
1640 1)));
1641 //The actual kernel starts after the 20 byte header.
1642 kernel_start_addr = (unsigned char*)(image_addr +
1643 page_size + PATCHED_KERNEL_HEADER_SIZE);
1644 kernel_size = hdr->kernel_size;
1645 patched_kernel_hdr_size = PATCHED_KERNEL_HEADER_SIZE;
1646 } else {
1647 dprintf(INFO, "Kernel image not patched..Unable to locate dt offset\n");
1648 kptr = (struct kernel64_hdr *)(image_addr + page_size);
1649 kernel_start_addr = (unsigned char *)(image_addr + page_size);
1650 kernel_size = hdr->kernel_size;
1651 }
Matthew Qinbb7923d2015-02-09 10:56:09 +08001652 }
1653
1654 /*
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001655 * Update the kernel/ramdisk/tags address if the boot image header
1656 * has default values, these default values come from mkbootimg when
1657 * the boot image is flashed using fastboot flash:raw
1658 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001659 update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr));
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001660
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001661 /* Get virtual addresses since the hdr saves physical addresses. */
1662 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
1663 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
1664 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001665
Matthew Qinbb7923d2015-02-09 10:56:09 +08001666 kernel_size = ROUND_TO_PAGE(kernel_size, page_mask);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001667 /* Check if the addresses in the header are valid. */
Matthew Qinbb7923d2015-02-09 10:56:09 +08001668 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_size) ||
Mayank Grovere559cec2017-10-17 15:12:03 +05301669 check_ddr_addr_range_bound(hdr->kernel_addr, kernel_size) ||
1670 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual) ||
1671 check_ddr_addr_range_bound(hdr->ramdisk_addr, ramdisk_actual))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001672 {
Mayank Grovere559cec2017-10-17 15:12:03 +05301673 dprintf(CRITICAL, "kernel/ramdisk addresses are not valid.\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001674 return -1;
1675 }
1676
1677#ifndef DEVICE_TREE
Mayank Grovere559cec2017-10-17 15:12:03 +05301678 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE) ||
1679 check_ddr_addr_range_bound(hdr->tags_addr, MAX_TAGS_SIZE))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001680 {
Mayank Grovere559cec2017-10-17 15:12:03 +05301681 dprintf(CRITICAL, "Tags addresses are not valid.\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001682 return -1;
1683 }
1684#endif
1685
Matthew Qin49e51fa2015-02-09 10:40:45 +08001686 /* Move kernel, ramdisk and device tree to correct address */
Matthew Qinbb7923d2015-02-09 10:56:09 +08001687 memmove((void*) hdr->kernel_addr, kernel_start_addr, kernel_size);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001688 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001689
Matthew Qin49e51fa2015-02-09 10:40:45 +08001690 #if DEVICE_TREE
Parth Dixit4097b622016-03-15 14:42:27 +05301691 if(dt_size) {
Matthew Qin49e51fa2015-02-09 10:40:45 +08001692 dt_table_offset = ((uint32_t)image_addr + page_size + kernel_actual + ramdisk_actual + second_actual);
1693 table = (struct dt_table*) dt_table_offset;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001694
Matthew Qin49e51fa2015-02-09 10:40:45 +08001695 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
1696 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1697 return -1;
1698 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001699
P.V. Phani Kumar82916762016-03-09 09:20:19 +05301700 /* Its Error if, dt_hdr_size (table->num_entries * dt_entry size + Dev_Tree Header)
1701 goes beyound hdr->dt_size*/
Parth Dixit4097b622016-03-15 14:42:27 +05301702 if (dt_hdr_size > ROUND_TO_PAGE(dt_size,hdr->page_size)) {
P.V. Phani Kumar82916762016-03-09 09:20:19 +05301703 dprintf(CRITICAL, "ERROR: Invalid Device Tree size \n");
1704 return -1;
1705 }
1706
Matthew Qin49e51fa2015-02-09 10:40:45 +08001707 /* Find index of device tree within device tree table */
1708 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
1709 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1710 return -1;
1711 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001712
P.V. Phani Kumar3c333202016-03-09 11:54:37 +05301713 if(dt_entry.offset > (UINT_MAX - dt_entry.size)) {
1714 dprintf(CRITICAL, "ERROR: Device tree contents are Invalid\n");
1715 return -1;
1716 }
1717
1718 /* Ensure we are not overshooting dt_size with the dt_entry selected */
Parth Dixit4097b622016-03-15 14:42:27 +05301719 if ((dt_entry.offset + dt_entry.size) > dt_size) {
P.V. Phani Kumar3c333202016-03-09 11:54:37 +05301720 dprintf(CRITICAL, "ERROR: Device tree contents are Invalid\n");
1721 return -1;
1722 }
1723
Matthew Qin271927e2015-03-31 22:07:07 -04001724 if (is_gzip_package((unsigned char *)dt_table_offset + dt_entry.offset, dt_entry.size))
1725 {
1726 unsigned int compressed_size = 0;
1727 out_addr += out_len;
1728 out_avai_len -= out_len;
Matthew Qin0b15b322015-05-19 05:20:54 -04001729 dprintf(INFO, "decompressing dtb: start\n");
Matthew Qin271927e2015-03-31 22:07:07 -04001730 rc = decompress((unsigned char *)dt_table_offset + dt_entry.offset,
1731 dt_entry.size, out_addr, out_avai_len,
1732 &compressed_size, &dtb_size);
1733 if (rc)
1734 {
Matthew Qin0b15b322015-05-19 05:20:54 -04001735 dprintf(CRITICAL, "decompressing dtb failed!!!\n");
Matthew Qin271927e2015-03-31 22:07:07 -04001736 ASSERT(0);
1737 }
1738
Matthew Qin0b15b322015-05-19 05:20:54 -04001739 dprintf(INFO, "decompressing dtb: done\n");
Matthew Qin271927e2015-03-31 22:07:07 -04001740 best_match_dt_addr = out_addr;
1741 } else {
1742 best_match_dt_addr = (unsigned char *)dt_table_offset + dt_entry.offset;
1743 dtb_size = dt_entry.size;
1744 }
1745
Matthew Qin49e51fa2015-02-09 10:40:45 +08001746 /* Validate and Read device device tree in the tags_addr */
Mayank Grovere559cec2017-10-17 15:12:03 +05301747 if (check_aboot_addr_range_overlap(hdr->tags_addr, dtb_size) ||
1748 check_ddr_addr_range_bound(hdr->tags_addr, dtb_size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001749 {
Mayank Grovere559cec2017-10-17 15:12:03 +05301750 dprintf(CRITICAL, "Device tree addresses are not valid\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001751 return -1;
1752 }
Shashank Mittal162244e2011-08-08 19:01:25 -07001753
Matthew Qin271927e2015-03-31 22:07:07 -04001754 memmove((void *)hdr->tags_addr, (char *)best_match_dt_addr, dtb_size);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001755 } else {
1756 /* Validate the tags_addr */
Mayank Grovere559cec2017-10-17 15:12:03 +05301757 if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual) ||
1758 check_ddr_addr_range_bound(hdr->tags_addr, kernel_actual))
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001759 {
Mayank Grovere559cec2017-10-17 15:12:03 +05301760 dprintf(CRITICAL, "Device tree addresses are not valid.\n");
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001761 return -1;
1762 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08001763 /*
1764 * If appended dev tree is found, update the atags with
1765 * memory address to the DTB appended location on RAM.
1766 * Else update with the atags address in the kernel header
1767 */
1768 void *dtb;
Ameya Thakur10a33452016-06-13 14:24:26 -07001769 dtb = dev_tree_appended(
1770 (void*)(image_addr + page_size +
1771 patched_kernel_hdr_size),
1772 hdr->kernel_size, dtb_offset,
1773 (void *)hdr->tags_addr);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001774 if (!dtb) {
1775 dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001776 return -1;
1777 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07001778 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08001779 #endif
Shashank Mittal23b8f422010-04-16 19:27:21 -07001780
Stanimir Varbanov69ec5462013-07-18 18:17:42 +03001781 if (boot_into_recovery && !device.is_unlocked && !device.is_tampered)
1782 target_load_ssd_keystore();
1783
Shashank Mittal23b8f422010-04-16 19:27:21 -07001784unified_boot:
Shashank Mittal23b8f422010-04-16 19:27:21 -07001785
Dima Zavin77e41f32013-03-06 16:10:43 -08001786 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07001787 (const char *)hdr->cmdline, board_machtype(),
Shashank Mittal23b8f422010-04-16 19:27:21 -07001788 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
1789
1790 return 0;
1791}
1792
Dima Zavin214cc642009-01-26 11:16:21 -08001793int boot_linux_from_flash(void)
1794{
1795 struct boot_img_hdr *hdr = (void*) buf;
Dima Zavin214cc642009-01-26 11:16:21 -08001796 struct ptentry *ptn;
1797 struct ptable *ptable;
1798 unsigned offset = 0;
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001799
Shashank Mittalcd98d472011-08-02 14:29:24 -07001800 unsigned char *image_addr = 0;
1801 unsigned kernel_actual;
1802 unsigned ramdisk_actual;
1803 unsigned imagesize_actual;
vijay kumar8f53e362015-11-24 13:38:11 +05301804 unsigned second_actual = 0;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001805
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001806#if DEVICE_TREE
Monika Singh292b3e92018-03-17 22:40:23 +05301807 struct dt_table *table = NULL;
Joel Kingaa335dc2013-06-03 16:11:08 -07001808 struct dt_entry dt_entry;
vijay kumar8f53e362015-11-24 13:38:11 +05301809 unsigned dt_table_offset;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001810 uint32_t dt_actual;
Monika Singh292b3e92018-03-17 22:40:23 +05301811 uint32_t dt_hdr_size = 0;
vijay kumar8f53e362015-11-24 13:38:11 +05301812 unsigned int dtb_size = 0;
1813 unsigned char *best_match_dt_addr = NULL;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001814#endif
1815
David Ng183a7422009-12-07 14:55:21 -08001816 if (target_is_emmc_boot()) {
1817 hdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
1818 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1819 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
1820 return -1;
1821 }
1822 goto continue_boot;
1823 }
1824
Dima Zavin214cc642009-01-26 11:16:21 -08001825 ptable = flash_get_ptable();
1826 if (ptable == NULL) {
1827 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1828 return -1;
1829 }
1830
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001831 if(!boot_into_recovery)
1832 {
1833 ptn = ptable_find(ptable, "boot");
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001834
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001835 if (ptn == NULL) {
1836 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1837 return -1;
1838 }
1839 }
1840 else
1841 {
1842 ptn = ptable_find(ptable, "recovery");
1843 if (ptn == NULL) {
1844 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
1845 return -1;
1846 }
Dima Zavin214cc642009-01-26 11:16:21 -08001847 }
1848
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05301849 /* Read boot.img header from flash */
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001850 if (flash_read(ptn, offset, buf, page_size)) {
Dima Zavin214cc642009-01-26 11:16:21 -08001851 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
1852 return -1;
1853 }
Dima Zavin214cc642009-01-26 11:16:21 -08001854
1855 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001856 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Dima Zavin214cc642009-01-26 11:16:21 -08001857 return -1;
1858 }
1859
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001860 if (hdr->page_size != page_size) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001861 dprintf(CRITICAL, "ERROR: Invalid boot image pagesize. Device pagesize: %d, Image pagesize: %d\n",page_size,hdr->page_size);
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001862 return -1;
1863 }
1864
Kishor PK9e91b592017-05-24 12:14:55 +05301865 image_addr = (unsigned char *)target_get_scratch_address();
1866 memcpy(image_addr, (void *)buf, page_size);
vijay kumar287bb542015-09-29 13:01:52 +05301867
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001868 /*
1869 * Update the kernel/ramdisk/tags address if the boot image header
1870 * has default values, these default values come from mkbootimg when
1871 * the boot image is flashed using fastboot flash:raw
1872 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001873 update_ker_tags_rdisk_addr(hdr, false);
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001874
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001875 /* Get virtual addresses since the hdr saves physical addresses. */
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001876 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
1877 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
1878 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
1879
1880 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1881 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Mayank Grover032736f2017-07-14 20:34:51 +05301882 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001883
Kishor PK9e91b592017-05-24 12:14:55 +05301884 /* ensure commandline is terminated */
1885 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
1886
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001887 /* Check if the addresses in the header are valid. */
1888 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
Mayank Grovere559cec2017-10-17 15:12:03 +05301889 check_ddr_addr_range_bound(hdr->kernel_addr, kernel_actual) ||
1890 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual) ||
1891 check_ddr_addr_range_bound(hdr->ramdisk_addr, ramdisk_actual))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001892 {
Mayank Grovere559cec2017-10-17 15:12:03 +05301893 dprintf(CRITICAL, "kernel/ramdisk addresses are not valid.\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001894 return -1;
1895 }
1896
1897#ifndef DEVICE_TREE
Kishor PKd8ddcad2017-07-27 13:53:57 +05301898 if (UINT_MAX < ((uint64_t)kernel_actual + (uint64_t)ramdisk_actual+ (uint64_t)second_actual + page_size)) {
Mayank Grover032736f2017-07-14 20:34:51 +05301899 dprintf(CRITICAL, "Integer overflow detected in bootimage header fields\n");
1900 return -1;
1901 }
Kishor PKd8ddcad2017-07-27 13:53:57 +05301902 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + second_actual);
Mayank Grover032736f2017-07-14 20:34:51 +05301903
Mayank Grovere559cec2017-10-17 15:12:03 +05301904 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE) ||
1905 check_ddr_addr_range_bound(hdr->tags_addr, MAX_TAGS_SIZE))
Vijay Kumar Pendotid3ed20e2016-09-20 00:34:46 +05301906 {
Mayank Grovere559cec2017-10-17 15:12:03 +05301907 dprintf(CRITICAL, "Tags addresses are not valid.\n");
Vijay Kumar Pendotid3ed20e2016-09-20 00:34:46 +05301908 return -1;
1909 }
1910#else
1911
1912#ifndef OSVERSION_IN_BOOTIMAGE
1913 dt_size = hdr->dt_size;
1914#endif
Mayank Grover032736f2017-07-14 20:34:51 +05301915 dt_actual = ROUND_TO_PAGE(dt_size, page_mask);
Kishor PKd8ddcad2017-07-27 13:53:57 +05301916 if (UINT_MAX < ((uint64_t)kernel_actual + (uint64_t)ramdisk_actual+ (uint64_t)second_actual + (uint64_t)dt_actual + page_size)) {
Mayank Grover032736f2017-07-14 20:34:51 +05301917 dprintf(CRITICAL, "Integer overflow detected in bootimage header fields\n");
1918 return -1;
1919 }
1920
Kishor PKd8ddcad2017-07-27 13:53:57 +05301921 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + second_actual + dt_actual);
Mayank Grover032736f2017-07-14 20:34:51 +05301922
Mayank Grovere559cec2017-10-17 15:12:03 +05301923 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_size) ||
1924 check_ddr_addr_range_bound(hdr->tags_addr, dt_size))
Mayank Grover032736f2017-07-14 20:34:51 +05301925 {
Mayank Grovere559cec2017-10-17 15:12:03 +05301926 dprintf(CRITICAL, "Device tree addresses are not valid.\n");
Mayank Grover032736f2017-07-14 20:34:51 +05301927 return -1;
1928 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001929#endif
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001930
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05301931 /* Read full boot.img from flash */
1932 dprintf(INFO, "Loading (%s) image (%d): start\n",
1933 (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual);
1934 bs_set_timestamp(BS_KERNEL_LOAD_START);
1935
1936 if (UINT_MAX - page_size < imagesize_actual)
1937 {
1938 dprintf(CRITICAL,"Integer overflow detected in bootimage header fields %u %s\n", __LINE__,__func__);
1939 return -1;
1940 }
1941
1942 /*Check the availability of RAM before reading boot image + max signature length from flash*/
1943 if (target_get_max_flash_size() < (imagesize_actual + page_size))
1944 {
1945 dprintf(CRITICAL, "bootimage size is greater than DDR can hold\n");
1946 return -1;
1947 }
1948
1949 offset = page_size;
1950 /* Read image without signature and header */
1951 if (flash_read(ptn, offset, (void *)(image_addr + offset), imagesize_actual - page_size))
1952 {
1953 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
1954 return -1;
1955 }
1956
1957 dprintf(INFO, "Loading (%s) image (%d): done\n",
1958 (!boot_into_recovery ? "boot" : "recovery"), imagesize_actual);
1959 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
1960
Shashank Mittalcd98d472011-08-02 14:29:24 -07001961 /* Authenticate Kernel */
Deepa Dinamani23b60d42013-06-24 18:10:52 -07001962 if(target_use_signed_kernel() && (!device.is_unlocked))
Shashank Mittalcd98d472011-08-02 14:29:24 -07001963 {
Shashank Mittalcd98d472011-08-02 14:29:24 -07001964 offset = imagesize_actual;
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05301965
Shashank Mittalcd98d472011-08-02 14:29:24 -07001966 /* Read signature */
1967 if (flash_read(ptn, offset, (void *)(image_addr + offset), page_size))
1968 {
1969 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001970 return -1;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001971 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07001972
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301973 verify_signed_bootimg((uint32_t)image_addr, imagesize_actual);
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05301974 }
1975 offset = page_size;
1976 if(hdr->second_size != 0) {
1977 if (UINT_MAX - offset < second_actual)
1978 {
1979 dprintf(CRITICAL, "ERROR: Integer overflow in boot image header %s\t%d\n",__func__,__LINE__);
1980 return -1;
vijay kumar8f53e362015-11-24 13:38:11 +05301981 }
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05301982 offset += second_actual;
1983 /* Second image loading not implemented. */
1984 ASSERT(0);
1985 }
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05301986 /* Move kernel and ramdisk to correct address */
1987 memmove((void*) hdr->kernel_addr, (char*) (image_addr + page_size), hdr->kernel_size);
1988 memmove((void*) hdr->ramdisk_addr, (char*) (image_addr + page_size + kernel_actual), hdr->ramdisk_size);
1989
1990#if DEVICE_TREE
1991 if(dt_size != 0) {
1992
1993 dt_table_offset = ((uint32_t)image_addr + page_size + kernel_actual + ramdisk_actual + second_actual);
1994
1995 table = (struct dt_table*) dt_table_offset;
1996
1997 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
1998 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1999 return -1;
2000 }
2001
2002 /* Its Error if, dt_hdr_size (table->num_entries * dt_entry size + Dev_Tree Header)
2003 goes beyound hdr->dt_size*/
2004 if (dt_hdr_size > ROUND_TO_PAGE(dt_size,hdr->page_size)) {
2005 dprintf(CRITICAL, "ERROR: Invalid Device Tree size \n");
2006 return -1;
2007 }
2008
2009 /* Find index of device tree within device tree table */
2010 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
2011 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
2012 return -1;
2013 }
2014
2015 /* Validate and Read device device tree in the "tags_add */
2016 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size) ||
2017 check_ddr_addr_range_bound(hdr->tags_addr, dt_entry.size))
2018 {
2019 dprintf(CRITICAL, "Device tree addresses are not valid.\n");
2020 return -1;
2021 }
2022
2023 if(dt_entry.offset > (UINT_MAX - dt_entry.size)) {
2024 dprintf(CRITICAL, "ERROR: Device tree contents are Invalid\n");
2025 return -1;
2026 }
2027
2028 /* Ensure we are not overshooting dt_size with the dt_entry selected */
2029 if ((dt_entry.offset + dt_entry.size) > dt_size) {
2030 dprintf(CRITICAL, "ERROR: Device tree contents are Invalid\n");
2031 return -1;
2032 }
2033
2034 best_match_dt_addr = (unsigned char *)table + dt_entry.offset;
2035 dtb_size = dt_entry.size;
2036 memmove((void *)hdr->tags_addr, (char *)best_match_dt_addr, dtb_size);
2037 }
2038#endif
2039 if(target_use_signed_kernel() && (!device.is_unlocked))
2040 {
Shashank Mittal162244e2011-08-08 19:01:25 -07002041 /* Make sure everything from scratch address is read before next step!*/
Shashank Mittala0032282011-08-26 14:50:11 -07002042 if(device.is_tampered)
Shashank Mittal162244e2011-08-08 19:01:25 -07002043 {
2044 write_device_info_flash(&device);
2045 }
Channagoud Kadabi5c86fe32012-02-16 10:58:48 +05302046#if USE_PCOM_SECBOOT
2047 set_tamper_flag(device.is_tampered);
2048#endif
Shashank Mittalcd98d472011-08-02 14:29:24 -07002049 }
David Ng183a7422009-12-07 14:55:21 -08002050continue_boot:
Dima Zavin214cc642009-01-26 11:16:21 -08002051
Dima Zavin214cc642009-01-26 11:16:21 -08002052 /* TODO: create/pass atags to kernel */
2053
Ajay Dudanie28a6072011-07-01 13:59:46 -07002054 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07002055 (const char *)hdr->cmdline, board_machtype(),
Dima Zavin214cc642009-01-26 11:16:21 -08002056 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
2057
2058 return 0;
2059}
Brian Swetland9c4c0752009-01-25 16:23:50 -08002060
Shashank Mittal162244e2011-08-08 19:01:25 -07002061void write_device_info_mmc(device_info *dev)
2062{
Shashank Mittal162244e2011-08-08 19:01:25 -07002063 unsigned long long ptn = 0;
2064 unsigned long long size;
2065 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002066 uint8_t lun = 0;
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002067 uint32_t ret = 0;
Mayank Groverddd348d2018-01-23 14:07:09 +05302068 uint32_t device_info_sz = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07002069
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002070 if (devinfo_present)
2071 index = partition_get_index("devinfo");
2072 else
2073 index = partition_get_index("aboot");
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07002074
Shashank Mittal162244e2011-08-08 19:01:25 -07002075 ptn = partition_get_offset(index);
2076 if(ptn == 0)
2077 {
2078 return;
2079 }
2080
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002081 lun = partition_get_lun(index);
2082 mmc_set_lun(lun);
2083
Shashank Mittal162244e2011-08-08 19:01:25 -07002084 size = partition_get_size(index);
2085
Mayank Groverddd348d2018-01-23 14:07:09 +05302086 device_info_sz = ROUND_TO_PAGE(sizeof(struct device_info),
2087 mmc_blocksize_mask);
2088 if (device_info_sz == UINT_MAX)
2089 {
2090 dprintf(CRITICAL, "ERROR: Incorrect blocksize of card\n");
2091 return;
2092 }
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07002093
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002094 if (devinfo_present)
Mayank Groverddd348d2018-01-23 14:07:09 +05302095 ret = mmc_write(ptn, device_info_sz, (void *)info_buf);
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002096 else
Mayank Groverddd348d2018-01-23 14:07:09 +05302097 ret = mmc_write((ptn + size - device_info_sz), device_info_sz, (void *)info_buf);
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002098 if (ret)
Shashank Mittal162244e2011-08-08 19:01:25 -07002099 {
2100 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
Sridhar Parasurame1a97922015-07-27 15:31:22 -07002101 ASSERT(0);
Shashank Mittal162244e2011-08-08 19:01:25 -07002102 }
2103}
2104
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002105void read_device_info_mmc(struct device_info *info)
Shashank Mittal162244e2011-08-08 19:01:25 -07002106{
Shashank Mittal162244e2011-08-08 19:01:25 -07002107 unsigned long long ptn = 0;
2108 unsigned long long size;
2109 int index = INVALID_PTN;
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002110 uint32_t ret = 0;
Mayank Groverddd348d2018-01-23 14:07:09 +05302111 uint32_t device_info_sz = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07002112
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002113 if ((index = partition_get_index("devinfo")) < 0)
2114 {
2115 devinfo_present = false;
2116 index = partition_get_index("aboot");
2117 }
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07002118
Shashank Mittal162244e2011-08-08 19:01:25 -07002119 ptn = partition_get_offset(index);
2120 if(ptn == 0)
2121 {
2122 return;
2123 }
2124
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07002125 mmc_set_lun(partition_get_lun(index));
2126
Shashank Mittal162244e2011-08-08 19:01:25 -07002127 size = partition_get_size(index);
2128
Mayank Groverddd348d2018-01-23 14:07:09 +05302129 device_info_sz = ROUND_TO_PAGE(sizeof(struct device_info),
2130 mmc_blocksize_mask);
2131 if (device_info_sz == UINT_MAX)
2132 {
2133 dprintf(CRITICAL, "ERROR: Incorrect blocksize of card\n");
2134 return;
2135 }
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07002136
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002137 if (devinfo_present)
Mayank Groverddd348d2018-01-23 14:07:09 +05302138 ret = mmc_read(ptn, (void *)info_buf, device_info_sz);
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002139 else
Mayank Groverddd348d2018-01-23 14:07:09 +05302140 ret = mmc_read((ptn + size - device_info_sz), (void *)info_buf, device_info_sz);
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002141 if (ret)
Shashank Mittal162244e2011-08-08 19:01:25 -07002142 {
2143 dprintf(CRITICAL, "ERROR: Cannot read device info\n");
Sridhar Parasurame1a97922015-07-27 15:31:22 -07002144 ASSERT(0);
Shashank Mittal162244e2011-08-08 19:01:25 -07002145 }
Shashank Mittal162244e2011-08-08 19:01:25 -07002146}
2147
2148void write_device_info_flash(device_info *dev)
2149{
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002150 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
Shashank Mittal162244e2011-08-08 19:01:25 -07002151 struct ptentry *ptn;
2152 struct ptable *ptable;
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002153 if(info == NULL)
2154 {
2155 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
2156 ASSERT(0);
2157 }
2158 info_buf = info;
Shashank Mittal162244e2011-08-08 19:01:25 -07002159 ptable = flash_get_ptable();
2160 if (ptable == NULL)
2161 {
2162 dprintf(CRITICAL, "ERROR: Partition table not found\n");
2163 return;
2164 }
2165
2166 ptn = ptable_find(ptable, "devinfo");
2167 if (ptn == NULL)
2168 {
Smita Ghosh670c8b82015-05-07 09:30:03 -07002169 dprintf(CRITICAL, "ERROR: No devinfo partition found\n");
Shashank Mittal162244e2011-08-08 19:01:25 -07002170 return;
2171 }
2172
Mayank Groverdedbc892017-10-24 13:41:34 +05302173 memset(info, 0, BOOT_IMG_MAX_PAGE_SIZE);
Shashank Mittal162244e2011-08-08 19:01:25 -07002174 memcpy(info, dev, sizeof(device_info));
2175
2176 if (flash_write(ptn, 0, (void *)info_buf, page_size))
2177 {
2178 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
2179 return;
2180 }
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002181 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07002182}
2183
vijay kumarc65876c2015-04-24 13:29:16 +05302184static int read_allow_oem_unlock(device_info *dev)
2185{
vijay kumarc65876c2015-04-24 13:29:16 +05302186 unsigned offset;
2187 int index;
2188 unsigned long long ptn;
2189 unsigned long long ptn_size;
2190 unsigned blocksize = mmc_get_device_blocksize();
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07002191 STACKBUF_DMA_ALIGN(buf, blocksize);
vijay kumarc65876c2015-04-24 13:29:16 +05302192
vijay kumarca2e6812015-07-08 20:28:25 +05302193 index = partition_get_index(frp_ptns[0]);
vijay kumarc65876c2015-04-24 13:29:16 +05302194 if (index == INVALID_PTN)
2195 {
vijay kumarca2e6812015-07-08 20:28:25 +05302196 index = partition_get_index(frp_ptns[1]);
2197 if (index == INVALID_PTN)
2198 {
2199 dprintf(CRITICAL, "Neither '%s' nor '%s' partition found\n", frp_ptns[0],frp_ptns[1]);
2200 return -1;
2201 }
vijay kumarc65876c2015-04-24 13:29:16 +05302202 }
2203
2204 ptn = partition_get_offset(index);
2205 ptn_size = partition_get_size(index);
2206 offset = ptn_size - blocksize;
2207
Mayank Grover48860402016-11-29 12:34:53 +05302208 /* Set Lun for partition */
2209 mmc_set_lun(partition_get_lun(index));
2210
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07002211 if (mmc_read(ptn + offset, (void *)buf, blocksize))
vijay kumarc65876c2015-04-24 13:29:16 +05302212 {
2213 dprintf(CRITICAL, "Reading MMC failed\n");
2214 return -1;
2215 }
2216
2217 /*is_allow_unlock is a bool value stored at the LSB of last byte*/
2218 is_allow_unlock = buf[blocksize-1] & 0x01;
2219 return 0;
2220}
2221
2222static int write_allow_oem_unlock(bool allow_unlock)
2223{
vijay kumarc65876c2015-04-24 13:29:16 +05302224 unsigned offset;
vijay kumarc65876c2015-04-24 13:29:16 +05302225 int index;
2226 unsigned long long ptn;
2227 unsigned long long ptn_size;
2228 unsigned blocksize = mmc_get_device_blocksize();
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07002229 STACKBUF_DMA_ALIGN(buf, blocksize);
vijay kumarc65876c2015-04-24 13:29:16 +05302230
vijay kumarca2e6812015-07-08 20:28:25 +05302231 index = partition_get_index(frp_ptns[0]);
vijay kumarc65876c2015-04-24 13:29:16 +05302232 if (index == INVALID_PTN)
2233 {
vijay kumarca2e6812015-07-08 20:28:25 +05302234 index = partition_get_index(frp_ptns[1]);
2235 if (index == INVALID_PTN)
2236 {
2237 dprintf(CRITICAL, "Neither '%s' nor '%s' partition found\n", frp_ptns[0],frp_ptns[1]);
2238 return -1;
2239 }
vijay kumarc65876c2015-04-24 13:29:16 +05302240 }
2241
2242 ptn = partition_get_offset(index);
2243 ptn_size = partition_get_size(index);
2244 offset = ptn_size - blocksize;
Mayank Grover48860402016-11-29 12:34:53 +05302245 mmc_set_lun(partition_get_lun(index));
vijay kumarc65876c2015-04-24 13:29:16 +05302246
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07002247 if (mmc_read(ptn + offset, (void *)buf, blocksize))
vijay kumarc65876c2015-04-24 13:29:16 +05302248 {
2249 dprintf(CRITICAL, "Reading MMC failed\n");
2250 return -1;
2251 }
2252
2253 /*is_allow_unlock is a bool value stored at the LSB of last byte*/
2254 buf[blocksize-1] = allow_unlock;
2255 if (mmc_write(ptn + offset, blocksize, buf))
2256 {
2257 dprintf(CRITICAL, "Writing MMC failed\n");
2258 return -1;
2259 }
2260
2261 return 0;
2262}
2263
Shashank Mittal162244e2011-08-08 19:01:25 -07002264void read_device_info_flash(device_info *dev)
2265{
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002266 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
Shashank Mittal162244e2011-08-08 19:01:25 -07002267 struct ptentry *ptn;
2268 struct ptable *ptable;
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002269 if(info == NULL)
2270 {
2271 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
2272 ASSERT(0);
2273 }
2274 info_buf = info;
Shashank Mittal162244e2011-08-08 19:01:25 -07002275 ptable = flash_get_ptable();
2276 if (ptable == NULL)
2277 {
2278 dprintf(CRITICAL, "ERROR: Partition table not found\n");
2279 return;
2280 }
2281
2282 ptn = ptable_find(ptable, "devinfo");
2283 if (ptn == NULL)
2284 {
Smita Ghosh670c8b82015-05-07 09:30:03 -07002285 dprintf(CRITICAL, "ERROR: No devinfo partition found\n");
Shashank Mittal162244e2011-08-08 19:01:25 -07002286 return;
2287 }
2288
2289 if (flash_read(ptn, 0, (void *)info_buf, page_size))
2290 {
2291 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
2292 return;
2293 }
2294
2295 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
2296 {
Shashank Mittal162244e2011-08-08 19:01:25 -07002297 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
2298 info->is_unlocked = 0;
Shashank Mittala0032282011-08-26 14:50:11 -07002299 info->is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07002300 write_device_info_flash(info);
2301 }
2302 memcpy(dev, info, sizeof(device_info));
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002303 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07002304}
2305
2306void write_device_info(device_info *dev)
2307{
2308 if(target_is_emmc_boot())
2309 {
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002310 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
2311 if(info == NULL)
2312 {
2313 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
2314 ASSERT(0);
2315 }
2316 info_buf = info;
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002317 memcpy(info, dev, sizeof(struct device_info));
2318
2319#if USE_RPMB_FOR_DEVINFO
Mayank Grover912eaa62017-10-26 12:08:53 +05302320 if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05302321 is_secure_boot_enable()) {
2322 if((write_device_info_rpmb((void*) info, PAGE_SIZE)) < 0)
2323 ASSERT(0);
Sridhar Parasurame1a97922015-07-27 15:31:22 -07002324 }
Channagoud Kadabic80cb492015-04-28 16:08:28 -07002325 else
2326 write_device_info_mmc(info);
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002327#else
2328 write_device_info_mmc(info);
2329#endif
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002330 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07002331 }
2332 else
2333 {
2334 write_device_info_flash(dev);
2335 }
2336}
2337
Monika Singh94316462018-03-15 18:39:01 +05302338int read_rollback_index(uint32_t loc, uint64_t *roll_back_index)
2339{
2340 if (!devinfo_present) {
2341 dprintf(CRITICAL, "DeviceInfo not initalized \n");
2342 return -EINVAL;
2343 }
2344 if (loc >= ARRAY_SIZE(device.rollback_index)) {
2345 dprintf(CRITICAL, "%s() Loc out of range index: %d, array len: %d\n",
2346 __func__, loc, ARRAY_SIZE(device.rollback_index));
2347 ASSERT(0);
2348 }
2349
2350 *roll_back_index = device.rollback_index[loc];
2351 return 0;
2352}
2353
2354int write_rollback_index(uint32_t loc, uint64_t roll_back_index)
2355{
2356 if (!devinfo_present) {
2357 dprintf(CRITICAL, "DeviceInfo not initalized \n");
2358 return -EINVAL;
2359 }
2360 if (loc >= ARRAY_SIZE(device.rollback_index)) {
2361 dprintf(CRITICAL, "%s() Loc out of range index: %d, array len: %d\n",
2362 __func__, loc, ARRAY_SIZE(device.rollback_index));
2363 ASSERT(0);
2364 }
2365
2366 device.rollback_index[loc] = roll_back_index;
2367 write_device_info(&device);
2368 return 0;
2369}
2370
Shashank Mittal162244e2011-08-08 19:01:25 -07002371void read_device_info(device_info *dev)
2372{
2373 if(target_is_emmc_boot())
2374 {
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002375 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
2376 if(info == NULL)
2377 {
2378 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
2379 ASSERT(0);
2380 }
2381 info_buf = info;
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002382
2383#if USE_RPMB_FOR_DEVINFO
Mayank Grover912eaa62017-10-26 12:08:53 +05302384 if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05302385 is_secure_boot_enable()) {
2386 if((read_device_info_rpmb((void*) info, PAGE_SIZE)) < 0)
2387 ASSERT(0);
Sridhar Parasurame1a97922015-07-27 15:31:22 -07002388 }
Channagoud Kadabic80cb492015-04-28 16:08:28 -07002389 else
2390 read_device_info_mmc(info);
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002391#else
2392 read_device_info_mmc(info);
2393#endif
2394
2395 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
2396 {
2397 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
lijuang511a2b52015-08-14 20:50:51 +08002398 if (is_secure_boot_enable()) {
Channagoud Kadabi05f78ba2015-07-06 11:58:14 -07002399 info->is_unlocked = 0;
Monika Singh292b3e92018-03-17 22:40:23 +05302400#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover912eaa62017-10-26 12:08:53 +05302401 if (VB_M <= target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +05302402 info->is_unlock_critical = 0;
Parth Dixitddbc7352015-10-18 03:13:31 +05302403#endif
lijuang511a2b52015-08-14 20:50:51 +08002404 } else {
Channagoud Kadabi2fda4092015-07-07 13:34:11 -07002405 info->is_unlocked = 1;
Monika Singh292b3e92018-03-17 22:40:23 +05302406#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover912eaa62017-10-26 12:08:53 +05302407 if (VB_M <= target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +05302408 info->is_unlock_critical = 1;
Parth Dixitddbc7352015-10-18 03:13:31 +05302409#endif
lijuang511a2b52015-08-14 20:50:51 +08002410 }
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002411 info->is_tampered = 0;
2412 info->charger_screen_enabled = 0;
Monika Singh292b3e92018-03-17 22:40:23 +05302413#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover912eaa62017-10-26 12:08:53 +05302414 if (VB_M <= target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +05302415 info->verity_mode = 1; //enforcing by default
Parth Dixitddbc7352015-10-18 03:13:31 +05302416#endif
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002417 write_device_info(info);
2418 }
2419 memcpy(dev, info, sizeof(device_info));
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002420 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07002421 }
2422 else
2423 {
2424 read_device_info_flash(dev);
2425 }
2426}
2427
2428void reset_device_info()
2429{
2430 dprintf(ALWAYS, "reset_device_info called.");
Shashank Mittala0032282011-08-26 14:50:11 -07002431 device.is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07002432 write_device_info(&device);
2433}
2434
2435void set_device_root()
2436{
2437 dprintf(ALWAYS, "set_device_root called.");
Shashank Mittala0032282011-08-26 14:50:11 -07002438 device.is_tampered = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -07002439 write_device_info(&device);
2440}
2441
lijuang4ece1e72015-08-14 21:02:36 +08002442/* set device unlock value
2443 * Must check FRP before call this function
2444 * Need to wipe data when unlock status changed
2445 * type 0: oem unlock
2446 * type 1: unlock critical
2447 * status 0: unlock as false
2448 * status 1: lock as true
2449 */
2450void set_device_unlock_value(int type, bool status)
lijuang21f12f52015-08-22 16:22:19 +08002451{
lijuang4ece1e72015-08-14 21:02:36 +08002452 if (type == UNLOCK)
2453 device.is_unlocked = status;
Monika Singh292b3e92018-03-17 22:40:23 +05302454#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover912eaa62017-10-26 12:08:53 +05302455 else if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05302456 type == UNLOCK_CRITICAL)
2457 device.is_unlock_critical = status;
Parth Dixitddbc7352015-10-18 03:13:31 +05302458#endif
lijuang4ece1e72015-08-14 21:02:36 +08002459 write_device_info(&device);
2460}
2461
2462static void set_device_unlock(int type, bool status)
2463{
2464 int is_unlocked = -1;
2465 char response[MAX_RSP_SIZE];
2466
2467 /* check device unlock status if it is as expected */
2468 if (type == UNLOCK)
2469 is_unlocked = device.is_unlocked;
Monika Singh292b3e92018-03-17 22:40:23 +05302470#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover912eaa62017-10-26 12:08:53 +05302471 if(VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05302472 type == UNLOCK_CRITICAL)
2473 {
2474 is_unlocked = device.is_unlock_critical;
2475 }
Parth Dixitddbc7352015-10-18 03:13:31 +05302476#endif
lijuang4ece1e72015-08-14 21:02:36 +08002477 if (is_unlocked == status) {
2478 snprintf(response, sizeof(response), "\tDevice already : %s", (status ? "unlocked!" : "locked!"));
2479 fastboot_info(response);
2480 fastboot_okay("");
2481 return;
2482 }
2483
2484 /* status is true, it means to unlock device */
2485 if (status) {
lijuang21f12f52015-08-22 16:22:19 +08002486 if(!is_allow_unlock) {
2487 fastboot_fail("oem unlock is not allowed");
2488 return;
2489 }
2490
lijuang4ece1e72015-08-14 21:02:36 +08002491#if FBCON_DISPLAY_MSG
lijuang9a7d3b92015-11-30 14:41:24 +08002492 display_unlock_menu(type);
lijuang4ece1e72015-08-14 21:02:36 +08002493 fastboot_okay("");
2494 return;
2495#else
2496 if (type == UNLOCK) {
2497 fastboot_fail("Need wipe userdata. Do 'fastboot oem unlock-go'");
2498 return;
2499 }
2500#endif
lijuang21f12f52015-08-22 16:22:19 +08002501 }
lijuang4ece1e72015-08-14 21:02:36 +08002502
2503 set_device_unlock_value(type, status);
2504
2505 /* wipe data */
2506 struct recovery_message msg;
Kishor PK60a68212017-05-08 16:55:57 +05302507 memset(&msg, 0, sizeof(msg));
lijuang4ece1e72015-08-14 21:02:36 +08002508 snprintf(msg.recovery, sizeof(msg.recovery), "recovery\n--wipe_data");
2509 write_misc(0, &msg, sizeof(msg));
2510
2511 fastboot_okay("");
2512 reboot_device(RECOVERY_MODE);
lijuang21f12f52015-08-22 16:22:19 +08002513}
2514
lijuang511a2b52015-08-14 20:50:51 +08002515static bool critical_flash_allowed(const char * entry)
2516{
2517 uint32_t i = 0;
2518 if (entry == NULL)
2519 return false;
2520
2521 for (i = 0; i < ARRAY_SIZE(critical_flash_allowed_ptn); i++) {
2522 if(!strcmp(entry, critical_flash_allowed_ptn[i]))
2523 return true;
2524 }
2525 return false;
Matthew Qin271927e2015-03-31 22:07:07 -04002526}
2527
2528#if DEVICE_TREE
Amol Jadicb524072012-08-09 16:40:18 -07002529int copy_dtb(uint8_t *boot_image_start, unsigned int scratch_offset)
2530{
2531 uint32 dt_image_offset = 0;
Amol Jadicb524072012-08-09 16:40:18 -07002532 uint32_t n;
Monika Singh292b3e92018-03-17 22:40:23 +05302533 struct dt_table *table = NULL;
Amol Jadicb524072012-08-09 16:40:18 -07002534 struct dt_entry dt_entry;
Monika Singh292b3e92018-03-17 22:40:23 +05302535 uint32_t dt_hdr_size = 0;
Amol Jadicb524072012-08-09 16:40:18 -07002536 unsigned int compressed_size = 0;
2537 unsigned int dtb_size = 0;
2538 unsigned int out_avai_len = 0;
2539 unsigned char *out_addr = NULL;
2540 unsigned char *best_match_dt_addr = NULL;
2541 int rc;
2542
2543 struct boot_img_hdr *hdr = (struct boot_img_hdr *) (boot_image_start);
2544
Parth Dixit4097b622016-03-15 14:42:27 +05302545#ifndef OSVERSION_IN_BOOTIMAGE
2546 dt_size = hdr->dt_size;
2547#endif
2548
2549 if(dt_size != 0) {
Amol Jadicb524072012-08-09 16:40:18 -07002550 /* add kernel offset */
2551 dt_image_offset += page_size;
2552 n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
2553 dt_image_offset += n;
2554
2555 /* add ramdisk offset */
2556 n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
2557 dt_image_offset += n;
2558
2559 /* add second offset */
2560 if(hdr->second_size != 0) {
2561 n = ROUND_TO_PAGE(hdr->second_size, page_mask);
2562 dt_image_offset += n;
2563 }
2564
2565 /* offset now point to start of dt.img */
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07002566 table = (struct dt_table*)(boot_image_start + dt_image_offset);
Amol Jadicb524072012-08-09 16:40:18 -07002567
Deepa Dinamani19648b42013-09-05 17:05:55 -07002568 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Amol Jadicb524072012-08-09 16:40:18 -07002569 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
2570 return -1;
2571 }
P.V. Phani Kumar82916762016-03-09 09:20:19 +05302572
2573 /* Its Error if, dt_hdr_size (table->num_entries * dt_entry size + Dev_Tree Header)
2574 goes beyound hdr->dt_size*/
Parth Dixit4097b622016-03-15 14:42:27 +05302575 if (dt_hdr_size > ROUND_TO_PAGE(dt_size,hdr->page_size)) {
P.V. Phani Kumar82916762016-03-09 09:20:19 +05302576 dprintf(CRITICAL, "ERROR: Invalid Device Tree size \n");
2577 return -1;
2578 }
2579
Joel Kingaa335dc2013-06-03 16:11:08 -07002580 /* Find index of device tree within device tree table */
2581 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Amol Jadicb524072012-08-09 16:40:18 -07002582 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
2583 return -1;
2584 }
2585
Matthew Qin271927e2015-03-31 22:07:07 -04002586 best_match_dt_addr = (unsigned char *)boot_image_start + dt_image_offset + dt_entry.offset;
2587 if (is_gzip_package(best_match_dt_addr, dt_entry.size))
2588 {
2589 out_addr = (unsigned char *)target_get_scratch_address() + scratch_offset;
2590 out_avai_len = target_get_max_flash_size() - scratch_offset;
Matthew Qin0b15b322015-05-19 05:20:54 -04002591 dprintf(INFO, "decompressing dtb: start\n");
Matthew Qin271927e2015-03-31 22:07:07 -04002592 rc = decompress(best_match_dt_addr,
2593 dt_entry.size, out_addr, out_avai_len,
2594 &compressed_size, &dtb_size);
2595 if (rc)
2596 {
Matthew Qin0b15b322015-05-19 05:20:54 -04002597 dprintf(CRITICAL, "decompressing dtb failed!!!\n");
Matthew Qin271927e2015-03-31 22:07:07 -04002598 ASSERT(0);
2599 }
2600
Matthew Qin0b15b322015-05-19 05:20:54 -04002601 dprintf(INFO, "decompressing dtb: done\n");
Matthew Qin271927e2015-03-31 22:07:07 -04002602 best_match_dt_addr = out_addr;
2603 } else {
2604 dtb_size = dt_entry.size;
2605 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002606 /* Validate and Read device device tree in the "tags_add */
Mayank Grovere559cec2017-10-17 15:12:03 +05302607 if (check_aboot_addr_range_overlap(hdr->tags_addr, dtb_size) ||
2608 check_ddr_addr_range_bound(hdr->tags_addr, dtb_size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002609 {
Mayank Grovere559cec2017-10-17 15:12:03 +05302610 dprintf(CRITICAL, "Device tree addresses are not valid.\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002611 return -1;
2612 }
2613
Amol Jadicb524072012-08-09 16:40:18 -07002614 /* Read device device tree in the "tags_add */
Matthew Qin271927e2015-03-31 22:07:07 -04002615 memmove((void*) hdr->tags_addr, (void *)best_match_dt_addr, dtb_size);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002616 } else
2617 return -1;
Amol Jadicb524072012-08-09 16:40:18 -07002618
2619 /* Everything looks fine. Return success. */
2620 return 0;
2621}
2622#endif
2623
Brian Swetland9c4c0752009-01-25 16:23:50 -08002624void cmd_boot(const char *arg, void *data, unsigned sz)
2625{
2626 unsigned kernel_actual;
2627 unsigned ramdisk_actual;
Kishor PKd8ddcad2017-07-27 13:53:57 +05302628 unsigned second_actual;
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002629 uint32_t image_actual;
2630 uint32_t dt_actual = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08002631 struct boot_img_hdr *hdr = NULL;
2632 struct kernel64_hdr *kptr = NULL;
Brian Swetland9c4c0752009-01-25 16:23:50 -08002633 char *ptr = ((char*) data);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002634 int ret = 0;
2635 uint8_t dtb_copied = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08002636 unsigned int out_len = 0;
2637 unsigned int out_avai_len = 0;
2638 unsigned char *out_addr = NULL;
2639 uint32_t dtb_offset = 0;
2640 unsigned char *kernel_start_addr = NULL;
2641 unsigned int kernel_size = 0;
Matthew Qin271927e2015-03-31 22:07:07 -04002642 unsigned int scratch_offset = 0;
Monika Singh292b3e92018-03-17 22:40:23 +05302643#if !VERIFIED_BOOT_2
2644 uint32_t sig_actual = 0;
2645 uint32_t sig_size = 0;
2646#ifdef MDTP_SUPPORT
2647 static bool is_mdtp_activated = 0;
2648#endif /* MDTP_SUPPORT */
2649#endif
Matthew Qinbb7923d2015-02-09 10:56:09 +08002650
lijuang2008ff22016-03-07 17:56:27 +08002651#if FBCON_DISPLAY_MSG
2652 /* Exit keys' detection thread firstly */
2653 exit_menu_keys_detection();
2654#endif
2655
Monika Singh292b3e92018-03-17 22:40:23 +05302656#if VERIFIED_BOOT || VERIFIED_BOOT_2
Channagoud Kadabi6d5375e2015-06-23 17:15:42 -07002657 if(target_build_variant_user() && !device.is_unlocked)
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002658 {
2659 fastboot_fail("unlock device to use this command");
lijuang2008ff22016-03-07 17:56:27 +08002660 goto boot_failed;
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002661 }
2662#endif
2663
Brian Swetland9c4c0752009-01-25 16:23:50 -08002664 if (sz < sizeof(hdr)) {
2665 fastboot_fail("invalid bootimage header");
lijuang2008ff22016-03-07 17:56:27 +08002666 goto boot_failed;
Brian Swetland9c4c0752009-01-25 16:23:50 -08002667 }
2668
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002669 hdr = (struct boot_img_hdr *)data;
Brian Swetland9c4c0752009-01-25 16:23:50 -08002670
2671 /* ensure commandline is terminated */
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002672 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -08002673
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002674 if(target_is_emmc_boot() && hdr->page_size) {
2675 page_size = hdr->page_size;
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07002676 page_mask = page_size - 1;
2677 }
2678
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002679 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
2680 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Kishor PKd8ddcad2017-07-27 13:53:57 +05302681 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002682#if DEVICE_TREE
Parth Dixit4097b622016-03-15 14:42:27 +05302683#ifndef OSVERSION_IN_BOOTIMAGE
2684 dt_size = hdr->dt_size;
2685#endif
2686 dt_actual = ROUND_TO_PAGE(dt_size, page_mask);
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002687#endif
2688
2689 image_actual = ADD_OF(page_size, kernel_actual);
2690 image_actual = ADD_OF(image_actual, ramdisk_actual);
Kishor PKd8ddcad2017-07-27 13:53:57 +05302691 image_actual = ADD_OF(image_actual, second_actual);
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002692 image_actual = ADD_OF(image_actual, dt_actual);
2693
Kishor PK5134b332017-05-09 17:50:08 +05302694 /* Checking to prevent oob access in read_der_message_length */
2695 if (image_actual > sz) {
2696 fastboot_fail("bootimage header fields are invalid");
2697 goto boot_failed;
2698 }
Monika Singh292b3e92018-03-17 22:40:23 +05302699#if VERIFIED_BOOT_2
2700 memset(&info, 0, sizeof(bootinfo));
2701 info.images[0].image_buffer = data;
2702 info.images[0].imgsize = image_actual;
2703 info.images[0].name = "boot";
2704 info.num_loaded_images = 1;
2705 info.multi_slot_boot = partition_multislot_is_supported();
2706 if (load_image_and_auth(&info))
2707 goto boot_failed;
2708 vbcmdline = info.vbcmdline;
2709#else
Kishor PK5134b332017-05-09 17:50:08 +05302710 sig_size = sz - image_actual;
2711
P.V. Phani Kumareecfd822016-03-09 20:09:03 +05302712 if (target_use_signed_kernel() && (!device.is_unlocked)) {
Monika Singh292b3e92018-03-17 22:40:23 +05302713 unsigned chk;
P.V. Phani Kumareecfd822016-03-09 20:09:03 +05302714 /* Calculate the signature length from boot image */
2715 sig_actual = read_der_message_length(
Kishor PK5134b332017-05-09 17:50:08 +05302716 (unsigned char*)(data + image_actual), sig_size);
Monika Singh292b3e92018-03-17 22:40:23 +05302717 chk = ADD_OF(image_actual, sig_actual);
Channagoud Kadabi3d839012014-06-26 14:26:39 -07002718
Monika Singh292b3e92018-03-17 22:40:23 +05302719 if (chk > sz) {
Kishor PK5134b332017-05-09 17:50:08 +05302720 fastboot_fail("bootimage header fields are invalid");
2721 goto boot_failed;
2722 }
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002723 }
2724
Sridhar Parasuram361e4cd2015-09-24 11:19:19 -07002725 // Initialize boot state before trying to verify boot.img
2726#if VERIFIED_BOOT
Channagoud Kadabi699466e2015-11-03 12:37:42 -08002727 boot_verifier_init();
Mayank Groverb337e932017-01-18 20:00:40 +05302728#endif
Channagoud Kadabibf6ce7d2015-09-17 13:25:35 -07002729 /* Handle overflow if the input image size is greater than
2730 * boot image buffer can hold
2731 */
Monika Singh292b3e92018-03-17 22:40:23 +05302732 if ((target_get_max_flash_size() - page_size) < image_actual)
Channagoud Kadabibf6ce7d2015-09-17 13:25:35 -07002733 {
2734 fastboot_fail("booimage: size is greater than boot image buffer can hold");
lijuang2008ff22016-03-07 17:56:27 +08002735 goto boot_failed;
Channagoud Kadabibf6ce7d2015-09-17 13:25:35 -07002736 }
Sridhar Parasuram361e4cd2015-09-24 11:19:19 -07002737
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002738 /* Verify the boot image
2739 * device & page_size are initialized in aboot_init
2740 */
Sridhar Parasuram7ae9fbc2015-09-17 09:56:30 -07002741 if (target_use_signed_kernel() && (!device.is_unlocked)) {
Channagoud Kadabi3d839012014-06-26 14:26:39 -07002742 /* Pass size excluding signature size, otherwise we would try to
2743 * access signature beyond its length
2744 */
Monika Singh292b3e92018-03-17 22:40:23 +05302745 verify_signed_bootimg((uint32_t)data, image_actual);
Sridhar Parasuram7ae9fbc2015-09-17 09:56:30 -07002746 }
Amit Blay4aa292f2015-04-28 21:55:59 +03002747#ifdef MDTP_SUPPORT
2748 else
2749 {
Shay Nachmani062aa3f2015-05-17 17:28:44 +03002750 /* fastboot boot is not allowed when MDTP is activated */
Amit Blay4aa292f2015-04-28 21:55:59 +03002751 mdtp_ext_partition_verification_t ext_partition;
Amit Blay8a510302015-08-17 09:20:01 +03002752
2753 if (!is_mdtp_activated) {
2754 ext_partition.partition = MDTP_PARTITION_NONE;
2755 mdtp_fwlock_verify_lock(&ext_partition);
2756 }
Amit Blay4aa292f2015-04-28 21:55:59 +03002757 }
Shay Nachmani062aa3f2015-05-17 17:28:44 +03002758
Amir Kotzer7c768c02016-04-13 09:08:05 +03002759 /* If mdtp state cannot be validate, block fastboot boot*/
2760 if(mdtp_activated(&is_mdtp_activated)){
2761 dprintf(CRITICAL, "mdtp_activated cannot validate state.\n");
2762 dprintf(CRITICAL, "Can not proceed with fastboot boot command.\n");
2763 goto boot_failed;
2764 }
Shay Nachmani062aa3f2015-05-17 17:28:44 +03002765 if(is_mdtp_activated){
2766 dprintf(CRITICAL, "fastboot boot command is not available.\n");
lijuang2008ff22016-03-07 17:56:27 +08002767 goto boot_failed;
Shay Nachmani062aa3f2015-05-17 17:28:44 +03002768 }
Amit Blay4aa292f2015-04-28 21:55:59 +03002769#endif /* MDTP_SUPPORT */
Monika Singh292b3e92018-03-17 22:40:23 +05302770#endif /* VERIFIED_BOOT_2 else */
Amit Blay4aa292f2015-04-28 21:55:59 +03002771
Sridhar Parasuram361e4cd2015-09-24 11:19:19 -07002772#if VERIFIED_BOOT
Monika Singh292b3e92018-03-17 22:40:23 +05302773 if (VB_M == target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +05302774 {
2775 /* set boot and system versions. */
2776 set_os_version((unsigned char *)data);
2777 // send root of trust
2778 if(!send_rot_command((uint32_t)device.is_unlocked))
2779 ASSERT(0);
2780 }
Parth Dixitbc9b6492015-10-18 00:41:38 +05302781#endif
Channagoud Kadabia22144f2013-03-20 11:49:01 -07002782 /*
Matthew Qinbb7923d2015-02-09 10:56:09 +08002783 * Check if the kernel image is a gzip package. If yes, need to decompress it.
2784 * If not, continue booting.
2785 */
2786 if (is_gzip_package((unsigned char *)(data + page_size), hdr->kernel_size))
2787 {
2788 out_addr = (unsigned char *)target_get_scratch_address();
2789 out_addr = (unsigned char *)(out_addr + image_actual + page_size);
2790 out_avai_len = target_get_max_flash_size() - image_actual - page_size;
Matthew Qin0b15b322015-05-19 05:20:54 -04002791 dprintf(INFO, "decompressing kernel image: start\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08002792 ret = decompress((unsigned char *)(ptr + page_size),
2793 hdr->kernel_size, out_addr, out_avai_len,
2794 &dtb_offset, &out_len);
2795 if (ret)
2796 {
Matthew Qin0b15b322015-05-19 05:20:54 -04002797 dprintf(CRITICAL, "decompressing image failed!!!\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08002798 ASSERT(0);
2799 }
2800
Matthew Qin0b15b322015-05-19 05:20:54 -04002801 dprintf(INFO, "decompressing kernel image: done\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08002802 kptr = (struct kernel64_hdr *)out_addr;
2803 kernel_start_addr = out_addr;
2804 kernel_size = out_len;
2805 } else {
2806 kptr = (struct kernel64_hdr*)((char *)data + page_size);
2807 kernel_start_addr = (unsigned char *)((char *)data + page_size);
2808 kernel_size = hdr->kernel_size;
2809 }
2810
2811 /*
Channagoud Kadabia22144f2013-03-20 11:49:01 -07002812 * Update the kernel/ramdisk/tags address if the boot image header
2813 * has default values, these default values come from mkbootimg when
2814 * the boot image is flashed using fastboot flash:raw
2815 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08002816 update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr));
Dima Zavin3cadfff2013-03-21 14:30:48 -07002817
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002818 /* Get virtual addresses since the hdr saves physical addresses. */
2819 hdr->kernel_addr = VA(hdr->kernel_addr);
2820 hdr->ramdisk_addr = VA(hdr->ramdisk_addr);
2821 hdr->tags_addr = VA(hdr->tags_addr);
Brian Swetland9c4c0752009-01-25 16:23:50 -08002822
Matthew Qinbb7923d2015-02-09 10:56:09 +08002823 kernel_size = ROUND_TO_PAGE(kernel_size, page_mask);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002824 /* Check if the addresses in the header are valid. */
Matthew Qinbb7923d2015-02-09 10:56:09 +08002825 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_size) ||
Mayank Grovere559cec2017-10-17 15:12:03 +05302826 check_ddr_addr_range_bound(hdr->kernel_addr, kernel_size) ||
2827 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual) ||
2828 check_ddr_addr_range_bound(hdr->ramdisk_addr, ramdisk_actual))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002829 {
Mayank Grovere559cec2017-10-17 15:12:03 +05302830 dprintf(CRITICAL, "kernel/ramdisk addresses are not valid.\n");
lijuang2008ff22016-03-07 17:56:27 +08002831 goto boot_failed;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002832 }
2833
Amol Jadicb524072012-08-09 16:40:18 -07002834#if DEVICE_TREE
Matthew Qin271927e2015-03-31 22:07:07 -04002835 scratch_offset = image_actual + page_size + out_len;
Amol Jadicb524072012-08-09 16:40:18 -07002836 /* find correct dtb and copy it to right location */
Matthew Qin271927e2015-03-31 22:07:07 -04002837 ret = copy_dtb(data, scratch_offset);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002838
2839 dtb_copied = !ret ? 1 : 0;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002840#else
Mayank Grovere559cec2017-10-17 15:12:03 +05302841 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE) ||
2842 check_ddr_addr_range_bound(hdr->tags_addr, MAX_TAGS_SIZE))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002843 {
Mayank Grovere559cec2017-10-17 15:12:03 +05302844 dprintf(CRITICAL, "Tags addresses are not valid.\n");
lijuang2008ff22016-03-07 17:56:27 +08002845 goto boot_failed;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002846 }
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002847#endif
2848
2849 /* Load ramdisk & kernel */
2850 memmove((void*) hdr->ramdisk_addr, ptr + page_size + kernel_actual, hdr->ramdisk_size);
Matthew Qinbb7923d2015-02-09 10:56:09 +08002851 memmove((void*) hdr->kernel_addr, (char*) (kernel_start_addr), kernel_size);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002852
2853#if DEVICE_TREE
Mayank Grovere559cec2017-10-17 15:12:03 +05302854 if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual) ||
2855 check_ddr_addr_range_bound(hdr->tags_addr, kernel_actual))
Matthew Qinbb7923d2015-02-09 10:56:09 +08002856 {
Mayank Grovere559cec2017-10-17 15:12:03 +05302857 dprintf(CRITICAL, "Tags addresses are not valid.\n");
lijuang2008ff22016-03-07 17:56:27 +08002858 goto boot_failed;
Matthew Qinbb7923d2015-02-09 10:56:09 +08002859 }
2860
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002861 /*
2862 * If dtb is not found look for appended DTB in the kernel.
2863 * If appended dev tree is found, update the atags with
2864 * memory address to the DTB appended location on RAM.
2865 * Else update with the atags address in the kernel header
2866 */
2867 if (!dtb_copied) {
2868 void *dtb;
Matthew Qinbb7923d2015-02-09 10:56:09 +08002869 dtb = dev_tree_appended((void*)(ptr + page_size),
2870 hdr->kernel_size, dtb_offset,
Dima Zavine63e5572013-05-03 12:23:06 -07002871 (void *)hdr->tags_addr);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002872 if (!dtb) {
2873 fastboot_fail("dtb not found");
lijuang2008ff22016-03-07 17:56:27 +08002874 goto boot_failed;
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002875 }
Amol Jadicb524072012-08-09 16:40:18 -07002876 }
2877#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08002878
2879 fastboot_okay("");
Amol Jadi7c4316c2013-10-07 14:19:26 -07002880 fastboot_stop();
Brian Swetland9c4c0752009-01-25 16:23:50 -08002881
Dima Zavin77e41f32013-03-06 16:10:43 -08002882 boot_linux((void*) hdr->kernel_addr, (void*) hdr->tags_addr,
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002883 (const char*) hdr->cmdline, board_machtype(),
2884 (void*) hdr->ramdisk_addr, hdr->ramdisk_size);
lijuang2008ff22016-03-07 17:56:27 +08002885
2886 /* fastboot already stop, it's no need to show fastboot menu */
2887 return;
2888boot_failed:
2889#if FBCON_DISPLAY_MSG
2890 /* revert to fastboot menu if boot failed */
2891 display_fastboot_menu();
2892#endif
2893 return;
Brian Swetland9c4c0752009-01-25 16:23:50 -08002894}
2895
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002896void cmd_erase_nand(const char *arg, void *data, unsigned sz)
Dima Zavin214cc642009-01-26 11:16:21 -08002897{
2898 struct ptentry *ptn;
2899 struct ptable *ptable;
2900
2901 ptable = flash_get_ptable();
2902 if (ptable == NULL) {
2903 fastboot_fail("partition table doesn't exist");
2904 return;
2905 }
2906
2907 ptn = ptable_find(ptable, arg);
2908 if (ptn == NULL) {
2909 fastboot_fail("unknown partition name");
2910 return;
2911 }
2912
Monika Singh292b3e92018-03-17 22:40:23 +05302913 if (!strncmp(arg, "avb_custom_key", strlen("avb_custom_key"))) {
2914 dprintf(INFO, "erasing avb_custom_key\n");
2915 if (erase_userkey()) {
2916 fastboot_fail("Erasing avb_custom_key failed");
2917 } else {
2918 fastboot_okay("");
2919 }
2920 return;
2921 }
2922
Dima Zavin214cc642009-01-26 11:16:21 -08002923 if (flash_erase(ptn)) {
2924 fastboot_fail("failed to erase partition");
2925 return;
2926 }
2927 fastboot_okay("");
2928}
2929
Bikas Gurungd48bd242010-09-04 19:54:32 -07002930
2931void cmd_erase_mmc(const char *arg, void *data, unsigned sz)
2932{
2933 unsigned long long ptn = 0;
Oliver Wangcee448d2013-10-22 18:40:13 +08002934 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07002935 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002936 uint8_t lun = 0;
Vijay Kumar Pendoti0b21f462016-05-02 17:09:18 +05302937 char *footer = NULL;
Bikas Gurungd48bd242010-09-04 19:54:32 -07002938
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002939#if VERIFIED_BOOT
2940 if(!strcmp(arg, KEYSTORE_PTN_NAME))
2941 {
2942 if(!device.is_unlocked)
2943 {
2944 fastboot_fail("unlock device to erase keystore");
2945 return;
2946 }
2947 }
2948#endif
2949
Kinson Chikf1a43512011-07-14 11:28:39 -07002950 index = partition_get_index(arg);
2951 ptn = partition_get_offset(index);
Oliver Wangcee448d2013-10-22 18:40:13 +08002952 size = partition_get_size(index);
Neeti Desaica8c9602011-10-06 11:40:00 -07002953
Kinson Chikf1a43512011-07-14 11:28:39 -07002954 if(ptn == 0) {
Neeti Desaica8c9602011-10-06 11:40:00 -07002955 fastboot_fail("Partition table doesn't exist\n");
Bikas Gurungd48bd242010-09-04 19:54:32 -07002956 return;
2957 }
Kun Liang2f1601a2013-08-12 16:29:54 +08002958
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002959 lun = partition_get_lun(index);
2960 mmc_set_lun(lun);
2961
Monika Singh292b3e92018-03-17 22:40:23 +05302962 if (!strncmp(arg, "avb_custom_key", strlen("avb_custom_key"))) {
2963 dprintf(INFO, "erasing avb_custom_key\n");
2964 if (erase_userkey()) {
2965 fastboot_fail("Erasing avb_custom_key failed");
2966 } else {
2967 fastboot_okay("");
2968 }
2969 return;
2970 }
Sridhar Parasuramd7957122015-02-27 11:33:40 -08002971 if (platform_boot_dev_isemmc())
2972 {
2973 if (mmc_erase_card(ptn, size)) {
2974 fastboot_fail("failed to erase partition\n");
2975 return;
2976 }
2977 } else {
2978 BUF_DMA_ALIGN(out, DEFAULT_ERASE_SIZE);
2979 size = partition_get_size(index);
2980 if (size > DEFAULT_ERASE_SIZE)
2981 size = DEFAULT_ERASE_SIZE;
Kun Liang2f1601a2013-08-12 16:29:54 +08002982
Sridhar Parasuramd7957122015-02-27 11:33:40 -08002983 /* Simple inefficient version of erase. Just writing
2984 0 in first several blocks */
2985 if (mmc_write(ptn , size, (unsigned int *)out)) {
2986 fastboot_fail("failed to erase partition");
2987 return;
2988 }
Vijay Kumar Pendoti0b21f462016-05-02 17:09:18 +05302989 /*Erase FDE metadata at the userdata footer*/
2990 if(!(strncmp(arg, "userdata", 8)))
2991 {
2992 footer = memalign(CACHE_LINE, FOOTER_SIZE);
2993 memset((void *)footer, 0, FOOTER_SIZE);
2994
2995 size = partition_get_size(index);
2996
2997 if (mmc_write((ptn + size) - FOOTER_SIZE , FOOTER_SIZE, (unsigned int *)footer)) {
2998 fastboot_fail("failed to erase userdata footer");
2999 free(footer);
3000 return;
3001 }
3002 free(footer);
3003 }
Bikas Gurungd48bd242010-09-04 19:54:32 -07003004 }
Monika Singh292b3e92018-03-17 22:40:23 +05303005#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover912eaa62017-10-26 12:08:53 +05303006 if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05303007 !(strncmp(arg, "userdata", 8)) &&
3008 send_delete_keys_to_tz())
Sridhar Parasuram32b30662015-07-10 13:33:22 -07003009 ASSERT(0);
3010#endif
Bikas Gurungd48bd242010-09-04 19:54:32 -07003011 fastboot_okay("");
3012}
3013
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003014void cmd_erase(const char *arg, void *data, unsigned sz)
3015{
Monika Singh292b3e92018-03-17 22:40:23 +05303016#if VERIFIED_BOOT || VERIFIED_BOOT_2
Channagoud Kadabi35297672015-06-13 11:09:49 -07003017 if (target_build_variant_user())
Channagoud Kadabice3cf422015-04-17 16:02:30 -07003018 {
Sridhar Parasuramc32d07d2015-07-12 10:57:48 -07003019 if(!device.is_unlocked)
Channagoud Kadabice3cf422015-04-17 16:02:30 -07003020 {
Channagoud Kadabi35297672015-06-13 11:09:49 -07003021 fastboot_fail("device is locked. Cannot erase");
Channagoud Kadabice3cf422015-04-17 16:02:30 -07003022 return;
3023 }
3024 }
3025#endif
3026
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003027 if(target_is_emmc_boot())
3028 cmd_erase_mmc(arg, data, sz);
3029 else
3030 cmd_erase_nand(arg, data, sz);
3031}
Bikas Gurungd48bd242010-09-04 19:54:32 -07003032
Mayank Grover11ff9692018-01-11 11:54:49 +05303033/* Get the size from partiton name */
3034static void get_partition_size(const char *arg, char *response)
3035{
3036 uint64_t ptn = 0;
3037 uint64_t size;
3038 int index = INVALID_PTN;
3039
3040 index = partition_get_index(arg);
3041
3042 if (index == INVALID_PTN)
3043 {
3044 dprintf(CRITICAL, "Invalid partition index\n");
3045 return;
3046 }
3047
3048 ptn = partition_get_offset(index);
3049
3050 if(!ptn)
3051 {
3052 dprintf(CRITICAL, "Invalid partition name %s\n", arg);
3053 return;
3054 }
3055
3056 size = partition_get_size(index);
3057
3058 snprintf(response, MAX_RSP_SIZE, "\t 0x%llx", size);
3059 return;
3060}
3061
Mayank Grover52cd10a2018-03-15 12:57:54 +05303062/* Function to check partition type of a partition*/
3063static fs_signature_type
3064check_partition_fs_signature(const char *arg)
3065{
3066 fs_signature_type ret = NO_FS;
3067 int index;
3068 unsigned long long ptn;
3069 char *sb_buffer = malloc(mmc_blocksize);
3070 if (!sb_buffer)
3071 {
3072 dprintf(CRITICAL, "ERROR: Failed to allocate buffer for superblock\n");
3073 goto out;
3074 }
3075
3076 /* Read super block */
3077 if ((index = partition_get_index(arg)) < 0)
3078 {
3079 dprintf(CRITICAL, "ERROR: %s() doesn't exsit\n", arg);
3080 goto out;
3081 }
3082 ptn = partition_get_offset(index);
3083 mmc_set_lun(partition_get_lun(index));
3084 if(mmc_read(ptn + FS_SUPERBLOCK_OFFSET,
3085 (void *)sb_buffer, mmc_blocksize))
3086 {
3087 dprintf(CRITICAL, "ERROR: Failed to read Superblock\n");
3088 goto out;
3089 }
3090
3091 if (*((uint16 *)(&sb_buffer[EXT_MAGIC_OFFSET_SB]))
3092 == (uint16)EXT_MAGIC)
3093 {
3094 dprintf(SPEW, "%s() Found EXT FS\n", arg);
3095 ret = EXT_FS_SIGNATURE;
3096 }
3097 else if (*((uint32 *)(&sb_buffer[F2FS_MAGIC_OFFSET_SB]))
3098 == F2FS_MAGIC)
3099 {
3100 dprintf(SPEW, "%s() Found F2FS FS\n", arg);
3101 ret = EXT_F2FS_SIGNATURE;
3102 }
3103 else
3104 {
3105 dprintf(SPEW, "%s() Reverting to default 0x%x\n",
3106 arg, *((uint16 *)(&sb_buffer[EXT_MAGIC_OFFSET_SB])));
3107 ret = NO_FS;
3108 }
3109
3110out:
3111 if(sb_buffer)
3112 free(sb_buffer);
3113 return ret;
3114}
3115
Mayank Groverd38fe012018-03-13 15:33:16 +05303116/* Function to get partition type */
3117static void get_partition_type(const char *arg, char *response)
3118{
3119 uint n = 0;
Mayank Grover52cd10a2018-03-15 12:57:54 +05303120 fs_signature_type fs_signature;
Mayank Groverd38fe012018-03-13 15:33:16 +05303121
3122 if (arg == NULL ||
3123 response == NULL)
3124 {
3125 dprintf(CRITICAL, "Invalid input parameter\n");
3126 return;
3127 }
3128
3129 /* By default copy raw to response */
3130 strncpy(response, RAW_STR, strlen(RAW_STR));
3131
3132 /* Mark partiton type for known paritions only */
3133 for (n=0; n < ARRAY_SIZE(part_type_known); n++)
3134 {
3135 if (!strncmp(part_type_known[n].part_name, arg, strlen(arg)))
3136 {
Mayank Grover52cd10a2018-03-15 12:57:54 +05303137 /* Check partition for FS signature */
3138 fs_signature = check_partition_fs_signature(arg);
3139 switch (fs_signature)
3140 {
3141 case EXT_FS_SIGNATURE:
3142 strncpy(response, EXT_STR, strlen(EXT_STR));
3143 break;
3144 case EXT_F2FS_SIGNATURE:
3145 strncpy(response, F2FS_STR, strlen(F2FS_STR));
3146 break;
3147 case NO_FS:
3148 strncpy(response, part_type_known[n].type_response, MAX_RSP_SIZE);
3149 }
Mayank Groverd38fe012018-03-13 15:33:16 +05303150 }
3151 }
3152 return;
3153}
3154
Mayank Grover11ff9692018-01-11 11:54:49 +05303155/*
3156 * Publish the partition type & size info
3157 * fastboot getvar will publish the required information.
3158 * fastboot getvar partition_size:<partition_name>: partition size in hex
3159 * fastboot getvar partition_type:<partition_name>: partition type (ext/fat)
3160 */
3161static void publish_getvar_partition_info(struct getvar_partition_info *info, uint8_t num_parts)
3162{
Mayank Groverd38fe012018-03-13 15:33:16 +05303163 uint8_t i;
Mayank Grover11ff9692018-01-11 11:54:49 +05303164 static bool published = false;
3165 struct partition_entry *ptn_entry =
3166 partition_get_partition_entries();
3167 memset(info, 0, sizeof(struct getvar_partition_info)* num_parts);
3168
3169 for (i = 0; i < num_parts; i++) {
3170 strlcat(info[i].part_name, (const char *)ptn_entry[i].name, MAX_RSP_SIZE);
3171 strlcat(info[i].getvar_size, "partition-size:", MAX_GET_VAR_NAME_SIZE);
3172 strlcat(info[i].getvar_type, "partition-type:", MAX_GET_VAR_NAME_SIZE);
3173
Mayank Groverd38fe012018-03-13 15:33:16 +05303174 get_partition_type(info[i].part_name, info[i].type_response);
Mayank Grover11ff9692018-01-11 11:54:49 +05303175 get_partition_size(info[i].part_name, info[i].size_response);
Mayank Grover11ff9692018-01-11 11:54:49 +05303176 if (strlcat(info[i].getvar_size, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
3177 {
3178 dprintf(CRITICAL, "partition size name truncated\n");
3179 return;
3180 }
3181 if (strlcat(info[i].getvar_type, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
3182 {
3183 dprintf(CRITICAL, "partition type name truncated\n");
3184 return;
3185 }
3186
3187 if (!published)
3188 {
3189 /* publish partition size & type info */
3190 fastboot_publish((const char *) info[i].getvar_size, (const char *) info[i].size_response);
3191 fastboot_publish((const char *) info[i].getvar_type, (const char *) info[i].type_response);
3192 }
3193 }
3194 if (!published)
3195 published = true;
3196}
3197
3198
Ajay Dudani5c761132011-04-07 20:19:04 -07003199void cmd_flash_mmc_img(const char *arg, void *data, unsigned sz)
Shashank Mittal23b8f422010-04-16 19:27:21 -07003200{
3201 unsigned long long ptn = 0;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07003202 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07003203 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003204 char *token = NULL;
3205 char *pname = NULL;
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07003206 char *sp;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003207 uint8_t lun = 0;
3208 bool lun_set = false;
Mayank Grover351a75e2017-05-30 20:06:08 +05303209 int current_active_slot = INVALID;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07003210
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07003211 token = strtok_r((char *)arg, ":", &sp);
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003212 pname = token;
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07003213 token = strtok_r(NULL, ":", &sp);
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003214 if(token)
3215 {
3216 lun = atoi(token);
3217 mmc_set_lun(lun);
3218 lun_set = true;
3219 }
3220
Mao Jinlong226f33a2014-07-04 17:24:10 +08003221 if (pname)
Greg Grisco6e754772011-06-23 12:19:39 -07003222 {
Channagoud Kadabiad259832015-05-29 11:14:17 -07003223 if (!strncmp(pname, "frp-unlock", strlen("frp-unlock")))
3224 {
3225 if (!aboot_frp_unlock(pname, data, sz))
3226 {
3227 fastboot_info("FRP unlock successful");
3228 fastboot_okay("");
3229 }
3230 else
3231 fastboot_fail("Secret key is invalid, please update the bootloader with secret key");
3232
3233 return;
3234 }
3235
Mao Jinlong226f33a2014-07-04 17:24:10 +08003236 if (!strcmp(pname, "partition"))
3237 {
3238 dprintf(INFO, "Attempt to write partition image.\n");
3239 if (write_partition(sz, (unsigned char *) data)) {
3240 fastboot_fail("failed to write partition");
Greg Grisco6e754772011-06-23 12:19:39 -07003241 return;
3242 }
Mayank Grover11ff9692018-01-11 11:54:49 +05303243 /* Re-publish partition table */
3244 publish_getvar_partition_info(part_info, partition_get_partition_count());
Mayank Grover351a75e2017-05-30 20:06:08 +05303245
3246 /* Rescan partition table to ensure we have multislot support*/
3247 if (partition_scan_for_multislot())
3248 {
3249 current_active_slot = partition_find_active_slot();
3250 dprintf(INFO, "Multislot supported: Slot %s active",
3251 (SUFFIX_SLOT(current_active_slot)));
3252 }
3253 partition_mark_active_slot(current_active_slot);
Greg Grisco6e754772011-06-23 12:19:39 -07003254 }
Mao Jinlong226f33a2014-07-04 17:24:10 +08003255 else
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003256 {
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003257#if VERIFIED_BOOT
3258 if(!strcmp(pname, KEYSTORE_PTN_NAME))
3259 {
3260 if(!device.is_unlocked)
3261 {
3262 fastboot_fail("unlock device to flash keystore");
3263 return;
3264 }
Gaurav Nebhwanic7313cc2015-12-15 22:25:04 +05303265 if(!boot_verify_validate_keystore((unsigned char *)data,sz))
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003266 {
3267 fastboot_fail("image is not a keystore file");
3268 return;
3269 }
3270 }
3271#endif
Mao Jinlong226f33a2014-07-04 17:24:10 +08003272 index = partition_get_index(pname);
3273 ptn = partition_get_offset(index);
3274 if(ptn == 0) {
3275 fastboot_fail("partition table doesn't exist");
3276 return;
3277 }
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003278
Mayank Grover351a75e2017-05-30 20:06:08 +05303279 if (!strncmp(pname, "boot", strlen("boot"))
3280 || !strcmp(pname, "recovery"))
3281 {
Mao Jinlong226f33a2014-07-04 17:24:10 +08003282 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
3283 fastboot_fail("image is not a boot image");
3284 return;
3285 }
Mayank Grover351a75e2017-05-30 20:06:08 +05303286
3287 /* Reset multislot_partition attributes in case of flashing boot */
3288 if (partition_multislot_is_supported())
3289 {
3290 partition_reset_attributes(index);
3291 }
Mao Jinlong226f33a2014-07-04 17:24:10 +08003292 }
3293
3294 if(!lun_set)
3295 {
3296 lun = partition_get_lun(index);
3297 mmc_set_lun(lun);
3298 }
3299
3300 size = partition_get_size(index);
Gaurav Nebhwani4d2389c2016-03-17 21:10:05 +05303301 if (ROUND_TO_PAGE(sz, mmc_blocksize_mask) > size) {
Mao Jinlong226f33a2014-07-04 17:24:10 +08003302 fastboot_fail("size too large");
3303 return;
3304 }
3305 else if (mmc_write(ptn , sz, (unsigned int *)data)) {
3306 fastboot_fail("flash write failure");
3307 return;
3308 }
Greg Grisco6e754772011-06-23 12:19:39 -07003309 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07003310 }
3311 fastboot_okay("");
3312 return;
3313}
3314
Ajay Dudanide984792015-03-02 09:57:41 -08003315void cmd_flash_meta_img(const char *arg, void *data, unsigned sz)
3316{
3317 int i, images;
3318 meta_header_t *meta_header;
3319 img_header_entry_t *img_header_entry;
Parth Dixit3e2d3032016-03-04 17:11:52 +05303320 /*End of the image address*/
3321 uintptr_t data_end;
3322
3323 if( (UINT_MAX - sz) > (uintptr_t)data )
3324 data_end = (uintptr_t)data + sz;
3325 else
3326 {
3327 fastboot_fail("Cannot flash: image header corrupt");
3328 return;
3329 }
3330
3331 if( data_end < ((uintptr_t)data + sizeof(meta_header_t)))
3332 {
3333 fastboot_fail("Cannot flash: image header corrupt");
3334 return;
3335 }
Ajay Dudanide984792015-03-02 09:57:41 -08003336
lijuang8ee21882016-04-19 16:57:11 +08003337 /* If device is locked:
3338 * Forbid to flash image to avoid the device to bypass the image
3339 * which with "any" name other than bootloader. Because it maybe
3340 * a meta package of all partitions.
3341 */
Monika Singh292b3e92018-03-17 22:40:23 +05303342#if VERIFIED_BOOT || VERIFIED_BOOT_2
lijuang8ee21882016-04-19 16:57:11 +08003343 if (target_build_variant_user()) {
3344 if (!device.is_unlocked) {
3345 fastboot_fail("Device is locked, meta image flashing is not allowed");
3346 return;
3347 }
Mayank Grover889be1b2017-09-12 20:12:23 +05303348
Mayank Grover912eaa62017-10-26 12:08:53 +05303349 if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05303350 !device.is_unlock_critical)
3351 {
lijuang8ee21882016-04-19 16:57:11 +08003352 fastboot_fail("Device is critical locked, Meta image flashing is not allowed");
3353 return;
3354 }
lijuang8ee21882016-04-19 16:57:11 +08003355 }
3356#endif
3357
Ajay Dudanide984792015-03-02 09:57:41 -08003358 meta_header = (meta_header_t*) data;
Parth Dixit3e2d3032016-03-04 17:11:52 +05303359 if( data_end < ((uintptr_t)data + meta_header->img_hdr_sz))
3360 {
3361 fastboot_fail("Cannot flash: image header corrupt");
3362 return;
3363 }
Ajay Dudanide984792015-03-02 09:57:41 -08003364 img_header_entry = (img_header_entry_t*) (data+sizeof(meta_header_t));
3365
3366 images = meta_header->img_hdr_sz / sizeof(img_header_entry_t);
3367
3368 for (i=0; i<images; i++) {
3369
3370 if((img_header_entry[i].ptn_name == NULL) ||
3371 (img_header_entry[i].start_offset == 0) ||
3372 (img_header_entry[i].size == 0))
3373 break;
Kishor PK49831502017-04-21 17:55:43 +05303374 if ((UINT_MAX - img_header_entry[i].start_offset) < (uintptr_t)data) {
3375 fastboot_fail("Integer overflow detected in start_offset of img");
3376 break;
3377 }
3378 else if ((UINT_MAX - (img_header_entry[i].start_offset + (uintptr_t)data)) < img_header_entry[i].size) {
3379 fastboot_fail("Integer overflow detected in size of img");
3380 break;
3381 }
Parth Dixit3e2d3032016-03-04 17:11:52 +05303382 if( data_end < ((uintptr_t)data + img_header_entry[i].start_offset
3383 + img_header_entry[i].size) )
3384 {
3385 fastboot_fail("Cannot flash: image size mismatch");
3386 break;
3387 }
3388
Ajay Dudanide984792015-03-02 09:57:41 -08003389 cmd_flash_mmc_img(img_header_entry[i].ptn_name,
3390 (void *) data + img_header_entry[i].start_offset,
3391 img_header_entry[i].size);
3392 }
3393
Channagoud Kadabiaafbbfc2015-03-04 17:52:56 -08003394 if (!strncmp(arg, "bootloader", strlen("bootloader")))
3395 {
3396 strlcpy(device.bootloader_version, TARGET(BOARD), MAX_VERSION_LEN);
3397 strlcat(device.bootloader_version, "-", MAX_VERSION_LEN);
3398 strlcat(device.bootloader_version, meta_header->img_version, MAX_VERSION_LEN);
3399 }
3400 else
3401 {
3402 strlcpy(device.radio_version, TARGET(BOARD), MAX_VERSION_LEN);
3403 strlcat(device.radio_version, "-", MAX_VERSION_LEN);
3404 strlcat(device.radio_version, meta_header->img_version, MAX_VERSION_LEN);
3405 }
3406
3407 write_device_info(&device);
Ajay Dudanide984792015-03-02 09:57:41 -08003408 fastboot_okay("");
3409 return;
3410}
3411
Ajay Dudani5c761132011-04-07 20:19:04 -07003412void cmd_flash_mmc_sparse_img(const char *arg, void *data, unsigned sz)
3413{
3414 unsigned int chunk;
wufeng.jiang813dc352015-06-02 23:02:46 -04003415 uint64_t chunk_data_sz;
Channagoud Kadabif627cf82013-09-09 14:08:20 -07003416 uint32_t *fill_buf = NULL;
3417 uint32_t fill_val;
Mayank Grover4f50bba2017-07-19 18:17:08 +05303418 uint32_t blk_sz_actual = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07003419 sparse_header_t *sparse_header;
3420 chunk_header_t *chunk_header;
Ajay Dudaniab18f022011-05-12 14:39:22 -07003421 uint32_t total_blocks = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07003422 unsigned long long ptn = 0;
Channagoud Kadabi65b91002011-10-11 17:34:33 +05303423 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07003424 int index = INVALID_PTN;
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05303425 uint32_t i;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003426 uint8_t lun = 0;
vijay kumar800255e2015-04-24 20:26:19 +05303427 /*End of the sparse image address*/
Parth Dixit64b1a482016-03-07 16:31:26 +05303428 uintptr_t data_end = (uintptr_t)data + sz;
Ajay Dudani5c761132011-04-07 20:19:04 -07003429
Kinson Chikf1a43512011-07-14 11:28:39 -07003430 index = partition_get_index(arg);
3431 ptn = partition_get_offset(index);
3432 if(ptn == 0) {
Ajay Dudani5c761132011-04-07 20:19:04 -07003433 fastboot_fail("partition table doesn't exist");
3434 return;
3435 }
3436
Channagoud Kadabi65b91002011-10-11 17:34:33 +05303437 size = partition_get_size(index);
Channagoud Kadabi65b91002011-10-11 17:34:33 +05303438
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003439 lun = partition_get_lun(index);
3440 mmc_set_lun(lun);
3441
vijay kumar800255e2015-04-24 20:26:19 +05303442 if (sz < sizeof(sparse_header_t)) {
3443 fastboot_fail("size too low");
3444 return;
3445 }
3446
Ajay Dudani5c761132011-04-07 20:19:04 -07003447 /* Read and skip over sparse image header */
3448 sparse_header = (sparse_header_t *) data;
vijay kumar800255e2015-04-24 20:26:19 +05303449
Mayank Grover48bd9bb2017-07-19 12:04:16 +05303450 if (!sparse_header->blk_sz || (sparse_header->blk_sz % 4)){
3451 fastboot_fail("Invalid block size\n");
3452 return;
3453 }
3454
vijay kumar1321f342015-03-27 12:13:42 +05303455 if (((uint64_t)sparse_header->total_blks * (uint64_t)sparse_header->blk_sz) > size) {
Ajay Dudani876b3282012-12-21 14:12:17 -08003456 fastboot_fail("size too large");
3457 return;
3458 }
3459
vijay kumarde4fcf62015-04-23 13:05:49 +05303460 data += sizeof(sparse_header_t);
vijay kumar800255e2015-04-24 20:26:19 +05303461
Parth Dixit64b1a482016-03-07 16:31:26 +05303462 if (data_end < (uintptr_t)data) {
vijay kumar800255e2015-04-24 20:26:19 +05303463 fastboot_fail("buffer overreads occured due to invalid sparse header");
3464 return;
3465 }
3466
vijay kumarde4fcf62015-04-23 13:05:49 +05303467 if(sparse_header->file_hdr_sz != sizeof(sparse_header_t))
Ajay Dudani5c761132011-04-07 20:19:04 -07003468 {
vijay kumarde4fcf62015-04-23 13:05:49 +05303469 fastboot_fail("sparse header size mismatch");
3470 return;
Ajay Dudani5c761132011-04-07 20:19:04 -07003471 }
3472
Ajay Dudanib06c05f2011-05-12 14:46:10 -07003473 dprintf (SPEW, "=== Sparse Image Header ===\n");
3474 dprintf (SPEW, "magic: 0x%x\n", sparse_header->magic);
3475 dprintf (SPEW, "major_version: 0x%x\n", sparse_header->major_version);
3476 dprintf (SPEW, "minor_version: 0x%x\n", sparse_header->minor_version);
3477 dprintf (SPEW, "file_hdr_sz: %d\n", sparse_header->file_hdr_sz);
3478 dprintf (SPEW, "chunk_hdr_sz: %d\n", sparse_header->chunk_hdr_sz);
3479 dprintf (SPEW, "blk_sz: %d\n", sparse_header->blk_sz);
3480 dprintf (SPEW, "total_blks: %d\n", sparse_header->total_blks);
3481 dprintf (SPEW, "total_chunks: %d\n", sparse_header->total_chunks);
Ajay Dudani5c761132011-04-07 20:19:04 -07003482
3483 /* Start processing chunks */
3484 for (chunk=0; chunk<sparse_header->total_chunks; chunk++)
3485 {
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05303486 /* Make sure the total image size does not exceed the partition size */
3487 if(((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz) >= size) {
3488 fastboot_fail("size too large");
3489 return;
3490 }
Ajay Dudani5c761132011-04-07 20:19:04 -07003491 /* Read and skip over chunk header */
3492 chunk_header = (chunk_header_t *) data;
3493 data += sizeof(chunk_header_t);
3494
Parth Dixit64b1a482016-03-07 16:31:26 +05303495 if (data_end < (uintptr_t)data) {
vijay kumar800255e2015-04-24 20:26:19 +05303496 fastboot_fail("buffer overreads occured due to invalid sparse header");
3497 return;
3498 }
3499
Ajay Dudani5c761132011-04-07 20:19:04 -07003500 dprintf (SPEW, "=== Chunk Header ===\n");
3501 dprintf (SPEW, "chunk_type: 0x%x\n", chunk_header->chunk_type);
3502 dprintf (SPEW, "chunk_data_sz: 0x%x\n", chunk_header->chunk_sz);
3503 dprintf (SPEW, "total_size: 0x%x\n", chunk_header->total_sz);
3504
vijay kumar800255e2015-04-24 20:26:19 +05303505 if(sparse_header->chunk_hdr_sz != sizeof(chunk_header_t))
Ajay Dudani5c761132011-04-07 20:19:04 -07003506 {
vijay kumar800255e2015-04-24 20:26:19 +05303507 fastboot_fail("chunk header size mismatch");
3508 return;
Ajay Dudani5c761132011-04-07 20:19:04 -07003509 }
3510
wufeng.jiang813dc352015-06-02 23:02:46 -04003511 chunk_data_sz = (uint64_t)sparse_header->blk_sz * chunk_header->chunk_sz;
3512
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05303513 /* Make sure that the chunk size calculated from sparse image does not
3514 * exceed partition size
3515 */
3516 if ((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz + chunk_data_sz > size)
3517 {
3518 fastboot_fail("Chunk data size exceeds partition size");
3519 return;
3520 }
3521
Ajay Dudani5c761132011-04-07 20:19:04 -07003522 switch (chunk_header->chunk_type)
3523 {
3524 case CHUNK_TYPE_RAW:
wufeng.jiang813dc352015-06-02 23:02:46 -04003525 if((uint64_t)chunk_header->total_sz != ((uint64_t)sparse_header->chunk_hdr_sz +
Ajay Dudani5c761132011-04-07 20:19:04 -07003526 chunk_data_sz))
3527 {
3528 fastboot_fail("Bogus chunk size for chunk type Raw");
3529 return;
3530 }
3531
Parth Dixit64b1a482016-03-07 16:31:26 +05303532 if (data_end < (uintptr_t)data + chunk_data_sz) {
vijay kumar800255e2015-04-24 20:26:19 +05303533 fastboot_fail("buffer overreads occured due to invalid sparse header");
3534 return;
3535 }
3536
wufeng.jiang813dc352015-06-02 23:02:46 -04003537 /* chunk_header->total_sz is uint32,So chunk_data_sz is now less than 2^32
3538 otherwise it will return in the line above
3539 */
Ajay Dudaniab18f022011-05-12 14:39:22 -07003540 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
wufeng.jiang813dc352015-06-02 23:02:46 -04003541 (uint32_t)chunk_data_sz,
Ajay Dudaniab18f022011-05-12 14:39:22 -07003542 (unsigned int*)data))
Ajay Dudani5c761132011-04-07 20:19:04 -07003543 {
3544 fastboot_fail("flash write failure");
3545 return;
3546 }
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05303547 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
3548 fastboot_fail("Bogus size for RAW chunk type");
3549 return;
3550 }
Ajay Dudani5c761132011-04-07 20:19:04 -07003551 total_blocks += chunk_header->chunk_sz;
wufeng.jiang813dc352015-06-02 23:02:46 -04003552 data += (uint32_t)chunk_data_sz;
Ajay Dudani5c761132011-04-07 20:19:04 -07003553 break;
3554
Channagoud Kadabif627cf82013-09-09 14:08:20 -07003555 case CHUNK_TYPE_FILL:
3556 if(chunk_header->total_sz != (sparse_header->chunk_hdr_sz +
3557 sizeof(uint32_t)))
3558 {
3559 fastboot_fail("Bogus chunk size for chunk type FILL");
3560 return;
3561 }
3562
Mayank Grover4f50bba2017-07-19 18:17:08 +05303563 blk_sz_actual = ROUNDUP(sparse_header->blk_sz, CACHE_LINE);
3564 /* Integer overflow detected */
3565 if (blk_sz_actual < sparse_header->blk_sz)
3566 {
3567 fastboot_fail("Invalid block size");
3568 return;
3569 }
3570
3571 fill_buf = (uint32_t *)memalign(CACHE_LINE, blk_sz_actual);
Channagoud Kadabif627cf82013-09-09 14:08:20 -07003572 if (!fill_buf)
3573 {
3574 fastboot_fail("Malloc failed for: CHUNK_TYPE_FILL");
3575 return;
3576 }
3577
Parth Dixit64b1a482016-03-07 16:31:26 +05303578 if (data_end < (uintptr_t)data + sizeof(uint32_t)) {
vijay kumar800255e2015-04-24 20:26:19 +05303579 fastboot_fail("buffer overreads occured due to invalid sparse header");
Gaurav Nebhwani163cbf82016-03-17 21:21:27 +05303580 free(fill_buf);
vijay kumar800255e2015-04-24 20:26:19 +05303581 return;
3582 }
Channagoud Kadabif627cf82013-09-09 14:08:20 -07003583 fill_val = *(uint32_t *)data;
3584 data = (char *) data + sizeof(uint32_t);
Channagoud Kadabif627cf82013-09-09 14:08:20 -07003585
3586 for (i = 0; i < (sparse_header->blk_sz / sizeof(fill_val)); i++)
3587 {
3588 fill_buf[i] = fill_val;
3589 }
3590
Gaurav Nebhwanie94cbe12016-03-17 21:16:34 +05303591 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz))
3592 {
3593 fastboot_fail("bogus size for chunk FILL type");
3594 free(fill_buf);
3595 return;
3596 }
3597
wufeng.jiang813dc352015-06-02 23:02:46 -04003598 for (i = 0; i < chunk_header->chunk_sz; i++)
Channagoud Kadabif627cf82013-09-09 14:08:20 -07003599 {
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05303600 /* Make sure that the data written to partition does not exceed partition size */
3601 if ((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz + sparse_header->blk_sz > size)
3602 {
3603 fastboot_fail("Chunk data size for fill type exceeds partition size");
Gaurav Nebhwani163cbf82016-03-17 21:21:27 +05303604 free(fill_buf);
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05303605 return;
3606 }
3607
Channagoud Kadabif627cf82013-09-09 14:08:20 -07003608 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
3609 sparse_header->blk_sz,
3610 fill_buf))
3611 {
3612 fastboot_fail("flash write failure");
3613 free(fill_buf);
3614 return;
3615 }
3616
3617 total_blocks++;
3618 }
3619
3620 free(fill_buf);
3621 break;
3622
Ajay Dudani5c761132011-04-07 20:19:04 -07003623 case CHUNK_TYPE_DONT_CARE:
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05303624 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
3625 fastboot_fail("bogus size for chunk DONT CARE type");
3626 return;
3627 }
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07003628 total_blocks += chunk_header->chunk_sz;
3629 break;
3630
Ajay Dudani5c761132011-04-07 20:19:04 -07003631 case CHUNK_TYPE_CRC:
3632 if(chunk_header->total_sz != sparse_header->chunk_hdr_sz)
3633 {
wufeng.jiang813dc352015-06-02 23:02:46 -04003634 fastboot_fail("Bogus chunk size for chunk type CRC");
Ajay Dudani5c761132011-04-07 20:19:04 -07003635 return;
3636 }
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05303637 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
3638 fastboot_fail("bogus size for chunk CRC type");
3639 return;
3640 }
Ajay Dudani5c761132011-04-07 20:19:04 -07003641 total_blocks += chunk_header->chunk_sz;
Parth Dixit64b1a482016-03-07 16:31:26 +05303642 if ((uintptr_t)data > UINT_MAX - chunk_data_sz) {
vijay kumar800255e2015-04-24 20:26:19 +05303643 fastboot_fail("integer overflow occured");
3644 return;
3645 }
wufeng.jiang813dc352015-06-02 23:02:46 -04003646 data += (uint32_t)chunk_data_sz;
Parth Dixit64b1a482016-03-07 16:31:26 +05303647 if (data_end < (uintptr_t)data) {
vijay kumar800255e2015-04-24 20:26:19 +05303648 fastboot_fail("buffer overreads occured due to invalid sparse header");
3649 return;
3650 }
Ajay Dudani5c761132011-04-07 20:19:04 -07003651 break;
3652
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07003653 default:
Channagoud Kadabif627cf82013-09-09 14:08:20 -07003654 dprintf(CRITICAL, "Unkown chunk type: %x\n",chunk_header->chunk_type);
Ajay Dudani5c761132011-04-07 20:19:04 -07003655 fastboot_fail("Unknown chunk type");
3656 return;
3657 }
3658 }
3659
Ajay Dudani0c6927b2011-05-18 11:12:16 -07003660 dprintf(INFO, "Wrote %d blocks, expected to write %d blocks\n",
3661 total_blocks, sparse_header->total_blks);
3662
3663 if(total_blocks != sparse_header->total_blks)
3664 {
3665 fastboot_fail("sparse image write failure");
3666 }
Ajay Dudani5c761132011-04-07 20:19:04 -07003667
3668 fastboot_okay("");
3669 return;
3670}
3671
3672void cmd_flash_mmc(const char *arg, void *data, unsigned sz)
3673{
3674 sparse_header_t *sparse_header;
Ajay Dudanide984792015-03-02 09:57:41 -08003675 meta_header_t *meta_header;
Ajay Dudani5c761132011-04-07 20:19:04 -07003676
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07003677#ifdef SSD_ENABLE
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -08003678 /* 8 Byte Magic + 2048 Byte xml + Encrypted Data */
3679 unsigned int *magic_number = (unsigned int *) data;
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08003680 int ret=0;
3681 uint32 major_version=0;
3682 uint32 minor_version=0;
3683
3684 ret = scm_svc_version(&major_version,&minor_version);
3685 if(!ret)
3686 {
3687 if(major_version >= 2)
3688 {
Amir Samuelovbb65ce02013-05-05 12:20:18 +03003689 if( !strcmp(arg, "ssd") || !strcmp(arg, "tqs") )
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08003690 {
3691 ret = encrypt_scm((uint32 **) &data, &sz);
3692 if (ret != 0) {
3693 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
3694 return;
3695 }
3696
Amir Samuelovbb65ce02013-05-05 12:20:18 +03003697 /* Protect only for SSD */
3698 if (!strcmp(arg, "ssd")) {
3699 ret = scm_protect_keystore((uint32 *) data, sz);
3700 if (ret != 0) {
3701 dprintf(CRITICAL, "ERROR: scm_protect_keystore Failed\n");
3702 return;
3703 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08003704 }
3705 }
3706 else
3707 {
3708 ret = decrypt_scm_v2((uint32 **) &data, &sz);
3709 if(ret != 0)
3710 {
3711 dprintf(CRITICAL,"ERROR: Decryption Failure\n");
3712 return;
3713 }
3714 }
3715 }
3716 else
3717 {
3718 if (magic_number[0] == DECRYPT_MAGIC_0 &&
3719 magic_number[1] == DECRYPT_MAGIC_1)
3720 {
3721 ret = decrypt_scm((uint32 **) &data, &sz);
3722 if (ret != 0) {
3723 dprintf(CRITICAL, "ERROR: Invalid secure image\n");
3724 return;
3725 }
3726 }
3727 else if (magic_number[0] == ENCRYPT_MAGIC_0 &&
3728 magic_number[1] == ENCRYPT_MAGIC_1)
3729 {
3730 ret = encrypt_scm((uint32 **) &data, &sz);
3731 if (ret != 0) {
3732 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
3733 return;
3734 }
3735 }
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07003736 }
3737 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08003738 else
Neeti Desai127b9e02012-03-20 16:11:23 -07003739 {
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08003740 dprintf(CRITICAL,"INVALID SVC Version\n");
3741 return;
Neeti Desai127b9e02012-03-20 16:11:23 -07003742 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08003743#endif /* SSD_ENABLE */
Neeti Desai127b9e02012-03-20 16:11:23 -07003744
Monika Singh292b3e92018-03-17 22:40:23 +05303745#if VERIFIED_BOOT || VERIFIED_BOOT_2
Channagoud Kadabi35297672015-06-13 11:09:49 -07003746 if (target_build_variant_user())
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003747 {
lijuang511a2b52015-08-14 20:50:51 +08003748 /* if device is locked:
3749 * common partition will not allow to be flashed
3750 * critical partition will allow to flash image.
3751 */
3752 if(!device.is_unlocked && !critical_flash_allowed(arg)) {
3753 fastboot_fail("Partition flashing is not allowed");
3754 return;
3755 }
Mayank Grover889be1b2017-09-12 20:12:23 +05303756
lijuang511a2b52015-08-14 20:50:51 +08003757 /* if device critical is locked:
3758 * common partition will allow to be flashed
3759 * critical partition will not allow to flash image.
3760 */
Mayank Grover912eaa62017-10-26 12:08:53 +05303761 if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05303762 !device.is_unlock_critical &&
3763 critical_flash_allowed(arg)) {
3764 fastboot_fail("Critical partition flashing is not allowed");
3765 return;
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003766 }
3767 }
3768#endif
Monika Singh292b3e92018-03-17 22:40:23 +05303769 if (!strncmp(arg, "avb_custom_key", strlen("avb_custom_key"))) {
3770 dprintf(INFO, "flashing avb_custom_key\n");
3771 if (store_userkey(data, sz)) {
3772 fastboot_fail("Flashing avb_custom_key failed");
3773 } else {
3774 fastboot_okay("");
3775 }
3776 return;
3777 }
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003778
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07003779 sparse_header = (sparse_header_t *) data;
Ajay Dudanide984792015-03-02 09:57:41 -08003780 meta_header = (meta_header_t *) data;
3781 if (sparse_header->magic == SPARSE_HEADER_MAGIC)
Ajay Dudani5c761132011-04-07 20:19:04 -07003782 cmd_flash_mmc_sparse_img(arg, data, sz);
Ajay Dudanide984792015-03-02 09:57:41 -08003783 else if (meta_header->magic == META_HEADER_MAGIC)
3784 cmd_flash_meta_img(arg, data, sz);
3785 else
3786 cmd_flash_mmc_img(arg, data, sz);
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07003787
3788#if VERIFIED_BOOT
Mayank Grover912eaa62017-10-26 12:08:53 +05303789 if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05303790 (!strncmp(arg, "system", 6)) &&
3791 !device.verity_mode)
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07003792 // reset dm_verity mode to enforcing
3793 device.verity_mode = 1;
3794 write_device_info(&device);
Parth Dixitddbc7352015-10-18 03:13:31 +05303795#endif
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07003796
Ajay Dudani5c761132011-04-07 20:19:04 -07003797 return;
3798}
3799
Tanya Brokhman84eaaf62015-02-24 21:57:06 +02003800void cmd_updatevol(const char *vol_name, void *data, unsigned sz)
3801{
3802 struct ptentry *sys_ptn;
3803 struct ptable *ptable;
3804
3805 ptable = flash_get_ptable();
3806 if (ptable == NULL) {
3807 fastboot_fail("partition table doesn't exist");
3808 return;
3809 }
3810
3811 sys_ptn = ptable_find(ptable, "system");
3812 if (sys_ptn == NULL) {
3813 fastboot_fail("system partition not found");
3814 return;
3815 }
3816
3817 sz = ROUND_TO_PAGE(sz, page_mask);
3818 if (update_ubi_vol(sys_ptn, vol_name, data, sz))
3819 fastboot_fail("update_ubi_vol failed");
3820 else
3821 fastboot_okay("");
3822}
3823
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003824void cmd_flash_nand(const char *arg, void *data, unsigned sz)
Dima Zavin214cc642009-01-26 11:16:21 -08003825{
3826 struct ptentry *ptn;
3827 struct ptable *ptable;
3828 unsigned extra = 0;
Gaurav Nebhwanid9730712016-05-06 14:28:29 +05303829 uint64_t partition_size = 0;
Vijay Kumar Pendotic6f22a72017-05-19 22:44:34 +05303830 unsigned bytes_to_round_page = 0;
3831 unsigned rounded_size = 0;
Dima Zavin214cc642009-01-26 11:16:21 -08003832
Kishor PK38ed93d2017-04-25 14:19:26 +05303833 if((uintptr_t)data > (UINT_MAX - sz)) {
3834 fastboot_fail("Cannot flash: image header corrupt");
3835 return;
3836 }
3837
Dima Zavin214cc642009-01-26 11:16:21 -08003838 ptable = flash_get_ptable();
3839 if (ptable == NULL) {
3840 fastboot_fail("partition table doesn't exist");
3841 return;
3842 }
3843
3844 ptn = ptable_find(ptable, arg);
3845 if (ptn == NULL) {
Tanya Brokhman84eaaf62015-02-24 21:57:06 +02003846 dprintf(INFO, "unknown partition name (%s). Trying updatevol\n",
3847 arg);
3848 cmd_updatevol(arg, data, sz);
Dima Zavin214cc642009-01-26 11:16:21 -08003849 return;
3850 }
3851
Monika Singh292b3e92018-03-17 22:40:23 +05303852 if (!strncmp(arg, "avb_custom_key", strlen("avb_custom_key"))) {
3853 dprintf(INFO, "flashing avb_custom_key\n");
3854 if (store_userkey(data, sz)) {
3855 fastboot_fail("Flashing avb_custom_key failed");
3856 } else {
3857 fastboot_okay("");
3858 }
3859 return;
3860 }
3861
Dima Zavin214cc642009-01-26 11:16:21 -08003862 if (!strcmp(ptn->name, "boot") || !strcmp(ptn->name, "recovery")) {
Kishor PK38ed93d2017-04-25 14:19:26 +05303863 if((sz > BOOT_MAGIC_SIZE) && (!memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE))) {
3864 dprintf(INFO, "Verified the BOOT_MAGIC in image header \n");
3865 } else {
3866 fastboot_fail("Image is not a boot image");
Dima Zavin214cc642009-01-26 11:16:21 -08003867 return;
3868 }
3869 }
3870
Amol Jadi5c61a952012-05-04 17:05:35 -07003871 if (!strcmp(ptn->name, "system")
Deepa Dinamani13e32c42012-03-12 14:34:17 -07003872 || !strcmp(ptn->name, "userdata")
3873 || !strcmp(ptn->name, "persist")
Sundarajan Srinivasanb063a852013-11-19 14:02:27 -08003874 || !strcmp(ptn->name, "recoveryfs")
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -08003875 || !strcmp(ptn->name, "modem"))
Tanya Brokhman1c94f1a2015-02-15 09:05:03 +02003876 extra = 1;
Kishor PK38ed93d2017-04-25 14:19:26 +05303877 else {
Vijay Kumar Pendotic6f22a72017-05-19 22:44:34 +05303878 rounded_size = ROUNDUP(sz, page_size);
3879 bytes_to_round_page = rounded_size - sz;
3880 if (bytes_to_round_page) {
3881 if (((uintptr_t)data + sz ) > (UINT_MAX - bytes_to_round_page)) {
3882 fastboot_fail("Integer overflow detected");
3883 return;
3884 }
3885 if (((uintptr_t)data + sz + bytes_to_round_page) >
3886 ((uintptr_t)target_get_scratch_address() + target_get_max_flash_size())) {
3887 fastboot_fail("Buffer size is not aligned to page_size");
3888 return;
3889 }
3890 else {
3891 memset(data + sz, 0, bytes_to_round_page);
3892 sz = rounded_size;
3893 }
Kishor PK38ed93d2017-04-25 14:19:26 +05303894 }
Gaurav Nebhwanid9730712016-05-06 14:28:29 +05303895 }
3896
Kishor PK38ed93d2017-04-25 14:19:26 +05303897 /*Checking partition_size for the possible integer overflow */
3898 partition_size = validate_partition_size(ptn);
3899
Gaurav Nebhwanid9730712016-05-06 14:28:29 +05303900 if (sz > partition_size) {
3901 fastboot_fail("Image size too large");
3902 return;
3903 }
3904
Dima Zavin214cc642009-01-26 11:16:21 -08003905 dprintf(INFO, "writing %d bytes to '%s'\n", sz, ptn->name);
Mayank Grover6cde9352017-06-06 18:45:23 +05303906 if ((sz > UBI_EC_HDR_SIZE) &&
3907 (!memcmp((void *)data, UBI_MAGIC, UBI_MAGIC_SIZE))) {
Tanya Brokhman1c94f1a2015-02-15 09:05:03 +02003908 if (flash_ubi_img(ptn, data, sz)) {
3909 fastboot_fail("flash write failure");
3910 return;
3911 }
3912 } else {
3913 if (flash_write(ptn, extra, data, sz)) {
3914 fastboot_fail("flash write failure");
3915 return;
3916 }
Dima Zavin214cc642009-01-26 11:16:21 -08003917 }
3918 dprintf(INFO, "partition '%s' updated\n", ptn->name);
3919 fastboot_okay("");
3920}
3921
Kishor PK38ed93d2017-04-25 14:19:26 +05303922
3923static inline uint64_t validate_partition_size(struct ptentry *ptn)
3924{
3925 if (ptn->length && flash_num_pages_per_blk() && page_size) {
3926 if ((ptn->length < ( UINT_MAX / flash_num_pages_per_blk())) && ((ptn->length * flash_num_pages_per_blk()) < ( UINT_MAX / page_size))) {
3927 return ptn->length * flash_num_pages_per_blk() * page_size;
3928 }
3929 }
3930 return 0;
3931}
3932
3933
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003934void cmd_flash(const char *arg, void *data, unsigned sz)
3935{
3936 if(target_is_emmc_boot())
3937 cmd_flash_mmc(arg, data, sz);
3938 else
3939 cmd_flash_nand(arg, data, sz);
3940}
3941
Dima Zavin214cc642009-01-26 11:16:21 -08003942void cmd_continue(const char *arg, void *data, unsigned sz)
3943{
3944 fastboot_okay("");
Amol Jadi7c4316c2013-10-07 14:19:26 -07003945 fastboot_stop();
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003946
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003947 if (target_is_emmc_boot())
3948 {
lijuanga40d6302015-07-20 20:10:13 +08003949#if FBCON_DISPLAY_MSG
lijuangde34d502016-02-26 16:04:50 +08003950 /* Exit keys' detection thread firstly */
3951 exit_menu_keys_detection();
lijuanga40d6302015-07-20 20:10:13 +08003952#endif
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003953 boot_linux_from_mmc();
3954 }
3955 else
3956 {
3957 boot_linux_from_flash();
3958 }
Dima Zavin214cc642009-01-26 11:16:21 -08003959}
3960
Chandan Uddaraju94183c02010-01-15 15:13:59 -08003961void cmd_reboot(const char *arg, void *data, unsigned sz)
3962{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003963 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08003964 fastboot_okay("");
3965 reboot_device(0);
3966}
3967
Mayank Grover351a75e2017-05-30 20:06:08 +05303968void cmd_set_active(const char *arg, void *data, unsigned sz)
3969{
Mayank Grover6ccc1c92017-07-04 17:36:46 +05303970 char *p, *sp = NULL;
Mayank Grover351a75e2017-05-30 20:06:08 +05303971 unsigned i,current_active_slot;
3972 const char *current_slot_suffix;
3973
3974 if (!partition_multislot_is_supported())
3975 {
3976 fastboot_fail("Command not supported");
3977 return;
3978 }
3979
3980 if (arg)
3981 {
Mayank Grover6ccc1c92017-07-04 17:36:46 +05303982 p = strtok_r((char *)arg, ":", &sp);
Mayank Grover5eb5f692017-07-19 20:16:04 +05303983 if (p)
Mayank Grover351a75e2017-05-30 20:06:08 +05303984 {
3985 current_active_slot = partition_find_active_slot();
3986
3987 /* Check if trying to make curent slot active */
3988 current_slot_suffix = SUFFIX_SLOT(current_active_slot);
Mayank Grover6ccc1c92017-07-04 17:36:46 +05303989 current_slot_suffix = strtok_r((char *)current_slot_suffix,
3990 (char *)suffix_delimiter, &sp);
3991
Mayank Grover5eb5f692017-07-19 20:16:04 +05303992 if (current_slot_suffix &&
3993 !strncmp(p, current_slot_suffix, strlen(current_slot_suffix)))
Mayank Grover351a75e2017-05-30 20:06:08 +05303994 {
3995 fastboot_okay("Slot already set active");
3996 return;
3997 }
3998 else
3999 {
4000 for (i = 0; i < AB_SUPPORTED_SLOTS; i++)
4001 {
4002 current_slot_suffix = SUFFIX_SLOT(i);
Mayank Grover6ccc1c92017-07-04 17:36:46 +05304003 current_slot_suffix = strtok_r((char *)current_slot_suffix,
4004 (char *)suffix_delimiter, &sp);
Mayank Grover5eb5f692017-07-19 20:16:04 +05304005 if (current_slot_suffix &&
4006 !strncmp(p, current_slot_suffix, strlen(current_slot_suffix)))
Mayank Grover351a75e2017-05-30 20:06:08 +05304007 {
4008 partition_switch_slots(current_active_slot, i);
4009 publish_getvar_multislot_vars();
4010 fastboot_okay("");
4011 return;
4012 }
4013 }
4014 }
4015 }
4016 }
4017 fastboot_fail("Invalid slot suffix.");
4018 return;
4019}
4020
Chandan Uddaraju94183c02010-01-15 15:13:59 -08004021void cmd_reboot_bootloader(const char *arg, void *data, unsigned sz)
4022{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004023 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08004024 fastboot_okay("");
4025 reboot_device(FASTBOOT_MODE);
4026}
4027
Ameya Thakur11cf1a62013-08-05 12:44:48 -07004028void cmd_oem_enable_charger_screen(const char *arg, void *data, unsigned size)
4029{
4030 dprintf(INFO, "Enabling charger screen check\n");
4031 device.charger_screen_enabled = 1;
4032 write_device_info(&device);
4033 fastboot_okay("");
4034}
4035
4036void cmd_oem_disable_charger_screen(const char *arg, void *data, unsigned size)
4037{
4038 dprintf(INFO, "Disabling charger screen check\n");
4039 device.charger_screen_enabled = 0;
4040 write_device_info(&device);
4041 fastboot_okay("");
4042}
4043
lijuanga25d8bb2015-09-16 13:11:52 +08004044void cmd_oem_off_mode_charger(const char *arg, void *data, unsigned size)
4045{
4046 char *p = NULL;
4047 const char *delim = " \t\n\r";
Parth Dixit407f15b2016-01-07 14:47:37 +05304048 char *sp;
lijuanga25d8bb2015-09-16 13:11:52 +08004049
4050 if (arg) {
Parth Dixit407f15b2016-01-07 14:47:37 +05304051 p = strtok_r((char *)arg, delim, &sp);
lijuanga25d8bb2015-09-16 13:11:52 +08004052 if (p) {
4053 if (!strncmp(p, "0", 1)) {
4054 device.charger_screen_enabled = 0;
4055 } else if (!strncmp(p, "1", 1)) {
4056 device.charger_screen_enabled = 1;
4057 }
4058 }
4059 }
4060
4061 /* update charger_screen_enabled value for getvar
4062 * command
4063 */
4064 snprintf(charger_screen_enabled, MAX_RSP_SIZE, "%d",
4065 device.charger_screen_enabled);
4066
4067 write_device_info(&device);
4068 fastboot_okay("");
4069}
4070
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05304071void cmd_oem_select_display_panel(const char *arg, void *data, unsigned size)
4072{
4073 dprintf(INFO, "Selecting display panel %s\n", arg);
4074 if (arg)
4075 strlcpy(device.display_panel, arg,
4076 sizeof(device.display_panel));
4077 write_device_info(&device);
4078 fastboot_okay("");
4079}
4080
Shashank Mittal162244e2011-08-08 19:01:25 -07004081void cmd_oem_unlock(const char *arg, void *data, unsigned sz)
4082{
lijuang4ece1e72015-08-14 21:02:36 +08004083 set_device_unlock(UNLOCK, TRUE);
vijay kumarc65876c2015-04-24 13:29:16 +05304084}
4085
4086void cmd_oem_unlock_go(const char *arg, void *data, unsigned sz)
4087{
lijuang4ece1e72015-08-14 21:02:36 +08004088 if(!device.is_unlocked) {
vijay kumarc65876c2015-04-24 13:29:16 +05304089 if(!is_allow_unlock) {
4090 fastboot_fail("oem unlock is not allowed");
4091 return;
4092 }
4093
lijuang4ece1e72015-08-14 21:02:36 +08004094 set_device_unlock_value(UNLOCK, TRUE);
vijay kumarc65876c2015-04-24 13:29:16 +05304095
lijuang4ece1e72015-08-14 21:02:36 +08004096 /* wipe data */
vijay kumarc65876c2015-04-24 13:29:16 +05304097 struct recovery_message msg;
Kishor PK60a68212017-05-08 16:55:57 +05304098 memset(&msg, 0, sizeof(msg));
vijay kumarc65876c2015-04-24 13:29:16 +05304099 snprintf(msg.recovery, sizeof(msg.recovery), "recovery\n--wipe_data");
4100 write_misc(0, &msg, sizeof(msg));
4101
4102 fastboot_okay("");
4103 reboot_device(RECOVERY_MODE);
Shashank Mittald3e54dd2014-08-28 15:24:02 -07004104 }
4105 fastboot_okay("");
4106}
4107
Channagoud Kadabiad259832015-05-29 11:14:17 -07004108static int aboot_frp_unlock(char *pname, void *data, unsigned sz)
4109{
Mayank Grover70f8b0e2017-05-17 11:49:00 +05304110 int ret=1;
4111 bool authentication_success=false;
Channagoud Kadabiad259832015-05-29 11:14:17 -07004112
Mayank Grover70f8b0e2017-05-17 11:49:00 +05304113 /*
4114 Authentication method not implemented.
4115
4116 OEM to implement, authentication system which on successful validataion,
4117 calls write_allow_oem_unlock() with is_allow_unlock.
4118 */
4119#if 0
4120 authentication_success = oem_specific_auth_mthd();
4121#endif
4122
4123 if (authentication_success)
Channagoud Kadabiad259832015-05-29 11:14:17 -07004124 {
Mayank Grover70f8b0e2017-05-17 11:49:00 +05304125 is_allow_unlock = true;
4126 write_allow_oem_unlock(is_allow_unlock);
4127 ret = 0;
Channagoud Kadabiad259832015-05-29 11:14:17 -07004128 }
4129 return ret;
4130}
4131
Shashank Mittald3e54dd2014-08-28 15:24:02 -07004132void cmd_oem_lock(const char *arg, void *data, unsigned sz)
4133{
lijuang4ece1e72015-08-14 21:02:36 +08004134 set_device_unlock(UNLOCK, FALSE);
Shashank Mittal162244e2011-08-08 19:01:25 -07004135}
4136
Shashank Mittala0032282011-08-26 14:50:11 -07004137void cmd_oem_devinfo(const char *arg, void *data, unsigned sz)
4138{
lijuang511a2b52015-08-14 20:50:51 +08004139 char response[MAX_RSP_SIZE];
Ameya Thakur11cf1a62013-08-05 12:44:48 -07004140 snprintf(response, sizeof(response), "\tDevice tampered: %s", (device.is_tampered ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07004141 fastboot_info(response);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07004142 snprintf(response, sizeof(response), "\tDevice unlocked: %s", (device.is_unlocked ? "true" : "false"));
4143 fastboot_info(response);
Monika Singh292b3e92018-03-17 22:40:23 +05304144#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover912eaa62017-10-26 12:08:53 +05304145 if (VB_M <= target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +05304146 {
4147 snprintf(response, sizeof(response), "\tDevice critical unlocked: %s",
4148 (device.is_unlock_critical ? "true" : "false"));
4149 fastboot_info(response);
4150 }
Parth Dixitddbc7352015-10-18 03:13:31 +05304151#endif
Ameya Thakur11cf1a62013-08-05 12:44:48 -07004152 snprintf(response, sizeof(response), "\tCharger screen enabled: %s", (device.charger_screen_enabled ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07004153 fastboot_info(response);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05304154 snprintf(response, sizeof(response), "\tDisplay panel: %s", (device.display_panel));
4155 fastboot_info(response);
Shashank Mittala0032282011-08-26 14:50:11 -07004156 fastboot_okay("");
4157}
4158
lijuang511a2b52015-08-14 20:50:51 +08004159void cmd_flashing_get_unlock_ability(const char *arg, void *data, unsigned sz)
4160{
4161 char response[MAX_RSP_SIZE];
4162 snprintf(response, sizeof(response), "\tget_unlock_ability: %d", is_allow_unlock);
4163 fastboot_info(response);
4164 fastboot_okay("");
4165}
4166
4167void cmd_flashing_lock_critical(const char *arg, void *data, unsigned sz)
4168{
lijuang4ece1e72015-08-14 21:02:36 +08004169 set_device_unlock(UNLOCK_CRITICAL, FALSE);
lijuang511a2b52015-08-14 20:50:51 +08004170}
4171
4172void cmd_flashing_unlock_critical(const char *arg, void *data, unsigned sz)
4173{
lijuang4ece1e72015-08-14 21:02:36 +08004174 set_device_unlock(UNLOCK_CRITICAL, TRUE);
lijuang511a2b52015-08-14 20:50:51 +08004175}
4176
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07004177void cmd_preflash(const char *arg, void *data, unsigned sz)
4178{
4179 fastboot_okay("");
4180}
4181
Mao Flynn7b379f32015-04-20 00:28:30 +08004182static uint8_t logo_header[LOGO_IMG_HEADER_SIZE];
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304183
Mao Flynn7b379f32015-04-20 00:28:30 +08004184int splash_screen_check_header(logo_img_header *header)
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304185{
Mao Flynn7b379f32015-04-20 00:28:30 +08004186 if (memcmp(header->magic, LOGO_IMG_MAGIC, 8))
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304187 return -1;
Mao Flynn7b379f32015-04-20 00:28:30 +08004188 if (header->width == 0 || header->height == 0)
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304189 return -1;
4190 return 0;
4191}
4192
Mao Flynn7b379f32015-04-20 00:28:30 +08004193int splash_screen_flash()
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07004194{
4195 struct ptentry *ptn;
4196 struct ptable *ptable;
Mao Flynn7b379f32015-04-20 00:28:30 +08004197 struct logo_img_header *header;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07004198 struct fbcon_config *fb_display = NULL;
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08004199
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304200 ptable = flash_get_ptable();
4201 if (ptable == NULL) {
Mao Flynn7b379f32015-04-20 00:28:30 +08004202 dprintf(CRITICAL, "ERROR: Partition table not found\n");
4203 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304204 }
Mao Flynn7b379f32015-04-20 00:28:30 +08004205
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304206 ptn = ptable_find(ptable, "splash");
4207 if (ptn == NULL) {
4208 dprintf(CRITICAL, "ERROR: splash Partition not found\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08004209 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304210 }
Mao Flynn7b379f32015-04-20 00:28:30 +08004211 if (flash_read(ptn, 0, (void *)logo_header, LOGO_IMG_HEADER_SIZE)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304212 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08004213 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304214 }
4215
Mao Flynn7b379f32015-04-20 00:28:30 +08004216 header = (struct logo_img_header *)logo_header;
4217 if (splash_screen_check_header(header)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304218 dprintf(CRITICAL, "ERROR: Boot image header invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08004219 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304220 }
4221
4222 fb_display = fbcon_display();
4223 if (fb_display) {
Mao Flynn7b379f32015-04-20 00:28:30 +08004224 if (header->type && (header->blocks != 0)) { // RLE24 compressed data
4225 uint8_t *base = (uint8_t *) fb_display->base + LOGO_IMG_OFFSET;
4226
4227 /* if the logo is full-screen size, remove "fbcon_clear()" */
4228 if ((header->width != fb_display->width)
4229 || (header->height != fb_display->height))
4230 fbcon_clear();
4231
4232 if (flash_read(ptn + LOGO_IMG_HEADER_SIZE, 0,
4233 (uint32_t *)base,
4234 (header->blocks * 512))) {
4235 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
4236 return -1;
4237 }
4238 fbcon_extract_to_screen(header, base);
4239 return 0;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07004240 }
Mao Flynn7b379f32015-04-20 00:28:30 +08004241
Veera Sundaram Sankaranc33416f2015-09-28 10:25:55 -07004242 if ((header->width > fb_display->width) || (header->height > fb_display->height)) {
4243 dprintf(CRITICAL, "Logo config greater than fb config. Fall back default logo\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08004244 return -1;
4245 }
4246
Vineet Bajaj99291ed2014-09-09 12:29:46 +05304247 uint8_t *base = (uint8_t *) fb_display->base;
Sachin Bhayare619e9e42017-05-15 13:10:31 +05304248 uint32_t fb_size = ROUNDUP(fb_display->width *
4249 fb_display->height *
4250 (fb_display->bpp / 8), 4096);
4251 uint32_t splash_size = ((((header->width * header->height *
4252 fb_display->bpp/8) + 511) >> 9) << 9);
4253
4254 if (splash_size > fb_size) {
4255 dprintf(CRITICAL, "ERROR: Splash image size invalid\n");
4256 return -1;
4257 }
4258
Mao Flynn7b379f32015-04-20 00:28:30 +08004259 if (flash_read(ptn + LOGO_IMG_HEADER_SIZE, 0,
4260 (uint32_t *)base,
4261 ((((header->width * header->height * fb_display->bpp/8) + 511) >> 9) << 9))) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304262 fbcon_clear();
Vineet Bajaj99291ed2014-09-09 12:29:46 +05304263 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08004264 return -1;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07004265 }
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304266 }
4267
Mao Flynn7b379f32015-04-20 00:28:30 +08004268 return 0;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304269}
4270
Mao Flynn7b379f32015-04-20 00:28:30 +08004271int splash_screen_mmc()
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304272{
4273 int index = INVALID_PTN;
4274 unsigned long long ptn = 0;
4275 struct fbcon_config *fb_display = NULL;
Mao Flynn7b379f32015-04-20 00:28:30 +08004276 struct logo_img_header *header;
Mao Flynn1893a7f2015-06-03 12:03:36 +08004277 uint32_t blocksize, realsize, readsize;
4278 uint8_t *base;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304279
4280 index = partition_get_index("splash");
4281 if (index == 0) {
4282 dprintf(CRITICAL, "ERROR: splash Partition table not found\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08004283 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304284 }
4285
4286 ptn = partition_get_offset(index);
4287 if (ptn == 0) {
4288 dprintf(CRITICAL, "ERROR: splash Partition invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08004289 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304290 }
4291
Mao Flynn1893a7f2015-06-03 12:03:36 +08004292 mmc_set_lun(partition_get_lun(index));
4293
4294 blocksize = mmc_get_device_blocksize();
4295 if (blocksize == 0) {
4296 dprintf(CRITICAL, "ERROR:splash Partition invalid blocksize\n");
4297 return -1;
4298 }
4299
4300 fb_display = fbcon_display();
Channagoud Kadabidaf10a62015-07-22 11:51:55 -07004301 if (!fb_display)
4302 {
4303 dprintf(CRITICAL, "ERROR: fb config is not allocated\n");
4304 return -1;
4305 }
4306
Mao Flynn1893a7f2015-06-03 12:03:36 +08004307 base = (uint8_t *) fb_display->base;
4308
4309 if (mmc_read(ptn, (uint32_t *)(base + LOGO_IMG_OFFSET), blocksize)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304310 dprintf(CRITICAL, "ERROR: Cannot read splash image header\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08004311 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304312 }
4313
Mao Flynn1893a7f2015-06-03 12:03:36 +08004314 header = (struct logo_img_header *)(base + LOGO_IMG_OFFSET);
Mao Flynn7b379f32015-04-20 00:28:30 +08004315 if (splash_screen_check_header(header)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304316 dprintf(CRITICAL, "ERROR: Splash image header invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08004317 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304318 }
4319
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304320 if (fb_display) {
Mao Flynn1893a7f2015-06-03 12:03:36 +08004321 if (header->type && (header->blocks != 0)) { /* 1 RLE24 compressed data */
4322 base += LOGO_IMG_OFFSET;
Mao Flynn7b379f32015-04-20 00:28:30 +08004323
Mao Flynn1893a7f2015-06-03 12:03:36 +08004324 realsize = header->blocks * 512;
4325 readsize = ROUNDUP((realsize + LOGO_IMG_HEADER_SIZE), blocksize) - blocksize;
4326
4327 /* if the logo is not full-screen size, clean screen */
Mao Flynn7b379f32015-04-20 00:28:30 +08004328 if ((header->width != fb_display->width)
4329 || (header->height != fb_display->height))
4330 fbcon_clear();
4331
Sachin Bhayare619e9e42017-05-15 13:10:31 +05304332 uint32_t fb_size = ROUNDUP(fb_display->width *
4333 fb_display->height *
4334 (fb_display->bpp / 8), 4096);
4335
4336 if (readsize > fb_size) {
4337 dprintf(CRITICAL, "ERROR: Splash image size invalid\n");
4338 return -1;
4339 }
4340
Mao Flynn1893a7f2015-06-03 12:03:36 +08004341 if (mmc_read(ptn + blocksize, (uint32_t *)(base + blocksize), readsize)) {
Mao Flynn7b379f32015-04-20 00:28:30 +08004342 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
4343 return -1;
4344 }
Mao Flynn7b379f32015-04-20 00:28:30 +08004345
Mao Flynn1893a7f2015-06-03 12:03:36 +08004346 fbcon_extract_to_screen(header, (base + LOGO_IMG_HEADER_SIZE));
4347 } else { /* 2 Raw BGR data */
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304348
Veera Sundaram Sankaranc33416f2015-09-28 10:25:55 -07004349 if ((header->width > fb_display->width) || (header->height > fb_display->height)) {
4350 dprintf(CRITICAL, "Logo config greater than fb config. Fall back default logo\n");
Mao Flynn1893a7f2015-06-03 12:03:36 +08004351 return -1;
4352 }
4353
4354 realsize = header->width * header->height * fb_display->bpp / 8;
4355 readsize = ROUNDUP((realsize + LOGO_IMG_HEADER_SIZE), blocksize) - blocksize;
4356
4357 if (blocksize == LOGO_IMG_HEADER_SIZE) { /* read the content directly */
4358 if (mmc_read((ptn + LOGO_IMG_HEADER_SIZE), (uint32_t *)base, readsize)) {
4359 fbcon_clear();
4360 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
4361 return -1;
4362 }
4363 } else {
4364 if (mmc_read(ptn + blocksize ,
4365 (uint32_t *)(base + LOGO_IMG_OFFSET + blocksize), readsize)) {
4366 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
4367 return -1;
4368 }
4369 memmove(base, (base + LOGO_IMG_OFFSET + LOGO_IMG_HEADER_SIZE), realsize);
4370 }
4371 }
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304372 }
4373
Mao Flynn7b379f32015-04-20 00:28:30 +08004374 return 0;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304375}
4376
Mao Flynn7b379f32015-04-20 00:28:30 +08004377int fetch_image_from_partition()
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304378{
4379 if (target_is_emmc_boot()) {
4380 return splash_screen_mmc();
4381 } else {
4382 return splash_screen_flash();
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07004383 }
4384}
4385
Mayank Grover351a75e2017-05-30 20:06:08 +05304386void publish_getvar_multislot_vars()
4387{
4388 int i,count;
4389 static bool published = false;
4390 static char slot_count[MAX_RSP_SIZE];
4391 static struct ab_slot_info slot_info[AB_SUPPORTED_SLOTS];
4392 static char active_slot_suffix[MAX_RSP_SIZE];
4393 static char has_slot_pname[NUM_PARTITIONS][MAX_GET_VAR_NAME_SIZE];
4394 static char has_slot_reply[NUM_PARTITIONS][MAX_RSP_SIZE];
4395 const char *tmp;
4396 char tmpbuff[MAX_GET_VAR_NAME_SIZE];
Mayank Grover2bd84bb2017-06-20 11:06:07 +05304397 signed active_slt;
Mayank Grover351a75e2017-05-30 20:06:08 +05304398
4399 if (!published)
4400 {
4401 /* Update slot meta info */
4402 count = partition_fill_partition_meta(has_slot_pname, has_slot_reply,
4403 partition_get_partition_count());
4404 for(i=0; i<count; i++)
4405 {
4406 memset(tmpbuff, 0, MAX_GET_VAR_NAME_SIZE);
Mayank Grover2bd84bb2017-06-20 11:06:07 +05304407 snprintf(tmpbuff, MAX_GET_VAR_NAME_SIZE,"has-slot:%s",
4408 has_slot_pname[i]);
4409 strlcpy(has_slot_pname[i], tmpbuff, MAX_GET_VAR_NAME_SIZE);
Mayank Grover351a75e2017-05-30 20:06:08 +05304410 fastboot_publish(has_slot_pname[i], has_slot_reply[i]);
4411 }
4412
4413 for (i=0; i<AB_SUPPORTED_SLOTS; i++)
4414 {
4415 tmp = SUFFIX_SLOT(i);
Mayank Grover7b8a8f82017-10-27 13:07:59 +05304416 tmp++; // to remove "_" from slot_suffix.
Mayank Grover2bd84bb2017-06-20 11:06:07 +05304417 snprintf(slot_info[i].slot_is_unbootable, sizeof(slot_info[i].slot_is_unbootable),
4418 "slot-unbootable:%s", tmp);
4419 snprintf(slot_info[i].slot_is_active, sizeof(slot_info[i].slot_is_active),
4420 "slot-active:%s", tmp);
4421 snprintf(slot_info[i].slot_is_succesful, sizeof(slot_info[i].slot_is_succesful),
4422 "slot-success:%s", tmp);
4423 snprintf(slot_info[i].slot_retry_count, sizeof(slot_info[i].slot_retry_count),
4424 "slot-retry-count:%s", tmp);
Mayank Grover351a75e2017-05-30 20:06:08 +05304425 fastboot_publish(slot_info[i].slot_is_unbootable,
4426 slot_info[i].slot_is_unbootable_rsp);
4427 fastboot_publish(slot_info[i].slot_is_active,
4428 slot_info[i].slot_is_active_rsp);
4429 fastboot_publish(slot_info[i].slot_is_succesful,
4430 slot_info[i].slot_is_succesful_rsp);
4431 fastboot_publish(slot_info[i].slot_retry_count,
4432 slot_info[i].slot_retry_count_rsp);
4433 }
4434 fastboot_publish("current-slot", active_slot_suffix);
4435 snprintf(slot_count, sizeof(slot_count),"%d", AB_SUPPORTED_SLOTS);
4436 fastboot_publish("slot-count", slot_count);
4437 published = true;
4438 }
4439
Mayank Grover2bd84bb2017-06-20 11:06:07 +05304440 active_slt = partition_find_active_slot();
4441 if (active_slt != INVALID)
Mayank Grover7b8a8f82017-10-27 13:07:59 +05304442 {
4443 tmp = SUFFIX_SLOT(active_slt);
4444 tmp++; // to remove "_" from slot_suffix.
4445 snprintf(active_slot_suffix, sizeof(active_slot_suffix), "%s", tmp);
4446 }
Mayank Grover2bd84bb2017-06-20 11:06:07 +05304447 else
4448 strlcpy(active_slot_suffix, "INVALID", sizeof(active_slot_suffix));
4449
Mayank Grover351a75e2017-05-30 20:06:08 +05304450 /* Update partition meta information */
4451 partition_fill_slot_meta(slot_info);
4452 return;
4453}
4454
lijuang4ece1e72015-08-14 21:02:36 +08004455void get_product_name(unsigned char *buf)
4456{
4457 snprintf((char*)buf, MAX_RSP_SIZE, "%s", TARGET(BOARD));
4458 return;
4459}
4460
Mayank Grover7b8a8f82017-10-27 13:07:59 +05304461#if PRODUCT_IOT
4462void get_bootloader_version_iot(unsigned char *buf)
4463{
4464 if (buf != NULL)
4465 {
4466 strlcpy(buf, TARGET(BOARD), MAX_VERSION_LEN);
4467 strlcat(buf, "-", MAX_VERSION_LEN);
4468 strlcat(buf, PRODUCT_IOT_VERSION, MAX_VERSION_LEN);
4469 }
4470 return;
4471}
4472#endif
4473
lijuang4ece1e72015-08-14 21:02:36 +08004474void get_bootloader_version(unsigned char *buf)
4475{
4476 snprintf((char*)buf, MAX_RSP_SIZE, "%s", device.bootloader_version);
4477 return;
4478}
4479
4480void get_baseband_version(unsigned char *buf)
4481{
4482 snprintf((char*)buf, MAX_RSP_SIZE, "%s", device.radio_version);
4483 return;
4484}
4485
4486bool is_device_locked()
4487{
4488 return device.is_unlocked ? false:true;
4489}
4490
Amol Jadi5edf3552013-07-23 14:15:34 -07004491/* register commands and variables for fastboot */
4492void aboot_fastboot_register_commands(void)
4493{
Sridhar Parasurame94e8152014-10-24 14:06:03 -07004494 int i;
lijuangf16461c2015-08-03 17:09:34 +08004495 char hw_platform_buf[MAX_RSP_SIZE];
Amol Jadi5edf3552013-07-23 14:15:34 -07004496
Sridhar Parasurame94e8152014-10-24 14:06:03 -07004497 struct fastboot_cmd_desc cmd_list[] = {
lijuang511a2b52015-08-14 20:50:51 +08004498 /* By default the enabled list is empty. */
4499 {"", NULL},
4500 /* move commands enclosed within the below ifndef to here
4501 * if they need to be enabled in user build.
4502 */
Sridhar Parasurame94e8152014-10-24 14:06:03 -07004503#ifndef DISABLE_FASTBOOT_CMDS
lijuang511a2b52015-08-14 20:50:51 +08004504 /* Register the following commands only for non-user builds */
4505 {"flash:", cmd_flash},
4506 {"erase:", cmd_erase},
4507 {"boot", cmd_boot},
4508 {"continue", cmd_continue},
4509 {"reboot", cmd_reboot},
4510 {"reboot-bootloader", cmd_reboot_bootloader},
4511 {"oem unlock", cmd_oem_unlock},
4512 {"oem unlock-go", cmd_oem_unlock_go},
4513 {"oem lock", cmd_oem_lock},
4514 {"flashing unlock", cmd_oem_unlock},
4515 {"flashing lock", cmd_oem_lock},
4516 {"flashing lock_critical", cmd_flashing_lock_critical},
4517 {"flashing unlock_critical", cmd_flashing_unlock_critical},
4518 {"flashing get_unlock_ability", cmd_flashing_get_unlock_ability},
4519 {"oem device-info", cmd_oem_devinfo},
4520 {"preflash", cmd_preflash},
4521 {"oem enable-charger-screen", cmd_oem_enable_charger_screen},
4522 {"oem disable-charger-screen", cmd_oem_disable_charger_screen},
lijuanga25d8bb2015-09-16 13:11:52 +08004523 {"oem off-mode-charge", cmd_oem_off_mode_charger},
lijuang511a2b52015-08-14 20:50:51 +08004524 {"oem select-display-panel", cmd_oem_select_display_panel},
Mayank Grover6ccc1c92017-07-04 17:36:46 +05304525 {"set_active",cmd_set_active},
Channagoud Kadabi736c4962015-08-21 11:56:52 -07004526#if UNITTEST_FW_SUPPORT
Channagoud Kadabid5ddf482015-09-28 10:31:35 -07004527 {"oem run-tests", cmd_oem_runtests},
Channagoud Kadabi736c4962015-08-21 11:56:52 -07004528#endif
Sridhar Parasurame94e8152014-10-24 14:06:03 -07004529#endif
lijuang511a2b52015-08-14 20:50:51 +08004530 };
Sridhar Parasurame94e8152014-10-24 14:06:03 -07004531
4532 int fastboot_cmds_count = sizeof(cmd_list)/sizeof(cmd_list[0]);
4533 for (i = 1; i < fastboot_cmds_count; i++)
4534 fastboot_register(cmd_list[i].name,cmd_list[i].cb);
4535
Amol Jadi5edf3552013-07-23 14:15:34 -07004536 /* publish variables and their values */
4537 fastboot_publish("product", TARGET(BOARD));
4538 fastboot_publish("kernel", "lk");
4539 fastboot_publish("serialno", sn_buf);
4540
4541 /*
4542 * partition info is supported only for emmc partitions
4543 * Calling this for NAND prints some error messages which
4544 * is harmless but misleading. Avoid calling this for NAND
4545 * devices.
4546 */
4547 if (target_is_emmc_boot())
Mayank Grover7b8a8f82017-10-27 13:07:59 +05304548 publish_getvar_partition_info(part_info, partition_get_partition_count());
Amol Jadi5edf3552013-07-23 14:15:34 -07004549
Mayank Grover351a75e2017-05-30 20:06:08 +05304550 if (partition_multislot_is_supported())
4551 publish_getvar_multislot_vars();
4552
Amol Jadi5edf3552013-07-23 14:15:34 -07004553 /* Max download size supported */
Ameya Thakur11cf1a62013-08-05 12:44:48 -07004554 snprintf(max_download_size, MAX_RSP_SIZE, "\t0x%x",
4555 target_get_max_flash_size());
Amol Jadi5edf3552013-07-23 14:15:34 -07004556 fastboot_publish("max-download-size", (const char *) max_download_size);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07004557 /* Is the charger screen check enabled */
4558 snprintf(charger_screen_enabled, MAX_RSP_SIZE, "%d",
4559 device.charger_screen_enabled);
4560 fastboot_publish("charger-screen-enabled",
4561 (const char *) charger_screen_enabled);
lijuanga25d8bb2015-09-16 13:11:52 +08004562 fastboot_publish("off-mode-charge", (const char *) charger_screen_enabled);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05304563 snprintf(panel_display_mode, MAX_RSP_SIZE, "%s",
4564 device.display_panel);
4565 fastboot_publish("display-panel",
4566 (const char *) panel_display_mode);
Mayank Grover7b8a8f82017-10-27 13:07:59 +05304567#if PRODUCT_IOT
4568 get_bootloader_version_iot(&bootloader_version_string);
4569 fastboot_publish("version-bootloader", (const char *) bootloader_version_string);
4570
4571 /* Version baseband is n/a for apq iot devices */
4572 fastboot_publish("version-baseband", "N/A");
4573
4574 /* IOT targets support only mmc target */
4575 snprintf(block_size_string, MAX_RSP_SIZE, "0x%x", mmc_get_device_blocksize());
4576 fastboot_publish("erase-block-size", (const char *) block_size_string);
4577 fastboot_publish("logical-block-size", (const char *) block_size_string);
4578#else
Channagoud Kadabiaafbbfc2015-03-04 17:52:56 -08004579 fastboot_publish("version-bootloader", (const char *) device.bootloader_version);
4580 fastboot_publish("version-baseband", (const char *) device.radio_version);
Mayank Grover7b8a8f82017-10-27 13:07:59 +05304581#endif
lijuangf16461c2015-08-03 17:09:34 +08004582 fastboot_publish("secure", is_secure_boot_enable()? "yes":"no");
Mayank Grover7b8a8f82017-10-27 13:07:59 +05304583 fastboot_publish("unlocked", device.is_unlocked ? "yes":"no");
lijuangf16461c2015-08-03 17:09:34 +08004584 smem_get_hw_platform_name((unsigned char *) hw_platform_buf, sizeof(hw_platform_buf));
4585 snprintf(get_variant, MAX_RSP_SIZE, "%s %s", hw_platform_buf,
4586 target_is_emmc_boot()? "eMMC":"UFS");
4587 fastboot_publish("variant", (const char *) get_variant);
lijuang65c5a822015-08-29 16:35:36 +08004588#if CHECK_BAT_VOLTAGE
lijuang102dfa92015-10-09 18:31:03 +08004589 update_battery_status();
lijuang65c5a822015-08-29 16:35:36 +08004590 fastboot_publish("battery-voltage", (const char *) battery_voltage);
lijuang102dfa92015-10-09 18:31:03 +08004591 fastboot_publish("battery-soc-ok", (const char *) battery_soc_ok);
lijuang65c5a822015-08-29 16:35:36 +08004592#endif
Amol Jadi5edf3552013-07-23 14:15:34 -07004593}
4594
Brian Swetland9c4c0752009-01-25 16:23:50 -08004595void aboot_init(const struct app_descriptor *app)
4596{
Shashank Mittal4f99a882010-02-01 13:58:50 -08004597 unsigned reboot_mode = 0;
Mayank Grover351a75e2017-05-30 20:06:08 +05304598 int boot_err_type = 0;
4599 int boot_slot = INVALID;
Chandan Uddarajubedca152010-06-02 23:05:15 -07004600
lijuang39831732016-01-08 17:49:02 +08004601 /* Initialise wdog to catch early lk crashes */
4602#if WDOG_SUPPORT
4603 msm_wdog_init();
4604#endif
4605
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07004606 /* Setup page size information for nv storage */
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004607 if (target_is_emmc_boot())
4608 {
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07004609 page_size = mmc_page_size();
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004610 page_mask = page_size - 1;
Gaurav Nebhwani4d2389c2016-03-17 21:10:05 +05304611 mmc_blocksize = mmc_get_device_blocksize();
4612 mmc_blocksize_mask = mmc_blocksize - 1;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004613 }
4614 else
4615 {
4616 page_size = flash_page_size();
4617 page_mask = page_size - 1;
4618 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07004619 ASSERT((MEMBASE + MEMSIZE) > MEMBASE);
4620
Ameya Thakur11cf1a62013-08-05 12:44:48 -07004621 read_device_info(&device);
vijay kumarc65876c2015-04-24 13:29:16 +05304622 read_allow_oem_unlock(&device);
Shashank Mittal162244e2011-08-08 19:01:25 -07004623
Mayank Grover351a75e2017-05-30 20:06:08 +05304624 /* Detect multi-slot support */
4625 if (partition_multislot_is_supported())
4626 {
4627 boot_slot = partition_find_active_slot();
4628 if (boot_slot == INVALID)
4629 {
4630 boot_into_fastboot = true;
4631 dprintf(INFO, "Active Slot: (INVALID)\n");
4632 }
4633 else
4634 {
4635 /* Setting the state of system to boot active slot */
4636 partition_mark_active_slot(boot_slot);
4637 dprintf(INFO, "Active Slot: (%s)\n", SUFFIX_SLOT(boot_slot));
4638 }
4639 }
4640
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08004641 /* Display splash screen if enabled */
4642#if DISPLAY_SPLASH_SCREEN
lijuang99c02d82015-02-13 19:04:34 +08004643#if NO_ALARM_DISPLAY
4644 if (!check_alarm_boot()) {
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08004645#endif
lijuang99c02d82015-02-13 19:04:34 +08004646 dprintf(SPEW, "Display Init: Start\n");
Ajay Singh Parmara7865a82015-04-16 21:27:52 -07004647#if DISPLAY_HDMI_PRIMARY
4648 if (!strlen(device.display_panel))
4649 strlcpy(device.display_panel, DISPLAY_PANEL_HDMI,
4650 sizeof(device.display_panel));
4651#endif
Channagoud Kadabi90869ce2015-04-27 11:15:14 -07004652#if ENABLE_WBC
Channagoud Kadabid801ac72015-08-26 11:21:51 -07004653 /* Wait if the display shutdown is in progress */
4654 while(pm_app_display_shutdown_in_prgs());
Channagoud Kadabi90869ce2015-04-27 11:15:14 -07004655 if (!pm_appsbl_display_init_done())
4656 target_display_init(device.display_panel);
4657 else
4658 display_image_on_screen();
4659#else
lijuang99c02d82015-02-13 19:04:34 +08004660 target_display_init(device.display_panel);
Channagoud Kadabi90869ce2015-04-27 11:15:14 -07004661#endif
lijuang99c02d82015-02-13 19:04:34 +08004662 dprintf(SPEW, "Display Init: Done\n");
4663#if NO_ALARM_DISPLAY
4664 }
4665#endif
4666#endif
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08004667
Greg Griscod6250552011-06-29 14:40:23 -07004668 target_serialno((unsigned char *) sn_buf);
Ajay Dudanib06c05f2011-05-12 14:46:10 -07004669 dprintf(SPEW,"serial number: %s\n",sn_buf);
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -08004670
Dhaval Patel223ec952013-07-18 14:49:44 -07004671 memset(display_panel_buf, '\0', MAX_PANEL_BUF_SIZE);
4672
Matthew Qindefd5562014-07-11 18:02:40 +08004673 /*
4674 * Check power off reason if user force reset,
4675 * if yes phone will do normal boot.
4676 */
4677 if (is_user_force_reset())
4678 goto normal_boot;
4679
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07004680 /* Check if we should do something other than booting up */
Ameya Thakur0b9c2442013-05-10 13:22:56 -07004681 if (keys_get_state(KEY_VOLUMEUP) && keys_get_state(KEY_VOLUMEDOWN))
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07004682 {
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03004683 dprintf(ALWAYS,"dload mode key sequence detected\n");
lijuang395b5e62015-11-19 17:39:44 +08004684 reboot_device(EMERGENCY_DLOAD);
4685 dprintf(CRITICAL,"Failed to reboot into dload mode\n");
4686
Ameya Thakur0b9c2442013-05-10 13:22:56 -07004687 boot_into_fastboot = true;
4688 }
4689 if (!boot_into_fastboot)
4690 {
4691 if (keys_get_state(KEY_HOME) || keys_get_state(KEY_VOLUMEUP))
4692 boot_into_recovery = 1;
4693 if (!boot_into_recovery &&
4694 (keys_get_state(KEY_BACK) || keys_get_state(KEY_VOLUMEDOWN)))
Pavel Nedev5d91d412013-04-29 11:34:24 +03004695 boot_into_fastboot = true;
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07004696 }
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004697 #if NO_KEYPAD_DRIVER
Kinson Chik0b1c8162011-08-31 16:31:57 -07004698 if (fastboot_trigger())
Pavel Nedev5d91d412013-04-29 11:34:24 +03004699 boot_into_fastboot = true;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004700 #endif
Chandan Uddarajubedca152010-06-02 23:05:15 -07004701
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07004702#if USE_PON_REBOOT_REG
4703 reboot_mode = check_hard_reboot_mode();
4704#else
Ajay Dudani77421292010-10-27 19:34:06 -07004705 reboot_mode = check_reboot_mode();
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07004706#endif
4707 if (reboot_mode == RECOVERY_MODE)
4708 {
Ajay Dudani77421292010-10-27 19:34:06 -07004709 boot_into_recovery = 1;
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07004710 }
4711 else if(reboot_mode == FASTBOOT_MODE)
4712 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03004713 boot_into_fastboot = true;
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07004714 }
4715 else if(reboot_mode == ALARM_BOOT)
4716 {
Matthew Qind886f3c2014-01-17 16:52:01 +08004717 boot_reason_alarm = true;
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07004718 }
Parth Dixit207573a2015-10-27 17:26:21 +05304719#if VERIFIED_BOOT
Mayank Grover912eaa62017-10-26 12:08:53 +05304720 else if (VB_M <= target_get_vb_version())
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07004721 {
Mayank Grover889be1b2017-09-12 20:12:23 +05304722 if (reboot_mode == DM_VERITY_ENFORCING)
4723 {
4724 device.verity_mode = 1;
4725 write_device_info(&device);
4726 }
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07004727#if ENABLE_VB_ATTEST
Mayank Grover889be1b2017-09-12 20:12:23 +05304728 else if (reboot_mode == DM_VERITY_EIO)
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07004729#else
Mayank Grover889be1b2017-09-12 20:12:23 +05304730 else if (reboot_mode == DM_VERITY_LOGGING)
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07004731#endif
Mayank Grover889be1b2017-09-12 20:12:23 +05304732 {
4733 device.verity_mode = 0;
4734 write_device_info(&device);
4735 }
4736 else if (reboot_mode == DM_VERITY_KEYSCLEAR)
4737 {
4738 if(send_delete_keys_to_tz())
4739 ASSERT(0);
4740 }
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07004741 }
Parth Dixitddbc7352015-10-18 03:13:31 +05304742#endif
Ajay Dudani77421292010-10-27 19:34:06 -07004743
Matthew Qindefd5562014-07-11 18:02:40 +08004744normal_boot:
Pavel Nedev5d91d412013-04-29 11:34:24 +03004745 if (!boot_into_fastboot)
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004746 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03004747 if (target_is_emmc_boot())
Shashank Mittala0032282011-08-26 14:50:11 -07004748 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03004749 if(emmc_recovery_init())
4750 dprintf(ALWAYS,"error in emmc_recovery_init\n");
4751 if(target_use_signed_kernel())
Shashank Mittala0032282011-08-26 14:50:11 -07004752 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03004753 if((device.is_unlocked) || (device.is_tampered))
4754 {
4755 #ifdef TZ_TAMPER_FUSE
4756 set_tamper_fuse_cmd();
4757 #endif
4758 #if USE_PCOM_SECBOOT
4759 set_tamper_flag(device.is_tampered);
4760 #endif
4761 }
Shashank Mittala0032282011-08-26 14:50:11 -07004762 }
Amit Blay6281ebc2015-01-11 14:44:08 +02004763
Mayank Grover351a75e2017-05-30 20:06:08 +05304764retry_boot:
4765 /* Trying to boot active partition */
4766 if (partition_multislot_is_supported())
4767 {
4768 boot_slot = partition_find_boot_slot();
4769 partition_mark_active_slot(boot_slot);
4770 if (boot_slot == INVALID)
4771 goto fastboot;
4772 }
4773
4774 boot_err_type = boot_linux_from_mmc();
4775 switch (boot_err_type)
4776 {
4777 case ERR_INVALID_PAGE_SIZE:
4778 case ERR_DT_PARSE:
4779 case ERR_ABOOT_ADDR_OVERLAP:
Mayank Grover10cabbe2017-10-30 19:11:05 +05304780 case ERR_INVALID_BOOT_MAGIC:
Mayank Grover351a75e2017-05-30 20:06:08 +05304781 if(partition_multislot_is_supported())
Mayank Grover10cabbe2017-10-30 19:11:05 +05304782 {
4783 /*
4784 * Deactivate current slot, as it failed to
4785 * boot, and retry next slot.
4786 */
4787 partition_deactivate_slot(boot_slot);
Mayank Grover351a75e2017-05-30 20:06:08 +05304788 goto retry_boot;
Mayank Grover10cabbe2017-10-30 19:11:05 +05304789 }
Mayank Grover351a75e2017-05-30 20:06:08 +05304790 else
4791 break;
Mayank Grover351a75e2017-05-30 20:06:08 +05304792 default:
4793 break;
4794 /* going to fastboot menu */
4795 }
Shashank Mittala0032282011-08-26 14:50:11 -07004796 }
Pavel Nedev5d91d412013-04-29 11:34:24 +03004797 else
4798 {
4799 recovery_init();
4800 #if USE_PCOM_SECBOOT
4801 if((device.is_unlocked) || (device.is_tampered))
4802 set_tamper_flag(device.is_tampered);
4803 #endif
4804 boot_linux_from_flash();
4805 }
4806 dprintf(CRITICAL, "ERROR: Could not do normal boot. Reverting "
4807 "to fastboot mode.\n");
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004808 }
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07004809
Mayank Grover351a75e2017-05-30 20:06:08 +05304810fastboot:
Amol Jadi5edf3552013-07-23 14:15:34 -07004811 /* We are here means regular boot did not happen. Start fastboot. */
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07004812
Amol Jadi5edf3552013-07-23 14:15:34 -07004813 /* register aboot specific fastboot commands */
4814 aboot_fastboot_register_commands();
Amol Jadi57abe4c2011-05-24 15:47:27 -07004815
Amol Jadi5edf3552013-07-23 14:15:34 -07004816 /* dump partition table for debug info */
Kinson Chikf1a43512011-07-14 11:28:39 -07004817 partition_dump();
Amol Jadi5edf3552013-07-23 14:15:34 -07004818
4819 /* initialize and start fastboot */
4820 fastboot_init(target_get_scratch_address(), target_get_max_flash_size());
lijuang4ece1e72015-08-14 21:02:36 +08004821#if FBCON_DISPLAY_MSG
lijuang9a7d3b92015-11-30 14:41:24 +08004822 display_fastboot_menu();
lijuang4ece1e72015-08-14 21:02:36 +08004823#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08004824}
4825
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07004826uint32_t get_page_size()
4827{
4828 return page_size;
4829}
4830
Amir Samuelov57a6fa22013-06-05 16:36:43 +03004831/*
4832 * Calculated and save hash (SHA256) for non-signed boot image.
4833 *
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07004834 * @param image_addr - Boot image address
4835 * @param image_size - Size of the boot image
Amir Samuelov57a6fa22013-06-05 16:36:43 +03004836 *
4837 * @return int - 0 on success, negative value on failure.
4838 */
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07004839static int aboot_save_boot_hash_mmc(uint32_t image_addr, uint32_t image_size)
Amir Samuelov57a6fa22013-06-05 16:36:43 +03004840{
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07004841 unsigned int digest[8];
4842#if IMAGE_VERIF_ALGO_SHA1
4843 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA1;
4844#else
4845 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA256;
4846#endif
Amir Samuelov57a6fa22013-06-05 16:36:43 +03004847
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07004848 target_crypto_init_params();
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -08004849 hash_find((unsigned char *) image_addr, image_size, (unsigned char *)&digest, auth_algo);
Amir Samuelov57a6fa22013-06-05 16:36:43 +03004850
4851 save_kernel_hash_cmd(digest);
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07004852 dprintf(INFO, "aboot_save_boot_hash_mmc: imagesize_actual size %d bytes.\n", (int) image_size);
Amir Samuelov57a6fa22013-06-05 16:36:43 +03004853
4854 return 0;
4855}
4856
Mayank Grover351a75e2017-05-30 20:06:08 +05304857
Brian Swetland9c4c0752009-01-25 16:23:50 -08004858APP_START(aboot)
4859 .init = aboot_init,
4860APP_END