blob: c1836121b648c8f145c86a3fccc21b908c1c53c8 [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>
Greg Griscod6250552011-06-29 14:40:23 -070052#include <platform.h>
Shashank Mittalcd98d472011-08-02 14:29:24 -070053#include <crypto_hash.h>
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070054#include <malloc.h>
Amol Jadi492d5a52013-03-15 16:12:34 -070055#include <boot_stats.h>
Amir Samuelov57a6fa22013-06-05 16:36:43 +030056#include <sha.h>
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -070057#include <platform/iomap.h>
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -070058#include <boot_device.h>
Shashank Mittald3e54dd2014-08-28 15:24:02 -070059#include <boot_verifier.h>
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +053060#include <image_verify.h>
Matthew Qinbb7923d2015-02-09 10:56:09 +080061#include <decompress.h>
Unnati Gandhi17b3bfc2015-05-11 12:58:16 +053062#include <platform/timer.h>
lijuang511a2b52015-08-14 20:50:51 +080063#include <sys/types.h>
Channagoud Kadabi036c6052015-02-09 15:19:59 -080064#if USE_RPMB_FOR_DEVINFO
65#include <rpmb.h>
66#endif
Dima Zavin214cc642009-01-26 11:16:21 -080067
Channagoud Kadabi90869ce2015-04-27 11:15:14 -070068#if ENABLE_WBC
69#include <pm_app_smbchg.h>
70#endif
71
Neeti Desai17379b82012-06-04 18:42:53 -070072#if DEVICE_TREE
73#include <libfdt.h>
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070074#include <dev_tree.h>
Neeti Desai17379b82012-06-04 18:42:53 -070075#endif
76
Aparna Mallavarapu118ccae2015-06-03 13:47:11 +053077#if WDOG_SUPPORT
78#include <wdog.h>
79#endif
80
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -070081#include <reboot.h>
Shashank Mittalcd98d472011-08-02 14:29:24 -070082#include "image_verify.h"
Shashank Mittal024c0332010-02-03 11:44:00 -080083#include "recovery.h"
Brian Swetland9c4c0752009-01-25 16:23:50 -080084#include "bootimg.h"
85#include "fastboot.h"
Ajay Dudani5c761132011-04-07 20:19:04 -070086#include "sparse_format.h"
Ajay Dudanide984792015-03-02 09:57:41 -080087#include "meta_format.h"
Greg Grisco6e754772011-06-23 12:19:39 -070088#include "mmc.h"
Shashank Mittal162244e2011-08-08 19:01:25 -070089#include "devinfo.h"
Neeti Desai465491e2012-07-31 12:53:35 -070090#include "board.h"
Shashank Mittal162244e2011-08-08 19:01:25 -070091#include "scm.h"
Amit Blay6281ebc2015-01-11 14:44:08 +020092#include "mdtp.h"
Sridhar Parasuram32b30662015-07-10 13:33:22 -070093#include "secapp_loader.h"
lijuanga40d6302015-07-20 20:10:13 +080094#include <menu_keys_detect.h>
95#include <display_menu.h>
Channagoud Kadabi736c4962015-08-21 11:56:52 -070096#include "fastboot_test.h"
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -070097
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070098extern bool target_use_signed_kernel(void);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070099extern void platform_uninit(void);
Channagoud Kadabi33defe22013-06-18 18:35:40 -0700100extern void target_uninit(void);
Joonwoo Park61112782013-10-02 19:50:39 -0700101extern int get_target_boot_params(const char *cmdline, const char *part,
vijay kumar870515d2015-08-31 16:37:24 +0530102 char **buf);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700103
Sridhar Parasuram7e16d172015-07-05 11:35:23 -0700104void *info_buf;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700105void write_device_info_mmc(device_info *dev);
106void write_device_info_flash(device_info *dev);
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -0700107static int aboot_save_boot_hash_mmc(uint32_t image_addr, uint32_t image_size);
Channagoud Kadabiad259832015-05-29 11:14:17 -0700108static int aboot_frp_unlock(char *pname, void *data, unsigned sz);
Kishor PK38ed93d2017-04-25 14:19:26 +0530109static inline uint64_t validate_partition_size();
Parth Dixit54ac3bb2017-03-07 15:52:48 +0530110bool pwr_key_is_pressed = false;
Hareesh Gauthamabf1e1e2017-04-12 18:09:24 +0530111static bool is_systemd_present=false;
Mayank Grover351a75e2017-05-30 20:06:08 +0530112static void publish_getvar_multislot_vars();
Sridhar Parasurame94e8152014-10-24 14:06:03 -0700113/* fastboot command function pointer */
114typedef void (*fastboot_cmd_fn) (const char *, void *, unsigned);
115
116struct fastboot_cmd_desc {
117 char * name;
118 fastboot_cmd_fn cb;
119};
120
Subbaraman Narayanamurthyeb92bcc2010-07-20 14:32:46 -0700121#define EXPAND(NAME) #NAME
122#define TARGET(NAME) EXPAND(NAME)
Brian Swetland2defe162009-08-18 14:35:59 -0700123
Ajay Singh Parmara7865a82015-04-16 21:27:52 -0700124#define DISPLAY_PANEL_HDMI "hdmi"
125
Ajay Dudanicd01f9b2010-02-23 21:13:04 -0800126#ifdef MEMBASE
127#define EMMC_BOOT_IMG_HEADER_ADDR (0xFF000+(MEMBASE))
128#else
David Ng183a7422009-12-07 14:55:21 -0800129#define EMMC_BOOT_IMG_HEADER_ADDR 0xFF000
Ajay Dudanicd01f9b2010-02-23 21:13:04 -0800130#endif
131
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700132#ifndef MEMSIZE
133#define MEMSIZE 1024*1024
134#endif
135
136#define MAX_TAGS_SIZE 1024
137
Kun Liang2f1601a2013-08-12 16:29:54 +0800138/* make 4096 as default size to ensure EFS,EXT4's erasing */
139#define DEFAULT_ERASE_SIZE 4096
Ujwal Patelc0b0a252015-08-16 14:05:35 -0700140#define MAX_PANEL_BUF_SIZE 196
Vijay Kumar Pendoti0b21f462016-05-02 17:09:18 +0530141#define FOOTER_SIZE 16384
Kun Liang2f1601a2013-08-12 16:29:54 +0800142
Dhaval Patelf83d73b2014-06-23 16:24:37 -0700143#define DISPLAY_DEFAULT_PREFIX "mdss_mdp"
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700144#define BOOT_DEV_MAX_LEN 64
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -0800145
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800146#define IS_ARM64(ptr) (ptr->magic_64 == KERNEL64_HDR_MAGIC) ? true : false
147
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -0700148#define ADD_OF(a, b) (UINT_MAX - b > a) ? (a + b) : UINT_MAX
149
Ameya Thakur10a33452016-06-13 14:24:26 -0700150//Size of the header that is used in case the boot image has
151//a uncompressed kernel + appended dtb
152#define PATCHED_KERNEL_HEADER_SIZE 20
153
154//String used to determine if the boot image has
155//a uncompressed kernel + appended dtb
156#define PATCHED_KERNEL_MAGIC "UNCOMPRESSED_IMG"
157
Sridhar Parasuram7bd4aaf2015-02-12 11:14:38 -0800158#if USE_BOOTDEV_CMDLINE
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700159static const char *emmc_cmdline = " androidboot.bootdevice=";
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700160#else
David Ng183a7422009-12-07 14:55:21 -0800161static const char *emmc_cmdline = " androidboot.emmc=true";
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700162#endif
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800163static const char *usb_sn_cmdline = " androidboot.serialno=";
Pavel Nedev328ac822013-04-05 15:25:11 +0300164static const char *androidboot_mode = " androidboot.mode=";
Hareesh Gauthamabf1e1e2017-04-12 18:09:24 +0530165
166static const char *systemd_ffbm_mode = " systemd.unit=ffbm.target";
Matthew Qind886f3c2014-01-17 16:52:01 +0800167static const char *alarmboot_cmdline = " androidboot.alarmboot=true";
Pavel Nedev898298c2013-02-27 12:36:09 -0800168static const char *loglevel = " quiet";
Ajay Dudanica3a33c2011-11-18 08:31:40 -0800169static const char *battchg_pause = " androidboot.mode=charger";
Shashank Mittalcd98d472011-08-02 14:29:24 -0700170static const char *auth_kernel = " androidboot.authorized_kernel=true";
Pavel Nedev5614d222013-06-17 18:01:02 +0300171static const char *secondary_gpt_enable = " gpt";
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200172static const char *mdtp_activated_flag = " mdtp";
David Ng183a7422009-12-07 14:55:21 -0800173
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800174static const char *baseband_apq = " androidboot.baseband=apq";
175static const char *baseband_msm = " androidboot.baseband=msm";
176static const char *baseband_csfb = " androidboot.baseband=csfb";
177static const char *baseband_svlte2a = " androidboot.baseband=svlte2a";
Ajay Dudani403bc492011-09-30 16:17:21 -0700178static const char *baseband_mdm = " androidboot.baseband=mdm";
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800179static const char *baseband_mdm2 = " androidboot.baseband=mdm2";
Amol Jadi5c61a952012-05-04 17:05:35 -0700180static const char *baseband_sglte = " androidboot.baseband=sglte";
Amol Jadi2a15a272013-01-22 12:03:36 -0800181static const char *baseband_dsda = " androidboot.baseband=dsda";
182static const char *baseband_dsda2 = " androidboot.baseband=dsda2";
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800183static const char *baseband_sglte2 = " androidboot.baseband=sglte2";
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800184static const char *warmboot_cmdline = " qpnp-power-on.warm_boot=1";
Vijay Kumar Pendotib228cfc2016-06-13 20:15:23 +0530185static const char *baseband_apq_nowgr = " androidboot.baseband=baseband_apq_nowgr";
Mayank Grover351a75e2017-05-30 20:06:08 +0530186static const char *androidboot_slot_suffix = " androidboot.slot_suffix=";
187static const char *skip_ramfs = " skip_initramfs";
Sourabh Banerjee6c7153c2018-03-20 01:21:57 +0530188#ifdef INIT_BIN_LE
189static const char *sys_path_cmdline = " rootwait ro init="INIT_BIN_LE;
190#else
Mayank Grover3804be72017-06-22 11:59:23 +0530191static const char *sys_path_cmdline = " rootwait ro init=/init";
Sourabh Banerjee6c7153c2018-03-20 01:21:57 +0530192#endif
Sourabh Banerjee386b1322018-02-27 09:37:28 +0530193
194#if VERITY_LE
195static const char *verity_dev = " root=/dev/dm-0";
196static const char *verity_system_part = " dm=\"system";
197static const char *verity_params = " none ro,0 1 android-verity /dev/mmcblk0p";
198#else
Mayank Grover3804be72017-06-22 11:59:23 +0530199static const char *sys_path = " root=/dev/mmcblk0p";
Sourabh Banerjee386b1322018-02-27 09:37:28 +0530200#endif
Ajay Dudanid04110c2011-01-17 23:55:07 -0800201
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700202#if VERIFIED_BOOT
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700203static const char *verity_mode = " androidboot.veritymode=";
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700204static const char *verified_state= " androidboot.verifiedbootstate=";
Parth Dixita5715a02015-10-29 12:25:10 +0530205static const char *keymaster_v1= " androidboot.keymaster=1";
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700206//indexed based on enum values, green is 0 by default
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700207
208struct verified_boot_verity_mode vbvm[] =
209{
Channagoud Kadabi86b0c112016-03-16 19:23:16 -0700210#if ENABLE_VB_ATTEST
211 {false, "eio"},
212#else
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700213 {false, "logging"},
Channagoud Kadabi86b0c112016-03-16 19:23:16 -0700214#endif
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700215 {true, "enforcing"},
216};
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700217struct verified_boot_state_name vbsn[] =
218{
219 {GREEN, "green"},
220 {ORANGE, "orange"},
221 {YELLOW,"yellow"},
222 {RED,"red" },
223};
224#endif
Channagoud Kadabi86b0c112016-03-16 19:23:16 -0700225/*As per spec delay wait time before shutdown in Red state*/
226#define DELAY_WAIT 30000
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700227static unsigned page_size = 0;
228static unsigned page_mask = 0;
Gaurav Nebhwani4d2389c2016-03-17 21:10:05 +0530229static unsigned mmc_blocksize = 0;
230static unsigned mmc_blocksize_mask = 0;
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700231static char ffbm_mode_string[FFBM_MODE_BUF_SIZE];
232static bool boot_into_ffbm;
vijay kumar870515d2015-08-31 16:37:24 +0530233static char *target_boot_params = NULL;
Matthew Qind886f3c2014-01-17 16:52:01 +0800234static bool boot_reason_alarm;
Channagoud Kadabi80a182b2015-03-11 17:04:23 -0700235static bool devinfo_present = true;
Channagoud Kadabi736c4962015-08-21 11:56:52 -0700236bool boot_into_fastboot = false;
Parth Dixit4097b622016-03-15 14:42:27 +0530237static uint32_t dt_size = 0;
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700238
Shashank Mittalcd98d472011-08-02 14:29:24 -0700239/* Assuming unauthorized kernel image by default */
240static int auth_kernel_img = 0;
lijuang511a2b52015-08-14 20:50:51 +0800241static device_info device = {DEVICE_MAGIC, 0, 0, 0, 0, {0}, {0},{0}, 1};
vijay kumarc65876c2015-04-24 13:29:16 +0530242static bool is_allow_unlock = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -0700243
vijay kumarca2e6812015-07-08 20:28:25 +0530244static char frp_ptns[2][8] = {"config","frp"};
245
lijuang511a2b52015-08-14 20:50:51 +0800246static const char *critical_flash_allowed_ptn[] = {
247 "aboot",
248 "rpm",
249 "tz",
250 "sbl",
251 "sdi",
252 "sbl1",
253 "xbl",
254 "hyp",
255 "pmic",
256 "bootloader",
257 "devinfo",
258 "partition"};
259
Dima Zavin42168f22009-01-30 11:52:22 -0800260struct atag_ptbl_entry
261{
262 char name[16];
263 unsigned offset;
264 unsigned size;
265 unsigned flags;
266};
267
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700268/*
269 * Partition info, required to be published
270 * for fastboot
271 */
272struct getvar_partition_info {
Mayank Grover7b8a8f82017-10-27 13:07:59 +0530273 char part_name[MAX_GPT_NAME_SIZE]; /* Partition name */
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700274 char getvar_size[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for size */
275 char getvar_type[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for type */
276 char size_response[MAX_RSP_SIZE]; /* fastboot response for size */
277 char type_response[MAX_RSP_SIZE]; /* fastboot response for type */
278};
279
280/*
Mayank Grover7b8a8f82017-10-27 13:07:59 +0530281 * Update the part_type_known for known paritions types.
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700282 */
Mayank Groverd38fe012018-03-13 15:33:16 +0530283#define RAW_STR "raw"
Mayank Grover7b8a8f82017-10-27 13:07:59 +0530284struct getvar_partition_info part_info[NUM_PARTITIONS];
285struct getvar_partition_info part_type_known[] =
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700286{
Mayank Grover49920212018-02-09 18:15:55 +0530287 { "system" , "partition-size:", "partition-type:", "", "ext4" },
288 { "userdata" , "partition-size:", "partition-type:", "", "ext4" },
289 { "cache" , "partition-size:", "partition-type:", "", "ext4" },
290 { "recoveryfs" , "partition-size:", "partition-type:", "", "ext4" },
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700291};
292
293char max_download_size[MAX_RSP_SIZE];
Ameya Thakur11cf1a62013-08-05 12:44:48 -0700294char charger_screen_enabled[MAX_RSP_SIZE];
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800295char sn_buf[13];
Dhaval Patel223ec952013-07-18 14:49:44 -0700296char display_panel_buf[MAX_PANEL_BUF_SIZE];
Unnati Gandhi62c8ab82014-01-24 11:01:01 +0530297char panel_display_mode[MAX_RSP_SIZE];
Mayank Grover7b8a8f82017-10-27 13:07:59 +0530298#if PRODUCT_IOT
299char block_size_string[MAX_RSP_SIZE];
300
301/* For IOT we are using custom version */
302#define PRODUCT_IOT_VERSION "IOT001"
303char bootloader_version_string[MAX_RSP_SIZE];
304#endif
lijuang102dfa92015-10-09 18:31:03 +0800305
306#if CHECK_BAT_VOLTAGE
lijuang65c5a822015-08-29 16:35:36 +0800307char battery_voltage[MAX_RSP_SIZE];
lijuang102dfa92015-10-09 18:31:03 +0800308char battery_soc_ok [MAX_RSP_SIZE];
309#endif
310
lijuangf16461c2015-08-03 17:09:34 +0800311char get_variant[MAX_RSP_SIZE];
Greg Griscod6250552011-06-29 14:40:23 -0700312
Greg Griscod2471ef2011-07-14 13:00:42 -0700313extern int emmc_recovery_init(void);
314
Kinson Chik0b1c8162011-08-31 16:31:57 -0700315#if NO_KEYPAD_DRIVER
316extern int fastboot_trigger(void);
317#endif
Greg Griscod2471ef2011-07-14 13:00:42 -0700318
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800319static void update_ker_tags_rdisk_addr(struct boot_img_hdr *hdr, bool is_arm64)
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700320{
321 /* overwrite the destination of specified for the project */
Channagoud Kadabi7042fa32013-04-26 16:44:14 -0700322#ifdef ABOOT_IGNORE_BOOT_HEADER_ADDRS
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800323 if (is_arm64)
324 hdr->kernel_addr = ABOOT_FORCE_KERNEL64_ADDR;
325 else
326 hdr->kernel_addr = ABOOT_FORCE_KERNEL_ADDR;
Channagoud Kadabi7042fa32013-04-26 16:44:14 -0700327 hdr->ramdisk_addr = ABOOT_FORCE_RAMDISK_ADDR;
328 hdr->tags_addr = ABOOT_FORCE_TAGS_ADDR;
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700329#endif
330}
331
Dima Zavin42168f22009-01-30 11:52:22 -0800332static void ptentry_to_tag(unsigned **ptr, struct ptentry *ptn)
333{
334 struct atag_ptbl_entry atag_ptn;
335
336 memcpy(atag_ptn.name, ptn->name, 16);
337 atag_ptn.name[15] = '\0';
338 atag_ptn.offset = ptn->start;
339 atag_ptn.size = ptn->length;
340 atag_ptn.flags = ptn->flags;
341 memcpy(*ptr, &atag_ptn, sizeof(struct atag_ptbl_entry));
342 *ptr += sizeof(struct atag_ptbl_entry) / sizeof(unsigned);
343}
Brian Swetland9c4c0752009-01-25 16:23:50 -0800344
lijuang102dfa92015-10-09 18:31:03 +0800345#if CHECK_BAT_VOLTAGE
346void update_battery_status(void)
347{
348 snprintf(battery_voltage,MAX_RSP_SIZE, "%d",target_get_battery_voltage());
349 snprintf(battery_soc_ok ,MAX_RSP_SIZE, "%s",target_battery_soc_ok()? "yes":"no");
350}
351#endif
352
Neeti Desaie245d492012-06-01 12:52:13 -0700353unsigned char *update_cmdline(const char * cmdline)
Brian Swetland9c4c0752009-01-25 16:23:50 -0800354{
David Ng183a7422009-12-07 14:55:21 -0800355 int cmdline_len = 0;
356 int have_cmdline = 0;
Amol Jadi168b7712012-03-06 16:15:00 -0800357 unsigned char *cmdline_final = NULL;
Neeti Desaie245d492012-06-01 12:52:13 -0700358 int pause_at_bootup = 0;
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800359 bool warm_boot = false;
Pavel Nedev5614d222013-06-17 18:01:02 +0300360 bool gpt_exists = partition_gpt_exists();
Joonwoo Park61112782013-10-02 19:50:39 -0700361 int have_target_boot_params = 0;
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700362 char *boot_dev_buf = NULL;
Mayank Grover351a75e2017-05-30 20:06:08 +0530363 bool is_mdtp_activated = 0;
364 int current_active_slot = INVALID;
Mayank Grover3804be72017-06-22 11:59:23 +0530365 int system_ptn_index = -1;
366 unsigned int lun = 0;
367 char lun_char_base = 'a';
Sourabh Banerjee386b1322018-02-27 09:37:28 +0530368#if VERITY_LE
369 int syspath_buflen = strlen(verity_dev)
370 + strlen(verity_system_part) + (sizeof(char) * 2) + 2
371 + strlen(verity_params) + sizeof(int) + 2;
372#else
373 int syspath_buflen = strlen(sys_path) + sizeof(int) + 2; /*allocate buflen for largest possible string*/
374#endif
Mayank Grover3804be72017-06-22 11:59:23 +0530375 char syspath_buf[syspath_buflen];
Mayank Grover889be1b2017-09-12 20:12:23 +0530376#if VERIFIED_BOOT
377 uint32_t boot_state = RED;
378#endif
Hareesh Gauthamabf1e1e2017-04-12 18:09:24 +0530379
380#if USE_LE_SYSTEMD
381 is_systemd_present=true;
382#endif
383
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700384#if VERIFIED_BOOT
Mayank Grover912eaa62017-10-26 12:08:53 +0530385 if (VB_M <= target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +0530386 {
387 boot_state = boot_verify_get_state();
388 }
Parth Dixitddbc7352015-10-18 03:13:31 +0530389#endif
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700390
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200391#ifdef MDTP_SUPPORT
392 mdtp_activated(&is_mdtp_activated);
393#endif /* MDTP_SUPPORT */
Dima Zavin42168f22009-01-30 11:52:22 -0800394
Brian Swetland9c4c0752009-01-25 16:23:50 -0800395 if (cmdline && cmdline[0]) {
David Ng183a7422009-12-07 14:55:21 -0800396 cmdline_len = strlen(cmdline);
397 have_cmdline = 1;
398 }
399 if (target_is_emmc_boot()) {
400 cmdline_len += strlen(emmc_cmdline);
Sridhar Parasuram7bd4aaf2015-02-12 11:14:38 -0800401#if USE_BOOTDEV_CMDLINE
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700402 boot_dev_buf = (char *) malloc(sizeof(char) * BOOT_DEV_MAX_LEN);
403 ASSERT(boot_dev_buf);
404 platform_boot_dev_cmdline(boot_dev_buf);
405 cmdline_len += strlen(boot_dev_buf);
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700406#endif
David Ng183a7422009-12-07 14:55:21 -0800407 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800408
409 cmdline_len += strlen(usb_sn_cmdline);
410 cmdline_len += strlen(sn_buf);
411
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700412#if VERIFIED_BOOT
Mayank Grover912eaa62017-10-26 12:08:53 +0530413 if (VB_M <= target_get_vb_version())
Sridhar Parasuram7d8bb9a2015-09-23 18:21:19 -0700414 {
Mayank Grover889be1b2017-09-12 20:12:23 +0530415 cmdline_len += strlen(verified_state) + strlen(vbsn[boot_state].name);
416 if ((device.verity_mode != 0 ) && (device.verity_mode != 1))
417 {
418 dprintf(CRITICAL, "Devinfo paritition possibly corrupted!!!. Please erase devinfo partition to continue booting\n");
419 ASSERT(0);
420 }
421 cmdline_len += strlen(verity_mode) + strlen(vbvm[device.verity_mode].name);
422 cmdline_len += strlen(keymaster_v1);
Sridhar Parasuram7d8bb9a2015-09-23 18:21:19 -0700423 }
Parth Dixitddbc7352015-10-18 03:13:31 +0530424#endif
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700425
Pavel Nedev5614d222013-06-17 18:01:02 +0300426 if (boot_into_recovery && gpt_exists)
427 cmdline_len += strlen(secondary_gpt_enable);
428
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200429 if(is_mdtp_activated)
430 cmdline_len += strlen(mdtp_activated_flag);
431
Pavel Nedev328ac822013-04-05 15:25:11 +0300432 if (boot_into_ffbm) {
433 cmdline_len += strlen(androidboot_mode);
Hareesh Gauthamabf1e1e2017-04-12 18:09:24 +0530434
435 if(is_systemd_present)
436 cmdline_len += strlen(systemd_ffbm_mode);
437
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700438 cmdline_len += strlen(ffbm_mode_string);
Pavel Nedev898298c2013-02-27 12:36:09 -0800439 /* reduce kernel console messages to speed-up boot */
440 cmdline_len += strlen(loglevel);
Matthew Qind886f3c2014-01-17 16:52:01 +0800441 } else if (boot_reason_alarm) {
442 cmdline_len += strlen(alarmboot_cmdline);
Zhenhua Huang431dafa2015-06-30 16:13:37 +0800443 } else if ((target_build_variant_user() || device.charger_screen_enabled)
444 && target_pause_for_battery_charge()) {
David Ngf773dde2010-07-26 19:55:08 -0700445 pause_at_bootup = 1;
446 cmdline_len += strlen(battchg_pause);
447 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800448
Shashank Mittalcd98d472011-08-02 14:29:24 -0700449 if(target_use_signed_kernel() && auth_kernel_img) {
450 cmdline_len += strlen(auth_kernel);
451 }
452
Joonwoo Park61112782013-10-02 19:50:39 -0700453 if (get_target_boot_params(cmdline, boot_into_recovery ? "recoveryfs" :
454 "system",
vijay kumar870515d2015-08-31 16:37:24 +0530455 &target_boot_params) == 0) {
Joonwoo Park61112782013-10-02 19:50:39 -0700456 have_target_boot_params = 1;
457 cmdline_len += strlen(target_boot_params);
458 }
459
Ajay Dudanid04110c2011-01-17 23:55:07 -0800460 /* Determine correct androidboot.baseband to use */
461 switch(target_baseband())
462 {
463 case BASEBAND_APQ:
464 cmdline_len += strlen(baseband_apq);
465 break;
466
467 case BASEBAND_MSM:
468 cmdline_len += strlen(baseband_msm);
469 break;
470
471 case BASEBAND_CSFB:
472 cmdline_len += strlen(baseband_csfb);
473 break;
474
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800475 case BASEBAND_SVLTE2A:
476 cmdline_len += strlen(baseband_svlte2a);
Ajay Dudanid04110c2011-01-17 23:55:07 -0800477 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700478
479 case BASEBAND_MDM:
480 cmdline_len += strlen(baseband_mdm);
481 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700482
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800483 case BASEBAND_MDM2:
484 cmdline_len += strlen(baseband_mdm2);
485 break;
486
Amol Jadi5c61a952012-05-04 17:05:35 -0700487 case BASEBAND_SGLTE:
488 cmdline_len += strlen(baseband_sglte);
489 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530490
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800491 case BASEBAND_SGLTE2:
492 cmdline_len += strlen(baseband_sglte2);
493 break;
494
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530495 case BASEBAND_DSDA:
496 cmdline_len += strlen(baseband_dsda);
497 break;
Amol Jadi2a15a272013-01-22 12:03:36 -0800498
499 case BASEBAND_DSDA2:
500 cmdline_len += strlen(baseband_dsda2);
501 break;
Vijay Kumar Pendotib228cfc2016-06-13 20:15:23 +0530502 case BASEBAND_APQ_NOWGR:
503 cmdline_len += strlen(baseband_apq_nowgr);
504 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800505 }
506
Vladislav Levenetz3cd45242017-04-05 08:58:07 +0300507#if ENABLE_DISPLAY
Lijuan Gao4a5b8322014-07-24 10:38:42 +0800508 if (cmdline) {
509 if ((strstr(cmdline, DISPLAY_DEFAULT_PREFIX) == NULL) &&
Padmanabhan Komandurubccbcdc2015-06-30 16:19:24 +0530510 target_display_panel_node(display_panel_buf,
511 MAX_PANEL_BUF_SIZE) &&
Lijuan Gao4a5b8322014-07-24 10:38:42 +0800512 strlen(display_panel_buf)) {
513 cmdline_len += strlen(display_panel_buf);
514 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700515 }
Vladislav Levenetz3cd45242017-04-05 08:58:07 +0300516#endif
Dhaval Patel223ec952013-07-18 14:49:44 -0700517
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800518 if (target_warm_boot()) {
519 warm_boot = true;
520 cmdline_len += strlen(warmboot_cmdline);
521 }
522
Mayank Grover351a75e2017-05-30 20:06:08 +0530523 if (partition_multislot_is_supported())
524 {
525 current_active_slot = partition_find_active_slot();
526 cmdline_len += (strlen(androidboot_slot_suffix)+
527 strlen(SUFFIX_SLOT(current_active_slot)));
528
Mayank Grover3804be72017-06-22 11:59:23 +0530529 system_ptn_index = partition_get_index("system");
530 if (platform_boot_dev_isemmc())
531 {
Sourabh Banerjee386b1322018-02-27 09:37:28 +0530532#if VERITY_LE
533 /*
534 Condition 4: Verity and A/B both enabled
535 Eventual command line looks like:
536 ... androidboot.slot_suffix=<slot_suffix> ... rootfstype=ext4 ...
537 ... root=/dev/dm-0 dm="system_<slot_suffix> none ro,0 1 android-verity /dev/mmcblk0p<NN>"
538 */
539 snprintf(syspath_buf, syspath_buflen, " %s %s%s %s%d\"",
540 verity_dev,
541 verity_system_part, suffix_slot[current_active_slot],
542 verity_params, system_ptn_index + 1);
543#else
544 /*
545 Condition 5: A/B enabled, but verity disabled
546 Eventual command line looks like:
547 ... androidboot.slot_suffix=<slot_suffix> ... rootfstype=ext4 ...
548 ... root=/dev/mmcblk0p<NN> ...
549 */
550 snprintf(syspath_buf, syspath_buflen, " %s%d",
551 sys_path, system_ptn_index + 1);
552#endif
Mayank Grover3804be72017-06-22 11:59:23 +0530553 }
554 else
555 {
556 lun = partition_get_lun(system_ptn_index);
557 snprintf(syspath_buf, syspath_buflen, " root=/dev/sd%c%d",
558 lun_char_base + lun,
559 partition_get_index_in_lun("system", lun));
560 }
Mayank Grover351a75e2017-05-30 20:06:08 +0530561
Mayank Grover3804be72017-06-22 11:59:23 +0530562 cmdline_len += strlen(sys_path_cmdline);
563 cmdline_len += strlen(syspath_buf);
Mayank Grover351a75e2017-05-30 20:06:08 +0530564 if (!boot_into_recovery)
565 cmdline_len += strlen(skip_ramfs);
566 }
567
Channagoud Kadabi85c7ec32016-01-28 23:09:21 -0800568#if TARGET_CMDLINE_SUPPORT
569 char *target_cmdline_buf = malloc(TARGET_MAX_CMDLNBUF);
570 int target_cmd_line_len;
571 ASSERT(target_cmdline_buf);
572 target_cmd_line_len = target_update_cmdline(target_cmdline_buf);
573 cmdline_len += target_cmd_line_len;
574#endif
575
David Ng183a7422009-12-07 14:55:21 -0800576 if (cmdline_len > 0) {
577 const char *src;
Maria Yu52254c02014-07-04 16:14:54 +0800578 unsigned char *dst;
579
580 cmdline_final = (unsigned char*) malloc((cmdline_len + 4) & (~3));
581 ASSERT(cmdline_final != NULL);
vijay kumar287bb542015-09-29 13:01:52 +0530582 memset((void *)cmdline_final, 0, sizeof(*cmdline_final));
Maria Yu52254c02014-07-04 16:14:54 +0800583 dst = cmdline_final;
Neeti Desaie245d492012-06-01 12:52:13 -0700584
Amol Jadi168b7712012-03-06 16:15:00 -0800585 /* Save start ptr for debug print */
David Ng183a7422009-12-07 14:55:21 -0800586 if (have_cmdline) {
587 src = cmdline;
588 while ((*dst++ = *src++));
589 }
590 if (target_is_emmc_boot()) {
591 src = emmc_cmdline;
592 if (have_cmdline) --dst;
David Ngf773dde2010-07-26 19:55:08 -0700593 have_cmdline = 1;
594 while ((*dst++ = *src++));
Sridhar Parasuram7bd4aaf2015-02-12 11:14:38 -0800595#if USE_BOOTDEV_CMDLINE
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700596 src = boot_dev_buf;
597 if (have_cmdline) --dst;
598 while ((*dst++ = *src++));
599#endif
David Ngf773dde2010-07-26 19:55:08 -0700600 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800601
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700602#if VERIFIED_BOOT
Mayank Grover912eaa62017-10-26 12:08:53 +0530603 if (VB_M <= target_get_vb_version())
Sridhar Parasuram7d8bb9a2015-09-23 18:21:19 -0700604 {
Mayank Grover889be1b2017-09-12 20:12:23 +0530605 src = verified_state;
606 if(have_cmdline) --dst;
607 have_cmdline = 1;
608 while ((*dst++ = *src++));
609 src = vbsn[boot_state].name;
610 if(have_cmdline) --dst;
611 while ((*dst++ = *src++));
612
613 if ((device.verity_mode != 0 ) && (device.verity_mode != 1))
614 {
615 dprintf(CRITICAL, "Devinfo paritition possibly corrupted!!!. Please erase devinfo partition to continue booting\n");
616 ASSERT(0);
617 }
618 src = verity_mode;
619 if(have_cmdline) --dst;
620 while ((*dst++ = *src++));
621 src = vbvm[device.verity_mode].name;
622 if(have_cmdline) -- dst;
623 while ((*dst++ = *src++));
624 src = keymaster_v1;
625 if(have_cmdline) --dst;
626 while ((*dst++ = *src++));
Sridhar Parasuram7d8bb9a2015-09-23 18:21:19 -0700627 }
Parth Dixitddbc7352015-10-18 03:13:31 +0530628#endif
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800629 src = usb_sn_cmdline;
630 if (have_cmdline) --dst;
631 have_cmdline = 1;
632 while ((*dst++ = *src++));
633 src = sn_buf;
634 if (have_cmdline) --dst;
635 have_cmdline = 1;
636 while ((*dst++ = *src++));
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800637 if (warm_boot) {
638 if (have_cmdline) --dst;
639 src = warmboot_cmdline;
640 while ((*dst++ = *src++));
641 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800642
Pavel Nedev5614d222013-06-17 18:01:02 +0300643 if (boot_into_recovery && gpt_exists) {
644 src = secondary_gpt_enable;
645 if (have_cmdline) --dst;
646 while ((*dst++ = *src++));
647 }
648
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200649 if (is_mdtp_activated) {
650 src = mdtp_activated_flag;
651 if (have_cmdline) --dst;
652 while ((*dst++ = *src++));
653 }
654
Pavel Nedev328ac822013-04-05 15:25:11 +0300655 if (boot_into_ffbm) {
656 src = androidboot_mode;
657 if (have_cmdline) --dst;
658 while ((*dst++ = *src++));
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700659 src = ffbm_mode_string;
Pavel Nedev328ac822013-04-05 15:25:11 +0300660 if (have_cmdline) --dst;
661 while ((*dst++ = *src++));
Hareesh Gauthamabf1e1e2017-04-12 18:09:24 +0530662
663 if(is_systemd_present) {
664 src = systemd_ffbm_mode;
665 if (have_cmdline) --dst;
666 while ((*dst++ = *src++));
667 }
668
Pavel Nedev898298c2013-02-27 12:36:09 -0800669 src = loglevel;
670 if (have_cmdline) --dst;
671 while ((*dst++ = *src++));
Matthew Qind886f3c2014-01-17 16:52:01 +0800672 } else if (boot_reason_alarm) {
673 src = alarmboot_cmdline;
674 if (have_cmdline) --dst;
675 while ((*dst++ = *src++));
Pavel Nedev328ac822013-04-05 15:25:11 +0300676 } else if (pause_at_bootup) {
David Ngf773dde2010-07-26 19:55:08 -0700677 src = battchg_pause;
678 if (have_cmdline) --dst;
David Ng183a7422009-12-07 14:55:21 -0800679 while ((*dst++ = *src++));
680 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800681
Shashank Mittalcd98d472011-08-02 14:29:24 -0700682 if(target_use_signed_kernel() && auth_kernel_img) {
683 src = auth_kernel;
684 if (have_cmdline) --dst;
685 while ((*dst++ = *src++));
686 }
687
Ajay Dudanid04110c2011-01-17 23:55:07 -0800688 switch(target_baseband())
689 {
690 case BASEBAND_APQ:
691 src = baseband_apq;
692 if (have_cmdline) --dst;
693 while ((*dst++ = *src++));
694 break;
695
696 case BASEBAND_MSM:
697 src = baseband_msm;
698 if (have_cmdline) --dst;
699 while ((*dst++ = *src++));
700 break;
701
702 case BASEBAND_CSFB:
703 src = baseband_csfb;
704 if (have_cmdline) --dst;
705 while ((*dst++ = *src++));
706 break;
707
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800708 case BASEBAND_SVLTE2A:
709 src = baseband_svlte2a;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800710 if (have_cmdline) --dst;
711 while ((*dst++ = *src++));
712 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700713
714 case BASEBAND_MDM:
715 src = baseband_mdm;
716 if (have_cmdline) --dst;
717 while ((*dst++ = *src++));
718 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700719
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800720 case BASEBAND_MDM2:
721 src = baseband_mdm2;
722 if (have_cmdline) --dst;
723 while ((*dst++ = *src++));
724 break;
725
Amol Jadi5c61a952012-05-04 17:05:35 -0700726 case BASEBAND_SGLTE:
727 src = baseband_sglte;
728 if (have_cmdline) --dst;
729 while ((*dst++ = *src++));
730 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530731
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800732 case BASEBAND_SGLTE2:
733 src = baseband_sglte2;
734 if (have_cmdline) --dst;
735 while ((*dst++ = *src++));
736 break;
737
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530738 case BASEBAND_DSDA:
739 src = baseband_dsda;
740 if (have_cmdline) --dst;
741 while ((*dst++ = *src++));
742 break;
Amol Jadi2a15a272013-01-22 12:03:36 -0800743
744 case BASEBAND_DSDA2:
745 src = baseband_dsda2;
746 if (have_cmdline) --dst;
747 while ((*dst++ = *src++));
748 break;
Vijay Kumar Pendotib228cfc2016-06-13 20:15:23 +0530749 case BASEBAND_APQ_NOWGR:
750 src = baseband_apq_nowgr;
751 if (have_cmdline) --dst;
752 while ((*dst++ = *src++));
753 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800754 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700755
756 if (strlen(display_panel_buf)) {
Dhaval Patel223ec952013-07-18 14:49:44 -0700757 src = display_panel_buf;
758 if (have_cmdline) --dst;
759 while ((*dst++ = *src++));
760 }
Joonwoo Park61112782013-10-02 19:50:39 -0700761
762 if (have_target_boot_params) {
763 if (have_cmdline) --dst;
764 src = target_boot_params;
765 while ((*dst++ = *src++));
vijay kumar870515d2015-08-31 16:37:24 +0530766 free(target_boot_params);
Joonwoo Park61112782013-10-02 19:50:39 -0700767 }
Channagoud Kadabi85c7ec32016-01-28 23:09:21 -0800768
Mayank Grover351a75e2017-05-30 20:06:08 +0530769 if (partition_multislot_is_supported() && have_cmdline)
770 {
771 src = androidboot_slot_suffix;
772 --dst;
773 while ((*dst++ = *src++));
774 --dst;
775 src = SUFFIX_SLOT(current_active_slot);
776 while ((*dst++ = *src++));
777
778 if (!boot_into_recovery)
779 {
780 src = skip_ramfs;
781 --dst;
782 while ((*dst++ = *src++));
783 }
784
785 src = sys_path_cmdline;
786 --dst;
787 while ((*dst++ = *src++));
Mayank Grover3804be72017-06-22 11:59:23 +0530788
789 src = syspath_buf;
790 --dst;
791 while ((*dst++ = *src++));
Mayank Grover351a75e2017-05-30 20:06:08 +0530792 }
793
Channagoud Kadabi85c7ec32016-01-28 23:09:21 -0800794#if TARGET_CMDLINE_SUPPORT
795 if (target_cmdline_buf && target_cmd_line_len)
796 {
797 if (have_cmdline) --dst;
798 src = target_cmdline_buf;
799 while((*dst++ = *src++));
800 free(target_cmdline_buf);
801 }
802#endif
Neeti Desaie245d492012-06-01 12:52:13 -0700803 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700804
805
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700806 if (boot_dev_buf)
807 free(boot_dev_buf);
808
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -0800809 if (cmdline_final)
810 dprintf(INFO, "cmdline: %s\n", cmdline_final);
811 else
812 dprintf(INFO, "cmdline is NULL\n");
Neeti Desaie245d492012-06-01 12:52:13 -0700813 return cmdline_final;
814}
815
816unsigned *atag_core(unsigned *ptr)
817{
818 /* CORE */
819 *ptr++ = 2;
820 *ptr++ = 0x54410001;
821
822 return ptr;
823
824}
825
826unsigned *atag_ramdisk(unsigned *ptr, void *ramdisk,
827 unsigned ramdisk_size)
828{
829 if (ramdisk_size) {
830 *ptr++ = 4;
831 *ptr++ = 0x54420005;
832 *ptr++ = (unsigned)ramdisk;
833 *ptr++ = ramdisk_size;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800834 }
835
Neeti Desaie245d492012-06-01 12:52:13 -0700836 return ptr;
837}
838
839unsigned *atag_ptable(unsigned **ptr_addr)
840{
841 int i;
842 struct ptable *ptable;
843
844 if ((ptable = flash_get_ptable()) && (ptable->count != 0)) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700845 *(*ptr_addr)++ = 2 + (ptable->count * (sizeof(struct atag_ptbl_entry) /
846 sizeof(unsigned)));
Neeti Desaie245d492012-06-01 12:52:13 -0700847 *(*ptr_addr)++ = 0x4d534d70;
848 for (i = 0; i < ptable->count; ++i)
849 ptentry_to_tag(ptr_addr, ptable_get(ptable, i));
850 }
851
852 return (*ptr_addr);
853}
854
855unsigned *atag_cmdline(unsigned *ptr, const char *cmdline)
856{
857 int cmdline_length = 0;
858 int n;
Neeti Desaie245d492012-06-01 12:52:13 -0700859 char *dest;
860
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800861 cmdline_length = strlen((const char*)cmdline);
Neeti Desaie245d492012-06-01 12:52:13 -0700862 n = (cmdline_length + 4) & (~3);
863
864 *ptr++ = (n / 4) + 2;
865 *ptr++ = 0x54410009;
866 dest = (char *) ptr;
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800867 while ((*dest++ = *cmdline++));
Neeti Desaie245d492012-06-01 12:52:13 -0700868 ptr += (n / 4);
869
870 return ptr;
871}
872
873unsigned *atag_end(unsigned *ptr)
874{
Brian Swetland9c4c0752009-01-25 16:23:50 -0800875 /* END */
876 *ptr++ = 0;
877 *ptr++ = 0;
878
Neeti Desaie245d492012-06-01 12:52:13 -0700879 return ptr;
880}
881
882void generate_atags(unsigned *ptr, const char *cmdline,
883 void *ramdisk, unsigned ramdisk_size)
884{
vijay kumar21a37452015-12-29 16:23:21 +0530885 unsigned *orig_ptr = ptr;
Neeti Desaie245d492012-06-01 12:52:13 -0700886 ptr = atag_core(ptr);
887 ptr = atag_ramdisk(ptr, ramdisk, ramdisk_size);
888 ptr = target_atag_mem(ptr);
889
890 /* Skip NAND partition ATAGS for eMMC boot */
891 if (!target_is_emmc_boot()){
892 ptr = atag_ptable(&ptr);
893 }
894
vijay kumar21a37452015-12-29 16:23:21 +0530895 /*
896 * Atags size filled till + cmdline size + 1 unsigned for 4-byte boundary + 4 unsigned
897 * for atag identifier in atag_cmdline and atag_end should be with in MAX_TAGS_SIZE bytes
898 */
Mayank Groverbc8a2ef2018-02-23 18:03:36 +0530899 if (!cmdline)
900 return;
901
vijay kumar21a37452015-12-29 16:23:21 +0530902 if (((ptr - orig_ptr) + strlen(cmdline) + 5 * sizeof(unsigned)) < MAX_TAGS_SIZE) {
903 ptr = atag_cmdline(ptr, cmdline);
904 ptr = atag_end(ptr);
905 }
906 else {
907 dprintf(CRITICAL,"Crossing ATAGs Max size allowed\n");
908 ASSERT(0);
909 }
Neeti Desaie245d492012-06-01 12:52:13 -0700910}
911
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700912typedef void entry_func_ptr(unsigned, unsigned, unsigned*);
Neeti Desaie245d492012-06-01 12:52:13 -0700913void boot_linux(void *kernel, unsigned *tags,
914 const char *cmdline, unsigned machtype,
915 void *ramdisk, unsigned ramdisk_size)
916{
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800917 unsigned char *final_cmdline;
Amol Jadib6be5c12012-11-14 13:39:51 -0800918#if DEVICE_TREE
Neeti Desai17379b82012-06-04 18:42:53 -0700919 int ret = 0;
Amol Jadib6be5c12012-11-14 13:39:51 -0800920#endif
921
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700922 void (*entry)(unsigned, unsigned, unsigned*) = (entry_func_ptr*)(PA((addr_t)kernel));
Deepa Dinamani25a9f762012-11-30 15:57:15 -0800923 uint32_t tags_phys = PA((addr_t)tags);
vijay kumar1a50a642015-11-16 12:41:15 +0530924 struct kernel64_hdr *kptr = ((struct kernel64_hdr*)(PA((addr_t)kernel)));
Deepa Dinamani25a9f762012-11-30 15:57:15 -0800925
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530926 ramdisk = (void *)PA((addr_t)ramdisk);
Neeti Desaie245d492012-06-01 12:52:13 -0700927
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800928 final_cmdline = update_cmdline((const char*)cmdline);
929
Neeti Desai17379b82012-06-04 18:42:53 -0700930#if DEVICE_TREE
Amol Jadib6be5c12012-11-14 13:39:51 -0800931 dprintf(INFO, "Updating device tree: start\n");
932
Neeti Desai17379b82012-06-04 18:42:53 -0700933 /* Update the Device Tree */
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530934 ret = update_device_tree((void *)tags,(const char *)final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -0700935 if(ret)
936 {
937 dprintf(CRITICAL, "ERROR: Updating Device Tree Failed \n");
938 ASSERT(0);
939 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800940 dprintf(INFO, "Updating device tree: done\n");
Neeti Desai17379b82012-06-04 18:42:53 -0700941#else
Neeti Desaie245d492012-06-01 12:52:13 -0700942 /* Generating the Atags */
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800943 generate_atags(tags, final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -0700944#endif
Neeti Desaie245d492012-06-01 12:52:13 -0700945
Mayank Groverbc8a2ef2018-02-23 18:03:36 +0530946 if (final_cmdline)
947 free(final_cmdline);
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -0700948
949#if VERIFIED_BOOT
Mayank Grover912eaa62017-10-26 12:08:53 +0530950 if (VB_M <= target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +0530951 {
952 if (device.verity_mode == 0) {
lijuangbdd9bb42016-03-01 18:22:17 +0800953#if FBCON_DISPLAY_MSG
Channagoud Kadabi86b0c112016-03-16 19:23:16 -0700954#if ENABLE_VB_ATTEST
Mayank Grover889be1b2017-09-12 20:12:23 +0530955 display_bootverify_menu(DISPLAY_MENU_EIO);
956 wait_for_users_action();
957 if(!pwr_key_is_pressed)
958 shutdown_device();
Channagoud Kadabi86b0c112016-03-16 19:23:16 -0700959#else
Mayank Grover889be1b2017-09-12 20:12:23 +0530960 display_bootverify_menu(DISPLAY_MENU_LOGGING);
Channagoud Kadabi86b0c112016-03-16 19:23:16 -0700961#endif
Mayank Grover889be1b2017-09-12 20:12:23 +0530962 wait_for_users_action();
lijuangbdd9bb42016-03-01 18:22:17 +0800963#else
Mayank Grover889be1b2017-09-12 20:12:23 +0530964 dprintf(CRITICAL,
965 "The dm-verity is not started in enforcing mode.\nWait for 5 seconds before proceeding\n");
966 mdelay(5000);
lijuangbdd9bb42016-03-01 18:22:17 +0800967#endif
Mayank Grover889be1b2017-09-12 20:12:23 +0530968 }
lijuangbdd9bb42016-03-01 18:22:17 +0800969 }
lijuangbdd9bb42016-03-01 18:22:17 +0800970#endif
971
972#if VERIFIED_BOOT
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -0700973 /* Write protect the device info */
Channagoud Kadabi3bd9d1e2015-05-05 16:18:20 -0700974 if (!boot_into_recovery && target_build_variant_user() && devinfo_present && mmc_write_protect("devinfo", 1))
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -0700975 {
976 dprintf(INFO, "Failed to write protect dev info\n");
977 ASSERT(0);
978 }
979#endif
980
Aravind Venkateswaran8f076242014-02-25 16:25:30 -0800981 /* Turn off splash screen if enabled */
982#if DISPLAY_SPLASH_SCREEN
983 target_display_shutdown();
984#endif
985
Veera Sundaram Sankaran67ea0932015-09-25 10:09:30 -0700986 /* Perform target specific cleanup */
987 target_uninit();
Aravind Venkateswaran8f076242014-02-25 16:25:30 -0800988
Deepa Dinamani33734bc2013-03-06 12:16:06 -0800989 dprintf(INFO, "booting linux @ %p, ramdisk @ %p (%d), tags/device tree @ %p\n",
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530990 entry, ramdisk, ramdisk_size, (void *)tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800991
992 enter_critical_section();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700993
Amol Jadi4421e652011-06-16 15:00:48 -0700994 /* do any platform specific cleanup before kernel entry */
995 platform_uninit();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700996
Brian Swetland9c4c0752009-01-25 16:23:50 -0800997 arch_disable_cache(UCACHE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700998
Amol Jadi504f9fe2012-08-16 13:56:48 -0700999#if ARM_WITH_MMU
Brian Swetland9c4c0752009-01-25 16:23:50 -08001000 arch_disable_mmu();
Amol Jadi504f9fe2012-08-16 13:56:48 -07001001#endif
Amol Jadi492d5a52013-03-15 16:12:34 -07001002 bs_set_timestamp(BS_KERNEL_ENTRY);
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001003
1004 if (IS_ARM64(kptr))
1005 /* Jump to a 64bit kernel */
1006 scm_elexec_call((paddr_t)kernel, tags_phys);
1007 else
1008 /* Jump to a 32bit kernel */
1009 entry(0, machtype, (unsigned*)tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001010}
1011
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001012/* Function to check if the memory address range falls within the aboot
1013 * boundaries.
1014 * start: Start of the memory region
1015 * size: Size of the memory region
1016 */
Vijay Kumar Pendoti9c002ad2016-03-09 13:52:45 +05301017int check_aboot_addr_range_overlap(uintptr_t start, uint32_t size)
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001018{
1019 /* Check for boundary conditions. */
Sundarajan Srinivasance2a0ea2013-12-16 17:02:56 -08001020 if ((UINT_MAX - start) < size)
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001021 return -1;
1022
1023 /* Check for memory overlap. */
1024 if ((start < MEMBASE) && ((start + size) <= MEMBASE))
1025 return 0;
Channagoud Kadabi94143912013-10-15 12:53:52 -07001026 else if (start >= (MEMBASE + MEMSIZE))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001027 return 0;
1028 else
1029 return -1;
1030}
1031
Mayank Grovere559cec2017-10-17 15:12:03 +05301032/* Function to check if the memory address range falls beyond ddr region.
1033 * start: Start of the memory region
1034 * size: Size of the memory region
1035 */
1036int check_ddr_addr_range_bound(uintptr_t start, uint32_t size)
1037{
1038 uintptr_t ddr_pa_start_addr = PA(get_ddr_start());
1039 uint64_t ddr_size = smem_get_ddr_size();
1040 uint64_t ddr_pa_end_addr = ddr_pa_start_addr + ddr_size;
1041 uintptr_t pa_start_addr = PA(start);
1042
1043 /* Check for boundary conditions. */
1044 if ((UINT_MAX - start) < size)
1045 return -1;
1046
1047 /* Check if memory range is beyond the ddr range. */
1048 if (pa_start_addr < ddr_pa_start_addr ||
1049 pa_start_addr >= (ddr_pa_end_addr) ||
1050 (pa_start_addr + size) > ddr_pa_end_addr)
1051 return -1;
1052 else
1053 return 0;
1054}
1055
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001056BUF_DMA_ALIGN(buf, BOOT_IMG_MAX_PAGE_SIZE); //Equal to max-supported pagesize
Dima Zavin214cc642009-01-26 11:16:21 -08001057
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001058static void verify_signed_bootimg(uint32_t bootimg_addr, uint32_t bootimg_size)
1059{
1060 int ret;
Channagoud Kadabia8c623f2015-01-13 14:48:48 -08001061
1062#if !VERIFIED_BOOT
Sundarajan Srinivasance54d6e2013-11-11 12:45:00 -08001063#if IMAGE_VERIF_ALGO_SHA1
1064 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA1;
1065#else
1066 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA256;
1067#endif
Channagoud Kadabia8c623f2015-01-13 14:48:48 -08001068#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001069
1070 /* Assume device is rooted at this time. */
1071 device.is_tampered = 1;
1072
1073 dprintf(INFO, "Authenticating boot image (%d): start\n", bootimg_size);
1074
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001075#if VERIFIED_BOOT
Mayank Grover68fbf0d2017-10-24 14:13:39 +05301076 char *ptn_name = NULL;
1077 if (boot_into_recovery &&
1078 (!partition_multislot_is_supported()))
1079 ptn_name = "/recovery";
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001080 else
Mayank Grover68fbf0d2017-10-24 14:13:39 +05301081 ptn_name = "/boot";
1082
1083 ret = boot_verify_image((unsigned char *)bootimg_addr,
1084 bootimg_size, ptn_name);
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001085 boot_verify_print_state();
1086#else
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001087 ret = image_verify((unsigned char *)bootimg_addr,
1088 (unsigned char *)(bootimg_addr + bootimg_size),
1089 bootimg_size,
Sundarajan Srinivasance54d6e2013-11-11 12:45:00 -08001090 auth_algo);
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001091#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001092 dprintf(INFO, "Authenticating boot image: done return value = %d\n", ret);
1093
1094 if (ret)
1095 {
1096 /* Authorized kernel */
1097 device.is_tampered = 0;
Sundarajan Srinivasan3fb21f12013-09-16 18:36:15 -07001098 auth_kernel_img = 1;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001099 }
1100
Amit Blay4aa292f2015-04-28 21:55:59 +03001101#ifdef MDTP_SUPPORT
1102 {
1103 /* Verify MDTP lock.
1104 * For boot & recovery partitions, use aboot's verification result.
1105 */
1106 mdtp_ext_partition_verification_t ext_partition;
1107 ext_partition.partition = boot_into_recovery ? MDTP_PARTITION_RECOVERY : MDTP_PARTITION_BOOT;
1108 ext_partition.integrity_state = device.is_tampered ? MDTP_PARTITION_STATE_INVALID : MDTP_PARTITION_STATE_VALID;
1109 ext_partition.page_size = 0; /* Not needed since already validated */
1110 ext_partition.image_addr = 0; /* Not needed since already validated */
1111 ext_partition.image_size = 0; /* Not needed since already validated */
1112 ext_partition.sig_avail = FALSE; /* Not needed since already validated */
1113 mdtp_fwlock_verify_lock(&ext_partition);
1114 }
1115#endif /* MDTP_SUPPORT */
1116
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001117#if USE_PCOM_SECBOOT
1118 set_tamper_flag(device.is_tampered);
1119#endif
1120
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001121#if VERIFIED_BOOT
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001122 switch(boot_verify_get_state())
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001123 {
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001124 case RED:
lijuanga40d6302015-07-20 20:10:13 +08001125#if FBCON_DISPLAY_MSG
lijuang9a7d3b92015-11-30 14:41:24 +08001126 display_bootverify_menu(DISPLAY_MENU_RED);
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07001127#if ENABLE_VB_ATTEST
1128 mdelay(DELAY_WAIT);
1129 shutdown_device();
1130#else
lijuanga40d6302015-07-20 20:10:13 +08001131 wait_for_users_action();
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07001132#endif
lijuanga40d6302015-07-20 20:10:13 +08001133#else
1134 dprintf(CRITICAL,
1135 "Your device has failed verification and may not work properly.\nWait for 5 seconds before proceeding\n");
1136 mdelay(5000);
1137#endif
1138
1139 break;
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001140 case YELLOW:
lijuanga40d6302015-07-20 20:10:13 +08001141#if FBCON_DISPLAY_MSG
lijuang9a7d3b92015-11-30 14:41:24 +08001142 display_bootverify_menu(DISPLAY_MENU_YELLOW);
lijuanga40d6302015-07-20 20:10:13 +08001143 wait_for_users_action();
1144#else
1145 dprintf(CRITICAL,
1146 "Your device has loaded a different operating system.\nWait for 5 seconds before proceeding\n");
1147 mdelay(5000);
1148#endif
1149 break;
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001150 default:
lijuanga40d6302015-07-20 20:10:13 +08001151 break;
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001152 }
1153#endif
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001154#if !VERIFIED_BOOT
Unnati Gandhi1be04752015-03-27 19:41:53 +05301155 if(device.is_tampered)
1156 {
1157 write_device_info_mmc(&device);
1158 #ifdef TZ_TAMPER_FUSE
1159 set_tamper_fuse_cmd();
1160 #endif
1161 #ifdef ASSERT_ON_TAMPER
1162 dprintf(CRITICAL, "Device is tampered. Asserting..\n");
1163 ASSERT(0);
1164 #endif
1165 }
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001166#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001167}
1168
Maunik Shah0f3c8ac2014-03-02 17:47:58 +05301169static bool check_format_bit()
1170{
1171 bool ret = false;
1172 int index;
1173 uint64_t offset;
1174 struct boot_selection_info *in = NULL;
1175 char *buf = NULL;
1176
1177 index = partition_get_index("bootselect");
1178 if (index == INVALID_PTN)
1179 {
1180 dprintf(INFO, "Unable to locate /bootselect partition\n");
1181 return ret;
1182 }
1183 offset = partition_get_offset(index);
1184 if(!offset)
1185 {
1186 dprintf(INFO, "partition /bootselect doesn't exist\n");
1187 return ret;
1188 }
1189 buf = (char *) memalign(CACHE_LINE, ROUNDUP(page_size, CACHE_LINE));
Mayank Grover48860402016-11-29 12:34:53 +05301190 mmc_set_lun(partition_get_lun(index));
Maunik Shah0f3c8ac2014-03-02 17:47:58 +05301191 ASSERT(buf);
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301192 if (mmc_read(offset, (uint32_t *)buf, page_size))
Maunik Shah0f3c8ac2014-03-02 17:47:58 +05301193 {
1194 dprintf(INFO, "mmc read failure /bootselect %d\n", page_size);
1195 free(buf);
1196 return ret;
1197 }
1198 in = (struct boot_selection_info *) buf;
1199 if ((in->signature == BOOTSELECT_SIGNATURE) &&
1200 (in->version == BOOTSELECT_VERSION)) {
1201 if ((in->state_info & BOOTSELECT_FORMAT) &&
1202 !(in->state_info & BOOTSELECT_FACTORY))
1203 ret = true;
1204 } else {
1205 dprintf(CRITICAL, "Signature: 0x%08x or version: 0x%08x mismatched of /bootselect\n",
1206 in->signature, in->version);
1207 ASSERT(0);
1208 }
1209 free(buf);
1210 return ret;
1211}
1212
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001213void boot_verifier_init()
1214{
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001215 uint32_t boot_state;
1216 /* Check if device unlock */
1217 if(device.is_unlocked)
1218 {
1219 boot_verify_send_event(DEV_UNLOCK);
1220 boot_verify_print_state();
1221 dprintf(CRITICAL, "Device is unlocked! Skipping verification...\n");
1222 return;
1223 }
1224 else
1225 {
1226 boot_verify_send_event(BOOT_INIT);
1227 }
1228
1229 /* Initialize keystore */
1230 boot_state = boot_verify_keystore_init();
1231 if(boot_state == YELLOW)
1232 {
1233 boot_verify_print_state();
1234 dprintf(CRITICAL, "Keystore verification failed! Continuing anyways...\n");
1235 }
1236}
1237
Shashank Mittal23b8f422010-04-16 19:27:21 -07001238int boot_linux_from_mmc(void)
1239{
1240 struct boot_img_hdr *hdr = (void*) buf;
1241 struct boot_img_hdr *uhdr;
1242 unsigned offset = 0;
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07001243 int rcode;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001244 unsigned long long ptn = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001245 int index = INVALID_PTN;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001246
Shashank Mittalcd98d472011-08-02 14:29:24 -07001247 unsigned char *image_addr = 0;
1248 unsigned kernel_actual;
1249 unsigned ramdisk_actual;
1250 unsigned imagesize_actual;
Neeti Desai465491e2012-07-31 12:53:35 -07001251 unsigned second_actual = 0;
Neeti Desai465491e2012-07-31 12:53:35 -07001252
Matthew Qin271927e2015-03-31 22:07:07 -04001253 unsigned int dtb_size = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08001254 unsigned int out_len = 0;
1255 unsigned int out_avai_len = 0;
1256 unsigned char *out_addr = NULL;
1257 uint32_t dtb_offset = 0;
1258 unsigned char *kernel_start_addr = NULL;
1259 unsigned int kernel_size = 0;
Ameya Thakur10a33452016-06-13 14:24:26 -07001260 unsigned int patched_kernel_hdr_size = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08001261 int rc;
Mayank Groverc93cad82017-10-03 12:23:45 +05301262 char *ptn_name = NULL;
Neeti Desai465491e2012-07-31 12:53:35 -07001263#if DEVICE_TREE
1264 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -07001265 struct dt_entry dt_entry;
Neeti Desai465491e2012-07-31 12:53:35 -07001266 unsigned dt_table_offset;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001267 uint32_t dt_actual;
Deepa Dinamani19648b42013-09-05 17:05:55 -07001268 uint32_t dt_hdr_size;
Matthew Qin271927e2015-03-31 22:07:07 -04001269 unsigned char *best_match_dt_addr = NULL;
Neeti Desai465491e2012-07-31 12:53:35 -07001270#endif
Matthew Qin49e51fa2015-02-09 10:40:45 +08001271 struct kernel64_hdr *kptr = NULL;
Mayank Grover351a75e2017-05-30 20:06:08 +05301272 int current_active_slot = INVALID;
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001273
Maunik Shah0f3c8ac2014-03-02 17:47:58 +05301274 if (check_format_bit())
1275 boot_into_recovery = 1;
1276
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07001277 if (!boot_into_recovery) {
1278 memset(ffbm_mode_string, '\0', sizeof(ffbm_mode_string));
1279 rcode = get_ffbm(ffbm_mode_string, sizeof(ffbm_mode_string));
1280 if (rcode <= 0) {
1281 boot_into_ffbm = false;
1282 if (rcode < 0)
1283 dprintf(CRITICAL,"failed to get ffbm cookie");
1284 } else
1285 boot_into_ffbm = true;
1286 } else
1287 boot_into_ffbm = false;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001288 uhdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
1289 if (!memcmp(uhdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1290 dprintf(INFO, "Unified boot method!\n");
1291 hdr = uhdr;
1292 goto unified_boot;
1293 }
Mayank Groverc93cad82017-10-03 12:23:45 +05301294
1295 /* For a/b recovery image code is on boot partition.
1296 If we support multislot, always use boot partition. */
1297 if (boot_into_recovery &&
1298 (!partition_multislot_is_supported()))
1299 ptn_name = "recovery";
1300 else
1301 ptn_name = "boot";
1302
1303 index = partition_get_index(ptn_name);
1304 ptn = partition_get_offset(index);
1305 if(ptn == 0) {
1306 dprintf(CRITICAL, "ERROR: No %s partition found\n", ptn_name);
1307 return -1;
Kinson Chikf1a43512011-07-14 11:28:39 -07001308 }
Mayank Groverc93cad82017-10-03 12:23:45 +05301309
Channagoud Kadabief0547c2015-02-10 12:57:38 -08001310 /* Set Lun for boot & recovery partitions */
1311 mmc_set_lun(partition_get_lun(index));
Shashank Mittal23b8f422010-04-16 19:27:21 -07001312
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301313 if (mmc_read(ptn + offset, (uint32_t *) buf, page_size)) {
Shashank Mittal23b8f422010-04-16 19:27:21 -07001314 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
1315 return -1;
1316 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001317
1318 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001319 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Mayank Grover351a75e2017-05-30 20:06:08 +05301320 return ERR_INVALID_BOOT_MAGIC;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001321 }
1322
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07001323 if (hdr->page_size && (hdr->page_size != page_size)) {
vijay kumar2e21b3a2014-06-26 17:40:15 +05301324
1325 if (hdr->page_size > BOOT_IMG_MAX_PAGE_SIZE) {
1326 dprintf(CRITICAL, "ERROR: Invalid page size\n");
1327 return -1;
1328 }
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07001329 page_size = hdr->page_size;
1330 page_mask = page_size - 1;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001331 }
1332
Matthew Qin49e51fa2015-02-09 10:40:45 +08001333 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1334 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Kishor PKd8ddcad2017-07-27 13:53:57 +05301335 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001336
Matthew Qin49e51fa2015-02-09 10:40:45 +08001337 image_addr = (unsigned char *)target_get_scratch_address();
Kishor PK9e91b592017-05-24 12:14:55 +05301338 memcpy(image_addr, (void *)buf, page_size);
1339
1340 /* ensure commandline is terminated */
1341 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
Matthew Qin49e51fa2015-02-09 10:40:45 +08001342
1343#if DEVICE_TREE
Parth Dixit4097b622016-03-15 14:42:27 +05301344#ifndef OSVERSION_IN_BOOTIMAGE
1345 dt_size = hdr->dt_size;
1346#endif
1347 dt_actual = ROUND_TO_PAGE(dt_size, page_mask);
Kishor PKd8ddcad2017-07-27 13:53:57 +05301348 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 +05301349 dprintf(CRITICAL, "Integer overflow detected in bootimage header fields at %u in %s\n",__LINE__,__FILE__);
1350 return -1;
1351 }
Kishor PKd8ddcad2017-07-27 13:53:57 +05301352 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + second_actual + dt_actual);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001353#else
Kishor PKd8ddcad2017-07-27 13:53:57 +05301354 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 +05301355 dprintf(CRITICAL, "Integer overflow detected in bootimage header fields at %u in %s\n",__LINE__,__FILE__);
1356 return -1;
1357 }
Kishor PKd8ddcad2017-07-27 13:53:57 +05301358 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + second_actual);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001359#endif
1360
1361#if VERIFIED_BOOT
1362 boot_verifier_init();
1363#endif
1364
Vijay Kumar Pendoti9c002ad2016-03-09 13:52:45 +05301365 if (check_aboot_addr_range_overlap((uintptr_t) image_addr, imagesize_actual))
Matthew Qin49e51fa2015-02-09 10:40:45 +08001366 {
1367 dprintf(CRITICAL, "Boot image buffer address overlaps with aboot addresses.\n");
1368 return -1;
1369 }
1370
Matthew Qinbb7923d2015-02-09 10:56:09 +08001371 /*
1372 * Update loading flow of bootimage to support compressed/uncompressed
1373 * bootimage on both 64bit and 32bit platform.
1374 * 1. Load bootimage from emmc partition onto DDR.
1375 * 2. Check if bootimage is gzip format. If yes, decompress compressed kernel
1376 * 3. Check kernel header and update kernel load addr for 64bit and 32bit
1377 * platform accordingly.
1378 * 4. Sanity Check on kernel_addr and ramdisk_addr and copy data.
1379 */
Mayank Grover351a75e2017-05-30 20:06:08 +05301380 if (partition_multislot_is_supported())
1381 {
1382 current_active_slot = partition_find_active_slot();
1383 dprintf(INFO, "Loading boot image (%d) active_slot(%s): start\n",
1384 imagesize_actual, SUFFIX_SLOT(current_active_slot));
1385 }
1386 else
1387 {
1388 dprintf(INFO, "Loading (%s) image (%d): start\n",
1389 (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual);
1390 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08001391 bs_set_timestamp(BS_KERNEL_LOAD_START);
1392
Gaurav Nebhwani43e2a462016-03-17 21:32:56 +05301393 if ((target_get_max_flash_size() - page_size) < imagesize_actual)
1394 {
1395 dprintf(CRITICAL, "booimage size is greater than DDR can hold\n");
1396 return -1;
1397 }
Kishor PK9e91b592017-05-24 12:14:55 +05301398 offset = page_size;
1399 /* Read image without signature and header*/
1400 if (mmc_read(ptn + offset, (void *)(image_addr + offset), imagesize_actual - page_size))
Matthew Qin49e51fa2015-02-09 10:40:45 +08001401 {
1402 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
1403 return -1;
1404 }
1405
Mayank Grover351a75e2017-05-30 20:06:08 +05301406 if (partition_multislot_is_supported())
1407 {
1408 dprintf(INFO, "Loading boot image (%d) active_slot(%s): done\n",
1409 imagesize_actual, SUFFIX_SLOT(current_active_slot));
1410 }
1411 else
1412 {
1413 dprintf(INFO, "Loading (%s) image (%d): done\n",
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001414 (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual);
1415
Mayank Grover351a75e2017-05-30 20:06:08 +05301416 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08001417 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
1418
1419 /* Authenticate Kernel */
1420 dprintf(INFO, "use_signed_kernel=%d, is_unlocked=%d, is_tampered=%d.\n",
1421 (int) target_use_signed_kernel(),
1422 device.is_unlocked,
1423 device.is_tampered);
1424
Channagoud Kadabi736c4962015-08-21 11:56:52 -07001425 /* Change the condition a little bit to include the test framework support.
1426 * We would never reach this point if device is in fastboot mode, even if we did
1427 * that means we are in test mode, so execute kernel authentication part for the
1428 * tests */
Parth Dixitcb0c6082015-12-30 15:01:13 +05301429 if((target_use_signed_kernel() && (!device.is_unlocked)) || is_test_mode_enabled())
Matthew Qin49e51fa2015-02-09 10:40:45 +08001430 {
1431 offset = imagesize_actual;
Vijay Kumar Pendoti9c002ad2016-03-09 13:52:45 +05301432 if (check_aboot_addr_range_overlap((uintptr_t)image_addr + offset, page_size))
Matthew Qin49e51fa2015-02-09 10:40:45 +08001433 {
1434 dprintf(CRITICAL, "Signature read buffer address overlaps with aboot addresses.\n");
1435 return -1;
1436 }
1437
1438 /* Read signature */
1439 if(mmc_read(ptn + offset, (void *)(image_addr + offset), page_size))
1440 {
1441 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
1442 return -1;
1443 }
1444
1445 verify_signed_bootimg((uint32_t)image_addr, imagesize_actual);
Channagoud Kadabi736c4962015-08-21 11:56:52 -07001446 /* The purpose of our test is done here */
Parth Dixitcb0c6082015-12-30 15:01:13 +05301447 if(is_test_mode_enabled() && auth_kernel_img)
Channagoud Kadabi736c4962015-08-21 11:56:52 -07001448 return 0;
Matthew Qin49e51fa2015-02-09 10:40:45 +08001449 } else {
1450 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
1451 #ifdef TZ_SAVE_KERNEL_HASH
1452 aboot_save_boot_hash_mmc((uint32_t) image_addr, imagesize_actual);
1453 #endif /* TZ_SAVE_KERNEL_HASH */
Amit Blay4aa292f2015-04-28 21:55:59 +03001454
1455#ifdef MDTP_SUPPORT
1456 {
1457 /* Verify MDTP lock.
1458 * For boot & recovery partitions, MDTP will use boot_verifier APIs,
1459 * since verification was skipped in aboot. The signature is not part of the loaded image.
1460 */
1461 mdtp_ext_partition_verification_t ext_partition;
1462 ext_partition.partition = boot_into_recovery ? MDTP_PARTITION_RECOVERY : MDTP_PARTITION_BOOT;
1463 ext_partition.integrity_state = MDTP_PARTITION_STATE_UNSET;
1464 ext_partition.page_size = page_size;
1465 ext_partition.image_addr = (uint32)image_addr;
1466 ext_partition.image_size = imagesize_actual;
1467 ext_partition.sig_avail = FALSE;
1468 mdtp_fwlock_verify_lock(&ext_partition);
1469 }
1470#endif /* MDTP_SUPPORT */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001471 }
1472
Sridhar Parasuram00bfedb2015-05-26 14:21:27 -07001473#if VERIFIED_BOOT
taozhang93088bd2016-11-16 15:50:46 +08001474 if((boot_verify_get_state() == ORANGE) && (!boot_into_ffbm))
Reut Zysmanba8a9d52016-02-18 11:44:04 +02001475 {
1476#if FBCON_DISPLAY_MSG
1477 display_bootverify_menu(DISPLAY_MENU_ORANGE);
1478 wait_for_users_action();
1479#else
1480 dprintf(CRITICAL,
1481 "Your device has been unlocked and can't be trusted.\nWait for 5 seconds before proceeding\n");
1482 mdelay(5000);
1483#endif
1484 }
1485#endif
1486
1487#if VERIFIED_BOOT
Mayank Grover912eaa62017-10-26 12:08:53 +05301488 if (VB_M <= target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +05301489 {
1490 /* set boot and system versions. */
1491 set_os_version((unsigned char *)image_addr);
1492 // send root of trust
1493 if(!send_rot_command((uint32_t)device.is_unlocked))
1494 ASSERT(0);
1495 }
Parth Dixitbc9b6492015-10-18 00:41:38 +05301496#endif
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001497 /*
Matthew Qinbb7923d2015-02-09 10:56:09 +08001498 * Check if the kernel image is a gzip package. If yes, need to decompress it.
1499 * If not, continue booting.
1500 */
1501 if (is_gzip_package((unsigned char *)(image_addr + page_size), hdr->kernel_size))
1502 {
1503 out_addr = (unsigned char *)(image_addr + imagesize_actual + page_size);
1504 out_avai_len = target_get_max_flash_size() - imagesize_actual - page_size;
Matthew Qin0b15b322015-05-19 05:20:54 -04001505 dprintf(INFO, "decompressing kernel image: start\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001506 rc = decompress((unsigned char *)(image_addr + page_size),
1507 hdr->kernel_size, out_addr, out_avai_len,
1508 &dtb_offset, &out_len);
1509 if (rc)
1510 {
Matthew Qin0b15b322015-05-19 05:20:54 -04001511 dprintf(CRITICAL, "decompressing kernel image failed!!!\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001512 ASSERT(0);
1513 }
1514
Matthew Qin0b15b322015-05-19 05:20:54 -04001515 dprintf(INFO, "decompressing kernel image: done\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001516 kptr = (struct kernel64_hdr *)out_addr;
1517 kernel_start_addr = out_addr;
1518 kernel_size = out_len;
1519 } else {
Ameya Thakur10a33452016-06-13 14:24:26 -07001520 dprintf(INFO, "Uncpmpressed kernel in use\n");
1521 if (!strncmp((char*)(image_addr + page_size),
1522 PATCHED_KERNEL_MAGIC,
1523 sizeof(PATCHED_KERNEL_MAGIC) - 1)) {
1524 dprintf(INFO, "Patched kernel detected\n");
1525 kptr = (struct kernel64_hdr *)(image_addr + page_size +
1526 PATCHED_KERNEL_HEADER_SIZE);
1527 //The size of the kernel is stored at start of kernel image + 16
1528 //The dtb would start just after the kernel
1529 dtb_offset = *((uint32_t*)((unsigned char*)
1530 (image_addr + page_size +
1531 sizeof(PATCHED_KERNEL_MAGIC) -
1532 1)));
1533 //The actual kernel starts after the 20 byte header.
1534 kernel_start_addr = (unsigned char*)(image_addr +
1535 page_size + PATCHED_KERNEL_HEADER_SIZE);
1536 kernel_size = hdr->kernel_size;
1537 patched_kernel_hdr_size = PATCHED_KERNEL_HEADER_SIZE;
1538 } else {
1539 dprintf(INFO, "Kernel image not patched..Unable to locate dt offset\n");
1540 kptr = (struct kernel64_hdr *)(image_addr + page_size);
1541 kernel_start_addr = (unsigned char *)(image_addr + page_size);
1542 kernel_size = hdr->kernel_size;
1543 }
Matthew Qinbb7923d2015-02-09 10:56:09 +08001544 }
1545
1546 /*
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001547 * Update the kernel/ramdisk/tags address if the boot image header
1548 * has default values, these default values come from mkbootimg when
1549 * the boot image is flashed using fastboot flash:raw
1550 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001551 update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr));
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001552
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001553 /* Get virtual addresses since the hdr saves physical addresses. */
1554 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
1555 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
1556 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001557
Matthew Qinbb7923d2015-02-09 10:56:09 +08001558 kernel_size = ROUND_TO_PAGE(kernel_size, page_mask);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001559 /* Check if the addresses in the header are valid. */
Matthew Qinbb7923d2015-02-09 10:56:09 +08001560 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_size) ||
Mayank Grovere559cec2017-10-17 15:12:03 +05301561 check_ddr_addr_range_bound(hdr->kernel_addr, kernel_size) ||
1562 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual) ||
1563 check_ddr_addr_range_bound(hdr->ramdisk_addr, ramdisk_actual))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001564 {
Mayank Grovere559cec2017-10-17 15:12:03 +05301565 dprintf(CRITICAL, "kernel/ramdisk addresses are not valid.\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001566 return -1;
1567 }
1568
1569#ifndef DEVICE_TREE
Mayank Grovere559cec2017-10-17 15:12:03 +05301570 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE) ||
1571 check_ddr_addr_range_bound(hdr->tags_addr, MAX_TAGS_SIZE))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001572 {
Mayank Grovere559cec2017-10-17 15:12:03 +05301573 dprintf(CRITICAL, "Tags addresses are not valid.\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001574 return -1;
1575 }
1576#endif
1577
Matthew Qin49e51fa2015-02-09 10:40:45 +08001578 /* Move kernel, ramdisk and device tree to correct address */
Matthew Qinbb7923d2015-02-09 10:56:09 +08001579 memmove((void*) hdr->kernel_addr, kernel_start_addr, kernel_size);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001580 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001581
Matthew Qin49e51fa2015-02-09 10:40:45 +08001582 #if DEVICE_TREE
Parth Dixit4097b622016-03-15 14:42:27 +05301583 if(dt_size) {
Matthew Qin49e51fa2015-02-09 10:40:45 +08001584 dt_table_offset = ((uint32_t)image_addr + page_size + kernel_actual + ramdisk_actual + second_actual);
1585 table = (struct dt_table*) dt_table_offset;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001586
Matthew Qin49e51fa2015-02-09 10:40:45 +08001587 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
1588 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1589 return -1;
1590 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001591
P.V. Phani Kumar82916762016-03-09 09:20:19 +05301592 /* Its Error if, dt_hdr_size (table->num_entries * dt_entry size + Dev_Tree Header)
1593 goes beyound hdr->dt_size*/
Parth Dixit4097b622016-03-15 14:42:27 +05301594 if (dt_hdr_size > ROUND_TO_PAGE(dt_size,hdr->page_size)) {
P.V. Phani Kumar82916762016-03-09 09:20:19 +05301595 dprintf(CRITICAL, "ERROR: Invalid Device Tree size \n");
1596 return -1;
1597 }
1598
Matthew Qin49e51fa2015-02-09 10:40:45 +08001599 /* Find index of device tree within device tree table */
1600 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
1601 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1602 return -1;
1603 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001604
P.V. Phani Kumar3c333202016-03-09 11:54:37 +05301605 if(dt_entry.offset > (UINT_MAX - dt_entry.size)) {
1606 dprintf(CRITICAL, "ERROR: Device tree contents are Invalid\n");
1607 return -1;
1608 }
1609
1610 /* Ensure we are not overshooting dt_size with the dt_entry selected */
Parth Dixit4097b622016-03-15 14:42:27 +05301611 if ((dt_entry.offset + dt_entry.size) > dt_size) {
P.V. Phani Kumar3c333202016-03-09 11:54:37 +05301612 dprintf(CRITICAL, "ERROR: Device tree contents are Invalid\n");
1613 return -1;
1614 }
1615
Matthew Qin271927e2015-03-31 22:07:07 -04001616 if (is_gzip_package((unsigned char *)dt_table_offset + dt_entry.offset, dt_entry.size))
1617 {
1618 unsigned int compressed_size = 0;
1619 out_addr += out_len;
1620 out_avai_len -= out_len;
Matthew Qin0b15b322015-05-19 05:20:54 -04001621 dprintf(INFO, "decompressing dtb: start\n");
Matthew Qin271927e2015-03-31 22:07:07 -04001622 rc = decompress((unsigned char *)dt_table_offset + dt_entry.offset,
1623 dt_entry.size, out_addr, out_avai_len,
1624 &compressed_size, &dtb_size);
1625 if (rc)
1626 {
Matthew Qin0b15b322015-05-19 05:20:54 -04001627 dprintf(CRITICAL, "decompressing dtb failed!!!\n");
Matthew Qin271927e2015-03-31 22:07:07 -04001628 ASSERT(0);
1629 }
1630
Matthew Qin0b15b322015-05-19 05:20:54 -04001631 dprintf(INFO, "decompressing dtb: done\n");
Matthew Qin271927e2015-03-31 22:07:07 -04001632 best_match_dt_addr = out_addr;
1633 } else {
1634 best_match_dt_addr = (unsigned char *)dt_table_offset + dt_entry.offset;
1635 dtb_size = dt_entry.size;
1636 }
1637
Matthew Qin49e51fa2015-02-09 10:40:45 +08001638 /* Validate and Read device device tree in the tags_addr */
Mayank Grovere559cec2017-10-17 15:12:03 +05301639 if (check_aboot_addr_range_overlap(hdr->tags_addr, dtb_size) ||
1640 check_ddr_addr_range_bound(hdr->tags_addr, dtb_size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001641 {
Mayank Grovere559cec2017-10-17 15:12:03 +05301642 dprintf(CRITICAL, "Device tree addresses are not valid\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001643 return -1;
1644 }
Shashank Mittal162244e2011-08-08 19:01:25 -07001645
Matthew Qin271927e2015-03-31 22:07:07 -04001646 memmove((void *)hdr->tags_addr, (char *)best_match_dt_addr, dtb_size);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001647 } else {
1648 /* Validate the tags_addr */
Mayank Grovere559cec2017-10-17 15:12:03 +05301649 if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual) ||
1650 check_ddr_addr_range_bound(hdr->tags_addr, kernel_actual))
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001651 {
Mayank Grovere559cec2017-10-17 15:12:03 +05301652 dprintf(CRITICAL, "Device tree addresses are not valid.\n");
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001653 return -1;
1654 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08001655 /*
1656 * If appended dev tree is found, update the atags with
1657 * memory address to the DTB appended location on RAM.
1658 * Else update with the atags address in the kernel header
1659 */
1660 void *dtb;
Ameya Thakur10a33452016-06-13 14:24:26 -07001661 dtb = dev_tree_appended(
1662 (void*)(image_addr + page_size +
1663 patched_kernel_hdr_size),
1664 hdr->kernel_size, dtb_offset,
1665 (void *)hdr->tags_addr);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001666 if (!dtb) {
1667 dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001668 return -1;
1669 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07001670 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08001671 #endif
Shashank Mittal23b8f422010-04-16 19:27:21 -07001672
Stanimir Varbanov69ec5462013-07-18 18:17:42 +03001673 if (boot_into_recovery && !device.is_unlocked && !device.is_tampered)
1674 target_load_ssd_keystore();
1675
Shashank Mittal23b8f422010-04-16 19:27:21 -07001676unified_boot:
Shashank Mittal23b8f422010-04-16 19:27:21 -07001677
Dima Zavin77e41f32013-03-06 16:10:43 -08001678 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07001679 (const char *)hdr->cmdline, board_machtype(),
Shashank Mittal23b8f422010-04-16 19:27:21 -07001680 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
1681
1682 return 0;
1683}
1684
Dima Zavin214cc642009-01-26 11:16:21 -08001685int boot_linux_from_flash(void)
1686{
1687 struct boot_img_hdr *hdr = (void*) buf;
Dima Zavin214cc642009-01-26 11:16:21 -08001688 struct ptentry *ptn;
1689 struct ptable *ptable;
1690 unsigned offset = 0;
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001691
Shashank Mittalcd98d472011-08-02 14:29:24 -07001692 unsigned char *image_addr = 0;
1693 unsigned kernel_actual;
1694 unsigned ramdisk_actual;
1695 unsigned imagesize_actual;
vijay kumar8f53e362015-11-24 13:38:11 +05301696 unsigned second_actual = 0;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001697
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001698#if DEVICE_TREE
1699 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -07001700 struct dt_entry dt_entry;
vijay kumar8f53e362015-11-24 13:38:11 +05301701 unsigned dt_table_offset;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001702 uint32_t dt_actual;
Deepa Dinamani19648b42013-09-05 17:05:55 -07001703 uint32_t dt_hdr_size;
vijay kumar8f53e362015-11-24 13:38:11 +05301704 unsigned int dtb_size = 0;
1705 unsigned char *best_match_dt_addr = NULL;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001706#endif
1707
David Ng183a7422009-12-07 14:55:21 -08001708 if (target_is_emmc_boot()) {
1709 hdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
1710 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1711 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
1712 return -1;
1713 }
1714 goto continue_boot;
1715 }
1716
Dima Zavin214cc642009-01-26 11:16:21 -08001717 ptable = flash_get_ptable();
1718 if (ptable == NULL) {
1719 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1720 return -1;
1721 }
1722
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001723 if(!boot_into_recovery)
1724 {
1725 ptn = ptable_find(ptable, "boot");
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001726
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001727 if (ptn == NULL) {
1728 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1729 return -1;
1730 }
1731 }
1732 else
1733 {
1734 ptn = ptable_find(ptable, "recovery");
1735 if (ptn == NULL) {
1736 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
1737 return -1;
1738 }
Dima Zavin214cc642009-01-26 11:16:21 -08001739 }
1740
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05301741 /* Read boot.img header from flash */
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001742 if (flash_read(ptn, offset, buf, page_size)) {
Dima Zavin214cc642009-01-26 11:16:21 -08001743 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
1744 return -1;
1745 }
Dima Zavin214cc642009-01-26 11:16:21 -08001746
1747 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001748 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Dima Zavin214cc642009-01-26 11:16:21 -08001749 return -1;
1750 }
1751
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001752 if (hdr->page_size != page_size) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001753 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 -08001754 return -1;
1755 }
1756
Kishor PK9e91b592017-05-24 12:14:55 +05301757 image_addr = (unsigned char *)target_get_scratch_address();
1758 memcpy(image_addr, (void *)buf, page_size);
vijay kumar287bb542015-09-29 13:01:52 +05301759
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001760 /*
1761 * Update the kernel/ramdisk/tags address if the boot image header
1762 * has default values, these default values come from mkbootimg when
1763 * the boot image is flashed using fastboot flash:raw
1764 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001765 update_ker_tags_rdisk_addr(hdr, false);
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001766
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001767 /* Get virtual addresses since the hdr saves physical addresses. */
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001768 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
1769 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
1770 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
1771
1772 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1773 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Mayank Grover032736f2017-07-14 20:34:51 +05301774 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001775
Kishor PK9e91b592017-05-24 12:14:55 +05301776 /* ensure commandline is terminated */
1777 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
1778
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001779 /* Check if the addresses in the header are valid. */
1780 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
Mayank Grovere559cec2017-10-17 15:12:03 +05301781 check_ddr_addr_range_bound(hdr->kernel_addr, kernel_actual) ||
1782 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual) ||
1783 check_ddr_addr_range_bound(hdr->ramdisk_addr, ramdisk_actual))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001784 {
Mayank Grovere559cec2017-10-17 15:12:03 +05301785 dprintf(CRITICAL, "kernel/ramdisk addresses are not valid.\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001786 return -1;
1787 }
1788
1789#ifndef DEVICE_TREE
Kishor PKd8ddcad2017-07-27 13:53:57 +05301790 if (UINT_MAX < ((uint64_t)kernel_actual + (uint64_t)ramdisk_actual+ (uint64_t)second_actual + page_size)) {
Mayank Grover032736f2017-07-14 20:34:51 +05301791 dprintf(CRITICAL, "Integer overflow detected in bootimage header fields\n");
1792 return -1;
1793 }
Kishor PKd8ddcad2017-07-27 13:53:57 +05301794 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + second_actual);
Mayank Grover032736f2017-07-14 20:34:51 +05301795
Mayank Grovere559cec2017-10-17 15:12:03 +05301796 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE) ||
1797 check_ddr_addr_range_bound(hdr->tags_addr, MAX_TAGS_SIZE))
Vijay Kumar Pendotid3ed20e2016-09-20 00:34:46 +05301798 {
Mayank Grovere559cec2017-10-17 15:12:03 +05301799 dprintf(CRITICAL, "Tags addresses are not valid.\n");
Vijay Kumar Pendotid3ed20e2016-09-20 00:34:46 +05301800 return -1;
1801 }
1802#else
1803
1804#ifndef OSVERSION_IN_BOOTIMAGE
1805 dt_size = hdr->dt_size;
1806#endif
Mayank Grover032736f2017-07-14 20:34:51 +05301807 dt_actual = ROUND_TO_PAGE(dt_size, page_mask);
Kishor PKd8ddcad2017-07-27 13:53:57 +05301808 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 +05301809 dprintf(CRITICAL, "Integer overflow detected in bootimage header fields\n");
1810 return -1;
1811 }
1812
Kishor PKd8ddcad2017-07-27 13:53:57 +05301813 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + second_actual + dt_actual);
Mayank Grover032736f2017-07-14 20:34:51 +05301814
Mayank Grovere559cec2017-10-17 15:12:03 +05301815 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_size) ||
1816 check_ddr_addr_range_bound(hdr->tags_addr, dt_size))
Mayank Grover032736f2017-07-14 20:34:51 +05301817 {
Mayank Grovere559cec2017-10-17 15:12:03 +05301818 dprintf(CRITICAL, "Device tree addresses are not valid.\n");
Mayank Grover032736f2017-07-14 20:34:51 +05301819 return -1;
1820 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001821#endif
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001822
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05301823 /* Read full boot.img from flash */
1824 dprintf(INFO, "Loading (%s) image (%d): start\n",
1825 (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual);
1826 bs_set_timestamp(BS_KERNEL_LOAD_START);
1827
1828 if (UINT_MAX - page_size < imagesize_actual)
1829 {
1830 dprintf(CRITICAL,"Integer overflow detected in bootimage header fields %u %s\n", __LINE__,__func__);
1831 return -1;
1832 }
1833
1834 /*Check the availability of RAM before reading boot image + max signature length from flash*/
1835 if (target_get_max_flash_size() < (imagesize_actual + page_size))
1836 {
1837 dprintf(CRITICAL, "bootimage size is greater than DDR can hold\n");
1838 return -1;
1839 }
1840
1841 offset = page_size;
1842 /* Read image without signature and header */
1843 if (flash_read(ptn, offset, (void *)(image_addr + offset), imagesize_actual - page_size))
1844 {
1845 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
1846 return -1;
1847 }
1848
1849 dprintf(INFO, "Loading (%s) image (%d): done\n",
1850 (!boot_into_recovery ? "boot" : "recovery"), imagesize_actual);
1851 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
1852
Shashank Mittalcd98d472011-08-02 14:29:24 -07001853 /* Authenticate Kernel */
Deepa Dinamani23b60d42013-06-24 18:10:52 -07001854 if(target_use_signed_kernel() && (!device.is_unlocked))
Shashank Mittalcd98d472011-08-02 14:29:24 -07001855 {
Shashank Mittalcd98d472011-08-02 14:29:24 -07001856 offset = imagesize_actual;
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05301857
Shashank Mittalcd98d472011-08-02 14:29:24 -07001858 /* Read signature */
1859 if (flash_read(ptn, offset, (void *)(image_addr + offset), page_size))
1860 {
1861 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001862 return -1;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001863 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07001864
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301865 verify_signed_bootimg((uint32_t)image_addr, imagesize_actual);
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05301866 }
1867 offset = page_size;
1868 if(hdr->second_size != 0) {
1869 if (UINT_MAX - offset < second_actual)
1870 {
1871 dprintf(CRITICAL, "ERROR: Integer overflow in boot image header %s\t%d\n",__func__,__LINE__);
1872 return -1;
vijay kumar8f53e362015-11-24 13:38:11 +05301873 }
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05301874 offset += second_actual;
1875 /* Second image loading not implemented. */
1876 ASSERT(0);
1877 }
Shashank Mittal162244e2011-08-08 19:01:25 -07001878
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05301879 /* Move kernel and ramdisk to correct address */
1880 memmove((void*) hdr->kernel_addr, (char*) (image_addr + page_size), hdr->kernel_size);
1881 memmove((void*) hdr->ramdisk_addr, (char*) (image_addr + page_size + kernel_actual), hdr->ramdisk_size);
1882
1883#if DEVICE_TREE
1884 if(dt_size != 0) {
1885
1886 dt_table_offset = ((uint32_t)image_addr + page_size + kernel_actual + ramdisk_actual + second_actual);
1887
1888 table = (struct dt_table*) dt_table_offset;
1889
1890 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
1891 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1892 return -1;
1893 }
1894
1895 /* Its Error if, dt_hdr_size (table->num_entries * dt_entry size + Dev_Tree Header)
1896 goes beyound hdr->dt_size*/
1897 if (dt_hdr_size > ROUND_TO_PAGE(dt_size,hdr->page_size)) {
1898 dprintf(CRITICAL, "ERROR: Invalid Device Tree size \n");
1899 return -1;
1900 }
1901
1902 /* Find index of device tree within device tree table */
1903 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
1904 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1905 return -1;
1906 }
1907
1908 /* Validate and Read device device tree in the "tags_add */
1909 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size) ||
1910 check_ddr_addr_range_bound(hdr->tags_addr, dt_entry.size))
1911 {
1912 dprintf(CRITICAL, "Device tree addresses are not valid.\n");
1913 return -1;
1914 }
1915
1916 if(dt_entry.offset > (UINT_MAX - dt_entry.size)) {
1917 dprintf(CRITICAL, "ERROR: Device tree contents are Invalid\n");
1918 return -1;
1919 }
1920
1921 /* Ensure we are not overshooting dt_size with the dt_entry selected */
1922 if ((dt_entry.offset + dt_entry.size) > dt_size) {
1923 dprintf(CRITICAL, "ERROR: Device tree contents are Invalid\n");
1924 return -1;
1925 }
1926
1927 best_match_dt_addr = (unsigned char *)table + dt_entry.offset;
1928 dtb_size = dt_entry.size;
1929 memmove((void *)hdr->tags_addr, (char *)best_match_dt_addr, dtb_size);
1930 }
1931#endif
1932 if(target_use_signed_kernel() && (!device.is_unlocked))
1933 {
Shashank Mittal162244e2011-08-08 19:01:25 -07001934 /* Make sure everything from scratch address is read before next step!*/
Shashank Mittala0032282011-08-26 14:50:11 -07001935 if(device.is_tampered)
Shashank Mittal162244e2011-08-08 19:01:25 -07001936 {
1937 write_device_info_flash(&device);
1938 }
Channagoud Kadabi5c86fe32012-02-16 10:58:48 +05301939#if USE_PCOM_SECBOOT
1940 set_tamper_flag(device.is_tampered);
1941#endif
Shashank Mittalcd98d472011-08-02 14:29:24 -07001942 }
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001943
David Ng183a7422009-12-07 14:55:21 -08001944continue_boot:
Dima Zavin214cc642009-01-26 11:16:21 -08001945
Dima Zavin214cc642009-01-26 11:16:21 -08001946 /* TODO: create/pass atags to kernel */
1947
Ajay Dudanie28a6072011-07-01 13:59:46 -07001948 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07001949 (const char *)hdr->cmdline, board_machtype(),
Dima Zavin214cc642009-01-26 11:16:21 -08001950 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
1951
1952 return 0;
1953}
Brian Swetland9c4c0752009-01-25 16:23:50 -08001954
Shashank Mittal162244e2011-08-08 19:01:25 -07001955void write_device_info_mmc(device_info *dev)
1956{
Shashank Mittal162244e2011-08-08 19:01:25 -07001957 unsigned long long ptn = 0;
1958 unsigned long long size;
1959 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001960 uint8_t lun = 0;
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001961 uint32_t ret = 0;
Mayank Groverddd348d2018-01-23 14:07:09 +05301962 uint32_t device_info_sz = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001963
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001964 if (devinfo_present)
1965 index = partition_get_index("devinfo");
1966 else
1967 index = partition_get_index("aboot");
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001968
Shashank Mittal162244e2011-08-08 19:01:25 -07001969 ptn = partition_get_offset(index);
1970 if(ptn == 0)
1971 {
1972 return;
1973 }
1974
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001975 lun = partition_get_lun(index);
1976 mmc_set_lun(lun);
1977
Shashank Mittal162244e2011-08-08 19:01:25 -07001978 size = partition_get_size(index);
1979
Mayank Groverddd348d2018-01-23 14:07:09 +05301980 device_info_sz = ROUND_TO_PAGE(sizeof(struct device_info),
1981 mmc_blocksize_mask);
1982 if (device_info_sz == UINT_MAX)
1983 {
1984 dprintf(CRITICAL, "ERROR: Incorrect blocksize of card\n");
1985 return;
1986 }
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001987
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001988 if (devinfo_present)
Mayank Groverddd348d2018-01-23 14:07:09 +05301989 ret = mmc_write(ptn, device_info_sz, (void *)info_buf);
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001990 else
Mayank Groverddd348d2018-01-23 14:07:09 +05301991 ret = mmc_write((ptn + size - device_info_sz), device_info_sz, (void *)info_buf);
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001992 if (ret)
Shashank Mittal162244e2011-08-08 19:01:25 -07001993 {
1994 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
Sridhar Parasurame1a97922015-07-27 15:31:22 -07001995 ASSERT(0);
Shashank Mittal162244e2011-08-08 19:01:25 -07001996 }
1997}
1998
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001999void read_device_info_mmc(struct device_info *info)
Shashank Mittal162244e2011-08-08 19:01:25 -07002000{
Shashank Mittal162244e2011-08-08 19:01:25 -07002001 unsigned long long ptn = 0;
2002 unsigned long long size;
2003 int index = INVALID_PTN;
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002004 uint32_t ret = 0;
Mayank Groverddd348d2018-01-23 14:07:09 +05302005 uint32_t device_info_sz = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07002006
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002007 if ((index = partition_get_index("devinfo")) < 0)
2008 {
2009 devinfo_present = false;
2010 index = partition_get_index("aboot");
2011 }
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07002012
Shashank Mittal162244e2011-08-08 19:01:25 -07002013 ptn = partition_get_offset(index);
2014 if(ptn == 0)
2015 {
2016 return;
2017 }
2018
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07002019 mmc_set_lun(partition_get_lun(index));
2020
Shashank Mittal162244e2011-08-08 19:01:25 -07002021 size = partition_get_size(index);
2022
Mayank Groverddd348d2018-01-23 14:07:09 +05302023 device_info_sz = ROUND_TO_PAGE(sizeof(struct device_info),
2024 mmc_blocksize_mask);
2025 if (device_info_sz == UINT_MAX)
2026 {
2027 dprintf(CRITICAL, "ERROR: Incorrect blocksize of card\n");
2028 return;
2029 }
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07002030
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002031 if (devinfo_present)
Mayank Groverddd348d2018-01-23 14:07:09 +05302032 ret = mmc_read(ptn, (void *)info_buf, device_info_sz);
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002033 else
Mayank Groverddd348d2018-01-23 14:07:09 +05302034 ret = mmc_read((ptn + size - device_info_sz), (void *)info_buf, device_info_sz);
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002035 if (ret)
Shashank Mittal162244e2011-08-08 19:01:25 -07002036 {
2037 dprintf(CRITICAL, "ERROR: Cannot read device info\n");
Sridhar Parasurame1a97922015-07-27 15:31:22 -07002038 ASSERT(0);
Shashank Mittal162244e2011-08-08 19:01:25 -07002039 }
Shashank Mittal162244e2011-08-08 19:01:25 -07002040}
2041
2042void write_device_info_flash(device_info *dev)
2043{
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002044 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
Shashank Mittal162244e2011-08-08 19:01:25 -07002045 struct ptentry *ptn;
2046 struct ptable *ptable;
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002047 if(info == NULL)
2048 {
2049 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
2050 ASSERT(0);
2051 }
2052 info_buf = info;
Shashank Mittal162244e2011-08-08 19:01:25 -07002053 ptable = flash_get_ptable();
2054 if (ptable == NULL)
2055 {
2056 dprintf(CRITICAL, "ERROR: Partition table not found\n");
2057 return;
2058 }
2059
2060 ptn = ptable_find(ptable, "devinfo");
2061 if (ptn == NULL)
2062 {
Smita Ghosh670c8b82015-05-07 09:30:03 -07002063 dprintf(CRITICAL, "ERROR: No devinfo partition found\n");
Shashank Mittal162244e2011-08-08 19:01:25 -07002064 return;
2065 }
2066
Mayank Groverdedbc892017-10-24 13:41:34 +05302067 memset(info, 0, BOOT_IMG_MAX_PAGE_SIZE);
Shashank Mittal162244e2011-08-08 19:01:25 -07002068 memcpy(info, dev, sizeof(device_info));
2069
2070 if (flash_write(ptn, 0, (void *)info_buf, page_size))
2071 {
2072 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
2073 return;
2074 }
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002075 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07002076}
2077
vijay kumarc65876c2015-04-24 13:29:16 +05302078static int read_allow_oem_unlock(device_info *dev)
2079{
vijay kumarc65876c2015-04-24 13:29:16 +05302080 unsigned offset;
2081 int index;
2082 unsigned long long ptn;
2083 unsigned long long ptn_size;
2084 unsigned blocksize = mmc_get_device_blocksize();
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07002085 STACKBUF_DMA_ALIGN(buf, blocksize);
vijay kumarc65876c2015-04-24 13:29:16 +05302086
vijay kumarca2e6812015-07-08 20:28:25 +05302087 index = partition_get_index(frp_ptns[0]);
vijay kumarc65876c2015-04-24 13:29:16 +05302088 if (index == INVALID_PTN)
2089 {
vijay kumarca2e6812015-07-08 20:28:25 +05302090 index = partition_get_index(frp_ptns[1]);
2091 if (index == INVALID_PTN)
2092 {
2093 dprintf(CRITICAL, "Neither '%s' nor '%s' partition found\n", frp_ptns[0],frp_ptns[1]);
2094 return -1;
2095 }
vijay kumarc65876c2015-04-24 13:29:16 +05302096 }
2097
2098 ptn = partition_get_offset(index);
2099 ptn_size = partition_get_size(index);
2100 offset = ptn_size - blocksize;
2101
Mayank Grover48860402016-11-29 12:34:53 +05302102 /* Set Lun for partition */
2103 mmc_set_lun(partition_get_lun(index));
2104
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07002105 if (mmc_read(ptn + offset, (void *)buf, blocksize))
vijay kumarc65876c2015-04-24 13:29:16 +05302106 {
2107 dprintf(CRITICAL, "Reading MMC failed\n");
2108 return -1;
2109 }
2110
2111 /*is_allow_unlock is a bool value stored at the LSB of last byte*/
2112 is_allow_unlock = buf[blocksize-1] & 0x01;
2113 return 0;
2114}
2115
2116static int write_allow_oem_unlock(bool allow_unlock)
2117{
vijay kumarc65876c2015-04-24 13:29:16 +05302118 unsigned offset;
vijay kumarc65876c2015-04-24 13:29:16 +05302119 int index;
2120 unsigned long long ptn;
2121 unsigned long long ptn_size;
2122 unsigned blocksize = mmc_get_device_blocksize();
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07002123 STACKBUF_DMA_ALIGN(buf, blocksize);
vijay kumarc65876c2015-04-24 13:29:16 +05302124
vijay kumarca2e6812015-07-08 20:28:25 +05302125 index = partition_get_index(frp_ptns[0]);
vijay kumarc65876c2015-04-24 13:29:16 +05302126 if (index == INVALID_PTN)
2127 {
vijay kumarca2e6812015-07-08 20:28:25 +05302128 index = partition_get_index(frp_ptns[1]);
2129 if (index == INVALID_PTN)
2130 {
2131 dprintf(CRITICAL, "Neither '%s' nor '%s' partition found\n", frp_ptns[0],frp_ptns[1]);
2132 return -1;
2133 }
vijay kumarc65876c2015-04-24 13:29:16 +05302134 }
2135
2136 ptn = partition_get_offset(index);
2137 ptn_size = partition_get_size(index);
2138 offset = ptn_size - blocksize;
Mayank Grover48860402016-11-29 12:34:53 +05302139 mmc_set_lun(partition_get_lun(index));
vijay kumarc65876c2015-04-24 13:29:16 +05302140
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07002141 if (mmc_read(ptn + offset, (void *)buf, blocksize))
vijay kumarc65876c2015-04-24 13:29:16 +05302142 {
2143 dprintf(CRITICAL, "Reading MMC failed\n");
2144 return -1;
2145 }
2146
2147 /*is_allow_unlock is a bool value stored at the LSB of last byte*/
2148 buf[blocksize-1] = allow_unlock;
2149 if (mmc_write(ptn + offset, blocksize, buf))
2150 {
2151 dprintf(CRITICAL, "Writing MMC failed\n");
2152 return -1;
2153 }
2154
2155 return 0;
2156}
2157
Shashank Mittal162244e2011-08-08 19:01:25 -07002158void read_device_info_flash(device_info *dev)
2159{
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002160 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
Shashank Mittal162244e2011-08-08 19:01:25 -07002161 struct ptentry *ptn;
2162 struct ptable *ptable;
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002163 if(info == NULL)
2164 {
2165 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
2166 ASSERT(0);
2167 }
2168 info_buf = info;
Shashank Mittal162244e2011-08-08 19:01:25 -07002169 ptable = flash_get_ptable();
2170 if (ptable == NULL)
2171 {
2172 dprintf(CRITICAL, "ERROR: Partition table not found\n");
2173 return;
2174 }
2175
2176 ptn = ptable_find(ptable, "devinfo");
2177 if (ptn == NULL)
2178 {
Smita Ghosh670c8b82015-05-07 09:30:03 -07002179 dprintf(CRITICAL, "ERROR: No devinfo partition found\n");
Shashank Mittal162244e2011-08-08 19:01:25 -07002180 return;
2181 }
2182
2183 if (flash_read(ptn, 0, (void *)info_buf, page_size))
2184 {
2185 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
2186 return;
2187 }
2188
2189 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
2190 {
Shashank Mittal162244e2011-08-08 19:01:25 -07002191 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
2192 info->is_unlocked = 0;
Shashank Mittala0032282011-08-26 14:50:11 -07002193 info->is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07002194 write_device_info_flash(info);
2195 }
2196 memcpy(dev, info, sizeof(device_info));
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002197 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07002198}
2199
2200void write_device_info(device_info *dev)
2201{
2202 if(target_is_emmc_boot())
2203 {
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002204 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
2205 if(info == NULL)
2206 {
2207 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
2208 ASSERT(0);
2209 }
2210 info_buf = info;
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002211 memcpy(info, dev, sizeof(struct device_info));
2212
2213#if USE_RPMB_FOR_DEVINFO
Mayank Grover912eaa62017-10-26 12:08:53 +05302214 if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05302215 is_secure_boot_enable()) {
2216 if((write_device_info_rpmb((void*) info, PAGE_SIZE)) < 0)
2217 ASSERT(0);
Sridhar Parasurame1a97922015-07-27 15:31:22 -07002218 }
Channagoud Kadabic80cb492015-04-28 16:08:28 -07002219 else
2220 write_device_info_mmc(info);
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002221#else
2222 write_device_info_mmc(info);
2223#endif
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002224 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07002225 }
2226 else
2227 {
2228 write_device_info_flash(dev);
2229 }
2230}
2231
2232void read_device_info(device_info *dev)
2233{
2234 if(target_is_emmc_boot())
2235 {
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002236 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
2237 if(info == NULL)
2238 {
2239 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
2240 ASSERT(0);
2241 }
2242 info_buf = info;
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002243
2244#if USE_RPMB_FOR_DEVINFO
Mayank Grover912eaa62017-10-26 12:08:53 +05302245 if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05302246 is_secure_boot_enable()) {
2247 if((read_device_info_rpmb((void*) info, PAGE_SIZE)) < 0)
2248 ASSERT(0);
Sridhar Parasurame1a97922015-07-27 15:31:22 -07002249 }
Channagoud Kadabic80cb492015-04-28 16:08:28 -07002250 else
2251 read_device_info_mmc(info);
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002252#else
2253 read_device_info_mmc(info);
2254#endif
2255
2256 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
2257 {
2258 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
lijuang511a2b52015-08-14 20:50:51 +08002259 if (is_secure_boot_enable()) {
Channagoud Kadabi05f78ba2015-07-06 11:58:14 -07002260 info->is_unlocked = 0;
Mayank Grover889be1b2017-09-12 20:12:23 +05302261#if VERIFIED_BOOT
Mayank Grover912eaa62017-10-26 12:08:53 +05302262 if (VB_M <= target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +05302263 info->is_unlock_critical = 0;
Parth Dixitddbc7352015-10-18 03:13:31 +05302264#endif
lijuang511a2b52015-08-14 20:50:51 +08002265 } else {
Channagoud Kadabi2fda4092015-07-07 13:34:11 -07002266 info->is_unlocked = 1;
Mayank Grover889be1b2017-09-12 20:12:23 +05302267#if VERIFIED_BOOT
Mayank Grover912eaa62017-10-26 12:08:53 +05302268 if (VB_M <= target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +05302269 info->is_unlock_critical = 1;
Parth Dixitddbc7352015-10-18 03:13:31 +05302270#endif
lijuang511a2b52015-08-14 20:50:51 +08002271 }
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002272 info->is_tampered = 0;
2273 info->charger_screen_enabled = 0;
Mayank Grover889be1b2017-09-12 20:12:23 +05302274#if VERIFIED_BOOT
Mayank Grover912eaa62017-10-26 12:08:53 +05302275 if (VB_M <= target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +05302276 info->verity_mode = 1; //enforcing by default
Parth Dixitddbc7352015-10-18 03:13:31 +05302277#endif
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002278 write_device_info(info);
2279 }
2280 memcpy(dev, info, sizeof(device_info));
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002281 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07002282 }
2283 else
2284 {
2285 read_device_info_flash(dev);
2286 }
2287}
2288
2289void reset_device_info()
2290{
2291 dprintf(ALWAYS, "reset_device_info called.");
Shashank Mittala0032282011-08-26 14:50:11 -07002292 device.is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07002293 write_device_info(&device);
2294}
2295
2296void set_device_root()
2297{
2298 dprintf(ALWAYS, "set_device_root called.");
Shashank Mittala0032282011-08-26 14:50:11 -07002299 device.is_tampered = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -07002300 write_device_info(&device);
2301}
2302
lijuang4ece1e72015-08-14 21:02:36 +08002303/* set device unlock value
2304 * Must check FRP before call this function
2305 * Need to wipe data when unlock status changed
2306 * type 0: oem unlock
2307 * type 1: unlock critical
2308 * status 0: unlock as false
2309 * status 1: lock as true
2310 */
2311void set_device_unlock_value(int type, bool status)
lijuang21f12f52015-08-22 16:22:19 +08002312{
lijuang4ece1e72015-08-14 21:02:36 +08002313 if (type == UNLOCK)
2314 device.is_unlocked = status;
Mayank Grover889be1b2017-09-12 20:12:23 +05302315#if VERIFIED_BOOT
Mayank Grover912eaa62017-10-26 12:08:53 +05302316 else if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05302317 type == UNLOCK_CRITICAL)
2318 device.is_unlock_critical = status;
Parth Dixitddbc7352015-10-18 03:13:31 +05302319#endif
lijuang4ece1e72015-08-14 21:02:36 +08002320 write_device_info(&device);
2321}
2322
2323static void set_device_unlock(int type, bool status)
2324{
2325 int is_unlocked = -1;
2326 char response[MAX_RSP_SIZE];
2327
2328 /* check device unlock status if it is as expected */
2329 if (type == UNLOCK)
2330 is_unlocked = device.is_unlocked;
Mayank Grover889be1b2017-09-12 20:12:23 +05302331#if VERIFIED_BOOT
Mayank Grover912eaa62017-10-26 12:08:53 +05302332 if(VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05302333 type == UNLOCK_CRITICAL)
2334 {
2335 is_unlocked = device.is_unlock_critical;
2336 }
Parth Dixitddbc7352015-10-18 03:13:31 +05302337#endif
lijuang4ece1e72015-08-14 21:02:36 +08002338 if (is_unlocked == status) {
2339 snprintf(response, sizeof(response), "\tDevice already : %s", (status ? "unlocked!" : "locked!"));
2340 fastboot_info(response);
2341 fastboot_okay("");
2342 return;
2343 }
2344
2345 /* status is true, it means to unlock device */
2346 if (status) {
lijuang21f12f52015-08-22 16:22:19 +08002347 if(!is_allow_unlock) {
2348 fastboot_fail("oem unlock is not allowed");
2349 return;
2350 }
2351
lijuang4ece1e72015-08-14 21:02:36 +08002352#if FBCON_DISPLAY_MSG
lijuang9a7d3b92015-11-30 14:41:24 +08002353 display_unlock_menu(type);
lijuang4ece1e72015-08-14 21:02:36 +08002354 fastboot_okay("");
2355 return;
2356#else
2357 if (type == UNLOCK) {
2358 fastboot_fail("Need wipe userdata. Do 'fastboot oem unlock-go'");
2359 return;
2360 }
2361#endif
lijuang21f12f52015-08-22 16:22:19 +08002362 }
lijuang4ece1e72015-08-14 21:02:36 +08002363
2364 set_device_unlock_value(type, status);
2365
2366 /* wipe data */
2367 struct recovery_message msg;
Kishor PK60a68212017-05-08 16:55:57 +05302368 memset(&msg, 0, sizeof(msg));
lijuang4ece1e72015-08-14 21:02:36 +08002369 snprintf(msg.recovery, sizeof(msg.recovery), "recovery\n--wipe_data");
2370 write_misc(0, &msg, sizeof(msg));
2371
2372 fastboot_okay("");
2373 reboot_device(RECOVERY_MODE);
lijuang21f12f52015-08-22 16:22:19 +08002374}
2375
lijuang511a2b52015-08-14 20:50:51 +08002376static bool critical_flash_allowed(const char * entry)
2377{
2378 uint32_t i = 0;
2379 if (entry == NULL)
2380 return false;
2381
2382 for (i = 0; i < ARRAY_SIZE(critical_flash_allowed_ptn); i++) {
2383 if(!strcmp(entry, critical_flash_allowed_ptn[i]))
2384 return true;
2385 }
2386 return false;
Matthew Qin271927e2015-03-31 22:07:07 -04002387}
2388
2389#if DEVICE_TREE
Amol Jadicb524072012-08-09 16:40:18 -07002390int copy_dtb(uint8_t *boot_image_start, unsigned int scratch_offset)
2391{
2392 uint32 dt_image_offset = 0;
Amol Jadicb524072012-08-09 16:40:18 -07002393 uint32_t n;
Amol Jadicb524072012-08-09 16:40:18 -07002394 struct dt_table *table;
2395 struct dt_entry dt_entry;
2396 uint32_t dt_hdr_size;
2397 unsigned int compressed_size = 0;
2398 unsigned int dtb_size = 0;
2399 unsigned int out_avai_len = 0;
2400 unsigned char *out_addr = NULL;
2401 unsigned char *best_match_dt_addr = NULL;
2402 int rc;
2403
2404 struct boot_img_hdr *hdr = (struct boot_img_hdr *) (boot_image_start);
2405
Parth Dixit4097b622016-03-15 14:42:27 +05302406#ifndef OSVERSION_IN_BOOTIMAGE
2407 dt_size = hdr->dt_size;
2408#endif
2409
2410 if(dt_size != 0) {
Amol Jadicb524072012-08-09 16:40:18 -07002411 /* add kernel offset */
2412 dt_image_offset += page_size;
2413 n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
2414 dt_image_offset += n;
2415
2416 /* add ramdisk offset */
2417 n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
2418 dt_image_offset += n;
2419
2420 /* add second offset */
2421 if(hdr->second_size != 0) {
2422 n = ROUND_TO_PAGE(hdr->second_size, page_mask);
2423 dt_image_offset += n;
2424 }
2425
2426 /* offset now point to start of dt.img */
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07002427 table = (struct dt_table*)(boot_image_start + dt_image_offset);
Amol Jadicb524072012-08-09 16:40:18 -07002428
Deepa Dinamani19648b42013-09-05 17:05:55 -07002429 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Amol Jadicb524072012-08-09 16:40:18 -07002430 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
2431 return -1;
2432 }
P.V. Phani Kumar82916762016-03-09 09:20:19 +05302433
2434 /* Its Error if, dt_hdr_size (table->num_entries * dt_entry size + Dev_Tree Header)
2435 goes beyound hdr->dt_size*/
Parth Dixit4097b622016-03-15 14:42:27 +05302436 if (dt_hdr_size > ROUND_TO_PAGE(dt_size,hdr->page_size)) {
P.V. Phani Kumar82916762016-03-09 09:20:19 +05302437 dprintf(CRITICAL, "ERROR: Invalid Device Tree size \n");
2438 return -1;
2439 }
2440
Joel Kingaa335dc2013-06-03 16:11:08 -07002441 /* Find index of device tree within device tree table */
2442 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Amol Jadicb524072012-08-09 16:40:18 -07002443 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
2444 return -1;
2445 }
2446
Matthew Qin271927e2015-03-31 22:07:07 -04002447 best_match_dt_addr = (unsigned char *)boot_image_start + dt_image_offset + dt_entry.offset;
2448 if (is_gzip_package(best_match_dt_addr, dt_entry.size))
2449 {
2450 out_addr = (unsigned char *)target_get_scratch_address() + scratch_offset;
2451 out_avai_len = target_get_max_flash_size() - scratch_offset;
Matthew Qin0b15b322015-05-19 05:20:54 -04002452 dprintf(INFO, "decompressing dtb: start\n");
Matthew Qin271927e2015-03-31 22:07:07 -04002453 rc = decompress(best_match_dt_addr,
2454 dt_entry.size, out_addr, out_avai_len,
2455 &compressed_size, &dtb_size);
2456 if (rc)
2457 {
Matthew Qin0b15b322015-05-19 05:20:54 -04002458 dprintf(CRITICAL, "decompressing dtb failed!!!\n");
Matthew Qin271927e2015-03-31 22:07:07 -04002459 ASSERT(0);
2460 }
2461
Matthew Qin0b15b322015-05-19 05:20:54 -04002462 dprintf(INFO, "decompressing dtb: done\n");
Matthew Qin271927e2015-03-31 22:07:07 -04002463 best_match_dt_addr = out_addr;
2464 } else {
2465 dtb_size = dt_entry.size;
2466 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002467 /* Validate and Read device device tree in the "tags_add */
Mayank Grovere559cec2017-10-17 15:12:03 +05302468 if (check_aboot_addr_range_overlap(hdr->tags_addr, dtb_size) ||
2469 check_ddr_addr_range_bound(hdr->tags_addr, dtb_size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002470 {
Mayank Grovere559cec2017-10-17 15:12:03 +05302471 dprintf(CRITICAL, "Device tree addresses are not valid.\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002472 return -1;
2473 }
2474
Amol Jadicb524072012-08-09 16:40:18 -07002475 /* Read device device tree in the "tags_add */
Matthew Qin271927e2015-03-31 22:07:07 -04002476 memmove((void*) hdr->tags_addr, (void *)best_match_dt_addr, dtb_size);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002477 } else
2478 return -1;
Amol Jadicb524072012-08-09 16:40:18 -07002479
2480 /* Everything looks fine. Return success. */
2481 return 0;
2482}
2483#endif
2484
Brian Swetland9c4c0752009-01-25 16:23:50 -08002485void cmd_boot(const char *arg, void *data, unsigned sz)
2486{
Amit Blay8a510302015-08-17 09:20:01 +03002487#ifdef MDTP_SUPPORT
2488 static bool is_mdtp_activated = 0;
2489#endif /* MDTP_SUPPORT */
Brian Swetland9c4c0752009-01-25 16:23:50 -08002490 unsigned kernel_actual;
2491 unsigned ramdisk_actual;
Kishor PKd8ddcad2017-07-27 13:53:57 +05302492 unsigned second_actual;
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002493 uint32_t image_actual;
2494 uint32_t dt_actual = 0;
P.V. Phani Kumareecfd822016-03-09 20:09:03 +05302495 uint32_t sig_actual = 0;
Kishor PK5134b332017-05-09 17:50:08 +05302496 uint32_t sig_size = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08002497 struct boot_img_hdr *hdr = NULL;
2498 struct kernel64_hdr *kptr = NULL;
Brian Swetland9c4c0752009-01-25 16:23:50 -08002499 char *ptr = ((char*) data);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002500 int ret = 0;
2501 uint8_t dtb_copied = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08002502 unsigned int out_len = 0;
2503 unsigned int out_avai_len = 0;
2504 unsigned char *out_addr = NULL;
2505 uint32_t dtb_offset = 0;
2506 unsigned char *kernel_start_addr = NULL;
2507 unsigned int kernel_size = 0;
Matthew Qin271927e2015-03-31 22:07:07 -04002508 unsigned int scratch_offset = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08002509
lijuang2008ff22016-03-07 17:56:27 +08002510#if FBCON_DISPLAY_MSG
2511 /* Exit keys' detection thread firstly */
2512 exit_menu_keys_detection();
2513#endif
2514
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002515#if VERIFIED_BOOT
Channagoud Kadabi6d5375e2015-06-23 17:15:42 -07002516 if(target_build_variant_user() && !device.is_unlocked)
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002517 {
2518 fastboot_fail("unlock device to use this command");
lijuang2008ff22016-03-07 17:56:27 +08002519 goto boot_failed;
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002520 }
2521#endif
2522
Brian Swetland9c4c0752009-01-25 16:23:50 -08002523 if (sz < sizeof(hdr)) {
2524 fastboot_fail("invalid bootimage header");
lijuang2008ff22016-03-07 17:56:27 +08002525 goto boot_failed;
Brian Swetland9c4c0752009-01-25 16:23:50 -08002526 }
2527
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002528 hdr = (struct boot_img_hdr *)data;
Brian Swetland9c4c0752009-01-25 16:23:50 -08002529
2530 /* ensure commandline is terminated */
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002531 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -08002532
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002533 if(target_is_emmc_boot() && hdr->page_size) {
2534 page_size = hdr->page_size;
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07002535 page_mask = page_size - 1;
2536 }
2537
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002538 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
2539 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Kishor PKd8ddcad2017-07-27 13:53:57 +05302540 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002541#if DEVICE_TREE
Parth Dixit4097b622016-03-15 14:42:27 +05302542#ifndef OSVERSION_IN_BOOTIMAGE
2543 dt_size = hdr->dt_size;
2544#endif
2545 dt_actual = ROUND_TO_PAGE(dt_size, page_mask);
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002546#endif
2547
2548 image_actual = ADD_OF(page_size, kernel_actual);
2549 image_actual = ADD_OF(image_actual, ramdisk_actual);
Kishor PKd8ddcad2017-07-27 13:53:57 +05302550 image_actual = ADD_OF(image_actual, second_actual);
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002551 image_actual = ADD_OF(image_actual, dt_actual);
2552
Kishor PK5134b332017-05-09 17:50:08 +05302553 /* Checking to prevent oob access in read_der_message_length */
2554 if (image_actual > sz) {
2555 fastboot_fail("bootimage header fields are invalid");
2556 goto boot_failed;
2557 }
2558 sig_size = sz - image_actual;
2559
P.V. Phani Kumareecfd822016-03-09 20:09:03 +05302560 if (target_use_signed_kernel() && (!device.is_unlocked)) {
2561 /* Calculate the signature length from boot image */
2562 sig_actual = read_der_message_length(
Kishor PK5134b332017-05-09 17:50:08 +05302563 (unsigned char*)(data + image_actual), sig_size);
Channagoud Kadabi3d839012014-06-26 14:26:39 -07002564 image_actual = ADD_OF(image_actual, sig_actual);
2565
Kishor PK5134b332017-05-09 17:50:08 +05302566 if (image_actual > sz) {
2567 fastboot_fail("bootimage header fields are invalid");
2568 goto boot_failed;
2569 }
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002570 }
2571
Sridhar Parasuram361e4cd2015-09-24 11:19:19 -07002572 // Initialize boot state before trying to verify boot.img
2573#if VERIFIED_BOOT
Channagoud Kadabi699466e2015-11-03 12:37:42 -08002574 boot_verifier_init();
Mayank Groverb337e932017-01-18 20:00:40 +05302575#endif
Channagoud Kadabibf6ce7d2015-09-17 13:25:35 -07002576 /* Handle overflow if the input image size is greater than
2577 * boot image buffer can hold
2578 */
2579 if ((target_get_max_flash_size() - (image_actual - sig_actual)) < page_size)
2580 {
2581 fastboot_fail("booimage: size is greater than boot image buffer can hold");
lijuang2008ff22016-03-07 17:56:27 +08002582 goto boot_failed;
Channagoud Kadabibf6ce7d2015-09-17 13:25:35 -07002583 }
Sridhar Parasuram361e4cd2015-09-24 11:19:19 -07002584
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002585 /* Verify the boot image
2586 * device & page_size are initialized in aboot_init
2587 */
Sridhar Parasuram7ae9fbc2015-09-17 09:56:30 -07002588 if (target_use_signed_kernel() && (!device.is_unlocked)) {
Channagoud Kadabi3d839012014-06-26 14:26:39 -07002589 /* Pass size excluding signature size, otherwise we would try to
2590 * access signature beyond its length
2591 */
2592 verify_signed_bootimg((uint32_t)data, (image_actual - sig_actual));
Sridhar Parasuram7ae9fbc2015-09-17 09:56:30 -07002593 }
Amit Blay4aa292f2015-04-28 21:55:59 +03002594#ifdef MDTP_SUPPORT
2595 else
2596 {
Shay Nachmani062aa3f2015-05-17 17:28:44 +03002597 /* fastboot boot is not allowed when MDTP is activated */
Amit Blay4aa292f2015-04-28 21:55:59 +03002598 mdtp_ext_partition_verification_t ext_partition;
Amit Blay8a510302015-08-17 09:20:01 +03002599
2600 if (!is_mdtp_activated) {
2601 ext_partition.partition = MDTP_PARTITION_NONE;
2602 mdtp_fwlock_verify_lock(&ext_partition);
2603 }
Amit Blay4aa292f2015-04-28 21:55:59 +03002604 }
Shay Nachmani062aa3f2015-05-17 17:28:44 +03002605
Amir Kotzer7c768c02016-04-13 09:08:05 +03002606 /* If mdtp state cannot be validate, block fastboot boot*/
2607 if(mdtp_activated(&is_mdtp_activated)){
2608 dprintf(CRITICAL, "mdtp_activated cannot validate state.\n");
2609 dprintf(CRITICAL, "Can not proceed with fastboot boot command.\n");
2610 goto boot_failed;
2611 }
Shay Nachmani062aa3f2015-05-17 17:28:44 +03002612 if(is_mdtp_activated){
2613 dprintf(CRITICAL, "fastboot boot command is not available.\n");
lijuang2008ff22016-03-07 17:56:27 +08002614 goto boot_failed;
Shay Nachmani062aa3f2015-05-17 17:28:44 +03002615 }
Amit Blay4aa292f2015-04-28 21:55:59 +03002616#endif /* MDTP_SUPPORT */
2617
Sridhar Parasuram361e4cd2015-09-24 11:19:19 -07002618#if VERIFIED_BOOT
Mayank Grover912eaa62017-10-26 12:08:53 +05302619 if (VB_M <= target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +05302620 {
2621 /* set boot and system versions. */
2622 set_os_version((unsigned char *)data);
2623 // send root of trust
2624 if(!send_rot_command((uint32_t)device.is_unlocked))
2625 ASSERT(0);
2626 }
Parth Dixitbc9b6492015-10-18 00:41:38 +05302627#endif
Channagoud Kadabia22144f2013-03-20 11:49:01 -07002628 /*
Matthew Qinbb7923d2015-02-09 10:56:09 +08002629 * Check if the kernel image is a gzip package. If yes, need to decompress it.
2630 * If not, continue booting.
2631 */
2632 if (is_gzip_package((unsigned char *)(data + page_size), hdr->kernel_size))
2633 {
2634 out_addr = (unsigned char *)target_get_scratch_address();
2635 out_addr = (unsigned char *)(out_addr + image_actual + page_size);
2636 out_avai_len = target_get_max_flash_size() - image_actual - page_size;
Matthew Qin0b15b322015-05-19 05:20:54 -04002637 dprintf(INFO, "decompressing kernel image: start\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08002638 ret = decompress((unsigned char *)(ptr + page_size),
2639 hdr->kernel_size, out_addr, out_avai_len,
2640 &dtb_offset, &out_len);
2641 if (ret)
2642 {
Matthew Qin0b15b322015-05-19 05:20:54 -04002643 dprintf(CRITICAL, "decompressing image failed!!!\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08002644 ASSERT(0);
2645 }
2646
Matthew Qin0b15b322015-05-19 05:20:54 -04002647 dprintf(INFO, "decompressing kernel image: done\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08002648 kptr = (struct kernel64_hdr *)out_addr;
2649 kernel_start_addr = out_addr;
2650 kernel_size = out_len;
2651 } else {
2652 kptr = (struct kernel64_hdr*)((char *)data + page_size);
2653 kernel_start_addr = (unsigned char *)((char *)data + page_size);
2654 kernel_size = hdr->kernel_size;
2655 }
2656
2657 /*
Channagoud Kadabia22144f2013-03-20 11:49:01 -07002658 * Update the kernel/ramdisk/tags address if the boot image header
2659 * has default values, these default values come from mkbootimg when
2660 * the boot image is flashed using fastboot flash:raw
2661 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08002662 update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr));
Dima Zavin3cadfff2013-03-21 14:30:48 -07002663
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002664 /* Get virtual addresses since the hdr saves physical addresses. */
2665 hdr->kernel_addr = VA(hdr->kernel_addr);
2666 hdr->ramdisk_addr = VA(hdr->ramdisk_addr);
2667 hdr->tags_addr = VA(hdr->tags_addr);
Brian Swetland9c4c0752009-01-25 16:23:50 -08002668
Matthew Qinbb7923d2015-02-09 10:56:09 +08002669 kernel_size = ROUND_TO_PAGE(kernel_size, page_mask);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002670 /* Check if the addresses in the header are valid. */
Matthew Qinbb7923d2015-02-09 10:56:09 +08002671 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_size) ||
Mayank Grovere559cec2017-10-17 15:12:03 +05302672 check_ddr_addr_range_bound(hdr->kernel_addr, kernel_size) ||
2673 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual) ||
2674 check_ddr_addr_range_bound(hdr->ramdisk_addr, ramdisk_actual))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002675 {
Mayank Grovere559cec2017-10-17 15:12:03 +05302676 dprintf(CRITICAL, "kernel/ramdisk addresses are not valid.\n");
lijuang2008ff22016-03-07 17:56:27 +08002677 goto boot_failed;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002678 }
2679
Amol Jadicb524072012-08-09 16:40:18 -07002680#if DEVICE_TREE
Matthew Qin271927e2015-03-31 22:07:07 -04002681 scratch_offset = image_actual + page_size + out_len;
Amol Jadicb524072012-08-09 16:40:18 -07002682 /* find correct dtb and copy it to right location */
Matthew Qin271927e2015-03-31 22:07:07 -04002683 ret = copy_dtb(data, scratch_offset);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002684
2685 dtb_copied = !ret ? 1 : 0;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002686#else
Mayank Grovere559cec2017-10-17 15:12:03 +05302687 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE) ||
2688 check_ddr_addr_range_bound(hdr->tags_addr, MAX_TAGS_SIZE))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002689 {
Mayank Grovere559cec2017-10-17 15:12:03 +05302690 dprintf(CRITICAL, "Tags addresses are not valid.\n");
lijuang2008ff22016-03-07 17:56:27 +08002691 goto boot_failed;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002692 }
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002693#endif
2694
2695 /* Load ramdisk & kernel */
2696 memmove((void*) hdr->ramdisk_addr, ptr + page_size + kernel_actual, hdr->ramdisk_size);
Matthew Qinbb7923d2015-02-09 10:56:09 +08002697 memmove((void*) hdr->kernel_addr, (char*) (kernel_start_addr), kernel_size);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002698
2699#if DEVICE_TREE
Mayank Grovere559cec2017-10-17 15:12:03 +05302700 if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual) ||
2701 check_ddr_addr_range_bound(hdr->tags_addr, kernel_actual))
Matthew Qinbb7923d2015-02-09 10:56:09 +08002702 {
Mayank Grovere559cec2017-10-17 15:12:03 +05302703 dprintf(CRITICAL, "Tags addresses are not valid.\n");
lijuang2008ff22016-03-07 17:56:27 +08002704 goto boot_failed;
Matthew Qinbb7923d2015-02-09 10:56:09 +08002705 }
2706
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002707 /*
2708 * If dtb is not found look for appended DTB in the kernel.
2709 * If appended dev tree is found, update the atags with
2710 * memory address to the DTB appended location on RAM.
2711 * Else update with the atags address in the kernel header
2712 */
2713 if (!dtb_copied) {
2714 void *dtb;
Matthew Qinbb7923d2015-02-09 10:56:09 +08002715 dtb = dev_tree_appended((void*)(ptr + page_size),
2716 hdr->kernel_size, dtb_offset,
Dima Zavine63e5572013-05-03 12:23:06 -07002717 (void *)hdr->tags_addr);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002718 if (!dtb) {
2719 fastboot_fail("dtb not found");
lijuang2008ff22016-03-07 17:56:27 +08002720 goto boot_failed;
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002721 }
Amol Jadicb524072012-08-09 16:40:18 -07002722 }
2723#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08002724
2725 fastboot_okay("");
Amol Jadi7c4316c2013-10-07 14:19:26 -07002726 fastboot_stop();
Brian Swetland9c4c0752009-01-25 16:23:50 -08002727
Dima Zavin77e41f32013-03-06 16:10:43 -08002728 boot_linux((void*) hdr->kernel_addr, (void*) hdr->tags_addr,
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002729 (const char*) hdr->cmdline, board_machtype(),
2730 (void*) hdr->ramdisk_addr, hdr->ramdisk_size);
lijuang2008ff22016-03-07 17:56:27 +08002731
2732 /* fastboot already stop, it's no need to show fastboot menu */
2733 return;
2734boot_failed:
2735#if FBCON_DISPLAY_MSG
2736 /* revert to fastboot menu if boot failed */
2737 display_fastboot_menu();
2738#endif
2739 return;
Brian Swetland9c4c0752009-01-25 16:23:50 -08002740}
2741
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002742void cmd_erase_nand(const char *arg, void *data, unsigned sz)
Dima Zavin214cc642009-01-26 11:16:21 -08002743{
2744 struct ptentry *ptn;
2745 struct ptable *ptable;
2746
2747 ptable = flash_get_ptable();
2748 if (ptable == NULL) {
2749 fastboot_fail("partition table doesn't exist");
2750 return;
2751 }
2752
2753 ptn = ptable_find(ptable, arg);
2754 if (ptn == NULL) {
2755 fastboot_fail("unknown partition name");
2756 return;
2757 }
2758
2759 if (flash_erase(ptn)) {
2760 fastboot_fail("failed to erase partition");
2761 return;
2762 }
2763 fastboot_okay("");
2764}
2765
Bikas Gurungd48bd242010-09-04 19:54:32 -07002766
2767void cmd_erase_mmc(const char *arg, void *data, unsigned sz)
2768{
2769 unsigned long long ptn = 0;
Oliver Wangcee448d2013-10-22 18:40:13 +08002770 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07002771 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002772 uint8_t lun = 0;
Vijay Kumar Pendoti0b21f462016-05-02 17:09:18 +05302773 char *footer = NULL;
Bikas Gurungd48bd242010-09-04 19:54:32 -07002774
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002775#if VERIFIED_BOOT
2776 if(!strcmp(arg, KEYSTORE_PTN_NAME))
2777 {
2778 if(!device.is_unlocked)
2779 {
2780 fastboot_fail("unlock device to erase keystore");
2781 return;
2782 }
2783 }
2784#endif
2785
Kinson Chikf1a43512011-07-14 11:28:39 -07002786 index = partition_get_index(arg);
2787 ptn = partition_get_offset(index);
Oliver Wangcee448d2013-10-22 18:40:13 +08002788 size = partition_get_size(index);
Neeti Desaica8c9602011-10-06 11:40:00 -07002789
Kinson Chikf1a43512011-07-14 11:28:39 -07002790 if(ptn == 0) {
Neeti Desaica8c9602011-10-06 11:40:00 -07002791 fastboot_fail("Partition table doesn't exist\n");
Bikas Gurungd48bd242010-09-04 19:54:32 -07002792 return;
2793 }
Kun Liang2f1601a2013-08-12 16:29:54 +08002794
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002795 lun = partition_get_lun(index);
2796 mmc_set_lun(lun);
2797
Sridhar Parasuramd7957122015-02-27 11:33:40 -08002798 if (platform_boot_dev_isemmc())
2799 {
2800 if (mmc_erase_card(ptn, size)) {
2801 fastboot_fail("failed to erase partition\n");
2802 return;
2803 }
2804 } else {
2805 BUF_DMA_ALIGN(out, DEFAULT_ERASE_SIZE);
2806 size = partition_get_size(index);
2807 if (size > DEFAULT_ERASE_SIZE)
2808 size = DEFAULT_ERASE_SIZE;
Kun Liang2f1601a2013-08-12 16:29:54 +08002809
Sridhar Parasuramd7957122015-02-27 11:33:40 -08002810 /* Simple inefficient version of erase. Just writing
2811 0 in first several blocks */
2812 if (mmc_write(ptn , size, (unsigned int *)out)) {
2813 fastboot_fail("failed to erase partition");
2814 return;
2815 }
Vijay Kumar Pendoti0b21f462016-05-02 17:09:18 +05302816 /*Erase FDE metadata at the userdata footer*/
2817 if(!(strncmp(arg, "userdata", 8)))
2818 {
2819 footer = memalign(CACHE_LINE, FOOTER_SIZE);
2820 memset((void *)footer, 0, FOOTER_SIZE);
2821
2822 size = partition_get_size(index);
2823
2824 if (mmc_write((ptn + size) - FOOTER_SIZE , FOOTER_SIZE, (unsigned int *)footer)) {
2825 fastboot_fail("failed to erase userdata footer");
2826 free(footer);
2827 return;
2828 }
2829 free(footer);
2830 }
Bikas Gurungd48bd242010-09-04 19:54:32 -07002831 }
Sridhar Parasuram32b30662015-07-10 13:33:22 -07002832#if VERIFIED_BOOT
Mayank Grover912eaa62017-10-26 12:08:53 +05302833 if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05302834 !(strncmp(arg, "userdata", 8)) &&
2835 send_delete_keys_to_tz())
Sridhar Parasuram32b30662015-07-10 13:33:22 -07002836 ASSERT(0);
2837#endif
Bikas Gurungd48bd242010-09-04 19:54:32 -07002838 fastboot_okay("");
2839}
2840
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002841void cmd_erase(const char *arg, void *data, unsigned sz)
2842{
Channagoud Kadabice3cf422015-04-17 16:02:30 -07002843#if VERIFIED_BOOT
Channagoud Kadabi35297672015-06-13 11:09:49 -07002844 if (target_build_variant_user())
Channagoud Kadabice3cf422015-04-17 16:02:30 -07002845 {
Sridhar Parasuramc32d07d2015-07-12 10:57:48 -07002846 if(!device.is_unlocked)
Channagoud Kadabice3cf422015-04-17 16:02:30 -07002847 {
Channagoud Kadabi35297672015-06-13 11:09:49 -07002848 fastboot_fail("device is locked. Cannot erase");
Channagoud Kadabice3cf422015-04-17 16:02:30 -07002849 return;
2850 }
2851 }
2852#endif
2853
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002854 if(target_is_emmc_boot())
2855 cmd_erase_mmc(arg, data, sz);
2856 else
2857 cmd_erase_nand(arg, data, sz);
2858}
Bikas Gurungd48bd242010-09-04 19:54:32 -07002859
Mayank Grover11ff9692018-01-11 11:54:49 +05302860/* Get the size from partiton name */
2861static void get_partition_size(const char *arg, char *response)
2862{
2863 uint64_t ptn = 0;
2864 uint64_t size;
2865 int index = INVALID_PTN;
2866
2867 index = partition_get_index(arg);
2868
2869 if (index == INVALID_PTN)
2870 {
2871 dprintf(CRITICAL, "Invalid partition index\n");
2872 return;
2873 }
2874
2875 ptn = partition_get_offset(index);
2876
2877 if(!ptn)
2878 {
2879 dprintf(CRITICAL, "Invalid partition name %s\n", arg);
2880 return;
2881 }
2882
2883 size = partition_get_size(index);
2884
2885 snprintf(response, MAX_RSP_SIZE, "\t 0x%llx", size);
2886 return;
2887}
2888
Mayank Groverd38fe012018-03-13 15:33:16 +05302889/* Function to get partition type */
2890static void get_partition_type(const char *arg, char *response)
2891{
2892 uint n = 0;
2893
2894 if (arg == NULL ||
2895 response == NULL)
2896 {
2897 dprintf(CRITICAL, "Invalid input parameter\n");
2898 return;
2899 }
2900
2901 /* By default copy raw to response */
2902 strncpy(response, RAW_STR, strlen(RAW_STR));
2903
2904 /* Mark partiton type for known paritions only */
2905 for (n=0; n < ARRAY_SIZE(part_type_known); n++)
2906 {
2907 if (!strncmp(part_type_known[n].part_name, arg, strlen(arg)))
2908 {
2909 strncpy(response, part_type_known[n].type_response, MAX_RSP_SIZE);
2910 break;
2911 }
2912 }
2913 return;
2914}
2915
Mayank Grover11ff9692018-01-11 11:54:49 +05302916/*
2917 * Publish the partition type & size info
2918 * fastboot getvar will publish the required information.
2919 * fastboot getvar partition_size:<partition_name>: partition size in hex
2920 * fastboot getvar partition_type:<partition_name>: partition type (ext/fat)
2921 */
2922static void publish_getvar_partition_info(struct getvar_partition_info *info, uint8_t num_parts)
2923{
Mayank Groverd38fe012018-03-13 15:33:16 +05302924 uint8_t i;
Mayank Grover11ff9692018-01-11 11:54:49 +05302925 static bool published = false;
2926 struct partition_entry *ptn_entry =
2927 partition_get_partition_entries();
2928 memset(info, 0, sizeof(struct getvar_partition_info)* num_parts);
2929
2930 for (i = 0; i < num_parts; i++) {
2931 strlcat(info[i].part_name, (const char *)ptn_entry[i].name, MAX_RSP_SIZE);
2932 strlcat(info[i].getvar_size, "partition-size:", MAX_GET_VAR_NAME_SIZE);
2933 strlcat(info[i].getvar_type, "partition-type:", MAX_GET_VAR_NAME_SIZE);
2934
Mayank Groverd38fe012018-03-13 15:33:16 +05302935 get_partition_type(info[i].part_name, info[i].type_response);
Mayank Grover11ff9692018-01-11 11:54:49 +05302936 get_partition_size(info[i].part_name, info[i].size_response);
Mayank Grover11ff9692018-01-11 11:54:49 +05302937 if (strlcat(info[i].getvar_size, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
2938 {
2939 dprintf(CRITICAL, "partition size name truncated\n");
2940 return;
2941 }
2942 if (strlcat(info[i].getvar_type, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
2943 {
2944 dprintf(CRITICAL, "partition type name truncated\n");
2945 return;
2946 }
2947
2948 if (!published)
2949 {
2950 /* publish partition size & type info */
2951 fastboot_publish((const char *) info[i].getvar_size, (const char *) info[i].size_response);
2952 fastboot_publish((const char *) info[i].getvar_type, (const char *) info[i].type_response);
2953 }
2954 }
2955 if (!published)
2956 published = true;
2957}
2958
2959
Ajay Dudani5c761132011-04-07 20:19:04 -07002960void cmd_flash_mmc_img(const char *arg, void *data, unsigned sz)
Shashank Mittal23b8f422010-04-16 19:27:21 -07002961{
2962 unsigned long long ptn = 0;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07002963 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07002964 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002965 char *token = NULL;
2966 char *pname = NULL;
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07002967 char *sp;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002968 uint8_t lun = 0;
2969 bool lun_set = false;
Mayank Grover351a75e2017-05-30 20:06:08 +05302970 int current_active_slot = INVALID;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07002971
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07002972 token = strtok_r((char *)arg, ":", &sp);
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002973 pname = token;
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07002974 token = strtok_r(NULL, ":", &sp);
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002975 if(token)
2976 {
2977 lun = atoi(token);
2978 mmc_set_lun(lun);
2979 lun_set = true;
2980 }
2981
Mao Jinlong226f33a2014-07-04 17:24:10 +08002982 if (pname)
Greg Grisco6e754772011-06-23 12:19:39 -07002983 {
Channagoud Kadabiad259832015-05-29 11:14:17 -07002984 if (!strncmp(pname, "frp-unlock", strlen("frp-unlock")))
2985 {
2986 if (!aboot_frp_unlock(pname, data, sz))
2987 {
2988 fastboot_info("FRP unlock successful");
2989 fastboot_okay("");
2990 }
2991 else
2992 fastboot_fail("Secret key is invalid, please update the bootloader with secret key");
2993
2994 return;
2995 }
2996
Mao Jinlong226f33a2014-07-04 17:24:10 +08002997 if (!strcmp(pname, "partition"))
2998 {
2999 dprintf(INFO, "Attempt to write partition image.\n");
3000 if (write_partition(sz, (unsigned char *) data)) {
3001 fastboot_fail("failed to write partition");
Greg Grisco6e754772011-06-23 12:19:39 -07003002 return;
3003 }
Mayank Grover11ff9692018-01-11 11:54:49 +05303004 /* Re-publish partition table */
3005 publish_getvar_partition_info(part_info, partition_get_partition_count());
Mayank Grover351a75e2017-05-30 20:06:08 +05303006
3007 /* Rescan partition table to ensure we have multislot support*/
3008 if (partition_scan_for_multislot())
3009 {
3010 current_active_slot = partition_find_active_slot();
3011 dprintf(INFO, "Multislot supported: Slot %s active",
3012 (SUFFIX_SLOT(current_active_slot)));
3013 }
3014 partition_mark_active_slot(current_active_slot);
Greg Grisco6e754772011-06-23 12:19:39 -07003015 }
Mao Jinlong226f33a2014-07-04 17:24:10 +08003016 else
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003017 {
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003018#if VERIFIED_BOOT
3019 if(!strcmp(pname, KEYSTORE_PTN_NAME))
3020 {
3021 if(!device.is_unlocked)
3022 {
3023 fastboot_fail("unlock device to flash keystore");
3024 return;
3025 }
Gaurav Nebhwanic7313cc2015-12-15 22:25:04 +05303026 if(!boot_verify_validate_keystore((unsigned char *)data,sz))
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003027 {
3028 fastboot_fail("image is not a keystore file");
3029 return;
3030 }
3031 }
3032#endif
Mao Jinlong226f33a2014-07-04 17:24:10 +08003033 index = partition_get_index(pname);
3034 ptn = partition_get_offset(index);
3035 if(ptn == 0) {
3036 fastboot_fail("partition table doesn't exist");
3037 return;
3038 }
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003039
Mayank Grover351a75e2017-05-30 20:06:08 +05303040 if (!strncmp(pname, "boot", strlen("boot"))
3041 || !strcmp(pname, "recovery"))
3042 {
Mao Jinlong226f33a2014-07-04 17:24:10 +08003043 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
3044 fastboot_fail("image is not a boot image");
3045 return;
3046 }
Mayank Grover351a75e2017-05-30 20:06:08 +05303047
3048 /* Reset multislot_partition attributes in case of flashing boot */
3049 if (partition_multislot_is_supported())
3050 {
3051 partition_reset_attributes(index);
3052 }
Mao Jinlong226f33a2014-07-04 17:24:10 +08003053 }
3054
3055 if(!lun_set)
3056 {
3057 lun = partition_get_lun(index);
3058 mmc_set_lun(lun);
3059 }
3060
3061 size = partition_get_size(index);
Gaurav Nebhwani4d2389c2016-03-17 21:10:05 +05303062 if (ROUND_TO_PAGE(sz, mmc_blocksize_mask) > size) {
Mao Jinlong226f33a2014-07-04 17:24:10 +08003063 fastboot_fail("size too large");
3064 return;
3065 }
3066 else if (mmc_write(ptn , sz, (unsigned int *)data)) {
3067 fastboot_fail("flash write failure");
3068 return;
3069 }
Greg Grisco6e754772011-06-23 12:19:39 -07003070 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07003071 }
3072 fastboot_okay("");
3073 return;
3074}
3075
Ajay Dudanide984792015-03-02 09:57:41 -08003076void cmd_flash_meta_img(const char *arg, void *data, unsigned sz)
3077{
3078 int i, images;
3079 meta_header_t *meta_header;
3080 img_header_entry_t *img_header_entry;
Parth Dixit3e2d3032016-03-04 17:11:52 +05303081 /*End of the image address*/
3082 uintptr_t data_end;
3083
3084 if( (UINT_MAX - sz) > (uintptr_t)data )
3085 data_end = (uintptr_t)data + sz;
3086 else
3087 {
3088 fastboot_fail("Cannot flash: image header corrupt");
3089 return;
3090 }
3091
3092 if( data_end < ((uintptr_t)data + sizeof(meta_header_t)))
3093 {
3094 fastboot_fail("Cannot flash: image header corrupt");
3095 return;
3096 }
Ajay Dudanide984792015-03-02 09:57:41 -08003097
lijuang8ee21882016-04-19 16:57:11 +08003098 /* If device is locked:
3099 * Forbid to flash image to avoid the device to bypass the image
3100 * which with "any" name other than bootloader. Because it maybe
3101 * a meta package of all partitions.
3102 */
3103#if VERIFIED_BOOT
3104 if (target_build_variant_user()) {
3105 if (!device.is_unlocked) {
3106 fastboot_fail("Device is locked, meta image flashing is not allowed");
3107 return;
3108 }
Mayank Grover889be1b2017-09-12 20:12:23 +05303109
Mayank Grover912eaa62017-10-26 12:08:53 +05303110 if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05303111 !device.is_unlock_critical)
3112 {
lijuang8ee21882016-04-19 16:57:11 +08003113 fastboot_fail("Device is critical locked, Meta image flashing is not allowed");
3114 return;
3115 }
lijuang8ee21882016-04-19 16:57:11 +08003116 }
3117#endif
3118
Ajay Dudanide984792015-03-02 09:57:41 -08003119 meta_header = (meta_header_t*) data;
Parth Dixit3e2d3032016-03-04 17:11:52 +05303120 if( data_end < ((uintptr_t)data + meta_header->img_hdr_sz))
3121 {
3122 fastboot_fail("Cannot flash: image header corrupt");
3123 return;
3124 }
Ajay Dudanide984792015-03-02 09:57:41 -08003125 img_header_entry = (img_header_entry_t*) (data+sizeof(meta_header_t));
3126
3127 images = meta_header->img_hdr_sz / sizeof(img_header_entry_t);
3128
3129 for (i=0; i<images; i++) {
3130
3131 if((img_header_entry[i].ptn_name == NULL) ||
3132 (img_header_entry[i].start_offset == 0) ||
3133 (img_header_entry[i].size == 0))
3134 break;
Kishor PK49831502017-04-21 17:55:43 +05303135 if ((UINT_MAX - img_header_entry[i].start_offset) < (uintptr_t)data) {
3136 fastboot_fail("Integer overflow detected in start_offset of img");
3137 break;
3138 }
3139 else if ((UINT_MAX - (img_header_entry[i].start_offset + (uintptr_t)data)) < img_header_entry[i].size) {
3140 fastboot_fail("Integer overflow detected in size of img");
3141 break;
3142 }
Parth Dixit3e2d3032016-03-04 17:11:52 +05303143 if( data_end < ((uintptr_t)data + img_header_entry[i].start_offset
3144 + img_header_entry[i].size) )
3145 {
3146 fastboot_fail("Cannot flash: image size mismatch");
3147 break;
3148 }
3149
Ajay Dudanide984792015-03-02 09:57:41 -08003150 cmd_flash_mmc_img(img_header_entry[i].ptn_name,
3151 (void *) data + img_header_entry[i].start_offset,
3152 img_header_entry[i].size);
3153 }
3154
Channagoud Kadabiaafbbfc2015-03-04 17:52:56 -08003155 if (!strncmp(arg, "bootloader", strlen("bootloader")))
3156 {
3157 strlcpy(device.bootloader_version, TARGET(BOARD), MAX_VERSION_LEN);
3158 strlcat(device.bootloader_version, "-", MAX_VERSION_LEN);
3159 strlcat(device.bootloader_version, meta_header->img_version, MAX_VERSION_LEN);
3160 }
3161 else
3162 {
3163 strlcpy(device.radio_version, TARGET(BOARD), MAX_VERSION_LEN);
3164 strlcat(device.radio_version, "-", MAX_VERSION_LEN);
3165 strlcat(device.radio_version, meta_header->img_version, MAX_VERSION_LEN);
3166 }
3167
3168 write_device_info(&device);
Ajay Dudanide984792015-03-02 09:57:41 -08003169 fastboot_okay("");
3170 return;
3171}
3172
Ajay Dudani5c761132011-04-07 20:19:04 -07003173void cmd_flash_mmc_sparse_img(const char *arg, void *data, unsigned sz)
3174{
3175 unsigned int chunk;
wufeng.jiang813dc352015-06-02 23:02:46 -04003176 uint64_t chunk_data_sz;
Channagoud Kadabif627cf82013-09-09 14:08:20 -07003177 uint32_t *fill_buf = NULL;
3178 uint32_t fill_val;
Mayank Grover4f50bba2017-07-19 18:17:08 +05303179 uint32_t blk_sz_actual = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07003180 sparse_header_t *sparse_header;
3181 chunk_header_t *chunk_header;
Ajay Dudaniab18f022011-05-12 14:39:22 -07003182 uint32_t total_blocks = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07003183 unsigned long long ptn = 0;
Channagoud Kadabi65b91002011-10-11 17:34:33 +05303184 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07003185 int index = INVALID_PTN;
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05303186 uint32_t i;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003187 uint8_t lun = 0;
vijay kumar800255e2015-04-24 20:26:19 +05303188 /*End of the sparse image address*/
Parth Dixit64b1a482016-03-07 16:31:26 +05303189 uintptr_t data_end = (uintptr_t)data + sz;
Ajay Dudani5c761132011-04-07 20:19:04 -07003190
Kinson Chikf1a43512011-07-14 11:28:39 -07003191 index = partition_get_index(arg);
3192 ptn = partition_get_offset(index);
3193 if(ptn == 0) {
Ajay Dudani5c761132011-04-07 20:19:04 -07003194 fastboot_fail("partition table doesn't exist");
3195 return;
3196 }
3197
Channagoud Kadabi65b91002011-10-11 17:34:33 +05303198 size = partition_get_size(index);
Channagoud Kadabi65b91002011-10-11 17:34:33 +05303199
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003200 lun = partition_get_lun(index);
3201 mmc_set_lun(lun);
3202
vijay kumar800255e2015-04-24 20:26:19 +05303203 if (sz < sizeof(sparse_header_t)) {
3204 fastboot_fail("size too low");
3205 return;
3206 }
3207
Ajay Dudani5c761132011-04-07 20:19:04 -07003208 /* Read and skip over sparse image header */
3209 sparse_header = (sparse_header_t *) data;
vijay kumar800255e2015-04-24 20:26:19 +05303210
Mayank Grover48bd9bb2017-07-19 12:04:16 +05303211 if (!sparse_header->blk_sz || (sparse_header->blk_sz % 4)){
3212 fastboot_fail("Invalid block size\n");
3213 return;
3214 }
3215
vijay kumar1321f342015-03-27 12:13:42 +05303216 if (((uint64_t)sparse_header->total_blks * (uint64_t)sparse_header->blk_sz) > size) {
Ajay Dudani876b3282012-12-21 14:12:17 -08003217 fastboot_fail("size too large");
3218 return;
3219 }
3220
vijay kumarde4fcf62015-04-23 13:05:49 +05303221 data += sizeof(sparse_header_t);
vijay kumar800255e2015-04-24 20:26:19 +05303222
Parth Dixit64b1a482016-03-07 16:31:26 +05303223 if (data_end < (uintptr_t)data) {
vijay kumar800255e2015-04-24 20:26:19 +05303224 fastboot_fail("buffer overreads occured due to invalid sparse header");
3225 return;
3226 }
3227
vijay kumarde4fcf62015-04-23 13:05:49 +05303228 if(sparse_header->file_hdr_sz != sizeof(sparse_header_t))
Ajay Dudani5c761132011-04-07 20:19:04 -07003229 {
vijay kumarde4fcf62015-04-23 13:05:49 +05303230 fastboot_fail("sparse header size mismatch");
3231 return;
Ajay Dudani5c761132011-04-07 20:19:04 -07003232 }
3233
Ajay Dudanib06c05f2011-05-12 14:46:10 -07003234 dprintf (SPEW, "=== Sparse Image Header ===\n");
3235 dprintf (SPEW, "magic: 0x%x\n", sparse_header->magic);
3236 dprintf (SPEW, "major_version: 0x%x\n", sparse_header->major_version);
3237 dprintf (SPEW, "minor_version: 0x%x\n", sparse_header->minor_version);
3238 dprintf (SPEW, "file_hdr_sz: %d\n", sparse_header->file_hdr_sz);
3239 dprintf (SPEW, "chunk_hdr_sz: %d\n", sparse_header->chunk_hdr_sz);
3240 dprintf (SPEW, "blk_sz: %d\n", sparse_header->blk_sz);
3241 dprintf (SPEW, "total_blks: %d\n", sparse_header->total_blks);
3242 dprintf (SPEW, "total_chunks: %d\n", sparse_header->total_chunks);
Ajay Dudani5c761132011-04-07 20:19:04 -07003243
3244 /* Start processing chunks */
3245 for (chunk=0; chunk<sparse_header->total_chunks; chunk++)
3246 {
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05303247 /* Make sure the total image size does not exceed the partition size */
3248 if(((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz) >= size) {
3249 fastboot_fail("size too large");
3250 return;
3251 }
Ajay Dudani5c761132011-04-07 20:19:04 -07003252 /* Read and skip over chunk header */
3253 chunk_header = (chunk_header_t *) data;
3254 data += sizeof(chunk_header_t);
3255
Parth Dixit64b1a482016-03-07 16:31:26 +05303256 if (data_end < (uintptr_t)data) {
vijay kumar800255e2015-04-24 20:26:19 +05303257 fastboot_fail("buffer overreads occured due to invalid sparse header");
3258 return;
3259 }
3260
Ajay Dudani5c761132011-04-07 20:19:04 -07003261 dprintf (SPEW, "=== Chunk Header ===\n");
3262 dprintf (SPEW, "chunk_type: 0x%x\n", chunk_header->chunk_type);
3263 dprintf (SPEW, "chunk_data_sz: 0x%x\n", chunk_header->chunk_sz);
3264 dprintf (SPEW, "total_size: 0x%x\n", chunk_header->total_sz);
3265
vijay kumar800255e2015-04-24 20:26:19 +05303266 if(sparse_header->chunk_hdr_sz != sizeof(chunk_header_t))
Ajay Dudani5c761132011-04-07 20:19:04 -07003267 {
vijay kumar800255e2015-04-24 20:26:19 +05303268 fastboot_fail("chunk header size mismatch");
3269 return;
Ajay Dudani5c761132011-04-07 20:19:04 -07003270 }
3271
wufeng.jiang813dc352015-06-02 23:02:46 -04003272 chunk_data_sz = (uint64_t)sparse_header->blk_sz * chunk_header->chunk_sz;
3273
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05303274 /* Make sure that the chunk size calculated from sparse image does not
3275 * exceed partition size
3276 */
3277 if ((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz + chunk_data_sz > size)
3278 {
3279 fastboot_fail("Chunk data size exceeds partition size");
3280 return;
3281 }
3282
Ajay Dudani5c761132011-04-07 20:19:04 -07003283 switch (chunk_header->chunk_type)
3284 {
3285 case CHUNK_TYPE_RAW:
wufeng.jiang813dc352015-06-02 23:02:46 -04003286 if((uint64_t)chunk_header->total_sz != ((uint64_t)sparse_header->chunk_hdr_sz +
Ajay Dudani5c761132011-04-07 20:19:04 -07003287 chunk_data_sz))
3288 {
3289 fastboot_fail("Bogus chunk size for chunk type Raw");
3290 return;
3291 }
3292
Parth Dixit64b1a482016-03-07 16:31:26 +05303293 if (data_end < (uintptr_t)data + chunk_data_sz) {
vijay kumar800255e2015-04-24 20:26:19 +05303294 fastboot_fail("buffer overreads occured due to invalid sparse header");
3295 return;
3296 }
3297
wufeng.jiang813dc352015-06-02 23:02:46 -04003298 /* chunk_header->total_sz is uint32,So chunk_data_sz is now less than 2^32
3299 otherwise it will return in the line above
3300 */
Ajay Dudaniab18f022011-05-12 14:39:22 -07003301 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
wufeng.jiang813dc352015-06-02 23:02:46 -04003302 (uint32_t)chunk_data_sz,
Ajay Dudaniab18f022011-05-12 14:39:22 -07003303 (unsigned int*)data))
Ajay Dudani5c761132011-04-07 20:19:04 -07003304 {
3305 fastboot_fail("flash write failure");
3306 return;
3307 }
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05303308 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
3309 fastboot_fail("Bogus size for RAW chunk type");
3310 return;
3311 }
Ajay Dudani5c761132011-04-07 20:19:04 -07003312 total_blocks += chunk_header->chunk_sz;
wufeng.jiang813dc352015-06-02 23:02:46 -04003313 data += (uint32_t)chunk_data_sz;
Ajay Dudani5c761132011-04-07 20:19:04 -07003314 break;
3315
Channagoud Kadabif627cf82013-09-09 14:08:20 -07003316 case CHUNK_TYPE_FILL:
3317 if(chunk_header->total_sz != (sparse_header->chunk_hdr_sz +
3318 sizeof(uint32_t)))
3319 {
3320 fastboot_fail("Bogus chunk size for chunk type FILL");
3321 return;
3322 }
3323
Mayank Grover4f50bba2017-07-19 18:17:08 +05303324 blk_sz_actual = ROUNDUP(sparse_header->blk_sz, CACHE_LINE);
3325 /* Integer overflow detected */
3326 if (blk_sz_actual < sparse_header->blk_sz)
3327 {
3328 fastboot_fail("Invalid block size");
3329 return;
3330 }
3331
3332 fill_buf = (uint32_t *)memalign(CACHE_LINE, blk_sz_actual);
Channagoud Kadabif627cf82013-09-09 14:08:20 -07003333 if (!fill_buf)
3334 {
3335 fastboot_fail("Malloc failed for: CHUNK_TYPE_FILL");
3336 return;
3337 }
3338
Parth Dixit64b1a482016-03-07 16:31:26 +05303339 if (data_end < (uintptr_t)data + sizeof(uint32_t)) {
vijay kumar800255e2015-04-24 20:26:19 +05303340 fastboot_fail("buffer overreads occured due to invalid sparse header");
Gaurav Nebhwani163cbf82016-03-17 21:21:27 +05303341 free(fill_buf);
vijay kumar800255e2015-04-24 20:26:19 +05303342 return;
3343 }
Channagoud Kadabif627cf82013-09-09 14:08:20 -07003344 fill_val = *(uint32_t *)data;
3345 data = (char *) data + sizeof(uint32_t);
Channagoud Kadabif627cf82013-09-09 14:08:20 -07003346
3347 for (i = 0; i < (sparse_header->blk_sz / sizeof(fill_val)); i++)
3348 {
3349 fill_buf[i] = fill_val;
3350 }
3351
Gaurav Nebhwanie94cbe12016-03-17 21:16:34 +05303352 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz))
3353 {
3354 fastboot_fail("bogus size for chunk FILL type");
3355 free(fill_buf);
3356 return;
3357 }
3358
wufeng.jiang813dc352015-06-02 23:02:46 -04003359 for (i = 0; i < chunk_header->chunk_sz; i++)
Channagoud Kadabif627cf82013-09-09 14:08:20 -07003360 {
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05303361 /* Make sure that the data written to partition does not exceed partition size */
3362 if ((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz + sparse_header->blk_sz > size)
3363 {
3364 fastboot_fail("Chunk data size for fill type exceeds partition size");
Gaurav Nebhwani163cbf82016-03-17 21:21:27 +05303365 free(fill_buf);
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05303366 return;
3367 }
3368
Channagoud Kadabif627cf82013-09-09 14:08:20 -07003369 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
3370 sparse_header->blk_sz,
3371 fill_buf))
3372 {
3373 fastboot_fail("flash write failure");
3374 free(fill_buf);
3375 return;
3376 }
3377
3378 total_blocks++;
3379 }
3380
3381 free(fill_buf);
3382 break;
3383
Ajay Dudani5c761132011-04-07 20:19:04 -07003384 case CHUNK_TYPE_DONT_CARE:
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05303385 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
3386 fastboot_fail("bogus size for chunk DONT CARE type");
3387 return;
3388 }
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07003389 total_blocks += chunk_header->chunk_sz;
3390 break;
3391
Ajay Dudani5c761132011-04-07 20:19:04 -07003392 case CHUNK_TYPE_CRC:
3393 if(chunk_header->total_sz != sparse_header->chunk_hdr_sz)
3394 {
wufeng.jiang813dc352015-06-02 23:02:46 -04003395 fastboot_fail("Bogus chunk size for chunk type CRC");
Ajay Dudani5c761132011-04-07 20:19:04 -07003396 return;
3397 }
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05303398 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
3399 fastboot_fail("bogus size for chunk CRC type");
3400 return;
3401 }
Ajay Dudani5c761132011-04-07 20:19:04 -07003402 total_blocks += chunk_header->chunk_sz;
Parth Dixit64b1a482016-03-07 16:31:26 +05303403 if ((uintptr_t)data > UINT_MAX - chunk_data_sz) {
vijay kumar800255e2015-04-24 20:26:19 +05303404 fastboot_fail("integer overflow occured");
3405 return;
3406 }
wufeng.jiang813dc352015-06-02 23:02:46 -04003407 data += (uint32_t)chunk_data_sz;
Parth Dixit64b1a482016-03-07 16:31:26 +05303408 if (data_end < (uintptr_t)data) {
vijay kumar800255e2015-04-24 20:26:19 +05303409 fastboot_fail("buffer overreads occured due to invalid sparse header");
3410 return;
3411 }
Ajay Dudani5c761132011-04-07 20:19:04 -07003412 break;
3413
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07003414 default:
Channagoud Kadabif627cf82013-09-09 14:08:20 -07003415 dprintf(CRITICAL, "Unkown chunk type: %x\n",chunk_header->chunk_type);
Ajay Dudani5c761132011-04-07 20:19:04 -07003416 fastboot_fail("Unknown chunk type");
3417 return;
3418 }
3419 }
3420
Ajay Dudani0c6927b2011-05-18 11:12:16 -07003421 dprintf(INFO, "Wrote %d blocks, expected to write %d blocks\n",
3422 total_blocks, sparse_header->total_blks);
3423
3424 if(total_blocks != sparse_header->total_blks)
3425 {
3426 fastboot_fail("sparse image write failure");
3427 }
Ajay Dudani5c761132011-04-07 20:19:04 -07003428
3429 fastboot_okay("");
3430 return;
3431}
3432
3433void cmd_flash_mmc(const char *arg, void *data, unsigned sz)
3434{
3435 sparse_header_t *sparse_header;
Ajay Dudanide984792015-03-02 09:57:41 -08003436 meta_header_t *meta_header;
Ajay Dudani5c761132011-04-07 20:19:04 -07003437
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07003438#ifdef SSD_ENABLE
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -08003439 /* 8 Byte Magic + 2048 Byte xml + Encrypted Data */
3440 unsigned int *magic_number = (unsigned int *) data;
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08003441 int ret=0;
3442 uint32 major_version=0;
3443 uint32 minor_version=0;
3444
3445 ret = scm_svc_version(&major_version,&minor_version);
3446 if(!ret)
3447 {
3448 if(major_version >= 2)
3449 {
Amir Samuelovbb65ce02013-05-05 12:20:18 +03003450 if( !strcmp(arg, "ssd") || !strcmp(arg, "tqs") )
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08003451 {
3452 ret = encrypt_scm((uint32 **) &data, &sz);
3453 if (ret != 0) {
3454 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
3455 return;
3456 }
3457
Amir Samuelovbb65ce02013-05-05 12:20:18 +03003458 /* Protect only for SSD */
3459 if (!strcmp(arg, "ssd")) {
3460 ret = scm_protect_keystore((uint32 *) data, sz);
3461 if (ret != 0) {
3462 dprintf(CRITICAL, "ERROR: scm_protect_keystore Failed\n");
3463 return;
3464 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08003465 }
3466 }
3467 else
3468 {
3469 ret = decrypt_scm_v2((uint32 **) &data, &sz);
3470 if(ret != 0)
3471 {
3472 dprintf(CRITICAL,"ERROR: Decryption Failure\n");
3473 return;
3474 }
3475 }
3476 }
3477 else
3478 {
3479 if (magic_number[0] == DECRYPT_MAGIC_0 &&
3480 magic_number[1] == DECRYPT_MAGIC_1)
3481 {
3482 ret = decrypt_scm((uint32 **) &data, &sz);
3483 if (ret != 0) {
3484 dprintf(CRITICAL, "ERROR: Invalid secure image\n");
3485 return;
3486 }
3487 }
3488 else if (magic_number[0] == ENCRYPT_MAGIC_0 &&
3489 magic_number[1] == ENCRYPT_MAGIC_1)
3490 {
3491 ret = encrypt_scm((uint32 **) &data, &sz);
3492 if (ret != 0) {
3493 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
3494 return;
3495 }
3496 }
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07003497 }
3498 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08003499 else
Neeti Desai127b9e02012-03-20 16:11:23 -07003500 {
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08003501 dprintf(CRITICAL,"INVALID SVC Version\n");
3502 return;
Neeti Desai127b9e02012-03-20 16:11:23 -07003503 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08003504#endif /* SSD_ENABLE */
Neeti Desai127b9e02012-03-20 16:11:23 -07003505
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003506#if VERIFIED_BOOT
Channagoud Kadabi35297672015-06-13 11:09:49 -07003507 if (target_build_variant_user())
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003508 {
lijuang511a2b52015-08-14 20:50:51 +08003509 /* if device is locked:
3510 * common partition will not allow to be flashed
3511 * critical partition will allow to flash image.
3512 */
3513 if(!device.is_unlocked && !critical_flash_allowed(arg)) {
3514 fastboot_fail("Partition flashing is not allowed");
3515 return;
3516 }
Mayank Grover889be1b2017-09-12 20:12:23 +05303517
lijuang511a2b52015-08-14 20:50:51 +08003518 /* if device critical is locked:
3519 * common partition will allow to be flashed
3520 * critical partition will not allow to flash image.
3521 */
Mayank Grover912eaa62017-10-26 12:08:53 +05303522 if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05303523 !device.is_unlock_critical &&
3524 critical_flash_allowed(arg)) {
3525 fastboot_fail("Critical partition flashing is not allowed");
3526 return;
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003527 }
3528 }
3529#endif
3530
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07003531 sparse_header = (sparse_header_t *) data;
Ajay Dudanide984792015-03-02 09:57:41 -08003532 meta_header = (meta_header_t *) data;
3533 if (sparse_header->magic == SPARSE_HEADER_MAGIC)
Ajay Dudani5c761132011-04-07 20:19:04 -07003534 cmd_flash_mmc_sparse_img(arg, data, sz);
Ajay Dudanide984792015-03-02 09:57:41 -08003535 else if (meta_header->magic == META_HEADER_MAGIC)
3536 cmd_flash_meta_img(arg, data, sz);
3537 else
3538 cmd_flash_mmc_img(arg, data, sz);
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07003539
3540#if VERIFIED_BOOT
Mayank Grover912eaa62017-10-26 12:08:53 +05303541 if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05303542 (!strncmp(arg, "system", 6)) &&
3543 !device.verity_mode)
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07003544 // reset dm_verity mode to enforcing
3545 device.verity_mode = 1;
3546 write_device_info(&device);
Parth Dixitddbc7352015-10-18 03:13:31 +05303547#endif
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07003548
Ajay Dudani5c761132011-04-07 20:19:04 -07003549 return;
3550}
3551
Tanya Brokhman84eaaf62015-02-24 21:57:06 +02003552void cmd_updatevol(const char *vol_name, void *data, unsigned sz)
3553{
3554 struct ptentry *sys_ptn;
3555 struct ptable *ptable;
3556
3557 ptable = flash_get_ptable();
3558 if (ptable == NULL) {
3559 fastboot_fail("partition table doesn't exist");
3560 return;
3561 }
3562
3563 sys_ptn = ptable_find(ptable, "system");
3564 if (sys_ptn == NULL) {
3565 fastboot_fail("system partition not found");
3566 return;
3567 }
3568
3569 sz = ROUND_TO_PAGE(sz, page_mask);
3570 if (update_ubi_vol(sys_ptn, vol_name, data, sz))
3571 fastboot_fail("update_ubi_vol failed");
3572 else
3573 fastboot_okay("");
3574}
3575
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003576void cmd_flash_nand(const char *arg, void *data, unsigned sz)
Dima Zavin214cc642009-01-26 11:16:21 -08003577{
3578 struct ptentry *ptn;
3579 struct ptable *ptable;
3580 unsigned extra = 0;
Gaurav Nebhwanid9730712016-05-06 14:28:29 +05303581 uint64_t partition_size = 0;
Vijay Kumar Pendotic6f22a72017-05-19 22:44:34 +05303582 unsigned bytes_to_round_page = 0;
3583 unsigned rounded_size = 0;
Dima Zavin214cc642009-01-26 11:16:21 -08003584
Kishor PK38ed93d2017-04-25 14:19:26 +05303585 if((uintptr_t)data > (UINT_MAX - sz)) {
3586 fastboot_fail("Cannot flash: image header corrupt");
3587 return;
3588 }
3589
Dima Zavin214cc642009-01-26 11:16:21 -08003590 ptable = flash_get_ptable();
3591 if (ptable == NULL) {
3592 fastboot_fail("partition table doesn't exist");
3593 return;
3594 }
3595
3596 ptn = ptable_find(ptable, arg);
3597 if (ptn == NULL) {
Tanya Brokhman84eaaf62015-02-24 21:57:06 +02003598 dprintf(INFO, "unknown partition name (%s). Trying updatevol\n",
3599 arg);
3600 cmd_updatevol(arg, data, sz);
Dima Zavin214cc642009-01-26 11:16:21 -08003601 return;
3602 }
3603
3604 if (!strcmp(ptn->name, "boot") || !strcmp(ptn->name, "recovery")) {
Kishor PK38ed93d2017-04-25 14:19:26 +05303605 if((sz > BOOT_MAGIC_SIZE) && (!memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE))) {
3606 dprintf(INFO, "Verified the BOOT_MAGIC in image header \n");
3607 } else {
3608 fastboot_fail("Image is not a boot image");
Dima Zavin214cc642009-01-26 11:16:21 -08003609 return;
3610 }
3611 }
3612
Amol Jadi5c61a952012-05-04 17:05:35 -07003613 if (!strcmp(ptn->name, "system")
Deepa Dinamani13e32c42012-03-12 14:34:17 -07003614 || !strcmp(ptn->name, "userdata")
3615 || !strcmp(ptn->name, "persist")
Sundarajan Srinivasanb063a852013-11-19 14:02:27 -08003616 || !strcmp(ptn->name, "recoveryfs")
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -08003617 || !strcmp(ptn->name, "modem"))
Tanya Brokhman1c94f1a2015-02-15 09:05:03 +02003618 extra = 1;
Kishor PK38ed93d2017-04-25 14:19:26 +05303619 else {
Vijay Kumar Pendotic6f22a72017-05-19 22:44:34 +05303620 rounded_size = ROUNDUP(sz, page_size);
3621 bytes_to_round_page = rounded_size - sz;
3622 if (bytes_to_round_page) {
3623 if (((uintptr_t)data + sz ) > (UINT_MAX - bytes_to_round_page)) {
3624 fastboot_fail("Integer overflow detected");
3625 return;
3626 }
3627 if (((uintptr_t)data + sz + bytes_to_round_page) >
3628 ((uintptr_t)target_get_scratch_address() + target_get_max_flash_size())) {
3629 fastboot_fail("Buffer size is not aligned to page_size");
3630 return;
3631 }
3632 else {
3633 memset(data + sz, 0, bytes_to_round_page);
3634 sz = rounded_size;
3635 }
Kishor PK38ed93d2017-04-25 14:19:26 +05303636 }
Gaurav Nebhwanid9730712016-05-06 14:28:29 +05303637 }
3638
Kishor PK38ed93d2017-04-25 14:19:26 +05303639 /*Checking partition_size for the possible integer overflow */
3640 partition_size = validate_partition_size(ptn);
3641
Gaurav Nebhwanid9730712016-05-06 14:28:29 +05303642 if (sz > partition_size) {
3643 fastboot_fail("Image size too large");
3644 return;
3645 }
3646
Dima Zavin214cc642009-01-26 11:16:21 -08003647 dprintf(INFO, "writing %d bytes to '%s'\n", sz, ptn->name);
Mayank Grover6cde9352017-06-06 18:45:23 +05303648 if ((sz > UBI_EC_HDR_SIZE) &&
3649 (!memcmp((void *)data, UBI_MAGIC, UBI_MAGIC_SIZE))) {
Tanya Brokhman1c94f1a2015-02-15 09:05:03 +02003650 if (flash_ubi_img(ptn, data, sz)) {
3651 fastboot_fail("flash write failure");
3652 return;
3653 }
3654 } else {
3655 if (flash_write(ptn, extra, data, sz)) {
3656 fastboot_fail("flash write failure");
3657 return;
3658 }
Dima Zavin214cc642009-01-26 11:16:21 -08003659 }
3660 dprintf(INFO, "partition '%s' updated\n", ptn->name);
3661 fastboot_okay("");
3662}
3663
Kishor PK38ed93d2017-04-25 14:19:26 +05303664
3665static inline uint64_t validate_partition_size(struct ptentry *ptn)
3666{
3667 if (ptn->length && flash_num_pages_per_blk() && page_size) {
3668 if ((ptn->length < ( UINT_MAX / flash_num_pages_per_blk())) && ((ptn->length * flash_num_pages_per_blk()) < ( UINT_MAX / page_size))) {
3669 return ptn->length * flash_num_pages_per_blk() * page_size;
3670 }
3671 }
3672 return 0;
3673}
3674
3675
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003676void cmd_flash(const char *arg, void *data, unsigned sz)
3677{
3678 if(target_is_emmc_boot())
3679 cmd_flash_mmc(arg, data, sz);
3680 else
3681 cmd_flash_nand(arg, data, sz);
3682}
3683
Dima Zavin214cc642009-01-26 11:16:21 -08003684void cmd_continue(const char *arg, void *data, unsigned sz)
3685{
3686 fastboot_okay("");
Amol Jadi7c4316c2013-10-07 14:19:26 -07003687 fastboot_stop();
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003688
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003689 if (target_is_emmc_boot())
3690 {
lijuanga40d6302015-07-20 20:10:13 +08003691#if FBCON_DISPLAY_MSG
lijuangde34d502016-02-26 16:04:50 +08003692 /* Exit keys' detection thread firstly */
3693 exit_menu_keys_detection();
lijuanga40d6302015-07-20 20:10:13 +08003694#endif
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003695 boot_linux_from_mmc();
3696 }
3697 else
3698 {
3699 boot_linux_from_flash();
3700 }
Dima Zavin214cc642009-01-26 11:16:21 -08003701}
3702
Chandan Uddaraju94183c02010-01-15 15:13:59 -08003703void cmd_reboot(const char *arg, void *data, unsigned sz)
3704{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003705 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08003706 fastboot_okay("");
3707 reboot_device(0);
3708}
3709
Mayank Grover351a75e2017-05-30 20:06:08 +05303710void cmd_set_active(const char *arg, void *data, unsigned sz)
3711{
Mayank Grover6ccc1c92017-07-04 17:36:46 +05303712 char *p, *sp = NULL;
Mayank Grover351a75e2017-05-30 20:06:08 +05303713 unsigned i,current_active_slot;
3714 const char *current_slot_suffix;
3715
3716 if (!partition_multislot_is_supported())
3717 {
3718 fastboot_fail("Command not supported");
3719 return;
3720 }
3721
3722 if (arg)
3723 {
Mayank Grover6ccc1c92017-07-04 17:36:46 +05303724 p = strtok_r((char *)arg, ":", &sp);
Mayank Grover5eb5f692017-07-19 20:16:04 +05303725 if (p)
Mayank Grover351a75e2017-05-30 20:06:08 +05303726 {
3727 current_active_slot = partition_find_active_slot();
3728
3729 /* Check if trying to make curent slot active */
3730 current_slot_suffix = SUFFIX_SLOT(current_active_slot);
Mayank Grover6ccc1c92017-07-04 17:36:46 +05303731 current_slot_suffix = strtok_r((char *)current_slot_suffix,
3732 (char *)suffix_delimiter, &sp);
3733
Mayank Grover5eb5f692017-07-19 20:16:04 +05303734 if (current_slot_suffix &&
3735 !strncmp(p, current_slot_suffix, strlen(current_slot_suffix)))
Mayank Grover351a75e2017-05-30 20:06:08 +05303736 {
3737 fastboot_okay("Slot already set active");
3738 return;
3739 }
3740 else
3741 {
3742 for (i = 0; i < AB_SUPPORTED_SLOTS; i++)
3743 {
3744 current_slot_suffix = SUFFIX_SLOT(i);
Mayank Grover6ccc1c92017-07-04 17:36:46 +05303745 current_slot_suffix = strtok_r((char *)current_slot_suffix,
3746 (char *)suffix_delimiter, &sp);
Mayank Grover5eb5f692017-07-19 20:16:04 +05303747 if (current_slot_suffix &&
3748 !strncmp(p, current_slot_suffix, strlen(current_slot_suffix)))
Mayank Grover351a75e2017-05-30 20:06:08 +05303749 {
3750 partition_switch_slots(current_active_slot, i);
3751 publish_getvar_multislot_vars();
3752 fastboot_okay("");
3753 return;
3754 }
3755 }
3756 }
3757 }
3758 }
3759 fastboot_fail("Invalid slot suffix.");
3760 return;
3761}
3762
Chandan Uddaraju94183c02010-01-15 15:13:59 -08003763void cmd_reboot_bootloader(const char *arg, void *data, unsigned sz)
3764{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003765 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08003766 fastboot_okay("");
3767 reboot_device(FASTBOOT_MODE);
3768}
3769
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003770void cmd_oem_enable_charger_screen(const char *arg, void *data, unsigned size)
3771{
3772 dprintf(INFO, "Enabling charger screen check\n");
3773 device.charger_screen_enabled = 1;
3774 write_device_info(&device);
3775 fastboot_okay("");
3776}
3777
3778void cmd_oem_disable_charger_screen(const char *arg, void *data, unsigned size)
3779{
3780 dprintf(INFO, "Disabling charger screen check\n");
3781 device.charger_screen_enabled = 0;
3782 write_device_info(&device);
3783 fastboot_okay("");
3784}
3785
lijuanga25d8bb2015-09-16 13:11:52 +08003786void cmd_oem_off_mode_charger(const char *arg, void *data, unsigned size)
3787{
3788 char *p = NULL;
3789 const char *delim = " \t\n\r";
Parth Dixit407f15b2016-01-07 14:47:37 +05303790 char *sp;
lijuanga25d8bb2015-09-16 13:11:52 +08003791
3792 if (arg) {
Parth Dixit407f15b2016-01-07 14:47:37 +05303793 p = strtok_r((char *)arg, delim, &sp);
lijuanga25d8bb2015-09-16 13:11:52 +08003794 if (p) {
3795 if (!strncmp(p, "0", 1)) {
3796 device.charger_screen_enabled = 0;
3797 } else if (!strncmp(p, "1", 1)) {
3798 device.charger_screen_enabled = 1;
3799 }
3800 }
3801 }
3802
3803 /* update charger_screen_enabled value for getvar
3804 * command
3805 */
3806 snprintf(charger_screen_enabled, MAX_RSP_SIZE, "%d",
3807 device.charger_screen_enabled);
3808
3809 write_device_info(&device);
3810 fastboot_okay("");
3811}
3812
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05303813void cmd_oem_select_display_panel(const char *arg, void *data, unsigned size)
3814{
3815 dprintf(INFO, "Selecting display panel %s\n", arg);
3816 if (arg)
3817 strlcpy(device.display_panel, arg,
3818 sizeof(device.display_panel));
3819 write_device_info(&device);
3820 fastboot_okay("");
3821}
3822
Shashank Mittal162244e2011-08-08 19:01:25 -07003823void cmd_oem_unlock(const char *arg, void *data, unsigned sz)
3824{
lijuang4ece1e72015-08-14 21:02:36 +08003825 set_device_unlock(UNLOCK, TRUE);
vijay kumarc65876c2015-04-24 13:29:16 +05303826}
3827
3828void cmd_oem_unlock_go(const char *arg, void *data, unsigned sz)
3829{
lijuang4ece1e72015-08-14 21:02:36 +08003830 if(!device.is_unlocked) {
vijay kumarc65876c2015-04-24 13:29:16 +05303831 if(!is_allow_unlock) {
3832 fastboot_fail("oem unlock is not allowed");
3833 return;
3834 }
3835
lijuang4ece1e72015-08-14 21:02:36 +08003836 set_device_unlock_value(UNLOCK, TRUE);
vijay kumarc65876c2015-04-24 13:29:16 +05303837
lijuang4ece1e72015-08-14 21:02:36 +08003838 /* wipe data */
vijay kumarc65876c2015-04-24 13:29:16 +05303839 struct recovery_message msg;
Kishor PK60a68212017-05-08 16:55:57 +05303840 memset(&msg, 0, sizeof(msg));
vijay kumarc65876c2015-04-24 13:29:16 +05303841 snprintf(msg.recovery, sizeof(msg.recovery), "recovery\n--wipe_data");
3842 write_misc(0, &msg, sizeof(msg));
3843
3844 fastboot_okay("");
3845 reboot_device(RECOVERY_MODE);
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003846 }
3847 fastboot_okay("");
3848}
3849
Channagoud Kadabiad259832015-05-29 11:14:17 -07003850static int aboot_frp_unlock(char *pname, void *data, unsigned sz)
3851{
Mayank Grover70f8b0e2017-05-17 11:49:00 +05303852 int ret=1;
3853 bool authentication_success=false;
Channagoud Kadabiad259832015-05-29 11:14:17 -07003854
Mayank Grover70f8b0e2017-05-17 11:49:00 +05303855 /*
3856 Authentication method not implemented.
3857
3858 OEM to implement, authentication system which on successful validataion,
3859 calls write_allow_oem_unlock() with is_allow_unlock.
3860 */
3861#if 0
3862 authentication_success = oem_specific_auth_mthd();
3863#endif
3864
3865 if (authentication_success)
Channagoud Kadabiad259832015-05-29 11:14:17 -07003866 {
Mayank Grover70f8b0e2017-05-17 11:49:00 +05303867 is_allow_unlock = true;
3868 write_allow_oem_unlock(is_allow_unlock);
3869 ret = 0;
Channagoud Kadabiad259832015-05-29 11:14:17 -07003870 }
3871 return ret;
3872}
3873
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003874void cmd_oem_lock(const char *arg, void *data, unsigned sz)
3875{
lijuang4ece1e72015-08-14 21:02:36 +08003876 set_device_unlock(UNLOCK, FALSE);
Shashank Mittal162244e2011-08-08 19:01:25 -07003877}
3878
Shashank Mittala0032282011-08-26 14:50:11 -07003879void cmd_oem_devinfo(const char *arg, void *data, unsigned sz)
3880{
lijuang511a2b52015-08-14 20:50:51 +08003881 char response[MAX_RSP_SIZE];
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003882 snprintf(response, sizeof(response), "\tDevice tampered: %s", (device.is_tampered ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07003883 fastboot_info(response);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003884 snprintf(response, sizeof(response), "\tDevice unlocked: %s", (device.is_unlocked ? "true" : "false"));
3885 fastboot_info(response);
Mayank Grover889be1b2017-09-12 20:12:23 +05303886#if VERIFIED_BOOT
Mayank Grover912eaa62017-10-26 12:08:53 +05303887 if (VB_M <= target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +05303888 {
3889 snprintf(response, sizeof(response), "\tDevice critical unlocked: %s",
3890 (device.is_unlock_critical ? "true" : "false"));
3891 fastboot_info(response);
3892 }
Parth Dixitddbc7352015-10-18 03:13:31 +05303893#endif
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003894 snprintf(response, sizeof(response), "\tCharger screen enabled: %s", (device.charger_screen_enabled ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07003895 fastboot_info(response);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05303896 snprintf(response, sizeof(response), "\tDisplay panel: %s", (device.display_panel));
3897 fastboot_info(response);
Shashank Mittala0032282011-08-26 14:50:11 -07003898 fastboot_okay("");
3899}
3900
lijuang511a2b52015-08-14 20:50:51 +08003901void cmd_flashing_get_unlock_ability(const char *arg, void *data, unsigned sz)
3902{
3903 char response[MAX_RSP_SIZE];
3904 snprintf(response, sizeof(response), "\tget_unlock_ability: %d", is_allow_unlock);
3905 fastboot_info(response);
3906 fastboot_okay("");
3907}
3908
3909void cmd_flashing_lock_critical(const char *arg, void *data, unsigned sz)
3910{
lijuang4ece1e72015-08-14 21:02:36 +08003911 set_device_unlock(UNLOCK_CRITICAL, FALSE);
lijuang511a2b52015-08-14 20:50:51 +08003912}
3913
3914void cmd_flashing_unlock_critical(const char *arg, void *data, unsigned sz)
3915{
lijuang4ece1e72015-08-14 21:02:36 +08003916 set_device_unlock(UNLOCK_CRITICAL, TRUE);
lijuang511a2b52015-08-14 20:50:51 +08003917}
3918
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07003919void cmd_preflash(const char *arg, void *data, unsigned sz)
3920{
3921 fastboot_okay("");
3922}
3923
Mao Flynn7b379f32015-04-20 00:28:30 +08003924static uint8_t logo_header[LOGO_IMG_HEADER_SIZE];
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303925
Mao Flynn7b379f32015-04-20 00:28:30 +08003926int splash_screen_check_header(logo_img_header *header)
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303927{
Mao Flynn7b379f32015-04-20 00:28:30 +08003928 if (memcmp(header->magic, LOGO_IMG_MAGIC, 8))
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303929 return -1;
Mao Flynn7b379f32015-04-20 00:28:30 +08003930 if (header->width == 0 || header->height == 0)
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303931 return -1;
3932 return 0;
3933}
3934
Mao Flynn7b379f32015-04-20 00:28:30 +08003935int splash_screen_flash()
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003936{
3937 struct ptentry *ptn;
3938 struct ptable *ptable;
Mao Flynn7b379f32015-04-20 00:28:30 +08003939 struct logo_img_header *header;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003940 struct fbcon_config *fb_display = NULL;
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08003941
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303942 ptable = flash_get_ptable();
3943 if (ptable == NULL) {
Mao Flynn7b379f32015-04-20 00:28:30 +08003944 dprintf(CRITICAL, "ERROR: Partition table not found\n");
3945 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303946 }
Mao Flynn7b379f32015-04-20 00:28:30 +08003947
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303948 ptn = ptable_find(ptable, "splash");
3949 if (ptn == NULL) {
3950 dprintf(CRITICAL, "ERROR: splash Partition not found\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003951 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303952 }
Mao Flynn7b379f32015-04-20 00:28:30 +08003953 if (flash_read(ptn, 0, (void *)logo_header, LOGO_IMG_HEADER_SIZE)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303954 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003955 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303956 }
3957
Mao Flynn7b379f32015-04-20 00:28:30 +08003958 header = (struct logo_img_header *)logo_header;
3959 if (splash_screen_check_header(header)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303960 dprintf(CRITICAL, "ERROR: Boot image header invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003961 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303962 }
3963
3964 fb_display = fbcon_display();
3965 if (fb_display) {
Mao Flynn7b379f32015-04-20 00:28:30 +08003966 if (header->type && (header->blocks != 0)) { // RLE24 compressed data
3967 uint8_t *base = (uint8_t *) fb_display->base + LOGO_IMG_OFFSET;
3968
3969 /* if the logo is full-screen size, remove "fbcon_clear()" */
3970 if ((header->width != fb_display->width)
3971 || (header->height != fb_display->height))
3972 fbcon_clear();
3973
3974 if (flash_read(ptn + LOGO_IMG_HEADER_SIZE, 0,
3975 (uint32_t *)base,
3976 (header->blocks * 512))) {
3977 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
3978 return -1;
3979 }
3980 fbcon_extract_to_screen(header, base);
3981 return 0;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003982 }
Mao Flynn7b379f32015-04-20 00:28:30 +08003983
Veera Sundaram Sankaranc33416f2015-09-28 10:25:55 -07003984 if ((header->width > fb_display->width) || (header->height > fb_display->height)) {
3985 dprintf(CRITICAL, "Logo config greater than fb config. Fall back default logo\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003986 return -1;
3987 }
3988
Vineet Bajaj99291ed2014-09-09 12:29:46 +05303989 uint8_t *base = (uint8_t *) fb_display->base;
Sachin Bhayare619e9e42017-05-15 13:10:31 +05303990 uint32_t fb_size = ROUNDUP(fb_display->width *
3991 fb_display->height *
3992 (fb_display->bpp / 8), 4096);
3993 uint32_t splash_size = ((((header->width * header->height *
3994 fb_display->bpp/8) + 511) >> 9) << 9);
3995
3996 if (splash_size > fb_size) {
3997 dprintf(CRITICAL, "ERROR: Splash image size invalid\n");
3998 return -1;
3999 }
4000
Mao Flynn7b379f32015-04-20 00:28:30 +08004001 if (flash_read(ptn + LOGO_IMG_HEADER_SIZE, 0,
4002 (uint32_t *)base,
4003 ((((header->width * header->height * fb_display->bpp/8) + 511) >> 9) << 9))) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304004 fbcon_clear();
Vineet Bajaj99291ed2014-09-09 12:29:46 +05304005 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08004006 return -1;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07004007 }
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304008 }
4009
Mao Flynn7b379f32015-04-20 00:28:30 +08004010 return 0;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304011}
4012
Mao Flynn7b379f32015-04-20 00:28:30 +08004013int splash_screen_mmc()
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304014{
4015 int index = INVALID_PTN;
4016 unsigned long long ptn = 0;
4017 struct fbcon_config *fb_display = NULL;
Mao Flynn7b379f32015-04-20 00:28:30 +08004018 struct logo_img_header *header;
Mao Flynn1893a7f2015-06-03 12:03:36 +08004019 uint32_t blocksize, realsize, readsize;
4020 uint8_t *base;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304021
4022 index = partition_get_index("splash");
4023 if (index == 0) {
4024 dprintf(CRITICAL, "ERROR: splash Partition table not found\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08004025 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304026 }
4027
4028 ptn = partition_get_offset(index);
4029 if (ptn == 0) {
4030 dprintf(CRITICAL, "ERROR: splash Partition invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08004031 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304032 }
4033
Mao Flynn1893a7f2015-06-03 12:03:36 +08004034 mmc_set_lun(partition_get_lun(index));
4035
4036 blocksize = mmc_get_device_blocksize();
4037 if (blocksize == 0) {
4038 dprintf(CRITICAL, "ERROR:splash Partition invalid blocksize\n");
4039 return -1;
4040 }
4041
4042 fb_display = fbcon_display();
Channagoud Kadabidaf10a62015-07-22 11:51:55 -07004043 if (!fb_display)
4044 {
4045 dprintf(CRITICAL, "ERROR: fb config is not allocated\n");
4046 return -1;
4047 }
4048
Mao Flynn1893a7f2015-06-03 12:03:36 +08004049 base = (uint8_t *) fb_display->base;
4050
4051 if (mmc_read(ptn, (uint32_t *)(base + LOGO_IMG_OFFSET), blocksize)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304052 dprintf(CRITICAL, "ERROR: Cannot read splash image header\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08004053 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304054 }
4055
Mao Flynn1893a7f2015-06-03 12:03:36 +08004056 header = (struct logo_img_header *)(base + LOGO_IMG_OFFSET);
Mao Flynn7b379f32015-04-20 00:28:30 +08004057 if (splash_screen_check_header(header)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304058 dprintf(CRITICAL, "ERROR: Splash image header invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08004059 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304060 }
4061
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304062 if (fb_display) {
Mao Flynn1893a7f2015-06-03 12:03:36 +08004063 if (header->type && (header->blocks != 0)) { /* 1 RLE24 compressed data */
4064 base += LOGO_IMG_OFFSET;
Mao Flynn7b379f32015-04-20 00:28:30 +08004065
Mao Flynn1893a7f2015-06-03 12:03:36 +08004066 realsize = header->blocks * 512;
4067 readsize = ROUNDUP((realsize + LOGO_IMG_HEADER_SIZE), blocksize) - blocksize;
4068
4069 /* if the logo is not full-screen size, clean screen */
Mao Flynn7b379f32015-04-20 00:28:30 +08004070 if ((header->width != fb_display->width)
4071 || (header->height != fb_display->height))
4072 fbcon_clear();
4073
Sachin Bhayare619e9e42017-05-15 13:10:31 +05304074 uint32_t fb_size = ROUNDUP(fb_display->width *
4075 fb_display->height *
4076 (fb_display->bpp / 8), 4096);
4077
4078 if (readsize > fb_size) {
4079 dprintf(CRITICAL, "ERROR: Splash image size invalid\n");
4080 return -1;
4081 }
4082
Mao Flynn1893a7f2015-06-03 12:03:36 +08004083 if (mmc_read(ptn + blocksize, (uint32_t *)(base + blocksize), readsize)) {
Mao Flynn7b379f32015-04-20 00:28:30 +08004084 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
4085 return -1;
4086 }
Mao Flynn7b379f32015-04-20 00:28:30 +08004087
Mao Flynn1893a7f2015-06-03 12:03:36 +08004088 fbcon_extract_to_screen(header, (base + LOGO_IMG_HEADER_SIZE));
4089 } else { /* 2 Raw BGR data */
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304090
Veera Sundaram Sankaranc33416f2015-09-28 10:25:55 -07004091 if ((header->width > fb_display->width) || (header->height > fb_display->height)) {
4092 dprintf(CRITICAL, "Logo config greater than fb config. Fall back default logo\n");
Mao Flynn1893a7f2015-06-03 12:03:36 +08004093 return -1;
4094 }
4095
4096 realsize = header->width * header->height * fb_display->bpp / 8;
4097 readsize = ROUNDUP((realsize + LOGO_IMG_HEADER_SIZE), blocksize) - blocksize;
4098
4099 if (blocksize == LOGO_IMG_HEADER_SIZE) { /* read the content directly */
4100 if (mmc_read((ptn + LOGO_IMG_HEADER_SIZE), (uint32_t *)base, readsize)) {
4101 fbcon_clear();
4102 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
4103 return -1;
4104 }
4105 } else {
4106 if (mmc_read(ptn + blocksize ,
4107 (uint32_t *)(base + LOGO_IMG_OFFSET + blocksize), readsize)) {
4108 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
4109 return -1;
4110 }
4111 memmove(base, (base + LOGO_IMG_OFFSET + LOGO_IMG_HEADER_SIZE), realsize);
4112 }
4113 }
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304114 }
4115
Mao Flynn7b379f32015-04-20 00:28:30 +08004116 return 0;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304117}
4118
Mao Flynn7b379f32015-04-20 00:28:30 +08004119int fetch_image_from_partition()
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304120{
4121 if (target_is_emmc_boot()) {
4122 return splash_screen_mmc();
4123 } else {
4124 return splash_screen_flash();
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07004125 }
4126}
4127
Mayank Grover351a75e2017-05-30 20:06:08 +05304128void publish_getvar_multislot_vars()
4129{
4130 int i,count;
4131 static bool published = false;
4132 static char slot_count[MAX_RSP_SIZE];
4133 static struct ab_slot_info slot_info[AB_SUPPORTED_SLOTS];
4134 static char active_slot_suffix[MAX_RSP_SIZE];
4135 static char has_slot_pname[NUM_PARTITIONS][MAX_GET_VAR_NAME_SIZE];
4136 static char has_slot_reply[NUM_PARTITIONS][MAX_RSP_SIZE];
4137 const char *tmp;
4138 char tmpbuff[MAX_GET_VAR_NAME_SIZE];
Mayank Grover2bd84bb2017-06-20 11:06:07 +05304139 signed active_slt;
Mayank Grover351a75e2017-05-30 20:06:08 +05304140
4141 if (!published)
4142 {
4143 /* Update slot meta info */
4144 count = partition_fill_partition_meta(has_slot_pname, has_slot_reply,
4145 partition_get_partition_count());
4146 for(i=0; i<count; i++)
4147 {
4148 memset(tmpbuff, 0, MAX_GET_VAR_NAME_SIZE);
Mayank Grover2bd84bb2017-06-20 11:06:07 +05304149 snprintf(tmpbuff, MAX_GET_VAR_NAME_SIZE,"has-slot:%s",
4150 has_slot_pname[i]);
4151 strlcpy(has_slot_pname[i], tmpbuff, MAX_GET_VAR_NAME_SIZE);
Mayank Grover351a75e2017-05-30 20:06:08 +05304152 fastboot_publish(has_slot_pname[i], has_slot_reply[i]);
4153 }
4154
4155 for (i=0; i<AB_SUPPORTED_SLOTS; i++)
4156 {
4157 tmp = SUFFIX_SLOT(i);
Mayank Grover7b8a8f82017-10-27 13:07:59 +05304158 tmp++; // to remove "_" from slot_suffix.
Mayank Grover2bd84bb2017-06-20 11:06:07 +05304159 snprintf(slot_info[i].slot_is_unbootable, sizeof(slot_info[i].slot_is_unbootable),
4160 "slot-unbootable:%s", tmp);
4161 snprintf(slot_info[i].slot_is_active, sizeof(slot_info[i].slot_is_active),
4162 "slot-active:%s", tmp);
4163 snprintf(slot_info[i].slot_is_succesful, sizeof(slot_info[i].slot_is_succesful),
4164 "slot-success:%s", tmp);
4165 snprintf(slot_info[i].slot_retry_count, sizeof(slot_info[i].slot_retry_count),
4166 "slot-retry-count:%s", tmp);
Mayank Grover351a75e2017-05-30 20:06:08 +05304167 fastboot_publish(slot_info[i].slot_is_unbootable,
4168 slot_info[i].slot_is_unbootable_rsp);
4169 fastboot_publish(slot_info[i].slot_is_active,
4170 slot_info[i].slot_is_active_rsp);
4171 fastboot_publish(slot_info[i].slot_is_succesful,
4172 slot_info[i].slot_is_succesful_rsp);
4173 fastboot_publish(slot_info[i].slot_retry_count,
4174 slot_info[i].slot_retry_count_rsp);
4175 }
4176 fastboot_publish("current-slot", active_slot_suffix);
4177 snprintf(slot_count, sizeof(slot_count),"%d", AB_SUPPORTED_SLOTS);
4178 fastboot_publish("slot-count", slot_count);
4179 published = true;
4180 }
4181
Mayank Grover2bd84bb2017-06-20 11:06:07 +05304182 active_slt = partition_find_active_slot();
4183 if (active_slt != INVALID)
Mayank Grover7b8a8f82017-10-27 13:07:59 +05304184 {
4185 tmp = SUFFIX_SLOT(active_slt);
4186 tmp++; // to remove "_" from slot_suffix.
4187 snprintf(active_slot_suffix, sizeof(active_slot_suffix), "%s", tmp);
4188 }
Mayank Grover2bd84bb2017-06-20 11:06:07 +05304189 else
4190 strlcpy(active_slot_suffix, "INVALID", sizeof(active_slot_suffix));
4191
Mayank Grover351a75e2017-05-30 20:06:08 +05304192 /* Update partition meta information */
4193 partition_fill_slot_meta(slot_info);
4194 return;
4195}
4196
lijuang4ece1e72015-08-14 21:02:36 +08004197void get_product_name(unsigned char *buf)
4198{
4199 snprintf((char*)buf, MAX_RSP_SIZE, "%s", TARGET(BOARD));
4200 return;
4201}
4202
Mayank Grover7b8a8f82017-10-27 13:07:59 +05304203#if PRODUCT_IOT
4204void get_bootloader_version_iot(unsigned char *buf)
4205{
4206 if (buf != NULL)
4207 {
4208 strlcpy(buf, TARGET(BOARD), MAX_VERSION_LEN);
4209 strlcat(buf, "-", MAX_VERSION_LEN);
4210 strlcat(buf, PRODUCT_IOT_VERSION, MAX_VERSION_LEN);
4211 }
4212 return;
4213}
4214#endif
4215
lijuang4ece1e72015-08-14 21:02:36 +08004216void get_bootloader_version(unsigned char *buf)
4217{
4218 snprintf((char*)buf, MAX_RSP_SIZE, "%s", device.bootloader_version);
4219 return;
4220}
4221
4222void get_baseband_version(unsigned char *buf)
4223{
4224 snprintf((char*)buf, MAX_RSP_SIZE, "%s", device.radio_version);
4225 return;
4226}
4227
4228bool is_device_locked()
4229{
4230 return device.is_unlocked ? false:true;
4231}
4232
Amol Jadi5edf3552013-07-23 14:15:34 -07004233/* register commands and variables for fastboot */
4234void aboot_fastboot_register_commands(void)
4235{
Sridhar Parasurame94e8152014-10-24 14:06:03 -07004236 int i;
lijuangf16461c2015-08-03 17:09:34 +08004237 char hw_platform_buf[MAX_RSP_SIZE];
Amol Jadi5edf3552013-07-23 14:15:34 -07004238
Sridhar Parasurame94e8152014-10-24 14:06:03 -07004239 struct fastboot_cmd_desc cmd_list[] = {
lijuang511a2b52015-08-14 20:50:51 +08004240 /* By default the enabled list is empty. */
4241 {"", NULL},
4242 /* move commands enclosed within the below ifndef to here
4243 * if they need to be enabled in user build.
4244 */
Sridhar Parasurame94e8152014-10-24 14:06:03 -07004245#ifndef DISABLE_FASTBOOT_CMDS
lijuang511a2b52015-08-14 20:50:51 +08004246 /* Register the following commands only for non-user builds */
4247 {"flash:", cmd_flash},
4248 {"erase:", cmd_erase},
4249 {"boot", cmd_boot},
4250 {"continue", cmd_continue},
4251 {"reboot", cmd_reboot},
4252 {"reboot-bootloader", cmd_reboot_bootloader},
4253 {"oem unlock", cmd_oem_unlock},
4254 {"oem unlock-go", cmd_oem_unlock_go},
4255 {"oem lock", cmd_oem_lock},
4256 {"flashing unlock", cmd_oem_unlock},
4257 {"flashing lock", cmd_oem_lock},
4258 {"flashing lock_critical", cmd_flashing_lock_critical},
4259 {"flashing unlock_critical", cmd_flashing_unlock_critical},
4260 {"flashing get_unlock_ability", cmd_flashing_get_unlock_ability},
4261 {"oem device-info", cmd_oem_devinfo},
4262 {"preflash", cmd_preflash},
4263 {"oem enable-charger-screen", cmd_oem_enable_charger_screen},
4264 {"oem disable-charger-screen", cmd_oem_disable_charger_screen},
lijuanga25d8bb2015-09-16 13:11:52 +08004265 {"oem off-mode-charge", cmd_oem_off_mode_charger},
lijuang511a2b52015-08-14 20:50:51 +08004266 {"oem select-display-panel", cmd_oem_select_display_panel},
Mayank Grover6ccc1c92017-07-04 17:36:46 +05304267 {"set_active",cmd_set_active},
Channagoud Kadabi736c4962015-08-21 11:56:52 -07004268#if UNITTEST_FW_SUPPORT
Channagoud Kadabid5ddf482015-09-28 10:31:35 -07004269 {"oem run-tests", cmd_oem_runtests},
Channagoud Kadabi736c4962015-08-21 11:56:52 -07004270#endif
Sridhar Parasurame94e8152014-10-24 14:06:03 -07004271#endif
lijuang511a2b52015-08-14 20:50:51 +08004272 };
Sridhar Parasurame94e8152014-10-24 14:06:03 -07004273
4274 int fastboot_cmds_count = sizeof(cmd_list)/sizeof(cmd_list[0]);
4275 for (i = 1; i < fastboot_cmds_count; i++)
4276 fastboot_register(cmd_list[i].name,cmd_list[i].cb);
4277
Amol Jadi5edf3552013-07-23 14:15:34 -07004278 /* publish variables and their values */
4279 fastboot_publish("product", TARGET(BOARD));
4280 fastboot_publish("kernel", "lk");
4281 fastboot_publish("serialno", sn_buf);
4282
4283 /*
4284 * partition info is supported only for emmc partitions
4285 * Calling this for NAND prints some error messages which
4286 * is harmless but misleading. Avoid calling this for NAND
4287 * devices.
4288 */
4289 if (target_is_emmc_boot())
Mayank Grover7b8a8f82017-10-27 13:07:59 +05304290 publish_getvar_partition_info(part_info, partition_get_partition_count());
Amol Jadi5edf3552013-07-23 14:15:34 -07004291
Mayank Grover351a75e2017-05-30 20:06:08 +05304292 if (partition_multislot_is_supported())
4293 publish_getvar_multislot_vars();
4294
Amol Jadi5edf3552013-07-23 14:15:34 -07004295 /* Max download size supported */
Ameya Thakur11cf1a62013-08-05 12:44:48 -07004296 snprintf(max_download_size, MAX_RSP_SIZE, "\t0x%x",
4297 target_get_max_flash_size());
Amol Jadi5edf3552013-07-23 14:15:34 -07004298 fastboot_publish("max-download-size", (const char *) max_download_size);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07004299 /* Is the charger screen check enabled */
4300 snprintf(charger_screen_enabled, MAX_RSP_SIZE, "%d",
4301 device.charger_screen_enabled);
4302 fastboot_publish("charger-screen-enabled",
4303 (const char *) charger_screen_enabled);
lijuanga25d8bb2015-09-16 13:11:52 +08004304 fastboot_publish("off-mode-charge", (const char *) charger_screen_enabled);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05304305 snprintf(panel_display_mode, MAX_RSP_SIZE, "%s",
4306 device.display_panel);
4307 fastboot_publish("display-panel",
4308 (const char *) panel_display_mode);
Mayank Grover7b8a8f82017-10-27 13:07:59 +05304309#if PRODUCT_IOT
4310 get_bootloader_version_iot(&bootloader_version_string);
4311 fastboot_publish("version-bootloader", (const char *) bootloader_version_string);
4312
4313 /* Version baseband is n/a for apq iot devices */
4314 fastboot_publish("version-baseband", "N/A");
4315
4316 /* IOT targets support only mmc target */
4317 snprintf(block_size_string, MAX_RSP_SIZE, "0x%x", mmc_get_device_blocksize());
4318 fastboot_publish("erase-block-size", (const char *) block_size_string);
4319 fastboot_publish("logical-block-size", (const char *) block_size_string);
4320#else
Channagoud Kadabiaafbbfc2015-03-04 17:52:56 -08004321 fastboot_publish("version-bootloader", (const char *) device.bootloader_version);
4322 fastboot_publish("version-baseband", (const char *) device.radio_version);
Mayank Grover7b8a8f82017-10-27 13:07:59 +05304323#endif
lijuangf16461c2015-08-03 17:09:34 +08004324 fastboot_publish("secure", is_secure_boot_enable()? "yes":"no");
Mayank Grover7b8a8f82017-10-27 13:07:59 +05304325 fastboot_publish("unlocked", device.is_unlocked ? "yes":"no");
lijuangf16461c2015-08-03 17:09:34 +08004326 smem_get_hw_platform_name((unsigned char *) hw_platform_buf, sizeof(hw_platform_buf));
4327 snprintf(get_variant, MAX_RSP_SIZE, "%s %s", hw_platform_buf,
4328 target_is_emmc_boot()? "eMMC":"UFS");
4329 fastboot_publish("variant", (const char *) get_variant);
lijuang65c5a822015-08-29 16:35:36 +08004330#if CHECK_BAT_VOLTAGE
lijuang102dfa92015-10-09 18:31:03 +08004331 update_battery_status();
lijuang65c5a822015-08-29 16:35:36 +08004332 fastboot_publish("battery-voltage", (const char *) battery_voltage);
lijuang102dfa92015-10-09 18:31:03 +08004333 fastboot_publish("battery-soc-ok", (const char *) battery_soc_ok);
lijuang65c5a822015-08-29 16:35:36 +08004334#endif
Amol Jadi5edf3552013-07-23 14:15:34 -07004335}
4336
Brian Swetland9c4c0752009-01-25 16:23:50 -08004337void aboot_init(const struct app_descriptor *app)
4338{
Shashank Mittal4f99a882010-02-01 13:58:50 -08004339 unsigned reboot_mode = 0;
Mayank Grover351a75e2017-05-30 20:06:08 +05304340 int boot_err_type = 0;
4341 int boot_slot = INVALID;
Chandan Uddarajubedca152010-06-02 23:05:15 -07004342
lijuang39831732016-01-08 17:49:02 +08004343 /* Initialise wdog to catch early lk crashes */
4344#if WDOG_SUPPORT
4345 msm_wdog_init();
4346#endif
4347
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07004348 /* Setup page size information for nv storage */
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004349 if (target_is_emmc_boot())
4350 {
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07004351 page_size = mmc_page_size();
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004352 page_mask = page_size - 1;
Gaurav Nebhwani4d2389c2016-03-17 21:10:05 +05304353 mmc_blocksize = mmc_get_device_blocksize();
4354 mmc_blocksize_mask = mmc_blocksize - 1;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004355 }
4356 else
4357 {
4358 page_size = flash_page_size();
4359 page_mask = page_size - 1;
4360 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07004361 ASSERT((MEMBASE + MEMSIZE) > MEMBASE);
4362
Ameya Thakur11cf1a62013-08-05 12:44:48 -07004363 read_device_info(&device);
vijay kumarc65876c2015-04-24 13:29:16 +05304364 read_allow_oem_unlock(&device);
Shashank Mittal162244e2011-08-08 19:01:25 -07004365
Mayank Grover351a75e2017-05-30 20:06:08 +05304366 /* Detect multi-slot support */
4367 if (partition_multislot_is_supported())
4368 {
4369 boot_slot = partition_find_active_slot();
4370 if (boot_slot == INVALID)
4371 {
4372 boot_into_fastboot = true;
4373 dprintf(INFO, "Active Slot: (INVALID)\n");
4374 }
4375 else
4376 {
4377 /* Setting the state of system to boot active slot */
4378 partition_mark_active_slot(boot_slot);
4379 dprintf(INFO, "Active Slot: (%s)\n", SUFFIX_SLOT(boot_slot));
4380 }
4381 }
4382
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08004383 /* Display splash screen if enabled */
4384#if DISPLAY_SPLASH_SCREEN
lijuang99c02d82015-02-13 19:04:34 +08004385#if NO_ALARM_DISPLAY
4386 if (!check_alarm_boot()) {
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08004387#endif
lijuang99c02d82015-02-13 19:04:34 +08004388 dprintf(SPEW, "Display Init: Start\n");
Ajay Singh Parmara7865a82015-04-16 21:27:52 -07004389#if DISPLAY_HDMI_PRIMARY
4390 if (!strlen(device.display_panel))
4391 strlcpy(device.display_panel, DISPLAY_PANEL_HDMI,
4392 sizeof(device.display_panel));
4393#endif
Channagoud Kadabi90869ce2015-04-27 11:15:14 -07004394#if ENABLE_WBC
Channagoud Kadabid801ac72015-08-26 11:21:51 -07004395 /* Wait if the display shutdown is in progress */
4396 while(pm_app_display_shutdown_in_prgs());
Channagoud Kadabi90869ce2015-04-27 11:15:14 -07004397 if (!pm_appsbl_display_init_done())
4398 target_display_init(device.display_panel);
4399 else
4400 display_image_on_screen();
4401#else
lijuang99c02d82015-02-13 19:04:34 +08004402 target_display_init(device.display_panel);
Channagoud Kadabi90869ce2015-04-27 11:15:14 -07004403#endif
lijuang99c02d82015-02-13 19:04:34 +08004404 dprintf(SPEW, "Display Init: Done\n");
4405#if NO_ALARM_DISPLAY
4406 }
4407#endif
4408#endif
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08004409
Greg Griscod6250552011-06-29 14:40:23 -07004410 target_serialno((unsigned char *) sn_buf);
Ajay Dudanib06c05f2011-05-12 14:46:10 -07004411 dprintf(SPEW,"serial number: %s\n",sn_buf);
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -08004412
Dhaval Patel223ec952013-07-18 14:49:44 -07004413 memset(display_panel_buf, '\0', MAX_PANEL_BUF_SIZE);
4414
Matthew Qindefd5562014-07-11 18:02:40 +08004415 /*
4416 * Check power off reason if user force reset,
4417 * if yes phone will do normal boot.
4418 */
4419 if (is_user_force_reset())
4420 goto normal_boot;
4421
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07004422 /* Check if we should do something other than booting up */
Ameya Thakur0b9c2442013-05-10 13:22:56 -07004423 if (keys_get_state(KEY_VOLUMEUP) && keys_get_state(KEY_VOLUMEDOWN))
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07004424 {
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03004425 dprintf(ALWAYS,"dload mode key sequence detected\n");
lijuang395b5e62015-11-19 17:39:44 +08004426 reboot_device(EMERGENCY_DLOAD);
4427 dprintf(CRITICAL,"Failed to reboot into dload mode\n");
4428
Ameya Thakur0b9c2442013-05-10 13:22:56 -07004429 boot_into_fastboot = true;
4430 }
4431 if (!boot_into_fastboot)
4432 {
4433 if (keys_get_state(KEY_HOME) || keys_get_state(KEY_VOLUMEUP))
4434 boot_into_recovery = 1;
4435 if (!boot_into_recovery &&
4436 (keys_get_state(KEY_BACK) || keys_get_state(KEY_VOLUMEDOWN)))
Pavel Nedev5d91d412013-04-29 11:34:24 +03004437 boot_into_fastboot = true;
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07004438 }
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004439 #if NO_KEYPAD_DRIVER
Kinson Chik0b1c8162011-08-31 16:31:57 -07004440 if (fastboot_trigger())
Pavel Nedev5d91d412013-04-29 11:34:24 +03004441 boot_into_fastboot = true;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004442 #endif
Chandan Uddarajubedca152010-06-02 23:05:15 -07004443
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07004444#if USE_PON_REBOOT_REG
4445 reboot_mode = check_hard_reboot_mode();
4446#else
Ajay Dudani77421292010-10-27 19:34:06 -07004447 reboot_mode = check_reboot_mode();
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07004448#endif
4449 if (reboot_mode == RECOVERY_MODE)
4450 {
Ajay Dudani77421292010-10-27 19:34:06 -07004451 boot_into_recovery = 1;
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07004452 }
4453 else if(reboot_mode == FASTBOOT_MODE)
4454 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03004455 boot_into_fastboot = true;
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07004456 }
4457 else if(reboot_mode == ALARM_BOOT)
4458 {
Matthew Qind886f3c2014-01-17 16:52:01 +08004459 boot_reason_alarm = true;
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07004460 }
Parth Dixit207573a2015-10-27 17:26:21 +05304461#if VERIFIED_BOOT
Mayank Grover912eaa62017-10-26 12:08:53 +05304462 else if (VB_M <= target_get_vb_version())
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07004463 {
Mayank Grover889be1b2017-09-12 20:12:23 +05304464 if (reboot_mode == DM_VERITY_ENFORCING)
4465 {
4466 device.verity_mode = 1;
4467 write_device_info(&device);
4468 }
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07004469#if ENABLE_VB_ATTEST
Mayank Grover889be1b2017-09-12 20:12:23 +05304470 else if (reboot_mode == DM_VERITY_EIO)
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07004471#else
Mayank Grover889be1b2017-09-12 20:12:23 +05304472 else if (reboot_mode == DM_VERITY_LOGGING)
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07004473#endif
Mayank Grover889be1b2017-09-12 20:12:23 +05304474 {
4475 device.verity_mode = 0;
4476 write_device_info(&device);
4477 }
4478 else if (reboot_mode == DM_VERITY_KEYSCLEAR)
4479 {
4480 if(send_delete_keys_to_tz())
4481 ASSERT(0);
4482 }
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07004483 }
Parth Dixitddbc7352015-10-18 03:13:31 +05304484#endif
Ajay Dudani77421292010-10-27 19:34:06 -07004485
Matthew Qindefd5562014-07-11 18:02:40 +08004486normal_boot:
Pavel Nedev5d91d412013-04-29 11:34:24 +03004487 if (!boot_into_fastboot)
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004488 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03004489 if (target_is_emmc_boot())
Shashank Mittala0032282011-08-26 14:50:11 -07004490 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03004491 if(emmc_recovery_init())
4492 dprintf(ALWAYS,"error in emmc_recovery_init\n");
4493 if(target_use_signed_kernel())
Shashank Mittala0032282011-08-26 14:50:11 -07004494 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03004495 if((device.is_unlocked) || (device.is_tampered))
4496 {
4497 #ifdef TZ_TAMPER_FUSE
4498 set_tamper_fuse_cmd();
4499 #endif
4500 #if USE_PCOM_SECBOOT
4501 set_tamper_flag(device.is_tampered);
4502 #endif
4503 }
Shashank Mittala0032282011-08-26 14:50:11 -07004504 }
Amit Blay6281ebc2015-01-11 14:44:08 +02004505
Mayank Grover351a75e2017-05-30 20:06:08 +05304506retry_boot:
4507 /* Trying to boot active partition */
4508 if (partition_multislot_is_supported())
4509 {
4510 boot_slot = partition_find_boot_slot();
4511 partition_mark_active_slot(boot_slot);
4512 if (boot_slot == INVALID)
4513 goto fastboot;
4514 }
4515
4516 boot_err_type = boot_linux_from_mmc();
4517 switch (boot_err_type)
4518 {
4519 case ERR_INVALID_PAGE_SIZE:
4520 case ERR_DT_PARSE:
4521 case ERR_ABOOT_ADDR_OVERLAP:
Mayank Grover10cabbe2017-10-30 19:11:05 +05304522 case ERR_INVALID_BOOT_MAGIC:
Mayank Grover351a75e2017-05-30 20:06:08 +05304523 if(partition_multislot_is_supported())
Mayank Grover10cabbe2017-10-30 19:11:05 +05304524 {
4525 /*
4526 * Deactivate current slot, as it failed to
4527 * boot, and retry next slot.
4528 */
4529 partition_deactivate_slot(boot_slot);
Mayank Grover351a75e2017-05-30 20:06:08 +05304530 goto retry_boot;
Mayank Grover10cabbe2017-10-30 19:11:05 +05304531 }
Mayank Grover351a75e2017-05-30 20:06:08 +05304532 else
4533 break;
Mayank Grover351a75e2017-05-30 20:06:08 +05304534 default:
4535 break;
4536 /* going to fastboot menu */
4537 }
Shashank Mittala0032282011-08-26 14:50:11 -07004538 }
Pavel Nedev5d91d412013-04-29 11:34:24 +03004539 else
4540 {
4541 recovery_init();
4542 #if USE_PCOM_SECBOOT
4543 if((device.is_unlocked) || (device.is_tampered))
4544 set_tamper_flag(device.is_tampered);
4545 #endif
4546 boot_linux_from_flash();
4547 }
4548 dprintf(CRITICAL, "ERROR: Could not do normal boot. Reverting "
4549 "to fastboot mode.\n");
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004550 }
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07004551
Mayank Grover351a75e2017-05-30 20:06:08 +05304552fastboot:
Amol Jadi5edf3552013-07-23 14:15:34 -07004553 /* We are here means regular boot did not happen. Start fastboot. */
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07004554
Amol Jadi5edf3552013-07-23 14:15:34 -07004555 /* register aboot specific fastboot commands */
4556 aboot_fastboot_register_commands();
Amol Jadi57abe4c2011-05-24 15:47:27 -07004557
Amol Jadi5edf3552013-07-23 14:15:34 -07004558 /* dump partition table for debug info */
Kinson Chikf1a43512011-07-14 11:28:39 -07004559 partition_dump();
Amol Jadi5edf3552013-07-23 14:15:34 -07004560
4561 /* initialize and start fastboot */
4562 fastboot_init(target_get_scratch_address(), target_get_max_flash_size());
lijuang4ece1e72015-08-14 21:02:36 +08004563#if FBCON_DISPLAY_MSG
lijuang9a7d3b92015-11-30 14:41:24 +08004564 display_fastboot_menu();
lijuang4ece1e72015-08-14 21:02:36 +08004565#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08004566}
4567
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07004568uint32_t get_page_size()
4569{
4570 return page_size;
4571}
4572
Amir Samuelov57a6fa22013-06-05 16:36:43 +03004573/*
4574 * Calculated and save hash (SHA256) for non-signed boot image.
4575 *
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07004576 * @param image_addr - Boot image address
4577 * @param image_size - Size of the boot image
Amir Samuelov57a6fa22013-06-05 16:36:43 +03004578 *
4579 * @return int - 0 on success, negative value on failure.
4580 */
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07004581static int aboot_save_boot_hash_mmc(uint32_t image_addr, uint32_t image_size)
Amir Samuelov57a6fa22013-06-05 16:36:43 +03004582{
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07004583 unsigned int digest[8];
4584#if IMAGE_VERIF_ALGO_SHA1
4585 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA1;
4586#else
4587 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA256;
4588#endif
Amir Samuelov57a6fa22013-06-05 16:36:43 +03004589
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07004590 target_crypto_init_params();
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -08004591 hash_find((unsigned char *) image_addr, image_size, (unsigned char *)&digest, auth_algo);
Amir Samuelov57a6fa22013-06-05 16:36:43 +03004592
4593 save_kernel_hash_cmd(digest);
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07004594 dprintf(INFO, "aboot_save_boot_hash_mmc: imagesize_actual size %d bytes.\n", (int) image_size);
Amir Samuelov57a6fa22013-06-05 16:36:43 +03004595
4596 return 0;
4597}
4598
Mayank Grover351a75e2017-05-30 20:06:08 +05304599
Brian Swetland9c4c0752009-01-25 16:23:50 -08004600APP_START(aboot)
4601 .init = aboot_init,
4602APP_END