blob: 22d146bdf34992c31a79c506099c9607b32b957f [file] [log] [blame]
Brian Swetland9c4c0752009-01-25 16:23:50 -08001/*
2 * Copyright (c) 2009, Google Inc.
3 * All rights reserved.
4 *
Kishor PK49831502017-04-21 17:55:43 +05305 * Copyright (c) 2009-2017, The Linux Foundation. All rights reserved.
Brian Swetland9c4c0752009-01-25 16:23:50 -08006 *
Chandan Uddaraju5fa471a2009-12-02 17:31:34 -08007 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070014 * * Neither the name of The Linux Foundation nor
Chandan Uddaraju5fa471a2009-12-02 17:31:34 -080015 * the names of its contributors may be used to endorse or promote
16 * products derived from this software without specific prior written
17 * permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22 * NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 *
Brian Swetland9c4c0752009-01-25 16:23:50 -080031 */
32
33#include <app.h>
34#include <debug.h>
35#include <arch/arm.h>
Brian Swetland9c4c0752009-01-25 16:23:50 -080036#include <string.h>
Channagoud Kadabi132ff552013-04-19 14:34:44 -070037#include <stdlib.h>
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -070038#include <limits.h>
Brian Swetland9c4c0752009-01-25 16:23:50 -080039#include <kernel/thread.h>
40#include <arch/ops.h>
41
Dima Zavin214cc642009-01-26 11:16:21 -080042#include <dev/flash.h>
Tanya Brokhman1c94f1a2015-02-15 09:05:03 +020043#include <dev/flash-ubi.h>
Dima Zavin214cc642009-01-26 11:16:21 -080044#include <lib/ptable.h>
Dima Zavinb4283602009-01-26 16:36:57 -080045#include <dev/keys.h>
Shashank Mittal4f99a882010-02-01 13:58:50 -080046#include <dev/fbcon.h>
Ajay Dudanid04110c2011-01-17 23:55:07 -080047#include <baseband.h>
Greg Griscod6250552011-06-29 14:40:23 -070048#include <target.h>
49#include <mmc.h>
Kinson Chikf1a43512011-07-14 11:28:39 -070050#include <partition_parser.h>
Mayank Grover351a75e2017-05-30 20:06:08 +053051#include <ab_partition_parser.h>
Greg Griscod6250552011-06-29 14:40:23 -070052#include <platform.h>
Shashank Mittalcd98d472011-08-02 14:29:24 -070053#include <crypto_hash.h>
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070054#include <malloc.h>
Amol Jadi492d5a52013-03-15 16:12:34 -070055#include <boot_stats.h>
Amir Samuelov57a6fa22013-06-05 16:36:43 +030056#include <sha.h>
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -070057#include <platform/iomap.h>
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -070058#include <boot_device.h>
Shashank Mittald3e54dd2014-08-28 15:24:02 -070059#include <boot_verifier.h>
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +053060#include <image_verify.h>
Matthew Qinbb7923d2015-02-09 10:56:09 +080061#include <decompress.h>
Unnati Gandhi17b3bfc2015-05-11 12:58:16 +053062#include <platform/timer.h>
lijuang511a2b52015-08-14 20:50:51 +080063#include <sys/types.h>
Channagoud Kadabi036c6052015-02-09 15:19:59 -080064#if USE_RPMB_FOR_DEVINFO
65#include <rpmb.h>
66#endif
Dima Zavin214cc642009-01-26 11:16:21 -080067
Channagoud Kadabi90869ce2015-04-27 11:15:14 -070068#if ENABLE_WBC
69#include <pm_app_smbchg.h>
70#endif
71
Neeti Desai17379b82012-06-04 18:42:53 -070072#if DEVICE_TREE
73#include <libfdt.h>
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070074#include <dev_tree.h>
Neeti Desai17379b82012-06-04 18:42:53 -070075#endif
76
Aparna Mallavarapu118ccae2015-06-03 13:47:11 +053077#if WDOG_SUPPORT
78#include <wdog.h>
79#endif
80
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -070081#include <reboot.h>
Shashank Mittalcd98d472011-08-02 14:29:24 -070082#include "image_verify.h"
Shashank Mittal024c0332010-02-03 11:44:00 -080083#include "recovery.h"
Brian Swetland9c4c0752009-01-25 16:23:50 -080084#include "bootimg.h"
85#include "fastboot.h"
Ajay Dudani5c761132011-04-07 20:19:04 -070086#include "sparse_format.h"
Ajay Dudanide984792015-03-02 09:57:41 -080087#include "meta_format.h"
Greg Grisco6e754772011-06-23 12:19:39 -070088#include "mmc.h"
Shashank Mittal162244e2011-08-08 19:01:25 -070089#include "devinfo.h"
Neeti Desai465491e2012-07-31 12:53:35 -070090#include "board.h"
Shashank Mittal162244e2011-08-08 19:01:25 -070091#include "scm.h"
Amit Blay6281ebc2015-01-11 14:44:08 +020092#include "mdtp.h"
Sridhar Parasuram32b30662015-07-10 13:33:22 -070093#include "secapp_loader.h"
lijuanga40d6302015-07-20 20:10:13 +080094#include <menu_keys_detect.h>
95#include <display_menu.h>
Channagoud Kadabi736c4962015-08-21 11:56:52 -070096#include "fastboot_test.h"
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -070097
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070098extern bool target_use_signed_kernel(void);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070099extern void platform_uninit(void);
Channagoud Kadabi33defe22013-06-18 18:35:40 -0700100extern void target_uninit(void);
Joonwoo Park61112782013-10-02 19:50:39 -0700101extern int get_target_boot_params(const char *cmdline, const char *part,
vijay kumar870515d2015-08-31 16:37:24 +0530102 char **buf);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700103
Sridhar Parasuram7e16d172015-07-05 11:35:23 -0700104void *info_buf;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700105void write_device_info_mmc(device_info *dev);
106void write_device_info_flash(device_info *dev);
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -0700107static int aboot_save_boot_hash_mmc(uint32_t image_addr, uint32_t image_size);
Channagoud Kadabiad259832015-05-29 11:14:17 -0700108static int aboot_frp_unlock(char *pname, void *data, unsigned sz);
Kishor PK38ed93d2017-04-25 14:19:26 +0530109static inline uint64_t validate_partition_size();
Parth Dixit54ac3bb2017-03-07 15:52:48 +0530110bool pwr_key_is_pressed = false;
Hareesh Gauthamabf1e1e2017-04-12 18:09:24 +0530111static bool is_systemd_present=false;
Mayank Grover351a75e2017-05-30 20:06:08 +0530112static void publish_getvar_multislot_vars();
Sridhar Parasurame94e8152014-10-24 14:06:03 -0700113/* fastboot command function pointer */
114typedef void (*fastboot_cmd_fn) (const char *, void *, unsigned);
115
116struct fastboot_cmd_desc {
117 char * name;
118 fastboot_cmd_fn cb;
119};
120
Subbaraman Narayanamurthyeb92bcc2010-07-20 14:32:46 -0700121#define EXPAND(NAME) #NAME
122#define TARGET(NAME) EXPAND(NAME)
Brian Swetland2defe162009-08-18 14:35:59 -0700123
Ajay Singh Parmara7865a82015-04-16 21:27:52 -0700124#define DISPLAY_PANEL_HDMI "hdmi"
125
Ajay Dudanicd01f9b2010-02-23 21:13:04 -0800126#ifdef MEMBASE
127#define EMMC_BOOT_IMG_HEADER_ADDR (0xFF000+(MEMBASE))
128#else
David Ng183a7422009-12-07 14:55:21 -0800129#define EMMC_BOOT_IMG_HEADER_ADDR 0xFF000
Ajay Dudanicd01f9b2010-02-23 21:13:04 -0800130#endif
131
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700132#ifndef MEMSIZE
133#define MEMSIZE 1024*1024
134#endif
135
136#define MAX_TAGS_SIZE 1024
137
Kun Liang2f1601a2013-08-12 16:29:54 +0800138/* make 4096 as default size to ensure EFS,EXT4's erasing */
139#define DEFAULT_ERASE_SIZE 4096
Ujwal Patelc0b0a252015-08-16 14:05:35 -0700140#define MAX_PANEL_BUF_SIZE 196
Vijay Kumar Pendoti0b21f462016-05-02 17:09:18 +0530141#define FOOTER_SIZE 16384
Kun Liang2f1601a2013-08-12 16:29:54 +0800142
Dhaval Patelf83d73b2014-06-23 16:24:37 -0700143#define DISPLAY_DEFAULT_PREFIX "mdss_mdp"
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700144#define BOOT_DEV_MAX_LEN 64
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -0800145
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800146#define IS_ARM64(ptr) (ptr->magic_64 == KERNEL64_HDR_MAGIC) ? true : false
147
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -0700148#define ADD_OF(a, b) (UINT_MAX - b > a) ? (a + b) : UINT_MAX
149
Ameya Thakur10a33452016-06-13 14:24:26 -0700150//Size of the header that is used in case the boot image has
151//a uncompressed kernel + appended dtb
152#define PATCHED_KERNEL_HEADER_SIZE 20
153
154//String used to determine if the boot image has
155//a uncompressed kernel + appended dtb
156#define PATCHED_KERNEL_MAGIC "UNCOMPRESSED_IMG"
157
Sridhar Parasuram7bd4aaf2015-02-12 11:14:38 -0800158#if USE_BOOTDEV_CMDLINE
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700159static const char *emmc_cmdline = " androidboot.bootdevice=";
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700160#else
David Ng183a7422009-12-07 14:55:21 -0800161static const char *emmc_cmdline = " androidboot.emmc=true";
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700162#endif
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800163static const char *usb_sn_cmdline = " androidboot.serialno=";
Pavel Nedev328ac822013-04-05 15:25:11 +0300164static const char *androidboot_mode = " androidboot.mode=";
Hareesh Gauthamabf1e1e2017-04-12 18:09:24 +0530165
166static const char *systemd_ffbm_mode = " systemd.unit=ffbm.target";
Matthew Qind886f3c2014-01-17 16:52:01 +0800167static const char *alarmboot_cmdline = " androidboot.alarmboot=true";
Pavel Nedev898298c2013-02-27 12:36:09 -0800168static const char *loglevel = " quiet";
Ajay Dudanica3a33c2011-11-18 08:31:40 -0800169static const char *battchg_pause = " androidboot.mode=charger";
Shashank Mittalcd98d472011-08-02 14:29:24 -0700170static const char *auth_kernel = " androidboot.authorized_kernel=true";
Pavel Nedev5614d222013-06-17 18:01:02 +0300171static const char *secondary_gpt_enable = " gpt";
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200172static const char *mdtp_activated_flag = " mdtp";
David Ng183a7422009-12-07 14:55:21 -0800173
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800174static const char *baseband_apq = " androidboot.baseband=apq";
175static const char *baseband_msm = " androidboot.baseband=msm";
176static const char *baseband_csfb = " androidboot.baseband=csfb";
177static const char *baseband_svlte2a = " androidboot.baseband=svlte2a";
Ajay Dudani403bc492011-09-30 16:17:21 -0700178static const char *baseband_mdm = " androidboot.baseband=mdm";
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800179static const char *baseband_mdm2 = " androidboot.baseband=mdm2";
Amol Jadi5c61a952012-05-04 17:05:35 -0700180static const char *baseband_sglte = " androidboot.baseband=sglte";
Amol Jadi2a15a272013-01-22 12:03:36 -0800181static const char *baseband_dsda = " androidboot.baseband=dsda";
182static const char *baseband_dsda2 = " androidboot.baseband=dsda2";
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800183static const char *baseband_sglte2 = " androidboot.baseband=sglte2";
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800184static const char *warmboot_cmdline = " qpnp-power-on.warm_boot=1";
Vijay Kumar Pendotib228cfc2016-06-13 20:15:23 +0530185static const char *baseband_apq_nowgr = " androidboot.baseband=baseband_apq_nowgr";
Mayank Grover351a75e2017-05-30 20:06:08 +0530186static const char *androidboot_slot_suffix = " androidboot.slot_suffix=";
187static const char *skip_ramfs = " skip_initramfs";
Mayank Grover3804be72017-06-22 11:59:23 +0530188static const char *sys_path_cmdline = " rootwait ro init=/init";
189static const char *sys_path = " root=/dev/mmcblk0p";
Ajay Dudanid04110c2011-01-17 23:55:07 -0800190
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700191#if VERIFIED_BOOT
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700192static const char *verity_mode = " androidboot.veritymode=";
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700193static const char *verified_state= " androidboot.verifiedbootstate=";
Parth Dixita5715a02015-10-29 12:25:10 +0530194static const char *keymaster_v1= " androidboot.keymaster=1";
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700195//indexed based on enum values, green is 0 by default
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700196
197struct verified_boot_verity_mode vbvm[] =
198{
Channagoud Kadabi86b0c112016-03-16 19:23:16 -0700199#if ENABLE_VB_ATTEST
200 {false, "eio"},
201#else
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700202 {false, "logging"},
Channagoud Kadabi86b0c112016-03-16 19:23:16 -0700203#endif
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700204 {true, "enforcing"},
205};
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700206struct verified_boot_state_name vbsn[] =
207{
208 {GREEN, "green"},
209 {ORANGE, "orange"},
210 {YELLOW,"yellow"},
211 {RED,"red" },
212};
213#endif
Channagoud Kadabi86b0c112016-03-16 19:23:16 -0700214/*As per spec delay wait time before shutdown in Red state*/
215#define DELAY_WAIT 30000
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700216static unsigned page_size = 0;
217static unsigned page_mask = 0;
Gaurav Nebhwani4d2389c2016-03-17 21:10:05 +0530218static unsigned mmc_blocksize = 0;
219static unsigned mmc_blocksize_mask = 0;
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700220static char ffbm_mode_string[FFBM_MODE_BUF_SIZE];
221static bool boot_into_ffbm;
vijay kumar870515d2015-08-31 16:37:24 +0530222static char *target_boot_params = NULL;
Matthew Qind886f3c2014-01-17 16:52:01 +0800223static bool boot_reason_alarm;
Channagoud Kadabi80a182b2015-03-11 17:04:23 -0700224static bool devinfo_present = true;
Channagoud Kadabi736c4962015-08-21 11:56:52 -0700225bool boot_into_fastboot = false;
Parth Dixit4097b622016-03-15 14:42:27 +0530226static uint32_t dt_size = 0;
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700227
Shashank Mittalcd98d472011-08-02 14:29:24 -0700228/* Assuming unauthorized kernel image by default */
229static int auth_kernel_img = 0;
lijuang511a2b52015-08-14 20:50:51 +0800230static device_info device = {DEVICE_MAGIC, 0, 0, 0, 0, {0}, {0},{0}, 1};
vijay kumarc65876c2015-04-24 13:29:16 +0530231static bool is_allow_unlock = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -0700232
vijay kumarca2e6812015-07-08 20:28:25 +0530233static char frp_ptns[2][8] = {"config","frp"};
234
lijuang511a2b52015-08-14 20:50:51 +0800235static const char *critical_flash_allowed_ptn[] = {
236 "aboot",
237 "rpm",
238 "tz",
239 "sbl",
240 "sdi",
241 "sbl1",
242 "xbl",
243 "hyp",
244 "pmic",
245 "bootloader",
246 "devinfo",
247 "partition"};
248
Dima Zavin42168f22009-01-30 11:52:22 -0800249struct atag_ptbl_entry
250{
251 char name[16];
252 unsigned offset;
253 unsigned size;
254 unsigned flags;
255};
256
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700257/*
258 * Partition info, required to be published
259 * for fastboot
260 */
261struct getvar_partition_info {
262 const char part_name[MAX_GPT_NAME_SIZE]; /* Partition name */
263 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/*
270 * Right now, we are publishing the info for only
271 * three partitions
272 */
273struct getvar_partition_info part_info[] =
274{
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];
lijuang102dfa92015-10-09 18:31:03 +0800285
286#if CHECK_BAT_VOLTAGE
lijuang65c5a822015-08-29 16:35:36 +0800287char battery_voltage[MAX_RSP_SIZE];
lijuang102dfa92015-10-09 18:31:03 +0800288char battery_soc_ok [MAX_RSP_SIZE];
289#endif
290
lijuangf16461c2015-08-03 17:09:34 +0800291char get_variant[MAX_RSP_SIZE];
Greg Griscod6250552011-06-29 14:40:23 -0700292
Greg Griscod2471ef2011-07-14 13:00:42 -0700293extern int emmc_recovery_init(void);
294
Kinson Chik0b1c8162011-08-31 16:31:57 -0700295#if NO_KEYPAD_DRIVER
296extern int fastboot_trigger(void);
297#endif
Greg Griscod2471ef2011-07-14 13:00:42 -0700298
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800299static void update_ker_tags_rdisk_addr(struct boot_img_hdr *hdr, bool is_arm64)
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700300{
301 /* overwrite the destination of specified for the project */
Channagoud Kadabi7042fa32013-04-26 16:44:14 -0700302#ifdef ABOOT_IGNORE_BOOT_HEADER_ADDRS
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800303 if (is_arm64)
304 hdr->kernel_addr = ABOOT_FORCE_KERNEL64_ADDR;
305 else
306 hdr->kernel_addr = ABOOT_FORCE_KERNEL_ADDR;
Channagoud Kadabi7042fa32013-04-26 16:44:14 -0700307 hdr->ramdisk_addr = ABOOT_FORCE_RAMDISK_ADDR;
308 hdr->tags_addr = ABOOT_FORCE_TAGS_ADDR;
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700309#endif
310}
311
Dima Zavin42168f22009-01-30 11:52:22 -0800312static void ptentry_to_tag(unsigned **ptr, struct ptentry *ptn)
313{
314 struct atag_ptbl_entry atag_ptn;
315
316 memcpy(atag_ptn.name, ptn->name, 16);
317 atag_ptn.name[15] = '\0';
318 atag_ptn.offset = ptn->start;
319 atag_ptn.size = ptn->length;
320 atag_ptn.flags = ptn->flags;
321 memcpy(*ptr, &atag_ptn, sizeof(struct atag_ptbl_entry));
322 *ptr += sizeof(struct atag_ptbl_entry) / sizeof(unsigned);
323}
Brian Swetland9c4c0752009-01-25 16:23:50 -0800324
lijuang102dfa92015-10-09 18:31:03 +0800325#if CHECK_BAT_VOLTAGE
326void update_battery_status(void)
327{
328 snprintf(battery_voltage,MAX_RSP_SIZE, "%d",target_get_battery_voltage());
329 snprintf(battery_soc_ok ,MAX_RSP_SIZE, "%s",target_battery_soc_ok()? "yes":"no");
330}
331#endif
332
Neeti Desaie245d492012-06-01 12:52:13 -0700333unsigned char *update_cmdline(const char * cmdline)
Brian Swetland9c4c0752009-01-25 16:23:50 -0800334{
David Ng183a7422009-12-07 14:55:21 -0800335 int cmdline_len = 0;
336 int have_cmdline = 0;
Amol Jadi168b7712012-03-06 16:15:00 -0800337 unsigned char *cmdline_final = NULL;
Neeti Desaie245d492012-06-01 12:52:13 -0700338 int pause_at_bootup = 0;
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800339 bool warm_boot = false;
Pavel Nedev5614d222013-06-17 18:01:02 +0300340 bool gpt_exists = partition_gpt_exists();
Joonwoo Park61112782013-10-02 19:50:39 -0700341 int have_target_boot_params = 0;
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700342 char *boot_dev_buf = NULL;
Mayank Grover351a75e2017-05-30 20:06:08 +0530343 bool is_mdtp_activated = 0;
344 int current_active_slot = INVALID;
Mayank Grover3804be72017-06-22 11:59:23 +0530345 int system_ptn_index = -1;
346 unsigned int lun = 0;
347 char lun_char_base = 'a';
348 int syspath_buflen = strlen(sys_path) + sizeof(int) + 1; /*allocate buflen for largest possible string*/
349 char syspath_buf[syspath_buflen];
Mayank Grover889be1b2017-09-12 20:12:23 +0530350#if VERIFIED_BOOT
351 uint32_t boot_state = RED;
352#endif
Hareesh Gauthamabf1e1e2017-04-12 18:09:24 +0530353
354#if USE_LE_SYSTEMD
355 is_systemd_present=true;
356#endif
357
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700358#if VERIFIED_BOOT
Mayank Grover889be1b2017-09-12 20:12:23 +0530359 if (VB_V2 == target_get_vb_version())
360 {
361 boot_state = boot_verify_get_state();
362 }
Parth Dixitddbc7352015-10-18 03:13:31 +0530363#endif
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700364
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200365#ifdef MDTP_SUPPORT
366 mdtp_activated(&is_mdtp_activated);
367#endif /* MDTP_SUPPORT */
Dima Zavin42168f22009-01-30 11:52:22 -0800368
Brian Swetland9c4c0752009-01-25 16:23:50 -0800369 if (cmdline && cmdline[0]) {
David Ng183a7422009-12-07 14:55:21 -0800370 cmdline_len = strlen(cmdline);
371 have_cmdline = 1;
372 }
373 if (target_is_emmc_boot()) {
374 cmdline_len += strlen(emmc_cmdline);
Sridhar Parasuram7bd4aaf2015-02-12 11:14:38 -0800375#if USE_BOOTDEV_CMDLINE
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700376 boot_dev_buf = (char *) malloc(sizeof(char) * BOOT_DEV_MAX_LEN);
377 ASSERT(boot_dev_buf);
378 platform_boot_dev_cmdline(boot_dev_buf);
379 cmdline_len += strlen(boot_dev_buf);
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700380#endif
David Ng183a7422009-12-07 14:55:21 -0800381 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800382
383 cmdline_len += strlen(usb_sn_cmdline);
384 cmdline_len += strlen(sn_buf);
385
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700386#if VERIFIED_BOOT
Mayank Grover889be1b2017-09-12 20:12:23 +0530387 if (VB_V2 == target_get_vb_version())
Sridhar Parasuram7d8bb9a2015-09-23 18:21:19 -0700388 {
Mayank Grover889be1b2017-09-12 20:12:23 +0530389 cmdline_len += strlen(verified_state) + strlen(vbsn[boot_state].name);
390 if ((device.verity_mode != 0 ) && (device.verity_mode != 1))
391 {
392 dprintf(CRITICAL, "Devinfo paritition possibly corrupted!!!. Please erase devinfo partition to continue booting\n");
393 ASSERT(0);
394 }
395 cmdline_len += strlen(verity_mode) + strlen(vbvm[device.verity_mode].name);
396 cmdline_len += strlen(keymaster_v1);
Sridhar Parasuram7d8bb9a2015-09-23 18:21:19 -0700397 }
Parth Dixitddbc7352015-10-18 03:13:31 +0530398#endif
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700399
Pavel Nedev5614d222013-06-17 18:01:02 +0300400 if (boot_into_recovery && gpt_exists)
401 cmdline_len += strlen(secondary_gpt_enable);
402
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200403 if(is_mdtp_activated)
404 cmdline_len += strlen(mdtp_activated_flag);
405
Pavel Nedev328ac822013-04-05 15:25:11 +0300406 if (boot_into_ffbm) {
407 cmdline_len += strlen(androidboot_mode);
Hareesh Gauthamabf1e1e2017-04-12 18:09:24 +0530408
409 if(is_systemd_present)
410 cmdline_len += strlen(systemd_ffbm_mode);
411
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700412 cmdline_len += strlen(ffbm_mode_string);
Pavel Nedev898298c2013-02-27 12:36:09 -0800413 /* reduce kernel console messages to speed-up boot */
414 cmdline_len += strlen(loglevel);
Matthew Qind886f3c2014-01-17 16:52:01 +0800415 } else if (boot_reason_alarm) {
416 cmdline_len += strlen(alarmboot_cmdline);
Zhenhua Huang431dafa2015-06-30 16:13:37 +0800417 } else if ((target_build_variant_user() || device.charger_screen_enabled)
418 && target_pause_for_battery_charge()) {
David Ngf773dde2010-07-26 19:55:08 -0700419 pause_at_bootup = 1;
420 cmdline_len += strlen(battchg_pause);
421 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800422
Shashank Mittalcd98d472011-08-02 14:29:24 -0700423 if(target_use_signed_kernel() && auth_kernel_img) {
424 cmdline_len += strlen(auth_kernel);
425 }
426
Joonwoo Park61112782013-10-02 19:50:39 -0700427 if (get_target_boot_params(cmdline, boot_into_recovery ? "recoveryfs" :
428 "system",
vijay kumar870515d2015-08-31 16:37:24 +0530429 &target_boot_params) == 0) {
Joonwoo Park61112782013-10-02 19:50:39 -0700430 have_target_boot_params = 1;
431 cmdline_len += strlen(target_boot_params);
432 }
433
Ajay Dudanid04110c2011-01-17 23:55:07 -0800434 /* Determine correct androidboot.baseband to use */
435 switch(target_baseband())
436 {
437 case BASEBAND_APQ:
438 cmdline_len += strlen(baseband_apq);
439 break;
440
441 case BASEBAND_MSM:
442 cmdline_len += strlen(baseband_msm);
443 break;
444
445 case BASEBAND_CSFB:
446 cmdline_len += strlen(baseband_csfb);
447 break;
448
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800449 case BASEBAND_SVLTE2A:
450 cmdline_len += strlen(baseband_svlte2a);
Ajay Dudanid04110c2011-01-17 23:55:07 -0800451 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700452
453 case BASEBAND_MDM:
454 cmdline_len += strlen(baseband_mdm);
455 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700456
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800457 case BASEBAND_MDM2:
458 cmdline_len += strlen(baseband_mdm2);
459 break;
460
Amol Jadi5c61a952012-05-04 17:05:35 -0700461 case BASEBAND_SGLTE:
462 cmdline_len += strlen(baseband_sglte);
463 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530464
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800465 case BASEBAND_SGLTE2:
466 cmdline_len += strlen(baseband_sglte2);
467 break;
468
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530469 case BASEBAND_DSDA:
470 cmdline_len += strlen(baseband_dsda);
471 break;
Amol Jadi2a15a272013-01-22 12:03:36 -0800472
473 case BASEBAND_DSDA2:
474 cmdline_len += strlen(baseband_dsda2);
475 break;
Vijay Kumar Pendotib228cfc2016-06-13 20:15:23 +0530476 case BASEBAND_APQ_NOWGR:
477 cmdline_len += strlen(baseband_apq_nowgr);
478 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800479 }
480
Vladislav Levenetz3cd45242017-04-05 08:58:07 +0300481#if ENABLE_DISPLAY
Lijuan Gao4a5b8322014-07-24 10:38:42 +0800482 if (cmdline) {
483 if ((strstr(cmdline, DISPLAY_DEFAULT_PREFIX) == NULL) &&
Padmanabhan Komandurubccbcdc2015-06-30 16:19:24 +0530484 target_display_panel_node(display_panel_buf,
485 MAX_PANEL_BUF_SIZE) &&
Lijuan Gao4a5b8322014-07-24 10:38:42 +0800486 strlen(display_panel_buf)) {
487 cmdline_len += strlen(display_panel_buf);
488 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700489 }
Vladislav Levenetz3cd45242017-04-05 08:58:07 +0300490#endif
Dhaval Patel223ec952013-07-18 14:49:44 -0700491
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800492 if (target_warm_boot()) {
493 warm_boot = true;
494 cmdline_len += strlen(warmboot_cmdline);
495 }
496
Mayank Grover351a75e2017-05-30 20:06:08 +0530497 if (partition_multislot_is_supported())
498 {
499 current_active_slot = partition_find_active_slot();
500 cmdline_len += (strlen(androidboot_slot_suffix)+
501 strlen(SUFFIX_SLOT(current_active_slot)));
502
Mayank Grover3804be72017-06-22 11:59:23 +0530503 system_ptn_index = partition_get_index("system");
504 if (platform_boot_dev_isemmc())
505 {
506 snprintf(syspath_buf, syspath_buflen, " root=/dev/mmcblk0p%d",
507 system_ptn_index + 1);
508 }
509 else
510 {
511 lun = partition_get_lun(system_ptn_index);
512 snprintf(syspath_buf, syspath_buflen, " root=/dev/sd%c%d",
513 lun_char_base + lun,
514 partition_get_index_in_lun("system", lun));
515 }
Mayank Grover351a75e2017-05-30 20:06:08 +0530516
Mayank Grover3804be72017-06-22 11:59:23 +0530517 cmdline_len += strlen(sys_path_cmdline);
518 cmdline_len += strlen(syspath_buf);
Mayank Grover351a75e2017-05-30 20:06:08 +0530519 if (!boot_into_recovery)
520 cmdline_len += strlen(skip_ramfs);
521 }
522
Channagoud Kadabi85c7ec32016-01-28 23:09:21 -0800523#if TARGET_CMDLINE_SUPPORT
524 char *target_cmdline_buf = malloc(TARGET_MAX_CMDLNBUF);
525 int target_cmd_line_len;
526 ASSERT(target_cmdline_buf);
527 target_cmd_line_len = target_update_cmdline(target_cmdline_buf);
528 cmdline_len += target_cmd_line_len;
529#endif
530
David Ng183a7422009-12-07 14:55:21 -0800531 if (cmdline_len > 0) {
532 const char *src;
Maria Yu52254c02014-07-04 16:14:54 +0800533 unsigned char *dst;
534
535 cmdline_final = (unsigned char*) malloc((cmdline_len + 4) & (~3));
536 ASSERT(cmdline_final != NULL);
vijay kumar287bb542015-09-29 13:01:52 +0530537 memset((void *)cmdline_final, 0, sizeof(*cmdline_final));
Maria Yu52254c02014-07-04 16:14:54 +0800538 dst = cmdline_final;
Neeti Desaie245d492012-06-01 12:52:13 -0700539
Amol Jadi168b7712012-03-06 16:15:00 -0800540 /* Save start ptr for debug print */
David Ng183a7422009-12-07 14:55:21 -0800541 if (have_cmdline) {
542 src = cmdline;
543 while ((*dst++ = *src++));
544 }
545 if (target_is_emmc_boot()) {
546 src = emmc_cmdline;
547 if (have_cmdline) --dst;
David Ngf773dde2010-07-26 19:55:08 -0700548 have_cmdline = 1;
549 while ((*dst++ = *src++));
Sridhar Parasuram7bd4aaf2015-02-12 11:14:38 -0800550#if USE_BOOTDEV_CMDLINE
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700551 src = boot_dev_buf;
552 if (have_cmdline) --dst;
553 while ((*dst++ = *src++));
554#endif
David Ngf773dde2010-07-26 19:55:08 -0700555 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800556
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700557#if VERIFIED_BOOT
Mayank Grover889be1b2017-09-12 20:12:23 +0530558 if (VB_V2 == target_get_vb_version())
Sridhar Parasuram7d8bb9a2015-09-23 18:21:19 -0700559 {
Mayank Grover889be1b2017-09-12 20:12:23 +0530560 src = verified_state;
561 if(have_cmdline) --dst;
562 have_cmdline = 1;
563 while ((*dst++ = *src++));
564 src = vbsn[boot_state].name;
565 if(have_cmdline) --dst;
566 while ((*dst++ = *src++));
567
568 if ((device.verity_mode != 0 ) && (device.verity_mode != 1))
569 {
570 dprintf(CRITICAL, "Devinfo paritition possibly corrupted!!!. Please erase devinfo partition to continue booting\n");
571 ASSERT(0);
572 }
573 src = verity_mode;
574 if(have_cmdline) --dst;
575 while ((*dst++ = *src++));
576 src = vbvm[device.verity_mode].name;
577 if(have_cmdline) -- dst;
578 while ((*dst++ = *src++));
579 src = keymaster_v1;
580 if(have_cmdline) --dst;
581 while ((*dst++ = *src++));
Sridhar Parasuram7d8bb9a2015-09-23 18:21:19 -0700582 }
Parth Dixitddbc7352015-10-18 03:13:31 +0530583#endif
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800584 src = usb_sn_cmdline;
585 if (have_cmdline) --dst;
586 have_cmdline = 1;
587 while ((*dst++ = *src++));
588 src = sn_buf;
589 if (have_cmdline) --dst;
590 have_cmdline = 1;
591 while ((*dst++ = *src++));
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800592 if (warm_boot) {
593 if (have_cmdline) --dst;
594 src = warmboot_cmdline;
595 while ((*dst++ = *src++));
596 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800597
Pavel Nedev5614d222013-06-17 18:01:02 +0300598 if (boot_into_recovery && gpt_exists) {
599 src = secondary_gpt_enable;
600 if (have_cmdline) --dst;
601 while ((*dst++ = *src++));
602 }
603
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200604 if (is_mdtp_activated) {
605 src = mdtp_activated_flag;
606 if (have_cmdline) --dst;
607 while ((*dst++ = *src++));
608 }
609
Pavel Nedev328ac822013-04-05 15:25:11 +0300610 if (boot_into_ffbm) {
611 src = androidboot_mode;
612 if (have_cmdline) --dst;
613 while ((*dst++ = *src++));
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700614 src = ffbm_mode_string;
Pavel Nedev328ac822013-04-05 15:25:11 +0300615 if (have_cmdline) --dst;
616 while ((*dst++ = *src++));
Hareesh Gauthamabf1e1e2017-04-12 18:09:24 +0530617
618 if(is_systemd_present) {
619 src = systemd_ffbm_mode;
620 if (have_cmdline) --dst;
621 while ((*dst++ = *src++));
622 }
623
Pavel Nedev898298c2013-02-27 12:36:09 -0800624 src = loglevel;
625 if (have_cmdline) --dst;
626 while ((*dst++ = *src++));
Matthew Qind886f3c2014-01-17 16:52:01 +0800627 } else if (boot_reason_alarm) {
628 src = alarmboot_cmdline;
629 if (have_cmdline) --dst;
630 while ((*dst++ = *src++));
Pavel Nedev328ac822013-04-05 15:25:11 +0300631 } else if (pause_at_bootup) {
David Ngf773dde2010-07-26 19:55:08 -0700632 src = battchg_pause;
633 if (have_cmdline) --dst;
David Ng183a7422009-12-07 14:55:21 -0800634 while ((*dst++ = *src++));
635 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800636
Shashank Mittalcd98d472011-08-02 14:29:24 -0700637 if(target_use_signed_kernel() && auth_kernel_img) {
638 src = auth_kernel;
639 if (have_cmdline) --dst;
640 while ((*dst++ = *src++));
641 }
642
Ajay Dudanid04110c2011-01-17 23:55:07 -0800643 switch(target_baseband())
644 {
645 case BASEBAND_APQ:
646 src = baseband_apq;
647 if (have_cmdline) --dst;
648 while ((*dst++ = *src++));
649 break;
650
651 case BASEBAND_MSM:
652 src = baseband_msm;
653 if (have_cmdline) --dst;
654 while ((*dst++ = *src++));
655 break;
656
657 case BASEBAND_CSFB:
658 src = baseband_csfb;
659 if (have_cmdline) --dst;
660 while ((*dst++ = *src++));
661 break;
662
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800663 case BASEBAND_SVLTE2A:
664 src = baseband_svlte2a;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800665 if (have_cmdline) --dst;
666 while ((*dst++ = *src++));
667 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700668
669 case BASEBAND_MDM:
670 src = baseband_mdm;
671 if (have_cmdline) --dst;
672 while ((*dst++ = *src++));
673 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700674
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800675 case BASEBAND_MDM2:
676 src = baseband_mdm2;
677 if (have_cmdline) --dst;
678 while ((*dst++ = *src++));
679 break;
680
Amol Jadi5c61a952012-05-04 17:05:35 -0700681 case BASEBAND_SGLTE:
682 src = baseband_sglte;
683 if (have_cmdline) --dst;
684 while ((*dst++ = *src++));
685 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530686
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800687 case BASEBAND_SGLTE2:
688 src = baseband_sglte2;
689 if (have_cmdline) --dst;
690 while ((*dst++ = *src++));
691 break;
692
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530693 case BASEBAND_DSDA:
694 src = baseband_dsda;
695 if (have_cmdline) --dst;
696 while ((*dst++ = *src++));
697 break;
Amol Jadi2a15a272013-01-22 12:03:36 -0800698
699 case BASEBAND_DSDA2:
700 src = baseband_dsda2;
701 if (have_cmdline) --dst;
702 while ((*dst++ = *src++));
703 break;
Vijay Kumar Pendotib228cfc2016-06-13 20:15:23 +0530704 case BASEBAND_APQ_NOWGR:
705 src = baseband_apq_nowgr;
706 if (have_cmdline) --dst;
707 while ((*dst++ = *src++));
708 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800709 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700710
711 if (strlen(display_panel_buf)) {
Dhaval Patel223ec952013-07-18 14:49:44 -0700712 src = display_panel_buf;
713 if (have_cmdline) --dst;
714 while ((*dst++ = *src++));
715 }
Joonwoo Park61112782013-10-02 19:50:39 -0700716
717 if (have_target_boot_params) {
718 if (have_cmdline) --dst;
719 src = target_boot_params;
720 while ((*dst++ = *src++));
vijay kumar870515d2015-08-31 16:37:24 +0530721 free(target_boot_params);
Joonwoo Park61112782013-10-02 19:50:39 -0700722 }
Channagoud Kadabi85c7ec32016-01-28 23:09:21 -0800723
Mayank Grover351a75e2017-05-30 20:06:08 +0530724 if (partition_multislot_is_supported() && have_cmdline)
725 {
726 src = androidboot_slot_suffix;
727 --dst;
728 while ((*dst++ = *src++));
729 --dst;
730 src = SUFFIX_SLOT(current_active_slot);
731 while ((*dst++ = *src++));
732
733 if (!boot_into_recovery)
734 {
735 src = skip_ramfs;
736 --dst;
737 while ((*dst++ = *src++));
738 }
739
740 src = sys_path_cmdline;
741 --dst;
742 while ((*dst++ = *src++));
Mayank Grover3804be72017-06-22 11:59:23 +0530743
744 src = syspath_buf;
745 --dst;
746 while ((*dst++ = *src++));
Mayank Grover351a75e2017-05-30 20:06:08 +0530747 }
748
Channagoud Kadabi85c7ec32016-01-28 23:09:21 -0800749#if TARGET_CMDLINE_SUPPORT
750 if (target_cmdline_buf && target_cmd_line_len)
751 {
752 if (have_cmdline) --dst;
753 src = target_cmdline_buf;
754 while((*dst++ = *src++));
755 free(target_cmdline_buf);
756 }
757#endif
Neeti Desaie245d492012-06-01 12:52:13 -0700758 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700759
760
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700761 if (boot_dev_buf)
762 free(boot_dev_buf);
763
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -0800764 if (cmdline_final)
765 dprintf(INFO, "cmdline: %s\n", cmdline_final);
766 else
767 dprintf(INFO, "cmdline is NULL\n");
Neeti Desaie245d492012-06-01 12:52:13 -0700768 return cmdline_final;
769}
770
771unsigned *atag_core(unsigned *ptr)
772{
773 /* CORE */
774 *ptr++ = 2;
775 *ptr++ = 0x54410001;
776
777 return ptr;
778
779}
780
781unsigned *atag_ramdisk(unsigned *ptr, void *ramdisk,
782 unsigned ramdisk_size)
783{
784 if (ramdisk_size) {
785 *ptr++ = 4;
786 *ptr++ = 0x54420005;
787 *ptr++ = (unsigned)ramdisk;
788 *ptr++ = ramdisk_size;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800789 }
790
Neeti Desaie245d492012-06-01 12:52:13 -0700791 return ptr;
792}
793
794unsigned *atag_ptable(unsigned **ptr_addr)
795{
796 int i;
797 struct ptable *ptable;
798
799 if ((ptable = flash_get_ptable()) && (ptable->count != 0)) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700800 *(*ptr_addr)++ = 2 + (ptable->count * (sizeof(struct atag_ptbl_entry) /
801 sizeof(unsigned)));
Neeti Desaie245d492012-06-01 12:52:13 -0700802 *(*ptr_addr)++ = 0x4d534d70;
803 for (i = 0; i < ptable->count; ++i)
804 ptentry_to_tag(ptr_addr, ptable_get(ptable, i));
805 }
806
807 return (*ptr_addr);
808}
809
810unsigned *atag_cmdline(unsigned *ptr, const char *cmdline)
811{
812 int cmdline_length = 0;
813 int n;
Neeti Desaie245d492012-06-01 12:52:13 -0700814 char *dest;
815
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800816 cmdline_length = strlen((const char*)cmdline);
Neeti Desaie245d492012-06-01 12:52:13 -0700817 n = (cmdline_length + 4) & (~3);
818
819 *ptr++ = (n / 4) + 2;
820 *ptr++ = 0x54410009;
821 dest = (char *) ptr;
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800822 while ((*dest++ = *cmdline++));
Neeti Desaie245d492012-06-01 12:52:13 -0700823 ptr += (n / 4);
824
825 return ptr;
826}
827
828unsigned *atag_end(unsigned *ptr)
829{
Brian Swetland9c4c0752009-01-25 16:23:50 -0800830 /* END */
831 *ptr++ = 0;
832 *ptr++ = 0;
833
Neeti Desaie245d492012-06-01 12:52:13 -0700834 return ptr;
835}
836
837void generate_atags(unsigned *ptr, const char *cmdline,
838 void *ramdisk, unsigned ramdisk_size)
839{
vijay kumar21a37452015-12-29 16:23:21 +0530840 unsigned *orig_ptr = ptr;
Neeti Desaie245d492012-06-01 12:52:13 -0700841 ptr = atag_core(ptr);
842 ptr = atag_ramdisk(ptr, ramdisk, ramdisk_size);
843 ptr = target_atag_mem(ptr);
844
845 /* Skip NAND partition ATAGS for eMMC boot */
846 if (!target_is_emmc_boot()){
847 ptr = atag_ptable(&ptr);
848 }
849
vijay kumar21a37452015-12-29 16:23:21 +0530850 /*
851 * Atags size filled till + cmdline size + 1 unsigned for 4-byte boundary + 4 unsigned
852 * for atag identifier in atag_cmdline and atag_end should be with in MAX_TAGS_SIZE bytes
853 */
854 if (((ptr - orig_ptr) + strlen(cmdline) + 5 * sizeof(unsigned)) < MAX_TAGS_SIZE) {
855 ptr = atag_cmdline(ptr, cmdline);
856 ptr = atag_end(ptr);
857 }
858 else {
859 dprintf(CRITICAL,"Crossing ATAGs Max size allowed\n");
860 ASSERT(0);
861 }
Neeti Desaie245d492012-06-01 12:52:13 -0700862}
863
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700864typedef void entry_func_ptr(unsigned, unsigned, unsigned*);
Neeti Desaie245d492012-06-01 12:52:13 -0700865void boot_linux(void *kernel, unsigned *tags,
866 const char *cmdline, unsigned machtype,
867 void *ramdisk, unsigned ramdisk_size)
868{
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800869 unsigned char *final_cmdline;
Amol Jadib6be5c12012-11-14 13:39:51 -0800870#if DEVICE_TREE
Neeti Desai17379b82012-06-04 18:42:53 -0700871 int ret = 0;
Amol Jadib6be5c12012-11-14 13:39:51 -0800872#endif
873
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700874 void (*entry)(unsigned, unsigned, unsigned*) = (entry_func_ptr*)(PA((addr_t)kernel));
Deepa Dinamani25a9f762012-11-30 15:57:15 -0800875 uint32_t tags_phys = PA((addr_t)tags);
vijay kumar1a50a642015-11-16 12:41:15 +0530876 struct kernel64_hdr *kptr = ((struct kernel64_hdr*)(PA((addr_t)kernel)));
Deepa Dinamani25a9f762012-11-30 15:57:15 -0800877
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530878 ramdisk = (void *)PA((addr_t)ramdisk);
Neeti Desaie245d492012-06-01 12:52:13 -0700879
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800880 final_cmdline = update_cmdline((const char*)cmdline);
881
Neeti Desai17379b82012-06-04 18:42:53 -0700882#if DEVICE_TREE
Amol Jadib6be5c12012-11-14 13:39:51 -0800883 dprintf(INFO, "Updating device tree: start\n");
884
Neeti Desai17379b82012-06-04 18:42:53 -0700885 /* Update the Device Tree */
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530886 ret = update_device_tree((void *)tags,(const char *)final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -0700887 if(ret)
888 {
889 dprintf(CRITICAL, "ERROR: Updating Device Tree Failed \n");
890 ASSERT(0);
891 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800892 dprintf(INFO, "Updating device tree: done\n");
Neeti Desai17379b82012-06-04 18:42:53 -0700893#else
Neeti Desaie245d492012-06-01 12:52:13 -0700894 /* Generating the Atags */
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800895 generate_atags(tags, final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -0700896#endif
Neeti Desaie245d492012-06-01 12:52:13 -0700897
Maria Yu52254c02014-07-04 16:14:54 +0800898 free(final_cmdline);
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -0700899
900#if VERIFIED_BOOT
Mayank Grover889be1b2017-09-12 20:12:23 +0530901 if (VB_V2 == target_get_vb_version())
902 {
903 if (device.verity_mode == 0) {
lijuangbdd9bb42016-03-01 18:22:17 +0800904#if FBCON_DISPLAY_MSG
Channagoud Kadabi86b0c112016-03-16 19:23:16 -0700905#if ENABLE_VB_ATTEST
Mayank Grover889be1b2017-09-12 20:12:23 +0530906 display_bootverify_menu(DISPLAY_MENU_EIO);
907 wait_for_users_action();
908 if(!pwr_key_is_pressed)
909 shutdown_device();
Channagoud Kadabi86b0c112016-03-16 19:23:16 -0700910#else
Mayank Grover889be1b2017-09-12 20:12:23 +0530911 display_bootverify_menu(DISPLAY_MENU_LOGGING);
Channagoud Kadabi86b0c112016-03-16 19:23:16 -0700912#endif
Mayank Grover889be1b2017-09-12 20:12:23 +0530913 wait_for_users_action();
lijuangbdd9bb42016-03-01 18:22:17 +0800914#else
Mayank Grover889be1b2017-09-12 20:12:23 +0530915 dprintf(CRITICAL,
916 "The dm-verity is not started in enforcing mode.\nWait for 5 seconds before proceeding\n");
917 mdelay(5000);
lijuangbdd9bb42016-03-01 18:22:17 +0800918#endif
Mayank Grover889be1b2017-09-12 20:12:23 +0530919 }
lijuangbdd9bb42016-03-01 18:22:17 +0800920 }
lijuangbdd9bb42016-03-01 18:22:17 +0800921#endif
922
923#if VERIFIED_BOOT
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -0700924 /* Write protect the device info */
Channagoud Kadabi3bd9d1e2015-05-05 16:18:20 -0700925 if (!boot_into_recovery && target_build_variant_user() && devinfo_present && mmc_write_protect("devinfo", 1))
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -0700926 {
927 dprintf(INFO, "Failed to write protect dev info\n");
928 ASSERT(0);
929 }
930#endif
931
Aravind Venkateswaran8f076242014-02-25 16:25:30 -0800932 /* Turn off splash screen if enabled */
933#if DISPLAY_SPLASH_SCREEN
934 target_display_shutdown();
935#endif
936
Veera Sundaram Sankaran67ea0932015-09-25 10:09:30 -0700937 /* Perform target specific cleanup */
938 target_uninit();
Aravind Venkateswaran8f076242014-02-25 16:25:30 -0800939
Deepa Dinamani33734bc2013-03-06 12:16:06 -0800940 dprintf(INFO, "booting linux @ %p, ramdisk @ %p (%d), tags/device tree @ %p\n",
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530941 entry, ramdisk, ramdisk_size, (void *)tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800942
943 enter_critical_section();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700944
Amol Jadi4421e652011-06-16 15:00:48 -0700945 /* do any platform specific cleanup before kernel entry */
946 platform_uninit();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700947
Brian Swetland9c4c0752009-01-25 16:23:50 -0800948 arch_disable_cache(UCACHE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700949
Amol Jadi504f9fe2012-08-16 13:56:48 -0700950#if ARM_WITH_MMU
Brian Swetland9c4c0752009-01-25 16:23:50 -0800951 arch_disable_mmu();
Amol Jadi504f9fe2012-08-16 13:56:48 -0700952#endif
Amol Jadi492d5a52013-03-15 16:12:34 -0700953 bs_set_timestamp(BS_KERNEL_ENTRY);
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800954
955 if (IS_ARM64(kptr))
956 /* Jump to a 64bit kernel */
957 scm_elexec_call((paddr_t)kernel, tags_phys);
958 else
959 /* Jump to a 32bit kernel */
960 entry(0, machtype, (unsigned*)tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800961}
962
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700963/* Function to check if the memory address range falls within the aboot
964 * boundaries.
965 * start: Start of the memory region
966 * size: Size of the memory region
967 */
Vijay Kumar Pendoti9c002ad2016-03-09 13:52:45 +0530968int check_aboot_addr_range_overlap(uintptr_t start, uint32_t size)
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700969{
970 /* Check for boundary conditions. */
Sundarajan Srinivasance2a0ea2013-12-16 17:02:56 -0800971 if ((UINT_MAX - start) < size)
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700972 return -1;
973
974 /* Check for memory overlap. */
975 if ((start < MEMBASE) && ((start + size) <= MEMBASE))
976 return 0;
Channagoud Kadabi94143912013-10-15 12:53:52 -0700977 else if (start >= (MEMBASE + MEMSIZE))
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700978 return 0;
979 else
980 return -1;
981}
982
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800983#define ROUND_TO_PAGE(x,y) (((x) + (y)) & (~(y)))
Brian Swetland9c4c0752009-01-25 16:23:50 -0800984
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800985BUF_DMA_ALIGN(buf, BOOT_IMG_MAX_PAGE_SIZE); //Equal to max-supported pagesize
Amol Jadib6be5c12012-11-14 13:39:51 -0800986#if DEVICE_TREE
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800987BUF_DMA_ALIGN(dt_buf, BOOT_IMG_MAX_PAGE_SIZE);
Amol Jadib6be5c12012-11-14 13:39:51 -0800988#endif
Dima Zavin214cc642009-01-26 11:16:21 -0800989
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700990static void verify_signed_bootimg(uint32_t bootimg_addr, uint32_t bootimg_size)
991{
992 int ret;
Channagoud Kadabia8c623f2015-01-13 14:48:48 -0800993
994#if !VERIFIED_BOOT
Sundarajan Srinivasance54d6e2013-11-11 12:45:00 -0800995#if IMAGE_VERIF_ALGO_SHA1
996 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA1;
997#else
998 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA256;
999#endif
Channagoud Kadabia8c623f2015-01-13 14:48:48 -08001000#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001001
1002 /* Assume device is rooted at this time. */
1003 device.is_tampered = 1;
1004
1005 dprintf(INFO, "Authenticating boot image (%d): start\n", bootimg_size);
1006
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001007#if VERIFIED_BOOT
1008 if(boot_into_recovery)
1009 {
1010 ret = boot_verify_image((unsigned char *)bootimg_addr,
Unnati Gandhi47051252015-03-02 15:21:09 +05301011 bootimg_size, "/recovery");
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001012 }
1013 else
1014 {
1015 ret = boot_verify_image((unsigned char *)bootimg_addr,
Unnati Gandhi47051252015-03-02 15:21:09 +05301016 bootimg_size, "/boot");
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001017 }
1018 boot_verify_print_state();
1019#else
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001020 ret = image_verify((unsigned char *)bootimg_addr,
1021 (unsigned char *)(bootimg_addr + bootimg_size),
1022 bootimg_size,
Sundarajan Srinivasance54d6e2013-11-11 12:45:00 -08001023 auth_algo);
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001024#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001025 dprintf(INFO, "Authenticating boot image: done return value = %d\n", ret);
1026
1027 if (ret)
1028 {
1029 /* Authorized kernel */
1030 device.is_tampered = 0;
Sundarajan Srinivasan3fb21f12013-09-16 18:36:15 -07001031 auth_kernel_img = 1;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001032 }
1033
Amit Blay4aa292f2015-04-28 21:55:59 +03001034#ifdef MDTP_SUPPORT
1035 {
1036 /* Verify MDTP lock.
1037 * For boot & recovery partitions, use aboot's verification result.
1038 */
1039 mdtp_ext_partition_verification_t ext_partition;
1040 ext_partition.partition = boot_into_recovery ? MDTP_PARTITION_RECOVERY : MDTP_PARTITION_BOOT;
1041 ext_partition.integrity_state = device.is_tampered ? MDTP_PARTITION_STATE_INVALID : MDTP_PARTITION_STATE_VALID;
1042 ext_partition.page_size = 0; /* Not needed since already validated */
1043 ext_partition.image_addr = 0; /* Not needed since already validated */
1044 ext_partition.image_size = 0; /* Not needed since already validated */
1045 ext_partition.sig_avail = FALSE; /* Not needed since already validated */
1046 mdtp_fwlock_verify_lock(&ext_partition);
1047 }
1048#endif /* MDTP_SUPPORT */
1049
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001050#if USE_PCOM_SECBOOT
1051 set_tamper_flag(device.is_tampered);
1052#endif
1053
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001054#if VERIFIED_BOOT
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001055 switch(boot_verify_get_state())
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001056 {
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001057 case RED:
lijuanga40d6302015-07-20 20:10:13 +08001058#if FBCON_DISPLAY_MSG
lijuang9a7d3b92015-11-30 14:41:24 +08001059 display_bootverify_menu(DISPLAY_MENU_RED);
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07001060#if ENABLE_VB_ATTEST
1061 mdelay(DELAY_WAIT);
1062 shutdown_device();
1063#else
lijuanga40d6302015-07-20 20:10:13 +08001064 wait_for_users_action();
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07001065#endif
lijuanga40d6302015-07-20 20:10:13 +08001066#else
1067 dprintf(CRITICAL,
1068 "Your device has failed verification and may not work properly.\nWait for 5 seconds before proceeding\n");
1069 mdelay(5000);
1070#endif
1071
1072 break;
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001073 case YELLOW:
lijuanga40d6302015-07-20 20:10:13 +08001074#if FBCON_DISPLAY_MSG
lijuang9a7d3b92015-11-30 14:41:24 +08001075 display_bootverify_menu(DISPLAY_MENU_YELLOW);
lijuanga40d6302015-07-20 20:10:13 +08001076 wait_for_users_action();
1077#else
1078 dprintf(CRITICAL,
1079 "Your device has loaded a different operating system.\nWait for 5 seconds before proceeding\n");
1080 mdelay(5000);
1081#endif
1082 break;
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001083 default:
lijuanga40d6302015-07-20 20:10:13 +08001084 break;
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001085 }
1086#endif
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001087#if !VERIFIED_BOOT
Unnati Gandhi1be04752015-03-27 19:41:53 +05301088 if(device.is_tampered)
1089 {
1090 write_device_info_mmc(&device);
1091 #ifdef TZ_TAMPER_FUSE
1092 set_tamper_fuse_cmd();
1093 #endif
1094 #ifdef ASSERT_ON_TAMPER
1095 dprintf(CRITICAL, "Device is tampered. Asserting..\n");
1096 ASSERT(0);
1097 #endif
1098 }
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001099#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001100}
1101
Maunik Shah0f3c8ac2014-03-02 17:47:58 +05301102static bool check_format_bit()
1103{
1104 bool ret = false;
1105 int index;
1106 uint64_t offset;
1107 struct boot_selection_info *in = NULL;
1108 char *buf = NULL;
1109
1110 index = partition_get_index("bootselect");
1111 if (index == INVALID_PTN)
1112 {
1113 dprintf(INFO, "Unable to locate /bootselect partition\n");
1114 return ret;
1115 }
1116 offset = partition_get_offset(index);
1117 if(!offset)
1118 {
1119 dprintf(INFO, "partition /bootselect doesn't exist\n");
1120 return ret;
1121 }
1122 buf = (char *) memalign(CACHE_LINE, ROUNDUP(page_size, CACHE_LINE));
Mayank Grover48860402016-11-29 12:34:53 +05301123 mmc_set_lun(partition_get_lun(index));
Maunik Shah0f3c8ac2014-03-02 17:47:58 +05301124 ASSERT(buf);
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301125 if (mmc_read(offset, (uint32_t *)buf, page_size))
Maunik Shah0f3c8ac2014-03-02 17:47:58 +05301126 {
1127 dprintf(INFO, "mmc read failure /bootselect %d\n", page_size);
1128 free(buf);
1129 return ret;
1130 }
1131 in = (struct boot_selection_info *) buf;
1132 if ((in->signature == BOOTSELECT_SIGNATURE) &&
1133 (in->version == BOOTSELECT_VERSION)) {
1134 if ((in->state_info & BOOTSELECT_FORMAT) &&
1135 !(in->state_info & BOOTSELECT_FACTORY))
1136 ret = true;
1137 } else {
1138 dprintf(CRITICAL, "Signature: 0x%08x or version: 0x%08x mismatched of /bootselect\n",
1139 in->signature, in->version);
1140 ASSERT(0);
1141 }
1142 free(buf);
1143 return ret;
1144}
1145
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001146void boot_verifier_init()
1147{
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001148 uint32_t boot_state;
1149 /* Check if device unlock */
1150 if(device.is_unlocked)
1151 {
1152 boot_verify_send_event(DEV_UNLOCK);
1153 boot_verify_print_state();
1154 dprintf(CRITICAL, "Device is unlocked! Skipping verification...\n");
1155 return;
1156 }
1157 else
1158 {
1159 boot_verify_send_event(BOOT_INIT);
1160 }
1161
1162 /* Initialize keystore */
1163 boot_state = boot_verify_keystore_init();
1164 if(boot_state == YELLOW)
1165 {
1166 boot_verify_print_state();
1167 dprintf(CRITICAL, "Keystore verification failed! Continuing anyways...\n");
1168 }
1169}
1170
Shashank Mittal23b8f422010-04-16 19:27:21 -07001171int boot_linux_from_mmc(void)
1172{
1173 struct boot_img_hdr *hdr = (void*) buf;
1174 struct boot_img_hdr *uhdr;
1175 unsigned offset = 0;
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07001176 int rcode;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001177 unsigned long long ptn = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001178 int index = INVALID_PTN;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001179
Shashank Mittalcd98d472011-08-02 14:29:24 -07001180 unsigned char *image_addr = 0;
1181 unsigned kernel_actual;
1182 unsigned ramdisk_actual;
1183 unsigned imagesize_actual;
Neeti Desai465491e2012-07-31 12:53:35 -07001184 unsigned second_actual = 0;
Neeti Desai465491e2012-07-31 12:53:35 -07001185
Matthew Qin271927e2015-03-31 22:07:07 -04001186 unsigned int dtb_size = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08001187 unsigned int out_len = 0;
1188 unsigned int out_avai_len = 0;
1189 unsigned char *out_addr = NULL;
1190 uint32_t dtb_offset = 0;
1191 unsigned char *kernel_start_addr = NULL;
1192 unsigned int kernel_size = 0;
Ameya Thakur10a33452016-06-13 14:24:26 -07001193 unsigned int patched_kernel_hdr_size = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08001194 int rc;
Mayank Groverc93cad82017-10-03 12:23:45 +05301195 char *ptn_name = NULL;
Neeti Desai465491e2012-07-31 12:53:35 -07001196#if DEVICE_TREE
1197 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -07001198 struct dt_entry dt_entry;
Neeti Desai465491e2012-07-31 12:53:35 -07001199 unsigned dt_table_offset;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001200 uint32_t dt_actual;
Deepa Dinamani19648b42013-09-05 17:05:55 -07001201 uint32_t dt_hdr_size;
Matthew Qin271927e2015-03-31 22:07:07 -04001202 unsigned char *best_match_dt_addr = NULL;
Neeti Desai465491e2012-07-31 12:53:35 -07001203#endif
Matthew Qin49e51fa2015-02-09 10:40:45 +08001204 struct kernel64_hdr *kptr = NULL;
Mayank Grover351a75e2017-05-30 20:06:08 +05301205 int current_active_slot = INVALID;
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001206
Maunik Shah0f3c8ac2014-03-02 17:47:58 +05301207 if (check_format_bit())
1208 boot_into_recovery = 1;
1209
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07001210 if (!boot_into_recovery) {
1211 memset(ffbm_mode_string, '\0', sizeof(ffbm_mode_string));
1212 rcode = get_ffbm(ffbm_mode_string, sizeof(ffbm_mode_string));
1213 if (rcode <= 0) {
1214 boot_into_ffbm = false;
1215 if (rcode < 0)
1216 dprintf(CRITICAL,"failed to get ffbm cookie");
1217 } else
1218 boot_into_ffbm = true;
1219 } else
1220 boot_into_ffbm = false;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001221 uhdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
1222 if (!memcmp(uhdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1223 dprintf(INFO, "Unified boot method!\n");
1224 hdr = uhdr;
1225 goto unified_boot;
1226 }
Mayank Groverc93cad82017-10-03 12:23:45 +05301227
1228 /* For a/b recovery image code is on boot partition.
1229 If we support multislot, always use boot partition. */
1230 if (boot_into_recovery &&
1231 (!partition_multislot_is_supported()))
1232 ptn_name = "recovery";
1233 else
1234 ptn_name = "boot";
1235
1236 index = partition_get_index(ptn_name);
1237 ptn = partition_get_offset(index);
1238 if(ptn == 0) {
1239 dprintf(CRITICAL, "ERROR: No %s partition found\n", ptn_name);
1240 return -1;
Kinson Chikf1a43512011-07-14 11:28:39 -07001241 }
Mayank Groverc93cad82017-10-03 12:23:45 +05301242
Channagoud Kadabief0547c2015-02-10 12:57:38 -08001243 /* Set Lun for boot & recovery partitions */
1244 mmc_set_lun(partition_get_lun(index));
Shashank Mittal23b8f422010-04-16 19:27:21 -07001245
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301246 if (mmc_read(ptn + offset, (uint32_t *) buf, page_size)) {
Shashank Mittal23b8f422010-04-16 19:27:21 -07001247 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
1248 return -1;
1249 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001250
1251 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001252 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Mayank Grover351a75e2017-05-30 20:06:08 +05301253 return ERR_INVALID_BOOT_MAGIC;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001254 }
1255
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07001256 if (hdr->page_size && (hdr->page_size != page_size)) {
vijay kumar2e21b3a2014-06-26 17:40:15 +05301257
1258 if (hdr->page_size > BOOT_IMG_MAX_PAGE_SIZE) {
1259 dprintf(CRITICAL, "ERROR: Invalid page size\n");
1260 return -1;
1261 }
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07001262 page_size = hdr->page_size;
1263 page_mask = page_size - 1;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001264 }
1265
Matthew Qin49e51fa2015-02-09 10:40:45 +08001266 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1267 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Kishor PKd8ddcad2017-07-27 13:53:57 +05301268 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001269
Matthew Qin49e51fa2015-02-09 10:40:45 +08001270 image_addr = (unsigned char *)target_get_scratch_address();
Kishor PK9e91b592017-05-24 12:14:55 +05301271 memcpy(image_addr, (void *)buf, page_size);
1272
1273 /* ensure commandline is terminated */
1274 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
Matthew Qin49e51fa2015-02-09 10:40:45 +08001275
1276#if DEVICE_TREE
Parth Dixit4097b622016-03-15 14:42:27 +05301277#ifndef OSVERSION_IN_BOOTIMAGE
1278 dt_size = hdr->dt_size;
1279#endif
1280 dt_actual = ROUND_TO_PAGE(dt_size, page_mask);
Kishor PKd8ddcad2017-07-27 13:53:57 +05301281 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 +05301282 dprintf(CRITICAL, "Integer overflow detected in bootimage header fields at %u in %s\n",__LINE__,__FILE__);
1283 return -1;
1284 }
Kishor PKd8ddcad2017-07-27 13:53:57 +05301285 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + second_actual + dt_actual);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001286#else
Kishor PKd8ddcad2017-07-27 13:53:57 +05301287 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 +05301288 dprintf(CRITICAL, "Integer overflow detected in bootimage header fields at %u in %s\n",__LINE__,__FILE__);
1289 return -1;
1290 }
Kishor PKd8ddcad2017-07-27 13:53:57 +05301291 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + second_actual);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001292#endif
1293
1294#if VERIFIED_BOOT
1295 boot_verifier_init();
1296#endif
1297
Vijay Kumar Pendoti9c002ad2016-03-09 13:52:45 +05301298 if (check_aboot_addr_range_overlap((uintptr_t) image_addr, imagesize_actual))
Matthew Qin49e51fa2015-02-09 10:40:45 +08001299 {
1300 dprintf(CRITICAL, "Boot image buffer address overlaps with aboot addresses.\n");
1301 return -1;
1302 }
1303
Matthew Qinbb7923d2015-02-09 10:56:09 +08001304 /*
1305 * Update loading flow of bootimage to support compressed/uncompressed
1306 * bootimage on both 64bit and 32bit platform.
1307 * 1. Load bootimage from emmc partition onto DDR.
1308 * 2. Check if bootimage is gzip format. If yes, decompress compressed kernel
1309 * 3. Check kernel header and update kernel load addr for 64bit and 32bit
1310 * platform accordingly.
1311 * 4. Sanity Check on kernel_addr and ramdisk_addr and copy data.
1312 */
Mayank Grover351a75e2017-05-30 20:06:08 +05301313 if (partition_multislot_is_supported())
1314 {
1315 current_active_slot = partition_find_active_slot();
1316 dprintf(INFO, "Loading boot image (%d) active_slot(%s): start\n",
1317 imagesize_actual, SUFFIX_SLOT(current_active_slot));
1318 }
1319 else
1320 {
1321 dprintf(INFO, "Loading (%s) image (%d): start\n",
1322 (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual);
1323 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08001324 bs_set_timestamp(BS_KERNEL_LOAD_START);
1325
Gaurav Nebhwani43e2a462016-03-17 21:32:56 +05301326 if ((target_get_max_flash_size() - page_size) < imagesize_actual)
1327 {
1328 dprintf(CRITICAL, "booimage size is greater than DDR can hold\n");
1329 return -1;
1330 }
Kishor PK9e91b592017-05-24 12:14:55 +05301331 offset = page_size;
1332 /* Read image without signature and header*/
1333 if (mmc_read(ptn + offset, (void *)(image_addr + offset), imagesize_actual - page_size))
Matthew Qin49e51fa2015-02-09 10:40:45 +08001334 {
1335 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
1336 return -1;
1337 }
1338
Mayank Grover351a75e2017-05-30 20:06:08 +05301339 if (partition_multislot_is_supported())
1340 {
1341 dprintf(INFO, "Loading boot image (%d) active_slot(%s): done\n",
1342 imagesize_actual, SUFFIX_SLOT(current_active_slot));
1343 }
1344 else
1345 {
1346 dprintf(INFO, "Loading (%s) image (%d): done\n",
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001347 (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual);
1348
Mayank Grover351a75e2017-05-30 20:06:08 +05301349 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08001350 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
1351
1352 /* Authenticate Kernel */
1353 dprintf(INFO, "use_signed_kernel=%d, is_unlocked=%d, is_tampered=%d.\n",
1354 (int) target_use_signed_kernel(),
1355 device.is_unlocked,
1356 device.is_tampered);
1357
Channagoud Kadabi736c4962015-08-21 11:56:52 -07001358 /* Change the condition a little bit to include the test framework support.
1359 * We would never reach this point if device is in fastboot mode, even if we did
1360 * that means we are in test mode, so execute kernel authentication part for the
1361 * tests */
Parth Dixitcb0c6082015-12-30 15:01:13 +05301362 if((target_use_signed_kernel() && (!device.is_unlocked)) || is_test_mode_enabled())
Matthew Qin49e51fa2015-02-09 10:40:45 +08001363 {
1364 offset = imagesize_actual;
Vijay Kumar Pendoti9c002ad2016-03-09 13:52:45 +05301365 if (check_aboot_addr_range_overlap((uintptr_t)image_addr + offset, page_size))
Matthew Qin49e51fa2015-02-09 10:40:45 +08001366 {
1367 dprintf(CRITICAL, "Signature read buffer address overlaps with aboot addresses.\n");
1368 return -1;
1369 }
1370
1371 /* Read signature */
1372 if(mmc_read(ptn + offset, (void *)(image_addr + offset), page_size))
1373 {
1374 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
1375 return -1;
1376 }
1377
1378 verify_signed_bootimg((uint32_t)image_addr, imagesize_actual);
Channagoud Kadabi736c4962015-08-21 11:56:52 -07001379 /* The purpose of our test is done here */
Parth Dixitcb0c6082015-12-30 15:01:13 +05301380 if(is_test_mode_enabled() && auth_kernel_img)
Channagoud Kadabi736c4962015-08-21 11:56:52 -07001381 return 0;
Matthew Qin49e51fa2015-02-09 10:40:45 +08001382 } else {
1383 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
1384 #ifdef TZ_SAVE_KERNEL_HASH
1385 aboot_save_boot_hash_mmc((uint32_t) image_addr, imagesize_actual);
1386 #endif /* TZ_SAVE_KERNEL_HASH */
Amit Blay4aa292f2015-04-28 21:55:59 +03001387
1388#ifdef MDTP_SUPPORT
1389 {
1390 /* Verify MDTP lock.
1391 * For boot & recovery partitions, MDTP will use boot_verifier APIs,
1392 * since verification was skipped in aboot. The signature is not part of the loaded image.
1393 */
1394 mdtp_ext_partition_verification_t ext_partition;
1395 ext_partition.partition = boot_into_recovery ? MDTP_PARTITION_RECOVERY : MDTP_PARTITION_BOOT;
1396 ext_partition.integrity_state = MDTP_PARTITION_STATE_UNSET;
1397 ext_partition.page_size = page_size;
1398 ext_partition.image_addr = (uint32)image_addr;
1399 ext_partition.image_size = imagesize_actual;
1400 ext_partition.sig_avail = FALSE;
1401 mdtp_fwlock_verify_lock(&ext_partition);
1402 }
1403#endif /* MDTP_SUPPORT */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001404 }
1405
Sridhar Parasuram00bfedb2015-05-26 14:21:27 -07001406#if VERIFIED_BOOT
taozhang93088bd2016-11-16 15:50:46 +08001407 if((boot_verify_get_state() == ORANGE) && (!boot_into_ffbm))
Reut Zysmanba8a9d52016-02-18 11:44:04 +02001408 {
1409#if FBCON_DISPLAY_MSG
1410 display_bootverify_menu(DISPLAY_MENU_ORANGE);
1411 wait_for_users_action();
1412#else
1413 dprintf(CRITICAL,
1414 "Your device has been unlocked and can't be trusted.\nWait for 5 seconds before proceeding\n");
1415 mdelay(5000);
1416#endif
1417 }
1418#endif
1419
1420#if VERIFIED_BOOT
Mayank Grover889be1b2017-09-12 20:12:23 +05301421 if (VB_V2 == target_get_vb_version())
1422 {
1423 /* set boot and system versions. */
1424 set_os_version((unsigned char *)image_addr);
1425 // send root of trust
1426 if(!send_rot_command((uint32_t)device.is_unlocked))
1427 ASSERT(0);
1428 }
Parth Dixitbc9b6492015-10-18 00:41:38 +05301429#endif
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001430 /*
Matthew Qinbb7923d2015-02-09 10:56:09 +08001431 * Check if the kernel image is a gzip package. If yes, need to decompress it.
1432 * If not, continue booting.
1433 */
1434 if (is_gzip_package((unsigned char *)(image_addr + page_size), hdr->kernel_size))
1435 {
1436 out_addr = (unsigned char *)(image_addr + imagesize_actual + page_size);
1437 out_avai_len = target_get_max_flash_size() - imagesize_actual - page_size;
Matthew Qin0b15b322015-05-19 05:20:54 -04001438 dprintf(INFO, "decompressing kernel image: start\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001439 rc = decompress((unsigned char *)(image_addr + page_size),
1440 hdr->kernel_size, out_addr, out_avai_len,
1441 &dtb_offset, &out_len);
1442 if (rc)
1443 {
Matthew Qin0b15b322015-05-19 05:20:54 -04001444 dprintf(CRITICAL, "decompressing kernel image failed!!!\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001445 ASSERT(0);
1446 }
1447
Matthew Qin0b15b322015-05-19 05:20:54 -04001448 dprintf(INFO, "decompressing kernel image: done\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001449 kptr = (struct kernel64_hdr *)out_addr;
1450 kernel_start_addr = out_addr;
1451 kernel_size = out_len;
1452 } else {
Ameya Thakur10a33452016-06-13 14:24:26 -07001453 dprintf(INFO, "Uncpmpressed kernel in use\n");
1454 if (!strncmp((char*)(image_addr + page_size),
1455 PATCHED_KERNEL_MAGIC,
1456 sizeof(PATCHED_KERNEL_MAGIC) - 1)) {
1457 dprintf(INFO, "Patched kernel detected\n");
1458 kptr = (struct kernel64_hdr *)(image_addr + page_size +
1459 PATCHED_KERNEL_HEADER_SIZE);
1460 //The size of the kernel is stored at start of kernel image + 16
1461 //The dtb would start just after the kernel
1462 dtb_offset = *((uint32_t*)((unsigned char*)
1463 (image_addr + page_size +
1464 sizeof(PATCHED_KERNEL_MAGIC) -
1465 1)));
1466 //The actual kernel starts after the 20 byte header.
1467 kernel_start_addr = (unsigned char*)(image_addr +
1468 page_size + PATCHED_KERNEL_HEADER_SIZE);
1469 kernel_size = hdr->kernel_size;
1470 patched_kernel_hdr_size = PATCHED_KERNEL_HEADER_SIZE;
1471 } else {
1472 dprintf(INFO, "Kernel image not patched..Unable to locate dt offset\n");
1473 kptr = (struct kernel64_hdr *)(image_addr + page_size);
1474 kernel_start_addr = (unsigned char *)(image_addr + page_size);
1475 kernel_size = hdr->kernel_size;
1476 }
Matthew Qinbb7923d2015-02-09 10:56:09 +08001477 }
1478
1479 /*
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001480 * Update the kernel/ramdisk/tags address if the boot image header
1481 * has default values, these default values come from mkbootimg when
1482 * the boot image is flashed using fastboot flash:raw
1483 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001484 update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr));
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001485
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001486 /* Get virtual addresses since the hdr saves physical addresses. */
1487 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
1488 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
1489 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001490
Matthew Qinbb7923d2015-02-09 10:56:09 +08001491 kernel_size = ROUND_TO_PAGE(kernel_size, page_mask);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001492 /* Check if the addresses in the header are valid. */
Matthew Qinbb7923d2015-02-09 10:56:09 +08001493 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_size) ||
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001494 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
1495 {
1496 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
1497 return -1;
1498 }
1499
1500#ifndef DEVICE_TREE
1501 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1502 {
1503 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
1504 return -1;
1505 }
1506#endif
1507
Matthew Qin49e51fa2015-02-09 10:40:45 +08001508 /* Move kernel, ramdisk and device tree to correct address */
Matthew Qinbb7923d2015-02-09 10:56:09 +08001509 memmove((void*) hdr->kernel_addr, kernel_start_addr, kernel_size);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001510 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001511
Matthew Qin49e51fa2015-02-09 10:40:45 +08001512 #if DEVICE_TREE
Parth Dixit4097b622016-03-15 14:42:27 +05301513 if(dt_size) {
Matthew Qin49e51fa2015-02-09 10:40:45 +08001514 dt_table_offset = ((uint32_t)image_addr + page_size + kernel_actual + ramdisk_actual + second_actual);
1515 table = (struct dt_table*) dt_table_offset;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001516
Matthew Qin49e51fa2015-02-09 10:40:45 +08001517 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
1518 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1519 return -1;
1520 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001521
P.V. Phani Kumar82916762016-03-09 09:20:19 +05301522 /* Its Error if, dt_hdr_size (table->num_entries * dt_entry size + Dev_Tree Header)
1523 goes beyound hdr->dt_size*/
Parth Dixit4097b622016-03-15 14:42:27 +05301524 if (dt_hdr_size > ROUND_TO_PAGE(dt_size,hdr->page_size)) {
P.V. Phani Kumar82916762016-03-09 09:20:19 +05301525 dprintf(CRITICAL, "ERROR: Invalid Device Tree size \n");
1526 return -1;
1527 }
1528
Matthew Qin49e51fa2015-02-09 10:40:45 +08001529 /* Find index of device tree within device tree table */
1530 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
1531 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1532 return -1;
1533 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001534
P.V. Phani Kumar3c333202016-03-09 11:54:37 +05301535 if(dt_entry.offset > (UINT_MAX - dt_entry.size)) {
1536 dprintf(CRITICAL, "ERROR: Device tree contents are Invalid\n");
1537 return -1;
1538 }
1539
1540 /* Ensure we are not overshooting dt_size with the dt_entry selected */
Parth Dixit4097b622016-03-15 14:42:27 +05301541 if ((dt_entry.offset + dt_entry.size) > dt_size) {
P.V. Phani Kumar3c333202016-03-09 11:54:37 +05301542 dprintf(CRITICAL, "ERROR: Device tree contents are Invalid\n");
1543 return -1;
1544 }
1545
Matthew Qin271927e2015-03-31 22:07:07 -04001546 if (is_gzip_package((unsigned char *)dt_table_offset + dt_entry.offset, dt_entry.size))
1547 {
1548 unsigned int compressed_size = 0;
1549 out_addr += out_len;
1550 out_avai_len -= out_len;
Matthew Qin0b15b322015-05-19 05:20:54 -04001551 dprintf(INFO, "decompressing dtb: start\n");
Matthew Qin271927e2015-03-31 22:07:07 -04001552 rc = decompress((unsigned char *)dt_table_offset + dt_entry.offset,
1553 dt_entry.size, out_addr, out_avai_len,
1554 &compressed_size, &dtb_size);
1555 if (rc)
1556 {
Matthew Qin0b15b322015-05-19 05:20:54 -04001557 dprintf(CRITICAL, "decompressing dtb failed!!!\n");
Matthew Qin271927e2015-03-31 22:07:07 -04001558 ASSERT(0);
1559 }
1560
Matthew Qin0b15b322015-05-19 05:20:54 -04001561 dprintf(INFO, "decompressing dtb: done\n");
Matthew Qin271927e2015-03-31 22:07:07 -04001562 best_match_dt_addr = out_addr;
1563 } else {
1564 best_match_dt_addr = (unsigned char *)dt_table_offset + dt_entry.offset;
1565 dtb_size = dt_entry.size;
1566 }
1567
Matthew Qin49e51fa2015-02-09 10:40:45 +08001568 /* Validate and Read device device tree in the tags_addr */
Matthew Qin271927e2015-03-31 22:07:07 -04001569 if (check_aboot_addr_range_overlap(hdr->tags_addr, dtb_size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001570 {
1571 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1572 return -1;
1573 }
Shashank Mittal162244e2011-08-08 19:01:25 -07001574
Matthew Qin271927e2015-03-31 22:07:07 -04001575 memmove((void *)hdr->tags_addr, (char *)best_match_dt_addr, dtb_size);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001576 } else {
1577 /* Validate the tags_addr */
1578 if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual))
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001579 {
1580 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1581 return -1;
1582 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08001583 /*
1584 * If appended dev tree is found, update the atags with
1585 * memory address to the DTB appended location on RAM.
1586 * Else update with the atags address in the kernel header
1587 */
1588 void *dtb;
Ameya Thakur10a33452016-06-13 14:24:26 -07001589 dtb = dev_tree_appended(
1590 (void*)(image_addr + page_size +
1591 patched_kernel_hdr_size),
1592 hdr->kernel_size, dtb_offset,
1593 (void *)hdr->tags_addr);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001594 if (!dtb) {
1595 dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001596 return -1;
1597 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07001598 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08001599 #endif
Shashank Mittal23b8f422010-04-16 19:27:21 -07001600
Stanimir Varbanov69ec5462013-07-18 18:17:42 +03001601 if (boot_into_recovery && !device.is_unlocked && !device.is_tampered)
1602 target_load_ssd_keystore();
1603
Shashank Mittal23b8f422010-04-16 19:27:21 -07001604unified_boot:
Shashank Mittal23b8f422010-04-16 19:27:21 -07001605
Dima Zavin77e41f32013-03-06 16:10:43 -08001606 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07001607 (const char *)hdr->cmdline, board_machtype(),
Shashank Mittal23b8f422010-04-16 19:27:21 -07001608 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
1609
1610 return 0;
1611}
1612
Dima Zavin214cc642009-01-26 11:16:21 -08001613int boot_linux_from_flash(void)
1614{
1615 struct boot_img_hdr *hdr = (void*) buf;
Dima Zavin214cc642009-01-26 11:16:21 -08001616 struct ptentry *ptn;
1617 struct ptable *ptable;
1618 unsigned offset = 0;
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001619
Shashank Mittalcd98d472011-08-02 14:29:24 -07001620 unsigned char *image_addr = 0;
1621 unsigned kernel_actual;
1622 unsigned ramdisk_actual;
1623 unsigned imagesize_actual;
vijay kumar8f53e362015-11-24 13:38:11 +05301624 unsigned second_actual = 0;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001625
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001626#if DEVICE_TREE
1627 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -07001628 struct dt_entry dt_entry;
vijay kumar8f53e362015-11-24 13:38:11 +05301629 unsigned dt_table_offset;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001630 uint32_t dt_actual;
Deepa Dinamani19648b42013-09-05 17:05:55 -07001631 uint32_t dt_hdr_size;
vijay kumar8f53e362015-11-24 13:38:11 +05301632 unsigned int dtb_size = 0;
1633 unsigned char *best_match_dt_addr = NULL;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001634#endif
1635
David Ng183a7422009-12-07 14:55:21 -08001636 if (target_is_emmc_boot()) {
1637 hdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
1638 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1639 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
1640 return -1;
1641 }
1642 goto continue_boot;
1643 }
1644
Dima Zavin214cc642009-01-26 11:16:21 -08001645 ptable = flash_get_ptable();
1646 if (ptable == NULL) {
1647 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1648 return -1;
1649 }
1650
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001651 if(!boot_into_recovery)
1652 {
1653 ptn = ptable_find(ptable, "boot");
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001654
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001655 if (ptn == NULL) {
1656 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1657 return -1;
1658 }
1659 }
1660 else
1661 {
1662 ptn = ptable_find(ptable, "recovery");
1663 if (ptn == NULL) {
1664 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
1665 return -1;
1666 }
Dima Zavin214cc642009-01-26 11:16:21 -08001667 }
1668
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001669 if (flash_read(ptn, offset, buf, page_size)) {
Dima Zavin214cc642009-01-26 11:16:21 -08001670 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
1671 return -1;
1672 }
Dima Zavin214cc642009-01-26 11:16:21 -08001673
1674 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001675 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Dima Zavin214cc642009-01-26 11:16:21 -08001676 return -1;
1677 }
1678
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001679 if (hdr->page_size != page_size) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001680 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 -08001681 return -1;
1682 }
1683
Kishor PK9e91b592017-05-24 12:14:55 +05301684 image_addr = (unsigned char *)target_get_scratch_address();
1685 memcpy(image_addr, (void *)buf, page_size);
vijay kumar287bb542015-09-29 13:01:52 +05301686
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001687 /*
1688 * Update the kernel/ramdisk/tags address if the boot image header
1689 * has default values, these default values come from mkbootimg when
1690 * the boot image is flashed using fastboot flash:raw
1691 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001692 update_ker_tags_rdisk_addr(hdr, false);
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001693
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001694 /* Get virtual addresses since the hdr saves physical addresses. */
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001695 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
1696 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
1697 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
1698
1699 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1700 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Mayank Grover032736f2017-07-14 20:34:51 +05301701 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001702
Kishor PK9e91b592017-05-24 12:14:55 +05301703 /* ensure commandline is terminated */
1704 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
1705
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001706 /* Check if the addresses in the header are valid. */
1707 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
1708 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
1709 {
1710 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
1711 return -1;
1712 }
1713
1714#ifndef DEVICE_TREE
Kishor PKd8ddcad2017-07-27 13:53:57 +05301715 if (UINT_MAX < ((uint64_t)kernel_actual + (uint64_t)ramdisk_actual+ (uint64_t)second_actual + page_size)) {
Mayank Grover032736f2017-07-14 20:34:51 +05301716 dprintf(CRITICAL, "Integer overflow detected in bootimage header fields\n");
1717 return -1;
1718 }
Kishor PKd8ddcad2017-07-27 13:53:57 +05301719 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + second_actual);
Mayank Grover032736f2017-07-14 20:34:51 +05301720
Vijay Kumar Pendotid3ed20e2016-09-20 00:34:46 +05301721 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1722 {
1723 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
1724 return -1;
1725 }
1726#else
1727
1728#ifndef OSVERSION_IN_BOOTIMAGE
1729 dt_size = hdr->dt_size;
1730#endif
Mayank Grover032736f2017-07-14 20:34:51 +05301731 dt_actual = ROUND_TO_PAGE(dt_size, page_mask);
Kishor PKd8ddcad2017-07-27 13:53:57 +05301732 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 +05301733 dprintf(CRITICAL, "Integer overflow detected in bootimage header fields\n");
1734 return -1;
1735 }
1736
Kishor PKd8ddcad2017-07-27 13:53:57 +05301737 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + second_actual + dt_actual);
Mayank Grover032736f2017-07-14 20:34:51 +05301738
1739 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_size))
1740 {
1741 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1742 return -1;
1743 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001744#endif
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001745
Shashank Mittalcd98d472011-08-02 14:29:24 -07001746 /* Authenticate Kernel */
Deepa Dinamani23b60d42013-06-24 18:10:52 -07001747 if(target_use_signed_kernel() && (!device.is_unlocked))
Shashank Mittalcd98d472011-08-02 14:29:24 -07001748 {
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001749 dprintf(INFO, "Loading (%s) image (%d): start\n",
1750 (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001751 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -08001752
Vijay Kumar Pendotib3458362016-07-05 13:45:54 +05301753 if (UINT_MAX - page_size < imagesize_actual)
1754 {
1755 dprintf(CRITICAL,"Integer overflow detected in bootimage header fields %u %s\n", __LINE__,__func__);
1756 return -1;
1757 }
1758
1759 /*Check the availability of RAM before reading boot image + max signature length from flash*/
1760 if (target_get_max_flash_size() < (imagesize_actual + page_size))
1761 {
1762 dprintf(CRITICAL, "bootimage size is greater than DDR can hold\n");
1763 return -1;
1764 }
Kishor PK9e91b592017-05-24 12:14:55 +05301765 offset = page_size;
1766 /* Read image without signature and header*/
1767 if (flash_read(ptn, offset, (void *)(image_addr + offset), imagesize_actual - page_size))
Shashank Mittalcd98d472011-08-02 14:29:24 -07001768 {
1769 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
1770 return -1;
1771 }
Dima Zavin214cc642009-01-26 11:16:21 -08001772
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001773 dprintf(INFO, "Loading (%s) image (%d): done\n",
1774 (!boot_into_recovery ? "boot" : "recovery"), imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001775 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Amol Jadib6be5c12012-11-14 13:39:51 -08001776
Shashank Mittalcd98d472011-08-02 14:29:24 -07001777 offset = imagesize_actual;
1778 /* Read signature */
1779 if (flash_read(ptn, offset, (void *)(image_addr + offset), page_size))
1780 {
1781 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001782 return -1;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001783 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07001784
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301785 verify_signed_bootimg((uint32_t)image_addr, imagesize_actual);
Shashank Mittalcd98d472011-08-02 14:29:24 -07001786
1787 /* Move kernel and ramdisk to correct address */
Matthew Qinbb7923d2015-02-09 10:56:09 +08001788 memmove((void*) hdr->kernel_addr, (char*) (image_addr + page_size), hdr->kernel_size);
1789 memmove((void*) hdr->ramdisk_addr, (char*) (image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001790#if DEVICE_TREE
Parth Dixit4097b622016-03-15 14:42:27 +05301791 if(dt_size != 0) {
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001792
vijay kumar8f53e362015-11-24 13:38:11 +05301793 dt_table_offset = ((uint32_t)image_addr + page_size + kernel_actual + ramdisk_actual + second_actual);
1794
1795 table = (struct dt_table*) dt_table_offset;
1796
1797 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0){
1798 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1799 return -1;
1800 }
1801
Mayank Grover81e019e2017-07-14 15:59:06 +05301802 /* Its Error if, dt_hdr_size (table->num_entries * dt_entry size + Dev_Tree Header)
1803 goes beyound hdr->dt_size*/
1804 if (dt_hdr_size > ROUND_TO_PAGE(dt_size, hdr->page_size)) {
1805 dprintf(CRITICAL, "ERROR: Invalid Device Tree size \n");
1806 return -1;
1807 }
1808
vijay kumar8f53e362015-11-24 13:38:11 +05301809 /* Find index of device tree within device tree table */
1810 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
1811 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1812 return -1;
1813 }
1814
1815 /* Validate and Read device device tree in the "tags_add */
1816 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size)){
1817 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1818 return -1;
1819 }
1820
Mayank Groverebf85ae2017-01-25 13:35:37 +05301821 if(dt_entry.offset > (UINT_MAX - dt_entry.size)) {
1822 dprintf(CRITICAL, "ERROR: Device tree contents are Invalid\n");
1823 return -1;
1824 }
1825
1826 /* Ensure we are not overshooting dt_size with the dt_entry selected */
1827 if ((dt_entry.offset + dt_entry.size) > dt_size) {
1828 dprintf(CRITICAL, "ERROR: Device tree contents are Invalid\n");
1829 return -1;
1830 }
1831
vijay kumar8f53e362015-11-24 13:38:11 +05301832 best_match_dt_addr = (unsigned char *)table + dt_entry.offset;
1833 dtb_size = dt_entry.size;
1834 memmove((void *)hdr->tags_addr, (char *)best_match_dt_addr, dtb_size);
1835 }
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001836#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001837
1838 /* Make sure everything from scratch address is read before next step!*/
Shashank Mittala0032282011-08-26 14:50:11 -07001839 if(device.is_tampered)
Shashank Mittal162244e2011-08-08 19:01:25 -07001840 {
1841 write_device_info_flash(&device);
1842 }
Channagoud Kadabi5c86fe32012-02-16 10:58:48 +05301843#if USE_PCOM_SECBOOT
1844 set_tamper_flag(device.is_tampered);
1845#endif
Shashank Mittalcd98d472011-08-02 14:29:24 -07001846 }
1847 else
1848 {
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001849 dprintf(INFO, "Loading (%s) image (%d): start\n",
1850 (!boot_into_recovery ? "boot" : "recovery"), kernel_actual + ramdisk_actual);
1851
Amol Jadi492d5a52013-03-15 16:12:34 -07001852 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -08001853
Mayank Grover032736f2017-07-14 20:34:51 +05301854 offset = page_size;
Vijay Kumar Pendoti47bc1282016-05-05 15:46:17 +05301855 if (UINT_MAX - offset < kernel_actual)
1856 {
1857 dprintf(CRITICAL, "ERROR: Integer overflow in boot image header %s\t%d\n",__func__,__LINE__);
1858 return -1;
1859 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001860 if (flash_read(ptn, offset, (void *)hdr->kernel_addr, kernel_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -07001861 dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
1862 return -1;
1863 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001864 offset += kernel_actual;
Vijay Kumar Pendoti47bc1282016-05-05 15:46:17 +05301865 if (UINT_MAX - offset < ramdisk_actual)
1866 {
1867 dprintf(CRITICAL, "ERROR: Integer overflow in boot image header %s\t%d\n",__func__,__LINE__);
1868 return -1;
1869 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001870 if (flash_read(ptn, offset, (void *)hdr->ramdisk_addr, ramdisk_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -07001871 dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
1872 return -1;
1873 }
Vijay Kumar Pendoti47bc1282016-05-05 15:46:17 +05301874
Amol Jadib6be5c12012-11-14 13:39:51 -08001875 offset += ramdisk_actual;
1876
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001877 dprintf(INFO, "Loading (%s) image (%d): done\n",
1878 (!boot_into_recovery ? "boot" : "recovery"), kernel_actual + ramdisk_actual);
1879
Amol Jadi492d5a52013-03-15 16:12:34 -07001880 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001881
1882 if(hdr->second_size != 0) {
Vijay Kumar Pendoti47bc1282016-05-05 15:46:17 +05301883 if (UINT_MAX - offset < second_actual)
1884 {
1885 dprintf(CRITICAL, "ERROR: Integer overflow in boot image header %s\t%d\n",__func__,__LINE__);
1886 return -1;
1887 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001888 offset += second_actual;
1889 /* Second image loading not implemented. */
1890 ASSERT(0);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001891 }
1892
1893#if DEVICE_TREE
Parth Dixit4097b622016-03-15 14:42:27 +05301894 if(dt_size != 0) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001895
1896 /* Read the device tree table into buffer */
1897 if(flash_read(ptn, offset, (void *) dt_buf, page_size)) {
1898 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
1899 return -1;
1900 }
1901
1902 table = (struct dt_table*) dt_buf;
1903
Deepa Dinamani19648b42013-09-05 17:05:55 -07001904 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001905 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1906 return -1;
1907 }
1908
P.V. Phani Kumar82916762016-03-09 09:20:19 +05301909 /* Its Error if, dt_hdr_size (table->num_entries * dt_entry size + Dev_Tree Header)
1910 goes beyound hdr->dt_size*/
Parth Dixit4097b622016-03-15 14:42:27 +05301911 if (dt_hdr_size > ROUND_TO_PAGE(dt_size,hdr->page_size)) {
P.V. Phani Kumar82916762016-03-09 09:20:19 +05301912 dprintf(CRITICAL, "ERROR: Invalid Device Tree size \n");
1913 return -1;
1914 }
1915
Mayank Grover032736f2017-07-14 20:34:51 +05301916 table = (void *) target_get_scratch_address();
1917 /*Check the availability of RAM before reading boot image + max signature length from flash*/
1918 if (target_get_max_flash_size() < dt_actual)
1919 {
1920 dprintf(CRITICAL, "ERROR: dt_image size is greater than DDR can hold\n");
Deepa Dinamani19648b42013-09-05 17:05:55 -07001921 return -1;
1922 }
1923
Mayank Grover032736f2017-07-14 20:34:51 +05301924 /* Read the entire device tree table into buffer */
1925 if(flash_read(ptn, offset, (void *)table, dt_actual)) {
1926 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
1927 return -1;
1928 }
Deepa Dinamani19648b42013-09-05 17:05:55 -07001929
Joel Kingaa335dc2013-06-03 16:11:08 -07001930 /* Find index of device tree within device tree table */
1931 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001932 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1933 return -1;
1934 }
1935
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001936 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001937 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001938 {
1939 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1940 return -1;
1941 }
1942
Mayank Grover032736f2017-07-14 20:34:51 +05301943 if(dt_entry.offset > (UINT_MAX - dt_entry.size)) {
1944 dprintf(CRITICAL, "ERROR: Device tree contents are Invalid\n");
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001945 return -1;
1946 }
Mayank Grover032736f2017-07-14 20:34:51 +05301947
1948 /* Ensure we are not overshooting dt_size with the dt_entry selected */
1949 if ((dt_entry.offset + dt_entry.size) > dt_size) {
1950 dprintf(CRITICAL, "ERROR: Device tree contents are Invalid\n");
1951 return -1;
1952 }
1953
1954 best_match_dt_addr = (unsigned char *)table + dt_entry.offset;
1955 dtb_size = dt_entry.size;
1956 memmove((void *)hdr->tags_addr, (char *)best_match_dt_addr, dtb_size);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001957 }
1958#endif
1959
Shashank Mittalcd98d472011-08-02 14:29:24 -07001960 }
David Ng183a7422009-12-07 14:55:21 -08001961continue_boot:
Dima Zavin214cc642009-01-26 11:16:21 -08001962
Dima Zavin214cc642009-01-26 11:16:21 -08001963 /* TODO: create/pass atags to kernel */
1964
Ajay Dudanie28a6072011-07-01 13:59:46 -07001965 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07001966 (const char *)hdr->cmdline, board_machtype(),
Dima Zavin214cc642009-01-26 11:16:21 -08001967 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
1968
1969 return 0;
1970}
Brian Swetland9c4c0752009-01-25 16:23:50 -08001971
Shashank Mittal162244e2011-08-08 19:01:25 -07001972void write_device_info_mmc(device_info *dev)
1973{
Shashank Mittal162244e2011-08-08 19:01:25 -07001974 unsigned long long ptn = 0;
1975 unsigned long long size;
1976 int index = INVALID_PTN;
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001977 uint32_t blocksize;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001978 uint8_t lun = 0;
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001979 uint32_t ret = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001980
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001981 if (devinfo_present)
1982 index = partition_get_index("devinfo");
1983 else
1984 index = partition_get_index("aboot");
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001985
Shashank Mittal162244e2011-08-08 19:01:25 -07001986 ptn = partition_get_offset(index);
1987 if(ptn == 0)
1988 {
1989 return;
1990 }
1991
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001992 lun = partition_get_lun(index);
1993 mmc_set_lun(lun);
1994
Shashank Mittal162244e2011-08-08 19:01:25 -07001995 size = partition_get_size(index);
1996
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001997 blocksize = mmc_get_device_blocksize();
1998
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001999 if (devinfo_present)
2000 ret = mmc_write(ptn, blocksize, (void *)info_buf);
2001 else
2002 ret = mmc_write((ptn + size - blocksize), blocksize, (void *)info_buf);
2003 if (ret)
Shashank Mittal162244e2011-08-08 19:01:25 -07002004 {
2005 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
Sridhar Parasurame1a97922015-07-27 15:31:22 -07002006 ASSERT(0);
Shashank Mittal162244e2011-08-08 19:01:25 -07002007 }
2008}
2009
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002010void read_device_info_mmc(struct device_info *info)
Shashank Mittal162244e2011-08-08 19:01:25 -07002011{
Shashank Mittal162244e2011-08-08 19:01:25 -07002012 unsigned long long ptn = 0;
2013 unsigned long long size;
2014 int index = INVALID_PTN;
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07002015 uint32_t blocksize;
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002016 uint32_t ret = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07002017
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002018 if ((index = partition_get_index("devinfo")) < 0)
2019 {
2020 devinfo_present = false;
2021 index = partition_get_index("aboot");
2022 }
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07002023
Shashank Mittal162244e2011-08-08 19:01:25 -07002024 ptn = partition_get_offset(index);
2025 if(ptn == 0)
2026 {
2027 return;
2028 }
2029
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07002030 mmc_set_lun(partition_get_lun(index));
2031
Shashank Mittal162244e2011-08-08 19:01:25 -07002032 size = partition_get_size(index);
2033
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07002034 blocksize = mmc_get_device_blocksize();
2035
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07002036 if (devinfo_present)
2037 ret = mmc_read(ptn, (void *)info_buf, blocksize);
2038 else
2039 ret = mmc_read((ptn + size - blocksize), (void *)info_buf, blocksize);
2040 if (ret)
Shashank Mittal162244e2011-08-08 19:01:25 -07002041 {
2042 dprintf(CRITICAL, "ERROR: Cannot read device info\n");
Sridhar Parasurame1a97922015-07-27 15:31:22 -07002043 ASSERT(0);
Shashank Mittal162244e2011-08-08 19:01:25 -07002044 }
Shashank Mittal162244e2011-08-08 19:01:25 -07002045}
2046
2047void write_device_info_flash(device_info *dev)
2048{
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002049 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
Shashank Mittal162244e2011-08-08 19:01:25 -07002050 struct ptentry *ptn;
2051 struct ptable *ptable;
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002052 if(info == NULL)
2053 {
2054 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
2055 ASSERT(0);
2056 }
2057 info_buf = info;
Shashank Mittal162244e2011-08-08 19:01:25 -07002058 ptable = flash_get_ptable();
2059 if (ptable == NULL)
2060 {
2061 dprintf(CRITICAL, "ERROR: Partition table not found\n");
2062 return;
2063 }
2064
2065 ptn = ptable_find(ptable, "devinfo");
2066 if (ptn == NULL)
2067 {
Smita Ghosh670c8b82015-05-07 09:30:03 -07002068 dprintf(CRITICAL, "ERROR: No devinfo partition found\n");
Shashank Mittal162244e2011-08-08 19:01:25 -07002069 return;
2070 }
2071
Mayank Groverdedbc892017-10-24 13:41:34 +05302072 memset(info, 0, BOOT_IMG_MAX_PAGE_SIZE);
Shashank Mittal162244e2011-08-08 19:01:25 -07002073 memcpy(info, dev, sizeof(device_info));
2074
2075 if (flash_write(ptn, 0, (void *)info_buf, page_size))
2076 {
2077 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
2078 return;
2079 }
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002080 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07002081}
2082
vijay kumarc65876c2015-04-24 13:29:16 +05302083static int read_allow_oem_unlock(device_info *dev)
2084{
vijay kumarc65876c2015-04-24 13:29:16 +05302085 unsigned offset;
2086 int index;
2087 unsigned long long ptn;
2088 unsigned long long ptn_size;
2089 unsigned blocksize = mmc_get_device_blocksize();
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07002090 STACKBUF_DMA_ALIGN(buf, blocksize);
vijay kumarc65876c2015-04-24 13:29:16 +05302091
vijay kumarca2e6812015-07-08 20:28:25 +05302092 index = partition_get_index(frp_ptns[0]);
vijay kumarc65876c2015-04-24 13:29:16 +05302093 if (index == INVALID_PTN)
2094 {
vijay kumarca2e6812015-07-08 20:28:25 +05302095 index = partition_get_index(frp_ptns[1]);
2096 if (index == INVALID_PTN)
2097 {
2098 dprintf(CRITICAL, "Neither '%s' nor '%s' partition found\n", frp_ptns[0],frp_ptns[1]);
2099 return -1;
2100 }
vijay kumarc65876c2015-04-24 13:29:16 +05302101 }
2102
2103 ptn = partition_get_offset(index);
2104 ptn_size = partition_get_size(index);
2105 offset = ptn_size - blocksize;
2106
Mayank Grover48860402016-11-29 12:34:53 +05302107 /* Set Lun for partition */
2108 mmc_set_lun(partition_get_lun(index));
2109
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07002110 if (mmc_read(ptn + offset, (void *)buf, blocksize))
vijay kumarc65876c2015-04-24 13:29:16 +05302111 {
2112 dprintf(CRITICAL, "Reading MMC failed\n");
2113 return -1;
2114 }
2115
2116 /*is_allow_unlock is a bool value stored at the LSB of last byte*/
2117 is_allow_unlock = buf[blocksize-1] & 0x01;
2118 return 0;
2119}
2120
2121static int write_allow_oem_unlock(bool allow_unlock)
2122{
vijay kumarc65876c2015-04-24 13:29:16 +05302123 unsigned offset;
vijay kumarc65876c2015-04-24 13:29:16 +05302124 int index;
2125 unsigned long long ptn;
2126 unsigned long long ptn_size;
2127 unsigned blocksize = mmc_get_device_blocksize();
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07002128 STACKBUF_DMA_ALIGN(buf, blocksize);
vijay kumarc65876c2015-04-24 13:29:16 +05302129
vijay kumarca2e6812015-07-08 20:28:25 +05302130 index = partition_get_index(frp_ptns[0]);
vijay kumarc65876c2015-04-24 13:29:16 +05302131 if (index == INVALID_PTN)
2132 {
vijay kumarca2e6812015-07-08 20:28:25 +05302133 index = partition_get_index(frp_ptns[1]);
2134 if (index == INVALID_PTN)
2135 {
2136 dprintf(CRITICAL, "Neither '%s' nor '%s' partition found\n", frp_ptns[0],frp_ptns[1]);
2137 return -1;
2138 }
vijay kumarc65876c2015-04-24 13:29:16 +05302139 }
2140
2141 ptn = partition_get_offset(index);
2142 ptn_size = partition_get_size(index);
2143 offset = ptn_size - blocksize;
Mayank Grover48860402016-11-29 12:34:53 +05302144 mmc_set_lun(partition_get_lun(index));
vijay kumarc65876c2015-04-24 13:29:16 +05302145
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07002146 if (mmc_read(ptn + offset, (void *)buf, blocksize))
vijay kumarc65876c2015-04-24 13:29:16 +05302147 {
2148 dprintf(CRITICAL, "Reading MMC failed\n");
2149 return -1;
2150 }
2151
2152 /*is_allow_unlock is a bool value stored at the LSB of last byte*/
2153 buf[blocksize-1] = allow_unlock;
2154 if (mmc_write(ptn + offset, blocksize, buf))
2155 {
2156 dprintf(CRITICAL, "Writing MMC failed\n");
2157 return -1;
2158 }
2159
2160 return 0;
2161}
2162
Shashank Mittal162244e2011-08-08 19:01:25 -07002163void read_device_info_flash(device_info *dev)
2164{
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002165 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
Shashank Mittal162244e2011-08-08 19:01:25 -07002166 struct ptentry *ptn;
2167 struct ptable *ptable;
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002168 if(info == NULL)
2169 {
2170 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
2171 ASSERT(0);
2172 }
2173 info_buf = info;
Shashank Mittal162244e2011-08-08 19:01:25 -07002174 ptable = flash_get_ptable();
2175 if (ptable == NULL)
2176 {
2177 dprintf(CRITICAL, "ERROR: Partition table not found\n");
2178 return;
2179 }
2180
2181 ptn = ptable_find(ptable, "devinfo");
2182 if (ptn == NULL)
2183 {
Smita Ghosh670c8b82015-05-07 09:30:03 -07002184 dprintf(CRITICAL, "ERROR: No devinfo partition found\n");
Shashank Mittal162244e2011-08-08 19:01:25 -07002185 return;
2186 }
2187
2188 if (flash_read(ptn, 0, (void *)info_buf, page_size))
2189 {
2190 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
2191 return;
2192 }
2193
2194 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
2195 {
Shashank Mittal162244e2011-08-08 19:01:25 -07002196 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
2197 info->is_unlocked = 0;
Shashank Mittala0032282011-08-26 14:50:11 -07002198 info->is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07002199 write_device_info_flash(info);
2200 }
2201 memcpy(dev, info, sizeof(device_info));
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002202 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07002203}
2204
2205void write_device_info(device_info *dev)
2206{
2207 if(target_is_emmc_boot())
2208 {
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002209 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
2210 if(info == NULL)
2211 {
2212 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
2213 ASSERT(0);
2214 }
2215 info_buf = info;
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002216 memcpy(info, dev, sizeof(struct device_info));
2217
2218#if USE_RPMB_FOR_DEVINFO
Mayank Grover889be1b2017-09-12 20:12:23 +05302219 if (VB_V2 == target_get_vb_version() &&
2220 is_secure_boot_enable()) {
2221 if((write_device_info_rpmb((void*) info, PAGE_SIZE)) < 0)
2222 ASSERT(0);
Sridhar Parasurame1a97922015-07-27 15:31:22 -07002223 }
Channagoud Kadabic80cb492015-04-28 16:08:28 -07002224 else
2225 write_device_info_mmc(info);
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002226#else
2227 write_device_info_mmc(info);
2228#endif
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002229 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07002230 }
2231 else
2232 {
2233 write_device_info_flash(dev);
2234 }
2235}
2236
2237void read_device_info(device_info *dev)
2238{
2239 if(target_is_emmc_boot())
2240 {
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002241 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
2242 if(info == NULL)
2243 {
2244 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
2245 ASSERT(0);
2246 }
2247 info_buf = info;
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002248
2249#if USE_RPMB_FOR_DEVINFO
Mayank Grover889be1b2017-09-12 20:12:23 +05302250 if (VB_V2 == target_get_vb_version() &&
2251 is_secure_boot_enable()) {
2252 if((read_device_info_rpmb((void*) info, PAGE_SIZE)) < 0)
2253 ASSERT(0);
Sridhar Parasurame1a97922015-07-27 15:31:22 -07002254 }
Channagoud Kadabic80cb492015-04-28 16:08:28 -07002255 else
2256 read_device_info_mmc(info);
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002257#else
2258 read_device_info_mmc(info);
2259#endif
2260
2261 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
2262 {
2263 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
lijuang511a2b52015-08-14 20:50:51 +08002264 if (is_secure_boot_enable()) {
Channagoud Kadabi05f78ba2015-07-06 11:58:14 -07002265 info->is_unlocked = 0;
Mayank Grover889be1b2017-09-12 20:12:23 +05302266#if VERIFIED_BOOT
2267 if (VB_V2 == target_get_vb_version())
2268 info->is_unlock_critical = 0;
Parth Dixitddbc7352015-10-18 03:13:31 +05302269#endif
lijuang511a2b52015-08-14 20:50:51 +08002270 } else {
Channagoud Kadabi2fda4092015-07-07 13:34:11 -07002271 info->is_unlocked = 1;
Mayank Grover889be1b2017-09-12 20:12:23 +05302272#if VERIFIED_BOOT
2273 if (VB_V2 == target_get_vb_version())
2274 info->is_unlock_critical = 1;
Parth Dixitddbc7352015-10-18 03:13:31 +05302275#endif
lijuang511a2b52015-08-14 20:50:51 +08002276 }
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002277 info->is_tampered = 0;
2278 info->charger_screen_enabled = 0;
Mayank Grover889be1b2017-09-12 20:12:23 +05302279#if VERIFIED_BOOT
2280 if (VB_V2 == target_get_vb_version())
2281 info->verity_mode = 1; //enforcing by default
Parth Dixitddbc7352015-10-18 03:13:31 +05302282#endif
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002283 write_device_info(info);
2284 }
2285 memcpy(dev, info, sizeof(device_info));
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002286 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07002287 }
2288 else
2289 {
2290 read_device_info_flash(dev);
2291 }
2292}
2293
2294void reset_device_info()
2295{
2296 dprintf(ALWAYS, "reset_device_info called.");
Shashank Mittala0032282011-08-26 14:50:11 -07002297 device.is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07002298 write_device_info(&device);
2299}
2300
2301void set_device_root()
2302{
2303 dprintf(ALWAYS, "set_device_root called.");
Shashank Mittala0032282011-08-26 14:50:11 -07002304 device.is_tampered = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -07002305 write_device_info(&device);
2306}
2307
lijuang4ece1e72015-08-14 21:02:36 +08002308/* set device unlock value
2309 * Must check FRP before call this function
2310 * Need to wipe data when unlock status changed
2311 * type 0: oem unlock
2312 * type 1: unlock critical
2313 * status 0: unlock as false
2314 * status 1: lock as true
2315 */
2316void set_device_unlock_value(int type, bool status)
lijuang21f12f52015-08-22 16:22:19 +08002317{
lijuang4ece1e72015-08-14 21:02:36 +08002318 if (type == UNLOCK)
2319 device.is_unlocked = status;
Mayank Grover889be1b2017-09-12 20:12:23 +05302320#if VERIFIED_BOOT
2321 else if (VB_V2 == target_get_vb_version() &&
2322 type == UNLOCK_CRITICAL)
2323 device.is_unlock_critical = status;
Parth Dixitddbc7352015-10-18 03:13:31 +05302324#endif
lijuang4ece1e72015-08-14 21:02:36 +08002325 write_device_info(&device);
2326}
2327
2328static void set_device_unlock(int type, bool status)
2329{
2330 int is_unlocked = -1;
2331 char response[MAX_RSP_SIZE];
2332
2333 /* check device unlock status if it is as expected */
2334 if (type == UNLOCK)
2335 is_unlocked = device.is_unlocked;
Mayank Grover889be1b2017-09-12 20:12:23 +05302336#if VERIFIED_BOOT
2337 if(VB_V2 == target_get_vb_version() &&
2338 type == UNLOCK_CRITICAL)
2339 {
2340 is_unlocked = device.is_unlock_critical;
2341 }
Parth Dixitddbc7352015-10-18 03:13:31 +05302342#endif
lijuang4ece1e72015-08-14 21:02:36 +08002343 if (is_unlocked == status) {
2344 snprintf(response, sizeof(response), "\tDevice already : %s", (status ? "unlocked!" : "locked!"));
2345 fastboot_info(response);
2346 fastboot_okay("");
2347 return;
2348 }
2349
2350 /* status is true, it means to unlock device */
2351 if (status) {
lijuang21f12f52015-08-22 16:22:19 +08002352 if(!is_allow_unlock) {
2353 fastboot_fail("oem unlock is not allowed");
2354 return;
2355 }
2356
lijuang4ece1e72015-08-14 21:02:36 +08002357#if FBCON_DISPLAY_MSG
lijuang9a7d3b92015-11-30 14:41:24 +08002358 display_unlock_menu(type);
lijuang4ece1e72015-08-14 21:02:36 +08002359 fastboot_okay("");
2360 return;
2361#else
2362 if (type == UNLOCK) {
2363 fastboot_fail("Need wipe userdata. Do 'fastboot oem unlock-go'");
2364 return;
2365 }
2366#endif
lijuang21f12f52015-08-22 16:22:19 +08002367 }
lijuang4ece1e72015-08-14 21:02:36 +08002368
2369 set_device_unlock_value(type, status);
2370
2371 /* wipe data */
2372 struct recovery_message msg;
Kishor PK60a68212017-05-08 16:55:57 +05302373 memset(&msg, 0, sizeof(msg));
lijuang4ece1e72015-08-14 21:02:36 +08002374 snprintf(msg.recovery, sizeof(msg.recovery), "recovery\n--wipe_data");
2375 write_misc(0, &msg, sizeof(msg));
2376
2377 fastboot_okay("");
2378 reboot_device(RECOVERY_MODE);
lijuang21f12f52015-08-22 16:22:19 +08002379}
2380
lijuang511a2b52015-08-14 20:50:51 +08002381static bool critical_flash_allowed(const char * entry)
2382{
2383 uint32_t i = 0;
2384 if (entry == NULL)
2385 return false;
2386
2387 for (i = 0; i < ARRAY_SIZE(critical_flash_allowed_ptn); i++) {
2388 if(!strcmp(entry, critical_flash_allowed_ptn[i]))
2389 return true;
2390 }
2391 return false;
Matthew Qin271927e2015-03-31 22:07:07 -04002392}
2393
2394#if DEVICE_TREE
Amol Jadicb524072012-08-09 16:40:18 -07002395int copy_dtb(uint8_t *boot_image_start, unsigned int scratch_offset)
2396{
2397 uint32 dt_image_offset = 0;
Amol Jadicb524072012-08-09 16:40:18 -07002398 uint32_t n;
Amol Jadicb524072012-08-09 16:40:18 -07002399 struct dt_table *table;
2400 struct dt_entry dt_entry;
2401 uint32_t dt_hdr_size;
2402 unsigned int compressed_size = 0;
2403 unsigned int dtb_size = 0;
2404 unsigned int out_avai_len = 0;
2405 unsigned char *out_addr = NULL;
2406 unsigned char *best_match_dt_addr = NULL;
2407 int rc;
2408
2409 struct boot_img_hdr *hdr = (struct boot_img_hdr *) (boot_image_start);
2410
Parth Dixit4097b622016-03-15 14:42:27 +05302411#ifndef OSVERSION_IN_BOOTIMAGE
2412 dt_size = hdr->dt_size;
2413#endif
2414
2415 if(dt_size != 0) {
Amol Jadicb524072012-08-09 16:40:18 -07002416 /* add kernel offset */
2417 dt_image_offset += page_size;
2418 n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
2419 dt_image_offset += n;
2420
2421 /* add ramdisk offset */
2422 n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
2423 dt_image_offset += n;
2424
2425 /* add second offset */
2426 if(hdr->second_size != 0) {
2427 n = ROUND_TO_PAGE(hdr->second_size, page_mask);
2428 dt_image_offset += n;
2429 }
2430
2431 /* offset now point to start of dt.img */
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07002432 table = (struct dt_table*)(boot_image_start + dt_image_offset);
Amol Jadicb524072012-08-09 16:40:18 -07002433
Deepa Dinamani19648b42013-09-05 17:05:55 -07002434 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Amol Jadicb524072012-08-09 16:40:18 -07002435 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
2436 return -1;
2437 }
P.V. Phani Kumar82916762016-03-09 09:20:19 +05302438
2439 /* Its Error if, dt_hdr_size (table->num_entries * dt_entry size + Dev_Tree Header)
2440 goes beyound hdr->dt_size*/
Parth Dixit4097b622016-03-15 14:42:27 +05302441 if (dt_hdr_size > ROUND_TO_PAGE(dt_size,hdr->page_size)) {
P.V. Phani Kumar82916762016-03-09 09:20:19 +05302442 dprintf(CRITICAL, "ERROR: Invalid Device Tree size \n");
2443 return -1;
2444 }
2445
Joel Kingaa335dc2013-06-03 16:11:08 -07002446 /* Find index of device tree within device tree table */
2447 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Amol Jadicb524072012-08-09 16:40:18 -07002448 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
2449 return -1;
2450 }
2451
Matthew Qin271927e2015-03-31 22:07:07 -04002452 best_match_dt_addr = (unsigned char *)boot_image_start + dt_image_offset + dt_entry.offset;
2453 if (is_gzip_package(best_match_dt_addr, dt_entry.size))
2454 {
2455 out_addr = (unsigned char *)target_get_scratch_address() + scratch_offset;
2456 out_avai_len = target_get_max_flash_size() - scratch_offset;
Matthew Qin0b15b322015-05-19 05:20:54 -04002457 dprintf(INFO, "decompressing dtb: start\n");
Matthew Qin271927e2015-03-31 22:07:07 -04002458 rc = decompress(best_match_dt_addr,
2459 dt_entry.size, out_addr, out_avai_len,
2460 &compressed_size, &dtb_size);
2461 if (rc)
2462 {
Matthew Qin0b15b322015-05-19 05:20:54 -04002463 dprintf(CRITICAL, "decompressing dtb failed!!!\n");
Matthew Qin271927e2015-03-31 22:07:07 -04002464 ASSERT(0);
2465 }
2466
Matthew Qin0b15b322015-05-19 05:20:54 -04002467 dprintf(INFO, "decompressing dtb: done\n");
Matthew Qin271927e2015-03-31 22:07:07 -04002468 best_match_dt_addr = out_addr;
2469 } else {
2470 dtb_size = dt_entry.size;
2471 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002472 /* Validate and Read device device tree in the "tags_add */
Matthew Qin271927e2015-03-31 22:07:07 -04002473 if (check_aboot_addr_range_overlap(hdr->tags_addr, dtb_size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002474 {
2475 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
2476 return -1;
2477 }
2478
Amol Jadicb524072012-08-09 16:40:18 -07002479 /* Read device device tree in the "tags_add */
Matthew Qin271927e2015-03-31 22:07:07 -04002480 memmove((void*) hdr->tags_addr, (void *)best_match_dt_addr, dtb_size);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002481 } else
2482 return -1;
Amol Jadicb524072012-08-09 16:40:18 -07002483
2484 /* Everything looks fine. Return success. */
2485 return 0;
2486}
2487#endif
2488
Brian Swetland9c4c0752009-01-25 16:23:50 -08002489void cmd_boot(const char *arg, void *data, unsigned sz)
2490{
Amit Blay8a510302015-08-17 09:20:01 +03002491#ifdef MDTP_SUPPORT
2492 static bool is_mdtp_activated = 0;
2493#endif /* MDTP_SUPPORT */
Brian Swetland9c4c0752009-01-25 16:23:50 -08002494 unsigned kernel_actual;
2495 unsigned ramdisk_actual;
Kishor PKd8ddcad2017-07-27 13:53:57 +05302496 unsigned second_actual;
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002497 uint32_t image_actual;
2498 uint32_t dt_actual = 0;
P.V. Phani Kumareecfd822016-03-09 20:09:03 +05302499 uint32_t sig_actual = 0;
Kishor PK5134b332017-05-09 17:50:08 +05302500 uint32_t sig_size = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08002501 struct boot_img_hdr *hdr = NULL;
2502 struct kernel64_hdr *kptr = NULL;
Brian Swetland9c4c0752009-01-25 16:23:50 -08002503 char *ptr = ((char*) data);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002504 int ret = 0;
2505 uint8_t dtb_copied = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08002506 unsigned int out_len = 0;
2507 unsigned int out_avai_len = 0;
2508 unsigned char *out_addr = NULL;
2509 uint32_t dtb_offset = 0;
2510 unsigned char *kernel_start_addr = NULL;
2511 unsigned int kernel_size = 0;
Matthew Qin271927e2015-03-31 22:07:07 -04002512 unsigned int scratch_offset = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08002513
lijuang2008ff22016-03-07 17:56:27 +08002514#if FBCON_DISPLAY_MSG
2515 /* Exit keys' detection thread firstly */
2516 exit_menu_keys_detection();
2517#endif
2518
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002519#if VERIFIED_BOOT
Channagoud Kadabi6d5375e2015-06-23 17:15:42 -07002520 if(target_build_variant_user() && !device.is_unlocked)
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002521 {
2522 fastboot_fail("unlock device to use this command");
lijuang2008ff22016-03-07 17:56:27 +08002523 goto boot_failed;
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002524 }
2525#endif
2526
Brian Swetland9c4c0752009-01-25 16:23:50 -08002527 if (sz < sizeof(hdr)) {
2528 fastboot_fail("invalid bootimage header");
lijuang2008ff22016-03-07 17:56:27 +08002529 goto boot_failed;
Brian Swetland9c4c0752009-01-25 16:23:50 -08002530 }
2531
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002532 hdr = (struct boot_img_hdr *)data;
Brian Swetland9c4c0752009-01-25 16:23:50 -08002533
2534 /* ensure commandline is terminated */
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002535 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -08002536
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002537 if(target_is_emmc_boot() && hdr->page_size) {
2538 page_size = hdr->page_size;
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07002539 page_mask = page_size - 1;
2540 }
2541
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002542 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
2543 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Kishor PKd8ddcad2017-07-27 13:53:57 +05302544 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002545#if DEVICE_TREE
Parth Dixit4097b622016-03-15 14:42:27 +05302546#ifndef OSVERSION_IN_BOOTIMAGE
2547 dt_size = hdr->dt_size;
2548#endif
2549 dt_actual = ROUND_TO_PAGE(dt_size, page_mask);
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002550#endif
2551
2552 image_actual = ADD_OF(page_size, kernel_actual);
2553 image_actual = ADD_OF(image_actual, ramdisk_actual);
Kishor PKd8ddcad2017-07-27 13:53:57 +05302554 image_actual = ADD_OF(image_actual, second_actual);
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002555 image_actual = ADD_OF(image_actual, dt_actual);
2556
Kishor PK5134b332017-05-09 17:50:08 +05302557 /* Checking to prevent oob access in read_der_message_length */
2558 if (image_actual > sz) {
2559 fastboot_fail("bootimage header fields are invalid");
2560 goto boot_failed;
2561 }
2562 sig_size = sz - image_actual;
2563
P.V. Phani Kumareecfd822016-03-09 20:09:03 +05302564 if (target_use_signed_kernel() && (!device.is_unlocked)) {
2565 /* Calculate the signature length from boot image */
2566 sig_actual = read_der_message_length(
Kishor PK5134b332017-05-09 17:50:08 +05302567 (unsigned char*)(data + image_actual), sig_size);
Channagoud Kadabi3d839012014-06-26 14:26:39 -07002568 image_actual = ADD_OF(image_actual, sig_actual);
2569
Kishor PK5134b332017-05-09 17:50:08 +05302570 if (image_actual > sz) {
2571 fastboot_fail("bootimage header fields are invalid");
2572 goto boot_failed;
2573 }
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002574 }
2575
Sridhar Parasuram361e4cd2015-09-24 11:19:19 -07002576 // Initialize boot state before trying to verify boot.img
2577#if VERIFIED_BOOT
Channagoud Kadabi699466e2015-11-03 12:37:42 -08002578 boot_verifier_init();
Mayank Groverb337e932017-01-18 20:00:40 +05302579#endif
Channagoud Kadabibf6ce7d2015-09-17 13:25:35 -07002580 /* Handle overflow if the input image size is greater than
2581 * boot image buffer can hold
2582 */
2583 if ((target_get_max_flash_size() - (image_actual - sig_actual)) < page_size)
2584 {
2585 fastboot_fail("booimage: size is greater than boot image buffer can hold");
lijuang2008ff22016-03-07 17:56:27 +08002586 goto boot_failed;
Channagoud Kadabibf6ce7d2015-09-17 13:25:35 -07002587 }
Sridhar Parasuram361e4cd2015-09-24 11:19:19 -07002588
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002589 /* Verify the boot image
2590 * device & page_size are initialized in aboot_init
2591 */
Sridhar Parasuram7ae9fbc2015-09-17 09:56:30 -07002592 if (target_use_signed_kernel() && (!device.is_unlocked)) {
Channagoud Kadabi3d839012014-06-26 14:26:39 -07002593 /* Pass size excluding signature size, otherwise we would try to
2594 * access signature beyond its length
2595 */
2596 verify_signed_bootimg((uint32_t)data, (image_actual - sig_actual));
Sridhar Parasuram7ae9fbc2015-09-17 09:56:30 -07002597 }
Amit Blay4aa292f2015-04-28 21:55:59 +03002598#ifdef MDTP_SUPPORT
2599 else
2600 {
Shay Nachmani062aa3f2015-05-17 17:28:44 +03002601 /* fastboot boot is not allowed when MDTP is activated */
Amit Blay4aa292f2015-04-28 21:55:59 +03002602 mdtp_ext_partition_verification_t ext_partition;
Amit Blay8a510302015-08-17 09:20:01 +03002603
2604 if (!is_mdtp_activated) {
2605 ext_partition.partition = MDTP_PARTITION_NONE;
2606 mdtp_fwlock_verify_lock(&ext_partition);
2607 }
Amit Blay4aa292f2015-04-28 21:55:59 +03002608 }
Shay Nachmani062aa3f2015-05-17 17:28:44 +03002609
Amir Kotzer7c768c02016-04-13 09:08:05 +03002610 /* If mdtp state cannot be validate, block fastboot boot*/
2611 if(mdtp_activated(&is_mdtp_activated)){
2612 dprintf(CRITICAL, "mdtp_activated cannot validate state.\n");
2613 dprintf(CRITICAL, "Can not proceed with fastboot boot command.\n");
2614 goto boot_failed;
2615 }
Shay Nachmani062aa3f2015-05-17 17:28:44 +03002616 if(is_mdtp_activated){
2617 dprintf(CRITICAL, "fastboot boot command is not available.\n");
lijuang2008ff22016-03-07 17:56:27 +08002618 goto boot_failed;
Shay Nachmani062aa3f2015-05-17 17:28:44 +03002619 }
Amit Blay4aa292f2015-04-28 21:55:59 +03002620#endif /* MDTP_SUPPORT */
2621
Sridhar Parasuram361e4cd2015-09-24 11:19:19 -07002622#if VERIFIED_BOOT
Mayank Grover889be1b2017-09-12 20:12:23 +05302623 if (VB_V2 == target_get_vb_version())
2624 {
2625 /* set boot and system versions. */
2626 set_os_version((unsigned char *)data);
2627 // send root of trust
2628 if(!send_rot_command((uint32_t)device.is_unlocked))
2629 ASSERT(0);
2630 }
Parth Dixitbc9b6492015-10-18 00:41:38 +05302631#endif
Channagoud Kadabia22144f2013-03-20 11:49:01 -07002632 /*
Matthew Qinbb7923d2015-02-09 10:56:09 +08002633 * Check if the kernel image is a gzip package. If yes, need to decompress it.
2634 * If not, continue booting.
2635 */
2636 if (is_gzip_package((unsigned char *)(data + page_size), hdr->kernel_size))
2637 {
2638 out_addr = (unsigned char *)target_get_scratch_address();
2639 out_addr = (unsigned char *)(out_addr + image_actual + page_size);
2640 out_avai_len = target_get_max_flash_size() - image_actual - page_size;
Matthew Qin0b15b322015-05-19 05:20:54 -04002641 dprintf(INFO, "decompressing kernel image: start\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08002642 ret = decompress((unsigned char *)(ptr + page_size),
2643 hdr->kernel_size, out_addr, out_avai_len,
2644 &dtb_offset, &out_len);
2645 if (ret)
2646 {
Matthew Qin0b15b322015-05-19 05:20:54 -04002647 dprintf(CRITICAL, "decompressing image failed!!!\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08002648 ASSERT(0);
2649 }
2650
Matthew Qin0b15b322015-05-19 05:20:54 -04002651 dprintf(INFO, "decompressing kernel image: done\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08002652 kptr = (struct kernel64_hdr *)out_addr;
2653 kernel_start_addr = out_addr;
2654 kernel_size = out_len;
2655 } else {
2656 kptr = (struct kernel64_hdr*)((char *)data + page_size);
2657 kernel_start_addr = (unsigned char *)((char *)data + page_size);
2658 kernel_size = hdr->kernel_size;
2659 }
2660
2661 /*
Channagoud Kadabia22144f2013-03-20 11:49:01 -07002662 * Update the kernel/ramdisk/tags address if the boot image header
2663 * has default values, these default values come from mkbootimg when
2664 * the boot image is flashed using fastboot flash:raw
2665 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08002666 update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr));
Dima Zavin3cadfff2013-03-21 14:30:48 -07002667
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002668 /* Get virtual addresses since the hdr saves physical addresses. */
2669 hdr->kernel_addr = VA(hdr->kernel_addr);
2670 hdr->ramdisk_addr = VA(hdr->ramdisk_addr);
2671 hdr->tags_addr = VA(hdr->tags_addr);
Brian Swetland9c4c0752009-01-25 16:23:50 -08002672
Matthew Qinbb7923d2015-02-09 10:56:09 +08002673 kernel_size = ROUND_TO_PAGE(kernel_size, page_mask);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002674 /* Check if the addresses in the header are valid. */
Matthew Qinbb7923d2015-02-09 10:56:09 +08002675 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_size) ||
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002676 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
2677 {
2678 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
lijuang2008ff22016-03-07 17:56:27 +08002679 goto boot_failed;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002680 }
2681
Amol Jadicb524072012-08-09 16:40:18 -07002682#if DEVICE_TREE
Matthew Qin271927e2015-03-31 22:07:07 -04002683 scratch_offset = image_actual + page_size + out_len;
Amol Jadicb524072012-08-09 16:40:18 -07002684 /* find correct dtb and copy it to right location */
Matthew Qin271927e2015-03-31 22:07:07 -04002685 ret = copy_dtb(data, scratch_offset);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002686
2687 dtb_copied = !ret ? 1 : 0;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002688#else
2689 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
2690 {
2691 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
lijuang2008ff22016-03-07 17:56:27 +08002692 goto boot_failed;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002693 }
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002694#endif
2695
2696 /* Load ramdisk & kernel */
2697 memmove((void*) hdr->ramdisk_addr, ptr + page_size + kernel_actual, hdr->ramdisk_size);
Matthew Qinbb7923d2015-02-09 10:56:09 +08002698 memmove((void*) hdr->kernel_addr, (char*) (kernel_start_addr), kernel_size);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002699
2700#if DEVICE_TREE
Matthew Qinbb7923d2015-02-09 10:56:09 +08002701 if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual))
2702 {
2703 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
lijuang2008ff22016-03-07 17:56:27 +08002704 goto boot_failed;
Matthew Qinbb7923d2015-02-09 10:56:09 +08002705 }
2706
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002707 /*
2708 * If dtb is not found look for appended DTB in the kernel.
2709 * If appended dev tree is found, update the atags with
2710 * memory address to the DTB appended location on RAM.
2711 * Else update with the atags address in the kernel header
2712 */
2713 if (!dtb_copied) {
2714 void *dtb;
Matthew Qinbb7923d2015-02-09 10:56:09 +08002715 dtb = dev_tree_appended((void*)(ptr + page_size),
2716 hdr->kernel_size, dtb_offset,
Dima Zavine63e5572013-05-03 12:23:06 -07002717 (void *)hdr->tags_addr);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002718 if (!dtb) {
2719 fastboot_fail("dtb not found");
lijuang2008ff22016-03-07 17:56:27 +08002720 goto boot_failed;
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002721 }
Amol Jadicb524072012-08-09 16:40:18 -07002722 }
2723#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08002724
2725 fastboot_okay("");
Amol Jadi7c4316c2013-10-07 14:19:26 -07002726 fastboot_stop();
Brian Swetland9c4c0752009-01-25 16:23:50 -08002727
Dima Zavin77e41f32013-03-06 16:10:43 -08002728 boot_linux((void*) hdr->kernel_addr, (void*) hdr->tags_addr,
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002729 (const char*) hdr->cmdline, board_machtype(),
2730 (void*) hdr->ramdisk_addr, hdr->ramdisk_size);
lijuang2008ff22016-03-07 17:56:27 +08002731
2732 /* fastboot already stop, it's no need to show fastboot menu */
2733 return;
2734boot_failed:
2735#if FBCON_DISPLAY_MSG
2736 /* revert to fastboot menu if boot failed */
2737 display_fastboot_menu();
2738#endif
2739 return;
Brian Swetland9c4c0752009-01-25 16:23:50 -08002740}
2741
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002742void cmd_erase_nand(const char *arg, void *data, unsigned sz)
Dima Zavin214cc642009-01-26 11:16:21 -08002743{
2744 struct ptentry *ptn;
2745 struct ptable *ptable;
2746
2747 ptable = flash_get_ptable();
2748 if (ptable == NULL) {
2749 fastboot_fail("partition table doesn't exist");
2750 return;
2751 }
2752
2753 ptn = ptable_find(ptable, arg);
2754 if (ptn == NULL) {
2755 fastboot_fail("unknown partition name");
2756 return;
2757 }
2758
2759 if (flash_erase(ptn)) {
2760 fastboot_fail("failed to erase partition");
2761 return;
2762 }
2763 fastboot_okay("");
2764}
2765
Bikas Gurungd48bd242010-09-04 19:54:32 -07002766
2767void cmd_erase_mmc(const char *arg, void *data, unsigned sz)
2768{
2769 unsigned long long ptn = 0;
Oliver Wangcee448d2013-10-22 18:40:13 +08002770 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07002771 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002772 uint8_t lun = 0;
Vijay Kumar Pendoti0b21f462016-05-02 17:09:18 +05302773 char *footer = NULL;
Bikas Gurungd48bd242010-09-04 19:54:32 -07002774
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002775#if VERIFIED_BOOT
2776 if(!strcmp(arg, KEYSTORE_PTN_NAME))
2777 {
2778 if(!device.is_unlocked)
2779 {
2780 fastboot_fail("unlock device to erase keystore");
2781 return;
2782 }
2783 }
2784#endif
2785
Kinson Chikf1a43512011-07-14 11:28:39 -07002786 index = partition_get_index(arg);
2787 ptn = partition_get_offset(index);
Oliver Wangcee448d2013-10-22 18:40:13 +08002788 size = partition_get_size(index);
Neeti Desaica8c9602011-10-06 11:40:00 -07002789
Kinson Chikf1a43512011-07-14 11:28:39 -07002790 if(ptn == 0) {
Neeti Desaica8c9602011-10-06 11:40:00 -07002791 fastboot_fail("Partition table doesn't exist\n");
Bikas Gurungd48bd242010-09-04 19:54:32 -07002792 return;
2793 }
Kun Liang2f1601a2013-08-12 16:29:54 +08002794
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002795 lun = partition_get_lun(index);
2796 mmc_set_lun(lun);
2797
Sridhar Parasuramd7957122015-02-27 11:33:40 -08002798 if (platform_boot_dev_isemmc())
2799 {
2800 if (mmc_erase_card(ptn, size)) {
2801 fastboot_fail("failed to erase partition\n");
2802 return;
2803 }
2804 } else {
2805 BUF_DMA_ALIGN(out, DEFAULT_ERASE_SIZE);
2806 size = partition_get_size(index);
2807 if (size > DEFAULT_ERASE_SIZE)
2808 size = DEFAULT_ERASE_SIZE;
Kun Liang2f1601a2013-08-12 16:29:54 +08002809
Sridhar Parasuramd7957122015-02-27 11:33:40 -08002810 /* Simple inefficient version of erase. Just writing
2811 0 in first several blocks */
2812 if (mmc_write(ptn , size, (unsigned int *)out)) {
2813 fastboot_fail("failed to erase partition");
2814 return;
2815 }
Vijay Kumar Pendoti0b21f462016-05-02 17:09:18 +05302816 /*Erase FDE metadata at the userdata footer*/
2817 if(!(strncmp(arg, "userdata", 8)))
2818 {
2819 footer = memalign(CACHE_LINE, FOOTER_SIZE);
2820 memset((void *)footer, 0, FOOTER_SIZE);
2821
2822 size = partition_get_size(index);
2823
2824 if (mmc_write((ptn + size) - FOOTER_SIZE , FOOTER_SIZE, (unsigned int *)footer)) {
2825 fastboot_fail("failed to erase userdata footer");
2826 free(footer);
2827 return;
2828 }
2829 free(footer);
2830 }
Bikas Gurungd48bd242010-09-04 19:54:32 -07002831 }
Sridhar Parasuram32b30662015-07-10 13:33:22 -07002832#if VERIFIED_BOOT
Mayank Grover889be1b2017-09-12 20:12:23 +05302833 if (VB_V2 == target_get_vb_version() &&
2834 !(strncmp(arg, "userdata", 8)) &&
2835 send_delete_keys_to_tz())
Sridhar Parasuram32b30662015-07-10 13:33:22 -07002836 ASSERT(0);
2837#endif
Bikas Gurungd48bd242010-09-04 19:54:32 -07002838 fastboot_okay("");
2839}
2840
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002841void cmd_erase(const char *arg, void *data, unsigned sz)
2842{
Channagoud Kadabice3cf422015-04-17 16:02:30 -07002843#if VERIFIED_BOOT
Channagoud Kadabi35297672015-06-13 11:09:49 -07002844 if (target_build_variant_user())
Channagoud Kadabice3cf422015-04-17 16:02:30 -07002845 {
Sridhar Parasuramc32d07d2015-07-12 10:57:48 -07002846 if(!device.is_unlocked)
Channagoud Kadabice3cf422015-04-17 16:02:30 -07002847 {
Channagoud Kadabi35297672015-06-13 11:09:49 -07002848 fastboot_fail("device is locked. Cannot erase");
Channagoud Kadabice3cf422015-04-17 16:02:30 -07002849 return;
2850 }
2851 }
2852#endif
2853
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002854 if(target_is_emmc_boot())
2855 cmd_erase_mmc(arg, data, sz);
2856 else
2857 cmd_erase_nand(arg, data, sz);
2858}
Bikas Gurungd48bd242010-09-04 19:54:32 -07002859
Ajay Dudani5c761132011-04-07 20:19:04 -07002860void cmd_flash_mmc_img(const char *arg, void *data, unsigned sz)
Shashank Mittal23b8f422010-04-16 19:27:21 -07002861{
2862 unsigned long long ptn = 0;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07002863 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07002864 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002865 char *token = NULL;
2866 char *pname = NULL;
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07002867 char *sp;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002868 uint8_t lun = 0;
2869 bool lun_set = false;
Mayank Grover351a75e2017-05-30 20:06:08 +05302870 int current_active_slot = INVALID;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07002871
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07002872 token = strtok_r((char *)arg, ":", &sp);
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002873 pname = token;
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07002874 token = strtok_r(NULL, ":", &sp);
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002875 if(token)
2876 {
2877 lun = atoi(token);
2878 mmc_set_lun(lun);
2879 lun_set = true;
2880 }
2881
Mao Jinlong226f33a2014-07-04 17:24:10 +08002882 if (pname)
Greg Grisco6e754772011-06-23 12:19:39 -07002883 {
Channagoud Kadabiad259832015-05-29 11:14:17 -07002884 if (!strncmp(pname, "frp-unlock", strlen("frp-unlock")))
2885 {
2886 if (!aboot_frp_unlock(pname, data, sz))
2887 {
2888 fastboot_info("FRP unlock successful");
2889 fastboot_okay("");
2890 }
2891 else
2892 fastboot_fail("Secret key is invalid, please update the bootloader with secret key");
2893
2894 return;
2895 }
2896
Mao Jinlong226f33a2014-07-04 17:24:10 +08002897 if (!strcmp(pname, "partition"))
2898 {
2899 dprintf(INFO, "Attempt to write partition image.\n");
2900 if (write_partition(sz, (unsigned char *) data)) {
2901 fastboot_fail("failed to write partition");
Greg Grisco6e754772011-06-23 12:19:39 -07002902 return;
2903 }
Mayank Grover351a75e2017-05-30 20:06:08 +05302904
2905 /* Rescan partition table to ensure we have multislot support*/
2906 if (partition_scan_for_multislot())
2907 {
2908 current_active_slot = partition_find_active_slot();
2909 dprintf(INFO, "Multislot supported: Slot %s active",
2910 (SUFFIX_SLOT(current_active_slot)));
2911 }
2912 partition_mark_active_slot(current_active_slot);
Greg Grisco6e754772011-06-23 12:19:39 -07002913 }
Mao Jinlong226f33a2014-07-04 17:24:10 +08002914 else
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002915 {
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002916#if VERIFIED_BOOT
2917 if(!strcmp(pname, KEYSTORE_PTN_NAME))
2918 {
2919 if(!device.is_unlocked)
2920 {
2921 fastboot_fail("unlock device to flash keystore");
2922 return;
2923 }
Gaurav Nebhwanic7313cc2015-12-15 22:25:04 +05302924 if(!boot_verify_validate_keystore((unsigned char *)data,sz))
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002925 {
2926 fastboot_fail("image is not a keystore file");
2927 return;
2928 }
2929 }
2930#endif
Mao Jinlong226f33a2014-07-04 17:24:10 +08002931 index = partition_get_index(pname);
2932 ptn = partition_get_offset(index);
2933 if(ptn == 0) {
2934 fastboot_fail("partition table doesn't exist");
2935 return;
2936 }
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002937
Mayank Grover351a75e2017-05-30 20:06:08 +05302938 if (!strncmp(pname, "boot", strlen("boot"))
2939 || !strcmp(pname, "recovery"))
2940 {
Mao Jinlong226f33a2014-07-04 17:24:10 +08002941 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
2942 fastboot_fail("image is not a boot image");
2943 return;
2944 }
Mayank Grover351a75e2017-05-30 20:06:08 +05302945
2946 /* Reset multislot_partition attributes in case of flashing boot */
2947 if (partition_multislot_is_supported())
2948 {
2949 partition_reset_attributes(index);
2950 }
Mao Jinlong226f33a2014-07-04 17:24:10 +08002951 }
2952
2953 if(!lun_set)
2954 {
2955 lun = partition_get_lun(index);
2956 mmc_set_lun(lun);
2957 }
2958
2959 size = partition_get_size(index);
Gaurav Nebhwani4d2389c2016-03-17 21:10:05 +05302960 if (ROUND_TO_PAGE(sz, mmc_blocksize_mask) > size) {
Mao Jinlong226f33a2014-07-04 17:24:10 +08002961 fastboot_fail("size too large");
2962 return;
2963 }
2964 else if (mmc_write(ptn , sz, (unsigned int *)data)) {
2965 fastboot_fail("flash write failure");
2966 return;
2967 }
Greg Grisco6e754772011-06-23 12:19:39 -07002968 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07002969 }
2970 fastboot_okay("");
2971 return;
2972}
2973
Ajay Dudanide984792015-03-02 09:57:41 -08002974void cmd_flash_meta_img(const char *arg, void *data, unsigned sz)
2975{
2976 int i, images;
2977 meta_header_t *meta_header;
2978 img_header_entry_t *img_header_entry;
Parth Dixit3e2d3032016-03-04 17:11:52 +05302979 /*End of the image address*/
2980 uintptr_t data_end;
2981
2982 if( (UINT_MAX - sz) > (uintptr_t)data )
2983 data_end = (uintptr_t)data + sz;
2984 else
2985 {
2986 fastboot_fail("Cannot flash: image header corrupt");
2987 return;
2988 }
2989
2990 if( data_end < ((uintptr_t)data + sizeof(meta_header_t)))
2991 {
2992 fastboot_fail("Cannot flash: image header corrupt");
2993 return;
2994 }
Ajay Dudanide984792015-03-02 09:57:41 -08002995
lijuang8ee21882016-04-19 16:57:11 +08002996 /* If device is locked:
2997 * Forbid to flash image to avoid the device to bypass the image
2998 * which with "any" name other than bootloader. Because it maybe
2999 * a meta package of all partitions.
3000 */
3001#if VERIFIED_BOOT
3002 if (target_build_variant_user()) {
3003 if (!device.is_unlocked) {
3004 fastboot_fail("Device is locked, meta image flashing is not allowed");
3005 return;
3006 }
Mayank Grover889be1b2017-09-12 20:12:23 +05303007
3008 if (VB_V2 == target_get_vb_version() &&
3009 !device.is_unlock_critical)
3010 {
lijuang8ee21882016-04-19 16:57:11 +08003011 fastboot_fail("Device is critical locked, Meta image flashing is not allowed");
3012 return;
3013 }
lijuang8ee21882016-04-19 16:57:11 +08003014 }
3015#endif
3016
Ajay Dudanide984792015-03-02 09:57:41 -08003017 meta_header = (meta_header_t*) data;
Parth Dixit3e2d3032016-03-04 17:11:52 +05303018 if( data_end < ((uintptr_t)data + meta_header->img_hdr_sz))
3019 {
3020 fastboot_fail("Cannot flash: image header corrupt");
3021 return;
3022 }
Ajay Dudanide984792015-03-02 09:57:41 -08003023 img_header_entry = (img_header_entry_t*) (data+sizeof(meta_header_t));
3024
3025 images = meta_header->img_hdr_sz / sizeof(img_header_entry_t);
3026
3027 for (i=0; i<images; i++) {
3028
3029 if((img_header_entry[i].ptn_name == NULL) ||
3030 (img_header_entry[i].start_offset == 0) ||
3031 (img_header_entry[i].size == 0))
3032 break;
Kishor PK49831502017-04-21 17:55:43 +05303033 if ((UINT_MAX - img_header_entry[i].start_offset) < (uintptr_t)data) {
3034 fastboot_fail("Integer overflow detected in start_offset of img");
3035 break;
3036 }
3037 else if ((UINT_MAX - (img_header_entry[i].start_offset + (uintptr_t)data)) < img_header_entry[i].size) {
3038 fastboot_fail("Integer overflow detected in size of img");
3039 break;
3040 }
Parth Dixit3e2d3032016-03-04 17:11:52 +05303041 if( data_end < ((uintptr_t)data + img_header_entry[i].start_offset
3042 + img_header_entry[i].size) )
3043 {
3044 fastboot_fail("Cannot flash: image size mismatch");
3045 break;
3046 }
3047
Ajay Dudanide984792015-03-02 09:57:41 -08003048 cmd_flash_mmc_img(img_header_entry[i].ptn_name,
3049 (void *) data + img_header_entry[i].start_offset,
3050 img_header_entry[i].size);
3051 }
3052
Channagoud Kadabiaafbbfc2015-03-04 17:52:56 -08003053 if (!strncmp(arg, "bootloader", strlen("bootloader")))
3054 {
3055 strlcpy(device.bootloader_version, TARGET(BOARD), MAX_VERSION_LEN);
3056 strlcat(device.bootloader_version, "-", MAX_VERSION_LEN);
3057 strlcat(device.bootloader_version, meta_header->img_version, MAX_VERSION_LEN);
3058 }
3059 else
3060 {
3061 strlcpy(device.radio_version, TARGET(BOARD), MAX_VERSION_LEN);
3062 strlcat(device.radio_version, "-", MAX_VERSION_LEN);
3063 strlcat(device.radio_version, meta_header->img_version, MAX_VERSION_LEN);
3064 }
3065
3066 write_device_info(&device);
Ajay Dudanide984792015-03-02 09:57:41 -08003067 fastboot_okay("");
3068 return;
3069}
3070
Ajay Dudani5c761132011-04-07 20:19:04 -07003071void cmd_flash_mmc_sparse_img(const char *arg, void *data, unsigned sz)
3072{
3073 unsigned int chunk;
wufeng.jiang813dc352015-06-02 23:02:46 -04003074 uint64_t chunk_data_sz;
Channagoud Kadabif627cf82013-09-09 14:08:20 -07003075 uint32_t *fill_buf = NULL;
3076 uint32_t fill_val;
Mayank Grover4f50bba2017-07-19 18:17:08 +05303077 uint32_t blk_sz_actual = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07003078 sparse_header_t *sparse_header;
3079 chunk_header_t *chunk_header;
Ajay Dudaniab18f022011-05-12 14:39:22 -07003080 uint32_t total_blocks = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07003081 unsigned long long ptn = 0;
Channagoud Kadabi65b91002011-10-11 17:34:33 +05303082 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07003083 int index = INVALID_PTN;
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05303084 uint32_t i;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003085 uint8_t lun = 0;
vijay kumar800255e2015-04-24 20:26:19 +05303086 /*End of the sparse image address*/
Parth Dixit64b1a482016-03-07 16:31:26 +05303087 uintptr_t data_end = (uintptr_t)data + sz;
Ajay Dudani5c761132011-04-07 20:19:04 -07003088
Kinson Chikf1a43512011-07-14 11:28:39 -07003089 index = partition_get_index(arg);
3090 ptn = partition_get_offset(index);
3091 if(ptn == 0) {
Ajay Dudani5c761132011-04-07 20:19:04 -07003092 fastboot_fail("partition table doesn't exist");
3093 return;
3094 }
3095
Channagoud Kadabi65b91002011-10-11 17:34:33 +05303096 size = partition_get_size(index);
Channagoud Kadabi65b91002011-10-11 17:34:33 +05303097
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08003098 lun = partition_get_lun(index);
3099 mmc_set_lun(lun);
3100
vijay kumar800255e2015-04-24 20:26:19 +05303101 if (sz < sizeof(sparse_header_t)) {
3102 fastboot_fail("size too low");
3103 return;
3104 }
3105
Ajay Dudani5c761132011-04-07 20:19:04 -07003106 /* Read and skip over sparse image header */
3107 sparse_header = (sparse_header_t *) data;
vijay kumar800255e2015-04-24 20:26:19 +05303108
Mayank Grover48bd9bb2017-07-19 12:04:16 +05303109 if (!sparse_header->blk_sz || (sparse_header->blk_sz % 4)){
3110 fastboot_fail("Invalid block size\n");
3111 return;
3112 }
3113
vijay kumar1321f342015-03-27 12:13:42 +05303114 if (((uint64_t)sparse_header->total_blks * (uint64_t)sparse_header->blk_sz) > size) {
Ajay Dudani876b3282012-12-21 14:12:17 -08003115 fastboot_fail("size too large");
3116 return;
3117 }
3118
vijay kumarde4fcf62015-04-23 13:05:49 +05303119 data += sizeof(sparse_header_t);
vijay kumar800255e2015-04-24 20:26:19 +05303120
Parth Dixit64b1a482016-03-07 16:31:26 +05303121 if (data_end < (uintptr_t)data) {
vijay kumar800255e2015-04-24 20:26:19 +05303122 fastboot_fail("buffer overreads occured due to invalid sparse header");
3123 return;
3124 }
3125
vijay kumarde4fcf62015-04-23 13:05:49 +05303126 if(sparse_header->file_hdr_sz != sizeof(sparse_header_t))
Ajay Dudani5c761132011-04-07 20:19:04 -07003127 {
vijay kumarde4fcf62015-04-23 13:05:49 +05303128 fastboot_fail("sparse header size mismatch");
3129 return;
Ajay Dudani5c761132011-04-07 20:19:04 -07003130 }
3131
Ajay Dudanib06c05f2011-05-12 14:46:10 -07003132 dprintf (SPEW, "=== Sparse Image Header ===\n");
3133 dprintf (SPEW, "magic: 0x%x\n", sparse_header->magic);
3134 dprintf (SPEW, "major_version: 0x%x\n", sparse_header->major_version);
3135 dprintf (SPEW, "minor_version: 0x%x\n", sparse_header->minor_version);
3136 dprintf (SPEW, "file_hdr_sz: %d\n", sparse_header->file_hdr_sz);
3137 dprintf (SPEW, "chunk_hdr_sz: %d\n", sparse_header->chunk_hdr_sz);
3138 dprintf (SPEW, "blk_sz: %d\n", sparse_header->blk_sz);
3139 dprintf (SPEW, "total_blks: %d\n", sparse_header->total_blks);
3140 dprintf (SPEW, "total_chunks: %d\n", sparse_header->total_chunks);
Ajay Dudani5c761132011-04-07 20:19:04 -07003141
3142 /* Start processing chunks */
3143 for (chunk=0; chunk<sparse_header->total_chunks; chunk++)
3144 {
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05303145 /* Make sure the total image size does not exceed the partition size */
3146 if(((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz) >= size) {
3147 fastboot_fail("size too large");
3148 return;
3149 }
Ajay Dudani5c761132011-04-07 20:19:04 -07003150 /* Read and skip over chunk header */
3151 chunk_header = (chunk_header_t *) data;
3152 data += sizeof(chunk_header_t);
3153
Parth Dixit64b1a482016-03-07 16:31:26 +05303154 if (data_end < (uintptr_t)data) {
vijay kumar800255e2015-04-24 20:26:19 +05303155 fastboot_fail("buffer overreads occured due to invalid sparse header");
3156 return;
3157 }
3158
Ajay Dudani5c761132011-04-07 20:19:04 -07003159 dprintf (SPEW, "=== Chunk Header ===\n");
3160 dprintf (SPEW, "chunk_type: 0x%x\n", chunk_header->chunk_type);
3161 dprintf (SPEW, "chunk_data_sz: 0x%x\n", chunk_header->chunk_sz);
3162 dprintf (SPEW, "total_size: 0x%x\n", chunk_header->total_sz);
3163
vijay kumar800255e2015-04-24 20:26:19 +05303164 if(sparse_header->chunk_hdr_sz != sizeof(chunk_header_t))
Ajay Dudani5c761132011-04-07 20:19:04 -07003165 {
vijay kumar800255e2015-04-24 20:26:19 +05303166 fastboot_fail("chunk header size mismatch");
3167 return;
Ajay Dudani5c761132011-04-07 20:19:04 -07003168 }
3169
wufeng.jiang813dc352015-06-02 23:02:46 -04003170 chunk_data_sz = (uint64_t)sparse_header->blk_sz * chunk_header->chunk_sz;
3171
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05303172 /* Make sure that the chunk size calculated from sparse image does not
3173 * exceed partition size
3174 */
3175 if ((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz + chunk_data_sz > size)
3176 {
3177 fastboot_fail("Chunk data size exceeds partition size");
3178 return;
3179 }
3180
Ajay Dudani5c761132011-04-07 20:19:04 -07003181 switch (chunk_header->chunk_type)
3182 {
3183 case CHUNK_TYPE_RAW:
wufeng.jiang813dc352015-06-02 23:02:46 -04003184 if((uint64_t)chunk_header->total_sz != ((uint64_t)sparse_header->chunk_hdr_sz +
Ajay Dudani5c761132011-04-07 20:19:04 -07003185 chunk_data_sz))
3186 {
3187 fastboot_fail("Bogus chunk size for chunk type Raw");
3188 return;
3189 }
3190
Parth Dixit64b1a482016-03-07 16:31:26 +05303191 if (data_end < (uintptr_t)data + chunk_data_sz) {
vijay kumar800255e2015-04-24 20:26:19 +05303192 fastboot_fail("buffer overreads occured due to invalid sparse header");
3193 return;
3194 }
3195
wufeng.jiang813dc352015-06-02 23:02:46 -04003196 /* chunk_header->total_sz is uint32,So chunk_data_sz is now less than 2^32
3197 otherwise it will return in the line above
3198 */
Ajay Dudaniab18f022011-05-12 14:39:22 -07003199 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
wufeng.jiang813dc352015-06-02 23:02:46 -04003200 (uint32_t)chunk_data_sz,
Ajay Dudaniab18f022011-05-12 14:39:22 -07003201 (unsigned int*)data))
Ajay Dudani5c761132011-04-07 20:19:04 -07003202 {
3203 fastboot_fail("flash write failure");
3204 return;
3205 }
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05303206 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
3207 fastboot_fail("Bogus size for RAW chunk type");
3208 return;
3209 }
Ajay Dudani5c761132011-04-07 20:19:04 -07003210 total_blocks += chunk_header->chunk_sz;
wufeng.jiang813dc352015-06-02 23:02:46 -04003211 data += (uint32_t)chunk_data_sz;
Ajay Dudani5c761132011-04-07 20:19:04 -07003212 break;
3213
Channagoud Kadabif627cf82013-09-09 14:08:20 -07003214 case CHUNK_TYPE_FILL:
3215 if(chunk_header->total_sz != (sparse_header->chunk_hdr_sz +
3216 sizeof(uint32_t)))
3217 {
3218 fastboot_fail("Bogus chunk size for chunk type FILL");
3219 return;
3220 }
3221
Mayank Grover4f50bba2017-07-19 18:17:08 +05303222 blk_sz_actual = ROUNDUP(sparse_header->blk_sz, CACHE_LINE);
3223 /* Integer overflow detected */
3224 if (blk_sz_actual < sparse_header->blk_sz)
3225 {
3226 fastboot_fail("Invalid block size");
3227 return;
3228 }
3229
3230 fill_buf = (uint32_t *)memalign(CACHE_LINE, blk_sz_actual);
Channagoud Kadabif627cf82013-09-09 14:08:20 -07003231 if (!fill_buf)
3232 {
3233 fastboot_fail("Malloc failed for: CHUNK_TYPE_FILL");
3234 return;
3235 }
3236
Parth Dixit64b1a482016-03-07 16:31:26 +05303237 if (data_end < (uintptr_t)data + sizeof(uint32_t)) {
vijay kumar800255e2015-04-24 20:26:19 +05303238 fastboot_fail("buffer overreads occured due to invalid sparse header");
Gaurav Nebhwani163cbf82016-03-17 21:21:27 +05303239 free(fill_buf);
vijay kumar800255e2015-04-24 20:26:19 +05303240 return;
3241 }
Channagoud Kadabif627cf82013-09-09 14:08:20 -07003242 fill_val = *(uint32_t *)data;
3243 data = (char *) data + sizeof(uint32_t);
Channagoud Kadabif627cf82013-09-09 14:08:20 -07003244
3245 for (i = 0; i < (sparse_header->blk_sz / sizeof(fill_val)); i++)
3246 {
3247 fill_buf[i] = fill_val;
3248 }
3249
Gaurav Nebhwanie94cbe12016-03-17 21:16:34 +05303250 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz))
3251 {
3252 fastboot_fail("bogus size for chunk FILL type");
3253 free(fill_buf);
3254 return;
3255 }
3256
wufeng.jiang813dc352015-06-02 23:02:46 -04003257 for (i = 0; i < chunk_header->chunk_sz; i++)
Channagoud Kadabif627cf82013-09-09 14:08:20 -07003258 {
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05303259 /* Make sure that the data written to partition does not exceed partition size */
3260 if ((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz + sparse_header->blk_sz > size)
3261 {
3262 fastboot_fail("Chunk data size for fill type exceeds partition size");
Gaurav Nebhwani163cbf82016-03-17 21:21:27 +05303263 free(fill_buf);
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05303264 return;
3265 }
3266
Channagoud Kadabif627cf82013-09-09 14:08:20 -07003267 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
3268 sparse_header->blk_sz,
3269 fill_buf))
3270 {
3271 fastboot_fail("flash write failure");
3272 free(fill_buf);
3273 return;
3274 }
3275
3276 total_blocks++;
3277 }
3278
3279 free(fill_buf);
3280 break;
3281
Ajay Dudani5c761132011-04-07 20:19:04 -07003282 case CHUNK_TYPE_DONT_CARE:
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05303283 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
3284 fastboot_fail("bogus size for chunk DONT CARE type");
3285 return;
3286 }
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07003287 total_blocks += chunk_header->chunk_sz;
3288 break;
3289
Ajay Dudani5c761132011-04-07 20:19:04 -07003290 case CHUNK_TYPE_CRC:
3291 if(chunk_header->total_sz != sparse_header->chunk_hdr_sz)
3292 {
wufeng.jiang813dc352015-06-02 23:02:46 -04003293 fastboot_fail("Bogus chunk size for chunk type CRC");
Ajay Dudani5c761132011-04-07 20:19:04 -07003294 return;
3295 }
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05303296 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
3297 fastboot_fail("bogus size for chunk CRC type");
3298 return;
3299 }
Ajay Dudani5c761132011-04-07 20:19:04 -07003300 total_blocks += chunk_header->chunk_sz;
Parth Dixit64b1a482016-03-07 16:31:26 +05303301 if ((uintptr_t)data > UINT_MAX - chunk_data_sz) {
vijay kumar800255e2015-04-24 20:26:19 +05303302 fastboot_fail("integer overflow occured");
3303 return;
3304 }
wufeng.jiang813dc352015-06-02 23:02:46 -04003305 data += (uint32_t)chunk_data_sz;
Parth Dixit64b1a482016-03-07 16:31:26 +05303306 if (data_end < (uintptr_t)data) {
vijay kumar800255e2015-04-24 20:26:19 +05303307 fastboot_fail("buffer overreads occured due to invalid sparse header");
3308 return;
3309 }
Ajay Dudani5c761132011-04-07 20:19:04 -07003310 break;
3311
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07003312 default:
Channagoud Kadabif627cf82013-09-09 14:08:20 -07003313 dprintf(CRITICAL, "Unkown chunk type: %x\n",chunk_header->chunk_type);
Ajay Dudani5c761132011-04-07 20:19:04 -07003314 fastboot_fail("Unknown chunk type");
3315 return;
3316 }
3317 }
3318
Ajay Dudani0c6927b2011-05-18 11:12:16 -07003319 dprintf(INFO, "Wrote %d blocks, expected to write %d blocks\n",
3320 total_blocks, sparse_header->total_blks);
3321
3322 if(total_blocks != sparse_header->total_blks)
3323 {
3324 fastboot_fail("sparse image write failure");
3325 }
Ajay Dudani5c761132011-04-07 20:19:04 -07003326
3327 fastboot_okay("");
3328 return;
3329}
3330
3331void cmd_flash_mmc(const char *arg, void *data, unsigned sz)
3332{
3333 sparse_header_t *sparse_header;
Ajay Dudanide984792015-03-02 09:57:41 -08003334 meta_header_t *meta_header;
Ajay Dudani5c761132011-04-07 20:19:04 -07003335
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07003336#ifdef SSD_ENABLE
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -08003337 /* 8 Byte Magic + 2048 Byte xml + Encrypted Data */
3338 unsigned int *magic_number = (unsigned int *) data;
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08003339 int ret=0;
3340 uint32 major_version=0;
3341 uint32 minor_version=0;
3342
3343 ret = scm_svc_version(&major_version,&minor_version);
3344 if(!ret)
3345 {
3346 if(major_version >= 2)
3347 {
Amir Samuelovbb65ce02013-05-05 12:20:18 +03003348 if( !strcmp(arg, "ssd") || !strcmp(arg, "tqs") )
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08003349 {
3350 ret = encrypt_scm((uint32 **) &data, &sz);
3351 if (ret != 0) {
3352 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
3353 return;
3354 }
3355
Amir Samuelovbb65ce02013-05-05 12:20:18 +03003356 /* Protect only for SSD */
3357 if (!strcmp(arg, "ssd")) {
3358 ret = scm_protect_keystore((uint32 *) data, sz);
3359 if (ret != 0) {
3360 dprintf(CRITICAL, "ERROR: scm_protect_keystore Failed\n");
3361 return;
3362 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08003363 }
3364 }
3365 else
3366 {
3367 ret = decrypt_scm_v2((uint32 **) &data, &sz);
3368 if(ret != 0)
3369 {
3370 dprintf(CRITICAL,"ERROR: Decryption Failure\n");
3371 return;
3372 }
3373 }
3374 }
3375 else
3376 {
3377 if (magic_number[0] == DECRYPT_MAGIC_0 &&
3378 magic_number[1] == DECRYPT_MAGIC_1)
3379 {
3380 ret = decrypt_scm((uint32 **) &data, &sz);
3381 if (ret != 0) {
3382 dprintf(CRITICAL, "ERROR: Invalid secure image\n");
3383 return;
3384 }
3385 }
3386 else if (magic_number[0] == ENCRYPT_MAGIC_0 &&
3387 magic_number[1] == ENCRYPT_MAGIC_1)
3388 {
3389 ret = encrypt_scm((uint32 **) &data, &sz);
3390 if (ret != 0) {
3391 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
3392 return;
3393 }
3394 }
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07003395 }
3396 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08003397 else
Neeti Desai127b9e02012-03-20 16:11:23 -07003398 {
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08003399 dprintf(CRITICAL,"INVALID SVC Version\n");
3400 return;
Neeti Desai127b9e02012-03-20 16:11:23 -07003401 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08003402#endif /* SSD_ENABLE */
Neeti Desai127b9e02012-03-20 16:11:23 -07003403
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003404#if VERIFIED_BOOT
Channagoud Kadabi35297672015-06-13 11:09:49 -07003405 if (target_build_variant_user())
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003406 {
lijuang511a2b52015-08-14 20:50:51 +08003407 /* if device is locked:
3408 * common partition will not allow to be flashed
3409 * critical partition will allow to flash image.
3410 */
3411 if(!device.is_unlocked && !critical_flash_allowed(arg)) {
3412 fastboot_fail("Partition flashing is not allowed");
3413 return;
3414 }
Mayank Grover889be1b2017-09-12 20:12:23 +05303415
lijuang511a2b52015-08-14 20:50:51 +08003416 /* if device critical is locked:
3417 * common partition will allow to be flashed
3418 * critical partition will not allow to flash image.
3419 */
Mayank Grover889be1b2017-09-12 20:12:23 +05303420 if (VB_V2 == target_get_vb_version() &&
3421 !device.is_unlock_critical &&
3422 critical_flash_allowed(arg)) {
3423 fastboot_fail("Critical partition flashing is not allowed");
3424 return;
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003425 }
3426 }
3427#endif
3428
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07003429 sparse_header = (sparse_header_t *) data;
Ajay Dudanide984792015-03-02 09:57:41 -08003430 meta_header = (meta_header_t *) data;
3431 if (sparse_header->magic == SPARSE_HEADER_MAGIC)
Ajay Dudani5c761132011-04-07 20:19:04 -07003432 cmd_flash_mmc_sparse_img(arg, data, sz);
Ajay Dudanide984792015-03-02 09:57:41 -08003433 else if (meta_header->magic == META_HEADER_MAGIC)
3434 cmd_flash_meta_img(arg, data, sz);
3435 else
3436 cmd_flash_mmc_img(arg, data, sz);
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07003437
3438#if VERIFIED_BOOT
Mayank Grover889be1b2017-09-12 20:12:23 +05303439 if (VB_V2 == target_get_vb_version() &&
3440 (!strncmp(arg, "system", 6)) &&
3441 !device.verity_mode)
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07003442 // reset dm_verity mode to enforcing
3443 device.verity_mode = 1;
3444 write_device_info(&device);
Parth Dixitddbc7352015-10-18 03:13:31 +05303445#endif
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07003446
Ajay Dudani5c761132011-04-07 20:19:04 -07003447 return;
3448}
3449
Tanya Brokhman84eaaf62015-02-24 21:57:06 +02003450void cmd_updatevol(const char *vol_name, void *data, unsigned sz)
3451{
3452 struct ptentry *sys_ptn;
3453 struct ptable *ptable;
3454
3455 ptable = flash_get_ptable();
3456 if (ptable == NULL) {
3457 fastboot_fail("partition table doesn't exist");
3458 return;
3459 }
3460
3461 sys_ptn = ptable_find(ptable, "system");
3462 if (sys_ptn == NULL) {
3463 fastboot_fail("system partition not found");
3464 return;
3465 }
3466
3467 sz = ROUND_TO_PAGE(sz, page_mask);
3468 if (update_ubi_vol(sys_ptn, vol_name, data, sz))
3469 fastboot_fail("update_ubi_vol failed");
3470 else
3471 fastboot_okay("");
3472}
3473
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003474void cmd_flash_nand(const char *arg, void *data, unsigned sz)
Dima Zavin214cc642009-01-26 11:16:21 -08003475{
3476 struct ptentry *ptn;
3477 struct ptable *ptable;
3478 unsigned extra = 0;
Gaurav Nebhwanid9730712016-05-06 14:28:29 +05303479 uint64_t partition_size = 0;
Vijay Kumar Pendotic6f22a72017-05-19 22:44:34 +05303480 unsigned bytes_to_round_page = 0;
3481 unsigned rounded_size = 0;
Dima Zavin214cc642009-01-26 11:16:21 -08003482
Kishor PK38ed93d2017-04-25 14:19:26 +05303483 if((uintptr_t)data > (UINT_MAX - sz)) {
3484 fastboot_fail("Cannot flash: image header corrupt");
3485 return;
3486 }
3487
Dima Zavin214cc642009-01-26 11:16:21 -08003488 ptable = flash_get_ptable();
3489 if (ptable == NULL) {
3490 fastboot_fail("partition table doesn't exist");
3491 return;
3492 }
3493
3494 ptn = ptable_find(ptable, arg);
3495 if (ptn == NULL) {
Tanya Brokhman84eaaf62015-02-24 21:57:06 +02003496 dprintf(INFO, "unknown partition name (%s). Trying updatevol\n",
3497 arg);
3498 cmd_updatevol(arg, data, sz);
Dima Zavin214cc642009-01-26 11:16:21 -08003499 return;
3500 }
3501
3502 if (!strcmp(ptn->name, "boot") || !strcmp(ptn->name, "recovery")) {
Kishor PK38ed93d2017-04-25 14:19:26 +05303503 if((sz > BOOT_MAGIC_SIZE) && (!memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE))) {
3504 dprintf(INFO, "Verified the BOOT_MAGIC in image header \n");
3505 } else {
3506 fastboot_fail("Image is not a boot image");
Dima Zavin214cc642009-01-26 11:16:21 -08003507 return;
3508 }
3509 }
3510
Amol Jadi5c61a952012-05-04 17:05:35 -07003511 if (!strcmp(ptn->name, "system")
Deepa Dinamani13e32c42012-03-12 14:34:17 -07003512 || !strcmp(ptn->name, "userdata")
3513 || !strcmp(ptn->name, "persist")
Sundarajan Srinivasanb063a852013-11-19 14:02:27 -08003514 || !strcmp(ptn->name, "recoveryfs")
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -08003515 || !strcmp(ptn->name, "modem"))
Tanya Brokhman1c94f1a2015-02-15 09:05:03 +02003516 extra = 1;
Kishor PK38ed93d2017-04-25 14:19:26 +05303517 else {
Vijay Kumar Pendotic6f22a72017-05-19 22:44:34 +05303518 rounded_size = ROUNDUP(sz, page_size);
3519 bytes_to_round_page = rounded_size - sz;
3520 if (bytes_to_round_page) {
3521 if (((uintptr_t)data + sz ) > (UINT_MAX - bytes_to_round_page)) {
3522 fastboot_fail("Integer overflow detected");
3523 return;
3524 }
3525 if (((uintptr_t)data + sz + bytes_to_round_page) >
3526 ((uintptr_t)target_get_scratch_address() + target_get_max_flash_size())) {
3527 fastboot_fail("Buffer size is not aligned to page_size");
3528 return;
3529 }
3530 else {
3531 memset(data + sz, 0, bytes_to_round_page);
3532 sz = rounded_size;
3533 }
Kishor PK38ed93d2017-04-25 14:19:26 +05303534 }
Gaurav Nebhwanid9730712016-05-06 14:28:29 +05303535 }
3536
Kishor PK38ed93d2017-04-25 14:19:26 +05303537 /*Checking partition_size for the possible integer overflow */
3538 partition_size = validate_partition_size(ptn);
3539
Gaurav Nebhwanid9730712016-05-06 14:28:29 +05303540 if (sz > partition_size) {
3541 fastboot_fail("Image size too large");
3542 return;
3543 }
3544
Dima Zavin214cc642009-01-26 11:16:21 -08003545 dprintf(INFO, "writing %d bytes to '%s'\n", sz, ptn->name);
Mayank Grover6cde9352017-06-06 18:45:23 +05303546 if ((sz > UBI_EC_HDR_SIZE) &&
3547 (!memcmp((void *)data, UBI_MAGIC, UBI_MAGIC_SIZE))) {
Tanya Brokhman1c94f1a2015-02-15 09:05:03 +02003548 if (flash_ubi_img(ptn, data, sz)) {
3549 fastboot_fail("flash write failure");
3550 return;
3551 }
3552 } else {
3553 if (flash_write(ptn, extra, data, sz)) {
3554 fastboot_fail("flash write failure");
3555 return;
3556 }
Dima Zavin214cc642009-01-26 11:16:21 -08003557 }
3558 dprintf(INFO, "partition '%s' updated\n", ptn->name);
3559 fastboot_okay("");
3560}
3561
Kishor PK38ed93d2017-04-25 14:19:26 +05303562
3563static inline uint64_t validate_partition_size(struct ptentry *ptn)
3564{
3565 if (ptn->length && flash_num_pages_per_blk() && page_size) {
3566 if ((ptn->length < ( UINT_MAX / flash_num_pages_per_blk())) && ((ptn->length * flash_num_pages_per_blk()) < ( UINT_MAX / page_size))) {
3567 return ptn->length * flash_num_pages_per_blk() * page_size;
3568 }
3569 }
3570 return 0;
3571}
3572
3573
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003574void cmd_flash(const char *arg, void *data, unsigned sz)
3575{
3576 if(target_is_emmc_boot())
3577 cmd_flash_mmc(arg, data, sz);
3578 else
3579 cmd_flash_nand(arg, data, sz);
3580}
3581
Dima Zavin214cc642009-01-26 11:16:21 -08003582void cmd_continue(const char *arg, void *data, unsigned sz)
3583{
3584 fastboot_okay("");
Amol Jadi7c4316c2013-10-07 14:19:26 -07003585 fastboot_stop();
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003586
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003587 if (target_is_emmc_boot())
3588 {
lijuanga40d6302015-07-20 20:10:13 +08003589#if FBCON_DISPLAY_MSG
lijuangde34d502016-02-26 16:04:50 +08003590 /* Exit keys' detection thread firstly */
3591 exit_menu_keys_detection();
lijuanga40d6302015-07-20 20:10:13 +08003592#endif
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003593 boot_linux_from_mmc();
3594 }
3595 else
3596 {
3597 boot_linux_from_flash();
3598 }
Dima Zavin214cc642009-01-26 11:16:21 -08003599}
3600
Chandan Uddaraju94183c02010-01-15 15:13:59 -08003601void cmd_reboot(const char *arg, void *data, unsigned sz)
3602{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003603 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08003604 fastboot_okay("");
3605 reboot_device(0);
3606}
3607
Mayank Grover351a75e2017-05-30 20:06:08 +05303608void cmd_set_active(const char *arg, void *data, unsigned sz)
3609{
Mayank Grover6ccc1c92017-07-04 17:36:46 +05303610 char *p, *sp = NULL;
Mayank Grover351a75e2017-05-30 20:06:08 +05303611 unsigned i,current_active_slot;
3612 const char *current_slot_suffix;
3613
3614 if (!partition_multislot_is_supported())
3615 {
3616 fastboot_fail("Command not supported");
3617 return;
3618 }
3619
3620 if (arg)
3621 {
Mayank Grover6ccc1c92017-07-04 17:36:46 +05303622 p = strtok_r((char *)arg, ":", &sp);
Mayank Grover5eb5f692017-07-19 20:16:04 +05303623 if (p)
Mayank Grover351a75e2017-05-30 20:06:08 +05303624 {
3625 current_active_slot = partition_find_active_slot();
3626
3627 /* Check if trying to make curent slot active */
3628 current_slot_suffix = SUFFIX_SLOT(current_active_slot);
Mayank Grover6ccc1c92017-07-04 17:36:46 +05303629 current_slot_suffix = strtok_r((char *)current_slot_suffix,
3630 (char *)suffix_delimiter, &sp);
3631
Mayank Grover5eb5f692017-07-19 20:16:04 +05303632 if (current_slot_suffix &&
3633 !strncmp(p, current_slot_suffix, strlen(current_slot_suffix)))
Mayank Grover351a75e2017-05-30 20:06:08 +05303634 {
3635 fastboot_okay("Slot already set active");
3636 return;
3637 }
3638 else
3639 {
3640 for (i = 0; i < AB_SUPPORTED_SLOTS; i++)
3641 {
3642 current_slot_suffix = SUFFIX_SLOT(i);
Mayank Grover6ccc1c92017-07-04 17:36:46 +05303643 current_slot_suffix = strtok_r((char *)current_slot_suffix,
3644 (char *)suffix_delimiter, &sp);
Mayank Grover5eb5f692017-07-19 20:16:04 +05303645 if (current_slot_suffix &&
3646 !strncmp(p, current_slot_suffix, strlen(current_slot_suffix)))
Mayank Grover351a75e2017-05-30 20:06:08 +05303647 {
3648 partition_switch_slots(current_active_slot, i);
3649 publish_getvar_multislot_vars();
3650 fastboot_okay("");
3651 return;
3652 }
3653 }
3654 }
3655 }
3656 }
3657 fastboot_fail("Invalid slot suffix.");
3658 return;
3659}
3660
Chandan Uddaraju94183c02010-01-15 15:13:59 -08003661void cmd_reboot_bootloader(const char *arg, void *data, unsigned sz)
3662{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003663 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08003664 fastboot_okay("");
3665 reboot_device(FASTBOOT_MODE);
3666}
3667
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003668void cmd_oem_enable_charger_screen(const char *arg, void *data, unsigned size)
3669{
3670 dprintf(INFO, "Enabling charger screen check\n");
3671 device.charger_screen_enabled = 1;
3672 write_device_info(&device);
3673 fastboot_okay("");
3674}
3675
3676void cmd_oem_disable_charger_screen(const char *arg, void *data, unsigned size)
3677{
3678 dprintf(INFO, "Disabling charger screen check\n");
3679 device.charger_screen_enabled = 0;
3680 write_device_info(&device);
3681 fastboot_okay("");
3682}
3683
lijuanga25d8bb2015-09-16 13:11:52 +08003684void cmd_oem_off_mode_charger(const char *arg, void *data, unsigned size)
3685{
3686 char *p = NULL;
3687 const char *delim = " \t\n\r";
Parth Dixit407f15b2016-01-07 14:47:37 +05303688 char *sp;
lijuanga25d8bb2015-09-16 13:11:52 +08003689
3690 if (arg) {
Parth Dixit407f15b2016-01-07 14:47:37 +05303691 p = strtok_r((char *)arg, delim, &sp);
lijuanga25d8bb2015-09-16 13:11:52 +08003692 if (p) {
3693 if (!strncmp(p, "0", 1)) {
3694 device.charger_screen_enabled = 0;
3695 } else if (!strncmp(p, "1", 1)) {
3696 device.charger_screen_enabled = 1;
3697 }
3698 }
3699 }
3700
3701 /* update charger_screen_enabled value for getvar
3702 * command
3703 */
3704 snprintf(charger_screen_enabled, MAX_RSP_SIZE, "%d",
3705 device.charger_screen_enabled);
3706
3707 write_device_info(&device);
3708 fastboot_okay("");
3709}
3710
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05303711void cmd_oem_select_display_panel(const char *arg, void *data, unsigned size)
3712{
3713 dprintf(INFO, "Selecting display panel %s\n", arg);
3714 if (arg)
3715 strlcpy(device.display_panel, arg,
3716 sizeof(device.display_panel));
3717 write_device_info(&device);
3718 fastboot_okay("");
3719}
3720
Shashank Mittal162244e2011-08-08 19:01:25 -07003721void cmd_oem_unlock(const char *arg, void *data, unsigned sz)
3722{
lijuang4ece1e72015-08-14 21:02:36 +08003723 set_device_unlock(UNLOCK, TRUE);
vijay kumarc65876c2015-04-24 13:29:16 +05303724}
3725
3726void cmd_oem_unlock_go(const char *arg, void *data, unsigned sz)
3727{
lijuang4ece1e72015-08-14 21:02:36 +08003728 if(!device.is_unlocked) {
vijay kumarc65876c2015-04-24 13:29:16 +05303729 if(!is_allow_unlock) {
3730 fastboot_fail("oem unlock is not allowed");
3731 return;
3732 }
3733
lijuang4ece1e72015-08-14 21:02:36 +08003734 set_device_unlock_value(UNLOCK, TRUE);
vijay kumarc65876c2015-04-24 13:29:16 +05303735
lijuang4ece1e72015-08-14 21:02:36 +08003736 /* wipe data */
vijay kumarc65876c2015-04-24 13:29:16 +05303737 struct recovery_message msg;
Kishor PK60a68212017-05-08 16:55:57 +05303738 memset(&msg, 0, sizeof(msg));
vijay kumarc65876c2015-04-24 13:29:16 +05303739 snprintf(msg.recovery, sizeof(msg.recovery), "recovery\n--wipe_data");
3740 write_misc(0, &msg, sizeof(msg));
3741
3742 fastboot_okay("");
3743 reboot_device(RECOVERY_MODE);
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003744 }
3745 fastboot_okay("");
3746}
3747
Channagoud Kadabiad259832015-05-29 11:14:17 -07003748static int aboot_frp_unlock(char *pname, void *data, unsigned sz)
3749{
Mayank Grover70f8b0e2017-05-17 11:49:00 +05303750 int ret=1;
3751 bool authentication_success=false;
Channagoud Kadabiad259832015-05-29 11:14:17 -07003752
Mayank Grover70f8b0e2017-05-17 11:49:00 +05303753 /*
3754 Authentication method not implemented.
3755
3756 OEM to implement, authentication system which on successful validataion,
3757 calls write_allow_oem_unlock() with is_allow_unlock.
3758 */
3759#if 0
3760 authentication_success = oem_specific_auth_mthd();
3761#endif
3762
3763 if (authentication_success)
Channagoud Kadabiad259832015-05-29 11:14:17 -07003764 {
Mayank Grover70f8b0e2017-05-17 11:49:00 +05303765 is_allow_unlock = true;
3766 write_allow_oem_unlock(is_allow_unlock);
3767 ret = 0;
Channagoud Kadabiad259832015-05-29 11:14:17 -07003768 }
3769 return ret;
3770}
3771
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003772void cmd_oem_lock(const char *arg, void *data, unsigned sz)
3773{
lijuang4ece1e72015-08-14 21:02:36 +08003774 set_device_unlock(UNLOCK, FALSE);
Shashank Mittal162244e2011-08-08 19:01:25 -07003775}
3776
Shashank Mittala0032282011-08-26 14:50:11 -07003777void cmd_oem_devinfo(const char *arg, void *data, unsigned sz)
3778{
lijuang511a2b52015-08-14 20:50:51 +08003779 char response[MAX_RSP_SIZE];
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003780 snprintf(response, sizeof(response), "\tDevice tampered: %s", (device.is_tampered ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07003781 fastboot_info(response);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003782 snprintf(response, sizeof(response), "\tDevice unlocked: %s", (device.is_unlocked ? "true" : "false"));
3783 fastboot_info(response);
Mayank Grover889be1b2017-09-12 20:12:23 +05303784#if VERIFIED_BOOT
3785 if (VB_V2 == target_get_vb_version())
3786 {
3787 snprintf(response, sizeof(response), "\tDevice critical unlocked: %s",
3788 (device.is_unlock_critical ? "true" : "false"));
3789 fastboot_info(response);
3790 }
Parth Dixitddbc7352015-10-18 03:13:31 +05303791#endif
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003792 snprintf(response, sizeof(response), "\tCharger screen enabled: %s", (device.charger_screen_enabled ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07003793 fastboot_info(response);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05303794 snprintf(response, sizeof(response), "\tDisplay panel: %s", (device.display_panel));
3795 fastboot_info(response);
Shashank Mittala0032282011-08-26 14:50:11 -07003796 fastboot_okay("");
3797}
3798
lijuang511a2b52015-08-14 20:50:51 +08003799void cmd_flashing_get_unlock_ability(const char *arg, void *data, unsigned sz)
3800{
3801 char response[MAX_RSP_SIZE];
3802 snprintf(response, sizeof(response), "\tget_unlock_ability: %d", is_allow_unlock);
3803 fastboot_info(response);
3804 fastboot_okay("");
3805}
3806
3807void cmd_flashing_lock_critical(const char *arg, void *data, unsigned sz)
3808{
lijuang4ece1e72015-08-14 21:02:36 +08003809 set_device_unlock(UNLOCK_CRITICAL, FALSE);
lijuang511a2b52015-08-14 20:50:51 +08003810}
3811
3812void cmd_flashing_unlock_critical(const char *arg, void *data, unsigned sz)
3813{
lijuang4ece1e72015-08-14 21:02:36 +08003814 set_device_unlock(UNLOCK_CRITICAL, TRUE);
lijuang511a2b52015-08-14 20:50:51 +08003815}
3816
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07003817void cmd_preflash(const char *arg, void *data, unsigned sz)
3818{
3819 fastboot_okay("");
3820}
3821
Mao Flynn7b379f32015-04-20 00:28:30 +08003822static uint8_t logo_header[LOGO_IMG_HEADER_SIZE];
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303823
Mao Flynn7b379f32015-04-20 00:28:30 +08003824int splash_screen_check_header(logo_img_header *header)
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303825{
Mao Flynn7b379f32015-04-20 00:28:30 +08003826 if (memcmp(header->magic, LOGO_IMG_MAGIC, 8))
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303827 return -1;
Mao Flynn7b379f32015-04-20 00:28:30 +08003828 if (header->width == 0 || header->height == 0)
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303829 return -1;
3830 return 0;
3831}
3832
Mao Flynn7b379f32015-04-20 00:28:30 +08003833int splash_screen_flash()
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003834{
3835 struct ptentry *ptn;
3836 struct ptable *ptable;
Mao Flynn7b379f32015-04-20 00:28:30 +08003837 struct logo_img_header *header;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003838 struct fbcon_config *fb_display = NULL;
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08003839
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303840 ptable = flash_get_ptable();
3841 if (ptable == NULL) {
Mao Flynn7b379f32015-04-20 00:28:30 +08003842 dprintf(CRITICAL, "ERROR: Partition table not found\n");
3843 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303844 }
Mao Flynn7b379f32015-04-20 00:28:30 +08003845
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303846 ptn = ptable_find(ptable, "splash");
3847 if (ptn == NULL) {
3848 dprintf(CRITICAL, "ERROR: splash Partition not found\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003849 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303850 }
Mao Flynn7b379f32015-04-20 00:28:30 +08003851 if (flash_read(ptn, 0, (void *)logo_header, LOGO_IMG_HEADER_SIZE)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303852 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003853 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303854 }
3855
Mao Flynn7b379f32015-04-20 00:28:30 +08003856 header = (struct logo_img_header *)logo_header;
3857 if (splash_screen_check_header(header)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303858 dprintf(CRITICAL, "ERROR: Boot image header invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003859 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303860 }
3861
3862 fb_display = fbcon_display();
3863 if (fb_display) {
Mao Flynn7b379f32015-04-20 00:28:30 +08003864 if (header->type && (header->blocks != 0)) { // RLE24 compressed data
3865 uint8_t *base = (uint8_t *) fb_display->base + LOGO_IMG_OFFSET;
3866
3867 /* if the logo is full-screen size, remove "fbcon_clear()" */
3868 if ((header->width != fb_display->width)
3869 || (header->height != fb_display->height))
3870 fbcon_clear();
3871
3872 if (flash_read(ptn + LOGO_IMG_HEADER_SIZE, 0,
3873 (uint32_t *)base,
3874 (header->blocks * 512))) {
3875 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
3876 return -1;
3877 }
3878 fbcon_extract_to_screen(header, base);
3879 return 0;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003880 }
Mao Flynn7b379f32015-04-20 00:28:30 +08003881
Veera Sundaram Sankaranc33416f2015-09-28 10:25:55 -07003882 if ((header->width > fb_display->width) || (header->height > fb_display->height)) {
3883 dprintf(CRITICAL, "Logo config greater than fb config. Fall back default logo\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003884 return -1;
3885 }
3886
Vineet Bajaj99291ed2014-09-09 12:29:46 +05303887 uint8_t *base = (uint8_t *) fb_display->base;
Sachin Bhayare619e9e42017-05-15 13:10:31 +05303888 uint32_t fb_size = ROUNDUP(fb_display->width *
3889 fb_display->height *
3890 (fb_display->bpp / 8), 4096);
3891 uint32_t splash_size = ((((header->width * header->height *
3892 fb_display->bpp/8) + 511) >> 9) << 9);
3893
3894 if (splash_size > fb_size) {
3895 dprintf(CRITICAL, "ERROR: Splash image size invalid\n");
3896 return -1;
3897 }
3898
Mao Flynn7b379f32015-04-20 00:28:30 +08003899 if (flash_read(ptn + LOGO_IMG_HEADER_SIZE, 0,
3900 (uint32_t *)base,
3901 ((((header->width * header->height * fb_display->bpp/8) + 511) >> 9) << 9))) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303902 fbcon_clear();
Vineet Bajaj99291ed2014-09-09 12:29:46 +05303903 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003904 return -1;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003905 }
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303906 }
3907
Mao Flynn7b379f32015-04-20 00:28:30 +08003908 return 0;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303909}
3910
Mao Flynn7b379f32015-04-20 00:28:30 +08003911int splash_screen_mmc()
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303912{
3913 int index = INVALID_PTN;
3914 unsigned long long ptn = 0;
3915 struct fbcon_config *fb_display = NULL;
Mao Flynn7b379f32015-04-20 00:28:30 +08003916 struct logo_img_header *header;
Mao Flynn1893a7f2015-06-03 12:03:36 +08003917 uint32_t blocksize, realsize, readsize;
3918 uint8_t *base;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303919
3920 index = partition_get_index("splash");
3921 if (index == 0) {
3922 dprintf(CRITICAL, "ERROR: splash Partition table not found\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003923 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303924 }
3925
3926 ptn = partition_get_offset(index);
3927 if (ptn == 0) {
3928 dprintf(CRITICAL, "ERROR: splash Partition invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003929 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303930 }
3931
Mao Flynn1893a7f2015-06-03 12:03:36 +08003932 mmc_set_lun(partition_get_lun(index));
3933
3934 blocksize = mmc_get_device_blocksize();
3935 if (blocksize == 0) {
3936 dprintf(CRITICAL, "ERROR:splash Partition invalid blocksize\n");
3937 return -1;
3938 }
3939
3940 fb_display = fbcon_display();
Channagoud Kadabidaf10a62015-07-22 11:51:55 -07003941 if (!fb_display)
3942 {
3943 dprintf(CRITICAL, "ERROR: fb config is not allocated\n");
3944 return -1;
3945 }
3946
Mao Flynn1893a7f2015-06-03 12:03:36 +08003947 base = (uint8_t *) fb_display->base;
3948
3949 if (mmc_read(ptn, (uint32_t *)(base + LOGO_IMG_OFFSET), blocksize)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303950 dprintf(CRITICAL, "ERROR: Cannot read splash image header\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003951 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303952 }
3953
Mao Flynn1893a7f2015-06-03 12:03:36 +08003954 header = (struct logo_img_header *)(base + LOGO_IMG_OFFSET);
Mao Flynn7b379f32015-04-20 00:28:30 +08003955 if (splash_screen_check_header(header)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303956 dprintf(CRITICAL, "ERROR: Splash image header invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003957 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303958 }
3959
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303960 if (fb_display) {
Mao Flynn1893a7f2015-06-03 12:03:36 +08003961 if (header->type && (header->blocks != 0)) { /* 1 RLE24 compressed data */
3962 base += LOGO_IMG_OFFSET;
Mao Flynn7b379f32015-04-20 00:28:30 +08003963
Mao Flynn1893a7f2015-06-03 12:03:36 +08003964 realsize = header->blocks * 512;
3965 readsize = ROUNDUP((realsize + LOGO_IMG_HEADER_SIZE), blocksize) - blocksize;
3966
3967 /* if the logo is not full-screen size, clean screen */
Mao Flynn7b379f32015-04-20 00:28:30 +08003968 if ((header->width != fb_display->width)
3969 || (header->height != fb_display->height))
3970 fbcon_clear();
3971
Sachin Bhayare619e9e42017-05-15 13:10:31 +05303972 uint32_t fb_size = ROUNDUP(fb_display->width *
3973 fb_display->height *
3974 (fb_display->bpp / 8), 4096);
3975
3976 if (readsize > fb_size) {
3977 dprintf(CRITICAL, "ERROR: Splash image size invalid\n");
3978 return -1;
3979 }
3980
Mao Flynn1893a7f2015-06-03 12:03:36 +08003981 if (mmc_read(ptn + blocksize, (uint32_t *)(base + blocksize), readsize)) {
Mao Flynn7b379f32015-04-20 00:28:30 +08003982 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
3983 return -1;
3984 }
Mao Flynn7b379f32015-04-20 00:28:30 +08003985
Mao Flynn1893a7f2015-06-03 12:03:36 +08003986 fbcon_extract_to_screen(header, (base + LOGO_IMG_HEADER_SIZE));
3987 } else { /* 2 Raw BGR data */
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303988
Veera Sundaram Sankaranc33416f2015-09-28 10:25:55 -07003989 if ((header->width > fb_display->width) || (header->height > fb_display->height)) {
3990 dprintf(CRITICAL, "Logo config greater than fb config. Fall back default logo\n");
Mao Flynn1893a7f2015-06-03 12:03:36 +08003991 return -1;
3992 }
3993
3994 realsize = header->width * header->height * fb_display->bpp / 8;
3995 readsize = ROUNDUP((realsize + LOGO_IMG_HEADER_SIZE), blocksize) - blocksize;
3996
3997 if (blocksize == LOGO_IMG_HEADER_SIZE) { /* read the content directly */
3998 if (mmc_read((ptn + LOGO_IMG_HEADER_SIZE), (uint32_t *)base, readsize)) {
3999 fbcon_clear();
4000 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
4001 return -1;
4002 }
4003 } else {
4004 if (mmc_read(ptn + blocksize ,
4005 (uint32_t *)(base + LOGO_IMG_OFFSET + blocksize), readsize)) {
4006 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
4007 return -1;
4008 }
4009 memmove(base, (base + LOGO_IMG_OFFSET + LOGO_IMG_HEADER_SIZE), realsize);
4010 }
4011 }
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304012 }
4013
Mao Flynn7b379f32015-04-20 00:28:30 +08004014 return 0;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304015}
4016
Mao Flynn7b379f32015-04-20 00:28:30 +08004017int fetch_image_from_partition()
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05304018{
4019 if (target_is_emmc_boot()) {
4020 return splash_screen_mmc();
4021 } else {
4022 return splash_screen_flash();
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07004023 }
4024}
4025
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07004026/* Get the size from partiton name */
4027static void get_partition_size(const char *arg, char *response)
4028{
4029 uint64_t ptn = 0;
4030 uint64_t size;
4031 int index = INVALID_PTN;
4032
4033 index = partition_get_index(arg);
4034
4035 if (index == INVALID_PTN)
4036 {
4037 dprintf(CRITICAL, "Invalid partition index\n");
4038 return;
4039 }
4040
4041 ptn = partition_get_offset(index);
4042
4043 if(!ptn)
4044 {
4045 dprintf(CRITICAL, "Invalid partition name %s\n", arg);
4046 return;
4047 }
4048
4049 size = partition_get_size(index);
4050
4051 snprintf(response, MAX_RSP_SIZE, "\t 0x%llx", size);
4052 return;
4053}
4054
4055/*
4056 * Publish the partition type & size info
4057 * fastboot getvar will publish the required information.
4058 * fastboot getvar partition_size:<partition_name>: partition size in hex
4059 * fastboot getvar partition_type:<partition_name>: partition type (ext/fat)
4060 */
4061static void publish_getvar_partition_info(struct getvar_partition_info *info, uint8_t num_parts)
4062{
4063 uint8_t i;
4064
4065 for (i = 0; i < num_parts; i++) {
4066 get_partition_size(info[i].part_name, info[i].size_response);
4067
4068 if (strlcat(info[i].getvar_size, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
4069 {
4070 dprintf(CRITICAL, "partition size name truncated\n");
4071 return;
4072 }
4073 if (strlcat(info[i].getvar_type, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
4074 {
4075 dprintf(CRITICAL, "partition type name truncated\n");
4076 return;
4077 }
4078
4079 /* publish partition size & type info */
4080 fastboot_publish((const char *) info[i].getvar_size, (const char *) info[i].size_response);
4081 fastboot_publish((const char *) info[i].getvar_type, (const char *) info[i].type_response);
4082 }
4083}
4084
Mayank Grover351a75e2017-05-30 20:06:08 +05304085void publish_getvar_multislot_vars()
4086{
4087 int i,count;
4088 static bool published = false;
4089 static char slot_count[MAX_RSP_SIZE];
4090 static struct ab_slot_info slot_info[AB_SUPPORTED_SLOTS];
4091 static char active_slot_suffix[MAX_RSP_SIZE];
4092 static char has_slot_pname[NUM_PARTITIONS][MAX_GET_VAR_NAME_SIZE];
4093 static char has_slot_reply[NUM_PARTITIONS][MAX_RSP_SIZE];
4094 const char *tmp;
4095 char tmpbuff[MAX_GET_VAR_NAME_SIZE];
Mayank Grover2bd84bb2017-06-20 11:06:07 +05304096 signed active_slt;
Mayank Grover351a75e2017-05-30 20:06:08 +05304097
4098 if (!published)
4099 {
4100 /* Update slot meta info */
4101 count = partition_fill_partition_meta(has_slot_pname, has_slot_reply,
4102 partition_get_partition_count());
4103 for(i=0; i<count; i++)
4104 {
4105 memset(tmpbuff, 0, MAX_GET_VAR_NAME_SIZE);
Mayank Grover2bd84bb2017-06-20 11:06:07 +05304106 snprintf(tmpbuff, MAX_GET_VAR_NAME_SIZE,"has-slot:%s",
4107 has_slot_pname[i]);
4108 strlcpy(has_slot_pname[i], tmpbuff, MAX_GET_VAR_NAME_SIZE);
Mayank Grover351a75e2017-05-30 20:06:08 +05304109 fastboot_publish(has_slot_pname[i], has_slot_reply[i]);
4110 }
4111
4112 for (i=0; i<AB_SUPPORTED_SLOTS; i++)
4113 {
4114 tmp = SUFFIX_SLOT(i);
Mayank Grover2bd84bb2017-06-20 11:06:07 +05304115 snprintf(slot_info[i].slot_is_unbootable, sizeof(slot_info[i].slot_is_unbootable),
4116 "slot-unbootable:%s", tmp);
4117 snprintf(slot_info[i].slot_is_active, sizeof(slot_info[i].slot_is_active),
4118 "slot-active:%s", tmp);
4119 snprintf(slot_info[i].slot_is_succesful, sizeof(slot_info[i].slot_is_succesful),
4120 "slot-success:%s", tmp);
4121 snprintf(slot_info[i].slot_retry_count, sizeof(slot_info[i].slot_retry_count),
4122 "slot-retry-count:%s", tmp);
Mayank Grover351a75e2017-05-30 20:06:08 +05304123 fastboot_publish(slot_info[i].slot_is_unbootable,
4124 slot_info[i].slot_is_unbootable_rsp);
4125 fastboot_publish(slot_info[i].slot_is_active,
4126 slot_info[i].slot_is_active_rsp);
4127 fastboot_publish(slot_info[i].slot_is_succesful,
4128 slot_info[i].slot_is_succesful_rsp);
4129 fastboot_publish(slot_info[i].slot_retry_count,
4130 slot_info[i].slot_retry_count_rsp);
4131 }
4132 fastboot_publish("current-slot", active_slot_suffix);
4133 snprintf(slot_count, sizeof(slot_count),"%d", AB_SUPPORTED_SLOTS);
4134 fastboot_publish("slot-count", slot_count);
4135 published = true;
4136 }
4137
Mayank Grover2bd84bb2017-06-20 11:06:07 +05304138 active_slt = partition_find_active_slot();
4139 if (active_slt != INVALID)
4140 snprintf(active_slot_suffix, sizeof(active_slot_suffix), "%s",
4141 SUFFIX_SLOT(active_slt));
4142 else
4143 strlcpy(active_slot_suffix, "INVALID", sizeof(active_slot_suffix));
4144
Mayank Grover351a75e2017-05-30 20:06:08 +05304145 /* Update partition meta information */
4146 partition_fill_slot_meta(slot_info);
4147 return;
4148}
4149
lijuang4ece1e72015-08-14 21:02:36 +08004150void get_product_name(unsigned char *buf)
4151{
4152 snprintf((char*)buf, MAX_RSP_SIZE, "%s", TARGET(BOARD));
4153 return;
4154}
4155
4156void get_bootloader_version(unsigned char *buf)
4157{
4158 snprintf((char*)buf, MAX_RSP_SIZE, "%s", device.bootloader_version);
4159 return;
4160}
4161
4162void get_baseband_version(unsigned char *buf)
4163{
4164 snprintf((char*)buf, MAX_RSP_SIZE, "%s", device.radio_version);
4165 return;
4166}
4167
4168bool is_device_locked()
4169{
4170 return device.is_unlocked ? false:true;
4171}
4172
Amol Jadi5edf3552013-07-23 14:15:34 -07004173/* register commands and variables for fastboot */
4174void aboot_fastboot_register_commands(void)
4175{
Sridhar Parasurame94e8152014-10-24 14:06:03 -07004176 int i;
lijuangf16461c2015-08-03 17:09:34 +08004177 char hw_platform_buf[MAX_RSP_SIZE];
Amol Jadi5edf3552013-07-23 14:15:34 -07004178
Sridhar Parasurame94e8152014-10-24 14:06:03 -07004179 struct fastboot_cmd_desc cmd_list[] = {
lijuang511a2b52015-08-14 20:50:51 +08004180 /* By default the enabled list is empty. */
4181 {"", NULL},
4182 /* move commands enclosed within the below ifndef to here
4183 * if they need to be enabled in user build.
4184 */
Sridhar Parasurame94e8152014-10-24 14:06:03 -07004185#ifndef DISABLE_FASTBOOT_CMDS
lijuang511a2b52015-08-14 20:50:51 +08004186 /* Register the following commands only for non-user builds */
4187 {"flash:", cmd_flash},
4188 {"erase:", cmd_erase},
4189 {"boot", cmd_boot},
4190 {"continue", cmd_continue},
4191 {"reboot", cmd_reboot},
4192 {"reboot-bootloader", cmd_reboot_bootloader},
4193 {"oem unlock", cmd_oem_unlock},
4194 {"oem unlock-go", cmd_oem_unlock_go},
4195 {"oem lock", cmd_oem_lock},
4196 {"flashing unlock", cmd_oem_unlock},
4197 {"flashing lock", cmd_oem_lock},
4198 {"flashing lock_critical", cmd_flashing_lock_critical},
4199 {"flashing unlock_critical", cmd_flashing_unlock_critical},
4200 {"flashing get_unlock_ability", cmd_flashing_get_unlock_ability},
4201 {"oem device-info", cmd_oem_devinfo},
4202 {"preflash", cmd_preflash},
4203 {"oem enable-charger-screen", cmd_oem_enable_charger_screen},
4204 {"oem disable-charger-screen", cmd_oem_disable_charger_screen},
lijuanga25d8bb2015-09-16 13:11:52 +08004205 {"oem off-mode-charge", cmd_oem_off_mode_charger},
lijuang511a2b52015-08-14 20:50:51 +08004206 {"oem select-display-panel", cmd_oem_select_display_panel},
Mayank Grover6ccc1c92017-07-04 17:36:46 +05304207 {"set_active",cmd_set_active},
Channagoud Kadabi736c4962015-08-21 11:56:52 -07004208#if UNITTEST_FW_SUPPORT
Channagoud Kadabid5ddf482015-09-28 10:31:35 -07004209 {"oem run-tests", cmd_oem_runtests},
Channagoud Kadabi736c4962015-08-21 11:56:52 -07004210#endif
Sridhar Parasurame94e8152014-10-24 14:06:03 -07004211#endif
lijuang511a2b52015-08-14 20:50:51 +08004212 };
Sridhar Parasurame94e8152014-10-24 14:06:03 -07004213
4214 int fastboot_cmds_count = sizeof(cmd_list)/sizeof(cmd_list[0]);
4215 for (i = 1; i < fastboot_cmds_count; i++)
4216 fastboot_register(cmd_list[i].name,cmd_list[i].cb);
4217
Amol Jadi5edf3552013-07-23 14:15:34 -07004218 /* publish variables and their values */
4219 fastboot_publish("product", TARGET(BOARD));
4220 fastboot_publish("kernel", "lk");
4221 fastboot_publish("serialno", sn_buf);
4222
4223 /*
4224 * partition info is supported only for emmc partitions
4225 * Calling this for NAND prints some error messages which
4226 * is harmless but misleading. Avoid calling this for NAND
4227 * devices.
4228 */
4229 if (target_is_emmc_boot())
4230 publish_getvar_partition_info(part_info, ARRAY_SIZE(part_info));
4231
Mayank Grover351a75e2017-05-30 20:06:08 +05304232 if (partition_multislot_is_supported())
4233 publish_getvar_multislot_vars();
4234
Amol Jadi5edf3552013-07-23 14:15:34 -07004235 /* Max download size supported */
Ameya Thakur11cf1a62013-08-05 12:44:48 -07004236 snprintf(max_download_size, MAX_RSP_SIZE, "\t0x%x",
4237 target_get_max_flash_size());
Amol Jadi5edf3552013-07-23 14:15:34 -07004238 fastboot_publish("max-download-size", (const char *) max_download_size);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07004239 /* Is the charger screen check enabled */
4240 snprintf(charger_screen_enabled, MAX_RSP_SIZE, "%d",
4241 device.charger_screen_enabled);
4242 fastboot_publish("charger-screen-enabled",
4243 (const char *) charger_screen_enabled);
lijuanga25d8bb2015-09-16 13:11:52 +08004244 fastboot_publish("off-mode-charge", (const char *) charger_screen_enabled);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05304245 snprintf(panel_display_mode, MAX_RSP_SIZE, "%s",
4246 device.display_panel);
4247 fastboot_publish("display-panel",
4248 (const char *) panel_display_mode);
Channagoud Kadabiaafbbfc2015-03-04 17:52:56 -08004249 fastboot_publish("version-bootloader", (const char *) device.bootloader_version);
4250 fastboot_publish("version-baseband", (const char *) device.radio_version);
lijuangf16461c2015-08-03 17:09:34 +08004251 fastboot_publish("secure", is_secure_boot_enable()? "yes":"no");
4252 smem_get_hw_platform_name((unsigned char *) hw_platform_buf, sizeof(hw_platform_buf));
4253 snprintf(get_variant, MAX_RSP_SIZE, "%s %s", hw_platform_buf,
4254 target_is_emmc_boot()? "eMMC":"UFS");
4255 fastboot_publish("variant", (const char *) get_variant);
lijuang65c5a822015-08-29 16:35:36 +08004256#if CHECK_BAT_VOLTAGE
lijuang102dfa92015-10-09 18:31:03 +08004257 update_battery_status();
lijuang65c5a822015-08-29 16:35:36 +08004258 fastboot_publish("battery-voltage", (const char *) battery_voltage);
lijuang102dfa92015-10-09 18:31:03 +08004259 fastboot_publish("battery-soc-ok", (const char *) battery_soc_ok);
lijuang65c5a822015-08-29 16:35:36 +08004260#endif
Amol Jadi5edf3552013-07-23 14:15:34 -07004261}
4262
Brian Swetland9c4c0752009-01-25 16:23:50 -08004263void aboot_init(const struct app_descriptor *app)
4264{
Shashank Mittal4f99a882010-02-01 13:58:50 -08004265 unsigned reboot_mode = 0;
Mayank Grover351a75e2017-05-30 20:06:08 +05304266 int boot_err_type = 0;
4267 int boot_slot = INVALID;
Chandan Uddarajubedca152010-06-02 23:05:15 -07004268
lijuang39831732016-01-08 17:49:02 +08004269 /* Initialise wdog to catch early lk crashes */
4270#if WDOG_SUPPORT
4271 msm_wdog_init();
4272#endif
4273
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07004274 /* Setup page size information for nv storage */
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004275 if (target_is_emmc_boot())
4276 {
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07004277 page_size = mmc_page_size();
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004278 page_mask = page_size - 1;
Gaurav Nebhwani4d2389c2016-03-17 21:10:05 +05304279 mmc_blocksize = mmc_get_device_blocksize();
4280 mmc_blocksize_mask = mmc_blocksize - 1;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004281 }
4282 else
4283 {
4284 page_size = flash_page_size();
4285 page_mask = page_size - 1;
4286 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07004287 ASSERT((MEMBASE + MEMSIZE) > MEMBASE);
4288
Ameya Thakur11cf1a62013-08-05 12:44:48 -07004289 read_device_info(&device);
vijay kumarc65876c2015-04-24 13:29:16 +05304290 read_allow_oem_unlock(&device);
Shashank Mittal162244e2011-08-08 19:01:25 -07004291
Mayank Grover351a75e2017-05-30 20:06:08 +05304292 /* Detect multi-slot support */
4293 if (partition_multislot_is_supported())
4294 {
4295 boot_slot = partition_find_active_slot();
4296 if (boot_slot == INVALID)
4297 {
4298 boot_into_fastboot = true;
4299 dprintf(INFO, "Active Slot: (INVALID)\n");
4300 }
4301 else
4302 {
4303 /* Setting the state of system to boot active slot */
4304 partition_mark_active_slot(boot_slot);
4305 dprintf(INFO, "Active Slot: (%s)\n", SUFFIX_SLOT(boot_slot));
4306 }
4307 }
4308
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08004309 /* Display splash screen if enabled */
4310#if DISPLAY_SPLASH_SCREEN
lijuang99c02d82015-02-13 19:04:34 +08004311#if NO_ALARM_DISPLAY
4312 if (!check_alarm_boot()) {
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08004313#endif
lijuang99c02d82015-02-13 19:04:34 +08004314 dprintf(SPEW, "Display Init: Start\n");
Ajay Singh Parmara7865a82015-04-16 21:27:52 -07004315#if DISPLAY_HDMI_PRIMARY
4316 if (!strlen(device.display_panel))
4317 strlcpy(device.display_panel, DISPLAY_PANEL_HDMI,
4318 sizeof(device.display_panel));
4319#endif
Channagoud Kadabi90869ce2015-04-27 11:15:14 -07004320#if ENABLE_WBC
Channagoud Kadabid801ac72015-08-26 11:21:51 -07004321 /* Wait if the display shutdown is in progress */
4322 while(pm_app_display_shutdown_in_prgs());
Channagoud Kadabi90869ce2015-04-27 11:15:14 -07004323 if (!pm_appsbl_display_init_done())
4324 target_display_init(device.display_panel);
4325 else
4326 display_image_on_screen();
4327#else
lijuang99c02d82015-02-13 19:04:34 +08004328 target_display_init(device.display_panel);
Channagoud Kadabi90869ce2015-04-27 11:15:14 -07004329#endif
lijuang99c02d82015-02-13 19:04:34 +08004330 dprintf(SPEW, "Display Init: Done\n");
4331#if NO_ALARM_DISPLAY
4332 }
4333#endif
4334#endif
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08004335
Greg Griscod6250552011-06-29 14:40:23 -07004336 target_serialno((unsigned char *) sn_buf);
Ajay Dudanib06c05f2011-05-12 14:46:10 -07004337 dprintf(SPEW,"serial number: %s\n",sn_buf);
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -08004338
Dhaval Patel223ec952013-07-18 14:49:44 -07004339 memset(display_panel_buf, '\0', MAX_PANEL_BUF_SIZE);
4340
Matthew Qindefd5562014-07-11 18:02:40 +08004341 /*
4342 * Check power off reason if user force reset,
4343 * if yes phone will do normal boot.
4344 */
4345 if (is_user_force_reset())
4346 goto normal_boot;
4347
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07004348 /* Check if we should do something other than booting up */
Ameya Thakur0b9c2442013-05-10 13:22:56 -07004349 if (keys_get_state(KEY_VOLUMEUP) && keys_get_state(KEY_VOLUMEDOWN))
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07004350 {
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03004351 dprintf(ALWAYS,"dload mode key sequence detected\n");
lijuang395b5e62015-11-19 17:39:44 +08004352 reboot_device(EMERGENCY_DLOAD);
4353 dprintf(CRITICAL,"Failed to reboot into dload mode\n");
4354
Ameya Thakur0b9c2442013-05-10 13:22:56 -07004355 boot_into_fastboot = true;
4356 }
4357 if (!boot_into_fastboot)
4358 {
4359 if (keys_get_state(KEY_HOME) || keys_get_state(KEY_VOLUMEUP))
4360 boot_into_recovery = 1;
4361 if (!boot_into_recovery &&
4362 (keys_get_state(KEY_BACK) || keys_get_state(KEY_VOLUMEDOWN)))
Pavel Nedev5d91d412013-04-29 11:34:24 +03004363 boot_into_fastboot = true;
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07004364 }
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004365 #if NO_KEYPAD_DRIVER
Kinson Chik0b1c8162011-08-31 16:31:57 -07004366 if (fastboot_trigger())
Pavel Nedev5d91d412013-04-29 11:34:24 +03004367 boot_into_fastboot = true;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004368 #endif
Chandan Uddarajubedca152010-06-02 23:05:15 -07004369
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07004370#if USE_PON_REBOOT_REG
4371 reboot_mode = check_hard_reboot_mode();
4372#else
Ajay Dudani77421292010-10-27 19:34:06 -07004373 reboot_mode = check_reboot_mode();
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07004374#endif
4375 if (reboot_mode == RECOVERY_MODE)
4376 {
Ajay Dudani77421292010-10-27 19:34:06 -07004377 boot_into_recovery = 1;
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07004378 }
4379 else if(reboot_mode == FASTBOOT_MODE)
4380 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03004381 boot_into_fastboot = true;
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07004382 }
4383 else if(reboot_mode == ALARM_BOOT)
4384 {
Matthew Qind886f3c2014-01-17 16:52:01 +08004385 boot_reason_alarm = true;
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07004386 }
Parth Dixit207573a2015-10-27 17:26:21 +05304387#if VERIFIED_BOOT
Mayank Grover889be1b2017-09-12 20:12:23 +05304388 else if (VB_V2 == target_get_vb_version())
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07004389 {
Mayank Grover889be1b2017-09-12 20:12:23 +05304390 if (reboot_mode == DM_VERITY_ENFORCING)
4391 {
4392 device.verity_mode = 1;
4393 write_device_info(&device);
4394 }
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07004395#if ENABLE_VB_ATTEST
Mayank Grover889be1b2017-09-12 20:12:23 +05304396 else if (reboot_mode == DM_VERITY_EIO)
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07004397#else
Mayank Grover889be1b2017-09-12 20:12:23 +05304398 else if (reboot_mode == DM_VERITY_LOGGING)
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07004399#endif
Mayank Grover889be1b2017-09-12 20:12:23 +05304400 {
4401 device.verity_mode = 0;
4402 write_device_info(&device);
4403 }
4404 else if (reboot_mode == DM_VERITY_KEYSCLEAR)
4405 {
4406 if(send_delete_keys_to_tz())
4407 ASSERT(0);
4408 }
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07004409 }
Parth Dixitddbc7352015-10-18 03:13:31 +05304410#endif
Ajay Dudani77421292010-10-27 19:34:06 -07004411
Matthew Qindefd5562014-07-11 18:02:40 +08004412normal_boot:
Pavel Nedev5d91d412013-04-29 11:34:24 +03004413 if (!boot_into_fastboot)
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004414 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03004415 if (target_is_emmc_boot())
Shashank Mittala0032282011-08-26 14:50:11 -07004416 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03004417 if(emmc_recovery_init())
4418 dprintf(ALWAYS,"error in emmc_recovery_init\n");
4419 if(target_use_signed_kernel())
Shashank Mittala0032282011-08-26 14:50:11 -07004420 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03004421 if((device.is_unlocked) || (device.is_tampered))
4422 {
4423 #ifdef TZ_TAMPER_FUSE
4424 set_tamper_fuse_cmd();
4425 #endif
4426 #if USE_PCOM_SECBOOT
4427 set_tamper_flag(device.is_tampered);
4428 #endif
4429 }
Shashank Mittala0032282011-08-26 14:50:11 -07004430 }
Amit Blay6281ebc2015-01-11 14:44:08 +02004431
Mayank Grover351a75e2017-05-30 20:06:08 +05304432retry_boot:
4433 /* Trying to boot active partition */
4434 if (partition_multislot_is_supported())
4435 {
4436 boot_slot = partition_find_boot_slot();
4437 partition_mark_active_slot(boot_slot);
4438 if (boot_slot == INVALID)
4439 goto fastboot;
4440 }
4441
4442 boot_err_type = boot_linux_from_mmc();
4443 switch (boot_err_type)
4444 {
4445 case ERR_INVALID_PAGE_SIZE:
4446 case ERR_DT_PARSE:
4447 case ERR_ABOOT_ADDR_OVERLAP:
4448 if(partition_multislot_is_supported())
4449 goto retry_boot;
4450 else
4451 break;
4452 case ERR_INVALID_BOOT_MAGIC:
4453 default:
4454 break;
4455 /* going to fastboot menu */
4456 }
Shashank Mittala0032282011-08-26 14:50:11 -07004457 }
Pavel Nedev5d91d412013-04-29 11:34:24 +03004458 else
4459 {
4460 recovery_init();
4461 #if USE_PCOM_SECBOOT
4462 if((device.is_unlocked) || (device.is_tampered))
4463 set_tamper_flag(device.is_tampered);
4464 #endif
4465 boot_linux_from_flash();
4466 }
4467 dprintf(CRITICAL, "ERROR: Could not do normal boot. Reverting "
4468 "to fastboot mode.\n");
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004469 }
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07004470
Mayank Grover351a75e2017-05-30 20:06:08 +05304471fastboot:
Amol Jadi5edf3552013-07-23 14:15:34 -07004472 /* We are here means regular boot did not happen. Start fastboot. */
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07004473
Amol Jadi5edf3552013-07-23 14:15:34 -07004474 /* register aboot specific fastboot commands */
4475 aboot_fastboot_register_commands();
Amol Jadi57abe4c2011-05-24 15:47:27 -07004476
Amol Jadi5edf3552013-07-23 14:15:34 -07004477 /* dump partition table for debug info */
Kinson Chikf1a43512011-07-14 11:28:39 -07004478 partition_dump();
Amol Jadi5edf3552013-07-23 14:15:34 -07004479
4480 /* initialize and start fastboot */
4481 fastboot_init(target_get_scratch_address(), target_get_max_flash_size());
lijuang4ece1e72015-08-14 21:02:36 +08004482#if FBCON_DISPLAY_MSG
lijuang9a7d3b92015-11-30 14:41:24 +08004483 display_fastboot_menu();
lijuang4ece1e72015-08-14 21:02:36 +08004484#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08004485}
4486
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07004487uint32_t get_page_size()
4488{
4489 return page_size;
4490}
4491
Amir Samuelov57a6fa22013-06-05 16:36:43 +03004492/*
4493 * Calculated and save hash (SHA256) for non-signed boot image.
4494 *
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07004495 * @param image_addr - Boot image address
4496 * @param image_size - Size of the boot image
Amir Samuelov57a6fa22013-06-05 16:36:43 +03004497 *
4498 * @return int - 0 on success, negative value on failure.
4499 */
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07004500static int aboot_save_boot_hash_mmc(uint32_t image_addr, uint32_t image_size)
Amir Samuelov57a6fa22013-06-05 16:36:43 +03004501{
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07004502 unsigned int digest[8];
4503#if IMAGE_VERIF_ALGO_SHA1
4504 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA1;
4505#else
4506 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA256;
4507#endif
Amir Samuelov57a6fa22013-06-05 16:36:43 +03004508
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07004509 target_crypto_init_params();
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -08004510 hash_find((unsigned char *) image_addr, image_size, (unsigned char *)&digest, auth_algo);
Amir Samuelov57a6fa22013-06-05 16:36:43 +03004511
4512 save_kernel_hash_cmd(digest);
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07004513 dprintf(INFO, "aboot_save_boot_hash_mmc: imagesize_actual size %d bytes.\n", (int) image_size);
Amir Samuelov57a6fa22013-06-05 16:36:43 +03004514
4515 return 0;
4516}
4517
Mayank Grover351a75e2017-05-30 20:06:08 +05304518
Brian Swetland9c4c0752009-01-25 16:23:50 -08004519APP_START(aboot)
4520 .init = aboot_init,
4521APP_END