blob: a9ac9d433a8daf439a15b436759549c4503f2de6 [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;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700106void write_device_info_mmc(device_info *dev);
107void write_device_info_flash(device_info *dev);
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -0700108static int aboot_save_boot_hash_mmc(uint32_t image_addr, uint32_t image_size);
Channagoud Kadabiad259832015-05-29 11:14:17 -0700109static int aboot_frp_unlock(char *pname, void *data, unsigned sz);
Kishor PK38ed93d2017-04-25 14:19:26 +0530110static inline uint64_t validate_partition_size();
Parth Dixit54ac3bb2017-03-07 15:52:48 +0530111bool pwr_key_is_pressed = false;
Hareesh Gauthamabf1e1e2017-04-12 18:09:24 +0530112static bool is_systemd_present=false;
Mayank Grover351a75e2017-05-30 20:06:08 +0530113static void publish_getvar_multislot_vars();
Sridhar Parasurame94e8152014-10-24 14:06:03 -0700114/* fastboot command function pointer */
115typedef void (*fastboot_cmd_fn) (const char *, void *, unsigned);
Monika Singh292b3e92018-03-17 22:40:23 +0530116bool get_perm_attr_status();
Sridhar Parasurame94e8152014-10-24 14:06:03 -0700117
118struct fastboot_cmd_desc {
119 char * name;
120 fastboot_cmd_fn cb;
121};
122
Subbaraman Narayanamurthyeb92bcc2010-07-20 14:32:46 -0700123#define EXPAND(NAME) #NAME
124#define TARGET(NAME) EXPAND(NAME)
Brian Swetland2defe162009-08-18 14:35:59 -0700125
Ajay Singh Parmara7865a82015-04-16 21:27:52 -0700126#define DISPLAY_PANEL_HDMI "hdmi"
127
Ajay Dudanicd01f9b2010-02-23 21:13:04 -0800128#ifdef MEMBASE
129#define EMMC_BOOT_IMG_HEADER_ADDR (0xFF000+(MEMBASE))
130#else
David Ng183a7422009-12-07 14:55:21 -0800131#define EMMC_BOOT_IMG_HEADER_ADDR 0xFF000
Ajay Dudanicd01f9b2010-02-23 21:13:04 -0800132#endif
133
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700134#ifndef MEMSIZE
135#define MEMSIZE 1024*1024
136#endif
137
138#define MAX_TAGS_SIZE 1024
Kishor PKee5c0a32018-03-06 16:49:46 +0530139#define PLL_CODES_OFFSET 4096
Kun Liang2f1601a2013-08-12 16:29:54 +0800140/* make 4096 as default size to ensure EFS,EXT4's erasing */
141#define DEFAULT_ERASE_SIZE 4096
Ujwal Patelc0b0a252015-08-16 14:05:35 -0700142#define MAX_PANEL_BUF_SIZE 196
Vijay Kumar Pendoti0b21f462016-05-02 17:09:18 +0530143#define FOOTER_SIZE 16384
Kun Liang2f1601a2013-08-12 16:29:54 +0800144
Dhaval Patelf83d73b2014-06-23 16:24:37 -0700145#define DISPLAY_DEFAULT_PREFIX "mdss_mdp"
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700146#define BOOT_DEV_MAX_LEN 64
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -0800147
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800148#define IS_ARM64(ptr) (ptr->magic_64 == KERNEL64_HDR_MAGIC) ? true : false
149
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -0700150#define ADD_OF(a, b) (UINT_MAX - b > a) ? (a + b) : UINT_MAX
151
Ameya Thakur10a33452016-06-13 14:24:26 -0700152//Size of the header that is used in case the boot image has
153//a uncompressed kernel + appended dtb
154#define PATCHED_KERNEL_HEADER_SIZE 20
155
156//String used to determine if the boot image has
157//a uncompressed kernel + appended dtb
158#define PATCHED_KERNEL_MAGIC "UNCOMPRESSED_IMG"
159
Sridhar Parasuram7bd4aaf2015-02-12 11:14:38 -0800160#if USE_BOOTDEV_CMDLINE
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700161static const char *emmc_cmdline = " androidboot.bootdevice=";
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700162#else
David Ng183a7422009-12-07 14:55:21 -0800163static const char *emmc_cmdline = " androidboot.emmc=true";
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700164#endif
Mayank Groverb716edf2019-05-08 16:06:55 +0530165static const char *dynamic_bootdev_cmdline =
166 " androidboot.boot_devices=soc/";
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800167static const char *usb_sn_cmdline = " androidboot.serialno=";
Pavel Nedev328ac822013-04-05 15:25:11 +0300168static const char *androidboot_mode = " androidboot.mode=";
Hareesh Gauthamabf1e1e2017-04-12 18:09:24 +0530169
170static const char *systemd_ffbm_mode = " systemd.unit=ffbm.target";
Matthew Qind886f3c2014-01-17 16:52:01 +0800171static const char *alarmboot_cmdline = " androidboot.alarmboot=true";
Pavel Nedev898298c2013-02-27 12:36:09 -0800172static const char *loglevel = " quiet";
Ajay Dudanica3a33c2011-11-18 08:31:40 -0800173static const char *battchg_pause = " androidboot.mode=charger";
Shashank Mittalcd98d472011-08-02 14:29:24 -0700174static const char *auth_kernel = " androidboot.authorized_kernel=true";
Pavel Nedev5614d222013-06-17 18:01:02 +0300175static const char *secondary_gpt_enable = " gpt";
Monika Singh292b3e92018-03-17 22:40:23 +0530176#ifdef MDTP_SUPPORT
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200177static const char *mdtp_activated_flag = " mdtp";
Monika Singh292b3e92018-03-17 22:40:23 +0530178#endif
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800179static const char *baseband_apq = " androidboot.baseband=apq";
180static const char *baseband_msm = " androidboot.baseband=msm";
181static const char *baseband_csfb = " androidboot.baseband=csfb";
182static const char *baseband_svlte2a = " androidboot.baseband=svlte2a";
Ajay Dudani403bc492011-09-30 16:17:21 -0700183static const char *baseband_mdm = " androidboot.baseband=mdm";
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800184static const char *baseband_mdm2 = " androidboot.baseband=mdm2";
Amol Jadi5c61a952012-05-04 17:05:35 -0700185static const char *baseband_sglte = " androidboot.baseband=sglte";
Amol Jadi2a15a272013-01-22 12:03:36 -0800186static const char *baseband_dsda = " androidboot.baseband=dsda";
187static const char *baseband_dsda2 = " androidboot.baseband=dsda2";
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800188static const char *baseband_sglte2 = " androidboot.baseband=sglte2";
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800189static const char *warmboot_cmdline = " qpnp-power-on.warm_boot=1";
Vijay Kumar Pendotib228cfc2016-06-13 20:15:23 +0530190static const char *baseband_apq_nowgr = " androidboot.baseband=baseband_apq_nowgr";
Mayank Grover351a75e2017-05-30 20:06:08 +0530191static const char *androidboot_slot_suffix = " androidboot.slot_suffix=";
192static const char *skip_ramfs = " skip_initramfs";
Mayank Grover466d6562018-05-10 14:52:20 +0530193
194#if HIBERNATION_SUPPORT
195static const char *resume = " resume=/dev/mmcblk0p";
196#endif
197
Sourabh Banerjee6c7153c2018-03-20 01:21:57 +0530198#ifdef INIT_BIN_LE
199static const char *sys_path_cmdline = " rootwait ro init="INIT_BIN_LE;
200#else
Mayank Grover3804be72017-06-22 11:59:23 +0530201static const char *sys_path_cmdline = " rootwait ro init=/init";
Sourabh Banerjee6c7153c2018-03-20 01:21:57 +0530202#endif
Sourabh Banerjee386b1322018-02-27 09:37:28 +0530203
204#if VERITY_LE
205static const char *verity_dev = " root=/dev/dm-0";
206static const char *verity_system_part = " dm=\"system";
207static const char *verity_params = " none ro,0 1 android-verity /dev/mmcblk0p";
208#else
Mayank Grover5384ed82018-05-09 12:09:24 +0530209static const char *sys_path = " root=/dev/mmcblk0p";
lijuang83ef4b22018-08-23 11:01:55 +0800210
211#define MAX_DTBO_IDX_STR 64
212static const char *android_boot_dtbo_idx = " androidboot.dtbo_idx=";
Parth Dixit1375d9e2019-07-08 20:56:13 +0530213
214#define MAX_DTB_IDX_STR MAX_DTBO_IDX_STR
215static const char *android_boot_dtb_idx = " androidboot.dtb_idx=";
Sourabh Banerjee386b1322018-02-27 09:37:28 +0530216#endif
Ajay Dudanid04110c2011-01-17 23:55:07 -0800217
michaellincb058f12020-04-24 15:05:10 +0800218//<20200424-michaellin, Add PCBA stage to system properties
219#include <platform/gpio.h>
220static const char *PCBA_STAGE_0 = " androidboot.pcbastage=EP0";
221static const char *PCBA_STAGE_1 = " androidboot.pcbastage=EP1";
222static const char *PCBA_STAGE_2 = " androidboot.pcbastage=EP2";
223static const char *PCBA_STAGE_3 = " androidboot.pcbastage=FP";
224static const char *PCBA_STAGE_4 = " androidboot.pcbastage=MP";
michaellin52b647a2020-05-05 14:58:47 +0800225static const char *PCBA_STAGE_5 = " androidboot.pcbastage=MP-8903MB_001";
michaellincb058f12020-04-24 15:05:10 +0800226static const char *PCBA_STAGE_F = " androidboot.pcbastage=Reserved";
227//>20200424-michaellin
228
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700229#if VERIFIED_BOOT
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700230static const char *verity_mode = " androidboot.veritymode=";
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700231static const char *verified_state= " androidboot.verifiedbootstate=";
Parth Dixita5715a02015-10-29 12:25:10 +0530232static const char *keymaster_v1= " androidboot.keymaster=1";
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700233//indexed based on enum values, green is 0 by default
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700234
235struct verified_boot_verity_mode vbvm[] =
236{
Channagoud Kadabi86b0c112016-03-16 19:23:16 -0700237#if ENABLE_VB_ATTEST
238 {false, "eio"},
239#else
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700240 {false, "logging"},
Channagoud Kadabi86b0c112016-03-16 19:23:16 -0700241#endif
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700242 {true, "enforcing"},
243};
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700244struct verified_boot_state_name vbsn[] =
245{
246 {GREEN, "green"},
247 {ORANGE, "orange"},
248 {YELLOW,"yellow"},
249 {RED,"red" },
250};
251#endif
Channagoud Kadabi86b0c112016-03-16 19:23:16 -0700252/*As per spec delay wait time before shutdown in Red state*/
253#define DELAY_WAIT 30000
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700254static unsigned page_size = 0;
255static unsigned page_mask = 0;
Gaurav Nebhwani4d2389c2016-03-17 21:10:05 +0530256static unsigned mmc_blocksize = 0;
257static unsigned mmc_blocksize_mask = 0;
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700258static char ffbm_mode_string[FFBM_MODE_BUF_SIZE];
259static bool boot_into_ffbm;
vijay kumar870515d2015-08-31 16:37:24 +0530260static char *target_boot_params = NULL;
Matthew Qind886f3c2014-01-17 16:52:01 +0800261static bool boot_reason_alarm;
Channagoud Kadabi80a182b2015-03-11 17:04:23 -0700262static bool devinfo_present = true;
Channagoud Kadabi736c4962015-08-21 11:56:52 -0700263bool boot_into_fastboot = false;
Parth Dixit4097b622016-03-15 14:42:27 +0530264static uint32_t dt_size = 0;
Monika Singh292b3e92018-03-17 22:40:23 +0530265static char *vbcmdline;
266static bootinfo info = {0};
Mayank Grovere1ab96c2018-09-04 20:31:31 +0530267static void *recovery_dtbo_buf = NULL;
268static uint32_t recovery_dtbo_size = 0;
269
Shashank Mittalcd98d472011-08-02 14:29:24 -0700270/* Assuming unauthorized kernel image by default */
271static int auth_kernel_img = 0;
Monika Singh292b3e92018-03-17 22:40:23 +0530272static device_info device = {DEVICE_MAGIC,0,0,0,0,{0},{0},{0},1,{0},0,{0}};
273
jhchen7a504d12020-04-24 15:45:57 +0800274/*[Arima_8910][jhchen] add fuse check property 20181031 begin*/
275#ifdef ENABLE_FUSE_CHECK
276static const char *fuse_blown = " androidboot.oem.color=red";
277static const char *fuse_not_blown = " androidboot.oem.color=brown";
278#endif
279/*[Arima_8910][jhchen] 20181031 end*/
280
281/*[Arima_8910][jhchen] add fuse check property 20181031 begin*/
282#ifdef ENABLE_FUSE_CHECK
283 int is_fused = 0;
284#endif
285/*[Arima_8910][jhchen] 20181031 end*/
286
vijay kumarc65876c2015-04-24 13:29:16 +0530287static bool is_allow_unlock = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -0700288
vijay kumarca2e6812015-07-08 20:28:25 +0530289static char frp_ptns[2][8] = {"config","frp"};
290
lijuang511a2b52015-08-14 20:50:51 +0800291static const char *critical_flash_allowed_ptn[] = {
292 "aboot",
293 "rpm",
294 "tz",
295 "sbl",
296 "sdi",
297 "sbl1",
298 "xbl",
299 "hyp",
300 "pmic",
301 "bootloader",
302 "devinfo",
303 "partition"};
304
Dima Zavin42168f22009-01-30 11:52:22 -0800305struct atag_ptbl_entry
306{
307 char name[16];
308 unsigned offset;
309 unsigned size;
310 unsigned flags;
311};
312
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700313/*
314 * Partition info, required to be published
315 * for fastboot
316 */
317struct getvar_partition_info {
Mayank Grover7b8a8f82017-10-27 13:07:59 +0530318 char part_name[MAX_GPT_NAME_SIZE]; /* Partition name */
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700319 char getvar_size[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for size */
320 char getvar_type[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for type */
321 char size_response[MAX_RSP_SIZE]; /* fastboot response for size */
322 char type_response[MAX_RSP_SIZE]; /* fastboot response for type */
323};
324
325/*
Mayank Grover7b8a8f82017-10-27 13:07:59 +0530326 * Update the part_type_known for known paritions types.
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700327 */
Mayank Groverd38fe012018-03-13 15:33:16 +0530328#define RAW_STR "raw"
Mayank Grover52cd10a2018-03-15 12:57:54 +0530329#define EXT_STR "ext4"
330#define F2FS_STR "f2fs"
331
332#define FS_SUPERBLOCK_OFFSET 0x400
333#define EXT_MAGIC 0xEF53
334#define EXT_MAGIC_OFFSET_SB 0x38
335#define F2FS_MAGIC 0xF2F52010 // F2FS Magic Number
336#define F2FS_MAGIC_OFFSET_SB 0x0
337
338typedef enum fs_signature_type {
339 EXT_FS_SIGNATURE = 1,
340 EXT_F2FS_SIGNATURE = 2,
341 NO_FS = -1
342} fs_signature_type;
343
Mayank Grover7b8a8f82017-10-27 13:07:59 +0530344struct getvar_partition_info part_info[NUM_PARTITIONS];
345struct getvar_partition_info part_type_known[] =
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700346{
Mayank Grover49920212018-02-09 18:15:55 +0530347 { "system" , "partition-size:", "partition-type:", "", "ext4" },
348 { "userdata" , "partition-size:", "partition-type:", "", "ext4" },
349 { "cache" , "partition-size:", "partition-type:", "", "ext4" },
350 { "recoveryfs" , "partition-size:", "partition-type:", "", "ext4" },
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700351};
352
353char max_download_size[MAX_RSP_SIZE];
Ameya Thakur11cf1a62013-08-05 12:44:48 -0700354char charger_screen_enabled[MAX_RSP_SIZE];
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800355char sn_buf[13];
Dhaval Patel223ec952013-07-18 14:49:44 -0700356char display_panel_buf[MAX_PANEL_BUF_SIZE];
Unnati Gandhi62c8ab82014-01-24 11:01:01 +0530357char panel_display_mode[MAX_RSP_SIZE];
Mayank Grovera64dfbe2018-05-17 14:06:34 +0530358char soc_version_str[MAX_RSP_SIZE];
Mayank Grover7b8a8f82017-10-27 13:07:59 +0530359char block_size_string[MAX_RSP_SIZE];
Mukesh Ojhabb6e7f32018-03-02 15:04:06 +0530360#if PRODUCT_IOT
Mayank Grover7b8a8f82017-10-27 13:07:59 +0530361
362/* For IOT we are using custom version */
363#define PRODUCT_IOT_VERSION "IOT001"
364char bootloader_version_string[MAX_RSP_SIZE];
365#endif
lijuang102dfa92015-10-09 18:31:03 +0800366
367#if CHECK_BAT_VOLTAGE
lijuang65c5a822015-08-29 16:35:36 +0800368char battery_voltage[MAX_RSP_SIZE];
lijuang102dfa92015-10-09 18:31:03 +0800369char battery_soc_ok [MAX_RSP_SIZE];
370#endif
371
lijuangf16461c2015-08-03 17:09:34 +0800372char get_variant[MAX_RSP_SIZE];
Greg Griscod6250552011-06-29 14:40:23 -0700373
Greg Griscod2471ef2011-07-14 13:00:42 -0700374extern int emmc_recovery_init(void);
375
Kinson Chik0b1c8162011-08-31 16:31:57 -0700376#if NO_KEYPAD_DRIVER
377extern int fastboot_trigger(void);
378#endif
Greg Griscod2471ef2011-07-14 13:00:42 -0700379
Mayank Grovere1ab96c2018-09-04 20:31:31 +0530380static void update_ker_tags_rdisk_addr(boot_img_hdr *hdr, bool is_arm64)
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700381{
382 /* overwrite the destination of specified for the project */
Channagoud Kadabi7042fa32013-04-26 16:44:14 -0700383#ifdef ABOOT_IGNORE_BOOT_HEADER_ADDRS
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800384 if (is_arm64)
385 hdr->kernel_addr = ABOOT_FORCE_KERNEL64_ADDR;
386 else
387 hdr->kernel_addr = ABOOT_FORCE_KERNEL_ADDR;
Channagoud Kadabi7042fa32013-04-26 16:44:14 -0700388 hdr->ramdisk_addr = ABOOT_FORCE_RAMDISK_ADDR;
389 hdr->tags_addr = ABOOT_FORCE_TAGS_ADDR;
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700390#endif
391}
392
Dima Zavin42168f22009-01-30 11:52:22 -0800393static void ptentry_to_tag(unsigned **ptr, struct ptentry *ptn)
394{
395 struct atag_ptbl_entry atag_ptn;
396
397 memcpy(atag_ptn.name, ptn->name, 16);
398 atag_ptn.name[15] = '\0';
399 atag_ptn.offset = ptn->start;
400 atag_ptn.size = ptn->length;
401 atag_ptn.flags = ptn->flags;
402 memcpy(*ptr, &atag_ptn, sizeof(struct atag_ptbl_entry));
403 *ptr += sizeof(struct atag_ptbl_entry) / sizeof(unsigned);
404}
Mayank Grover9df84c02018-08-30 15:46:35 +0530405#ifdef VERIFIED_BOOT_2
406void load_vbmeta_image(void **vbmeta_image_buf, uint32_t *vbmeta_image_sz)
407{
408 int index = 0;
409 char *vbm_img_buf = NULL;
410 unsigned long long ptn = 0;
411 unsigned long long ptn_size = 0;
412
413 /* Immediately return if dtbo is not supported */
414 index = partition_get_index("vbmeta");
415 ptn = partition_get_offset(index);
416 if(!ptn)
417 {
418 dprintf(CRITICAL, "ERROR: vbmeta partition not found.\n");
419 return;
420 }
421
422 ptn_size = partition_get_size(index);
423 if (ptn_size > MAX_SUPPORTED_VBMETA_IMG_BUF)
424 {
425 dprintf(CRITICAL, "ERROR: vbmeta parition size is greater than supported.\n");
426 return;
427 }
428
429 vbm_img_buf = (char *)memalign(CACHE_LINE, ROUNDUP((uint32_t)ptn_size, CACHE_LINE));
430 if (!vbm_img_buf)
431 {
432 dprintf(CRITICAL, "ERROR: vbmeta unable to locate buffer\n");
433 return;
434 }
435
436 mmc_set_lun(partition_get_lun(index));
437 if (mmc_read(ptn, (uint32_t *)vbm_img_buf, (uint32_t)ptn_size))
438 {
439 dprintf(CRITICAL, "ERROR: vbmeta read failure\n");
440 free(vbm_img_buf);
441 return;
442 }
443
444 *vbmeta_image_buf = vbm_img_buf;
445 *vbmeta_image_sz = (uint32_t)ptn_size;
446 return;
447}
448#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -0800449
lijuang102dfa92015-10-09 18:31:03 +0800450#if CHECK_BAT_VOLTAGE
451void update_battery_status(void)
452{
453 snprintf(battery_voltage,MAX_RSP_SIZE, "%d",target_get_battery_voltage());
454 snprintf(battery_soc_ok ,MAX_RSP_SIZE, "%s",target_battery_soc_ok()? "yes":"no");
455}
456#endif
457
michaellincb058f12020-04-24 15:05:10 +0800458//<20200424-michaellin, Add PCBA stage to system properties
459uint32_t GetPcbaVariant(void)
460{
461 uint8_t GPIO_97=10;
462 uint8_t GPIO_98=10;
463 uint8_t GPIO_99=10;
464
465 uint8_t pcba_stage = 0;
466
467 // Config GPIO
468 gpio_tlmm_config(97, 0, GPIO_INPUT, GPIO_NO_PULL, GPIO_2MA, GPIO_ENABLE);
469 gpio_tlmm_config(98, 0, GPIO_INPUT, GPIO_NO_PULL, GPIO_2MA, GPIO_ENABLE);
470 gpio_tlmm_config(99, 0, GPIO_INPUT, GPIO_NO_PULL, GPIO_2MA, GPIO_ENABLE);
471
472 /* Wait for the gpio config to take effect - debounce time */
473 thread_sleep(10);
474
475 /* Get status of GPIO */
476 GPIO_97 = gpio_status(97);
477 GPIO_98 = gpio_status(98);
478 GPIO_99 = gpio_status(99);
479
480 pcba_stage = (GPIO_99 << 2) + (GPIO_98 << 1) + GPIO_97;
481 dprintf(ALWAYS, "pcba_stage status: %u\n", pcba_stage);
michaellin52b647a2020-05-05 14:58:47 +0800482 // GPIO_99 GPIO_98 GPIO_97
michaellincb058f12020-04-24 15:05:10 +0800483 // 0 0 0 EP0
484 // 0 0 1 EP1
485 // 0 1 0 EP2
michaellin52b647a2020-05-05 14:58:47 +0800486 // 0 1 1 FP(8901MB-007)/PP
487 // 1 0 0 MP(8901MB-008)
488 // 1 0 1 MP-8903MB_001
michaellincb058f12020-04-24 15:05:10 +0800489 return pcba_stage;
490}
491//>20200424-michaellin
492
Neeti Desaie245d492012-06-01 12:52:13 -0700493unsigned char *update_cmdline(const char * cmdline)
Brian Swetland9c4c0752009-01-25 16:23:50 -0800494{
David Ng183a7422009-12-07 14:55:21 -0800495 int cmdline_len = 0;
496 int have_cmdline = 0;
Amol Jadi168b7712012-03-06 16:15:00 -0800497 unsigned char *cmdline_final = NULL;
Neeti Desaie245d492012-06-01 12:52:13 -0700498 int pause_at_bootup = 0;
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800499 bool warm_boot = false;
Pavel Nedev5614d222013-06-17 18:01:02 +0300500 bool gpt_exists = partition_gpt_exists();
Joonwoo Park61112782013-10-02 19:50:39 -0700501 int have_target_boot_params = 0;
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700502 char *boot_dev_buf = NULL;
Monika Singh292b3e92018-03-17 22:40:23 +0530503#ifdef MDTP_SUPPORT
Mayank Grover351a75e2017-05-30 20:06:08 +0530504 bool is_mdtp_activated = 0;
Monika Singh292b3e92018-03-17 22:40:23 +0530505#endif
Mayank Grover351a75e2017-05-30 20:06:08 +0530506 int current_active_slot = INVALID;
Mayank Grover3804be72017-06-22 11:59:23 +0530507 int system_ptn_index = -1;
508 unsigned int lun = 0;
509 char lun_char_base = 'a';
Sourabh Banerjee386b1322018-02-27 09:37:28 +0530510#if VERITY_LE
511 int syspath_buflen = strlen(verity_dev)
512 + strlen(verity_system_part) + (sizeof(char) * 2) + 2
513 + strlen(verity_params) + sizeof(int) + 2;
514#else
lijuang83ef4b22018-08-23 11:01:55 +0800515 int syspath_buflen = strlen(sys_path) + sizeof(int) + 2; /*allocate buflen for largest possible string*/
516 char dtbo_idx_str[MAX_DTBO_IDX_STR] = "\0";
517 int dtbo_idx = INVALID_PTN;
Parth Dixit1375d9e2019-07-08 20:56:13 +0530518 char dtb_idx_str[MAX_DTB_IDX_STR] = "\0";
519 int dtb_idx = INVALID_PTN;
Sourabh Banerjee386b1322018-02-27 09:37:28 +0530520#endif
Mayank Grover3804be72017-06-22 11:59:23 +0530521 char syspath_buf[syspath_buflen];
Mayank Grover466d6562018-05-10 14:52:20 +0530522#if HIBERNATION_SUPPORT
523 int resume_buflen = strlen(resume) + sizeof(int) + 2;
524 char resume_buf[resume_buflen];
525 int swap_ptn_index = INVALID_PTN;
526#endif
527
Mayank Grover889be1b2017-09-12 20:12:23 +0530528#if VERIFIED_BOOT
529 uint32_t boot_state = RED;
530#endif
Hareesh Gauthamabf1e1e2017-04-12 18:09:24 +0530531
532#if USE_LE_SYSTEMD
533 is_systemd_present=true;
534#endif
535
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700536#if VERIFIED_BOOT
Mayank Grover912eaa62017-10-26 12:08:53 +0530537 if (VB_M <= target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +0530538 {
539 boot_state = boot_verify_get_state();
540 }
Parth Dixitddbc7352015-10-18 03:13:31 +0530541#endif
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700542
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200543#ifdef MDTP_SUPPORT
544 mdtp_activated(&is_mdtp_activated);
545#endif /* MDTP_SUPPORT */
Dima Zavin42168f22009-01-30 11:52:22 -0800546
Brian Swetland9c4c0752009-01-25 16:23:50 -0800547 if (cmdline && cmdline[0]) {
David Ng183a7422009-12-07 14:55:21 -0800548 cmdline_len = strlen(cmdline);
549 have_cmdline = 1;
550 }
551 if (target_is_emmc_boot()) {
552 cmdline_len += strlen(emmc_cmdline);
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700553 boot_dev_buf = (char *) malloc(sizeof(char) * BOOT_DEV_MAX_LEN);
Mayank Groverb716edf2019-05-08 16:06:55 +0530554 if (!boot_dev_buf) {
555 dprintf(CRITICAL, "ERROR: Failed to allocate boot_dev_buf\n");
556 } else {
557 platform_boot_dev_cmdline(boot_dev_buf);
558#if USE_BOOTDEV_CMDLINE
559 cmdline_len += strlen(boot_dev_buf);
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700560#endif
Mayank Groverb716edf2019-05-08 16:06:55 +0530561 if (target_dynamic_partition_supported()) {
562 cmdline_len += strlen(dynamic_bootdev_cmdline);
563 cmdline_len += strlen(boot_dev_buf);
564 }
565 }
David Ng183a7422009-12-07 14:55:21 -0800566 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800567
568 cmdline_len += strlen(usb_sn_cmdline);
569 cmdline_len += strlen(sn_buf);
570
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700571#if VERIFIED_BOOT
Mayank Grover912eaa62017-10-26 12:08:53 +0530572 if (VB_M <= target_get_vb_version())
Sridhar Parasuram7d8bb9a2015-09-23 18:21:19 -0700573 {
Mayank Grover889be1b2017-09-12 20:12:23 +0530574 cmdline_len += strlen(verified_state) + strlen(vbsn[boot_state].name);
575 if ((device.verity_mode != 0 ) && (device.verity_mode != 1))
576 {
577 dprintf(CRITICAL, "Devinfo paritition possibly corrupted!!!. Please erase devinfo partition to continue booting\n");
578 ASSERT(0);
579 }
580 cmdline_len += strlen(verity_mode) + strlen(vbvm[device.verity_mode].name);
581 cmdline_len += strlen(keymaster_v1);
Sridhar Parasuram7d8bb9a2015-09-23 18:21:19 -0700582 }
Parth Dixitddbc7352015-10-18 03:13:31 +0530583#endif
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700584
Monika Singh292b3e92018-03-17 22:40:23 +0530585
586 if (vbcmdline != NULL) {
587 dprintf(DEBUG, "UpdateCmdLine vbcmdline present len %d\n",
588 strlen(vbcmdline));
589 cmdline_len += strlen(vbcmdline);
590 }
591
Pavel Nedev5614d222013-06-17 18:01:02 +0300592 if (boot_into_recovery && gpt_exists)
593 cmdline_len += strlen(secondary_gpt_enable);
594
Monika Singh292b3e92018-03-17 22:40:23 +0530595#ifdef MDTP_SUPPORT
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200596 if(is_mdtp_activated)
597 cmdline_len += strlen(mdtp_activated_flag);
Monika Singh292b3e92018-03-17 22:40:23 +0530598#endif
Pavel Nedev328ac822013-04-05 15:25:11 +0300599 if (boot_into_ffbm) {
600 cmdline_len += strlen(androidboot_mode);
Hareesh Gauthamabf1e1e2017-04-12 18:09:24 +0530601
602 if(is_systemd_present)
603 cmdline_len += strlen(systemd_ffbm_mode);
604
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700605 cmdline_len += strlen(ffbm_mode_string);
Pavel Nedev898298c2013-02-27 12:36:09 -0800606 /* reduce kernel console messages to speed-up boot */
607 cmdline_len += strlen(loglevel);
Matthew Qind886f3c2014-01-17 16:52:01 +0800608 } else if (boot_reason_alarm) {
609 cmdline_len += strlen(alarmboot_cmdline);
Zhenhua Huang431dafa2015-06-30 16:13:37 +0800610 } else if ((target_build_variant_user() || device.charger_screen_enabled)
lijuang266b17d2018-08-17 18:53:42 +0800611 && target_pause_for_battery_charge() && !boot_into_recovery) {
David Ngf773dde2010-07-26 19:55:08 -0700612 pause_at_bootup = 1;
613 cmdline_len += strlen(battchg_pause);
614 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800615
Shashank Mittalcd98d472011-08-02 14:29:24 -0700616 if(target_use_signed_kernel() && auth_kernel_img) {
617 cmdline_len += strlen(auth_kernel);
618 }
619
Joonwoo Park61112782013-10-02 19:50:39 -0700620 if (get_target_boot_params(cmdline, boot_into_recovery ? "recoveryfs" :
621 "system",
vijay kumar870515d2015-08-31 16:37:24 +0530622 &target_boot_params) == 0) {
Joonwoo Park61112782013-10-02 19:50:39 -0700623 have_target_boot_params = 1;
624 cmdline_len += strlen(target_boot_params);
625 }
626
Ajay Dudanid04110c2011-01-17 23:55:07 -0800627 /* Determine correct androidboot.baseband to use */
628 switch(target_baseband())
629 {
630 case BASEBAND_APQ:
631 cmdline_len += strlen(baseband_apq);
632 break;
633
634 case BASEBAND_MSM:
635 cmdline_len += strlen(baseband_msm);
636 break;
637
638 case BASEBAND_CSFB:
639 cmdline_len += strlen(baseband_csfb);
640 break;
641
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800642 case BASEBAND_SVLTE2A:
643 cmdline_len += strlen(baseband_svlte2a);
Ajay Dudanid04110c2011-01-17 23:55:07 -0800644 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700645
646 case BASEBAND_MDM:
647 cmdline_len += strlen(baseband_mdm);
648 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700649
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800650 case BASEBAND_MDM2:
651 cmdline_len += strlen(baseband_mdm2);
652 break;
653
Amol Jadi5c61a952012-05-04 17:05:35 -0700654 case BASEBAND_SGLTE:
655 cmdline_len += strlen(baseband_sglte);
656 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530657
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800658 case BASEBAND_SGLTE2:
659 cmdline_len += strlen(baseband_sglte2);
660 break;
661
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530662 case BASEBAND_DSDA:
663 cmdline_len += strlen(baseband_dsda);
664 break;
Amol Jadi2a15a272013-01-22 12:03:36 -0800665
666 case BASEBAND_DSDA2:
667 cmdline_len += strlen(baseband_dsda2);
668 break;
Vijay Kumar Pendotib228cfc2016-06-13 20:15:23 +0530669 case BASEBAND_APQ_NOWGR:
670 cmdline_len += strlen(baseband_apq_nowgr);
671 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800672 }
673
michaellincb058f12020-04-24 15:05:10 +0800674 //<20200424-michaellin, Add PCBA stage to system properties
675 switch(GetPcbaVariant())
676 {
677 case 0: // EP0
678 cmdline_len += strlen(PCBA_STAGE_0);
679 break;
680
681 case 1: // EP1
682 cmdline_len += strlen(PCBA_STAGE_1);
683 break;
684
685 case 2: // EP2
686 cmdline_len += strlen(PCBA_STAGE_2);
687 break;
688
michaellin52b647a2020-05-05 14:58:47 +0800689 case 3: // FP(8901MB-007)/PP
michaellincb058f12020-04-24 15:05:10 +0800690 cmdline_len += strlen(PCBA_STAGE_3);
691 break;
692
michaellin52b647a2020-05-05 14:58:47 +0800693 case 4: // MP(8901MB-008)
michaellincb058f12020-04-24 15:05:10 +0800694 cmdline_len += strlen(PCBA_STAGE_4);
695 break;
696
michaellin52b647a2020-05-05 14:58:47 +0800697 case 5: // MP-8903MB_001
698 cmdline_len += strlen(PCBA_STAGE_5);
699 break;
700
michaellincb058f12020-04-24 15:05:10 +0800701 default:// Reserved
702 cmdline_len += strlen(PCBA_STAGE_F);
703 break;
704 }
705 //>20200424-michaellin
706
Vladislav Levenetz3cd45242017-04-05 08:58:07 +0300707#if ENABLE_DISPLAY
Lijuan Gao4a5b8322014-07-24 10:38:42 +0800708 if (cmdline) {
709 if ((strstr(cmdline, DISPLAY_DEFAULT_PREFIX) == NULL) &&
Padmanabhan Komandurubccbcdc2015-06-30 16:19:24 +0530710 target_display_panel_node(display_panel_buf,
711 MAX_PANEL_BUF_SIZE) &&
Lijuan Gao4a5b8322014-07-24 10:38:42 +0800712 strlen(display_panel_buf)) {
713 cmdline_len += strlen(display_panel_buf);
714 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700715 }
Vladislav Levenetz3cd45242017-04-05 08:58:07 +0300716#endif
Dhaval Patel223ec952013-07-18 14:49:44 -0700717
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800718 if (target_warm_boot()) {
719 warm_boot = true;
720 cmdline_len += strlen(warmboot_cmdline);
721 }
722
Mayank Grover5384ed82018-05-09 12:09:24 +0530723 if (target_uses_system_as_root() ||
724 partition_multislot_is_supported())
Mayank Grover351a75e2017-05-30 20:06:08 +0530725 {
726 current_active_slot = partition_find_active_slot();
727 cmdline_len += (strlen(androidboot_slot_suffix)+
728 strlen(SUFFIX_SLOT(current_active_slot)));
729
Mayank Grover3804be72017-06-22 11:59:23 +0530730 system_ptn_index = partition_get_index("system");
731 if (platform_boot_dev_isemmc())
732 {
Sourabh Banerjee386b1322018-02-27 09:37:28 +0530733#if VERITY_LE
734 /*
735 Condition 4: Verity and A/B both enabled
736 Eventual command line looks like:
737 ... androidboot.slot_suffix=<slot_suffix> ... rootfstype=ext4 ...
738 ... root=/dev/dm-0 dm="system_<slot_suffix> none ro,0 1 android-verity /dev/mmcblk0p<NN>"
739 */
740 snprintf(syspath_buf, syspath_buflen, " %s %s%s %s%d\"",
741 verity_dev,
742 verity_system_part, suffix_slot[current_active_slot],
743 verity_params, system_ptn_index + 1);
744#else
745 /*
746 Condition 5: A/B enabled, but verity disabled
747 Eventual command line looks like:
748 ... androidboot.slot_suffix=<slot_suffix> ... rootfstype=ext4 ...
749 ... root=/dev/mmcblk0p<NN> ...
750 */
751 snprintf(syspath_buf, syspath_buflen, " %s%d",
752 sys_path, system_ptn_index + 1);
753#endif
Mayank Grover3804be72017-06-22 11:59:23 +0530754 }
755 else
756 {
757 lun = partition_get_lun(system_ptn_index);
758 snprintf(syspath_buf, syspath_buflen, " root=/dev/sd%c%d",
759 lun_char_base + lun,
760 partition_get_index_in_lun("system", lun));
761 }
Mayank Grover351a75e2017-05-30 20:06:08 +0530762
Monika Singh292b3e92018-03-17 22:40:23 +0530763#ifndef VERIFIED_BOOT_2
Mayank Grover3804be72017-06-22 11:59:23 +0530764 cmdline_len += strlen(syspath_buf);
Monika Singh292b3e92018-03-17 22:40:23 +0530765#endif
Mayank Grover5384ed82018-05-09 12:09:24 +0530766 }
767
768 if (target_uses_system_as_root() ||
769 partition_multislot_is_supported())
770 {
771 cmdline_len += strlen(sys_path_cmdline);
Mayank Groverb716edf2019-05-08 16:06:55 +0530772
773 /* For dynamic partition, support skip skip_initramfs */
774 if (!target_dynamic_partition_supported() &&
775 !boot_into_recovery)
Mayank Grover351a75e2017-05-30 20:06:08 +0530776 cmdline_len += strlen(skip_ramfs);
777 }
778
Mayank Grover466d6562018-05-10 14:52:20 +0530779#if HIBERNATION_SUPPORT
780 if (platform_boot_dev_isemmc())
781 {
782 swap_ptn_index = partition_get_index("swap");
783 if (swap_ptn_index != INVALID_PTN)
784 {
785 snprintf(resume_buf, resume_buflen,
786 " %s%d", resume,
787 (swap_ptn_index + 1));
788 cmdline_len += strlen(resume_buf);
789 }
790 else
791 {
792 dprintf(INFO, "WARNING: swap partition not found\n");
793 }
794 }
795#endif
796
Channagoud Kadabi85c7ec32016-01-28 23:09:21 -0800797#if TARGET_CMDLINE_SUPPORT
798 char *target_cmdline_buf = malloc(TARGET_MAX_CMDLNBUF);
799 int target_cmd_line_len;
800 ASSERT(target_cmdline_buf);
801 target_cmd_line_len = target_update_cmdline(target_cmdline_buf);
802 cmdline_len += target_cmd_line_len;
803#endif
804
lijuang83ef4b22018-08-23 11:01:55 +0800805#if !VERITY_LE
806 dtbo_idx = get_dtbo_idx ();
807 if (dtbo_idx != INVALID_PTN) {
808 snprintf(dtbo_idx_str, sizeof(dtbo_idx_str), "%s%d",
809 android_boot_dtbo_idx, dtbo_idx);
810 cmdline_len += strlen (dtbo_idx_str);
811 }
Parth Dixit1375d9e2019-07-08 20:56:13 +0530812
813 dtb_idx = get_dtb_idx ();
814 if (dtb_idx != INVALID_PTN) {
815 snprintf(dtb_idx_str, sizeof(dtb_idx_str), "%s%d",
816 android_boot_dtb_idx, dtb_idx);
817 cmdline_len += strlen (dtb_idx_str);
818 }
lijuang83ef4b22018-08-23 11:01:55 +0800819#endif
820
jhchen7a504d12020-04-24 15:45:57 +0800821/*[Arima_8910][jhchen] add fuse check property 20181031 begin*/
822#ifdef ENABLE_FUSE_CHECK
823 {
824 int hw_key_status = readl(0xA01D0);
825 is_fused = (hw_key_status & 0x2) >> 1;
826 dprintf(CRITICAL, "hw_key_status = 0x%x, is_fused=%d\n", hw_key_status, is_fused);
827 if (is_fused) {
828 cmdline_len += strlen(fuse_blown);
829 } else {
830 cmdline_len += strlen(fuse_not_blown);
831 }
832 }
833#endif
834/*[Arima_8910][jhchen] 20181031 end*/
835
David Ng183a7422009-12-07 14:55:21 -0800836 if (cmdline_len > 0) {
837 const char *src;
Maria Yu52254c02014-07-04 16:14:54 +0800838 unsigned char *dst;
839
840 cmdline_final = (unsigned char*) malloc((cmdline_len + 4) & (~3));
841 ASSERT(cmdline_final != NULL);
vijay kumar287bb542015-09-29 13:01:52 +0530842 memset((void *)cmdline_final, 0, sizeof(*cmdline_final));
Maria Yu52254c02014-07-04 16:14:54 +0800843 dst = cmdline_final;
Neeti Desaie245d492012-06-01 12:52:13 -0700844
Amol Jadi168b7712012-03-06 16:15:00 -0800845 /* Save start ptr for debug print */
David Ng183a7422009-12-07 14:55:21 -0800846 if (have_cmdline) {
847 src = cmdline;
848 while ((*dst++ = *src++));
849 }
850 if (target_is_emmc_boot()) {
851 src = emmc_cmdline;
852 if (have_cmdline) --dst;
David Ngf773dde2010-07-26 19:55:08 -0700853 have_cmdline = 1;
854 while ((*dst++ = *src++));
Sridhar Parasuram7bd4aaf2015-02-12 11:14:38 -0800855#if USE_BOOTDEV_CMDLINE
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700856 src = boot_dev_buf;
Mayank Groverb716edf2019-05-08 16:06:55 +0530857 if (have_cmdline &&
858 boot_dev_buf) {
859 --dst;
860 while ((*dst++ = *src++));
861 }
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700862#endif
Mayank Groverb716edf2019-05-08 16:06:55 +0530863 /* Dynamic partition append boot_devices */
864 if (target_dynamic_partition_supported() &&
865 boot_dev_buf) {
866 src = dynamic_bootdev_cmdline;
867 if (have_cmdline) --dst;
868 while ((*dst++ = *src++));
869 src = boot_dev_buf;
870 if (have_cmdline) --dst;
871 while ((*dst++ = *src++));
872 }
David Ngf773dde2010-07-26 19:55:08 -0700873 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800874
michaellincb058f12020-04-24 15:05:10 +0800875 //<20200424-michaellin, Add PCBA stage to system properties
876 switch(GetPcbaVariant())
877 {
878 case 0:
879 src = PCBA_STAGE_0;
880 if (have_cmdline) --dst;
881 while ((*dst++ = *src++));
882 break;
883 case 1:
884 src = PCBA_STAGE_1;
885 if (have_cmdline) --dst;
886 while ((*dst++ = *src++));
887 break;
888 case 2:
889 src = PCBA_STAGE_2;
890 if (have_cmdline) --dst;
891 while ((*dst++ = *src++));
892 break;
893 case 3:
894 src = PCBA_STAGE_3;
895 if (have_cmdline) --dst;
896 while ((*dst++ = *src++));
897 break;
898 case 4:
899 src = PCBA_STAGE_4;
900 if (have_cmdline) --dst;
901 while ((*dst++ = *src++));
902 break;
michaellin52b647a2020-05-05 14:58:47 +0800903 case 5:
904 src = PCBA_STAGE_5;
905 if (have_cmdline) --dst;
906 while ((*dst++ = *src++));
907 break;
michaellincb058f12020-04-24 15:05:10 +0800908 default:
909 src = PCBA_STAGE_F;
910 if (have_cmdline) --dst;
911 while ((*dst++ = *src++));
912 break;
913 }
914 //>20200424-michaellin
915
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700916#if VERIFIED_BOOT
Mayank Grover912eaa62017-10-26 12:08:53 +0530917 if (VB_M <= target_get_vb_version())
Sridhar Parasuram7d8bb9a2015-09-23 18:21:19 -0700918 {
Mayank Grover889be1b2017-09-12 20:12:23 +0530919 src = verified_state;
920 if(have_cmdline) --dst;
921 have_cmdline = 1;
922 while ((*dst++ = *src++));
923 src = vbsn[boot_state].name;
924 if(have_cmdline) --dst;
925 while ((*dst++ = *src++));
926
927 if ((device.verity_mode != 0 ) && (device.verity_mode != 1))
928 {
929 dprintf(CRITICAL, "Devinfo paritition possibly corrupted!!!. Please erase devinfo partition to continue booting\n");
930 ASSERT(0);
931 }
932 src = verity_mode;
933 if(have_cmdline) --dst;
934 while ((*dst++ = *src++));
935 src = vbvm[device.verity_mode].name;
936 if(have_cmdline) -- dst;
937 while ((*dst++ = *src++));
938 src = keymaster_v1;
939 if(have_cmdline) --dst;
940 while ((*dst++ = *src++));
Sridhar Parasuram7d8bb9a2015-09-23 18:21:19 -0700941 }
Parth Dixitddbc7352015-10-18 03:13:31 +0530942#endif
Monika Singh292b3e92018-03-17 22:40:23 +0530943
944 if (vbcmdline != NULL) {
945 src = vbcmdline;
946 if (have_cmdline) --dst;
947 while ((*dst++ = *src++));
948 }
949
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800950 src = usb_sn_cmdline;
951 if (have_cmdline) --dst;
952 have_cmdline = 1;
953 while ((*dst++ = *src++));
954 src = sn_buf;
955 if (have_cmdline) --dst;
956 have_cmdline = 1;
957 while ((*dst++ = *src++));
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800958 if (warm_boot) {
959 if (have_cmdline) --dst;
960 src = warmboot_cmdline;
961 while ((*dst++ = *src++));
962 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800963
Pavel Nedev5614d222013-06-17 18:01:02 +0300964 if (boot_into_recovery && gpt_exists) {
965 src = secondary_gpt_enable;
966 if (have_cmdline) --dst;
967 while ((*dst++ = *src++));
968 }
Monika Singh292b3e92018-03-17 22:40:23 +0530969#ifdef MDTP_SUPPORT
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200970 if (is_mdtp_activated) {
971 src = mdtp_activated_flag;
972 if (have_cmdline) --dst;
973 while ((*dst++ = *src++));
974 }
Monika Singh292b3e92018-03-17 22:40:23 +0530975#endif
Pavel Nedev328ac822013-04-05 15:25:11 +0300976 if (boot_into_ffbm) {
977 src = androidboot_mode;
978 if (have_cmdline) --dst;
979 while ((*dst++ = *src++));
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700980 src = ffbm_mode_string;
Pavel Nedev328ac822013-04-05 15:25:11 +0300981 if (have_cmdline) --dst;
982 while ((*dst++ = *src++));
Hareesh Gauthamabf1e1e2017-04-12 18:09:24 +0530983
984 if(is_systemd_present) {
985 src = systemd_ffbm_mode;
986 if (have_cmdline) --dst;
987 while ((*dst++ = *src++));
988 }
989
Pavel Nedev898298c2013-02-27 12:36:09 -0800990 src = loglevel;
991 if (have_cmdline) --dst;
992 while ((*dst++ = *src++));
Matthew Qind886f3c2014-01-17 16:52:01 +0800993 } else if (boot_reason_alarm) {
994 src = alarmboot_cmdline;
995 if (have_cmdline) --dst;
996 while ((*dst++ = *src++));
Pavel Nedev328ac822013-04-05 15:25:11 +0300997 } else if (pause_at_bootup) {
David Ngf773dde2010-07-26 19:55:08 -0700998 src = battchg_pause;
999 if (have_cmdline) --dst;
David Ng183a7422009-12-07 14:55:21 -08001000 while ((*dst++ = *src++));
1001 }
Ajay Dudanid04110c2011-01-17 23:55:07 -08001002
Shashank Mittalcd98d472011-08-02 14:29:24 -07001003 if(target_use_signed_kernel() && auth_kernel_img) {
1004 src = auth_kernel;
1005 if (have_cmdline) --dst;
1006 while ((*dst++ = *src++));
1007 }
1008
Ajay Dudanid04110c2011-01-17 23:55:07 -08001009 switch(target_baseband())
1010 {
1011 case BASEBAND_APQ:
1012 src = baseband_apq;
1013 if (have_cmdline) --dst;
1014 while ((*dst++ = *src++));
1015 break;
1016
1017 case BASEBAND_MSM:
1018 src = baseband_msm;
1019 if (have_cmdline) --dst;
1020 while ((*dst++ = *src++));
1021 break;
1022
1023 case BASEBAND_CSFB:
1024 src = baseband_csfb;
1025 if (have_cmdline) --dst;
1026 while ((*dst++ = *src++));
1027 break;
1028
Ajay Dudani6cff85e2011-02-04 16:02:16 -08001029 case BASEBAND_SVLTE2A:
1030 src = baseband_svlte2a;
Ajay Dudanid04110c2011-01-17 23:55:07 -08001031 if (have_cmdline) --dst;
1032 while ((*dst++ = *src++));
1033 break;
Ajay Dudani403bc492011-09-30 16:17:21 -07001034
1035 case BASEBAND_MDM:
1036 src = baseband_mdm;
1037 if (have_cmdline) --dst;
1038 while ((*dst++ = *src++));
1039 break;
Amol Jadi5c61a952012-05-04 17:05:35 -07001040
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -08001041 case BASEBAND_MDM2:
1042 src = baseband_mdm2;
1043 if (have_cmdline) --dst;
1044 while ((*dst++ = *src++));
1045 break;
1046
Amol Jadi5c61a952012-05-04 17:05:35 -07001047 case BASEBAND_SGLTE:
1048 src = baseband_sglte;
1049 if (have_cmdline) --dst;
1050 while ((*dst++ = *src++));
1051 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +05301052
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -08001053 case BASEBAND_SGLTE2:
1054 src = baseband_sglte2;
1055 if (have_cmdline) --dst;
1056 while ((*dst++ = *src++));
1057 break;
1058
Channagoud Kadabi141f2982012-10-31 11:23:02 +05301059 case BASEBAND_DSDA:
1060 src = baseband_dsda;
1061 if (have_cmdline) --dst;
1062 while ((*dst++ = *src++));
1063 break;
Amol Jadi2a15a272013-01-22 12:03:36 -08001064
1065 case BASEBAND_DSDA2:
1066 src = baseband_dsda2;
1067 if (have_cmdline) --dst;
1068 while ((*dst++ = *src++));
1069 break;
Vijay Kumar Pendotib228cfc2016-06-13 20:15:23 +05301070 case BASEBAND_APQ_NOWGR:
1071 src = baseband_apq_nowgr;
1072 if (have_cmdline) --dst;
1073 while ((*dst++ = *src++));
1074 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -08001075 }
Dhaval Patel223ec952013-07-18 14:49:44 -07001076
1077 if (strlen(display_panel_buf)) {
Dhaval Patel223ec952013-07-18 14:49:44 -07001078 src = display_panel_buf;
1079 if (have_cmdline) --dst;
1080 while ((*dst++ = *src++));
1081 }
Joonwoo Park61112782013-10-02 19:50:39 -07001082
1083 if (have_target_boot_params) {
1084 if (have_cmdline) --dst;
1085 src = target_boot_params;
1086 while ((*dst++ = *src++));
vijay kumar870515d2015-08-31 16:37:24 +05301087 free(target_boot_params);
Joonwoo Park61112782013-10-02 19:50:39 -07001088 }
Channagoud Kadabi85c7ec32016-01-28 23:09:21 -08001089
Mayank Grover351a75e2017-05-30 20:06:08 +05301090 if (partition_multislot_is_supported() && have_cmdline)
1091 {
1092 src = androidboot_slot_suffix;
1093 --dst;
1094 while ((*dst++ = *src++));
1095 --dst;
1096 src = SUFFIX_SLOT(current_active_slot);
1097 while ((*dst++ = *src++));
Mayank Grover5384ed82018-05-09 12:09:24 +05301098 }
Mayank Grover351a75e2017-05-30 20:06:08 +05301099
Mayank Grover351a75e2017-05-30 20:06:08 +05301100
Mayank Grover5384ed82018-05-09 12:09:24 +05301101 /*
1102 * System-As-Root behaviour, system.img should contain both
1103 * system content and ramdisk content, and should be mounted at
1104 * root(a/b).
1105 * Apending skip_ramfs for non a/b builds which use, system as root.
1106 */
1107 if ((target_uses_system_as_root() ||
1108 partition_multislot_is_supported()) &&
1109 have_cmdline)
1110 {
Mayank Groverb716edf2019-05-08 16:06:55 +05301111 if (!target_dynamic_partition_supported() &&
1112 !boot_into_recovery)
Mayank Grover5384ed82018-05-09 12:09:24 +05301113 {
1114 src = skip_ramfs;
Mayank Grover351a75e2017-05-30 20:06:08 +05301115 --dst;
1116 while ((*dst++ = *src++));
Mayank Grover5384ed82018-05-09 12:09:24 +05301117 }
1118
1119 src = sys_path_cmdline;
1120 --dst;
1121 while ((*dst++ = *src++));
Mayank Grover3804be72017-06-22 11:59:23 +05301122
Monika Singh292b3e92018-03-17 22:40:23 +05301123#ifndef VERIFIED_BOOT_2
Mayank Grover5384ed82018-05-09 12:09:24 +05301124 src = syspath_buf;
1125 --dst;
1126 while ((*dst++ = *src++));
Monika Singh292b3e92018-03-17 22:40:23 +05301127#endif
Mayank Grover351a75e2017-05-30 20:06:08 +05301128 }
1129
Mayank Grover466d6562018-05-10 14:52:20 +05301130#if HIBERNATION_SUPPORT
1131 if (swap_ptn_index != INVALID_PTN)
1132 {
1133 src = resume_buf;
1134 --dst;
1135 while ((*dst++ = *src++));
1136 }
1137#endif
1138
Channagoud Kadabi85c7ec32016-01-28 23:09:21 -08001139#if TARGET_CMDLINE_SUPPORT
1140 if (target_cmdline_buf && target_cmd_line_len)
1141 {
1142 if (have_cmdline) --dst;
1143 src = target_cmdline_buf;
1144 while((*dst++ = *src++));
1145 free(target_cmdline_buf);
1146 }
1147#endif
lijuang83ef4b22018-08-23 11:01:55 +08001148
1149#if !VERITY_LE
1150 if (dtbo_idx != INVALID_PTN) {
1151 src = dtbo_idx_str;
1152 --dst;
1153 while ((*dst++ = *src++));
1154 }
Parth Dixit1375d9e2019-07-08 20:56:13 +05301155
1156 if (dtb_idx != INVALID_PTN) {
1157 src = dtb_idx_str;
1158 --dst;
1159 while ((*dst++ = *src++));
1160 }
lijuang83ef4b22018-08-23 11:01:55 +08001161#endif
jhchen7a504d12020-04-24 15:45:57 +08001162
1163/*[Arima_8910][jhchen] add fuse check property 20181031 begin*/
1164#ifdef ENABLE_FUSE_CHECK
1165 if (is_fused) {
1166 src = fuse_blown;
1167 if (have_cmdline) --dst;
1168 while ((*dst++ = *src++));
1169 } else {
1170 src = fuse_not_blown;
1171 if (have_cmdline) --dst;
1172 while ((*dst++ = *src++));
1173 }
1174#endif
1175/*[Arima_8910][jhchen] 20181031 end*/
Neeti Desaie245d492012-06-01 12:52:13 -07001176 }
Dhaval Patel223ec952013-07-18 14:49:44 -07001177
1178
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -07001179 if (boot_dev_buf)
1180 free(boot_dev_buf);
1181
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -08001182 if (cmdline_final)
1183 dprintf(INFO, "cmdline: %s\n", cmdline_final);
1184 else
1185 dprintf(INFO, "cmdline is NULL\n");
Neeti Desaie245d492012-06-01 12:52:13 -07001186 return cmdline_final;
1187}
1188
1189unsigned *atag_core(unsigned *ptr)
1190{
1191 /* CORE */
1192 *ptr++ = 2;
1193 *ptr++ = 0x54410001;
1194
1195 return ptr;
1196
1197}
1198
1199unsigned *atag_ramdisk(unsigned *ptr, void *ramdisk,
1200 unsigned ramdisk_size)
1201{
1202 if (ramdisk_size) {
1203 *ptr++ = 4;
1204 *ptr++ = 0x54420005;
1205 *ptr++ = (unsigned)ramdisk;
1206 *ptr++ = ramdisk_size;
Brian Swetland9c4c0752009-01-25 16:23:50 -08001207 }
1208
Neeti Desaie245d492012-06-01 12:52:13 -07001209 return ptr;
1210}
1211
1212unsigned *atag_ptable(unsigned **ptr_addr)
1213{
1214 int i;
1215 struct ptable *ptable;
1216
1217 if ((ptable = flash_get_ptable()) && (ptable->count != 0)) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001218 *(*ptr_addr)++ = 2 + (ptable->count * (sizeof(struct atag_ptbl_entry) /
1219 sizeof(unsigned)));
Neeti Desaie245d492012-06-01 12:52:13 -07001220 *(*ptr_addr)++ = 0x4d534d70;
1221 for (i = 0; i < ptable->count; ++i)
1222 ptentry_to_tag(ptr_addr, ptable_get(ptable, i));
1223 }
1224
1225 return (*ptr_addr);
1226}
1227
1228unsigned *atag_cmdline(unsigned *ptr, const char *cmdline)
1229{
1230 int cmdline_length = 0;
1231 int n;
Neeti Desaie245d492012-06-01 12:52:13 -07001232 char *dest;
1233
Amol Jadi10c7d1c2013-01-25 13:24:29 -08001234 cmdline_length = strlen((const char*)cmdline);
Neeti Desaie245d492012-06-01 12:52:13 -07001235 n = (cmdline_length + 4) & (~3);
1236
1237 *ptr++ = (n / 4) + 2;
1238 *ptr++ = 0x54410009;
1239 dest = (char *) ptr;
Amol Jadi10c7d1c2013-01-25 13:24:29 -08001240 while ((*dest++ = *cmdline++));
Neeti Desaie245d492012-06-01 12:52:13 -07001241 ptr += (n / 4);
1242
1243 return ptr;
1244}
1245
1246unsigned *atag_end(unsigned *ptr)
1247{
Brian Swetland9c4c0752009-01-25 16:23:50 -08001248 /* END */
1249 *ptr++ = 0;
1250 *ptr++ = 0;
1251
Neeti Desaie245d492012-06-01 12:52:13 -07001252 return ptr;
1253}
1254
1255void generate_atags(unsigned *ptr, const char *cmdline,
1256 void *ramdisk, unsigned ramdisk_size)
1257{
vijay kumar21a37452015-12-29 16:23:21 +05301258 unsigned *orig_ptr = ptr;
Neeti Desaie245d492012-06-01 12:52:13 -07001259 ptr = atag_core(ptr);
1260 ptr = atag_ramdisk(ptr, ramdisk, ramdisk_size);
1261 ptr = target_atag_mem(ptr);
1262
1263 /* Skip NAND partition ATAGS for eMMC boot */
1264 if (!target_is_emmc_boot()){
1265 ptr = atag_ptable(&ptr);
1266 }
1267
vijay kumar21a37452015-12-29 16:23:21 +05301268 /*
1269 * Atags size filled till + cmdline size + 1 unsigned for 4-byte boundary + 4 unsigned
1270 * for atag identifier in atag_cmdline and atag_end should be with in MAX_TAGS_SIZE bytes
1271 */
Mayank Groverbc8a2ef2018-02-23 18:03:36 +05301272 if (!cmdline)
1273 return;
1274
vijay kumar21a37452015-12-29 16:23:21 +05301275 if (((ptr - orig_ptr) + strlen(cmdline) + 5 * sizeof(unsigned)) < MAX_TAGS_SIZE) {
1276 ptr = atag_cmdline(ptr, cmdline);
1277 ptr = atag_end(ptr);
1278 }
1279 else {
1280 dprintf(CRITICAL,"Crossing ATAGs Max size allowed\n");
1281 ASSERT(0);
1282 }
Neeti Desaie245d492012-06-01 12:52:13 -07001283}
1284
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001285typedef void entry_func_ptr(unsigned, unsigned, unsigned*);
Neeti Desaie245d492012-06-01 12:52:13 -07001286void boot_linux(void *kernel, unsigned *tags,
1287 const char *cmdline, unsigned machtype,
1288 void *ramdisk, unsigned ramdisk_size)
1289{
Amol Jadi10c7d1c2013-01-25 13:24:29 -08001290 unsigned char *final_cmdline;
Amol Jadib6be5c12012-11-14 13:39:51 -08001291#if DEVICE_TREE
Neeti Desai17379b82012-06-04 18:42:53 -07001292 int ret = 0;
Amol Jadib6be5c12012-11-14 13:39:51 -08001293#endif
1294
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001295 void (*entry)(unsigned, unsigned, unsigned*) = (entry_func_ptr*)(PA((addr_t)kernel));
Deepa Dinamani25a9f762012-11-30 15:57:15 -08001296 uint32_t tags_phys = PA((addr_t)tags);
vijay kumar1a50a642015-11-16 12:41:15 +05301297 struct kernel64_hdr *kptr = ((struct kernel64_hdr*)(PA((addr_t)kernel)));
Deepa Dinamani25a9f762012-11-30 15:57:15 -08001298
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301299 ramdisk = (void *)PA((addr_t)ramdisk);
Neeti Desaie245d492012-06-01 12:52:13 -07001300
Amol Jadi10c7d1c2013-01-25 13:24:29 -08001301 final_cmdline = update_cmdline((const char*)cmdline);
1302
Neeti Desai17379b82012-06-04 18:42:53 -07001303#if DEVICE_TREE
Amol Jadib6be5c12012-11-14 13:39:51 -08001304 dprintf(INFO, "Updating device tree: start\n");
1305
Neeti Desai17379b82012-06-04 18:42:53 -07001306 /* Update the Device Tree */
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301307 ret = update_device_tree((void *)tags,(const char *)final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -07001308 if(ret)
1309 {
1310 dprintf(CRITICAL, "ERROR: Updating Device Tree Failed \n");
1311 ASSERT(0);
1312 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001313 dprintf(INFO, "Updating device tree: done\n");
Neeti Desai17379b82012-06-04 18:42:53 -07001314#else
Neeti Desaie245d492012-06-01 12:52:13 -07001315 /* Generating the Atags */
Amol Jadi10c7d1c2013-01-25 13:24:29 -08001316 generate_atags(tags, final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -07001317#endif
Neeti Desaie245d492012-06-01 12:52:13 -07001318
Monika Singh0ec6db82019-07-18 16:57:58 +05301319#if VERIFIED_BOOT
Monika Singhb0db4b82018-09-26 12:18:02 +05301320 if (VB_M <= target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +05301321 {
1322 if (device.verity_mode == 0) {
lijuangbdd9bb42016-03-01 18:22:17 +08001323#if FBCON_DISPLAY_MSG
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07001324#if ENABLE_VB_ATTEST
Mayank Grover889be1b2017-09-12 20:12:23 +05301325 display_bootverify_menu(DISPLAY_MENU_EIO);
1326 wait_for_users_action();
1327 if(!pwr_key_is_pressed)
1328 shutdown_device();
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07001329#else
Mayank Grover889be1b2017-09-12 20:12:23 +05301330 display_bootverify_menu(DISPLAY_MENU_LOGGING);
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07001331#endif
Mayank Grover889be1b2017-09-12 20:12:23 +05301332 wait_for_users_action();
lijuangbdd9bb42016-03-01 18:22:17 +08001333#else
Mayank Grover889be1b2017-09-12 20:12:23 +05301334 dprintf(CRITICAL,
1335 "The dm-verity is not started in enforcing mode.\nWait for 5 seconds before proceeding\n");
1336 mdelay(5000);
lijuangbdd9bb42016-03-01 18:22:17 +08001337#endif
Mayank Grover889be1b2017-09-12 20:12:23 +05301338 }
lijuangbdd9bb42016-03-01 18:22:17 +08001339 }
lijuangbdd9bb42016-03-01 18:22:17 +08001340#endif
1341
1342#if VERIFIED_BOOT
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001343 /* Write protect the device info */
Channagoud Kadabi3bd9d1e2015-05-05 16:18:20 -07001344 if (!boot_into_recovery && target_build_variant_user() && devinfo_present && mmc_write_protect("devinfo", 1))
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001345 {
1346 dprintf(INFO, "Failed to write protect dev info\n");
1347 ASSERT(0);
1348 }
1349#endif
1350
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08001351 /* Turn off splash screen if enabled */
1352#if DISPLAY_SPLASH_SCREEN
1353 target_display_shutdown();
1354#endif
1355
Veera Sundaram Sankaran67ea0932015-09-25 10:09:30 -07001356 /* Perform target specific cleanup */
1357 target_uninit();
Monika Singh292b3e92018-03-17 22:40:23 +05301358 free_verified_boot_resource(&info);
1359 if (final_cmdline)
1360 free(final_cmdline);
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08001361
Deepa Dinamani33734bc2013-03-06 12:16:06 -08001362 dprintf(INFO, "booting linux @ %p, ramdisk @ %p (%d), tags/device tree @ %p\n",
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301363 entry, ramdisk, ramdisk_size, (void *)tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001364
1365 enter_critical_section();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001366
Amol Jadi4421e652011-06-16 15:00:48 -07001367 /* do any platform specific cleanup before kernel entry */
1368 platform_uninit();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001369
Brian Swetland9c4c0752009-01-25 16:23:50 -08001370 arch_disable_cache(UCACHE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001371
Amol Jadi504f9fe2012-08-16 13:56:48 -07001372#if ARM_WITH_MMU
Brian Swetland9c4c0752009-01-25 16:23:50 -08001373 arch_disable_mmu();
Amol Jadi504f9fe2012-08-16 13:56:48 -07001374#endif
Amol Jadi492d5a52013-03-15 16:12:34 -07001375 bs_set_timestamp(BS_KERNEL_ENTRY);
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001376
1377 if (IS_ARM64(kptr))
1378 /* Jump to a 64bit kernel */
1379 scm_elexec_call((paddr_t)kernel, tags_phys);
1380 else
1381 /* Jump to a 32bit kernel */
1382 entry(0, machtype, (unsigned*)tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001383}
1384
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001385/* Function to check if the memory address range falls within the aboot
1386 * boundaries.
1387 * start: Start of the memory region
1388 * size: Size of the memory region
1389 */
Vijay Kumar Pendoti9c002ad2016-03-09 13:52:45 +05301390int check_aboot_addr_range_overlap(uintptr_t start, uint32_t size)
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001391{
1392 /* Check for boundary conditions. */
Sundarajan Srinivasance2a0ea2013-12-16 17:02:56 -08001393 if ((UINT_MAX - start) < size)
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001394 return -1;
1395
1396 /* Check for memory overlap. */
1397 if ((start < MEMBASE) && ((start + size) <= MEMBASE))
1398 return 0;
Channagoud Kadabi94143912013-10-15 12:53:52 -07001399 else if (start >= (MEMBASE + MEMSIZE))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001400 return 0;
1401 else
1402 return -1;
1403}
1404
Mayank Grovere559cec2017-10-17 15:12:03 +05301405/* Function to check if the memory address range falls beyond ddr region.
1406 * start: Start of the memory region
1407 * size: Size of the memory region
1408 */
1409int check_ddr_addr_range_bound(uintptr_t start, uint32_t size)
1410{
1411 uintptr_t ddr_pa_start_addr = PA(get_ddr_start());
1412 uint64_t ddr_size = smem_get_ddr_size();
1413 uint64_t ddr_pa_end_addr = ddr_pa_start_addr + ddr_size;
1414 uintptr_t pa_start_addr = PA(start);
1415
1416 /* Check for boundary conditions. */
1417 if ((UINT_MAX - start) < size)
1418 return -1;
1419
1420 /* Check if memory range is beyond the ddr range. */
1421 if (pa_start_addr < ddr_pa_start_addr ||
1422 pa_start_addr >= (ddr_pa_end_addr) ||
1423 (pa_start_addr + size) > ddr_pa_end_addr)
1424 return -1;
1425 else
1426 return 0;
1427}
1428
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001429BUF_DMA_ALIGN(buf, BOOT_IMG_MAX_PAGE_SIZE); //Equal to max-supported pagesize
Dima Zavin214cc642009-01-26 11:16:21 -08001430
Mayank Groverdd080e82018-09-04 20:12:13 +05301431int getimage(void **image_buffer, uint32_t *imgsize,
1432 const char *imgname)
Monika Singh292b3e92018-03-17 22:40:23 +05301433{
Mayank Groverdd080e82018-09-04 20:12:13 +05301434 uint32_t loadedindex;
1435 if (image_buffer == NULL || imgsize == NULL ||
Monika Singh292b3e92018-03-17 22:40:23 +05301436 imgname == NULL) {
1437 dprintf(CRITICAL, "getimage: invalid parameters\n");
1438 return -1;
1439 }
Mayank Groverdd080e82018-09-04 20:12:13 +05301440 for (loadedindex = 0; loadedindex < info.num_loaded_images; loadedindex++) {
1441 if (!strncmp(info.images[loadedindex].name, imgname,
Monika Singh292b3e92018-03-17 22:40:23 +05301442 strlen(imgname))) {
Mayank Groverdd080e82018-09-04 20:12:13 +05301443 *image_buffer = info.images[loadedindex].image_buffer;
1444 *imgsize = info.images[loadedindex].imgsize;
Mayank Grover027a9412018-09-04 15:12:05 +05301445 dprintf(SPEW, "getimage(): Loaded image [%s|%d]\n",
1446 info.images[loadedindex].name,
1447 info.images[loadedindex].imgsize);
Monika Singh292b3e92018-03-17 22:40:23 +05301448 return 0;
1449 }
1450 }
1451 return -1;
1452}
1453
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001454static void verify_signed_bootimg(uint32_t bootimg_addr, uint32_t bootimg_size)
1455{
1456 int ret;
Channagoud Kadabia8c623f2015-01-13 14:48:48 -08001457
1458#if !VERIFIED_BOOT
Sundarajan Srinivasance54d6e2013-11-11 12:45:00 -08001459#if IMAGE_VERIF_ALGO_SHA1
1460 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA1;
1461#else
1462 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA256;
1463#endif
Channagoud Kadabia8c623f2015-01-13 14:48:48 -08001464#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001465
1466 /* Assume device is rooted at this time. */
1467 device.is_tampered = 1;
1468
1469 dprintf(INFO, "Authenticating boot image (%d): start\n", bootimg_size);
1470
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001471#if VERIFIED_BOOT
Monika Singh7d2fc272018-03-16 17:16:01 +05301472 uint32_t bootstate;
1473 if(boot_into_recovery &&
Mayank Grover68fbf0d2017-10-24 14:13:39 +05301474 (!partition_multislot_is_supported()))
Monika Singh7d2fc272018-03-16 17:16:01 +05301475 {
1476 ret = boot_verify_image((unsigned char *)bootimg_addr,
1477 bootimg_size, "/recovery", &bootstate);
1478 }
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001479 else
Monika Singh7d2fc272018-03-16 17:16:01 +05301480 {
1481 ret = boot_verify_image((unsigned char *)bootimg_addr,
1482 bootimg_size, "/boot", &bootstate);
1483 }
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001484 boot_verify_print_state();
1485#else
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001486 ret = image_verify((unsigned char *)bootimg_addr,
1487 (unsigned char *)(bootimg_addr + bootimg_size),
1488 bootimg_size,
Sundarajan Srinivasance54d6e2013-11-11 12:45:00 -08001489 auth_algo);
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001490#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001491 dprintf(INFO, "Authenticating boot image: done return value = %d\n", ret);
1492
1493 if (ret)
1494 {
1495 /* Authorized kernel */
1496 device.is_tampered = 0;
Sundarajan Srinivasan3fb21f12013-09-16 18:36:15 -07001497 auth_kernel_img = 1;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001498 }
1499
Amit Blay4aa292f2015-04-28 21:55:59 +03001500#ifdef MDTP_SUPPORT
1501 {
1502 /* Verify MDTP lock.
1503 * For boot & recovery partitions, use aboot's verification result.
1504 */
1505 mdtp_ext_partition_verification_t ext_partition;
1506 ext_partition.partition = boot_into_recovery ? MDTP_PARTITION_RECOVERY : MDTP_PARTITION_BOOT;
1507 ext_partition.integrity_state = device.is_tampered ? MDTP_PARTITION_STATE_INVALID : MDTP_PARTITION_STATE_VALID;
1508 ext_partition.page_size = 0; /* Not needed since already validated */
1509 ext_partition.image_addr = 0; /* Not needed since already validated */
1510 ext_partition.image_size = 0; /* Not needed since already validated */
1511 ext_partition.sig_avail = FALSE; /* Not needed since already validated */
1512 mdtp_fwlock_verify_lock(&ext_partition);
1513 }
1514#endif /* MDTP_SUPPORT */
1515
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001516#if USE_PCOM_SECBOOT
1517 set_tamper_flag(device.is_tampered);
1518#endif
1519
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001520#if VERIFIED_BOOT
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001521 switch(boot_verify_get_state())
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001522 {
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001523 case RED:
lijuanga40d6302015-07-20 20:10:13 +08001524#if FBCON_DISPLAY_MSG
lijuang9a7d3b92015-11-30 14:41:24 +08001525 display_bootverify_menu(DISPLAY_MENU_RED);
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07001526#if ENABLE_VB_ATTEST
1527 mdelay(DELAY_WAIT);
1528 shutdown_device();
1529#else
lijuanga40d6302015-07-20 20:10:13 +08001530 wait_for_users_action();
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07001531#endif
lijuanga40d6302015-07-20 20:10:13 +08001532#else
1533 dprintf(CRITICAL,
1534 "Your device has failed verification and may not work properly.\nWait for 5 seconds before proceeding\n");
1535 mdelay(5000);
1536#endif
1537
1538 break;
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001539 case YELLOW:
lijuanga40d6302015-07-20 20:10:13 +08001540#if FBCON_DISPLAY_MSG
lijuang9a7d3b92015-11-30 14:41:24 +08001541 display_bootverify_menu(DISPLAY_MENU_YELLOW);
lijuanga40d6302015-07-20 20:10:13 +08001542 wait_for_users_action();
1543#else
1544 dprintf(CRITICAL,
1545 "Your device has loaded a different operating system.\nWait for 5 seconds before proceeding\n");
1546 mdelay(5000);
1547#endif
1548 break;
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001549 default:
lijuanga40d6302015-07-20 20:10:13 +08001550 break;
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001551 }
1552#endif
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001553#if !VERIFIED_BOOT
Unnati Gandhi1be04752015-03-27 19:41:53 +05301554 if(device.is_tampered)
1555 {
1556 write_device_info_mmc(&device);
1557 #ifdef TZ_TAMPER_FUSE
Monika Singh98257462018-06-06 11:28:49 +05301558 set_tamper_fuse_cmd(HLOS_IMG_TAMPER_FUSE);
Unnati Gandhi1be04752015-03-27 19:41:53 +05301559 #endif
1560 #ifdef ASSERT_ON_TAMPER
1561 dprintf(CRITICAL, "Device is tampered. Asserting..\n");
1562 ASSERT(0);
1563 #endif
1564 }
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001565#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001566}
1567
Maunik Shah0f3c8ac2014-03-02 17:47:58 +05301568static bool check_format_bit()
1569{
1570 bool ret = false;
1571 int index;
1572 uint64_t offset;
1573 struct boot_selection_info *in = NULL;
1574 char *buf = NULL;
1575
1576 index = partition_get_index("bootselect");
1577 if (index == INVALID_PTN)
1578 {
1579 dprintf(INFO, "Unable to locate /bootselect partition\n");
1580 return ret;
1581 }
1582 offset = partition_get_offset(index);
1583 if(!offset)
1584 {
1585 dprintf(INFO, "partition /bootselect doesn't exist\n");
1586 return ret;
1587 }
1588 buf = (char *) memalign(CACHE_LINE, ROUNDUP(page_size, CACHE_LINE));
Mayank Grover48860402016-11-29 12:34:53 +05301589 mmc_set_lun(partition_get_lun(index));
Maunik Shah0f3c8ac2014-03-02 17:47:58 +05301590 ASSERT(buf);
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301591 if (mmc_read(offset, (uint32_t *)buf, page_size))
Maunik Shah0f3c8ac2014-03-02 17:47:58 +05301592 {
1593 dprintf(INFO, "mmc read failure /bootselect %d\n", page_size);
1594 free(buf);
1595 return ret;
1596 }
1597 in = (struct boot_selection_info *) buf;
1598 if ((in->signature == BOOTSELECT_SIGNATURE) &&
1599 (in->version == BOOTSELECT_VERSION)) {
1600 if ((in->state_info & BOOTSELECT_FORMAT) &&
1601 !(in->state_info & BOOTSELECT_FACTORY))
1602 ret = true;
1603 } else {
1604 dprintf(CRITICAL, "Signature: 0x%08x or version: 0x%08x mismatched of /bootselect\n",
1605 in->signature, in->version);
1606 ASSERT(0);
1607 }
1608 free(buf);
1609 return ret;
1610}
1611
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001612void boot_verifier_init()
1613{
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001614 uint32_t boot_state;
1615 /* Check if device unlock */
1616 if(device.is_unlocked)
1617 {
1618 boot_verify_send_event(DEV_UNLOCK);
1619 boot_verify_print_state();
1620 dprintf(CRITICAL, "Device is unlocked! Skipping verification...\n");
1621 return;
1622 }
1623 else
1624 {
1625 boot_verify_send_event(BOOT_INIT);
1626 }
1627
1628 /* Initialize keystore */
1629 boot_state = boot_verify_keystore_init();
1630 if(boot_state == YELLOW)
1631 {
1632 boot_verify_print_state();
1633 dprintf(CRITICAL, "Keystore verification failed! Continuing anyways...\n");
1634 }
1635}
1636
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301637/* Function to return recovery appended dtbo buffer info */
1638void get_recovery_dtbo_info(uint32_t *dtbo_size, void **dtbo_buf)
1639{
1640 *dtbo_size = recovery_dtbo_size;
1641 *dtbo_buf = recovery_dtbo_buf;
1642 return;
1643}
1644
Shashank Mittal23b8f422010-04-16 19:27:21 -07001645int boot_linux_from_mmc(void)
1646{
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301647 boot_img_hdr *hdr = (void*) buf;
1648 boot_img_hdr *uhdr;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001649 unsigned offset = 0;
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07001650 int rcode;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001651 unsigned long long ptn = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001652 int index = INVALID_PTN;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001653
Shashank Mittalcd98d472011-08-02 14:29:24 -07001654 unsigned char *image_addr = 0;
1655 unsigned kernel_actual;
1656 unsigned ramdisk_actual;
1657 unsigned imagesize_actual;
Neeti Desai465491e2012-07-31 12:53:35 -07001658 unsigned second_actual = 0;
Parth Dixit1375d9e2019-07-08 20:56:13 +05301659 void * image_buf = NULL;
Neeti Desai465491e2012-07-31 12:53:35 -07001660
Matthew Qin271927e2015-03-31 22:07:07 -04001661 unsigned int dtb_size = 0;
Parth Dixit1375d9e2019-07-08 20:56:13 +05301662 unsigned dtb_image_size = 0;
1663 uint32_t dtb_image_offset = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08001664 unsigned int out_len = 0;
1665 unsigned int out_avai_len = 0;
1666 unsigned char *out_addr = NULL;
1667 uint32_t dtb_offset = 0;
1668 unsigned char *kernel_start_addr = NULL;
1669 unsigned int kernel_size = 0;
Ameya Thakur10a33452016-06-13 14:24:26 -07001670 unsigned int patched_kernel_hdr_size = 0;
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301671 uint64_t image_size = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08001672 int rc;
Monika Singh292b3e92018-03-17 22:40:23 +05301673#if VERIFIED_BOOT_2
1674 int status;
Mayank Grover027a9412018-09-04 15:12:05 +05301675 void *dtbo_image_buf = NULL;
1676 uint32_t dtbo_image_sz = 0;
Mayank Grover9df84c02018-08-30 15:46:35 +05301677 void *vbmeta_image_buf = NULL;
1678 uint32_t vbmeta_image_sz = 0;
Monika Singh292b3e92018-03-17 22:40:23 +05301679#endif
Mayank Groverc93cad82017-10-03 12:23:45 +05301680 char *ptn_name = NULL;
Neeti Desai465491e2012-07-31 12:53:35 -07001681#if DEVICE_TREE
1682 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -07001683 struct dt_entry dt_entry;
Neeti Desai465491e2012-07-31 12:53:35 -07001684 unsigned dt_table_offset;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001685 uint32_t dt_actual;
Deepa Dinamani19648b42013-09-05 17:05:55 -07001686 uint32_t dt_hdr_size;
Matthew Qin271927e2015-03-31 22:07:07 -04001687 unsigned char *best_match_dt_addr = NULL;
Neeti Desai465491e2012-07-31 12:53:35 -07001688#endif
Matthew Qin49e51fa2015-02-09 10:40:45 +08001689 struct kernel64_hdr *kptr = NULL;
Mayank Grover351a75e2017-05-30 20:06:08 +05301690 int current_active_slot = INVALID;
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001691
Maunik Shah0f3c8ac2014-03-02 17:47:58 +05301692 if (check_format_bit())
1693 boot_into_recovery = 1;
1694
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07001695 if (!boot_into_recovery) {
1696 memset(ffbm_mode_string, '\0', sizeof(ffbm_mode_string));
1697 rcode = get_ffbm(ffbm_mode_string, sizeof(ffbm_mode_string));
1698 if (rcode <= 0) {
1699 boot_into_ffbm = false;
1700 if (rcode < 0)
1701 dprintf(CRITICAL,"failed to get ffbm cookie");
1702 } else
1703 boot_into_ffbm = true;
1704 } else
1705 boot_into_ffbm = false;
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301706 uhdr = (boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001707 if (!memcmp(uhdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1708 dprintf(INFO, "Unified boot method!\n");
1709 hdr = uhdr;
1710 goto unified_boot;
1711 }
Mayank Groverc93cad82017-10-03 12:23:45 +05301712
1713 /* For a/b recovery image code is on boot partition.
1714 If we support multislot, always use boot partition. */
1715 if (boot_into_recovery &&
Mayank Grover8ab4a762019-04-25 17:23:34 +05301716 ((!partition_multislot_is_supported()) ||
1717 (target_dynamic_partition_supported())))
Mayank Groverc93cad82017-10-03 12:23:45 +05301718 ptn_name = "recovery";
1719 else
1720 ptn_name = "boot";
1721
1722 index = partition_get_index(ptn_name);
1723 ptn = partition_get_offset(index);
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301724 image_size = partition_get_size(index);
1725 if(ptn == 0 || image_size == 0) {
Mayank Groverc93cad82017-10-03 12:23:45 +05301726 dprintf(CRITICAL, "ERROR: No %s partition found\n", ptn_name);
1727 return -1;
Kinson Chikf1a43512011-07-14 11:28:39 -07001728 }
Mayank Groverc93cad82017-10-03 12:23:45 +05301729
Channagoud Kadabief0547c2015-02-10 12:57:38 -08001730 /* Set Lun for boot & recovery partitions */
1731 mmc_set_lun(partition_get_lun(index));
Shashank Mittal23b8f422010-04-16 19:27:21 -07001732
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301733 if (mmc_read(ptn + offset, (uint32_t *) buf, page_size)) {
Shashank Mittal23b8f422010-04-16 19:27:21 -07001734 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
1735 return -1;
1736 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001737
1738 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001739 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Mayank Grover351a75e2017-05-30 20:06:08 +05301740 return ERR_INVALID_BOOT_MAGIC;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001741 }
1742
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07001743 if (hdr->page_size && (hdr->page_size != page_size)) {
vijay kumar2e21b3a2014-06-26 17:40:15 +05301744
1745 if (hdr->page_size > BOOT_IMG_MAX_PAGE_SIZE) {
1746 dprintf(CRITICAL, "ERROR: Invalid page size\n");
1747 return -1;
1748 }
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07001749 page_size = hdr->page_size;
1750 page_mask = page_size - 1;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001751 }
1752
Matthew Qin49e51fa2015-02-09 10:40:45 +08001753 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1754 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Kishor PKd8ddcad2017-07-27 13:53:57 +05301755 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001756
Matthew Qin49e51fa2015-02-09 10:40:45 +08001757 image_addr = (unsigned char *)target_get_scratch_address();
Mayank Grover6fd36792018-09-10 13:26:58 +05301758#if VERIFIED_BOOT_2
1759 /* Create hole in start of image for VB salt to copy */
1760 image_addr += SALT_BUFF_OFFSET;
1761#endif
Kishor PK9e91b592017-05-24 12:14:55 +05301762 memcpy(image_addr, (void *)buf, page_size);
1763
1764 /* ensure commandline is terminated */
1765 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
Matthew Qin49e51fa2015-02-09 10:40:45 +08001766
1767#if DEVICE_TREE
Parth Dixit4097b622016-03-15 14:42:27 +05301768#ifndef OSVERSION_IN_BOOTIMAGE
1769 dt_size = hdr->dt_size;
Mayank Grover5a502582018-09-12 11:24:49 +05301770#else
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301771 dprintf(INFO, "BootImage Header: %d\n", hdr->header_version);
Mayank Grover5a502582018-09-12 11:24:49 +05301772#endif
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301773
Parth Dixit4097b622016-03-15 14:42:27 +05301774 dt_actual = ROUND_TO_PAGE(dt_size, page_mask);
Kishor PKd8ddcad2017-07-27 13:53:57 +05301775 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 +05301776 dprintf(CRITICAL, "Integer overflow detected in bootimage header fields at %u in %s\n",__LINE__,__FILE__);
1777 return -1;
1778 }
Kishor PKd8ddcad2017-07-27 13:53:57 +05301779 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + second_actual + dt_actual);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001780#else
Kishor PKd8ddcad2017-07-27 13:53:57 +05301781 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 +05301782 dprintf(CRITICAL, "Integer overflow detected in bootimage header fields at %u in %s\n",__LINE__,__FILE__);
1783 return -1;
1784 }
Kishor PKd8ddcad2017-07-27 13:53:57 +05301785 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + second_actual);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001786#endif
Parth Dixit1375d9e2019-07-08 20:56:13 +05301787 dtb_image_size = hdr->kernel_size;
Matthew Qin49e51fa2015-02-09 10:40:45 +08001788
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301789#ifdef OSVERSION_IN_BOOTIMAGE
1790 /* If header version is ONE and booting into recovery,
1791 dtbo is appended with recovery image.
1792 Doing following:
1793 * Validating the recovery offset and size.
1794 * Extracting recovery dtbo to be used as dtbo.
1795 */
1796 if (boot_into_recovery &&
1797 hdr->header_version == BOOT_HEADER_VERSION_ONE)
1798 {
1799 struct boot_img_hdr_v1 *hdr1 =
1800 (struct boot_img_hdr_v1 *) (image_addr + sizeof(boot_img_hdr));
Mayank Grovera1a83c72018-09-24 11:23:15 +05301801 unsigned int recovery_dtbo_actual = 0;
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301802
Mayank Grovera1a83c72018-09-24 11:23:15 +05301803 recovery_dtbo_actual = ROUND_TO_PAGE(hdr1->recovery_dtbo_size, page_mask);
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301804 if ((hdr1->header_size !=
1805 sizeof(struct boot_img_hdr_v1) + sizeof(boot_img_hdr)))
1806 {
1807 dprintf(CRITICAL, "Invalid boot image header: %d\n", hdr1->header_size);
1808 return -1;
1809 }
1810
Mayank Grovera1a83c72018-09-24 11:23:15 +05301811 if (recovery_dtbo_actual > MAX_SUPPORTED_DTBO_IMG_BUF)
1812 {
1813 dprintf(CRITICAL, "Recovery Dtbo Size too big %x, Allowed size %x\n", recovery_dtbo_actual,
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301814 MAX_SUPPORTED_DTBO_IMG_BUF);
1815 return -1;
1816 }
1817
Mayank Grovera1a83c72018-09-24 11:23:15 +05301818 if (UINT_MAX < ((uint64_t)imagesize_actual + recovery_dtbo_actual))
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301819 {
1820 dprintf(CRITICAL, "Integer overflow detected in recoveryimage header fields at %u in %s\n",__LINE__,__FILE__);
1821 return -1;
1822 }
1823
Mayank Grovera1a83c72018-09-24 11:23:15 +05301824 if (UINT_MAX < (hdr1->recovery_dtbo_offset + recovery_dtbo_actual)) {
1825 dprintf(CRITICAL,
1826 "Integer overflow detected in recovery image header fields at %u in %s\n",__LINE__,__FILE__);
1827 return -1;
1828 }
1829
1830 if (hdr1->recovery_dtbo_offset + recovery_dtbo_actual > image_size)
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301831 {
1832 dprintf(CRITICAL, "Invalid recovery dtbo: Recovery Dtbo Offset=0x%llx,"
1833 " Recovery Dtbo Size=0x%x, Image Size=0x%llx\n",
1834 hdr1->recovery_dtbo_offset, recovery_dtbo_size, image_size);
1835 return -1;
1836 }
1837
1838 recovery_dtbo_buf = (void *)(hdr1->recovery_dtbo_offset + image_addr);
Mayank Grovera1a83c72018-09-24 11:23:15 +05301839 recovery_dtbo_size = recovery_dtbo_actual;
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301840 imagesize_actual += recovery_dtbo_size;
1841
1842 dprintf(SPEW, "Header version: %d\n", hdr->header_version);
1843 dprintf(SPEW, "Recovery Dtbo Size 0x%x\n", recovery_dtbo_size);
1844 dprintf(SPEW, "Recovery Dtbo Offset 0x%llx\n", hdr1->recovery_dtbo_offset);
1845
1846 }
Parth Dixit1375d9e2019-07-08 20:56:13 +05301847
1848 if ( hdr->header_version == BOOT_HEADER_VERSION_TWO) {
1849 struct boot_img_hdr_v1 *hdr1 =
1850 (struct boot_img_hdr_v1 *) (image_addr + sizeof(boot_img_hdr));
1851 struct boot_img_hdr_v2 *hdr2 = (struct boot_img_hdr_v2 *)
1852 (image_addr + sizeof(boot_img_hdr) +
1853 BOOT_IMAGE_HEADER_V2_OFFSET);
1854 unsigned int recovery_dtbo_actual = 0;
1855
lijuang0b17ba22019-09-04 14:20:47 +08001856 recovery_dtbo_actual =
1857 ROUND_TO_PAGE(hdr1->recovery_dtbo_size, page_mask);
1858 imagesize_actual += recovery_dtbo_actual;
Parth Dixit1375d9e2019-07-08 20:56:13 +05301859
1860 imagesize_actual += ROUND_TO_PAGE(hdr2->dtb_size, page_mask);
1861
1862
1863 dtb_image_offset = page_size + patched_kernel_hdr_size +
1864 kernel_actual + ramdisk_actual + second_actual +
1865 recovery_dtbo_actual;
1866
1867 dprintf(SPEW, "Header version: %d\n", hdr->header_version);
1868 dprintf(SPEW, "Dtb image offset 0x%x\n", dtb_image_offset);
1869 }
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301870#endif
1871
Parth Dixit1375d9e2019-07-08 20:56:13 +05301872
Saranya Chidurab1aaf232018-11-19 15:47:50 +05301873 /* Validate the boot/recovery image size is within the bounds of partition size */
1874 if (imagesize_actual > image_size) {
1875 dprintf(CRITICAL, "Image size is greater than partition size.\n");
1876 return -1;
1877 }
1878
Matthew Qin49e51fa2015-02-09 10:40:45 +08001879#if VERIFIED_BOOT
1880 boot_verifier_init();
1881#endif
1882
Mayank Grover1ceaee22019-04-01 17:54:32 +05301883#if VERIFIED_BOOT_2
1884 /* read full partition if device is unlocked */
1885 if (device.is_unlocked)
1886 imagesize_actual = image_size;
1887#endif
1888
Vijay Kumar Pendoti9c002ad2016-03-09 13:52:45 +05301889 if (check_aboot_addr_range_overlap((uintptr_t) image_addr, imagesize_actual))
Matthew Qin49e51fa2015-02-09 10:40:45 +08001890 {
1891 dprintf(CRITICAL, "Boot image buffer address overlaps with aboot addresses.\n");
1892 return -1;
1893 }
1894
Matthew Qinbb7923d2015-02-09 10:56:09 +08001895 /*
1896 * Update loading flow of bootimage to support compressed/uncompressed
1897 * bootimage on both 64bit and 32bit platform.
1898 * 1. Load bootimage from emmc partition onto DDR.
1899 * 2. Check if bootimage is gzip format. If yes, decompress compressed kernel
1900 * 3. Check kernel header and update kernel load addr for 64bit and 32bit
1901 * platform accordingly.
1902 * 4. Sanity Check on kernel_addr and ramdisk_addr and copy data.
1903 */
Mayank Grover351a75e2017-05-30 20:06:08 +05301904 if (partition_multislot_is_supported())
1905 {
1906 current_active_slot = partition_find_active_slot();
1907 dprintf(INFO, "Loading boot image (%d) active_slot(%s): start\n",
1908 imagesize_actual, SUFFIX_SLOT(current_active_slot));
1909 }
1910 else
1911 {
1912 dprintf(INFO, "Loading (%s) image (%d): start\n",
1913 (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual);
1914 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08001915 bs_set_timestamp(BS_KERNEL_LOAD_START);
1916
Gaurav Nebhwani43e2a462016-03-17 21:32:56 +05301917 if ((target_get_max_flash_size() - page_size) < imagesize_actual)
1918 {
1919 dprintf(CRITICAL, "booimage size is greater than DDR can hold\n");
1920 return -1;
1921 }
Kishor PK9e91b592017-05-24 12:14:55 +05301922 offset = page_size;
1923 /* Read image without signature and header*/
1924 if (mmc_read(ptn + offset, (void *)(image_addr + offset), imagesize_actual - page_size))
Matthew Qin49e51fa2015-02-09 10:40:45 +08001925 {
1926 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
1927 return -1;
1928 }
1929
Mayank Grover351a75e2017-05-30 20:06:08 +05301930 if (partition_multislot_is_supported())
1931 {
1932 dprintf(INFO, "Loading boot image (%d) active_slot(%s): done\n",
1933 imagesize_actual, SUFFIX_SLOT(current_active_slot));
1934 }
1935 else
1936 {
1937 dprintf(INFO, "Loading (%s) image (%d): done\n",
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001938 (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual);
1939
Mayank Grover351a75e2017-05-30 20:06:08 +05301940 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08001941 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
1942
1943 /* Authenticate Kernel */
1944 dprintf(INFO, "use_signed_kernel=%d, is_unlocked=%d, is_tampered=%d.\n",
1945 (int) target_use_signed_kernel(),
1946 device.is_unlocked,
1947 device.is_tampered);
Monika Singh292b3e92018-03-17 22:40:23 +05301948#if VERIFIED_BOOT_2
Mayank Grover1ceaee22019-04-01 17:54:32 +05301949 /* if device is unlocked skip reading signature, as full partition is read */
1950 if (!device.is_unlocked)
Monika Singh292b3e92018-03-17 22:40:23 +05301951 {
Mayank Grover1ceaee22019-04-01 17:54:32 +05301952 offset = imagesize_actual;
1953 if (check_aboot_addr_range_overlap((uintptr_t)image_addr + offset, page_size))
1954 {
1955 dprintf(CRITICAL, "Signature read buffer address overlaps with aboot addresses.\n");
1956 return -1;
1957 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08001958
Mayank Grover1ceaee22019-04-01 17:54:32 +05301959 /* Read signature */
1960 if(mmc_read(ptn + offset, (void *)(image_addr + offset), page_size))
1961 {
1962 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
1963 return -1;
1964 }
Monika Singh292b3e92018-03-17 22:40:23 +05301965 }
1966
Mayank Grover027a9412018-09-04 15:12:05 +05301967 /* load and validate dtbo partition */
1968 load_validate_dtbo_image(&dtbo_image_buf, &dtbo_image_sz);
1969
Mayank Grover9df84c02018-08-30 15:46:35 +05301970 /* load vbmeta partition */
1971 load_vbmeta_image(&vbmeta_image_buf, &vbmeta_image_sz);
1972
Monika Singh292b3e92018-03-17 22:40:23 +05301973 memset(&info, 0, sizeof(bootinfo));
Mayank Grover027a9412018-09-04 15:12:05 +05301974
1975 /* Pass loaded boot image passed */
Mayank Grover6fd36792018-09-10 13:26:58 +05301976 info.images[IMG_BOOT].image_buffer = SUB_SALT_BUFF_OFFSET(image_addr);
Mayank Grover027a9412018-09-04 15:12:05 +05301977 info.images[IMG_BOOT].imgsize = imagesize_actual;
1978 info.images[IMG_BOOT].name = ptn_name;
1979 ++info.num_loaded_images;
1980
1981 /* Pass loaded dtbo image */
1982 if (dtbo_image_buf != NULL) {
Mayank Grover6fd36792018-09-10 13:26:58 +05301983 info.images[IMG_DTBO].image_buffer =
1984 SUB_SALT_BUFF_OFFSET(dtbo_image_buf);
Mayank Grover027a9412018-09-04 15:12:05 +05301985 info.images[IMG_DTBO].imgsize = dtbo_image_sz;
1986 info.images[IMG_DTBO].name = "dtbo";
1987 ++info.num_loaded_images;
1988 }
1989
Mayank Grover9df84c02018-08-30 15:46:35 +05301990 /* Pass loaded vbmeta image */
1991 if (vbmeta_image_buf != NULL) {
1992 info.images[IMG_VBMETA].image_buffer = vbmeta_image_buf;
1993 info.images[IMG_VBMETA].imgsize = vbmeta_image_sz;
1994 info.images[IMG_VBMETA].name = "vbmeta";
1995 ++info.num_loaded_images;
1996 }
1997
Mayank Grovere1ab96c2018-09-04 20:31:31 +05301998 info.header_version = hdr->header_version;
Monika Singh292b3e92018-03-17 22:40:23 +05301999 info.multi_slot_boot = partition_multislot_is_supported();
2000 info.bootreason_alarm = boot_reason_alarm;
2001 info.bootinto_recovery = boot_into_recovery;
2002 status = load_image_and_auth(&info);
2003 if(status)
2004 return -1;
2005
2006 vbcmdline = info.vbcmdline;
Mayank Grover9df84c02018-08-30 15:46:35 +05302007
2008 /* Free the buffer allocated to vbmeta post verification */
Saranya Chidurab4933332018-10-15 17:30:06 +05302009 if (vbmeta_image_buf != NULL) {
2010 free(vbmeta_image_buf);
2011 --info.num_loaded_images;
2012 }
Monika Singh292b3e92018-03-17 22:40:23 +05302013#else
Channagoud Kadabi736c4962015-08-21 11:56:52 -07002014 /* Change the condition a little bit to include the test framework support.
2015 * We would never reach this point if device is in fastboot mode, even if we did
2016 * that means we are in test mode, so execute kernel authentication part for the
2017 * tests */
Parth Dixitcb0c6082015-12-30 15:01:13 +05302018 if((target_use_signed_kernel() && (!device.is_unlocked)) || is_test_mode_enabled())
Matthew Qin49e51fa2015-02-09 10:40:45 +08002019 {
2020 offset = imagesize_actual;
Vijay Kumar Pendoti9c002ad2016-03-09 13:52:45 +05302021 if (check_aboot_addr_range_overlap((uintptr_t)image_addr + offset, page_size))
Matthew Qin49e51fa2015-02-09 10:40:45 +08002022 {
2023 dprintf(CRITICAL, "Signature read buffer address overlaps with aboot addresses.\n");
2024 return -1;
2025 }
2026
2027 /* Read signature */
2028 if(mmc_read(ptn + offset, (void *)(image_addr + offset), page_size))
2029 {
2030 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
2031 return -1;
2032 }
2033
2034 verify_signed_bootimg((uint32_t)image_addr, imagesize_actual);
Channagoud Kadabi736c4962015-08-21 11:56:52 -07002035 /* The purpose of our test is done here */
Parth Dixitcb0c6082015-12-30 15:01:13 +05302036 if(is_test_mode_enabled() && auth_kernel_img)
Channagoud Kadabi736c4962015-08-21 11:56:52 -07002037 return 0;
Matthew Qin49e51fa2015-02-09 10:40:45 +08002038 } else {
2039 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
2040 #ifdef TZ_SAVE_KERNEL_HASH
2041 aboot_save_boot_hash_mmc((uint32_t) image_addr, imagesize_actual);
2042 #endif /* TZ_SAVE_KERNEL_HASH */
Amit Blay4aa292f2015-04-28 21:55:59 +03002043
2044#ifdef MDTP_SUPPORT
2045 {
2046 /* Verify MDTP lock.
2047 * For boot & recovery partitions, MDTP will use boot_verifier APIs,
2048 * since verification was skipped in aboot. The signature is not part of the loaded image.
2049 */
2050 mdtp_ext_partition_verification_t ext_partition;
2051 ext_partition.partition = boot_into_recovery ? MDTP_PARTITION_RECOVERY : MDTP_PARTITION_BOOT;
2052 ext_partition.integrity_state = MDTP_PARTITION_STATE_UNSET;
2053 ext_partition.page_size = page_size;
2054 ext_partition.image_addr = (uint32)image_addr;
2055 ext_partition.image_size = imagesize_actual;
2056 ext_partition.sig_avail = FALSE;
2057 mdtp_fwlock_verify_lock(&ext_partition);
2058 }
2059#endif /* MDTP_SUPPORT */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08002060 }
Monika Singh292b3e92018-03-17 22:40:23 +05302061#endif
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08002062
Sridhar Parasuram00bfedb2015-05-26 14:21:27 -07002063#if VERIFIED_BOOT
taozhang93088bd2016-11-16 15:50:46 +08002064 if((boot_verify_get_state() == ORANGE) && (!boot_into_ffbm))
Reut Zysmanba8a9d52016-02-18 11:44:04 +02002065 {
2066#if FBCON_DISPLAY_MSG
2067 display_bootverify_menu(DISPLAY_MENU_ORANGE);
2068 wait_for_users_action();
2069#else
2070 dprintf(CRITICAL,
2071 "Your device has been unlocked and can't be trusted.\nWait for 5 seconds before proceeding\n");
2072 mdelay(5000);
2073#endif
2074 }
2075#endif
2076
2077#if VERIFIED_BOOT
Monika Singh292b3e92018-03-17 22:40:23 +05302078 if (VB_M == target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +05302079 {
2080 /* set boot and system versions. */
2081 set_os_version((unsigned char *)image_addr);
2082 // send root of trust
2083 if(!send_rot_command((uint32_t)device.is_unlocked))
2084 ASSERT(0);
2085 }
Parth Dixitbc9b6492015-10-18 00:41:38 +05302086#endif
Channagoud Kadabia22144f2013-03-20 11:49:01 -07002087 /*
Matthew Qinbb7923d2015-02-09 10:56:09 +08002088 * Check if the kernel image is a gzip package. If yes, need to decompress it.
2089 * If not, continue booting.
2090 */
2091 if (is_gzip_package((unsigned char *)(image_addr + page_size), hdr->kernel_size))
2092 {
2093 out_addr = (unsigned char *)(image_addr + imagesize_actual + page_size);
2094 out_avai_len = target_get_max_flash_size() - imagesize_actual - page_size;
Mayank Grover027a9412018-09-04 15:12:05 +05302095#if VERIFIED_BOOT_2
2096 if (dtbo_image_sz)
2097 out_avai_len -= DTBO_IMG_BUF;
2098#endif
Matthew Qin0b15b322015-05-19 05:20:54 -04002099 dprintf(INFO, "decompressing kernel image: start\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08002100 rc = decompress((unsigned char *)(image_addr + page_size),
2101 hdr->kernel_size, out_addr, out_avai_len,
2102 &dtb_offset, &out_len);
2103 if (rc)
2104 {
Matthew Qin0b15b322015-05-19 05:20:54 -04002105 dprintf(CRITICAL, "decompressing kernel image failed!!!\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08002106 ASSERT(0);
2107 }
2108
Matthew Qin0b15b322015-05-19 05:20:54 -04002109 dprintf(INFO, "decompressing kernel image: done\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08002110 kptr = (struct kernel64_hdr *)out_addr;
2111 kernel_start_addr = out_addr;
2112 kernel_size = out_len;
2113 } else {
Ameya Thakur10a33452016-06-13 14:24:26 -07002114 dprintf(INFO, "Uncpmpressed kernel in use\n");
2115 if (!strncmp((char*)(image_addr + page_size),
2116 PATCHED_KERNEL_MAGIC,
2117 sizeof(PATCHED_KERNEL_MAGIC) - 1)) {
2118 dprintf(INFO, "Patched kernel detected\n");
2119 kptr = (struct kernel64_hdr *)(image_addr + page_size +
2120 PATCHED_KERNEL_HEADER_SIZE);
2121 //The size of the kernel is stored at start of kernel image + 16
2122 //The dtb would start just after the kernel
2123 dtb_offset = *((uint32_t*)((unsigned char*)
2124 (image_addr + page_size +
2125 sizeof(PATCHED_KERNEL_MAGIC) -
2126 1)));
2127 //The actual kernel starts after the 20 byte header.
2128 kernel_start_addr = (unsigned char*)(image_addr +
2129 page_size + PATCHED_KERNEL_HEADER_SIZE);
2130 kernel_size = hdr->kernel_size;
2131 patched_kernel_hdr_size = PATCHED_KERNEL_HEADER_SIZE;
2132 } else {
2133 dprintf(INFO, "Kernel image not patched..Unable to locate dt offset\n");
2134 kptr = (struct kernel64_hdr *)(image_addr + page_size);
2135 kernel_start_addr = (unsigned char *)(image_addr + page_size);
2136 kernel_size = hdr->kernel_size;
2137 }
Matthew Qinbb7923d2015-02-09 10:56:09 +08002138 }
2139
2140 /*
Channagoud Kadabia22144f2013-03-20 11:49:01 -07002141 * Update the kernel/ramdisk/tags address if the boot image header
2142 * has default values, these default values come from mkbootimg when
2143 * the boot image is flashed using fastboot flash:raw
2144 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08002145 update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr));
Channagoud Kadabia22144f2013-03-20 11:49:01 -07002146
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002147 /* Get virtual addresses since the hdr saves physical addresses. */
2148 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
2149 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
2150 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002151
Matthew Qinbb7923d2015-02-09 10:56:09 +08002152 kernel_size = ROUND_TO_PAGE(kernel_size, page_mask);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002153 /* Check if the addresses in the header are valid. */
Matthew Qinbb7923d2015-02-09 10:56:09 +08002154 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_size) ||
Mayank Grovere559cec2017-10-17 15:12:03 +05302155 check_ddr_addr_range_bound(hdr->kernel_addr, kernel_size) ||
2156 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual) ||
2157 check_ddr_addr_range_bound(hdr->ramdisk_addr, ramdisk_actual))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002158 {
Mayank Grovere559cec2017-10-17 15:12:03 +05302159 dprintf(CRITICAL, "kernel/ramdisk addresses are not valid.\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002160 return -1;
2161 }
2162
2163#ifndef DEVICE_TREE
Mayank Grovere559cec2017-10-17 15:12:03 +05302164 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE) ||
2165 check_ddr_addr_range_bound(hdr->tags_addr, MAX_TAGS_SIZE))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002166 {
Mayank Grovere559cec2017-10-17 15:12:03 +05302167 dprintf(CRITICAL, "Tags addresses are not valid.\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002168 return -1;
2169 }
2170#endif
2171
Matthew Qin49e51fa2015-02-09 10:40:45 +08002172 /* Move kernel, ramdisk and device tree to correct address */
Matthew Qinbb7923d2015-02-09 10:56:09 +08002173 memmove((void*) hdr->kernel_addr, kernel_start_addr, kernel_size);
Matthew Qin49e51fa2015-02-09 10:40:45 +08002174 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002175
Matthew Qin49e51fa2015-02-09 10:40:45 +08002176 #if DEVICE_TREE
Parth Dixit4097b622016-03-15 14:42:27 +05302177 if(dt_size) {
Matthew Qin49e51fa2015-02-09 10:40:45 +08002178 dt_table_offset = ((uint32_t)image_addr + page_size + kernel_actual + ramdisk_actual + second_actual);
2179 table = (struct dt_table*) dt_table_offset;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002180
Matthew Qin49e51fa2015-02-09 10:40:45 +08002181 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
2182 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
2183 return -1;
2184 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002185
P.V. Phani Kumar82916762016-03-09 09:20:19 +05302186 /* Its Error if, dt_hdr_size (table->num_entries * dt_entry size + Dev_Tree Header)
2187 goes beyound hdr->dt_size*/
Parth Dixit4097b622016-03-15 14:42:27 +05302188 if (dt_hdr_size > ROUND_TO_PAGE(dt_size,hdr->page_size)) {
P.V. Phani Kumar82916762016-03-09 09:20:19 +05302189 dprintf(CRITICAL, "ERROR: Invalid Device Tree size \n");
2190 return -1;
2191 }
2192
Matthew Qin49e51fa2015-02-09 10:40:45 +08002193 /* Find index of device tree within device tree table */
2194 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
2195 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
2196 return -1;
2197 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002198
P.V. Phani Kumar3c333202016-03-09 11:54:37 +05302199 if(dt_entry.offset > (UINT_MAX - dt_entry.size)) {
2200 dprintf(CRITICAL, "ERROR: Device tree contents are Invalid\n");
2201 return -1;
2202 }
2203
2204 /* Ensure we are not overshooting dt_size with the dt_entry selected */
Parth Dixit4097b622016-03-15 14:42:27 +05302205 if ((dt_entry.offset + dt_entry.size) > dt_size) {
P.V. Phani Kumar3c333202016-03-09 11:54:37 +05302206 dprintf(CRITICAL, "ERROR: Device tree contents are Invalid\n");
2207 return -1;
2208 }
2209
Matthew Qin271927e2015-03-31 22:07:07 -04002210 if (is_gzip_package((unsigned char *)dt_table_offset + dt_entry.offset, dt_entry.size))
2211 {
2212 unsigned int compressed_size = 0;
2213 out_addr += out_len;
2214 out_avai_len -= out_len;
Matthew Qin0b15b322015-05-19 05:20:54 -04002215 dprintf(INFO, "decompressing dtb: start\n");
Matthew Qin271927e2015-03-31 22:07:07 -04002216 rc = decompress((unsigned char *)dt_table_offset + dt_entry.offset,
2217 dt_entry.size, out_addr, out_avai_len,
2218 &compressed_size, &dtb_size);
2219 if (rc)
2220 {
Matthew Qin0b15b322015-05-19 05:20:54 -04002221 dprintf(CRITICAL, "decompressing dtb failed!!!\n");
Matthew Qin271927e2015-03-31 22:07:07 -04002222 ASSERT(0);
2223 }
2224
Matthew Qin0b15b322015-05-19 05:20:54 -04002225 dprintf(INFO, "decompressing dtb: done\n");
Matthew Qin271927e2015-03-31 22:07:07 -04002226 best_match_dt_addr = out_addr;
2227 } else {
2228 best_match_dt_addr = (unsigned char *)dt_table_offset + dt_entry.offset;
2229 dtb_size = dt_entry.size;
2230 }
2231
Matthew Qin49e51fa2015-02-09 10:40:45 +08002232 /* Validate and Read device device tree in the tags_addr */
Mayank Grovere559cec2017-10-17 15:12:03 +05302233 if (check_aboot_addr_range_overlap(hdr->tags_addr, dtb_size) ||
2234 check_ddr_addr_range_bound(hdr->tags_addr, dtb_size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002235 {
Mayank Grovere559cec2017-10-17 15:12:03 +05302236 dprintf(CRITICAL, "Device tree addresses are not valid\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002237 return -1;
2238 }
Shashank Mittal162244e2011-08-08 19:01:25 -07002239
Matthew Qin271927e2015-03-31 22:07:07 -04002240 memmove((void *)hdr->tags_addr, (char *)best_match_dt_addr, dtb_size);
Matthew Qin49e51fa2015-02-09 10:40:45 +08002241 } else {
2242 /* Validate the tags_addr */
Mayank Grovere559cec2017-10-17 15:12:03 +05302243 if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual) ||
2244 check_ddr_addr_range_bound(hdr->tags_addr, kernel_actual))
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07002245 {
Mayank Grovere559cec2017-10-17 15:12:03 +05302246 dprintf(CRITICAL, "Device tree addresses are not valid.\n");
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07002247 return -1;
2248 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08002249 /*
2250 * If appended dev tree is found, update the atags with
2251 * memory address to the DTB appended location on RAM.
2252 * Else update with the atags address in the kernel header
Mayank Grover1ed3e8b2018-09-19 15:16:50 +05302253 *
2254 * Make sure everything from scratch address is read before next step!
2255 * In case of dtbo, this API is going to read dtbo on scratch.
Matthew Qin49e51fa2015-02-09 10:40:45 +08002256 */
2257 void *dtb;
Parth Dixit1375d9e2019-07-08 20:56:13 +05302258 image_buf = (void*)(image_addr + page_size + patched_kernel_hdr_size);
2259
Nagireddy Annem0d60bb62019-08-16 19:16:00 +05302260#ifdef OSVERSION_IN_BOOTIMAGE
Parth Dixit1375d9e2019-07-08 20:56:13 +05302261 if ( hdr->header_version == BOOT_HEADER_VERSION_TWO) {
2262
2263 image_buf = (void*)(image_addr);
2264 dtb_offset = dtb_image_offset;
2265 dtb_image_size = imagesize_actual;
2266 }
Nagireddy Annem0d60bb62019-08-16 19:16:00 +05302267#endif
Parth Dixit1375d9e2019-07-08 20:56:13 +05302268
2269 dtb = dev_tree_appended(image_buf, dtb_image_size, dtb_offset,
Ameya Thakur10a33452016-06-13 14:24:26 -07002270 (void *)hdr->tags_addr);
Matthew Qin49e51fa2015-02-09 10:40:45 +08002271 if (!dtb) {
2272 dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07002273 return -1;
2274 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07002275 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08002276 #endif
Shashank Mittal23b8f422010-04-16 19:27:21 -07002277
Stanimir Varbanov69ec5462013-07-18 18:17:42 +03002278 if (boot_into_recovery && !device.is_unlocked && !device.is_tampered)
2279 target_load_ssd_keystore();
2280
Shashank Mittal23b8f422010-04-16 19:27:21 -07002281unified_boot:
Shashank Mittal23b8f422010-04-16 19:27:21 -07002282
Dima Zavin77e41f32013-03-06 16:10:43 -08002283 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07002284 (const char *)hdr->cmdline, board_machtype(),
Shashank Mittal23b8f422010-04-16 19:27:21 -07002285 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
2286
2287 return 0;
2288}
2289
Dima Zavin214cc642009-01-26 11:16:21 -08002290int boot_linux_from_flash(void)
2291{
Mayank Grovere1ab96c2018-09-04 20:31:31 +05302292 boot_img_hdr *hdr = (void*) buf;
Dima Zavin214cc642009-01-26 11:16:21 -08002293 struct ptentry *ptn;
2294 struct ptable *ptable;
2295 unsigned offset = 0;
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08002296
Shashank Mittalcd98d472011-08-02 14:29:24 -07002297 unsigned char *image_addr = 0;
2298 unsigned kernel_actual;
2299 unsigned ramdisk_actual;
2300 unsigned imagesize_actual;
vijay kumar8f53e362015-11-24 13:38:11 +05302301 unsigned second_actual = 0;
Shashank Mittalcd98d472011-08-02 14:29:24 -07002302
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07002303#if DEVICE_TREE
Monika Singh292b3e92018-03-17 22:40:23 +05302304 struct dt_table *table = NULL;
Joel Kingaa335dc2013-06-03 16:11:08 -07002305 struct dt_entry dt_entry;
vijay kumar8f53e362015-11-24 13:38:11 +05302306 unsigned dt_table_offset;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08002307 uint32_t dt_actual;
Monika Singh292b3e92018-03-17 22:40:23 +05302308 uint32_t dt_hdr_size = 0;
Vivek Kumar07bd0862018-04-10 14:19:23 +05302309 uint32_t dtb_offset = 0;
vijay kumar8f53e362015-11-24 13:38:11 +05302310 unsigned int dtb_size = 0;
2311 unsigned char *best_match_dt_addr = NULL;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07002312#endif
2313
David Ng183a7422009-12-07 14:55:21 -08002314 if (target_is_emmc_boot()) {
Mayank Grovere1ab96c2018-09-04 20:31:31 +05302315 hdr = (boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
David Ng183a7422009-12-07 14:55:21 -08002316 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
2317 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
2318 return -1;
2319 }
2320 goto continue_boot;
2321 }
2322
Dima Zavin214cc642009-01-26 11:16:21 -08002323 ptable = flash_get_ptable();
2324 if (ptable == NULL) {
2325 dprintf(CRITICAL, "ERROR: Partition table not found\n");
2326 return -1;
2327 }
2328
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08002329 if(!boot_into_recovery)
2330 {
2331 ptn = ptable_find(ptable, "boot");
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07002332
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08002333 if (ptn == NULL) {
2334 dprintf(CRITICAL, "ERROR: No boot partition found\n");
2335 return -1;
2336 }
2337 }
2338 else
2339 {
2340 ptn = ptable_find(ptable, "recovery");
2341 if (ptn == NULL) {
2342 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
2343 return -1;
2344 }
Dima Zavin214cc642009-01-26 11:16:21 -08002345 }
2346
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05302347 /* Read boot.img header from flash */
Shashank Mittaldcc2e352009-11-19 19:11:16 -08002348 if (flash_read(ptn, offset, buf, page_size)) {
Dima Zavin214cc642009-01-26 11:16:21 -08002349 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
2350 return -1;
2351 }
Dima Zavin214cc642009-01-26 11:16:21 -08002352
2353 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07002354 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Dima Zavin214cc642009-01-26 11:16:21 -08002355 return -1;
2356 }
2357
Shashank Mittaldcc2e352009-11-19 19:11:16 -08002358 if (hdr->page_size != page_size) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07002359 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 -08002360 return -1;
2361 }
2362
Kishor PK9e91b592017-05-24 12:14:55 +05302363 image_addr = (unsigned char *)target_get_scratch_address();
2364 memcpy(image_addr, (void *)buf, page_size);
vijay kumar287bb542015-09-29 13:01:52 +05302365
Channagoud Kadabia22144f2013-03-20 11:49:01 -07002366 /*
2367 * Update the kernel/ramdisk/tags address if the boot image header
2368 * has default values, these default values come from mkbootimg when
2369 * the boot image is flashed using fastboot flash:raw
2370 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08002371 update_ker_tags_rdisk_addr(hdr, false);
Channagoud Kadabia22144f2013-03-20 11:49:01 -07002372
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002373 /* Get virtual addresses since the hdr saves physical addresses. */
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002374 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
2375 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
2376 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
2377
2378 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
2379 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Mayank Grover032736f2017-07-14 20:34:51 +05302380 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002381
Kishor PK9e91b592017-05-24 12:14:55 +05302382 /* ensure commandline is terminated */
2383 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
2384
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002385 /* Check if the addresses in the header are valid. */
2386 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
Mayank Grovere559cec2017-10-17 15:12:03 +05302387 check_ddr_addr_range_bound(hdr->kernel_addr, kernel_actual) ||
2388 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual) ||
2389 check_ddr_addr_range_bound(hdr->ramdisk_addr, ramdisk_actual))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002390 {
Mayank Grovere559cec2017-10-17 15:12:03 +05302391 dprintf(CRITICAL, "kernel/ramdisk addresses are not valid.\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002392 return -1;
2393 }
2394
2395#ifndef DEVICE_TREE
Kishor PKd8ddcad2017-07-27 13:53:57 +05302396 if (UINT_MAX < ((uint64_t)kernel_actual + (uint64_t)ramdisk_actual+ (uint64_t)second_actual + page_size)) {
Mayank Grover032736f2017-07-14 20:34:51 +05302397 dprintf(CRITICAL, "Integer overflow detected in bootimage header fields\n");
2398 return -1;
2399 }
Kishor PKd8ddcad2017-07-27 13:53:57 +05302400 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + second_actual);
Mayank Grover032736f2017-07-14 20:34:51 +05302401
Mayank Grovere559cec2017-10-17 15:12:03 +05302402 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE) ||
2403 check_ddr_addr_range_bound(hdr->tags_addr, MAX_TAGS_SIZE))
Vijay Kumar Pendotid3ed20e2016-09-20 00:34:46 +05302404 {
Mayank Grovere559cec2017-10-17 15:12:03 +05302405 dprintf(CRITICAL, "Tags addresses are not valid.\n");
Vijay Kumar Pendotid3ed20e2016-09-20 00:34:46 +05302406 return -1;
2407 }
2408#else
2409
2410#ifndef OSVERSION_IN_BOOTIMAGE
2411 dt_size = hdr->dt_size;
2412#endif
Mayank Grover032736f2017-07-14 20:34:51 +05302413 dt_actual = ROUND_TO_PAGE(dt_size, page_mask);
Kishor PKd8ddcad2017-07-27 13:53:57 +05302414 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 +05302415 dprintf(CRITICAL, "Integer overflow detected in bootimage header fields\n");
2416 return -1;
2417 }
2418
Kishor PKd8ddcad2017-07-27 13:53:57 +05302419 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + second_actual + dt_actual);
Mayank Grover032736f2017-07-14 20:34:51 +05302420
Mayank Grovere559cec2017-10-17 15:12:03 +05302421 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_size) ||
2422 check_ddr_addr_range_bound(hdr->tags_addr, dt_size))
Mayank Grover032736f2017-07-14 20:34:51 +05302423 {
Mayank Grovere559cec2017-10-17 15:12:03 +05302424 dprintf(CRITICAL, "Device tree addresses are not valid.\n");
Mayank Grover032736f2017-07-14 20:34:51 +05302425 return -1;
2426 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002427#endif
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002428
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05302429 /* Read full boot.img from flash */
2430 dprintf(INFO, "Loading (%s) image (%d): start\n",
2431 (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual);
2432 bs_set_timestamp(BS_KERNEL_LOAD_START);
2433
2434 if (UINT_MAX - page_size < imagesize_actual)
2435 {
2436 dprintf(CRITICAL,"Integer overflow detected in bootimage header fields %u %s\n", __LINE__,__func__);
2437 return -1;
2438 }
2439
2440 /*Check the availability of RAM before reading boot image + max signature length from flash*/
2441 if (target_get_max_flash_size() < (imagesize_actual + page_size))
2442 {
2443 dprintf(CRITICAL, "bootimage size is greater than DDR can hold\n");
2444 return -1;
2445 }
2446
2447 offset = page_size;
2448 /* Read image without signature and header */
2449 if (flash_read(ptn, offset, (void *)(image_addr + offset), imagesize_actual - page_size))
2450 {
2451 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
2452 return -1;
2453 }
2454
2455 dprintf(INFO, "Loading (%s) image (%d): done\n",
2456 (!boot_into_recovery ? "boot" : "recovery"), imagesize_actual);
2457 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
2458
Shashank Mittalcd98d472011-08-02 14:29:24 -07002459 /* Authenticate Kernel */
Deepa Dinamani23b60d42013-06-24 18:10:52 -07002460 if(target_use_signed_kernel() && (!device.is_unlocked))
Shashank Mittalcd98d472011-08-02 14:29:24 -07002461 {
Shashank Mittalcd98d472011-08-02 14:29:24 -07002462 offset = imagesize_actual;
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05302463
Shashank Mittalcd98d472011-08-02 14:29:24 -07002464 /* Read signature */
2465 if (flash_read(ptn, offset, (void *)(image_addr + offset), page_size))
2466 {
2467 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002468 return -1;
Shashank Mittalcd98d472011-08-02 14:29:24 -07002469 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07002470
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05302471 verify_signed_bootimg((uint32_t)image_addr, imagesize_actual);
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05302472 }
2473 offset = page_size;
2474 if(hdr->second_size != 0) {
2475 if (UINT_MAX - offset < second_actual)
2476 {
2477 dprintf(CRITICAL, "ERROR: Integer overflow in boot image header %s\t%d\n",__func__,__LINE__);
2478 return -1;
vijay kumar8f53e362015-11-24 13:38:11 +05302479 }
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05302480 offset += second_actual;
2481 /* Second image loading not implemented. */
2482 ASSERT(0);
2483 }
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05302484 /* Move kernel and ramdisk to correct address */
2485 memmove((void*) hdr->kernel_addr, (char*) (image_addr + page_size), hdr->kernel_size);
2486 memmove((void*) hdr->ramdisk_addr, (char*) (image_addr + page_size + kernel_actual), hdr->ramdisk_size);
2487
2488#if DEVICE_TREE
2489 if(dt_size != 0) {
2490
2491 dt_table_offset = ((uint32_t)image_addr + page_size + kernel_actual + ramdisk_actual + second_actual);
2492
2493 table = (struct dt_table*) dt_table_offset;
2494
2495 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
2496 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
2497 return -1;
2498 }
2499
2500 /* Its Error if, dt_hdr_size (table->num_entries * dt_entry size + Dev_Tree Header)
2501 goes beyound hdr->dt_size*/
2502 if (dt_hdr_size > ROUND_TO_PAGE(dt_size,hdr->page_size)) {
2503 dprintf(CRITICAL, "ERROR: Invalid Device Tree size \n");
2504 return -1;
2505 }
2506
2507 /* Find index of device tree within device tree table */
2508 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
2509 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
2510 return -1;
2511 }
2512
2513 /* Validate and Read device device tree in the "tags_add */
2514 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size) ||
2515 check_ddr_addr_range_bound(hdr->tags_addr, dt_entry.size))
2516 {
2517 dprintf(CRITICAL, "Device tree addresses are not valid.\n");
2518 return -1;
2519 }
2520
2521 if(dt_entry.offset > (UINT_MAX - dt_entry.size)) {
2522 dprintf(CRITICAL, "ERROR: Device tree contents are Invalid\n");
2523 return -1;
2524 }
2525
2526 /* Ensure we are not overshooting dt_size with the dt_entry selected */
2527 if ((dt_entry.offset + dt_entry.size) > dt_size) {
2528 dprintf(CRITICAL, "ERROR: Device tree contents are Invalid\n");
2529 return -1;
2530 }
2531
2532 best_match_dt_addr = (unsigned char *)table + dt_entry.offset;
2533 dtb_size = dt_entry.size;
2534 memmove((void *)hdr->tags_addr, (char *)best_match_dt_addr, dtb_size);
Vivek Kumar07bd0862018-04-10 14:19:23 +05302535
2536 } else {
2537 /* Validate the tags_addr */
2538 if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual) ||
2539 check_ddr_addr_range_bound(hdr->tags_addr, kernel_actual))
2540 {
2541 dprintf(CRITICAL, "Device tree addresses are not valid.\n");
2542 return -1;
2543 }
2544 /*
2545 * If appended dev tree is found, update the atags with
2546 * memory address to the DTB appended location on RAM.
2547 * Else update with the atags address in the kernel header
Mayank Grover1ed3e8b2018-09-19 15:16:50 +05302548 *
2549 * Make sure everything from scratch address is read before next step!
2550 * In case of dtbo, this API is going to read dtbo on scratch.
Vivek Kumar07bd0862018-04-10 14:19:23 +05302551 */
2552 void *dtb = NULL;
2553 dtb = dev_tree_appended((void*)(image_addr + page_size ),hdr->kernel_size, dtb_offset, (void *)hdr->tags_addr);
2554 if (!dtb) {
2555 dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
2556 return -1;
2557 }
2558 }
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05302559#endif
2560 if(target_use_signed_kernel() && (!device.is_unlocked))
2561 {
Shashank Mittal162244e2011-08-08 19:01:25 -07002562 /* Make sure everything from scratch address is read before next step!*/
Shashank Mittala0032282011-08-26 14:50:11 -07002563 if(device.is_tampered)
Shashank Mittal162244e2011-08-08 19:01:25 -07002564 {
2565 write_device_info_flash(&device);
2566 }
Channagoud Kadabi5c86fe32012-02-16 10:58:48 +05302567#if USE_PCOM_SECBOOT
2568 set_tamper_flag(device.is_tampered);
2569#endif
Shashank Mittalcd98d472011-08-02 14:29:24 -07002570 }
David Ng183a7422009-12-07 14:55:21 -08002571continue_boot:
Dima Zavin214cc642009-01-26 11:16:21 -08002572
Dima Zavin214cc642009-01-26 11:16:21 -08002573 /* TODO: create/pass atags to kernel */
2574
Ajay Dudanie28a6072011-07-01 13:59:46 -07002575 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07002576 (const char *)hdr->cmdline, board_machtype(),
Dima Zavin214cc642009-01-26 11:16:21 -08002577 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
2578
2579 return 0;
2580}
Brian Swetland9c4c0752009-01-25 16:23:50 -08002581
Shashank Mittal162244e2011-08-08 19:01:25 -07002582void write_device_info_mmc(device_info *dev)
2583{
Shashank Mittal162244e2011-08-08 19:01:25 -07002584 unsigned long long ptn = 0;
2585 unsigned long long size;
2586 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002587 uint8_t lun = 0;
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002588 uint32_t ret = 0;
Mayank Groverddd348d2018-01-23 14:07:09 +05302589 uint32_t device_info_sz = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07002590
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002591 if (devinfo_present)
2592 index = partition_get_index("devinfo");
2593 else
2594 index = partition_get_index("aboot");
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07002595
Shashank Mittal162244e2011-08-08 19:01:25 -07002596 ptn = partition_get_offset(index);
2597 if(ptn == 0)
2598 {
2599 return;
2600 }
2601
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002602 lun = partition_get_lun(index);
2603 mmc_set_lun(lun);
2604
Shashank Mittal162244e2011-08-08 19:01:25 -07002605 size = partition_get_size(index);
2606
Mayank Groverddd348d2018-01-23 14:07:09 +05302607 device_info_sz = ROUND_TO_PAGE(sizeof(struct device_info),
2608 mmc_blocksize_mask);
2609 if (device_info_sz == UINT_MAX)
2610 {
2611 dprintf(CRITICAL, "ERROR: Incorrect blocksize of card\n");
2612 return;
2613 }
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07002614
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002615 if (devinfo_present)
Mayank Groverddd348d2018-01-23 14:07:09 +05302616 ret = mmc_write(ptn, device_info_sz, (void *)info_buf);
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002617 else
Mayank Groverddd348d2018-01-23 14:07:09 +05302618 ret = mmc_write((ptn + size - device_info_sz), device_info_sz, (void *)info_buf);
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002619 if (ret)
Shashank Mittal162244e2011-08-08 19:01:25 -07002620 {
2621 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
Sridhar Parasurame1a97922015-07-27 15:31:22 -07002622 ASSERT(0);
Shashank Mittal162244e2011-08-08 19:01:25 -07002623 }
2624}
2625
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002626void read_device_info_mmc(struct device_info *info)
Shashank Mittal162244e2011-08-08 19:01:25 -07002627{
Shashank Mittal162244e2011-08-08 19:01:25 -07002628 unsigned long long ptn = 0;
2629 unsigned long long size;
2630 int index = INVALID_PTN;
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002631 uint32_t ret = 0;
Mayank Groverddd348d2018-01-23 14:07:09 +05302632 uint32_t device_info_sz = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07002633
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002634 if ((index = partition_get_index("devinfo")) < 0)
2635 {
2636 devinfo_present = false;
2637 index = partition_get_index("aboot");
2638 }
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07002639
Shashank Mittal162244e2011-08-08 19:01:25 -07002640 ptn = partition_get_offset(index);
2641 if(ptn == 0)
2642 {
2643 return;
2644 }
2645
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07002646 mmc_set_lun(partition_get_lun(index));
2647
Shashank Mittal162244e2011-08-08 19:01:25 -07002648 size = partition_get_size(index);
2649
Mayank Groverddd348d2018-01-23 14:07:09 +05302650 device_info_sz = ROUND_TO_PAGE(sizeof(struct device_info),
2651 mmc_blocksize_mask);
2652 if (device_info_sz == UINT_MAX)
2653 {
2654 dprintf(CRITICAL, "ERROR: Incorrect blocksize of card\n");
2655 return;
2656 }
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07002657
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002658 if (devinfo_present)
Mayank Groverddd348d2018-01-23 14:07:09 +05302659 ret = mmc_read(ptn, (void *)info_buf, device_info_sz);
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002660 else
Mayank Groverddd348d2018-01-23 14:07:09 +05302661 ret = mmc_read((ptn + size - device_info_sz), (void *)info_buf, device_info_sz);
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002662 if (ret)
Shashank Mittal162244e2011-08-08 19:01:25 -07002663 {
2664 dprintf(CRITICAL, "ERROR: Cannot read device info\n");
Sridhar Parasurame1a97922015-07-27 15:31:22 -07002665 ASSERT(0);
Shashank Mittal162244e2011-08-08 19:01:25 -07002666 }
Shashank Mittal162244e2011-08-08 19:01:25 -07002667}
2668
2669void write_device_info_flash(device_info *dev)
2670{
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002671 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
Shashank Mittal162244e2011-08-08 19:01:25 -07002672 struct ptentry *ptn;
2673 struct ptable *ptable;
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002674 if(info == NULL)
2675 {
2676 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
2677 ASSERT(0);
2678 }
2679 info_buf = info;
Shashank Mittal162244e2011-08-08 19:01:25 -07002680 ptable = flash_get_ptable();
2681 if (ptable == NULL)
2682 {
2683 dprintf(CRITICAL, "ERROR: Partition table not found\n");
2684 return;
2685 }
2686
2687 ptn = ptable_find(ptable, "devinfo");
2688 if (ptn == NULL)
2689 {
Smita Ghosh670c8b82015-05-07 09:30:03 -07002690 dprintf(CRITICAL, "ERROR: No devinfo partition found\n");
Shashank Mittal162244e2011-08-08 19:01:25 -07002691 return;
2692 }
2693
Mayank Groverdedbc892017-10-24 13:41:34 +05302694 memset(info, 0, BOOT_IMG_MAX_PAGE_SIZE);
Shashank Mittal162244e2011-08-08 19:01:25 -07002695 memcpy(info, dev, sizeof(device_info));
2696
2697 if (flash_write(ptn, 0, (void *)info_buf, page_size))
2698 {
2699 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
2700 return;
2701 }
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002702 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07002703}
2704
vijay kumarc65876c2015-04-24 13:29:16 +05302705static int read_allow_oem_unlock(device_info *dev)
2706{
vijay kumarc65876c2015-04-24 13:29:16 +05302707 unsigned offset;
2708 int index;
2709 unsigned long long ptn;
2710 unsigned long long ptn_size;
2711 unsigned blocksize = mmc_get_device_blocksize();
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07002712 STACKBUF_DMA_ALIGN(buf, blocksize);
vijay kumarc65876c2015-04-24 13:29:16 +05302713
vijay kumarca2e6812015-07-08 20:28:25 +05302714 index = partition_get_index(frp_ptns[0]);
vijay kumarc65876c2015-04-24 13:29:16 +05302715 if (index == INVALID_PTN)
2716 {
vijay kumarca2e6812015-07-08 20:28:25 +05302717 index = partition_get_index(frp_ptns[1]);
2718 if (index == INVALID_PTN)
2719 {
2720 dprintf(CRITICAL, "Neither '%s' nor '%s' partition found\n", frp_ptns[0],frp_ptns[1]);
2721 return -1;
2722 }
vijay kumarc65876c2015-04-24 13:29:16 +05302723 }
2724
2725 ptn = partition_get_offset(index);
2726 ptn_size = partition_get_size(index);
2727 offset = ptn_size - blocksize;
2728
Mayank Grover48860402016-11-29 12:34:53 +05302729 /* Set Lun for partition */
2730 mmc_set_lun(partition_get_lun(index));
2731
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07002732 if (mmc_read(ptn + offset, (void *)buf, blocksize))
vijay kumarc65876c2015-04-24 13:29:16 +05302733 {
2734 dprintf(CRITICAL, "Reading MMC failed\n");
2735 return -1;
2736 }
2737
2738 /*is_allow_unlock is a bool value stored at the LSB of last byte*/
2739 is_allow_unlock = buf[blocksize-1] & 0x01;
2740 return 0;
2741}
2742
2743static int write_allow_oem_unlock(bool allow_unlock)
2744{
vijay kumarc65876c2015-04-24 13:29:16 +05302745 unsigned offset;
vijay kumarc65876c2015-04-24 13:29:16 +05302746 int index;
2747 unsigned long long ptn;
2748 unsigned long long ptn_size;
2749 unsigned blocksize = mmc_get_device_blocksize();
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07002750 STACKBUF_DMA_ALIGN(buf, blocksize);
vijay kumarc65876c2015-04-24 13:29:16 +05302751
vijay kumarca2e6812015-07-08 20:28:25 +05302752 index = partition_get_index(frp_ptns[0]);
vijay kumarc65876c2015-04-24 13:29:16 +05302753 if (index == INVALID_PTN)
2754 {
vijay kumarca2e6812015-07-08 20:28:25 +05302755 index = partition_get_index(frp_ptns[1]);
2756 if (index == INVALID_PTN)
2757 {
2758 dprintf(CRITICAL, "Neither '%s' nor '%s' partition found\n", frp_ptns[0],frp_ptns[1]);
2759 return -1;
2760 }
vijay kumarc65876c2015-04-24 13:29:16 +05302761 }
2762
2763 ptn = partition_get_offset(index);
2764 ptn_size = partition_get_size(index);
2765 offset = ptn_size - blocksize;
Mayank Grover48860402016-11-29 12:34:53 +05302766 mmc_set_lun(partition_get_lun(index));
vijay kumarc65876c2015-04-24 13:29:16 +05302767
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07002768 if (mmc_read(ptn + offset, (void *)buf, blocksize))
vijay kumarc65876c2015-04-24 13:29:16 +05302769 {
2770 dprintf(CRITICAL, "Reading MMC failed\n");
2771 return -1;
2772 }
2773
2774 /*is_allow_unlock is a bool value stored at the LSB of last byte*/
2775 buf[blocksize-1] = allow_unlock;
2776 if (mmc_write(ptn + offset, blocksize, buf))
2777 {
2778 dprintf(CRITICAL, "Writing MMC failed\n");
2779 return -1;
2780 }
2781
2782 return 0;
2783}
2784
Shashank Mittal162244e2011-08-08 19:01:25 -07002785void read_device_info_flash(device_info *dev)
2786{
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002787 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
Shashank Mittal162244e2011-08-08 19:01:25 -07002788 struct ptentry *ptn;
2789 struct ptable *ptable;
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002790 if(info == NULL)
2791 {
2792 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
2793 ASSERT(0);
2794 }
2795 info_buf = info;
Shashank Mittal162244e2011-08-08 19:01:25 -07002796 ptable = flash_get_ptable();
2797 if (ptable == NULL)
2798 {
2799 dprintf(CRITICAL, "ERROR: Partition table not found\n");
2800 return;
2801 }
2802
2803 ptn = ptable_find(ptable, "devinfo");
2804 if (ptn == NULL)
2805 {
Smita Ghosh670c8b82015-05-07 09:30:03 -07002806 dprintf(CRITICAL, "ERROR: No devinfo partition found\n");
Shashank Mittal162244e2011-08-08 19:01:25 -07002807 return;
2808 }
2809
2810 if (flash_read(ptn, 0, (void *)info_buf, page_size))
2811 {
2812 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
2813 return;
2814 }
2815
2816 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
2817 {
Shashank Mittal162244e2011-08-08 19:01:25 -07002818 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
2819 info->is_unlocked = 0;
Shashank Mittala0032282011-08-26 14:50:11 -07002820 info->is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07002821 write_device_info_flash(info);
2822 }
2823 memcpy(dev, info, sizeof(device_info));
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002824 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07002825}
2826
2827void write_device_info(device_info *dev)
2828{
2829 if(target_is_emmc_boot())
2830 {
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002831 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
2832 if(info == NULL)
2833 {
2834 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
2835 ASSERT(0);
2836 }
2837 info_buf = info;
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002838 memcpy(info, dev, sizeof(struct device_info));
2839
2840#if USE_RPMB_FOR_DEVINFO
Mayank Grover912eaa62017-10-26 12:08:53 +05302841 if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05302842 is_secure_boot_enable()) {
2843 if((write_device_info_rpmb((void*) info, PAGE_SIZE)) < 0)
2844 ASSERT(0);
Sridhar Parasurame1a97922015-07-27 15:31:22 -07002845 }
Channagoud Kadabic80cb492015-04-28 16:08:28 -07002846 else
2847 write_device_info_mmc(info);
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002848#else
2849 write_device_info_mmc(info);
2850#endif
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002851 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07002852 }
2853 else
2854 {
2855 write_device_info_flash(dev);
2856 }
2857}
2858
Monika Singh94316462018-03-15 18:39:01 +05302859int read_rollback_index(uint32_t loc, uint64_t *roll_back_index)
2860{
2861 if (!devinfo_present) {
2862 dprintf(CRITICAL, "DeviceInfo not initalized \n");
2863 return -EINVAL;
2864 }
2865 if (loc >= ARRAY_SIZE(device.rollback_index)) {
2866 dprintf(CRITICAL, "%s() Loc out of range index: %d, array len: %d\n",
2867 __func__, loc, ARRAY_SIZE(device.rollback_index));
2868 ASSERT(0);
2869 }
2870
2871 *roll_back_index = device.rollback_index[loc];
2872 return 0;
2873}
2874
2875int write_rollback_index(uint32_t loc, uint64_t roll_back_index)
2876{
2877 if (!devinfo_present) {
2878 dprintf(CRITICAL, "DeviceInfo not initalized \n");
2879 return -EINVAL;
2880 }
2881 if (loc >= ARRAY_SIZE(device.rollback_index)) {
2882 dprintf(CRITICAL, "%s() Loc out of range index: %d, array len: %d\n",
2883 __func__, loc, ARRAY_SIZE(device.rollback_index));
2884 ASSERT(0);
2885 }
2886
2887 device.rollback_index[loc] = roll_back_index;
2888 write_device_info(&device);
2889 return 0;
2890}
2891
Monika Singhb0fad822018-03-15 18:50:55 +05302892int store_userkey(uint8_t *user_key, uint32_t user_key_size)
2893{
2894 if (!devinfo_present) {
2895 dprintf(CRITICAL, "DeviceInfo not initalized \n");
2896 return -EINVAL;
2897 }
2898
2899 if (user_key_size > ARRAY_SIZE(device.user_public_key)) {
2900 dprintf(CRITICAL, "StoreUserKey, UserKeySize too large!\n");
2901 return -ENODEV;
2902 }
2903
2904 memcpy(device.user_public_key, user_key, user_key_size);
2905 device.user_public_key_length = user_key_size;
2906 write_device_info(&device);
2907 return 0;
2908}
2909
2910int erase_userkey()
2911{
2912 if (!devinfo_present) {
2913 dprintf(CRITICAL, "DeviceInfo not initalized \n");
2914 return -EINVAL;
2915 }
2916 memset(device.user_public_key, 0, ARRAY_SIZE(device.user_public_key));
2917 device.user_public_key_length = 0;
2918 write_device_info(&device);
2919 return 0;
2920}
2921
2922int get_userkey(uint8_t **user_key, uint32_t *user_key_size)
2923{
2924 if (!devinfo_present) {
2925 dprintf(CRITICAL, "DeviceInfo not initalized \n");
2926 return -EINVAL;
2927 }
2928 *user_key = device.user_public_key;
2929 *user_key_size = device.user_public_key_length;
2930 return 0;
2931}
2932
Shashank Mittal162244e2011-08-08 19:01:25 -07002933void read_device_info(device_info *dev)
2934{
2935 if(target_is_emmc_boot())
2936 {
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002937 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
2938 if(info == NULL)
2939 {
2940 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
2941 ASSERT(0);
2942 }
2943 info_buf = info;
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002944
2945#if USE_RPMB_FOR_DEVINFO
Mayank Grover912eaa62017-10-26 12:08:53 +05302946 if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05302947 is_secure_boot_enable()) {
2948 if((read_device_info_rpmb((void*) info, PAGE_SIZE)) < 0)
2949 ASSERT(0);
Sridhar Parasurame1a97922015-07-27 15:31:22 -07002950 }
Channagoud Kadabic80cb492015-04-28 16:08:28 -07002951 else
2952 read_device_info_mmc(info);
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002953#else
2954 read_device_info_mmc(info);
2955#endif
2956
2957 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
2958 {
2959 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
lijuang511a2b52015-08-14 20:50:51 +08002960 if (is_secure_boot_enable()) {
Channagoud Kadabi05f78ba2015-07-06 11:58:14 -07002961 info->is_unlocked = 0;
Monika Singh292b3e92018-03-17 22:40:23 +05302962#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover912eaa62017-10-26 12:08:53 +05302963 if (VB_M <= target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +05302964 info->is_unlock_critical = 0;
Parth Dixitddbc7352015-10-18 03:13:31 +05302965#endif
lijuang511a2b52015-08-14 20:50:51 +08002966 } else {
Channagoud Kadabi2fda4092015-07-07 13:34:11 -07002967 info->is_unlocked = 1;
Monika Singh292b3e92018-03-17 22:40:23 +05302968#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover912eaa62017-10-26 12:08:53 +05302969 if (VB_M <= target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +05302970 info->is_unlock_critical = 1;
Parth Dixitddbc7352015-10-18 03:13:31 +05302971#endif
lijuang511a2b52015-08-14 20:50:51 +08002972 }
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002973 info->is_tampered = 0;
2974 info->charger_screen_enabled = 0;
Monika Singh292b3e92018-03-17 22:40:23 +05302975#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover912eaa62017-10-26 12:08:53 +05302976 if (VB_M <= target_get_vb_version())
Monika Singh1c71a1c2019-12-03 12:12:48 +05302977 {
Mayank Grover889be1b2017-09-12 20:12:23 +05302978 info->verity_mode = 1; //enforcing by default
Monika Singh1c71a1c2019-12-03 12:12:48 +05302979 info->user_public_key_length = 0;
2980 memset(info->rollback_index, 0, sizeof(info->rollback_index));
2981 memset(info->user_public_key, 0, sizeof(info->user_public_key));
2982 }
Parth Dixitddbc7352015-10-18 03:13:31 +05302983#endif
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002984 write_device_info(info);
2985 }
jessicatseng1b9da342020-04-22 11:13:20 +08002986
2987//<2020/04/22-JessicaTseng, Enable off-charging
2988 info->charger_screen_enabled = 1;
2989 write_device_info(info);
2990//>2020/04/22-JessicaTseng
2991
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002992 memcpy(dev, info, sizeof(device_info));
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002993 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07002994 }
2995 else
2996 {
2997 read_device_info_flash(dev);
2998 }
2999}
3000
3001void reset_device_info()
3002{
3003 dprintf(ALWAYS, "reset_device_info called.");
Shashank Mittala0032282011-08-26 14:50:11 -07003004 device.is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07003005 write_device_info(&device);
3006}
3007
3008void set_device_root()
3009{
3010 dprintf(ALWAYS, "set_device_root called.");
Shashank Mittala0032282011-08-26 14:50:11 -07003011 device.is_tampered = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -07003012 write_device_info(&device);
3013}
3014
lijuang4ece1e72015-08-14 21:02:36 +08003015/* set device unlock value
3016 * Must check FRP before call this function
3017 * Need to wipe data when unlock status changed
3018 * type 0: oem unlock
3019 * type 1: unlock critical
3020 * status 0: unlock as false
3021 * status 1: lock as true
3022 */
3023void set_device_unlock_value(int type, bool status)
lijuang21f12f52015-08-22 16:22:19 +08003024{
lijuang4ece1e72015-08-14 21:02:36 +08003025 if (type == UNLOCK)
3026 device.is_unlocked = status;
Monika Singh292b3e92018-03-17 22:40:23 +05303027#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover912eaa62017-10-26 12:08:53 +05303028 else if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05303029 type == UNLOCK_CRITICAL)
3030 device.is_unlock_critical = status;
Parth Dixitddbc7352015-10-18 03:13:31 +05303031#endif
lijuang4ece1e72015-08-14 21:02:36 +08003032 write_device_info(&device);
3033}
3034
3035static void set_device_unlock(int type, bool status)
3036{
3037 int is_unlocked = -1;
3038 char response[MAX_RSP_SIZE];
3039
3040 /* check device unlock status if it is as expected */
3041 if (type == UNLOCK)
3042 is_unlocked = device.is_unlocked;
Monika Singh292b3e92018-03-17 22:40:23 +05303043#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover912eaa62017-10-26 12:08:53 +05303044 if(VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05303045 type == UNLOCK_CRITICAL)
3046 {
3047 is_unlocked = device.is_unlock_critical;
3048 }
Parth Dixitddbc7352015-10-18 03:13:31 +05303049#endif
lijuang4ece1e72015-08-14 21:02:36 +08003050 if (is_unlocked == status) {
3051 snprintf(response, sizeof(response), "\tDevice already : %s", (status ? "unlocked!" : "locked!"));
3052 fastboot_info(response);
3053 fastboot_okay("");
3054 return;
3055 }
3056
3057 /* status is true, it means to unlock device */
lijuang07c5d6e2018-04-23 18:32:13 +08003058 if (status && !is_allow_unlock) {
3059 fastboot_fail("oem unlock is not allowed");
3060 return;
3061 }
lijuang21f12f52015-08-22 16:22:19 +08003062
ericlin8ef2adf2020-04-29 14:14:24 +08003063 //<2018/01/09-EricLin, To remove display_unlock_menu for quiet mode.
3064 if(target_build_variant_user()){
lijuang4ece1e72015-08-14 21:02:36 +08003065#if FBCON_DISPLAY_MSG
lijuang07c5d6e2018-04-23 18:32:13 +08003066 display_unlock_menu(type, status);
3067 fastboot_okay("");
3068 return;
lijuang4ece1e72015-08-14 21:02:36 +08003069#else
lijuang07c5d6e2018-04-23 18:32:13 +08003070 if (status && type == UNLOCK) {
3071 fastboot_fail("Need wipe userdata. Do 'fastboot oem unlock-go'");
3072 return;
lijuang21f12f52015-08-22 16:22:19 +08003073 }
lijuang07c5d6e2018-04-23 18:32:13 +08003074#endif
ericlin8ef2adf2020-04-29 14:14:24 +08003075 }
3076 //<2018/01/09-EricLin
lijuang4ece1e72015-08-14 21:02:36 +08003077
3078 set_device_unlock_value(type, status);
3079
3080 /* wipe data */
3081 struct recovery_message msg;
Kishor PK60a68212017-05-08 16:55:57 +05303082 memset(&msg, 0, sizeof(msg));
lijuang4ece1e72015-08-14 21:02:36 +08003083 snprintf(msg.recovery, sizeof(msg.recovery), "recovery\n--wipe_data");
3084 write_misc(0, &msg, sizeof(msg));
3085
3086 fastboot_okay("");
ericlin8ef2adf2020-04-29 14:14:24 +08003087 //<2018/10/16-EricLin, To remove reset after oem lock/unlock.
3088 //reboot_device(RECOVERY_MODE);
3089 //<2018/10/16-EricLin
lijuang21f12f52015-08-22 16:22:19 +08003090}
3091
lijuang511a2b52015-08-14 20:50:51 +08003092static bool critical_flash_allowed(const char * entry)
3093{
3094 uint32_t i = 0;
3095 if (entry == NULL)
3096 return false;
3097
3098 for (i = 0; i < ARRAY_SIZE(critical_flash_allowed_ptn); i++) {
3099 if(!strcmp(entry, critical_flash_allowed_ptn[i]))
3100 return true;
3101 }
3102 return false;
Matthew Qin271927e2015-03-31 22:07:07 -04003103}
3104
3105#if DEVICE_TREE
Amol Jadicb524072012-08-09 16:40:18 -07003106int copy_dtb(uint8_t *boot_image_start, unsigned int scratch_offset)
3107{
3108 uint32 dt_image_offset = 0;
Amol Jadicb524072012-08-09 16:40:18 -07003109 uint32_t n;
Monika Singh292b3e92018-03-17 22:40:23 +05303110 struct dt_table *table = NULL;
Amol Jadicb524072012-08-09 16:40:18 -07003111 struct dt_entry dt_entry;
Monika Singh292b3e92018-03-17 22:40:23 +05303112 uint32_t dt_hdr_size = 0;
Amol Jadicb524072012-08-09 16:40:18 -07003113 unsigned int compressed_size = 0;
3114 unsigned int dtb_size = 0;
3115 unsigned int out_avai_len = 0;
3116 unsigned char *out_addr = NULL;
3117 unsigned char *best_match_dt_addr = NULL;
3118 int rc;
3119
Mayank Grovere1ab96c2018-09-04 20:31:31 +05303120 boot_img_hdr *hdr = (boot_img_hdr *) (boot_image_start);
Amol Jadicb524072012-08-09 16:40:18 -07003121
Parth Dixit4097b622016-03-15 14:42:27 +05303122#ifndef OSVERSION_IN_BOOTIMAGE
3123 dt_size = hdr->dt_size;
3124#endif
3125
3126 if(dt_size != 0) {
Amol Jadicb524072012-08-09 16:40:18 -07003127 /* add kernel offset */
3128 dt_image_offset += page_size;
3129 n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
3130 dt_image_offset += n;
3131
3132 /* add ramdisk offset */
3133 n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
3134 dt_image_offset += n;
3135
3136 /* add second offset */
3137 if(hdr->second_size != 0) {
3138 n = ROUND_TO_PAGE(hdr->second_size, page_mask);
3139 dt_image_offset += n;
3140 }
3141
3142 /* offset now point to start of dt.img */
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07003143 table = (struct dt_table*)(boot_image_start + dt_image_offset);
Amol Jadicb524072012-08-09 16:40:18 -07003144
Deepa Dinamani19648b42013-09-05 17:05:55 -07003145 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Amol Jadicb524072012-08-09 16:40:18 -07003146 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
3147 return -1;
3148 }
P.V. Phani Kumar82916762016-03-09 09:20:19 +05303149
3150 /* Its Error if, dt_hdr_size (table->num_entries * dt_entry size + Dev_Tree Header)
3151 goes beyound hdr->dt_size*/
Parth Dixit4097b622016-03-15 14:42:27 +05303152 if (dt_hdr_size > ROUND_TO_PAGE(dt_size,hdr->page_size)) {
P.V. Phani Kumar82916762016-03-09 09:20:19 +05303153 dprintf(CRITICAL, "ERROR: Invalid Device Tree size \n");
3154 return -1;
3155 }
3156
Joel Kingaa335dc2013-06-03 16:11:08 -07003157 /* Find index of device tree within device tree table */
3158 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Amol Jadicb524072012-08-09 16:40:18 -07003159 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
3160 return -1;
3161 }
3162
Matthew Qin271927e2015-03-31 22:07:07 -04003163 best_match_dt_addr = (unsigned char *)boot_image_start + dt_image_offset + dt_entry.offset;
3164 if (is_gzip_package(best_match_dt_addr, dt_entry.size))
3165 {
3166 out_addr = (unsigned char *)target_get_scratch_address() + scratch_offset;
3167 out_avai_len = target_get_max_flash_size() - scratch_offset;
Matthew Qin0b15b322015-05-19 05:20:54 -04003168 dprintf(INFO, "decompressing dtb: start\n");
Matthew Qin271927e2015-03-31 22:07:07 -04003169 rc = decompress(best_match_dt_addr,
3170 dt_entry.size, out_addr, out_avai_len,
3171 &compressed_size, &dtb_size);
3172 if (rc)
3173 {
Matthew Qin0b15b322015-05-19 05:20:54 -04003174 dprintf(CRITICAL, "decompressing dtb failed!!!\n");
Matthew Qin271927e2015-03-31 22:07:07 -04003175 ASSERT(0);
3176 }
3177
Matthew Qin0b15b322015-05-19 05:20:54 -04003178 dprintf(INFO, "decompressing dtb: done\n");
Matthew Qin271927e2015-03-31 22:07:07 -04003179 best_match_dt_addr = out_addr;
3180 } else {
3181 dtb_size = dt_entry.size;
3182 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07003183 /* Validate and Read device device tree in the "tags_add */
Mayank Grovere559cec2017-10-17 15:12:03 +05303184 if (check_aboot_addr_range_overlap(hdr->tags_addr, dtb_size) ||
3185 check_ddr_addr_range_bound(hdr->tags_addr, dtb_size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07003186 {
Mayank Grovere559cec2017-10-17 15:12:03 +05303187 dprintf(CRITICAL, "Device tree addresses are not valid.\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07003188 return -1;
3189 }
3190
Amol Jadicb524072012-08-09 16:40:18 -07003191 /* Read device device tree in the "tags_add */
Matthew Qin271927e2015-03-31 22:07:07 -04003192 memmove((void*) hdr->tags_addr, (void *)best_match_dt_addr, dtb_size);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07003193 } else
3194 return -1;
Amol Jadicb524072012-08-09 16:40:18 -07003195
3196 /* Everything looks fine. Return success. */
3197 return 0;
3198}
3199#endif
3200
Brian Swetland9c4c0752009-01-25 16:23:50 -08003201void cmd_boot(const char *arg, void *data, unsigned sz)
3202{
3203 unsigned kernel_actual;
3204 unsigned ramdisk_actual;
Kishor PKd8ddcad2017-07-27 13:53:57 +05303205 unsigned second_actual;
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07003206 uint32_t image_actual;
3207 uint32_t dt_actual = 0;
Mayank Grovere1ab96c2018-09-04 20:31:31 +05303208 boot_img_hdr *hdr = NULL;
Matthew Qinbb7923d2015-02-09 10:56:09 +08003209 struct kernel64_hdr *kptr = NULL;
Brian Swetland9c4c0752009-01-25 16:23:50 -08003210 char *ptr = ((char*) data);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07003211 int ret = 0;
3212 uint8_t dtb_copied = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08003213 unsigned int out_len = 0;
3214 unsigned int out_avai_len = 0;
3215 unsigned char *out_addr = NULL;
3216 uint32_t dtb_offset = 0;
3217 unsigned char *kernel_start_addr = NULL;
3218 unsigned int kernel_size = 0;
Matthew Qin271927e2015-03-31 22:07:07 -04003219 unsigned int scratch_offset = 0;
Saranya Chidurab4933332018-10-15 17:30:06 +05303220#if VERIFIED_BOOT_2
3221 void *dtbo_image_buf = NULL;
3222 uint32_t dtbo_image_sz = 0;
3223 void *vbmeta_image_buf = NULL;
3224 uint32_t vbmeta_image_sz = 0;
3225#endif
Monika Singh292b3e92018-03-17 22:40:23 +05303226#if !VERIFIED_BOOT_2
3227 uint32_t sig_actual = 0;
3228 uint32_t sig_size = 0;
3229#ifdef MDTP_SUPPORT
3230 static bool is_mdtp_activated = 0;
3231#endif /* MDTP_SUPPORT */
3232#endif
Matthew Qinbb7923d2015-02-09 10:56:09 +08003233
lijuang2008ff22016-03-07 17:56:27 +08003234#if FBCON_DISPLAY_MSG
3235 /* Exit keys' detection thread firstly */
3236 exit_menu_keys_detection();
3237#endif
3238
Monika Singh292b3e92018-03-17 22:40:23 +05303239#if VERIFIED_BOOT || VERIFIED_BOOT_2
Channagoud Kadabi6d5375e2015-06-23 17:15:42 -07003240 if(target_build_variant_user() && !device.is_unlocked)
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003241 {
3242 fastboot_fail("unlock device to use this command");
lijuang2008ff22016-03-07 17:56:27 +08003243 goto boot_failed;
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003244 }
3245#endif
3246
Brian Swetland9c4c0752009-01-25 16:23:50 -08003247 if (sz < sizeof(hdr)) {
3248 fastboot_fail("invalid bootimage header");
lijuang2008ff22016-03-07 17:56:27 +08003249 goto boot_failed;
Brian Swetland9c4c0752009-01-25 16:23:50 -08003250 }
3251
Mayank Grovere1ab96c2018-09-04 20:31:31 +05303252 hdr = (boot_img_hdr *)data;
Brian Swetland9c4c0752009-01-25 16:23:50 -08003253
3254 /* ensure commandline is terminated */
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07003255 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -08003256
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07003257 if(target_is_emmc_boot() && hdr->page_size) {
3258 page_size = hdr->page_size;
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07003259 page_mask = page_size - 1;
3260 }
3261
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07003262 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
3263 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Kishor PKd8ddcad2017-07-27 13:53:57 +05303264 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07003265#if DEVICE_TREE
Parth Dixit4097b622016-03-15 14:42:27 +05303266#ifndef OSVERSION_IN_BOOTIMAGE
3267 dt_size = hdr->dt_size;
3268#endif
3269 dt_actual = ROUND_TO_PAGE(dt_size, page_mask);
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07003270#endif
3271
3272 image_actual = ADD_OF(page_size, kernel_actual);
3273 image_actual = ADD_OF(image_actual, ramdisk_actual);
Kishor PKd8ddcad2017-07-27 13:53:57 +05303274 image_actual = ADD_OF(image_actual, second_actual);
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07003275 image_actual = ADD_OF(image_actual, dt_actual);
3276
Kishor PK5134b332017-05-09 17:50:08 +05303277 /* Checking to prevent oob access in read_der_message_length */
3278 if (image_actual > sz) {
3279 fastboot_fail("bootimage header fields are invalid");
3280 goto boot_failed;
3281 }
Saranya Chidurab4933332018-10-15 17:30:06 +05303282
Monika Singh292b3e92018-03-17 22:40:23 +05303283#if VERIFIED_BOOT_2
Mayank Grover1ceaee22019-04-01 17:54:32 +05303284 /* Pass size of boot partition, as imgsize, to avoid
3285 read fewer bytes error */
3286 image_actual = partition_get_size(partition_get_index("boot"));
Saranya Chidurab4933332018-10-15 17:30:06 +05303287
3288 /* load and validate dtbo partition */
3289 load_validate_dtbo_image(&dtbo_image_buf, &dtbo_image_sz);
3290
3291 /* load vbmeta partition */
3292 load_vbmeta_image(&vbmeta_image_buf, &vbmeta_image_sz);
3293
Monika Singh292b3e92018-03-17 22:40:23 +05303294 memset(&info, 0, sizeof(bootinfo));
Saranya Chidurab4933332018-10-15 17:30:06 +05303295
3296 info.images[IMG_BOOT].image_buffer = SUB_SALT_BUFF_OFFSET(data);
3297 info.images[IMG_BOOT].imgsize = image_actual;
3298 info.images[IMG_BOOT].name = "boot";
3299 ++info.num_loaded_images;
3300
3301 /* Pass loaded dtbo image */
3302 if (dtbo_image_buf != NULL) {
3303 info.images[IMG_DTBO].image_buffer = SUB_SALT_BUFF_OFFSET(dtbo_image_buf);
3304 info.images[IMG_DTBO].imgsize = dtbo_image_sz;
3305 info.images[IMG_DTBO].name = "dtbo";
3306 ++info.num_loaded_images;
3307 }
3308
3309 /* Pass loaded vbmeta image */
3310 if (vbmeta_image_buf != NULL) {
3311 info.images[IMG_VBMETA].image_buffer = vbmeta_image_buf;
3312 info.images[IMG_VBMETA].imgsize = vbmeta_image_sz;
3313 info.images[IMG_VBMETA].name = "vbmeta";
3314 ++info.num_loaded_images;
3315 }
3316
Monika Singh292b3e92018-03-17 22:40:23 +05303317 info.multi_slot_boot = partition_multislot_is_supported();
3318 if (load_image_and_auth(&info))
3319 goto boot_failed;
3320 vbcmdline = info.vbcmdline;
Saranya Chidurab4933332018-10-15 17:30:06 +05303321
3322 /* Free the buffer allocated to vbmeta post verification */
3323 if (vbmeta_image_buf != NULL) {
3324 free(vbmeta_image_buf);
3325 --info.num_loaded_images;
3326 }
Monika Singh292b3e92018-03-17 22:40:23 +05303327#else
Kishor PK5134b332017-05-09 17:50:08 +05303328 sig_size = sz - image_actual;
3329
P.V. Phani Kumareecfd822016-03-09 20:09:03 +05303330 if (target_use_signed_kernel() && (!device.is_unlocked)) {
Monika Singh292b3e92018-03-17 22:40:23 +05303331 unsigned chk;
P.V. Phani Kumareecfd822016-03-09 20:09:03 +05303332 /* Calculate the signature length from boot image */
3333 sig_actual = read_der_message_length(
Kishor PK5134b332017-05-09 17:50:08 +05303334 (unsigned char*)(data + image_actual), sig_size);
Monika Singh292b3e92018-03-17 22:40:23 +05303335 chk = ADD_OF(image_actual, sig_actual);
Channagoud Kadabi3d839012014-06-26 14:26:39 -07003336
Monika Singh292b3e92018-03-17 22:40:23 +05303337 if (chk > sz) {
Kishor PK5134b332017-05-09 17:50:08 +05303338 fastboot_fail("bootimage header fields are invalid");
3339 goto boot_failed;
3340 }
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07003341 }
3342
Sridhar Parasuram361e4cd2015-09-24 11:19:19 -07003343 // Initialize boot state before trying to verify boot.img
3344#if VERIFIED_BOOT
Channagoud Kadabi699466e2015-11-03 12:37:42 -08003345 boot_verifier_init();
Mayank Groverb337e932017-01-18 20:00:40 +05303346#endif
Channagoud Kadabibf6ce7d2015-09-17 13:25:35 -07003347 /* Handle overflow if the input image size is greater than
3348 * boot image buffer can hold
3349 */
Monika Singh292b3e92018-03-17 22:40:23 +05303350 if ((target_get_max_flash_size() - page_size) < image_actual)
Channagoud Kadabibf6ce7d2015-09-17 13:25:35 -07003351 {
3352 fastboot_fail("booimage: size is greater than boot image buffer can hold");
lijuang2008ff22016-03-07 17:56:27 +08003353 goto boot_failed;
Channagoud Kadabibf6ce7d2015-09-17 13:25:35 -07003354 }
Sridhar Parasuram361e4cd2015-09-24 11:19:19 -07003355
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07003356 /* Verify the boot image
3357 * device & page_size are initialized in aboot_init
3358 */
Sridhar Parasuram7ae9fbc2015-09-17 09:56:30 -07003359 if (target_use_signed_kernel() && (!device.is_unlocked)) {
Channagoud Kadabi3d839012014-06-26 14:26:39 -07003360 /* Pass size excluding signature size, otherwise we would try to
3361 * access signature beyond its length
3362 */
Monika Singh292b3e92018-03-17 22:40:23 +05303363 verify_signed_bootimg((uint32_t)data, image_actual);
Sridhar Parasuram7ae9fbc2015-09-17 09:56:30 -07003364 }
Amit Blay4aa292f2015-04-28 21:55:59 +03003365#ifdef MDTP_SUPPORT
3366 else
3367 {
Shay Nachmani062aa3f2015-05-17 17:28:44 +03003368 /* fastboot boot is not allowed when MDTP is activated */
Amit Blay4aa292f2015-04-28 21:55:59 +03003369 mdtp_ext_partition_verification_t ext_partition;
Amit Blay8a510302015-08-17 09:20:01 +03003370
3371 if (!is_mdtp_activated) {
3372 ext_partition.partition = MDTP_PARTITION_NONE;
3373 mdtp_fwlock_verify_lock(&ext_partition);
3374 }
Amit Blay4aa292f2015-04-28 21:55:59 +03003375 }
Shay Nachmani062aa3f2015-05-17 17:28:44 +03003376
Amir Kotzer7c768c02016-04-13 09:08:05 +03003377 /* If mdtp state cannot be validate, block fastboot boot*/
3378 if(mdtp_activated(&is_mdtp_activated)){
3379 dprintf(CRITICAL, "mdtp_activated cannot validate state.\n");
3380 dprintf(CRITICAL, "Can not proceed with fastboot boot command.\n");
3381 goto boot_failed;
3382 }
Shay Nachmani062aa3f2015-05-17 17:28:44 +03003383 if(is_mdtp_activated){
3384 dprintf(CRITICAL, "fastboot boot command is not available.\n");
lijuang2008ff22016-03-07 17:56:27 +08003385 goto boot_failed;
Shay Nachmani062aa3f2015-05-17 17:28:44 +03003386 }
Amit Blay4aa292f2015-04-28 21:55:59 +03003387#endif /* MDTP_SUPPORT */
Monika Singh292b3e92018-03-17 22:40:23 +05303388#endif /* VERIFIED_BOOT_2 else */
Amit Blay4aa292f2015-04-28 21:55:59 +03003389
Sridhar Parasuram361e4cd2015-09-24 11:19:19 -07003390#if VERIFIED_BOOT
Monika Singh292b3e92018-03-17 22:40:23 +05303391 if (VB_M == target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +05303392 {
3393 /* set boot and system versions. */
3394 set_os_version((unsigned char *)data);
3395 // send root of trust
3396 if(!send_rot_command((uint32_t)device.is_unlocked))
3397 ASSERT(0);
3398 }
Parth Dixitbc9b6492015-10-18 00:41:38 +05303399#endif
Channagoud Kadabia22144f2013-03-20 11:49:01 -07003400 /*
Matthew Qinbb7923d2015-02-09 10:56:09 +08003401 * Check if the kernel image is a gzip package. If yes, need to decompress it.
3402 * If not, continue booting.
3403 */
3404 if (is_gzip_package((unsigned char *)(data + page_size), hdr->kernel_size))
3405 {
3406 out_addr = (unsigned char *)target_get_scratch_address();
3407 out_addr = (unsigned char *)(out_addr + image_actual + page_size);
3408 out_avai_len = target_get_max_flash_size() - image_actual - page_size;
Saranya Chidurab4933332018-10-15 17:30:06 +05303409#if VERIFIED_BOOT_2
3410 if (dtbo_image_sz)
3411 out_avai_len -= DTBO_IMG_BUF;
3412#endif
Matthew Qin0b15b322015-05-19 05:20:54 -04003413 dprintf(INFO, "decompressing kernel image: start\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08003414 ret = decompress((unsigned char *)(ptr + page_size),
3415 hdr->kernel_size, out_addr, out_avai_len,
3416 &dtb_offset, &out_len);
3417 if (ret)
3418 {
Matthew Qin0b15b322015-05-19 05:20:54 -04003419 dprintf(CRITICAL, "decompressing image failed!!!\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08003420 ASSERT(0);
3421 }
3422
Matthew Qin0b15b322015-05-19 05:20:54 -04003423 dprintf(INFO, "decompressing kernel image: done\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08003424 kptr = (struct kernel64_hdr *)out_addr;
3425 kernel_start_addr = out_addr;
3426 kernel_size = out_len;
3427 } else {
3428 kptr = (struct kernel64_hdr*)((char *)data + page_size);
3429 kernel_start_addr = (unsigned char *)((char *)data + page_size);
3430 kernel_size = hdr->kernel_size;
3431 }
3432
3433 /*
Channagoud Kadabia22144f2013-03-20 11:49:01 -07003434 * Update the kernel/ramdisk/tags address if the boot image header
3435 * has default values, these default values come from mkbootimg when
3436 * the boot image is flashed using fastboot flash:raw
3437 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08003438 update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr));
Dima Zavin3cadfff2013-03-21 14:30:48 -07003439
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07003440 /* Get virtual addresses since the hdr saves physical addresses. */
3441 hdr->kernel_addr = VA(hdr->kernel_addr);
3442 hdr->ramdisk_addr = VA(hdr->ramdisk_addr);
3443 hdr->tags_addr = VA(hdr->tags_addr);
Brian Swetland9c4c0752009-01-25 16:23:50 -08003444
Matthew Qinbb7923d2015-02-09 10:56:09 +08003445 kernel_size = ROUND_TO_PAGE(kernel_size, page_mask);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07003446 /* Check if the addresses in the header are valid. */
Matthew Qinbb7923d2015-02-09 10:56:09 +08003447 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_size) ||
Mayank Grovere559cec2017-10-17 15:12:03 +05303448 check_ddr_addr_range_bound(hdr->kernel_addr, kernel_size) ||
3449 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual) ||
3450 check_ddr_addr_range_bound(hdr->ramdisk_addr, ramdisk_actual))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07003451 {
Mayank Grovere559cec2017-10-17 15:12:03 +05303452 dprintf(CRITICAL, "kernel/ramdisk addresses are not valid.\n");
lijuang2008ff22016-03-07 17:56:27 +08003453 goto boot_failed;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07003454 }
3455
Amol Jadicb524072012-08-09 16:40:18 -07003456#if DEVICE_TREE
Matthew Qin271927e2015-03-31 22:07:07 -04003457 scratch_offset = image_actual + page_size + out_len;
Amol Jadicb524072012-08-09 16:40:18 -07003458 /* find correct dtb and copy it to right location */
Matthew Qin271927e2015-03-31 22:07:07 -04003459 ret = copy_dtb(data, scratch_offset);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07003460
3461 dtb_copied = !ret ? 1 : 0;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07003462#else
Mayank Grovere559cec2017-10-17 15:12:03 +05303463 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE) ||
3464 check_ddr_addr_range_bound(hdr->tags_addr, MAX_TAGS_SIZE))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07003465 {
Mayank Grovere559cec2017-10-17 15:12:03 +05303466 dprintf(CRITICAL, "Tags addresses are not valid.\n");
lijuang2008ff22016-03-07 17:56:27 +08003467 goto boot_failed;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07003468 }
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07003469#endif
3470
3471 /* Load ramdisk & kernel */
3472 memmove((void*) hdr->ramdisk_addr, ptr + page_size + kernel_actual, hdr->ramdisk_size);
Matthew Qinbb7923d2015-02-09 10:56:09 +08003473 memmove((void*) hdr->kernel_addr, (char*) (kernel_start_addr), kernel_size);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07003474
3475#if DEVICE_TREE
Mayank Grovere559cec2017-10-17 15:12:03 +05303476 if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual) ||
3477 check_ddr_addr_range_bound(hdr->tags_addr, kernel_actual))
Matthew Qinbb7923d2015-02-09 10:56:09 +08003478 {
Mayank Grovere559cec2017-10-17 15:12:03 +05303479 dprintf(CRITICAL, "Tags addresses are not valid.\n");
lijuang2008ff22016-03-07 17:56:27 +08003480 goto boot_failed;
Matthew Qinbb7923d2015-02-09 10:56:09 +08003481 }
3482
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07003483 /*
3484 * If dtb is not found look for appended DTB in the kernel.
3485 * If appended dev tree is found, update the atags with
3486 * memory address to the DTB appended location on RAM.
3487 * Else update with the atags address in the kernel header
3488 */
3489 if (!dtb_copied) {
3490 void *dtb;
Matthew Qinbb7923d2015-02-09 10:56:09 +08003491 dtb = dev_tree_appended((void*)(ptr + page_size),
3492 hdr->kernel_size, dtb_offset,
Dima Zavine63e5572013-05-03 12:23:06 -07003493 (void *)hdr->tags_addr);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07003494 if (!dtb) {
3495 fastboot_fail("dtb not found");
lijuang2008ff22016-03-07 17:56:27 +08003496 goto boot_failed;
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07003497 }
Amol Jadicb524072012-08-09 16:40:18 -07003498 }
3499#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08003500
3501 fastboot_okay("");
Amol Jadi7c4316c2013-10-07 14:19:26 -07003502 fastboot_stop();
Brian Swetland9c4c0752009-01-25 16:23:50 -08003503
Dima Zavin77e41f32013-03-06 16:10:43 -08003504 boot_linux((void*) hdr->kernel_addr, (void*) hdr->tags_addr,
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07003505 (const char*) hdr->cmdline, board_machtype(),
3506 (void*) hdr->ramdisk_addr, hdr->ramdisk_size);
lijuang2008ff22016-03-07 17:56:27 +08003507
3508 /* fastboot already stop, it's no need to show fastboot menu */
3509 return;
3510boot_failed:
3511#if FBCON_DISPLAY_MSG
3512 /* revert to fastboot menu if boot failed */
3513 display_fastboot_menu();
3514#endif
3515 return;
Brian Swetland9c4c0752009-01-25 16:23:50 -08003516}
3517
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003518void cmd_erase_nand(const char *arg, void *data, unsigned sz)
Dima Zavin214cc642009-01-26 11:16:21 -08003519{
3520 struct ptentry *ptn;
3521 struct ptable *ptable;
3522
3523 ptable = flash_get_ptable();
3524 if (ptable == NULL) {
3525 fastboot_fail("partition table doesn't exist");
3526 return;
3527 }
3528
3529 ptn = ptable_find(ptable, arg);
3530 if (ptn == NULL) {
3531 fastboot_fail("unknown partition name");
3532 return;
3533 }
3534
Monika Singh292b3e92018-03-17 22:40:23 +05303535 if (!strncmp(arg, "avb_custom_key", strlen("avb_custom_key"))) {
3536 dprintf(INFO, "erasing avb_custom_key\n");
3537 if (erase_userkey()) {
3538 fastboot_fail("Erasing avb_custom_key failed");
3539 } else {
3540 fastboot_okay("");
3541 }
3542 return;
3543 }
3544
Dima Zavin214cc642009-01-26 11:16:21 -08003545 if (flash_erase(ptn)) {
3546 fastboot_fail("failed to erase partition");
3547 return;
3548 }
3549 fastboot_okay("");
3550}
3551
Bikas Gurungd48bd242010-09-04 19:54:32 -07003552
3553void cmd_erase_mmc(const char *arg, void *data, unsigned sz)
3554{
3555 unsigned long long ptn = 0;
Oliver Wangcee448d2013-10-22 18:40:13 +08003556 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07003557 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003558 uint8_t lun = 0;
Vijay Kumar Pendoti0b21f462016-05-02 17:09:18 +05303559 char *footer = NULL;
Bikas Gurungd48bd242010-09-04 19:54:32 -07003560
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003561#if VERIFIED_BOOT
3562 if(!strcmp(arg, KEYSTORE_PTN_NAME))
3563 {
3564 if(!device.is_unlocked)
3565 {
3566 fastboot_fail("unlock device to erase keystore");
3567 return;
3568 }
3569 }
3570#endif
3571
Kinson Chikf1a43512011-07-14 11:28:39 -07003572 index = partition_get_index(arg);
3573 ptn = partition_get_offset(index);
Oliver Wangcee448d2013-10-22 18:40:13 +08003574 size = partition_get_size(index);
Neeti Desaica8c9602011-10-06 11:40:00 -07003575
Monika Singhc4778b72018-05-16 11:16:42 +05303576 if (!strncmp(arg, "avb_custom_key", strlen("avb_custom_key"))) {
3577 dprintf(INFO, "erasing avb_custom_key\n");
3578 if (erase_userkey()) {
3579 fastboot_fail("Erasing avb_custom_key failed");
3580 } else {
3581 fastboot_okay("");
3582 }
3583 return;
3584 }
3585
Kinson Chikf1a43512011-07-14 11:28:39 -07003586 if(ptn == 0) {
Neeti Desaica8c9602011-10-06 11:40:00 -07003587 fastboot_fail("Partition table doesn't exist\n");
Bikas Gurungd48bd242010-09-04 19:54:32 -07003588 return;
3589 }
Kun Liang2f1601a2013-08-12 16:29:54 +08003590
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003591 lun = partition_get_lun(index);
3592 mmc_set_lun(lun);
3593
Sridhar Parasuramd7957122015-02-27 11:33:40 -08003594 if (platform_boot_dev_isemmc())
3595 {
3596 if (mmc_erase_card(ptn, size)) {
3597 fastboot_fail("failed to erase partition\n");
3598 return;
3599 }
3600 } else {
3601 BUF_DMA_ALIGN(out, DEFAULT_ERASE_SIZE);
3602 size = partition_get_size(index);
3603 if (size > DEFAULT_ERASE_SIZE)
3604 size = DEFAULT_ERASE_SIZE;
Kun Liang2f1601a2013-08-12 16:29:54 +08003605
Sridhar Parasuramd7957122015-02-27 11:33:40 -08003606 /* Simple inefficient version of erase. Just writing
3607 0 in first several blocks */
3608 if (mmc_write(ptn , size, (unsigned int *)out)) {
3609 fastboot_fail("failed to erase partition");
3610 return;
3611 }
Vijay Kumar Pendoti0b21f462016-05-02 17:09:18 +05303612 /*Erase FDE metadata at the userdata footer*/
3613 if(!(strncmp(arg, "userdata", 8)))
3614 {
3615 footer = memalign(CACHE_LINE, FOOTER_SIZE);
3616 memset((void *)footer, 0, FOOTER_SIZE);
3617
3618 size = partition_get_size(index);
3619
3620 if (mmc_write((ptn + size) - FOOTER_SIZE , FOOTER_SIZE, (unsigned int *)footer)) {
3621 fastboot_fail("failed to erase userdata footer");
3622 free(footer);
3623 return;
3624 }
3625 free(footer);
3626 }
Bikas Gurungd48bd242010-09-04 19:54:32 -07003627 }
Monika Singh292b3e92018-03-17 22:40:23 +05303628#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover912eaa62017-10-26 12:08:53 +05303629 if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05303630 !(strncmp(arg, "userdata", 8)) &&
3631 send_delete_keys_to_tz())
Sridhar Parasuram32b30662015-07-10 13:33:22 -07003632 ASSERT(0);
3633#endif
Bikas Gurungd48bd242010-09-04 19:54:32 -07003634 fastboot_okay("");
3635}
3636
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003637void cmd_erase(const char *arg, void *data, unsigned sz)
3638{
Monika Singh292b3e92018-03-17 22:40:23 +05303639#if VERIFIED_BOOT || VERIFIED_BOOT_2
Channagoud Kadabi35297672015-06-13 11:09:49 -07003640 if (target_build_variant_user())
Channagoud Kadabice3cf422015-04-17 16:02:30 -07003641 {
Sridhar Parasuramc32d07d2015-07-12 10:57:48 -07003642 if(!device.is_unlocked)
Channagoud Kadabice3cf422015-04-17 16:02:30 -07003643 {
Channagoud Kadabi35297672015-06-13 11:09:49 -07003644 fastboot_fail("device is locked. Cannot erase");
Channagoud Kadabice3cf422015-04-17 16:02:30 -07003645 return;
3646 }
3647 }
3648#endif
3649
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003650 if(target_is_emmc_boot())
3651 cmd_erase_mmc(arg, data, sz);
3652 else
3653 cmd_erase_nand(arg, data, sz);
3654}
Bikas Gurungd48bd242010-09-04 19:54:32 -07003655
Mayank Grover11ff9692018-01-11 11:54:49 +05303656/* Get the size from partiton name */
3657static void get_partition_size(const char *arg, char *response)
3658{
3659 uint64_t ptn = 0;
3660 uint64_t size;
3661 int index = INVALID_PTN;
3662
3663 index = partition_get_index(arg);
3664
3665 if (index == INVALID_PTN)
3666 {
3667 dprintf(CRITICAL, "Invalid partition index\n");
3668 return;
3669 }
3670
3671 ptn = partition_get_offset(index);
3672
3673 if(!ptn)
3674 {
3675 dprintf(CRITICAL, "Invalid partition name %s\n", arg);
3676 return;
3677 }
3678
3679 size = partition_get_size(index);
3680
3681 snprintf(response, MAX_RSP_SIZE, "\t 0x%llx", size);
3682 return;
3683}
3684
Mayank Grover52cd10a2018-03-15 12:57:54 +05303685/* Function to check partition type of a partition*/
3686static fs_signature_type
3687check_partition_fs_signature(const char *arg)
3688{
3689 fs_signature_type ret = NO_FS;
3690 int index;
3691 unsigned long long ptn;
Mayank Grover399826a2018-08-27 12:15:15 +05303692 char *buffer = memalign(CACHE_LINE, mmc_blocksize);
3693 uint32_t sb_blk_offset = 0;
3694 char *sb_buffer = buffer;
3695
Mayank Grover52cd10a2018-03-15 12:57:54 +05303696 if (!sb_buffer)
3697 {
3698 dprintf(CRITICAL, "ERROR: Failed to allocate buffer for superblock\n");
3699 goto out;
3700 }
3701
3702 /* Read super block */
3703 if ((index = partition_get_index(arg)) < 0)
3704 {
3705 dprintf(CRITICAL, "ERROR: %s() doesn't exsit\n", arg);
3706 goto out;
3707 }
3708 ptn = partition_get_offset(index);
3709 mmc_set_lun(partition_get_lun(index));
Mayank Grover399826a2018-08-27 12:15:15 +05303710 sb_blk_offset = (FS_SUPERBLOCK_OFFSET/mmc_blocksize);
3711
3712 if(mmc_read(ptn + (sb_blk_offset * mmc_blocksize),
Mayank Grover52cd10a2018-03-15 12:57:54 +05303713 (void *)sb_buffer, mmc_blocksize))
3714 {
3715 dprintf(CRITICAL, "ERROR: Failed to read Superblock\n");
3716 goto out;
3717 }
3718
Mayank Grover399826a2018-08-27 12:15:15 +05303719 if (sb_blk_offset == 0)
3720 sb_buffer += FS_SUPERBLOCK_OFFSET;
3721
3722 if (*((uint16 *)(&sb_buffer[EXT_MAGIC_OFFSET_SB])) == (uint16)EXT_MAGIC)
Mayank Grover52cd10a2018-03-15 12:57:54 +05303723 {
3724 dprintf(SPEW, "%s() Found EXT FS\n", arg);
3725 ret = EXT_FS_SIGNATURE;
3726 }
Mayank Grover399826a2018-08-27 12:15:15 +05303727 else if (*((uint32 *)(&sb_buffer[F2FS_MAGIC_OFFSET_SB])) == F2FS_MAGIC)
Mayank Grover52cd10a2018-03-15 12:57:54 +05303728 {
3729 dprintf(SPEW, "%s() Found F2FS FS\n", arg);
3730 ret = EXT_F2FS_SIGNATURE;
3731 }
3732 else
3733 {
3734 dprintf(SPEW, "%s() Reverting to default 0x%x\n",
3735 arg, *((uint16 *)(&sb_buffer[EXT_MAGIC_OFFSET_SB])));
3736 ret = NO_FS;
3737 }
3738
3739out:
Mayank Grover399826a2018-08-27 12:15:15 +05303740 if(buffer)
3741 free(buffer);
Mayank Grover52cd10a2018-03-15 12:57:54 +05303742 return ret;
3743}
3744
Mayank Groverd38fe012018-03-13 15:33:16 +05303745/* Function to get partition type */
3746static void get_partition_type(const char *arg, char *response)
3747{
3748 uint n = 0;
Mayank Grover52cd10a2018-03-15 12:57:54 +05303749 fs_signature_type fs_signature;
Mayank Groverd38fe012018-03-13 15:33:16 +05303750
3751 if (arg == NULL ||
3752 response == NULL)
3753 {
3754 dprintf(CRITICAL, "Invalid input parameter\n");
3755 return;
3756 }
3757
3758 /* By default copy raw to response */
Mayank Grover0e559042018-04-11 17:49:30 +05303759 strlcpy(response, RAW_STR, MAX_RSP_SIZE);
Mayank Groverd38fe012018-03-13 15:33:16 +05303760
3761 /* Mark partiton type for known paritions only */
3762 for (n=0; n < ARRAY_SIZE(part_type_known); n++)
3763 {
3764 if (!strncmp(part_type_known[n].part_name, arg, strlen(arg)))
3765 {
Mayank Grover52cd10a2018-03-15 12:57:54 +05303766 /* Check partition for FS signature */
3767 fs_signature = check_partition_fs_signature(arg);
3768 switch (fs_signature)
3769 {
3770 case EXT_FS_SIGNATURE:
Mayank Grover0e559042018-04-11 17:49:30 +05303771 strlcpy(response, EXT_STR, MAX_RSP_SIZE);
Mayank Grover52cd10a2018-03-15 12:57:54 +05303772 break;
3773 case EXT_F2FS_SIGNATURE:
Mayank Grover0e559042018-04-11 17:49:30 +05303774 strlcpy(response, F2FS_STR, MAX_RSP_SIZE);
Mayank Grover52cd10a2018-03-15 12:57:54 +05303775 break;
3776 case NO_FS:
Mayank Grover0e559042018-04-11 17:49:30 +05303777 strlcpy(response, part_type_known[n].type_response, MAX_RSP_SIZE);
Mayank Grover52cd10a2018-03-15 12:57:54 +05303778 }
Mayank Groverd38fe012018-03-13 15:33:16 +05303779 }
3780 }
3781 return;
3782}
3783
Mayank Grover11ff9692018-01-11 11:54:49 +05303784/*
3785 * Publish the partition type & size info
3786 * fastboot getvar will publish the required information.
3787 * fastboot getvar partition_size:<partition_name>: partition size in hex
3788 * fastboot getvar partition_type:<partition_name>: partition type (ext/fat)
3789 */
3790static void publish_getvar_partition_info(struct getvar_partition_info *info, uint8_t num_parts)
3791{
Mayank Groverd38fe012018-03-13 15:33:16 +05303792 uint8_t i;
Mayank Grover11ff9692018-01-11 11:54:49 +05303793 static bool published = false;
3794 struct partition_entry *ptn_entry =
3795 partition_get_partition_entries();
3796 memset(info, 0, sizeof(struct getvar_partition_info)* num_parts);
3797
3798 for (i = 0; i < num_parts; i++) {
3799 strlcat(info[i].part_name, (const char *)ptn_entry[i].name, MAX_RSP_SIZE);
3800 strlcat(info[i].getvar_size, "partition-size:", MAX_GET_VAR_NAME_SIZE);
3801 strlcat(info[i].getvar_type, "partition-type:", MAX_GET_VAR_NAME_SIZE);
3802
Mayank Groverd38fe012018-03-13 15:33:16 +05303803 get_partition_type(info[i].part_name, info[i].type_response);
Mayank Grover11ff9692018-01-11 11:54:49 +05303804 get_partition_size(info[i].part_name, info[i].size_response);
Mayank Grover11ff9692018-01-11 11:54:49 +05303805 if (strlcat(info[i].getvar_size, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
3806 {
3807 dprintf(CRITICAL, "partition size name truncated\n");
3808 return;
3809 }
3810 if (strlcat(info[i].getvar_type, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
3811 {
3812 dprintf(CRITICAL, "partition type name truncated\n");
3813 return;
3814 }
3815
3816 if (!published)
3817 {
3818 /* publish partition size & type info */
3819 fastboot_publish((const char *) info[i].getvar_size, (const char *) info[i].size_response);
3820 fastboot_publish((const char *) info[i].getvar_type, (const char *) info[i].type_response);
3821 }
3822 }
3823 if (!published)
3824 published = true;
3825}
3826
3827
Ajay Dudani5c761132011-04-07 20:19:04 -07003828void cmd_flash_mmc_img(const char *arg, void *data, unsigned sz)
Shashank Mittal23b8f422010-04-16 19:27:21 -07003829{
3830 unsigned long long ptn = 0;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07003831 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07003832 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003833 char *token = NULL;
3834 char *pname = NULL;
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07003835 char *sp;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003836 uint8_t lun = 0;
3837 bool lun_set = false;
Mayank Grover351a75e2017-05-30 20:06:08 +05303838 int current_active_slot = INVALID;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07003839
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07003840 token = strtok_r((char *)arg, ":", &sp);
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003841 pname = token;
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07003842 token = strtok_r(NULL, ":", &sp);
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003843 if(token)
3844 {
3845 lun = atoi(token);
3846 mmc_set_lun(lun);
3847 lun_set = true;
3848 }
3849
Mao Jinlong226f33a2014-07-04 17:24:10 +08003850 if (pname)
Greg Grisco6e754772011-06-23 12:19:39 -07003851 {
Channagoud Kadabiad259832015-05-29 11:14:17 -07003852 if (!strncmp(pname, "frp-unlock", strlen("frp-unlock")))
3853 {
3854 if (!aboot_frp_unlock(pname, data, sz))
3855 {
3856 fastboot_info("FRP unlock successful");
3857 fastboot_okay("");
3858 }
3859 else
3860 fastboot_fail("Secret key is invalid, please update the bootloader with secret key");
3861
3862 return;
3863 }
3864
Mao Jinlong226f33a2014-07-04 17:24:10 +08003865 if (!strcmp(pname, "partition"))
3866 {
3867 dprintf(INFO, "Attempt to write partition image.\n");
3868 if (write_partition(sz, (unsigned char *) data)) {
3869 fastboot_fail("failed to write partition");
Greg Grisco6e754772011-06-23 12:19:39 -07003870 return;
3871 }
Mayank Grover11ff9692018-01-11 11:54:49 +05303872 /* Re-publish partition table */
3873 publish_getvar_partition_info(part_info, partition_get_partition_count());
Mayank Grover351a75e2017-05-30 20:06:08 +05303874
3875 /* Rescan partition table to ensure we have multislot support*/
3876 if (partition_scan_for_multislot())
3877 {
3878 current_active_slot = partition_find_active_slot();
3879 dprintf(INFO, "Multislot supported: Slot %s active",
3880 (SUFFIX_SLOT(current_active_slot)));
3881 }
3882 partition_mark_active_slot(current_active_slot);
Greg Grisco6e754772011-06-23 12:19:39 -07003883 }
Mao Jinlong226f33a2014-07-04 17:24:10 +08003884 else
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003885 {
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003886#if VERIFIED_BOOT
3887 if(!strcmp(pname, KEYSTORE_PTN_NAME))
3888 {
3889 if(!device.is_unlocked)
3890 {
3891 fastboot_fail("unlock device to flash keystore");
3892 return;
3893 }
Gaurav Nebhwanic7313cc2015-12-15 22:25:04 +05303894 if(!boot_verify_validate_keystore((unsigned char *)data,sz))
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003895 {
3896 fastboot_fail("image is not a keystore file");
3897 return;
3898 }
3899 }
3900#endif
Mao Jinlong226f33a2014-07-04 17:24:10 +08003901 index = partition_get_index(pname);
3902 ptn = partition_get_offset(index);
3903 if(ptn == 0) {
3904 fastboot_fail("partition table doesn't exist");
3905 return;
3906 }
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003907
Mayank Grover351a75e2017-05-30 20:06:08 +05303908 if (!strncmp(pname, "boot", strlen("boot"))
3909 || !strcmp(pname, "recovery"))
3910 {
Mao Jinlong226f33a2014-07-04 17:24:10 +08003911 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
3912 fastboot_fail("image is not a boot image");
3913 return;
3914 }
Mayank Grover351a75e2017-05-30 20:06:08 +05303915
3916 /* Reset multislot_partition attributes in case of flashing boot */
3917 if (partition_multislot_is_supported())
3918 {
3919 partition_reset_attributes(index);
3920 }
Mao Jinlong226f33a2014-07-04 17:24:10 +08003921 }
3922
3923 if(!lun_set)
3924 {
3925 lun = partition_get_lun(index);
3926 mmc_set_lun(lun);
3927 }
3928
3929 size = partition_get_size(index);
Gaurav Nebhwani4d2389c2016-03-17 21:10:05 +05303930 if (ROUND_TO_PAGE(sz, mmc_blocksize_mask) > size) {
Mao Jinlong226f33a2014-07-04 17:24:10 +08003931 fastboot_fail("size too large");
3932 return;
3933 }
3934 else if (mmc_write(ptn , sz, (unsigned int *)data)) {
3935 fastboot_fail("flash write failure");
3936 return;
3937 }
Greg Grisco6e754772011-06-23 12:19:39 -07003938 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07003939 }
3940 fastboot_okay("");
3941 return;
3942}
3943
Ajay Dudanide984792015-03-02 09:57:41 -08003944void cmd_flash_meta_img(const char *arg, void *data, unsigned sz)
3945{
3946 int i, images;
3947 meta_header_t *meta_header;
3948 img_header_entry_t *img_header_entry;
Parth Dixit3e2d3032016-03-04 17:11:52 +05303949 /*End of the image address*/
3950 uintptr_t data_end;
3951
3952 if( (UINT_MAX - sz) > (uintptr_t)data )
3953 data_end = (uintptr_t)data + sz;
3954 else
3955 {
3956 fastboot_fail("Cannot flash: image header corrupt");
3957 return;
3958 }
3959
3960 if( data_end < ((uintptr_t)data + sizeof(meta_header_t)))
3961 {
3962 fastboot_fail("Cannot flash: image header corrupt");
3963 return;
3964 }
Ajay Dudanide984792015-03-02 09:57:41 -08003965
lijuang8ee21882016-04-19 16:57:11 +08003966 /* If device is locked:
3967 * Forbid to flash image to avoid the device to bypass the image
3968 * which with "any" name other than bootloader. Because it maybe
3969 * a meta package of all partitions.
3970 */
Monika Singh292b3e92018-03-17 22:40:23 +05303971#if VERIFIED_BOOT || VERIFIED_BOOT_2
lijuang8ee21882016-04-19 16:57:11 +08003972 if (target_build_variant_user()) {
3973 if (!device.is_unlocked) {
3974 fastboot_fail("Device is locked, meta image flashing is not allowed");
3975 return;
3976 }
Mayank Grover889be1b2017-09-12 20:12:23 +05303977
Mayank Grover912eaa62017-10-26 12:08:53 +05303978 if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05303979 !device.is_unlock_critical)
3980 {
lijuang8ee21882016-04-19 16:57:11 +08003981 fastboot_fail("Device is critical locked, Meta image flashing is not allowed");
3982 return;
3983 }
lijuang8ee21882016-04-19 16:57:11 +08003984 }
3985#endif
3986
Ajay Dudanide984792015-03-02 09:57:41 -08003987 meta_header = (meta_header_t*) data;
Parth Dixit3e2d3032016-03-04 17:11:52 +05303988 if( data_end < ((uintptr_t)data + meta_header->img_hdr_sz))
3989 {
3990 fastboot_fail("Cannot flash: image header corrupt");
3991 return;
3992 }
Ajay Dudanide984792015-03-02 09:57:41 -08003993 img_header_entry = (img_header_entry_t*) (data+sizeof(meta_header_t));
3994
3995 images = meta_header->img_hdr_sz / sizeof(img_header_entry_t);
3996
3997 for (i=0; i<images; i++) {
3998
3999 if((img_header_entry[i].ptn_name == NULL) ||
4000 (img_header_entry[i].start_offset == 0) ||
4001 (img_header_entry[i].size == 0))
4002 break;
Kishor PK49831502017-04-21 17:55:43 +05304003 if ((UINT_MAX - img_header_entry[i].start_offset) < (uintptr_t)data) {
4004 fastboot_fail("Integer overflow detected in start_offset of img");
4005 break;
4006 }
4007 else if ((UINT_MAX - (img_header_entry[i].start_offset + (uintptr_t)data)) < img_header_entry[i].size) {
4008 fastboot_fail("Integer overflow detected in size of img");
4009 break;
4010 }
Parth Dixit3e2d3032016-03-04 17:11:52 +05304011 if( data_end < ((uintptr_t)data + img_header_entry[i].start_offset
4012 + img_header_entry[i].size) )
4013 {
4014 fastboot_fail("Cannot flash: image size mismatch");
4015 break;
4016 }
4017
Ajay Dudanide984792015-03-02 09:57:41 -08004018 cmd_flash_mmc_img(img_header_entry[i].ptn_name,
4019 (void *) data + img_header_entry[i].start_offset,
4020 img_header_entry[i].size);
4021 }
4022
Channagoud Kadabiaafbbfc2015-03-04 17:52:56 -08004023 if (!strncmp(arg, "bootloader", strlen("bootloader")))
4024 {
4025 strlcpy(device.bootloader_version, TARGET(BOARD), MAX_VERSION_LEN);
4026 strlcat(device.bootloader_version, "-", MAX_VERSION_LEN);
4027 strlcat(device.bootloader_version, meta_header->img_version, MAX_VERSION_LEN);
4028 }
4029 else
4030 {
4031 strlcpy(device.radio_version, TARGET(BOARD), MAX_VERSION_LEN);
4032 strlcat(device.radio_version, "-", MAX_VERSION_LEN);
4033 strlcat(device.radio_version, meta_header->img_version, MAX_VERSION_LEN);
4034 }
4035
4036 write_device_info(&device);
Ajay Dudanide984792015-03-02 09:57:41 -08004037 fastboot_okay("");
4038 return;
4039}
4040
Ajay Dudani5c761132011-04-07 20:19:04 -07004041void cmd_flash_mmc_sparse_img(const char *arg, void *data, unsigned sz)
4042{
4043 unsigned int chunk;
wufeng.jiang813dc352015-06-02 23:02:46 -04004044 uint64_t chunk_data_sz;
Channagoud Kadabif627cf82013-09-09 14:08:20 -07004045 uint32_t *fill_buf = NULL;
4046 uint32_t fill_val;
Mayank Grover4f50bba2017-07-19 18:17:08 +05304047 uint32_t blk_sz_actual = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07004048 sparse_header_t *sparse_header;
4049 chunk_header_t *chunk_header;
Ajay Dudaniab18f022011-05-12 14:39:22 -07004050 uint32_t total_blocks = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07004051 unsigned long long ptn = 0;
Channagoud Kadabi65b91002011-10-11 17:34:33 +05304052 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07004053 int index = INVALID_PTN;
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05304054 uint32_t i;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08004055 uint8_t lun = 0;
vijay kumar800255e2015-04-24 20:26:19 +05304056 /*End of the sparse image address*/
Parth Dixit64b1a482016-03-07 16:31:26 +05304057 uintptr_t data_end = (uintptr_t)data + sz;
Ajay Dudani5c761132011-04-07 20:19:04 -07004058
Kinson Chikf1a43512011-07-14 11:28:39 -07004059 index = partition_get_index(arg);
4060 ptn = partition_get_offset(index);
4061 if(ptn == 0) {
Ajay Dudani5c761132011-04-07 20:19:04 -07004062 fastboot_fail("partition table doesn't exist");
4063 return;
4064 }
4065
Channagoud Kadabi65b91002011-10-11 17:34:33 +05304066 size = partition_get_size(index);
Channagoud Kadabi65b91002011-10-11 17:34:33 +05304067
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08004068 lun = partition_get_lun(index);
4069 mmc_set_lun(lun);
4070
vijay kumar800255e2015-04-24 20:26:19 +05304071 if (sz < sizeof(sparse_header_t)) {
4072 fastboot_fail("size too low");
4073 return;
4074 }
4075
Ajay Dudani5c761132011-04-07 20:19:04 -07004076 /* Read and skip over sparse image header */
4077 sparse_header = (sparse_header_t *) data;
vijay kumar800255e2015-04-24 20:26:19 +05304078
Mayank Grover48bd9bb2017-07-19 12:04:16 +05304079 if (!sparse_header->blk_sz || (sparse_header->blk_sz % 4)){
4080 fastboot_fail("Invalid block size\n");
4081 return;
4082 }
4083
vijay kumar1321f342015-03-27 12:13:42 +05304084 if (((uint64_t)sparse_header->total_blks * (uint64_t)sparse_header->blk_sz) > size) {
Ajay Dudani876b3282012-12-21 14:12:17 -08004085 fastboot_fail("size too large");
4086 return;
4087 }
4088
vijay kumarde4fcf62015-04-23 13:05:49 +05304089 data += sizeof(sparse_header_t);
vijay kumar800255e2015-04-24 20:26:19 +05304090
Parth Dixit64b1a482016-03-07 16:31:26 +05304091 if (data_end < (uintptr_t)data) {
vijay kumar800255e2015-04-24 20:26:19 +05304092 fastboot_fail("buffer overreads occured due to invalid sparse header");
4093 return;
4094 }
4095
vijay kumarde4fcf62015-04-23 13:05:49 +05304096 if(sparse_header->file_hdr_sz != sizeof(sparse_header_t))
Ajay Dudani5c761132011-04-07 20:19:04 -07004097 {
vijay kumarde4fcf62015-04-23 13:05:49 +05304098 fastboot_fail("sparse header size mismatch");
4099 return;
Ajay Dudani5c761132011-04-07 20:19:04 -07004100 }
4101
Ajay Dudanib06c05f2011-05-12 14:46:10 -07004102 dprintf (SPEW, "=== Sparse Image Header ===\n");
4103 dprintf (SPEW, "magic: 0x%x\n", sparse_header->magic);
4104 dprintf (SPEW, "major_version: 0x%x\n", sparse_header->major_version);
4105 dprintf (SPEW, "minor_version: 0x%x\n", sparse_header->minor_version);
4106 dprintf (SPEW, "file_hdr_sz: %d\n", sparse_header->file_hdr_sz);
4107 dprintf (SPEW, "chunk_hdr_sz: %d\n", sparse_header->chunk_hdr_sz);
4108 dprintf (SPEW, "blk_sz: %d\n", sparse_header->blk_sz);
4109 dprintf (SPEW, "total_blks: %d\n", sparse_header->total_blks);
4110 dprintf (SPEW, "total_chunks: %d\n", sparse_header->total_chunks);
Ajay Dudani5c761132011-04-07 20:19:04 -07004111
4112 /* Start processing chunks */
4113 for (chunk=0; chunk<sparse_header->total_chunks; chunk++)
4114 {
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05304115 /* Make sure the total image size does not exceed the partition size */
4116 if(((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz) >= size) {
4117 fastboot_fail("size too large");
4118 return;
4119 }
Ajay Dudani5c761132011-04-07 20:19:04 -07004120 /* Read and skip over chunk header */
4121 chunk_header = (chunk_header_t *) data;
4122 data += sizeof(chunk_header_t);
4123
Parth Dixit64b1a482016-03-07 16:31:26 +05304124 if (data_end < (uintptr_t)data) {
vijay kumar800255e2015-04-24 20:26:19 +05304125 fastboot_fail("buffer overreads occured due to invalid sparse header");
4126 return;
4127 }
4128
Ajay Dudani5c761132011-04-07 20:19:04 -07004129 dprintf (SPEW, "=== Chunk Header ===\n");
4130 dprintf (SPEW, "chunk_type: 0x%x\n", chunk_header->chunk_type);
4131 dprintf (SPEW, "chunk_data_sz: 0x%x\n", chunk_header->chunk_sz);
4132 dprintf (SPEW, "total_size: 0x%x\n", chunk_header->total_sz);
4133
vijay kumar800255e2015-04-24 20:26:19 +05304134 if(sparse_header->chunk_hdr_sz != sizeof(chunk_header_t))
Ajay Dudani5c761132011-04-07 20:19:04 -07004135 {
vijay kumar800255e2015-04-24 20:26:19 +05304136 fastboot_fail("chunk header size mismatch");
4137 return;
Ajay Dudani5c761132011-04-07 20:19:04 -07004138 }
4139
wufeng.jiang813dc352015-06-02 23:02:46 -04004140 chunk_data_sz = (uint64_t)sparse_header->blk_sz * chunk_header->chunk_sz;
4141
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05304142 /* Make sure that the chunk size calculated from sparse image does not
4143 * exceed partition size
4144 */
4145 if ((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz + chunk_data_sz > size)
4146 {
4147 fastboot_fail("Chunk data size exceeds partition size");
4148 return;
4149 }
4150
Ajay Dudani5c761132011-04-07 20:19:04 -07004151 switch (chunk_header->chunk_type)
4152 {
4153 case CHUNK_TYPE_RAW:
wufeng.jiang813dc352015-06-02 23:02:46 -04004154 if((uint64_t)chunk_header->total_sz != ((uint64_t)sparse_header->chunk_hdr_sz +
Ajay Dudani5c761132011-04-07 20:19:04 -07004155 chunk_data_sz))
4156 {
4157 fastboot_fail("Bogus chunk size for chunk type Raw");
4158 return;
4159 }
4160
Parth Dixit64b1a482016-03-07 16:31:26 +05304161 if (data_end < (uintptr_t)data + chunk_data_sz) {
vijay kumar800255e2015-04-24 20:26:19 +05304162 fastboot_fail("buffer overreads occured due to invalid sparse header");
4163 return;
4164 }
4165
wufeng.jiang813dc352015-06-02 23:02:46 -04004166 /* chunk_header->total_sz is uint32,So chunk_data_sz is now less than 2^32
4167 otherwise it will return in the line above
4168 */
Ajay Dudaniab18f022011-05-12 14:39:22 -07004169 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
wufeng.jiang813dc352015-06-02 23:02:46 -04004170 (uint32_t)chunk_data_sz,
Ajay Dudaniab18f022011-05-12 14:39:22 -07004171 (unsigned int*)data))
Ajay Dudani5c761132011-04-07 20:19:04 -07004172 {
4173 fastboot_fail("flash write failure");
4174 return;
4175 }
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05304176 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
4177 fastboot_fail("Bogus size for RAW chunk type");
4178 return;
4179 }
Ajay Dudani5c761132011-04-07 20:19:04 -07004180 total_blocks += chunk_header->chunk_sz;
wufeng.jiang813dc352015-06-02 23:02:46 -04004181 data += (uint32_t)chunk_data_sz;
Ajay Dudani5c761132011-04-07 20:19:04 -07004182 break;
4183
Channagoud Kadabif627cf82013-09-09 14:08:20 -07004184 case CHUNK_TYPE_FILL:
4185 if(chunk_header->total_sz != (sparse_header->chunk_hdr_sz +
4186 sizeof(uint32_t)))
4187 {
4188 fastboot_fail("Bogus chunk size for chunk type FILL");
4189 return;
4190 }
4191
Mayank Grover4f50bba2017-07-19 18:17:08 +05304192 blk_sz_actual = ROUNDUP(sparse_header->blk_sz, CACHE_LINE);
4193 /* Integer overflow detected */
4194 if (blk_sz_actual < sparse_header->blk_sz)
4195 {
4196 fastboot_fail("Invalid block size");
4197 return;
4198 }
4199
4200 fill_buf = (uint32_t *)memalign(CACHE_LINE, blk_sz_actual);
Channagoud Kadabif627cf82013-09-09 14:08:20 -07004201 if (!fill_buf)
4202 {
4203 fastboot_fail("Malloc failed for: CHUNK_TYPE_FILL");
4204 return;
4205 }
4206
Parth Dixit64b1a482016-03-07 16:31:26 +05304207 if (data_end < (uintptr_t)data + sizeof(uint32_t)) {
vijay kumar800255e2015-04-24 20:26:19 +05304208 fastboot_fail("buffer overreads occured due to invalid sparse header");
Gaurav Nebhwani163cbf82016-03-17 21:21:27 +05304209 free(fill_buf);
vijay kumar800255e2015-04-24 20:26:19 +05304210 return;
4211 }
Channagoud Kadabif627cf82013-09-09 14:08:20 -07004212 fill_val = *(uint32_t *)data;
4213 data = (char *) data + sizeof(uint32_t);
Channagoud Kadabif627cf82013-09-09 14:08:20 -07004214
4215 for (i = 0; i < (sparse_header->blk_sz / sizeof(fill_val)); i++)
4216 {
4217 fill_buf[i] = fill_val;
4218 }
4219
Gaurav Nebhwanie94cbe12016-03-17 21:16:34 +05304220 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz))
4221 {
4222 fastboot_fail("bogus size for chunk FILL type");
4223 free(fill_buf);
4224 return;
4225 }
4226
wufeng.jiang813dc352015-06-02 23:02:46 -04004227 for (i = 0; i < chunk_header->chunk_sz; i++)
Channagoud Kadabif627cf82013-09-09 14:08:20 -07004228 {
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05304229 /* Make sure that the data written to partition does not exceed partition size */
4230 if ((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz + sparse_header->blk_sz > size)
4231 {
4232 fastboot_fail("Chunk data size for fill type exceeds partition size");
Gaurav Nebhwani163cbf82016-03-17 21:21:27 +05304233 free(fill_buf);
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05304234 return;
4235 }
4236
Channagoud Kadabif627cf82013-09-09 14:08:20 -07004237 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
4238 sparse_header->blk_sz,
4239 fill_buf))
4240 {
4241 fastboot_fail("flash write failure");
4242 free(fill_buf);
4243 return;
4244 }
4245
4246 total_blocks++;
4247 }
4248
4249 free(fill_buf);
4250 break;
4251
Ajay Dudani5c761132011-04-07 20:19:04 -07004252 case CHUNK_TYPE_DONT_CARE:
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05304253 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
4254 fastboot_fail("bogus size for chunk DONT CARE type");
4255 return;
4256 }
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07004257 total_blocks += chunk_header->chunk_sz;
4258 break;
4259
Ajay Dudani5c761132011-04-07 20:19:04 -07004260 case CHUNK_TYPE_CRC:
4261 if(chunk_header->total_sz != sparse_header->chunk_hdr_sz)
4262 {
wufeng.jiang813dc352015-06-02 23:02:46 -04004263 fastboot_fail("Bogus chunk size for chunk type CRC");
Ajay Dudani5c761132011-04-07 20:19:04 -07004264 return;
4265 }
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05304266 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
4267 fastboot_fail("bogus size for chunk CRC type");
4268 return;
4269 }
Ajay Dudani5c761132011-04-07 20:19:04 -07004270 total_blocks += chunk_header->chunk_sz;
Parth Dixit64b1a482016-03-07 16:31:26 +05304271 if ((uintptr_t)data > UINT_MAX - chunk_data_sz) {
vijay kumar800255e2015-04-24 20:26:19 +05304272 fastboot_fail("integer overflow occured");
4273 return;
4274 }
wufeng.jiang813dc352015-06-02 23:02:46 -04004275 data += (uint32_t)chunk_data_sz;
Parth Dixit64b1a482016-03-07 16:31:26 +05304276 if (data_end < (uintptr_t)data) {
vijay kumar800255e2015-04-24 20:26:19 +05304277 fastboot_fail("buffer overreads occured due to invalid sparse header");
4278 return;
4279 }
Ajay Dudani5c761132011-04-07 20:19:04 -07004280 break;
4281
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07004282 default:
Channagoud Kadabif627cf82013-09-09 14:08:20 -07004283 dprintf(CRITICAL, "Unkown chunk type: %x\n",chunk_header->chunk_type);
Ajay Dudani5c761132011-04-07 20:19:04 -07004284 fastboot_fail("Unknown chunk type");
4285 return;
4286 }
4287 }
4288
Ajay Dudani0c6927b2011-05-18 11:12:16 -07004289 dprintf(INFO, "Wrote %d blocks, expected to write %d blocks\n",
4290 total_blocks, sparse_header->total_blks);
4291
4292 if(total_blocks != sparse_header->total_blks)
4293 {
4294 fastboot_fail("sparse image write failure");
4295 }
Ajay Dudani5c761132011-04-07 20:19:04 -07004296
4297 fastboot_okay("");
4298 return;
4299}
4300
4301void cmd_flash_mmc(const char *arg, void *data, unsigned sz)
4302{
4303 sparse_header_t *sparse_header;
Ajay Dudanide984792015-03-02 09:57:41 -08004304 meta_header_t *meta_header;
Ajay Dudani5c761132011-04-07 20:19:04 -07004305
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07004306#ifdef SSD_ENABLE
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -08004307 /* 8 Byte Magic + 2048 Byte xml + Encrypted Data */
4308 unsigned int *magic_number = (unsigned int *) data;
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08004309 int ret=0;
4310 uint32 major_version=0;
4311 uint32 minor_version=0;
4312
4313 ret = scm_svc_version(&major_version,&minor_version);
4314 if(!ret)
4315 {
4316 if(major_version >= 2)
4317 {
Amir Samuelovbb65ce02013-05-05 12:20:18 +03004318 if( !strcmp(arg, "ssd") || !strcmp(arg, "tqs") )
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08004319 {
4320 ret = encrypt_scm((uint32 **) &data, &sz);
4321 if (ret != 0) {
4322 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
4323 return;
4324 }
4325
Amir Samuelovbb65ce02013-05-05 12:20:18 +03004326 /* Protect only for SSD */
4327 if (!strcmp(arg, "ssd")) {
4328 ret = scm_protect_keystore((uint32 *) data, sz);
4329 if (ret != 0) {
4330 dprintf(CRITICAL, "ERROR: scm_protect_keystore Failed\n");
4331 return;
4332 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08004333 }
4334 }
4335 else
4336 {
4337 ret = decrypt_scm_v2((uint32 **) &data, &sz);
4338 if(ret != 0)
4339 {
4340 dprintf(CRITICAL,"ERROR: Decryption Failure\n");
4341 return;
4342 }
4343 }
4344 }
4345 else
4346 {
4347 if (magic_number[0] == DECRYPT_MAGIC_0 &&
4348 magic_number[1] == DECRYPT_MAGIC_1)
4349 {
4350 ret = decrypt_scm((uint32 **) &data, &sz);
4351 if (ret != 0) {
4352 dprintf(CRITICAL, "ERROR: Invalid secure image\n");
4353 return;
4354 }
4355 }
4356 else if (magic_number[0] == ENCRYPT_MAGIC_0 &&
4357 magic_number[1] == ENCRYPT_MAGIC_1)
4358 {
4359 ret = encrypt_scm((uint32 **) &data, &sz);
4360 if (ret != 0) {
4361 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
4362 return;
4363 }
4364 }
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07004365 }
4366 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08004367 else
Neeti Desai127b9e02012-03-20 16:11:23 -07004368 {
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08004369 dprintf(CRITICAL,"INVALID SVC Version\n");
4370 return;
Neeti Desai127b9e02012-03-20 16:11:23 -07004371 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08004372#endif /* SSD_ENABLE */
Neeti Desai127b9e02012-03-20 16:11:23 -07004373
Monika Singh292b3e92018-03-17 22:40:23 +05304374#if VERIFIED_BOOT || VERIFIED_BOOT_2
Channagoud Kadabi35297672015-06-13 11:09:49 -07004375 if (target_build_variant_user())
Shashank Mittald3e54dd2014-08-28 15:24:02 -07004376 {
lijuang511a2b52015-08-14 20:50:51 +08004377 /* if device is locked:
4378 * common partition will not allow to be flashed
4379 * critical partition will allow to flash image.
4380 */
4381 if(!device.is_unlocked && !critical_flash_allowed(arg)) {
4382 fastboot_fail("Partition flashing is not allowed");
4383 return;
4384 }
Mayank Grover889be1b2017-09-12 20:12:23 +05304385
lijuang511a2b52015-08-14 20:50:51 +08004386 /* if device critical is locked:
4387 * common partition will allow to be flashed
4388 * critical partition will not allow to flash image.
4389 */
Mayank Grover912eaa62017-10-26 12:08:53 +05304390 if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05304391 !device.is_unlock_critical &&
4392 critical_flash_allowed(arg)) {
4393 fastboot_fail("Critical partition flashing is not allowed");
4394 return;
Shashank Mittald3e54dd2014-08-28 15:24:02 -07004395 }
4396 }
4397#endif
Monika Singh292b3e92018-03-17 22:40:23 +05304398 if (!strncmp(arg, "avb_custom_key", strlen("avb_custom_key"))) {
4399 dprintf(INFO, "flashing avb_custom_key\n");
4400 if (store_userkey(data, sz)) {
4401 fastboot_fail("Flashing avb_custom_key failed");
4402 } else {
4403 fastboot_okay("");
4404 }
4405 return;
4406 }
Shashank Mittald3e54dd2014-08-28 15:24:02 -07004407
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07004408 sparse_header = (sparse_header_t *) data;
Ajay Dudanide984792015-03-02 09:57:41 -08004409 meta_header = (meta_header_t *) data;
4410 if (sparse_header->magic == SPARSE_HEADER_MAGIC)
Ajay Dudani5c761132011-04-07 20:19:04 -07004411 cmd_flash_mmc_sparse_img(arg, data, sz);
Ajay Dudanide984792015-03-02 09:57:41 -08004412 else if (meta_header->magic == META_HEADER_MAGIC)
4413 cmd_flash_meta_img(arg, data, sz);
4414 else
4415 cmd_flash_mmc_img(arg, data, sz);
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07004416
4417#if VERIFIED_BOOT
Mayank Grover912eaa62017-10-26 12:08:53 +05304418 if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05304419 (!strncmp(arg, "system", 6)) &&
4420 !device.verity_mode)
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07004421 // reset dm_verity mode to enforcing
4422 device.verity_mode = 1;
4423 write_device_info(&device);
Parth Dixitddbc7352015-10-18 03:13:31 +05304424#endif
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07004425
Ajay Dudani5c761132011-04-07 20:19:04 -07004426 return;
4427}
4428
Tanya Brokhman84eaaf62015-02-24 21:57:06 +02004429void cmd_updatevol(const char *vol_name, void *data, unsigned sz)
4430{
4431 struct ptentry *sys_ptn;
4432 struct ptable *ptable;
4433
4434 ptable = flash_get_ptable();
4435 if (ptable == NULL) {
4436 fastboot_fail("partition table doesn't exist");
4437 return;
4438 }
4439
4440 sys_ptn = ptable_find(ptable, "system");
4441 if (sys_ptn == NULL) {
4442 fastboot_fail("system partition not found");
4443 return;
4444 }
4445
4446 sz = ROUND_TO_PAGE(sz, page_mask);
4447 if (update_ubi_vol(sys_ptn, vol_name, data, sz))
4448 fastboot_fail("update_ubi_vol failed");
4449 else
4450 fastboot_okay("");
4451}
4452
Sridhar Parasurame94e8152014-10-24 14:06:03 -07004453void cmd_flash_nand(const char *arg, void *data, unsigned sz)
Dima Zavin214cc642009-01-26 11:16:21 -08004454{
4455 struct ptentry *ptn;
4456 struct ptable *ptable;
4457 unsigned extra = 0;
Gaurav Nebhwanid9730712016-05-06 14:28:29 +05304458 uint64_t partition_size = 0;
Vijay Kumar Pendotic6f22a72017-05-19 22:44:34 +05304459 unsigned bytes_to_round_page = 0;
4460 unsigned rounded_size = 0;
Dima Zavin214cc642009-01-26 11:16:21 -08004461
Kishor PK38ed93d2017-04-25 14:19:26 +05304462 if((uintptr_t)data > (UINT_MAX - sz)) {
4463 fastboot_fail("Cannot flash: image header corrupt");
4464 return;
4465 }
4466
Dima Zavin214cc642009-01-26 11:16:21 -08004467 ptable = flash_get_ptable();
4468 if (ptable == NULL) {
4469 fastboot_fail("partition table doesn't exist");
4470 return;
4471 }
4472
4473 ptn = ptable_find(ptable, arg);
4474 if (ptn == NULL) {
Tanya Brokhman84eaaf62015-02-24 21:57:06 +02004475 dprintf(INFO, "unknown partition name (%s). Trying updatevol\n",
4476 arg);
4477 cmd_updatevol(arg, data, sz);
Dima Zavin214cc642009-01-26 11:16:21 -08004478 return;
4479 }
4480
Monika Singh292b3e92018-03-17 22:40:23 +05304481 if (!strncmp(arg, "avb_custom_key", strlen("avb_custom_key"))) {
4482 dprintf(INFO, "flashing avb_custom_key\n");
4483 if (store_userkey(data, sz)) {
4484 fastboot_fail("Flashing avb_custom_key failed");
4485 } else {
4486 fastboot_okay("");
4487 }
4488 return;
4489 }
4490
Dima Zavin214cc642009-01-26 11:16:21 -08004491 if (!strcmp(ptn->name, "boot") || !strcmp(ptn->name, "recovery")) {
Kishor PK38ed93d2017-04-25 14:19:26 +05304492 if((sz > BOOT_MAGIC_SIZE) && (!memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE))) {
4493 dprintf(INFO, "Verified the BOOT_MAGIC in image header \n");
4494 } else {
4495 fastboot_fail("Image is not a boot image");
Dima Zavin214cc642009-01-26 11:16:21 -08004496 return;
4497 }
4498 }
4499
Amol Jadi5c61a952012-05-04 17:05:35 -07004500 if (!strcmp(ptn->name, "system")
Deepa Dinamani13e32c42012-03-12 14:34:17 -07004501 || !strcmp(ptn->name, "userdata")
4502 || !strcmp(ptn->name, "persist")
Sundarajan Srinivasanb063a852013-11-19 14:02:27 -08004503 || !strcmp(ptn->name, "recoveryfs")
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -08004504 || !strcmp(ptn->name, "modem"))
Tanya Brokhman1c94f1a2015-02-15 09:05:03 +02004505 extra = 1;
Kishor PK38ed93d2017-04-25 14:19:26 +05304506 else {
Vijay Kumar Pendotic6f22a72017-05-19 22:44:34 +05304507 rounded_size = ROUNDUP(sz, page_size);
4508 bytes_to_round_page = rounded_size - sz;
4509 if (bytes_to_round_page) {
4510 if (((uintptr_t)data + sz ) > (UINT_MAX - bytes_to_round_page)) {
4511 fastboot_fail("Integer overflow detected");
4512 return;
4513 }
4514 if (((uintptr_t)data + sz + bytes_to_round_page) >
4515 ((uintptr_t)target_get_scratch_address() + target_get_max_flash_size())) {
4516 fastboot_fail("Buffer size is not aligned to page_size");
4517 return;
4518 }
4519 else {
4520 memset(data + sz, 0, bytes_to_round_page);
4521 sz = rounded_size;
4522 }
Kishor PK38ed93d2017-04-25 14:19:26 +05304523 }
Gaurav Nebhwanid9730712016-05-06 14:28:29 +05304524 }
4525
Kishor PK38ed93d2017-04-25 14:19:26 +05304526 /*Checking partition_size for the possible integer overflow */
4527 partition_size = validate_partition_size(ptn);
4528
Gaurav Nebhwanid9730712016-05-06 14:28:29 +05304529 if (sz > partition_size) {
4530 fastboot_fail("Image size too large");
4531 return;
4532 }
4533
Dima Zavin214cc642009-01-26 11:16:21 -08004534 dprintf(INFO, "writing %d bytes to '%s'\n", sz, ptn->name);
Mayank Grover6cde9352017-06-06 18:45:23 +05304535 if ((sz > UBI_EC_HDR_SIZE) &&
4536 (!memcmp((void *)data, UBI_MAGIC, UBI_MAGIC_SIZE))) {
Tanya Brokhman1c94f1a2015-02-15 09:05:03 +02004537 if (flash_ubi_img(ptn, data, sz)) {
4538 fastboot_fail("flash write failure");
4539 return;
4540 }
4541 } else {
4542 if (flash_write(ptn, extra, data, sz)) {
4543 fastboot_fail("flash write failure");
4544 return;
4545 }
Dima Zavin214cc642009-01-26 11:16:21 -08004546 }
4547 dprintf(INFO, "partition '%s' updated\n", ptn->name);
4548 fastboot_okay("");
4549}
4550
Kishor PK38ed93d2017-04-25 14:19:26 +05304551
4552static inline uint64_t validate_partition_size(struct ptentry *ptn)
4553{
4554 if (ptn->length && flash_num_pages_per_blk() && page_size) {
4555 if ((ptn->length < ( UINT_MAX / flash_num_pages_per_blk())) && ((ptn->length * flash_num_pages_per_blk()) < ( UINT_MAX / page_size))) {
4556 return ptn->length * flash_num_pages_per_blk() * page_size;
4557 }
4558 }
4559 return 0;
4560}
4561
4562
Sridhar Parasurame94e8152014-10-24 14:06:03 -07004563void cmd_flash(const char *arg, void *data, unsigned sz)
4564{
4565 if(target_is_emmc_boot())
4566 cmd_flash_mmc(arg, data, sz);
4567 else
4568 cmd_flash_nand(arg, data, sz);
4569}
4570
Dima Zavin214cc642009-01-26 11:16:21 -08004571void cmd_continue(const char *arg, void *data, unsigned sz)
4572{
4573 fastboot_okay("");
Amol Jadi7c4316c2013-10-07 14:19:26 -07004574 fastboot_stop();
Shashank Mittald3e54dd2014-08-28 15:24:02 -07004575
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004576 if (target_is_emmc_boot())
4577 {
lijuanga40d6302015-07-20 20:10:13 +08004578#if FBCON_DISPLAY_MSG
lijuangde34d502016-02-26 16:04:50 +08004579 /* Exit keys' detection thread firstly */
4580 exit_menu_keys_detection();
lijuanga40d6302015-07-20 20:10:13 +08004581#endif
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004582 boot_linux_from_mmc();
4583 }
4584 else
4585 {
4586 boot_linux_from_flash();
4587 }
Dima Zavin214cc642009-01-26 11:16:21 -08004588}
4589
Chandan Uddaraju94183c02010-01-15 15:13:59 -08004590void cmd_reboot(const char *arg, void *data, unsigned sz)
4591{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004592 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08004593 fastboot_okay("");
4594 reboot_device(0);
4595}
4596
Mayank Grover351a75e2017-05-30 20:06:08 +05304597void cmd_set_active(const char *arg, void *data, unsigned sz)
4598{
Mayank Grover6ccc1c92017-07-04 17:36:46 +05304599 char *p, *sp = NULL;
Mayank Grover351a75e2017-05-30 20:06:08 +05304600 unsigned i,current_active_slot;
4601 const char *current_slot_suffix;
4602
4603 if (!partition_multislot_is_supported())
4604 {
4605 fastboot_fail("Command not supported");
4606 return;
4607 }
4608
4609 if (arg)
4610 {
Mayank Grover6ccc1c92017-07-04 17:36:46 +05304611 p = strtok_r((char *)arg, ":", &sp);
Mayank Grover5eb5f692017-07-19 20:16:04 +05304612 if (p)
Mayank Grover351a75e2017-05-30 20:06:08 +05304613 {
4614 current_active_slot = partition_find_active_slot();
4615
4616 /* Check if trying to make curent slot active */
4617 current_slot_suffix = SUFFIX_SLOT(current_active_slot);
Mayank Grover6ccc1c92017-07-04 17:36:46 +05304618 current_slot_suffix = strtok_r((char *)current_slot_suffix,
4619 (char *)suffix_delimiter, &sp);
4620
Mayank Grover5eb5f692017-07-19 20:16:04 +05304621 if (current_slot_suffix &&
4622 !strncmp(p, current_slot_suffix, strlen(current_slot_suffix)))
Mayank Grover351a75e2017-05-30 20:06:08 +05304623 {
4624 fastboot_okay("Slot already set active");
4625 return;
4626 }
4627 else
4628 {
4629 for (i = 0; i < AB_SUPPORTED_SLOTS; i++)
4630 {
4631 current_slot_suffix = SUFFIX_SLOT(i);
Mayank Grover6ccc1c92017-07-04 17:36:46 +05304632 current_slot_suffix = strtok_r((char *)current_slot_suffix,
4633 (char *)suffix_delimiter, &sp);
Mayank Grover5eb5f692017-07-19 20:16:04 +05304634 if (current_slot_suffix &&
4635 !strncmp(p, current_slot_suffix, strlen(current_slot_suffix)))
Mayank Grover351a75e2017-05-30 20:06:08 +05304636 {
lijuang8b03e5f2019-07-12 18:16:19 +08004637 partition_switch_slots(current_active_slot, i, true);
Mayank Grover351a75e2017-05-30 20:06:08 +05304638 publish_getvar_multislot_vars();
4639 fastboot_okay("");
4640 return;
4641 }
4642 }
4643 }
4644 }
4645 }
4646 fastboot_fail("Invalid slot suffix.");
4647 return;
4648}
4649
Mayank Grover98c4c742019-04-25 17:21:37 +05304650#if DYNAMIC_PARTITION_SUPPORT
4651void cmd_reboot_fastboot(const char *arg, void *data, unsigned sz)
4652{
4653 dprintf(INFO, "rebooting the device - userspace fastboot\n");
4654 if (send_recovery_cmd(RECOVERY_BOOT_FASTBOOT_CMD)) {
4655 dprintf(CRITICAL, "ERROR: Failed to update recovery commands\n");
4656 fastboot_fail("Failed to update recovery command");
4657 return;
4658 }
4659 fastboot_okay("");
4660 reboot_device(REBOOT_MODE_UNKNOWN);
4661
4662 //shouldn't come here.
4663 dprintf(CRITICAL, "ERROR: Failed to reboot device\n");
4664 return;
4665}
4666
4667void cmd_reboot_recovery(const char *arg, void *data, unsigned sz)
4668{
4669 dprintf(INFO, "rebooting the device - recovery\n");
4670 if (send_recovery_cmd(RECOVERY_BOOT_RECOVERY_CMD)) {
4671 dprintf(CRITICAL, "ERROR: Failed to update recovery commands\n");
4672 fastboot_fail("Failed to update recovery command");
4673 return;
4674 }
4675 fastboot_okay("");
4676 reboot_device(REBOOT_MODE_UNKNOWN);
4677
4678 //shouldn't come here.
4679 dprintf(CRITICAL, "ERROR: Failed to reboot device\n");
4680 return;
4681}
4682#endif
4683
Chandan Uddaraju94183c02010-01-15 15:13:59 -08004684void cmd_reboot_bootloader(const char *arg, void *data, unsigned sz)
4685{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004686 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08004687 fastboot_okay("");
4688 reboot_device(FASTBOOT_MODE);
4689}
4690
Ameya Thakur11cf1a62013-08-05 12:44:48 -07004691void cmd_oem_enable_charger_screen(const char *arg, void *data, unsigned size)
4692{
4693 dprintf(INFO, "Enabling charger screen check\n");
4694 device.charger_screen_enabled = 1;
4695 write_device_info(&device);
4696 fastboot_okay("");
4697}
4698
4699void cmd_oem_disable_charger_screen(const char *arg, void *data, unsigned size)
4700{
4701 dprintf(INFO, "Disabling charger screen check\n");
4702 device.charger_screen_enabled = 0;
4703 write_device_info(&device);
4704 fastboot_okay("");
4705}
4706
lijuanga25d8bb2015-09-16 13:11:52 +08004707void cmd_oem_off_mode_charger(const char *arg, void *data, unsigned size)
4708{
4709 char *p = NULL;
4710 const char *delim = " \t\n\r";
Parth Dixit407f15b2016-01-07 14:47:37 +05304711 char *sp;
lijuanga25d8bb2015-09-16 13:11:52 +08004712
4713 if (arg) {
Parth Dixit407f15b2016-01-07 14:47:37 +05304714 p = strtok_r((char *)arg, delim, &sp);
lijuanga25d8bb2015-09-16 13:11:52 +08004715 if (p) {
4716 if (!strncmp(p, "0", 1)) {
4717 device.charger_screen_enabled = 0;
4718 } else if (!strncmp(p, "1", 1)) {
4719 device.charger_screen_enabled = 1;
4720 }
4721 }
4722 }
4723
4724 /* update charger_screen_enabled value for getvar
4725 * command
4726 */
4727 snprintf(charger_screen_enabled, MAX_RSP_SIZE, "%d",
4728 device.charger_screen_enabled);
4729
4730 write_device_info(&device);
4731 fastboot_okay("");
4732}
4733
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05304734void cmd_oem_select_display_panel(const char *arg, void *data, unsigned size)
4735{
4736 dprintf(INFO, "Selecting display panel %s\n", arg);
4737 if (arg)
4738 strlcpy(device.display_panel, arg,
4739 sizeof(device.display_panel));
4740 write_device_info(&device);
4741 fastboot_okay("");
4742}
4743
Shashank Mittal162244e2011-08-08 19:01:25 -07004744void cmd_oem_unlock(const char *arg, void *data, unsigned sz)
4745{
lijuang4ece1e72015-08-14 21:02:36 +08004746 set_device_unlock(UNLOCK, TRUE);
vijay kumarc65876c2015-04-24 13:29:16 +05304747}
4748
4749void cmd_oem_unlock_go(const char *arg, void *data, unsigned sz)
4750{
lijuang4ece1e72015-08-14 21:02:36 +08004751 if(!device.is_unlocked) {
vijay kumarc65876c2015-04-24 13:29:16 +05304752 if(!is_allow_unlock) {
4753 fastboot_fail("oem unlock is not allowed");
4754 return;
4755 }
4756
lijuang4ece1e72015-08-14 21:02:36 +08004757 set_device_unlock_value(UNLOCK, TRUE);
vijay kumarc65876c2015-04-24 13:29:16 +05304758
lijuang4ece1e72015-08-14 21:02:36 +08004759 /* wipe data */
vijay kumarc65876c2015-04-24 13:29:16 +05304760 struct recovery_message msg;
Kishor PK60a68212017-05-08 16:55:57 +05304761 memset(&msg, 0, sizeof(msg));
vijay kumarc65876c2015-04-24 13:29:16 +05304762 snprintf(msg.recovery, sizeof(msg.recovery), "recovery\n--wipe_data");
4763 write_misc(0, &msg, sizeof(msg));
4764
4765 fastboot_okay("");
4766 reboot_device(RECOVERY_MODE);
Shashank Mittald3e54dd2014-08-28 15:24:02 -07004767 }
4768 fastboot_okay("");
4769}
4770
Channagoud Kadabiad259832015-05-29 11:14:17 -07004771static int aboot_frp_unlock(char *pname, void *data, unsigned sz)
4772{
Mayank Grover70f8b0e2017-05-17 11:49:00 +05304773 int ret=1;
4774 bool authentication_success=false;
Channagoud Kadabiad259832015-05-29 11:14:17 -07004775
Mayank Grover70f8b0e2017-05-17 11:49:00 +05304776 /*
4777 Authentication method not implemented.
4778
4779 OEM to implement, authentication system which on successful validataion,
4780 calls write_allow_oem_unlock() with is_allow_unlock.
4781 */
4782#if 0
4783 authentication_success = oem_specific_auth_mthd();
4784#endif
4785
4786 if (authentication_success)
Channagoud Kadabiad259832015-05-29 11:14:17 -07004787 {
Mayank Grover70f8b0e2017-05-17 11:49:00 +05304788 is_allow_unlock = true;
4789 write_allow_oem_unlock(is_allow_unlock);
4790 ret = 0;
Channagoud Kadabiad259832015-05-29 11:14:17 -07004791 }
4792 return ret;
4793}
4794
Shashank Mittald3e54dd2014-08-28 15:24:02 -07004795void cmd_oem_lock(const char *arg, void *data, unsigned sz)
4796{
lijuang4ece1e72015-08-14 21:02:36 +08004797 set_device_unlock(UNLOCK, FALSE);
Shashank Mittal162244e2011-08-08 19:01:25 -07004798}
4799
Shashank Mittala0032282011-08-26 14:50:11 -07004800void cmd_oem_devinfo(const char *arg, void *data, unsigned sz)
4801{
lijuang511a2b52015-08-14 20:50:51 +08004802 char response[MAX_RSP_SIZE];
Ameya Thakur11cf1a62013-08-05 12:44:48 -07004803 snprintf(response, sizeof(response), "\tDevice tampered: %s", (device.is_tampered ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07004804 fastboot_info(response);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07004805 snprintf(response, sizeof(response), "\tDevice unlocked: %s", (device.is_unlocked ? "true" : "false"));
4806 fastboot_info(response);
Monika Singh292b3e92018-03-17 22:40:23 +05304807#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover912eaa62017-10-26 12:08:53 +05304808 if (VB_M <= target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +05304809 {
4810 snprintf(response, sizeof(response), "\tDevice critical unlocked: %s",
4811 (device.is_unlock_critical ? "true" : "false"));
4812 fastboot_info(response);
4813 }
Parth Dixitddbc7352015-10-18 03:13:31 +05304814#endif
Ameya Thakur11cf1a62013-08-05 12:44:48 -07004815 snprintf(response, sizeof(response), "\tCharger screen enabled: %s", (device.charger_screen_enabled ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07004816 fastboot_info(response);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05304817 snprintf(response, sizeof(response), "\tDisplay panel: %s", (device.display_panel));
4818 fastboot_info(response);
Shashank Mittala0032282011-08-26 14:50:11 -07004819 fastboot_okay("");
4820}
4821
lijuang511a2b52015-08-14 20:50:51 +08004822void cmd_flashing_get_unlock_ability(const char *arg, void *data, unsigned sz)
4823{
4824 char response[MAX_RSP_SIZE];
4825 snprintf(response, sizeof(response), "\tget_unlock_ability: %d", is_allow_unlock);
4826 fastboot_info(response);
4827 fastboot_okay("");
4828}
4829
4830void cmd_flashing_lock_critical(const char *arg, void *data, unsigned sz)
4831{
lijuang4ece1e72015-08-14 21:02:36 +08004832 set_device_unlock(UNLOCK_CRITICAL, FALSE);
lijuang511a2b52015-08-14 20:50:51 +08004833}
4834
4835void cmd_flashing_unlock_critical(const char *arg, void *data, unsigned sz)
4836{
lijuang4ece1e72015-08-14 21:02:36 +08004837 set_device_unlock(UNLOCK_CRITICAL, TRUE);
lijuang511a2b52015-08-14 20:50:51 +08004838}
4839
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07004840void cmd_preflash(const char *arg, void *data, unsigned sz)
4841{
4842 fastboot_okay("");
4843}
4844
Mao Flynn7b379f32015-04-20 00:28:30 +08004845static uint8_t logo_header[LOGO_IMG_HEADER_SIZE];
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304846
Mao Flynn7b379f32015-04-20 00:28:30 +08004847int splash_screen_check_header(logo_img_header *header)
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304848{
Mao Flynn7b379f32015-04-20 00:28:30 +08004849 if (memcmp(header->magic, LOGO_IMG_MAGIC, 8))
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304850 return -1;
Mao Flynn7b379f32015-04-20 00:28:30 +08004851 if (header->width == 0 || header->height == 0)
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304852 return -1;
4853 return 0;
4854}
4855
Mao Flynn7b379f32015-04-20 00:28:30 +08004856int splash_screen_flash()
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07004857{
4858 struct ptentry *ptn;
4859 struct ptable *ptable;
Mao Flynn7b379f32015-04-20 00:28:30 +08004860 struct logo_img_header *header;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07004861 struct fbcon_config *fb_display = NULL;
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08004862
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304863 ptable = flash_get_ptable();
4864 if (ptable == NULL) {
Mao Flynn7b379f32015-04-20 00:28:30 +08004865 dprintf(CRITICAL, "ERROR: Partition table not found\n");
4866 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304867 }
Mao Flynn7b379f32015-04-20 00:28:30 +08004868
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304869 ptn = ptable_find(ptable, "splash");
4870 if (ptn == NULL) {
4871 dprintf(CRITICAL, "ERROR: splash Partition not found\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08004872 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304873 }
Mao Flynn7b379f32015-04-20 00:28:30 +08004874 if (flash_read(ptn, 0, (void *)logo_header, LOGO_IMG_HEADER_SIZE)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304875 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08004876 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304877 }
4878
Mao Flynn7b379f32015-04-20 00:28:30 +08004879 header = (struct logo_img_header *)logo_header;
4880 if (splash_screen_check_header(header)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304881 dprintf(CRITICAL, "ERROR: Boot image header invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08004882 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304883 }
4884
4885 fb_display = fbcon_display();
4886 if (fb_display) {
raghavendra ambadas554f4fd2018-10-09 15:50:40 +05304887 if (header->type && (header->blocks != 0) &&
4888 (UINT_MAX >= header->blocks * 512) &&
4889 ((header->blocks * 512) <= (fb_display->width *
4890 fb_display->height * (fb_display->bpp / 8)))) {
4891 /* RLE24 compressed data */
Mao Flynn7b379f32015-04-20 00:28:30 +08004892 uint8_t *base = (uint8_t *) fb_display->base + LOGO_IMG_OFFSET;
4893
4894 /* if the logo is full-screen size, remove "fbcon_clear()" */
4895 if ((header->width != fb_display->width)
4896 || (header->height != fb_display->height))
4897 fbcon_clear();
4898
4899 if (flash_read(ptn + LOGO_IMG_HEADER_SIZE, 0,
4900 (uint32_t *)base,
4901 (header->blocks * 512))) {
4902 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
4903 return -1;
4904 }
4905 fbcon_extract_to_screen(header, base);
4906 return 0;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07004907 }
Mao Flynn7b379f32015-04-20 00:28:30 +08004908
Veera Sundaram Sankaranc33416f2015-09-28 10:25:55 -07004909 if ((header->width > fb_display->width) || (header->height > fb_display->height)) {
4910 dprintf(CRITICAL, "Logo config greater than fb config. Fall back default logo\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08004911 return -1;
4912 }
4913
Vineet Bajaj99291ed2014-09-09 12:29:46 +05304914 uint8_t *base = (uint8_t *) fb_display->base;
Sachin Bhayare619e9e42017-05-15 13:10:31 +05304915 uint32_t fb_size = ROUNDUP(fb_display->width *
4916 fb_display->height *
4917 (fb_display->bpp / 8), 4096);
4918 uint32_t splash_size = ((((header->width * header->height *
4919 fb_display->bpp/8) + 511) >> 9) << 9);
4920
4921 if (splash_size > fb_size) {
4922 dprintf(CRITICAL, "ERROR: Splash image size invalid\n");
4923 return -1;
4924 }
4925
Mao Flynn7b379f32015-04-20 00:28:30 +08004926 if (flash_read(ptn + LOGO_IMG_HEADER_SIZE, 0,
4927 (uint32_t *)base,
4928 ((((header->width * header->height * fb_display->bpp/8) + 511) >> 9) << 9))) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304929 fbcon_clear();
Vineet Bajaj99291ed2014-09-09 12:29:46 +05304930 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08004931 return -1;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07004932 }
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304933 }
4934
Mao Flynn7b379f32015-04-20 00:28:30 +08004935 return 0;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304936}
4937
Mao Flynn7b379f32015-04-20 00:28:30 +08004938int splash_screen_mmc()
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304939{
4940 int index = INVALID_PTN;
4941 unsigned long long ptn = 0;
4942 struct fbcon_config *fb_display = NULL;
Mao Flynn7b379f32015-04-20 00:28:30 +08004943 struct logo_img_header *header;
Mao Flynn1893a7f2015-06-03 12:03:36 +08004944 uint32_t blocksize, realsize, readsize;
4945 uint8_t *base;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304946
4947 index = partition_get_index("splash");
4948 if (index == 0) {
4949 dprintf(CRITICAL, "ERROR: splash Partition table not found\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08004950 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304951 }
4952
4953 ptn = partition_get_offset(index);
4954 if (ptn == 0) {
4955 dprintf(CRITICAL, "ERROR: splash Partition invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08004956 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304957 }
4958
Mao Flynn1893a7f2015-06-03 12:03:36 +08004959 mmc_set_lun(partition_get_lun(index));
4960
4961 blocksize = mmc_get_device_blocksize();
4962 if (blocksize == 0) {
4963 dprintf(CRITICAL, "ERROR:splash Partition invalid blocksize\n");
4964 return -1;
4965 }
4966
4967 fb_display = fbcon_display();
Channagoud Kadabidaf10a62015-07-22 11:51:55 -07004968 if (!fb_display)
4969 {
4970 dprintf(CRITICAL, "ERROR: fb config is not allocated\n");
4971 return -1;
4972 }
4973
Mao Flynn1893a7f2015-06-03 12:03:36 +08004974 base = (uint8_t *) fb_display->base;
4975
jialongjhan1efba002020-05-22 18:25:56 +08004976 //[Arima][8901][JialongJhan]splash image partition offset might be incorrect 20190503 Start
4977 if (mmc_read(ptn , (uint32_t *)(base + LOGO_IMG_OFFSET), blocksize)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304978 dprintf(CRITICAL, "ERROR: Cannot read splash image header\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08004979 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304980 }
jialongjhan1efba002020-05-22 18:25:56 +08004981 //[Arima][8901][JialongJhan]splash image partition offset might be incorrect 20190503 End
Mao Flynn1893a7f2015-06-03 12:03:36 +08004982 header = (struct logo_img_header *)(base + LOGO_IMG_OFFSET);
Mao Flynn7b379f32015-04-20 00:28:30 +08004983 if (splash_screen_check_header(header)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304984 dprintf(CRITICAL, "ERROR: Splash image header invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08004985 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304986 }
4987
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304988 if (fb_display) {
raghavendra ambadas554f4fd2018-10-09 15:50:40 +05304989 if (header->type && (header->blocks != 0) &&
4990 (UINT_MAX >= header->blocks * 512 + LOGO_IMG_HEADER_SIZE) &&
4991 ((header->blocks * 512) <= (fb_display->width *
4992 fb_display->height * (fb_display->bpp / 8)))) {
4993 /* 1 RLE24 compressed data */
Mao Flynn1893a7f2015-06-03 12:03:36 +08004994 base += LOGO_IMG_OFFSET;
Mao Flynn7b379f32015-04-20 00:28:30 +08004995
Mao Flynn1893a7f2015-06-03 12:03:36 +08004996 realsize = header->blocks * 512;
4997 readsize = ROUNDUP((realsize + LOGO_IMG_HEADER_SIZE), blocksize) - blocksize;
4998
4999 /* if the logo is not full-screen size, clean screen */
Mao Flynn7b379f32015-04-20 00:28:30 +08005000 if ((header->width != fb_display->width)
5001 || (header->height != fb_display->height))
5002 fbcon_clear();
5003
Sachin Bhayare619e9e42017-05-15 13:10:31 +05305004 uint32_t fb_size = ROUNDUP(fb_display->width *
5005 fb_display->height *
5006 (fb_display->bpp / 8), 4096);
5007
5008 if (readsize > fb_size) {
5009 dprintf(CRITICAL, "ERROR: Splash image size invalid\n");
5010 return -1;
5011 }
5012
jialongjhan1efba002020-05-22 18:25:56 +08005013 //[Arima][8901][JialongJhan]splash image partition offset might be incorrect 20190503 Start
5014 if (mmc_read(ptn + blocksize, (uint32_t *)(base + blocksize), readsize)) {
Mao Flynn7b379f32015-04-20 00:28:30 +08005015 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
5016 return -1;
5017 }
jialongjhan1efba002020-05-22 18:25:56 +08005018 //[Arima][8901][JialongJhan]splash image partition offset might be incorrect 20190503 End
Mao Flynn7b379f32015-04-20 00:28:30 +08005019
Mao Flynn1893a7f2015-06-03 12:03:36 +08005020 fbcon_extract_to_screen(header, (base + LOGO_IMG_HEADER_SIZE));
5021 } else { /* 2 Raw BGR data */
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05305022
Veera Sundaram Sankaranc33416f2015-09-28 10:25:55 -07005023 if ((header->width > fb_display->width) || (header->height > fb_display->height)) {
5024 dprintf(CRITICAL, "Logo config greater than fb config. Fall back default logo\n");
Mao Flynn1893a7f2015-06-03 12:03:36 +08005025 return -1;
5026 }
5027
5028 realsize = header->width * header->height * fb_display->bpp / 8;
5029 readsize = ROUNDUP((realsize + LOGO_IMG_HEADER_SIZE), blocksize) - blocksize;
5030
5031 if (blocksize == LOGO_IMG_HEADER_SIZE) { /* read the content directly */
Kishor PKee5c0a32018-03-06 16:49:46 +05305032 if (mmc_read((ptn + PLL_CODES_OFFSET + LOGO_IMG_HEADER_SIZE), (uint32_t *)base, readsize)) {
Mao Flynn1893a7f2015-06-03 12:03:36 +08005033 fbcon_clear();
5034 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
5035 return -1;
5036 }
5037 } else {
Kishor PKee5c0a32018-03-06 16:49:46 +05305038 if (mmc_read(ptn + PLL_CODES_OFFSET + blocksize ,
Mao Flynn1893a7f2015-06-03 12:03:36 +08005039 (uint32_t *)(base + LOGO_IMG_OFFSET + blocksize), readsize)) {
5040 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
5041 return -1;
5042 }
5043 memmove(base, (base + LOGO_IMG_OFFSET + LOGO_IMG_HEADER_SIZE), realsize);
5044 }
5045 }
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05305046 }
5047
Mao Flynn7b379f32015-04-20 00:28:30 +08005048 return 0;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05305049}
5050
Mao Flynn7b379f32015-04-20 00:28:30 +08005051int fetch_image_from_partition()
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05305052{
5053 if (target_is_emmc_boot()) {
5054 return splash_screen_mmc();
5055 } else {
5056 return splash_screen_flash();
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07005057 }
5058}
5059
Mayank Grover351a75e2017-05-30 20:06:08 +05305060void publish_getvar_multislot_vars()
5061{
5062 int i,count;
5063 static bool published = false;
5064 static char slot_count[MAX_RSP_SIZE];
5065 static struct ab_slot_info slot_info[AB_SUPPORTED_SLOTS];
5066 static char active_slot_suffix[MAX_RSP_SIZE];
5067 static char has_slot_pname[NUM_PARTITIONS][MAX_GET_VAR_NAME_SIZE];
5068 static char has_slot_reply[NUM_PARTITIONS][MAX_RSP_SIZE];
5069 const char *tmp;
5070 char tmpbuff[MAX_GET_VAR_NAME_SIZE];
Mayank Grover2bd84bb2017-06-20 11:06:07 +05305071 signed active_slt;
Mayank Grover351a75e2017-05-30 20:06:08 +05305072
5073 if (!published)
5074 {
5075 /* Update slot meta info */
5076 count = partition_fill_partition_meta(has_slot_pname, has_slot_reply,
5077 partition_get_partition_count());
5078 for(i=0; i<count; i++)
5079 {
5080 memset(tmpbuff, 0, MAX_GET_VAR_NAME_SIZE);
Mayank Grover2bd84bb2017-06-20 11:06:07 +05305081 snprintf(tmpbuff, MAX_GET_VAR_NAME_SIZE,"has-slot:%s",
5082 has_slot_pname[i]);
5083 strlcpy(has_slot_pname[i], tmpbuff, MAX_GET_VAR_NAME_SIZE);
Mayank Grover351a75e2017-05-30 20:06:08 +05305084 fastboot_publish(has_slot_pname[i], has_slot_reply[i]);
5085 }
5086
5087 for (i=0; i<AB_SUPPORTED_SLOTS; i++)
5088 {
5089 tmp = SUFFIX_SLOT(i);
Mayank Grover7b8a8f82017-10-27 13:07:59 +05305090 tmp++; // to remove "_" from slot_suffix.
Mayank Grover2bd84bb2017-06-20 11:06:07 +05305091 snprintf(slot_info[i].slot_is_unbootable, sizeof(slot_info[i].slot_is_unbootable),
5092 "slot-unbootable:%s", tmp);
5093 snprintf(slot_info[i].slot_is_active, sizeof(slot_info[i].slot_is_active),
5094 "slot-active:%s", tmp);
5095 snprintf(slot_info[i].slot_is_succesful, sizeof(slot_info[i].slot_is_succesful),
5096 "slot-success:%s", tmp);
5097 snprintf(slot_info[i].slot_retry_count, sizeof(slot_info[i].slot_retry_count),
5098 "slot-retry-count:%s", tmp);
Mayank Grover351a75e2017-05-30 20:06:08 +05305099 fastboot_publish(slot_info[i].slot_is_unbootable,
5100 slot_info[i].slot_is_unbootable_rsp);
5101 fastboot_publish(slot_info[i].slot_is_active,
5102 slot_info[i].slot_is_active_rsp);
5103 fastboot_publish(slot_info[i].slot_is_succesful,
5104 slot_info[i].slot_is_succesful_rsp);
5105 fastboot_publish(slot_info[i].slot_retry_count,
5106 slot_info[i].slot_retry_count_rsp);
5107 }
5108 fastboot_publish("current-slot", active_slot_suffix);
5109 snprintf(slot_count, sizeof(slot_count),"%d", AB_SUPPORTED_SLOTS);
5110 fastboot_publish("slot-count", slot_count);
5111 published = true;
5112 }
5113
Mayank Grover2bd84bb2017-06-20 11:06:07 +05305114 active_slt = partition_find_active_slot();
5115 if (active_slt != INVALID)
Mayank Grover7b8a8f82017-10-27 13:07:59 +05305116 {
5117 tmp = SUFFIX_SLOT(active_slt);
5118 tmp++; // to remove "_" from slot_suffix.
5119 snprintf(active_slot_suffix, sizeof(active_slot_suffix), "%s", tmp);
5120 }
Mayank Grover2bd84bb2017-06-20 11:06:07 +05305121 else
5122 strlcpy(active_slot_suffix, "INVALID", sizeof(active_slot_suffix));
5123
Mayank Grover351a75e2017-05-30 20:06:08 +05305124 /* Update partition meta information */
5125 partition_fill_slot_meta(slot_info);
5126 return;
5127}
5128
lijuang4ece1e72015-08-14 21:02:36 +08005129void get_product_name(unsigned char *buf)
5130{
5131 snprintf((char*)buf, MAX_RSP_SIZE, "%s", TARGET(BOARD));
5132 return;
5133}
5134
Mayank Grover7b8a8f82017-10-27 13:07:59 +05305135#if PRODUCT_IOT
5136void get_bootloader_version_iot(unsigned char *buf)
5137{
5138 if (buf != NULL)
5139 {
5140 strlcpy(buf, TARGET(BOARD), MAX_VERSION_LEN);
5141 strlcat(buf, "-", MAX_VERSION_LEN);
5142 strlcat(buf, PRODUCT_IOT_VERSION, MAX_VERSION_LEN);
5143 }
5144 return;
5145}
5146#endif
5147
lijuang4ece1e72015-08-14 21:02:36 +08005148void get_bootloader_version(unsigned char *buf)
5149{
jhchen7a504d12020-04-24 15:45:57 +08005150 snprintf((char*)buf, MAX_RSP_SIZE, "%s", device.bootloader_version);
lijuang4ece1e72015-08-14 21:02:36 +08005151 return;
5152}
5153
5154void get_baseband_version(unsigned char *buf)
5155{
jhchen7a504d12020-04-24 15:45:57 +08005156 snprintf((char*)buf, MAX_RSP_SIZE, "%s", device.radio_version);
lijuang4ece1e72015-08-14 21:02:36 +08005157 return;
5158}
5159
Monika Singh32a09f72018-03-14 13:08:29 +05305160bool is_device_locked_critical()
5161{
5162 return device.is_unlock_critical ? false:true;
5163}
5164
lijuang4ece1e72015-08-14 21:02:36 +08005165bool is_device_locked()
5166{
5167 return device.is_unlocked ? false:true;
5168}
5169
Monika Singh32a09f72018-03-14 13:08:29 +05305170bool is_verity_enforcing()
5171{
5172 return device.verity_mode ? true:false;
5173}
5174
Amol Jadi5edf3552013-07-23 14:15:34 -07005175/* register commands and variables for fastboot */
5176void aboot_fastboot_register_commands(void)
5177{
Sridhar Parasurame94e8152014-10-24 14:06:03 -07005178 int i;
lijuangf16461c2015-08-03 17:09:34 +08005179 char hw_platform_buf[MAX_RSP_SIZE];
Amol Jadi5edf3552013-07-23 14:15:34 -07005180
Sridhar Parasurame94e8152014-10-24 14:06:03 -07005181 struct fastboot_cmd_desc cmd_list[] = {
lijuang511a2b52015-08-14 20:50:51 +08005182 /* By default the enabled list is empty. */
5183 {"", NULL},
5184 /* move commands enclosed within the below ifndef to here
5185 * if they need to be enabled in user build.
5186 */
Sridhar Parasurame94e8152014-10-24 14:06:03 -07005187#ifndef DISABLE_FASTBOOT_CMDS
lijuang511a2b52015-08-14 20:50:51 +08005188 /* Register the following commands only for non-user builds */
5189 {"flash:", cmd_flash},
5190 {"erase:", cmd_erase},
5191 {"boot", cmd_boot},
5192 {"continue", cmd_continue},
5193 {"reboot", cmd_reboot},
5194 {"reboot-bootloader", cmd_reboot_bootloader},
5195 {"oem unlock", cmd_oem_unlock},
5196 {"oem unlock-go", cmd_oem_unlock_go},
5197 {"oem lock", cmd_oem_lock},
5198 {"flashing unlock", cmd_oem_unlock},
5199 {"flashing lock", cmd_oem_lock},
5200 {"flashing lock_critical", cmd_flashing_lock_critical},
5201 {"flashing unlock_critical", cmd_flashing_unlock_critical},
5202 {"flashing get_unlock_ability", cmd_flashing_get_unlock_ability},
5203 {"oem device-info", cmd_oem_devinfo},
5204 {"preflash", cmd_preflash},
5205 {"oem enable-charger-screen", cmd_oem_enable_charger_screen},
5206 {"oem disable-charger-screen", cmd_oem_disable_charger_screen},
lijuanga25d8bb2015-09-16 13:11:52 +08005207 {"oem off-mode-charge", cmd_oem_off_mode_charger},
lijuang511a2b52015-08-14 20:50:51 +08005208 {"oem select-display-panel", cmd_oem_select_display_panel},
Mayank Grover6ccc1c92017-07-04 17:36:46 +05305209 {"set_active",cmd_set_active},
Mayank Grover98c4c742019-04-25 17:21:37 +05305210#if DYNAMIC_PARTITION_SUPPORT
5211 {"reboot-fastboot",cmd_reboot_fastboot},
5212 {"reboot-recovery",cmd_reboot_recovery},
5213#endif
Channagoud Kadabi736c4962015-08-21 11:56:52 -07005214#if UNITTEST_FW_SUPPORT
Channagoud Kadabid5ddf482015-09-28 10:31:35 -07005215 {"oem run-tests", cmd_oem_runtests},
Channagoud Kadabi736c4962015-08-21 11:56:52 -07005216#endif
Sridhar Parasurame94e8152014-10-24 14:06:03 -07005217#endif
lijuang511a2b52015-08-14 20:50:51 +08005218 };
Sridhar Parasurame94e8152014-10-24 14:06:03 -07005219
5220 int fastboot_cmds_count = sizeof(cmd_list)/sizeof(cmd_list[0]);
5221 for (i = 1; i < fastboot_cmds_count; i++)
5222 fastboot_register(cmd_list[i].name,cmd_list[i].cb);
5223
Amol Jadi5edf3552013-07-23 14:15:34 -07005224 /* publish variables and their values */
5225 fastboot_publish("product", TARGET(BOARD));
5226 fastboot_publish("kernel", "lk");
5227 fastboot_publish("serialno", sn_buf);
5228
Mayank Grovera64dfbe2018-05-17 14:06:34 +05305229 /*publish hw-revision major(upper 16 bits) and minor(lower 16 bits)*/
5230 snprintf(soc_version_str, MAX_RSP_SIZE, "%x", board_soc_version());
5231 fastboot_publish("hw-revision", soc_version_str);
5232
Amol Jadi5edf3552013-07-23 14:15:34 -07005233 /*
5234 * partition info is supported only for emmc partitions
5235 * Calling this for NAND prints some error messages which
5236 * is harmless but misleading. Avoid calling this for NAND
5237 * devices.
5238 */
5239 if (target_is_emmc_boot())
Mayank Grover7b8a8f82017-10-27 13:07:59 +05305240 publish_getvar_partition_info(part_info, partition_get_partition_count());
Amol Jadi5edf3552013-07-23 14:15:34 -07005241
Mayank Grover351a75e2017-05-30 20:06:08 +05305242 if (partition_multislot_is_supported())
5243 publish_getvar_multislot_vars();
5244
Amol Jadi5edf3552013-07-23 14:15:34 -07005245 /* Max download size supported */
lijuang2fbed912019-07-12 14:15:05 +08005246#if !VERIFIED_BOOT_2
Ameya Thakur11cf1a62013-08-05 12:44:48 -07005247 snprintf(max_download_size, MAX_RSP_SIZE, "\t0x%x",
5248 target_get_max_flash_size());
lijuang2fbed912019-07-12 14:15:05 +08005249#else
5250 snprintf(max_download_size, MAX_RSP_SIZE, "\t0x%x",
5251 SUB_SALT_BUFF_OFFSET(target_get_max_flash_size()));
5252#endif
5253
Amol Jadi5edf3552013-07-23 14:15:34 -07005254 fastboot_publish("max-download-size", (const char *) max_download_size);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07005255 /* Is the charger screen check enabled */
5256 snprintf(charger_screen_enabled, MAX_RSP_SIZE, "%d",
5257 device.charger_screen_enabled);
5258 fastboot_publish("charger-screen-enabled",
5259 (const char *) charger_screen_enabled);
lijuanga25d8bb2015-09-16 13:11:52 +08005260 fastboot_publish("off-mode-charge", (const char *) charger_screen_enabled);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05305261 snprintf(panel_display_mode, MAX_RSP_SIZE, "%s",
5262 device.display_panel);
5263 fastboot_publish("display-panel",
5264 (const char *) panel_display_mode);
Mukesh Ojhabb6e7f32018-03-02 15:04:06 +05305265
5266 if (target_is_emmc_boot())
5267 {
5268 mmc_blocksize = mmc_get_device_blocksize();
5269 }
5270 else
5271 {
5272 mmc_blocksize = flash_block_size();
5273 }
5274 snprintf(block_size_string, MAX_RSP_SIZE, "0x%x", mmc_blocksize);
5275 fastboot_publish("erase-block-size", (const char *) block_size_string);
5276 fastboot_publish("logical-block-size", (const char *) block_size_string);
Mayank Grover7b8a8f82017-10-27 13:07:59 +05305277#if PRODUCT_IOT
5278 get_bootloader_version_iot(&bootloader_version_string);
5279 fastboot_publish("version-bootloader", (const char *) bootloader_version_string);
5280
5281 /* Version baseband is n/a for apq iot devices */
5282 fastboot_publish("version-baseband", "N/A");
Mayank Grover7b8a8f82017-10-27 13:07:59 +05305283#else
Channagoud Kadabiaafbbfc2015-03-04 17:52:56 -08005284 fastboot_publish("version-bootloader", (const char *) device.bootloader_version);
5285 fastboot_publish("version-baseband", (const char *) device.radio_version);
Mayank Grover7b8a8f82017-10-27 13:07:59 +05305286#endif
lijuangf16461c2015-08-03 17:09:34 +08005287 fastboot_publish("secure", is_secure_boot_enable()? "yes":"no");
Mayank Grover7b8a8f82017-10-27 13:07:59 +05305288 fastboot_publish("unlocked", device.is_unlocked ? "yes":"no");
lijuangf16461c2015-08-03 17:09:34 +08005289 smem_get_hw_platform_name((unsigned char *) hw_platform_buf, sizeof(hw_platform_buf));
5290 snprintf(get_variant, MAX_RSP_SIZE, "%s %s", hw_platform_buf,
5291 target_is_emmc_boot()? "eMMC":"UFS");
5292 fastboot_publish("variant", (const char *) get_variant);
lijuang65c5a822015-08-29 16:35:36 +08005293#if CHECK_BAT_VOLTAGE
lijuang102dfa92015-10-09 18:31:03 +08005294 update_battery_status();
lijuang65c5a822015-08-29 16:35:36 +08005295 fastboot_publish("battery-voltage", (const char *) battery_voltage);
lijuang102dfa92015-10-09 18:31:03 +08005296 fastboot_publish("battery-soc-ok", (const char *) battery_soc_ok);
lijuang65c5a822015-08-29 16:35:36 +08005297#endif
Mayank Grover98c4c742019-04-25 17:21:37 +05305298 if (target_dynamic_partition_supported())
5299 fastboot_publish("is-userspace", "no");
Amol Jadi5edf3552013-07-23 14:15:34 -07005300}
5301
Brian Swetland9c4c0752009-01-25 16:23:50 -08005302void aboot_init(const struct app_descriptor *app)
5303{
Shashank Mittal4f99a882010-02-01 13:58:50 -08005304 unsigned reboot_mode = 0;
Mayank Grover351a75e2017-05-30 20:06:08 +05305305 int boot_err_type = 0;
5306 int boot_slot = INVALID;
jessicatseng67c4fd02020-05-26 13:17:56 +08005307//<2020/05/26-JessicaTseng, Add low battery icon
5308 int vbat = 0;
5309 char boot_vbat[MAX_RSP_SIZE];
5310//>2020/05/26-JessicaTseng
Chandan Uddarajubedca152010-06-02 23:05:15 -07005311
lijuang39831732016-01-08 17:49:02 +08005312 /* Initialise wdog to catch early lk crashes */
5313#if WDOG_SUPPORT
5314 msm_wdog_init();
5315#endif
5316
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07005317 /* Setup page size information for nv storage */
Shashank Mittald8c42bf2010-06-09 15:44:28 -07005318 if (target_is_emmc_boot())
5319 {
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07005320 page_size = mmc_page_size();
Shashank Mittald8c42bf2010-06-09 15:44:28 -07005321 page_mask = page_size - 1;
Gaurav Nebhwani4d2389c2016-03-17 21:10:05 +05305322 mmc_blocksize = mmc_get_device_blocksize();
5323 mmc_blocksize_mask = mmc_blocksize - 1;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07005324 }
5325 else
5326 {
5327 page_size = flash_page_size();
5328 page_mask = page_size - 1;
5329 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07005330 ASSERT((MEMBASE + MEMSIZE) > MEMBASE);
5331
Ameya Thakur11cf1a62013-08-05 12:44:48 -07005332 read_device_info(&device);
vijay kumarc65876c2015-04-24 13:29:16 +05305333 read_allow_oem_unlock(&device);
Shashank Mittal162244e2011-08-08 19:01:25 -07005334
Mayank Grover351a75e2017-05-30 20:06:08 +05305335 /* Detect multi-slot support */
5336 if (partition_multislot_is_supported())
5337 {
5338 boot_slot = partition_find_active_slot();
5339 if (boot_slot == INVALID)
5340 {
5341 boot_into_fastboot = true;
5342 dprintf(INFO, "Active Slot: (INVALID)\n");
5343 }
5344 else
5345 {
5346 /* Setting the state of system to boot active slot */
5347 partition_mark_active_slot(boot_slot);
5348 dprintf(INFO, "Active Slot: (%s)\n", SUFFIX_SLOT(boot_slot));
5349 }
5350 }
5351
Greg Griscod6250552011-06-29 14:40:23 -07005352 target_serialno((unsigned char *) sn_buf);
Ajay Dudanib06c05f2011-05-12 14:46:10 -07005353 dprintf(SPEW,"serial number: %s\n",sn_buf);
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -08005354
Dhaval Patel223ec952013-07-18 14:49:44 -07005355 memset(display_panel_buf, '\0', MAX_PANEL_BUF_SIZE);
5356
Matthew Qindefd5562014-07-11 18:02:40 +08005357 /*
5358 * Check power off reason if user force reset,
5359 * if yes phone will do normal boot.
5360 */
5361 if (is_user_force_reset())
5362 goto normal_boot;
5363
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07005364 /* Check if we should do something other than booting up */
Ameya Thakur0b9c2442013-05-10 13:22:56 -07005365 if (keys_get_state(KEY_VOLUMEUP) && keys_get_state(KEY_VOLUMEDOWN))
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07005366 {
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03005367 dprintf(ALWAYS,"dload mode key sequence detected\n");
lijuang395b5e62015-11-19 17:39:44 +08005368 reboot_device(EMERGENCY_DLOAD);
5369 dprintf(CRITICAL,"Failed to reboot into dload mode\n");
5370
Ameya Thakur0b9c2442013-05-10 13:22:56 -07005371 boot_into_fastboot = true;
5372 }
5373 if (!boot_into_fastboot)
5374 {
5375 if (keys_get_state(KEY_HOME) || keys_get_state(KEY_VOLUMEUP))
5376 boot_into_recovery = 1;
5377 if (!boot_into_recovery &&
5378 (keys_get_state(KEY_BACK) || keys_get_state(KEY_VOLUMEDOWN)))
Pavel Nedev5d91d412013-04-29 11:34:24 +03005379 boot_into_fastboot = true;
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07005380 }
Shashank Mittald8c42bf2010-06-09 15:44:28 -07005381 #if NO_KEYPAD_DRIVER
Kinson Chik0b1c8162011-08-31 16:31:57 -07005382 if (fastboot_trigger())
Pavel Nedev5d91d412013-04-29 11:34:24 +03005383 boot_into_fastboot = true;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07005384 #endif
Chandan Uddarajubedca152010-06-02 23:05:15 -07005385
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07005386#if USE_PON_REBOOT_REG
5387 reboot_mode = check_hard_reboot_mode();
5388#else
Ajay Dudani77421292010-10-27 19:34:06 -07005389 reboot_mode = check_reboot_mode();
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07005390#endif
5391 if (reboot_mode == RECOVERY_MODE)
5392 {
Ajay Dudani77421292010-10-27 19:34:06 -07005393 boot_into_recovery = 1;
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07005394 }
5395 else if(reboot_mode == FASTBOOT_MODE)
5396 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03005397 boot_into_fastboot = true;
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07005398 }
5399 else if(reboot_mode == ALARM_BOOT)
5400 {
Matthew Qind886f3c2014-01-17 16:52:01 +08005401 boot_reason_alarm = true;
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07005402 }
Monika Singhb0db4b82018-09-26 12:18:02 +05305403#if VERIFIED_BOOT || VERIFIED_BOOT_2
Mayank Grover912eaa62017-10-26 12:08:53 +05305404 else if (VB_M <= target_get_vb_version())
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07005405 {
Mayank Grover889be1b2017-09-12 20:12:23 +05305406 if (reboot_mode == DM_VERITY_ENFORCING)
5407 {
5408 device.verity_mode = 1;
5409 write_device_info(&device);
5410 }
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07005411#if ENABLE_VB_ATTEST
Mayank Grover889be1b2017-09-12 20:12:23 +05305412 else if (reboot_mode == DM_VERITY_EIO)
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07005413#else
Mayank Grover889be1b2017-09-12 20:12:23 +05305414 else if (reboot_mode == DM_VERITY_LOGGING)
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07005415#endif
Mayank Grover889be1b2017-09-12 20:12:23 +05305416 {
5417 device.verity_mode = 0;
5418 write_device_info(&device);
5419 }
5420 else if (reboot_mode == DM_VERITY_KEYSCLEAR)
5421 {
5422 if(send_delete_keys_to_tz())
5423 ASSERT(0);
5424 }
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07005425 }
Parth Dixitddbc7352015-10-18 03:13:31 +05305426#endif
Ajay Dudani77421292010-10-27 19:34:06 -07005427
jialongjhan1efba002020-05-22 18:25:56 +08005428//[Arima][8901][JialongJhan][20200522]modify Display splash screen if enabled Start
5429 /* Display splash screen if enabled */
5430#if DISPLAY_SPLASH_SCREEN
5431#if NO_ALARM_DISPLAY
5432 if (!check_alarm_boot()) {
5433#endif
5434 dprintf(SPEW, "Display Init: Start\n");
5435#if DISPLAY_HDMI_PRIMARY
5436 if (!strlen(device.display_panel))
5437 strlcpy(device.display_panel, DISPLAY_PANEL_HDMI,
5438 sizeof(device.display_panel));
5439#endif
5440#if ENABLE_WBC
5441 /* Wait if the display shutdown is in progress */
5442 while(pm_app_display_shutdown_in_prgs());
5443 if (!pm_appsbl_display_init_done())
5444 target_display_init(device.display_panel);
5445 else
5446 display_image_on_screen();
5447#else
5448 target_display_init(device.display_panel);
5449#endif
5450 dprintf(SPEW, "Display Init: Done\n");
5451#if NO_ALARM_DISPLAY
5452 }
5453#endif
5454#endif
5455
5456//[Arima][8901][JialongJhan][20200522]modify Display splash screen if enabled End
5457
Matthew Qindefd5562014-07-11 18:02:40 +08005458normal_boot:
Pavel Nedev5d91d412013-04-29 11:34:24 +03005459 if (!boot_into_fastboot)
Shashank Mittald8c42bf2010-06-09 15:44:28 -07005460 {
jessicatseng67c4fd02020-05-26 13:17:56 +08005461//<2020/05/26-JessicaTseng, Add low battery icon
5462 if(!target_pause_for_battery_charge())
5463 {
5464 vbat = target_get_battery_voltage();
5465 snprintf(boot_vbat, MAX_RSP_SIZE, "%d", vbat);
5466 dprintf(CRITICAL,"battery_voltage: %s\n", boot_vbat);
5467 if(vbat < 3500000)
5468 {
5469 display_lowbattery_image_on_screen();
5470 //[Arima][8901][JialongJhan] Command mode reflash screen when low battery logo shown 20190516 Start
5471 msm_display_flush();
5472 //[Arima][8901][JialongJhan] Command mode reflash screen when low battery logo shown 20190516 End
5473 dprintf(CRITICAL,"Low battery, cannot boot up...\n");
5474 mdelay(3000);
5475 shutdown_device();
5476 }
5477 }
5478//>2020/05/26-JessicaTseng
5479
Pavel Nedev5d91d412013-04-29 11:34:24 +03005480 if (target_is_emmc_boot())
Shashank Mittala0032282011-08-26 14:50:11 -07005481 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03005482 if(emmc_recovery_init())
5483 dprintf(ALWAYS,"error in emmc_recovery_init\n");
5484 if(target_use_signed_kernel())
Shashank Mittala0032282011-08-26 14:50:11 -07005485 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03005486 if((device.is_unlocked) || (device.is_tampered))
5487 {
5488 #ifdef TZ_TAMPER_FUSE
Monika Singh98257462018-06-06 11:28:49 +05305489 set_tamper_fuse_cmd(HLOS_IMG_TAMPER_FUSE);
Pavel Nedev5d91d412013-04-29 11:34:24 +03005490 #endif
5491 #if USE_PCOM_SECBOOT
5492 set_tamper_flag(device.is_tampered);
5493 #endif
5494 }
Shashank Mittala0032282011-08-26 14:50:11 -07005495 }
Amit Blay6281ebc2015-01-11 14:44:08 +02005496
Mayank Grover351a75e2017-05-30 20:06:08 +05305497retry_boot:
5498 /* Trying to boot active partition */
5499 if (partition_multislot_is_supported())
5500 {
5501 boot_slot = partition_find_boot_slot();
5502 partition_mark_active_slot(boot_slot);
5503 if (boot_slot == INVALID)
5504 goto fastboot;
5505 }
5506
5507 boot_err_type = boot_linux_from_mmc();
5508 switch (boot_err_type)
5509 {
5510 case ERR_INVALID_PAGE_SIZE:
5511 case ERR_DT_PARSE:
5512 case ERR_ABOOT_ADDR_OVERLAP:
Mayank Grover10cabbe2017-10-30 19:11:05 +05305513 case ERR_INVALID_BOOT_MAGIC:
Mayank Grover351a75e2017-05-30 20:06:08 +05305514 if(partition_multislot_is_supported())
Mayank Grover10cabbe2017-10-30 19:11:05 +05305515 {
5516 /*
5517 * Deactivate current slot, as it failed to
5518 * boot, and retry next slot.
5519 */
5520 partition_deactivate_slot(boot_slot);
Mayank Grover351a75e2017-05-30 20:06:08 +05305521 goto retry_boot;
Mayank Grover10cabbe2017-10-30 19:11:05 +05305522 }
Mayank Grover351a75e2017-05-30 20:06:08 +05305523 else
5524 break;
Mayank Grover351a75e2017-05-30 20:06:08 +05305525 default:
5526 break;
5527 /* going to fastboot menu */
5528 }
Shashank Mittala0032282011-08-26 14:50:11 -07005529 }
Pavel Nedev5d91d412013-04-29 11:34:24 +03005530 else
5531 {
5532 recovery_init();
5533 #if USE_PCOM_SECBOOT
5534 if((device.is_unlocked) || (device.is_tampered))
5535 set_tamper_flag(device.is_tampered);
5536 #endif
5537 boot_linux_from_flash();
5538 }
5539 dprintf(CRITICAL, "ERROR: Could not do normal boot. Reverting "
5540 "to fastboot mode.\n");
Shashank Mittald8c42bf2010-06-09 15:44:28 -07005541 }
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07005542
Mayank Grover351a75e2017-05-30 20:06:08 +05305543fastboot:
jialongjhan1efba002020-05-22 18:25:56 +08005544
5545 //[Arima][8901][JialongJhan]Show Fastboot logo 1 second 20190627 Start
5546 mdelay(1000);
5547 //[Arima][8901][JialongJhan]Show Fastboot logo 1 second 20190627 End
5548
Amol Jadi5edf3552013-07-23 14:15:34 -07005549 /* We are here means regular boot did not happen. Start fastboot. */
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07005550
Amol Jadi5edf3552013-07-23 14:15:34 -07005551 /* register aboot specific fastboot commands */
5552 aboot_fastboot_register_commands();
Amol Jadi57abe4c2011-05-24 15:47:27 -07005553
Amol Jadi5edf3552013-07-23 14:15:34 -07005554 /* dump partition table for debug info */
Kinson Chikf1a43512011-07-14 11:28:39 -07005555 partition_dump();
Amol Jadi5edf3552013-07-23 14:15:34 -07005556
5557 /* initialize and start fastboot */
Saranya Chidura41b34532018-10-16 12:29:52 +05305558#if !VERIFIED_BOOT_2
Amol Jadi5edf3552013-07-23 14:15:34 -07005559 fastboot_init(target_get_scratch_address(), target_get_max_flash_size());
Saranya Chidura41b34532018-10-16 12:29:52 +05305560#else
5561 /* Add salt buffer offset at start of image address to copy VB salt */
5562 fastboot_init(ADD_SALT_BUFF_OFFSET(target_get_scratch_address()),
5563 SUB_SALT_BUFF_OFFSET(target_get_max_flash_size()));
5564#endif
lijuang4ece1e72015-08-14 21:02:36 +08005565#if FBCON_DISPLAY_MSG
lijuang9a7d3b92015-11-30 14:41:24 +08005566 display_fastboot_menu();
lijuang4ece1e72015-08-14 21:02:36 +08005567#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08005568}
5569
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07005570uint32_t get_page_size()
5571{
5572 return page_size;
5573}
5574
Amir Samuelov57a6fa22013-06-05 16:36:43 +03005575/*
5576 * Calculated and save hash (SHA256) for non-signed boot image.
5577 *
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07005578 * @param image_addr - Boot image address
5579 * @param image_size - Size of the boot image
Amir Samuelov57a6fa22013-06-05 16:36:43 +03005580 *
5581 * @return int - 0 on success, negative value on failure.
5582 */
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07005583static int aboot_save_boot_hash_mmc(uint32_t image_addr, uint32_t image_size)
Amir Samuelov57a6fa22013-06-05 16:36:43 +03005584{
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07005585 unsigned int digest[8];
5586#if IMAGE_VERIF_ALGO_SHA1
5587 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA1;
5588#else
5589 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA256;
5590#endif
Amir Samuelov57a6fa22013-06-05 16:36:43 +03005591
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07005592 target_crypto_init_params();
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -08005593 hash_find((unsigned char *) image_addr, image_size, (unsigned char *)&digest, auth_algo);
Amir Samuelov57a6fa22013-06-05 16:36:43 +03005594
5595 save_kernel_hash_cmd(digest);
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07005596 dprintf(INFO, "aboot_save_boot_hash_mmc: imagesize_actual size %d bytes.\n", (int) image_size);
Amir Samuelov57a6fa22013-06-05 16:36:43 +03005597
5598 return 0;
5599}
5600
Mayank Grover351a75e2017-05-30 20:06:08 +05305601
Brian Swetland9c4c0752009-01-25 16:23:50 -08005602APP_START(aboot)
5603 .init = aboot_init,
5604APP_END