blob: cc34f7a80925be8074f44216cc3333127eb1cdbe [file] [log] [blame]
Brian Swetland9c4c0752009-01-25 16:23:50 -08001/*
2 * Copyright (c) 2009, Google Inc.
3 * All rights reserved.
4 *
Kishor PK49831502017-04-21 17:55:43 +05305 * Copyright (c) 2009-2017, 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
Brian Swetland9c4c0752009-01-25 16:23:50 -08001014
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001015BUF_DMA_ALIGN(buf, BOOT_IMG_MAX_PAGE_SIZE); //Equal to max-supported pagesize
Amol Jadib6be5c12012-11-14 13:39:51 -08001016#if DEVICE_TREE
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001017BUF_DMA_ALIGN(dt_buf, BOOT_IMG_MAX_PAGE_SIZE);
Amol Jadib6be5c12012-11-14 13:39:51 -08001018#endif
Dima Zavin214cc642009-01-26 11:16:21 -08001019
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001020static void verify_signed_bootimg(uint32_t bootimg_addr, uint32_t bootimg_size)
1021{
1022 int ret;
Channagoud Kadabia8c623f2015-01-13 14:48:48 -08001023
1024#if !VERIFIED_BOOT
Sundarajan Srinivasance54d6e2013-11-11 12:45:00 -08001025#if IMAGE_VERIF_ALGO_SHA1
1026 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA1;
1027#else
1028 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA256;
1029#endif
Channagoud Kadabia8c623f2015-01-13 14:48:48 -08001030#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001031
1032 /* Assume device is rooted at this time. */
1033 device.is_tampered = 1;
1034
1035 dprintf(INFO, "Authenticating boot image (%d): start\n", bootimg_size);
1036
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001037#if VERIFIED_BOOT
Mayank Grover68fbf0d2017-10-24 14:13:39 +05301038 char *ptn_name = NULL;
1039 if (boot_into_recovery &&
1040 (!partition_multislot_is_supported()))
1041 ptn_name = "/recovery";
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001042 else
Mayank Grover68fbf0d2017-10-24 14:13:39 +05301043 ptn_name = "/boot";
1044
1045 ret = boot_verify_image((unsigned char *)bootimg_addr,
1046 bootimg_size, ptn_name);
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001047 boot_verify_print_state();
1048#else
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001049 ret = image_verify((unsigned char *)bootimg_addr,
1050 (unsigned char *)(bootimg_addr + bootimg_size),
1051 bootimg_size,
Sundarajan Srinivasance54d6e2013-11-11 12:45:00 -08001052 auth_algo);
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001053#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001054 dprintf(INFO, "Authenticating boot image: done return value = %d\n", ret);
1055
1056 if (ret)
1057 {
1058 /* Authorized kernel */
1059 device.is_tampered = 0;
Sundarajan Srinivasan3fb21f12013-09-16 18:36:15 -07001060 auth_kernel_img = 1;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001061 }
1062
Amit Blay4aa292f2015-04-28 21:55:59 +03001063#ifdef MDTP_SUPPORT
1064 {
1065 /* Verify MDTP lock.
1066 * For boot & recovery partitions, use aboot's verification result.
1067 */
1068 mdtp_ext_partition_verification_t ext_partition;
1069 ext_partition.partition = boot_into_recovery ? MDTP_PARTITION_RECOVERY : MDTP_PARTITION_BOOT;
1070 ext_partition.integrity_state = device.is_tampered ? MDTP_PARTITION_STATE_INVALID : MDTP_PARTITION_STATE_VALID;
1071 ext_partition.page_size = 0; /* Not needed since already validated */
1072 ext_partition.image_addr = 0; /* Not needed since already validated */
1073 ext_partition.image_size = 0; /* Not needed since already validated */
1074 ext_partition.sig_avail = FALSE; /* Not needed since already validated */
1075 mdtp_fwlock_verify_lock(&ext_partition);
1076 }
1077#endif /* MDTP_SUPPORT */
1078
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001079#if USE_PCOM_SECBOOT
1080 set_tamper_flag(device.is_tampered);
1081#endif
1082
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001083#if VERIFIED_BOOT
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001084 switch(boot_verify_get_state())
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001085 {
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001086 case RED:
lijuanga40d6302015-07-20 20:10:13 +08001087#if FBCON_DISPLAY_MSG
lijuang9a7d3b92015-11-30 14:41:24 +08001088 display_bootverify_menu(DISPLAY_MENU_RED);
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07001089#if ENABLE_VB_ATTEST
1090 mdelay(DELAY_WAIT);
1091 shutdown_device();
1092#else
lijuanga40d6302015-07-20 20:10:13 +08001093 wait_for_users_action();
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07001094#endif
lijuanga40d6302015-07-20 20:10:13 +08001095#else
1096 dprintf(CRITICAL,
1097 "Your device has failed verification and may not work properly.\nWait for 5 seconds before proceeding\n");
1098 mdelay(5000);
1099#endif
1100
1101 break;
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001102 case YELLOW:
lijuanga40d6302015-07-20 20:10:13 +08001103#if FBCON_DISPLAY_MSG
lijuang9a7d3b92015-11-30 14:41:24 +08001104 display_bootverify_menu(DISPLAY_MENU_YELLOW);
lijuanga40d6302015-07-20 20:10:13 +08001105 wait_for_users_action();
1106#else
1107 dprintf(CRITICAL,
1108 "Your device has loaded a different operating system.\nWait for 5 seconds before proceeding\n");
1109 mdelay(5000);
1110#endif
1111 break;
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001112 default:
lijuanga40d6302015-07-20 20:10:13 +08001113 break;
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001114 }
1115#endif
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001116#if !VERIFIED_BOOT
Unnati Gandhi1be04752015-03-27 19:41:53 +05301117 if(device.is_tampered)
1118 {
1119 write_device_info_mmc(&device);
1120 #ifdef TZ_TAMPER_FUSE
1121 set_tamper_fuse_cmd();
1122 #endif
1123 #ifdef ASSERT_ON_TAMPER
1124 dprintf(CRITICAL, "Device is tampered. Asserting..\n");
1125 ASSERT(0);
1126 #endif
1127 }
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001128#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001129}
1130
Maunik Shah0f3c8ac2014-03-02 17:47:58 +05301131static bool check_format_bit()
1132{
1133 bool ret = false;
1134 int index;
1135 uint64_t offset;
1136 struct boot_selection_info *in = NULL;
1137 char *buf = NULL;
1138
1139 index = partition_get_index("bootselect");
1140 if (index == INVALID_PTN)
1141 {
1142 dprintf(INFO, "Unable to locate /bootselect partition\n");
1143 return ret;
1144 }
1145 offset = partition_get_offset(index);
1146 if(!offset)
1147 {
1148 dprintf(INFO, "partition /bootselect doesn't exist\n");
1149 return ret;
1150 }
1151 buf = (char *) memalign(CACHE_LINE, ROUNDUP(page_size, CACHE_LINE));
Mayank Grover48860402016-11-29 12:34:53 +05301152 mmc_set_lun(partition_get_lun(index));
Maunik Shah0f3c8ac2014-03-02 17:47:58 +05301153 ASSERT(buf);
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301154 if (mmc_read(offset, (uint32_t *)buf, page_size))
Maunik Shah0f3c8ac2014-03-02 17:47:58 +05301155 {
1156 dprintf(INFO, "mmc read failure /bootselect %d\n", page_size);
1157 free(buf);
1158 return ret;
1159 }
1160 in = (struct boot_selection_info *) buf;
1161 if ((in->signature == BOOTSELECT_SIGNATURE) &&
1162 (in->version == BOOTSELECT_VERSION)) {
1163 if ((in->state_info & BOOTSELECT_FORMAT) &&
1164 !(in->state_info & BOOTSELECT_FACTORY))
1165 ret = true;
1166 } else {
1167 dprintf(CRITICAL, "Signature: 0x%08x or version: 0x%08x mismatched of /bootselect\n",
1168 in->signature, in->version);
1169 ASSERT(0);
1170 }
1171 free(buf);
1172 return ret;
1173}
1174
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001175void boot_verifier_init()
1176{
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001177 uint32_t boot_state;
1178 /* Check if device unlock */
1179 if(device.is_unlocked)
1180 {
1181 boot_verify_send_event(DEV_UNLOCK);
1182 boot_verify_print_state();
1183 dprintf(CRITICAL, "Device is unlocked! Skipping verification...\n");
1184 return;
1185 }
1186 else
1187 {
1188 boot_verify_send_event(BOOT_INIT);
1189 }
1190
1191 /* Initialize keystore */
1192 boot_state = boot_verify_keystore_init();
1193 if(boot_state == YELLOW)
1194 {
1195 boot_verify_print_state();
1196 dprintf(CRITICAL, "Keystore verification failed! Continuing anyways...\n");
1197 }
1198}
1199
Shashank Mittal23b8f422010-04-16 19:27:21 -07001200int boot_linux_from_mmc(void)
1201{
1202 struct boot_img_hdr *hdr = (void*) buf;
1203 struct boot_img_hdr *uhdr;
1204 unsigned offset = 0;
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07001205 int rcode;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001206 unsigned long long ptn = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001207 int index = INVALID_PTN;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001208
Shashank Mittalcd98d472011-08-02 14:29:24 -07001209 unsigned char *image_addr = 0;
1210 unsigned kernel_actual;
1211 unsigned ramdisk_actual;
1212 unsigned imagesize_actual;
Neeti Desai465491e2012-07-31 12:53:35 -07001213 unsigned second_actual = 0;
Neeti Desai465491e2012-07-31 12:53:35 -07001214
Matthew Qin271927e2015-03-31 22:07:07 -04001215 unsigned int dtb_size = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08001216 unsigned int out_len = 0;
1217 unsigned int out_avai_len = 0;
1218 unsigned char *out_addr = NULL;
1219 uint32_t dtb_offset = 0;
1220 unsigned char *kernel_start_addr = NULL;
1221 unsigned int kernel_size = 0;
Ameya Thakur10a33452016-06-13 14:24:26 -07001222 unsigned int patched_kernel_hdr_size = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08001223 int rc;
Mayank Groverc93cad82017-10-03 12:23:45 +05301224 char *ptn_name = NULL;
Neeti Desai465491e2012-07-31 12:53:35 -07001225#if DEVICE_TREE
1226 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -07001227 struct dt_entry dt_entry;
Neeti Desai465491e2012-07-31 12:53:35 -07001228 unsigned dt_table_offset;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001229 uint32_t dt_actual;
Deepa Dinamani19648b42013-09-05 17:05:55 -07001230 uint32_t dt_hdr_size;
Matthew Qin271927e2015-03-31 22:07:07 -04001231 unsigned char *best_match_dt_addr = NULL;
Neeti Desai465491e2012-07-31 12:53:35 -07001232#endif
Matthew Qin49e51fa2015-02-09 10:40:45 +08001233 struct kernel64_hdr *kptr = NULL;
Mayank Grover351a75e2017-05-30 20:06:08 +05301234 int current_active_slot = INVALID;
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001235
Maunik Shah0f3c8ac2014-03-02 17:47:58 +05301236 if (check_format_bit())
1237 boot_into_recovery = 1;
1238
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07001239 if (!boot_into_recovery) {
1240 memset(ffbm_mode_string, '\0', sizeof(ffbm_mode_string));
1241 rcode = get_ffbm(ffbm_mode_string, sizeof(ffbm_mode_string));
1242 if (rcode <= 0) {
1243 boot_into_ffbm = false;
1244 if (rcode < 0)
1245 dprintf(CRITICAL,"failed to get ffbm cookie");
1246 } else
1247 boot_into_ffbm = true;
1248 } else
1249 boot_into_ffbm = false;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001250 uhdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
1251 if (!memcmp(uhdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1252 dprintf(INFO, "Unified boot method!\n");
1253 hdr = uhdr;
1254 goto unified_boot;
1255 }
Mayank Groverc93cad82017-10-03 12:23:45 +05301256
1257 /* For a/b recovery image code is on boot partition.
1258 If we support multislot, always use boot partition. */
1259 if (boot_into_recovery &&
1260 (!partition_multislot_is_supported()))
1261 ptn_name = "recovery";
1262 else
1263 ptn_name = "boot";
1264
1265 index = partition_get_index(ptn_name);
1266 ptn = partition_get_offset(index);
1267 if(ptn == 0) {
1268 dprintf(CRITICAL, "ERROR: No %s partition found\n", ptn_name);
1269 return -1;
Kinson Chikf1a43512011-07-14 11:28:39 -07001270 }
Mayank Groverc93cad82017-10-03 12:23:45 +05301271
Channagoud Kadabief0547c2015-02-10 12:57:38 -08001272 /* Set Lun for boot & recovery partitions */
1273 mmc_set_lun(partition_get_lun(index));
Shashank Mittal23b8f422010-04-16 19:27:21 -07001274
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301275 if (mmc_read(ptn + offset, (uint32_t *) buf, page_size)) {
Shashank Mittal23b8f422010-04-16 19:27:21 -07001276 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
1277 return -1;
1278 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001279
1280 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001281 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Mayank Grover351a75e2017-05-30 20:06:08 +05301282 return ERR_INVALID_BOOT_MAGIC;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001283 }
1284
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07001285 if (hdr->page_size && (hdr->page_size != page_size)) {
vijay kumar2e21b3a2014-06-26 17:40:15 +05301286
1287 if (hdr->page_size > BOOT_IMG_MAX_PAGE_SIZE) {
1288 dprintf(CRITICAL, "ERROR: Invalid page size\n");
1289 return -1;
1290 }
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07001291 page_size = hdr->page_size;
1292 page_mask = page_size - 1;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001293 }
1294
Matthew Qin49e51fa2015-02-09 10:40:45 +08001295 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1296 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Kishor PKd8ddcad2017-07-27 13:53:57 +05301297 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001298
Matthew Qin49e51fa2015-02-09 10:40:45 +08001299 image_addr = (unsigned char *)target_get_scratch_address();
Kishor PK9e91b592017-05-24 12:14:55 +05301300 memcpy(image_addr, (void *)buf, page_size);
1301
1302 /* ensure commandline is terminated */
1303 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
Matthew Qin49e51fa2015-02-09 10:40:45 +08001304
1305#if DEVICE_TREE
Parth Dixit4097b622016-03-15 14:42:27 +05301306#ifndef OSVERSION_IN_BOOTIMAGE
1307 dt_size = hdr->dt_size;
1308#endif
1309 dt_actual = ROUND_TO_PAGE(dt_size, page_mask);
Kishor PKd8ddcad2017-07-27 13:53:57 +05301310 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 +05301311 dprintf(CRITICAL, "Integer overflow detected in bootimage header fields at %u in %s\n",__LINE__,__FILE__);
1312 return -1;
1313 }
Kishor PKd8ddcad2017-07-27 13:53:57 +05301314 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + second_actual + dt_actual);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001315#else
Kishor PKd8ddcad2017-07-27 13:53:57 +05301316 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 +05301317 dprintf(CRITICAL, "Integer overflow detected in bootimage header fields at %u in %s\n",__LINE__,__FILE__);
1318 return -1;
1319 }
Kishor PKd8ddcad2017-07-27 13:53:57 +05301320 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + second_actual);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001321#endif
1322
1323#if VERIFIED_BOOT
1324 boot_verifier_init();
1325#endif
1326
Vijay Kumar Pendoti9c002ad2016-03-09 13:52:45 +05301327 if (check_aboot_addr_range_overlap((uintptr_t) image_addr, imagesize_actual))
Matthew Qin49e51fa2015-02-09 10:40:45 +08001328 {
1329 dprintf(CRITICAL, "Boot image buffer address overlaps with aboot addresses.\n");
1330 return -1;
1331 }
1332
Matthew Qinbb7923d2015-02-09 10:56:09 +08001333 /*
1334 * Update loading flow of bootimage to support compressed/uncompressed
1335 * bootimage on both 64bit and 32bit platform.
1336 * 1. Load bootimage from emmc partition onto DDR.
1337 * 2. Check if bootimage is gzip format. If yes, decompress compressed kernel
1338 * 3. Check kernel header and update kernel load addr for 64bit and 32bit
1339 * platform accordingly.
1340 * 4. Sanity Check on kernel_addr and ramdisk_addr and copy data.
1341 */
Mayank Grover351a75e2017-05-30 20:06:08 +05301342 if (partition_multislot_is_supported())
1343 {
1344 current_active_slot = partition_find_active_slot();
1345 dprintf(INFO, "Loading boot image (%d) active_slot(%s): start\n",
1346 imagesize_actual, SUFFIX_SLOT(current_active_slot));
1347 }
1348 else
1349 {
1350 dprintf(INFO, "Loading (%s) image (%d): start\n",
1351 (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual);
1352 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08001353 bs_set_timestamp(BS_KERNEL_LOAD_START);
1354
Gaurav Nebhwani43e2a462016-03-17 21:32:56 +05301355 if ((target_get_max_flash_size() - page_size) < imagesize_actual)
1356 {
1357 dprintf(CRITICAL, "booimage size is greater than DDR can hold\n");
1358 return -1;
1359 }
Kishor PK9e91b592017-05-24 12:14:55 +05301360 offset = page_size;
1361 /* Read image without signature and header*/
1362 if (mmc_read(ptn + offset, (void *)(image_addr + offset), imagesize_actual - page_size))
Matthew Qin49e51fa2015-02-09 10:40:45 +08001363 {
1364 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
1365 return -1;
1366 }
1367
Mayank Grover351a75e2017-05-30 20:06:08 +05301368 if (partition_multislot_is_supported())
1369 {
1370 dprintf(INFO, "Loading boot image (%d) active_slot(%s): done\n",
1371 imagesize_actual, SUFFIX_SLOT(current_active_slot));
1372 }
1373 else
1374 {
1375 dprintf(INFO, "Loading (%s) image (%d): done\n",
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001376 (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual);
1377
Mayank Grover351a75e2017-05-30 20:06:08 +05301378 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08001379 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
1380
1381 /* Authenticate Kernel */
1382 dprintf(INFO, "use_signed_kernel=%d, is_unlocked=%d, is_tampered=%d.\n",
1383 (int) target_use_signed_kernel(),
1384 device.is_unlocked,
1385 device.is_tampered);
1386
Channagoud Kadabi736c4962015-08-21 11:56:52 -07001387 /* Change the condition a little bit to include the test framework support.
1388 * We would never reach this point if device is in fastboot mode, even if we did
1389 * that means we are in test mode, so execute kernel authentication part for the
1390 * tests */
Parth Dixitcb0c6082015-12-30 15:01:13 +05301391 if((target_use_signed_kernel() && (!device.is_unlocked)) || is_test_mode_enabled())
Matthew Qin49e51fa2015-02-09 10:40:45 +08001392 {
1393 offset = imagesize_actual;
Vijay Kumar Pendoti9c002ad2016-03-09 13:52:45 +05301394 if (check_aboot_addr_range_overlap((uintptr_t)image_addr + offset, page_size))
Matthew Qin49e51fa2015-02-09 10:40:45 +08001395 {
1396 dprintf(CRITICAL, "Signature read buffer address overlaps with aboot addresses.\n");
1397 return -1;
1398 }
1399
1400 /* Read signature */
1401 if(mmc_read(ptn + offset, (void *)(image_addr + offset), page_size))
1402 {
1403 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
1404 return -1;
1405 }
1406
1407 verify_signed_bootimg((uint32_t)image_addr, imagesize_actual);
Channagoud Kadabi736c4962015-08-21 11:56:52 -07001408 /* The purpose of our test is done here */
Parth Dixitcb0c6082015-12-30 15:01:13 +05301409 if(is_test_mode_enabled() && auth_kernel_img)
Channagoud Kadabi736c4962015-08-21 11:56:52 -07001410 return 0;
Matthew Qin49e51fa2015-02-09 10:40:45 +08001411 } else {
1412 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
1413 #ifdef TZ_SAVE_KERNEL_HASH
1414 aboot_save_boot_hash_mmc((uint32_t) image_addr, imagesize_actual);
1415 #endif /* TZ_SAVE_KERNEL_HASH */
Amit Blay4aa292f2015-04-28 21:55:59 +03001416
1417#ifdef MDTP_SUPPORT
1418 {
1419 /* Verify MDTP lock.
1420 * For boot & recovery partitions, MDTP will use boot_verifier APIs,
1421 * since verification was skipped in aboot. The signature is not part of the loaded image.
1422 */
1423 mdtp_ext_partition_verification_t ext_partition;
1424 ext_partition.partition = boot_into_recovery ? MDTP_PARTITION_RECOVERY : MDTP_PARTITION_BOOT;
1425 ext_partition.integrity_state = MDTP_PARTITION_STATE_UNSET;
1426 ext_partition.page_size = page_size;
1427 ext_partition.image_addr = (uint32)image_addr;
1428 ext_partition.image_size = imagesize_actual;
1429 ext_partition.sig_avail = FALSE;
1430 mdtp_fwlock_verify_lock(&ext_partition);
1431 }
1432#endif /* MDTP_SUPPORT */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001433 }
1434
Sridhar Parasuram00bfedb2015-05-26 14:21:27 -07001435#if VERIFIED_BOOT
taozhang93088bd2016-11-16 15:50:46 +08001436 if((boot_verify_get_state() == ORANGE) && (!boot_into_ffbm))
Reut Zysmanba8a9d52016-02-18 11:44:04 +02001437 {
1438#if FBCON_DISPLAY_MSG
1439 display_bootverify_menu(DISPLAY_MENU_ORANGE);
1440 wait_for_users_action();
1441#else
1442 dprintf(CRITICAL,
1443 "Your device has been unlocked and can't be trusted.\nWait for 5 seconds before proceeding\n");
1444 mdelay(5000);
1445#endif
1446 }
1447#endif
1448
1449#if VERIFIED_BOOT
Mayank Grover912eaa62017-10-26 12:08:53 +05301450 if (VB_M <= target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +05301451 {
1452 /* set boot and system versions. */
1453 set_os_version((unsigned char *)image_addr);
1454 // send root of trust
1455 if(!send_rot_command((uint32_t)device.is_unlocked))
1456 ASSERT(0);
1457 }
Parth Dixitbc9b6492015-10-18 00:41:38 +05301458#endif
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001459 /*
Matthew Qinbb7923d2015-02-09 10:56:09 +08001460 * Check if the kernel image is a gzip package. If yes, need to decompress it.
1461 * If not, continue booting.
1462 */
1463 if (is_gzip_package((unsigned char *)(image_addr + page_size), hdr->kernel_size))
1464 {
1465 out_addr = (unsigned char *)(image_addr + imagesize_actual + page_size);
1466 out_avai_len = target_get_max_flash_size() - imagesize_actual - page_size;
Matthew Qin0b15b322015-05-19 05:20:54 -04001467 dprintf(INFO, "decompressing kernel image: start\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001468 rc = decompress((unsigned char *)(image_addr + page_size),
1469 hdr->kernel_size, out_addr, out_avai_len,
1470 &dtb_offset, &out_len);
1471 if (rc)
1472 {
Matthew Qin0b15b322015-05-19 05:20:54 -04001473 dprintf(CRITICAL, "decompressing kernel image failed!!!\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001474 ASSERT(0);
1475 }
1476
Matthew Qin0b15b322015-05-19 05:20:54 -04001477 dprintf(INFO, "decompressing kernel image: done\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001478 kptr = (struct kernel64_hdr *)out_addr;
1479 kernel_start_addr = out_addr;
1480 kernel_size = out_len;
1481 } else {
Ameya Thakur10a33452016-06-13 14:24:26 -07001482 dprintf(INFO, "Uncpmpressed kernel in use\n");
1483 if (!strncmp((char*)(image_addr + page_size),
1484 PATCHED_KERNEL_MAGIC,
1485 sizeof(PATCHED_KERNEL_MAGIC) - 1)) {
1486 dprintf(INFO, "Patched kernel detected\n");
1487 kptr = (struct kernel64_hdr *)(image_addr + page_size +
1488 PATCHED_KERNEL_HEADER_SIZE);
1489 //The size of the kernel is stored at start of kernel image + 16
1490 //The dtb would start just after the kernel
1491 dtb_offset = *((uint32_t*)((unsigned char*)
1492 (image_addr + page_size +
1493 sizeof(PATCHED_KERNEL_MAGIC) -
1494 1)));
1495 //The actual kernel starts after the 20 byte header.
1496 kernel_start_addr = (unsigned char*)(image_addr +
1497 page_size + PATCHED_KERNEL_HEADER_SIZE);
1498 kernel_size = hdr->kernel_size;
1499 patched_kernel_hdr_size = PATCHED_KERNEL_HEADER_SIZE;
1500 } else {
1501 dprintf(INFO, "Kernel image not patched..Unable to locate dt offset\n");
1502 kptr = (struct kernel64_hdr *)(image_addr + page_size);
1503 kernel_start_addr = (unsigned char *)(image_addr + page_size);
1504 kernel_size = hdr->kernel_size;
1505 }
Matthew Qinbb7923d2015-02-09 10:56:09 +08001506 }
1507
1508 /*
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001509 * Update the kernel/ramdisk/tags address if the boot image header
1510 * has default values, these default values come from mkbootimg when
1511 * the boot image is flashed using fastboot flash:raw
1512 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001513 update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr));
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001514
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001515 /* Get virtual addresses since the hdr saves physical addresses. */
1516 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
1517 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
1518 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001519
Matthew Qinbb7923d2015-02-09 10:56:09 +08001520 kernel_size = ROUND_TO_PAGE(kernel_size, page_mask);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001521 /* Check if the addresses in the header are valid. */
Matthew Qinbb7923d2015-02-09 10:56:09 +08001522 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_size) ||
Mayank Grovere559cec2017-10-17 15:12:03 +05301523 check_ddr_addr_range_bound(hdr->kernel_addr, kernel_size) ||
1524 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual) ||
1525 check_ddr_addr_range_bound(hdr->ramdisk_addr, ramdisk_actual))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001526 {
Mayank Grovere559cec2017-10-17 15:12:03 +05301527 dprintf(CRITICAL, "kernel/ramdisk addresses are not valid.\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001528 return -1;
1529 }
1530
1531#ifndef DEVICE_TREE
Mayank Grovere559cec2017-10-17 15:12:03 +05301532 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE) ||
1533 check_ddr_addr_range_bound(hdr->tags_addr, MAX_TAGS_SIZE))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001534 {
Mayank Grovere559cec2017-10-17 15:12:03 +05301535 dprintf(CRITICAL, "Tags addresses are not valid.\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001536 return -1;
1537 }
1538#endif
1539
Matthew Qin49e51fa2015-02-09 10:40:45 +08001540 /* Move kernel, ramdisk and device tree to correct address */
Matthew Qinbb7923d2015-02-09 10:56:09 +08001541 memmove((void*) hdr->kernel_addr, kernel_start_addr, kernel_size);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001542 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001543
Matthew Qin49e51fa2015-02-09 10:40:45 +08001544 #if DEVICE_TREE
Parth Dixit4097b622016-03-15 14:42:27 +05301545 if(dt_size) {
Matthew Qin49e51fa2015-02-09 10:40:45 +08001546 dt_table_offset = ((uint32_t)image_addr + page_size + kernel_actual + ramdisk_actual + second_actual);
1547 table = (struct dt_table*) dt_table_offset;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001548
Matthew Qin49e51fa2015-02-09 10:40:45 +08001549 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
1550 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1551 return -1;
1552 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001553
P.V. Phani Kumar82916762016-03-09 09:20:19 +05301554 /* Its Error if, dt_hdr_size (table->num_entries * dt_entry size + Dev_Tree Header)
1555 goes beyound hdr->dt_size*/
Parth Dixit4097b622016-03-15 14:42:27 +05301556 if (dt_hdr_size > ROUND_TO_PAGE(dt_size,hdr->page_size)) {
P.V. Phani Kumar82916762016-03-09 09:20:19 +05301557 dprintf(CRITICAL, "ERROR: Invalid Device Tree size \n");
1558 return -1;
1559 }
1560
Matthew Qin49e51fa2015-02-09 10:40:45 +08001561 /* Find index of device tree within device tree table */
1562 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
1563 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1564 return -1;
1565 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001566
P.V. Phani Kumar3c333202016-03-09 11:54:37 +05301567 if(dt_entry.offset > (UINT_MAX - dt_entry.size)) {
1568 dprintf(CRITICAL, "ERROR: Device tree contents are Invalid\n");
1569 return -1;
1570 }
1571
1572 /* Ensure we are not overshooting dt_size with the dt_entry selected */
Parth Dixit4097b622016-03-15 14:42:27 +05301573 if ((dt_entry.offset + dt_entry.size) > dt_size) {
P.V. Phani Kumar3c333202016-03-09 11:54:37 +05301574 dprintf(CRITICAL, "ERROR: Device tree contents are Invalid\n");
1575 return -1;
1576 }
1577
Matthew Qin271927e2015-03-31 22:07:07 -04001578 if (is_gzip_package((unsigned char *)dt_table_offset + dt_entry.offset, dt_entry.size))
1579 {
1580 unsigned int compressed_size = 0;
1581 out_addr += out_len;
1582 out_avai_len -= out_len;
Matthew Qin0b15b322015-05-19 05:20:54 -04001583 dprintf(INFO, "decompressing dtb: start\n");
Matthew Qin271927e2015-03-31 22:07:07 -04001584 rc = decompress((unsigned char *)dt_table_offset + dt_entry.offset,
1585 dt_entry.size, out_addr, out_avai_len,
1586 &compressed_size, &dtb_size);
1587 if (rc)
1588 {
Matthew Qin0b15b322015-05-19 05:20:54 -04001589 dprintf(CRITICAL, "decompressing dtb failed!!!\n");
Matthew Qin271927e2015-03-31 22:07:07 -04001590 ASSERT(0);
1591 }
1592
Matthew Qin0b15b322015-05-19 05:20:54 -04001593 dprintf(INFO, "decompressing dtb: done\n");
Matthew Qin271927e2015-03-31 22:07:07 -04001594 best_match_dt_addr = out_addr;
1595 } else {
1596 best_match_dt_addr = (unsigned char *)dt_table_offset + dt_entry.offset;
1597 dtb_size = dt_entry.size;
1598 }
1599
Matthew Qin49e51fa2015-02-09 10:40:45 +08001600 /* Validate and Read device device tree in the tags_addr */
Mayank Grovere559cec2017-10-17 15:12:03 +05301601 if (check_aboot_addr_range_overlap(hdr->tags_addr, dtb_size) ||
1602 check_ddr_addr_range_bound(hdr->tags_addr, dtb_size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001603 {
Mayank Grovere559cec2017-10-17 15:12:03 +05301604 dprintf(CRITICAL, "Device tree addresses are not valid\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001605 return -1;
1606 }
Shashank Mittal162244e2011-08-08 19:01:25 -07001607
Matthew Qin271927e2015-03-31 22:07:07 -04001608 memmove((void *)hdr->tags_addr, (char *)best_match_dt_addr, dtb_size);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001609 } else {
1610 /* Validate the tags_addr */
Mayank Grovere559cec2017-10-17 15:12:03 +05301611 if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual) ||
1612 check_ddr_addr_range_bound(hdr->tags_addr, kernel_actual))
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001613 {
Mayank Grovere559cec2017-10-17 15:12:03 +05301614 dprintf(CRITICAL, "Device tree addresses are not valid.\n");
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001615 return -1;
1616 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08001617 /*
1618 * If appended dev tree is found, update the atags with
1619 * memory address to the DTB appended location on RAM.
1620 * Else update with the atags address in the kernel header
1621 */
1622 void *dtb;
Ameya Thakur10a33452016-06-13 14:24:26 -07001623 dtb = dev_tree_appended(
1624 (void*)(image_addr + page_size +
1625 patched_kernel_hdr_size),
1626 hdr->kernel_size, dtb_offset,
1627 (void *)hdr->tags_addr);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001628 if (!dtb) {
1629 dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001630 return -1;
1631 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07001632 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08001633 #endif
Shashank Mittal23b8f422010-04-16 19:27:21 -07001634
Stanimir Varbanov69ec5462013-07-18 18:17:42 +03001635 if (boot_into_recovery && !device.is_unlocked && !device.is_tampered)
1636 target_load_ssd_keystore();
1637
Shashank Mittal23b8f422010-04-16 19:27:21 -07001638unified_boot:
Shashank Mittal23b8f422010-04-16 19:27:21 -07001639
Dima Zavin77e41f32013-03-06 16:10:43 -08001640 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07001641 (const char *)hdr->cmdline, board_machtype(),
Shashank Mittal23b8f422010-04-16 19:27:21 -07001642 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
1643
1644 return 0;
1645}
1646
Dima Zavin214cc642009-01-26 11:16:21 -08001647int boot_linux_from_flash(void)
1648{
1649 struct boot_img_hdr *hdr = (void*) buf;
Dima Zavin214cc642009-01-26 11:16:21 -08001650 struct ptentry *ptn;
1651 struct ptable *ptable;
1652 unsigned offset = 0;
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001653
Shashank Mittalcd98d472011-08-02 14:29:24 -07001654 unsigned char *image_addr = 0;
1655 unsigned kernel_actual;
1656 unsigned ramdisk_actual;
1657 unsigned imagesize_actual;
vijay kumar8f53e362015-11-24 13:38:11 +05301658 unsigned second_actual = 0;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001659
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001660#if DEVICE_TREE
1661 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -07001662 struct dt_entry dt_entry;
vijay kumar8f53e362015-11-24 13:38:11 +05301663 unsigned dt_table_offset;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001664 uint32_t dt_actual;
Deepa Dinamani19648b42013-09-05 17:05:55 -07001665 uint32_t dt_hdr_size;
vijay kumar8f53e362015-11-24 13:38:11 +05301666 unsigned int dtb_size = 0;
1667 unsigned char *best_match_dt_addr = NULL;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001668#endif
1669
David Ng183a7422009-12-07 14:55:21 -08001670 if (target_is_emmc_boot()) {
1671 hdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
1672 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1673 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
1674 return -1;
1675 }
1676 goto continue_boot;
1677 }
1678
Dima Zavin214cc642009-01-26 11:16:21 -08001679 ptable = flash_get_ptable();
1680 if (ptable == NULL) {
1681 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1682 return -1;
1683 }
1684
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001685 if(!boot_into_recovery)
1686 {
1687 ptn = ptable_find(ptable, "boot");
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001688
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001689 if (ptn == NULL) {
1690 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1691 return -1;
1692 }
1693 }
1694 else
1695 {
1696 ptn = ptable_find(ptable, "recovery");
1697 if (ptn == NULL) {
1698 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
1699 return -1;
1700 }
Dima Zavin214cc642009-01-26 11:16:21 -08001701 }
1702
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001703 if (flash_read(ptn, offset, buf, page_size)) {
Dima Zavin214cc642009-01-26 11:16:21 -08001704 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
1705 return -1;
1706 }
Dima Zavin214cc642009-01-26 11:16:21 -08001707
1708 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001709 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Dima Zavin214cc642009-01-26 11:16:21 -08001710 return -1;
1711 }
1712
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001713 if (hdr->page_size != page_size) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001714 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 -08001715 return -1;
1716 }
1717
Kishor PK9e91b592017-05-24 12:14:55 +05301718 image_addr = (unsigned char *)target_get_scratch_address();
1719 memcpy(image_addr, (void *)buf, page_size);
vijay kumar287bb542015-09-29 13:01:52 +05301720
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001721 /*
1722 * Update the kernel/ramdisk/tags address if the boot image header
1723 * has default values, these default values come from mkbootimg when
1724 * the boot image is flashed using fastboot flash:raw
1725 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001726 update_ker_tags_rdisk_addr(hdr, false);
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001727
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001728 /* Get virtual addresses since the hdr saves physical addresses. */
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001729 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
1730 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
1731 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
1732
1733 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1734 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Mayank Grover032736f2017-07-14 20:34:51 +05301735 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001736
Kishor PK9e91b592017-05-24 12:14:55 +05301737 /* ensure commandline is terminated */
1738 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
1739
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001740 /* Check if the addresses in the header are valid. */
1741 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
Mayank Grovere559cec2017-10-17 15:12:03 +05301742 check_ddr_addr_range_bound(hdr->kernel_addr, kernel_actual) ||
1743 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual) ||
1744 check_ddr_addr_range_bound(hdr->ramdisk_addr, ramdisk_actual))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001745 {
Mayank Grovere559cec2017-10-17 15:12:03 +05301746 dprintf(CRITICAL, "kernel/ramdisk addresses are not valid.\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001747 return -1;
1748 }
1749
1750#ifndef DEVICE_TREE
Kishor PKd8ddcad2017-07-27 13:53:57 +05301751 if (UINT_MAX < ((uint64_t)kernel_actual + (uint64_t)ramdisk_actual+ (uint64_t)second_actual + page_size)) {
Mayank Grover032736f2017-07-14 20:34:51 +05301752 dprintf(CRITICAL, "Integer overflow detected in bootimage header fields\n");
1753 return -1;
1754 }
Kishor PKd8ddcad2017-07-27 13:53:57 +05301755 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + second_actual);
Mayank Grover032736f2017-07-14 20:34:51 +05301756
Mayank Grovere559cec2017-10-17 15:12:03 +05301757 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE) ||
1758 check_ddr_addr_range_bound(hdr->tags_addr, MAX_TAGS_SIZE))
Vijay Kumar Pendotid3ed20e2016-09-20 00:34:46 +05301759 {
Mayank Grovere559cec2017-10-17 15:12:03 +05301760 dprintf(CRITICAL, "Tags addresses are not valid.\n");
Vijay Kumar Pendotid3ed20e2016-09-20 00:34:46 +05301761 return -1;
1762 }
1763#else
1764
1765#ifndef OSVERSION_IN_BOOTIMAGE
1766 dt_size = hdr->dt_size;
1767#endif
Mayank Grover032736f2017-07-14 20:34:51 +05301768 dt_actual = ROUND_TO_PAGE(dt_size, page_mask);
Kishor PKd8ddcad2017-07-27 13:53:57 +05301769 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 +05301770 dprintf(CRITICAL, "Integer overflow detected in bootimage header fields\n");
1771 return -1;
1772 }
1773
Kishor PKd8ddcad2017-07-27 13:53:57 +05301774 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + second_actual + dt_actual);
Mayank Grover032736f2017-07-14 20:34:51 +05301775
Mayank Grovere559cec2017-10-17 15:12:03 +05301776 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_size) ||
1777 check_ddr_addr_range_bound(hdr->tags_addr, dt_size))
Mayank Grover032736f2017-07-14 20:34:51 +05301778 {
Mayank Grovere559cec2017-10-17 15:12:03 +05301779 dprintf(CRITICAL, "Device tree addresses are not valid.\n");
Mayank Grover032736f2017-07-14 20:34:51 +05301780 return -1;
1781 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001782#endif
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001783
Shashank Mittalcd98d472011-08-02 14:29:24 -07001784 /* Authenticate Kernel */
Deepa Dinamani23b60d42013-06-24 18:10:52 -07001785 if(target_use_signed_kernel() && (!device.is_unlocked))
Shashank Mittalcd98d472011-08-02 14:29:24 -07001786 {
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001787 dprintf(INFO, "Loading (%s) image (%d): start\n",
1788 (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001789 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -08001790
Vijay Kumar Pendotib3458362016-07-05 13:45:54 +05301791 if (UINT_MAX - page_size < imagesize_actual)
1792 {
1793 dprintf(CRITICAL,"Integer overflow detected in bootimage header fields %u %s\n", __LINE__,__func__);
1794 return -1;
1795 }
1796
1797 /*Check the availability of RAM before reading boot image + max signature length from flash*/
1798 if (target_get_max_flash_size() < (imagesize_actual + page_size))
1799 {
1800 dprintf(CRITICAL, "bootimage size is greater than DDR can hold\n");
1801 return -1;
1802 }
Kishor PK9e91b592017-05-24 12:14:55 +05301803 offset = page_size;
1804 /* Read image without signature and header*/
1805 if (flash_read(ptn, offset, (void *)(image_addr + offset), imagesize_actual - page_size))
Shashank Mittalcd98d472011-08-02 14:29:24 -07001806 {
1807 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
1808 return -1;
1809 }
Dima Zavin214cc642009-01-26 11:16:21 -08001810
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001811 dprintf(INFO, "Loading (%s) image (%d): done\n",
1812 (!boot_into_recovery ? "boot" : "recovery"), imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001813 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Amol Jadib6be5c12012-11-14 13:39:51 -08001814
Shashank Mittalcd98d472011-08-02 14:29:24 -07001815 offset = imagesize_actual;
1816 /* 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);
Shashank Mittalcd98d472011-08-02 14:29:24 -07001824
1825 /* Move kernel and ramdisk to correct address */
Matthew Qinbb7923d2015-02-09 10:56:09 +08001826 memmove((void*) hdr->kernel_addr, (char*) (image_addr + page_size), hdr->kernel_size);
1827 memmove((void*) hdr->ramdisk_addr, (char*) (image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001828#if DEVICE_TREE
Parth Dixit4097b622016-03-15 14:42:27 +05301829 if(dt_size != 0) {
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001830
vijay kumar8f53e362015-11-24 13:38:11 +05301831 dt_table_offset = ((uint32_t)image_addr + page_size + kernel_actual + ramdisk_actual + second_actual);
1832
1833 table = (struct dt_table*) dt_table_offset;
1834
1835 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0){
1836 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1837 return -1;
1838 }
1839
Mayank Grover81e019e2017-07-14 15:59:06 +05301840 /* Its Error if, dt_hdr_size (table->num_entries * dt_entry size + Dev_Tree Header)
1841 goes beyound hdr->dt_size*/
1842 if (dt_hdr_size > ROUND_TO_PAGE(dt_size, hdr->page_size)) {
1843 dprintf(CRITICAL, "ERROR: Invalid Device Tree size \n");
1844 return -1;
1845 }
1846
vijay kumar8f53e362015-11-24 13:38:11 +05301847 /* Find index of device tree within device tree table */
1848 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
1849 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1850 return -1;
1851 }
1852
1853 /* Validate and Read device device tree in the "tags_add */
Mayank Grovere559cec2017-10-17 15:12:03 +05301854 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size) ||
1855 check_ddr_addr_range_bound(hdr->tags_addr, dt_entry.size)){
1856 dprintf(CRITICAL, "Device tree addresses are not valid.\n");
vijay kumar8f53e362015-11-24 13:38:11 +05301857 return -1;
1858 }
1859
Mayank Groverebf85ae2017-01-25 13:35:37 +05301860 if(dt_entry.offset > (UINT_MAX - dt_entry.size)) {
1861 dprintf(CRITICAL, "ERROR: Device tree contents are Invalid\n");
1862 return -1;
1863 }
1864
1865 /* Ensure we are not overshooting dt_size with the dt_entry selected */
1866 if ((dt_entry.offset + dt_entry.size) > dt_size) {
1867 dprintf(CRITICAL, "ERROR: Device tree contents are Invalid\n");
1868 return -1;
1869 }
1870
vijay kumar8f53e362015-11-24 13:38:11 +05301871 best_match_dt_addr = (unsigned char *)table + dt_entry.offset;
1872 dtb_size = dt_entry.size;
1873 memmove((void *)hdr->tags_addr, (char *)best_match_dt_addr, dtb_size);
1874 }
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001875#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001876
1877 /* Make sure everything from scratch address is read before next step!*/
Shashank Mittala0032282011-08-26 14:50:11 -07001878 if(device.is_tampered)
Shashank Mittal162244e2011-08-08 19:01:25 -07001879 {
1880 write_device_info_flash(&device);
1881 }
Channagoud Kadabi5c86fe32012-02-16 10:58:48 +05301882#if USE_PCOM_SECBOOT
1883 set_tamper_flag(device.is_tampered);
1884#endif
Shashank Mittalcd98d472011-08-02 14:29:24 -07001885 }
1886 else
1887 {
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001888 dprintf(INFO, "Loading (%s) image (%d): start\n",
1889 (!boot_into_recovery ? "boot" : "recovery"), kernel_actual + ramdisk_actual);
1890
Amol Jadi492d5a52013-03-15 16:12:34 -07001891 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -08001892
Mayank Grover032736f2017-07-14 20:34:51 +05301893 offset = page_size;
Vijay Kumar Pendoti47bc1282016-05-05 15:46:17 +05301894 if (UINT_MAX - offset < kernel_actual)
1895 {
1896 dprintf(CRITICAL, "ERROR: Integer overflow in boot image header %s\t%d\n",__func__,__LINE__);
1897 return -1;
1898 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001899 if (flash_read(ptn, offset, (void *)hdr->kernel_addr, kernel_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -07001900 dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
1901 return -1;
1902 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001903 offset += kernel_actual;
Vijay Kumar Pendoti47bc1282016-05-05 15:46:17 +05301904 if (UINT_MAX - offset < ramdisk_actual)
1905 {
1906 dprintf(CRITICAL, "ERROR: Integer overflow in boot image header %s\t%d\n",__func__,__LINE__);
1907 return -1;
1908 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001909 if (flash_read(ptn, offset, (void *)hdr->ramdisk_addr, ramdisk_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -07001910 dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
1911 return -1;
1912 }
Vijay Kumar Pendoti47bc1282016-05-05 15:46:17 +05301913
Amol Jadib6be5c12012-11-14 13:39:51 -08001914 offset += ramdisk_actual;
1915
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001916 dprintf(INFO, "Loading (%s) image (%d): done\n",
1917 (!boot_into_recovery ? "boot" : "recovery"), kernel_actual + ramdisk_actual);
1918
Amol Jadi492d5a52013-03-15 16:12:34 -07001919 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001920
1921 if(hdr->second_size != 0) {
Vijay Kumar Pendoti47bc1282016-05-05 15:46:17 +05301922 if (UINT_MAX - offset < second_actual)
1923 {
1924 dprintf(CRITICAL, "ERROR: Integer overflow in boot image header %s\t%d\n",__func__,__LINE__);
1925 return -1;
1926 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001927 offset += second_actual;
1928 /* Second image loading not implemented. */
1929 ASSERT(0);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001930 }
1931
1932#if DEVICE_TREE
Parth Dixit4097b622016-03-15 14:42:27 +05301933 if(dt_size != 0) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001934
1935 /* Read the device tree table into buffer */
1936 if(flash_read(ptn, offset, (void *) dt_buf, page_size)) {
1937 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
1938 return -1;
1939 }
1940
1941 table = (struct dt_table*) dt_buf;
1942
Deepa Dinamani19648b42013-09-05 17:05:55 -07001943 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001944 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1945 return -1;
1946 }
1947
P.V. Phani Kumar82916762016-03-09 09:20:19 +05301948 /* Its Error if, dt_hdr_size (table->num_entries * dt_entry size + Dev_Tree Header)
1949 goes beyound hdr->dt_size*/
Parth Dixit4097b622016-03-15 14:42:27 +05301950 if (dt_hdr_size > ROUND_TO_PAGE(dt_size,hdr->page_size)) {
P.V. Phani Kumar82916762016-03-09 09:20:19 +05301951 dprintf(CRITICAL, "ERROR: Invalid Device Tree size \n");
1952 return -1;
1953 }
1954
Mayank Grover032736f2017-07-14 20:34:51 +05301955 table = (void *) target_get_scratch_address();
1956 /*Check the availability of RAM before reading boot image + max signature length from flash*/
1957 if (target_get_max_flash_size() < dt_actual)
1958 {
1959 dprintf(CRITICAL, "ERROR: dt_image size is greater than DDR can hold\n");
Deepa Dinamani19648b42013-09-05 17:05:55 -07001960 return -1;
1961 }
1962
Mayank Grover032736f2017-07-14 20:34:51 +05301963 /* Read the entire device tree table into buffer */
1964 if(flash_read(ptn, offset, (void *)table, dt_actual)) {
1965 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
1966 return -1;
1967 }
Deepa Dinamani19648b42013-09-05 17:05:55 -07001968
Joel Kingaa335dc2013-06-03 16:11:08 -07001969 /* Find index of device tree within device tree table */
1970 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001971 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1972 return -1;
1973 }
1974
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001975 /* Validate and Read device device tree in the "tags_add */
Mayank Grovere559cec2017-10-17 15:12:03 +05301976 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size) ||
1977 check_ddr_addr_range_bound(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001978 {
Mayank Grovere559cec2017-10-17 15:12:03 +05301979 dprintf(CRITICAL, "Device tree addresses are not valid.\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001980 return -1;
1981 }
1982
Mayank Grover032736f2017-07-14 20:34:51 +05301983 if(dt_entry.offset > (UINT_MAX - dt_entry.size)) {
1984 dprintf(CRITICAL, "ERROR: Device tree contents are Invalid\n");
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001985 return -1;
1986 }
Mayank Grover032736f2017-07-14 20:34:51 +05301987
1988 /* Ensure we are not overshooting dt_size with the dt_entry selected */
1989 if ((dt_entry.offset + dt_entry.size) > dt_size) {
1990 dprintf(CRITICAL, "ERROR: Device tree contents are Invalid\n");
1991 return -1;
1992 }
1993
1994 best_match_dt_addr = (unsigned char *)table + dt_entry.offset;
1995 dtb_size = dt_entry.size;
1996 memmove((void *)hdr->tags_addr, (char *)best_match_dt_addr, dtb_size);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001997 }
1998#endif
1999
Shashank Mittalcd98d472011-08-02 14:29:24 -07002000 }
David Ng183a7422009-12-07 14:55:21 -08002001continue_boot:
Dima Zavin214cc642009-01-26 11:16:21 -08002002
Dima Zavin214cc642009-01-26 11:16:21 -08002003 /* TODO: create/pass atags to kernel */
2004
Ajay Dudanie28a6072011-07-01 13:59:46 -07002005 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07002006 (const char *)hdr->cmdline, board_machtype(),
Dima Zavin214cc642009-01-26 11:16:21 -08002007 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
2008
2009 return 0;
2010}
Brian Swetland9c4c0752009-01-25 16:23:50 -08002011
Shashank Mittal162244e2011-08-08 19:01:25 -07002012void write_device_info_mmc(device_info *dev)
2013{
Shashank Mittal162244e2011-08-08 19:01:25 -07002014 unsigned long long ptn = 0;
2015 unsigned long long size;
2016 int index = INVALID_PTN;
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07002017 uint32_t blocksize;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002018 uint8_t lun = 0;
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002019 uint32_t ret = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07002020
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002021 if (devinfo_present)
2022 index = partition_get_index("devinfo");
2023 else
2024 index = partition_get_index("aboot");
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07002025
Shashank Mittal162244e2011-08-08 19:01:25 -07002026 ptn = partition_get_offset(index);
2027 if(ptn == 0)
2028 {
2029 return;
2030 }
2031
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002032 lun = partition_get_lun(index);
2033 mmc_set_lun(lun);
2034
Shashank Mittal162244e2011-08-08 19:01:25 -07002035 size = partition_get_size(index);
2036
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07002037 blocksize = mmc_get_device_blocksize();
2038
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002039 if (devinfo_present)
2040 ret = mmc_write(ptn, blocksize, (void *)info_buf);
2041 else
2042 ret = mmc_write((ptn + size - blocksize), blocksize, (void *)info_buf);
2043 if (ret)
Shashank Mittal162244e2011-08-08 19:01:25 -07002044 {
2045 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
Sridhar Parasurame1a97922015-07-27 15:31:22 -07002046 ASSERT(0);
Shashank Mittal162244e2011-08-08 19:01:25 -07002047 }
2048}
2049
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002050void read_device_info_mmc(struct device_info *info)
Shashank Mittal162244e2011-08-08 19:01:25 -07002051{
Shashank Mittal162244e2011-08-08 19:01:25 -07002052 unsigned long long ptn = 0;
2053 unsigned long long size;
2054 int index = INVALID_PTN;
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07002055 uint32_t blocksize;
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002056 uint32_t ret = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07002057
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002058 if ((index = partition_get_index("devinfo")) < 0)
2059 {
2060 devinfo_present = false;
2061 index = partition_get_index("aboot");
2062 }
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07002063
Shashank Mittal162244e2011-08-08 19:01:25 -07002064 ptn = partition_get_offset(index);
2065 if(ptn == 0)
2066 {
2067 return;
2068 }
2069
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07002070 mmc_set_lun(partition_get_lun(index));
2071
Shashank Mittal162244e2011-08-08 19:01:25 -07002072 size = partition_get_size(index);
2073
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07002074 blocksize = mmc_get_device_blocksize();
2075
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002076 if (devinfo_present)
2077 ret = mmc_read(ptn, (void *)info_buf, blocksize);
2078 else
2079 ret = mmc_read((ptn + size - blocksize), (void *)info_buf, blocksize);
2080 if (ret)
Shashank Mittal162244e2011-08-08 19:01:25 -07002081 {
2082 dprintf(CRITICAL, "ERROR: Cannot read device info\n");
Sridhar Parasurame1a97922015-07-27 15:31:22 -07002083 ASSERT(0);
Shashank Mittal162244e2011-08-08 19:01:25 -07002084 }
Shashank Mittal162244e2011-08-08 19:01:25 -07002085}
2086
2087void write_device_info_flash(device_info *dev)
2088{
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002089 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
Shashank Mittal162244e2011-08-08 19:01:25 -07002090 struct ptentry *ptn;
2091 struct ptable *ptable;
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002092 if(info == NULL)
2093 {
2094 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
2095 ASSERT(0);
2096 }
2097 info_buf = info;
Shashank Mittal162244e2011-08-08 19:01:25 -07002098 ptable = flash_get_ptable();
2099 if (ptable == NULL)
2100 {
2101 dprintf(CRITICAL, "ERROR: Partition table not found\n");
2102 return;
2103 }
2104
2105 ptn = ptable_find(ptable, "devinfo");
2106 if (ptn == NULL)
2107 {
Smita Ghosh670c8b82015-05-07 09:30:03 -07002108 dprintf(CRITICAL, "ERROR: No devinfo partition found\n");
Shashank Mittal162244e2011-08-08 19:01:25 -07002109 return;
2110 }
2111
Mayank Groverdedbc892017-10-24 13:41:34 +05302112 memset(info, 0, BOOT_IMG_MAX_PAGE_SIZE);
Shashank Mittal162244e2011-08-08 19:01:25 -07002113 memcpy(info, dev, sizeof(device_info));
2114
2115 if (flash_write(ptn, 0, (void *)info_buf, page_size))
2116 {
2117 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
2118 return;
2119 }
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002120 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07002121}
2122
vijay kumarc65876c2015-04-24 13:29:16 +05302123static int read_allow_oem_unlock(device_info *dev)
2124{
vijay kumarc65876c2015-04-24 13:29:16 +05302125 unsigned offset;
2126 int index;
2127 unsigned long long ptn;
2128 unsigned long long ptn_size;
2129 unsigned blocksize = mmc_get_device_blocksize();
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07002130 STACKBUF_DMA_ALIGN(buf, blocksize);
vijay kumarc65876c2015-04-24 13:29:16 +05302131
vijay kumarca2e6812015-07-08 20:28:25 +05302132 index = partition_get_index(frp_ptns[0]);
vijay kumarc65876c2015-04-24 13:29:16 +05302133 if (index == INVALID_PTN)
2134 {
vijay kumarca2e6812015-07-08 20:28:25 +05302135 index = partition_get_index(frp_ptns[1]);
2136 if (index == INVALID_PTN)
2137 {
2138 dprintf(CRITICAL, "Neither '%s' nor '%s' partition found\n", frp_ptns[0],frp_ptns[1]);
2139 return -1;
2140 }
vijay kumarc65876c2015-04-24 13:29:16 +05302141 }
2142
2143 ptn = partition_get_offset(index);
2144 ptn_size = partition_get_size(index);
2145 offset = ptn_size - blocksize;
2146
Mayank Grover48860402016-11-29 12:34:53 +05302147 /* Set Lun for partition */
2148 mmc_set_lun(partition_get_lun(index));
2149
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07002150 if (mmc_read(ptn + offset, (void *)buf, blocksize))
vijay kumarc65876c2015-04-24 13:29:16 +05302151 {
2152 dprintf(CRITICAL, "Reading MMC failed\n");
2153 return -1;
2154 }
2155
2156 /*is_allow_unlock is a bool value stored at the LSB of last byte*/
2157 is_allow_unlock = buf[blocksize-1] & 0x01;
2158 return 0;
2159}
2160
2161static int write_allow_oem_unlock(bool allow_unlock)
2162{
vijay kumarc65876c2015-04-24 13:29:16 +05302163 unsigned offset;
vijay kumarc65876c2015-04-24 13:29:16 +05302164 int index;
2165 unsigned long long ptn;
2166 unsigned long long ptn_size;
2167 unsigned blocksize = mmc_get_device_blocksize();
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07002168 STACKBUF_DMA_ALIGN(buf, blocksize);
vijay kumarc65876c2015-04-24 13:29:16 +05302169
vijay kumarca2e6812015-07-08 20:28:25 +05302170 index = partition_get_index(frp_ptns[0]);
vijay kumarc65876c2015-04-24 13:29:16 +05302171 if (index == INVALID_PTN)
2172 {
vijay kumarca2e6812015-07-08 20:28:25 +05302173 index = partition_get_index(frp_ptns[1]);
2174 if (index == INVALID_PTN)
2175 {
2176 dprintf(CRITICAL, "Neither '%s' nor '%s' partition found\n", frp_ptns[0],frp_ptns[1]);
2177 return -1;
2178 }
vijay kumarc65876c2015-04-24 13:29:16 +05302179 }
2180
2181 ptn = partition_get_offset(index);
2182 ptn_size = partition_get_size(index);
2183 offset = ptn_size - blocksize;
Mayank Grover48860402016-11-29 12:34:53 +05302184 mmc_set_lun(partition_get_lun(index));
vijay kumarc65876c2015-04-24 13:29:16 +05302185
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07002186 if (mmc_read(ptn + offset, (void *)buf, blocksize))
vijay kumarc65876c2015-04-24 13:29:16 +05302187 {
2188 dprintf(CRITICAL, "Reading MMC failed\n");
2189 return -1;
2190 }
2191
2192 /*is_allow_unlock is a bool value stored at the LSB of last byte*/
2193 buf[blocksize-1] = allow_unlock;
2194 if (mmc_write(ptn + offset, blocksize, buf))
2195 {
2196 dprintf(CRITICAL, "Writing MMC failed\n");
2197 return -1;
2198 }
2199
2200 return 0;
2201}
2202
Shashank Mittal162244e2011-08-08 19:01:25 -07002203void read_device_info_flash(device_info *dev)
2204{
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002205 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
Shashank Mittal162244e2011-08-08 19:01:25 -07002206 struct ptentry *ptn;
2207 struct ptable *ptable;
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002208 if(info == NULL)
2209 {
2210 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
2211 ASSERT(0);
2212 }
2213 info_buf = info;
Shashank Mittal162244e2011-08-08 19:01:25 -07002214 ptable = flash_get_ptable();
2215 if (ptable == NULL)
2216 {
2217 dprintf(CRITICAL, "ERROR: Partition table not found\n");
2218 return;
2219 }
2220
2221 ptn = ptable_find(ptable, "devinfo");
2222 if (ptn == NULL)
2223 {
Smita Ghosh670c8b82015-05-07 09:30:03 -07002224 dprintf(CRITICAL, "ERROR: No devinfo partition found\n");
Shashank Mittal162244e2011-08-08 19:01:25 -07002225 return;
2226 }
2227
2228 if (flash_read(ptn, 0, (void *)info_buf, page_size))
2229 {
2230 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
2231 return;
2232 }
2233
2234 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
2235 {
Shashank Mittal162244e2011-08-08 19:01:25 -07002236 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
2237 info->is_unlocked = 0;
Shashank Mittala0032282011-08-26 14:50:11 -07002238 info->is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07002239 write_device_info_flash(info);
2240 }
2241 memcpy(dev, info, sizeof(device_info));
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002242 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07002243}
2244
2245void write_device_info(device_info *dev)
2246{
2247 if(target_is_emmc_boot())
2248 {
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002249 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
2250 if(info == NULL)
2251 {
2252 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
2253 ASSERT(0);
2254 }
2255 info_buf = info;
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002256 memcpy(info, dev, sizeof(struct device_info));
2257
2258#if USE_RPMB_FOR_DEVINFO
Mayank Grover912eaa62017-10-26 12:08:53 +05302259 if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05302260 is_secure_boot_enable()) {
2261 if((write_device_info_rpmb((void*) info, PAGE_SIZE)) < 0)
2262 ASSERT(0);
Sridhar Parasurame1a97922015-07-27 15:31:22 -07002263 }
Channagoud Kadabic80cb492015-04-28 16:08:28 -07002264 else
2265 write_device_info_mmc(info);
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002266#else
2267 write_device_info_mmc(info);
2268#endif
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002269 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07002270 }
2271 else
2272 {
2273 write_device_info_flash(dev);
2274 }
2275}
2276
2277void read_device_info(device_info *dev)
2278{
2279 if(target_is_emmc_boot())
2280 {
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002281 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
2282 if(info == NULL)
2283 {
2284 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
2285 ASSERT(0);
2286 }
2287 info_buf = info;
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002288
2289#if USE_RPMB_FOR_DEVINFO
Mayank Grover912eaa62017-10-26 12:08:53 +05302290 if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05302291 is_secure_boot_enable()) {
2292 if((read_device_info_rpmb((void*) info, PAGE_SIZE)) < 0)
2293 ASSERT(0);
Sridhar Parasurame1a97922015-07-27 15:31:22 -07002294 }
Channagoud Kadabic80cb492015-04-28 16:08:28 -07002295 else
2296 read_device_info_mmc(info);
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002297#else
2298 read_device_info_mmc(info);
2299#endif
2300
2301 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
2302 {
2303 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
lijuang511a2b52015-08-14 20:50:51 +08002304 if (is_secure_boot_enable()) {
Channagoud Kadabi05f78ba2015-07-06 11:58:14 -07002305 info->is_unlocked = 0;
Mayank Grover889be1b2017-09-12 20:12:23 +05302306#if VERIFIED_BOOT
Mayank Grover912eaa62017-10-26 12:08:53 +05302307 if (VB_M <= target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +05302308 info->is_unlock_critical = 0;
Parth Dixitddbc7352015-10-18 03:13:31 +05302309#endif
lijuang511a2b52015-08-14 20:50:51 +08002310 } else {
Channagoud Kadabi2fda4092015-07-07 13:34:11 -07002311 info->is_unlocked = 1;
Mayank Grover889be1b2017-09-12 20:12:23 +05302312#if VERIFIED_BOOT
Mayank Grover912eaa62017-10-26 12:08:53 +05302313 if (VB_M <= target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +05302314 info->is_unlock_critical = 1;
Parth Dixitddbc7352015-10-18 03:13:31 +05302315#endif
lijuang511a2b52015-08-14 20:50:51 +08002316 }
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002317 info->is_tampered = 0;
2318 info->charger_screen_enabled = 0;
Mayank Grover889be1b2017-09-12 20:12:23 +05302319#if VERIFIED_BOOT
Mayank Grover912eaa62017-10-26 12:08:53 +05302320 if (VB_M <= target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +05302321 info->verity_mode = 1; //enforcing by default
Parth Dixitddbc7352015-10-18 03:13:31 +05302322#endif
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002323 write_device_info(info);
2324 }
2325 memcpy(dev, info, sizeof(device_info));
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002326 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07002327 }
2328 else
2329 {
2330 read_device_info_flash(dev);
2331 }
2332}
2333
2334void reset_device_info()
2335{
2336 dprintf(ALWAYS, "reset_device_info called.");
Shashank Mittala0032282011-08-26 14:50:11 -07002337 device.is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07002338 write_device_info(&device);
2339}
2340
2341void set_device_root()
2342{
2343 dprintf(ALWAYS, "set_device_root called.");
Shashank Mittala0032282011-08-26 14:50:11 -07002344 device.is_tampered = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -07002345 write_device_info(&device);
2346}
2347
lijuang4ece1e72015-08-14 21:02:36 +08002348/* set device unlock value
2349 * Must check FRP before call this function
2350 * Need to wipe data when unlock status changed
2351 * type 0: oem unlock
2352 * type 1: unlock critical
2353 * status 0: unlock as false
2354 * status 1: lock as true
2355 */
2356void set_device_unlock_value(int type, bool status)
lijuang21f12f52015-08-22 16:22:19 +08002357{
lijuang4ece1e72015-08-14 21:02:36 +08002358 if (type == UNLOCK)
2359 device.is_unlocked = status;
Mayank Grover889be1b2017-09-12 20:12:23 +05302360#if VERIFIED_BOOT
Mayank Grover912eaa62017-10-26 12:08:53 +05302361 else if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05302362 type == UNLOCK_CRITICAL)
2363 device.is_unlock_critical = status;
Parth Dixitddbc7352015-10-18 03:13:31 +05302364#endif
lijuang4ece1e72015-08-14 21:02:36 +08002365 write_device_info(&device);
2366}
2367
2368static void set_device_unlock(int type, bool status)
2369{
2370 int is_unlocked = -1;
2371 char response[MAX_RSP_SIZE];
2372
2373 /* check device unlock status if it is as expected */
2374 if (type == UNLOCK)
2375 is_unlocked = device.is_unlocked;
Mayank Grover889be1b2017-09-12 20:12:23 +05302376#if VERIFIED_BOOT
Mayank Grover912eaa62017-10-26 12:08:53 +05302377 if(VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05302378 type == UNLOCK_CRITICAL)
2379 {
2380 is_unlocked = device.is_unlock_critical;
2381 }
Parth Dixitddbc7352015-10-18 03:13:31 +05302382#endif
lijuang4ece1e72015-08-14 21:02:36 +08002383 if (is_unlocked == status) {
2384 snprintf(response, sizeof(response), "\tDevice already : %s", (status ? "unlocked!" : "locked!"));
2385 fastboot_info(response);
2386 fastboot_okay("");
2387 return;
2388 }
2389
2390 /* status is true, it means to unlock device */
2391 if (status) {
lijuang21f12f52015-08-22 16:22:19 +08002392 if(!is_allow_unlock) {
2393 fastboot_fail("oem unlock is not allowed");
2394 return;
2395 }
2396
lijuang4ece1e72015-08-14 21:02:36 +08002397#if FBCON_DISPLAY_MSG
lijuang9a7d3b92015-11-30 14:41:24 +08002398 display_unlock_menu(type);
lijuang4ece1e72015-08-14 21:02:36 +08002399 fastboot_okay("");
2400 return;
2401#else
2402 if (type == UNLOCK) {
2403 fastboot_fail("Need wipe userdata. Do 'fastboot oem unlock-go'");
2404 return;
2405 }
2406#endif
lijuang21f12f52015-08-22 16:22:19 +08002407 }
lijuang4ece1e72015-08-14 21:02:36 +08002408
2409 set_device_unlock_value(type, status);
2410
2411 /* wipe data */
2412 struct recovery_message msg;
Kishor PK60a68212017-05-08 16:55:57 +05302413 memset(&msg, 0, sizeof(msg));
lijuang4ece1e72015-08-14 21:02:36 +08002414 snprintf(msg.recovery, sizeof(msg.recovery), "recovery\n--wipe_data");
2415 write_misc(0, &msg, sizeof(msg));
2416
2417 fastboot_okay("");
2418 reboot_device(RECOVERY_MODE);
lijuang21f12f52015-08-22 16:22:19 +08002419}
2420
lijuang511a2b52015-08-14 20:50:51 +08002421static bool critical_flash_allowed(const char * entry)
2422{
2423 uint32_t i = 0;
2424 if (entry == NULL)
2425 return false;
2426
2427 for (i = 0; i < ARRAY_SIZE(critical_flash_allowed_ptn); i++) {
2428 if(!strcmp(entry, critical_flash_allowed_ptn[i]))
2429 return true;
2430 }
2431 return false;
Matthew Qin271927e2015-03-31 22:07:07 -04002432}
2433
2434#if DEVICE_TREE
Amol Jadicb524072012-08-09 16:40:18 -07002435int copy_dtb(uint8_t *boot_image_start, unsigned int scratch_offset)
2436{
2437 uint32 dt_image_offset = 0;
Amol Jadicb524072012-08-09 16:40:18 -07002438 uint32_t n;
Amol Jadicb524072012-08-09 16:40:18 -07002439 struct dt_table *table;
2440 struct dt_entry dt_entry;
2441 uint32_t dt_hdr_size;
2442 unsigned int compressed_size = 0;
2443 unsigned int dtb_size = 0;
2444 unsigned int out_avai_len = 0;
2445 unsigned char *out_addr = NULL;
2446 unsigned char *best_match_dt_addr = NULL;
2447 int rc;
2448
2449 struct boot_img_hdr *hdr = (struct boot_img_hdr *) (boot_image_start);
2450
Parth Dixit4097b622016-03-15 14:42:27 +05302451#ifndef OSVERSION_IN_BOOTIMAGE
2452 dt_size = hdr->dt_size;
2453#endif
2454
2455 if(dt_size != 0) {
Amol Jadicb524072012-08-09 16:40:18 -07002456 /* add kernel offset */
2457 dt_image_offset += page_size;
2458 n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
2459 dt_image_offset += n;
2460
2461 /* add ramdisk offset */
2462 n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
2463 dt_image_offset += n;
2464
2465 /* add second offset */
2466 if(hdr->second_size != 0) {
2467 n = ROUND_TO_PAGE(hdr->second_size, page_mask);
2468 dt_image_offset += n;
2469 }
2470
2471 /* offset now point to start of dt.img */
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07002472 table = (struct dt_table*)(boot_image_start + dt_image_offset);
Amol Jadicb524072012-08-09 16:40:18 -07002473
Deepa Dinamani19648b42013-09-05 17:05:55 -07002474 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Amol Jadicb524072012-08-09 16:40:18 -07002475 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
2476 return -1;
2477 }
P.V. Phani Kumar82916762016-03-09 09:20:19 +05302478
2479 /* Its Error if, dt_hdr_size (table->num_entries * dt_entry size + Dev_Tree Header)
2480 goes beyound hdr->dt_size*/
Parth Dixit4097b622016-03-15 14:42:27 +05302481 if (dt_hdr_size > ROUND_TO_PAGE(dt_size,hdr->page_size)) {
P.V. Phani Kumar82916762016-03-09 09:20:19 +05302482 dprintf(CRITICAL, "ERROR: Invalid Device Tree size \n");
2483 return -1;
2484 }
2485
Joel Kingaa335dc2013-06-03 16:11:08 -07002486 /* Find index of device tree within device tree table */
2487 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Amol Jadicb524072012-08-09 16:40:18 -07002488 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
2489 return -1;
2490 }
2491
Matthew Qin271927e2015-03-31 22:07:07 -04002492 best_match_dt_addr = (unsigned char *)boot_image_start + dt_image_offset + dt_entry.offset;
2493 if (is_gzip_package(best_match_dt_addr, dt_entry.size))
2494 {
2495 out_addr = (unsigned char *)target_get_scratch_address() + scratch_offset;
2496 out_avai_len = target_get_max_flash_size() - scratch_offset;
Matthew Qin0b15b322015-05-19 05:20:54 -04002497 dprintf(INFO, "decompressing dtb: start\n");
Matthew Qin271927e2015-03-31 22:07:07 -04002498 rc = decompress(best_match_dt_addr,
2499 dt_entry.size, out_addr, out_avai_len,
2500 &compressed_size, &dtb_size);
2501 if (rc)
2502 {
Matthew Qin0b15b322015-05-19 05:20:54 -04002503 dprintf(CRITICAL, "decompressing dtb failed!!!\n");
Matthew Qin271927e2015-03-31 22:07:07 -04002504 ASSERT(0);
2505 }
2506
Matthew Qin0b15b322015-05-19 05:20:54 -04002507 dprintf(INFO, "decompressing dtb: done\n");
Matthew Qin271927e2015-03-31 22:07:07 -04002508 best_match_dt_addr = out_addr;
2509 } else {
2510 dtb_size = dt_entry.size;
2511 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002512 /* Validate and Read device device tree in the "tags_add */
Mayank Grovere559cec2017-10-17 15:12:03 +05302513 if (check_aboot_addr_range_overlap(hdr->tags_addr, dtb_size) ||
2514 check_ddr_addr_range_bound(hdr->tags_addr, dtb_size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002515 {
Mayank Grovere559cec2017-10-17 15:12:03 +05302516 dprintf(CRITICAL, "Device tree addresses are not valid.\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002517 return -1;
2518 }
2519
Amol Jadicb524072012-08-09 16:40:18 -07002520 /* Read device device tree in the "tags_add */
Matthew Qin271927e2015-03-31 22:07:07 -04002521 memmove((void*) hdr->tags_addr, (void *)best_match_dt_addr, dtb_size);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002522 } else
2523 return -1;
Amol Jadicb524072012-08-09 16:40:18 -07002524
2525 /* Everything looks fine. Return success. */
2526 return 0;
2527}
2528#endif
2529
Brian Swetland9c4c0752009-01-25 16:23:50 -08002530void cmd_boot(const char *arg, void *data, unsigned sz)
2531{
Amit Blay8a510302015-08-17 09:20:01 +03002532#ifdef MDTP_SUPPORT
2533 static bool is_mdtp_activated = 0;
2534#endif /* MDTP_SUPPORT */
Brian Swetland9c4c0752009-01-25 16:23:50 -08002535 unsigned kernel_actual;
2536 unsigned ramdisk_actual;
Kishor PKd8ddcad2017-07-27 13:53:57 +05302537 unsigned second_actual;
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002538 uint32_t image_actual;
2539 uint32_t dt_actual = 0;
P.V. Phani Kumareecfd822016-03-09 20:09:03 +05302540 uint32_t sig_actual = 0;
Kishor PK5134b332017-05-09 17:50:08 +05302541 uint32_t sig_size = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08002542 struct boot_img_hdr *hdr = NULL;
2543 struct kernel64_hdr *kptr = NULL;
Brian Swetland9c4c0752009-01-25 16:23:50 -08002544 char *ptr = ((char*) data);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002545 int ret = 0;
2546 uint8_t dtb_copied = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08002547 unsigned int out_len = 0;
2548 unsigned int out_avai_len = 0;
2549 unsigned char *out_addr = NULL;
2550 uint32_t dtb_offset = 0;
2551 unsigned char *kernel_start_addr = NULL;
2552 unsigned int kernel_size = 0;
Matthew Qin271927e2015-03-31 22:07:07 -04002553 unsigned int scratch_offset = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08002554
lijuang2008ff22016-03-07 17:56:27 +08002555#if FBCON_DISPLAY_MSG
2556 /* Exit keys' detection thread firstly */
2557 exit_menu_keys_detection();
2558#endif
2559
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002560#if VERIFIED_BOOT
Channagoud Kadabi6d5375e2015-06-23 17:15:42 -07002561 if(target_build_variant_user() && !device.is_unlocked)
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002562 {
2563 fastboot_fail("unlock device to use this command");
lijuang2008ff22016-03-07 17:56:27 +08002564 goto boot_failed;
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002565 }
2566#endif
2567
Brian Swetland9c4c0752009-01-25 16:23:50 -08002568 if (sz < sizeof(hdr)) {
2569 fastboot_fail("invalid bootimage header");
lijuang2008ff22016-03-07 17:56:27 +08002570 goto boot_failed;
Brian Swetland9c4c0752009-01-25 16:23:50 -08002571 }
2572
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002573 hdr = (struct boot_img_hdr *)data;
Brian Swetland9c4c0752009-01-25 16:23:50 -08002574
2575 /* ensure commandline is terminated */
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002576 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -08002577
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002578 if(target_is_emmc_boot() && hdr->page_size) {
2579 page_size = hdr->page_size;
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07002580 page_mask = page_size - 1;
2581 }
2582
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002583 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
2584 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Kishor PKd8ddcad2017-07-27 13:53:57 +05302585 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002586#if DEVICE_TREE
Parth Dixit4097b622016-03-15 14:42:27 +05302587#ifndef OSVERSION_IN_BOOTIMAGE
2588 dt_size = hdr->dt_size;
2589#endif
2590 dt_actual = ROUND_TO_PAGE(dt_size, page_mask);
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002591#endif
2592
2593 image_actual = ADD_OF(page_size, kernel_actual);
2594 image_actual = ADD_OF(image_actual, ramdisk_actual);
Kishor PKd8ddcad2017-07-27 13:53:57 +05302595 image_actual = ADD_OF(image_actual, second_actual);
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002596 image_actual = ADD_OF(image_actual, dt_actual);
2597
Kishor PK5134b332017-05-09 17:50:08 +05302598 /* Checking to prevent oob access in read_der_message_length */
2599 if (image_actual > sz) {
2600 fastboot_fail("bootimage header fields are invalid");
2601 goto boot_failed;
2602 }
2603 sig_size = sz - image_actual;
2604
P.V. Phani Kumareecfd822016-03-09 20:09:03 +05302605 if (target_use_signed_kernel() && (!device.is_unlocked)) {
2606 /* Calculate the signature length from boot image */
2607 sig_actual = read_der_message_length(
Kishor PK5134b332017-05-09 17:50:08 +05302608 (unsigned char*)(data + image_actual), sig_size);
Channagoud Kadabi3d839012014-06-26 14:26:39 -07002609 image_actual = ADD_OF(image_actual, sig_actual);
2610
Kishor PK5134b332017-05-09 17:50:08 +05302611 if (image_actual > sz) {
2612 fastboot_fail("bootimage header fields are invalid");
2613 goto boot_failed;
2614 }
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002615 }
2616
Sridhar Parasuram361e4cd2015-09-24 11:19:19 -07002617 // Initialize boot state before trying to verify boot.img
2618#if VERIFIED_BOOT
Channagoud Kadabi699466e2015-11-03 12:37:42 -08002619 boot_verifier_init();
Mayank Groverb337e932017-01-18 20:00:40 +05302620#endif
Channagoud Kadabibf6ce7d2015-09-17 13:25:35 -07002621 /* Handle overflow if the input image size is greater than
2622 * boot image buffer can hold
2623 */
2624 if ((target_get_max_flash_size() - (image_actual - sig_actual)) < page_size)
2625 {
2626 fastboot_fail("booimage: size is greater than boot image buffer can hold");
lijuang2008ff22016-03-07 17:56:27 +08002627 goto boot_failed;
Channagoud Kadabibf6ce7d2015-09-17 13:25:35 -07002628 }
Sridhar Parasuram361e4cd2015-09-24 11:19:19 -07002629
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002630 /* Verify the boot image
2631 * device & page_size are initialized in aboot_init
2632 */
Sridhar Parasuram7ae9fbc2015-09-17 09:56:30 -07002633 if (target_use_signed_kernel() && (!device.is_unlocked)) {
Channagoud Kadabi3d839012014-06-26 14:26:39 -07002634 /* Pass size excluding signature size, otherwise we would try to
2635 * access signature beyond its length
2636 */
2637 verify_signed_bootimg((uint32_t)data, (image_actual - sig_actual));
Sridhar Parasuram7ae9fbc2015-09-17 09:56:30 -07002638 }
Amit Blay4aa292f2015-04-28 21:55:59 +03002639#ifdef MDTP_SUPPORT
2640 else
2641 {
Shay Nachmani062aa3f2015-05-17 17:28:44 +03002642 /* fastboot boot is not allowed when MDTP is activated */
Amit Blay4aa292f2015-04-28 21:55:59 +03002643 mdtp_ext_partition_verification_t ext_partition;
Amit Blay8a510302015-08-17 09:20:01 +03002644
2645 if (!is_mdtp_activated) {
2646 ext_partition.partition = MDTP_PARTITION_NONE;
2647 mdtp_fwlock_verify_lock(&ext_partition);
2648 }
Amit Blay4aa292f2015-04-28 21:55:59 +03002649 }
Shay Nachmani062aa3f2015-05-17 17:28:44 +03002650
Amir Kotzer7c768c02016-04-13 09:08:05 +03002651 /* If mdtp state cannot be validate, block fastboot boot*/
2652 if(mdtp_activated(&is_mdtp_activated)){
2653 dprintf(CRITICAL, "mdtp_activated cannot validate state.\n");
2654 dprintf(CRITICAL, "Can not proceed with fastboot boot command.\n");
2655 goto boot_failed;
2656 }
Shay Nachmani062aa3f2015-05-17 17:28:44 +03002657 if(is_mdtp_activated){
2658 dprintf(CRITICAL, "fastboot boot command is not available.\n");
lijuang2008ff22016-03-07 17:56:27 +08002659 goto boot_failed;
Shay Nachmani062aa3f2015-05-17 17:28:44 +03002660 }
Amit Blay4aa292f2015-04-28 21:55:59 +03002661#endif /* MDTP_SUPPORT */
2662
Sridhar Parasuram361e4cd2015-09-24 11:19:19 -07002663#if VERIFIED_BOOT
Mayank Grover912eaa62017-10-26 12:08:53 +05302664 if (VB_M <= target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +05302665 {
2666 /* set boot and system versions. */
2667 set_os_version((unsigned char *)data);
2668 // send root of trust
2669 if(!send_rot_command((uint32_t)device.is_unlocked))
2670 ASSERT(0);
2671 }
Parth Dixitbc9b6492015-10-18 00:41:38 +05302672#endif
Channagoud Kadabia22144f2013-03-20 11:49:01 -07002673 /*
Matthew Qinbb7923d2015-02-09 10:56:09 +08002674 * Check if the kernel image is a gzip package. If yes, need to decompress it.
2675 * If not, continue booting.
2676 */
2677 if (is_gzip_package((unsigned char *)(data + page_size), hdr->kernel_size))
2678 {
2679 out_addr = (unsigned char *)target_get_scratch_address();
2680 out_addr = (unsigned char *)(out_addr + image_actual + page_size);
2681 out_avai_len = target_get_max_flash_size() - image_actual - page_size;
Matthew Qin0b15b322015-05-19 05:20:54 -04002682 dprintf(INFO, "decompressing kernel image: start\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08002683 ret = decompress((unsigned char *)(ptr + page_size),
2684 hdr->kernel_size, out_addr, out_avai_len,
2685 &dtb_offset, &out_len);
2686 if (ret)
2687 {
Matthew Qin0b15b322015-05-19 05:20:54 -04002688 dprintf(CRITICAL, "decompressing image failed!!!\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08002689 ASSERT(0);
2690 }
2691
Matthew Qin0b15b322015-05-19 05:20:54 -04002692 dprintf(INFO, "decompressing kernel image: done\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08002693 kptr = (struct kernel64_hdr *)out_addr;
2694 kernel_start_addr = out_addr;
2695 kernel_size = out_len;
2696 } else {
2697 kptr = (struct kernel64_hdr*)((char *)data + page_size);
2698 kernel_start_addr = (unsigned char *)((char *)data + page_size);
2699 kernel_size = hdr->kernel_size;
2700 }
2701
2702 /*
Channagoud Kadabia22144f2013-03-20 11:49:01 -07002703 * Update the kernel/ramdisk/tags address if the boot image header
2704 * has default values, these default values come from mkbootimg when
2705 * the boot image is flashed using fastboot flash:raw
2706 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08002707 update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr));
Dima Zavin3cadfff2013-03-21 14:30:48 -07002708
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002709 /* Get virtual addresses since the hdr saves physical addresses. */
2710 hdr->kernel_addr = VA(hdr->kernel_addr);
2711 hdr->ramdisk_addr = VA(hdr->ramdisk_addr);
2712 hdr->tags_addr = VA(hdr->tags_addr);
Brian Swetland9c4c0752009-01-25 16:23:50 -08002713
Matthew Qinbb7923d2015-02-09 10:56:09 +08002714 kernel_size = ROUND_TO_PAGE(kernel_size, page_mask);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002715 /* Check if the addresses in the header are valid. */
Matthew Qinbb7923d2015-02-09 10:56:09 +08002716 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_size) ||
Mayank Grovere559cec2017-10-17 15:12:03 +05302717 check_ddr_addr_range_bound(hdr->kernel_addr, kernel_size) ||
2718 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual) ||
2719 check_ddr_addr_range_bound(hdr->ramdisk_addr, ramdisk_actual))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002720 {
Mayank Grovere559cec2017-10-17 15:12:03 +05302721 dprintf(CRITICAL, "kernel/ramdisk addresses are not valid.\n");
lijuang2008ff22016-03-07 17:56:27 +08002722 goto boot_failed;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002723 }
2724
Amol Jadicb524072012-08-09 16:40:18 -07002725#if DEVICE_TREE
Matthew Qin271927e2015-03-31 22:07:07 -04002726 scratch_offset = image_actual + page_size + out_len;
Amol Jadicb524072012-08-09 16:40:18 -07002727 /* find correct dtb and copy it to right location */
Matthew Qin271927e2015-03-31 22:07:07 -04002728 ret = copy_dtb(data, scratch_offset);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002729
2730 dtb_copied = !ret ? 1 : 0;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002731#else
Mayank Grovere559cec2017-10-17 15:12:03 +05302732 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE) ||
2733 check_ddr_addr_range_bound(hdr->tags_addr, MAX_TAGS_SIZE))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002734 {
Mayank Grovere559cec2017-10-17 15:12:03 +05302735 dprintf(CRITICAL, "Tags addresses are not valid.\n");
lijuang2008ff22016-03-07 17:56:27 +08002736 goto boot_failed;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002737 }
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002738#endif
2739
2740 /* Load ramdisk & kernel */
2741 memmove((void*) hdr->ramdisk_addr, ptr + page_size + kernel_actual, hdr->ramdisk_size);
Matthew Qinbb7923d2015-02-09 10:56:09 +08002742 memmove((void*) hdr->kernel_addr, (char*) (kernel_start_addr), kernel_size);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002743
2744#if DEVICE_TREE
Mayank Grovere559cec2017-10-17 15:12:03 +05302745 if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual) ||
2746 check_ddr_addr_range_bound(hdr->tags_addr, kernel_actual))
Matthew Qinbb7923d2015-02-09 10:56:09 +08002747 {
Mayank Grovere559cec2017-10-17 15:12:03 +05302748 dprintf(CRITICAL, "Tags addresses are not valid.\n");
lijuang2008ff22016-03-07 17:56:27 +08002749 goto boot_failed;
Matthew Qinbb7923d2015-02-09 10:56:09 +08002750 }
2751
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002752 /*
2753 * If dtb is not found look for appended DTB in the kernel.
2754 * If appended dev tree is found, update the atags with
2755 * memory address to the DTB appended location on RAM.
2756 * Else update with the atags address in the kernel header
2757 */
2758 if (!dtb_copied) {
2759 void *dtb;
Matthew Qinbb7923d2015-02-09 10:56:09 +08002760 dtb = dev_tree_appended((void*)(ptr + page_size),
2761 hdr->kernel_size, dtb_offset,
Dima Zavine63e5572013-05-03 12:23:06 -07002762 (void *)hdr->tags_addr);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002763 if (!dtb) {
2764 fastboot_fail("dtb not found");
lijuang2008ff22016-03-07 17:56:27 +08002765 goto boot_failed;
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002766 }
Amol Jadicb524072012-08-09 16:40:18 -07002767 }
2768#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08002769
2770 fastboot_okay("");
Amol Jadi7c4316c2013-10-07 14:19:26 -07002771 fastboot_stop();
Brian Swetland9c4c0752009-01-25 16:23:50 -08002772
Dima Zavin77e41f32013-03-06 16:10:43 -08002773 boot_linux((void*) hdr->kernel_addr, (void*) hdr->tags_addr,
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002774 (const char*) hdr->cmdline, board_machtype(),
2775 (void*) hdr->ramdisk_addr, hdr->ramdisk_size);
lijuang2008ff22016-03-07 17:56:27 +08002776
2777 /* fastboot already stop, it's no need to show fastboot menu */
2778 return;
2779boot_failed:
2780#if FBCON_DISPLAY_MSG
2781 /* revert to fastboot menu if boot failed */
2782 display_fastboot_menu();
2783#endif
2784 return;
Brian Swetland9c4c0752009-01-25 16:23:50 -08002785}
2786
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002787void cmd_erase_nand(const char *arg, void *data, unsigned sz)
Dima Zavin214cc642009-01-26 11:16:21 -08002788{
2789 struct ptentry *ptn;
2790 struct ptable *ptable;
2791
2792 ptable = flash_get_ptable();
2793 if (ptable == NULL) {
2794 fastboot_fail("partition table doesn't exist");
2795 return;
2796 }
2797
2798 ptn = ptable_find(ptable, arg);
2799 if (ptn == NULL) {
2800 fastboot_fail("unknown partition name");
2801 return;
2802 }
2803
2804 if (flash_erase(ptn)) {
2805 fastboot_fail("failed to erase partition");
2806 return;
2807 }
2808 fastboot_okay("");
2809}
2810
Bikas Gurungd48bd242010-09-04 19:54:32 -07002811
2812void cmd_erase_mmc(const char *arg, void *data, unsigned sz)
2813{
2814 unsigned long long ptn = 0;
Oliver Wangcee448d2013-10-22 18:40:13 +08002815 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07002816 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002817 uint8_t lun = 0;
Vijay Kumar Pendoti0b21f462016-05-02 17:09:18 +05302818 char *footer = NULL;
Bikas Gurungd48bd242010-09-04 19:54:32 -07002819
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002820#if VERIFIED_BOOT
2821 if(!strcmp(arg, KEYSTORE_PTN_NAME))
2822 {
2823 if(!device.is_unlocked)
2824 {
2825 fastboot_fail("unlock device to erase keystore");
2826 return;
2827 }
2828 }
2829#endif
2830
Kinson Chikf1a43512011-07-14 11:28:39 -07002831 index = partition_get_index(arg);
2832 ptn = partition_get_offset(index);
Oliver Wangcee448d2013-10-22 18:40:13 +08002833 size = partition_get_size(index);
Neeti Desaica8c9602011-10-06 11:40:00 -07002834
Kinson Chikf1a43512011-07-14 11:28:39 -07002835 if(ptn == 0) {
Neeti Desaica8c9602011-10-06 11:40:00 -07002836 fastboot_fail("Partition table doesn't exist\n");
Bikas Gurungd48bd242010-09-04 19:54:32 -07002837 return;
2838 }
Kun Liang2f1601a2013-08-12 16:29:54 +08002839
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002840 lun = partition_get_lun(index);
2841 mmc_set_lun(lun);
2842
Sridhar Parasuramd7957122015-02-27 11:33:40 -08002843 if (platform_boot_dev_isemmc())
2844 {
2845 if (mmc_erase_card(ptn, size)) {
2846 fastboot_fail("failed to erase partition\n");
2847 return;
2848 }
2849 } else {
2850 BUF_DMA_ALIGN(out, DEFAULT_ERASE_SIZE);
2851 size = partition_get_size(index);
2852 if (size > DEFAULT_ERASE_SIZE)
2853 size = DEFAULT_ERASE_SIZE;
Kun Liang2f1601a2013-08-12 16:29:54 +08002854
Sridhar Parasuramd7957122015-02-27 11:33:40 -08002855 /* Simple inefficient version of erase. Just writing
2856 0 in first several blocks */
2857 if (mmc_write(ptn , size, (unsigned int *)out)) {
2858 fastboot_fail("failed to erase partition");
2859 return;
2860 }
Vijay Kumar Pendoti0b21f462016-05-02 17:09:18 +05302861 /*Erase FDE metadata at the userdata footer*/
2862 if(!(strncmp(arg, "userdata", 8)))
2863 {
2864 footer = memalign(CACHE_LINE, FOOTER_SIZE);
2865 memset((void *)footer, 0, FOOTER_SIZE);
2866
2867 size = partition_get_size(index);
2868
2869 if (mmc_write((ptn + size) - FOOTER_SIZE , FOOTER_SIZE, (unsigned int *)footer)) {
2870 fastboot_fail("failed to erase userdata footer");
2871 free(footer);
2872 return;
2873 }
2874 free(footer);
2875 }
Bikas Gurungd48bd242010-09-04 19:54:32 -07002876 }
Sridhar Parasuram32b30662015-07-10 13:33:22 -07002877#if VERIFIED_BOOT
Mayank Grover912eaa62017-10-26 12:08:53 +05302878 if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05302879 !(strncmp(arg, "userdata", 8)) &&
2880 send_delete_keys_to_tz())
Sridhar Parasuram32b30662015-07-10 13:33:22 -07002881 ASSERT(0);
2882#endif
Bikas Gurungd48bd242010-09-04 19:54:32 -07002883 fastboot_okay("");
2884}
2885
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002886void cmd_erase(const char *arg, void *data, unsigned sz)
2887{
Channagoud Kadabice3cf422015-04-17 16:02:30 -07002888#if VERIFIED_BOOT
Channagoud Kadabi35297672015-06-13 11:09:49 -07002889 if (target_build_variant_user())
Channagoud Kadabice3cf422015-04-17 16:02:30 -07002890 {
Sridhar Parasuramc32d07d2015-07-12 10:57:48 -07002891 if(!device.is_unlocked)
Channagoud Kadabice3cf422015-04-17 16:02:30 -07002892 {
Channagoud Kadabi35297672015-06-13 11:09:49 -07002893 fastboot_fail("device is locked. Cannot erase");
Channagoud Kadabice3cf422015-04-17 16:02:30 -07002894 return;
2895 }
2896 }
2897#endif
2898
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002899 if(target_is_emmc_boot())
2900 cmd_erase_mmc(arg, data, sz);
2901 else
2902 cmd_erase_nand(arg, data, sz);
2903}
Bikas Gurungd48bd242010-09-04 19:54:32 -07002904
Ajay Dudani5c761132011-04-07 20:19:04 -07002905void cmd_flash_mmc_img(const char *arg, void *data, unsigned sz)
Shashank Mittal23b8f422010-04-16 19:27:21 -07002906{
2907 unsigned long long ptn = 0;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07002908 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07002909 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002910 char *token = NULL;
2911 char *pname = NULL;
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07002912 char *sp;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002913 uint8_t lun = 0;
2914 bool lun_set = false;
Mayank Grover351a75e2017-05-30 20:06:08 +05302915 int current_active_slot = INVALID;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07002916
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07002917 token = strtok_r((char *)arg, ":", &sp);
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002918 pname = token;
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07002919 token = strtok_r(NULL, ":", &sp);
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002920 if(token)
2921 {
2922 lun = atoi(token);
2923 mmc_set_lun(lun);
2924 lun_set = true;
2925 }
2926
Mao Jinlong226f33a2014-07-04 17:24:10 +08002927 if (pname)
Greg Grisco6e754772011-06-23 12:19:39 -07002928 {
Channagoud Kadabiad259832015-05-29 11:14:17 -07002929 if (!strncmp(pname, "frp-unlock", strlen("frp-unlock")))
2930 {
2931 if (!aboot_frp_unlock(pname, data, sz))
2932 {
2933 fastboot_info("FRP unlock successful");
2934 fastboot_okay("");
2935 }
2936 else
2937 fastboot_fail("Secret key is invalid, please update the bootloader with secret key");
2938
2939 return;
2940 }
2941
Mao Jinlong226f33a2014-07-04 17:24:10 +08002942 if (!strcmp(pname, "partition"))
2943 {
2944 dprintf(INFO, "Attempt to write partition image.\n");
2945 if (write_partition(sz, (unsigned char *) data)) {
2946 fastboot_fail("failed to write partition");
Greg Grisco6e754772011-06-23 12:19:39 -07002947 return;
2948 }
Mayank Grover351a75e2017-05-30 20:06:08 +05302949
2950 /* Rescan partition table to ensure we have multislot support*/
2951 if (partition_scan_for_multislot())
2952 {
2953 current_active_slot = partition_find_active_slot();
2954 dprintf(INFO, "Multislot supported: Slot %s active",
2955 (SUFFIX_SLOT(current_active_slot)));
2956 }
2957 partition_mark_active_slot(current_active_slot);
Greg Grisco6e754772011-06-23 12:19:39 -07002958 }
Mao Jinlong226f33a2014-07-04 17:24:10 +08002959 else
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002960 {
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002961#if VERIFIED_BOOT
2962 if(!strcmp(pname, KEYSTORE_PTN_NAME))
2963 {
2964 if(!device.is_unlocked)
2965 {
2966 fastboot_fail("unlock device to flash keystore");
2967 return;
2968 }
Gaurav Nebhwanic7313cc2015-12-15 22:25:04 +05302969 if(!boot_verify_validate_keystore((unsigned char *)data,sz))
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002970 {
2971 fastboot_fail("image is not a keystore file");
2972 return;
2973 }
2974 }
2975#endif
Mao Jinlong226f33a2014-07-04 17:24:10 +08002976 index = partition_get_index(pname);
2977 ptn = partition_get_offset(index);
2978 if(ptn == 0) {
2979 fastboot_fail("partition table doesn't exist");
2980 return;
2981 }
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002982
Mayank Grover351a75e2017-05-30 20:06:08 +05302983 if (!strncmp(pname, "boot", strlen("boot"))
2984 || !strcmp(pname, "recovery"))
2985 {
Mao Jinlong226f33a2014-07-04 17:24:10 +08002986 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
2987 fastboot_fail("image is not a boot image");
2988 return;
2989 }
Mayank Grover351a75e2017-05-30 20:06:08 +05302990
2991 /* Reset multislot_partition attributes in case of flashing boot */
2992 if (partition_multislot_is_supported())
2993 {
2994 partition_reset_attributes(index);
2995 }
Mao Jinlong226f33a2014-07-04 17:24:10 +08002996 }
2997
2998 if(!lun_set)
2999 {
3000 lun = partition_get_lun(index);
3001 mmc_set_lun(lun);
3002 }
3003
3004 size = partition_get_size(index);
Gaurav Nebhwani4d2389c2016-03-17 21:10:05 +05303005 if (ROUND_TO_PAGE(sz, mmc_blocksize_mask) > size) {
Mao Jinlong226f33a2014-07-04 17:24:10 +08003006 fastboot_fail("size too large");
3007 return;
3008 }
3009 else if (mmc_write(ptn , sz, (unsigned int *)data)) {
3010 fastboot_fail("flash write failure");
3011 return;
3012 }
Greg Grisco6e754772011-06-23 12:19:39 -07003013 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07003014 }
3015 fastboot_okay("");
3016 return;
3017}
3018
Ajay Dudanide984792015-03-02 09:57:41 -08003019void cmd_flash_meta_img(const char *arg, void *data, unsigned sz)
3020{
3021 int i, images;
3022 meta_header_t *meta_header;
3023 img_header_entry_t *img_header_entry;
Parth Dixit3e2d3032016-03-04 17:11:52 +05303024 /*End of the image address*/
3025 uintptr_t data_end;
3026
3027 if( (UINT_MAX - sz) > (uintptr_t)data )
3028 data_end = (uintptr_t)data + sz;
3029 else
3030 {
3031 fastboot_fail("Cannot flash: image header corrupt");
3032 return;
3033 }
3034
3035 if( data_end < ((uintptr_t)data + sizeof(meta_header_t)))
3036 {
3037 fastboot_fail("Cannot flash: image header corrupt");
3038 return;
3039 }
Ajay Dudanide984792015-03-02 09:57:41 -08003040
lijuang8ee21882016-04-19 16:57:11 +08003041 /* If device is locked:
3042 * Forbid to flash image to avoid the device to bypass the image
3043 * which with "any" name other than bootloader. Because it maybe
3044 * a meta package of all partitions.
3045 */
3046#if VERIFIED_BOOT
3047 if (target_build_variant_user()) {
3048 if (!device.is_unlocked) {
3049 fastboot_fail("Device is locked, meta image flashing is not allowed");
3050 return;
3051 }
Mayank Grover889be1b2017-09-12 20:12:23 +05303052
Mayank Grover912eaa62017-10-26 12:08:53 +05303053 if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05303054 !device.is_unlock_critical)
3055 {
lijuang8ee21882016-04-19 16:57:11 +08003056 fastboot_fail("Device is critical locked, Meta image flashing is not allowed");
3057 return;
3058 }
lijuang8ee21882016-04-19 16:57:11 +08003059 }
3060#endif
3061
Ajay Dudanide984792015-03-02 09:57:41 -08003062 meta_header = (meta_header_t*) data;
Parth Dixit3e2d3032016-03-04 17:11:52 +05303063 if( data_end < ((uintptr_t)data + meta_header->img_hdr_sz))
3064 {
3065 fastboot_fail("Cannot flash: image header corrupt");
3066 return;
3067 }
Ajay Dudanide984792015-03-02 09:57:41 -08003068 img_header_entry = (img_header_entry_t*) (data+sizeof(meta_header_t));
3069
3070 images = meta_header->img_hdr_sz / sizeof(img_header_entry_t);
3071
3072 for (i=0; i<images; i++) {
3073
3074 if((img_header_entry[i].ptn_name == NULL) ||
3075 (img_header_entry[i].start_offset == 0) ||
3076 (img_header_entry[i].size == 0))
3077 break;
Kishor PK49831502017-04-21 17:55:43 +05303078 if ((UINT_MAX - img_header_entry[i].start_offset) < (uintptr_t)data) {
3079 fastboot_fail("Integer overflow detected in start_offset of img");
3080 break;
3081 }
3082 else if ((UINT_MAX - (img_header_entry[i].start_offset + (uintptr_t)data)) < img_header_entry[i].size) {
3083 fastboot_fail("Integer overflow detected in size of img");
3084 break;
3085 }
Parth Dixit3e2d3032016-03-04 17:11:52 +05303086 if( data_end < ((uintptr_t)data + img_header_entry[i].start_offset
3087 + img_header_entry[i].size) )
3088 {
3089 fastboot_fail("Cannot flash: image size mismatch");
3090 break;
3091 }
3092
Ajay Dudanide984792015-03-02 09:57:41 -08003093 cmd_flash_mmc_img(img_header_entry[i].ptn_name,
3094 (void *) data + img_header_entry[i].start_offset,
3095 img_header_entry[i].size);
3096 }
3097
Channagoud Kadabiaafbbfc2015-03-04 17:52:56 -08003098 if (!strncmp(arg, "bootloader", strlen("bootloader")))
3099 {
3100 strlcpy(device.bootloader_version, TARGET(BOARD), MAX_VERSION_LEN);
3101 strlcat(device.bootloader_version, "-", MAX_VERSION_LEN);
3102 strlcat(device.bootloader_version, meta_header->img_version, MAX_VERSION_LEN);
3103 }
3104 else
3105 {
3106 strlcpy(device.radio_version, TARGET(BOARD), MAX_VERSION_LEN);
3107 strlcat(device.radio_version, "-", MAX_VERSION_LEN);
3108 strlcat(device.radio_version, meta_header->img_version, MAX_VERSION_LEN);
3109 }
3110
3111 write_device_info(&device);
Ajay Dudanide984792015-03-02 09:57:41 -08003112 fastboot_okay("");
3113 return;
3114}
3115
Ajay Dudani5c761132011-04-07 20:19:04 -07003116void cmd_flash_mmc_sparse_img(const char *arg, void *data, unsigned sz)
3117{
3118 unsigned int chunk;
wufeng.jiang813dc352015-06-02 23:02:46 -04003119 uint64_t chunk_data_sz;
Channagoud Kadabif627cf82013-09-09 14:08:20 -07003120 uint32_t *fill_buf = NULL;
3121 uint32_t fill_val;
Mayank Grover4f50bba2017-07-19 18:17:08 +05303122 uint32_t blk_sz_actual = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07003123 sparse_header_t *sparse_header;
3124 chunk_header_t *chunk_header;
Ajay Dudaniab18f022011-05-12 14:39:22 -07003125 uint32_t total_blocks = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07003126 unsigned long long ptn = 0;
Channagoud Kadabi65b91002011-10-11 17:34:33 +05303127 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07003128 int index = INVALID_PTN;
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05303129 uint32_t i;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003130 uint8_t lun = 0;
vijay kumar800255e2015-04-24 20:26:19 +05303131 /*End of the sparse image address*/
Parth Dixit64b1a482016-03-07 16:31:26 +05303132 uintptr_t data_end = (uintptr_t)data + sz;
Ajay Dudani5c761132011-04-07 20:19:04 -07003133
Kinson Chikf1a43512011-07-14 11:28:39 -07003134 index = partition_get_index(arg);
3135 ptn = partition_get_offset(index);
3136 if(ptn == 0) {
Ajay Dudani5c761132011-04-07 20:19:04 -07003137 fastboot_fail("partition table doesn't exist");
3138 return;
3139 }
3140
Channagoud Kadabi65b91002011-10-11 17:34:33 +05303141 size = partition_get_size(index);
Channagoud Kadabi65b91002011-10-11 17:34:33 +05303142
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003143 lun = partition_get_lun(index);
3144 mmc_set_lun(lun);
3145
vijay kumar800255e2015-04-24 20:26:19 +05303146 if (sz < sizeof(sparse_header_t)) {
3147 fastboot_fail("size too low");
3148 return;
3149 }
3150
Ajay Dudani5c761132011-04-07 20:19:04 -07003151 /* Read and skip over sparse image header */
3152 sparse_header = (sparse_header_t *) data;
vijay kumar800255e2015-04-24 20:26:19 +05303153
Mayank Grover48bd9bb2017-07-19 12:04:16 +05303154 if (!sparse_header->blk_sz || (sparse_header->blk_sz % 4)){
3155 fastboot_fail("Invalid block size\n");
3156 return;
3157 }
3158
vijay kumar1321f342015-03-27 12:13:42 +05303159 if (((uint64_t)sparse_header->total_blks * (uint64_t)sparse_header->blk_sz) > size) {
Ajay Dudani876b3282012-12-21 14:12:17 -08003160 fastboot_fail("size too large");
3161 return;
3162 }
3163
vijay kumarde4fcf62015-04-23 13:05:49 +05303164 data += sizeof(sparse_header_t);
vijay kumar800255e2015-04-24 20:26:19 +05303165
Parth Dixit64b1a482016-03-07 16:31:26 +05303166 if (data_end < (uintptr_t)data) {
vijay kumar800255e2015-04-24 20:26:19 +05303167 fastboot_fail("buffer overreads occured due to invalid sparse header");
3168 return;
3169 }
3170
vijay kumarde4fcf62015-04-23 13:05:49 +05303171 if(sparse_header->file_hdr_sz != sizeof(sparse_header_t))
Ajay Dudani5c761132011-04-07 20:19:04 -07003172 {
vijay kumarde4fcf62015-04-23 13:05:49 +05303173 fastboot_fail("sparse header size mismatch");
3174 return;
Ajay Dudani5c761132011-04-07 20:19:04 -07003175 }
3176
Ajay Dudanib06c05f2011-05-12 14:46:10 -07003177 dprintf (SPEW, "=== Sparse Image Header ===\n");
3178 dprintf (SPEW, "magic: 0x%x\n", sparse_header->magic);
3179 dprintf (SPEW, "major_version: 0x%x\n", sparse_header->major_version);
3180 dprintf (SPEW, "minor_version: 0x%x\n", sparse_header->minor_version);
3181 dprintf (SPEW, "file_hdr_sz: %d\n", sparse_header->file_hdr_sz);
3182 dprintf (SPEW, "chunk_hdr_sz: %d\n", sparse_header->chunk_hdr_sz);
3183 dprintf (SPEW, "blk_sz: %d\n", sparse_header->blk_sz);
3184 dprintf (SPEW, "total_blks: %d\n", sparse_header->total_blks);
3185 dprintf (SPEW, "total_chunks: %d\n", sparse_header->total_chunks);
Ajay Dudani5c761132011-04-07 20:19:04 -07003186
3187 /* Start processing chunks */
3188 for (chunk=0; chunk<sparse_header->total_chunks; chunk++)
3189 {
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05303190 /* Make sure the total image size does not exceed the partition size */
3191 if(((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz) >= size) {
3192 fastboot_fail("size too large");
3193 return;
3194 }
Ajay Dudani5c761132011-04-07 20:19:04 -07003195 /* Read and skip over chunk header */
3196 chunk_header = (chunk_header_t *) data;
3197 data += sizeof(chunk_header_t);
3198
Parth Dixit64b1a482016-03-07 16:31:26 +05303199 if (data_end < (uintptr_t)data) {
vijay kumar800255e2015-04-24 20:26:19 +05303200 fastboot_fail("buffer overreads occured due to invalid sparse header");
3201 return;
3202 }
3203
Ajay Dudani5c761132011-04-07 20:19:04 -07003204 dprintf (SPEW, "=== Chunk Header ===\n");
3205 dprintf (SPEW, "chunk_type: 0x%x\n", chunk_header->chunk_type);
3206 dprintf (SPEW, "chunk_data_sz: 0x%x\n", chunk_header->chunk_sz);
3207 dprintf (SPEW, "total_size: 0x%x\n", chunk_header->total_sz);
3208
vijay kumar800255e2015-04-24 20:26:19 +05303209 if(sparse_header->chunk_hdr_sz != sizeof(chunk_header_t))
Ajay Dudani5c761132011-04-07 20:19:04 -07003210 {
vijay kumar800255e2015-04-24 20:26:19 +05303211 fastboot_fail("chunk header size mismatch");
3212 return;
Ajay Dudani5c761132011-04-07 20:19:04 -07003213 }
3214
wufeng.jiang813dc352015-06-02 23:02:46 -04003215 chunk_data_sz = (uint64_t)sparse_header->blk_sz * chunk_header->chunk_sz;
3216
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05303217 /* Make sure that the chunk size calculated from sparse image does not
3218 * exceed partition size
3219 */
3220 if ((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz + chunk_data_sz > size)
3221 {
3222 fastboot_fail("Chunk data size exceeds partition size");
3223 return;
3224 }
3225
Ajay Dudani5c761132011-04-07 20:19:04 -07003226 switch (chunk_header->chunk_type)
3227 {
3228 case CHUNK_TYPE_RAW:
wufeng.jiang813dc352015-06-02 23:02:46 -04003229 if((uint64_t)chunk_header->total_sz != ((uint64_t)sparse_header->chunk_hdr_sz +
Ajay Dudani5c761132011-04-07 20:19:04 -07003230 chunk_data_sz))
3231 {
3232 fastboot_fail("Bogus chunk size for chunk type Raw");
3233 return;
3234 }
3235
Parth Dixit64b1a482016-03-07 16:31:26 +05303236 if (data_end < (uintptr_t)data + chunk_data_sz) {
vijay kumar800255e2015-04-24 20:26:19 +05303237 fastboot_fail("buffer overreads occured due to invalid sparse header");
3238 return;
3239 }
3240
wufeng.jiang813dc352015-06-02 23:02:46 -04003241 /* chunk_header->total_sz is uint32,So chunk_data_sz is now less than 2^32
3242 otherwise it will return in the line above
3243 */
Ajay Dudaniab18f022011-05-12 14:39:22 -07003244 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
wufeng.jiang813dc352015-06-02 23:02:46 -04003245 (uint32_t)chunk_data_sz,
Ajay Dudaniab18f022011-05-12 14:39:22 -07003246 (unsigned int*)data))
Ajay Dudani5c761132011-04-07 20:19:04 -07003247 {
3248 fastboot_fail("flash write failure");
3249 return;
3250 }
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05303251 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
3252 fastboot_fail("Bogus size for RAW chunk type");
3253 return;
3254 }
Ajay Dudani5c761132011-04-07 20:19:04 -07003255 total_blocks += chunk_header->chunk_sz;
wufeng.jiang813dc352015-06-02 23:02:46 -04003256 data += (uint32_t)chunk_data_sz;
Ajay Dudani5c761132011-04-07 20:19:04 -07003257 break;
3258
Channagoud Kadabif627cf82013-09-09 14:08:20 -07003259 case CHUNK_TYPE_FILL:
3260 if(chunk_header->total_sz != (sparse_header->chunk_hdr_sz +
3261 sizeof(uint32_t)))
3262 {
3263 fastboot_fail("Bogus chunk size for chunk type FILL");
3264 return;
3265 }
3266
Mayank Grover4f50bba2017-07-19 18:17:08 +05303267 blk_sz_actual = ROUNDUP(sparse_header->blk_sz, CACHE_LINE);
3268 /* Integer overflow detected */
3269 if (blk_sz_actual < sparse_header->blk_sz)
3270 {
3271 fastboot_fail("Invalid block size");
3272 return;
3273 }
3274
3275 fill_buf = (uint32_t *)memalign(CACHE_LINE, blk_sz_actual);
Channagoud Kadabif627cf82013-09-09 14:08:20 -07003276 if (!fill_buf)
3277 {
3278 fastboot_fail("Malloc failed for: CHUNK_TYPE_FILL");
3279 return;
3280 }
3281
Parth Dixit64b1a482016-03-07 16:31:26 +05303282 if (data_end < (uintptr_t)data + sizeof(uint32_t)) {
vijay kumar800255e2015-04-24 20:26:19 +05303283 fastboot_fail("buffer overreads occured due to invalid sparse header");
Gaurav Nebhwani163cbf82016-03-17 21:21:27 +05303284 free(fill_buf);
vijay kumar800255e2015-04-24 20:26:19 +05303285 return;
3286 }
Channagoud Kadabif627cf82013-09-09 14:08:20 -07003287 fill_val = *(uint32_t *)data;
3288 data = (char *) data + sizeof(uint32_t);
Channagoud Kadabif627cf82013-09-09 14:08:20 -07003289
3290 for (i = 0; i < (sparse_header->blk_sz / sizeof(fill_val)); i++)
3291 {
3292 fill_buf[i] = fill_val;
3293 }
3294
Gaurav Nebhwanie94cbe12016-03-17 21:16:34 +05303295 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz))
3296 {
3297 fastboot_fail("bogus size for chunk FILL type");
3298 free(fill_buf);
3299 return;
3300 }
3301
wufeng.jiang813dc352015-06-02 23:02:46 -04003302 for (i = 0; i < chunk_header->chunk_sz; i++)
Channagoud Kadabif627cf82013-09-09 14:08:20 -07003303 {
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05303304 /* Make sure that the data written to partition does not exceed partition size */
3305 if ((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz + sparse_header->blk_sz > size)
3306 {
3307 fastboot_fail("Chunk data size for fill type exceeds partition size");
Gaurav Nebhwani163cbf82016-03-17 21:21:27 +05303308 free(fill_buf);
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05303309 return;
3310 }
3311
Channagoud Kadabif627cf82013-09-09 14:08:20 -07003312 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
3313 sparse_header->blk_sz,
3314 fill_buf))
3315 {
3316 fastboot_fail("flash write failure");
3317 free(fill_buf);
3318 return;
3319 }
3320
3321 total_blocks++;
3322 }
3323
3324 free(fill_buf);
3325 break;
3326
Ajay Dudani5c761132011-04-07 20:19:04 -07003327 case CHUNK_TYPE_DONT_CARE:
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05303328 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
3329 fastboot_fail("bogus size for chunk DONT CARE type");
3330 return;
3331 }
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07003332 total_blocks += chunk_header->chunk_sz;
3333 break;
3334
Ajay Dudani5c761132011-04-07 20:19:04 -07003335 case CHUNK_TYPE_CRC:
3336 if(chunk_header->total_sz != sparse_header->chunk_hdr_sz)
3337 {
wufeng.jiang813dc352015-06-02 23:02:46 -04003338 fastboot_fail("Bogus chunk size for chunk type CRC");
Ajay Dudani5c761132011-04-07 20:19:04 -07003339 return;
3340 }
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05303341 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
3342 fastboot_fail("bogus size for chunk CRC type");
3343 return;
3344 }
Ajay Dudani5c761132011-04-07 20:19:04 -07003345 total_blocks += chunk_header->chunk_sz;
Parth Dixit64b1a482016-03-07 16:31:26 +05303346 if ((uintptr_t)data > UINT_MAX - chunk_data_sz) {
vijay kumar800255e2015-04-24 20:26:19 +05303347 fastboot_fail("integer overflow occured");
3348 return;
3349 }
wufeng.jiang813dc352015-06-02 23:02:46 -04003350 data += (uint32_t)chunk_data_sz;
Parth Dixit64b1a482016-03-07 16:31:26 +05303351 if (data_end < (uintptr_t)data) {
vijay kumar800255e2015-04-24 20:26:19 +05303352 fastboot_fail("buffer overreads occured due to invalid sparse header");
3353 return;
3354 }
Ajay Dudani5c761132011-04-07 20:19:04 -07003355 break;
3356
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07003357 default:
Channagoud Kadabif627cf82013-09-09 14:08:20 -07003358 dprintf(CRITICAL, "Unkown chunk type: %x\n",chunk_header->chunk_type);
Ajay Dudani5c761132011-04-07 20:19:04 -07003359 fastboot_fail("Unknown chunk type");
3360 return;
3361 }
3362 }
3363
Ajay Dudani0c6927b2011-05-18 11:12:16 -07003364 dprintf(INFO, "Wrote %d blocks, expected to write %d blocks\n",
3365 total_blocks, sparse_header->total_blks);
3366
3367 if(total_blocks != sparse_header->total_blks)
3368 {
3369 fastboot_fail("sparse image write failure");
3370 }
Ajay Dudani5c761132011-04-07 20:19:04 -07003371
3372 fastboot_okay("");
3373 return;
3374}
3375
3376void cmd_flash_mmc(const char *arg, void *data, unsigned sz)
3377{
3378 sparse_header_t *sparse_header;
Ajay Dudanide984792015-03-02 09:57:41 -08003379 meta_header_t *meta_header;
Ajay Dudani5c761132011-04-07 20:19:04 -07003380
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07003381#ifdef SSD_ENABLE
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -08003382 /* 8 Byte Magic + 2048 Byte xml + Encrypted Data */
3383 unsigned int *magic_number = (unsigned int *) data;
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08003384 int ret=0;
3385 uint32 major_version=0;
3386 uint32 minor_version=0;
3387
3388 ret = scm_svc_version(&major_version,&minor_version);
3389 if(!ret)
3390 {
3391 if(major_version >= 2)
3392 {
Amir Samuelovbb65ce02013-05-05 12:20:18 +03003393 if( !strcmp(arg, "ssd") || !strcmp(arg, "tqs") )
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08003394 {
3395 ret = encrypt_scm((uint32 **) &data, &sz);
3396 if (ret != 0) {
3397 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
3398 return;
3399 }
3400
Amir Samuelovbb65ce02013-05-05 12:20:18 +03003401 /* Protect only for SSD */
3402 if (!strcmp(arg, "ssd")) {
3403 ret = scm_protect_keystore((uint32 *) data, sz);
3404 if (ret != 0) {
3405 dprintf(CRITICAL, "ERROR: scm_protect_keystore Failed\n");
3406 return;
3407 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08003408 }
3409 }
3410 else
3411 {
3412 ret = decrypt_scm_v2((uint32 **) &data, &sz);
3413 if(ret != 0)
3414 {
3415 dprintf(CRITICAL,"ERROR: Decryption Failure\n");
3416 return;
3417 }
3418 }
3419 }
3420 else
3421 {
3422 if (magic_number[0] == DECRYPT_MAGIC_0 &&
3423 magic_number[1] == DECRYPT_MAGIC_1)
3424 {
3425 ret = decrypt_scm((uint32 **) &data, &sz);
3426 if (ret != 0) {
3427 dprintf(CRITICAL, "ERROR: Invalid secure image\n");
3428 return;
3429 }
3430 }
3431 else if (magic_number[0] == ENCRYPT_MAGIC_0 &&
3432 magic_number[1] == ENCRYPT_MAGIC_1)
3433 {
3434 ret = encrypt_scm((uint32 **) &data, &sz);
3435 if (ret != 0) {
3436 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
3437 return;
3438 }
3439 }
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07003440 }
3441 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08003442 else
Neeti Desai127b9e02012-03-20 16:11:23 -07003443 {
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08003444 dprintf(CRITICAL,"INVALID SVC Version\n");
3445 return;
Neeti Desai127b9e02012-03-20 16:11:23 -07003446 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08003447#endif /* SSD_ENABLE */
Neeti Desai127b9e02012-03-20 16:11:23 -07003448
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003449#if VERIFIED_BOOT
Channagoud Kadabi35297672015-06-13 11:09:49 -07003450 if (target_build_variant_user())
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003451 {
lijuang511a2b52015-08-14 20:50:51 +08003452 /* if device is locked:
3453 * common partition will not allow to be flashed
3454 * critical partition will allow to flash image.
3455 */
3456 if(!device.is_unlocked && !critical_flash_allowed(arg)) {
3457 fastboot_fail("Partition flashing is not allowed");
3458 return;
3459 }
Mayank Grover889be1b2017-09-12 20:12:23 +05303460
lijuang511a2b52015-08-14 20:50:51 +08003461 /* if device critical is locked:
3462 * common partition will allow to be flashed
3463 * critical partition will not allow to flash image.
3464 */
Mayank Grover912eaa62017-10-26 12:08:53 +05303465 if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05303466 !device.is_unlock_critical &&
3467 critical_flash_allowed(arg)) {
3468 fastboot_fail("Critical partition flashing is not allowed");
3469 return;
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003470 }
3471 }
3472#endif
3473
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07003474 sparse_header = (sparse_header_t *) data;
Ajay Dudanide984792015-03-02 09:57:41 -08003475 meta_header = (meta_header_t *) data;
3476 if (sparse_header->magic == SPARSE_HEADER_MAGIC)
Ajay Dudani5c761132011-04-07 20:19:04 -07003477 cmd_flash_mmc_sparse_img(arg, data, sz);
Ajay Dudanide984792015-03-02 09:57:41 -08003478 else if (meta_header->magic == META_HEADER_MAGIC)
3479 cmd_flash_meta_img(arg, data, sz);
3480 else
3481 cmd_flash_mmc_img(arg, data, sz);
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07003482
3483#if VERIFIED_BOOT
Mayank Grover912eaa62017-10-26 12:08:53 +05303484 if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05303485 (!strncmp(arg, "system", 6)) &&
3486 !device.verity_mode)
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07003487 // reset dm_verity mode to enforcing
3488 device.verity_mode = 1;
3489 write_device_info(&device);
Parth Dixitddbc7352015-10-18 03:13:31 +05303490#endif
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07003491
Ajay Dudani5c761132011-04-07 20:19:04 -07003492 return;
3493}
3494
Tanya Brokhman84eaaf62015-02-24 21:57:06 +02003495void cmd_updatevol(const char *vol_name, void *data, unsigned sz)
3496{
3497 struct ptentry *sys_ptn;
3498 struct ptable *ptable;
3499
3500 ptable = flash_get_ptable();
3501 if (ptable == NULL) {
3502 fastboot_fail("partition table doesn't exist");
3503 return;
3504 }
3505
3506 sys_ptn = ptable_find(ptable, "system");
3507 if (sys_ptn == NULL) {
3508 fastboot_fail("system partition not found");
3509 return;
3510 }
3511
3512 sz = ROUND_TO_PAGE(sz, page_mask);
3513 if (update_ubi_vol(sys_ptn, vol_name, data, sz))
3514 fastboot_fail("update_ubi_vol failed");
3515 else
3516 fastboot_okay("");
3517}
3518
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003519void cmd_flash_nand(const char *arg, void *data, unsigned sz)
Dima Zavin214cc642009-01-26 11:16:21 -08003520{
3521 struct ptentry *ptn;
3522 struct ptable *ptable;
3523 unsigned extra = 0;
Gaurav Nebhwanid9730712016-05-06 14:28:29 +05303524 uint64_t partition_size = 0;
Vijay Kumar Pendotic6f22a72017-05-19 22:44:34 +05303525 unsigned bytes_to_round_page = 0;
3526 unsigned rounded_size = 0;
Dima Zavin214cc642009-01-26 11:16:21 -08003527
Kishor PK38ed93d2017-04-25 14:19:26 +05303528 if((uintptr_t)data > (UINT_MAX - sz)) {
3529 fastboot_fail("Cannot flash: image header corrupt");
3530 return;
3531 }
3532
Dima Zavin214cc642009-01-26 11:16:21 -08003533 ptable = flash_get_ptable();
3534 if (ptable == NULL) {
3535 fastboot_fail("partition table doesn't exist");
3536 return;
3537 }
3538
3539 ptn = ptable_find(ptable, arg);
3540 if (ptn == NULL) {
Tanya Brokhman84eaaf62015-02-24 21:57:06 +02003541 dprintf(INFO, "unknown partition name (%s). Trying updatevol\n",
3542 arg);
3543 cmd_updatevol(arg, data, sz);
Dima Zavin214cc642009-01-26 11:16:21 -08003544 return;
3545 }
3546
3547 if (!strcmp(ptn->name, "boot") || !strcmp(ptn->name, "recovery")) {
Kishor PK38ed93d2017-04-25 14:19:26 +05303548 if((sz > BOOT_MAGIC_SIZE) && (!memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE))) {
3549 dprintf(INFO, "Verified the BOOT_MAGIC in image header \n");
3550 } else {
3551 fastboot_fail("Image is not a boot image");
Dima Zavin214cc642009-01-26 11:16:21 -08003552 return;
3553 }
3554 }
3555
Amol Jadi5c61a952012-05-04 17:05:35 -07003556 if (!strcmp(ptn->name, "system")
Deepa Dinamani13e32c42012-03-12 14:34:17 -07003557 || !strcmp(ptn->name, "userdata")
3558 || !strcmp(ptn->name, "persist")
Sundarajan Srinivasanb063a852013-11-19 14:02:27 -08003559 || !strcmp(ptn->name, "recoveryfs")
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -08003560 || !strcmp(ptn->name, "modem"))
Tanya Brokhman1c94f1a2015-02-15 09:05:03 +02003561 extra = 1;
Kishor PK38ed93d2017-04-25 14:19:26 +05303562 else {
Vijay Kumar Pendotic6f22a72017-05-19 22:44:34 +05303563 rounded_size = ROUNDUP(sz, page_size);
3564 bytes_to_round_page = rounded_size - sz;
3565 if (bytes_to_round_page) {
3566 if (((uintptr_t)data + sz ) > (UINT_MAX - bytes_to_round_page)) {
3567 fastboot_fail("Integer overflow detected");
3568 return;
3569 }
3570 if (((uintptr_t)data + sz + bytes_to_round_page) >
3571 ((uintptr_t)target_get_scratch_address() + target_get_max_flash_size())) {
3572 fastboot_fail("Buffer size is not aligned to page_size");
3573 return;
3574 }
3575 else {
3576 memset(data + sz, 0, bytes_to_round_page);
3577 sz = rounded_size;
3578 }
Kishor PK38ed93d2017-04-25 14:19:26 +05303579 }
Gaurav Nebhwanid9730712016-05-06 14:28:29 +05303580 }
3581
Kishor PK38ed93d2017-04-25 14:19:26 +05303582 /*Checking partition_size for the possible integer overflow */
3583 partition_size = validate_partition_size(ptn);
3584
Gaurav Nebhwanid9730712016-05-06 14:28:29 +05303585 if (sz > partition_size) {
3586 fastboot_fail("Image size too large");
3587 return;
3588 }
3589
Dima Zavin214cc642009-01-26 11:16:21 -08003590 dprintf(INFO, "writing %d bytes to '%s'\n", sz, ptn->name);
Mayank Grover6cde9352017-06-06 18:45:23 +05303591 if ((sz > UBI_EC_HDR_SIZE) &&
3592 (!memcmp((void *)data, UBI_MAGIC, UBI_MAGIC_SIZE))) {
Tanya Brokhman1c94f1a2015-02-15 09:05:03 +02003593 if (flash_ubi_img(ptn, data, sz)) {
3594 fastboot_fail("flash write failure");
3595 return;
3596 }
3597 } else {
3598 if (flash_write(ptn, extra, data, sz)) {
3599 fastboot_fail("flash write failure");
3600 return;
3601 }
Dima Zavin214cc642009-01-26 11:16:21 -08003602 }
3603 dprintf(INFO, "partition '%s' updated\n", ptn->name);
3604 fastboot_okay("");
3605}
3606
Kishor PK38ed93d2017-04-25 14:19:26 +05303607
3608static inline uint64_t validate_partition_size(struct ptentry *ptn)
3609{
3610 if (ptn->length && flash_num_pages_per_blk() && page_size) {
3611 if ((ptn->length < ( UINT_MAX / flash_num_pages_per_blk())) && ((ptn->length * flash_num_pages_per_blk()) < ( UINT_MAX / page_size))) {
3612 return ptn->length * flash_num_pages_per_blk() * page_size;
3613 }
3614 }
3615 return 0;
3616}
3617
3618
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003619void cmd_flash(const char *arg, void *data, unsigned sz)
3620{
3621 if(target_is_emmc_boot())
3622 cmd_flash_mmc(arg, data, sz);
3623 else
3624 cmd_flash_nand(arg, data, sz);
3625}
3626
Dima Zavin214cc642009-01-26 11:16:21 -08003627void cmd_continue(const char *arg, void *data, unsigned sz)
3628{
3629 fastboot_okay("");
Amol Jadi7c4316c2013-10-07 14:19:26 -07003630 fastboot_stop();
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003631
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003632 if (target_is_emmc_boot())
3633 {
lijuanga40d6302015-07-20 20:10:13 +08003634#if FBCON_DISPLAY_MSG
lijuangde34d502016-02-26 16:04:50 +08003635 /* Exit keys' detection thread firstly */
3636 exit_menu_keys_detection();
lijuanga40d6302015-07-20 20:10:13 +08003637#endif
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003638 boot_linux_from_mmc();
3639 }
3640 else
3641 {
3642 boot_linux_from_flash();
3643 }
Dima Zavin214cc642009-01-26 11:16:21 -08003644}
3645
Chandan Uddaraju94183c02010-01-15 15:13:59 -08003646void cmd_reboot(const char *arg, void *data, unsigned sz)
3647{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003648 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08003649 fastboot_okay("");
3650 reboot_device(0);
3651}
3652
Mayank Grover351a75e2017-05-30 20:06:08 +05303653void cmd_set_active(const char *arg, void *data, unsigned sz)
3654{
Mayank Grover6ccc1c92017-07-04 17:36:46 +05303655 char *p, *sp = NULL;
Mayank Grover351a75e2017-05-30 20:06:08 +05303656 unsigned i,current_active_slot;
3657 const char *current_slot_suffix;
3658
3659 if (!partition_multislot_is_supported())
3660 {
3661 fastboot_fail("Command not supported");
3662 return;
3663 }
3664
3665 if (arg)
3666 {
Mayank Grover6ccc1c92017-07-04 17:36:46 +05303667 p = strtok_r((char *)arg, ":", &sp);
Mayank Grover5eb5f692017-07-19 20:16:04 +05303668 if (p)
Mayank Grover351a75e2017-05-30 20:06:08 +05303669 {
3670 current_active_slot = partition_find_active_slot();
3671
3672 /* Check if trying to make curent slot active */
3673 current_slot_suffix = SUFFIX_SLOT(current_active_slot);
Mayank Grover6ccc1c92017-07-04 17:36:46 +05303674 current_slot_suffix = strtok_r((char *)current_slot_suffix,
3675 (char *)suffix_delimiter, &sp);
3676
Mayank Grover5eb5f692017-07-19 20:16:04 +05303677 if (current_slot_suffix &&
3678 !strncmp(p, current_slot_suffix, strlen(current_slot_suffix)))
Mayank Grover351a75e2017-05-30 20:06:08 +05303679 {
3680 fastboot_okay("Slot already set active");
3681 return;
3682 }
3683 else
3684 {
3685 for (i = 0; i < AB_SUPPORTED_SLOTS; i++)
3686 {
3687 current_slot_suffix = SUFFIX_SLOT(i);
Mayank Grover6ccc1c92017-07-04 17:36:46 +05303688 current_slot_suffix = strtok_r((char *)current_slot_suffix,
3689 (char *)suffix_delimiter, &sp);
Mayank Grover5eb5f692017-07-19 20:16:04 +05303690 if (current_slot_suffix &&
3691 !strncmp(p, current_slot_suffix, strlen(current_slot_suffix)))
Mayank Grover351a75e2017-05-30 20:06:08 +05303692 {
3693 partition_switch_slots(current_active_slot, i);
3694 publish_getvar_multislot_vars();
3695 fastboot_okay("");
3696 return;
3697 }
3698 }
3699 }
3700 }
3701 }
3702 fastboot_fail("Invalid slot suffix.");
3703 return;
3704}
3705
Chandan Uddaraju94183c02010-01-15 15:13:59 -08003706void cmd_reboot_bootloader(const char *arg, void *data, unsigned sz)
3707{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003708 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08003709 fastboot_okay("");
3710 reboot_device(FASTBOOT_MODE);
3711}
3712
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003713void cmd_oem_enable_charger_screen(const char *arg, void *data, unsigned size)
3714{
3715 dprintf(INFO, "Enabling charger screen check\n");
3716 device.charger_screen_enabled = 1;
3717 write_device_info(&device);
3718 fastboot_okay("");
3719}
3720
3721void cmd_oem_disable_charger_screen(const char *arg, void *data, unsigned size)
3722{
3723 dprintf(INFO, "Disabling charger screen check\n");
3724 device.charger_screen_enabled = 0;
3725 write_device_info(&device);
3726 fastboot_okay("");
3727}
3728
lijuanga25d8bb2015-09-16 13:11:52 +08003729void cmd_oem_off_mode_charger(const char *arg, void *data, unsigned size)
3730{
3731 char *p = NULL;
3732 const char *delim = " \t\n\r";
Parth Dixit407f15b2016-01-07 14:47:37 +05303733 char *sp;
lijuanga25d8bb2015-09-16 13:11:52 +08003734
3735 if (arg) {
Parth Dixit407f15b2016-01-07 14:47:37 +05303736 p = strtok_r((char *)arg, delim, &sp);
lijuanga25d8bb2015-09-16 13:11:52 +08003737 if (p) {
3738 if (!strncmp(p, "0", 1)) {
3739 device.charger_screen_enabled = 0;
3740 } else if (!strncmp(p, "1", 1)) {
3741 device.charger_screen_enabled = 1;
3742 }
3743 }
3744 }
3745
3746 /* update charger_screen_enabled value for getvar
3747 * command
3748 */
3749 snprintf(charger_screen_enabled, MAX_RSP_SIZE, "%d",
3750 device.charger_screen_enabled);
3751
3752 write_device_info(&device);
3753 fastboot_okay("");
3754}
3755
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05303756void cmd_oem_select_display_panel(const char *arg, void *data, unsigned size)
3757{
3758 dprintf(INFO, "Selecting display panel %s\n", arg);
3759 if (arg)
3760 strlcpy(device.display_panel, arg,
3761 sizeof(device.display_panel));
3762 write_device_info(&device);
3763 fastboot_okay("");
3764}
3765
Shashank Mittal162244e2011-08-08 19:01:25 -07003766void cmd_oem_unlock(const char *arg, void *data, unsigned sz)
3767{
lijuang4ece1e72015-08-14 21:02:36 +08003768 set_device_unlock(UNLOCK, TRUE);
vijay kumarc65876c2015-04-24 13:29:16 +05303769}
3770
3771void cmd_oem_unlock_go(const char *arg, void *data, unsigned sz)
3772{
lijuang4ece1e72015-08-14 21:02:36 +08003773 if(!device.is_unlocked) {
vijay kumarc65876c2015-04-24 13:29:16 +05303774 if(!is_allow_unlock) {
3775 fastboot_fail("oem unlock is not allowed");
3776 return;
3777 }
3778
lijuang4ece1e72015-08-14 21:02:36 +08003779 set_device_unlock_value(UNLOCK, TRUE);
vijay kumarc65876c2015-04-24 13:29:16 +05303780
lijuang4ece1e72015-08-14 21:02:36 +08003781 /* wipe data */
vijay kumarc65876c2015-04-24 13:29:16 +05303782 struct recovery_message msg;
Kishor PK60a68212017-05-08 16:55:57 +05303783 memset(&msg, 0, sizeof(msg));
vijay kumarc65876c2015-04-24 13:29:16 +05303784 snprintf(msg.recovery, sizeof(msg.recovery), "recovery\n--wipe_data");
3785 write_misc(0, &msg, sizeof(msg));
3786
3787 fastboot_okay("");
3788 reboot_device(RECOVERY_MODE);
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003789 }
3790 fastboot_okay("");
3791}
3792
Channagoud Kadabiad259832015-05-29 11:14:17 -07003793static int aboot_frp_unlock(char *pname, void *data, unsigned sz)
3794{
Mayank Grover70f8b0e2017-05-17 11:49:00 +05303795 int ret=1;
3796 bool authentication_success=false;
Channagoud Kadabiad259832015-05-29 11:14:17 -07003797
Mayank Grover70f8b0e2017-05-17 11:49:00 +05303798 /*
3799 Authentication method not implemented.
3800
3801 OEM to implement, authentication system which on successful validataion,
3802 calls write_allow_oem_unlock() with is_allow_unlock.
3803 */
3804#if 0
3805 authentication_success = oem_specific_auth_mthd();
3806#endif
3807
3808 if (authentication_success)
Channagoud Kadabiad259832015-05-29 11:14:17 -07003809 {
Mayank Grover70f8b0e2017-05-17 11:49:00 +05303810 is_allow_unlock = true;
3811 write_allow_oem_unlock(is_allow_unlock);
3812 ret = 0;
Channagoud Kadabiad259832015-05-29 11:14:17 -07003813 }
3814 return ret;
3815}
3816
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003817void cmd_oem_lock(const char *arg, void *data, unsigned sz)
3818{
lijuang4ece1e72015-08-14 21:02:36 +08003819 set_device_unlock(UNLOCK, FALSE);
Shashank Mittal162244e2011-08-08 19:01:25 -07003820}
3821
Shashank Mittala0032282011-08-26 14:50:11 -07003822void cmd_oem_devinfo(const char *arg, void *data, unsigned sz)
3823{
lijuang511a2b52015-08-14 20:50:51 +08003824 char response[MAX_RSP_SIZE];
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003825 snprintf(response, sizeof(response), "\tDevice tampered: %s", (device.is_tampered ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07003826 fastboot_info(response);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003827 snprintf(response, sizeof(response), "\tDevice unlocked: %s", (device.is_unlocked ? "true" : "false"));
3828 fastboot_info(response);
Mayank Grover889be1b2017-09-12 20:12:23 +05303829#if VERIFIED_BOOT
Mayank Grover912eaa62017-10-26 12:08:53 +05303830 if (VB_M <= target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +05303831 {
3832 snprintf(response, sizeof(response), "\tDevice critical unlocked: %s",
3833 (device.is_unlock_critical ? "true" : "false"));
3834 fastboot_info(response);
3835 }
Parth Dixitddbc7352015-10-18 03:13:31 +05303836#endif
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003837 snprintf(response, sizeof(response), "\tCharger screen enabled: %s", (device.charger_screen_enabled ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07003838 fastboot_info(response);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05303839 snprintf(response, sizeof(response), "\tDisplay panel: %s", (device.display_panel));
3840 fastboot_info(response);
Shashank Mittala0032282011-08-26 14:50:11 -07003841 fastboot_okay("");
3842}
3843
lijuang511a2b52015-08-14 20:50:51 +08003844void cmd_flashing_get_unlock_ability(const char *arg, void *data, unsigned sz)
3845{
3846 char response[MAX_RSP_SIZE];
3847 snprintf(response, sizeof(response), "\tget_unlock_ability: %d", is_allow_unlock);
3848 fastboot_info(response);
3849 fastboot_okay("");
3850}
3851
3852void cmd_flashing_lock_critical(const char *arg, void *data, unsigned sz)
3853{
lijuang4ece1e72015-08-14 21:02:36 +08003854 set_device_unlock(UNLOCK_CRITICAL, FALSE);
lijuang511a2b52015-08-14 20:50:51 +08003855}
3856
3857void cmd_flashing_unlock_critical(const char *arg, void *data, unsigned sz)
3858{
lijuang4ece1e72015-08-14 21:02:36 +08003859 set_device_unlock(UNLOCK_CRITICAL, TRUE);
lijuang511a2b52015-08-14 20:50:51 +08003860}
3861
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07003862void cmd_preflash(const char *arg, void *data, unsigned sz)
3863{
3864 fastboot_okay("");
3865}
3866
Mao Flynn7b379f32015-04-20 00:28:30 +08003867static uint8_t logo_header[LOGO_IMG_HEADER_SIZE];
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303868
Mao Flynn7b379f32015-04-20 00:28:30 +08003869int splash_screen_check_header(logo_img_header *header)
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303870{
Mao Flynn7b379f32015-04-20 00:28:30 +08003871 if (memcmp(header->magic, LOGO_IMG_MAGIC, 8))
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303872 return -1;
Mao Flynn7b379f32015-04-20 00:28:30 +08003873 if (header->width == 0 || header->height == 0)
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303874 return -1;
3875 return 0;
3876}
3877
Mao Flynn7b379f32015-04-20 00:28:30 +08003878int splash_screen_flash()
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003879{
3880 struct ptentry *ptn;
3881 struct ptable *ptable;
Mao Flynn7b379f32015-04-20 00:28:30 +08003882 struct logo_img_header *header;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003883 struct fbcon_config *fb_display = NULL;
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08003884
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303885 ptable = flash_get_ptable();
3886 if (ptable == NULL) {
Mao Flynn7b379f32015-04-20 00:28:30 +08003887 dprintf(CRITICAL, "ERROR: Partition table not found\n");
3888 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303889 }
Mao Flynn7b379f32015-04-20 00:28:30 +08003890
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303891 ptn = ptable_find(ptable, "splash");
3892 if (ptn == NULL) {
3893 dprintf(CRITICAL, "ERROR: splash Partition not found\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003894 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303895 }
Mao Flynn7b379f32015-04-20 00:28:30 +08003896 if (flash_read(ptn, 0, (void *)logo_header, LOGO_IMG_HEADER_SIZE)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303897 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003898 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303899 }
3900
Mao Flynn7b379f32015-04-20 00:28:30 +08003901 header = (struct logo_img_header *)logo_header;
3902 if (splash_screen_check_header(header)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303903 dprintf(CRITICAL, "ERROR: Boot image header invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003904 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303905 }
3906
3907 fb_display = fbcon_display();
3908 if (fb_display) {
Mao Flynn7b379f32015-04-20 00:28:30 +08003909 if (header->type && (header->blocks != 0)) { // RLE24 compressed data
3910 uint8_t *base = (uint8_t *) fb_display->base + LOGO_IMG_OFFSET;
3911
3912 /* if the logo is full-screen size, remove "fbcon_clear()" */
3913 if ((header->width != fb_display->width)
3914 || (header->height != fb_display->height))
3915 fbcon_clear();
3916
3917 if (flash_read(ptn + LOGO_IMG_HEADER_SIZE, 0,
3918 (uint32_t *)base,
3919 (header->blocks * 512))) {
3920 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
3921 return -1;
3922 }
3923 fbcon_extract_to_screen(header, base);
3924 return 0;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003925 }
Mao Flynn7b379f32015-04-20 00:28:30 +08003926
Veera Sundaram Sankaranc33416f2015-09-28 10:25:55 -07003927 if ((header->width > fb_display->width) || (header->height > fb_display->height)) {
3928 dprintf(CRITICAL, "Logo config greater than fb config. Fall back default logo\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003929 return -1;
3930 }
3931
Vineet Bajaj99291ed2014-09-09 12:29:46 +05303932 uint8_t *base = (uint8_t *) fb_display->base;
Sachin Bhayare619e9e42017-05-15 13:10:31 +05303933 uint32_t fb_size = ROUNDUP(fb_display->width *
3934 fb_display->height *
3935 (fb_display->bpp / 8), 4096);
3936 uint32_t splash_size = ((((header->width * header->height *
3937 fb_display->bpp/8) + 511) >> 9) << 9);
3938
3939 if (splash_size > fb_size) {
3940 dprintf(CRITICAL, "ERROR: Splash image size invalid\n");
3941 return -1;
3942 }
3943
Mao Flynn7b379f32015-04-20 00:28:30 +08003944 if (flash_read(ptn + LOGO_IMG_HEADER_SIZE, 0,
3945 (uint32_t *)base,
3946 ((((header->width * header->height * fb_display->bpp/8) + 511) >> 9) << 9))) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303947 fbcon_clear();
Vineet Bajaj99291ed2014-09-09 12:29:46 +05303948 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003949 return -1;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003950 }
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303951 }
3952
Mao Flynn7b379f32015-04-20 00:28:30 +08003953 return 0;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303954}
3955
Mao Flynn7b379f32015-04-20 00:28:30 +08003956int splash_screen_mmc()
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303957{
3958 int index = INVALID_PTN;
3959 unsigned long long ptn = 0;
3960 struct fbcon_config *fb_display = NULL;
Mao Flynn7b379f32015-04-20 00:28:30 +08003961 struct logo_img_header *header;
Mao Flynn1893a7f2015-06-03 12:03:36 +08003962 uint32_t blocksize, realsize, readsize;
3963 uint8_t *base;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303964
3965 index = partition_get_index("splash");
3966 if (index == 0) {
3967 dprintf(CRITICAL, "ERROR: splash Partition table not found\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003968 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303969 }
3970
3971 ptn = partition_get_offset(index);
3972 if (ptn == 0) {
3973 dprintf(CRITICAL, "ERROR: splash Partition invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003974 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303975 }
3976
Mao Flynn1893a7f2015-06-03 12:03:36 +08003977 mmc_set_lun(partition_get_lun(index));
3978
3979 blocksize = mmc_get_device_blocksize();
3980 if (blocksize == 0) {
3981 dprintf(CRITICAL, "ERROR:splash Partition invalid blocksize\n");
3982 return -1;
3983 }
3984
3985 fb_display = fbcon_display();
Channagoud Kadabidaf10a62015-07-22 11:51:55 -07003986 if (!fb_display)
3987 {
3988 dprintf(CRITICAL, "ERROR: fb config is not allocated\n");
3989 return -1;
3990 }
3991
Mao Flynn1893a7f2015-06-03 12:03:36 +08003992 base = (uint8_t *) fb_display->base;
3993
3994 if (mmc_read(ptn, (uint32_t *)(base + LOGO_IMG_OFFSET), blocksize)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303995 dprintf(CRITICAL, "ERROR: Cannot read splash image header\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003996 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303997 }
3998
Mao Flynn1893a7f2015-06-03 12:03:36 +08003999 header = (struct logo_img_header *)(base + LOGO_IMG_OFFSET);
Mao Flynn7b379f32015-04-20 00:28:30 +08004000 if (splash_screen_check_header(header)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304001 dprintf(CRITICAL, "ERROR: Splash image header invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08004002 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304003 }
4004
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304005 if (fb_display) {
Mao Flynn1893a7f2015-06-03 12:03:36 +08004006 if (header->type && (header->blocks != 0)) { /* 1 RLE24 compressed data */
4007 base += LOGO_IMG_OFFSET;
Mao Flynn7b379f32015-04-20 00:28:30 +08004008
Mao Flynn1893a7f2015-06-03 12:03:36 +08004009 realsize = header->blocks * 512;
4010 readsize = ROUNDUP((realsize + LOGO_IMG_HEADER_SIZE), blocksize) - blocksize;
4011
4012 /* if the logo is not full-screen size, clean screen */
Mao Flynn7b379f32015-04-20 00:28:30 +08004013 if ((header->width != fb_display->width)
4014 || (header->height != fb_display->height))
4015 fbcon_clear();
4016
Sachin Bhayare619e9e42017-05-15 13:10:31 +05304017 uint32_t fb_size = ROUNDUP(fb_display->width *
4018 fb_display->height *
4019 (fb_display->bpp / 8), 4096);
4020
4021 if (readsize > fb_size) {
4022 dprintf(CRITICAL, "ERROR: Splash image size invalid\n");
4023 return -1;
4024 }
4025
Mao Flynn1893a7f2015-06-03 12:03:36 +08004026 if (mmc_read(ptn + blocksize, (uint32_t *)(base + blocksize), readsize)) {
Mao Flynn7b379f32015-04-20 00:28:30 +08004027 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
4028 return -1;
4029 }
Mao Flynn7b379f32015-04-20 00:28:30 +08004030
Mao Flynn1893a7f2015-06-03 12:03:36 +08004031 fbcon_extract_to_screen(header, (base + LOGO_IMG_HEADER_SIZE));
4032 } else { /* 2 Raw BGR data */
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304033
Veera Sundaram Sankaranc33416f2015-09-28 10:25:55 -07004034 if ((header->width > fb_display->width) || (header->height > fb_display->height)) {
4035 dprintf(CRITICAL, "Logo config greater than fb config. Fall back default logo\n");
Mao Flynn1893a7f2015-06-03 12:03:36 +08004036 return -1;
4037 }
4038
4039 realsize = header->width * header->height * fb_display->bpp / 8;
4040 readsize = ROUNDUP((realsize + LOGO_IMG_HEADER_SIZE), blocksize) - blocksize;
4041
4042 if (blocksize == LOGO_IMG_HEADER_SIZE) { /* read the content directly */
4043 if (mmc_read((ptn + LOGO_IMG_HEADER_SIZE), (uint32_t *)base, readsize)) {
4044 fbcon_clear();
4045 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
4046 return -1;
4047 }
4048 } else {
4049 if (mmc_read(ptn + blocksize ,
4050 (uint32_t *)(base + LOGO_IMG_OFFSET + blocksize), readsize)) {
4051 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
4052 return -1;
4053 }
4054 memmove(base, (base + LOGO_IMG_OFFSET + LOGO_IMG_HEADER_SIZE), realsize);
4055 }
4056 }
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304057 }
4058
Mao Flynn7b379f32015-04-20 00:28:30 +08004059 return 0;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304060}
4061
Mao Flynn7b379f32015-04-20 00:28:30 +08004062int fetch_image_from_partition()
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304063{
4064 if (target_is_emmc_boot()) {
4065 return splash_screen_mmc();
4066 } else {
4067 return splash_screen_flash();
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07004068 }
4069}
4070
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07004071/* Get the size from partiton name */
4072static void get_partition_size(const char *arg, char *response)
4073{
4074 uint64_t ptn = 0;
4075 uint64_t size;
4076 int index = INVALID_PTN;
4077
4078 index = partition_get_index(arg);
4079
4080 if (index == INVALID_PTN)
4081 {
4082 dprintf(CRITICAL, "Invalid partition index\n");
4083 return;
4084 }
4085
4086 ptn = partition_get_offset(index);
4087
4088 if(!ptn)
4089 {
4090 dprintf(CRITICAL, "Invalid partition name %s\n", arg);
4091 return;
4092 }
4093
4094 size = partition_get_size(index);
4095
4096 snprintf(response, MAX_RSP_SIZE, "\t 0x%llx", size);
4097 return;
4098}
4099
4100/*
4101 * Publish the partition type & size info
4102 * fastboot getvar will publish the required information.
4103 * fastboot getvar partition_size:<partition_name>: partition size in hex
4104 * fastboot getvar partition_type:<partition_name>: partition type (ext/fat)
4105 */
4106static void publish_getvar_partition_info(struct getvar_partition_info *info, uint8_t num_parts)
4107{
Mayank Grover7b8a8f82017-10-27 13:07:59 +05304108 uint8_t i,n;
4109 struct partition_entry *ptn_entry =
4110 partition_get_partition_entries();
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07004111
4112 for (i = 0; i < num_parts; i++) {
Mayank Grover7b8a8f82017-10-27 13:07:59 +05304113 strlcat(info[i].part_name, (char const *)ptn_entry[i].name, MAX_RSP_SIZE);
4114 strlcat(info[i].getvar_size, "partition-size:", MAX_GET_VAR_NAME_SIZE);
4115 strlcat(info[i].getvar_type, "partition-type:", MAX_GET_VAR_NAME_SIZE);
4116
4117 /* Mark partiton type for known paritions only */
4118 for (n=0; n < ARRAY_SIZE(part_type_known); n++)
4119 {
4120 if (!strncmp(part_type_known[n].part_name, info[i].part_name,
4121 strlen(part_type_known[n].part_name)))
4122 {
4123 strlcat(info[i].type_response,
4124 part_type_known[n].type_response,
4125 MAX_RSP_SIZE);
4126 break;
4127 }
4128 }
4129
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07004130 get_partition_size(info[i].part_name, info[i].size_response);
4131
4132 if (strlcat(info[i].getvar_size, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
4133 {
4134 dprintf(CRITICAL, "partition size name truncated\n");
4135 return;
4136 }
4137 if (strlcat(info[i].getvar_type, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
4138 {
4139 dprintf(CRITICAL, "partition type name truncated\n");
4140 return;
4141 }
4142
4143 /* publish partition size & type info */
4144 fastboot_publish((const char *) info[i].getvar_size, (const char *) info[i].size_response);
4145 fastboot_publish((const char *) info[i].getvar_type, (const char *) info[i].type_response);
4146 }
4147}
4148
Mayank Grover351a75e2017-05-30 20:06:08 +05304149void publish_getvar_multislot_vars()
4150{
4151 int i,count;
4152 static bool published = false;
4153 static char slot_count[MAX_RSP_SIZE];
4154 static struct ab_slot_info slot_info[AB_SUPPORTED_SLOTS];
4155 static char active_slot_suffix[MAX_RSP_SIZE];
4156 static char has_slot_pname[NUM_PARTITIONS][MAX_GET_VAR_NAME_SIZE];
4157 static char has_slot_reply[NUM_PARTITIONS][MAX_RSP_SIZE];
4158 const char *tmp;
4159 char tmpbuff[MAX_GET_VAR_NAME_SIZE];
Mayank Grover2bd84bb2017-06-20 11:06:07 +05304160 signed active_slt;
Mayank Grover351a75e2017-05-30 20:06:08 +05304161
4162 if (!published)
4163 {
4164 /* Update slot meta info */
4165 count = partition_fill_partition_meta(has_slot_pname, has_slot_reply,
4166 partition_get_partition_count());
4167 for(i=0; i<count; i++)
4168 {
4169 memset(tmpbuff, 0, MAX_GET_VAR_NAME_SIZE);
Mayank Grover2bd84bb2017-06-20 11:06:07 +05304170 snprintf(tmpbuff, MAX_GET_VAR_NAME_SIZE,"has-slot:%s",
4171 has_slot_pname[i]);
4172 strlcpy(has_slot_pname[i], tmpbuff, MAX_GET_VAR_NAME_SIZE);
Mayank Grover351a75e2017-05-30 20:06:08 +05304173 fastboot_publish(has_slot_pname[i], has_slot_reply[i]);
4174 }
4175
4176 for (i=0; i<AB_SUPPORTED_SLOTS; i++)
4177 {
4178 tmp = SUFFIX_SLOT(i);
Mayank Grover7b8a8f82017-10-27 13:07:59 +05304179 tmp++; // to remove "_" from slot_suffix.
Mayank Grover2bd84bb2017-06-20 11:06:07 +05304180 snprintf(slot_info[i].slot_is_unbootable, sizeof(slot_info[i].slot_is_unbootable),
4181 "slot-unbootable:%s", tmp);
4182 snprintf(slot_info[i].slot_is_active, sizeof(slot_info[i].slot_is_active),
4183 "slot-active:%s", tmp);
4184 snprintf(slot_info[i].slot_is_succesful, sizeof(slot_info[i].slot_is_succesful),
4185 "slot-success:%s", tmp);
4186 snprintf(slot_info[i].slot_retry_count, sizeof(slot_info[i].slot_retry_count),
4187 "slot-retry-count:%s", tmp);
Mayank Grover351a75e2017-05-30 20:06:08 +05304188 fastboot_publish(slot_info[i].slot_is_unbootable,
4189 slot_info[i].slot_is_unbootable_rsp);
4190 fastboot_publish(slot_info[i].slot_is_active,
4191 slot_info[i].slot_is_active_rsp);
4192 fastboot_publish(slot_info[i].slot_is_succesful,
4193 slot_info[i].slot_is_succesful_rsp);
4194 fastboot_publish(slot_info[i].slot_retry_count,
4195 slot_info[i].slot_retry_count_rsp);
4196 }
4197 fastboot_publish("current-slot", active_slot_suffix);
4198 snprintf(slot_count, sizeof(slot_count),"%d", AB_SUPPORTED_SLOTS);
4199 fastboot_publish("slot-count", slot_count);
4200 published = true;
4201 }
4202
Mayank Grover2bd84bb2017-06-20 11:06:07 +05304203 active_slt = partition_find_active_slot();
4204 if (active_slt != INVALID)
Mayank Grover7b8a8f82017-10-27 13:07:59 +05304205 {
4206 tmp = SUFFIX_SLOT(active_slt);
4207 tmp++; // to remove "_" from slot_suffix.
4208 snprintf(active_slot_suffix, sizeof(active_slot_suffix), "%s", tmp);
4209 }
Mayank Grover2bd84bb2017-06-20 11:06:07 +05304210 else
4211 strlcpy(active_slot_suffix, "INVALID", sizeof(active_slot_suffix));
4212
Mayank Grover351a75e2017-05-30 20:06:08 +05304213 /* Update partition meta information */
4214 partition_fill_slot_meta(slot_info);
4215 return;
4216}
4217
lijuang4ece1e72015-08-14 21:02:36 +08004218void get_product_name(unsigned char *buf)
4219{
4220 snprintf((char*)buf, MAX_RSP_SIZE, "%s", TARGET(BOARD));
4221 return;
4222}
4223
Mayank Grover7b8a8f82017-10-27 13:07:59 +05304224#if PRODUCT_IOT
4225void get_bootloader_version_iot(unsigned char *buf)
4226{
4227 if (buf != NULL)
4228 {
4229 strlcpy(buf, TARGET(BOARD), MAX_VERSION_LEN);
4230 strlcat(buf, "-", MAX_VERSION_LEN);
4231 strlcat(buf, PRODUCT_IOT_VERSION, MAX_VERSION_LEN);
4232 }
4233 return;
4234}
4235#endif
4236
lijuang4ece1e72015-08-14 21:02:36 +08004237void get_bootloader_version(unsigned char *buf)
4238{
4239 snprintf((char*)buf, MAX_RSP_SIZE, "%s", device.bootloader_version);
4240 return;
4241}
4242
4243void get_baseband_version(unsigned char *buf)
4244{
4245 snprintf((char*)buf, MAX_RSP_SIZE, "%s", device.radio_version);
4246 return;
4247}
4248
4249bool is_device_locked()
4250{
4251 return device.is_unlocked ? false:true;
4252}
4253
Amol Jadi5edf3552013-07-23 14:15:34 -07004254/* register commands and variables for fastboot */
4255void aboot_fastboot_register_commands(void)
4256{
Sridhar Parasurame94e8152014-10-24 14:06:03 -07004257 int i;
lijuangf16461c2015-08-03 17:09:34 +08004258 char hw_platform_buf[MAX_RSP_SIZE];
Amol Jadi5edf3552013-07-23 14:15:34 -07004259
Sridhar Parasurame94e8152014-10-24 14:06:03 -07004260 struct fastboot_cmd_desc cmd_list[] = {
lijuang511a2b52015-08-14 20:50:51 +08004261 /* By default the enabled list is empty. */
4262 {"", NULL},
4263 /* move commands enclosed within the below ifndef to here
4264 * if they need to be enabled in user build.
4265 */
Sridhar Parasurame94e8152014-10-24 14:06:03 -07004266#ifndef DISABLE_FASTBOOT_CMDS
lijuang511a2b52015-08-14 20:50:51 +08004267 /* Register the following commands only for non-user builds */
4268 {"flash:", cmd_flash},
4269 {"erase:", cmd_erase},
4270 {"boot", cmd_boot},
4271 {"continue", cmd_continue},
4272 {"reboot", cmd_reboot},
4273 {"reboot-bootloader", cmd_reboot_bootloader},
4274 {"oem unlock", cmd_oem_unlock},
4275 {"oem unlock-go", cmd_oem_unlock_go},
4276 {"oem lock", cmd_oem_lock},
4277 {"flashing unlock", cmd_oem_unlock},
4278 {"flashing lock", cmd_oem_lock},
4279 {"flashing lock_critical", cmd_flashing_lock_critical},
4280 {"flashing unlock_critical", cmd_flashing_unlock_critical},
4281 {"flashing get_unlock_ability", cmd_flashing_get_unlock_ability},
4282 {"oem device-info", cmd_oem_devinfo},
4283 {"preflash", cmd_preflash},
4284 {"oem enable-charger-screen", cmd_oem_enable_charger_screen},
4285 {"oem disable-charger-screen", cmd_oem_disable_charger_screen},
lijuanga25d8bb2015-09-16 13:11:52 +08004286 {"oem off-mode-charge", cmd_oem_off_mode_charger},
lijuang511a2b52015-08-14 20:50:51 +08004287 {"oem select-display-panel", cmd_oem_select_display_panel},
Mayank Grover6ccc1c92017-07-04 17:36:46 +05304288 {"set_active",cmd_set_active},
Channagoud Kadabi736c4962015-08-21 11:56:52 -07004289#if UNITTEST_FW_SUPPORT
Channagoud Kadabid5ddf482015-09-28 10:31:35 -07004290 {"oem run-tests", cmd_oem_runtests},
Channagoud Kadabi736c4962015-08-21 11:56:52 -07004291#endif
Sridhar Parasurame94e8152014-10-24 14:06:03 -07004292#endif
lijuang511a2b52015-08-14 20:50:51 +08004293 };
Sridhar Parasurame94e8152014-10-24 14:06:03 -07004294
4295 int fastboot_cmds_count = sizeof(cmd_list)/sizeof(cmd_list[0]);
4296 for (i = 1; i < fastboot_cmds_count; i++)
4297 fastboot_register(cmd_list[i].name,cmd_list[i].cb);
4298
Amol Jadi5edf3552013-07-23 14:15:34 -07004299 /* publish variables and their values */
4300 fastboot_publish("product", TARGET(BOARD));
4301 fastboot_publish("kernel", "lk");
4302 fastboot_publish("serialno", sn_buf);
4303
4304 /*
4305 * partition info is supported only for emmc partitions
4306 * Calling this for NAND prints some error messages which
4307 * is harmless but misleading. Avoid calling this for NAND
4308 * devices.
4309 */
4310 if (target_is_emmc_boot())
Mayank Grover7b8a8f82017-10-27 13:07:59 +05304311 publish_getvar_partition_info(part_info, partition_get_partition_count());
Amol Jadi5edf3552013-07-23 14:15:34 -07004312
Mayank Grover351a75e2017-05-30 20:06:08 +05304313 if (partition_multislot_is_supported())
4314 publish_getvar_multislot_vars();
4315
Amol Jadi5edf3552013-07-23 14:15:34 -07004316 /* Max download size supported */
Ameya Thakur11cf1a62013-08-05 12:44:48 -07004317 snprintf(max_download_size, MAX_RSP_SIZE, "\t0x%x",
4318 target_get_max_flash_size());
Amol Jadi5edf3552013-07-23 14:15:34 -07004319 fastboot_publish("max-download-size", (const char *) max_download_size);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07004320 /* Is the charger screen check enabled */
4321 snprintf(charger_screen_enabled, MAX_RSP_SIZE, "%d",
4322 device.charger_screen_enabled);
4323 fastboot_publish("charger-screen-enabled",
4324 (const char *) charger_screen_enabled);
lijuanga25d8bb2015-09-16 13:11:52 +08004325 fastboot_publish("off-mode-charge", (const char *) charger_screen_enabled);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05304326 snprintf(panel_display_mode, MAX_RSP_SIZE, "%s",
4327 device.display_panel);
4328 fastboot_publish("display-panel",
4329 (const char *) panel_display_mode);
Mayank Grover7b8a8f82017-10-27 13:07:59 +05304330#if PRODUCT_IOT
4331 get_bootloader_version_iot(&bootloader_version_string);
4332 fastboot_publish("version-bootloader", (const char *) bootloader_version_string);
4333
4334 /* Version baseband is n/a for apq iot devices */
4335 fastboot_publish("version-baseband", "N/A");
4336
4337 /* IOT targets support only mmc target */
4338 snprintf(block_size_string, MAX_RSP_SIZE, "0x%x", mmc_get_device_blocksize());
4339 fastboot_publish("erase-block-size", (const char *) block_size_string);
4340 fastboot_publish("logical-block-size", (const char *) block_size_string);
4341#else
Channagoud Kadabiaafbbfc2015-03-04 17:52:56 -08004342 fastboot_publish("version-bootloader", (const char *) device.bootloader_version);
4343 fastboot_publish("version-baseband", (const char *) device.radio_version);
Mayank Grover7b8a8f82017-10-27 13:07:59 +05304344#endif
lijuangf16461c2015-08-03 17:09:34 +08004345 fastboot_publish("secure", is_secure_boot_enable()? "yes":"no");
Mayank Grover7b8a8f82017-10-27 13:07:59 +05304346 fastboot_publish("unlocked", device.is_unlocked ? "yes":"no");
lijuangf16461c2015-08-03 17:09:34 +08004347 smem_get_hw_platform_name((unsigned char *) hw_platform_buf, sizeof(hw_platform_buf));
4348 snprintf(get_variant, MAX_RSP_SIZE, "%s %s", hw_platform_buf,
4349 target_is_emmc_boot()? "eMMC":"UFS");
4350 fastboot_publish("variant", (const char *) get_variant);
lijuang65c5a822015-08-29 16:35:36 +08004351#if CHECK_BAT_VOLTAGE
lijuang102dfa92015-10-09 18:31:03 +08004352 update_battery_status();
lijuang65c5a822015-08-29 16:35:36 +08004353 fastboot_publish("battery-voltage", (const char *) battery_voltage);
lijuang102dfa92015-10-09 18:31:03 +08004354 fastboot_publish("battery-soc-ok", (const char *) battery_soc_ok);
lijuang65c5a822015-08-29 16:35:36 +08004355#endif
Amol Jadi5edf3552013-07-23 14:15:34 -07004356}
4357
Brian Swetland9c4c0752009-01-25 16:23:50 -08004358void aboot_init(const struct app_descriptor *app)
4359{
Shashank Mittal4f99a882010-02-01 13:58:50 -08004360 unsigned reboot_mode = 0;
Mayank Grover351a75e2017-05-30 20:06:08 +05304361 int boot_err_type = 0;
4362 int boot_slot = INVALID;
Chandan Uddarajubedca152010-06-02 23:05:15 -07004363
lijuang39831732016-01-08 17:49:02 +08004364 /* Initialise wdog to catch early lk crashes */
4365#if WDOG_SUPPORT
4366 msm_wdog_init();
4367#endif
4368
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07004369 /* Setup page size information for nv storage */
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004370 if (target_is_emmc_boot())
4371 {
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07004372 page_size = mmc_page_size();
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004373 page_mask = page_size - 1;
Gaurav Nebhwani4d2389c2016-03-17 21:10:05 +05304374 mmc_blocksize = mmc_get_device_blocksize();
4375 mmc_blocksize_mask = mmc_blocksize - 1;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004376 }
4377 else
4378 {
4379 page_size = flash_page_size();
4380 page_mask = page_size - 1;
4381 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07004382 ASSERT((MEMBASE + MEMSIZE) > MEMBASE);
4383
Ameya Thakur11cf1a62013-08-05 12:44:48 -07004384 read_device_info(&device);
vijay kumarc65876c2015-04-24 13:29:16 +05304385 read_allow_oem_unlock(&device);
Shashank Mittal162244e2011-08-08 19:01:25 -07004386
Mayank Grover351a75e2017-05-30 20:06:08 +05304387 /* Detect multi-slot support */
4388 if (partition_multislot_is_supported())
4389 {
4390 boot_slot = partition_find_active_slot();
4391 if (boot_slot == INVALID)
4392 {
4393 boot_into_fastboot = true;
4394 dprintf(INFO, "Active Slot: (INVALID)\n");
4395 }
4396 else
4397 {
4398 /* Setting the state of system to boot active slot */
4399 partition_mark_active_slot(boot_slot);
4400 dprintf(INFO, "Active Slot: (%s)\n", SUFFIX_SLOT(boot_slot));
4401 }
4402 }
4403
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08004404 /* Display splash screen if enabled */
4405#if DISPLAY_SPLASH_SCREEN
lijuang99c02d82015-02-13 19:04:34 +08004406#if NO_ALARM_DISPLAY
4407 if (!check_alarm_boot()) {
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08004408#endif
lijuang99c02d82015-02-13 19:04:34 +08004409 dprintf(SPEW, "Display Init: Start\n");
Ajay Singh Parmara7865a82015-04-16 21:27:52 -07004410#if DISPLAY_HDMI_PRIMARY
4411 if (!strlen(device.display_panel))
4412 strlcpy(device.display_panel, DISPLAY_PANEL_HDMI,
4413 sizeof(device.display_panel));
4414#endif
Channagoud Kadabi90869ce2015-04-27 11:15:14 -07004415#if ENABLE_WBC
Channagoud Kadabid801ac72015-08-26 11:21:51 -07004416 /* Wait if the display shutdown is in progress */
4417 while(pm_app_display_shutdown_in_prgs());
Channagoud Kadabi90869ce2015-04-27 11:15:14 -07004418 if (!pm_appsbl_display_init_done())
4419 target_display_init(device.display_panel);
4420 else
4421 display_image_on_screen();
4422#else
lijuang99c02d82015-02-13 19:04:34 +08004423 target_display_init(device.display_panel);
Channagoud Kadabi90869ce2015-04-27 11:15:14 -07004424#endif
lijuang99c02d82015-02-13 19:04:34 +08004425 dprintf(SPEW, "Display Init: Done\n");
4426#if NO_ALARM_DISPLAY
4427 }
4428#endif
4429#endif
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08004430
Greg Griscod6250552011-06-29 14:40:23 -07004431 target_serialno((unsigned char *) sn_buf);
Ajay Dudanib06c05f2011-05-12 14:46:10 -07004432 dprintf(SPEW,"serial number: %s\n",sn_buf);
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -08004433
Dhaval Patel223ec952013-07-18 14:49:44 -07004434 memset(display_panel_buf, '\0', MAX_PANEL_BUF_SIZE);
4435
Matthew Qindefd5562014-07-11 18:02:40 +08004436 /*
4437 * Check power off reason if user force reset,
4438 * if yes phone will do normal boot.
4439 */
4440 if (is_user_force_reset())
4441 goto normal_boot;
4442
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07004443 /* Check if we should do something other than booting up */
Ameya Thakur0b9c2442013-05-10 13:22:56 -07004444 if (keys_get_state(KEY_VOLUMEUP) && keys_get_state(KEY_VOLUMEDOWN))
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07004445 {
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03004446 dprintf(ALWAYS,"dload mode key sequence detected\n");
lijuang395b5e62015-11-19 17:39:44 +08004447 reboot_device(EMERGENCY_DLOAD);
4448 dprintf(CRITICAL,"Failed to reboot into dload mode\n");
4449
Ameya Thakur0b9c2442013-05-10 13:22:56 -07004450 boot_into_fastboot = true;
4451 }
4452 if (!boot_into_fastboot)
4453 {
4454 if (keys_get_state(KEY_HOME) || keys_get_state(KEY_VOLUMEUP))
4455 boot_into_recovery = 1;
4456 if (!boot_into_recovery &&
4457 (keys_get_state(KEY_BACK) || keys_get_state(KEY_VOLUMEDOWN)))
Pavel Nedev5d91d412013-04-29 11:34:24 +03004458 boot_into_fastboot = true;
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07004459 }
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004460 #if NO_KEYPAD_DRIVER
Kinson Chik0b1c8162011-08-31 16:31:57 -07004461 if (fastboot_trigger())
Pavel Nedev5d91d412013-04-29 11:34:24 +03004462 boot_into_fastboot = true;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004463 #endif
Chandan Uddarajubedca152010-06-02 23:05:15 -07004464
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07004465#if USE_PON_REBOOT_REG
4466 reboot_mode = check_hard_reboot_mode();
4467#else
Ajay Dudani77421292010-10-27 19:34:06 -07004468 reboot_mode = check_reboot_mode();
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07004469#endif
4470 if (reboot_mode == RECOVERY_MODE)
4471 {
Ajay Dudani77421292010-10-27 19:34:06 -07004472 boot_into_recovery = 1;
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07004473 }
4474 else if(reboot_mode == FASTBOOT_MODE)
4475 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03004476 boot_into_fastboot = true;
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07004477 }
4478 else if(reboot_mode == ALARM_BOOT)
4479 {
Matthew Qind886f3c2014-01-17 16:52:01 +08004480 boot_reason_alarm = true;
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07004481 }
Parth Dixit207573a2015-10-27 17:26:21 +05304482#if VERIFIED_BOOT
Mayank Grover912eaa62017-10-26 12:08:53 +05304483 else if (VB_M <= target_get_vb_version())
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07004484 {
Mayank Grover889be1b2017-09-12 20:12:23 +05304485 if (reboot_mode == DM_VERITY_ENFORCING)
4486 {
4487 device.verity_mode = 1;
4488 write_device_info(&device);
4489 }
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07004490#if ENABLE_VB_ATTEST
Mayank Grover889be1b2017-09-12 20:12:23 +05304491 else if (reboot_mode == DM_VERITY_EIO)
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07004492#else
Mayank Grover889be1b2017-09-12 20:12:23 +05304493 else if (reboot_mode == DM_VERITY_LOGGING)
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07004494#endif
Mayank Grover889be1b2017-09-12 20:12:23 +05304495 {
4496 device.verity_mode = 0;
4497 write_device_info(&device);
4498 }
4499 else if (reboot_mode == DM_VERITY_KEYSCLEAR)
4500 {
4501 if(send_delete_keys_to_tz())
4502 ASSERT(0);
4503 }
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07004504 }
Parth Dixitddbc7352015-10-18 03:13:31 +05304505#endif
Ajay Dudani77421292010-10-27 19:34:06 -07004506
Matthew Qindefd5562014-07-11 18:02:40 +08004507normal_boot:
Pavel Nedev5d91d412013-04-29 11:34:24 +03004508 if (!boot_into_fastboot)
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004509 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03004510 if (target_is_emmc_boot())
Shashank Mittala0032282011-08-26 14:50:11 -07004511 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03004512 if(emmc_recovery_init())
4513 dprintf(ALWAYS,"error in emmc_recovery_init\n");
4514 if(target_use_signed_kernel())
Shashank Mittala0032282011-08-26 14:50:11 -07004515 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03004516 if((device.is_unlocked) || (device.is_tampered))
4517 {
4518 #ifdef TZ_TAMPER_FUSE
4519 set_tamper_fuse_cmd();
4520 #endif
4521 #if USE_PCOM_SECBOOT
4522 set_tamper_flag(device.is_tampered);
4523 #endif
4524 }
Shashank Mittala0032282011-08-26 14:50:11 -07004525 }
Amit Blay6281ebc2015-01-11 14:44:08 +02004526
Mayank Grover351a75e2017-05-30 20:06:08 +05304527retry_boot:
4528 /* Trying to boot active partition */
4529 if (partition_multislot_is_supported())
4530 {
4531 boot_slot = partition_find_boot_slot();
4532 partition_mark_active_slot(boot_slot);
4533 if (boot_slot == INVALID)
4534 goto fastboot;
4535 }
4536
4537 boot_err_type = boot_linux_from_mmc();
4538 switch (boot_err_type)
4539 {
4540 case ERR_INVALID_PAGE_SIZE:
4541 case ERR_DT_PARSE:
4542 case ERR_ABOOT_ADDR_OVERLAP:
Mayank Grover10cabbe2017-10-30 19:11:05 +05304543 case ERR_INVALID_BOOT_MAGIC:
Mayank Grover351a75e2017-05-30 20:06:08 +05304544 if(partition_multislot_is_supported())
Mayank Grover10cabbe2017-10-30 19:11:05 +05304545 {
4546 /*
4547 * Deactivate current slot, as it failed to
4548 * boot, and retry next slot.
4549 */
4550 partition_deactivate_slot(boot_slot);
Mayank Grover351a75e2017-05-30 20:06:08 +05304551 goto retry_boot;
Mayank Grover10cabbe2017-10-30 19:11:05 +05304552 }
Mayank Grover351a75e2017-05-30 20:06:08 +05304553 else
4554 break;
Mayank Grover351a75e2017-05-30 20:06:08 +05304555 default:
4556 break;
4557 /* going to fastboot menu */
4558 }
Shashank Mittala0032282011-08-26 14:50:11 -07004559 }
Pavel Nedev5d91d412013-04-29 11:34:24 +03004560 else
4561 {
4562 recovery_init();
4563 #if USE_PCOM_SECBOOT
4564 if((device.is_unlocked) || (device.is_tampered))
4565 set_tamper_flag(device.is_tampered);
4566 #endif
4567 boot_linux_from_flash();
4568 }
4569 dprintf(CRITICAL, "ERROR: Could not do normal boot. Reverting "
4570 "to fastboot mode.\n");
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004571 }
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07004572
Mayank Grover351a75e2017-05-30 20:06:08 +05304573fastboot:
Amol Jadi5edf3552013-07-23 14:15:34 -07004574 /* We are here means regular boot did not happen. Start fastboot. */
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07004575
Amol Jadi5edf3552013-07-23 14:15:34 -07004576 /* register aboot specific fastboot commands */
4577 aboot_fastboot_register_commands();
Amol Jadi57abe4c2011-05-24 15:47:27 -07004578
Amol Jadi5edf3552013-07-23 14:15:34 -07004579 /* dump partition table for debug info */
Kinson Chikf1a43512011-07-14 11:28:39 -07004580 partition_dump();
Amol Jadi5edf3552013-07-23 14:15:34 -07004581
4582 /* initialize and start fastboot */
4583 fastboot_init(target_get_scratch_address(), target_get_max_flash_size());
lijuang4ece1e72015-08-14 21:02:36 +08004584#if FBCON_DISPLAY_MSG
lijuang9a7d3b92015-11-30 14:41:24 +08004585 display_fastboot_menu();
lijuang4ece1e72015-08-14 21:02:36 +08004586#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08004587}
4588
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07004589uint32_t get_page_size()
4590{
4591 return page_size;
4592}
4593
Amir Samuelov57a6fa22013-06-05 16:36:43 +03004594/*
4595 * Calculated and save hash (SHA256) for non-signed boot image.
4596 *
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07004597 * @param image_addr - Boot image address
4598 * @param image_size - Size of the boot image
Amir Samuelov57a6fa22013-06-05 16:36:43 +03004599 *
4600 * @return int - 0 on success, negative value on failure.
4601 */
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07004602static int aboot_save_boot_hash_mmc(uint32_t image_addr, uint32_t image_size)
Amir Samuelov57a6fa22013-06-05 16:36:43 +03004603{
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07004604 unsigned int digest[8];
4605#if IMAGE_VERIF_ALGO_SHA1
4606 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA1;
4607#else
4608 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA256;
4609#endif
Amir Samuelov57a6fa22013-06-05 16:36:43 +03004610
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07004611 target_crypto_init_params();
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -08004612 hash_find((unsigned char *) image_addr, image_size, (unsigned char *)&digest, auth_algo);
Amir Samuelov57a6fa22013-06-05 16:36:43 +03004613
4614 save_kernel_hash_cmd(digest);
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07004615 dprintf(INFO, "aboot_save_boot_hash_mmc: imagesize_actual size %d bytes.\n", (int) image_size);
Amir Samuelov57a6fa22013-06-05 16:36:43 +03004616
4617 return 0;
4618}
4619
Mayank Grover351a75e2017-05-30 20:06:08 +05304620
Brian Swetland9c4c0752009-01-25 16:23:50 -08004621APP_START(aboot)
4622 .init = aboot_init,
4623APP_END