blob: 09ba226133add36ee48dcab316397ef6c7386a82 [file] [log] [blame]
Brian Swetland9c4c0752009-01-25 16:23:50 -08001/*
2 * Copyright (c) 2009, Google Inc.
3 * All rights reserved.
4 *
lijuang9a7d3b92015-11-30 14:41:24 +08005 * Copyright (c) 2009-2016, 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>
Greg Griscod6250552011-06-29 14:40:23 -070051#include <platform.h>
Shashank Mittalcd98d472011-08-02 14:29:24 -070052#include <crypto_hash.h>
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070053#include <malloc.h>
Amol Jadi492d5a52013-03-15 16:12:34 -070054#include <boot_stats.h>
Amir Samuelov57a6fa22013-06-05 16:36:43 +030055#include <sha.h>
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -070056#include <platform/iomap.h>
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -070057#include <boot_device.h>
Shashank Mittald3e54dd2014-08-28 15:24:02 -070058#include <boot_verifier.h>
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +053059#include <image_verify.h>
Matthew Qinbb7923d2015-02-09 10:56:09 +080060#include <decompress.h>
Unnati Gandhi17b3bfc2015-05-11 12:58:16 +053061#include <platform/timer.h>
lijuang511a2b52015-08-14 20:50:51 +080062#include <sys/types.h>
Channagoud Kadabi036c6052015-02-09 15:19:59 -080063#if USE_RPMB_FOR_DEVINFO
64#include <rpmb.h>
65#endif
Dima Zavin214cc642009-01-26 11:16:21 -080066
Channagoud Kadabi90869ce2015-04-27 11:15:14 -070067#if ENABLE_WBC
68#include <pm_app_smbchg.h>
69#endif
70
Neeti Desai17379b82012-06-04 18:42:53 -070071#if DEVICE_TREE
72#include <libfdt.h>
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070073#include <dev_tree.h>
Neeti Desai17379b82012-06-04 18:42:53 -070074#endif
75
Aparna Mallavarapu118ccae2015-06-03 13:47:11 +053076#if WDOG_SUPPORT
77#include <wdog.h>
78#endif
79
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -070080#include <reboot.h>
Shashank Mittalcd98d472011-08-02 14:29:24 -070081#include "image_verify.h"
Shashank Mittal024c0332010-02-03 11:44:00 -080082#include "recovery.h"
Brian Swetland9c4c0752009-01-25 16:23:50 -080083#include "bootimg.h"
84#include "fastboot.h"
Ajay Dudani5c761132011-04-07 20:19:04 -070085#include "sparse_format.h"
Ajay Dudanide984792015-03-02 09:57:41 -080086#include "meta_format.h"
Greg Grisco6e754772011-06-23 12:19:39 -070087#include "mmc.h"
Shashank Mittal162244e2011-08-08 19:01:25 -070088#include "devinfo.h"
Neeti Desai465491e2012-07-31 12:53:35 -070089#include "board.h"
Shashank Mittal162244e2011-08-08 19:01:25 -070090#include "scm.h"
Amit Blay6281ebc2015-01-11 14:44:08 +020091#include "mdtp.h"
Sridhar Parasuram32b30662015-07-10 13:33:22 -070092#include "secapp_loader.h"
lijuanga40d6302015-07-20 20:10:13 +080093#include <menu_keys_detect.h>
94#include <display_menu.h>
Channagoud Kadabi736c4962015-08-21 11:56:52 -070095#include "fastboot_test.h"
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -070096
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070097extern bool target_use_signed_kernel(void);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070098extern void platform_uninit(void);
Channagoud Kadabi33defe22013-06-18 18:35:40 -070099extern void target_uninit(void);
Joonwoo Park61112782013-10-02 19:50:39 -0700100extern int get_target_boot_params(const char *cmdline, const char *part,
vijay kumar870515d2015-08-31 16:37:24 +0530101 char **buf);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700102
Sridhar Parasuram7e16d172015-07-05 11:35:23 -0700103void *info_buf;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700104void write_device_info_mmc(device_info *dev);
105void write_device_info_flash(device_info *dev);
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -0700106static int aboot_save_boot_hash_mmc(uint32_t image_addr, uint32_t image_size);
Channagoud Kadabiad259832015-05-29 11:14:17 -0700107static int aboot_frp_unlock(char *pname, void *data, unsigned sz);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700108
Sridhar Parasurame94e8152014-10-24 14:06:03 -0700109/* fastboot command function pointer */
110typedef void (*fastboot_cmd_fn) (const char *, void *, unsigned);
111
112struct fastboot_cmd_desc {
113 char * name;
114 fastboot_cmd_fn cb;
115};
116
Subbaraman Narayanamurthyeb92bcc2010-07-20 14:32:46 -0700117#define EXPAND(NAME) #NAME
118#define TARGET(NAME) EXPAND(NAME)
Brian Swetland2defe162009-08-18 14:35:59 -0700119
Ajay Singh Parmara7865a82015-04-16 21:27:52 -0700120#define DISPLAY_PANEL_HDMI "hdmi"
121
Ajay Dudanicd01f9b2010-02-23 21:13:04 -0800122#ifdef MEMBASE
123#define EMMC_BOOT_IMG_HEADER_ADDR (0xFF000+(MEMBASE))
124#else
David Ng183a7422009-12-07 14:55:21 -0800125#define EMMC_BOOT_IMG_HEADER_ADDR 0xFF000
Ajay Dudanicd01f9b2010-02-23 21:13:04 -0800126#endif
127
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700128#ifndef MEMSIZE
129#define MEMSIZE 1024*1024
130#endif
131
132#define MAX_TAGS_SIZE 1024
133
Kun Liang2f1601a2013-08-12 16:29:54 +0800134/* make 4096 as default size to ensure EFS,EXT4's erasing */
135#define DEFAULT_ERASE_SIZE 4096
Ujwal Patelc0b0a252015-08-16 14:05:35 -0700136#define MAX_PANEL_BUF_SIZE 196
Vijay Kumar Pendoti0b21f462016-05-02 17:09:18 +0530137#define FOOTER_SIZE 16384
Kun Liang2f1601a2013-08-12 16:29:54 +0800138
Dhaval Patelf83d73b2014-06-23 16:24:37 -0700139#define DISPLAY_DEFAULT_PREFIX "mdss_mdp"
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700140#define BOOT_DEV_MAX_LEN 64
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -0800141
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800142#define IS_ARM64(ptr) (ptr->magic_64 == KERNEL64_HDR_MAGIC) ? true : false
143
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -0700144#define ADD_OF(a, b) (UINT_MAX - b > a) ? (a + b) : UINT_MAX
145
Ameya Thakur10a33452016-06-13 14:24:26 -0700146//Size of the header that is used in case the boot image has
147//a uncompressed kernel + appended dtb
148#define PATCHED_KERNEL_HEADER_SIZE 20
149
150//String used to determine if the boot image has
151//a uncompressed kernel + appended dtb
152#define PATCHED_KERNEL_MAGIC "UNCOMPRESSED_IMG"
153
Sridhar Parasuram7bd4aaf2015-02-12 11:14:38 -0800154#if USE_BOOTDEV_CMDLINE
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700155static const char *emmc_cmdline = " androidboot.bootdevice=";
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700156#else
David Ng183a7422009-12-07 14:55:21 -0800157static const char *emmc_cmdline = " androidboot.emmc=true";
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700158#endif
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800159static const char *usb_sn_cmdline = " androidboot.serialno=";
Pavel Nedev328ac822013-04-05 15:25:11 +0300160static const char *androidboot_mode = " androidboot.mode=";
Matthew Qind886f3c2014-01-17 16:52:01 +0800161static const char *alarmboot_cmdline = " androidboot.alarmboot=true";
Pavel Nedev898298c2013-02-27 12:36:09 -0800162static const char *loglevel = " quiet";
Ajay Dudanica3a33c2011-11-18 08:31:40 -0800163static const char *battchg_pause = " androidboot.mode=charger";
Shashank Mittalcd98d472011-08-02 14:29:24 -0700164static const char *auth_kernel = " androidboot.authorized_kernel=true";
Pavel Nedev5614d222013-06-17 18:01:02 +0300165static const char *secondary_gpt_enable = " gpt";
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200166static const char *mdtp_activated_flag = " mdtp";
David Ng183a7422009-12-07 14:55:21 -0800167
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800168static const char *baseband_apq = " androidboot.baseband=apq";
169static const char *baseband_msm = " androidboot.baseband=msm";
170static const char *baseband_csfb = " androidboot.baseband=csfb";
171static const char *baseband_svlte2a = " androidboot.baseband=svlte2a";
Ajay Dudani403bc492011-09-30 16:17:21 -0700172static const char *baseband_mdm = " androidboot.baseband=mdm";
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800173static const char *baseband_mdm2 = " androidboot.baseband=mdm2";
Amol Jadi5c61a952012-05-04 17:05:35 -0700174static const char *baseband_sglte = " androidboot.baseband=sglte";
Amol Jadi2a15a272013-01-22 12:03:36 -0800175static const char *baseband_dsda = " androidboot.baseband=dsda";
176static const char *baseband_dsda2 = " androidboot.baseband=dsda2";
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800177static const char *baseband_sglte2 = " androidboot.baseband=sglte2";
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800178static const char *warmboot_cmdline = " qpnp-power-on.warm_boot=1";
Vijay Kumar Pendotib228cfc2016-06-13 20:15:23 +0530179static const char *baseband_apq_nowgr = " androidboot.baseband=baseband_apq_nowgr";
Ajay Dudanid04110c2011-01-17 23:55:07 -0800180
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700181#if VERIFIED_BOOT
Parth Dixitddbc7352015-10-18 03:13:31 +0530182#if !VBOOT_MOTA
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700183static const char *verity_mode = " androidboot.veritymode=";
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700184static const char *verified_state= " androidboot.verifiedbootstate=";
Parth Dixita5715a02015-10-29 12:25:10 +0530185static const char *keymaster_v1= " androidboot.keymaster=1";
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700186//indexed based on enum values, green is 0 by default
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700187
188struct verified_boot_verity_mode vbvm[] =
189{
190 {false, "logging"},
191 {true, "enforcing"},
192};
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700193struct verified_boot_state_name vbsn[] =
194{
195 {GREEN, "green"},
196 {ORANGE, "orange"},
197 {YELLOW,"yellow"},
198 {RED,"red" },
199};
200#endif
Parth Dixitddbc7352015-10-18 03:13:31 +0530201#endif
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700202
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700203static unsigned page_size = 0;
204static unsigned page_mask = 0;
Gaurav Nebhwani4d2389c2016-03-17 21:10:05 +0530205static unsigned mmc_blocksize = 0;
206static unsigned mmc_blocksize_mask = 0;
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700207static char ffbm_mode_string[FFBM_MODE_BUF_SIZE];
208static bool boot_into_ffbm;
vijay kumar870515d2015-08-31 16:37:24 +0530209static char *target_boot_params = NULL;
Matthew Qind886f3c2014-01-17 16:52:01 +0800210static bool boot_reason_alarm;
Channagoud Kadabi80a182b2015-03-11 17:04:23 -0700211static bool devinfo_present = true;
Channagoud Kadabi736c4962015-08-21 11:56:52 -0700212bool boot_into_fastboot = false;
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700213
Shashank Mittalcd98d472011-08-02 14:29:24 -0700214/* Assuming unauthorized kernel image by default */
215static int auth_kernel_img = 0;
Parth Dixit6f5822f2015-10-18 01:20:53 +0530216#if VBOOT_MOTA
217static device_info device = {DEVICE_MAGIC, 0, 0, 0, 0, {0}, {0},{0}};
218#else
lijuang511a2b52015-08-14 20:50:51 +0800219static device_info device = {DEVICE_MAGIC, 0, 0, 0, 0, {0}, {0},{0}, 1};
Parth Dixit6f5822f2015-10-18 01:20:53 +0530220#endif
vijay kumarc65876c2015-04-24 13:29:16 +0530221static bool is_allow_unlock = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -0700222
vijay kumarca2e6812015-07-08 20:28:25 +0530223static char frp_ptns[2][8] = {"config","frp"};
224
lijuang511a2b52015-08-14 20:50:51 +0800225static const char *critical_flash_allowed_ptn[] = {
226 "aboot",
227 "rpm",
228 "tz",
229 "sbl",
230 "sdi",
231 "sbl1",
232 "xbl",
233 "hyp",
234 "pmic",
235 "bootloader",
236 "devinfo",
237 "partition"};
238
Dima Zavin42168f22009-01-30 11:52:22 -0800239struct atag_ptbl_entry
240{
241 char name[16];
242 unsigned offset;
243 unsigned size;
244 unsigned flags;
245};
246
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700247/*
248 * Partition info, required to be published
249 * for fastboot
250 */
251struct getvar_partition_info {
252 const char part_name[MAX_GPT_NAME_SIZE]; /* Partition name */
253 char getvar_size[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for size */
254 char getvar_type[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for type */
255 char size_response[MAX_RSP_SIZE]; /* fastboot response for size */
256 char type_response[MAX_RSP_SIZE]; /* fastboot response for type */
257};
258
259/*
260 * Right now, we are publishing the info for only
261 * three partitions
262 */
263struct getvar_partition_info part_info[] =
264{
265 { "system" , "partition-size:", "partition-type:", "", "ext4" },
266 { "userdata", "partition-size:", "partition-type:", "", "ext4" },
267 { "cache" , "partition-size:", "partition-type:", "", "ext4" },
268};
269
270char max_download_size[MAX_RSP_SIZE];
Ameya Thakur11cf1a62013-08-05 12:44:48 -0700271char charger_screen_enabled[MAX_RSP_SIZE];
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800272char sn_buf[13];
Dhaval Patel223ec952013-07-18 14:49:44 -0700273char display_panel_buf[MAX_PANEL_BUF_SIZE];
Unnati Gandhi62c8ab82014-01-24 11:01:01 +0530274char panel_display_mode[MAX_RSP_SIZE];
lijuang102dfa92015-10-09 18:31:03 +0800275
276#if CHECK_BAT_VOLTAGE
lijuang65c5a822015-08-29 16:35:36 +0800277char battery_voltage[MAX_RSP_SIZE];
lijuang102dfa92015-10-09 18:31:03 +0800278char battery_soc_ok [MAX_RSP_SIZE];
279#endif
280
lijuangf16461c2015-08-03 17:09:34 +0800281char get_variant[MAX_RSP_SIZE];
Greg Griscod6250552011-06-29 14:40:23 -0700282
Greg Griscod2471ef2011-07-14 13:00:42 -0700283extern int emmc_recovery_init(void);
284
Kinson Chik0b1c8162011-08-31 16:31:57 -0700285#if NO_KEYPAD_DRIVER
286extern int fastboot_trigger(void);
287#endif
Greg Griscod2471ef2011-07-14 13:00:42 -0700288
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800289static void update_ker_tags_rdisk_addr(struct boot_img_hdr *hdr, bool is_arm64)
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700290{
291 /* overwrite the destination of specified for the project */
Channagoud Kadabi7042fa32013-04-26 16:44:14 -0700292#ifdef ABOOT_IGNORE_BOOT_HEADER_ADDRS
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800293 if (is_arm64)
294 hdr->kernel_addr = ABOOT_FORCE_KERNEL64_ADDR;
295 else
296 hdr->kernel_addr = ABOOT_FORCE_KERNEL_ADDR;
Channagoud Kadabi7042fa32013-04-26 16:44:14 -0700297 hdr->ramdisk_addr = ABOOT_FORCE_RAMDISK_ADDR;
298 hdr->tags_addr = ABOOT_FORCE_TAGS_ADDR;
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700299#endif
300}
301
Dima Zavin42168f22009-01-30 11:52:22 -0800302static void ptentry_to_tag(unsigned **ptr, struct ptentry *ptn)
303{
304 struct atag_ptbl_entry atag_ptn;
305
306 memcpy(atag_ptn.name, ptn->name, 16);
307 atag_ptn.name[15] = '\0';
308 atag_ptn.offset = ptn->start;
309 atag_ptn.size = ptn->length;
310 atag_ptn.flags = ptn->flags;
311 memcpy(*ptr, &atag_ptn, sizeof(struct atag_ptbl_entry));
312 *ptr += sizeof(struct atag_ptbl_entry) / sizeof(unsigned);
313}
Brian Swetland9c4c0752009-01-25 16:23:50 -0800314
lijuang102dfa92015-10-09 18:31:03 +0800315#if CHECK_BAT_VOLTAGE
316void update_battery_status(void)
317{
318 snprintf(battery_voltage,MAX_RSP_SIZE, "%d",target_get_battery_voltage());
319 snprintf(battery_soc_ok ,MAX_RSP_SIZE, "%s",target_battery_soc_ok()? "yes":"no");
320}
321#endif
322
Neeti Desaie245d492012-06-01 12:52:13 -0700323unsigned char *update_cmdline(const char * cmdline)
Brian Swetland9c4c0752009-01-25 16:23:50 -0800324{
David Ng183a7422009-12-07 14:55:21 -0800325 int cmdline_len = 0;
326 int have_cmdline = 0;
Amol Jadi168b7712012-03-06 16:15:00 -0800327 unsigned char *cmdline_final = NULL;
Neeti Desaie245d492012-06-01 12:52:13 -0700328 int pause_at_bootup = 0;
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800329 bool warm_boot = false;
Pavel Nedev5614d222013-06-17 18:01:02 +0300330 bool gpt_exists = partition_gpt_exists();
Joonwoo Park61112782013-10-02 19:50:39 -0700331 int have_target_boot_params = 0;
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700332 char *boot_dev_buf = NULL;
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200333 bool is_mdtp_activated = 0;
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700334#if VERIFIED_BOOT
Parth Dixitddbc7352015-10-18 03:13:31 +0530335#if !VBOOT_MOTA
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700336 uint32_t boot_state = boot_verify_get_state();
337#endif
Parth Dixitddbc7352015-10-18 03:13:31 +0530338#endif
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700339
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200340#ifdef MDTP_SUPPORT
341 mdtp_activated(&is_mdtp_activated);
342#endif /* MDTP_SUPPORT */
Dima Zavin42168f22009-01-30 11:52:22 -0800343
Brian Swetland9c4c0752009-01-25 16:23:50 -0800344 if (cmdline && cmdline[0]) {
David Ng183a7422009-12-07 14:55:21 -0800345 cmdline_len = strlen(cmdline);
346 have_cmdline = 1;
347 }
348 if (target_is_emmc_boot()) {
349 cmdline_len += strlen(emmc_cmdline);
Sridhar Parasuram7bd4aaf2015-02-12 11:14:38 -0800350#if USE_BOOTDEV_CMDLINE
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700351 boot_dev_buf = (char *) malloc(sizeof(char) * BOOT_DEV_MAX_LEN);
352 ASSERT(boot_dev_buf);
353 platform_boot_dev_cmdline(boot_dev_buf);
354 cmdline_len += strlen(boot_dev_buf);
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700355#endif
David Ng183a7422009-12-07 14:55:21 -0800356 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800357
358 cmdline_len += strlen(usb_sn_cmdline);
359 cmdline_len += strlen(sn_buf);
360
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700361#if VERIFIED_BOOT
Parth Dixitddbc7352015-10-18 03:13:31 +0530362#if !VBOOT_MOTA
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700363 cmdline_len += strlen(verified_state) + strlen(vbsn[boot_state].name);
Sridhar Parasuram7d8bb9a2015-09-23 18:21:19 -0700364 if ((device.verity_mode != 0 ) && (device.verity_mode != 1))
365 {
366 dprintf(CRITICAL, "Devinfo paritition possibly corrupted!!!. Please erase devinfo partition to continue booting\n");
367 ASSERT(0);
368 }
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700369 cmdline_len += strlen(verity_mode) + strlen(vbvm[device.verity_mode].name);
Parth Dixita5715a02015-10-29 12:25:10 +0530370 cmdline_len += strlen(keymaster_v1);
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700371#endif
Parth Dixitddbc7352015-10-18 03:13:31 +0530372#endif
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700373
Pavel Nedev5614d222013-06-17 18:01:02 +0300374 if (boot_into_recovery && gpt_exists)
375 cmdline_len += strlen(secondary_gpt_enable);
376
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200377 if(is_mdtp_activated)
378 cmdline_len += strlen(mdtp_activated_flag);
379
Pavel Nedev328ac822013-04-05 15:25:11 +0300380 if (boot_into_ffbm) {
381 cmdline_len += strlen(androidboot_mode);
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700382 cmdline_len += strlen(ffbm_mode_string);
Pavel Nedev898298c2013-02-27 12:36:09 -0800383 /* reduce kernel console messages to speed-up boot */
384 cmdline_len += strlen(loglevel);
Matthew Qind886f3c2014-01-17 16:52:01 +0800385 } else if (boot_reason_alarm) {
386 cmdline_len += strlen(alarmboot_cmdline);
Zhenhua Huang431dafa2015-06-30 16:13:37 +0800387 } else if ((target_build_variant_user() || device.charger_screen_enabled)
388 && target_pause_for_battery_charge()) {
David Ngf773dde2010-07-26 19:55:08 -0700389 pause_at_bootup = 1;
390 cmdline_len += strlen(battchg_pause);
391 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800392
Shashank Mittalcd98d472011-08-02 14:29:24 -0700393 if(target_use_signed_kernel() && auth_kernel_img) {
394 cmdline_len += strlen(auth_kernel);
395 }
396
Joonwoo Park61112782013-10-02 19:50:39 -0700397 if (get_target_boot_params(cmdline, boot_into_recovery ? "recoveryfs" :
398 "system",
vijay kumar870515d2015-08-31 16:37:24 +0530399 &target_boot_params) == 0) {
Joonwoo Park61112782013-10-02 19:50:39 -0700400 have_target_boot_params = 1;
401 cmdline_len += strlen(target_boot_params);
402 }
403
Ajay Dudanid04110c2011-01-17 23:55:07 -0800404 /* Determine correct androidboot.baseband to use */
405 switch(target_baseband())
406 {
407 case BASEBAND_APQ:
408 cmdline_len += strlen(baseband_apq);
409 break;
410
411 case BASEBAND_MSM:
412 cmdline_len += strlen(baseband_msm);
413 break;
414
415 case BASEBAND_CSFB:
416 cmdline_len += strlen(baseband_csfb);
417 break;
418
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800419 case BASEBAND_SVLTE2A:
420 cmdline_len += strlen(baseband_svlte2a);
Ajay Dudanid04110c2011-01-17 23:55:07 -0800421 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700422
423 case BASEBAND_MDM:
424 cmdline_len += strlen(baseband_mdm);
425 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700426
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800427 case BASEBAND_MDM2:
428 cmdline_len += strlen(baseband_mdm2);
429 break;
430
Amol Jadi5c61a952012-05-04 17:05:35 -0700431 case BASEBAND_SGLTE:
432 cmdline_len += strlen(baseband_sglte);
433 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530434
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800435 case BASEBAND_SGLTE2:
436 cmdline_len += strlen(baseband_sglte2);
437 break;
438
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530439 case BASEBAND_DSDA:
440 cmdline_len += strlen(baseband_dsda);
441 break;
Amol Jadi2a15a272013-01-22 12:03:36 -0800442
443 case BASEBAND_DSDA2:
444 cmdline_len += strlen(baseband_dsda2);
445 break;
Vijay Kumar Pendotib228cfc2016-06-13 20:15:23 +0530446 case BASEBAND_APQ_NOWGR:
447 cmdline_len += strlen(baseband_apq_nowgr);
448 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800449 }
450
Lijuan Gao4a5b8322014-07-24 10:38:42 +0800451 if (cmdline) {
452 if ((strstr(cmdline, DISPLAY_DEFAULT_PREFIX) == NULL) &&
Padmanabhan Komandurubccbcdc2015-06-30 16:19:24 +0530453 target_display_panel_node(display_panel_buf,
454 MAX_PANEL_BUF_SIZE) &&
Lijuan Gao4a5b8322014-07-24 10:38:42 +0800455 strlen(display_panel_buf)) {
456 cmdline_len += strlen(display_panel_buf);
457 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700458 }
459
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800460 if (target_warm_boot()) {
461 warm_boot = true;
462 cmdline_len += strlen(warmboot_cmdline);
463 }
464
Channagoud Kadabi85c7ec32016-01-28 23:09:21 -0800465#if TARGET_CMDLINE_SUPPORT
466 char *target_cmdline_buf = malloc(TARGET_MAX_CMDLNBUF);
467 int target_cmd_line_len;
468 ASSERT(target_cmdline_buf);
469 target_cmd_line_len = target_update_cmdline(target_cmdline_buf);
470 cmdline_len += target_cmd_line_len;
471#endif
472
David Ng183a7422009-12-07 14:55:21 -0800473 if (cmdline_len > 0) {
474 const char *src;
Maria Yu52254c02014-07-04 16:14:54 +0800475 unsigned char *dst;
476
477 cmdline_final = (unsigned char*) malloc((cmdline_len + 4) & (~3));
478 ASSERT(cmdline_final != NULL);
vijay kumar287bb542015-09-29 13:01:52 +0530479 memset((void *)cmdline_final, 0, sizeof(*cmdline_final));
Maria Yu52254c02014-07-04 16:14:54 +0800480 dst = cmdline_final;
Neeti Desaie245d492012-06-01 12:52:13 -0700481
Amol Jadi168b7712012-03-06 16:15:00 -0800482 /* Save start ptr for debug print */
David Ng183a7422009-12-07 14:55:21 -0800483 if (have_cmdline) {
484 src = cmdline;
485 while ((*dst++ = *src++));
486 }
487 if (target_is_emmc_boot()) {
488 src = emmc_cmdline;
489 if (have_cmdline) --dst;
David Ngf773dde2010-07-26 19:55:08 -0700490 have_cmdline = 1;
491 while ((*dst++ = *src++));
Sridhar Parasuram7bd4aaf2015-02-12 11:14:38 -0800492#if USE_BOOTDEV_CMDLINE
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700493 src = boot_dev_buf;
494 if (have_cmdline) --dst;
495 while ((*dst++ = *src++));
496#endif
David Ngf773dde2010-07-26 19:55:08 -0700497 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800498
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700499#if VERIFIED_BOOT
Parth Dixitddbc7352015-10-18 03:13:31 +0530500#if !VBOOT_MOTA
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700501 src = verified_state;
502 if(have_cmdline) --dst;
503 have_cmdline = 1;
504 while ((*dst++ = *src++));
505 src = vbsn[boot_state].name;
506 if(have_cmdline) --dst;
507 while ((*dst++ = *src++));
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700508
Sridhar Parasuram7d8bb9a2015-09-23 18:21:19 -0700509 if ((device.verity_mode != 0 ) && (device.verity_mode != 1))
510 {
511 dprintf(CRITICAL, "Devinfo paritition possibly corrupted!!!. Please erase devinfo partition to continue booting\n");
512 ASSERT(0);
513 }
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700514 src = verity_mode;
515 if(have_cmdline) --dst;
516 while ((*dst++ = *src++));
517 src = vbvm[device.verity_mode].name;
518 if(have_cmdline) -- dst;
519 while ((*dst++ = *src++));
Parth Dixita5715a02015-10-29 12:25:10 +0530520 src = keymaster_v1;
521 if(have_cmdline) --dst;
522 while ((*dst++ = *src++));
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700523#endif
Parth Dixitddbc7352015-10-18 03:13:31 +0530524#endif
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800525 src = usb_sn_cmdline;
526 if (have_cmdline) --dst;
527 have_cmdline = 1;
528 while ((*dst++ = *src++));
529 src = sn_buf;
530 if (have_cmdline) --dst;
531 have_cmdline = 1;
532 while ((*dst++ = *src++));
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800533 if (warm_boot) {
534 if (have_cmdline) --dst;
535 src = warmboot_cmdline;
536 while ((*dst++ = *src++));
537 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800538
Pavel Nedev5614d222013-06-17 18:01:02 +0300539 if (boot_into_recovery && gpt_exists) {
540 src = secondary_gpt_enable;
541 if (have_cmdline) --dst;
542 while ((*dst++ = *src++));
543 }
544
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200545 if (is_mdtp_activated) {
546 src = mdtp_activated_flag;
547 if (have_cmdline) --dst;
548 while ((*dst++ = *src++));
549 }
550
Pavel Nedev328ac822013-04-05 15:25:11 +0300551 if (boot_into_ffbm) {
552 src = androidboot_mode;
553 if (have_cmdline) --dst;
554 while ((*dst++ = *src++));
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700555 src = ffbm_mode_string;
Pavel Nedev328ac822013-04-05 15:25:11 +0300556 if (have_cmdline) --dst;
557 while ((*dst++ = *src++));
Pavel Nedev898298c2013-02-27 12:36:09 -0800558 src = loglevel;
559 if (have_cmdline) --dst;
560 while ((*dst++ = *src++));
Matthew Qind886f3c2014-01-17 16:52:01 +0800561 } else if (boot_reason_alarm) {
562 src = alarmboot_cmdline;
563 if (have_cmdline) --dst;
564 while ((*dst++ = *src++));
Pavel Nedev328ac822013-04-05 15:25:11 +0300565 } else if (pause_at_bootup) {
David Ngf773dde2010-07-26 19:55:08 -0700566 src = battchg_pause;
567 if (have_cmdline) --dst;
David Ng183a7422009-12-07 14:55:21 -0800568 while ((*dst++ = *src++));
569 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800570
Shashank Mittalcd98d472011-08-02 14:29:24 -0700571 if(target_use_signed_kernel() && auth_kernel_img) {
572 src = auth_kernel;
573 if (have_cmdline) --dst;
574 while ((*dst++ = *src++));
575 }
576
Ajay Dudanid04110c2011-01-17 23:55:07 -0800577 switch(target_baseband())
578 {
579 case BASEBAND_APQ:
580 src = baseband_apq;
581 if (have_cmdline) --dst;
582 while ((*dst++ = *src++));
583 break;
584
585 case BASEBAND_MSM:
586 src = baseband_msm;
587 if (have_cmdline) --dst;
588 while ((*dst++ = *src++));
589 break;
590
591 case BASEBAND_CSFB:
592 src = baseband_csfb;
593 if (have_cmdline) --dst;
594 while ((*dst++ = *src++));
595 break;
596
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800597 case BASEBAND_SVLTE2A:
598 src = baseband_svlte2a;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800599 if (have_cmdline) --dst;
600 while ((*dst++ = *src++));
601 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700602
603 case BASEBAND_MDM:
604 src = baseband_mdm;
605 if (have_cmdline) --dst;
606 while ((*dst++ = *src++));
607 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700608
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800609 case BASEBAND_MDM2:
610 src = baseband_mdm2;
611 if (have_cmdline) --dst;
612 while ((*dst++ = *src++));
613 break;
614
Amol Jadi5c61a952012-05-04 17:05:35 -0700615 case BASEBAND_SGLTE:
616 src = baseband_sglte;
617 if (have_cmdline) --dst;
618 while ((*dst++ = *src++));
619 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530620
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800621 case BASEBAND_SGLTE2:
622 src = baseband_sglte2;
623 if (have_cmdline) --dst;
624 while ((*dst++ = *src++));
625 break;
626
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530627 case BASEBAND_DSDA:
628 src = baseband_dsda;
629 if (have_cmdline) --dst;
630 while ((*dst++ = *src++));
631 break;
Amol Jadi2a15a272013-01-22 12:03:36 -0800632
633 case BASEBAND_DSDA2:
634 src = baseband_dsda2;
635 if (have_cmdline) --dst;
636 while ((*dst++ = *src++));
637 break;
Vijay Kumar Pendotib228cfc2016-06-13 20:15:23 +0530638 case BASEBAND_APQ_NOWGR:
639 src = baseband_apq_nowgr;
640 if (have_cmdline) --dst;
641 while ((*dst++ = *src++));
642 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800643 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700644
645 if (strlen(display_panel_buf)) {
Dhaval Patel223ec952013-07-18 14:49:44 -0700646 src = display_panel_buf;
647 if (have_cmdline) --dst;
648 while ((*dst++ = *src++));
649 }
Joonwoo Park61112782013-10-02 19:50:39 -0700650
651 if (have_target_boot_params) {
652 if (have_cmdline) --dst;
653 src = target_boot_params;
654 while ((*dst++ = *src++));
vijay kumar870515d2015-08-31 16:37:24 +0530655 free(target_boot_params);
Joonwoo Park61112782013-10-02 19:50:39 -0700656 }
Channagoud Kadabi85c7ec32016-01-28 23:09:21 -0800657
658#if TARGET_CMDLINE_SUPPORT
659 if (target_cmdline_buf && target_cmd_line_len)
660 {
661 if (have_cmdline) --dst;
662 src = target_cmdline_buf;
663 while((*dst++ = *src++));
664 free(target_cmdline_buf);
665 }
666#endif
Neeti Desaie245d492012-06-01 12:52:13 -0700667 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700668
669
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700670 if (boot_dev_buf)
671 free(boot_dev_buf);
672
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -0800673 if (cmdline_final)
674 dprintf(INFO, "cmdline: %s\n", cmdline_final);
675 else
676 dprintf(INFO, "cmdline is NULL\n");
Neeti Desaie245d492012-06-01 12:52:13 -0700677 return cmdline_final;
678}
679
680unsigned *atag_core(unsigned *ptr)
681{
682 /* CORE */
683 *ptr++ = 2;
684 *ptr++ = 0x54410001;
685
686 return ptr;
687
688}
689
690unsigned *atag_ramdisk(unsigned *ptr, void *ramdisk,
691 unsigned ramdisk_size)
692{
693 if (ramdisk_size) {
694 *ptr++ = 4;
695 *ptr++ = 0x54420005;
696 *ptr++ = (unsigned)ramdisk;
697 *ptr++ = ramdisk_size;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800698 }
699
Neeti Desaie245d492012-06-01 12:52:13 -0700700 return ptr;
701}
702
703unsigned *atag_ptable(unsigned **ptr_addr)
704{
705 int i;
706 struct ptable *ptable;
707
708 if ((ptable = flash_get_ptable()) && (ptable->count != 0)) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700709 *(*ptr_addr)++ = 2 + (ptable->count * (sizeof(struct atag_ptbl_entry) /
710 sizeof(unsigned)));
Neeti Desaie245d492012-06-01 12:52:13 -0700711 *(*ptr_addr)++ = 0x4d534d70;
712 for (i = 0; i < ptable->count; ++i)
713 ptentry_to_tag(ptr_addr, ptable_get(ptable, i));
714 }
715
716 return (*ptr_addr);
717}
718
719unsigned *atag_cmdline(unsigned *ptr, const char *cmdline)
720{
721 int cmdline_length = 0;
722 int n;
Neeti Desaie245d492012-06-01 12:52:13 -0700723 char *dest;
724
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800725 cmdline_length = strlen((const char*)cmdline);
Neeti Desaie245d492012-06-01 12:52:13 -0700726 n = (cmdline_length + 4) & (~3);
727
728 *ptr++ = (n / 4) + 2;
729 *ptr++ = 0x54410009;
730 dest = (char *) ptr;
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800731 while ((*dest++ = *cmdline++));
Neeti Desaie245d492012-06-01 12:52:13 -0700732 ptr += (n / 4);
733
734 return ptr;
735}
736
737unsigned *atag_end(unsigned *ptr)
738{
Brian Swetland9c4c0752009-01-25 16:23:50 -0800739 /* END */
740 *ptr++ = 0;
741 *ptr++ = 0;
742
Neeti Desaie245d492012-06-01 12:52:13 -0700743 return ptr;
744}
745
746void generate_atags(unsigned *ptr, const char *cmdline,
747 void *ramdisk, unsigned ramdisk_size)
748{
vijay kumar21a37452015-12-29 16:23:21 +0530749 unsigned *orig_ptr = ptr;
Neeti Desaie245d492012-06-01 12:52:13 -0700750 ptr = atag_core(ptr);
751 ptr = atag_ramdisk(ptr, ramdisk, ramdisk_size);
752 ptr = target_atag_mem(ptr);
753
754 /* Skip NAND partition ATAGS for eMMC boot */
755 if (!target_is_emmc_boot()){
756 ptr = atag_ptable(&ptr);
757 }
758
vijay kumar21a37452015-12-29 16:23:21 +0530759 /*
760 * Atags size filled till + cmdline size + 1 unsigned for 4-byte boundary + 4 unsigned
761 * for atag identifier in atag_cmdline and atag_end should be with in MAX_TAGS_SIZE bytes
762 */
763 if (((ptr - orig_ptr) + strlen(cmdline) + 5 * sizeof(unsigned)) < MAX_TAGS_SIZE) {
764 ptr = atag_cmdline(ptr, cmdline);
765 ptr = atag_end(ptr);
766 }
767 else {
768 dprintf(CRITICAL,"Crossing ATAGs Max size allowed\n");
769 ASSERT(0);
770 }
Neeti Desaie245d492012-06-01 12:52:13 -0700771}
772
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700773typedef void entry_func_ptr(unsigned, unsigned, unsigned*);
Neeti Desaie245d492012-06-01 12:52:13 -0700774void boot_linux(void *kernel, unsigned *tags,
775 const char *cmdline, unsigned machtype,
776 void *ramdisk, unsigned ramdisk_size)
777{
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800778 unsigned char *final_cmdline;
Amol Jadib6be5c12012-11-14 13:39:51 -0800779#if DEVICE_TREE
Neeti Desai17379b82012-06-04 18:42:53 -0700780 int ret = 0;
Amol Jadib6be5c12012-11-14 13:39:51 -0800781#endif
782
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700783 void (*entry)(unsigned, unsigned, unsigned*) = (entry_func_ptr*)(PA((addr_t)kernel));
Deepa Dinamani25a9f762012-11-30 15:57:15 -0800784 uint32_t tags_phys = PA((addr_t)tags);
vijay kumar1a50a642015-11-16 12:41:15 +0530785 struct kernel64_hdr *kptr = ((struct kernel64_hdr*)(PA((addr_t)kernel)));
Deepa Dinamani25a9f762012-11-30 15:57:15 -0800786
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530787 ramdisk = (void *)PA((addr_t)ramdisk);
Neeti Desaie245d492012-06-01 12:52:13 -0700788
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800789 final_cmdline = update_cmdline((const char*)cmdline);
790
Neeti Desai17379b82012-06-04 18:42:53 -0700791#if DEVICE_TREE
Amol Jadib6be5c12012-11-14 13:39:51 -0800792 dprintf(INFO, "Updating device tree: start\n");
793
Neeti Desai17379b82012-06-04 18:42:53 -0700794 /* Update the Device Tree */
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530795 ret = update_device_tree((void *)tags,(const char *)final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -0700796 if(ret)
797 {
798 dprintf(CRITICAL, "ERROR: Updating Device Tree Failed \n");
799 ASSERT(0);
800 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800801 dprintf(INFO, "Updating device tree: done\n");
Neeti Desai17379b82012-06-04 18:42:53 -0700802#else
Neeti Desaie245d492012-06-01 12:52:13 -0700803 /* Generating the Atags */
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800804 generate_atags(tags, final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -0700805#endif
Neeti Desaie245d492012-06-01 12:52:13 -0700806
Maria Yu52254c02014-07-04 16:14:54 +0800807 free(final_cmdline);
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -0700808
809#if VERIFIED_BOOT
lijuangbdd9bb42016-03-01 18:22:17 +0800810#if !VBOOT_MOTA
811 if (device.verity_mode == 0) {
812#if FBCON_DISPLAY_MSG
813 display_bootverify_menu(DISPLAY_MENU_LOGGING);
814 wait_for_users_action();
815#else
816 dprintf(CRITICAL,
817 "The dm-verity is not started in enforcing mode.\nWait for 5 seconds before proceeding\n");
818 mdelay(5000);
819#endif
820 }
821
822#endif
823#endif
824
825#if VERIFIED_BOOT
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -0700826 /* Write protect the device info */
Channagoud Kadabi3bd9d1e2015-05-05 16:18:20 -0700827 if (!boot_into_recovery && target_build_variant_user() && devinfo_present && mmc_write_protect("devinfo", 1))
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -0700828 {
829 dprintf(INFO, "Failed to write protect dev info\n");
830 ASSERT(0);
831 }
832#endif
833
Aravind Venkateswaran8f076242014-02-25 16:25:30 -0800834 /* Turn off splash screen if enabled */
835#if DISPLAY_SPLASH_SCREEN
836 target_display_shutdown();
837#endif
838
Veera Sundaram Sankaran67ea0932015-09-25 10:09:30 -0700839 /* Perform target specific cleanup */
840 target_uninit();
Aravind Venkateswaran8f076242014-02-25 16:25:30 -0800841
Deepa Dinamani33734bc2013-03-06 12:16:06 -0800842 dprintf(INFO, "booting linux @ %p, ramdisk @ %p (%d), tags/device tree @ %p\n",
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530843 entry, ramdisk, ramdisk_size, (void *)tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800844
845 enter_critical_section();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700846
Amol Jadi4421e652011-06-16 15:00:48 -0700847 /* do any platform specific cleanup before kernel entry */
848 platform_uninit();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700849
Brian Swetland9c4c0752009-01-25 16:23:50 -0800850 arch_disable_cache(UCACHE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700851
Amol Jadi504f9fe2012-08-16 13:56:48 -0700852#if ARM_WITH_MMU
Brian Swetland9c4c0752009-01-25 16:23:50 -0800853 arch_disable_mmu();
Amol Jadi504f9fe2012-08-16 13:56:48 -0700854#endif
Amol Jadi492d5a52013-03-15 16:12:34 -0700855 bs_set_timestamp(BS_KERNEL_ENTRY);
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800856
857 if (IS_ARM64(kptr))
858 /* Jump to a 64bit kernel */
859 scm_elexec_call((paddr_t)kernel, tags_phys);
860 else
861 /* Jump to a 32bit kernel */
862 entry(0, machtype, (unsigned*)tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800863}
864
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700865/* Function to check if the memory address range falls within the aboot
866 * boundaries.
867 * start: Start of the memory region
868 * size: Size of the memory region
869 */
Vijay Kumar Pendoti9c002ad2016-03-09 13:52:45 +0530870int check_aboot_addr_range_overlap(uintptr_t start, uint32_t size)
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700871{
872 /* Check for boundary conditions. */
Sundarajan Srinivasance2a0ea2013-12-16 17:02:56 -0800873 if ((UINT_MAX - start) < size)
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700874 return -1;
875
876 /* Check for memory overlap. */
877 if ((start < MEMBASE) && ((start + size) <= MEMBASE))
878 return 0;
Channagoud Kadabi94143912013-10-15 12:53:52 -0700879 else if (start >= (MEMBASE + MEMSIZE))
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700880 return 0;
881 else
882 return -1;
883}
884
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800885#define ROUND_TO_PAGE(x,y) (((x) + (y)) & (~(y)))
Brian Swetland9c4c0752009-01-25 16:23:50 -0800886
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800887BUF_DMA_ALIGN(buf, BOOT_IMG_MAX_PAGE_SIZE); //Equal to max-supported pagesize
Amol Jadib6be5c12012-11-14 13:39:51 -0800888#if DEVICE_TREE
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800889BUF_DMA_ALIGN(dt_buf, BOOT_IMG_MAX_PAGE_SIZE);
Amol Jadib6be5c12012-11-14 13:39:51 -0800890#endif
Dima Zavin214cc642009-01-26 11:16:21 -0800891
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700892static void verify_signed_bootimg(uint32_t bootimg_addr, uint32_t bootimg_size)
893{
894 int ret;
Channagoud Kadabia8c623f2015-01-13 14:48:48 -0800895
896#if !VERIFIED_BOOT
Sundarajan Srinivasance54d6e2013-11-11 12:45:00 -0800897#if IMAGE_VERIF_ALGO_SHA1
898 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA1;
899#else
900 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA256;
901#endif
Channagoud Kadabia8c623f2015-01-13 14:48:48 -0800902#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700903
904 /* Assume device is rooted at this time. */
905 device.is_tampered = 1;
906
907 dprintf(INFO, "Authenticating boot image (%d): start\n", bootimg_size);
908
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700909#if VERIFIED_BOOT
910 if(boot_into_recovery)
911 {
912 ret = boot_verify_image((unsigned char *)bootimg_addr,
Unnati Gandhi47051252015-03-02 15:21:09 +0530913 bootimg_size, "/recovery");
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700914 }
915 else
916 {
917 ret = boot_verify_image((unsigned char *)bootimg_addr,
Unnati Gandhi47051252015-03-02 15:21:09 +0530918 bootimg_size, "/boot");
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700919 }
920 boot_verify_print_state();
921#else
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700922 ret = image_verify((unsigned char *)bootimg_addr,
923 (unsigned char *)(bootimg_addr + bootimg_size),
924 bootimg_size,
Sundarajan Srinivasance54d6e2013-11-11 12:45:00 -0800925 auth_algo);
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700926#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700927 dprintf(INFO, "Authenticating boot image: done return value = %d\n", ret);
928
929 if (ret)
930 {
931 /* Authorized kernel */
932 device.is_tampered = 0;
Sundarajan Srinivasan3fb21f12013-09-16 18:36:15 -0700933 auth_kernel_img = 1;
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700934 }
935
Amit Blay4aa292f2015-04-28 21:55:59 +0300936#ifdef MDTP_SUPPORT
937 {
938 /* Verify MDTP lock.
939 * For boot & recovery partitions, use aboot's verification result.
940 */
941 mdtp_ext_partition_verification_t ext_partition;
942 ext_partition.partition = boot_into_recovery ? MDTP_PARTITION_RECOVERY : MDTP_PARTITION_BOOT;
943 ext_partition.integrity_state = device.is_tampered ? MDTP_PARTITION_STATE_INVALID : MDTP_PARTITION_STATE_VALID;
944 ext_partition.page_size = 0; /* Not needed since already validated */
945 ext_partition.image_addr = 0; /* Not needed since already validated */
946 ext_partition.image_size = 0; /* Not needed since already validated */
947 ext_partition.sig_avail = FALSE; /* Not needed since already validated */
948 mdtp_fwlock_verify_lock(&ext_partition);
949 }
950#endif /* MDTP_SUPPORT */
951
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700952#if USE_PCOM_SECBOOT
953 set_tamper_flag(device.is_tampered);
954#endif
955
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700956#if VERIFIED_BOOT
Sridhar Parasuram8b792422015-07-05 11:38:13 -0700957 switch(boot_verify_get_state())
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700958 {
Sridhar Parasuram8b792422015-07-05 11:38:13 -0700959 case RED:
lijuanga40d6302015-07-20 20:10:13 +0800960#if FBCON_DISPLAY_MSG
lijuang9a7d3b92015-11-30 14:41:24 +0800961 display_bootverify_menu(DISPLAY_MENU_RED);
lijuanga40d6302015-07-20 20:10:13 +0800962 wait_for_users_action();
963#else
964 dprintf(CRITICAL,
965 "Your device has failed verification and may not work properly.\nWait for 5 seconds before proceeding\n");
966 mdelay(5000);
967#endif
968
969 break;
Sridhar Parasuram8b792422015-07-05 11:38:13 -0700970 case YELLOW:
lijuanga40d6302015-07-20 20:10:13 +0800971#if FBCON_DISPLAY_MSG
lijuang9a7d3b92015-11-30 14:41:24 +0800972 display_bootverify_menu(DISPLAY_MENU_YELLOW);
lijuanga40d6302015-07-20 20:10:13 +0800973 wait_for_users_action();
974#else
975 dprintf(CRITICAL,
976 "Your device has loaded a different operating system.\nWait for 5 seconds before proceeding\n");
977 mdelay(5000);
978#endif
979 break;
Sridhar Parasuram8b792422015-07-05 11:38:13 -0700980 default:
lijuanga40d6302015-07-20 20:10:13 +0800981 break;
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700982 }
983#endif
Sridhar Parasuram8b792422015-07-05 11:38:13 -0700984#if !VERIFIED_BOOT
Unnati Gandhi1be04752015-03-27 19:41:53 +0530985 if(device.is_tampered)
986 {
987 write_device_info_mmc(&device);
988 #ifdef TZ_TAMPER_FUSE
989 set_tamper_fuse_cmd();
990 #endif
991 #ifdef ASSERT_ON_TAMPER
992 dprintf(CRITICAL, "Device is tampered. Asserting..\n");
993 ASSERT(0);
994 #endif
995 }
Sridhar Parasuram8b792422015-07-05 11:38:13 -0700996#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700997}
998
Maunik Shah0f3c8ac2014-03-02 17:47:58 +0530999static bool check_format_bit()
1000{
1001 bool ret = false;
1002 int index;
1003 uint64_t offset;
1004 struct boot_selection_info *in = NULL;
1005 char *buf = NULL;
1006
1007 index = partition_get_index("bootselect");
1008 if (index == INVALID_PTN)
1009 {
1010 dprintf(INFO, "Unable to locate /bootselect partition\n");
1011 return ret;
1012 }
1013 offset = partition_get_offset(index);
1014 if(!offset)
1015 {
1016 dprintf(INFO, "partition /bootselect doesn't exist\n");
1017 return ret;
1018 }
1019 buf = (char *) memalign(CACHE_LINE, ROUNDUP(page_size, CACHE_LINE));
1020 ASSERT(buf);
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301021 if (mmc_read(offset, (uint32_t *)buf, page_size))
Maunik Shah0f3c8ac2014-03-02 17:47:58 +05301022 {
1023 dprintf(INFO, "mmc read failure /bootselect %d\n", page_size);
1024 free(buf);
1025 return ret;
1026 }
1027 in = (struct boot_selection_info *) buf;
1028 if ((in->signature == BOOTSELECT_SIGNATURE) &&
1029 (in->version == BOOTSELECT_VERSION)) {
1030 if ((in->state_info & BOOTSELECT_FORMAT) &&
1031 !(in->state_info & BOOTSELECT_FACTORY))
1032 ret = true;
1033 } else {
1034 dprintf(CRITICAL, "Signature: 0x%08x or version: 0x%08x mismatched of /bootselect\n",
1035 in->signature, in->version);
1036 ASSERT(0);
1037 }
1038 free(buf);
1039 return ret;
1040}
1041
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001042void boot_verifier_init()
1043{
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001044 uint32_t boot_state;
1045 /* Check if device unlock */
1046 if(device.is_unlocked)
1047 {
1048 boot_verify_send_event(DEV_UNLOCK);
1049 boot_verify_print_state();
1050 dprintf(CRITICAL, "Device is unlocked! Skipping verification...\n");
1051 return;
1052 }
1053 else
1054 {
1055 boot_verify_send_event(BOOT_INIT);
1056 }
1057
1058 /* Initialize keystore */
1059 boot_state = boot_verify_keystore_init();
1060 if(boot_state == YELLOW)
1061 {
1062 boot_verify_print_state();
1063 dprintf(CRITICAL, "Keystore verification failed! Continuing anyways...\n");
1064 }
1065}
1066
Shashank Mittal23b8f422010-04-16 19:27:21 -07001067int boot_linux_from_mmc(void)
1068{
1069 struct boot_img_hdr *hdr = (void*) buf;
1070 struct boot_img_hdr *uhdr;
1071 unsigned offset = 0;
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07001072 int rcode;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001073 unsigned long long ptn = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001074 int index = INVALID_PTN;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001075
Shashank Mittalcd98d472011-08-02 14:29:24 -07001076 unsigned char *image_addr = 0;
1077 unsigned kernel_actual;
1078 unsigned ramdisk_actual;
1079 unsigned imagesize_actual;
Neeti Desai465491e2012-07-31 12:53:35 -07001080 unsigned second_actual = 0;
Neeti Desai465491e2012-07-31 12:53:35 -07001081
Matthew Qin271927e2015-03-31 22:07:07 -04001082 unsigned int dtb_size = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08001083 unsigned int out_len = 0;
1084 unsigned int out_avai_len = 0;
1085 unsigned char *out_addr = NULL;
1086 uint32_t dtb_offset = 0;
1087 unsigned char *kernel_start_addr = NULL;
1088 unsigned int kernel_size = 0;
Ameya Thakur10a33452016-06-13 14:24:26 -07001089 unsigned int patched_kernel_hdr_size = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08001090 int rc;
Neeti Desai465491e2012-07-31 12:53:35 -07001091#if DEVICE_TREE
1092 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -07001093 struct dt_entry dt_entry;
Neeti Desai465491e2012-07-31 12:53:35 -07001094 unsigned dt_table_offset;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001095 uint32_t dt_actual;
Deepa Dinamani19648b42013-09-05 17:05:55 -07001096 uint32_t dt_hdr_size;
Matthew Qin271927e2015-03-31 22:07:07 -04001097 unsigned char *best_match_dt_addr = NULL;
Neeti Desai465491e2012-07-31 12:53:35 -07001098#endif
Matthew Qin49e51fa2015-02-09 10:40:45 +08001099 struct kernel64_hdr *kptr = NULL;
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001100
Maunik Shah0f3c8ac2014-03-02 17:47:58 +05301101 if (check_format_bit())
1102 boot_into_recovery = 1;
1103
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07001104 if (!boot_into_recovery) {
1105 memset(ffbm_mode_string, '\0', sizeof(ffbm_mode_string));
1106 rcode = get_ffbm(ffbm_mode_string, sizeof(ffbm_mode_string));
1107 if (rcode <= 0) {
1108 boot_into_ffbm = false;
1109 if (rcode < 0)
1110 dprintf(CRITICAL,"failed to get ffbm cookie");
1111 } else
1112 boot_into_ffbm = true;
1113 } else
1114 boot_into_ffbm = false;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001115 uhdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
1116 if (!memcmp(uhdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1117 dprintf(INFO, "Unified boot method!\n");
1118 hdr = uhdr;
1119 goto unified_boot;
1120 }
Greg Griscod6250552011-06-29 14:40:23 -07001121 if (!boot_into_recovery) {
Kinson Chikf1a43512011-07-14 11:28:39 -07001122 index = partition_get_index("boot");
1123 ptn = partition_get_offset(index);
1124 if(ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -07001125 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1126 return -1;
1127 }
Kinson Chikf1a43512011-07-14 11:28:39 -07001128 }
1129 else {
1130 index = partition_get_index("recovery");
1131 ptn = partition_get_offset(index);
1132 if(ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -07001133 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
1134 return -1;
1135 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001136 }
Channagoud Kadabief0547c2015-02-10 12:57:38 -08001137 /* Set Lun for boot & recovery partitions */
1138 mmc_set_lun(partition_get_lun(index));
Shashank Mittal23b8f422010-04-16 19:27:21 -07001139
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301140 if (mmc_read(ptn + offset, (uint32_t *) buf, page_size)) {
Shashank Mittal23b8f422010-04-16 19:27:21 -07001141 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
1142 return -1;
1143 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001144
1145 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001146 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Shashank Mittal23b8f422010-04-16 19:27:21 -07001147 return -1;
1148 }
1149
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07001150 if (hdr->page_size && (hdr->page_size != page_size)) {
vijay kumar2e21b3a2014-06-26 17:40:15 +05301151
1152 if (hdr->page_size > BOOT_IMG_MAX_PAGE_SIZE) {
1153 dprintf(CRITICAL, "ERROR: Invalid page size\n");
1154 return -1;
1155 }
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07001156 page_size = hdr->page_size;
1157 page_mask = page_size - 1;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001158 }
1159
vijay kumar287bb542015-09-29 13:01:52 +05301160 /* ensure commandline is terminated */
1161 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
1162
Matthew Qin49e51fa2015-02-09 10:40:45 +08001163 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1164 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001165
Matthew Qin49e51fa2015-02-09 10:40:45 +08001166 image_addr = (unsigned char *)target_get_scratch_address();
1167
1168#if DEVICE_TREE
1169 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
1170 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + dt_actual);
1171#else
1172 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
1173#endif
1174
1175#if VERIFIED_BOOT
1176 boot_verifier_init();
1177#endif
1178
Vijay Kumar Pendoti9c002ad2016-03-09 13:52:45 +05301179 if (check_aboot_addr_range_overlap((uintptr_t) image_addr, imagesize_actual))
Matthew Qin49e51fa2015-02-09 10:40:45 +08001180 {
1181 dprintf(CRITICAL, "Boot image buffer address overlaps with aboot addresses.\n");
1182 return -1;
1183 }
1184
Matthew Qinbb7923d2015-02-09 10:56:09 +08001185 /*
1186 * Update loading flow of bootimage to support compressed/uncompressed
1187 * bootimage on both 64bit and 32bit platform.
1188 * 1. Load bootimage from emmc partition onto DDR.
1189 * 2. Check if bootimage is gzip format. If yes, decompress compressed kernel
1190 * 3. Check kernel header and update kernel load addr for 64bit and 32bit
1191 * platform accordingly.
1192 * 4. Sanity Check on kernel_addr and ramdisk_addr and copy data.
1193 */
1194
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001195 dprintf(INFO, "Loading (%s) image (%d): start\n",
1196 (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001197 bs_set_timestamp(BS_KERNEL_LOAD_START);
1198
Gaurav Nebhwani43e2a462016-03-17 21:32:56 +05301199 if ((target_get_max_flash_size() - page_size) < imagesize_actual)
1200 {
1201 dprintf(CRITICAL, "booimage size is greater than DDR can hold\n");
1202 return -1;
1203 }
1204
Matthew Qinbb7923d2015-02-09 10:56:09 +08001205 /* Read image without signature */
Matthew Qin49e51fa2015-02-09 10:40:45 +08001206 if (mmc_read(ptn + offset, (void *)image_addr, imagesize_actual))
1207 {
1208 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
1209 return -1;
1210 }
1211
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001212 dprintf(INFO, "Loading (%s) image (%d): done\n",
1213 (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual);
1214
Matthew Qin49e51fa2015-02-09 10:40:45 +08001215 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
1216
1217 /* Authenticate Kernel */
1218 dprintf(INFO, "use_signed_kernel=%d, is_unlocked=%d, is_tampered=%d.\n",
1219 (int) target_use_signed_kernel(),
1220 device.is_unlocked,
1221 device.is_tampered);
1222
Channagoud Kadabi736c4962015-08-21 11:56:52 -07001223 /* Change the condition a little bit to include the test framework support.
1224 * We would never reach this point if device is in fastboot mode, even if we did
1225 * that means we are in test mode, so execute kernel authentication part for the
1226 * tests */
Parth Dixitcb0c6082015-12-30 15:01:13 +05301227 if((target_use_signed_kernel() && (!device.is_unlocked)) || is_test_mode_enabled())
Matthew Qin49e51fa2015-02-09 10:40:45 +08001228 {
1229 offset = imagesize_actual;
Vijay Kumar Pendoti9c002ad2016-03-09 13:52:45 +05301230 if (check_aboot_addr_range_overlap((uintptr_t)image_addr + offset, page_size))
Matthew Qin49e51fa2015-02-09 10:40:45 +08001231 {
1232 dprintf(CRITICAL, "Signature read buffer address overlaps with aboot addresses.\n");
1233 return -1;
1234 }
1235
1236 /* Read signature */
1237 if(mmc_read(ptn + offset, (void *)(image_addr + offset), page_size))
1238 {
1239 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
1240 return -1;
1241 }
1242
1243 verify_signed_bootimg((uint32_t)image_addr, imagesize_actual);
Channagoud Kadabi736c4962015-08-21 11:56:52 -07001244 /* The purpose of our test is done here */
Parth Dixitcb0c6082015-12-30 15:01:13 +05301245 if(is_test_mode_enabled() && auth_kernel_img)
Channagoud Kadabi736c4962015-08-21 11:56:52 -07001246 return 0;
Matthew Qin49e51fa2015-02-09 10:40:45 +08001247 } else {
1248 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
1249 #ifdef TZ_SAVE_KERNEL_HASH
1250 aboot_save_boot_hash_mmc((uint32_t) image_addr, imagesize_actual);
1251 #endif /* TZ_SAVE_KERNEL_HASH */
Amit Blay4aa292f2015-04-28 21:55:59 +03001252
1253#ifdef MDTP_SUPPORT
1254 {
1255 /* Verify MDTP lock.
1256 * For boot & recovery partitions, MDTP will use boot_verifier APIs,
1257 * since verification was skipped in aboot. The signature is not part of the loaded image.
1258 */
1259 mdtp_ext_partition_verification_t ext_partition;
1260 ext_partition.partition = boot_into_recovery ? MDTP_PARTITION_RECOVERY : MDTP_PARTITION_BOOT;
1261 ext_partition.integrity_state = MDTP_PARTITION_STATE_UNSET;
1262 ext_partition.page_size = page_size;
1263 ext_partition.image_addr = (uint32)image_addr;
1264 ext_partition.image_size = imagesize_actual;
1265 ext_partition.sig_avail = FALSE;
1266 mdtp_fwlock_verify_lock(&ext_partition);
1267 }
1268#endif /* MDTP_SUPPORT */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001269 }
1270
Sridhar Parasuram00bfedb2015-05-26 14:21:27 -07001271#if VERIFIED_BOOT
Reut Zysmanba8a9d52016-02-18 11:44:04 +02001272 if(boot_verify_get_state() == ORANGE)
1273 {
1274#if FBCON_DISPLAY_MSG
1275 display_bootverify_menu(DISPLAY_MENU_ORANGE);
1276 wait_for_users_action();
1277#else
1278 dprintf(CRITICAL,
1279 "Your device has been unlocked and can't be trusted.\nWait for 5 seconds before proceeding\n");
1280 mdelay(5000);
1281#endif
1282 }
1283#endif
1284
1285#if VERIFIED_BOOT
Parth Dixitbc9b6492015-10-18 00:41:38 +05301286#if !VBOOT_MOTA
Sridhar Parasuram00bfedb2015-05-26 14:21:27 -07001287 // send root of trust
Sridhar Parasuram96300dc2015-06-11 10:37:11 -07001288 if(!send_rot_command((uint32_t)device.is_unlocked))
Sridhar Parasuram00bfedb2015-05-26 14:21:27 -07001289 ASSERT(0);
1290#endif
Parth Dixitbc9b6492015-10-18 00:41:38 +05301291#endif
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001292 /*
Matthew Qinbb7923d2015-02-09 10:56:09 +08001293 * Check if the kernel image is a gzip package. If yes, need to decompress it.
1294 * If not, continue booting.
1295 */
1296 if (is_gzip_package((unsigned char *)(image_addr + page_size), hdr->kernel_size))
1297 {
1298 out_addr = (unsigned char *)(image_addr + imagesize_actual + page_size);
1299 out_avai_len = target_get_max_flash_size() - imagesize_actual - page_size;
Matthew Qin0b15b322015-05-19 05:20:54 -04001300 dprintf(INFO, "decompressing kernel image: start\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001301 rc = decompress((unsigned char *)(image_addr + page_size),
1302 hdr->kernel_size, out_addr, out_avai_len,
1303 &dtb_offset, &out_len);
1304 if (rc)
1305 {
Matthew Qin0b15b322015-05-19 05:20:54 -04001306 dprintf(CRITICAL, "decompressing kernel image failed!!!\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001307 ASSERT(0);
1308 }
1309
Matthew Qin0b15b322015-05-19 05:20:54 -04001310 dprintf(INFO, "decompressing kernel image: done\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001311 kptr = (struct kernel64_hdr *)out_addr;
1312 kernel_start_addr = out_addr;
1313 kernel_size = out_len;
1314 } else {
Ameya Thakur10a33452016-06-13 14:24:26 -07001315 dprintf(INFO, "Uncpmpressed kernel in use\n");
1316 if (!strncmp((char*)(image_addr + page_size),
1317 PATCHED_KERNEL_MAGIC,
1318 sizeof(PATCHED_KERNEL_MAGIC) - 1)) {
1319 dprintf(INFO, "Patched kernel detected\n");
1320 kptr = (struct kernel64_hdr *)(image_addr + page_size +
1321 PATCHED_KERNEL_HEADER_SIZE);
1322 //The size of the kernel is stored at start of kernel image + 16
1323 //The dtb would start just after the kernel
1324 dtb_offset = *((uint32_t*)((unsigned char*)
1325 (image_addr + page_size +
1326 sizeof(PATCHED_KERNEL_MAGIC) -
1327 1)));
1328 //The actual kernel starts after the 20 byte header.
1329 kernel_start_addr = (unsigned char*)(image_addr +
1330 page_size + PATCHED_KERNEL_HEADER_SIZE);
1331 kernel_size = hdr->kernel_size;
1332 patched_kernel_hdr_size = PATCHED_KERNEL_HEADER_SIZE;
1333 } else {
1334 dprintf(INFO, "Kernel image not patched..Unable to locate dt offset\n");
1335 kptr = (struct kernel64_hdr *)(image_addr + page_size);
1336 kernel_start_addr = (unsigned char *)(image_addr + page_size);
1337 kernel_size = hdr->kernel_size;
1338 }
Matthew Qinbb7923d2015-02-09 10:56:09 +08001339 }
1340
1341 /*
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001342 * Update the kernel/ramdisk/tags address if the boot image header
1343 * has default values, these default values come from mkbootimg when
1344 * the boot image is flashed using fastboot flash:raw
1345 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001346 update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr));
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001347
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001348 /* Get virtual addresses since the hdr saves physical addresses. */
1349 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
1350 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
1351 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001352
Matthew Qinbb7923d2015-02-09 10:56:09 +08001353 kernel_size = ROUND_TO_PAGE(kernel_size, page_mask);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001354 /* Check if the addresses in the header are valid. */
Matthew Qinbb7923d2015-02-09 10:56:09 +08001355 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_size) ||
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001356 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
1357 {
1358 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
1359 return -1;
1360 }
1361
1362#ifndef DEVICE_TREE
1363 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1364 {
1365 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
1366 return -1;
1367 }
1368#endif
1369
Matthew Qin49e51fa2015-02-09 10:40:45 +08001370 /* Move kernel, ramdisk and device tree to correct address */
Matthew Qinbb7923d2015-02-09 10:56:09 +08001371 memmove((void*) hdr->kernel_addr, kernel_start_addr, kernel_size);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001372 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001373
Matthew Qin49e51fa2015-02-09 10:40:45 +08001374 #if DEVICE_TREE
1375 if(hdr->dt_size) {
1376 dt_table_offset = ((uint32_t)image_addr + page_size + kernel_actual + ramdisk_actual + second_actual);
1377 table = (struct dt_table*) dt_table_offset;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001378
Matthew Qin49e51fa2015-02-09 10:40:45 +08001379 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
1380 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1381 return -1;
1382 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001383
P.V. Phani Kumar82916762016-03-09 09:20:19 +05301384 /* Its Error if, dt_hdr_size (table->num_entries * dt_entry size + Dev_Tree Header)
1385 goes beyound hdr->dt_size*/
1386 if (dt_hdr_size > ROUND_TO_PAGE(hdr->dt_size,hdr->page_size)) {
1387 dprintf(CRITICAL, "ERROR: Invalid Device Tree size \n");
1388 return -1;
1389 }
1390
Matthew Qin49e51fa2015-02-09 10:40:45 +08001391 /* Find index of device tree within device tree table */
1392 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
1393 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1394 return -1;
1395 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001396
P.V. Phani Kumar3c333202016-03-09 11:54:37 +05301397 if(dt_entry.offset > (UINT_MAX - dt_entry.size)) {
1398 dprintf(CRITICAL, "ERROR: Device tree contents are Invalid\n");
1399 return -1;
1400 }
1401
1402 /* Ensure we are not overshooting dt_size with the dt_entry selected */
1403 if ((dt_entry.offset + dt_entry.size) > hdr->dt_size) {
1404 dprintf(CRITICAL, "ERROR: Device tree contents are Invalid\n");
1405 return -1;
1406 }
1407
Matthew Qin271927e2015-03-31 22:07:07 -04001408 if (is_gzip_package((unsigned char *)dt_table_offset + dt_entry.offset, dt_entry.size))
1409 {
1410 unsigned int compressed_size = 0;
1411 out_addr += out_len;
1412 out_avai_len -= out_len;
Matthew Qin0b15b322015-05-19 05:20:54 -04001413 dprintf(INFO, "decompressing dtb: start\n");
Matthew Qin271927e2015-03-31 22:07:07 -04001414 rc = decompress((unsigned char *)dt_table_offset + dt_entry.offset,
1415 dt_entry.size, out_addr, out_avai_len,
1416 &compressed_size, &dtb_size);
1417 if (rc)
1418 {
Matthew Qin0b15b322015-05-19 05:20:54 -04001419 dprintf(CRITICAL, "decompressing dtb failed!!!\n");
Matthew Qin271927e2015-03-31 22:07:07 -04001420 ASSERT(0);
1421 }
1422
Matthew Qin0b15b322015-05-19 05:20:54 -04001423 dprintf(INFO, "decompressing dtb: done\n");
Matthew Qin271927e2015-03-31 22:07:07 -04001424 best_match_dt_addr = out_addr;
1425 } else {
1426 best_match_dt_addr = (unsigned char *)dt_table_offset + dt_entry.offset;
1427 dtb_size = dt_entry.size;
1428 }
1429
Matthew Qin49e51fa2015-02-09 10:40:45 +08001430 /* Validate and Read device device tree in the tags_addr */
Matthew Qin271927e2015-03-31 22:07:07 -04001431 if (check_aboot_addr_range_overlap(hdr->tags_addr, dtb_size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001432 {
1433 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1434 return -1;
1435 }
Shashank Mittal162244e2011-08-08 19:01:25 -07001436
Matthew Qin271927e2015-03-31 22:07:07 -04001437 memmove((void *)hdr->tags_addr, (char *)best_match_dt_addr, dtb_size);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001438 } else {
1439 /* Validate the tags_addr */
1440 if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual))
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001441 {
1442 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1443 return -1;
1444 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08001445 /*
1446 * If appended dev tree is found, update the atags with
1447 * memory address to the DTB appended location on RAM.
1448 * Else update with the atags address in the kernel header
1449 */
1450 void *dtb;
Ameya Thakur10a33452016-06-13 14:24:26 -07001451 dtb = dev_tree_appended(
1452 (void*)(image_addr + page_size +
1453 patched_kernel_hdr_size),
1454 hdr->kernel_size, dtb_offset,
1455 (void *)hdr->tags_addr);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001456 if (!dtb) {
1457 dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001458 return -1;
1459 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07001460 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08001461 #endif
Shashank Mittal23b8f422010-04-16 19:27:21 -07001462
Stanimir Varbanov69ec5462013-07-18 18:17:42 +03001463 if (boot_into_recovery && !device.is_unlocked && !device.is_tampered)
1464 target_load_ssd_keystore();
1465
Shashank Mittal23b8f422010-04-16 19:27:21 -07001466unified_boot:
Shashank Mittal23b8f422010-04-16 19:27:21 -07001467
Dima Zavin77e41f32013-03-06 16:10:43 -08001468 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07001469 (const char *)hdr->cmdline, board_machtype(),
Shashank Mittal23b8f422010-04-16 19:27:21 -07001470 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
1471
1472 return 0;
1473}
1474
Dima Zavin214cc642009-01-26 11:16:21 -08001475int boot_linux_from_flash(void)
1476{
1477 struct boot_img_hdr *hdr = (void*) buf;
Dima Zavin214cc642009-01-26 11:16:21 -08001478 struct ptentry *ptn;
1479 struct ptable *ptable;
1480 unsigned offset = 0;
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001481
Shashank Mittalcd98d472011-08-02 14:29:24 -07001482 unsigned char *image_addr = 0;
1483 unsigned kernel_actual;
1484 unsigned ramdisk_actual;
1485 unsigned imagesize_actual;
vijay kumar8f53e362015-11-24 13:38:11 +05301486 unsigned second_actual = 0;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001487
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001488#if DEVICE_TREE
1489 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -07001490 struct dt_entry dt_entry;
vijay kumar8f53e362015-11-24 13:38:11 +05301491 unsigned dt_table_offset;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001492 uint32_t dt_actual;
Deepa Dinamani19648b42013-09-05 17:05:55 -07001493 uint32_t dt_hdr_size;
vijay kumar8f53e362015-11-24 13:38:11 +05301494 unsigned int dtb_size = 0;
1495 unsigned char *best_match_dt_addr = NULL;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001496#endif
1497
David Ng183a7422009-12-07 14:55:21 -08001498 if (target_is_emmc_boot()) {
1499 hdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
1500 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1501 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
1502 return -1;
1503 }
1504 goto continue_boot;
1505 }
1506
Dima Zavin214cc642009-01-26 11:16:21 -08001507 ptable = flash_get_ptable();
1508 if (ptable == NULL) {
1509 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1510 return -1;
1511 }
1512
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001513 if(!boot_into_recovery)
1514 {
1515 ptn = ptable_find(ptable, "boot");
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001516
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001517 if (ptn == NULL) {
1518 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1519 return -1;
1520 }
1521 }
1522 else
1523 {
1524 ptn = ptable_find(ptable, "recovery");
1525 if (ptn == NULL) {
1526 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
1527 return -1;
1528 }
Dima Zavin214cc642009-01-26 11:16:21 -08001529 }
1530
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001531 if (flash_read(ptn, offset, buf, page_size)) {
Dima Zavin214cc642009-01-26 11:16:21 -08001532 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
1533 return -1;
1534 }
Dima Zavin214cc642009-01-26 11:16:21 -08001535
1536 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001537 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Dima Zavin214cc642009-01-26 11:16:21 -08001538 return -1;
1539 }
1540
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001541 if (hdr->page_size != page_size) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001542 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 -08001543 return -1;
1544 }
1545
vijay kumar287bb542015-09-29 13:01:52 +05301546 /* ensure commandline is terminated */
1547 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
1548
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001549 /*
1550 * Update the kernel/ramdisk/tags address if the boot image header
1551 * has default values, these default values come from mkbootimg when
1552 * the boot image is flashed using fastboot flash:raw
1553 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001554 update_ker_tags_rdisk_addr(hdr, false);
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001555
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001556 /* Get virtual addresses since the hdr saves physical addresses. */
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001557 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
1558 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
1559 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
1560
1561 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1562 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1563
1564 /* Check if the addresses in the header are valid. */
1565 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
1566 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
1567 {
1568 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
1569 return -1;
1570 }
1571
1572#ifndef DEVICE_TREE
1573 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1574 {
1575 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
1576 return -1;
1577 }
1578#endif
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001579
Shashank Mittalcd98d472011-08-02 14:29:24 -07001580 /* Authenticate Kernel */
Deepa Dinamani23b60d42013-06-24 18:10:52 -07001581 if(target_use_signed_kernel() && (!device.is_unlocked))
Shashank Mittalcd98d472011-08-02 14:29:24 -07001582 {
1583 image_addr = (unsigned char *)target_get_scratch_address();
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001584 offset = 0;
1585
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001586#if DEVICE_TREE
1587 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
vijay kumar966a9822015-12-09 18:36:09 +05301588
1589 if (UINT_MAX < ((uint64_t)kernel_actual + (uint64_t)ramdisk_actual+ (uint64_t)dt_actual + page_size)) {
1590 dprintf(CRITICAL, "Integer overflow detected in bootimage header fields\n");
1591 return -1;
1592 }
1593
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001594 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + dt_actual);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001595
1596 if (check_aboot_addr_range_overlap(hdr->tags_addr, hdr->dt_size))
1597 {
1598 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1599 return -1;
1600 }
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001601#else
vijay kumar966a9822015-12-09 18:36:09 +05301602 if (UINT_MAX < ((uint64_t)kernel_actual + (uint64_t)ramdisk_actual+ page_size)) {
1603 dprintf(CRITICAL, "Integer overflow detected in bootimage header fields\n");
1604 return -1;
1605 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07001606 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001607#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001608
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001609 dprintf(INFO, "Loading (%s) image (%d): start\n",
1610 (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001611 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -08001612
Shashank Mittalcd98d472011-08-02 14:29:24 -07001613 /* Read image without signature */
1614 if (flash_read(ptn, offset, (void *)image_addr, imagesize_actual))
1615 {
1616 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
1617 return -1;
1618 }
Dima Zavin214cc642009-01-26 11:16:21 -08001619
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001620 dprintf(INFO, "Loading (%s) image (%d): done\n",
1621 (!boot_into_recovery ? "boot" : "recovery"), imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001622 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Amol Jadib6be5c12012-11-14 13:39:51 -08001623
Shashank Mittalcd98d472011-08-02 14:29:24 -07001624 offset = imagesize_actual;
1625 /* Read signature */
1626 if (flash_read(ptn, offset, (void *)(image_addr + offset), page_size))
1627 {
1628 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001629 return -1;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001630 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07001631
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301632 verify_signed_bootimg((uint32_t)image_addr, imagesize_actual);
Shashank Mittalcd98d472011-08-02 14:29:24 -07001633
1634 /* Move kernel and ramdisk to correct address */
Matthew Qinbb7923d2015-02-09 10:56:09 +08001635 memmove((void*) hdr->kernel_addr, (char*) (image_addr + page_size), hdr->kernel_size);
1636 memmove((void*) hdr->ramdisk_addr, (char*) (image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001637#if DEVICE_TREE
vijay kumar8f53e362015-11-24 13:38:11 +05301638 if(hdr->dt_size != 0) {
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001639
vijay kumar8f53e362015-11-24 13:38:11 +05301640 dt_table_offset = ((uint32_t)image_addr + page_size + kernel_actual + ramdisk_actual + second_actual);
1641
1642 table = (struct dt_table*) dt_table_offset;
1643
1644 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0){
1645 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1646 return -1;
1647 }
1648
1649 /* Find index of device tree within device tree table */
1650 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
1651 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1652 return -1;
1653 }
1654
1655 /* Validate and Read device device tree in the "tags_add */
1656 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size)){
1657 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1658 return -1;
1659 }
1660
1661 best_match_dt_addr = (unsigned char *)table + dt_entry.offset;
1662 dtb_size = dt_entry.size;
1663 memmove((void *)hdr->tags_addr, (char *)best_match_dt_addr, dtb_size);
1664 }
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001665#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001666
1667 /* Make sure everything from scratch address is read before next step!*/
Shashank Mittala0032282011-08-26 14:50:11 -07001668 if(device.is_tampered)
Shashank Mittal162244e2011-08-08 19:01:25 -07001669 {
1670 write_device_info_flash(&device);
1671 }
Channagoud Kadabi5c86fe32012-02-16 10:58:48 +05301672#if USE_PCOM_SECBOOT
1673 set_tamper_flag(device.is_tampered);
1674#endif
Shashank Mittalcd98d472011-08-02 14:29:24 -07001675 }
1676 else
1677 {
Shashank Mittal162244e2011-08-08 19:01:25 -07001678 offset = page_size;
1679
Amol Jadib6be5c12012-11-14 13:39:51 -08001680 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1681 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1682 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
1683
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001684 dprintf(INFO, "Loading (%s) image (%d): start\n",
1685 (!boot_into_recovery ? "boot" : "recovery"), kernel_actual + ramdisk_actual);
1686
Amol Jadi492d5a52013-03-15 16:12:34 -07001687 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -08001688
1689 if (flash_read(ptn, offset, (void *)hdr->kernel_addr, kernel_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -07001690 dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
1691 return -1;
1692 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001693 offset += kernel_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001694
Amol Jadib6be5c12012-11-14 13:39:51 -08001695 if (flash_read(ptn, offset, (void *)hdr->ramdisk_addr, ramdisk_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -07001696 dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
1697 return -1;
1698 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001699 offset += ramdisk_actual;
1700
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001701 dprintf(INFO, "Loading (%s) image (%d): done\n",
1702 (!boot_into_recovery ? "boot" : "recovery"), kernel_actual + ramdisk_actual);
1703
Amol Jadi492d5a52013-03-15 16:12:34 -07001704 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001705
1706 if(hdr->second_size != 0) {
Amol Jadib6be5c12012-11-14 13:39:51 -08001707 offset += second_actual;
1708 /* Second image loading not implemented. */
1709 ASSERT(0);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001710 }
1711
1712#if DEVICE_TREE
1713 if(hdr->dt_size != 0) {
1714
1715 /* Read the device tree table into buffer */
1716 if(flash_read(ptn, offset, (void *) dt_buf, page_size)) {
1717 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
1718 return -1;
1719 }
1720
1721 table = (struct dt_table*) dt_buf;
1722
Deepa Dinamani19648b42013-09-05 17:05:55 -07001723 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001724 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1725 return -1;
1726 }
1727
P.V. Phani Kumar82916762016-03-09 09:20:19 +05301728 /* Its Error if, dt_hdr_size (table->num_entries * dt_entry size + Dev_Tree Header)
1729 goes beyound hdr->dt_size*/
1730 if (dt_hdr_size > ROUND_TO_PAGE(hdr->dt_size,hdr->page_size)) {
1731 dprintf(CRITICAL, "ERROR: Invalid Device Tree size \n");
1732 return -1;
1733 }
1734
Deepa Dinamani19648b42013-09-05 17:05:55 -07001735 table = (struct dt_table*) memalign(CACHE_LINE, dt_hdr_size);
1736 if (!table)
1737 return -1;
1738
1739 /* Read the entire device tree table into buffer */
1740 if(flash_read(ptn, offset, (void *)table, dt_hdr_size)) {
1741 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
1742 return -1;
1743 }
1744
1745
Joel Kingaa335dc2013-06-03 16:11:08 -07001746 /* Find index of device tree within device tree table */
1747 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001748 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1749 return -1;
1750 }
1751
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001752 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001753 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001754 {
1755 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1756 return -1;
1757 }
1758
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001759 /* Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001760 if(flash_read(ptn, offset + dt_entry.offset,
1761 (void *)hdr->tags_addr, dt_entry.size)) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001762 dprintf(CRITICAL, "ERROR: Cannot read device tree\n");
1763 return -1;
1764 }
1765 }
1766#endif
1767
Shashank Mittalcd98d472011-08-02 14:29:24 -07001768 }
David Ng183a7422009-12-07 14:55:21 -08001769continue_boot:
Dima Zavin214cc642009-01-26 11:16:21 -08001770
Dima Zavin214cc642009-01-26 11:16:21 -08001771 /* TODO: create/pass atags to kernel */
1772
Ajay Dudanie28a6072011-07-01 13:59:46 -07001773 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07001774 (const char *)hdr->cmdline, board_machtype(),
Dima Zavin214cc642009-01-26 11:16:21 -08001775 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
1776
1777 return 0;
1778}
Brian Swetland9c4c0752009-01-25 16:23:50 -08001779
Shashank Mittal162244e2011-08-08 19:01:25 -07001780void write_device_info_mmc(device_info *dev)
1781{
Shashank Mittal162244e2011-08-08 19:01:25 -07001782 unsigned long long ptn = 0;
1783 unsigned long long size;
1784 int index = INVALID_PTN;
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001785 uint32_t blocksize;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001786 uint8_t lun = 0;
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001787 uint32_t ret = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001788
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001789 if (devinfo_present)
1790 index = partition_get_index("devinfo");
1791 else
1792 index = partition_get_index("aboot");
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001793
Shashank Mittal162244e2011-08-08 19:01:25 -07001794 ptn = partition_get_offset(index);
1795 if(ptn == 0)
1796 {
1797 return;
1798 }
1799
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001800 lun = partition_get_lun(index);
1801 mmc_set_lun(lun);
1802
Shashank Mittal162244e2011-08-08 19:01:25 -07001803 size = partition_get_size(index);
1804
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001805 blocksize = mmc_get_device_blocksize();
1806
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001807 if (devinfo_present)
1808 ret = mmc_write(ptn, blocksize, (void *)info_buf);
1809 else
1810 ret = mmc_write((ptn + size - blocksize), blocksize, (void *)info_buf);
1811 if (ret)
Shashank Mittal162244e2011-08-08 19:01:25 -07001812 {
1813 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
Sridhar Parasurame1a97922015-07-27 15:31:22 -07001814 ASSERT(0);
Shashank Mittal162244e2011-08-08 19:01:25 -07001815 }
1816}
1817
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001818void read_device_info_mmc(struct device_info *info)
Shashank Mittal162244e2011-08-08 19:01:25 -07001819{
Shashank Mittal162244e2011-08-08 19:01:25 -07001820 unsigned long long ptn = 0;
1821 unsigned long long size;
1822 int index = INVALID_PTN;
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001823 uint32_t blocksize;
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001824 uint32_t ret = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001825
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001826 if ((index = partition_get_index("devinfo")) < 0)
1827 {
1828 devinfo_present = false;
1829 index = partition_get_index("aboot");
1830 }
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001831
Shashank Mittal162244e2011-08-08 19:01:25 -07001832 ptn = partition_get_offset(index);
1833 if(ptn == 0)
1834 {
1835 return;
1836 }
1837
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001838 mmc_set_lun(partition_get_lun(index));
1839
Shashank Mittal162244e2011-08-08 19:01:25 -07001840 size = partition_get_size(index);
1841
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001842 blocksize = mmc_get_device_blocksize();
1843
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001844 if (devinfo_present)
1845 ret = mmc_read(ptn, (void *)info_buf, blocksize);
1846 else
1847 ret = mmc_read((ptn + size - blocksize), (void *)info_buf, blocksize);
1848 if (ret)
Shashank Mittal162244e2011-08-08 19:01:25 -07001849 {
1850 dprintf(CRITICAL, "ERROR: Cannot read device info\n");
Sridhar Parasurame1a97922015-07-27 15:31:22 -07001851 ASSERT(0);
Shashank Mittal162244e2011-08-08 19:01:25 -07001852 }
Shashank Mittal162244e2011-08-08 19:01:25 -07001853}
1854
1855void write_device_info_flash(device_info *dev)
1856{
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001857 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
Shashank Mittal162244e2011-08-08 19:01:25 -07001858 struct ptentry *ptn;
1859 struct ptable *ptable;
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001860 if(info == NULL)
1861 {
1862 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
1863 ASSERT(0);
1864 }
1865 info_buf = info;
Shashank Mittal162244e2011-08-08 19:01:25 -07001866 ptable = flash_get_ptable();
1867 if (ptable == NULL)
1868 {
1869 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1870 return;
1871 }
1872
1873 ptn = ptable_find(ptable, "devinfo");
1874 if (ptn == NULL)
1875 {
Smita Ghosh670c8b82015-05-07 09:30:03 -07001876 dprintf(CRITICAL, "ERROR: No devinfo partition found\n");
Shashank Mittal162244e2011-08-08 19:01:25 -07001877 return;
1878 }
1879
1880 memcpy(info, dev, sizeof(device_info));
1881
1882 if (flash_write(ptn, 0, (void *)info_buf, page_size))
1883 {
1884 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1885 return;
1886 }
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001887 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07001888}
1889
vijay kumarc65876c2015-04-24 13:29:16 +05301890static int read_allow_oem_unlock(device_info *dev)
1891{
vijay kumarc65876c2015-04-24 13:29:16 +05301892 unsigned offset;
1893 int index;
1894 unsigned long long ptn;
1895 unsigned long long ptn_size;
1896 unsigned blocksize = mmc_get_device_blocksize();
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07001897 STACKBUF_DMA_ALIGN(buf, blocksize);
vijay kumarc65876c2015-04-24 13:29:16 +05301898
vijay kumarca2e6812015-07-08 20:28:25 +05301899 index = partition_get_index(frp_ptns[0]);
vijay kumarc65876c2015-04-24 13:29:16 +05301900 if (index == INVALID_PTN)
1901 {
vijay kumarca2e6812015-07-08 20:28:25 +05301902 index = partition_get_index(frp_ptns[1]);
1903 if (index == INVALID_PTN)
1904 {
1905 dprintf(CRITICAL, "Neither '%s' nor '%s' partition found\n", frp_ptns[0],frp_ptns[1]);
1906 return -1;
1907 }
vijay kumarc65876c2015-04-24 13:29:16 +05301908 }
1909
1910 ptn = partition_get_offset(index);
1911 ptn_size = partition_get_size(index);
1912 offset = ptn_size - blocksize;
1913
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07001914 if (mmc_read(ptn + offset, (void *)buf, blocksize))
vijay kumarc65876c2015-04-24 13:29:16 +05301915 {
1916 dprintf(CRITICAL, "Reading MMC failed\n");
1917 return -1;
1918 }
1919
1920 /*is_allow_unlock is a bool value stored at the LSB of last byte*/
1921 is_allow_unlock = buf[blocksize-1] & 0x01;
1922 return 0;
1923}
1924
1925static int write_allow_oem_unlock(bool allow_unlock)
1926{
vijay kumarc65876c2015-04-24 13:29:16 +05301927 unsigned offset;
vijay kumarc65876c2015-04-24 13:29:16 +05301928 int index;
1929 unsigned long long ptn;
1930 unsigned long long ptn_size;
1931 unsigned blocksize = mmc_get_device_blocksize();
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07001932 STACKBUF_DMA_ALIGN(buf, blocksize);
vijay kumarc65876c2015-04-24 13:29:16 +05301933
vijay kumarca2e6812015-07-08 20:28:25 +05301934 index = partition_get_index(frp_ptns[0]);
vijay kumarc65876c2015-04-24 13:29:16 +05301935 if (index == INVALID_PTN)
1936 {
vijay kumarca2e6812015-07-08 20:28:25 +05301937 index = partition_get_index(frp_ptns[1]);
1938 if (index == INVALID_PTN)
1939 {
1940 dprintf(CRITICAL, "Neither '%s' nor '%s' partition found\n", frp_ptns[0],frp_ptns[1]);
1941 return -1;
1942 }
vijay kumarc65876c2015-04-24 13:29:16 +05301943 }
1944
1945 ptn = partition_get_offset(index);
1946 ptn_size = partition_get_size(index);
1947 offset = ptn_size - blocksize;
1948
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07001949 if (mmc_read(ptn + offset, (void *)buf, blocksize))
vijay kumarc65876c2015-04-24 13:29:16 +05301950 {
1951 dprintf(CRITICAL, "Reading MMC failed\n");
1952 return -1;
1953 }
1954
1955 /*is_allow_unlock is a bool value stored at the LSB of last byte*/
1956 buf[blocksize-1] = allow_unlock;
1957 if (mmc_write(ptn + offset, blocksize, buf))
1958 {
1959 dprintf(CRITICAL, "Writing MMC failed\n");
1960 return -1;
1961 }
1962
1963 return 0;
1964}
1965
Shashank Mittal162244e2011-08-08 19:01:25 -07001966void read_device_info_flash(device_info *dev)
1967{
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001968 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
Shashank Mittal162244e2011-08-08 19:01:25 -07001969 struct ptentry *ptn;
1970 struct ptable *ptable;
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001971 if(info == NULL)
1972 {
1973 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
1974 ASSERT(0);
1975 }
1976 info_buf = info;
Shashank Mittal162244e2011-08-08 19:01:25 -07001977 ptable = flash_get_ptable();
1978 if (ptable == NULL)
1979 {
1980 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1981 return;
1982 }
1983
1984 ptn = ptable_find(ptable, "devinfo");
1985 if (ptn == NULL)
1986 {
Smita Ghosh670c8b82015-05-07 09:30:03 -07001987 dprintf(CRITICAL, "ERROR: No devinfo partition found\n");
Shashank Mittal162244e2011-08-08 19:01:25 -07001988 return;
1989 }
1990
1991 if (flash_read(ptn, 0, (void *)info_buf, page_size))
1992 {
1993 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1994 return;
1995 }
1996
1997 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
1998 {
Shashank Mittal162244e2011-08-08 19:01:25 -07001999 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
2000 info->is_unlocked = 0;
Shashank Mittala0032282011-08-26 14:50:11 -07002001 info->is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07002002 write_device_info_flash(info);
2003 }
2004 memcpy(dev, info, sizeof(device_info));
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002005 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07002006}
2007
2008void write_device_info(device_info *dev)
2009{
2010 if(target_is_emmc_boot())
2011 {
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002012 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
2013 if(info == NULL)
2014 {
2015 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
2016 ASSERT(0);
2017 }
2018 info_buf = info;
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002019 memcpy(info, dev, sizeof(struct device_info));
2020
2021#if USE_RPMB_FOR_DEVINFO
Sridhar Parasurame1a97922015-07-27 15:31:22 -07002022 if (is_secure_boot_enable()) {
2023 if((write_device_info_rpmb((void*) info, PAGE_SIZE)) < 0)
2024 ASSERT(0);
2025 }
Channagoud Kadabic80cb492015-04-28 16:08:28 -07002026 else
2027 write_device_info_mmc(info);
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002028#else
2029 write_device_info_mmc(info);
2030#endif
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002031 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07002032 }
2033 else
2034 {
2035 write_device_info_flash(dev);
2036 }
2037}
2038
2039void read_device_info(device_info *dev)
2040{
2041 if(target_is_emmc_boot())
2042 {
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002043 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
2044 if(info == NULL)
2045 {
2046 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
2047 ASSERT(0);
2048 }
2049 info_buf = info;
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002050
2051#if USE_RPMB_FOR_DEVINFO
Sridhar Parasurame1a97922015-07-27 15:31:22 -07002052 if (is_secure_boot_enable()) {
2053 if((read_device_info_rpmb((void*) info, PAGE_SIZE)) < 0)
2054 ASSERT(0);
2055 }
Channagoud Kadabic80cb492015-04-28 16:08:28 -07002056 else
2057 read_device_info_mmc(info);
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002058#else
2059 read_device_info_mmc(info);
2060#endif
2061
2062 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
2063 {
2064 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
lijuang511a2b52015-08-14 20:50:51 +08002065 if (is_secure_boot_enable()) {
Channagoud Kadabi05f78ba2015-07-06 11:58:14 -07002066 info->is_unlocked = 0;
Parth Dixitddbc7352015-10-18 03:13:31 +05302067#if !VBOOT_MOTA
lijuang511a2b52015-08-14 20:50:51 +08002068 info->is_unlock_critical = 0;
Parth Dixitddbc7352015-10-18 03:13:31 +05302069#endif
lijuang511a2b52015-08-14 20:50:51 +08002070 } else {
Channagoud Kadabi2fda4092015-07-07 13:34:11 -07002071 info->is_unlocked = 1;
Parth Dixitddbc7352015-10-18 03:13:31 +05302072#if !VBOOT_MOTA
lijuang511a2b52015-08-14 20:50:51 +08002073 info->is_unlock_critical = 1;
Parth Dixitddbc7352015-10-18 03:13:31 +05302074#endif
lijuang511a2b52015-08-14 20:50:51 +08002075 }
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002076 info->is_tampered = 0;
2077 info->charger_screen_enabled = 0;
Parth Dixitddbc7352015-10-18 03:13:31 +05302078#if !VBOOT_MOTA
Sridhar Parasuram287e5062015-08-24 16:17:22 -07002079 info->verity_mode = 1; //enforcing by default
Parth Dixitddbc7352015-10-18 03:13:31 +05302080#endif
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002081 write_device_info(info);
2082 }
2083 memcpy(dev, info, sizeof(device_info));
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002084 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07002085 }
2086 else
2087 {
2088 read_device_info_flash(dev);
2089 }
2090}
2091
2092void reset_device_info()
2093{
2094 dprintf(ALWAYS, "reset_device_info called.");
Shashank Mittala0032282011-08-26 14:50:11 -07002095 device.is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07002096 write_device_info(&device);
2097}
2098
2099void set_device_root()
2100{
2101 dprintf(ALWAYS, "set_device_root called.");
Shashank Mittala0032282011-08-26 14:50:11 -07002102 device.is_tampered = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -07002103 write_device_info(&device);
2104}
2105
lijuang4ece1e72015-08-14 21:02:36 +08002106/* set device unlock value
2107 * Must check FRP before call this function
2108 * Need to wipe data when unlock status changed
2109 * type 0: oem unlock
2110 * type 1: unlock critical
2111 * status 0: unlock as false
2112 * status 1: lock as true
2113 */
2114void set_device_unlock_value(int type, bool status)
lijuang21f12f52015-08-22 16:22:19 +08002115{
lijuang4ece1e72015-08-14 21:02:36 +08002116 if (type == UNLOCK)
2117 device.is_unlocked = status;
Parth Dixitddbc7352015-10-18 03:13:31 +05302118#if !VBOOT_MOTA
lijuang4ece1e72015-08-14 21:02:36 +08002119 else if (type == UNLOCK_CRITICAL)
2120 device.is_unlock_critical = status;
Parth Dixitddbc7352015-10-18 03:13:31 +05302121#endif
lijuang4ece1e72015-08-14 21:02:36 +08002122 write_device_info(&device);
2123}
2124
2125static void set_device_unlock(int type, bool status)
2126{
2127 int is_unlocked = -1;
2128 char response[MAX_RSP_SIZE];
2129
2130 /* check device unlock status if it is as expected */
2131 if (type == UNLOCK)
2132 is_unlocked = device.is_unlocked;
Parth Dixitddbc7352015-10-18 03:13:31 +05302133#if !VBOOT_MOTA
lijuang4ece1e72015-08-14 21:02:36 +08002134 else if (type == UNLOCK_CRITICAL)
2135 is_unlocked = device.is_unlock_critical;
Parth Dixitddbc7352015-10-18 03:13:31 +05302136#endif
lijuang4ece1e72015-08-14 21:02:36 +08002137 if (is_unlocked == status) {
2138 snprintf(response, sizeof(response), "\tDevice already : %s", (status ? "unlocked!" : "locked!"));
2139 fastboot_info(response);
2140 fastboot_okay("");
2141 return;
2142 }
2143
2144 /* status is true, it means to unlock device */
2145 if (status) {
lijuang21f12f52015-08-22 16:22:19 +08002146 if(!is_allow_unlock) {
2147 fastboot_fail("oem unlock is not allowed");
2148 return;
2149 }
2150
lijuang4ece1e72015-08-14 21:02:36 +08002151#if FBCON_DISPLAY_MSG
lijuang9a7d3b92015-11-30 14:41:24 +08002152 display_unlock_menu(type);
lijuang4ece1e72015-08-14 21:02:36 +08002153 fastboot_okay("");
2154 return;
2155#else
2156 if (type == UNLOCK) {
2157 fastboot_fail("Need wipe userdata. Do 'fastboot oem unlock-go'");
2158 return;
2159 }
2160#endif
lijuang21f12f52015-08-22 16:22:19 +08002161 }
lijuang4ece1e72015-08-14 21:02:36 +08002162
2163 set_device_unlock_value(type, status);
2164
2165 /* wipe data */
2166 struct recovery_message msg;
2167
2168 snprintf(msg.recovery, sizeof(msg.recovery), "recovery\n--wipe_data");
2169 write_misc(0, &msg, sizeof(msg));
2170
2171 fastboot_okay("");
2172 reboot_device(RECOVERY_MODE);
lijuang21f12f52015-08-22 16:22:19 +08002173}
2174
lijuang511a2b52015-08-14 20:50:51 +08002175static bool critical_flash_allowed(const char * entry)
2176{
2177 uint32_t i = 0;
2178 if (entry == NULL)
2179 return false;
2180
2181 for (i = 0; i < ARRAY_SIZE(critical_flash_allowed_ptn); i++) {
2182 if(!strcmp(entry, critical_flash_allowed_ptn[i]))
2183 return true;
2184 }
2185 return false;
Matthew Qin271927e2015-03-31 22:07:07 -04002186}
2187
2188#if DEVICE_TREE
Amol Jadicb524072012-08-09 16:40:18 -07002189int copy_dtb(uint8_t *boot_image_start, unsigned int scratch_offset)
2190{
2191 uint32 dt_image_offset = 0;
Amol Jadicb524072012-08-09 16:40:18 -07002192 uint32_t n;
Amol Jadicb524072012-08-09 16:40:18 -07002193 struct dt_table *table;
2194 struct dt_entry dt_entry;
2195 uint32_t dt_hdr_size;
2196 unsigned int compressed_size = 0;
2197 unsigned int dtb_size = 0;
2198 unsigned int out_avai_len = 0;
2199 unsigned char *out_addr = NULL;
2200 unsigned char *best_match_dt_addr = NULL;
2201 int rc;
2202
2203 struct boot_img_hdr *hdr = (struct boot_img_hdr *) (boot_image_start);
2204
2205 if(hdr->dt_size != 0) {
2206 /* add kernel offset */
2207 dt_image_offset += page_size;
2208 n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07002209 dt_image_offset += n;
Amol Jadicb524072012-08-09 16:40:18 -07002210
Deepa Dinamani19648b42013-09-05 17:05:55 -07002211 /* add ramdisk offset */
Amol Jadicb524072012-08-09 16:40:18 -07002212 n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
2213 dt_image_offset += n;
2214
Joel Kingaa335dc2013-06-03 16:11:08 -07002215 /* add second offset */
2216 if(hdr->second_size != 0) {
Amol Jadicb524072012-08-09 16:40:18 -07002217 n = ROUND_TO_PAGE(hdr->second_size, page_mask);
2218 dt_image_offset += n;
2219 }
2220
Matthew Qin271927e2015-03-31 22:07:07 -04002221 /* offset now point to start of dt.img */
2222 table = (struct dt_table*)(boot_image_start + dt_image_offset);
2223
2224 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
2225 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
Matthew Qin0b15b322015-05-19 05:20:54 -04002226 return -1;
Matthew Qin271927e2015-03-31 22:07:07 -04002227 }
P.V. Phani Kumar82916762016-03-09 09:20:19 +05302228
2229 /* Its Error if, dt_hdr_size (table->num_entries * dt_entry size + Dev_Tree Header)
2230 goes beyound hdr->dt_size*/
2231 if (dt_hdr_size > ROUND_TO_PAGE(hdr->dt_size,hdr->page_size)) {
2232 dprintf(CRITICAL, "ERROR: Invalid Device Tree size \n");
2233 return -1;
2234 }
2235
Amol Jadicb524072012-08-09 16:40:18 -07002236 /* Find index of device tree within device tree table */
2237 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
2238 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
2239 return -1;
2240 }
2241
Matthew Qin271927e2015-03-31 22:07:07 -04002242 best_match_dt_addr = (unsigned char *)boot_image_start + dt_image_offset + dt_entry.offset;
2243 if (is_gzip_package(best_match_dt_addr, dt_entry.size))
2244 {
2245 out_addr = (unsigned char *)target_get_scratch_address() + scratch_offset;
2246 out_avai_len = target_get_max_flash_size() - scratch_offset;
2247 dprintf(INFO, "decompressing dtb: start\n");
2248 rc = decompress(best_match_dt_addr,
2249 dt_entry.size, out_addr, out_avai_len,
2250 &compressed_size, &dtb_size);
2251 if (rc)
2252 {
Matthew Qin0b15b322015-05-19 05:20:54 -04002253 dprintf(CRITICAL, "decompressing dtb failed!!!\n");
Matthew Qin271927e2015-03-31 22:07:07 -04002254 ASSERT(0);
2255 }
2256
Matthew Qin0b15b322015-05-19 05:20:54 -04002257 dprintf(INFO, "decompressing dtb: done\n");
Matthew Qin271927e2015-03-31 22:07:07 -04002258 best_match_dt_addr = out_addr;
2259 } else {
2260 dtb_size = dt_entry.size;
2261 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002262 /* Validate and Read device device tree in the "tags_add */
Matthew Qin271927e2015-03-31 22:07:07 -04002263 if (check_aboot_addr_range_overlap(hdr->tags_addr, dtb_size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002264 {
2265 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
2266 return -1;
2267 }
2268
Amol Jadicb524072012-08-09 16:40:18 -07002269 /* Read device device tree in the "tags_add */
Matthew Qin271927e2015-03-31 22:07:07 -04002270 memmove((void*) hdr->tags_addr, (void *)best_match_dt_addr, dtb_size);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002271 } else
2272 return -1;
Amol Jadicb524072012-08-09 16:40:18 -07002273
2274 /* Everything looks fine. Return success. */
2275 return 0;
2276}
2277#endif
2278
Brian Swetland9c4c0752009-01-25 16:23:50 -08002279void cmd_boot(const char *arg, void *data, unsigned sz)
2280{
Amit Blay8a510302015-08-17 09:20:01 +03002281#ifdef MDTP_SUPPORT
2282 static bool is_mdtp_activated = 0;
2283#endif /* MDTP_SUPPORT */
Brian Swetland9c4c0752009-01-25 16:23:50 -08002284 unsigned kernel_actual;
2285 unsigned ramdisk_actual;
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002286 uint32_t image_actual;
2287 uint32_t dt_actual = 0;
P.V. Phani Kumareecfd822016-03-09 20:09:03 +05302288 uint32_t sig_actual = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08002289 struct boot_img_hdr *hdr = NULL;
2290 struct kernel64_hdr *kptr = NULL;
Brian Swetland9c4c0752009-01-25 16:23:50 -08002291 char *ptr = ((char*) data);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002292 int ret = 0;
2293 uint8_t dtb_copied = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08002294 unsigned int out_len = 0;
2295 unsigned int out_avai_len = 0;
2296 unsigned char *out_addr = NULL;
2297 uint32_t dtb_offset = 0;
2298 unsigned char *kernel_start_addr = NULL;
2299 unsigned int kernel_size = 0;
Matthew Qin271927e2015-03-31 22:07:07 -04002300 unsigned int scratch_offset = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08002301
lijuang2008ff22016-03-07 17:56:27 +08002302#if FBCON_DISPLAY_MSG
2303 /* Exit keys' detection thread firstly */
2304 exit_menu_keys_detection();
2305#endif
2306
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002307#if VERIFIED_BOOT
Channagoud Kadabi6d5375e2015-06-23 17:15:42 -07002308 if(target_build_variant_user() && !device.is_unlocked)
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002309 {
2310 fastboot_fail("unlock device to use this command");
lijuang2008ff22016-03-07 17:56:27 +08002311 goto boot_failed;
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002312 }
2313#endif
2314
Brian Swetland9c4c0752009-01-25 16:23:50 -08002315 if (sz < sizeof(hdr)) {
2316 fastboot_fail("invalid bootimage header");
lijuang2008ff22016-03-07 17:56:27 +08002317 goto boot_failed;
Brian Swetland9c4c0752009-01-25 16:23:50 -08002318 }
2319
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002320 hdr = (struct boot_img_hdr *)data;
Brian Swetland9c4c0752009-01-25 16:23:50 -08002321
2322 /* ensure commandline is terminated */
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002323 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -08002324
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002325 if(target_is_emmc_boot() && hdr->page_size) {
2326 page_size = hdr->page_size;
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07002327 page_mask = page_size - 1;
2328 }
2329
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002330 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
2331 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002332#if DEVICE_TREE
2333 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
2334#endif
2335
2336 image_actual = ADD_OF(page_size, kernel_actual);
2337 image_actual = ADD_OF(image_actual, ramdisk_actual);
2338 image_actual = ADD_OF(image_actual, dt_actual);
2339
P.V. Phani Kumareecfd822016-03-09 20:09:03 +05302340 if (target_use_signed_kernel() && (!device.is_unlocked)) {
2341 /* Calculate the signature length from boot image */
2342 sig_actual = read_der_message_length(
2343 (unsigned char*)(data + image_actual),sz);
Channagoud Kadabi3d839012014-06-26 14:26:39 -07002344 image_actual = ADD_OF(image_actual, sig_actual);
P.V. Phani Kumareecfd822016-03-09 20:09:03 +05302345 }
Channagoud Kadabi3d839012014-06-26 14:26:39 -07002346
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002347 /* sz should have atleast raw boot image */
2348 if (image_actual > sz) {
Channagoud Kadabi3d839012014-06-26 14:26:39 -07002349 fastboot_fail("bootimage: incomplete or not signed");
lijuang2008ff22016-03-07 17:56:27 +08002350 goto boot_failed;
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002351 }
2352
Sridhar Parasuram361e4cd2015-09-24 11:19:19 -07002353 // Initialize boot state before trying to verify boot.img
2354#if VERIFIED_BOOT
Channagoud Kadabi699466e2015-11-03 12:37:42 -08002355 boot_verifier_init();
Channagoud Kadabibf6ce7d2015-09-17 13:25:35 -07002356 /* Handle overflow if the input image size is greater than
2357 * boot image buffer can hold
2358 */
2359 if ((target_get_max_flash_size() - (image_actual - sig_actual)) < page_size)
2360 {
2361 fastboot_fail("booimage: size is greater than boot image buffer can hold");
lijuang2008ff22016-03-07 17:56:27 +08002362 goto boot_failed;
Channagoud Kadabibf6ce7d2015-09-17 13:25:35 -07002363 }
Sridhar Parasuram361e4cd2015-09-24 11:19:19 -07002364#endif
2365
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002366 /* Verify the boot image
2367 * device & page_size are initialized in aboot_init
2368 */
Sridhar Parasuram7ae9fbc2015-09-17 09:56:30 -07002369 if (target_use_signed_kernel() && (!device.is_unlocked)) {
Channagoud Kadabi3d839012014-06-26 14:26:39 -07002370 /* Pass size excluding signature size, otherwise we would try to
2371 * access signature beyond its length
2372 */
2373 verify_signed_bootimg((uint32_t)data, (image_actual - sig_actual));
Sridhar Parasuram7ae9fbc2015-09-17 09:56:30 -07002374 }
Amit Blay4aa292f2015-04-28 21:55:59 +03002375#ifdef MDTP_SUPPORT
2376 else
2377 {
Shay Nachmani062aa3f2015-05-17 17:28:44 +03002378 /* fastboot boot is not allowed when MDTP is activated */
Amit Blay4aa292f2015-04-28 21:55:59 +03002379 mdtp_ext_partition_verification_t ext_partition;
Amit Blay8a510302015-08-17 09:20:01 +03002380
2381 if (!is_mdtp_activated) {
2382 ext_partition.partition = MDTP_PARTITION_NONE;
2383 mdtp_fwlock_verify_lock(&ext_partition);
2384 }
Amit Blay4aa292f2015-04-28 21:55:59 +03002385 }
Shay Nachmani062aa3f2015-05-17 17:28:44 +03002386
Amir Kotzer7c768c02016-04-13 09:08:05 +03002387 /* If mdtp state cannot be validate, block fastboot boot*/
2388 if(mdtp_activated(&is_mdtp_activated)){
2389 dprintf(CRITICAL, "mdtp_activated cannot validate state.\n");
2390 dprintf(CRITICAL, "Can not proceed with fastboot boot command.\n");
2391 goto boot_failed;
2392 }
Shay Nachmani062aa3f2015-05-17 17:28:44 +03002393 if(is_mdtp_activated){
2394 dprintf(CRITICAL, "fastboot boot command is not available.\n");
lijuang2008ff22016-03-07 17:56:27 +08002395 goto boot_failed;
Shay Nachmani062aa3f2015-05-17 17:28:44 +03002396 }
Amit Blay4aa292f2015-04-28 21:55:59 +03002397#endif /* MDTP_SUPPORT */
2398
Sridhar Parasuram361e4cd2015-09-24 11:19:19 -07002399#if VERIFIED_BOOT
Parth Dixitbc9b6492015-10-18 00:41:38 +05302400#if !VBOOT_MOTA
Sridhar Parasuram361e4cd2015-09-24 11:19:19 -07002401 // send root of trust
2402 if(!send_rot_command((uint32_t)device.is_unlocked))
2403 ASSERT(0);
2404#endif
Parth Dixitbc9b6492015-10-18 00:41:38 +05302405#endif
Channagoud Kadabia22144f2013-03-20 11:49:01 -07002406 /*
Matthew Qinbb7923d2015-02-09 10:56:09 +08002407 * Check if the kernel image is a gzip package. If yes, need to decompress it.
2408 * If not, continue booting.
2409 */
2410 if (is_gzip_package((unsigned char *)(data + page_size), hdr->kernel_size))
2411 {
2412 out_addr = (unsigned char *)target_get_scratch_address();
2413 out_addr = (unsigned char *)(out_addr + image_actual + page_size);
2414 out_avai_len = target_get_max_flash_size() - image_actual - page_size;
Matthew Qin0b15b322015-05-19 05:20:54 -04002415 dprintf(INFO, "decompressing kernel image: start\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08002416 ret = decompress((unsigned char *)(ptr + page_size),
2417 hdr->kernel_size, out_addr, out_avai_len,
2418 &dtb_offset, &out_len);
2419 if (ret)
2420 {
Matthew Qin0b15b322015-05-19 05:20:54 -04002421 dprintf(CRITICAL, "decompressing image failed!!!\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08002422 ASSERT(0);
2423 }
2424
Matthew Qin0b15b322015-05-19 05:20:54 -04002425 dprintf(INFO, "decompressing kernel image: done\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08002426 kptr = (struct kernel64_hdr *)out_addr;
2427 kernel_start_addr = out_addr;
2428 kernel_size = out_len;
2429 } else {
2430 kptr = (struct kernel64_hdr*)((char *)data + page_size);
2431 kernel_start_addr = (unsigned char *)((char *)data + page_size);
2432 kernel_size = hdr->kernel_size;
2433 }
2434
2435 /*
Channagoud Kadabia22144f2013-03-20 11:49:01 -07002436 * Update the kernel/ramdisk/tags address if the boot image header
2437 * has default values, these default values come from mkbootimg when
2438 * the boot image is flashed using fastboot flash:raw
2439 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08002440 update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr));
Dima Zavin3cadfff2013-03-21 14:30:48 -07002441
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002442 /* Get virtual addresses since the hdr saves physical addresses. */
2443 hdr->kernel_addr = VA(hdr->kernel_addr);
2444 hdr->ramdisk_addr = VA(hdr->ramdisk_addr);
2445 hdr->tags_addr = VA(hdr->tags_addr);
Brian Swetland9c4c0752009-01-25 16:23:50 -08002446
Matthew Qinbb7923d2015-02-09 10:56:09 +08002447 kernel_size = ROUND_TO_PAGE(kernel_size, page_mask);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002448 /* Check if the addresses in the header are valid. */
Matthew Qinbb7923d2015-02-09 10:56:09 +08002449 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_size) ||
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002450 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
2451 {
2452 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
lijuang2008ff22016-03-07 17:56:27 +08002453 goto boot_failed;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002454 }
2455
Amol Jadicb524072012-08-09 16:40:18 -07002456#if DEVICE_TREE
Matthew Qin271927e2015-03-31 22:07:07 -04002457 scratch_offset = image_actual + page_size + out_len;
Amol Jadicb524072012-08-09 16:40:18 -07002458 /* find correct dtb and copy it to right location */
Matthew Qin271927e2015-03-31 22:07:07 -04002459 ret = copy_dtb(data, scratch_offset);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002460
2461 dtb_copied = !ret ? 1 : 0;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002462#else
2463 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
2464 {
2465 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
lijuang2008ff22016-03-07 17:56:27 +08002466 goto boot_failed;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002467 }
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002468#endif
2469
2470 /* Load ramdisk & kernel */
2471 memmove((void*) hdr->ramdisk_addr, ptr + page_size + kernel_actual, hdr->ramdisk_size);
Matthew Qinbb7923d2015-02-09 10:56:09 +08002472 memmove((void*) hdr->kernel_addr, (char*) (kernel_start_addr), kernel_size);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002473
2474#if DEVICE_TREE
Matthew Qinbb7923d2015-02-09 10:56:09 +08002475 if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual))
2476 {
2477 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
lijuang2008ff22016-03-07 17:56:27 +08002478 goto boot_failed;
Matthew Qinbb7923d2015-02-09 10:56:09 +08002479 }
2480
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002481 /*
2482 * If dtb is not found look for appended DTB in the kernel.
2483 * If appended dev tree is found, update the atags with
2484 * memory address to the DTB appended location on RAM.
2485 * Else update with the atags address in the kernel header
2486 */
2487 if (!dtb_copied) {
2488 void *dtb;
Matthew Qinbb7923d2015-02-09 10:56:09 +08002489 dtb = dev_tree_appended((void*)(ptr + page_size),
2490 hdr->kernel_size, dtb_offset,
Dima Zavine63e5572013-05-03 12:23:06 -07002491 (void *)hdr->tags_addr);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002492 if (!dtb) {
2493 fastboot_fail("dtb not found");
lijuang2008ff22016-03-07 17:56:27 +08002494 goto boot_failed;
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002495 }
Amol Jadicb524072012-08-09 16:40:18 -07002496 }
2497#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08002498
2499 fastboot_okay("");
Amol Jadi7c4316c2013-10-07 14:19:26 -07002500 fastboot_stop();
Brian Swetland9c4c0752009-01-25 16:23:50 -08002501
Dima Zavin77e41f32013-03-06 16:10:43 -08002502 boot_linux((void*) hdr->kernel_addr, (void*) hdr->tags_addr,
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002503 (const char*) hdr->cmdline, board_machtype(),
2504 (void*) hdr->ramdisk_addr, hdr->ramdisk_size);
lijuang2008ff22016-03-07 17:56:27 +08002505
2506 /* fastboot already stop, it's no need to show fastboot menu */
2507 return;
2508boot_failed:
2509#if FBCON_DISPLAY_MSG
2510 /* revert to fastboot menu if boot failed */
2511 display_fastboot_menu();
2512#endif
2513 return;
Brian Swetland9c4c0752009-01-25 16:23:50 -08002514}
2515
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002516void cmd_erase_nand(const char *arg, void *data, unsigned sz)
Dima Zavin214cc642009-01-26 11:16:21 -08002517{
2518 struct ptentry *ptn;
2519 struct ptable *ptable;
2520
2521 ptable = flash_get_ptable();
2522 if (ptable == NULL) {
2523 fastboot_fail("partition table doesn't exist");
2524 return;
2525 }
2526
2527 ptn = ptable_find(ptable, arg);
2528 if (ptn == NULL) {
2529 fastboot_fail("unknown partition name");
2530 return;
2531 }
2532
2533 if (flash_erase(ptn)) {
2534 fastboot_fail("failed to erase partition");
2535 return;
2536 }
2537 fastboot_okay("");
2538}
2539
Bikas Gurungd48bd242010-09-04 19:54:32 -07002540
2541void cmd_erase_mmc(const char *arg, void *data, unsigned sz)
2542{
2543 unsigned long long ptn = 0;
Oliver Wangcee448d2013-10-22 18:40:13 +08002544 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07002545 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002546 uint8_t lun = 0;
Vijay Kumar Pendoti0b21f462016-05-02 17:09:18 +05302547 char *footer = NULL;
Bikas Gurungd48bd242010-09-04 19:54:32 -07002548
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002549#if VERIFIED_BOOT
2550 if(!strcmp(arg, KEYSTORE_PTN_NAME))
2551 {
2552 if(!device.is_unlocked)
2553 {
2554 fastboot_fail("unlock device to erase keystore");
2555 return;
2556 }
2557 }
2558#endif
2559
Kinson Chikf1a43512011-07-14 11:28:39 -07002560 index = partition_get_index(arg);
2561 ptn = partition_get_offset(index);
Oliver Wangcee448d2013-10-22 18:40:13 +08002562 size = partition_get_size(index);
Neeti Desaica8c9602011-10-06 11:40:00 -07002563
Kinson Chikf1a43512011-07-14 11:28:39 -07002564 if(ptn == 0) {
Neeti Desaica8c9602011-10-06 11:40:00 -07002565 fastboot_fail("Partition table doesn't exist\n");
Bikas Gurungd48bd242010-09-04 19:54:32 -07002566 return;
2567 }
Kun Liang2f1601a2013-08-12 16:29:54 +08002568
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002569 lun = partition_get_lun(index);
2570 mmc_set_lun(lun);
2571
Sridhar Parasuramd7957122015-02-27 11:33:40 -08002572 if (platform_boot_dev_isemmc())
2573 {
2574 if (mmc_erase_card(ptn, size)) {
2575 fastboot_fail("failed to erase partition\n");
2576 return;
2577 }
2578 } else {
2579 BUF_DMA_ALIGN(out, DEFAULT_ERASE_SIZE);
2580 size = partition_get_size(index);
2581 if (size > DEFAULT_ERASE_SIZE)
2582 size = DEFAULT_ERASE_SIZE;
Kun Liang2f1601a2013-08-12 16:29:54 +08002583
Sridhar Parasuramd7957122015-02-27 11:33:40 -08002584 /* Simple inefficient version of erase. Just writing
2585 0 in first several blocks */
2586 if (mmc_write(ptn , size, (unsigned int *)out)) {
2587 fastboot_fail("failed to erase partition");
2588 return;
2589 }
Vijay Kumar Pendoti0b21f462016-05-02 17:09:18 +05302590 /*Erase FDE metadata at the userdata footer*/
2591 if(!(strncmp(arg, "userdata", 8)))
2592 {
2593 footer = memalign(CACHE_LINE, FOOTER_SIZE);
2594 memset((void *)footer, 0, FOOTER_SIZE);
2595
2596 size = partition_get_size(index);
2597
2598 if (mmc_write((ptn + size) - FOOTER_SIZE , FOOTER_SIZE, (unsigned int *)footer)) {
2599 fastboot_fail("failed to erase userdata footer");
2600 free(footer);
2601 return;
2602 }
2603 free(footer);
2604 }
Bikas Gurungd48bd242010-09-04 19:54:32 -07002605 }
Sridhar Parasuram32b30662015-07-10 13:33:22 -07002606#if VERIFIED_BOOT
Parth Dixitbc9b6492015-10-18 00:41:38 +05302607#if !VBOOT_MOTA
Sridhar Parasuram32b30662015-07-10 13:33:22 -07002608 if(!(strncmp(arg, "userdata", 8)))
2609 if(send_delete_keys_to_tz())
2610 ASSERT(0);
2611#endif
Parth Dixitbc9b6492015-10-18 00:41:38 +05302612#endif
Bikas Gurungd48bd242010-09-04 19:54:32 -07002613 fastboot_okay("");
2614}
2615
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002616void cmd_erase(const char *arg, void *data, unsigned sz)
2617{
Channagoud Kadabice3cf422015-04-17 16:02:30 -07002618#if VERIFIED_BOOT
Channagoud Kadabi35297672015-06-13 11:09:49 -07002619 if (target_build_variant_user())
Channagoud Kadabice3cf422015-04-17 16:02:30 -07002620 {
Sridhar Parasuramc32d07d2015-07-12 10:57:48 -07002621 if(!device.is_unlocked)
Channagoud Kadabice3cf422015-04-17 16:02:30 -07002622 {
Channagoud Kadabi35297672015-06-13 11:09:49 -07002623 fastboot_fail("device is locked. Cannot erase");
Channagoud Kadabice3cf422015-04-17 16:02:30 -07002624 return;
2625 }
2626 }
2627#endif
2628
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002629 if(target_is_emmc_boot())
2630 cmd_erase_mmc(arg, data, sz);
2631 else
2632 cmd_erase_nand(arg, data, sz);
2633}
Bikas Gurungd48bd242010-09-04 19:54:32 -07002634
Channagoud Kadabiad259832015-05-29 11:14:17 -07002635static uint32_t aboot_get_secret_key()
2636{
2637 /* 0 is invalid secret key, update this implementation to return
2638 * device specific unique secret key
2639 */
2640 return 0;
2641}
2642
Ajay Dudani5c761132011-04-07 20:19:04 -07002643void cmd_flash_mmc_img(const char *arg, void *data, unsigned sz)
Shashank Mittal23b8f422010-04-16 19:27:21 -07002644{
2645 unsigned long long ptn = 0;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07002646 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07002647 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002648 char *token = NULL;
2649 char *pname = NULL;
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07002650 char *sp;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002651 uint8_t lun = 0;
2652 bool lun_set = false;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07002653
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07002654 token = strtok_r((char *)arg, ":", &sp);
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002655 pname = token;
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07002656 token = strtok_r(NULL, ":", &sp);
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002657 if(token)
2658 {
2659 lun = atoi(token);
2660 mmc_set_lun(lun);
2661 lun_set = true;
2662 }
2663
Mao Jinlong226f33a2014-07-04 17:24:10 +08002664 if (pname)
Greg Grisco6e754772011-06-23 12:19:39 -07002665 {
Channagoud Kadabiad259832015-05-29 11:14:17 -07002666 if (!strncmp(pname, "frp-unlock", strlen("frp-unlock")))
2667 {
2668 if (!aboot_frp_unlock(pname, data, sz))
2669 {
2670 fastboot_info("FRP unlock successful");
2671 fastboot_okay("");
2672 }
2673 else
2674 fastboot_fail("Secret key is invalid, please update the bootloader with secret key");
2675
2676 return;
2677 }
2678
Mao Jinlong226f33a2014-07-04 17:24:10 +08002679 if (!strcmp(pname, "partition"))
2680 {
2681 dprintf(INFO, "Attempt to write partition image.\n");
2682 if (write_partition(sz, (unsigned char *) data)) {
2683 fastboot_fail("failed to write partition");
Greg Grisco6e754772011-06-23 12:19:39 -07002684 return;
2685 }
2686 }
Mao Jinlong226f33a2014-07-04 17:24:10 +08002687 else
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002688 {
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002689#if VERIFIED_BOOT
2690 if(!strcmp(pname, KEYSTORE_PTN_NAME))
2691 {
2692 if(!device.is_unlocked)
2693 {
2694 fastboot_fail("unlock device to flash keystore");
2695 return;
2696 }
Gaurav Nebhwanic7313cc2015-12-15 22:25:04 +05302697 if(!boot_verify_validate_keystore((unsigned char *)data,sz))
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002698 {
2699 fastboot_fail("image is not a keystore file");
2700 return;
2701 }
2702 }
2703#endif
Mao Jinlong226f33a2014-07-04 17:24:10 +08002704 index = partition_get_index(pname);
2705 ptn = partition_get_offset(index);
2706 if(ptn == 0) {
2707 fastboot_fail("partition table doesn't exist");
2708 return;
2709 }
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002710
Mao Jinlong226f33a2014-07-04 17:24:10 +08002711 if (!strcmp(pname, "boot") || !strcmp(pname, "recovery")) {
2712 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
2713 fastboot_fail("image is not a boot image");
2714 return;
2715 }
2716 }
2717
2718 if(!lun_set)
2719 {
2720 lun = partition_get_lun(index);
2721 mmc_set_lun(lun);
2722 }
2723
2724 size = partition_get_size(index);
Gaurav Nebhwani4d2389c2016-03-17 21:10:05 +05302725 if (ROUND_TO_PAGE(sz, mmc_blocksize_mask) > size) {
Mao Jinlong226f33a2014-07-04 17:24:10 +08002726 fastboot_fail("size too large");
2727 return;
2728 }
2729 else if (mmc_write(ptn , sz, (unsigned int *)data)) {
2730 fastboot_fail("flash write failure");
2731 return;
2732 }
Greg Grisco6e754772011-06-23 12:19:39 -07002733 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07002734 }
2735 fastboot_okay("");
2736 return;
2737}
2738
Ajay Dudanide984792015-03-02 09:57:41 -08002739void cmd_flash_meta_img(const char *arg, void *data, unsigned sz)
2740{
2741 int i, images;
2742 meta_header_t *meta_header;
2743 img_header_entry_t *img_header_entry;
Parth Dixit3e2d3032016-03-04 17:11:52 +05302744 /*End of the image address*/
2745 uintptr_t data_end;
2746
2747 if( (UINT_MAX - sz) > (uintptr_t)data )
2748 data_end = (uintptr_t)data + sz;
2749 else
2750 {
2751 fastboot_fail("Cannot flash: image header corrupt");
2752 return;
2753 }
2754
2755 if( data_end < ((uintptr_t)data + sizeof(meta_header_t)))
2756 {
2757 fastboot_fail("Cannot flash: image header corrupt");
2758 return;
2759 }
Ajay Dudanide984792015-03-02 09:57:41 -08002760
lijuang8ee21882016-04-19 16:57:11 +08002761 /* If device is locked:
2762 * Forbid to flash image to avoid the device to bypass the image
2763 * which with "any" name other than bootloader. Because it maybe
2764 * a meta package of all partitions.
2765 */
2766#if VERIFIED_BOOT
2767 if (target_build_variant_user()) {
2768 if (!device.is_unlocked) {
2769 fastboot_fail("Device is locked, meta image flashing is not allowed");
2770 return;
2771 }
2772#if !VBOOT_MOTA
2773 if(!device.is_unlock_critical) {
2774 fastboot_fail("Device is critical locked, Meta image flashing is not allowed");
2775 return;
2776 }
2777#endif
2778 }
2779#endif
2780
Ajay Dudanide984792015-03-02 09:57:41 -08002781 meta_header = (meta_header_t*) data;
Parth Dixit3e2d3032016-03-04 17:11:52 +05302782 if( data_end < ((uintptr_t)data + meta_header->img_hdr_sz))
2783 {
2784 fastboot_fail("Cannot flash: image header corrupt");
2785 return;
2786 }
Ajay Dudanide984792015-03-02 09:57:41 -08002787 img_header_entry = (img_header_entry_t*) (data+sizeof(meta_header_t));
2788
2789 images = meta_header->img_hdr_sz / sizeof(img_header_entry_t);
2790
2791 for (i=0; i<images; i++) {
2792
2793 if((img_header_entry[i].ptn_name == NULL) ||
2794 (img_header_entry[i].start_offset == 0) ||
2795 (img_header_entry[i].size == 0))
2796 break;
2797
Parth Dixit3e2d3032016-03-04 17:11:52 +05302798 if( data_end < ((uintptr_t)data + img_header_entry[i].start_offset
2799 + img_header_entry[i].size) )
2800 {
2801 fastboot_fail("Cannot flash: image size mismatch");
2802 break;
2803 }
2804
Ajay Dudanide984792015-03-02 09:57:41 -08002805 cmd_flash_mmc_img(img_header_entry[i].ptn_name,
2806 (void *) data + img_header_entry[i].start_offset,
2807 img_header_entry[i].size);
2808 }
2809
Channagoud Kadabiaafbbfc2015-03-04 17:52:56 -08002810 if (!strncmp(arg, "bootloader", strlen("bootloader")))
2811 {
2812 strlcpy(device.bootloader_version, TARGET(BOARD), MAX_VERSION_LEN);
2813 strlcat(device.bootloader_version, "-", MAX_VERSION_LEN);
2814 strlcat(device.bootloader_version, meta_header->img_version, MAX_VERSION_LEN);
2815 }
2816 else
2817 {
2818 strlcpy(device.radio_version, TARGET(BOARD), MAX_VERSION_LEN);
2819 strlcat(device.radio_version, "-", MAX_VERSION_LEN);
2820 strlcat(device.radio_version, meta_header->img_version, MAX_VERSION_LEN);
2821 }
2822
2823 write_device_info(&device);
Ajay Dudanide984792015-03-02 09:57:41 -08002824 fastboot_okay("");
2825 return;
2826}
2827
Ajay Dudani5c761132011-04-07 20:19:04 -07002828void cmd_flash_mmc_sparse_img(const char *arg, void *data, unsigned sz)
2829{
2830 unsigned int chunk;
wufeng.jiang813dc352015-06-02 23:02:46 -04002831 uint64_t chunk_data_sz;
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002832 uint32_t *fill_buf = NULL;
2833 uint32_t fill_val;
Ajay Dudani5c761132011-04-07 20:19:04 -07002834 sparse_header_t *sparse_header;
2835 chunk_header_t *chunk_header;
Ajay Dudaniab18f022011-05-12 14:39:22 -07002836 uint32_t total_blocks = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07002837 unsigned long long ptn = 0;
Channagoud Kadabi65b91002011-10-11 17:34:33 +05302838 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07002839 int index = INVALID_PTN;
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05302840 uint32_t i;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002841 uint8_t lun = 0;
vijay kumar800255e2015-04-24 20:26:19 +05302842 /*End of the sparse image address*/
Parth Dixit64b1a482016-03-07 16:31:26 +05302843 uintptr_t data_end = (uintptr_t)data + sz;
Ajay Dudani5c761132011-04-07 20:19:04 -07002844
Kinson Chikf1a43512011-07-14 11:28:39 -07002845 index = partition_get_index(arg);
2846 ptn = partition_get_offset(index);
2847 if(ptn == 0) {
Ajay Dudani5c761132011-04-07 20:19:04 -07002848 fastboot_fail("partition table doesn't exist");
2849 return;
2850 }
2851
Channagoud Kadabi65b91002011-10-11 17:34:33 +05302852 size = partition_get_size(index);
Channagoud Kadabi65b91002011-10-11 17:34:33 +05302853
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002854 lun = partition_get_lun(index);
2855 mmc_set_lun(lun);
2856
vijay kumar800255e2015-04-24 20:26:19 +05302857 if (sz < sizeof(sparse_header_t)) {
2858 fastboot_fail("size too low");
2859 return;
2860 }
2861
Ajay Dudani5c761132011-04-07 20:19:04 -07002862 /* Read and skip over sparse image header */
2863 sparse_header = (sparse_header_t *) data;
vijay kumar800255e2015-04-24 20:26:19 +05302864
vijay kumar1321f342015-03-27 12:13:42 +05302865 if (((uint64_t)sparse_header->total_blks * (uint64_t)sparse_header->blk_sz) > size) {
Ajay Dudani876b3282012-12-21 14:12:17 -08002866 fastboot_fail("size too large");
2867 return;
2868 }
2869
vijay kumarde4fcf62015-04-23 13:05:49 +05302870 data += sizeof(sparse_header_t);
vijay kumar800255e2015-04-24 20:26:19 +05302871
Parth Dixit64b1a482016-03-07 16:31:26 +05302872 if (data_end < (uintptr_t)data) {
vijay kumar800255e2015-04-24 20:26:19 +05302873 fastboot_fail("buffer overreads occured due to invalid sparse header");
2874 return;
2875 }
2876
vijay kumarde4fcf62015-04-23 13:05:49 +05302877 if(sparse_header->file_hdr_sz != sizeof(sparse_header_t))
Ajay Dudani5c761132011-04-07 20:19:04 -07002878 {
vijay kumarde4fcf62015-04-23 13:05:49 +05302879 fastboot_fail("sparse header size mismatch");
2880 return;
Ajay Dudani5c761132011-04-07 20:19:04 -07002881 }
2882
Ajay Dudanib06c05f2011-05-12 14:46:10 -07002883 dprintf (SPEW, "=== Sparse Image Header ===\n");
2884 dprintf (SPEW, "magic: 0x%x\n", sparse_header->magic);
2885 dprintf (SPEW, "major_version: 0x%x\n", sparse_header->major_version);
2886 dprintf (SPEW, "minor_version: 0x%x\n", sparse_header->minor_version);
2887 dprintf (SPEW, "file_hdr_sz: %d\n", sparse_header->file_hdr_sz);
2888 dprintf (SPEW, "chunk_hdr_sz: %d\n", sparse_header->chunk_hdr_sz);
2889 dprintf (SPEW, "blk_sz: %d\n", sparse_header->blk_sz);
2890 dprintf (SPEW, "total_blks: %d\n", sparse_header->total_blks);
2891 dprintf (SPEW, "total_chunks: %d\n", sparse_header->total_chunks);
Ajay Dudani5c761132011-04-07 20:19:04 -07002892
2893 /* Start processing chunks */
2894 for (chunk=0; chunk<sparse_header->total_chunks; chunk++)
2895 {
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302896 /* Make sure the total image size does not exceed the partition size */
2897 if(((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz) >= size) {
2898 fastboot_fail("size too large");
2899 return;
2900 }
Ajay Dudani5c761132011-04-07 20:19:04 -07002901 /* Read and skip over chunk header */
2902 chunk_header = (chunk_header_t *) data;
2903 data += sizeof(chunk_header_t);
2904
Parth Dixit64b1a482016-03-07 16:31:26 +05302905 if (data_end < (uintptr_t)data) {
vijay kumar800255e2015-04-24 20:26:19 +05302906 fastboot_fail("buffer overreads occured due to invalid sparse header");
2907 return;
2908 }
2909
Ajay Dudani5c761132011-04-07 20:19:04 -07002910 dprintf (SPEW, "=== Chunk Header ===\n");
2911 dprintf (SPEW, "chunk_type: 0x%x\n", chunk_header->chunk_type);
2912 dprintf (SPEW, "chunk_data_sz: 0x%x\n", chunk_header->chunk_sz);
2913 dprintf (SPEW, "total_size: 0x%x\n", chunk_header->total_sz);
2914
vijay kumar800255e2015-04-24 20:26:19 +05302915 if(sparse_header->chunk_hdr_sz != sizeof(chunk_header_t))
Ajay Dudani5c761132011-04-07 20:19:04 -07002916 {
vijay kumar800255e2015-04-24 20:26:19 +05302917 fastboot_fail("chunk header size mismatch");
2918 return;
Ajay Dudani5c761132011-04-07 20:19:04 -07002919 }
2920
wufeng.jiang813dc352015-06-02 23:02:46 -04002921 if (!sparse_header->blk_sz ){
2922 fastboot_fail("Invalid block size\n");
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302923 return;
2924 }
2925
wufeng.jiang813dc352015-06-02 23:02:46 -04002926 chunk_data_sz = (uint64_t)sparse_header->blk_sz * chunk_header->chunk_sz;
2927
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302928 /* Make sure that the chunk size calculated from sparse image does not
2929 * exceed partition size
2930 */
2931 if ((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz + chunk_data_sz > size)
2932 {
2933 fastboot_fail("Chunk data size exceeds partition size");
2934 return;
2935 }
2936
Ajay Dudani5c761132011-04-07 20:19:04 -07002937 switch (chunk_header->chunk_type)
2938 {
2939 case CHUNK_TYPE_RAW:
wufeng.jiang813dc352015-06-02 23:02:46 -04002940 if((uint64_t)chunk_header->total_sz != ((uint64_t)sparse_header->chunk_hdr_sz +
Ajay Dudani5c761132011-04-07 20:19:04 -07002941 chunk_data_sz))
2942 {
2943 fastboot_fail("Bogus chunk size for chunk type Raw");
2944 return;
2945 }
2946
Parth Dixit64b1a482016-03-07 16:31:26 +05302947 if (data_end < (uintptr_t)data + chunk_data_sz) {
vijay kumar800255e2015-04-24 20:26:19 +05302948 fastboot_fail("buffer overreads occured due to invalid sparse header");
2949 return;
2950 }
2951
wufeng.jiang813dc352015-06-02 23:02:46 -04002952 /* chunk_header->total_sz is uint32,So chunk_data_sz is now less than 2^32
2953 otherwise it will return in the line above
2954 */
Ajay Dudaniab18f022011-05-12 14:39:22 -07002955 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
wufeng.jiang813dc352015-06-02 23:02:46 -04002956 (uint32_t)chunk_data_sz,
Ajay Dudaniab18f022011-05-12 14:39:22 -07002957 (unsigned int*)data))
Ajay Dudani5c761132011-04-07 20:19:04 -07002958 {
2959 fastboot_fail("flash write failure");
2960 return;
2961 }
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302962 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
2963 fastboot_fail("Bogus size for RAW chunk type");
2964 return;
2965 }
Ajay Dudani5c761132011-04-07 20:19:04 -07002966 total_blocks += chunk_header->chunk_sz;
wufeng.jiang813dc352015-06-02 23:02:46 -04002967 data += (uint32_t)chunk_data_sz;
Ajay Dudani5c761132011-04-07 20:19:04 -07002968 break;
2969
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002970 case CHUNK_TYPE_FILL:
2971 if(chunk_header->total_sz != (sparse_header->chunk_hdr_sz +
2972 sizeof(uint32_t)))
2973 {
2974 fastboot_fail("Bogus chunk size for chunk type FILL");
2975 return;
2976 }
2977
2978 fill_buf = (uint32_t *)memalign(CACHE_LINE, ROUNDUP(sparse_header->blk_sz, CACHE_LINE));
2979 if (!fill_buf)
2980 {
2981 fastboot_fail("Malloc failed for: CHUNK_TYPE_FILL");
2982 return;
2983 }
2984
Parth Dixit64b1a482016-03-07 16:31:26 +05302985 if (data_end < (uintptr_t)data + sizeof(uint32_t)) {
vijay kumar800255e2015-04-24 20:26:19 +05302986 fastboot_fail("buffer overreads occured due to invalid sparse header");
Gaurav Nebhwani163cbf82016-03-17 21:21:27 +05302987 free(fill_buf);
vijay kumar800255e2015-04-24 20:26:19 +05302988 return;
2989 }
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002990 fill_val = *(uint32_t *)data;
2991 data = (char *) data + sizeof(uint32_t);
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002992
2993 for (i = 0; i < (sparse_header->blk_sz / sizeof(fill_val)); i++)
2994 {
2995 fill_buf[i] = fill_val;
2996 }
2997
Gaurav Nebhwanie94cbe12016-03-17 21:16:34 +05302998 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz))
2999 {
3000 fastboot_fail("bogus size for chunk FILL type");
3001 free(fill_buf);
3002 return;
3003 }
3004
wufeng.jiang813dc352015-06-02 23:02:46 -04003005 for (i = 0; i < chunk_header->chunk_sz; i++)
Channagoud Kadabif627cf82013-09-09 14:08:20 -07003006 {
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05303007 /* Make sure that the data written to partition does not exceed partition size */
3008 if ((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz + sparse_header->blk_sz > size)
3009 {
3010 fastboot_fail("Chunk data size for fill type exceeds partition size");
Gaurav Nebhwani163cbf82016-03-17 21:21:27 +05303011 free(fill_buf);
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05303012 return;
3013 }
3014
Channagoud Kadabif627cf82013-09-09 14:08:20 -07003015 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
3016 sparse_header->blk_sz,
3017 fill_buf))
3018 {
3019 fastboot_fail("flash write failure");
3020 free(fill_buf);
3021 return;
3022 }
3023
3024 total_blocks++;
3025 }
3026
3027 free(fill_buf);
3028 break;
3029
Ajay Dudani5c761132011-04-07 20:19:04 -07003030 case CHUNK_TYPE_DONT_CARE:
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05303031 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
3032 fastboot_fail("bogus size for chunk DONT CARE type");
3033 return;
3034 }
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07003035 total_blocks += chunk_header->chunk_sz;
3036 break;
3037
Ajay Dudani5c761132011-04-07 20:19:04 -07003038 case CHUNK_TYPE_CRC:
3039 if(chunk_header->total_sz != sparse_header->chunk_hdr_sz)
3040 {
wufeng.jiang813dc352015-06-02 23:02:46 -04003041 fastboot_fail("Bogus chunk size for chunk type CRC");
Ajay Dudani5c761132011-04-07 20:19:04 -07003042 return;
3043 }
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05303044 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
3045 fastboot_fail("bogus size for chunk CRC type");
3046 return;
3047 }
Ajay Dudani5c761132011-04-07 20:19:04 -07003048 total_blocks += chunk_header->chunk_sz;
Parth Dixit64b1a482016-03-07 16:31:26 +05303049 if ((uintptr_t)data > UINT_MAX - chunk_data_sz) {
vijay kumar800255e2015-04-24 20:26:19 +05303050 fastboot_fail("integer overflow occured");
3051 return;
3052 }
wufeng.jiang813dc352015-06-02 23:02:46 -04003053 data += (uint32_t)chunk_data_sz;
Parth Dixit64b1a482016-03-07 16:31:26 +05303054 if (data_end < (uintptr_t)data) {
vijay kumar800255e2015-04-24 20:26:19 +05303055 fastboot_fail("buffer overreads occured due to invalid sparse header");
3056 return;
3057 }
Ajay Dudani5c761132011-04-07 20:19:04 -07003058 break;
3059
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07003060 default:
Channagoud Kadabif627cf82013-09-09 14:08:20 -07003061 dprintf(CRITICAL, "Unkown chunk type: %x\n",chunk_header->chunk_type);
Ajay Dudani5c761132011-04-07 20:19:04 -07003062 fastboot_fail("Unknown chunk type");
3063 return;
3064 }
3065 }
3066
Ajay Dudani0c6927b2011-05-18 11:12:16 -07003067 dprintf(INFO, "Wrote %d blocks, expected to write %d blocks\n",
3068 total_blocks, sparse_header->total_blks);
3069
3070 if(total_blocks != sparse_header->total_blks)
3071 {
3072 fastboot_fail("sparse image write failure");
3073 }
Ajay Dudani5c761132011-04-07 20:19:04 -07003074
3075 fastboot_okay("");
3076 return;
3077}
3078
3079void cmd_flash_mmc(const char *arg, void *data, unsigned sz)
3080{
3081 sparse_header_t *sparse_header;
Ajay Dudanide984792015-03-02 09:57:41 -08003082 meta_header_t *meta_header;
Ajay Dudani5c761132011-04-07 20:19:04 -07003083
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07003084#ifdef SSD_ENABLE
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -08003085 /* 8 Byte Magic + 2048 Byte xml + Encrypted Data */
3086 unsigned int *magic_number = (unsigned int *) data;
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08003087 int ret=0;
3088 uint32 major_version=0;
3089 uint32 minor_version=0;
3090
3091 ret = scm_svc_version(&major_version,&minor_version);
3092 if(!ret)
3093 {
3094 if(major_version >= 2)
3095 {
Amir Samuelovbb65ce02013-05-05 12:20:18 +03003096 if( !strcmp(arg, "ssd") || !strcmp(arg, "tqs") )
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08003097 {
3098 ret = encrypt_scm((uint32 **) &data, &sz);
3099 if (ret != 0) {
3100 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
3101 return;
3102 }
3103
Amir Samuelovbb65ce02013-05-05 12:20:18 +03003104 /* Protect only for SSD */
3105 if (!strcmp(arg, "ssd")) {
3106 ret = scm_protect_keystore((uint32 *) data, sz);
3107 if (ret != 0) {
3108 dprintf(CRITICAL, "ERROR: scm_protect_keystore Failed\n");
3109 return;
3110 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08003111 }
3112 }
3113 else
3114 {
3115 ret = decrypt_scm_v2((uint32 **) &data, &sz);
3116 if(ret != 0)
3117 {
3118 dprintf(CRITICAL,"ERROR: Decryption Failure\n");
3119 return;
3120 }
3121 }
3122 }
3123 else
3124 {
3125 if (magic_number[0] == DECRYPT_MAGIC_0 &&
3126 magic_number[1] == DECRYPT_MAGIC_1)
3127 {
3128 ret = decrypt_scm((uint32 **) &data, &sz);
3129 if (ret != 0) {
3130 dprintf(CRITICAL, "ERROR: Invalid secure image\n");
3131 return;
3132 }
3133 }
3134 else if (magic_number[0] == ENCRYPT_MAGIC_0 &&
3135 magic_number[1] == ENCRYPT_MAGIC_1)
3136 {
3137 ret = encrypt_scm((uint32 **) &data, &sz);
3138 if (ret != 0) {
3139 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
3140 return;
3141 }
3142 }
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07003143 }
3144 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08003145 else
Neeti Desai127b9e02012-03-20 16:11:23 -07003146 {
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08003147 dprintf(CRITICAL,"INVALID SVC Version\n");
3148 return;
Neeti Desai127b9e02012-03-20 16:11:23 -07003149 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08003150#endif /* SSD_ENABLE */
Neeti Desai127b9e02012-03-20 16:11:23 -07003151
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003152#if VERIFIED_BOOT
Channagoud Kadabi35297672015-06-13 11:09:49 -07003153 if (target_build_variant_user())
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003154 {
lijuang511a2b52015-08-14 20:50:51 +08003155 /* if device is locked:
3156 * common partition will not allow to be flashed
3157 * critical partition will allow to flash image.
3158 */
3159 if(!device.is_unlocked && !critical_flash_allowed(arg)) {
3160 fastboot_fail("Partition flashing is not allowed");
3161 return;
3162 }
Parth Dixitddbc7352015-10-18 03:13:31 +05303163#if !VBOOT_MOTA
lijuang511a2b52015-08-14 20:50:51 +08003164 /* if device critical is locked:
3165 * common partition will allow to be flashed
3166 * critical partition will not allow to flash image.
3167 */
3168 if(!device.is_unlock_critical && critical_flash_allowed(arg)) {
3169 fastboot_fail("Critical partition flashing is not allowed");
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003170 return;
3171 }
Parth Dixitddbc7352015-10-18 03:13:31 +05303172#endif
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003173 }
3174#endif
3175
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07003176 sparse_header = (sparse_header_t *) data;
Ajay Dudanide984792015-03-02 09:57:41 -08003177 meta_header = (meta_header_t *) data;
3178 if (sparse_header->magic == SPARSE_HEADER_MAGIC)
Ajay Dudani5c761132011-04-07 20:19:04 -07003179 cmd_flash_mmc_sparse_img(arg, data, sz);
Ajay Dudanide984792015-03-02 09:57:41 -08003180 else if (meta_header->magic == META_HEADER_MAGIC)
3181 cmd_flash_meta_img(arg, data, sz);
3182 else
3183 cmd_flash_mmc_img(arg, data, sz);
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07003184
3185#if VERIFIED_BOOT
Parth Dixitddbc7352015-10-18 03:13:31 +05303186#if !VBOOT_MOTA
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07003187 if((!strncmp(arg, "system", 6)) && !device.verity_mode)
3188 {
3189 // reset dm_verity mode to enforcing
3190 device.verity_mode = 1;
3191 write_device_info(&device);
3192 }
3193#endif
Parth Dixitddbc7352015-10-18 03:13:31 +05303194#endif
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07003195
Ajay Dudani5c761132011-04-07 20:19:04 -07003196 return;
3197}
3198
Tanya Brokhman84eaaf62015-02-24 21:57:06 +02003199void cmd_updatevol(const char *vol_name, void *data, unsigned sz)
3200{
3201 struct ptentry *sys_ptn;
3202 struct ptable *ptable;
3203
3204 ptable = flash_get_ptable();
3205 if (ptable == NULL) {
3206 fastboot_fail("partition table doesn't exist");
3207 return;
3208 }
3209
3210 sys_ptn = ptable_find(ptable, "system");
3211 if (sys_ptn == NULL) {
3212 fastboot_fail("system partition not found");
3213 return;
3214 }
3215
3216 sz = ROUND_TO_PAGE(sz, page_mask);
3217 if (update_ubi_vol(sys_ptn, vol_name, data, sz))
3218 fastboot_fail("update_ubi_vol failed");
3219 else
3220 fastboot_okay("");
3221}
3222
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003223void cmd_flash_nand(const char *arg, void *data, unsigned sz)
Dima Zavin214cc642009-01-26 11:16:21 -08003224{
3225 struct ptentry *ptn;
3226 struct ptable *ptable;
3227 unsigned extra = 0;
Gaurav Nebhwanid9730712016-05-06 14:28:29 +05303228 uint64_t partition_size = 0;
Dima Zavin214cc642009-01-26 11:16:21 -08003229
3230 ptable = flash_get_ptable();
3231 if (ptable == NULL) {
3232 fastboot_fail("partition table doesn't exist");
3233 return;
3234 }
3235
3236 ptn = ptable_find(ptable, arg);
3237 if (ptn == NULL) {
Tanya Brokhman84eaaf62015-02-24 21:57:06 +02003238 dprintf(INFO, "unknown partition name (%s). Trying updatevol\n",
3239 arg);
3240 cmd_updatevol(arg, data, sz);
Dima Zavin214cc642009-01-26 11:16:21 -08003241 return;
3242 }
3243
3244 if (!strcmp(ptn->name, "boot") || !strcmp(ptn->name, "recovery")) {
3245 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
3246 fastboot_fail("image is not a boot image");
3247 return;
3248 }
3249 }
3250
Amol Jadi5c61a952012-05-04 17:05:35 -07003251 if (!strcmp(ptn->name, "system")
Deepa Dinamani13e32c42012-03-12 14:34:17 -07003252 || !strcmp(ptn->name, "userdata")
3253 || !strcmp(ptn->name, "persist")
Sundarajan Srinivasanb063a852013-11-19 14:02:27 -08003254 || !strcmp(ptn->name, "recoveryfs")
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -08003255 || !strcmp(ptn->name, "modem"))
Tanya Brokhman1c94f1a2015-02-15 09:05:03 +02003256 extra = 1;
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -08003257 else
Shashank Mittaldcc2e352009-11-19 19:11:16 -08003258 sz = ROUND_TO_PAGE(sz, page_mask);
Dima Zavin214cc642009-01-26 11:16:21 -08003259
Gaurav Nebhwanid9730712016-05-06 14:28:29 +05303260 partition_size = (uint64_t)ptn->length * (uint64_t)flash_num_pages_per_blk() * (uint64_t)flash_page_size();
3261 if (partition_size > UINT_MAX) {
3262 fastboot_fail("Invalid partition size");
3263 return;
3264 }
3265
3266 if (sz > partition_size) {
3267 fastboot_fail("Image size too large");
3268 return;
3269 }
3270
Dima Zavin214cc642009-01-26 11:16:21 -08003271 dprintf(INFO, "writing %d bytes to '%s'\n", sz, ptn->name);
Tanya Brokhman1c94f1a2015-02-15 09:05:03 +02003272 if (!memcmp((void *)data, UBI_MAGIC, UBI_MAGIC_SIZE)) {
3273 if (flash_ubi_img(ptn, data, sz)) {
3274 fastboot_fail("flash write failure");
3275 return;
3276 }
3277 } else {
3278 if (flash_write(ptn, extra, data, sz)) {
3279 fastboot_fail("flash write failure");
3280 return;
3281 }
Dima Zavin214cc642009-01-26 11:16:21 -08003282 }
3283 dprintf(INFO, "partition '%s' updated\n", ptn->name);
3284 fastboot_okay("");
3285}
3286
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003287void cmd_flash(const char *arg, void *data, unsigned sz)
3288{
3289 if(target_is_emmc_boot())
3290 cmd_flash_mmc(arg, data, sz);
3291 else
3292 cmd_flash_nand(arg, data, sz);
3293}
3294
Dima Zavin214cc642009-01-26 11:16:21 -08003295void cmd_continue(const char *arg, void *data, unsigned sz)
3296{
3297 fastboot_okay("");
Amol Jadi7c4316c2013-10-07 14:19:26 -07003298 fastboot_stop();
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003299
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003300 if (target_is_emmc_boot())
3301 {
lijuanga40d6302015-07-20 20:10:13 +08003302#if FBCON_DISPLAY_MSG
lijuangde34d502016-02-26 16:04:50 +08003303 /* Exit keys' detection thread firstly */
3304 exit_menu_keys_detection();
lijuanga40d6302015-07-20 20:10:13 +08003305#endif
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003306 boot_linux_from_mmc();
3307 }
3308 else
3309 {
3310 boot_linux_from_flash();
3311 }
Dima Zavin214cc642009-01-26 11:16:21 -08003312}
3313
Chandan Uddaraju94183c02010-01-15 15:13:59 -08003314void cmd_reboot(const char *arg, void *data, unsigned sz)
3315{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003316 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08003317 fastboot_okay("");
3318 reboot_device(0);
3319}
3320
3321void cmd_reboot_bootloader(const char *arg, void *data, unsigned sz)
3322{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003323 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08003324 fastboot_okay("");
3325 reboot_device(FASTBOOT_MODE);
3326}
3327
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003328void cmd_oem_enable_charger_screen(const char *arg, void *data, unsigned size)
3329{
3330 dprintf(INFO, "Enabling charger screen check\n");
3331 device.charger_screen_enabled = 1;
3332 write_device_info(&device);
3333 fastboot_okay("");
3334}
3335
3336void cmd_oem_disable_charger_screen(const char *arg, void *data, unsigned size)
3337{
3338 dprintf(INFO, "Disabling charger screen check\n");
3339 device.charger_screen_enabled = 0;
3340 write_device_info(&device);
3341 fastboot_okay("");
3342}
3343
lijuanga25d8bb2015-09-16 13:11:52 +08003344void cmd_oem_off_mode_charger(const char *arg, void *data, unsigned size)
3345{
3346 char *p = NULL;
3347 const char *delim = " \t\n\r";
Parth Dixit407f15b2016-01-07 14:47:37 +05303348 char *sp;
lijuanga25d8bb2015-09-16 13:11:52 +08003349
3350 if (arg) {
Parth Dixit407f15b2016-01-07 14:47:37 +05303351 p = strtok_r((char *)arg, delim, &sp);
lijuanga25d8bb2015-09-16 13:11:52 +08003352 if (p) {
3353 if (!strncmp(p, "0", 1)) {
3354 device.charger_screen_enabled = 0;
3355 } else if (!strncmp(p, "1", 1)) {
3356 device.charger_screen_enabled = 1;
3357 }
3358 }
3359 }
3360
3361 /* update charger_screen_enabled value for getvar
3362 * command
3363 */
3364 snprintf(charger_screen_enabled, MAX_RSP_SIZE, "%d",
3365 device.charger_screen_enabled);
3366
3367 write_device_info(&device);
3368 fastboot_okay("");
3369}
3370
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05303371void cmd_oem_select_display_panel(const char *arg, void *data, unsigned size)
3372{
3373 dprintf(INFO, "Selecting display panel %s\n", arg);
3374 if (arg)
3375 strlcpy(device.display_panel, arg,
3376 sizeof(device.display_panel));
3377 write_device_info(&device);
3378 fastboot_okay("");
3379}
3380
Shashank Mittal162244e2011-08-08 19:01:25 -07003381void cmd_oem_unlock(const char *arg, void *data, unsigned sz)
3382{
lijuang4ece1e72015-08-14 21:02:36 +08003383 set_device_unlock(UNLOCK, TRUE);
vijay kumarc65876c2015-04-24 13:29:16 +05303384}
3385
3386void cmd_oem_unlock_go(const char *arg, void *data, unsigned sz)
3387{
lijuang4ece1e72015-08-14 21:02:36 +08003388 if(!device.is_unlocked) {
vijay kumarc65876c2015-04-24 13:29:16 +05303389 if(!is_allow_unlock) {
3390 fastboot_fail("oem unlock is not allowed");
3391 return;
3392 }
3393
lijuang4ece1e72015-08-14 21:02:36 +08003394 set_device_unlock_value(UNLOCK, TRUE);
vijay kumarc65876c2015-04-24 13:29:16 +05303395
lijuang4ece1e72015-08-14 21:02:36 +08003396 /* wipe data */
vijay kumarc65876c2015-04-24 13:29:16 +05303397 struct recovery_message msg;
lijuang4ece1e72015-08-14 21:02:36 +08003398
vijay kumarc65876c2015-04-24 13:29:16 +05303399 snprintf(msg.recovery, sizeof(msg.recovery), "recovery\n--wipe_data");
3400 write_misc(0, &msg, sizeof(msg));
3401
3402 fastboot_okay("");
3403 reboot_device(RECOVERY_MODE);
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003404 }
3405 fastboot_okay("");
3406}
3407
Channagoud Kadabiad259832015-05-29 11:14:17 -07003408static int aboot_frp_unlock(char *pname, void *data, unsigned sz)
3409{
3410 int ret = 1;
3411 uint32_t secret_key;
3412 char seckey_buffer[MAX_RSP_SIZE];
3413
3414 secret_key = aboot_get_secret_key();
3415 if (secret_key)
3416 {
3417 snprintf((char *) seckey_buffer, MAX_RSP_SIZE, "%x", secret_key);
3418 if (!memcmp((void *)data, (void *)seckey_buffer, sz))
3419 {
3420 is_allow_unlock = true;
3421 write_allow_oem_unlock(is_allow_unlock);
3422 ret = 0;
3423 }
3424 }
3425 return ret;
3426}
3427
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003428void cmd_oem_lock(const char *arg, void *data, unsigned sz)
3429{
lijuang4ece1e72015-08-14 21:02:36 +08003430 set_device_unlock(UNLOCK, FALSE);
Shashank Mittal162244e2011-08-08 19:01:25 -07003431}
3432
Shashank Mittala0032282011-08-26 14:50:11 -07003433void cmd_oem_devinfo(const char *arg, void *data, unsigned sz)
3434{
lijuang511a2b52015-08-14 20:50:51 +08003435 char response[MAX_RSP_SIZE];
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003436 snprintf(response, sizeof(response), "\tDevice tampered: %s", (device.is_tampered ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07003437 fastboot_info(response);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003438 snprintf(response, sizeof(response), "\tDevice unlocked: %s", (device.is_unlocked ? "true" : "false"));
3439 fastboot_info(response);
Parth Dixitddbc7352015-10-18 03:13:31 +05303440#if !VBOOT_MOTA
lijuang511a2b52015-08-14 20:50:51 +08003441 snprintf(response, sizeof(response), "\tDevice critical unlocked: %s", (device.is_unlock_critical ? "true" : "false"));
3442 fastboot_info(response);
Parth Dixitddbc7352015-10-18 03:13:31 +05303443#endif
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003444 snprintf(response, sizeof(response), "\tCharger screen enabled: %s", (device.charger_screen_enabled ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07003445 fastboot_info(response);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05303446 snprintf(response, sizeof(response), "\tDisplay panel: %s", (device.display_panel));
3447 fastboot_info(response);
Shashank Mittala0032282011-08-26 14:50:11 -07003448 fastboot_okay("");
3449}
3450
lijuang511a2b52015-08-14 20:50:51 +08003451void cmd_flashing_get_unlock_ability(const char *arg, void *data, unsigned sz)
3452{
3453 char response[MAX_RSP_SIZE];
3454 snprintf(response, sizeof(response), "\tget_unlock_ability: %d", is_allow_unlock);
3455 fastboot_info(response);
3456 fastboot_okay("");
3457}
3458
3459void cmd_flashing_lock_critical(const char *arg, void *data, unsigned sz)
3460{
lijuang4ece1e72015-08-14 21:02:36 +08003461 set_device_unlock(UNLOCK_CRITICAL, FALSE);
lijuang511a2b52015-08-14 20:50:51 +08003462}
3463
3464void cmd_flashing_unlock_critical(const char *arg, void *data, unsigned sz)
3465{
lijuang4ece1e72015-08-14 21:02:36 +08003466 set_device_unlock(UNLOCK_CRITICAL, TRUE);
lijuang511a2b52015-08-14 20:50:51 +08003467}
3468
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07003469void cmd_preflash(const char *arg, void *data, unsigned sz)
3470{
3471 fastboot_okay("");
3472}
3473
Mao Flynn7b379f32015-04-20 00:28:30 +08003474static uint8_t logo_header[LOGO_IMG_HEADER_SIZE];
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303475
Mao Flynn7b379f32015-04-20 00:28:30 +08003476int splash_screen_check_header(logo_img_header *header)
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303477{
Mao Flynn7b379f32015-04-20 00:28:30 +08003478 if (memcmp(header->magic, LOGO_IMG_MAGIC, 8))
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303479 return -1;
Mao Flynn7b379f32015-04-20 00:28:30 +08003480 if (header->width == 0 || header->height == 0)
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303481 return -1;
3482 return 0;
3483}
3484
Mao Flynn7b379f32015-04-20 00:28:30 +08003485int splash_screen_flash()
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003486{
3487 struct ptentry *ptn;
3488 struct ptable *ptable;
Mao Flynn7b379f32015-04-20 00:28:30 +08003489 struct logo_img_header *header;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003490 struct fbcon_config *fb_display = NULL;
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08003491
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303492 ptable = flash_get_ptable();
3493 if (ptable == NULL) {
Mao Flynn7b379f32015-04-20 00:28:30 +08003494 dprintf(CRITICAL, "ERROR: Partition table not found\n");
3495 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303496 }
Mao Flynn7b379f32015-04-20 00:28:30 +08003497
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303498 ptn = ptable_find(ptable, "splash");
3499 if (ptn == NULL) {
3500 dprintf(CRITICAL, "ERROR: splash Partition not found\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003501 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303502 }
Mao Flynn7b379f32015-04-20 00:28:30 +08003503 if (flash_read(ptn, 0, (void *)logo_header, LOGO_IMG_HEADER_SIZE)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303504 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003505 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303506 }
3507
Mao Flynn7b379f32015-04-20 00:28:30 +08003508 header = (struct logo_img_header *)logo_header;
3509 if (splash_screen_check_header(header)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303510 dprintf(CRITICAL, "ERROR: Boot image header invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003511 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303512 }
3513
3514 fb_display = fbcon_display();
3515 if (fb_display) {
Mao Flynn7b379f32015-04-20 00:28:30 +08003516 if (header->type && (header->blocks != 0)) { // RLE24 compressed data
3517 uint8_t *base = (uint8_t *) fb_display->base + LOGO_IMG_OFFSET;
3518
3519 /* if the logo is full-screen size, remove "fbcon_clear()" */
3520 if ((header->width != fb_display->width)
3521 || (header->height != fb_display->height))
3522 fbcon_clear();
3523
3524 if (flash_read(ptn + LOGO_IMG_HEADER_SIZE, 0,
3525 (uint32_t *)base,
3526 (header->blocks * 512))) {
3527 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
3528 return -1;
3529 }
3530 fbcon_extract_to_screen(header, base);
3531 return 0;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003532 }
Mao Flynn7b379f32015-04-20 00:28:30 +08003533
Veera Sundaram Sankaranc33416f2015-09-28 10:25:55 -07003534 if ((header->width > fb_display->width) || (header->height > fb_display->height)) {
3535 dprintf(CRITICAL, "Logo config greater than fb config. Fall back default logo\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003536 return -1;
3537 }
3538
Vineet Bajaj99291ed2014-09-09 12:29:46 +05303539 uint8_t *base = (uint8_t *) fb_display->base;
Mao Flynn7b379f32015-04-20 00:28:30 +08003540 if (flash_read(ptn + LOGO_IMG_HEADER_SIZE, 0,
3541 (uint32_t *)base,
3542 ((((header->width * header->height * fb_display->bpp/8) + 511) >> 9) << 9))) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303543 fbcon_clear();
Vineet Bajaj99291ed2014-09-09 12:29:46 +05303544 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003545 return -1;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003546 }
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303547 }
3548
Mao Flynn7b379f32015-04-20 00:28:30 +08003549 return 0;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303550}
3551
Mao Flynn7b379f32015-04-20 00:28:30 +08003552int splash_screen_mmc()
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303553{
3554 int index = INVALID_PTN;
3555 unsigned long long ptn = 0;
3556 struct fbcon_config *fb_display = NULL;
Mao Flynn7b379f32015-04-20 00:28:30 +08003557 struct logo_img_header *header;
Mao Flynn1893a7f2015-06-03 12:03:36 +08003558 uint32_t blocksize, realsize, readsize;
3559 uint8_t *base;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303560
3561 index = partition_get_index("splash");
3562 if (index == 0) {
3563 dprintf(CRITICAL, "ERROR: splash Partition table not found\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003564 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303565 }
3566
3567 ptn = partition_get_offset(index);
3568 if (ptn == 0) {
3569 dprintf(CRITICAL, "ERROR: splash Partition invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003570 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303571 }
3572
Mao Flynn1893a7f2015-06-03 12:03:36 +08003573 mmc_set_lun(partition_get_lun(index));
3574
3575 blocksize = mmc_get_device_blocksize();
3576 if (blocksize == 0) {
3577 dprintf(CRITICAL, "ERROR:splash Partition invalid blocksize\n");
3578 return -1;
3579 }
3580
3581 fb_display = fbcon_display();
Channagoud Kadabidaf10a62015-07-22 11:51:55 -07003582 if (!fb_display)
3583 {
3584 dprintf(CRITICAL, "ERROR: fb config is not allocated\n");
3585 return -1;
3586 }
3587
Mao Flynn1893a7f2015-06-03 12:03:36 +08003588 base = (uint8_t *) fb_display->base;
3589
3590 if (mmc_read(ptn, (uint32_t *)(base + LOGO_IMG_OFFSET), blocksize)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303591 dprintf(CRITICAL, "ERROR: Cannot read splash image header\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003592 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303593 }
3594
Mao Flynn1893a7f2015-06-03 12:03:36 +08003595 header = (struct logo_img_header *)(base + LOGO_IMG_OFFSET);
Mao Flynn7b379f32015-04-20 00:28:30 +08003596 if (splash_screen_check_header(header)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303597 dprintf(CRITICAL, "ERROR: Splash image header invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003598 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303599 }
3600
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303601 if (fb_display) {
Mao Flynn1893a7f2015-06-03 12:03:36 +08003602 if (header->type && (header->blocks != 0)) { /* 1 RLE24 compressed data */
3603 base += LOGO_IMG_OFFSET;
Mao Flynn7b379f32015-04-20 00:28:30 +08003604
Mao Flynn1893a7f2015-06-03 12:03:36 +08003605 realsize = header->blocks * 512;
3606 readsize = ROUNDUP((realsize + LOGO_IMG_HEADER_SIZE), blocksize) - blocksize;
3607
3608 /* if the logo is not full-screen size, clean screen */
Mao Flynn7b379f32015-04-20 00:28:30 +08003609 if ((header->width != fb_display->width)
3610 || (header->height != fb_display->height))
3611 fbcon_clear();
3612
Mao Flynn1893a7f2015-06-03 12:03:36 +08003613 if (mmc_read(ptn + blocksize, (uint32_t *)(base + blocksize), readsize)) {
Mao Flynn7b379f32015-04-20 00:28:30 +08003614 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
3615 return -1;
3616 }
Mao Flynn7b379f32015-04-20 00:28:30 +08003617
Mao Flynn1893a7f2015-06-03 12:03:36 +08003618 fbcon_extract_to_screen(header, (base + LOGO_IMG_HEADER_SIZE));
3619 } else { /* 2 Raw BGR data */
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303620
Veera Sundaram Sankaranc33416f2015-09-28 10:25:55 -07003621 if ((header->width > fb_display->width) || (header->height > fb_display->height)) {
3622 dprintf(CRITICAL, "Logo config greater than fb config. Fall back default logo\n");
Mao Flynn1893a7f2015-06-03 12:03:36 +08003623 return -1;
3624 }
3625
3626 realsize = header->width * header->height * fb_display->bpp / 8;
3627 readsize = ROUNDUP((realsize + LOGO_IMG_HEADER_SIZE), blocksize) - blocksize;
3628
3629 if (blocksize == LOGO_IMG_HEADER_SIZE) { /* read the content directly */
3630 if (mmc_read((ptn + LOGO_IMG_HEADER_SIZE), (uint32_t *)base, readsize)) {
3631 fbcon_clear();
3632 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
3633 return -1;
3634 }
3635 } else {
3636 if (mmc_read(ptn + blocksize ,
3637 (uint32_t *)(base + LOGO_IMG_OFFSET + blocksize), readsize)) {
3638 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
3639 return -1;
3640 }
3641 memmove(base, (base + LOGO_IMG_OFFSET + LOGO_IMG_HEADER_SIZE), realsize);
3642 }
3643 }
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303644 }
3645
Mao Flynn7b379f32015-04-20 00:28:30 +08003646 return 0;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303647}
3648
Mao Flynn7b379f32015-04-20 00:28:30 +08003649int fetch_image_from_partition()
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303650{
3651 if (target_is_emmc_boot()) {
3652 return splash_screen_mmc();
3653 } else {
3654 return splash_screen_flash();
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003655 }
3656}
3657
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07003658/* Get the size from partiton name */
3659static void get_partition_size(const char *arg, char *response)
3660{
3661 uint64_t ptn = 0;
3662 uint64_t size;
3663 int index = INVALID_PTN;
3664
3665 index = partition_get_index(arg);
3666
3667 if (index == INVALID_PTN)
3668 {
3669 dprintf(CRITICAL, "Invalid partition index\n");
3670 return;
3671 }
3672
3673 ptn = partition_get_offset(index);
3674
3675 if(!ptn)
3676 {
3677 dprintf(CRITICAL, "Invalid partition name %s\n", arg);
3678 return;
3679 }
3680
3681 size = partition_get_size(index);
3682
3683 snprintf(response, MAX_RSP_SIZE, "\t 0x%llx", size);
3684 return;
3685}
3686
3687/*
3688 * Publish the partition type & size info
3689 * fastboot getvar will publish the required information.
3690 * fastboot getvar partition_size:<partition_name>: partition size in hex
3691 * fastboot getvar partition_type:<partition_name>: partition type (ext/fat)
3692 */
3693static void publish_getvar_partition_info(struct getvar_partition_info *info, uint8_t num_parts)
3694{
3695 uint8_t i;
3696
3697 for (i = 0; i < num_parts; i++) {
3698 get_partition_size(info[i].part_name, info[i].size_response);
3699
3700 if (strlcat(info[i].getvar_size, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
3701 {
3702 dprintf(CRITICAL, "partition size name truncated\n");
3703 return;
3704 }
3705 if (strlcat(info[i].getvar_type, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
3706 {
3707 dprintf(CRITICAL, "partition type name truncated\n");
3708 return;
3709 }
3710
3711 /* publish partition size & type info */
3712 fastboot_publish((const char *) info[i].getvar_size, (const char *) info[i].size_response);
3713 fastboot_publish((const char *) info[i].getvar_type, (const char *) info[i].type_response);
3714 }
3715}
3716
lijuang4ece1e72015-08-14 21:02:36 +08003717void get_product_name(unsigned char *buf)
3718{
3719 snprintf((char*)buf, MAX_RSP_SIZE, "%s", TARGET(BOARD));
3720 return;
3721}
3722
3723void get_bootloader_version(unsigned char *buf)
3724{
3725 snprintf((char*)buf, MAX_RSP_SIZE, "%s", device.bootloader_version);
3726 return;
3727}
3728
3729void get_baseband_version(unsigned char *buf)
3730{
3731 snprintf((char*)buf, MAX_RSP_SIZE, "%s", device.radio_version);
3732 return;
3733}
3734
3735bool is_device_locked()
3736{
3737 return device.is_unlocked ? false:true;
3738}
3739
Amol Jadi5edf3552013-07-23 14:15:34 -07003740/* register commands and variables for fastboot */
3741void aboot_fastboot_register_commands(void)
3742{
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003743 int i;
lijuangf16461c2015-08-03 17:09:34 +08003744 char hw_platform_buf[MAX_RSP_SIZE];
Amol Jadi5edf3552013-07-23 14:15:34 -07003745
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003746 struct fastboot_cmd_desc cmd_list[] = {
lijuang511a2b52015-08-14 20:50:51 +08003747 /* By default the enabled list is empty. */
3748 {"", NULL},
3749 /* move commands enclosed within the below ifndef to here
3750 * if they need to be enabled in user build.
3751 */
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003752#ifndef DISABLE_FASTBOOT_CMDS
lijuang511a2b52015-08-14 20:50:51 +08003753 /* Register the following commands only for non-user builds */
3754 {"flash:", cmd_flash},
3755 {"erase:", cmd_erase},
3756 {"boot", cmd_boot},
3757 {"continue", cmd_continue},
3758 {"reboot", cmd_reboot},
3759 {"reboot-bootloader", cmd_reboot_bootloader},
3760 {"oem unlock", cmd_oem_unlock},
3761 {"oem unlock-go", cmd_oem_unlock_go},
3762 {"oem lock", cmd_oem_lock},
3763 {"flashing unlock", cmd_oem_unlock},
3764 {"flashing lock", cmd_oem_lock},
3765 {"flashing lock_critical", cmd_flashing_lock_critical},
3766 {"flashing unlock_critical", cmd_flashing_unlock_critical},
3767 {"flashing get_unlock_ability", cmd_flashing_get_unlock_ability},
3768 {"oem device-info", cmd_oem_devinfo},
3769 {"preflash", cmd_preflash},
3770 {"oem enable-charger-screen", cmd_oem_enable_charger_screen},
3771 {"oem disable-charger-screen", cmd_oem_disable_charger_screen},
lijuanga25d8bb2015-09-16 13:11:52 +08003772 {"oem off-mode-charge", cmd_oem_off_mode_charger},
lijuang511a2b52015-08-14 20:50:51 +08003773 {"oem select-display-panel", cmd_oem_select_display_panel},
Channagoud Kadabi736c4962015-08-21 11:56:52 -07003774#if UNITTEST_FW_SUPPORT
Channagoud Kadabid5ddf482015-09-28 10:31:35 -07003775 {"oem run-tests", cmd_oem_runtests},
Channagoud Kadabi736c4962015-08-21 11:56:52 -07003776#endif
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003777#endif
lijuang511a2b52015-08-14 20:50:51 +08003778 };
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003779
3780 int fastboot_cmds_count = sizeof(cmd_list)/sizeof(cmd_list[0]);
3781 for (i = 1; i < fastboot_cmds_count; i++)
3782 fastboot_register(cmd_list[i].name,cmd_list[i].cb);
3783
Amol Jadi5edf3552013-07-23 14:15:34 -07003784 /* publish variables and their values */
3785 fastboot_publish("product", TARGET(BOARD));
3786 fastboot_publish("kernel", "lk");
3787 fastboot_publish("serialno", sn_buf);
3788
3789 /*
3790 * partition info is supported only for emmc partitions
3791 * Calling this for NAND prints some error messages which
3792 * is harmless but misleading. Avoid calling this for NAND
3793 * devices.
3794 */
3795 if (target_is_emmc_boot())
3796 publish_getvar_partition_info(part_info, ARRAY_SIZE(part_info));
3797
3798 /* Max download size supported */
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003799 snprintf(max_download_size, MAX_RSP_SIZE, "\t0x%x",
3800 target_get_max_flash_size());
Amol Jadi5edf3552013-07-23 14:15:34 -07003801 fastboot_publish("max-download-size", (const char *) max_download_size);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003802 /* Is the charger screen check enabled */
3803 snprintf(charger_screen_enabled, MAX_RSP_SIZE, "%d",
3804 device.charger_screen_enabled);
3805 fastboot_publish("charger-screen-enabled",
3806 (const char *) charger_screen_enabled);
lijuanga25d8bb2015-09-16 13:11:52 +08003807 fastboot_publish("off-mode-charge", (const char *) charger_screen_enabled);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05303808 snprintf(panel_display_mode, MAX_RSP_SIZE, "%s",
3809 device.display_panel);
3810 fastboot_publish("display-panel",
3811 (const char *) panel_display_mode);
Channagoud Kadabiaafbbfc2015-03-04 17:52:56 -08003812 fastboot_publish("version-bootloader", (const char *) device.bootloader_version);
3813 fastboot_publish("version-baseband", (const char *) device.radio_version);
lijuangf16461c2015-08-03 17:09:34 +08003814 fastboot_publish("secure", is_secure_boot_enable()? "yes":"no");
3815 smem_get_hw_platform_name((unsigned char *) hw_platform_buf, sizeof(hw_platform_buf));
3816 snprintf(get_variant, MAX_RSP_SIZE, "%s %s", hw_platform_buf,
3817 target_is_emmc_boot()? "eMMC":"UFS");
3818 fastboot_publish("variant", (const char *) get_variant);
lijuang65c5a822015-08-29 16:35:36 +08003819#if CHECK_BAT_VOLTAGE
lijuang102dfa92015-10-09 18:31:03 +08003820 update_battery_status();
lijuang65c5a822015-08-29 16:35:36 +08003821 fastboot_publish("battery-voltage", (const char *) battery_voltage);
lijuang102dfa92015-10-09 18:31:03 +08003822 fastboot_publish("battery-soc-ok", (const char *) battery_soc_ok);
lijuang65c5a822015-08-29 16:35:36 +08003823#endif
Amol Jadi5edf3552013-07-23 14:15:34 -07003824}
3825
Brian Swetland9c4c0752009-01-25 16:23:50 -08003826void aboot_init(const struct app_descriptor *app)
3827{
Shashank Mittal4f99a882010-02-01 13:58:50 -08003828 unsigned reboot_mode = 0;
Chandan Uddarajubedca152010-06-02 23:05:15 -07003829
lijuang39831732016-01-08 17:49:02 +08003830 /* Initialise wdog to catch early lk crashes */
3831#if WDOG_SUPPORT
3832 msm_wdog_init();
3833#endif
3834
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07003835 /* Setup page size information for nv storage */
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003836 if (target_is_emmc_boot())
3837 {
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07003838 page_size = mmc_page_size();
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003839 page_mask = page_size - 1;
Gaurav Nebhwani4d2389c2016-03-17 21:10:05 +05303840 mmc_blocksize = mmc_get_device_blocksize();
3841 mmc_blocksize_mask = mmc_blocksize - 1;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003842 }
3843 else
3844 {
3845 page_size = flash_page_size();
3846 page_mask = page_size - 1;
3847 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07003848 ASSERT((MEMBASE + MEMSIZE) > MEMBASE);
3849
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003850 read_device_info(&device);
vijay kumarc65876c2015-04-24 13:29:16 +05303851 read_allow_oem_unlock(&device);
Shashank Mittal162244e2011-08-08 19:01:25 -07003852
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08003853 /* Display splash screen if enabled */
3854#if DISPLAY_SPLASH_SCREEN
lijuang99c02d82015-02-13 19:04:34 +08003855#if NO_ALARM_DISPLAY
3856 if (!check_alarm_boot()) {
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08003857#endif
lijuang99c02d82015-02-13 19:04:34 +08003858 dprintf(SPEW, "Display Init: Start\n");
Ajay Singh Parmara7865a82015-04-16 21:27:52 -07003859#if DISPLAY_HDMI_PRIMARY
3860 if (!strlen(device.display_panel))
3861 strlcpy(device.display_panel, DISPLAY_PANEL_HDMI,
3862 sizeof(device.display_panel));
3863#endif
Channagoud Kadabi90869ce2015-04-27 11:15:14 -07003864#if ENABLE_WBC
Channagoud Kadabid801ac72015-08-26 11:21:51 -07003865 /* Wait if the display shutdown is in progress */
3866 while(pm_app_display_shutdown_in_prgs());
Channagoud Kadabi90869ce2015-04-27 11:15:14 -07003867 if (!pm_appsbl_display_init_done())
3868 target_display_init(device.display_panel);
3869 else
3870 display_image_on_screen();
3871#else
lijuang99c02d82015-02-13 19:04:34 +08003872 target_display_init(device.display_panel);
Channagoud Kadabi90869ce2015-04-27 11:15:14 -07003873#endif
lijuang99c02d82015-02-13 19:04:34 +08003874 dprintf(SPEW, "Display Init: Done\n");
3875#if NO_ALARM_DISPLAY
3876 }
3877#endif
3878#endif
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08003879
Greg Griscod6250552011-06-29 14:40:23 -07003880 target_serialno((unsigned char *) sn_buf);
Ajay Dudanib06c05f2011-05-12 14:46:10 -07003881 dprintf(SPEW,"serial number: %s\n",sn_buf);
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -08003882
Dhaval Patel223ec952013-07-18 14:49:44 -07003883 memset(display_panel_buf, '\0', MAX_PANEL_BUF_SIZE);
3884
Matthew Qindefd5562014-07-11 18:02:40 +08003885 /*
3886 * Check power off reason if user force reset,
3887 * if yes phone will do normal boot.
3888 */
3889 if (is_user_force_reset())
3890 goto normal_boot;
3891
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003892 /* Check if we should do something other than booting up */
Ameya Thakur0b9c2442013-05-10 13:22:56 -07003893 if (keys_get_state(KEY_VOLUMEUP) && keys_get_state(KEY_VOLUMEDOWN))
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07003894 {
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03003895 dprintf(ALWAYS,"dload mode key sequence detected\n");
lijuang395b5e62015-11-19 17:39:44 +08003896 reboot_device(EMERGENCY_DLOAD);
3897 dprintf(CRITICAL,"Failed to reboot into dload mode\n");
3898
Ameya Thakur0b9c2442013-05-10 13:22:56 -07003899 boot_into_fastboot = true;
3900 }
3901 if (!boot_into_fastboot)
3902 {
3903 if (keys_get_state(KEY_HOME) || keys_get_state(KEY_VOLUMEUP))
3904 boot_into_recovery = 1;
3905 if (!boot_into_recovery &&
3906 (keys_get_state(KEY_BACK) || keys_get_state(KEY_VOLUMEDOWN)))
Pavel Nedev5d91d412013-04-29 11:34:24 +03003907 boot_into_fastboot = true;
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07003908 }
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003909 #if NO_KEYPAD_DRIVER
Kinson Chik0b1c8162011-08-31 16:31:57 -07003910 if (fastboot_trigger())
Pavel Nedev5d91d412013-04-29 11:34:24 +03003911 boot_into_fastboot = true;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003912 #endif
Chandan Uddarajubedca152010-06-02 23:05:15 -07003913
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003914#if USE_PON_REBOOT_REG
3915 reboot_mode = check_hard_reboot_mode();
3916#else
Ajay Dudani77421292010-10-27 19:34:06 -07003917 reboot_mode = check_reboot_mode();
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003918#endif
3919 if (reboot_mode == RECOVERY_MODE)
3920 {
Ajay Dudani77421292010-10-27 19:34:06 -07003921 boot_into_recovery = 1;
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003922 }
3923 else if(reboot_mode == FASTBOOT_MODE)
3924 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03003925 boot_into_fastboot = true;
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003926 }
3927 else if(reboot_mode == ALARM_BOOT)
3928 {
Matthew Qind886f3c2014-01-17 16:52:01 +08003929 boot_reason_alarm = true;
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003930 }
Parth Dixit207573a2015-10-27 17:26:21 +05303931#if VERIFIED_BOOT
Parth Dixitddbc7352015-10-18 03:13:31 +05303932#if !VBOOT_MOTA
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003933 else if (reboot_mode == DM_VERITY_ENFORCING)
3934 {
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07003935 device.verity_mode = 1;
3936 write_device_info(&device);
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003937 }
3938 else if (reboot_mode == DM_VERITY_LOGGING)
3939 {
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07003940 device.verity_mode = 0;
3941 write_device_info(&device);
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003942 }
3943 else if (reboot_mode == DM_VERITY_KEYSCLEAR)
3944 {
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07003945 if(send_delete_keys_to_tz())
3946 ASSERT(0);
Ajay Dudani77421292010-10-27 19:34:06 -07003947 }
Parth Dixit207573a2015-10-27 17:26:21 +05303948#endif
Parth Dixitddbc7352015-10-18 03:13:31 +05303949#endif
Ajay Dudani77421292010-10-27 19:34:06 -07003950
Matthew Qindefd5562014-07-11 18:02:40 +08003951normal_boot:
Pavel Nedev5d91d412013-04-29 11:34:24 +03003952 if (!boot_into_fastboot)
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003953 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03003954 if (target_is_emmc_boot())
Shashank Mittala0032282011-08-26 14:50:11 -07003955 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03003956 if(emmc_recovery_init())
3957 dprintf(ALWAYS,"error in emmc_recovery_init\n");
3958 if(target_use_signed_kernel())
Shashank Mittala0032282011-08-26 14:50:11 -07003959 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03003960 if((device.is_unlocked) || (device.is_tampered))
3961 {
3962 #ifdef TZ_TAMPER_FUSE
3963 set_tamper_fuse_cmd();
3964 #endif
3965 #if USE_PCOM_SECBOOT
3966 set_tamper_flag(device.is_tampered);
3967 #endif
3968 }
Shashank Mittala0032282011-08-26 14:50:11 -07003969 }
Amit Blay6281ebc2015-01-11 14:44:08 +02003970
Pavel Nedev5d91d412013-04-29 11:34:24 +03003971 boot_linux_from_mmc();
Shashank Mittala0032282011-08-26 14:50:11 -07003972 }
Pavel Nedev5d91d412013-04-29 11:34:24 +03003973 else
3974 {
3975 recovery_init();
3976 #if USE_PCOM_SECBOOT
3977 if((device.is_unlocked) || (device.is_tampered))
3978 set_tamper_flag(device.is_tampered);
3979 #endif
3980 boot_linux_from_flash();
3981 }
3982 dprintf(CRITICAL, "ERROR: Could not do normal boot. Reverting "
3983 "to fastboot mode.\n");
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003984 }
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003985
Amol Jadi5edf3552013-07-23 14:15:34 -07003986 /* We are here means regular boot did not happen. Start fastboot. */
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07003987
Amol Jadi5edf3552013-07-23 14:15:34 -07003988 /* register aboot specific fastboot commands */
3989 aboot_fastboot_register_commands();
Amol Jadi57abe4c2011-05-24 15:47:27 -07003990
Amol Jadi5edf3552013-07-23 14:15:34 -07003991 /* dump partition table for debug info */
Kinson Chikf1a43512011-07-14 11:28:39 -07003992 partition_dump();
Amol Jadi5edf3552013-07-23 14:15:34 -07003993
3994 /* initialize and start fastboot */
3995 fastboot_init(target_get_scratch_address(), target_get_max_flash_size());
lijuang4ece1e72015-08-14 21:02:36 +08003996#if FBCON_DISPLAY_MSG
lijuang9a7d3b92015-11-30 14:41:24 +08003997 display_fastboot_menu();
lijuang4ece1e72015-08-14 21:02:36 +08003998#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08003999}
4000
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07004001uint32_t get_page_size()
4002{
4003 return page_size;
4004}
4005
Amir Samuelov57a6fa22013-06-05 16:36:43 +03004006/*
4007 * Calculated and save hash (SHA256) for non-signed boot image.
4008 *
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07004009 * @param image_addr - Boot image address
4010 * @param image_size - Size of the boot image
Amir Samuelov57a6fa22013-06-05 16:36:43 +03004011 *
4012 * @return int - 0 on success, negative value on failure.
4013 */
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07004014static int aboot_save_boot_hash_mmc(uint32_t image_addr, uint32_t image_size)
Amir Samuelov57a6fa22013-06-05 16:36:43 +03004015{
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07004016 unsigned int digest[8];
4017#if IMAGE_VERIF_ALGO_SHA1
4018 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA1;
4019#else
4020 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA256;
4021#endif
Amir Samuelov57a6fa22013-06-05 16:36:43 +03004022
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07004023 target_crypto_init_params();
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -08004024 hash_find((unsigned char *) image_addr, image_size, (unsigned char *)&digest, auth_algo);
Amir Samuelov57a6fa22013-06-05 16:36:43 +03004025
4026 save_kernel_hash_cmd(digest);
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07004027 dprintf(INFO, "aboot_save_boot_hash_mmc: imagesize_actual size %d bytes.\n", (int) image_size);
Amir Samuelov57a6fa22013-06-05 16:36:43 +03004028
4029 return 0;
4030}
4031
Brian Swetland9c4c0752009-01-25 16:23:50 -08004032APP_START(aboot)
4033 .init = aboot_init,
4034APP_END