blob: b16e432be2ad2cf9defc5ad9b732b8496687848b [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{
Channagoud Kadabi86b0c112016-03-16 19:23:16 -0700190#if ENABLE_VB_ATTEST
191 {false, "eio"},
192#else
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700193 {false, "logging"},
Channagoud Kadabi86b0c112016-03-16 19:23:16 -0700194#endif
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700195 {true, "enforcing"},
196};
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700197struct verified_boot_state_name vbsn[] =
198{
199 {GREEN, "green"},
200 {ORANGE, "orange"},
201 {YELLOW,"yellow"},
202 {RED,"red" },
203};
204#endif
Parth Dixitddbc7352015-10-18 03:13:31 +0530205#endif
Channagoud Kadabi86b0c112016-03-16 19:23:16 -0700206/*As per spec delay wait time before shutdown in Red state*/
207#define DELAY_WAIT 30000
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700208static unsigned page_size = 0;
209static unsigned page_mask = 0;
Gaurav Nebhwani4d2389c2016-03-17 21:10:05 +0530210static unsigned mmc_blocksize = 0;
211static unsigned mmc_blocksize_mask = 0;
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700212static char ffbm_mode_string[FFBM_MODE_BUF_SIZE];
213static bool boot_into_ffbm;
vijay kumar870515d2015-08-31 16:37:24 +0530214static char *target_boot_params = NULL;
Matthew Qind886f3c2014-01-17 16:52:01 +0800215static bool boot_reason_alarm;
Channagoud Kadabi80a182b2015-03-11 17:04:23 -0700216static bool devinfo_present = true;
Channagoud Kadabi736c4962015-08-21 11:56:52 -0700217bool boot_into_fastboot = false;
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700218
Shashank Mittalcd98d472011-08-02 14:29:24 -0700219/* Assuming unauthorized kernel image by default */
220static int auth_kernel_img = 0;
Parth Dixit6f5822f2015-10-18 01:20:53 +0530221#if VBOOT_MOTA
222static device_info device = {DEVICE_MAGIC, 0, 0, 0, 0, {0}, {0},{0}};
223#else
lijuang511a2b52015-08-14 20:50:51 +0800224static device_info device = {DEVICE_MAGIC, 0, 0, 0, 0, {0}, {0},{0}, 1};
Parth Dixit6f5822f2015-10-18 01:20:53 +0530225#endif
vijay kumarc65876c2015-04-24 13:29:16 +0530226static bool is_allow_unlock = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -0700227
vijay kumarca2e6812015-07-08 20:28:25 +0530228static char frp_ptns[2][8] = {"config","frp"};
229
lijuang511a2b52015-08-14 20:50:51 +0800230static const char *critical_flash_allowed_ptn[] = {
231 "aboot",
232 "rpm",
233 "tz",
234 "sbl",
235 "sdi",
236 "sbl1",
237 "xbl",
238 "hyp",
239 "pmic",
240 "bootloader",
241 "devinfo",
242 "partition"};
243
Dima Zavin42168f22009-01-30 11:52:22 -0800244struct atag_ptbl_entry
245{
246 char name[16];
247 unsigned offset;
248 unsigned size;
249 unsigned flags;
250};
251
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700252/*
253 * Partition info, required to be published
254 * for fastboot
255 */
256struct getvar_partition_info {
257 const char part_name[MAX_GPT_NAME_SIZE]; /* Partition name */
258 char getvar_size[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for size */
259 char getvar_type[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for type */
260 char size_response[MAX_RSP_SIZE]; /* fastboot response for size */
261 char type_response[MAX_RSP_SIZE]; /* fastboot response for type */
262};
263
264/*
265 * Right now, we are publishing the info for only
266 * three partitions
267 */
268struct getvar_partition_info part_info[] =
269{
270 { "system" , "partition-size:", "partition-type:", "", "ext4" },
271 { "userdata", "partition-size:", "partition-type:", "", "ext4" },
272 { "cache" , "partition-size:", "partition-type:", "", "ext4" },
273};
274
275char max_download_size[MAX_RSP_SIZE];
Ameya Thakur11cf1a62013-08-05 12:44:48 -0700276char charger_screen_enabled[MAX_RSP_SIZE];
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800277char sn_buf[13];
Dhaval Patel223ec952013-07-18 14:49:44 -0700278char display_panel_buf[MAX_PANEL_BUF_SIZE];
Unnati Gandhi62c8ab82014-01-24 11:01:01 +0530279char panel_display_mode[MAX_RSP_SIZE];
lijuang102dfa92015-10-09 18:31:03 +0800280
281#if CHECK_BAT_VOLTAGE
lijuang65c5a822015-08-29 16:35:36 +0800282char battery_voltage[MAX_RSP_SIZE];
lijuang102dfa92015-10-09 18:31:03 +0800283char battery_soc_ok [MAX_RSP_SIZE];
284#endif
285
lijuangf16461c2015-08-03 17:09:34 +0800286char get_variant[MAX_RSP_SIZE];
Greg Griscod6250552011-06-29 14:40:23 -0700287
Greg Griscod2471ef2011-07-14 13:00:42 -0700288extern int emmc_recovery_init(void);
289
Kinson Chik0b1c8162011-08-31 16:31:57 -0700290#if NO_KEYPAD_DRIVER
291extern int fastboot_trigger(void);
292#endif
Greg Griscod2471ef2011-07-14 13:00:42 -0700293
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800294static void update_ker_tags_rdisk_addr(struct boot_img_hdr *hdr, bool is_arm64)
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700295{
296 /* overwrite the destination of specified for the project */
Channagoud Kadabi7042fa32013-04-26 16:44:14 -0700297#ifdef ABOOT_IGNORE_BOOT_HEADER_ADDRS
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800298 if (is_arm64)
299 hdr->kernel_addr = ABOOT_FORCE_KERNEL64_ADDR;
300 else
301 hdr->kernel_addr = ABOOT_FORCE_KERNEL_ADDR;
Channagoud Kadabi7042fa32013-04-26 16:44:14 -0700302 hdr->ramdisk_addr = ABOOT_FORCE_RAMDISK_ADDR;
303 hdr->tags_addr = ABOOT_FORCE_TAGS_ADDR;
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700304#endif
305}
306
Dima Zavin42168f22009-01-30 11:52:22 -0800307static void ptentry_to_tag(unsigned **ptr, struct ptentry *ptn)
308{
309 struct atag_ptbl_entry atag_ptn;
310
311 memcpy(atag_ptn.name, ptn->name, 16);
312 atag_ptn.name[15] = '\0';
313 atag_ptn.offset = ptn->start;
314 atag_ptn.size = ptn->length;
315 atag_ptn.flags = ptn->flags;
316 memcpy(*ptr, &atag_ptn, sizeof(struct atag_ptbl_entry));
317 *ptr += sizeof(struct atag_ptbl_entry) / sizeof(unsigned);
318}
Brian Swetland9c4c0752009-01-25 16:23:50 -0800319
lijuang102dfa92015-10-09 18:31:03 +0800320#if CHECK_BAT_VOLTAGE
321void update_battery_status(void)
322{
323 snprintf(battery_voltage,MAX_RSP_SIZE, "%d",target_get_battery_voltage());
324 snprintf(battery_soc_ok ,MAX_RSP_SIZE, "%s",target_battery_soc_ok()? "yes":"no");
325}
326#endif
327
Neeti Desaie245d492012-06-01 12:52:13 -0700328unsigned char *update_cmdline(const char * cmdline)
Brian Swetland9c4c0752009-01-25 16:23:50 -0800329{
David Ng183a7422009-12-07 14:55:21 -0800330 int cmdline_len = 0;
331 int have_cmdline = 0;
Amol Jadi168b7712012-03-06 16:15:00 -0800332 unsigned char *cmdline_final = NULL;
Neeti Desaie245d492012-06-01 12:52:13 -0700333 int pause_at_bootup = 0;
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800334 bool warm_boot = false;
Pavel Nedev5614d222013-06-17 18:01:02 +0300335 bool gpt_exists = partition_gpt_exists();
Joonwoo Park61112782013-10-02 19:50:39 -0700336 int have_target_boot_params = 0;
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700337 char *boot_dev_buf = NULL;
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200338 bool is_mdtp_activated = 0;
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700339#if VERIFIED_BOOT
Parth Dixitddbc7352015-10-18 03:13:31 +0530340#if !VBOOT_MOTA
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700341 uint32_t boot_state = boot_verify_get_state();
342#endif
Parth Dixitddbc7352015-10-18 03:13:31 +0530343#endif
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700344
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200345#ifdef MDTP_SUPPORT
346 mdtp_activated(&is_mdtp_activated);
347#endif /* MDTP_SUPPORT */
Dima Zavin42168f22009-01-30 11:52:22 -0800348
Brian Swetland9c4c0752009-01-25 16:23:50 -0800349 if (cmdline && cmdline[0]) {
David Ng183a7422009-12-07 14:55:21 -0800350 cmdline_len = strlen(cmdline);
351 have_cmdline = 1;
352 }
353 if (target_is_emmc_boot()) {
354 cmdline_len += strlen(emmc_cmdline);
Sridhar Parasuram7bd4aaf2015-02-12 11:14:38 -0800355#if USE_BOOTDEV_CMDLINE
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700356 boot_dev_buf = (char *) malloc(sizeof(char) * BOOT_DEV_MAX_LEN);
357 ASSERT(boot_dev_buf);
358 platform_boot_dev_cmdline(boot_dev_buf);
359 cmdline_len += strlen(boot_dev_buf);
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700360#endif
David Ng183a7422009-12-07 14:55:21 -0800361 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800362
363 cmdline_len += strlen(usb_sn_cmdline);
364 cmdline_len += strlen(sn_buf);
365
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700366#if VERIFIED_BOOT
Parth Dixitddbc7352015-10-18 03:13:31 +0530367#if !VBOOT_MOTA
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700368 cmdline_len += strlen(verified_state) + strlen(vbsn[boot_state].name);
Sridhar Parasuram7d8bb9a2015-09-23 18:21:19 -0700369 if ((device.verity_mode != 0 ) && (device.verity_mode != 1))
370 {
371 dprintf(CRITICAL, "Devinfo paritition possibly corrupted!!!. Please erase devinfo partition to continue booting\n");
372 ASSERT(0);
373 }
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700374 cmdline_len += strlen(verity_mode) + strlen(vbvm[device.verity_mode].name);
Parth Dixita5715a02015-10-29 12:25:10 +0530375 cmdline_len += strlen(keymaster_v1);
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700376#endif
Parth Dixitddbc7352015-10-18 03:13:31 +0530377#endif
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700378
Pavel Nedev5614d222013-06-17 18:01:02 +0300379 if (boot_into_recovery && gpt_exists)
380 cmdline_len += strlen(secondary_gpt_enable);
381
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200382 if(is_mdtp_activated)
383 cmdline_len += strlen(mdtp_activated_flag);
384
Pavel Nedev328ac822013-04-05 15:25:11 +0300385 if (boot_into_ffbm) {
386 cmdline_len += strlen(androidboot_mode);
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700387 cmdline_len += strlen(ffbm_mode_string);
Pavel Nedev898298c2013-02-27 12:36:09 -0800388 /* reduce kernel console messages to speed-up boot */
389 cmdline_len += strlen(loglevel);
Matthew Qind886f3c2014-01-17 16:52:01 +0800390 } else if (boot_reason_alarm) {
391 cmdline_len += strlen(alarmboot_cmdline);
Zhenhua Huang431dafa2015-06-30 16:13:37 +0800392 } else if ((target_build_variant_user() || device.charger_screen_enabled)
393 && target_pause_for_battery_charge()) {
David Ngf773dde2010-07-26 19:55:08 -0700394 pause_at_bootup = 1;
395 cmdline_len += strlen(battchg_pause);
396 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800397
Shashank Mittalcd98d472011-08-02 14:29:24 -0700398 if(target_use_signed_kernel() && auth_kernel_img) {
399 cmdline_len += strlen(auth_kernel);
400 }
401
Joonwoo Park61112782013-10-02 19:50:39 -0700402 if (get_target_boot_params(cmdline, boot_into_recovery ? "recoveryfs" :
403 "system",
vijay kumar870515d2015-08-31 16:37:24 +0530404 &target_boot_params) == 0) {
Joonwoo Park61112782013-10-02 19:50:39 -0700405 have_target_boot_params = 1;
406 cmdline_len += strlen(target_boot_params);
407 }
408
Ajay Dudanid04110c2011-01-17 23:55:07 -0800409 /* Determine correct androidboot.baseband to use */
410 switch(target_baseband())
411 {
412 case BASEBAND_APQ:
413 cmdline_len += strlen(baseband_apq);
414 break;
415
416 case BASEBAND_MSM:
417 cmdline_len += strlen(baseband_msm);
418 break;
419
420 case BASEBAND_CSFB:
421 cmdline_len += strlen(baseband_csfb);
422 break;
423
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800424 case BASEBAND_SVLTE2A:
425 cmdline_len += strlen(baseband_svlte2a);
Ajay Dudanid04110c2011-01-17 23:55:07 -0800426 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700427
428 case BASEBAND_MDM:
429 cmdline_len += strlen(baseband_mdm);
430 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700431
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800432 case BASEBAND_MDM2:
433 cmdline_len += strlen(baseband_mdm2);
434 break;
435
Amol Jadi5c61a952012-05-04 17:05:35 -0700436 case BASEBAND_SGLTE:
437 cmdline_len += strlen(baseband_sglte);
438 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530439
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800440 case BASEBAND_SGLTE2:
441 cmdline_len += strlen(baseband_sglte2);
442 break;
443
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530444 case BASEBAND_DSDA:
445 cmdline_len += strlen(baseband_dsda);
446 break;
Amol Jadi2a15a272013-01-22 12:03:36 -0800447
448 case BASEBAND_DSDA2:
449 cmdline_len += strlen(baseband_dsda2);
450 break;
Vijay Kumar Pendotib228cfc2016-06-13 20:15:23 +0530451 case BASEBAND_APQ_NOWGR:
452 cmdline_len += strlen(baseband_apq_nowgr);
453 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800454 }
455
Lijuan Gao4a5b8322014-07-24 10:38:42 +0800456 if (cmdline) {
457 if ((strstr(cmdline, DISPLAY_DEFAULT_PREFIX) == NULL) &&
Padmanabhan Komandurubccbcdc2015-06-30 16:19:24 +0530458 target_display_panel_node(display_panel_buf,
459 MAX_PANEL_BUF_SIZE) &&
Lijuan Gao4a5b8322014-07-24 10:38:42 +0800460 strlen(display_panel_buf)) {
461 cmdline_len += strlen(display_panel_buf);
462 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700463 }
464
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800465 if (target_warm_boot()) {
466 warm_boot = true;
467 cmdline_len += strlen(warmboot_cmdline);
468 }
469
Channagoud Kadabi85c7ec32016-01-28 23:09:21 -0800470#if TARGET_CMDLINE_SUPPORT
471 char *target_cmdline_buf = malloc(TARGET_MAX_CMDLNBUF);
472 int target_cmd_line_len;
473 ASSERT(target_cmdline_buf);
474 target_cmd_line_len = target_update_cmdline(target_cmdline_buf);
475 cmdline_len += target_cmd_line_len;
476#endif
477
David Ng183a7422009-12-07 14:55:21 -0800478 if (cmdline_len > 0) {
479 const char *src;
Maria Yu52254c02014-07-04 16:14:54 +0800480 unsigned char *dst;
481
482 cmdline_final = (unsigned char*) malloc((cmdline_len + 4) & (~3));
483 ASSERT(cmdline_final != NULL);
vijay kumar287bb542015-09-29 13:01:52 +0530484 memset((void *)cmdline_final, 0, sizeof(*cmdline_final));
Maria Yu52254c02014-07-04 16:14:54 +0800485 dst = cmdline_final;
Neeti Desaie245d492012-06-01 12:52:13 -0700486
Amol Jadi168b7712012-03-06 16:15:00 -0800487 /* Save start ptr for debug print */
David Ng183a7422009-12-07 14:55:21 -0800488 if (have_cmdline) {
489 src = cmdline;
490 while ((*dst++ = *src++));
491 }
492 if (target_is_emmc_boot()) {
493 src = emmc_cmdline;
494 if (have_cmdline) --dst;
David Ngf773dde2010-07-26 19:55:08 -0700495 have_cmdline = 1;
496 while ((*dst++ = *src++));
Sridhar Parasuram7bd4aaf2015-02-12 11:14:38 -0800497#if USE_BOOTDEV_CMDLINE
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700498 src = boot_dev_buf;
499 if (have_cmdline) --dst;
500 while ((*dst++ = *src++));
501#endif
David Ngf773dde2010-07-26 19:55:08 -0700502 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800503
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700504#if VERIFIED_BOOT
Parth Dixitddbc7352015-10-18 03:13:31 +0530505#if !VBOOT_MOTA
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700506 src = verified_state;
507 if(have_cmdline) --dst;
508 have_cmdline = 1;
509 while ((*dst++ = *src++));
510 src = vbsn[boot_state].name;
511 if(have_cmdline) --dst;
512 while ((*dst++ = *src++));
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700513
Sridhar Parasuram7d8bb9a2015-09-23 18:21:19 -0700514 if ((device.verity_mode != 0 ) && (device.verity_mode != 1))
515 {
516 dprintf(CRITICAL, "Devinfo paritition possibly corrupted!!!. Please erase devinfo partition to continue booting\n");
517 ASSERT(0);
518 }
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700519 src = verity_mode;
520 if(have_cmdline) --dst;
521 while ((*dst++ = *src++));
522 src = vbvm[device.verity_mode].name;
523 if(have_cmdline) -- dst;
524 while ((*dst++ = *src++));
Parth Dixita5715a02015-10-29 12:25:10 +0530525 src = keymaster_v1;
526 if(have_cmdline) --dst;
527 while ((*dst++ = *src++));
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700528#endif
Parth Dixitddbc7352015-10-18 03:13:31 +0530529#endif
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800530 src = usb_sn_cmdline;
531 if (have_cmdline) --dst;
532 have_cmdline = 1;
533 while ((*dst++ = *src++));
534 src = sn_buf;
535 if (have_cmdline) --dst;
536 have_cmdline = 1;
537 while ((*dst++ = *src++));
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800538 if (warm_boot) {
539 if (have_cmdline) --dst;
540 src = warmboot_cmdline;
541 while ((*dst++ = *src++));
542 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800543
Pavel Nedev5614d222013-06-17 18:01:02 +0300544 if (boot_into_recovery && gpt_exists) {
545 src = secondary_gpt_enable;
546 if (have_cmdline) --dst;
547 while ((*dst++ = *src++));
548 }
549
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200550 if (is_mdtp_activated) {
551 src = mdtp_activated_flag;
552 if (have_cmdline) --dst;
553 while ((*dst++ = *src++));
554 }
555
Pavel Nedev328ac822013-04-05 15:25:11 +0300556 if (boot_into_ffbm) {
557 src = androidboot_mode;
558 if (have_cmdline) --dst;
559 while ((*dst++ = *src++));
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700560 src = ffbm_mode_string;
Pavel Nedev328ac822013-04-05 15:25:11 +0300561 if (have_cmdline) --dst;
562 while ((*dst++ = *src++));
Pavel Nedev898298c2013-02-27 12:36:09 -0800563 src = loglevel;
564 if (have_cmdline) --dst;
565 while ((*dst++ = *src++));
Matthew Qind886f3c2014-01-17 16:52:01 +0800566 } else if (boot_reason_alarm) {
567 src = alarmboot_cmdline;
568 if (have_cmdline) --dst;
569 while ((*dst++ = *src++));
Pavel Nedev328ac822013-04-05 15:25:11 +0300570 } else if (pause_at_bootup) {
David Ngf773dde2010-07-26 19:55:08 -0700571 src = battchg_pause;
572 if (have_cmdline) --dst;
David Ng183a7422009-12-07 14:55:21 -0800573 while ((*dst++ = *src++));
574 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800575
Shashank Mittalcd98d472011-08-02 14:29:24 -0700576 if(target_use_signed_kernel() && auth_kernel_img) {
577 src = auth_kernel;
578 if (have_cmdline) --dst;
579 while ((*dst++ = *src++));
580 }
581
Ajay Dudanid04110c2011-01-17 23:55:07 -0800582 switch(target_baseband())
583 {
584 case BASEBAND_APQ:
585 src = baseband_apq;
586 if (have_cmdline) --dst;
587 while ((*dst++ = *src++));
588 break;
589
590 case BASEBAND_MSM:
591 src = baseband_msm;
592 if (have_cmdline) --dst;
593 while ((*dst++ = *src++));
594 break;
595
596 case BASEBAND_CSFB:
597 src = baseband_csfb;
598 if (have_cmdline) --dst;
599 while ((*dst++ = *src++));
600 break;
601
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800602 case BASEBAND_SVLTE2A:
603 src = baseband_svlte2a;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800604 if (have_cmdline) --dst;
605 while ((*dst++ = *src++));
606 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700607
608 case BASEBAND_MDM:
609 src = baseband_mdm;
610 if (have_cmdline) --dst;
611 while ((*dst++ = *src++));
612 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700613
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800614 case BASEBAND_MDM2:
615 src = baseband_mdm2;
616 if (have_cmdline) --dst;
617 while ((*dst++ = *src++));
618 break;
619
Amol Jadi5c61a952012-05-04 17:05:35 -0700620 case BASEBAND_SGLTE:
621 src = baseband_sglte;
622 if (have_cmdline) --dst;
623 while ((*dst++ = *src++));
624 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530625
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800626 case BASEBAND_SGLTE2:
627 src = baseband_sglte2;
628 if (have_cmdline) --dst;
629 while ((*dst++ = *src++));
630 break;
631
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530632 case BASEBAND_DSDA:
633 src = baseband_dsda;
634 if (have_cmdline) --dst;
635 while ((*dst++ = *src++));
636 break;
Amol Jadi2a15a272013-01-22 12:03:36 -0800637
638 case BASEBAND_DSDA2:
639 src = baseband_dsda2;
640 if (have_cmdline) --dst;
641 while ((*dst++ = *src++));
642 break;
Vijay Kumar Pendotib228cfc2016-06-13 20:15:23 +0530643 case BASEBAND_APQ_NOWGR:
644 src = baseband_apq_nowgr;
645 if (have_cmdline) --dst;
646 while ((*dst++ = *src++));
647 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800648 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700649
650 if (strlen(display_panel_buf)) {
Dhaval Patel223ec952013-07-18 14:49:44 -0700651 src = display_panel_buf;
652 if (have_cmdline) --dst;
653 while ((*dst++ = *src++));
654 }
Joonwoo Park61112782013-10-02 19:50:39 -0700655
656 if (have_target_boot_params) {
657 if (have_cmdline) --dst;
658 src = target_boot_params;
659 while ((*dst++ = *src++));
vijay kumar870515d2015-08-31 16:37:24 +0530660 free(target_boot_params);
Joonwoo Park61112782013-10-02 19:50:39 -0700661 }
Channagoud Kadabi85c7ec32016-01-28 23:09:21 -0800662
663#if TARGET_CMDLINE_SUPPORT
664 if (target_cmdline_buf && target_cmd_line_len)
665 {
666 if (have_cmdline) --dst;
667 src = target_cmdline_buf;
668 while((*dst++ = *src++));
669 free(target_cmdline_buf);
670 }
671#endif
Neeti Desaie245d492012-06-01 12:52:13 -0700672 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700673
674
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700675 if (boot_dev_buf)
676 free(boot_dev_buf);
677
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -0800678 if (cmdline_final)
679 dprintf(INFO, "cmdline: %s\n", cmdline_final);
680 else
681 dprintf(INFO, "cmdline is NULL\n");
Neeti Desaie245d492012-06-01 12:52:13 -0700682 return cmdline_final;
683}
684
685unsigned *atag_core(unsigned *ptr)
686{
687 /* CORE */
688 *ptr++ = 2;
689 *ptr++ = 0x54410001;
690
691 return ptr;
692
693}
694
695unsigned *atag_ramdisk(unsigned *ptr, void *ramdisk,
696 unsigned ramdisk_size)
697{
698 if (ramdisk_size) {
699 *ptr++ = 4;
700 *ptr++ = 0x54420005;
701 *ptr++ = (unsigned)ramdisk;
702 *ptr++ = ramdisk_size;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800703 }
704
Neeti Desaie245d492012-06-01 12:52:13 -0700705 return ptr;
706}
707
708unsigned *atag_ptable(unsigned **ptr_addr)
709{
710 int i;
711 struct ptable *ptable;
712
713 if ((ptable = flash_get_ptable()) && (ptable->count != 0)) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700714 *(*ptr_addr)++ = 2 + (ptable->count * (sizeof(struct atag_ptbl_entry) /
715 sizeof(unsigned)));
Neeti Desaie245d492012-06-01 12:52:13 -0700716 *(*ptr_addr)++ = 0x4d534d70;
717 for (i = 0; i < ptable->count; ++i)
718 ptentry_to_tag(ptr_addr, ptable_get(ptable, i));
719 }
720
721 return (*ptr_addr);
722}
723
724unsigned *atag_cmdline(unsigned *ptr, const char *cmdline)
725{
726 int cmdline_length = 0;
727 int n;
Neeti Desaie245d492012-06-01 12:52:13 -0700728 char *dest;
729
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800730 cmdline_length = strlen((const char*)cmdline);
Neeti Desaie245d492012-06-01 12:52:13 -0700731 n = (cmdline_length + 4) & (~3);
732
733 *ptr++ = (n / 4) + 2;
734 *ptr++ = 0x54410009;
735 dest = (char *) ptr;
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800736 while ((*dest++ = *cmdline++));
Neeti Desaie245d492012-06-01 12:52:13 -0700737 ptr += (n / 4);
738
739 return ptr;
740}
741
742unsigned *atag_end(unsigned *ptr)
743{
Brian Swetland9c4c0752009-01-25 16:23:50 -0800744 /* END */
745 *ptr++ = 0;
746 *ptr++ = 0;
747
Neeti Desaie245d492012-06-01 12:52:13 -0700748 return ptr;
749}
750
751void generate_atags(unsigned *ptr, const char *cmdline,
752 void *ramdisk, unsigned ramdisk_size)
753{
vijay kumar21a37452015-12-29 16:23:21 +0530754 unsigned *orig_ptr = ptr;
Neeti Desaie245d492012-06-01 12:52:13 -0700755 ptr = atag_core(ptr);
756 ptr = atag_ramdisk(ptr, ramdisk, ramdisk_size);
757 ptr = target_atag_mem(ptr);
758
759 /* Skip NAND partition ATAGS for eMMC boot */
760 if (!target_is_emmc_boot()){
761 ptr = atag_ptable(&ptr);
762 }
763
vijay kumar21a37452015-12-29 16:23:21 +0530764 /*
765 * Atags size filled till + cmdline size + 1 unsigned for 4-byte boundary + 4 unsigned
766 * for atag identifier in atag_cmdline and atag_end should be with in MAX_TAGS_SIZE bytes
767 */
768 if (((ptr - orig_ptr) + strlen(cmdline) + 5 * sizeof(unsigned)) < MAX_TAGS_SIZE) {
769 ptr = atag_cmdline(ptr, cmdline);
770 ptr = atag_end(ptr);
771 }
772 else {
773 dprintf(CRITICAL,"Crossing ATAGs Max size allowed\n");
774 ASSERT(0);
775 }
Neeti Desaie245d492012-06-01 12:52:13 -0700776}
777
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700778typedef void entry_func_ptr(unsigned, unsigned, unsigned*);
Neeti Desaie245d492012-06-01 12:52:13 -0700779void boot_linux(void *kernel, unsigned *tags,
780 const char *cmdline, unsigned machtype,
781 void *ramdisk, unsigned ramdisk_size)
782{
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800783 unsigned char *final_cmdline;
Amol Jadib6be5c12012-11-14 13:39:51 -0800784#if DEVICE_TREE
Neeti Desai17379b82012-06-04 18:42:53 -0700785 int ret = 0;
Amol Jadib6be5c12012-11-14 13:39:51 -0800786#endif
787
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700788 void (*entry)(unsigned, unsigned, unsigned*) = (entry_func_ptr*)(PA((addr_t)kernel));
Deepa Dinamani25a9f762012-11-30 15:57:15 -0800789 uint32_t tags_phys = PA((addr_t)tags);
vijay kumar1a50a642015-11-16 12:41:15 +0530790 struct kernel64_hdr *kptr = ((struct kernel64_hdr*)(PA((addr_t)kernel)));
Deepa Dinamani25a9f762012-11-30 15:57:15 -0800791
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530792 ramdisk = (void *)PA((addr_t)ramdisk);
Neeti Desaie245d492012-06-01 12:52:13 -0700793
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800794 final_cmdline = update_cmdline((const char*)cmdline);
795
Neeti Desai17379b82012-06-04 18:42:53 -0700796#if DEVICE_TREE
Amol Jadib6be5c12012-11-14 13:39:51 -0800797 dprintf(INFO, "Updating device tree: start\n");
798
Neeti Desai17379b82012-06-04 18:42:53 -0700799 /* Update the Device Tree */
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530800 ret = update_device_tree((void *)tags,(const char *)final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -0700801 if(ret)
802 {
803 dprintf(CRITICAL, "ERROR: Updating Device Tree Failed \n");
804 ASSERT(0);
805 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800806 dprintf(INFO, "Updating device tree: done\n");
Neeti Desai17379b82012-06-04 18:42:53 -0700807#else
Neeti Desaie245d492012-06-01 12:52:13 -0700808 /* Generating the Atags */
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800809 generate_atags(tags, final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -0700810#endif
Neeti Desaie245d492012-06-01 12:52:13 -0700811
Maria Yu52254c02014-07-04 16:14:54 +0800812 free(final_cmdline);
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -0700813
814#if VERIFIED_BOOT
lijuangbdd9bb42016-03-01 18:22:17 +0800815#if !VBOOT_MOTA
816 if (device.verity_mode == 0) {
817#if FBCON_DISPLAY_MSG
Channagoud Kadabi86b0c112016-03-16 19:23:16 -0700818#if ENABLE_VB_ATTEST
819 display_bootverify_menu(DISPLAY_MENU_EIO);
820#else
lijuangbdd9bb42016-03-01 18:22:17 +0800821 display_bootverify_menu(DISPLAY_MENU_LOGGING);
Channagoud Kadabi86b0c112016-03-16 19:23:16 -0700822#endif
lijuangbdd9bb42016-03-01 18:22:17 +0800823 wait_for_users_action();
824#else
825 dprintf(CRITICAL,
826 "The dm-verity is not started in enforcing mode.\nWait for 5 seconds before proceeding\n");
827 mdelay(5000);
828#endif
829 }
830
831#endif
832#endif
833
834#if VERIFIED_BOOT
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -0700835 /* Write protect the device info */
Channagoud Kadabi3bd9d1e2015-05-05 16:18:20 -0700836 if (!boot_into_recovery && target_build_variant_user() && devinfo_present && mmc_write_protect("devinfo", 1))
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -0700837 {
838 dprintf(INFO, "Failed to write protect dev info\n");
839 ASSERT(0);
840 }
841#endif
842
Aravind Venkateswaran8f076242014-02-25 16:25:30 -0800843 /* Turn off splash screen if enabled */
844#if DISPLAY_SPLASH_SCREEN
845 target_display_shutdown();
846#endif
847
Veera Sundaram Sankaran67ea0932015-09-25 10:09:30 -0700848 /* Perform target specific cleanup */
849 target_uninit();
Aravind Venkateswaran8f076242014-02-25 16:25:30 -0800850
Deepa Dinamani33734bc2013-03-06 12:16:06 -0800851 dprintf(INFO, "booting linux @ %p, ramdisk @ %p (%d), tags/device tree @ %p\n",
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530852 entry, ramdisk, ramdisk_size, (void *)tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800853
854 enter_critical_section();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700855
Amol Jadi4421e652011-06-16 15:00:48 -0700856 /* do any platform specific cleanup before kernel entry */
857 platform_uninit();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700858
Brian Swetland9c4c0752009-01-25 16:23:50 -0800859 arch_disable_cache(UCACHE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700860
Amol Jadi504f9fe2012-08-16 13:56:48 -0700861#if ARM_WITH_MMU
Brian Swetland9c4c0752009-01-25 16:23:50 -0800862 arch_disable_mmu();
Amol Jadi504f9fe2012-08-16 13:56:48 -0700863#endif
Amol Jadi492d5a52013-03-15 16:12:34 -0700864 bs_set_timestamp(BS_KERNEL_ENTRY);
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800865
866 if (IS_ARM64(kptr))
867 /* Jump to a 64bit kernel */
868 scm_elexec_call((paddr_t)kernel, tags_phys);
869 else
870 /* Jump to a 32bit kernel */
871 entry(0, machtype, (unsigned*)tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800872}
873
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700874/* Function to check if the memory address range falls within the aboot
875 * boundaries.
876 * start: Start of the memory region
877 * size: Size of the memory region
878 */
Vijay Kumar Pendoti9c002ad2016-03-09 13:52:45 +0530879int check_aboot_addr_range_overlap(uintptr_t start, uint32_t size)
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700880{
881 /* Check for boundary conditions. */
Sundarajan Srinivasance2a0ea2013-12-16 17:02:56 -0800882 if ((UINT_MAX - start) < size)
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700883 return -1;
884
885 /* Check for memory overlap. */
886 if ((start < MEMBASE) && ((start + size) <= MEMBASE))
887 return 0;
Channagoud Kadabi94143912013-10-15 12:53:52 -0700888 else if (start >= (MEMBASE + MEMSIZE))
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700889 return 0;
890 else
891 return -1;
892}
893
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800894#define ROUND_TO_PAGE(x,y) (((x) + (y)) & (~(y)))
Brian Swetland9c4c0752009-01-25 16:23:50 -0800895
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800896BUF_DMA_ALIGN(buf, BOOT_IMG_MAX_PAGE_SIZE); //Equal to max-supported pagesize
Amol Jadib6be5c12012-11-14 13:39:51 -0800897#if DEVICE_TREE
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800898BUF_DMA_ALIGN(dt_buf, BOOT_IMG_MAX_PAGE_SIZE);
Amol Jadib6be5c12012-11-14 13:39:51 -0800899#endif
Dima Zavin214cc642009-01-26 11:16:21 -0800900
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700901static void verify_signed_bootimg(uint32_t bootimg_addr, uint32_t bootimg_size)
902{
903 int ret;
Channagoud Kadabia8c623f2015-01-13 14:48:48 -0800904
905#if !VERIFIED_BOOT
Sundarajan Srinivasance54d6e2013-11-11 12:45:00 -0800906#if IMAGE_VERIF_ALGO_SHA1
907 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA1;
908#else
909 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA256;
910#endif
Channagoud Kadabia8c623f2015-01-13 14:48:48 -0800911#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700912
913 /* Assume device is rooted at this time. */
914 device.is_tampered = 1;
915
916 dprintf(INFO, "Authenticating boot image (%d): start\n", bootimg_size);
917
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700918#if VERIFIED_BOOT
919 if(boot_into_recovery)
920 {
921 ret = boot_verify_image((unsigned char *)bootimg_addr,
Unnati Gandhi47051252015-03-02 15:21:09 +0530922 bootimg_size, "/recovery");
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700923 }
924 else
925 {
926 ret = boot_verify_image((unsigned char *)bootimg_addr,
Unnati Gandhi47051252015-03-02 15:21:09 +0530927 bootimg_size, "/boot");
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700928 }
929 boot_verify_print_state();
930#else
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700931 ret = image_verify((unsigned char *)bootimg_addr,
932 (unsigned char *)(bootimg_addr + bootimg_size),
933 bootimg_size,
Sundarajan Srinivasance54d6e2013-11-11 12:45:00 -0800934 auth_algo);
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700935#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700936 dprintf(INFO, "Authenticating boot image: done return value = %d\n", ret);
937
938 if (ret)
939 {
940 /* Authorized kernel */
941 device.is_tampered = 0;
Sundarajan Srinivasan3fb21f12013-09-16 18:36:15 -0700942 auth_kernel_img = 1;
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700943 }
944
Amit Blay4aa292f2015-04-28 21:55:59 +0300945#ifdef MDTP_SUPPORT
946 {
947 /* Verify MDTP lock.
948 * For boot & recovery partitions, use aboot's verification result.
949 */
950 mdtp_ext_partition_verification_t ext_partition;
951 ext_partition.partition = boot_into_recovery ? MDTP_PARTITION_RECOVERY : MDTP_PARTITION_BOOT;
952 ext_partition.integrity_state = device.is_tampered ? MDTP_PARTITION_STATE_INVALID : MDTP_PARTITION_STATE_VALID;
953 ext_partition.page_size = 0; /* Not needed since already validated */
954 ext_partition.image_addr = 0; /* Not needed since already validated */
955 ext_partition.image_size = 0; /* Not needed since already validated */
956 ext_partition.sig_avail = FALSE; /* Not needed since already validated */
957 mdtp_fwlock_verify_lock(&ext_partition);
958 }
959#endif /* MDTP_SUPPORT */
960
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700961#if USE_PCOM_SECBOOT
962 set_tamper_flag(device.is_tampered);
963#endif
964
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700965#if VERIFIED_BOOT
Sridhar Parasuram8b792422015-07-05 11:38:13 -0700966 switch(boot_verify_get_state())
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700967 {
Sridhar Parasuram8b792422015-07-05 11:38:13 -0700968 case RED:
lijuanga40d6302015-07-20 20:10:13 +0800969#if FBCON_DISPLAY_MSG
lijuang9a7d3b92015-11-30 14:41:24 +0800970 display_bootverify_menu(DISPLAY_MENU_RED);
Channagoud Kadabi86b0c112016-03-16 19:23:16 -0700971#if ENABLE_VB_ATTEST
972 mdelay(DELAY_WAIT);
973 shutdown_device();
974#else
lijuanga40d6302015-07-20 20:10:13 +0800975 wait_for_users_action();
Channagoud Kadabi86b0c112016-03-16 19:23:16 -0700976#endif
lijuanga40d6302015-07-20 20:10:13 +0800977#else
978 dprintf(CRITICAL,
979 "Your device has failed verification and may not work properly.\nWait for 5 seconds before proceeding\n");
980 mdelay(5000);
981#endif
982
983 break;
Sridhar Parasuram8b792422015-07-05 11:38:13 -0700984 case YELLOW:
lijuanga40d6302015-07-20 20:10:13 +0800985#if FBCON_DISPLAY_MSG
lijuang9a7d3b92015-11-30 14:41:24 +0800986 display_bootverify_menu(DISPLAY_MENU_YELLOW);
lijuanga40d6302015-07-20 20:10:13 +0800987 wait_for_users_action();
988#else
989 dprintf(CRITICAL,
990 "Your device has loaded a different operating system.\nWait for 5 seconds before proceeding\n");
991 mdelay(5000);
992#endif
993 break;
Sridhar Parasuram8b792422015-07-05 11:38:13 -0700994 default:
lijuanga40d6302015-07-20 20:10:13 +0800995 break;
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700996 }
997#endif
Sridhar Parasuram8b792422015-07-05 11:38:13 -0700998#if !VERIFIED_BOOT
Unnati Gandhi1be04752015-03-27 19:41:53 +0530999 if(device.is_tampered)
1000 {
1001 write_device_info_mmc(&device);
1002 #ifdef TZ_TAMPER_FUSE
1003 set_tamper_fuse_cmd();
1004 #endif
1005 #ifdef ASSERT_ON_TAMPER
1006 dprintf(CRITICAL, "Device is tampered. Asserting..\n");
1007 ASSERT(0);
1008 #endif
1009 }
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001010#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001011}
1012
Maunik Shah0f3c8ac2014-03-02 17:47:58 +05301013static bool check_format_bit()
1014{
1015 bool ret = false;
1016 int index;
1017 uint64_t offset;
1018 struct boot_selection_info *in = NULL;
1019 char *buf = NULL;
1020
1021 index = partition_get_index("bootselect");
1022 if (index == INVALID_PTN)
1023 {
1024 dprintf(INFO, "Unable to locate /bootselect partition\n");
1025 return ret;
1026 }
1027 offset = partition_get_offset(index);
1028 if(!offset)
1029 {
1030 dprintf(INFO, "partition /bootselect doesn't exist\n");
1031 return ret;
1032 }
1033 buf = (char *) memalign(CACHE_LINE, ROUNDUP(page_size, CACHE_LINE));
1034 ASSERT(buf);
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301035 if (mmc_read(offset, (uint32_t *)buf, page_size))
Maunik Shah0f3c8ac2014-03-02 17:47:58 +05301036 {
1037 dprintf(INFO, "mmc read failure /bootselect %d\n", page_size);
1038 free(buf);
1039 return ret;
1040 }
1041 in = (struct boot_selection_info *) buf;
1042 if ((in->signature == BOOTSELECT_SIGNATURE) &&
1043 (in->version == BOOTSELECT_VERSION)) {
1044 if ((in->state_info & BOOTSELECT_FORMAT) &&
1045 !(in->state_info & BOOTSELECT_FACTORY))
1046 ret = true;
1047 } else {
1048 dprintf(CRITICAL, "Signature: 0x%08x or version: 0x%08x mismatched of /bootselect\n",
1049 in->signature, in->version);
1050 ASSERT(0);
1051 }
1052 free(buf);
1053 return ret;
1054}
1055
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001056void boot_verifier_init()
1057{
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001058 uint32_t boot_state;
1059 /* Check if device unlock */
1060 if(device.is_unlocked)
1061 {
1062 boot_verify_send_event(DEV_UNLOCK);
1063 boot_verify_print_state();
1064 dprintf(CRITICAL, "Device is unlocked! Skipping verification...\n");
1065 return;
1066 }
1067 else
1068 {
1069 boot_verify_send_event(BOOT_INIT);
1070 }
1071
1072 /* Initialize keystore */
1073 boot_state = boot_verify_keystore_init();
1074 if(boot_state == YELLOW)
1075 {
1076 boot_verify_print_state();
1077 dprintf(CRITICAL, "Keystore verification failed! Continuing anyways...\n");
1078 }
1079}
1080
Shashank Mittal23b8f422010-04-16 19:27:21 -07001081int boot_linux_from_mmc(void)
1082{
1083 struct boot_img_hdr *hdr = (void*) buf;
1084 struct boot_img_hdr *uhdr;
1085 unsigned offset = 0;
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07001086 int rcode;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001087 unsigned long long ptn = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001088 int index = INVALID_PTN;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001089
Shashank Mittalcd98d472011-08-02 14:29:24 -07001090 unsigned char *image_addr = 0;
1091 unsigned kernel_actual;
1092 unsigned ramdisk_actual;
1093 unsigned imagesize_actual;
Neeti Desai465491e2012-07-31 12:53:35 -07001094 unsigned second_actual = 0;
Neeti Desai465491e2012-07-31 12:53:35 -07001095
Matthew Qin271927e2015-03-31 22:07:07 -04001096 unsigned int dtb_size = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08001097 unsigned int out_len = 0;
1098 unsigned int out_avai_len = 0;
1099 unsigned char *out_addr = NULL;
1100 uint32_t dtb_offset = 0;
1101 unsigned char *kernel_start_addr = NULL;
1102 unsigned int kernel_size = 0;
Ameya Thakur10a33452016-06-13 14:24:26 -07001103 unsigned int patched_kernel_hdr_size = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08001104 int rc;
Neeti Desai465491e2012-07-31 12:53:35 -07001105#if DEVICE_TREE
1106 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -07001107 struct dt_entry dt_entry;
Neeti Desai465491e2012-07-31 12:53:35 -07001108 unsigned dt_table_offset;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001109 uint32_t dt_actual;
Deepa Dinamani19648b42013-09-05 17:05:55 -07001110 uint32_t dt_hdr_size;
Matthew Qin271927e2015-03-31 22:07:07 -04001111 unsigned char *best_match_dt_addr = NULL;
Neeti Desai465491e2012-07-31 12:53:35 -07001112#endif
Matthew Qin49e51fa2015-02-09 10:40:45 +08001113 struct kernel64_hdr *kptr = NULL;
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001114
Maunik Shah0f3c8ac2014-03-02 17:47:58 +05301115 if (check_format_bit())
1116 boot_into_recovery = 1;
1117
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07001118 if (!boot_into_recovery) {
1119 memset(ffbm_mode_string, '\0', sizeof(ffbm_mode_string));
1120 rcode = get_ffbm(ffbm_mode_string, sizeof(ffbm_mode_string));
1121 if (rcode <= 0) {
1122 boot_into_ffbm = false;
1123 if (rcode < 0)
1124 dprintf(CRITICAL,"failed to get ffbm cookie");
1125 } else
1126 boot_into_ffbm = true;
1127 } else
1128 boot_into_ffbm = false;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001129 uhdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
1130 if (!memcmp(uhdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1131 dprintf(INFO, "Unified boot method!\n");
1132 hdr = uhdr;
1133 goto unified_boot;
1134 }
Greg Griscod6250552011-06-29 14:40:23 -07001135 if (!boot_into_recovery) {
Kinson Chikf1a43512011-07-14 11:28:39 -07001136 index = partition_get_index("boot");
1137 ptn = partition_get_offset(index);
1138 if(ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -07001139 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1140 return -1;
1141 }
Kinson Chikf1a43512011-07-14 11:28:39 -07001142 }
1143 else {
1144 index = partition_get_index("recovery");
1145 ptn = partition_get_offset(index);
1146 if(ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -07001147 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
1148 return -1;
1149 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001150 }
Channagoud Kadabief0547c2015-02-10 12:57:38 -08001151 /* Set Lun for boot & recovery partitions */
1152 mmc_set_lun(partition_get_lun(index));
Shashank Mittal23b8f422010-04-16 19:27:21 -07001153
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301154 if (mmc_read(ptn + offset, (uint32_t *) buf, page_size)) {
Shashank Mittal23b8f422010-04-16 19:27:21 -07001155 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
1156 return -1;
1157 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001158
1159 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001160 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Shashank Mittal23b8f422010-04-16 19:27:21 -07001161 return -1;
1162 }
1163
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07001164 if (hdr->page_size && (hdr->page_size != page_size)) {
vijay kumar2e21b3a2014-06-26 17:40:15 +05301165
1166 if (hdr->page_size > BOOT_IMG_MAX_PAGE_SIZE) {
1167 dprintf(CRITICAL, "ERROR: Invalid page size\n");
1168 return -1;
1169 }
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07001170 page_size = hdr->page_size;
1171 page_mask = page_size - 1;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001172 }
1173
vijay kumar287bb542015-09-29 13:01:52 +05301174 /* ensure commandline is terminated */
1175 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
1176
Matthew Qin49e51fa2015-02-09 10:40:45 +08001177 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1178 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001179
Matthew Qin49e51fa2015-02-09 10:40:45 +08001180 image_addr = (unsigned char *)target_get_scratch_address();
1181
1182#if DEVICE_TREE
1183 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
1184 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + dt_actual);
1185#else
1186 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
1187#endif
1188
1189#if VERIFIED_BOOT
1190 boot_verifier_init();
1191#endif
1192
Vijay Kumar Pendoti9c002ad2016-03-09 13:52:45 +05301193 if (check_aboot_addr_range_overlap((uintptr_t) image_addr, imagesize_actual))
Matthew Qin49e51fa2015-02-09 10:40:45 +08001194 {
1195 dprintf(CRITICAL, "Boot image buffer address overlaps with aboot addresses.\n");
1196 return -1;
1197 }
1198
Matthew Qinbb7923d2015-02-09 10:56:09 +08001199 /*
1200 * Update loading flow of bootimage to support compressed/uncompressed
1201 * bootimage on both 64bit and 32bit platform.
1202 * 1. Load bootimage from emmc partition onto DDR.
1203 * 2. Check if bootimage is gzip format. If yes, decompress compressed kernel
1204 * 3. Check kernel header and update kernel load addr for 64bit and 32bit
1205 * platform accordingly.
1206 * 4. Sanity Check on kernel_addr and ramdisk_addr and copy data.
1207 */
1208
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001209 dprintf(INFO, "Loading (%s) image (%d): start\n",
1210 (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001211 bs_set_timestamp(BS_KERNEL_LOAD_START);
1212
Gaurav Nebhwani43e2a462016-03-17 21:32:56 +05301213 if ((target_get_max_flash_size() - page_size) < imagesize_actual)
1214 {
1215 dprintf(CRITICAL, "booimage size is greater than DDR can hold\n");
1216 return -1;
1217 }
1218
Matthew Qinbb7923d2015-02-09 10:56:09 +08001219 /* Read image without signature */
Matthew Qin49e51fa2015-02-09 10:40:45 +08001220 if (mmc_read(ptn + offset, (void *)image_addr, imagesize_actual))
1221 {
1222 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
1223 return -1;
1224 }
1225
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001226 dprintf(INFO, "Loading (%s) image (%d): done\n",
1227 (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual);
1228
Matthew Qin49e51fa2015-02-09 10:40:45 +08001229 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
1230
1231 /* Authenticate Kernel */
1232 dprintf(INFO, "use_signed_kernel=%d, is_unlocked=%d, is_tampered=%d.\n",
1233 (int) target_use_signed_kernel(),
1234 device.is_unlocked,
1235 device.is_tampered);
1236
Channagoud Kadabi736c4962015-08-21 11:56:52 -07001237 /* Change the condition a little bit to include the test framework support.
1238 * We would never reach this point if device is in fastboot mode, even if we did
1239 * that means we are in test mode, so execute kernel authentication part for the
1240 * tests */
Parth Dixitcb0c6082015-12-30 15:01:13 +05301241 if((target_use_signed_kernel() && (!device.is_unlocked)) || is_test_mode_enabled())
Matthew Qin49e51fa2015-02-09 10:40:45 +08001242 {
1243 offset = imagesize_actual;
Vijay Kumar Pendoti9c002ad2016-03-09 13:52:45 +05301244 if (check_aboot_addr_range_overlap((uintptr_t)image_addr + offset, page_size))
Matthew Qin49e51fa2015-02-09 10:40:45 +08001245 {
1246 dprintf(CRITICAL, "Signature read buffer address overlaps with aboot addresses.\n");
1247 return -1;
1248 }
1249
1250 /* Read signature */
1251 if(mmc_read(ptn + offset, (void *)(image_addr + offset), page_size))
1252 {
1253 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
1254 return -1;
1255 }
1256
1257 verify_signed_bootimg((uint32_t)image_addr, imagesize_actual);
Channagoud Kadabi736c4962015-08-21 11:56:52 -07001258 /* The purpose of our test is done here */
Parth Dixitcb0c6082015-12-30 15:01:13 +05301259 if(is_test_mode_enabled() && auth_kernel_img)
Channagoud Kadabi736c4962015-08-21 11:56:52 -07001260 return 0;
Matthew Qin49e51fa2015-02-09 10:40:45 +08001261 } else {
1262 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
1263 #ifdef TZ_SAVE_KERNEL_HASH
1264 aboot_save_boot_hash_mmc((uint32_t) image_addr, imagesize_actual);
1265 #endif /* TZ_SAVE_KERNEL_HASH */
Amit Blay4aa292f2015-04-28 21:55:59 +03001266
1267#ifdef MDTP_SUPPORT
1268 {
1269 /* Verify MDTP lock.
1270 * For boot & recovery partitions, MDTP will use boot_verifier APIs,
1271 * since verification was skipped in aboot. The signature is not part of the loaded image.
1272 */
1273 mdtp_ext_partition_verification_t ext_partition;
1274 ext_partition.partition = boot_into_recovery ? MDTP_PARTITION_RECOVERY : MDTP_PARTITION_BOOT;
1275 ext_partition.integrity_state = MDTP_PARTITION_STATE_UNSET;
1276 ext_partition.page_size = page_size;
1277 ext_partition.image_addr = (uint32)image_addr;
1278 ext_partition.image_size = imagesize_actual;
1279 ext_partition.sig_avail = FALSE;
1280 mdtp_fwlock_verify_lock(&ext_partition);
1281 }
1282#endif /* MDTP_SUPPORT */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001283 }
1284
Sridhar Parasuram00bfedb2015-05-26 14:21:27 -07001285#if VERIFIED_BOOT
Reut Zysmanba8a9d52016-02-18 11:44:04 +02001286 if(boot_verify_get_state() == ORANGE)
1287 {
1288#if FBCON_DISPLAY_MSG
1289 display_bootverify_menu(DISPLAY_MENU_ORANGE);
1290 wait_for_users_action();
1291#else
1292 dprintf(CRITICAL,
1293 "Your device has been unlocked and can't be trusted.\nWait for 5 seconds before proceeding\n");
1294 mdelay(5000);
1295#endif
1296 }
1297#endif
1298
1299#if VERIFIED_BOOT
Parth Dixitbc9b6492015-10-18 00:41:38 +05301300#if !VBOOT_MOTA
Sridhar Parasuram00bfedb2015-05-26 14:21:27 -07001301 // send root of trust
Sridhar Parasuram96300dc2015-06-11 10:37:11 -07001302 if(!send_rot_command((uint32_t)device.is_unlocked))
Sridhar Parasuram00bfedb2015-05-26 14:21:27 -07001303 ASSERT(0);
1304#endif
Parth Dixitbc9b6492015-10-18 00:41:38 +05301305#endif
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001306 /*
Matthew Qinbb7923d2015-02-09 10:56:09 +08001307 * Check if the kernel image is a gzip package. If yes, need to decompress it.
1308 * If not, continue booting.
1309 */
1310 if (is_gzip_package((unsigned char *)(image_addr + page_size), hdr->kernel_size))
1311 {
1312 out_addr = (unsigned char *)(image_addr + imagesize_actual + page_size);
1313 out_avai_len = target_get_max_flash_size() - imagesize_actual - page_size;
Matthew Qin0b15b322015-05-19 05:20:54 -04001314 dprintf(INFO, "decompressing kernel image: start\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001315 rc = decompress((unsigned char *)(image_addr + page_size),
1316 hdr->kernel_size, out_addr, out_avai_len,
1317 &dtb_offset, &out_len);
1318 if (rc)
1319 {
Matthew Qin0b15b322015-05-19 05:20:54 -04001320 dprintf(CRITICAL, "decompressing kernel image failed!!!\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001321 ASSERT(0);
1322 }
1323
Matthew Qin0b15b322015-05-19 05:20:54 -04001324 dprintf(INFO, "decompressing kernel image: done\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001325 kptr = (struct kernel64_hdr *)out_addr;
1326 kernel_start_addr = out_addr;
1327 kernel_size = out_len;
1328 } else {
Ameya Thakur10a33452016-06-13 14:24:26 -07001329 dprintf(INFO, "Uncpmpressed kernel in use\n");
1330 if (!strncmp((char*)(image_addr + page_size),
1331 PATCHED_KERNEL_MAGIC,
1332 sizeof(PATCHED_KERNEL_MAGIC) - 1)) {
1333 dprintf(INFO, "Patched kernel detected\n");
1334 kptr = (struct kernel64_hdr *)(image_addr + page_size +
1335 PATCHED_KERNEL_HEADER_SIZE);
1336 //The size of the kernel is stored at start of kernel image + 16
1337 //The dtb would start just after the kernel
1338 dtb_offset = *((uint32_t*)((unsigned char*)
1339 (image_addr + page_size +
1340 sizeof(PATCHED_KERNEL_MAGIC) -
1341 1)));
1342 //The actual kernel starts after the 20 byte header.
1343 kernel_start_addr = (unsigned char*)(image_addr +
1344 page_size + PATCHED_KERNEL_HEADER_SIZE);
1345 kernel_size = hdr->kernel_size;
1346 patched_kernel_hdr_size = PATCHED_KERNEL_HEADER_SIZE;
1347 } else {
1348 dprintf(INFO, "Kernel image not patched..Unable to locate dt offset\n");
1349 kptr = (struct kernel64_hdr *)(image_addr + page_size);
1350 kernel_start_addr = (unsigned char *)(image_addr + page_size);
1351 kernel_size = hdr->kernel_size;
1352 }
Matthew Qinbb7923d2015-02-09 10:56:09 +08001353 }
1354
1355 /*
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001356 * Update the kernel/ramdisk/tags address if the boot image header
1357 * has default values, these default values come from mkbootimg when
1358 * the boot image is flashed using fastboot flash:raw
1359 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001360 update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr));
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001361
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001362 /* Get virtual addresses since the hdr saves physical addresses. */
1363 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
1364 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
1365 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001366
Matthew Qinbb7923d2015-02-09 10:56:09 +08001367 kernel_size = ROUND_TO_PAGE(kernel_size, page_mask);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001368 /* Check if the addresses in the header are valid. */
Matthew Qinbb7923d2015-02-09 10:56:09 +08001369 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_size) ||
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001370 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
1371 {
1372 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
1373 return -1;
1374 }
1375
1376#ifndef DEVICE_TREE
1377 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1378 {
1379 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
1380 return -1;
1381 }
1382#endif
1383
Matthew Qin49e51fa2015-02-09 10:40:45 +08001384 /* Move kernel, ramdisk and device tree to correct address */
Matthew Qinbb7923d2015-02-09 10:56:09 +08001385 memmove((void*) hdr->kernel_addr, kernel_start_addr, kernel_size);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001386 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001387
Matthew Qin49e51fa2015-02-09 10:40:45 +08001388 #if DEVICE_TREE
1389 if(hdr->dt_size) {
1390 dt_table_offset = ((uint32_t)image_addr + page_size + kernel_actual + ramdisk_actual + second_actual);
1391 table = (struct dt_table*) dt_table_offset;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001392
Matthew Qin49e51fa2015-02-09 10:40:45 +08001393 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
1394 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1395 return -1;
1396 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001397
P.V. Phani Kumar82916762016-03-09 09:20:19 +05301398 /* Its Error if, dt_hdr_size (table->num_entries * dt_entry size + Dev_Tree Header)
1399 goes beyound hdr->dt_size*/
1400 if (dt_hdr_size > ROUND_TO_PAGE(hdr->dt_size,hdr->page_size)) {
1401 dprintf(CRITICAL, "ERROR: Invalid Device Tree size \n");
1402 return -1;
1403 }
1404
Matthew Qin49e51fa2015-02-09 10:40:45 +08001405 /* Find index of device tree within device tree table */
1406 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
1407 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1408 return -1;
1409 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001410
P.V. Phani Kumar3c333202016-03-09 11:54:37 +05301411 if(dt_entry.offset > (UINT_MAX - dt_entry.size)) {
1412 dprintf(CRITICAL, "ERROR: Device tree contents are Invalid\n");
1413 return -1;
1414 }
1415
1416 /* Ensure we are not overshooting dt_size with the dt_entry selected */
1417 if ((dt_entry.offset + dt_entry.size) > hdr->dt_size) {
1418 dprintf(CRITICAL, "ERROR: Device tree contents are Invalid\n");
1419 return -1;
1420 }
1421
Matthew Qin271927e2015-03-31 22:07:07 -04001422 if (is_gzip_package((unsigned char *)dt_table_offset + dt_entry.offset, dt_entry.size))
1423 {
1424 unsigned int compressed_size = 0;
1425 out_addr += out_len;
1426 out_avai_len -= out_len;
Matthew Qin0b15b322015-05-19 05:20:54 -04001427 dprintf(INFO, "decompressing dtb: start\n");
Matthew Qin271927e2015-03-31 22:07:07 -04001428 rc = decompress((unsigned char *)dt_table_offset + dt_entry.offset,
1429 dt_entry.size, out_addr, out_avai_len,
1430 &compressed_size, &dtb_size);
1431 if (rc)
1432 {
Matthew Qin0b15b322015-05-19 05:20:54 -04001433 dprintf(CRITICAL, "decompressing dtb failed!!!\n");
Matthew Qin271927e2015-03-31 22:07:07 -04001434 ASSERT(0);
1435 }
1436
Matthew Qin0b15b322015-05-19 05:20:54 -04001437 dprintf(INFO, "decompressing dtb: done\n");
Matthew Qin271927e2015-03-31 22:07:07 -04001438 best_match_dt_addr = out_addr;
1439 } else {
1440 best_match_dt_addr = (unsigned char *)dt_table_offset + dt_entry.offset;
1441 dtb_size = dt_entry.size;
1442 }
1443
Matthew Qin49e51fa2015-02-09 10:40:45 +08001444 /* Validate and Read device device tree in the tags_addr */
Matthew Qin271927e2015-03-31 22:07:07 -04001445 if (check_aboot_addr_range_overlap(hdr->tags_addr, dtb_size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001446 {
1447 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1448 return -1;
1449 }
Shashank Mittal162244e2011-08-08 19:01:25 -07001450
Matthew Qin271927e2015-03-31 22:07:07 -04001451 memmove((void *)hdr->tags_addr, (char *)best_match_dt_addr, dtb_size);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001452 } else {
1453 /* Validate the tags_addr */
1454 if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual))
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001455 {
1456 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1457 return -1;
1458 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08001459 /*
1460 * If appended dev tree is found, update the atags with
1461 * memory address to the DTB appended location on RAM.
1462 * Else update with the atags address in the kernel header
1463 */
1464 void *dtb;
Ameya Thakur10a33452016-06-13 14:24:26 -07001465 dtb = dev_tree_appended(
1466 (void*)(image_addr + page_size +
1467 patched_kernel_hdr_size),
1468 hdr->kernel_size, dtb_offset,
1469 (void *)hdr->tags_addr);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001470 if (!dtb) {
1471 dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001472 return -1;
1473 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07001474 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08001475 #endif
Shashank Mittal23b8f422010-04-16 19:27:21 -07001476
Stanimir Varbanov69ec5462013-07-18 18:17:42 +03001477 if (boot_into_recovery && !device.is_unlocked && !device.is_tampered)
1478 target_load_ssd_keystore();
1479
Shashank Mittal23b8f422010-04-16 19:27:21 -07001480unified_boot:
Shashank Mittal23b8f422010-04-16 19:27:21 -07001481
Dima Zavin77e41f32013-03-06 16:10:43 -08001482 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07001483 (const char *)hdr->cmdline, board_machtype(),
Shashank Mittal23b8f422010-04-16 19:27:21 -07001484 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
1485
1486 return 0;
1487}
1488
Dima Zavin214cc642009-01-26 11:16:21 -08001489int boot_linux_from_flash(void)
1490{
1491 struct boot_img_hdr *hdr = (void*) buf;
Dima Zavin214cc642009-01-26 11:16:21 -08001492 struct ptentry *ptn;
1493 struct ptable *ptable;
1494 unsigned offset = 0;
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001495
Shashank Mittalcd98d472011-08-02 14:29:24 -07001496 unsigned char *image_addr = 0;
1497 unsigned kernel_actual;
1498 unsigned ramdisk_actual;
1499 unsigned imagesize_actual;
vijay kumar8f53e362015-11-24 13:38:11 +05301500 unsigned second_actual = 0;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001501
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001502#if DEVICE_TREE
1503 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -07001504 struct dt_entry dt_entry;
vijay kumar8f53e362015-11-24 13:38:11 +05301505 unsigned dt_table_offset;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001506 uint32_t dt_actual;
Deepa Dinamani19648b42013-09-05 17:05:55 -07001507 uint32_t dt_hdr_size;
vijay kumar8f53e362015-11-24 13:38:11 +05301508 unsigned int dtb_size = 0;
1509 unsigned char *best_match_dt_addr = NULL;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001510#endif
1511
David Ng183a7422009-12-07 14:55:21 -08001512 if (target_is_emmc_boot()) {
1513 hdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
1514 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1515 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
1516 return -1;
1517 }
1518 goto continue_boot;
1519 }
1520
Dima Zavin214cc642009-01-26 11:16:21 -08001521 ptable = flash_get_ptable();
1522 if (ptable == NULL) {
1523 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1524 return -1;
1525 }
1526
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001527 if(!boot_into_recovery)
1528 {
1529 ptn = ptable_find(ptable, "boot");
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001530
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001531 if (ptn == NULL) {
1532 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1533 return -1;
1534 }
1535 }
1536 else
1537 {
1538 ptn = ptable_find(ptable, "recovery");
1539 if (ptn == NULL) {
1540 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
1541 return -1;
1542 }
Dima Zavin214cc642009-01-26 11:16:21 -08001543 }
1544
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001545 if (flash_read(ptn, offset, buf, page_size)) {
Dima Zavin214cc642009-01-26 11:16:21 -08001546 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
1547 return -1;
1548 }
Dima Zavin214cc642009-01-26 11:16:21 -08001549
1550 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001551 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Dima Zavin214cc642009-01-26 11:16:21 -08001552 return -1;
1553 }
1554
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001555 if (hdr->page_size != page_size) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001556 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 -08001557 return -1;
1558 }
1559
vijay kumar287bb542015-09-29 13:01:52 +05301560 /* ensure commandline is terminated */
1561 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
1562
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001563 /*
1564 * Update the kernel/ramdisk/tags address if the boot image header
1565 * has default values, these default values come from mkbootimg when
1566 * the boot image is flashed using fastboot flash:raw
1567 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001568 update_ker_tags_rdisk_addr(hdr, false);
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001569
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001570 /* Get virtual addresses since the hdr saves physical addresses. */
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001571 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
1572 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
1573 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
1574
1575 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1576 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1577
1578 /* Check if the addresses in the header are valid. */
1579 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
1580 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
1581 {
1582 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
1583 return -1;
1584 }
1585
1586#ifndef DEVICE_TREE
1587 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1588 {
1589 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
1590 return -1;
1591 }
1592#endif
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001593
Shashank Mittalcd98d472011-08-02 14:29:24 -07001594 /* Authenticate Kernel */
Deepa Dinamani23b60d42013-06-24 18:10:52 -07001595 if(target_use_signed_kernel() && (!device.is_unlocked))
Shashank Mittalcd98d472011-08-02 14:29:24 -07001596 {
1597 image_addr = (unsigned char *)target_get_scratch_address();
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001598 offset = 0;
1599
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001600#if DEVICE_TREE
1601 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
vijay kumar966a9822015-12-09 18:36:09 +05301602
1603 if (UINT_MAX < ((uint64_t)kernel_actual + (uint64_t)ramdisk_actual+ (uint64_t)dt_actual + page_size)) {
1604 dprintf(CRITICAL, "Integer overflow detected in bootimage header fields\n");
1605 return -1;
1606 }
1607
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001608 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + dt_actual);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001609
1610 if (check_aboot_addr_range_overlap(hdr->tags_addr, hdr->dt_size))
1611 {
1612 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1613 return -1;
1614 }
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001615#else
vijay kumar966a9822015-12-09 18:36:09 +05301616 if (UINT_MAX < ((uint64_t)kernel_actual + (uint64_t)ramdisk_actual+ page_size)) {
1617 dprintf(CRITICAL, "Integer overflow detected in bootimage header fields\n");
1618 return -1;
1619 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07001620 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001621#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001622
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001623 dprintf(INFO, "Loading (%s) image (%d): start\n",
1624 (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001625 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -08001626
Shashank Mittalcd98d472011-08-02 14:29:24 -07001627 /* Read image without signature */
1628 if (flash_read(ptn, offset, (void *)image_addr, imagesize_actual))
1629 {
1630 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
1631 return -1;
1632 }
Dima Zavin214cc642009-01-26 11:16:21 -08001633
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001634 dprintf(INFO, "Loading (%s) image (%d): done\n",
1635 (!boot_into_recovery ? "boot" : "recovery"), imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001636 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Amol Jadib6be5c12012-11-14 13:39:51 -08001637
Shashank Mittalcd98d472011-08-02 14:29:24 -07001638 offset = imagesize_actual;
1639 /* Read signature */
1640 if (flash_read(ptn, offset, (void *)(image_addr + offset), page_size))
1641 {
1642 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001643 return -1;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001644 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07001645
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301646 verify_signed_bootimg((uint32_t)image_addr, imagesize_actual);
Shashank Mittalcd98d472011-08-02 14:29:24 -07001647
1648 /* Move kernel and ramdisk to correct address */
Matthew Qinbb7923d2015-02-09 10:56:09 +08001649 memmove((void*) hdr->kernel_addr, (char*) (image_addr + page_size), hdr->kernel_size);
1650 memmove((void*) hdr->ramdisk_addr, (char*) (image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001651#if DEVICE_TREE
vijay kumar8f53e362015-11-24 13:38:11 +05301652 if(hdr->dt_size != 0) {
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001653
vijay kumar8f53e362015-11-24 13:38:11 +05301654 dt_table_offset = ((uint32_t)image_addr + page_size + kernel_actual + ramdisk_actual + second_actual);
1655
1656 table = (struct dt_table*) dt_table_offset;
1657
1658 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0){
1659 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1660 return -1;
1661 }
1662
1663 /* Find index of device tree within device tree table */
1664 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
1665 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1666 return -1;
1667 }
1668
1669 /* Validate and Read device device tree in the "tags_add */
1670 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size)){
1671 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1672 return -1;
1673 }
1674
1675 best_match_dt_addr = (unsigned char *)table + dt_entry.offset;
1676 dtb_size = dt_entry.size;
1677 memmove((void *)hdr->tags_addr, (char *)best_match_dt_addr, dtb_size);
1678 }
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001679#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001680
1681 /* Make sure everything from scratch address is read before next step!*/
Shashank Mittala0032282011-08-26 14:50:11 -07001682 if(device.is_tampered)
Shashank Mittal162244e2011-08-08 19:01:25 -07001683 {
1684 write_device_info_flash(&device);
1685 }
Channagoud Kadabi5c86fe32012-02-16 10:58:48 +05301686#if USE_PCOM_SECBOOT
1687 set_tamper_flag(device.is_tampered);
1688#endif
Shashank Mittalcd98d472011-08-02 14:29:24 -07001689 }
1690 else
1691 {
Shashank Mittal162244e2011-08-08 19:01:25 -07001692 offset = page_size;
1693
Amol Jadib6be5c12012-11-14 13:39:51 -08001694 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1695 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1696 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
1697
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001698 dprintf(INFO, "Loading (%s) image (%d): start\n",
1699 (!boot_into_recovery ? "boot" : "recovery"), kernel_actual + ramdisk_actual);
1700
Amol Jadi492d5a52013-03-15 16:12:34 -07001701 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -08001702
1703 if (flash_read(ptn, offset, (void *)hdr->kernel_addr, kernel_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -07001704 dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
1705 return -1;
1706 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001707 offset += kernel_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001708
Amol Jadib6be5c12012-11-14 13:39:51 -08001709 if (flash_read(ptn, offset, (void *)hdr->ramdisk_addr, ramdisk_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -07001710 dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
1711 return -1;
1712 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001713 offset += ramdisk_actual;
1714
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001715 dprintf(INFO, "Loading (%s) image (%d): done\n",
1716 (!boot_into_recovery ? "boot" : "recovery"), kernel_actual + ramdisk_actual);
1717
Amol Jadi492d5a52013-03-15 16:12:34 -07001718 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001719
1720 if(hdr->second_size != 0) {
Amol Jadib6be5c12012-11-14 13:39:51 -08001721 offset += second_actual;
1722 /* Second image loading not implemented. */
1723 ASSERT(0);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001724 }
1725
1726#if DEVICE_TREE
1727 if(hdr->dt_size != 0) {
1728
1729 /* Read the device tree table into buffer */
1730 if(flash_read(ptn, offset, (void *) dt_buf, page_size)) {
1731 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
1732 return -1;
1733 }
1734
1735 table = (struct dt_table*) dt_buf;
1736
Deepa Dinamani19648b42013-09-05 17:05:55 -07001737 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001738 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1739 return -1;
1740 }
1741
P.V. Phani Kumar82916762016-03-09 09:20:19 +05301742 /* Its Error if, dt_hdr_size (table->num_entries * dt_entry size + Dev_Tree Header)
1743 goes beyound hdr->dt_size*/
1744 if (dt_hdr_size > ROUND_TO_PAGE(hdr->dt_size,hdr->page_size)) {
1745 dprintf(CRITICAL, "ERROR: Invalid Device Tree size \n");
1746 return -1;
1747 }
1748
Deepa Dinamani19648b42013-09-05 17:05:55 -07001749 table = (struct dt_table*) memalign(CACHE_LINE, dt_hdr_size);
1750 if (!table)
1751 return -1;
1752
1753 /* Read the entire device tree table into buffer */
1754 if(flash_read(ptn, offset, (void *)table, dt_hdr_size)) {
1755 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
1756 return -1;
1757 }
1758
1759
Joel Kingaa335dc2013-06-03 16:11:08 -07001760 /* Find index of device tree within device tree table */
1761 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001762 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1763 return -1;
1764 }
1765
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001766 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001767 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001768 {
1769 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1770 return -1;
1771 }
1772
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001773 /* Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001774 if(flash_read(ptn, offset + dt_entry.offset,
1775 (void *)hdr->tags_addr, dt_entry.size)) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001776 dprintf(CRITICAL, "ERROR: Cannot read device tree\n");
1777 return -1;
1778 }
1779 }
1780#endif
1781
Shashank Mittalcd98d472011-08-02 14:29:24 -07001782 }
David Ng183a7422009-12-07 14:55:21 -08001783continue_boot:
Dima Zavin214cc642009-01-26 11:16:21 -08001784
Dima Zavin214cc642009-01-26 11:16:21 -08001785 /* TODO: create/pass atags to kernel */
1786
Ajay Dudanie28a6072011-07-01 13:59:46 -07001787 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07001788 (const char *)hdr->cmdline, board_machtype(),
Dima Zavin214cc642009-01-26 11:16:21 -08001789 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
1790
1791 return 0;
1792}
Brian Swetland9c4c0752009-01-25 16:23:50 -08001793
Shashank Mittal162244e2011-08-08 19:01:25 -07001794void write_device_info_mmc(device_info *dev)
1795{
Shashank Mittal162244e2011-08-08 19:01:25 -07001796 unsigned long long ptn = 0;
1797 unsigned long long size;
1798 int index = INVALID_PTN;
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001799 uint32_t blocksize;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001800 uint8_t lun = 0;
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001801 uint32_t ret = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001802
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001803 if (devinfo_present)
1804 index = partition_get_index("devinfo");
1805 else
1806 index = partition_get_index("aboot");
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001807
Shashank Mittal162244e2011-08-08 19:01:25 -07001808 ptn = partition_get_offset(index);
1809 if(ptn == 0)
1810 {
1811 return;
1812 }
1813
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001814 lun = partition_get_lun(index);
1815 mmc_set_lun(lun);
1816
Shashank Mittal162244e2011-08-08 19:01:25 -07001817 size = partition_get_size(index);
1818
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001819 blocksize = mmc_get_device_blocksize();
1820
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001821 if (devinfo_present)
1822 ret = mmc_write(ptn, blocksize, (void *)info_buf);
1823 else
1824 ret = mmc_write((ptn + size - blocksize), blocksize, (void *)info_buf);
1825 if (ret)
Shashank Mittal162244e2011-08-08 19:01:25 -07001826 {
1827 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
Sridhar Parasurame1a97922015-07-27 15:31:22 -07001828 ASSERT(0);
Shashank Mittal162244e2011-08-08 19:01:25 -07001829 }
1830}
1831
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001832void read_device_info_mmc(struct device_info *info)
Shashank Mittal162244e2011-08-08 19:01:25 -07001833{
Shashank Mittal162244e2011-08-08 19:01:25 -07001834 unsigned long long ptn = 0;
1835 unsigned long long size;
1836 int index = INVALID_PTN;
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001837 uint32_t blocksize;
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001838 uint32_t ret = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001839
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001840 if ((index = partition_get_index("devinfo")) < 0)
1841 {
1842 devinfo_present = false;
1843 index = partition_get_index("aboot");
1844 }
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001845
Shashank Mittal162244e2011-08-08 19:01:25 -07001846 ptn = partition_get_offset(index);
1847 if(ptn == 0)
1848 {
1849 return;
1850 }
1851
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001852 mmc_set_lun(partition_get_lun(index));
1853
Shashank Mittal162244e2011-08-08 19:01:25 -07001854 size = partition_get_size(index);
1855
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001856 blocksize = mmc_get_device_blocksize();
1857
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001858 if (devinfo_present)
1859 ret = mmc_read(ptn, (void *)info_buf, blocksize);
1860 else
1861 ret = mmc_read((ptn + size - blocksize), (void *)info_buf, blocksize);
1862 if (ret)
Shashank Mittal162244e2011-08-08 19:01:25 -07001863 {
1864 dprintf(CRITICAL, "ERROR: Cannot read device info\n");
Sridhar Parasurame1a97922015-07-27 15:31:22 -07001865 ASSERT(0);
Shashank Mittal162244e2011-08-08 19:01:25 -07001866 }
Shashank Mittal162244e2011-08-08 19:01:25 -07001867}
1868
1869void write_device_info_flash(device_info *dev)
1870{
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001871 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
Shashank Mittal162244e2011-08-08 19:01:25 -07001872 struct ptentry *ptn;
1873 struct ptable *ptable;
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001874 if(info == NULL)
1875 {
1876 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
1877 ASSERT(0);
1878 }
1879 info_buf = info;
Shashank Mittal162244e2011-08-08 19:01:25 -07001880 ptable = flash_get_ptable();
1881 if (ptable == NULL)
1882 {
1883 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1884 return;
1885 }
1886
1887 ptn = ptable_find(ptable, "devinfo");
1888 if (ptn == NULL)
1889 {
Smita Ghosh670c8b82015-05-07 09:30:03 -07001890 dprintf(CRITICAL, "ERROR: No devinfo partition found\n");
Shashank Mittal162244e2011-08-08 19:01:25 -07001891 return;
1892 }
1893
1894 memcpy(info, dev, sizeof(device_info));
1895
1896 if (flash_write(ptn, 0, (void *)info_buf, page_size))
1897 {
1898 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1899 return;
1900 }
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001901 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07001902}
1903
vijay kumarc65876c2015-04-24 13:29:16 +05301904static int read_allow_oem_unlock(device_info *dev)
1905{
vijay kumarc65876c2015-04-24 13:29:16 +05301906 unsigned offset;
1907 int index;
1908 unsigned long long ptn;
1909 unsigned long long ptn_size;
1910 unsigned blocksize = mmc_get_device_blocksize();
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07001911 STACKBUF_DMA_ALIGN(buf, blocksize);
vijay kumarc65876c2015-04-24 13:29:16 +05301912
vijay kumarca2e6812015-07-08 20:28:25 +05301913 index = partition_get_index(frp_ptns[0]);
vijay kumarc65876c2015-04-24 13:29:16 +05301914 if (index == INVALID_PTN)
1915 {
vijay kumarca2e6812015-07-08 20:28:25 +05301916 index = partition_get_index(frp_ptns[1]);
1917 if (index == INVALID_PTN)
1918 {
1919 dprintf(CRITICAL, "Neither '%s' nor '%s' partition found\n", frp_ptns[0],frp_ptns[1]);
1920 return -1;
1921 }
vijay kumarc65876c2015-04-24 13:29:16 +05301922 }
1923
1924 ptn = partition_get_offset(index);
1925 ptn_size = partition_get_size(index);
1926 offset = ptn_size - blocksize;
1927
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07001928 if (mmc_read(ptn + offset, (void *)buf, blocksize))
vijay kumarc65876c2015-04-24 13:29:16 +05301929 {
1930 dprintf(CRITICAL, "Reading MMC failed\n");
1931 return -1;
1932 }
1933
1934 /*is_allow_unlock is a bool value stored at the LSB of last byte*/
1935 is_allow_unlock = buf[blocksize-1] & 0x01;
1936 return 0;
1937}
1938
1939static int write_allow_oem_unlock(bool allow_unlock)
1940{
vijay kumarc65876c2015-04-24 13:29:16 +05301941 unsigned offset;
vijay kumarc65876c2015-04-24 13:29:16 +05301942 int index;
1943 unsigned long long ptn;
1944 unsigned long long ptn_size;
1945 unsigned blocksize = mmc_get_device_blocksize();
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07001946 STACKBUF_DMA_ALIGN(buf, blocksize);
vijay kumarc65876c2015-04-24 13:29:16 +05301947
vijay kumarca2e6812015-07-08 20:28:25 +05301948 index = partition_get_index(frp_ptns[0]);
vijay kumarc65876c2015-04-24 13:29:16 +05301949 if (index == INVALID_PTN)
1950 {
vijay kumarca2e6812015-07-08 20:28:25 +05301951 index = partition_get_index(frp_ptns[1]);
1952 if (index == INVALID_PTN)
1953 {
1954 dprintf(CRITICAL, "Neither '%s' nor '%s' partition found\n", frp_ptns[0],frp_ptns[1]);
1955 return -1;
1956 }
vijay kumarc65876c2015-04-24 13:29:16 +05301957 }
1958
1959 ptn = partition_get_offset(index);
1960 ptn_size = partition_get_size(index);
1961 offset = ptn_size - blocksize;
1962
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07001963 if (mmc_read(ptn + offset, (void *)buf, blocksize))
vijay kumarc65876c2015-04-24 13:29:16 +05301964 {
1965 dprintf(CRITICAL, "Reading MMC failed\n");
1966 return -1;
1967 }
1968
1969 /*is_allow_unlock is a bool value stored at the LSB of last byte*/
1970 buf[blocksize-1] = allow_unlock;
1971 if (mmc_write(ptn + offset, blocksize, buf))
1972 {
1973 dprintf(CRITICAL, "Writing MMC failed\n");
1974 return -1;
1975 }
1976
1977 return 0;
1978}
1979
Shashank Mittal162244e2011-08-08 19:01:25 -07001980void read_device_info_flash(device_info *dev)
1981{
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001982 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
Shashank Mittal162244e2011-08-08 19:01:25 -07001983 struct ptentry *ptn;
1984 struct ptable *ptable;
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001985 if(info == NULL)
1986 {
1987 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
1988 ASSERT(0);
1989 }
1990 info_buf = info;
Shashank Mittal162244e2011-08-08 19:01:25 -07001991 ptable = flash_get_ptable();
1992 if (ptable == NULL)
1993 {
1994 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1995 return;
1996 }
1997
1998 ptn = ptable_find(ptable, "devinfo");
1999 if (ptn == NULL)
2000 {
Smita Ghosh670c8b82015-05-07 09:30:03 -07002001 dprintf(CRITICAL, "ERROR: No devinfo partition found\n");
Shashank Mittal162244e2011-08-08 19:01:25 -07002002 return;
2003 }
2004
2005 if (flash_read(ptn, 0, (void *)info_buf, page_size))
2006 {
2007 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
2008 return;
2009 }
2010
2011 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
2012 {
Shashank Mittal162244e2011-08-08 19:01:25 -07002013 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
2014 info->is_unlocked = 0;
Shashank Mittala0032282011-08-26 14:50:11 -07002015 info->is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07002016 write_device_info_flash(info);
2017 }
2018 memcpy(dev, info, sizeof(device_info));
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002019 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07002020}
2021
2022void write_device_info(device_info *dev)
2023{
2024 if(target_is_emmc_boot())
2025 {
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002026 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
2027 if(info == NULL)
2028 {
2029 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
2030 ASSERT(0);
2031 }
2032 info_buf = info;
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002033 memcpy(info, dev, sizeof(struct device_info));
2034
2035#if USE_RPMB_FOR_DEVINFO
Sridhar Parasurame1a97922015-07-27 15:31:22 -07002036 if (is_secure_boot_enable()) {
2037 if((write_device_info_rpmb((void*) info, PAGE_SIZE)) < 0)
2038 ASSERT(0);
2039 }
Channagoud Kadabic80cb492015-04-28 16:08:28 -07002040 else
2041 write_device_info_mmc(info);
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002042#else
2043 write_device_info_mmc(info);
2044#endif
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002045 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07002046 }
2047 else
2048 {
2049 write_device_info_flash(dev);
2050 }
2051}
2052
2053void read_device_info(device_info *dev)
2054{
2055 if(target_is_emmc_boot())
2056 {
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002057 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
2058 if(info == NULL)
2059 {
2060 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
2061 ASSERT(0);
2062 }
2063 info_buf = info;
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002064
2065#if USE_RPMB_FOR_DEVINFO
Sridhar Parasurame1a97922015-07-27 15:31:22 -07002066 if (is_secure_boot_enable()) {
2067 if((read_device_info_rpmb((void*) info, PAGE_SIZE)) < 0)
2068 ASSERT(0);
2069 }
Channagoud Kadabic80cb492015-04-28 16:08:28 -07002070 else
2071 read_device_info_mmc(info);
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002072#else
2073 read_device_info_mmc(info);
2074#endif
2075
2076 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
2077 {
2078 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
lijuang511a2b52015-08-14 20:50:51 +08002079 if (is_secure_boot_enable()) {
Channagoud Kadabi05f78ba2015-07-06 11:58:14 -07002080 info->is_unlocked = 0;
Parth Dixitddbc7352015-10-18 03:13:31 +05302081#if !VBOOT_MOTA
lijuang511a2b52015-08-14 20:50:51 +08002082 info->is_unlock_critical = 0;
Parth Dixitddbc7352015-10-18 03:13:31 +05302083#endif
lijuang511a2b52015-08-14 20:50:51 +08002084 } else {
Channagoud Kadabi2fda4092015-07-07 13:34:11 -07002085 info->is_unlocked = 1;
Parth Dixitddbc7352015-10-18 03:13:31 +05302086#if !VBOOT_MOTA
lijuang511a2b52015-08-14 20:50:51 +08002087 info->is_unlock_critical = 1;
Parth Dixitddbc7352015-10-18 03:13:31 +05302088#endif
lijuang511a2b52015-08-14 20:50:51 +08002089 }
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002090 info->is_tampered = 0;
2091 info->charger_screen_enabled = 0;
Parth Dixitddbc7352015-10-18 03:13:31 +05302092#if !VBOOT_MOTA
Sridhar Parasuram287e5062015-08-24 16:17:22 -07002093 info->verity_mode = 1; //enforcing by default
Parth Dixitddbc7352015-10-18 03:13:31 +05302094#endif
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002095 write_device_info(info);
2096 }
2097 memcpy(dev, info, sizeof(device_info));
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002098 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07002099 }
2100 else
2101 {
2102 read_device_info_flash(dev);
2103 }
2104}
2105
2106void reset_device_info()
2107{
2108 dprintf(ALWAYS, "reset_device_info called.");
Shashank Mittala0032282011-08-26 14:50:11 -07002109 device.is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07002110 write_device_info(&device);
2111}
2112
2113void set_device_root()
2114{
2115 dprintf(ALWAYS, "set_device_root called.");
Shashank Mittala0032282011-08-26 14:50:11 -07002116 device.is_tampered = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -07002117 write_device_info(&device);
2118}
2119
lijuang4ece1e72015-08-14 21:02:36 +08002120/* set device unlock value
2121 * Must check FRP before call this function
2122 * Need to wipe data when unlock status changed
2123 * type 0: oem unlock
2124 * type 1: unlock critical
2125 * status 0: unlock as false
2126 * status 1: lock as true
2127 */
2128void set_device_unlock_value(int type, bool status)
lijuang21f12f52015-08-22 16:22:19 +08002129{
lijuang4ece1e72015-08-14 21:02:36 +08002130 if (type == UNLOCK)
2131 device.is_unlocked = status;
Parth Dixitddbc7352015-10-18 03:13:31 +05302132#if !VBOOT_MOTA
lijuang4ece1e72015-08-14 21:02:36 +08002133 else if (type == UNLOCK_CRITICAL)
2134 device.is_unlock_critical = status;
Parth Dixitddbc7352015-10-18 03:13:31 +05302135#endif
lijuang4ece1e72015-08-14 21:02:36 +08002136 write_device_info(&device);
2137}
2138
2139static void set_device_unlock(int type, bool status)
2140{
2141 int is_unlocked = -1;
2142 char response[MAX_RSP_SIZE];
2143
2144 /* check device unlock status if it is as expected */
2145 if (type == UNLOCK)
2146 is_unlocked = device.is_unlocked;
Parth Dixitddbc7352015-10-18 03:13:31 +05302147#if !VBOOT_MOTA
lijuang4ece1e72015-08-14 21:02:36 +08002148 else if (type == UNLOCK_CRITICAL)
2149 is_unlocked = device.is_unlock_critical;
Parth Dixitddbc7352015-10-18 03:13:31 +05302150#endif
lijuang4ece1e72015-08-14 21:02:36 +08002151 if (is_unlocked == status) {
2152 snprintf(response, sizeof(response), "\tDevice already : %s", (status ? "unlocked!" : "locked!"));
2153 fastboot_info(response);
2154 fastboot_okay("");
2155 return;
2156 }
2157
2158 /* status is true, it means to unlock device */
2159 if (status) {
lijuang21f12f52015-08-22 16:22:19 +08002160 if(!is_allow_unlock) {
2161 fastboot_fail("oem unlock is not allowed");
2162 return;
2163 }
2164
lijuang4ece1e72015-08-14 21:02:36 +08002165#if FBCON_DISPLAY_MSG
lijuang9a7d3b92015-11-30 14:41:24 +08002166 display_unlock_menu(type);
lijuang4ece1e72015-08-14 21:02:36 +08002167 fastboot_okay("");
2168 return;
2169#else
2170 if (type == UNLOCK) {
2171 fastboot_fail("Need wipe userdata. Do 'fastboot oem unlock-go'");
2172 return;
2173 }
2174#endif
lijuang21f12f52015-08-22 16:22:19 +08002175 }
lijuang4ece1e72015-08-14 21:02:36 +08002176
2177 set_device_unlock_value(type, status);
2178
2179 /* wipe data */
2180 struct recovery_message msg;
2181
2182 snprintf(msg.recovery, sizeof(msg.recovery), "recovery\n--wipe_data");
2183 write_misc(0, &msg, sizeof(msg));
2184
2185 fastboot_okay("");
2186 reboot_device(RECOVERY_MODE);
lijuang21f12f52015-08-22 16:22:19 +08002187}
2188
lijuang511a2b52015-08-14 20:50:51 +08002189static bool critical_flash_allowed(const char * entry)
2190{
2191 uint32_t i = 0;
2192 if (entry == NULL)
2193 return false;
2194
2195 for (i = 0; i < ARRAY_SIZE(critical_flash_allowed_ptn); i++) {
2196 if(!strcmp(entry, critical_flash_allowed_ptn[i]))
2197 return true;
2198 }
2199 return false;
Matthew Qin271927e2015-03-31 22:07:07 -04002200}
2201
2202#if DEVICE_TREE
Amol Jadicb524072012-08-09 16:40:18 -07002203int copy_dtb(uint8_t *boot_image_start, unsigned int scratch_offset)
2204{
2205 uint32 dt_image_offset = 0;
Amol Jadicb524072012-08-09 16:40:18 -07002206 uint32_t n;
Amol Jadicb524072012-08-09 16:40:18 -07002207 struct dt_table *table;
2208 struct dt_entry dt_entry;
2209 uint32_t dt_hdr_size;
2210 unsigned int compressed_size = 0;
2211 unsigned int dtb_size = 0;
2212 unsigned int out_avai_len = 0;
2213 unsigned char *out_addr = NULL;
2214 unsigned char *best_match_dt_addr = NULL;
2215 int rc;
2216
2217 struct boot_img_hdr *hdr = (struct boot_img_hdr *) (boot_image_start);
2218
2219 if(hdr->dt_size != 0) {
2220 /* add kernel offset */
2221 dt_image_offset += page_size;
2222 n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07002223 dt_image_offset += n;
Amol Jadicb524072012-08-09 16:40:18 -07002224
Deepa Dinamani19648b42013-09-05 17:05:55 -07002225 /* add ramdisk offset */
Amol Jadicb524072012-08-09 16:40:18 -07002226 n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
2227 dt_image_offset += n;
2228
Joel Kingaa335dc2013-06-03 16:11:08 -07002229 /* add second offset */
2230 if(hdr->second_size != 0) {
Amol Jadicb524072012-08-09 16:40:18 -07002231 n = ROUND_TO_PAGE(hdr->second_size, page_mask);
2232 dt_image_offset += n;
2233 }
2234
Matthew Qin271927e2015-03-31 22:07:07 -04002235 /* offset now point to start of dt.img */
2236 table = (struct dt_table*)(boot_image_start + dt_image_offset);
2237
2238 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
2239 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
Matthew Qin0b15b322015-05-19 05:20:54 -04002240 return -1;
Matthew Qin271927e2015-03-31 22:07:07 -04002241 }
P.V. Phani Kumar82916762016-03-09 09:20:19 +05302242
2243 /* Its Error if, dt_hdr_size (table->num_entries * dt_entry size + Dev_Tree Header)
2244 goes beyound hdr->dt_size*/
2245 if (dt_hdr_size > ROUND_TO_PAGE(hdr->dt_size,hdr->page_size)) {
2246 dprintf(CRITICAL, "ERROR: Invalid Device Tree size \n");
2247 return -1;
2248 }
2249
Amol Jadicb524072012-08-09 16:40:18 -07002250 /* Find index of device tree within device tree table */
2251 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
2252 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
2253 return -1;
2254 }
2255
Matthew Qin271927e2015-03-31 22:07:07 -04002256 best_match_dt_addr = (unsigned char *)boot_image_start + dt_image_offset + dt_entry.offset;
2257 if (is_gzip_package(best_match_dt_addr, dt_entry.size))
2258 {
2259 out_addr = (unsigned char *)target_get_scratch_address() + scratch_offset;
2260 out_avai_len = target_get_max_flash_size() - scratch_offset;
2261 dprintf(INFO, "decompressing dtb: start\n");
2262 rc = decompress(best_match_dt_addr,
2263 dt_entry.size, out_addr, out_avai_len,
2264 &compressed_size, &dtb_size);
2265 if (rc)
2266 {
Matthew Qin0b15b322015-05-19 05:20:54 -04002267 dprintf(CRITICAL, "decompressing dtb failed!!!\n");
Matthew Qin271927e2015-03-31 22:07:07 -04002268 ASSERT(0);
2269 }
2270
Matthew Qin0b15b322015-05-19 05:20:54 -04002271 dprintf(INFO, "decompressing dtb: done\n");
Matthew Qin271927e2015-03-31 22:07:07 -04002272 best_match_dt_addr = out_addr;
2273 } else {
2274 dtb_size = dt_entry.size;
2275 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002276 /* Validate and Read device device tree in the "tags_add */
Matthew Qin271927e2015-03-31 22:07:07 -04002277 if (check_aboot_addr_range_overlap(hdr->tags_addr, dtb_size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002278 {
2279 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
2280 return -1;
2281 }
2282
Amol Jadicb524072012-08-09 16:40:18 -07002283 /* Read device device tree in the "tags_add */
Matthew Qin271927e2015-03-31 22:07:07 -04002284 memmove((void*) hdr->tags_addr, (void *)best_match_dt_addr, dtb_size);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002285 } else
2286 return -1;
Amol Jadicb524072012-08-09 16:40:18 -07002287
2288 /* Everything looks fine. Return success. */
2289 return 0;
2290}
2291#endif
2292
Brian Swetland9c4c0752009-01-25 16:23:50 -08002293void cmd_boot(const char *arg, void *data, unsigned sz)
2294{
Amit Blay8a510302015-08-17 09:20:01 +03002295#ifdef MDTP_SUPPORT
2296 static bool is_mdtp_activated = 0;
2297#endif /* MDTP_SUPPORT */
Brian Swetland9c4c0752009-01-25 16:23:50 -08002298 unsigned kernel_actual;
2299 unsigned ramdisk_actual;
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002300 uint32_t image_actual;
2301 uint32_t dt_actual = 0;
P.V. Phani Kumareecfd822016-03-09 20:09:03 +05302302 uint32_t sig_actual = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08002303 struct boot_img_hdr *hdr = NULL;
2304 struct kernel64_hdr *kptr = NULL;
Brian Swetland9c4c0752009-01-25 16:23:50 -08002305 char *ptr = ((char*) data);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002306 int ret = 0;
2307 uint8_t dtb_copied = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08002308 unsigned int out_len = 0;
2309 unsigned int out_avai_len = 0;
2310 unsigned char *out_addr = NULL;
2311 uint32_t dtb_offset = 0;
2312 unsigned char *kernel_start_addr = NULL;
2313 unsigned int kernel_size = 0;
Matthew Qin271927e2015-03-31 22:07:07 -04002314 unsigned int scratch_offset = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08002315
lijuang2008ff22016-03-07 17:56:27 +08002316#if FBCON_DISPLAY_MSG
2317 /* Exit keys' detection thread firstly */
2318 exit_menu_keys_detection();
2319#endif
2320
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002321#if VERIFIED_BOOT
Channagoud Kadabi6d5375e2015-06-23 17:15:42 -07002322 if(target_build_variant_user() && !device.is_unlocked)
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002323 {
2324 fastboot_fail("unlock device to use this command");
lijuang2008ff22016-03-07 17:56:27 +08002325 goto boot_failed;
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002326 }
2327#endif
2328
Brian Swetland9c4c0752009-01-25 16:23:50 -08002329 if (sz < sizeof(hdr)) {
2330 fastboot_fail("invalid bootimage header");
lijuang2008ff22016-03-07 17:56:27 +08002331 goto boot_failed;
Brian Swetland9c4c0752009-01-25 16:23:50 -08002332 }
2333
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002334 hdr = (struct boot_img_hdr *)data;
Brian Swetland9c4c0752009-01-25 16:23:50 -08002335
2336 /* ensure commandline is terminated */
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002337 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -08002338
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002339 if(target_is_emmc_boot() && hdr->page_size) {
2340 page_size = hdr->page_size;
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07002341 page_mask = page_size - 1;
2342 }
2343
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002344 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
2345 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002346#if DEVICE_TREE
2347 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
2348#endif
2349
2350 image_actual = ADD_OF(page_size, kernel_actual);
2351 image_actual = ADD_OF(image_actual, ramdisk_actual);
2352 image_actual = ADD_OF(image_actual, dt_actual);
2353
P.V. Phani Kumareecfd822016-03-09 20:09:03 +05302354 if (target_use_signed_kernel() && (!device.is_unlocked)) {
2355 /* Calculate the signature length from boot image */
2356 sig_actual = read_der_message_length(
2357 (unsigned char*)(data + image_actual),sz);
Channagoud Kadabi3d839012014-06-26 14:26:39 -07002358 image_actual = ADD_OF(image_actual, sig_actual);
P.V. Phani Kumareecfd822016-03-09 20:09:03 +05302359 }
Channagoud Kadabi3d839012014-06-26 14:26:39 -07002360
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002361 /* sz should have atleast raw boot image */
2362 if (image_actual > sz) {
Channagoud Kadabi3d839012014-06-26 14:26:39 -07002363 fastboot_fail("bootimage: incomplete or not signed");
lijuang2008ff22016-03-07 17:56:27 +08002364 goto boot_failed;
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002365 }
2366
Sridhar Parasuram361e4cd2015-09-24 11:19:19 -07002367 // Initialize boot state before trying to verify boot.img
2368#if VERIFIED_BOOT
Channagoud Kadabi699466e2015-11-03 12:37:42 -08002369 boot_verifier_init();
Channagoud Kadabibf6ce7d2015-09-17 13:25:35 -07002370 /* Handle overflow if the input image size is greater than
2371 * boot image buffer can hold
2372 */
2373 if ((target_get_max_flash_size() - (image_actual - sig_actual)) < page_size)
2374 {
2375 fastboot_fail("booimage: size is greater than boot image buffer can hold");
lijuang2008ff22016-03-07 17:56:27 +08002376 goto boot_failed;
Channagoud Kadabibf6ce7d2015-09-17 13:25:35 -07002377 }
Sridhar Parasuram361e4cd2015-09-24 11:19:19 -07002378#endif
2379
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002380 /* Verify the boot image
2381 * device & page_size are initialized in aboot_init
2382 */
Sridhar Parasuram7ae9fbc2015-09-17 09:56:30 -07002383 if (target_use_signed_kernel() && (!device.is_unlocked)) {
Channagoud Kadabi3d839012014-06-26 14:26:39 -07002384 /* Pass size excluding signature size, otherwise we would try to
2385 * access signature beyond its length
2386 */
2387 verify_signed_bootimg((uint32_t)data, (image_actual - sig_actual));
Sridhar Parasuram7ae9fbc2015-09-17 09:56:30 -07002388 }
Amit Blay4aa292f2015-04-28 21:55:59 +03002389#ifdef MDTP_SUPPORT
2390 else
2391 {
Shay Nachmani062aa3f2015-05-17 17:28:44 +03002392 /* fastboot boot is not allowed when MDTP is activated */
Amit Blay4aa292f2015-04-28 21:55:59 +03002393 mdtp_ext_partition_verification_t ext_partition;
Amit Blay8a510302015-08-17 09:20:01 +03002394
2395 if (!is_mdtp_activated) {
2396 ext_partition.partition = MDTP_PARTITION_NONE;
2397 mdtp_fwlock_verify_lock(&ext_partition);
2398 }
Amit Blay4aa292f2015-04-28 21:55:59 +03002399 }
Shay Nachmani062aa3f2015-05-17 17:28:44 +03002400
Amir Kotzer7c768c02016-04-13 09:08:05 +03002401 /* If mdtp state cannot be validate, block fastboot boot*/
2402 if(mdtp_activated(&is_mdtp_activated)){
2403 dprintf(CRITICAL, "mdtp_activated cannot validate state.\n");
2404 dprintf(CRITICAL, "Can not proceed with fastboot boot command.\n");
2405 goto boot_failed;
2406 }
Shay Nachmani062aa3f2015-05-17 17:28:44 +03002407 if(is_mdtp_activated){
2408 dprintf(CRITICAL, "fastboot boot command is not available.\n");
lijuang2008ff22016-03-07 17:56:27 +08002409 goto boot_failed;
Shay Nachmani062aa3f2015-05-17 17:28:44 +03002410 }
Amit Blay4aa292f2015-04-28 21:55:59 +03002411#endif /* MDTP_SUPPORT */
2412
Sridhar Parasuram361e4cd2015-09-24 11:19:19 -07002413#if VERIFIED_BOOT
Parth Dixitbc9b6492015-10-18 00:41:38 +05302414#if !VBOOT_MOTA
Sridhar Parasuram361e4cd2015-09-24 11:19:19 -07002415 // send root of trust
2416 if(!send_rot_command((uint32_t)device.is_unlocked))
2417 ASSERT(0);
2418#endif
Parth Dixitbc9b6492015-10-18 00:41:38 +05302419#endif
Channagoud Kadabia22144f2013-03-20 11:49:01 -07002420 /*
Matthew Qinbb7923d2015-02-09 10:56:09 +08002421 * Check if the kernel image is a gzip package. If yes, need to decompress it.
2422 * If not, continue booting.
2423 */
2424 if (is_gzip_package((unsigned char *)(data + page_size), hdr->kernel_size))
2425 {
2426 out_addr = (unsigned char *)target_get_scratch_address();
2427 out_addr = (unsigned char *)(out_addr + image_actual + page_size);
2428 out_avai_len = target_get_max_flash_size() - image_actual - page_size;
Matthew Qin0b15b322015-05-19 05:20:54 -04002429 dprintf(INFO, "decompressing kernel image: start\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08002430 ret = decompress((unsigned char *)(ptr + page_size),
2431 hdr->kernel_size, out_addr, out_avai_len,
2432 &dtb_offset, &out_len);
2433 if (ret)
2434 {
Matthew Qin0b15b322015-05-19 05:20:54 -04002435 dprintf(CRITICAL, "decompressing image failed!!!\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08002436 ASSERT(0);
2437 }
2438
Matthew Qin0b15b322015-05-19 05:20:54 -04002439 dprintf(INFO, "decompressing kernel image: done\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08002440 kptr = (struct kernel64_hdr *)out_addr;
2441 kernel_start_addr = out_addr;
2442 kernel_size = out_len;
2443 } else {
2444 kptr = (struct kernel64_hdr*)((char *)data + page_size);
2445 kernel_start_addr = (unsigned char *)((char *)data + page_size);
2446 kernel_size = hdr->kernel_size;
2447 }
2448
2449 /*
Channagoud Kadabia22144f2013-03-20 11:49:01 -07002450 * Update the kernel/ramdisk/tags address if the boot image header
2451 * has default values, these default values come from mkbootimg when
2452 * the boot image is flashed using fastboot flash:raw
2453 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08002454 update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr));
Dima Zavin3cadfff2013-03-21 14:30:48 -07002455
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002456 /* Get virtual addresses since the hdr saves physical addresses. */
2457 hdr->kernel_addr = VA(hdr->kernel_addr);
2458 hdr->ramdisk_addr = VA(hdr->ramdisk_addr);
2459 hdr->tags_addr = VA(hdr->tags_addr);
Brian Swetland9c4c0752009-01-25 16:23:50 -08002460
Matthew Qinbb7923d2015-02-09 10:56:09 +08002461 kernel_size = ROUND_TO_PAGE(kernel_size, page_mask);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002462 /* Check if the addresses in the header are valid. */
Matthew Qinbb7923d2015-02-09 10:56:09 +08002463 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_size) ||
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002464 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
2465 {
2466 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
lijuang2008ff22016-03-07 17:56:27 +08002467 goto boot_failed;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002468 }
2469
Amol Jadicb524072012-08-09 16:40:18 -07002470#if DEVICE_TREE
Matthew Qin271927e2015-03-31 22:07:07 -04002471 scratch_offset = image_actual + page_size + out_len;
Amol Jadicb524072012-08-09 16:40:18 -07002472 /* find correct dtb and copy it to right location */
Matthew Qin271927e2015-03-31 22:07:07 -04002473 ret = copy_dtb(data, scratch_offset);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002474
2475 dtb_copied = !ret ? 1 : 0;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002476#else
2477 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
2478 {
2479 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
lijuang2008ff22016-03-07 17:56:27 +08002480 goto boot_failed;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002481 }
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002482#endif
2483
2484 /* Load ramdisk & kernel */
2485 memmove((void*) hdr->ramdisk_addr, ptr + page_size + kernel_actual, hdr->ramdisk_size);
Matthew Qinbb7923d2015-02-09 10:56:09 +08002486 memmove((void*) hdr->kernel_addr, (char*) (kernel_start_addr), kernel_size);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002487
2488#if DEVICE_TREE
Matthew Qinbb7923d2015-02-09 10:56:09 +08002489 if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual))
2490 {
2491 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
lijuang2008ff22016-03-07 17:56:27 +08002492 goto boot_failed;
Matthew Qinbb7923d2015-02-09 10:56:09 +08002493 }
2494
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002495 /*
2496 * If dtb is not found look for appended DTB in the kernel.
2497 * If appended dev tree is found, update the atags with
2498 * memory address to the DTB appended location on RAM.
2499 * Else update with the atags address in the kernel header
2500 */
2501 if (!dtb_copied) {
2502 void *dtb;
Matthew Qinbb7923d2015-02-09 10:56:09 +08002503 dtb = dev_tree_appended((void*)(ptr + page_size),
2504 hdr->kernel_size, dtb_offset,
Dima Zavine63e5572013-05-03 12:23:06 -07002505 (void *)hdr->tags_addr);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002506 if (!dtb) {
2507 fastboot_fail("dtb not found");
lijuang2008ff22016-03-07 17:56:27 +08002508 goto boot_failed;
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002509 }
Amol Jadicb524072012-08-09 16:40:18 -07002510 }
2511#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08002512
2513 fastboot_okay("");
Amol Jadi7c4316c2013-10-07 14:19:26 -07002514 fastboot_stop();
Brian Swetland9c4c0752009-01-25 16:23:50 -08002515
Dima Zavin77e41f32013-03-06 16:10:43 -08002516 boot_linux((void*) hdr->kernel_addr, (void*) hdr->tags_addr,
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002517 (const char*) hdr->cmdline, board_machtype(),
2518 (void*) hdr->ramdisk_addr, hdr->ramdisk_size);
lijuang2008ff22016-03-07 17:56:27 +08002519
2520 /* fastboot already stop, it's no need to show fastboot menu */
2521 return;
2522boot_failed:
2523#if FBCON_DISPLAY_MSG
2524 /* revert to fastboot menu if boot failed */
2525 display_fastboot_menu();
2526#endif
2527 return;
Brian Swetland9c4c0752009-01-25 16:23:50 -08002528}
2529
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002530void cmd_erase_nand(const char *arg, void *data, unsigned sz)
Dima Zavin214cc642009-01-26 11:16:21 -08002531{
2532 struct ptentry *ptn;
2533 struct ptable *ptable;
2534
2535 ptable = flash_get_ptable();
2536 if (ptable == NULL) {
2537 fastboot_fail("partition table doesn't exist");
2538 return;
2539 }
2540
2541 ptn = ptable_find(ptable, arg);
2542 if (ptn == NULL) {
2543 fastboot_fail("unknown partition name");
2544 return;
2545 }
2546
2547 if (flash_erase(ptn)) {
2548 fastboot_fail("failed to erase partition");
2549 return;
2550 }
2551 fastboot_okay("");
2552}
2553
Bikas Gurungd48bd242010-09-04 19:54:32 -07002554
2555void cmd_erase_mmc(const char *arg, void *data, unsigned sz)
2556{
2557 unsigned long long ptn = 0;
Oliver Wangcee448d2013-10-22 18:40:13 +08002558 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07002559 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002560 uint8_t lun = 0;
Vijay Kumar Pendoti0b21f462016-05-02 17:09:18 +05302561 char *footer = NULL;
Bikas Gurungd48bd242010-09-04 19:54:32 -07002562
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002563#if VERIFIED_BOOT
2564 if(!strcmp(arg, KEYSTORE_PTN_NAME))
2565 {
2566 if(!device.is_unlocked)
2567 {
2568 fastboot_fail("unlock device to erase keystore");
2569 return;
2570 }
2571 }
2572#endif
2573
Kinson Chikf1a43512011-07-14 11:28:39 -07002574 index = partition_get_index(arg);
2575 ptn = partition_get_offset(index);
Oliver Wangcee448d2013-10-22 18:40:13 +08002576 size = partition_get_size(index);
Neeti Desaica8c9602011-10-06 11:40:00 -07002577
Kinson Chikf1a43512011-07-14 11:28:39 -07002578 if(ptn == 0) {
Neeti Desaica8c9602011-10-06 11:40:00 -07002579 fastboot_fail("Partition table doesn't exist\n");
Bikas Gurungd48bd242010-09-04 19:54:32 -07002580 return;
2581 }
Kun Liang2f1601a2013-08-12 16:29:54 +08002582
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002583 lun = partition_get_lun(index);
2584 mmc_set_lun(lun);
2585
Sridhar Parasuramd7957122015-02-27 11:33:40 -08002586 if (platform_boot_dev_isemmc())
2587 {
2588 if (mmc_erase_card(ptn, size)) {
2589 fastboot_fail("failed to erase partition\n");
2590 return;
2591 }
2592 } else {
2593 BUF_DMA_ALIGN(out, DEFAULT_ERASE_SIZE);
2594 size = partition_get_size(index);
2595 if (size > DEFAULT_ERASE_SIZE)
2596 size = DEFAULT_ERASE_SIZE;
Kun Liang2f1601a2013-08-12 16:29:54 +08002597
Sridhar Parasuramd7957122015-02-27 11:33:40 -08002598 /* Simple inefficient version of erase. Just writing
2599 0 in first several blocks */
2600 if (mmc_write(ptn , size, (unsigned int *)out)) {
2601 fastboot_fail("failed to erase partition");
2602 return;
2603 }
Vijay Kumar Pendoti0b21f462016-05-02 17:09:18 +05302604 /*Erase FDE metadata at the userdata footer*/
2605 if(!(strncmp(arg, "userdata", 8)))
2606 {
2607 footer = memalign(CACHE_LINE, FOOTER_SIZE);
2608 memset((void *)footer, 0, FOOTER_SIZE);
2609
2610 size = partition_get_size(index);
2611
2612 if (mmc_write((ptn + size) - FOOTER_SIZE , FOOTER_SIZE, (unsigned int *)footer)) {
2613 fastboot_fail("failed to erase userdata footer");
2614 free(footer);
2615 return;
2616 }
2617 free(footer);
2618 }
Bikas Gurungd48bd242010-09-04 19:54:32 -07002619 }
Sridhar Parasuram32b30662015-07-10 13:33:22 -07002620#if VERIFIED_BOOT
Parth Dixitbc9b6492015-10-18 00:41:38 +05302621#if !VBOOT_MOTA
Sridhar Parasuram32b30662015-07-10 13:33:22 -07002622 if(!(strncmp(arg, "userdata", 8)))
2623 if(send_delete_keys_to_tz())
2624 ASSERT(0);
2625#endif
Parth Dixitbc9b6492015-10-18 00:41:38 +05302626#endif
Bikas Gurungd48bd242010-09-04 19:54:32 -07002627 fastboot_okay("");
2628}
2629
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002630void cmd_erase(const char *arg, void *data, unsigned sz)
2631{
Channagoud Kadabice3cf422015-04-17 16:02:30 -07002632#if VERIFIED_BOOT
Channagoud Kadabi35297672015-06-13 11:09:49 -07002633 if (target_build_variant_user())
Channagoud Kadabice3cf422015-04-17 16:02:30 -07002634 {
Sridhar Parasuramc32d07d2015-07-12 10:57:48 -07002635 if(!device.is_unlocked)
Channagoud Kadabice3cf422015-04-17 16:02:30 -07002636 {
Channagoud Kadabi35297672015-06-13 11:09:49 -07002637 fastboot_fail("device is locked. Cannot erase");
Channagoud Kadabice3cf422015-04-17 16:02:30 -07002638 return;
2639 }
2640 }
2641#endif
2642
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002643 if(target_is_emmc_boot())
2644 cmd_erase_mmc(arg, data, sz);
2645 else
2646 cmd_erase_nand(arg, data, sz);
2647}
Bikas Gurungd48bd242010-09-04 19:54:32 -07002648
Channagoud Kadabiad259832015-05-29 11:14:17 -07002649static uint32_t aboot_get_secret_key()
2650{
2651 /* 0 is invalid secret key, update this implementation to return
2652 * device specific unique secret key
2653 */
2654 return 0;
2655}
2656
Ajay Dudani5c761132011-04-07 20:19:04 -07002657void cmd_flash_mmc_img(const char *arg, void *data, unsigned sz)
Shashank Mittal23b8f422010-04-16 19:27:21 -07002658{
2659 unsigned long long ptn = 0;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07002660 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07002661 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002662 char *token = NULL;
2663 char *pname = NULL;
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07002664 char *sp;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002665 uint8_t lun = 0;
2666 bool lun_set = false;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07002667
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07002668 token = strtok_r((char *)arg, ":", &sp);
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002669 pname = token;
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07002670 token = strtok_r(NULL, ":", &sp);
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002671 if(token)
2672 {
2673 lun = atoi(token);
2674 mmc_set_lun(lun);
2675 lun_set = true;
2676 }
2677
Mao Jinlong226f33a2014-07-04 17:24:10 +08002678 if (pname)
Greg Grisco6e754772011-06-23 12:19:39 -07002679 {
Channagoud Kadabiad259832015-05-29 11:14:17 -07002680 if (!strncmp(pname, "frp-unlock", strlen("frp-unlock")))
2681 {
2682 if (!aboot_frp_unlock(pname, data, sz))
2683 {
2684 fastboot_info("FRP unlock successful");
2685 fastboot_okay("");
2686 }
2687 else
2688 fastboot_fail("Secret key is invalid, please update the bootloader with secret key");
2689
2690 return;
2691 }
2692
Mao Jinlong226f33a2014-07-04 17:24:10 +08002693 if (!strcmp(pname, "partition"))
2694 {
2695 dprintf(INFO, "Attempt to write partition image.\n");
2696 if (write_partition(sz, (unsigned char *) data)) {
2697 fastboot_fail("failed to write partition");
Greg Grisco6e754772011-06-23 12:19:39 -07002698 return;
2699 }
2700 }
Mao Jinlong226f33a2014-07-04 17:24:10 +08002701 else
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002702 {
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002703#if VERIFIED_BOOT
2704 if(!strcmp(pname, KEYSTORE_PTN_NAME))
2705 {
2706 if(!device.is_unlocked)
2707 {
2708 fastboot_fail("unlock device to flash keystore");
2709 return;
2710 }
Gaurav Nebhwanic7313cc2015-12-15 22:25:04 +05302711 if(!boot_verify_validate_keystore((unsigned char *)data,sz))
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002712 {
2713 fastboot_fail("image is not a keystore file");
2714 return;
2715 }
2716 }
2717#endif
Mao Jinlong226f33a2014-07-04 17:24:10 +08002718 index = partition_get_index(pname);
2719 ptn = partition_get_offset(index);
2720 if(ptn == 0) {
2721 fastboot_fail("partition table doesn't exist");
2722 return;
2723 }
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002724
Mao Jinlong226f33a2014-07-04 17:24:10 +08002725 if (!strcmp(pname, "boot") || !strcmp(pname, "recovery")) {
2726 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
2727 fastboot_fail("image is not a boot image");
2728 return;
2729 }
2730 }
2731
2732 if(!lun_set)
2733 {
2734 lun = partition_get_lun(index);
2735 mmc_set_lun(lun);
2736 }
2737
2738 size = partition_get_size(index);
Gaurav Nebhwani4d2389c2016-03-17 21:10:05 +05302739 if (ROUND_TO_PAGE(sz, mmc_blocksize_mask) > size) {
Mao Jinlong226f33a2014-07-04 17:24:10 +08002740 fastboot_fail("size too large");
2741 return;
2742 }
2743 else if (mmc_write(ptn , sz, (unsigned int *)data)) {
2744 fastboot_fail("flash write failure");
2745 return;
2746 }
Greg Grisco6e754772011-06-23 12:19:39 -07002747 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07002748 }
2749 fastboot_okay("");
2750 return;
2751}
2752
Ajay Dudanide984792015-03-02 09:57:41 -08002753void cmd_flash_meta_img(const char *arg, void *data, unsigned sz)
2754{
2755 int i, images;
2756 meta_header_t *meta_header;
2757 img_header_entry_t *img_header_entry;
Parth Dixit3e2d3032016-03-04 17:11:52 +05302758 /*End of the image address*/
2759 uintptr_t data_end;
2760
2761 if( (UINT_MAX - sz) > (uintptr_t)data )
2762 data_end = (uintptr_t)data + sz;
2763 else
2764 {
2765 fastboot_fail("Cannot flash: image header corrupt");
2766 return;
2767 }
2768
2769 if( data_end < ((uintptr_t)data + sizeof(meta_header_t)))
2770 {
2771 fastboot_fail("Cannot flash: image header corrupt");
2772 return;
2773 }
Ajay Dudanide984792015-03-02 09:57:41 -08002774
lijuang8ee21882016-04-19 16:57:11 +08002775 /* If device is locked:
2776 * Forbid to flash image to avoid the device to bypass the image
2777 * which with "any" name other than bootloader. Because it maybe
2778 * a meta package of all partitions.
2779 */
2780#if VERIFIED_BOOT
2781 if (target_build_variant_user()) {
2782 if (!device.is_unlocked) {
2783 fastboot_fail("Device is locked, meta image flashing is not allowed");
2784 return;
2785 }
2786#if !VBOOT_MOTA
2787 if(!device.is_unlock_critical) {
2788 fastboot_fail("Device is critical locked, Meta image flashing is not allowed");
2789 return;
2790 }
2791#endif
2792 }
2793#endif
2794
Ajay Dudanide984792015-03-02 09:57:41 -08002795 meta_header = (meta_header_t*) data;
Parth Dixit3e2d3032016-03-04 17:11:52 +05302796 if( data_end < ((uintptr_t)data + meta_header->img_hdr_sz))
2797 {
2798 fastboot_fail("Cannot flash: image header corrupt");
2799 return;
2800 }
Ajay Dudanide984792015-03-02 09:57:41 -08002801 img_header_entry = (img_header_entry_t*) (data+sizeof(meta_header_t));
2802
2803 images = meta_header->img_hdr_sz / sizeof(img_header_entry_t);
2804
2805 for (i=0; i<images; i++) {
2806
2807 if((img_header_entry[i].ptn_name == NULL) ||
2808 (img_header_entry[i].start_offset == 0) ||
2809 (img_header_entry[i].size == 0))
2810 break;
2811
Parth Dixit3e2d3032016-03-04 17:11:52 +05302812 if( data_end < ((uintptr_t)data + img_header_entry[i].start_offset
2813 + img_header_entry[i].size) )
2814 {
2815 fastboot_fail("Cannot flash: image size mismatch");
2816 break;
2817 }
2818
Ajay Dudanide984792015-03-02 09:57:41 -08002819 cmd_flash_mmc_img(img_header_entry[i].ptn_name,
2820 (void *) data + img_header_entry[i].start_offset,
2821 img_header_entry[i].size);
2822 }
2823
Channagoud Kadabiaafbbfc2015-03-04 17:52:56 -08002824 if (!strncmp(arg, "bootloader", strlen("bootloader")))
2825 {
2826 strlcpy(device.bootloader_version, TARGET(BOARD), MAX_VERSION_LEN);
2827 strlcat(device.bootloader_version, "-", MAX_VERSION_LEN);
2828 strlcat(device.bootloader_version, meta_header->img_version, MAX_VERSION_LEN);
2829 }
2830 else
2831 {
2832 strlcpy(device.radio_version, TARGET(BOARD), MAX_VERSION_LEN);
2833 strlcat(device.radio_version, "-", MAX_VERSION_LEN);
2834 strlcat(device.radio_version, meta_header->img_version, MAX_VERSION_LEN);
2835 }
2836
2837 write_device_info(&device);
Ajay Dudanide984792015-03-02 09:57:41 -08002838 fastboot_okay("");
2839 return;
2840}
2841
Ajay Dudani5c761132011-04-07 20:19:04 -07002842void cmd_flash_mmc_sparse_img(const char *arg, void *data, unsigned sz)
2843{
2844 unsigned int chunk;
wufeng.jiang813dc352015-06-02 23:02:46 -04002845 uint64_t chunk_data_sz;
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002846 uint32_t *fill_buf = NULL;
2847 uint32_t fill_val;
Ajay Dudani5c761132011-04-07 20:19:04 -07002848 sparse_header_t *sparse_header;
2849 chunk_header_t *chunk_header;
Ajay Dudaniab18f022011-05-12 14:39:22 -07002850 uint32_t total_blocks = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07002851 unsigned long long ptn = 0;
Channagoud Kadabi65b91002011-10-11 17:34:33 +05302852 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07002853 int index = INVALID_PTN;
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05302854 uint32_t i;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002855 uint8_t lun = 0;
vijay kumar800255e2015-04-24 20:26:19 +05302856 /*End of the sparse image address*/
Parth Dixit64b1a482016-03-07 16:31:26 +05302857 uintptr_t data_end = (uintptr_t)data + sz;
Ajay Dudani5c761132011-04-07 20:19:04 -07002858
Kinson Chikf1a43512011-07-14 11:28:39 -07002859 index = partition_get_index(arg);
2860 ptn = partition_get_offset(index);
2861 if(ptn == 0) {
Ajay Dudani5c761132011-04-07 20:19:04 -07002862 fastboot_fail("partition table doesn't exist");
2863 return;
2864 }
2865
Channagoud Kadabi65b91002011-10-11 17:34:33 +05302866 size = partition_get_size(index);
Channagoud Kadabi65b91002011-10-11 17:34:33 +05302867
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002868 lun = partition_get_lun(index);
2869 mmc_set_lun(lun);
2870
vijay kumar800255e2015-04-24 20:26:19 +05302871 if (sz < sizeof(sparse_header_t)) {
2872 fastboot_fail("size too low");
2873 return;
2874 }
2875
Ajay Dudani5c761132011-04-07 20:19:04 -07002876 /* Read and skip over sparse image header */
2877 sparse_header = (sparse_header_t *) data;
vijay kumar800255e2015-04-24 20:26:19 +05302878
vijay kumar1321f342015-03-27 12:13:42 +05302879 if (((uint64_t)sparse_header->total_blks * (uint64_t)sparse_header->blk_sz) > size) {
Ajay Dudani876b3282012-12-21 14:12:17 -08002880 fastboot_fail("size too large");
2881 return;
2882 }
2883
vijay kumarde4fcf62015-04-23 13:05:49 +05302884 data += sizeof(sparse_header_t);
vijay kumar800255e2015-04-24 20:26:19 +05302885
Parth Dixit64b1a482016-03-07 16:31:26 +05302886 if (data_end < (uintptr_t)data) {
vijay kumar800255e2015-04-24 20:26:19 +05302887 fastboot_fail("buffer overreads occured due to invalid sparse header");
2888 return;
2889 }
2890
vijay kumarde4fcf62015-04-23 13:05:49 +05302891 if(sparse_header->file_hdr_sz != sizeof(sparse_header_t))
Ajay Dudani5c761132011-04-07 20:19:04 -07002892 {
vijay kumarde4fcf62015-04-23 13:05:49 +05302893 fastboot_fail("sparse header size mismatch");
2894 return;
Ajay Dudani5c761132011-04-07 20:19:04 -07002895 }
2896
Ajay Dudanib06c05f2011-05-12 14:46:10 -07002897 dprintf (SPEW, "=== Sparse Image Header ===\n");
2898 dprintf (SPEW, "magic: 0x%x\n", sparse_header->magic);
2899 dprintf (SPEW, "major_version: 0x%x\n", sparse_header->major_version);
2900 dprintf (SPEW, "minor_version: 0x%x\n", sparse_header->minor_version);
2901 dprintf (SPEW, "file_hdr_sz: %d\n", sparse_header->file_hdr_sz);
2902 dprintf (SPEW, "chunk_hdr_sz: %d\n", sparse_header->chunk_hdr_sz);
2903 dprintf (SPEW, "blk_sz: %d\n", sparse_header->blk_sz);
2904 dprintf (SPEW, "total_blks: %d\n", sparse_header->total_blks);
2905 dprintf (SPEW, "total_chunks: %d\n", sparse_header->total_chunks);
Ajay Dudani5c761132011-04-07 20:19:04 -07002906
2907 /* Start processing chunks */
2908 for (chunk=0; chunk<sparse_header->total_chunks; chunk++)
2909 {
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302910 /* Make sure the total image size does not exceed the partition size */
2911 if(((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz) >= size) {
2912 fastboot_fail("size too large");
2913 return;
2914 }
Ajay Dudani5c761132011-04-07 20:19:04 -07002915 /* Read and skip over chunk header */
2916 chunk_header = (chunk_header_t *) data;
2917 data += sizeof(chunk_header_t);
2918
Parth Dixit64b1a482016-03-07 16:31:26 +05302919 if (data_end < (uintptr_t)data) {
vijay kumar800255e2015-04-24 20:26:19 +05302920 fastboot_fail("buffer overreads occured due to invalid sparse header");
2921 return;
2922 }
2923
Ajay Dudani5c761132011-04-07 20:19:04 -07002924 dprintf (SPEW, "=== Chunk Header ===\n");
2925 dprintf (SPEW, "chunk_type: 0x%x\n", chunk_header->chunk_type);
2926 dprintf (SPEW, "chunk_data_sz: 0x%x\n", chunk_header->chunk_sz);
2927 dprintf (SPEW, "total_size: 0x%x\n", chunk_header->total_sz);
2928
vijay kumar800255e2015-04-24 20:26:19 +05302929 if(sparse_header->chunk_hdr_sz != sizeof(chunk_header_t))
Ajay Dudani5c761132011-04-07 20:19:04 -07002930 {
vijay kumar800255e2015-04-24 20:26:19 +05302931 fastboot_fail("chunk header size mismatch");
2932 return;
Ajay Dudani5c761132011-04-07 20:19:04 -07002933 }
2934
wufeng.jiang813dc352015-06-02 23:02:46 -04002935 if (!sparse_header->blk_sz ){
2936 fastboot_fail("Invalid block size\n");
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302937 return;
2938 }
2939
wufeng.jiang813dc352015-06-02 23:02:46 -04002940 chunk_data_sz = (uint64_t)sparse_header->blk_sz * chunk_header->chunk_sz;
2941
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302942 /* Make sure that the chunk size calculated from sparse image does not
2943 * exceed partition size
2944 */
2945 if ((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz + chunk_data_sz > size)
2946 {
2947 fastboot_fail("Chunk data size exceeds partition size");
2948 return;
2949 }
2950
Ajay Dudani5c761132011-04-07 20:19:04 -07002951 switch (chunk_header->chunk_type)
2952 {
2953 case CHUNK_TYPE_RAW:
wufeng.jiang813dc352015-06-02 23:02:46 -04002954 if((uint64_t)chunk_header->total_sz != ((uint64_t)sparse_header->chunk_hdr_sz +
Ajay Dudani5c761132011-04-07 20:19:04 -07002955 chunk_data_sz))
2956 {
2957 fastboot_fail("Bogus chunk size for chunk type Raw");
2958 return;
2959 }
2960
Parth Dixit64b1a482016-03-07 16:31:26 +05302961 if (data_end < (uintptr_t)data + chunk_data_sz) {
vijay kumar800255e2015-04-24 20:26:19 +05302962 fastboot_fail("buffer overreads occured due to invalid sparse header");
2963 return;
2964 }
2965
wufeng.jiang813dc352015-06-02 23:02:46 -04002966 /* chunk_header->total_sz is uint32,So chunk_data_sz is now less than 2^32
2967 otherwise it will return in the line above
2968 */
Ajay Dudaniab18f022011-05-12 14:39:22 -07002969 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
wufeng.jiang813dc352015-06-02 23:02:46 -04002970 (uint32_t)chunk_data_sz,
Ajay Dudaniab18f022011-05-12 14:39:22 -07002971 (unsigned int*)data))
Ajay Dudani5c761132011-04-07 20:19:04 -07002972 {
2973 fastboot_fail("flash write failure");
2974 return;
2975 }
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302976 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
2977 fastboot_fail("Bogus size for RAW chunk type");
2978 return;
2979 }
Ajay Dudani5c761132011-04-07 20:19:04 -07002980 total_blocks += chunk_header->chunk_sz;
wufeng.jiang813dc352015-06-02 23:02:46 -04002981 data += (uint32_t)chunk_data_sz;
Ajay Dudani5c761132011-04-07 20:19:04 -07002982 break;
2983
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002984 case CHUNK_TYPE_FILL:
2985 if(chunk_header->total_sz != (sparse_header->chunk_hdr_sz +
2986 sizeof(uint32_t)))
2987 {
2988 fastboot_fail("Bogus chunk size for chunk type FILL");
2989 return;
2990 }
2991
2992 fill_buf = (uint32_t *)memalign(CACHE_LINE, ROUNDUP(sparse_header->blk_sz, CACHE_LINE));
2993 if (!fill_buf)
2994 {
2995 fastboot_fail("Malloc failed for: CHUNK_TYPE_FILL");
2996 return;
2997 }
2998
Parth Dixit64b1a482016-03-07 16:31:26 +05302999 if (data_end < (uintptr_t)data + sizeof(uint32_t)) {
vijay kumar800255e2015-04-24 20:26:19 +05303000 fastboot_fail("buffer overreads occured due to invalid sparse header");
Gaurav Nebhwani163cbf82016-03-17 21:21:27 +05303001 free(fill_buf);
vijay kumar800255e2015-04-24 20:26:19 +05303002 return;
3003 }
Channagoud Kadabif627cf82013-09-09 14:08:20 -07003004 fill_val = *(uint32_t *)data;
3005 data = (char *) data + sizeof(uint32_t);
Channagoud Kadabif627cf82013-09-09 14:08:20 -07003006
3007 for (i = 0; i < (sparse_header->blk_sz / sizeof(fill_val)); i++)
3008 {
3009 fill_buf[i] = fill_val;
3010 }
3011
Gaurav Nebhwanie94cbe12016-03-17 21:16:34 +05303012 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz))
3013 {
3014 fastboot_fail("bogus size for chunk FILL type");
3015 free(fill_buf);
3016 return;
3017 }
3018
wufeng.jiang813dc352015-06-02 23:02:46 -04003019 for (i = 0; i < chunk_header->chunk_sz; i++)
Channagoud Kadabif627cf82013-09-09 14:08:20 -07003020 {
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05303021 /* Make sure that the data written to partition does not exceed partition size */
3022 if ((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz + sparse_header->blk_sz > size)
3023 {
3024 fastboot_fail("Chunk data size for fill type exceeds partition size");
Gaurav Nebhwani163cbf82016-03-17 21:21:27 +05303025 free(fill_buf);
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05303026 return;
3027 }
3028
Channagoud Kadabif627cf82013-09-09 14:08:20 -07003029 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
3030 sparse_header->blk_sz,
3031 fill_buf))
3032 {
3033 fastboot_fail("flash write failure");
3034 free(fill_buf);
3035 return;
3036 }
3037
3038 total_blocks++;
3039 }
3040
3041 free(fill_buf);
3042 break;
3043
Ajay Dudani5c761132011-04-07 20:19:04 -07003044 case CHUNK_TYPE_DONT_CARE:
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05303045 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
3046 fastboot_fail("bogus size for chunk DONT CARE type");
3047 return;
3048 }
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07003049 total_blocks += chunk_header->chunk_sz;
3050 break;
3051
Ajay Dudani5c761132011-04-07 20:19:04 -07003052 case CHUNK_TYPE_CRC:
3053 if(chunk_header->total_sz != sparse_header->chunk_hdr_sz)
3054 {
wufeng.jiang813dc352015-06-02 23:02:46 -04003055 fastboot_fail("Bogus chunk size for chunk type CRC");
Ajay Dudani5c761132011-04-07 20:19:04 -07003056 return;
3057 }
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05303058 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
3059 fastboot_fail("bogus size for chunk CRC type");
3060 return;
3061 }
Ajay Dudani5c761132011-04-07 20:19:04 -07003062 total_blocks += chunk_header->chunk_sz;
Parth Dixit64b1a482016-03-07 16:31:26 +05303063 if ((uintptr_t)data > UINT_MAX - chunk_data_sz) {
vijay kumar800255e2015-04-24 20:26:19 +05303064 fastboot_fail("integer overflow occured");
3065 return;
3066 }
wufeng.jiang813dc352015-06-02 23:02:46 -04003067 data += (uint32_t)chunk_data_sz;
Parth Dixit64b1a482016-03-07 16:31:26 +05303068 if (data_end < (uintptr_t)data) {
vijay kumar800255e2015-04-24 20:26:19 +05303069 fastboot_fail("buffer overreads occured due to invalid sparse header");
3070 return;
3071 }
Ajay Dudani5c761132011-04-07 20:19:04 -07003072 break;
3073
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07003074 default:
Channagoud Kadabif627cf82013-09-09 14:08:20 -07003075 dprintf(CRITICAL, "Unkown chunk type: %x\n",chunk_header->chunk_type);
Ajay Dudani5c761132011-04-07 20:19:04 -07003076 fastboot_fail("Unknown chunk type");
3077 return;
3078 }
3079 }
3080
Ajay Dudani0c6927b2011-05-18 11:12:16 -07003081 dprintf(INFO, "Wrote %d blocks, expected to write %d blocks\n",
3082 total_blocks, sparse_header->total_blks);
3083
3084 if(total_blocks != sparse_header->total_blks)
3085 {
3086 fastboot_fail("sparse image write failure");
3087 }
Ajay Dudani5c761132011-04-07 20:19:04 -07003088
3089 fastboot_okay("");
3090 return;
3091}
3092
3093void cmd_flash_mmc(const char *arg, void *data, unsigned sz)
3094{
3095 sparse_header_t *sparse_header;
Ajay Dudanide984792015-03-02 09:57:41 -08003096 meta_header_t *meta_header;
Ajay Dudani5c761132011-04-07 20:19:04 -07003097
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07003098#ifdef SSD_ENABLE
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -08003099 /* 8 Byte Magic + 2048 Byte xml + Encrypted Data */
3100 unsigned int *magic_number = (unsigned int *) data;
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08003101 int ret=0;
3102 uint32 major_version=0;
3103 uint32 minor_version=0;
3104
3105 ret = scm_svc_version(&major_version,&minor_version);
3106 if(!ret)
3107 {
3108 if(major_version >= 2)
3109 {
Amir Samuelovbb65ce02013-05-05 12:20:18 +03003110 if( !strcmp(arg, "ssd") || !strcmp(arg, "tqs") )
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08003111 {
3112 ret = encrypt_scm((uint32 **) &data, &sz);
3113 if (ret != 0) {
3114 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
3115 return;
3116 }
3117
Amir Samuelovbb65ce02013-05-05 12:20:18 +03003118 /* Protect only for SSD */
3119 if (!strcmp(arg, "ssd")) {
3120 ret = scm_protect_keystore((uint32 *) data, sz);
3121 if (ret != 0) {
3122 dprintf(CRITICAL, "ERROR: scm_protect_keystore Failed\n");
3123 return;
3124 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08003125 }
3126 }
3127 else
3128 {
3129 ret = decrypt_scm_v2((uint32 **) &data, &sz);
3130 if(ret != 0)
3131 {
3132 dprintf(CRITICAL,"ERROR: Decryption Failure\n");
3133 return;
3134 }
3135 }
3136 }
3137 else
3138 {
3139 if (magic_number[0] == DECRYPT_MAGIC_0 &&
3140 magic_number[1] == DECRYPT_MAGIC_1)
3141 {
3142 ret = decrypt_scm((uint32 **) &data, &sz);
3143 if (ret != 0) {
3144 dprintf(CRITICAL, "ERROR: Invalid secure image\n");
3145 return;
3146 }
3147 }
3148 else if (magic_number[0] == ENCRYPT_MAGIC_0 &&
3149 magic_number[1] == ENCRYPT_MAGIC_1)
3150 {
3151 ret = encrypt_scm((uint32 **) &data, &sz);
3152 if (ret != 0) {
3153 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
3154 return;
3155 }
3156 }
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07003157 }
3158 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08003159 else
Neeti Desai127b9e02012-03-20 16:11:23 -07003160 {
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08003161 dprintf(CRITICAL,"INVALID SVC Version\n");
3162 return;
Neeti Desai127b9e02012-03-20 16:11:23 -07003163 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08003164#endif /* SSD_ENABLE */
Neeti Desai127b9e02012-03-20 16:11:23 -07003165
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003166#if VERIFIED_BOOT
Channagoud Kadabi35297672015-06-13 11:09:49 -07003167 if (target_build_variant_user())
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003168 {
lijuang511a2b52015-08-14 20:50:51 +08003169 /* if device is locked:
3170 * common partition will not allow to be flashed
3171 * critical partition will allow to flash image.
3172 */
3173 if(!device.is_unlocked && !critical_flash_allowed(arg)) {
3174 fastboot_fail("Partition flashing is not allowed");
3175 return;
3176 }
Parth Dixitddbc7352015-10-18 03:13:31 +05303177#if !VBOOT_MOTA
lijuang511a2b52015-08-14 20:50:51 +08003178 /* if device critical is locked:
3179 * common partition will allow to be flashed
3180 * critical partition will not allow to flash image.
3181 */
3182 if(!device.is_unlock_critical && critical_flash_allowed(arg)) {
3183 fastboot_fail("Critical partition flashing is not allowed");
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003184 return;
3185 }
Parth Dixitddbc7352015-10-18 03:13:31 +05303186#endif
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003187 }
3188#endif
3189
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07003190 sparse_header = (sparse_header_t *) data;
Ajay Dudanide984792015-03-02 09:57:41 -08003191 meta_header = (meta_header_t *) data;
3192 if (sparse_header->magic == SPARSE_HEADER_MAGIC)
Ajay Dudani5c761132011-04-07 20:19:04 -07003193 cmd_flash_mmc_sparse_img(arg, data, sz);
Ajay Dudanide984792015-03-02 09:57:41 -08003194 else if (meta_header->magic == META_HEADER_MAGIC)
3195 cmd_flash_meta_img(arg, data, sz);
3196 else
3197 cmd_flash_mmc_img(arg, data, sz);
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07003198
3199#if VERIFIED_BOOT
Parth Dixitddbc7352015-10-18 03:13:31 +05303200#if !VBOOT_MOTA
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07003201 if((!strncmp(arg, "system", 6)) && !device.verity_mode)
3202 {
3203 // reset dm_verity mode to enforcing
3204 device.verity_mode = 1;
3205 write_device_info(&device);
3206 }
3207#endif
Parth Dixitddbc7352015-10-18 03:13:31 +05303208#endif
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07003209
Ajay Dudani5c761132011-04-07 20:19:04 -07003210 return;
3211}
3212
Tanya Brokhman84eaaf62015-02-24 21:57:06 +02003213void cmd_updatevol(const char *vol_name, void *data, unsigned sz)
3214{
3215 struct ptentry *sys_ptn;
3216 struct ptable *ptable;
3217
3218 ptable = flash_get_ptable();
3219 if (ptable == NULL) {
3220 fastboot_fail("partition table doesn't exist");
3221 return;
3222 }
3223
3224 sys_ptn = ptable_find(ptable, "system");
3225 if (sys_ptn == NULL) {
3226 fastboot_fail("system partition not found");
3227 return;
3228 }
3229
3230 sz = ROUND_TO_PAGE(sz, page_mask);
3231 if (update_ubi_vol(sys_ptn, vol_name, data, sz))
3232 fastboot_fail("update_ubi_vol failed");
3233 else
3234 fastboot_okay("");
3235}
3236
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003237void cmd_flash_nand(const char *arg, void *data, unsigned sz)
Dima Zavin214cc642009-01-26 11:16:21 -08003238{
3239 struct ptentry *ptn;
3240 struct ptable *ptable;
3241 unsigned extra = 0;
Gaurav Nebhwanid9730712016-05-06 14:28:29 +05303242 uint64_t partition_size = 0;
Dima Zavin214cc642009-01-26 11:16:21 -08003243
3244 ptable = flash_get_ptable();
3245 if (ptable == NULL) {
3246 fastboot_fail("partition table doesn't exist");
3247 return;
3248 }
3249
3250 ptn = ptable_find(ptable, arg);
3251 if (ptn == NULL) {
Tanya Brokhman84eaaf62015-02-24 21:57:06 +02003252 dprintf(INFO, "unknown partition name (%s). Trying updatevol\n",
3253 arg);
3254 cmd_updatevol(arg, data, sz);
Dima Zavin214cc642009-01-26 11:16:21 -08003255 return;
3256 }
3257
3258 if (!strcmp(ptn->name, "boot") || !strcmp(ptn->name, "recovery")) {
3259 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
3260 fastboot_fail("image is not a boot image");
3261 return;
3262 }
3263 }
3264
Amol Jadi5c61a952012-05-04 17:05:35 -07003265 if (!strcmp(ptn->name, "system")
Deepa Dinamani13e32c42012-03-12 14:34:17 -07003266 || !strcmp(ptn->name, "userdata")
3267 || !strcmp(ptn->name, "persist")
Sundarajan Srinivasanb063a852013-11-19 14:02:27 -08003268 || !strcmp(ptn->name, "recoveryfs")
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -08003269 || !strcmp(ptn->name, "modem"))
Tanya Brokhman1c94f1a2015-02-15 09:05:03 +02003270 extra = 1;
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -08003271 else
Shashank Mittaldcc2e352009-11-19 19:11:16 -08003272 sz = ROUND_TO_PAGE(sz, page_mask);
Dima Zavin214cc642009-01-26 11:16:21 -08003273
Gaurav Nebhwanid9730712016-05-06 14:28:29 +05303274 partition_size = (uint64_t)ptn->length * (uint64_t)flash_num_pages_per_blk() * (uint64_t)flash_page_size();
3275 if (partition_size > UINT_MAX) {
3276 fastboot_fail("Invalid partition size");
3277 return;
3278 }
3279
3280 if (sz > partition_size) {
3281 fastboot_fail("Image size too large");
3282 return;
3283 }
3284
Dima Zavin214cc642009-01-26 11:16:21 -08003285 dprintf(INFO, "writing %d bytes to '%s'\n", sz, ptn->name);
Tanya Brokhman1c94f1a2015-02-15 09:05:03 +02003286 if (!memcmp((void *)data, UBI_MAGIC, UBI_MAGIC_SIZE)) {
3287 if (flash_ubi_img(ptn, data, sz)) {
3288 fastboot_fail("flash write failure");
3289 return;
3290 }
3291 } else {
3292 if (flash_write(ptn, extra, data, sz)) {
3293 fastboot_fail("flash write failure");
3294 return;
3295 }
Dima Zavin214cc642009-01-26 11:16:21 -08003296 }
3297 dprintf(INFO, "partition '%s' updated\n", ptn->name);
3298 fastboot_okay("");
3299}
3300
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003301void cmd_flash(const char *arg, void *data, unsigned sz)
3302{
3303 if(target_is_emmc_boot())
3304 cmd_flash_mmc(arg, data, sz);
3305 else
3306 cmd_flash_nand(arg, data, sz);
3307}
3308
Dima Zavin214cc642009-01-26 11:16:21 -08003309void cmd_continue(const char *arg, void *data, unsigned sz)
3310{
3311 fastboot_okay("");
Amol Jadi7c4316c2013-10-07 14:19:26 -07003312 fastboot_stop();
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003313
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003314 if (target_is_emmc_boot())
3315 {
lijuanga40d6302015-07-20 20:10:13 +08003316#if FBCON_DISPLAY_MSG
lijuangde34d502016-02-26 16:04:50 +08003317 /* Exit keys' detection thread firstly */
3318 exit_menu_keys_detection();
lijuanga40d6302015-07-20 20:10:13 +08003319#endif
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003320 boot_linux_from_mmc();
3321 }
3322 else
3323 {
3324 boot_linux_from_flash();
3325 }
Dima Zavin214cc642009-01-26 11:16:21 -08003326}
3327
Chandan Uddaraju94183c02010-01-15 15:13:59 -08003328void cmd_reboot(const char *arg, void *data, unsigned sz)
3329{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003330 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08003331 fastboot_okay("");
3332 reboot_device(0);
3333}
3334
3335void cmd_reboot_bootloader(const char *arg, void *data, unsigned sz)
3336{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003337 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08003338 fastboot_okay("");
3339 reboot_device(FASTBOOT_MODE);
3340}
3341
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003342void cmd_oem_enable_charger_screen(const char *arg, void *data, unsigned size)
3343{
3344 dprintf(INFO, "Enabling charger screen check\n");
3345 device.charger_screen_enabled = 1;
3346 write_device_info(&device);
3347 fastboot_okay("");
3348}
3349
3350void cmd_oem_disable_charger_screen(const char *arg, void *data, unsigned size)
3351{
3352 dprintf(INFO, "Disabling charger screen check\n");
3353 device.charger_screen_enabled = 0;
3354 write_device_info(&device);
3355 fastboot_okay("");
3356}
3357
lijuanga25d8bb2015-09-16 13:11:52 +08003358void cmd_oem_off_mode_charger(const char *arg, void *data, unsigned size)
3359{
3360 char *p = NULL;
3361 const char *delim = " \t\n\r";
Parth Dixit407f15b2016-01-07 14:47:37 +05303362 char *sp;
lijuanga25d8bb2015-09-16 13:11:52 +08003363
3364 if (arg) {
Parth Dixit407f15b2016-01-07 14:47:37 +05303365 p = strtok_r((char *)arg, delim, &sp);
lijuanga25d8bb2015-09-16 13:11:52 +08003366 if (p) {
3367 if (!strncmp(p, "0", 1)) {
3368 device.charger_screen_enabled = 0;
3369 } else if (!strncmp(p, "1", 1)) {
3370 device.charger_screen_enabled = 1;
3371 }
3372 }
3373 }
3374
3375 /* update charger_screen_enabled value for getvar
3376 * command
3377 */
3378 snprintf(charger_screen_enabled, MAX_RSP_SIZE, "%d",
3379 device.charger_screen_enabled);
3380
3381 write_device_info(&device);
3382 fastboot_okay("");
3383}
3384
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05303385void cmd_oem_select_display_panel(const char *arg, void *data, unsigned size)
3386{
3387 dprintf(INFO, "Selecting display panel %s\n", arg);
3388 if (arg)
3389 strlcpy(device.display_panel, arg,
3390 sizeof(device.display_panel));
3391 write_device_info(&device);
3392 fastboot_okay("");
3393}
3394
Shashank Mittal162244e2011-08-08 19:01:25 -07003395void cmd_oem_unlock(const char *arg, void *data, unsigned sz)
3396{
lijuang4ece1e72015-08-14 21:02:36 +08003397 set_device_unlock(UNLOCK, TRUE);
vijay kumarc65876c2015-04-24 13:29:16 +05303398}
3399
3400void cmd_oem_unlock_go(const char *arg, void *data, unsigned sz)
3401{
lijuang4ece1e72015-08-14 21:02:36 +08003402 if(!device.is_unlocked) {
vijay kumarc65876c2015-04-24 13:29:16 +05303403 if(!is_allow_unlock) {
3404 fastboot_fail("oem unlock is not allowed");
3405 return;
3406 }
3407
lijuang4ece1e72015-08-14 21:02:36 +08003408 set_device_unlock_value(UNLOCK, TRUE);
vijay kumarc65876c2015-04-24 13:29:16 +05303409
lijuang4ece1e72015-08-14 21:02:36 +08003410 /* wipe data */
vijay kumarc65876c2015-04-24 13:29:16 +05303411 struct recovery_message msg;
lijuang4ece1e72015-08-14 21:02:36 +08003412
vijay kumarc65876c2015-04-24 13:29:16 +05303413 snprintf(msg.recovery, sizeof(msg.recovery), "recovery\n--wipe_data");
3414 write_misc(0, &msg, sizeof(msg));
3415
3416 fastboot_okay("");
3417 reboot_device(RECOVERY_MODE);
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003418 }
3419 fastboot_okay("");
3420}
3421
Channagoud Kadabiad259832015-05-29 11:14:17 -07003422static int aboot_frp_unlock(char *pname, void *data, unsigned sz)
3423{
3424 int ret = 1;
3425 uint32_t secret_key;
3426 char seckey_buffer[MAX_RSP_SIZE];
3427
3428 secret_key = aboot_get_secret_key();
3429 if (secret_key)
3430 {
3431 snprintf((char *) seckey_buffer, MAX_RSP_SIZE, "%x", secret_key);
3432 if (!memcmp((void *)data, (void *)seckey_buffer, sz))
3433 {
3434 is_allow_unlock = true;
3435 write_allow_oem_unlock(is_allow_unlock);
3436 ret = 0;
3437 }
3438 }
3439 return ret;
3440}
3441
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003442void cmd_oem_lock(const char *arg, void *data, unsigned sz)
3443{
lijuang4ece1e72015-08-14 21:02:36 +08003444 set_device_unlock(UNLOCK, FALSE);
Shashank Mittal162244e2011-08-08 19:01:25 -07003445}
3446
Shashank Mittala0032282011-08-26 14:50:11 -07003447void cmd_oem_devinfo(const char *arg, void *data, unsigned sz)
3448{
lijuang511a2b52015-08-14 20:50:51 +08003449 char response[MAX_RSP_SIZE];
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003450 snprintf(response, sizeof(response), "\tDevice tampered: %s", (device.is_tampered ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07003451 fastboot_info(response);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003452 snprintf(response, sizeof(response), "\tDevice unlocked: %s", (device.is_unlocked ? "true" : "false"));
3453 fastboot_info(response);
Parth Dixitddbc7352015-10-18 03:13:31 +05303454#if !VBOOT_MOTA
lijuang511a2b52015-08-14 20:50:51 +08003455 snprintf(response, sizeof(response), "\tDevice critical unlocked: %s", (device.is_unlock_critical ? "true" : "false"));
3456 fastboot_info(response);
Parth Dixitddbc7352015-10-18 03:13:31 +05303457#endif
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003458 snprintf(response, sizeof(response), "\tCharger screen enabled: %s", (device.charger_screen_enabled ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07003459 fastboot_info(response);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05303460 snprintf(response, sizeof(response), "\tDisplay panel: %s", (device.display_panel));
3461 fastboot_info(response);
Shashank Mittala0032282011-08-26 14:50:11 -07003462 fastboot_okay("");
3463}
3464
lijuang511a2b52015-08-14 20:50:51 +08003465void cmd_flashing_get_unlock_ability(const char *arg, void *data, unsigned sz)
3466{
3467 char response[MAX_RSP_SIZE];
3468 snprintf(response, sizeof(response), "\tget_unlock_ability: %d", is_allow_unlock);
3469 fastboot_info(response);
3470 fastboot_okay("");
3471}
3472
3473void cmd_flashing_lock_critical(const char *arg, void *data, unsigned sz)
3474{
lijuang4ece1e72015-08-14 21:02:36 +08003475 set_device_unlock(UNLOCK_CRITICAL, FALSE);
lijuang511a2b52015-08-14 20:50:51 +08003476}
3477
3478void cmd_flashing_unlock_critical(const char *arg, void *data, unsigned sz)
3479{
lijuang4ece1e72015-08-14 21:02:36 +08003480 set_device_unlock(UNLOCK_CRITICAL, TRUE);
lijuang511a2b52015-08-14 20:50:51 +08003481}
3482
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07003483void cmd_preflash(const char *arg, void *data, unsigned sz)
3484{
3485 fastboot_okay("");
3486}
3487
Mao Flynn7b379f32015-04-20 00:28:30 +08003488static uint8_t logo_header[LOGO_IMG_HEADER_SIZE];
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303489
Mao Flynn7b379f32015-04-20 00:28:30 +08003490int splash_screen_check_header(logo_img_header *header)
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303491{
Mao Flynn7b379f32015-04-20 00:28:30 +08003492 if (memcmp(header->magic, LOGO_IMG_MAGIC, 8))
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303493 return -1;
Mao Flynn7b379f32015-04-20 00:28:30 +08003494 if (header->width == 0 || header->height == 0)
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303495 return -1;
3496 return 0;
3497}
3498
Mao Flynn7b379f32015-04-20 00:28:30 +08003499int splash_screen_flash()
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003500{
3501 struct ptentry *ptn;
3502 struct ptable *ptable;
Mao Flynn7b379f32015-04-20 00:28:30 +08003503 struct logo_img_header *header;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003504 struct fbcon_config *fb_display = NULL;
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08003505
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303506 ptable = flash_get_ptable();
3507 if (ptable == NULL) {
Mao Flynn7b379f32015-04-20 00:28:30 +08003508 dprintf(CRITICAL, "ERROR: Partition table not found\n");
3509 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303510 }
Mao Flynn7b379f32015-04-20 00:28:30 +08003511
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303512 ptn = ptable_find(ptable, "splash");
3513 if (ptn == NULL) {
3514 dprintf(CRITICAL, "ERROR: splash Partition not found\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003515 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303516 }
Mao Flynn7b379f32015-04-20 00:28:30 +08003517 if (flash_read(ptn, 0, (void *)logo_header, LOGO_IMG_HEADER_SIZE)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303518 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003519 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303520 }
3521
Mao Flynn7b379f32015-04-20 00:28:30 +08003522 header = (struct logo_img_header *)logo_header;
3523 if (splash_screen_check_header(header)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303524 dprintf(CRITICAL, "ERROR: Boot image header invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003525 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303526 }
3527
3528 fb_display = fbcon_display();
3529 if (fb_display) {
Mao Flynn7b379f32015-04-20 00:28:30 +08003530 if (header->type && (header->blocks != 0)) { // RLE24 compressed data
3531 uint8_t *base = (uint8_t *) fb_display->base + LOGO_IMG_OFFSET;
3532
3533 /* if the logo is full-screen size, remove "fbcon_clear()" */
3534 if ((header->width != fb_display->width)
3535 || (header->height != fb_display->height))
3536 fbcon_clear();
3537
3538 if (flash_read(ptn + LOGO_IMG_HEADER_SIZE, 0,
3539 (uint32_t *)base,
3540 (header->blocks * 512))) {
3541 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
3542 return -1;
3543 }
3544 fbcon_extract_to_screen(header, base);
3545 return 0;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003546 }
Mao Flynn7b379f32015-04-20 00:28:30 +08003547
Veera Sundaram Sankaranc33416f2015-09-28 10:25:55 -07003548 if ((header->width > fb_display->width) || (header->height > fb_display->height)) {
3549 dprintf(CRITICAL, "Logo config greater than fb config. Fall back default logo\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003550 return -1;
3551 }
3552
Vineet Bajaj99291ed2014-09-09 12:29:46 +05303553 uint8_t *base = (uint8_t *) fb_display->base;
Mao Flynn7b379f32015-04-20 00:28:30 +08003554 if (flash_read(ptn + LOGO_IMG_HEADER_SIZE, 0,
3555 (uint32_t *)base,
3556 ((((header->width * header->height * fb_display->bpp/8) + 511) >> 9) << 9))) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303557 fbcon_clear();
Vineet Bajaj99291ed2014-09-09 12:29:46 +05303558 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003559 return -1;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003560 }
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303561 }
3562
Mao Flynn7b379f32015-04-20 00:28:30 +08003563 return 0;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303564}
3565
Mao Flynn7b379f32015-04-20 00:28:30 +08003566int splash_screen_mmc()
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303567{
3568 int index = INVALID_PTN;
3569 unsigned long long ptn = 0;
3570 struct fbcon_config *fb_display = NULL;
Mao Flynn7b379f32015-04-20 00:28:30 +08003571 struct logo_img_header *header;
Mao Flynn1893a7f2015-06-03 12:03:36 +08003572 uint32_t blocksize, realsize, readsize;
3573 uint8_t *base;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303574
3575 index = partition_get_index("splash");
3576 if (index == 0) {
3577 dprintf(CRITICAL, "ERROR: splash Partition table not found\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003578 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303579 }
3580
3581 ptn = partition_get_offset(index);
3582 if (ptn == 0) {
3583 dprintf(CRITICAL, "ERROR: splash Partition invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003584 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303585 }
3586
Mao Flynn1893a7f2015-06-03 12:03:36 +08003587 mmc_set_lun(partition_get_lun(index));
3588
3589 blocksize = mmc_get_device_blocksize();
3590 if (blocksize == 0) {
3591 dprintf(CRITICAL, "ERROR:splash Partition invalid blocksize\n");
3592 return -1;
3593 }
3594
3595 fb_display = fbcon_display();
Channagoud Kadabidaf10a62015-07-22 11:51:55 -07003596 if (!fb_display)
3597 {
3598 dprintf(CRITICAL, "ERROR: fb config is not allocated\n");
3599 return -1;
3600 }
3601
Mao Flynn1893a7f2015-06-03 12:03:36 +08003602 base = (uint8_t *) fb_display->base;
3603
3604 if (mmc_read(ptn, (uint32_t *)(base + LOGO_IMG_OFFSET), blocksize)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303605 dprintf(CRITICAL, "ERROR: Cannot read splash image header\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003606 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303607 }
3608
Mao Flynn1893a7f2015-06-03 12:03:36 +08003609 header = (struct logo_img_header *)(base + LOGO_IMG_OFFSET);
Mao Flynn7b379f32015-04-20 00:28:30 +08003610 if (splash_screen_check_header(header)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303611 dprintf(CRITICAL, "ERROR: Splash image header invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003612 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303613 }
3614
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303615 if (fb_display) {
Mao Flynn1893a7f2015-06-03 12:03:36 +08003616 if (header->type && (header->blocks != 0)) { /* 1 RLE24 compressed data */
3617 base += LOGO_IMG_OFFSET;
Mao Flynn7b379f32015-04-20 00:28:30 +08003618
Mao Flynn1893a7f2015-06-03 12:03:36 +08003619 realsize = header->blocks * 512;
3620 readsize = ROUNDUP((realsize + LOGO_IMG_HEADER_SIZE), blocksize) - blocksize;
3621
3622 /* if the logo is not full-screen size, clean screen */
Mao Flynn7b379f32015-04-20 00:28:30 +08003623 if ((header->width != fb_display->width)
3624 || (header->height != fb_display->height))
3625 fbcon_clear();
3626
Mao Flynn1893a7f2015-06-03 12:03:36 +08003627 if (mmc_read(ptn + blocksize, (uint32_t *)(base + blocksize), readsize)) {
Mao Flynn7b379f32015-04-20 00:28:30 +08003628 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
3629 return -1;
3630 }
Mao Flynn7b379f32015-04-20 00:28:30 +08003631
Mao Flynn1893a7f2015-06-03 12:03:36 +08003632 fbcon_extract_to_screen(header, (base + LOGO_IMG_HEADER_SIZE));
3633 } else { /* 2 Raw BGR data */
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303634
Veera Sundaram Sankaranc33416f2015-09-28 10:25:55 -07003635 if ((header->width > fb_display->width) || (header->height > fb_display->height)) {
3636 dprintf(CRITICAL, "Logo config greater than fb config. Fall back default logo\n");
Mao Flynn1893a7f2015-06-03 12:03:36 +08003637 return -1;
3638 }
3639
3640 realsize = header->width * header->height * fb_display->bpp / 8;
3641 readsize = ROUNDUP((realsize + LOGO_IMG_HEADER_SIZE), blocksize) - blocksize;
3642
3643 if (blocksize == LOGO_IMG_HEADER_SIZE) { /* read the content directly */
3644 if (mmc_read((ptn + LOGO_IMG_HEADER_SIZE), (uint32_t *)base, readsize)) {
3645 fbcon_clear();
3646 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
3647 return -1;
3648 }
3649 } else {
3650 if (mmc_read(ptn + blocksize ,
3651 (uint32_t *)(base + LOGO_IMG_OFFSET + blocksize), readsize)) {
3652 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
3653 return -1;
3654 }
3655 memmove(base, (base + LOGO_IMG_OFFSET + LOGO_IMG_HEADER_SIZE), realsize);
3656 }
3657 }
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303658 }
3659
Mao Flynn7b379f32015-04-20 00:28:30 +08003660 return 0;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303661}
3662
Mao Flynn7b379f32015-04-20 00:28:30 +08003663int fetch_image_from_partition()
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303664{
3665 if (target_is_emmc_boot()) {
3666 return splash_screen_mmc();
3667 } else {
3668 return splash_screen_flash();
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003669 }
3670}
3671
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07003672/* Get the size from partiton name */
3673static void get_partition_size(const char *arg, char *response)
3674{
3675 uint64_t ptn = 0;
3676 uint64_t size;
3677 int index = INVALID_PTN;
3678
3679 index = partition_get_index(arg);
3680
3681 if (index == INVALID_PTN)
3682 {
3683 dprintf(CRITICAL, "Invalid partition index\n");
3684 return;
3685 }
3686
3687 ptn = partition_get_offset(index);
3688
3689 if(!ptn)
3690 {
3691 dprintf(CRITICAL, "Invalid partition name %s\n", arg);
3692 return;
3693 }
3694
3695 size = partition_get_size(index);
3696
3697 snprintf(response, MAX_RSP_SIZE, "\t 0x%llx", size);
3698 return;
3699}
3700
3701/*
3702 * Publish the partition type & size info
3703 * fastboot getvar will publish the required information.
3704 * fastboot getvar partition_size:<partition_name>: partition size in hex
3705 * fastboot getvar partition_type:<partition_name>: partition type (ext/fat)
3706 */
3707static void publish_getvar_partition_info(struct getvar_partition_info *info, uint8_t num_parts)
3708{
3709 uint8_t i;
3710
3711 for (i = 0; i < num_parts; i++) {
3712 get_partition_size(info[i].part_name, info[i].size_response);
3713
3714 if (strlcat(info[i].getvar_size, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
3715 {
3716 dprintf(CRITICAL, "partition size name truncated\n");
3717 return;
3718 }
3719 if (strlcat(info[i].getvar_type, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
3720 {
3721 dprintf(CRITICAL, "partition type name truncated\n");
3722 return;
3723 }
3724
3725 /* publish partition size & type info */
3726 fastboot_publish((const char *) info[i].getvar_size, (const char *) info[i].size_response);
3727 fastboot_publish((const char *) info[i].getvar_type, (const char *) info[i].type_response);
3728 }
3729}
3730
lijuang4ece1e72015-08-14 21:02:36 +08003731void get_product_name(unsigned char *buf)
3732{
3733 snprintf((char*)buf, MAX_RSP_SIZE, "%s", TARGET(BOARD));
3734 return;
3735}
3736
3737void get_bootloader_version(unsigned char *buf)
3738{
3739 snprintf((char*)buf, MAX_RSP_SIZE, "%s", device.bootloader_version);
3740 return;
3741}
3742
3743void get_baseband_version(unsigned char *buf)
3744{
3745 snprintf((char*)buf, MAX_RSP_SIZE, "%s", device.radio_version);
3746 return;
3747}
3748
3749bool is_device_locked()
3750{
3751 return device.is_unlocked ? false:true;
3752}
3753
Amol Jadi5edf3552013-07-23 14:15:34 -07003754/* register commands and variables for fastboot */
3755void aboot_fastboot_register_commands(void)
3756{
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003757 int i;
lijuangf16461c2015-08-03 17:09:34 +08003758 char hw_platform_buf[MAX_RSP_SIZE];
Amol Jadi5edf3552013-07-23 14:15:34 -07003759
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003760 struct fastboot_cmd_desc cmd_list[] = {
lijuang511a2b52015-08-14 20:50:51 +08003761 /* By default the enabled list is empty. */
3762 {"", NULL},
3763 /* move commands enclosed within the below ifndef to here
3764 * if they need to be enabled in user build.
3765 */
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003766#ifndef DISABLE_FASTBOOT_CMDS
lijuang511a2b52015-08-14 20:50:51 +08003767 /* Register the following commands only for non-user builds */
3768 {"flash:", cmd_flash},
3769 {"erase:", cmd_erase},
3770 {"boot", cmd_boot},
3771 {"continue", cmd_continue},
3772 {"reboot", cmd_reboot},
3773 {"reboot-bootloader", cmd_reboot_bootloader},
3774 {"oem unlock", cmd_oem_unlock},
3775 {"oem unlock-go", cmd_oem_unlock_go},
3776 {"oem lock", cmd_oem_lock},
3777 {"flashing unlock", cmd_oem_unlock},
3778 {"flashing lock", cmd_oem_lock},
3779 {"flashing lock_critical", cmd_flashing_lock_critical},
3780 {"flashing unlock_critical", cmd_flashing_unlock_critical},
3781 {"flashing get_unlock_ability", cmd_flashing_get_unlock_ability},
3782 {"oem device-info", cmd_oem_devinfo},
3783 {"preflash", cmd_preflash},
3784 {"oem enable-charger-screen", cmd_oem_enable_charger_screen},
3785 {"oem disable-charger-screen", cmd_oem_disable_charger_screen},
lijuanga25d8bb2015-09-16 13:11:52 +08003786 {"oem off-mode-charge", cmd_oem_off_mode_charger},
lijuang511a2b52015-08-14 20:50:51 +08003787 {"oem select-display-panel", cmd_oem_select_display_panel},
Channagoud Kadabi736c4962015-08-21 11:56:52 -07003788#if UNITTEST_FW_SUPPORT
Channagoud Kadabid5ddf482015-09-28 10:31:35 -07003789 {"oem run-tests", cmd_oem_runtests},
Channagoud Kadabi736c4962015-08-21 11:56:52 -07003790#endif
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003791#endif
lijuang511a2b52015-08-14 20:50:51 +08003792 };
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003793
3794 int fastboot_cmds_count = sizeof(cmd_list)/sizeof(cmd_list[0]);
3795 for (i = 1; i < fastboot_cmds_count; i++)
3796 fastboot_register(cmd_list[i].name,cmd_list[i].cb);
3797
Amol Jadi5edf3552013-07-23 14:15:34 -07003798 /* publish variables and their values */
3799 fastboot_publish("product", TARGET(BOARD));
3800 fastboot_publish("kernel", "lk");
3801 fastboot_publish("serialno", sn_buf);
3802
3803 /*
3804 * partition info is supported only for emmc partitions
3805 * Calling this for NAND prints some error messages which
3806 * is harmless but misleading. Avoid calling this for NAND
3807 * devices.
3808 */
3809 if (target_is_emmc_boot())
3810 publish_getvar_partition_info(part_info, ARRAY_SIZE(part_info));
3811
3812 /* Max download size supported */
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003813 snprintf(max_download_size, MAX_RSP_SIZE, "\t0x%x",
3814 target_get_max_flash_size());
Amol Jadi5edf3552013-07-23 14:15:34 -07003815 fastboot_publish("max-download-size", (const char *) max_download_size);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003816 /* Is the charger screen check enabled */
3817 snprintf(charger_screen_enabled, MAX_RSP_SIZE, "%d",
3818 device.charger_screen_enabled);
3819 fastboot_publish("charger-screen-enabled",
3820 (const char *) charger_screen_enabled);
lijuanga25d8bb2015-09-16 13:11:52 +08003821 fastboot_publish("off-mode-charge", (const char *) charger_screen_enabled);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05303822 snprintf(panel_display_mode, MAX_RSP_SIZE, "%s",
3823 device.display_panel);
3824 fastboot_publish("display-panel",
3825 (const char *) panel_display_mode);
Channagoud Kadabiaafbbfc2015-03-04 17:52:56 -08003826 fastboot_publish("version-bootloader", (const char *) device.bootloader_version);
3827 fastboot_publish("version-baseband", (const char *) device.radio_version);
lijuangf16461c2015-08-03 17:09:34 +08003828 fastboot_publish("secure", is_secure_boot_enable()? "yes":"no");
3829 smem_get_hw_platform_name((unsigned char *) hw_platform_buf, sizeof(hw_platform_buf));
3830 snprintf(get_variant, MAX_RSP_SIZE, "%s %s", hw_platform_buf,
3831 target_is_emmc_boot()? "eMMC":"UFS");
3832 fastboot_publish("variant", (const char *) get_variant);
lijuang65c5a822015-08-29 16:35:36 +08003833#if CHECK_BAT_VOLTAGE
lijuang102dfa92015-10-09 18:31:03 +08003834 update_battery_status();
lijuang65c5a822015-08-29 16:35:36 +08003835 fastboot_publish("battery-voltage", (const char *) battery_voltage);
lijuang102dfa92015-10-09 18:31:03 +08003836 fastboot_publish("battery-soc-ok", (const char *) battery_soc_ok);
lijuang65c5a822015-08-29 16:35:36 +08003837#endif
Amol Jadi5edf3552013-07-23 14:15:34 -07003838}
3839
Brian Swetland9c4c0752009-01-25 16:23:50 -08003840void aboot_init(const struct app_descriptor *app)
3841{
Shashank Mittal4f99a882010-02-01 13:58:50 -08003842 unsigned reboot_mode = 0;
Chandan Uddarajubedca152010-06-02 23:05:15 -07003843
lijuang39831732016-01-08 17:49:02 +08003844 /* Initialise wdog to catch early lk crashes */
3845#if WDOG_SUPPORT
3846 msm_wdog_init();
3847#endif
3848
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07003849 /* Setup page size information for nv storage */
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003850 if (target_is_emmc_boot())
3851 {
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07003852 page_size = mmc_page_size();
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003853 page_mask = page_size - 1;
Gaurav Nebhwani4d2389c2016-03-17 21:10:05 +05303854 mmc_blocksize = mmc_get_device_blocksize();
3855 mmc_blocksize_mask = mmc_blocksize - 1;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003856 }
3857 else
3858 {
3859 page_size = flash_page_size();
3860 page_mask = page_size - 1;
3861 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07003862 ASSERT((MEMBASE + MEMSIZE) > MEMBASE);
3863
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003864 read_device_info(&device);
vijay kumarc65876c2015-04-24 13:29:16 +05303865 read_allow_oem_unlock(&device);
Shashank Mittal162244e2011-08-08 19:01:25 -07003866
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08003867 /* Display splash screen if enabled */
3868#if DISPLAY_SPLASH_SCREEN
lijuang99c02d82015-02-13 19:04:34 +08003869#if NO_ALARM_DISPLAY
3870 if (!check_alarm_boot()) {
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08003871#endif
lijuang99c02d82015-02-13 19:04:34 +08003872 dprintf(SPEW, "Display Init: Start\n");
Ajay Singh Parmara7865a82015-04-16 21:27:52 -07003873#if DISPLAY_HDMI_PRIMARY
3874 if (!strlen(device.display_panel))
3875 strlcpy(device.display_panel, DISPLAY_PANEL_HDMI,
3876 sizeof(device.display_panel));
3877#endif
Channagoud Kadabi90869ce2015-04-27 11:15:14 -07003878#if ENABLE_WBC
Channagoud Kadabid801ac72015-08-26 11:21:51 -07003879 /* Wait if the display shutdown is in progress */
3880 while(pm_app_display_shutdown_in_prgs());
Channagoud Kadabi90869ce2015-04-27 11:15:14 -07003881 if (!pm_appsbl_display_init_done())
3882 target_display_init(device.display_panel);
3883 else
3884 display_image_on_screen();
3885#else
lijuang99c02d82015-02-13 19:04:34 +08003886 target_display_init(device.display_panel);
Channagoud Kadabi90869ce2015-04-27 11:15:14 -07003887#endif
lijuang99c02d82015-02-13 19:04:34 +08003888 dprintf(SPEW, "Display Init: Done\n");
3889#if NO_ALARM_DISPLAY
3890 }
3891#endif
3892#endif
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08003893
Greg Griscod6250552011-06-29 14:40:23 -07003894 target_serialno((unsigned char *) sn_buf);
Ajay Dudanib06c05f2011-05-12 14:46:10 -07003895 dprintf(SPEW,"serial number: %s\n",sn_buf);
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -08003896
Dhaval Patel223ec952013-07-18 14:49:44 -07003897 memset(display_panel_buf, '\0', MAX_PANEL_BUF_SIZE);
3898
Matthew Qindefd5562014-07-11 18:02:40 +08003899 /*
3900 * Check power off reason if user force reset,
3901 * if yes phone will do normal boot.
3902 */
3903 if (is_user_force_reset())
3904 goto normal_boot;
3905
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003906 /* Check if we should do something other than booting up */
Ameya Thakur0b9c2442013-05-10 13:22:56 -07003907 if (keys_get_state(KEY_VOLUMEUP) && keys_get_state(KEY_VOLUMEDOWN))
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07003908 {
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03003909 dprintf(ALWAYS,"dload mode key sequence detected\n");
lijuang395b5e62015-11-19 17:39:44 +08003910 reboot_device(EMERGENCY_DLOAD);
3911 dprintf(CRITICAL,"Failed to reboot into dload mode\n");
3912
Ameya Thakur0b9c2442013-05-10 13:22:56 -07003913 boot_into_fastboot = true;
3914 }
3915 if (!boot_into_fastboot)
3916 {
3917 if (keys_get_state(KEY_HOME) || keys_get_state(KEY_VOLUMEUP))
3918 boot_into_recovery = 1;
3919 if (!boot_into_recovery &&
3920 (keys_get_state(KEY_BACK) || keys_get_state(KEY_VOLUMEDOWN)))
Pavel Nedev5d91d412013-04-29 11:34:24 +03003921 boot_into_fastboot = true;
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07003922 }
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003923 #if NO_KEYPAD_DRIVER
Kinson Chik0b1c8162011-08-31 16:31:57 -07003924 if (fastboot_trigger())
Pavel Nedev5d91d412013-04-29 11:34:24 +03003925 boot_into_fastboot = true;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003926 #endif
Chandan Uddarajubedca152010-06-02 23:05:15 -07003927
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003928#if USE_PON_REBOOT_REG
3929 reboot_mode = check_hard_reboot_mode();
3930#else
Ajay Dudani77421292010-10-27 19:34:06 -07003931 reboot_mode = check_reboot_mode();
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003932#endif
3933 if (reboot_mode == RECOVERY_MODE)
3934 {
Ajay Dudani77421292010-10-27 19:34:06 -07003935 boot_into_recovery = 1;
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003936 }
3937 else if(reboot_mode == FASTBOOT_MODE)
3938 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03003939 boot_into_fastboot = true;
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003940 }
3941 else if(reboot_mode == ALARM_BOOT)
3942 {
Matthew Qind886f3c2014-01-17 16:52:01 +08003943 boot_reason_alarm = true;
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003944 }
Parth Dixit207573a2015-10-27 17:26:21 +05303945#if VERIFIED_BOOT
Parth Dixitddbc7352015-10-18 03:13:31 +05303946#if !VBOOT_MOTA
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003947 else if (reboot_mode == DM_VERITY_ENFORCING)
3948 {
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07003949 device.verity_mode = 1;
3950 write_device_info(&device);
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003951 }
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07003952#if ENABLE_VB_ATTEST
3953 else if (reboot_mode == DM_VERITY_EIO)
3954#else
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003955 else if (reboot_mode == DM_VERITY_LOGGING)
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07003956#endif
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003957 {
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07003958 device.verity_mode = 0;
3959 write_device_info(&device);
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003960 }
3961 else if (reboot_mode == DM_VERITY_KEYSCLEAR)
3962 {
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07003963 if(send_delete_keys_to_tz())
3964 ASSERT(0);
Ajay Dudani77421292010-10-27 19:34:06 -07003965 }
Parth Dixit207573a2015-10-27 17:26:21 +05303966#endif
Parth Dixitddbc7352015-10-18 03:13:31 +05303967#endif
Ajay Dudani77421292010-10-27 19:34:06 -07003968
Matthew Qindefd5562014-07-11 18:02:40 +08003969normal_boot:
Pavel Nedev5d91d412013-04-29 11:34:24 +03003970 if (!boot_into_fastboot)
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003971 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03003972 if (target_is_emmc_boot())
Shashank Mittala0032282011-08-26 14:50:11 -07003973 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03003974 if(emmc_recovery_init())
3975 dprintf(ALWAYS,"error in emmc_recovery_init\n");
3976 if(target_use_signed_kernel())
Shashank Mittala0032282011-08-26 14:50:11 -07003977 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03003978 if((device.is_unlocked) || (device.is_tampered))
3979 {
3980 #ifdef TZ_TAMPER_FUSE
3981 set_tamper_fuse_cmd();
3982 #endif
3983 #if USE_PCOM_SECBOOT
3984 set_tamper_flag(device.is_tampered);
3985 #endif
3986 }
Shashank Mittala0032282011-08-26 14:50:11 -07003987 }
Amit Blay6281ebc2015-01-11 14:44:08 +02003988
Pavel Nedev5d91d412013-04-29 11:34:24 +03003989 boot_linux_from_mmc();
Shashank Mittala0032282011-08-26 14:50:11 -07003990 }
Pavel Nedev5d91d412013-04-29 11:34:24 +03003991 else
3992 {
3993 recovery_init();
3994 #if USE_PCOM_SECBOOT
3995 if((device.is_unlocked) || (device.is_tampered))
3996 set_tamper_flag(device.is_tampered);
3997 #endif
3998 boot_linux_from_flash();
3999 }
4000 dprintf(CRITICAL, "ERROR: Could not do normal boot. Reverting "
4001 "to fastboot mode.\n");
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004002 }
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07004003
Amol Jadi5edf3552013-07-23 14:15:34 -07004004 /* We are here means regular boot did not happen. Start fastboot. */
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07004005
Amol Jadi5edf3552013-07-23 14:15:34 -07004006 /* register aboot specific fastboot commands */
4007 aboot_fastboot_register_commands();
Amol Jadi57abe4c2011-05-24 15:47:27 -07004008
Amol Jadi5edf3552013-07-23 14:15:34 -07004009 /* dump partition table for debug info */
Kinson Chikf1a43512011-07-14 11:28:39 -07004010 partition_dump();
Amol Jadi5edf3552013-07-23 14:15:34 -07004011
4012 /* initialize and start fastboot */
4013 fastboot_init(target_get_scratch_address(), target_get_max_flash_size());
lijuang4ece1e72015-08-14 21:02:36 +08004014#if FBCON_DISPLAY_MSG
lijuang9a7d3b92015-11-30 14:41:24 +08004015 display_fastboot_menu();
lijuang4ece1e72015-08-14 21:02:36 +08004016#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08004017}
4018
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07004019uint32_t get_page_size()
4020{
4021 return page_size;
4022}
4023
Amir Samuelov57a6fa22013-06-05 16:36:43 +03004024/*
4025 * Calculated and save hash (SHA256) for non-signed boot image.
4026 *
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07004027 * @param image_addr - Boot image address
4028 * @param image_size - Size of the boot image
Amir Samuelov57a6fa22013-06-05 16:36:43 +03004029 *
4030 * @return int - 0 on success, negative value on failure.
4031 */
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07004032static int aboot_save_boot_hash_mmc(uint32_t image_addr, uint32_t image_size)
Amir Samuelov57a6fa22013-06-05 16:36:43 +03004033{
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07004034 unsigned int digest[8];
4035#if IMAGE_VERIF_ALGO_SHA1
4036 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA1;
4037#else
4038 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA256;
4039#endif
Amir Samuelov57a6fa22013-06-05 16:36:43 +03004040
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07004041 target_crypto_init_params();
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -08004042 hash_find((unsigned char *) image_addr, image_size, (unsigned char *)&digest, auth_algo);
Amir Samuelov57a6fa22013-06-05 16:36:43 +03004043
4044 save_kernel_hash_cmd(digest);
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07004045 dprintf(INFO, "aboot_save_boot_hash_mmc: imagesize_actual size %d bytes.\n", (int) image_size);
Amir Samuelov57a6fa22013-06-05 16:36:43 +03004046
4047 return 0;
4048}
4049
Brian Swetland9c4c0752009-01-25 16:23:50 -08004050APP_START(aboot)
4051 .init = aboot_init,
4052APP_END