blob: 02644c2dfb9fdb141a40298b35ddbb527d857686 [file] [log] [blame]
Brian Swetland9c4c0752009-01-25 16:23:50 -08001/*
2 * Copyright (c) 2009, Google Inc.
3 * All rights reserved.
4 *
Mayank Grover11ff9692018-01-11 11:54:49 +05305 * Copyright (c) 2009-2018, The Linux Foundation. All rights reserved.
Brian Swetland9c4c0752009-01-25 16:23:50 -08006 *
Chandan Uddaraju5fa471a2009-12-02 17:31:34 -08007 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070014 * * Neither the name of The Linux Foundation nor
Chandan Uddaraju5fa471a2009-12-02 17:31:34 -080015 * the names of its contributors may be used to endorse or promote
16 * products derived from this software without specific prior written
17 * permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22 * NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 *
Brian Swetland9c4c0752009-01-25 16:23:50 -080031 */
32
33#include <app.h>
34#include <debug.h>
35#include <arch/arm.h>
Brian Swetland9c4c0752009-01-25 16:23:50 -080036#include <string.h>
Channagoud Kadabi132ff552013-04-19 14:34:44 -070037#include <stdlib.h>
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -070038#include <limits.h>
Brian Swetland9c4c0752009-01-25 16:23:50 -080039#include <kernel/thread.h>
40#include <arch/ops.h>
41
Dima Zavin214cc642009-01-26 11:16:21 -080042#include <dev/flash.h>
Tanya Brokhman1c94f1a2015-02-15 09:05:03 +020043#include <dev/flash-ubi.h>
Dima Zavin214cc642009-01-26 11:16:21 -080044#include <lib/ptable.h>
Dima Zavinb4283602009-01-26 16:36:57 -080045#include <dev/keys.h>
Shashank Mittal4f99a882010-02-01 13:58:50 -080046#include <dev/fbcon.h>
Ajay Dudanid04110c2011-01-17 23:55:07 -080047#include <baseband.h>
Greg Griscod6250552011-06-29 14:40:23 -070048#include <target.h>
49#include <mmc.h>
Kinson Chikf1a43512011-07-14 11:28:39 -070050#include <partition_parser.h>
Mayank Grover351a75e2017-05-30 20:06:08 +053051#include <ab_partition_parser.h>
Monika Singh292b3e92018-03-17 22:40:23 +053052#include <verifiedboot.h>
Greg Griscod6250552011-06-29 14:40:23 -070053#include <platform.h>
Shashank Mittalcd98d472011-08-02 14:29:24 -070054#include <crypto_hash.h>
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070055#include <malloc.h>
Amol Jadi492d5a52013-03-15 16:12:34 -070056#include <boot_stats.h>
Amir Samuelov57a6fa22013-06-05 16:36:43 +030057#include <sha.h>
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -070058#include <platform/iomap.h>
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -070059#include <boot_device.h>
Shashank Mittald3e54dd2014-08-28 15:24:02 -070060#include <boot_verifier.h>
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +053061#include <image_verify.h>
Matthew Qinbb7923d2015-02-09 10:56:09 +080062#include <decompress.h>
Unnati Gandhi17b3bfc2015-05-11 12:58:16 +053063#include <platform/timer.h>
lijuang511a2b52015-08-14 20:50:51 +080064#include <sys/types.h>
Channagoud Kadabi036c6052015-02-09 15:19:59 -080065#if USE_RPMB_FOR_DEVINFO
66#include <rpmb.h>
67#endif
Dima Zavin214cc642009-01-26 11:16:21 -080068
Channagoud Kadabi90869ce2015-04-27 11:15:14 -070069#if ENABLE_WBC
70#include <pm_app_smbchg.h>
71#endif
72
Neeti Desai17379b82012-06-04 18:42:53 -070073#if DEVICE_TREE
74#include <libfdt.h>
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070075#include <dev_tree.h>
Neeti Desai17379b82012-06-04 18:42:53 -070076#endif
77
Aparna Mallavarapu118ccae2015-06-03 13:47:11 +053078#if WDOG_SUPPORT
79#include <wdog.h>
80#endif
81
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -070082#include <reboot.h>
Shashank Mittalcd98d472011-08-02 14:29:24 -070083#include "image_verify.h"
Shashank Mittal024c0332010-02-03 11:44:00 -080084#include "recovery.h"
Brian Swetland9c4c0752009-01-25 16:23:50 -080085#include "bootimg.h"
86#include "fastboot.h"
Ajay Dudani5c761132011-04-07 20:19:04 -070087#include "sparse_format.h"
Ajay Dudanide984792015-03-02 09:57:41 -080088#include "meta_format.h"
Greg Grisco6e754772011-06-23 12:19:39 -070089#include "mmc.h"
Shashank Mittal162244e2011-08-08 19:01:25 -070090#include "devinfo.h"
Neeti Desai465491e2012-07-31 12:53:35 -070091#include "board.h"
Shashank Mittal162244e2011-08-08 19:01:25 -070092#include "scm.h"
Amit Blay6281ebc2015-01-11 14:44:08 +020093#include "mdtp.h"
Sridhar Parasuram32b30662015-07-10 13:33:22 -070094#include "secapp_loader.h"
lijuanga40d6302015-07-20 20:10:13 +080095#include <menu_keys_detect.h>
96#include <display_menu.h>
Channagoud Kadabi736c4962015-08-21 11:56:52 -070097#include "fastboot_test.h"
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -070098
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070099extern bool target_use_signed_kernel(void);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700100extern void platform_uninit(void);
Channagoud Kadabi33defe22013-06-18 18:35:40 -0700101extern void target_uninit(void);
Joonwoo Park61112782013-10-02 19:50:39 -0700102extern int get_target_boot_params(const char *cmdline, const char *part,
vijay kumar870515d2015-08-31 16:37:24 +0530103 char **buf);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700104
Sridhar Parasuram7e16d172015-07-05 11:35:23 -0700105void *info_buf;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700106void write_device_info_mmc(device_info *dev);
107void write_device_info_flash(device_info *dev);
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -0700108static int aboot_save_boot_hash_mmc(uint32_t image_addr, uint32_t image_size);
Channagoud Kadabiad259832015-05-29 11:14:17 -0700109static int aboot_frp_unlock(char *pname, void *data, unsigned sz);
Kishor PK38ed93d2017-04-25 14:19:26 +0530110static inline uint64_t validate_partition_size();
Parth Dixit54ac3bb2017-03-07 15:52:48 +0530111bool pwr_key_is_pressed = false;
Hareesh Gauthamabf1e1e2017-04-12 18:09:24 +0530112static bool is_systemd_present=false;
Mayank Grover351a75e2017-05-30 20:06:08 +0530113static void publish_getvar_multislot_vars();
Sridhar Parasurame94e8152014-10-24 14:06:03 -0700114/* fastboot command function pointer */
115typedef void (*fastboot_cmd_fn) (const char *, void *, unsigned);
Monika Singh292b3e92018-03-17 22:40:23 +0530116bool get_perm_attr_status();
Sridhar Parasurame94e8152014-10-24 14:06:03 -0700117
118struct fastboot_cmd_desc {
119 char * name;
120 fastboot_cmd_fn cb;
121};
122
Subbaraman Narayanamurthyeb92bcc2010-07-20 14:32:46 -0700123#define EXPAND(NAME) #NAME
124#define TARGET(NAME) EXPAND(NAME)
Brian Swetland2defe162009-08-18 14:35:59 -0700125
Ajay Singh Parmara7865a82015-04-16 21:27:52 -0700126#define DISPLAY_PANEL_HDMI "hdmi"
127
Ajay Dudanicd01f9b2010-02-23 21:13:04 -0800128#ifdef MEMBASE
129#define EMMC_BOOT_IMG_HEADER_ADDR (0xFF000+(MEMBASE))
130#else
David Ng183a7422009-12-07 14:55:21 -0800131#define EMMC_BOOT_IMG_HEADER_ADDR 0xFF000
Ajay Dudanicd01f9b2010-02-23 21:13:04 -0800132#endif
133
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700134#ifndef MEMSIZE
135#define MEMSIZE 1024*1024
136#endif
137
138#define MAX_TAGS_SIZE 1024
Kishor PKee5c0a32018-03-06 16:49:46 +0530139#define PLL_CODES_OFFSET 4096
Kun Liang2f1601a2013-08-12 16:29:54 +0800140/* make 4096 as default size to ensure EFS,EXT4's erasing */
141#define DEFAULT_ERASE_SIZE 4096
Ujwal Patelc0b0a252015-08-16 14:05:35 -0700142#define MAX_PANEL_BUF_SIZE 196
Vijay Kumar Pendoti0b21f462016-05-02 17:09:18 +0530143#define FOOTER_SIZE 16384
Kun Liang2f1601a2013-08-12 16:29:54 +0800144
Dhaval Patelf83d73b2014-06-23 16:24:37 -0700145#define DISPLAY_DEFAULT_PREFIX "mdss_mdp"
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700146#define BOOT_DEV_MAX_LEN 64
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -0800147
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800148#define IS_ARM64(ptr) (ptr->magic_64 == KERNEL64_HDR_MAGIC) ? true : false
149
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -0700150#define ADD_OF(a, b) (UINT_MAX - b > a) ? (a + b) : UINT_MAX
151
Ameya Thakur10a33452016-06-13 14:24:26 -0700152//Size of the header that is used in case the boot image has
153//a uncompressed kernel + appended dtb
154#define PATCHED_KERNEL_HEADER_SIZE 20
155
156//String used to determine if the boot image has
157//a uncompressed kernel + appended dtb
158#define PATCHED_KERNEL_MAGIC "UNCOMPRESSED_IMG"
159
Sridhar Parasuram7bd4aaf2015-02-12 11:14:38 -0800160#if USE_BOOTDEV_CMDLINE
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700161static const char *emmc_cmdline = " androidboot.bootdevice=";
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700162#else
David Ng183a7422009-12-07 14:55:21 -0800163static const char *emmc_cmdline = " androidboot.emmc=true";
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700164#endif
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800165static const char *usb_sn_cmdline = " androidboot.serialno=";
Pavel Nedev328ac822013-04-05 15:25:11 +0300166static const char *androidboot_mode = " androidboot.mode=";
Hareesh Gauthamabf1e1e2017-04-12 18:09:24 +0530167
168static const char *systemd_ffbm_mode = " systemd.unit=ffbm.target";
Matthew Qind886f3c2014-01-17 16:52:01 +0800169static const char *alarmboot_cmdline = " androidboot.alarmboot=true";
Pavel Nedev898298c2013-02-27 12:36:09 -0800170static const char *loglevel = " quiet";
Ajay Dudanica3a33c2011-11-18 08:31:40 -0800171static const char *battchg_pause = " androidboot.mode=charger";
Shashank Mittalcd98d472011-08-02 14:29:24 -0700172static const char *auth_kernel = " androidboot.authorized_kernel=true";
Pavel Nedev5614d222013-06-17 18:01:02 +0300173static const char *secondary_gpt_enable = " gpt";
Monika Singh292b3e92018-03-17 22:40:23 +0530174#ifdef MDTP_SUPPORT
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200175static const char *mdtp_activated_flag = " mdtp";
Monika Singh292b3e92018-03-17 22:40:23 +0530176#endif
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800177static const char *baseband_apq = " androidboot.baseband=apq";
178static const char *baseband_msm = " androidboot.baseband=msm";
179static const char *baseband_csfb = " androidboot.baseband=csfb";
180static const char *baseband_svlte2a = " androidboot.baseband=svlte2a";
Ajay Dudani403bc492011-09-30 16:17:21 -0700181static const char *baseband_mdm = " androidboot.baseband=mdm";
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800182static const char *baseband_mdm2 = " androidboot.baseband=mdm2";
Amol Jadi5c61a952012-05-04 17:05:35 -0700183static const char *baseband_sglte = " androidboot.baseband=sglte";
Amol Jadi2a15a272013-01-22 12:03:36 -0800184static const char *baseband_dsda = " androidboot.baseband=dsda";
185static const char *baseband_dsda2 = " androidboot.baseband=dsda2";
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800186static const char *baseband_sglte2 = " androidboot.baseband=sglte2";
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800187static const char *warmboot_cmdline = " qpnp-power-on.warm_boot=1";
Vijay Kumar Pendotib228cfc2016-06-13 20:15:23 +0530188static const char *baseband_apq_nowgr = " androidboot.baseband=baseband_apq_nowgr";
Mayank Grover351a75e2017-05-30 20:06:08 +0530189static const char *androidboot_slot_suffix = " androidboot.slot_suffix=";
190static const char *skip_ramfs = " skip_initramfs";
Mayank Grover466d6562018-05-10 14:52:20 +0530191
192#if HIBERNATION_SUPPORT
193static const char *resume = " resume=/dev/mmcblk0p";
194#endif
195
Sourabh Banerjee6c7153c2018-03-20 01:21:57 +0530196#ifdef INIT_BIN_LE
197static const char *sys_path_cmdline = " rootwait ro init="INIT_BIN_LE;
198#else
Mayank Grover3804be72017-06-22 11:59:23 +0530199static const char *sys_path_cmdline = " rootwait ro init=/init";
Sourabh Banerjee6c7153c2018-03-20 01:21:57 +0530200#endif
Sourabh Banerjee386b1322018-02-27 09:37:28 +0530201
202#if VERITY_LE
203static const char *verity_dev = " root=/dev/dm-0";
204static const char *verity_system_part = " dm=\"system";
205static const char *verity_params = " none ro,0 1 android-verity /dev/mmcblk0p";
206#else
Mayank Grover5384ed82018-05-09 12:09:24 +0530207static const char *sys_path = " root=/dev/mmcblk0p";
lijuang83ef4b22018-08-23 11:01:55 +0800208
209#define MAX_DTBO_IDX_STR 64
210static const char *android_boot_dtbo_idx = " androidboot.dtbo_idx=";
Sourabh Banerjee386b1322018-02-27 09:37:28 +0530211#endif
Ajay Dudanid04110c2011-01-17 23:55:07 -0800212
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700213#if VERIFIED_BOOT
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700214static const char *verity_mode = " androidboot.veritymode=";
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700215static const char *verified_state= " androidboot.verifiedbootstate=";
Parth Dixita5715a02015-10-29 12:25:10 +0530216static const char *keymaster_v1= " androidboot.keymaster=1";
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700217//indexed based on enum values, green is 0 by default
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700218
219struct verified_boot_verity_mode vbvm[] =
220{
Channagoud Kadabi86b0c112016-03-16 19:23:16 -0700221#if ENABLE_VB_ATTEST
222 {false, "eio"},
223#else
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700224 {false, "logging"},
Channagoud Kadabi86b0c112016-03-16 19:23:16 -0700225#endif
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700226 {true, "enforcing"},
227};
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700228struct verified_boot_state_name vbsn[] =
229{
230 {GREEN, "green"},
231 {ORANGE, "orange"},
232 {YELLOW,"yellow"},
233 {RED,"red" },
234};
235#endif
Channagoud Kadabi86b0c112016-03-16 19:23:16 -0700236/*As per spec delay wait time before shutdown in Red state*/
237#define DELAY_WAIT 30000
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700238static unsigned page_size = 0;
239static unsigned page_mask = 0;
Gaurav Nebhwani4d2389c2016-03-17 21:10:05 +0530240static unsigned mmc_blocksize = 0;
241static unsigned mmc_blocksize_mask = 0;
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700242static char ffbm_mode_string[FFBM_MODE_BUF_SIZE];
243static bool boot_into_ffbm;
vijay kumar870515d2015-08-31 16:37:24 +0530244static char *target_boot_params = NULL;
Matthew Qind886f3c2014-01-17 16:52:01 +0800245static bool boot_reason_alarm;
Channagoud Kadabi80a182b2015-03-11 17:04:23 -0700246static bool devinfo_present = true;
Channagoud Kadabi736c4962015-08-21 11:56:52 -0700247bool boot_into_fastboot = false;
Parth Dixit4097b622016-03-15 14:42:27 +0530248static uint32_t dt_size = 0;
Monika Singh292b3e92018-03-17 22:40:23 +0530249static char *vbcmdline;
250static bootinfo info = {0};
Shashank Mittalcd98d472011-08-02 14:29:24 -0700251/* Assuming unauthorized kernel image by default */
252static int auth_kernel_img = 0;
Monika Singh292b3e92018-03-17 22:40:23 +0530253static device_info device = {DEVICE_MAGIC,0,0,0,0,{0},{0},{0},1,{0},0,{0}};
254
vijay kumarc65876c2015-04-24 13:29:16 +0530255static bool is_allow_unlock = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -0700256
vijay kumarca2e6812015-07-08 20:28:25 +0530257static char frp_ptns[2][8] = {"config","frp"};
258
lijuang511a2b52015-08-14 20:50:51 +0800259static const char *critical_flash_allowed_ptn[] = {
260 "aboot",
261 "rpm",
262 "tz",
263 "sbl",
264 "sdi",
265 "sbl1",
266 "xbl",
267 "hyp",
268 "pmic",
269 "bootloader",
270 "devinfo",
271 "partition"};
272
Dima Zavin42168f22009-01-30 11:52:22 -0800273struct atag_ptbl_entry
274{
275 char name[16];
276 unsigned offset;
277 unsigned size;
278 unsigned flags;
279};
280
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700281/*
282 * Partition info, required to be published
283 * for fastboot
284 */
285struct getvar_partition_info {
Mayank Grover7b8a8f82017-10-27 13:07:59 +0530286 char part_name[MAX_GPT_NAME_SIZE]; /* Partition name */
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700287 char getvar_size[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for size */
288 char getvar_type[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for type */
289 char size_response[MAX_RSP_SIZE]; /* fastboot response for size */
290 char type_response[MAX_RSP_SIZE]; /* fastboot response for type */
291};
292
293/*
Mayank Grover7b8a8f82017-10-27 13:07:59 +0530294 * Update the part_type_known for known paritions types.
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700295 */
Mayank Groverd38fe012018-03-13 15:33:16 +0530296#define RAW_STR "raw"
Mayank Grover52cd10a2018-03-15 12:57:54 +0530297#define EXT_STR "ext4"
298#define F2FS_STR "f2fs"
299
300#define FS_SUPERBLOCK_OFFSET 0x400
301#define EXT_MAGIC 0xEF53
302#define EXT_MAGIC_OFFSET_SB 0x38
303#define F2FS_MAGIC 0xF2F52010 // F2FS Magic Number
304#define F2FS_MAGIC_OFFSET_SB 0x0
305
306typedef enum fs_signature_type {
307 EXT_FS_SIGNATURE = 1,
308 EXT_F2FS_SIGNATURE = 2,
309 NO_FS = -1
310} fs_signature_type;
311
Mayank Grover7b8a8f82017-10-27 13:07:59 +0530312struct getvar_partition_info part_info[NUM_PARTITIONS];
313struct getvar_partition_info part_type_known[] =
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700314{
Mayank Grover49920212018-02-09 18:15:55 +0530315 { "system" , "partition-size:", "partition-type:", "", "ext4" },
316 { "userdata" , "partition-size:", "partition-type:", "", "ext4" },
317 { "cache" , "partition-size:", "partition-type:", "", "ext4" },
318 { "recoveryfs" , "partition-size:", "partition-type:", "", "ext4" },
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700319};
320
321char max_download_size[MAX_RSP_SIZE];
Ameya Thakur11cf1a62013-08-05 12:44:48 -0700322char charger_screen_enabled[MAX_RSP_SIZE];
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800323char sn_buf[13];
Dhaval Patel223ec952013-07-18 14:49:44 -0700324char display_panel_buf[MAX_PANEL_BUF_SIZE];
Unnati Gandhi62c8ab82014-01-24 11:01:01 +0530325char panel_display_mode[MAX_RSP_SIZE];
Mayank Grovera64dfbe2018-05-17 14:06:34 +0530326char soc_version_str[MAX_RSP_SIZE];
Mayank Grover7b8a8f82017-10-27 13:07:59 +0530327char block_size_string[MAX_RSP_SIZE];
Mukesh Ojhabb6e7f32018-03-02 15:04:06 +0530328#if PRODUCT_IOT
Mayank Grover7b8a8f82017-10-27 13:07:59 +0530329
330/* For IOT we are using custom version */
331#define PRODUCT_IOT_VERSION "IOT001"
332char bootloader_version_string[MAX_RSP_SIZE];
333#endif
lijuang102dfa92015-10-09 18:31:03 +0800334
335#if CHECK_BAT_VOLTAGE
lijuang65c5a822015-08-29 16:35:36 +0800336char battery_voltage[MAX_RSP_SIZE];
lijuang102dfa92015-10-09 18:31:03 +0800337char battery_soc_ok [MAX_RSP_SIZE];
338#endif
339
lijuangf16461c2015-08-03 17:09:34 +0800340char get_variant[MAX_RSP_SIZE];
Greg Griscod6250552011-06-29 14:40:23 -0700341
Greg Griscod2471ef2011-07-14 13:00:42 -0700342extern int emmc_recovery_init(void);
343
Kinson Chik0b1c8162011-08-31 16:31:57 -0700344#if NO_KEYPAD_DRIVER
345extern int fastboot_trigger(void);
346#endif
Greg Griscod2471ef2011-07-14 13:00:42 -0700347
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800348static void update_ker_tags_rdisk_addr(struct boot_img_hdr *hdr, bool is_arm64)
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700349{
350 /* overwrite the destination of specified for the project */
Channagoud Kadabi7042fa32013-04-26 16:44:14 -0700351#ifdef ABOOT_IGNORE_BOOT_HEADER_ADDRS
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800352 if (is_arm64)
353 hdr->kernel_addr = ABOOT_FORCE_KERNEL64_ADDR;
354 else
355 hdr->kernel_addr = ABOOT_FORCE_KERNEL_ADDR;
Channagoud Kadabi7042fa32013-04-26 16:44:14 -0700356 hdr->ramdisk_addr = ABOOT_FORCE_RAMDISK_ADDR;
357 hdr->tags_addr = ABOOT_FORCE_TAGS_ADDR;
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700358#endif
359}
360
Dima Zavin42168f22009-01-30 11:52:22 -0800361static void ptentry_to_tag(unsigned **ptr, struct ptentry *ptn)
362{
363 struct atag_ptbl_entry atag_ptn;
364
365 memcpy(atag_ptn.name, ptn->name, 16);
366 atag_ptn.name[15] = '\0';
367 atag_ptn.offset = ptn->start;
368 atag_ptn.size = ptn->length;
369 atag_ptn.flags = ptn->flags;
370 memcpy(*ptr, &atag_ptn, sizeof(struct atag_ptbl_entry));
371 *ptr += sizeof(struct atag_ptbl_entry) / sizeof(unsigned);
372}
Mayank Grover9df84c02018-08-30 15:46:35 +0530373#ifdef VERIFIED_BOOT_2
374void load_vbmeta_image(void **vbmeta_image_buf, uint32_t *vbmeta_image_sz)
375{
376 int index = 0;
377 char *vbm_img_buf = NULL;
378 unsigned long long ptn = 0;
379 unsigned long long ptn_size = 0;
380
381 /* Immediately return if dtbo is not supported */
382 index = partition_get_index("vbmeta");
383 ptn = partition_get_offset(index);
384 if(!ptn)
385 {
386 dprintf(CRITICAL, "ERROR: vbmeta partition not found.\n");
387 return;
388 }
389
390 ptn_size = partition_get_size(index);
391 if (ptn_size > MAX_SUPPORTED_VBMETA_IMG_BUF)
392 {
393 dprintf(CRITICAL, "ERROR: vbmeta parition size is greater than supported.\n");
394 return;
395 }
396
397 vbm_img_buf = (char *)memalign(CACHE_LINE, ROUNDUP((uint32_t)ptn_size, CACHE_LINE));
398 if (!vbm_img_buf)
399 {
400 dprintf(CRITICAL, "ERROR: vbmeta unable to locate buffer\n");
401 return;
402 }
403
404 mmc_set_lun(partition_get_lun(index));
405 if (mmc_read(ptn, (uint32_t *)vbm_img_buf, (uint32_t)ptn_size))
406 {
407 dprintf(CRITICAL, "ERROR: vbmeta read failure\n");
408 free(vbm_img_buf);
409 return;
410 }
411
412 *vbmeta_image_buf = vbm_img_buf;
413 *vbmeta_image_sz = (uint32_t)ptn_size;
414 return;
415}
416#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -0800417
lijuang102dfa92015-10-09 18:31:03 +0800418#if CHECK_BAT_VOLTAGE
419void update_battery_status(void)
420{
421 snprintf(battery_voltage,MAX_RSP_SIZE, "%d",target_get_battery_voltage());
422 snprintf(battery_soc_ok ,MAX_RSP_SIZE, "%s",target_battery_soc_ok()? "yes":"no");
423}
424#endif
425
Neeti Desaie245d492012-06-01 12:52:13 -0700426unsigned char *update_cmdline(const char * cmdline)
Brian Swetland9c4c0752009-01-25 16:23:50 -0800427{
David Ng183a7422009-12-07 14:55:21 -0800428 int cmdline_len = 0;
429 int have_cmdline = 0;
Amol Jadi168b7712012-03-06 16:15:00 -0800430 unsigned char *cmdline_final = NULL;
Neeti Desaie245d492012-06-01 12:52:13 -0700431 int pause_at_bootup = 0;
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800432 bool warm_boot = false;
Pavel Nedev5614d222013-06-17 18:01:02 +0300433 bool gpt_exists = partition_gpt_exists();
Joonwoo Park61112782013-10-02 19:50:39 -0700434 int have_target_boot_params = 0;
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700435 char *boot_dev_buf = NULL;
Monika Singh292b3e92018-03-17 22:40:23 +0530436#ifdef MDTP_SUPPORT
Mayank Grover351a75e2017-05-30 20:06:08 +0530437 bool is_mdtp_activated = 0;
Monika Singh292b3e92018-03-17 22:40:23 +0530438#endif
Mayank Grover351a75e2017-05-30 20:06:08 +0530439 int current_active_slot = INVALID;
Mayank Grover3804be72017-06-22 11:59:23 +0530440 int system_ptn_index = -1;
441 unsigned int lun = 0;
442 char lun_char_base = 'a';
Sourabh Banerjee386b1322018-02-27 09:37:28 +0530443#if VERITY_LE
444 int syspath_buflen = strlen(verity_dev)
445 + strlen(verity_system_part) + (sizeof(char) * 2) + 2
446 + strlen(verity_params) + sizeof(int) + 2;
447#else
lijuang83ef4b22018-08-23 11:01:55 +0800448 int syspath_buflen = strlen(sys_path) + sizeof(int) + 2; /*allocate buflen for largest possible string*/
449 char dtbo_idx_str[MAX_DTBO_IDX_STR] = "\0";
450 int dtbo_idx = INVALID_PTN;
Sourabh Banerjee386b1322018-02-27 09:37:28 +0530451#endif
Mayank Grover3804be72017-06-22 11:59:23 +0530452 char syspath_buf[syspath_buflen];
Mayank Grover466d6562018-05-10 14:52:20 +0530453#if HIBERNATION_SUPPORT
454 int resume_buflen = strlen(resume) + sizeof(int) + 2;
455 char resume_buf[resume_buflen];
456 int swap_ptn_index = INVALID_PTN;
457#endif
458
Mayank Grover889be1b2017-09-12 20:12:23 +0530459#if VERIFIED_BOOT
460 uint32_t boot_state = RED;
461#endif
Hareesh Gauthamabf1e1e2017-04-12 18:09:24 +0530462
463#if USE_LE_SYSTEMD
464 is_systemd_present=true;
465#endif
466
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700467#if VERIFIED_BOOT
Mayank Grover912eaa62017-10-26 12:08:53 +0530468 if (VB_M <= target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +0530469 {
470 boot_state = boot_verify_get_state();
471 }
Parth Dixitddbc7352015-10-18 03:13:31 +0530472#endif
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700473
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200474#ifdef MDTP_SUPPORT
475 mdtp_activated(&is_mdtp_activated);
476#endif /* MDTP_SUPPORT */
Dima Zavin42168f22009-01-30 11:52:22 -0800477
Brian Swetland9c4c0752009-01-25 16:23:50 -0800478 if (cmdline && cmdline[0]) {
David Ng183a7422009-12-07 14:55:21 -0800479 cmdline_len = strlen(cmdline);
480 have_cmdline = 1;
481 }
482 if (target_is_emmc_boot()) {
483 cmdline_len += strlen(emmc_cmdline);
Sridhar Parasuram7bd4aaf2015-02-12 11:14:38 -0800484#if USE_BOOTDEV_CMDLINE
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700485 boot_dev_buf = (char *) malloc(sizeof(char) * BOOT_DEV_MAX_LEN);
486 ASSERT(boot_dev_buf);
487 platform_boot_dev_cmdline(boot_dev_buf);
488 cmdline_len += strlen(boot_dev_buf);
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700489#endif
David Ng183a7422009-12-07 14:55:21 -0800490 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800491
492 cmdline_len += strlen(usb_sn_cmdline);
493 cmdline_len += strlen(sn_buf);
494
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700495#if VERIFIED_BOOT
Mayank Grover912eaa62017-10-26 12:08:53 +0530496 if (VB_M <= target_get_vb_version())
Sridhar Parasuram7d8bb9a2015-09-23 18:21:19 -0700497 {
Mayank Grover889be1b2017-09-12 20:12:23 +0530498 cmdline_len += strlen(verified_state) + strlen(vbsn[boot_state].name);
499 if ((device.verity_mode != 0 ) && (device.verity_mode != 1))
500 {
501 dprintf(CRITICAL, "Devinfo paritition possibly corrupted!!!. Please erase devinfo partition to continue booting\n");
502 ASSERT(0);
503 }
504 cmdline_len += strlen(verity_mode) + strlen(vbvm[device.verity_mode].name);
505 cmdline_len += strlen(keymaster_v1);
Sridhar Parasuram7d8bb9a2015-09-23 18:21:19 -0700506 }
Parth Dixitddbc7352015-10-18 03:13:31 +0530507#endif
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700508
Monika Singh292b3e92018-03-17 22:40:23 +0530509
510 if (vbcmdline != NULL) {
511 dprintf(DEBUG, "UpdateCmdLine vbcmdline present len %d\n",
512 strlen(vbcmdline));
513 cmdline_len += strlen(vbcmdline);
514 }
515
Pavel Nedev5614d222013-06-17 18:01:02 +0300516 if (boot_into_recovery && gpt_exists)
517 cmdline_len += strlen(secondary_gpt_enable);
518
Monika Singh292b3e92018-03-17 22:40:23 +0530519#ifdef MDTP_SUPPORT
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200520 if(is_mdtp_activated)
521 cmdline_len += strlen(mdtp_activated_flag);
Monika Singh292b3e92018-03-17 22:40:23 +0530522#endif
Pavel Nedev328ac822013-04-05 15:25:11 +0300523 if (boot_into_ffbm) {
524 cmdline_len += strlen(androidboot_mode);
Hareesh Gauthamabf1e1e2017-04-12 18:09:24 +0530525
526 if(is_systemd_present)
527 cmdline_len += strlen(systemd_ffbm_mode);
528
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700529 cmdline_len += strlen(ffbm_mode_string);
Pavel Nedev898298c2013-02-27 12:36:09 -0800530 /* reduce kernel console messages to speed-up boot */
531 cmdline_len += strlen(loglevel);
Matthew Qind886f3c2014-01-17 16:52:01 +0800532 } else if (boot_reason_alarm) {
533 cmdline_len += strlen(alarmboot_cmdline);
Zhenhua Huang431dafa2015-06-30 16:13:37 +0800534 } else if ((target_build_variant_user() || device.charger_screen_enabled)
lijuang266b17d2018-08-17 18:53:42 +0800535 && target_pause_for_battery_charge() && !boot_into_recovery) {
David Ngf773dde2010-07-26 19:55:08 -0700536 pause_at_bootup = 1;
537 cmdline_len += strlen(battchg_pause);
538 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800539
Shashank Mittalcd98d472011-08-02 14:29:24 -0700540 if(target_use_signed_kernel() && auth_kernel_img) {
541 cmdline_len += strlen(auth_kernel);
542 }
543
Joonwoo Park61112782013-10-02 19:50:39 -0700544 if (get_target_boot_params(cmdline, boot_into_recovery ? "recoveryfs" :
545 "system",
vijay kumar870515d2015-08-31 16:37:24 +0530546 &target_boot_params) == 0) {
Joonwoo Park61112782013-10-02 19:50:39 -0700547 have_target_boot_params = 1;
548 cmdline_len += strlen(target_boot_params);
549 }
550
Ajay Dudanid04110c2011-01-17 23:55:07 -0800551 /* Determine correct androidboot.baseband to use */
552 switch(target_baseband())
553 {
554 case BASEBAND_APQ:
555 cmdline_len += strlen(baseband_apq);
556 break;
557
558 case BASEBAND_MSM:
559 cmdline_len += strlen(baseband_msm);
560 break;
561
562 case BASEBAND_CSFB:
563 cmdline_len += strlen(baseband_csfb);
564 break;
565
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800566 case BASEBAND_SVLTE2A:
567 cmdline_len += strlen(baseband_svlte2a);
Ajay Dudanid04110c2011-01-17 23:55:07 -0800568 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700569
570 case BASEBAND_MDM:
571 cmdline_len += strlen(baseband_mdm);
572 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700573
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800574 case BASEBAND_MDM2:
575 cmdline_len += strlen(baseband_mdm2);
576 break;
577
Amol Jadi5c61a952012-05-04 17:05:35 -0700578 case BASEBAND_SGLTE:
579 cmdline_len += strlen(baseband_sglte);
580 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530581
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800582 case BASEBAND_SGLTE2:
583 cmdline_len += strlen(baseband_sglte2);
584 break;
585
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530586 case BASEBAND_DSDA:
587 cmdline_len += strlen(baseband_dsda);
588 break;
Amol Jadi2a15a272013-01-22 12:03:36 -0800589
590 case BASEBAND_DSDA2:
591 cmdline_len += strlen(baseband_dsda2);
592 break;
Vijay Kumar Pendotib228cfc2016-06-13 20:15:23 +0530593 case BASEBAND_APQ_NOWGR:
594 cmdline_len += strlen(baseband_apq_nowgr);
595 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800596 }
597
Vladislav Levenetz3cd45242017-04-05 08:58:07 +0300598#if ENABLE_DISPLAY
Lijuan Gao4a5b8322014-07-24 10:38:42 +0800599 if (cmdline) {
600 if ((strstr(cmdline, DISPLAY_DEFAULT_PREFIX) == NULL) &&
Padmanabhan Komandurubccbcdc2015-06-30 16:19:24 +0530601 target_display_panel_node(display_panel_buf,
602 MAX_PANEL_BUF_SIZE) &&
Lijuan Gao4a5b8322014-07-24 10:38:42 +0800603 strlen(display_panel_buf)) {
604 cmdline_len += strlen(display_panel_buf);
605 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700606 }
Vladislav Levenetz3cd45242017-04-05 08:58:07 +0300607#endif
Dhaval Patel223ec952013-07-18 14:49:44 -0700608
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800609 if (target_warm_boot()) {
610 warm_boot = true;
611 cmdline_len += strlen(warmboot_cmdline);
612 }
613
Mayank Grover5384ed82018-05-09 12:09:24 +0530614 if (target_uses_system_as_root() ||
615 partition_multislot_is_supported())
Mayank Grover351a75e2017-05-30 20:06:08 +0530616 {
617 current_active_slot = partition_find_active_slot();
618 cmdline_len += (strlen(androidboot_slot_suffix)+
619 strlen(SUFFIX_SLOT(current_active_slot)));
620
Mayank Grover3804be72017-06-22 11:59:23 +0530621 system_ptn_index = partition_get_index("system");
622 if (platform_boot_dev_isemmc())
623 {
Sourabh Banerjee386b1322018-02-27 09:37:28 +0530624#if VERITY_LE
625 /*
626 Condition 4: Verity and A/B both enabled
627 Eventual command line looks like:
628 ... androidboot.slot_suffix=<slot_suffix> ... rootfstype=ext4 ...
629 ... root=/dev/dm-0 dm="system_<slot_suffix> none ro,0 1 android-verity /dev/mmcblk0p<NN>"
630 */
631 snprintf(syspath_buf, syspath_buflen, " %s %s%s %s%d\"",
632 verity_dev,
633 verity_system_part, suffix_slot[current_active_slot],
634 verity_params, system_ptn_index + 1);
635#else
636 /*
637 Condition 5: A/B enabled, but verity disabled
638 Eventual command line looks like:
639 ... androidboot.slot_suffix=<slot_suffix> ... rootfstype=ext4 ...
640 ... root=/dev/mmcblk0p<NN> ...
641 */
642 snprintf(syspath_buf, syspath_buflen, " %s%d",
643 sys_path, system_ptn_index + 1);
644#endif
Mayank Grover3804be72017-06-22 11:59:23 +0530645 }
646 else
647 {
648 lun = partition_get_lun(system_ptn_index);
649 snprintf(syspath_buf, syspath_buflen, " root=/dev/sd%c%d",
650 lun_char_base + lun,
651 partition_get_index_in_lun("system", lun));
652 }
Mayank Grover351a75e2017-05-30 20:06:08 +0530653
Monika Singh292b3e92018-03-17 22:40:23 +0530654#ifndef VERIFIED_BOOT_2
Mayank Grover3804be72017-06-22 11:59:23 +0530655 cmdline_len += strlen(syspath_buf);
Monika Singh292b3e92018-03-17 22:40:23 +0530656#endif
Mayank Grover5384ed82018-05-09 12:09:24 +0530657 }
658
659 if (target_uses_system_as_root() ||
660 partition_multislot_is_supported())
661 {
662 cmdline_len += strlen(sys_path_cmdline);
Mayank Grover351a75e2017-05-30 20:06:08 +0530663 if (!boot_into_recovery)
664 cmdline_len += strlen(skip_ramfs);
665 }
666
Mayank Grover466d6562018-05-10 14:52:20 +0530667#if HIBERNATION_SUPPORT
668 if (platform_boot_dev_isemmc())
669 {
670 swap_ptn_index = partition_get_index("swap");
671 if (swap_ptn_index != INVALID_PTN)
672 {
673 snprintf(resume_buf, resume_buflen,
674 " %s%d", resume,
675 (swap_ptn_index + 1));
676 cmdline_len += strlen(resume_buf);
677 }
678 else
679 {
680 dprintf(INFO, "WARNING: swap partition not found\n");
681 }
682 }
683#endif
684
Channagoud Kadabi85c7ec32016-01-28 23:09:21 -0800685#if TARGET_CMDLINE_SUPPORT
686 char *target_cmdline_buf = malloc(TARGET_MAX_CMDLNBUF);
687 int target_cmd_line_len;
688 ASSERT(target_cmdline_buf);
689 target_cmd_line_len = target_update_cmdline(target_cmdline_buf);
690 cmdline_len += target_cmd_line_len;
691#endif
692
lijuang83ef4b22018-08-23 11:01:55 +0800693#if !VERITY_LE
694 dtbo_idx = get_dtbo_idx ();
695 if (dtbo_idx != INVALID_PTN) {
696 snprintf(dtbo_idx_str, sizeof(dtbo_idx_str), "%s%d",
697 android_boot_dtbo_idx, dtbo_idx);
698 cmdline_len += strlen (dtbo_idx_str);
699 }
700#endif
701
David Ng183a7422009-12-07 14:55:21 -0800702 if (cmdline_len > 0) {
703 const char *src;
Maria Yu52254c02014-07-04 16:14:54 +0800704 unsigned char *dst;
705
706 cmdline_final = (unsigned char*) malloc((cmdline_len + 4) & (~3));
707 ASSERT(cmdline_final != NULL);
vijay kumar287bb542015-09-29 13:01:52 +0530708 memset((void *)cmdline_final, 0, sizeof(*cmdline_final));
Maria Yu52254c02014-07-04 16:14:54 +0800709 dst = cmdline_final;
Neeti Desaie245d492012-06-01 12:52:13 -0700710
Amol Jadi168b7712012-03-06 16:15:00 -0800711 /* Save start ptr for debug print */
David Ng183a7422009-12-07 14:55:21 -0800712 if (have_cmdline) {
713 src = cmdline;
714 while ((*dst++ = *src++));
715 }
716 if (target_is_emmc_boot()) {
717 src = emmc_cmdline;
718 if (have_cmdline) --dst;
David Ngf773dde2010-07-26 19:55:08 -0700719 have_cmdline = 1;
720 while ((*dst++ = *src++));
Sridhar Parasuram7bd4aaf2015-02-12 11:14:38 -0800721#if USE_BOOTDEV_CMDLINE
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700722 src = boot_dev_buf;
723 if (have_cmdline) --dst;
724 while ((*dst++ = *src++));
725#endif
David Ngf773dde2010-07-26 19:55:08 -0700726 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800727
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700728#if VERIFIED_BOOT
Mayank Grover912eaa62017-10-26 12:08:53 +0530729 if (VB_M <= target_get_vb_version())
Sridhar Parasuram7d8bb9a2015-09-23 18:21:19 -0700730 {
Mayank Grover889be1b2017-09-12 20:12:23 +0530731 src = verified_state;
732 if(have_cmdline) --dst;
733 have_cmdline = 1;
734 while ((*dst++ = *src++));
735 src = vbsn[boot_state].name;
736 if(have_cmdline) --dst;
737 while ((*dst++ = *src++));
738
739 if ((device.verity_mode != 0 ) && (device.verity_mode != 1))
740 {
741 dprintf(CRITICAL, "Devinfo paritition possibly corrupted!!!. Please erase devinfo partition to continue booting\n");
742 ASSERT(0);
743 }
744 src = verity_mode;
745 if(have_cmdline) --dst;
746 while ((*dst++ = *src++));
747 src = vbvm[device.verity_mode].name;
748 if(have_cmdline) -- dst;
749 while ((*dst++ = *src++));
750 src = keymaster_v1;
751 if(have_cmdline) --dst;
752 while ((*dst++ = *src++));
Sridhar Parasuram7d8bb9a2015-09-23 18:21:19 -0700753 }
Parth Dixitddbc7352015-10-18 03:13:31 +0530754#endif
Monika Singh292b3e92018-03-17 22:40:23 +0530755
756 if (vbcmdline != NULL) {
757 src = vbcmdline;
758 if (have_cmdline) --dst;
759 while ((*dst++ = *src++));
760 }
761
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800762 src = usb_sn_cmdline;
763 if (have_cmdline) --dst;
764 have_cmdline = 1;
765 while ((*dst++ = *src++));
766 src = sn_buf;
767 if (have_cmdline) --dst;
768 have_cmdline = 1;
769 while ((*dst++ = *src++));
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800770 if (warm_boot) {
771 if (have_cmdline) --dst;
772 src = warmboot_cmdline;
773 while ((*dst++ = *src++));
774 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800775
Pavel Nedev5614d222013-06-17 18:01:02 +0300776 if (boot_into_recovery && gpt_exists) {
777 src = secondary_gpt_enable;
778 if (have_cmdline) --dst;
779 while ((*dst++ = *src++));
780 }
Monika Singh292b3e92018-03-17 22:40:23 +0530781#ifdef MDTP_SUPPORT
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200782 if (is_mdtp_activated) {
783 src = mdtp_activated_flag;
784 if (have_cmdline) --dst;
785 while ((*dst++ = *src++));
786 }
Monika Singh292b3e92018-03-17 22:40:23 +0530787#endif
Pavel Nedev328ac822013-04-05 15:25:11 +0300788 if (boot_into_ffbm) {
789 src = androidboot_mode;
790 if (have_cmdline) --dst;
791 while ((*dst++ = *src++));
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700792 src = ffbm_mode_string;
Pavel Nedev328ac822013-04-05 15:25:11 +0300793 if (have_cmdline) --dst;
794 while ((*dst++ = *src++));
Hareesh Gauthamabf1e1e2017-04-12 18:09:24 +0530795
796 if(is_systemd_present) {
797 src = systemd_ffbm_mode;
798 if (have_cmdline) --dst;
799 while ((*dst++ = *src++));
800 }
801
Pavel Nedev898298c2013-02-27 12:36:09 -0800802 src = loglevel;
803 if (have_cmdline) --dst;
804 while ((*dst++ = *src++));
Matthew Qind886f3c2014-01-17 16:52:01 +0800805 } else if (boot_reason_alarm) {
806 src = alarmboot_cmdline;
807 if (have_cmdline) --dst;
808 while ((*dst++ = *src++));
Pavel Nedev328ac822013-04-05 15:25:11 +0300809 } else if (pause_at_bootup) {
David Ngf773dde2010-07-26 19:55:08 -0700810 src = battchg_pause;
811 if (have_cmdline) --dst;
David Ng183a7422009-12-07 14:55:21 -0800812 while ((*dst++ = *src++));
813 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800814
Shashank Mittalcd98d472011-08-02 14:29:24 -0700815 if(target_use_signed_kernel() && auth_kernel_img) {
816 src = auth_kernel;
817 if (have_cmdline) --dst;
818 while ((*dst++ = *src++));
819 }
820
Ajay Dudanid04110c2011-01-17 23:55:07 -0800821 switch(target_baseband())
822 {
823 case BASEBAND_APQ:
824 src = baseband_apq;
825 if (have_cmdline) --dst;
826 while ((*dst++ = *src++));
827 break;
828
829 case BASEBAND_MSM:
830 src = baseband_msm;
831 if (have_cmdline) --dst;
832 while ((*dst++ = *src++));
833 break;
834
835 case BASEBAND_CSFB:
836 src = baseband_csfb;
837 if (have_cmdline) --dst;
838 while ((*dst++ = *src++));
839 break;
840
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800841 case BASEBAND_SVLTE2A:
842 src = baseband_svlte2a;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800843 if (have_cmdline) --dst;
844 while ((*dst++ = *src++));
845 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700846
847 case BASEBAND_MDM:
848 src = baseband_mdm;
849 if (have_cmdline) --dst;
850 while ((*dst++ = *src++));
851 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700852
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800853 case BASEBAND_MDM2:
854 src = baseband_mdm2;
855 if (have_cmdline) --dst;
856 while ((*dst++ = *src++));
857 break;
858
Amol Jadi5c61a952012-05-04 17:05:35 -0700859 case BASEBAND_SGLTE:
860 src = baseband_sglte;
861 if (have_cmdline) --dst;
862 while ((*dst++ = *src++));
863 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530864
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800865 case BASEBAND_SGLTE2:
866 src = baseband_sglte2;
867 if (have_cmdline) --dst;
868 while ((*dst++ = *src++));
869 break;
870
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530871 case BASEBAND_DSDA:
872 src = baseband_dsda;
873 if (have_cmdline) --dst;
874 while ((*dst++ = *src++));
875 break;
Amol Jadi2a15a272013-01-22 12:03:36 -0800876
877 case BASEBAND_DSDA2:
878 src = baseband_dsda2;
879 if (have_cmdline) --dst;
880 while ((*dst++ = *src++));
881 break;
Vijay Kumar Pendotib228cfc2016-06-13 20:15:23 +0530882 case BASEBAND_APQ_NOWGR:
883 src = baseband_apq_nowgr;
884 if (have_cmdline) --dst;
885 while ((*dst++ = *src++));
886 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800887 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700888
889 if (strlen(display_panel_buf)) {
Dhaval Patel223ec952013-07-18 14:49:44 -0700890 src = display_panel_buf;
891 if (have_cmdline) --dst;
892 while ((*dst++ = *src++));
893 }
Joonwoo Park61112782013-10-02 19:50:39 -0700894
895 if (have_target_boot_params) {
896 if (have_cmdline) --dst;
897 src = target_boot_params;
898 while ((*dst++ = *src++));
vijay kumar870515d2015-08-31 16:37:24 +0530899 free(target_boot_params);
Joonwoo Park61112782013-10-02 19:50:39 -0700900 }
Channagoud Kadabi85c7ec32016-01-28 23:09:21 -0800901
Mayank Grover351a75e2017-05-30 20:06:08 +0530902 if (partition_multislot_is_supported() && have_cmdline)
903 {
904 src = androidboot_slot_suffix;
905 --dst;
906 while ((*dst++ = *src++));
907 --dst;
908 src = SUFFIX_SLOT(current_active_slot);
909 while ((*dst++ = *src++));
Mayank Grover5384ed82018-05-09 12:09:24 +0530910 }
Mayank Grover351a75e2017-05-30 20:06:08 +0530911
Mayank Grover351a75e2017-05-30 20:06:08 +0530912
Mayank Grover5384ed82018-05-09 12:09:24 +0530913 /*
914 * System-As-Root behaviour, system.img should contain both
915 * system content and ramdisk content, and should be mounted at
916 * root(a/b).
917 * Apending skip_ramfs for non a/b builds which use, system as root.
918 */
919 if ((target_uses_system_as_root() ||
920 partition_multislot_is_supported()) &&
921 have_cmdline)
922 {
923 if (!boot_into_recovery)
924 {
925 src = skip_ramfs;
Mayank Grover351a75e2017-05-30 20:06:08 +0530926 --dst;
927 while ((*dst++ = *src++));
Mayank Grover5384ed82018-05-09 12:09:24 +0530928 }
929
930 src = sys_path_cmdline;
931 --dst;
932 while ((*dst++ = *src++));
Mayank Grover3804be72017-06-22 11:59:23 +0530933
Monika Singh292b3e92018-03-17 22:40:23 +0530934#ifndef VERIFIED_BOOT_2
Mayank Grover5384ed82018-05-09 12:09:24 +0530935 src = syspath_buf;
936 --dst;
937 while ((*dst++ = *src++));
Monika Singh292b3e92018-03-17 22:40:23 +0530938#endif
Mayank Grover351a75e2017-05-30 20:06:08 +0530939 }
940
Mayank Grover466d6562018-05-10 14:52:20 +0530941#if HIBERNATION_SUPPORT
942 if (swap_ptn_index != INVALID_PTN)
943 {
944 src = resume_buf;
945 --dst;
946 while ((*dst++ = *src++));
947 }
948#endif
949
Channagoud Kadabi85c7ec32016-01-28 23:09:21 -0800950#if TARGET_CMDLINE_SUPPORT
951 if (target_cmdline_buf && target_cmd_line_len)
952 {
953 if (have_cmdline) --dst;
954 src = target_cmdline_buf;
955 while((*dst++ = *src++));
956 free(target_cmdline_buf);
957 }
958#endif
lijuang83ef4b22018-08-23 11:01:55 +0800959
960#if !VERITY_LE
961 if (dtbo_idx != INVALID_PTN) {
962 src = dtbo_idx_str;
963 --dst;
964 while ((*dst++ = *src++));
965 }
966#endif
Neeti Desaie245d492012-06-01 12:52:13 -0700967 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700968
969
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700970 if (boot_dev_buf)
971 free(boot_dev_buf);
972
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -0800973 if (cmdline_final)
974 dprintf(INFO, "cmdline: %s\n", cmdline_final);
975 else
976 dprintf(INFO, "cmdline is NULL\n");
Neeti Desaie245d492012-06-01 12:52:13 -0700977 return cmdline_final;
978}
979
980unsigned *atag_core(unsigned *ptr)
981{
982 /* CORE */
983 *ptr++ = 2;
984 *ptr++ = 0x54410001;
985
986 return ptr;
987
988}
989
990unsigned *atag_ramdisk(unsigned *ptr, void *ramdisk,
991 unsigned ramdisk_size)
992{
993 if (ramdisk_size) {
994 *ptr++ = 4;
995 *ptr++ = 0x54420005;
996 *ptr++ = (unsigned)ramdisk;
997 *ptr++ = ramdisk_size;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800998 }
999
Neeti Desaie245d492012-06-01 12:52:13 -07001000 return ptr;
1001}
1002
1003unsigned *atag_ptable(unsigned **ptr_addr)
1004{
1005 int i;
1006 struct ptable *ptable;
1007
1008 if ((ptable = flash_get_ptable()) && (ptable->count != 0)) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001009 *(*ptr_addr)++ = 2 + (ptable->count * (sizeof(struct atag_ptbl_entry) /
1010 sizeof(unsigned)));
Neeti Desaie245d492012-06-01 12:52:13 -07001011 *(*ptr_addr)++ = 0x4d534d70;
1012 for (i = 0; i < ptable->count; ++i)
1013 ptentry_to_tag(ptr_addr, ptable_get(ptable, i));
1014 }
1015
1016 return (*ptr_addr);
1017}
1018
1019unsigned *atag_cmdline(unsigned *ptr, const char *cmdline)
1020{
1021 int cmdline_length = 0;
1022 int n;
Neeti Desaie245d492012-06-01 12:52:13 -07001023 char *dest;
1024
Amol Jadi10c7d1c2013-01-25 13:24:29 -08001025 cmdline_length = strlen((const char*)cmdline);
Neeti Desaie245d492012-06-01 12:52:13 -07001026 n = (cmdline_length + 4) & (~3);
1027
1028 *ptr++ = (n / 4) + 2;
1029 *ptr++ = 0x54410009;
1030 dest = (char *) ptr;
Amol Jadi10c7d1c2013-01-25 13:24:29 -08001031 while ((*dest++ = *cmdline++));
Neeti Desaie245d492012-06-01 12:52:13 -07001032 ptr += (n / 4);
1033
1034 return ptr;
1035}
1036
1037unsigned *atag_end(unsigned *ptr)
1038{
Brian Swetland9c4c0752009-01-25 16:23:50 -08001039 /* END */
1040 *ptr++ = 0;
1041 *ptr++ = 0;
1042
Neeti Desaie245d492012-06-01 12:52:13 -07001043 return ptr;
1044}
1045
1046void generate_atags(unsigned *ptr, const char *cmdline,
1047 void *ramdisk, unsigned ramdisk_size)
1048{
vijay kumar21a37452015-12-29 16:23:21 +05301049 unsigned *orig_ptr = ptr;
Neeti Desaie245d492012-06-01 12:52:13 -07001050 ptr = atag_core(ptr);
1051 ptr = atag_ramdisk(ptr, ramdisk, ramdisk_size);
1052 ptr = target_atag_mem(ptr);
1053
1054 /* Skip NAND partition ATAGS for eMMC boot */
1055 if (!target_is_emmc_boot()){
1056 ptr = atag_ptable(&ptr);
1057 }
1058
vijay kumar21a37452015-12-29 16:23:21 +05301059 /*
1060 * Atags size filled till + cmdline size + 1 unsigned for 4-byte boundary + 4 unsigned
1061 * for atag identifier in atag_cmdline and atag_end should be with in MAX_TAGS_SIZE bytes
1062 */
Mayank Groverbc8a2ef2018-02-23 18:03:36 +05301063 if (!cmdline)
1064 return;
1065
vijay kumar21a37452015-12-29 16:23:21 +05301066 if (((ptr - orig_ptr) + strlen(cmdline) + 5 * sizeof(unsigned)) < MAX_TAGS_SIZE) {
1067 ptr = atag_cmdline(ptr, cmdline);
1068 ptr = atag_end(ptr);
1069 }
1070 else {
1071 dprintf(CRITICAL,"Crossing ATAGs Max size allowed\n");
1072 ASSERT(0);
1073 }
Neeti Desaie245d492012-06-01 12:52:13 -07001074}
1075
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001076typedef void entry_func_ptr(unsigned, unsigned, unsigned*);
Neeti Desaie245d492012-06-01 12:52:13 -07001077void boot_linux(void *kernel, unsigned *tags,
1078 const char *cmdline, unsigned machtype,
1079 void *ramdisk, unsigned ramdisk_size)
1080{
Amol Jadi10c7d1c2013-01-25 13:24:29 -08001081 unsigned char *final_cmdline;
Amol Jadib6be5c12012-11-14 13:39:51 -08001082#if DEVICE_TREE
Neeti Desai17379b82012-06-04 18:42:53 -07001083 int ret = 0;
Amol Jadib6be5c12012-11-14 13:39:51 -08001084#endif
1085
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001086 void (*entry)(unsigned, unsigned, unsigned*) = (entry_func_ptr*)(PA((addr_t)kernel));
Deepa Dinamani25a9f762012-11-30 15:57:15 -08001087 uint32_t tags_phys = PA((addr_t)tags);
vijay kumar1a50a642015-11-16 12:41:15 +05301088 struct kernel64_hdr *kptr = ((struct kernel64_hdr*)(PA((addr_t)kernel)));
Deepa Dinamani25a9f762012-11-30 15:57:15 -08001089
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301090 ramdisk = (void *)PA((addr_t)ramdisk);
Neeti Desaie245d492012-06-01 12:52:13 -07001091
Amol Jadi10c7d1c2013-01-25 13:24:29 -08001092 final_cmdline = update_cmdline((const char*)cmdline);
1093
Neeti Desai17379b82012-06-04 18:42:53 -07001094#if DEVICE_TREE
Amol Jadib6be5c12012-11-14 13:39:51 -08001095 dprintf(INFO, "Updating device tree: start\n");
1096
Neeti Desai17379b82012-06-04 18:42:53 -07001097 /* Update the Device Tree */
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301098 ret = update_device_tree((void *)tags,(const char *)final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -07001099 if(ret)
1100 {
1101 dprintf(CRITICAL, "ERROR: Updating Device Tree Failed \n");
1102 ASSERT(0);
1103 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001104 dprintf(INFO, "Updating device tree: done\n");
Neeti Desai17379b82012-06-04 18:42:53 -07001105#else
Neeti Desaie245d492012-06-01 12:52:13 -07001106 /* Generating the Atags */
Amol Jadi10c7d1c2013-01-25 13:24:29 -08001107 generate_atags(tags, final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -07001108#endif
Neeti Desaie245d492012-06-01 12:52:13 -07001109
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001110#if VERIFIED_BOOT
Monika Singh292b3e92018-03-17 22:40:23 +05301111 if (VB_M == target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +05301112 {
1113 if (device.verity_mode == 0) {
lijuangbdd9bb42016-03-01 18:22:17 +08001114#if FBCON_DISPLAY_MSG
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07001115#if ENABLE_VB_ATTEST
Mayank Grover889be1b2017-09-12 20:12:23 +05301116 display_bootverify_menu(DISPLAY_MENU_EIO);
1117 wait_for_users_action();
1118 if(!pwr_key_is_pressed)
1119 shutdown_device();
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07001120#else
Mayank Grover889be1b2017-09-12 20:12:23 +05301121 display_bootverify_menu(DISPLAY_MENU_LOGGING);
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07001122#endif
Mayank Grover889be1b2017-09-12 20:12:23 +05301123 wait_for_users_action();
lijuangbdd9bb42016-03-01 18:22:17 +08001124#else
Mayank Grover889be1b2017-09-12 20:12:23 +05301125 dprintf(CRITICAL,
1126 "The dm-verity is not started in enforcing mode.\nWait for 5 seconds before proceeding\n");
1127 mdelay(5000);
lijuangbdd9bb42016-03-01 18:22:17 +08001128#endif
Mayank Grover889be1b2017-09-12 20:12:23 +05301129 }
lijuangbdd9bb42016-03-01 18:22:17 +08001130 }
lijuangbdd9bb42016-03-01 18:22:17 +08001131#endif
1132
1133#if VERIFIED_BOOT
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001134 /* Write protect the device info */
Channagoud Kadabi3bd9d1e2015-05-05 16:18:20 -07001135 if (!boot_into_recovery && target_build_variant_user() && devinfo_present && mmc_write_protect("devinfo", 1))
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001136 {
1137 dprintf(INFO, "Failed to write protect dev info\n");
1138 ASSERT(0);
1139 }
1140#endif
1141
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08001142 /* Turn off splash screen if enabled */
1143#if DISPLAY_SPLASH_SCREEN
1144 target_display_shutdown();
1145#endif
1146
Veera Sundaram Sankaran67ea0932015-09-25 10:09:30 -07001147 /* Perform target specific cleanup */
1148 target_uninit();
Monika Singh292b3e92018-03-17 22:40:23 +05301149 free_verified_boot_resource(&info);
1150 if (final_cmdline)
1151 free(final_cmdline);
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08001152
Deepa Dinamani33734bc2013-03-06 12:16:06 -08001153 dprintf(INFO, "booting linux @ %p, ramdisk @ %p (%d), tags/device tree @ %p\n",
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301154 entry, ramdisk, ramdisk_size, (void *)tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001155
1156 enter_critical_section();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001157
Amol Jadi4421e652011-06-16 15:00:48 -07001158 /* do any platform specific cleanup before kernel entry */
1159 platform_uninit();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001160
Brian Swetland9c4c0752009-01-25 16:23:50 -08001161 arch_disable_cache(UCACHE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001162
Amol Jadi504f9fe2012-08-16 13:56:48 -07001163#if ARM_WITH_MMU
Brian Swetland9c4c0752009-01-25 16:23:50 -08001164 arch_disable_mmu();
Amol Jadi504f9fe2012-08-16 13:56:48 -07001165#endif
Amol Jadi492d5a52013-03-15 16:12:34 -07001166 bs_set_timestamp(BS_KERNEL_ENTRY);
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001167
1168 if (IS_ARM64(kptr))
1169 /* Jump to a 64bit kernel */
1170 scm_elexec_call((paddr_t)kernel, tags_phys);
1171 else
1172 /* Jump to a 32bit kernel */
1173 entry(0, machtype, (unsigned*)tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001174}
1175
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001176/* Function to check if the memory address range falls within the aboot
1177 * boundaries.
1178 * start: Start of the memory region
1179 * size: Size of the memory region
1180 */
Vijay Kumar Pendoti9c002ad2016-03-09 13:52:45 +05301181int check_aboot_addr_range_overlap(uintptr_t start, uint32_t size)
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001182{
1183 /* Check for boundary conditions. */
Sundarajan Srinivasance2a0ea2013-12-16 17:02:56 -08001184 if ((UINT_MAX - start) < size)
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001185 return -1;
1186
1187 /* Check for memory overlap. */
1188 if ((start < MEMBASE) && ((start + size) <= MEMBASE))
1189 return 0;
Channagoud Kadabi94143912013-10-15 12:53:52 -07001190 else if (start >= (MEMBASE + MEMSIZE))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001191 return 0;
1192 else
1193 return -1;
1194}
1195
Mayank Grovere559cec2017-10-17 15:12:03 +05301196/* Function to check if the memory address range falls beyond ddr region.
1197 * start: Start of the memory region
1198 * size: Size of the memory region
1199 */
1200int check_ddr_addr_range_bound(uintptr_t start, uint32_t size)
1201{
1202 uintptr_t ddr_pa_start_addr = PA(get_ddr_start());
1203 uint64_t ddr_size = smem_get_ddr_size();
1204 uint64_t ddr_pa_end_addr = ddr_pa_start_addr + ddr_size;
1205 uintptr_t pa_start_addr = PA(start);
1206
1207 /* Check for boundary conditions. */
1208 if ((UINT_MAX - start) < size)
1209 return -1;
1210
1211 /* Check if memory range is beyond the ddr range. */
1212 if (pa_start_addr < ddr_pa_start_addr ||
1213 pa_start_addr >= (ddr_pa_end_addr) ||
1214 (pa_start_addr + size) > ddr_pa_end_addr)
1215 return -1;
1216 else
1217 return 0;
1218}
1219
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001220BUF_DMA_ALIGN(buf, BOOT_IMG_MAX_PAGE_SIZE); //Equal to max-supported pagesize
Dima Zavin214cc642009-01-26 11:16:21 -08001221
Mayank Groverdd080e82018-09-04 20:12:13 +05301222int getimage(void **image_buffer, uint32_t *imgsize,
1223 const char *imgname)
Monika Singh292b3e92018-03-17 22:40:23 +05301224{
Mayank Groverdd080e82018-09-04 20:12:13 +05301225 uint32_t loadedindex;
1226 if (image_buffer == NULL || imgsize == NULL ||
Monika Singh292b3e92018-03-17 22:40:23 +05301227 imgname == NULL) {
1228 dprintf(CRITICAL, "getimage: invalid parameters\n");
1229 return -1;
1230 }
Mayank Groverdd080e82018-09-04 20:12:13 +05301231 for (loadedindex = 0; loadedindex < info.num_loaded_images; loadedindex++) {
1232 if (!strncmp(info.images[loadedindex].name, imgname,
Monika Singh292b3e92018-03-17 22:40:23 +05301233 strlen(imgname))) {
Mayank Groverdd080e82018-09-04 20:12:13 +05301234 *image_buffer = info.images[loadedindex].image_buffer;
1235 *imgsize = info.images[loadedindex].imgsize;
Mayank Grover027a9412018-09-04 15:12:05 +05301236 dprintf(SPEW, "getimage(): Loaded image [%s|%d]\n",
1237 info.images[loadedindex].name,
1238 info.images[loadedindex].imgsize);
Monika Singh292b3e92018-03-17 22:40:23 +05301239 return 0;
1240 }
1241 }
1242 return -1;
1243}
1244
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001245static void verify_signed_bootimg(uint32_t bootimg_addr, uint32_t bootimg_size)
1246{
1247 int ret;
Channagoud Kadabia8c623f2015-01-13 14:48:48 -08001248
1249#if !VERIFIED_BOOT
Sundarajan Srinivasance54d6e2013-11-11 12:45:00 -08001250#if IMAGE_VERIF_ALGO_SHA1
1251 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA1;
1252#else
1253 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA256;
1254#endif
Channagoud Kadabia8c623f2015-01-13 14:48:48 -08001255#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001256
1257 /* Assume device is rooted at this time. */
1258 device.is_tampered = 1;
1259
1260 dprintf(INFO, "Authenticating boot image (%d): start\n", bootimg_size);
1261
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001262#if VERIFIED_BOOT
Monika Singh7d2fc272018-03-16 17:16:01 +05301263 uint32_t bootstate;
1264 if(boot_into_recovery &&
Mayank Grover68fbf0d2017-10-24 14:13:39 +05301265 (!partition_multislot_is_supported()))
Monika Singh7d2fc272018-03-16 17:16:01 +05301266 {
1267 ret = boot_verify_image((unsigned char *)bootimg_addr,
1268 bootimg_size, "/recovery", &bootstate);
1269 }
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001270 else
Monika Singh7d2fc272018-03-16 17:16:01 +05301271 {
1272 ret = boot_verify_image((unsigned char *)bootimg_addr,
1273 bootimg_size, "/boot", &bootstate);
1274 }
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001275 boot_verify_print_state();
1276#else
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001277 ret = image_verify((unsigned char *)bootimg_addr,
1278 (unsigned char *)(bootimg_addr + bootimg_size),
1279 bootimg_size,
Sundarajan Srinivasance54d6e2013-11-11 12:45:00 -08001280 auth_algo);
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001281#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001282 dprintf(INFO, "Authenticating boot image: done return value = %d\n", ret);
1283
1284 if (ret)
1285 {
1286 /* Authorized kernel */
1287 device.is_tampered = 0;
Sundarajan Srinivasan3fb21f12013-09-16 18:36:15 -07001288 auth_kernel_img = 1;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001289 }
1290
Amit Blay4aa292f2015-04-28 21:55:59 +03001291#ifdef MDTP_SUPPORT
1292 {
1293 /* Verify MDTP lock.
1294 * For boot & recovery partitions, use aboot's verification result.
1295 */
1296 mdtp_ext_partition_verification_t ext_partition;
1297 ext_partition.partition = boot_into_recovery ? MDTP_PARTITION_RECOVERY : MDTP_PARTITION_BOOT;
1298 ext_partition.integrity_state = device.is_tampered ? MDTP_PARTITION_STATE_INVALID : MDTP_PARTITION_STATE_VALID;
1299 ext_partition.page_size = 0; /* Not needed since already validated */
1300 ext_partition.image_addr = 0; /* Not needed since already validated */
1301 ext_partition.image_size = 0; /* Not needed since already validated */
1302 ext_partition.sig_avail = FALSE; /* Not needed since already validated */
1303 mdtp_fwlock_verify_lock(&ext_partition);
1304 }
1305#endif /* MDTP_SUPPORT */
1306
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001307#if USE_PCOM_SECBOOT
1308 set_tamper_flag(device.is_tampered);
1309#endif
1310
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001311#if VERIFIED_BOOT
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001312 switch(boot_verify_get_state())
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001313 {
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001314 case RED:
lijuanga40d6302015-07-20 20:10:13 +08001315#if FBCON_DISPLAY_MSG
lijuang9a7d3b92015-11-30 14:41:24 +08001316 display_bootverify_menu(DISPLAY_MENU_RED);
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07001317#if ENABLE_VB_ATTEST
1318 mdelay(DELAY_WAIT);
1319 shutdown_device();
1320#else
lijuanga40d6302015-07-20 20:10:13 +08001321 wait_for_users_action();
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07001322#endif
lijuanga40d6302015-07-20 20:10:13 +08001323#else
1324 dprintf(CRITICAL,
1325 "Your device has failed verification and may not work properly.\nWait for 5 seconds before proceeding\n");
1326 mdelay(5000);
1327#endif
1328
1329 break;
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001330 case YELLOW:
lijuanga40d6302015-07-20 20:10:13 +08001331#if FBCON_DISPLAY_MSG
lijuang9a7d3b92015-11-30 14:41:24 +08001332 display_bootverify_menu(DISPLAY_MENU_YELLOW);
lijuanga40d6302015-07-20 20:10:13 +08001333 wait_for_users_action();
1334#else
1335 dprintf(CRITICAL,
1336 "Your device has loaded a different operating system.\nWait for 5 seconds before proceeding\n");
1337 mdelay(5000);
1338#endif
1339 break;
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001340 default:
lijuanga40d6302015-07-20 20:10:13 +08001341 break;
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001342 }
1343#endif
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001344#if !VERIFIED_BOOT
Unnati Gandhi1be04752015-03-27 19:41:53 +05301345 if(device.is_tampered)
1346 {
1347 write_device_info_mmc(&device);
1348 #ifdef TZ_TAMPER_FUSE
Monika Singh98257462018-06-06 11:28:49 +05301349 set_tamper_fuse_cmd(HLOS_IMG_TAMPER_FUSE);
Unnati Gandhi1be04752015-03-27 19:41:53 +05301350 #endif
1351 #ifdef ASSERT_ON_TAMPER
1352 dprintf(CRITICAL, "Device is tampered. Asserting..\n");
1353 ASSERT(0);
1354 #endif
1355 }
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001356#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001357}
1358
Maunik Shah0f3c8ac2014-03-02 17:47:58 +05301359static bool check_format_bit()
1360{
1361 bool ret = false;
1362 int index;
1363 uint64_t offset;
1364 struct boot_selection_info *in = NULL;
1365 char *buf = NULL;
1366
1367 index = partition_get_index("bootselect");
1368 if (index == INVALID_PTN)
1369 {
1370 dprintf(INFO, "Unable to locate /bootselect partition\n");
1371 return ret;
1372 }
1373 offset = partition_get_offset(index);
1374 if(!offset)
1375 {
1376 dprintf(INFO, "partition /bootselect doesn't exist\n");
1377 return ret;
1378 }
1379 buf = (char *) memalign(CACHE_LINE, ROUNDUP(page_size, CACHE_LINE));
Mayank Grover48860402016-11-29 12:34:53 +05301380 mmc_set_lun(partition_get_lun(index));
Maunik Shah0f3c8ac2014-03-02 17:47:58 +05301381 ASSERT(buf);
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301382 if (mmc_read(offset, (uint32_t *)buf, page_size))
Maunik Shah0f3c8ac2014-03-02 17:47:58 +05301383 {
1384 dprintf(INFO, "mmc read failure /bootselect %d\n", page_size);
1385 free(buf);
1386 return ret;
1387 }
1388 in = (struct boot_selection_info *) buf;
1389 if ((in->signature == BOOTSELECT_SIGNATURE) &&
1390 (in->version == BOOTSELECT_VERSION)) {
1391 if ((in->state_info & BOOTSELECT_FORMAT) &&
1392 !(in->state_info & BOOTSELECT_FACTORY))
1393 ret = true;
1394 } else {
1395 dprintf(CRITICAL, "Signature: 0x%08x or version: 0x%08x mismatched of /bootselect\n",
1396 in->signature, in->version);
1397 ASSERT(0);
1398 }
1399 free(buf);
1400 return ret;
1401}
1402
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001403void boot_verifier_init()
1404{
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001405 uint32_t boot_state;
1406 /* Check if device unlock */
1407 if(device.is_unlocked)
1408 {
1409 boot_verify_send_event(DEV_UNLOCK);
1410 boot_verify_print_state();
1411 dprintf(CRITICAL, "Device is unlocked! Skipping verification...\n");
1412 return;
1413 }
1414 else
1415 {
1416 boot_verify_send_event(BOOT_INIT);
1417 }
1418
1419 /* Initialize keystore */
1420 boot_state = boot_verify_keystore_init();
1421 if(boot_state == YELLOW)
1422 {
1423 boot_verify_print_state();
1424 dprintf(CRITICAL, "Keystore verification failed! Continuing anyways...\n");
1425 }
1426}
1427
Shashank Mittal23b8f422010-04-16 19:27:21 -07001428int boot_linux_from_mmc(void)
1429{
1430 struct boot_img_hdr *hdr = (void*) buf;
1431 struct boot_img_hdr *uhdr;
1432 unsigned offset = 0;
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07001433 int rcode;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001434 unsigned long long ptn = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001435 int index = INVALID_PTN;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001436
Shashank Mittalcd98d472011-08-02 14:29:24 -07001437 unsigned char *image_addr = 0;
1438 unsigned kernel_actual;
1439 unsigned ramdisk_actual;
1440 unsigned imagesize_actual;
Neeti Desai465491e2012-07-31 12:53:35 -07001441 unsigned second_actual = 0;
Neeti Desai465491e2012-07-31 12:53:35 -07001442
Matthew Qin271927e2015-03-31 22:07:07 -04001443 unsigned int dtb_size = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08001444 unsigned int out_len = 0;
1445 unsigned int out_avai_len = 0;
1446 unsigned char *out_addr = NULL;
1447 uint32_t dtb_offset = 0;
1448 unsigned char *kernel_start_addr = NULL;
1449 unsigned int kernel_size = 0;
Ameya Thakur10a33452016-06-13 14:24:26 -07001450 unsigned int patched_kernel_hdr_size = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08001451 int rc;
Monika Singh292b3e92018-03-17 22:40:23 +05301452#if VERIFIED_BOOT_2
1453 int status;
Mayank Grover027a9412018-09-04 15:12:05 +05301454 void *dtbo_image_buf = NULL;
1455 uint32_t dtbo_image_sz = 0;
Mayank Grover9df84c02018-08-30 15:46:35 +05301456 void *vbmeta_image_buf = NULL;
1457 uint32_t vbmeta_image_sz = 0;
Monika Singh292b3e92018-03-17 22:40:23 +05301458#endif
Mayank Groverc93cad82017-10-03 12:23:45 +05301459 char *ptn_name = NULL;
Neeti Desai465491e2012-07-31 12:53:35 -07001460#if DEVICE_TREE
1461 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -07001462 struct dt_entry dt_entry;
Neeti Desai465491e2012-07-31 12:53:35 -07001463 unsigned dt_table_offset;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001464 uint32_t dt_actual;
Deepa Dinamani19648b42013-09-05 17:05:55 -07001465 uint32_t dt_hdr_size;
Matthew Qin271927e2015-03-31 22:07:07 -04001466 unsigned char *best_match_dt_addr = NULL;
Neeti Desai465491e2012-07-31 12:53:35 -07001467#endif
Matthew Qin49e51fa2015-02-09 10:40:45 +08001468 struct kernel64_hdr *kptr = NULL;
Mayank Grover351a75e2017-05-30 20:06:08 +05301469 int current_active_slot = INVALID;
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001470
Maunik Shah0f3c8ac2014-03-02 17:47:58 +05301471 if (check_format_bit())
1472 boot_into_recovery = 1;
1473
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07001474 if (!boot_into_recovery) {
1475 memset(ffbm_mode_string, '\0', sizeof(ffbm_mode_string));
1476 rcode = get_ffbm(ffbm_mode_string, sizeof(ffbm_mode_string));
1477 if (rcode <= 0) {
1478 boot_into_ffbm = false;
1479 if (rcode < 0)
1480 dprintf(CRITICAL,"failed to get ffbm cookie");
1481 } else
1482 boot_into_ffbm = true;
1483 } else
1484 boot_into_ffbm = false;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001485 uhdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
1486 if (!memcmp(uhdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1487 dprintf(INFO, "Unified boot method!\n");
1488 hdr = uhdr;
1489 goto unified_boot;
1490 }
Mayank Groverc93cad82017-10-03 12:23:45 +05301491
1492 /* For a/b recovery image code is on boot partition.
1493 If we support multislot, always use boot partition. */
1494 if (boot_into_recovery &&
1495 (!partition_multislot_is_supported()))
1496 ptn_name = "recovery";
1497 else
1498 ptn_name = "boot";
1499
1500 index = partition_get_index(ptn_name);
1501 ptn = partition_get_offset(index);
1502 if(ptn == 0) {
1503 dprintf(CRITICAL, "ERROR: No %s partition found\n", ptn_name);
1504 return -1;
Kinson Chikf1a43512011-07-14 11:28:39 -07001505 }
Mayank Groverc93cad82017-10-03 12:23:45 +05301506
Channagoud Kadabief0547c2015-02-10 12:57:38 -08001507 /* Set Lun for boot & recovery partitions */
1508 mmc_set_lun(partition_get_lun(index));
Shashank Mittal23b8f422010-04-16 19:27:21 -07001509
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301510 if (mmc_read(ptn + offset, (uint32_t *) buf, page_size)) {
Shashank Mittal23b8f422010-04-16 19:27:21 -07001511 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
1512 return -1;
1513 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001514
1515 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001516 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Mayank Grover351a75e2017-05-30 20:06:08 +05301517 return ERR_INVALID_BOOT_MAGIC;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001518 }
1519
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07001520 if (hdr->page_size && (hdr->page_size != page_size)) {
vijay kumar2e21b3a2014-06-26 17:40:15 +05301521
1522 if (hdr->page_size > BOOT_IMG_MAX_PAGE_SIZE) {
1523 dprintf(CRITICAL, "ERROR: Invalid page size\n");
1524 return -1;
1525 }
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07001526 page_size = hdr->page_size;
1527 page_mask = page_size - 1;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001528 }
1529
Matthew Qin49e51fa2015-02-09 10:40:45 +08001530 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1531 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Kishor PKd8ddcad2017-07-27 13:53:57 +05301532 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001533
Matthew Qin49e51fa2015-02-09 10:40:45 +08001534 image_addr = (unsigned char *)target_get_scratch_address();
Kishor PK9e91b592017-05-24 12:14:55 +05301535 memcpy(image_addr, (void *)buf, page_size);
1536
1537 /* ensure commandline is terminated */
1538 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
Matthew Qin49e51fa2015-02-09 10:40:45 +08001539
1540#if DEVICE_TREE
Parth Dixit4097b622016-03-15 14:42:27 +05301541#ifndef OSVERSION_IN_BOOTIMAGE
1542 dt_size = hdr->dt_size;
1543#endif
1544 dt_actual = ROUND_TO_PAGE(dt_size, page_mask);
Kishor PKd8ddcad2017-07-27 13:53:57 +05301545 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 +05301546 dprintf(CRITICAL, "Integer overflow detected in bootimage header fields at %u in %s\n",__LINE__,__FILE__);
1547 return -1;
1548 }
Kishor PKd8ddcad2017-07-27 13:53:57 +05301549 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + second_actual + dt_actual);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001550#else
Kishor PKd8ddcad2017-07-27 13:53:57 +05301551 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 +05301552 dprintf(CRITICAL, "Integer overflow detected in bootimage header fields at %u in %s\n",__LINE__,__FILE__);
1553 return -1;
1554 }
Kishor PKd8ddcad2017-07-27 13:53:57 +05301555 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + second_actual);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001556#endif
1557
1558#if VERIFIED_BOOT
1559 boot_verifier_init();
1560#endif
1561
Vijay Kumar Pendoti9c002ad2016-03-09 13:52:45 +05301562 if (check_aboot_addr_range_overlap((uintptr_t) image_addr, imagesize_actual))
Matthew Qin49e51fa2015-02-09 10:40:45 +08001563 {
1564 dprintf(CRITICAL, "Boot image buffer address overlaps with aboot addresses.\n");
1565 return -1;
1566 }
1567
Matthew Qinbb7923d2015-02-09 10:56:09 +08001568 /*
1569 * Update loading flow of bootimage to support compressed/uncompressed
1570 * bootimage on both 64bit and 32bit platform.
1571 * 1. Load bootimage from emmc partition onto DDR.
1572 * 2. Check if bootimage is gzip format. If yes, decompress compressed kernel
1573 * 3. Check kernel header and update kernel load addr for 64bit and 32bit
1574 * platform accordingly.
1575 * 4. Sanity Check on kernel_addr and ramdisk_addr and copy data.
1576 */
Mayank Grover351a75e2017-05-30 20:06:08 +05301577 if (partition_multislot_is_supported())
1578 {
1579 current_active_slot = partition_find_active_slot();
1580 dprintf(INFO, "Loading boot image (%d) active_slot(%s): start\n",
1581 imagesize_actual, SUFFIX_SLOT(current_active_slot));
1582 }
1583 else
1584 {
1585 dprintf(INFO, "Loading (%s) image (%d): start\n",
1586 (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual);
1587 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08001588 bs_set_timestamp(BS_KERNEL_LOAD_START);
1589
Gaurav Nebhwani43e2a462016-03-17 21:32:56 +05301590 if ((target_get_max_flash_size() - page_size) < imagesize_actual)
1591 {
1592 dprintf(CRITICAL, "booimage size is greater than DDR can hold\n");
1593 return -1;
1594 }
Kishor PK9e91b592017-05-24 12:14:55 +05301595 offset = page_size;
1596 /* Read image without signature and header*/
1597 if (mmc_read(ptn + offset, (void *)(image_addr + offset), imagesize_actual - page_size))
Matthew Qin49e51fa2015-02-09 10:40:45 +08001598 {
1599 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
1600 return -1;
1601 }
1602
Mayank Grover351a75e2017-05-30 20:06:08 +05301603 if (partition_multislot_is_supported())
1604 {
1605 dprintf(INFO, "Loading boot image (%d) active_slot(%s): done\n",
1606 imagesize_actual, SUFFIX_SLOT(current_active_slot));
1607 }
1608 else
1609 {
1610 dprintf(INFO, "Loading (%s) image (%d): done\n",
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001611 (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual);
1612
Mayank Grover351a75e2017-05-30 20:06:08 +05301613 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08001614 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
1615
1616 /* Authenticate Kernel */
1617 dprintf(INFO, "use_signed_kernel=%d, is_unlocked=%d, is_tampered=%d.\n",
1618 (int) target_use_signed_kernel(),
1619 device.is_unlocked,
1620 device.is_tampered);
Monika Singh292b3e92018-03-17 22:40:23 +05301621#if VERIFIED_BOOT_2
1622 offset = imagesize_actual;
1623 if (check_aboot_addr_range_overlap((uintptr_t)image_addr + offset, page_size))
1624 {
1625 dprintf(CRITICAL, "Signature read buffer address overlaps with aboot addresses.\n");
1626 return -1;
1627 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08001628
Monika Singh292b3e92018-03-17 22:40:23 +05301629 /* Read signature */
1630 if(mmc_read(ptn + offset, (void *)(image_addr + offset), page_size))
1631 {
1632 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
1633 return -1;
1634 }
1635
Mayank Grover027a9412018-09-04 15:12:05 +05301636 /* load and validate dtbo partition */
1637 load_validate_dtbo_image(&dtbo_image_buf, &dtbo_image_sz);
1638
Mayank Grover9df84c02018-08-30 15:46:35 +05301639 /* load vbmeta partition */
1640 load_vbmeta_image(&vbmeta_image_buf, &vbmeta_image_sz);
1641
Monika Singh292b3e92018-03-17 22:40:23 +05301642 memset(&info, 0, sizeof(bootinfo));
Mayank Grover027a9412018-09-04 15:12:05 +05301643
1644 /* Pass loaded boot image passed */
1645 info.images[IMG_BOOT].image_buffer = image_addr;
1646 info.images[IMG_BOOT].imgsize = imagesize_actual;
1647 info.images[IMG_BOOT].name = ptn_name;
1648 ++info.num_loaded_images;
1649
1650 /* Pass loaded dtbo image */
1651 if (dtbo_image_buf != NULL) {
1652 info.images[IMG_DTBO].image_buffer = dtbo_image_buf;
1653 info.images[IMG_DTBO].imgsize = dtbo_image_sz;
1654 info.images[IMG_DTBO].name = "dtbo";
1655 ++info.num_loaded_images;
1656 }
1657
Mayank Grover9df84c02018-08-30 15:46:35 +05301658 /* Pass loaded vbmeta image */
1659 if (vbmeta_image_buf != NULL) {
1660 info.images[IMG_VBMETA].image_buffer = vbmeta_image_buf;
1661 info.images[IMG_VBMETA].imgsize = vbmeta_image_sz;
1662 info.images[IMG_VBMETA].name = "vbmeta";
1663 ++info.num_loaded_images;
1664 }
1665
Monika Singh292b3e92018-03-17 22:40:23 +05301666 info.multi_slot_boot = partition_multislot_is_supported();
1667 info.bootreason_alarm = boot_reason_alarm;
1668 info.bootinto_recovery = boot_into_recovery;
1669 status = load_image_and_auth(&info);
1670 if(status)
1671 return -1;
1672
1673 vbcmdline = info.vbcmdline;
Mayank Grover9df84c02018-08-30 15:46:35 +05301674
1675 /* Free the buffer allocated to vbmeta post verification */
1676 free(vbmeta_image_buf);
1677 --info.num_loaded_images;
Monika Singh292b3e92018-03-17 22:40:23 +05301678#else
Channagoud Kadabi736c4962015-08-21 11:56:52 -07001679 /* Change the condition a little bit to include the test framework support.
1680 * We would never reach this point if device is in fastboot mode, even if we did
1681 * that means we are in test mode, so execute kernel authentication part for the
1682 * tests */
Parth Dixitcb0c6082015-12-30 15:01:13 +05301683 if((target_use_signed_kernel() && (!device.is_unlocked)) || is_test_mode_enabled())
Matthew Qin49e51fa2015-02-09 10:40:45 +08001684 {
1685 offset = imagesize_actual;
Vijay Kumar Pendoti9c002ad2016-03-09 13:52:45 +05301686 if (check_aboot_addr_range_overlap((uintptr_t)image_addr + offset, page_size))
Matthew Qin49e51fa2015-02-09 10:40:45 +08001687 {
1688 dprintf(CRITICAL, "Signature read buffer address overlaps with aboot addresses.\n");
1689 return -1;
1690 }
1691
1692 /* Read signature */
1693 if(mmc_read(ptn + offset, (void *)(image_addr + offset), page_size))
1694 {
1695 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
1696 return -1;
1697 }
1698
1699 verify_signed_bootimg((uint32_t)image_addr, imagesize_actual);
Channagoud Kadabi736c4962015-08-21 11:56:52 -07001700 /* The purpose of our test is done here */
Parth Dixitcb0c6082015-12-30 15:01:13 +05301701 if(is_test_mode_enabled() && auth_kernel_img)
Channagoud Kadabi736c4962015-08-21 11:56:52 -07001702 return 0;
Matthew Qin49e51fa2015-02-09 10:40:45 +08001703 } else {
1704 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
1705 #ifdef TZ_SAVE_KERNEL_HASH
1706 aboot_save_boot_hash_mmc((uint32_t) image_addr, imagesize_actual);
1707 #endif /* TZ_SAVE_KERNEL_HASH */
Amit Blay4aa292f2015-04-28 21:55:59 +03001708
1709#ifdef MDTP_SUPPORT
1710 {
1711 /* Verify MDTP lock.
1712 * For boot & recovery partitions, MDTP will use boot_verifier APIs,
1713 * since verification was skipped in aboot. The signature is not part of the loaded image.
1714 */
1715 mdtp_ext_partition_verification_t ext_partition;
1716 ext_partition.partition = boot_into_recovery ? MDTP_PARTITION_RECOVERY : MDTP_PARTITION_BOOT;
1717 ext_partition.integrity_state = MDTP_PARTITION_STATE_UNSET;
1718 ext_partition.page_size = page_size;
1719 ext_partition.image_addr = (uint32)image_addr;
1720 ext_partition.image_size = imagesize_actual;
1721 ext_partition.sig_avail = FALSE;
1722 mdtp_fwlock_verify_lock(&ext_partition);
1723 }
1724#endif /* MDTP_SUPPORT */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001725 }
Monika Singh292b3e92018-03-17 22:40:23 +05301726#endif
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001727
Sridhar Parasuram00bfedb2015-05-26 14:21:27 -07001728#if VERIFIED_BOOT
taozhang93088bd2016-11-16 15:50:46 +08001729 if((boot_verify_get_state() == ORANGE) && (!boot_into_ffbm))
Reut Zysmanba8a9d52016-02-18 11:44:04 +02001730 {
1731#if FBCON_DISPLAY_MSG
1732 display_bootverify_menu(DISPLAY_MENU_ORANGE);
1733 wait_for_users_action();
1734#else
1735 dprintf(CRITICAL,
1736 "Your device has been unlocked and can't be trusted.\nWait for 5 seconds before proceeding\n");
1737 mdelay(5000);
1738#endif
1739 }
1740#endif
1741
1742#if VERIFIED_BOOT
Monika Singh292b3e92018-03-17 22:40:23 +05301743 if (VB_M == target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +05301744 {
1745 /* set boot and system versions. */
1746 set_os_version((unsigned char *)image_addr);
1747 // send root of trust
1748 if(!send_rot_command((uint32_t)device.is_unlocked))
1749 ASSERT(0);
1750 }
Parth Dixitbc9b6492015-10-18 00:41:38 +05301751#endif
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001752 /*
Matthew Qinbb7923d2015-02-09 10:56:09 +08001753 * Check if the kernel image is a gzip package. If yes, need to decompress it.
1754 * If not, continue booting.
1755 */
1756 if (is_gzip_package((unsigned char *)(image_addr + page_size), hdr->kernel_size))
1757 {
1758 out_addr = (unsigned char *)(image_addr + imagesize_actual + page_size);
1759 out_avai_len = target_get_max_flash_size() - imagesize_actual - page_size;
Mayank Grover027a9412018-09-04 15:12:05 +05301760#if VERIFIED_BOOT_2
1761 if (dtbo_image_sz)
1762 out_avai_len -= DTBO_IMG_BUF;
1763#endif
Matthew Qin0b15b322015-05-19 05:20:54 -04001764 dprintf(INFO, "decompressing kernel image: start\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001765 rc = decompress((unsigned char *)(image_addr + page_size),
1766 hdr->kernel_size, out_addr, out_avai_len,
1767 &dtb_offset, &out_len);
1768 if (rc)
1769 {
Matthew Qin0b15b322015-05-19 05:20:54 -04001770 dprintf(CRITICAL, "decompressing kernel image failed!!!\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001771 ASSERT(0);
1772 }
1773
Matthew Qin0b15b322015-05-19 05:20:54 -04001774 dprintf(INFO, "decompressing kernel image: done\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001775 kptr = (struct kernel64_hdr *)out_addr;
1776 kernel_start_addr = out_addr;
1777 kernel_size = out_len;
1778 } else {
Ameya Thakur10a33452016-06-13 14:24:26 -07001779 dprintf(INFO, "Uncpmpressed kernel in use\n");
1780 if (!strncmp((char*)(image_addr + page_size),
1781 PATCHED_KERNEL_MAGIC,
1782 sizeof(PATCHED_KERNEL_MAGIC) - 1)) {
1783 dprintf(INFO, "Patched kernel detected\n");
1784 kptr = (struct kernel64_hdr *)(image_addr + page_size +
1785 PATCHED_KERNEL_HEADER_SIZE);
1786 //The size of the kernel is stored at start of kernel image + 16
1787 //The dtb would start just after the kernel
1788 dtb_offset = *((uint32_t*)((unsigned char*)
1789 (image_addr + page_size +
1790 sizeof(PATCHED_KERNEL_MAGIC) -
1791 1)));
1792 //The actual kernel starts after the 20 byte header.
1793 kernel_start_addr = (unsigned char*)(image_addr +
1794 page_size + PATCHED_KERNEL_HEADER_SIZE);
1795 kernel_size = hdr->kernel_size;
1796 patched_kernel_hdr_size = PATCHED_KERNEL_HEADER_SIZE;
1797 } else {
1798 dprintf(INFO, "Kernel image not patched..Unable to locate dt offset\n");
1799 kptr = (struct kernel64_hdr *)(image_addr + page_size);
1800 kernel_start_addr = (unsigned char *)(image_addr + page_size);
1801 kernel_size = hdr->kernel_size;
1802 }
Matthew Qinbb7923d2015-02-09 10:56:09 +08001803 }
1804
1805 /*
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001806 * Update the kernel/ramdisk/tags address if the boot image header
1807 * has default values, these default values come from mkbootimg when
1808 * the boot image is flashed using fastboot flash:raw
1809 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001810 update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr));
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001811
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001812 /* Get virtual addresses since the hdr saves physical addresses. */
1813 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
1814 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
1815 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001816
Matthew Qinbb7923d2015-02-09 10:56:09 +08001817 kernel_size = ROUND_TO_PAGE(kernel_size, page_mask);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001818 /* Check if the addresses in the header are valid. */
Matthew Qinbb7923d2015-02-09 10:56:09 +08001819 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_size) ||
Mayank Grovere559cec2017-10-17 15:12:03 +05301820 check_ddr_addr_range_bound(hdr->kernel_addr, kernel_size) ||
1821 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual) ||
1822 check_ddr_addr_range_bound(hdr->ramdisk_addr, ramdisk_actual))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001823 {
Mayank Grovere559cec2017-10-17 15:12:03 +05301824 dprintf(CRITICAL, "kernel/ramdisk addresses are not valid.\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001825 return -1;
1826 }
1827
1828#ifndef DEVICE_TREE
Mayank Grovere559cec2017-10-17 15:12:03 +05301829 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE) ||
1830 check_ddr_addr_range_bound(hdr->tags_addr, MAX_TAGS_SIZE))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001831 {
Mayank Grovere559cec2017-10-17 15:12:03 +05301832 dprintf(CRITICAL, "Tags addresses are not valid.\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001833 return -1;
1834 }
1835#endif
1836
Matthew Qin49e51fa2015-02-09 10:40:45 +08001837 /* Move kernel, ramdisk and device tree to correct address */
Matthew Qinbb7923d2015-02-09 10:56:09 +08001838 memmove((void*) hdr->kernel_addr, kernel_start_addr, kernel_size);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001839 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001840
Matthew Qin49e51fa2015-02-09 10:40:45 +08001841 #if DEVICE_TREE
Parth Dixit4097b622016-03-15 14:42:27 +05301842 if(dt_size) {
Matthew Qin49e51fa2015-02-09 10:40:45 +08001843 dt_table_offset = ((uint32_t)image_addr + page_size + kernel_actual + ramdisk_actual + second_actual);
1844 table = (struct dt_table*) dt_table_offset;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001845
Matthew Qin49e51fa2015-02-09 10:40:45 +08001846 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
1847 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1848 return -1;
1849 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001850
P.V. Phani Kumar82916762016-03-09 09:20:19 +05301851 /* Its Error if, dt_hdr_size (table->num_entries * dt_entry size + Dev_Tree Header)
1852 goes beyound hdr->dt_size*/
Parth Dixit4097b622016-03-15 14:42:27 +05301853 if (dt_hdr_size > ROUND_TO_PAGE(dt_size,hdr->page_size)) {
P.V. Phani Kumar82916762016-03-09 09:20:19 +05301854 dprintf(CRITICAL, "ERROR: Invalid Device Tree size \n");
1855 return -1;
1856 }
1857
Matthew Qin49e51fa2015-02-09 10:40:45 +08001858 /* Find index of device tree within device tree table */
1859 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
1860 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1861 return -1;
1862 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001863
P.V. Phani Kumar3c333202016-03-09 11:54:37 +05301864 if(dt_entry.offset > (UINT_MAX - dt_entry.size)) {
1865 dprintf(CRITICAL, "ERROR: Device tree contents are Invalid\n");
1866 return -1;
1867 }
1868
1869 /* Ensure we are not overshooting dt_size with the dt_entry selected */
Parth Dixit4097b622016-03-15 14:42:27 +05301870 if ((dt_entry.offset + dt_entry.size) > dt_size) {
P.V. Phani Kumar3c333202016-03-09 11:54:37 +05301871 dprintf(CRITICAL, "ERROR: Device tree contents are Invalid\n");
1872 return -1;
1873 }
1874
Matthew Qin271927e2015-03-31 22:07:07 -04001875 if (is_gzip_package((unsigned char *)dt_table_offset + dt_entry.offset, dt_entry.size))
1876 {
1877 unsigned int compressed_size = 0;
1878 out_addr += out_len;
1879 out_avai_len -= out_len;
Matthew Qin0b15b322015-05-19 05:20:54 -04001880 dprintf(INFO, "decompressing dtb: start\n");
Matthew Qin271927e2015-03-31 22:07:07 -04001881 rc = decompress((unsigned char *)dt_table_offset + dt_entry.offset,
1882 dt_entry.size, out_addr, out_avai_len,
1883 &compressed_size, &dtb_size);
1884 if (rc)
1885 {
Matthew Qin0b15b322015-05-19 05:20:54 -04001886 dprintf(CRITICAL, "decompressing dtb failed!!!\n");
Matthew Qin271927e2015-03-31 22:07:07 -04001887 ASSERT(0);
1888 }
1889
Matthew Qin0b15b322015-05-19 05:20:54 -04001890 dprintf(INFO, "decompressing dtb: done\n");
Matthew Qin271927e2015-03-31 22:07:07 -04001891 best_match_dt_addr = out_addr;
1892 } else {
1893 best_match_dt_addr = (unsigned char *)dt_table_offset + dt_entry.offset;
1894 dtb_size = dt_entry.size;
1895 }
1896
Matthew Qin49e51fa2015-02-09 10:40:45 +08001897 /* Validate and Read device device tree in the tags_addr */
Mayank Grovere559cec2017-10-17 15:12:03 +05301898 if (check_aboot_addr_range_overlap(hdr->tags_addr, dtb_size) ||
1899 check_ddr_addr_range_bound(hdr->tags_addr, dtb_size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001900 {
Mayank Grovere559cec2017-10-17 15:12:03 +05301901 dprintf(CRITICAL, "Device tree addresses are not valid\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001902 return -1;
1903 }
Shashank Mittal162244e2011-08-08 19:01:25 -07001904
Matthew Qin271927e2015-03-31 22:07:07 -04001905 memmove((void *)hdr->tags_addr, (char *)best_match_dt_addr, dtb_size);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001906 } else {
1907 /* Validate the tags_addr */
Mayank Grovere559cec2017-10-17 15:12:03 +05301908 if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual) ||
1909 check_ddr_addr_range_bound(hdr->tags_addr, kernel_actual))
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001910 {
Mayank Grovere559cec2017-10-17 15:12:03 +05301911 dprintf(CRITICAL, "Device tree addresses are not valid.\n");
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001912 return -1;
1913 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08001914 /*
1915 * If appended dev tree is found, update the atags with
1916 * memory address to the DTB appended location on RAM.
1917 * Else update with the atags address in the kernel header
1918 */
1919 void *dtb;
Ameya Thakur10a33452016-06-13 14:24:26 -07001920 dtb = dev_tree_appended(
1921 (void*)(image_addr + page_size +
1922 patched_kernel_hdr_size),
1923 hdr->kernel_size, dtb_offset,
1924 (void *)hdr->tags_addr);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001925 if (!dtb) {
1926 dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001927 return -1;
1928 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07001929 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08001930 #endif
Shashank Mittal23b8f422010-04-16 19:27:21 -07001931
Stanimir Varbanov69ec5462013-07-18 18:17:42 +03001932 if (boot_into_recovery && !device.is_unlocked && !device.is_tampered)
1933 target_load_ssd_keystore();
1934
Shashank Mittal23b8f422010-04-16 19:27:21 -07001935unified_boot:
Shashank Mittal23b8f422010-04-16 19:27:21 -07001936
Dima Zavin77e41f32013-03-06 16:10:43 -08001937 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07001938 (const char *)hdr->cmdline, board_machtype(),
Shashank Mittal23b8f422010-04-16 19:27:21 -07001939 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
1940
1941 return 0;
1942}
1943
Dima Zavin214cc642009-01-26 11:16:21 -08001944int boot_linux_from_flash(void)
1945{
1946 struct boot_img_hdr *hdr = (void*) buf;
Dima Zavin214cc642009-01-26 11:16:21 -08001947 struct ptentry *ptn;
1948 struct ptable *ptable;
1949 unsigned offset = 0;
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001950
Shashank Mittalcd98d472011-08-02 14:29:24 -07001951 unsigned char *image_addr = 0;
1952 unsigned kernel_actual;
1953 unsigned ramdisk_actual;
1954 unsigned imagesize_actual;
vijay kumar8f53e362015-11-24 13:38:11 +05301955 unsigned second_actual = 0;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001956
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001957#if DEVICE_TREE
Monika Singh292b3e92018-03-17 22:40:23 +05301958 struct dt_table *table = NULL;
Joel Kingaa335dc2013-06-03 16:11:08 -07001959 struct dt_entry dt_entry;
vijay kumar8f53e362015-11-24 13:38:11 +05301960 unsigned dt_table_offset;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001961 uint32_t dt_actual;
Monika Singh292b3e92018-03-17 22:40:23 +05301962 uint32_t dt_hdr_size = 0;
Vivek Kumar07bd0862018-04-10 14:19:23 +05301963 uint32_t dtb_offset = 0;
vijay kumar8f53e362015-11-24 13:38:11 +05301964 unsigned int dtb_size = 0;
1965 unsigned char *best_match_dt_addr = NULL;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001966#endif
1967
David Ng183a7422009-12-07 14:55:21 -08001968 if (target_is_emmc_boot()) {
1969 hdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
1970 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1971 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
1972 return -1;
1973 }
1974 goto continue_boot;
1975 }
1976
Dima Zavin214cc642009-01-26 11:16:21 -08001977 ptable = flash_get_ptable();
1978 if (ptable == NULL) {
1979 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1980 return -1;
1981 }
1982
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001983 if(!boot_into_recovery)
1984 {
1985 ptn = ptable_find(ptable, "boot");
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001986
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001987 if (ptn == NULL) {
1988 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1989 return -1;
1990 }
1991 }
1992 else
1993 {
1994 ptn = ptable_find(ptable, "recovery");
1995 if (ptn == NULL) {
1996 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
1997 return -1;
1998 }
Dima Zavin214cc642009-01-26 11:16:21 -08001999 }
2000
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05302001 /* Read boot.img header from flash */
Shashank Mittaldcc2e352009-11-19 19:11:16 -08002002 if (flash_read(ptn, offset, buf, page_size)) {
Dima Zavin214cc642009-01-26 11:16:21 -08002003 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
2004 return -1;
2005 }
Dima Zavin214cc642009-01-26 11:16:21 -08002006
2007 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07002008 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Dima Zavin214cc642009-01-26 11:16:21 -08002009 return -1;
2010 }
2011
Shashank Mittaldcc2e352009-11-19 19:11:16 -08002012 if (hdr->page_size != page_size) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07002013 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 -08002014 return -1;
2015 }
2016
Kishor PK9e91b592017-05-24 12:14:55 +05302017 image_addr = (unsigned char *)target_get_scratch_address();
2018 memcpy(image_addr, (void *)buf, page_size);
vijay kumar287bb542015-09-29 13:01:52 +05302019
Channagoud Kadabia22144f2013-03-20 11:49:01 -07002020 /*
2021 * Update the kernel/ramdisk/tags address if the boot image header
2022 * has default values, these default values come from mkbootimg when
2023 * the boot image is flashed using fastboot flash:raw
2024 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08002025 update_ker_tags_rdisk_addr(hdr, false);
Channagoud Kadabia22144f2013-03-20 11:49:01 -07002026
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002027 /* Get virtual addresses since the hdr saves physical addresses. */
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002028 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
2029 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
2030 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
2031
2032 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
2033 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Mayank Grover032736f2017-07-14 20:34:51 +05302034 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002035
Kishor PK9e91b592017-05-24 12:14:55 +05302036 /* ensure commandline is terminated */
2037 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
2038
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002039 /* Check if the addresses in the header are valid. */
2040 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
Mayank Grovere559cec2017-10-17 15:12:03 +05302041 check_ddr_addr_range_bound(hdr->kernel_addr, kernel_actual) ||
2042 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual) ||
2043 check_ddr_addr_range_bound(hdr->ramdisk_addr, ramdisk_actual))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002044 {
Mayank Grovere559cec2017-10-17 15:12:03 +05302045 dprintf(CRITICAL, "kernel/ramdisk addresses are not valid.\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002046 return -1;
2047 }
2048
2049#ifndef DEVICE_TREE
Kishor PKd8ddcad2017-07-27 13:53:57 +05302050 if (UINT_MAX < ((uint64_t)kernel_actual + (uint64_t)ramdisk_actual+ (uint64_t)second_actual + page_size)) {
Mayank Grover032736f2017-07-14 20:34:51 +05302051 dprintf(CRITICAL, "Integer overflow detected in bootimage header fields\n");
2052 return -1;
2053 }
Kishor PKd8ddcad2017-07-27 13:53:57 +05302054 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + second_actual);
Mayank Grover032736f2017-07-14 20:34:51 +05302055
Mayank Grovere559cec2017-10-17 15:12:03 +05302056 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE) ||
2057 check_ddr_addr_range_bound(hdr->tags_addr, MAX_TAGS_SIZE))
Vijay Kumar Pendotid3ed20e2016-09-20 00:34:46 +05302058 {
Mayank Grovere559cec2017-10-17 15:12:03 +05302059 dprintf(CRITICAL, "Tags addresses are not valid.\n");
Vijay Kumar Pendotid3ed20e2016-09-20 00:34:46 +05302060 return -1;
2061 }
2062#else
2063
2064#ifndef OSVERSION_IN_BOOTIMAGE
2065 dt_size = hdr->dt_size;
2066#endif
Mayank Grover032736f2017-07-14 20:34:51 +05302067 dt_actual = ROUND_TO_PAGE(dt_size, page_mask);
Kishor PKd8ddcad2017-07-27 13:53:57 +05302068 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 +05302069 dprintf(CRITICAL, "Integer overflow detected in bootimage header fields\n");
2070 return -1;
2071 }
2072
Kishor PKd8ddcad2017-07-27 13:53:57 +05302073 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + second_actual + dt_actual);
Mayank Grover032736f2017-07-14 20:34:51 +05302074
Mayank Grovere559cec2017-10-17 15:12:03 +05302075 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_size) ||
2076 check_ddr_addr_range_bound(hdr->tags_addr, dt_size))
Mayank Grover032736f2017-07-14 20:34:51 +05302077 {
Mayank Grovere559cec2017-10-17 15:12:03 +05302078 dprintf(CRITICAL, "Device tree addresses are not valid.\n");
Mayank Grover032736f2017-07-14 20:34:51 +05302079 return -1;
2080 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002081#endif
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002082
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05302083 /* Read full boot.img from flash */
2084 dprintf(INFO, "Loading (%s) image (%d): start\n",
2085 (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual);
2086 bs_set_timestamp(BS_KERNEL_LOAD_START);
2087
2088 if (UINT_MAX - page_size < imagesize_actual)
2089 {
2090 dprintf(CRITICAL,"Integer overflow detected in bootimage header fields %u %s\n", __LINE__,__func__);
2091 return -1;
2092 }
2093
2094 /*Check the availability of RAM before reading boot image + max signature length from flash*/
2095 if (target_get_max_flash_size() < (imagesize_actual + page_size))
2096 {
2097 dprintf(CRITICAL, "bootimage size is greater than DDR can hold\n");
2098 return -1;
2099 }
2100
2101 offset = page_size;
2102 /* Read image without signature and header */
2103 if (flash_read(ptn, offset, (void *)(image_addr + offset), imagesize_actual - page_size))
2104 {
2105 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
2106 return -1;
2107 }
2108
2109 dprintf(INFO, "Loading (%s) image (%d): done\n",
2110 (!boot_into_recovery ? "boot" : "recovery"), imagesize_actual);
2111 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
2112
Shashank Mittalcd98d472011-08-02 14:29:24 -07002113 /* Authenticate Kernel */
Deepa Dinamani23b60d42013-06-24 18:10:52 -07002114 if(target_use_signed_kernel() && (!device.is_unlocked))
Shashank Mittalcd98d472011-08-02 14:29:24 -07002115 {
Shashank Mittalcd98d472011-08-02 14:29:24 -07002116 offset = imagesize_actual;
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05302117
Shashank Mittalcd98d472011-08-02 14:29:24 -07002118 /* Read signature */
2119 if (flash_read(ptn, offset, (void *)(image_addr + offset), page_size))
2120 {
2121 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002122 return -1;
Shashank Mittalcd98d472011-08-02 14:29:24 -07002123 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07002124
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05302125 verify_signed_bootimg((uint32_t)image_addr, imagesize_actual);
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05302126 }
2127 offset = page_size;
2128 if(hdr->second_size != 0) {
2129 if (UINT_MAX - offset < second_actual)
2130 {
2131 dprintf(CRITICAL, "ERROR: Integer overflow in boot image header %s\t%d\n",__func__,__LINE__);
2132 return -1;
vijay kumar8f53e362015-11-24 13:38:11 +05302133 }
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05302134 offset += second_actual;
2135 /* Second image loading not implemented. */
2136 ASSERT(0);
2137 }
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05302138 /* Move kernel and ramdisk to correct address */
2139 memmove((void*) hdr->kernel_addr, (char*) (image_addr + page_size), hdr->kernel_size);
2140 memmove((void*) hdr->ramdisk_addr, (char*) (image_addr + page_size + kernel_actual), hdr->ramdisk_size);
2141
2142#if DEVICE_TREE
2143 if(dt_size != 0) {
2144
2145 dt_table_offset = ((uint32_t)image_addr + page_size + kernel_actual + ramdisk_actual + second_actual);
2146
2147 table = (struct dt_table*) dt_table_offset;
2148
2149 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
2150 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
2151 return -1;
2152 }
2153
2154 /* Its Error if, dt_hdr_size (table->num_entries * dt_entry size + Dev_Tree Header)
2155 goes beyound hdr->dt_size*/
2156 if (dt_hdr_size > ROUND_TO_PAGE(dt_size,hdr->page_size)) {
2157 dprintf(CRITICAL, "ERROR: Invalid Device Tree size \n");
2158 return -1;
2159 }
2160
2161 /* Find index of device tree within device tree table */
2162 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
2163 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
2164 return -1;
2165 }
2166
2167 /* Validate and Read device device tree in the "tags_add */
2168 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size) ||
2169 check_ddr_addr_range_bound(hdr->tags_addr, dt_entry.size))
2170 {
2171 dprintf(CRITICAL, "Device tree addresses are not valid.\n");
2172 return -1;
2173 }
2174
2175 if(dt_entry.offset > (UINT_MAX - dt_entry.size)) {
2176 dprintf(CRITICAL, "ERROR: Device tree contents are Invalid\n");
2177 return -1;
2178 }
2179
2180 /* Ensure we are not overshooting dt_size with the dt_entry selected */
2181 if ((dt_entry.offset + dt_entry.size) > dt_size) {
2182 dprintf(CRITICAL, "ERROR: Device tree contents are Invalid\n");
2183 return -1;
2184 }
2185
2186 best_match_dt_addr = (unsigned char *)table + dt_entry.offset;
2187 dtb_size = dt_entry.size;
2188 memmove((void *)hdr->tags_addr, (char *)best_match_dt_addr, dtb_size);
Vivek Kumar07bd0862018-04-10 14:19:23 +05302189
2190 } else {
2191 /* Validate the tags_addr */
2192 if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual) ||
2193 check_ddr_addr_range_bound(hdr->tags_addr, kernel_actual))
2194 {
2195 dprintf(CRITICAL, "Device tree addresses are not valid.\n");
2196 return -1;
2197 }
2198 /*
2199 * If appended dev tree is found, update the atags with
2200 * memory address to the DTB appended location on RAM.
2201 * Else update with the atags address in the kernel header
2202 */
2203 void *dtb = NULL;
2204 dtb = dev_tree_appended((void*)(image_addr + page_size ),hdr->kernel_size, dtb_offset, (void *)hdr->tags_addr);
2205 if (!dtb) {
2206 dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
2207 return -1;
2208 }
2209 }
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05302210#endif
2211 if(target_use_signed_kernel() && (!device.is_unlocked))
2212 {
Shashank Mittal162244e2011-08-08 19:01:25 -07002213 /* Make sure everything from scratch address is read before next step!*/
Shashank Mittala0032282011-08-26 14:50:11 -07002214 if(device.is_tampered)
Shashank Mittal162244e2011-08-08 19:01:25 -07002215 {
2216 write_device_info_flash(&device);
2217 }
Channagoud Kadabi5c86fe32012-02-16 10:58:48 +05302218#if USE_PCOM_SECBOOT
2219 set_tamper_flag(device.is_tampered);
2220#endif
Shashank Mittalcd98d472011-08-02 14:29:24 -07002221 }
David Ng183a7422009-12-07 14:55:21 -08002222continue_boot:
Dima Zavin214cc642009-01-26 11:16:21 -08002223
Dima Zavin214cc642009-01-26 11:16:21 -08002224 /* TODO: create/pass atags to kernel */
2225
Ajay Dudanie28a6072011-07-01 13:59:46 -07002226 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07002227 (const char *)hdr->cmdline, board_machtype(),
Dima Zavin214cc642009-01-26 11:16:21 -08002228 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
2229
2230 return 0;
2231}
Brian Swetland9c4c0752009-01-25 16:23:50 -08002232
Shashank Mittal162244e2011-08-08 19:01:25 -07002233void write_device_info_mmc(device_info *dev)
2234{
Shashank Mittal162244e2011-08-08 19:01:25 -07002235 unsigned long long ptn = 0;
2236 unsigned long long size;
2237 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002238 uint8_t lun = 0;
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002239 uint32_t ret = 0;
Mayank Groverddd348d2018-01-23 14:07:09 +05302240 uint32_t device_info_sz = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07002241
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002242 if (devinfo_present)
2243 index = partition_get_index("devinfo");
2244 else
2245 index = partition_get_index("aboot");
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07002246
Shashank Mittal162244e2011-08-08 19:01:25 -07002247 ptn = partition_get_offset(index);
2248 if(ptn == 0)
2249 {
2250 return;
2251 }
2252
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002253 lun = partition_get_lun(index);
2254 mmc_set_lun(lun);
2255
Shashank Mittal162244e2011-08-08 19:01:25 -07002256 size = partition_get_size(index);
2257
Mayank Groverddd348d2018-01-23 14:07:09 +05302258 device_info_sz = ROUND_TO_PAGE(sizeof(struct device_info),
2259 mmc_blocksize_mask);
2260 if (device_info_sz == UINT_MAX)
2261 {
2262 dprintf(CRITICAL, "ERROR: Incorrect blocksize of card\n");
2263 return;
2264 }
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07002265
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002266 if (devinfo_present)
Mayank Groverddd348d2018-01-23 14:07:09 +05302267 ret = mmc_write(ptn, device_info_sz, (void *)info_buf);
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002268 else
Mayank Groverddd348d2018-01-23 14:07:09 +05302269 ret = mmc_write((ptn + size - device_info_sz), device_info_sz, (void *)info_buf);
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002270 if (ret)
Shashank Mittal162244e2011-08-08 19:01:25 -07002271 {
2272 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
Sridhar Parasurame1a97922015-07-27 15:31:22 -07002273 ASSERT(0);
Shashank Mittal162244e2011-08-08 19:01:25 -07002274 }
2275}
2276
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002277void read_device_info_mmc(struct device_info *info)
Shashank Mittal162244e2011-08-08 19:01:25 -07002278{
Shashank Mittal162244e2011-08-08 19:01:25 -07002279 unsigned long long ptn = 0;
2280 unsigned long long size;
2281 int index = INVALID_PTN;
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002282 uint32_t ret = 0;
Mayank Groverddd348d2018-01-23 14:07:09 +05302283 uint32_t device_info_sz = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07002284
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002285 if ((index = partition_get_index("devinfo")) < 0)
2286 {
2287 devinfo_present = false;
2288 index = partition_get_index("aboot");
2289 }
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07002290
Shashank Mittal162244e2011-08-08 19:01:25 -07002291 ptn = partition_get_offset(index);
2292 if(ptn == 0)
2293 {
2294 return;
2295 }
2296
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07002297 mmc_set_lun(partition_get_lun(index));
2298
Shashank Mittal162244e2011-08-08 19:01:25 -07002299 size = partition_get_size(index);
2300
Mayank Groverddd348d2018-01-23 14:07:09 +05302301 device_info_sz = ROUND_TO_PAGE(sizeof(struct device_info),
2302 mmc_blocksize_mask);
2303 if (device_info_sz == UINT_MAX)
2304 {
2305 dprintf(CRITICAL, "ERROR: Incorrect blocksize of card\n");
2306 return;
2307 }
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07002308
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002309 if (devinfo_present)
Mayank Groverddd348d2018-01-23 14:07:09 +05302310 ret = mmc_read(ptn, (void *)info_buf, device_info_sz);
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002311 else
Mayank Groverddd348d2018-01-23 14:07:09 +05302312 ret = mmc_read((ptn + size - device_info_sz), (void *)info_buf, device_info_sz);
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002313 if (ret)
Shashank Mittal162244e2011-08-08 19:01:25 -07002314 {
2315 dprintf(CRITICAL, "ERROR: Cannot read device info\n");
Sridhar Parasurame1a97922015-07-27 15:31:22 -07002316 ASSERT(0);
Shashank Mittal162244e2011-08-08 19:01:25 -07002317 }
Shashank Mittal162244e2011-08-08 19:01:25 -07002318}
2319
2320void write_device_info_flash(device_info *dev)
2321{
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002322 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
Shashank Mittal162244e2011-08-08 19:01:25 -07002323 struct ptentry *ptn;
2324 struct ptable *ptable;
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002325 if(info == NULL)
2326 {
2327 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
2328 ASSERT(0);
2329 }
2330 info_buf = info;
Shashank Mittal162244e2011-08-08 19:01:25 -07002331 ptable = flash_get_ptable();
2332 if (ptable == NULL)
2333 {
2334 dprintf(CRITICAL, "ERROR: Partition table not found\n");
2335 return;
2336 }
2337
2338 ptn = ptable_find(ptable, "devinfo");
2339 if (ptn == NULL)
2340 {
Smita Ghosh670c8b82015-05-07 09:30:03 -07002341 dprintf(CRITICAL, "ERROR: No devinfo partition found\n");
Shashank Mittal162244e2011-08-08 19:01:25 -07002342 return;
2343 }
2344
Mayank Groverdedbc892017-10-24 13:41:34 +05302345 memset(info, 0, BOOT_IMG_MAX_PAGE_SIZE);
Shashank Mittal162244e2011-08-08 19:01:25 -07002346 memcpy(info, dev, sizeof(device_info));
2347
2348 if (flash_write(ptn, 0, (void *)info_buf, page_size))
2349 {
2350 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
2351 return;
2352 }
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002353 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07002354}
2355
vijay kumarc65876c2015-04-24 13:29:16 +05302356static int read_allow_oem_unlock(device_info *dev)
2357{
vijay kumarc65876c2015-04-24 13:29:16 +05302358 unsigned offset;
2359 int index;
2360 unsigned long long ptn;
2361 unsigned long long ptn_size;
2362 unsigned blocksize = mmc_get_device_blocksize();
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07002363 STACKBUF_DMA_ALIGN(buf, blocksize);
vijay kumarc65876c2015-04-24 13:29:16 +05302364
vijay kumarca2e6812015-07-08 20:28:25 +05302365 index = partition_get_index(frp_ptns[0]);
vijay kumarc65876c2015-04-24 13:29:16 +05302366 if (index == INVALID_PTN)
2367 {
vijay kumarca2e6812015-07-08 20:28:25 +05302368 index = partition_get_index(frp_ptns[1]);
2369 if (index == INVALID_PTN)
2370 {
2371 dprintf(CRITICAL, "Neither '%s' nor '%s' partition found\n", frp_ptns[0],frp_ptns[1]);
2372 return -1;
2373 }
vijay kumarc65876c2015-04-24 13:29:16 +05302374 }
2375
2376 ptn = partition_get_offset(index);
2377 ptn_size = partition_get_size(index);
2378 offset = ptn_size - blocksize;
2379
Mayank Grover48860402016-11-29 12:34:53 +05302380 /* Set Lun for partition */
2381 mmc_set_lun(partition_get_lun(index));
2382
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07002383 if (mmc_read(ptn + offset, (void *)buf, blocksize))
vijay kumarc65876c2015-04-24 13:29:16 +05302384 {
2385 dprintf(CRITICAL, "Reading MMC failed\n");
2386 return -1;
2387 }
2388
2389 /*is_allow_unlock is a bool value stored at the LSB of last byte*/
2390 is_allow_unlock = buf[blocksize-1] & 0x01;
2391 return 0;
2392}
2393
2394static int write_allow_oem_unlock(bool allow_unlock)
2395{
vijay kumarc65876c2015-04-24 13:29:16 +05302396 unsigned offset;
vijay kumarc65876c2015-04-24 13:29:16 +05302397 int index;
2398 unsigned long long ptn;
2399 unsigned long long ptn_size;
2400 unsigned blocksize = mmc_get_device_blocksize();
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07002401 STACKBUF_DMA_ALIGN(buf, blocksize);
vijay kumarc65876c2015-04-24 13:29:16 +05302402
vijay kumarca2e6812015-07-08 20:28:25 +05302403 index = partition_get_index(frp_ptns[0]);
vijay kumarc65876c2015-04-24 13:29:16 +05302404 if (index == INVALID_PTN)
2405 {
vijay kumarca2e6812015-07-08 20:28:25 +05302406 index = partition_get_index(frp_ptns[1]);
2407 if (index == INVALID_PTN)
2408 {
2409 dprintf(CRITICAL, "Neither '%s' nor '%s' partition found\n", frp_ptns[0],frp_ptns[1]);
2410 return -1;
2411 }
vijay kumarc65876c2015-04-24 13:29:16 +05302412 }
2413
2414 ptn = partition_get_offset(index);
2415 ptn_size = partition_get_size(index);
2416 offset = ptn_size - blocksize;
Mayank Grover48860402016-11-29 12:34:53 +05302417 mmc_set_lun(partition_get_lun(index));
vijay kumarc65876c2015-04-24 13:29:16 +05302418
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07002419 if (mmc_read(ptn + offset, (void *)buf, blocksize))
vijay kumarc65876c2015-04-24 13:29:16 +05302420 {
2421 dprintf(CRITICAL, "Reading MMC failed\n");
2422 return -1;
2423 }
2424
2425 /*is_allow_unlock is a bool value stored at the LSB of last byte*/
2426 buf[blocksize-1] = allow_unlock;
2427 if (mmc_write(ptn + offset, blocksize, buf))
2428 {
2429 dprintf(CRITICAL, "Writing MMC failed\n");
2430 return -1;
2431 }
2432
2433 return 0;
2434}
2435
Shashank Mittal162244e2011-08-08 19:01:25 -07002436void read_device_info_flash(device_info *dev)
2437{
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002438 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
Shashank Mittal162244e2011-08-08 19:01:25 -07002439 struct ptentry *ptn;
2440 struct ptable *ptable;
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002441 if(info == NULL)
2442 {
2443 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
2444 ASSERT(0);
2445 }
2446 info_buf = info;
Shashank Mittal162244e2011-08-08 19:01:25 -07002447 ptable = flash_get_ptable();
2448 if (ptable == NULL)
2449 {
2450 dprintf(CRITICAL, "ERROR: Partition table not found\n");
2451 return;
2452 }
2453
2454 ptn = ptable_find(ptable, "devinfo");
2455 if (ptn == NULL)
2456 {
Smita Ghosh670c8b82015-05-07 09:30:03 -07002457 dprintf(CRITICAL, "ERROR: No devinfo partition found\n");
Shashank Mittal162244e2011-08-08 19:01:25 -07002458 return;
2459 }
2460
2461 if (flash_read(ptn, 0, (void *)info_buf, page_size))
2462 {
2463 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
2464 return;
2465 }
2466
2467 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
2468 {
Shashank Mittal162244e2011-08-08 19:01:25 -07002469 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
2470 info->is_unlocked = 0;
Shashank Mittala0032282011-08-26 14:50:11 -07002471 info->is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07002472 write_device_info_flash(info);
2473 }
2474 memcpy(dev, info, sizeof(device_info));
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002475 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07002476}
2477
2478void write_device_info(device_info *dev)
2479{
2480 if(target_is_emmc_boot())
2481 {
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002482 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
2483 if(info == NULL)
2484 {
2485 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
2486 ASSERT(0);
2487 }
2488 info_buf = info;
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002489 memcpy(info, dev, sizeof(struct device_info));
2490
2491#if USE_RPMB_FOR_DEVINFO
Mayank Grover912eaa62017-10-26 12:08:53 +05302492 if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05302493 is_secure_boot_enable()) {
2494 if((write_device_info_rpmb((void*) info, PAGE_SIZE)) < 0)
2495 ASSERT(0);
Sridhar Parasurame1a97922015-07-27 15:31:22 -07002496 }
Channagoud Kadabic80cb492015-04-28 16:08:28 -07002497 else
2498 write_device_info_mmc(info);
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002499#else
2500 write_device_info_mmc(info);
2501#endif
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002502 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07002503 }
2504 else
2505 {
2506 write_device_info_flash(dev);
2507 }
2508}
2509
Monika Singh94316462018-03-15 18:39:01 +05302510int read_rollback_index(uint32_t loc, uint64_t *roll_back_index)
2511{
2512 if (!devinfo_present) {
2513 dprintf(CRITICAL, "DeviceInfo not initalized \n");
2514 return -EINVAL;
2515 }
2516 if (loc >= ARRAY_SIZE(device.rollback_index)) {
2517 dprintf(CRITICAL, "%s() Loc out of range index: %d, array len: %d\n",
2518 __func__, loc, ARRAY_SIZE(device.rollback_index));
2519 ASSERT(0);
2520 }
2521
2522 *roll_back_index = device.rollback_index[loc];
2523 return 0;
2524}
2525
2526int write_rollback_index(uint32_t loc, uint64_t roll_back_index)
2527{
2528 if (!devinfo_present) {
2529 dprintf(CRITICAL, "DeviceInfo not initalized \n");
2530 return -EINVAL;
2531 }
2532 if (loc >= ARRAY_SIZE(device.rollback_index)) {
2533 dprintf(CRITICAL, "%s() Loc out of range index: %d, array len: %d\n",
2534 __func__, loc, ARRAY_SIZE(device.rollback_index));
2535 ASSERT(0);
2536 }
2537
2538 device.rollback_index[loc] = roll_back_index;
2539 write_device_info(&device);
2540 return 0;
2541}
2542
Monika Singhb0fad822018-03-15 18:50:55 +05302543int store_userkey(uint8_t *user_key, uint32_t user_key_size)
2544{
2545 if (!devinfo_present) {
2546 dprintf(CRITICAL, "DeviceInfo not initalized \n");
2547 return -EINVAL;
2548 }
2549
2550 if (user_key_size > ARRAY_SIZE(device.user_public_key)) {
2551 dprintf(CRITICAL, "StoreUserKey, UserKeySize too large!\n");
2552 return -ENODEV;
2553 }
2554
2555 memcpy(device.user_public_key, user_key, user_key_size);
2556 device.user_public_key_length = user_key_size;
2557 write_device_info(&device);
2558 return 0;
2559}
2560
2561int erase_userkey()
2562{
2563 if (!devinfo_present) {
2564 dprintf(CRITICAL, "DeviceInfo not initalized \n");
2565 return -EINVAL;
2566 }
2567 memset(device.user_public_key, 0, ARRAY_SIZE(device.user_public_key));
2568 device.user_public_key_length = 0;
2569 write_device_info(&device);
2570 return 0;
2571}
2572
2573int get_userkey(uint8_t **user_key, uint32_t *user_key_size)
2574{
2575 if (!devinfo_present) {
2576 dprintf(CRITICAL, "DeviceInfo not initalized \n");
2577 return -EINVAL;
2578 }
2579 *user_key = device.user_public_key;
2580 *user_key_size = device.user_public_key_length;
2581 return 0;
2582}
2583
Shashank Mittal162244e2011-08-08 19:01:25 -07002584void read_device_info(device_info *dev)
2585{
2586 if(target_is_emmc_boot())
2587 {
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002588 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
2589 if(info == NULL)
2590 {
2591 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
2592 ASSERT(0);
2593 }
2594 info_buf = info;
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002595
2596#if USE_RPMB_FOR_DEVINFO
Mayank Grover912eaa62017-10-26 12:08:53 +05302597 if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05302598 is_secure_boot_enable()) {
2599 if((read_device_info_rpmb((void*) info, PAGE_SIZE)) < 0)
2600 ASSERT(0);
Sridhar Parasurame1a97922015-07-27 15:31:22 -07002601 }
Channagoud Kadabic80cb492015-04-28 16:08:28 -07002602 else
2603 read_device_info_mmc(info);
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002604#else
2605 read_device_info_mmc(info);
2606#endif
2607
2608 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
2609 {
2610 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
lijuang511a2b52015-08-14 20:50:51 +08002611 if (is_secure_boot_enable()) {
Channagoud Kadabi05f78ba2015-07-06 11:58:14 -07002612 info->is_unlocked = 0;
Monika Singh292b3e92018-03-17 22:40:23 +05302613#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover912eaa62017-10-26 12:08:53 +05302614 if (VB_M <= target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +05302615 info->is_unlock_critical = 0;
Parth Dixitddbc7352015-10-18 03:13:31 +05302616#endif
lijuang511a2b52015-08-14 20:50:51 +08002617 } else {
Channagoud Kadabi2fda4092015-07-07 13:34:11 -07002618 info->is_unlocked = 1;
Monika Singh292b3e92018-03-17 22:40:23 +05302619#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover912eaa62017-10-26 12:08:53 +05302620 if (VB_M <= target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +05302621 info->is_unlock_critical = 1;
Parth Dixitddbc7352015-10-18 03:13:31 +05302622#endif
lijuang511a2b52015-08-14 20:50:51 +08002623 }
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002624 info->is_tampered = 0;
2625 info->charger_screen_enabled = 0;
Monika Singh292b3e92018-03-17 22:40:23 +05302626#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover912eaa62017-10-26 12:08:53 +05302627 if (VB_M <= target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +05302628 info->verity_mode = 1; //enforcing by default
Parth Dixitddbc7352015-10-18 03:13:31 +05302629#endif
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002630 write_device_info(info);
2631 }
2632 memcpy(dev, info, sizeof(device_info));
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002633 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07002634 }
2635 else
2636 {
2637 read_device_info_flash(dev);
2638 }
2639}
2640
2641void reset_device_info()
2642{
2643 dprintf(ALWAYS, "reset_device_info called.");
Shashank Mittala0032282011-08-26 14:50:11 -07002644 device.is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07002645 write_device_info(&device);
2646}
2647
2648void set_device_root()
2649{
2650 dprintf(ALWAYS, "set_device_root called.");
Shashank Mittala0032282011-08-26 14:50:11 -07002651 device.is_tampered = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -07002652 write_device_info(&device);
2653}
2654
lijuang4ece1e72015-08-14 21:02:36 +08002655/* set device unlock value
2656 * Must check FRP before call this function
2657 * Need to wipe data when unlock status changed
2658 * type 0: oem unlock
2659 * type 1: unlock critical
2660 * status 0: unlock as false
2661 * status 1: lock as true
2662 */
2663void set_device_unlock_value(int type, bool status)
lijuang21f12f52015-08-22 16:22:19 +08002664{
lijuang4ece1e72015-08-14 21:02:36 +08002665 if (type == UNLOCK)
2666 device.is_unlocked = status;
Monika Singh292b3e92018-03-17 22:40:23 +05302667#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover912eaa62017-10-26 12:08:53 +05302668 else if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05302669 type == UNLOCK_CRITICAL)
2670 device.is_unlock_critical = status;
Parth Dixitddbc7352015-10-18 03:13:31 +05302671#endif
lijuang4ece1e72015-08-14 21:02:36 +08002672 write_device_info(&device);
2673}
2674
2675static void set_device_unlock(int type, bool status)
2676{
2677 int is_unlocked = -1;
2678 char response[MAX_RSP_SIZE];
2679
2680 /* check device unlock status if it is as expected */
2681 if (type == UNLOCK)
2682 is_unlocked = device.is_unlocked;
Monika Singh292b3e92018-03-17 22:40:23 +05302683#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover912eaa62017-10-26 12:08:53 +05302684 if(VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05302685 type == UNLOCK_CRITICAL)
2686 {
2687 is_unlocked = device.is_unlock_critical;
2688 }
Parth Dixitddbc7352015-10-18 03:13:31 +05302689#endif
lijuang4ece1e72015-08-14 21:02:36 +08002690 if (is_unlocked == status) {
2691 snprintf(response, sizeof(response), "\tDevice already : %s", (status ? "unlocked!" : "locked!"));
2692 fastboot_info(response);
2693 fastboot_okay("");
2694 return;
2695 }
2696
2697 /* status is true, it means to unlock device */
lijuang07c5d6e2018-04-23 18:32:13 +08002698 if (status && !is_allow_unlock) {
2699 fastboot_fail("oem unlock is not allowed");
2700 return;
2701 }
lijuang21f12f52015-08-22 16:22:19 +08002702
lijuang4ece1e72015-08-14 21:02:36 +08002703#if FBCON_DISPLAY_MSG
lijuang07c5d6e2018-04-23 18:32:13 +08002704 display_unlock_menu(type, status);
2705 fastboot_okay("");
2706 return;
lijuang4ece1e72015-08-14 21:02:36 +08002707#else
lijuang07c5d6e2018-04-23 18:32:13 +08002708 if (status && type == UNLOCK) {
2709 fastboot_fail("Need wipe userdata. Do 'fastboot oem unlock-go'");
2710 return;
lijuang21f12f52015-08-22 16:22:19 +08002711 }
lijuang07c5d6e2018-04-23 18:32:13 +08002712#endif
lijuang4ece1e72015-08-14 21:02:36 +08002713
2714 set_device_unlock_value(type, status);
2715
2716 /* wipe data */
2717 struct recovery_message msg;
Kishor PK60a68212017-05-08 16:55:57 +05302718 memset(&msg, 0, sizeof(msg));
lijuang4ece1e72015-08-14 21:02:36 +08002719 snprintf(msg.recovery, sizeof(msg.recovery), "recovery\n--wipe_data");
2720 write_misc(0, &msg, sizeof(msg));
2721
2722 fastboot_okay("");
2723 reboot_device(RECOVERY_MODE);
lijuang21f12f52015-08-22 16:22:19 +08002724}
2725
lijuang511a2b52015-08-14 20:50:51 +08002726static bool critical_flash_allowed(const char * entry)
2727{
2728 uint32_t i = 0;
2729 if (entry == NULL)
2730 return false;
2731
2732 for (i = 0; i < ARRAY_SIZE(critical_flash_allowed_ptn); i++) {
2733 if(!strcmp(entry, critical_flash_allowed_ptn[i]))
2734 return true;
2735 }
2736 return false;
Matthew Qin271927e2015-03-31 22:07:07 -04002737}
2738
2739#if DEVICE_TREE
Amol Jadicb524072012-08-09 16:40:18 -07002740int copy_dtb(uint8_t *boot_image_start, unsigned int scratch_offset)
2741{
2742 uint32 dt_image_offset = 0;
Amol Jadicb524072012-08-09 16:40:18 -07002743 uint32_t n;
Monika Singh292b3e92018-03-17 22:40:23 +05302744 struct dt_table *table = NULL;
Amol Jadicb524072012-08-09 16:40:18 -07002745 struct dt_entry dt_entry;
Monika Singh292b3e92018-03-17 22:40:23 +05302746 uint32_t dt_hdr_size = 0;
Amol Jadicb524072012-08-09 16:40:18 -07002747 unsigned int compressed_size = 0;
2748 unsigned int dtb_size = 0;
2749 unsigned int out_avai_len = 0;
2750 unsigned char *out_addr = NULL;
2751 unsigned char *best_match_dt_addr = NULL;
2752 int rc;
2753
2754 struct boot_img_hdr *hdr = (struct boot_img_hdr *) (boot_image_start);
2755
Parth Dixit4097b622016-03-15 14:42:27 +05302756#ifndef OSVERSION_IN_BOOTIMAGE
2757 dt_size = hdr->dt_size;
2758#endif
2759
2760 if(dt_size != 0) {
Amol Jadicb524072012-08-09 16:40:18 -07002761 /* add kernel offset */
2762 dt_image_offset += page_size;
2763 n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
2764 dt_image_offset += n;
2765
2766 /* add ramdisk offset */
2767 n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
2768 dt_image_offset += n;
2769
2770 /* add second offset */
2771 if(hdr->second_size != 0) {
2772 n = ROUND_TO_PAGE(hdr->second_size, page_mask);
2773 dt_image_offset += n;
2774 }
2775
2776 /* offset now point to start of dt.img */
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07002777 table = (struct dt_table*)(boot_image_start + dt_image_offset);
Amol Jadicb524072012-08-09 16:40:18 -07002778
Deepa Dinamani19648b42013-09-05 17:05:55 -07002779 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Amol Jadicb524072012-08-09 16:40:18 -07002780 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
2781 return -1;
2782 }
P.V. Phani Kumar82916762016-03-09 09:20:19 +05302783
2784 /* Its Error if, dt_hdr_size (table->num_entries * dt_entry size + Dev_Tree Header)
2785 goes beyound hdr->dt_size*/
Parth Dixit4097b622016-03-15 14:42:27 +05302786 if (dt_hdr_size > ROUND_TO_PAGE(dt_size,hdr->page_size)) {
P.V. Phani Kumar82916762016-03-09 09:20:19 +05302787 dprintf(CRITICAL, "ERROR: Invalid Device Tree size \n");
2788 return -1;
2789 }
2790
Joel Kingaa335dc2013-06-03 16:11:08 -07002791 /* Find index of device tree within device tree table */
2792 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Amol Jadicb524072012-08-09 16:40:18 -07002793 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
2794 return -1;
2795 }
2796
Matthew Qin271927e2015-03-31 22:07:07 -04002797 best_match_dt_addr = (unsigned char *)boot_image_start + dt_image_offset + dt_entry.offset;
2798 if (is_gzip_package(best_match_dt_addr, dt_entry.size))
2799 {
2800 out_addr = (unsigned char *)target_get_scratch_address() + scratch_offset;
2801 out_avai_len = target_get_max_flash_size() - scratch_offset;
Matthew Qin0b15b322015-05-19 05:20:54 -04002802 dprintf(INFO, "decompressing dtb: start\n");
Matthew Qin271927e2015-03-31 22:07:07 -04002803 rc = decompress(best_match_dt_addr,
2804 dt_entry.size, out_addr, out_avai_len,
2805 &compressed_size, &dtb_size);
2806 if (rc)
2807 {
Matthew Qin0b15b322015-05-19 05:20:54 -04002808 dprintf(CRITICAL, "decompressing dtb failed!!!\n");
Matthew Qin271927e2015-03-31 22:07:07 -04002809 ASSERT(0);
2810 }
2811
Matthew Qin0b15b322015-05-19 05:20:54 -04002812 dprintf(INFO, "decompressing dtb: done\n");
Matthew Qin271927e2015-03-31 22:07:07 -04002813 best_match_dt_addr = out_addr;
2814 } else {
2815 dtb_size = dt_entry.size;
2816 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002817 /* Validate and Read device device tree in the "tags_add */
Mayank Grovere559cec2017-10-17 15:12:03 +05302818 if (check_aboot_addr_range_overlap(hdr->tags_addr, dtb_size) ||
2819 check_ddr_addr_range_bound(hdr->tags_addr, dtb_size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002820 {
Mayank Grovere559cec2017-10-17 15:12:03 +05302821 dprintf(CRITICAL, "Device tree addresses are not valid.\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002822 return -1;
2823 }
2824
Amol Jadicb524072012-08-09 16:40:18 -07002825 /* Read device device tree in the "tags_add */
Matthew Qin271927e2015-03-31 22:07:07 -04002826 memmove((void*) hdr->tags_addr, (void *)best_match_dt_addr, dtb_size);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002827 } else
2828 return -1;
Amol Jadicb524072012-08-09 16:40:18 -07002829
2830 /* Everything looks fine. Return success. */
2831 return 0;
2832}
2833#endif
2834
Brian Swetland9c4c0752009-01-25 16:23:50 -08002835void cmd_boot(const char *arg, void *data, unsigned sz)
2836{
2837 unsigned kernel_actual;
2838 unsigned ramdisk_actual;
Kishor PKd8ddcad2017-07-27 13:53:57 +05302839 unsigned second_actual;
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002840 uint32_t image_actual;
2841 uint32_t dt_actual = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08002842 struct boot_img_hdr *hdr = NULL;
2843 struct kernel64_hdr *kptr = NULL;
Brian Swetland9c4c0752009-01-25 16:23:50 -08002844 char *ptr = ((char*) data);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002845 int ret = 0;
2846 uint8_t dtb_copied = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08002847 unsigned int out_len = 0;
2848 unsigned int out_avai_len = 0;
2849 unsigned char *out_addr = NULL;
2850 uint32_t dtb_offset = 0;
2851 unsigned char *kernel_start_addr = NULL;
2852 unsigned int kernel_size = 0;
Matthew Qin271927e2015-03-31 22:07:07 -04002853 unsigned int scratch_offset = 0;
Monika Singh292b3e92018-03-17 22:40:23 +05302854#if !VERIFIED_BOOT_2
2855 uint32_t sig_actual = 0;
2856 uint32_t sig_size = 0;
2857#ifdef MDTP_SUPPORT
2858 static bool is_mdtp_activated = 0;
2859#endif /* MDTP_SUPPORT */
2860#endif
Matthew Qinbb7923d2015-02-09 10:56:09 +08002861
lijuang2008ff22016-03-07 17:56:27 +08002862#if FBCON_DISPLAY_MSG
2863 /* Exit keys' detection thread firstly */
2864 exit_menu_keys_detection();
2865#endif
2866
Monika Singh292b3e92018-03-17 22:40:23 +05302867#if VERIFIED_BOOT || VERIFIED_BOOT_2
Channagoud Kadabi6d5375e2015-06-23 17:15:42 -07002868 if(target_build_variant_user() && !device.is_unlocked)
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002869 {
2870 fastboot_fail("unlock device to use this command");
lijuang2008ff22016-03-07 17:56:27 +08002871 goto boot_failed;
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002872 }
2873#endif
2874
Brian Swetland9c4c0752009-01-25 16:23:50 -08002875 if (sz < sizeof(hdr)) {
2876 fastboot_fail("invalid bootimage header");
lijuang2008ff22016-03-07 17:56:27 +08002877 goto boot_failed;
Brian Swetland9c4c0752009-01-25 16:23:50 -08002878 }
2879
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002880 hdr = (struct boot_img_hdr *)data;
Brian Swetland9c4c0752009-01-25 16:23:50 -08002881
2882 /* ensure commandline is terminated */
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002883 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -08002884
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002885 if(target_is_emmc_boot() && hdr->page_size) {
2886 page_size = hdr->page_size;
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07002887 page_mask = page_size - 1;
2888 }
2889
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002890 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
2891 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Kishor PKd8ddcad2017-07-27 13:53:57 +05302892 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002893#if DEVICE_TREE
Parth Dixit4097b622016-03-15 14:42:27 +05302894#ifndef OSVERSION_IN_BOOTIMAGE
2895 dt_size = hdr->dt_size;
2896#endif
2897 dt_actual = ROUND_TO_PAGE(dt_size, page_mask);
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002898#endif
2899
2900 image_actual = ADD_OF(page_size, kernel_actual);
2901 image_actual = ADD_OF(image_actual, ramdisk_actual);
Kishor PKd8ddcad2017-07-27 13:53:57 +05302902 image_actual = ADD_OF(image_actual, second_actual);
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002903 image_actual = ADD_OF(image_actual, dt_actual);
2904
Kishor PK5134b332017-05-09 17:50:08 +05302905 /* Checking to prevent oob access in read_der_message_length */
2906 if (image_actual > sz) {
2907 fastboot_fail("bootimage header fields are invalid");
2908 goto boot_failed;
2909 }
Monika Singh292b3e92018-03-17 22:40:23 +05302910#if VERIFIED_BOOT_2
2911 memset(&info, 0, sizeof(bootinfo));
2912 info.images[0].image_buffer = data;
2913 info.images[0].imgsize = image_actual;
2914 info.images[0].name = "boot";
2915 info.num_loaded_images = 1;
2916 info.multi_slot_boot = partition_multislot_is_supported();
2917 if (load_image_and_auth(&info))
2918 goto boot_failed;
2919 vbcmdline = info.vbcmdline;
2920#else
Kishor PK5134b332017-05-09 17:50:08 +05302921 sig_size = sz - image_actual;
2922
P.V. Phani Kumareecfd822016-03-09 20:09:03 +05302923 if (target_use_signed_kernel() && (!device.is_unlocked)) {
Monika Singh292b3e92018-03-17 22:40:23 +05302924 unsigned chk;
P.V. Phani Kumareecfd822016-03-09 20:09:03 +05302925 /* Calculate the signature length from boot image */
2926 sig_actual = read_der_message_length(
Kishor PK5134b332017-05-09 17:50:08 +05302927 (unsigned char*)(data + image_actual), sig_size);
Monika Singh292b3e92018-03-17 22:40:23 +05302928 chk = ADD_OF(image_actual, sig_actual);
Channagoud Kadabi3d839012014-06-26 14:26:39 -07002929
Monika Singh292b3e92018-03-17 22:40:23 +05302930 if (chk > sz) {
Kishor PK5134b332017-05-09 17:50:08 +05302931 fastboot_fail("bootimage header fields are invalid");
2932 goto boot_failed;
2933 }
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002934 }
2935
Sridhar Parasuram361e4cd2015-09-24 11:19:19 -07002936 // Initialize boot state before trying to verify boot.img
2937#if VERIFIED_BOOT
Channagoud Kadabi699466e2015-11-03 12:37:42 -08002938 boot_verifier_init();
Mayank Groverb337e932017-01-18 20:00:40 +05302939#endif
Channagoud Kadabibf6ce7d2015-09-17 13:25:35 -07002940 /* Handle overflow if the input image size is greater than
2941 * boot image buffer can hold
2942 */
Monika Singh292b3e92018-03-17 22:40:23 +05302943 if ((target_get_max_flash_size() - page_size) < image_actual)
Channagoud Kadabibf6ce7d2015-09-17 13:25:35 -07002944 {
2945 fastboot_fail("booimage: size is greater than boot image buffer can hold");
lijuang2008ff22016-03-07 17:56:27 +08002946 goto boot_failed;
Channagoud Kadabibf6ce7d2015-09-17 13:25:35 -07002947 }
Sridhar Parasuram361e4cd2015-09-24 11:19:19 -07002948
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002949 /* Verify the boot image
2950 * device & page_size are initialized in aboot_init
2951 */
Sridhar Parasuram7ae9fbc2015-09-17 09:56:30 -07002952 if (target_use_signed_kernel() && (!device.is_unlocked)) {
Channagoud Kadabi3d839012014-06-26 14:26:39 -07002953 /* Pass size excluding signature size, otherwise we would try to
2954 * access signature beyond its length
2955 */
Monika Singh292b3e92018-03-17 22:40:23 +05302956 verify_signed_bootimg((uint32_t)data, image_actual);
Sridhar Parasuram7ae9fbc2015-09-17 09:56:30 -07002957 }
Amit Blay4aa292f2015-04-28 21:55:59 +03002958#ifdef MDTP_SUPPORT
2959 else
2960 {
Shay Nachmani062aa3f2015-05-17 17:28:44 +03002961 /* fastboot boot is not allowed when MDTP is activated */
Amit Blay4aa292f2015-04-28 21:55:59 +03002962 mdtp_ext_partition_verification_t ext_partition;
Amit Blay8a510302015-08-17 09:20:01 +03002963
2964 if (!is_mdtp_activated) {
2965 ext_partition.partition = MDTP_PARTITION_NONE;
2966 mdtp_fwlock_verify_lock(&ext_partition);
2967 }
Amit Blay4aa292f2015-04-28 21:55:59 +03002968 }
Shay Nachmani062aa3f2015-05-17 17:28:44 +03002969
Amir Kotzer7c768c02016-04-13 09:08:05 +03002970 /* If mdtp state cannot be validate, block fastboot boot*/
2971 if(mdtp_activated(&is_mdtp_activated)){
2972 dprintf(CRITICAL, "mdtp_activated cannot validate state.\n");
2973 dprintf(CRITICAL, "Can not proceed with fastboot boot command.\n");
2974 goto boot_failed;
2975 }
Shay Nachmani062aa3f2015-05-17 17:28:44 +03002976 if(is_mdtp_activated){
2977 dprintf(CRITICAL, "fastboot boot command is not available.\n");
lijuang2008ff22016-03-07 17:56:27 +08002978 goto boot_failed;
Shay Nachmani062aa3f2015-05-17 17:28:44 +03002979 }
Amit Blay4aa292f2015-04-28 21:55:59 +03002980#endif /* MDTP_SUPPORT */
Monika Singh292b3e92018-03-17 22:40:23 +05302981#endif /* VERIFIED_BOOT_2 else */
Amit Blay4aa292f2015-04-28 21:55:59 +03002982
Sridhar Parasuram361e4cd2015-09-24 11:19:19 -07002983#if VERIFIED_BOOT
Monika Singh292b3e92018-03-17 22:40:23 +05302984 if (VB_M == target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +05302985 {
2986 /* set boot and system versions. */
2987 set_os_version((unsigned char *)data);
2988 // send root of trust
2989 if(!send_rot_command((uint32_t)device.is_unlocked))
2990 ASSERT(0);
2991 }
Parth Dixitbc9b6492015-10-18 00:41:38 +05302992#endif
Channagoud Kadabia22144f2013-03-20 11:49:01 -07002993 /*
Matthew Qinbb7923d2015-02-09 10:56:09 +08002994 * Check if the kernel image is a gzip package. If yes, need to decompress it.
2995 * If not, continue booting.
2996 */
2997 if (is_gzip_package((unsigned char *)(data + page_size), hdr->kernel_size))
2998 {
2999 out_addr = (unsigned char *)target_get_scratch_address();
3000 out_addr = (unsigned char *)(out_addr + image_actual + page_size);
3001 out_avai_len = target_get_max_flash_size() - image_actual - page_size;
Matthew Qin0b15b322015-05-19 05:20:54 -04003002 dprintf(INFO, "decompressing kernel image: start\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08003003 ret = decompress((unsigned char *)(ptr + page_size),
3004 hdr->kernel_size, out_addr, out_avai_len,
3005 &dtb_offset, &out_len);
3006 if (ret)
3007 {
Matthew Qin0b15b322015-05-19 05:20:54 -04003008 dprintf(CRITICAL, "decompressing image failed!!!\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08003009 ASSERT(0);
3010 }
3011
Matthew Qin0b15b322015-05-19 05:20:54 -04003012 dprintf(INFO, "decompressing kernel image: done\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08003013 kptr = (struct kernel64_hdr *)out_addr;
3014 kernel_start_addr = out_addr;
3015 kernel_size = out_len;
3016 } else {
3017 kptr = (struct kernel64_hdr*)((char *)data + page_size);
3018 kernel_start_addr = (unsigned char *)((char *)data + page_size);
3019 kernel_size = hdr->kernel_size;
3020 }
3021
3022 /*
Channagoud Kadabia22144f2013-03-20 11:49:01 -07003023 * Update the kernel/ramdisk/tags address if the boot image header
3024 * has default values, these default values come from mkbootimg when
3025 * the boot image is flashed using fastboot flash:raw
3026 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08003027 update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr));
Dima Zavin3cadfff2013-03-21 14:30:48 -07003028
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07003029 /* Get virtual addresses since the hdr saves physical addresses. */
3030 hdr->kernel_addr = VA(hdr->kernel_addr);
3031 hdr->ramdisk_addr = VA(hdr->ramdisk_addr);
3032 hdr->tags_addr = VA(hdr->tags_addr);
Brian Swetland9c4c0752009-01-25 16:23:50 -08003033
Matthew Qinbb7923d2015-02-09 10:56:09 +08003034 kernel_size = ROUND_TO_PAGE(kernel_size, page_mask);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07003035 /* Check if the addresses in the header are valid. */
Matthew Qinbb7923d2015-02-09 10:56:09 +08003036 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_size) ||
Mayank Grovere559cec2017-10-17 15:12:03 +05303037 check_ddr_addr_range_bound(hdr->kernel_addr, kernel_size) ||
3038 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual) ||
3039 check_ddr_addr_range_bound(hdr->ramdisk_addr, ramdisk_actual))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07003040 {
Mayank Grovere559cec2017-10-17 15:12:03 +05303041 dprintf(CRITICAL, "kernel/ramdisk addresses are not valid.\n");
lijuang2008ff22016-03-07 17:56:27 +08003042 goto boot_failed;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07003043 }
3044
Amol Jadicb524072012-08-09 16:40:18 -07003045#if DEVICE_TREE
Matthew Qin271927e2015-03-31 22:07:07 -04003046 scratch_offset = image_actual + page_size + out_len;
Amol Jadicb524072012-08-09 16:40:18 -07003047 /* find correct dtb and copy it to right location */
Matthew Qin271927e2015-03-31 22:07:07 -04003048 ret = copy_dtb(data, scratch_offset);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07003049
3050 dtb_copied = !ret ? 1 : 0;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07003051#else
Mayank Grovere559cec2017-10-17 15:12:03 +05303052 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE) ||
3053 check_ddr_addr_range_bound(hdr->tags_addr, MAX_TAGS_SIZE))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07003054 {
Mayank Grovere559cec2017-10-17 15:12:03 +05303055 dprintf(CRITICAL, "Tags addresses are not valid.\n");
lijuang2008ff22016-03-07 17:56:27 +08003056 goto boot_failed;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07003057 }
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07003058#endif
3059
3060 /* Load ramdisk & kernel */
3061 memmove((void*) hdr->ramdisk_addr, ptr + page_size + kernel_actual, hdr->ramdisk_size);
Matthew Qinbb7923d2015-02-09 10:56:09 +08003062 memmove((void*) hdr->kernel_addr, (char*) (kernel_start_addr), kernel_size);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07003063
3064#if DEVICE_TREE
Mayank Grovere559cec2017-10-17 15:12:03 +05303065 if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual) ||
3066 check_ddr_addr_range_bound(hdr->tags_addr, kernel_actual))
Matthew Qinbb7923d2015-02-09 10:56:09 +08003067 {
Mayank Grovere559cec2017-10-17 15:12:03 +05303068 dprintf(CRITICAL, "Tags addresses are not valid.\n");
lijuang2008ff22016-03-07 17:56:27 +08003069 goto boot_failed;
Matthew Qinbb7923d2015-02-09 10:56:09 +08003070 }
3071
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07003072 /*
3073 * If dtb is not found look for appended DTB in the kernel.
3074 * If appended dev tree is found, update the atags with
3075 * memory address to the DTB appended location on RAM.
3076 * Else update with the atags address in the kernel header
3077 */
3078 if (!dtb_copied) {
3079 void *dtb;
Matthew Qinbb7923d2015-02-09 10:56:09 +08003080 dtb = dev_tree_appended((void*)(ptr + page_size),
3081 hdr->kernel_size, dtb_offset,
Dima Zavine63e5572013-05-03 12:23:06 -07003082 (void *)hdr->tags_addr);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07003083 if (!dtb) {
3084 fastboot_fail("dtb not found");
lijuang2008ff22016-03-07 17:56:27 +08003085 goto boot_failed;
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07003086 }
Amol Jadicb524072012-08-09 16:40:18 -07003087 }
3088#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08003089
3090 fastboot_okay("");
Amol Jadi7c4316c2013-10-07 14:19:26 -07003091 fastboot_stop();
Brian Swetland9c4c0752009-01-25 16:23:50 -08003092
Dima Zavin77e41f32013-03-06 16:10:43 -08003093 boot_linux((void*) hdr->kernel_addr, (void*) hdr->tags_addr,
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07003094 (const char*) hdr->cmdline, board_machtype(),
3095 (void*) hdr->ramdisk_addr, hdr->ramdisk_size);
lijuang2008ff22016-03-07 17:56:27 +08003096
3097 /* fastboot already stop, it's no need to show fastboot menu */
3098 return;
3099boot_failed:
3100#if FBCON_DISPLAY_MSG
3101 /* revert to fastboot menu if boot failed */
3102 display_fastboot_menu();
3103#endif
3104 return;
Brian Swetland9c4c0752009-01-25 16:23:50 -08003105}
3106
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003107void cmd_erase_nand(const char *arg, void *data, unsigned sz)
Dima Zavin214cc642009-01-26 11:16:21 -08003108{
3109 struct ptentry *ptn;
3110 struct ptable *ptable;
3111
3112 ptable = flash_get_ptable();
3113 if (ptable == NULL) {
3114 fastboot_fail("partition table doesn't exist");
3115 return;
3116 }
3117
3118 ptn = ptable_find(ptable, arg);
3119 if (ptn == NULL) {
3120 fastboot_fail("unknown partition name");
3121 return;
3122 }
3123
Monika Singh292b3e92018-03-17 22:40:23 +05303124 if (!strncmp(arg, "avb_custom_key", strlen("avb_custom_key"))) {
3125 dprintf(INFO, "erasing avb_custom_key\n");
3126 if (erase_userkey()) {
3127 fastboot_fail("Erasing avb_custom_key failed");
3128 } else {
3129 fastboot_okay("");
3130 }
3131 return;
3132 }
3133
Dima Zavin214cc642009-01-26 11:16:21 -08003134 if (flash_erase(ptn)) {
3135 fastboot_fail("failed to erase partition");
3136 return;
3137 }
3138 fastboot_okay("");
3139}
3140
Bikas Gurungd48bd242010-09-04 19:54:32 -07003141
3142void cmd_erase_mmc(const char *arg, void *data, unsigned sz)
3143{
3144 unsigned long long ptn = 0;
Oliver Wangcee448d2013-10-22 18:40:13 +08003145 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07003146 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003147 uint8_t lun = 0;
Vijay Kumar Pendoti0b21f462016-05-02 17:09:18 +05303148 char *footer = NULL;
Bikas Gurungd48bd242010-09-04 19:54:32 -07003149
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003150#if VERIFIED_BOOT
3151 if(!strcmp(arg, KEYSTORE_PTN_NAME))
3152 {
3153 if(!device.is_unlocked)
3154 {
3155 fastboot_fail("unlock device to erase keystore");
3156 return;
3157 }
3158 }
3159#endif
3160
Kinson Chikf1a43512011-07-14 11:28:39 -07003161 index = partition_get_index(arg);
3162 ptn = partition_get_offset(index);
Oliver Wangcee448d2013-10-22 18:40:13 +08003163 size = partition_get_size(index);
Neeti Desaica8c9602011-10-06 11:40:00 -07003164
Monika Singhc4778b72018-05-16 11:16:42 +05303165 if (!strncmp(arg, "avb_custom_key", strlen("avb_custom_key"))) {
3166 dprintf(INFO, "erasing avb_custom_key\n");
3167 if (erase_userkey()) {
3168 fastboot_fail("Erasing avb_custom_key failed");
3169 } else {
3170 fastboot_okay("");
3171 }
3172 return;
3173 }
3174
Kinson Chikf1a43512011-07-14 11:28:39 -07003175 if(ptn == 0) {
Neeti Desaica8c9602011-10-06 11:40:00 -07003176 fastboot_fail("Partition table doesn't exist\n");
Bikas Gurungd48bd242010-09-04 19:54:32 -07003177 return;
3178 }
Kun Liang2f1601a2013-08-12 16:29:54 +08003179
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003180 lun = partition_get_lun(index);
3181 mmc_set_lun(lun);
3182
Sridhar Parasuramd7957122015-02-27 11:33:40 -08003183 if (platform_boot_dev_isemmc())
3184 {
3185 if (mmc_erase_card(ptn, size)) {
3186 fastboot_fail("failed to erase partition\n");
3187 return;
3188 }
3189 } else {
3190 BUF_DMA_ALIGN(out, DEFAULT_ERASE_SIZE);
3191 size = partition_get_size(index);
3192 if (size > DEFAULT_ERASE_SIZE)
3193 size = DEFAULT_ERASE_SIZE;
Kun Liang2f1601a2013-08-12 16:29:54 +08003194
Sridhar Parasuramd7957122015-02-27 11:33:40 -08003195 /* Simple inefficient version of erase. Just writing
3196 0 in first several blocks */
3197 if (mmc_write(ptn , size, (unsigned int *)out)) {
3198 fastboot_fail("failed to erase partition");
3199 return;
3200 }
Vijay Kumar Pendoti0b21f462016-05-02 17:09:18 +05303201 /*Erase FDE metadata at the userdata footer*/
3202 if(!(strncmp(arg, "userdata", 8)))
3203 {
3204 footer = memalign(CACHE_LINE, FOOTER_SIZE);
3205 memset((void *)footer, 0, FOOTER_SIZE);
3206
3207 size = partition_get_size(index);
3208
3209 if (mmc_write((ptn + size) - FOOTER_SIZE , FOOTER_SIZE, (unsigned int *)footer)) {
3210 fastboot_fail("failed to erase userdata footer");
3211 free(footer);
3212 return;
3213 }
3214 free(footer);
3215 }
Bikas Gurungd48bd242010-09-04 19:54:32 -07003216 }
Monika Singh292b3e92018-03-17 22:40:23 +05303217#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover912eaa62017-10-26 12:08:53 +05303218 if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05303219 !(strncmp(arg, "userdata", 8)) &&
3220 send_delete_keys_to_tz())
Sridhar Parasuram32b30662015-07-10 13:33:22 -07003221 ASSERT(0);
3222#endif
Bikas Gurungd48bd242010-09-04 19:54:32 -07003223 fastboot_okay("");
3224}
3225
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003226void cmd_erase(const char *arg, void *data, unsigned sz)
3227{
Monika Singh292b3e92018-03-17 22:40:23 +05303228#if VERIFIED_BOOT || VERIFIED_BOOT_2
Channagoud Kadabi35297672015-06-13 11:09:49 -07003229 if (target_build_variant_user())
Channagoud Kadabice3cf422015-04-17 16:02:30 -07003230 {
Sridhar Parasuramc32d07d2015-07-12 10:57:48 -07003231 if(!device.is_unlocked)
Channagoud Kadabice3cf422015-04-17 16:02:30 -07003232 {
Channagoud Kadabi35297672015-06-13 11:09:49 -07003233 fastboot_fail("device is locked. Cannot erase");
Channagoud Kadabice3cf422015-04-17 16:02:30 -07003234 return;
3235 }
3236 }
3237#endif
3238
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003239 if(target_is_emmc_boot())
3240 cmd_erase_mmc(arg, data, sz);
3241 else
3242 cmd_erase_nand(arg, data, sz);
3243}
Bikas Gurungd48bd242010-09-04 19:54:32 -07003244
Mayank Grover11ff9692018-01-11 11:54:49 +05303245/* Get the size from partiton name */
3246static void get_partition_size(const char *arg, char *response)
3247{
3248 uint64_t ptn = 0;
3249 uint64_t size;
3250 int index = INVALID_PTN;
3251
3252 index = partition_get_index(arg);
3253
3254 if (index == INVALID_PTN)
3255 {
3256 dprintf(CRITICAL, "Invalid partition index\n");
3257 return;
3258 }
3259
3260 ptn = partition_get_offset(index);
3261
3262 if(!ptn)
3263 {
3264 dprintf(CRITICAL, "Invalid partition name %s\n", arg);
3265 return;
3266 }
3267
3268 size = partition_get_size(index);
3269
3270 snprintf(response, MAX_RSP_SIZE, "\t 0x%llx", size);
3271 return;
3272}
3273
Mayank Grover52cd10a2018-03-15 12:57:54 +05303274/* Function to check partition type of a partition*/
3275static fs_signature_type
3276check_partition_fs_signature(const char *arg)
3277{
3278 fs_signature_type ret = NO_FS;
3279 int index;
3280 unsigned long long ptn;
Mayank Grover399826a2018-08-27 12:15:15 +05303281 char *buffer = memalign(CACHE_LINE, mmc_blocksize);
3282 uint32_t sb_blk_offset = 0;
3283 char *sb_buffer = buffer;
3284
Mayank Grover52cd10a2018-03-15 12:57:54 +05303285 if (!sb_buffer)
3286 {
3287 dprintf(CRITICAL, "ERROR: Failed to allocate buffer for superblock\n");
3288 goto out;
3289 }
3290
3291 /* Read super block */
3292 if ((index = partition_get_index(arg)) < 0)
3293 {
3294 dprintf(CRITICAL, "ERROR: %s() doesn't exsit\n", arg);
3295 goto out;
3296 }
3297 ptn = partition_get_offset(index);
3298 mmc_set_lun(partition_get_lun(index));
Mayank Grover399826a2018-08-27 12:15:15 +05303299 sb_blk_offset = (FS_SUPERBLOCK_OFFSET/mmc_blocksize);
3300
3301 if(mmc_read(ptn + (sb_blk_offset * mmc_blocksize),
Mayank Grover52cd10a2018-03-15 12:57:54 +05303302 (void *)sb_buffer, mmc_blocksize))
3303 {
3304 dprintf(CRITICAL, "ERROR: Failed to read Superblock\n");
3305 goto out;
3306 }
3307
Mayank Grover399826a2018-08-27 12:15:15 +05303308 if (sb_blk_offset == 0)
3309 sb_buffer += FS_SUPERBLOCK_OFFSET;
3310
3311 if (*((uint16 *)(&sb_buffer[EXT_MAGIC_OFFSET_SB])) == (uint16)EXT_MAGIC)
Mayank Grover52cd10a2018-03-15 12:57:54 +05303312 {
3313 dprintf(SPEW, "%s() Found EXT FS\n", arg);
3314 ret = EXT_FS_SIGNATURE;
3315 }
Mayank Grover399826a2018-08-27 12:15:15 +05303316 else if (*((uint32 *)(&sb_buffer[F2FS_MAGIC_OFFSET_SB])) == F2FS_MAGIC)
Mayank Grover52cd10a2018-03-15 12:57:54 +05303317 {
3318 dprintf(SPEW, "%s() Found F2FS FS\n", arg);
3319 ret = EXT_F2FS_SIGNATURE;
3320 }
3321 else
3322 {
3323 dprintf(SPEW, "%s() Reverting to default 0x%x\n",
3324 arg, *((uint16 *)(&sb_buffer[EXT_MAGIC_OFFSET_SB])));
3325 ret = NO_FS;
3326 }
3327
3328out:
Mayank Grover399826a2018-08-27 12:15:15 +05303329 if(buffer)
3330 free(buffer);
Mayank Grover52cd10a2018-03-15 12:57:54 +05303331 return ret;
3332}
3333
Mayank Groverd38fe012018-03-13 15:33:16 +05303334/* Function to get partition type */
3335static void get_partition_type(const char *arg, char *response)
3336{
3337 uint n = 0;
Mayank Grover52cd10a2018-03-15 12:57:54 +05303338 fs_signature_type fs_signature;
Mayank Groverd38fe012018-03-13 15:33:16 +05303339
3340 if (arg == NULL ||
3341 response == NULL)
3342 {
3343 dprintf(CRITICAL, "Invalid input parameter\n");
3344 return;
3345 }
3346
3347 /* By default copy raw to response */
Mayank Grover0e559042018-04-11 17:49:30 +05303348 strlcpy(response, RAW_STR, MAX_RSP_SIZE);
Mayank Groverd38fe012018-03-13 15:33:16 +05303349
3350 /* Mark partiton type for known paritions only */
3351 for (n=0; n < ARRAY_SIZE(part_type_known); n++)
3352 {
3353 if (!strncmp(part_type_known[n].part_name, arg, strlen(arg)))
3354 {
Mayank Grover52cd10a2018-03-15 12:57:54 +05303355 /* Check partition for FS signature */
3356 fs_signature = check_partition_fs_signature(arg);
3357 switch (fs_signature)
3358 {
3359 case EXT_FS_SIGNATURE:
Mayank Grover0e559042018-04-11 17:49:30 +05303360 strlcpy(response, EXT_STR, MAX_RSP_SIZE);
Mayank Grover52cd10a2018-03-15 12:57:54 +05303361 break;
3362 case EXT_F2FS_SIGNATURE:
Mayank Grover0e559042018-04-11 17:49:30 +05303363 strlcpy(response, F2FS_STR, MAX_RSP_SIZE);
Mayank Grover52cd10a2018-03-15 12:57:54 +05303364 break;
3365 case NO_FS:
Mayank Grover0e559042018-04-11 17:49:30 +05303366 strlcpy(response, part_type_known[n].type_response, MAX_RSP_SIZE);
Mayank Grover52cd10a2018-03-15 12:57:54 +05303367 }
Mayank Groverd38fe012018-03-13 15:33:16 +05303368 }
3369 }
3370 return;
3371}
3372
Mayank Grover11ff9692018-01-11 11:54:49 +05303373/*
3374 * Publish the partition type & size info
3375 * fastboot getvar will publish the required information.
3376 * fastboot getvar partition_size:<partition_name>: partition size in hex
3377 * fastboot getvar partition_type:<partition_name>: partition type (ext/fat)
3378 */
3379static void publish_getvar_partition_info(struct getvar_partition_info *info, uint8_t num_parts)
3380{
Mayank Groverd38fe012018-03-13 15:33:16 +05303381 uint8_t i;
Mayank Grover11ff9692018-01-11 11:54:49 +05303382 static bool published = false;
3383 struct partition_entry *ptn_entry =
3384 partition_get_partition_entries();
3385 memset(info, 0, sizeof(struct getvar_partition_info)* num_parts);
3386
3387 for (i = 0; i < num_parts; i++) {
3388 strlcat(info[i].part_name, (const char *)ptn_entry[i].name, MAX_RSP_SIZE);
3389 strlcat(info[i].getvar_size, "partition-size:", MAX_GET_VAR_NAME_SIZE);
3390 strlcat(info[i].getvar_type, "partition-type:", MAX_GET_VAR_NAME_SIZE);
3391
Mayank Groverd38fe012018-03-13 15:33:16 +05303392 get_partition_type(info[i].part_name, info[i].type_response);
Mayank Grover11ff9692018-01-11 11:54:49 +05303393 get_partition_size(info[i].part_name, info[i].size_response);
Mayank Grover11ff9692018-01-11 11:54:49 +05303394 if (strlcat(info[i].getvar_size, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
3395 {
3396 dprintf(CRITICAL, "partition size name truncated\n");
3397 return;
3398 }
3399 if (strlcat(info[i].getvar_type, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
3400 {
3401 dprintf(CRITICAL, "partition type name truncated\n");
3402 return;
3403 }
3404
3405 if (!published)
3406 {
3407 /* publish partition size & type info */
3408 fastboot_publish((const char *) info[i].getvar_size, (const char *) info[i].size_response);
3409 fastboot_publish((const char *) info[i].getvar_type, (const char *) info[i].type_response);
3410 }
3411 }
3412 if (!published)
3413 published = true;
3414}
3415
3416
Ajay Dudani5c761132011-04-07 20:19:04 -07003417void cmd_flash_mmc_img(const char *arg, void *data, unsigned sz)
Shashank Mittal23b8f422010-04-16 19:27:21 -07003418{
3419 unsigned long long ptn = 0;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07003420 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07003421 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003422 char *token = NULL;
3423 char *pname = NULL;
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07003424 char *sp;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003425 uint8_t lun = 0;
3426 bool lun_set = false;
Mayank Grover351a75e2017-05-30 20:06:08 +05303427 int current_active_slot = INVALID;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07003428
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07003429 token = strtok_r((char *)arg, ":", &sp);
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003430 pname = token;
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07003431 token = strtok_r(NULL, ":", &sp);
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003432 if(token)
3433 {
3434 lun = atoi(token);
3435 mmc_set_lun(lun);
3436 lun_set = true;
3437 }
3438
Mao Jinlong226f33a2014-07-04 17:24:10 +08003439 if (pname)
Greg Grisco6e754772011-06-23 12:19:39 -07003440 {
Channagoud Kadabiad259832015-05-29 11:14:17 -07003441 if (!strncmp(pname, "frp-unlock", strlen("frp-unlock")))
3442 {
3443 if (!aboot_frp_unlock(pname, data, sz))
3444 {
3445 fastboot_info("FRP unlock successful");
3446 fastboot_okay("");
3447 }
3448 else
3449 fastboot_fail("Secret key is invalid, please update the bootloader with secret key");
3450
3451 return;
3452 }
3453
Mao Jinlong226f33a2014-07-04 17:24:10 +08003454 if (!strcmp(pname, "partition"))
3455 {
3456 dprintf(INFO, "Attempt to write partition image.\n");
3457 if (write_partition(sz, (unsigned char *) data)) {
3458 fastboot_fail("failed to write partition");
Greg Grisco6e754772011-06-23 12:19:39 -07003459 return;
3460 }
Mayank Grover11ff9692018-01-11 11:54:49 +05303461 /* Re-publish partition table */
3462 publish_getvar_partition_info(part_info, partition_get_partition_count());
Mayank Grover351a75e2017-05-30 20:06:08 +05303463
3464 /* Rescan partition table to ensure we have multislot support*/
3465 if (partition_scan_for_multislot())
3466 {
3467 current_active_slot = partition_find_active_slot();
3468 dprintf(INFO, "Multislot supported: Slot %s active",
3469 (SUFFIX_SLOT(current_active_slot)));
3470 }
3471 partition_mark_active_slot(current_active_slot);
Greg Grisco6e754772011-06-23 12:19:39 -07003472 }
Mao Jinlong226f33a2014-07-04 17:24:10 +08003473 else
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003474 {
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003475#if VERIFIED_BOOT
3476 if(!strcmp(pname, KEYSTORE_PTN_NAME))
3477 {
3478 if(!device.is_unlocked)
3479 {
3480 fastboot_fail("unlock device to flash keystore");
3481 return;
3482 }
Gaurav Nebhwanic7313cc2015-12-15 22:25:04 +05303483 if(!boot_verify_validate_keystore((unsigned char *)data,sz))
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003484 {
3485 fastboot_fail("image is not a keystore file");
3486 return;
3487 }
3488 }
3489#endif
Mao Jinlong226f33a2014-07-04 17:24:10 +08003490 index = partition_get_index(pname);
3491 ptn = partition_get_offset(index);
3492 if(ptn == 0) {
3493 fastboot_fail("partition table doesn't exist");
3494 return;
3495 }
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003496
Mayank Grover351a75e2017-05-30 20:06:08 +05303497 if (!strncmp(pname, "boot", strlen("boot"))
3498 || !strcmp(pname, "recovery"))
3499 {
Mao Jinlong226f33a2014-07-04 17:24:10 +08003500 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
3501 fastboot_fail("image is not a boot image");
3502 return;
3503 }
Mayank Grover351a75e2017-05-30 20:06:08 +05303504
3505 /* Reset multislot_partition attributes in case of flashing boot */
3506 if (partition_multislot_is_supported())
3507 {
3508 partition_reset_attributes(index);
3509 }
Mao Jinlong226f33a2014-07-04 17:24:10 +08003510 }
3511
3512 if(!lun_set)
3513 {
3514 lun = partition_get_lun(index);
3515 mmc_set_lun(lun);
3516 }
3517
3518 size = partition_get_size(index);
Gaurav Nebhwani4d2389c2016-03-17 21:10:05 +05303519 if (ROUND_TO_PAGE(sz, mmc_blocksize_mask) > size) {
Mao Jinlong226f33a2014-07-04 17:24:10 +08003520 fastboot_fail("size too large");
3521 return;
3522 }
3523 else if (mmc_write(ptn , sz, (unsigned int *)data)) {
3524 fastboot_fail("flash write failure");
3525 return;
3526 }
Greg Grisco6e754772011-06-23 12:19:39 -07003527 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07003528 }
3529 fastboot_okay("");
3530 return;
3531}
3532
Ajay Dudanide984792015-03-02 09:57:41 -08003533void cmd_flash_meta_img(const char *arg, void *data, unsigned sz)
3534{
3535 int i, images;
3536 meta_header_t *meta_header;
3537 img_header_entry_t *img_header_entry;
Parth Dixit3e2d3032016-03-04 17:11:52 +05303538 /*End of the image address*/
3539 uintptr_t data_end;
3540
3541 if( (UINT_MAX - sz) > (uintptr_t)data )
3542 data_end = (uintptr_t)data + sz;
3543 else
3544 {
3545 fastboot_fail("Cannot flash: image header corrupt");
3546 return;
3547 }
3548
3549 if( data_end < ((uintptr_t)data + sizeof(meta_header_t)))
3550 {
3551 fastboot_fail("Cannot flash: image header corrupt");
3552 return;
3553 }
Ajay Dudanide984792015-03-02 09:57:41 -08003554
lijuang8ee21882016-04-19 16:57:11 +08003555 /* If device is locked:
3556 * Forbid to flash image to avoid the device to bypass the image
3557 * which with "any" name other than bootloader. Because it maybe
3558 * a meta package of all partitions.
3559 */
Monika Singh292b3e92018-03-17 22:40:23 +05303560#if VERIFIED_BOOT || VERIFIED_BOOT_2
lijuang8ee21882016-04-19 16:57:11 +08003561 if (target_build_variant_user()) {
3562 if (!device.is_unlocked) {
3563 fastboot_fail("Device is locked, meta image flashing is not allowed");
3564 return;
3565 }
Mayank Grover889be1b2017-09-12 20:12:23 +05303566
Mayank Grover912eaa62017-10-26 12:08:53 +05303567 if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05303568 !device.is_unlock_critical)
3569 {
lijuang8ee21882016-04-19 16:57:11 +08003570 fastboot_fail("Device is critical locked, Meta image flashing is not allowed");
3571 return;
3572 }
lijuang8ee21882016-04-19 16:57:11 +08003573 }
3574#endif
3575
Ajay Dudanide984792015-03-02 09:57:41 -08003576 meta_header = (meta_header_t*) data;
Parth Dixit3e2d3032016-03-04 17:11:52 +05303577 if( data_end < ((uintptr_t)data + meta_header->img_hdr_sz))
3578 {
3579 fastboot_fail("Cannot flash: image header corrupt");
3580 return;
3581 }
Ajay Dudanide984792015-03-02 09:57:41 -08003582 img_header_entry = (img_header_entry_t*) (data+sizeof(meta_header_t));
3583
3584 images = meta_header->img_hdr_sz / sizeof(img_header_entry_t);
3585
3586 for (i=0; i<images; i++) {
3587
3588 if((img_header_entry[i].ptn_name == NULL) ||
3589 (img_header_entry[i].start_offset == 0) ||
3590 (img_header_entry[i].size == 0))
3591 break;
Kishor PK49831502017-04-21 17:55:43 +05303592 if ((UINT_MAX - img_header_entry[i].start_offset) < (uintptr_t)data) {
3593 fastboot_fail("Integer overflow detected in start_offset of img");
3594 break;
3595 }
3596 else if ((UINT_MAX - (img_header_entry[i].start_offset + (uintptr_t)data)) < img_header_entry[i].size) {
3597 fastboot_fail("Integer overflow detected in size of img");
3598 break;
3599 }
Parth Dixit3e2d3032016-03-04 17:11:52 +05303600 if( data_end < ((uintptr_t)data + img_header_entry[i].start_offset
3601 + img_header_entry[i].size) )
3602 {
3603 fastboot_fail("Cannot flash: image size mismatch");
3604 break;
3605 }
3606
Ajay Dudanide984792015-03-02 09:57:41 -08003607 cmd_flash_mmc_img(img_header_entry[i].ptn_name,
3608 (void *) data + img_header_entry[i].start_offset,
3609 img_header_entry[i].size);
3610 }
3611
Channagoud Kadabiaafbbfc2015-03-04 17:52:56 -08003612 if (!strncmp(arg, "bootloader", strlen("bootloader")))
3613 {
3614 strlcpy(device.bootloader_version, TARGET(BOARD), MAX_VERSION_LEN);
3615 strlcat(device.bootloader_version, "-", MAX_VERSION_LEN);
3616 strlcat(device.bootloader_version, meta_header->img_version, MAX_VERSION_LEN);
3617 }
3618 else
3619 {
3620 strlcpy(device.radio_version, TARGET(BOARD), MAX_VERSION_LEN);
3621 strlcat(device.radio_version, "-", MAX_VERSION_LEN);
3622 strlcat(device.radio_version, meta_header->img_version, MAX_VERSION_LEN);
3623 }
3624
3625 write_device_info(&device);
Ajay Dudanide984792015-03-02 09:57:41 -08003626 fastboot_okay("");
3627 return;
3628}
3629
Ajay Dudani5c761132011-04-07 20:19:04 -07003630void cmd_flash_mmc_sparse_img(const char *arg, void *data, unsigned sz)
3631{
3632 unsigned int chunk;
wufeng.jiang813dc352015-06-02 23:02:46 -04003633 uint64_t chunk_data_sz;
Channagoud Kadabif627cf82013-09-09 14:08:20 -07003634 uint32_t *fill_buf = NULL;
3635 uint32_t fill_val;
Mayank Grover4f50bba2017-07-19 18:17:08 +05303636 uint32_t blk_sz_actual = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07003637 sparse_header_t *sparse_header;
3638 chunk_header_t *chunk_header;
Ajay Dudaniab18f022011-05-12 14:39:22 -07003639 uint32_t total_blocks = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07003640 unsigned long long ptn = 0;
Channagoud Kadabi65b91002011-10-11 17:34:33 +05303641 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07003642 int index = INVALID_PTN;
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05303643 uint32_t i;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003644 uint8_t lun = 0;
vijay kumar800255e2015-04-24 20:26:19 +05303645 /*End of the sparse image address*/
Parth Dixit64b1a482016-03-07 16:31:26 +05303646 uintptr_t data_end = (uintptr_t)data + sz;
Ajay Dudani5c761132011-04-07 20:19:04 -07003647
Kinson Chikf1a43512011-07-14 11:28:39 -07003648 index = partition_get_index(arg);
3649 ptn = partition_get_offset(index);
3650 if(ptn == 0) {
Ajay Dudani5c761132011-04-07 20:19:04 -07003651 fastboot_fail("partition table doesn't exist");
3652 return;
3653 }
3654
Channagoud Kadabi65b91002011-10-11 17:34:33 +05303655 size = partition_get_size(index);
Channagoud Kadabi65b91002011-10-11 17:34:33 +05303656
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003657 lun = partition_get_lun(index);
3658 mmc_set_lun(lun);
3659
vijay kumar800255e2015-04-24 20:26:19 +05303660 if (sz < sizeof(sparse_header_t)) {
3661 fastboot_fail("size too low");
3662 return;
3663 }
3664
Ajay Dudani5c761132011-04-07 20:19:04 -07003665 /* Read and skip over sparse image header */
3666 sparse_header = (sparse_header_t *) data;
vijay kumar800255e2015-04-24 20:26:19 +05303667
Mayank Grover48bd9bb2017-07-19 12:04:16 +05303668 if (!sparse_header->blk_sz || (sparse_header->blk_sz % 4)){
3669 fastboot_fail("Invalid block size\n");
3670 return;
3671 }
3672
vijay kumar1321f342015-03-27 12:13:42 +05303673 if (((uint64_t)sparse_header->total_blks * (uint64_t)sparse_header->blk_sz) > size) {
Ajay Dudani876b3282012-12-21 14:12:17 -08003674 fastboot_fail("size too large");
3675 return;
3676 }
3677
vijay kumarde4fcf62015-04-23 13:05:49 +05303678 data += sizeof(sparse_header_t);
vijay kumar800255e2015-04-24 20:26:19 +05303679
Parth Dixit64b1a482016-03-07 16:31:26 +05303680 if (data_end < (uintptr_t)data) {
vijay kumar800255e2015-04-24 20:26:19 +05303681 fastboot_fail("buffer overreads occured due to invalid sparse header");
3682 return;
3683 }
3684
vijay kumarde4fcf62015-04-23 13:05:49 +05303685 if(sparse_header->file_hdr_sz != sizeof(sparse_header_t))
Ajay Dudani5c761132011-04-07 20:19:04 -07003686 {
vijay kumarde4fcf62015-04-23 13:05:49 +05303687 fastboot_fail("sparse header size mismatch");
3688 return;
Ajay Dudani5c761132011-04-07 20:19:04 -07003689 }
3690
Ajay Dudanib06c05f2011-05-12 14:46:10 -07003691 dprintf (SPEW, "=== Sparse Image Header ===\n");
3692 dprintf (SPEW, "magic: 0x%x\n", sparse_header->magic);
3693 dprintf (SPEW, "major_version: 0x%x\n", sparse_header->major_version);
3694 dprintf (SPEW, "minor_version: 0x%x\n", sparse_header->minor_version);
3695 dprintf (SPEW, "file_hdr_sz: %d\n", sparse_header->file_hdr_sz);
3696 dprintf (SPEW, "chunk_hdr_sz: %d\n", sparse_header->chunk_hdr_sz);
3697 dprintf (SPEW, "blk_sz: %d\n", sparse_header->blk_sz);
3698 dprintf (SPEW, "total_blks: %d\n", sparse_header->total_blks);
3699 dprintf (SPEW, "total_chunks: %d\n", sparse_header->total_chunks);
Ajay Dudani5c761132011-04-07 20:19:04 -07003700
3701 /* Start processing chunks */
3702 for (chunk=0; chunk<sparse_header->total_chunks; chunk++)
3703 {
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05303704 /* Make sure the total image size does not exceed the partition size */
3705 if(((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz) >= size) {
3706 fastboot_fail("size too large");
3707 return;
3708 }
Ajay Dudani5c761132011-04-07 20:19:04 -07003709 /* Read and skip over chunk header */
3710 chunk_header = (chunk_header_t *) data;
3711 data += sizeof(chunk_header_t);
3712
Parth Dixit64b1a482016-03-07 16:31:26 +05303713 if (data_end < (uintptr_t)data) {
vijay kumar800255e2015-04-24 20:26:19 +05303714 fastboot_fail("buffer overreads occured due to invalid sparse header");
3715 return;
3716 }
3717
Ajay Dudani5c761132011-04-07 20:19:04 -07003718 dprintf (SPEW, "=== Chunk Header ===\n");
3719 dprintf (SPEW, "chunk_type: 0x%x\n", chunk_header->chunk_type);
3720 dprintf (SPEW, "chunk_data_sz: 0x%x\n", chunk_header->chunk_sz);
3721 dprintf (SPEW, "total_size: 0x%x\n", chunk_header->total_sz);
3722
vijay kumar800255e2015-04-24 20:26:19 +05303723 if(sparse_header->chunk_hdr_sz != sizeof(chunk_header_t))
Ajay Dudani5c761132011-04-07 20:19:04 -07003724 {
vijay kumar800255e2015-04-24 20:26:19 +05303725 fastboot_fail("chunk header size mismatch");
3726 return;
Ajay Dudani5c761132011-04-07 20:19:04 -07003727 }
3728
wufeng.jiang813dc352015-06-02 23:02:46 -04003729 chunk_data_sz = (uint64_t)sparse_header->blk_sz * chunk_header->chunk_sz;
3730
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05303731 /* Make sure that the chunk size calculated from sparse image does not
3732 * exceed partition size
3733 */
3734 if ((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz + chunk_data_sz > size)
3735 {
3736 fastboot_fail("Chunk data size exceeds partition size");
3737 return;
3738 }
3739
Ajay Dudani5c761132011-04-07 20:19:04 -07003740 switch (chunk_header->chunk_type)
3741 {
3742 case CHUNK_TYPE_RAW:
wufeng.jiang813dc352015-06-02 23:02:46 -04003743 if((uint64_t)chunk_header->total_sz != ((uint64_t)sparse_header->chunk_hdr_sz +
Ajay Dudani5c761132011-04-07 20:19:04 -07003744 chunk_data_sz))
3745 {
3746 fastboot_fail("Bogus chunk size for chunk type Raw");
3747 return;
3748 }
3749
Parth Dixit64b1a482016-03-07 16:31:26 +05303750 if (data_end < (uintptr_t)data + chunk_data_sz) {
vijay kumar800255e2015-04-24 20:26:19 +05303751 fastboot_fail("buffer overreads occured due to invalid sparse header");
3752 return;
3753 }
3754
wufeng.jiang813dc352015-06-02 23:02:46 -04003755 /* chunk_header->total_sz is uint32,So chunk_data_sz is now less than 2^32
3756 otherwise it will return in the line above
3757 */
Ajay Dudaniab18f022011-05-12 14:39:22 -07003758 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
wufeng.jiang813dc352015-06-02 23:02:46 -04003759 (uint32_t)chunk_data_sz,
Ajay Dudaniab18f022011-05-12 14:39:22 -07003760 (unsigned int*)data))
Ajay Dudani5c761132011-04-07 20:19:04 -07003761 {
3762 fastboot_fail("flash write failure");
3763 return;
3764 }
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05303765 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
3766 fastboot_fail("Bogus size for RAW chunk type");
3767 return;
3768 }
Ajay Dudani5c761132011-04-07 20:19:04 -07003769 total_blocks += chunk_header->chunk_sz;
wufeng.jiang813dc352015-06-02 23:02:46 -04003770 data += (uint32_t)chunk_data_sz;
Ajay Dudani5c761132011-04-07 20:19:04 -07003771 break;
3772
Channagoud Kadabif627cf82013-09-09 14:08:20 -07003773 case CHUNK_TYPE_FILL:
3774 if(chunk_header->total_sz != (sparse_header->chunk_hdr_sz +
3775 sizeof(uint32_t)))
3776 {
3777 fastboot_fail("Bogus chunk size for chunk type FILL");
3778 return;
3779 }
3780
Mayank Grover4f50bba2017-07-19 18:17:08 +05303781 blk_sz_actual = ROUNDUP(sparse_header->blk_sz, CACHE_LINE);
3782 /* Integer overflow detected */
3783 if (blk_sz_actual < sparse_header->blk_sz)
3784 {
3785 fastboot_fail("Invalid block size");
3786 return;
3787 }
3788
3789 fill_buf = (uint32_t *)memalign(CACHE_LINE, blk_sz_actual);
Channagoud Kadabif627cf82013-09-09 14:08:20 -07003790 if (!fill_buf)
3791 {
3792 fastboot_fail("Malloc failed for: CHUNK_TYPE_FILL");
3793 return;
3794 }
3795
Parth Dixit64b1a482016-03-07 16:31:26 +05303796 if (data_end < (uintptr_t)data + sizeof(uint32_t)) {
vijay kumar800255e2015-04-24 20:26:19 +05303797 fastboot_fail("buffer overreads occured due to invalid sparse header");
Gaurav Nebhwani163cbf82016-03-17 21:21:27 +05303798 free(fill_buf);
vijay kumar800255e2015-04-24 20:26:19 +05303799 return;
3800 }
Channagoud Kadabif627cf82013-09-09 14:08:20 -07003801 fill_val = *(uint32_t *)data;
3802 data = (char *) data + sizeof(uint32_t);
Channagoud Kadabif627cf82013-09-09 14:08:20 -07003803
3804 for (i = 0; i < (sparse_header->blk_sz / sizeof(fill_val)); i++)
3805 {
3806 fill_buf[i] = fill_val;
3807 }
3808
Gaurav Nebhwanie94cbe12016-03-17 21:16:34 +05303809 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz))
3810 {
3811 fastboot_fail("bogus size for chunk FILL type");
3812 free(fill_buf);
3813 return;
3814 }
3815
wufeng.jiang813dc352015-06-02 23:02:46 -04003816 for (i = 0; i < chunk_header->chunk_sz; i++)
Channagoud Kadabif627cf82013-09-09 14:08:20 -07003817 {
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05303818 /* Make sure that the data written to partition does not exceed partition size */
3819 if ((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz + sparse_header->blk_sz > size)
3820 {
3821 fastboot_fail("Chunk data size for fill type exceeds partition size");
Gaurav Nebhwani163cbf82016-03-17 21:21:27 +05303822 free(fill_buf);
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05303823 return;
3824 }
3825
Channagoud Kadabif627cf82013-09-09 14:08:20 -07003826 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
3827 sparse_header->blk_sz,
3828 fill_buf))
3829 {
3830 fastboot_fail("flash write failure");
3831 free(fill_buf);
3832 return;
3833 }
3834
3835 total_blocks++;
3836 }
3837
3838 free(fill_buf);
3839 break;
3840
Ajay Dudani5c761132011-04-07 20:19:04 -07003841 case CHUNK_TYPE_DONT_CARE:
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05303842 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
3843 fastboot_fail("bogus size for chunk DONT CARE type");
3844 return;
3845 }
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07003846 total_blocks += chunk_header->chunk_sz;
3847 break;
3848
Ajay Dudani5c761132011-04-07 20:19:04 -07003849 case CHUNK_TYPE_CRC:
3850 if(chunk_header->total_sz != sparse_header->chunk_hdr_sz)
3851 {
wufeng.jiang813dc352015-06-02 23:02:46 -04003852 fastboot_fail("Bogus chunk size for chunk type CRC");
Ajay Dudani5c761132011-04-07 20:19:04 -07003853 return;
3854 }
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05303855 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
3856 fastboot_fail("bogus size for chunk CRC type");
3857 return;
3858 }
Ajay Dudani5c761132011-04-07 20:19:04 -07003859 total_blocks += chunk_header->chunk_sz;
Parth Dixit64b1a482016-03-07 16:31:26 +05303860 if ((uintptr_t)data > UINT_MAX - chunk_data_sz) {
vijay kumar800255e2015-04-24 20:26:19 +05303861 fastboot_fail("integer overflow occured");
3862 return;
3863 }
wufeng.jiang813dc352015-06-02 23:02:46 -04003864 data += (uint32_t)chunk_data_sz;
Parth Dixit64b1a482016-03-07 16:31:26 +05303865 if (data_end < (uintptr_t)data) {
vijay kumar800255e2015-04-24 20:26:19 +05303866 fastboot_fail("buffer overreads occured due to invalid sparse header");
3867 return;
3868 }
Ajay Dudani5c761132011-04-07 20:19:04 -07003869 break;
3870
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07003871 default:
Channagoud Kadabif627cf82013-09-09 14:08:20 -07003872 dprintf(CRITICAL, "Unkown chunk type: %x\n",chunk_header->chunk_type);
Ajay Dudani5c761132011-04-07 20:19:04 -07003873 fastboot_fail("Unknown chunk type");
3874 return;
3875 }
3876 }
3877
Ajay Dudani0c6927b2011-05-18 11:12:16 -07003878 dprintf(INFO, "Wrote %d blocks, expected to write %d blocks\n",
3879 total_blocks, sparse_header->total_blks);
3880
3881 if(total_blocks != sparse_header->total_blks)
3882 {
3883 fastboot_fail("sparse image write failure");
3884 }
Ajay Dudani5c761132011-04-07 20:19:04 -07003885
3886 fastboot_okay("");
3887 return;
3888}
3889
3890void cmd_flash_mmc(const char *arg, void *data, unsigned sz)
3891{
3892 sparse_header_t *sparse_header;
Ajay Dudanide984792015-03-02 09:57:41 -08003893 meta_header_t *meta_header;
Ajay Dudani5c761132011-04-07 20:19:04 -07003894
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07003895#ifdef SSD_ENABLE
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -08003896 /* 8 Byte Magic + 2048 Byte xml + Encrypted Data */
3897 unsigned int *magic_number = (unsigned int *) data;
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08003898 int ret=0;
3899 uint32 major_version=0;
3900 uint32 minor_version=0;
3901
3902 ret = scm_svc_version(&major_version,&minor_version);
3903 if(!ret)
3904 {
3905 if(major_version >= 2)
3906 {
Amir Samuelovbb65ce02013-05-05 12:20:18 +03003907 if( !strcmp(arg, "ssd") || !strcmp(arg, "tqs") )
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08003908 {
3909 ret = encrypt_scm((uint32 **) &data, &sz);
3910 if (ret != 0) {
3911 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
3912 return;
3913 }
3914
Amir Samuelovbb65ce02013-05-05 12:20:18 +03003915 /* Protect only for SSD */
3916 if (!strcmp(arg, "ssd")) {
3917 ret = scm_protect_keystore((uint32 *) data, sz);
3918 if (ret != 0) {
3919 dprintf(CRITICAL, "ERROR: scm_protect_keystore Failed\n");
3920 return;
3921 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08003922 }
3923 }
3924 else
3925 {
3926 ret = decrypt_scm_v2((uint32 **) &data, &sz);
3927 if(ret != 0)
3928 {
3929 dprintf(CRITICAL,"ERROR: Decryption Failure\n");
3930 return;
3931 }
3932 }
3933 }
3934 else
3935 {
3936 if (magic_number[0] == DECRYPT_MAGIC_0 &&
3937 magic_number[1] == DECRYPT_MAGIC_1)
3938 {
3939 ret = decrypt_scm((uint32 **) &data, &sz);
3940 if (ret != 0) {
3941 dprintf(CRITICAL, "ERROR: Invalid secure image\n");
3942 return;
3943 }
3944 }
3945 else if (magic_number[0] == ENCRYPT_MAGIC_0 &&
3946 magic_number[1] == ENCRYPT_MAGIC_1)
3947 {
3948 ret = encrypt_scm((uint32 **) &data, &sz);
3949 if (ret != 0) {
3950 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
3951 return;
3952 }
3953 }
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07003954 }
3955 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08003956 else
Neeti Desai127b9e02012-03-20 16:11:23 -07003957 {
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08003958 dprintf(CRITICAL,"INVALID SVC Version\n");
3959 return;
Neeti Desai127b9e02012-03-20 16:11:23 -07003960 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08003961#endif /* SSD_ENABLE */
Neeti Desai127b9e02012-03-20 16:11:23 -07003962
Monika Singh292b3e92018-03-17 22:40:23 +05303963#if VERIFIED_BOOT || VERIFIED_BOOT_2
Channagoud Kadabi35297672015-06-13 11:09:49 -07003964 if (target_build_variant_user())
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003965 {
lijuang511a2b52015-08-14 20:50:51 +08003966 /* if device is locked:
3967 * common partition will not allow to be flashed
3968 * critical partition will allow to flash image.
3969 */
3970 if(!device.is_unlocked && !critical_flash_allowed(arg)) {
3971 fastboot_fail("Partition flashing is not allowed");
3972 return;
3973 }
Mayank Grover889be1b2017-09-12 20:12:23 +05303974
lijuang511a2b52015-08-14 20:50:51 +08003975 /* if device critical is locked:
3976 * common partition will allow to be flashed
3977 * critical partition will not allow to flash image.
3978 */
Mayank Grover912eaa62017-10-26 12:08:53 +05303979 if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05303980 !device.is_unlock_critical &&
3981 critical_flash_allowed(arg)) {
3982 fastboot_fail("Critical partition flashing is not allowed");
3983 return;
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003984 }
3985 }
3986#endif
Monika Singh292b3e92018-03-17 22:40:23 +05303987 if (!strncmp(arg, "avb_custom_key", strlen("avb_custom_key"))) {
3988 dprintf(INFO, "flashing avb_custom_key\n");
3989 if (store_userkey(data, sz)) {
3990 fastboot_fail("Flashing avb_custom_key failed");
3991 } else {
3992 fastboot_okay("");
3993 }
3994 return;
3995 }
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003996
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07003997 sparse_header = (sparse_header_t *) data;
Ajay Dudanide984792015-03-02 09:57:41 -08003998 meta_header = (meta_header_t *) data;
3999 if (sparse_header->magic == SPARSE_HEADER_MAGIC)
Ajay Dudani5c761132011-04-07 20:19:04 -07004000 cmd_flash_mmc_sparse_img(arg, data, sz);
Ajay Dudanide984792015-03-02 09:57:41 -08004001 else if (meta_header->magic == META_HEADER_MAGIC)
4002 cmd_flash_meta_img(arg, data, sz);
4003 else
4004 cmd_flash_mmc_img(arg, data, sz);
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07004005
4006#if VERIFIED_BOOT
Mayank Grover912eaa62017-10-26 12:08:53 +05304007 if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05304008 (!strncmp(arg, "system", 6)) &&
4009 !device.verity_mode)
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07004010 // reset dm_verity mode to enforcing
4011 device.verity_mode = 1;
4012 write_device_info(&device);
Parth Dixitddbc7352015-10-18 03:13:31 +05304013#endif
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07004014
Ajay Dudani5c761132011-04-07 20:19:04 -07004015 return;
4016}
4017
Tanya Brokhman84eaaf62015-02-24 21:57:06 +02004018void cmd_updatevol(const char *vol_name, void *data, unsigned sz)
4019{
4020 struct ptentry *sys_ptn;
4021 struct ptable *ptable;
4022
4023 ptable = flash_get_ptable();
4024 if (ptable == NULL) {
4025 fastboot_fail("partition table doesn't exist");
4026 return;
4027 }
4028
4029 sys_ptn = ptable_find(ptable, "system");
4030 if (sys_ptn == NULL) {
4031 fastboot_fail("system partition not found");
4032 return;
4033 }
4034
4035 sz = ROUND_TO_PAGE(sz, page_mask);
4036 if (update_ubi_vol(sys_ptn, vol_name, data, sz))
4037 fastboot_fail("update_ubi_vol failed");
4038 else
4039 fastboot_okay("");
4040}
4041
Sridhar Parasurame94e8152014-10-24 14:06:03 -07004042void cmd_flash_nand(const char *arg, void *data, unsigned sz)
Dima Zavin214cc642009-01-26 11:16:21 -08004043{
4044 struct ptentry *ptn;
4045 struct ptable *ptable;
4046 unsigned extra = 0;
Gaurav Nebhwanid9730712016-05-06 14:28:29 +05304047 uint64_t partition_size = 0;
Vijay Kumar Pendotic6f22a72017-05-19 22:44:34 +05304048 unsigned bytes_to_round_page = 0;
4049 unsigned rounded_size = 0;
Dima Zavin214cc642009-01-26 11:16:21 -08004050
Kishor PK38ed93d2017-04-25 14:19:26 +05304051 if((uintptr_t)data > (UINT_MAX - sz)) {
4052 fastboot_fail("Cannot flash: image header corrupt");
4053 return;
4054 }
4055
Dima Zavin214cc642009-01-26 11:16:21 -08004056 ptable = flash_get_ptable();
4057 if (ptable == NULL) {
4058 fastboot_fail("partition table doesn't exist");
4059 return;
4060 }
4061
4062 ptn = ptable_find(ptable, arg);
4063 if (ptn == NULL) {
Tanya Brokhman84eaaf62015-02-24 21:57:06 +02004064 dprintf(INFO, "unknown partition name (%s). Trying updatevol\n",
4065 arg);
4066 cmd_updatevol(arg, data, sz);
Dima Zavin214cc642009-01-26 11:16:21 -08004067 return;
4068 }
4069
Monika Singh292b3e92018-03-17 22:40:23 +05304070 if (!strncmp(arg, "avb_custom_key", strlen("avb_custom_key"))) {
4071 dprintf(INFO, "flashing avb_custom_key\n");
4072 if (store_userkey(data, sz)) {
4073 fastboot_fail("Flashing avb_custom_key failed");
4074 } else {
4075 fastboot_okay("");
4076 }
4077 return;
4078 }
4079
Dima Zavin214cc642009-01-26 11:16:21 -08004080 if (!strcmp(ptn->name, "boot") || !strcmp(ptn->name, "recovery")) {
Kishor PK38ed93d2017-04-25 14:19:26 +05304081 if((sz > BOOT_MAGIC_SIZE) && (!memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE))) {
4082 dprintf(INFO, "Verified the BOOT_MAGIC in image header \n");
4083 } else {
4084 fastboot_fail("Image is not a boot image");
Dima Zavin214cc642009-01-26 11:16:21 -08004085 return;
4086 }
4087 }
4088
Amol Jadi5c61a952012-05-04 17:05:35 -07004089 if (!strcmp(ptn->name, "system")
Deepa Dinamani13e32c42012-03-12 14:34:17 -07004090 || !strcmp(ptn->name, "userdata")
4091 || !strcmp(ptn->name, "persist")
Sundarajan Srinivasanb063a852013-11-19 14:02:27 -08004092 || !strcmp(ptn->name, "recoveryfs")
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -08004093 || !strcmp(ptn->name, "modem"))
Tanya Brokhman1c94f1a2015-02-15 09:05:03 +02004094 extra = 1;
Kishor PK38ed93d2017-04-25 14:19:26 +05304095 else {
Vijay Kumar Pendotic6f22a72017-05-19 22:44:34 +05304096 rounded_size = ROUNDUP(sz, page_size);
4097 bytes_to_round_page = rounded_size - sz;
4098 if (bytes_to_round_page) {
4099 if (((uintptr_t)data + sz ) > (UINT_MAX - bytes_to_round_page)) {
4100 fastboot_fail("Integer overflow detected");
4101 return;
4102 }
4103 if (((uintptr_t)data + sz + bytes_to_round_page) >
4104 ((uintptr_t)target_get_scratch_address() + target_get_max_flash_size())) {
4105 fastboot_fail("Buffer size is not aligned to page_size");
4106 return;
4107 }
4108 else {
4109 memset(data + sz, 0, bytes_to_round_page);
4110 sz = rounded_size;
4111 }
Kishor PK38ed93d2017-04-25 14:19:26 +05304112 }
Gaurav Nebhwanid9730712016-05-06 14:28:29 +05304113 }
4114
Kishor PK38ed93d2017-04-25 14:19:26 +05304115 /*Checking partition_size for the possible integer overflow */
4116 partition_size = validate_partition_size(ptn);
4117
Gaurav Nebhwanid9730712016-05-06 14:28:29 +05304118 if (sz > partition_size) {
4119 fastboot_fail("Image size too large");
4120 return;
4121 }
4122
Dima Zavin214cc642009-01-26 11:16:21 -08004123 dprintf(INFO, "writing %d bytes to '%s'\n", sz, ptn->name);
Mayank Grover6cde9352017-06-06 18:45:23 +05304124 if ((sz > UBI_EC_HDR_SIZE) &&
4125 (!memcmp((void *)data, UBI_MAGIC, UBI_MAGIC_SIZE))) {
Tanya Brokhman1c94f1a2015-02-15 09:05:03 +02004126 if (flash_ubi_img(ptn, data, sz)) {
4127 fastboot_fail("flash write failure");
4128 return;
4129 }
4130 } else {
4131 if (flash_write(ptn, extra, data, sz)) {
4132 fastboot_fail("flash write failure");
4133 return;
4134 }
Dima Zavin214cc642009-01-26 11:16:21 -08004135 }
4136 dprintf(INFO, "partition '%s' updated\n", ptn->name);
4137 fastboot_okay("");
4138}
4139
Kishor PK38ed93d2017-04-25 14:19:26 +05304140
4141static inline uint64_t validate_partition_size(struct ptentry *ptn)
4142{
4143 if (ptn->length && flash_num_pages_per_blk() && page_size) {
4144 if ((ptn->length < ( UINT_MAX / flash_num_pages_per_blk())) && ((ptn->length * flash_num_pages_per_blk()) < ( UINT_MAX / page_size))) {
4145 return ptn->length * flash_num_pages_per_blk() * page_size;
4146 }
4147 }
4148 return 0;
4149}
4150
4151
Sridhar Parasurame94e8152014-10-24 14:06:03 -07004152void cmd_flash(const char *arg, void *data, unsigned sz)
4153{
4154 if(target_is_emmc_boot())
4155 cmd_flash_mmc(arg, data, sz);
4156 else
4157 cmd_flash_nand(arg, data, sz);
4158}
4159
Dima Zavin214cc642009-01-26 11:16:21 -08004160void cmd_continue(const char *arg, void *data, unsigned sz)
4161{
4162 fastboot_okay("");
Amol Jadi7c4316c2013-10-07 14:19:26 -07004163 fastboot_stop();
Shashank Mittald3e54dd2014-08-28 15:24:02 -07004164
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004165 if (target_is_emmc_boot())
4166 {
lijuanga40d6302015-07-20 20:10:13 +08004167#if FBCON_DISPLAY_MSG
lijuangde34d502016-02-26 16:04:50 +08004168 /* Exit keys' detection thread firstly */
4169 exit_menu_keys_detection();
lijuanga40d6302015-07-20 20:10:13 +08004170#endif
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004171 boot_linux_from_mmc();
4172 }
4173 else
4174 {
4175 boot_linux_from_flash();
4176 }
Dima Zavin214cc642009-01-26 11:16:21 -08004177}
4178
Chandan Uddaraju94183c02010-01-15 15:13:59 -08004179void cmd_reboot(const char *arg, void *data, unsigned sz)
4180{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004181 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08004182 fastboot_okay("");
4183 reboot_device(0);
4184}
4185
Mayank Grover351a75e2017-05-30 20:06:08 +05304186void cmd_set_active(const char *arg, void *data, unsigned sz)
4187{
Mayank Grover6ccc1c92017-07-04 17:36:46 +05304188 char *p, *sp = NULL;
Mayank Grover351a75e2017-05-30 20:06:08 +05304189 unsigned i,current_active_slot;
4190 const char *current_slot_suffix;
4191
4192 if (!partition_multislot_is_supported())
4193 {
4194 fastboot_fail("Command not supported");
4195 return;
4196 }
4197
4198 if (arg)
4199 {
Mayank Grover6ccc1c92017-07-04 17:36:46 +05304200 p = strtok_r((char *)arg, ":", &sp);
Mayank Grover5eb5f692017-07-19 20:16:04 +05304201 if (p)
Mayank Grover351a75e2017-05-30 20:06:08 +05304202 {
4203 current_active_slot = partition_find_active_slot();
4204
4205 /* Check if trying to make curent slot active */
4206 current_slot_suffix = SUFFIX_SLOT(current_active_slot);
Mayank Grover6ccc1c92017-07-04 17:36:46 +05304207 current_slot_suffix = strtok_r((char *)current_slot_suffix,
4208 (char *)suffix_delimiter, &sp);
4209
Mayank Grover5eb5f692017-07-19 20:16:04 +05304210 if (current_slot_suffix &&
4211 !strncmp(p, current_slot_suffix, strlen(current_slot_suffix)))
Mayank Grover351a75e2017-05-30 20:06:08 +05304212 {
4213 fastboot_okay("Slot already set active");
4214 return;
4215 }
4216 else
4217 {
4218 for (i = 0; i < AB_SUPPORTED_SLOTS; i++)
4219 {
4220 current_slot_suffix = SUFFIX_SLOT(i);
Mayank Grover6ccc1c92017-07-04 17:36:46 +05304221 current_slot_suffix = strtok_r((char *)current_slot_suffix,
4222 (char *)suffix_delimiter, &sp);
Mayank Grover5eb5f692017-07-19 20:16:04 +05304223 if (current_slot_suffix &&
4224 !strncmp(p, current_slot_suffix, strlen(current_slot_suffix)))
Mayank Grover351a75e2017-05-30 20:06:08 +05304225 {
4226 partition_switch_slots(current_active_slot, i);
4227 publish_getvar_multislot_vars();
4228 fastboot_okay("");
4229 return;
4230 }
4231 }
4232 }
4233 }
4234 }
4235 fastboot_fail("Invalid slot suffix.");
4236 return;
4237}
4238
Chandan Uddaraju94183c02010-01-15 15:13:59 -08004239void cmd_reboot_bootloader(const char *arg, void *data, unsigned sz)
4240{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004241 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08004242 fastboot_okay("");
4243 reboot_device(FASTBOOT_MODE);
4244}
4245
Ameya Thakur11cf1a62013-08-05 12:44:48 -07004246void cmd_oem_enable_charger_screen(const char *arg, void *data, unsigned size)
4247{
4248 dprintf(INFO, "Enabling charger screen check\n");
4249 device.charger_screen_enabled = 1;
4250 write_device_info(&device);
4251 fastboot_okay("");
4252}
4253
4254void cmd_oem_disable_charger_screen(const char *arg, void *data, unsigned size)
4255{
4256 dprintf(INFO, "Disabling charger screen check\n");
4257 device.charger_screen_enabled = 0;
4258 write_device_info(&device);
4259 fastboot_okay("");
4260}
4261
lijuanga25d8bb2015-09-16 13:11:52 +08004262void cmd_oem_off_mode_charger(const char *arg, void *data, unsigned size)
4263{
4264 char *p = NULL;
4265 const char *delim = " \t\n\r";
Parth Dixit407f15b2016-01-07 14:47:37 +05304266 char *sp;
lijuanga25d8bb2015-09-16 13:11:52 +08004267
4268 if (arg) {
Parth Dixit407f15b2016-01-07 14:47:37 +05304269 p = strtok_r((char *)arg, delim, &sp);
lijuanga25d8bb2015-09-16 13:11:52 +08004270 if (p) {
4271 if (!strncmp(p, "0", 1)) {
4272 device.charger_screen_enabled = 0;
4273 } else if (!strncmp(p, "1", 1)) {
4274 device.charger_screen_enabled = 1;
4275 }
4276 }
4277 }
4278
4279 /* update charger_screen_enabled value for getvar
4280 * command
4281 */
4282 snprintf(charger_screen_enabled, MAX_RSP_SIZE, "%d",
4283 device.charger_screen_enabled);
4284
4285 write_device_info(&device);
4286 fastboot_okay("");
4287}
4288
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05304289void cmd_oem_select_display_panel(const char *arg, void *data, unsigned size)
4290{
4291 dprintf(INFO, "Selecting display panel %s\n", arg);
4292 if (arg)
4293 strlcpy(device.display_panel, arg,
4294 sizeof(device.display_panel));
4295 write_device_info(&device);
4296 fastboot_okay("");
4297}
4298
Shashank Mittal162244e2011-08-08 19:01:25 -07004299void cmd_oem_unlock(const char *arg, void *data, unsigned sz)
4300{
lijuang4ece1e72015-08-14 21:02:36 +08004301 set_device_unlock(UNLOCK, TRUE);
vijay kumarc65876c2015-04-24 13:29:16 +05304302}
4303
4304void cmd_oem_unlock_go(const char *arg, void *data, unsigned sz)
4305{
lijuang4ece1e72015-08-14 21:02:36 +08004306 if(!device.is_unlocked) {
vijay kumarc65876c2015-04-24 13:29:16 +05304307 if(!is_allow_unlock) {
4308 fastboot_fail("oem unlock is not allowed");
4309 return;
4310 }
4311
lijuang4ece1e72015-08-14 21:02:36 +08004312 set_device_unlock_value(UNLOCK, TRUE);
vijay kumarc65876c2015-04-24 13:29:16 +05304313
lijuang4ece1e72015-08-14 21:02:36 +08004314 /* wipe data */
vijay kumarc65876c2015-04-24 13:29:16 +05304315 struct recovery_message msg;
Kishor PK60a68212017-05-08 16:55:57 +05304316 memset(&msg, 0, sizeof(msg));
vijay kumarc65876c2015-04-24 13:29:16 +05304317 snprintf(msg.recovery, sizeof(msg.recovery), "recovery\n--wipe_data");
4318 write_misc(0, &msg, sizeof(msg));
4319
4320 fastboot_okay("");
4321 reboot_device(RECOVERY_MODE);
Shashank Mittald3e54dd2014-08-28 15:24:02 -07004322 }
4323 fastboot_okay("");
4324}
4325
Channagoud Kadabiad259832015-05-29 11:14:17 -07004326static int aboot_frp_unlock(char *pname, void *data, unsigned sz)
4327{
Mayank Grover70f8b0e2017-05-17 11:49:00 +05304328 int ret=1;
4329 bool authentication_success=false;
Channagoud Kadabiad259832015-05-29 11:14:17 -07004330
Mayank Grover70f8b0e2017-05-17 11:49:00 +05304331 /*
4332 Authentication method not implemented.
4333
4334 OEM to implement, authentication system which on successful validataion,
4335 calls write_allow_oem_unlock() with is_allow_unlock.
4336 */
4337#if 0
4338 authentication_success = oem_specific_auth_mthd();
4339#endif
4340
4341 if (authentication_success)
Channagoud Kadabiad259832015-05-29 11:14:17 -07004342 {
Mayank Grover70f8b0e2017-05-17 11:49:00 +05304343 is_allow_unlock = true;
4344 write_allow_oem_unlock(is_allow_unlock);
4345 ret = 0;
Channagoud Kadabiad259832015-05-29 11:14:17 -07004346 }
4347 return ret;
4348}
4349
Shashank Mittald3e54dd2014-08-28 15:24:02 -07004350void cmd_oem_lock(const char *arg, void *data, unsigned sz)
4351{
lijuang4ece1e72015-08-14 21:02:36 +08004352 set_device_unlock(UNLOCK, FALSE);
Shashank Mittal162244e2011-08-08 19:01:25 -07004353}
4354
Shashank Mittala0032282011-08-26 14:50:11 -07004355void cmd_oem_devinfo(const char *arg, void *data, unsigned sz)
4356{
lijuang511a2b52015-08-14 20:50:51 +08004357 char response[MAX_RSP_SIZE];
Ameya Thakur11cf1a62013-08-05 12:44:48 -07004358 snprintf(response, sizeof(response), "\tDevice tampered: %s", (device.is_tampered ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07004359 fastboot_info(response);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07004360 snprintf(response, sizeof(response), "\tDevice unlocked: %s", (device.is_unlocked ? "true" : "false"));
4361 fastboot_info(response);
Monika Singh292b3e92018-03-17 22:40:23 +05304362#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover912eaa62017-10-26 12:08:53 +05304363 if (VB_M <= target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +05304364 {
4365 snprintf(response, sizeof(response), "\tDevice critical unlocked: %s",
4366 (device.is_unlock_critical ? "true" : "false"));
4367 fastboot_info(response);
4368 }
Parth Dixitddbc7352015-10-18 03:13:31 +05304369#endif
Ameya Thakur11cf1a62013-08-05 12:44:48 -07004370 snprintf(response, sizeof(response), "\tCharger screen enabled: %s", (device.charger_screen_enabled ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07004371 fastboot_info(response);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05304372 snprintf(response, sizeof(response), "\tDisplay panel: %s", (device.display_panel));
4373 fastboot_info(response);
Shashank Mittala0032282011-08-26 14:50:11 -07004374 fastboot_okay("");
4375}
4376
lijuang511a2b52015-08-14 20:50:51 +08004377void cmd_flashing_get_unlock_ability(const char *arg, void *data, unsigned sz)
4378{
4379 char response[MAX_RSP_SIZE];
4380 snprintf(response, sizeof(response), "\tget_unlock_ability: %d", is_allow_unlock);
4381 fastboot_info(response);
4382 fastboot_okay("");
4383}
4384
4385void cmd_flashing_lock_critical(const char *arg, void *data, unsigned sz)
4386{
lijuang4ece1e72015-08-14 21:02:36 +08004387 set_device_unlock(UNLOCK_CRITICAL, FALSE);
lijuang511a2b52015-08-14 20:50:51 +08004388}
4389
4390void cmd_flashing_unlock_critical(const char *arg, void *data, unsigned sz)
4391{
lijuang4ece1e72015-08-14 21:02:36 +08004392 set_device_unlock(UNLOCK_CRITICAL, TRUE);
lijuang511a2b52015-08-14 20:50:51 +08004393}
4394
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07004395void cmd_preflash(const char *arg, void *data, unsigned sz)
4396{
4397 fastboot_okay("");
4398}
4399
Mao Flynn7b379f32015-04-20 00:28:30 +08004400static uint8_t logo_header[LOGO_IMG_HEADER_SIZE];
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304401
Mao Flynn7b379f32015-04-20 00:28:30 +08004402int splash_screen_check_header(logo_img_header *header)
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304403{
Mao Flynn7b379f32015-04-20 00:28:30 +08004404 if (memcmp(header->magic, LOGO_IMG_MAGIC, 8))
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304405 return -1;
Mao Flynn7b379f32015-04-20 00:28:30 +08004406 if (header->width == 0 || header->height == 0)
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304407 return -1;
4408 return 0;
4409}
4410
Mao Flynn7b379f32015-04-20 00:28:30 +08004411int splash_screen_flash()
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07004412{
4413 struct ptentry *ptn;
4414 struct ptable *ptable;
Mao Flynn7b379f32015-04-20 00:28:30 +08004415 struct logo_img_header *header;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07004416 struct fbcon_config *fb_display = NULL;
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08004417
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304418 ptable = flash_get_ptable();
4419 if (ptable == NULL) {
Mao Flynn7b379f32015-04-20 00:28:30 +08004420 dprintf(CRITICAL, "ERROR: Partition table not found\n");
4421 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304422 }
Mao Flynn7b379f32015-04-20 00:28:30 +08004423
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304424 ptn = ptable_find(ptable, "splash");
4425 if (ptn == NULL) {
4426 dprintf(CRITICAL, "ERROR: splash Partition not found\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08004427 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304428 }
Mao Flynn7b379f32015-04-20 00:28:30 +08004429 if (flash_read(ptn, 0, (void *)logo_header, LOGO_IMG_HEADER_SIZE)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304430 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08004431 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304432 }
4433
Mao Flynn7b379f32015-04-20 00:28:30 +08004434 header = (struct logo_img_header *)logo_header;
4435 if (splash_screen_check_header(header)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304436 dprintf(CRITICAL, "ERROR: Boot image header invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08004437 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304438 }
4439
4440 fb_display = fbcon_display();
4441 if (fb_display) {
Mao Flynn7b379f32015-04-20 00:28:30 +08004442 if (header->type && (header->blocks != 0)) { // RLE24 compressed data
4443 uint8_t *base = (uint8_t *) fb_display->base + LOGO_IMG_OFFSET;
4444
4445 /* if the logo is full-screen size, remove "fbcon_clear()" */
4446 if ((header->width != fb_display->width)
4447 || (header->height != fb_display->height))
4448 fbcon_clear();
4449
4450 if (flash_read(ptn + LOGO_IMG_HEADER_SIZE, 0,
4451 (uint32_t *)base,
4452 (header->blocks * 512))) {
4453 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
4454 return -1;
4455 }
4456 fbcon_extract_to_screen(header, base);
4457 return 0;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07004458 }
Mao Flynn7b379f32015-04-20 00:28:30 +08004459
Veera Sundaram Sankaranc33416f2015-09-28 10:25:55 -07004460 if ((header->width > fb_display->width) || (header->height > fb_display->height)) {
4461 dprintf(CRITICAL, "Logo config greater than fb config. Fall back default logo\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08004462 return -1;
4463 }
4464
Vineet Bajaj99291ed2014-09-09 12:29:46 +05304465 uint8_t *base = (uint8_t *) fb_display->base;
Sachin Bhayare619e9e42017-05-15 13:10:31 +05304466 uint32_t fb_size = ROUNDUP(fb_display->width *
4467 fb_display->height *
4468 (fb_display->bpp / 8), 4096);
4469 uint32_t splash_size = ((((header->width * header->height *
4470 fb_display->bpp/8) + 511) >> 9) << 9);
4471
4472 if (splash_size > fb_size) {
4473 dprintf(CRITICAL, "ERROR: Splash image size invalid\n");
4474 return -1;
4475 }
4476
Mao Flynn7b379f32015-04-20 00:28:30 +08004477 if (flash_read(ptn + LOGO_IMG_HEADER_SIZE, 0,
4478 (uint32_t *)base,
4479 ((((header->width * header->height * fb_display->bpp/8) + 511) >> 9) << 9))) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304480 fbcon_clear();
Vineet Bajaj99291ed2014-09-09 12:29:46 +05304481 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08004482 return -1;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07004483 }
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304484 }
4485
Mao Flynn7b379f32015-04-20 00:28:30 +08004486 return 0;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304487}
4488
Mao Flynn7b379f32015-04-20 00:28:30 +08004489int splash_screen_mmc()
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304490{
4491 int index = INVALID_PTN;
4492 unsigned long long ptn = 0;
4493 struct fbcon_config *fb_display = NULL;
Mao Flynn7b379f32015-04-20 00:28:30 +08004494 struct logo_img_header *header;
Mao Flynn1893a7f2015-06-03 12:03:36 +08004495 uint32_t blocksize, realsize, readsize;
4496 uint8_t *base;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304497
4498 index = partition_get_index("splash");
4499 if (index == 0) {
4500 dprintf(CRITICAL, "ERROR: splash Partition table not found\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08004501 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304502 }
4503
4504 ptn = partition_get_offset(index);
4505 if (ptn == 0) {
4506 dprintf(CRITICAL, "ERROR: splash Partition invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08004507 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304508 }
4509
Mao Flynn1893a7f2015-06-03 12:03:36 +08004510 mmc_set_lun(partition_get_lun(index));
4511
4512 blocksize = mmc_get_device_blocksize();
4513 if (blocksize == 0) {
4514 dprintf(CRITICAL, "ERROR:splash Partition invalid blocksize\n");
4515 return -1;
4516 }
4517
4518 fb_display = fbcon_display();
Channagoud Kadabidaf10a62015-07-22 11:51:55 -07004519 if (!fb_display)
4520 {
4521 dprintf(CRITICAL, "ERROR: fb config is not allocated\n");
4522 return -1;
4523 }
4524
Mao Flynn1893a7f2015-06-03 12:03:36 +08004525 base = (uint8_t *) fb_display->base;
4526
Kishor PKee5c0a32018-03-06 16:49:46 +05304527 if (mmc_read(ptn + PLL_CODES_OFFSET, (uint32_t *)(base + LOGO_IMG_OFFSET), blocksize)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304528 dprintf(CRITICAL, "ERROR: Cannot read splash image header\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08004529 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304530 }
4531
Mao Flynn1893a7f2015-06-03 12:03:36 +08004532 header = (struct logo_img_header *)(base + LOGO_IMG_OFFSET);
Mao Flynn7b379f32015-04-20 00:28:30 +08004533 if (splash_screen_check_header(header)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304534 dprintf(CRITICAL, "ERROR: Splash image header invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08004535 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304536 }
4537
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304538 if (fb_display) {
Mao Flynn1893a7f2015-06-03 12:03:36 +08004539 if (header->type && (header->blocks != 0)) { /* 1 RLE24 compressed data */
4540 base += LOGO_IMG_OFFSET;
Mao Flynn7b379f32015-04-20 00:28:30 +08004541
Mao Flynn1893a7f2015-06-03 12:03:36 +08004542 realsize = header->blocks * 512;
4543 readsize = ROUNDUP((realsize + LOGO_IMG_HEADER_SIZE), blocksize) - blocksize;
4544
4545 /* if the logo is not full-screen size, clean screen */
Mao Flynn7b379f32015-04-20 00:28:30 +08004546 if ((header->width != fb_display->width)
4547 || (header->height != fb_display->height))
4548 fbcon_clear();
4549
Sachin Bhayare619e9e42017-05-15 13:10:31 +05304550 uint32_t fb_size = ROUNDUP(fb_display->width *
4551 fb_display->height *
4552 (fb_display->bpp / 8), 4096);
4553
4554 if (readsize > fb_size) {
4555 dprintf(CRITICAL, "ERROR: Splash image size invalid\n");
4556 return -1;
4557 }
4558
Kishor PKee5c0a32018-03-06 16:49:46 +05304559 if (mmc_read(ptn + PLL_CODES_OFFSET + blocksize, (uint32_t *)(base + blocksize), readsize)) {
Mao Flynn7b379f32015-04-20 00:28:30 +08004560 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
4561 return -1;
4562 }
Mao Flynn7b379f32015-04-20 00:28:30 +08004563
Mao Flynn1893a7f2015-06-03 12:03:36 +08004564 fbcon_extract_to_screen(header, (base + LOGO_IMG_HEADER_SIZE));
4565 } else { /* 2 Raw BGR data */
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304566
Veera Sundaram Sankaranc33416f2015-09-28 10:25:55 -07004567 if ((header->width > fb_display->width) || (header->height > fb_display->height)) {
4568 dprintf(CRITICAL, "Logo config greater than fb config. Fall back default logo\n");
Mao Flynn1893a7f2015-06-03 12:03:36 +08004569 return -1;
4570 }
4571
4572 realsize = header->width * header->height * fb_display->bpp / 8;
4573 readsize = ROUNDUP((realsize + LOGO_IMG_HEADER_SIZE), blocksize) - blocksize;
4574
4575 if (blocksize == LOGO_IMG_HEADER_SIZE) { /* read the content directly */
Kishor PKee5c0a32018-03-06 16:49:46 +05304576 if (mmc_read((ptn + PLL_CODES_OFFSET + LOGO_IMG_HEADER_SIZE), (uint32_t *)base, readsize)) {
Mao Flynn1893a7f2015-06-03 12:03:36 +08004577 fbcon_clear();
4578 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
4579 return -1;
4580 }
4581 } else {
Kishor PKee5c0a32018-03-06 16:49:46 +05304582 if (mmc_read(ptn + PLL_CODES_OFFSET + blocksize ,
Mao Flynn1893a7f2015-06-03 12:03:36 +08004583 (uint32_t *)(base + LOGO_IMG_OFFSET + blocksize), readsize)) {
4584 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
4585 return -1;
4586 }
4587 memmove(base, (base + LOGO_IMG_OFFSET + LOGO_IMG_HEADER_SIZE), realsize);
4588 }
4589 }
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304590 }
4591
Mao Flynn7b379f32015-04-20 00:28:30 +08004592 return 0;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304593}
4594
Mao Flynn7b379f32015-04-20 00:28:30 +08004595int fetch_image_from_partition()
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304596{
4597 if (target_is_emmc_boot()) {
4598 return splash_screen_mmc();
4599 } else {
4600 return splash_screen_flash();
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07004601 }
4602}
4603
Mayank Grover351a75e2017-05-30 20:06:08 +05304604void publish_getvar_multislot_vars()
4605{
4606 int i,count;
4607 static bool published = false;
4608 static char slot_count[MAX_RSP_SIZE];
4609 static struct ab_slot_info slot_info[AB_SUPPORTED_SLOTS];
4610 static char active_slot_suffix[MAX_RSP_SIZE];
4611 static char has_slot_pname[NUM_PARTITIONS][MAX_GET_VAR_NAME_SIZE];
4612 static char has_slot_reply[NUM_PARTITIONS][MAX_RSP_SIZE];
4613 const char *tmp;
4614 char tmpbuff[MAX_GET_VAR_NAME_SIZE];
Mayank Grover2bd84bb2017-06-20 11:06:07 +05304615 signed active_slt;
Mayank Grover351a75e2017-05-30 20:06:08 +05304616
4617 if (!published)
4618 {
4619 /* Update slot meta info */
4620 count = partition_fill_partition_meta(has_slot_pname, has_slot_reply,
4621 partition_get_partition_count());
4622 for(i=0; i<count; i++)
4623 {
4624 memset(tmpbuff, 0, MAX_GET_VAR_NAME_SIZE);
Mayank Grover2bd84bb2017-06-20 11:06:07 +05304625 snprintf(tmpbuff, MAX_GET_VAR_NAME_SIZE,"has-slot:%s",
4626 has_slot_pname[i]);
4627 strlcpy(has_slot_pname[i], tmpbuff, MAX_GET_VAR_NAME_SIZE);
Mayank Grover351a75e2017-05-30 20:06:08 +05304628 fastboot_publish(has_slot_pname[i], has_slot_reply[i]);
4629 }
4630
4631 for (i=0; i<AB_SUPPORTED_SLOTS; i++)
4632 {
4633 tmp = SUFFIX_SLOT(i);
Mayank Grover7b8a8f82017-10-27 13:07:59 +05304634 tmp++; // to remove "_" from slot_suffix.
Mayank Grover2bd84bb2017-06-20 11:06:07 +05304635 snprintf(slot_info[i].slot_is_unbootable, sizeof(slot_info[i].slot_is_unbootable),
4636 "slot-unbootable:%s", tmp);
4637 snprintf(slot_info[i].slot_is_active, sizeof(slot_info[i].slot_is_active),
4638 "slot-active:%s", tmp);
4639 snprintf(slot_info[i].slot_is_succesful, sizeof(slot_info[i].slot_is_succesful),
4640 "slot-success:%s", tmp);
4641 snprintf(slot_info[i].slot_retry_count, sizeof(slot_info[i].slot_retry_count),
4642 "slot-retry-count:%s", tmp);
Mayank Grover351a75e2017-05-30 20:06:08 +05304643 fastboot_publish(slot_info[i].slot_is_unbootable,
4644 slot_info[i].slot_is_unbootable_rsp);
4645 fastboot_publish(slot_info[i].slot_is_active,
4646 slot_info[i].slot_is_active_rsp);
4647 fastboot_publish(slot_info[i].slot_is_succesful,
4648 slot_info[i].slot_is_succesful_rsp);
4649 fastboot_publish(slot_info[i].slot_retry_count,
4650 slot_info[i].slot_retry_count_rsp);
4651 }
4652 fastboot_publish("current-slot", active_slot_suffix);
4653 snprintf(slot_count, sizeof(slot_count),"%d", AB_SUPPORTED_SLOTS);
4654 fastboot_publish("slot-count", slot_count);
4655 published = true;
4656 }
4657
Mayank Grover2bd84bb2017-06-20 11:06:07 +05304658 active_slt = partition_find_active_slot();
4659 if (active_slt != INVALID)
Mayank Grover7b8a8f82017-10-27 13:07:59 +05304660 {
4661 tmp = SUFFIX_SLOT(active_slt);
4662 tmp++; // to remove "_" from slot_suffix.
4663 snprintf(active_slot_suffix, sizeof(active_slot_suffix), "%s", tmp);
4664 }
Mayank Grover2bd84bb2017-06-20 11:06:07 +05304665 else
4666 strlcpy(active_slot_suffix, "INVALID", sizeof(active_slot_suffix));
4667
Mayank Grover351a75e2017-05-30 20:06:08 +05304668 /* Update partition meta information */
4669 partition_fill_slot_meta(slot_info);
4670 return;
4671}
4672
lijuang4ece1e72015-08-14 21:02:36 +08004673void get_product_name(unsigned char *buf)
4674{
4675 snprintf((char*)buf, MAX_RSP_SIZE, "%s", TARGET(BOARD));
4676 return;
4677}
4678
Mayank Grover7b8a8f82017-10-27 13:07:59 +05304679#if PRODUCT_IOT
4680void get_bootloader_version_iot(unsigned char *buf)
4681{
4682 if (buf != NULL)
4683 {
4684 strlcpy(buf, TARGET(BOARD), MAX_VERSION_LEN);
4685 strlcat(buf, "-", MAX_VERSION_LEN);
4686 strlcat(buf, PRODUCT_IOT_VERSION, MAX_VERSION_LEN);
4687 }
4688 return;
4689}
4690#endif
4691
lijuang4ece1e72015-08-14 21:02:36 +08004692void get_bootloader_version(unsigned char *buf)
4693{
4694 snprintf((char*)buf, MAX_RSP_SIZE, "%s", device.bootloader_version);
4695 return;
4696}
4697
4698void get_baseband_version(unsigned char *buf)
4699{
4700 snprintf((char*)buf, MAX_RSP_SIZE, "%s", device.radio_version);
4701 return;
4702}
4703
Monika Singh32a09f72018-03-14 13:08:29 +05304704bool is_device_locked_critical()
4705{
4706 return device.is_unlock_critical ? false:true;
4707}
4708
lijuang4ece1e72015-08-14 21:02:36 +08004709bool is_device_locked()
4710{
4711 return device.is_unlocked ? false:true;
4712}
4713
Monika Singh32a09f72018-03-14 13:08:29 +05304714bool is_verity_enforcing()
4715{
4716 return device.verity_mode ? true:false;
4717}
4718
Amol Jadi5edf3552013-07-23 14:15:34 -07004719/* register commands and variables for fastboot */
4720void aboot_fastboot_register_commands(void)
4721{
Sridhar Parasurame94e8152014-10-24 14:06:03 -07004722 int i;
lijuangf16461c2015-08-03 17:09:34 +08004723 char hw_platform_buf[MAX_RSP_SIZE];
Amol Jadi5edf3552013-07-23 14:15:34 -07004724
Sridhar Parasurame94e8152014-10-24 14:06:03 -07004725 struct fastboot_cmd_desc cmd_list[] = {
lijuang511a2b52015-08-14 20:50:51 +08004726 /* By default the enabled list is empty. */
4727 {"", NULL},
4728 /* move commands enclosed within the below ifndef to here
4729 * if they need to be enabled in user build.
4730 */
Sridhar Parasurame94e8152014-10-24 14:06:03 -07004731#ifndef DISABLE_FASTBOOT_CMDS
lijuang511a2b52015-08-14 20:50:51 +08004732 /* Register the following commands only for non-user builds */
4733 {"flash:", cmd_flash},
4734 {"erase:", cmd_erase},
4735 {"boot", cmd_boot},
4736 {"continue", cmd_continue},
4737 {"reboot", cmd_reboot},
4738 {"reboot-bootloader", cmd_reboot_bootloader},
4739 {"oem unlock", cmd_oem_unlock},
4740 {"oem unlock-go", cmd_oem_unlock_go},
4741 {"oem lock", cmd_oem_lock},
4742 {"flashing unlock", cmd_oem_unlock},
4743 {"flashing lock", cmd_oem_lock},
4744 {"flashing lock_critical", cmd_flashing_lock_critical},
4745 {"flashing unlock_critical", cmd_flashing_unlock_critical},
4746 {"flashing get_unlock_ability", cmd_flashing_get_unlock_ability},
4747 {"oem device-info", cmd_oem_devinfo},
4748 {"preflash", cmd_preflash},
4749 {"oem enable-charger-screen", cmd_oem_enable_charger_screen},
4750 {"oem disable-charger-screen", cmd_oem_disable_charger_screen},
lijuanga25d8bb2015-09-16 13:11:52 +08004751 {"oem off-mode-charge", cmd_oem_off_mode_charger},
lijuang511a2b52015-08-14 20:50:51 +08004752 {"oem select-display-panel", cmd_oem_select_display_panel},
Mayank Grover6ccc1c92017-07-04 17:36:46 +05304753 {"set_active",cmd_set_active},
Channagoud Kadabi736c4962015-08-21 11:56:52 -07004754#if UNITTEST_FW_SUPPORT
Channagoud Kadabid5ddf482015-09-28 10:31:35 -07004755 {"oem run-tests", cmd_oem_runtests},
Channagoud Kadabi736c4962015-08-21 11:56:52 -07004756#endif
Sridhar Parasurame94e8152014-10-24 14:06:03 -07004757#endif
lijuang511a2b52015-08-14 20:50:51 +08004758 };
Sridhar Parasurame94e8152014-10-24 14:06:03 -07004759
4760 int fastboot_cmds_count = sizeof(cmd_list)/sizeof(cmd_list[0]);
4761 for (i = 1; i < fastboot_cmds_count; i++)
4762 fastboot_register(cmd_list[i].name,cmd_list[i].cb);
4763
Amol Jadi5edf3552013-07-23 14:15:34 -07004764 /* publish variables and their values */
4765 fastboot_publish("product", TARGET(BOARD));
4766 fastboot_publish("kernel", "lk");
4767 fastboot_publish("serialno", sn_buf);
4768
Mayank Grovera64dfbe2018-05-17 14:06:34 +05304769 /*publish hw-revision major(upper 16 bits) and minor(lower 16 bits)*/
4770 snprintf(soc_version_str, MAX_RSP_SIZE, "%x", board_soc_version());
4771 fastboot_publish("hw-revision", soc_version_str);
4772
Amol Jadi5edf3552013-07-23 14:15:34 -07004773 /*
4774 * partition info is supported only for emmc partitions
4775 * Calling this for NAND prints some error messages which
4776 * is harmless but misleading. Avoid calling this for NAND
4777 * devices.
4778 */
4779 if (target_is_emmc_boot())
Mayank Grover7b8a8f82017-10-27 13:07:59 +05304780 publish_getvar_partition_info(part_info, partition_get_partition_count());
Amol Jadi5edf3552013-07-23 14:15:34 -07004781
Mayank Grover351a75e2017-05-30 20:06:08 +05304782 if (partition_multislot_is_supported())
4783 publish_getvar_multislot_vars();
4784
Amol Jadi5edf3552013-07-23 14:15:34 -07004785 /* Max download size supported */
Ameya Thakur11cf1a62013-08-05 12:44:48 -07004786 snprintf(max_download_size, MAX_RSP_SIZE, "\t0x%x",
4787 target_get_max_flash_size());
Amol Jadi5edf3552013-07-23 14:15:34 -07004788 fastboot_publish("max-download-size", (const char *) max_download_size);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07004789 /* Is the charger screen check enabled */
4790 snprintf(charger_screen_enabled, MAX_RSP_SIZE, "%d",
4791 device.charger_screen_enabled);
4792 fastboot_publish("charger-screen-enabled",
4793 (const char *) charger_screen_enabled);
lijuanga25d8bb2015-09-16 13:11:52 +08004794 fastboot_publish("off-mode-charge", (const char *) charger_screen_enabled);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05304795 snprintf(panel_display_mode, MAX_RSP_SIZE, "%s",
4796 device.display_panel);
4797 fastboot_publish("display-panel",
4798 (const char *) panel_display_mode);
Mukesh Ojhabb6e7f32018-03-02 15:04:06 +05304799
4800 if (target_is_emmc_boot())
4801 {
4802 mmc_blocksize = mmc_get_device_blocksize();
4803 }
4804 else
4805 {
4806 mmc_blocksize = flash_block_size();
4807 }
4808 snprintf(block_size_string, MAX_RSP_SIZE, "0x%x", mmc_blocksize);
4809 fastboot_publish("erase-block-size", (const char *) block_size_string);
4810 fastboot_publish("logical-block-size", (const char *) block_size_string);
Mayank Grover7b8a8f82017-10-27 13:07:59 +05304811#if PRODUCT_IOT
4812 get_bootloader_version_iot(&bootloader_version_string);
4813 fastboot_publish("version-bootloader", (const char *) bootloader_version_string);
4814
4815 /* Version baseband is n/a for apq iot devices */
4816 fastboot_publish("version-baseband", "N/A");
Mayank Grover7b8a8f82017-10-27 13:07:59 +05304817#else
Channagoud Kadabiaafbbfc2015-03-04 17:52:56 -08004818 fastboot_publish("version-bootloader", (const char *) device.bootloader_version);
4819 fastboot_publish("version-baseband", (const char *) device.radio_version);
Mayank Grover7b8a8f82017-10-27 13:07:59 +05304820#endif
lijuangf16461c2015-08-03 17:09:34 +08004821 fastboot_publish("secure", is_secure_boot_enable()? "yes":"no");
Mayank Grover7b8a8f82017-10-27 13:07:59 +05304822 fastboot_publish("unlocked", device.is_unlocked ? "yes":"no");
lijuangf16461c2015-08-03 17:09:34 +08004823 smem_get_hw_platform_name((unsigned char *) hw_platform_buf, sizeof(hw_platform_buf));
4824 snprintf(get_variant, MAX_RSP_SIZE, "%s %s", hw_platform_buf,
4825 target_is_emmc_boot()? "eMMC":"UFS");
4826 fastboot_publish("variant", (const char *) get_variant);
lijuang65c5a822015-08-29 16:35:36 +08004827#if CHECK_BAT_VOLTAGE
lijuang102dfa92015-10-09 18:31:03 +08004828 update_battery_status();
lijuang65c5a822015-08-29 16:35:36 +08004829 fastboot_publish("battery-voltage", (const char *) battery_voltage);
lijuang102dfa92015-10-09 18:31:03 +08004830 fastboot_publish("battery-soc-ok", (const char *) battery_soc_ok);
lijuang65c5a822015-08-29 16:35:36 +08004831#endif
Amol Jadi5edf3552013-07-23 14:15:34 -07004832}
4833
Brian Swetland9c4c0752009-01-25 16:23:50 -08004834void aboot_init(const struct app_descriptor *app)
4835{
Shashank Mittal4f99a882010-02-01 13:58:50 -08004836 unsigned reboot_mode = 0;
Mayank Grover351a75e2017-05-30 20:06:08 +05304837 int boot_err_type = 0;
4838 int boot_slot = INVALID;
Chandan Uddarajubedca152010-06-02 23:05:15 -07004839
lijuang39831732016-01-08 17:49:02 +08004840 /* Initialise wdog to catch early lk crashes */
4841#if WDOG_SUPPORT
4842 msm_wdog_init();
4843#endif
4844
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07004845 /* Setup page size information for nv storage */
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004846 if (target_is_emmc_boot())
4847 {
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07004848 page_size = mmc_page_size();
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004849 page_mask = page_size - 1;
Gaurav Nebhwani4d2389c2016-03-17 21:10:05 +05304850 mmc_blocksize = mmc_get_device_blocksize();
4851 mmc_blocksize_mask = mmc_blocksize - 1;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004852 }
4853 else
4854 {
4855 page_size = flash_page_size();
4856 page_mask = page_size - 1;
4857 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07004858 ASSERT((MEMBASE + MEMSIZE) > MEMBASE);
4859
Ameya Thakur11cf1a62013-08-05 12:44:48 -07004860 read_device_info(&device);
vijay kumarc65876c2015-04-24 13:29:16 +05304861 read_allow_oem_unlock(&device);
Shashank Mittal162244e2011-08-08 19:01:25 -07004862
Mayank Grover351a75e2017-05-30 20:06:08 +05304863 /* Detect multi-slot support */
4864 if (partition_multislot_is_supported())
4865 {
4866 boot_slot = partition_find_active_slot();
4867 if (boot_slot == INVALID)
4868 {
4869 boot_into_fastboot = true;
4870 dprintf(INFO, "Active Slot: (INVALID)\n");
4871 }
4872 else
4873 {
4874 /* Setting the state of system to boot active slot */
4875 partition_mark_active_slot(boot_slot);
4876 dprintf(INFO, "Active Slot: (%s)\n", SUFFIX_SLOT(boot_slot));
4877 }
4878 }
4879
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08004880 /* Display splash screen if enabled */
4881#if DISPLAY_SPLASH_SCREEN
lijuang99c02d82015-02-13 19:04:34 +08004882#if NO_ALARM_DISPLAY
4883 if (!check_alarm_boot()) {
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08004884#endif
lijuang99c02d82015-02-13 19:04:34 +08004885 dprintf(SPEW, "Display Init: Start\n");
Ajay Singh Parmara7865a82015-04-16 21:27:52 -07004886#if DISPLAY_HDMI_PRIMARY
4887 if (!strlen(device.display_panel))
4888 strlcpy(device.display_panel, DISPLAY_PANEL_HDMI,
4889 sizeof(device.display_panel));
4890#endif
Channagoud Kadabi90869ce2015-04-27 11:15:14 -07004891#if ENABLE_WBC
Channagoud Kadabid801ac72015-08-26 11:21:51 -07004892 /* Wait if the display shutdown is in progress */
4893 while(pm_app_display_shutdown_in_prgs());
Channagoud Kadabi90869ce2015-04-27 11:15:14 -07004894 if (!pm_appsbl_display_init_done())
4895 target_display_init(device.display_panel);
4896 else
4897 display_image_on_screen();
4898#else
lijuang99c02d82015-02-13 19:04:34 +08004899 target_display_init(device.display_panel);
Channagoud Kadabi90869ce2015-04-27 11:15:14 -07004900#endif
lijuang99c02d82015-02-13 19:04:34 +08004901 dprintf(SPEW, "Display Init: Done\n");
4902#if NO_ALARM_DISPLAY
4903 }
4904#endif
4905#endif
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08004906
Greg Griscod6250552011-06-29 14:40:23 -07004907 target_serialno((unsigned char *) sn_buf);
Ajay Dudanib06c05f2011-05-12 14:46:10 -07004908 dprintf(SPEW,"serial number: %s\n",sn_buf);
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -08004909
Dhaval Patel223ec952013-07-18 14:49:44 -07004910 memset(display_panel_buf, '\0', MAX_PANEL_BUF_SIZE);
4911
Matthew Qindefd5562014-07-11 18:02:40 +08004912 /*
4913 * Check power off reason if user force reset,
4914 * if yes phone will do normal boot.
4915 */
4916 if (is_user_force_reset())
4917 goto normal_boot;
4918
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07004919 /* Check if we should do something other than booting up */
Ameya Thakur0b9c2442013-05-10 13:22:56 -07004920 if (keys_get_state(KEY_VOLUMEUP) && keys_get_state(KEY_VOLUMEDOWN))
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07004921 {
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03004922 dprintf(ALWAYS,"dload mode key sequence detected\n");
lijuang395b5e62015-11-19 17:39:44 +08004923 reboot_device(EMERGENCY_DLOAD);
4924 dprintf(CRITICAL,"Failed to reboot into dload mode\n");
4925
Ameya Thakur0b9c2442013-05-10 13:22:56 -07004926 boot_into_fastboot = true;
4927 }
4928 if (!boot_into_fastboot)
4929 {
4930 if (keys_get_state(KEY_HOME) || keys_get_state(KEY_VOLUMEUP))
4931 boot_into_recovery = 1;
4932 if (!boot_into_recovery &&
4933 (keys_get_state(KEY_BACK) || keys_get_state(KEY_VOLUMEDOWN)))
Pavel Nedev5d91d412013-04-29 11:34:24 +03004934 boot_into_fastboot = true;
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07004935 }
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004936 #if NO_KEYPAD_DRIVER
Kinson Chik0b1c8162011-08-31 16:31:57 -07004937 if (fastboot_trigger())
Pavel Nedev5d91d412013-04-29 11:34:24 +03004938 boot_into_fastboot = true;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004939 #endif
Chandan Uddarajubedca152010-06-02 23:05:15 -07004940
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07004941#if USE_PON_REBOOT_REG
4942 reboot_mode = check_hard_reboot_mode();
4943#else
Ajay Dudani77421292010-10-27 19:34:06 -07004944 reboot_mode = check_reboot_mode();
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07004945#endif
4946 if (reboot_mode == RECOVERY_MODE)
4947 {
Ajay Dudani77421292010-10-27 19:34:06 -07004948 boot_into_recovery = 1;
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07004949 }
4950 else if(reboot_mode == FASTBOOT_MODE)
4951 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03004952 boot_into_fastboot = true;
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07004953 }
4954 else if(reboot_mode == ALARM_BOOT)
4955 {
Matthew Qind886f3c2014-01-17 16:52:01 +08004956 boot_reason_alarm = true;
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07004957 }
Parth Dixit207573a2015-10-27 17:26:21 +05304958#if VERIFIED_BOOT
Mayank Grover912eaa62017-10-26 12:08:53 +05304959 else if (VB_M <= target_get_vb_version())
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07004960 {
Mayank Grover889be1b2017-09-12 20:12:23 +05304961 if (reboot_mode == DM_VERITY_ENFORCING)
4962 {
4963 device.verity_mode = 1;
4964 write_device_info(&device);
4965 }
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07004966#if ENABLE_VB_ATTEST
Mayank Grover889be1b2017-09-12 20:12:23 +05304967 else if (reboot_mode == DM_VERITY_EIO)
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07004968#else
Mayank Grover889be1b2017-09-12 20:12:23 +05304969 else if (reboot_mode == DM_VERITY_LOGGING)
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07004970#endif
Mayank Grover889be1b2017-09-12 20:12:23 +05304971 {
4972 device.verity_mode = 0;
4973 write_device_info(&device);
4974 }
4975 else if (reboot_mode == DM_VERITY_KEYSCLEAR)
4976 {
4977 if(send_delete_keys_to_tz())
4978 ASSERT(0);
4979 }
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07004980 }
Parth Dixitddbc7352015-10-18 03:13:31 +05304981#endif
Ajay Dudani77421292010-10-27 19:34:06 -07004982
Matthew Qindefd5562014-07-11 18:02:40 +08004983normal_boot:
Pavel Nedev5d91d412013-04-29 11:34:24 +03004984 if (!boot_into_fastboot)
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004985 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03004986 if (target_is_emmc_boot())
Shashank Mittala0032282011-08-26 14:50:11 -07004987 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03004988 if(emmc_recovery_init())
4989 dprintf(ALWAYS,"error in emmc_recovery_init\n");
4990 if(target_use_signed_kernel())
Shashank Mittala0032282011-08-26 14:50:11 -07004991 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03004992 if((device.is_unlocked) || (device.is_tampered))
4993 {
4994 #ifdef TZ_TAMPER_FUSE
Monika Singh98257462018-06-06 11:28:49 +05304995 set_tamper_fuse_cmd(HLOS_IMG_TAMPER_FUSE);
Pavel Nedev5d91d412013-04-29 11:34:24 +03004996 #endif
4997 #if USE_PCOM_SECBOOT
4998 set_tamper_flag(device.is_tampered);
4999 #endif
5000 }
Shashank Mittala0032282011-08-26 14:50:11 -07005001 }
Amit Blay6281ebc2015-01-11 14:44:08 +02005002
Mayank Grover351a75e2017-05-30 20:06:08 +05305003retry_boot:
5004 /* Trying to boot active partition */
5005 if (partition_multislot_is_supported())
5006 {
5007 boot_slot = partition_find_boot_slot();
5008 partition_mark_active_slot(boot_slot);
5009 if (boot_slot == INVALID)
5010 goto fastboot;
5011 }
5012
5013 boot_err_type = boot_linux_from_mmc();
5014 switch (boot_err_type)
5015 {
5016 case ERR_INVALID_PAGE_SIZE:
5017 case ERR_DT_PARSE:
5018 case ERR_ABOOT_ADDR_OVERLAP:
Mayank Grover10cabbe2017-10-30 19:11:05 +05305019 case ERR_INVALID_BOOT_MAGIC:
Mayank Grover351a75e2017-05-30 20:06:08 +05305020 if(partition_multislot_is_supported())
Mayank Grover10cabbe2017-10-30 19:11:05 +05305021 {
5022 /*
5023 * Deactivate current slot, as it failed to
5024 * boot, and retry next slot.
5025 */
5026 partition_deactivate_slot(boot_slot);
Mayank Grover351a75e2017-05-30 20:06:08 +05305027 goto retry_boot;
Mayank Grover10cabbe2017-10-30 19:11:05 +05305028 }
Mayank Grover351a75e2017-05-30 20:06:08 +05305029 else
5030 break;
Mayank Grover351a75e2017-05-30 20:06:08 +05305031 default:
5032 break;
5033 /* going to fastboot menu */
5034 }
Shashank Mittala0032282011-08-26 14:50:11 -07005035 }
Pavel Nedev5d91d412013-04-29 11:34:24 +03005036 else
5037 {
5038 recovery_init();
5039 #if USE_PCOM_SECBOOT
5040 if((device.is_unlocked) || (device.is_tampered))
5041 set_tamper_flag(device.is_tampered);
5042 #endif
5043 boot_linux_from_flash();
5044 }
5045 dprintf(CRITICAL, "ERROR: Could not do normal boot. Reverting "
5046 "to fastboot mode.\n");
Shashank Mittald8c42bf2010-06-09 15:44:28 -07005047 }
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07005048
Mayank Grover351a75e2017-05-30 20:06:08 +05305049fastboot:
Amol Jadi5edf3552013-07-23 14:15:34 -07005050 /* We are here means regular boot did not happen. Start fastboot. */
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07005051
Amol Jadi5edf3552013-07-23 14:15:34 -07005052 /* register aboot specific fastboot commands */
5053 aboot_fastboot_register_commands();
Amol Jadi57abe4c2011-05-24 15:47:27 -07005054
Amol Jadi5edf3552013-07-23 14:15:34 -07005055 /* dump partition table for debug info */
Kinson Chikf1a43512011-07-14 11:28:39 -07005056 partition_dump();
Amol Jadi5edf3552013-07-23 14:15:34 -07005057
5058 /* initialize and start fastboot */
5059 fastboot_init(target_get_scratch_address(), target_get_max_flash_size());
lijuang4ece1e72015-08-14 21:02:36 +08005060#if FBCON_DISPLAY_MSG
lijuang9a7d3b92015-11-30 14:41:24 +08005061 display_fastboot_menu();
lijuang4ece1e72015-08-14 21:02:36 +08005062#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08005063}
5064
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07005065uint32_t get_page_size()
5066{
5067 return page_size;
5068}
5069
Amir Samuelov57a6fa22013-06-05 16:36:43 +03005070/*
5071 * Calculated and save hash (SHA256) for non-signed boot image.
5072 *
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07005073 * @param image_addr - Boot image address
5074 * @param image_size - Size of the boot image
Amir Samuelov57a6fa22013-06-05 16:36:43 +03005075 *
5076 * @return int - 0 on success, negative value on failure.
5077 */
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07005078static int aboot_save_boot_hash_mmc(uint32_t image_addr, uint32_t image_size)
Amir Samuelov57a6fa22013-06-05 16:36:43 +03005079{
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07005080 unsigned int digest[8];
5081#if IMAGE_VERIF_ALGO_SHA1
5082 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA1;
5083#else
5084 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA256;
5085#endif
Amir Samuelov57a6fa22013-06-05 16:36:43 +03005086
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07005087 target_crypto_init_params();
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -08005088 hash_find((unsigned char *) image_addr, image_size, (unsigned char *)&digest, auth_algo);
Amir Samuelov57a6fa22013-06-05 16:36:43 +03005089
5090 save_kernel_hash_cmd(digest);
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07005091 dprintf(INFO, "aboot_save_boot_hash_mmc: imagesize_actual size %d bytes.\n", (int) image_size);
Amir Samuelov57a6fa22013-06-05 16:36:43 +03005092
5093 return 0;
5094}
5095
Mayank Grover351a75e2017-05-30 20:06:08 +05305096
Brian Swetland9c4c0752009-01-25 16:23:50 -08005097APP_START(aboot)
5098 .init = aboot_init,
5099APP_END