blob: 77b428299fba2d211622106437224477968a1e7f [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";
189static const char *sys_path = " root=/dev/mmcblk0p";
Ajay Dudanid04110c2011-01-17 23:55:07 -0800190
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700191#if VERIFIED_BOOT
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700192static const char *verity_mode = " androidboot.veritymode=";
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700193static const char *verified_state= " androidboot.verifiedbootstate=";
Parth Dixita5715a02015-10-29 12:25:10 +0530194static const char *keymaster_v1= " androidboot.keymaster=1";
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700195//indexed based on enum values, green is 0 by default
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700196
197struct verified_boot_verity_mode vbvm[] =
198{
Channagoud Kadabi86b0c112016-03-16 19:23:16 -0700199#if ENABLE_VB_ATTEST
200 {false, "eio"},
201#else
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700202 {false, "logging"},
Channagoud Kadabi86b0c112016-03-16 19:23:16 -0700203#endif
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700204 {true, "enforcing"},
205};
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700206struct verified_boot_state_name vbsn[] =
207{
208 {GREEN, "green"},
209 {ORANGE, "orange"},
210 {YELLOW,"yellow"},
211 {RED,"red" },
212};
213#endif
Channagoud Kadabi86b0c112016-03-16 19:23:16 -0700214/*As per spec delay wait time before shutdown in Red state*/
215#define DELAY_WAIT 30000
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700216static unsigned page_size = 0;
217static unsigned page_mask = 0;
Gaurav Nebhwani4d2389c2016-03-17 21:10:05 +0530218static unsigned mmc_blocksize = 0;
219static unsigned mmc_blocksize_mask = 0;
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700220static char ffbm_mode_string[FFBM_MODE_BUF_SIZE];
221static bool boot_into_ffbm;
vijay kumar870515d2015-08-31 16:37:24 +0530222static char *target_boot_params = NULL;
Matthew Qind886f3c2014-01-17 16:52:01 +0800223static bool boot_reason_alarm;
Channagoud Kadabi80a182b2015-03-11 17:04:23 -0700224static bool devinfo_present = true;
Channagoud Kadabi736c4962015-08-21 11:56:52 -0700225bool boot_into_fastboot = false;
Parth Dixit4097b622016-03-15 14:42:27 +0530226static uint32_t dt_size = 0;
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700227
Shashank Mittalcd98d472011-08-02 14:29:24 -0700228/* Assuming unauthorized kernel image by default */
229static int auth_kernel_img = 0;
lijuang511a2b52015-08-14 20:50:51 +0800230static device_info device = {DEVICE_MAGIC, 0, 0, 0, 0, {0}, {0},{0}, 1};
vijay kumarc65876c2015-04-24 13:29:16 +0530231static bool is_allow_unlock = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -0700232
vijay kumarca2e6812015-07-08 20:28:25 +0530233static char frp_ptns[2][8] = {"config","frp"};
234
lijuang511a2b52015-08-14 20:50:51 +0800235static const char *critical_flash_allowed_ptn[] = {
236 "aboot",
237 "rpm",
238 "tz",
239 "sbl",
240 "sdi",
241 "sbl1",
242 "xbl",
243 "hyp",
244 "pmic",
245 "bootloader",
246 "devinfo",
247 "partition"};
248
Dima Zavin42168f22009-01-30 11:52:22 -0800249struct atag_ptbl_entry
250{
251 char name[16];
252 unsigned offset;
253 unsigned size;
254 unsigned flags;
255};
256
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700257/*
258 * Partition info, required to be published
259 * for fastboot
260 */
261struct getvar_partition_info {
Mayank Grover7b8a8f82017-10-27 13:07:59 +0530262 char part_name[MAX_GPT_NAME_SIZE]; /* Partition name */
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700263 char getvar_size[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for size */
264 char getvar_type[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for type */
265 char size_response[MAX_RSP_SIZE]; /* fastboot response for size */
266 char type_response[MAX_RSP_SIZE]; /* fastboot response for type */
267};
268
269/*
Mayank Grover7b8a8f82017-10-27 13:07:59 +0530270 * Update the part_type_known for known paritions types.
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700271 */
Mayank Grover7b8a8f82017-10-27 13:07:59 +0530272struct getvar_partition_info part_info[NUM_PARTITIONS];
273struct getvar_partition_info part_type_known[] =
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700274{
275 { "system" , "partition-size:", "partition-type:", "", "ext4" },
276 { "userdata", "partition-size:", "partition-type:", "", "ext4" },
277 { "cache" , "partition-size:", "partition-type:", "", "ext4" },
278};
279
280char max_download_size[MAX_RSP_SIZE];
Ameya Thakur11cf1a62013-08-05 12:44:48 -0700281char charger_screen_enabled[MAX_RSP_SIZE];
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800282char sn_buf[13];
Dhaval Patel223ec952013-07-18 14:49:44 -0700283char display_panel_buf[MAX_PANEL_BUF_SIZE];
Unnati Gandhi62c8ab82014-01-24 11:01:01 +0530284char panel_display_mode[MAX_RSP_SIZE];
Mayank Grover7b8a8f82017-10-27 13:07:59 +0530285#if PRODUCT_IOT
286char block_size_string[MAX_RSP_SIZE];
287
288/* For IOT we are using custom version */
289#define PRODUCT_IOT_VERSION "IOT001"
290char bootloader_version_string[MAX_RSP_SIZE];
291#endif
lijuang102dfa92015-10-09 18:31:03 +0800292
293#if CHECK_BAT_VOLTAGE
lijuang65c5a822015-08-29 16:35:36 +0800294char battery_voltage[MAX_RSP_SIZE];
lijuang102dfa92015-10-09 18:31:03 +0800295char battery_soc_ok [MAX_RSP_SIZE];
296#endif
297
lijuangf16461c2015-08-03 17:09:34 +0800298char get_variant[MAX_RSP_SIZE];
Greg Griscod6250552011-06-29 14:40:23 -0700299
Greg Griscod2471ef2011-07-14 13:00:42 -0700300extern int emmc_recovery_init(void);
301
Kinson Chik0b1c8162011-08-31 16:31:57 -0700302#if NO_KEYPAD_DRIVER
303extern int fastboot_trigger(void);
304#endif
Greg Griscod2471ef2011-07-14 13:00:42 -0700305
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800306static void update_ker_tags_rdisk_addr(struct boot_img_hdr *hdr, bool is_arm64)
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700307{
308 /* overwrite the destination of specified for the project */
Channagoud Kadabi7042fa32013-04-26 16:44:14 -0700309#ifdef ABOOT_IGNORE_BOOT_HEADER_ADDRS
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800310 if (is_arm64)
311 hdr->kernel_addr = ABOOT_FORCE_KERNEL64_ADDR;
312 else
313 hdr->kernel_addr = ABOOT_FORCE_KERNEL_ADDR;
Channagoud Kadabi7042fa32013-04-26 16:44:14 -0700314 hdr->ramdisk_addr = ABOOT_FORCE_RAMDISK_ADDR;
315 hdr->tags_addr = ABOOT_FORCE_TAGS_ADDR;
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700316#endif
317}
318
Dima Zavin42168f22009-01-30 11:52:22 -0800319static void ptentry_to_tag(unsigned **ptr, struct ptentry *ptn)
320{
321 struct atag_ptbl_entry atag_ptn;
322
323 memcpy(atag_ptn.name, ptn->name, 16);
324 atag_ptn.name[15] = '\0';
325 atag_ptn.offset = ptn->start;
326 atag_ptn.size = ptn->length;
327 atag_ptn.flags = ptn->flags;
328 memcpy(*ptr, &atag_ptn, sizeof(struct atag_ptbl_entry));
329 *ptr += sizeof(struct atag_ptbl_entry) / sizeof(unsigned);
330}
Brian Swetland9c4c0752009-01-25 16:23:50 -0800331
lijuang102dfa92015-10-09 18:31:03 +0800332#if CHECK_BAT_VOLTAGE
333void update_battery_status(void)
334{
335 snprintf(battery_voltage,MAX_RSP_SIZE, "%d",target_get_battery_voltage());
336 snprintf(battery_soc_ok ,MAX_RSP_SIZE, "%s",target_battery_soc_ok()? "yes":"no");
337}
338#endif
339
Neeti Desaie245d492012-06-01 12:52:13 -0700340unsigned char *update_cmdline(const char * cmdline)
Brian Swetland9c4c0752009-01-25 16:23:50 -0800341{
David Ng183a7422009-12-07 14:55:21 -0800342 int cmdline_len = 0;
343 int have_cmdline = 0;
Amol Jadi168b7712012-03-06 16:15:00 -0800344 unsigned char *cmdline_final = NULL;
Neeti Desaie245d492012-06-01 12:52:13 -0700345 int pause_at_bootup = 0;
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800346 bool warm_boot = false;
Pavel Nedev5614d222013-06-17 18:01:02 +0300347 bool gpt_exists = partition_gpt_exists();
Joonwoo Park61112782013-10-02 19:50:39 -0700348 int have_target_boot_params = 0;
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700349 char *boot_dev_buf = NULL;
Mayank Grover351a75e2017-05-30 20:06:08 +0530350 bool is_mdtp_activated = 0;
351 int current_active_slot = INVALID;
Mayank Grover3804be72017-06-22 11:59:23 +0530352 int system_ptn_index = -1;
353 unsigned int lun = 0;
354 char lun_char_base = 'a';
355 int syspath_buflen = strlen(sys_path) + sizeof(int) + 1; /*allocate buflen for largest possible string*/
356 char syspath_buf[syspath_buflen];
Mayank Grover889be1b2017-09-12 20:12:23 +0530357#if VERIFIED_BOOT
358 uint32_t boot_state = RED;
359#endif
Hareesh Gauthamabf1e1e2017-04-12 18:09:24 +0530360
361#if USE_LE_SYSTEMD
362 is_systemd_present=true;
363#endif
364
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700365#if VERIFIED_BOOT
Mayank Grover912eaa62017-10-26 12:08:53 +0530366 if (VB_M <= target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +0530367 {
368 boot_state = boot_verify_get_state();
369 }
Parth Dixitddbc7352015-10-18 03:13:31 +0530370#endif
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700371
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200372#ifdef MDTP_SUPPORT
373 mdtp_activated(&is_mdtp_activated);
374#endif /* MDTP_SUPPORT */
Dima Zavin42168f22009-01-30 11:52:22 -0800375
Brian Swetland9c4c0752009-01-25 16:23:50 -0800376 if (cmdline && cmdline[0]) {
David Ng183a7422009-12-07 14:55:21 -0800377 cmdline_len = strlen(cmdline);
378 have_cmdline = 1;
379 }
380 if (target_is_emmc_boot()) {
381 cmdline_len += strlen(emmc_cmdline);
Sridhar Parasuram7bd4aaf2015-02-12 11:14:38 -0800382#if USE_BOOTDEV_CMDLINE
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700383 boot_dev_buf = (char *) malloc(sizeof(char) * BOOT_DEV_MAX_LEN);
384 ASSERT(boot_dev_buf);
385 platform_boot_dev_cmdline(boot_dev_buf);
386 cmdline_len += strlen(boot_dev_buf);
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700387#endif
David Ng183a7422009-12-07 14:55:21 -0800388 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800389
390 cmdline_len += strlen(usb_sn_cmdline);
391 cmdline_len += strlen(sn_buf);
392
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700393#if VERIFIED_BOOT
Mayank Grover912eaa62017-10-26 12:08:53 +0530394 if (VB_M <= target_get_vb_version())
Sridhar Parasuram7d8bb9a2015-09-23 18:21:19 -0700395 {
Mayank Grover889be1b2017-09-12 20:12:23 +0530396 cmdline_len += strlen(verified_state) + strlen(vbsn[boot_state].name);
397 if ((device.verity_mode != 0 ) && (device.verity_mode != 1))
398 {
399 dprintf(CRITICAL, "Devinfo paritition possibly corrupted!!!. Please erase devinfo partition to continue booting\n");
400 ASSERT(0);
401 }
402 cmdline_len += strlen(verity_mode) + strlen(vbvm[device.verity_mode].name);
403 cmdline_len += strlen(keymaster_v1);
Sridhar Parasuram7d8bb9a2015-09-23 18:21:19 -0700404 }
Parth Dixitddbc7352015-10-18 03:13:31 +0530405#endif
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700406
Pavel Nedev5614d222013-06-17 18:01:02 +0300407 if (boot_into_recovery && gpt_exists)
408 cmdline_len += strlen(secondary_gpt_enable);
409
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200410 if(is_mdtp_activated)
411 cmdline_len += strlen(mdtp_activated_flag);
412
Pavel Nedev328ac822013-04-05 15:25:11 +0300413 if (boot_into_ffbm) {
414 cmdline_len += strlen(androidboot_mode);
Hareesh Gauthamabf1e1e2017-04-12 18:09:24 +0530415
416 if(is_systemd_present)
417 cmdline_len += strlen(systemd_ffbm_mode);
418
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700419 cmdline_len += strlen(ffbm_mode_string);
Pavel Nedev898298c2013-02-27 12:36:09 -0800420 /* reduce kernel console messages to speed-up boot */
421 cmdline_len += strlen(loglevel);
Matthew Qind886f3c2014-01-17 16:52:01 +0800422 } else if (boot_reason_alarm) {
423 cmdline_len += strlen(alarmboot_cmdline);
Zhenhua Huang431dafa2015-06-30 16:13:37 +0800424 } else if ((target_build_variant_user() || device.charger_screen_enabled)
425 && target_pause_for_battery_charge()) {
David Ngf773dde2010-07-26 19:55:08 -0700426 pause_at_bootup = 1;
427 cmdline_len += strlen(battchg_pause);
428 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800429
Shashank Mittalcd98d472011-08-02 14:29:24 -0700430 if(target_use_signed_kernel() && auth_kernel_img) {
431 cmdline_len += strlen(auth_kernel);
432 }
433
Joonwoo Park61112782013-10-02 19:50:39 -0700434 if (get_target_boot_params(cmdline, boot_into_recovery ? "recoveryfs" :
435 "system",
vijay kumar870515d2015-08-31 16:37:24 +0530436 &target_boot_params) == 0) {
Joonwoo Park61112782013-10-02 19:50:39 -0700437 have_target_boot_params = 1;
438 cmdline_len += strlen(target_boot_params);
439 }
440
Ajay Dudanid04110c2011-01-17 23:55:07 -0800441 /* Determine correct androidboot.baseband to use */
442 switch(target_baseband())
443 {
444 case BASEBAND_APQ:
445 cmdline_len += strlen(baseband_apq);
446 break;
447
448 case BASEBAND_MSM:
449 cmdline_len += strlen(baseband_msm);
450 break;
451
452 case BASEBAND_CSFB:
453 cmdline_len += strlen(baseband_csfb);
454 break;
455
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800456 case BASEBAND_SVLTE2A:
457 cmdline_len += strlen(baseband_svlte2a);
Ajay Dudanid04110c2011-01-17 23:55:07 -0800458 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700459
460 case BASEBAND_MDM:
461 cmdline_len += strlen(baseband_mdm);
462 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700463
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800464 case BASEBAND_MDM2:
465 cmdline_len += strlen(baseband_mdm2);
466 break;
467
Amol Jadi5c61a952012-05-04 17:05:35 -0700468 case BASEBAND_SGLTE:
469 cmdline_len += strlen(baseband_sglte);
470 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530471
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800472 case BASEBAND_SGLTE2:
473 cmdline_len += strlen(baseband_sglte2);
474 break;
475
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530476 case BASEBAND_DSDA:
477 cmdline_len += strlen(baseband_dsda);
478 break;
Amol Jadi2a15a272013-01-22 12:03:36 -0800479
480 case BASEBAND_DSDA2:
481 cmdline_len += strlen(baseband_dsda2);
482 break;
Vijay Kumar Pendotib228cfc2016-06-13 20:15:23 +0530483 case BASEBAND_APQ_NOWGR:
484 cmdline_len += strlen(baseband_apq_nowgr);
485 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800486 }
487
Vladislav Levenetz3cd45242017-04-05 08:58:07 +0300488#if ENABLE_DISPLAY
Lijuan Gao4a5b8322014-07-24 10:38:42 +0800489 if (cmdline) {
490 if ((strstr(cmdline, DISPLAY_DEFAULT_PREFIX) == NULL) &&
Padmanabhan Komandurubccbcdc2015-06-30 16:19:24 +0530491 target_display_panel_node(display_panel_buf,
492 MAX_PANEL_BUF_SIZE) &&
Lijuan Gao4a5b8322014-07-24 10:38:42 +0800493 strlen(display_panel_buf)) {
494 cmdline_len += strlen(display_panel_buf);
495 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700496 }
Vladislav Levenetz3cd45242017-04-05 08:58:07 +0300497#endif
Dhaval Patel223ec952013-07-18 14:49:44 -0700498
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800499 if (target_warm_boot()) {
500 warm_boot = true;
501 cmdline_len += strlen(warmboot_cmdline);
502 }
503
Mayank Grover351a75e2017-05-30 20:06:08 +0530504 if (partition_multislot_is_supported())
505 {
506 current_active_slot = partition_find_active_slot();
507 cmdline_len += (strlen(androidboot_slot_suffix)+
508 strlen(SUFFIX_SLOT(current_active_slot)));
509
Mayank Grover3804be72017-06-22 11:59:23 +0530510 system_ptn_index = partition_get_index("system");
511 if (platform_boot_dev_isemmc())
512 {
513 snprintf(syspath_buf, syspath_buflen, " root=/dev/mmcblk0p%d",
514 system_ptn_index + 1);
515 }
516 else
517 {
518 lun = partition_get_lun(system_ptn_index);
519 snprintf(syspath_buf, syspath_buflen, " root=/dev/sd%c%d",
520 lun_char_base + lun,
521 partition_get_index_in_lun("system", lun));
522 }
Mayank Grover351a75e2017-05-30 20:06:08 +0530523
Mayank Grover3804be72017-06-22 11:59:23 +0530524 cmdline_len += strlen(sys_path_cmdline);
525 cmdline_len += strlen(syspath_buf);
Mayank Grover351a75e2017-05-30 20:06:08 +0530526 if (!boot_into_recovery)
527 cmdline_len += strlen(skip_ramfs);
528 }
529
Channagoud Kadabi85c7ec32016-01-28 23:09:21 -0800530#if TARGET_CMDLINE_SUPPORT
531 char *target_cmdline_buf = malloc(TARGET_MAX_CMDLNBUF);
532 int target_cmd_line_len;
533 ASSERT(target_cmdline_buf);
534 target_cmd_line_len = target_update_cmdline(target_cmdline_buf);
535 cmdline_len += target_cmd_line_len;
536#endif
537
David Ng183a7422009-12-07 14:55:21 -0800538 if (cmdline_len > 0) {
539 const char *src;
Maria Yu52254c02014-07-04 16:14:54 +0800540 unsigned char *dst;
541
542 cmdline_final = (unsigned char*) malloc((cmdline_len + 4) & (~3));
543 ASSERT(cmdline_final != NULL);
vijay kumar287bb542015-09-29 13:01:52 +0530544 memset((void *)cmdline_final, 0, sizeof(*cmdline_final));
Maria Yu52254c02014-07-04 16:14:54 +0800545 dst = cmdline_final;
Neeti Desaie245d492012-06-01 12:52:13 -0700546
Amol Jadi168b7712012-03-06 16:15:00 -0800547 /* Save start ptr for debug print */
David Ng183a7422009-12-07 14:55:21 -0800548 if (have_cmdline) {
549 src = cmdline;
550 while ((*dst++ = *src++));
551 }
552 if (target_is_emmc_boot()) {
553 src = emmc_cmdline;
554 if (have_cmdline) --dst;
David Ngf773dde2010-07-26 19:55:08 -0700555 have_cmdline = 1;
556 while ((*dst++ = *src++));
Sridhar Parasuram7bd4aaf2015-02-12 11:14:38 -0800557#if USE_BOOTDEV_CMDLINE
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700558 src = boot_dev_buf;
559 if (have_cmdline) --dst;
560 while ((*dst++ = *src++));
561#endif
David Ngf773dde2010-07-26 19:55:08 -0700562 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800563
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700564#if VERIFIED_BOOT
Mayank Grover912eaa62017-10-26 12:08:53 +0530565 if (VB_M <= target_get_vb_version())
Sridhar Parasuram7d8bb9a2015-09-23 18:21:19 -0700566 {
Mayank Grover889be1b2017-09-12 20:12:23 +0530567 src = verified_state;
568 if(have_cmdline) --dst;
569 have_cmdline = 1;
570 while ((*dst++ = *src++));
571 src = vbsn[boot_state].name;
572 if(have_cmdline) --dst;
573 while ((*dst++ = *src++));
574
575 if ((device.verity_mode != 0 ) && (device.verity_mode != 1))
576 {
577 dprintf(CRITICAL, "Devinfo paritition possibly corrupted!!!. Please erase devinfo partition to continue booting\n");
578 ASSERT(0);
579 }
580 src = verity_mode;
581 if(have_cmdline) --dst;
582 while ((*dst++ = *src++));
583 src = vbvm[device.verity_mode].name;
584 if(have_cmdline) -- dst;
585 while ((*dst++ = *src++));
586 src = keymaster_v1;
587 if(have_cmdline) --dst;
588 while ((*dst++ = *src++));
Sridhar Parasuram7d8bb9a2015-09-23 18:21:19 -0700589 }
Parth Dixitddbc7352015-10-18 03:13:31 +0530590#endif
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800591 src = usb_sn_cmdline;
592 if (have_cmdline) --dst;
593 have_cmdline = 1;
594 while ((*dst++ = *src++));
595 src = sn_buf;
596 if (have_cmdline) --dst;
597 have_cmdline = 1;
598 while ((*dst++ = *src++));
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800599 if (warm_boot) {
600 if (have_cmdline) --dst;
601 src = warmboot_cmdline;
602 while ((*dst++ = *src++));
603 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800604
Pavel Nedev5614d222013-06-17 18:01:02 +0300605 if (boot_into_recovery && gpt_exists) {
606 src = secondary_gpt_enable;
607 if (have_cmdline) --dst;
608 while ((*dst++ = *src++));
609 }
610
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200611 if (is_mdtp_activated) {
612 src = mdtp_activated_flag;
613 if (have_cmdline) --dst;
614 while ((*dst++ = *src++));
615 }
616
Pavel Nedev328ac822013-04-05 15:25:11 +0300617 if (boot_into_ffbm) {
618 src = androidboot_mode;
619 if (have_cmdline) --dst;
620 while ((*dst++ = *src++));
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700621 src = ffbm_mode_string;
Pavel Nedev328ac822013-04-05 15:25:11 +0300622 if (have_cmdline) --dst;
623 while ((*dst++ = *src++));
Hareesh Gauthamabf1e1e2017-04-12 18:09:24 +0530624
625 if(is_systemd_present) {
626 src = systemd_ffbm_mode;
627 if (have_cmdline) --dst;
628 while ((*dst++ = *src++));
629 }
630
Pavel Nedev898298c2013-02-27 12:36:09 -0800631 src = loglevel;
632 if (have_cmdline) --dst;
633 while ((*dst++ = *src++));
Matthew Qind886f3c2014-01-17 16:52:01 +0800634 } else if (boot_reason_alarm) {
635 src = alarmboot_cmdline;
636 if (have_cmdline) --dst;
637 while ((*dst++ = *src++));
Pavel Nedev328ac822013-04-05 15:25:11 +0300638 } else if (pause_at_bootup) {
David Ngf773dde2010-07-26 19:55:08 -0700639 src = battchg_pause;
640 if (have_cmdline) --dst;
David Ng183a7422009-12-07 14:55:21 -0800641 while ((*dst++ = *src++));
642 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800643
Shashank Mittalcd98d472011-08-02 14:29:24 -0700644 if(target_use_signed_kernel() && auth_kernel_img) {
645 src = auth_kernel;
646 if (have_cmdline) --dst;
647 while ((*dst++ = *src++));
648 }
649
Ajay Dudanid04110c2011-01-17 23:55:07 -0800650 switch(target_baseband())
651 {
652 case BASEBAND_APQ:
653 src = baseband_apq;
654 if (have_cmdline) --dst;
655 while ((*dst++ = *src++));
656 break;
657
658 case BASEBAND_MSM:
659 src = baseband_msm;
660 if (have_cmdline) --dst;
661 while ((*dst++ = *src++));
662 break;
663
664 case BASEBAND_CSFB:
665 src = baseband_csfb;
666 if (have_cmdline) --dst;
667 while ((*dst++ = *src++));
668 break;
669
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800670 case BASEBAND_SVLTE2A:
671 src = baseband_svlte2a;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800672 if (have_cmdline) --dst;
673 while ((*dst++ = *src++));
674 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700675
676 case BASEBAND_MDM:
677 src = baseband_mdm;
678 if (have_cmdline) --dst;
679 while ((*dst++ = *src++));
680 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700681
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800682 case BASEBAND_MDM2:
683 src = baseband_mdm2;
684 if (have_cmdline) --dst;
685 while ((*dst++ = *src++));
686 break;
687
Amol Jadi5c61a952012-05-04 17:05:35 -0700688 case BASEBAND_SGLTE:
689 src = baseband_sglte;
690 if (have_cmdline) --dst;
691 while ((*dst++ = *src++));
692 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530693
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800694 case BASEBAND_SGLTE2:
695 src = baseband_sglte2;
696 if (have_cmdline) --dst;
697 while ((*dst++ = *src++));
698 break;
699
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530700 case BASEBAND_DSDA:
701 src = baseband_dsda;
702 if (have_cmdline) --dst;
703 while ((*dst++ = *src++));
704 break;
Amol Jadi2a15a272013-01-22 12:03:36 -0800705
706 case BASEBAND_DSDA2:
707 src = baseband_dsda2;
708 if (have_cmdline) --dst;
709 while ((*dst++ = *src++));
710 break;
Vijay Kumar Pendotib228cfc2016-06-13 20:15:23 +0530711 case BASEBAND_APQ_NOWGR:
712 src = baseband_apq_nowgr;
713 if (have_cmdline) --dst;
714 while ((*dst++ = *src++));
715 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800716 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700717
718 if (strlen(display_panel_buf)) {
Dhaval Patel223ec952013-07-18 14:49:44 -0700719 src = display_panel_buf;
720 if (have_cmdline) --dst;
721 while ((*dst++ = *src++));
722 }
Joonwoo Park61112782013-10-02 19:50:39 -0700723
724 if (have_target_boot_params) {
725 if (have_cmdline) --dst;
726 src = target_boot_params;
727 while ((*dst++ = *src++));
vijay kumar870515d2015-08-31 16:37:24 +0530728 free(target_boot_params);
Joonwoo Park61112782013-10-02 19:50:39 -0700729 }
Channagoud Kadabi85c7ec32016-01-28 23:09:21 -0800730
Mayank Grover351a75e2017-05-30 20:06:08 +0530731 if (partition_multislot_is_supported() && have_cmdline)
732 {
733 src = androidboot_slot_suffix;
734 --dst;
735 while ((*dst++ = *src++));
736 --dst;
737 src = SUFFIX_SLOT(current_active_slot);
738 while ((*dst++ = *src++));
739
740 if (!boot_into_recovery)
741 {
742 src = skip_ramfs;
743 --dst;
744 while ((*dst++ = *src++));
745 }
746
747 src = sys_path_cmdline;
748 --dst;
749 while ((*dst++ = *src++));
Mayank Grover3804be72017-06-22 11:59:23 +0530750
751 src = syspath_buf;
752 --dst;
753 while ((*dst++ = *src++));
Mayank Grover351a75e2017-05-30 20:06:08 +0530754 }
755
Channagoud Kadabi85c7ec32016-01-28 23:09:21 -0800756#if TARGET_CMDLINE_SUPPORT
757 if (target_cmdline_buf && target_cmd_line_len)
758 {
759 if (have_cmdline) --dst;
760 src = target_cmdline_buf;
761 while((*dst++ = *src++));
762 free(target_cmdline_buf);
763 }
764#endif
Neeti Desaie245d492012-06-01 12:52:13 -0700765 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700766
767
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700768 if (boot_dev_buf)
769 free(boot_dev_buf);
770
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -0800771 if (cmdline_final)
772 dprintf(INFO, "cmdline: %s\n", cmdline_final);
773 else
774 dprintf(INFO, "cmdline is NULL\n");
Neeti Desaie245d492012-06-01 12:52:13 -0700775 return cmdline_final;
776}
777
778unsigned *atag_core(unsigned *ptr)
779{
780 /* CORE */
781 *ptr++ = 2;
782 *ptr++ = 0x54410001;
783
784 return ptr;
785
786}
787
788unsigned *atag_ramdisk(unsigned *ptr, void *ramdisk,
789 unsigned ramdisk_size)
790{
791 if (ramdisk_size) {
792 *ptr++ = 4;
793 *ptr++ = 0x54420005;
794 *ptr++ = (unsigned)ramdisk;
795 *ptr++ = ramdisk_size;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800796 }
797
Neeti Desaie245d492012-06-01 12:52:13 -0700798 return ptr;
799}
800
801unsigned *atag_ptable(unsigned **ptr_addr)
802{
803 int i;
804 struct ptable *ptable;
805
806 if ((ptable = flash_get_ptable()) && (ptable->count != 0)) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700807 *(*ptr_addr)++ = 2 + (ptable->count * (sizeof(struct atag_ptbl_entry) /
808 sizeof(unsigned)));
Neeti Desaie245d492012-06-01 12:52:13 -0700809 *(*ptr_addr)++ = 0x4d534d70;
810 for (i = 0; i < ptable->count; ++i)
811 ptentry_to_tag(ptr_addr, ptable_get(ptable, i));
812 }
813
814 return (*ptr_addr);
815}
816
817unsigned *atag_cmdline(unsigned *ptr, const char *cmdline)
818{
819 int cmdline_length = 0;
820 int n;
Neeti Desaie245d492012-06-01 12:52:13 -0700821 char *dest;
822
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800823 cmdline_length = strlen((const char*)cmdline);
Neeti Desaie245d492012-06-01 12:52:13 -0700824 n = (cmdline_length + 4) & (~3);
825
826 *ptr++ = (n / 4) + 2;
827 *ptr++ = 0x54410009;
828 dest = (char *) ptr;
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800829 while ((*dest++ = *cmdline++));
Neeti Desaie245d492012-06-01 12:52:13 -0700830 ptr += (n / 4);
831
832 return ptr;
833}
834
835unsigned *atag_end(unsigned *ptr)
836{
Brian Swetland9c4c0752009-01-25 16:23:50 -0800837 /* END */
838 *ptr++ = 0;
839 *ptr++ = 0;
840
Neeti Desaie245d492012-06-01 12:52:13 -0700841 return ptr;
842}
843
844void generate_atags(unsigned *ptr, const char *cmdline,
845 void *ramdisk, unsigned ramdisk_size)
846{
vijay kumar21a37452015-12-29 16:23:21 +0530847 unsigned *orig_ptr = ptr;
Neeti Desaie245d492012-06-01 12:52:13 -0700848 ptr = atag_core(ptr);
849 ptr = atag_ramdisk(ptr, ramdisk, ramdisk_size);
850 ptr = target_atag_mem(ptr);
851
852 /* Skip NAND partition ATAGS for eMMC boot */
853 if (!target_is_emmc_boot()){
854 ptr = atag_ptable(&ptr);
855 }
856
vijay kumar21a37452015-12-29 16:23:21 +0530857 /*
858 * Atags size filled till + cmdline size + 1 unsigned for 4-byte boundary + 4 unsigned
859 * for atag identifier in atag_cmdline and atag_end should be with in MAX_TAGS_SIZE bytes
860 */
861 if (((ptr - orig_ptr) + strlen(cmdline) + 5 * sizeof(unsigned)) < MAX_TAGS_SIZE) {
862 ptr = atag_cmdline(ptr, cmdline);
863 ptr = atag_end(ptr);
864 }
865 else {
866 dprintf(CRITICAL,"Crossing ATAGs Max size allowed\n");
867 ASSERT(0);
868 }
Neeti Desaie245d492012-06-01 12:52:13 -0700869}
870
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700871typedef void entry_func_ptr(unsigned, unsigned, unsigned*);
Neeti Desaie245d492012-06-01 12:52:13 -0700872void boot_linux(void *kernel, unsigned *tags,
873 const char *cmdline, unsigned machtype,
874 void *ramdisk, unsigned ramdisk_size)
875{
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800876 unsigned char *final_cmdline;
Amol Jadib6be5c12012-11-14 13:39:51 -0800877#if DEVICE_TREE
Neeti Desai17379b82012-06-04 18:42:53 -0700878 int ret = 0;
Amol Jadib6be5c12012-11-14 13:39:51 -0800879#endif
880
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700881 void (*entry)(unsigned, unsigned, unsigned*) = (entry_func_ptr*)(PA((addr_t)kernel));
Deepa Dinamani25a9f762012-11-30 15:57:15 -0800882 uint32_t tags_phys = PA((addr_t)tags);
vijay kumar1a50a642015-11-16 12:41:15 +0530883 struct kernel64_hdr *kptr = ((struct kernel64_hdr*)(PA((addr_t)kernel)));
Deepa Dinamani25a9f762012-11-30 15:57:15 -0800884
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530885 ramdisk = (void *)PA((addr_t)ramdisk);
Neeti Desaie245d492012-06-01 12:52:13 -0700886
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800887 final_cmdline = update_cmdline((const char*)cmdline);
888
Neeti Desai17379b82012-06-04 18:42:53 -0700889#if DEVICE_TREE
Amol Jadib6be5c12012-11-14 13:39:51 -0800890 dprintf(INFO, "Updating device tree: start\n");
891
Neeti Desai17379b82012-06-04 18:42:53 -0700892 /* Update the Device Tree */
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530893 ret = update_device_tree((void *)tags,(const char *)final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -0700894 if(ret)
895 {
896 dprintf(CRITICAL, "ERROR: Updating Device Tree Failed \n");
897 ASSERT(0);
898 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800899 dprintf(INFO, "Updating device tree: done\n");
Neeti Desai17379b82012-06-04 18:42:53 -0700900#else
Neeti Desaie245d492012-06-01 12:52:13 -0700901 /* Generating the Atags */
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800902 generate_atags(tags, final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -0700903#endif
Neeti Desaie245d492012-06-01 12:52:13 -0700904
Maria Yu52254c02014-07-04 16:14:54 +0800905 free(final_cmdline);
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -0700906
907#if VERIFIED_BOOT
Mayank Grover912eaa62017-10-26 12:08:53 +0530908 if (VB_M <= target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +0530909 {
910 if (device.verity_mode == 0) {
lijuangbdd9bb42016-03-01 18:22:17 +0800911#if FBCON_DISPLAY_MSG
Channagoud Kadabi86b0c112016-03-16 19:23:16 -0700912#if ENABLE_VB_ATTEST
Mayank Grover889be1b2017-09-12 20:12:23 +0530913 display_bootverify_menu(DISPLAY_MENU_EIO);
914 wait_for_users_action();
915 if(!pwr_key_is_pressed)
916 shutdown_device();
Channagoud Kadabi86b0c112016-03-16 19:23:16 -0700917#else
Mayank Grover889be1b2017-09-12 20:12:23 +0530918 display_bootverify_menu(DISPLAY_MENU_LOGGING);
Channagoud Kadabi86b0c112016-03-16 19:23:16 -0700919#endif
Mayank Grover889be1b2017-09-12 20:12:23 +0530920 wait_for_users_action();
lijuangbdd9bb42016-03-01 18:22:17 +0800921#else
Mayank Grover889be1b2017-09-12 20:12:23 +0530922 dprintf(CRITICAL,
923 "The dm-verity is not started in enforcing mode.\nWait for 5 seconds before proceeding\n");
924 mdelay(5000);
lijuangbdd9bb42016-03-01 18:22:17 +0800925#endif
Mayank Grover889be1b2017-09-12 20:12:23 +0530926 }
lijuangbdd9bb42016-03-01 18:22:17 +0800927 }
lijuangbdd9bb42016-03-01 18:22:17 +0800928#endif
929
930#if VERIFIED_BOOT
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -0700931 /* Write protect the device info */
Channagoud Kadabi3bd9d1e2015-05-05 16:18:20 -0700932 if (!boot_into_recovery && target_build_variant_user() && devinfo_present && mmc_write_protect("devinfo", 1))
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -0700933 {
934 dprintf(INFO, "Failed to write protect dev info\n");
935 ASSERT(0);
936 }
937#endif
938
Aravind Venkateswaran8f076242014-02-25 16:25:30 -0800939 /* Turn off splash screen if enabled */
940#if DISPLAY_SPLASH_SCREEN
941 target_display_shutdown();
942#endif
943
Veera Sundaram Sankaran67ea0932015-09-25 10:09:30 -0700944 /* Perform target specific cleanup */
945 target_uninit();
Aravind Venkateswaran8f076242014-02-25 16:25:30 -0800946
Deepa Dinamani33734bc2013-03-06 12:16:06 -0800947 dprintf(INFO, "booting linux @ %p, ramdisk @ %p (%d), tags/device tree @ %p\n",
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530948 entry, ramdisk, ramdisk_size, (void *)tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800949
950 enter_critical_section();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700951
Amol Jadi4421e652011-06-16 15:00:48 -0700952 /* do any platform specific cleanup before kernel entry */
953 platform_uninit();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700954
Brian Swetland9c4c0752009-01-25 16:23:50 -0800955 arch_disable_cache(UCACHE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700956
Amol Jadi504f9fe2012-08-16 13:56:48 -0700957#if ARM_WITH_MMU
Brian Swetland9c4c0752009-01-25 16:23:50 -0800958 arch_disable_mmu();
Amol Jadi504f9fe2012-08-16 13:56:48 -0700959#endif
Amol Jadi492d5a52013-03-15 16:12:34 -0700960 bs_set_timestamp(BS_KERNEL_ENTRY);
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800961
962 if (IS_ARM64(kptr))
963 /* Jump to a 64bit kernel */
964 scm_elexec_call((paddr_t)kernel, tags_phys);
965 else
966 /* Jump to a 32bit kernel */
967 entry(0, machtype, (unsigned*)tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800968}
969
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700970/* Function to check if the memory address range falls within the aboot
971 * boundaries.
972 * start: Start of the memory region
973 * size: Size of the memory region
974 */
Vijay Kumar Pendoti9c002ad2016-03-09 13:52:45 +0530975int check_aboot_addr_range_overlap(uintptr_t start, uint32_t size)
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700976{
977 /* Check for boundary conditions. */
Sundarajan Srinivasance2a0ea2013-12-16 17:02:56 -0800978 if ((UINT_MAX - start) < size)
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700979 return -1;
980
981 /* Check for memory overlap. */
982 if ((start < MEMBASE) && ((start + size) <= MEMBASE))
983 return 0;
Channagoud Kadabi94143912013-10-15 12:53:52 -0700984 else if (start >= (MEMBASE + MEMSIZE))
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700985 return 0;
986 else
987 return -1;
988}
989
Mayank Grovere559cec2017-10-17 15:12:03 +0530990/* Function to check if the memory address range falls beyond ddr region.
991 * start: Start of the memory region
992 * size: Size of the memory region
993 */
994int check_ddr_addr_range_bound(uintptr_t start, uint32_t size)
995{
996 uintptr_t ddr_pa_start_addr = PA(get_ddr_start());
997 uint64_t ddr_size = smem_get_ddr_size();
998 uint64_t ddr_pa_end_addr = ddr_pa_start_addr + ddr_size;
999 uintptr_t pa_start_addr = PA(start);
1000
1001 /* Check for boundary conditions. */
1002 if ((UINT_MAX - start) < size)
1003 return -1;
1004
1005 /* Check if memory range is beyond the ddr range. */
1006 if (pa_start_addr < ddr_pa_start_addr ||
1007 pa_start_addr >= (ddr_pa_end_addr) ||
1008 (pa_start_addr + size) > ddr_pa_end_addr)
1009 return -1;
1010 else
1011 return 0;
1012}
1013
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001014BUF_DMA_ALIGN(buf, BOOT_IMG_MAX_PAGE_SIZE); //Equal to max-supported pagesize
Dima Zavin214cc642009-01-26 11:16:21 -08001015
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001016static void verify_signed_bootimg(uint32_t bootimg_addr, uint32_t bootimg_size)
1017{
1018 int ret;
Channagoud Kadabia8c623f2015-01-13 14:48:48 -08001019
1020#if !VERIFIED_BOOT
Sundarajan Srinivasance54d6e2013-11-11 12:45:00 -08001021#if IMAGE_VERIF_ALGO_SHA1
1022 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA1;
1023#else
1024 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA256;
1025#endif
Channagoud Kadabia8c623f2015-01-13 14:48:48 -08001026#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001027
1028 /* Assume device is rooted at this time. */
1029 device.is_tampered = 1;
1030
1031 dprintf(INFO, "Authenticating boot image (%d): start\n", bootimg_size);
1032
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001033#if VERIFIED_BOOT
Mayank Grover68fbf0d2017-10-24 14:13:39 +05301034 char *ptn_name = NULL;
1035 if (boot_into_recovery &&
1036 (!partition_multislot_is_supported()))
1037 ptn_name = "/recovery";
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001038 else
Mayank Grover68fbf0d2017-10-24 14:13:39 +05301039 ptn_name = "/boot";
1040
1041 ret = boot_verify_image((unsigned char *)bootimg_addr,
1042 bootimg_size, ptn_name);
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001043 boot_verify_print_state();
1044#else
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001045 ret = image_verify((unsigned char *)bootimg_addr,
1046 (unsigned char *)(bootimg_addr + bootimg_size),
1047 bootimg_size,
Sundarajan Srinivasance54d6e2013-11-11 12:45:00 -08001048 auth_algo);
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001049#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001050 dprintf(INFO, "Authenticating boot image: done return value = %d\n", ret);
1051
1052 if (ret)
1053 {
1054 /* Authorized kernel */
1055 device.is_tampered = 0;
Sundarajan Srinivasan3fb21f12013-09-16 18:36:15 -07001056 auth_kernel_img = 1;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001057 }
1058
Amit Blay4aa292f2015-04-28 21:55:59 +03001059#ifdef MDTP_SUPPORT
1060 {
1061 /* Verify MDTP lock.
1062 * For boot & recovery partitions, use aboot's verification result.
1063 */
1064 mdtp_ext_partition_verification_t ext_partition;
1065 ext_partition.partition = boot_into_recovery ? MDTP_PARTITION_RECOVERY : MDTP_PARTITION_BOOT;
1066 ext_partition.integrity_state = device.is_tampered ? MDTP_PARTITION_STATE_INVALID : MDTP_PARTITION_STATE_VALID;
1067 ext_partition.page_size = 0; /* Not needed since already validated */
1068 ext_partition.image_addr = 0; /* Not needed since already validated */
1069 ext_partition.image_size = 0; /* Not needed since already validated */
1070 ext_partition.sig_avail = FALSE; /* Not needed since already validated */
1071 mdtp_fwlock_verify_lock(&ext_partition);
1072 }
1073#endif /* MDTP_SUPPORT */
1074
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001075#if USE_PCOM_SECBOOT
1076 set_tamper_flag(device.is_tampered);
1077#endif
1078
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001079#if VERIFIED_BOOT
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001080 switch(boot_verify_get_state())
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001081 {
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001082 case RED:
lijuanga40d6302015-07-20 20:10:13 +08001083#if FBCON_DISPLAY_MSG
lijuang9a7d3b92015-11-30 14:41:24 +08001084 display_bootverify_menu(DISPLAY_MENU_RED);
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07001085#if ENABLE_VB_ATTEST
1086 mdelay(DELAY_WAIT);
1087 shutdown_device();
1088#else
lijuanga40d6302015-07-20 20:10:13 +08001089 wait_for_users_action();
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07001090#endif
lijuanga40d6302015-07-20 20:10:13 +08001091#else
1092 dprintf(CRITICAL,
1093 "Your device has failed verification and may not work properly.\nWait for 5 seconds before proceeding\n");
1094 mdelay(5000);
1095#endif
1096
1097 break;
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001098 case YELLOW:
lijuanga40d6302015-07-20 20:10:13 +08001099#if FBCON_DISPLAY_MSG
lijuang9a7d3b92015-11-30 14:41:24 +08001100 display_bootverify_menu(DISPLAY_MENU_YELLOW);
lijuanga40d6302015-07-20 20:10:13 +08001101 wait_for_users_action();
1102#else
1103 dprintf(CRITICAL,
1104 "Your device has loaded a different operating system.\nWait for 5 seconds before proceeding\n");
1105 mdelay(5000);
1106#endif
1107 break;
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001108 default:
lijuanga40d6302015-07-20 20:10:13 +08001109 break;
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001110 }
1111#endif
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001112#if !VERIFIED_BOOT
Unnati Gandhi1be04752015-03-27 19:41:53 +05301113 if(device.is_tampered)
1114 {
1115 write_device_info_mmc(&device);
1116 #ifdef TZ_TAMPER_FUSE
1117 set_tamper_fuse_cmd();
1118 #endif
1119 #ifdef ASSERT_ON_TAMPER
1120 dprintf(CRITICAL, "Device is tampered. Asserting..\n");
1121 ASSERT(0);
1122 #endif
1123 }
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001124#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001125}
1126
Maunik Shah0f3c8ac2014-03-02 17:47:58 +05301127static bool check_format_bit()
1128{
1129 bool ret = false;
1130 int index;
1131 uint64_t offset;
1132 struct boot_selection_info *in = NULL;
1133 char *buf = NULL;
1134
1135 index = partition_get_index("bootselect");
1136 if (index == INVALID_PTN)
1137 {
1138 dprintf(INFO, "Unable to locate /bootselect partition\n");
1139 return ret;
1140 }
1141 offset = partition_get_offset(index);
1142 if(!offset)
1143 {
1144 dprintf(INFO, "partition /bootselect doesn't exist\n");
1145 return ret;
1146 }
1147 buf = (char *) memalign(CACHE_LINE, ROUNDUP(page_size, CACHE_LINE));
Mayank Grover48860402016-11-29 12:34:53 +05301148 mmc_set_lun(partition_get_lun(index));
Maunik Shah0f3c8ac2014-03-02 17:47:58 +05301149 ASSERT(buf);
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301150 if (mmc_read(offset, (uint32_t *)buf, page_size))
Maunik Shah0f3c8ac2014-03-02 17:47:58 +05301151 {
1152 dprintf(INFO, "mmc read failure /bootselect %d\n", page_size);
1153 free(buf);
1154 return ret;
1155 }
1156 in = (struct boot_selection_info *) buf;
1157 if ((in->signature == BOOTSELECT_SIGNATURE) &&
1158 (in->version == BOOTSELECT_VERSION)) {
1159 if ((in->state_info & BOOTSELECT_FORMAT) &&
1160 !(in->state_info & BOOTSELECT_FACTORY))
1161 ret = true;
1162 } else {
1163 dprintf(CRITICAL, "Signature: 0x%08x or version: 0x%08x mismatched of /bootselect\n",
1164 in->signature, in->version);
1165 ASSERT(0);
1166 }
1167 free(buf);
1168 return ret;
1169}
1170
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001171void boot_verifier_init()
1172{
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001173 uint32_t boot_state;
1174 /* Check if device unlock */
1175 if(device.is_unlocked)
1176 {
1177 boot_verify_send_event(DEV_UNLOCK);
1178 boot_verify_print_state();
1179 dprintf(CRITICAL, "Device is unlocked! Skipping verification...\n");
1180 return;
1181 }
1182 else
1183 {
1184 boot_verify_send_event(BOOT_INIT);
1185 }
1186
1187 /* Initialize keystore */
1188 boot_state = boot_verify_keystore_init();
1189 if(boot_state == YELLOW)
1190 {
1191 boot_verify_print_state();
1192 dprintf(CRITICAL, "Keystore verification failed! Continuing anyways...\n");
1193 }
1194}
1195
Shashank Mittal23b8f422010-04-16 19:27:21 -07001196int boot_linux_from_mmc(void)
1197{
1198 struct boot_img_hdr *hdr = (void*) buf;
1199 struct boot_img_hdr *uhdr;
1200 unsigned offset = 0;
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07001201 int rcode;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001202 unsigned long long ptn = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001203 int index = INVALID_PTN;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001204
Shashank Mittalcd98d472011-08-02 14:29:24 -07001205 unsigned char *image_addr = 0;
1206 unsigned kernel_actual;
1207 unsigned ramdisk_actual;
1208 unsigned imagesize_actual;
Neeti Desai465491e2012-07-31 12:53:35 -07001209 unsigned second_actual = 0;
Neeti Desai465491e2012-07-31 12:53:35 -07001210
Matthew Qin271927e2015-03-31 22:07:07 -04001211 unsigned int dtb_size = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08001212 unsigned int out_len = 0;
1213 unsigned int out_avai_len = 0;
1214 unsigned char *out_addr = NULL;
1215 uint32_t dtb_offset = 0;
1216 unsigned char *kernel_start_addr = NULL;
1217 unsigned int kernel_size = 0;
Ameya Thakur10a33452016-06-13 14:24:26 -07001218 unsigned int patched_kernel_hdr_size = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08001219 int rc;
Mayank Groverc93cad82017-10-03 12:23:45 +05301220 char *ptn_name = NULL;
Neeti Desai465491e2012-07-31 12:53:35 -07001221#if DEVICE_TREE
1222 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -07001223 struct dt_entry dt_entry;
Neeti Desai465491e2012-07-31 12:53:35 -07001224 unsigned dt_table_offset;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001225 uint32_t dt_actual;
Deepa Dinamani19648b42013-09-05 17:05:55 -07001226 uint32_t dt_hdr_size;
Matthew Qin271927e2015-03-31 22:07:07 -04001227 unsigned char *best_match_dt_addr = NULL;
Neeti Desai465491e2012-07-31 12:53:35 -07001228#endif
Matthew Qin49e51fa2015-02-09 10:40:45 +08001229 struct kernel64_hdr *kptr = NULL;
Mayank Grover351a75e2017-05-30 20:06:08 +05301230 int current_active_slot = INVALID;
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001231
Maunik Shah0f3c8ac2014-03-02 17:47:58 +05301232 if (check_format_bit())
1233 boot_into_recovery = 1;
1234
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07001235 if (!boot_into_recovery) {
1236 memset(ffbm_mode_string, '\0', sizeof(ffbm_mode_string));
1237 rcode = get_ffbm(ffbm_mode_string, sizeof(ffbm_mode_string));
1238 if (rcode <= 0) {
1239 boot_into_ffbm = false;
1240 if (rcode < 0)
1241 dprintf(CRITICAL,"failed to get ffbm cookie");
1242 } else
1243 boot_into_ffbm = true;
1244 } else
1245 boot_into_ffbm = false;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001246 uhdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
1247 if (!memcmp(uhdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1248 dprintf(INFO, "Unified boot method!\n");
1249 hdr = uhdr;
1250 goto unified_boot;
1251 }
Mayank Groverc93cad82017-10-03 12:23:45 +05301252
1253 /* For a/b recovery image code is on boot partition.
1254 If we support multislot, always use boot partition. */
1255 if (boot_into_recovery &&
1256 (!partition_multislot_is_supported()))
1257 ptn_name = "recovery";
1258 else
1259 ptn_name = "boot";
1260
1261 index = partition_get_index(ptn_name);
1262 ptn = partition_get_offset(index);
1263 if(ptn == 0) {
1264 dprintf(CRITICAL, "ERROR: No %s partition found\n", ptn_name);
1265 return -1;
Kinson Chikf1a43512011-07-14 11:28:39 -07001266 }
Mayank Groverc93cad82017-10-03 12:23:45 +05301267
Channagoud Kadabief0547c2015-02-10 12:57:38 -08001268 /* Set Lun for boot & recovery partitions */
1269 mmc_set_lun(partition_get_lun(index));
Shashank Mittal23b8f422010-04-16 19:27:21 -07001270
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301271 if (mmc_read(ptn + offset, (uint32_t *) buf, page_size)) {
Shashank Mittal23b8f422010-04-16 19:27:21 -07001272 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
1273 return -1;
1274 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001275
1276 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001277 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Mayank Grover351a75e2017-05-30 20:06:08 +05301278 return ERR_INVALID_BOOT_MAGIC;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001279 }
1280
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07001281 if (hdr->page_size && (hdr->page_size != page_size)) {
vijay kumar2e21b3a2014-06-26 17:40:15 +05301282
1283 if (hdr->page_size > BOOT_IMG_MAX_PAGE_SIZE) {
1284 dprintf(CRITICAL, "ERROR: Invalid page size\n");
1285 return -1;
1286 }
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07001287 page_size = hdr->page_size;
1288 page_mask = page_size - 1;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001289 }
1290
Matthew Qin49e51fa2015-02-09 10:40:45 +08001291 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1292 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Kishor PKd8ddcad2017-07-27 13:53:57 +05301293 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001294
Matthew Qin49e51fa2015-02-09 10:40:45 +08001295 image_addr = (unsigned char *)target_get_scratch_address();
Kishor PK9e91b592017-05-24 12:14:55 +05301296 memcpy(image_addr, (void *)buf, page_size);
1297
1298 /* ensure commandline is terminated */
1299 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
Matthew Qin49e51fa2015-02-09 10:40:45 +08001300
1301#if DEVICE_TREE
Parth Dixit4097b622016-03-15 14:42:27 +05301302#ifndef OSVERSION_IN_BOOTIMAGE
1303 dt_size = hdr->dt_size;
1304#endif
1305 dt_actual = ROUND_TO_PAGE(dt_size, page_mask);
Kishor PKd8ddcad2017-07-27 13:53:57 +05301306 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 +05301307 dprintf(CRITICAL, "Integer overflow detected in bootimage header fields at %u in %s\n",__LINE__,__FILE__);
1308 return -1;
1309 }
Kishor PKd8ddcad2017-07-27 13:53:57 +05301310 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + second_actual + dt_actual);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001311#else
Kishor PKd8ddcad2017-07-27 13:53:57 +05301312 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 +05301313 dprintf(CRITICAL, "Integer overflow detected in bootimage header fields at %u in %s\n",__LINE__,__FILE__);
1314 return -1;
1315 }
Kishor PKd8ddcad2017-07-27 13:53:57 +05301316 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + second_actual);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001317#endif
1318
1319#if VERIFIED_BOOT
1320 boot_verifier_init();
1321#endif
1322
Vijay Kumar Pendoti9c002ad2016-03-09 13:52:45 +05301323 if (check_aboot_addr_range_overlap((uintptr_t) image_addr, imagesize_actual))
Matthew Qin49e51fa2015-02-09 10:40:45 +08001324 {
1325 dprintf(CRITICAL, "Boot image buffer address overlaps with aboot addresses.\n");
1326 return -1;
1327 }
1328
Matthew Qinbb7923d2015-02-09 10:56:09 +08001329 /*
1330 * Update loading flow of bootimage to support compressed/uncompressed
1331 * bootimage on both 64bit and 32bit platform.
1332 * 1. Load bootimage from emmc partition onto DDR.
1333 * 2. Check if bootimage is gzip format. If yes, decompress compressed kernel
1334 * 3. Check kernel header and update kernel load addr for 64bit and 32bit
1335 * platform accordingly.
1336 * 4. Sanity Check on kernel_addr and ramdisk_addr and copy data.
1337 */
Mayank Grover351a75e2017-05-30 20:06:08 +05301338 if (partition_multislot_is_supported())
1339 {
1340 current_active_slot = partition_find_active_slot();
1341 dprintf(INFO, "Loading boot image (%d) active_slot(%s): start\n",
1342 imagesize_actual, SUFFIX_SLOT(current_active_slot));
1343 }
1344 else
1345 {
1346 dprintf(INFO, "Loading (%s) image (%d): start\n",
1347 (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual);
1348 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08001349 bs_set_timestamp(BS_KERNEL_LOAD_START);
1350
Gaurav Nebhwani43e2a462016-03-17 21:32:56 +05301351 if ((target_get_max_flash_size() - page_size) < imagesize_actual)
1352 {
1353 dprintf(CRITICAL, "booimage size is greater than DDR can hold\n");
1354 return -1;
1355 }
Kishor PK9e91b592017-05-24 12:14:55 +05301356 offset = page_size;
1357 /* Read image without signature and header*/
1358 if (mmc_read(ptn + offset, (void *)(image_addr + offset), imagesize_actual - page_size))
Matthew Qin49e51fa2015-02-09 10:40:45 +08001359 {
1360 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
1361 return -1;
1362 }
1363
Mayank Grover351a75e2017-05-30 20:06:08 +05301364 if (partition_multislot_is_supported())
1365 {
1366 dprintf(INFO, "Loading boot image (%d) active_slot(%s): done\n",
1367 imagesize_actual, SUFFIX_SLOT(current_active_slot));
1368 }
1369 else
1370 {
1371 dprintf(INFO, "Loading (%s) image (%d): done\n",
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001372 (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual);
1373
Mayank Grover351a75e2017-05-30 20:06:08 +05301374 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08001375 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
1376
1377 /* Authenticate Kernel */
1378 dprintf(INFO, "use_signed_kernel=%d, is_unlocked=%d, is_tampered=%d.\n",
1379 (int) target_use_signed_kernel(),
1380 device.is_unlocked,
1381 device.is_tampered);
1382
Channagoud Kadabi736c4962015-08-21 11:56:52 -07001383 /* Change the condition a little bit to include the test framework support.
1384 * We would never reach this point if device is in fastboot mode, even if we did
1385 * that means we are in test mode, so execute kernel authentication part for the
1386 * tests */
Parth Dixitcb0c6082015-12-30 15:01:13 +05301387 if((target_use_signed_kernel() && (!device.is_unlocked)) || is_test_mode_enabled())
Matthew Qin49e51fa2015-02-09 10:40:45 +08001388 {
1389 offset = imagesize_actual;
Vijay Kumar Pendoti9c002ad2016-03-09 13:52:45 +05301390 if (check_aboot_addr_range_overlap((uintptr_t)image_addr + offset, page_size))
Matthew Qin49e51fa2015-02-09 10:40:45 +08001391 {
1392 dprintf(CRITICAL, "Signature read buffer address overlaps with aboot addresses.\n");
1393 return -1;
1394 }
1395
1396 /* Read signature */
1397 if(mmc_read(ptn + offset, (void *)(image_addr + offset), page_size))
1398 {
1399 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
1400 return -1;
1401 }
1402
1403 verify_signed_bootimg((uint32_t)image_addr, imagesize_actual);
Channagoud Kadabi736c4962015-08-21 11:56:52 -07001404 /* The purpose of our test is done here */
Parth Dixitcb0c6082015-12-30 15:01:13 +05301405 if(is_test_mode_enabled() && auth_kernel_img)
Channagoud Kadabi736c4962015-08-21 11:56:52 -07001406 return 0;
Matthew Qin49e51fa2015-02-09 10:40:45 +08001407 } else {
1408 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
1409 #ifdef TZ_SAVE_KERNEL_HASH
1410 aboot_save_boot_hash_mmc((uint32_t) image_addr, imagesize_actual);
1411 #endif /* TZ_SAVE_KERNEL_HASH */
Amit Blay4aa292f2015-04-28 21:55:59 +03001412
1413#ifdef MDTP_SUPPORT
1414 {
1415 /* Verify MDTP lock.
1416 * For boot & recovery partitions, MDTP will use boot_verifier APIs,
1417 * since verification was skipped in aboot. The signature is not part of the loaded image.
1418 */
1419 mdtp_ext_partition_verification_t ext_partition;
1420 ext_partition.partition = boot_into_recovery ? MDTP_PARTITION_RECOVERY : MDTP_PARTITION_BOOT;
1421 ext_partition.integrity_state = MDTP_PARTITION_STATE_UNSET;
1422 ext_partition.page_size = page_size;
1423 ext_partition.image_addr = (uint32)image_addr;
1424 ext_partition.image_size = imagesize_actual;
1425 ext_partition.sig_avail = FALSE;
1426 mdtp_fwlock_verify_lock(&ext_partition);
1427 }
1428#endif /* MDTP_SUPPORT */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001429 }
1430
Sridhar Parasuram00bfedb2015-05-26 14:21:27 -07001431#if VERIFIED_BOOT
taozhang93088bd2016-11-16 15:50:46 +08001432 if((boot_verify_get_state() == ORANGE) && (!boot_into_ffbm))
Reut Zysmanba8a9d52016-02-18 11:44:04 +02001433 {
1434#if FBCON_DISPLAY_MSG
1435 display_bootverify_menu(DISPLAY_MENU_ORANGE);
1436 wait_for_users_action();
1437#else
1438 dprintf(CRITICAL,
1439 "Your device has been unlocked and can't be trusted.\nWait for 5 seconds before proceeding\n");
1440 mdelay(5000);
1441#endif
1442 }
1443#endif
1444
1445#if VERIFIED_BOOT
Mayank Grover912eaa62017-10-26 12:08:53 +05301446 if (VB_M <= target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +05301447 {
1448 /* set boot and system versions. */
1449 set_os_version((unsigned char *)image_addr);
1450 // send root of trust
1451 if(!send_rot_command((uint32_t)device.is_unlocked))
1452 ASSERT(0);
1453 }
Parth Dixitbc9b6492015-10-18 00:41:38 +05301454#endif
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001455 /*
Matthew Qinbb7923d2015-02-09 10:56:09 +08001456 * Check if the kernel image is a gzip package. If yes, need to decompress it.
1457 * If not, continue booting.
1458 */
1459 if (is_gzip_package((unsigned char *)(image_addr + page_size), hdr->kernel_size))
1460 {
1461 out_addr = (unsigned char *)(image_addr + imagesize_actual + page_size);
1462 out_avai_len = target_get_max_flash_size() - imagesize_actual - page_size;
Matthew Qin0b15b322015-05-19 05:20:54 -04001463 dprintf(INFO, "decompressing kernel image: start\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001464 rc = decompress((unsigned char *)(image_addr + page_size),
1465 hdr->kernel_size, out_addr, out_avai_len,
1466 &dtb_offset, &out_len);
1467 if (rc)
1468 {
Matthew Qin0b15b322015-05-19 05:20:54 -04001469 dprintf(CRITICAL, "decompressing kernel image failed!!!\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001470 ASSERT(0);
1471 }
1472
Matthew Qin0b15b322015-05-19 05:20:54 -04001473 dprintf(INFO, "decompressing kernel image: done\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001474 kptr = (struct kernel64_hdr *)out_addr;
1475 kernel_start_addr = out_addr;
1476 kernel_size = out_len;
1477 } else {
Ameya Thakur10a33452016-06-13 14:24:26 -07001478 dprintf(INFO, "Uncpmpressed kernel in use\n");
1479 if (!strncmp((char*)(image_addr + page_size),
1480 PATCHED_KERNEL_MAGIC,
1481 sizeof(PATCHED_KERNEL_MAGIC) - 1)) {
1482 dprintf(INFO, "Patched kernel detected\n");
1483 kptr = (struct kernel64_hdr *)(image_addr + page_size +
1484 PATCHED_KERNEL_HEADER_SIZE);
1485 //The size of the kernel is stored at start of kernel image + 16
1486 //The dtb would start just after the kernel
1487 dtb_offset = *((uint32_t*)((unsigned char*)
1488 (image_addr + page_size +
1489 sizeof(PATCHED_KERNEL_MAGIC) -
1490 1)));
1491 //The actual kernel starts after the 20 byte header.
1492 kernel_start_addr = (unsigned char*)(image_addr +
1493 page_size + PATCHED_KERNEL_HEADER_SIZE);
1494 kernel_size = hdr->kernel_size;
1495 patched_kernel_hdr_size = PATCHED_KERNEL_HEADER_SIZE;
1496 } else {
1497 dprintf(INFO, "Kernel image not patched..Unable to locate dt offset\n");
1498 kptr = (struct kernel64_hdr *)(image_addr + page_size);
1499 kernel_start_addr = (unsigned char *)(image_addr + page_size);
1500 kernel_size = hdr->kernel_size;
1501 }
Matthew Qinbb7923d2015-02-09 10:56:09 +08001502 }
1503
1504 /*
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001505 * Update the kernel/ramdisk/tags address if the boot image header
1506 * has default values, these default values come from mkbootimg when
1507 * the boot image is flashed using fastboot flash:raw
1508 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001509 update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr));
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001510
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001511 /* Get virtual addresses since the hdr saves physical addresses. */
1512 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
1513 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
1514 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001515
Matthew Qinbb7923d2015-02-09 10:56:09 +08001516 kernel_size = ROUND_TO_PAGE(kernel_size, page_mask);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001517 /* Check if the addresses in the header are valid. */
Matthew Qinbb7923d2015-02-09 10:56:09 +08001518 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_size) ||
Mayank Grovere559cec2017-10-17 15:12:03 +05301519 check_ddr_addr_range_bound(hdr->kernel_addr, kernel_size) ||
1520 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual) ||
1521 check_ddr_addr_range_bound(hdr->ramdisk_addr, ramdisk_actual))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001522 {
Mayank Grovere559cec2017-10-17 15:12:03 +05301523 dprintf(CRITICAL, "kernel/ramdisk addresses are not valid.\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001524 return -1;
1525 }
1526
1527#ifndef DEVICE_TREE
Mayank Grovere559cec2017-10-17 15:12:03 +05301528 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE) ||
1529 check_ddr_addr_range_bound(hdr->tags_addr, MAX_TAGS_SIZE))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001530 {
Mayank Grovere559cec2017-10-17 15:12:03 +05301531 dprintf(CRITICAL, "Tags addresses are not valid.\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001532 return -1;
1533 }
1534#endif
1535
Matthew Qin49e51fa2015-02-09 10:40:45 +08001536 /* Move kernel, ramdisk and device tree to correct address */
Matthew Qinbb7923d2015-02-09 10:56:09 +08001537 memmove((void*) hdr->kernel_addr, kernel_start_addr, kernel_size);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001538 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001539
Matthew Qin49e51fa2015-02-09 10:40:45 +08001540 #if DEVICE_TREE
Parth Dixit4097b622016-03-15 14:42:27 +05301541 if(dt_size) {
Matthew Qin49e51fa2015-02-09 10:40:45 +08001542 dt_table_offset = ((uint32_t)image_addr + page_size + kernel_actual + ramdisk_actual + second_actual);
1543 table = (struct dt_table*) dt_table_offset;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001544
Matthew Qin49e51fa2015-02-09 10:40:45 +08001545 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
1546 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1547 return -1;
1548 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001549
P.V. Phani Kumar82916762016-03-09 09:20:19 +05301550 /* Its Error if, dt_hdr_size (table->num_entries * dt_entry size + Dev_Tree Header)
1551 goes beyound hdr->dt_size*/
Parth Dixit4097b622016-03-15 14:42:27 +05301552 if (dt_hdr_size > ROUND_TO_PAGE(dt_size,hdr->page_size)) {
P.V. Phani Kumar82916762016-03-09 09:20:19 +05301553 dprintf(CRITICAL, "ERROR: Invalid Device Tree size \n");
1554 return -1;
1555 }
1556
Matthew Qin49e51fa2015-02-09 10:40:45 +08001557 /* Find index of device tree within device tree table */
1558 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
1559 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1560 return -1;
1561 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001562
P.V. Phani Kumar3c333202016-03-09 11:54:37 +05301563 if(dt_entry.offset > (UINT_MAX - dt_entry.size)) {
1564 dprintf(CRITICAL, "ERROR: Device tree contents are Invalid\n");
1565 return -1;
1566 }
1567
1568 /* Ensure we are not overshooting dt_size with the dt_entry selected */
Parth Dixit4097b622016-03-15 14:42:27 +05301569 if ((dt_entry.offset + dt_entry.size) > dt_size) {
P.V. Phani Kumar3c333202016-03-09 11:54:37 +05301570 dprintf(CRITICAL, "ERROR: Device tree contents are Invalid\n");
1571 return -1;
1572 }
1573
Matthew Qin271927e2015-03-31 22:07:07 -04001574 if (is_gzip_package((unsigned char *)dt_table_offset + dt_entry.offset, dt_entry.size))
1575 {
1576 unsigned int compressed_size = 0;
1577 out_addr += out_len;
1578 out_avai_len -= out_len;
Matthew Qin0b15b322015-05-19 05:20:54 -04001579 dprintf(INFO, "decompressing dtb: start\n");
Matthew Qin271927e2015-03-31 22:07:07 -04001580 rc = decompress((unsigned char *)dt_table_offset + dt_entry.offset,
1581 dt_entry.size, out_addr, out_avai_len,
1582 &compressed_size, &dtb_size);
1583 if (rc)
1584 {
Matthew Qin0b15b322015-05-19 05:20:54 -04001585 dprintf(CRITICAL, "decompressing dtb failed!!!\n");
Matthew Qin271927e2015-03-31 22:07:07 -04001586 ASSERT(0);
1587 }
1588
Matthew Qin0b15b322015-05-19 05:20:54 -04001589 dprintf(INFO, "decompressing dtb: done\n");
Matthew Qin271927e2015-03-31 22:07:07 -04001590 best_match_dt_addr = out_addr;
1591 } else {
1592 best_match_dt_addr = (unsigned char *)dt_table_offset + dt_entry.offset;
1593 dtb_size = dt_entry.size;
1594 }
1595
Matthew Qin49e51fa2015-02-09 10:40:45 +08001596 /* Validate and Read device device tree in the tags_addr */
Mayank Grovere559cec2017-10-17 15:12:03 +05301597 if (check_aboot_addr_range_overlap(hdr->tags_addr, dtb_size) ||
1598 check_ddr_addr_range_bound(hdr->tags_addr, dtb_size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001599 {
Mayank Grovere559cec2017-10-17 15:12:03 +05301600 dprintf(CRITICAL, "Device tree addresses are not valid\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001601 return -1;
1602 }
Shashank Mittal162244e2011-08-08 19:01:25 -07001603
Matthew Qin271927e2015-03-31 22:07:07 -04001604 memmove((void *)hdr->tags_addr, (char *)best_match_dt_addr, dtb_size);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001605 } else {
1606 /* Validate the tags_addr */
Mayank Grovere559cec2017-10-17 15:12:03 +05301607 if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual) ||
1608 check_ddr_addr_range_bound(hdr->tags_addr, kernel_actual))
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001609 {
Mayank Grovere559cec2017-10-17 15:12:03 +05301610 dprintf(CRITICAL, "Device tree addresses are not valid.\n");
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001611 return -1;
1612 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08001613 /*
1614 * If appended dev tree is found, update the atags with
1615 * memory address to the DTB appended location on RAM.
1616 * Else update with the atags address in the kernel header
1617 */
1618 void *dtb;
Ameya Thakur10a33452016-06-13 14:24:26 -07001619 dtb = dev_tree_appended(
1620 (void*)(image_addr + page_size +
1621 patched_kernel_hdr_size),
1622 hdr->kernel_size, dtb_offset,
1623 (void *)hdr->tags_addr);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001624 if (!dtb) {
1625 dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001626 return -1;
1627 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07001628 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08001629 #endif
Shashank Mittal23b8f422010-04-16 19:27:21 -07001630
Stanimir Varbanov69ec5462013-07-18 18:17:42 +03001631 if (boot_into_recovery && !device.is_unlocked && !device.is_tampered)
1632 target_load_ssd_keystore();
1633
Shashank Mittal23b8f422010-04-16 19:27:21 -07001634unified_boot:
Shashank Mittal23b8f422010-04-16 19:27:21 -07001635
Dima Zavin77e41f32013-03-06 16:10:43 -08001636 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07001637 (const char *)hdr->cmdline, board_machtype(),
Shashank Mittal23b8f422010-04-16 19:27:21 -07001638 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
1639
1640 return 0;
1641}
1642
Dima Zavin214cc642009-01-26 11:16:21 -08001643int boot_linux_from_flash(void)
1644{
1645 struct boot_img_hdr *hdr = (void*) buf;
Dima Zavin214cc642009-01-26 11:16:21 -08001646 struct ptentry *ptn;
1647 struct ptable *ptable;
1648 unsigned offset = 0;
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001649
Shashank Mittalcd98d472011-08-02 14:29:24 -07001650 unsigned char *image_addr = 0;
1651 unsigned kernel_actual;
1652 unsigned ramdisk_actual;
1653 unsigned imagesize_actual;
vijay kumar8f53e362015-11-24 13:38:11 +05301654 unsigned second_actual = 0;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001655
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001656#if DEVICE_TREE
1657 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -07001658 struct dt_entry dt_entry;
vijay kumar8f53e362015-11-24 13:38:11 +05301659 unsigned dt_table_offset;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001660 uint32_t dt_actual;
Deepa Dinamani19648b42013-09-05 17:05:55 -07001661 uint32_t dt_hdr_size;
vijay kumar8f53e362015-11-24 13:38:11 +05301662 unsigned int dtb_size = 0;
1663 unsigned char *best_match_dt_addr = NULL;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001664#endif
1665
David Ng183a7422009-12-07 14:55:21 -08001666 if (target_is_emmc_boot()) {
1667 hdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
1668 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1669 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
1670 return -1;
1671 }
1672 goto continue_boot;
1673 }
1674
Dima Zavin214cc642009-01-26 11:16:21 -08001675 ptable = flash_get_ptable();
1676 if (ptable == NULL) {
1677 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1678 return -1;
1679 }
1680
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001681 if(!boot_into_recovery)
1682 {
1683 ptn = ptable_find(ptable, "boot");
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001684
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001685 if (ptn == NULL) {
1686 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1687 return -1;
1688 }
1689 }
1690 else
1691 {
1692 ptn = ptable_find(ptable, "recovery");
1693 if (ptn == NULL) {
1694 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
1695 return -1;
1696 }
Dima Zavin214cc642009-01-26 11:16:21 -08001697 }
1698
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05301699 /* Read boot.img header from flash */
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001700 if (flash_read(ptn, offset, buf, page_size)) {
Dima Zavin214cc642009-01-26 11:16:21 -08001701 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
1702 return -1;
1703 }
Dima Zavin214cc642009-01-26 11:16:21 -08001704
1705 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001706 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Dima Zavin214cc642009-01-26 11:16:21 -08001707 return -1;
1708 }
1709
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001710 if (hdr->page_size != page_size) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001711 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 -08001712 return -1;
1713 }
1714
Kishor PK9e91b592017-05-24 12:14:55 +05301715 image_addr = (unsigned char *)target_get_scratch_address();
1716 memcpy(image_addr, (void *)buf, page_size);
vijay kumar287bb542015-09-29 13:01:52 +05301717
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001718 /*
1719 * Update the kernel/ramdisk/tags address if the boot image header
1720 * has default values, these default values come from mkbootimg when
1721 * the boot image is flashed using fastboot flash:raw
1722 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001723 update_ker_tags_rdisk_addr(hdr, false);
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001724
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001725 /* Get virtual addresses since the hdr saves physical addresses. */
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001726 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
1727 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
1728 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
1729
1730 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1731 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Mayank Grover032736f2017-07-14 20:34:51 +05301732 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001733
Kishor PK9e91b592017-05-24 12:14:55 +05301734 /* ensure commandline is terminated */
1735 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
1736
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001737 /* Check if the addresses in the header are valid. */
1738 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
Mayank Grovere559cec2017-10-17 15:12:03 +05301739 check_ddr_addr_range_bound(hdr->kernel_addr, kernel_actual) ||
1740 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual) ||
1741 check_ddr_addr_range_bound(hdr->ramdisk_addr, ramdisk_actual))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001742 {
Mayank Grovere559cec2017-10-17 15:12:03 +05301743 dprintf(CRITICAL, "kernel/ramdisk addresses are not valid.\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001744 return -1;
1745 }
1746
1747#ifndef DEVICE_TREE
Kishor PKd8ddcad2017-07-27 13:53:57 +05301748 if (UINT_MAX < ((uint64_t)kernel_actual + (uint64_t)ramdisk_actual+ (uint64_t)second_actual + page_size)) {
Mayank Grover032736f2017-07-14 20:34:51 +05301749 dprintf(CRITICAL, "Integer overflow detected in bootimage header fields\n");
1750 return -1;
1751 }
Kishor PKd8ddcad2017-07-27 13:53:57 +05301752 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + second_actual);
Mayank Grover032736f2017-07-14 20:34:51 +05301753
Mayank Grovere559cec2017-10-17 15:12:03 +05301754 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE) ||
1755 check_ddr_addr_range_bound(hdr->tags_addr, MAX_TAGS_SIZE))
Vijay Kumar Pendotid3ed20e2016-09-20 00:34:46 +05301756 {
Mayank Grovere559cec2017-10-17 15:12:03 +05301757 dprintf(CRITICAL, "Tags addresses are not valid.\n");
Vijay Kumar Pendotid3ed20e2016-09-20 00:34:46 +05301758 return -1;
1759 }
1760#else
1761
1762#ifndef OSVERSION_IN_BOOTIMAGE
1763 dt_size = hdr->dt_size;
1764#endif
Mayank Grover032736f2017-07-14 20:34:51 +05301765 dt_actual = ROUND_TO_PAGE(dt_size, page_mask);
Kishor PKd8ddcad2017-07-27 13:53:57 +05301766 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 +05301767 dprintf(CRITICAL, "Integer overflow detected in bootimage header fields\n");
1768 return -1;
1769 }
1770
Kishor PKd8ddcad2017-07-27 13:53:57 +05301771 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + second_actual + dt_actual);
Mayank Grover032736f2017-07-14 20:34:51 +05301772
Mayank Grovere559cec2017-10-17 15:12:03 +05301773 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_size) ||
1774 check_ddr_addr_range_bound(hdr->tags_addr, dt_size))
Mayank Grover032736f2017-07-14 20:34:51 +05301775 {
Mayank Grovere559cec2017-10-17 15:12:03 +05301776 dprintf(CRITICAL, "Device tree addresses are not valid.\n");
Mayank Grover032736f2017-07-14 20:34:51 +05301777 return -1;
1778 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001779#endif
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001780
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05301781 /* Read full boot.img from flash */
1782 dprintf(INFO, "Loading (%s) image (%d): start\n",
1783 (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual);
1784 bs_set_timestamp(BS_KERNEL_LOAD_START);
1785
1786 if (UINT_MAX - page_size < imagesize_actual)
1787 {
1788 dprintf(CRITICAL,"Integer overflow detected in bootimage header fields %u %s\n", __LINE__,__func__);
1789 return -1;
1790 }
1791
1792 /*Check the availability of RAM before reading boot image + max signature length from flash*/
1793 if (target_get_max_flash_size() < (imagesize_actual + page_size))
1794 {
1795 dprintf(CRITICAL, "bootimage size is greater than DDR can hold\n");
1796 return -1;
1797 }
1798
1799 offset = page_size;
1800 /* Read image without signature and header */
1801 if (flash_read(ptn, offset, (void *)(image_addr + offset), imagesize_actual - page_size))
1802 {
1803 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
1804 return -1;
1805 }
1806
1807 dprintf(INFO, "Loading (%s) image (%d): done\n",
1808 (!boot_into_recovery ? "boot" : "recovery"), imagesize_actual);
1809 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
1810
Shashank Mittalcd98d472011-08-02 14:29:24 -07001811 /* Authenticate Kernel */
Deepa Dinamani23b60d42013-06-24 18:10:52 -07001812 if(target_use_signed_kernel() && (!device.is_unlocked))
Shashank Mittalcd98d472011-08-02 14:29:24 -07001813 {
Shashank Mittalcd98d472011-08-02 14:29:24 -07001814 offset = imagesize_actual;
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05301815
Shashank Mittalcd98d472011-08-02 14:29:24 -07001816 /* Read signature */
1817 if (flash_read(ptn, offset, (void *)(image_addr + offset), page_size))
1818 {
1819 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001820 return -1;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001821 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07001822
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301823 verify_signed_bootimg((uint32_t)image_addr, imagesize_actual);
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05301824 }
1825 offset = page_size;
1826 if(hdr->second_size != 0) {
1827 if (UINT_MAX - offset < second_actual)
1828 {
1829 dprintf(CRITICAL, "ERROR: Integer overflow in boot image header %s\t%d\n",__func__,__LINE__);
1830 return -1;
vijay kumar8f53e362015-11-24 13:38:11 +05301831 }
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05301832 offset += second_actual;
1833 /* Second image loading not implemented. */
1834 ASSERT(0);
1835 }
Shashank Mittal162244e2011-08-08 19:01:25 -07001836
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05301837 /* Move kernel and ramdisk to correct address */
1838 memmove((void*) hdr->kernel_addr, (char*) (image_addr + page_size), hdr->kernel_size);
1839 memmove((void*) hdr->ramdisk_addr, (char*) (image_addr + page_size + kernel_actual), hdr->ramdisk_size);
1840
1841#if DEVICE_TREE
1842 if(dt_size != 0) {
1843
1844 dt_table_offset = ((uint32_t)image_addr + page_size + kernel_actual + ramdisk_actual + second_actual);
1845
1846 table = (struct dt_table*) dt_table_offset;
1847
1848 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
1849 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1850 return -1;
1851 }
1852
1853 /* Its Error if, dt_hdr_size (table->num_entries * dt_entry size + Dev_Tree Header)
1854 goes beyound hdr->dt_size*/
1855 if (dt_hdr_size > ROUND_TO_PAGE(dt_size,hdr->page_size)) {
1856 dprintf(CRITICAL, "ERROR: Invalid Device Tree size \n");
1857 return -1;
1858 }
1859
1860 /* Find index of device tree within device tree table */
1861 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
1862 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1863 return -1;
1864 }
1865
1866 /* Validate and Read device device tree in the "tags_add */
1867 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size) ||
1868 check_ddr_addr_range_bound(hdr->tags_addr, dt_entry.size))
1869 {
1870 dprintf(CRITICAL, "Device tree addresses are not valid.\n");
1871 return -1;
1872 }
1873
1874 if(dt_entry.offset > (UINT_MAX - dt_entry.size)) {
1875 dprintf(CRITICAL, "ERROR: Device tree contents are Invalid\n");
1876 return -1;
1877 }
1878
1879 /* Ensure we are not overshooting dt_size with the dt_entry selected */
1880 if ((dt_entry.offset + dt_entry.size) > dt_size) {
1881 dprintf(CRITICAL, "ERROR: Device tree contents are Invalid\n");
1882 return -1;
1883 }
1884
1885 best_match_dt_addr = (unsigned char *)table + dt_entry.offset;
1886 dtb_size = dt_entry.size;
1887 memmove((void *)hdr->tags_addr, (char *)best_match_dt_addr, dtb_size);
1888 }
1889#endif
1890 if(target_use_signed_kernel() && (!device.is_unlocked))
1891 {
Shashank Mittal162244e2011-08-08 19:01:25 -07001892 /* Make sure everything from scratch address is read before next step!*/
Shashank Mittala0032282011-08-26 14:50:11 -07001893 if(device.is_tampered)
Shashank Mittal162244e2011-08-08 19:01:25 -07001894 {
1895 write_device_info_flash(&device);
1896 }
Channagoud Kadabi5c86fe32012-02-16 10:58:48 +05301897#if USE_PCOM_SECBOOT
1898 set_tamper_flag(device.is_tampered);
1899#endif
Shashank Mittalcd98d472011-08-02 14:29:24 -07001900 }
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001901
David Ng183a7422009-12-07 14:55:21 -08001902continue_boot:
Dima Zavin214cc642009-01-26 11:16:21 -08001903
Dima Zavin214cc642009-01-26 11:16:21 -08001904 /* TODO: create/pass atags to kernel */
1905
Ajay Dudanie28a6072011-07-01 13:59:46 -07001906 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07001907 (const char *)hdr->cmdline, board_machtype(),
Dima Zavin214cc642009-01-26 11:16:21 -08001908 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
1909
1910 return 0;
1911}
Brian Swetland9c4c0752009-01-25 16:23:50 -08001912
Shashank Mittal162244e2011-08-08 19:01:25 -07001913void write_device_info_mmc(device_info *dev)
1914{
Shashank Mittal162244e2011-08-08 19:01:25 -07001915 unsigned long long ptn = 0;
1916 unsigned long long size;
1917 int index = INVALID_PTN;
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001918 uint32_t blocksize;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001919 uint8_t lun = 0;
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001920 uint32_t ret = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001921
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001922 if (devinfo_present)
1923 index = partition_get_index("devinfo");
1924 else
1925 index = partition_get_index("aboot");
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001926
Shashank Mittal162244e2011-08-08 19:01:25 -07001927 ptn = partition_get_offset(index);
1928 if(ptn == 0)
1929 {
1930 return;
1931 }
1932
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001933 lun = partition_get_lun(index);
1934 mmc_set_lun(lun);
1935
Shashank Mittal162244e2011-08-08 19:01:25 -07001936 size = partition_get_size(index);
1937
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001938 blocksize = mmc_get_device_blocksize();
1939
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001940 if (devinfo_present)
1941 ret = mmc_write(ptn, blocksize, (void *)info_buf);
1942 else
1943 ret = mmc_write((ptn + size - blocksize), blocksize, (void *)info_buf);
1944 if (ret)
Shashank Mittal162244e2011-08-08 19:01:25 -07001945 {
1946 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
Sridhar Parasurame1a97922015-07-27 15:31:22 -07001947 ASSERT(0);
Shashank Mittal162244e2011-08-08 19:01:25 -07001948 }
1949}
1950
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001951void read_device_info_mmc(struct device_info *info)
Shashank Mittal162244e2011-08-08 19:01:25 -07001952{
Shashank Mittal162244e2011-08-08 19:01:25 -07001953 unsigned long long ptn = 0;
1954 unsigned long long size;
1955 int index = INVALID_PTN;
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001956 uint32_t blocksize;
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001957 uint32_t ret = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001958
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001959 if ((index = partition_get_index("devinfo")) < 0)
1960 {
1961 devinfo_present = false;
1962 index = partition_get_index("aboot");
1963 }
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001964
Shashank Mittal162244e2011-08-08 19:01:25 -07001965 ptn = partition_get_offset(index);
1966 if(ptn == 0)
1967 {
1968 return;
1969 }
1970
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001971 mmc_set_lun(partition_get_lun(index));
1972
Shashank Mittal162244e2011-08-08 19:01:25 -07001973 size = partition_get_size(index);
1974
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001975 blocksize = mmc_get_device_blocksize();
1976
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001977 if (devinfo_present)
1978 ret = mmc_read(ptn, (void *)info_buf, blocksize);
1979 else
1980 ret = mmc_read((ptn + size - blocksize), (void *)info_buf, blocksize);
1981 if (ret)
Shashank Mittal162244e2011-08-08 19:01:25 -07001982 {
1983 dprintf(CRITICAL, "ERROR: Cannot read device info\n");
Sridhar Parasurame1a97922015-07-27 15:31:22 -07001984 ASSERT(0);
Shashank Mittal162244e2011-08-08 19:01:25 -07001985 }
Shashank Mittal162244e2011-08-08 19:01:25 -07001986}
1987
1988void write_device_info_flash(device_info *dev)
1989{
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001990 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
Shashank Mittal162244e2011-08-08 19:01:25 -07001991 struct ptentry *ptn;
1992 struct ptable *ptable;
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001993 if(info == NULL)
1994 {
1995 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
1996 ASSERT(0);
1997 }
1998 info_buf = info;
Shashank Mittal162244e2011-08-08 19:01:25 -07001999 ptable = flash_get_ptable();
2000 if (ptable == NULL)
2001 {
2002 dprintf(CRITICAL, "ERROR: Partition table not found\n");
2003 return;
2004 }
2005
2006 ptn = ptable_find(ptable, "devinfo");
2007 if (ptn == NULL)
2008 {
Smita Ghosh670c8b82015-05-07 09:30:03 -07002009 dprintf(CRITICAL, "ERROR: No devinfo partition found\n");
Shashank Mittal162244e2011-08-08 19:01:25 -07002010 return;
2011 }
2012
Mayank Groverdedbc892017-10-24 13:41:34 +05302013 memset(info, 0, BOOT_IMG_MAX_PAGE_SIZE);
Shashank Mittal162244e2011-08-08 19:01:25 -07002014 memcpy(info, dev, sizeof(device_info));
2015
2016 if (flash_write(ptn, 0, (void *)info_buf, page_size))
2017 {
2018 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
2019 return;
2020 }
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002021 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07002022}
2023
vijay kumarc65876c2015-04-24 13:29:16 +05302024static int read_allow_oem_unlock(device_info *dev)
2025{
vijay kumarc65876c2015-04-24 13:29:16 +05302026 unsigned offset;
2027 int index;
2028 unsigned long long ptn;
2029 unsigned long long ptn_size;
2030 unsigned blocksize = mmc_get_device_blocksize();
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07002031 STACKBUF_DMA_ALIGN(buf, blocksize);
vijay kumarc65876c2015-04-24 13:29:16 +05302032
vijay kumarca2e6812015-07-08 20:28:25 +05302033 index = partition_get_index(frp_ptns[0]);
vijay kumarc65876c2015-04-24 13:29:16 +05302034 if (index == INVALID_PTN)
2035 {
vijay kumarca2e6812015-07-08 20:28:25 +05302036 index = partition_get_index(frp_ptns[1]);
2037 if (index == INVALID_PTN)
2038 {
2039 dprintf(CRITICAL, "Neither '%s' nor '%s' partition found\n", frp_ptns[0],frp_ptns[1]);
2040 return -1;
2041 }
vijay kumarc65876c2015-04-24 13:29:16 +05302042 }
2043
2044 ptn = partition_get_offset(index);
2045 ptn_size = partition_get_size(index);
2046 offset = ptn_size - blocksize;
2047
Mayank Grover48860402016-11-29 12:34:53 +05302048 /* Set Lun for partition */
2049 mmc_set_lun(partition_get_lun(index));
2050
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07002051 if (mmc_read(ptn + offset, (void *)buf, blocksize))
vijay kumarc65876c2015-04-24 13:29:16 +05302052 {
2053 dprintf(CRITICAL, "Reading MMC failed\n");
2054 return -1;
2055 }
2056
2057 /*is_allow_unlock is a bool value stored at the LSB of last byte*/
2058 is_allow_unlock = buf[blocksize-1] & 0x01;
2059 return 0;
2060}
2061
2062static int write_allow_oem_unlock(bool allow_unlock)
2063{
vijay kumarc65876c2015-04-24 13:29:16 +05302064 unsigned offset;
vijay kumarc65876c2015-04-24 13:29:16 +05302065 int index;
2066 unsigned long long ptn;
2067 unsigned long long ptn_size;
2068 unsigned blocksize = mmc_get_device_blocksize();
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07002069 STACKBUF_DMA_ALIGN(buf, blocksize);
vijay kumarc65876c2015-04-24 13:29:16 +05302070
vijay kumarca2e6812015-07-08 20:28:25 +05302071 index = partition_get_index(frp_ptns[0]);
vijay kumarc65876c2015-04-24 13:29:16 +05302072 if (index == INVALID_PTN)
2073 {
vijay kumarca2e6812015-07-08 20:28:25 +05302074 index = partition_get_index(frp_ptns[1]);
2075 if (index == INVALID_PTN)
2076 {
2077 dprintf(CRITICAL, "Neither '%s' nor '%s' partition found\n", frp_ptns[0],frp_ptns[1]);
2078 return -1;
2079 }
vijay kumarc65876c2015-04-24 13:29:16 +05302080 }
2081
2082 ptn = partition_get_offset(index);
2083 ptn_size = partition_get_size(index);
2084 offset = ptn_size - blocksize;
Mayank Grover48860402016-11-29 12:34:53 +05302085 mmc_set_lun(partition_get_lun(index));
vijay kumarc65876c2015-04-24 13:29:16 +05302086
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07002087 if (mmc_read(ptn + offset, (void *)buf, blocksize))
vijay kumarc65876c2015-04-24 13:29:16 +05302088 {
2089 dprintf(CRITICAL, "Reading MMC failed\n");
2090 return -1;
2091 }
2092
2093 /*is_allow_unlock is a bool value stored at the LSB of last byte*/
2094 buf[blocksize-1] = allow_unlock;
2095 if (mmc_write(ptn + offset, blocksize, buf))
2096 {
2097 dprintf(CRITICAL, "Writing MMC failed\n");
2098 return -1;
2099 }
2100
2101 return 0;
2102}
2103
Shashank Mittal162244e2011-08-08 19:01:25 -07002104void read_device_info_flash(device_info *dev)
2105{
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002106 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
Shashank Mittal162244e2011-08-08 19:01:25 -07002107 struct ptentry *ptn;
2108 struct ptable *ptable;
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002109 if(info == NULL)
2110 {
2111 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
2112 ASSERT(0);
2113 }
2114 info_buf = info;
Shashank Mittal162244e2011-08-08 19:01:25 -07002115 ptable = flash_get_ptable();
2116 if (ptable == NULL)
2117 {
2118 dprintf(CRITICAL, "ERROR: Partition table not found\n");
2119 return;
2120 }
2121
2122 ptn = ptable_find(ptable, "devinfo");
2123 if (ptn == NULL)
2124 {
Smita Ghosh670c8b82015-05-07 09:30:03 -07002125 dprintf(CRITICAL, "ERROR: No devinfo partition found\n");
Shashank Mittal162244e2011-08-08 19:01:25 -07002126 return;
2127 }
2128
2129 if (flash_read(ptn, 0, (void *)info_buf, page_size))
2130 {
2131 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
2132 return;
2133 }
2134
2135 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
2136 {
Shashank Mittal162244e2011-08-08 19:01:25 -07002137 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
2138 info->is_unlocked = 0;
Shashank Mittala0032282011-08-26 14:50:11 -07002139 info->is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07002140 write_device_info_flash(info);
2141 }
2142 memcpy(dev, info, sizeof(device_info));
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002143 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07002144}
2145
2146void write_device_info(device_info *dev)
2147{
2148 if(target_is_emmc_boot())
2149 {
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002150 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
2151 if(info == NULL)
2152 {
2153 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
2154 ASSERT(0);
2155 }
2156 info_buf = info;
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002157 memcpy(info, dev, sizeof(struct device_info));
2158
2159#if USE_RPMB_FOR_DEVINFO
Mayank Grover912eaa62017-10-26 12:08:53 +05302160 if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05302161 is_secure_boot_enable()) {
2162 if((write_device_info_rpmb((void*) info, PAGE_SIZE)) < 0)
2163 ASSERT(0);
Sridhar Parasurame1a97922015-07-27 15:31:22 -07002164 }
Channagoud Kadabic80cb492015-04-28 16:08:28 -07002165 else
2166 write_device_info_mmc(info);
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002167#else
2168 write_device_info_mmc(info);
2169#endif
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002170 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07002171 }
2172 else
2173 {
2174 write_device_info_flash(dev);
2175 }
2176}
2177
2178void read_device_info(device_info *dev)
2179{
2180 if(target_is_emmc_boot())
2181 {
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002182 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
2183 if(info == NULL)
2184 {
2185 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
2186 ASSERT(0);
2187 }
2188 info_buf = info;
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002189
2190#if USE_RPMB_FOR_DEVINFO
Mayank Grover912eaa62017-10-26 12:08:53 +05302191 if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05302192 is_secure_boot_enable()) {
2193 if((read_device_info_rpmb((void*) info, PAGE_SIZE)) < 0)
2194 ASSERT(0);
Sridhar Parasurame1a97922015-07-27 15:31:22 -07002195 }
Channagoud Kadabic80cb492015-04-28 16:08:28 -07002196 else
2197 read_device_info_mmc(info);
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002198#else
2199 read_device_info_mmc(info);
2200#endif
2201
2202 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
2203 {
2204 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
lijuang511a2b52015-08-14 20:50:51 +08002205 if (is_secure_boot_enable()) {
Channagoud Kadabi05f78ba2015-07-06 11:58:14 -07002206 info->is_unlocked = 0;
Mayank Grover889be1b2017-09-12 20:12:23 +05302207#if VERIFIED_BOOT
Mayank Grover912eaa62017-10-26 12:08:53 +05302208 if (VB_M <= target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +05302209 info->is_unlock_critical = 0;
Parth Dixitddbc7352015-10-18 03:13:31 +05302210#endif
lijuang511a2b52015-08-14 20:50:51 +08002211 } else {
Channagoud Kadabi2fda4092015-07-07 13:34:11 -07002212 info->is_unlocked = 1;
Mayank Grover889be1b2017-09-12 20:12:23 +05302213#if VERIFIED_BOOT
Mayank Grover912eaa62017-10-26 12:08:53 +05302214 if (VB_M <= target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +05302215 info->is_unlock_critical = 1;
Parth Dixitddbc7352015-10-18 03:13:31 +05302216#endif
lijuang511a2b52015-08-14 20:50:51 +08002217 }
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002218 info->is_tampered = 0;
2219 info->charger_screen_enabled = 0;
Mayank Grover889be1b2017-09-12 20:12:23 +05302220#if VERIFIED_BOOT
Mayank Grover912eaa62017-10-26 12:08:53 +05302221 if (VB_M <= target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +05302222 info->verity_mode = 1; //enforcing by default
Parth Dixitddbc7352015-10-18 03:13:31 +05302223#endif
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002224 write_device_info(info);
2225 }
2226 memcpy(dev, info, sizeof(device_info));
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002227 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07002228 }
2229 else
2230 {
2231 read_device_info_flash(dev);
2232 }
2233}
2234
2235void reset_device_info()
2236{
2237 dprintf(ALWAYS, "reset_device_info called.");
Shashank Mittala0032282011-08-26 14:50:11 -07002238 device.is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07002239 write_device_info(&device);
2240}
2241
2242void set_device_root()
2243{
2244 dprintf(ALWAYS, "set_device_root called.");
Shashank Mittala0032282011-08-26 14:50:11 -07002245 device.is_tampered = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -07002246 write_device_info(&device);
2247}
2248
lijuang4ece1e72015-08-14 21:02:36 +08002249/* set device unlock value
2250 * Must check FRP before call this function
2251 * Need to wipe data when unlock status changed
2252 * type 0: oem unlock
2253 * type 1: unlock critical
2254 * status 0: unlock as false
2255 * status 1: lock as true
2256 */
2257void set_device_unlock_value(int type, bool status)
lijuang21f12f52015-08-22 16:22:19 +08002258{
lijuang4ece1e72015-08-14 21:02:36 +08002259 if (type == UNLOCK)
2260 device.is_unlocked = status;
Mayank Grover889be1b2017-09-12 20:12:23 +05302261#if VERIFIED_BOOT
Mayank Grover912eaa62017-10-26 12:08:53 +05302262 else if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05302263 type == UNLOCK_CRITICAL)
2264 device.is_unlock_critical = status;
Parth Dixitddbc7352015-10-18 03:13:31 +05302265#endif
lijuang4ece1e72015-08-14 21:02:36 +08002266 write_device_info(&device);
2267}
2268
2269static void set_device_unlock(int type, bool status)
2270{
2271 int is_unlocked = -1;
2272 char response[MAX_RSP_SIZE];
2273
2274 /* check device unlock status if it is as expected */
2275 if (type == UNLOCK)
2276 is_unlocked = device.is_unlocked;
Mayank Grover889be1b2017-09-12 20:12:23 +05302277#if VERIFIED_BOOT
Mayank Grover912eaa62017-10-26 12:08:53 +05302278 if(VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05302279 type == UNLOCK_CRITICAL)
2280 {
2281 is_unlocked = device.is_unlock_critical;
2282 }
Parth Dixitddbc7352015-10-18 03:13:31 +05302283#endif
lijuang4ece1e72015-08-14 21:02:36 +08002284 if (is_unlocked == status) {
2285 snprintf(response, sizeof(response), "\tDevice already : %s", (status ? "unlocked!" : "locked!"));
2286 fastboot_info(response);
2287 fastboot_okay("");
2288 return;
2289 }
2290
2291 /* status is true, it means to unlock device */
2292 if (status) {
lijuang21f12f52015-08-22 16:22:19 +08002293 if(!is_allow_unlock) {
2294 fastboot_fail("oem unlock is not allowed");
2295 return;
2296 }
2297
lijuang4ece1e72015-08-14 21:02:36 +08002298#if FBCON_DISPLAY_MSG
lijuang9a7d3b92015-11-30 14:41:24 +08002299 display_unlock_menu(type);
lijuang4ece1e72015-08-14 21:02:36 +08002300 fastboot_okay("");
2301 return;
2302#else
2303 if (type == UNLOCK) {
2304 fastboot_fail("Need wipe userdata. Do 'fastboot oem unlock-go'");
2305 return;
2306 }
2307#endif
lijuang21f12f52015-08-22 16:22:19 +08002308 }
lijuang4ece1e72015-08-14 21:02:36 +08002309
2310 set_device_unlock_value(type, status);
2311
2312 /* wipe data */
2313 struct recovery_message msg;
Kishor PK60a68212017-05-08 16:55:57 +05302314 memset(&msg, 0, sizeof(msg));
lijuang4ece1e72015-08-14 21:02:36 +08002315 snprintf(msg.recovery, sizeof(msg.recovery), "recovery\n--wipe_data");
2316 write_misc(0, &msg, sizeof(msg));
2317
2318 fastboot_okay("");
2319 reboot_device(RECOVERY_MODE);
lijuang21f12f52015-08-22 16:22:19 +08002320}
2321
lijuang511a2b52015-08-14 20:50:51 +08002322static bool critical_flash_allowed(const char * entry)
2323{
2324 uint32_t i = 0;
2325 if (entry == NULL)
2326 return false;
2327
2328 for (i = 0; i < ARRAY_SIZE(critical_flash_allowed_ptn); i++) {
2329 if(!strcmp(entry, critical_flash_allowed_ptn[i]))
2330 return true;
2331 }
2332 return false;
Matthew Qin271927e2015-03-31 22:07:07 -04002333}
2334
2335#if DEVICE_TREE
Amol Jadicb524072012-08-09 16:40:18 -07002336int copy_dtb(uint8_t *boot_image_start, unsigned int scratch_offset)
2337{
2338 uint32 dt_image_offset = 0;
Amol Jadicb524072012-08-09 16:40:18 -07002339 uint32_t n;
Amol Jadicb524072012-08-09 16:40:18 -07002340 struct dt_table *table;
2341 struct dt_entry dt_entry;
2342 uint32_t dt_hdr_size;
2343 unsigned int compressed_size = 0;
2344 unsigned int dtb_size = 0;
2345 unsigned int out_avai_len = 0;
2346 unsigned char *out_addr = NULL;
2347 unsigned char *best_match_dt_addr = NULL;
2348 int rc;
2349
2350 struct boot_img_hdr *hdr = (struct boot_img_hdr *) (boot_image_start);
2351
Parth Dixit4097b622016-03-15 14:42:27 +05302352#ifndef OSVERSION_IN_BOOTIMAGE
2353 dt_size = hdr->dt_size;
2354#endif
2355
2356 if(dt_size != 0) {
Amol Jadicb524072012-08-09 16:40:18 -07002357 /* add kernel offset */
2358 dt_image_offset += page_size;
2359 n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
2360 dt_image_offset += n;
2361
2362 /* add ramdisk offset */
2363 n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
2364 dt_image_offset += n;
2365
2366 /* add second offset */
2367 if(hdr->second_size != 0) {
2368 n = ROUND_TO_PAGE(hdr->second_size, page_mask);
2369 dt_image_offset += n;
2370 }
2371
2372 /* offset now point to start of dt.img */
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07002373 table = (struct dt_table*)(boot_image_start + dt_image_offset);
Amol Jadicb524072012-08-09 16:40:18 -07002374
Deepa Dinamani19648b42013-09-05 17:05:55 -07002375 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Amol Jadicb524072012-08-09 16:40:18 -07002376 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
2377 return -1;
2378 }
P.V. Phani Kumar82916762016-03-09 09:20:19 +05302379
2380 /* Its Error if, dt_hdr_size (table->num_entries * dt_entry size + Dev_Tree Header)
2381 goes beyound hdr->dt_size*/
Parth Dixit4097b622016-03-15 14:42:27 +05302382 if (dt_hdr_size > ROUND_TO_PAGE(dt_size,hdr->page_size)) {
P.V. Phani Kumar82916762016-03-09 09:20:19 +05302383 dprintf(CRITICAL, "ERROR: Invalid Device Tree size \n");
2384 return -1;
2385 }
2386
Joel Kingaa335dc2013-06-03 16:11:08 -07002387 /* Find index of device tree within device tree table */
2388 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Amol Jadicb524072012-08-09 16:40:18 -07002389 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
2390 return -1;
2391 }
2392
Matthew Qin271927e2015-03-31 22:07:07 -04002393 best_match_dt_addr = (unsigned char *)boot_image_start + dt_image_offset + dt_entry.offset;
2394 if (is_gzip_package(best_match_dt_addr, dt_entry.size))
2395 {
2396 out_addr = (unsigned char *)target_get_scratch_address() + scratch_offset;
2397 out_avai_len = target_get_max_flash_size() - scratch_offset;
Matthew Qin0b15b322015-05-19 05:20:54 -04002398 dprintf(INFO, "decompressing dtb: start\n");
Matthew Qin271927e2015-03-31 22:07:07 -04002399 rc = decompress(best_match_dt_addr,
2400 dt_entry.size, out_addr, out_avai_len,
2401 &compressed_size, &dtb_size);
2402 if (rc)
2403 {
Matthew Qin0b15b322015-05-19 05:20:54 -04002404 dprintf(CRITICAL, "decompressing dtb failed!!!\n");
Matthew Qin271927e2015-03-31 22:07:07 -04002405 ASSERT(0);
2406 }
2407
Matthew Qin0b15b322015-05-19 05:20:54 -04002408 dprintf(INFO, "decompressing dtb: done\n");
Matthew Qin271927e2015-03-31 22:07:07 -04002409 best_match_dt_addr = out_addr;
2410 } else {
2411 dtb_size = dt_entry.size;
2412 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002413 /* Validate and Read device device tree in the "tags_add */
Mayank Grovere559cec2017-10-17 15:12:03 +05302414 if (check_aboot_addr_range_overlap(hdr->tags_addr, dtb_size) ||
2415 check_ddr_addr_range_bound(hdr->tags_addr, dtb_size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002416 {
Mayank Grovere559cec2017-10-17 15:12:03 +05302417 dprintf(CRITICAL, "Device tree addresses are not valid.\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002418 return -1;
2419 }
2420
Amol Jadicb524072012-08-09 16:40:18 -07002421 /* Read device device tree in the "tags_add */
Matthew Qin271927e2015-03-31 22:07:07 -04002422 memmove((void*) hdr->tags_addr, (void *)best_match_dt_addr, dtb_size);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002423 } else
2424 return -1;
Amol Jadicb524072012-08-09 16:40:18 -07002425
2426 /* Everything looks fine. Return success. */
2427 return 0;
2428}
2429#endif
2430
Brian Swetland9c4c0752009-01-25 16:23:50 -08002431void cmd_boot(const char *arg, void *data, unsigned sz)
2432{
Amit Blay8a510302015-08-17 09:20:01 +03002433#ifdef MDTP_SUPPORT
2434 static bool is_mdtp_activated = 0;
2435#endif /* MDTP_SUPPORT */
Brian Swetland9c4c0752009-01-25 16:23:50 -08002436 unsigned kernel_actual;
2437 unsigned ramdisk_actual;
Kishor PKd8ddcad2017-07-27 13:53:57 +05302438 unsigned second_actual;
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002439 uint32_t image_actual;
2440 uint32_t dt_actual = 0;
P.V. Phani Kumareecfd822016-03-09 20:09:03 +05302441 uint32_t sig_actual = 0;
Kishor PK5134b332017-05-09 17:50:08 +05302442 uint32_t sig_size = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08002443 struct boot_img_hdr *hdr = NULL;
2444 struct kernel64_hdr *kptr = NULL;
Brian Swetland9c4c0752009-01-25 16:23:50 -08002445 char *ptr = ((char*) data);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002446 int ret = 0;
2447 uint8_t dtb_copied = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08002448 unsigned int out_len = 0;
2449 unsigned int out_avai_len = 0;
2450 unsigned char *out_addr = NULL;
2451 uint32_t dtb_offset = 0;
2452 unsigned char *kernel_start_addr = NULL;
2453 unsigned int kernel_size = 0;
Matthew Qin271927e2015-03-31 22:07:07 -04002454 unsigned int scratch_offset = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08002455
lijuang2008ff22016-03-07 17:56:27 +08002456#if FBCON_DISPLAY_MSG
2457 /* Exit keys' detection thread firstly */
2458 exit_menu_keys_detection();
2459#endif
2460
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002461#if VERIFIED_BOOT
Channagoud Kadabi6d5375e2015-06-23 17:15:42 -07002462 if(target_build_variant_user() && !device.is_unlocked)
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002463 {
2464 fastboot_fail("unlock device to use this command");
lijuang2008ff22016-03-07 17:56:27 +08002465 goto boot_failed;
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002466 }
2467#endif
2468
Brian Swetland9c4c0752009-01-25 16:23:50 -08002469 if (sz < sizeof(hdr)) {
2470 fastboot_fail("invalid bootimage header");
lijuang2008ff22016-03-07 17:56:27 +08002471 goto boot_failed;
Brian Swetland9c4c0752009-01-25 16:23:50 -08002472 }
2473
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002474 hdr = (struct boot_img_hdr *)data;
Brian Swetland9c4c0752009-01-25 16:23:50 -08002475
2476 /* ensure commandline is terminated */
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002477 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -08002478
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002479 if(target_is_emmc_boot() && hdr->page_size) {
2480 page_size = hdr->page_size;
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07002481 page_mask = page_size - 1;
2482 }
2483
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002484 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
2485 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Kishor PKd8ddcad2017-07-27 13:53:57 +05302486 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002487#if DEVICE_TREE
Parth Dixit4097b622016-03-15 14:42:27 +05302488#ifndef OSVERSION_IN_BOOTIMAGE
2489 dt_size = hdr->dt_size;
2490#endif
2491 dt_actual = ROUND_TO_PAGE(dt_size, page_mask);
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002492#endif
2493
2494 image_actual = ADD_OF(page_size, kernel_actual);
2495 image_actual = ADD_OF(image_actual, ramdisk_actual);
Kishor PKd8ddcad2017-07-27 13:53:57 +05302496 image_actual = ADD_OF(image_actual, second_actual);
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002497 image_actual = ADD_OF(image_actual, dt_actual);
2498
Kishor PK5134b332017-05-09 17:50:08 +05302499 /* Checking to prevent oob access in read_der_message_length */
2500 if (image_actual > sz) {
2501 fastboot_fail("bootimage header fields are invalid");
2502 goto boot_failed;
2503 }
2504 sig_size = sz - image_actual;
2505
P.V. Phani Kumareecfd822016-03-09 20:09:03 +05302506 if (target_use_signed_kernel() && (!device.is_unlocked)) {
2507 /* Calculate the signature length from boot image */
2508 sig_actual = read_der_message_length(
Kishor PK5134b332017-05-09 17:50:08 +05302509 (unsigned char*)(data + image_actual), sig_size);
Channagoud Kadabi3d839012014-06-26 14:26:39 -07002510 image_actual = ADD_OF(image_actual, sig_actual);
2511
Kishor PK5134b332017-05-09 17:50:08 +05302512 if (image_actual > sz) {
2513 fastboot_fail("bootimage header fields are invalid");
2514 goto boot_failed;
2515 }
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002516 }
2517
Sridhar Parasuram361e4cd2015-09-24 11:19:19 -07002518 // Initialize boot state before trying to verify boot.img
2519#if VERIFIED_BOOT
Channagoud Kadabi699466e2015-11-03 12:37:42 -08002520 boot_verifier_init();
Mayank Groverb337e932017-01-18 20:00:40 +05302521#endif
Channagoud Kadabibf6ce7d2015-09-17 13:25:35 -07002522 /* Handle overflow if the input image size is greater than
2523 * boot image buffer can hold
2524 */
2525 if ((target_get_max_flash_size() - (image_actual - sig_actual)) < page_size)
2526 {
2527 fastboot_fail("booimage: size is greater than boot image buffer can hold");
lijuang2008ff22016-03-07 17:56:27 +08002528 goto boot_failed;
Channagoud Kadabibf6ce7d2015-09-17 13:25:35 -07002529 }
Sridhar Parasuram361e4cd2015-09-24 11:19:19 -07002530
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002531 /* Verify the boot image
2532 * device & page_size are initialized in aboot_init
2533 */
Sridhar Parasuram7ae9fbc2015-09-17 09:56:30 -07002534 if (target_use_signed_kernel() && (!device.is_unlocked)) {
Channagoud Kadabi3d839012014-06-26 14:26:39 -07002535 /* Pass size excluding signature size, otherwise we would try to
2536 * access signature beyond its length
2537 */
2538 verify_signed_bootimg((uint32_t)data, (image_actual - sig_actual));
Sridhar Parasuram7ae9fbc2015-09-17 09:56:30 -07002539 }
Amit Blay4aa292f2015-04-28 21:55:59 +03002540#ifdef MDTP_SUPPORT
2541 else
2542 {
Shay Nachmani062aa3f2015-05-17 17:28:44 +03002543 /* fastboot boot is not allowed when MDTP is activated */
Amit Blay4aa292f2015-04-28 21:55:59 +03002544 mdtp_ext_partition_verification_t ext_partition;
Amit Blay8a510302015-08-17 09:20:01 +03002545
2546 if (!is_mdtp_activated) {
2547 ext_partition.partition = MDTP_PARTITION_NONE;
2548 mdtp_fwlock_verify_lock(&ext_partition);
2549 }
Amit Blay4aa292f2015-04-28 21:55:59 +03002550 }
Shay Nachmani062aa3f2015-05-17 17:28:44 +03002551
Amir Kotzer7c768c02016-04-13 09:08:05 +03002552 /* If mdtp state cannot be validate, block fastboot boot*/
2553 if(mdtp_activated(&is_mdtp_activated)){
2554 dprintf(CRITICAL, "mdtp_activated cannot validate state.\n");
2555 dprintf(CRITICAL, "Can not proceed with fastboot boot command.\n");
2556 goto boot_failed;
2557 }
Shay Nachmani062aa3f2015-05-17 17:28:44 +03002558 if(is_mdtp_activated){
2559 dprintf(CRITICAL, "fastboot boot command is not available.\n");
lijuang2008ff22016-03-07 17:56:27 +08002560 goto boot_failed;
Shay Nachmani062aa3f2015-05-17 17:28:44 +03002561 }
Amit Blay4aa292f2015-04-28 21:55:59 +03002562#endif /* MDTP_SUPPORT */
2563
Sridhar Parasuram361e4cd2015-09-24 11:19:19 -07002564#if VERIFIED_BOOT
Mayank Grover912eaa62017-10-26 12:08:53 +05302565 if (VB_M <= target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +05302566 {
2567 /* set boot and system versions. */
2568 set_os_version((unsigned char *)data);
2569 // send root of trust
2570 if(!send_rot_command((uint32_t)device.is_unlocked))
2571 ASSERT(0);
2572 }
Parth Dixitbc9b6492015-10-18 00:41:38 +05302573#endif
Channagoud Kadabia22144f2013-03-20 11:49:01 -07002574 /*
Matthew Qinbb7923d2015-02-09 10:56:09 +08002575 * Check if the kernel image is a gzip package. If yes, need to decompress it.
2576 * If not, continue booting.
2577 */
2578 if (is_gzip_package((unsigned char *)(data + page_size), hdr->kernel_size))
2579 {
2580 out_addr = (unsigned char *)target_get_scratch_address();
2581 out_addr = (unsigned char *)(out_addr + image_actual + page_size);
2582 out_avai_len = target_get_max_flash_size() - image_actual - page_size;
Matthew Qin0b15b322015-05-19 05:20:54 -04002583 dprintf(INFO, "decompressing kernel image: start\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08002584 ret = decompress((unsigned char *)(ptr + page_size),
2585 hdr->kernel_size, out_addr, out_avai_len,
2586 &dtb_offset, &out_len);
2587 if (ret)
2588 {
Matthew Qin0b15b322015-05-19 05:20:54 -04002589 dprintf(CRITICAL, "decompressing image failed!!!\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08002590 ASSERT(0);
2591 }
2592
Matthew Qin0b15b322015-05-19 05:20:54 -04002593 dprintf(INFO, "decompressing kernel image: done\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08002594 kptr = (struct kernel64_hdr *)out_addr;
2595 kernel_start_addr = out_addr;
2596 kernel_size = out_len;
2597 } else {
2598 kptr = (struct kernel64_hdr*)((char *)data + page_size);
2599 kernel_start_addr = (unsigned char *)((char *)data + page_size);
2600 kernel_size = hdr->kernel_size;
2601 }
2602
2603 /*
Channagoud Kadabia22144f2013-03-20 11:49:01 -07002604 * Update the kernel/ramdisk/tags address if the boot image header
2605 * has default values, these default values come from mkbootimg when
2606 * the boot image is flashed using fastboot flash:raw
2607 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08002608 update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr));
Dima Zavin3cadfff2013-03-21 14:30:48 -07002609
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002610 /* Get virtual addresses since the hdr saves physical addresses. */
2611 hdr->kernel_addr = VA(hdr->kernel_addr);
2612 hdr->ramdisk_addr = VA(hdr->ramdisk_addr);
2613 hdr->tags_addr = VA(hdr->tags_addr);
Brian Swetland9c4c0752009-01-25 16:23:50 -08002614
Matthew Qinbb7923d2015-02-09 10:56:09 +08002615 kernel_size = ROUND_TO_PAGE(kernel_size, page_mask);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002616 /* Check if the addresses in the header are valid. */
Matthew Qinbb7923d2015-02-09 10:56:09 +08002617 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_size) ||
Mayank Grovere559cec2017-10-17 15:12:03 +05302618 check_ddr_addr_range_bound(hdr->kernel_addr, kernel_size) ||
2619 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual) ||
2620 check_ddr_addr_range_bound(hdr->ramdisk_addr, ramdisk_actual))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002621 {
Mayank Grovere559cec2017-10-17 15:12:03 +05302622 dprintf(CRITICAL, "kernel/ramdisk addresses are not valid.\n");
lijuang2008ff22016-03-07 17:56:27 +08002623 goto boot_failed;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002624 }
2625
Amol Jadicb524072012-08-09 16:40:18 -07002626#if DEVICE_TREE
Matthew Qin271927e2015-03-31 22:07:07 -04002627 scratch_offset = image_actual + page_size + out_len;
Amol Jadicb524072012-08-09 16:40:18 -07002628 /* find correct dtb and copy it to right location */
Matthew Qin271927e2015-03-31 22:07:07 -04002629 ret = copy_dtb(data, scratch_offset);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002630
2631 dtb_copied = !ret ? 1 : 0;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002632#else
Mayank Grovere559cec2017-10-17 15:12:03 +05302633 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE) ||
2634 check_ddr_addr_range_bound(hdr->tags_addr, MAX_TAGS_SIZE))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002635 {
Mayank Grovere559cec2017-10-17 15:12:03 +05302636 dprintf(CRITICAL, "Tags addresses are not valid.\n");
lijuang2008ff22016-03-07 17:56:27 +08002637 goto boot_failed;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002638 }
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002639#endif
2640
2641 /* Load ramdisk & kernel */
2642 memmove((void*) hdr->ramdisk_addr, ptr + page_size + kernel_actual, hdr->ramdisk_size);
Matthew Qinbb7923d2015-02-09 10:56:09 +08002643 memmove((void*) hdr->kernel_addr, (char*) (kernel_start_addr), kernel_size);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002644
2645#if DEVICE_TREE
Mayank Grovere559cec2017-10-17 15:12:03 +05302646 if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual) ||
2647 check_ddr_addr_range_bound(hdr->tags_addr, kernel_actual))
Matthew Qinbb7923d2015-02-09 10:56:09 +08002648 {
Mayank Grovere559cec2017-10-17 15:12:03 +05302649 dprintf(CRITICAL, "Tags addresses are not valid.\n");
lijuang2008ff22016-03-07 17:56:27 +08002650 goto boot_failed;
Matthew Qinbb7923d2015-02-09 10:56:09 +08002651 }
2652
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002653 /*
2654 * If dtb is not found look for appended DTB in the kernel.
2655 * If appended dev tree is found, update the atags with
2656 * memory address to the DTB appended location on RAM.
2657 * Else update with the atags address in the kernel header
2658 */
2659 if (!dtb_copied) {
2660 void *dtb;
Matthew Qinbb7923d2015-02-09 10:56:09 +08002661 dtb = dev_tree_appended((void*)(ptr + page_size),
2662 hdr->kernel_size, dtb_offset,
Dima Zavine63e5572013-05-03 12:23:06 -07002663 (void *)hdr->tags_addr);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002664 if (!dtb) {
2665 fastboot_fail("dtb not found");
lijuang2008ff22016-03-07 17:56:27 +08002666 goto boot_failed;
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002667 }
Amol Jadicb524072012-08-09 16:40:18 -07002668 }
2669#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08002670
2671 fastboot_okay("");
Amol Jadi7c4316c2013-10-07 14:19:26 -07002672 fastboot_stop();
Brian Swetland9c4c0752009-01-25 16:23:50 -08002673
Dima Zavin77e41f32013-03-06 16:10:43 -08002674 boot_linux((void*) hdr->kernel_addr, (void*) hdr->tags_addr,
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002675 (const char*) hdr->cmdline, board_machtype(),
2676 (void*) hdr->ramdisk_addr, hdr->ramdisk_size);
lijuang2008ff22016-03-07 17:56:27 +08002677
2678 /* fastboot already stop, it's no need to show fastboot menu */
2679 return;
2680boot_failed:
2681#if FBCON_DISPLAY_MSG
2682 /* revert to fastboot menu if boot failed */
2683 display_fastboot_menu();
2684#endif
2685 return;
Brian Swetland9c4c0752009-01-25 16:23:50 -08002686}
2687
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002688void cmd_erase_nand(const char *arg, void *data, unsigned sz)
Dima Zavin214cc642009-01-26 11:16:21 -08002689{
2690 struct ptentry *ptn;
2691 struct ptable *ptable;
2692
2693 ptable = flash_get_ptable();
2694 if (ptable == NULL) {
2695 fastboot_fail("partition table doesn't exist");
2696 return;
2697 }
2698
2699 ptn = ptable_find(ptable, arg);
2700 if (ptn == NULL) {
2701 fastboot_fail("unknown partition name");
2702 return;
2703 }
2704
2705 if (flash_erase(ptn)) {
2706 fastboot_fail("failed to erase partition");
2707 return;
2708 }
2709 fastboot_okay("");
2710}
2711
Bikas Gurungd48bd242010-09-04 19:54:32 -07002712
2713void cmd_erase_mmc(const char *arg, void *data, unsigned sz)
2714{
2715 unsigned long long ptn = 0;
Oliver Wangcee448d2013-10-22 18:40:13 +08002716 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07002717 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002718 uint8_t lun = 0;
Vijay Kumar Pendoti0b21f462016-05-02 17:09:18 +05302719 char *footer = NULL;
Bikas Gurungd48bd242010-09-04 19:54:32 -07002720
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002721#if VERIFIED_BOOT
2722 if(!strcmp(arg, KEYSTORE_PTN_NAME))
2723 {
2724 if(!device.is_unlocked)
2725 {
2726 fastboot_fail("unlock device to erase keystore");
2727 return;
2728 }
2729 }
2730#endif
2731
Kinson Chikf1a43512011-07-14 11:28:39 -07002732 index = partition_get_index(arg);
2733 ptn = partition_get_offset(index);
Oliver Wangcee448d2013-10-22 18:40:13 +08002734 size = partition_get_size(index);
Neeti Desaica8c9602011-10-06 11:40:00 -07002735
Kinson Chikf1a43512011-07-14 11:28:39 -07002736 if(ptn == 0) {
Neeti Desaica8c9602011-10-06 11:40:00 -07002737 fastboot_fail("Partition table doesn't exist\n");
Bikas Gurungd48bd242010-09-04 19:54:32 -07002738 return;
2739 }
Kun Liang2f1601a2013-08-12 16:29:54 +08002740
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002741 lun = partition_get_lun(index);
2742 mmc_set_lun(lun);
2743
Sridhar Parasuramd7957122015-02-27 11:33:40 -08002744 if (platform_boot_dev_isemmc())
2745 {
2746 if (mmc_erase_card(ptn, size)) {
2747 fastboot_fail("failed to erase partition\n");
2748 return;
2749 }
2750 } else {
2751 BUF_DMA_ALIGN(out, DEFAULT_ERASE_SIZE);
2752 size = partition_get_size(index);
2753 if (size > DEFAULT_ERASE_SIZE)
2754 size = DEFAULT_ERASE_SIZE;
Kun Liang2f1601a2013-08-12 16:29:54 +08002755
Sridhar Parasuramd7957122015-02-27 11:33:40 -08002756 /* Simple inefficient version of erase. Just writing
2757 0 in first several blocks */
2758 if (mmc_write(ptn , size, (unsigned int *)out)) {
2759 fastboot_fail("failed to erase partition");
2760 return;
2761 }
Vijay Kumar Pendoti0b21f462016-05-02 17:09:18 +05302762 /*Erase FDE metadata at the userdata footer*/
2763 if(!(strncmp(arg, "userdata", 8)))
2764 {
2765 footer = memalign(CACHE_LINE, FOOTER_SIZE);
2766 memset((void *)footer, 0, FOOTER_SIZE);
2767
2768 size = partition_get_size(index);
2769
2770 if (mmc_write((ptn + size) - FOOTER_SIZE , FOOTER_SIZE, (unsigned int *)footer)) {
2771 fastboot_fail("failed to erase userdata footer");
2772 free(footer);
2773 return;
2774 }
2775 free(footer);
2776 }
Bikas Gurungd48bd242010-09-04 19:54:32 -07002777 }
Sridhar Parasuram32b30662015-07-10 13:33:22 -07002778#if VERIFIED_BOOT
Mayank Grover912eaa62017-10-26 12:08:53 +05302779 if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05302780 !(strncmp(arg, "userdata", 8)) &&
2781 send_delete_keys_to_tz())
Sridhar Parasuram32b30662015-07-10 13:33:22 -07002782 ASSERT(0);
2783#endif
Bikas Gurungd48bd242010-09-04 19:54:32 -07002784 fastboot_okay("");
2785}
2786
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002787void cmd_erase(const char *arg, void *data, unsigned sz)
2788{
Channagoud Kadabice3cf422015-04-17 16:02:30 -07002789#if VERIFIED_BOOT
Channagoud Kadabi35297672015-06-13 11:09:49 -07002790 if (target_build_variant_user())
Channagoud Kadabice3cf422015-04-17 16:02:30 -07002791 {
Sridhar Parasuramc32d07d2015-07-12 10:57:48 -07002792 if(!device.is_unlocked)
Channagoud Kadabice3cf422015-04-17 16:02:30 -07002793 {
Channagoud Kadabi35297672015-06-13 11:09:49 -07002794 fastboot_fail("device is locked. Cannot erase");
Channagoud Kadabice3cf422015-04-17 16:02:30 -07002795 return;
2796 }
2797 }
2798#endif
2799
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002800 if(target_is_emmc_boot())
2801 cmd_erase_mmc(arg, data, sz);
2802 else
2803 cmd_erase_nand(arg, data, sz);
2804}
Bikas Gurungd48bd242010-09-04 19:54:32 -07002805
Mayank Grover11ff9692018-01-11 11:54:49 +05302806/* Get the size from partiton name */
2807static void get_partition_size(const char *arg, char *response)
2808{
2809 uint64_t ptn = 0;
2810 uint64_t size;
2811 int index = INVALID_PTN;
2812
2813 index = partition_get_index(arg);
2814
2815 if (index == INVALID_PTN)
2816 {
2817 dprintf(CRITICAL, "Invalid partition index\n");
2818 return;
2819 }
2820
2821 ptn = partition_get_offset(index);
2822
2823 if(!ptn)
2824 {
2825 dprintf(CRITICAL, "Invalid partition name %s\n", arg);
2826 return;
2827 }
2828
2829 size = partition_get_size(index);
2830
2831 snprintf(response, MAX_RSP_SIZE, "\t 0x%llx", size);
2832 return;
2833}
2834
2835/*
2836 * Publish the partition type & size info
2837 * fastboot getvar will publish the required information.
2838 * fastboot getvar partition_size:<partition_name>: partition size in hex
2839 * fastboot getvar partition_type:<partition_name>: partition type (ext/fat)
2840 */
2841static void publish_getvar_partition_info(struct getvar_partition_info *info, uint8_t num_parts)
2842{
2843 uint8_t i,n;
2844 static bool published = false;
2845 struct partition_entry *ptn_entry =
2846 partition_get_partition_entries();
2847 memset(info, 0, sizeof(struct getvar_partition_info)* num_parts);
2848
2849 for (i = 0; i < num_parts; i++) {
2850 strlcat(info[i].part_name, (const char *)ptn_entry[i].name, MAX_RSP_SIZE);
2851 strlcat(info[i].getvar_size, "partition-size:", MAX_GET_VAR_NAME_SIZE);
2852 strlcat(info[i].getvar_type, "partition-type:", MAX_GET_VAR_NAME_SIZE);
2853
2854 /* Mark partiton type for known paritions only */
2855 for (n=0; n < ARRAY_SIZE(part_type_known); n++)
2856 {
2857 if (!strncmp(part_type_known[n].part_name, info[i].part_name,
2858 strlen(part_type_known[n].part_name)))
2859 {
2860 strlcat(info[i].type_response,
2861 part_type_known[n].type_response,
2862 MAX_RSP_SIZE);
2863 break;
2864 }
2865 }
2866
2867 get_partition_size(info[i].part_name, info[i].size_response);
2868
2869 if (strlcat(info[i].getvar_size, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
2870 {
2871 dprintf(CRITICAL, "partition size name truncated\n");
2872 return;
2873 }
2874 if (strlcat(info[i].getvar_type, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
2875 {
2876 dprintf(CRITICAL, "partition type name truncated\n");
2877 return;
2878 }
2879
2880 if (!published)
2881 {
2882 /* publish partition size & type info */
2883 fastboot_publish((const char *) info[i].getvar_size, (const char *) info[i].size_response);
2884 fastboot_publish((const char *) info[i].getvar_type, (const char *) info[i].type_response);
2885 }
2886 }
2887 if (!published)
2888 published = true;
2889}
2890
2891
Ajay Dudani5c761132011-04-07 20:19:04 -07002892void cmd_flash_mmc_img(const char *arg, void *data, unsigned sz)
Shashank Mittal23b8f422010-04-16 19:27:21 -07002893{
2894 unsigned long long ptn = 0;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07002895 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07002896 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002897 char *token = NULL;
2898 char *pname = NULL;
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07002899 char *sp;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002900 uint8_t lun = 0;
2901 bool lun_set = false;
Mayank Grover351a75e2017-05-30 20:06:08 +05302902 int current_active_slot = INVALID;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07002903
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07002904 token = strtok_r((char *)arg, ":", &sp);
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002905 pname = token;
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07002906 token = strtok_r(NULL, ":", &sp);
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002907 if(token)
2908 {
2909 lun = atoi(token);
2910 mmc_set_lun(lun);
2911 lun_set = true;
2912 }
2913
Mao Jinlong226f33a2014-07-04 17:24:10 +08002914 if (pname)
Greg Grisco6e754772011-06-23 12:19:39 -07002915 {
Channagoud Kadabiad259832015-05-29 11:14:17 -07002916 if (!strncmp(pname, "frp-unlock", strlen("frp-unlock")))
2917 {
2918 if (!aboot_frp_unlock(pname, data, sz))
2919 {
2920 fastboot_info("FRP unlock successful");
2921 fastboot_okay("");
2922 }
2923 else
2924 fastboot_fail("Secret key is invalid, please update the bootloader with secret key");
2925
2926 return;
2927 }
2928
Mao Jinlong226f33a2014-07-04 17:24:10 +08002929 if (!strcmp(pname, "partition"))
2930 {
2931 dprintf(INFO, "Attempt to write partition image.\n");
2932 if (write_partition(sz, (unsigned char *) data)) {
2933 fastboot_fail("failed to write partition");
Greg Grisco6e754772011-06-23 12:19:39 -07002934 return;
2935 }
Mayank Grover11ff9692018-01-11 11:54:49 +05302936 /* Re-publish partition table */
2937 publish_getvar_partition_info(part_info, partition_get_partition_count());
Mayank Grover351a75e2017-05-30 20:06:08 +05302938
2939 /* Rescan partition table to ensure we have multislot support*/
2940 if (partition_scan_for_multislot())
2941 {
2942 current_active_slot = partition_find_active_slot();
2943 dprintf(INFO, "Multislot supported: Slot %s active",
2944 (SUFFIX_SLOT(current_active_slot)));
2945 }
2946 partition_mark_active_slot(current_active_slot);
Greg Grisco6e754772011-06-23 12:19:39 -07002947 }
Mao Jinlong226f33a2014-07-04 17:24:10 +08002948 else
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002949 {
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002950#if VERIFIED_BOOT
2951 if(!strcmp(pname, KEYSTORE_PTN_NAME))
2952 {
2953 if(!device.is_unlocked)
2954 {
2955 fastboot_fail("unlock device to flash keystore");
2956 return;
2957 }
Gaurav Nebhwanic7313cc2015-12-15 22:25:04 +05302958 if(!boot_verify_validate_keystore((unsigned char *)data,sz))
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002959 {
2960 fastboot_fail("image is not a keystore file");
2961 return;
2962 }
2963 }
2964#endif
Mao Jinlong226f33a2014-07-04 17:24:10 +08002965 index = partition_get_index(pname);
2966 ptn = partition_get_offset(index);
2967 if(ptn == 0) {
2968 fastboot_fail("partition table doesn't exist");
2969 return;
2970 }
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002971
Mayank Grover351a75e2017-05-30 20:06:08 +05302972 if (!strncmp(pname, "boot", strlen("boot"))
2973 || !strcmp(pname, "recovery"))
2974 {
Mao Jinlong226f33a2014-07-04 17:24:10 +08002975 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
2976 fastboot_fail("image is not a boot image");
2977 return;
2978 }
Mayank Grover351a75e2017-05-30 20:06:08 +05302979
2980 /* Reset multislot_partition attributes in case of flashing boot */
2981 if (partition_multislot_is_supported())
2982 {
2983 partition_reset_attributes(index);
2984 }
Mao Jinlong226f33a2014-07-04 17:24:10 +08002985 }
2986
2987 if(!lun_set)
2988 {
2989 lun = partition_get_lun(index);
2990 mmc_set_lun(lun);
2991 }
2992
2993 size = partition_get_size(index);
Gaurav Nebhwani4d2389c2016-03-17 21:10:05 +05302994 if (ROUND_TO_PAGE(sz, mmc_blocksize_mask) > size) {
Mao Jinlong226f33a2014-07-04 17:24:10 +08002995 fastboot_fail("size too large");
2996 return;
2997 }
2998 else if (mmc_write(ptn , sz, (unsigned int *)data)) {
2999 fastboot_fail("flash write failure");
3000 return;
3001 }
Greg Grisco6e754772011-06-23 12:19:39 -07003002 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07003003 }
3004 fastboot_okay("");
3005 return;
3006}
3007
Ajay Dudanide984792015-03-02 09:57:41 -08003008void cmd_flash_meta_img(const char *arg, void *data, unsigned sz)
3009{
3010 int i, images;
3011 meta_header_t *meta_header;
3012 img_header_entry_t *img_header_entry;
Parth Dixit3e2d3032016-03-04 17:11:52 +05303013 /*End of the image address*/
3014 uintptr_t data_end;
3015
3016 if( (UINT_MAX - sz) > (uintptr_t)data )
3017 data_end = (uintptr_t)data + sz;
3018 else
3019 {
3020 fastboot_fail("Cannot flash: image header corrupt");
3021 return;
3022 }
3023
3024 if( data_end < ((uintptr_t)data + sizeof(meta_header_t)))
3025 {
3026 fastboot_fail("Cannot flash: image header corrupt");
3027 return;
3028 }
Ajay Dudanide984792015-03-02 09:57:41 -08003029
lijuang8ee21882016-04-19 16:57:11 +08003030 /* If device is locked:
3031 * Forbid to flash image to avoid the device to bypass the image
3032 * which with "any" name other than bootloader. Because it maybe
3033 * a meta package of all partitions.
3034 */
3035#if VERIFIED_BOOT
3036 if (target_build_variant_user()) {
3037 if (!device.is_unlocked) {
3038 fastboot_fail("Device is locked, meta image flashing is not allowed");
3039 return;
3040 }
Mayank Grover889be1b2017-09-12 20:12:23 +05303041
Mayank Grover912eaa62017-10-26 12:08:53 +05303042 if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05303043 !device.is_unlock_critical)
3044 {
lijuang8ee21882016-04-19 16:57:11 +08003045 fastboot_fail("Device is critical locked, Meta image flashing is not allowed");
3046 return;
3047 }
lijuang8ee21882016-04-19 16:57:11 +08003048 }
3049#endif
3050
Ajay Dudanide984792015-03-02 09:57:41 -08003051 meta_header = (meta_header_t*) data;
Parth Dixit3e2d3032016-03-04 17:11:52 +05303052 if( data_end < ((uintptr_t)data + meta_header->img_hdr_sz))
3053 {
3054 fastboot_fail("Cannot flash: image header corrupt");
3055 return;
3056 }
Ajay Dudanide984792015-03-02 09:57:41 -08003057 img_header_entry = (img_header_entry_t*) (data+sizeof(meta_header_t));
3058
3059 images = meta_header->img_hdr_sz / sizeof(img_header_entry_t);
3060
3061 for (i=0; i<images; i++) {
3062
3063 if((img_header_entry[i].ptn_name == NULL) ||
3064 (img_header_entry[i].start_offset == 0) ||
3065 (img_header_entry[i].size == 0))
3066 break;
Kishor PK49831502017-04-21 17:55:43 +05303067 if ((UINT_MAX - img_header_entry[i].start_offset) < (uintptr_t)data) {
3068 fastboot_fail("Integer overflow detected in start_offset of img");
3069 break;
3070 }
3071 else if ((UINT_MAX - (img_header_entry[i].start_offset + (uintptr_t)data)) < img_header_entry[i].size) {
3072 fastboot_fail("Integer overflow detected in size of img");
3073 break;
3074 }
Parth Dixit3e2d3032016-03-04 17:11:52 +05303075 if( data_end < ((uintptr_t)data + img_header_entry[i].start_offset
3076 + img_header_entry[i].size) )
3077 {
3078 fastboot_fail("Cannot flash: image size mismatch");
3079 break;
3080 }
3081
Ajay Dudanide984792015-03-02 09:57:41 -08003082 cmd_flash_mmc_img(img_header_entry[i].ptn_name,
3083 (void *) data + img_header_entry[i].start_offset,
3084 img_header_entry[i].size);
3085 }
3086
Channagoud Kadabiaafbbfc2015-03-04 17:52:56 -08003087 if (!strncmp(arg, "bootloader", strlen("bootloader")))
3088 {
3089 strlcpy(device.bootloader_version, TARGET(BOARD), MAX_VERSION_LEN);
3090 strlcat(device.bootloader_version, "-", MAX_VERSION_LEN);
3091 strlcat(device.bootloader_version, meta_header->img_version, MAX_VERSION_LEN);
3092 }
3093 else
3094 {
3095 strlcpy(device.radio_version, TARGET(BOARD), MAX_VERSION_LEN);
3096 strlcat(device.radio_version, "-", MAX_VERSION_LEN);
3097 strlcat(device.radio_version, meta_header->img_version, MAX_VERSION_LEN);
3098 }
3099
3100 write_device_info(&device);
Ajay Dudanide984792015-03-02 09:57:41 -08003101 fastboot_okay("");
3102 return;
3103}
3104
Ajay Dudani5c761132011-04-07 20:19:04 -07003105void cmd_flash_mmc_sparse_img(const char *arg, void *data, unsigned sz)
3106{
3107 unsigned int chunk;
wufeng.jiang813dc352015-06-02 23:02:46 -04003108 uint64_t chunk_data_sz;
Channagoud Kadabif627cf82013-09-09 14:08:20 -07003109 uint32_t *fill_buf = NULL;
3110 uint32_t fill_val;
Mayank Grover4f50bba2017-07-19 18:17:08 +05303111 uint32_t blk_sz_actual = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07003112 sparse_header_t *sparse_header;
3113 chunk_header_t *chunk_header;
Ajay Dudaniab18f022011-05-12 14:39:22 -07003114 uint32_t total_blocks = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07003115 unsigned long long ptn = 0;
Channagoud Kadabi65b91002011-10-11 17:34:33 +05303116 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07003117 int index = INVALID_PTN;
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05303118 uint32_t i;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003119 uint8_t lun = 0;
vijay kumar800255e2015-04-24 20:26:19 +05303120 /*End of the sparse image address*/
Parth Dixit64b1a482016-03-07 16:31:26 +05303121 uintptr_t data_end = (uintptr_t)data + sz;
Ajay Dudani5c761132011-04-07 20:19:04 -07003122
Kinson Chikf1a43512011-07-14 11:28:39 -07003123 index = partition_get_index(arg);
3124 ptn = partition_get_offset(index);
3125 if(ptn == 0) {
Ajay Dudani5c761132011-04-07 20:19:04 -07003126 fastboot_fail("partition table doesn't exist");
3127 return;
3128 }
3129
Channagoud Kadabi65b91002011-10-11 17:34:33 +05303130 size = partition_get_size(index);
Channagoud Kadabi65b91002011-10-11 17:34:33 +05303131
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003132 lun = partition_get_lun(index);
3133 mmc_set_lun(lun);
3134
vijay kumar800255e2015-04-24 20:26:19 +05303135 if (sz < sizeof(sparse_header_t)) {
3136 fastboot_fail("size too low");
3137 return;
3138 }
3139
Ajay Dudani5c761132011-04-07 20:19:04 -07003140 /* Read and skip over sparse image header */
3141 sparse_header = (sparse_header_t *) data;
vijay kumar800255e2015-04-24 20:26:19 +05303142
Mayank Grover48bd9bb2017-07-19 12:04:16 +05303143 if (!sparse_header->blk_sz || (sparse_header->blk_sz % 4)){
3144 fastboot_fail("Invalid block size\n");
3145 return;
3146 }
3147
vijay kumar1321f342015-03-27 12:13:42 +05303148 if (((uint64_t)sparse_header->total_blks * (uint64_t)sparse_header->blk_sz) > size) {
Ajay Dudani876b3282012-12-21 14:12:17 -08003149 fastboot_fail("size too large");
3150 return;
3151 }
3152
vijay kumarde4fcf62015-04-23 13:05:49 +05303153 data += sizeof(sparse_header_t);
vijay kumar800255e2015-04-24 20:26:19 +05303154
Parth Dixit64b1a482016-03-07 16:31:26 +05303155 if (data_end < (uintptr_t)data) {
vijay kumar800255e2015-04-24 20:26:19 +05303156 fastboot_fail("buffer overreads occured due to invalid sparse header");
3157 return;
3158 }
3159
vijay kumarde4fcf62015-04-23 13:05:49 +05303160 if(sparse_header->file_hdr_sz != sizeof(sparse_header_t))
Ajay Dudani5c761132011-04-07 20:19:04 -07003161 {
vijay kumarde4fcf62015-04-23 13:05:49 +05303162 fastboot_fail("sparse header size mismatch");
3163 return;
Ajay Dudani5c761132011-04-07 20:19:04 -07003164 }
3165
Ajay Dudanib06c05f2011-05-12 14:46:10 -07003166 dprintf (SPEW, "=== Sparse Image Header ===\n");
3167 dprintf (SPEW, "magic: 0x%x\n", sparse_header->magic);
3168 dprintf (SPEW, "major_version: 0x%x\n", sparse_header->major_version);
3169 dprintf (SPEW, "minor_version: 0x%x\n", sparse_header->minor_version);
3170 dprintf (SPEW, "file_hdr_sz: %d\n", sparse_header->file_hdr_sz);
3171 dprintf (SPEW, "chunk_hdr_sz: %d\n", sparse_header->chunk_hdr_sz);
3172 dprintf (SPEW, "blk_sz: %d\n", sparse_header->blk_sz);
3173 dprintf (SPEW, "total_blks: %d\n", sparse_header->total_blks);
3174 dprintf (SPEW, "total_chunks: %d\n", sparse_header->total_chunks);
Ajay Dudani5c761132011-04-07 20:19:04 -07003175
3176 /* Start processing chunks */
3177 for (chunk=0; chunk<sparse_header->total_chunks; chunk++)
3178 {
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05303179 /* Make sure the total image size does not exceed the partition size */
3180 if(((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz) >= size) {
3181 fastboot_fail("size too large");
3182 return;
3183 }
Ajay Dudani5c761132011-04-07 20:19:04 -07003184 /* Read and skip over chunk header */
3185 chunk_header = (chunk_header_t *) data;
3186 data += sizeof(chunk_header_t);
3187
Parth Dixit64b1a482016-03-07 16:31:26 +05303188 if (data_end < (uintptr_t)data) {
vijay kumar800255e2015-04-24 20:26:19 +05303189 fastboot_fail("buffer overreads occured due to invalid sparse header");
3190 return;
3191 }
3192
Ajay Dudani5c761132011-04-07 20:19:04 -07003193 dprintf (SPEW, "=== Chunk Header ===\n");
3194 dprintf (SPEW, "chunk_type: 0x%x\n", chunk_header->chunk_type);
3195 dprintf (SPEW, "chunk_data_sz: 0x%x\n", chunk_header->chunk_sz);
3196 dprintf (SPEW, "total_size: 0x%x\n", chunk_header->total_sz);
3197
vijay kumar800255e2015-04-24 20:26:19 +05303198 if(sparse_header->chunk_hdr_sz != sizeof(chunk_header_t))
Ajay Dudani5c761132011-04-07 20:19:04 -07003199 {
vijay kumar800255e2015-04-24 20:26:19 +05303200 fastboot_fail("chunk header size mismatch");
3201 return;
Ajay Dudani5c761132011-04-07 20:19:04 -07003202 }
3203
wufeng.jiang813dc352015-06-02 23:02:46 -04003204 chunk_data_sz = (uint64_t)sparse_header->blk_sz * chunk_header->chunk_sz;
3205
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05303206 /* Make sure that the chunk size calculated from sparse image does not
3207 * exceed partition size
3208 */
3209 if ((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz + chunk_data_sz > size)
3210 {
3211 fastboot_fail("Chunk data size exceeds partition size");
3212 return;
3213 }
3214
Ajay Dudani5c761132011-04-07 20:19:04 -07003215 switch (chunk_header->chunk_type)
3216 {
3217 case CHUNK_TYPE_RAW:
wufeng.jiang813dc352015-06-02 23:02:46 -04003218 if((uint64_t)chunk_header->total_sz != ((uint64_t)sparse_header->chunk_hdr_sz +
Ajay Dudani5c761132011-04-07 20:19:04 -07003219 chunk_data_sz))
3220 {
3221 fastboot_fail("Bogus chunk size for chunk type Raw");
3222 return;
3223 }
3224
Parth Dixit64b1a482016-03-07 16:31:26 +05303225 if (data_end < (uintptr_t)data + chunk_data_sz) {
vijay kumar800255e2015-04-24 20:26:19 +05303226 fastboot_fail("buffer overreads occured due to invalid sparse header");
3227 return;
3228 }
3229
wufeng.jiang813dc352015-06-02 23:02:46 -04003230 /* chunk_header->total_sz is uint32,So chunk_data_sz is now less than 2^32
3231 otherwise it will return in the line above
3232 */
Ajay Dudaniab18f022011-05-12 14:39:22 -07003233 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
wufeng.jiang813dc352015-06-02 23:02:46 -04003234 (uint32_t)chunk_data_sz,
Ajay Dudaniab18f022011-05-12 14:39:22 -07003235 (unsigned int*)data))
Ajay Dudani5c761132011-04-07 20:19:04 -07003236 {
3237 fastboot_fail("flash write failure");
3238 return;
3239 }
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05303240 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
3241 fastboot_fail("Bogus size for RAW chunk type");
3242 return;
3243 }
Ajay Dudani5c761132011-04-07 20:19:04 -07003244 total_blocks += chunk_header->chunk_sz;
wufeng.jiang813dc352015-06-02 23:02:46 -04003245 data += (uint32_t)chunk_data_sz;
Ajay Dudani5c761132011-04-07 20:19:04 -07003246 break;
3247
Channagoud Kadabif627cf82013-09-09 14:08:20 -07003248 case CHUNK_TYPE_FILL:
3249 if(chunk_header->total_sz != (sparse_header->chunk_hdr_sz +
3250 sizeof(uint32_t)))
3251 {
3252 fastboot_fail("Bogus chunk size for chunk type FILL");
3253 return;
3254 }
3255
Mayank Grover4f50bba2017-07-19 18:17:08 +05303256 blk_sz_actual = ROUNDUP(sparse_header->blk_sz, CACHE_LINE);
3257 /* Integer overflow detected */
3258 if (blk_sz_actual < sparse_header->blk_sz)
3259 {
3260 fastboot_fail("Invalid block size");
3261 return;
3262 }
3263
3264 fill_buf = (uint32_t *)memalign(CACHE_LINE, blk_sz_actual);
Channagoud Kadabif627cf82013-09-09 14:08:20 -07003265 if (!fill_buf)
3266 {
3267 fastboot_fail("Malloc failed for: CHUNK_TYPE_FILL");
3268 return;
3269 }
3270
Parth Dixit64b1a482016-03-07 16:31:26 +05303271 if (data_end < (uintptr_t)data + sizeof(uint32_t)) {
vijay kumar800255e2015-04-24 20:26:19 +05303272 fastboot_fail("buffer overreads occured due to invalid sparse header");
Gaurav Nebhwani163cbf82016-03-17 21:21:27 +05303273 free(fill_buf);
vijay kumar800255e2015-04-24 20:26:19 +05303274 return;
3275 }
Channagoud Kadabif627cf82013-09-09 14:08:20 -07003276 fill_val = *(uint32_t *)data;
3277 data = (char *) data + sizeof(uint32_t);
Channagoud Kadabif627cf82013-09-09 14:08:20 -07003278
3279 for (i = 0; i < (sparse_header->blk_sz / sizeof(fill_val)); i++)
3280 {
3281 fill_buf[i] = fill_val;
3282 }
3283
Gaurav Nebhwanie94cbe12016-03-17 21:16:34 +05303284 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz))
3285 {
3286 fastboot_fail("bogus size for chunk FILL type");
3287 free(fill_buf);
3288 return;
3289 }
3290
wufeng.jiang813dc352015-06-02 23:02:46 -04003291 for (i = 0; i < chunk_header->chunk_sz; i++)
Channagoud Kadabif627cf82013-09-09 14:08:20 -07003292 {
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05303293 /* Make sure that the data written to partition does not exceed partition size */
3294 if ((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz + sparse_header->blk_sz > size)
3295 {
3296 fastboot_fail("Chunk data size for fill type exceeds partition size");
Gaurav Nebhwani163cbf82016-03-17 21:21:27 +05303297 free(fill_buf);
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05303298 return;
3299 }
3300
Channagoud Kadabif627cf82013-09-09 14:08:20 -07003301 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
3302 sparse_header->blk_sz,
3303 fill_buf))
3304 {
3305 fastboot_fail("flash write failure");
3306 free(fill_buf);
3307 return;
3308 }
3309
3310 total_blocks++;
3311 }
3312
3313 free(fill_buf);
3314 break;
3315
Ajay Dudani5c761132011-04-07 20:19:04 -07003316 case CHUNK_TYPE_DONT_CARE:
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05303317 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
3318 fastboot_fail("bogus size for chunk DONT CARE type");
3319 return;
3320 }
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07003321 total_blocks += chunk_header->chunk_sz;
3322 break;
3323
Ajay Dudani5c761132011-04-07 20:19:04 -07003324 case CHUNK_TYPE_CRC:
3325 if(chunk_header->total_sz != sparse_header->chunk_hdr_sz)
3326 {
wufeng.jiang813dc352015-06-02 23:02:46 -04003327 fastboot_fail("Bogus chunk size for chunk type CRC");
Ajay Dudani5c761132011-04-07 20:19:04 -07003328 return;
3329 }
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05303330 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
3331 fastboot_fail("bogus size for chunk CRC type");
3332 return;
3333 }
Ajay Dudani5c761132011-04-07 20:19:04 -07003334 total_blocks += chunk_header->chunk_sz;
Parth Dixit64b1a482016-03-07 16:31:26 +05303335 if ((uintptr_t)data > UINT_MAX - chunk_data_sz) {
vijay kumar800255e2015-04-24 20:26:19 +05303336 fastboot_fail("integer overflow occured");
3337 return;
3338 }
wufeng.jiang813dc352015-06-02 23:02:46 -04003339 data += (uint32_t)chunk_data_sz;
Parth Dixit64b1a482016-03-07 16:31:26 +05303340 if (data_end < (uintptr_t)data) {
vijay kumar800255e2015-04-24 20:26:19 +05303341 fastboot_fail("buffer overreads occured due to invalid sparse header");
3342 return;
3343 }
Ajay Dudani5c761132011-04-07 20:19:04 -07003344 break;
3345
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07003346 default:
Channagoud Kadabif627cf82013-09-09 14:08:20 -07003347 dprintf(CRITICAL, "Unkown chunk type: %x\n",chunk_header->chunk_type);
Ajay Dudani5c761132011-04-07 20:19:04 -07003348 fastboot_fail("Unknown chunk type");
3349 return;
3350 }
3351 }
3352
Ajay Dudani0c6927b2011-05-18 11:12:16 -07003353 dprintf(INFO, "Wrote %d blocks, expected to write %d blocks\n",
3354 total_blocks, sparse_header->total_blks);
3355
3356 if(total_blocks != sparse_header->total_blks)
3357 {
3358 fastboot_fail("sparse image write failure");
3359 }
Ajay Dudani5c761132011-04-07 20:19:04 -07003360
3361 fastboot_okay("");
3362 return;
3363}
3364
3365void cmd_flash_mmc(const char *arg, void *data, unsigned sz)
3366{
3367 sparse_header_t *sparse_header;
Ajay Dudanide984792015-03-02 09:57:41 -08003368 meta_header_t *meta_header;
Ajay Dudani5c761132011-04-07 20:19:04 -07003369
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07003370#ifdef SSD_ENABLE
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -08003371 /* 8 Byte Magic + 2048 Byte xml + Encrypted Data */
3372 unsigned int *magic_number = (unsigned int *) data;
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08003373 int ret=0;
3374 uint32 major_version=0;
3375 uint32 minor_version=0;
3376
3377 ret = scm_svc_version(&major_version,&minor_version);
3378 if(!ret)
3379 {
3380 if(major_version >= 2)
3381 {
Amir Samuelovbb65ce02013-05-05 12:20:18 +03003382 if( !strcmp(arg, "ssd") || !strcmp(arg, "tqs") )
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08003383 {
3384 ret = encrypt_scm((uint32 **) &data, &sz);
3385 if (ret != 0) {
3386 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
3387 return;
3388 }
3389
Amir Samuelovbb65ce02013-05-05 12:20:18 +03003390 /* Protect only for SSD */
3391 if (!strcmp(arg, "ssd")) {
3392 ret = scm_protect_keystore((uint32 *) data, sz);
3393 if (ret != 0) {
3394 dprintf(CRITICAL, "ERROR: scm_protect_keystore Failed\n");
3395 return;
3396 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08003397 }
3398 }
3399 else
3400 {
3401 ret = decrypt_scm_v2((uint32 **) &data, &sz);
3402 if(ret != 0)
3403 {
3404 dprintf(CRITICAL,"ERROR: Decryption Failure\n");
3405 return;
3406 }
3407 }
3408 }
3409 else
3410 {
3411 if (magic_number[0] == DECRYPT_MAGIC_0 &&
3412 magic_number[1] == DECRYPT_MAGIC_1)
3413 {
3414 ret = decrypt_scm((uint32 **) &data, &sz);
3415 if (ret != 0) {
3416 dprintf(CRITICAL, "ERROR: Invalid secure image\n");
3417 return;
3418 }
3419 }
3420 else if (magic_number[0] == ENCRYPT_MAGIC_0 &&
3421 magic_number[1] == ENCRYPT_MAGIC_1)
3422 {
3423 ret = encrypt_scm((uint32 **) &data, &sz);
3424 if (ret != 0) {
3425 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
3426 return;
3427 }
3428 }
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07003429 }
3430 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08003431 else
Neeti Desai127b9e02012-03-20 16:11:23 -07003432 {
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08003433 dprintf(CRITICAL,"INVALID SVC Version\n");
3434 return;
Neeti Desai127b9e02012-03-20 16:11:23 -07003435 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08003436#endif /* SSD_ENABLE */
Neeti Desai127b9e02012-03-20 16:11:23 -07003437
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003438#if VERIFIED_BOOT
Channagoud Kadabi35297672015-06-13 11:09:49 -07003439 if (target_build_variant_user())
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003440 {
lijuang511a2b52015-08-14 20:50:51 +08003441 /* if device is locked:
3442 * common partition will not allow to be flashed
3443 * critical partition will allow to flash image.
3444 */
3445 if(!device.is_unlocked && !critical_flash_allowed(arg)) {
3446 fastboot_fail("Partition flashing is not allowed");
3447 return;
3448 }
Mayank Grover889be1b2017-09-12 20:12:23 +05303449
lijuang511a2b52015-08-14 20:50:51 +08003450 /* if device critical is locked:
3451 * common partition will allow to be flashed
3452 * critical partition will not allow to flash image.
3453 */
Mayank Grover912eaa62017-10-26 12:08:53 +05303454 if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05303455 !device.is_unlock_critical &&
3456 critical_flash_allowed(arg)) {
3457 fastboot_fail("Critical partition flashing is not allowed");
3458 return;
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003459 }
3460 }
3461#endif
3462
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07003463 sparse_header = (sparse_header_t *) data;
Ajay Dudanide984792015-03-02 09:57:41 -08003464 meta_header = (meta_header_t *) data;
3465 if (sparse_header->magic == SPARSE_HEADER_MAGIC)
Ajay Dudani5c761132011-04-07 20:19:04 -07003466 cmd_flash_mmc_sparse_img(arg, data, sz);
Ajay Dudanide984792015-03-02 09:57:41 -08003467 else if (meta_header->magic == META_HEADER_MAGIC)
3468 cmd_flash_meta_img(arg, data, sz);
3469 else
3470 cmd_flash_mmc_img(arg, data, sz);
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07003471
3472#if VERIFIED_BOOT
Mayank Grover912eaa62017-10-26 12:08:53 +05303473 if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05303474 (!strncmp(arg, "system", 6)) &&
3475 !device.verity_mode)
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07003476 // reset dm_verity mode to enforcing
3477 device.verity_mode = 1;
3478 write_device_info(&device);
Parth Dixitddbc7352015-10-18 03:13:31 +05303479#endif
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07003480
Ajay Dudani5c761132011-04-07 20:19:04 -07003481 return;
3482}
3483
Tanya Brokhman84eaaf62015-02-24 21:57:06 +02003484void cmd_updatevol(const char *vol_name, void *data, unsigned sz)
3485{
3486 struct ptentry *sys_ptn;
3487 struct ptable *ptable;
3488
3489 ptable = flash_get_ptable();
3490 if (ptable == NULL) {
3491 fastboot_fail("partition table doesn't exist");
3492 return;
3493 }
3494
3495 sys_ptn = ptable_find(ptable, "system");
3496 if (sys_ptn == NULL) {
3497 fastboot_fail("system partition not found");
3498 return;
3499 }
3500
3501 sz = ROUND_TO_PAGE(sz, page_mask);
3502 if (update_ubi_vol(sys_ptn, vol_name, data, sz))
3503 fastboot_fail("update_ubi_vol failed");
3504 else
3505 fastboot_okay("");
3506}
3507
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003508void cmd_flash_nand(const char *arg, void *data, unsigned sz)
Dima Zavin214cc642009-01-26 11:16:21 -08003509{
3510 struct ptentry *ptn;
3511 struct ptable *ptable;
3512 unsigned extra = 0;
Gaurav Nebhwanid9730712016-05-06 14:28:29 +05303513 uint64_t partition_size = 0;
Vijay Kumar Pendotic6f22a72017-05-19 22:44:34 +05303514 unsigned bytes_to_round_page = 0;
3515 unsigned rounded_size = 0;
Dima Zavin214cc642009-01-26 11:16:21 -08003516
Kishor PK38ed93d2017-04-25 14:19:26 +05303517 if((uintptr_t)data > (UINT_MAX - sz)) {
3518 fastboot_fail("Cannot flash: image header corrupt");
3519 return;
3520 }
3521
Dima Zavin214cc642009-01-26 11:16:21 -08003522 ptable = flash_get_ptable();
3523 if (ptable == NULL) {
3524 fastboot_fail("partition table doesn't exist");
3525 return;
3526 }
3527
3528 ptn = ptable_find(ptable, arg);
3529 if (ptn == NULL) {
Tanya Brokhman84eaaf62015-02-24 21:57:06 +02003530 dprintf(INFO, "unknown partition name (%s). Trying updatevol\n",
3531 arg);
3532 cmd_updatevol(arg, data, sz);
Dima Zavin214cc642009-01-26 11:16:21 -08003533 return;
3534 }
3535
3536 if (!strcmp(ptn->name, "boot") || !strcmp(ptn->name, "recovery")) {
Kishor PK38ed93d2017-04-25 14:19:26 +05303537 if((sz > BOOT_MAGIC_SIZE) && (!memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE))) {
3538 dprintf(INFO, "Verified the BOOT_MAGIC in image header \n");
3539 } else {
3540 fastboot_fail("Image is not a boot image");
Dima Zavin214cc642009-01-26 11:16:21 -08003541 return;
3542 }
3543 }
3544
Amol Jadi5c61a952012-05-04 17:05:35 -07003545 if (!strcmp(ptn->name, "system")
Deepa Dinamani13e32c42012-03-12 14:34:17 -07003546 || !strcmp(ptn->name, "userdata")
3547 || !strcmp(ptn->name, "persist")
Sundarajan Srinivasanb063a852013-11-19 14:02:27 -08003548 || !strcmp(ptn->name, "recoveryfs")
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -08003549 || !strcmp(ptn->name, "modem"))
Tanya Brokhman1c94f1a2015-02-15 09:05:03 +02003550 extra = 1;
Kishor PK38ed93d2017-04-25 14:19:26 +05303551 else {
Vijay Kumar Pendotic6f22a72017-05-19 22:44:34 +05303552 rounded_size = ROUNDUP(sz, page_size);
3553 bytes_to_round_page = rounded_size - sz;
3554 if (bytes_to_round_page) {
3555 if (((uintptr_t)data + sz ) > (UINT_MAX - bytes_to_round_page)) {
3556 fastboot_fail("Integer overflow detected");
3557 return;
3558 }
3559 if (((uintptr_t)data + sz + bytes_to_round_page) >
3560 ((uintptr_t)target_get_scratch_address() + target_get_max_flash_size())) {
3561 fastboot_fail("Buffer size is not aligned to page_size");
3562 return;
3563 }
3564 else {
3565 memset(data + sz, 0, bytes_to_round_page);
3566 sz = rounded_size;
3567 }
Kishor PK38ed93d2017-04-25 14:19:26 +05303568 }
Gaurav Nebhwanid9730712016-05-06 14:28:29 +05303569 }
3570
Kishor PK38ed93d2017-04-25 14:19:26 +05303571 /*Checking partition_size for the possible integer overflow */
3572 partition_size = validate_partition_size(ptn);
3573
Gaurav Nebhwanid9730712016-05-06 14:28:29 +05303574 if (sz > partition_size) {
3575 fastboot_fail("Image size too large");
3576 return;
3577 }
3578
Dima Zavin214cc642009-01-26 11:16:21 -08003579 dprintf(INFO, "writing %d bytes to '%s'\n", sz, ptn->name);
Mayank Grover6cde9352017-06-06 18:45:23 +05303580 if ((sz > UBI_EC_HDR_SIZE) &&
3581 (!memcmp((void *)data, UBI_MAGIC, UBI_MAGIC_SIZE))) {
Tanya Brokhman1c94f1a2015-02-15 09:05:03 +02003582 if (flash_ubi_img(ptn, data, sz)) {
3583 fastboot_fail("flash write failure");
3584 return;
3585 }
3586 } else {
3587 if (flash_write(ptn, extra, data, sz)) {
3588 fastboot_fail("flash write failure");
3589 return;
3590 }
Dima Zavin214cc642009-01-26 11:16:21 -08003591 }
3592 dprintf(INFO, "partition '%s' updated\n", ptn->name);
3593 fastboot_okay("");
3594}
3595
Kishor PK38ed93d2017-04-25 14:19:26 +05303596
3597static inline uint64_t validate_partition_size(struct ptentry *ptn)
3598{
3599 if (ptn->length && flash_num_pages_per_blk() && page_size) {
3600 if ((ptn->length < ( UINT_MAX / flash_num_pages_per_blk())) && ((ptn->length * flash_num_pages_per_blk()) < ( UINT_MAX / page_size))) {
3601 return ptn->length * flash_num_pages_per_blk() * page_size;
3602 }
3603 }
3604 return 0;
3605}
3606
3607
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003608void cmd_flash(const char *arg, void *data, unsigned sz)
3609{
3610 if(target_is_emmc_boot())
3611 cmd_flash_mmc(arg, data, sz);
3612 else
3613 cmd_flash_nand(arg, data, sz);
3614}
3615
Dima Zavin214cc642009-01-26 11:16:21 -08003616void cmd_continue(const char *arg, void *data, unsigned sz)
3617{
3618 fastboot_okay("");
Amol Jadi7c4316c2013-10-07 14:19:26 -07003619 fastboot_stop();
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003620
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003621 if (target_is_emmc_boot())
3622 {
lijuanga40d6302015-07-20 20:10:13 +08003623#if FBCON_DISPLAY_MSG
lijuangde34d502016-02-26 16:04:50 +08003624 /* Exit keys' detection thread firstly */
3625 exit_menu_keys_detection();
lijuanga40d6302015-07-20 20:10:13 +08003626#endif
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003627 boot_linux_from_mmc();
3628 }
3629 else
3630 {
3631 boot_linux_from_flash();
3632 }
Dima Zavin214cc642009-01-26 11:16:21 -08003633}
3634
Chandan Uddaraju94183c02010-01-15 15:13:59 -08003635void cmd_reboot(const char *arg, void *data, unsigned sz)
3636{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003637 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08003638 fastboot_okay("");
3639 reboot_device(0);
3640}
3641
Mayank Grover351a75e2017-05-30 20:06:08 +05303642void cmd_set_active(const char *arg, void *data, unsigned sz)
3643{
Mayank Grover6ccc1c92017-07-04 17:36:46 +05303644 char *p, *sp = NULL;
Mayank Grover351a75e2017-05-30 20:06:08 +05303645 unsigned i,current_active_slot;
3646 const char *current_slot_suffix;
3647
3648 if (!partition_multislot_is_supported())
3649 {
3650 fastboot_fail("Command not supported");
3651 return;
3652 }
3653
3654 if (arg)
3655 {
Mayank Grover6ccc1c92017-07-04 17:36:46 +05303656 p = strtok_r((char *)arg, ":", &sp);
Mayank Grover5eb5f692017-07-19 20:16:04 +05303657 if (p)
Mayank Grover351a75e2017-05-30 20:06:08 +05303658 {
3659 current_active_slot = partition_find_active_slot();
3660
3661 /* Check if trying to make curent slot active */
3662 current_slot_suffix = SUFFIX_SLOT(current_active_slot);
Mayank Grover6ccc1c92017-07-04 17:36:46 +05303663 current_slot_suffix = strtok_r((char *)current_slot_suffix,
3664 (char *)suffix_delimiter, &sp);
3665
Mayank Grover5eb5f692017-07-19 20:16:04 +05303666 if (current_slot_suffix &&
3667 !strncmp(p, current_slot_suffix, strlen(current_slot_suffix)))
Mayank Grover351a75e2017-05-30 20:06:08 +05303668 {
3669 fastboot_okay("Slot already set active");
3670 return;
3671 }
3672 else
3673 {
3674 for (i = 0; i < AB_SUPPORTED_SLOTS; i++)
3675 {
3676 current_slot_suffix = SUFFIX_SLOT(i);
Mayank Grover6ccc1c92017-07-04 17:36:46 +05303677 current_slot_suffix = strtok_r((char *)current_slot_suffix,
3678 (char *)suffix_delimiter, &sp);
Mayank Grover5eb5f692017-07-19 20:16:04 +05303679 if (current_slot_suffix &&
3680 !strncmp(p, current_slot_suffix, strlen(current_slot_suffix)))
Mayank Grover351a75e2017-05-30 20:06:08 +05303681 {
3682 partition_switch_slots(current_active_slot, i);
3683 publish_getvar_multislot_vars();
3684 fastboot_okay("");
3685 return;
3686 }
3687 }
3688 }
3689 }
3690 }
3691 fastboot_fail("Invalid slot suffix.");
3692 return;
3693}
3694
Chandan Uddaraju94183c02010-01-15 15:13:59 -08003695void cmd_reboot_bootloader(const char *arg, void *data, unsigned sz)
3696{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003697 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08003698 fastboot_okay("");
3699 reboot_device(FASTBOOT_MODE);
3700}
3701
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003702void cmd_oem_enable_charger_screen(const char *arg, void *data, unsigned size)
3703{
3704 dprintf(INFO, "Enabling charger screen check\n");
3705 device.charger_screen_enabled = 1;
3706 write_device_info(&device);
3707 fastboot_okay("");
3708}
3709
3710void cmd_oem_disable_charger_screen(const char *arg, void *data, unsigned size)
3711{
3712 dprintf(INFO, "Disabling charger screen check\n");
3713 device.charger_screen_enabled = 0;
3714 write_device_info(&device);
3715 fastboot_okay("");
3716}
3717
lijuanga25d8bb2015-09-16 13:11:52 +08003718void cmd_oem_off_mode_charger(const char *arg, void *data, unsigned size)
3719{
3720 char *p = NULL;
3721 const char *delim = " \t\n\r";
Parth Dixit407f15b2016-01-07 14:47:37 +05303722 char *sp;
lijuanga25d8bb2015-09-16 13:11:52 +08003723
3724 if (arg) {
Parth Dixit407f15b2016-01-07 14:47:37 +05303725 p = strtok_r((char *)arg, delim, &sp);
lijuanga25d8bb2015-09-16 13:11:52 +08003726 if (p) {
3727 if (!strncmp(p, "0", 1)) {
3728 device.charger_screen_enabled = 0;
3729 } else if (!strncmp(p, "1", 1)) {
3730 device.charger_screen_enabled = 1;
3731 }
3732 }
3733 }
3734
3735 /* update charger_screen_enabled value for getvar
3736 * command
3737 */
3738 snprintf(charger_screen_enabled, MAX_RSP_SIZE, "%d",
3739 device.charger_screen_enabled);
3740
3741 write_device_info(&device);
3742 fastboot_okay("");
3743}
3744
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05303745void cmd_oem_select_display_panel(const char *arg, void *data, unsigned size)
3746{
3747 dprintf(INFO, "Selecting display panel %s\n", arg);
3748 if (arg)
3749 strlcpy(device.display_panel, arg,
3750 sizeof(device.display_panel));
3751 write_device_info(&device);
3752 fastboot_okay("");
3753}
3754
Shashank Mittal162244e2011-08-08 19:01:25 -07003755void cmd_oem_unlock(const char *arg, void *data, unsigned sz)
3756{
lijuang4ece1e72015-08-14 21:02:36 +08003757 set_device_unlock(UNLOCK, TRUE);
vijay kumarc65876c2015-04-24 13:29:16 +05303758}
3759
3760void cmd_oem_unlock_go(const char *arg, void *data, unsigned sz)
3761{
lijuang4ece1e72015-08-14 21:02:36 +08003762 if(!device.is_unlocked) {
vijay kumarc65876c2015-04-24 13:29:16 +05303763 if(!is_allow_unlock) {
3764 fastboot_fail("oem unlock is not allowed");
3765 return;
3766 }
3767
lijuang4ece1e72015-08-14 21:02:36 +08003768 set_device_unlock_value(UNLOCK, TRUE);
vijay kumarc65876c2015-04-24 13:29:16 +05303769
lijuang4ece1e72015-08-14 21:02:36 +08003770 /* wipe data */
vijay kumarc65876c2015-04-24 13:29:16 +05303771 struct recovery_message msg;
Kishor PK60a68212017-05-08 16:55:57 +05303772 memset(&msg, 0, sizeof(msg));
vijay kumarc65876c2015-04-24 13:29:16 +05303773 snprintf(msg.recovery, sizeof(msg.recovery), "recovery\n--wipe_data");
3774 write_misc(0, &msg, sizeof(msg));
3775
3776 fastboot_okay("");
3777 reboot_device(RECOVERY_MODE);
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003778 }
3779 fastboot_okay("");
3780}
3781
Channagoud Kadabiad259832015-05-29 11:14:17 -07003782static int aboot_frp_unlock(char *pname, void *data, unsigned sz)
3783{
Mayank Grover70f8b0e2017-05-17 11:49:00 +05303784 int ret=1;
3785 bool authentication_success=false;
Channagoud Kadabiad259832015-05-29 11:14:17 -07003786
Mayank Grover70f8b0e2017-05-17 11:49:00 +05303787 /*
3788 Authentication method not implemented.
3789
3790 OEM to implement, authentication system which on successful validataion,
3791 calls write_allow_oem_unlock() with is_allow_unlock.
3792 */
3793#if 0
3794 authentication_success = oem_specific_auth_mthd();
3795#endif
3796
3797 if (authentication_success)
Channagoud Kadabiad259832015-05-29 11:14:17 -07003798 {
Mayank Grover70f8b0e2017-05-17 11:49:00 +05303799 is_allow_unlock = true;
3800 write_allow_oem_unlock(is_allow_unlock);
3801 ret = 0;
Channagoud Kadabiad259832015-05-29 11:14:17 -07003802 }
3803 return ret;
3804}
3805
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003806void cmd_oem_lock(const char *arg, void *data, unsigned sz)
3807{
lijuang4ece1e72015-08-14 21:02:36 +08003808 set_device_unlock(UNLOCK, FALSE);
Shashank Mittal162244e2011-08-08 19:01:25 -07003809}
3810
Shashank Mittala0032282011-08-26 14:50:11 -07003811void cmd_oem_devinfo(const char *arg, void *data, unsigned sz)
3812{
lijuang511a2b52015-08-14 20:50:51 +08003813 char response[MAX_RSP_SIZE];
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003814 snprintf(response, sizeof(response), "\tDevice tampered: %s", (device.is_tampered ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07003815 fastboot_info(response);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003816 snprintf(response, sizeof(response), "\tDevice unlocked: %s", (device.is_unlocked ? "true" : "false"));
3817 fastboot_info(response);
Mayank Grover889be1b2017-09-12 20:12:23 +05303818#if VERIFIED_BOOT
Mayank Grover912eaa62017-10-26 12:08:53 +05303819 if (VB_M <= target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +05303820 {
3821 snprintf(response, sizeof(response), "\tDevice critical unlocked: %s",
3822 (device.is_unlock_critical ? "true" : "false"));
3823 fastboot_info(response);
3824 }
Parth Dixitddbc7352015-10-18 03:13:31 +05303825#endif
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003826 snprintf(response, sizeof(response), "\tCharger screen enabled: %s", (device.charger_screen_enabled ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07003827 fastboot_info(response);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05303828 snprintf(response, sizeof(response), "\tDisplay panel: %s", (device.display_panel));
3829 fastboot_info(response);
Shashank Mittala0032282011-08-26 14:50:11 -07003830 fastboot_okay("");
3831}
3832
lijuang511a2b52015-08-14 20:50:51 +08003833void cmd_flashing_get_unlock_ability(const char *arg, void *data, unsigned sz)
3834{
3835 char response[MAX_RSP_SIZE];
3836 snprintf(response, sizeof(response), "\tget_unlock_ability: %d", is_allow_unlock);
3837 fastboot_info(response);
3838 fastboot_okay("");
3839}
3840
3841void cmd_flashing_lock_critical(const char *arg, void *data, unsigned sz)
3842{
lijuang4ece1e72015-08-14 21:02:36 +08003843 set_device_unlock(UNLOCK_CRITICAL, FALSE);
lijuang511a2b52015-08-14 20:50:51 +08003844}
3845
3846void cmd_flashing_unlock_critical(const char *arg, void *data, unsigned sz)
3847{
lijuang4ece1e72015-08-14 21:02:36 +08003848 set_device_unlock(UNLOCK_CRITICAL, TRUE);
lijuang511a2b52015-08-14 20:50:51 +08003849}
3850
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07003851void cmd_preflash(const char *arg, void *data, unsigned sz)
3852{
3853 fastboot_okay("");
3854}
3855
Mao Flynn7b379f32015-04-20 00:28:30 +08003856static uint8_t logo_header[LOGO_IMG_HEADER_SIZE];
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303857
Mao Flynn7b379f32015-04-20 00:28:30 +08003858int splash_screen_check_header(logo_img_header *header)
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303859{
Mao Flynn7b379f32015-04-20 00:28:30 +08003860 if (memcmp(header->magic, LOGO_IMG_MAGIC, 8))
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303861 return -1;
Mao Flynn7b379f32015-04-20 00:28:30 +08003862 if (header->width == 0 || header->height == 0)
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303863 return -1;
3864 return 0;
3865}
3866
Mao Flynn7b379f32015-04-20 00:28:30 +08003867int splash_screen_flash()
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003868{
3869 struct ptentry *ptn;
3870 struct ptable *ptable;
Mao Flynn7b379f32015-04-20 00:28:30 +08003871 struct logo_img_header *header;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003872 struct fbcon_config *fb_display = NULL;
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08003873
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303874 ptable = flash_get_ptable();
3875 if (ptable == NULL) {
Mao Flynn7b379f32015-04-20 00:28:30 +08003876 dprintf(CRITICAL, "ERROR: Partition table not found\n");
3877 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303878 }
Mao Flynn7b379f32015-04-20 00:28:30 +08003879
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303880 ptn = ptable_find(ptable, "splash");
3881 if (ptn == NULL) {
3882 dprintf(CRITICAL, "ERROR: splash Partition not found\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003883 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303884 }
Mao Flynn7b379f32015-04-20 00:28:30 +08003885 if (flash_read(ptn, 0, (void *)logo_header, LOGO_IMG_HEADER_SIZE)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303886 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003887 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303888 }
3889
Mao Flynn7b379f32015-04-20 00:28:30 +08003890 header = (struct logo_img_header *)logo_header;
3891 if (splash_screen_check_header(header)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303892 dprintf(CRITICAL, "ERROR: Boot image header invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003893 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303894 }
3895
3896 fb_display = fbcon_display();
3897 if (fb_display) {
Mao Flynn7b379f32015-04-20 00:28:30 +08003898 if (header->type && (header->blocks != 0)) { // RLE24 compressed data
3899 uint8_t *base = (uint8_t *) fb_display->base + LOGO_IMG_OFFSET;
3900
3901 /* if the logo is full-screen size, remove "fbcon_clear()" */
3902 if ((header->width != fb_display->width)
3903 || (header->height != fb_display->height))
3904 fbcon_clear();
3905
3906 if (flash_read(ptn + LOGO_IMG_HEADER_SIZE, 0,
3907 (uint32_t *)base,
3908 (header->blocks * 512))) {
3909 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
3910 return -1;
3911 }
3912 fbcon_extract_to_screen(header, base);
3913 return 0;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003914 }
Mao Flynn7b379f32015-04-20 00:28:30 +08003915
Veera Sundaram Sankaranc33416f2015-09-28 10:25:55 -07003916 if ((header->width > fb_display->width) || (header->height > fb_display->height)) {
3917 dprintf(CRITICAL, "Logo config greater than fb config. Fall back default logo\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003918 return -1;
3919 }
3920
Vineet Bajaj99291ed2014-09-09 12:29:46 +05303921 uint8_t *base = (uint8_t *) fb_display->base;
Sachin Bhayare619e9e42017-05-15 13:10:31 +05303922 uint32_t fb_size = ROUNDUP(fb_display->width *
3923 fb_display->height *
3924 (fb_display->bpp / 8), 4096);
3925 uint32_t splash_size = ((((header->width * header->height *
3926 fb_display->bpp/8) + 511) >> 9) << 9);
3927
3928 if (splash_size > fb_size) {
3929 dprintf(CRITICAL, "ERROR: Splash image size invalid\n");
3930 return -1;
3931 }
3932
Mao Flynn7b379f32015-04-20 00:28:30 +08003933 if (flash_read(ptn + LOGO_IMG_HEADER_SIZE, 0,
3934 (uint32_t *)base,
3935 ((((header->width * header->height * fb_display->bpp/8) + 511) >> 9) << 9))) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303936 fbcon_clear();
Vineet Bajaj99291ed2014-09-09 12:29:46 +05303937 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003938 return -1;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003939 }
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303940 }
3941
Mao Flynn7b379f32015-04-20 00:28:30 +08003942 return 0;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303943}
3944
Mao Flynn7b379f32015-04-20 00:28:30 +08003945int splash_screen_mmc()
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303946{
3947 int index = INVALID_PTN;
3948 unsigned long long ptn = 0;
3949 struct fbcon_config *fb_display = NULL;
Mao Flynn7b379f32015-04-20 00:28:30 +08003950 struct logo_img_header *header;
Mao Flynn1893a7f2015-06-03 12:03:36 +08003951 uint32_t blocksize, realsize, readsize;
3952 uint8_t *base;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303953
3954 index = partition_get_index("splash");
3955 if (index == 0) {
3956 dprintf(CRITICAL, "ERROR: splash Partition table not found\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003957 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303958 }
3959
3960 ptn = partition_get_offset(index);
3961 if (ptn == 0) {
3962 dprintf(CRITICAL, "ERROR: splash Partition invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003963 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303964 }
3965
Mao Flynn1893a7f2015-06-03 12:03:36 +08003966 mmc_set_lun(partition_get_lun(index));
3967
3968 blocksize = mmc_get_device_blocksize();
3969 if (blocksize == 0) {
3970 dprintf(CRITICAL, "ERROR:splash Partition invalid blocksize\n");
3971 return -1;
3972 }
3973
3974 fb_display = fbcon_display();
Channagoud Kadabidaf10a62015-07-22 11:51:55 -07003975 if (!fb_display)
3976 {
3977 dprintf(CRITICAL, "ERROR: fb config is not allocated\n");
3978 return -1;
3979 }
3980
Mao Flynn1893a7f2015-06-03 12:03:36 +08003981 base = (uint8_t *) fb_display->base;
3982
3983 if (mmc_read(ptn, (uint32_t *)(base + LOGO_IMG_OFFSET), blocksize)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303984 dprintf(CRITICAL, "ERROR: Cannot read splash image header\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003985 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303986 }
3987
Mao Flynn1893a7f2015-06-03 12:03:36 +08003988 header = (struct logo_img_header *)(base + LOGO_IMG_OFFSET);
Mao Flynn7b379f32015-04-20 00:28:30 +08003989 if (splash_screen_check_header(header)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303990 dprintf(CRITICAL, "ERROR: Splash image header invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003991 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303992 }
3993
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303994 if (fb_display) {
Mao Flynn1893a7f2015-06-03 12:03:36 +08003995 if (header->type && (header->blocks != 0)) { /* 1 RLE24 compressed data */
3996 base += LOGO_IMG_OFFSET;
Mao Flynn7b379f32015-04-20 00:28:30 +08003997
Mao Flynn1893a7f2015-06-03 12:03:36 +08003998 realsize = header->blocks * 512;
3999 readsize = ROUNDUP((realsize + LOGO_IMG_HEADER_SIZE), blocksize) - blocksize;
4000
4001 /* if the logo is not full-screen size, clean screen */
Mao Flynn7b379f32015-04-20 00:28:30 +08004002 if ((header->width != fb_display->width)
4003 || (header->height != fb_display->height))
4004 fbcon_clear();
4005
Sachin Bhayare619e9e42017-05-15 13:10:31 +05304006 uint32_t fb_size = ROUNDUP(fb_display->width *
4007 fb_display->height *
4008 (fb_display->bpp / 8), 4096);
4009
4010 if (readsize > fb_size) {
4011 dprintf(CRITICAL, "ERROR: Splash image size invalid\n");
4012 return -1;
4013 }
4014
Mao Flynn1893a7f2015-06-03 12:03:36 +08004015 if (mmc_read(ptn + blocksize, (uint32_t *)(base + blocksize), readsize)) {
Mao Flynn7b379f32015-04-20 00:28:30 +08004016 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
4017 return -1;
4018 }
Mao Flynn7b379f32015-04-20 00:28:30 +08004019
Mao Flynn1893a7f2015-06-03 12:03:36 +08004020 fbcon_extract_to_screen(header, (base + LOGO_IMG_HEADER_SIZE));
4021 } else { /* 2 Raw BGR data */
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304022
Veera Sundaram Sankaranc33416f2015-09-28 10:25:55 -07004023 if ((header->width > fb_display->width) || (header->height > fb_display->height)) {
4024 dprintf(CRITICAL, "Logo config greater than fb config. Fall back default logo\n");
Mao Flynn1893a7f2015-06-03 12:03:36 +08004025 return -1;
4026 }
4027
4028 realsize = header->width * header->height * fb_display->bpp / 8;
4029 readsize = ROUNDUP((realsize + LOGO_IMG_HEADER_SIZE), blocksize) - blocksize;
4030
4031 if (blocksize == LOGO_IMG_HEADER_SIZE) { /* read the content directly */
4032 if (mmc_read((ptn + LOGO_IMG_HEADER_SIZE), (uint32_t *)base, readsize)) {
4033 fbcon_clear();
4034 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
4035 return -1;
4036 }
4037 } else {
4038 if (mmc_read(ptn + blocksize ,
4039 (uint32_t *)(base + LOGO_IMG_OFFSET + blocksize), readsize)) {
4040 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
4041 return -1;
4042 }
4043 memmove(base, (base + LOGO_IMG_OFFSET + LOGO_IMG_HEADER_SIZE), realsize);
4044 }
4045 }
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304046 }
4047
Mao Flynn7b379f32015-04-20 00:28:30 +08004048 return 0;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304049}
4050
Mao Flynn7b379f32015-04-20 00:28:30 +08004051int fetch_image_from_partition()
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304052{
4053 if (target_is_emmc_boot()) {
4054 return splash_screen_mmc();
4055 } else {
4056 return splash_screen_flash();
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07004057 }
4058}
4059
Mayank Grover351a75e2017-05-30 20:06:08 +05304060void publish_getvar_multislot_vars()
4061{
4062 int i,count;
4063 static bool published = false;
4064 static char slot_count[MAX_RSP_SIZE];
4065 static struct ab_slot_info slot_info[AB_SUPPORTED_SLOTS];
4066 static char active_slot_suffix[MAX_RSP_SIZE];
4067 static char has_slot_pname[NUM_PARTITIONS][MAX_GET_VAR_NAME_SIZE];
4068 static char has_slot_reply[NUM_PARTITIONS][MAX_RSP_SIZE];
4069 const char *tmp;
4070 char tmpbuff[MAX_GET_VAR_NAME_SIZE];
Mayank Grover2bd84bb2017-06-20 11:06:07 +05304071 signed active_slt;
Mayank Grover351a75e2017-05-30 20:06:08 +05304072
4073 if (!published)
4074 {
4075 /* Update slot meta info */
4076 count = partition_fill_partition_meta(has_slot_pname, has_slot_reply,
4077 partition_get_partition_count());
4078 for(i=0; i<count; i++)
4079 {
4080 memset(tmpbuff, 0, MAX_GET_VAR_NAME_SIZE);
Mayank Grover2bd84bb2017-06-20 11:06:07 +05304081 snprintf(tmpbuff, MAX_GET_VAR_NAME_SIZE,"has-slot:%s",
4082 has_slot_pname[i]);
4083 strlcpy(has_slot_pname[i], tmpbuff, MAX_GET_VAR_NAME_SIZE);
Mayank Grover351a75e2017-05-30 20:06:08 +05304084 fastboot_publish(has_slot_pname[i], has_slot_reply[i]);
4085 }
4086
4087 for (i=0; i<AB_SUPPORTED_SLOTS; i++)
4088 {
4089 tmp = SUFFIX_SLOT(i);
Mayank Grover7b8a8f82017-10-27 13:07:59 +05304090 tmp++; // to remove "_" from slot_suffix.
Mayank Grover2bd84bb2017-06-20 11:06:07 +05304091 snprintf(slot_info[i].slot_is_unbootable, sizeof(slot_info[i].slot_is_unbootable),
4092 "slot-unbootable:%s", tmp);
4093 snprintf(slot_info[i].slot_is_active, sizeof(slot_info[i].slot_is_active),
4094 "slot-active:%s", tmp);
4095 snprintf(slot_info[i].slot_is_succesful, sizeof(slot_info[i].slot_is_succesful),
4096 "slot-success:%s", tmp);
4097 snprintf(slot_info[i].slot_retry_count, sizeof(slot_info[i].slot_retry_count),
4098 "slot-retry-count:%s", tmp);
Mayank Grover351a75e2017-05-30 20:06:08 +05304099 fastboot_publish(slot_info[i].slot_is_unbootable,
4100 slot_info[i].slot_is_unbootable_rsp);
4101 fastboot_publish(slot_info[i].slot_is_active,
4102 slot_info[i].slot_is_active_rsp);
4103 fastboot_publish(slot_info[i].slot_is_succesful,
4104 slot_info[i].slot_is_succesful_rsp);
4105 fastboot_publish(slot_info[i].slot_retry_count,
4106 slot_info[i].slot_retry_count_rsp);
4107 }
4108 fastboot_publish("current-slot", active_slot_suffix);
4109 snprintf(slot_count, sizeof(slot_count),"%d", AB_SUPPORTED_SLOTS);
4110 fastboot_publish("slot-count", slot_count);
4111 published = true;
4112 }
4113
Mayank Grover2bd84bb2017-06-20 11:06:07 +05304114 active_slt = partition_find_active_slot();
4115 if (active_slt != INVALID)
Mayank Grover7b8a8f82017-10-27 13:07:59 +05304116 {
4117 tmp = SUFFIX_SLOT(active_slt);
4118 tmp++; // to remove "_" from slot_suffix.
4119 snprintf(active_slot_suffix, sizeof(active_slot_suffix), "%s", tmp);
4120 }
Mayank Grover2bd84bb2017-06-20 11:06:07 +05304121 else
4122 strlcpy(active_slot_suffix, "INVALID", sizeof(active_slot_suffix));
4123
Mayank Grover351a75e2017-05-30 20:06:08 +05304124 /* Update partition meta information */
4125 partition_fill_slot_meta(slot_info);
4126 return;
4127}
4128
lijuang4ece1e72015-08-14 21:02:36 +08004129void get_product_name(unsigned char *buf)
4130{
4131 snprintf((char*)buf, MAX_RSP_SIZE, "%s", TARGET(BOARD));
4132 return;
4133}
4134
Mayank Grover7b8a8f82017-10-27 13:07:59 +05304135#if PRODUCT_IOT
4136void get_bootloader_version_iot(unsigned char *buf)
4137{
4138 if (buf != NULL)
4139 {
4140 strlcpy(buf, TARGET(BOARD), MAX_VERSION_LEN);
4141 strlcat(buf, "-", MAX_VERSION_LEN);
4142 strlcat(buf, PRODUCT_IOT_VERSION, MAX_VERSION_LEN);
4143 }
4144 return;
4145}
4146#endif
4147
lijuang4ece1e72015-08-14 21:02:36 +08004148void get_bootloader_version(unsigned char *buf)
4149{
4150 snprintf((char*)buf, MAX_RSP_SIZE, "%s", device.bootloader_version);
4151 return;
4152}
4153
4154void get_baseband_version(unsigned char *buf)
4155{
4156 snprintf((char*)buf, MAX_RSP_SIZE, "%s", device.radio_version);
4157 return;
4158}
4159
4160bool is_device_locked()
4161{
4162 return device.is_unlocked ? false:true;
4163}
4164
Amol Jadi5edf3552013-07-23 14:15:34 -07004165/* register commands and variables for fastboot */
4166void aboot_fastboot_register_commands(void)
4167{
Sridhar Parasurame94e8152014-10-24 14:06:03 -07004168 int i;
lijuangf16461c2015-08-03 17:09:34 +08004169 char hw_platform_buf[MAX_RSP_SIZE];
Amol Jadi5edf3552013-07-23 14:15:34 -07004170
Sridhar Parasurame94e8152014-10-24 14:06:03 -07004171 struct fastboot_cmd_desc cmd_list[] = {
lijuang511a2b52015-08-14 20:50:51 +08004172 /* By default the enabled list is empty. */
4173 {"", NULL},
4174 /* move commands enclosed within the below ifndef to here
4175 * if they need to be enabled in user build.
4176 */
Sridhar Parasurame94e8152014-10-24 14:06:03 -07004177#ifndef DISABLE_FASTBOOT_CMDS
lijuang511a2b52015-08-14 20:50:51 +08004178 /* Register the following commands only for non-user builds */
4179 {"flash:", cmd_flash},
4180 {"erase:", cmd_erase},
4181 {"boot", cmd_boot},
4182 {"continue", cmd_continue},
4183 {"reboot", cmd_reboot},
4184 {"reboot-bootloader", cmd_reboot_bootloader},
4185 {"oem unlock", cmd_oem_unlock},
4186 {"oem unlock-go", cmd_oem_unlock_go},
4187 {"oem lock", cmd_oem_lock},
4188 {"flashing unlock", cmd_oem_unlock},
4189 {"flashing lock", cmd_oem_lock},
4190 {"flashing lock_critical", cmd_flashing_lock_critical},
4191 {"flashing unlock_critical", cmd_flashing_unlock_critical},
4192 {"flashing get_unlock_ability", cmd_flashing_get_unlock_ability},
4193 {"oem device-info", cmd_oem_devinfo},
4194 {"preflash", cmd_preflash},
4195 {"oem enable-charger-screen", cmd_oem_enable_charger_screen},
4196 {"oem disable-charger-screen", cmd_oem_disable_charger_screen},
lijuanga25d8bb2015-09-16 13:11:52 +08004197 {"oem off-mode-charge", cmd_oem_off_mode_charger},
lijuang511a2b52015-08-14 20:50:51 +08004198 {"oem select-display-panel", cmd_oem_select_display_panel},
Mayank Grover6ccc1c92017-07-04 17:36:46 +05304199 {"set_active",cmd_set_active},
Channagoud Kadabi736c4962015-08-21 11:56:52 -07004200#if UNITTEST_FW_SUPPORT
Channagoud Kadabid5ddf482015-09-28 10:31:35 -07004201 {"oem run-tests", cmd_oem_runtests},
Channagoud Kadabi736c4962015-08-21 11:56:52 -07004202#endif
Sridhar Parasurame94e8152014-10-24 14:06:03 -07004203#endif
lijuang511a2b52015-08-14 20:50:51 +08004204 };
Sridhar Parasurame94e8152014-10-24 14:06:03 -07004205
4206 int fastboot_cmds_count = sizeof(cmd_list)/sizeof(cmd_list[0]);
4207 for (i = 1; i < fastboot_cmds_count; i++)
4208 fastboot_register(cmd_list[i].name,cmd_list[i].cb);
4209
Amol Jadi5edf3552013-07-23 14:15:34 -07004210 /* publish variables and their values */
4211 fastboot_publish("product", TARGET(BOARD));
4212 fastboot_publish("kernel", "lk");
4213 fastboot_publish("serialno", sn_buf);
4214
4215 /*
4216 * partition info is supported only for emmc partitions
4217 * Calling this for NAND prints some error messages which
4218 * is harmless but misleading. Avoid calling this for NAND
4219 * devices.
4220 */
4221 if (target_is_emmc_boot())
Mayank Grover7b8a8f82017-10-27 13:07:59 +05304222 publish_getvar_partition_info(part_info, partition_get_partition_count());
Amol Jadi5edf3552013-07-23 14:15:34 -07004223
Mayank Grover351a75e2017-05-30 20:06:08 +05304224 if (partition_multislot_is_supported())
4225 publish_getvar_multislot_vars();
4226
Amol Jadi5edf3552013-07-23 14:15:34 -07004227 /* Max download size supported */
Ameya Thakur11cf1a62013-08-05 12:44:48 -07004228 snprintf(max_download_size, MAX_RSP_SIZE, "\t0x%x",
4229 target_get_max_flash_size());
Amol Jadi5edf3552013-07-23 14:15:34 -07004230 fastboot_publish("max-download-size", (const char *) max_download_size);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07004231 /* Is the charger screen check enabled */
4232 snprintf(charger_screen_enabled, MAX_RSP_SIZE, "%d",
4233 device.charger_screen_enabled);
4234 fastboot_publish("charger-screen-enabled",
4235 (const char *) charger_screen_enabled);
lijuanga25d8bb2015-09-16 13:11:52 +08004236 fastboot_publish("off-mode-charge", (const char *) charger_screen_enabled);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05304237 snprintf(panel_display_mode, MAX_RSP_SIZE, "%s",
4238 device.display_panel);
4239 fastboot_publish("display-panel",
4240 (const char *) panel_display_mode);
Mayank Grover7b8a8f82017-10-27 13:07:59 +05304241#if PRODUCT_IOT
4242 get_bootloader_version_iot(&bootloader_version_string);
4243 fastboot_publish("version-bootloader", (const char *) bootloader_version_string);
4244
4245 /* Version baseband is n/a for apq iot devices */
4246 fastboot_publish("version-baseband", "N/A");
4247
4248 /* IOT targets support only mmc target */
4249 snprintf(block_size_string, MAX_RSP_SIZE, "0x%x", mmc_get_device_blocksize());
4250 fastboot_publish("erase-block-size", (const char *) block_size_string);
4251 fastboot_publish("logical-block-size", (const char *) block_size_string);
4252#else
Channagoud Kadabiaafbbfc2015-03-04 17:52:56 -08004253 fastboot_publish("version-bootloader", (const char *) device.bootloader_version);
4254 fastboot_publish("version-baseband", (const char *) device.radio_version);
Mayank Grover7b8a8f82017-10-27 13:07:59 +05304255#endif
lijuangf16461c2015-08-03 17:09:34 +08004256 fastboot_publish("secure", is_secure_boot_enable()? "yes":"no");
Mayank Grover7b8a8f82017-10-27 13:07:59 +05304257 fastboot_publish("unlocked", device.is_unlocked ? "yes":"no");
lijuangf16461c2015-08-03 17:09:34 +08004258 smem_get_hw_platform_name((unsigned char *) hw_platform_buf, sizeof(hw_platform_buf));
4259 snprintf(get_variant, MAX_RSP_SIZE, "%s %s", hw_platform_buf,
4260 target_is_emmc_boot()? "eMMC":"UFS");
4261 fastboot_publish("variant", (const char *) get_variant);
lijuang65c5a822015-08-29 16:35:36 +08004262#if CHECK_BAT_VOLTAGE
lijuang102dfa92015-10-09 18:31:03 +08004263 update_battery_status();
lijuang65c5a822015-08-29 16:35:36 +08004264 fastboot_publish("battery-voltage", (const char *) battery_voltage);
lijuang102dfa92015-10-09 18:31:03 +08004265 fastboot_publish("battery-soc-ok", (const char *) battery_soc_ok);
lijuang65c5a822015-08-29 16:35:36 +08004266#endif
Amol Jadi5edf3552013-07-23 14:15:34 -07004267}
4268
Brian Swetland9c4c0752009-01-25 16:23:50 -08004269void aboot_init(const struct app_descriptor *app)
4270{
Shashank Mittal4f99a882010-02-01 13:58:50 -08004271 unsigned reboot_mode = 0;
Mayank Grover351a75e2017-05-30 20:06:08 +05304272 int boot_err_type = 0;
4273 int boot_slot = INVALID;
Chandan Uddarajubedca152010-06-02 23:05:15 -07004274
lijuang39831732016-01-08 17:49:02 +08004275 /* Initialise wdog to catch early lk crashes */
4276#if WDOG_SUPPORT
4277 msm_wdog_init();
4278#endif
4279
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07004280 /* Setup page size information for nv storage */
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004281 if (target_is_emmc_boot())
4282 {
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07004283 page_size = mmc_page_size();
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004284 page_mask = page_size - 1;
Gaurav Nebhwani4d2389c2016-03-17 21:10:05 +05304285 mmc_blocksize = mmc_get_device_blocksize();
4286 mmc_blocksize_mask = mmc_blocksize - 1;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004287 }
4288 else
4289 {
4290 page_size = flash_page_size();
4291 page_mask = page_size - 1;
4292 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07004293 ASSERT((MEMBASE + MEMSIZE) > MEMBASE);
4294
Ameya Thakur11cf1a62013-08-05 12:44:48 -07004295 read_device_info(&device);
vijay kumarc65876c2015-04-24 13:29:16 +05304296 read_allow_oem_unlock(&device);
Shashank Mittal162244e2011-08-08 19:01:25 -07004297
Mayank Grover351a75e2017-05-30 20:06:08 +05304298 /* Detect multi-slot support */
4299 if (partition_multislot_is_supported())
4300 {
4301 boot_slot = partition_find_active_slot();
4302 if (boot_slot == INVALID)
4303 {
4304 boot_into_fastboot = true;
4305 dprintf(INFO, "Active Slot: (INVALID)\n");
4306 }
4307 else
4308 {
4309 /* Setting the state of system to boot active slot */
4310 partition_mark_active_slot(boot_slot);
4311 dprintf(INFO, "Active Slot: (%s)\n", SUFFIX_SLOT(boot_slot));
4312 }
4313 }
4314
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08004315 /* Display splash screen if enabled */
4316#if DISPLAY_SPLASH_SCREEN
lijuang99c02d82015-02-13 19:04:34 +08004317#if NO_ALARM_DISPLAY
4318 if (!check_alarm_boot()) {
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08004319#endif
lijuang99c02d82015-02-13 19:04:34 +08004320 dprintf(SPEW, "Display Init: Start\n");
Ajay Singh Parmara7865a82015-04-16 21:27:52 -07004321#if DISPLAY_HDMI_PRIMARY
4322 if (!strlen(device.display_panel))
4323 strlcpy(device.display_panel, DISPLAY_PANEL_HDMI,
4324 sizeof(device.display_panel));
4325#endif
Channagoud Kadabi90869ce2015-04-27 11:15:14 -07004326#if ENABLE_WBC
Channagoud Kadabid801ac72015-08-26 11:21:51 -07004327 /* Wait if the display shutdown is in progress */
4328 while(pm_app_display_shutdown_in_prgs());
Channagoud Kadabi90869ce2015-04-27 11:15:14 -07004329 if (!pm_appsbl_display_init_done())
4330 target_display_init(device.display_panel);
4331 else
4332 display_image_on_screen();
4333#else
lijuang99c02d82015-02-13 19:04:34 +08004334 target_display_init(device.display_panel);
Channagoud Kadabi90869ce2015-04-27 11:15:14 -07004335#endif
lijuang99c02d82015-02-13 19:04:34 +08004336 dprintf(SPEW, "Display Init: Done\n");
4337#if NO_ALARM_DISPLAY
4338 }
4339#endif
4340#endif
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08004341
Greg Griscod6250552011-06-29 14:40:23 -07004342 target_serialno((unsigned char *) sn_buf);
Ajay Dudanib06c05f2011-05-12 14:46:10 -07004343 dprintf(SPEW,"serial number: %s\n",sn_buf);
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -08004344
Dhaval Patel223ec952013-07-18 14:49:44 -07004345 memset(display_panel_buf, '\0', MAX_PANEL_BUF_SIZE);
4346
Matthew Qindefd5562014-07-11 18:02:40 +08004347 /*
4348 * Check power off reason if user force reset,
4349 * if yes phone will do normal boot.
4350 */
4351 if (is_user_force_reset())
4352 goto normal_boot;
4353
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07004354 /* Check if we should do something other than booting up */
Ameya Thakur0b9c2442013-05-10 13:22:56 -07004355 if (keys_get_state(KEY_VOLUMEUP) && keys_get_state(KEY_VOLUMEDOWN))
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07004356 {
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03004357 dprintf(ALWAYS,"dload mode key sequence detected\n");
lijuang395b5e62015-11-19 17:39:44 +08004358 reboot_device(EMERGENCY_DLOAD);
4359 dprintf(CRITICAL,"Failed to reboot into dload mode\n");
4360
Ameya Thakur0b9c2442013-05-10 13:22:56 -07004361 boot_into_fastboot = true;
4362 }
4363 if (!boot_into_fastboot)
4364 {
4365 if (keys_get_state(KEY_HOME) || keys_get_state(KEY_VOLUMEUP))
4366 boot_into_recovery = 1;
4367 if (!boot_into_recovery &&
4368 (keys_get_state(KEY_BACK) || keys_get_state(KEY_VOLUMEDOWN)))
Pavel Nedev5d91d412013-04-29 11:34:24 +03004369 boot_into_fastboot = true;
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07004370 }
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004371 #if NO_KEYPAD_DRIVER
Kinson Chik0b1c8162011-08-31 16:31:57 -07004372 if (fastboot_trigger())
Pavel Nedev5d91d412013-04-29 11:34:24 +03004373 boot_into_fastboot = true;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004374 #endif
Chandan Uddarajubedca152010-06-02 23:05:15 -07004375
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07004376#if USE_PON_REBOOT_REG
4377 reboot_mode = check_hard_reboot_mode();
4378#else
Ajay Dudani77421292010-10-27 19:34:06 -07004379 reboot_mode = check_reboot_mode();
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07004380#endif
4381 if (reboot_mode == RECOVERY_MODE)
4382 {
Ajay Dudani77421292010-10-27 19:34:06 -07004383 boot_into_recovery = 1;
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07004384 }
4385 else if(reboot_mode == FASTBOOT_MODE)
4386 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03004387 boot_into_fastboot = true;
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07004388 }
4389 else if(reboot_mode == ALARM_BOOT)
4390 {
Matthew Qind886f3c2014-01-17 16:52:01 +08004391 boot_reason_alarm = true;
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07004392 }
Parth Dixit207573a2015-10-27 17:26:21 +05304393#if VERIFIED_BOOT
Mayank Grover912eaa62017-10-26 12:08:53 +05304394 else if (VB_M <= target_get_vb_version())
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07004395 {
Mayank Grover889be1b2017-09-12 20:12:23 +05304396 if (reboot_mode == DM_VERITY_ENFORCING)
4397 {
4398 device.verity_mode = 1;
4399 write_device_info(&device);
4400 }
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07004401#if ENABLE_VB_ATTEST
Mayank Grover889be1b2017-09-12 20:12:23 +05304402 else if (reboot_mode == DM_VERITY_EIO)
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07004403#else
Mayank Grover889be1b2017-09-12 20:12:23 +05304404 else if (reboot_mode == DM_VERITY_LOGGING)
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07004405#endif
Mayank Grover889be1b2017-09-12 20:12:23 +05304406 {
4407 device.verity_mode = 0;
4408 write_device_info(&device);
4409 }
4410 else if (reboot_mode == DM_VERITY_KEYSCLEAR)
4411 {
4412 if(send_delete_keys_to_tz())
4413 ASSERT(0);
4414 }
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07004415 }
Parth Dixitddbc7352015-10-18 03:13:31 +05304416#endif
Ajay Dudani77421292010-10-27 19:34:06 -07004417
Matthew Qindefd5562014-07-11 18:02:40 +08004418normal_boot:
Pavel Nedev5d91d412013-04-29 11:34:24 +03004419 if (!boot_into_fastboot)
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004420 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03004421 if (target_is_emmc_boot())
Shashank Mittala0032282011-08-26 14:50:11 -07004422 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03004423 if(emmc_recovery_init())
4424 dprintf(ALWAYS,"error in emmc_recovery_init\n");
4425 if(target_use_signed_kernel())
Shashank Mittala0032282011-08-26 14:50:11 -07004426 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03004427 if((device.is_unlocked) || (device.is_tampered))
4428 {
4429 #ifdef TZ_TAMPER_FUSE
4430 set_tamper_fuse_cmd();
4431 #endif
4432 #if USE_PCOM_SECBOOT
4433 set_tamper_flag(device.is_tampered);
4434 #endif
4435 }
Shashank Mittala0032282011-08-26 14:50:11 -07004436 }
Amit Blay6281ebc2015-01-11 14:44:08 +02004437
Mayank Grover351a75e2017-05-30 20:06:08 +05304438retry_boot:
4439 /* Trying to boot active partition */
4440 if (partition_multislot_is_supported())
4441 {
4442 boot_slot = partition_find_boot_slot();
4443 partition_mark_active_slot(boot_slot);
4444 if (boot_slot == INVALID)
4445 goto fastboot;
4446 }
4447
4448 boot_err_type = boot_linux_from_mmc();
4449 switch (boot_err_type)
4450 {
4451 case ERR_INVALID_PAGE_SIZE:
4452 case ERR_DT_PARSE:
4453 case ERR_ABOOT_ADDR_OVERLAP:
Mayank Grover10cabbe2017-10-30 19:11:05 +05304454 case ERR_INVALID_BOOT_MAGIC:
Mayank Grover351a75e2017-05-30 20:06:08 +05304455 if(partition_multislot_is_supported())
Mayank Grover10cabbe2017-10-30 19:11:05 +05304456 {
4457 /*
4458 * Deactivate current slot, as it failed to
4459 * boot, and retry next slot.
4460 */
4461 partition_deactivate_slot(boot_slot);
Mayank Grover351a75e2017-05-30 20:06:08 +05304462 goto retry_boot;
Mayank Grover10cabbe2017-10-30 19:11:05 +05304463 }
Mayank Grover351a75e2017-05-30 20:06:08 +05304464 else
4465 break;
Mayank Grover351a75e2017-05-30 20:06:08 +05304466 default:
4467 break;
4468 /* going to fastboot menu */
4469 }
Shashank Mittala0032282011-08-26 14:50:11 -07004470 }
Pavel Nedev5d91d412013-04-29 11:34:24 +03004471 else
4472 {
4473 recovery_init();
4474 #if USE_PCOM_SECBOOT
4475 if((device.is_unlocked) || (device.is_tampered))
4476 set_tamper_flag(device.is_tampered);
4477 #endif
4478 boot_linux_from_flash();
4479 }
4480 dprintf(CRITICAL, "ERROR: Could not do normal boot. Reverting "
4481 "to fastboot mode.\n");
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004482 }
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07004483
Mayank Grover351a75e2017-05-30 20:06:08 +05304484fastboot:
Amol Jadi5edf3552013-07-23 14:15:34 -07004485 /* We are here means regular boot did not happen. Start fastboot. */
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07004486
Amol Jadi5edf3552013-07-23 14:15:34 -07004487 /* register aboot specific fastboot commands */
4488 aboot_fastboot_register_commands();
Amol Jadi57abe4c2011-05-24 15:47:27 -07004489
Amol Jadi5edf3552013-07-23 14:15:34 -07004490 /* dump partition table for debug info */
Kinson Chikf1a43512011-07-14 11:28:39 -07004491 partition_dump();
Amol Jadi5edf3552013-07-23 14:15:34 -07004492
4493 /* initialize and start fastboot */
4494 fastboot_init(target_get_scratch_address(), target_get_max_flash_size());
lijuang4ece1e72015-08-14 21:02:36 +08004495#if FBCON_DISPLAY_MSG
lijuang9a7d3b92015-11-30 14:41:24 +08004496 display_fastboot_menu();
lijuang4ece1e72015-08-14 21:02:36 +08004497#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08004498}
4499
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07004500uint32_t get_page_size()
4501{
4502 return page_size;
4503}
4504
Amir Samuelov57a6fa22013-06-05 16:36:43 +03004505/*
4506 * Calculated and save hash (SHA256) for non-signed boot image.
4507 *
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07004508 * @param image_addr - Boot image address
4509 * @param image_size - Size of the boot image
Amir Samuelov57a6fa22013-06-05 16:36:43 +03004510 *
4511 * @return int - 0 on success, negative value on failure.
4512 */
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07004513static int aboot_save_boot_hash_mmc(uint32_t image_addr, uint32_t image_size)
Amir Samuelov57a6fa22013-06-05 16:36:43 +03004514{
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07004515 unsigned int digest[8];
4516#if IMAGE_VERIF_ALGO_SHA1
4517 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA1;
4518#else
4519 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA256;
4520#endif
Amir Samuelov57a6fa22013-06-05 16:36:43 +03004521
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07004522 target_crypto_init_params();
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -08004523 hash_find((unsigned char *) image_addr, image_size, (unsigned char *)&digest, auth_algo);
Amir Samuelov57a6fa22013-06-05 16:36:43 +03004524
4525 save_kernel_hash_cmd(digest);
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07004526 dprintf(INFO, "aboot_save_boot_hash_mmc: imagesize_actual size %d bytes.\n", (int) image_size);
Amir Samuelov57a6fa22013-06-05 16:36:43 +03004527
4528 return 0;
4529}
4530
Mayank Grover351a75e2017-05-30 20:06:08 +05304531
Brian Swetland9c4c0752009-01-25 16:23:50 -08004532APP_START(aboot)
4533 .init = aboot_init,
4534APP_END