blob: e286518ddb71d478b6104be528b040173e1c03ed [file] [log] [blame]
Brian Swetland9c4c0752009-01-25 16:23:50 -08001/*
2 * Copyright (c) 2009, Google Inc.
3 * All rights reserved.
4 *
Mayank Grover11ff9692018-01-11 11:54:49 +05305 * Copyright (c) 2009-2018, The Linux Foundation. All rights reserved.
Brian Swetland9c4c0752009-01-25 16:23:50 -08006 *
Chandan Uddaraju5fa471a2009-12-02 17:31:34 -08007 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070014 * * Neither the name of The Linux Foundation nor
Chandan Uddaraju5fa471a2009-12-02 17:31:34 -080015 * the names of its contributors may be used to endorse or promote
16 * products derived from this software without specific prior written
17 * permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22 * NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 *
Brian Swetland9c4c0752009-01-25 16:23:50 -080031 */
32
33#include <app.h>
34#include <debug.h>
35#include <arch/arm.h>
Brian Swetland9c4c0752009-01-25 16:23:50 -080036#include <string.h>
Channagoud Kadabi132ff552013-04-19 14:34:44 -070037#include <stdlib.h>
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -070038#include <limits.h>
Brian Swetland9c4c0752009-01-25 16:23:50 -080039#include <kernel/thread.h>
40#include <arch/ops.h>
41
Dima Zavin214cc642009-01-26 11:16:21 -080042#include <dev/flash.h>
Tanya Brokhman1c94f1a2015-02-15 09:05:03 +020043#include <dev/flash-ubi.h>
Dima Zavin214cc642009-01-26 11:16:21 -080044#include <lib/ptable.h>
Dima Zavinb4283602009-01-26 16:36:57 -080045#include <dev/keys.h>
Shashank Mittal4f99a882010-02-01 13:58:50 -080046#include <dev/fbcon.h>
Ajay Dudanid04110c2011-01-17 23:55:07 -080047#include <baseband.h>
Greg Griscod6250552011-06-29 14:40:23 -070048#include <target.h>
49#include <mmc.h>
Kinson Chikf1a43512011-07-14 11:28:39 -070050#include <partition_parser.h>
Mayank Grover351a75e2017-05-30 20:06:08 +053051#include <ab_partition_parser.h>
Greg Griscod6250552011-06-29 14:40:23 -070052#include <platform.h>
Shashank Mittalcd98d472011-08-02 14:29:24 -070053#include <crypto_hash.h>
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070054#include <malloc.h>
Amol Jadi492d5a52013-03-15 16:12:34 -070055#include <boot_stats.h>
Amir Samuelov57a6fa22013-06-05 16:36:43 +030056#include <sha.h>
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -070057#include <platform/iomap.h>
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -070058#include <boot_device.h>
Shashank Mittald3e54dd2014-08-28 15:24:02 -070059#include <boot_verifier.h>
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +053060#include <image_verify.h>
Matthew Qinbb7923d2015-02-09 10:56:09 +080061#include <decompress.h>
Unnati Gandhi17b3bfc2015-05-11 12:58:16 +053062#include <platform/timer.h>
lijuang511a2b52015-08-14 20:50:51 +080063#include <sys/types.h>
Channagoud Kadabi036c6052015-02-09 15:19:59 -080064#if USE_RPMB_FOR_DEVINFO
65#include <rpmb.h>
66#endif
Dima Zavin214cc642009-01-26 11:16:21 -080067
Channagoud Kadabi90869ce2015-04-27 11:15:14 -070068#if ENABLE_WBC
69#include <pm_app_smbchg.h>
70#endif
71
Neeti Desai17379b82012-06-04 18:42:53 -070072#if DEVICE_TREE
73#include <libfdt.h>
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070074#include <dev_tree.h>
Neeti Desai17379b82012-06-04 18:42:53 -070075#endif
76
Aparna Mallavarapu118ccae2015-06-03 13:47:11 +053077#if WDOG_SUPPORT
78#include <wdog.h>
79#endif
80
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -070081#include <reboot.h>
Shashank Mittalcd98d472011-08-02 14:29:24 -070082#include "image_verify.h"
Shashank Mittal024c0332010-02-03 11:44:00 -080083#include "recovery.h"
Brian Swetland9c4c0752009-01-25 16:23:50 -080084#include "bootimg.h"
85#include "fastboot.h"
Ajay Dudani5c761132011-04-07 20:19:04 -070086#include "sparse_format.h"
Ajay Dudanide984792015-03-02 09:57:41 -080087#include "meta_format.h"
Greg Grisco6e754772011-06-23 12:19:39 -070088#include "mmc.h"
Shashank Mittal162244e2011-08-08 19:01:25 -070089#include "devinfo.h"
Neeti Desai465491e2012-07-31 12:53:35 -070090#include "board.h"
Shashank Mittal162244e2011-08-08 19:01:25 -070091#include "scm.h"
Amit Blay6281ebc2015-01-11 14:44:08 +020092#include "mdtp.h"
Sridhar Parasuram32b30662015-07-10 13:33:22 -070093#include "secapp_loader.h"
lijuanga40d6302015-07-20 20:10:13 +080094#include <menu_keys_detect.h>
95#include <display_menu.h>
Channagoud Kadabi736c4962015-08-21 11:56:52 -070096#include "fastboot_test.h"
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -070097
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070098extern bool target_use_signed_kernel(void);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070099extern void platform_uninit(void);
Channagoud Kadabi33defe22013-06-18 18:35:40 -0700100extern void target_uninit(void);
Joonwoo Park61112782013-10-02 19:50:39 -0700101extern int get_target_boot_params(const char *cmdline, const char *part,
vijay kumar870515d2015-08-31 16:37:24 +0530102 char **buf);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700103
Sridhar Parasuram7e16d172015-07-05 11:35:23 -0700104void *info_buf;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700105void write_device_info_mmc(device_info *dev);
106void write_device_info_flash(device_info *dev);
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -0700107static int aboot_save_boot_hash_mmc(uint32_t image_addr, uint32_t image_size);
Channagoud Kadabiad259832015-05-29 11:14:17 -0700108static int aboot_frp_unlock(char *pname, void *data, unsigned sz);
Kishor PK38ed93d2017-04-25 14:19:26 +0530109static inline uint64_t validate_partition_size();
Parth Dixit54ac3bb2017-03-07 15:52:48 +0530110bool pwr_key_is_pressed = false;
Hareesh Gauthamabf1e1e2017-04-12 18:09:24 +0530111static bool is_systemd_present=false;
Mayank Grover351a75e2017-05-30 20:06:08 +0530112static void publish_getvar_multislot_vars();
Sridhar Parasurame94e8152014-10-24 14:06:03 -0700113/* fastboot command function pointer */
114typedef void (*fastboot_cmd_fn) (const char *, void *, unsigned);
115
116struct fastboot_cmd_desc {
117 char * name;
118 fastboot_cmd_fn cb;
119};
120
Subbaraman Narayanamurthyeb92bcc2010-07-20 14:32:46 -0700121#define EXPAND(NAME) #NAME
122#define TARGET(NAME) EXPAND(NAME)
Brian Swetland2defe162009-08-18 14:35:59 -0700123
Ajay Singh Parmara7865a82015-04-16 21:27:52 -0700124#define DISPLAY_PANEL_HDMI "hdmi"
125
Ajay Dudanicd01f9b2010-02-23 21:13:04 -0800126#ifdef MEMBASE
127#define EMMC_BOOT_IMG_HEADER_ADDR (0xFF000+(MEMBASE))
128#else
David Ng183a7422009-12-07 14:55:21 -0800129#define EMMC_BOOT_IMG_HEADER_ADDR 0xFF000
Ajay Dudanicd01f9b2010-02-23 21:13:04 -0800130#endif
131
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700132#ifndef MEMSIZE
133#define MEMSIZE 1024*1024
134#endif
135
136#define MAX_TAGS_SIZE 1024
137
Kun Liang2f1601a2013-08-12 16:29:54 +0800138/* make 4096 as default size to ensure EFS,EXT4's erasing */
139#define DEFAULT_ERASE_SIZE 4096
Ujwal Patelc0b0a252015-08-16 14:05:35 -0700140#define MAX_PANEL_BUF_SIZE 196
Vijay Kumar Pendoti0b21f462016-05-02 17:09:18 +0530141#define FOOTER_SIZE 16384
Kun Liang2f1601a2013-08-12 16:29:54 +0800142
Dhaval Patelf83d73b2014-06-23 16:24:37 -0700143#define DISPLAY_DEFAULT_PREFIX "mdss_mdp"
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700144#define BOOT_DEV_MAX_LEN 64
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -0800145
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800146#define IS_ARM64(ptr) (ptr->magic_64 == KERNEL64_HDR_MAGIC) ? true : false
147
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -0700148#define ADD_OF(a, b) (UINT_MAX - b > a) ? (a + b) : UINT_MAX
149
Ameya Thakur10a33452016-06-13 14:24:26 -0700150//Size of the header that is used in case the boot image has
151//a uncompressed kernel + appended dtb
152#define PATCHED_KERNEL_HEADER_SIZE 20
153
154//String used to determine if the boot image has
155//a uncompressed kernel + appended dtb
156#define PATCHED_KERNEL_MAGIC "UNCOMPRESSED_IMG"
157
Sridhar Parasuram7bd4aaf2015-02-12 11:14:38 -0800158#if USE_BOOTDEV_CMDLINE
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700159static const char *emmc_cmdline = " androidboot.bootdevice=";
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700160#else
David Ng183a7422009-12-07 14:55:21 -0800161static const char *emmc_cmdline = " androidboot.emmc=true";
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700162#endif
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800163static const char *usb_sn_cmdline = " androidboot.serialno=";
Pavel Nedev328ac822013-04-05 15:25:11 +0300164static const char *androidboot_mode = " androidboot.mode=";
Hareesh Gauthamabf1e1e2017-04-12 18:09:24 +0530165
166static const char *systemd_ffbm_mode = " systemd.unit=ffbm.target";
Matthew Qind886f3c2014-01-17 16:52:01 +0800167static const char *alarmboot_cmdline = " androidboot.alarmboot=true";
Pavel Nedev898298c2013-02-27 12:36:09 -0800168static const char *loglevel = " quiet";
Ajay Dudanica3a33c2011-11-18 08:31:40 -0800169static const char *battchg_pause = " androidboot.mode=charger";
Shashank Mittalcd98d472011-08-02 14:29:24 -0700170static const char *auth_kernel = " androidboot.authorized_kernel=true";
Pavel Nedev5614d222013-06-17 18:01:02 +0300171static const char *secondary_gpt_enable = " gpt";
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200172static const char *mdtp_activated_flag = " mdtp";
David Ng183a7422009-12-07 14:55:21 -0800173
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800174static const char *baseband_apq = " androidboot.baseband=apq";
175static const char *baseband_msm = " androidboot.baseband=msm";
176static const char *baseband_csfb = " androidboot.baseband=csfb";
177static const char *baseband_svlte2a = " androidboot.baseband=svlte2a";
Ajay Dudani403bc492011-09-30 16:17:21 -0700178static const char *baseband_mdm = " androidboot.baseband=mdm";
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800179static const char *baseband_mdm2 = " androidboot.baseband=mdm2";
Amol Jadi5c61a952012-05-04 17:05:35 -0700180static const char *baseband_sglte = " androidboot.baseband=sglte";
Amol Jadi2a15a272013-01-22 12:03:36 -0800181static const char *baseband_dsda = " androidboot.baseband=dsda";
182static const char *baseband_dsda2 = " androidboot.baseband=dsda2";
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800183static const char *baseband_sglte2 = " androidboot.baseband=sglte2";
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800184static const char *warmboot_cmdline = " qpnp-power-on.warm_boot=1";
Vijay Kumar Pendotib228cfc2016-06-13 20:15:23 +0530185static const char *baseband_apq_nowgr = " androidboot.baseband=baseband_apq_nowgr";
Mayank Grover351a75e2017-05-30 20:06:08 +0530186static const char *androidboot_slot_suffix = " androidboot.slot_suffix=";
187static const char *skip_ramfs = " skip_initramfs";
Mayank Grover3804be72017-06-22 11:59:23 +0530188static const char *sys_path_cmdline = " rootwait ro init=/init";
189static const char *sys_path = " root=/dev/mmcblk0p";
Ajay Dudanid04110c2011-01-17 23:55:07 -0800190
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700191#if VERIFIED_BOOT
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700192static const char *verity_mode = " androidboot.veritymode=";
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700193static const char *verified_state= " androidboot.verifiedbootstate=";
Parth Dixita5715a02015-10-29 12:25:10 +0530194static const char *keymaster_v1= " androidboot.keymaster=1";
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700195//indexed based on enum values, green is 0 by default
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700196
197struct verified_boot_verity_mode vbvm[] =
198{
Channagoud Kadabi86b0c112016-03-16 19:23:16 -0700199#if ENABLE_VB_ATTEST
200 {false, "eio"},
201#else
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700202 {false, "logging"},
Channagoud Kadabi86b0c112016-03-16 19:23:16 -0700203#endif
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700204 {true, "enforcing"},
205};
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700206struct verified_boot_state_name vbsn[] =
207{
208 {GREEN, "green"},
209 {ORANGE, "orange"},
210 {YELLOW,"yellow"},
211 {RED,"red" },
212};
213#endif
Channagoud Kadabi86b0c112016-03-16 19:23:16 -0700214/*As per spec delay wait time before shutdown in Red state*/
215#define DELAY_WAIT 30000
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700216static unsigned page_size = 0;
217static unsigned page_mask = 0;
Gaurav Nebhwani4d2389c2016-03-17 21:10:05 +0530218static unsigned mmc_blocksize = 0;
219static unsigned mmc_blocksize_mask = 0;
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700220static char ffbm_mode_string[FFBM_MODE_BUF_SIZE];
221static bool boot_into_ffbm;
vijay kumar870515d2015-08-31 16:37:24 +0530222static char *target_boot_params = NULL;
Matthew Qind886f3c2014-01-17 16:52:01 +0800223static bool boot_reason_alarm;
Channagoud Kadabi80a182b2015-03-11 17:04:23 -0700224static bool devinfo_present = true;
Channagoud Kadabi736c4962015-08-21 11:56:52 -0700225bool boot_into_fastboot = false;
Parth Dixit4097b622016-03-15 14:42:27 +0530226static uint32_t dt_size = 0;
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700227
Shashank Mittalcd98d472011-08-02 14:29:24 -0700228/* Assuming unauthorized kernel image by default */
229static int auth_kernel_img = 0;
lijuang511a2b52015-08-14 20:50:51 +0800230static device_info device = {DEVICE_MAGIC, 0, 0, 0, 0, {0}, {0},{0}, 1};
vijay kumarc65876c2015-04-24 13:29:16 +0530231static bool is_allow_unlock = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -0700232
vijay kumarca2e6812015-07-08 20:28:25 +0530233static char frp_ptns[2][8] = {"config","frp"};
234
lijuang511a2b52015-08-14 20:50:51 +0800235static const char *critical_flash_allowed_ptn[] = {
236 "aboot",
237 "rpm",
238 "tz",
239 "sbl",
240 "sdi",
241 "sbl1",
242 "xbl",
243 "hyp",
244 "pmic",
245 "bootloader",
246 "devinfo",
247 "partition"};
248
Dima Zavin42168f22009-01-30 11:52:22 -0800249struct atag_ptbl_entry
250{
251 char name[16];
252 unsigned offset;
253 unsigned size;
254 unsigned flags;
255};
256
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700257/*
258 * Partition info, required to be published
259 * for fastboot
260 */
261struct getvar_partition_info {
Mayank Grover7b8a8f82017-10-27 13:07:59 +0530262 char part_name[MAX_GPT_NAME_SIZE]; /* Partition name */
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700263 char getvar_size[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for size */
264 char getvar_type[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for type */
265 char size_response[MAX_RSP_SIZE]; /* fastboot response for size */
266 char type_response[MAX_RSP_SIZE]; /* fastboot response for type */
267};
268
269/*
Mayank Grover7b8a8f82017-10-27 13:07:59 +0530270 * Update the part_type_known for known paritions types.
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700271 */
Mayank Grover7b8a8f82017-10-27 13:07:59 +0530272struct getvar_partition_info part_info[NUM_PARTITIONS];
273struct getvar_partition_info part_type_known[] =
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700274{
275 { "system" , "partition-size:", "partition-type:", "", "ext4" },
276 { "userdata", "partition-size:", "partition-type:", "", "ext4" },
277 { "cache" , "partition-size:", "partition-type:", "", "ext4" },
278};
279
280char max_download_size[MAX_RSP_SIZE];
Ameya Thakur11cf1a62013-08-05 12:44:48 -0700281char charger_screen_enabled[MAX_RSP_SIZE];
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800282char sn_buf[13];
Dhaval Patel223ec952013-07-18 14:49:44 -0700283char display_panel_buf[MAX_PANEL_BUF_SIZE];
Unnati Gandhi62c8ab82014-01-24 11:01:01 +0530284char panel_display_mode[MAX_RSP_SIZE];
Mayank Grover7b8a8f82017-10-27 13:07:59 +0530285#if PRODUCT_IOT
286char block_size_string[MAX_RSP_SIZE];
287
288/* For IOT we are using custom version */
289#define PRODUCT_IOT_VERSION "IOT001"
290char bootloader_version_string[MAX_RSP_SIZE];
291#endif
lijuang102dfa92015-10-09 18:31:03 +0800292
293#if CHECK_BAT_VOLTAGE
lijuang65c5a822015-08-29 16:35:36 +0800294char battery_voltage[MAX_RSP_SIZE];
lijuang102dfa92015-10-09 18:31:03 +0800295char battery_soc_ok [MAX_RSP_SIZE];
296#endif
297
lijuangf16461c2015-08-03 17:09:34 +0800298char get_variant[MAX_RSP_SIZE];
Greg Griscod6250552011-06-29 14:40:23 -0700299
Greg Griscod2471ef2011-07-14 13:00:42 -0700300extern int emmc_recovery_init(void);
301
Kinson Chik0b1c8162011-08-31 16:31:57 -0700302#if NO_KEYPAD_DRIVER
303extern int fastboot_trigger(void);
304#endif
Greg Griscod2471ef2011-07-14 13:00:42 -0700305
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800306static void update_ker_tags_rdisk_addr(struct boot_img_hdr *hdr, bool is_arm64)
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700307{
308 /* overwrite the destination of specified for the project */
Channagoud Kadabi7042fa32013-04-26 16:44:14 -0700309#ifdef ABOOT_IGNORE_BOOT_HEADER_ADDRS
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800310 if (is_arm64)
311 hdr->kernel_addr = ABOOT_FORCE_KERNEL64_ADDR;
312 else
313 hdr->kernel_addr = ABOOT_FORCE_KERNEL_ADDR;
Channagoud Kadabi7042fa32013-04-26 16:44:14 -0700314 hdr->ramdisk_addr = ABOOT_FORCE_RAMDISK_ADDR;
315 hdr->tags_addr = ABOOT_FORCE_TAGS_ADDR;
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700316#endif
317}
318
Dima Zavin42168f22009-01-30 11:52:22 -0800319static void ptentry_to_tag(unsigned **ptr, struct ptentry *ptn)
320{
321 struct atag_ptbl_entry atag_ptn;
322
323 memcpy(atag_ptn.name, ptn->name, 16);
324 atag_ptn.name[15] = '\0';
325 atag_ptn.offset = ptn->start;
326 atag_ptn.size = ptn->length;
327 atag_ptn.flags = ptn->flags;
328 memcpy(*ptr, &atag_ptn, sizeof(struct atag_ptbl_entry));
329 *ptr += sizeof(struct atag_ptbl_entry) / sizeof(unsigned);
330}
Brian Swetland9c4c0752009-01-25 16:23:50 -0800331
lijuang102dfa92015-10-09 18:31:03 +0800332#if CHECK_BAT_VOLTAGE
333void update_battery_status(void)
334{
335 snprintf(battery_voltage,MAX_RSP_SIZE, "%d",target_get_battery_voltage());
336 snprintf(battery_soc_ok ,MAX_RSP_SIZE, "%s",target_battery_soc_ok()? "yes":"no");
337}
338#endif
339
Neeti Desaie245d492012-06-01 12:52:13 -0700340unsigned char *update_cmdline(const char * cmdline)
Brian Swetland9c4c0752009-01-25 16:23:50 -0800341{
David Ng183a7422009-12-07 14:55:21 -0800342 int cmdline_len = 0;
343 int have_cmdline = 0;
Amol Jadi168b7712012-03-06 16:15:00 -0800344 unsigned char *cmdline_final = NULL;
Neeti Desaie245d492012-06-01 12:52:13 -0700345 int pause_at_bootup = 0;
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800346 bool warm_boot = false;
Pavel Nedev5614d222013-06-17 18:01:02 +0300347 bool gpt_exists = partition_gpt_exists();
Joonwoo Park61112782013-10-02 19:50:39 -0700348 int have_target_boot_params = 0;
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700349 char *boot_dev_buf = NULL;
Mayank Grover351a75e2017-05-30 20:06:08 +0530350 bool is_mdtp_activated = 0;
351 int current_active_slot = INVALID;
Mayank Grover3804be72017-06-22 11:59:23 +0530352 int system_ptn_index = -1;
353 unsigned int lun = 0;
354 char lun_char_base = 'a';
355 int syspath_buflen = strlen(sys_path) + sizeof(int) + 1; /*allocate buflen for largest possible string*/
356 char syspath_buf[syspath_buflen];
Mayank Grover889be1b2017-09-12 20:12:23 +0530357#if VERIFIED_BOOT
358 uint32_t boot_state = RED;
359#endif
Hareesh Gauthamabf1e1e2017-04-12 18:09:24 +0530360
361#if USE_LE_SYSTEMD
362 is_systemd_present=true;
363#endif
364
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700365#if VERIFIED_BOOT
Mayank Grover912eaa62017-10-26 12:08:53 +0530366 if (VB_M <= target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +0530367 {
368 boot_state = boot_verify_get_state();
369 }
Parth Dixitddbc7352015-10-18 03:13:31 +0530370#endif
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700371
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200372#ifdef MDTP_SUPPORT
373 mdtp_activated(&is_mdtp_activated);
374#endif /* MDTP_SUPPORT */
Dima Zavin42168f22009-01-30 11:52:22 -0800375
Brian Swetland9c4c0752009-01-25 16:23:50 -0800376 if (cmdline && cmdline[0]) {
David Ng183a7422009-12-07 14:55:21 -0800377 cmdline_len = strlen(cmdline);
378 have_cmdline = 1;
379 }
380 if (target_is_emmc_boot()) {
381 cmdline_len += strlen(emmc_cmdline);
Sridhar Parasuram7bd4aaf2015-02-12 11:14:38 -0800382#if USE_BOOTDEV_CMDLINE
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700383 boot_dev_buf = (char *) malloc(sizeof(char) * BOOT_DEV_MAX_LEN);
384 ASSERT(boot_dev_buf);
385 platform_boot_dev_cmdline(boot_dev_buf);
386 cmdline_len += strlen(boot_dev_buf);
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700387#endif
David Ng183a7422009-12-07 14:55:21 -0800388 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800389
390 cmdline_len += strlen(usb_sn_cmdline);
391 cmdline_len += strlen(sn_buf);
392
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700393#if VERIFIED_BOOT
Mayank Grover912eaa62017-10-26 12:08:53 +0530394 if (VB_M <= target_get_vb_version())
Sridhar Parasuram7d8bb9a2015-09-23 18:21:19 -0700395 {
Mayank Grover889be1b2017-09-12 20:12:23 +0530396 cmdline_len += strlen(verified_state) + strlen(vbsn[boot_state].name);
397 if ((device.verity_mode != 0 ) && (device.verity_mode != 1))
398 {
399 dprintf(CRITICAL, "Devinfo paritition possibly corrupted!!!. Please erase devinfo partition to continue booting\n");
400 ASSERT(0);
401 }
402 cmdline_len += strlen(verity_mode) + strlen(vbvm[device.verity_mode].name);
403 cmdline_len += strlen(keymaster_v1);
Sridhar Parasuram7d8bb9a2015-09-23 18:21:19 -0700404 }
Parth Dixitddbc7352015-10-18 03:13:31 +0530405#endif
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700406
Pavel Nedev5614d222013-06-17 18:01:02 +0300407 if (boot_into_recovery && gpt_exists)
408 cmdline_len += strlen(secondary_gpt_enable);
409
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200410 if(is_mdtp_activated)
411 cmdline_len += strlen(mdtp_activated_flag);
412
Pavel Nedev328ac822013-04-05 15:25:11 +0300413 if (boot_into_ffbm) {
414 cmdline_len += strlen(androidboot_mode);
Hareesh Gauthamabf1e1e2017-04-12 18:09:24 +0530415
416 if(is_systemd_present)
417 cmdline_len += strlen(systemd_ffbm_mode);
418
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700419 cmdline_len += strlen(ffbm_mode_string);
Pavel Nedev898298c2013-02-27 12:36:09 -0800420 /* reduce kernel console messages to speed-up boot */
421 cmdline_len += strlen(loglevel);
Matthew Qind886f3c2014-01-17 16:52:01 +0800422 } else if (boot_reason_alarm) {
423 cmdline_len += strlen(alarmboot_cmdline);
Zhenhua Huang431dafa2015-06-30 16:13:37 +0800424 } else if ((target_build_variant_user() || device.charger_screen_enabled)
425 && target_pause_for_battery_charge()) {
David Ngf773dde2010-07-26 19:55:08 -0700426 pause_at_bootup = 1;
427 cmdline_len += strlen(battchg_pause);
428 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800429
Shashank Mittalcd98d472011-08-02 14:29:24 -0700430 if(target_use_signed_kernel() && auth_kernel_img) {
431 cmdline_len += strlen(auth_kernel);
432 }
433
Joonwoo Park61112782013-10-02 19:50:39 -0700434 if (get_target_boot_params(cmdline, boot_into_recovery ? "recoveryfs" :
435 "system",
vijay kumar870515d2015-08-31 16:37:24 +0530436 &target_boot_params) == 0) {
Joonwoo Park61112782013-10-02 19:50:39 -0700437 have_target_boot_params = 1;
438 cmdline_len += strlen(target_boot_params);
439 }
440
Ajay Dudanid04110c2011-01-17 23:55:07 -0800441 /* Determine correct androidboot.baseband to use */
442 switch(target_baseband())
443 {
444 case BASEBAND_APQ:
445 cmdline_len += strlen(baseband_apq);
446 break;
447
448 case BASEBAND_MSM:
449 cmdline_len += strlen(baseband_msm);
450 break;
451
452 case BASEBAND_CSFB:
453 cmdline_len += strlen(baseband_csfb);
454 break;
455
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800456 case BASEBAND_SVLTE2A:
457 cmdline_len += strlen(baseband_svlte2a);
Ajay Dudanid04110c2011-01-17 23:55:07 -0800458 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700459
460 case BASEBAND_MDM:
461 cmdline_len += strlen(baseband_mdm);
462 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700463
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800464 case BASEBAND_MDM2:
465 cmdline_len += strlen(baseband_mdm2);
466 break;
467
Amol Jadi5c61a952012-05-04 17:05:35 -0700468 case BASEBAND_SGLTE:
469 cmdline_len += strlen(baseband_sglte);
470 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530471
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800472 case BASEBAND_SGLTE2:
473 cmdline_len += strlen(baseband_sglte2);
474 break;
475
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530476 case BASEBAND_DSDA:
477 cmdline_len += strlen(baseband_dsda);
478 break;
Amol Jadi2a15a272013-01-22 12:03:36 -0800479
480 case BASEBAND_DSDA2:
481 cmdline_len += strlen(baseband_dsda2);
482 break;
Vijay Kumar Pendotib228cfc2016-06-13 20:15:23 +0530483 case BASEBAND_APQ_NOWGR:
484 cmdline_len += strlen(baseband_apq_nowgr);
485 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800486 }
487
Vladislav Levenetz3cd45242017-04-05 08:58:07 +0300488#if ENABLE_DISPLAY
Lijuan Gao4a5b8322014-07-24 10:38:42 +0800489 if (cmdline) {
490 if ((strstr(cmdline, DISPLAY_DEFAULT_PREFIX) == NULL) &&
Padmanabhan Komandurubccbcdc2015-06-30 16:19:24 +0530491 target_display_panel_node(display_panel_buf,
492 MAX_PANEL_BUF_SIZE) &&
Lijuan Gao4a5b8322014-07-24 10:38:42 +0800493 strlen(display_panel_buf)) {
494 cmdline_len += strlen(display_panel_buf);
495 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700496 }
Vladislav Levenetz3cd45242017-04-05 08:58:07 +0300497#endif
Dhaval Patel223ec952013-07-18 14:49:44 -0700498
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800499 if (target_warm_boot()) {
500 warm_boot = true;
501 cmdline_len += strlen(warmboot_cmdline);
502 }
503
Mayank Grover351a75e2017-05-30 20:06:08 +0530504 if (partition_multislot_is_supported())
505 {
506 current_active_slot = partition_find_active_slot();
507 cmdline_len += (strlen(androidboot_slot_suffix)+
508 strlen(SUFFIX_SLOT(current_active_slot)));
509
Mayank Grover3804be72017-06-22 11:59:23 +0530510 system_ptn_index = partition_get_index("system");
511 if (platform_boot_dev_isemmc())
512 {
513 snprintf(syspath_buf, syspath_buflen, " root=/dev/mmcblk0p%d",
514 system_ptn_index + 1);
515 }
516 else
517 {
518 lun = partition_get_lun(system_ptn_index);
519 snprintf(syspath_buf, syspath_buflen, " root=/dev/sd%c%d",
520 lun_char_base + lun,
521 partition_get_index_in_lun("system", lun));
522 }
Mayank Grover351a75e2017-05-30 20:06:08 +0530523
Mayank Grover3804be72017-06-22 11:59:23 +0530524 cmdline_len += strlen(sys_path_cmdline);
525 cmdline_len += strlen(syspath_buf);
Mayank Grover351a75e2017-05-30 20:06:08 +0530526 if (!boot_into_recovery)
527 cmdline_len += strlen(skip_ramfs);
528 }
529
Channagoud Kadabi85c7ec32016-01-28 23:09:21 -0800530#if TARGET_CMDLINE_SUPPORT
531 char *target_cmdline_buf = malloc(TARGET_MAX_CMDLNBUF);
532 int target_cmd_line_len;
533 ASSERT(target_cmdline_buf);
534 target_cmd_line_len = target_update_cmdline(target_cmdline_buf);
535 cmdline_len += target_cmd_line_len;
536#endif
537
David Ng183a7422009-12-07 14:55:21 -0800538 if (cmdline_len > 0) {
539 const char *src;
Maria Yu52254c02014-07-04 16:14:54 +0800540 unsigned char *dst;
541
542 cmdline_final = (unsigned char*) malloc((cmdline_len + 4) & (~3));
543 ASSERT(cmdline_final != NULL);
vijay kumar287bb542015-09-29 13:01:52 +0530544 memset((void *)cmdline_final, 0, sizeof(*cmdline_final));
Maria Yu52254c02014-07-04 16:14:54 +0800545 dst = cmdline_final;
Neeti Desaie245d492012-06-01 12:52:13 -0700546
Amol Jadi168b7712012-03-06 16:15:00 -0800547 /* Save start ptr for debug print */
David Ng183a7422009-12-07 14:55:21 -0800548 if (have_cmdline) {
549 src = cmdline;
550 while ((*dst++ = *src++));
551 }
552 if (target_is_emmc_boot()) {
553 src = emmc_cmdline;
554 if (have_cmdline) --dst;
David Ngf773dde2010-07-26 19:55:08 -0700555 have_cmdline = 1;
556 while ((*dst++ = *src++));
Sridhar Parasuram7bd4aaf2015-02-12 11:14:38 -0800557#if USE_BOOTDEV_CMDLINE
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700558 src = boot_dev_buf;
559 if (have_cmdline) --dst;
560 while ((*dst++ = *src++));
561#endif
David Ngf773dde2010-07-26 19:55:08 -0700562 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800563
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700564#if VERIFIED_BOOT
Mayank Grover912eaa62017-10-26 12:08:53 +0530565 if (VB_M <= target_get_vb_version())
Sridhar Parasuram7d8bb9a2015-09-23 18:21:19 -0700566 {
Mayank Grover889be1b2017-09-12 20:12:23 +0530567 src = verified_state;
568 if(have_cmdline) --dst;
569 have_cmdline = 1;
570 while ((*dst++ = *src++));
571 src = vbsn[boot_state].name;
572 if(have_cmdline) --dst;
573 while ((*dst++ = *src++));
574
575 if ((device.verity_mode != 0 ) && (device.verity_mode != 1))
576 {
577 dprintf(CRITICAL, "Devinfo paritition possibly corrupted!!!. Please erase devinfo partition to continue booting\n");
578 ASSERT(0);
579 }
580 src = verity_mode;
581 if(have_cmdline) --dst;
582 while ((*dst++ = *src++));
583 src = vbvm[device.verity_mode].name;
584 if(have_cmdline) -- dst;
585 while ((*dst++ = *src++));
586 src = keymaster_v1;
587 if(have_cmdline) --dst;
588 while ((*dst++ = *src++));
Sridhar Parasuram7d8bb9a2015-09-23 18:21:19 -0700589 }
Parth Dixitddbc7352015-10-18 03:13:31 +0530590#endif
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800591 src = usb_sn_cmdline;
592 if (have_cmdline) --dst;
593 have_cmdline = 1;
594 while ((*dst++ = *src++));
595 src = sn_buf;
596 if (have_cmdline) --dst;
597 have_cmdline = 1;
598 while ((*dst++ = *src++));
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800599 if (warm_boot) {
600 if (have_cmdline) --dst;
601 src = warmboot_cmdline;
602 while ((*dst++ = *src++));
603 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800604
Pavel Nedev5614d222013-06-17 18:01:02 +0300605 if (boot_into_recovery && gpt_exists) {
606 src = secondary_gpt_enable;
607 if (have_cmdline) --dst;
608 while ((*dst++ = *src++));
609 }
610
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200611 if (is_mdtp_activated) {
612 src = mdtp_activated_flag;
613 if (have_cmdline) --dst;
614 while ((*dst++ = *src++));
615 }
616
Pavel Nedev328ac822013-04-05 15:25:11 +0300617 if (boot_into_ffbm) {
618 src = androidboot_mode;
619 if (have_cmdline) --dst;
620 while ((*dst++ = *src++));
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700621 src = ffbm_mode_string;
Pavel Nedev328ac822013-04-05 15:25:11 +0300622 if (have_cmdline) --dst;
623 while ((*dst++ = *src++));
Hareesh Gauthamabf1e1e2017-04-12 18:09:24 +0530624
625 if(is_systemd_present) {
626 src = systemd_ffbm_mode;
627 if (have_cmdline) --dst;
628 while ((*dst++ = *src++));
629 }
630
Pavel Nedev898298c2013-02-27 12:36:09 -0800631 src = loglevel;
632 if (have_cmdline) --dst;
633 while ((*dst++ = *src++));
Matthew Qind886f3c2014-01-17 16:52:01 +0800634 } else if (boot_reason_alarm) {
635 src = alarmboot_cmdline;
636 if (have_cmdline) --dst;
637 while ((*dst++ = *src++));
Pavel Nedev328ac822013-04-05 15:25:11 +0300638 } else if (pause_at_bootup) {
David Ngf773dde2010-07-26 19:55:08 -0700639 src = battchg_pause;
640 if (have_cmdline) --dst;
David Ng183a7422009-12-07 14:55:21 -0800641 while ((*dst++ = *src++));
642 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800643
Shashank Mittalcd98d472011-08-02 14:29:24 -0700644 if(target_use_signed_kernel() && auth_kernel_img) {
645 src = auth_kernel;
646 if (have_cmdline) --dst;
647 while ((*dst++ = *src++));
648 }
649
Ajay Dudanid04110c2011-01-17 23:55:07 -0800650 switch(target_baseband())
651 {
652 case BASEBAND_APQ:
653 src = baseband_apq;
654 if (have_cmdline) --dst;
655 while ((*dst++ = *src++));
656 break;
657
658 case BASEBAND_MSM:
659 src = baseband_msm;
660 if (have_cmdline) --dst;
661 while ((*dst++ = *src++));
662 break;
663
664 case BASEBAND_CSFB:
665 src = baseband_csfb;
666 if (have_cmdline) --dst;
667 while ((*dst++ = *src++));
668 break;
669
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800670 case BASEBAND_SVLTE2A:
671 src = baseband_svlte2a;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800672 if (have_cmdline) --dst;
673 while ((*dst++ = *src++));
674 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700675
676 case BASEBAND_MDM:
677 src = baseband_mdm;
678 if (have_cmdline) --dst;
679 while ((*dst++ = *src++));
680 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700681
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800682 case BASEBAND_MDM2:
683 src = baseband_mdm2;
684 if (have_cmdline) --dst;
685 while ((*dst++ = *src++));
686 break;
687
Amol Jadi5c61a952012-05-04 17:05:35 -0700688 case BASEBAND_SGLTE:
689 src = baseband_sglte;
690 if (have_cmdline) --dst;
691 while ((*dst++ = *src++));
692 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530693
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800694 case BASEBAND_SGLTE2:
695 src = baseband_sglte2;
696 if (have_cmdline) --dst;
697 while ((*dst++ = *src++));
698 break;
699
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530700 case BASEBAND_DSDA:
701 src = baseband_dsda;
702 if (have_cmdline) --dst;
703 while ((*dst++ = *src++));
704 break;
Amol Jadi2a15a272013-01-22 12:03:36 -0800705
706 case BASEBAND_DSDA2:
707 src = baseband_dsda2;
708 if (have_cmdline) --dst;
709 while ((*dst++ = *src++));
710 break;
Vijay Kumar Pendotib228cfc2016-06-13 20:15:23 +0530711 case BASEBAND_APQ_NOWGR:
712 src = baseband_apq_nowgr;
713 if (have_cmdline) --dst;
714 while ((*dst++ = *src++));
715 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800716 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700717
718 if (strlen(display_panel_buf)) {
Dhaval Patel223ec952013-07-18 14:49:44 -0700719 src = display_panel_buf;
720 if (have_cmdline) --dst;
721 while ((*dst++ = *src++));
722 }
Joonwoo Park61112782013-10-02 19:50:39 -0700723
724 if (have_target_boot_params) {
725 if (have_cmdline) --dst;
726 src = target_boot_params;
727 while ((*dst++ = *src++));
vijay kumar870515d2015-08-31 16:37:24 +0530728 free(target_boot_params);
Joonwoo Park61112782013-10-02 19:50:39 -0700729 }
Channagoud Kadabi85c7ec32016-01-28 23:09:21 -0800730
Mayank Grover351a75e2017-05-30 20:06:08 +0530731 if (partition_multislot_is_supported() && have_cmdline)
732 {
733 src = androidboot_slot_suffix;
734 --dst;
735 while ((*dst++ = *src++));
736 --dst;
737 src = SUFFIX_SLOT(current_active_slot);
738 while ((*dst++ = *src++));
739
740 if (!boot_into_recovery)
741 {
742 src = skip_ramfs;
743 --dst;
744 while ((*dst++ = *src++));
745 }
746
747 src = sys_path_cmdline;
748 --dst;
749 while ((*dst++ = *src++));
Mayank Grover3804be72017-06-22 11:59:23 +0530750
751 src = syspath_buf;
752 --dst;
753 while ((*dst++ = *src++));
Mayank Grover351a75e2017-05-30 20:06:08 +0530754 }
755
Channagoud Kadabi85c7ec32016-01-28 23:09:21 -0800756#if TARGET_CMDLINE_SUPPORT
757 if (target_cmdline_buf && target_cmd_line_len)
758 {
759 if (have_cmdline) --dst;
760 src = target_cmdline_buf;
761 while((*dst++ = *src++));
762 free(target_cmdline_buf);
763 }
764#endif
Neeti Desaie245d492012-06-01 12:52:13 -0700765 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700766
767
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700768 if (boot_dev_buf)
769 free(boot_dev_buf);
770
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -0800771 if (cmdline_final)
772 dprintf(INFO, "cmdline: %s\n", cmdline_final);
773 else
774 dprintf(INFO, "cmdline is NULL\n");
Neeti Desaie245d492012-06-01 12:52:13 -0700775 return cmdline_final;
776}
777
778unsigned *atag_core(unsigned *ptr)
779{
780 /* CORE */
781 *ptr++ = 2;
782 *ptr++ = 0x54410001;
783
784 return ptr;
785
786}
787
788unsigned *atag_ramdisk(unsigned *ptr, void *ramdisk,
789 unsigned ramdisk_size)
790{
791 if (ramdisk_size) {
792 *ptr++ = 4;
793 *ptr++ = 0x54420005;
794 *ptr++ = (unsigned)ramdisk;
795 *ptr++ = ramdisk_size;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800796 }
797
Neeti Desaie245d492012-06-01 12:52:13 -0700798 return ptr;
799}
800
801unsigned *atag_ptable(unsigned **ptr_addr)
802{
803 int i;
804 struct ptable *ptable;
805
806 if ((ptable = flash_get_ptable()) && (ptable->count != 0)) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700807 *(*ptr_addr)++ = 2 + (ptable->count * (sizeof(struct atag_ptbl_entry) /
808 sizeof(unsigned)));
Neeti Desaie245d492012-06-01 12:52:13 -0700809 *(*ptr_addr)++ = 0x4d534d70;
810 for (i = 0; i < ptable->count; ++i)
811 ptentry_to_tag(ptr_addr, ptable_get(ptable, i));
812 }
813
814 return (*ptr_addr);
815}
816
817unsigned *atag_cmdline(unsigned *ptr, const char *cmdline)
818{
819 int cmdline_length = 0;
820 int n;
Neeti Desaie245d492012-06-01 12:52:13 -0700821 char *dest;
822
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800823 cmdline_length = strlen((const char*)cmdline);
Neeti Desaie245d492012-06-01 12:52:13 -0700824 n = (cmdline_length + 4) & (~3);
825
826 *ptr++ = (n / 4) + 2;
827 *ptr++ = 0x54410009;
828 dest = (char *) ptr;
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800829 while ((*dest++ = *cmdline++));
Neeti Desaie245d492012-06-01 12:52:13 -0700830 ptr += (n / 4);
831
832 return ptr;
833}
834
835unsigned *atag_end(unsigned *ptr)
836{
Brian Swetland9c4c0752009-01-25 16:23:50 -0800837 /* END */
838 *ptr++ = 0;
839 *ptr++ = 0;
840
Neeti Desaie245d492012-06-01 12:52:13 -0700841 return ptr;
842}
843
844void generate_atags(unsigned *ptr, const char *cmdline,
845 void *ramdisk, unsigned ramdisk_size)
846{
vijay kumar21a37452015-12-29 16:23:21 +0530847 unsigned *orig_ptr = ptr;
Neeti Desaie245d492012-06-01 12:52:13 -0700848 ptr = atag_core(ptr);
849 ptr = atag_ramdisk(ptr, ramdisk, ramdisk_size);
850 ptr = target_atag_mem(ptr);
851
852 /* Skip NAND partition ATAGS for eMMC boot */
853 if (!target_is_emmc_boot()){
854 ptr = atag_ptable(&ptr);
855 }
856
vijay kumar21a37452015-12-29 16:23:21 +0530857 /*
858 * Atags size filled till + cmdline size + 1 unsigned for 4-byte boundary + 4 unsigned
859 * for atag identifier in atag_cmdline and atag_end should be with in MAX_TAGS_SIZE bytes
860 */
861 if (((ptr - orig_ptr) + strlen(cmdline) + 5 * sizeof(unsigned)) < MAX_TAGS_SIZE) {
862 ptr = atag_cmdline(ptr, cmdline);
863 ptr = atag_end(ptr);
864 }
865 else {
866 dprintf(CRITICAL,"Crossing ATAGs Max size allowed\n");
867 ASSERT(0);
868 }
Neeti Desaie245d492012-06-01 12:52:13 -0700869}
870
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700871typedef void entry_func_ptr(unsigned, unsigned, unsigned*);
Neeti Desaie245d492012-06-01 12:52:13 -0700872void boot_linux(void *kernel, unsigned *tags,
873 const char *cmdline, unsigned machtype,
874 void *ramdisk, unsigned ramdisk_size)
875{
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800876 unsigned char *final_cmdline;
Amol Jadib6be5c12012-11-14 13:39:51 -0800877#if DEVICE_TREE
Neeti Desai17379b82012-06-04 18:42:53 -0700878 int ret = 0;
Amol Jadib6be5c12012-11-14 13:39:51 -0800879#endif
880
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700881 void (*entry)(unsigned, unsigned, unsigned*) = (entry_func_ptr*)(PA((addr_t)kernel));
Deepa Dinamani25a9f762012-11-30 15:57:15 -0800882 uint32_t tags_phys = PA((addr_t)tags);
vijay kumar1a50a642015-11-16 12:41:15 +0530883 struct kernel64_hdr *kptr = ((struct kernel64_hdr*)(PA((addr_t)kernel)));
Deepa Dinamani25a9f762012-11-30 15:57:15 -0800884
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530885 ramdisk = (void *)PA((addr_t)ramdisk);
Neeti Desaie245d492012-06-01 12:52:13 -0700886
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800887 final_cmdline = update_cmdline((const char*)cmdline);
888
Neeti Desai17379b82012-06-04 18:42:53 -0700889#if DEVICE_TREE
Amol Jadib6be5c12012-11-14 13:39:51 -0800890 dprintf(INFO, "Updating device tree: start\n");
891
Neeti Desai17379b82012-06-04 18:42:53 -0700892 /* Update the Device Tree */
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530893 ret = update_device_tree((void *)tags,(const char *)final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -0700894 if(ret)
895 {
896 dprintf(CRITICAL, "ERROR: Updating Device Tree Failed \n");
897 ASSERT(0);
898 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800899 dprintf(INFO, "Updating device tree: done\n");
Neeti Desai17379b82012-06-04 18:42:53 -0700900#else
Neeti Desaie245d492012-06-01 12:52:13 -0700901 /* Generating the Atags */
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800902 generate_atags(tags, final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -0700903#endif
Neeti Desaie245d492012-06-01 12:52:13 -0700904
Maria Yu52254c02014-07-04 16:14:54 +0800905 free(final_cmdline);
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -0700906
907#if VERIFIED_BOOT
Mayank Grover912eaa62017-10-26 12:08:53 +0530908 if (VB_M <= target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +0530909 {
910 if (device.verity_mode == 0) {
lijuangbdd9bb42016-03-01 18:22:17 +0800911#if FBCON_DISPLAY_MSG
Channagoud Kadabi86b0c112016-03-16 19:23:16 -0700912#if ENABLE_VB_ATTEST
Mayank Grover889be1b2017-09-12 20:12:23 +0530913 display_bootverify_menu(DISPLAY_MENU_EIO);
914 wait_for_users_action();
915 if(!pwr_key_is_pressed)
916 shutdown_device();
Channagoud Kadabi86b0c112016-03-16 19:23:16 -0700917#else
Mayank Grover889be1b2017-09-12 20:12:23 +0530918 display_bootverify_menu(DISPLAY_MENU_LOGGING);
Channagoud Kadabi86b0c112016-03-16 19:23:16 -0700919#endif
Mayank Grover889be1b2017-09-12 20:12:23 +0530920 wait_for_users_action();
lijuangbdd9bb42016-03-01 18:22:17 +0800921#else
Mayank Grover889be1b2017-09-12 20:12:23 +0530922 dprintf(CRITICAL,
923 "The dm-verity is not started in enforcing mode.\nWait for 5 seconds before proceeding\n");
924 mdelay(5000);
lijuangbdd9bb42016-03-01 18:22:17 +0800925#endif
Mayank Grover889be1b2017-09-12 20:12:23 +0530926 }
lijuangbdd9bb42016-03-01 18:22:17 +0800927 }
lijuangbdd9bb42016-03-01 18:22:17 +0800928#endif
929
930#if VERIFIED_BOOT
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -0700931 /* Write protect the device info */
Channagoud Kadabi3bd9d1e2015-05-05 16:18:20 -0700932 if (!boot_into_recovery && target_build_variant_user() && devinfo_present && mmc_write_protect("devinfo", 1))
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -0700933 {
934 dprintf(INFO, "Failed to write protect dev info\n");
935 ASSERT(0);
936 }
937#endif
938
Aravind Venkateswaran8f076242014-02-25 16:25:30 -0800939 /* Turn off splash screen if enabled */
940#if DISPLAY_SPLASH_SCREEN
941 target_display_shutdown();
942#endif
943
Veera Sundaram Sankaran67ea0932015-09-25 10:09:30 -0700944 /* Perform target specific cleanup */
945 target_uninit();
Aravind Venkateswaran8f076242014-02-25 16:25:30 -0800946
Deepa Dinamani33734bc2013-03-06 12:16:06 -0800947 dprintf(INFO, "booting linux @ %p, ramdisk @ %p (%d), tags/device tree @ %p\n",
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530948 entry, ramdisk, ramdisk_size, (void *)tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800949
950 enter_critical_section();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700951
Amol Jadi4421e652011-06-16 15:00:48 -0700952 /* do any platform specific cleanup before kernel entry */
953 platform_uninit();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700954
Brian Swetland9c4c0752009-01-25 16:23:50 -0800955 arch_disable_cache(UCACHE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700956
Amol Jadi504f9fe2012-08-16 13:56:48 -0700957#if ARM_WITH_MMU
Brian Swetland9c4c0752009-01-25 16:23:50 -0800958 arch_disable_mmu();
Amol Jadi504f9fe2012-08-16 13:56:48 -0700959#endif
Amol Jadi492d5a52013-03-15 16:12:34 -0700960 bs_set_timestamp(BS_KERNEL_ENTRY);
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800961
962 if (IS_ARM64(kptr))
963 /* Jump to a 64bit kernel */
964 scm_elexec_call((paddr_t)kernel, tags_phys);
965 else
966 /* Jump to a 32bit kernel */
967 entry(0, machtype, (unsigned*)tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800968}
969
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700970/* Function to check if the memory address range falls within the aboot
971 * boundaries.
972 * start: Start of the memory region
973 * size: Size of the memory region
974 */
Vijay Kumar Pendoti9c002ad2016-03-09 13:52:45 +0530975int check_aboot_addr_range_overlap(uintptr_t start, uint32_t size)
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700976{
977 /* Check for boundary conditions. */
Sundarajan Srinivasance2a0ea2013-12-16 17:02:56 -0800978 if ((UINT_MAX - start) < size)
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700979 return -1;
980
981 /* Check for memory overlap. */
982 if ((start < MEMBASE) && ((start + size) <= MEMBASE))
983 return 0;
Channagoud Kadabi94143912013-10-15 12:53:52 -0700984 else if (start >= (MEMBASE + MEMSIZE))
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700985 return 0;
986 else
987 return -1;
988}
989
Mayank Grovere559cec2017-10-17 15:12:03 +0530990/* Function to check if the memory address range falls beyond ddr region.
991 * start: Start of the memory region
992 * size: Size of the memory region
993 */
994int check_ddr_addr_range_bound(uintptr_t start, uint32_t size)
995{
996 uintptr_t ddr_pa_start_addr = PA(get_ddr_start());
997 uint64_t ddr_size = smem_get_ddr_size();
998 uint64_t ddr_pa_end_addr = ddr_pa_start_addr + ddr_size;
999 uintptr_t pa_start_addr = PA(start);
1000
1001 /* Check for boundary conditions. */
1002 if ((UINT_MAX - start) < size)
1003 return -1;
1004
1005 /* Check if memory range is beyond the ddr range. */
1006 if (pa_start_addr < ddr_pa_start_addr ||
1007 pa_start_addr >= (ddr_pa_end_addr) ||
1008 (pa_start_addr + size) > ddr_pa_end_addr)
1009 return -1;
1010 else
1011 return 0;
1012}
1013
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001014BUF_DMA_ALIGN(buf, BOOT_IMG_MAX_PAGE_SIZE); //Equal to max-supported pagesize
Dima Zavin214cc642009-01-26 11:16:21 -08001015
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001016static void verify_signed_bootimg(uint32_t bootimg_addr, uint32_t bootimg_size)
1017{
1018 int ret;
Channagoud Kadabia8c623f2015-01-13 14:48:48 -08001019
1020#if !VERIFIED_BOOT
Sundarajan Srinivasance54d6e2013-11-11 12:45:00 -08001021#if IMAGE_VERIF_ALGO_SHA1
1022 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA1;
1023#else
1024 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA256;
1025#endif
Channagoud Kadabia8c623f2015-01-13 14:48:48 -08001026#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001027
1028 /* Assume device is rooted at this time. */
1029 device.is_tampered = 1;
1030
1031 dprintf(INFO, "Authenticating boot image (%d): start\n", bootimg_size);
1032
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001033#if VERIFIED_BOOT
Mayank Grover68fbf0d2017-10-24 14:13:39 +05301034 char *ptn_name = NULL;
1035 if (boot_into_recovery &&
1036 (!partition_multislot_is_supported()))
1037 ptn_name = "/recovery";
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001038 else
Mayank Grover68fbf0d2017-10-24 14:13:39 +05301039 ptn_name = "/boot";
1040
1041 ret = boot_verify_image((unsigned char *)bootimg_addr,
1042 bootimg_size, ptn_name);
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001043 boot_verify_print_state();
1044#else
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001045 ret = image_verify((unsigned char *)bootimg_addr,
1046 (unsigned char *)(bootimg_addr + bootimg_size),
1047 bootimg_size,
Sundarajan Srinivasance54d6e2013-11-11 12:45:00 -08001048 auth_algo);
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001049#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001050 dprintf(INFO, "Authenticating boot image: done return value = %d\n", ret);
1051
1052 if (ret)
1053 {
1054 /* Authorized kernel */
1055 device.is_tampered = 0;
Sundarajan Srinivasan3fb21f12013-09-16 18:36:15 -07001056 auth_kernel_img = 1;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001057 }
1058
Amit Blay4aa292f2015-04-28 21:55:59 +03001059#ifdef MDTP_SUPPORT
1060 {
1061 /* Verify MDTP lock.
1062 * For boot & recovery partitions, use aboot's verification result.
1063 */
1064 mdtp_ext_partition_verification_t ext_partition;
1065 ext_partition.partition = boot_into_recovery ? MDTP_PARTITION_RECOVERY : MDTP_PARTITION_BOOT;
1066 ext_partition.integrity_state = device.is_tampered ? MDTP_PARTITION_STATE_INVALID : MDTP_PARTITION_STATE_VALID;
1067 ext_partition.page_size = 0; /* Not needed since already validated */
1068 ext_partition.image_addr = 0; /* Not needed since already validated */
1069 ext_partition.image_size = 0; /* Not needed since already validated */
1070 ext_partition.sig_avail = FALSE; /* Not needed since already validated */
1071 mdtp_fwlock_verify_lock(&ext_partition);
1072 }
1073#endif /* MDTP_SUPPORT */
1074
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001075#if USE_PCOM_SECBOOT
1076 set_tamper_flag(device.is_tampered);
1077#endif
1078
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001079#if VERIFIED_BOOT
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001080 switch(boot_verify_get_state())
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001081 {
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001082 case RED:
lijuanga40d6302015-07-20 20:10:13 +08001083#if FBCON_DISPLAY_MSG
lijuang9a7d3b92015-11-30 14:41:24 +08001084 display_bootverify_menu(DISPLAY_MENU_RED);
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07001085#if ENABLE_VB_ATTEST
1086 mdelay(DELAY_WAIT);
1087 shutdown_device();
1088#else
lijuanga40d6302015-07-20 20:10:13 +08001089 wait_for_users_action();
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07001090#endif
lijuanga40d6302015-07-20 20:10:13 +08001091#else
1092 dprintf(CRITICAL,
1093 "Your device has failed verification and may not work properly.\nWait for 5 seconds before proceeding\n");
1094 mdelay(5000);
1095#endif
1096
1097 break;
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001098 case YELLOW:
lijuanga40d6302015-07-20 20:10:13 +08001099#if FBCON_DISPLAY_MSG
lijuang9a7d3b92015-11-30 14:41:24 +08001100 display_bootverify_menu(DISPLAY_MENU_YELLOW);
lijuanga40d6302015-07-20 20:10:13 +08001101 wait_for_users_action();
1102#else
1103 dprintf(CRITICAL,
1104 "Your device has loaded a different operating system.\nWait for 5 seconds before proceeding\n");
1105 mdelay(5000);
1106#endif
1107 break;
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001108 default:
lijuanga40d6302015-07-20 20:10:13 +08001109 break;
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001110 }
1111#endif
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001112#if !VERIFIED_BOOT
Unnati Gandhi1be04752015-03-27 19:41:53 +05301113 if(device.is_tampered)
1114 {
1115 write_device_info_mmc(&device);
1116 #ifdef TZ_TAMPER_FUSE
1117 set_tamper_fuse_cmd();
1118 #endif
1119 #ifdef ASSERT_ON_TAMPER
1120 dprintf(CRITICAL, "Device is tampered. Asserting..\n");
1121 ASSERT(0);
1122 #endif
1123 }
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001124#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001125}
1126
Maunik Shah0f3c8ac2014-03-02 17:47:58 +05301127static bool check_format_bit()
1128{
1129 bool ret = false;
1130 int index;
1131 uint64_t offset;
1132 struct boot_selection_info *in = NULL;
1133 char *buf = NULL;
1134
1135 index = partition_get_index("bootselect");
1136 if (index == INVALID_PTN)
1137 {
1138 dprintf(INFO, "Unable to locate /bootselect partition\n");
1139 return ret;
1140 }
1141 offset = partition_get_offset(index);
1142 if(!offset)
1143 {
1144 dprintf(INFO, "partition /bootselect doesn't exist\n");
1145 return ret;
1146 }
1147 buf = (char *) memalign(CACHE_LINE, ROUNDUP(page_size, CACHE_LINE));
Mayank Grover48860402016-11-29 12:34:53 +05301148 mmc_set_lun(partition_get_lun(index));
Maunik Shah0f3c8ac2014-03-02 17:47:58 +05301149 ASSERT(buf);
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301150 if (mmc_read(offset, (uint32_t *)buf, page_size))
Maunik Shah0f3c8ac2014-03-02 17:47:58 +05301151 {
1152 dprintf(INFO, "mmc read failure /bootselect %d\n", page_size);
1153 free(buf);
1154 return ret;
1155 }
1156 in = (struct boot_selection_info *) buf;
1157 if ((in->signature == BOOTSELECT_SIGNATURE) &&
1158 (in->version == BOOTSELECT_VERSION)) {
1159 if ((in->state_info & BOOTSELECT_FORMAT) &&
1160 !(in->state_info & BOOTSELECT_FACTORY))
1161 ret = true;
1162 } else {
1163 dprintf(CRITICAL, "Signature: 0x%08x or version: 0x%08x mismatched of /bootselect\n",
1164 in->signature, in->version);
1165 ASSERT(0);
1166 }
1167 free(buf);
1168 return ret;
1169}
1170
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001171void boot_verifier_init()
1172{
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001173 uint32_t boot_state;
1174 /* Check if device unlock */
1175 if(device.is_unlocked)
1176 {
1177 boot_verify_send_event(DEV_UNLOCK);
1178 boot_verify_print_state();
1179 dprintf(CRITICAL, "Device is unlocked! Skipping verification...\n");
1180 return;
1181 }
1182 else
1183 {
1184 boot_verify_send_event(BOOT_INIT);
1185 }
1186
1187 /* Initialize keystore */
1188 boot_state = boot_verify_keystore_init();
1189 if(boot_state == YELLOW)
1190 {
1191 boot_verify_print_state();
1192 dprintf(CRITICAL, "Keystore verification failed! Continuing anyways...\n");
1193 }
1194}
1195
Shashank Mittal23b8f422010-04-16 19:27:21 -07001196int boot_linux_from_mmc(void)
1197{
1198 struct boot_img_hdr *hdr = (void*) buf;
1199 struct boot_img_hdr *uhdr;
1200 unsigned offset = 0;
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07001201 int rcode;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001202 unsigned long long ptn = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001203 int index = INVALID_PTN;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001204
Shashank Mittalcd98d472011-08-02 14:29:24 -07001205 unsigned char *image_addr = 0;
1206 unsigned kernel_actual;
1207 unsigned ramdisk_actual;
1208 unsigned imagesize_actual;
Neeti Desai465491e2012-07-31 12:53:35 -07001209 unsigned second_actual = 0;
Neeti Desai465491e2012-07-31 12:53:35 -07001210
Matthew Qin271927e2015-03-31 22:07:07 -04001211 unsigned int dtb_size = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08001212 unsigned int out_len = 0;
1213 unsigned int out_avai_len = 0;
1214 unsigned char *out_addr = NULL;
1215 uint32_t dtb_offset = 0;
1216 unsigned char *kernel_start_addr = NULL;
1217 unsigned int kernel_size = 0;
Ameya Thakur10a33452016-06-13 14:24:26 -07001218 unsigned int patched_kernel_hdr_size = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08001219 int rc;
Mayank Groverc93cad82017-10-03 12:23:45 +05301220 char *ptn_name = NULL;
Neeti Desai465491e2012-07-31 12:53:35 -07001221#if DEVICE_TREE
1222 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -07001223 struct dt_entry dt_entry;
Neeti Desai465491e2012-07-31 12:53:35 -07001224 unsigned dt_table_offset;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001225 uint32_t dt_actual;
Deepa Dinamani19648b42013-09-05 17:05:55 -07001226 uint32_t dt_hdr_size;
Matthew Qin271927e2015-03-31 22:07:07 -04001227 unsigned char *best_match_dt_addr = NULL;
Neeti Desai465491e2012-07-31 12:53:35 -07001228#endif
Matthew Qin49e51fa2015-02-09 10:40:45 +08001229 struct kernel64_hdr *kptr = NULL;
Mayank Grover351a75e2017-05-30 20:06:08 +05301230 int current_active_slot = INVALID;
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001231
Maunik Shah0f3c8ac2014-03-02 17:47:58 +05301232 if (check_format_bit())
1233 boot_into_recovery = 1;
1234
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07001235 if (!boot_into_recovery) {
1236 memset(ffbm_mode_string, '\0', sizeof(ffbm_mode_string));
1237 rcode = get_ffbm(ffbm_mode_string, sizeof(ffbm_mode_string));
1238 if (rcode <= 0) {
1239 boot_into_ffbm = false;
1240 if (rcode < 0)
1241 dprintf(CRITICAL,"failed to get ffbm cookie");
1242 } else
1243 boot_into_ffbm = true;
1244 } else
1245 boot_into_ffbm = false;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001246 uhdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
1247 if (!memcmp(uhdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1248 dprintf(INFO, "Unified boot method!\n");
1249 hdr = uhdr;
1250 goto unified_boot;
1251 }
Mayank Groverc93cad82017-10-03 12:23:45 +05301252
1253 /* For a/b recovery image code is on boot partition.
1254 If we support multislot, always use boot partition. */
1255 if (boot_into_recovery &&
1256 (!partition_multislot_is_supported()))
1257 ptn_name = "recovery";
1258 else
1259 ptn_name = "boot";
1260
1261 index = partition_get_index(ptn_name);
1262 ptn = partition_get_offset(index);
1263 if(ptn == 0) {
1264 dprintf(CRITICAL, "ERROR: No %s partition found\n", ptn_name);
1265 return -1;
Kinson Chikf1a43512011-07-14 11:28:39 -07001266 }
Mayank Groverc93cad82017-10-03 12:23:45 +05301267
Channagoud Kadabief0547c2015-02-10 12:57:38 -08001268 /* Set Lun for boot & recovery partitions */
1269 mmc_set_lun(partition_get_lun(index));
Shashank Mittal23b8f422010-04-16 19:27:21 -07001270
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301271 if (mmc_read(ptn + offset, (uint32_t *) buf, page_size)) {
Shashank Mittal23b8f422010-04-16 19:27:21 -07001272 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
1273 return -1;
1274 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001275
1276 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001277 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Mayank Grover351a75e2017-05-30 20:06:08 +05301278 return ERR_INVALID_BOOT_MAGIC;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001279 }
1280
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07001281 if (hdr->page_size && (hdr->page_size != page_size)) {
vijay kumar2e21b3a2014-06-26 17:40:15 +05301282
1283 if (hdr->page_size > BOOT_IMG_MAX_PAGE_SIZE) {
1284 dprintf(CRITICAL, "ERROR: Invalid page size\n");
1285 return -1;
1286 }
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07001287 page_size = hdr->page_size;
1288 page_mask = page_size - 1;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001289 }
1290
Matthew Qin49e51fa2015-02-09 10:40:45 +08001291 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1292 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Kishor PKd8ddcad2017-07-27 13:53:57 +05301293 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001294
Matthew Qin49e51fa2015-02-09 10:40:45 +08001295 image_addr = (unsigned char *)target_get_scratch_address();
Kishor PK9e91b592017-05-24 12:14:55 +05301296 memcpy(image_addr, (void *)buf, page_size);
1297
1298 /* ensure commandline is terminated */
1299 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
Matthew Qin49e51fa2015-02-09 10:40:45 +08001300
1301#if DEVICE_TREE
Parth Dixit4097b622016-03-15 14:42:27 +05301302#ifndef OSVERSION_IN_BOOTIMAGE
1303 dt_size = hdr->dt_size;
1304#endif
1305 dt_actual = ROUND_TO_PAGE(dt_size, page_mask);
Kishor PKd8ddcad2017-07-27 13:53:57 +05301306 if (UINT_MAX < ((uint64_t)kernel_actual + (uint64_t)ramdisk_actual+ (uint64_t)second_actual + (uint64_t)dt_actual + page_size)) {
Vijay Kumar Pendoti208f9622016-06-09 19:34:01 +05301307 dprintf(CRITICAL, "Integer overflow detected in bootimage header fields at %u in %s\n",__LINE__,__FILE__);
1308 return -1;
1309 }
Kishor PKd8ddcad2017-07-27 13:53:57 +05301310 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + second_actual + dt_actual);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001311#else
Kishor PKd8ddcad2017-07-27 13:53:57 +05301312 if (UINT_MAX < ((uint64_t)kernel_actual + (uint64_t)ramdisk_actual + (uint64_t)second_actual + page_size)) {
Vijay Kumar Pendoti208f9622016-06-09 19:34:01 +05301313 dprintf(CRITICAL, "Integer overflow detected in bootimage header fields at %u in %s\n",__LINE__,__FILE__);
1314 return -1;
1315 }
Kishor PKd8ddcad2017-07-27 13:53:57 +05301316 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + second_actual);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001317#endif
1318
1319#if VERIFIED_BOOT
1320 boot_verifier_init();
1321#endif
1322
Vijay Kumar Pendoti9c002ad2016-03-09 13:52:45 +05301323 if (check_aboot_addr_range_overlap((uintptr_t) image_addr, imagesize_actual))
Matthew Qin49e51fa2015-02-09 10:40:45 +08001324 {
1325 dprintf(CRITICAL, "Boot image buffer address overlaps with aboot addresses.\n");
1326 return -1;
1327 }
1328
Matthew Qinbb7923d2015-02-09 10:56:09 +08001329 /*
1330 * Update loading flow of bootimage to support compressed/uncompressed
1331 * bootimage on both 64bit and 32bit platform.
1332 * 1. Load bootimage from emmc partition onto DDR.
1333 * 2. Check if bootimage is gzip format. If yes, decompress compressed kernel
1334 * 3. Check kernel header and update kernel load addr for 64bit and 32bit
1335 * platform accordingly.
1336 * 4. Sanity Check on kernel_addr and ramdisk_addr and copy data.
1337 */
Mayank Grover351a75e2017-05-30 20:06:08 +05301338 if (partition_multislot_is_supported())
1339 {
1340 current_active_slot = partition_find_active_slot();
1341 dprintf(INFO, "Loading boot image (%d) active_slot(%s): start\n",
1342 imagesize_actual, SUFFIX_SLOT(current_active_slot));
1343 }
1344 else
1345 {
1346 dprintf(INFO, "Loading (%s) image (%d): start\n",
1347 (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual);
1348 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08001349 bs_set_timestamp(BS_KERNEL_LOAD_START);
1350
Gaurav Nebhwani43e2a462016-03-17 21:32:56 +05301351 if ((target_get_max_flash_size() - page_size) < imagesize_actual)
1352 {
1353 dprintf(CRITICAL, "booimage size is greater than DDR can hold\n");
1354 return -1;
1355 }
Kishor PK9e91b592017-05-24 12:14:55 +05301356 offset = page_size;
1357 /* Read image without signature and header*/
1358 if (mmc_read(ptn + offset, (void *)(image_addr + offset), imagesize_actual - page_size))
Matthew Qin49e51fa2015-02-09 10:40:45 +08001359 {
1360 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
1361 return -1;
1362 }
1363
Mayank Grover351a75e2017-05-30 20:06:08 +05301364 if (partition_multislot_is_supported())
1365 {
1366 dprintf(INFO, "Loading boot image (%d) active_slot(%s): done\n",
1367 imagesize_actual, SUFFIX_SLOT(current_active_slot));
1368 }
1369 else
1370 {
1371 dprintf(INFO, "Loading (%s) image (%d): done\n",
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001372 (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual);
1373
Mayank Grover351a75e2017-05-30 20:06:08 +05301374 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08001375 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
1376
1377 /* Authenticate Kernel */
1378 dprintf(INFO, "use_signed_kernel=%d, is_unlocked=%d, is_tampered=%d.\n",
1379 (int) target_use_signed_kernel(),
1380 device.is_unlocked,
1381 device.is_tampered);
1382
Channagoud Kadabi736c4962015-08-21 11:56:52 -07001383 /* Change the condition a little bit to include the test framework support.
1384 * We would never reach this point if device is in fastboot mode, even if we did
1385 * that means we are in test mode, so execute kernel authentication part for the
1386 * tests */
Parth Dixitcb0c6082015-12-30 15:01:13 +05301387 if((target_use_signed_kernel() && (!device.is_unlocked)) || is_test_mode_enabled())
Matthew Qin49e51fa2015-02-09 10:40:45 +08001388 {
1389 offset = imagesize_actual;
Vijay Kumar Pendoti9c002ad2016-03-09 13:52:45 +05301390 if (check_aboot_addr_range_overlap((uintptr_t)image_addr + offset, page_size))
Matthew Qin49e51fa2015-02-09 10:40:45 +08001391 {
1392 dprintf(CRITICAL, "Signature read buffer address overlaps with aboot addresses.\n");
1393 return -1;
1394 }
1395
1396 /* Read signature */
1397 if(mmc_read(ptn + offset, (void *)(image_addr + offset), page_size))
1398 {
1399 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
1400 return -1;
1401 }
1402
1403 verify_signed_bootimg((uint32_t)image_addr, imagesize_actual);
Channagoud Kadabi736c4962015-08-21 11:56:52 -07001404 /* The purpose of our test is done here */
Parth Dixitcb0c6082015-12-30 15:01:13 +05301405 if(is_test_mode_enabled() && auth_kernel_img)
Channagoud Kadabi736c4962015-08-21 11:56:52 -07001406 return 0;
Matthew Qin49e51fa2015-02-09 10:40:45 +08001407 } else {
1408 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
1409 #ifdef TZ_SAVE_KERNEL_HASH
1410 aboot_save_boot_hash_mmc((uint32_t) image_addr, imagesize_actual);
1411 #endif /* TZ_SAVE_KERNEL_HASH */
Amit Blay4aa292f2015-04-28 21:55:59 +03001412
1413#ifdef MDTP_SUPPORT
1414 {
1415 /* Verify MDTP lock.
1416 * For boot & recovery partitions, MDTP will use boot_verifier APIs,
1417 * since verification was skipped in aboot. The signature is not part of the loaded image.
1418 */
1419 mdtp_ext_partition_verification_t ext_partition;
1420 ext_partition.partition = boot_into_recovery ? MDTP_PARTITION_RECOVERY : MDTP_PARTITION_BOOT;
1421 ext_partition.integrity_state = MDTP_PARTITION_STATE_UNSET;
1422 ext_partition.page_size = page_size;
1423 ext_partition.image_addr = (uint32)image_addr;
1424 ext_partition.image_size = imagesize_actual;
1425 ext_partition.sig_avail = FALSE;
1426 mdtp_fwlock_verify_lock(&ext_partition);
1427 }
1428#endif /* MDTP_SUPPORT */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001429 }
1430
Sridhar Parasuram00bfedb2015-05-26 14:21:27 -07001431#if VERIFIED_BOOT
taozhang93088bd2016-11-16 15:50:46 +08001432 if((boot_verify_get_state() == ORANGE) && (!boot_into_ffbm))
Reut Zysmanba8a9d52016-02-18 11:44:04 +02001433 {
1434#if FBCON_DISPLAY_MSG
1435 display_bootverify_menu(DISPLAY_MENU_ORANGE);
1436 wait_for_users_action();
1437#else
1438 dprintf(CRITICAL,
1439 "Your device has been unlocked and can't be trusted.\nWait for 5 seconds before proceeding\n");
1440 mdelay(5000);
1441#endif
1442 }
1443#endif
1444
1445#if VERIFIED_BOOT
Mayank Grover912eaa62017-10-26 12:08:53 +05301446 if (VB_M <= target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +05301447 {
1448 /* set boot and system versions. */
1449 set_os_version((unsigned char *)image_addr);
1450 // send root of trust
1451 if(!send_rot_command((uint32_t)device.is_unlocked))
1452 ASSERT(0);
1453 }
Parth Dixitbc9b6492015-10-18 00:41:38 +05301454#endif
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001455 /*
Matthew Qinbb7923d2015-02-09 10:56:09 +08001456 * Check if the kernel image is a gzip package. If yes, need to decompress it.
1457 * If not, continue booting.
1458 */
1459 if (is_gzip_package((unsigned char *)(image_addr + page_size), hdr->kernel_size))
1460 {
1461 out_addr = (unsigned char *)(image_addr + imagesize_actual + page_size);
1462 out_avai_len = target_get_max_flash_size() - imagesize_actual - page_size;
Matthew Qin0b15b322015-05-19 05:20:54 -04001463 dprintf(INFO, "decompressing kernel image: start\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001464 rc = decompress((unsigned char *)(image_addr + page_size),
1465 hdr->kernel_size, out_addr, out_avai_len,
1466 &dtb_offset, &out_len);
1467 if (rc)
1468 {
Matthew Qin0b15b322015-05-19 05:20:54 -04001469 dprintf(CRITICAL, "decompressing kernel image failed!!!\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001470 ASSERT(0);
1471 }
1472
Matthew Qin0b15b322015-05-19 05:20:54 -04001473 dprintf(INFO, "decompressing kernel image: done\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001474 kptr = (struct kernel64_hdr *)out_addr;
1475 kernel_start_addr = out_addr;
1476 kernel_size = out_len;
1477 } else {
Ameya Thakur10a33452016-06-13 14:24:26 -07001478 dprintf(INFO, "Uncpmpressed kernel in use\n");
1479 if (!strncmp((char*)(image_addr + page_size),
1480 PATCHED_KERNEL_MAGIC,
1481 sizeof(PATCHED_KERNEL_MAGIC) - 1)) {
1482 dprintf(INFO, "Patched kernel detected\n");
1483 kptr = (struct kernel64_hdr *)(image_addr + page_size +
1484 PATCHED_KERNEL_HEADER_SIZE);
1485 //The size of the kernel is stored at start of kernel image + 16
1486 //The dtb would start just after the kernel
1487 dtb_offset = *((uint32_t*)((unsigned char*)
1488 (image_addr + page_size +
1489 sizeof(PATCHED_KERNEL_MAGIC) -
1490 1)));
1491 //The actual kernel starts after the 20 byte header.
1492 kernel_start_addr = (unsigned char*)(image_addr +
1493 page_size + PATCHED_KERNEL_HEADER_SIZE);
1494 kernel_size = hdr->kernel_size;
1495 patched_kernel_hdr_size = PATCHED_KERNEL_HEADER_SIZE;
1496 } else {
1497 dprintf(INFO, "Kernel image not patched..Unable to locate dt offset\n");
1498 kptr = (struct kernel64_hdr *)(image_addr + page_size);
1499 kernel_start_addr = (unsigned char *)(image_addr + page_size);
1500 kernel_size = hdr->kernel_size;
1501 }
Matthew Qinbb7923d2015-02-09 10:56:09 +08001502 }
1503
1504 /*
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001505 * Update the kernel/ramdisk/tags address if the boot image header
1506 * has default values, these default values come from mkbootimg when
1507 * the boot image is flashed using fastboot flash:raw
1508 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001509 update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr));
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001510
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001511 /* Get virtual addresses since the hdr saves physical addresses. */
1512 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
1513 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
1514 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001515
Matthew Qinbb7923d2015-02-09 10:56:09 +08001516 kernel_size = ROUND_TO_PAGE(kernel_size, page_mask);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001517 /* Check if the addresses in the header are valid. */
Matthew Qinbb7923d2015-02-09 10:56:09 +08001518 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_size) ||
Mayank Grovere559cec2017-10-17 15:12:03 +05301519 check_ddr_addr_range_bound(hdr->kernel_addr, kernel_size) ||
1520 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual) ||
1521 check_ddr_addr_range_bound(hdr->ramdisk_addr, ramdisk_actual))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001522 {
Mayank Grovere559cec2017-10-17 15:12:03 +05301523 dprintf(CRITICAL, "kernel/ramdisk addresses are not valid.\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001524 return -1;
1525 }
1526
1527#ifndef DEVICE_TREE
Mayank Grovere559cec2017-10-17 15:12:03 +05301528 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE) ||
1529 check_ddr_addr_range_bound(hdr->tags_addr, MAX_TAGS_SIZE))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001530 {
Mayank Grovere559cec2017-10-17 15:12:03 +05301531 dprintf(CRITICAL, "Tags addresses are not valid.\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001532 return -1;
1533 }
1534#endif
1535
Matthew Qin49e51fa2015-02-09 10:40:45 +08001536 /* Move kernel, ramdisk and device tree to correct address */
Matthew Qinbb7923d2015-02-09 10:56:09 +08001537 memmove((void*) hdr->kernel_addr, kernel_start_addr, kernel_size);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001538 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001539
Matthew Qin49e51fa2015-02-09 10:40:45 +08001540 #if DEVICE_TREE
Parth Dixit4097b622016-03-15 14:42:27 +05301541 if(dt_size) {
Matthew Qin49e51fa2015-02-09 10:40:45 +08001542 dt_table_offset = ((uint32_t)image_addr + page_size + kernel_actual + ramdisk_actual + second_actual);
1543 table = (struct dt_table*) dt_table_offset;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001544
Matthew Qin49e51fa2015-02-09 10:40:45 +08001545 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
1546 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1547 return -1;
1548 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001549
P.V. Phani Kumar82916762016-03-09 09:20:19 +05301550 /* Its Error if, dt_hdr_size (table->num_entries * dt_entry size + Dev_Tree Header)
1551 goes beyound hdr->dt_size*/
Parth Dixit4097b622016-03-15 14:42:27 +05301552 if (dt_hdr_size > ROUND_TO_PAGE(dt_size,hdr->page_size)) {
P.V. Phani Kumar82916762016-03-09 09:20:19 +05301553 dprintf(CRITICAL, "ERROR: Invalid Device Tree size \n");
1554 return -1;
1555 }
1556
Matthew Qin49e51fa2015-02-09 10:40:45 +08001557 /* Find index of device tree within device tree table */
1558 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
1559 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1560 return -1;
1561 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001562
P.V. Phani Kumar3c333202016-03-09 11:54:37 +05301563 if(dt_entry.offset > (UINT_MAX - dt_entry.size)) {
1564 dprintf(CRITICAL, "ERROR: Device tree contents are Invalid\n");
1565 return -1;
1566 }
1567
1568 /* Ensure we are not overshooting dt_size with the dt_entry selected */
Parth Dixit4097b622016-03-15 14:42:27 +05301569 if ((dt_entry.offset + dt_entry.size) > dt_size) {
P.V. Phani Kumar3c333202016-03-09 11:54:37 +05301570 dprintf(CRITICAL, "ERROR: Device tree contents are Invalid\n");
1571 return -1;
1572 }
1573
Matthew Qin271927e2015-03-31 22:07:07 -04001574 if (is_gzip_package((unsigned char *)dt_table_offset + dt_entry.offset, dt_entry.size))
1575 {
1576 unsigned int compressed_size = 0;
1577 out_addr += out_len;
1578 out_avai_len -= out_len;
Matthew Qin0b15b322015-05-19 05:20:54 -04001579 dprintf(INFO, "decompressing dtb: start\n");
Matthew Qin271927e2015-03-31 22:07:07 -04001580 rc = decompress((unsigned char *)dt_table_offset + dt_entry.offset,
1581 dt_entry.size, out_addr, out_avai_len,
1582 &compressed_size, &dtb_size);
1583 if (rc)
1584 {
Matthew Qin0b15b322015-05-19 05:20:54 -04001585 dprintf(CRITICAL, "decompressing dtb failed!!!\n");
Matthew Qin271927e2015-03-31 22:07:07 -04001586 ASSERT(0);
1587 }
1588
Matthew Qin0b15b322015-05-19 05:20:54 -04001589 dprintf(INFO, "decompressing dtb: done\n");
Matthew Qin271927e2015-03-31 22:07:07 -04001590 best_match_dt_addr = out_addr;
1591 } else {
1592 best_match_dt_addr = (unsigned char *)dt_table_offset + dt_entry.offset;
1593 dtb_size = dt_entry.size;
1594 }
1595
Matthew Qin49e51fa2015-02-09 10:40:45 +08001596 /* Validate and Read device device tree in the tags_addr */
Mayank Grovere559cec2017-10-17 15:12:03 +05301597 if (check_aboot_addr_range_overlap(hdr->tags_addr, dtb_size) ||
1598 check_ddr_addr_range_bound(hdr->tags_addr, dtb_size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001599 {
Mayank Grovere559cec2017-10-17 15:12:03 +05301600 dprintf(CRITICAL, "Device tree addresses are not valid\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001601 return -1;
1602 }
Shashank Mittal162244e2011-08-08 19:01:25 -07001603
Matthew Qin271927e2015-03-31 22:07:07 -04001604 memmove((void *)hdr->tags_addr, (char *)best_match_dt_addr, dtb_size);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001605 } else {
1606 /* Validate the tags_addr */
Mayank Grovere559cec2017-10-17 15:12:03 +05301607 if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual) ||
1608 check_ddr_addr_range_bound(hdr->tags_addr, kernel_actual))
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001609 {
Mayank Grovere559cec2017-10-17 15:12:03 +05301610 dprintf(CRITICAL, "Device tree addresses are not valid.\n");
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001611 return -1;
1612 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08001613 /*
1614 * If appended dev tree is found, update the atags with
1615 * memory address to the DTB appended location on RAM.
1616 * Else update with the atags address in the kernel header
1617 */
1618 void *dtb;
Ameya Thakur10a33452016-06-13 14:24:26 -07001619 dtb = dev_tree_appended(
1620 (void*)(image_addr + page_size +
1621 patched_kernel_hdr_size),
1622 hdr->kernel_size, dtb_offset,
1623 (void *)hdr->tags_addr);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001624 if (!dtb) {
1625 dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001626 return -1;
1627 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07001628 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08001629 #endif
Shashank Mittal23b8f422010-04-16 19:27:21 -07001630
Stanimir Varbanov69ec5462013-07-18 18:17:42 +03001631 if (boot_into_recovery && !device.is_unlocked && !device.is_tampered)
1632 target_load_ssd_keystore();
1633
Shashank Mittal23b8f422010-04-16 19:27:21 -07001634unified_boot:
Shashank Mittal23b8f422010-04-16 19:27:21 -07001635
Dima Zavin77e41f32013-03-06 16:10:43 -08001636 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07001637 (const char *)hdr->cmdline, board_machtype(),
Shashank Mittal23b8f422010-04-16 19:27:21 -07001638 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
1639
1640 return 0;
1641}
1642
Dima Zavin214cc642009-01-26 11:16:21 -08001643int boot_linux_from_flash(void)
1644{
1645 struct boot_img_hdr *hdr = (void*) buf;
Dima Zavin214cc642009-01-26 11:16:21 -08001646 struct ptentry *ptn;
1647 struct ptable *ptable;
1648 unsigned offset = 0;
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001649
Shashank Mittalcd98d472011-08-02 14:29:24 -07001650 unsigned char *image_addr = 0;
1651 unsigned kernel_actual;
1652 unsigned ramdisk_actual;
1653 unsigned imagesize_actual;
vijay kumar8f53e362015-11-24 13:38:11 +05301654 unsigned second_actual = 0;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001655
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001656#if DEVICE_TREE
1657 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -07001658 struct dt_entry dt_entry;
vijay kumar8f53e362015-11-24 13:38:11 +05301659 unsigned dt_table_offset;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001660 uint32_t dt_actual;
Deepa Dinamani19648b42013-09-05 17:05:55 -07001661 uint32_t dt_hdr_size;
vijay kumar8f53e362015-11-24 13:38:11 +05301662 unsigned int dtb_size = 0;
1663 unsigned char *best_match_dt_addr = NULL;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001664#endif
1665
David Ng183a7422009-12-07 14:55:21 -08001666 if (target_is_emmc_boot()) {
1667 hdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
1668 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1669 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
1670 return -1;
1671 }
1672 goto continue_boot;
1673 }
1674
Dima Zavin214cc642009-01-26 11:16:21 -08001675 ptable = flash_get_ptable();
1676 if (ptable == NULL) {
1677 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1678 return -1;
1679 }
1680
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001681 if(!boot_into_recovery)
1682 {
1683 ptn = ptable_find(ptable, "boot");
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001684
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001685 if (ptn == NULL) {
1686 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1687 return -1;
1688 }
1689 }
1690 else
1691 {
1692 ptn = ptable_find(ptable, "recovery");
1693 if (ptn == NULL) {
1694 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
1695 return -1;
1696 }
Dima Zavin214cc642009-01-26 11:16:21 -08001697 }
1698
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05301699 /* Read boot.img header from flash */
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001700 if (flash_read(ptn, offset, buf, page_size)) {
Dima Zavin214cc642009-01-26 11:16:21 -08001701 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
1702 return -1;
1703 }
Dima Zavin214cc642009-01-26 11:16:21 -08001704
1705 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001706 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Dima Zavin214cc642009-01-26 11:16:21 -08001707 return -1;
1708 }
1709
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001710 if (hdr->page_size != page_size) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001711 dprintf(CRITICAL, "ERROR: Invalid boot image pagesize. Device pagesize: %d, Image pagesize: %d\n",page_size,hdr->page_size);
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001712 return -1;
1713 }
1714
Kishor PK9e91b592017-05-24 12:14:55 +05301715 image_addr = (unsigned char *)target_get_scratch_address();
1716 memcpy(image_addr, (void *)buf, page_size);
vijay kumar287bb542015-09-29 13:01:52 +05301717
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001718 /*
1719 * Update the kernel/ramdisk/tags address if the boot image header
1720 * has default values, these default values come from mkbootimg when
1721 * the boot image is flashed using fastboot flash:raw
1722 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001723 update_ker_tags_rdisk_addr(hdr, false);
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001724
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001725 /* Get virtual addresses since the hdr saves physical addresses. */
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001726 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
1727 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
1728 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
1729
1730 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1731 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Mayank Grover032736f2017-07-14 20:34:51 +05301732 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001733
Kishor PK9e91b592017-05-24 12:14:55 +05301734 /* ensure commandline is terminated */
1735 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
1736
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001737 /* Check if the addresses in the header are valid. */
1738 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
Mayank Grovere559cec2017-10-17 15:12:03 +05301739 check_ddr_addr_range_bound(hdr->kernel_addr, kernel_actual) ||
1740 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual) ||
1741 check_ddr_addr_range_bound(hdr->ramdisk_addr, ramdisk_actual))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001742 {
Mayank Grovere559cec2017-10-17 15:12:03 +05301743 dprintf(CRITICAL, "kernel/ramdisk addresses are not valid.\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001744 return -1;
1745 }
1746
1747#ifndef DEVICE_TREE
Kishor PKd8ddcad2017-07-27 13:53:57 +05301748 if (UINT_MAX < ((uint64_t)kernel_actual + (uint64_t)ramdisk_actual+ (uint64_t)second_actual + page_size)) {
Mayank Grover032736f2017-07-14 20:34:51 +05301749 dprintf(CRITICAL, "Integer overflow detected in bootimage header fields\n");
1750 return -1;
1751 }
Kishor PKd8ddcad2017-07-27 13:53:57 +05301752 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + second_actual);
Mayank Grover032736f2017-07-14 20:34:51 +05301753
Mayank Grovere559cec2017-10-17 15:12:03 +05301754 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE) ||
1755 check_ddr_addr_range_bound(hdr->tags_addr, MAX_TAGS_SIZE))
Vijay Kumar Pendotid3ed20e2016-09-20 00:34:46 +05301756 {
Mayank Grovere559cec2017-10-17 15:12:03 +05301757 dprintf(CRITICAL, "Tags addresses are not valid.\n");
Vijay Kumar Pendotid3ed20e2016-09-20 00:34:46 +05301758 return -1;
1759 }
1760#else
1761
1762#ifndef OSVERSION_IN_BOOTIMAGE
1763 dt_size = hdr->dt_size;
1764#endif
Mayank Grover032736f2017-07-14 20:34:51 +05301765 dt_actual = ROUND_TO_PAGE(dt_size, page_mask);
Kishor PKd8ddcad2017-07-27 13:53:57 +05301766 if (UINT_MAX < ((uint64_t)kernel_actual + (uint64_t)ramdisk_actual+ (uint64_t)second_actual + (uint64_t)dt_actual + page_size)) {
Mayank Grover032736f2017-07-14 20:34:51 +05301767 dprintf(CRITICAL, "Integer overflow detected in bootimage header fields\n");
1768 return -1;
1769 }
1770
Kishor PKd8ddcad2017-07-27 13:53:57 +05301771 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + second_actual + dt_actual);
Mayank Grover032736f2017-07-14 20:34:51 +05301772
Mayank Grovere559cec2017-10-17 15:12:03 +05301773 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_size) ||
1774 check_ddr_addr_range_bound(hdr->tags_addr, dt_size))
Mayank Grover032736f2017-07-14 20:34:51 +05301775 {
Mayank Grovere559cec2017-10-17 15:12:03 +05301776 dprintf(CRITICAL, "Device tree addresses are not valid.\n");
Mayank Grover032736f2017-07-14 20:34:51 +05301777 return -1;
1778 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001779#endif
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001780
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05301781 /* Read full boot.img from flash */
1782 dprintf(INFO, "Loading (%s) image (%d): start\n",
1783 (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual);
1784 bs_set_timestamp(BS_KERNEL_LOAD_START);
1785
1786 if (UINT_MAX - page_size < imagesize_actual)
1787 {
1788 dprintf(CRITICAL,"Integer overflow detected in bootimage header fields %u %s\n", __LINE__,__func__);
1789 return -1;
1790 }
1791
1792 /*Check the availability of RAM before reading boot image + max signature length from flash*/
1793 if (target_get_max_flash_size() < (imagesize_actual + page_size))
1794 {
1795 dprintf(CRITICAL, "bootimage size is greater than DDR can hold\n");
1796 return -1;
1797 }
1798
1799 offset = page_size;
1800 /* Read image without signature and header */
1801 if (flash_read(ptn, offset, (void *)(image_addr + offset), imagesize_actual - page_size))
1802 {
1803 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
1804 return -1;
1805 }
1806
1807 dprintf(INFO, "Loading (%s) image (%d): done\n",
1808 (!boot_into_recovery ? "boot" : "recovery"), imagesize_actual);
1809 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
1810
Shashank Mittalcd98d472011-08-02 14:29:24 -07001811 /* Authenticate Kernel */
Deepa Dinamani23b60d42013-06-24 18:10:52 -07001812 if(target_use_signed_kernel() && (!device.is_unlocked))
Shashank Mittalcd98d472011-08-02 14:29:24 -07001813 {
Shashank Mittalcd98d472011-08-02 14:29:24 -07001814 offset = imagesize_actual;
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05301815
Shashank Mittalcd98d472011-08-02 14:29:24 -07001816 /* Read signature */
1817 if (flash_read(ptn, offset, (void *)(image_addr + offset), page_size))
1818 {
1819 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001820 return -1;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001821 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07001822
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301823 verify_signed_bootimg((uint32_t)image_addr, imagesize_actual);
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05301824 }
1825 offset = page_size;
1826 if(hdr->second_size != 0) {
1827 if (UINT_MAX - offset < second_actual)
1828 {
1829 dprintf(CRITICAL, "ERROR: Integer overflow in boot image header %s\t%d\n",__func__,__LINE__);
1830 return -1;
vijay kumar8f53e362015-11-24 13:38:11 +05301831 }
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05301832 offset += second_actual;
1833 /* Second image loading not implemented. */
1834 ASSERT(0);
1835 }
Shashank Mittal162244e2011-08-08 19:01:25 -07001836
Mayank Groverfc7a2ce2017-12-20 12:58:17 +05301837 /* Move kernel and ramdisk to correct address */
1838 memmove((void*) hdr->kernel_addr, (char*) (image_addr + page_size), hdr->kernel_size);
1839 memmove((void*) hdr->ramdisk_addr, (char*) (image_addr + page_size + kernel_actual), hdr->ramdisk_size);
1840
1841#if DEVICE_TREE
1842 if(dt_size != 0) {
1843
1844 dt_table_offset = ((uint32_t)image_addr + page_size + kernel_actual + ramdisk_actual + second_actual);
1845
1846 table = (struct dt_table*) dt_table_offset;
1847
1848 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
1849 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1850 return -1;
1851 }
1852
1853 /* Its Error if, dt_hdr_size (table->num_entries * dt_entry size + Dev_Tree Header)
1854 goes beyound hdr->dt_size*/
1855 if (dt_hdr_size > ROUND_TO_PAGE(dt_size,hdr->page_size)) {
1856 dprintf(CRITICAL, "ERROR: Invalid Device Tree size \n");
1857 return -1;
1858 }
1859
1860 /* Find index of device tree within device tree table */
1861 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
1862 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1863 return -1;
1864 }
1865
1866 /* Validate and Read device device tree in the "tags_add */
1867 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size) ||
1868 check_ddr_addr_range_bound(hdr->tags_addr, dt_entry.size))
1869 {
1870 dprintf(CRITICAL, "Device tree addresses are not valid.\n");
1871 return -1;
1872 }
1873
1874 if(dt_entry.offset > (UINT_MAX - dt_entry.size)) {
1875 dprintf(CRITICAL, "ERROR: Device tree contents are Invalid\n");
1876 return -1;
1877 }
1878
1879 /* Ensure we are not overshooting dt_size with the dt_entry selected */
1880 if ((dt_entry.offset + dt_entry.size) > dt_size) {
1881 dprintf(CRITICAL, "ERROR: Device tree contents are Invalid\n");
1882 return -1;
1883 }
1884
1885 best_match_dt_addr = (unsigned char *)table + dt_entry.offset;
1886 dtb_size = dt_entry.size;
1887 memmove((void *)hdr->tags_addr, (char *)best_match_dt_addr, dtb_size);
1888 }
1889#endif
1890 if(target_use_signed_kernel() && (!device.is_unlocked))
1891 {
Shashank Mittal162244e2011-08-08 19:01:25 -07001892 /* Make sure everything from scratch address is read before next step!*/
Shashank Mittala0032282011-08-26 14:50:11 -07001893 if(device.is_tampered)
Shashank Mittal162244e2011-08-08 19:01:25 -07001894 {
1895 write_device_info_flash(&device);
1896 }
Channagoud Kadabi5c86fe32012-02-16 10:58:48 +05301897#if USE_PCOM_SECBOOT
1898 set_tamper_flag(device.is_tampered);
1899#endif
Shashank Mittalcd98d472011-08-02 14:29:24 -07001900 }
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001901
David Ng183a7422009-12-07 14:55:21 -08001902continue_boot:
Dima Zavin214cc642009-01-26 11:16:21 -08001903
Dima Zavin214cc642009-01-26 11:16:21 -08001904 /* TODO: create/pass atags to kernel */
1905
Ajay Dudanie28a6072011-07-01 13:59:46 -07001906 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07001907 (const char *)hdr->cmdline, board_machtype(),
Dima Zavin214cc642009-01-26 11:16:21 -08001908 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
1909
1910 return 0;
1911}
Brian Swetland9c4c0752009-01-25 16:23:50 -08001912
Shashank Mittal162244e2011-08-08 19:01:25 -07001913void write_device_info_mmc(device_info *dev)
1914{
Shashank Mittal162244e2011-08-08 19:01:25 -07001915 unsigned long long ptn = 0;
1916 unsigned long long size;
1917 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001918 uint8_t lun = 0;
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001919 uint32_t ret = 0;
Mayank Groverddd348d2018-01-23 14:07:09 +05301920 uint32_t device_info_sz = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001921
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001922 if (devinfo_present)
1923 index = partition_get_index("devinfo");
1924 else
1925 index = partition_get_index("aboot");
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001926
Shashank Mittal162244e2011-08-08 19:01:25 -07001927 ptn = partition_get_offset(index);
1928 if(ptn == 0)
1929 {
1930 return;
1931 }
1932
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001933 lun = partition_get_lun(index);
1934 mmc_set_lun(lun);
1935
Shashank Mittal162244e2011-08-08 19:01:25 -07001936 size = partition_get_size(index);
1937
Mayank Groverddd348d2018-01-23 14:07:09 +05301938 device_info_sz = ROUND_TO_PAGE(sizeof(struct device_info),
1939 mmc_blocksize_mask);
1940 if (device_info_sz == UINT_MAX)
1941 {
1942 dprintf(CRITICAL, "ERROR: Incorrect blocksize of card\n");
1943 return;
1944 }
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001945
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001946 if (devinfo_present)
Mayank Groverddd348d2018-01-23 14:07:09 +05301947 ret = mmc_write(ptn, device_info_sz, (void *)info_buf);
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001948 else
Mayank Groverddd348d2018-01-23 14:07:09 +05301949 ret = mmc_write((ptn + size - device_info_sz), device_info_sz, (void *)info_buf);
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001950 if (ret)
Shashank Mittal162244e2011-08-08 19:01:25 -07001951 {
1952 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
Sridhar Parasurame1a97922015-07-27 15:31:22 -07001953 ASSERT(0);
Shashank Mittal162244e2011-08-08 19:01:25 -07001954 }
1955}
1956
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001957void read_device_info_mmc(struct device_info *info)
Shashank Mittal162244e2011-08-08 19:01:25 -07001958{
Shashank Mittal162244e2011-08-08 19:01:25 -07001959 unsigned long long ptn = 0;
1960 unsigned long long size;
1961 int index = INVALID_PTN;
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001962 uint32_t ret = 0;
Mayank Groverddd348d2018-01-23 14:07:09 +05301963 uint32_t device_info_sz = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001964
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001965 if ((index = partition_get_index("devinfo")) < 0)
1966 {
1967 devinfo_present = false;
1968 index = partition_get_index("aboot");
1969 }
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001970
Shashank Mittal162244e2011-08-08 19:01:25 -07001971 ptn = partition_get_offset(index);
1972 if(ptn == 0)
1973 {
1974 return;
1975 }
1976
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001977 mmc_set_lun(partition_get_lun(index));
1978
Shashank Mittal162244e2011-08-08 19:01:25 -07001979 size = partition_get_size(index);
1980
Mayank Groverddd348d2018-01-23 14:07:09 +05301981 device_info_sz = ROUND_TO_PAGE(sizeof(struct device_info),
1982 mmc_blocksize_mask);
1983 if (device_info_sz == UINT_MAX)
1984 {
1985 dprintf(CRITICAL, "ERROR: Incorrect blocksize of card\n");
1986 return;
1987 }
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001988
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001989 if (devinfo_present)
Mayank Groverddd348d2018-01-23 14:07:09 +05301990 ret = mmc_read(ptn, (void *)info_buf, device_info_sz);
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001991 else
Mayank Groverddd348d2018-01-23 14:07:09 +05301992 ret = mmc_read((ptn + size - device_info_sz), (void *)info_buf, device_info_sz);
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001993 if (ret)
Shashank Mittal162244e2011-08-08 19:01:25 -07001994 {
1995 dprintf(CRITICAL, "ERROR: Cannot read device info\n");
Sridhar Parasurame1a97922015-07-27 15:31:22 -07001996 ASSERT(0);
Shashank Mittal162244e2011-08-08 19:01:25 -07001997 }
Shashank Mittal162244e2011-08-08 19:01:25 -07001998}
1999
2000void write_device_info_flash(device_info *dev)
2001{
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002002 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
Shashank Mittal162244e2011-08-08 19:01:25 -07002003 struct ptentry *ptn;
2004 struct ptable *ptable;
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002005 if(info == NULL)
2006 {
2007 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
2008 ASSERT(0);
2009 }
2010 info_buf = info;
Shashank Mittal162244e2011-08-08 19:01:25 -07002011 ptable = flash_get_ptable();
2012 if (ptable == NULL)
2013 {
2014 dprintf(CRITICAL, "ERROR: Partition table not found\n");
2015 return;
2016 }
2017
2018 ptn = ptable_find(ptable, "devinfo");
2019 if (ptn == NULL)
2020 {
Smita Ghosh670c8b82015-05-07 09:30:03 -07002021 dprintf(CRITICAL, "ERROR: No devinfo partition found\n");
Shashank Mittal162244e2011-08-08 19:01:25 -07002022 return;
2023 }
2024
Mayank Groverdedbc892017-10-24 13:41:34 +05302025 memset(info, 0, BOOT_IMG_MAX_PAGE_SIZE);
Shashank Mittal162244e2011-08-08 19:01:25 -07002026 memcpy(info, dev, sizeof(device_info));
2027
2028 if (flash_write(ptn, 0, (void *)info_buf, page_size))
2029 {
2030 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
2031 return;
2032 }
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002033 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07002034}
2035
vijay kumarc65876c2015-04-24 13:29:16 +05302036static int read_allow_oem_unlock(device_info *dev)
2037{
vijay kumarc65876c2015-04-24 13:29:16 +05302038 unsigned offset;
2039 int index;
2040 unsigned long long ptn;
2041 unsigned long long ptn_size;
2042 unsigned blocksize = mmc_get_device_blocksize();
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07002043 STACKBUF_DMA_ALIGN(buf, blocksize);
vijay kumarc65876c2015-04-24 13:29:16 +05302044
vijay kumarca2e6812015-07-08 20:28:25 +05302045 index = partition_get_index(frp_ptns[0]);
vijay kumarc65876c2015-04-24 13:29:16 +05302046 if (index == INVALID_PTN)
2047 {
vijay kumarca2e6812015-07-08 20:28:25 +05302048 index = partition_get_index(frp_ptns[1]);
2049 if (index == INVALID_PTN)
2050 {
2051 dprintf(CRITICAL, "Neither '%s' nor '%s' partition found\n", frp_ptns[0],frp_ptns[1]);
2052 return -1;
2053 }
vijay kumarc65876c2015-04-24 13:29:16 +05302054 }
2055
2056 ptn = partition_get_offset(index);
2057 ptn_size = partition_get_size(index);
2058 offset = ptn_size - blocksize;
2059
Mayank Grover48860402016-11-29 12:34:53 +05302060 /* Set Lun for partition */
2061 mmc_set_lun(partition_get_lun(index));
2062
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07002063 if (mmc_read(ptn + offset, (void *)buf, blocksize))
vijay kumarc65876c2015-04-24 13:29:16 +05302064 {
2065 dprintf(CRITICAL, "Reading MMC failed\n");
2066 return -1;
2067 }
2068
2069 /*is_allow_unlock is a bool value stored at the LSB of last byte*/
2070 is_allow_unlock = buf[blocksize-1] & 0x01;
2071 return 0;
2072}
2073
2074static int write_allow_oem_unlock(bool allow_unlock)
2075{
vijay kumarc65876c2015-04-24 13:29:16 +05302076 unsigned offset;
vijay kumarc65876c2015-04-24 13:29:16 +05302077 int index;
2078 unsigned long long ptn;
2079 unsigned long long ptn_size;
2080 unsigned blocksize = mmc_get_device_blocksize();
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07002081 STACKBUF_DMA_ALIGN(buf, blocksize);
vijay kumarc65876c2015-04-24 13:29:16 +05302082
vijay kumarca2e6812015-07-08 20:28:25 +05302083 index = partition_get_index(frp_ptns[0]);
vijay kumarc65876c2015-04-24 13:29:16 +05302084 if (index == INVALID_PTN)
2085 {
vijay kumarca2e6812015-07-08 20:28:25 +05302086 index = partition_get_index(frp_ptns[1]);
2087 if (index == INVALID_PTN)
2088 {
2089 dprintf(CRITICAL, "Neither '%s' nor '%s' partition found\n", frp_ptns[0],frp_ptns[1]);
2090 return -1;
2091 }
vijay kumarc65876c2015-04-24 13:29:16 +05302092 }
2093
2094 ptn = partition_get_offset(index);
2095 ptn_size = partition_get_size(index);
2096 offset = ptn_size - blocksize;
Mayank Grover48860402016-11-29 12:34:53 +05302097 mmc_set_lun(partition_get_lun(index));
vijay kumarc65876c2015-04-24 13:29:16 +05302098
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07002099 if (mmc_read(ptn + offset, (void *)buf, blocksize))
vijay kumarc65876c2015-04-24 13:29:16 +05302100 {
2101 dprintf(CRITICAL, "Reading MMC failed\n");
2102 return -1;
2103 }
2104
2105 /*is_allow_unlock is a bool value stored at the LSB of last byte*/
2106 buf[blocksize-1] = allow_unlock;
2107 if (mmc_write(ptn + offset, blocksize, buf))
2108 {
2109 dprintf(CRITICAL, "Writing MMC failed\n");
2110 return -1;
2111 }
2112
2113 return 0;
2114}
2115
Shashank Mittal162244e2011-08-08 19:01:25 -07002116void read_device_info_flash(device_info *dev)
2117{
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002118 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
Shashank Mittal162244e2011-08-08 19:01:25 -07002119 struct ptentry *ptn;
2120 struct ptable *ptable;
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002121 if(info == NULL)
2122 {
2123 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
2124 ASSERT(0);
2125 }
2126 info_buf = info;
Shashank Mittal162244e2011-08-08 19:01:25 -07002127 ptable = flash_get_ptable();
2128 if (ptable == NULL)
2129 {
2130 dprintf(CRITICAL, "ERROR: Partition table not found\n");
2131 return;
2132 }
2133
2134 ptn = ptable_find(ptable, "devinfo");
2135 if (ptn == NULL)
2136 {
Smita Ghosh670c8b82015-05-07 09:30:03 -07002137 dprintf(CRITICAL, "ERROR: No devinfo partition found\n");
Shashank Mittal162244e2011-08-08 19:01:25 -07002138 return;
2139 }
2140
2141 if (flash_read(ptn, 0, (void *)info_buf, page_size))
2142 {
2143 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
2144 return;
2145 }
2146
2147 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
2148 {
Shashank Mittal162244e2011-08-08 19:01:25 -07002149 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
2150 info->is_unlocked = 0;
Shashank Mittala0032282011-08-26 14:50:11 -07002151 info->is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07002152 write_device_info_flash(info);
2153 }
2154 memcpy(dev, info, sizeof(device_info));
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002155 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07002156}
2157
2158void write_device_info(device_info *dev)
2159{
2160 if(target_is_emmc_boot())
2161 {
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002162 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
2163 if(info == NULL)
2164 {
2165 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
2166 ASSERT(0);
2167 }
2168 info_buf = info;
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002169 memcpy(info, dev, sizeof(struct device_info));
2170
2171#if USE_RPMB_FOR_DEVINFO
Mayank Grover912eaa62017-10-26 12:08:53 +05302172 if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05302173 is_secure_boot_enable()) {
2174 if((write_device_info_rpmb((void*) info, PAGE_SIZE)) < 0)
2175 ASSERT(0);
Sridhar Parasurame1a97922015-07-27 15:31:22 -07002176 }
Channagoud Kadabic80cb492015-04-28 16:08:28 -07002177 else
2178 write_device_info_mmc(info);
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002179#else
2180 write_device_info_mmc(info);
2181#endif
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002182 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07002183 }
2184 else
2185 {
2186 write_device_info_flash(dev);
2187 }
2188}
2189
2190void read_device_info(device_info *dev)
2191{
2192 if(target_is_emmc_boot())
2193 {
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002194 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
2195 if(info == NULL)
2196 {
2197 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
2198 ASSERT(0);
2199 }
2200 info_buf = info;
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002201
2202#if USE_RPMB_FOR_DEVINFO
Mayank Grover912eaa62017-10-26 12:08:53 +05302203 if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05302204 is_secure_boot_enable()) {
2205 if((read_device_info_rpmb((void*) info, PAGE_SIZE)) < 0)
2206 ASSERT(0);
Sridhar Parasurame1a97922015-07-27 15:31:22 -07002207 }
Channagoud Kadabic80cb492015-04-28 16:08:28 -07002208 else
2209 read_device_info_mmc(info);
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002210#else
2211 read_device_info_mmc(info);
2212#endif
2213
2214 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
2215 {
2216 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
lijuang511a2b52015-08-14 20:50:51 +08002217 if (is_secure_boot_enable()) {
Channagoud Kadabi05f78ba2015-07-06 11:58:14 -07002218 info->is_unlocked = 0;
Mayank Grover889be1b2017-09-12 20:12:23 +05302219#if VERIFIED_BOOT
Mayank Grover912eaa62017-10-26 12:08:53 +05302220 if (VB_M <= target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +05302221 info->is_unlock_critical = 0;
Parth Dixitddbc7352015-10-18 03:13:31 +05302222#endif
lijuang511a2b52015-08-14 20:50:51 +08002223 } else {
Channagoud Kadabi2fda4092015-07-07 13:34:11 -07002224 info->is_unlocked = 1;
Mayank Grover889be1b2017-09-12 20:12:23 +05302225#if VERIFIED_BOOT
Mayank Grover912eaa62017-10-26 12:08:53 +05302226 if (VB_M <= target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +05302227 info->is_unlock_critical = 1;
Parth Dixitddbc7352015-10-18 03:13:31 +05302228#endif
lijuang511a2b52015-08-14 20:50:51 +08002229 }
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002230 info->is_tampered = 0;
2231 info->charger_screen_enabled = 0;
Mayank Grover889be1b2017-09-12 20:12:23 +05302232#if VERIFIED_BOOT
Mayank Grover912eaa62017-10-26 12:08:53 +05302233 if (VB_M <= target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +05302234 info->verity_mode = 1; //enforcing by default
Parth Dixitddbc7352015-10-18 03:13:31 +05302235#endif
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002236 write_device_info(info);
2237 }
2238 memcpy(dev, info, sizeof(device_info));
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002239 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07002240 }
2241 else
2242 {
2243 read_device_info_flash(dev);
2244 }
2245}
2246
2247void reset_device_info()
2248{
2249 dprintf(ALWAYS, "reset_device_info called.");
Shashank Mittala0032282011-08-26 14:50:11 -07002250 device.is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07002251 write_device_info(&device);
2252}
2253
2254void set_device_root()
2255{
2256 dprintf(ALWAYS, "set_device_root called.");
Shashank Mittala0032282011-08-26 14:50:11 -07002257 device.is_tampered = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -07002258 write_device_info(&device);
2259}
2260
lijuang4ece1e72015-08-14 21:02:36 +08002261/* set device unlock value
2262 * Must check FRP before call this function
2263 * Need to wipe data when unlock status changed
2264 * type 0: oem unlock
2265 * type 1: unlock critical
2266 * status 0: unlock as false
2267 * status 1: lock as true
2268 */
2269void set_device_unlock_value(int type, bool status)
lijuang21f12f52015-08-22 16:22:19 +08002270{
lijuang4ece1e72015-08-14 21:02:36 +08002271 if (type == UNLOCK)
2272 device.is_unlocked = status;
Mayank Grover889be1b2017-09-12 20:12:23 +05302273#if VERIFIED_BOOT
Mayank Grover912eaa62017-10-26 12:08:53 +05302274 else if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05302275 type == UNLOCK_CRITICAL)
2276 device.is_unlock_critical = status;
Parth Dixitddbc7352015-10-18 03:13:31 +05302277#endif
lijuang4ece1e72015-08-14 21:02:36 +08002278 write_device_info(&device);
2279}
2280
2281static void set_device_unlock(int type, bool status)
2282{
2283 int is_unlocked = -1;
2284 char response[MAX_RSP_SIZE];
2285
2286 /* check device unlock status if it is as expected */
2287 if (type == UNLOCK)
2288 is_unlocked = device.is_unlocked;
Mayank Grover889be1b2017-09-12 20:12:23 +05302289#if VERIFIED_BOOT
Mayank Grover912eaa62017-10-26 12:08:53 +05302290 if(VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05302291 type == UNLOCK_CRITICAL)
2292 {
2293 is_unlocked = device.is_unlock_critical;
2294 }
Parth Dixitddbc7352015-10-18 03:13:31 +05302295#endif
lijuang4ece1e72015-08-14 21:02:36 +08002296 if (is_unlocked == status) {
2297 snprintf(response, sizeof(response), "\tDevice already : %s", (status ? "unlocked!" : "locked!"));
2298 fastboot_info(response);
2299 fastboot_okay("");
2300 return;
2301 }
2302
2303 /* status is true, it means to unlock device */
2304 if (status) {
lijuang21f12f52015-08-22 16:22:19 +08002305 if(!is_allow_unlock) {
2306 fastboot_fail("oem unlock is not allowed");
2307 return;
2308 }
2309
lijuang4ece1e72015-08-14 21:02:36 +08002310#if FBCON_DISPLAY_MSG
lijuang9a7d3b92015-11-30 14:41:24 +08002311 display_unlock_menu(type);
lijuang4ece1e72015-08-14 21:02:36 +08002312 fastboot_okay("");
2313 return;
2314#else
2315 if (type == UNLOCK) {
2316 fastboot_fail("Need wipe userdata. Do 'fastboot oem unlock-go'");
2317 return;
2318 }
2319#endif
lijuang21f12f52015-08-22 16:22:19 +08002320 }
lijuang4ece1e72015-08-14 21:02:36 +08002321
2322 set_device_unlock_value(type, status);
2323
2324 /* wipe data */
2325 struct recovery_message msg;
Kishor PK60a68212017-05-08 16:55:57 +05302326 memset(&msg, 0, sizeof(msg));
lijuang4ece1e72015-08-14 21:02:36 +08002327 snprintf(msg.recovery, sizeof(msg.recovery), "recovery\n--wipe_data");
2328 write_misc(0, &msg, sizeof(msg));
2329
2330 fastboot_okay("");
2331 reboot_device(RECOVERY_MODE);
lijuang21f12f52015-08-22 16:22:19 +08002332}
2333
lijuang511a2b52015-08-14 20:50:51 +08002334static bool critical_flash_allowed(const char * entry)
2335{
2336 uint32_t i = 0;
2337 if (entry == NULL)
2338 return false;
2339
2340 for (i = 0; i < ARRAY_SIZE(critical_flash_allowed_ptn); i++) {
2341 if(!strcmp(entry, critical_flash_allowed_ptn[i]))
2342 return true;
2343 }
2344 return false;
Matthew Qin271927e2015-03-31 22:07:07 -04002345}
2346
2347#if DEVICE_TREE
Amol Jadicb524072012-08-09 16:40:18 -07002348int copy_dtb(uint8_t *boot_image_start, unsigned int scratch_offset)
2349{
2350 uint32 dt_image_offset = 0;
Amol Jadicb524072012-08-09 16:40:18 -07002351 uint32_t n;
Amol Jadicb524072012-08-09 16:40:18 -07002352 struct dt_table *table;
2353 struct dt_entry dt_entry;
2354 uint32_t dt_hdr_size;
2355 unsigned int compressed_size = 0;
2356 unsigned int dtb_size = 0;
2357 unsigned int out_avai_len = 0;
2358 unsigned char *out_addr = NULL;
2359 unsigned char *best_match_dt_addr = NULL;
2360 int rc;
2361
2362 struct boot_img_hdr *hdr = (struct boot_img_hdr *) (boot_image_start);
2363
Parth Dixit4097b622016-03-15 14:42:27 +05302364#ifndef OSVERSION_IN_BOOTIMAGE
2365 dt_size = hdr->dt_size;
2366#endif
2367
2368 if(dt_size != 0) {
Amol Jadicb524072012-08-09 16:40:18 -07002369 /* add kernel offset */
2370 dt_image_offset += page_size;
2371 n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
2372 dt_image_offset += n;
2373
2374 /* add ramdisk offset */
2375 n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
2376 dt_image_offset += n;
2377
2378 /* add second offset */
2379 if(hdr->second_size != 0) {
2380 n = ROUND_TO_PAGE(hdr->second_size, page_mask);
2381 dt_image_offset += n;
2382 }
2383
2384 /* offset now point to start of dt.img */
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07002385 table = (struct dt_table*)(boot_image_start + dt_image_offset);
Amol Jadicb524072012-08-09 16:40:18 -07002386
Deepa Dinamani19648b42013-09-05 17:05:55 -07002387 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Amol Jadicb524072012-08-09 16:40:18 -07002388 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
2389 return -1;
2390 }
P.V. Phani Kumar82916762016-03-09 09:20:19 +05302391
2392 /* Its Error if, dt_hdr_size (table->num_entries * dt_entry size + Dev_Tree Header)
2393 goes beyound hdr->dt_size*/
Parth Dixit4097b622016-03-15 14:42:27 +05302394 if (dt_hdr_size > ROUND_TO_PAGE(dt_size,hdr->page_size)) {
P.V. Phani Kumar82916762016-03-09 09:20:19 +05302395 dprintf(CRITICAL, "ERROR: Invalid Device Tree size \n");
2396 return -1;
2397 }
2398
Joel Kingaa335dc2013-06-03 16:11:08 -07002399 /* Find index of device tree within device tree table */
2400 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Amol Jadicb524072012-08-09 16:40:18 -07002401 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
2402 return -1;
2403 }
2404
Matthew Qin271927e2015-03-31 22:07:07 -04002405 best_match_dt_addr = (unsigned char *)boot_image_start + dt_image_offset + dt_entry.offset;
2406 if (is_gzip_package(best_match_dt_addr, dt_entry.size))
2407 {
2408 out_addr = (unsigned char *)target_get_scratch_address() + scratch_offset;
2409 out_avai_len = target_get_max_flash_size() - scratch_offset;
Matthew Qin0b15b322015-05-19 05:20:54 -04002410 dprintf(INFO, "decompressing dtb: start\n");
Matthew Qin271927e2015-03-31 22:07:07 -04002411 rc = decompress(best_match_dt_addr,
2412 dt_entry.size, out_addr, out_avai_len,
2413 &compressed_size, &dtb_size);
2414 if (rc)
2415 {
Matthew Qin0b15b322015-05-19 05:20:54 -04002416 dprintf(CRITICAL, "decompressing dtb failed!!!\n");
Matthew Qin271927e2015-03-31 22:07:07 -04002417 ASSERT(0);
2418 }
2419
Matthew Qin0b15b322015-05-19 05:20:54 -04002420 dprintf(INFO, "decompressing dtb: done\n");
Matthew Qin271927e2015-03-31 22:07:07 -04002421 best_match_dt_addr = out_addr;
2422 } else {
2423 dtb_size = dt_entry.size;
2424 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002425 /* Validate and Read device device tree in the "tags_add */
Mayank Grovere559cec2017-10-17 15:12:03 +05302426 if (check_aboot_addr_range_overlap(hdr->tags_addr, dtb_size) ||
2427 check_ddr_addr_range_bound(hdr->tags_addr, dtb_size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002428 {
Mayank Grovere559cec2017-10-17 15:12:03 +05302429 dprintf(CRITICAL, "Device tree addresses are not valid.\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002430 return -1;
2431 }
2432
Amol Jadicb524072012-08-09 16:40:18 -07002433 /* Read device device tree in the "tags_add */
Matthew Qin271927e2015-03-31 22:07:07 -04002434 memmove((void*) hdr->tags_addr, (void *)best_match_dt_addr, dtb_size);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002435 } else
2436 return -1;
Amol Jadicb524072012-08-09 16:40:18 -07002437
2438 /* Everything looks fine. Return success. */
2439 return 0;
2440}
2441#endif
2442
Brian Swetland9c4c0752009-01-25 16:23:50 -08002443void cmd_boot(const char *arg, void *data, unsigned sz)
2444{
Amit Blay8a510302015-08-17 09:20:01 +03002445#ifdef MDTP_SUPPORT
2446 static bool is_mdtp_activated = 0;
2447#endif /* MDTP_SUPPORT */
Brian Swetland9c4c0752009-01-25 16:23:50 -08002448 unsigned kernel_actual;
2449 unsigned ramdisk_actual;
Kishor PKd8ddcad2017-07-27 13:53:57 +05302450 unsigned second_actual;
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002451 uint32_t image_actual;
2452 uint32_t dt_actual = 0;
P.V. Phani Kumareecfd822016-03-09 20:09:03 +05302453 uint32_t sig_actual = 0;
Kishor PK5134b332017-05-09 17:50:08 +05302454 uint32_t sig_size = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08002455 struct boot_img_hdr *hdr = NULL;
2456 struct kernel64_hdr *kptr = NULL;
Brian Swetland9c4c0752009-01-25 16:23:50 -08002457 char *ptr = ((char*) data);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002458 int ret = 0;
2459 uint8_t dtb_copied = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08002460 unsigned int out_len = 0;
2461 unsigned int out_avai_len = 0;
2462 unsigned char *out_addr = NULL;
2463 uint32_t dtb_offset = 0;
2464 unsigned char *kernel_start_addr = NULL;
2465 unsigned int kernel_size = 0;
Matthew Qin271927e2015-03-31 22:07:07 -04002466 unsigned int scratch_offset = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08002467
lijuang2008ff22016-03-07 17:56:27 +08002468#if FBCON_DISPLAY_MSG
2469 /* Exit keys' detection thread firstly */
2470 exit_menu_keys_detection();
2471#endif
2472
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002473#if VERIFIED_BOOT
Channagoud Kadabi6d5375e2015-06-23 17:15:42 -07002474 if(target_build_variant_user() && !device.is_unlocked)
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002475 {
2476 fastboot_fail("unlock device to use this command");
lijuang2008ff22016-03-07 17:56:27 +08002477 goto boot_failed;
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002478 }
2479#endif
2480
Brian Swetland9c4c0752009-01-25 16:23:50 -08002481 if (sz < sizeof(hdr)) {
2482 fastboot_fail("invalid bootimage header");
lijuang2008ff22016-03-07 17:56:27 +08002483 goto boot_failed;
Brian Swetland9c4c0752009-01-25 16:23:50 -08002484 }
2485
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002486 hdr = (struct boot_img_hdr *)data;
Brian Swetland9c4c0752009-01-25 16:23:50 -08002487
2488 /* ensure commandline is terminated */
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002489 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -08002490
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002491 if(target_is_emmc_boot() && hdr->page_size) {
2492 page_size = hdr->page_size;
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07002493 page_mask = page_size - 1;
2494 }
2495
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002496 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
2497 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Kishor PKd8ddcad2017-07-27 13:53:57 +05302498 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002499#if DEVICE_TREE
Parth Dixit4097b622016-03-15 14:42:27 +05302500#ifndef OSVERSION_IN_BOOTIMAGE
2501 dt_size = hdr->dt_size;
2502#endif
2503 dt_actual = ROUND_TO_PAGE(dt_size, page_mask);
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002504#endif
2505
2506 image_actual = ADD_OF(page_size, kernel_actual);
2507 image_actual = ADD_OF(image_actual, ramdisk_actual);
Kishor PKd8ddcad2017-07-27 13:53:57 +05302508 image_actual = ADD_OF(image_actual, second_actual);
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002509 image_actual = ADD_OF(image_actual, dt_actual);
2510
Kishor PK5134b332017-05-09 17:50:08 +05302511 /* Checking to prevent oob access in read_der_message_length */
2512 if (image_actual > sz) {
2513 fastboot_fail("bootimage header fields are invalid");
2514 goto boot_failed;
2515 }
2516 sig_size = sz - image_actual;
2517
P.V. Phani Kumareecfd822016-03-09 20:09:03 +05302518 if (target_use_signed_kernel() && (!device.is_unlocked)) {
2519 /* Calculate the signature length from boot image */
2520 sig_actual = read_der_message_length(
Kishor PK5134b332017-05-09 17:50:08 +05302521 (unsigned char*)(data + image_actual), sig_size);
Channagoud Kadabi3d839012014-06-26 14:26:39 -07002522 image_actual = ADD_OF(image_actual, sig_actual);
2523
Kishor PK5134b332017-05-09 17:50:08 +05302524 if (image_actual > sz) {
2525 fastboot_fail("bootimage header fields are invalid");
2526 goto boot_failed;
2527 }
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002528 }
2529
Sridhar Parasuram361e4cd2015-09-24 11:19:19 -07002530 // Initialize boot state before trying to verify boot.img
2531#if VERIFIED_BOOT
Channagoud Kadabi699466e2015-11-03 12:37:42 -08002532 boot_verifier_init();
Mayank Groverb337e932017-01-18 20:00:40 +05302533#endif
Channagoud Kadabibf6ce7d2015-09-17 13:25:35 -07002534 /* Handle overflow if the input image size is greater than
2535 * boot image buffer can hold
2536 */
2537 if ((target_get_max_flash_size() - (image_actual - sig_actual)) < page_size)
2538 {
2539 fastboot_fail("booimage: size is greater than boot image buffer can hold");
lijuang2008ff22016-03-07 17:56:27 +08002540 goto boot_failed;
Channagoud Kadabibf6ce7d2015-09-17 13:25:35 -07002541 }
Sridhar Parasuram361e4cd2015-09-24 11:19:19 -07002542
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002543 /* Verify the boot image
2544 * device & page_size are initialized in aboot_init
2545 */
Sridhar Parasuram7ae9fbc2015-09-17 09:56:30 -07002546 if (target_use_signed_kernel() && (!device.is_unlocked)) {
Channagoud Kadabi3d839012014-06-26 14:26:39 -07002547 /* Pass size excluding signature size, otherwise we would try to
2548 * access signature beyond its length
2549 */
2550 verify_signed_bootimg((uint32_t)data, (image_actual - sig_actual));
Sridhar Parasuram7ae9fbc2015-09-17 09:56:30 -07002551 }
Amit Blay4aa292f2015-04-28 21:55:59 +03002552#ifdef MDTP_SUPPORT
2553 else
2554 {
Shay Nachmani062aa3f2015-05-17 17:28:44 +03002555 /* fastboot boot is not allowed when MDTP is activated */
Amit Blay4aa292f2015-04-28 21:55:59 +03002556 mdtp_ext_partition_verification_t ext_partition;
Amit Blay8a510302015-08-17 09:20:01 +03002557
2558 if (!is_mdtp_activated) {
2559 ext_partition.partition = MDTP_PARTITION_NONE;
2560 mdtp_fwlock_verify_lock(&ext_partition);
2561 }
Amit Blay4aa292f2015-04-28 21:55:59 +03002562 }
Shay Nachmani062aa3f2015-05-17 17:28:44 +03002563
Amir Kotzer7c768c02016-04-13 09:08:05 +03002564 /* If mdtp state cannot be validate, block fastboot boot*/
2565 if(mdtp_activated(&is_mdtp_activated)){
2566 dprintf(CRITICAL, "mdtp_activated cannot validate state.\n");
2567 dprintf(CRITICAL, "Can not proceed with fastboot boot command.\n");
2568 goto boot_failed;
2569 }
Shay Nachmani062aa3f2015-05-17 17:28:44 +03002570 if(is_mdtp_activated){
2571 dprintf(CRITICAL, "fastboot boot command is not available.\n");
lijuang2008ff22016-03-07 17:56:27 +08002572 goto boot_failed;
Shay Nachmani062aa3f2015-05-17 17:28:44 +03002573 }
Amit Blay4aa292f2015-04-28 21:55:59 +03002574#endif /* MDTP_SUPPORT */
2575
Sridhar Parasuram361e4cd2015-09-24 11:19:19 -07002576#if VERIFIED_BOOT
Mayank Grover912eaa62017-10-26 12:08:53 +05302577 if (VB_M <= target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +05302578 {
2579 /* set boot and system versions. */
2580 set_os_version((unsigned char *)data);
2581 // send root of trust
2582 if(!send_rot_command((uint32_t)device.is_unlocked))
2583 ASSERT(0);
2584 }
Parth Dixitbc9b6492015-10-18 00:41:38 +05302585#endif
Channagoud Kadabia22144f2013-03-20 11:49:01 -07002586 /*
Matthew Qinbb7923d2015-02-09 10:56:09 +08002587 * Check if the kernel image is a gzip package. If yes, need to decompress it.
2588 * If not, continue booting.
2589 */
2590 if (is_gzip_package((unsigned char *)(data + page_size), hdr->kernel_size))
2591 {
2592 out_addr = (unsigned char *)target_get_scratch_address();
2593 out_addr = (unsigned char *)(out_addr + image_actual + page_size);
2594 out_avai_len = target_get_max_flash_size() - image_actual - page_size;
Matthew Qin0b15b322015-05-19 05:20:54 -04002595 dprintf(INFO, "decompressing kernel image: start\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08002596 ret = decompress((unsigned char *)(ptr + page_size),
2597 hdr->kernel_size, out_addr, out_avai_len,
2598 &dtb_offset, &out_len);
2599 if (ret)
2600 {
Matthew Qin0b15b322015-05-19 05:20:54 -04002601 dprintf(CRITICAL, "decompressing image failed!!!\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08002602 ASSERT(0);
2603 }
2604
Matthew Qin0b15b322015-05-19 05:20:54 -04002605 dprintf(INFO, "decompressing kernel image: done\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08002606 kptr = (struct kernel64_hdr *)out_addr;
2607 kernel_start_addr = out_addr;
2608 kernel_size = out_len;
2609 } else {
2610 kptr = (struct kernel64_hdr*)((char *)data + page_size);
2611 kernel_start_addr = (unsigned char *)((char *)data + page_size);
2612 kernel_size = hdr->kernel_size;
2613 }
2614
2615 /*
Channagoud Kadabia22144f2013-03-20 11:49:01 -07002616 * Update the kernel/ramdisk/tags address if the boot image header
2617 * has default values, these default values come from mkbootimg when
2618 * the boot image is flashed using fastboot flash:raw
2619 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08002620 update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr));
Dima Zavin3cadfff2013-03-21 14:30:48 -07002621
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002622 /* Get virtual addresses since the hdr saves physical addresses. */
2623 hdr->kernel_addr = VA(hdr->kernel_addr);
2624 hdr->ramdisk_addr = VA(hdr->ramdisk_addr);
2625 hdr->tags_addr = VA(hdr->tags_addr);
Brian Swetland9c4c0752009-01-25 16:23:50 -08002626
Matthew Qinbb7923d2015-02-09 10:56:09 +08002627 kernel_size = ROUND_TO_PAGE(kernel_size, page_mask);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002628 /* Check if the addresses in the header are valid. */
Matthew Qinbb7923d2015-02-09 10:56:09 +08002629 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_size) ||
Mayank Grovere559cec2017-10-17 15:12:03 +05302630 check_ddr_addr_range_bound(hdr->kernel_addr, kernel_size) ||
2631 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual) ||
2632 check_ddr_addr_range_bound(hdr->ramdisk_addr, ramdisk_actual))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002633 {
Mayank Grovere559cec2017-10-17 15:12:03 +05302634 dprintf(CRITICAL, "kernel/ramdisk addresses are not valid.\n");
lijuang2008ff22016-03-07 17:56:27 +08002635 goto boot_failed;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002636 }
2637
Amol Jadicb524072012-08-09 16:40:18 -07002638#if DEVICE_TREE
Matthew Qin271927e2015-03-31 22:07:07 -04002639 scratch_offset = image_actual + page_size + out_len;
Amol Jadicb524072012-08-09 16:40:18 -07002640 /* find correct dtb and copy it to right location */
Matthew Qin271927e2015-03-31 22:07:07 -04002641 ret = copy_dtb(data, scratch_offset);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002642
2643 dtb_copied = !ret ? 1 : 0;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002644#else
Mayank Grovere559cec2017-10-17 15:12:03 +05302645 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE) ||
2646 check_ddr_addr_range_bound(hdr->tags_addr, MAX_TAGS_SIZE))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002647 {
Mayank Grovere559cec2017-10-17 15:12:03 +05302648 dprintf(CRITICAL, "Tags addresses are not valid.\n");
lijuang2008ff22016-03-07 17:56:27 +08002649 goto boot_failed;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002650 }
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002651#endif
2652
2653 /* Load ramdisk & kernel */
2654 memmove((void*) hdr->ramdisk_addr, ptr + page_size + kernel_actual, hdr->ramdisk_size);
Matthew Qinbb7923d2015-02-09 10:56:09 +08002655 memmove((void*) hdr->kernel_addr, (char*) (kernel_start_addr), kernel_size);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002656
2657#if DEVICE_TREE
Mayank Grovere559cec2017-10-17 15:12:03 +05302658 if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual) ||
2659 check_ddr_addr_range_bound(hdr->tags_addr, kernel_actual))
Matthew Qinbb7923d2015-02-09 10:56:09 +08002660 {
Mayank Grovere559cec2017-10-17 15:12:03 +05302661 dprintf(CRITICAL, "Tags addresses are not valid.\n");
lijuang2008ff22016-03-07 17:56:27 +08002662 goto boot_failed;
Matthew Qinbb7923d2015-02-09 10:56:09 +08002663 }
2664
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002665 /*
2666 * If dtb is not found look for appended DTB in the kernel.
2667 * If appended dev tree is found, update the atags with
2668 * memory address to the DTB appended location on RAM.
2669 * Else update with the atags address in the kernel header
2670 */
2671 if (!dtb_copied) {
2672 void *dtb;
Matthew Qinbb7923d2015-02-09 10:56:09 +08002673 dtb = dev_tree_appended((void*)(ptr + page_size),
2674 hdr->kernel_size, dtb_offset,
Dima Zavine63e5572013-05-03 12:23:06 -07002675 (void *)hdr->tags_addr);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002676 if (!dtb) {
2677 fastboot_fail("dtb not found");
lijuang2008ff22016-03-07 17:56:27 +08002678 goto boot_failed;
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002679 }
Amol Jadicb524072012-08-09 16:40:18 -07002680 }
2681#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08002682
2683 fastboot_okay("");
Amol Jadi7c4316c2013-10-07 14:19:26 -07002684 fastboot_stop();
Brian Swetland9c4c0752009-01-25 16:23:50 -08002685
Dima Zavin77e41f32013-03-06 16:10:43 -08002686 boot_linux((void*) hdr->kernel_addr, (void*) hdr->tags_addr,
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002687 (const char*) hdr->cmdline, board_machtype(),
2688 (void*) hdr->ramdisk_addr, hdr->ramdisk_size);
lijuang2008ff22016-03-07 17:56:27 +08002689
2690 /* fastboot already stop, it's no need to show fastboot menu */
2691 return;
2692boot_failed:
2693#if FBCON_DISPLAY_MSG
2694 /* revert to fastboot menu if boot failed */
2695 display_fastboot_menu();
2696#endif
2697 return;
Brian Swetland9c4c0752009-01-25 16:23:50 -08002698}
2699
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002700void cmd_erase_nand(const char *arg, void *data, unsigned sz)
Dima Zavin214cc642009-01-26 11:16:21 -08002701{
2702 struct ptentry *ptn;
2703 struct ptable *ptable;
2704
2705 ptable = flash_get_ptable();
2706 if (ptable == NULL) {
2707 fastboot_fail("partition table doesn't exist");
2708 return;
2709 }
2710
2711 ptn = ptable_find(ptable, arg);
2712 if (ptn == NULL) {
2713 fastboot_fail("unknown partition name");
2714 return;
2715 }
2716
2717 if (flash_erase(ptn)) {
2718 fastboot_fail("failed to erase partition");
2719 return;
2720 }
2721 fastboot_okay("");
2722}
2723
Bikas Gurungd48bd242010-09-04 19:54:32 -07002724
2725void cmd_erase_mmc(const char *arg, void *data, unsigned sz)
2726{
2727 unsigned long long ptn = 0;
Oliver Wangcee448d2013-10-22 18:40:13 +08002728 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07002729 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002730 uint8_t lun = 0;
Vijay Kumar Pendoti0b21f462016-05-02 17:09:18 +05302731 char *footer = NULL;
Bikas Gurungd48bd242010-09-04 19:54:32 -07002732
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002733#if VERIFIED_BOOT
2734 if(!strcmp(arg, KEYSTORE_PTN_NAME))
2735 {
2736 if(!device.is_unlocked)
2737 {
2738 fastboot_fail("unlock device to erase keystore");
2739 return;
2740 }
2741 }
2742#endif
2743
Kinson Chikf1a43512011-07-14 11:28:39 -07002744 index = partition_get_index(arg);
2745 ptn = partition_get_offset(index);
Oliver Wangcee448d2013-10-22 18:40:13 +08002746 size = partition_get_size(index);
Neeti Desaica8c9602011-10-06 11:40:00 -07002747
Kinson Chikf1a43512011-07-14 11:28:39 -07002748 if(ptn == 0) {
Neeti Desaica8c9602011-10-06 11:40:00 -07002749 fastboot_fail("Partition table doesn't exist\n");
Bikas Gurungd48bd242010-09-04 19:54:32 -07002750 return;
2751 }
Kun Liang2f1601a2013-08-12 16:29:54 +08002752
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002753 lun = partition_get_lun(index);
2754 mmc_set_lun(lun);
2755
Sridhar Parasuramd7957122015-02-27 11:33:40 -08002756 if (platform_boot_dev_isemmc())
2757 {
2758 if (mmc_erase_card(ptn, size)) {
2759 fastboot_fail("failed to erase partition\n");
2760 return;
2761 }
2762 } else {
2763 BUF_DMA_ALIGN(out, DEFAULT_ERASE_SIZE);
2764 size = partition_get_size(index);
2765 if (size > DEFAULT_ERASE_SIZE)
2766 size = DEFAULT_ERASE_SIZE;
Kun Liang2f1601a2013-08-12 16:29:54 +08002767
Sridhar Parasuramd7957122015-02-27 11:33:40 -08002768 /* Simple inefficient version of erase. Just writing
2769 0 in first several blocks */
2770 if (mmc_write(ptn , size, (unsigned int *)out)) {
2771 fastboot_fail("failed to erase partition");
2772 return;
2773 }
Vijay Kumar Pendoti0b21f462016-05-02 17:09:18 +05302774 /*Erase FDE metadata at the userdata footer*/
2775 if(!(strncmp(arg, "userdata", 8)))
2776 {
2777 footer = memalign(CACHE_LINE, FOOTER_SIZE);
2778 memset((void *)footer, 0, FOOTER_SIZE);
2779
2780 size = partition_get_size(index);
2781
2782 if (mmc_write((ptn + size) - FOOTER_SIZE , FOOTER_SIZE, (unsigned int *)footer)) {
2783 fastboot_fail("failed to erase userdata footer");
2784 free(footer);
2785 return;
2786 }
2787 free(footer);
2788 }
Bikas Gurungd48bd242010-09-04 19:54:32 -07002789 }
Sridhar Parasuram32b30662015-07-10 13:33:22 -07002790#if VERIFIED_BOOT
Mayank Grover912eaa62017-10-26 12:08:53 +05302791 if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05302792 !(strncmp(arg, "userdata", 8)) &&
2793 send_delete_keys_to_tz())
Sridhar Parasuram32b30662015-07-10 13:33:22 -07002794 ASSERT(0);
2795#endif
Bikas Gurungd48bd242010-09-04 19:54:32 -07002796 fastboot_okay("");
2797}
2798
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002799void cmd_erase(const char *arg, void *data, unsigned sz)
2800{
Channagoud Kadabice3cf422015-04-17 16:02:30 -07002801#if VERIFIED_BOOT
Channagoud Kadabi35297672015-06-13 11:09:49 -07002802 if (target_build_variant_user())
Channagoud Kadabice3cf422015-04-17 16:02:30 -07002803 {
Sridhar Parasuramc32d07d2015-07-12 10:57:48 -07002804 if(!device.is_unlocked)
Channagoud Kadabice3cf422015-04-17 16:02:30 -07002805 {
Channagoud Kadabi35297672015-06-13 11:09:49 -07002806 fastboot_fail("device is locked. Cannot erase");
Channagoud Kadabice3cf422015-04-17 16:02:30 -07002807 return;
2808 }
2809 }
2810#endif
2811
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002812 if(target_is_emmc_boot())
2813 cmd_erase_mmc(arg, data, sz);
2814 else
2815 cmd_erase_nand(arg, data, sz);
2816}
Bikas Gurungd48bd242010-09-04 19:54:32 -07002817
Mayank Grover11ff9692018-01-11 11:54:49 +05302818/* Get the size from partiton name */
2819static void get_partition_size(const char *arg, char *response)
2820{
2821 uint64_t ptn = 0;
2822 uint64_t size;
2823 int index = INVALID_PTN;
2824
2825 index = partition_get_index(arg);
2826
2827 if (index == INVALID_PTN)
2828 {
2829 dprintf(CRITICAL, "Invalid partition index\n");
2830 return;
2831 }
2832
2833 ptn = partition_get_offset(index);
2834
2835 if(!ptn)
2836 {
2837 dprintf(CRITICAL, "Invalid partition name %s\n", arg);
2838 return;
2839 }
2840
2841 size = partition_get_size(index);
2842
2843 snprintf(response, MAX_RSP_SIZE, "\t 0x%llx", size);
2844 return;
2845}
2846
2847/*
2848 * Publish the partition type & size info
2849 * fastboot getvar will publish the required information.
2850 * fastboot getvar partition_size:<partition_name>: partition size in hex
2851 * fastboot getvar partition_type:<partition_name>: partition type (ext/fat)
2852 */
2853static void publish_getvar_partition_info(struct getvar_partition_info *info, uint8_t num_parts)
2854{
2855 uint8_t i,n;
2856 static bool published = false;
2857 struct partition_entry *ptn_entry =
2858 partition_get_partition_entries();
2859 memset(info, 0, sizeof(struct getvar_partition_info)* num_parts);
2860
2861 for (i = 0; i < num_parts; i++) {
2862 strlcat(info[i].part_name, (const char *)ptn_entry[i].name, MAX_RSP_SIZE);
2863 strlcat(info[i].getvar_size, "partition-size:", MAX_GET_VAR_NAME_SIZE);
2864 strlcat(info[i].getvar_type, "partition-type:", MAX_GET_VAR_NAME_SIZE);
2865
2866 /* Mark partiton type for known paritions only */
2867 for (n=0; n < ARRAY_SIZE(part_type_known); n++)
2868 {
2869 if (!strncmp(part_type_known[n].part_name, info[i].part_name,
2870 strlen(part_type_known[n].part_name)))
2871 {
2872 strlcat(info[i].type_response,
2873 part_type_known[n].type_response,
2874 MAX_RSP_SIZE);
2875 break;
2876 }
2877 }
2878
2879 get_partition_size(info[i].part_name, info[i].size_response);
2880
2881 if (strlcat(info[i].getvar_size, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
2882 {
2883 dprintf(CRITICAL, "partition size name truncated\n");
2884 return;
2885 }
2886 if (strlcat(info[i].getvar_type, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
2887 {
2888 dprintf(CRITICAL, "partition type name truncated\n");
2889 return;
2890 }
2891
2892 if (!published)
2893 {
2894 /* publish partition size & type info */
2895 fastboot_publish((const char *) info[i].getvar_size, (const char *) info[i].size_response);
2896 fastboot_publish((const char *) info[i].getvar_type, (const char *) info[i].type_response);
2897 }
2898 }
2899 if (!published)
2900 published = true;
2901}
2902
2903
Ajay Dudani5c761132011-04-07 20:19:04 -07002904void cmd_flash_mmc_img(const char *arg, void *data, unsigned sz)
Shashank Mittal23b8f422010-04-16 19:27:21 -07002905{
2906 unsigned long long ptn = 0;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07002907 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07002908 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002909 char *token = NULL;
2910 char *pname = NULL;
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07002911 char *sp;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002912 uint8_t lun = 0;
2913 bool lun_set = false;
Mayank Grover351a75e2017-05-30 20:06:08 +05302914 int current_active_slot = INVALID;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07002915
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07002916 token = strtok_r((char *)arg, ":", &sp);
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002917 pname = token;
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07002918 token = strtok_r(NULL, ":", &sp);
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002919 if(token)
2920 {
2921 lun = atoi(token);
2922 mmc_set_lun(lun);
2923 lun_set = true;
2924 }
2925
Mao Jinlong226f33a2014-07-04 17:24:10 +08002926 if (pname)
Greg Grisco6e754772011-06-23 12:19:39 -07002927 {
Channagoud Kadabiad259832015-05-29 11:14:17 -07002928 if (!strncmp(pname, "frp-unlock", strlen("frp-unlock")))
2929 {
2930 if (!aboot_frp_unlock(pname, data, sz))
2931 {
2932 fastboot_info("FRP unlock successful");
2933 fastboot_okay("");
2934 }
2935 else
2936 fastboot_fail("Secret key is invalid, please update the bootloader with secret key");
2937
2938 return;
2939 }
2940
Mao Jinlong226f33a2014-07-04 17:24:10 +08002941 if (!strcmp(pname, "partition"))
2942 {
2943 dprintf(INFO, "Attempt to write partition image.\n");
2944 if (write_partition(sz, (unsigned char *) data)) {
2945 fastboot_fail("failed to write partition");
Greg Grisco6e754772011-06-23 12:19:39 -07002946 return;
2947 }
Mayank Grover11ff9692018-01-11 11:54:49 +05302948 /* Re-publish partition table */
2949 publish_getvar_partition_info(part_info, partition_get_partition_count());
Mayank Grover351a75e2017-05-30 20:06:08 +05302950
2951 /* Rescan partition table to ensure we have multislot support*/
2952 if (partition_scan_for_multislot())
2953 {
2954 current_active_slot = partition_find_active_slot();
2955 dprintf(INFO, "Multislot supported: Slot %s active",
2956 (SUFFIX_SLOT(current_active_slot)));
2957 }
2958 partition_mark_active_slot(current_active_slot);
Greg Grisco6e754772011-06-23 12:19:39 -07002959 }
Mao Jinlong226f33a2014-07-04 17:24:10 +08002960 else
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002961 {
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002962#if VERIFIED_BOOT
2963 if(!strcmp(pname, KEYSTORE_PTN_NAME))
2964 {
2965 if(!device.is_unlocked)
2966 {
2967 fastboot_fail("unlock device to flash keystore");
2968 return;
2969 }
Gaurav Nebhwanic7313cc2015-12-15 22:25:04 +05302970 if(!boot_verify_validate_keystore((unsigned char *)data,sz))
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002971 {
2972 fastboot_fail("image is not a keystore file");
2973 return;
2974 }
2975 }
2976#endif
Mao Jinlong226f33a2014-07-04 17:24:10 +08002977 index = partition_get_index(pname);
2978 ptn = partition_get_offset(index);
2979 if(ptn == 0) {
2980 fastboot_fail("partition table doesn't exist");
2981 return;
2982 }
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002983
Mayank Grover351a75e2017-05-30 20:06:08 +05302984 if (!strncmp(pname, "boot", strlen("boot"))
2985 || !strcmp(pname, "recovery"))
2986 {
Mao Jinlong226f33a2014-07-04 17:24:10 +08002987 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
2988 fastboot_fail("image is not a boot image");
2989 return;
2990 }
Mayank Grover351a75e2017-05-30 20:06:08 +05302991
2992 /* Reset multislot_partition attributes in case of flashing boot */
2993 if (partition_multislot_is_supported())
2994 {
2995 partition_reset_attributes(index);
2996 }
Mao Jinlong226f33a2014-07-04 17:24:10 +08002997 }
2998
2999 if(!lun_set)
3000 {
3001 lun = partition_get_lun(index);
3002 mmc_set_lun(lun);
3003 }
3004
3005 size = partition_get_size(index);
Gaurav Nebhwani4d2389c2016-03-17 21:10:05 +05303006 if (ROUND_TO_PAGE(sz, mmc_blocksize_mask) > size) {
Mao Jinlong226f33a2014-07-04 17:24:10 +08003007 fastboot_fail("size too large");
3008 return;
3009 }
3010 else if (mmc_write(ptn , sz, (unsigned int *)data)) {
3011 fastboot_fail("flash write failure");
3012 return;
3013 }
Greg Grisco6e754772011-06-23 12:19:39 -07003014 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07003015 }
3016 fastboot_okay("");
3017 return;
3018}
3019
Ajay Dudanide984792015-03-02 09:57:41 -08003020void cmd_flash_meta_img(const char *arg, void *data, unsigned sz)
3021{
3022 int i, images;
3023 meta_header_t *meta_header;
3024 img_header_entry_t *img_header_entry;
Parth Dixit3e2d3032016-03-04 17:11:52 +05303025 /*End of the image address*/
3026 uintptr_t data_end;
3027
3028 if( (UINT_MAX - sz) > (uintptr_t)data )
3029 data_end = (uintptr_t)data + sz;
3030 else
3031 {
3032 fastboot_fail("Cannot flash: image header corrupt");
3033 return;
3034 }
3035
3036 if( data_end < ((uintptr_t)data + sizeof(meta_header_t)))
3037 {
3038 fastboot_fail("Cannot flash: image header corrupt");
3039 return;
3040 }
Ajay Dudanide984792015-03-02 09:57:41 -08003041
lijuang8ee21882016-04-19 16:57:11 +08003042 /* If device is locked:
3043 * Forbid to flash image to avoid the device to bypass the image
3044 * which with "any" name other than bootloader. Because it maybe
3045 * a meta package of all partitions.
3046 */
3047#if VERIFIED_BOOT
3048 if (target_build_variant_user()) {
3049 if (!device.is_unlocked) {
3050 fastboot_fail("Device is locked, meta image flashing is not allowed");
3051 return;
3052 }
Mayank Grover889be1b2017-09-12 20:12:23 +05303053
Mayank Grover912eaa62017-10-26 12:08:53 +05303054 if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05303055 !device.is_unlock_critical)
3056 {
lijuang8ee21882016-04-19 16:57:11 +08003057 fastboot_fail("Device is critical locked, Meta image flashing is not allowed");
3058 return;
3059 }
lijuang8ee21882016-04-19 16:57:11 +08003060 }
3061#endif
3062
Ajay Dudanide984792015-03-02 09:57:41 -08003063 meta_header = (meta_header_t*) data;
Parth Dixit3e2d3032016-03-04 17:11:52 +05303064 if( data_end < ((uintptr_t)data + meta_header->img_hdr_sz))
3065 {
3066 fastboot_fail("Cannot flash: image header corrupt");
3067 return;
3068 }
Ajay Dudanide984792015-03-02 09:57:41 -08003069 img_header_entry = (img_header_entry_t*) (data+sizeof(meta_header_t));
3070
3071 images = meta_header->img_hdr_sz / sizeof(img_header_entry_t);
3072
3073 for (i=0; i<images; i++) {
3074
3075 if((img_header_entry[i].ptn_name == NULL) ||
3076 (img_header_entry[i].start_offset == 0) ||
3077 (img_header_entry[i].size == 0))
3078 break;
Kishor PK49831502017-04-21 17:55:43 +05303079 if ((UINT_MAX - img_header_entry[i].start_offset) < (uintptr_t)data) {
3080 fastboot_fail("Integer overflow detected in start_offset of img");
3081 break;
3082 }
3083 else if ((UINT_MAX - (img_header_entry[i].start_offset + (uintptr_t)data)) < img_header_entry[i].size) {
3084 fastboot_fail("Integer overflow detected in size of img");
3085 break;
3086 }
Parth Dixit3e2d3032016-03-04 17:11:52 +05303087 if( data_end < ((uintptr_t)data + img_header_entry[i].start_offset
3088 + img_header_entry[i].size) )
3089 {
3090 fastboot_fail("Cannot flash: image size mismatch");
3091 break;
3092 }
3093
Ajay Dudanide984792015-03-02 09:57:41 -08003094 cmd_flash_mmc_img(img_header_entry[i].ptn_name,
3095 (void *) data + img_header_entry[i].start_offset,
3096 img_header_entry[i].size);
3097 }
3098
Channagoud Kadabiaafbbfc2015-03-04 17:52:56 -08003099 if (!strncmp(arg, "bootloader", strlen("bootloader")))
3100 {
3101 strlcpy(device.bootloader_version, TARGET(BOARD), MAX_VERSION_LEN);
3102 strlcat(device.bootloader_version, "-", MAX_VERSION_LEN);
3103 strlcat(device.bootloader_version, meta_header->img_version, MAX_VERSION_LEN);
3104 }
3105 else
3106 {
3107 strlcpy(device.radio_version, TARGET(BOARD), MAX_VERSION_LEN);
3108 strlcat(device.radio_version, "-", MAX_VERSION_LEN);
3109 strlcat(device.radio_version, meta_header->img_version, MAX_VERSION_LEN);
3110 }
3111
3112 write_device_info(&device);
Ajay Dudanide984792015-03-02 09:57:41 -08003113 fastboot_okay("");
3114 return;
3115}
3116
Ajay Dudani5c761132011-04-07 20:19:04 -07003117void cmd_flash_mmc_sparse_img(const char *arg, void *data, unsigned sz)
3118{
3119 unsigned int chunk;
wufeng.jiang813dc352015-06-02 23:02:46 -04003120 uint64_t chunk_data_sz;
Channagoud Kadabif627cf82013-09-09 14:08:20 -07003121 uint32_t *fill_buf = NULL;
3122 uint32_t fill_val;
Mayank Grover4f50bba2017-07-19 18:17:08 +05303123 uint32_t blk_sz_actual = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07003124 sparse_header_t *sparse_header;
3125 chunk_header_t *chunk_header;
Ajay Dudaniab18f022011-05-12 14:39:22 -07003126 uint32_t total_blocks = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07003127 unsigned long long ptn = 0;
Channagoud Kadabi65b91002011-10-11 17:34:33 +05303128 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07003129 int index = INVALID_PTN;
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05303130 uint32_t i;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003131 uint8_t lun = 0;
vijay kumar800255e2015-04-24 20:26:19 +05303132 /*End of the sparse image address*/
Parth Dixit64b1a482016-03-07 16:31:26 +05303133 uintptr_t data_end = (uintptr_t)data + sz;
Ajay Dudani5c761132011-04-07 20:19:04 -07003134
Kinson Chikf1a43512011-07-14 11:28:39 -07003135 index = partition_get_index(arg);
3136 ptn = partition_get_offset(index);
3137 if(ptn == 0) {
Ajay Dudani5c761132011-04-07 20:19:04 -07003138 fastboot_fail("partition table doesn't exist");
3139 return;
3140 }
3141
Channagoud Kadabi65b91002011-10-11 17:34:33 +05303142 size = partition_get_size(index);
Channagoud Kadabi65b91002011-10-11 17:34:33 +05303143
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003144 lun = partition_get_lun(index);
3145 mmc_set_lun(lun);
3146
vijay kumar800255e2015-04-24 20:26:19 +05303147 if (sz < sizeof(sparse_header_t)) {
3148 fastboot_fail("size too low");
3149 return;
3150 }
3151
Ajay Dudani5c761132011-04-07 20:19:04 -07003152 /* Read and skip over sparse image header */
3153 sparse_header = (sparse_header_t *) data;
vijay kumar800255e2015-04-24 20:26:19 +05303154
Mayank Grover48bd9bb2017-07-19 12:04:16 +05303155 if (!sparse_header->blk_sz || (sparse_header->blk_sz % 4)){
3156 fastboot_fail("Invalid block size\n");
3157 return;
3158 }
3159
vijay kumar1321f342015-03-27 12:13:42 +05303160 if (((uint64_t)sparse_header->total_blks * (uint64_t)sparse_header->blk_sz) > size) {
Ajay Dudani876b3282012-12-21 14:12:17 -08003161 fastboot_fail("size too large");
3162 return;
3163 }
3164
vijay kumarde4fcf62015-04-23 13:05:49 +05303165 data += sizeof(sparse_header_t);
vijay kumar800255e2015-04-24 20:26:19 +05303166
Parth Dixit64b1a482016-03-07 16:31:26 +05303167 if (data_end < (uintptr_t)data) {
vijay kumar800255e2015-04-24 20:26:19 +05303168 fastboot_fail("buffer overreads occured due to invalid sparse header");
3169 return;
3170 }
3171
vijay kumarde4fcf62015-04-23 13:05:49 +05303172 if(sparse_header->file_hdr_sz != sizeof(sparse_header_t))
Ajay Dudani5c761132011-04-07 20:19:04 -07003173 {
vijay kumarde4fcf62015-04-23 13:05:49 +05303174 fastboot_fail("sparse header size mismatch");
3175 return;
Ajay Dudani5c761132011-04-07 20:19:04 -07003176 }
3177
Ajay Dudanib06c05f2011-05-12 14:46:10 -07003178 dprintf (SPEW, "=== Sparse Image Header ===\n");
3179 dprintf (SPEW, "magic: 0x%x\n", sparse_header->magic);
3180 dprintf (SPEW, "major_version: 0x%x\n", sparse_header->major_version);
3181 dprintf (SPEW, "minor_version: 0x%x\n", sparse_header->minor_version);
3182 dprintf (SPEW, "file_hdr_sz: %d\n", sparse_header->file_hdr_sz);
3183 dprintf (SPEW, "chunk_hdr_sz: %d\n", sparse_header->chunk_hdr_sz);
3184 dprintf (SPEW, "blk_sz: %d\n", sparse_header->blk_sz);
3185 dprintf (SPEW, "total_blks: %d\n", sparse_header->total_blks);
3186 dprintf (SPEW, "total_chunks: %d\n", sparse_header->total_chunks);
Ajay Dudani5c761132011-04-07 20:19:04 -07003187
3188 /* Start processing chunks */
3189 for (chunk=0; chunk<sparse_header->total_chunks; chunk++)
3190 {
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05303191 /* Make sure the total image size does not exceed the partition size */
3192 if(((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz) >= size) {
3193 fastboot_fail("size too large");
3194 return;
3195 }
Ajay Dudani5c761132011-04-07 20:19:04 -07003196 /* Read and skip over chunk header */
3197 chunk_header = (chunk_header_t *) data;
3198 data += sizeof(chunk_header_t);
3199
Parth Dixit64b1a482016-03-07 16:31:26 +05303200 if (data_end < (uintptr_t)data) {
vijay kumar800255e2015-04-24 20:26:19 +05303201 fastboot_fail("buffer overreads occured due to invalid sparse header");
3202 return;
3203 }
3204
Ajay Dudani5c761132011-04-07 20:19:04 -07003205 dprintf (SPEW, "=== Chunk Header ===\n");
3206 dprintf (SPEW, "chunk_type: 0x%x\n", chunk_header->chunk_type);
3207 dprintf (SPEW, "chunk_data_sz: 0x%x\n", chunk_header->chunk_sz);
3208 dprintf (SPEW, "total_size: 0x%x\n", chunk_header->total_sz);
3209
vijay kumar800255e2015-04-24 20:26:19 +05303210 if(sparse_header->chunk_hdr_sz != sizeof(chunk_header_t))
Ajay Dudani5c761132011-04-07 20:19:04 -07003211 {
vijay kumar800255e2015-04-24 20:26:19 +05303212 fastboot_fail("chunk header size mismatch");
3213 return;
Ajay Dudani5c761132011-04-07 20:19:04 -07003214 }
3215
wufeng.jiang813dc352015-06-02 23:02:46 -04003216 chunk_data_sz = (uint64_t)sparse_header->blk_sz * chunk_header->chunk_sz;
3217
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05303218 /* Make sure that the chunk size calculated from sparse image does not
3219 * exceed partition size
3220 */
3221 if ((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz + chunk_data_sz > size)
3222 {
3223 fastboot_fail("Chunk data size exceeds partition size");
3224 return;
3225 }
3226
Ajay Dudani5c761132011-04-07 20:19:04 -07003227 switch (chunk_header->chunk_type)
3228 {
3229 case CHUNK_TYPE_RAW:
wufeng.jiang813dc352015-06-02 23:02:46 -04003230 if((uint64_t)chunk_header->total_sz != ((uint64_t)sparse_header->chunk_hdr_sz +
Ajay Dudani5c761132011-04-07 20:19:04 -07003231 chunk_data_sz))
3232 {
3233 fastboot_fail("Bogus chunk size for chunk type Raw");
3234 return;
3235 }
3236
Parth Dixit64b1a482016-03-07 16:31:26 +05303237 if (data_end < (uintptr_t)data + chunk_data_sz) {
vijay kumar800255e2015-04-24 20:26:19 +05303238 fastboot_fail("buffer overreads occured due to invalid sparse header");
3239 return;
3240 }
3241
wufeng.jiang813dc352015-06-02 23:02:46 -04003242 /* chunk_header->total_sz is uint32,So chunk_data_sz is now less than 2^32
3243 otherwise it will return in the line above
3244 */
Ajay Dudaniab18f022011-05-12 14:39:22 -07003245 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
wufeng.jiang813dc352015-06-02 23:02:46 -04003246 (uint32_t)chunk_data_sz,
Ajay Dudaniab18f022011-05-12 14:39:22 -07003247 (unsigned int*)data))
Ajay Dudani5c761132011-04-07 20:19:04 -07003248 {
3249 fastboot_fail("flash write failure");
3250 return;
3251 }
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05303252 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
3253 fastboot_fail("Bogus size for RAW chunk type");
3254 return;
3255 }
Ajay Dudani5c761132011-04-07 20:19:04 -07003256 total_blocks += chunk_header->chunk_sz;
wufeng.jiang813dc352015-06-02 23:02:46 -04003257 data += (uint32_t)chunk_data_sz;
Ajay Dudani5c761132011-04-07 20:19:04 -07003258 break;
3259
Channagoud Kadabif627cf82013-09-09 14:08:20 -07003260 case CHUNK_TYPE_FILL:
3261 if(chunk_header->total_sz != (sparse_header->chunk_hdr_sz +
3262 sizeof(uint32_t)))
3263 {
3264 fastboot_fail("Bogus chunk size for chunk type FILL");
3265 return;
3266 }
3267
Mayank Grover4f50bba2017-07-19 18:17:08 +05303268 blk_sz_actual = ROUNDUP(sparse_header->blk_sz, CACHE_LINE);
3269 /* Integer overflow detected */
3270 if (blk_sz_actual < sparse_header->blk_sz)
3271 {
3272 fastboot_fail("Invalid block size");
3273 return;
3274 }
3275
3276 fill_buf = (uint32_t *)memalign(CACHE_LINE, blk_sz_actual);
Channagoud Kadabif627cf82013-09-09 14:08:20 -07003277 if (!fill_buf)
3278 {
3279 fastboot_fail("Malloc failed for: CHUNK_TYPE_FILL");
3280 return;
3281 }
3282
Parth Dixit64b1a482016-03-07 16:31:26 +05303283 if (data_end < (uintptr_t)data + sizeof(uint32_t)) {
vijay kumar800255e2015-04-24 20:26:19 +05303284 fastboot_fail("buffer overreads occured due to invalid sparse header");
Gaurav Nebhwani163cbf82016-03-17 21:21:27 +05303285 free(fill_buf);
vijay kumar800255e2015-04-24 20:26:19 +05303286 return;
3287 }
Channagoud Kadabif627cf82013-09-09 14:08:20 -07003288 fill_val = *(uint32_t *)data;
3289 data = (char *) data + sizeof(uint32_t);
Channagoud Kadabif627cf82013-09-09 14:08:20 -07003290
3291 for (i = 0; i < (sparse_header->blk_sz / sizeof(fill_val)); i++)
3292 {
3293 fill_buf[i] = fill_val;
3294 }
3295
Gaurav Nebhwanie94cbe12016-03-17 21:16:34 +05303296 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz))
3297 {
3298 fastboot_fail("bogus size for chunk FILL type");
3299 free(fill_buf);
3300 return;
3301 }
3302
wufeng.jiang813dc352015-06-02 23:02:46 -04003303 for (i = 0; i < chunk_header->chunk_sz; i++)
Channagoud Kadabif627cf82013-09-09 14:08:20 -07003304 {
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05303305 /* Make sure that the data written to partition does not exceed partition size */
3306 if ((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz + sparse_header->blk_sz > size)
3307 {
3308 fastboot_fail("Chunk data size for fill type exceeds partition size");
Gaurav Nebhwani163cbf82016-03-17 21:21:27 +05303309 free(fill_buf);
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05303310 return;
3311 }
3312
Channagoud Kadabif627cf82013-09-09 14:08:20 -07003313 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
3314 sparse_header->blk_sz,
3315 fill_buf))
3316 {
3317 fastboot_fail("flash write failure");
3318 free(fill_buf);
3319 return;
3320 }
3321
3322 total_blocks++;
3323 }
3324
3325 free(fill_buf);
3326 break;
3327
Ajay Dudani5c761132011-04-07 20:19:04 -07003328 case CHUNK_TYPE_DONT_CARE:
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05303329 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
3330 fastboot_fail("bogus size for chunk DONT CARE type");
3331 return;
3332 }
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07003333 total_blocks += chunk_header->chunk_sz;
3334 break;
3335
Ajay Dudani5c761132011-04-07 20:19:04 -07003336 case CHUNK_TYPE_CRC:
3337 if(chunk_header->total_sz != sparse_header->chunk_hdr_sz)
3338 {
wufeng.jiang813dc352015-06-02 23:02:46 -04003339 fastboot_fail("Bogus chunk size for chunk type CRC");
Ajay Dudani5c761132011-04-07 20:19:04 -07003340 return;
3341 }
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05303342 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
3343 fastboot_fail("bogus size for chunk CRC type");
3344 return;
3345 }
Ajay Dudani5c761132011-04-07 20:19:04 -07003346 total_blocks += chunk_header->chunk_sz;
Parth Dixit64b1a482016-03-07 16:31:26 +05303347 if ((uintptr_t)data > UINT_MAX - chunk_data_sz) {
vijay kumar800255e2015-04-24 20:26:19 +05303348 fastboot_fail("integer overflow occured");
3349 return;
3350 }
wufeng.jiang813dc352015-06-02 23:02:46 -04003351 data += (uint32_t)chunk_data_sz;
Parth Dixit64b1a482016-03-07 16:31:26 +05303352 if (data_end < (uintptr_t)data) {
vijay kumar800255e2015-04-24 20:26:19 +05303353 fastboot_fail("buffer overreads occured due to invalid sparse header");
3354 return;
3355 }
Ajay Dudani5c761132011-04-07 20:19:04 -07003356 break;
3357
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07003358 default:
Channagoud Kadabif627cf82013-09-09 14:08:20 -07003359 dprintf(CRITICAL, "Unkown chunk type: %x\n",chunk_header->chunk_type);
Ajay Dudani5c761132011-04-07 20:19:04 -07003360 fastboot_fail("Unknown chunk type");
3361 return;
3362 }
3363 }
3364
Ajay Dudani0c6927b2011-05-18 11:12:16 -07003365 dprintf(INFO, "Wrote %d blocks, expected to write %d blocks\n",
3366 total_blocks, sparse_header->total_blks);
3367
3368 if(total_blocks != sparse_header->total_blks)
3369 {
3370 fastboot_fail("sparse image write failure");
3371 }
Ajay Dudani5c761132011-04-07 20:19:04 -07003372
3373 fastboot_okay("");
3374 return;
3375}
3376
3377void cmd_flash_mmc(const char *arg, void *data, unsigned sz)
3378{
3379 sparse_header_t *sparse_header;
Ajay Dudanide984792015-03-02 09:57:41 -08003380 meta_header_t *meta_header;
Ajay Dudani5c761132011-04-07 20:19:04 -07003381
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07003382#ifdef SSD_ENABLE
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -08003383 /* 8 Byte Magic + 2048 Byte xml + Encrypted Data */
3384 unsigned int *magic_number = (unsigned int *) data;
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08003385 int ret=0;
3386 uint32 major_version=0;
3387 uint32 minor_version=0;
3388
3389 ret = scm_svc_version(&major_version,&minor_version);
3390 if(!ret)
3391 {
3392 if(major_version >= 2)
3393 {
Amir Samuelovbb65ce02013-05-05 12:20:18 +03003394 if( !strcmp(arg, "ssd") || !strcmp(arg, "tqs") )
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08003395 {
3396 ret = encrypt_scm((uint32 **) &data, &sz);
3397 if (ret != 0) {
3398 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
3399 return;
3400 }
3401
Amir Samuelovbb65ce02013-05-05 12:20:18 +03003402 /* Protect only for SSD */
3403 if (!strcmp(arg, "ssd")) {
3404 ret = scm_protect_keystore((uint32 *) data, sz);
3405 if (ret != 0) {
3406 dprintf(CRITICAL, "ERROR: scm_protect_keystore Failed\n");
3407 return;
3408 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08003409 }
3410 }
3411 else
3412 {
3413 ret = decrypt_scm_v2((uint32 **) &data, &sz);
3414 if(ret != 0)
3415 {
3416 dprintf(CRITICAL,"ERROR: Decryption Failure\n");
3417 return;
3418 }
3419 }
3420 }
3421 else
3422 {
3423 if (magic_number[0] == DECRYPT_MAGIC_0 &&
3424 magic_number[1] == DECRYPT_MAGIC_1)
3425 {
3426 ret = decrypt_scm((uint32 **) &data, &sz);
3427 if (ret != 0) {
3428 dprintf(CRITICAL, "ERROR: Invalid secure image\n");
3429 return;
3430 }
3431 }
3432 else if (magic_number[0] == ENCRYPT_MAGIC_0 &&
3433 magic_number[1] == ENCRYPT_MAGIC_1)
3434 {
3435 ret = encrypt_scm((uint32 **) &data, &sz);
3436 if (ret != 0) {
3437 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
3438 return;
3439 }
3440 }
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07003441 }
3442 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08003443 else
Neeti Desai127b9e02012-03-20 16:11:23 -07003444 {
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08003445 dprintf(CRITICAL,"INVALID SVC Version\n");
3446 return;
Neeti Desai127b9e02012-03-20 16:11:23 -07003447 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08003448#endif /* SSD_ENABLE */
Neeti Desai127b9e02012-03-20 16:11:23 -07003449
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003450#if VERIFIED_BOOT
Channagoud Kadabi35297672015-06-13 11:09:49 -07003451 if (target_build_variant_user())
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003452 {
lijuang511a2b52015-08-14 20:50:51 +08003453 /* if device is locked:
3454 * common partition will not allow to be flashed
3455 * critical partition will allow to flash image.
3456 */
3457 if(!device.is_unlocked && !critical_flash_allowed(arg)) {
3458 fastboot_fail("Partition flashing is not allowed");
3459 return;
3460 }
Mayank Grover889be1b2017-09-12 20:12:23 +05303461
lijuang511a2b52015-08-14 20:50:51 +08003462 /* if device critical is locked:
3463 * common partition will allow to be flashed
3464 * critical partition will not allow to flash image.
3465 */
Mayank Grover912eaa62017-10-26 12:08:53 +05303466 if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05303467 !device.is_unlock_critical &&
3468 critical_flash_allowed(arg)) {
3469 fastboot_fail("Critical partition flashing is not allowed");
3470 return;
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003471 }
3472 }
3473#endif
3474
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07003475 sparse_header = (sparse_header_t *) data;
Ajay Dudanide984792015-03-02 09:57:41 -08003476 meta_header = (meta_header_t *) data;
3477 if (sparse_header->magic == SPARSE_HEADER_MAGIC)
Ajay Dudani5c761132011-04-07 20:19:04 -07003478 cmd_flash_mmc_sparse_img(arg, data, sz);
Ajay Dudanide984792015-03-02 09:57:41 -08003479 else if (meta_header->magic == META_HEADER_MAGIC)
3480 cmd_flash_meta_img(arg, data, sz);
3481 else
3482 cmd_flash_mmc_img(arg, data, sz);
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07003483
3484#if VERIFIED_BOOT
Mayank Grover912eaa62017-10-26 12:08:53 +05303485 if (VB_M <= target_get_vb_version() &&
Mayank Grover889be1b2017-09-12 20:12:23 +05303486 (!strncmp(arg, "system", 6)) &&
3487 !device.verity_mode)
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07003488 // reset dm_verity mode to enforcing
3489 device.verity_mode = 1;
3490 write_device_info(&device);
Parth Dixitddbc7352015-10-18 03:13:31 +05303491#endif
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07003492
Ajay Dudani5c761132011-04-07 20:19:04 -07003493 return;
3494}
3495
Tanya Brokhman84eaaf62015-02-24 21:57:06 +02003496void cmd_updatevol(const char *vol_name, void *data, unsigned sz)
3497{
3498 struct ptentry *sys_ptn;
3499 struct ptable *ptable;
3500
3501 ptable = flash_get_ptable();
3502 if (ptable == NULL) {
3503 fastboot_fail("partition table doesn't exist");
3504 return;
3505 }
3506
3507 sys_ptn = ptable_find(ptable, "system");
3508 if (sys_ptn == NULL) {
3509 fastboot_fail("system partition not found");
3510 return;
3511 }
3512
3513 sz = ROUND_TO_PAGE(sz, page_mask);
3514 if (update_ubi_vol(sys_ptn, vol_name, data, sz))
3515 fastboot_fail("update_ubi_vol failed");
3516 else
3517 fastboot_okay("");
3518}
3519
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003520void cmd_flash_nand(const char *arg, void *data, unsigned sz)
Dima Zavin214cc642009-01-26 11:16:21 -08003521{
3522 struct ptentry *ptn;
3523 struct ptable *ptable;
3524 unsigned extra = 0;
Gaurav Nebhwanid9730712016-05-06 14:28:29 +05303525 uint64_t partition_size = 0;
Vijay Kumar Pendotic6f22a72017-05-19 22:44:34 +05303526 unsigned bytes_to_round_page = 0;
3527 unsigned rounded_size = 0;
Dima Zavin214cc642009-01-26 11:16:21 -08003528
Kishor PK38ed93d2017-04-25 14:19:26 +05303529 if((uintptr_t)data > (UINT_MAX - sz)) {
3530 fastboot_fail("Cannot flash: image header corrupt");
3531 return;
3532 }
3533
Dima Zavin214cc642009-01-26 11:16:21 -08003534 ptable = flash_get_ptable();
3535 if (ptable == NULL) {
3536 fastboot_fail("partition table doesn't exist");
3537 return;
3538 }
3539
3540 ptn = ptable_find(ptable, arg);
3541 if (ptn == NULL) {
Tanya Brokhman84eaaf62015-02-24 21:57:06 +02003542 dprintf(INFO, "unknown partition name (%s). Trying updatevol\n",
3543 arg);
3544 cmd_updatevol(arg, data, sz);
Dima Zavin214cc642009-01-26 11:16:21 -08003545 return;
3546 }
3547
3548 if (!strcmp(ptn->name, "boot") || !strcmp(ptn->name, "recovery")) {
Kishor PK38ed93d2017-04-25 14:19:26 +05303549 if((sz > BOOT_MAGIC_SIZE) && (!memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE))) {
3550 dprintf(INFO, "Verified the BOOT_MAGIC in image header \n");
3551 } else {
3552 fastboot_fail("Image is not a boot image");
Dima Zavin214cc642009-01-26 11:16:21 -08003553 return;
3554 }
3555 }
3556
Amol Jadi5c61a952012-05-04 17:05:35 -07003557 if (!strcmp(ptn->name, "system")
Deepa Dinamani13e32c42012-03-12 14:34:17 -07003558 || !strcmp(ptn->name, "userdata")
3559 || !strcmp(ptn->name, "persist")
Sundarajan Srinivasanb063a852013-11-19 14:02:27 -08003560 || !strcmp(ptn->name, "recoveryfs")
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -08003561 || !strcmp(ptn->name, "modem"))
Tanya Brokhman1c94f1a2015-02-15 09:05:03 +02003562 extra = 1;
Kishor PK38ed93d2017-04-25 14:19:26 +05303563 else {
Vijay Kumar Pendotic6f22a72017-05-19 22:44:34 +05303564 rounded_size = ROUNDUP(sz, page_size);
3565 bytes_to_round_page = rounded_size - sz;
3566 if (bytes_to_round_page) {
3567 if (((uintptr_t)data + sz ) > (UINT_MAX - bytes_to_round_page)) {
3568 fastboot_fail("Integer overflow detected");
3569 return;
3570 }
3571 if (((uintptr_t)data + sz + bytes_to_round_page) >
3572 ((uintptr_t)target_get_scratch_address() + target_get_max_flash_size())) {
3573 fastboot_fail("Buffer size is not aligned to page_size");
3574 return;
3575 }
3576 else {
3577 memset(data + sz, 0, bytes_to_round_page);
3578 sz = rounded_size;
3579 }
Kishor PK38ed93d2017-04-25 14:19:26 +05303580 }
Gaurav Nebhwanid9730712016-05-06 14:28:29 +05303581 }
3582
Kishor PK38ed93d2017-04-25 14:19:26 +05303583 /*Checking partition_size for the possible integer overflow */
3584 partition_size = validate_partition_size(ptn);
3585
Gaurav Nebhwanid9730712016-05-06 14:28:29 +05303586 if (sz > partition_size) {
3587 fastboot_fail("Image size too large");
3588 return;
3589 }
3590
Dima Zavin214cc642009-01-26 11:16:21 -08003591 dprintf(INFO, "writing %d bytes to '%s'\n", sz, ptn->name);
Mayank Grover6cde9352017-06-06 18:45:23 +05303592 if ((sz > UBI_EC_HDR_SIZE) &&
3593 (!memcmp((void *)data, UBI_MAGIC, UBI_MAGIC_SIZE))) {
Tanya Brokhman1c94f1a2015-02-15 09:05:03 +02003594 if (flash_ubi_img(ptn, data, sz)) {
3595 fastboot_fail("flash write failure");
3596 return;
3597 }
3598 } else {
3599 if (flash_write(ptn, extra, data, sz)) {
3600 fastboot_fail("flash write failure");
3601 return;
3602 }
Dima Zavin214cc642009-01-26 11:16:21 -08003603 }
3604 dprintf(INFO, "partition '%s' updated\n", ptn->name);
3605 fastboot_okay("");
3606}
3607
Kishor PK38ed93d2017-04-25 14:19:26 +05303608
3609static inline uint64_t validate_partition_size(struct ptentry *ptn)
3610{
3611 if (ptn->length && flash_num_pages_per_blk() && page_size) {
3612 if ((ptn->length < ( UINT_MAX / flash_num_pages_per_blk())) && ((ptn->length * flash_num_pages_per_blk()) < ( UINT_MAX / page_size))) {
3613 return ptn->length * flash_num_pages_per_blk() * page_size;
3614 }
3615 }
3616 return 0;
3617}
3618
3619
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003620void cmd_flash(const char *arg, void *data, unsigned sz)
3621{
3622 if(target_is_emmc_boot())
3623 cmd_flash_mmc(arg, data, sz);
3624 else
3625 cmd_flash_nand(arg, data, sz);
3626}
3627
Dima Zavin214cc642009-01-26 11:16:21 -08003628void cmd_continue(const char *arg, void *data, unsigned sz)
3629{
3630 fastboot_okay("");
Amol Jadi7c4316c2013-10-07 14:19:26 -07003631 fastboot_stop();
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003632
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003633 if (target_is_emmc_boot())
3634 {
lijuanga40d6302015-07-20 20:10:13 +08003635#if FBCON_DISPLAY_MSG
lijuangde34d502016-02-26 16:04:50 +08003636 /* Exit keys' detection thread firstly */
3637 exit_menu_keys_detection();
lijuanga40d6302015-07-20 20:10:13 +08003638#endif
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003639 boot_linux_from_mmc();
3640 }
3641 else
3642 {
3643 boot_linux_from_flash();
3644 }
Dima Zavin214cc642009-01-26 11:16:21 -08003645}
3646
Chandan Uddaraju94183c02010-01-15 15:13:59 -08003647void cmd_reboot(const char *arg, void *data, unsigned sz)
3648{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003649 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08003650 fastboot_okay("");
3651 reboot_device(0);
3652}
3653
Mayank Grover351a75e2017-05-30 20:06:08 +05303654void cmd_set_active(const char *arg, void *data, unsigned sz)
3655{
Mayank Grover6ccc1c92017-07-04 17:36:46 +05303656 char *p, *sp = NULL;
Mayank Grover351a75e2017-05-30 20:06:08 +05303657 unsigned i,current_active_slot;
3658 const char *current_slot_suffix;
3659
3660 if (!partition_multislot_is_supported())
3661 {
3662 fastboot_fail("Command not supported");
3663 return;
3664 }
3665
3666 if (arg)
3667 {
Mayank Grover6ccc1c92017-07-04 17:36:46 +05303668 p = strtok_r((char *)arg, ":", &sp);
Mayank Grover5eb5f692017-07-19 20:16:04 +05303669 if (p)
Mayank Grover351a75e2017-05-30 20:06:08 +05303670 {
3671 current_active_slot = partition_find_active_slot();
3672
3673 /* Check if trying to make curent slot active */
3674 current_slot_suffix = SUFFIX_SLOT(current_active_slot);
Mayank Grover6ccc1c92017-07-04 17:36:46 +05303675 current_slot_suffix = strtok_r((char *)current_slot_suffix,
3676 (char *)suffix_delimiter, &sp);
3677
Mayank Grover5eb5f692017-07-19 20:16:04 +05303678 if (current_slot_suffix &&
3679 !strncmp(p, current_slot_suffix, strlen(current_slot_suffix)))
Mayank Grover351a75e2017-05-30 20:06:08 +05303680 {
3681 fastboot_okay("Slot already set active");
3682 return;
3683 }
3684 else
3685 {
3686 for (i = 0; i < AB_SUPPORTED_SLOTS; i++)
3687 {
3688 current_slot_suffix = SUFFIX_SLOT(i);
Mayank Grover6ccc1c92017-07-04 17:36:46 +05303689 current_slot_suffix = strtok_r((char *)current_slot_suffix,
3690 (char *)suffix_delimiter, &sp);
Mayank Grover5eb5f692017-07-19 20:16:04 +05303691 if (current_slot_suffix &&
3692 !strncmp(p, current_slot_suffix, strlen(current_slot_suffix)))
Mayank Grover351a75e2017-05-30 20:06:08 +05303693 {
3694 partition_switch_slots(current_active_slot, i);
3695 publish_getvar_multislot_vars();
3696 fastboot_okay("");
3697 return;
3698 }
3699 }
3700 }
3701 }
3702 }
3703 fastboot_fail("Invalid slot suffix.");
3704 return;
3705}
3706
Chandan Uddaraju94183c02010-01-15 15:13:59 -08003707void cmd_reboot_bootloader(const char *arg, void *data, unsigned sz)
3708{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003709 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08003710 fastboot_okay("");
3711 reboot_device(FASTBOOT_MODE);
3712}
3713
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003714void cmd_oem_enable_charger_screen(const char *arg, void *data, unsigned size)
3715{
3716 dprintf(INFO, "Enabling charger screen check\n");
3717 device.charger_screen_enabled = 1;
3718 write_device_info(&device);
3719 fastboot_okay("");
3720}
3721
3722void cmd_oem_disable_charger_screen(const char *arg, void *data, unsigned size)
3723{
3724 dprintf(INFO, "Disabling charger screen check\n");
3725 device.charger_screen_enabled = 0;
3726 write_device_info(&device);
3727 fastboot_okay("");
3728}
3729
lijuanga25d8bb2015-09-16 13:11:52 +08003730void cmd_oem_off_mode_charger(const char *arg, void *data, unsigned size)
3731{
3732 char *p = NULL;
3733 const char *delim = " \t\n\r";
Parth Dixit407f15b2016-01-07 14:47:37 +05303734 char *sp;
lijuanga25d8bb2015-09-16 13:11:52 +08003735
3736 if (arg) {
Parth Dixit407f15b2016-01-07 14:47:37 +05303737 p = strtok_r((char *)arg, delim, &sp);
lijuanga25d8bb2015-09-16 13:11:52 +08003738 if (p) {
3739 if (!strncmp(p, "0", 1)) {
3740 device.charger_screen_enabled = 0;
3741 } else if (!strncmp(p, "1", 1)) {
3742 device.charger_screen_enabled = 1;
3743 }
3744 }
3745 }
3746
3747 /* update charger_screen_enabled value for getvar
3748 * command
3749 */
3750 snprintf(charger_screen_enabled, MAX_RSP_SIZE, "%d",
3751 device.charger_screen_enabled);
3752
3753 write_device_info(&device);
3754 fastboot_okay("");
3755}
3756
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05303757void cmd_oem_select_display_panel(const char *arg, void *data, unsigned size)
3758{
3759 dprintf(INFO, "Selecting display panel %s\n", arg);
3760 if (arg)
3761 strlcpy(device.display_panel, arg,
3762 sizeof(device.display_panel));
3763 write_device_info(&device);
3764 fastboot_okay("");
3765}
3766
Shashank Mittal162244e2011-08-08 19:01:25 -07003767void cmd_oem_unlock(const char *arg, void *data, unsigned sz)
3768{
lijuang4ece1e72015-08-14 21:02:36 +08003769 set_device_unlock(UNLOCK, TRUE);
vijay kumarc65876c2015-04-24 13:29:16 +05303770}
3771
3772void cmd_oem_unlock_go(const char *arg, void *data, unsigned sz)
3773{
lijuang4ece1e72015-08-14 21:02:36 +08003774 if(!device.is_unlocked) {
vijay kumarc65876c2015-04-24 13:29:16 +05303775 if(!is_allow_unlock) {
3776 fastboot_fail("oem unlock is not allowed");
3777 return;
3778 }
3779
lijuang4ece1e72015-08-14 21:02:36 +08003780 set_device_unlock_value(UNLOCK, TRUE);
vijay kumarc65876c2015-04-24 13:29:16 +05303781
lijuang4ece1e72015-08-14 21:02:36 +08003782 /* wipe data */
vijay kumarc65876c2015-04-24 13:29:16 +05303783 struct recovery_message msg;
Kishor PK60a68212017-05-08 16:55:57 +05303784 memset(&msg, 0, sizeof(msg));
vijay kumarc65876c2015-04-24 13:29:16 +05303785 snprintf(msg.recovery, sizeof(msg.recovery), "recovery\n--wipe_data");
3786 write_misc(0, &msg, sizeof(msg));
3787
3788 fastboot_okay("");
3789 reboot_device(RECOVERY_MODE);
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003790 }
3791 fastboot_okay("");
3792}
3793
Channagoud Kadabiad259832015-05-29 11:14:17 -07003794static int aboot_frp_unlock(char *pname, void *data, unsigned sz)
3795{
Mayank Grover70f8b0e2017-05-17 11:49:00 +05303796 int ret=1;
3797 bool authentication_success=false;
Channagoud Kadabiad259832015-05-29 11:14:17 -07003798
Mayank Grover70f8b0e2017-05-17 11:49:00 +05303799 /*
3800 Authentication method not implemented.
3801
3802 OEM to implement, authentication system which on successful validataion,
3803 calls write_allow_oem_unlock() with is_allow_unlock.
3804 */
3805#if 0
3806 authentication_success = oem_specific_auth_mthd();
3807#endif
3808
3809 if (authentication_success)
Channagoud Kadabiad259832015-05-29 11:14:17 -07003810 {
Mayank Grover70f8b0e2017-05-17 11:49:00 +05303811 is_allow_unlock = true;
3812 write_allow_oem_unlock(is_allow_unlock);
3813 ret = 0;
Channagoud Kadabiad259832015-05-29 11:14:17 -07003814 }
3815 return ret;
3816}
3817
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003818void cmd_oem_lock(const char *arg, void *data, unsigned sz)
3819{
lijuang4ece1e72015-08-14 21:02:36 +08003820 set_device_unlock(UNLOCK, FALSE);
Shashank Mittal162244e2011-08-08 19:01:25 -07003821}
3822
Shashank Mittala0032282011-08-26 14:50:11 -07003823void cmd_oem_devinfo(const char *arg, void *data, unsigned sz)
3824{
lijuang511a2b52015-08-14 20:50:51 +08003825 char response[MAX_RSP_SIZE];
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003826 snprintf(response, sizeof(response), "\tDevice tampered: %s", (device.is_tampered ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07003827 fastboot_info(response);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003828 snprintf(response, sizeof(response), "\tDevice unlocked: %s", (device.is_unlocked ? "true" : "false"));
3829 fastboot_info(response);
Mayank Grover889be1b2017-09-12 20:12:23 +05303830#if VERIFIED_BOOT
Mayank Grover912eaa62017-10-26 12:08:53 +05303831 if (VB_M <= target_get_vb_version())
Mayank Grover889be1b2017-09-12 20:12:23 +05303832 {
3833 snprintf(response, sizeof(response), "\tDevice critical unlocked: %s",
3834 (device.is_unlock_critical ? "true" : "false"));
3835 fastboot_info(response);
3836 }
Parth Dixitddbc7352015-10-18 03:13:31 +05303837#endif
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003838 snprintf(response, sizeof(response), "\tCharger screen enabled: %s", (device.charger_screen_enabled ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07003839 fastboot_info(response);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05303840 snprintf(response, sizeof(response), "\tDisplay panel: %s", (device.display_panel));
3841 fastboot_info(response);
Shashank Mittala0032282011-08-26 14:50:11 -07003842 fastboot_okay("");
3843}
3844
lijuang511a2b52015-08-14 20:50:51 +08003845void cmd_flashing_get_unlock_ability(const char *arg, void *data, unsigned sz)
3846{
3847 char response[MAX_RSP_SIZE];
3848 snprintf(response, sizeof(response), "\tget_unlock_ability: %d", is_allow_unlock);
3849 fastboot_info(response);
3850 fastboot_okay("");
3851}
3852
3853void cmd_flashing_lock_critical(const char *arg, void *data, unsigned sz)
3854{
lijuang4ece1e72015-08-14 21:02:36 +08003855 set_device_unlock(UNLOCK_CRITICAL, FALSE);
lijuang511a2b52015-08-14 20:50:51 +08003856}
3857
3858void cmd_flashing_unlock_critical(const char *arg, void *data, unsigned sz)
3859{
lijuang4ece1e72015-08-14 21:02:36 +08003860 set_device_unlock(UNLOCK_CRITICAL, TRUE);
lijuang511a2b52015-08-14 20:50:51 +08003861}
3862
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07003863void cmd_preflash(const char *arg, void *data, unsigned sz)
3864{
3865 fastboot_okay("");
3866}
3867
Mao Flynn7b379f32015-04-20 00:28:30 +08003868static uint8_t logo_header[LOGO_IMG_HEADER_SIZE];
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303869
Mao Flynn7b379f32015-04-20 00:28:30 +08003870int splash_screen_check_header(logo_img_header *header)
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303871{
Mao Flynn7b379f32015-04-20 00:28:30 +08003872 if (memcmp(header->magic, LOGO_IMG_MAGIC, 8))
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303873 return -1;
Mao Flynn7b379f32015-04-20 00:28:30 +08003874 if (header->width == 0 || header->height == 0)
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303875 return -1;
3876 return 0;
3877}
3878
Mao Flynn7b379f32015-04-20 00:28:30 +08003879int splash_screen_flash()
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003880{
3881 struct ptentry *ptn;
3882 struct ptable *ptable;
Mao Flynn7b379f32015-04-20 00:28:30 +08003883 struct logo_img_header *header;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003884 struct fbcon_config *fb_display = NULL;
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08003885
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303886 ptable = flash_get_ptable();
3887 if (ptable == NULL) {
Mao Flynn7b379f32015-04-20 00:28:30 +08003888 dprintf(CRITICAL, "ERROR: Partition table not found\n");
3889 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303890 }
Mao Flynn7b379f32015-04-20 00:28:30 +08003891
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303892 ptn = ptable_find(ptable, "splash");
3893 if (ptn == NULL) {
3894 dprintf(CRITICAL, "ERROR: splash Partition not found\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003895 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303896 }
Mao Flynn7b379f32015-04-20 00:28:30 +08003897 if (flash_read(ptn, 0, (void *)logo_header, LOGO_IMG_HEADER_SIZE)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303898 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003899 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303900 }
3901
Mao Flynn7b379f32015-04-20 00:28:30 +08003902 header = (struct logo_img_header *)logo_header;
3903 if (splash_screen_check_header(header)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303904 dprintf(CRITICAL, "ERROR: Boot image header invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003905 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303906 }
3907
3908 fb_display = fbcon_display();
3909 if (fb_display) {
Mao Flynn7b379f32015-04-20 00:28:30 +08003910 if (header->type && (header->blocks != 0)) { // RLE24 compressed data
3911 uint8_t *base = (uint8_t *) fb_display->base + LOGO_IMG_OFFSET;
3912
3913 /* if the logo is full-screen size, remove "fbcon_clear()" */
3914 if ((header->width != fb_display->width)
3915 || (header->height != fb_display->height))
3916 fbcon_clear();
3917
3918 if (flash_read(ptn + LOGO_IMG_HEADER_SIZE, 0,
3919 (uint32_t *)base,
3920 (header->blocks * 512))) {
3921 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
3922 return -1;
3923 }
3924 fbcon_extract_to_screen(header, base);
3925 return 0;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003926 }
Mao Flynn7b379f32015-04-20 00:28:30 +08003927
Veera Sundaram Sankaranc33416f2015-09-28 10:25:55 -07003928 if ((header->width > fb_display->width) || (header->height > fb_display->height)) {
3929 dprintf(CRITICAL, "Logo config greater than fb config. Fall back default logo\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003930 return -1;
3931 }
3932
Vineet Bajaj99291ed2014-09-09 12:29:46 +05303933 uint8_t *base = (uint8_t *) fb_display->base;
Sachin Bhayare619e9e42017-05-15 13:10:31 +05303934 uint32_t fb_size = ROUNDUP(fb_display->width *
3935 fb_display->height *
3936 (fb_display->bpp / 8), 4096);
3937 uint32_t splash_size = ((((header->width * header->height *
3938 fb_display->bpp/8) + 511) >> 9) << 9);
3939
3940 if (splash_size > fb_size) {
3941 dprintf(CRITICAL, "ERROR: Splash image size invalid\n");
3942 return -1;
3943 }
3944
Mao Flynn7b379f32015-04-20 00:28:30 +08003945 if (flash_read(ptn + LOGO_IMG_HEADER_SIZE, 0,
3946 (uint32_t *)base,
3947 ((((header->width * header->height * fb_display->bpp/8) + 511) >> 9) << 9))) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303948 fbcon_clear();
Vineet Bajaj99291ed2014-09-09 12:29:46 +05303949 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003950 return -1;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003951 }
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303952 }
3953
Mao Flynn7b379f32015-04-20 00:28:30 +08003954 return 0;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303955}
3956
Mao Flynn7b379f32015-04-20 00:28:30 +08003957int splash_screen_mmc()
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303958{
3959 int index = INVALID_PTN;
3960 unsigned long long ptn = 0;
3961 struct fbcon_config *fb_display = NULL;
Mao Flynn7b379f32015-04-20 00:28:30 +08003962 struct logo_img_header *header;
Mao Flynn1893a7f2015-06-03 12:03:36 +08003963 uint32_t blocksize, realsize, readsize;
3964 uint8_t *base;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303965
3966 index = partition_get_index("splash");
3967 if (index == 0) {
3968 dprintf(CRITICAL, "ERROR: splash Partition table not found\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003969 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303970 }
3971
3972 ptn = partition_get_offset(index);
3973 if (ptn == 0) {
3974 dprintf(CRITICAL, "ERROR: splash Partition invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003975 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303976 }
3977
Mao Flynn1893a7f2015-06-03 12:03:36 +08003978 mmc_set_lun(partition_get_lun(index));
3979
3980 blocksize = mmc_get_device_blocksize();
3981 if (blocksize == 0) {
3982 dprintf(CRITICAL, "ERROR:splash Partition invalid blocksize\n");
3983 return -1;
3984 }
3985
3986 fb_display = fbcon_display();
Channagoud Kadabidaf10a62015-07-22 11:51:55 -07003987 if (!fb_display)
3988 {
3989 dprintf(CRITICAL, "ERROR: fb config is not allocated\n");
3990 return -1;
3991 }
3992
Mao Flynn1893a7f2015-06-03 12:03:36 +08003993 base = (uint8_t *) fb_display->base;
3994
3995 if (mmc_read(ptn, (uint32_t *)(base + LOGO_IMG_OFFSET), blocksize)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303996 dprintf(CRITICAL, "ERROR: Cannot read splash image header\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003997 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303998 }
3999
Mao Flynn1893a7f2015-06-03 12:03:36 +08004000 header = (struct logo_img_header *)(base + LOGO_IMG_OFFSET);
Mao Flynn7b379f32015-04-20 00:28:30 +08004001 if (splash_screen_check_header(header)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304002 dprintf(CRITICAL, "ERROR: Splash image header invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08004003 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304004 }
4005
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304006 if (fb_display) {
Mao Flynn1893a7f2015-06-03 12:03:36 +08004007 if (header->type && (header->blocks != 0)) { /* 1 RLE24 compressed data */
4008 base += LOGO_IMG_OFFSET;
Mao Flynn7b379f32015-04-20 00:28:30 +08004009
Mao Flynn1893a7f2015-06-03 12:03:36 +08004010 realsize = header->blocks * 512;
4011 readsize = ROUNDUP((realsize + LOGO_IMG_HEADER_SIZE), blocksize) - blocksize;
4012
4013 /* if the logo is not full-screen size, clean screen */
Mao Flynn7b379f32015-04-20 00:28:30 +08004014 if ((header->width != fb_display->width)
4015 || (header->height != fb_display->height))
4016 fbcon_clear();
4017
Sachin Bhayare619e9e42017-05-15 13:10:31 +05304018 uint32_t fb_size = ROUNDUP(fb_display->width *
4019 fb_display->height *
4020 (fb_display->bpp / 8), 4096);
4021
4022 if (readsize > fb_size) {
4023 dprintf(CRITICAL, "ERROR: Splash image size invalid\n");
4024 return -1;
4025 }
4026
Mao Flynn1893a7f2015-06-03 12:03:36 +08004027 if (mmc_read(ptn + blocksize, (uint32_t *)(base + blocksize), readsize)) {
Mao Flynn7b379f32015-04-20 00:28:30 +08004028 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
4029 return -1;
4030 }
Mao Flynn7b379f32015-04-20 00:28:30 +08004031
Mao Flynn1893a7f2015-06-03 12:03:36 +08004032 fbcon_extract_to_screen(header, (base + LOGO_IMG_HEADER_SIZE));
4033 } else { /* 2 Raw BGR data */
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304034
Veera Sundaram Sankaranc33416f2015-09-28 10:25:55 -07004035 if ((header->width > fb_display->width) || (header->height > fb_display->height)) {
4036 dprintf(CRITICAL, "Logo config greater than fb config. Fall back default logo\n");
Mao Flynn1893a7f2015-06-03 12:03:36 +08004037 return -1;
4038 }
4039
4040 realsize = header->width * header->height * fb_display->bpp / 8;
4041 readsize = ROUNDUP((realsize + LOGO_IMG_HEADER_SIZE), blocksize) - blocksize;
4042
4043 if (blocksize == LOGO_IMG_HEADER_SIZE) { /* read the content directly */
4044 if (mmc_read((ptn + LOGO_IMG_HEADER_SIZE), (uint32_t *)base, readsize)) {
4045 fbcon_clear();
4046 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
4047 return -1;
4048 }
4049 } else {
4050 if (mmc_read(ptn + blocksize ,
4051 (uint32_t *)(base + LOGO_IMG_OFFSET + blocksize), readsize)) {
4052 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
4053 return -1;
4054 }
4055 memmove(base, (base + LOGO_IMG_OFFSET + LOGO_IMG_HEADER_SIZE), realsize);
4056 }
4057 }
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304058 }
4059
Mao Flynn7b379f32015-04-20 00:28:30 +08004060 return 0;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304061}
4062
Mao Flynn7b379f32015-04-20 00:28:30 +08004063int fetch_image_from_partition()
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304064{
4065 if (target_is_emmc_boot()) {
4066 return splash_screen_mmc();
4067 } else {
4068 return splash_screen_flash();
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07004069 }
4070}
4071
Mayank Grover351a75e2017-05-30 20:06:08 +05304072void publish_getvar_multislot_vars()
4073{
4074 int i,count;
4075 static bool published = false;
4076 static char slot_count[MAX_RSP_SIZE];
4077 static struct ab_slot_info slot_info[AB_SUPPORTED_SLOTS];
4078 static char active_slot_suffix[MAX_RSP_SIZE];
4079 static char has_slot_pname[NUM_PARTITIONS][MAX_GET_VAR_NAME_SIZE];
4080 static char has_slot_reply[NUM_PARTITIONS][MAX_RSP_SIZE];
4081 const char *tmp;
4082 char tmpbuff[MAX_GET_VAR_NAME_SIZE];
Mayank Grover2bd84bb2017-06-20 11:06:07 +05304083 signed active_slt;
Mayank Grover351a75e2017-05-30 20:06:08 +05304084
4085 if (!published)
4086 {
4087 /* Update slot meta info */
4088 count = partition_fill_partition_meta(has_slot_pname, has_slot_reply,
4089 partition_get_partition_count());
4090 for(i=0; i<count; i++)
4091 {
4092 memset(tmpbuff, 0, MAX_GET_VAR_NAME_SIZE);
Mayank Grover2bd84bb2017-06-20 11:06:07 +05304093 snprintf(tmpbuff, MAX_GET_VAR_NAME_SIZE,"has-slot:%s",
4094 has_slot_pname[i]);
4095 strlcpy(has_slot_pname[i], tmpbuff, MAX_GET_VAR_NAME_SIZE);
Mayank Grover351a75e2017-05-30 20:06:08 +05304096 fastboot_publish(has_slot_pname[i], has_slot_reply[i]);
4097 }
4098
4099 for (i=0; i<AB_SUPPORTED_SLOTS; i++)
4100 {
4101 tmp = SUFFIX_SLOT(i);
Mayank Grover7b8a8f82017-10-27 13:07:59 +05304102 tmp++; // to remove "_" from slot_suffix.
Mayank Grover2bd84bb2017-06-20 11:06:07 +05304103 snprintf(slot_info[i].slot_is_unbootable, sizeof(slot_info[i].slot_is_unbootable),
4104 "slot-unbootable:%s", tmp);
4105 snprintf(slot_info[i].slot_is_active, sizeof(slot_info[i].slot_is_active),
4106 "slot-active:%s", tmp);
4107 snprintf(slot_info[i].slot_is_succesful, sizeof(slot_info[i].slot_is_succesful),
4108 "slot-success:%s", tmp);
4109 snprintf(slot_info[i].slot_retry_count, sizeof(slot_info[i].slot_retry_count),
4110 "slot-retry-count:%s", tmp);
Mayank Grover351a75e2017-05-30 20:06:08 +05304111 fastboot_publish(slot_info[i].slot_is_unbootable,
4112 slot_info[i].slot_is_unbootable_rsp);
4113 fastboot_publish(slot_info[i].slot_is_active,
4114 slot_info[i].slot_is_active_rsp);
4115 fastboot_publish(slot_info[i].slot_is_succesful,
4116 slot_info[i].slot_is_succesful_rsp);
4117 fastboot_publish(slot_info[i].slot_retry_count,
4118 slot_info[i].slot_retry_count_rsp);
4119 }
4120 fastboot_publish("current-slot", active_slot_suffix);
4121 snprintf(slot_count, sizeof(slot_count),"%d", AB_SUPPORTED_SLOTS);
4122 fastboot_publish("slot-count", slot_count);
4123 published = true;
4124 }
4125
Mayank Grover2bd84bb2017-06-20 11:06:07 +05304126 active_slt = partition_find_active_slot();
4127 if (active_slt != INVALID)
Mayank Grover7b8a8f82017-10-27 13:07:59 +05304128 {
4129 tmp = SUFFIX_SLOT(active_slt);
4130 tmp++; // to remove "_" from slot_suffix.
4131 snprintf(active_slot_suffix, sizeof(active_slot_suffix), "%s", tmp);
4132 }
Mayank Grover2bd84bb2017-06-20 11:06:07 +05304133 else
4134 strlcpy(active_slot_suffix, "INVALID", sizeof(active_slot_suffix));
4135
Mayank Grover351a75e2017-05-30 20:06:08 +05304136 /* Update partition meta information */
4137 partition_fill_slot_meta(slot_info);
4138 return;
4139}
4140
lijuang4ece1e72015-08-14 21:02:36 +08004141void get_product_name(unsigned char *buf)
4142{
4143 snprintf((char*)buf, MAX_RSP_SIZE, "%s", TARGET(BOARD));
4144 return;
4145}
4146
Mayank Grover7b8a8f82017-10-27 13:07:59 +05304147#if PRODUCT_IOT
4148void get_bootloader_version_iot(unsigned char *buf)
4149{
4150 if (buf != NULL)
4151 {
4152 strlcpy(buf, TARGET(BOARD), MAX_VERSION_LEN);
4153 strlcat(buf, "-", MAX_VERSION_LEN);
4154 strlcat(buf, PRODUCT_IOT_VERSION, MAX_VERSION_LEN);
4155 }
4156 return;
4157}
4158#endif
4159
lijuang4ece1e72015-08-14 21:02:36 +08004160void get_bootloader_version(unsigned char *buf)
4161{
4162 snprintf((char*)buf, MAX_RSP_SIZE, "%s", device.bootloader_version);
4163 return;
4164}
4165
4166void get_baseband_version(unsigned char *buf)
4167{
4168 snprintf((char*)buf, MAX_RSP_SIZE, "%s", device.radio_version);
4169 return;
4170}
4171
4172bool is_device_locked()
4173{
4174 return device.is_unlocked ? false:true;
4175}
4176
Amol Jadi5edf3552013-07-23 14:15:34 -07004177/* register commands and variables for fastboot */
4178void aboot_fastboot_register_commands(void)
4179{
Sridhar Parasurame94e8152014-10-24 14:06:03 -07004180 int i;
lijuangf16461c2015-08-03 17:09:34 +08004181 char hw_platform_buf[MAX_RSP_SIZE];
Amol Jadi5edf3552013-07-23 14:15:34 -07004182
Sridhar Parasurame94e8152014-10-24 14:06:03 -07004183 struct fastboot_cmd_desc cmd_list[] = {
lijuang511a2b52015-08-14 20:50:51 +08004184 /* By default the enabled list is empty. */
4185 {"", NULL},
4186 /* move commands enclosed within the below ifndef to here
4187 * if they need to be enabled in user build.
4188 */
Sridhar Parasurame94e8152014-10-24 14:06:03 -07004189#ifndef DISABLE_FASTBOOT_CMDS
lijuang511a2b52015-08-14 20:50:51 +08004190 /* Register the following commands only for non-user builds */
4191 {"flash:", cmd_flash},
4192 {"erase:", cmd_erase},
4193 {"boot", cmd_boot},
4194 {"continue", cmd_continue},
4195 {"reboot", cmd_reboot},
4196 {"reboot-bootloader", cmd_reboot_bootloader},
4197 {"oem unlock", cmd_oem_unlock},
4198 {"oem unlock-go", cmd_oem_unlock_go},
4199 {"oem lock", cmd_oem_lock},
4200 {"flashing unlock", cmd_oem_unlock},
4201 {"flashing lock", cmd_oem_lock},
4202 {"flashing lock_critical", cmd_flashing_lock_critical},
4203 {"flashing unlock_critical", cmd_flashing_unlock_critical},
4204 {"flashing get_unlock_ability", cmd_flashing_get_unlock_ability},
4205 {"oem device-info", cmd_oem_devinfo},
4206 {"preflash", cmd_preflash},
4207 {"oem enable-charger-screen", cmd_oem_enable_charger_screen},
4208 {"oem disable-charger-screen", cmd_oem_disable_charger_screen},
lijuanga25d8bb2015-09-16 13:11:52 +08004209 {"oem off-mode-charge", cmd_oem_off_mode_charger},
lijuang511a2b52015-08-14 20:50:51 +08004210 {"oem select-display-panel", cmd_oem_select_display_panel},
Mayank Grover6ccc1c92017-07-04 17:36:46 +05304211 {"set_active",cmd_set_active},
Channagoud Kadabi736c4962015-08-21 11:56:52 -07004212#if UNITTEST_FW_SUPPORT
Channagoud Kadabid5ddf482015-09-28 10:31:35 -07004213 {"oem run-tests", cmd_oem_runtests},
Channagoud Kadabi736c4962015-08-21 11:56:52 -07004214#endif
Sridhar Parasurame94e8152014-10-24 14:06:03 -07004215#endif
lijuang511a2b52015-08-14 20:50:51 +08004216 };
Sridhar Parasurame94e8152014-10-24 14:06:03 -07004217
4218 int fastboot_cmds_count = sizeof(cmd_list)/sizeof(cmd_list[0]);
4219 for (i = 1; i < fastboot_cmds_count; i++)
4220 fastboot_register(cmd_list[i].name,cmd_list[i].cb);
4221
Amol Jadi5edf3552013-07-23 14:15:34 -07004222 /* publish variables and their values */
4223 fastboot_publish("product", TARGET(BOARD));
4224 fastboot_publish("kernel", "lk");
4225 fastboot_publish("serialno", sn_buf);
4226
4227 /*
4228 * partition info is supported only for emmc partitions
4229 * Calling this for NAND prints some error messages which
4230 * is harmless but misleading. Avoid calling this for NAND
4231 * devices.
4232 */
4233 if (target_is_emmc_boot())
Mayank Grover7b8a8f82017-10-27 13:07:59 +05304234 publish_getvar_partition_info(part_info, partition_get_partition_count());
Amol Jadi5edf3552013-07-23 14:15:34 -07004235
Mayank Grover351a75e2017-05-30 20:06:08 +05304236 if (partition_multislot_is_supported())
4237 publish_getvar_multislot_vars();
4238
Amol Jadi5edf3552013-07-23 14:15:34 -07004239 /* Max download size supported */
Ameya Thakur11cf1a62013-08-05 12:44:48 -07004240 snprintf(max_download_size, MAX_RSP_SIZE, "\t0x%x",
4241 target_get_max_flash_size());
Amol Jadi5edf3552013-07-23 14:15:34 -07004242 fastboot_publish("max-download-size", (const char *) max_download_size);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07004243 /* Is the charger screen check enabled */
4244 snprintf(charger_screen_enabled, MAX_RSP_SIZE, "%d",
4245 device.charger_screen_enabled);
4246 fastboot_publish("charger-screen-enabled",
4247 (const char *) charger_screen_enabled);
lijuanga25d8bb2015-09-16 13:11:52 +08004248 fastboot_publish("off-mode-charge", (const char *) charger_screen_enabled);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05304249 snprintf(panel_display_mode, MAX_RSP_SIZE, "%s",
4250 device.display_panel);
4251 fastboot_publish("display-panel",
4252 (const char *) panel_display_mode);
Mayank Grover7b8a8f82017-10-27 13:07:59 +05304253#if PRODUCT_IOT
4254 get_bootloader_version_iot(&bootloader_version_string);
4255 fastboot_publish("version-bootloader", (const char *) bootloader_version_string);
4256
4257 /* Version baseband is n/a for apq iot devices */
4258 fastboot_publish("version-baseband", "N/A");
4259
4260 /* IOT targets support only mmc target */
4261 snprintf(block_size_string, MAX_RSP_SIZE, "0x%x", mmc_get_device_blocksize());
4262 fastboot_publish("erase-block-size", (const char *) block_size_string);
4263 fastboot_publish("logical-block-size", (const char *) block_size_string);
4264#else
Channagoud Kadabiaafbbfc2015-03-04 17:52:56 -08004265 fastboot_publish("version-bootloader", (const char *) device.bootloader_version);
4266 fastboot_publish("version-baseband", (const char *) device.radio_version);
Mayank Grover7b8a8f82017-10-27 13:07:59 +05304267#endif
lijuangf16461c2015-08-03 17:09:34 +08004268 fastboot_publish("secure", is_secure_boot_enable()? "yes":"no");
Mayank Grover7b8a8f82017-10-27 13:07:59 +05304269 fastboot_publish("unlocked", device.is_unlocked ? "yes":"no");
lijuangf16461c2015-08-03 17:09:34 +08004270 smem_get_hw_platform_name((unsigned char *) hw_platform_buf, sizeof(hw_platform_buf));
4271 snprintf(get_variant, MAX_RSP_SIZE, "%s %s", hw_platform_buf,
4272 target_is_emmc_boot()? "eMMC":"UFS");
4273 fastboot_publish("variant", (const char *) get_variant);
lijuang65c5a822015-08-29 16:35:36 +08004274#if CHECK_BAT_VOLTAGE
lijuang102dfa92015-10-09 18:31:03 +08004275 update_battery_status();
lijuang65c5a822015-08-29 16:35:36 +08004276 fastboot_publish("battery-voltage", (const char *) battery_voltage);
lijuang102dfa92015-10-09 18:31:03 +08004277 fastboot_publish("battery-soc-ok", (const char *) battery_soc_ok);
lijuang65c5a822015-08-29 16:35:36 +08004278#endif
Amol Jadi5edf3552013-07-23 14:15:34 -07004279}
4280
Brian Swetland9c4c0752009-01-25 16:23:50 -08004281void aboot_init(const struct app_descriptor *app)
4282{
Shashank Mittal4f99a882010-02-01 13:58:50 -08004283 unsigned reboot_mode = 0;
Mayank Grover351a75e2017-05-30 20:06:08 +05304284 int boot_err_type = 0;
4285 int boot_slot = INVALID;
Chandan Uddarajubedca152010-06-02 23:05:15 -07004286
lijuang39831732016-01-08 17:49:02 +08004287 /* Initialise wdog to catch early lk crashes */
4288#if WDOG_SUPPORT
4289 msm_wdog_init();
4290#endif
4291
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07004292 /* Setup page size information for nv storage */
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004293 if (target_is_emmc_boot())
4294 {
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07004295 page_size = mmc_page_size();
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004296 page_mask = page_size - 1;
Gaurav Nebhwani4d2389c2016-03-17 21:10:05 +05304297 mmc_blocksize = mmc_get_device_blocksize();
4298 mmc_blocksize_mask = mmc_blocksize - 1;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004299 }
4300 else
4301 {
4302 page_size = flash_page_size();
4303 page_mask = page_size - 1;
4304 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07004305 ASSERT((MEMBASE + MEMSIZE) > MEMBASE);
4306
Ameya Thakur11cf1a62013-08-05 12:44:48 -07004307 read_device_info(&device);
vijay kumarc65876c2015-04-24 13:29:16 +05304308 read_allow_oem_unlock(&device);
Shashank Mittal162244e2011-08-08 19:01:25 -07004309
Mayank Grover351a75e2017-05-30 20:06:08 +05304310 /* Detect multi-slot support */
4311 if (partition_multislot_is_supported())
4312 {
4313 boot_slot = partition_find_active_slot();
4314 if (boot_slot == INVALID)
4315 {
4316 boot_into_fastboot = true;
4317 dprintf(INFO, "Active Slot: (INVALID)\n");
4318 }
4319 else
4320 {
4321 /* Setting the state of system to boot active slot */
4322 partition_mark_active_slot(boot_slot);
4323 dprintf(INFO, "Active Slot: (%s)\n", SUFFIX_SLOT(boot_slot));
4324 }
4325 }
4326
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08004327 /* Display splash screen if enabled */
4328#if DISPLAY_SPLASH_SCREEN
lijuang99c02d82015-02-13 19:04:34 +08004329#if NO_ALARM_DISPLAY
4330 if (!check_alarm_boot()) {
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08004331#endif
lijuang99c02d82015-02-13 19:04:34 +08004332 dprintf(SPEW, "Display Init: Start\n");
Ajay Singh Parmara7865a82015-04-16 21:27:52 -07004333#if DISPLAY_HDMI_PRIMARY
4334 if (!strlen(device.display_panel))
4335 strlcpy(device.display_panel, DISPLAY_PANEL_HDMI,
4336 sizeof(device.display_panel));
4337#endif
Channagoud Kadabi90869ce2015-04-27 11:15:14 -07004338#if ENABLE_WBC
Channagoud Kadabid801ac72015-08-26 11:21:51 -07004339 /* Wait if the display shutdown is in progress */
4340 while(pm_app_display_shutdown_in_prgs());
Channagoud Kadabi90869ce2015-04-27 11:15:14 -07004341 if (!pm_appsbl_display_init_done())
4342 target_display_init(device.display_panel);
4343 else
4344 display_image_on_screen();
4345#else
lijuang99c02d82015-02-13 19:04:34 +08004346 target_display_init(device.display_panel);
Channagoud Kadabi90869ce2015-04-27 11:15:14 -07004347#endif
lijuang99c02d82015-02-13 19:04:34 +08004348 dprintf(SPEW, "Display Init: Done\n");
4349#if NO_ALARM_DISPLAY
4350 }
4351#endif
4352#endif
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08004353
Greg Griscod6250552011-06-29 14:40:23 -07004354 target_serialno((unsigned char *) sn_buf);
Ajay Dudanib06c05f2011-05-12 14:46:10 -07004355 dprintf(SPEW,"serial number: %s\n",sn_buf);
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -08004356
Dhaval Patel223ec952013-07-18 14:49:44 -07004357 memset(display_panel_buf, '\0', MAX_PANEL_BUF_SIZE);
4358
Matthew Qindefd5562014-07-11 18:02:40 +08004359 /*
4360 * Check power off reason if user force reset,
4361 * if yes phone will do normal boot.
4362 */
4363 if (is_user_force_reset())
4364 goto normal_boot;
4365
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07004366 /* Check if we should do something other than booting up */
Ameya Thakur0b9c2442013-05-10 13:22:56 -07004367 if (keys_get_state(KEY_VOLUMEUP) && keys_get_state(KEY_VOLUMEDOWN))
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07004368 {
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03004369 dprintf(ALWAYS,"dload mode key sequence detected\n");
lijuang395b5e62015-11-19 17:39:44 +08004370 reboot_device(EMERGENCY_DLOAD);
4371 dprintf(CRITICAL,"Failed to reboot into dload mode\n");
4372
Ameya Thakur0b9c2442013-05-10 13:22:56 -07004373 boot_into_fastboot = true;
4374 }
4375 if (!boot_into_fastboot)
4376 {
4377 if (keys_get_state(KEY_HOME) || keys_get_state(KEY_VOLUMEUP))
4378 boot_into_recovery = 1;
4379 if (!boot_into_recovery &&
4380 (keys_get_state(KEY_BACK) || keys_get_state(KEY_VOLUMEDOWN)))
Pavel Nedev5d91d412013-04-29 11:34:24 +03004381 boot_into_fastboot = true;
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07004382 }
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004383 #if NO_KEYPAD_DRIVER
Kinson Chik0b1c8162011-08-31 16:31:57 -07004384 if (fastboot_trigger())
Pavel Nedev5d91d412013-04-29 11:34:24 +03004385 boot_into_fastboot = true;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004386 #endif
Chandan Uddarajubedca152010-06-02 23:05:15 -07004387
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07004388#if USE_PON_REBOOT_REG
4389 reboot_mode = check_hard_reboot_mode();
4390#else
Ajay Dudani77421292010-10-27 19:34:06 -07004391 reboot_mode = check_reboot_mode();
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07004392#endif
4393 if (reboot_mode == RECOVERY_MODE)
4394 {
Ajay Dudani77421292010-10-27 19:34:06 -07004395 boot_into_recovery = 1;
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07004396 }
4397 else if(reboot_mode == FASTBOOT_MODE)
4398 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03004399 boot_into_fastboot = true;
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07004400 }
4401 else if(reboot_mode == ALARM_BOOT)
4402 {
Matthew Qind886f3c2014-01-17 16:52:01 +08004403 boot_reason_alarm = true;
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07004404 }
Parth Dixit207573a2015-10-27 17:26:21 +05304405#if VERIFIED_BOOT
Mayank Grover912eaa62017-10-26 12:08:53 +05304406 else if (VB_M <= target_get_vb_version())
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07004407 {
Mayank Grover889be1b2017-09-12 20:12:23 +05304408 if (reboot_mode == DM_VERITY_ENFORCING)
4409 {
4410 device.verity_mode = 1;
4411 write_device_info(&device);
4412 }
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07004413#if ENABLE_VB_ATTEST
Mayank Grover889be1b2017-09-12 20:12:23 +05304414 else if (reboot_mode == DM_VERITY_EIO)
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07004415#else
Mayank Grover889be1b2017-09-12 20:12:23 +05304416 else if (reboot_mode == DM_VERITY_LOGGING)
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07004417#endif
Mayank Grover889be1b2017-09-12 20:12:23 +05304418 {
4419 device.verity_mode = 0;
4420 write_device_info(&device);
4421 }
4422 else if (reboot_mode == DM_VERITY_KEYSCLEAR)
4423 {
4424 if(send_delete_keys_to_tz())
4425 ASSERT(0);
4426 }
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07004427 }
Parth Dixitddbc7352015-10-18 03:13:31 +05304428#endif
Ajay Dudani77421292010-10-27 19:34:06 -07004429
Matthew Qindefd5562014-07-11 18:02:40 +08004430normal_boot:
Pavel Nedev5d91d412013-04-29 11:34:24 +03004431 if (!boot_into_fastboot)
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004432 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03004433 if (target_is_emmc_boot())
Shashank Mittala0032282011-08-26 14:50:11 -07004434 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03004435 if(emmc_recovery_init())
4436 dprintf(ALWAYS,"error in emmc_recovery_init\n");
4437 if(target_use_signed_kernel())
Shashank Mittala0032282011-08-26 14:50:11 -07004438 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03004439 if((device.is_unlocked) || (device.is_tampered))
4440 {
4441 #ifdef TZ_TAMPER_FUSE
4442 set_tamper_fuse_cmd();
4443 #endif
4444 #if USE_PCOM_SECBOOT
4445 set_tamper_flag(device.is_tampered);
4446 #endif
4447 }
Shashank Mittala0032282011-08-26 14:50:11 -07004448 }
Amit Blay6281ebc2015-01-11 14:44:08 +02004449
Mayank Grover351a75e2017-05-30 20:06:08 +05304450retry_boot:
4451 /* Trying to boot active partition */
4452 if (partition_multislot_is_supported())
4453 {
4454 boot_slot = partition_find_boot_slot();
4455 partition_mark_active_slot(boot_slot);
4456 if (boot_slot == INVALID)
4457 goto fastboot;
4458 }
4459
4460 boot_err_type = boot_linux_from_mmc();
4461 switch (boot_err_type)
4462 {
4463 case ERR_INVALID_PAGE_SIZE:
4464 case ERR_DT_PARSE:
4465 case ERR_ABOOT_ADDR_OVERLAP:
Mayank Grover10cabbe2017-10-30 19:11:05 +05304466 case ERR_INVALID_BOOT_MAGIC:
Mayank Grover351a75e2017-05-30 20:06:08 +05304467 if(partition_multislot_is_supported())
Mayank Grover10cabbe2017-10-30 19:11:05 +05304468 {
4469 /*
4470 * Deactivate current slot, as it failed to
4471 * boot, and retry next slot.
4472 */
4473 partition_deactivate_slot(boot_slot);
Mayank Grover351a75e2017-05-30 20:06:08 +05304474 goto retry_boot;
Mayank Grover10cabbe2017-10-30 19:11:05 +05304475 }
Mayank Grover351a75e2017-05-30 20:06:08 +05304476 else
4477 break;
Mayank Grover351a75e2017-05-30 20:06:08 +05304478 default:
4479 break;
4480 /* going to fastboot menu */
4481 }
Shashank Mittala0032282011-08-26 14:50:11 -07004482 }
Pavel Nedev5d91d412013-04-29 11:34:24 +03004483 else
4484 {
4485 recovery_init();
4486 #if USE_PCOM_SECBOOT
4487 if((device.is_unlocked) || (device.is_tampered))
4488 set_tamper_flag(device.is_tampered);
4489 #endif
4490 boot_linux_from_flash();
4491 }
4492 dprintf(CRITICAL, "ERROR: Could not do normal boot. Reverting "
4493 "to fastboot mode.\n");
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004494 }
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07004495
Mayank Grover351a75e2017-05-30 20:06:08 +05304496fastboot:
Amol Jadi5edf3552013-07-23 14:15:34 -07004497 /* We are here means regular boot did not happen. Start fastboot. */
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07004498
Amol Jadi5edf3552013-07-23 14:15:34 -07004499 /* register aboot specific fastboot commands */
4500 aboot_fastboot_register_commands();
Amol Jadi57abe4c2011-05-24 15:47:27 -07004501
Amol Jadi5edf3552013-07-23 14:15:34 -07004502 /* dump partition table for debug info */
Kinson Chikf1a43512011-07-14 11:28:39 -07004503 partition_dump();
Amol Jadi5edf3552013-07-23 14:15:34 -07004504
4505 /* initialize and start fastboot */
4506 fastboot_init(target_get_scratch_address(), target_get_max_flash_size());
lijuang4ece1e72015-08-14 21:02:36 +08004507#if FBCON_DISPLAY_MSG
lijuang9a7d3b92015-11-30 14:41:24 +08004508 display_fastboot_menu();
lijuang4ece1e72015-08-14 21:02:36 +08004509#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08004510}
4511
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07004512uint32_t get_page_size()
4513{
4514 return page_size;
4515}
4516
Amir Samuelov57a6fa22013-06-05 16:36:43 +03004517/*
4518 * Calculated and save hash (SHA256) for non-signed boot image.
4519 *
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07004520 * @param image_addr - Boot image address
4521 * @param image_size - Size of the boot image
Amir Samuelov57a6fa22013-06-05 16:36:43 +03004522 *
4523 * @return int - 0 on success, negative value on failure.
4524 */
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07004525static int aboot_save_boot_hash_mmc(uint32_t image_addr, uint32_t image_size)
Amir Samuelov57a6fa22013-06-05 16:36:43 +03004526{
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07004527 unsigned int digest[8];
4528#if IMAGE_VERIF_ALGO_SHA1
4529 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA1;
4530#else
4531 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA256;
4532#endif
Amir Samuelov57a6fa22013-06-05 16:36:43 +03004533
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07004534 target_crypto_init_params();
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -08004535 hash_find((unsigned char *) image_addr, image_size, (unsigned char *)&digest, auth_algo);
Amir Samuelov57a6fa22013-06-05 16:36:43 +03004536
4537 save_kernel_hash_cmd(digest);
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07004538 dprintf(INFO, "aboot_save_boot_hash_mmc: imagesize_actual size %d bytes.\n", (int) image_size);
Amir Samuelov57a6fa22013-06-05 16:36:43 +03004539
4540 return 0;
4541}
4542
Mayank Grover351a75e2017-05-30 20:06:08 +05304543
Brian Swetland9c4c0752009-01-25 16:23:50 -08004544APP_START(aboot)
4545 .init = aboot_init,
4546APP_END