blob: 91479fc1a40c0ea944feaf1092bec7f6232ee0dd [file] [log] [blame]
Brian Swetland9c4c0752009-01-25 16:23:50 -08001/*
2 * Copyright (c) 2009, Google Inc.
3 * All rights reserved.
4 *
Mayank Grover11ff9692018-01-11 11:54:49 +05305 * Copyright (c) 2009-2018, The Linux Foundation. All rights reserved.
Brian Swetland9c4c0752009-01-25 16:23:50 -08006 *
Chandan Uddaraju5fa471a2009-12-02 17:31:34 -08007 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070014 * * Neither the name of The Linux Foundation nor
Chandan Uddaraju5fa471a2009-12-02 17:31:34 -080015 * the names of its contributors may be used to endorse or promote
16 * products derived from this software without specific prior written
17 * permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22 * NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 *
Brian Swetland9c4c0752009-01-25 16:23:50 -080031 */
32
33#include <app.h>
34#include <debug.h>
35#include <arch/arm.h>
Brian Swetland9c4c0752009-01-25 16:23:50 -080036#include <string.h>
Channagoud Kadabi132ff552013-04-19 14:34:44 -070037#include <stdlib.h>
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -070038#include <limits.h>
Brian Swetland9c4c0752009-01-25 16:23:50 -080039#include <kernel/thread.h>
40#include <arch/ops.h>
41
Dima Zavin214cc642009-01-26 11:16:21 -080042#include <dev/flash.h>
Tanya Brokhman1c94f1a2015-02-15 09:05:03 +020043#include <dev/flash-ubi.h>
Dima Zavin214cc642009-01-26 11:16:21 -080044#include <lib/ptable.h>
Dima Zavinb4283602009-01-26 16:36:57 -080045#include <dev/keys.h>
Shashank Mittal4f99a882010-02-01 13:58:50 -080046#include <dev/fbcon.h>
Ajay Dudanid04110c2011-01-17 23:55:07 -080047#include <baseband.h>
Greg Griscod6250552011-06-29 14:40:23 -070048#include <target.h>
49#include <mmc.h>
Kinson Chikf1a43512011-07-14 11:28:39 -070050#include <partition_parser.h>
Mayank Grover351a75e2017-05-30 20:06:08 +053051#include <ab_partition_parser.h>
Greg Griscod6250552011-06-29 14:40:23 -070052#include <platform.h>
Shashank Mittalcd98d472011-08-02 14:29:24 -070053#include <crypto_hash.h>
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070054#include <malloc.h>
Amol Jadi492d5a52013-03-15 16:12:34 -070055#include <boot_stats.h>
Amir Samuelov57a6fa22013-06-05 16:36:43 +030056#include <sha.h>
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -070057#include <platform/iomap.h>
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -070058#include <boot_device.h>
Shashank Mittald3e54dd2014-08-28 15:24:02 -070059#include <boot_verifier.h>
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +053060#include <image_verify.h>
Matthew Qinbb7923d2015-02-09 10:56:09 +080061#include <decompress.h>
Unnati Gandhi17b3bfc2015-05-11 12:58:16 +053062#include <platform/timer.h>
lijuang511a2b52015-08-14 20:50:51 +080063#include <sys/types.h>
Channagoud Kadabi036c6052015-02-09 15:19:59 -080064#if USE_RPMB_FOR_DEVINFO
65#include <rpmb.h>
66#endif
Dima Zavin214cc642009-01-26 11:16:21 -080067
Channagoud Kadabi90869ce2015-04-27 11:15:14 -070068#if ENABLE_WBC
69#include <pm_app_smbchg.h>
70#endif
71
Neeti Desai17379b82012-06-04 18:42:53 -070072#if DEVICE_TREE
73#include <libfdt.h>
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070074#include <dev_tree.h>
Neeti Desai17379b82012-06-04 18:42:53 -070075#endif
76
Aparna Mallavarapu118ccae2015-06-03 13:47:11 +053077#if WDOG_SUPPORT
78#include <wdog.h>
79#endif
80
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -070081#include <reboot.h>
Shashank Mittalcd98d472011-08-02 14:29:24 -070082#include "image_verify.h"
Shashank Mittal024c0332010-02-03 11:44:00 -080083#include "recovery.h"
Brian Swetland9c4c0752009-01-25 16:23:50 -080084#include "bootimg.h"
85#include "fastboot.h"
Ajay Dudani5c761132011-04-07 20:19:04 -070086#include "sparse_format.h"
Ajay Dudanide984792015-03-02 09:57:41 -080087#include "meta_format.h"
Greg Grisco6e754772011-06-23 12:19:39 -070088#include "mmc.h"
Shashank Mittal162244e2011-08-08 19:01:25 -070089#include "devinfo.h"
Neeti Desai465491e2012-07-31 12:53:35 -070090#include "board.h"
Shashank Mittal162244e2011-08-08 19:01:25 -070091#include "scm.h"
Amit Blay6281ebc2015-01-11 14:44:08 +020092#include "mdtp.h"
Sridhar Parasuram32b30662015-07-10 13:33:22 -070093#include "secapp_loader.h"
lijuanga40d6302015-07-20 20:10:13 +080094#include <menu_keys_detect.h>
95#include <display_menu.h>
Channagoud Kadabi736c4962015-08-21 11:56:52 -070096#include "fastboot_test.h"
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -070097
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070098extern bool target_use_signed_kernel(void);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070099extern void platform_uninit(void);
Channagoud Kadabi33defe22013-06-18 18:35:40 -0700100extern void target_uninit(void);
Joonwoo Park61112782013-10-02 19:50:39 -0700101extern int get_target_boot_params(const char *cmdline, const char *part,
vijay kumar870515d2015-08-31 16:37:24 +0530102 char **buf);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700103
Sridhar Parasuram7e16d172015-07-05 11:35:23 -0700104void *info_buf;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700105void write_device_info_mmc(device_info *dev);
106void write_device_info_flash(device_info *dev);
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -0700107static int aboot_save_boot_hash_mmc(uint32_t image_addr, uint32_t image_size);
Channagoud Kadabiad259832015-05-29 11:14:17 -0700108static int aboot_frp_unlock(char *pname, void *data, unsigned sz);
Kishor PK38ed93d2017-04-25 14:19:26 +0530109static inline uint64_t validate_partition_size();
Parth Dixit54ac3bb2017-03-07 15:52:48 +0530110bool pwr_key_is_pressed = false;
Hareesh Gauthamabf1e1e2017-04-12 18:09:24 +0530111static bool is_systemd_present=false;
Mayank Grover351a75e2017-05-30 20:06:08 +0530112static void publish_getvar_multislot_vars();
Sridhar Parasurame94e8152014-10-24 14:06:03 -0700113/* fastboot command function pointer */
114typedef void (*fastboot_cmd_fn) (const char *, void *, unsigned);
115
116struct fastboot_cmd_desc {
117 char * name;
118 fastboot_cmd_fn cb;
119};
120
Subbaraman Narayanamurthyeb92bcc2010-07-20 14:32:46 -0700121#define EXPAND(NAME) #NAME
122#define TARGET(NAME) EXPAND(NAME)
Brian Swetland2defe162009-08-18 14:35:59 -0700123
Ajay Singh Parmara7865a82015-04-16 21:27:52 -0700124#define DISPLAY_PANEL_HDMI "hdmi"
125
Ajay Dudanicd01f9b2010-02-23 21:13:04 -0800126#ifdef MEMBASE
127#define EMMC_BOOT_IMG_HEADER_ADDR (0xFF000+(MEMBASE))
128#else
David Ng183a7422009-12-07 14:55:21 -0800129#define EMMC_BOOT_IMG_HEADER_ADDR 0xFF000
Ajay Dudanicd01f9b2010-02-23 21:13:04 -0800130#endif
131
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700132#ifndef MEMSIZE
133#define MEMSIZE 1024*1024
134#endif
135
136#define MAX_TAGS_SIZE 1024
137
Kun Liang2f1601a2013-08-12 16:29:54 +0800138/* make 4096 as default size to ensure EFS,EXT4's erasing */
139#define DEFAULT_ERASE_SIZE 4096
Ujwal Patelc0b0a252015-08-16 14:05:35 -0700140#define MAX_PANEL_BUF_SIZE 196
Vijay Kumar Pendoti0b21f462016-05-02 17:09:18 +0530141#define FOOTER_SIZE 16384
Kun Liang2f1601a2013-08-12 16:29:54 +0800142
Dhaval Patelf83d73b2014-06-23 16:24:37 -0700143#define DISPLAY_DEFAULT_PREFIX "mdss_mdp"
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700144#define BOOT_DEV_MAX_LEN 64
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -0800145
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800146#define IS_ARM64(ptr) (ptr->magic_64 == KERNEL64_HDR_MAGIC) ? true : false
147
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -0700148#define ADD_OF(a, b) (UINT_MAX - b > a) ? (a + b) : UINT_MAX
149
Ameya Thakur10a33452016-06-13 14:24:26 -0700150//Size of the header that is used in case the boot image has
151//a uncompressed kernel + appended dtb
152#define PATCHED_KERNEL_HEADER_SIZE 20
153
154//String used to determine if the boot image has
155//a uncompressed kernel + appended dtb
156#define PATCHED_KERNEL_MAGIC "UNCOMPRESSED_IMG"
157
Sridhar Parasuram7bd4aaf2015-02-12 11:14:38 -0800158#if USE_BOOTDEV_CMDLINE
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700159static const char *emmc_cmdline = " androidboot.bootdevice=";
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700160#else
David Ng183a7422009-12-07 14:55:21 -0800161static const char *emmc_cmdline = " androidboot.emmc=true";
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700162#endif
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800163static const char *usb_sn_cmdline = " androidboot.serialno=";
Pavel Nedev328ac822013-04-05 15:25:11 +0300164static const char *androidboot_mode = " androidboot.mode=";
Hareesh Gauthamabf1e1e2017-04-12 18:09:24 +0530165
166static const char *systemd_ffbm_mode = " systemd.unit=ffbm.target";
Matthew Qind886f3c2014-01-17 16:52:01 +0800167static const char *alarmboot_cmdline = " androidboot.alarmboot=true";
Pavel Nedev898298c2013-02-27 12:36:09 -0800168static const char *loglevel = " quiet";
Ajay Dudanica3a33c2011-11-18 08:31:40 -0800169static const char *battchg_pause = " androidboot.mode=charger";
Shashank Mittalcd98d472011-08-02 14:29:24 -0700170static const char *auth_kernel = " androidboot.authorized_kernel=true";
Pavel Nedev5614d222013-06-17 18:01:02 +0300171static const char *secondary_gpt_enable = " gpt";
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200172static const char *mdtp_activated_flag = " mdtp";
David Ng183a7422009-12-07 14:55:21 -0800173
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800174static const char *baseband_apq = " androidboot.baseband=apq";
175static const char *baseband_msm = " androidboot.baseband=msm";
176static const char *baseband_csfb = " androidboot.baseband=csfb";
177static const char *baseband_svlte2a = " androidboot.baseband=svlte2a";
Ajay Dudani403bc492011-09-30 16:17:21 -0700178static const char *baseband_mdm = " androidboot.baseband=mdm";
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800179static const char *baseband_mdm2 = " androidboot.baseband=mdm2";
Amol Jadi5c61a952012-05-04 17:05:35 -0700180static const char *baseband_sglte = " androidboot.baseband=sglte";
Amol Jadi2a15a272013-01-22 12:03:36 -0800181static const char *baseband_dsda = " androidboot.baseband=dsda";
182static const char *baseband_dsda2 = " androidboot.baseband=dsda2";
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800183static const char *baseband_sglte2 = " androidboot.baseband=sglte2";
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800184static const char *warmboot_cmdline = " qpnp-power-on.warm_boot=1";
Vijay Kumar Pendotib228cfc2016-06-13 20:15:23 +0530185static const char *baseband_apq_nowgr = " androidboot.baseband=baseband_apq_nowgr";
Mayank Grover351a75e2017-05-30 20:06:08 +0530186static const char *androidboot_slot_suffix = " androidboot.slot_suffix=";
187static const char *skip_ramfs = " skip_initramfs";
Mayank Grover3804be72017-06-22 11:59:23 +0530188static const char *sys_path_cmdline = " rootwait ro init=/init";
Sourabh Banerjee386b1322018-02-27 09:37:28 +0530189
190#if VERITY_LE
191static const char *verity_dev = " root=/dev/dm-0";
192static const char *verity_system_part = " dm=\"system";
193static const char *verity_params = " none ro,0 1 android-verity /dev/mmcblk0p";
194#else
Mayank Grover3804be72017-06-22 11:59:23 +0530195static const char *sys_path = " root=/dev/mmcblk0p";
Sourabh Banerjee386b1322018-02-27 09:37:28 +0530196#endif
Ajay Dudanid04110c2011-01-17 23:55:07 -0800197
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700198#if VERIFIED_BOOT
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700199static const char *verity_mode = " androidboot.veritymode=";
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700200static const char *verified_state= " androidboot.verifiedbootstate=";
Parth Dixita5715a02015-10-29 12:25:10 +0530201static const char *keymaster_v1= " androidboot.keymaster=1";
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700202//indexed based on enum values, green is 0 by default
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700203
204struct verified_boot_verity_mode vbvm[] =
205{
Channagoud Kadabi86b0c112016-03-16 19:23:16 -0700206#if ENABLE_VB_ATTEST
207 {false, "eio"},
208#else
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700209 {false, "logging"},
Channagoud Kadabi86b0c112016-03-16 19:23:16 -0700210#endif
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700211 {true, "enforcing"},
212};
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700213struct verified_boot_state_name vbsn[] =
214{
215 {GREEN, "green"},
216 {ORANGE, "orange"},
217 {YELLOW,"yellow"},
218 {RED,"red" },
219};
220#endif
Channagoud Kadabi86b0c112016-03-16 19:23:16 -0700221/*As per spec delay wait time before shutdown in Red state*/
222#define DELAY_WAIT 30000
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700223static unsigned page_size = 0;
224static unsigned page_mask = 0;
Gaurav Nebhwani4d2389c2016-03-17 21:10:05 +0530225static unsigned mmc_blocksize = 0;
226static unsigned mmc_blocksize_mask = 0;
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700227static char ffbm_mode_string[FFBM_MODE_BUF_SIZE];
228static bool boot_into_ffbm;
vijay kumar870515d2015-08-31 16:37:24 +0530229static char *target_boot_params = NULL;
Matthew Qind886f3c2014-01-17 16:52:01 +0800230static bool boot_reason_alarm;
Channagoud Kadabi80a182b2015-03-11 17:04:23 -0700231static bool devinfo_present = true;
Channagoud Kadabi736c4962015-08-21 11:56:52 -0700232bool boot_into_fastboot = false;
Parth Dixit4097b622016-03-15 14:42:27 +0530233static uint32_t dt_size = 0;
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700234
Shashank Mittalcd98d472011-08-02 14:29:24 -0700235/* Assuming unauthorized kernel image by default */
236static int auth_kernel_img = 0;
lijuang511a2b52015-08-14 20:50:51 +0800237static device_info device = {DEVICE_MAGIC, 0, 0, 0, 0, {0}, {0},{0}, 1};
vijay kumarc65876c2015-04-24 13:29:16 +0530238static bool is_allow_unlock = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -0700239
vijay kumarca2e6812015-07-08 20:28:25 +0530240static char frp_ptns[2][8] = {"config","frp"};
241
lijuang511a2b52015-08-14 20:50:51 +0800242static const char *critical_flash_allowed_ptn[] = {
243 "aboot",
244 "rpm",
245 "tz",
246 "sbl",
247 "sdi",
248 "sbl1",
249 "xbl",
250 "hyp",
251 "pmic",
252 "bootloader",
253 "devinfo",
254 "partition"};
255
Dima Zavin42168f22009-01-30 11:52:22 -0800256struct atag_ptbl_entry
257{
258 char name[16];
259 unsigned offset;
260 unsigned size;
261 unsigned flags;
262};
263
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700264/*
265 * Partition info, required to be published
266 * for fastboot
267 */
268struct getvar_partition_info {
Mayank Grover7b8a8f82017-10-27 13:07:59 +0530269 char part_name[MAX_GPT_NAME_SIZE]; /* Partition name */
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700270 char getvar_size[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for size */
271 char getvar_type[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for type */
272 char size_response[MAX_RSP_SIZE]; /* fastboot response for size */
273 char type_response[MAX_RSP_SIZE]; /* fastboot response for type */
274};
275
276/*
Mayank Grover7b8a8f82017-10-27 13:07:59 +0530277 * Update the part_type_known for known paritions types.
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700278 */
Mayank Groverd38fe012018-03-13 15:33:16 +0530279#define RAW_STR "raw"
Mayank Grover52cd10a2018-03-15 12:57:54 +0530280#define EXT_STR "ext4"
281#define F2FS_STR "f2fs"
282
283#define FS_SUPERBLOCK_OFFSET 0x400
284#define EXT_MAGIC 0xEF53
285#define EXT_MAGIC_OFFSET_SB 0x38
286#define F2FS_MAGIC 0xF2F52010 // F2FS Magic Number
287#define F2FS_MAGIC_OFFSET_SB 0x0
288
289typedef enum fs_signature_type {
290 EXT_FS_SIGNATURE = 1,
291 EXT_F2FS_SIGNATURE = 2,
292 NO_FS = -1
293} fs_signature_type;
294
Mayank Grover7b8a8f82017-10-27 13:07:59 +0530295struct getvar_partition_info part_info[NUM_PARTITIONS];
296struct getvar_partition_info part_type_known[] =
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700297{
Mayank Grover49920212018-02-09 18:15:55 +0530298 { "system" , "partition-size:", "partition-type:", "", "ext4" },
299 { "userdata" , "partition-size:", "partition-type:", "", "ext4" },
300 { "cache" , "partition-size:", "partition-type:", "", "ext4" },
301 { "recoveryfs" , "partition-size:", "partition-type:", "", "ext4" },
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700302};
303
304char max_download_size[MAX_RSP_SIZE];
Ameya Thakur11cf1a62013-08-05 12:44:48 -0700305char charger_screen_enabled[MAX_RSP_SIZE];
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800306char sn_buf[13];
Dhaval Patel223ec952013-07-18 14:49:44 -0700307char display_panel_buf[MAX_PANEL_BUF_SIZE];
Unnati Gandhi62c8ab82014-01-24 11:01:01 +0530308char panel_display_mode[MAX_RSP_SIZE];
Mayank Grover7b8a8f82017-10-27 13:07:59 +0530309#if PRODUCT_IOT
310char block_size_string[MAX_RSP_SIZE];
311
312/* For IOT we are using custom version */
313#define PRODUCT_IOT_VERSION "IOT001"
314char bootloader_version_string[MAX_RSP_SIZE];
315#endif
lijuang102dfa92015-10-09 18:31:03 +0800316
317#if CHECK_BAT_VOLTAGE
lijuang65c5a822015-08-29 16:35:36 +0800318char battery_voltage[MAX_RSP_SIZE];
lijuang102dfa92015-10-09 18:31:03 +0800319char battery_soc_ok [MAX_RSP_SIZE];
320#endif
321
lijuangf16461c2015-08-03 17:09:34 +0800322char get_variant[MAX_RSP_SIZE];
Greg Griscod6250552011-06-29 14:40:23 -0700323
Greg Griscod2471ef2011-07-14 13:00:42 -0700324extern int emmc_recovery_init(void);
325
Kinson Chik0b1c8162011-08-31 16:31:57 -0700326#if NO_KEYPAD_DRIVER
327extern int fastboot_trigger(void);
328#endif
Greg Griscod2471ef2011-07-14 13:00:42 -0700329
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800330static void update_ker_tags_rdisk_addr(struct boot_img_hdr *hdr, bool is_arm64)
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700331{
332 /* overwrite the destination of specified for the project */
Channagoud Kadabi7042fa32013-04-26 16:44:14 -0700333#ifdef ABOOT_IGNORE_BOOT_HEADER_ADDRS
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800334 if (is_arm64)
335 hdr->kernel_addr = ABOOT_FORCE_KERNEL64_ADDR;
336 else
337 hdr->kernel_addr = ABOOT_FORCE_KERNEL_ADDR;
Channagoud Kadabi7042fa32013-04-26 16:44:14 -0700338 hdr->ramdisk_addr = ABOOT_FORCE_RAMDISK_ADDR;
339 hdr->tags_addr = ABOOT_FORCE_TAGS_ADDR;
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700340#endif
341}
342
Dima Zavin42168f22009-01-30 11:52:22 -0800343static void ptentry_to_tag(unsigned **ptr, struct ptentry *ptn)
344{
345 struct atag_ptbl_entry atag_ptn;
346
347 memcpy(atag_ptn.name, ptn->name, 16);
348 atag_ptn.name[15] = '\0';
349 atag_ptn.offset = ptn->start;
350 atag_ptn.size = ptn->length;
351 atag_ptn.flags = ptn->flags;
352 memcpy(*ptr, &atag_ptn, sizeof(struct atag_ptbl_entry));
353 *ptr += sizeof(struct atag_ptbl_entry) / sizeof(unsigned);
354}
Brian Swetland9c4c0752009-01-25 16:23:50 -0800355
lijuang102dfa92015-10-09 18:31:03 +0800356#if CHECK_BAT_VOLTAGE
357void update_battery_status(void)
358{
359 snprintf(battery_voltage,MAX_RSP_SIZE, "%d",target_get_battery_voltage());
360 snprintf(battery_soc_ok ,MAX_RSP_SIZE, "%s",target_battery_soc_ok()? "yes":"no");
361}
362#endif
363
Neeti Desaie245d492012-06-01 12:52:13 -0700364unsigned char *update_cmdline(const char * cmdline)
Brian Swetland9c4c0752009-01-25 16:23:50 -0800365{
David Ng183a7422009-12-07 14:55:21 -0800366 int cmdline_len = 0;
367 int have_cmdline = 0;
Amol Jadi168b7712012-03-06 16:15:00 -0800368 unsigned char *cmdline_final = NULL;
Neeti Desaie245d492012-06-01 12:52:13 -0700369 int pause_at_bootup = 0;
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800370 bool warm_boot = false;
Pavel Nedev5614d222013-06-17 18:01:02 +0300371 bool gpt_exists = partition_gpt_exists();
Joonwoo Park61112782013-10-02 19:50:39 -0700372 int have_target_boot_params = 0;
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700373 char *boot_dev_buf = NULL;
Mayank Grover351a75e2017-05-30 20:06:08 +0530374 bool is_mdtp_activated = 0;
375 int current_active_slot = INVALID;
Mayank Grover3804be72017-06-22 11:59:23 +0530376 int system_ptn_index = -1;
377 unsigned int lun = 0;
378 char lun_char_base = 'a';
Sourabh Banerjee386b1322018-02-27 09:37:28 +0530379#if VERITY_LE
380 int syspath_buflen = strlen(verity_dev)
381 + strlen(verity_system_part) + (sizeof(char) * 2) + 2
382 + strlen(verity_params) + sizeof(int) + 2;
383#else
384 int syspath_buflen = strlen(sys_path) + sizeof(int) + 2; /*allocate buflen for largest possible string*/
385#endif
Mayank Grover3804be72017-06-22 11:59:23 +0530386 char syspath_buf[syspath_buflen];
Mayank Grover889be1b2017-09-12 20:12:23 +0530387#if VERIFIED_BOOT
388 uint32_t boot_state = RED;
389#endif
Hareesh Gauthamabf1e1e2017-04-12 18:09:24 +0530390
391#if USE_LE_SYSTEMD
392 is_systemd_present=true;
393#endif
394
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700395#if VERIFIED_BOOT
Mayank Grover912eaa62017-10-26 12:08:53 +0530396 if (VB_M <= target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +0530397 {
398 boot_state = boot_verify_get_state();
399 }
Parth Dixitddbc7352015-10-18 03:13:31 +0530400#endif
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700401
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200402#ifdef MDTP_SUPPORT
403 mdtp_activated(&is_mdtp_activated);
404#endif /* MDTP_SUPPORT */
Dima Zavin42168f22009-01-30 11:52:22 -0800405
Brian Swetland9c4c0752009-01-25 16:23:50 -0800406 if (cmdline && cmdline[0]) {
David Ng183a7422009-12-07 14:55:21 -0800407 cmdline_len = strlen(cmdline);
408 have_cmdline = 1;
409 }
410 if (target_is_emmc_boot()) {
411 cmdline_len += strlen(emmc_cmdline);
Sridhar Parasuram7bd4aaf2015-02-12 11:14:38 -0800412#if USE_BOOTDEV_CMDLINE
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700413 boot_dev_buf = (char *) malloc(sizeof(char) * BOOT_DEV_MAX_LEN);
414 ASSERT(boot_dev_buf);
415 platform_boot_dev_cmdline(boot_dev_buf);
416 cmdline_len += strlen(boot_dev_buf);
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700417#endif
David Ng183a7422009-12-07 14:55:21 -0800418 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800419
420 cmdline_len += strlen(usb_sn_cmdline);
421 cmdline_len += strlen(sn_buf);
422
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700423#if VERIFIED_BOOT
Mayank Grover912eaa62017-10-26 12:08:53 +0530424 if (VB_M <= target_get_vb_version())
Sridhar Parasuram7d8bb9a2015-09-23 18:21:19 -0700425 {
Mayank Grover889be1b2017-09-12 20:12:23 +0530426 cmdline_len += strlen(verified_state) + strlen(vbsn[boot_state].name);
427 if ((device.verity_mode != 0 ) && (device.verity_mode != 1))
428 {
429 dprintf(CRITICAL, "Devinfo paritition possibly corrupted!!!. Please erase devinfo partition to continue booting\n");
430 ASSERT(0);
431 }
432 cmdline_len += strlen(verity_mode) + strlen(vbvm[device.verity_mode].name);
433 cmdline_len += strlen(keymaster_v1);
Sridhar Parasuram7d8bb9a2015-09-23 18:21:19 -0700434 }
Parth Dixitddbc7352015-10-18 03:13:31 +0530435#endif
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700436
Pavel Nedev5614d222013-06-17 18:01:02 +0300437 if (boot_into_recovery && gpt_exists)
438 cmdline_len += strlen(secondary_gpt_enable);
439
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200440 if(is_mdtp_activated)
441 cmdline_len += strlen(mdtp_activated_flag);
442
Pavel Nedev328ac822013-04-05 15:25:11 +0300443 if (boot_into_ffbm) {
444 cmdline_len += strlen(androidboot_mode);
Hareesh Gauthamabf1e1e2017-04-12 18:09:24 +0530445
446 if(is_systemd_present)
447 cmdline_len += strlen(systemd_ffbm_mode);
448
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700449 cmdline_len += strlen(ffbm_mode_string);
Pavel Nedev898298c2013-02-27 12:36:09 -0800450 /* reduce kernel console messages to speed-up boot */
451 cmdline_len += strlen(loglevel);
Matthew Qind886f3c2014-01-17 16:52:01 +0800452 } else if (boot_reason_alarm) {
453 cmdline_len += strlen(alarmboot_cmdline);
Zhenhua Huang431dafa2015-06-30 16:13:37 +0800454 } else if ((target_build_variant_user() || device.charger_screen_enabled)
455 && target_pause_for_battery_charge()) {
David Ngf773dde2010-07-26 19:55:08 -0700456 pause_at_bootup = 1;
457 cmdline_len += strlen(battchg_pause);
458 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800459
Shashank Mittalcd98d472011-08-02 14:29:24 -0700460 if(target_use_signed_kernel() && auth_kernel_img) {
461 cmdline_len += strlen(auth_kernel);
462 }
463
Joonwoo Park61112782013-10-02 19:50:39 -0700464 if (get_target_boot_params(cmdline, boot_into_recovery ? "recoveryfs" :
465 "system",
vijay kumar870515d2015-08-31 16:37:24 +0530466 &target_boot_params) == 0) {
Joonwoo Park61112782013-10-02 19:50:39 -0700467 have_target_boot_params = 1;
468 cmdline_len += strlen(target_boot_params);
469 }
470
Ajay Dudanid04110c2011-01-17 23:55:07 -0800471 /* Determine correct androidboot.baseband to use */
472 switch(target_baseband())
473 {
474 case BASEBAND_APQ:
475 cmdline_len += strlen(baseband_apq);
476 break;
477
478 case BASEBAND_MSM:
479 cmdline_len += strlen(baseband_msm);
480 break;
481
482 case BASEBAND_CSFB:
483 cmdline_len += strlen(baseband_csfb);
484 break;
485
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800486 case BASEBAND_SVLTE2A:
487 cmdline_len += strlen(baseband_svlte2a);
Ajay Dudanid04110c2011-01-17 23:55:07 -0800488 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700489
490 case BASEBAND_MDM:
491 cmdline_len += strlen(baseband_mdm);
492 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700493
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800494 case BASEBAND_MDM2:
495 cmdline_len += strlen(baseband_mdm2);
496 break;
497
Amol Jadi5c61a952012-05-04 17:05:35 -0700498 case BASEBAND_SGLTE:
499 cmdline_len += strlen(baseband_sglte);
500 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530501
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800502 case BASEBAND_SGLTE2:
503 cmdline_len += strlen(baseband_sglte2);
504 break;
505
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530506 case BASEBAND_DSDA:
507 cmdline_len += strlen(baseband_dsda);
508 break;
Amol Jadi2a15a272013-01-22 12:03:36 -0800509
510 case BASEBAND_DSDA2:
511 cmdline_len += strlen(baseband_dsda2);
512 break;
Vijay Kumar Pendotib228cfc2016-06-13 20:15:23 +0530513 case BASEBAND_APQ_NOWGR:
514 cmdline_len += strlen(baseband_apq_nowgr);
515 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800516 }
517
Vladislav Levenetz3cd45242017-04-05 08:58:07 +0300518#if ENABLE_DISPLAY
Lijuan Gao4a5b8322014-07-24 10:38:42 +0800519 if (cmdline) {
520 if ((strstr(cmdline, DISPLAY_DEFAULT_PREFIX) == NULL) &&
Padmanabhan Komandurubccbcdc2015-06-30 16:19:24 +0530521 target_display_panel_node(display_panel_buf,
522 MAX_PANEL_BUF_SIZE) &&
Lijuan Gao4a5b8322014-07-24 10:38:42 +0800523 strlen(display_panel_buf)) {
524 cmdline_len += strlen(display_panel_buf);
525 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700526 }
Vladislav Levenetz3cd45242017-04-05 08:58:07 +0300527#endif
Dhaval Patel223ec952013-07-18 14:49:44 -0700528
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800529 if (target_warm_boot()) {
530 warm_boot = true;
531 cmdline_len += strlen(warmboot_cmdline);
532 }
533
Mayank Grover351a75e2017-05-30 20:06:08 +0530534 if (partition_multislot_is_supported())
535 {
536 current_active_slot = partition_find_active_slot();
537 cmdline_len += (strlen(androidboot_slot_suffix)+
538 strlen(SUFFIX_SLOT(current_active_slot)));
539
Mayank Grover3804be72017-06-22 11:59:23 +0530540 system_ptn_index = partition_get_index("system");
541 if (platform_boot_dev_isemmc())
542 {
Sourabh Banerjee386b1322018-02-27 09:37:28 +0530543#if VERITY_LE
544 /*
545 Condition 4: Verity and A/B both enabled
546 Eventual command line looks like:
547 ... androidboot.slot_suffix=<slot_suffix> ... rootfstype=ext4 ...
548 ... root=/dev/dm-0 dm="system_<slot_suffix> none ro,0 1 android-verity /dev/mmcblk0p<NN>"
549 */
550 snprintf(syspath_buf, syspath_buflen, " %s %s%s %s%d\"",
551 verity_dev,
552 verity_system_part, suffix_slot[current_active_slot],
553 verity_params, system_ptn_index + 1);
554#else
555 /*
556 Condition 5: A/B enabled, but verity disabled
557 Eventual command line looks like:
558 ... androidboot.slot_suffix=<slot_suffix> ... rootfstype=ext4 ...
559 ... root=/dev/mmcblk0p<NN> ...
560 */
561 snprintf(syspath_buf, syspath_buflen, " %s%d",
562 sys_path, system_ptn_index + 1);
563#endif
Mayank Grover3804be72017-06-22 11:59:23 +0530564 }
565 else
566 {
567 lun = partition_get_lun(system_ptn_index);
568 snprintf(syspath_buf, syspath_buflen, " root=/dev/sd%c%d",
569 lun_char_base + lun,
570 partition_get_index_in_lun("system", lun));
571 }
Mayank Grover351a75e2017-05-30 20:06:08 +0530572
Mayank Grover3804be72017-06-22 11:59:23 +0530573 cmdline_len += strlen(sys_path_cmdline);
574 cmdline_len += strlen(syspath_buf);
Mayank Grover351a75e2017-05-30 20:06:08 +0530575 if (!boot_into_recovery)
576 cmdline_len += strlen(skip_ramfs);
577 }
578
Channagoud Kadabi85c7ec32016-01-28 23:09:21 -0800579#if TARGET_CMDLINE_SUPPORT
580 char *target_cmdline_buf = malloc(TARGET_MAX_CMDLNBUF);
581 int target_cmd_line_len;
582 ASSERT(target_cmdline_buf);
583 target_cmd_line_len = target_update_cmdline(target_cmdline_buf);
584 cmdline_len += target_cmd_line_len;
585#endif
586
David Ng183a7422009-12-07 14:55:21 -0800587 if (cmdline_len > 0) {
588 const char *src;
Maria Yu52254c02014-07-04 16:14:54 +0800589 unsigned char *dst;
590
591 cmdline_final = (unsigned char*) malloc((cmdline_len + 4) & (~3));
592 ASSERT(cmdline_final != NULL);
vijay kumar287bb542015-09-29 13:01:52 +0530593 memset((void *)cmdline_final, 0, sizeof(*cmdline_final));
Maria Yu52254c02014-07-04 16:14:54 +0800594 dst = cmdline_final;
Neeti Desaie245d492012-06-01 12:52:13 -0700595
Amol Jadi168b7712012-03-06 16:15:00 -0800596 /* Save start ptr for debug print */
David Ng183a7422009-12-07 14:55:21 -0800597 if (have_cmdline) {
598 src = cmdline;
599 while ((*dst++ = *src++));
600 }
601 if (target_is_emmc_boot()) {
602 src = emmc_cmdline;
603 if (have_cmdline) --dst;
David Ngf773dde2010-07-26 19:55:08 -0700604 have_cmdline = 1;
605 while ((*dst++ = *src++));
Sridhar Parasuram7bd4aaf2015-02-12 11:14:38 -0800606#if USE_BOOTDEV_CMDLINE
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700607 src = boot_dev_buf;
608 if (have_cmdline) --dst;
609 while ((*dst++ = *src++));
610#endif
David Ngf773dde2010-07-26 19:55:08 -0700611 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800612
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700613#if VERIFIED_BOOT
Mayank Grover912eaa62017-10-26 12:08:53 +0530614 if (VB_M <= target_get_vb_version())
Sridhar Parasuram7d8bb9a2015-09-23 18:21:19 -0700615 {
Mayank Grover889be1b2017-09-12 20:12:23 +0530616 src = verified_state;
617 if(have_cmdline) --dst;
618 have_cmdline = 1;
619 while ((*dst++ = *src++));
620 src = vbsn[boot_state].name;
621 if(have_cmdline) --dst;
622 while ((*dst++ = *src++));
623
624 if ((device.verity_mode != 0 ) && (device.verity_mode != 1))
625 {
626 dprintf(CRITICAL, "Devinfo paritition possibly corrupted!!!. Please erase devinfo partition to continue booting\n");
627 ASSERT(0);
628 }
629 src = verity_mode;
630 if(have_cmdline) --dst;
631 while ((*dst++ = *src++));
632 src = vbvm[device.verity_mode].name;
633 if(have_cmdline) -- dst;
634 while ((*dst++ = *src++));
635 src = keymaster_v1;
636 if(have_cmdline) --dst;
637 while ((*dst++ = *src++));
Sridhar Parasuram7d8bb9a2015-09-23 18:21:19 -0700638 }
Parth Dixitddbc7352015-10-18 03:13:31 +0530639#endif
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800640 src = usb_sn_cmdline;
641 if (have_cmdline) --dst;
642 have_cmdline = 1;
643 while ((*dst++ = *src++));
644 src = sn_buf;
645 if (have_cmdline) --dst;
646 have_cmdline = 1;
647 while ((*dst++ = *src++));
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800648 if (warm_boot) {
649 if (have_cmdline) --dst;
650 src = warmboot_cmdline;
651 while ((*dst++ = *src++));
652 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800653
Pavel Nedev5614d222013-06-17 18:01:02 +0300654 if (boot_into_recovery && gpt_exists) {
655 src = secondary_gpt_enable;
656 if (have_cmdline) --dst;
657 while ((*dst++ = *src++));
658 }
659
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200660 if (is_mdtp_activated) {
661 src = mdtp_activated_flag;
662 if (have_cmdline) --dst;
663 while ((*dst++ = *src++));
664 }
665
Pavel Nedev328ac822013-04-05 15:25:11 +0300666 if (boot_into_ffbm) {
667 src = androidboot_mode;
668 if (have_cmdline) --dst;
669 while ((*dst++ = *src++));
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700670 src = ffbm_mode_string;
Pavel Nedev328ac822013-04-05 15:25:11 +0300671 if (have_cmdline) --dst;
672 while ((*dst++ = *src++));
Hareesh Gauthamabf1e1e2017-04-12 18:09:24 +0530673
674 if(is_systemd_present) {
675 src = systemd_ffbm_mode;
676 if (have_cmdline) --dst;
677 while ((*dst++ = *src++));
678 }
679
Pavel Nedev898298c2013-02-27 12:36:09 -0800680 src = loglevel;
681 if (have_cmdline) --dst;
682 while ((*dst++ = *src++));
Matthew Qind886f3c2014-01-17 16:52:01 +0800683 } else if (boot_reason_alarm) {
684 src = alarmboot_cmdline;
685 if (have_cmdline) --dst;
686 while ((*dst++ = *src++));
Pavel Nedev328ac822013-04-05 15:25:11 +0300687 } else if (pause_at_bootup) {
David Ngf773dde2010-07-26 19:55:08 -0700688 src = battchg_pause;
689 if (have_cmdline) --dst;
David Ng183a7422009-12-07 14:55:21 -0800690 while ((*dst++ = *src++));
691 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800692
Shashank Mittalcd98d472011-08-02 14:29:24 -0700693 if(target_use_signed_kernel() && auth_kernel_img) {
694 src = auth_kernel;
695 if (have_cmdline) --dst;
696 while ((*dst++ = *src++));
697 }
698
Ajay Dudanid04110c2011-01-17 23:55:07 -0800699 switch(target_baseband())
700 {
701 case BASEBAND_APQ:
702 src = baseband_apq;
703 if (have_cmdline) --dst;
704 while ((*dst++ = *src++));
705 break;
706
707 case BASEBAND_MSM:
708 src = baseband_msm;
709 if (have_cmdline) --dst;
710 while ((*dst++ = *src++));
711 break;
712
713 case BASEBAND_CSFB:
714 src = baseband_csfb;
715 if (have_cmdline) --dst;
716 while ((*dst++ = *src++));
717 break;
718
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800719 case BASEBAND_SVLTE2A:
720 src = baseband_svlte2a;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800721 if (have_cmdline) --dst;
722 while ((*dst++ = *src++));
723 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700724
725 case BASEBAND_MDM:
726 src = baseband_mdm;
727 if (have_cmdline) --dst;
728 while ((*dst++ = *src++));
729 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700730
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800731 case BASEBAND_MDM2:
732 src = baseband_mdm2;
733 if (have_cmdline) --dst;
734 while ((*dst++ = *src++));
735 break;
736
Amol Jadi5c61a952012-05-04 17:05:35 -0700737 case BASEBAND_SGLTE:
738 src = baseband_sglte;
739 if (have_cmdline) --dst;
740 while ((*dst++ = *src++));
741 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530742
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800743 case BASEBAND_SGLTE2:
744 src = baseband_sglte2;
745 if (have_cmdline) --dst;
746 while ((*dst++ = *src++));
747 break;
748
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530749 case BASEBAND_DSDA:
750 src = baseband_dsda;
751 if (have_cmdline) --dst;
752 while ((*dst++ = *src++));
753 break;
Amol Jadi2a15a272013-01-22 12:03:36 -0800754
755 case BASEBAND_DSDA2:
756 src = baseband_dsda2;
757 if (have_cmdline) --dst;
758 while ((*dst++ = *src++));
759 break;
Vijay Kumar Pendotib228cfc2016-06-13 20:15:23 +0530760 case BASEBAND_APQ_NOWGR:
761 src = baseband_apq_nowgr;
762 if (have_cmdline) --dst;
763 while ((*dst++ = *src++));
764 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800765 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700766
767 if (strlen(display_panel_buf)) {
Dhaval Patel223ec952013-07-18 14:49:44 -0700768 src = display_panel_buf;
769 if (have_cmdline) --dst;
770 while ((*dst++ = *src++));
771 }
Joonwoo Park61112782013-10-02 19:50:39 -0700772
773 if (have_target_boot_params) {
774 if (have_cmdline) --dst;
775 src = target_boot_params;
776 while ((*dst++ = *src++));
vijay kumar870515d2015-08-31 16:37:24 +0530777 free(target_boot_params);
Joonwoo Park61112782013-10-02 19:50:39 -0700778 }
Channagoud Kadabi85c7ec32016-01-28 23:09:21 -0800779
Mayank Grover351a75e2017-05-30 20:06:08 +0530780 if (partition_multislot_is_supported() && have_cmdline)
781 {
782 src = androidboot_slot_suffix;
783 --dst;
784 while ((*dst++ = *src++));
785 --dst;
786 src = SUFFIX_SLOT(current_active_slot);
787 while ((*dst++ = *src++));
788
789 if (!boot_into_recovery)
790 {
791 src = skip_ramfs;
792 --dst;
793 while ((*dst++ = *src++));
794 }
795
796 src = sys_path_cmdline;
797 --dst;
798 while ((*dst++ = *src++));
Mayank Grover3804be72017-06-22 11:59:23 +0530799
800 src = syspath_buf;
801 --dst;
802 while ((*dst++ = *src++));
Mayank Grover351a75e2017-05-30 20:06:08 +0530803 }
804
Channagoud Kadabi85c7ec32016-01-28 23:09:21 -0800805#if TARGET_CMDLINE_SUPPORT
806 if (target_cmdline_buf && target_cmd_line_len)
807 {
808 if (have_cmdline) --dst;
809 src = target_cmdline_buf;
810 while((*dst++ = *src++));
811 free(target_cmdline_buf);
812 }
813#endif
Neeti Desaie245d492012-06-01 12:52:13 -0700814 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700815
816
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700817 if (boot_dev_buf)
818 free(boot_dev_buf);
819
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -0800820 if (cmdline_final)
821 dprintf(INFO, "cmdline: %s\n", cmdline_final);
822 else
823 dprintf(INFO, "cmdline is NULL\n");
Neeti Desaie245d492012-06-01 12:52:13 -0700824 return cmdline_final;
825}
826
827unsigned *atag_core(unsigned *ptr)
828{
829 /* CORE */
830 *ptr++ = 2;
831 *ptr++ = 0x54410001;
832
833 return ptr;
834
835}
836
837unsigned *atag_ramdisk(unsigned *ptr, void *ramdisk,
838 unsigned ramdisk_size)
839{
840 if (ramdisk_size) {
841 *ptr++ = 4;
842 *ptr++ = 0x54420005;
843 *ptr++ = (unsigned)ramdisk;
844 *ptr++ = ramdisk_size;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800845 }
846
Neeti Desaie245d492012-06-01 12:52:13 -0700847 return ptr;
848}
849
850unsigned *atag_ptable(unsigned **ptr_addr)
851{
852 int i;
853 struct ptable *ptable;
854
855 if ((ptable = flash_get_ptable()) && (ptable->count != 0)) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700856 *(*ptr_addr)++ = 2 + (ptable->count * (sizeof(struct atag_ptbl_entry) /
857 sizeof(unsigned)));
Neeti Desaie245d492012-06-01 12:52:13 -0700858 *(*ptr_addr)++ = 0x4d534d70;
859 for (i = 0; i < ptable->count; ++i)
860 ptentry_to_tag(ptr_addr, ptable_get(ptable, i));
861 }
862
863 return (*ptr_addr);
864}
865
866unsigned *atag_cmdline(unsigned *ptr, const char *cmdline)
867{
868 int cmdline_length = 0;
869 int n;
Neeti Desaie245d492012-06-01 12:52:13 -0700870 char *dest;
871
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800872 cmdline_length = strlen((const char*)cmdline);
Neeti Desaie245d492012-06-01 12:52:13 -0700873 n = (cmdline_length + 4) & (~3);
874
875 *ptr++ = (n / 4) + 2;
876 *ptr++ = 0x54410009;
877 dest = (char *) ptr;
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800878 while ((*dest++ = *cmdline++));
Neeti Desaie245d492012-06-01 12:52:13 -0700879 ptr += (n / 4);
880
881 return ptr;
882}
883
884unsigned *atag_end(unsigned *ptr)
885{
Brian Swetland9c4c0752009-01-25 16:23:50 -0800886 /* END */
887 *ptr++ = 0;
888 *ptr++ = 0;
889
Neeti Desaie245d492012-06-01 12:52:13 -0700890 return ptr;
891}
892
893void generate_atags(unsigned *ptr, const char *cmdline,
894 void *ramdisk, unsigned ramdisk_size)
895{
vijay kumar21a37452015-12-29 16:23:21 +0530896 unsigned *orig_ptr = ptr;
Neeti Desaie245d492012-06-01 12:52:13 -0700897 ptr = atag_core(ptr);
898 ptr = atag_ramdisk(ptr, ramdisk, ramdisk_size);
899 ptr = target_atag_mem(ptr);
900
901 /* Skip NAND partition ATAGS for eMMC boot */
902 if (!target_is_emmc_boot()){
903 ptr = atag_ptable(&ptr);
904 }
905
vijay kumar21a37452015-12-29 16:23:21 +0530906 /*
907 * Atags size filled till + cmdline size + 1 unsigned for 4-byte boundary + 4 unsigned
908 * for atag identifier in atag_cmdline and atag_end should be with in MAX_TAGS_SIZE bytes
909 */
Mayank Groverbc8a2ef2018-02-23 18:03:36 +0530910 if (!cmdline)
911 return;
912
vijay kumar21a37452015-12-29 16:23:21 +0530913 if (((ptr - orig_ptr) + strlen(cmdline) + 5 * sizeof(unsigned)) < MAX_TAGS_SIZE) {
914 ptr = atag_cmdline(ptr, cmdline);
915 ptr = atag_end(ptr);
916 }
917 else {
918 dprintf(CRITICAL,"Crossing ATAGs Max size allowed\n");
919 ASSERT(0);
920 }
Neeti Desaie245d492012-06-01 12:52:13 -0700921}
922
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700923typedef void entry_func_ptr(unsigned, unsigned, unsigned*);
Neeti Desaie245d492012-06-01 12:52:13 -0700924void boot_linux(void *kernel, unsigned *tags,
925 const char *cmdline, unsigned machtype,
926 void *ramdisk, unsigned ramdisk_size)
927{
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800928 unsigned char *final_cmdline;
Amol Jadib6be5c12012-11-14 13:39:51 -0800929#if DEVICE_TREE
Neeti Desai17379b82012-06-04 18:42:53 -0700930 int ret = 0;
Amol Jadib6be5c12012-11-14 13:39:51 -0800931#endif
932
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700933 void (*entry)(unsigned, unsigned, unsigned*) = (entry_func_ptr*)(PA((addr_t)kernel));
Deepa Dinamani25a9f762012-11-30 15:57:15 -0800934 uint32_t tags_phys = PA((addr_t)tags);
vijay kumar1a50a642015-11-16 12:41:15 +0530935 struct kernel64_hdr *kptr = ((struct kernel64_hdr*)(PA((addr_t)kernel)));
Deepa Dinamani25a9f762012-11-30 15:57:15 -0800936
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530937 ramdisk = (void *)PA((addr_t)ramdisk);
Neeti Desaie245d492012-06-01 12:52:13 -0700938
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800939 final_cmdline = update_cmdline((const char*)cmdline);
940
Neeti Desai17379b82012-06-04 18:42:53 -0700941#if DEVICE_TREE
Amol Jadib6be5c12012-11-14 13:39:51 -0800942 dprintf(INFO, "Updating device tree: start\n");
943
Neeti Desai17379b82012-06-04 18:42:53 -0700944 /* Update the Device Tree */
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530945 ret = update_device_tree((void *)tags,(const char *)final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -0700946 if(ret)
947 {
948 dprintf(CRITICAL, "ERROR: Updating Device Tree Failed \n");
949 ASSERT(0);
950 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800951 dprintf(INFO, "Updating device tree: done\n");
Neeti Desai17379b82012-06-04 18:42:53 -0700952#else
Neeti Desaie245d492012-06-01 12:52:13 -0700953 /* Generating the Atags */
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800954 generate_atags(tags, final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -0700955#endif
Neeti Desaie245d492012-06-01 12:52:13 -0700956
Mayank Groverbc8a2ef2018-02-23 18:03:36 +0530957 if (final_cmdline)
958 free(final_cmdline);
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -0700959
960#if VERIFIED_BOOT
Mayank Grover912eaa62017-10-26 12:08:53 +0530961 if (VB_M <= target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +0530962 {
963 if (device.verity_mode == 0) {
lijuangbdd9bb42016-03-01 18:22:17 +0800964#if FBCON_DISPLAY_MSG
Channagoud Kadabi86b0c112016-03-16 19:23:16 -0700965#if ENABLE_VB_ATTEST
Mayank Grover889be1b2017-09-12 20:12:23 +0530966 display_bootverify_menu(DISPLAY_MENU_EIO);
967 wait_for_users_action();
968 if(!pwr_key_is_pressed)
969 shutdown_device();
Channagoud Kadabi86b0c112016-03-16 19:23:16 -0700970#else
Mayank Grover889be1b2017-09-12 20:12:23 +0530971 display_bootverify_menu(DISPLAY_MENU_LOGGING);
Channagoud Kadabi86b0c112016-03-16 19:23:16 -0700972#endif
Mayank Grover889be1b2017-09-12 20:12:23 +0530973 wait_for_users_action();
lijuangbdd9bb42016-03-01 18:22:17 +0800974#else
Mayank Grover889be1b2017-09-12 20:12:23 +0530975 dprintf(CRITICAL,
976 "The dm-verity is not started in enforcing mode.\nWait for 5 seconds before proceeding\n");
977 mdelay(5000);
lijuangbdd9bb42016-03-01 18:22:17 +0800978#endif
Mayank Grover889be1b2017-09-12 20:12:23 +0530979 }
lijuangbdd9bb42016-03-01 18:22:17 +0800980 }
lijuangbdd9bb42016-03-01 18:22:17 +0800981#endif
982
983#if VERIFIED_BOOT
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -0700984 /* Write protect the device info */
Channagoud Kadabi3bd9d1e2015-05-05 16:18:20 -0700985 if (!boot_into_recovery && target_build_variant_user() && devinfo_present && mmc_write_protect("devinfo", 1))
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -0700986 {
987 dprintf(INFO, "Failed to write protect dev info\n");
988 ASSERT(0);
989 }
990#endif
991
Aravind Venkateswaran8f076242014-02-25 16:25:30 -0800992 /* Turn off splash screen if enabled */
993#if DISPLAY_SPLASH_SCREEN
994 target_display_shutdown();
995#endif
996
Veera Sundaram Sankaran67ea0932015-09-25 10:09:30 -0700997 /* Perform target specific cleanup */
998 target_uninit();
Aravind Venkateswaran8f076242014-02-25 16:25:30 -0800999
Deepa Dinamani33734bc2013-03-06 12:16:06 -08001000 dprintf(INFO, "booting linux @ %p, ramdisk @ %p (%d), tags/device tree @ %p\n",
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301001 entry, ramdisk, ramdisk_size, (void *)tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001002
1003 enter_critical_section();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001004
Amol Jadi4421e652011-06-16 15:00:48 -07001005 /* do any platform specific cleanup before kernel entry */
1006 platform_uninit();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001007
Brian Swetland9c4c0752009-01-25 16:23:50 -08001008 arch_disable_cache(UCACHE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001009
Amol Jadi504f9fe2012-08-16 13:56:48 -07001010#if ARM_WITH_MMU
Brian Swetland9c4c0752009-01-25 16:23:50 -08001011 arch_disable_mmu();
Amol Jadi504f9fe2012-08-16 13:56:48 -07001012#endif
Amol Jadi492d5a52013-03-15 16:12:34 -07001013 bs_set_timestamp(BS_KERNEL_ENTRY);
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001014
1015 if (IS_ARM64(kptr))
1016 /* Jump to a 64bit kernel */
1017 scm_elexec_call((paddr_t)kernel, tags_phys);
1018 else
1019 /* Jump to a 32bit kernel */
1020 entry(0, machtype, (unsigned*)tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -08001021}
1022
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001023/* Function to check if the memory address range falls within the aboot
1024 * boundaries.
1025 * start: Start of the memory region
1026 * size: Size of the memory region
1027 */
Vijay Kumar Pendoti9c002ad2016-03-09 13:52:45 +05301028int check_aboot_addr_range_overlap(uintptr_t start, uint32_t size)
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001029{
1030 /* Check for boundary conditions. */
Sundarajan Srinivasance2a0ea2013-12-16 17:02:56 -08001031 if ((UINT_MAX - start) < size)
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001032 return -1;
1033
1034 /* Check for memory overlap. */
1035 if ((start < MEMBASE) && ((start + size) <= MEMBASE))
1036 return 0;
Channagoud Kadabi94143912013-10-15 12:53:52 -07001037 else if (start >= (MEMBASE + MEMSIZE))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001038 return 0;
1039 else
1040 return -1;
1041}
1042
Mayank Grovere559cec2017-10-17 15:12:03 +05301043/* Function to check if the memory address range falls beyond ddr region.
1044 * start: Start of the memory region
1045 * size: Size of the memory region
1046 */
1047int check_ddr_addr_range_bound(uintptr_t start, uint32_t size)
1048{
1049 uintptr_t ddr_pa_start_addr = PA(get_ddr_start());
1050 uint64_t ddr_size = smem_get_ddr_size();
1051 uint64_t ddr_pa_end_addr = ddr_pa_start_addr + ddr_size;
1052 uintptr_t pa_start_addr = PA(start);
1053
1054 /* Check for boundary conditions. */
1055 if ((UINT_MAX - start) < size)
1056 return -1;
1057
1058 /* Check if memory range is beyond the ddr range. */
1059 if (pa_start_addr < ddr_pa_start_addr ||
1060 pa_start_addr >= (ddr_pa_end_addr) ||
1061 (pa_start_addr + size) > ddr_pa_end_addr)
1062 return -1;
1063 else
1064 return 0;
1065}
1066
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001067BUF_DMA_ALIGN(buf, BOOT_IMG_MAX_PAGE_SIZE); //Equal to max-supported pagesize
Dima Zavin214cc642009-01-26 11:16:21 -08001068
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001069static void verify_signed_bootimg(uint32_t bootimg_addr, uint32_t bootimg_size)
1070{
1071 int ret;
Channagoud Kadabia8c623f2015-01-13 14:48:48 -08001072
1073#if !VERIFIED_BOOT
Sundarajan Srinivasance54d6e2013-11-11 12:45:00 -08001074#if IMAGE_VERIF_ALGO_SHA1
1075 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA1;
1076#else
1077 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA256;
1078#endif
Channagoud Kadabia8c623f2015-01-13 14:48:48 -08001079#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001080
1081 /* Assume device is rooted at this time. */
1082 device.is_tampered = 1;
1083
1084 dprintf(INFO, "Authenticating boot image (%d): start\n", bootimg_size);
1085
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001086#if VERIFIED_BOOT
Mayank Grover68fbf0d2017-10-24 14:13:39 +05301087 char *ptn_name = NULL;
1088 if (boot_into_recovery &&
1089 (!partition_multislot_is_supported()))
1090 ptn_name = "/recovery";
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001091 else
Mayank Grover68fbf0d2017-10-24 14:13:39 +05301092 ptn_name = "/boot";
1093
1094 ret = boot_verify_image((unsigned char *)bootimg_addr,
1095 bootimg_size, ptn_name);
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001096 boot_verify_print_state();
1097#else
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001098 ret = image_verify((unsigned char *)bootimg_addr,
1099 (unsigned char *)(bootimg_addr + bootimg_size),
1100 bootimg_size,
Sundarajan Srinivasance54d6e2013-11-11 12:45:00 -08001101 auth_algo);
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001102#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001103 dprintf(INFO, "Authenticating boot image: done return value = %d\n", ret);
1104
1105 if (ret)
1106 {
1107 /* Authorized kernel */
1108 device.is_tampered = 0;
Sundarajan Srinivasan3fb21f12013-09-16 18:36:15 -07001109 auth_kernel_img = 1;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001110 }
1111
Amit Blay4aa292f2015-04-28 21:55:59 +03001112#ifdef MDTP_SUPPORT
1113 {
1114 /* Verify MDTP lock.
1115 * For boot & recovery partitions, use aboot's verification result.
1116 */
1117 mdtp_ext_partition_verification_t ext_partition;
1118 ext_partition.partition = boot_into_recovery ? MDTP_PARTITION_RECOVERY : MDTP_PARTITION_BOOT;
1119 ext_partition.integrity_state = device.is_tampered ? MDTP_PARTITION_STATE_INVALID : MDTP_PARTITION_STATE_VALID;
1120 ext_partition.page_size = 0; /* Not needed since already validated */
1121 ext_partition.image_addr = 0; /* Not needed since already validated */
1122 ext_partition.image_size = 0; /* Not needed since already validated */
1123 ext_partition.sig_avail = FALSE; /* Not needed since already validated */
1124 mdtp_fwlock_verify_lock(&ext_partition);
1125 }
1126#endif /* MDTP_SUPPORT */
1127
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001128#if USE_PCOM_SECBOOT
1129 set_tamper_flag(device.is_tampered);
1130#endif
1131
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001132#if VERIFIED_BOOT
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001133 switch(boot_verify_get_state())
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001134 {
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001135 case RED:
lijuanga40d6302015-07-20 20:10:13 +08001136#if FBCON_DISPLAY_MSG
lijuang9a7d3b92015-11-30 14:41:24 +08001137 display_bootverify_menu(DISPLAY_MENU_RED);
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07001138#if ENABLE_VB_ATTEST
1139 mdelay(DELAY_WAIT);
1140 shutdown_device();
1141#else
lijuanga40d6302015-07-20 20:10:13 +08001142 wait_for_users_action();
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07001143#endif
lijuanga40d6302015-07-20 20:10:13 +08001144#else
1145 dprintf(CRITICAL,
1146 "Your device has failed verification and may not work properly.\nWait for 5 seconds before proceeding\n");
1147 mdelay(5000);
1148#endif
1149
1150 break;
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001151 case YELLOW:
lijuanga40d6302015-07-20 20:10:13 +08001152#if FBCON_DISPLAY_MSG
lijuang9a7d3b92015-11-30 14:41:24 +08001153 display_bootverify_menu(DISPLAY_MENU_YELLOW);
lijuanga40d6302015-07-20 20:10:13 +08001154 wait_for_users_action();
1155#else
1156 dprintf(CRITICAL,
1157 "Your device has loaded a different operating system.\nWait for 5 seconds before proceeding\n");
1158 mdelay(5000);
1159#endif
1160 break;
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001161 default:
lijuanga40d6302015-07-20 20:10:13 +08001162 break;
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001163 }
1164#endif
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001165#if !VERIFIED_BOOT
Unnati Gandhi1be04752015-03-27 19:41:53 +05301166 if(device.is_tampered)
1167 {
1168 write_device_info_mmc(&device);
1169 #ifdef TZ_TAMPER_FUSE
1170 set_tamper_fuse_cmd();
1171 #endif
1172 #ifdef ASSERT_ON_TAMPER
1173 dprintf(CRITICAL, "Device is tampered. Asserting..\n");
1174 ASSERT(0);
1175 #endif
1176 }
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001177#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001178}
1179
Maunik Shah0f3c8ac2014-03-02 17:47:58 +05301180static bool check_format_bit()
1181{
1182 bool ret = false;
1183 int index;
1184 uint64_t offset;
1185 struct boot_selection_info *in = NULL;
1186 char *buf = NULL;
1187
1188 index = partition_get_index("bootselect");
1189 if (index == INVALID_PTN)
1190 {
1191 dprintf(INFO, "Unable to locate /bootselect partition\n");
1192 return ret;
1193 }
1194 offset = partition_get_offset(index);
1195 if(!offset)
1196 {
1197 dprintf(INFO, "partition /bootselect doesn't exist\n");
1198 return ret;
1199 }
1200 buf = (char *) memalign(CACHE_LINE, ROUNDUP(page_size, CACHE_LINE));
Mayank Grover48860402016-11-29 12:34:53 +05301201 mmc_set_lun(partition_get_lun(index));
Maunik Shah0f3c8ac2014-03-02 17:47:58 +05301202 ASSERT(buf);
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301203 if (mmc_read(offset, (uint32_t *)buf, page_size))
Maunik Shah0f3c8ac2014-03-02 17:47:58 +05301204 {
1205 dprintf(INFO, "mmc read failure /bootselect %d\n", page_size);
1206 free(buf);
1207 return ret;
1208 }
1209 in = (struct boot_selection_info *) buf;
1210 if ((in->signature == BOOTSELECT_SIGNATURE) &&
1211 (in->version == BOOTSELECT_VERSION)) {
1212 if ((in->state_info & BOOTSELECT_FORMAT) &&
1213 !(in->state_info & BOOTSELECT_FACTORY))
1214 ret = true;
1215 } else {
1216 dprintf(CRITICAL, "Signature: 0x%08x or version: 0x%08x mismatched of /bootselect\n",
1217 in->signature, in->version);
1218 ASSERT(0);
1219 }
1220 free(buf);
1221 return ret;
1222}
1223
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001224void boot_verifier_init()
1225{
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001226 uint32_t boot_state;
1227 /* Check if device unlock */
1228 if(device.is_unlocked)
1229 {
1230 boot_verify_send_event(DEV_UNLOCK);
1231 boot_verify_print_state();
1232 dprintf(CRITICAL, "Device is unlocked! Skipping verification...\n");
1233 return;
1234 }
1235 else
1236 {
1237 boot_verify_send_event(BOOT_INIT);
1238 }
1239
1240 /* Initialize keystore */
1241 boot_state = boot_verify_keystore_init();
1242 if(boot_state == YELLOW)
1243 {
1244 boot_verify_print_state();
1245 dprintf(CRITICAL, "Keystore verification failed! Continuing anyways...\n");
1246 }
1247}
1248
Shashank Mittal23b8f422010-04-16 19:27:21 -07001249int boot_linux_from_mmc(void)
1250{
1251 struct boot_img_hdr *hdr = (void*) buf;
1252 struct boot_img_hdr *uhdr;
1253 unsigned offset = 0;
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07001254 int rcode;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001255 unsigned long long ptn = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001256 int index = INVALID_PTN;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001257
Shashank Mittalcd98d472011-08-02 14:29:24 -07001258 unsigned char *image_addr = 0;
1259 unsigned kernel_actual;
1260 unsigned ramdisk_actual;
1261 unsigned imagesize_actual;
Neeti Desai465491e2012-07-31 12:53:35 -07001262 unsigned second_actual = 0;
Neeti Desai465491e2012-07-31 12:53:35 -07001263
Matthew Qin271927e2015-03-31 22:07:07 -04001264 unsigned int dtb_size = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08001265 unsigned int out_len = 0;
1266 unsigned int out_avai_len = 0;
1267 unsigned char *out_addr = NULL;
1268 uint32_t dtb_offset = 0;
1269 unsigned char *kernel_start_addr = NULL;
1270 unsigned int kernel_size = 0;
Ameya Thakur10a33452016-06-13 14:24:26 -07001271 unsigned int patched_kernel_hdr_size = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08001272 int rc;
Mayank Groverc93cad82017-10-03 12:23:45 +05301273 char *ptn_name = NULL;
Neeti Desai465491e2012-07-31 12:53:35 -07001274#if DEVICE_TREE
1275 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -07001276 struct dt_entry dt_entry;
Neeti Desai465491e2012-07-31 12:53:35 -07001277 unsigned dt_table_offset;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001278 uint32_t dt_actual;
Deepa Dinamani19648b42013-09-05 17:05:55 -07001279 uint32_t dt_hdr_size;
Matthew Qin271927e2015-03-31 22:07:07 -04001280 unsigned char *best_match_dt_addr = NULL;
Neeti Desai465491e2012-07-31 12:53:35 -07001281#endif
Matthew Qin49e51fa2015-02-09 10:40:45 +08001282 struct kernel64_hdr *kptr = NULL;
Mayank Grover351a75e2017-05-30 20:06:08 +05301283 int current_active_slot = INVALID;
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001284
Maunik Shah0f3c8ac2014-03-02 17:47:58 +05301285 if (check_format_bit())
1286 boot_into_recovery = 1;
1287
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07001288 if (!boot_into_recovery) {
1289 memset(ffbm_mode_string, '\0', sizeof(ffbm_mode_string));
1290 rcode = get_ffbm(ffbm_mode_string, sizeof(ffbm_mode_string));
1291 if (rcode <= 0) {
1292 boot_into_ffbm = false;
1293 if (rcode < 0)
1294 dprintf(CRITICAL,"failed to get ffbm cookie");
1295 } else
1296 boot_into_ffbm = true;
1297 } else
1298 boot_into_ffbm = false;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001299 uhdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
1300 if (!memcmp(uhdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1301 dprintf(INFO, "Unified boot method!\n");
1302 hdr = uhdr;
1303 goto unified_boot;
1304 }
Mayank Groverc93cad82017-10-03 12:23:45 +05301305
1306 /* For a/b recovery image code is on boot partition.
1307 If we support multislot, always use boot partition. */
1308 if (boot_into_recovery &&
1309 (!partition_multislot_is_supported()))
1310 ptn_name = "recovery";
1311 else
1312 ptn_name = "boot";
1313
1314 index = partition_get_index(ptn_name);
1315 ptn = partition_get_offset(index);
1316 if(ptn == 0) {
1317 dprintf(CRITICAL, "ERROR: No %s partition found\n", ptn_name);
1318 return -1;
Kinson Chikf1a43512011-07-14 11:28:39 -07001319 }
Mayank Groverc93cad82017-10-03 12:23:45 +05301320
Channagoud Kadabief0547c2015-02-10 12:57:38 -08001321 /* Set Lun for boot & recovery partitions */
1322 mmc_set_lun(partition_get_lun(index));
Shashank Mittal23b8f422010-04-16 19:27:21 -07001323
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301324 if (mmc_read(ptn + offset, (uint32_t *) buf, page_size)) {
Shashank Mittal23b8f422010-04-16 19:27:21 -07001325 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
1326 return -1;
1327 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001328
1329 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001330 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Mayank Grover351a75e2017-05-30 20:06:08 +05301331 return ERR_INVALID_BOOT_MAGIC;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001332 }
1333
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07001334 if (hdr->page_size && (hdr->page_size != page_size)) {
vijay kumar2e21b3a2014-06-26 17:40:15 +05301335
1336 if (hdr->page_size > BOOT_IMG_MAX_PAGE_SIZE) {
1337 dprintf(CRITICAL, "ERROR: Invalid page size\n");
1338 return -1;
1339 }
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07001340 page_size = hdr->page_size;
1341 page_mask = page_size - 1;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001342 }
1343
Matthew Qin49e51fa2015-02-09 10:40:45 +08001344 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1345 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Kishor PKd8ddcad2017-07-27 13:53:57 +05301346 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001347
Matthew Qin49e51fa2015-02-09 10:40:45 +08001348 image_addr = (unsigned char *)target_get_scratch_address();
Kishor PK9e91b592017-05-24 12:14:55 +05301349 memcpy(image_addr, (void *)buf, page_size);
1350
1351 /* ensure commandline is terminated */
1352 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
Matthew Qin49e51fa2015-02-09 10:40:45 +08001353
1354#if DEVICE_TREE
Parth Dixit4097b622016-03-15 14:42:27 +05301355#ifndef OSVERSION_IN_BOOTIMAGE
1356 dt_size = hdr->dt_size;
1357#endif
1358 dt_actual = ROUND_TO_PAGE(dt_size, page_mask);
Kishor PKd8ddcad2017-07-27 13:53:57 +05301359 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 +05301360 dprintf(CRITICAL, "Integer overflow detected in bootimage header fields at %u in %s\n",__LINE__,__FILE__);
1361 return -1;
1362 }
Kishor PKd8ddcad2017-07-27 13:53:57 +05301363 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + second_actual + dt_actual);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001364#else
Kishor PKd8ddcad2017-07-27 13:53:57 +05301365 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 +05301366 dprintf(CRITICAL, "Integer overflow detected in bootimage header fields at %u in %s\n",__LINE__,__FILE__);
1367 return -1;
1368 }
Kishor PKd8ddcad2017-07-27 13:53:57 +05301369 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + second_actual);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001370#endif
1371
1372#if VERIFIED_BOOT
1373 boot_verifier_init();
1374#endif
1375
Vijay Kumar Pendoti9c002ad2016-03-09 13:52:45 +05301376 if (check_aboot_addr_range_overlap((uintptr_t) image_addr, imagesize_actual))
Matthew Qin49e51fa2015-02-09 10:40:45 +08001377 {
1378 dprintf(CRITICAL, "Boot image buffer address overlaps with aboot addresses.\n");
1379 return -1;
1380 }
1381
Matthew Qinbb7923d2015-02-09 10:56:09 +08001382 /*
1383 * Update loading flow of bootimage to support compressed/uncompressed
1384 * bootimage on both 64bit and 32bit platform.
1385 * 1. Load bootimage from emmc partition onto DDR.
1386 * 2. Check if bootimage is gzip format. If yes, decompress compressed kernel
1387 * 3. Check kernel header and update kernel load addr for 64bit and 32bit
1388 * platform accordingly.
1389 * 4. Sanity Check on kernel_addr and ramdisk_addr and copy data.
1390 */
Mayank Grover351a75e2017-05-30 20:06:08 +05301391 if (partition_multislot_is_supported())
1392 {
1393 current_active_slot = partition_find_active_slot();
1394 dprintf(INFO, "Loading boot image (%d) active_slot(%s): start\n",
1395 imagesize_actual, SUFFIX_SLOT(current_active_slot));
1396 }
1397 else
1398 {
1399 dprintf(INFO, "Loading (%s) image (%d): start\n",
1400 (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual);
1401 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08001402 bs_set_timestamp(BS_KERNEL_LOAD_START);
1403
Gaurav Nebhwani43e2a462016-03-17 21:32:56 +05301404 if ((target_get_max_flash_size() - page_size) < imagesize_actual)
1405 {
1406 dprintf(CRITICAL, "booimage size is greater than DDR can hold\n");
1407 return -1;
1408 }
Kishor PK9e91b592017-05-24 12:14:55 +05301409 offset = page_size;
1410 /* Read image without signature and header*/
1411 if (mmc_read(ptn + offset, (void *)(image_addr + offset), imagesize_actual - page_size))
Matthew Qin49e51fa2015-02-09 10:40:45 +08001412 {
1413 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
1414 return -1;
1415 }
1416
Mayank Grover351a75e2017-05-30 20:06:08 +05301417 if (partition_multislot_is_supported())
1418 {
1419 dprintf(INFO, "Loading boot image (%d) active_slot(%s): done\n",
1420 imagesize_actual, SUFFIX_SLOT(current_active_slot));
1421 }
1422 else
1423 {
1424 dprintf(INFO, "Loading (%s) image (%d): done\n",
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001425 (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual);
1426
Mayank Grover351a75e2017-05-30 20:06:08 +05301427 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08001428 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
1429
1430 /* Authenticate Kernel */
1431 dprintf(INFO, "use_signed_kernel=%d, is_unlocked=%d, is_tampered=%d.\n",
1432 (int) target_use_signed_kernel(),
1433 device.is_unlocked,
1434 device.is_tampered);
1435
Channagoud Kadabi736c4962015-08-21 11:56:52 -07001436 /* Change the condition a little bit to include the test framework support.
1437 * We would never reach this point if device is in fastboot mode, even if we did
1438 * that means we are in test mode, so execute kernel authentication part for the
1439 * tests */
Parth Dixitcb0c6082015-12-30 15:01:13 +05301440 if((target_use_signed_kernel() && (!device.is_unlocked)) || is_test_mode_enabled())
Matthew Qin49e51fa2015-02-09 10:40:45 +08001441 {
1442 offset = imagesize_actual;
Vijay Kumar Pendoti9c002ad2016-03-09 13:52:45 +05301443 if (check_aboot_addr_range_overlap((uintptr_t)image_addr + offset, page_size))
Matthew Qin49e51fa2015-02-09 10:40:45 +08001444 {
1445 dprintf(CRITICAL, "Signature read buffer address overlaps with aboot addresses.\n");
1446 return -1;
1447 }
1448
1449 /* Read signature */
1450 if(mmc_read(ptn + offset, (void *)(image_addr + offset), page_size))
1451 {
1452 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
1453 return -1;
1454 }
1455
1456 verify_signed_bootimg((uint32_t)image_addr, imagesize_actual);
Channagoud Kadabi736c4962015-08-21 11:56:52 -07001457 /* The purpose of our test is done here */
Parth Dixitcb0c6082015-12-30 15:01:13 +05301458 if(is_test_mode_enabled() && auth_kernel_img)
Channagoud Kadabi736c4962015-08-21 11:56:52 -07001459 return 0;
Matthew Qin49e51fa2015-02-09 10:40:45 +08001460 } else {
1461 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
1462 #ifdef TZ_SAVE_KERNEL_HASH
1463 aboot_save_boot_hash_mmc((uint32_t) image_addr, imagesize_actual);
1464 #endif /* TZ_SAVE_KERNEL_HASH */
Amit Blay4aa292f2015-04-28 21:55:59 +03001465
1466#ifdef MDTP_SUPPORT
1467 {
1468 /* Verify MDTP lock.
1469 * For boot & recovery partitions, MDTP will use boot_verifier APIs,
1470 * since verification was skipped in aboot. The signature is not part of the loaded image.
1471 */
1472 mdtp_ext_partition_verification_t ext_partition;
1473 ext_partition.partition = boot_into_recovery ? MDTP_PARTITION_RECOVERY : MDTP_PARTITION_BOOT;
1474 ext_partition.integrity_state = MDTP_PARTITION_STATE_UNSET;
1475 ext_partition.page_size = page_size;
1476 ext_partition.image_addr = (uint32)image_addr;
1477 ext_partition.image_size = imagesize_actual;
1478 ext_partition.sig_avail = FALSE;
1479 mdtp_fwlock_verify_lock(&ext_partition);
1480 }
1481#endif /* MDTP_SUPPORT */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001482 }
1483
Sridhar Parasuram00bfedb2015-05-26 14:21:27 -07001484#if VERIFIED_BOOT
taozhang93088bd2016-11-16 15:50:46 +08001485 if((boot_verify_get_state() == ORANGE) && (!boot_into_ffbm))
Reut Zysmanba8a9d52016-02-18 11:44:04 +02001486 {
1487#if FBCON_DISPLAY_MSG
1488 display_bootverify_menu(DISPLAY_MENU_ORANGE);
1489 wait_for_users_action();
1490#else
1491 dprintf(CRITICAL,
1492 "Your device has been unlocked and can't be trusted.\nWait for 5 seconds before proceeding\n");
1493 mdelay(5000);
1494#endif
1495 }
1496#endif
1497
1498#if VERIFIED_BOOT
Mayank Grover912eaa62017-10-26 12:08:53 +05301499 if (VB_M <= target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +05301500 {
1501 /* set boot and system versions. */
1502 set_os_version((unsigned char *)image_addr);
1503 // send root of trust
1504 if(!send_rot_command((uint32_t)device.is_unlocked))
1505 ASSERT(0);
1506 }
Parth Dixitbc9b6492015-10-18 00:41:38 +05301507#endif
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001508 /*
Matthew Qinbb7923d2015-02-09 10:56:09 +08001509 * Check if the kernel image is a gzip package. If yes, need to decompress it.
1510 * If not, continue booting.
1511 */
1512 if (is_gzip_package((unsigned char *)(image_addr + page_size), hdr->kernel_size))
1513 {
1514 out_addr = (unsigned char *)(image_addr + imagesize_actual + page_size);
1515 out_avai_len = target_get_max_flash_size() - imagesize_actual - page_size;
Matthew Qin0b15b322015-05-19 05:20:54 -04001516 dprintf(INFO, "decompressing kernel image: start\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001517 rc = decompress((unsigned char *)(image_addr + page_size),
1518 hdr->kernel_size, out_addr, out_avai_len,
1519 &dtb_offset, &out_len);
1520 if (rc)
1521 {
Matthew Qin0b15b322015-05-19 05:20:54 -04001522 dprintf(CRITICAL, "decompressing kernel image failed!!!\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001523 ASSERT(0);
1524 }
1525
Matthew Qin0b15b322015-05-19 05:20:54 -04001526 dprintf(INFO, "decompressing kernel image: done\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001527 kptr = (struct kernel64_hdr *)out_addr;
1528 kernel_start_addr = out_addr;
1529 kernel_size = out_len;
1530 } else {
Ameya Thakur10a33452016-06-13 14:24:26 -07001531 dprintf(INFO, "Uncpmpressed kernel in use\n");
1532 if (!strncmp((char*)(image_addr + page_size),
1533 PATCHED_KERNEL_MAGIC,
1534 sizeof(PATCHED_KERNEL_MAGIC) - 1)) {
1535 dprintf(INFO, "Patched kernel detected\n");
1536 kptr = (struct kernel64_hdr *)(image_addr + page_size +
1537 PATCHED_KERNEL_HEADER_SIZE);
1538 //The size of the kernel is stored at start of kernel image + 16
1539 //The dtb would start just after the kernel
1540 dtb_offset = *((uint32_t*)((unsigned char*)
1541 (image_addr + page_size +
1542 sizeof(PATCHED_KERNEL_MAGIC) -
1543 1)));
1544 //The actual kernel starts after the 20 byte header.
1545 kernel_start_addr = (unsigned char*)(image_addr +
1546 page_size + PATCHED_KERNEL_HEADER_SIZE);
1547 kernel_size = hdr->kernel_size;
1548 patched_kernel_hdr_size = PATCHED_KERNEL_HEADER_SIZE;
1549 } else {
1550 dprintf(INFO, "Kernel image not patched..Unable to locate dt offset\n");
1551 kptr = (struct kernel64_hdr *)(image_addr + page_size);
1552 kernel_start_addr = (unsigned char *)(image_addr + page_size);
1553 kernel_size = hdr->kernel_size;
1554 }
Matthew Qinbb7923d2015-02-09 10:56:09 +08001555 }
1556
1557 /*
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001558 * Update the kernel/ramdisk/tags address if the boot image header
1559 * has default values, these default values come from mkbootimg when
1560 * the boot image is flashed using fastboot flash:raw
1561 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001562 update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr));
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001563
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001564 /* Get virtual addresses since the hdr saves physical addresses. */
1565 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
1566 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
1567 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001568
Matthew Qinbb7923d2015-02-09 10:56:09 +08001569 kernel_size = ROUND_TO_PAGE(kernel_size, page_mask);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001570 /* Check if the addresses in the header are valid. */
Matthew Qinbb7923d2015-02-09 10:56:09 +08001571 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_size) ||
Mayank Grovere559cec2017-10-17 15:12:03 +05301572 check_ddr_addr_range_bound(hdr->kernel_addr, kernel_size) ||
1573 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual) ||
1574 check_ddr_addr_range_bound(hdr->ramdisk_addr, ramdisk_actual))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001575 {
Mayank Grovere559cec2017-10-17 15:12:03 +05301576 dprintf(CRITICAL, "kernel/ramdisk addresses are not valid.\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001577 return -1;
1578 }
1579
1580#ifndef DEVICE_TREE
Mayank Grovere559cec2017-10-17 15:12:03 +05301581 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE) ||
1582 check_ddr_addr_range_bound(hdr->tags_addr, MAX_TAGS_SIZE))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001583 {
Mayank Grovere559cec2017-10-17 15:12:03 +05301584 dprintf(CRITICAL, "Tags addresses are not valid.\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001585 return -1;
1586 }
1587#endif
1588
Matthew Qin49e51fa2015-02-09 10:40:45 +08001589 /* Move kernel, ramdisk and device tree to correct address */
Matthew Qinbb7923d2015-02-09 10:56:09 +08001590 memmove((void*) hdr->kernel_addr, kernel_start_addr, kernel_size);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001591 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001592
Matthew Qin49e51fa2015-02-09 10:40:45 +08001593 #if DEVICE_TREE
Parth Dixit4097b622016-03-15 14:42:27 +05301594 if(dt_size) {
Matthew Qin49e51fa2015-02-09 10:40:45 +08001595 dt_table_offset = ((uint32_t)image_addr + page_size + kernel_actual + ramdisk_actual + second_actual);
1596 table = (struct dt_table*) dt_table_offset;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001597
Matthew Qin49e51fa2015-02-09 10:40:45 +08001598 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
1599 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1600 return -1;
1601 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001602
P.V. Phani Kumar82916762016-03-09 09:20:19 +05301603 /* Its Error if, dt_hdr_size (table->num_entries * dt_entry size + Dev_Tree Header)
1604 goes beyound hdr->dt_size*/
Parth Dixit4097b622016-03-15 14:42:27 +05301605 if (dt_hdr_size > ROUND_TO_PAGE(dt_size,hdr->page_size)) {
P.V. Phani Kumar82916762016-03-09 09:20:19 +05301606 dprintf(CRITICAL, "ERROR: Invalid Device Tree size \n");
1607 return -1;
1608 }
1609
Matthew Qin49e51fa2015-02-09 10:40:45 +08001610 /* Find index of device tree within device tree table */
1611 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
1612 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1613 return -1;
1614 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001615
P.V. Phani Kumar3c333202016-03-09 11:54:37 +05301616 if(dt_entry.offset > (UINT_MAX - dt_entry.size)) {
1617 dprintf(CRITICAL, "ERROR: Device tree contents are Invalid\n");
1618 return -1;
1619 }
1620
1621 /* Ensure we are not overshooting dt_size with the dt_entry selected */
Parth Dixit4097b622016-03-15 14:42:27 +05301622 if ((dt_entry.offset + dt_entry.size) > dt_size) {
P.V. Phani Kumar3c333202016-03-09 11:54:37 +05301623 dprintf(CRITICAL, "ERROR: Device tree contents are Invalid\n");
1624 return -1;
1625 }
1626
Matthew Qin271927e2015-03-31 22:07:07 -04001627 if (is_gzip_package((unsigned char *)dt_table_offset + dt_entry.offset, dt_entry.size))
1628 {
1629 unsigned int compressed_size = 0;
1630 out_addr += out_len;
1631 out_avai_len -= out_len;
Matthew Qin0b15b322015-05-19 05:20:54 -04001632 dprintf(INFO, "decompressing dtb: start\n");
Matthew Qin271927e2015-03-31 22:07:07 -04001633 rc = decompress((unsigned char *)dt_table_offset + dt_entry.offset,
1634 dt_entry.size, out_addr, out_avai_len,
1635 &compressed_size, &dtb_size);
1636 if (rc)
1637 {
Matthew Qin0b15b322015-05-19 05:20:54 -04001638 dprintf(CRITICAL, "decompressing dtb failed!!!\n");
Matthew Qin271927e2015-03-31 22:07:07 -04001639 ASSERT(0);
1640 }
1641
Matthew Qin0b15b322015-05-19 05:20:54 -04001642 dprintf(INFO, "decompressing dtb: done\n");
Matthew Qin271927e2015-03-31 22:07:07 -04001643 best_match_dt_addr = out_addr;
1644 } else {
1645 best_match_dt_addr = (unsigned char *)dt_table_offset + dt_entry.offset;
1646 dtb_size = dt_entry.size;
1647 }
1648
Matthew Qin49e51fa2015-02-09 10:40:45 +08001649 /* Validate and Read device device tree in the tags_addr */
Mayank Grovere559cec2017-10-17 15:12:03 +05301650 if (check_aboot_addr_range_overlap(hdr->tags_addr, dtb_size) ||
1651 check_ddr_addr_range_bound(hdr->tags_addr, dtb_size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001652 {
Mayank Grovere559cec2017-10-17 15:12:03 +05301653 dprintf(CRITICAL, "Device tree addresses are not valid\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001654 return -1;
1655 }
Shashank Mittal162244e2011-08-08 19:01:25 -07001656
Matthew Qin271927e2015-03-31 22:07:07 -04001657 memmove((void *)hdr->tags_addr, (char *)best_match_dt_addr, dtb_size);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001658 } else {
1659 /* Validate the tags_addr */
Mayank Grovere559cec2017-10-17 15:12:03 +05301660 if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual) ||
1661 check_ddr_addr_range_bound(hdr->tags_addr, kernel_actual))
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001662 {
Mayank Grovere559cec2017-10-17 15:12:03 +05301663 dprintf(CRITICAL, "Device tree addresses are not valid.\n");
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001664 return -1;
1665 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08001666 /*
1667 * If appended dev tree is found, update the atags with
1668 * memory address to the DTB appended location on RAM.
1669 * Else update with the atags address in the kernel header
1670 */
1671 void *dtb;
Ameya Thakur10a33452016-06-13 14:24:26 -07001672 dtb = dev_tree_appended(
1673 (void*)(image_addr + page_size +
1674 patched_kernel_hdr_size),
1675 hdr->kernel_size, dtb_offset,
1676 (void *)hdr->tags_addr);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001677 if (!dtb) {
1678 dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001679 return -1;
1680 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07001681 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08001682 #endif
Shashank Mittal23b8f422010-04-16 19:27:21 -07001683
Stanimir Varbanov69ec5462013-07-18 18:17:42 +03001684 if (boot_into_recovery && !device.is_unlocked && !device.is_tampered)
1685 target_load_ssd_keystore();
1686
Shashank Mittal23b8f422010-04-16 19:27:21 -07001687unified_boot:
Shashank Mittal23b8f422010-04-16 19:27:21 -07001688
Dima Zavin77e41f32013-03-06 16:10:43 -08001689 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07001690 (const char *)hdr->cmdline, board_machtype(),
Shashank Mittal23b8f422010-04-16 19:27:21 -07001691 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
1692
1693 return 0;
1694}
1695
Dima Zavin214cc642009-01-26 11:16:21 -08001696int boot_linux_from_flash(void)
1697{
1698 struct boot_img_hdr *hdr = (void*) buf;
Dima Zavin214cc642009-01-26 11:16:21 -08001699 struct ptentry *ptn;
1700 struct ptable *ptable;
1701 unsigned offset = 0;
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001702
Shashank Mittalcd98d472011-08-02 14:29:24 -07001703 unsigned char *image_addr = 0;
1704 unsigned kernel_actual;
1705 unsigned ramdisk_actual;
1706 unsigned imagesize_actual;
vijay kumar8f53e362015-11-24 13:38:11 +05301707 unsigned second_actual = 0;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001708
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001709#if DEVICE_TREE
1710 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -07001711 struct dt_entry dt_entry;
vijay kumar8f53e362015-11-24 13:38:11 +05301712 unsigned dt_table_offset;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001713 uint32_t dt_actual;
Deepa Dinamani19648b42013-09-05 17:05:55 -07001714 uint32_t dt_hdr_size;
vijay kumar8f53e362015-11-24 13:38:11 +05301715 unsigned int dtb_size = 0;
1716 unsigned char *best_match_dt_addr = NULL;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001717#endif
1718
David Ng183a7422009-12-07 14:55:21 -08001719 if (target_is_emmc_boot()) {
1720 hdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
1721 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1722 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
1723 return -1;
1724 }
1725 goto continue_boot;
1726 }
1727
Dima Zavin214cc642009-01-26 11:16:21 -08001728 ptable = flash_get_ptable();
1729 if (ptable == NULL) {
1730 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1731 return -1;
1732 }
1733
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001734 if(!boot_into_recovery)
1735 {
1736 ptn = ptable_find(ptable, "boot");
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001737
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001738 if (ptn == NULL) {
1739 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1740 return -1;
1741 }
1742 }
1743 else
1744 {
1745 ptn = ptable_find(ptable, "recovery");
1746 if (ptn == NULL) {
1747 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
1748 return -1;
1749 }
Dima Zavin214cc642009-01-26 11:16:21 -08001750 }
1751
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05301752 /* Read boot.img header from flash */
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001753 if (flash_read(ptn, offset, buf, page_size)) {
Dima Zavin214cc642009-01-26 11:16:21 -08001754 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
1755 return -1;
1756 }
Dima Zavin214cc642009-01-26 11:16:21 -08001757
1758 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001759 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Dima Zavin214cc642009-01-26 11:16:21 -08001760 return -1;
1761 }
1762
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001763 if (hdr->page_size != page_size) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001764 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 -08001765 return -1;
1766 }
1767
Kishor PK9e91b592017-05-24 12:14:55 +05301768 image_addr = (unsigned char *)target_get_scratch_address();
1769 memcpy(image_addr, (void *)buf, page_size);
vijay kumar287bb542015-09-29 13:01:52 +05301770
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001771 /*
1772 * Update the kernel/ramdisk/tags address if the boot image header
1773 * has default values, these default values come from mkbootimg when
1774 * the boot image is flashed using fastboot flash:raw
1775 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001776 update_ker_tags_rdisk_addr(hdr, false);
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001777
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001778 /* Get virtual addresses since the hdr saves physical addresses. */
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001779 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
1780 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
1781 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
1782
1783 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1784 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Mayank Grover032736f2017-07-14 20:34:51 +05301785 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001786
Kishor PK9e91b592017-05-24 12:14:55 +05301787 /* ensure commandline is terminated */
1788 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
1789
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001790 /* Check if the addresses in the header are valid. */
1791 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
Mayank Grovere559cec2017-10-17 15:12:03 +05301792 check_ddr_addr_range_bound(hdr->kernel_addr, kernel_actual) ||
1793 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual) ||
1794 check_ddr_addr_range_bound(hdr->ramdisk_addr, ramdisk_actual))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001795 {
Mayank Grovere559cec2017-10-17 15:12:03 +05301796 dprintf(CRITICAL, "kernel/ramdisk addresses are not valid.\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001797 return -1;
1798 }
1799
1800#ifndef DEVICE_TREE
Kishor PKd8ddcad2017-07-27 13:53:57 +05301801 if (UINT_MAX < ((uint64_t)kernel_actual + (uint64_t)ramdisk_actual+ (uint64_t)second_actual + page_size)) {
Mayank Grover032736f2017-07-14 20:34:51 +05301802 dprintf(CRITICAL, "Integer overflow detected in bootimage header fields\n");
1803 return -1;
1804 }
Kishor PKd8ddcad2017-07-27 13:53:57 +05301805 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + second_actual);
Mayank Grover032736f2017-07-14 20:34:51 +05301806
Mayank Grovere559cec2017-10-17 15:12:03 +05301807 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE) ||
1808 check_ddr_addr_range_bound(hdr->tags_addr, MAX_TAGS_SIZE))
Vijay Kumar Pendotid3ed20e2016-09-20 00:34:46 +05301809 {
Mayank Grovere559cec2017-10-17 15:12:03 +05301810 dprintf(CRITICAL, "Tags addresses are not valid.\n");
Vijay Kumar Pendotid3ed20e2016-09-20 00:34:46 +05301811 return -1;
1812 }
1813#else
1814
1815#ifndef OSVERSION_IN_BOOTIMAGE
1816 dt_size = hdr->dt_size;
1817#endif
Mayank Grover032736f2017-07-14 20:34:51 +05301818 dt_actual = ROUND_TO_PAGE(dt_size, page_mask);
Kishor PKd8ddcad2017-07-27 13:53:57 +05301819 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 +05301820 dprintf(CRITICAL, "Integer overflow detected in bootimage header fields\n");
1821 return -1;
1822 }
1823
Kishor PKd8ddcad2017-07-27 13:53:57 +05301824 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + second_actual + dt_actual);
Mayank Grover032736f2017-07-14 20:34:51 +05301825
Mayank Grovere559cec2017-10-17 15:12:03 +05301826 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_size) ||
1827 check_ddr_addr_range_bound(hdr->tags_addr, dt_size))
Mayank Grover032736f2017-07-14 20:34:51 +05301828 {
Mayank Grovere559cec2017-10-17 15:12:03 +05301829 dprintf(CRITICAL, "Device tree addresses are not valid.\n");
Mayank Grover032736f2017-07-14 20:34:51 +05301830 return -1;
1831 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001832#endif
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001833
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05301834 /* Read full boot.img from flash */
1835 dprintf(INFO, "Loading (%s) image (%d): start\n",
1836 (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual);
1837 bs_set_timestamp(BS_KERNEL_LOAD_START);
1838
1839 if (UINT_MAX - page_size < imagesize_actual)
1840 {
1841 dprintf(CRITICAL,"Integer overflow detected in bootimage header fields %u %s\n", __LINE__,__func__);
1842 return -1;
1843 }
1844
1845 /*Check the availability of RAM before reading boot image + max signature length from flash*/
1846 if (target_get_max_flash_size() < (imagesize_actual + page_size))
1847 {
1848 dprintf(CRITICAL, "bootimage size is greater than DDR can hold\n");
1849 return -1;
1850 }
1851
1852 offset = page_size;
1853 /* Read image without signature and header */
1854 if (flash_read(ptn, offset, (void *)(image_addr + offset), imagesize_actual - page_size))
1855 {
1856 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
1857 return -1;
1858 }
1859
1860 dprintf(INFO, "Loading (%s) image (%d): done\n",
1861 (!boot_into_recovery ? "boot" : "recovery"), imagesize_actual);
1862 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
1863
Shashank Mittalcd98d472011-08-02 14:29:24 -07001864 /* Authenticate Kernel */
Deepa Dinamani23b60d42013-06-24 18:10:52 -07001865 if(target_use_signed_kernel() && (!device.is_unlocked))
Shashank Mittalcd98d472011-08-02 14:29:24 -07001866 {
Shashank Mittalcd98d472011-08-02 14:29:24 -07001867 offset = imagesize_actual;
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05301868
Shashank Mittalcd98d472011-08-02 14:29:24 -07001869 /* Read signature */
1870 if (flash_read(ptn, offset, (void *)(image_addr + offset), page_size))
1871 {
1872 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001873 return -1;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001874 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07001875
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301876 verify_signed_bootimg((uint32_t)image_addr, imagesize_actual);
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05301877 }
1878 offset = page_size;
1879 if(hdr->second_size != 0) {
1880 if (UINT_MAX - offset < second_actual)
1881 {
1882 dprintf(CRITICAL, "ERROR: Integer overflow in boot image header %s\t%d\n",__func__,__LINE__);
1883 return -1;
vijay kumar8f53e362015-11-24 13:38:11 +05301884 }
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05301885 offset += second_actual;
1886 /* Second image loading not implemented. */
1887 ASSERT(0);
1888 }
Shashank Mittal162244e2011-08-08 19:01:25 -07001889
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05301890 /* Move kernel and ramdisk to correct address */
1891 memmove((void*) hdr->kernel_addr, (char*) (image_addr + page_size), hdr->kernel_size);
1892 memmove((void*) hdr->ramdisk_addr, (char*) (image_addr + page_size + kernel_actual), hdr->ramdisk_size);
1893
1894#if DEVICE_TREE
1895 if(dt_size != 0) {
1896
1897 dt_table_offset = ((uint32_t)image_addr + page_size + kernel_actual + ramdisk_actual + second_actual);
1898
1899 table = (struct dt_table*) dt_table_offset;
1900
1901 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
1902 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1903 return -1;
1904 }
1905
1906 /* Its Error if, dt_hdr_size (table->num_entries * dt_entry size + Dev_Tree Header)
1907 goes beyound hdr->dt_size*/
1908 if (dt_hdr_size > ROUND_TO_PAGE(dt_size,hdr->page_size)) {
1909 dprintf(CRITICAL, "ERROR: Invalid Device Tree size \n");
1910 return -1;
1911 }
1912
1913 /* Find index of device tree within device tree table */
1914 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
1915 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1916 return -1;
1917 }
1918
1919 /* Validate and Read device device tree in the "tags_add */
1920 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size) ||
1921 check_ddr_addr_range_bound(hdr->tags_addr, dt_entry.size))
1922 {
1923 dprintf(CRITICAL, "Device tree addresses are not valid.\n");
1924 return -1;
1925 }
1926
1927 if(dt_entry.offset > (UINT_MAX - dt_entry.size)) {
1928 dprintf(CRITICAL, "ERROR: Device tree contents are Invalid\n");
1929 return -1;
1930 }
1931
1932 /* Ensure we are not overshooting dt_size with the dt_entry selected */
1933 if ((dt_entry.offset + dt_entry.size) > dt_size) {
1934 dprintf(CRITICAL, "ERROR: Device tree contents are Invalid\n");
1935 return -1;
1936 }
1937
1938 best_match_dt_addr = (unsigned char *)table + dt_entry.offset;
1939 dtb_size = dt_entry.size;
1940 memmove((void *)hdr->tags_addr, (char *)best_match_dt_addr, dtb_size);
1941 }
1942#endif
1943 if(target_use_signed_kernel() && (!device.is_unlocked))
1944 {
Shashank Mittal162244e2011-08-08 19:01:25 -07001945 /* Make sure everything from scratch address is read before next step!*/
Shashank Mittala0032282011-08-26 14:50:11 -07001946 if(device.is_tampered)
Shashank Mittal162244e2011-08-08 19:01:25 -07001947 {
1948 write_device_info_flash(&device);
1949 }
Channagoud Kadabi5c86fe32012-02-16 10:58:48 +05301950#if USE_PCOM_SECBOOT
1951 set_tamper_flag(device.is_tampered);
1952#endif
Shashank Mittalcd98d472011-08-02 14:29:24 -07001953 }
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001954
David Ng183a7422009-12-07 14:55:21 -08001955continue_boot:
Dima Zavin214cc642009-01-26 11:16:21 -08001956
Dima Zavin214cc642009-01-26 11:16:21 -08001957 /* TODO: create/pass atags to kernel */
1958
Ajay Dudanie28a6072011-07-01 13:59:46 -07001959 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07001960 (const char *)hdr->cmdline, board_machtype(),
Dima Zavin214cc642009-01-26 11:16:21 -08001961 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
1962
1963 return 0;
1964}
Brian Swetland9c4c0752009-01-25 16:23:50 -08001965
Shashank Mittal162244e2011-08-08 19:01:25 -07001966void write_device_info_mmc(device_info *dev)
1967{
Shashank Mittal162244e2011-08-08 19:01:25 -07001968 unsigned long long ptn = 0;
1969 unsigned long long size;
1970 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001971 uint8_t lun = 0;
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001972 uint32_t ret = 0;
Mayank Groverddd348d2018-01-23 14:07:09 +05301973 uint32_t device_info_sz = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001974
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001975 if (devinfo_present)
1976 index = partition_get_index("devinfo");
1977 else
1978 index = partition_get_index("aboot");
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001979
Shashank Mittal162244e2011-08-08 19:01:25 -07001980 ptn = partition_get_offset(index);
1981 if(ptn == 0)
1982 {
1983 return;
1984 }
1985
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001986 lun = partition_get_lun(index);
1987 mmc_set_lun(lun);
1988
Shashank Mittal162244e2011-08-08 19:01:25 -07001989 size = partition_get_size(index);
1990
Mayank Groverddd348d2018-01-23 14:07:09 +05301991 device_info_sz = ROUND_TO_PAGE(sizeof(struct device_info),
1992 mmc_blocksize_mask);
1993 if (device_info_sz == UINT_MAX)
1994 {
1995 dprintf(CRITICAL, "ERROR: Incorrect blocksize of card\n");
1996 return;
1997 }
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001998
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001999 if (devinfo_present)
Mayank Groverddd348d2018-01-23 14:07:09 +05302000 ret = mmc_write(ptn, device_info_sz, (void *)info_buf);
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002001 else
Mayank Groverddd348d2018-01-23 14:07:09 +05302002 ret = mmc_write((ptn + size - device_info_sz), device_info_sz, (void *)info_buf);
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002003 if (ret)
Shashank Mittal162244e2011-08-08 19:01:25 -07002004 {
2005 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
Sridhar Parasurame1a97922015-07-27 15:31:22 -07002006 ASSERT(0);
Shashank Mittal162244e2011-08-08 19:01:25 -07002007 }
2008}
2009
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002010void read_device_info_mmc(struct device_info *info)
Shashank Mittal162244e2011-08-08 19:01:25 -07002011{
Shashank Mittal162244e2011-08-08 19:01:25 -07002012 unsigned long long ptn = 0;
2013 unsigned long long size;
2014 int index = INVALID_PTN;
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002015 uint32_t ret = 0;
Mayank Groverddd348d2018-01-23 14:07:09 +05302016 uint32_t device_info_sz = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07002017
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002018 if ((index = partition_get_index("devinfo")) < 0)
2019 {
2020 devinfo_present = false;
2021 index = partition_get_index("aboot");
2022 }
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07002023
Shashank Mittal162244e2011-08-08 19:01:25 -07002024 ptn = partition_get_offset(index);
2025 if(ptn == 0)
2026 {
2027 return;
2028 }
2029
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07002030 mmc_set_lun(partition_get_lun(index));
2031
Shashank Mittal162244e2011-08-08 19:01:25 -07002032 size = partition_get_size(index);
2033
Mayank Groverddd348d2018-01-23 14:07:09 +05302034 device_info_sz = ROUND_TO_PAGE(sizeof(struct device_info),
2035 mmc_blocksize_mask);
2036 if (device_info_sz == UINT_MAX)
2037 {
2038 dprintf(CRITICAL, "ERROR: Incorrect blocksize of card\n");
2039 return;
2040 }
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07002041
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002042 if (devinfo_present)
Mayank Groverddd348d2018-01-23 14:07:09 +05302043 ret = mmc_read(ptn, (void *)info_buf, device_info_sz);
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002044 else
Mayank Groverddd348d2018-01-23 14:07:09 +05302045 ret = mmc_read((ptn + size - device_info_sz), (void *)info_buf, device_info_sz);
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002046 if (ret)
Shashank Mittal162244e2011-08-08 19:01:25 -07002047 {
2048 dprintf(CRITICAL, "ERROR: Cannot read device info\n");
Sridhar Parasurame1a97922015-07-27 15:31:22 -07002049 ASSERT(0);
Shashank Mittal162244e2011-08-08 19:01:25 -07002050 }
Shashank Mittal162244e2011-08-08 19:01:25 -07002051}
2052
2053void write_device_info_flash(device_info *dev)
2054{
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002055 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
Shashank Mittal162244e2011-08-08 19:01:25 -07002056 struct ptentry *ptn;
2057 struct ptable *ptable;
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002058 if(info == NULL)
2059 {
2060 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
2061 ASSERT(0);
2062 }
2063 info_buf = info;
Shashank Mittal162244e2011-08-08 19:01:25 -07002064 ptable = flash_get_ptable();
2065 if (ptable == NULL)
2066 {
2067 dprintf(CRITICAL, "ERROR: Partition table not found\n");
2068 return;
2069 }
2070
2071 ptn = ptable_find(ptable, "devinfo");
2072 if (ptn == NULL)
2073 {
Smita Ghosh670c8b82015-05-07 09:30:03 -07002074 dprintf(CRITICAL, "ERROR: No devinfo partition found\n");
Shashank Mittal162244e2011-08-08 19:01:25 -07002075 return;
2076 }
2077
Mayank Groverdedbc892017-10-24 13:41:34 +05302078 memset(info, 0, BOOT_IMG_MAX_PAGE_SIZE);
Shashank Mittal162244e2011-08-08 19:01:25 -07002079 memcpy(info, dev, sizeof(device_info));
2080
2081 if (flash_write(ptn, 0, (void *)info_buf, page_size))
2082 {
2083 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
2084 return;
2085 }
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002086 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07002087}
2088
vijay kumarc65876c2015-04-24 13:29:16 +05302089static int read_allow_oem_unlock(device_info *dev)
2090{
vijay kumarc65876c2015-04-24 13:29:16 +05302091 unsigned offset;
2092 int index;
2093 unsigned long long ptn;
2094 unsigned long long ptn_size;
2095 unsigned blocksize = mmc_get_device_blocksize();
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07002096 STACKBUF_DMA_ALIGN(buf, blocksize);
vijay kumarc65876c2015-04-24 13:29:16 +05302097
vijay kumarca2e6812015-07-08 20:28:25 +05302098 index = partition_get_index(frp_ptns[0]);
vijay kumarc65876c2015-04-24 13:29:16 +05302099 if (index == INVALID_PTN)
2100 {
vijay kumarca2e6812015-07-08 20:28:25 +05302101 index = partition_get_index(frp_ptns[1]);
2102 if (index == INVALID_PTN)
2103 {
2104 dprintf(CRITICAL, "Neither '%s' nor '%s' partition found\n", frp_ptns[0],frp_ptns[1]);
2105 return -1;
2106 }
vijay kumarc65876c2015-04-24 13:29:16 +05302107 }
2108
2109 ptn = partition_get_offset(index);
2110 ptn_size = partition_get_size(index);
2111 offset = ptn_size - blocksize;
2112
Mayank Grover48860402016-11-29 12:34:53 +05302113 /* Set Lun for partition */
2114 mmc_set_lun(partition_get_lun(index));
2115
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07002116 if (mmc_read(ptn + offset, (void *)buf, blocksize))
vijay kumarc65876c2015-04-24 13:29:16 +05302117 {
2118 dprintf(CRITICAL, "Reading MMC failed\n");
2119 return -1;
2120 }
2121
2122 /*is_allow_unlock is a bool value stored at the LSB of last byte*/
2123 is_allow_unlock = buf[blocksize-1] & 0x01;
2124 return 0;
2125}
2126
2127static int write_allow_oem_unlock(bool allow_unlock)
2128{
vijay kumarc65876c2015-04-24 13:29:16 +05302129 unsigned offset;
vijay kumarc65876c2015-04-24 13:29:16 +05302130 int index;
2131 unsigned long long ptn;
2132 unsigned long long ptn_size;
2133 unsigned blocksize = mmc_get_device_blocksize();
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07002134 STACKBUF_DMA_ALIGN(buf, blocksize);
vijay kumarc65876c2015-04-24 13:29:16 +05302135
vijay kumarca2e6812015-07-08 20:28:25 +05302136 index = partition_get_index(frp_ptns[0]);
vijay kumarc65876c2015-04-24 13:29:16 +05302137 if (index == INVALID_PTN)
2138 {
vijay kumarca2e6812015-07-08 20:28:25 +05302139 index = partition_get_index(frp_ptns[1]);
2140 if (index == INVALID_PTN)
2141 {
2142 dprintf(CRITICAL, "Neither '%s' nor '%s' partition found\n", frp_ptns[0],frp_ptns[1]);
2143 return -1;
2144 }
vijay kumarc65876c2015-04-24 13:29:16 +05302145 }
2146
2147 ptn = partition_get_offset(index);
2148 ptn_size = partition_get_size(index);
2149 offset = ptn_size - blocksize;
Mayank Grover48860402016-11-29 12:34:53 +05302150 mmc_set_lun(partition_get_lun(index));
vijay kumarc65876c2015-04-24 13:29:16 +05302151
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07002152 if (mmc_read(ptn + offset, (void *)buf, blocksize))
vijay kumarc65876c2015-04-24 13:29:16 +05302153 {
2154 dprintf(CRITICAL, "Reading MMC failed\n");
2155 return -1;
2156 }
2157
2158 /*is_allow_unlock is a bool value stored at the LSB of last byte*/
2159 buf[blocksize-1] = allow_unlock;
2160 if (mmc_write(ptn + offset, blocksize, buf))
2161 {
2162 dprintf(CRITICAL, "Writing MMC failed\n");
2163 return -1;
2164 }
2165
2166 return 0;
2167}
2168
Shashank Mittal162244e2011-08-08 19:01:25 -07002169void read_device_info_flash(device_info *dev)
2170{
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002171 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
Shashank Mittal162244e2011-08-08 19:01:25 -07002172 struct ptentry *ptn;
2173 struct ptable *ptable;
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002174 if(info == NULL)
2175 {
2176 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
2177 ASSERT(0);
2178 }
2179 info_buf = info;
Shashank Mittal162244e2011-08-08 19:01:25 -07002180 ptable = flash_get_ptable();
2181 if (ptable == NULL)
2182 {
2183 dprintf(CRITICAL, "ERROR: Partition table not found\n");
2184 return;
2185 }
2186
2187 ptn = ptable_find(ptable, "devinfo");
2188 if (ptn == NULL)
2189 {
Smita Ghosh670c8b82015-05-07 09:30:03 -07002190 dprintf(CRITICAL, "ERROR: No devinfo partition found\n");
Shashank Mittal162244e2011-08-08 19:01:25 -07002191 return;
2192 }
2193
2194 if (flash_read(ptn, 0, (void *)info_buf, page_size))
2195 {
2196 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
2197 return;
2198 }
2199
2200 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
2201 {
Shashank Mittal162244e2011-08-08 19:01:25 -07002202 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
2203 info->is_unlocked = 0;
Shashank Mittala0032282011-08-26 14:50:11 -07002204 info->is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07002205 write_device_info_flash(info);
2206 }
2207 memcpy(dev, info, sizeof(device_info));
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002208 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07002209}
2210
2211void write_device_info(device_info *dev)
2212{
2213 if(target_is_emmc_boot())
2214 {
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002215 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
2216 if(info == NULL)
2217 {
2218 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
2219 ASSERT(0);
2220 }
2221 info_buf = info;
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002222 memcpy(info, dev, sizeof(struct device_info));
2223
2224#if USE_RPMB_FOR_DEVINFO
Mayank Grover912eaa62017-10-26 12:08:53 +05302225 if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05302226 is_secure_boot_enable()) {
2227 if((write_device_info_rpmb((void*) info, PAGE_SIZE)) < 0)
2228 ASSERT(0);
Sridhar Parasurame1a97922015-07-27 15:31:22 -07002229 }
Channagoud Kadabic80cb492015-04-28 16:08:28 -07002230 else
2231 write_device_info_mmc(info);
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002232#else
2233 write_device_info_mmc(info);
2234#endif
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002235 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07002236 }
2237 else
2238 {
2239 write_device_info_flash(dev);
2240 }
2241}
2242
2243void read_device_info(device_info *dev)
2244{
2245 if(target_is_emmc_boot())
2246 {
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002247 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
2248 if(info == NULL)
2249 {
2250 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
2251 ASSERT(0);
2252 }
2253 info_buf = info;
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002254
2255#if USE_RPMB_FOR_DEVINFO
Mayank Grover912eaa62017-10-26 12:08:53 +05302256 if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05302257 is_secure_boot_enable()) {
2258 if((read_device_info_rpmb((void*) info, PAGE_SIZE)) < 0)
2259 ASSERT(0);
Sridhar Parasurame1a97922015-07-27 15:31:22 -07002260 }
Channagoud Kadabic80cb492015-04-28 16:08:28 -07002261 else
2262 read_device_info_mmc(info);
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002263#else
2264 read_device_info_mmc(info);
2265#endif
2266
2267 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
2268 {
2269 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
lijuang511a2b52015-08-14 20:50:51 +08002270 if (is_secure_boot_enable()) {
Channagoud Kadabi05f78ba2015-07-06 11:58:14 -07002271 info->is_unlocked = 0;
Mayank Grover889be1b2017-09-12 20:12:23 +05302272#if VERIFIED_BOOT
Mayank Grover912eaa62017-10-26 12:08:53 +05302273 if (VB_M <= target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +05302274 info->is_unlock_critical = 0;
Parth Dixitddbc7352015-10-18 03:13:31 +05302275#endif
lijuang511a2b52015-08-14 20:50:51 +08002276 } else {
Channagoud Kadabi2fda4092015-07-07 13:34:11 -07002277 info->is_unlocked = 1;
Mayank Grover889be1b2017-09-12 20:12:23 +05302278#if VERIFIED_BOOT
Mayank Grover912eaa62017-10-26 12:08:53 +05302279 if (VB_M <= target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +05302280 info->is_unlock_critical = 1;
Parth Dixitddbc7352015-10-18 03:13:31 +05302281#endif
lijuang511a2b52015-08-14 20:50:51 +08002282 }
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002283 info->is_tampered = 0;
2284 info->charger_screen_enabled = 0;
Mayank Grover889be1b2017-09-12 20:12:23 +05302285#if VERIFIED_BOOT
Mayank Grover912eaa62017-10-26 12:08:53 +05302286 if (VB_M <= target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +05302287 info->verity_mode = 1; //enforcing by default
Parth Dixitddbc7352015-10-18 03:13:31 +05302288#endif
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002289 write_device_info(info);
2290 }
2291 memcpy(dev, info, sizeof(device_info));
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002292 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07002293 }
2294 else
2295 {
2296 read_device_info_flash(dev);
2297 }
2298}
2299
2300void reset_device_info()
2301{
2302 dprintf(ALWAYS, "reset_device_info called.");
Shashank Mittala0032282011-08-26 14:50:11 -07002303 device.is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07002304 write_device_info(&device);
2305}
2306
2307void set_device_root()
2308{
2309 dprintf(ALWAYS, "set_device_root called.");
Shashank Mittala0032282011-08-26 14:50:11 -07002310 device.is_tampered = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -07002311 write_device_info(&device);
2312}
2313
lijuang4ece1e72015-08-14 21:02:36 +08002314/* set device unlock value
2315 * Must check FRP before call this function
2316 * Need to wipe data when unlock status changed
2317 * type 0: oem unlock
2318 * type 1: unlock critical
2319 * status 0: unlock as false
2320 * status 1: lock as true
2321 */
2322void set_device_unlock_value(int type, bool status)
lijuang21f12f52015-08-22 16:22:19 +08002323{
lijuang4ece1e72015-08-14 21:02:36 +08002324 if (type == UNLOCK)
2325 device.is_unlocked = status;
Mayank Grover889be1b2017-09-12 20:12:23 +05302326#if VERIFIED_BOOT
Mayank Grover912eaa62017-10-26 12:08:53 +05302327 else if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05302328 type == UNLOCK_CRITICAL)
2329 device.is_unlock_critical = status;
Parth Dixitddbc7352015-10-18 03:13:31 +05302330#endif
lijuang4ece1e72015-08-14 21:02:36 +08002331 write_device_info(&device);
2332}
2333
2334static void set_device_unlock(int type, bool status)
2335{
2336 int is_unlocked = -1;
2337 char response[MAX_RSP_SIZE];
2338
2339 /* check device unlock status if it is as expected */
2340 if (type == UNLOCK)
2341 is_unlocked = device.is_unlocked;
Mayank Grover889be1b2017-09-12 20:12:23 +05302342#if VERIFIED_BOOT
Mayank Grover912eaa62017-10-26 12:08:53 +05302343 if(VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05302344 type == UNLOCK_CRITICAL)
2345 {
2346 is_unlocked = device.is_unlock_critical;
2347 }
Parth Dixitddbc7352015-10-18 03:13:31 +05302348#endif
lijuang4ece1e72015-08-14 21:02:36 +08002349 if (is_unlocked == status) {
2350 snprintf(response, sizeof(response), "\tDevice already : %s", (status ? "unlocked!" : "locked!"));
2351 fastboot_info(response);
2352 fastboot_okay("");
2353 return;
2354 }
2355
2356 /* status is true, it means to unlock device */
2357 if (status) {
lijuang21f12f52015-08-22 16:22:19 +08002358 if(!is_allow_unlock) {
2359 fastboot_fail("oem unlock is not allowed");
2360 return;
2361 }
2362
lijuang4ece1e72015-08-14 21:02:36 +08002363#if FBCON_DISPLAY_MSG
lijuang9a7d3b92015-11-30 14:41:24 +08002364 display_unlock_menu(type);
lijuang4ece1e72015-08-14 21:02:36 +08002365 fastboot_okay("");
2366 return;
2367#else
2368 if (type == UNLOCK) {
2369 fastboot_fail("Need wipe userdata. Do 'fastboot oem unlock-go'");
2370 return;
2371 }
2372#endif
lijuang21f12f52015-08-22 16:22:19 +08002373 }
lijuang4ece1e72015-08-14 21:02:36 +08002374
2375 set_device_unlock_value(type, status);
2376
2377 /* wipe data */
2378 struct recovery_message msg;
Kishor PK60a68212017-05-08 16:55:57 +05302379 memset(&msg, 0, sizeof(msg));
lijuang4ece1e72015-08-14 21:02:36 +08002380 snprintf(msg.recovery, sizeof(msg.recovery), "recovery\n--wipe_data");
2381 write_misc(0, &msg, sizeof(msg));
2382
2383 fastboot_okay("");
2384 reboot_device(RECOVERY_MODE);
lijuang21f12f52015-08-22 16:22:19 +08002385}
2386
lijuang511a2b52015-08-14 20:50:51 +08002387static bool critical_flash_allowed(const char * entry)
2388{
2389 uint32_t i = 0;
2390 if (entry == NULL)
2391 return false;
2392
2393 for (i = 0; i < ARRAY_SIZE(critical_flash_allowed_ptn); i++) {
2394 if(!strcmp(entry, critical_flash_allowed_ptn[i]))
2395 return true;
2396 }
2397 return false;
Matthew Qin271927e2015-03-31 22:07:07 -04002398}
2399
2400#if DEVICE_TREE
Amol Jadicb524072012-08-09 16:40:18 -07002401int copy_dtb(uint8_t *boot_image_start, unsigned int scratch_offset)
2402{
2403 uint32 dt_image_offset = 0;
Amol Jadicb524072012-08-09 16:40:18 -07002404 uint32_t n;
Amol Jadicb524072012-08-09 16:40:18 -07002405 struct dt_table *table;
2406 struct dt_entry dt_entry;
2407 uint32_t dt_hdr_size;
2408 unsigned int compressed_size = 0;
2409 unsigned int dtb_size = 0;
2410 unsigned int out_avai_len = 0;
2411 unsigned char *out_addr = NULL;
2412 unsigned char *best_match_dt_addr = NULL;
2413 int rc;
2414
2415 struct boot_img_hdr *hdr = (struct boot_img_hdr *) (boot_image_start);
2416
Parth Dixit4097b622016-03-15 14:42:27 +05302417#ifndef OSVERSION_IN_BOOTIMAGE
2418 dt_size = hdr->dt_size;
2419#endif
2420
2421 if(dt_size != 0) {
Amol Jadicb524072012-08-09 16:40:18 -07002422 /* add kernel offset */
2423 dt_image_offset += page_size;
2424 n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
2425 dt_image_offset += n;
2426
2427 /* add ramdisk offset */
2428 n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
2429 dt_image_offset += n;
2430
2431 /* add second offset */
2432 if(hdr->second_size != 0) {
2433 n = ROUND_TO_PAGE(hdr->second_size, page_mask);
2434 dt_image_offset += n;
2435 }
2436
2437 /* offset now point to start of dt.img */
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07002438 table = (struct dt_table*)(boot_image_start + dt_image_offset);
Amol Jadicb524072012-08-09 16:40:18 -07002439
Deepa Dinamani19648b42013-09-05 17:05:55 -07002440 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Amol Jadicb524072012-08-09 16:40:18 -07002441 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
2442 return -1;
2443 }
P.V. Phani Kumar82916762016-03-09 09:20:19 +05302444
2445 /* Its Error if, dt_hdr_size (table->num_entries * dt_entry size + Dev_Tree Header)
2446 goes beyound hdr->dt_size*/
Parth Dixit4097b622016-03-15 14:42:27 +05302447 if (dt_hdr_size > ROUND_TO_PAGE(dt_size,hdr->page_size)) {
P.V. Phani Kumar82916762016-03-09 09:20:19 +05302448 dprintf(CRITICAL, "ERROR: Invalid Device Tree size \n");
2449 return -1;
2450 }
2451
Joel Kingaa335dc2013-06-03 16:11:08 -07002452 /* Find index of device tree within device tree table */
2453 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Amol Jadicb524072012-08-09 16:40:18 -07002454 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
2455 return -1;
2456 }
2457
Matthew Qin271927e2015-03-31 22:07:07 -04002458 best_match_dt_addr = (unsigned char *)boot_image_start + dt_image_offset + dt_entry.offset;
2459 if (is_gzip_package(best_match_dt_addr, dt_entry.size))
2460 {
2461 out_addr = (unsigned char *)target_get_scratch_address() + scratch_offset;
2462 out_avai_len = target_get_max_flash_size() - scratch_offset;
Matthew Qin0b15b322015-05-19 05:20:54 -04002463 dprintf(INFO, "decompressing dtb: start\n");
Matthew Qin271927e2015-03-31 22:07:07 -04002464 rc = decompress(best_match_dt_addr,
2465 dt_entry.size, out_addr, out_avai_len,
2466 &compressed_size, &dtb_size);
2467 if (rc)
2468 {
Matthew Qin0b15b322015-05-19 05:20:54 -04002469 dprintf(CRITICAL, "decompressing dtb failed!!!\n");
Matthew Qin271927e2015-03-31 22:07:07 -04002470 ASSERT(0);
2471 }
2472
Matthew Qin0b15b322015-05-19 05:20:54 -04002473 dprintf(INFO, "decompressing dtb: done\n");
Matthew Qin271927e2015-03-31 22:07:07 -04002474 best_match_dt_addr = out_addr;
2475 } else {
2476 dtb_size = dt_entry.size;
2477 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002478 /* Validate and Read device device tree in the "tags_add */
Mayank Grovere559cec2017-10-17 15:12:03 +05302479 if (check_aboot_addr_range_overlap(hdr->tags_addr, dtb_size) ||
2480 check_ddr_addr_range_bound(hdr->tags_addr, dtb_size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002481 {
Mayank Grovere559cec2017-10-17 15:12:03 +05302482 dprintf(CRITICAL, "Device tree addresses are not valid.\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002483 return -1;
2484 }
2485
Amol Jadicb524072012-08-09 16:40:18 -07002486 /* Read device device tree in the "tags_add */
Matthew Qin271927e2015-03-31 22:07:07 -04002487 memmove((void*) hdr->tags_addr, (void *)best_match_dt_addr, dtb_size);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002488 } else
2489 return -1;
Amol Jadicb524072012-08-09 16:40:18 -07002490
2491 /* Everything looks fine. Return success. */
2492 return 0;
2493}
2494#endif
2495
Brian Swetland9c4c0752009-01-25 16:23:50 -08002496void cmd_boot(const char *arg, void *data, unsigned sz)
2497{
Amit Blay8a510302015-08-17 09:20:01 +03002498#ifdef MDTP_SUPPORT
2499 static bool is_mdtp_activated = 0;
2500#endif /* MDTP_SUPPORT */
Brian Swetland9c4c0752009-01-25 16:23:50 -08002501 unsigned kernel_actual;
2502 unsigned ramdisk_actual;
Kishor PKd8ddcad2017-07-27 13:53:57 +05302503 unsigned second_actual;
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002504 uint32_t image_actual;
2505 uint32_t dt_actual = 0;
P.V. Phani Kumareecfd822016-03-09 20:09:03 +05302506 uint32_t sig_actual = 0;
Kishor PK5134b332017-05-09 17:50:08 +05302507 uint32_t sig_size = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08002508 struct boot_img_hdr *hdr = NULL;
2509 struct kernel64_hdr *kptr = NULL;
Brian Swetland9c4c0752009-01-25 16:23:50 -08002510 char *ptr = ((char*) data);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002511 int ret = 0;
2512 uint8_t dtb_copied = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08002513 unsigned int out_len = 0;
2514 unsigned int out_avai_len = 0;
2515 unsigned char *out_addr = NULL;
2516 uint32_t dtb_offset = 0;
2517 unsigned char *kernel_start_addr = NULL;
2518 unsigned int kernel_size = 0;
Matthew Qin271927e2015-03-31 22:07:07 -04002519 unsigned int scratch_offset = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08002520
lijuang2008ff22016-03-07 17:56:27 +08002521#if FBCON_DISPLAY_MSG
2522 /* Exit keys' detection thread firstly */
2523 exit_menu_keys_detection();
2524#endif
2525
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002526#if VERIFIED_BOOT
Channagoud Kadabi6d5375e2015-06-23 17:15:42 -07002527 if(target_build_variant_user() && !device.is_unlocked)
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002528 {
2529 fastboot_fail("unlock device to use this command");
lijuang2008ff22016-03-07 17:56:27 +08002530 goto boot_failed;
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002531 }
2532#endif
2533
Brian Swetland9c4c0752009-01-25 16:23:50 -08002534 if (sz < sizeof(hdr)) {
2535 fastboot_fail("invalid bootimage header");
lijuang2008ff22016-03-07 17:56:27 +08002536 goto boot_failed;
Brian Swetland9c4c0752009-01-25 16:23:50 -08002537 }
2538
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002539 hdr = (struct boot_img_hdr *)data;
Brian Swetland9c4c0752009-01-25 16:23:50 -08002540
2541 /* ensure commandline is terminated */
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002542 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -08002543
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002544 if(target_is_emmc_boot() && hdr->page_size) {
2545 page_size = hdr->page_size;
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07002546 page_mask = page_size - 1;
2547 }
2548
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002549 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
2550 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Kishor PKd8ddcad2017-07-27 13:53:57 +05302551 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002552#if DEVICE_TREE
Parth Dixit4097b622016-03-15 14:42:27 +05302553#ifndef OSVERSION_IN_BOOTIMAGE
2554 dt_size = hdr->dt_size;
2555#endif
2556 dt_actual = ROUND_TO_PAGE(dt_size, page_mask);
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002557#endif
2558
2559 image_actual = ADD_OF(page_size, kernel_actual);
2560 image_actual = ADD_OF(image_actual, ramdisk_actual);
Kishor PKd8ddcad2017-07-27 13:53:57 +05302561 image_actual = ADD_OF(image_actual, second_actual);
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002562 image_actual = ADD_OF(image_actual, dt_actual);
2563
Kishor PK5134b332017-05-09 17:50:08 +05302564 /* Checking to prevent oob access in read_der_message_length */
2565 if (image_actual > sz) {
2566 fastboot_fail("bootimage header fields are invalid");
2567 goto boot_failed;
2568 }
2569 sig_size = sz - image_actual;
2570
P.V. Phani Kumareecfd822016-03-09 20:09:03 +05302571 if (target_use_signed_kernel() && (!device.is_unlocked)) {
2572 /* Calculate the signature length from boot image */
2573 sig_actual = read_der_message_length(
Kishor PK5134b332017-05-09 17:50:08 +05302574 (unsigned char*)(data + image_actual), sig_size);
Channagoud Kadabi3d839012014-06-26 14:26:39 -07002575 image_actual = ADD_OF(image_actual, sig_actual);
2576
Kishor PK5134b332017-05-09 17:50:08 +05302577 if (image_actual > sz) {
2578 fastboot_fail("bootimage header fields are invalid");
2579 goto boot_failed;
2580 }
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002581 }
2582
Sridhar Parasuram361e4cd2015-09-24 11:19:19 -07002583 // Initialize boot state before trying to verify boot.img
2584#if VERIFIED_BOOT
Channagoud Kadabi699466e2015-11-03 12:37:42 -08002585 boot_verifier_init();
Mayank Groverb337e932017-01-18 20:00:40 +05302586#endif
Channagoud Kadabibf6ce7d2015-09-17 13:25:35 -07002587 /* Handle overflow if the input image size is greater than
2588 * boot image buffer can hold
2589 */
2590 if ((target_get_max_flash_size() - (image_actual - sig_actual)) < page_size)
2591 {
2592 fastboot_fail("booimage: size is greater than boot image buffer can hold");
lijuang2008ff22016-03-07 17:56:27 +08002593 goto boot_failed;
Channagoud Kadabibf6ce7d2015-09-17 13:25:35 -07002594 }
Sridhar Parasuram361e4cd2015-09-24 11:19:19 -07002595
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002596 /* Verify the boot image
2597 * device & page_size are initialized in aboot_init
2598 */
Sridhar Parasuram7ae9fbc2015-09-17 09:56:30 -07002599 if (target_use_signed_kernel() && (!device.is_unlocked)) {
Channagoud Kadabi3d839012014-06-26 14:26:39 -07002600 /* Pass size excluding signature size, otherwise we would try to
2601 * access signature beyond its length
2602 */
2603 verify_signed_bootimg((uint32_t)data, (image_actual - sig_actual));
Sridhar Parasuram7ae9fbc2015-09-17 09:56:30 -07002604 }
Amit Blay4aa292f2015-04-28 21:55:59 +03002605#ifdef MDTP_SUPPORT
2606 else
2607 {
Shay Nachmani062aa3f2015-05-17 17:28:44 +03002608 /* fastboot boot is not allowed when MDTP is activated */
Amit Blay4aa292f2015-04-28 21:55:59 +03002609 mdtp_ext_partition_verification_t ext_partition;
Amit Blay8a510302015-08-17 09:20:01 +03002610
2611 if (!is_mdtp_activated) {
2612 ext_partition.partition = MDTP_PARTITION_NONE;
2613 mdtp_fwlock_verify_lock(&ext_partition);
2614 }
Amit Blay4aa292f2015-04-28 21:55:59 +03002615 }
Shay Nachmani062aa3f2015-05-17 17:28:44 +03002616
Amir Kotzer7c768c02016-04-13 09:08:05 +03002617 /* If mdtp state cannot be validate, block fastboot boot*/
2618 if(mdtp_activated(&is_mdtp_activated)){
2619 dprintf(CRITICAL, "mdtp_activated cannot validate state.\n");
2620 dprintf(CRITICAL, "Can not proceed with fastboot boot command.\n");
2621 goto boot_failed;
2622 }
Shay Nachmani062aa3f2015-05-17 17:28:44 +03002623 if(is_mdtp_activated){
2624 dprintf(CRITICAL, "fastboot boot command is not available.\n");
lijuang2008ff22016-03-07 17:56:27 +08002625 goto boot_failed;
Shay Nachmani062aa3f2015-05-17 17:28:44 +03002626 }
Amit Blay4aa292f2015-04-28 21:55:59 +03002627#endif /* MDTP_SUPPORT */
2628
Sridhar Parasuram361e4cd2015-09-24 11:19:19 -07002629#if VERIFIED_BOOT
Mayank Grover912eaa62017-10-26 12:08:53 +05302630 if (VB_M <= target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +05302631 {
2632 /* set boot and system versions. */
2633 set_os_version((unsigned char *)data);
2634 // send root of trust
2635 if(!send_rot_command((uint32_t)device.is_unlocked))
2636 ASSERT(0);
2637 }
Parth Dixitbc9b6492015-10-18 00:41:38 +05302638#endif
Channagoud Kadabia22144f2013-03-20 11:49:01 -07002639 /*
Matthew Qinbb7923d2015-02-09 10:56:09 +08002640 * Check if the kernel image is a gzip package. If yes, need to decompress it.
2641 * If not, continue booting.
2642 */
2643 if (is_gzip_package((unsigned char *)(data + page_size), hdr->kernel_size))
2644 {
2645 out_addr = (unsigned char *)target_get_scratch_address();
2646 out_addr = (unsigned char *)(out_addr + image_actual + page_size);
2647 out_avai_len = target_get_max_flash_size() - image_actual - page_size;
Matthew Qin0b15b322015-05-19 05:20:54 -04002648 dprintf(INFO, "decompressing kernel image: start\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08002649 ret = decompress((unsigned char *)(ptr + page_size),
2650 hdr->kernel_size, out_addr, out_avai_len,
2651 &dtb_offset, &out_len);
2652 if (ret)
2653 {
Matthew Qin0b15b322015-05-19 05:20:54 -04002654 dprintf(CRITICAL, "decompressing image failed!!!\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08002655 ASSERT(0);
2656 }
2657
Matthew Qin0b15b322015-05-19 05:20:54 -04002658 dprintf(INFO, "decompressing kernel image: done\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08002659 kptr = (struct kernel64_hdr *)out_addr;
2660 kernel_start_addr = out_addr;
2661 kernel_size = out_len;
2662 } else {
2663 kptr = (struct kernel64_hdr*)((char *)data + page_size);
2664 kernel_start_addr = (unsigned char *)((char *)data + page_size);
2665 kernel_size = hdr->kernel_size;
2666 }
2667
2668 /*
Channagoud Kadabia22144f2013-03-20 11:49:01 -07002669 * Update the kernel/ramdisk/tags address if the boot image header
2670 * has default values, these default values come from mkbootimg when
2671 * the boot image is flashed using fastboot flash:raw
2672 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08002673 update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr));
Dima Zavin3cadfff2013-03-21 14:30:48 -07002674
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002675 /* Get virtual addresses since the hdr saves physical addresses. */
2676 hdr->kernel_addr = VA(hdr->kernel_addr);
2677 hdr->ramdisk_addr = VA(hdr->ramdisk_addr);
2678 hdr->tags_addr = VA(hdr->tags_addr);
Brian Swetland9c4c0752009-01-25 16:23:50 -08002679
Matthew Qinbb7923d2015-02-09 10:56:09 +08002680 kernel_size = ROUND_TO_PAGE(kernel_size, page_mask);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002681 /* Check if the addresses in the header are valid. */
Matthew Qinbb7923d2015-02-09 10:56:09 +08002682 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_size) ||
Mayank Grovere559cec2017-10-17 15:12:03 +05302683 check_ddr_addr_range_bound(hdr->kernel_addr, kernel_size) ||
2684 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual) ||
2685 check_ddr_addr_range_bound(hdr->ramdisk_addr, ramdisk_actual))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002686 {
Mayank Grovere559cec2017-10-17 15:12:03 +05302687 dprintf(CRITICAL, "kernel/ramdisk addresses are not valid.\n");
lijuang2008ff22016-03-07 17:56:27 +08002688 goto boot_failed;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002689 }
2690
Amol Jadicb524072012-08-09 16:40:18 -07002691#if DEVICE_TREE
Matthew Qin271927e2015-03-31 22:07:07 -04002692 scratch_offset = image_actual + page_size + out_len;
Amol Jadicb524072012-08-09 16:40:18 -07002693 /* find correct dtb and copy it to right location */
Matthew Qin271927e2015-03-31 22:07:07 -04002694 ret = copy_dtb(data, scratch_offset);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002695
2696 dtb_copied = !ret ? 1 : 0;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002697#else
Mayank Grovere559cec2017-10-17 15:12:03 +05302698 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE) ||
2699 check_ddr_addr_range_bound(hdr->tags_addr, MAX_TAGS_SIZE))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002700 {
Mayank Grovere559cec2017-10-17 15:12:03 +05302701 dprintf(CRITICAL, "Tags addresses are not valid.\n");
lijuang2008ff22016-03-07 17:56:27 +08002702 goto boot_failed;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002703 }
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002704#endif
2705
2706 /* Load ramdisk & kernel */
2707 memmove((void*) hdr->ramdisk_addr, ptr + page_size + kernel_actual, hdr->ramdisk_size);
Matthew Qinbb7923d2015-02-09 10:56:09 +08002708 memmove((void*) hdr->kernel_addr, (char*) (kernel_start_addr), kernel_size);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002709
2710#if DEVICE_TREE
Mayank Grovere559cec2017-10-17 15:12:03 +05302711 if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual) ||
2712 check_ddr_addr_range_bound(hdr->tags_addr, kernel_actual))
Matthew Qinbb7923d2015-02-09 10:56:09 +08002713 {
Mayank Grovere559cec2017-10-17 15:12:03 +05302714 dprintf(CRITICAL, "Tags addresses are not valid.\n");
lijuang2008ff22016-03-07 17:56:27 +08002715 goto boot_failed;
Matthew Qinbb7923d2015-02-09 10:56:09 +08002716 }
2717
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002718 /*
2719 * If dtb is not found look for appended DTB in the kernel.
2720 * If appended dev tree is found, update the atags with
2721 * memory address to the DTB appended location on RAM.
2722 * Else update with the atags address in the kernel header
2723 */
2724 if (!dtb_copied) {
2725 void *dtb;
Matthew Qinbb7923d2015-02-09 10:56:09 +08002726 dtb = dev_tree_appended((void*)(ptr + page_size),
2727 hdr->kernel_size, dtb_offset,
Dima Zavine63e5572013-05-03 12:23:06 -07002728 (void *)hdr->tags_addr);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002729 if (!dtb) {
2730 fastboot_fail("dtb not found");
lijuang2008ff22016-03-07 17:56:27 +08002731 goto boot_failed;
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002732 }
Amol Jadicb524072012-08-09 16:40:18 -07002733 }
2734#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08002735
2736 fastboot_okay("");
Amol Jadi7c4316c2013-10-07 14:19:26 -07002737 fastboot_stop();
Brian Swetland9c4c0752009-01-25 16:23:50 -08002738
Dima Zavin77e41f32013-03-06 16:10:43 -08002739 boot_linux((void*) hdr->kernel_addr, (void*) hdr->tags_addr,
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002740 (const char*) hdr->cmdline, board_machtype(),
2741 (void*) hdr->ramdisk_addr, hdr->ramdisk_size);
lijuang2008ff22016-03-07 17:56:27 +08002742
2743 /* fastboot already stop, it's no need to show fastboot menu */
2744 return;
2745boot_failed:
2746#if FBCON_DISPLAY_MSG
2747 /* revert to fastboot menu if boot failed */
2748 display_fastboot_menu();
2749#endif
2750 return;
Brian Swetland9c4c0752009-01-25 16:23:50 -08002751}
2752
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002753void cmd_erase_nand(const char *arg, void *data, unsigned sz)
Dima Zavin214cc642009-01-26 11:16:21 -08002754{
2755 struct ptentry *ptn;
2756 struct ptable *ptable;
2757
2758 ptable = flash_get_ptable();
2759 if (ptable == NULL) {
2760 fastboot_fail("partition table doesn't exist");
2761 return;
2762 }
2763
2764 ptn = ptable_find(ptable, arg);
2765 if (ptn == NULL) {
2766 fastboot_fail("unknown partition name");
2767 return;
2768 }
2769
2770 if (flash_erase(ptn)) {
2771 fastboot_fail("failed to erase partition");
2772 return;
2773 }
2774 fastboot_okay("");
2775}
2776
Bikas Gurungd48bd242010-09-04 19:54:32 -07002777
2778void cmd_erase_mmc(const char *arg, void *data, unsigned sz)
2779{
2780 unsigned long long ptn = 0;
Oliver Wangcee448d2013-10-22 18:40:13 +08002781 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07002782 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002783 uint8_t lun = 0;
Vijay Kumar Pendoti0b21f462016-05-02 17:09:18 +05302784 char *footer = NULL;
Bikas Gurungd48bd242010-09-04 19:54:32 -07002785
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002786#if VERIFIED_BOOT
2787 if(!strcmp(arg, KEYSTORE_PTN_NAME))
2788 {
2789 if(!device.is_unlocked)
2790 {
2791 fastboot_fail("unlock device to erase keystore");
2792 return;
2793 }
2794 }
2795#endif
2796
Kinson Chikf1a43512011-07-14 11:28:39 -07002797 index = partition_get_index(arg);
2798 ptn = partition_get_offset(index);
Oliver Wangcee448d2013-10-22 18:40:13 +08002799 size = partition_get_size(index);
Neeti Desaica8c9602011-10-06 11:40:00 -07002800
Kinson Chikf1a43512011-07-14 11:28:39 -07002801 if(ptn == 0) {
Neeti Desaica8c9602011-10-06 11:40:00 -07002802 fastboot_fail("Partition table doesn't exist\n");
Bikas Gurungd48bd242010-09-04 19:54:32 -07002803 return;
2804 }
Kun Liang2f1601a2013-08-12 16:29:54 +08002805
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002806 lun = partition_get_lun(index);
2807 mmc_set_lun(lun);
2808
Sridhar Parasuramd7957122015-02-27 11:33:40 -08002809 if (platform_boot_dev_isemmc())
2810 {
2811 if (mmc_erase_card(ptn, size)) {
2812 fastboot_fail("failed to erase partition\n");
2813 return;
2814 }
2815 } else {
2816 BUF_DMA_ALIGN(out, DEFAULT_ERASE_SIZE);
2817 size = partition_get_size(index);
2818 if (size > DEFAULT_ERASE_SIZE)
2819 size = DEFAULT_ERASE_SIZE;
Kun Liang2f1601a2013-08-12 16:29:54 +08002820
Sridhar Parasuramd7957122015-02-27 11:33:40 -08002821 /* Simple inefficient version of erase. Just writing
2822 0 in first several blocks */
2823 if (mmc_write(ptn , size, (unsigned int *)out)) {
2824 fastboot_fail("failed to erase partition");
2825 return;
2826 }
Vijay Kumar Pendoti0b21f462016-05-02 17:09:18 +05302827 /*Erase FDE metadata at the userdata footer*/
2828 if(!(strncmp(arg, "userdata", 8)))
2829 {
2830 footer = memalign(CACHE_LINE, FOOTER_SIZE);
2831 memset((void *)footer, 0, FOOTER_SIZE);
2832
2833 size = partition_get_size(index);
2834
2835 if (mmc_write((ptn + size) - FOOTER_SIZE , FOOTER_SIZE, (unsigned int *)footer)) {
2836 fastboot_fail("failed to erase userdata footer");
2837 free(footer);
2838 return;
2839 }
2840 free(footer);
2841 }
Bikas Gurungd48bd242010-09-04 19:54:32 -07002842 }
Sridhar Parasuram32b30662015-07-10 13:33:22 -07002843#if VERIFIED_BOOT
Mayank Grover912eaa62017-10-26 12:08:53 +05302844 if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05302845 !(strncmp(arg, "userdata", 8)) &&
2846 send_delete_keys_to_tz())
Sridhar Parasuram32b30662015-07-10 13:33:22 -07002847 ASSERT(0);
2848#endif
Bikas Gurungd48bd242010-09-04 19:54:32 -07002849 fastboot_okay("");
2850}
2851
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002852void cmd_erase(const char *arg, void *data, unsigned sz)
2853{
Channagoud Kadabice3cf422015-04-17 16:02:30 -07002854#if VERIFIED_BOOT
Channagoud Kadabi35297672015-06-13 11:09:49 -07002855 if (target_build_variant_user())
Channagoud Kadabice3cf422015-04-17 16:02:30 -07002856 {
Sridhar Parasuramc32d07d2015-07-12 10:57:48 -07002857 if(!device.is_unlocked)
Channagoud Kadabice3cf422015-04-17 16:02:30 -07002858 {
Channagoud Kadabi35297672015-06-13 11:09:49 -07002859 fastboot_fail("device is locked. Cannot erase");
Channagoud Kadabice3cf422015-04-17 16:02:30 -07002860 return;
2861 }
2862 }
2863#endif
2864
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002865 if(target_is_emmc_boot())
2866 cmd_erase_mmc(arg, data, sz);
2867 else
2868 cmd_erase_nand(arg, data, sz);
2869}
Bikas Gurungd48bd242010-09-04 19:54:32 -07002870
Mayank Grover11ff9692018-01-11 11:54:49 +05302871/* Get the size from partiton name */
2872static void get_partition_size(const char *arg, char *response)
2873{
2874 uint64_t ptn = 0;
2875 uint64_t size;
2876 int index = INVALID_PTN;
2877
2878 index = partition_get_index(arg);
2879
2880 if (index == INVALID_PTN)
2881 {
2882 dprintf(CRITICAL, "Invalid partition index\n");
2883 return;
2884 }
2885
2886 ptn = partition_get_offset(index);
2887
2888 if(!ptn)
2889 {
2890 dprintf(CRITICAL, "Invalid partition name %s\n", arg);
2891 return;
2892 }
2893
2894 size = partition_get_size(index);
2895
2896 snprintf(response, MAX_RSP_SIZE, "\t 0x%llx", size);
2897 return;
2898}
2899
Mayank Grover52cd10a2018-03-15 12:57:54 +05302900/* Function to check partition type of a partition*/
2901static fs_signature_type
2902check_partition_fs_signature(const char *arg)
2903{
2904 fs_signature_type ret = NO_FS;
2905 int index;
2906 unsigned long long ptn;
2907 char *sb_buffer = malloc(mmc_blocksize);
2908 if (!sb_buffer)
2909 {
2910 dprintf(CRITICAL, "ERROR: Failed to allocate buffer for superblock\n");
2911 goto out;
2912 }
2913
2914 /* Read super block */
2915 if ((index = partition_get_index(arg)) < 0)
2916 {
2917 dprintf(CRITICAL, "ERROR: %s() doesn't exsit\n", arg);
2918 goto out;
2919 }
2920 ptn = partition_get_offset(index);
2921 mmc_set_lun(partition_get_lun(index));
2922 if(mmc_read(ptn + FS_SUPERBLOCK_OFFSET,
2923 (void *)sb_buffer, mmc_blocksize))
2924 {
2925 dprintf(CRITICAL, "ERROR: Failed to read Superblock\n");
2926 goto out;
2927 }
2928
2929 if (*((uint16 *)(&sb_buffer[EXT_MAGIC_OFFSET_SB]))
2930 == (uint16)EXT_MAGIC)
2931 {
2932 dprintf(SPEW, "%s() Found EXT FS\n", arg);
2933 ret = EXT_FS_SIGNATURE;
2934 }
2935 else if (*((uint32 *)(&sb_buffer[F2FS_MAGIC_OFFSET_SB]))
2936 == F2FS_MAGIC)
2937 {
2938 dprintf(SPEW, "%s() Found F2FS FS\n", arg);
2939 ret = EXT_F2FS_SIGNATURE;
2940 }
2941 else
2942 {
2943 dprintf(SPEW, "%s() Reverting to default 0x%x\n",
2944 arg, *((uint16 *)(&sb_buffer[EXT_MAGIC_OFFSET_SB])));
2945 ret = NO_FS;
2946 }
2947
2948out:
2949 if(sb_buffer)
2950 free(sb_buffer);
2951 return ret;
2952}
2953
Mayank Groverd38fe012018-03-13 15:33:16 +05302954/* Function to get partition type */
2955static void get_partition_type(const char *arg, char *response)
2956{
2957 uint n = 0;
Mayank Grover52cd10a2018-03-15 12:57:54 +05302958 fs_signature_type fs_signature;
Mayank Groverd38fe012018-03-13 15:33:16 +05302959
2960 if (arg == NULL ||
2961 response == NULL)
2962 {
2963 dprintf(CRITICAL, "Invalid input parameter\n");
2964 return;
2965 }
2966
2967 /* By default copy raw to response */
2968 strncpy(response, RAW_STR, strlen(RAW_STR));
2969
2970 /* Mark partiton type for known paritions only */
2971 for (n=0; n < ARRAY_SIZE(part_type_known); n++)
2972 {
2973 if (!strncmp(part_type_known[n].part_name, arg, strlen(arg)))
2974 {
Mayank Grover52cd10a2018-03-15 12:57:54 +05302975 /* Check partition for FS signature */
2976 fs_signature = check_partition_fs_signature(arg);
2977 switch (fs_signature)
2978 {
2979 case EXT_FS_SIGNATURE:
2980 strncpy(response, EXT_STR, strlen(EXT_STR));
2981 break;
2982 case EXT_F2FS_SIGNATURE:
2983 strncpy(response, F2FS_STR, strlen(F2FS_STR));
2984 break;
2985 case NO_FS:
2986 strncpy(response, part_type_known[n].type_response, MAX_RSP_SIZE);
2987 }
Mayank Groverd38fe012018-03-13 15:33:16 +05302988 }
2989 }
2990 return;
2991}
2992
Mayank Grover11ff9692018-01-11 11:54:49 +05302993/*
2994 * Publish the partition type & size info
2995 * fastboot getvar will publish the required information.
2996 * fastboot getvar partition_size:<partition_name>: partition size in hex
2997 * fastboot getvar partition_type:<partition_name>: partition type (ext/fat)
2998 */
2999static void publish_getvar_partition_info(struct getvar_partition_info *info, uint8_t num_parts)
3000{
Mayank Groverd38fe012018-03-13 15:33:16 +05303001 uint8_t i;
Mayank Grover11ff9692018-01-11 11:54:49 +05303002 static bool published = false;
3003 struct partition_entry *ptn_entry =
3004 partition_get_partition_entries();
3005 memset(info, 0, sizeof(struct getvar_partition_info)* num_parts);
3006
3007 for (i = 0; i < num_parts; i++) {
3008 strlcat(info[i].part_name, (const char *)ptn_entry[i].name, MAX_RSP_SIZE);
3009 strlcat(info[i].getvar_size, "partition-size:", MAX_GET_VAR_NAME_SIZE);
3010 strlcat(info[i].getvar_type, "partition-type:", MAX_GET_VAR_NAME_SIZE);
3011
Mayank Groverd38fe012018-03-13 15:33:16 +05303012 get_partition_type(info[i].part_name, info[i].type_response);
Mayank Grover11ff9692018-01-11 11:54:49 +05303013 get_partition_size(info[i].part_name, info[i].size_response);
Mayank Grover11ff9692018-01-11 11:54:49 +05303014 if (strlcat(info[i].getvar_size, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
3015 {
3016 dprintf(CRITICAL, "partition size name truncated\n");
3017 return;
3018 }
3019 if (strlcat(info[i].getvar_type, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
3020 {
3021 dprintf(CRITICAL, "partition type name truncated\n");
3022 return;
3023 }
3024
3025 if (!published)
3026 {
3027 /* publish partition size & type info */
3028 fastboot_publish((const char *) info[i].getvar_size, (const char *) info[i].size_response);
3029 fastboot_publish((const char *) info[i].getvar_type, (const char *) info[i].type_response);
3030 }
3031 }
3032 if (!published)
3033 published = true;
3034}
3035
3036
Ajay Dudani5c761132011-04-07 20:19:04 -07003037void cmd_flash_mmc_img(const char *arg, void *data, unsigned sz)
Shashank Mittal23b8f422010-04-16 19:27:21 -07003038{
3039 unsigned long long ptn = 0;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07003040 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07003041 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003042 char *token = NULL;
3043 char *pname = NULL;
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07003044 char *sp;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003045 uint8_t lun = 0;
3046 bool lun_set = false;
Mayank Grover351a75e2017-05-30 20:06:08 +05303047 int current_active_slot = INVALID;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07003048
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07003049 token = strtok_r((char *)arg, ":", &sp);
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003050 pname = token;
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07003051 token = strtok_r(NULL, ":", &sp);
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003052 if(token)
3053 {
3054 lun = atoi(token);
3055 mmc_set_lun(lun);
3056 lun_set = true;
3057 }
3058
Mao Jinlong226f33a2014-07-04 17:24:10 +08003059 if (pname)
Greg Grisco6e754772011-06-23 12:19:39 -07003060 {
Channagoud Kadabiad259832015-05-29 11:14:17 -07003061 if (!strncmp(pname, "frp-unlock", strlen("frp-unlock")))
3062 {
3063 if (!aboot_frp_unlock(pname, data, sz))
3064 {
3065 fastboot_info("FRP unlock successful");
3066 fastboot_okay("");
3067 }
3068 else
3069 fastboot_fail("Secret key is invalid, please update the bootloader with secret key");
3070
3071 return;
3072 }
3073
Mao Jinlong226f33a2014-07-04 17:24:10 +08003074 if (!strcmp(pname, "partition"))
3075 {
3076 dprintf(INFO, "Attempt to write partition image.\n");
3077 if (write_partition(sz, (unsigned char *) data)) {
3078 fastboot_fail("failed to write partition");
Greg Grisco6e754772011-06-23 12:19:39 -07003079 return;
3080 }
Mayank Grover11ff9692018-01-11 11:54:49 +05303081 /* Re-publish partition table */
3082 publish_getvar_partition_info(part_info, partition_get_partition_count());
Mayank Grover351a75e2017-05-30 20:06:08 +05303083
3084 /* Rescan partition table to ensure we have multislot support*/
3085 if (partition_scan_for_multislot())
3086 {
3087 current_active_slot = partition_find_active_slot();
3088 dprintf(INFO, "Multislot supported: Slot %s active",
3089 (SUFFIX_SLOT(current_active_slot)));
3090 }
3091 partition_mark_active_slot(current_active_slot);
Greg Grisco6e754772011-06-23 12:19:39 -07003092 }
Mao Jinlong226f33a2014-07-04 17:24:10 +08003093 else
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003094 {
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003095#if VERIFIED_BOOT
3096 if(!strcmp(pname, KEYSTORE_PTN_NAME))
3097 {
3098 if(!device.is_unlocked)
3099 {
3100 fastboot_fail("unlock device to flash keystore");
3101 return;
3102 }
Gaurav Nebhwanic7313cc2015-12-15 22:25:04 +05303103 if(!boot_verify_validate_keystore((unsigned char *)data,sz))
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003104 {
3105 fastboot_fail("image is not a keystore file");
3106 return;
3107 }
3108 }
3109#endif
Mao Jinlong226f33a2014-07-04 17:24:10 +08003110 index = partition_get_index(pname);
3111 ptn = partition_get_offset(index);
3112 if(ptn == 0) {
3113 fastboot_fail("partition table doesn't exist");
3114 return;
3115 }
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003116
Mayank Grover351a75e2017-05-30 20:06:08 +05303117 if (!strncmp(pname, "boot", strlen("boot"))
3118 || !strcmp(pname, "recovery"))
3119 {
Mao Jinlong226f33a2014-07-04 17:24:10 +08003120 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
3121 fastboot_fail("image is not a boot image");
3122 return;
3123 }
Mayank Grover351a75e2017-05-30 20:06:08 +05303124
3125 /* Reset multislot_partition attributes in case of flashing boot */
3126 if (partition_multislot_is_supported())
3127 {
3128 partition_reset_attributes(index);
3129 }
Mao Jinlong226f33a2014-07-04 17:24:10 +08003130 }
3131
3132 if(!lun_set)
3133 {
3134 lun = partition_get_lun(index);
3135 mmc_set_lun(lun);
3136 }
3137
3138 size = partition_get_size(index);
Gaurav Nebhwani4d2389c2016-03-17 21:10:05 +05303139 if (ROUND_TO_PAGE(sz, mmc_blocksize_mask) > size) {
Mao Jinlong226f33a2014-07-04 17:24:10 +08003140 fastboot_fail("size too large");
3141 return;
3142 }
3143 else if (mmc_write(ptn , sz, (unsigned int *)data)) {
3144 fastboot_fail("flash write failure");
3145 return;
3146 }
Greg Grisco6e754772011-06-23 12:19:39 -07003147 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07003148 }
3149 fastboot_okay("");
3150 return;
3151}
3152
Ajay Dudanide984792015-03-02 09:57:41 -08003153void cmd_flash_meta_img(const char *arg, void *data, unsigned sz)
3154{
3155 int i, images;
3156 meta_header_t *meta_header;
3157 img_header_entry_t *img_header_entry;
Parth Dixit3e2d3032016-03-04 17:11:52 +05303158 /*End of the image address*/
3159 uintptr_t data_end;
3160
3161 if( (UINT_MAX - sz) > (uintptr_t)data )
3162 data_end = (uintptr_t)data + sz;
3163 else
3164 {
3165 fastboot_fail("Cannot flash: image header corrupt");
3166 return;
3167 }
3168
3169 if( data_end < ((uintptr_t)data + sizeof(meta_header_t)))
3170 {
3171 fastboot_fail("Cannot flash: image header corrupt");
3172 return;
3173 }
Ajay Dudanide984792015-03-02 09:57:41 -08003174
lijuang8ee21882016-04-19 16:57:11 +08003175 /* If device is locked:
3176 * Forbid to flash image to avoid the device to bypass the image
3177 * which with "any" name other than bootloader. Because it maybe
3178 * a meta package of all partitions.
3179 */
3180#if VERIFIED_BOOT
3181 if (target_build_variant_user()) {
3182 if (!device.is_unlocked) {
3183 fastboot_fail("Device is locked, meta image flashing is not allowed");
3184 return;
3185 }
Mayank Grover889be1b2017-09-12 20:12:23 +05303186
Mayank Grover912eaa62017-10-26 12:08:53 +05303187 if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05303188 !device.is_unlock_critical)
3189 {
lijuang8ee21882016-04-19 16:57:11 +08003190 fastboot_fail("Device is critical locked, Meta image flashing is not allowed");
3191 return;
3192 }
lijuang8ee21882016-04-19 16:57:11 +08003193 }
3194#endif
3195
Ajay Dudanide984792015-03-02 09:57:41 -08003196 meta_header = (meta_header_t*) data;
Parth Dixit3e2d3032016-03-04 17:11:52 +05303197 if( data_end < ((uintptr_t)data + meta_header->img_hdr_sz))
3198 {
3199 fastboot_fail("Cannot flash: image header corrupt");
3200 return;
3201 }
Ajay Dudanide984792015-03-02 09:57:41 -08003202 img_header_entry = (img_header_entry_t*) (data+sizeof(meta_header_t));
3203
3204 images = meta_header->img_hdr_sz / sizeof(img_header_entry_t);
3205
3206 for (i=0; i<images; i++) {
3207
3208 if((img_header_entry[i].ptn_name == NULL) ||
3209 (img_header_entry[i].start_offset == 0) ||
3210 (img_header_entry[i].size == 0))
3211 break;
Kishor PK49831502017-04-21 17:55:43 +05303212 if ((UINT_MAX - img_header_entry[i].start_offset) < (uintptr_t)data) {
3213 fastboot_fail("Integer overflow detected in start_offset of img");
3214 break;
3215 }
3216 else if ((UINT_MAX - (img_header_entry[i].start_offset + (uintptr_t)data)) < img_header_entry[i].size) {
3217 fastboot_fail("Integer overflow detected in size of img");
3218 break;
3219 }
Parth Dixit3e2d3032016-03-04 17:11:52 +05303220 if( data_end < ((uintptr_t)data + img_header_entry[i].start_offset
3221 + img_header_entry[i].size) )
3222 {
3223 fastboot_fail("Cannot flash: image size mismatch");
3224 break;
3225 }
3226
Ajay Dudanide984792015-03-02 09:57:41 -08003227 cmd_flash_mmc_img(img_header_entry[i].ptn_name,
3228 (void *) data + img_header_entry[i].start_offset,
3229 img_header_entry[i].size);
3230 }
3231
Channagoud Kadabiaafbbfc2015-03-04 17:52:56 -08003232 if (!strncmp(arg, "bootloader", strlen("bootloader")))
3233 {
3234 strlcpy(device.bootloader_version, TARGET(BOARD), MAX_VERSION_LEN);
3235 strlcat(device.bootloader_version, "-", MAX_VERSION_LEN);
3236 strlcat(device.bootloader_version, meta_header->img_version, MAX_VERSION_LEN);
3237 }
3238 else
3239 {
3240 strlcpy(device.radio_version, TARGET(BOARD), MAX_VERSION_LEN);
3241 strlcat(device.radio_version, "-", MAX_VERSION_LEN);
3242 strlcat(device.radio_version, meta_header->img_version, MAX_VERSION_LEN);
3243 }
3244
3245 write_device_info(&device);
Ajay Dudanide984792015-03-02 09:57:41 -08003246 fastboot_okay("");
3247 return;
3248}
3249
Ajay Dudani5c761132011-04-07 20:19:04 -07003250void cmd_flash_mmc_sparse_img(const char *arg, void *data, unsigned sz)
3251{
3252 unsigned int chunk;
wufeng.jiang813dc352015-06-02 23:02:46 -04003253 uint64_t chunk_data_sz;
Channagoud Kadabif627cf82013-09-09 14:08:20 -07003254 uint32_t *fill_buf = NULL;
3255 uint32_t fill_val;
Mayank Grover4f50bba2017-07-19 18:17:08 +05303256 uint32_t blk_sz_actual = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07003257 sparse_header_t *sparse_header;
3258 chunk_header_t *chunk_header;
Ajay Dudaniab18f022011-05-12 14:39:22 -07003259 uint32_t total_blocks = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07003260 unsigned long long ptn = 0;
Channagoud Kadabi65b91002011-10-11 17:34:33 +05303261 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07003262 int index = INVALID_PTN;
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05303263 uint32_t i;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003264 uint8_t lun = 0;
vijay kumar800255e2015-04-24 20:26:19 +05303265 /*End of the sparse image address*/
Parth Dixit64b1a482016-03-07 16:31:26 +05303266 uintptr_t data_end = (uintptr_t)data + sz;
Ajay Dudani5c761132011-04-07 20:19:04 -07003267
Kinson Chikf1a43512011-07-14 11:28:39 -07003268 index = partition_get_index(arg);
3269 ptn = partition_get_offset(index);
3270 if(ptn == 0) {
Ajay Dudani5c761132011-04-07 20:19:04 -07003271 fastboot_fail("partition table doesn't exist");
3272 return;
3273 }
3274
Channagoud Kadabi65b91002011-10-11 17:34:33 +05303275 size = partition_get_size(index);
Channagoud Kadabi65b91002011-10-11 17:34:33 +05303276
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003277 lun = partition_get_lun(index);
3278 mmc_set_lun(lun);
3279
vijay kumar800255e2015-04-24 20:26:19 +05303280 if (sz < sizeof(sparse_header_t)) {
3281 fastboot_fail("size too low");
3282 return;
3283 }
3284
Ajay Dudani5c761132011-04-07 20:19:04 -07003285 /* Read and skip over sparse image header */
3286 sparse_header = (sparse_header_t *) data;
vijay kumar800255e2015-04-24 20:26:19 +05303287
Mayank Grover48bd9bb2017-07-19 12:04:16 +05303288 if (!sparse_header->blk_sz || (sparse_header->blk_sz % 4)){
3289 fastboot_fail("Invalid block size\n");
3290 return;
3291 }
3292
vijay kumar1321f342015-03-27 12:13:42 +05303293 if (((uint64_t)sparse_header->total_blks * (uint64_t)sparse_header->blk_sz) > size) {
Ajay Dudani876b3282012-12-21 14:12:17 -08003294 fastboot_fail("size too large");
3295 return;
3296 }
3297
vijay kumarde4fcf62015-04-23 13:05:49 +05303298 data += sizeof(sparse_header_t);
vijay kumar800255e2015-04-24 20:26:19 +05303299
Parth Dixit64b1a482016-03-07 16:31:26 +05303300 if (data_end < (uintptr_t)data) {
vijay kumar800255e2015-04-24 20:26:19 +05303301 fastboot_fail("buffer overreads occured due to invalid sparse header");
3302 return;
3303 }
3304
vijay kumarde4fcf62015-04-23 13:05:49 +05303305 if(sparse_header->file_hdr_sz != sizeof(sparse_header_t))
Ajay Dudani5c761132011-04-07 20:19:04 -07003306 {
vijay kumarde4fcf62015-04-23 13:05:49 +05303307 fastboot_fail("sparse header size mismatch");
3308 return;
Ajay Dudani5c761132011-04-07 20:19:04 -07003309 }
3310
Ajay Dudanib06c05f2011-05-12 14:46:10 -07003311 dprintf (SPEW, "=== Sparse Image Header ===\n");
3312 dprintf (SPEW, "magic: 0x%x\n", sparse_header->magic);
3313 dprintf (SPEW, "major_version: 0x%x\n", sparse_header->major_version);
3314 dprintf (SPEW, "minor_version: 0x%x\n", sparse_header->minor_version);
3315 dprintf (SPEW, "file_hdr_sz: %d\n", sparse_header->file_hdr_sz);
3316 dprintf (SPEW, "chunk_hdr_sz: %d\n", sparse_header->chunk_hdr_sz);
3317 dprintf (SPEW, "blk_sz: %d\n", sparse_header->blk_sz);
3318 dprintf (SPEW, "total_blks: %d\n", sparse_header->total_blks);
3319 dprintf (SPEW, "total_chunks: %d\n", sparse_header->total_chunks);
Ajay Dudani5c761132011-04-07 20:19:04 -07003320
3321 /* Start processing chunks */
3322 for (chunk=0; chunk<sparse_header->total_chunks; chunk++)
3323 {
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05303324 /* Make sure the total image size does not exceed the partition size */
3325 if(((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz) >= size) {
3326 fastboot_fail("size too large");
3327 return;
3328 }
Ajay Dudani5c761132011-04-07 20:19:04 -07003329 /* Read and skip over chunk header */
3330 chunk_header = (chunk_header_t *) data;
3331 data += sizeof(chunk_header_t);
3332
Parth Dixit64b1a482016-03-07 16:31:26 +05303333 if (data_end < (uintptr_t)data) {
vijay kumar800255e2015-04-24 20:26:19 +05303334 fastboot_fail("buffer overreads occured due to invalid sparse header");
3335 return;
3336 }
3337
Ajay Dudani5c761132011-04-07 20:19:04 -07003338 dprintf (SPEW, "=== Chunk Header ===\n");
3339 dprintf (SPEW, "chunk_type: 0x%x\n", chunk_header->chunk_type);
3340 dprintf (SPEW, "chunk_data_sz: 0x%x\n", chunk_header->chunk_sz);
3341 dprintf (SPEW, "total_size: 0x%x\n", chunk_header->total_sz);
3342
vijay kumar800255e2015-04-24 20:26:19 +05303343 if(sparse_header->chunk_hdr_sz != sizeof(chunk_header_t))
Ajay Dudani5c761132011-04-07 20:19:04 -07003344 {
vijay kumar800255e2015-04-24 20:26:19 +05303345 fastboot_fail("chunk header size mismatch");
3346 return;
Ajay Dudani5c761132011-04-07 20:19:04 -07003347 }
3348
wufeng.jiang813dc352015-06-02 23:02:46 -04003349 chunk_data_sz = (uint64_t)sparse_header->blk_sz * chunk_header->chunk_sz;
3350
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05303351 /* Make sure that the chunk size calculated from sparse image does not
3352 * exceed partition size
3353 */
3354 if ((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz + chunk_data_sz > size)
3355 {
3356 fastboot_fail("Chunk data size exceeds partition size");
3357 return;
3358 }
3359
Ajay Dudani5c761132011-04-07 20:19:04 -07003360 switch (chunk_header->chunk_type)
3361 {
3362 case CHUNK_TYPE_RAW:
wufeng.jiang813dc352015-06-02 23:02:46 -04003363 if((uint64_t)chunk_header->total_sz != ((uint64_t)sparse_header->chunk_hdr_sz +
Ajay Dudani5c761132011-04-07 20:19:04 -07003364 chunk_data_sz))
3365 {
3366 fastboot_fail("Bogus chunk size for chunk type Raw");
3367 return;
3368 }
3369
Parth Dixit64b1a482016-03-07 16:31:26 +05303370 if (data_end < (uintptr_t)data + chunk_data_sz) {
vijay kumar800255e2015-04-24 20:26:19 +05303371 fastboot_fail("buffer overreads occured due to invalid sparse header");
3372 return;
3373 }
3374
wufeng.jiang813dc352015-06-02 23:02:46 -04003375 /* chunk_header->total_sz is uint32,So chunk_data_sz is now less than 2^32
3376 otherwise it will return in the line above
3377 */
Ajay Dudaniab18f022011-05-12 14:39:22 -07003378 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
wufeng.jiang813dc352015-06-02 23:02:46 -04003379 (uint32_t)chunk_data_sz,
Ajay Dudaniab18f022011-05-12 14:39:22 -07003380 (unsigned int*)data))
Ajay Dudani5c761132011-04-07 20:19:04 -07003381 {
3382 fastboot_fail("flash write failure");
3383 return;
3384 }
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05303385 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
3386 fastboot_fail("Bogus size for RAW chunk type");
3387 return;
3388 }
Ajay Dudani5c761132011-04-07 20:19:04 -07003389 total_blocks += chunk_header->chunk_sz;
wufeng.jiang813dc352015-06-02 23:02:46 -04003390 data += (uint32_t)chunk_data_sz;
Ajay Dudani5c761132011-04-07 20:19:04 -07003391 break;
3392
Channagoud Kadabif627cf82013-09-09 14:08:20 -07003393 case CHUNK_TYPE_FILL:
3394 if(chunk_header->total_sz != (sparse_header->chunk_hdr_sz +
3395 sizeof(uint32_t)))
3396 {
3397 fastboot_fail("Bogus chunk size for chunk type FILL");
3398 return;
3399 }
3400
Mayank Grover4f50bba2017-07-19 18:17:08 +05303401 blk_sz_actual = ROUNDUP(sparse_header->blk_sz, CACHE_LINE);
3402 /* Integer overflow detected */
3403 if (blk_sz_actual < sparse_header->blk_sz)
3404 {
3405 fastboot_fail("Invalid block size");
3406 return;
3407 }
3408
3409 fill_buf = (uint32_t *)memalign(CACHE_LINE, blk_sz_actual);
Channagoud Kadabif627cf82013-09-09 14:08:20 -07003410 if (!fill_buf)
3411 {
3412 fastboot_fail("Malloc failed for: CHUNK_TYPE_FILL");
3413 return;
3414 }
3415
Parth Dixit64b1a482016-03-07 16:31:26 +05303416 if (data_end < (uintptr_t)data + sizeof(uint32_t)) {
vijay kumar800255e2015-04-24 20:26:19 +05303417 fastboot_fail("buffer overreads occured due to invalid sparse header");
Gaurav Nebhwani163cbf82016-03-17 21:21:27 +05303418 free(fill_buf);
vijay kumar800255e2015-04-24 20:26:19 +05303419 return;
3420 }
Channagoud Kadabif627cf82013-09-09 14:08:20 -07003421 fill_val = *(uint32_t *)data;
3422 data = (char *) data + sizeof(uint32_t);
Channagoud Kadabif627cf82013-09-09 14:08:20 -07003423
3424 for (i = 0; i < (sparse_header->blk_sz / sizeof(fill_val)); i++)
3425 {
3426 fill_buf[i] = fill_val;
3427 }
3428
Gaurav Nebhwanie94cbe12016-03-17 21:16:34 +05303429 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz))
3430 {
3431 fastboot_fail("bogus size for chunk FILL type");
3432 free(fill_buf);
3433 return;
3434 }
3435
wufeng.jiang813dc352015-06-02 23:02:46 -04003436 for (i = 0; i < chunk_header->chunk_sz; i++)
Channagoud Kadabif627cf82013-09-09 14:08:20 -07003437 {
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05303438 /* Make sure that the data written to partition does not exceed partition size */
3439 if ((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz + sparse_header->blk_sz > size)
3440 {
3441 fastboot_fail("Chunk data size for fill type exceeds partition size");
Gaurav Nebhwani163cbf82016-03-17 21:21:27 +05303442 free(fill_buf);
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05303443 return;
3444 }
3445
Channagoud Kadabif627cf82013-09-09 14:08:20 -07003446 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
3447 sparse_header->blk_sz,
3448 fill_buf))
3449 {
3450 fastboot_fail("flash write failure");
3451 free(fill_buf);
3452 return;
3453 }
3454
3455 total_blocks++;
3456 }
3457
3458 free(fill_buf);
3459 break;
3460
Ajay Dudani5c761132011-04-07 20:19:04 -07003461 case CHUNK_TYPE_DONT_CARE:
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05303462 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
3463 fastboot_fail("bogus size for chunk DONT CARE type");
3464 return;
3465 }
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07003466 total_blocks += chunk_header->chunk_sz;
3467 break;
3468
Ajay Dudani5c761132011-04-07 20:19:04 -07003469 case CHUNK_TYPE_CRC:
3470 if(chunk_header->total_sz != sparse_header->chunk_hdr_sz)
3471 {
wufeng.jiang813dc352015-06-02 23:02:46 -04003472 fastboot_fail("Bogus chunk size for chunk type CRC");
Ajay Dudani5c761132011-04-07 20:19:04 -07003473 return;
3474 }
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05303475 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
3476 fastboot_fail("bogus size for chunk CRC type");
3477 return;
3478 }
Ajay Dudani5c761132011-04-07 20:19:04 -07003479 total_blocks += chunk_header->chunk_sz;
Parth Dixit64b1a482016-03-07 16:31:26 +05303480 if ((uintptr_t)data > UINT_MAX - chunk_data_sz) {
vijay kumar800255e2015-04-24 20:26:19 +05303481 fastboot_fail("integer overflow occured");
3482 return;
3483 }
wufeng.jiang813dc352015-06-02 23:02:46 -04003484 data += (uint32_t)chunk_data_sz;
Parth Dixit64b1a482016-03-07 16:31:26 +05303485 if (data_end < (uintptr_t)data) {
vijay kumar800255e2015-04-24 20:26:19 +05303486 fastboot_fail("buffer overreads occured due to invalid sparse header");
3487 return;
3488 }
Ajay Dudani5c761132011-04-07 20:19:04 -07003489 break;
3490
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07003491 default:
Channagoud Kadabif627cf82013-09-09 14:08:20 -07003492 dprintf(CRITICAL, "Unkown chunk type: %x\n",chunk_header->chunk_type);
Ajay Dudani5c761132011-04-07 20:19:04 -07003493 fastboot_fail("Unknown chunk type");
3494 return;
3495 }
3496 }
3497
Ajay Dudani0c6927b2011-05-18 11:12:16 -07003498 dprintf(INFO, "Wrote %d blocks, expected to write %d blocks\n",
3499 total_blocks, sparse_header->total_blks);
3500
3501 if(total_blocks != sparse_header->total_blks)
3502 {
3503 fastboot_fail("sparse image write failure");
3504 }
Ajay Dudani5c761132011-04-07 20:19:04 -07003505
3506 fastboot_okay("");
3507 return;
3508}
3509
3510void cmd_flash_mmc(const char *arg, void *data, unsigned sz)
3511{
3512 sparse_header_t *sparse_header;
Ajay Dudanide984792015-03-02 09:57:41 -08003513 meta_header_t *meta_header;
Ajay Dudani5c761132011-04-07 20:19:04 -07003514
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07003515#ifdef SSD_ENABLE
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -08003516 /* 8 Byte Magic + 2048 Byte xml + Encrypted Data */
3517 unsigned int *magic_number = (unsigned int *) data;
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08003518 int ret=0;
3519 uint32 major_version=0;
3520 uint32 minor_version=0;
3521
3522 ret = scm_svc_version(&major_version,&minor_version);
3523 if(!ret)
3524 {
3525 if(major_version >= 2)
3526 {
Amir Samuelovbb65ce02013-05-05 12:20:18 +03003527 if( !strcmp(arg, "ssd") || !strcmp(arg, "tqs") )
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08003528 {
3529 ret = encrypt_scm((uint32 **) &data, &sz);
3530 if (ret != 0) {
3531 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
3532 return;
3533 }
3534
Amir Samuelovbb65ce02013-05-05 12:20:18 +03003535 /* Protect only for SSD */
3536 if (!strcmp(arg, "ssd")) {
3537 ret = scm_protect_keystore((uint32 *) data, sz);
3538 if (ret != 0) {
3539 dprintf(CRITICAL, "ERROR: scm_protect_keystore Failed\n");
3540 return;
3541 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08003542 }
3543 }
3544 else
3545 {
3546 ret = decrypt_scm_v2((uint32 **) &data, &sz);
3547 if(ret != 0)
3548 {
3549 dprintf(CRITICAL,"ERROR: Decryption Failure\n");
3550 return;
3551 }
3552 }
3553 }
3554 else
3555 {
3556 if (magic_number[0] == DECRYPT_MAGIC_0 &&
3557 magic_number[1] == DECRYPT_MAGIC_1)
3558 {
3559 ret = decrypt_scm((uint32 **) &data, &sz);
3560 if (ret != 0) {
3561 dprintf(CRITICAL, "ERROR: Invalid secure image\n");
3562 return;
3563 }
3564 }
3565 else if (magic_number[0] == ENCRYPT_MAGIC_0 &&
3566 magic_number[1] == ENCRYPT_MAGIC_1)
3567 {
3568 ret = encrypt_scm((uint32 **) &data, &sz);
3569 if (ret != 0) {
3570 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
3571 return;
3572 }
3573 }
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07003574 }
3575 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08003576 else
Neeti Desai127b9e02012-03-20 16:11:23 -07003577 {
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08003578 dprintf(CRITICAL,"INVALID SVC Version\n");
3579 return;
Neeti Desai127b9e02012-03-20 16:11:23 -07003580 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08003581#endif /* SSD_ENABLE */
Neeti Desai127b9e02012-03-20 16:11:23 -07003582
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003583#if VERIFIED_BOOT
Channagoud Kadabi35297672015-06-13 11:09:49 -07003584 if (target_build_variant_user())
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003585 {
lijuang511a2b52015-08-14 20:50:51 +08003586 /* if device is locked:
3587 * common partition will not allow to be flashed
3588 * critical partition will allow to flash image.
3589 */
3590 if(!device.is_unlocked && !critical_flash_allowed(arg)) {
3591 fastboot_fail("Partition flashing is not allowed");
3592 return;
3593 }
Mayank Grover889be1b2017-09-12 20:12:23 +05303594
lijuang511a2b52015-08-14 20:50:51 +08003595 /* if device critical is locked:
3596 * common partition will allow to be flashed
3597 * critical partition will not allow to flash image.
3598 */
Mayank Grover912eaa62017-10-26 12:08:53 +05303599 if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05303600 !device.is_unlock_critical &&
3601 critical_flash_allowed(arg)) {
3602 fastboot_fail("Critical partition flashing is not allowed");
3603 return;
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003604 }
3605 }
3606#endif
3607
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07003608 sparse_header = (sparse_header_t *) data;
Ajay Dudanide984792015-03-02 09:57:41 -08003609 meta_header = (meta_header_t *) data;
3610 if (sparse_header->magic == SPARSE_HEADER_MAGIC)
Ajay Dudani5c761132011-04-07 20:19:04 -07003611 cmd_flash_mmc_sparse_img(arg, data, sz);
Ajay Dudanide984792015-03-02 09:57:41 -08003612 else if (meta_header->magic == META_HEADER_MAGIC)
3613 cmd_flash_meta_img(arg, data, sz);
3614 else
3615 cmd_flash_mmc_img(arg, data, sz);
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07003616
3617#if VERIFIED_BOOT
Mayank Grover912eaa62017-10-26 12:08:53 +05303618 if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05303619 (!strncmp(arg, "system", 6)) &&
3620 !device.verity_mode)
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07003621 // reset dm_verity mode to enforcing
3622 device.verity_mode = 1;
3623 write_device_info(&device);
Parth Dixitddbc7352015-10-18 03:13:31 +05303624#endif
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07003625
Ajay Dudani5c761132011-04-07 20:19:04 -07003626 return;
3627}
3628
Tanya Brokhman84eaaf62015-02-24 21:57:06 +02003629void cmd_updatevol(const char *vol_name, void *data, unsigned sz)
3630{
3631 struct ptentry *sys_ptn;
3632 struct ptable *ptable;
3633
3634 ptable = flash_get_ptable();
3635 if (ptable == NULL) {
3636 fastboot_fail("partition table doesn't exist");
3637 return;
3638 }
3639
3640 sys_ptn = ptable_find(ptable, "system");
3641 if (sys_ptn == NULL) {
3642 fastboot_fail("system partition not found");
3643 return;
3644 }
3645
3646 sz = ROUND_TO_PAGE(sz, page_mask);
3647 if (update_ubi_vol(sys_ptn, vol_name, data, sz))
3648 fastboot_fail("update_ubi_vol failed");
3649 else
3650 fastboot_okay("");
3651}
3652
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003653void cmd_flash_nand(const char *arg, void *data, unsigned sz)
Dima Zavin214cc642009-01-26 11:16:21 -08003654{
3655 struct ptentry *ptn;
3656 struct ptable *ptable;
3657 unsigned extra = 0;
Gaurav Nebhwanid9730712016-05-06 14:28:29 +05303658 uint64_t partition_size = 0;
Vijay Kumar Pendotic6f22a72017-05-19 22:44:34 +05303659 unsigned bytes_to_round_page = 0;
3660 unsigned rounded_size = 0;
Dima Zavin214cc642009-01-26 11:16:21 -08003661
Kishor PK38ed93d2017-04-25 14:19:26 +05303662 if((uintptr_t)data > (UINT_MAX - sz)) {
3663 fastboot_fail("Cannot flash: image header corrupt");
3664 return;
3665 }
3666
Dima Zavin214cc642009-01-26 11:16:21 -08003667 ptable = flash_get_ptable();
3668 if (ptable == NULL) {
3669 fastboot_fail("partition table doesn't exist");
3670 return;
3671 }
3672
3673 ptn = ptable_find(ptable, arg);
3674 if (ptn == NULL) {
Tanya Brokhman84eaaf62015-02-24 21:57:06 +02003675 dprintf(INFO, "unknown partition name (%s). Trying updatevol\n",
3676 arg);
3677 cmd_updatevol(arg, data, sz);
Dima Zavin214cc642009-01-26 11:16:21 -08003678 return;
3679 }
3680
3681 if (!strcmp(ptn->name, "boot") || !strcmp(ptn->name, "recovery")) {
Kishor PK38ed93d2017-04-25 14:19:26 +05303682 if((sz > BOOT_MAGIC_SIZE) && (!memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE))) {
3683 dprintf(INFO, "Verified the BOOT_MAGIC in image header \n");
3684 } else {
3685 fastboot_fail("Image is not a boot image");
Dima Zavin214cc642009-01-26 11:16:21 -08003686 return;
3687 }
3688 }
3689
Amol Jadi5c61a952012-05-04 17:05:35 -07003690 if (!strcmp(ptn->name, "system")
Deepa Dinamani13e32c42012-03-12 14:34:17 -07003691 || !strcmp(ptn->name, "userdata")
3692 || !strcmp(ptn->name, "persist")
Sundarajan Srinivasanb063a852013-11-19 14:02:27 -08003693 || !strcmp(ptn->name, "recoveryfs")
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -08003694 || !strcmp(ptn->name, "modem"))
Tanya Brokhman1c94f1a2015-02-15 09:05:03 +02003695 extra = 1;
Kishor PK38ed93d2017-04-25 14:19:26 +05303696 else {
Vijay Kumar Pendotic6f22a72017-05-19 22:44:34 +05303697 rounded_size = ROUNDUP(sz, page_size);
3698 bytes_to_round_page = rounded_size - sz;
3699 if (bytes_to_round_page) {
3700 if (((uintptr_t)data + sz ) > (UINT_MAX - bytes_to_round_page)) {
3701 fastboot_fail("Integer overflow detected");
3702 return;
3703 }
3704 if (((uintptr_t)data + sz + bytes_to_round_page) >
3705 ((uintptr_t)target_get_scratch_address() + target_get_max_flash_size())) {
3706 fastboot_fail("Buffer size is not aligned to page_size");
3707 return;
3708 }
3709 else {
3710 memset(data + sz, 0, bytes_to_round_page);
3711 sz = rounded_size;
3712 }
Kishor PK38ed93d2017-04-25 14:19:26 +05303713 }
Gaurav Nebhwanid9730712016-05-06 14:28:29 +05303714 }
3715
Kishor PK38ed93d2017-04-25 14:19:26 +05303716 /*Checking partition_size for the possible integer overflow */
3717 partition_size = validate_partition_size(ptn);
3718
Gaurav Nebhwanid9730712016-05-06 14:28:29 +05303719 if (sz > partition_size) {
3720 fastboot_fail("Image size too large");
3721 return;
3722 }
3723
Dima Zavin214cc642009-01-26 11:16:21 -08003724 dprintf(INFO, "writing %d bytes to '%s'\n", sz, ptn->name);
Mayank Grover6cde9352017-06-06 18:45:23 +05303725 if ((sz > UBI_EC_HDR_SIZE) &&
3726 (!memcmp((void *)data, UBI_MAGIC, UBI_MAGIC_SIZE))) {
Tanya Brokhman1c94f1a2015-02-15 09:05:03 +02003727 if (flash_ubi_img(ptn, data, sz)) {
3728 fastboot_fail("flash write failure");
3729 return;
3730 }
3731 } else {
3732 if (flash_write(ptn, extra, data, sz)) {
3733 fastboot_fail("flash write failure");
3734 return;
3735 }
Dima Zavin214cc642009-01-26 11:16:21 -08003736 }
3737 dprintf(INFO, "partition '%s' updated\n", ptn->name);
3738 fastboot_okay("");
3739}
3740
Kishor PK38ed93d2017-04-25 14:19:26 +05303741
3742static inline uint64_t validate_partition_size(struct ptentry *ptn)
3743{
3744 if (ptn->length && flash_num_pages_per_blk() && page_size) {
3745 if ((ptn->length < ( UINT_MAX / flash_num_pages_per_blk())) && ((ptn->length * flash_num_pages_per_blk()) < ( UINT_MAX / page_size))) {
3746 return ptn->length * flash_num_pages_per_blk() * page_size;
3747 }
3748 }
3749 return 0;
3750}
3751
3752
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003753void cmd_flash(const char *arg, void *data, unsigned sz)
3754{
3755 if(target_is_emmc_boot())
3756 cmd_flash_mmc(arg, data, sz);
3757 else
3758 cmd_flash_nand(arg, data, sz);
3759}
3760
Dima Zavin214cc642009-01-26 11:16:21 -08003761void cmd_continue(const char *arg, void *data, unsigned sz)
3762{
3763 fastboot_okay("");
Amol Jadi7c4316c2013-10-07 14:19:26 -07003764 fastboot_stop();
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003765
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003766 if (target_is_emmc_boot())
3767 {
lijuanga40d6302015-07-20 20:10:13 +08003768#if FBCON_DISPLAY_MSG
lijuangde34d502016-02-26 16:04:50 +08003769 /* Exit keys' detection thread firstly */
3770 exit_menu_keys_detection();
lijuanga40d6302015-07-20 20:10:13 +08003771#endif
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003772 boot_linux_from_mmc();
3773 }
3774 else
3775 {
3776 boot_linux_from_flash();
3777 }
Dima Zavin214cc642009-01-26 11:16:21 -08003778}
3779
Chandan Uddaraju94183c02010-01-15 15:13:59 -08003780void cmd_reboot(const char *arg, void *data, unsigned sz)
3781{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003782 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08003783 fastboot_okay("");
3784 reboot_device(0);
3785}
3786
Mayank Grover351a75e2017-05-30 20:06:08 +05303787void cmd_set_active(const char *arg, void *data, unsigned sz)
3788{
Mayank Grover6ccc1c92017-07-04 17:36:46 +05303789 char *p, *sp = NULL;
Mayank Grover351a75e2017-05-30 20:06:08 +05303790 unsigned i,current_active_slot;
3791 const char *current_slot_suffix;
3792
3793 if (!partition_multislot_is_supported())
3794 {
3795 fastboot_fail("Command not supported");
3796 return;
3797 }
3798
3799 if (arg)
3800 {
Mayank Grover6ccc1c92017-07-04 17:36:46 +05303801 p = strtok_r((char *)arg, ":", &sp);
Mayank Grover5eb5f692017-07-19 20:16:04 +05303802 if (p)
Mayank Grover351a75e2017-05-30 20:06:08 +05303803 {
3804 current_active_slot = partition_find_active_slot();
3805
3806 /* Check if trying to make curent slot active */
3807 current_slot_suffix = SUFFIX_SLOT(current_active_slot);
Mayank Grover6ccc1c92017-07-04 17:36:46 +05303808 current_slot_suffix = strtok_r((char *)current_slot_suffix,
3809 (char *)suffix_delimiter, &sp);
3810
Mayank Grover5eb5f692017-07-19 20:16:04 +05303811 if (current_slot_suffix &&
3812 !strncmp(p, current_slot_suffix, strlen(current_slot_suffix)))
Mayank Grover351a75e2017-05-30 20:06:08 +05303813 {
3814 fastboot_okay("Slot already set active");
3815 return;
3816 }
3817 else
3818 {
3819 for (i = 0; i < AB_SUPPORTED_SLOTS; i++)
3820 {
3821 current_slot_suffix = SUFFIX_SLOT(i);
Mayank Grover6ccc1c92017-07-04 17:36:46 +05303822 current_slot_suffix = strtok_r((char *)current_slot_suffix,
3823 (char *)suffix_delimiter, &sp);
Mayank Grover5eb5f692017-07-19 20:16:04 +05303824 if (current_slot_suffix &&
3825 !strncmp(p, current_slot_suffix, strlen(current_slot_suffix)))
Mayank Grover351a75e2017-05-30 20:06:08 +05303826 {
3827 partition_switch_slots(current_active_slot, i);
3828 publish_getvar_multislot_vars();
3829 fastboot_okay("");
3830 return;
3831 }
3832 }
3833 }
3834 }
3835 }
3836 fastboot_fail("Invalid slot suffix.");
3837 return;
3838}
3839
Chandan Uddaraju94183c02010-01-15 15:13:59 -08003840void cmd_reboot_bootloader(const char *arg, void *data, unsigned sz)
3841{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003842 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08003843 fastboot_okay("");
3844 reboot_device(FASTBOOT_MODE);
3845}
3846
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003847void cmd_oem_enable_charger_screen(const char *arg, void *data, unsigned size)
3848{
3849 dprintf(INFO, "Enabling charger screen check\n");
3850 device.charger_screen_enabled = 1;
3851 write_device_info(&device);
3852 fastboot_okay("");
3853}
3854
3855void cmd_oem_disable_charger_screen(const char *arg, void *data, unsigned size)
3856{
3857 dprintf(INFO, "Disabling charger screen check\n");
3858 device.charger_screen_enabled = 0;
3859 write_device_info(&device);
3860 fastboot_okay("");
3861}
3862
lijuanga25d8bb2015-09-16 13:11:52 +08003863void cmd_oem_off_mode_charger(const char *arg, void *data, unsigned size)
3864{
3865 char *p = NULL;
3866 const char *delim = " \t\n\r";
Parth Dixit407f15b2016-01-07 14:47:37 +05303867 char *sp;
lijuanga25d8bb2015-09-16 13:11:52 +08003868
3869 if (arg) {
Parth Dixit407f15b2016-01-07 14:47:37 +05303870 p = strtok_r((char *)arg, delim, &sp);
lijuanga25d8bb2015-09-16 13:11:52 +08003871 if (p) {
3872 if (!strncmp(p, "0", 1)) {
3873 device.charger_screen_enabled = 0;
3874 } else if (!strncmp(p, "1", 1)) {
3875 device.charger_screen_enabled = 1;
3876 }
3877 }
3878 }
3879
3880 /* update charger_screen_enabled value for getvar
3881 * command
3882 */
3883 snprintf(charger_screen_enabled, MAX_RSP_SIZE, "%d",
3884 device.charger_screen_enabled);
3885
3886 write_device_info(&device);
3887 fastboot_okay("");
3888}
3889
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05303890void cmd_oem_select_display_panel(const char *arg, void *data, unsigned size)
3891{
3892 dprintf(INFO, "Selecting display panel %s\n", arg);
3893 if (arg)
3894 strlcpy(device.display_panel, arg,
3895 sizeof(device.display_panel));
3896 write_device_info(&device);
3897 fastboot_okay("");
3898}
3899
Shashank Mittal162244e2011-08-08 19:01:25 -07003900void cmd_oem_unlock(const char *arg, void *data, unsigned sz)
3901{
lijuang4ece1e72015-08-14 21:02:36 +08003902 set_device_unlock(UNLOCK, TRUE);
vijay kumarc65876c2015-04-24 13:29:16 +05303903}
3904
3905void cmd_oem_unlock_go(const char *arg, void *data, unsigned sz)
3906{
lijuang4ece1e72015-08-14 21:02:36 +08003907 if(!device.is_unlocked) {
vijay kumarc65876c2015-04-24 13:29:16 +05303908 if(!is_allow_unlock) {
3909 fastboot_fail("oem unlock is not allowed");
3910 return;
3911 }
3912
lijuang4ece1e72015-08-14 21:02:36 +08003913 set_device_unlock_value(UNLOCK, TRUE);
vijay kumarc65876c2015-04-24 13:29:16 +05303914
lijuang4ece1e72015-08-14 21:02:36 +08003915 /* wipe data */
vijay kumarc65876c2015-04-24 13:29:16 +05303916 struct recovery_message msg;
Kishor PK60a68212017-05-08 16:55:57 +05303917 memset(&msg, 0, sizeof(msg));
vijay kumarc65876c2015-04-24 13:29:16 +05303918 snprintf(msg.recovery, sizeof(msg.recovery), "recovery\n--wipe_data");
3919 write_misc(0, &msg, sizeof(msg));
3920
3921 fastboot_okay("");
3922 reboot_device(RECOVERY_MODE);
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003923 }
3924 fastboot_okay("");
3925}
3926
Channagoud Kadabiad259832015-05-29 11:14:17 -07003927static int aboot_frp_unlock(char *pname, void *data, unsigned sz)
3928{
Mayank Grover70f8b0e2017-05-17 11:49:00 +05303929 int ret=1;
3930 bool authentication_success=false;
Channagoud Kadabiad259832015-05-29 11:14:17 -07003931
Mayank Grover70f8b0e2017-05-17 11:49:00 +05303932 /*
3933 Authentication method not implemented.
3934
3935 OEM to implement, authentication system which on successful validataion,
3936 calls write_allow_oem_unlock() with is_allow_unlock.
3937 */
3938#if 0
3939 authentication_success = oem_specific_auth_mthd();
3940#endif
3941
3942 if (authentication_success)
Channagoud Kadabiad259832015-05-29 11:14:17 -07003943 {
Mayank Grover70f8b0e2017-05-17 11:49:00 +05303944 is_allow_unlock = true;
3945 write_allow_oem_unlock(is_allow_unlock);
3946 ret = 0;
Channagoud Kadabiad259832015-05-29 11:14:17 -07003947 }
3948 return ret;
3949}
3950
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003951void cmd_oem_lock(const char *arg, void *data, unsigned sz)
3952{
lijuang4ece1e72015-08-14 21:02:36 +08003953 set_device_unlock(UNLOCK, FALSE);
Shashank Mittal162244e2011-08-08 19:01:25 -07003954}
3955
Shashank Mittala0032282011-08-26 14:50:11 -07003956void cmd_oem_devinfo(const char *arg, void *data, unsigned sz)
3957{
lijuang511a2b52015-08-14 20:50:51 +08003958 char response[MAX_RSP_SIZE];
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003959 snprintf(response, sizeof(response), "\tDevice tampered: %s", (device.is_tampered ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07003960 fastboot_info(response);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003961 snprintf(response, sizeof(response), "\tDevice unlocked: %s", (device.is_unlocked ? "true" : "false"));
3962 fastboot_info(response);
Mayank Grover889be1b2017-09-12 20:12:23 +05303963#if VERIFIED_BOOT
Mayank Grover912eaa62017-10-26 12:08:53 +05303964 if (VB_M <= target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +05303965 {
3966 snprintf(response, sizeof(response), "\tDevice critical unlocked: %s",
3967 (device.is_unlock_critical ? "true" : "false"));
3968 fastboot_info(response);
3969 }
Parth Dixitddbc7352015-10-18 03:13:31 +05303970#endif
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003971 snprintf(response, sizeof(response), "\tCharger screen enabled: %s", (device.charger_screen_enabled ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07003972 fastboot_info(response);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05303973 snprintf(response, sizeof(response), "\tDisplay panel: %s", (device.display_panel));
3974 fastboot_info(response);
Shashank Mittala0032282011-08-26 14:50:11 -07003975 fastboot_okay("");
3976}
3977
lijuang511a2b52015-08-14 20:50:51 +08003978void cmd_flashing_get_unlock_ability(const char *arg, void *data, unsigned sz)
3979{
3980 char response[MAX_RSP_SIZE];
3981 snprintf(response, sizeof(response), "\tget_unlock_ability: %d", is_allow_unlock);
3982 fastboot_info(response);
3983 fastboot_okay("");
3984}
3985
3986void cmd_flashing_lock_critical(const char *arg, void *data, unsigned sz)
3987{
lijuang4ece1e72015-08-14 21:02:36 +08003988 set_device_unlock(UNLOCK_CRITICAL, FALSE);
lijuang511a2b52015-08-14 20:50:51 +08003989}
3990
3991void cmd_flashing_unlock_critical(const char *arg, void *data, unsigned sz)
3992{
lijuang4ece1e72015-08-14 21:02:36 +08003993 set_device_unlock(UNLOCK_CRITICAL, TRUE);
lijuang511a2b52015-08-14 20:50:51 +08003994}
3995
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07003996void cmd_preflash(const char *arg, void *data, unsigned sz)
3997{
3998 fastboot_okay("");
3999}
4000
Mao Flynn7b379f32015-04-20 00:28:30 +08004001static uint8_t logo_header[LOGO_IMG_HEADER_SIZE];
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304002
Mao Flynn7b379f32015-04-20 00:28:30 +08004003int splash_screen_check_header(logo_img_header *header)
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304004{
Mao Flynn7b379f32015-04-20 00:28:30 +08004005 if (memcmp(header->magic, LOGO_IMG_MAGIC, 8))
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304006 return -1;
Mao Flynn7b379f32015-04-20 00:28:30 +08004007 if (header->width == 0 || header->height == 0)
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304008 return -1;
4009 return 0;
4010}
4011
Mao Flynn7b379f32015-04-20 00:28:30 +08004012int splash_screen_flash()
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07004013{
4014 struct ptentry *ptn;
4015 struct ptable *ptable;
Mao Flynn7b379f32015-04-20 00:28:30 +08004016 struct logo_img_header *header;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07004017 struct fbcon_config *fb_display = NULL;
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08004018
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304019 ptable = flash_get_ptable();
4020 if (ptable == NULL) {
Mao Flynn7b379f32015-04-20 00:28:30 +08004021 dprintf(CRITICAL, "ERROR: Partition table not found\n");
4022 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304023 }
Mao Flynn7b379f32015-04-20 00:28:30 +08004024
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304025 ptn = ptable_find(ptable, "splash");
4026 if (ptn == NULL) {
4027 dprintf(CRITICAL, "ERROR: splash Partition not found\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08004028 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304029 }
Mao Flynn7b379f32015-04-20 00:28:30 +08004030 if (flash_read(ptn, 0, (void *)logo_header, LOGO_IMG_HEADER_SIZE)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304031 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08004032 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304033 }
4034
Mao Flynn7b379f32015-04-20 00:28:30 +08004035 header = (struct logo_img_header *)logo_header;
4036 if (splash_screen_check_header(header)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304037 dprintf(CRITICAL, "ERROR: Boot image header invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08004038 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304039 }
4040
4041 fb_display = fbcon_display();
4042 if (fb_display) {
Mao Flynn7b379f32015-04-20 00:28:30 +08004043 if (header->type && (header->blocks != 0)) { // RLE24 compressed data
4044 uint8_t *base = (uint8_t *) fb_display->base + LOGO_IMG_OFFSET;
4045
4046 /* if the logo is full-screen size, remove "fbcon_clear()" */
4047 if ((header->width != fb_display->width)
4048 || (header->height != fb_display->height))
4049 fbcon_clear();
4050
4051 if (flash_read(ptn + LOGO_IMG_HEADER_SIZE, 0,
4052 (uint32_t *)base,
4053 (header->blocks * 512))) {
4054 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
4055 return -1;
4056 }
4057 fbcon_extract_to_screen(header, base);
4058 return 0;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07004059 }
Mao Flynn7b379f32015-04-20 00:28:30 +08004060
Veera Sundaram Sankaranc33416f2015-09-28 10:25:55 -07004061 if ((header->width > fb_display->width) || (header->height > fb_display->height)) {
4062 dprintf(CRITICAL, "Logo config greater than fb config. Fall back default logo\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08004063 return -1;
4064 }
4065
Vineet Bajaj99291ed2014-09-09 12:29:46 +05304066 uint8_t *base = (uint8_t *) fb_display->base;
Sachin Bhayare619e9e42017-05-15 13:10:31 +05304067 uint32_t fb_size = ROUNDUP(fb_display->width *
4068 fb_display->height *
4069 (fb_display->bpp / 8), 4096);
4070 uint32_t splash_size = ((((header->width * header->height *
4071 fb_display->bpp/8) + 511) >> 9) << 9);
4072
4073 if (splash_size > fb_size) {
4074 dprintf(CRITICAL, "ERROR: Splash image size invalid\n");
4075 return -1;
4076 }
4077
Mao Flynn7b379f32015-04-20 00:28:30 +08004078 if (flash_read(ptn + LOGO_IMG_HEADER_SIZE, 0,
4079 (uint32_t *)base,
4080 ((((header->width * header->height * fb_display->bpp/8) + 511) >> 9) << 9))) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304081 fbcon_clear();
Vineet Bajaj99291ed2014-09-09 12:29:46 +05304082 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08004083 return -1;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07004084 }
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304085 }
4086
Mao Flynn7b379f32015-04-20 00:28:30 +08004087 return 0;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304088}
4089
Mao Flynn7b379f32015-04-20 00:28:30 +08004090int splash_screen_mmc()
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304091{
4092 int index = INVALID_PTN;
4093 unsigned long long ptn = 0;
4094 struct fbcon_config *fb_display = NULL;
Mao Flynn7b379f32015-04-20 00:28:30 +08004095 struct logo_img_header *header;
Mao Flynn1893a7f2015-06-03 12:03:36 +08004096 uint32_t blocksize, realsize, readsize;
4097 uint8_t *base;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304098
4099 index = partition_get_index("splash");
4100 if (index == 0) {
4101 dprintf(CRITICAL, "ERROR: splash Partition table not found\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08004102 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304103 }
4104
4105 ptn = partition_get_offset(index);
4106 if (ptn == 0) {
4107 dprintf(CRITICAL, "ERROR: splash Partition invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08004108 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304109 }
4110
Mao Flynn1893a7f2015-06-03 12:03:36 +08004111 mmc_set_lun(partition_get_lun(index));
4112
4113 blocksize = mmc_get_device_blocksize();
4114 if (blocksize == 0) {
4115 dprintf(CRITICAL, "ERROR:splash Partition invalid blocksize\n");
4116 return -1;
4117 }
4118
4119 fb_display = fbcon_display();
Channagoud Kadabidaf10a62015-07-22 11:51:55 -07004120 if (!fb_display)
4121 {
4122 dprintf(CRITICAL, "ERROR: fb config is not allocated\n");
4123 return -1;
4124 }
4125
Mao Flynn1893a7f2015-06-03 12:03:36 +08004126 base = (uint8_t *) fb_display->base;
4127
4128 if (mmc_read(ptn, (uint32_t *)(base + LOGO_IMG_OFFSET), blocksize)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304129 dprintf(CRITICAL, "ERROR: Cannot read splash image header\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08004130 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304131 }
4132
Mao Flynn1893a7f2015-06-03 12:03:36 +08004133 header = (struct logo_img_header *)(base + LOGO_IMG_OFFSET);
Mao Flynn7b379f32015-04-20 00:28:30 +08004134 if (splash_screen_check_header(header)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304135 dprintf(CRITICAL, "ERROR: Splash image header invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08004136 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304137 }
4138
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304139 if (fb_display) {
Mao Flynn1893a7f2015-06-03 12:03:36 +08004140 if (header->type && (header->blocks != 0)) { /* 1 RLE24 compressed data */
4141 base += LOGO_IMG_OFFSET;
Mao Flynn7b379f32015-04-20 00:28:30 +08004142
Mao Flynn1893a7f2015-06-03 12:03:36 +08004143 realsize = header->blocks * 512;
4144 readsize = ROUNDUP((realsize + LOGO_IMG_HEADER_SIZE), blocksize) - blocksize;
4145
4146 /* if the logo is not full-screen size, clean screen */
Mao Flynn7b379f32015-04-20 00:28:30 +08004147 if ((header->width != fb_display->width)
4148 || (header->height != fb_display->height))
4149 fbcon_clear();
4150
Sachin Bhayare619e9e42017-05-15 13:10:31 +05304151 uint32_t fb_size = ROUNDUP(fb_display->width *
4152 fb_display->height *
4153 (fb_display->bpp / 8), 4096);
4154
4155 if (readsize > fb_size) {
4156 dprintf(CRITICAL, "ERROR: Splash image size invalid\n");
4157 return -1;
4158 }
4159
Mao Flynn1893a7f2015-06-03 12:03:36 +08004160 if (mmc_read(ptn + blocksize, (uint32_t *)(base + blocksize), readsize)) {
Mao Flynn7b379f32015-04-20 00:28:30 +08004161 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
4162 return -1;
4163 }
Mao Flynn7b379f32015-04-20 00:28:30 +08004164
Mao Flynn1893a7f2015-06-03 12:03:36 +08004165 fbcon_extract_to_screen(header, (base + LOGO_IMG_HEADER_SIZE));
4166 } else { /* 2 Raw BGR data */
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304167
Veera Sundaram Sankaranc33416f2015-09-28 10:25:55 -07004168 if ((header->width > fb_display->width) || (header->height > fb_display->height)) {
4169 dprintf(CRITICAL, "Logo config greater than fb config. Fall back default logo\n");
Mao Flynn1893a7f2015-06-03 12:03:36 +08004170 return -1;
4171 }
4172
4173 realsize = header->width * header->height * fb_display->bpp / 8;
4174 readsize = ROUNDUP((realsize + LOGO_IMG_HEADER_SIZE), blocksize) - blocksize;
4175
4176 if (blocksize == LOGO_IMG_HEADER_SIZE) { /* read the content directly */
4177 if (mmc_read((ptn + LOGO_IMG_HEADER_SIZE), (uint32_t *)base, readsize)) {
4178 fbcon_clear();
4179 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
4180 return -1;
4181 }
4182 } else {
4183 if (mmc_read(ptn + blocksize ,
4184 (uint32_t *)(base + LOGO_IMG_OFFSET + blocksize), readsize)) {
4185 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
4186 return -1;
4187 }
4188 memmove(base, (base + LOGO_IMG_OFFSET + LOGO_IMG_HEADER_SIZE), realsize);
4189 }
4190 }
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304191 }
4192
Mao Flynn7b379f32015-04-20 00:28:30 +08004193 return 0;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304194}
4195
Mao Flynn7b379f32015-04-20 00:28:30 +08004196int fetch_image_from_partition()
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304197{
4198 if (target_is_emmc_boot()) {
4199 return splash_screen_mmc();
4200 } else {
4201 return splash_screen_flash();
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07004202 }
4203}
4204
Mayank Grover351a75e2017-05-30 20:06:08 +05304205void publish_getvar_multislot_vars()
4206{
4207 int i,count;
4208 static bool published = false;
4209 static char slot_count[MAX_RSP_SIZE];
4210 static struct ab_slot_info slot_info[AB_SUPPORTED_SLOTS];
4211 static char active_slot_suffix[MAX_RSP_SIZE];
4212 static char has_slot_pname[NUM_PARTITIONS][MAX_GET_VAR_NAME_SIZE];
4213 static char has_slot_reply[NUM_PARTITIONS][MAX_RSP_SIZE];
4214 const char *tmp;
4215 char tmpbuff[MAX_GET_VAR_NAME_SIZE];
Mayank Grover2bd84bb2017-06-20 11:06:07 +05304216 signed active_slt;
Mayank Grover351a75e2017-05-30 20:06:08 +05304217
4218 if (!published)
4219 {
4220 /* Update slot meta info */
4221 count = partition_fill_partition_meta(has_slot_pname, has_slot_reply,
4222 partition_get_partition_count());
4223 for(i=0; i<count; i++)
4224 {
4225 memset(tmpbuff, 0, MAX_GET_VAR_NAME_SIZE);
Mayank Grover2bd84bb2017-06-20 11:06:07 +05304226 snprintf(tmpbuff, MAX_GET_VAR_NAME_SIZE,"has-slot:%s",
4227 has_slot_pname[i]);
4228 strlcpy(has_slot_pname[i], tmpbuff, MAX_GET_VAR_NAME_SIZE);
Mayank Grover351a75e2017-05-30 20:06:08 +05304229 fastboot_publish(has_slot_pname[i], has_slot_reply[i]);
4230 }
4231
4232 for (i=0; i<AB_SUPPORTED_SLOTS; i++)
4233 {
4234 tmp = SUFFIX_SLOT(i);
Mayank Grover7b8a8f82017-10-27 13:07:59 +05304235 tmp++; // to remove "_" from slot_suffix.
Mayank Grover2bd84bb2017-06-20 11:06:07 +05304236 snprintf(slot_info[i].slot_is_unbootable, sizeof(slot_info[i].slot_is_unbootable),
4237 "slot-unbootable:%s", tmp);
4238 snprintf(slot_info[i].slot_is_active, sizeof(slot_info[i].slot_is_active),
4239 "slot-active:%s", tmp);
4240 snprintf(slot_info[i].slot_is_succesful, sizeof(slot_info[i].slot_is_succesful),
4241 "slot-success:%s", tmp);
4242 snprintf(slot_info[i].slot_retry_count, sizeof(slot_info[i].slot_retry_count),
4243 "slot-retry-count:%s", tmp);
Mayank Grover351a75e2017-05-30 20:06:08 +05304244 fastboot_publish(slot_info[i].slot_is_unbootable,
4245 slot_info[i].slot_is_unbootable_rsp);
4246 fastboot_publish(slot_info[i].slot_is_active,
4247 slot_info[i].slot_is_active_rsp);
4248 fastboot_publish(slot_info[i].slot_is_succesful,
4249 slot_info[i].slot_is_succesful_rsp);
4250 fastboot_publish(slot_info[i].slot_retry_count,
4251 slot_info[i].slot_retry_count_rsp);
4252 }
4253 fastboot_publish("current-slot", active_slot_suffix);
4254 snprintf(slot_count, sizeof(slot_count),"%d", AB_SUPPORTED_SLOTS);
4255 fastboot_publish("slot-count", slot_count);
4256 published = true;
4257 }
4258
Mayank Grover2bd84bb2017-06-20 11:06:07 +05304259 active_slt = partition_find_active_slot();
4260 if (active_slt != INVALID)
Mayank Grover7b8a8f82017-10-27 13:07:59 +05304261 {
4262 tmp = SUFFIX_SLOT(active_slt);
4263 tmp++; // to remove "_" from slot_suffix.
4264 snprintf(active_slot_suffix, sizeof(active_slot_suffix), "%s", tmp);
4265 }
Mayank Grover2bd84bb2017-06-20 11:06:07 +05304266 else
4267 strlcpy(active_slot_suffix, "INVALID", sizeof(active_slot_suffix));
4268
Mayank Grover351a75e2017-05-30 20:06:08 +05304269 /* Update partition meta information */
4270 partition_fill_slot_meta(slot_info);
4271 return;
4272}
4273
lijuang4ece1e72015-08-14 21:02:36 +08004274void get_product_name(unsigned char *buf)
4275{
4276 snprintf((char*)buf, MAX_RSP_SIZE, "%s", TARGET(BOARD));
4277 return;
4278}
4279
Mayank Grover7b8a8f82017-10-27 13:07:59 +05304280#if PRODUCT_IOT
4281void get_bootloader_version_iot(unsigned char *buf)
4282{
4283 if (buf != NULL)
4284 {
4285 strlcpy(buf, TARGET(BOARD), MAX_VERSION_LEN);
4286 strlcat(buf, "-", MAX_VERSION_LEN);
4287 strlcat(buf, PRODUCT_IOT_VERSION, MAX_VERSION_LEN);
4288 }
4289 return;
4290}
4291#endif
4292
lijuang4ece1e72015-08-14 21:02:36 +08004293void get_bootloader_version(unsigned char *buf)
4294{
4295 snprintf((char*)buf, MAX_RSP_SIZE, "%s", device.bootloader_version);
4296 return;
4297}
4298
4299void get_baseband_version(unsigned char *buf)
4300{
4301 snprintf((char*)buf, MAX_RSP_SIZE, "%s", device.radio_version);
4302 return;
4303}
4304
4305bool is_device_locked()
4306{
4307 return device.is_unlocked ? false:true;
4308}
4309
Amol Jadi5edf3552013-07-23 14:15:34 -07004310/* register commands and variables for fastboot */
4311void aboot_fastboot_register_commands(void)
4312{
Sridhar Parasurame94e8152014-10-24 14:06:03 -07004313 int i;
lijuangf16461c2015-08-03 17:09:34 +08004314 char hw_platform_buf[MAX_RSP_SIZE];
Amol Jadi5edf3552013-07-23 14:15:34 -07004315
Sridhar Parasurame94e8152014-10-24 14:06:03 -07004316 struct fastboot_cmd_desc cmd_list[] = {
lijuang511a2b52015-08-14 20:50:51 +08004317 /* By default the enabled list is empty. */
4318 {"", NULL},
4319 /* move commands enclosed within the below ifndef to here
4320 * if they need to be enabled in user build.
4321 */
Sridhar Parasurame94e8152014-10-24 14:06:03 -07004322#ifndef DISABLE_FASTBOOT_CMDS
lijuang511a2b52015-08-14 20:50:51 +08004323 /* Register the following commands only for non-user builds */
4324 {"flash:", cmd_flash},
4325 {"erase:", cmd_erase},
4326 {"boot", cmd_boot},
4327 {"continue", cmd_continue},
4328 {"reboot", cmd_reboot},
4329 {"reboot-bootloader", cmd_reboot_bootloader},
4330 {"oem unlock", cmd_oem_unlock},
4331 {"oem unlock-go", cmd_oem_unlock_go},
4332 {"oem lock", cmd_oem_lock},
4333 {"flashing unlock", cmd_oem_unlock},
4334 {"flashing lock", cmd_oem_lock},
4335 {"flashing lock_critical", cmd_flashing_lock_critical},
4336 {"flashing unlock_critical", cmd_flashing_unlock_critical},
4337 {"flashing get_unlock_ability", cmd_flashing_get_unlock_ability},
4338 {"oem device-info", cmd_oem_devinfo},
4339 {"preflash", cmd_preflash},
4340 {"oem enable-charger-screen", cmd_oem_enable_charger_screen},
4341 {"oem disable-charger-screen", cmd_oem_disable_charger_screen},
lijuanga25d8bb2015-09-16 13:11:52 +08004342 {"oem off-mode-charge", cmd_oem_off_mode_charger},
lijuang511a2b52015-08-14 20:50:51 +08004343 {"oem select-display-panel", cmd_oem_select_display_panel},
Mayank Grover6ccc1c92017-07-04 17:36:46 +05304344 {"set_active",cmd_set_active},
Channagoud Kadabi736c4962015-08-21 11:56:52 -07004345#if UNITTEST_FW_SUPPORT
Channagoud Kadabid5ddf482015-09-28 10:31:35 -07004346 {"oem run-tests", cmd_oem_runtests},
Channagoud Kadabi736c4962015-08-21 11:56:52 -07004347#endif
Sridhar Parasurame94e8152014-10-24 14:06:03 -07004348#endif
lijuang511a2b52015-08-14 20:50:51 +08004349 };
Sridhar Parasurame94e8152014-10-24 14:06:03 -07004350
4351 int fastboot_cmds_count = sizeof(cmd_list)/sizeof(cmd_list[0]);
4352 for (i = 1; i < fastboot_cmds_count; i++)
4353 fastboot_register(cmd_list[i].name,cmd_list[i].cb);
4354
Amol Jadi5edf3552013-07-23 14:15:34 -07004355 /* publish variables and their values */
4356 fastboot_publish("product", TARGET(BOARD));
4357 fastboot_publish("kernel", "lk");
4358 fastboot_publish("serialno", sn_buf);
4359
4360 /*
4361 * partition info is supported only for emmc partitions
4362 * Calling this for NAND prints some error messages which
4363 * is harmless but misleading. Avoid calling this for NAND
4364 * devices.
4365 */
4366 if (target_is_emmc_boot())
Mayank Grover7b8a8f82017-10-27 13:07:59 +05304367 publish_getvar_partition_info(part_info, partition_get_partition_count());
Amol Jadi5edf3552013-07-23 14:15:34 -07004368
Mayank Grover351a75e2017-05-30 20:06:08 +05304369 if (partition_multislot_is_supported())
4370 publish_getvar_multislot_vars();
4371
Amol Jadi5edf3552013-07-23 14:15:34 -07004372 /* Max download size supported */
Ameya Thakur11cf1a62013-08-05 12:44:48 -07004373 snprintf(max_download_size, MAX_RSP_SIZE, "\t0x%x",
4374 target_get_max_flash_size());
Amol Jadi5edf3552013-07-23 14:15:34 -07004375 fastboot_publish("max-download-size", (const char *) max_download_size);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07004376 /* Is the charger screen check enabled */
4377 snprintf(charger_screen_enabled, MAX_RSP_SIZE, "%d",
4378 device.charger_screen_enabled);
4379 fastboot_publish("charger-screen-enabled",
4380 (const char *) charger_screen_enabled);
lijuanga25d8bb2015-09-16 13:11:52 +08004381 fastboot_publish("off-mode-charge", (const char *) charger_screen_enabled);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05304382 snprintf(panel_display_mode, MAX_RSP_SIZE, "%s",
4383 device.display_panel);
4384 fastboot_publish("display-panel",
4385 (const char *) panel_display_mode);
Mayank Grover7b8a8f82017-10-27 13:07:59 +05304386#if PRODUCT_IOT
4387 get_bootloader_version_iot(&bootloader_version_string);
4388 fastboot_publish("version-bootloader", (const char *) bootloader_version_string);
4389
4390 /* Version baseband is n/a for apq iot devices */
4391 fastboot_publish("version-baseband", "N/A");
4392
4393 /* IOT targets support only mmc target */
4394 snprintf(block_size_string, MAX_RSP_SIZE, "0x%x", mmc_get_device_blocksize());
4395 fastboot_publish("erase-block-size", (const char *) block_size_string);
4396 fastboot_publish("logical-block-size", (const char *) block_size_string);
4397#else
Channagoud Kadabiaafbbfc2015-03-04 17:52:56 -08004398 fastboot_publish("version-bootloader", (const char *) device.bootloader_version);
4399 fastboot_publish("version-baseband", (const char *) device.radio_version);
Mayank Grover7b8a8f82017-10-27 13:07:59 +05304400#endif
lijuangf16461c2015-08-03 17:09:34 +08004401 fastboot_publish("secure", is_secure_boot_enable()? "yes":"no");
Mayank Grover7b8a8f82017-10-27 13:07:59 +05304402 fastboot_publish("unlocked", device.is_unlocked ? "yes":"no");
lijuangf16461c2015-08-03 17:09:34 +08004403 smem_get_hw_platform_name((unsigned char *) hw_platform_buf, sizeof(hw_platform_buf));
4404 snprintf(get_variant, MAX_RSP_SIZE, "%s %s", hw_platform_buf,
4405 target_is_emmc_boot()? "eMMC":"UFS");
4406 fastboot_publish("variant", (const char *) get_variant);
lijuang65c5a822015-08-29 16:35:36 +08004407#if CHECK_BAT_VOLTAGE
lijuang102dfa92015-10-09 18:31:03 +08004408 update_battery_status();
lijuang65c5a822015-08-29 16:35:36 +08004409 fastboot_publish("battery-voltage", (const char *) battery_voltage);
lijuang102dfa92015-10-09 18:31:03 +08004410 fastboot_publish("battery-soc-ok", (const char *) battery_soc_ok);
lijuang65c5a822015-08-29 16:35:36 +08004411#endif
Amol Jadi5edf3552013-07-23 14:15:34 -07004412}
4413
Brian Swetland9c4c0752009-01-25 16:23:50 -08004414void aboot_init(const struct app_descriptor *app)
4415{
Shashank Mittal4f99a882010-02-01 13:58:50 -08004416 unsigned reboot_mode = 0;
Mayank Grover351a75e2017-05-30 20:06:08 +05304417 int boot_err_type = 0;
4418 int boot_slot = INVALID;
Chandan Uddarajubedca152010-06-02 23:05:15 -07004419
lijuang39831732016-01-08 17:49:02 +08004420 /* Initialise wdog to catch early lk crashes */
4421#if WDOG_SUPPORT
4422 msm_wdog_init();
4423#endif
4424
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07004425 /* Setup page size information for nv storage */
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004426 if (target_is_emmc_boot())
4427 {
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07004428 page_size = mmc_page_size();
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004429 page_mask = page_size - 1;
Gaurav Nebhwani4d2389c2016-03-17 21:10:05 +05304430 mmc_blocksize = mmc_get_device_blocksize();
4431 mmc_blocksize_mask = mmc_blocksize - 1;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004432 }
4433 else
4434 {
4435 page_size = flash_page_size();
4436 page_mask = page_size - 1;
4437 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07004438 ASSERT((MEMBASE + MEMSIZE) > MEMBASE);
4439
Ameya Thakur11cf1a62013-08-05 12:44:48 -07004440 read_device_info(&device);
vijay kumarc65876c2015-04-24 13:29:16 +05304441 read_allow_oem_unlock(&device);
Shashank Mittal162244e2011-08-08 19:01:25 -07004442
Mayank Grover351a75e2017-05-30 20:06:08 +05304443 /* Detect multi-slot support */
4444 if (partition_multislot_is_supported())
4445 {
4446 boot_slot = partition_find_active_slot();
4447 if (boot_slot == INVALID)
4448 {
4449 boot_into_fastboot = true;
4450 dprintf(INFO, "Active Slot: (INVALID)\n");
4451 }
4452 else
4453 {
4454 /* Setting the state of system to boot active slot */
4455 partition_mark_active_slot(boot_slot);
4456 dprintf(INFO, "Active Slot: (%s)\n", SUFFIX_SLOT(boot_slot));
4457 }
4458 }
4459
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08004460 /* Display splash screen if enabled */
4461#if DISPLAY_SPLASH_SCREEN
lijuang99c02d82015-02-13 19:04:34 +08004462#if NO_ALARM_DISPLAY
4463 if (!check_alarm_boot()) {
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08004464#endif
lijuang99c02d82015-02-13 19:04:34 +08004465 dprintf(SPEW, "Display Init: Start\n");
Ajay Singh Parmara7865a82015-04-16 21:27:52 -07004466#if DISPLAY_HDMI_PRIMARY
4467 if (!strlen(device.display_panel))
4468 strlcpy(device.display_panel, DISPLAY_PANEL_HDMI,
4469 sizeof(device.display_panel));
4470#endif
Channagoud Kadabi90869ce2015-04-27 11:15:14 -07004471#if ENABLE_WBC
Channagoud Kadabid801ac72015-08-26 11:21:51 -07004472 /* Wait if the display shutdown is in progress */
4473 while(pm_app_display_shutdown_in_prgs());
Channagoud Kadabi90869ce2015-04-27 11:15:14 -07004474 if (!pm_appsbl_display_init_done())
4475 target_display_init(device.display_panel);
4476 else
4477 display_image_on_screen();
4478#else
lijuang99c02d82015-02-13 19:04:34 +08004479 target_display_init(device.display_panel);
Channagoud Kadabi90869ce2015-04-27 11:15:14 -07004480#endif
lijuang99c02d82015-02-13 19:04:34 +08004481 dprintf(SPEW, "Display Init: Done\n");
4482#if NO_ALARM_DISPLAY
4483 }
4484#endif
4485#endif
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08004486
Greg Griscod6250552011-06-29 14:40:23 -07004487 target_serialno((unsigned char *) sn_buf);
Ajay Dudanib06c05f2011-05-12 14:46:10 -07004488 dprintf(SPEW,"serial number: %s\n",sn_buf);
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -08004489
Dhaval Patel223ec952013-07-18 14:49:44 -07004490 memset(display_panel_buf, '\0', MAX_PANEL_BUF_SIZE);
4491
Matthew Qindefd5562014-07-11 18:02:40 +08004492 /*
4493 * Check power off reason if user force reset,
4494 * if yes phone will do normal boot.
4495 */
4496 if (is_user_force_reset())
4497 goto normal_boot;
4498
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07004499 /* Check if we should do something other than booting up */
Ameya Thakur0b9c2442013-05-10 13:22:56 -07004500 if (keys_get_state(KEY_VOLUMEUP) && keys_get_state(KEY_VOLUMEDOWN))
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07004501 {
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03004502 dprintf(ALWAYS,"dload mode key sequence detected\n");
lijuang395b5e62015-11-19 17:39:44 +08004503 reboot_device(EMERGENCY_DLOAD);
4504 dprintf(CRITICAL,"Failed to reboot into dload mode\n");
4505
Ameya Thakur0b9c2442013-05-10 13:22:56 -07004506 boot_into_fastboot = true;
4507 }
4508 if (!boot_into_fastboot)
4509 {
4510 if (keys_get_state(KEY_HOME) || keys_get_state(KEY_VOLUMEUP))
4511 boot_into_recovery = 1;
4512 if (!boot_into_recovery &&
4513 (keys_get_state(KEY_BACK) || keys_get_state(KEY_VOLUMEDOWN)))
Pavel Nedev5d91d412013-04-29 11:34:24 +03004514 boot_into_fastboot = true;
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07004515 }
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004516 #if NO_KEYPAD_DRIVER
Kinson Chik0b1c8162011-08-31 16:31:57 -07004517 if (fastboot_trigger())
Pavel Nedev5d91d412013-04-29 11:34:24 +03004518 boot_into_fastboot = true;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004519 #endif
Chandan Uddarajubedca152010-06-02 23:05:15 -07004520
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07004521#if USE_PON_REBOOT_REG
4522 reboot_mode = check_hard_reboot_mode();
4523#else
Ajay Dudani77421292010-10-27 19:34:06 -07004524 reboot_mode = check_reboot_mode();
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07004525#endif
4526 if (reboot_mode == RECOVERY_MODE)
4527 {
Ajay Dudani77421292010-10-27 19:34:06 -07004528 boot_into_recovery = 1;
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07004529 }
4530 else if(reboot_mode == FASTBOOT_MODE)
4531 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03004532 boot_into_fastboot = true;
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07004533 }
4534 else if(reboot_mode == ALARM_BOOT)
4535 {
Matthew Qind886f3c2014-01-17 16:52:01 +08004536 boot_reason_alarm = true;
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07004537 }
Parth Dixit207573a2015-10-27 17:26:21 +05304538#if VERIFIED_BOOT
Mayank Grover912eaa62017-10-26 12:08:53 +05304539 else if (VB_M <= target_get_vb_version())
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07004540 {
Mayank Grover889be1b2017-09-12 20:12:23 +05304541 if (reboot_mode == DM_VERITY_ENFORCING)
4542 {
4543 device.verity_mode = 1;
4544 write_device_info(&device);
4545 }
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07004546#if ENABLE_VB_ATTEST
Mayank Grover889be1b2017-09-12 20:12:23 +05304547 else if (reboot_mode == DM_VERITY_EIO)
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07004548#else
Mayank Grover889be1b2017-09-12 20:12:23 +05304549 else if (reboot_mode == DM_VERITY_LOGGING)
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07004550#endif
Mayank Grover889be1b2017-09-12 20:12:23 +05304551 {
4552 device.verity_mode = 0;
4553 write_device_info(&device);
4554 }
4555 else if (reboot_mode == DM_VERITY_KEYSCLEAR)
4556 {
4557 if(send_delete_keys_to_tz())
4558 ASSERT(0);
4559 }
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07004560 }
Parth Dixitddbc7352015-10-18 03:13:31 +05304561#endif
Ajay Dudani77421292010-10-27 19:34:06 -07004562
Matthew Qindefd5562014-07-11 18:02:40 +08004563normal_boot:
Pavel Nedev5d91d412013-04-29 11:34:24 +03004564 if (!boot_into_fastboot)
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004565 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03004566 if (target_is_emmc_boot())
Shashank Mittala0032282011-08-26 14:50:11 -07004567 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03004568 if(emmc_recovery_init())
4569 dprintf(ALWAYS,"error in emmc_recovery_init\n");
4570 if(target_use_signed_kernel())
Shashank Mittala0032282011-08-26 14:50:11 -07004571 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03004572 if((device.is_unlocked) || (device.is_tampered))
4573 {
4574 #ifdef TZ_TAMPER_FUSE
4575 set_tamper_fuse_cmd();
4576 #endif
4577 #if USE_PCOM_SECBOOT
4578 set_tamper_flag(device.is_tampered);
4579 #endif
4580 }
Shashank Mittala0032282011-08-26 14:50:11 -07004581 }
Amit Blay6281ebc2015-01-11 14:44:08 +02004582
Mayank Grover351a75e2017-05-30 20:06:08 +05304583retry_boot:
4584 /* Trying to boot active partition */
4585 if (partition_multislot_is_supported())
4586 {
4587 boot_slot = partition_find_boot_slot();
4588 partition_mark_active_slot(boot_slot);
4589 if (boot_slot == INVALID)
4590 goto fastboot;
4591 }
4592
4593 boot_err_type = boot_linux_from_mmc();
4594 switch (boot_err_type)
4595 {
4596 case ERR_INVALID_PAGE_SIZE:
4597 case ERR_DT_PARSE:
4598 case ERR_ABOOT_ADDR_OVERLAP:
Mayank Grover10cabbe2017-10-30 19:11:05 +05304599 case ERR_INVALID_BOOT_MAGIC:
Mayank Grover351a75e2017-05-30 20:06:08 +05304600 if(partition_multislot_is_supported())
Mayank Grover10cabbe2017-10-30 19:11:05 +05304601 {
4602 /*
4603 * Deactivate current slot, as it failed to
4604 * boot, and retry next slot.
4605 */
4606 partition_deactivate_slot(boot_slot);
Mayank Grover351a75e2017-05-30 20:06:08 +05304607 goto retry_boot;
Mayank Grover10cabbe2017-10-30 19:11:05 +05304608 }
Mayank Grover351a75e2017-05-30 20:06:08 +05304609 else
4610 break;
Mayank Grover351a75e2017-05-30 20:06:08 +05304611 default:
4612 break;
4613 /* going to fastboot menu */
4614 }
Shashank Mittala0032282011-08-26 14:50:11 -07004615 }
Pavel Nedev5d91d412013-04-29 11:34:24 +03004616 else
4617 {
4618 recovery_init();
4619 #if USE_PCOM_SECBOOT
4620 if((device.is_unlocked) || (device.is_tampered))
4621 set_tamper_flag(device.is_tampered);
4622 #endif
4623 boot_linux_from_flash();
4624 }
4625 dprintf(CRITICAL, "ERROR: Could not do normal boot. Reverting "
4626 "to fastboot mode.\n");
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004627 }
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07004628
Mayank Grover351a75e2017-05-30 20:06:08 +05304629fastboot:
Amol Jadi5edf3552013-07-23 14:15:34 -07004630 /* We are here means regular boot did not happen. Start fastboot. */
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07004631
Amol Jadi5edf3552013-07-23 14:15:34 -07004632 /* register aboot specific fastboot commands */
4633 aboot_fastboot_register_commands();
Amol Jadi57abe4c2011-05-24 15:47:27 -07004634
Amol Jadi5edf3552013-07-23 14:15:34 -07004635 /* dump partition table for debug info */
Kinson Chikf1a43512011-07-14 11:28:39 -07004636 partition_dump();
Amol Jadi5edf3552013-07-23 14:15:34 -07004637
4638 /* initialize and start fastboot */
4639 fastboot_init(target_get_scratch_address(), target_get_max_flash_size());
lijuang4ece1e72015-08-14 21:02:36 +08004640#if FBCON_DISPLAY_MSG
lijuang9a7d3b92015-11-30 14:41:24 +08004641 display_fastboot_menu();
lijuang4ece1e72015-08-14 21:02:36 +08004642#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08004643}
4644
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07004645uint32_t get_page_size()
4646{
4647 return page_size;
4648}
4649
Amir Samuelov57a6fa22013-06-05 16:36:43 +03004650/*
4651 * Calculated and save hash (SHA256) for non-signed boot image.
4652 *
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07004653 * @param image_addr - Boot image address
4654 * @param image_size - Size of the boot image
Amir Samuelov57a6fa22013-06-05 16:36:43 +03004655 *
4656 * @return int - 0 on success, negative value on failure.
4657 */
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07004658static int aboot_save_boot_hash_mmc(uint32_t image_addr, uint32_t image_size)
Amir Samuelov57a6fa22013-06-05 16:36:43 +03004659{
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07004660 unsigned int digest[8];
4661#if IMAGE_VERIF_ALGO_SHA1
4662 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA1;
4663#else
4664 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA256;
4665#endif
Amir Samuelov57a6fa22013-06-05 16:36:43 +03004666
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07004667 target_crypto_init_params();
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -08004668 hash_find((unsigned char *) image_addr, image_size, (unsigned char *)&digest, auth_algo);
Amir Samuelov57a6fa22013-06-05 16:36:43 +03004669
4670 save_kernel_hash_cmd(digest);
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07004671 dprintf(INFO, "aboot_save_boot_hash_mmc: imagesize_actual size %d bytes.\n", (int) image_size);
Amir Samuelov57a6fa22013-06-05 16:36:43 +03004672
4673 return 0;
4674}
4675
Mayank Grover351a75e2017-05-30 20:06:08 +05304676
Brian Swetland9c4c0752009-01-25 16:23:50 -08004677APP_START(aboot)
4678 .init = aboot_init,
4679APP_END