blob: edcd171bbb12e42b3a03cfae769bc666cbb9cb9a [file] [log] [blame]
Brian Swetland9c4c0752009-01-25 16:23:50 -08001/*
2 * Copyright (c) 2009, Google Inc.
3 * All rights reserved.
4 *
Mayank Grover98c4c742019-04-25 17:21:37 +05305 * Copyright (c) 2009-2019, 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
Umang Chheda4c140de2019-12-19 14:30:38 +053070#include <pm_smbchg_common.h>
Channagoud Kadabi90869ce2015-04-27 11:15:14 -070071#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;
tracychuid184b912020-06-05 17:31:38 +0800106/*[20200605][TracyChui] Implement get Serial Number start*/
107#if defined(ENABLE_PRODINFO_ACCESS)
108void *prodinfo_buf;
109#endif
110/*[20200605][TracyChui] Implement get Serial Number end*/
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700111void write_device_info_mmc(device_info *dev);
112void write_device_info_flash(device_info *dev);
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -0700113static int aboot_save_boot_hash_mmc(uint32_t image_addr, uint32_t image_size);
Channagoud Kadabiad259832015-05-29 11:14:17 -0700114static int aboot_frp_unlock(char *pname, void *data, unsigned sz);
Kishor PK38ed93d2017-04-25 14:19:26 +0530115static inline uint64_t validate_partition_size();
Parth Dixit54ac3bb2017-03-07 15:52:48 +0530116bool pwr_key_is_pressed = false;
Hareesh Gauthamabf1e1e2017-04-12 18:09:24 +0530117static bool is_systemd_present=false;
Mayank Grover351a75e2017-05-30 20:06:08 +0530118static void publish_getvar_multislot_vars();
Sridhar Parasurame94e8152014-10-24 14:06:03 -0700119/* fastboot command function pointer */
120typedef void (*fastboot_cmd_fn) (const char *, void *, unsigned);
Monika Singh292b3e92018-03-17 22:40:23 +0530121bool get_perm_attr_status();
tracychuid184b912020-06-05 17:31:38 +0800122/*[20200605][TracyChui] Implement get Serial Number start*/
123#if defined(ENABLE_PRODINFO_ACCESS)
124void write_prod_info(prod_info *dev);
125#endif
126/*[20200605][TracyChui] Implement get Serial Number end */
Sridhar Parasurame94e8152014-10-24 14:06:03 -0700127
128struct fastboot_cmd_desc {
129 char * name;
130 fastboot_cmd_fn cb;
131};
132
Subbaraman Narayanamurthyeb92bcc2010-07-20 14:32:46 -0700133#define EXPAND(NAME) #NAME
134#define TARGET(NAME) EXPAND(NAME)
Brian Swetland2defe162009-08-18 14:35:59 -0700135
Ajay Singh Parmara7865a82015-04-16 21:27:52 -0700136#define DISPLAY_PANEL_HDMI "hdmi"
137
Ajay Dudanicd01f9b2010-02-23 21:13:04 -0800138#ifdef MEMBASE
139#define EMMC_BOOT_IMG_HEADER_ADDR (0xFF000+(MEMBASE))
140#else
David Ng183a7422009-12-07 14:55:21 -0800141#define EMMC_BOOT_IMG_HEADER_ADDR 0xFF000
Ajay Dudanicd01f9b2010-02-23 21:13:04 -0800142#endif
143
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700144#ifndef MEMSIZE
145#define MEMSIZE 1024*1024
146#endif
147
148#define MAX_TAGS_SIZE 1024
Kishor PKee5c0a32018-03-06 16:49:46 +0530149#define PLL_CODES_OFFSET 4096
Kun Liang2f1601a2013-08-12 16:29:54 +0800150/* make 4096 as default size to ensure EFS,EXT4's erasing */
151#define DEFAULT_ERASE_SIZE 4096
Ujwal Patelc0b0a252015-08-16 14:05:35 -0700152#define MAX_PANEL_BUF_SIZE 196
Vijay Kumar Pendoti0b21f462016-05-02 17:09:18 +0530153#define FOOTER_SIZE 16384
Kun Liang2f1601a2013-08-12 16:29:54 +0800154
Dhaval Patelf83d73b2014-06-23 16:24:37 -0700155#define DISPLAY_DEFAULT_PREFIX "mdss_mdp"
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700156#define BOOT_DEV_MAX_LEN 64
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -0800157
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800158#define IS_ARM64(ptr) (ptr->magic_64 == KERNEL64_HDR_MAGIC) ? true : false
159
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -0700160#define ADD_OF(a, b) (UINT_MAX - b > a) ? (a + b) : UINT_MAX
161
Ameya Thakur10a33452016-06-13 14:24:26 -0700162//Size of the header that is used in case the boot image has
163//a uncompressed kernel + appended dtb
164#define PATCHED_KERNEL_HEADER_SIZE 20
165
166//String used to determine if the boot image has
167//a uncompressed kernel + appended dtb
168#define PATCHED_KERNEL_MAGIC "UNCOMPRESSED_IMG"
169
Sridhar Parasuram7bd4aaf2015-02-12 11:14:38 -0800170#if USE_BOOTDEV_CMDLINE
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700171static const char *emmc_cmdline = " androidboot.bootdevice=";
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700172#else
David Ng183a7422009-12-07 14:55:21 -0800173static const char *emmc_cmdline = " androidboot.emmc=true";
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700174#endif
Mayank Groverb716edf2019-05-08 16:06:55 +0530175static const char *dynamic_bootdev_cmdline =
176 " androidboot.boot_devices=soc/";
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800177static const char *usb_sn_cmdline = " androidboot.serialno=";
Pavel Nedev328ac822013-04-05 15:25:11 +0300178static const char *androidboot_mode = " androidboot.mode=";
Hareesh Gauthamabf1e1e2017-04-12 18:09:24 +0530179
180static const char *systemd_ffbm_mode = " systemd.unit=ffbm.target";
Matthew Qind886f3c2014-01-17 16:52:01 +0800181static const char *alarmboot_cmdline = " androidboot.alarmboot=true";
Pavel Nedev898298c2013-02-27 12:36:09 -0800182static const char *loglevel = " quiet";
Ajay Dudanica3a33c2011-11-18 08:31:40 -0800183static const char *battchg_pause = " androidboot.mode=charger";
tracychuid184b912020-06-05 17:31:38 +0800184/*[20200605][TracyChui] Implement get Serial Number start */
185#if defined(ENABLE_PRODINFO_ACCESS)
186static const char *cust_sn_cmdline = " androidboot.customer_serialno=";
187static const char *factory_sn_cmdline = " androidboot.factory_serialno=";
188static const char *UsbAdbEnable = " androidboot.adb_enable=1";
189#endif
190/*[20200605][TracyChui] Implement get Serial Number end*/
Shashank Mittalcd98d472011-08-02 14:29:24 -0700191static const char *auth_kernel = " androidboot.authorized_kernel=true";
Pavel Nedev5614d222013-06-17 18:01:02 +0300192static const char *secondary_gpt_enable = " gpt";
Monika Singh292b3e92018-03-17 22:40:23 +0530193#ifdef MDTP_SUPPORT
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200194static const char *mdtp_activated_flag = " mdtp";
Monika Singh292b3e92018-03-17 22:40:23 +0530195#endif
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800196static const char *baseband_apq = " androidboot.baseband=apq";
197static const char *baseband_msm = " androidboot.baseband=msm";
198static const char *baseband_csfb = " androidboot.baseband=csfb";
199static const char *baseband_svlte2a = " androidboot.baseband=svlte2a";
Ajay Dudani403bc492011-09-30 16:17:21 -0700200static const char *baseband_mdm = " androidboot.baseband=mdm";
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800201static const char *baseband_mdm2 = " androidboot.baseband=mdm2";
Amol Jadi5c61a952012-05-04 17:05:35 -0700202static const char *baseband_sglte = " androidboot.baseband=sglte";
Amol Jadi2a15a272013-01-22 12:03:36 -0800203static const char *baseband_dsda = " androidboot.baseband=dsda";
204static const char *baseband_dsda2 = " androidboot.baseband=dsda2";
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800205static const char *baseband_sglte2 = " androidboot.baseband=sglte2";
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800206static const char *warmboot_cmdline = " qpnp-power-on.warm_boot=1";
Vijay Kumar Pendotib228cfc2016-06-13 20:15:23 +0530207static const char *baseband_apq_nowgr = " androidboot.baseband=baseband_apq_nowgr";
Mayank Grover351a75e2017-05-30 20:06:08 +0530208static const char *androidboot_slot_suffix = " androidboot.slot_suffix=";
209static const char *skip_ramfs = " skip_initramfs";
Mayank Grover466d6562018-05-10 14:52:20 +0530210
211#if HIBERNATION_SUPPORT
212static const char *resume = " resume=/dev/mmcblk0p";
213#endif
214
Sourabh Banerjee6c7153c2018-03-20 01:21:57 +0530215#ifdef INIT_BIN_LE
216static const char *sys_path_cmdline = " rootwait ro init="INIT_BIN_LE;
217#else
Mayank Grover3804be72017-06-22 11:59:23 +0530218static const char *sys_path_cmdline = " rootwait ro init=/init";
Sourabh Banerjee6c7153c2018-03-20 01:21:57 +0530219#endif
Sourabh Banerjee386b1322018-02-27 09:37:28 +0530220
221#if VERITY_LE
222static const char *verity_dev = " root=/dev/dm-0";
223static const char *verity_system_part = " dm=\"system";
224static const char *verity_params = " none ro,0 1 android-verity /dev/mmcblk0p";
225#else
Mayank Grover5384ed82018-05-09 12:09:24 +0530226static const char *sys_path = " root=/dev/mmcblk0p";
lijuang83ef4b22018-08-23 11:01:55 +0800227
228#define MAX_DTBO_IDX_STR 64
229static const char *android_boot_dtbo_idx = " androidboot.dtbo_idx=";
Parth Dixit1375d9e2019-07-08 20:56:13 +0530230
231#define MAX_DTB_IDX_STR MAX_DTBO_IDX_STR
232static const char *android_boot_dtb_idx = " androidboot.dtb_idx=";
Sourabh Banerjee386b1322018-02-27 09:37:28 +0530233#endif
Ajay Dudanid04110c2011-01-17 23:55:07 -0800234
michaellincb058f12020-04-24 15:05:10 +0800235//<20200424-michaellin, Add PCBA stage to system properties
236#include <platform/gpio.h>
237static const char *PCBA_STAGE_0 = " androidboot.pcbastage=EP0";
238static const char *PCBA_STAGE_1 = " androidboot.pcbastage=EP1";
239static const char *PCBA_STAGE_2 = " androidboot.pcbastage=EP2";
240static const char *PCBA_STAGE_3 = " androidboot.pcbastage=FP";
241static const char *PCBA_STAGE_4 = " androidboot.pcbastage=MP";
michaellin52b647a2020-05-05 14:58:47 +0800242static const char *PCBA_STAGE_5 = " androidboot.pcbastage=MP-8903MB_001";
michaellincb058f12020-04-24 15:05:10 +0800243static const char *PCBA_STAGE_F = " androidboot.pcbastage=Reserved";
244//>20200424-michaellin
245
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700246#if VERIFIED_BOOT
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700247static const char *verity_mode = " androidboot.veritymode=";
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700248static const char *verified_state= " androidboot.verifiedbootstate=";
Parth Dixita5715a02015-10-29 12:25:10 +0530249static const char *keymaster_v1= " androidboot.keymaster=1";
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700250//indexed based on enum values, green is 0 by default
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700251
252struct verified_boot_verity_mode vbvm[] =
253{
Channagoud Kadabi86b0c112016-03-16 19:23:16 -0700254#if ENABLE_VB_ATTEST
255 {false, "eio"},
256#else
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700257 {false, "logging"},
Channagoud Kadabi86b0c112016-03-16 19:23:16 -0700258#endif
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700259 {true, "enforcing"},
260};
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700261struct verified_boot_state_name vbsn[] =
262{
263 {GREEN, "green"},
264 {ORANGE, "orange"},
265 {YELLOW,"yellow"},
266 {RED,"red" },
267};
268#endif
Channagoud Kadabi86b0c112016-03-16 19:23:16 -0700269/*As per spec delay wait time before shutdown in Red state*/
270#define DELAY_WAIT 30000
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700271static unsigned page_size = 0;
272static unsigned page_mask = 0;
Gaurav Nebhwani4d2389c2016-03-17 21:10:05 +0530273static unsigned mmc_blocksize = 0;
274static unsigned mmc_blocksize_mask = 0;
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700275static char ffbm_mode_string[FFBM_MODE_BUF_SIZE];
276static bool boot_into_ffbm;
vijay kumar870515d2015-08-31 16:37:24 +0530277static char *target_boot_params = NULL;
Matthew Qind886f3c2014-01-17 16:52:01 +0800278static bool boot_reason_alarm;
Channagoud Kadabi80a182b2015-03-11 17:04:23 -0700279static bool devinfo_present = true;
tracychuid184b912020-06-05 17:31:38 +0800280/*[20200605][TracyChui] Implement get Serial Number start */
281#if defined(ENABLE_PRODINFO_ACCESS)
282static bool prodinfo_present = true;
283#endif
284/*[20200605][TracyChui] Implement get Serial Number end*/
Channagoud Kadabi736c4962015-08-21 11:56:52 -0700285bool boot_into_fastboot = false;
Parth Dixit4097b622016-03-15 14:42:27 +0530286static uint32_t dt_size = 0;
Monika Singh292b3e92018-03-17 22:40:23 +0530287static char *vbcmdline;
288static bootinfo info = {0};
Mayank Grovere1ab96c2018-09-04 20:31:31 +0530289static void *recovery_dtbo_buf = NULL;
290static uint32_t recovery_dtbo_size = 0;
291
Shashank Mittalcd98d472011-08-02 14:29:24 -0700292/* Assuming unauthorized kernel image by default */
293static int auth_kernel_img = 0;
Monika Singh292b3e92018-03-17 22:40:23 +0530294static device_info device = {DEVICE_MAGIC,0,0,0,0,{0},{0},{0},1,{0},0,{0}};
tracychuid184b912020-06-05 17:31:38 +0800295/*[20200605][TracyChui] Implement get Serial Number start*/
296#if defined(ENABLE_PRODINFO_ACCESS)
297static prod_info prod = {PRODINFO_MAGIC, {0}, {0}, 0};
298#endif
299/*[20200605][TracyChui] Implement get Serial Number end*/
Monika Singh292b3e92018-03-17 22:40:23 +0530300
jhchen7a504d12020-04-24 15:45:57 +0800301/*[Arima_8910][jhchen] add fuse check property 20181031 begin*/
302#ifdef ENABLE_FUSE_CHECK
303static const char *fuse_blown = " androidboot.oem.color=red";
304static const char *fuse_not_blown = " androidboot.oem.color=brown";
305#endif
306/*[Arima_8910][jhchen] 20181031 end*/
307
308/*[Arima_8910][jhchen] add fuse check property 20181031 begin*/
309#ifdef ENABLE_FUSE_CHECK
310 int is_fused = 0;
311#endif
312/*[Arima_8910][jhchen] 20181031 end*/
313
vijay kumarc65876c2015-04-24 13:29:16 +0530314static bool is_allow_unlock = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -0700315
vijay kumarca2e6812015-07-08 20:28:25 +0530316static char frp_ptns[2][8] = {"config","frp"};
317
lijuang511a2b52015-08-14 20:50:51 +0800318static const char *critical_flash_allowed_ptn[] = {
319 "aboot",
320 "rpm",
321 "tz",
322 "sbl",
323 "sdi",
324 "sbl1",
325 "xbl",
326 "hyp",
327 "pmic",
328 "bootloader",
329 "devinfo",
330 "partition"};
331
Dima Zavin42168f22009-01-30 11:52:22 -0800332struct atag_ptbl_entry
333{
334 char name[16];
335 unsigned offset;
336 unsigned size;
337 unsigned flags;
338};
339
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700340/*
341 * Partition info, required to be published
342 * for fastboot
343 */
344struct getvar_partition_info {
Mayank Grover7b8a8f82017-10-27 13:07:59 +0530345 char part_name[MAX_GPT_NAME_SIZE]; /* Partition name */
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700346 char getvar_size[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for size */
347 char getvar_type[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for type */
348 char size_response[MAX_RSP_SIZE]; /* fastboot response for size */
349 char type_response[MAX_RSP_SIZE]; /* fastboot response for type */
350};
351
352/*
Mayank Grover7b8a8f82017-10-27 13:07:59 +0530353 * Update the part_type_known for known paritions types.
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700354 */
Mayank Groverd38fe012018-03-13 15:33:16 +0530355#define RAW_STR "raw"
Mayank Grover52cd10a2018-03-15 12:57:54 +0530356#define EXT_STR "ext4"
357#define F2FS_STR "f2fs"
358
359#define FS_SUPERBLOCK_OFFSET 0x400
360#define EXT_MAGIC 0xEF53
361#define EXT_MAGIC_OFFSET_SB 0x38
362#define F2FS_MAGIC 0xF2F52010 // F2FS Magic Number
363#define F2FS_MAGIC_OFFSET_SB 0x0
364
365typedef enum fs_signature_type {
366 EXT_FS_SIGNATURE = 1,
367 EXT_F2FS_SIGNATURE = 2,
368 NO_FS = -1
369} fs_signature_type;
370
Mayank Grover7b8a8f82017-10-27 13:07:59 +0530371struct getvar_partition_info part_info[NUM_PARTITIONS];
372struct getvar_partition_info part_type_known[] =
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700373{
Mayank Grover49920212018-02-09 18:15:55 +0530374 { "system" , "partition-size:", "partition-type:", "", "ext4" },
375 { "userdata" , "partition-size:", "partition-type:", "", "ext4" },
376 { "cache" , "partition-size:", "partition-type:", "", "ext4" },
377 { "recoveryfs" , "partition-size:", "partition-type:", "", "ext4" },
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700378};
379
380char max_download_size[MAX_RSP_SIZE];
Ameya Thakur11cf1a62013-08-05 12:44:48 -0700381char charger_screen_enabled[MAX_RSP_SIZE];
tracychuid184b912020-06-05 17:31:38 +0800382/*[20200605][TracyChui] Implement get Serial Number start*/
383#if defined(ENABLE_PRODINFO_ACCESS)
384char cust_sn_buf[PRODINFO_MAX_SSN_LEN + 1];
385char factory_sn_buf[PRODINFO_MAX_SSN_LEN + 1];
386char AdbEnable[MAX_RSP_SIZE];
387#endif
388#if defined(ENABLE_PRODINFO_ACCESS)
389char sn_buf[PRODINFO_MAX_ISN_LEN + 1];
390#else
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800391char sn_buf[13];
tracychuid184b912020-06-05 17:31:38 +0800392#endif
393/*[20200605][TracyChui] Implement get Serial Number end*/
Dhaval Patel223ec952013-07-18 14:49:44 -0700394char display_panel_buf[MAX_PANEL_BUF_SIZE];
Unnati Gandhi62c8ab82014-01-24 11:01:01 +0530395char panel_display_mode[MAX_RSP_SIZE];
Mayank Grovera64dfbe2018-05-17 14:06:34 +0530396char soc_version_str[MAX_RSP_SIZE];
Mayank Grover7b8a8f82017-10-27 13:07:59 +0530397char block_size_string[MAX_RSP_SIZE];
Mukesh Ojhabb6e7f32018-03-02 15:04:06 +0530398#if PRODUCT_IOT
Mayank Grover7b8a8f82017-10-27 13:07:59 +0530399
400/* For IOT we are using custom version */
401#define PRODUCT_IOT_VERSION "IOT001"
402char bootloader_version_string[MAX_RSP_SIZE];
403#endif
lijuang102dfa92015-10-09 18:31:03 +0800404
405#if CHECK_BAT_VOLTAGE
lijuang65c5a822015-08-29 16:35:36 +0800406char battery_voltage[MAX_RSP_SIZE];
lijuang102dfa92015-10-09 18:31:03 +0800407char battery_soc_ok [MAX_RSP_SIZE];
408#endif
409
lijuangf16461c2015-08-03 17:09:34 +0800410char get_variant[MAX_RSP_SIZE];
Greg Griscod6250552011-06-29 14:40:23 -0700411
Greg Griscod2471ef2011-07-14 13:00:42 -0700412extern int emmc_recovery_init(void);
413
Kinson Chik0b1c8162011-08-31 16:31:57 -0700414#if NO_KEYPAD_DRIVER
415extern int fastboot_trigger(void);
416#endif
Greg Griscod2471ef2011-07-14 13:00:42 -0700417
Mayank Grovere1ab96c2018-09-04 20:31:31 +0530418static void update_ker_tags_rdisk_addr(boot_img_hdr *hdr, bool is_arm64)
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700419{
420 /* overwrite the destination of specified for the project */
Channagoud Kadabi7042fa32013-04-26 16:44:14 -0700421#ifdef ABOOT_IGNORE_BOOT_HEADER_ADDRS
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800422 if (is_arm64)
423 hdr->kernel_addr = ABOOT_FORCE_KERNEL64_ADDR;
424 else
425 hdr->kernel_addr = ABOOT_FORCE_KERNEL_ADDR;
Channagoud Kadabi7042fa32013-04-26 16:44:14 -0700426 hdr->ramdisk_addr = ABOOT_FORCE_RAMDISK_ADDR;
427 hdr->tags_addr = ABOOT_FORCE_TAGS_ADDR;
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700428#endif
429}
430
Dima Zavin42168f22009-01-30 11:52:22 -0800431static void ptentry_to_tag(unsigned **ptr, struct ptentry *ptn)
432{
433 struct atag_ptbl_entry atag_ptn;
434
435 memcpy(atag_ptn.name, ptn->name, 16);
436 atag_ptn.name[15] = '\0';
437 atag_ptn.offset = ptn->start;
438 atag_ptn.size = ptn->length;
439 atag_ptn.flags = ptn->flags;
440 memcpy(*ptr, &atag_ptn, sizeof(struct atag_ptbl_entry));
441 *ptr += sizeof(struct atag_ptbl_entry) / sizeof(unsigned);
442}
Mayank Grover9df84c02018-08-30 15:46:35 +0530443#ifdef VERIFIED_BOOT_2
444void load_vbmeta_image(void **vbmeta_image_buf, uint32_t *vbmeta_image_sz)
445{
446 int index = 0;
447 char *vbm_img_buf = NULL;
448 unsigned long long ptn = 0;
449 unsigned long long ptn_size = 0;
450
451 /* Immediately return if dtbo is not supported */
452 index = partition_get_index("vbmeta");
453 ptn = partition_get_offset(index);
454 if(!ptn)
455 {
456 dprintf(CRITICAL, "ERROR: vbmeta partition not found.\n");
457 return;
458 }
459
460 ptn_size = partition_get_size(index);
461 if (ptn_size > MAX_SUPPORTED_VBMETA_IMG_BUF)
462 {
463 dprintf(CRITICAL, "ERROR: vbmeta parition size is greater than supported.\n");
464 return;
465 }
466
467 vbm_img_buf = (char *)memalign(CACHE_LINE, ROUNDUP((uint32_t)ptn_size, CACHE_LINE));
468 if (!vbm_img_buf)
469 {
470 dprintf(CRITICAL, "ERROR: vbmeta unable to locate buffer\n");
471 return;
472 }
473
474 mmc_set_lun(partition_get_lun(index));
475 if (mmc_read(ptn, (uint32_t *)vbm_img_buf, (uint32_t)ptn_size))
476 {
477 dprintf(CRITICAL, "ERROR: vbmeta read failure\n");
478 free(vbm_img_buf);
479 return;
480 }
481
482 *vbmeta_image_buf = vbm_img_buf;
483 *vbmeta_image_sz = (uint32_t)ptn_size;
484 return;
485}
486#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -0800487
lijuang102dfa92015-10-09 18:31:03 +0800488#if CHECK_BAT_VOLTAGE
489void update_battery_status(void)
490{
491 snprintf(battery_voltage,MAX_RSP_SIZE, "%d",target_get_battery_voltage());
492 snprintf(battery_soc_ok ,MAX_RSP_SIZE, "%s",target_battery_soc_ok()? "yes":"no");
493}
494#endif
495
michaellincb058f12020-04-24 15:05:10 +0800496//<20200424-michaellin, Add PCBA stage to system properties
497uint32_t GetPcbaVariant(void)
498{
499 uint8_t GPIO_97=10;
500 uint8_t GPIO_98=10;
501 uint8_t GPIO_99=10;
502
503 uint8_t pcba_stage = 0;
504
505 // Config GPIO
506 gpio_tlmm_config(97, 0, GPIO_INPUT, GPIO_NO_PULL, GPIO_2MA, GPIO_ENABLE);
507 gpio_tlmm_config(98, 0, GPIO_INPUT, GPIO_NO_PULL, GPIO_2MA, GPIO_ENABLE);
508 gpio_tlmm_config(99, 0, GPIO_INPUT, GPIO_NO_PULL, GPIO_2MA, GPIO_ENABLE);
509
510 /* Wait for the gpio config to take effect - debounce time */
511 thread_sleep(10);
512
513 /* Get status of GPIO */
514 GPIO_97 = gpio_status(97);
515 GPIO_98 = gpio_status(98);
516 GPIO_99 = gpio_status(99);
517
518 pcba_stage = (GPIO_99 << 2) + (GPIO_98 << 1) + GPIO_97;
519 dprintf(ALWAYS, "pcba_stage status: %u\n", pcba_stage);
michaellin52b647a2020-05-05 14:58:47 +0800520 // GPIO_99 GPIO_98 GPIO_97
michaellincb058f12020-04-24 15:05:10 +0800521 // 0 0 0 EP0
522 // 0 0 1 EP1
523 // 0 1 0 EP2
michaellin52b647a2020-05-05 14:58:47 +0800524 // 0 1 1 FP(8901MB-007)/PP
525 // 1 0 0 MP(8901MB-008)
526 // 1 0 1 MP-8903MB_001
michaellincb058f12020-04-24 15:05:10 +0800527 return pcba_stage;
528}
529//>20200424-michaellin
530
Neeti Desaie245d492012-06-01 12:52:13 -0700531unsigned char *update_cmdline(const char * cmdline)
Brian Swetland9c4c0752009-01-25 16:23:50 -0800532{
David Ng183a7422009-12-07 14:55:21 -0800533 int cmdline_len = 0;
534 int have_cmdline = 0;
Amol Jadi168b7712012-03-06 16:15:00 -0800535 unsigned char *cmdline_final = NULL;
Neeti Desaie245d492012-06-01 12:52:13 -0700536 int pause_at_bootup = 0;
tracychuid184b912020-06-05 17:31:38 +0800537/*[20200605][TracyChui] Implement get Serial Number start*/
538#if defined(ENABLE_PRODINFO_ACCESS)
539 int AdbEnable = 0;
540#endif
541/*[20200605][TracyChui] Implement get Serial Number end*/
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800542 bool warm_boot = false;
Pavel Nedev5614d222013-06-17 18:01:02 +0300543 bool gpt_exists = partition_gpt_exists();
Joonwoo Park61112782013-10-02 19:50:39 -0700544 int have_target_boot_params = 0;
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700545 char *boot_dev_buf = NULL;
Monika Singh292b3e92018-03-17 22:40:23 +0530546#ifdef MDTP_SUPPORT
Mayank Grover351a75e2017-05-30 20:06:08 +0530547 bool is_mdtp_activated = 0;
Monika Singh292b3e92018-03-17 22:40:23 +0530548#endif
Mayank Grover351a75e2017-05-30 20:06:08 +0530549 int current_active_slot = INVALID;
Mayank Grover3804be72017-06-22 11:59:23 +0530550 int system_ptn_index = -1;
551 unsigned int lun = 0;
552 char lun_char_base = 'a';
Sourabh Banerjee386b1322018-02-27 09:37:28 +0530553#if VERITY_LE
554 int syspath_buflen = strlen(verity_dev)
555 + strlen(verity_system_part) + (sizeof(char) * 2) + 2
556 + strlen(verity_params) + sizeof(int) + 2;
557#else
lijuang83ef4b22018-08-23 11:01:55 +0800558 int syspath_buflen = strlen(sys_path) + sizeof(int) + 2; /*allocate buflen for largest possible string*/
559 char dtbo_idx_str[MAX_DTBO_IDX_STR] = "\0";
560 int dtbo_idx = INVALID_PTN;
Parth Dixit1375d9e2019-07-08 20:56:13 +0530561 char dtb_idx_str[MAX_DTB_IDX_STR] = "\0";
562 int dtb_idx = INVALID_PTN;
Sourabh Banerjee386b1322018-02-27 09:37:28 +0530563#endif
Mayank Grover3804be72017-06-22 11:59:23 +0530564 char syspath_buf[syspath_buflen];
Mayank Grover466d6562018-05-10 14:52:20 +0530565#if HIBERNATION_SUPPORT
566 int resume_buflen = strlen(resume) + sizeof(int) + 2;
567 char resume_buf[resume_buflen];
568 int swap_ptn_index = INVALID_PTN;
569#endif
570
Mayank Grover889be1b2017-09-12 20:12:23 +0530571#if VERIFIED_BOOT
572 uint32_t boot_state = RED;
573#endif
Hareesh Gauthamabf1e1e2017-04-12 18:09:24 +0530574
575#if USE_LE_SYSTEMD
576 is_systemd_present=true;
577#endif
578
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700579#if VERIFIED_BOOT
Mayank Grover912eaa62017-10-26 12:08:53 +0530580 if (VB_M <= target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +0530581 {
582 boot_state = boot_verify_get_state();
583 }
Parth Dixitddbc7352015-10-18 03:13:31 +0530584#endif
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700585
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200586#ifdef MDTP_SUPPORT
587 mdtp_activated(&is_mdtp_activated);
588#endif /* MDTP_SUPPORT */
Dima Zavin42168f22009-01-30 11:52:22 -0800589
Brian Swetland9c4c0752009-01-25 16:23:50 -0800590 if (cmdline && cmdline[0]) {
David Ng183a7422009-12-07 14:55:21 -0800591 cmdline_len = strlen(cmdline);
592 have_cmdline = 1;
593 }
594 if (target_is_emmc_boot()) {
595 cmdline_len += strlen(emmc_cmdline);
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700596 boot_dev_buf = (char *) malloc(sizeof(char) * BOOT_DEV_MAX_LEN);
Mayank Groverb716edf2019-05-08 16:06:55 +0530597 if (!boot_dev_buf) {
598 dprintf(CRITICAL, "ERROR: Failed to allocate boot_dev_buf\n");
599 } else {
600 platform_boot_dev_cmdline(boot_dev_buf);
601#if USE_BOOTDEV_CMDLINE
602 cmdline_len += strlen(boot_dev_buf);
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700603#endif
Mayank Groverb716edf2019-05-08 16:06:55 +0530604 if (target_dynamic_partition_supported()) {
605 cmdline_len += strlen(dynamic_bootdev_cmdline);
606 cmdline_len += strlen(boot_dev_buf);
607 }
608 }
David Ng183a7422009-12-07 14:55:21 -0800609 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800610
611 cmdline_len += strlen(usb_sn_cmdline);
612 cmdline_len += strlen(sn_buf);
tracychuid184b912020-06-05 17:31:38 +0800613/*[20200605][TracyChui] Implement get Serial Number start */
614#if defined(ENABLE_PRODINFO_ACCESS)
615 cmdline_len += strlen(cust_sn_cmdline);
616 cmdline_len += strlen(cust_sn_buf);
617 cmdline_len += strlen(factory_sn_cmdline);
618 cmdline_len += strlen(factory_sn_buf);
619#endif
620/*[20200605][TracyChui] Implement get Serial Number end */
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800621
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700622#if VERIFIED_BOOT
Mayank Grover912eaa62017-10-26 12:08:53 +0530623 if (VB_M <= target_get_vb_version())
Sridhar Parasuram7d8bb9a2015-09-23 18:21:19 -0700624 {
Mayank Grover889be1b2017-09-12 20:12:23 +0530625 cmdline_len += strlen(verified_state) + strlen(vbsn[boot_state].name);
626 if ((device.verity_mode != 0 ) && (device.verity_mode != 1))
627 {
628 dprintf(CRITICAL, "Devinfo paritition possibly corrupted!!!. Please erase devinfo partition to continue booting\n");
629 ASSERT(0);
630 }
631 cmdline_len += strlen(verity_mode) + strlen(vbvm[device.verity_mode].name);
632 cmdline_len += strlen(keymaster_v1);
Sridhar Parasuram7d8bb9a2015-09-23 18:21:19 -0700633 }
Parth Dixitddbc7352015-10-18 03:13:31 +0530634#endif
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700635
Monika Singh292b3e92018-03-17 22:40:23 +0530636
637 if (vbcmdline != NULL) {
638 dprintf(DEBUG, "UpdateCmdLine vbcmdline present len %d\n",
639 strlen(vbcmdline));
640 cmdline_len += strlen(vbcmdline);
641 }
642
Pavel Nedev5614d222013-06-17 18:01:02 +0300643 if (boot_into_recovery && gpt_exists)
644 cmdline_len += strlen(secondary_gpt_enable);
645
Monika Singh292b3e92018-03-17 22:40:23 +0530646#ifdef MDTP_SUPPORT
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200647 if(is_mdtp_activated)
648 cmdline_len += strlen(mdtp_activated_flag);
Monika Singh292b3e92018-03-17 22:40:23 +0530649#endif
Pavel Nedev328ac822013-04-05 15:25:11 +0300650 if (boot_into_ffbm) {
651 cmdline_len += strlen(androidboot_mode);
Hareesh Gauthamabf1e1e2017-04-12 18:09:24 +0530652
653 if(is_systemd_present)
654 cmdline_len += strlen(systemd_ffbm_mode);
655
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700656 cmdline_len += strlen(ffbm_mode_string);
Pavel Nedev898298c2013-02-27 12:36:09 -0800657 /* reduce kernel console messages to speed-up boot */
658 cmdline_len += strlen(loglevel);
Matthew Qind886f3c2014-01-17 16:52:01 +0800659 } else if (boot_reason_alarm) {
660 cmdline_len += strlen(alarmboot_cmdline);
Zhenhua Huang431dafa2015-06-30 16:13:37 +0800661 } else if ((target_build_variant_user() || device.charger_screen_enabled)
lijuang266b17d2018-08-17 18:53:42 +0800662 && target_pause_for_battery_charge() && !boot_into_recovery) {
David Ngf773dde2010-07-26 19:55:08 -0700663 pause_at_bootup = 1;
664 cmdline_len += strlen(battchg_pause);
665 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800666
tracychuid184b912020-06-05 17:31:38 +0800667/*[20200605][TracyChui] Implement get Serial Number start*/
668#if defined(ENABLE_PRODINFO_ACCESS)
669 if (prod.is_adb_enabled) {
670 dprintf(CRITICAL, "Device will enable adb\n");
671
672 prod.is_adb_enabled = 0;
673 write_prod_info(&prod);
674
675 AdbEnable = 1;
676 cmdline_len += strlen(UsbAdbEnable);
677 }
678#endif
679/*[20200605][TracyChui] Implement get Serial Number end*/
680
Shashank Mittalcd98d472011-08-02 14:29:24 -0700681 if(target_use_signed_kernel() && auth_kernel_img) {
682 cmdline_len += strlen(auth_kernel);
683 }
684
Joonwoo Park61112782013-10-02 19:50:39 -0700685 if (get_target_boot_params(cmdline, boot_into_recovery ? "recoveryfs" :
686 "system",
vijay kumar870515d2015-08-31 16:37:24 +0530687 &target_boot_params) == 0) {
Joonwoo Park61112782013-10-02 19:50:39 -0700688 have_target_boot_params = 1;
689 cmdline_len += strlen(target_boot_params);
690 }
691
Ajay Dudanid04110c2011-01-17 23:55:07 -0800692 /* Determine correct androidboot.baseband to use */
693 switch(target_baseband())
694 {
695 case BASEBAND_APQ:
696 cmdline_len += strlen(baseband_apq);
697 break;
698
699 case BASEBAND_MSM:
700 cmdline_len += strlen(baseband_msm);
701 break;
702
703 case BASEBAND_CSFB:
704 cmdline_len += strlen(baseband_csfb);
705 break;
706
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800707 case BASEBAND_SVLTE2A:
708 cmdline_len += strlen(baseband_svlte2a);
Ajay Dudanid04110c2011-01-17 23:55:07 -0800709 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700710
711 case BASEBAND_MDM:
712 cmdline_len += strlen(baseband_mdm);
713 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700714
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800715 case BASEBAND_MDM2:
716 cmdline_len += strlen(baseband_mdm2);
717 break;
718
Amol Jadi5c61a952012-05-04 17:05:35 -0700719 case BASEBAND_SGLTE:
720 cmdline_len += strlen(baseband_sglte);
721 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530722
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800723 case BASEBAND_SGLTE2:
724 cmdline_len += strlen(baseband_sglte2);
725 break;
726
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530727 case BASEBAND_DSDA:
728 cmdline_len += strlen(baseband_dsda);
729 break;
Amol Jadi2a15a272013-01-22 12:03:36 -0800730
731 case BASEBAND_DSDA2:
732 cmdline_len += strlen(baseband_dsda2);
733 break;
Vijay Kumar Pendotib228cfc2016-06-13 20:15:23 +0530734 case BASEBAND_APQ_NOWGR:
735 cmdline_len += strlen(baseband_apq_nowgr);
736 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800737 }
738
michaellincb058f12020-04-24 15:05:10 +0800739 //<20200424-michaellin, Add PCBA stage to system properties
740 switch(GetPcbaVariant())
741 {
742 case 0: // EP0
743 cmdline_len += strlen(PCBA_STAGE_0);
744 break;
745
746 case 1: // EP1
747 cmdline_len += strlen(PCBA_STAGE_1);
748 break;
749
750 case 2: // EP2
751 cmdline_len += strlen(PCBA_STAGE_2);
752 break;
753
michaellin52b647a2020-05-05 14:58:47 +0800754 case 3: // FP(8901MB-007)/PP
michaellincb058f12020-04-24 15:05:10 +0800755 cmdline_len += strlen(PCBA_STAGE_3);
756 break;
757
michaellin52b647a2020-05-05 14:58:47 +0800758 case 4: // MP(8901MB-008)
michaellincb058f12020-04-24 15:05:10 +0800759 cmdline_len += strlen(PCBA_STAGE_4);
760 break;
761
michaellin52b647a2020-05-05 14:58:47 +0800762 case 5: // MP-8903MB_001
763 cmdline_len += strlen(PCBA_STAGE_5);
764 break;
765
michaellincb058f12020-04-24 15:05:10 +0800766 default:// Reserved
767 cmdline_len += strlen(PCBA_STAGE_F);
768 break;
769 }
770 //>20200424-michaellin
771
Vladislav Levenetz3cd45242017-04-05 08:58:07 +0300772#if ENABLE_DISPLAY
Lijuan Gao4a5b8322014-07-24 10:38:42 +0800773 if (cmdline) {
774 if ((strstr(cmdline, DISPLAY_DEFAULT_PREFIX) == NULL) &&
Padmanabhan Komandurubccbcdc2015-06-30 16:19:24 +0530775 target_display_panel_node(display_panel_buf,
776 MAX_PANEL_BUF_SIZE) &&
Lijuan Gao4a5b8322014-07-24 10:38:42 +0800777 strlen(display_panel_buf)) {
778 cmdline_len += strlen(display_panel_buf);
779 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700780 }
Vladislav Levenetz3cd45242017-04-05 08:58:07 +0300781#endif
Dhaval Patel223ec952013-07-18 14:49:44 -0700782
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800783 if (target_warm_boot()) {
784 warm_boot = true;
785 cmdline_len += strlen(warmboot_cmdline);
786 }
787
Mayank Grover5384ed82018-05-09 12:09:24 +0530788 if (target_uses_system_as_root() ||
789 partition_multislot_is_supported())
Mayank Grover351a75e2017-05-30 20:06:08 +0530790 {
791 current_active_slot = partition_find_active_slot();
792 cmdline_len += (strlen(androidboot_slot_suffix)+
793 strlen(SUFFIX_SLOT(current_active_slot)));
794
Mayank Grover3804be72017-06-22 11:59:23 +0530795 system_ptn_index = partition_get_index("system");
796 if (platform_boot_dev_isemmc())
797 {
Sourabh Banerjee386b1322018-02-27 09:37:28 +0530798#if VERITY_LE
799 /*
800 Condition 4: Verity and A/B both enabled
801 Eventual command line looks like:
802 ... androidboot.slot_suffix=<slot_suffix> ... rootfstype=ext4 ...
803 ... root=/dev/dm-0 dm="system_<slot_suffix> none ro,0 1 android-verity /dev/mmcblk0p<NN>"
804 */
805 snprintf(syspath_buf, syspath_buflen, " %s %s%s %s%d\"",
806 verity_dev,
807 verity_system_part, suffix_slot[current_active_slot],
808 verity_params, system_ptn_index + 1);
809#else
810 /*
811 Condition 5: A/B enabled, but verity disabled
812 Eventual command line looks like:
813 ... androidboot.slot_suffix=<slot_suffix> ... rootfstype=ext4 ...
814 ... root=/dev/mmcblk0p<NN> ...
815 */
816 snprintf(syspath_buf, syspath_buflen, " %s%d",
817 sys_path, system_ptn_index + 1);
818#endif
Mayank Grover3804be72017-06-22 11:59:23 +0530819 }
820 else
821 {
822 lun = partition_get_lun(system_ptn_index);
823 snprintf(syspath_buf, syspath_buflen, " root=/dev/sd%c%d",
824 lun_char_base + lun,
825 partition_get_index_in_lun("system", lun));
826 }
Mayank Grover351a75e2017-05-30 20:06:08 +0530827
Monika Singh292b3e92018-03-17 22:40:23 +0530828#ifndef VERIFIED_BOOT_2
Mayank Grover3804be72017-06-22 11:59:23 +0530829 cmdline_len += strlen(syspath_buf);
Monika Singh292b3e92018-03-17 22:40:23 +0530830#endif
Mayank Grover5384ed82018-05-09 12:09:24 +0530831 }
832
833 if (target_uses_system_as_root() ||
834 partition_multislot_is_supported())
835 {
836 cmdline_len += strlen(sys_path_cmdline);
Mayank Groverb716edf2019-05-08 16:06:55 +0530837
838 /* For dynamic partition, support skip skip_initramfs */
839 if (!target_dynamic_partition_supported() &&
840 !boot_into_recovery)
Mayank Grover351a75e2017-05-30 20:06:08 +0530841 cmdline_len += strlen(skip_ramfs);
842 }
843
Mayank Grover466d6562018-05-10 14:52:20 +0530844#if HIBERNATION_SUPPORT
845 if (platform_boot_dev_isemmc())
846 {
847 swap_ptn_index = partition_get_index("swap");
848 if (swap_ptn_index != INVALID_PTN)
849 {
850 snprintf(resume_buf, resume_buflen,
851 " %s%d", resume,
852 (swap_ptn_index + 1));
853 cmdline_len += strlen(resume_buf);
854 }
855 else
856 {
857 dprintf(INFO, "WARNING: swap partition not found\n");
858 }
859 }
860#endif
861
Channagoud Kadabi85c7ec32016-01-28 23:09:21 -0800862#if TARGET_CMDLINE_SUPPORT
863 char *target_cmdline_buf = malloc(TARGET_MAX_CMDLNBUF);
864 int target_cmd_line_len;
865 ASSERT(target_cmdline_buf);
866 target_cmd_line_len = target_update_cmdline(target_cmdline_buf);
867 cmdline_len += target_cmd_line_len;
868#endif
869
lijuang83ef4b22018-08-23 11:01:55 +0800870#if !VERITY_LE
871 dtbo_idx = get_dtbo_idx ();
872 if (dtbo_idx != INVALID_PTN) {
873 snprintf(dtbo_idx_str, sizeof(dtbo_idx_str), "%s%d",
874 android_boot_dtbo_idx, dtbo_idx);
875 cmdline_len += strlen (dtbo_idx_str);
876 }
Parth Dixit1375d9e2019-07-08 20:56:13 +0530877
878 dtb_idx = get_dtb_idx ();
879 if (dtb_idx != INVALID_PTN) {
880 snprintf(dtb_idx_str, sizeof(dtb_idx_str), "%s%d",
881 android_boot_dtb_idx, dtb_idx);
882 cmdline_len += strlen (dtb_idx_str);
883 }
lijuang83ef4b22018-08-23 11:01:55 +0800884#endif
885
jhchen7a504d12020-04-24 15:45:57 +0800886/*[Arima_8910][jhchen] add fuse check property 20181031 begin*/
887#ifdef ENABLE_FUSE_CHECK
888 {
889 int hw_key_status = readl(0xA01D0);
890 is_fused = (hw_key_status & 0x2) >> 1;
891 dprintf(CRITICAL, "hw_key_status = 0x%x, is_fused=%d\n", hw_key_status, is_fused);
892 if (is_fused) {
893 cmdline_len += strlen(fuse_blown);
894 } else {
895 cmdline_len += strlen(fuse_not_blown);
896 }
897 }
898#endif
899/*[Arima_8910][jhchen] 20181031 end*/
900
David Ng183a7422009-12-07 14:55:21 -0800901 if (cmdline_len > 0) {
902 const char *src;
Maria Yu52254c02014-07-04 16:14:54 +0800903 unsigned char *dst;
904
905 cmdline_final = (unsigned char*) malloc((cmdline_len + 4) & (~3));
906 ASSERT(cmdline_final != NULL);
vijay kumar287bb542015-09-29 13:01:52 +0530907 memset((void *)cmdline_final, 0, sizeof(*cmdline_final));
Maria Yu52254c02014-07-04 16:14:54 +0800908 dst = cmdline_final;
Neeti Desaie245d492012-06-01 12:52:13 -0700909
Amol Jadi168b7712012-03-06 16:15:00 -0800910 /* Save start ptr for debug print */
David Ng183a7422009-12-07 14:55:21 -0800911 if (have_cmdline) {
912 src = cmdline;
913 while ((*dst++ = *src++));
914 }
915 if (target_is_emmc_boot()) {
916 src = emmc_cmdline;
917 if (have_cmdline) --dst;
David Ngf773dde2010-07-26 19:55:08 -0700918 have_cmdline = 1;
919 while ((*dst++ = *src++));
Sridhar Parasuram7bd4aaf2015-02-12 11:14:38 -0800920#if USE_BOOTDEV_CMDLINE
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700921 src = boot_dev_buf;
Mayank Groverb716edf2019-05-08 16:06:55 +0530922 if (have_cmdline &&
923 boot_dev_buf) {
924 --dst;
925 while ((*dst++ = *src++));
926 }
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700927#endif
Mayank Groverb716edf2019-05-08 16:06:55 +0530928 /* Dynamic partition append boot_devices */
929 if (target_dynamic_partition_supported() &&
930 boot_dev_buf) {
931 src = dynamic_bootdev_cmdline;
932 if (have_cmdline) --dst;
933 while ((*dst++ = *src++));
934 src = boot_dev_buf;
935 if (have_cmdline) --dst;
936 while ((*dst++ = *src++));
937 }
David Ngf773dde2010-07-26 19:55:08 -0700938 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800939
michaellincb058f12020-04-24 15:05:10 +0800940 //<20200424-michaellin, Add PCBA stage to system properties
941 switch(GetPcbaVariant())
942 {
943 case 0:
944 src = PCBA_STAGE_0;
945 if (have_cmdline) --dst;
946 while ((*dst++ = *src++));
947 break;
948 case 1:
949 src = PCBA_STAGE_1;
950 if (have_cmdline) --dst;
951 while ((*dst++ = *src++));
952 break;
953 case 2:
954 src = PCBA_STAGE_2;
955 if (have_cmdline) --dst;
956 while ((*dst++ = *src++));
957 break;
958 case 3:
959 src = PCBA_STAGE_3;
960 if (have_cmdline) --dst;
961 while ((*dst++ = *src++));
962 break;
963 case 4:
964 src = PCBA_STAGE_4;
965 if (have_cmdline) --dst;
966 while ((*dst++ = *src++));
967 break;
michaellin52b647a2020-05-05 14:58:47 +0800968 case 5:
969 src = PCBA_STAGE_5;
970 if (have_cmdline) --dst;
971 while ((*dst++ = *src++));
972 break;
michaellincb058f12020-04-24 15:05:10 +0800973 default:
974 src = PCBA_STAGE_F;
975 if (have_cmdline) --dst;
976 while ((*dst++ = *src++));
977 break;
978 }
979 //>20200424-michaellin
980
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700981#if VERIFIED_BOOT
Mayank Grover912eaa62017-10-26 12:08:53 +0530982 if (VB_M <= target_get_vb_version())
Sridhar Parasuram7d8bb9a2015-09-23 18:21:19 -0700983 {
Mayank Grover889be1b2017-09-12 20:12:23 +0530984 src = verified_state;
985 if(have_cmdline) --dst;
986 have_cmdline = 1;
987 while ((*dst++ = *src++));
988 src = vbsn[boot_state].name;
989 if(have_cmdline) --dst;
990 while ((*dst++ = *src++));
991
992 if ((device.verity_mode != 0 ) && (device.verity_mode != 1))
993 {
994 dprintf(CRITICAL, "Devinfo paritition possibly corrupted!!!. Please erase devinfo partition to continue booting\n");
995 ASSERT(0);
996 }
997 src = verity_mode;
998 if(have_cmdline) --dst;
999 while ((*dst++ = *src++));
1000 src = vbvm[device.verity_mode].name;
1001 if(have_cmdline) -- dst;
1002 while ((*dst++ = *src++));
1003 src = keymaster_v1;
1004 if(have_cmdline) --dst;
1005 while ((*dst++ = *src++));
Sridhar Parasuram7d8bb9a2015-09-23 18:21:19 -07001006 }
Parth Dixitddbc7352015-10-18 03:13:31 +05301007#endif
Monika Singh292b3e92018-03-17 22:40:23 +05301008
1009 if (vbcmdline != NULL) {
1010 src = vbcmdline;
1011 if (have_cmdline) --dst;
1012 while ((*dst++ = *src++));
1013 }
1014
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -08001015 src = usb_sn_cmdline;
1016 if (have_cmdline) --dst;
1017 have_cmdline = 1;
1018 while ((*dst++ = *src++));
1019 src = sn_buf;
1020 if (have_cmdline) --dst;
1021 have_cmdline = 1;
1022 while ((*dst++ = *src++));
tracychuid184b912020-06-05 17:31:38 +08001023
1024 /* [20200605][TracyChui] Implement get Serial Number */
1025 #if defined(ENABLE_PRODINFO_ACCESS)
1026 src = cust_sn_cmdline;
1027 if (have_cmdline) --dst;
1028 have_cmdline = 1;
1029 while ((*dst++ = *src++));
1030 src = cust_sn_buf;
1031 if (have_cmdline) --dst;
1032 have_cmdline = 1;
1033 while ((*dst++ = *src++));
1034 src = factory_sn_cmdline;
1035 if (have_cmdline) --dst;
1036 have_cmdline = 1;
1037 while ((*dst++ = *src++));
1038 src = factory_sn_buf;
1039 if (have_cmdline) --dst;
1040 have_cmdline = 1;
1041 while ((*dst++ = *src++));
1042 #endif
1043 /*[20200605][TracyChui] Implement get Serial Number end */
Hanumant Singh8e1ac232014-01-29 13:41:51 -08001044 if (warm_boot) {
1045 if (have_cmdline) --dst;
1046 src = warmboot_cmdline;
1047 while ((*dst++ = *src++));
1048 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -08001049
Pavel Nedev5614d222013-06-17 18:01:02 +03001050 if (boot_into_recovery && gpt_exists) {
1051 src = secondary_gpt_enable;
1052 if (have_cmdline) --dst;
1053 while ((*dst++ = *src++));
1054 }
Monika Singh292b3e92018-03-17 22:40:23 +05301055#ifdef MDTP_SUPPORT
Shay Nachmanibc10dfe2015-02-10 14:45:55 +02001056 if (is_mdtp_activated) {
1057 src = mdtp_activated_flag;
1058 if (have_cmdline) --dst;
1059 while ((*dst++ = *src++));
1060 }
Monika Singh292b3e92018-03-17 22:40:23 +05301061#endif
Pavel Nedev328ac822013-04-05 15:25:11 +03001062 if (boot_into_ffbm) {
1063 src = androidboot_mode;
1064 if (have_cmdline) --dst;
1065 while ((*dst++ = *src++));
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07001066 src = ffbm_mode_string;
Pavel Nedev328ac822013-04-05 15:25:11 +03001067 if (have_cmdline) --dst;
1068 while ((*dst++ = *src++));
Hareesh Gauthamabf1e1e2017-04-12 18:09:24 +05301069
1070 if(is_systemd_present) {
1071 src = systemd_ffbm_mode;
1072 if (have_cmdline) --dst;
1073 while ((*dst++ = *src++));
1074 }
1075
Pavel Nedev898298c2013-02-27 12:36:09 -08001076 src = loglevel;
1077 if (have_cmdline) --dst;
1078 while ((*dst++ = *src++));
Matthew Qind886f3c2014-01-17 16:52:01 +08001079 } else if (boot_reason_alarm) {
1080 src = alarmboot_cmdline;
1081 if (have_cmdline) --dst;
1082 while ((*dst++ = *src++));
Pavel Nedev328ac822013-04-05 15:25:11 +03001083 } else if (pause_at_bootup) {
David Ngf773dde2010-07-26 19:55:08 -07001084 src = battchg_pause;
1085 if (have_cmdline) --dst;
David Ng183a7422009-12-07 14:55:21 -08001086 while ((*dst++ = *src++));
1087 }
Ajay Dudanid04110c2011-01-17 23:55:07 -08001088
tracychuid184b912020-06-05 17:31:38 +08001089/*[20200605][TracyChui] Implement get Serial Number start*/
1090#if defined(ENABLE_PRODINFO_ACCESS)
1091 if (AdbEnable) {
1092 src = UsbAdbEnable;
1093 if (have_cmdline) --dst;
1094 while ((*dst++ = *src++));
1095 }
1096#endif
1097/*[20200605][TracyChui] Implement get Serial Number end*/
1098
Shashank Mittalcd98d472011-08-02 14:29:24 -07001099 if(target_use_signed_kernel() && auth_kernel_img) {
1100 src = auth_kernel;
1101 if (have_cmdline) --dst;
1102 while ((*dst++ = *src++));
1103 }
1104
Ajay Dudanid04110c2011-01-17 23:55:07 -08001105 switch(target_baseband())
1106 {
1107 case BASEBAND_APQ:
1108 src = baseband_apq;
1109 if (have_cmdline) --dst;
1110 while ((*dst++ = *src++));
1111 break;
1112
1113 case BASEBAND_MSM:
1114 src = baseband_msm;
1115 if (have_cmdline) --dst;
1116 while ((*dst++ = *src++));
1117 break;
1118
1119 case BASEBAND_CSFB:
1120 src = baseband_csfb;
1121 if (have_cmdline) --dst;
1122 while ((*dst++ = *src++));
1123 break;
1124
Ajay Dudani6cff85e2011-02-04 16:02:16 -08001125 case BASEBAND_SVLTE2A:
1126 src = baseband_svlte2a;
Ajay Dudanid04110c2011-01-17 23:55:07 -08001127 if (have_cmdline) --dst;
1128 while ((*dst++ = *src++));
1129 break;
Ajay Dudani403bc492011-09-30 16:17:21 -07001130
1131 case BASEBAND_MDM:
1132 src = baseband_mdm;
1133 if (have_cmdline) --dst;
1134 while ((*dst++ = *src++));
1135 break;
Amol Jadi5c61a952012-05-04 17:05:35 -07001136
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -08001137 case BASEBAND_MDM2:
1138 src = baseband_mdm2;
1139 if (have_cmdline) --dst;
1140 while ((*dst++ = *src++));
1141 break;
1142
Amol Jadi5c61a952012-05-04 17:05:35 -07001143 case BASEBAND_SGLTE:
1144 src = baseband_sglte;
1145 if (have_cmdline) --dst;
1146 while ((*dst++ = *src++));
1147 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +05301148
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -08001149 case BASEBAND_SGLTE2:
1150 src = baseband_sglte2;
1151 if (have_cmdline) --dst;
1152 while ((*dst++ = *src++));
1153 break;
1154
Channagoud Kadabi141f2982012-10-31 11:23:02 +05301155 case BASEBAND_DSDA:
1156 src = baseband_dsda;
1157 if (have_cmdline) --dst;
1158 while ((*dst++ = *src++));
1159 break;
Amol Jadi2a15a272013-01-22 12:03:36 -08001160
1161 case BASEBAND_DSDA2:
1162 src = baseband_dsda2;
1163 if (have_cmdline) --dst;
1164 while ((*dst++ = *src++));
1165 break;
Vijay Kumar Pendotib228cfc2016-06-13 20:15:23 +05301166 case BASEBAND_APQ_NOWGR:
1167 src = baseband_apq_nowgr;
1168 if (have_cmdline) --dst;
1169 while ((*dst++ = *src++));
1170 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -08001171 }
Dhaval Patel223ec952013-07-18 14:49:44 -07001172
1173 if (strlen(display_panel_buf)) {
Dhaval Patel223ec952013-07-18 14:49:44 -07001174 src = display_panel_buf;
1175 if (have_cmdline) --dst;
1176 while ((*dst++ = *src++));
1177 }
Joonwoo Park61112782013-10-02 19:50:39 -07001178
1179 if (have_target_boot_params) {
1180 if (have_cmdline) --dst;
1181 src = target_boot_params;
1182 while ((*dst++ = *src++));
vijay kumar870515d2015-08-31 16:37:24 +05301183 free(target_boot_params);
Joonwoo Park61112782013-10-02 19:50:39 -07001184 }
Channagoud Kadabi85c7ec32016-01-28 23:09:21 -08001185
Mayank Grover351a75e2017-05-30 20:06:08 +05301186 if (partition_multislot_is_supported() && have_cmdline)
1187 {
1188 src = androidboot_slot_suffix;
1189 --dst;
1190 while ((*dst++ = *src++));
1191 --dst;
1192 src = SUFFIX_SLOT(current_active_slot);
1193 while ((*dst++ = *src++));
Mayank Grover5384ed82018-05-09 12:09:24 +05301194 }
Mayank Grover351a75e2017-05-30 20:06:08 +05301195
Mayank Grover351a75e2017-05-30 20:06:08 +05301196
Mayank Grover5384ed82018-05-09 12:09:24 +05301197 /*
1198 * System-As-Root behaviour, system.img should contain both
1199 * system content and ramdisk content, and should be mounted at
1200 * root(a/b).
1201 * Apending skip_ramfs for non a/b builds which use, system as root.
1202 */
1203 if ((target_uses_system_as_root() ||
1204 partition_multislot_is_supported()) &&
1205 have_cmdline)
1206 {
Mayank Groverb716edf2019-05-08 16:06:55 +05301207 if (!target_dynamic_partition_supported() &&
1208 !boot_into_recovery)
Mayank Grover5384ed82018-05-09 12:09:24 +05301209 {
1210 src = skip_ramfs;
Mayank Grover351a75e2017-05-30 20:06:08 +05301211 --dst;
1212 while ((*dst++ = *src++));
Mayank Grover5384ed82018-05-09 12:09:24 +05301213 }
1214
1215 src = sys_path_cmdline;
1216 --dst;
1217 while ((*dst++ = *src++));
Mayank Grover3804be72017-06-22 11:59:23 +05301218
Monika Singh292b3e92018-03-17 22:40:23 +05301219#ifndef VERIFIED_BOOT_2
Mayank Grover5384ed82018-05-09 12:09:24 +05301220 src = syspath_buf;
1221 --dst;
1222 while ((*dst++ = *src++));
Monika Singh292b3e92018-03-17 22:40:23 +05301223#endif
Mayank Grover351a75e2017-05-30 20:06:08 +05301224 }
1225
Mayank Grover466d6562018-05-10 14:52:20 +05301226#if HIBERNATION_SUPPORT
1227 if (swap_ptn_index != INVALID_PTN)
1228 {
1229 src = resume_buf;
1230 --dst;
1231 while ((*dst++ = *src++));
1232 }
1233#endif
1234
Channagoud Kadabi85c7ec32016-01-28 23:09:21 -08001235#if TARGET_CMDLINE_SUPPORT
1236 if (target_cmdline_buf && target_cmd_line_len)
1237 {
1238 if (have_cmdline) --dst;
1239 src = target_cmdline_buf;
1240 while((*dst++ = *src++));
1241 free(target_cmdline_buf);
1242 }
1243#endif
lijuang83ef4b22018-08-23 11:01:55 +08001244
1245#if !VERITY_LE
1246 if (dtbo_idx != INVALID_PTN) {
1247 src = dtbo_idx_str;
1248 --dst;
1249 while ((*dst++ = *src++));
1250 }
Parth Dixit1375d9e2019-07-08 20:56:13 +05301251
1252 if (dtb_idx != INVALID_PTN) {
1253 src = dtb_idx_str;
1254 --dst;
1255 while ((*dst++ = *src++));
1256 }
lijuang83ef4b22018-08-23 11:01:55 +08001257#endif
jhchen7a504d12020-04-24 15:45:57 +08001258
1259/*[Arima_8910][jhchen] add fuse check property 20181031 begin*/
1260#ifdef ENABLE_FUSE_CHECK
1261 if (is_fused) {
1262 src = fuse_blown;
1263 if (have_cmdline) --dst;
1264 while ((*dst++ = *src++));
1265 } else {
1266 src = fuse_not_blown;
1267 if (have_cmdline) --dst;
1268 while ((*dst++ = *src++));
1269 }
1270#endif
1271/*[Arima_8910][jhchen] 20181031 end*/
Neeti Desaie245d492012-06-01 12:52:13 -07001272 }
Dhaval Patel223ec952013-07-18 14:49:44 -07001273
1274
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -07001275 if (boot_dev_buf)
1276 free(boot_dev_buf);
1277
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -08001278 if (cmdline_final)
1279 dprintf(INFO, "cmdline: %s\n", cmdline_final);
1280 else
1281 dprintf(INFO, "cmdline is NULL\n");
Neeti Desaie245d492012-06-01 12:52:13 -07001282 return cmdline_final;
1283}
1284
1285unsigned *atag_core(unsigned *ptr)
1286{
1287 /* CORE */
1288 *ptr++ = 2;
1289 *ptr++ = 0x54410001;
1290
1291 return ptr;
1292
1293}
1294
1295unsigned *atag_ramdisk(unsigned *ptr, void *ramdisk,
1296 unsigned ramdisk_size)
1297{
1298 if (ramdisk_size) {
1299 *ptr++ = 4;
1300 *ptr++ = 0x54420005;
1301 *ptr++ = (unsigned)ramdisk;
1302 *ptr++ = ramdisk_size;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001303 }
1304
Neeti Desaie245d492012-06-01 12:52:13 -07001305 return ptr;
1306}
1307
1308unsigned *atag_ptable(unsigned **ptr_addr)
1309{
1310 int i;
1311 struct ptable *ptable;
1312
1313 if ((ptable = flash_get_ptable()) && (ptable->count != 0)) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001314 *(*ptr_addr)++ = 2 + (ptable->count * (sizeof(struct atag_ptbl_entry) /
1315 sizeof(unsigned)));
Neeti Desaie245d492012-06-01 12:52:13 -07001316 *(*ptr_addr)++ = 0x4d534d70;
1317 for (i = 0; i < ptable->count; ++i)
1318 ptentry_to_tag(ptr_addr, ptable_get(ptable, i));
1319 }
1320
1321 return (*ptr_addr);
1322}
1323
1324unsigned *atag_cmdline(unsigned *ptr, const char *cmdline)
1325{
1326 int cmdline_length = 0;
1327 int n;
Neeti Desaie245d492012-06-01 12:52:13 -07001328 char *dest;
1329
Amol Jadi10c7d1c2013-01-25 13:24:29 -08001330 cmdline_length = strlen((const char*)cmdline);
Neeti Desaie245d492012-06-01 12:52:13 -07001331 n = (cmdline_length + 4) & (~3);
1332
1333 *ptr++ = (n / 4) + 2;
1334 *ptr++ = 0x54410009;
1335 dest = (char *) ptr;
Amol Jadi10c7d1c2013-01-25 13:24:29 -08001336 while ((*dest++ = *cmdline++));
Neeti Desaie245d492012-06-01 12:52:13 -07001337 ptr += (n / 4);
1338
1339 return ptr;
1340}
1341
1342unsigned *atag_end(unsigned *ptr)
1343{
Brian Swetland9c4c0752009-01-25 16:23:50 -08001344 /* END */
1345 *ptr++ = 0;
1346 *ptr++ = 0;
1347
Neeti Desaie245d492012-06-01 12:52:13 -07001348 return ptr;
1349}
1350
1351void generate_atags(unsigned *ptr, const char *cmdline,
1352 void *ramdisk, unsigned ramdisk_size)
1353{
vijay kumar21a37452015-12-29 16:23:21 +05301354 unsigned *orig_ptr = ptr;
Neeti Desaie245d492012-06-01 12:52:13 -07001355 ptr = atag_core(ptr);
1356 ptr = atag_ramdisk(ptr, ramdisk, ramdisk_size);
1357 ptr = target_atag_mem(ptr);
1358
1359 /* Skip NAND partition ATAGS for eMMC boot */
1360 if (!target_is_emmc_boot()){
1361 ptr = atag_ptable(&ptr);
1362 }
1363
vijay kumar21a37452015-12-29 16:23:21 +05301364 /*
1365 * Atags size filled till + cmdline size + 1 unsigned for 4-byte boundary + 4 unsigned
1366 * for atag identifier in atag_cmdline and atag_end should be with in MAX_TAGS_SIZE bytes
1367 */
Mayank Groverbc8a2ef2018-02-23 18:03:36 +05301368 if (!cmdline)
1369 return;
1370
vijay kumar21a37452015-12-29 16:23:21 +05301371 if (((ptr - orig_ptr) + strlen(cmdline) + 5 * sizeof(unsigned)) < MAX_TAGS_SIZE) {
1372 ptr = atag_cmdline(ptr, cmdline);
1373 ptr = atag_end(ptr);
1374 }
1375 else {
1376 dprintf(CRITICAL,"Crossing ATAGs Max size allowed\n");
1377 ASSERT(0);
1378 }
Neeti Desaie245d492012-06-01 12:52:13 -07001379}
1380
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001381typedef void entry_func_ptr(unsigned, unsigned, unsigned*);
Neeti Desaie245d492012-06-01 12:52:13 -07001382void boot_linux(void *kernel, unsigned *tags,
1383 const char *cmdline, unsigned machtype,
1384 void *ramdisk, unsigned ramdisk_size)
1385{
Amol Jadi10c7d1c2013-01-25 13:24:29 -08001386 unsigned char *final_cmdline;
Amol Jadib6be5c12012-11-14 13:39:51 -08001387#if DEVICE_TREE
Neeti Desai17379b82012-06-04 18:42:53 -07001388 int ret = 0;
Amol Jadib6be5c12012-11-14 13:39:51 -08001389#endif
1390
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001391 void (*entry)(unsigned, unsigned, unsigned*) = (entry_func_ptr*)(PA((addr_t)kernel));
Deepa Dinamani25a9f762012-11-30 15:57:15 -08001392 uint32_t tags_phys = PA((addr_t)tags);
vijay kumar1a50a642015-11-16 12:41:15 +05301393 struct kernel64_hdr *kptr = ((struct kernel64_hdr*)(PA((addr_t)kernel)));
Deepa Dinamani25a9f762012-11-30 15:57:15 -08001394
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301395 ramdisk = (void *)PA((addr_t)ramdisk);
Neeti Desaie245d492012-06-01 12:52:13 -07001396
Amol Jadi10c7d1c2013-01-25 13:24:29 -08001397 final_cmdline = update_cmdline((const char*)cmdline);
1398
Neeti Desai17379b82012-06-04 18:42:53 -07001399#if DEVICE_TREE
Amol Jadib6be5c12012-11-14 13:39:51 -08001400 dprintf(INFO, "Updating device tree: start\n");
1401
Neeti Desai17379b82012-06-04 18:42:53 -07001402 /* Update the Device Tree */
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301403 ret = update_device_tree((void *)tags,(const char *)final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -07001404 if(ret)
1405 {
1406 dprintf(CRITICAL, "ERROR: Updating Device Tree Failed \n");
1407 ASSERT(0);
1408 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001409 dprintf(INFO, "Updating device tree: done\n");
Neeti Desai17379b82012-06-04 18:42:53 -07001410#else
Neeti Desaie245d492012-06-01 12:52:13 -07001411 /* Generating the Atags */
Amol Jadi10c7d1c2013-01-25 13:24:29 -08001412 generate_atags(tags, final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -07001413#endif
Neeti Desaie245d492012-06-01 12:52:13 -07001414
Monika Singh0ec6db82019-07-18 16:57:58 +05301415#if VERIFIED_BOOT
Monika Singhb0db4b82018-09-26 12:18:02 +05301416 if (VB_M <= target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +05301417 {
1418 if (device.verity_mode == 0) {
lijuangbdd9bb42016-03-01 18:22:17 +08001419#if FBCON_DISPLAY_MSG
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07001420#if ENABLE_VB_ATTEST
Mayank Grover889be1b2017-09-12 20:12:23 +05301421 display_bootverify_menu(DISPLAY_MENU_EIO);
1422 wait_for_users_action();
1423 if(!pwr_key_is_pressed)
1424 shutdown_device();
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07001425#else
Mayank Grover889be1b2017-09-12 20:12:23 +05301426 display_bootverify_menu(DISPLAY_MENU_LOGGING);
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07001427#endif
Mayank Grover889be1b2017-09-12 20:12:23 +05301428 wait_for_users_action();
lijuangbdd9bb42016-03-01 18:22:17 +08001429#else
Mayank Grover889be1b2017-09-12 20:12:23 +05301430 dprintf(CRITICAL,
1431 "The dm-verity is not started in enforcing mode.\nWait for 5 seconds before proceeding\n");
1432 mdelay(5000);
lijuangbdd9bb42016-03-01 18:22:17 +08001433#endif
Mayank Grover889be1b2017-09-12 20:12:23 +05301434 }
lijuangbdd9bb42016-03-01 18:22:17 +08001435 }
lijuangbdd9bb42016-03-01 18:22:17 +08001436#endif
1437
1438#if VERIFIED_BOOT
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001439 /* Write protect the device info */
Channagoud Kadabi3bd9d1e2015-05-05 16:18:20 -07001440 if (!boot_into_recovery && target_build_variant_user() && devinfo_present && mmc_write_protect("devinfo", 1))
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001441 {
1442 dprintf(INFO, "Failed to write protect dev info\n");
1443 ASSERT(0);
1444 }
1445#endif
1446
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08001447 /* Turn off splash screen if enabled */
1448#if DISPLAY_SPLASH_SCREEN
1449 target_display_shutdown();
1450#endif
1451
Veera Sundaram Sankaran67ea0932015-09-25 10:09:30 -07001452 /* Perform target specific cleanup */
1453 target_uninit();
Monika Singh292b3e92018-03-17 22:40:23 +05301454 free_verified_boot_resource(&info);
1455 if (final_cmdline)
1456 free(final_cmdline);
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08001457
Deepa Dinamani33734bc2013-03-06 12:16:06 -08001458 dprintf(INFO, "booting linux @ %p, ramdisk @ %p (%d), tags/device tree @ %p\n",
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301459 entry, ramdisk, ramdisk_size, (void *)tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001460
1461 enter_critical_section();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001462
Amol Jadi4421e652011-06-16 15:00:48 -07001463 /* do any platform specific cleanup before kernel entry */
1464 platform_uninit();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001465
Brian Swetland9c4c0752009-01-25 16:23:50 -08001466 arch_disable_cache(UCACHE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001467
Amol Jadi504f9fe2012-08-16 13:56:48 -07001468#if ARM_WITH_MMU
Brian Swetland9c4c0752009-01-25 16:23:50 -08001469 arch_disable_mmu();
Amol Jadi504f9fe2012-08-16 13:56:48 -07001470#endif
Amol Jadi492d5a52013-03-15 16:12:34 -07001471 bs_set_timestamp(BS_KERNEL_ENTRY);
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001472
1473 if (IS_ARM64(kptr))
1474 /* Jump to a 64bit kernel */
1475 scm_elexec_call((paddr_t)kernel, tags_phys);
1476 else
1477 /* Jump to a 32bit kernel */
1478 entry(0, machtype, (unsigned*)tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001479}
1480
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001481/* Function to check if the memory address range falls within the aboot
1482 * boundaries.
1483 * start: Start of the memory region
1484 * size: Size of the memory region
1485 */
Vijay Kumar Pendoti9c002ad2016-03-09 13:52:45 +05301486int check_aboot_addr_range_overlap(uintptr_t start, uint32_t size)
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001487{
1488 /* Check for boundary conditions. */
Sundarajan Srinivasance2a0ea2013-12-16 17:02:56 -08001489 if ((UINT_MAX - start) < size)
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001490 return -1;
1491
1492 /* Check for memory overlap. */
1493 if ((start < MEMBASE) && ((start + size) <= MEMBASE))
1494 return 0;
Channagoud Kadabi94143912013-10-15 12:53:52 -07001495 else if (start >= (MEMBASE + MEMSIZE))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001496 return 0;
1497 else
1498 return -1;
1499}
1500
Mayank Grovere559cec2017-10-17 15:12:03 +05301501/* Function to check if the memory address range falls beyond ddr region.
1502 * start: Start of the memory region
1503 * size: Size of the memory region
1504 */
1505int check_ddr_addr_range_bound(uintptr_t start, uint32_t size)
1506{
1507 uintptr_t ddr_pa_start_addr = PA(get_ddr_start());
1508 uint64_t ddr_size = smem_get_ddr_size();
1509 uint64_t ddr_pa_end_addr = ddr_pa_start_addr + ddr_size;
1510 uintptr_t pa_start_addr = PA(start);
1511
1512 /* Check for boundary conditions. */
1513 if ((UINT_MAX - start) < size)
1514 return -1;
1515
1516 /* Check if memory range is beyond the ddr range. */
1517 if (pa_start_addr < ddr_pa_start_addr ||
1518 pa_start_addr >= (ddr_pa_end_addr) ||
1519 (pa_start_addr + size) > ddr_pa_end_addr)
1520 return -1;
1521 else
1522 return 0;
1523}
1524
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001525BUF_DMA_ALIGN(buf, BOOT_IMG_MAX_PAGE_SIZE); //Equal to max-supported pagesize
Dima Zavin214cc642009-01-26 11:16:21 -08001526
Mayank Groverdd080e82018-09-04 20:12:13 +05301527int getimage(void **image_buffer, uint32_t *imgsize,
1528 const char *imgname)
Monika Singh292b3e92018-03-17 22:40:23 +05301529{
Mayank Groverdd080e82018-09-04 20:12:13 +05301530 uint32_t loadedindex;
1531 if (image_buffer == NULL || imgsize == NULL ||
Monika Singh292b3e92018-03-17 22:40:23 +05301532 imgname == NULL) {
1533 dprintf(CRITICAL, "getimage: invalid parameters\n");
1534 return -1;
1535 }
Mayank Groverdd080e82018-09-04 20:12:13 +05301536 for (loadedindex = 0; loadedindex < info.num_loaded_images; loadedindex++) {
1537 if (!strncmp(info.images[loadedindex].name, imgname,
Monika Singh292b3e92018-03-17 22:40:23 +05301538 strlen(imgname))) {
Mayank Groverdd080e82018-09-04 20:12:13 +05301539 *image_buffer = info.images[loadedindex].image_buffer;
1540 *imgsize = info.images[loadedindex].imgsize;
Mayank Grover027a9412018-09-04 15:12:05 +05301541 dprintf(SPEW, "getimage(): Loaded image [%s|%d]\n",
1542 info.images[loadedindex].name,
1543 info.images[loadedindex].imgsize);
Monika Singh292b3e92018-03-17 22:40:23 +05301544 return 0;
1545 }
1546 }
1547 return -1;
1548}
1549
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001550static void verify_signed_bootimg(uint32_t bootimg_addr, uint32_t bootimg_size)
1551{
1552 int ret;
Channagoud Kadabia8c623f2015-01-13 14:48:48 -08001553
1554#if !VERIFIED_BOOT
Sundarajan Srinivasance54d6e2013-11-11 12:45:00 -08001555#if IMAGE_VERIF_ALGO_SHA1
1556 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA1;
1557#else
1558 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA256;
1559#endif
Channagoud Kadabia8c623f2015-01-13 14:48:48 -08001560#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001561
1562 /* Assume device is rooted at this time. */
1563 device.is_tampered = 1;
1564
1565 dprintf(INFO, "Authenticating boot image (%d): start\n", bootimg_size);
1566
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001567#if VERIFIED_BOOT
Monika Singh7d2fc272018-03-16 17:16:01 +05301568 uint32_t bootstate;
1569 if(boot_into_recovery &&
Mayank Grover68fbf0d2017-10-24 14:13:39 +05301570 (!partition_multislot_is_supported()))
Monika Singh7d2fc272018-03-16 17:16:01 +05301571 {
1572 ret = boot_verify_image((unsigned char *)bootimg_addr,
1573 bootimg_size, "/recovery", &bootstate);
1574 }
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001575 else
Monika Singh7d2fc272018-03-16 17:16:01 +05301576 {
1577 ret = boot_verify_image((unsigned char *)bootimg_addr,
1578 bootimg_size, "/boot", &bootstate);
1579 }
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001580 boot_verify_print_state();
1581#else
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001582 ret = image_verify((unsigned char *)bootimg_addr,
1583 (unsigned char *)(bootimg_addr + bootimg_size),
1584 bootimg_size,
Sundarajan Srinivasance54d6e2013-11-11 12:45:00 -08001585 auth_algo);
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001586#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001587 dprintf(INFO, "Authenticating boot image: done return value = %d\n", ret);
1588
1589 if (ret)
1590 {
1591 /* Authorized kernel */
1592 device.is_tampered = 0;
Sundarajan Srinivasan3fb21f12013-09-16 18:36:15 -07001593 auth_kernel_img = 1;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001594 }
1595
Amit Blay4aa292f2015-04-28 21:55:59 +03001596#ifdef MDTP_SUPPORT
1597 {
1598 /* Verify MDTP lock.
1599 * For boot & recovery partitions, use aboot's verification result.
1600 */
1601 mdtp_ext_partition_verification_t ext_partition;
1602 ext_partition.partition = boot_into_recovery ? MDTP_PARTITION_RECOVERY : MDTP_PARTITION_BOOT;
1603 ext_partition.integrity_state = device.is_tampered ? MDTP_PARTITION_STATE_INVALID : MDTP_PARTITION_STATE_VALID;
1604 ext_partition.page_size = 0; /* Not needed since already validated */
1605 ext_partition.image_addr = 0; /* Not needed since already validated */
1606 ext_partition.image_size = 0; /* Not needed since already validated */
1607 ext_partition.sig_avail = FALSE; /* Not needed since already validated */
1608 mdtp_fwlock_verify_lock(&ext_partition);
1609 }
1610#endif /* MDTP_SUPPORT */
1611
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001612#if USE_PCOM_SECBOOT
1613 set_tamper_flag(device.is_tampered);
1614#endif
1615
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001616#if VERIFIED_BOOT
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001617 switch(boot_verify_get_state())
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001618 {
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001619 case RED:
lijuanga40d6302015-07-20 20:10:13 +08001620#if FBCON_DISPLAY_MSG
lijuang9a7d3b92015-11-30 14:41:24 +08001621 display_bootverify_menu(DISPLAY_MENU_RED);
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07001622#if ENABLE_VB_ATTEST
1623 mdelay(DELAY_WAIT);
1624 shutdown_device();
1625#else
lijuanga40d6302015-07-20 20:10:13 +08001626 wait_for_users_action();
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07001627#endif
lijuanga40d6302015-07-20 20:10:13 +08001628#else
1629 dprintf(CRITICAL,
1630 "Your device has failed verification and may not work properly.\nWait for 5 seconds before proceeding\n");
1631 mdelay(5000);
1632#endif
1633
1634 break;
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001635 case YELLOW:
lijuanga40d6302015-07-20 20:10:13 +08001636#if FBCON_DISPLAY_MSG
lijuang9a7d3b92015-11-30 14:41:24 +08001637 display_bootverify_menu(DISPLAY_MENU_YELLOW);
lijuanga40d6302015-07-20 20:10:13 +08001638 wait_for_users_action();
1639#else
1640 dprintf(CRITICAL,
1641 "Your device has loaded a different operating system.\nWait for 5 seconds before proceeding\n");
1642 mdelay(5000);
1643#endif
1644 break;
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001645 default:
lijuanga40d6302015-07-20 20:10:13 +08001646 break;
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001647 }
1648#endif
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001649#if !VERIFIED_BOOT
Unnati Gandhi1be04752015-03-27 19:41:53 +05301650 if(device.is_tampered)
1651 {
1652 write_device_info_mmc(&device);
1653 #ifdef TZ_TAMPER_FUSE
Monika Singh98257462018-06-06 11:28:49 +05301654 set_tamper_fuse_cmd(HLOS_IMG_TAMPER_FUSE);
Unnati Gandhi1be04752015-03-27 19:41:53 +05301655 #endif
1656 #ifdef ASSERT_ON_TAMPER
1657 dprintf(CRITICAL, "Device is tampered. Asserting..\n");
1658 ASSERT(0);
1659 #endif
1660 }
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001661#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001662}
1663
Maunik Shah0f3c8ac2014-03-02 17:47:58 +05301664static bool check_format_bit()
1665{
1666 bool ret = false;
1667 int index;
1668 uint64_t offset;
1669 struct boot_selection_info *in = NULL;
1670 char *buf = NULL;
1671
1672 index = partition_get_index("bootselect");
1673 if (index == INVALID_PTN)
1674 {
1675 dprintf(INFO, "Unable to locate /bootselect partition\n");
1676 return ret;
1677 }
1678 offset = partition_get_offset(index);
1679 if(!offset)
1680 {
1681 dprintf(INFO, "partition /bootselect doesn't exist\n");
1682 return ret;
1683 }
1684 buf = (char *) memalign(CACHE_LINE, ROUNDUP(page_size, CACHE_LINE));
Mayank Grover48860402016-11-29 12:34:53 +05301685 mmc_set_lun(partition_get_lun(index));
Maunik Shah0f3c8ac2014-03-02 17:47:58 +05301686 ASSERT(buf);
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301687 if (mmc_read(offset, (uint32_t *)buf, page_size))
Maunik Shah0f3c8ac2014-03-02 17:47:58 +05301688 {
1689 dprintf(INFO, "mmc read failure /bootselect %d\n", page_size);
1690 free(buf);
1691 return ret;
1692 }
1693 in = (struct boot_selection_info *) buf;
1694 if ((in->signature == BOOTSELECT_SIGNATURE) &&
1695 (in->version == BOOTSELECT_VERSION)) {
1696 if ((in->state_info & BOOTSELECT_FORMAT) &&
1697 !(in->state_info & BOOTSELECT_FACTORY))
1698 ret = true;
1699 } else {
1700 dprintf(CRITICAL, "Signature: 0x%08x or version: 0x%08x mismatched of /bootselect\n",
1701 in->signature, in->version);
1702 ASSERT(0);
1703 }
1704 free(buf);
1705 return ret;
1706}
1707
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001708void boot_verifier_init()
1709{
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001710 uint32_t boot_state;
1711 /* Check if device unlock */
1712 if(device.is_unlocked)
1713 {
1714 boot_verify_send_event(DEV_UNLOCK);
1715 boot_verify_print_state();
1716 dprintf(CRITICAL, "Device is unlocked! Skipping verification...\n");
1717 return;
1718 }
1719 else
1720 {
1721 boot_verify_send_event(BOOT_INIT);
1722 }
1723
1724 /* Initialize keystore */
1725 boot_state = boot_verify_keystore_init();
1726 if(boot_state == YELLOW)
1727 {
1728 boot_verify_print_state();
1729 dprintf(CRITICAL, "Keystore verification failed! Continuing anyways...\n");
1730 }
1731}
1732
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301733/* Function to return recovery appended dtbo buffer info */
1734void get_recovery_dtbo_info(uint32_t *dtbo_size, void **dtbo_buf)
1735{
1736 *dtbo_size = recovery_dtbo_size;
1737 *dtbo_buf = recovery_dtbo_buf;
1738 return;
1739}
1740
Shashank Mittal23b8f422010-04-16 19:27:21 -07001741int boot_linux_from_mmc(void)
1742{
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301743 boot_img_hdr *hdr = (void*) buf;
1744 boot_img_hdr *uhdr;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001745 unsigned offset = 0;
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07001746 int rcode;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001747 unsigned long long ptn = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001748 int index = INVALID_PTN;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001749
Shashank Mittalcd98d472011-08-02 14:29:24 -07001750 unsigned char *image_addr = 0;
1751 unsigned kernel_actual;
1752 unsigned ramdisk_actual;
1753 unsigned imagesize_actual;
Neeti Desai465491e2012-07-31 12:53:35 -07001754 unsigned second_actual = 0;
Parth Dixit1375d9e2019-07-08 20:56:13 +05301755 void * image_buf = NULL;
Neeti Desai465491e2012-07-31 12:53:35 -07001756
Matthew Qin271927e2015-03-31 22:07:07 -04001757 unsigned int dtb_size = 0;
Parth Dixit1375d9e2019-07-08 20:56:13 +05301758 unsigned dtb_image_size = 0;
1759 uint32_t dtb_image_offset = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08001760 unsigned int out_len = 0;
1761 unsigned int out_avai_len = 0;
1762 unsigned char *out_addr = NULL;
1763 uint32_t dtb_offset = 0;
1764 unsigned char *kernel_start_addr = NULL;
1765 unsigned int kernel_size = 0;
Ameya Thakur10a33452016-06-13 14:24:26 -07001766 unsigned int patched_kernel_hdr_size = 0;
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301767 uint64_t image_size = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08001768 int rc;
Monika Singh292b3e92018-03-17 22:40:23 +05301769#if VERIFIED_BOOT_2
1770 int status;
Mayank Grover027a9412018-09-04 15:12:05 +05301771 void *dtbo_image_buf = NULL;
1772 uint32_t dtbo_image_sz = 0;
Mayank Grover9df84c02018-08-30 15:46:35 +05301773 void *vbmeta_image_buf = NULL;
1774 uint32_t vbmeta_image_sz = 0;
Monika Singh292b3e92018-03-17 22:40:23 +05301775#endif
Mayank Groverc93cad82017-10-03 12:23:45 +05301776 char *ptn_name = NULL;
Neeti Desai465491e2012-07-31 12:53:35 -07001777#if DEVICE_TREE
1778 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -07001779 struct dt_entry dt_entry;
Neeti Desai465491e2012-07-31 12:53:35 -07001780 unsigned dt_table_offset;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001781 uint32_t dt_actual;
Deepa Dinamani19648b42013-09-05 17:05:55 -07001782 uint32_t dt_hdr_size;
Matthew Qin271927e2015-03-31 22:07:07 -04001783 unsigned char *best_match_dt_addr = NULL;
Neeti Desai465491e2012-07-31 12:53:35 -07001784#endif
Matthew Qin49e51fa2015-02-09 10:40:45 +08001785 struct kernel64_hdr *kptr = NULL;
Mayank Grover351a75e2017-05-30 20:06:08 +05301786 int current_active_slot = INVALID;
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001787
Maunik Shah0f3c8ac2014-03-02 17:47:58 +05301788 if (check_format_bit())
1789 boot_into_recovery = 1;
1790
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07001791 if (!boot_into_recovery) {
1792 memset(ffbm_mode_string, '\0', sizeof(ffbm_mode_string));
1793 rcode = get_ffbm(ffbm_mode_string, sizeof(ffbm_mode_string));
1794 if (rcode <= 0) {
1795 boot_into_ffbm = false;
1796 if (rcode < 0)
1797 dprintf(CRITICAL,"failed to get ffbm cookie");
1798 } else
1799 boot_into_ffbm = true;
1800 } else
1801 boot_into_ffbm = false;
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301802 uhdr = (boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001803 if (!memcmp(uhdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1804 dprintf(INFO, "Unified boot method!\n");
1805 hdr = uhdr;
1806 goto unified_boot;
1807 }
Mayank Groverc93cad82017-10-03 12:23:45 +05301808
1809 /* For a/b recovery image code is on boot partition.
1810 If we support multislot, always use boot partition. */
1811 if (boot_into_recovery &&
Mayank Grover8ab4a762019-04-25 17:23:34 +05301812 ((!partition_multislot_is_supported()) ||
1813 (target_dynamic_partition_supported())))
Mayank Groverc93cad82017-10-03 12:23:45 +05301814 ptn_name = "recovery";
1815 else
1816 ptn_name = "boot";
1817
1818 index = partition_get_index(ptn_name);
1819 ptn = partition_get_offset(index);
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301820 image_size = partition_get_size(index);
1821 if(ptn == 0 || image_size == 0) {
Mayank Groverc93cad82017-10-03 12:23:45 +05301822 dprintf(CRITICAL, "ERROR: No %s partition found\n", ptn_name);
1823 return -1;
Kinson Chikf1a43512011-07-14 11:28:39 -07001824 }
Mayank Groverc93cad82017-10-03 12:23:45 +05301825
Channagoud Kadabief0547c2015-02-10 12:57:38 -08001826 /* Set Lun for boot & recovery partitions */
1827 mmc_set_lun(partition_get_lun(index));
Shashank Mittal23b8f422010-04-16 19:27:21 -07001828
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301829 if (mmc_read(ptn + offset, (uint32_t *) buf, page_size)) {
Shashank Mittal23b8f422010-04-16 19:27:21 -07001830 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
1831 return -1;
1832 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001833
1834 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001835 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Mayank Grover351a75e2017-05-30 20:06:08 +05301836 return ERR_INVALID_BOOT_MAGIC;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001837 }
1838
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07001839 if (hdr->page_size && (hdr->page_size != page_size)) {
vijay kumar2e21b3a2014-06-26 17:40:15 +05301840
1841 if (hdr->page_size > BOOT_IMG_MAX_PAGE_SIZE) {
1842 dprintf(CRITICAL, "ERROR: Invalid page size\n");
1843 return -1;
1844 }
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07001845 page_size = hdr->page_size;
1846 page_mask = page_size - 1;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001847 }
1848
Matthew Qin49e51fa2015-02-09 10:40:45 +08001849 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1850 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Kishor PKd8ddcad2017-07-27 13:53:57 +05301851 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001852
Matthew Qin49e51fa2015-02-09 10:40:45 +08001853 image_addr = (unsigned char *)target_get_scratch_address();
Mayank Grover6fd36792018-09-10 13:26:58 +05301854#if VERIFIED_BOOT_2
1855 /* Create hole in start of image for VB salt to copy */
1856 image_addr += SALT_BUFF_OFFSET;
1857#endif
Kishor PK9e91b592017-05-24 12:14:55 +05301858 memcpy(image_addr, (void *)buf, page_size);
1859
1860 /* ensure commandline is terminated */
1861 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
Matthew Qin49e51fa2015-02-09 10:40:45 +08001862
1863#if DEVICE_TREE
Parth Dixit4097b622016-03-15 14:42:27 +05301864#ifndef OSVERSION_IN_BOOTIMAGE
1865 dt_size = hdr->dt_size;
Mayank Grover5a502582018-09-12 11:24:49 +05301866#else
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301867 dprintf(INFO, "BootImage Header: %d\n", hdr->header_version);
Mayank Grover5a502582018-09-12 11:24:49 +05301868#endif
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301869
Parth Dixit4097b622016-03-15 14:42:27 +05301870 dt_actual = ROUND_TO_PAGE(dt_size, page_mask);
Kishor PKd8ddcad2017-07-27 13:53:57 +05301871 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 +05301872 dprintf(CRITICAL, "Integer overflow detected in bootimage header fields at %u in %s\n",__LINE__,__FILE__);
1873 return -1;
1874 }
Kishor PKd8ddcad2017-07-27 13:53:57 +05301875 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + second_actual + dt_actual);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001876#else
Kishor PKd8ddcad2017-07-27 13:53:57 +05301877 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 +05301878 dprintf(CRITICAL, "Integer overflow detected in bootimage header fields at %u in %s\n",__LINE__,__FILE__);
1879 return -1;
1880 }
Kishor PKd8ddcad2017-07-27 13:53:57 +05301881 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + second_actual);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001882#endif
Parth Dixit1375d9e2019-07-08 20:56:13 +05301883 dtb_image_size = hdr->kernel_size;
Matthew Qin49e51fa2015-02-09 10:40:45 +08001884
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301885#ifdef OSVERSION_IN_BOOTIMAGE
1886 /* If header version is ONE and booting into recovery,
1887 dtbo is appended with recovery image.
1888 Doing following:
1889 * Validating the recovery offset and size.
1890 * Extracting recovery dtbo to be used as dtbo.
1891 */
1892 if (boot_into_recovery &&
1893 hdr->header_version == BOOT_HEADER_VERSION_ONE)
1894 {
1895 struct boot_img_hdr_v1 *hdr1 =
1896 (struct boot_img_hdr_v1 *) (image_addr + sizeof(boot_img_hdr));
Mayank Grovera1a83c72018-09-24 11:23:15 +05301897 unsigned int recovery_dtbo_actual = 0;
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301898
Mayank Grovera1a83c72018-09-24 11:23:15 +05301899 recovery_dtbo_actual = ROUND_TO_PAGE(hdr1->recovery_dtbo_size, page_mask);
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301900 if ((hdr1->header_size !=
1901 sizeof(struct boot_img_hdr_v1) + sizeof(boot_img_hdr)))
1902 {
1903 dprintf(CRITICAL, "Invalid boot image header: %d\n", hdr1->header_size);
1904 return -1;
1905 }
1906
Mayank Grovera1a83c72018-09-24 11:23:15 +05301907 if (recovery_dtbo_actual > MAX_SUPPORTED_DTBO_IMG_BUF)
1908 {
1909 dprintf(CRITICAL, "Recovery Dtbo Size too big %x, Allowed size %x\n", recovery_dtbo_actual,
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301910 MAX_SUPPORTED_DTBO_IMG_BUF);
1911 return -1;
1912 }
1913
Mayank Grovera1a83c72018-09-24 11:23:15 +05301914 if (UINT_MAX < ((uint64_t)imagesize_actual + recovery_dtbo_actual))
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301915 {
1916 dprintf(CRITICAL, "Integer overflow detected in recoveryimage header fields at %u in %s\n",__LINE__,__FILE__);
1917 return -1;
1918 }
1919
Mayank Grovera1a83c72018-09-24 11:23:15 +05301920 if (UINT_MAX < (hdr1->recovery_dtbo_offset + recovery_dtbo_actual)) {
1921 dprintf(CRITICAL,
1922 "Integer overflow detected in recovery image header fields at %u in %s\n",__LINE__,__FILE__);
1923 return -1;
1924 }
1925
1926 if (hdr1->recovery_dtbo_offset + recovery_dtbo_actual > image_size)
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301927 {
1928 dprintf(CRITICAL, "Invalid recovery dtbo: Recovery Dtbo Offset=0x%llx,"
1929 " Recovery Dtbo Size=0x%x, Image Size=0x%llx\n",
1930 hdr1->recovery_dtbo_offset, recovery_dtbo_size, image_size);
1931 return -1;
1932 }
1933
1934 recovery_dtbo_buf = (void *)(hdr1->recovery_dtbo_offset + image_addr);
Mayank Grovera1a83c72018-09-24 11:23:15 +05301935 recovery_dtbo_size = recovery_dtbo_actual;
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301936 imagesize_actual += recovery_dtbo_size;
1937
1938 dprintf(SPEW, "Header version: %d\n", hdr->header_version);
1939 dprintf(SPEW, "Recovery Dtbo Size 0x%x\n", recovery_dtbo_size);
1940 dprintf(SPEW, "Recovery Dtbo Offset 0x%llx\n", hdr1->recovery_dtbo_offset);
1941
1942 }
Parth Dixit1375d9e2019-07-08 20:56:13 +05301943
1944 if ( hdr->header_version == BOOT_HEADER_VERSION_TWO) {
1945 struct boot_img_hdr_v1 *hdr1 =
1946 (struct boot_img_hdr_v1 *) (image_addr + sizeof(boot_img_hdr));
1947 struct boot_img_hdr_v2 *hdr2 = (struct boot_img_hdr_v2 *)
1948 (image_addr + sizeof(boot_img_hdr) +
1949 BOOT_IMAGE_HEADER_V2_OFFSET);
1950 unsigned int recovery_dtbo_actual = 0;
1951
lijuang0b17ba22019-09-04 14:20:47 +08001952 recovery_dtbo_actual =
1953 ROUND_TO_PAGE(hdr1->recovery_dtbo_size, page_mask);
1954 imagesize_actual += recovery_dtbo_actual;
Parth Dixit1375d9e2019-07-08 20:56:13 +05301955
1956 imagesize_actual += ROUND_TO_PAGE(hdr2->dtb_size, page_mask);
1957
1958
1959 dtb_image_offset = page_size + patched_kernel_hdr_size +
1960 kernel_actual + ramdisk_actual + second_actual +
1961 recovery_dtbo_actual;
1962
1963 dprintf(SPEW, "Header version: %d\n", hdr->header_version);
1964 dprintf(SPEW, "Dtb image offset 0x%x\n", dtb_image_offset);
1965 }
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301966#endif
1967
Parth Dixit1375d9e2019-07-08 20:56:13 +05301968
Saranya Chidurab1aaf232018-11-19 15:47:50 +05301969 /* Validate the boot/recovery image size is within the bounds of partition size */
1970 if (imagesize_actual > image_size) {
1971 dprintf(CRITICAL, "Image size is greater than partition size.\n");
1972 return -1;
1973 }
1974
Matthew Qin49e51fa2015-02-09 10:40:45 +08001975#if VERIFIED_BOOT
1976 boot_verifier_init();
1977#endif
1978
Mayank Grover1ceaee22019-04-01 17:54:32 +05301979#if VERIFIED_BOOT_2
1980 /* read full partition if device is unlocked */
1981 if (device.is_unlocked)
1982 imagesize_actual = image_size;
1983#endif
1984
Vijay Kumar Pendoti9c002ad2016-03-09 13:52:45 +05301985 if (check_aboot_addr_range_overlap((uintptr_t) image_addr, imagesize_actual))
Matthew Qin49e51fa2015-02-09 10:40:45 +08001986 {
1987 dprintf(CRITICAL, "Boot image buffer address overlaps with aboot addresses.\n");
1988 return -1;
1989 }
1990
Matthew Qinbb7923d2015-02-09 10:56:09 +08001991 /*
1992 * Update loading flow of bootimage to support compressed/uncompressed
1993 * bootimage on both 64bit and 32bit platform.
1994 * 1. Load bootimage from emmc partition onto DDR.
1995 * 2. Check if bootimage is gzip format. If yes, decompress compressed kernel
1996 * 3. Check kernel header and update kernel load addr for 64bit and 32bit
1997 * platform accordingly.
1998 * 4. Sanity Check on kernel_addr and ramdisk_addr and copy data.
1999 */
Mayank Grover351a75e2017-05-30 20:06:08 +05302000 if (partition_multislot_is_supported())
2001 {
2002 current_active_slot = partition_find_active_slot();
2003 dprintf(INFO, "Loading boot image (%d) active_slot(%s): start\n",
2004 imagesize_actual, SUFFIX_SLOT(current_active_slot));
2005 }
2006 else
2007 {
2008 dprintf(INFO, "Loading (%s) image (%d): start\n",
2009 (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual);
2010 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08002011 bs_set_timestamp(BS_KERNEL_LOAD_START);
2012
Gaurav Nebhwani43e2a462016-03-17 21:32:56 +05302013 if ((target_get_max_flash_size() - page_size) < imagesize_actual)
2014 {
2015 dprintf(CRITICAL, "booimage size is greater than DDR can hold\n");
2016 return -1;
2017 }
Kishor PK9e91b592017-05-24 12:14:55 +05302018 offset = page_size;
2019 /* Read image without signature and header*/
2020 if (mmc_read(ptn + offset, (void *)(image_addr + offset), imagesize_actual - page_size))
Matthew Qin49e51fa2015-02-09 10:40:45 +08002021 {
2022 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
2023 return -1;
2024 }
2025
Mayank Grover351a75e2017-05-30 20:06:08 +05302026 if (partition_multislot_is_supported())
2027 {
2028 dprintf(INFO, "Loading boot image (%d) active_slot(%s): done\n",
2029 imagesize_actual, SUFFIX_SLOT(current_active_slot));
2030 }
2031 else
2032 {
2033 dprintf(INFO, "Loading (%s) image (%d): done\n",
Channagoud Kadabif0705b52015-08-20 14:16:08 -07002034 (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual);
2035
Mayank Grover351a75e2017-05-30 20:06:08 +05302036 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08002037 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
2038
2039 /* Authenticate Kernel */
2040 dprintf(INFO, "use_signed_kernel=%d, is_unlocked=%d, is_tampered=%d.\n",
2041 (int) target_use_signed_kernel(),
2042 device.is_unlocked,
2043 device.is_tampered);
Monika Singh292b3e92018-03-17 22:40:23 +05302044#if VERIFIED_BOOT_2
Mayank Grover1ceaee22019-04-01 17:54:32 +05302045 /* if device is unlocked skip reading signature, as full partition is read */
2046 if (!device.is_unlocked)
Monika Singh292b3e92018-03-17 22:40:23 +05302047 {
Mayank Grover1ceaee22019-04-01 17:54:32 +05302048 offset = imagesize_actual;
2049 if (check_aboot_addr_range_overlap((uintptr_t)image_addr + offset, page_size))
2050 {
2051 dprintf(CRITICAL, "Signature read buffer address overlaps with aboot addresses.\n");
2052 return -1;
2053 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08002054
Mayank Grover1ceaee22019-04-01 17:54:32 +05302055 /* Read signature */
2056 if(mmc_read(ptn + offset, (void *)(image_addr + offset), page_size))
2057 {
2058 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
2059 return -1;
2060 }
Monika Singh292b3e92018-03-17 22:40:23 +05302061 }
2062
Mayank Grover027a9412018-09-04 15:12:05 +05302063 /* load and validate dtbo partition */
2064 load_validate_dtbo_image(&dtbo_image_buf, &dtbo_image_sz);
2065
Mayank Grover9df84c02018-08-30 15:46:35 +05302066 /* load vbmeta partition */
2067 load_vbmeta_image(&vbmeta_image_buf, &vbmeta_image_sz);
2068
Monika Singh292b3e92018-03-17 22:40:23 +05302069 memset(&info, 0, sizeof(bootinfo));
Mayank Grover027a9412018-09-04 15:12:05 +05302070
2071 /* Pass loaded boot image passed */
Mayank Grover6fd36792018-09-10 13:26:58 +05302072 info.images[IMG_BOOT].image_buffer = SUB_SALT_BUFF_OFFSET(image_addr);
Mayank Grover027a9412018-09-04 15:12:05 +05302073 info.images[IMG_BOOT].imgsize = imagesize_actual;
2074 info.images[IMG_BOOT].name = ptn_name;
2075 ++info.num_loaded_images;
2076
2077 /* Pass loaded dtbo image */
2078 if (dtbo_image_buf != NULL) {
Mayank Grover6fd36792018-09-10 13:26:58 +05302079 info.images[IMG_DTBO].image_buffer =
2080 SUB_SALT_BUFF_OFFSET(dtbo_image_buf);
Mayank Grover027a9412018-09-04 15:12:05 +05302081 info.images[IMG_DTBO].imgsize = dtbo_image_sz;
2082 info.images[IMG_DTBO].name = "dtbo";
2083 ++info.num_loaded_images;
2084 }
2085
Mayank Grover9df84c02018-08-30 15:46:35 +05302086 /* Pass loaded vbmeta image */
2087 if (vbmeta_image_buf != NULL) {
2088 info.images[IMG_VBMETA].image_buffer = vbmeta_image_buf;
2089 info.images[IMG_VBMETA].imgsize = vbmeta_image_sz;
2090 info.images[IMG_VBMETA].name = "vbmeta";
2091 ++info.num_loaded_images;
2092 }
2093
Mayank Grovere1ab96c2018-09-04 20:31:31 +05302094 info.header_version = hdr->header_version;
Monika Singh292b3e92018-03-17 22:40:23 +05302095 info.multi_slot_boot = partition_multislot_is_supported();
2096 info.bootreason_alarm = boot_reason_alarm;
2097 info.bootinto_recovery = boot_into_recovery;
2098 status = load_image_and_auth(&info);
2099 if(status)
2100 return -1;
2101
2102 vbcmdline = info.vbcmdline;
Mayank Grover9df84c02018-08-30 15:46:35 +05302103
2104 /* Free the buffer allocated to vbmeta post verification */
Saranya Chidurab4933332018-10-15 17:30:06 +05302105 if (vbmeta_image_buf != NULL) {
2106 free(vbmeta_image_buf);
2107 --info.num_loaded_images;
2108 }
Monika Singh292b3e92018-03-17 22:40:23 +05302109#else
Channagoud Kadabi736c4962015-08-21 11:56:52 -07002110 /* Change the condition a little bit to include the test framework support.
2111 * We would never reach this point if device is in fastboot mode, even if we did
2112 * that means we are in test mode, so execute kernel authentication part for the
2113 * tests */
Parth Dixitcb0c6082015-12-30 15:01:13 +05302114 if((target_use_signed_kernel() && (!device.is_unlocked)) || is_test_mode_enabled())
Matthew Qin49e51fa2015-02-09 10:40:45 +08002115 {
2116 offset = imagesize_actual;
Vijay Kumar Pendoti9c002ad2016-03-09 13:52:45 +05302117 if (check_aboot_addr_range_overlap((uintptr_t)image_addr + offset, page_size))
Matthew Qin49e51fa2015-02-09 10:40:45 +08002118 {
2119 dprintf(CRITICAL, "Signature read buffer address overlaps with aboot addresses.\n");
2120 return -1;
2121 }
2122
2123 /* Read signature */
2124 if(mmc_read(ptn + offset, (void *)(image_addr + offset), page_size))
2125 {
2126 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
2127 return -1;
2128 }
2129
2130 verify_signed_bootimg((uint32_t)image_addr, imagesize_actual);
Channagoud Kadabi736c4962015-08-21 11:56:52 -07002131 /* The purpose of our test is done here */
Parth Dixitcb0c6082015-12-30 15:01:13 +05302132 if(is_test_mode_enabled() && auth_kernel_img)
Channagoud Kadabi736c4962015-08-21 11:56:52 -07002133 return 0;
Matthew Qin49e51fa2015-02-09 10:40:45 +08002134 } else {
2135 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
2136 #ifdef TZ_SAVE_KERNEL_HASH
2137 aboot_save_boot_hash_mmc((uint32_t) image_addr, imagesize_actual);
2138 #endif /* TZ_SAVE_KERNEL_HASH */
Amit Blay4aa292f2015-04-28 21:55:59 +03002139
2140#ifdef MDTP_SUPPORT
2141 {
2142 /* Verify MDTP lock.
2143 * For boot & recovery partitions, MDTP will use boot_verifier APIs,
2144 * since verification was skipped in aboot. The signature is not part of the loaded image.
2145 */
2146 mdtp_ext_partition_verification_t ext_partition;
2147 ext_partition.partition = boot_into_recovery ? MDTP_PARTITION_RECOVERY : MDTP_PARTITION_BOOT;
2148 ext_partition.integrity_state = MDTP_PARTITION_STATE_UNSET;
2149 ext_partition.page_size = page_size;
2150 ext_partition.image_addr = (uint32)image_addr;
2151 ext_partition.image_size = imagesize_actual;
2152 ext_partition.sig_avail = FALSE;
2153 mdtp_fwlock_verify_lock(&ext_partition);
2154 }
2155#endif /* MDTP_SUPPORT */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08002156 }
Monika Singh292b3e92018-03-17 22:40:23 +05302157#endif
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08002158
Sridhar Parasuram00bfedb2015-05-26 14:21:27 -07002159#if VERIFIED_BOOT
taozhang93088bd2016-11-16 15:50:46 +08002160 if((boot_verify_get_state() == ORANGE) && (!boot_into_ffbm))
Reut Zysmanba8a9d52016-02-18 11:44:04 +02002161 {
2162#if FBCON_DISPLAY_MSG
2163 display_bootverify_menu(DISPLAY_MENU_ORANGE);
2164 wait_for_users_action();
2165#else
2166 dprintf(CRITICAL,
2167 "Your device has been unlocked and can't be trusted.\nWait for 5 seconds before proceeding\n");
2168 mdelay(5000);
2169#endif
2170 }
2171#endif
2172
2173#if VERIFIED_BOOT
Monika Singh292b3e92018-03-17 22:40:23 +05302174 if (VB_M == target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +05302175 {
2176 /* set boot and system versions. */
2177 set_os_version((unsigned char *)image_addr);
2178 // send root of trust
2179 if(!send_rot_command((uint32_t)device.is_unlocked))
2180 ASSERT(0);
2181 }
Parth Dixitbc9b6492015-10-18 00:41:38 +05302182#endif
Channagoud Kadabia22144f2013-03-20 11:49:01 -07002183 /*
Matthew Qinbb7923d2015-02-09 10:56:09 +08002184 * Check if the kernel image is a gzip package. If yes, need to decompress it.
2185 * If not, continue booting.
2186 */
2187 if (is_gzip_package((unsigned char *)(image_addr + page_size), hdr->kernel_size))
2188 {
2189 out_addr = (unsigned char *)(image_addr + imagesize_actual + page_size);
2190 out_avai_len = target_get_max_flash_size() - imagesize_actual - page_size;
Mayank Grover027a9412018-09-04 15:12:05 +05302191#if VERIFIED_BOOT_2
2192 if (dtbo_image_sz)
2193 out_avai_len -= DTBO_IMG_BUF;
2194#endif
Matthew Qin0b15b322015-05-19 05:20:54 -04002195 dprintf(INFO, "decompressing kernel image: start\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08002196 rc = decompress((unsigned char *)(image_addr + page_size),
2197 hdr->kernel_size, out_addr, out_avai_len,
2198 &dtb_offset, &out_len);
2199 if (rc)
2200 {
Matthew Qin0b15b322015-05-19 05:20:54 -04002201 dprintf(CRITICAL, "decompressing kernel image failed!!!\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08002202 ASSERT(0);
2203 }
2204
Matthew Qin0b15b322015-05-19 05:20:54 -04002205 dprintf(INFO, "decompressing kernel image: done\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08002206 kptr = (struct kernel64_hdr *)out_addr;
2207 kernel_start_addr = out_addr;
2208 kernel_size = out_len;
2209 } else {
Ameya Thakur10a33452016-06-13 14:24:26 -07002210 dprintf(INFO, "Uncpmpressed kernel in use\n");
2211 if (!strncmp((char*)(image_addr + page_size),
2212 PATCHED_KERNEL_MAGIC,
2213 sizeof(PATCHED_KERNEL_MAGIC) - 1)) {
2214 dprintf(INFO, "Patched kernel detected\n");
2215 kptr = (struct kernel64_hdr *)(image_addr + page_size +
2216 PATCHED_KERNEL_HEADER_SIZE);
2217 //The size of the kernel is stored at start of kernel image + 16
2218 //The dtb would start just after the kernel
2219 dtb_offset = *((uint32_t*)((unsigned char*)
2220 (image_addr + page_size +
2221 sizeof(PATCHED_KERNEL_MAGIC) -
2222 1)));
2223 //The actual kernel starts after the 20 byte header.
2224 kernel_start_addr = (unsigned char*)(image_addr +
2225 page_size + PATCHED_KERNEL_HEADER_SIZE);
2226 kernel_size = hdr->kernel_size;
2227 patched_kernel_hdr_size = PATCHED_KERNEL_HEADER_SIZE;
2228 } else {
2229 dprintf(INFO, "Kernel image not patched..Unable to locate dt offset\n");
2230 kptr = (struct kernel64_hdr *)(image_addr + page_size);
2231 kernel_start_addr = (unsigned char *)(image_addr + page_size);
2232 kernel_size = hdr->kernel_size;
2233 }
Matthew Qinbb7923d2015-02-09 10:56:09 +08002234 }
2235
2236 /*
Channagoud Kadabia22144f2013-03-20 11:49:01 -07002237 * Update the kernel/ramdisk/tags address if the boot image header
2238 * has default values, these default values come from mkbootimg when
2239 * the boot image is flashed using fastboot flash:raw
2240 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08002241 update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr));
Channagoud Kadabia22144f2013-03-20 11:49:01 -07002242
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002243 /* Get virtual addresses since the hdr saves physical addresses. */
2244 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
2245 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
2246 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002247
Matthew Qinbb7923d2015-02-09 10:56:09 +08002248 kernel_size = ROUND_TO_PAGE(kernel_size, page_mask);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002249 /* Check if the addresses in the header are valid. */
Matthew Qinbb7923d2015-02-09 10:56:09 +08002250 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_size) ||
Mayank Grovere559cec2017-10-17 15:12:03 +05302251 check_ddr_addr_range_bound(hdr->kernel_addr, kernel_size) ||
2252 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual) ||
2253 check_ddr_addr_range_bound(hdr->ramdisk_addr, ramdisk_actual))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002254 {
Mayank Grovere559cec2017-10-17 15:12:03 +05302255 dprintf(CRITICAL, "kernel/ramdisk addresses are not valid.\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002256 return -1;
2257 }
2258
2259#ifndef DEVICE_TREE
Mayank Grovere559cec2017-10-17 15:12:03 +05302260 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE) ||
2261 check_ddr_addr_range_bound(hdr->tags_addr, MAX_TAGS_SIZE))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002262 {
Mayank Grovere559cec2017-10-17 15:12:03 +05302263 dprintf(CRITICAL, "Tags addresses are not valid.\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002264 return -1;
2265 }
2266#endif
2267
Matthew Qin49e51fa2015-02-09 10:40:45 +08002268 /* Move kernel, ramdisk and device tree to correct address */
Matthew Qinbb7923d2015-02-09 10:56:09 +08002269 memmove((void*) hdr->kernel_addr, kernel_start_addr, kernel_size);
Matthew Qin49e51fa2015-02-09 10:40:45 +08002270 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002271
Matthew Qin49e51fa2015-02-09 10:40:45 +08002272 #if DEVICE_TREE
Parth Dixit4097b622016-03-15 14:42:27 +05302273 if(dt_size) {
Matthew Qin49e51fa2015-02-09 10:40:45 +08002274 dt_table_offset = ((uint32_t)image_addr + page_size + kernel_actual + ramdisk_actual + second_actual);
2275 table = (struct dt_table*) dt_table_offset;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002276
Matthew Qin49e51fa2015-02-09 10:40:45 +08002277 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
2278 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
2279 return -1;
2280 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002281
P.V. Phani Kumar82916762016-03-09 09:20:19 +05302282 /* Its Error if, dt_hdr_size (table->num_entries * dt_entry size + Dev_Tree Header)
2283 goes beyound hdr->dt_size*/
Parth Dixit4097b622016-03-15 14:42:27 +05302284 if (dt_hdr_size > ROUND_TO_PAGE(dt_size,hdr->page_size)) {
P.V. Phani Kumar82916762016-03-09 09:20:19 +05302285 dprintf(CRITICAL, "ERROR: Invalid Device Tree size \n");
2286 return -1;
2287 }
2288
Matthew Qin49e51fa2015-02-09 10:40:45 +08002289 /* Find index of device tree within device tree table */
2290 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
2291 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
2292 return -1;
2293 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002294
P.V. Phani Kumar3c333202016-03-09 11:54:37 +05302295 if(dt_entry.offset > (UINT_MAX - dt_entry.size)) {
2296 dprintf(CRITICAL, "ERROR: Device tree contents are Invalid\n");
2297 return -1;
2298 }
2299
2300 /* Ensure we are not overshooting dt_size with the dt_entry selected */
Parth Dixit4097b622016-03-15 14:42:27 +05302301 if ((dt_entry.offset + dt_entry.size) > dt_size) {
P.V. Phani Kumar3c333202016-03-09 11:54:37 +05302302 dprintf(CRITICAL, "ERROR: Device tree contents are Invalid\n");
2303 return -1;
2304 }
2305
Matthew Qin271927e2015-03-31 22:07:07 -04002306 if (is_gzip_package((unsigned char *)dt_table_offset + dt_entry.offset, dt_entry.size))
2307 {
2308 unsigned int compressed_size = 0;
2309 out_addr += out_len;
2310 out_avai_len -= out_len;
Matthew Qin0b15b322015-05-19 05:20:54 -04002311 dprintf(INFO, "decompressing dtb: start\n");
Matthew Qin271927e2015-03-31 22:07:07 -04002312 rc = decompress((unsigned char *)dt_table_offset + dt_entry.offset,
2313 dt_entry.size, out_addr, out_avai_len,
2314 &compressed_size, &dtb_size);
2315 if (rc)
2316 {
Matthew Qin0b15b322015-05-19 05:20:54 -04002317 dprintf(CRITICAL, "decompressing dtb failed!!!\n");
Matthew Qin271927e2015-03-31 22:07:07 -04002318 ASSERT(0);
2319 }
2320
Matthew Qin0b15b322015-05-19 05:20:54 -04002321 dprintf(INFO, "decompressing dtb: done\n");
Matthew Qin271927e2015-03-31 22:07:07 -04002322 best_match_dt_addr = out_addr;
2323 } else {
2324 best_match_dt_addr = (unsigned char *)dt_table_offset + dt_entry.offset;
2325 dtb_size = dt_entry.size;
2326 }
2327
Matthew Qin49e51fa2015-02-09 10:40:45 +08002328 /* Validate and Read device device tree in the tags_addr */
Mayank Grovere559cec2017-10-17 15:12:03 +05302329 if (check_aboot_addr_range_overlap(hdr->tags_addr, dtb_size) ||
2330 check_ddr_addr_range_bound(hdr->tags_addr, dtb_size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002331 {
Mayank Grovere559cec2017-10-17 15:12:03 +05302332 dprintf(CRITICAL, "Device tree addresses are not valid\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002333 return -1;
2334 }
Shashank Mittal162244e2011-08-08 19:01:25 -07002335
Matthew Qin271927e2015-03-31 22:07:07 -04002336 memmove((void *)hdr->tags_addr, (char *)best_match_dt_addr, dtb_size);
Matthew Qin49e51fa2015-02-09 10:40:45 +08002337 } else {
2338 /* Validate the tags_addr */
Mayank Grovere559cec2017-10-17 15:12:03 +05302339 if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual) ||
2340 check_ddr_addr_range_bound(hdr->tags_addr, kernel_actual))
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07002341 {
Mayank Grovere559cec2017-10-17 15:12:03 +05302342 dprintf(CRITICAL, "Device tree addresses are not valid.\n");
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07002343 return -1;
2344 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08002345 /*
2346 * If appended dev tree is found, update the atags with
2347 * memory address to the DTB appended location on RAM.
2348 * Else update with the atags address in the kernel header
Mayank Grover1ed3e8b2018-09-19 15:16:50 +05302349 *
2350 * Make sure everything from scratch address is read before next step!
2351 * In case of dtbo, this API is going to read dtbo on scratch.
Matthew Qin49e51fa2015-02-09 10:40:45 +08002352 */
2353 void *dtb;
Parth Dixit1375d9e2019-07-08 20:56:13 +05302354 image_buf = (void*)(image_addr + page_size + patched_kernel_hdr_size);
2355
Nagireddy Annem0d60bb62019-08-16 19:16:00 +05302356#ifdef OSVERSION_IN_BOOTIMAGE
Parth Dixit1375d9e2019-07-08 20:56:13 +05302357 if ( hdr->header_version == BOOT_HEADER_VERSION_TWO) {
2358
2359 image_buf = (void*)(image_addr);
2360 dtb_offset = dtb_image_offset;
2361 dtb_image_size = imagesize_actual;
2362 }
Nagireddy Annem0d60bb62019-08-16 19:16:00 +05302363#endif
Parth Dixit1375d9e2019-07-08 20:56:13 +05302364
2365 dtb = dev_tree_appended(image_buf, dtb_image_size, dtb_offset,
Ameya Thakur10a33452016-06-13 14:24:26 -07002366 (void *)hdr->tags_addr);
Matthew Qin49e51fa2015-02-09 10:40:45 +08002367 if (!dtb) {
2368 dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07002369 return -1;
2370 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07002371 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08002372 #endif
Shashank Mittal23b8f422010-04-16 19:27:21 -07002373
Stanimir Varbanov69ec5462013-07-18 18:17:42 +03002374 if (boot_into_recovery && !device.is_unlocked && !device.is_tampered)
2375 target_load_ssd_keystore();
2376
Shashank Mittal23b8f422010-04-16 19:27:21 -07002377unified_boot:
Shashank Mittal23b8f422010-04-16 19:27:21 -07002378
Dima Zavin77e41f32013-03-06 16:10:43 -08002379 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07002380 (const char *)hdr->cmdline, board_machtype(),
Shashank Mittal23b8f422010-04-16 19:27:21 -07002381 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
2382
2383 return 0;
2384}
2385
Dima Zavin214cc642009-01-26 11:16:21 -08002386int boot_linux_from_flash(void)
2387{
Mayank Grovere1ab96c2018-09-04 20:31:31 +05302388 boot_img_hdr *hdr = (void*) buf;
Dima Zavin214cc642009-01-26 11:16:21 -08002389 struct ptentry *ptn;
2390 struct ptable *ptable;
2391 unsigned offset = 0;
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08002392
Shashank Mittalcd98d472011-08-02 14:29:24 -07002393 unsigned char *image_addr = 0;
2394 unsigned kernel_actual;
2395 unsigned ramdisk_actual;
2396 unsigned imagesize_actual;
vijay kumar8f53e362015-11-24 13:38:11 +05302397 unsigned second_actual = 0;
Shashank Mittalcd98d472011-08-02 14:29:24 -07002398
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07002399#if DEVICE_TREE
Monika Singh292b3e92018-03-17 22:40:23 +05302400 struct dt_table *table = NULL;
Joel Kingaa335dc2013-06-03 16:11:08 -07002401 struct dt_entry dt_entry;
vijay kumar8f53e362015-11-24 13:38:11 +05302402 unsigned dt_table_offset;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08002403 uint32_t dt_actual;
Monika Singh292b3e92018-03-17 22:40:23 +05302404 uint32_t dt_hdr_size = 0;
Vivek Kumar07bd0862018-04-10 14:19:23 +05302405 uint32_t dtb_offset = 0;
vijay kumar8f53e362015-11-24 13:38:11 +05302406 unsigned int dtb_size = 0;
2407 unsigned char *best_match_dt_addr = NULL;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07002408#endif
2409
David Ng183a7422009-12-07 14:55:21 -08002410 if (target_is_emmc_boot()) {
Mayank Grovere1ab96c2018-09-04 20:31:31 +05302411 hdr = (boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
David Ng183a7422009-12-07 14:55:21 -08002412 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
2413 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
2414 return -1;
2415 }
2416 goto continue_boot;
2417 }
2418
Dima Zavin214cc642009-01-26 11:16:21 -08002419 ptable = flash_get_ptable();
2420 if (ptable == NULL) {
2421 dprintf(CRITICAL, "ERROR: Partition table not found\n");
2422 return -1;
2423 }
2424
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08002425 if(!boot_into_recovery)
2426 {
2427 ptn = ptable_find(ptable, "boot");
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07002428
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08002429 if (ptn == NULL) {
2430 dprintf(CRITICAL, "ERROR: No boot partition found\n");
2431 return -1;
2432 }
2433 }
2434 else
2435 {
2436 ptn = ptable_find(ptable, "recovery");
2437 if (ptn == NULL) {
2438 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
2439 return -1;
2440 }
Dima Zavin214cc642009-01-26 11:16:21 -08002441 }
2442
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05302443 /* Read boot.img header from flash */
Shashank Mittaldcc2e352009-11-19 19:11:16 -08002444 if (flash_read(ptn, offset, buf, page_size)) {
Dima Zavin214cc642009-01-26 11:16:21 -08002445 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
2446 return -1;
2447 }
Dima Zavin214cc642009-01-26 11:16:21 -08002448
2449 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07002450 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Dima Zavin214cc642009-01-26 11:16:21 -08002451 return -1;
2452 }
2453
Shashank Mittaldcc2e352009-11-19 19:11:16 -08002454 if (hdr->page_size != page_size) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07002455 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 -08002456 return -1;
2457 }
2458
Kishor PK9e91b592017-05-24 12:14:55 +05302459 image_addr = (unsigned char *)target_get_scratch_address();
2460 memcpy(image_addr, (void *)buf, page_size);
vijay kumar287bb542015-09-29 13:01:52 +05302461
Channagoud Kadabia22144f2013-03-20 11:49:01 -07002462 /*
2463 * Update the kernel/ramdisk/tags address if the boot image header
2464 * has default values, these default values come from mkbootimg when
2465 * the boot image is flashed using fastboot flash:raw
2466 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08002467 update_ker_tags_rdisk_addr(hdr, false);
Channagoud Kadabia22144f2013-03-20 11:49:01 -07002468
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002469 /* Get virtual addresses since the hdr saves physical addresses. */
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002470 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
2471 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
2472 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
2473
2474 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
2475 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Mayank Grover032736f2017-07-14 20:34:51 +05302476 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002477
Kishor PK9e91b592017-05-24 12:14:55 +05302478 /* ensure commandline is terminated */
2479 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
2480
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002481 /* Check if the addresses in the header are valid. */
2482 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
Mayank Grovere559cec2017-10-17 15:12:03 +05302483 check_ddr_addr_range_bound(hdr->kernel_addr, kernel_actual) ||
2484 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual) ||
2485 check_ddr_addr_range_bound(hdr->ramdisk_addr, ramdisk_actual))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002486 {
Mayank Grovere559cec2017-10-17 15:12:03 +05302487 dprintf(CRITICAL, "kernel/ramdisk addresses are not valid.\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002488 return -1;
2489 }
2490
2491#ifndef DEVICE_TREE
Kishor PKd8ddcad2017-07-27 13:53:57 +05302492 if (UINT_MAX < ((uint64_t)kernel_actual + (uint64_t)ramdisk_actual+ (uint64_t)second_actual + page_size)) {
Mayank Grover032736f2017-07-14 20:34:51 +05302493 dprintf(CRITICAL, "Integer overflow detected in bootimage header fields\n");
2494 return -1;
2495 }
Kishor PKd8ddcad2017-07-27 13:53:57 +05302496 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + second_actual);
Mayank Grover032736f2017-07-14 20:34:51 +05302497
Mayank Grovere559cec2017-10-17 15:12:03 +05302498 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE) ||
2499 check_ddr_addr_range_bound(hdr->tags_addr, MAX_TAGS_SIZE))
Vijay Kumar Pendotid3ed20e2016-09-20 00:34:46 +05302500 {
Mayank Grovere559cec2017-10-17 15:12:03 +05302501 dprintf(CRITICAL, "Tags addresses are not valid.\n");
Vijay Kumar Pendotid3ed20e2016-09-20 00:34:46 +05302502 return -1;
2503 }
2504#else
2505
2506#ifndef OSVERSION_IN_BOOTIMAGE
2507 dt_size = hdr->dt_size;
2508#endif
Mayank Grover032736f2017-07-14 20:34:51 +05302509 dt_actual = ROUND_TO_PAGE(dt_size, page_mask);
Kishor PKd8ddcad2017-07-27 13:53:57 +05302510 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 +05302511 dprintf(CRITICAL, "Integer overflow detected in bootimage header fields\n");
2512 return -1;
2513 }
2514
Kishor PKd8ddcad2017-07-27 13:53:57 +05302515 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + second_actual + dt_actual);
Mayank Grover032736f2017-07-14 20:34:51 +05302516
Mayank Grovere559cec2017-10-17 15:12:03 +05302517 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_size) ||
2518 check_ddr_addr_range_bound(hdr->tags_addr, dt_size))
Mayank Grover032736f2017-07-14 20:34:51 +05302519 {
Mayank Grovere559cec2017-10-17 15:12:03 +05302520 dprintf(CRITICAL, "Device tree addresses are not valid.\n");
Mayank Grover032736f2017-07-14 20:34:51 +05302521 return -1;
2522 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002523#endif
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002524
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05302525 /* Read full boot.img from flash */
2526 dprintf(INFO, "Loading (%s) image (%d): start\n",
2527 (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual);
2528 bs_set_timestamp(BS_KERNEL_LOAD_START);
2529
2530 if (UINT_MAX - page_size < imagesize_actual)
2531 {
2532 dprintf(CRITICAL,"Integer overflow detected in bootimage header fields %u %s\n", __LINE__,__func__);
2533 return -1;
2534 }
2535
2536 /*Check the availability of RAM before reading boot image + max signature length from flash*/
2537 if (target_get_max_flash_size() < (imagesize_actual + page_size))
2538 {
2539 dprintf(CRITICAL, "bootimage size is greater than DDR can hold\n");
2540 return -1;
2541 }
2542
2543 offset = page_size;
2544 /* Read image without signature and header */
2545 if (flash_read(ptn, offset, (void *)(image_addr + offset), imagesize_actual - page_size))
2546 {
2547 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
2548 return -1;
2549 }
2550
2551 dprintf(INFO, "Loading (%s) image (%d): done\n",
2552 (!boot_into_recovery ? "boot" : "recovery"), imagesize_actual);
2553 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
2554
Shashank Mittalcd98d472011-08-02 14:29:24 -07002555 /* Authenticate Kernel */
Deepa Dinamani23b60d42013-06-24 18:10:52 -07002556 if(target_use_signed_kernel() && (!device.is_unlocked))
Shashank Mittalcd98d472011-08-02 14:29:24 -07002557 {
Shashank Mittalcd98d472011-08-02 14:29:24 -07002558 offset = imagesize_actual;
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05302559
Shashank Mittalcd98d472011-08-02 14:29:24 -07002560 /* Read signature */
2561 if (flash_read(ptn, offset, (void *)(image_addr + offset), page_size))
2562 {
2563 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002564 return -1;
Shashank Mittalcd98d472011-08-02 14:29:24 -07002565 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07002566
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05302567 verify_signed_bootimg((uint32_t)image_addr, imagesize_actual);
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05302568 }
2569 offset = page_size;
2570 if(hdr->second_size != 0) {
2571 if (UINT_MAX - offset < second_actual)
2572 {
2573 dprintf(CRITICAL, "ERROR: Integer overflow in boot image header %s\t%d\n",__func__,__LINE__);
2574 return -1;
vijay kumar8f53e362015-11-24 13:38:11 +05302575 }
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05302576 offset += second_actual;
2577 /* Second image loading not implemented. */
2578 ASSERT(0);
2579 }
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05302580 /* Move kernel and ramdisk to correct address */
2581 memmove((void*) hdr->kernel_addr, (char*) (image_addr + page_size), hdr->kernel_size);
2582 memmove((void*) hdr->ramdisk_addr, (char*) (image_addr + page_size + kernel_actual), hdr->ramdisk_size);
2583
2584#if DEVICE_TREE
2585 if(dt_size != 0) {
2586
2587 dt_table_offset = ((uint32_t)image_addr + page_size + kernel_actual + ramdisk_actual + second_actual);
2588
2589 table = (struct dt_table*) dt_table_offset;
2590
2591 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
2592 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
2593 return -1;
2594 }
2595
2596 /* Its Error if, dt_hdr_size (table->num_entries * dt_entry size + Dev_Tree Header)
2597 goes beyound hdr->dt_size*/
2598 if (dt_hdr_size > ROUND_TO_PAGE(dt_size,hdr->page_size)) {
2599 dprintf(CRITICAL, "ERROR: Invalid Device Tree size \n");
2600 return -1;
2601 }
2602
2603 /* Find index of device tree within device tree table */
2604 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
2605 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
2606 return -1;
2607 }
2608
2609 /* Validate and Read device device tree in the "tags_add */
2610 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size) ||
2611 check_ddr_addr_range_bound(hdr->tags_addr, dt_entry.size))
2612 {
2613 dprintf(CRITICAL, "Device tree addresses are not valid.\n");
2614 return -1;
2615 }
2616
2617 if(dt_entry.offset > (UINT_MAX - dt_entry.size)) {
2618 dprintf(CRITICAL, "ERROR: Device tree contents are Invalid\n");
2619 return -1;
2620 }
2621
2622 /* Ensure we are not overshooting dt_size with the dt_entry selected */
2623 if ((dt_entry.offset + dt_entry.size) > dt_size) {
2624 dprintf(CRITICAL, "ERROR: Device tree contents are Invalid\n");
2625 return -1;
2626 }
2627
2628 best_match_dt_addr = (unsigned char *)table + dt_entry.offset;
2629 dtb_size = dt_entry.size;
2630 memmove((void *)hdr->tags_addr, (char *)best_match_dt_addr, dtb_size);
Vivek Kumar07bd0862018-04-10 14:19:23 +05302631
2632 } else {
2633 /* Validate the tags_addr */
2634 if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual) ||
2635 check_ddr_addr_range_bound(hdr->tags_addr, kernel_actual))
2636 {
2637 dprintf(CRITICAL, "Device tree addresses are not valid.\n");
2638 return -1;
2639 }
2640 /*
2641 * If appended dev tree is found, update the atags with
2642 * memory address to the DTB appended location on RAM.
2643 * Else update with the atags address in the kernel header
Mayank Grover1ed3e8b2018-09-19 15:16:50 +05302644 *
2645 * Make sure everything from scratch address is read before next step!
2646 * In case of dtbo, this API is going to read dtbo on scratch.
Vivek Kumar07bd0862018-04-10 14:19:23 +05302647 */
2648 void *dtb = NULL;
2649 dtb = dev_tree_appended((void*)(image_addr + page_size ),hdr->kernel_size, dtb_offset, (void *)hdr->tags_addr);
2650 if (!dtb) {
2651 dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
2652 return -1;
2653 }
2654 }
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05302655#endif
2656 if(target_use_signed_kernel() && (!device.is_unlocked))
2657 {
Shashank Mittal162244e2011-08-08 19:01:25 -07002658 /* Make sure everything from scratch address is read before next step!*/
Shashank Mittala0032282011-08-26 14:50:11 -07002659 if(device.is_tampered)
Shashank Mittal162244e2011-08-08 19:01:25 -07002660 {
2661 write_device_info_flash(&device);
2662 }
Channagoud Kadabi5c86fe32012-02-16 10:58:48 +05302663#if USE_PCOM_SECBOOT
2664 set_tamper_flag(device.is_tampered);
2665#endif
Shashank Mittalcd98d472011-08-02 14:29:24 -07002666 }
David Ng183a7422009-12-07 14:55:21 -08002667continue_boot:
Dima Zavin214cc642009-01-26 11:16:21 -08002668
Dima Zavin214cc642009-01-26 11:16:21 -08002669 /* TODO: create/pass atags to kernel */
2670
Ajay Dudanie28a6072011-07-01 13:59:46 -07002671 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07002672 (const char *)hdr->cmdline, board_machtype(),
Dima Zavin214cc642009-01-26 11:16:21 -08002673 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
2674
2675 return 0;
2676}
Brian Swetland9c4c0752009-01-25 16:23:50 -08002677
Shashank Mittal162244e2011-08-08 19:01:25 -07002678void write_device_info_mmc(device_info *dev)
2679{
Shashank Mittal162244e2011-08-08 19:01:25 -07002680 unsigned long long ptn = 0;
2681 unsigned long long size;
2682 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002683 uint8_t lun = 0;
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002684 uint32_t ret = 0;
Mayank Groverddd348d2018-01-23 14:07:09 +05302685 uint32_t device_info_sz = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07002686
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002687 if (devinfo_present)
2688 index = partition_get_index("devinfo");
2689 else
2690 index = partition_get_index("aboot");
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07002691
Shashank Mittal162244e2011-08-08 19:01:25 -07002692 ptn = partition_get_offset(index);
2693 if(ptn == 0)
2694 {
2695 return;
2696 }
2697
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002698 lun = partition_get_lun(index);
2699 mmc_set_lun(lun);
2700
Shashank Mittal162244e2011-08-08 19:01:25 -07002701 size = partition_get_size(index);
2702
Mayank Groverddd348d2018-01-23 14:07:09 +05302703 device_info_sz = ROUND_TO_PAGE(sizeof(struct device_info),
2704 mmc_blocksize_mask);
2705 if (device_info_sz == UINT_MAX)
2706 {
2707 dprintf(CRITICAL, "ERROR: Incorrect blocksize of card\n");
2708 return;
2709 }
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07002710
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002711 if (devinfo_present)
Mayank Groverddd348d2018-01-23 14:07:09 +05302712 ret = mmc_write(ptn, device_info_sz, (void *)info_buf);
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002713 else
Mayank Groverddd348d2018-01-23 14:07:09 +05302714 ret = mmc_write((ptn + size - device_info_sz), device_info_sz, (void *)info_buf);
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002715 if (ret)
Shashank Mittal162244e2011-08-08 19:01:25 -07002716 {
2717 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
Sridhar Parasurame1a97922015-07-27 15:31:22 -07002718 ASSERT(0);
Shashank Mittal162244e2011-08-08 19:01:25 -07002719 }
2720}
2721
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002722void read_device_info_mmc(struct device_info *info)
Shashank Mittal162244e2011-08-08 19:01:25 -07002723{
Shashank Mittal162244e2011-08-08 19:01:25 -07002724 unsigned long long ptn = 0;
2725 unsigned long long size;
2726 int index = INVALID_PTN;
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002727 uint32_t ret = 0;
Mayank Groverddd348d2018-01-23 14:07:09 +05302728 uint32_t device_info_sz = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07002729
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002730 if ((index = partition_get_index("devinfo")) < 0)
2731 {
2732 devinfo_present = false;
2733 index = partition_get_index("aboot");
2734 }
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07002735
Shashank Mittal162244e2011-08-08 19:01:25 -07002736 ptn = partition_get_offset(index);
2737 if(ptn == 0)
2738 {
2739 return;
2740 }
2741
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07002742 mmc_set_lun(partition_get_lun(index));
2743
Shashank Mittal162244e2011-08-08 19:01:25 -07002744 size = partition_get_size(index);
2745
Mayank Groverddd348d2018-01-23 14:07:09 +05302746 device_info_sz = ROUND_TO_PAGE(sizeof(struct device_info),
2747 mmc_blocksize_mask);
2748 if (device_info_sz == UINT_MAX)
2749 {
2750 dprintf(CRITICAL, "ERROR: Incorrect blocksize of card\n");
2751 return;
2752 }
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07002753
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002754 if (devinfo_present)
Mayank Groverddd348d2018-01-23 14:07:09 +05302755 ret = mmc_read(ptn, (void *)info_buf, device_info_sz);
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002756 else
Mayank Groverddd348d2018-01-23 14:07:09 +05302757 ret = mmc_read((ptn + size - device_info_sz), (void *)info_buf, device_info_sz);
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002758 if (ret)
Shashank Mittal162244e2011-08-08 19:01:25 -07002759 {
2760 dprintf(CRITICAL, "ERROR: Cannot read device info\n");
Sridhar Parasurame1a97922015-07-27 15:31:22 -07002761 ASSERT(0);
Shashank Mittal162244e2011-08-08 19:01:25 -07002762 }
Shashank Mittal162244e2011-08-08 19:01:25 -07002763}
2764
tracychuid184b912020-06-05 17:31:38 +08002765/*[20200605][TracyChui] Implement get Serial Number start*/
2766#if defined(ENABLE_PRODINFO_ACCESS)
2767void write_prod_info_mmc(prod_info *dev)
2768{
2769 unsigned long long ptn = 0;
2770 unsigned long long size;
2771 int index = INVALID_PTN;
2772 uint32_t blocksize;
2773 uint8_t lun = 0;
2774 uint32_t ret = 0;
2775
2776 if (prodinfo_present)
2777 index = partition_get_index("prodinfo");
2778 else
2779 index = partition_get_index("aboot");
2780
2781 ptn = partition_get_offset(index);
2782 if(ptn == 0)
2783 {
2784 return;
2785 }
2786
2787 lun = partition_get_lun(index);
2788 mmc_set_lun(lun);
2789
2790 size = partition_get_size(index);
2791
2792 blocksize = mmc_get_device_blocksize();
2793
2794 if (prodinfo_present)
2795 ret = mmc_write(ptn, blocksize, (void *)prodinfo_buf);
2796 else
2797 ret = mmc_write((ptn + size - blocksize), blocksize, (void *)prodinfo_buf);
2798 if (ret)
2799 {
2800 dprintf(CRITICAL, "ERROR: Cannot write prod info\n");
2801 ASSERT(0);
2802 }
2803}
2804
2805void read_prod_info_mmc(struct prod_info *info)
2806{
2807 unsigned long long ptn = 0;
2808 unsigned long long size;
2809 int index = INVALID_PTN;
2810 uint32_t blocksize;
2811 uint32_t ret = 0;
2812
2813 if ((index = partition_get_index("prodinfo")) < 0)
2814 {
2815 prodinfo_present = false;
2816 index = partition_get_index("aboot");
2817 }
2818
2819 ptn = partition_get_offset(index);
2820 if(ptn == 0)
2821 {
2822 return;
2823 }
2824
2825 mmc_set_lun(partition_get_lun(index));
2826
2827 size = partition_get_size(index);
2828
2829 blocksize = mmc_get_device_blocksize();
2830
2831 if (prodinfo_present)
2832 ret = mmc_read(ptn, (void *)prodinfo_buf, blocksize);
2833 else
2834 ret = mmc_read((ptn + size - blocksize), (void *)prodinfo_buf, blocksize);
2835 if (ret)
2836 {
2837 dprintf(CRITICAL, "ERROR: Cannot read prod info\n");
2838 ASSERT(0);
2839 }
2840}
2841#endif
2842/*[20200605][TracyChui] Implement get Serial Number end*/
2843
Shashank Mittal162244e2011-08-08 19:01:25 -07002844void write_device_info_flash(device_info *dev)
2845{
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002846 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
Shashank Mittal162244e2011-08-08 19:01:25 -07002847 struct ptentry *ptn;
2848 struct ptable *ptable;
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002849 if(info == NULL)
2850 {
2851 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
2852 ASSERT(0);
2853 }
2854 info_buf = info;
Shashank Mittal162244e2011-08-08 19:01:25 -07002855 ptable = flash_get_ptable();
2856 if (ptable == NULL)
2857 {
2858 dprintf(CRITICAL, "ERROR: Partition table not found\n");
2859 return;
2860 }
2861
2862 ptn = ptable_find(ptable, "devinfo");
2863 if (ptn == NULL)
2864 {
Smita Ghosh670c8b82015-05-07 09:30:03 -07002865 dprintf(CRITICAL, "ERROR: No devinfo partition found\n");
Shashank Mittal162244e2011-08-08 19:01:25 -07002866 return;
2867 }
2868
Mayank Groverdedbc892017-10-24 13:41:34 +05302869 memset(info, 0, BOOT_IMG_MAX_PAGE_SIZE);
Shashank Mittal162244e2011-08-08 19:01:25 -07002870 memcpy(info, dev, sizeof(device_info));
2871
2872 if (flash_write(ptn, 0, (void *)info_buf, page_size))
2873 {
2874 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
2875 return;
2876 }
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002877 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07002878}
2879
vijay kumarc65876c2015-04-24 13:29:16 +05302880static int read_allow_oem_unlock(device_info *dev)
2881{
vijay kumarc65876c2015-04-24 13:29:16 +05302882 unsigned offset;
2883 int index;
2884 unsigned long long ptn;
2885 unsigned long long ptn_size;
2886 unsigned blocksize = mmc_get_device_blocksize();
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07002887 STACKBUF_DMA_ALIGN(buf, blocksize);
vijay kumarc65876c2015-04-24 13:29:16 +05302888
vijay kumarca2e6812015-07-08 20:28:25 +05302889 index = partition_get_index(frp_ptns[0]);
vijay kumarc65876c2015-04-24 13:29:16 +05302890 if (index == INVALID_PTN)
2891 {
vijay kumarca2e6812015-07-08 20:28:25 +05302892 index = partition_get_index(frp_ptns[1]);
2893 if (index == INVALID_PTN)
2894 {
2895 dprintf(CRITICAL, "Neither '%s' nor '%s' partition found\n", frp_ptns[0],frp_ptns[1]);
2896 return -1;
2897 }
vijay kumarc65876c2015-04-24 13:29:16 +05302898 }
2899
2900 ptn = partition_get_offset(index);
2901 ptn_size = partition_get_size(index);
2902 offset = ptn_size - blocksize;
2903
Mayank Grover48860402016-11-29 12:34:53 +05302904 /* Set Lun for partition */
2905 mmc_set_lun(partition_get_lun(index));
2906
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07002907 if (mmc_read(ptn + offset, (void *)buf, blocksize))
vijay kumarc65876c2015-04-24 13:29:16 +05302908 {
2909 dprintf(CRITICAL, "Reading MMC failed\n");
2910 return -1;
2911 }
2912
2913 /*is_allow_unlock is a bool value stored at the LSB of last byte*/
2914 is_allow_unlock = buf[blocksize-1] & 0x01;
2915 return 0;
2916}
2917
2918static int write_allow_oem_unlock(bool allow_unlock)
2919{
vijay kumarc65876c2015-04-24 13:29:16 +05302920 unsigned offset;
vijay kumarc65876c2015-04-24 13:29:16 +05302921 int index;
2922 unsigned long long ptn;
2923 unsigned long long ptn_size;
2924 unsigned blocksize = mmc_get_device_blocksize();
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07002925 STACKBUF_DMA_ALIGN(buf, blocksize);
vijay kumarc65876c2015-04-24 13:29:16 +05302926
vijay kumarca2e6812015-07-08 20:28:25 +05302927 index = partition_get_index(frp_ptns[0]);
vijay kumarc65876c2015-04-24 13:29:16 +05302928 if (index == INVALID_PTN)
2929 {
vijay kumarca2e6812015-07-08 20:28:25 +05302930 index = partition_get_index(frp_ptns[1]);
2931 if (index == INVALID_PTN)
2932 {
2933 dprintf(CRITICAL, "Neither '%s' nor '%s' partition found\n", frp_ptns[0],frp_ptns[1]);
2934 return -1;
2935 }
vijay kumarc65876c2015-04-24 13:29:16 +05302936 }
2937
2938 ptn = partition_get_offset(index);
2939 ptn_size = partition_get_size(index);
2940 offset = ptn_size - blocksize;
Mayank Grover48860402016-11-29 12:34:53 +05302941 mmc_set_lun(partition_get_lun(index));
vijay kumarc65876c2015-04-24 13:29:16 +05302942
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07002943 if (mmc_read(ptn + offset, (void *)buf, blocksize))
vijay kumarc65876c2015-04-24 13:29:16 +05302944 {
2945 dprintf(CRITICAL, "Reading MMC failed\n");
2946 return -1;
2947 }
2948
2949 /*is_allow_unlock is a bool value stored at the LSB of last byte*/
2950 buf[blocksize-1] = allow_unlock;
2951 if (mmc_write(ptn + offset, blocksize, buf))
2952 {
2953 dprintf(CRITICAL, "Writing MMC failed\n");
2954 return -1;
2955 }
2956
2957 return 0;
2958}
2959
Shashank Mittal162244e2011-08-08 19:01:25 -07002960void read_device_info_flash(device_info *dev)
2961{
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002962 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
Shashank Mittal162244e2011-08-08 19:01:25 -07002963 struct ptentry *ptn;
2964 struct ptable *ptable;
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002965 if(info == NULL)
2966 {
2967 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
2968 ASSERT(0);
2969 }
2970 info_buf = info;
Shashank Mittal162244e2011-08-08 19:01:25 -07002971 ptable = flash_get_ptable();
2972 if (ptable == NULL)
2973 {
2974 dprintf(CRITICAL, "ERROR: Partition table not found\n");
2975 return;
2976 }
2977
2978 ptn = ptable_find(ptable, "devinfo");
2979 if (ptn == NULL)
2980 {
Smita Ghosh670c8b82015-05-07 09:30:03 -07002981 dprintf(CRITICAL, "ERROR: No devinfo partition found\n");
Shashank Mittal162244e2011-08-08 19:01:25 -07002982 return;
2983 }
2984
2985 if (flash_read(ptn, 0, (void *)info_buf, page_size))
2986 {
2987 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
2988 return;
2989 }
2990
2991 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
2992 {
Shashank Mittal162244e2011-08-08 19:01:25 -07002993 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
2994 info->is_unlocked = 0;
Shashank Mittala0032282011-08-26 14:50:11 -07002995 info->is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07002996 write_device_info_flash(info);
2997 }
2998 memcpy(dev, info, sizeof(device_info));
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002999 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07003000}
3001
3002void write_device_info(device_info *dev)
3003{
3004 if(target_is_emmc_boot())
3005 {
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07003006 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
3007 if(info == NULL)
3008 {
3009 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
3010 ASSERT(0);
3011 }
3012 info_buf = info;
Channagoud Kadabi036c6052015-02-09 15:19:59 -08003013 memcpy(info, dev, sizeof(struct device_info));
3014
3015#if USE_RPMB_FOR_DEVINFO
Mayank Grover912eaa62017-10-26 12:08:53 +05303016 if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05303017 is_secure_boot_enable()) {
3018 if((write_device_info_rpmb((void*) info, PAGE_SIZE)) < 0)
3019 ASSERT(0);
Sridhar Parasurame1a97922015-07-27 15:31:22 -07003020 }
Channagoud Kadabic80cb492015-04-28 16:08:28 -07003021 else
3022 write_device_info_mmc(info);
Channagoud Kadabi036c6052015-02-09 15:19:59 -08003023#else
3024 write_device_info_mmc(info);
3025#endif
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07003026 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07003027 }
3028 else
3029 {
3030 write_device_info_flash(dev);
3031 }
3032}
3033
Monika Singh94316462018-03-15 18:39:01 +05303034int read_rollback_index(uint32_t loc, uint64_t *roll_back_index)
3035{
3036 if (!devinfo_present) {
3037 dprintf(CRITICAL, "DeviceInfo not initalized \n");
3038 return -EINVAL;
3039 }
3040 if (loc >= ARRAY_SIZE(device.rollback_index)) {
3041 dprintf(CRITICAL, "%s() Loc out of range index: %d, array len: %d\n",
3042 __func__, loc, ARRAY_SIZE(device.rollback_index));
3043 ASSERT(0);
3044 }
3045
3046 *roll_back_index = device.rollback_index[loc];
3047 return 0;
3048}
3049
3050int write_rollback_index(uint32_t loc, uint64_t roll_back_index)
3051{
3052 if (!devinfo_present) {
3053 dprintf(CRITICAL, "DeviceInfo not initalized \n");
3054 return -EINVAL;
3055 }
3056 if (loc >= ARRAY_SIZE(device.rollback_index)) {
3057 dprintf(CRITICAL, "%s() Loc out of range index: %d, array len: %d\n",
3058 __func__, loc, ARRAY_SIZE(device.rollback_index));
3059 ASSERT(0);
3060 }
3061
3062 device.rollback_index[loc] = roll_back_index;
3063 write_device_info(&device);
3064 return 0;
3065}
3066
Monika Singhb0fad822018-03-15 18:50:55 +05303067int store_userkey(uint8_t *user_key, uint32_t user_key_size)
3068{
3069 if (!devinfo_present) {
3070 dprintf(CRITICAL, "DeviceInfo not initalized \n");
3071 return -EINVAL;
3072 }
3073
3074 if (user_key_size > ARRAY_SIZE(device.user_public_key)) {
3075 dprintf(CRITICAL, "StoreUserKey, UserKeySize too large!\n");
3076 return -ENODEV;
3077 }
3078
3079 memcpy(device.user_public_key, user_key, user_key_size);
3080 device.user_public_key_length = user_key_size;
3081 write_device_info(&device);
3082 return 0;
3083}
3084
3085int erase_userkey()
3086{
3087 if (!devinfo_present) {
3088 dprintf(CRITICAL, "DeviceInfo not initalized \n");
3089 return -EINVAL;
3090 }
3091 memset(device.user_public_key, 0, ARRAY_SIZE(device.user_public_key));
3092 device.user_public_key_length = 0;
3093 write_device_info(&device);
3094 return 0;
3095}
3096
3097int get_userkey(uint8_t **user_key, uint32_t *user_key_size)
3098{
3099 if (!devinfo_present) {
3100 dprintf(CRITICAL, "DeviceInfo not initalized \n");
3101 return -EINVAL;
3102 }
3103 *user_key = device.user_public_key;
3104 *user_key_size = device.user_public_key_length;
3105 return 0;
3106}
3107
Shashank Mittal162244e2011-08-08 19:01:25 -07003108void read_device_info(device_info *dev)
3109{
3110 if(target_is_emmc_boot())
3111 {
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07003112 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
3113 if(info == NULL)
3114 {
3115 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
3116 ASSERT(0);
3117 }
3118 info_buf = info;
Channagoud Kadabi036c6052015-02-09 15:19:59 -08003119
3120#if USE_RPMB_FOR_DEVINFO
Mayank Grover912eaa62017-10-26 12:08:53 +05303121 if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05303122 is_secure_boot_enable()) {
3123 if((read_device_info_rpmb((void*) info, PAGE_SIZE)) < 0)
3124 ASSERT(0);
Sridhar Parasurame1a97922015-07-27 15:31:22 -07003125 }
Channagoud Kadabic80cb492015-04-28 16:08:28 -07003126 else
3127 read_device_info_mmc(info);
Channagoud Kadabi036c6052015-02-09 15:19:59 -08003128#else
3129 read_device_info_mmc(info);
3130#endif
3131
3132 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
3133 {
3134 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
lijuang511a2b52015-08-14 20:50:51 +08003135 if (is_secure_boot_enable()) {
Channagoud Kadabi05f78ba2015-07-06 11:58:14 -07003136 info->is_unlocked = 0;
Monika Singh292b3e92018-03-17 22:40:23 +05303137#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover912eaa62017-10-26 12:08:53 +05303138 if (VB_M <= target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +05303139 info->is_unlock_critical = 0;
Parth Dixitddbc7352015-10-18 03:13:31 +05303140#endif
lijuang511a2b52015-08-14 20:50:51 +08003141 } else {
Channagoud Kadabi2fda4092015-07-07 13:34:11 -07003142 info->is_unlocked = 1;
Monika Singh292b3e92018-03-17 22:40:23 +05303143#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover912eaa62017-10-26 12:08:53 +05303144 if (VB_M <= target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +05303145 info->is_unlock_critical = 1;
Parth Dixitddbc7352015-10-18 03:13:31 +05303146#endif
lijuang511a2b52015-08-14 20:50:51 +08003147 }
Channagoud Kadabi036c6052015-02-09 15:19:59 -08003148 info->is_tampered = 0;
3149 info->charger_screen_enabled = 0;
Monika Singh292b3e92018-03-17 22:40:23 +05303150#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover912eaa62017-10-26 12:08:53 +05303151 if (VB_M <= target_get_vb_version())
Monika Singh1c71a1c2019-12-03 12:12:48 +05303152 {
Mayank Grover889be1b2017-09-12 20:12:23 +05303153 info->verity_mode = 1; //enforcing by default
Monika Singh1c71a1c2019-12-03 12:12:48 +05303154 info->user_public_key_length = 0;
3155 memset(info->rollback_index, 0, sizeof(info->rollback_index));
3156 memset(info->user_public_key, 0, sizeof(info->user_public_key));
3157 }
Parth Dixitddbc7352015-10-18 03:13:31 +05303158#endif
Channagoud Kadabi036c6052015-02-09 15:19:59 -08003159 write_device_info(info);
3160 }
jessicatseng1b9da342020-04-22 11:13:20 +08003161
3162//<2020/04/22-JessicaTseng, Enable off-charging
3163 info->charger_screen_enabled = 1;
3164 write_device_info(info);
3165//>2020/04/22-JessicaTseng
3166
Channagoud Kadabi036c6052015-02-09 15:19:59 -08003167 memcpy(dev, info, sizeof(device_info));
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07003168 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07003169 }
3170 else
3171 {
3172 read_device_info_flash(dev);
3173 }
3174}
tracychuid184b912020-06-05 17:31:38 +08003175/*[20200605][TracyChui] Implement get Serial Number start*/
3176#if defined(ENABLE_PRODINFO_ACCESS)
3177void write_prod_info(prod_info *dev)
3178{
3179 if(target_is_emmc_boot())
3180 {
3181 struct prod_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
3182 if(info == NULL)
3183 {
3184 dprintf(CRITICAL, "Failed to allocate memory for prod info struct\n");
3185 ASSERT(0);
3186 }
3187 prodinfo_buf = info;
3188 memcpy(info, dev, sizeof(struct prod_info));
3189
3190 write_prod_info_mmc(info);
3191 free(info);
3192 }
3193}
3194
3195void read_prod_info(prod_info *dev)
3196{
3197 if(target_is_emmc_boot())
3198 {
3199 struct prod_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
3200 if(info == NULL)
3201 {
3202 dprintf(CRITICAL, "Failed to allocate memory for prod info struct\n");
3203 ASSERT(0);
3204 }
3205 prodinfo_buf = info;
3206
3207 read_prod_info_mmc(info);
3208
3209 if (memcmp(info->magic, PRODINFO_MAGIC, PRODINFO_MAGIC_SIZE))
3210 {
3211 memcpy(info->magic, PRODINFO_MAGIC, PRODINFO_MAGIC_SIZE);
3212 memcpy(info->isn, "No_Serial_Number", PRODINFO_MAX_ISN_LEN);
3213 memcpy(info->ssn, "No_Custer_Serial_Number", PRODINFO_MAX_SSN_LEN);
3214 info->is_adb_enabled = 0;
3215 write_prod_info(info);
3216 }
3217 memcpy(dev, info, sizeof(prod_info));
3218 free(info);
3219 }
3220}
3221#endif
3222/*[20200605][TracyChui] Implement get Serial Number end*/
Shashank Mittal162244e2011-08-08 19:01:25 -07003223
3224void reset_device_info()
3225{
3226 dprintf(ALWAYS, "reset_device_info called.");
Shashank Mittala0032282011-08-26 14:50:11 -07003227 device.is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07003228 write_device_info(&device);
3229}
3230
3231void set_device_root()
3232{
3233 dprintf(ALWAYS, "set_device_root called.");
Shashank Mittala0032282011-08-26 14:50:11 -07003234 device.is_tampered = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -07003235 write_device_info(&device);
3236}
3237
lijuang4ece1e72015-08-14 21:02:36 +08003238/* set device unlock value
3239 * Must check FRP before call this function
3240 * Need to wipe data when unlock status changed
3241 * type 0: oem unlock
3242 * type 1: unlock critical
3243 * status 0: unlock as false
3244 * status 1: lock as true
3245 */
3246void set_device_unlock_value(int type, bool status)
lijuang21f12f52015-08-22 16:22:19 +08003247{
lijuang4ece1e72015-08-14 21:02:36 +08003248 if (type == UNLOCK)
3249 device.is_unlocked = status;
Monika Singh292b3e92018-03-17 22:40:23 +05303250#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover912eaa62017-10-26 12:08:53 +05303251 else if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05303252 type == UNLOCK_CRITICAL)
3253 device.is_unlock_critical = status;
Parth Dixitddbc7352015-10-18 03:13:31 +05303254#endif
lijuang4ece1e72015-08-14 21:02:36 +08003255 write_device_info(&device);
3256}
3257
3258static void set_device_unlock(int type, bool status)
3259{
3260 int is_unlocked = -1;
3261 char response[MAX_RSP_SIZE];
3262
3263 /* check device unlock status if it is as expected */
3264 if (type == UNLOCK)
3265 is_unlocked = device.is_unlocked;
Monika Singh292b3e92018-03-17 22:40:23 +05303266#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover912eaa62017-10-26 12:08:53 +05303267 if(VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05303268 type == UNLOCK_CRITICAL)
3269 {
3270 is_unlocked = device.is_unlock_critical;
3271 }
Parth Dixitddbc7352015-10-18 03:13:31 +05303272#endif
lijuang4ece1e72015-08-14 21:02:36 +08003273 if (is_unlocked == status) {
3274 snprintf(response, sizeof(response), "\tDevice already : %s", (status ? "unlocked!" : "locked!"));
3275 fastboot_info(response);
3276 fastboot_okay("");
3277 return;
3278 }
3279
3280 /* status is true, it means to unlock device */
lijuang07c5d6e2018-04-23 18:32:13 +08003281 if (status && !is_allow_unlock) {
3282 fastboot_fail("oem unlock is not allowed");
3283 return;
3284 }
lijuang21f12f52015-08-22 16:22:19 +08003285
ericlin8ef2adf2020-04-29 14:14:24 +08003286 //<2018/01/09-EricLin, To remove display_unlock_menu for quiet mode.
3287 if(target_build_variant_user()){
lijuang4ece1e72015-08-14 21:02:36 +08003288#if FBCON_DISPLAY_MSG
lijuang07c5d6e2018-04-23 18:32:13 +08003289 display_unlock_menu(type, status);
3290 fastboot_okay("");
3291 return;
lijuang4ece1e72015-08-14 21:02:36 +08003292#else
lijuang07c5d6e2018-04-23 18:32:13 +08003293 if (status && type == UNLOCK) {
3294 fastboot_fail("Need wipe userdata. Do 'fastboot oem unlock-go'");
3295 return;
lijuang21f12f52015-08-22 16:22:19 +08003296 }
lijuang07c5d6e2018-04-23 18:32:13 +08003297#endif
ericlin8ef2adf2020-04-29 14:14:24 +08003298 }
3299 //<2018/01/09-EricLin
lijuang4ece1e72015-08-14 21:02:36 +08003300
3301 set_device_unlock_value(type, status);
3302
3303 /* wipe data */
3304 struct recovery_message msg;
Kishor PK60a68212017-05-08 16:55:57 +05303305 memset(&msg, 0, sizeof(msg));
lijuang4ece1e72015-08-14 21:02:36 +08003306 snprintf(msg.recovery, sizeof(msg.recovery), "recovery\n--wipe_data");
3307 write_misc(0, &msg, sizeof(msg));
3308
3309 fastboot_okay("");
ericlin8ef2adf2020-04-29 14:14:24 +08003310 //<2018/10/16-EricLin, To remove reset after oem lock/unlock.
3311 //reboot_device(RECOVERY_MODE);
3312 //<2018/10/16-EricLin
lijuang21f12f52015-08-22 16:22:19 +08003313}
3314
lijuang511a2b52015-08-14 20:50:51 +08003315static bool critical_flash_allowed(const char * entry)
3316{
3317 uint32_t i = 0;
3318 if (entry == NULL)
3319 return false;
3320
3321 for (i = 0; i < ARRAY_SIZE(critical_flash_allowed_ptn); i++) {
3322 if(!strcmp(entry, critical_flash_allowed_ptn[i]))
3323 return true;
3324 }
3325 return false;
Matthew Qin271927e2015-03-31 22:07:07 -04003326}
3327
3328#if DEVICE_TREE
Amol Jadicb524072012-08-09 16:40:18 -07003329int copy_dtb(uint8_t *boot_image_start, unsigned int scratch_offset)
3330{
3331 uint32 dt_image_offset = 0;
Amol Jadicb524072012-08-09 16:40:18 -07003332 uint32_t n;
Monika Singh292b3e92018-03-17 22:40:23 +05303333 struct dt_table *table = NULL;
Amol Jadicb524072012-08-09 16:40:18 -07003334 struct dt_entry dt_entry;
Monika Singh292b3e92018-03-17 22:40:23 +05303335 uint32_t dt_hdr_size = 0;
Amol Jadicb524072012-08-09 16:40:18 -07003336 unsigned int compressed_size = 0;
3337 unsigned int dtb_size = 0;
3338 unsigned int out_avai_len = 0;
3339 unsigned char *out_addr = NULL;
3340 unsigned char *best_match_dt_addr = NULL;
3341 int rc;
3342
Mayank Grovere1ab96c2018-09-04 20:31:31 +05303343 boot_img_hdr *hdr = (boot_img_hdr *) (boot_image_start);
Amol Jadicb524072012-08-09 16:40:18 -07003344
Parth Dixit4097b622016-03-15 14:42:27 +05303345#ifndef OSVERSION_IN_BOOTIMAGE
3346 dt_size = hdr->dt_size;
3347#endif
3348
3349 if(dt_size != 0) {
Amol Jadicb524072012-08-09 16:40:18 -07003350 /* add kernel offset */
3351 dt_image_offset += page_size;
3352 n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
3353 dt_image_offset += n;
3354
3355 /* add ramdisk offset */
3356 n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
3357 dt_image_offset += n;
3358
3359 /* add second offset */
3360 if(hdr->second_size != 0) {
3361 n = ROUND_TO_PAGE(hdr->second_size, page_mask);
3362 dt_image_offset += n;
3363 }
3364
3365 /* offset now point to start of dt.img */
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07003366 table = (struct dt_table*)(boot_image_start + dt_image_offset);
Amol Jadicb524072012-08-09 16:40:18 -07003367
Deepa Dinamani19648b42013-09-05 17:05:55 -07003368 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Amol Jadicb524072012-08-09 16:40:18 -07003369 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
3370 return -1;
3371 }
P.V. Phani Kumar82916762016-03-09 09:20:19 +05303372
3373 /* Its Error if, dt_hdr_size (table->num_entries * dt_entry size + Dev_Tree Header)
3374 goes beyound hdr->dt_size*/
Parth Dixit4097b622016-03-15 14:42:27 +05303375 if (dt_hdr_size > ROUND_TO_PAGE(dt_size,hdr->page_size)) {
P.V. Phani Kumar82916762016-03-09 09:20:19 +05303376 dprintf(CRITICAL, "ERROR: Invalid Device Tree size \n");
3377 return -1;
3378 }
3379
Joel Kingaa335dc2013-06-03 16:11:08 -07003380 /* Find index of device tree within device tree table */
3381 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Amol Jadicb524072012-08-09 16:40:18 -07003382 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
3383 return -1;
3384 }
3385
Matthew Qin271927e2015-03-31 22:07:07 -04003386 best_match_dt_addr = (unsigned char *)boot_image_start + dt_image_offset + dt_entry.offset;
3387 if (is_gzip_package(best_match_dt_addr, dt_entry.size))
3388 {
3389 out_addr = (unsigned char *)target_get_scratch_address() + scratch_offset;
3390 out_avai_len = target_get_max_flash_size() - scratch_offset;
Matthew Qin0b15b322015-05-19 05:20:54 -04003391 dprintf(INFO, "decompressing dtb: start\n");
Matthew Qin271927e2015-03-31 22:07:07 -04003392 rc = decompress(best_match_dt_addr,
3393 dt_entry.size, out_addr, out_avai_len,
3394 &compressed_size, &dtb_size);
3395 if (rc)
3396 {
Matthew Qin0b15b322015-05-19 05:20:54 -04003397 dprintf(CRITICAL, "decompressing dtb failed!!!\n");
Matthew Qin271927e2015-03-31 22:07:07 -04003398 ASSERT(0);
3399 }
3400
Matthew Qin0b15b322015-05-19 05:20:54 -04003401 dprintf(INFO, "decompressing dtb: done\n");
Matthew Qin271927e2015-03-31 22:07:07 -04003402 best_match_dt_addr = out_addr;
3403 } else {
3404 dtb_size = dt_entry.size;
3405 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07003406 /* Validate and Read device device tree in the "tags_add */
Mayank Grovere559cec2017-10-17 15:12:03 +05303407 if (check_aboot_addr_range_overlap(hdr->tags_addr, dtb_size) ||
3408 check_ddr_addr_range_bound(hdr->tags_addr, dtb_size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07003409 {
Mayank Grovere559cec2017-10-17 15:12:03 +05303410 dprintf(CRITICAL, "Device tree addresses are not valid.\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07003411 return -1;
3412 }
3413
Amol Jadicb524072012-08-09 16:40:18 -07003414 /* Read device device tree in the "tags_add */
Matthew Qin271927e2015-03-31 22:07:07 -04003415 memmove((void*) hdr->tags_addr, (void *)best_match_dt_addr, dtb_size);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07003416 } else
3417 return -1;
Amol Jadicb524072012-08-09 16:40:18 -07003418
3419 /* Everything looks fine. Return success. */
3420 return 0;
3421}
3422#endif
3423
Brian Swetland9c4c0752009-01-25 16:23:50 -08003424void cmd_boot(const char *arg, void *data, unsigned sz)
3425{
3426 unsigned kernel_actual;
3427 unsigned ramdisk_actual;
Kishor PKd8ddcad2017-07-27 13:53:57 +05303428 unsigned second_actual;
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07003429 uint32_t image_actual;
3430 uint32_t dt_actual = 0;
Mayank Grovere1ab96c2018-09-04 20:31:31 +05303431 boot_img_hdr *hdr = NULL;
Matthew Qinbb7923d2015-02-09 10:56:09 +08003432 struct kernel64_hdr *kptr = NULL;
Brian Swetland9c4c0752009-01-25 16:23:50 -08003433 char *ptr = ((char*) data);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07003434 int ret = 0;
3435 uint8_t dtb_copied = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08003436 unsigned int out_len = 0;
3437 unsigned int out_avai_len = 0;
3438 unsigned char *out_addr = NULL;
3439 uint32_t dtb_offset = 0;
3440 unsigned char *kernel_start_addr = NULL;
3441 unsigned int kernel_size = 0;
Matthew Qin271927e2015-03-31 22:07:07 -04003442 unsigned int scratch_offset = 0;
Saranya Chidurab4933332018-10-15 17:30:06 +05303443#if VERIFIED_BOOT_2
3444 void *dtbo_image_buf = NULL;
3445 uint32_t dtbo_image_sz = 0;
3446 void *vbmeta_image_buf = NULL;
3447 uint32_t vbmeta_image_sz = 0;
3448#endif
Monika Singh292b3e92018-03-17 22:40:23 +05303449#if !VERIFIED_BOOT_2
3450 uint32_t sig_actual = 0;
3451 uint32_t sig_size = 0;
3452#ifdef MDTP_SUPPORT
3453 static bool is_mdtp_activated = 0;
3454#endif /* MDTP_SUPPORT */
3455#endif
Matthew Qinbb7923d2015-02-09 10:56:09 +08003456
lijuang2008ff22016-03-07 17:56:27 +08003457#if FBCON_DISPLAY_MSG
3458 /* Exit keys' detection thread firstly */
3459 exit_menu_keys_detection();
3460#endif
3461
Monika Singh292b3e92018-03-17 22:40:23 +05303462#if VERIFIED_BOOT || VERIFIED_BOOT_2
Channagoud Kadabi6d5375e2015-06-23 17:15:42 -07003463 if(target_build_variant_user() && !device.is_unlocked)
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003464 {
3465 fastboot_fail("unlock device to use this command");
lijuang2008ff22016-03-07 17:56:27 +08003466 goto boot_failed;
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003467 }
3468#endif
3469
Brian Swetland9c4c0752009-01-25 16:23:50 -08003470 if (sz < sizeof(hdr)) {
3471 fastboot_fail("invalid bootimage header");
lijuang2008ff22016-03-07 17:56:27 +08003472 goto boot_failed;
Brian Swetland9c4c0752009-01-25 16:23:50 -08003473 }
3474
Mayank Grovere1ab96c2018-09-04 20:31:31 +05303475 hdr = (boot_img_hdr *)data;
Brian Swetland9c4c0752009-01-25 16:23:50 -08003476
3477 /* ensure commandline is terminated */
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07003478 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -08003479
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07003480 if(target_is_emmc_boot() && hdr->page_size) {
3481 page_size = hdr->page_size;
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07003482 page_mask = page_size - 1;
3483 }
3484
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07003485 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
3486 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Kishor PKd8ddcad2017-07-27 13:53:57 +05303487 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07003488#if DEVICE_TREE
Parth Dixit4097b622016-03-15 14:42:27 +05303489#ifndef OSVERSION_IN_BOOTIMAGE
3490 dt_size = hdr->dt_size;
3491#endif
3492 dt_actual = ROUND_TO_PAGE(dt_size, page_mask);
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07003493#endif
3494
3495 image_actual = ADD_OF(page_size, kernel_actual);
3496 image_actual = ADD_OF(image_actual, ramdisk_actual);
Kishor PKd8ddcad2017-07-27 13:53:57 +05303497 image_actual = ADD_OF(image_actual, second_actual);
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07003498 image_actual = ADD_OF(image_actual, dt_actual);
3499
Kishor PK5134b332017-05-09 17:50:08 +05303500 /* Checking to prevent oob access in read_der_message_length */
3501 if (image_actual > sz) {
3502 fastboot_fail("bootimage header fields are invalid");
3503 goto boot_failed;
3504 }
Saranya Chidurab4933332018-10-15 17:30:06 +05303505
Monika Singh292b3e92018-03-17 22:40:23 +05303506#if VERIFIED_BOOT_2
Mayank Grover1ceaee22019-04-01 17:54:32 +05303507 /* Pass size of boot partition, as imgsize, to avoid
3508 read fewer bytes error */
3509 image_actual = partition_get_size(partition_get_index("boot"));
Saranya Chidurab4933332018-10-15 17:30:06 +05303510
3511 /* load and validate dtbo partition */
3512 load_validate_dtbo_image(&dtbo_image_buf, &dtbo_image_sz);
3513
3514 /* load vbmeta partition */
3515 load_vbmeta_image(&vbmeta_image_buf, &vbmeta_image_sz);
3516
Monika Singh292b3e92018-03-17 22:40:23 +05303517 memset(&info, 0, sizeof(bootinfo));
Saranya Chidurab4933332018-10-15 17:30:06 +05303518
3519 info.images[IMG_BOOT].image_buffer = SUB_SALT_BUFF_OFFSET(data);
3520 info.images[IMG_BOOT].imgsize = image_actual;
3521 info.images[IMG_BOOT].name = "boot";
3522 ++info.num_loaded_images;
3523
3524 /* Pass loaded dtbo image */
3525 if (dtbo_image_buf != NULL) {
3526 info.images[IMG_DTBO].image_buffer = SUB_SALT_BUFF_OFFSET(dtbo_image_buf);
3527 info.images[IMG_DTBO].imgsize = dtbo_image_sz;
3528 info.images[IMG_DTBO].name = "dtbo";
3529 ++info.num_loaded_images;
3530 }
3531
3532 /* Pass loaded vbmeta image */
3533 if (vbmeta_image_buf != NULL) {
3534 info.images[IMG_VBMETA].image_buffer = vbmeta_image_buf;
3535 info.images[IMG_VBMETA].imgsize = vbmeta_image_sz;
3536 info.images[IMG_VBMETA].name = "vbmeta";
3537 ++info.num_loaded_images;
3538 }
3539
Monika Singh292b3e92018-03-17 22:40:23 +05303540 info.multi_slot_boot = partition_multislot_is_supported();
3541 if (load_image_and_auth(&info))
3542 goto boot_failed;
3543 vbcmdline = info.vbcmdline;
Saranya Chidurab4933332018-10-15 17:30:06 +05303544
3545 /* Free the buffer allocated to vbmeta post verification */
3546 if (vbmeta_image_buf != NULL) {
3547 free(vbmeta_image_buf);
3548 --info.num_loaded_images;
3549 }
Monika Singh292b3e92018-03-17 22:40:23 +05303550#else
Kishor PK5134b332017-05-09 17:50:08 +05303551 sig_size = sz - image_actual;
3552
P.V. Phani Kumareecfd822016-03-09 20:09:03 +05303553 if (target_use_signed_kernel() && (!device.is_unlocked)) {
Monika Singh292b3e92018-03-17 22:40:23 +05303554 unsigned chk;
P.V. Phani Kumareecfd822016-03-09 20:09:03 +05303555 /* Calculate the signature length from boot image */
3556 sig_actual = read_der_message_length(
Kishor PK5134b332017-05-09 17:50:08 +05303557 (unsigned char*)(data + image_actual), sig_size);
Monika Singh292b3e92018-03-17 22:40:23 +05303558 chk = ADD_OF(image_actual, sig_actual);
Channagoud Kadabi3d839012014-06-26 14:26:39 -07003559
Monika Singh292b3e92018-03-17 22:40:23 +05303560 if (chk > sz) {
Kishor PK5134b332017-05-09 17:50:08 +05303561 fastboot_fail("bootimage header fields are invalid");
3562 goto boot_failed;
3563 }
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07003564 }
3565
Sridhar Parasuram361e4cd2015-09-24 11:19:19 -07003566 // Initialize boot state before trying to verify boot.img
3567#if VERIFIED_BOOT
Channagoud Kadabi699466e2015-11-03 12:37:42 -08003568 boot_verifier_init();
Mayank Groverb337e932017-01-18 20:00:40 +05303569#endif
Channagoud Kadabibf6ce7d2015-09-17 13:25:35 -07003570 /* Handle overflow if the input image size is greater than
3571 * boot image buffer can hold
3572 */
Monika Singh292b3e92018-03-17 22:40:23 +05303573 if ((target_get_max_flash_size() - page_size) < image_actual)
Channagoud Kadabibf6ce7d2015-09-17 13:25:35 -07003574 {
3575 fastboot_fail("booimage: size is greater than boot image buffer can hold");
lijuang2008ff22016-03-07 17:56:27 +08003576 goto boot_failed;
Channagoud Kadabibf6ce7d2015-09-17 13:25:35 -07003577 }
Sridhar Parasuram361e4cd2015-09-24 11:19:19 -07003578
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07003579 /* Verify the boot image
3580 * device & page_size are initialized in aboot_init
3581 */
Sridhar Parasuram7ae9fbc2015-09-17 09:56:30 -07003582 if (target_use_signed_kernel() && (!device.is_unlocked)) {
Channagoud Kadabi3d839012014-06-26 14:26:39 -07003583 /* Pass size excluding signature size, otherwise we would try to
3584 * access signature beyond its length
3585 */
Monika Singh292b3e92018-03-17 22:40:23 +05303586 verify_signed_bootimg((uint32_t)data, image_actual);
Sridhar Parasuram7ae9fbc2015-09-17 09:56:30 -07003587 }
Amit Blay4aa292f2015-04-28 21:55:59 +03003588#ifdef MDTP_SUPPORT
3589 else
3590 {
Shay Nachmani062aa3f2015-05-17 17:28:44 +03003591 /* fastboot boot is not allowed when MDTP is activated */
Amit Blay4aa292f2015-04-28 21:55:59 +03003592 mdtp_ext_partition_verification_t ext_partition;
Amit Blay8a510302015-08-17 09:20:01 +03003593
3594 if (!is_mdtp_activated) {
3595 ext_partition.partition = MDTP_PARTITION_NONE;
3596 mdtp_fwlock_verify_lock(&ext_partition);
3597 }
Amit Blay4aa292f2015-04-28 21:55:59 +03003598 }
Shay Nachmani062aa3f2015-05-17 17:28:44 +03003599
Amir Kotzer7c768c02016-04-13 09:08:05 +03003600 /* If mdtp state cannot be validate, block fastboot boot*/
3601 if(mdtp_activated(&is_mdtp_activated)){
3602 dprintf(CRITICAL, "mdtp_activated cannot validate state.\n");
3603 dprintf(CRITICAL, "Can not proceed with fastboot boot command.\n");
3604 goto boot_failed;
3605 }
Shay Nachmani062aa3f2015-05-17 17:28:44 +03003606 if(is_mdtp_activated){
3607 dprintf(CRITICAL, "fastboot boot command is not available.\n");
lijuang2008ff22016-03-07 17:56:27 +08003608 goto boot_failed;
Shay Nachmani062aa3f2015-05-17 17:28:44 +03003609 }
Amit Blay4aa292f2015-04-28 21:55:59 +03003610#endif /* MDTP_SUPPORT */
Monika Singh292b3e92018-03-17 22:40:23 +05303611#endif /* VERIFIED_BOOT_2 else */
Amit Blay4aa292f2015-04-28 21:55:59 +03003612
Sridhar Parasuram361e4cd2015-09-24 11:19:19 -07003613#if VERIFIED_BOOT
Monika Singh292b3e92018-03-17 22:40:23 +05303614 if (VB_M == target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +05303615 {
3616 /* set boot and system versions. */
3617 set_os_version((unsigned char *)data);
3618 // send root of trust
3619 if(!send_rot_command((uint32_t)device.is_unlocked))
3620 ASSERT(0);
3621 }
Parth Dixitbc9b6492015-10-18 00:41:38 +05303622#endif
Channagoud Kadabia22144f2013-03-20 11:49:01 -07003623 /*
Matthew Qinbb7923d2015-02-09 10:56:09 +08003624 * Check if the kernel image is a gzip package. If yes, need to decompress it.
3625 * If not, continue booting.
3626 */
3627 if (is_gzip_package((unsigned char *)(data + page_size), hdr->kernel_size))
3628 {
3629 out_addr = (unsigned char *)target_get_scratch_address();
3630 out_addr = (unsigned char *)(out_addr + image_actual + page_size);
3631 out_avai_len = target_get_max_flash_size() - image_actual - page_size;
Saranya Chidurab4933332018-10-15 17:30:06 +05303632#if VERIFIED_BOOT_2
3633 if (dtbo_image_sz)
3634 out_avai_len -= DTBO_IMG_BUF;
3635#endif
Matthew Qin0b15b322015-05-19 05:20:54 -04003636 dprintf(INFO, "decompressing kernel image: start\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08003637 ret = decompress((unsigned char *)(ptr + page_size),
3638 hdr->kernel_size, out_addr, out_avai_len,
3639 &dtb_offset, &out_len);
3640 if (ret)
3641 {
Matthew Qin0b15b322015-05-19 05:20:54 -04003642 dprintf(CRITICAL, "decompressing image failed!!!\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08003643 ASSERT(0);
3644 }
3645
Matthew Qin0b15b322015-05-19 05:20:54 -04003646 dprintf(INFO, "decompressing kernel image: done\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08003647 kptr = (struct kernel64_hdr *)out_addr;
3648 kernel_start_addr = out_addr;
3649 kernel_size = out_len;
3650 } else {
3651 kptr = (struct kernel64_hdr*)((char *)data + page_size);
3652 kernel_start_addr = (unsigned char *)((char *)data + page_size);
3653 kernel_size = hdr->kernel_size;
3654 }
3655
3656 /*
Channagoud Kadabia22144f2013-03-20 11:49:01 -07003657 * Update the kernel/ramdisk/tags address if the boot image header
3658 * has default values, these default values come from mkbootimg when
3659 * the boot image is flashed using fastboot flash:raw
3660 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08003661 update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr));
Dima Zavin3cadfff2013-03-21 14:30:48 -07003662
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07003663 /* Get virtual addresses since the hdr saves physical addresses. */
3664 hdr->kernel_addr = VA(hdr->kernel_addr);
3665 hdr->ramdisk_addr = VA(hdr->ramdisk_addr);
3666 hdr->tags_addr = VA(hdr->tags_addr);
Brian Swetland9c4c0752009-01-25 16:23:50 -08003667
Matthew Qinbb7923d2015-02-09 10:56:09 +08003668 kernel_size = ROUND_TO_PAGE(kernel_size, page_mask);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07003669 /* Check if the addresses in the header are valid. */
Matthew Qinbb7923d2015-02-09 10:56:09 +08003670 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_size) ||
Mayank Grovere559cec2017-10-17 15:12:03 +05303671 check_ddr_addr_range_bound(hdr->kernel_addr, kernel_size) ||
3672 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual) ||
3673 check_ddr_addr_range_bound(hdr->ramdisk_addr, ramdisk_actual))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07003674 {
Mayank Grovere559cec2017-10-17 15:12:03 +05303675 dprintf(CRITICAL, "kernel/ramdisk addresses are not valid.\n");
lijuang2008ff22016-03-07 17:56:27 +08003676 goto boot_failed;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07003677 }
3678
Amol Jadicb524072012-08-09 16:40:18 -07003679#if DEVICE_TREE
Matthew Qin271927e2015-03-31 22:07:07 -04003680 scratch_offset = image_actual + page_size + out_len;
Amol Jadicb524072012-08-09 16:40:18 -07003681 /* find correct dtb and copy it to right location */
Matthew Qin271927e2015-03-31 22:07:07 -04003682 ret = copy_dtb(data, scratch_offset);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07003683
3684 dtb_copied = !ret ? 1 : 0;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07003685#else
Mayank Grovere559cec2017-10-17 15:12:03 +05303686 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE) ||
3687 check_ddr_addr_range_bound(hdr->tags_addr, MAX_TAGS_SIZE))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07003688 {
Mayank Grovere559cec2017-10-17 15:12:03 +05303689 dprintf(CRITICAL, "Tags addresses are not valid.\n");
lijuang2008ff22016-03-07 17:56:27 +08003690 goto boot_failed;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07003691 }
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07003692#endif
3693
3694 /* Load ramdisk & kernel */
3695 memmove((void*) hdr->ramdisk_addr, ptr + page_size + kernel_actual, hdr->ramdisk_size);
Matthew Qinbb7923d2015-02-09 10:56:09 +08003696 memmove((void*) hdr->kernel_addr, (char*) (kernel_start_addr), kernel_size);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07003697
3698#if DEVICE_TREE
Mayank Grovere559cec2017-10-17 15:12:03 +05303699 if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual) ||
3700 check_ddr_addr_range_bound(hdr->tags_addr, kernel_actual))
Matthew Qinbb7923d2015-02-09 10:56:09 +08003701 {
Mayank Grovere559cec2017-10-17 15:12:03 +05303702 dprintf(CRITICAL, "Tags addresses are not valid.\n");
lijuang2008ff22016-03-07 17:56:27 +08003703 goto boot_failed;
Matthew Qinbb7923d2015-02-09 10:56:09 +08003704 }
3705
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07003706 /*
3707 * If dtb is not found look for appended DTB in the kernel.
3708 * If appended dev tree is found, update the atags with
3709 * memory address to the DTB appended location on RAM.
3710 * Else update with the atags address in the kernel header
3711 */
3712 if (!dtb_copied) {
3713 void *dtb;
Matthew Qinbb7923d2015-02-09 10:56:09 +08003714 dtb = dev_tree_appended((void*)(ptr + page_size),
3715 hdr->kernel_size, dtb_offset,
Dima Zavine63e5572013-05-03 12:23:06 -07003716 (void *)hdr->tags_addr);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07003717 if (!dtb) {
3718 fastboot_fail("dtb not found");
lijuang2008ff22016-03-07 17:56:27 +08003719 goto boot_failed;
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07003720 }
Amol Jadicb524072012-08-09 16:40:18 -07003721 }
3722#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08003723
3724 fastboot_okay("");
Amol Jadi7c4316c2013-10-07 14:19:26 -07003725 fastboot_stop();
Brian Swetland9c4c0752009-01-25 16:23:50 -08003726
Dima Zavin77e41f32013-03-06 16:10:43 -08003727 boot_linux((void*) hdr->kernel_addr, (void*) hdr->tags_addr,
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07003728 (const char*) hdr->cmdline, board_machtype(),
3729 (void*) hdr->ramdisk_addr, hdr->ramdisk_size);
lijuang2008ff22016-03-07 17:56:27 +08003730
3731 /* fastboot already stop, it's no need to show fastboot menu */
3732 return;
3733boot_failed:
3734#if FBCON_DISPLAY_MSG
3735 /* revert to fastboot menu if boot failed */
3736 display_fastboot_menu();
3737#endif
3738 return;
Brian Swetland9c4c0752009-01-25 16:23:50 -08003739}
3740
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003741void cmd_erase_nand(const char *arg, void *data, unsigned sz)
Dima Zavin214cc642009-01-26 11:16:21 -08003742{
3743 struct ptentry *ptn;
3744 struct ptable *ptable;
3745
3746 ptable = flash_get_ptable();
3747 if (ptable == NULL) {
3748 fastboot_fail("partition table doesn't exist");
3749 return;
3750 }
3751
3752 ptn = ptable_find(ptable, arg);
3753 if (ptn == NULL) {
3754 fastboot_fail("unknown partition name");
3755 return;
3756 }
3757
Monika Singh292b3e92018-03-17 22:40:23 +05303758 if (!strncmp(arg, "avb_custom_key", strlen("avb_custom_key"))) {
3759 dprintf(INFO, "erasing avb_custom_key\n");
3760 if (erase_userkey()) {
3761 fastboot_fail("Erasing avb_custom_key failed");
3762 } else {
3763 fastboot_okay("");
3764 }
3765 return;
3766 }
3767
Dima Zavin214cc642009-01-26 11:16:21 -08003768 if (flash_erase(ptn)) {
3769 fastboot_fail("failed to erase partition");
3770 return;
3771 }
3772 fastboot_okay("");
3773}
3774
Bikas Gurungd48bd242010-09-04 19:54:32 -07003775
3776void cmd_erase_mmc(const char *arg, void *data, unsigned sz)
3777{
3778 unsigned long long ptn = 0;
Oliver Wangcee448d2013-10-22 18:40:13 +08003779 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07003780 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003781 uint8_t lun = 0;
Vijay Kumar Pendoti0b21f462016-05-02 17:09:18 +05303782 char *footer = NULL;
Bikas Gurungd48bd242010-09-04 19:54:32 -07003783
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003784#if VERIFIED_BOOT
3785 if(!strcmp(arg, KEYSTORE_PTN_NAME))
3786 {
3787 if(!device.is_unlocked)
3788 {
3789 fastboot_fail("unlock device to erase keystore");
3790 return;
3791 }
3792 }
3793#endif
3794
Kinson Chikf1a43512011-07-14 11:28:39 -07003795 index = partition_get_index(arg);
3796 ptn = partition_get_offset(index);
Oliver Wangcee448d2013-10-22 18:40:13 +08003797 size = partition_get_size(index);
Neeti Desaica8c9602011-10-06 11:40:00 -07003798
Monika Singhc4778b72018-05-16 11:16:42 +05303799 if (!strncmp(arg, "avb_custom_key", strlen("avb_custom_key"))) {
3800 dprintf(INFO, "erasing avb_custom_key\n");
3801 if (erase_userkey()) {
3802 fastboot_fail("Erasing avb_custom_key failed");
3803 } else {
3804 fastboot_okay("");
3805 }
3806 return;
3807 }
3808
Kinson Chikf1a43512011-07-14 11:28:39 -07003809 if(ptn == 0) {
Neeti Desaica8c9602011-10-06 11:40:00 -07003810 fastboot_fail("Partition table doesn't exist\n");
Bikas Gurungd48bd242010-09-04 19:54:32 -07003811 return;
3812 }
Kun Liang2f1601a2013-08-12 16:29:54 +08003813
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003814 lun = partition_get_lun(index);
3815 mmc_set_lun(lun);
3816
Sridhar Parasuramd7957122015-02-27 11:33:40 -08003817 if (platform_boot_dev_isemmc())
3818 {
3819 if (mmc_erase_card(ptn, size)) {
3820 fastboot_fail("failed to erase partition\n");
3821 return;
3822 }
3823 } else {
3824 BUF_DMA_ALIGN(out, DEFAULT_ERASE_SIZE);
3825 size = partition_get_size(index);
3826 if (size > DEFAULT_ERASE_SIZE)
3827 size = DEFAULT_ERASE_SIZE;
Kun Liang2f1601a2013-08-12 16:29:54 +08003828
Sridhar Parasuramd7957122015-02-27 11:33:40 -08003829 /* Simple inefficient version of erase. Just writing
3830 0 in first several blocks */
3831 if (mmc_write(ptn , size, (unsigned int *)out)) {
3832 fastboot_fail("failed to erase partition");
3833 return;
3834 }
Vijay Kumar Pendoti0b21f462016-05-02 17:09:18 +05303835 /*Erase FDE metadata at the userdata footer*/
3836 if(!(strncmp(arg, "userdata", 8)))
3837 {
3838 footer = memalign(CACHE_LINE, FOOTER_SIZE);
3839 memset((void *)footer, 0, FOOTER_SIZE);
3840
3841 size = partition_get_size(index);
3842
3843 if (mmc_write((ptn + size) - FOOTER_SIZE , FOOTER_SIZE, (unsigned int *)footer)) {
3844 fastboot_fail("failed to erase userdata footer");
3845 free(footer);
3846 return;
3847 }
3848 free(footer);
3849 }
Bikas Gurungd48bd242010-09-04 19:54:32 -07003850 }
Monika Singh292b3e92018-03-17 22:40:23 +05303851#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover912eaa62017-10-26 12:08:53 +05303852 if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05303853 !(strncmp(arg, "userdata", 8)) &&
3854 send_delete_keys_to_tz())
Sridhar Parasuram32b30662015-07-10 13:33:22 -07003855 ASSERT(0);
3856#endif
Bikas Gurungd48bd242010-09-04 19:54:32 -07003857 fastboot_okay("");
3858}
3859
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003860void cmd_erase(const char *arg, void *data, unsigned sz)
3861{
Monika Singh292b3e92018-03-17 22:40:23 +05303862#if VERIFIED_BOOT || VERIFIED_BOOT_2
Channagoud Kadabi35297672015-06-13 11:09:49 -07003863 if (target_build_variant_user())
Channagoud Kadabice3cf422015-04-17 16:02:30 -07003864 {
Sridhar Parasuramc32d07d2015-07-12 10:57:48 -07003865 if(!device.is_unlocked)
Channagoud Kadabice3cf422015-04-17 16:02:30 -07003866 {
Channagoud Kadabi35297672015-06-13 11:09:49 -07003867 fastboot_fail("device is locked. Cannot erase");
Channagoud Kadabice3cf422015-04-17 16:02:30 -07003868 return;
3869 }
3870 }
3871#endif
3872
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003873 if(target_is_emmc_boot())
3874 cmd_erase_mmc(arg, data, sz);
3875 else
3876 cmd_erase_nand(arg, data, sz);
3877}
Bikas Gurungd48bd242010-09-04 19:54:32 -07003878
Mayank Grover11ff9692018-01-11 11:54:49 +05303879/* Get the size from partiton name */
3880static void get_partition_size(const char *arg, char *response)
3881{
3882 uint64_t ptn = 0;
3883 uint64_t size;
3884 int index = INVALID_PTN;
3885
3886 index = partition_get_index(arg);
3887
3888 if (index == INVALID_PTN)
3889 {
3890 dprintf(CRITICAL, "Invalid partition index\n");
3891 return;
3892 }
3893
3894 ptn = partition_get_offset(index);
3895
3896 if(!ptn)
3897 {
3898 dprintf(CRITICAL, "Invalid partition name %s\n", arg);
3899 return;
3900 }
3901
3902 size = partition_get_size(index);
3903
3904 snprintf(response, MAX_RSP_SIZE, "\t 0x%llx", size);
3905 return;
3906}
3907
Mayank Grover52cd10a2018-03-15 12:57:54 +05303908/* Function to check partition type of a partition*/
3909static fs_signature_type
3910check_partition_fs_signature(const char *arg)
3911{
3912 fs_signature_type ret = NO_FS;
3913 int index;
3914 unsigned long long ptn;
Mayank Grover399826a2018-08-27 12:15:15 +05303915 char *buffer = memalign(CACHE_LINE, mmc_blocksize);
3916 uint32_t sb_blk_offset = 0;
3917 char *sb_buffer = buffer;
3918
Mayank Grover52cd10a2018-03-15 12:57:54 +05303919 if (!sb_buffer)
3920 {
3921 dprintf(CRITICAL, "ERROR: Failed to allocate buffer for superblock\n");
3922 goto out;
3923 }
3924
3925 /* Read super block */
3926 if ((index = partition_get_index(arg)) < 0)
3927 {
3928 dprintf(CRITICAL, "ERROR: %s() doesn't exsit\n", arg);
3929 goto out;
3930 }
3931 ptn = partition_get_offset(index);
3932 mmc_set_lun(partition_get_lun(index));
Mayank Grover399826a2018-08-27 12:15:15 +05303933 sb_blk_offset = (FS_SUPERBLOCK_OFFSET/mmc_blocksize);
3934
3935 if(mmc_read(ptn + (sb_blk_offset * mmc_blocksize),
Mayank Grover52cd10a2018-03-15 12:57:54 +05303936 (void *)sb_buffer, mmc_blocksize))
3937 {
3938 dprintf(CRITICAL, "ERROR: Failed to read Superblock\n");
3939 goto out;
3940 }
3941
Mayank Grover399826a2018-08-27 12:15:15 +05303942 if (sb_blk_offset == 0)
3943 sb_buffer += FS_SUPERBLOCK_OFFSET;
3944
3945 if (*((uint16 *)(&sb_buffer[EXT_MAGIC_OFFSET_SB])) == (uint16)EXT_MAGIC)
Mayank Grover52cd10a2018-03-15 12:57:54 +05303946 {
3947 dprintf(SPEW, "%s() Found EXT FS\n", arg);
3948 ret = EXT_FS_SIGNATURE;
3949 }
Mayank Grover399826a2018-08-27 12:15:15 +05303950 else if (*((uint32 *)(&sb_buffer[F2FS_MAGIC_OFFSET_SB])) == F2FS_MAGIC)
Mayank Grover52cd10a2018-03-15 12:57:54 +05303951 {
3952 dprintf(SPEW, "%s() Found F2FS FS\n", arg);
3953 ret = EXT_F2FS_SIGNATURE;
3954 }
3955 else
3956 {
3957 dprintf(SPEW, "%s() Reverting to default 0x%x\n",
3958 arg, *((uint16 *)(&sb_buffer[EXT_MAGIC_OFFSET_SB])));
3959 ret = NO_FS;
3960 }
3961
3962out:
Mayank Grover399826a2018-08-27 12:15:15 +05303963 if(buffer)
3964 free(buffer);
Mayank Grover52cd10a2018-03-15 12:57:54 +05303965 return ret;
3966}
3967
Mayank Groverd38fe012018-03-13 15:33:16 +05303968/* Function to get partition type */
3969static void get_partition_type(const char *arg, char *response)
3970{
3971 uint n = 0;
Mayank Grover52cd10a2018-03-15 12:57:54 +05303972 fs_signature_type fs_signature;
Mayank Groverd38fe012018-03-13 15:33:16 +05303973
3974 if (arg == NULL ||
3975 response == NULL)
3976 {
3977 dprintf(CRITICAL, "Invalid input parameter\n");
3978 return;
3979 }
3980
3981 /* By default copy raw to response */
Mayank Grover0e559042018-04-11 17:49:30 +05303982 strlcpy(response, RAW_STR, MAX_RSP_SIZE);
Mayank Groverd38fe012018-03-13 15:33:16 +05303983
3984 /* Mark partiton type for known paritions only */
3985 for (n=0; n < ARRAY_SIZE(part_type_known); n++)
3986 {
3987 if (!strncmp(part_type_known[n].part_name, arg, strlen(arg)))
3988 {
Mayank Grover52cd10a2018-03-15 12:57:54 +05303989 /* Check partition for FS signature */
3990 fs_signature = check_partition_fs_signature(arg);
3991 switch (fs_signature)
3992 {
3993 case EXT_FS_SIGNATURE:
Mayank Grover0e559042018-04-11 17:49:30 +05303994 strlcpy(response, EXT_STR, MAX_RSP_SIZE);
Mayank Grover52cd10a2018-03-15 12:57:54 +05303995 break;
3996 case EXT_F2FS_SIGNATURE:
Mayank Grover0e559042018-04-11 17:49:30 +05303997 strlcpy(response, F2FS_STR, MAX_RSP_SIZE);
Mayank Grover52cd10a2018-03-15 12:57:54 +05303998 break;
3999 case NO_FS:
Mayank Grover0e559042018-04-11 17:49:30 +05304000 strlcpy(response, part_type_known[n].type_response, MAX_RSP_SIZE);
Mayank Grover52cd10a2018-03-15 12:57:54 +05304001 }
Mayank Groverd38fe012018-03-13 15:33:16 +05304002 }
4003 }
4004 return;
4005}
4006
Mayank Grover11ff9692018-01-11 11:54:49 +05304007/*
4008 * Publish the partition type & size info
4009 * fastboot getvar will publish the required information.
4010 * fastboot getvar partition_size:<partition_name>: partition size in hex
4011 * fastboot getvar partition_type:<partition_name>: partition type (ext/fat)
4012 */
4013static void publish_getvar_partition_info(struct getvar_partition_info *info, uint8_t num_parts)
4014{
Mayank Groverd38fe012018-03-13 15:33:16 +05304015 uint8_t i;
Mayank Grover11ff9692018-01-11 11:54:49 +05304016 static bool published = false;
4017 struct partition_entry *ptn_entry =
4018 partition_get_partition_entries();
4019 memset(info, 0, sizeof(struct getvar_partition_info)* num_parts);
4020
4021 for (i = 0; i < num_parts; i++) {
4022 strlcat(info[i].part_name, (const char *)ptn_entry[i].name, MAX_RSP_SIZE);
4023 strlcat(info[i].getvar_size, "partition-size:", MAX_GET_VAR_NAME_SIZE);
4024 strlcat(info[i].getvar_type, "partition-type:", MAX_GET_VAR_NAME_SIZE);
4025
Mayank Groverd38fe012018-03-13 15:33:16 +05304026 get_partition_type(info[i].part_name, info[i].type_response);
Mayank Grover11ff9692018-01-11 11:54:49 +05304027 get_partition_size(info[i].part_name, info[i].size_response);
Mayank Grover11ff9692018-01-11 11:54:49 +05304028 if (strlcat(info[i].getvar_size, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
4029 {
4030 dprintf(CRITICAL, "partition size name truncated\n");
4031 return;
4032 }
4033 if (strlcat(info[i].getvar_type, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
4034 {
4035 dprintf(CRITICAL, "partition type name truncated\n");
4036 return;
4037 }
4038
4039 if (!published)
4040 {
4041 /* publish partition size & type info */
4042 fastboot_publish((const char *) info[i].getvar_size, (const char *) info[i].size_response);
4043 fastboot_publish((const char *) info[i].getvar_type, (const char *) info[i].type_response);
4044 }
4045 }
4046 if (!published)
4047 published = true;
4048}
4049
4050
Ajay Dudani5c761132011-04-07 20:19:04 -07004051void cmd_flash_mmc_img(const char *arg, void *data, unsigned sz)
Shashank Mittal23b8f422010-04-16 19:27:21 -07004052{
4053 unsigned long long ptn = 0;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07004054 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07004055 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08004056 char *token = NULL;
4057 char *pname = NULL;
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07004058 char *sp;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08004059 uint8_t lun = 0;
4060 bool lun_set = false;
Mayank Grover351a75e2017-05-30 20:06:08 +05304061 int current_active_slot = INVALID;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07004062
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07004063 token = strtok_r((char *)arg, ":", &sp);
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08004064 pname = token;
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07004065 token = strtok_r(NULL, ":", &sp);
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08004066 if(token)
4067 {
4068 lun = atoi(token);
4069 mmc_set_lun(lun);
4070 lun_set = true;
4071 }
4072
Mao Jinlong226f33a2014-07-04 17:24:10 +08004073 if (pname)
Greg Grisco6e754772011-06-23 12:19:39 -07004074 {
Channagoud Kadabiad259832015-05-29 11:14:17 -07004075 if (!strncmp(pname, "frp-unlock", strlen("frp-unlock")))
4076 {
4077 if (!aboot_frp_unlock(pname, data, sz))
4078 {
4079 fastboot_info("FRP unlock successful");
4080 fastboot_okay("");
4081 }
4082 else
4083 fastboot_fail("Secret key is invalid, please update the bootloader with secret key");
4084
4085 return;
4086 }
4087
Mao Jinlong226f33a2014-07-04 17:24:10 +08004088 if (!strcmp(pname, "partition"))
4089 {
4090 dprintf(INFO, "Attempt to write partition image.\n");
4091 if (write_partition(sz, (unsigned char *) data)) {
4092 fastboot_fail("failed to write partition");
Greg Grisco6e754772011-06-23 12:19:39 -07004093 return;
4094 }
Mayank Grover11ff9692018-01-11 11:54:49 +05304095 /* Re-publish partition table */
4096 publish_getvar_partition_info(part_info, partition_get_partition_count());
Mayank Grover351a75e2017-05-30 20:06:08 +05304097
4098 /* Rescan partition table to ensure we have multislot support*/
4099 if (partition_scan_for_multislot())
4100 {
4101 current_active_slot = partition_find_active_slot();
4102 dprintf(INFO, "Multislot supported: Slot %s active",
4103 (SUFFIX_SLOT(current_active_slot)));
4104 }
4105 partition_mark_active_slot(current_active_slot);
Greg Grisco6e754772011-06-23 12:19:39 -07004106 }
Mao Jinlong226f33a2014-07-04 17:24:10 +08004107 else
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08004108 {
Shashank Mittald3e54dd2014-08-28 15:24:02 -07004109#if VERIFIED_BOOT
4110 if(!strcmp(pname, KEYSTORE_PTN_NAME))
4111 {
4112 if(!device.is_unlocked)
4113 {
4114 fastboot_fail("unlock device to flash keystore");
4115 return;
4116 }
Gaurav Nebhwanic7313cc2015-12-15 22:25:04 +05304117 if(!boot_verify_validate_keystore((unsigned char *)data,sz))
Shashank Mittald3e54dd2014-08-28 15:24:02 -07004118 {
4119 fastboot_fail("image is not a keystore file");
4120 return;
4121 }
4122 }
4123#endif
Mao Jinlong226f33a2014-07-04 17:24:10 +08004124 index = partition_get_index(pname);
4125 ptn = partition_get_offset(index);
4126 if(ptn == 0) {
4127 fastboot_fail("partition table doesn't exist");
4128 return;
4129 }
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08004130
Mayank Grover351a75e2017-05-30 20:06:08 +05304131 if (!strncmp(pname, "boot", strlen("boot"))
4132 || !strcmp(pname, "recovery"))
4133 {
Mao Jinlong226f33a2014-07-04 17:24:10 +08004134 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
4135 fastboot_fail("image is not a boot image");
4136 return;
4137 }
Mayank Grover351a75e2017-05-30 20:06:08 +05304138
4139 /* Reset multislot_partition attributes in case of flashing boot */
4140 if (partition_multislot_is_supported())
4141 {
4142 partition_reset_attributes(index);
4143 }
Mao Jinlong226f33a2014-07-04 17:24:10 +08004144 }
4145
4146 if(!lun_set)
4147 {
4148 lun = partition_get_lun(index);
4149 mmc_set_lun(lun);
4150 }
4151
4152 size = partition_get_size(index);
Gaurav Nebhwani4d2389c2016-03-17 21:10:05 +05304153 if (ROUND_TO_PAGE(sz, mmc_blocksize_mask) > size) {
Mao Jinlong226f33a2014-07-04 17:24:10 +08004154 fastboot_fail("size too large");
4155 return;
4156 }
4157 else if (mmc_write(ptn , sz, (unsigned int *)data)) {
4158 fastboot_fail("flash write failure");
4159 return;
4160 }
Greg Grisco6e754772011-06-23 12:19:39 -07004161 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07004162 }
4163 fastboot_okay("");
4164 return;
4165}
4166
Ajay Dudanide984792015-03-02 09:57:41 -08004167void cmd_flash_meta_img(const char *arg, void *data, unsigned sz)
4168{
4169 int i, images;
4170 meta_header_t *meta_header;
4171 img_header_entry_t *img_header_entry;
Parth Dixit3e2d3032016-03-04 17:11:52 +05304172 /*End of the image address*/
4173 uintptr_t data_end;
4174
4175 if( (UINT_MAX - sz) > (uintptr_t)data )
4176 data_end = (uintptr_t)data + sz;
4177 else
4178 {
4179 fastboot_fail("Cannot flash: image header corrupt");
4180 return;
4181 }
4182
4183 if( data_end < ((uintptr_t)data + sizeof(meta_header_t)))
4184 {
4185 fastboot_fail("Cannot flash: image header corrupt");
4186 return;
4187 }
Ajay Dudanide984792015-03-02 09:57:41 -08004188
lijuang8ee21882016-04-19 16:57:11 +08004189 /* If device is locked:
4190 * Forbid to flash image to avoid the device to bypass the image
4191 * which with "any" name other than bootloader. Because it maybe
4192 * a meta package of all partitions.
4193 */
Monika Singh292b3e92018-03-17 22:40:23 +05304194#if VERIFIED_BOOT || VERIFIED_BOOT_2
lijuang8ee21882016-04-19 16:57:11 +08004195 if (target_build_variant_user()) {
4196 if (!device.is_unlocked) {
4197 fastboot_fail("Device is locked, meta image flashing is not allowed");
4198 return;
4199 }
Mayank Grover889be1b2017-09-12 20:12:23 +05304200
Mayank Grover912eaa62017-10-26 12:08:53 +05304201 if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05304202 !device.is_unlock_critical)
4203 {
lijuang8ee21882016-04-19 16:57:11 +08004204 fastboot_fail("Device is critical locked, Meta image flashing is not allowed");
4205 return;
4206 }
lijuang8ee21882016-04-19 16:57:11 +08004207 }
4208#endif
4209
Ajay Dudanide984792015-03-02 09:57:41 -08004210 meta_header = (meta_header_t*) data;
Parth Dixit3e2d3032016-03-04 17:11:52 +05304211 if( data_end < ((uintptr_t)data + meta_header->img_hdr_sz))
4212 {
4213 fastboot_fail("Cannot flash: image header corrupt");
4214 return;
4215 }
Ajay Dudanide984792015-03-02 09:57:41 -08004216 img_header_entry = (img_header_entry_t*) (data+sizeof(meta_header_t));
4217
4218 images = meta_header->img_hdr_sz / sizeof(img_header_entry_t);
4219
4220 for (i=0; i<images; i++) {
4221
4222 if((img_header_entry[i].ptn_name == NULL) ||
4223 (img_header_entry[i].start_offset == 0) ||
4224 (img_header_entry[i].size == 0))
4225 break;
Kishor PK49831502017-04-21 17:55:43 +05304226 if ((UINT_MAX - img_header_entry[i].start_offset) < (uintptr_t)data) {
4227 fastboot_fail("Integer overflow detected in start_offset of img");
4228 break;
4229 }
4230 else if ((UINT_MAX - (img_header_entry[i].start_offset + (uintptr_t)data)) < img_header_entry[i].size) {
4231 fastboot_fail("Integer overflow detected in size of img");
4232 break;
4233 }
Parth Dixit3e2d3032016-03-04 17:11:52 +05304234 if( data_end < ((uintptr_t)data + img_header_entry[i].start_offset
4235 + img_header_entry[i].size) )
4236 {
4237 fastboot_fail("Cannot flash: image size mismatch");
4238 break;
4239 }
4240
Ajay Dudanide984792015-03-02 09:57:41 -08004241 cmd_flash_mmc_img(img_header_entry[i].ptn_name,
4242 (void *) data + img_header_entry[i].start_offset,
4243 img_header_entry[i].size);
4244 }
4245
Channagoud Kadabiaafbbfc2015-03-04 17:52:56 -08004246 if (!strncmp(arg, "bootloader", strlen("bootloader")))
4247 {
4248 strlcpy(device.bootloader_version, TARGET(BOARD), MAX_VERSION_LEN);
4249 strlcat(device.bootloader_version, "-", MAX_VERSION_LEN);
4250 strlcat(device.bootloader_version, meta_header->img_version, MAX_VERSION_LEN);
4251 }
4252 else
4253 {
4254 strlcpy(device.radio_version, TARGET(BOARD), MAX_VERSION_LEN);
4255 strlcat(device.radio_version, "-", MAX_VERSION_LEN);
4256 strlcat(device.radio_version, meta_header->img_version, MAX_VERSION_LEN);
4257 }
4258
4259 write_device_info(&device);
Ajay Dudanide984792015-03-02 09:57:41 -08004260 fastboot_okay("");
4261 return;
4262}
4263
Ajay Dudani5c761132011-04-07 20:19:04 -07004264void cmd_flash_mmc_sparse_img(const char *arg, void *data, unsigned sz)
4265{
4266 unsigned int chunk;
wufeng.jiang813dc352015-06-02 23:02:46 -04004267 uint64_t chunk_data_sz;
Channagoud Kadabif627cf82013-09-09 14:08:20 -07004268 uint32_t *fill_buf = NULL;
4269 uint32_t fill_val;
Mayank Grover4f50bba2017-07-19 18:17:08 +05304270 uint32_t blk_sz_actual = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07004271 sparse_header_t *sparse_header;
4272 chunk_header_t *chunk_header;
Ajay Dudaniab18f022011-05-12 14:39:22 -07004273 uint32_t total_blocks = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07004274 unsigned long long ptn = 0;
Channagoud Kadabi65b91002011-10-11 17:34:33 +05304275 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07004276 int index = INVALID_PTN;
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05304277 uint32_t i;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08004278 uint8_t lun = 0;
vijay kumar800255e2015-04-24 20:26:19 +05304279 /*End of the sparse image address*/
Parth Dixit64b1a482016-03-07 16:31:26 +05304280 uintptr_t data_end = (uintptr_t)data + sz;
Ajay Dudani5c761132011-04-07 20:19:04 -07004281
Kinson Chikf1a43512011-07-14 11:28:39 -07004282 index = partition_get_index(arg);
4283 ptn = partition_get_offset(index);
4284 if(ptn == 0) {
Ajay Dudani5c761132011-04-07 20:19:04 -07004285 fastboot_fail("partition table doesn't exist");
4286 return;
4287 }
4288
Channagoud Kadabi65b91002011-10-11 17:34:33 +05304289 size = partition_get_size(index);
Channagoud Kadabi65b91002011-10-11 17:34:33 +05304290
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08004291 lun = partition_get_lun(index);
4292 mmc_set_lun(lun);
4293
vijay kumar800255e2015-04-24 20:26:19 +05304294 if (sz < sizeof(sparse_header_t)) {
4295 fastboot_fail("size too low");
4296 return;
4297 }
4298
Ajay Dudani5c761132011-04-07 20:19:04 -07004299 /* Read and skip over sparse image header */
4300 sparse_header = (sparse_header_t *) data;
vijay kumar800255e2015-04-24 20:26:19 +05304301
Mayank Grover48bd9bb2017-07-19 12:04:16 +05304302 if (!sparse_header->blk_sz || (sparse_header->blk_sz % 4)){
4303 fastboot_fail("Invalid block size\n");
4304 return;
4305 }
4306
vijay kumar1321f342015-03-27 12:13:42 +05304307 if (((uint64_t)sparse_header->total_blks * (uint64_t)sparse_header->blk_sz) > size) {
Ajay Dudani876b3282012-12-21 14:12:17 -08004308 fastboot_fail("size too large");
4309 return;
4310 }
4311
vijay kumarde4fcf62015-04-23 13:05:49 +05304312 data += sizeof(sparse_header_t);
vijay kumar800255e2015-04-24 20:26:19 +05304313
Parth Dixit64b1a482016-03-07 16:31:26 +05304314 if (data_end < (uintptr_t)data) {
vijay kumar800255e2015-04-24 20:26:19 +05304315 fastboot_fail("buffer overreads occured due to invalid sparse header");
4316 return;
4317 }
4318
vijay kumarde4fcf62015-04-23 13:05:49 +05304319 if(sparse_header->file_hdr_sz != sizeof(sparse_header_t))
Ajay Dudani5c761132011-04-07 20:19:04 -07004320 {
vijay kumarde4fcf62015-04-23 13:05:49 +05304321 fastboot_fail("sparse header size mismatch");
4322 return;
Ajay Dudani5c761132011-04-07 20:19:04 -07004323 }
4324
Ajay Dudanib06c05f2011-05-12 14:46:10 -07004325 dprintf (SPEW, "=== Sparse Image Header ===\n");
4326 dprintf (SPEW, "magic: 0x%x\n", sparse_header->magic);
4327 dprintf (SPEW, "major_version: 0x%x\n", sparse_header->major_version);
4328 dprintf (SPEW, "minor_version: 0x%x\n", sparse_header->minor_version);
4329 dprintf (SPEW, "file_hdr_sz: %d\n", sparse_header->file_hdr_sz);
4330 dprintf (SPEW, "chunk_hdr_sz: %d\n", sparse_header->chunk_hdr_sz);
4331 dprintf (SPEW, "blk_sz: %d\n", sparse_header->blk_sz);
4332 dprintf (SPEW, "total_blks: %d\n", sparse_header->total_blks);
4333 dprintf (SPEW, "total_chunks: %d\n", sparse_header->total_chunks);
Ajay Dudani5c761132011-04-07 20:19:04 -07004334
4335 /* Start processing chunks */
4336 for (chunk=0; chunk<sparse_header->total_chunks; chunk++)
4337 {
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05304338 /* Make sure the total image size does not exceed the partition size */
4339 if(((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz) >= size) {
4340 fastboot_fail("size too large");
4341 return;
4342 }
Ajay Dudani5c761132011-04-07 20:19:04 -07004343 /* Read and skip over chunk header */
4344 chunk_header = (chunk_header_t *) data;
4345 data += sizeof(chunk_header_t);
4346
Parth Dixit64b1a482016-03-07 16:31:26 +05304347 if (data_end < (uintptr_t)data) {
vijay kumar800255e2015-04-24 20:26:19 +05304348 fastboot_fail("buffer overreads occured due to invalid sparse header");
4349 return;
4350 }
4351
Ajay Dudani5c761132011-04-07 20:19:04 -07004352 dprintf (SPEW, "=== Chunk Header ===\n");
4353 dprintf (SPEW, "chunk_type: 0x%x\n", chunk_header->chunk_type);
4354 dprintf (SPEW, "chunk_data_sz: 0x%x\n", chunk_header->chunk_sz);
4355 dprintf (SPEW, "total_size: 0x%x\n", chunk_header->total_sz);
4356
vijay kumar800255e2015-04-24 20:26:19 +05304357 if(sparse_header->chunk_hdr_sz != sizeof(chunk_header_t))
Ajay Dudani5c761132011-04-07 20:19:04 -07004358 {
vijay kumar800255e2015-04-24 20:26:19 +05304359 fastboot_fail("chunk header size mismatch");
4360 return;
Ajay Dudani5c761132011-04-07 20:19:04 -07004361 }
4362
wufeng.jiang813dc352015-06-02 23:02:46 -04004363 chunk_data_sz = (uint64_t)sparse_header->blk_sz * chunk_header->chunk_sz;
4364
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05304365 /* Make sure that the chunk size calculated from sparse image does not
4366 * exceed partition size
4367 */
4368 if ((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz + chunk_data_sz > size)
4369 {
4370 fastboot_fail("Chunk data size exceeds partition size");
4371 return;
4372 }
4373
Ajay Dudani5c761132011-04-07 20:19:04 -07004374 switch (chunk_header->chunk_type)
4375 {
4376 case CHUNK_TYPE_RAW:
wufeng.jiang813dc352015-06-02 23:02:46 -04004377 if((uint64_t)chunk_header->total_sz != ((uint64_t)sparse_header->chunk_hdr_sz +
Ajay Dudani5c761132011-04-07 20:19:04 -07004378 chunk_data_sz))
4379 {
4380 fastboot_fail("Bogus chunk size for chunk type Raw");
4381 return;
4382 }
4383
Parth Dixit64b1a482016-03-07 16:31:26 +05304384 if (data_end < (uintptr_t)data + chunk_data_sz) {
vijay kumar800255e2015-04-24 20:26:19 +05304385 fastboot_fail("buffer overreads occured due to invalid sparse header");
4386 return;
4387 }
4388
wufeng.jiang813dc352015-06-02 23:02:46 -04004389 /* chunk_header->total_sz is uint32,So chunk_data_sz is now less than 2^32
4390 otherwise it will return in the line above
4391 */
Ajay Dudaniab18f022011-05-12 14:39:22 -07004392 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
wufeng.jiang813dc352015-06-02 23:02:46 -04004393 (uint32_t)chunk_data_sz,
Ajay Dudaniab18f022011-05-12 14:39:22 -07004394 (unsigned int*)data))
Ajay Dudani5c761132011-04-07 20:19:04 -07004395 {
4396 fastboot_fail("flash write failure");
4397 return;
4398 }
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05304399 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
4400 fastboot_fail("Bogus size for RAW chunk type");
4401 return;
4402 }
Ajay Dudani5c761132011-04-07 20:19:04 -07004403 total_blocks += chunk_header->chunk_sz;
wufeng.jiang813dc352015-06-02 23:02:46 -04004404 data += (uint32_t)chunk_data_sz;
Ajay Dudani5c761132011-04-07 20:19:04 -07004405 break;
4406
Channagoud Kadabif627cf82013-09-09 14:08:20 -07004407 case CHUNK_TYPE_FILL:
4408 if(chunk_header->total_sz != (sparse_header->chunk_hdr_sz +
4409 sizeof(uint32_t)))
4410 {
4411 fastboot_fail("Bogus chunk size for chunk type FILL");
4412 return;
4413 }
4414
Mayank Grover4f50bba2017-07-19 18:17:08 +05304415 blk_sz_actual = ROUNDUP(sparse_header->blk_sz, CACHE_LINE);
4416 /* Integer overflow detected */
4417 if (blk_sz_actual < sparse_header->blk_sz)
4418 {
4419 fastboot_fail("Invalid block size");
4420 return;
4421 }
4422
4423 fill_buf = (uint32_t *)memalign(CACHE_LINE, blk_sz_actual);
Channagoud Kadabif627cf82013-09-09 14:08:20 -07004424 if (!fill_buf)
4425 {
4426 fastboot_fail("Malloc failed for: CHUNK_TYPE_FILL");
4427 return;
4428 }
4429
Parth Dixit64b1a482016-03-07 16:31:26 +05304430 if (data_end < (uintptr_t)data + sizeof(uint32_t)) {
vijay kumar800255e2015-04-24 20:26:19 +05304431 fastboot_fail("buffer overreads occured due to invalid sparse header");
Gaurav Nebhwani163cbf82016-03-17 21:21:27 +05304432 free(fill_buf);
vijay kumar800255e2015-04-24 20:26:19 +05304433 return;
4434 }
Channagoud Kadabif627cf82013-09-09 14:08:20 -07004435 fill_val = *(uint32_t *)data;
4436 data = (char *) data + sizeof(uint32_t);
Channagoud Kadabif627cf82013-09-09 14:08:20 -07004437
4438 for (i = 0; i < (sparse_header->blk_sz / sizeof(fill_val)); i++)
4439 {
4440 fill_buf[i] = fill_val;
4441 }
4442
Gaurav Nebhwanie94cbe12016-03-17 21:16:34 +05304443 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz))
4444 {
4445 fastboot_fail("bogus size for chunk FILL type");
4446 free(fill_buf);
4447 return;
4448 }
4449
wufeng.jiang813dc352015-06-02 23:02:46 -04004450 for (i = 0; i < chunk_header->chunk_sz; i++)
Channagoud Kadabif627cf82013-09-09 14:08:20 -07004451 {
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05304452 /* Make sure that the data written to partition does not exceed partition size */
4453 if ((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz + sparse_header->blk_sz > size)
4454 {
4455 fastboot_fail("Chunk data size for fill type exceeds partition size");
Gaurav Nebhwani163cbf82016-03-17 21:21:27 +05304456 free(fill_buf);
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05304457 return;
4458 }
4459
Channagoud Kadabif627cf82013-09-09 14:08:20 -07004460 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
4461 sparse_header->blk_sz,
4462 fill_buf))
4463 {
4464 fastboot_fail("flash write failure");
4465 free(fill_buf);
4466 return;
4467 }
4468
4469 total_blocks++;
4470 }
4471
4472 free(fill_buf);
4473 break;
4474
Ajay Dudani5c761132011-04-07 20:19:04 -07004475 case CHUNK_TYPE_DONT_CARE:
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05304476 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
4477 fastboot_fail("bogus size for chunk DONT CARE type");
4478 return;
4479 }
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07004480 total_blocks += chunk_header->chunk_sz;
4481 break;
4482
Ajay Dudani5c761132011-04-07 20:19:04 -07004483 case CHUNK_TYPE_CRC:
4484 if(chunk_header->total_sz != sparse_header->chunk_hdr_sz)
4485 {
wufeng.jiang813dc352015-06-02 23:02:46 -04004486 fastboot_fail("Bogus chunk size for chunk type CRC");
Ajay Dudani5c761132011-04-07 20:19:04 -07004487 return;
4488 }
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05304489 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
4490 fastboot_fail("bogus size for chunk CRC type");
4491 return;
4492 }
Ajay Dudani5c761132011-04-07 20:19:04 -07004493 total_blocks += chunk_header->chunk_sz;
Parth Dixit64b1a482016-03-07 16:31:26 +05304494 if ((uintptr_t)data > UINT_MAX - chunk_data_sz) {
vijay kumar800255e2015-04-24 20:26:19 +05304495 fastboot_fail("integer overflow occured");
4496 return;
4497 }
wufeng.jiang813dc352015-06-02 23:02:46 -04004498 data += (uint32_t)chunk_data_sz;
Parth Dixit64b1a482016-03-07 16:31:26 +05304499 if (data_end < (uintptr_t)data) {
vijay kumar800255e2015-04-24 20:26:19 +05304500 fastboot_fail("buffer overreads occured due to invalid sparse header");
4501 return;
4502 }
Ajay Dudani5c761132011-04-07 20:19:04 -07004503 break;
4504
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07004505 default:
Channagoud Kadabif627cf82013-09-09 14:08:20 -07004506 dprintf(CRITICAL, "Unkown chunk type: %x\n",chunk_header->chunk_type);
Ajay Dudani5c761132011-04-07 20:19:04 -07004507 fastboot_fail("Unknown chunk type");
4508 return;
4509 }
4510 }
4511
Ajay Dudani0c6927b2011-05-18 11:12:16 -07004512 dprintf(INFO, "Wrote %d blocks, expected to write %d blocks\n",
4513 total_blocks, sparse_header->total_blks);
4514
4515 if(total_blocks != sparse_header->total_blks)
4516 {
4517 fastboot_fail("sparse image write failure");
4518 }
Ajay Dudani5c761132011-04-07 20:19:04 -07004519
4520 fastboot_okay("");
4521 return;
4522}
4523
4524void cmd_flash_mmc(const char *arg, void *data, unsigned sz)
4525{
4526 sparse_header_t *sparse_header;
Ajay Dudanide984792015-03-02 09:57:41 -08004527 meta_header_t *meta_header;
Ajay Dudani5c761132011-04-07 20:19:04 -07004528
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07004529#ifdef SSD_ENABLE
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -08004530 /* 8 Byte Magic + 2048 Byte xml + Encrypted Data */
4531 unsigned int *magic_number = (unsigned int *) data;
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08004532 int ret=0;
4533 uint32 major_version=0;
4534 uint32 minor_version=0;
4535
4536 ret = scm_svc_version(&major_version,&minor_version);
4537 if(!ret)
4538 {
4539 if(major_version >= 2)
4540 {
Amir Samuelovbb65ce02013-05-05 12:20:18 +03004541 if( !strcmp(arg, "ssd") || !strcmp(arg, "tqs") )
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08004542 {
4543 ret = encrypt_scm((uint32 **) &data, &sz);
4544 if (ret != 0) {
4545 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
4546 return;
4547 }
4548
Amir Samuelovbb65ce02013-05-05 12:20:18 +03004549 /* Protect only for SSD */
4550 if (!strcmp(arg, "ssd")) {
4551 ret = scm_protect_keystore((uint32 *) data, sz);
4552 if (ret != 0) {
4553 dprintf(CRITICAL, "ERROR: scm_protect_keystore Failed\n");
4554 return;
4555 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08004556 }
4557 }
4558 else
4559 {
4560 ret = decrypt_scm_v2((uint32 **) &data, &sz);
4561 if(ret != 0)
4562 {
4563 dprintf(CRITICAL,"ERROR: Decryption Failure\n");
4564 return;
4565 }
4566 }
4567 }
4568 else
4569 {
4570 if (magic_number[0] == DECRYPT_MAGIC_0 &&
4571 magic_number[1] == DECRYPT_MAGIC_1)
4572 {
4573 ret = decrypt_scm((uint32 **) &data, &sz);
4574 if (ret != 0) {
4575 dprintf(CRITICAL, "ERROR: Invalid secure image\n");
4576 return;
4577 }
4578 }
4579 else if (magic_number[0] == ENCRYPT_MAGIC_0 &&
4580 magic_number[1] == ENCRYPT_MAGIC_1)
4581 {
4582 ret = encrypt_scm((uint32 **) &data, &sz);
4583 if (ret != 0) {
4584 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
4585 return;
4586 }
4587 }
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07004588 }
4589 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08004590 else
Neeti Desai127b9e02012-03-20 16:11:23 -07004591 {
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08004592 dprintf(CRITICAL,"INVALID SVC Version\n");
4593 return;
Neeti Desai127b9e02012-03-20 16:11:23 -07004594 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08004595#endif /* SSD_ENABLE */
Neeti Desai127b9e02012-03-20 16:11:23 -07004596
Monika Singh292b3e92018-03-17 22:40:23 +05304597#if VERIFIED_BOOT || VERIFIED_BOOT_2
Channagoud Kadabi35297672015-06-13 11:09:49 -07004598 if (target_build_variant_user())
Shashank Mittald3e54dd2014-08-28 15:24:02 -07004599 {
lijuang511a2b52015-08-14 20:50:51 +08004600 /* if device is locked:
4601 * common partition will not allow to be flashed
4602 * critical partition will allow to flash image.
4603 */
4604 if(!device.is_unlocked && !critical_flash_allowed(arg)) {
4605 fastboot_fail("Partition flashing is not allowed");
4606 return;
4607 }
Mayank Grover889be1b2017-09-12 20:12:23 +05304608
lijuang511a2b52015-08-14 20:50:51 +08004609 /* if device critical is locked:
4610 * common partition will allow to be flashed
4611 * critical partition will not allow to flash image.
4612 */
Mayank Grover912eaa62017-10-26 12:08:53 +05304613 if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05304614 !device.is_unlock_critical &&
4615 critical_flash_allowed(arg)) {
4616 fastboot_fail("Critical partition flashing is not allowed");
4617 return;
Shashank Mittald3e54dd2014-08-28 15:24:02 -07004618 }
4619 }
4620#endif
Monika Singh292b3e92018-03-17 22:40:23 +05304621 if (!strncmp(arg, "avb_custom_key", strlen("avb_custom_key"))) {
4622 dprintf(INFO, "flashing avb_custom_key\n");
4623 if (store_userkey(data, sz)) {
4624 fastboot_fail("Flashing avb_custom_key failed");
4625 } else {
4626 fastboot_okay("");
4627 }
4628 return;
4629 }
Shashank Mittald3e54dd2014-08-28 15:24:02 -07004630
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07004631 sparse_header = (sparse_header_t *) data;
Ajay Dudanide984792015-03-02 09:57:41 -08004632 meta_header = (meta_header_t *) data;
4633 if (sparse_header->magic == SPARSE_HEADER_MAGIC)
Ajay Dudani5c761132011-04-07 20:19:04 -07004634 cmd_flash_mmc_sparse_img(arg, data, sz);
Ajay Dudanide984792015-03-02 09:57:41 -08004635 else if (meta_header->magic == META_HEADER_MAGIC)
4636 cmd_flash_meta_img(arg, data, sz);
4637 else
4638 cmd_flash_mmc_img(arg, data, sz);
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07004639
4640#if VERIFIED_BOOT
Mayank Grover912eaa62017-10-26 12:08:53 +05304641 if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05304642 (!strncmp(arg, "system", 6)) &&
4643 !device.verity_mode)
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07004644 // reset dm_verity mode to enforcing
4645 device.verity_mode = 1;
4646 write_device_info(&device);
Parth Dixitddbc7352015-10-18 03:13:31 +05304647#endif
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07004648
Ajay Dudani5c761132011-04-07 20:19:04 -07004649 return;
4650}
4651
Tanya Brokhman84eaaf62015-02-24 21:57:06 +02004652void cmd_updatevol(const char *vol_name, void *data, unsigned sz)
4653{
4654 struct ptentry *sys_ptn;
4655 struct ptable *ptable;
4656
4657 ptable = flash_get_ptable();
4658 if (ptable == NULL) {
4659 fastboot_fail("partition table doesn't exist");
4660 return;
4661 }
4662
4663 sys_ptn = ptable_find(ptable, "system");
4664 if (sys_ptn == NULL) {
4665 fastboot_fail("system partition not found");
4666 return;
4667 }
4668
4669 sz = ROUND_TO_PAGE(sz, page_mask);
4670 if (update_ubi_vol(sys_ptn, vol_name, data, sz))
4671 fastboot_fail("update_ubi_vol failed");
4672 else
4673 fastboot_okay("");
4674}
4675
Sridhar Parasurame94e8152014-10-24 14:06:03 -07004676void cmd_flash_nand(const char *arg, void *data, unsigned sz)
Dima Zavin214cc642009-01-26 11:16:21 -08004677{
4678 struct ptentry *ptn;
4679 struct ptable *ptable;
4680 unsigned extra = 0;
Gaurav Nebhwanid9730712016-05-06 14:28:29 +05304681 uint64_t partition_size = 0;
Vijay Kumar Pendotic6f22a72017-05-19 22:44:34 +05304682 unsigned bytes_to_round_page = 0;
4683 unsigned rounded_size = 0;
Dima Zavin214cc642009-01-26 11:16:21 -08004684
Kishor PK38ed93d2017-04-25 14:19:26 +05304685 if((uintptr_t)data > (UINT_MAX - sz)) {
4686 fastboot_fail("Cannot flash: image header corrupt");
4687 return;
4688 }
4689
Dima Zavin214cc642009-01-26 11:16:21 -08004690 ptable = flash_get_ptable();
4691 if (ptable == NULL) {
4692 fastboot_fail("partition table doesn't exist");
4693 return;
4694 }
4695
4696 ptn = ptable_find(ptable, arg);
4697 if (ptn == NULL) {
Tanya Brokhman84eaaf62015-02-24 21:57:06 +02004698 dprintf(INFO, "unknown partition name (%s). Trying updatevol\n",
4699 arg);
4700 cmd_updatevol(arg, data, sz);
Dima Zavin214cc642009-01-26 11:16:21 -08004701 return;
4702 }
4703
Monika Singh292b3e92018-03-17 22:40:23 +05304704 if (!strncmp(arg, "avb_custom_key", strlen("avb_custom_key"))) {
4705 dprintf(INFO, "flashing avb_custom_key\n");
4706 if (store_userkey(data, sz)) {
4707 fastboot_fail("Flashing avb_custom_key failed");
4708 } else {
4709 fastboot_okay("");
4710 }
4711 return;
4712 }
4713
Dima Zavin214cc642009-01-26 11:16:21 -08004714 if (!strcmp(ptn->name, "boot") || !strcmp(ptn->name, "recovery")) {
Kishor PK38ed93d2017-04-25 14:19:26 +05304715 if((sz > BOOT_MAGIC_SIZE) && (!memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE))) {
4716 dprintf(INFO, "Verified the BOOT_MAGIC in image header \n");
4717 } else {
4718 fastboot_fail("Image is not a boot image");
Dima Zavin214cc642009-01-26 11:16:21 -08004719 return;
4720 }
4721 }
4722
Amol Jadi5c61a952012-05-04 17:05:35 -07004723 if (!strcmp(ptn->name, "system")
Deepa Dinamani13e32c42012-03-12 14:34:17 -07004724 || !strcmp(ptn->name, "userdata")
4725 || !strcmp(ptn->name, "persist")
Sundarajan Srinivasanb063a852013-11-19 14:02:27 -08004726 || !strcmp(ptn->name, "recoveryfs")
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -08004727 || !strcmp(ptn->name, "modem"))
Tanya Brokhman1c94f1a2015-02-15 09:05:03 +02004728 extra = 1;
Kishor PK38ed93d2017-04-25 14:19:26 +05304729 else {
Vijay Kumar Pendotic6f22a72017-05-19 22:44:34 +05304730 rounded_size = ROUNDUP(sz, page_size);
4731 bytes_to_round_page = rounded_size - sz;
4732 if (bytes_to_round_page) {
4733 if (((uintptr_t)data + sz ) > (UINT_MAX - bytes_to_round_page)) {
4734 fastboot_fail("Integer overflow detected");
4735 return;
4736 }
4737 if (((uintptr_t)data + sz + bytes_to_round_page) >
4738 ((uintptr_t)target_get_scratch_address() + target_get_max_flash_size())) {
4739 fastboot_fail("Buffer size is not aligned to page_size");
4740 return;
4741 }
4742 else {
4743 memset(data + sz, 0, bytes_to_round_page);
4744 sz = rounded_size;
4745 }
Kishor PK38ed93d2017-04-25 14:19:26 +05304746 }
Gaurav Nebhwanid9730712016-05-06 14:28:29 +05304747 }
4748
Kishor PK38ed93d2017-04-25 14:19:26 +05304749 /*Checking partition_size for the possible integer overflow */
4750 partition_size = validate_partition_size(ptn);
4751
Gaurav Nebhwanid9730712016-05-06 14:28:29 +05304752 if (sz > partition_size) {
4753 fastboot_fail("Image size too large");
4754 return;
4755 }
4756
Dima Zavin214cc642009-01-26 11:16:21 -08004757 dprintf(INFO, "writing %d bytes to '%s'\n", sz, ptn->name);
Mayank Grover6cde9352017-06-06 18:45:23 +05304758 if ((sz > UBI_EC_HDR_SIZE) &&
4759 (!memcmp((void *)data, UBI_MAGIC, UBI_MAGIC_SIZE))) {
Tanya Brokhman1c94f1a2015-02-15 09:05:03 +02004760 if (flash_ubi_img(ptn, data, sz)) {
4761 fastboot_fail("flash write failure");
4762 return;
4763 }
4764 } else {
4765 if (flash_write(ptn, extra, data, sz)) {
4766 fastboot_fail("flash write failure");
4767 return;
4768 }
Dima Zavin214cc642009-01-26 11:16:21 -08004769 }
4770 dprintf(INFO, "partition '%s' updated\n", ptn->name);
4771 fastboot_okay("");
4772}
4773
Kishor PK38ed93d2017-04-25 14:19:26 +05304774
4775static inline uint64_t validate_partition_size(struct ptentry *ptn)
4776{
4777 if (ptn->length && flash_num_pages_per_blk() && page_size) {
4778 if ((ptn->length < ( UINT_MAX / flash_num_pages_per_blk())) && ((ptn->length * flash_num_pages_per_blk()) < ( UINT_MAX / page_size))) {
4779 return ptn->length * flash_num_pages_per_blk() * page_size;
4780 }
4781 }
4782 return 0;
4783}
4784
4785
Sridhar Parasurame94e8152014-10-24 14:06:03 -07004786void cmd_flash(const char *arg, void *data, unsigned sz)
4787{
4788 if(target_is_emmc_boot())
4789 cmd_flash_mmc(arg, data, sz);
4790 else
4791 cmd_flash_nand(arg, data, sz);
4792}
4793
Dima Zavin214cc642009-01-26 11:16:21 -08004794void cmd_continue(const char *arg, void *data, unsigned sz)
4795{
4796 fastboot_okay("");
Amol Jadi7c4316c2013-10-07 14:19:26 -07004797 fastboot_stop();
Shashank Mittald3e54dd2014-08-28 15:24:02 -07004798
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004799 if (target_is_emmc_boot())
4800 {
lijuanga40d6302015-07-20 20:10:13 +08004801#if FBCON_DISPLAY_MSG
lijuangde34d502016-02-26 16:04:50 +08004802 /* Exit keys' detection thread firstly */
4803 exit_menu_keys_detection();
lijuanga40d6302015-07-20 20:10:13 +08004804#endif
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004805 boot_linux_from_mmc();
4806 }
4807 else
4808 {
4809 boot_linux_from_flash();
4810 }
Dima Zavin214cc642009-01-26 11:16:21 -08004811}
4812
Chandan Uddaraju94183c02010-01-15 15:13:59 -08004813void cmd_reboot(const char *arg, void *data, unsigned sz)
4814{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004815 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08004816 fastboot_okay("");
4817 reboot_device(0);
4818}
4819
Mayank Grover351a75e2017-05-30 20:06:08 +05304820void cmd_set_active(const char *arg, void *data, unsigned sz)
4821{
Mayank Grover6ccc1c92017-07-04 17:36:46 +05304822 char *p, *sp = NULL;
Mayank Grover351a75e2017-05-30 20:06:08 +05304823 unsigned i,current_active_slot;
4824 const char *current_slot_suffix;
4825
4826 if (!partition_multislot_is_supported())
4827 {
4828 fastboot_fail("Command not supported");
4829 return;
4830 }
4831
4832 if (arg)
4833 {
Mayank Grover6ccc1c92017-07-04 17:36:46 +05304834 p = strtok_r((char *)arg, ":", &sp);
Mayank Grover5eb5f692017-07-19 20:16:04 +05304835 if (p)
Mayank Grover351a75e2017-05-30 20:06:08 +05304836 {
4837 current_active_slot = partition_find_active_slot();
4838
4839 /* Check if trying to make curent slot active */
4840 current_slot_suffix = SUFFIX_SLOT(current_active_slot);
Mayank Grover6ccc1c92017-07-04 17:36:46 +05304841 current_slot_suffix = strtok_r((char *)current_slot_suffix,
4842 (char *)suffix_delimiter, &sp);
4843
Mayank Grover5eb5f692017-07-19 20:16:04 +05304844 if (current_slot_suffix &&
4845 !strncmp(p, current_slot_suffix, strlen(current_slot_suffix)))
Mayank Grover351a75e2017-05-30 20:06:08 +05304846 {
4847 fastboot_okay("Slot already set active");
4848 return;
4849 }
4850 else
4851 {
4852 for (i = 0; i < AB_SUPPORTED_SLOTS; i++)
4853 {
4854 current_slot_suffix = SUFFIX_SLOT(i);
Mayank Grover6ccc1c92017-07-04 17:36:46 +05304855 current_slot_suffix = strtok_r((char *)current_slot_suffix,
4856 (char *)suffix_delimiter, &sp);
Mayank Grover5eb5f692017-07-19 20:16:04 +05304857 if (current_slot_suffix &&
4858 !strncmp(p, current_slot_suffix, strlen(current_slot_suffix)))
Mayank Grover351a75e2017-05-30 20:06:08 +05304859 {
lijuang8b03e5f2019-07-12 18:16:19 +08004860 partition_switch_slots(current_active_slot, i, true);
Mayank Grover351a75e2017-05-30 20:06:08 +05304861 publish_getvar_multislot_vars();
4862 fastboot_okay("");
4863 return;
4864 }
4865 }
4866 }
4867 }
4868 }
4869 fastboot_fail("Invalid slot suffix.");
4870 return;
4871}
4872
Mayank Grover98c4c742019-04-25 17:21:37 +05304873#if DYNAMIC_PARTITION_SUPPORT
4874void cmd_reboot_fastboot(const char *arg, void *data, unsigned sz)
4875{
4876 dprintf(INFO, "rebooting the device - userspace fastboot\n");
4877 if (send_recovery_cmd(RECOVERY_BOOT_FASTBOOT_CMD)) {
4878 dprintf(CRITICAL, "ERROR: Failed to update recovery commands\n");
4879 fastboot_fail("Failed to update recovery command");
4880 return;
4881 }
4882 fastboot_okay("");
4883 reboot_device(REBOOT_MODE_UNKNOWN);
4884
4885 //shouldn't come here.
4886 dprintf(CRITICAL, "ERROR: Failed to reboot device\n");
4887 return;
4888}
4889
4890void cmd_reboot_recovery(const char *arg, void *data, unsigned sz)
4891{
4892 dprintf(INFO, "rebooting the device - recovery\n");
4893 if (send_recovery_cmd(RECOVERY_BOOT_RECOVERY_CMD)) {
4894 dprintf(CRITICAL, "ERROR: Failed to update recovery commands\n");
4895 fastboot_fail("Failed to update recovery command");
4896 return;
4897 }
4898 fastboot_okay("");
4899 reboot_device(REBOOT_MODE_UNKNOWN);
4900
4901 //shouldn't come here.
4902 dprintf(CRITICAL, "ERROR: Failed to reboot device\n");
4903 return;
4904}
4905#endif
4906
Chandan Uddaraju94183c02010-01-15 15:13:59 -08004907void cmd_reboot_bootloader(const char *arg, void *data, unsigned sz)
4908{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004909 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08004910 fastboot_okay("");
4911 reboot_device(FASTBOOT_MODE);
4912}
4913
Ameya Thakur11cf1a62013-08-05 12:44:48 -07004914void cmd_oem_enable_charger_screen(const char *arg, void *data, unsigned size)
4915{
4916 dprintf(INFO, "Enabling charger screen check\n");
4917 device.charger_screen_enabled = 1;
4918 write_device_info(&device);
4919 fastboot_okay("");
4920}
4921
4922void cmd_oem_disable_charger_screen(const char *arg, void *data, unsigned size)
4923{
4924 dprintf(INFO, "Disabling charger screen check\n");
4925 device.charger_screen_enabled = 0;
4926 write_device_info(&device);
4927 fastboot_okay("");
4928}
4929
tracychuid184b912020-06-05 17:31:38 +08004930/*[20200605][TracyChui] Implement get Serial Number start*/
4931#if defined(ENABLE_PRODINFO_ACCESS)
4932void CmdOemEnableAdb(const char *arg, void *data, unsigned size)
4933{
4934 dprintf(INFO, "Enabling Adb\n");
4935 prod.is_adb_enabled = 1;
4936 write_prod_info(&prod);
4937 fastboot_okay("");
4938}
4939#endif
4940/*[20200605][TracyChui] Implement get Serial Number end*/
4941
lijuanga25d8bb2015-09-16 13:11:52 +08004942void cmd_oem_off_mode_charger(const char *arg, void *data, unsigned size)
4943{
4944 char *p = NULL;
4945 const char *delim = " \t\n\r";
Parth Dixit407f15b2016-01-07 14:47:37 +05304946 char *sp;
lijuanga25d8bb2015-09-16 13:11:52 +08004947
4948 if (arg) {
Parth Dixit407f15b2016-01-07 14:47:37 +05304949 p = strtok_r((char *)arg, delim, &sp);
lijuanga25d8bb2015-09-16 13:11:52 +08004950 if (p) {
4951 if (!strncmp(p, "0", 1)) {
4952 device.charger_screen_enabled = 0;
4953 } else if (!strncmp(p, "1", 1)) {
4954 device.charger_screen_enabled = 1;
4955 }
4956 }
4957 }
4958
4959 /* update charger_screen_enabled value for getvar
4960 * command
4961 */
4962 snprintf(charger_screen_enabled, MAX_RSP_SIZE, "%d",
4963 device.charger_screen_enabled);
4964
4965 write_device_info(&device);
4966 fastboot_okay("");
4967}
4968
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05304969void cmd_oem_select_display_panel(const char *arg, void *data, unsigned size)
4970{
4971 dprintf(INFO, "Selecting display panel %s\n", arg);
4972 if (arg)
4973 strlcpy(device.display_panel, arg,
4974 sizeof(device.display_panel));
4975 write_device_info(&device);
4976 fastboot_okay("");
4977}
4978
Shashank Mittal162244e2011-08-08 19:01:25 -07004979void cmd_oem_unlock(const char *arg, void *data, unsigned sz)
4980{
lijuang4ece1e72015-08-14 21:02:36 +08004981 set_device_unlock(UNLOCK, TRUE);
vijay kumarc65876c2015-04-24 13:29:16 +05304982}
4983
4984void cmd_oem_unlock_go(const char *arg, void *data, unsigned sz)
4985{
lijuang4ece1e72015-08-14 21:02:36 +08004986 if(!device.is_unlocked) {
vijay kumarc65876c2015-04-24 13:29:16 +05304987 if(!is_allow_unlock) {
4988 fastboot_fail("oem unlock is not allowed");
4989 return;
4990 }
4991
lijuang4ece1e72015-08-14 21:02:36 +08004992 set_device_unlock_value(UNLOCK, TRUE);
vijay kumarc65876c2015-04-24 13:29:16 +05304993
lijuang4ece1e72015-08-14 21:02:36 +08004994 /* wipe data */
vijay kumarc65876c2015-04-24 13:29:16 +05304995 struct recovery_message msg;
Kishor PK60a68212017-05-08 16:55:57 +05304996 memset(&msg, 0, sizeof(msg));
vijay kumarc65876c2015-04-24 13:29:16 +05304997 snprintf(msg.recovery, sizeof(msg.recovery), "recovery\n--wipe_data");
4998 write_misc(0, &msg, sizeof(msg));
4999
5000 fastboot_okay("");
5001 reboot_device(RECOVERY_MODE);
Shashank Mittald3e54dd2014-08-28 15:24:02 -07005002 }
5003 fastboot_okay("");
5004}
5005
Channagoud Kadabiad259832015-05-29 11:14:17 -07005006static int aboot_frp_unlock(char *pname, void *data, unsigned sz)
5007{
Mayank Grover70f8b0e2017-05-17 11:49:00 +05305008 int ret=1;
5009 bool authentication_success=false;
Channagoud Kadabiad259832015-05-29 11:14:17 -07005010
Mayank Grover70f8b0e2017-05-17 11:49:00 +05305011 /*
5012 Authentication method not implemented.
5013
5014 OEM to implement, authentication system which on successful validataion,
5015 calls write_allow_oem_unlock() with is_allow_unlock.
5016 */
5017#if 0
5018 authentication_success = oem_specific_auth_mthd();
5019#endif
5020
5021 if (authentication_success)
Channagoud Kadabiad259832015-05-29 11:14:17 -07005022 {
Mayank Grover70f8b0e2017-05-17 11:49:00 +05305023 is_allow_unlock = true;
5024 write_allow_oem_unlock(is_allow_unlock);
5025 ret = 0;
Channagoud Kadabiad259832015-05-29 11:14:17 -07005026 }
5027 return ret;
5028}
5029
Shashank Mittald3e54dd2014-08-28 15:24:02 -07005030void cmd_oem_lock(const char *arg, void *data, unsigned sz)
5031{
lijuang4ece1e72015-08-14 21:02:36 +08005032 set_device_unlock(UNLOCK, FALSE);
Shashank Mittal162244e2011-08-08 19:01:25 -07005033}
5034
Shashank Mittala0032282011-08-26 14:50:11 -07005035void cmd_oem_devinfo(const char *arg, void *data, unsigned sz)
5036{
lijuang511a2b52015-08-14 20:50:51 +08005037 char response[MAX_RSP_SIZE];
Ameya Thakur11cf1a62013-08-05 12:44:48 -07005038 snprintf(response, sizeof(response), "\tDevice tampered: %s", (device.is_tampered ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07005039 fastboot_info(response);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07005040 snprintf(response, sizeof(response), "\tDevice unlocked: %s", (device.is_unlocked ? "true" : "false"));
5041 fastboot_info(response);
Monika Singh292b3e92018-03-17 22:40:23 +05305042#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover912eaa62017-10-26 12:08:53 +05305043 if (VB_M <= target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +05305044 {
5045 snprintf(response, sizeof(response), "\tDevice critical unlocked: %s",
5046 (device.is_unlock_critical ? "true" : "false"));
5047 fastboot_info(response);
5048 }
Parth Dixitddbc7352015-10-18 03:13:31 +05305049#endif
Ameya Thakur11cf1a62013-08-05 12:44:48 -07005050 snprintf(response, sizeof(response), "\tCharger screen enabled: %s", (device.charger_screen_enabled ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07005051 fastboot_info(response);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05305052 snprintf(response, sizeof(response), "\tDisplay panel: %s", (device.display_panel));
5053 fastboot_info(response);
tracychuid184b912020-06-05 17:31:38 +08005054/*[20200605][TracyChui] Implement get Serial Number start*/
5055#if defined(ENABLE_PRODINFO_ACCESS)
5056 snprintf(response, sizeof(response), "\tAdb enabled: %s", prod.is_adb_enabled ? "true" : "false");
5057 fastboot_info(response);
5058#endif
5059/*[20200605][TracyChui] Implement get Serial Number end*/
Shashank Mittala0032282011-08-26 14:50:11 -07005060 fastboot_okay("");
5061}
5062
lijuang511a2b52015-08-14 20:50:51 +08005063void cmd_flashing_get_unlock_ability(const char *arg, void *data, unsigned sz)
5064{
5065 char response[MAX_RSP_SIZE];
5066 snprintf(response, sizeof(response), "\tget_unlock_ability: %d", is_allow_unlock);
5067 fastboot_info(response);
5068 fastboot_okay("");
5069}
5070
5071void cmd_flashing_lock_critical(const char *arg, void *data, unsigned sz)
5072{
lijuang4ece1e72015-08-14 21:02:36 +08005073 set_device_unlock(UNLOCK_CRITICAL, FALSE);
lijuang511a2b52015-08-14 20:50:51 +08005074}
5075
5076void cmd_flashing_unlock_critical(const char *arg, void *data, unsigned sz)
5077{
lijuang4ece1e72015-08-14 21:02:36 +08005078 set_device_unlock(UNLOCK_CRITICAL, TRUE);
lijuang511a2b52015-08-14 20:50:51 +08005079}
5080
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07005081void cmd_preflash(const char *arg, void *data, unsigned sz)
5082{
5083 fastboot_okay("");
5084}
5085
Mao Flynn7b379f32015-04-20 00:28:30 +08005086static uint8_t logo_header[LOGO_IMG_HEADER_SIZE];
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05305087
Mao Flynn7b379f32015-04-20 00:28:30 +08005088int splash_screen_check_header(logo_img_header *header)
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05305089{
Mao Flynn7b379f32015-04-20 00:28:30 +08005090 if (memcmp(header->magic, LOGO_IMG_MAGIC, 8))
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05305091 return -1;
Mao Flynn7b379f32015-04-20 00:28:30 +08005092 if (header->width == 0 || header->height == 0)
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05305093 return -1;
5094 return 0;
5095}
5096
Mao Flynn7b379f32015-04-20 00:28:30 +08005097int splash_screen_flash()
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07005098{
5099 struct ptentry *ptn;
5100 struct ptable *ptable;
Mao Flynn7b379f32015-04-20 00:28:30 +08005101 struct logo_img_header *header;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07005102 struct fbcon_config *fb_display = NULL;
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08005103
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05305104 ptable = flash_get_ptable();
5105 if (ptable == NULL) {
Mao Flynn7b379f32015-04-20 00:28:30 +08005106 dprintf(CRITICAL, "ERROR: Partition table not found\n");
5107 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05305108 }
Mao Flynn7b379f32015-04-20 00:28:30 +08005109
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05305110 ptn = ptable_find(ptable, "splash");
5111 if (ptn == NULL) {
5112 dprintf(CRITICAL, "ERROR: splash Partition not found\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08005113 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05305114 }
Mao Flynn7b379f32015-04-20 00:28:30 +08005115 if (flash_read(ptn, 0, (void *)logo_header, LOGO_IMG_HEADER_SIZE)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05305116 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08005117 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05305118 }
5119
Mao Flynn7b379f32015-04-20 00:28:30 +08005120 header = (struct logo_img_header *)logo_header;
5121 if (splash_screen_check_header(header)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05305122 dprintf(CRITICAL, "ERROR: Boot image header invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08005123 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05305124 }
5125
5126 fb_display = fbcon_display();
5127 if (fb_display) {
raghavendra ambadas554f4fd2018-10-09 15:50:40 +05305128 if (header->type && (header->blocks != 0) &&
5129 (UINT_MAX >= header->blocks * 512) &&
5130 ((header->blocks * 512) <= (fb_display->width *
5131 fb_display->height * (fb_display->bpp / 8)))) {
5132 /* RLE24 compressed data */
Mao Flynn7b379f32015-04-20 00:28:30 +08005133 uint8_t *base = (uint8_t *) fb_display->base + LOGO_IMG_OFFSET;
5134
5135 /* if the logo is full-screen size, remove "fbcon_clear()" */
5136 if ((header->width != fb_display->width)
5137 || (header->height != fb_display->height))
5138 fbcon_clear();
5139
5140 if (flash_read(ptn + LOGO_IMG_HEADER_SIZE, 0,
5141 (uint32_t *)base,
5142 (header->blocks * 512))) {
5143 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
5144 return -1;
5145 }
5146 fbcon_extract_to_screen(header, base);
5147 return 0;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07005148 }
Mao Flynn7b379f32015-04-20 00:28:30 +08005149
Veera Sundaram Sankaranc33416f2015-09-28 10:25:55 -07005150 if ((header->width > fb_display->width) || (header->height > fb_display->height)) {
5151 dprintf(CRITICAL, "Logo config greater than fb config. Fall back default logo\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08005152 return -1;
5153 }
5154
Vineet Bajaj99291ed2014-09-09 12:29:46 +05305155 uint8_t *base = (uint8_t *) fb_display->base;
Sachin Bhayare619e9e42017-05-15 13:10:31 +05305156 uint32_t fb_size = ROUNDUP(fb_display->width *
5157 fb_display->height *
5158 (fb_display->bpp / 8), 4096);
5159 uint32_t splash_size = ((((header->width * header->height *
5160 fb_display->bpp/8) + 511) >> 9) << 9);
5161
5162 if (splash_size > fb_size) {
5163 dprintf(CRITICAL, "ERROR: Splash image size invalid\n");
5164 return -1;
5165 }
5166
Mao Flynn7b379f32015-04-20 00:28:30 +08005167 if (flash_read(ptn + LOGO_IMG_HEADER_SIZE, 0,
5168 (uint32_t *)base,
5169 ((((header->width * header->height * fb_display->bpp/8) + 511) >> 9) << 9))) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05305170 fbcon_clear();
Vineet Bajaj99291ed2014-09-09 12:29:46 +05305171 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08005172 return -1;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07005173 }
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05305174 }
5175
Mao Flynn7b379f32015-04-20 00:28:30 +08005176 return 0;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05305177}
5178
Mao Flynn7b379f32015-04-20 00:28:30 +08005179int splash_screen_mmc()
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05305180{
5181 int index = INVALID_PTN;
5182 unsigned long long ptn = 0;
5183 struct fbcon_config *fb_display = NULL;
Mao Flynn7b379f32015-04-20 00:28:30 +08005184 struct logo_img_header *header;
Mao Flynn1893a7f2015-06-03 12:03:36 +08005185 uint32_t blocksize, realsize, readsize;
5186 uint8_t *base;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05305187
5188 index = partition_get_index("splash");
5189 if (index == 0) {
5190 dprintf(CRITICAL, "ERROR: splash Partition table not found\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08005191 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05305192 }
5193
5194 ptn = partition_get_offset(index);
5195 if (ptn == 0) {
5196 dprintf(CRITICAL, "ERROR: splash Partition invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08005197 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05305198 }
5199
Mao Flynn1893a7f2015-06-03 12:03:36 +08005200 mmc_set_lun(partition_get_lun(index));
5201
5202 blocksize = mmc_get_device_blocksize();
5203 if (blocksize == 0) {
5204 dprintf(CRITICAL, "ERROR:splash Partition invalid blocksize\n");
5205 return -1;
5206 }
5207
5208 fb_display = fbcon_display();
Channagoud Kadabidaf10a62015-07-22 11:51:55 -07005209 if (!fb_display)
5210 {
5211 dprintf(CRITICAL, "ERROR: fb config is not allocated\n");
5212 return -1;
5213 }
5214
Mao Flynn1893a7f2015-06-03 12:03:36 +08005215 base = (uint8_t *) fb_display->base;
5216
jialongjhan1efba002020-05-22 18:25:56 +08005217 //[Arima][8901][JialongJhan]splash image partition offset might be incorrect 20190503 Start
5218 if (mmc_read(ptn , (uint32_t *)(base + LOGO_IMG_OFFSET), blocksize)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05305219 dprintf(CRITICAL, "ERROR: Cannot read splash image header\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08005220 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05305221 }
jialongjhan1efba002020-05-22 18:25:56 +08005222 //[Arima][8901][JialongJhan]splash image partition offset might be incorrect 20190503 End
Mao Flynn1893a7f2015-06-03 12:03:36 +08005223 header = (struct logo_img_header *)(base + LOGO_IMG_OFFSET);
Mao Flynn7b379f32015-04-20 00:28:30 +08005224 if (splash_screen_check_header(header)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05305225 dprintf(CRITICAL, "ERROR: Splash image header invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08005226 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05305227 }
5228
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05305229 if (fb_display) {
raghavendra ambadas554f4fd2018-10-09 15:50:40 +05305230 if (header->type && (header->blocks != 0) &&
5231 (UINT_MAX >= header->blocks * 512 + LOGO_IMG_HEADER_SIZE) &&
5232 ((header->blocks * 512) <= (fb_display->width *
5233 fb_display->height * (fb_display->bpp / 8)))) {
5234 /* 1 RLE24 compressed data */
Mao Flynn1893a7f2015-06-03 12:03:36 +08005235 base += LOGO_IMG_OFFSET;
Mao Flynn7b379f32015-04-20 00:28:30 +08005236
Mao Flynn1893a7f2015-06-03 12:03:36 +08005237 realsize = header->blocks * 512;
5238 readsize = ROUNDUP((realsize + LOGO_IMG_HEADER_SIZE), blocksize) - blocksize;
5239
5240 /* if the logo is not full-screen size, clean screen */
Mao Flynn7b379f32015-04-20 00:28:30 +08005241 if ((header->width != fb_display->width)
5242 || (header->height != fb_display->height))
5243 fbcon_clear();
5244
Sachin Bhayare619e9e42017-05-15 13:10:31 +05305245 uint32_t fb_size = ROUNDUP(fb_display->width *
5246 fb_display->height *
5247 (fb_display->bpp / 8), 4096);
5248
5249 if (readsize > fb_size) {
5250 dprintf(CRITICAL, "ERROR: Splash image size invalid\n");
5251 return -1;
5252 }
5253
jialongjhan1efba002020-05-22 18:25:56 +08005254 //[Arima][8901][JialongJhan]splash image partition offset might be incorrect 20190503 Start
5255 if (mmc_read(ptn + blocksize, (uint32_t *)(base + blocksize), readsize)) {
Mao Flynn7b379f32015-04-20 00:28:30 +08005256 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
5257 return -1;
5258 }
jialongjhan1efba002020-05-22 18:25:56 +08005259 //[Arima][8901][JialongJhan]splash image partition offset might be incorrect 20190503 End
Mao Flynn7b379f32015-04-20 00:28:30 +08005260
Mao Flynn1893a7f2015-06-03 12:03:36 +08005261 fbcon_extract_to_screen(header, (base + LOGO_IMG_HEADER_SIZE));
5262 } else { /* 2 Raw BGR data */
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05305263
Veera Sundaram Sankaranc33416f2015-09-28 10:25:55 -07005264 if ((header->width > fb_display->width) || (header->height > fb_display->height)) {
5265 dprintf(CRITICAL, "Logo config greater than fb config. Fall back default logo\n");
Mao Flynn1893a7f2015-06-03 12:03:36 +08005266 return -1;
5267 }
5268
5269 realsize = header->width * header->height * fb_display->bpp / 8;
5270 readsize = ROUNDUP((realsize + LOGO_IMG_HEADER_SIZE), blocksize) - blocksize;
5271
5272 if (blocksize == LOGO_IMG_HEADER_SIZE) { /* read the content directly */
Kishor PKee5c0a32018-03-06 16:49:46 +05305273 if (mmc_read((ptn + PLL_CODES_OFFSET + LOGO_IMG_HEADER_SIZE), (uint32_t *)base, readsize)) {
Mao Flynn1893a7f2015-06-03 12:03:36 +08005274 fbcon_clear();
5275 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
5276 return -1;
5277 }
5278 } else {
Kishor PKee5c0a32018-03-06 16:49:46 +05305279 if (mmc_read(ptn + PLL_CODES_OFFSET + blocksize ,
Mao Flynn1893a7f2015-06-03 12:03:36 +08005280 (uint32_t *)(base + LOGO_IMG_OFFSET + blocksize), readsize)) {
5281 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
5282 return -1;
5283 }
5284 memmove(base, (base + LOGO_IMG_OFFSET + LOGO_IMG_HEADER_SIZE), realsize);
5285 }
5286 }
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05305287 }
5288
Mao Flynn7b379f32015-04-20 00:28:30 +08005289 return 0;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05305290}
5291
Mao Flynn7b379f32015-04-20 00:28:30 +08005292int fetch_image_from_partition()
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05305293{
5294 if (target_is_emmc_boot()) {
5295 return splash_screen_mmc();
5296 } else {
5297 return splash_screen_flash();
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07005298 }
5299}
5300
Mayank Grover351a75e2017-05-30 20:06:08 +05305301void publish_getvar_multislot_vars()
5302{
5303 int i,count;
5304 static bool published = false;
5305 static char slot_count[MAX_RSP_SIZE];
5306 static struct ab_slot_info slot_info[AB_SUPPORTED_SLOTS];
5307 static char active_slot_suffix[MAX_RSP_SIZE];
5308 static char has_slot_pname[NUM_PARTITIONS][MAX_GET_VAR_NAME_SIZE];
5309 static char has_slot_reply[NUM_PARTITIONS][MAX_RSP_SIZE];
5310 const char *tmp;
5311 char tmpbuff[MAX_GET_VAR_NAME_SIZE];
Mayank Grover2bd84bb2017-06-20 11:06:07 +05305312 signed active_slt;
Mayank Grover351a75e2017-05-30 20:06:08 +05305313
5314 if (!published)
5315 {
5316 /* Update slot meta info */
5317 count = partition_fill_partition_meta(has_slot_pname, has_slot_reply,
5318 partition_get_partition_count());
5319 for(i=0; i<count; i++)
5320 {
5321 memset(tmpbuff, 0, MAX_GET_VAR_NAME_SIZE);
Mayank Grover2bd84bb2017-06-20 11:06:07 +05305322 snprintf(tmpbuff, MAX_GET_VAR_NAME_SIZE,"has-slot:%s",
5323 has_slot_pname[i]);
5324 strlcpy(has_slot_pname[i], tmpbuff, MAX_GET_VAR_NAME_SIZE);
Mayank Grover351a75e2017-05-30 20:06:08 +05305325 fastboot_publish(has_slot_pname[i], has_slot_reply[i]);
5326 }
5327
5328 for (i=0; i<AB_SUPPORTED_SLOTS; i++)
5329 {
5330 tmp = SUFFIX_SLOT(i);
Mayank Grover7b8a8f82017-10-27 13:07:59 +05305331 tmp++; // to remove "_" from slot_suffix.
Mayank Grover2bd84bb2017-06-20 11:06:07 +05305332 snprintf(slot_info[i].slot_is_unbootable, sizeof(slot_info[i].slot_is_unbootable),
5333 "slot-unbootable:%s", tmp);
5334 snprintf(slot_info[i].slot_is_active, sizeof(slot_info[i].slot_is_active),
5335 "slot-active:%s", tmp);
5336 snprintf(slot_info[i].slot_is_succesful, sizeof(slot_info[i].slot_is_succesful),
5337 "slot-success:%s", tmp);
5338 snprintf(slot_info[i].slot_retry_count, sizeof(slot_info[i].slot_retry_count),
5339 "slot-retry-count:%s", tmp);
Mayank Grover351a75e2017-05-30 20:06:08 +05305340 fastboot_publish(slot_info[i].slot_is_unbootable,
5341 slot_info[i].slot_is_unbootable_rsp);
5342 fastboot_publish(slot_info[i].slot_is_active,
5343 slot_info[i].slot_is_active_rsp);
5344 fastboot_publish(slot_info[i].slot_is_succesful,
5345 slot_info[i].slot_is_succesful_rsp);
5346 fastboot_publish(slot_info[i].slot_retry_count,
5347 slot_info[i].slot_retry_count_rsp);
5348 }
5349 fastboot_publish("current-slot", active_slot_suffix);
5350 snprintf(slot_count, sizeof(slot_count),"%d", AB_SUPPORTED_SLOTS);
5351 fastboot_publish("slot-count", slot_count);
5352 published = true;
5353 }
5354
Mayank Grover2bd84bb2017-06-20 11:06:07 +05305355 active_slt = partition_find_active_slot();
5356 if (active_slt != INVALID)
Mayank Grover7b8a8f82017-10-27 13:07:59 +05305357 {
5358 tmp = SUFFIX_SLOT(active_slt);
5359 tmp++; // to remove "_" from slot_suffix.
5360 snprintf(active_slot_suffix, sizeof(active_slot_suffix), "%s", tmp);
5361 }
Mayank Grover2bd84bb2017-06-20 11:06:07 +05305362 else
5363 strlcpy(active_slot_suffix, "INVALID", sizeof(active_slot_suffix));
5364
Mayank Grover351a75e2017-05-30 20:06:08 +05305365 /* Update partition meta information */
5366 partition_fill_slot_meta(slot_info);
5367 return;
5368}
5369
lijuang4ece1e72015-08-14 21:02:36 +08005370void get_product_name(unsigned char *buf)
5371{
5372 snprintf((char*)buf, MAX_RSP_SIZE, "%s", TARGET(BOARD));
5373 return;
5374}
5375
Mayank Grover7b8a8f82017-10-27 13:07:59 +05305376#if PRODUCT_IOT
5377void get_bootloader_version_iot(unsigned char *buf)
5378{
5379 if (buf != NULL)
5380 {
5381 strlcpy(buf, TARGET(BOARD), MAX_VERSION_LEN);
5382 strlcat(buf, "-", MAX_VERSION_LEN);
5383 strlcat(buf, PRODUCT_IOT_VERSION, MAX_VERSION_LEN);
5384 }
5385 return;
5386}
5387#endif
5388
lijuang4ece1e72015-08-14 21:02:36 +08005389void get_bootloader_version(unsigned char *buf)
5390{
jhchen7a504d12020-04-24 15:45:57 +08005391 snprintf((char*)buf, MAX_RSP_SIZE, "%s", device.bootloader_version);
lijuang4ece1e72015-08-14 21:02:36 +08005392 return;
5393}
5394
5395void get_baseband_version(unsigned char *buf)
5396{
jhchen7a504d12020-04-24 15:45:57 +08005397 snprintf((char*)buf, MAX_RSP_SIZE, "%s", device.radio_version);
lijuang4ece1e72015-08-14 21:02:36 +08005398 return;
5399}
5400
Monika Singh32a09f72018-03-14 13:08:29 +05305401bool is_device_locked_critical()
5402{
5403 return device.is_unlock_critical ? false:true;
5404}
5405
lijuang4ece1e72015-08-14 21:02:36 +08005406bool is_device_locked()
5407{
5408 return device.is_unlocked ? false:true;
5409}
5410
Monika Singh32a09f72018-03-14 13:08:29 +05305411bool is_verity_enforcing()
5412{
5413 return device.verity_mode ? true:false;
5414}
5415
Amol Jadi5edf3552013-07-23 14:15:34 -07005416/* register commands and variables for fastboot */
5417void aboot_fastboot_register_commands(void)
5418{
Sridhar Parasurame94e8152014-10-24 14:06:03 -07005419 int i;
lijuangf16461c2015-08-03 17:09:34 +08005420 char hw_platform_buf[MAX_RSP_SIZE];
Amol Jadi5edf3552013-07-23 14:15:34 -07005421
Sridhar Parasurame94e8152014-10-24 14:06:03 -07005422 struct fastboot_cmd_desc cmd_list[] = {
lijuang511a2b52015-08-14 20:50:51 +08005423 /* By default the enabled list is empty. */
5424 {"", NULL},
5425 /* move commands enclosed within the below ifndef to here
5426 * if they need to be enabled in user build.
5427 */
Sridhar Parasurame94e8152014-10-24 14:06:03 -07005428#ifndef DISABLE_FASTBOOT_CMDS
lijuang511a2b52015-08-14 20:50:51 +08005429 /* Register the following commands only for non-user builds */
5430 {"flash:", cmd_flash},
5431 {"erase:", cmd_erase},
5432 {"boot", cmd_boot},
5433 {"continue", cmd_continue},
5434 {"reboot", cmd_reboot},
5435 {"reboot-bootloader", cmd_reboot_bootloader},
5436 {"oem unlock", cmd_oem_unlock},
5437 {"oem unlock-go", cmd_oem_unlock_go},
5438 {"oem lock", cmd_oem_lock},
5439 {"flashing unlock", cmd_oem_unlock},
5440 {"flashing lock", cmd_oem_lock},
5441 {"flashing lock_critical", cmd_flashing_lock_critical},
5442 {"flashing unlock_critical", cmd_flashing_unlock_critical},
5443 {"flashing get_unlock_ability", cmd_flashing_get_unlock_ability},
5444 {"oem device-info", cmd_oem_devinfo},
5445 {"preflash", cmd_preflash},
5446 {"oem enable-charger-screen", cmd_oem_enable_charger_screen},
5447 {"oem disable-charger-screen", cmd_oem_disable_charger_screen},
lijuanga25d8bb2015-09-16 13:11:52 +08005448 {"oem off-mode-charge", cmd_oem_off_mode_charger},
lijuang511a2b52015-08-14 20:50:51 +08005449 {"oem select-display-panel", cmd_oem_select_display_panel},
Mayank Grover6ccc1c92017-07-04 17:36:46 +05305450 {"set_active",cmd_set_active},
Mayank Grover98c4c742019-04-25 17:21:37 +05305451#if DYNAMIC_PARTITION_SUPPORT
5452 {"reboot-fastboot",cmd_reboot_fastboot},
5453 {"reboot-recovery",cmd_reboot_recovery},
5454#endif
Channagoud Kadabi736c4962015-08-21 11:56:52 -07005455#if UNITTEST_FW_SUPPORT
Channagoud Kadabid5ddf482015-09-28 10:31:35 -07005456 {"oem run-tests", cmd_oem_runtests},
Channagoud Kadabi736c4962015-08-21 11:56:52 -07005457#endif
Sridhar Parasurame94e8152014-10-24 14:06:03 -07005458#endif
tracychuid184b912020-06-05 17:31:38 +08005459/*[20200605][TracyChui] Implement get Serial Number start*/
5460#if defined(ENABLE_PRODINFO_ACCESS)
5461 {"oem adb_enable", CmdOemEnableAdb},
5462#endif
5463/*[20200605][TracyChui] Implement get Serial Number end*/
lijuang511a2b52015-08-14 20:50:51 +08005464 };
Sridhar Parasurame94e8152014-10-24 14:06:03 -07005465
5466 int fastboot_cmds_count = sizeof(cmd_list)/sizeof(cmd_list[0]);
5467 for (i = 1; i < fastboot_cmds_count; i++)
5468 fastboot_register(cmd_list[i].name,cmd_list[i].cb);
5469
Amol Jadi5edf3552013-07-23 14:15:34 -07005470 /* publish variables and their values */
5471 fastboot_publish("product", TARGET(BOARD));
5472 fastboot_publish("kernel", "lk");
5473 fastboot_publish("serialno", sn_buf);
5474
Mayank Grovera64dfbe2018-05-17 14:06:34 +05305475 /*publish hw-revision major(upper 16 bits) and minor(lower 16 bits)*/
5476 snprintf(soc_version_str, MAX_RSP_SIZE, "%x", board_soc_version());
5477 fastboot_publish("hw-revision", soc_version_str);
5478
Amol Jadi5edf3552013-07-23 14:15:34 -07005479 /*
5480 * partition info is supported only for emmc partitions
5481 * Calling this for NAND prints some error messages which
5482 * is harmless but misleading. Avoid calling this for NAND
5483 * devices.
5484 */
5485 if (target_is_emmc_boot())
Mayank Grover7b8a8f82017-10-27 13:07:59 +05305486 publish_getvar_partition_info(part_info, partition_get_partition_count());
Amol Jadi5edf3552013-07-23 14:15:34 -07005487
Mayank Grover351a75e2017-05-30 20:06:08 +05305488 if (partition_multislot_is_supported())
5489 publish_getvar_multislot_vars();
5490
Amol Jadi5edf3552013-07-23 14:15:34 -07005491 /* Max download size supported */
lijuang2fbed912019-07-12 14:15:05 +08005492#if !VERIFIED_BOOT_2
Ameya Thakur11cf1a62013-08-05 12:44:48 -07005493 snprintf(max_download_size, MAX_RSP_SIZE, "\t0x%x",
5494 target_get_max_flash_size());
lijuang2fbed912019-07-12 14:15:05 +08005495#else
5496 snprintf(max_download_size, MAX_RSP_SIZE, "\t0x%x",
5497 SUB_SALT_BUFF_OFFSET(target_get_max_flash_size()));
5498#endif
5499
Amol Jadi5edf3552013-07-23 14:15:34 -07005500 fastboot_publish("max-download-size", (const char *) max_download_size);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07005501 /* Is the charger screen check enabled */
5502 snprintf(charger_screen_enabled, MAX_RSP_SIZE, "%d",
5503 device.charger_screen_enabled);
5504 fastboot_publish("charger-screen-enabled",
5505 (const char *) charger_screen_enabled);
tracychuid184b912020-06-05 17:31:38 +08005506/*[20200605][TracyChui] Implement get Serial Number start*/
5507#if defined(ENABLE_PRODINFO_ACCESS)
5508 read_prod_info(&prod);
5509 snprintf(AdbEnable, MAX_RSP_SIZE, "%d",
5510 prod.is_adb_enabled);
5511 fastboot_publish("adb-enabled",
5512 (const char *) AdbEnable);
5513#endif
5514/*[20200605][TracyChui] Implement get Serial Number end*/
lijuanga25d8bb2015-09-16 13:11:52 +08005515 fastboot_publish("off-mode-charge", (const char *) charger_screen_enabled);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05305516 snprintf(panel_display_mode, MAX_RSP_SIZE, "%s",
5517 device.display_panel);
5518 fastboot_publish("display-panel",
5519 (const char *) panel_display_mode);
Mukesh Ojhabb6e7f32018-03-02 15:04:06 +05305520
5521 if (target_is_emmc_boot())
5522 {
5523 mmc_blocksize = mmc_get_device_blocksize();
5524 }
5525 else
5526 {
5527 mmc_blocksize = flash_block_size();
5528 }
5529 snprintf(block_size_string, MAX_RSP_SIZE, "0x%x", mmc_blocksize);
5530 fastboot_publish("erase-block-size", (const char *) block_size_string);
5531 fastboot_publish("logical-block-size", (const char *) block_size_string);
Mayank Grover7b8a8f82017-10-27 13:07:59 +05305532#if PRODUCT_IOT
5533 get_bootloader_version_iot(&bootloader_version_string);
5534 fastboot_publish("version-bootloader", (const char *) bootloader_version_string);
5535
5536 /* Version baseband is n/a for apq iot devices */
5537 fastboot_publish("version-baseband", "N/A");
Mayank Grover7b8a8f82017-10-27 13:07:59 +05305538#else
Channagoud Kadabiaafbbfc2015-03-04 17:52:56 -08005539 fastboot_publish("version-bootloader", (const char *) device.bootloader_version);
5540 fastboot_publish("version-baseband", (const char *) device.radio_version);
Mayank Grover7b8a8f82017-10-27 13:07:59 +05305541#endif
lijuangf16461c2015-08-03 17:09:34 +08005542 fastboot_publish("secure", is_secure_boot_enable()? "yes":"no");
Mayank Grover7b8a8f82017-10-27 13:07:59 +05305543 fastboot_publish("unlocked", device.is_unlocked ? "yes":"no");
lijuangf16461c2015-08-03 17:09:34 +08005544 smem_get_hw_platform_name((unsigned char *) hw_platform_buf, sizeof(hw_platform_buf));
5545 snprintf(get_variant, MAX_RSP_SIZE, "%s %s", hw_platform_buf,
5546 target_is_emmc_boot()? "eMMC":"UFS");
5547 fastboot_publish("variant", (const char *) get_variant);
lijuang65c5a822015-08-29 16:35:36 +08005548#if CHECK_BAT_VOLTAGE
lijuang102dfa92015-10-09 18:31:03 +08005549 update_battery_status();
lijuang65c5a822015-08-29 16:35:36 +08005550 fastboot_publish("battery-voltage", (const char *) battery_voltage);
lijuang102dfa92015-10-09 18:31:03 +08005551 fastboot_publish("battery-soc-ok", (const char *) battery_soc_ok);
lijuang65c5a822015-08-29 16:35:36 +08005552#endif
Mayank Grover98c4c742019-04-25 17:21:37 +05305553 if (target_dynamic_partition_supported())
5554 fastboot_publish("is-userspace", "no");
Amol Jadi5edf3552013-07-23 14:15:34 -07005555}
5556
Brian Swetland9c4c0752009-01-25 16:23:50 -08005557void aboot_init(const struct app_descriptor *app)
5558{
Shashank Mittal4f99a882010-02-01 13:58:50 -08005559 unsigned reboot_mode = 0;
Mayank Grover351a75e2017-05-30 20:06:08 +05305560 int boot_err_type = 0;
5561 int boot_slot = INVALID;
jessicatseng67c4fd02020-05-26 13:17:56 +08005562//<2020/05/26-JessicaTseng, Add low battery icon
5563 int vbat = 0;
5564 char boot_vbat[MAX_RSP_SIZE];
5565//>2020/05/26-JessicaTseng
Chandan Uddarajubedca152010-06-02 23:05:15 -07005566
lijuang39831732016-01-08 17:49:02 +08005567 /* Initialise wdog to catch early lk crashes */
5568#if WDOG_SUPPORT
5569 msm_wdog_init();
5570#endif
5571
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07005572 /* Setup page size information for nv storage */
Shashank Mittald8c42bf2010-06-09 15:44:28 -07005573 if (target_is_emmc_boot())
5574 {
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07005575 page_size = mmc_page_size();
Shashank Mittald8c42bf2010-06-09 15:44:28 -07005576 page_mask = page_size - 1;
Gaurav Nebhwani4d2389c2016-03-17 21:10:05 +05305577 mmc_blocksize = mmc_get_device_blocksize();
5578 mmc_blocksize_mask = mmc_blocksize - 1;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07005579 }
5580 else
5581 {
5582 page_size = flash_page_size();
5583 page_mask = page_size - 1;
5584 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07005585 ASSERT((MEMBASE + MEMSIZE) > MEMBASE);
5586
Ameya Thakur11cf1a62013-08-05 12:44:48 -07005587 read_device_info(&device);
tracychuid184b912020-06-05 17:31:38 +08005588/*[20200605][TracyChui] Implement get Serial Number start*/
5589#if defined(ENABLE_PRODINFO_ACCESS)
5590 read_prod_info(&prod);
5591#endif
5592/*[20200605][TracyChui] Implement get Serial Number end*/
vijay kumarc65876c2015-04-24 13:29:16 +05305593 read_allow_oem_unlock(&device);
Shashank Mittal162244e2011-08-08 19:01:25 -07005594
Mayank Grover351a75e2017-05-30 20:06:08 +05305595 /* Detect multi-slot support */
5596 if (partition_multislot_is_supported())
5597 {
5598 boot_slot = partition_find_active_slot();
5599 if (boot_slot == INVALID)
5600 {
5601 boot_into_fastboot = true;
5602 dprintf(INFO, "Active Slot: (INVALID)\n");
5603 }
5604 else
5605 {
5606 /* Setting the state of system to boot active slot */
5607 partition_mark_active_slot(boot_slot);
5608 dprintf(INFO, "Active Slot: (%s)\n", SUFFIX_SLOT(boot_slot));
5609 }
5610 }
5611
Greg Griscod6250552011-06-29 14:40:23 -07005612 target_serialno((unsigned char *) sn_buf);
tracychuid184b912020-06-05 17:31:38 +08005613/*[20200605][TracyChui] Implement get Serial Number start*/
5614#if defined(ENABLE_PRODINFO_ACCESS)
5615 dprintf(CRITICAL,"serial number: %s\n",sn_buf);
5616#else
Ajay Dudanib06c05f2011-05-12 14:46:10 -07005617 dprintf(SPEW,"serial number: %s\n",sn_buf);
tracychuid184b912020-06-05 17:31:38 +08005618#endif
5619/*[20200605][TracyChui] Implement get Serial Number end*/
5620/*[20200605][TracyChui] Implement get Serial Number start */
5621#if defined(ENABLE_PRODINFO_ACCESS)
5622 read_prod_info(&prod);
5623 snprintf((char *)cust_sn_buf, PRODINFO_MAX_SSN_LEN + 1, "%s", prod.ssn);
5624 dprintf(CRITICAL,"customer serial number: %s\n", cust_sn_buf);
5625 snprintf((char *)factory_sn_buf, PRODINFO_MAX_ISN_LEN + 1, "%s", prod.isn);
5626 dprintf(CRITICAL,"factory serial number: %s\n", factory_sn_buf);
5627#endif
5628 /*[20200605][TracyChui] Implement get Serial Number end */
Dhaval Patel223ec952013-07-18 14:49:44 -07005629 memset(display_panel_buf, '\0', MAX_PANEL_BUF_SIZE);
5630
Matthew Qindefd5562014-07-11 18:02:40 +08005631 /*
5632 * Check power off reason if user force reset,
5633 * if yes phone will do normal boot.
5634 */
5635 if (is_user_force_reset())
5636 goto normal_boot;
5637
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07005638 /* Check if we should do something other than booting up */
Ameya Thakur0b9c2442013-05-10 13:22:56 -07005639 if (keys_get_state(KEY_VOLUMEUP) && keys_get_state(KEY_VOLUMEDOWN))
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07005640 {
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03005641 dprintf(ALWAYS,"dload mode key sequence detected\n");
lijuang395b5e62015-11-19 17:39:44 +08005642 reboot_device(EMERGENCY_DLOAD);
5643 dprintf(CRITICAL,"Failed to reboot into dload mode\n");
5644
Ameya Thakur0b9c2442013-05-10 13:22:56 -07005645 boot_into_fastboot = true;
5646 }
5647 if (!boot_into_fastboot)
5648 {
5649 if (keys_get_state(KEY_HOME) || keys_get_state(KEY_VOLUMEUP))
5650 boot_into_recovery = 1;
5651 if (!boot_into_recovery &&
5652 (keys_get_state(KEY_BACK) || keys_get_state(KEY_VOLUMEDOWN)))
Pavel Nedev5d91d412013-04-29 11:34:24 +03005653 boot_into_fastboot = true;
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07005654 }
Shashank Mittald8c42bf2010-06-09 15:44:28 -07005655 #if NO_KEYPAD_DRIVER
Kinson Chik0b1c8162011-08-31 16:31:57 -07005656 if (fastboot_trigger())
Pavel Nedev5d91d412013-04-29 11:34:24 +03005657 boot_into_fastboot = true;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07005658 #endif
Chandan Uddarajubedca152010-06-02 23:05:15 -07005659
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07005660#if USE_PON_REBOOT_REG
5661 reboot_mode = check_hard_reboot_mode();
5662#else
Ajay Dudani77421292010-10-27 19:34:06 -07005663 reboot_mode = check_reboot_mode();
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07005664#endif
5665 if (reboot_mode == RECOVERY_MODE)
5666 {
Ajay Dudani77421292010-10-27 19:34:06 -07005667 boot_into_recovery = 1;
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07005668 }
5669 else if(reboot_mode == FASTBOOT_MODE)
5670 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03005671 boot_into_fastboot = true;
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07005672 }
5673 else if(reboot_mode == ALARM_BOOT)
5674 {
Matthew Qind886f3c2014-01-17 16:52:01 +08005675 boot_reason_alarm = true;
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07005676 }
Monika Singhb0db4b82018-09-26 12:18:02 +05305677#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover912eaa62017-10-26 12:08:53 +05305678 else if (VB_M <= target_get_vb_version())
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07005679 {
Mayank Grover889be1b2017-09-12 20:12:23 +05305680 if (reboot_mode == DM_VERITY_ENFORCING)
5681 {
5682 device.verity_mode = 1;
5683 write_device_info(&device);
5684 }
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07005685#if ENABLE_VB_ATTEST
Mayank Grover889be1b2017-09-12 20:12:23 +05305686 else if (reboot_mode == DM_VERITY_EIO)
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07005687#else
Mayank Grover889be1b2017-09-12 20:12:23 +05305688 else if (reboot_mode == DM_VERITY_LOGGING)
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07005689#endif
Mayank Grover889be1b2017-09-12 20:12:23 +05305690 {
5691 device.verity_mode = 0;
5692 write_device_info(&device);
5693 }
5694 else if (reboot_mode == DM_VERITY_KEYSCLEAR)
5695 {
5696 if(send_delete_keys_to_tz())
5697 ASSERT(0);
5698 }
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07005699 }
Parth Dixitddbc7352015-10-18 03:13:31 +05305700#endif
Ajay Dudani77421292010-10-27 19:34:06 -07005701
jialongjhan1efba002020-05-22 18:25:56 +08005702//[Arima][8901][JialongJhan][20200522]modify Display splash screen if enabled Start
5703 /* Display splash screen if enabled */
5704#if DISPLAY_SPLASH_SCREEN
5705#if NO_ALARM_DISPLAY
5706 if (!check_alarm_boot()) {
5707#endif
5708 dprintf(SPEW, "Display Init: Start\n");
5709#if DISPLAY_HDMI_PRIMARY
5710 if (!strlen(device.display_panel))
5711 strlcpy(device.display_panel, DISPLAY_PANEL_HDMI,
5712 sizeof(device.display_panel));
5713#endif
5714#if ENABLE_WBC
5715 /* Wait if the display shutdown is in progress */
5716 while(pm_app_display_shutdown_in_prgs());
5717 if (!pm_appsbl_display_init_done())
5718 target_display_init(device.display_panel);
5719 else
5720 display_image_on_screen();
5721#else
5722 target_display_init(device.display_panel);
5723#endif
5724 dprintf(SPEW, "Display Init: Done\n");
5725#if NO_ALARM_DISPLAY
5726 }
5727#endif
5728#endif
5729
5730//[Arima][8901][JialongJhan][20200522]modify Display splash screen if enabled End
5731
Matthew Qindefd5562014-07-11 18:02:40 +08005732normal_boot:
Pavel Nedev5d91d412013-04-29 11:34:24 +03005733 if (!boot_into_fastboot)
Shashank Mittald8c42bf2010-06-09 15:44:28 -07005734 {
jessicatseng67c4fd02020-05-26 13:17:56 +08005735//<2020/05/26-JessicaTseng, Add low battery icon
5736 if(!target_pause_for_battery_charge())
5737 {
5738 vbat = target_get_battery_voltage();
5739 snprintf(boot_vbat, MAX_RSP_SIZE, "%d", vbat);
5740 dprintf(CRITICAL,"battery_voltage: %s\n", boot_vbat);
5741 if(vbat < 3500000)
5742 {
5743 display_lowbattery_image_on_screen();
5744 //[Arima][8901][JialongJhan] Command mode reflash screen when low battery logo shown 20190516 Start
5745 msm_display_flush();
5746 //[Arima][8901][JialongJhan] Command mode reflash screen when low battery logo shown 20190516 End
5747 dprintf(CRITICAL,"Low battery, cannot boot up...\n");
5748 mdelay(3000);
5749 shutdown_device();
5750 }
5751 }
5752//>2020/05/26-JessicaTseng
5753
Pavel Nedev5d91d412013-04-29 11:34:24 +03005754 if (target_is_emmc_boot())
Shashank Mittala0032282011-08-26 14:50:11 -07005755 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03005756 if(emmc_recovery_init())
5757 dprintf(ALWAYS,"error in emmc_recovery_init\n");
5758 if(target_use_signed_kernel())
Shashank Mittala0032282011-08-26 14:50:11 -07005759 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03005760 if((device.is_unlocked) || (device.is_tampered))
5761 {
5762 #ifdef TZ_TAMPER_FUSE
Monika Singh98257462018-06-06 11:28:49 +05305763 set_tamper_fuse_cmd(HLOS_IMG_TAMPER_FUSE);
Pavel Nedev5d91d412013-04-29 11:34:24 +03005764 #endif
5765 #if USE_PCOM_SECBOOT
5766 set_tamper_flag(device.is_tampered);
5767 #endif
5768 }
Shashank Mittala0032282011-08-26 14:50:11 -07005769 }
Amit Blay6281ebc2015-01-11 14:44:08 +02005770
Mayank Grover351a75e2017-05-30 20:06:08 +05305771retry_boot:
5772 /* Trying to boot active partition */
5773 if (partition_multislot_is_supported())
5774 {
5775 boot_slot = partition_find_boot_slot();
5776 partition_mark_active_slot(boot_slot);
5777 if (boot_slot == INVALID)
5778 goto fastboot;
5779 }
5780
5781 boot_err_type = boot_linux_from_mmc();
5782 switch (boot_err_type)
5783 {
5784 case ERR_INVALID_PAGE_SIZE:
5785 case ERR_DT_PARSE:
5786 case ERR_ABOOT_ADDR_OVERLAP:
Mayank Grover10cabbe2017-10-30 19:11:05 +05305787 case ERR_INVALID_BOOT_MAGIC:
Mayank Grover351a75e2017-05-30 20:06:08 +05305788 if(partition_multislot_is_supported())
Mayank Grover10cabbe2017-10-30 19:11:05 +05305789 {
5790 /*
5791 * Deactivate current slot, as it failed to
5792 * boot, and retry next slot.
5793 */
5794 partition_deactivate_slot(boot_slot);
Mayank Grover351a75e2017-05-30 20:06:08 +05305795 goto retry_boot;
Mayank Grover10cabbe2017-10-30 19:11:05 +05305796 }
Mayank Grover351a75e2017-05-30 20:06:08 +05305797 else
5798 break;
Mayank Grover351a75e2017-05-30 20:06:08 +05305799 default:
5800 break;
5801 /* going to fastboot menu */
5802 }
Shashank Mittala0032282011-08-26 14:50:11 -07005803 }
Pavel Nedev5d91d412013-04-29 11:34:24 +03005804 else
5805 {
5806 recovery_init();
5807 #if USE_PCOM_SECBOOT
5808 if((device.is_unlocked) || (device.is_tampered))
5809 set_tamper_flag(device.is_tampered);
5810 #endif
5811 boot_linux_from_flash();
5812 }
5813 dprintf(CRITICAL, "ERROR: Could not do normal boot. Reverting "
5814 "to fastboot mode.\n");
Shashank Mittald8c42bf2010-06-09 15:44:28 -07005815 }
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07005816
Mayank Grover351a75e2017-05-30 20:06:08 +05305817fastboot:
jialongjhan1efba002020-05-22 18:25:56 +08005818
5819 //[Arima][8901][JialongJhan]Show Fastboot logo 1 second 20190627 Start
5820 mdelay(1000);
5821 //[Arima][8901][JialongJhan]Show Fastboot logo 1 second 20190627 End
5822
Amol Jadi5edf3552013-07-23 14:15:34 -07005823 /* We are here means regular boot did not happen. Start fastboot. */
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07005824
Amol Jadi5edf3552013-07-23 14:15:34 -07005825 /* register aboot specific fastboot commands */
5826 aboot_fastboot_register_commands();
Amol Jadi57abe4c2011-05-24 15:47:27 -07005827
Amol Jadi5edf3552013-07-23 14:15:34 -07005828 /* dump partition table for debug info */
Kinson Chikf1a43512011-07-14 11:28:39 -07005829 partition_dump();
Amol Jadi5edf3552013-07-23 14:15:34 -07005830
5831 /* initialize and start fastboot */
Saranya Chidura41b34532018-10-16 12:29:52 +05305832#if !VERIFIED_BOOT_2
Amol Jadi5edf3552013-07-23 14:15:34 -07005833 fastboot_init(target_get_scratch_address(), target_get_max_flash_size());
Saranya Chidura41b34532018-10-16 12:29:52 +05305834#else
5835 /* Add salt buffer offset at start of image address to copy VB salt */
5836 fastboot_init(ADD_SALT_BUFF_OFFSET(target_get_scratch_address()),
5837 SUB_SALT_BUFF_OFFSET(target_get_max_flash_size()));
5838#endif
lijuang4ece1e72015-08-14 21:02:36 +08005839#if FBCON_DISPLAY_MSG
lijuang9a7d3b92015-11-30 14:41:24 +08005840 display_fastboot_menu();
lijuang4ece1e72015-08-14 21:02:36 +08005841#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08005842}
5843
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07005844uint32_t get_page_size()
5845{
5846 return page_size;
5847}
5848
Amir Samuelov57a6fa22013-06-05 16:36:43 +03005849/*
5850 * Calculated and save hash (SHA256) for non-signed boot image.
5851 *
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07005852 * @param image_addr - Boot image address
5853 * @param image_size - Size of the boot image
Amir Samuelov57a6fa22013-06-05 16:36:43 +03005854 *
5855 * @return int - 0 on success, negative value on failure.
5856 */
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07005857static int aboot_save_boot_hash_mmc(uint32_t image_addr, uint32_t image_size)
Amir Samuelov57a6fa22013-06-05 16:36:43 +03005858{
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07005859 unsigned int digest[8];
5860#if IMAGE_VERIF_ALGO_SHA1
5861 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA1;
5862#else
5863 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA256;
5864#endif
Amir Samuelov57a6fa22013-06-05 16:36:43 +03005865
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07005866 target_crypto_init_params();
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -08005867 hash_find((unsigned char *) image_addr, image_size, (unsigned char *)&digest, auth_algo);
Amir Samuelov57a6fa22013-06-05 16:36:43 +03005868
5869 save_kernel_hash_cmd(digest);
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07005870 dprintf(INFO, "aboot_save_boot_hash_mmc: imagesize_actual size %d bytes.\n", (int) image_size);
Amir Samuelov57a6fa22013-06-05 16:36:43 +03005871
5872 return 0;
5873}
5874
Mayank Grover351a75e2017-05-30 20:06:08 +05305875
Brian Swetland9c4c0752009-01-25 16:23:50 -08005876APP_START(aboot)
5877 .init = aboot_init,
5878APP_END