blob: 92d5a9416d1a5d69b12395b37f290ed6ffa8ed80 [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
Vijay Kumar Pendotib3458362016-07-05 13:45:54 +05301613 if (UINT_MAX - page_size < imagesize_actual)
1614 {
1615 dprintf(CRITICAL,"Integer overflow detected in bootimage header fields %u %s\n", __LINE__,__func__);
1616 return -1;
1617 }
1618
1619 /*Check the availability of RAM before reading boot image + max signature length from flash*/
1620 if (target_get_max_flash_size() < (imagesize_actual + page_size))
1621 {
1622 dprintf(CRITICAL, "bootimage size is greater than DDR can hold\n");
1623 return -1;
1624 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07001625 /* Read image without signature */
1626 if (flash_read(ptn, offset, (void *)image_addr, imagesize_actual))
1627 {
1628 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
1629 return -1;
1630 }
Dima Zavin214cc642009-01-26 11:16:21 -08001631
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001632 dprintf(INFO, "Loading (%s) image (%d): done\n",
1633 (!boot_into_recovery ? "boot" : "recovery"), imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001634 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Amol Jadib6be5c12012-11-14 13:39:51 -08001635
Shashank Mittalcd98d472011-08-02 14:29:24 -07001636 offset = imagesize_actual;
1637 /* Read signature */
1638 if (flash_read(ptn, offset, (void *)(image_addr + offset), page_size))
1639 {
1640 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001641 return -1;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001642 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07001643
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301644 verify_signed_bootimg((uint32_t)image_addr, imagesize_actual);
Shashank Mittalcd98d472011-08-02 14:29:24 -07001645
1646 /* Move kernel and ramdisk to correct address */
Matthew Qinbb7923d2015-02-09 10:56:09 +08001647 memmove((void*) hdr->kernel_addr, (char*) (image_addr + page_size), hdr->kernel_size);
1648 memmove((void*) hdr->ramdisk_addr, (char*) (image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001649#if DEVICE_TREE
vijay kumar8f53e362015-11-24 13:38:11 +05301650 if(hdr->dt_size != 0) {
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001651
vijay kumar8f53e362015-11-24 13:38:11 +05301652 dt_table_offset = ((uint32_t)image_addr + page_size + kernel_actual + ramdisk_actual + second_actual);
1653
1654 table = (struct dt_table*) dt_table_offset;
1655
1656 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0){
1657 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1658 return -1;
1659 }
1660
1661 /* Find index of device tree within device tree table */
1662 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
1663 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1664 return -1;
1665 }
1666
1667 /* Validate and Read device device tree in the "tags_add */
1668 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size)){
1669 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1670 return -1;
1671 }
1672
1673 best_match_dt_addr = (unsigned char *)table + dt_entry.offset;
1674 dtb_size = dt_entry.size;
1675 memmove((void *)hdr->tags_addr, (char *)best_match_dt_addr, dtb_size);
1676 }
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001677#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001678
1679 /* Make sure everything from scratch address is read before next step!*/
Shashank Mittala0032282011-08-26 14:50:11 -07001680 if(device.is_tampered)
Shashank Mittal162244e2011-08-08 19:01:25 -07001681 {
1682 write_device_info_flash(&device);
1683 }
Channagoud Kadabi5c86fe32012-02-16 10:58:48 +05301684#if USE_PCOM_SECBOOT
1685 set_tamper_flag(device.is_tampered);
1686#endif
Shashank Mittalcd98d472011-08-02 14:29:24 -07001687 }
1688 else
1689 {
Shashank Mittal162244e2011-08-08 19:01:25 -07001690 offset = page_size;
1691
Amol Jadib6be5c12012-11-14 13:39:51 -08001692 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1693 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1694 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
1695
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001696 dprintf(INFO, "Loading (%s) image (%d): start\n",
1697 (!boot_into_recovery ? "boot" : "recovery"), kernel_actual + ramdisk_actual);
1698
Amol Jadi492d5a52013-03-15 16:12:34 -07001699 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -08001700
1701 if (flash_read(ptn, offset, (void *)hdr->kernel_addr, kernel_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -07001702 dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
1703 return -1;
1704 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001705 offset += kernel_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001706
Amol Jadib6be5c12012-11-14 13:39:51 -08001707 if (flash_read(ptn, offset, (void *)hdr->ramdisk_addr, ramdisk_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -07001708 dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
1709 return -1;
1710 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001711 offset += ramdisk_actual;
1712
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001713 dprintf(INFO, "Loading (%s) image (%d): done\n",
1714 (!boot_into_recovery ? "boot" : "recovery"), kernel_actual + ramdisk_actual);
1715
Amol Jadi492d5a52013-03-15 16:12:34 -07001716 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001717
1718 if(hdr->second_size != 0) {
Amol Jadib6be5c12012-11-14 13:39:51 -08001719 offset += second_actual;
1720 /* Second image loading not implemented. */
1721 ASSERT(0);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001722 }
1723
1724#if DEVICE_TREE
1725 if(hdr->dt_size != 0) {
1726
1727 /* Read the device tree table into buffer */
1728 if(flash_read(ptn, offset, (void *) dt_buf, page_size)) {
1729 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
1730 return -1;
1731 }
1732
1733 table = (struct dt_table*) dt_buf;
1734
Deepa Dinamani19648b42013-09-05 17:05:55 -07001735 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001736 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1737 return -1;
1738 }
1739
P.V. Phani Kumar82916762016-03-09 09:20:19 +05301740 /* Its Error if, dt_hdr_size (table->num_entries * dt_entry size + Dev_Tree Header)
1741 goes beyound hdr->dt_size*/
1742 if (dt_hdr_size > ROUND_TO_PAGE(hdr->dt_size,hdr->page_size)) {
1743 dprintf(CRITICAL, "ERROR: Invalid Device Tree size \n");
1744 return -1;
1745 }
1746
Deepa Dinamani19648b42013-09-05 17:05:55 -07001747 table = (struct dt_table*) memalign(CACHE_LINE, dt_hdr_size);
1748 if (!table)
1749 return -1;
1750
1751 /* Read the entire device tree table into buffer */
1752 if(flash_read(ptn, offset, (void *)table, dt_hdr_size)) {
1753 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
1754 return -1;
1755 }
1756
1757
Joel Kingaa335dc2013-06-03 16:11:08 -07001758 /* Find index of device tree within device tree table */
1759 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001760 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1761 return -1;
1762 }
1763
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001764 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001765 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001766 {
1767 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1768 return -1;
1769 }
1770
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001771 /* Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001772 if(flash_read(ptn, offset + dt_entry.offset,
1773 (void *)hdr->tags_addr, dt_entry.size)) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001774 dprintf(CRITICAL, "ERROR: Cannot read device tree\n");
1775 return -1;
1776 }
1777 }
1778#endif
1779
Shashank Mittalcd98d472011-08-02 14:29:24 -07001780 }
David Ng183a7422009-12-07 14:55:21 -08001781continue_boot:
Dima Zavin214cc642009-01-26 11:16:21 -08001782
Dima Zavin214cc642009-01-26 11:16:21 -08001783 /* TODO: create/pass atags to kernel */
1784
Ajay Dudanie28a6072011-07-01 13:59:46 -07001785 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07001786 (const char *)hdr->cmdline, board_machtype(),
Dima Zavin214cc642009-01-26 11:16:21 -08001787 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
1788
1789 return 0;
1790}
Brian Swetland9c4c0752009-01-25 16:23:50 -08001791
Shashank Mittal162244e2011-08-08 19:01:25 -07001792void write_device_info_mmc(device_info *dev)
1793{
Shashank Mittal162244e2011-08-08 19:01:25 -07001794 unsigned long long ptn = 0;
1795 unsigned long long size;
1796 int index = INVALID_PTN;
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001797 uint32_t blocksize;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001798 uint8_t lun = 0;
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001799 uint32_t ret = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001800
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001801 if (devinfo_present)
1802 index = partition_get_index("devinfo");
1803 else
1804 index = partition_get_index("aboot");
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001805
Shashank Mittal162244e2011-08-08 19:01:25 -07001806 ptn = partition_get_offset(index);
1807 if(ptn == 0)
1808 {
1809 return;
1810 }
1811
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001812 lun = partition_get_lun(index);
1813 mmc_set_lun(lun);
1814
Shashank Mittal162244e2011-08-08 19:01:25 -07001815 size = partition_get_size(index);
1816
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001817 blocksize = mmc_get_device_blocksize();
1818
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001819 if (devinfo_present)
1820 ret = mmc_write(ptn, blocksize, (void *)info_buf);
1821 else
1822 ret = mmc_write((ptn + size - blocksize), blocksize, (void *)info_buf);
1823 if (ret)
Shashank Mittal162244e2011-08-08 19:01:25 -07001824 {
1825 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
Sridhar Parasurame1a97922015-07-27 15:31:22 -07001826 ASSERT(0);
Shashank Mittal162244e2011-08-08 19:01:25 -07001827 }
1828}
1829
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001830void read_device_info_mmc(struct device_info *info)
Shashank Mittal162244e2011-08-08 19:01:25 -07001831{
Shashank Mittal162244e2011-08-08 19:01:25 -07001832 unsigned long long ptn = 0;
1833 unsigned long long size;
1834 int index = INVALID_PTN;
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001835 uint32_t blocksize;
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001836 uint32_t ret = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001837
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001838 if ((index = partition_get_index("devinfo")) < 0)
1839 {
1840 devinfo_present = false;
1841 index = partition_get_index("aboot");
1842 }
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001843
Shashank Mittal162244e2011-08-08 19:01:25 -07001844 ptn = partition_get_offset(index);
1845 if(ptn == 0)
1846 {
1847 return;
1848 }
1849
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001850 mmc_set_lun(partition_get_lun(index));
1851
Shashank Mittal162244e2011-08-08 19:01:25 -07001852 size = partition_get_size(index);
1853
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001854 blocksize = mmc_get_device_blocksize();
1855
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001856 if (devinfo_present)
1857 ret = mmc_read(ptn, (void *)info_buf, blocksize);
1858 else
1859 ret = mmc_read((ptn + size - blocksize), (void *)info_buf, blocksize);
1860 if (ret)
Shashank Mittal162244e2011-08-08 19:01:25 -07001861 {
1862 dprintf(CRITICAL, "ERROR: Cannot read device info\n");
Sridhar Parasurame1a97922015-07-27 15:31:22 -07001863 ASSERT(0);
Shashank Mittal162244e2011-08-08 19:01:25 -07001864 }
Shashank Mittal162244e2011-08-08 19:01:25 -07001865}
1866
1867void write_device_info_flash(device_info *dev)
1868{
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001869 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
Shashank Mittal162244e2011-08-08 19:01:25 -07001870 struct ptentry *ptn;
1871 struct ptable *ptable;
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001872 if(info == NULL)
1873 {
1874 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
1875 ASSERT(0);
1876 }
1877 info_buf = info;
Shashank Mittal162244e2011-08-08 19:01:25 -07001878 ptable = flash_get_ptable();
1879 if (ptable == NULL)
1880 {
1881 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1882 return;
1883 }
1884
1885 ptn = ptable_find(ptable, "devinfo");
1886 if (ptn == NULL)
1887 {
Smita Ghosh670c8b82015-05-07 09:30:03 -07001888 dprintf(CRITICAL, "ERROR: No devinfo partition found\n");
Shashank Mittal162244e2011-08-08 19:01:25 -07001889 return;
1890 }
1891
1892 memcpy(info, dev, sizeof(device_info));
1893
1894 if (flash_write(ptn, 0, (void *)info_buf, page_size))
1895 {
1896 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1897 return;
1898 }
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001899 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07001900}
1901
vijay kumarc65876c2015-04-24 13:29:16 +05301902static int read_allow_oem_unlock(device_info *dev)
1903{
vijay kumarc65876c2015-04-24 13:29:16 +05301904 unsigned offset;
1905 int index;
1906 unsigned long long ptn;
1907 unsigned long long ptn_size;
1908 unsigned blocksize = mmc_get_device_blocksize();
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07001909 STACKBUF_DMA_ALIGN(buf, blocksize);
vijay kumarc65876c2015-04-24 13:29:16 +05301910
vijay kumarca2e6812015-07-08 20:28:25 +05301911 index = partition_get_index(frp_ptns[0]);
vijay kumarc65876c2015-04-24 13:29:16 +05301912 if (index == INVALID_PTN)
1913 {
vijay kumarca2e6812015-07-08 20:28:25 +05301914 index = partition_get_index(frp_ptns[1]);
1915 if (index == INVALID_PTN)
1916 {
1917 dprintf(CRITICAL, "Neither '%s' nor '%s' partition found\n", frp_ptns[0],frp_ptns[1]);
1918 return -1;
1919 }
vijay kumarc65876c2015-04-24 13:29:16 +05301920 }
1921
1922 ptn = partition_get_offset(index);
1923 ptn_size = partition_get_size(index);
1924 offset = ptn_size - blocksize;
1925
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07001926 if (mmc_read(ptn + offset, (void *)buf, blocksize))
vijay kumarc65876c2015-04-24 13:29:16 +05301927 {
1928 dprintf(CRITICAL, "Reading MMC failed\n");
1929 return -1;
1930 }
1931
1932 /*is_allow_unlock is a bool value stored at the LSB of last byte*/
1933 is_allow_unlock = buf[blocksize-1] & 0x01;
1934 return 0;
1935}
1936
1937static int write_allow_oem_unlock(bool allow_unlock)
1938{
vijay kumarc65876c2015-04-24 13:29:16 +05301939 unsigned offset;
vijay kumarc65876c2015-04-24 13:29:16 +05301940 int index;
1941 unsigned long long ptn;
1942 unsigned long long ptn_size;
1943 unsigned blocksize = mmc_get_device_blocksize();
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07001944 STACKBUF_DMA_ALIGN(buf, blocksize);
vijay kumarc65876c2015-04-24 13:29:16 +05301945
vijay kumarca2e6812015-07-08 20:28:25 +05301946 index = partition_get_index(frp_ptns[0]);
vijay kumarc65876c2015-04-24 13:29:16 +05301947 if (index == INVALID_PTN)
1948 {
vijay kumarca2e6812015-07-08 20:28:25 +05301949 index = partition_get_index(frp_ptns[1]);
1950 if (index == INVALID_PTN)
1951 {
1952 dprintf(CRITICAL, "Neither '%s' nor '%s' partition found\n", frp_ptns[0],frp_ptns[1]);
1953 return -1;
1954 }
vijay kumarc65876c2015-04-24 13:29:16 +05301955 }
1956
1957 ptn = partition_get_offset(index);
1958 ptn_size = partition_get_size(index);
1959 offset = ptn_size - blocksize;
1960
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07001961 if (mmc_read(ptn + offset, (void *)buf, blocksize))
vijay kumarc65876c2015-04-24 13:29:16 +05301962 {
1963 dprintf(CRITICAL, "Reading MMC failed\n");
1964 return -1;
1965 }
1966
1967 /*is_allow_unlock is a bool value stored at the LSB of last byte*/
1968 buf[blocksize-1] = allow_unlock;
1969 if (mmc_write(ptn + offset, blocksize, buf))
1970 {
1971 dprintf(CRITICAL, "Writing MMC failed\n");
1972 return -1;
1973 }
1974
1975 return 0;
1976}
1977
Shashank Mittal162244e2011-08-08 19:01:25 -07001978void read_device_info_flash(device_info *dev)
1979{
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001980 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
Shashank Mittal162244e2011-08-08 19:01:25 -07001981 struct ptentry *ptn;
1982 struct ptable *ptable;
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001983 if(info == NULL)
1984 {
1985 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
1986 ASSERT(0);
1987 }
1988 info_buf = info;
Shashank Mittal162244e2011-08-08 19:01:25 -07001989 ptable = flash_get_ptable();
1990 if (ptable == NULL)
1991 {
1992 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1993 return;
1994 }
1995
1996 ptn = ptable_find(ptable, "devinfo");
1997 if (ptn == NULL)
1998 {
Smita Ghosh670c8b82015-05-07 09:30:03 -07001999 dprintf(CRITICAL, "ERROR: No devinfo partition found\n");
Shashank Mittal162244e2011-08-08 19:01:25 -07002000 return;
2001 }
2002
2003 if (flash_read(ptn, 0, (void *)info_buf, page_size))
2004 {
2005 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
2006 return;
2007 }
2008
2009 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
2010 {
Shashank Mittal162244e2011-08-08 19:01:25 -07002011 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
2012 info->is_unlocked = 0;
Shashank Mittala0032282011-08-26 14:50:11 -07002013 info->is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07002014 write_device_info_flash(info);
2015 }
2016 memcpy(dev, info, sizeof(device_info));
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002017 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07002018}
2019
2020void write_device_info(device_info *dev)
2021{
2022 if(target_is_emmc_boot())
2023 {
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002024 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
2025 if(info == NULL)
2026 {
2027 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
2028 ASSERT(0);
2029 }
2030 info_buf = info;
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002031 memcpy(info, dev, sizeof(struct device_info));
2032
2033#if USE_RPMB_FOR_DEVINFO
Sridhar Parasurame1a97922015-07-27 15:31:22 -07002034 if (is_secure_boot_enable()) {
2035 if((write_device_info_rpmb((void*) info, PAGE_SIZE)) < 0)
2036 ASSERT(0);
2037 }
Channagoud Kadabic80cb492015-04-28 16:08:28 -07002038 else
2039 write_device_info_mmc(info);
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002040#else
2041 write_device_info_mmc(info);
2042#endif
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002043 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07002044 }
2045 else
2046 {
2047 write_device_info_flash(dev);
2048 }
2049}
2050
2051void read_device_info(device_info *dev)
2052{
2053 if(target_is_emmc_boot())
2054 {
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002055 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
2056 if(info == NULL)
2057 {
2058 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
2059 ASSERT(0);
2060 }
2061 info_buf = info;
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002062
2063#if USE_RPMB_FOR_DEVINFO
Sridhar Parasurame1a97922015-07-27 15:31:22 -07002064 if (is_secure_boot_enable()) {
2065 if((read_device_info_rpmb((void*) info, PAGE_SIZE)) < 0)
2066 ASSERT(0);
2067 }
Channagoud Kadabic80cb492015-04-28 16:08:28 -07002068 else
2069 read_device_info_mmc(info);
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002070#else
2071 read_device_info_mmc(info);
2072#endif
2073
2074 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
2075 {
2076 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
lijuang511a2b52015-08-14 20:50:51 +08002077 if (is_secure_boot_enable()) {
Channagoud Kadabi05f78ba2015-07-06 11:58:14 -07002078 info->is_unlocked = 0;
Parth Dixitddbc7352015-10-18 03:13:31 +05302079#if !VBOOT_MOTA
lijuang511a2b52015-08-14 20:50:51 +08002080 info->is_unlock_critical = 0;
Parth Dixitddbc7352015-10-18 03:13:31 +05302081#endif
lijuang511a2b52015-08-14 20:50:51 +08002082 } else {
Channagoud Kadabi2fda4092015-07-07 13:34:11 -07002083 info->is_unlocked = 1;
Parth Dixitddbc7352015-10-18 03:13:31 +05302084#if !VBOOT_MOTA
lijuang511a2b52015-08-14 20:50:51 +08002085 info->is_unlock_critical = 1;
Parth Dixitddbc7352015-10-18 03:13:31 +05302086#endif
lijuang511a2b52015-08-14 20:50:51 +08002087 }
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002088 info->is_tampered = 0;
2089 info->charger_screen_enabled = 0;
Parth Dixitddbc7352015-10-18 03:13:31 +05302090#if !VBOOT_MOTA
Sridhar Parasuram287e5062015-08-24 16:17:22 -07002091 info->verity_mode = 1; //enforcing by default
Parth Dixitddbc7352015-10-18 03:13:31 +05302092#endif
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002093 write_device_info(info);
2094 }
2095 memcpy(dev, info, sizeof(device_info));
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002096 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07002097 }
2098 else
2099 {
2100 read_device_info_flash(dev);
2101 }
2102}
2103
2104void reset_device_info()
2105{
2106 dprintf(ALWAYS, "reset_device_info called.");
Shashank Mittala0032282011-08-26 14:50:11 -07002107 device.is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07002108 write_device_info(&device);
2109}
2110
2111void set_device_root()
2112{
2113 dprintf(ALWAYS, "set_device_root called.");
Shashank Mittala0032282011-08-26 14:50:11 -07002114 device.is_tampered = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -07002115 write_device_info(&device);
2116}
2117
lijuang4ece1e72015-08-14 21:02:36 +08002118/* set device unlock value
2119 * Must check FRP before call this function
2120 * Need to wipe data when unlock status changed
2121 * type 0: oem unlock
2122 * type 1: unlock critical
2123 * status 0: unlock as false
2124 * status 1: lock as true
2125 */
2126void set_device_unlock_value(int type, bool status)
lijuang21f12f52015-08-22 16:22:19 +08002127{
lijuang4ece1e72015-08-14 21:02:36 +08002128 if (type == UNLOCK)
2129 device.is_unlocked = status;
Parth Dixitddbc7352015-10-18 03:13:31 +05302130#if !VBOOT_MOTA
lijuang4ece1e72015-08-14 21:02:36 +08002131 else if (type == UNLOCK_CRITICAL)
2132 device.is_unlock_critical = status;
Parth Dixitddbc7352015-10-18 03:13:31 +05302133#endif
lijuang4ece1e72015-08-14 21:02:36 +08002134 write_device_info(&device);
2135}
2136
2137static void set_device_unlock(int type, bool status)
2138{
2139 int is_unlocked = -1;
2140 char response[MAX_RSP_SIZE];
2141
2142 /* check device unlock status if it is as expected */
2143 if (type == UNLOCK)
2144 is_unlocked = device.is_unlocked;
Parth Dixitddbc7352015-10-18 03:13:31 +05302145#if !VBOOT_MOTA
lijuang4ece1e72015-08-14 21:02:36 +08002146 else if (type == UNLOCK_CRITICAL)
2147 is_unlocked = device.is_unlock_critical;
Parth Dixitddbc7352015-10-18 03:13:31 +05302148#endif
lijuang4ece1e72015-08-14 21:02:36 +08002149 if (is_unlocked == status) {
2150 snprintf(response, sizeof(response), "\tDevice already : %s", (status ? "unlocked!" : "locked!"));
2151 fastboot_info(response);
2152 fastboot_okay("");
2153 return;
2154 }
2155
2156 /* status is true, it means to unlock device */
2157 if (status) {
lijuang21f12f52015-08-22 16:22:19 +08002158 if(!is_allow_unlock) {
2159 fastboot_fail("oem unlock is not allowed");
2160 return;
2161 }
2162
lijuang4ece1e72015-08-14 21:02:36 +08002163#if FBCON_DISPLAY_MSG
lijuang9a7d3b92015-11-30 14:41:24 +08002164 display_unlock_menu(type);
lijuang4ece1e72015-08-14 21:02:36 +08002165 fastboot_okay("");
2166 return;
2167#else
2168 if (type == UNLOCK) {
2169 fastboot_fail("Need wipe userdata. Do 'fastboot oem unlock-go'");
2170 return;
2171 }
2172#endif
lijuang21f12f52015-08-22 16:22:19 +08002173 }
lijuang4ece1e72015-08-14 21:02:36 +08002174
2175 set_device_unlock_value(type, status);
2176
2177 /* wipe data */
2178 struct recovery_message msg;
2179
2180 snprintf(msg.recovery, sizeof(msg.recovery), "recovery\n--wipe_data");
2181 write_misc(0, &msg, sizeof(msg));
2182
2183 fastboot_okay("");
2184 reboot_device(RECOVERY_MODE);
lijuang21f12f52015-08-22 16:22:19 +08002185}
2186
lijuang511a2b52015-08-14 20:50:51 +08002187static bool critical_flash_allowed(const char * entry)
2188{
2189 uint32_t i = 0;
2190 if (entry == NULL)
2191 return false;
2192
2193 for (i = 0; i < ARRAY_SIZE(critical_flash_allowed_ptn); i++) {
2194 if(!strcmp(entry, critical_flash_allowed_ptn[i]))
2195 return true;
2196 }
2197 return false;
Matthew Qin271927e2015-03-31 22:07:07 -04002198}
2199
2200#if DEVICE_TREE
Amol Jadicb524072012-08-09 16:40:18 -07002201int copy_dtb(uint8_t *boot_image_start, unsigned int scratch_offset)
2202{
2203 uint32 dt_image_offset = 0;
Amol Jadicb524072012-08-09 16:40:18 -07002204 uint32_t n;
Amol Jadicb524072012-08-09 16:40:18 -07002205 struct dt_table *table;
2206 struct dt_entry dt_entry;
2207 uint32_t dt_hdr_size;
2208 unsigned int compressed_size = 0;
2209 unsigned int dtb_size = 0;
2210 unsigned int out_avai_len = 0;
2211 unsigned char *out_addr = NULL;
2212 unsigned char *best_match_dt_addr = NULL;
2213 int rc;
2214
2215 struct boot_img_hdr *hdr = (struct boot_img_hdr *) (boot_image_start);
2216
2217 if(hdr->dt_size != 0) {
2218 /* add kernel offset */
2219 dt_image_offset += page_size;
2220 n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07002221 dt_image_offset += n;
Amol Jadicb524072012-08-09 16:40:18 -07002222
Deepa Dinamani19648b42013-09-05 17:05:55 -07002223 /* add ramdisk offset */
Amol Jadicb524072012-08-09 16:40:18 -07002224 n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
2225 dt_image_offset += n;
2226
Joel Kingaa335dc2013-06-03 16:11:08 -07002227 /* add second offset */
2228 if(hdr->second_size != 0) {
Amol Jadicb524072012-08-09 16:40:18 -07002229 n = ROUND_TO_PAGE(hdr->second_size, page_mask);
2230 dt_image_offset += n;
2231 }
2232
Matthew Qin271927e2015-03-31 22:07:07 -04002233 /* offset now point to start of dt.img */
2234 table = (struct dt_table*)(boot_image_start + dt_image_offset);
2235
2236 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
2237 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
Matthew Qin0b15b322015-05-19 05:20:54 -04002238 return -1;
Matthew Qin271927e2015-03-31 22:07:07 -04002239 }
P.V. Phani Kumar82916762016-03-09 09:20:19 +05302240
2241 /* Its Error if, dt_hdr_size (table->num_entries * dt_entry size + Dev_Tree Header)
2242 goes beyound hdr->dt_size*/
2243 if (dt_hdr_size > ROUND_TO_PAGE(hdr->dt_size,hdr->page_size)) {
2244 dprintf(CRITICAL, "ERROR: Invalid Device Tree size \n");
2245 return -1;
2246 }
2247
Amol Jadicb524072012-08-09 16:40:18 -07002248 /* Find index of device tree within device tree table */
2249 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
2250 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
2251 return -1;
2252 }
2253
Matthew Qin271927e2015-03-31 22:07:07 -04002254 best_match_dt_addr = (unsigned char *)boot_image_start + dt_image_offset + dt_entry.offset;
2255 if (is_gzip_package(best_match_dt_addr, dt_entry.size))
2256 {
2257 out_addr = (unsigned char *)target_get_scratch_address() + scratch_offset;
2258 out_avai_len = target_get_max_flash_size() - scratch_offset;
2259 dprintf(INFO, "decompressing dtb: start\n");
2260 rc = decompress(best_match_dt_addr,
2261 dt_entry.size, out_addr, out_avai_len,
2262 &compressed_size, &dtb_size);
2263 if (rc)
2264 {
Matthew Qin0b15b322015-05-19 05:20:54 -04002265 dprintf(CRITICAL, "decompressing dtb failed!!!\n");
Matthew Qin271927e2015-03-31 22:07:07 -04002266 ASSERT(0);
2267 }
2268
Matthew Qin0b15b322015-05-19 05:20:54 -04002269 dprintf(INFO, "decompressing dtb: done\n");
Matthew Qin271927e2015-03-31 22:07:07 -04002270 best_match_dt_addr = out_addr;
2271 } else {
2272 dtb_size = dt_entry.size;
2273 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002274 /* Validate and Read device device tree in the "tags_add */
Matthew Qin271927e2015-03-31 22:07:07 -04002275 if (check_aboot_addr_range_overlap(hdr->tags_addr, dtb_size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002276 {
2277 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
2278 return -1;
2279 }
2280
Amol Jadicb524072012-08-09 16:40:18 -07002281 /* Read device device tree in the "tags_add */
Matthew Qin271927e2015-03-31 22:07:07 -04002282 memmove((void*) hdr->tags_addr, (void *)best_match_dt_addr, dtb_size);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002283 } else
2284 return -1;
Amol Jadicb524072012-08-09 16:40:18 -07002285
2286 /* Everything looks fine. Return success. */
2287 return 0;
2288}
2289#endif
2290
Brian Swetland9c4c0752009-01-25 16:23:50 -08002291void cmd_boot(const char *arg, void *data, unsigned sz)
2292{
Amit Blay8a510302015-08-17 09:20:01 +03002293#ifdef MDTP_SUPPORT
2294 static bool is_mdtp_activated = 0;
2295#endif /* MDTP_SUPPORT */
Brian Swetland9c4c0752009-01-25 16:23:50 -08002296 unsigned kernel_actual;
2297 unsigned ramdisk_actual;
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002298 uint32_t image_actual;
2299 uint32_t dt_actual = 0;
P.V. Phani Kumareecfd822016-03-09 20:09:03 +05302300 uint32_t sig_actual = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08002301 struct boot_img_hdr *hdr = NULL;
2302 struct kernel64_hdr *kptr = NULL;
Brian Swetland9c4c0752009-01-25 16:23:50 -08002303 char *ptr = ((char*) data);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002304 int ret = 0;
2305 uint8_t dtb_copied = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08002306 unsigned int out_len = 0;
2307 unsigned int out_avai_len = 0;
2308 unsigned char *out_addr = NULL;
2309 uint32_t dtb_offset = 0;
2310 unsigned char *kernel_start_addr = NULL;
2311 unsigned int kernel_size = 0;
Matthew Qin271927e2015-03-31 22:07:07 -04002312 unsigned int scratch_offset = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08002313
lijuang2008ff22016-03-07 17:56:27 +08002314#if FBCON_DISPLAY_MSG
2315 /* Exit keys' detection thread firstly */
2316 exit_menu_keys_detection();
2317#endif
2318
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002319#if VERIFIED_BOOT
Channagoud Kadabi6d5375e2015-06-23 17:15:42 -07002320 if(target_build_variant_user() && !device.is_unlocked)
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002321 {
2322 fastboot_fail("unlock device to use this command");
lijuang2008ff22016-03-07 17:56:27 +08002323 goto boot_failed;
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002324 }
2325#endif
2326
Brian Swetland9c4c0752009-01-25 16:23:50 -08002327 if (sz < sizeof(hdr)) {
2328 fastboot_fail("invalid bootimage header");
lijuang2008ff22016-03-07 17:56:27 +08002329 goto boot_failed;
Brian Swetland9c4c0752009-01-25 16:23:50 -08002330 }
2331
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002332 hdr = (struct boot_img_hdr *)data;
Brian Swetland9c4c0752009-01-25 16:23:50 -08002333
2334 /* ensure commandline is terminated */
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002335 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -08002336
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002337 if(target_is_emmc_boot() && hdr->page_size) {
2338 page_size = hdr->page_size;
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07002339 page_mask = page_size - 1;
2340 }
2341
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002342 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
2343 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002344#if DEVICE_TREE
2345 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
2346#endif
2347
2348 image_actual = ADD_OF(page_size, kernel_actual);
2349 image_actual = ADD_OF(image_actual, ramdisk_actual);
2350 image_actual = ADD_OF(image_actual, dt_actual);
2351
P.V. Phani Kumareecfd822016-03-09 20:09:03 +05302352 if (target_use_signed_kernel() && (!device.is_unlocked)) {
2353 /* Calculate the signature length from boot image */
2354 sig_actual = read_der_message_length(
2355 (unsigned char*)(data + image_actual),sz);
Channagoud Kadabi3d839012014-06-26 14:26:39 -07002356 image_actual = ADD_OF(image_actual, sig_actual);
P.V. Phani Kumareecfd822016-03-09 20:09:03 +05302357 }
Channagoud Kadabi3d839012014-06-26 14:26:39 -07002358
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002359 /* sz should have atleast raw boot image */
2360 if (image_actual > sz) {
Channagoud Kadabi3d839012014-06-26 14:26:39 -07002361 fastboot_fail("bootimage: incomplete or not signed");
lijuang2008ff22016-03-07 17:56:27 +08002362 goto boot_failed;
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002363 }
2364
Sridhar Parasuram361e4cd2015-09-24 11:19:19 -07002365 // Initialize boot state before trying to verify boot.img
2366#if VERIFIED_BOOT
Channagoud Kadabi699466e2015-11-03 12:37:42 -08002367 boot_verifier_init();
Channagoud Kadabibf6ce7d2015-09-17 13:25:35 -07002368 /* Handle overflow if the input image size is greater than
2369 * boot image buffer can hold
2370 */
2371 if ((target_get_max_flash_size() - (image_actual - sig_actual)) < page_size)
2372 {
2373 fastboot_fail("booimage: size is greater than boot image buffer can hold");
lijuang2008ff22016-03-07 17:56:27 +08002374 goto boot_failed;
Channagoud Kadabibf6ce7d2015-09-17 13:25:35 -07002375 }
Sridhar Parasuram361e4cd2015-09-24 11:19:19 -07002376#endif
2377
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002378 /* Verify the boot image
2379 * device & page_size are initialized in aboot_init
2380 */
Sridhar Parasuram7ae9fbc2015-09-17 09:56:30 -07002381 if (target_use_signed_kernel() && (!device.is_unlocked)) {
Channagoud Kadabi3d839012014-06-26 14:26:39 -07002382 /* Pass size excluding signature size, otherwise we would try to
2383 * access signature beyond its length
2384 */
2385 verify_signed_bootimg((uint32_t)data, (image_actual - sig_actual));
Sridhar Parasuram7ae9fbc2015-09-17 09:56:30 -07002386 }
Amit Blay4aa292f2015-04-28 21:55:59 +03002387#ifdef MDTP_SUPPORT
2388 else
2389 {
Shay Nachmani062aa3f2015-05-17 17:28:44 +03002390 /* fastboot boot is not allowed when MDTP is activated */
Amit Blay4aa292f2015-04-28 21:55:59 +03002391 mdtp_ext_partition_verification_t ext_partition;
Amit Blay8a510302015-08-17 09:20:01 +03002392
2393 if (!is_mdtp_activated) {
2394 ext_partition.partition = MDTP_PARTITION_NONE;
2395 mdtp_fwlock_verify_lock(&ext_partition);
2396 }
Amit Blay4aa292f2015-04-28 21:55:59 +03002397 }
Shay Nachmani062aa3f2015-05-17 17:28:44 +03002398
Amir Kotzer7c768c02016-04-13 09:08:05 +03002399 /* If mdtp state cannot be validate, block fastboot boot*/
2400 if(mdtp_activated(&is_mdtp_activated)){
2401 dprintf(CRITICAL, "mdtp_activated cannot validate state.\n");
2402 dprintf(CRITICAL, "Can not proceed with fastboot boot command.\n");
2403 goto boot_failed;
2404 }
Shay Nachmani062aa3f2015-05-17 17:28:44 +03002405 if(is_mdtp_activated){
2406 dprintf(CRITICAL, "fastboot boot command is not available.\n");
lijuang2008ff22016-03-07 17:56:27 +08002407 goto boot_failed;
Shay Nachmani062aa3f2015-05-17 17:28:44 +03002408 }
Amit Blay4aa292f2015-04-28 21:55:59 +03002409#endif /* MDTP_SUPPORT */
2410
Sridhar Parasuram361e4cd2015-09-24 11:19:19 -07002411#if VERIFIED_BOOT
Parth Dixitbc9b6492015-10-18 00:41:38 +05302412#if !VBOOT_MOTA
Sridhar Parasuram361e4cd2015-09-24 11:19:19 -07002413 // send root of trust
2414 if(!send_rot_command((uint32_t)device.is_unlocked))
2415 ASSERT(0);
2416#endif
Parth Dixitbc9b6492015-10-18 00:41:38 +05302417#endif
Channagoud Kadabia22144f2013-03-20 11:49:01 -07002418 /*
Matthew Qinbb7923d2015-02-09 10:56:09 +08002419 * Check if the kernel image is a gzip package. If yes, need to decompress it.
2420 * If not, continue booting.
2421 */
2422 if (is_gzip_package((unsigned char *)(data + page_size), hdr->kernel_size))
2423 {
2424 out_addr = (unsigned char *)target_get_scratch_address();
2425 out_addr = (unsigned char *)(out_addr + image_actual + page_size);
2426 out_avai_len = target_get_max_flash_size() - image_actual - page_size;
Matthew Qin0b15b322015-05-19 05:20:54 -04002427 dprintf(INFO, "decompressing kernel image: start\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08002428 ret = decompress((unsigned char *)(ptr + page_size),
2429 hdr->kernel_size, out_addr, out_avai_len,
2430 &dtb_offset, &out_len);
2431 if (ret)
2432 {
Matthew Qin0b15b322015-05-19 05:20:54 -04002433 dprintf(CRITICAL, "decompressing image failed!!!\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08002434 ASSERT(0);
2435 }
2436
Matthew Qin0b15b322015-05-19 05:20:54 -04002437 dprintf(INFO, "decompressing kernel image: done\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08002438 kptr = (struct kernel64_hdr *)out_addr;
2439 kernel_start_addr = out_addr;
2440 kernel_size = out_len;
2441 } else {
2442 kptr = (struct kernel64_hdr*)((char *)data + page_size);
2443 kernel_start_addr = (unsigned char *)((char *)data + page_size);
2444 kernel_size = hdr->kernel_size;
2445 }
2446
2447 /*
Channagoud Kadabia22144f2013-03-20 11:49:01 -07002448 * Update the kernel/ramdisk/tags address if the boot image header
2449 * has default values, these default values come from mkbootimg when
2450 * the boot image is flashed using fastboot flash:raw
2451 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08002452 update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr));
Dima Zavin3cadfff2013-03-21 14:30:48 -07002453
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002454 /* Get virtual addresses since the hdr saves physical addresses. */
2455 hdr->kernel_addr = VA(hdr->kernel_addr);
2456 hdr->ramdisk_addr = VA(hdr->ramdisk_addr);
2457 hdr->tags_addr = VA(hdr->tags_addr);
Brian Swetland9c4c0752009-01-25 16:23:50 -08002458
Matthew Qinbb7923d2015-02-09 10:56:09 +08002459 kernel_size = ROUND_TO_PAGE(kernel_size, page_mask);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002460 /* Check if the addresses in the header are valid. */
Matthew Qinbb7923d2015-02-09 10:56:09 +08002461 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_size) ||
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002462 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
2463 {
2464 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
lijuang2008ff22016-03-07 17:56:27 +08002465 goto boot_failed;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002466 }
2467
Amol Jadicb524072012-08-09 16:40:18 -07002468#if DEVICE_TREE
Matthew Qin271927e2015-03-31 22:07:07 -04002469 scratch_offset = image_actual + page_size + out_len;
Amol Jadicb524072012-08-09 16:40:18 -07002470 /* find correct dtb and copy it to right location */
Matthew Qin271927e2015-03-31 22:07:07 -04002471 ret = copy_dtb(data, scratch_offset);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002472
2473 dtb_copied = !ret ? 1 : 0;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002474#else
2475 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
2476 {
2477 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
lijuang2008ff22016-03-07 17:56:27 +08002478 goto boot_failed;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002479 }
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002480#endif
2481
2482 /* Load ramdisk & kernel */
2483 memmove((void*) hdr->ramdisk_addr, ptr + page_size + kernel_actual, hdr->ramdisk_size);
Matthew Qinbb7923d2015-02-09 10:56:09 +08002484 memmove((void*) hdr->kernel_addr, (char*) (kernel_start_addr), kernel_size);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002485
2486#if DEVICE_TREE
Matthew Qinbb7923d2015-02-09 10:56:09 +08002487 if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual))
2488 {
2489 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
lijuang2008ff22016-03-07 17:56:27 +08002490 goto boot_failed;
Matthew Qinbb7923d2015-02-09 10:56:09 +08002491 }
2492
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002493 /*
2494 * If dtb is not found look for appended DTB in the kernel.
2495 * If appended dev tree is found, update the atags with
2496 * memory address to the DTB appended location on RAM.
2497 * Else update with the atags address in the kernel header
2498 */
2499 if (!dtb_copied) {
2500 void *dtb;
Matthew Qinbb7923d2015-02-09 10:56:09 +08002501 dtb = dev_tree_appended((void*)(ptr + page_size),
2502 hdr->kernel_size, dtb_offset,
Dima Zavine63e5572013-05-03 12:23:06 -07002503 (void *)hdr->tags_addr);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002504 if (!dtb) {
2505 fastboot_fail("dtb not found");
lijuang2008ff22016-03-07 17:56:27 +08002506 goto boot_failed;
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002507 }
Amol Jadicb524072012-08-09 16:40:18 -07002508 }
2509#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08002510
2511 fastboot_okay("");
Amol Jadi7c4316c2013-10-07 14:19:26 -07002512 fastboot_stop();
Brian Swetland9c4c0752009-01-25 16:23:50 -08002513
Dima Zavin77e41f32013-03-06 16:10:43 -08002514 boot_linux((void*) hdr->kernel_addr, (void*) hdr->tags_addr,
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002515 (const char*) hdr->cmdline, board_machtype(),
2516 (void*) hdr->ramdisk_addr, hdr->ramdisk_size);
lijuang2008ff22016-03-07 17:56:27 +08002517
2518 /* fastboot already stop, it's no need to show fastboot menu */
2519 return;
2520boot_failed:
2521#if FBCON_DISPLAY_MSG
2522 /* revert to fastboot menu if boot failed */
2523 display_fastboot_menu();
2524#endif
2525 return;
Brian Swetland9c4c0752009-01-25 16:23:50 -08002526}
2527
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002528void cmd_erase_nand(const char *arg, void *data, unsigned sz)
Dima Zavin214cc642009-01-26 11:16:21 -08002529{
2530 struct ptentry *ptn;
2531 struct ptable *ptable;
2532
2533 ptable = flash_get_ptable();
2534 if (ptable == NULL) {
2535 fastboot_fail("partition table doesn't exist");
2536 return;
2537 }
2538
2539 ptn = ptable_find(ptable, arg);
2540 if (ptn == NULL) {
2541 fastboot_fail("unknown partition name");
2542 return;
2543 }
2544
2545 if (flash_erase(ptn)) {
2546 fastboot_fail("failed to erase partition");
2547 return;
2548 }
2549 fastboot_okay("");
2550}
2551
Bikas Gurungd48bd242010-09-04 19:54:32 -07002552
2553void cmd_erase_mmc(const char *arg, void *data, unsigned sz)
2554{
2555 unsigned long long ptn = 0;
Oliver Wangcee448d2013-10-22 18:40:13 +08002556 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07002557 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002558 uint8_t lun = 0;
Vijay Kumar Pendoti0b21f462016-05-02 17:09:18 +05302559 char *footer = NULL;
Bikas Gurungd48bd242010-09-04 19:54:32 -07002560
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002561#if VERIFIED_BOOT
2562 if(!strcmp(arg, KEYSTORE_PTN_NAME))
2563 {
2564 if(!device.is_unlocked)
2565 {
2566 fastboot_fail("unlock device to erase keystore");
2567 return;
2568 }
2569 }
2570#endif
2571
Kinson Chikf1a43512011-07-14 11:28:39 -07002572 index = partition_get_index(arg);
2573 ptn = partition_get_offset(index);
Oliver Wangcee448d2013-10-22 18:40:13 +08002574 size = partition_get_size(index);
Neeti Desaica8c9602011-10-06 11:40:00 -07002575
Kinson Chikf1a43512011-07-14 11:28:39 -07002576 if(ptn == 0) {
Neeti Desaica8c9602011-10-06 11:40:00 -07002577 fastboot_fail("Partition table doesn't exist\n");
Bikas Gurungd48bd242010-09-04 19:54:32 -07002578 return;
2579 }
Kun Liang2f1601a2013-08-12 16:29:54 +08002580
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002581 lun = partition_get_lun(index);
2582 mmc_set_lun(lun);
2583
Sridhar Parasuramd7957122015-02-27 11:33:40 -08002584 if (platform_boot_dev_isemmc())
2585 {
2586 if (mmc_erase_card(ptn, size)) {
2587 fastboot_fail("failed to erase partition\n");
2588 return;
2589 }
2590 } else {
2591 BUF_DMA_ALIGN(out, DEFAULT_ERASE_SIZE);
2592 size = partition_get_size(index);
2593 if (size > DEFAULT_ERASE_SIZE)
2594 size = DEFAULT_ERASE_SIZE;
Kun Liang2f1601a2013-08-12 16:29:54 +08002595
Sridhar Parasuramd7957122015-02-27 11:33:40 -08002596 /* Simple inefficient version of erase. Just writing
2597 0 in first several blocks */
2598 if (mmc_write(ptn , size, (unsigned int *)out)) {
2599 fastboot_fail("failed to erase partition");
2600 return;
2601 }
Vijay Kumar Pendoti0b21f462016-05-02 17:09:18 +05302602 /*Erase FDE metadata at the userdata footer*/
2603 if(!(strncmp(arg, "userdata", 8)))
2604 {
2605 footer = memalign(CACHE_LINE, FOOTER_SIZE);
2606 memset((void *)footer, 0, FOOTER_SIZE);
2607
2608 size = partition_get_size(index);
2609
2610 if (mmc_write((ptn + size) - FOOTER_SIZE , FOOTER_SIZE, (unsigned int *)footer)) {
2611 fastboot_fail("failed to erase userdata footer");
2612 free(footer);
2613 return;
2614 }
2615 free(footer);
2616 }
Bikas Gurungd48bd242010-09-04 19:54:32 -07002617 }
Sridhar Parasuram32b30662015-07-10 13:33:22 -07002618#if VERIFIED_BOOT
Parth Dixitbc9b6492015-10-18 00:41:38 +05302619#if !VBOOT_MOTA
Sridhar Parasuram32b30662015-07-10 13:33:22 -07002620 if(!(strncmp(arg, "userdata", 8)))
2621 if(send_delete_keys_to_tz())
2622 ASSERT(0);
2623#endif
Parth Dixitbc9b6492015-10-18 00:41:38 +05302624#endif
Bikas Gurungd48bd242010-09-04 19:54:32 -07002625 fastboot_okay("");
2626}
2627
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002628void cmd_erase(const char *arg, void *data, unsigned sz)
2629{
Channagoud Kadabice3cf422015-04-17 16:02:30 -07002630#if VERIFIED_BOOT
Channagoud Kadabi35297672015-06-13 11:09:49 -07002631 if (target_build_variant_user())
Channagoud Kadabice3cf422015-04-17 16:02:30 -07002632 {
Sridhar Parasuramc32d07d2015-07-12 10:57:48 -07002633 if(!device.is_unlocked)
Channagoud Kadabice3cf422015-04-17 16:02:30 -07002634 {
Channagoud Kadabi35297672015-06-13 11:09:49 -07002635 fastboot_fail("device is locked. Cannot erase");
Channagoud Kadabice3cf422015-04-17 16:02:30 -07002636 return;
2637 }
2638 }
2639#endif
2640
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002641 if(target_is_emmc_boot())
2642 cmd_erase_mmc(arg, data, sz);
2643 else
2644 cmd_erase_nand(arg, data, sz);
2645}
Bikas Gurungd48bd242010-09-04 19:54:32 -07002646
Channagoud Kadabiad259832015-05-29 11:14:17 -07002647static uint32_t aboot_get_secret_key()
2648{
2649 /* 0 is invalid secret key, update this implementation to return
2650 * device specific unique secret key
2651 */
2652 return 0;
2653}
2654
Ajay Dudani5c761132011-04-07 20:19:04 -07002655void cmd_flash_mmc_img(const char *arg, void *data, unsigned sz)
Shashank Mittal23b8f422010-04-16 19:27:21 -07002656{
2657 unsigned long long ptn = 0;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07002658 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07002659 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002660 char *token = NULL;
2661 char *pname = NULL;
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07002662 char *sp;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002663 uint8_t lun = 0;
2664 bool lun_set = false;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07002665
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07002666 token = strtok_r((char *)arg, ":", &sp);
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002667 pname = token;
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07002668 token = strtok_r(NULL, ":", &sp);
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002669 if(token)
2670 {
2671 lun = atoi(token);
2672 mmc_set_lun(lun);
2673 lun_set = true;
2674 }
2675
Mao Jinlong226f33a2014-07-04 17:24:10 +08002676 if (pname)
Greg Grisco6e754772011-06-23 12:19:39 -07002677 {
Channagoud Kadabiad259832015-05-29 11:14:17 -07002678 if (!strncmp(pname, "frp-unlock", strlen("frp-unlock")))
2679 {
2680 if (!aboot_frp_unlock(pname, data, sz))
2681 {
2682 fastboot_info("FRP unlock successful");
2683 fastboot_okay("");
2684 }
2685 else
2686 fastboot_fail("Secret key is invalid, please update the bootloader with secret key");
2687
2688 return;
2689 }
2690
Mao Jinlong226f33a2014-07-04 17:24:10 +08002691 if (!strcmp(pname, "partition"))
2692 {
2693 dprintf(INFO, "Attempt to write partition image.\n");
2694 if (write_partition(sz, (unsigned char *) data)) {
2695 fastboot_fail("failed to write partition");
Greg Grisco6e754772011-06-23 12:19:39 -07002696 return;
2697 }
2698 }
Mao Jinlong226f33a2014-07-04 17:24:10 +08002699 else
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002700 {
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002701#if VERIFIED_BOOT
2702 if(!strcmp(pname, KEYSTORE_PTN_NAME))
2703 {
2704 if(!device.is_unlocked)
2705 {
2706 fastboot_fail("unlock device to flash keystore");
2707 return;
2708 }
Gaurav Nebhwanic7313cc2015-12-15 22:25:04 +05302709 if(!boot_verify_validate_keystore((unsigned char *)data,sz))
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002710 {
2711 fastboot_fail("image is not a keystore file");
2712 return;
2713 }
2714 }
2715#endif
Mao Jinlong226f33a2014-07-04 17:24:10 +08002716 index = partition_get_index(pname);
2717 ptn = partition_get_offset(index);
2718 if(ptn == 0) {
2719 fastboot_fail("partition table doesn't exist");
2720 return;
2721 }
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002722
Mao Jinlong226f33a2014-07-04 17:24:10 +08002723 if (!strcmp(pname, "boot") || !strcmp(pname, "recovery")) {
2724 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
2725 fastboot_fail("image is not a boot image");
2726 return;
2727 }
2728 }
2729
2730 if(!lun_set)
2731 {
2732 lun = partition_get_lun(index);
2733 mmc_set_lun(lun);
2734 }
2735
2736 size = partition_get_size(index);
Gaurav Nebhwani4d2389c2016-03-17 21:10:05 +05302737 if (ROUND_TO_PAGE(sz, mmc_blocksize_mask) > size) {
Mao Jinlong226f33a2014-07-04 17:24:10 +08002738 fastboot_fail("size too large");
2739 return;
2740 }
2741 else if (mmc_write(ptn , sz, (unsigned int *)data)) {
2742 fastboot_fail("flash write failure");
2743 return;
2744 }
Greg Grisco6e754772011-06-23 12:19:39 -07002745 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07002746 }
2747 fastboot_okay("");
2748 return;
2749}
2750
Ajay Dudanide984792015-03-02 09:57:41 -08002751void cmd_flash_meta_img(const char *arg, void *data, unsigned sz)
2752{
2753 int i, images;
2754 meta_header_t *meta_header;
2755 img_header_entry_t *img_header_entry;
Parth Dixit3e2d3032016-03-04 17:11:52 +05302756 /*End of the image address*/
2757 uintptr_t data_end;
2758
2759 if( (UINT_MAX - sz) > (uintptr_t)data )
2760 data_end = (uintptr_t)data + sz;
2761 else
2762 {
2763 fastboot_fail("Cannot flash: image header corrupt");
2764 return;
2765 }
2766
2767 if( data_end < ((uintptr_t)data + sizeof(meta_header_t)))
2768 {
2769 fastboot_fail("Cannot flash: image header corrupt");
2770 return;
2771 }
Ajay Dudanide984792015-03-02 09:57:41 -08002772
lijuang8ee21882016-04-19 16:57:11 +08002773 /* If device is locked:
2774 * Forbid to flash image to avoid the device to bypass the image
2775 * which with "any" name other than bootloader. Because it maybe
2776 * a meta package of all partitions.
2777 */
2778#if VERIFIED_BOOT
2779 if (target_build_variant_user()) {
2780 if (!device.is_unlocked) {
2781 fastboot_fail("Device is locked, meta image flashing is not allowed");
2782 return;
2783 }
2784#if !VBOOT_MOTA
2785 if(!device.is_unlock_critical) {
2786 fastboot_fail("Device is critical locked, Meta image flashing is not allowed");
2787 return;
2788 }
2789#endif
2790 }
2791#endif
2792
Ajay Dudanide984792015-03-02 09:57:41 -08002793 meta_header = (meta_header_t*) data;
Parth Dixit3e2d3032016-03-04 17:11:52 +05302794 if( data_end < ((uintptr_t)data + meta_header->img_hdr_sz))
2795 {
2796 fastboot_fail("Cannot flash: image header corrupt");
2797 return;
2798 }
Ajay Dudanide984792015-03-02 09:57:41 -08002799 img_header_entry = (img_header_entry_t*) (data+sizeof(meta_header_t));
2800
2801 images = meta_header->img_hdr_sz / sizeof(img_header_entry_t);
2802
2803 for (i=0; i<images; i++) {
2804
2805 if((img_header_entry[i].ptn_name == NULL) ||
2806 (img_header_entry[i].start_offset == 0) ||
2807 (img_header_entry[i].size == 0))
2808 break;
2809
Parth Dixit3e2d3032016-03-04 17:11:52 +05302810 if( data_end < ((uintptr_t)data + img_header_entry[i].start_offset
2811 + img_header_entry[i].size) )
2812 {
2813 fastboot_fail("Cannot flash: image size mismatch");
2814 break;
2815 }
2816
Ajay Dudanide984792015-03-02 09:57:41 -08002817 cmd_flash_mmc_img(img_header_entry[i].ptn_name,
2818 (void *) data + img_header_entry[i].start_offset,
2819 img_header_entry[i].size);
2820 }
2821
Channagoud Kadabiaafbbfc2015-03-04 17:52:56 -08002822 if (!strncmp(arg, "bootloader", strlen("bootloader")))
2823 {
2824 strlcpy(device.bootloader_version, TARGET(BOARD), MAX_VERSION_LEN);
2825 strlcat(device.bootloader_version, "-", MAX_VERSION_LEN);
2826 strlcat(device.bootloader_version, meta_header->img_version, MAX_VERSION_LEN);
2827 }
2828 else
2829 {
2830 strlcpy(device.radio_version, TARGET(BOARD), MAX_VERSION_LEN);
2831 strlcat(device.radio_version, "-", MAX_VERSION_LEN);
2832 strlcat(device.radio_version, meta_header->img_version, MAX_VERSION_LEN);
2833 }
2834
2835 write_device_info(&device);
Ajay Dudanide984792015-03-02 09:57:41 -08002836 fastboot_okay("");
2837 return;
2838}
2839
Ajay Dudani5c761132011-04-07 20:19:04 -07002840void cmd_flash_mmc_sparse_img(const char *arg, void *data, unsigned sz)
2841{
2842 unsigned int chunk;
wufeng.jiang813dc352015-06-02 23:02:46 -04002843 uint64_t chunk_data_sz;
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002844 uint32_t *fill_buf = NULL;
2845 uint32_t fill_val;
Ajay Dudani5c761132011-04-07 20:19:04 -07002846 sparse_header_t *sparse_header;
2847 chunk_header_t *chunk_header;
Ajay Dudaniab18f022011-05-12 14:39:22 -07002848 uint32_t total_blocks = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07002849 unsigned long long ptn = 0;
Channagoud Kadabi65b91002011-10-11 17:34:33 +05302850 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07002851 int index = INVALID_PTN;
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05302852 uint32_t i;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002853 uint8_t lun = 0;
vijay kumar800255e2015-04-24 20:26:19 +05302854 /*End of the sparse image address*/
Parth Dixit64b1a482016-03-07 16:31:26 +05302855 uintptr_t data_end = (uintptr_t)data + sz;
Ajay Dudani5c761132011-04-07 20:19:04 -07002856
Kinson Chikf1a43512011-07-14 11:28:39 -07002857 index = partition_get_index(arg);
2858 ptn = partition_get_offset(index);
2859 if(ptn == 0) {
Ajay Dudani5c761132011-04-07 20:19:04 -07002860 fastboot_fail("partition table doesn't exist");
2861 return;
2862 }
2863
Channagoud Kadabi65b91002011-10-11 17:34:33 +05302864 size = partition_get_size(index);
Channagoud Kadabi65b91002011-10-11 17:34:33 +05302865
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002866 lun = partition_get_lun(index);
2867 mmc_set_lun(lun);
2868
vijay kumar800255e2015-04-24 20:26:19 +05302869 if (sz < sizeof(sparse_header_t)) {
2870 fastboot_fail("size too low");
2871 return;
2872 }
2873
Ajay Dudani5c761132011-04-07 20:19:04 -07002874 /* Read and skip over sparse image header */
2875 sparse_header = (sparse_header_t *) data;
vijay kumar800255e2015-04-24 20:26:19 +05302876
vijay kumar1321f342015-03-27 12:13:42 +05302877 if (((uint64_t)sparse_header->total_blks * (uint64_t)sparse_header->blk_sz) > size) {
Ajay Dudani876b3282012-12-21 14:12:17 -08002878 fastboot_fail("size too large");
2879 return;
2880 }
2881
vijay kumarde4fcf62015-04-23 13:05:49 +05302882 data += sizeof(sparse_header_t);
vijay kumar800255e2015-04-24 20:26:19 +05302883
Parth Dixit64b1a482016-03-07 16:31:26 +05302884 if (data_end < (uintptr_t)data) {
vijay kumar800255e2015-04-24 20:26:19 +05302885 fastboot_fail("buffer overreads occured due to invalid sparse header");
2886 return;
2887 }
2888
vijay kumarde4fcf62015-04-23 13:05:49 +05302889 if(sparse_header->file_hdr_sz != sizeof(sparse_header_t))
Ajay Dudani5c761132011-04-07 20:19:04 -07002890 {
vijay kumarde4fcf62015-04-23 13:05:49 +05302891 fastboot_fail("sparse header size mismatch");
2892 return;
Ajay Dudani5c761132011-04-07 20:19:04 -07002893 }
2894
Ajay Dudanib06c05f2011-05-12 14:46:10 -07002895 dprintf (SPEW, "=== Sparse Image Header ===\n");
2896 dprintf (SPEW, "magic: 0x%x\n", sparse_header->magic);
2897 dprintf (SPEW, "major_version: 0x%x\n", sparse_header->major_version);
2898 dprintf (SPEW, "minor_version: 0x%x\n", sparse_header->minor_version);
2899 dprintf (SPEW, "file_hdr_sz: %d\n", sparse_header->file_hdr_sz);
2900 dprintf (SPEW, "chunk_hdr_sz: %d\n", sparse_header->chunk_hdr_sz);
2901 dprintf (SPEW, "blk_sz: %d\n", sparse_header->blk_sz);
2902 dprintf (SPEW, "total_blks: %d\n", sparse_header->total_blks);
2903 dprintf (SPEW, "total_chunks: %d\n", sparse_header->total_chunks);
Ajay Dudani5c761132011-04-07 20:19:04 -07002904
2905 /* Start processing chunks */
2906 for (chunk=0; chunk<sparse_header->total_chunks; chunk++)
2907 {
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302908 /* Make sure the total image size does not exceed the partition size */
2909 if(((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz) >= size) {
2910 fastboot_fail("size too large");
2911 return;
2912 }
Ajay Dudani5c761132011-04-07 20:19:04 -07002913 /* Read and skip over chunk header */
2914 chunk_header = (chunk_header_t *) data;
2915 data += sizeof(chunk_header_t);
2916
Parth Dixit64b1a482016-03-07 16:31:26 +05302917 if (data_end < (uintptr_t)data) {
vijay kumar800255e2015-04-24 20:26:19 +05302918 fastboot_fail("buffer overreads occured due to invalid sparse header");
2919 return;
2920 }
2921
Ajay Dudani5c761132011-04-07 20:19:04 -07002922 dprintf (SPEW, "=== Chunk Header ===\n");
2923 dprintf (SPEW, "chunk_type: 0x%x\n", chunk_header->chunk_type);
2924 dprintf (SPEW, "chunk_data_sz: 0x%x\n", chunk_header->chunk_sz);
2925 dprintf (SPEW, "total_size: 0x%x\n", chunk_header->total_sz);
2926
vijay kumar800255e2015-04-24 20:26:19 +05302927 if(sparse_header->chunk_hdr_sz != sizeof(chunk_header_t))
Ajay Dudani5c761132011-04-07 20:19:04 -07002928 {
vijay kumar800255e2015-04-24 20:26:19 +05302929 fastboot_fail("chunk header size mismatch");
2930 return;
Ajay Dudani5c761132011-04-07 20:19:04 -07002931 }
2932
wufeng.jiang813dc352015-06-02 23:02:46 -04002933 if (!sparse_header->blk_sz ){
2934 fastboot_fail("Invalid block size\n");
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302935 return;
2936 }
2937
wufeng.jiang813dc352015-06-02 23:02:46 -04002938 chunk_data_sz = (uint64_t)sparse_header->blk_sz * chunk_header->chunk_sz;
2939
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302940 /* Make sure that the chunk size calculated from sparse image does not
2941 * exceed partition size
2942 */
2943 if ((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz + chunk_data_sz > size)
2944 {
2945 fastboot_fail("Chunk data size exceeds partition size");
2946 return;
2947 }
2948
Ajay Dudani5c761132011-04-07 20:19:04 -07002949 switch (chunk_header->chunk_type)
2950 {
2951 case CHUNK_TYPE_RAW:
wufeng.jiang813dc352015-06-02 23:02:46 -04002952 if((uint64_t)chunk_header->total_sz != ((uint64_t)sparse_header->chunk_hdr_sz +
Ajay Dudani5c761132011-04-07 20:19:04 -07002953 chunk_data_sz))
2954 {
2955 fastboot_fail("Bogus chunk size for chunk type Raw");
2956 return;
2957 }
2958
Parth Dixit64b1a482016-03-07 16:31:26 +05302959 if (data_end < (uintptr_t)data + chunk_data_sz) {
vijay kumar800255e2015-04-24 20:26:19 +05302960 fastboot_fail("buffer overreads occured due to invalid sparse header");
2961 return;
2962 }
2963
wufeng.jiang813dc352015-06-02 23:02:46 -04002964 /* chunk_header->total_sz is uint32,So chunk_data_sz is now less than 2^32
2965 otherwise it will return in the line above
2966 */
Ajay Dudaniab18f022011-05-12 14:39:22 -07002967 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
wufeng.jiang813dc352015-06-02 23:02:46 -04002968 (uint32_t)chunk_data_sz,
Ajay Dudaniab18f022011-05-12 14:39:22 -07002969 (unsigned int*)data))
Ajay Dudani5c761132011-04-07 20:19:04 -07002970 {
2971 fastboot_fail("flash write failure");
2972 return;
2973 }
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302974 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
2975 fastboot_fail("Bogus size for RAW chunk type");
2976 return;
2977 }
Ajay Dudani5c761132011-04-07 20:19:04 -07002978 total_blocks += chunk_header->chunk_sz;
wufeng.jiang813dc352015-06-02 23:02:46 -04002979 data += (uint32_t)chunk_data_sz;
Ajay Dudani5c761132011-04-07 20:19:04 -07002980 break;
2981
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002982 case CHUNK_TYPE_FILL:
2983 if(chunk_header->total_sz != (sparse_header->chunk_hdr_sz +
2984 sizeof(uint32_t)))
2985 {
2986 fastboot_fail("Bogus chunk size for chunk type FILL");
2987 return;
2988 }
2989
2990 fill_buf = (uint32_t *)memalign(CACHE_LINE, ROUNDUP(sparse_header->blk_sz, CACHE_LINE));
2991 if (!fill_buf)
2992 {
2993 fastboot_fail("Malloc failed for: CHUNK_TYPE_FILL");
2994 return;
2995 }
2996
Parth Dixit64b1a482016-03-07 16:31:26 +05302997 if (data_end < (uintptr_t)data + sizeof(uint32_t)) {
vijay kumar800255e2015-04-24 20:26:19 +05302998 fastboot_fail("buffer overreads occured due to invalid sparse header");
Gaurav Nebhwani163cbf82016-03-17 21:21:27 +05302999 free(fill_buf);
vijay kumar800255e2015-04-24 20:26:19 +05303000 return;
3001 }
Channagoud Kadabif627cf82013-09-09 14:08:20 -07003002 fill_val = *(uint32_t *)data;
3003 data = (char *) data + sizeof(uint32_t);
Channagoud Kadabif627cf82013-09-09 14:08:20 -07003004
3005 for (i = 0; i < (sparse_header->blk_sz / sizeof(fill_val)); i++)
3006 {
3007 fill_buf[i] = fill_val;
3008 }
3009
Gaurav Nebhwanie94cbe12016-03-17 21:16:34 +05303010 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz))
3011 {
3012 fastboot_fail("bogus size for chunk FILL type");
3013 free(fill_buf);
3014 return;
3015 }
3016
wufeng.jiang813dc352015-06-02 23:02:46 -04003017 for (i = 0; i < chunk_header->chunk_sz; i++)
Channagoud Kadabif627cf82013-09-09 14:08:20 -07003018 {
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05303019 /* Make sure that the data written to partition does not exceed partition size */
3020 if ((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz + sparse_header->blk_sz > size)
3021 {
3022 fastboot_fail("Chunk data size for fill type exceeds partition size");
Gaurav Nebhwani163cbf82016-03-17 21:21:27 +05303023 free(fill_buf);
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05303024 return;
3025 }
3026
Channagoud Kadabif627cf82013-09-09 14:08:20 -07003027 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
3028 sparse_header->blk_sz,
3029 fill_buf))
3030 {
3031 fastboot_fail("flash write failure");
3032 free(fill_buf);
3033 return;
3034 }
3035
3036 total_blocks++;
3037 }
3038
3039 free(fill_buf);
3040 break;
3041
Ajay Dudani5c761132011-04-07 20:19:04 -07003042 case CHUNK_TYPE_DONT_CARE:
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05303043 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
3044 fastboot_fail("bogus size for chunk DONT CARE type");
3045 return;
3046 }
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07003047 total_blocks += chunk_header->chunk_sz;
3048 break;
3049
Ajay Dudani5c761132011-04-07 20:19:04 -07003050 case CHUNK_TYPE_CRC:
3051 if(chunk_header->total_sz != sparse_header->chunk_hdr_sz)
3052 {
wufeng.jiang813dc352015-06-02 23:02:46 -04003053 fastboot_fail("Bogus chunk size for chunk type CRC");
Ajay Dudani5c761132011-04-07 20:19:04 -07003054 return;
3055 }
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05303056 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
3057 fastboot_fail("bogus size for chunk CRC type");
3058 return;
3059 }
Ajay Dudani5c761132011-04-07 20:19:04 -07003060 total_blocks += chunk_header->chunk_sz;
Parth Dixit64b1a482016-03-07 16:31:26 +05303061 if ((uintptr_t)data > UINT_MAX - chunk_data_sz) {
vijay kumar800255e2015-04-24 20:26:19 +05303062 fastboot_fail("integer overflow occured");
3063 return;
3064 }
wufeng.jiang813dc352015-06-02 23:02:46 -04003065 data += (uint32_t)chunk_data_sz;
Parth Dixit64b1a482016-03-07 16:31:26 +05303066 if (data_end < (uintptr_t)data) {
vijay kumar800255e2015-04-24 20:26:19 +05303067 fastboot_fail("buffer overreads occured due to invalid sparse header");
3068 return;
3069 }
Ajay Dudani5c761132011-04-07 20:19:04 -07003070 break;
3071
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07003072 default:
Channagoud Kadabif627cf82013-09-09 14:08:20 -07003073 dprintf(CRITICAL, "Unkown chunk type: %x\n",chunk_header->chunk_type);
Ajay Dudani5c761132011-04-07 20:19:04 -07003074 fastboot_fail("Unknown chunk type");
3075 return;
3076 }
3077 }
3078
Ajay Dudani0c6927b2011-05-18 11:12:16 -07003079 dprintf(INFO, "Wrote %d blocks, expected to write %d blocks\n",
3080 total_blocks, sparse_header->total_blks);
3081
3082 if(total_blocks != sparse_header->total_blks)
3083 {
3084 fastboot_fail("sparse image write failure");
3085 }
Ajay Dudani5c761132011-04-07 20:19:04 -07003086
3087 fastboot_okay("");
3088 return;
3089}
3090
3091void cmd_flash_mmc(const char *arg, void *data, unsigned sz)
3092{
3093 sparse_header_t *sparse_header;
Ajay Dudanide984792015-03-02 09:57:41 -08003094 meta_header_t *meta_header;
Ajay Dudani5c761132011-04-07 20:19:04 -07003095
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07003096#ifdef SSD_ENABLE
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -08003097 /* 8 Byte Magic + 2048 Byte xml + Encrypted Data */
3098 unsigned int *magic_number = (unsigned int *) data;
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08003099 int ret=0;
3100 uint32 major_version=0;
3101 uint32 minor_version=0;
3102
3103 ret = scm_svc_version(&major_version,&minor_version);
3104 if(!ret)
3105 {
3106 if(major_version >= 2)
3107 {
Amir Samuelovbb65ce02013-05-05 12:20:18 +03003108 if( !strcmp(arg, "ssd") || !strcmp(arg, "tqs") )
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08003109 {
3110 ret = encrypt_scm((uint32 **) &data, &sz);
3111 if (ret != 0) {
3112 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
3113 return;
3114 }
3115
Amir Samuelovbb65ce02013-05-05 12:20:18 +03003116 /* Protect only for SSD */
3117 if (!strcmp(arg, "ssd")) {
3118 ret = scm_protect_keystore((uint32 *) data, sz);
3119 if (ret != 0) {
3120 dprintf(CRITICAL, "ERROR: scm_protect_keystore Failed\n");
3121 return;
3122 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08003123 }
3124 }
3125 else
3126 {
3127 ret = decrypt_scm_v2((uint32 **) &data, &sz);
3128 if(ret != 0)
3129 {
3130 dprintf(CRITICAL,"ERROR: Decryption Failure\n");
3131 return;
3132 }
3133 }
3134 }
3135 else
3136 {
3137 if (magic_number[0] == DECRYPT_MAGIC_0 &&
3138 magic_number[1] == DECRYPT_MAGIC_1)
3139 {
3140 ret = decrypt_scm((uint32 **) &data, &sz);
3141 if (ret != 0) {
3142 dprintf(CRITICAL, "ERROR: Invalid secure image\n");
3143 return;
3144 }
3145 }
3146 else if (magic_number[0] == ENCRYPT_MAGIC_0 &&
3147 magic_number[1] == ENCRYPT_MAGIC_1)
3148 {
3149 ret = encrypt_scm((uint32 **) &data, &sz);
3150 if (ret != 0) {
3151 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
3152 return;
3153 }
3154 }
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07003155 }
3156 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08003157 else
Neeti Desai127b9e02012-03-20 16:11:23 -07003158 {
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08003159 dprintf(CRITICAL,"INVALID SVC Version\n");
3160 return;
Neeti Desai127b9e02012-03-20 16:11:23 -07003161 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08003162#endif /* SSD_ENABLE */
Neeti Desai127b9e02012-03-20 16:11:23 -07003163
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003164#if VERIFIED_BOOT
Channagoud Kadabi35297672015-06-13 11:09:49 -07003165 if (target_build_variant_user())
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003166 {
lijuang511a2b52015-08-14 20:50:51 +08003167 /* if device is locked:
3168 * common partition will not allow to be flashed
3169 * critical partition will allow to flash image.
3170 */
3171 if(!device.is_unlocked && !critical_flash_allowed(arg)) {
3172 fastboot_fail("Partition flashing is not allowed");
3173 return;
3174 }
Parth Dixitddbc7352015-10-18 03:13:31 +05303175#if !VBOOT_MOTA
lijuang511a2b52015-08-14 20:50:51 +08003176 /* if device critical is locked:
3177 * common partition will allow to be flashed
3178 * critical partition will not allow to flash image.
3179 */
3180 if(!device.is_unlock_critical && critical_flash_allowed(arg)) {
3181 fastboot_fail("Critical partition flashing is not allowed");
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003182 return;
3183 }
Parth Dixitddbc7352015-10-18 03:13:31 +05303184#endif
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003185 }
3186#endif
3187
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07003188 sparse_header = (sparse_header_t *) data;
Ajay Dudanide984792015-03-02 09:57:41 -08003189 meta_header = (meta_header_t *) data;
3190 if (sparse_header->magic == SPARSE_HEADER_MAGIC)
Ajay Dudani5c761132011-04-07 20:19:04 -07003191 cmd_flash_mmc_sparse_img(arg, data, sz);
Ajay Dudanide984792015-03-02 09:57:41 -08003192 else if (meta_header->magic == META_HEADER_MAGIC)
3193 cmd_flash_meta_img(arg, data, sz);
3194 else
3195 cmd_flash_mmc_img(arg, data, sz);
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07003196
3197#if VERIFIED_BOOT
Parth Dixitddbc7352015-10-18 03:13:31 +05303198#if !VBOOT_MOTA
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07003199 if((!strncmp(arg, "system", 6)) && !device.verity_mode)
3200 {
3201 // reset dm_verity mode to enforcing
3202 device.verity_mode = 1;
3203 write_device_info(&device);
3204 }
3205#endif
Parth Dixitddbc7352015-10-18 03:13:31 +05303206#endif
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07003207
Ajay Dudani5c761132011-04-07 20:19:04 -07003208 return;
3209}
3210
Tanya Brokhman84eaaf62015-02-24 21:57:06 +02003211void cmd_updatevol(const char *vol_name, void *data, unsigned sz)
3212{
3213 struct ptentry *sys_ptn;
3214 struct ptable *ptable;
3215
3216 ptable = flash_get_ptable();
3217 if (ptable == NULL) {
3218 fastboot_fail("partition table doesn't exist");
3219 return;
3220 }
3221
3222 sys_ptn = ptable_find(ptable, "system");
3223 if (sys_ptn == NULL) {
3224 fastboot_fail("system partition not found");
3225 return;
3226 }
3227
3228 sz = ROUND_TO_PAGE(sz, page_mask);
3229 if (update_ubi_vol(sys_ptn, vol_name, data, sz))
3230 fastboot_fail("update_ubi_vol failed");
3231 else
3232 fastboot_okay("");
3233}
3234
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003235void cmd_flash_nand(const char *arg, void *data, unsigned sz)
Dima Zavin214cc642009-01-26 11:16:21 -08003236{
3237 struct ptentry *ptn;
3238 struct ptable *ptable;
3239 unsigned extra = 0;
Gaurav Nebhwanid9730712016-05-06 14:28:29 +05303240 uint64_t partition_size = 0;
Dima Zavin214cc642009-01-26 11:16:21 -08003241
3242 ptable = flash_get_ptable();
3243 if (ptable == NULL) {
3244 fastboot_fail("partition table doesn't exist");
3245 return;
3246 }
3247
3248 ptn = ptable_find(ptable, arg);
3249 if (ptn == NULL) {
Tanya Brokhman84eaaf62015-02-24 21:57:06 +02003250 dprintf(INFO, "unknown partition name (%s). Trying updatevol\n",
3251 arg);
3252 cmd_updatevol(arg, data, sz);
Dima Zavin214cc642009-01-26 11:16:21 -08003253 return;
3254 }
3255
3256 if (!strcmp(ptn->name, "boot") || !strcmp(ptn->name, "recovery")) {
3257 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
3258 fastboot_fail("image is not a boot image");
3259 return;
3260 }
3261 }
3262
Amol Jadi5c61a952012-05-04 17:05:35 -07003263 if (!strcmp(ptn->name, "system")
Deepa Dinamani13e32c42012-03-12 14:34:17 -07003264 || !strcmp(ptn->name, "userdata")
3265 || !strcmp(ptn->name, "persist")
Sundarajan Srinivasanb063a852013-11-19 14:02:27 -08003266 || !strcmp(ptn->name, "recoveryfs")
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -08003267 || !strcmp(ptn->name, "modem"))
Tanya Brokhman1c94f1a2015-02-15 09:05:03 +02003268 extra = 1;
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -08003269 else
Shashank Mittaldcc2e352009-11-19 19:11:16 -08003270 sz = ROUND_TO_PAGE(sz, page_mask);
Dima Zavin214cc642009-01-26 11:16:21 -08003271
Gaurav Nebhwanid9730712016-05-06 14:28:29 +05303272 partition_size = (uint64_t)ptn->length * (uint64_t)flash_num_pages_per_blk() * (uint64_t)flash_page_size();
3273 if (partition_size > UINT_MAX) {
3274 fastboot_fail("Invalid partition size");
3275 return;
3276 }
3277
3278 if (sz > partition_size) {
3279 fastboot_fail("Image size too large");
3280 return;
3281 }
3282
Dima Zavin214cc642009-01-26 11:16:21 -08003283 dprintf(INFO, "writing %d bytes to '%s'\n", sz, ptn->name);
Tanya Brokhman1c94f1a2015-02-15 09:05:03 +02003284 if (!memcmp((void *)data, UBI_MAGIC, UBI_MAGIC_SIZE)) {
3285 if (flash_ubi_img(ptn, data, sz)) {
3286 fastboot_fail("flash write failure");
3287 return;
3288 }
3289 } else {
3290 if (flash_write(ptn, extra, data, sz)) {
3291 fastboot_fail("flash write failure");
3292 return;
3293 }
Dima Zavin214cc642009-01-26 11:16:21 -08003294 }
3295 dprintf(INFO, "partition '%s' updated\n", ptn->name);
3296 fastboot_okay("");
3297}
3298
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003299void cmd_flash(const char *arg, void *data, unsigned sz)
3300{
3301 if(target_is_emmc_boot())
3302 cmd_flash_mmc(arg, data, sz);
3303 else
3304 cmd_flash_nand(arg, data, sz);
3305}
3306
Dima Zavin214cc642009-01-26 11:16:21 -08003307void cmd_continue(const char *arg, void *data, unsigned sz)
3308{
3309 fastboot_okay("");
Amol Jadi7c4316c2013-10-07 14:19:26 -07003310 fastboot_stop();
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003311
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003312 if (target_is_emmc_boot())
3313 {
lijuanga40d6302015-07-20 20:10:13 +08003314#if FBCON_DISPLAY_MSG
lijuangde34d502016-02-26 16:04:50 +08003315 /* Exit keys' detection thread firstly */
3316 exit_menu_keys_detection();
lijuanga40d6302015-07-20 20:10:13 +08003317#endif
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003318 boot_linux_from_mmc();
3319 }
3320 else
3321 {
3322 boot_linux_from_flash();
3323 }
Dima Zavin214cc642009-01-26 11:16:21 -08003324}
3325
Chandan Uddaraju94183c02010-01-15 15:13:59 -08003326void cmd_reboot(const char *arg, void *data, unsigned sz)
3327{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003328 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08003329 fastboot_okay("");
3330 reboot_device(0);
3331}
3332
3333void cmd_reboot_bootloader(const char *arg, void *data, unsigned sz)
3334{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003335 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08003336 fastboot_okay("");
3337 reboot_device(FASTBOOT_MODE);
3338}
3339
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003340void cmd_oem_enable_charger_screen(const char *arg, void *data, unsigned size)
3341{
3342 dprintf(INFO, "Enabling charger screen check\n");
3343 device.charger_screen_enabled = 1;
3344 write_device_info(&device);
3345 fastboot_okay("");
3346}
3347
3348void cmd_oem_disable_charger_screen(const char *arg, void *data, unsigned size)
3349{
3350 dprintf(INFO, "Disabling charger screen check\n");
3351 device.charger_screen_enabled = 0;
3352 write_device_info(&device);
3353 fastboot_okay("");
3354}
3355
lijuanga25d8bb2015-09-16 13:11:52 +08003356void cmd_oem_off_mode_charger(const char *arg, void *data, unsigned size)
3357{
3358 char *p = NULL;
3359 const char *delim = " \t\n\r";
Parth Dixit407f15b2016-01-07 14:47:37 +05303360 char *sp;
lijuanga25d8bb2015-09-16 13:11:52 +08003361
3362 if (arg) {
Parth Dixit407f15b2016-01-07 14:47:37 +05303363 p = strtok_r((char *)arg, delim, &sp);
lijuanga25d8bb2015-09-16 13:11:52 +08003364 if (p) {
3365 if (!strncmp(p, "0", 1)) {
3366 device.charger_screen_enabled = 0;
3367 } else if (!strncmp(p, "1", 1)) {
3368 device.charger_screen_enabled = 1;
3369 }
3370 }
3371 }
3372
3373 /* update charger_screen_enabled value for getvar
3374 * command
3375 */
3376 snprintf(charger_screen_enabled, MAX_RSP_SIZE, "%d",
3377 device.charger_screen_enabled);
3378
3379 write_device_info(&device);
3380 fastboot_okay("");
3381}
3382
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05303383void cmd_oem_select_display_panel(const char *arg, void *data, unsigned size)
3384{
3385 dprintf(INFO, "Selecting display panel %s\n", arg);
3386 if (arg)
3387 strlcpy(device.display_panel, arg,
3388 sizeof(device.display_panel));
3389 write_device_info(&device);
3390 fastboot_okay("");
3391}
3392
Shashank Mittal162244e2011-08-08 19:01:25 -07003393void cmd_oem_unlock(const char *arg, void *data, unsigned sz)
3394{
lijuang4ece1e72015-08-14 21:02:36 +08003395 set_device_unlock(UNLOCK, TRUE);
vijay kumarc65876c2015-04-24 13:29:16 +05303396}
3397
3398void cmd_oem_unlock_go(const char *arg, void *data, unsigned sz)
3399{
lijuang4ece1e72015-08-14 21:02:36 +08003400 if(!device.is_unlocked) {
vijay kumarc65876c2015-04-24 13:29:16 +05303401 if(!is_allow_unlock) {
3402 fastboot_fail("oem unlock is not allowed");
3403 return;
3404 }
3405
lijuang4ece1e72015-08-14 21:02:36 +08003406 set_device_unlock_value(UNLOCK, TRUE);
vijay kumarc65876c2015-04-24 13:29:16 +05303407
lijuang4ece1e72015-08-14 21:02:36 +08003408 /* wipe data */
vijay kumarc65876c2015-04-24 13:29:16 +05303409 struct recovery_message msg;
lijuang4ece1e72015-08-14 21:02:36 +08003410
vijay kumarc65876c2015-04-24 13:29:16 +05303411 snprintf(msg.recovery, sizeof(msg.recovery), "recovery\n--wipe_data");
3412 write_misc(0, &msg, sizeof(msg));
3413
3414 fastboot_okay("");
3415 reboot_device(RECOVERY_MODE);
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003416 }
3417 fastboot_okay("");
3418}
3419
Channagoud Kadabiad259832015-05-29 11:14:17 -07003420static int aboot_frp_unlock(char *pname, void *data, unsigned sz)
3421{
3422 int ret = 1;
3423 uint32_t secret_key;
3424 char seckey_buffer[MAX_RSP_SIZE];
3425
3426 secret_key = aboot_get_secret_key();
3427 if (secret_key)
3428 {
3429 snprintf((char *) seckey_buffer, MAX_RSP_SIZE, "%x", secret_key);
3430 if (!memcmp((void *)data, (void *)seckey_buffer, sz))
3431 {
3432 is_allow_unlock = true;
3433 write_allow_oem_unlock(is_allow_unlock);
3434 ret = 0;
3435 }
3436 }
3437 return ret;
3438}
3439
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003440void cmd_oem_lock(const char *arg, void *data, unsigned sz)
3441{
lijuang4ece1e72015-08-14 21:02:36 +08003442 set_device_unlock(UNLOCK, FALSE);
Shashank Mittal162244e2011-08-08 19:01:25 -07003443}
3444
Shashank Mittala0032282011-08-26 14:50:11 -07003445void cmd_oem_devinfo(const char *arg, void *data, unsigned sz)
3446{
lijuang511a2b52015-08-14 20:50:51 +08003447 char response[MAX_RSP_SIZE];
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003448 snprintf(response, sizeof(response), "\tDevice tampered: %s", (device.is_tampered ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07003449 fastboot_info(response);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003450 snprintf(response, sizeof(response), "\tDevice unlocked: %s", (device.is_unlocked ? "true" : "false"));
3451 fastboot_info(response);
Parth Dixitddbc7352015-10-18 03:13:31 +05303452#if !VBOOT_MOTA
lijuang511a2b52015-08-14 20:50:51 +08003453 snprintf(response, sizeof(response), "\tDevice critical unlocked: %s", (device.is_unlock_critical ? "true" : "false"));
3454 fastboot_info(response);
Parth Dixitddbc7352015-10-18 03:13:31 +05303455#endif
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003456 snprintf(response, sizeof(response), "\tCharger screen enabled: %s", (device.charger_screen_enabled ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07003457 fastboot_info(response);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05303458 snprintf(response, sizeof(response), "\tDisplay panel: %s", (device.display_panel));
3459 fastboot_info(response);
Shashank Mittala0032282011-08-26 14:50:11 -07003460 fastboot_okay("");
3461}
3462
lijuang511a2b52015-08-14 20:50:51 +08003463void cmd_flashing_get_unlock_ability(const char *arg, void *data, unsigned sz)
3464{
3465 char response[MAX_RSP_SIZE];
3466 snprintf(response, sizeof(response), "\tget_unlock_ability: %d", is_allow_unlock);
3467 fastboot_info(response);
3468 fastboot_okay("");
3469}
3470
3471void cmd_flashing_lock_critical(const char *arg, void *data, unsigned sz)
3472{
lijuang4ece1e72015-08-14 21:02:36 +08003473 set_device_unlock(UNLOCK_CRITICAL, FALSE);
lijuang511a2b52015-08-14 20:50:51 +08003474}
3475
3476void cmd_flashing_unlock_critical(const char *arg, void *data, unsigned sz)
3477{
lijuang4ece1e72015-08-14 21:02:36 +08003478 set_device_unlock(UNLOCK_CRITICAL, TRUE);
lijuang511a2b52015-08-14 20:50:51 +08003479}
3480
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07003481void cmd_preflash(const char *arg, void *data, unsigned sz)
3482{
3483 fastboot_okay("");
3484}
3485
Mao Flynn7b379f32015-04-20 00:28:30 +08003486static uint8_t logo_header[LOGO_IMG_HEADER_SIZE];
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303487
Mao Flynn7b379f32015-04-20 00:28:30 +08003488int splash_screen_check_header(logo_img_header *header)
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303489{
Mao Flynn7b379f32015-04-20 00:28:30 +08003490 if (memcmp(header->magic, LOGO_IMG_MAGIC, 8))
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303491 return -1;
Mao Flynn7b379f32015-04-20 00:28:30 +08003492 if (header->width == 0 || header->height == 0)
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303493 return -1;
3494 return 0;
3495}
3496
Mao Flynn7b379f32015-04-20 00:28:30 +08003497int splash_screen_flash()
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003498{
3499 struct ptentry *ptn;
3500 struct ptable *ptable;
Mao Flynn7b379f32015-04-20 00:28:30 +08003501 struct logo_img_header *header;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003502 struct fbcon_config *fb_display = NULL;
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08003503
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303504 ptable = flash_get_ptable();
3505 if (ptable == NULL) {
Mao Flynn7b379f32015-04-20 00:28:30 +08003506 dprintf(CRITICAL, "ERROR: Partition table not found\n");
3507 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303508 }
Mao Flynn7b379f32015-04-20 00:28:30 +08003509
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303510 ptn = ptable_find(ptable, "splash");
3511 if (ptn == NULL) {
3512 dprintf(CRITICAL, "ERROR: splash Partition not found\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003513 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303514 }
Mao Flynn7b379f32015-04-20 00:28:30 +08003515 if (flash_read(ptn, 0, (void *)logo_header, LOGO_IMG_HEADER_SIZE)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303516 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003517 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303518 }
3519
Mao Flynn7b379f32015-04-20 00:28:30 +08003520 header = (struct logo_img_header *)logo_header;
3521 if (splash_screen_check_header(header)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303522 dprintf(CRITICAL, "ERROR: Boot image header invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003523 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303524 }
3525
3526 fb_display = fbcon_display();
3527 if (fb_display) {
Mao Flynn7b379f32015-04-20 00:28:30 +08003528 if (header->type && (header->blocks != 0)) { // RLE24 compressed data
3529 uint8_t *base = (uint8_t *) fb_display->base + LOGO_IMG_OFFSET;
3530
3531 /* if the logo is full-screen size, remove "fbcon_clear()" */
3532 if ((header->width != fb_display->width)
3533 || (header->height != fb_display->height))
3534 fbcon_clear();
3535
3536 if (flash_read(ptn + LOGO_IMG_HEADER_SIZE, 0,
3537 (uint32_t *)base,
3538 (header->blocks * 512))) {
3539 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
3540 return -1;
3541 }
3542 fbcon_extract_to_screen(header, base);
3543 return 0;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003544 }
Mao Flynn7b379f32015-04-20 00:28:30 +08003545
Veera Sundaram Sankaranc33416f2015-09-28 10:25:55 -07003546 if ((header->width > fb_display->width) || (header->height > fb_display->height)) {
3547 dprintf(CRITICAL, "Logo config greater than fb config. Fall back default logo\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003548 return -1;
3549 }
3550
Vineet Bajaj99291ed2014-09-09 12:29:46 +05303551 uint8_t *base = (uint8_t *) fb_display->base;
Mao Flynn7b379f32015-04-20 00:28:30 +08003552 if (flash_read(ptn + LOGO_IMG_HEADER_SIZE, 0,
3553 (uint32_t *)base,
3554 ((((header->width * header->height * fb_display->bpp/8) + 511) >> 9) << 9))) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303555 fbcon_clear();
Vineet Bajaj99291ed2014-09-09 12:29:46 +05303556 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003557 return -1;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003558 }
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303559 }
3560
Mao Flynn7b379f32015-04-20 00:28:30 +08003561 return 0;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303562}
3563
Mao Flynn7b379f32015-04-20 00:28:30 +08003564int splash_screen_mmc()
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303565{
3566 int index = INVALID_PTN;
3567 unsigned long long ptn = 0;
3568 struct fbcon_config *fb_display = NULL;
Mao Flynn7b379f32015-04-20 00:28:30 +08003569 struct logo_img_header *header;
Mao Flynn1893a7f2015-06-03 12:03:36 +08003570 uint32_t blocksize, realsize, readsize;
3571 uint8_t *base;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303572
3573 index = partition_get_index("splash");
3574 if (index == 0) {
3575 dprintf(CRITICAL, "ERROR: splash Partition table not found\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003576 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303577 }
3578
3579 ptn = partition_get_offset(index);
3580 if (ptn == 0) {
3581 dprintf(CRITICAL, "ERROR: splash Partition invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003582 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303583 }
3584
Mao Flynn1893a7f2015-06-03 12:03:36 +08003585 mmc_set_lun(partition_get_lun(index));
3586
3587 blocksize = mmc_get_device_blocksize();
3588 if (blocksize == 0) {
3589 dprintf(CRITICAL, "ERROR:splash Partition invalid blocksize\n");
3590 return -1;
3591 }
3592
3593 fb_display = fbcon_display();
Channagoud Kadabidaf10a62015-07-22 11:51:55 -07003594 if (!fb_display)
3595 {
3596 dprintf(CRITICAL, "ERROR: fb config is not allocated\n");
3597 return -1;
3598 }
3599
Mao Flynn1893a7f2015-06-03 12:03:36 +08003600 base = (uint8_t *) fb_display->base;
3601
3602 if (mmc_read(ptn, (uint32_t *)(base + LOGO_IMG_OFFSET), blocksize)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303603 dprintf(CRITICAL, "ERROR: Cannot read splash image header\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003604 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303605 }
3606
Mao Flynn1893a7f2015-06-03 12:03:36 +08003607 header = (struct logo_img_header *)(base + LOGO_IMG_OFFSET);
Mao Flynn7b379f32015-04-20 00:28:30 +08003608 if (splash_screen_check_header(header)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303609 dprintf(CRITICAL, "ERROR: Splash image header invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003610 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303611 }
3612
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303613 if (fb_display) {
Mao Flynn1893a7f2015-06-03 12:03:36 +08003614 if (header->type && (header->blocks != 0)) { /* 1 RLE24 compressed data */
3615 base += LOGO_IMG_OFFSET;
Mao Flynn7b379f32015-04-20 00:28:30 +08003616
Mao Flynn1893a7f2015-06-03 12:03:36 +08003617 realsize = header->blocks * 512;
3618 readsize = ROUNDUP((realsize + LOGO_IMG_HEADER_SIZE), blocksize) - blocksize;
3619
3620 /* if the logo is not full-screen size, clean screen */
Mao Flynn7b379f32015-04-20 00:28:30 +08003621 if ((header->width != fb_display->width)
3622 || (header->height != fb_display->height))
3623 fbcon_clear();
3624
Mao Flynn1893a7f2015-06-03 12:03:36 +08003625 if (mmc_read(ptn + blocksize, (uint32_t *)(base + blocksize), readsize)) {
Mao Flynn7b379f32015-04-20 00:28:30 +08003626 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
3627 return -1;
3628 }
Mao Flynn7b379f32015-04-20 00:28:30 +08003629
Mao Flynn1893a7f2015-06-03 12:03:36 +08003630 fbcon_extract_to_screen(header, (base + LOGO_IMG_HEADER_SIZE));
3631 } else { /* 2 Raw BGR data */
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303632
Veera Sundaram Sankaranc33416f2015-09-28 10:25:55 -07003633 if ((header->width > fb_display->width) || (header->height > fb_display->height)) {
3634 dprintf(CRITICAL, "Logo config greater than fb config. Fall back default logo\n");
Mao Flynn1893a7f2015-06-03 12:03:36 +08003635 return -1;
3636 }
3637
3638 realsize = header->width * header->height * fb_display->bpp / 8;
3639 readsize = ROUNDUP((realsize + LOGO_IMG_HEADER_SIZE), blocksize) - blocksize;
3640
3641 if (blocksize == LOGO_IMG_HEADER_SIZE) { /* read the content directly */
3642 if (mmc_read((ptn + LOGO_IMG_HEADER_SIZE), (uint32_t *)base, readsize)) {
3643 fbcon_clear();
3644 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
3645 return -1;
3646 }
3647 } else {
3648 if (mmc_read(ptn + blocksize ,
3649 (uint32_t *)(base + LOGO_IMG_OFFSET + blocksize), readsize)) {
3650 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
3651 return -1;
3652 }
3653 memmove(base, (base + LOGO_IMG_OFFSET + LOGO_IMG_HEADER_SIZE), realsize);
3654 }
3655 }
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303656 }
3657
Mao Flynn7b379f32015-04-20 00:28:30 +08003658 return 0;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303659}
3660
Mao Flynn7b379f32015-04-20 00:28:30 +08003661int fetch_image_from_partition()
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303662{
3663 if (target_is_emmc_boot()) {
3664 return splash_screen_mmc();
3665 } else {
3666 return splash_screen_flash();
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003667 }
3668}
3669
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07003670/* Get the size from partiton name */
3671static void get_partition_size(const char *arg, char *response)
3672{
3673 uint64_t ptn = 0;
3674 uint64_t size;
3675 int index = INVALID_PTN;
3676
3677 index = partition_get_index(arg);
3678
3679 if (index == INVALID_PTN)
3680 {
3681 dprintf(CRITICAL, "Invalid partition index\n");
3682 return;
3683 }
3684
3685 ptn = partition_get_offset(index);
3686
3687 if(!ptn)
3688 {
3689 dprintf(CRITICAL, "Invalid partition name %s\n", arg);
3690 return;
3691 }
3692
3693 size = partition_get_size(index);
3694
3695 snprintf(response, MAX_RSP_SIZE, "\t 0x%llx", size);
3696 return;
3697}
3698
3699/*
3700 * Publish the partition type & size info
3701 * fastboot getvar will publish the required information.
3702 * fastboot getvar partition_size:<partition_name>: partition size in hex
3703 * fastboot getvar partition_type:<partition_name>: partition type (ext/fat)
3704 */
3705static void publish_getvar_partition_info(struct getvar_partition_info *info, uint8_t num_parts)
3706{
3707 uint8_t i;
3708
3709 for (i = 0; i < num_parts; i++) {
3710 get_partition_size(info[i].part_name, info[i].size_response);
3711
3712 if (strlcat(info[i].getvar_size, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
3713 {
3714 dprintf(CRITICAL, "partition size name truncated\n");
3715 return;
3716 }
3717 if (strlcat(info[i].getvar_type, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
3718 {
3719 dprintf(CRITICAL, "partition type name truncated\n");
3720 return;
3721 }
3722
3723 /* publish partition size & type info */
3724 fastboot_publish((const char *) info[i].getvar_size, (const char *) info[i].size_response);
3725 fastboot_publish((const char *) info[i].getvar_type, (const char *) info[i].type_response);
3726 }
3727}
3728
lijuang4ece1e72015-08-14 21:02:36 +08003729void get_product_name(unsigned char *buf)
3730{
3731 snprintf((char*)buf, MAX_RSP_SIZE, "%s", TARGET(BOARD));
3732 return;
3733}
3734
3735void get_bootloader_version(unsigned char *buf)
3736{
3737 snprintf((char*)buf, MAX_RSP_SIZE, "%s", device.bootloader_version);
3738 return;
3739}
3740
3741void get_baseband_version(unsigned char *buf)
3742{
3743 snprintf((char*)buf, MAX_RSP_SIZE, "%s", device.radio_version);
3744 return;
3745}
3746
3747bool is_device_locked()
3748{
3749 return device.is_unlocked ? false:true;
3750}
3751
Amol Jadi5edf3552013-07-23 14:15:34 -07003752/* register commands and variables for fastboot */
3753void aboot_fastboot_register_commands(void)
3754{
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003755 int i;
lijuangf16461c2015-08-03 17:09:34 +08003756 char hw_platform_buf[MAX_RSP_SIZE];
Amol Jadi5edf3552013-07-23 14:15:34 -07003757
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003758 struct fastboot_cmd_desc cmd_list[] = {
lijuang511a2b52015-08-14 20:50:51 +08003759 /* By default the enabled list is empty. */
3760 {"", NULL},
3761 /* move commands enclosed within the below ifndef to here
3762 * if they need to be enabled in user build.
3763 */
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003764#ifndef DISABLE_FASTBOOT_CMDS
lijuang511a2b52015-08-14 20:50:51 +08003765 /* Register the following commands only for non-user builds */
3766 {"flash:", cmd_flash},
3767 {"erase:", cmd_erase},
3768 {"boot", cmd_boot},
3769 {"continue", cmd_continue},
3770 {"reboot", cmd_reboot},
3771 {"reboot-bootloader", cmd_reboot_bootloader},
3772 {"oem unlock", cmd_oem_unlock},
3773 {"oem unlock-go", cmd_oem_unlock_go},
3774 {"oem lock", cmd_oem_lock},
3775 {"flashing unlock", cmd_oem_unlock},
3776 {"flashing lock", cmd_oem_lock},
3777 {"flashing lock_critical", cmd_flashing_lock_critical},
3778 {"flashing unlock_critical", cmd_flashing_unlock_critical},
3779 {"flashing get_unlock_ability", cmd_flashing_get_unlock_ability},
3780 {"oem device-info", cmd_oem_devinfo},
3781 {"preflash", cmd_preflash},
3782 {"oem enable-charger-screen", cmd_oem_enable_charger_screen},
3783 {"oem disable-charger-screen", cmd_oem_disable_charger_screen},
lijuanga25d8bb2015-09-16 13:11:52 +08003784 {"oem off-mode-charge", cmd_oem_off_mode_charger},
lijuang511a2b52015-08-14 20:50:51 +08003785 {"oem select-display-panel", cmd_oem_select_display_panel},
Channagoud Kadabi736c4962015-08-21 11:56:52 -07003786#if UNITTEST_FW_SUPPORT
Channagoud Kadabid5ddf482015-09-28 10:31:35 -07003787 {"oem run-tests", cmd_oem_runtests},
Channagoud Kadabi736c4962015-08-21 11:56:52 -07003788#endif
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003789#endif
lijuang511a2b52015-08-14 20:50:51 +08003790 };
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003791
3792 int fastboot_cmds_count = sizeof(cmd_list)/sizeof(cmd_list[0]);
3793 for (i = 1; i < fastboot_cmds_count; i++)
3794 fastboot_register(cmd_list[i].name,cmd_list[i].cb);
3795
Amol Jadi5edf3552013-07-23 14:15:34 -07003796 /* publish variables and their values */
3797 fastboot_publish("product", TARGET(BOARD));
3798 fastboot_publish("kernel", "lk");
3799 fastboot_publish("serialno", sn_buf);
3800
3801 /*
3802 * partition info is supported only for emmc partitions
3803 * Calling this for NAND prints some error messages which
3804 * is harmless but misleading. Avoid calling this for NAND
3805 * devices.
3806 */
3807 if (target_is_emmc_boot())
3808 publish_getvar_partition_info(part_info, ARRAY_SIZE(part_info));
3809
3810 /* Max download size supported */
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003811 snprintf(max_download_size, MAX_RSP_SIZE, "\t0x%x",
3812 target_get_max_flash_size());
Amol Jadi5edf3552013-07-23 14:15:34 -07003813 fastboot_publish("max-download-size", (const char *) max_download_size);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003814 /* Is the charger screen check enabled */
3815 snprintf(charger_screen_enabled, MAX_RSP_SIZE, "%d",
3816 device.charger_screen_enabled);
3817 fastboot_publish("charger-screen-enabled",
3818 (const char *) charger_screen_enabled);
lijuanga25d8bb2015-09-16 13:11:52 +08003819 fastboot_publish("off-mode-charge", (const char *) charger_screen_enabled);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05303820 snprintf(panel_display_mode, MAX_RSP_SIZE, "%s",
3821 device.display_panel);
3822 fastboot_publish("display-panel",
3823 (const char *) panel_display_mode);
Channagoud Kadabiaafbbfc2015-03-04 17:52:56 -08003824 fastboot_publish("version-bootloader", (const char *) device.bootloader_version);
3825 fastboot_publish("version-baseband", (const char *) device.radio_version);
lijuangf16461c2015-08-03 17:09:34 +08003826 fastboot_publish("secure", is_secure_boot_enable()? "yes":"no");
3827 smem_get_hw_platform_name((unsigned char *) hw_platform_buf, sizeof(hw_platform_buf));
3828 snprintf(get_variant, MAX_RSP_SIZE, "%s %s", hw_platform_buf,
3829 target_is_emmc_boot()? "eMMC":"UFS");
3830 fastboot_publish("variant", (const char *) get_variant);
lijuang65c5a822015-08-29 16:35:36 +08003831#if CHECK_BAT_VOLTAGE
lijuang102dfa92015-10-09 18:31:03 +08003832 update_battery_status();
lijuang65c5a822015-08-29 16:35:36 +08003833 fastboot_publish("battery-voltage", (const char *) battery_voltage);
lijuang102dfa92015-10-09 18:31:03 +08003834 fastboot_publish("battery-soc-ok", (const char *) battery_soc_ok);
lijuang65c5a822015-08-29 16:35:36 +08003835#endif
Amol Jadi5edf3552013-07-23 14:15:34 -07003836}
3837
Brian Swetland9c4c0752009-01-25 16:23:50 -08003838void aboot_init(const struct app_descriptor *app)
3839{
Shashank Mittal4f99a882010-02-01 13:58:50 -08003840 unsigned reboot_mode = 0;
Chandan Uddarajubedca152010-06-02 23:05:15 -07003841
lijuang39831732016-01-08 17:49:02 +08003842 /* Initialise wdog to catch early lk crashes */
3843#if WDOG_SUPPORT
3844 msm_wdog_init();
3845#endif
3846
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07003847 /* Setup page size information for nv storage */
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003848 if (target_is_emmc_boot())
3849 {
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07003850 page_size = mmc_page_size();
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003851 page_mask = page_size - 1;
Gaurav Nebhwani4d2389c2016-03-17 21:10:05 +05303852 mmc_blocksize = mmc_get_device_blocksize();
3853 mmc_blocksize_mask = mmc_blocksize - 1;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003854 }
3855 else
3856 {
3857 page_size = flash_page_size();
3858 page_mask = page_size - 1;
3859 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07003860 ASSERT((MEMBASE + MEMSIZE) > MEMBASE);
3861
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003862 read_device_info(&device);
vijay kumarc65876c2015-04-24 13:29:16 +05303863 read_allow_oem_unlock(&device);
Shashank Mittal162244e2011-08-08 19:01:25 -07003864
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08003865 /* Display splash screen if enabled */
3866#if DISPLAY_SPLASH_SCREEN
lijuang99c02d82015-02-13 19:04:34 +08003867#if NO_ALARM_DISPLAY
3868 if (!check_alarm_boot()) {
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08003869#endif
lijuang99c02d82015-02-13 19:04:34 +08003870 dprintf(SPEW, "Display Init: Start\n");
Ajay Singh Parmara7865a82015-04-16 21:27:52 -07003871#if DISPLAY_HDMI_PRIMARY
3872 if (!strlen(device.display_panel))
3873 strlcpy(device.display_panel, DISPLAY_PANEL_HDMI,
3874 sizeof(device.display_panel));
3875#endif
Channagoud Kadabi90869ce2015-04-27 11:15:14 -07003876#if ENABLE_WBC
Channagoud Kadabid801ac72015-08-26 11:21:51 -07003877 /* Wait if the display shutdown is in progress */
3878 while(pm_app_display_shutdown_in_prgs());
Channagoud Kadabi90869ce2015-04-27 11:15:14 -07003879 if (!pm_appsbl_display_init_done())
3880 target_display_init(device.display_panel);
3881 else
3882 display_image_on_screen();
3883#else
lijuang99c02d82015-02-13 19:04:34 +08003884 target_display_init(device.display_panel);
Channagoud Kadabi90869ce2015-04-27 11:15:14 -07003885#endif
lijuang99c02d82015-02-13 19:04:34 +08003886 dprintf(SPEW, "Display Init: Done\n");
3887#if NO_ALARM_DISPLAY
3888 }
3889#endif
3890#endif
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08003891
Greg Griscod6250552011-06-29 14:40:23 -07003892 target_serialno((unsigned char *) sn_buf);
Ajay Dudanib06c05f2011-05-12 14:46:10 -07003893 dprintf(SPEW,"serial number: %s\n",sn_buf);
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -08003894
Dhaval Patel223ec952013-07-18 14:49:44 -07003895 memset(display_panel_buf, '\0', MAX_PANEL_BUF_SIZE);
3896
Matthew Qindefd5562014-07-11 18:02:40 +08003897 /*
3898 * Check power off reason if user force reset,
3899 * if yes phone will do normal boot.
3900 */
3901 if (is_user_force_reset())
3902 goto normal_boot;
3903
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003904 /* Check if we should do something other than booting up */
Ameya Thakur0b9c2442013-05-10 13:22:56 -07003905 if (keys_get_state(KEY_VOLUMEUP) && keys_get_state(KEY_VOLUMEDOWN))
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07003906 {
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03003907 dprintf(ALWAYS,"dload mode key sequence detected\n");
lijuang395b5e62015-11-19 17:39:44 +08003908 reboot_device(EMERGENCY_DLOAD);
3909 dprintf(CRITICAL,"Failed to reboot into dload mode\n");
3910
Ameya Thakur0b9c2442013-05-10 13:22:56 -07003911 boot_into_fastboot = true;
3912 }
3913 if (!boot_into_fastboot)
3914 {
3915 if (keys_get_state(KEY_HOME) || keys_get_state(KEY_VOLUMEUP))
3916 boot_into_recovery = 1;
3917 if (!boot_into_recovery &&
3918 (keys_get_state(KEY_BACK) || keys_get_state(KEY_VOLUMEDOWN)))
Pavel Nedev5d91d412013-04-29 11:34:24 +03003919 boot_into_fastboot = true;
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07003920 }
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003921 #if NO_KEYPAD_DRIVER
Kinson Chik0b1c8162011-08-31 16:31:57 -07003922 if (fastboot_trigger())
Pavel Nedev5d91d412013-04-29 11:34:24 +03003923 boot_into_fastboot = true;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003924 #endif
Chandan Uddarajubedca152010-06-02 23:05:15 -07003925
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003926#if USE_PON_REBOOT_REG
3927 reboot_mode = check_hard_reboot_mode();
3928#else
Ajay Dudani77421292010-10-27 19:34:06 -07003929 reboot_mode = check_reboot_mode();
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003930#endif
3931 if (reboot_mode == RECOVERY_MODE)
3932 {
Ajay Dudani77421292010-10-27 19:34:06 -07003933 boot_into_recovery = 1;
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003934 }
3935 else if(reboot_mode == FASTBOOT_MODE)
3936 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03003937 boot_into_fastboot = true;
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003938 }
3939 else if(reboot_mode == ALARM_BOOT)
3940 {
Matthew Qind886f3c2014-01-17 16:52:01 +08003941 boot_reason_alarm = true;
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003942 }
Parth Dixit207573a2015-10-27 17:26:21 +05303943#if VERIFIED_BOOT
Parth Dixitddbc7352015-10-18 03:13:31 +05303944#if !VBOOT_MOTA
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003945 else if (reboot_mode == DM_VERITY_ENFORCING)
3946 {
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07003947 device.verity_mode = 1;
3948 write_device_info(&device);
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003949 }
3950 else if (reboot_mode == DM_VERITY_LOGGING)
3951 {
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07003952 device.verity_mode = 0;
3953 write_device_info(&device);
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003954 }
3955 else if (reboot_mode == DM_VERITY_KEYSCLEAR)
3956 {
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07003957 if(send_delete_keys_to_tz())
3958 ASSERT(0);
Ajay Dudani77421292010-10-27 19:34:06 -07003959 }
Parth Dixit207573a2015-10-27 17:26:21 +05303960#endif
Parth Dixitddbc7352015-10-18 03:13:31 +05303961#endif
Ajay Dudani77421292010-10-27 19:34:06 -07003962
Matthew Qindefd5562014-07-11 18:02:40 +08003963normal_boot:
Pavel Nedev5d91d412013-04-29 11:34:24 +03003964 if (!boot_into_fastboot)
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003965 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03003966 if (target_is_emmc_boot())
Shashank Mittala0032282011-08-26 14:50:11 -07003967 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03003968 if(emmc_recovery_init())
3969 dprintf(ALWAYS,"error in emmc_recovery_init\n");
3970 if(target_use_signed_kernel())
Shashank Mittala0032282011-08-26 14:50:11 -07003971 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03003972 if((device.is_unlocked) || (device.is_tampered))
3973 {
3974 #ifdef TZ_TAMPER_FUSE
3975 set_tamper_fuse_cmd();
3976 #endif
3977 #if USE_PCOM_SECBOOT
3978 set_tamper_flag(device.is_tampered);
3979 #endif
3980 }
Shashank Mittala0032282011-08-26 14:50:11 -07003981 }
Amit Blay6281ebc2015-01-11 14:44:08 +02003982
Pavel Nedev5d91d412013-04-29 11:34:24 +03003983 boot_linux_from_mmc();
Shashank Mittala0032282011-08-26 14:50:11 -07003984 }
Pavel Nedev5d91d412013-04-29 11:34:24 +03003985 else
3986 {
3987 recovery_init();
3988 #if USE_PCOM_SECBOOT
3989 if((device.is_unlocked) || (device.is_tampered))
3990 set_tamper_flag(device.is_tampered);
3991 #endif
3992 boot_linux_from_flash();
3993 }
3994 dprintf(CRITICAL, "ERROR: Could not do normal boot. Reverting "
3995 "to fastboot mode.\n");
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003996 }
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003997
Amol Jadi5edf3552013-07-23 14:15:34 -07003998 /* We are here means regular boot did not happen. Start fastboot. */
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07003999
Amol Jadi5edf3552013-07-23 14:15:34 -07004000 /* register aboot specific fastboot commands */
4001 aboot_fastboot_register_commands();
Amol Jadi57abe4c2011-05-24 15:47:27 -07004002
Amol Jadi5edf3552013-07-23 14:15:34 -07004003 /* dump partition table for debug info */
Kinson Chikf1a43512011-07-14 11:28:39 -07004004 partition_dump();
Amol Jadi5edf3552013-07-23 14:15:34 -07004005
4006 /* initialize and start fastboot */
4007 fastboot_init(target_get_scratch_address(), target_get_max_flash_size());
lijuang4ece1e72015-08-14 21:02:36 +08004008#if FBCON_DISPLAY_MSG
lijuang9a7d3b92015-11-30 14:41:24 +08004009 display_fastboot_menu();
lijuang4ece1e72015-08-14 21:02:36 +08004010#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08004011}
4012
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07004013uint32_t get_page_size()
4014{
4015 return page_size;
4016}
4017
Amir Samuelov57a6fa22013-06-05 16:36:43 +03004018/*
4019 * Calculated and save hash (SHA256) for non-signed boot image.
4020 *
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07004021 * @param image_addr - Boot image address
4022 * @param image_size - Size of the boot image
Amir Samuelov57a6fa22013-06-05 16:36:43 +03004023 *
4024 * @return int - 0 on success, negative value on failure.
4025 */
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07004026static int aboot_save_boot_hash_mmc(uint32_t image_addr, uint32_t image_size)
Amir Samuelov57a6fa22013-06-05 16:36:43 +03004027{
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07004028 unsigned int digest[8];
4029#if IMAGE_VERIF_ALGO_SHA1
4030 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA1;
4031#else
4032 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA256;
4033#endif
Amir Samuelov57a6fa22013-06-05 16:36:43 +03004034
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07004035 target_crypto_init_params();
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -08004036 hash_find((unsigned char *) image_addr, image_size, (unsigned char *)&digest, auth_algo);
Amir Samuelov57a6fa22013-06-05 16:36:43 +03004037
4038 save_kernel_hash_cmd(digest);
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07004039 dprintf(INFO, "aboot_save_boot_hash_mmc: imagesize_actual size %d bytes.\n", (int) image_size);
Amir Samuelov57a6fa22013-06-05 16:36:43 +03004040
4041 return 0;
4042}
4043
Brian Swetland9c4c0752009-01-25 16:23:50 -08004044APP_START(aboot)
4045 .init = aboot_init,
4046APP_END