blob: 99dca487450813813ea5c4e52270a19ae7f8820a [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";
Ajay Dudanid04110c2011-01-17 23:55:07 -0800179
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700180#if VERIFIED_BOOT
Parth Dixitddbc7352015-10-18 03:13:31 +0530181#if !VBOOT_MOTA
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700182static const char *verity_mode = " androidboot.veritymode=";
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700183static const char *verified_state= " androidboot.verifiedbootstate=";
Parth Dixita5715a02015-10-29 12:25:10 +0530184static const char *keymaster_v1= " androidboot.keymaster=1";
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700185//indexed based on enum values, green is 0 by default
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700186
187struct verified_boot_verity_mode vbvm[] =
188{
189 {false, "logging"},
190 {true, "enforcing"},
191};
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700192struct verified_boot_state_name vbsn[] =
193{
194 {GREEN, "green"},
195 {ORANGE, "orange"},
196 {YELLOW,"yellow"},
197 {RED,"red" },
198};
199#endif
Parth Dixitddbc7352015-10-18 03:13:31 +0530200#endif
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700201
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700202static unsigned page_size = 0;
203static unsigned page_mask = 0;
Gaurav Nebhwani4d2389c2016-03-17 21:10:05 +0530204static unsigned mmc_blocksize = 0;
205static unsigned mmc_blocksize_mask = 0;
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700206static char ffbm_mode_string[FFBM_MODE_BUF_SIZE];
207static bool boot_into_ffbm;
vijay kumar870515d2015-08-31 16:37:24 +0530208static char *target_boot_params = NULL;
Matthew Qind886f3c2014-01-17 16:52:01 +0800209static bool boot_reason_alarm;
Channagoud Kadabi80a182b2015-03-11 17:04:23 -0700210static bool devinfo_present = true;
Channagoud Kadabi736c4962015-08-21 11:56:52 -0700211bool boot_into_fastboot = false;
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700212
Shashank Mittalcd98d472011-08-02 14:29:24 -0700213/* Assuming unauthorized kernel image by default */
214static int auth_kernel_img = 0;
Parth Dixit6f5822f2015-10-18 01:20:53 +0530215#if VBOOT_MOTA
216static device_info device = {DEVICE_MAGIC, 0, 0, 0, 0, {0}, {0},{0}};
217#else
lijuang511a2b52015-08-14 20:50:51 +0800218static device_info device = {DEVICE_MAGIC, 0, 0, 0, 0, {0}, {0},{0}, 1};
Parth Dixit6f5822f2015-10-18 01:20:53 +0530219#endif
vijay kumarc65876c2015-04-24 13:29:16 +0530220static bool is_allow_unlock = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -0700221
vijay kumarca2e6812015-07-08 20:28:25 +0530222static char frp_ptns[2][8] = {"config","frp"};
223
lijuang511a2b52015-08-14 20:50:51 +0800224static const char *critical_flash_allowed_ptn[] = {
225 "aboot",
226 "rpm",
227 "tz",
228 "sbl",
229 "sdi",
230 "sbl1",
231 "xbl",
232 "hyp",
233 "pmic",
234 "bootloader",
235 "devinfo",
236 "partition"};
237
Dima Zavin42168f22009-01-30 11:52:22 -0800238struct atag_ptbl_entry
239{
240 char name[16];
241 unsigned offset;
242 unsigned size;
243 unsigned flags;
244};
245
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700246/*
247 * Partition info, required to be published
248 * for fastboot
249 */
250struct getvar_partition_info {
251 const char part_name[MAX_GPT_NAME_SIZE]; /* Partition name */
252 char getvar_size[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for size */
253 char getvar_type[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for type */
254 char size_response[MAX_RSP_SIZE]; /* fastboot response for size */
255 char type_response[MAX_RSP_SIZE]; /* fastboot response for type */
256};
257
258/*
259 * Right now, we are publishing the info for only
260 * three partitions
261 */
262struct getvar_partition_info part_info[] =
263{
264 { "system" , "partition-size:", "partition-type:", "", "ext4" },
265 { "userdata", "partition-size:", "partition-type:", "", "ext4" },
266 { "cache" , "partition-size:", "partition-type:", "", "ext4" },
267};
268
269char max_download_size[MAX_RSP_SIZE];
Ameya Thakur11cf1a62013-08-05 12:44:48 -0700270char charger_screen_enabled[MAX_RSP_SIZE];
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800271char sn_buf[13];
Dhaval Patel223ec952013-07-18 14:49:44 -0700272char display_panel_buf[MAX_PANEL_BUF_SIZE];
Unnati Gandhi62c8ab82014-01-24 11:01:01 +0530273char panel_display_mode[MAX_RSP_SIZE];
lijuang102dfa92015-10-09 18:31:03 +0800274
275#if CHECK_BAT_VOLTAGE
lijuang65c5a822015-08-29 16:35:36 +0800276char battery_voltage[MAX_RSP_SIZE];
lijuang102dfa92015-10-09 18:31:03 +0800277char battery_soc_ok [MAX_RSP_SIZE];
278#endif
279
lijuangf16461c2015-08-03 17:09:34 +0800280char get_variant[MAX_RSP_SIZE];
Greg Griscod6250552011-06-29 14:40:23 -0700281
Greg Griscod2471ef2011-07-14 13:00:42 -0700282extern int emmc_recovery_init(void);
283
Kinson Chik0b1c8162011-08-31 16:31:57 -0700284#if NO_KEYPAD_DRIVER
285extern int fastboot_trigger(void);
286#endif
Greg Griscod2471ef2011-07-14 13:00:42 -0700287
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800288static void update_ker_tags_rdisk_addr(struct boot_img_hdr *hdr, bool is_arm64)
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700289{
290 /* overwrite the destination of specified for the project */
Channagoud Kadabi7042fa32013-04-26 16:44:14 -0700291#ifdef ABOOT_IGNORE_BOOT_HEADER_ADDRS
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800292 if (is_arm64)
293 hdr->kernel_addr = ABOOT_FORCE_KERNEL64_ADDR;
294 else
295 hdr->kernel_addr = ABOOT_FORCE_KERNEL_ADDR;
Channagoud Kadabi7042fa32013-04-26 16:44:14 -0700296 hdr->ramdisk_addr = ABOOT_FORCE_RAMDISK_ADDR;
297 hdr->tags_addr = ABOOT_FORCE_TAGS_ADDR;
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700298#endif
299}
300
Dima Zavin42168f22009-01-30 11:52:22 -0800301static void ptentry_to_tag(unsigned **ptr, struct ptentry *ptn)
302{
303 struct atag_ptbl_entry atag_ptn;
304
305 memcpy(atag_ptn.name, ptn->name, 16);
306 atag_ptn.name[15] = '\0';
307 atag_ptn.offset = ptn->start;
308 atag_ptn.size = ptn->length;
309 atag_ptn.flags = ptn->flags;
310 memcpy(*ptr, &atag_ptn, sizeof(struct atag_ptbl_entry));
311 *ptr += sizeof(struct atag_ptbl_entry) / sizeof(unsigned);
312}
Brian Swetland9c4c0752009-01-25 16:23:50 -0800313
lijuang102dfa92015-10-09 18:31:03 +0800314#if CHECK_BAT_VOLTAGE
315void update_battery_status(void)
316{
317 snprintf(battery_voltage,MAX_RSP_SIZE, "%d",target_get_battery_voltage());
318 snprintf(battery_soc_ok ,MAX_RSP_SIZE, "%s",target_battery_soc_ok()? "yes":"no");
319}
320#endif
321
Neeti Desaie245d492012-06-01 12:52:13 -0700322unsigned char *update_cmdline(const char * cmdline)
Brian Swetland9c4c0752009-01-25 16:23:50 -0800323{
David Ng183a7422009-12-07 14:55:21 -0800324 int cmdline_len = 0;
325 int have_cmdline = 0;
Amol Jadi168b7712012-03-06 16:15:00 -0800326 unsigned char *cmdline_final = NULL;
Neeti Desaie245d492012-06-01 12:52:13 -0700327 int pause_at_bootup = 0;
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800328 bool warm_boot = false;
Pavel Nedev5614d222013-06-17 18:01:02 +0300329 bool gpt_exists = partition_gpt_exists();
Joonwoo Park61112782013-10-02 19:50:39 -0700330 int have_target_boot_params = 0;
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700331 char *boot_dev_buf = NULL;
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200332 bool is_mdtp_activated = 0;
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700333#if VERIFIED_BOOT
Parth Dixitddbc7352015-10-18 03:13:31 +0530334#if !VBOOT_MOTA
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700335 uint32_t boot_state = boot_verify_get_state();
336#endif
Parth Dixitddbc7352015-10-18 03:13:31 +0530337#endif
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700338
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200339#ifdef MDTP_SUPPORT
340 mdtp_activated(&is_mdtp_activated);
341#endif /* MDTP_SUPPORT */
Dima Zavin42168f22009-01-30 11:52:22 -0800342
Brian Swetland9c4c0752009-01-25 16:23:50 -0800343 if (cmdline && cmdline[0]) {
David Ng183a7422009-12-07 14:55:21 -0800344 cmdline_len = strlen(cmdline);
345 have_cmdline = 1;
346 }
347 if (target_is_emmc_boot()) {
348 cmdline_len += strlen(emmc_cmdline);
Sridhar Parasuram7bd4aaf2015-02-12 11:14:38 -0800349#if USE_BOOTDEV_CMDLINE
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700350 boot_dev_buf = (char *) malloc(sizeof(char) * BOOT_DEV_MAX_LEN);
351 ASSERT(boot_dev_buf);
352 platform_boot_dev_cmdline(boot_dev_buf);
353 cmdline_len += strlen(boot_dev_buf);
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700354#endif
David Ng183a7422009-12-07 14:55:21 -0800355 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800356
357 cmdline_len += strlen(usb_sn_cmdline);
358 cmdline_len += strlen(sn_buf);
359
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700360#if VERIFIED_BOOT
Parth Dixitddbc7352015-10-18 03:13:31 +0530361#if !VBOOT_MOTA
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700362 cmdline_len += strlen(verified_state) + strlen(vbsn[boot_state].name);
Sridhar Parasuram7d8bb9a2015-09-23 18:21:19 -0700363 if ((device.verity_mode != 0 ) && (device.verity_mode != 1))
364 {
365 dprintf(CRITICAL, "Devinfo paritition possibly corrupted!!!. Please erase devinfo partition to continue booting\n");
366 ASSERT(0);
367 }
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700368 cmdline_len += strlen(verity_mode) + strlen(vbvm[device.verity_mode].name);
Parth Dixita5715a02015-10-29 12:25:10 +0530369 cmdline_len += strlen(keymaster_v1);
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700370#endif
Parth Dixitddbc7352015-10-18 03:13:31 +0530371#endif
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700372
Pavel Nedev5614d222013-06-17 18:01:02 +0300373 if (boot_into_recovery && gpt_exists)
374 cmdline_len += strlen(secondary_gpt_enable);
375
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200376 if(is_mdtp_activated)
377 cmdline_len += strlen(mdtp_activated_flag);
378
Pavel Nedev328ac822013-04-05 15:25:11 +0300379 if (boot_into_ffbm) {
380 cmdline_len += strlen(androidboot_mode);
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700381 cmdline_len += strlen(ffbm_mode_string);
Pavel Nedev898298c2013-02-27 12:36:09 -0800382 /* reduce kernel console messages to speed-up boot */
383 cmdline_len += strlen(loglevel);
Matthew Qind886f3c2014-01-17 16:52:01 +0800384 } else if (boot_reason_alarm) {
385 cmdline_len += strlen(alarmboot_cmdline);
Zhenhua Huang431dafa2015-06-30 16:13:37 +0800386 } else if ((target_build_variant_user() || device.charger_screen_enabled)
387 && target_pause_for_battery_charge()) {
David Ngf773dde2010-07-26 19:55:08 -0700388 pause_at_bootup = 1;
389 cmdline_len += strlen(battchg_pause);
390 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800391
Shashank Mittalcd98d472011-08-02 14:29:24 -0700392 if(target_use_signed_kernel() && auth_kernel_img) {
393 cmdline_len += strlen(auth_kernel);
394 }
395
Joonwoo Park61112782013-10-02 19:50:39 -0700396 if (get_target_boot_params(cmdline, boot_into_recovery ? "recoveryfs" :
397 "system",
vijay kumar870515d2015-08-31 16:37:24 +0530398 &target_boot_params) == 0) {
Joonwoo Park61112782013-10-02 19:50:39 -0700399 have_target_boot_params = 1;
400 cmdline_len += strlen(target_boot_params);
401 }
402
Ajay Dudanid04110c2011-01-17 23:55:07 -0800403 /* Determine correct androidboot.baseband to use */
404 switch(target_baseband())
405 {
406 case BASEBAND_APQ:
407 cmdline_len += strlen(baseband_apq);
408 break;
409
410 case BASEBAND_MSM:
411 cmdline_len += strlen(baseband_msm);
412 break;
413
414 case BASEBAND_CSFB:
415 cmdline_len += strlen(baseband_csfb);
416 break;
417
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800418 case BASEBAND_SVLTE2A:
419 cmdline_len += strlen(baseband_svlte2a);
Ajay Dudanid04110c2011-01-17 23:55:07 -0800420 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700421
422 case BASEBAND_MDM:
423 cmdline_len += strlen(baseband_mdm);
424 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700425
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800426 case BASEBAND_MDM2:
427 cmdline_len += strlen(baseband_mdm2);
428 break;
429
Amol Jadi5c61a952012-05-04 17:05:35 -0700430 case BASEBAND_SGLTE:
431 cmdline_len += strlen(baseband_sglte);
432 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530433
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800434 case BASEBAND_SGLTE2:
435 cmdline_len += strlen(baseband_sglte2);
436 break;
437
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530438 case BASEBAND_DSDA:
439 cmdline_len += strlen(baseband_dsda);
440 break;
Amol Jadi2a15a272013-01-22 12:03:36 -0800441
442 case BASEBAND_DSDA2:
443 cmdline_len += strlen(baseband_dsda2);
444 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800445 }
446
Lijuan Gao4a5b8322014-07-24 10:38:42 +0800447 if (cmdline) {
448 if ((strstr(cmdline, DISPLAY_DEFAULT_PREFIX) == NULL) &&
Padmanabhan Komandurubccbcdc2015-06-30 16:19:24 +0530449 target_display_panel_node(display_panel_buf,
450 MAX_PANEL_BUF_SIZE) &&
Lijuan Gao4a5b8322014-07-24 10:38:42 +0800451 strlen(display_panel_buf)) {
452 cmdline_len += strlen(display_panel_buf);
453 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700454 }
455
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800456 if (target_warm_boot()) {
457 warm_boot = true;
458 cmdline_len += strlen(warmboot_cmdline);
459 }
460
Channagoud Kadabi85c7ec32016-01-28 23:09:21 -0800461#if TARGET_CMDLINE_SUPPORT
462 char *target_cmdline_buf = malloc(TARGET_MAX_CMDLNBUF);
463 int target_cmd_line_len;
464 ASSERT(target_cmdline_buf);
465 target_cmd_line_len = target_update_cmdline(target_cmdline_buf);
466 cmdline_len += target_cmd_line_len;
467#endif
468
David Ng183a7422009-12-07 14:55:21 -0800469 if (cmdline_len > 0) {
470 const char *src;
Maria Yu52254c02014-07-04 16:14:54 +0800471 unsigned char *dst;
472
473 cmdline_final = (unsigned char*) malloc((cmdline_len + 4) & (~3));
474 ASSERT(cmdline_final != NULL);
vijay kumar287bb542015-09-29 13:01:52 +0530475 memset((void *)cmdline_final, 0, sizeof(*cmdline_final));
Maria Yu52254c02014-07-04 16:14:54 +0800476 dst = cmdline_final;
Neeti Desaie245d492012-06-01 12:52:13 -0700477
Amol Jadi168b7712012-03-06 16:15:00 -0800478 /* Save start ptr for debug print */
David Ng183a7422009-12-07 14:55:21 -0800479 if (have_cmdline) {
480 src = cmdline;
481 while ((*dst++ = *src++));
482 }
483 if (target_is_emmc_boot()) {
484 src = emmc_cmdline;
485 if (have_cmdline) --dst;
David Ngf773dde2010-07-26 19:55:08 -0700486 have_cmdline = 1;
487 while ((*dst++ = *src++));
Sridhar Parasuram7bd4aaf2015-02-12 11:14:38 -0800488#if USE_BOOTDEV_CMDLINE
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700489 src = boot_dev_buf;
490 if (have_cmdline) --dst;
491 while ((*dst++ = *src++));
492#endif
David Ngf773dde2010-07-26 19:55:08 -0700493 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800494
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700495#if VERIFIED_BOOT
Parth Dixitddbc7352015-10-18 03:13:31 +0530496#if !VBOOT_MOTA
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700497 src = verified_state;
498 if(have_cmdline) --dst;
499 have_cmdline = 1;
500 while ((*dst++ = *src++));
501 src = vbsn[boot_state].name;
502 if(have_cmdline) --dst;
503 while ((*dst++ = *src++));
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700504
Sridhar Parasuram7d8bb9a2015-09-23 18:21:19 -0700505 if ((device.verity_mode != 0 ) && (device.verity_mode != 1))
506 {
507 dprintf(CRITICAL, "Devinfo paritition possibly corrupted!!!. Please erase devinfo partition to continue booting\n");
508 ASSERT(0);
509 }
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700510 src = verity_mode;
511 if(have_cmdline) --dst;
512 while ((*dst++ = *src++));
513 src = vbvm[device.verity_mode].name;
514 if(have_cmdline) -- dst;
515 while ((*dst++ = *src++));
Parth Dixita5715a02015-10-29 12:25:10 +0530516 src = keymaster_v1;
517 if(have_cmdline) --dst;
518 while ((*dst++ = *src++));
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700519#endif
Parth Dixitddbc7352015-10-18 03:13:31 +0530520#endif
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800521 src = usb_sn_cmdline;
522 if (have_cmdline) --dst;
523 have_cmdline = 1;
524 while ((*dst++ = *src++));
525 src = sn_buf;
526 if (have_cmdline) --dst;
527 have_cmdline = 1;
528 while ((*dst++ = *src++));
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800529 if (warm_boot) {
530 if (have_cmdline) --dst;
531 src = warmboot_cmdline;
532 while ((*dst++ = *src++));
533 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800534
Pavel Nedev5614d222013-06-17 18:01:02 +0300535 if (boot_into_recovery && gpt_exists) {
536 src = secondary_gpt_enable;
537 if (have_cmdline) --dst;
538 while ((*dst++ = *src++));
539 }
540
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200541 if (is_mdtp_activated) {
542 src = mdtp_activated_flag;
543 if (have_cmdline) --dst;
544 while ((*dst++ = *src++));
545 }
546
Pavel Nedev328ac822013-04-05 15:25:11 +0300547 if (boot_into_ffbm) {
548 src = androidboot_mode;
549 if (have_cmdline) --dst;
550 while ((*dst++ = *src++));
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700551 src = ffbm_mode_string;
Pavel Nedev328ac822013-04-05 15:25:11 +0300552 if (have_cmdline) --dst;
553 while ((*dst++ = *src++));
Pavel Nedev898298c2013-02-27 12:36:09 -0800554 src = loglevel;
555 if (have_cmdline) --dst;
556 while ((*dst++ = *src++));
Matthew Qind886f3c2014-01-17 16:52:01 +0800557 } else if (boot_reason_alarm) {
558 src = alarmboot_cmdline;
559 if (have_cmdline) --dst;
560 while ((*dst++ = *src++));
Pavel Nedev328ac822013-04-05 15:25:11 +0300561 } else if (pause_at_bootup) {
David Ngf773dde2010-07-26 19:55:08 -0700562 src = battchg_pause;
563 if (have_cmdline) --dst;
David Ng183a7422009-12-07 14:55:21 -0800564 while ((*dst++ = *src++));
565 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800566
Shashank Mittalcd98d472011-08-02 14:29:24 -0700567 if(target_use_signed_kernel() && auth_kernel_img) {
568 src = auth_kernel;
569 if (have_cmdline) --dst;
570 while ((*dst++ = *src++));
571 }
572
Ajay Dudanid04110c2011-01-17 23:55:07 -0800573 switch(target_baseband())
574 {
575 case BASEBAND_APQ:
576 src = baseband_apq;
577 if (have_cmdline) --dst;
578 while ((*dst++ = *src++));
579 break;
580
581 case BASEBAND_MSM:
582 src = baseband_msm;
583 if (have_cmdline) --dst;
584 while ((*dst++ = *src++));
585 break;
586
587 case BASEBAND_CSFB:
588 src = baseband_csfb;
589 if (have_cmdline) --dst;
590 while ((*dst++ = *src++));
591 break;
592
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800593 case BASEBAND_SVLTE2A:
594 src = baseband_svlte2a;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800595 if (have_cmdline) --dst;
596 while ((*dst++ = *src++));
597 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700598
599 case BASEBAND_MDM:
600 src = baseband_mdm;
601 if (have_cmdline) --dst;
602 while ((*dst++ = *src++));
603 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700604
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800605 case BASEBAND_MDM2:
606 src = baseband_mdm2;
607 if (have_cmdline) --dst;
608 while ((*dst++ = *src++));
609 break;
610
Amol Jadi5c61a952012-05-04 17:05:35 -0700611 case BASEBAND_SGLTE:
612 src = baseband_sglte;
613 if (have_cmdline) --dst;
614 while ((*dst++ = *src++));
615 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530616
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800617 case BASEBAND_SGLTE2:
618 src = baseband_sglte2;
619 if (have_cmdline) --dst;
620 while ((*dst++ = *src++));
621 break;
622
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530623 case BASEBAND_DSDA:
624 src = baseband_dsda;
625 if (have_cmdline) --dst;
626 while ((*dst++ = *src++));
627 break;
Amol Jadi2a15a272013-01-22 12:03:36 -0800628
629 case BASEBAND_DSDA2:
630 src = baseband_dsda2;
631 if (have_cmdline) --dst;
632 while ((*dst++ = *src++));
633 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800634 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700635
636 if (strlen(display_panel_buf)) {
Dhaval Patel223ec952013-07-18 14:49:44 -0700637 src = display_panel_buf;
638 if (have_cmdline) --dst;
639 while ((*dst++ = *src++));
640 }
Joonwoo Park61112782013-10-02 19:50:39 -0700641
642 if (have_target_boot_params) {
643 if (have_cmdline) --dst;
644 src = target_boot_params;
645 while ((*dst++ = *src++));
vijay kumar870515d2015-08-31 16:37:24 +0530646 free(target_boot_params);
Joonwoo Park61112782013-10-02 19:50:39 -0700647 }
Channagoud Kadabi85c7ec32016-01-28 23:09:21 -0800648
649#if TARGET_CMDLINE_SUPPORT
650 if (target_cmdline_buf && target_cmd_line_len)
651 {
652 if (have_cmdline) --dst;
653 src = target_cmdline_buf;
654 while((*dst++ = *src++));
655 free(target_cmdline_buf);
656 }
657#endif
Neeti Desaie245d492012-06-01 12:52:13 -0700658 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700659
660
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700661 if (boot_dev_buf)
662 free(boot_dev_buf);
663
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -0800664 if (cmdline_final)
665 dprintf(INFO, "cmdline: %s\n", cmdline_final);
666 else
667 dprintf(INFO, "cmdline is NULL\n");
Neeti Desaie245d492012-06-01 12:52:13 -0700668 return cmdline_final;
669}
670
671unsigned *atag_core(unsigned *ptr)
672{
673 /* CORE */
674 *ptr++ = 2;
675 *ptr++ = 0x54410001;
676
677 return ptr;
678
679}
680
681unsigned *atag_ramdisk(unsigned *ptr, void *ramdisk,
682 unsigned ramdisk_size)
683{
684 if (ramdisk_size) {
685 *ptr++ = 4;
686 *ptr++ = 0x54420005;
687 *ptr++ = (unsigned)ramdisk;
688 *ptr++ = ramdisk_size;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800689 }
690
Neeti Desaie245d492012-06-01 12:52:13 -0700691 return ptr;
692}
693
694unsigned *atag_ptable(unsigned **ptr_addr)
695{
696 int i;
697 struct ptable *ptable;
698
699 if ((ptable = flash_get_ptable()) && (ptable->count != 0)) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700700 *(*ptr_addr)++ = 2 + (ptable->count * (sizeof(struct atag_ptbl_entry) /
701 sizeof(unsigned)));
Neeti Desaie245d492012-06-01 12:52:13 -0700702 *(*ptr_addr)++ = 0x4d534d70;
703 for (i = 0; i < ptable->count; ++i)
704 ptentry_to_tag(ptr_addr, ptable_get(ptable, i));
705 }
706
707 return (*ptr_addr);
708}
709
710unsigned *atag_cmdline(unsigned *ptr, const char *cmdline)
711{
712 int cmdline_length = 0;
713 int n;
Neeti Desaie245d492012-06-01 12:52:13 -0700714 char *dest;
715
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800716 cmdline_length = strlen((const char*)cmdline);
Neeti Desaie245d492012-06-01 12:52:13 -0700717 n = (cmdline_length + 4) & (~3);
718
719 *ptr++ = (n / 4) + 2;
720 *ptr++ = 0x54410009;
721 dest = (char *) ptr;
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800722 while ((*dest++ = *cmdline++));
Neeti Desaie245d492012-06-01 12:52:13 -0700723 ptr += (n / 4);
724
725 return ptr;
726}
727
728unsigned *atag_end(unsigned *ptr)
729{
Brian Swetland9c4c0752009-01-25 16:23:50 -0800730 /* END */
731 *ptr++ = 0;
732 *ptr++ = 0;
733
Neeti Desaie245d492012-06-01 12:52:13 -0700734 return ptr;
735}
736
737void generate_atags(unsigned *ptr, const char *cmdline,
738 void *ramdisk, unsigned ramdisk_size)
739{
vijay kumar21a37452015-12-29 16:23:21 +0530740 unsigned *orig_ptr = ptr;
Neeti Desaie245d492012-06-01 12:52:13 -0700741 ptr = atag_core(ptr);
742 ptr = atag_ramdisk(ptr, ramdisk, ramdisk_size);
743 ptr = target_atag_mem(ptr);
744
745 /* Skip NAND partition ATAGS for eMMC boot */
746 if (!target_is_emmc_boot()){
747 ptr = atag_ptable(&ptr);
748 }
749
vijay kumar21a37452015-12-29 16:23:21 +0530750 /*
751 * Atags size filled till + cmdline size + 1 unsigned for 4-byte boundary + 4 unsigned
752 * for atag identifier in atag_cmdline and atag_end should be with in MAX_TAGS_SIZE bytes
753 */
754 if (((ptr - orig_ptr) + strlen(cmdline) + 5 * sizeof(unsigned)) < MAX_TAGS_SIZE) {
755 ptr = atag_cmdline(ptr, cmdline);
756 ptr = atag_end(ptr);
757 }
758 else {
759 dprintf(CRITICAL,"Crossing ATAGs Max size allowed\n");
760 ASSERT(0);
761 }
Neeti Desaie245d492012-06-01 12:52:13 -0700762}
763
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700764typedef void entry_func_ptr(unsigned, unsigned, unsigned*);
Neeti Desaie245d492012-06-01 12:52:13 -0700765void boot_linux(void *kernel, unsigned *tags,
766 const char *cmdline, unsigned machtype,
767 void *ramdisk, unsigned ramdisk_size)
768{
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800769 unsigned char *final_cmdline;
Amol Jadib6be5c12012-11-14 13:39:51 -0800770#if DEVICE_TREE
Neeti Desai17379b82012-06-04 18:42:53 -0700771 int ret = 0;
Amol Jadib6be5c12012-11-14 13:39:51 -0800772#endif
773
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700774 void (*entry)(unsigned, unsigned, unsigned*) = (entry_func_ptr*)(PA((addr_t)kernel));
Deepa Dinamani25a9f762012-11-30 15:57:15 -0800775 uint32_t tags_phys = PA((addr_t)tags);
vijay kumar1a50a642015-11-16 12:41:15 +0530776 struct kernel64_hdr *kptr = ((struct kernel64_hdr*)(PA((addr_t)kernel)));
Deepa Dinamani25a9f762012-11-30 15:57:15 -0800777
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530778 ramdisk = (void *)PA((addr_t)ramdisk);
Neeti Desaie245d492012-06-01 12:52:13 -0700779
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800780 final_cmdline = update_cmdline((const char*)cmdline);
781
Neeti Desai17379b82012-06-04 18:42:53 -0700782#if DEVICE_TREE
Amol Jadib6be5c12012-11-14 13:39:51 -0800783 dprintf(INFO, "Updating device tree: start\n");
784
Neeti Desai17379b82012-06-04 18:42:53 -0700785 /* Update the Device Tree */
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530786 ret = update_device_tree((void *)tags,(const char *)final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -0700787 if(ret)
788 {
789 dprintf(CRITICAL, "ERROR: Updating Device Tree Failed \n");
790 ASSERT(0);
791 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800792 dprintf(INFO, "Updating device tree: done\n");
Neeti Desai17379b82012-06-04 18:42:53 -0700793#else
Neeti Desaie245d492012-06-01 12:52:13 -0700794 /* Generating the Atags */
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800795 generate_atags(tags, final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -0700796#endif
Neeti Desaie245d492012-06-01 12:52:13 -0700797
Maria Yu52254c02014-07-04 16:14:54 +0800798 free(final_cmdline);
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -0700799
800#if VERIFIED_BOOT
lijuangbdd9bb42016-03-01 18:22:17 +0800801#if !VBOOT_MOTA
802 if (device.verity_mode == 0) {
803#if FBCON_DISPLAY_MSG
804 display_bootverify_menu(DISPLAY_MENU_LOGGING);
805 wait_for_users_action();
806#else
807 dprintf(CRITICAL,
808 "The dm-verity is not started in enforcing mode.\nWait for 5 seconds before proceeding\n");
809 mdelay(5000);
810#endif
811 }
812
813#endif
814#endif
815
816#if VERIFIED_BOOT
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -0700817 /* Write protect the device info */
Channagoud Kadabi3bd9d1e2015-05-05 16:18:20 -0700818 if (!boot_into_recovery && target_build_variant_user() && devinfo_present && mmc_write_protect("devinfo", 1))
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -0700819 {
820 dprintf(INFO, "Failed to write protect dev info\n");
821 ASSERT(0);
822 }
823#endif
824
Aravind Venkateswaran8f076242014-02-25 16:25:30 -0800825 /* Turn off splash screen if enabled */
826#if DISPLAY_SPLASH_SCREEN
827 target_display_shutdown();
828#endif
829
Veera Sundaram Sankaran67ea0932015-09-25 10:09:30 -0700830 /* Perform target specific cleanup */
831 target_uninit();
Aravind Venkateswaran8f076242014-02-25 16:25:30 -0800832
Deepa Dinamani33734bc2013-03-06 12:16:06 -0800833 dprintf(INFO, "booting linux @ %p, ramdisk @ %p (%d), tags/device tree @ %p\n",
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530834 entry, ramdisk, ramdisk_size, (void *)tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800835
836 enter_critical_section();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700837
Amol Jadi4421e652011-06-16 15:00:48 -0700838 /* do any platform specific cleanup before kernel entry */
839 platform_uninit();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700840
Brian Swetland9c4c0752009-01-25 16:23:50 -0800841 arch_disable_cache(UCACHE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700842
Amol Jadi504f9fe2012-08-16 13:56:48 -0700843#if ARM_WITH_MMU
Brian Swetland9c4c0752009-01-25 16:23:50 -0800844 arch_disable_mmu();
Amol Jadi504f9fe2012-08-16 13:56:48 -0700845#endif
Amol Jadi492d5a52013-03-15 16:12:34 -0700846 bs_set_timestamp(BS_KERNEL_ENTRY);
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800847
848 if (IS_ARM64(kptr))
849 /* Jump to a 64bit kernel */
850 scm_elexec_call((paddr_t)kernel, tags_phys);
851 else
852 /* Jump to a 32bit kernel */
853 entry(0, machtype, (unsigned*)tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800854}
855
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700856/* Function to check if the memory address range falls within the aboot
857 * boundaries.
858 * start: Start of the memory region
859 * size: Size of the memory region
860 */
Vijay Kumar Pendoti9c002ad2016-03-09 13:52:45 +0530861int check_aboot_addr_range_overlap(uintptr_t start, uint32_t size)
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700862{
863 /* Check for boundary conditions. */
Sundarajan Srinivasance2a0ea2013-12-16 17:02:56 -0800864 if ((UINT_MAX - start) < size)
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700865 return -1;
866
867 /* Check for memory overlap. */
868 if ((start < MEMBASE) && ((start + size) <= MEMBASE))
869 return 0;
Channagoud Kadabi94143912013-10-15 12:53:52 -0700870 else if (start >= (MEMBASE + MEMSIZE))
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700871 return 0;
872 else
873 return -1;
874}
875
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800876#define ROUND_TO_PAGE(x,y) (((x) + (y)) & (~(y)))
Brian Swetland9c4c0752009-01-25 16:23:50 -0800877
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800878BUF_DMA_ALIGN(buf, BOOT_IMG_MAX_PAGE_SIZE); //Equal to max-supported pagesize
Amol Jadib6be5c12012-11-14 13:39:51 -0800879#if DEVICE_TREE
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800880BUF_DMA_ALIGN(dt_buf, BOOT_IMG_MAX_PAGE_SIZE);
Amol Jadib6be5c12012-11-14 13:39:51 -0800881#endif
Dima Zavin214cc642009-01-26 11:16:21 -0800882
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700883static void verify_signed_bootimg(uint32_t bootimg_addr, uint32_t bootimg_size)
884{
885 int ret;
Channagoud Kadabia8c623f2015-01-13 14:48:48 -0800886
887#if !VERIFIED_BOOT
Sundarajan Srinivasance54d6e2013-11-11 12:45:00 -0800888#if IMAGE_VERIF_ALGO_SHA1
889 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA1;
890#else
891 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA256;
892#endif
Channagoud Kadabia8c623f2015-01-13 14:48:48 -0800893#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700894
895 /* Assume device is rooted at this time. */
896 device.is_tampered = 1;
897
898 dprintf(INFO, "Authenticating boot image (%d): start\n", bootimg_size);
899
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700900#if VERIFIED_BOOT
901 if(boot_into_recovery)
902 {
903 ret = boot_verify_image((unsigned char *)bootimg_addr,
Unnati Gandhi47051252015-03-02 15:21:09 +0530904 bootimg_size, "/recovery");
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700905 }
906 else
907 {
908 ret = boot_verify_image((unsigned char *)bootimg_addr,
Unnati Gandhi47051252015-03-02 15:21:09 +0530909 bootimg_size, "/boot");
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700910 }
911 boot_verify_print_state();
912#else
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700913 ret = image_verify((unsigned char *)bootimg_addr,
914 (unsigned char *)(bootimg_addr + bootimg_size),
915 bootimg_size,
Sundarajan Srinivasance54d6e2013-11-11 12:45:00 -0800916 auth_algo);
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700917#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700918 dprintf(INFO, "Authenticating boot image: done return value = %d\n", ret);
919
920 if (ret)
921 {
922 /* Authorized kernel */
923 device.is_tampered = 0;
Sundarajan Srinivasan3fb21f12013-09-16 18:36:15 -0700924 auth_kernel_img = 1;
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700925 }
926
Amit Blay4aa292f2015-04-28 21:55:59 +0300927#ifdef MDTP_SUPPORT
928 {
929 /* Verify MDTP lock.
930 * For boot & recovery partitions, use aboot's verification result.
931 */
932 mdtp_ext_partition_verification_t ext_partition;
933 ext_partition.partition = boot_into_recovery ? MDTP_PARTITION_RECOVERY : MDTP_PARTITION_BOOT;
934 ext_partition.integrity_state = device.is_tampered ? MDTP_PARTITION_STATE_INVALID : MDTP_PARTITION_STATE_VALID;
935 ext_partition.page_size = 0; /* Not needed since already validated */
936 ext_partition.image_addr = 0; /* Not needed since already validated */
937 ext_partition.image_size = 0; /* Not needed since already validated */
938 ext_partition.sig_avail = FALSE; /* Not needed since already validated */
939 mdtp_fwlock_verify_lock(&ext_partition);
940 }
941#endif /* MDTP_SUPPORT */
942
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700943#if USE_PCOM_SECBOOT
944 set_tamper_flag(device.is_tampered);
945#endif
946
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700947#if VERIFIED_BOOT
Sridhar Parasuram8b792422015-07-05 11:38:13 -0700948 switch(boot_verify_get_state())
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700949 {
Sridhar Parasuram8b792422015-07-05 11:38:13 -0700950 case RED:
lijuanga40d6302015-07-20 20:10:13 +0800951#if FBCON_DISPLAY_MSG
lijuang9a7d3b92015-11-30 14:41:24 +0800952 display_bootverify_menu(DISPLAY_MENU_RED);
lijuanga40d6302015-07-20 20:10:13 +0800953 wait_for_users_action();
954#else
955 dprintf(CRITICAL,
956 "Your device has failed verification and may not work properly.\nWait for 5 seconds before proceeding\n");
957 mdelay(5000);
958#endif
959
960 break;
Sridhar Parasuram8b792422015-07-05 11:38:13 -0700961 case YELLOW:
lijuanga40d6302015-07-20 20:10:13 +0800962#if FBCON_DISPLAY_MSG
lijuang9a7d3b92015-11-30 14:41:24 +0800963 display_bootverify_menu(DISPLAY_MENU_YELLOW);
lijuanga40d6302015-07-20 20:10:13 +0800964 wait_for_users_action();
965#else
966 dprintf(CRITICAL,
967 "Your device has loaded a different operating system.\nWait for 5 seconds before proceeding\n");
968 mdelay(5000);
969#endif
970 break;
Sridhar Parasuram8b792422015-07-05 11:38:13 -0700971 default:
lijuanga40d6302015-07-20 20:10:13 +0800972 break;
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700973 }
974#endif
Sridhar Parasuram8b792422015-07-05 11:38:13 -0700975#if !VERIFIED_BOOT
Unnati Gandhi1be04752015-03-27 19:41:53 +0530976 if(device.is_tampered)
977 {
978 write_device_info_mmc(&device);
979 #ifdef TZ_TAMPER_FUSE
980 set_tamper_fuse_cmd();
981 #endif
982 #ifdef ASSERT_ON_TAMPER
983 dprintf(CRITICAL, "Device is tampered. Asserting..\n");
984 ASSERT(0);
985 #endif
986 }
Sridhar Parasuram8b792422015-07-05 11:38:13 -0700987#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700988}
989
Maunik Shah0f3c8ac2014-03-02 17:47:58 +0530990static bool check_format_bit()
991{
992 bool ret = false;
993 int index;
994 uint64_t offset;
995 struct boot_selection_info *in = NULL;
996 char *buf = NULL;
997
998 index = partition_get_index("bootselect");
999 if (index == INVALID_PTN)
1000 {
1001 dprintf(INFO, "Unable to locate /bootselect partition\n");
1002 return ret;
1003 }
1004 offset = partition_get_offset(index);
1005 if(!offset)
1006 {
1007 dprintf(INFO, "partition /bootselect doesn't exist\n");
1008 return ret;
1009 }
1010 buf = (char *) memalign(CACHE_LINE, ROUNDUP(page_size, CACHE_LINE));
1011 ASSERT(buf);
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301012 if (mmc_read(offset, (uint32_t *)buf, page_size))
Maunik Shah0f3c8ac2014-03-02 17:47:58 +05301013 {
1014 dprintf(INFO, "mmc read failure /bootselect %d\n", page_size);
1015 free(buf);
1016 return ret;
1017 }
1018 in = (struct boot_selection_info *) buf;
1019 if ((in->signature == BOOTSELECT_SIGNATURE) &&
1020 (in->version == BOOTSELECT_VERSION)) {
1021 if ((in->state_info & BOOTSELECT_FORMAT) &&
1022 !(in->state_info & BOOTSELECT_FACTORY))
1023 ret = true;
1024 } else {
1025 dprintf(CRITICAL, "Signature: 0x%08x or version: 0x%08x mismatched of /bootselect\n",
1026 in->signature, in->version);
1027 ASSERT(0);
1028 }
1029 free(buf);
1030 return ret;
1031}
1032
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001033void boot_verifier_init()
1034{
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001035 uint32_t boot_state;
1036 /* Check if device unlock */
1037 if(device.is_unlocked)
1038 {
1039 boot_verify_send_event(DEV_UNLOCK);
1040 boot_verify_print_state();
1041 dprintf(CRITICAL, "Device is unlocked! Skipping verification...\n");
1042 return;
1043 }
1044 else
1045 {
1046 boot_verify_send_event(BOOT_INIT);
1047 }
1048
1049 /* Initialize keystore */
1050 boot_state = boot_verify_keystore_init();
1051 if(boot_state == YELLOW)
1052 {
1053 boot_verify_print_state();
1054 dprintf(CRITICAL, "Keystore verification failed! Continuing anyways...\n");
1055 }
1056}
1057
Shashank Mittal23b8f422010-04-16 19:27:21 -07001058int boot_linux_from_mmc(void)
1059{
1060 struct boot_img_hdr *hdr = (void*) buf;
1061 struct boot_img_hdr *uhdr;
1062 unsigned offset = 0;
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07001063 int rcode;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001064 unsigned long long ptn = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001065 int index = INVALID_PTN;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001066
Shashank Mittalcd98d472011-08-02 14:29:24 -07001067 unsigned char *image_addr = 0;
1068 unsigned kernel_actual;
1069 unsigned ramdisk_actual;
1070 unsigned imagesize_actual;
Neeti Desai465491e2012-07-31 12:53:35 -07001071 unsigned second_actual = 0;
Neeti Desai465491e2012-07-31 12:53:35 -07001072
Matthew Qin271927e2015-03-31 22:07:07 -04001073 unsigned int dtb_size = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08001074 unsigned int out_len = 0;
1075 unsigned int out_avai_len = 0;
1076 unsigned char *out_addr = NULL;
1077 uint32_t dtb_offset = 0;
1078 unsigned char *kernel_start_addr = NULL;
1079 unsigned int kernel_size = 0;
Ameya Thakur10a33452016-06-13 14:24:26 -07001080 unsigned int patched_kernel_hdr_size = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08001081 int rc;
Neeti Desai465491e2012-07-31 12:53:35 -07001082#if DEVICE_TREE
1083 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -07001084 struct dt_entry dt_entry;
Neeti Desai465491e2012-07-31 12:53:35 -07001085 unsigned dt_table_offset;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001086 uint32_t dt_actual;
Deepa Dinamani19648b42013-09-05 17:05:55 -07001087 uint32_t dt_hdr_size;
Matthew Qin271927e2015-03-31 22:07:07 -04001088 unsigned char *best_match_dt_addr = NULL;
Neeti Desai465491e2012-07-31 12:53:35 -07001089#endif
Matthew Qin49e51fa2015-02-09 10:40:45 +08001090 struct kernel64_hdr *kptr = NULL;
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001091
Maunik Shah0f3c8ac2014-03-02 17:47:58 +05301092 if (check_format_bit())
1093 boot_into_recovery = 1;
1094
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07001095 if (!boot_into_recovery) {
1096 memset(ffbm_mode_string, '\0', sizeof(ffbm_mode_string));
1097 rcode = get_ffbm(ffbm_mode_string, sizeof(ffbm_mode_string));
1098 if (rcode <= 0) {
1099 boot_into_ffbm = false;
1100 if (rcode < 0)
1101 dprintf(CRITICAL,"failed to get ffbm cookie");
1102 } else
1103 boot_into_ffbm = true;
1104 } else
1105 boot_into_ffbm = false;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001106 uhdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
1107 if (!memcmp(uhdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1108 dprintf(INFO, "Unified boot method!\n");
1109 hdr = uhdr;
1110 goto unified_boot;
1111 }
Greg Griscod6250552011-06-29 14:40:23 -07001112 if (!boot_into_recovery) {
Kinson Chikf1a43512011-07-14 11:28:39 -07001113 index = partition_get_index("boot");
1114 ptn = partition_get_offset(index);
1115 if(ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -07001116 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1117 return -1;
1118 }
Kinson Chikf1a43512011-07-14 11:28:39 -07001119 }
1120 else {
1121 index = partition_get_index("recovery");
1122 ptn = partition_get_offset(index);
1123 if(ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -07001124 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
1125 return -1;
1126 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001127 }
Channagoud Kadabief0547c2015-02-10 12:57:38 -08001128 /* Set Lun for boot & recovery partitions */
1129 mmc_set_lun(partition_get_lun(index));
Shashank Mittal23b8f422010-04-16 19:27:21 -07001130
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301131 if (mmc_read(ptn + offset, (uint32_t *) buf, page_size)) {
Shashank Mittal23b8f422010-04-16 19:27:21 -07001132 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
1133 return -1;
1134 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001135
1136 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001137 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Shashank Mittal23b8f422010-04-16 19:27:21 -07001138 return -1;
1139 }
1140
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07001141 if (hdr->page_size && (hdr->page_size != page_size)) {
vijay kumar2e21b3a2014-06-26 17:40:15 +05301142
1143 if (hdr->page_size > BOOT_IMG_MAX_PAGE_SIZE) {
1144 dprintf(CRITICAL, "ERROR: Invalid page size\n");
1145 return -1;
1146 }
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07001147 page_size = hdr->page_size;
1148 page_mask = page_size - 1;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001149 }
1150
vijay kumar287bb542015-09-29 13:01:52 +05301151 /* ensure commandline is terminated */
1152 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
1153
Matthew Qin49e51fa2015-02-09 10:40:45 +08001154 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1155 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001156
Matthew Qin49e51fa2015-02-09 10:40:45 +08001157 image_addr = (unsigned char *)target_get_scratch_address();
1158
1159#if DEVICE_TREE
1160 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
1161 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + dt_actual);
1162#else
1163 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
1164#endif
1165
1166#if VERIFIED_BOOT
1167 boot_verifier_init();
1168#endif
1169
Vijay Kumar Pendoti9c002ad2016-03-09 13:52:45 +05301170 if (check_aboot_addr_range_overlap((uintptr_t) image_addr, imagesize_actual))
Matthew Qin49e51fa2015-02-09 10:40:45 +08001171 {
1172 dprintf(CRITICAL, "Boot image buffer address overlaps with aboot addresses.\n");
1173 return -1;
1174 }
1175
Matthew Qinbb7923d2015-02-09 10:56:09 +08001176 /*
1177 * Update loading flow of bootimage to support compressed/uncompressed
1178 * bootimage on both 64bit and 32bit platform.
1179 * 1. Load bootimage from emmc partition onto DDR.
1180 * 2. Check if bootimage is gzip format. If yes, decompress compressed kernel
1181 * 3. Check kernel header and update kernel load addr for 64bit and 32bit
1182 * platform accordingly.
1183 * 4. Sanity Check on kernel_addr and ramdisk_addr and copy data.
1184 */
1185
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001186 dprintf(INFO, "Loading (%s) image (%d): start\n",
1187 (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001188 bs_set_timestamp(BS_KERNEL_LOAD_START);
1189
Gaurav Nebhwani43e2a462016-03-17 21:32:56 +05301190 if ((target_get_max_flash_size() - page_size) < imagesize_actual)
1191 {
1192 dprintf(CRITICAL, "booimage size is greater than DDR can hold\n");
1193 return -1;
1194 }
1195
Matthew Qinbb7923d2015-02-09 10:56:09 +08001196 /* Read image without signature */
Matthew Qin49e51fa2015-02-09 10:40:45 +08001197 if (mmc_read(ptn + offset, (void *)image_addr, imagesize_actual))
1198 {
1199 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
1200 return -1;
1201 }
1202
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001203 dprintf(INFO, "Loading (%s) image (%d): done\n",
1204 (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual);
1205
Matthew Qin49e51fa2015-02-09 10:40:45 +08001206 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
1207
1208 /* Authenticate Kernel */
1209 dprintf(INFO, "use_signed_kernel=%d, is_unlocked=%d, is_tampered=%d.\n",
1210 (int) target_use_signed_kernel(),
1211 device.is_unlocked,
1212 device.is_tampered);
1213
Channagoud Kadabi736c4962015-08-21 11:56:52 -07001214 /* Change the condition a little bit to include the test framework support.
1215 * We would never reach this point if device is in fastboot mode, even if we did
1216 * that means we are in test mode, so execute kernel authentication part for the
1217 * tests */
Parth Dixitcb0c6082015-12-30 15:01:13 +05301218 if((target_use_signed_kernel() && (!device.is_unlocked)) || is_test_mode_enabled())
Matthew Qin49e51fa2015-02-09 10:40:45 +08001219 {
1220 offset = imagesize_actual;
Vijay Kumar Pendoti9c002ad2016-03-09 13:52:45 +05301221 if (check_aboot_addr_range_overlap((uintptr_t)image_addr + offset, page_size))
Matthew Qin49e51fa2015-02-09 10:40:45 +08001222 {
1223 dprintf(CRITICAL, "Signature read buffer address overlaps with aboot addresses.\n");
1224 return -1;
1225 }
1226
1227 /* Read signature */
1228 if(mmc_read(ptn + offset, (void *)(image_addr + offset), page_size))
1229 {
1230 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
1231 return -1;
1232 }
1233
1234 verify_signed_bootimg((uint32_t)image_addr, imagesize_actual);
Channagoud Kadabi736c4962015-08-21 11:56:52 -07001235 /* The purpose of our test is done here */
Parth Dixitcb0c6082015-12-30 15:01:13 +05301236 if(is_test_mode_enabled() && auth_kernel_img)
Channagoud Kadabi736c4962015-08-21 11:56:52 -07001237 return 0;
Matthew Qin49e51fa2015-02-09 10:40:45 +08001238 } else {
1239 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
1240 #ifdef TZ_SAVE_KERNEL_HASH
1241 aboot_save_boot_hash_mmc((uint32_t) image_addr, imagesize_actual);
1242 #endif /* TZ_SAVE_KERNEL_HASH */
Amit Blay4aa292f2015-04-28 21:55:59 +03001243
1244#ifdef MDTP_SUPPORT
1245 {
1246 /* Verify MDTP lock.
1247 * For boot & recovery partitions, MDTP will use boot_verifier APIs,
1248 * since verification was skipped in aboot. The signature is not part of the loaded image.
1249 */
1250 mdtp_ext_partition_verification_t ext_partition;
1251 ext_partition.partition = boot_into_recovery ? MDTP_PARTITION_RECOVERY : MDTP_PARTITION_BOOT;
1252 ext_partition.integrity_state = MDTP_PARTITION_STATE_UNSET;
1253 ext_partition.page_size = page_size;
1254 ext_partition.image_addr = (uint32)image_addr;
1255 ext_partition.image_size = imagesize_actual;
1256 ext_partition.sig_avail = FALSE;
1257 mdtp_fwlock_verify_lock(&ext_partition);
1258 }
1259#endif /* MDTP_SUPPORT */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001260 }
1261
Sridhar Parasuram00bfedb2015-05-26 14:21:27 -07001262#if VERIFIED_BOOT
Reut Zysmanba8a9d52016-02-18 11:44:04 +02001263 if(boot_verify_get_state() == ORANGE)
1264 {
1265#if FBCON_DISPLAY_MSG
1266 display_bootverify_menu(DISPLAY_MENU_ORANGE);
1267 wait_for_users_action();
1268#else
1269 dprintf(CRITICAL,
1270 "Your device has been unlocked and can't be trusted.\nWait for 5 seconds before proceeding\n");
1271 mdelay(5000);
1272#endif
1273 }
1274#endif
1275
1276#if VERIFIED_BOOT
Parth Dixitbc9b6492015-10-18 00:41:38 +05301277#if !VBOOT_MOTA
Sridhar Parasuram00bfedb2015-05-26 14:21:27 -07001278 // send root of trust
Sridhar Parasuram96300dc2015-06-11 10:37:11 -07001279 if(!send_rot_command((uint32_t)device.is_unlocked))
Sridhar Parasuram00bfedb2015-05-26 14:21:27 -07001280 ASSERT(0);
1281#endif
Parth Dixitbc9b6492015-10-18 00:41:38 +05301282#endif
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001283 /*
Matthew Qinbb7923d2015-02-09 10:56:09 +08001284 * Check if the kernel image is a gzip package. If yes, need to decompress it.
1285 * If not, continue booting.
1286 */
1287 if (is_gzip_package((unsigned char *)(image_addr + page_size), hdr->kernel_size))
1288 {
1289 out_addr = (unsigned char *)(image_addr + imagesize_actual + page_size);
1290 out_avai_len = target_get_max_flash_size() - imagesize_actual - page_size;
Matthew Qin0b15b322015-05-19 05:20:54 -04001291 dprintf(INFO, "decompressing kernel image: start\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001292 rc = decompress((unsigned char *)(image_addr + page_size),
1293 hdr->kernel_size, out_addr, out_avai_len,
1294 &dtb_offset, &out_len);
1295 if (rc)
1296 {
Matthew Qin0b15b322015-05-19 05:20:54 -04001297 dprintf(CRITICAL, "decompressing kernel image failed!!!\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001298 ASSERT(0);
1299 }
1300
Matthew Qin0b15b322015-05-19 05:20:54 -04001301 dprintf(INFO, "decompressing kernel image: done\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001302 kptr = (struct kernel64_hdr *)out_addr;
1303 kernel_start_addr = out_addr;
1304 kernel_size = out_len;
1305 } else {
Ameya Thakur10a33452016-06-13 14:24:26 -07001306 dprintf(INFO, "Uncpmpressed kernel in use\n");
1307 if (!strncmp((char*)(image_addr + page_size),
1308 PATCHED_KERNEL_MAGIC,
1309 sizeof(PATCHED_KERNEL_MAGIC) - 1)) {
1310 dprintf(INFO, "Patched kernel detected\n");
1311 kptr = (struct kernel64_hdr *)(image_addr + page_size +
1312 PATCHED_KERNEL_HEADER_SIZE);
1313 //The size of the kernel is stored at start of kernel image + 16
1314 //The dtb would start just after the kernel
1315 dtb_offset = *((uint32_t*)((unsigned char*)
1316 (image_addr + page_size +
1317 sizeof(PATCHED_KERNEL_MAGIC) -
1318 1)));
1319 //The actual kernel starts after the 20 byte header.
1320 kernel_start_addr = (unsigned char*)(image_addr +
1321 page_size + PATCHED_KERNEL_HEADER_SIZE);
1322 kernel_size = hdr->kernel_size;
1323 patched_kernel_hdr_size = PATCHED_KERNEL_HEADER_SIZE;
1324 } else {
1325 dprintf(INFO, "Kernel image not patched..Unable to locate dt offset\n");
1326 kptr = (struct kernel64_hdr *)(image_addr + page_size);
1327 kernel_start_addr = (unsigned char *)(image_addr + page_size);
1328 kernel_size = hdr->kernel_size;
1329 }
Matthew Qinbb7923d2015-02-09 10:56:09 +08001330 }
1331
1332 /*
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001333 * Update the kernel/ramdisk/tags address if the boot image header
1334 * has default values, these default values come from mkbootimg when
1335 * the boot image is flashed using fastboot flash:raw
1336 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001337 update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr));
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001338
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001339 /* Get virtual addresses since the hdr saves physical addresses. */
1340 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
1341 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
1342 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001343
Matthew Qinbb7923d2015-02-09 10:56:09 +08001344 kernel_size = ROUND_TO_PAGE(kernel_size, page_mask);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001345 /* Check if the addresses in the header are valid. */
Matthew Qinbb7923d2015-02-09 10:56:09 +08001346 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_size) ||
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001347 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
1348 {
1349 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
1350 return -1;
1351 }
1352
1353#ifndef DEVICE_TREE
1354 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1355 {
1356 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
1357 return -1;
1358 }
1359#endif
1360
Matthew Qin49e51fa2015-02-09 10:40:45 +08001361 /* Move kernel, ramdisk and device tree to correct address */
Matthew Qinbb7923d2015-02-09 10:56:09 +08001362 memmove((void*) hdr->kernel_addr, kernel_start_addr, kernel_size);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001363 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001364
Matthew Qin49e51fa2015-02-09 10:40:45 +08001365 #if DEVICE_TREE
1366 if(hdr->dt_size) {
1367 dt_table_offset = ((uint32_t)image_addr + page_size + kernel_actual + ramdisk_actual + second_actual);
1368 table = (struct dt_table*) dt_table_offset;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001369
Matthew Qin49e51fa2015-02-09 10:40:45 +08001370 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
1371 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1372 return -1;
1373 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001374
P.V. Phani Kumar82916762016-03-09 09:20:19 +05301375 /* Its Error if, dt_hdr_size (table->num_entries * dt_entry size + Dev_Tree Header)
1376 goes beyound hdr->dt_size*/
1377 if (dt_hdr_size > ROUND_TO_PAGE(hdr->dt_size,hdr->page_size)) {
1378 dprintf(CRITICAL, "ERROR: Invalid Device Tree size \n");
1379 return -1;
1380 }
1381
Matthew Qin49e51fa2015-02-09 10:40:45 +08001382 /* Find index of device tree within device tree table */
1383 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
1384 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1385 return -1;
1386 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001387
P.V. Phani Kumar3c333202016-03-09 11:54:37 +05301388 if(dt_entry.offset > (UINT_MAX - dt_entry.size)) {
1389 dprintf(CRITICAL, "ERROR: Device tree contents are Invalid\n");
1390 return -1;
1391 }
1392
1393 /* Ensure we are not overshooting dt_size with the dt_entry selected */
1394 if ((dt_entry.offset + dt_entry.size) > hdr->dt_size) {
1395 dprintf(CRITICAL, "ERROR: Device tree contents are Invalid\n");
1396 return -1;
1397 }
1398
Matthew Qin271927e2015-03-31 22:07:07 -04001399 if (is_gzip_package((unsigned char *)dt_table_offset + dt_entry.offset, dt_entry.size))
1400 {
1401 unsigned int compressed_size = 0;
1402 out_addr += out_len;
1403 out_avai_len -= out_len;
Matthew Qin0b15b322015-05-19 05:20:54 -04001404 dprintf(INFO, "decompressing dtb: start\n");
Matthew Qin271927e2015-03-31 22:07:07 -04001405 rc = decompress((unsigned char *)dt_table_offset + dt_entry.offset,
1406 dt_entry.size, out_addr, out_avai_len,
1407 &compressed_size, &dtb_size);
1408 if (rc)
1409 {
Matthew Qin0b15b322015-05-19 05:20:54 -04001410 dprintf(CRITICAL, "decompressing dtb failed!!!\n");
Matthew Qin271927e2015-03-31 22:07:07 -04001411 ASSERT(0);
1412 }
1413
Matthew Qin0b15b322015-05-19 05:20:54 -04001414 dprintf(INFO, "decompressing dtb: done\n");
Matthew Qin271927e2015-03-31 22:07:07 -04001415 best_match_dt_addr = out_addr;
1416 } else {
1417 best_match_dt_addr = (unsigned char *)dt_table_offset + dt_entry.offset;
1418 dtb_size = dt_entry.size;
1419 }
1420
Matthew Qin49e51fa2015-02-09 10:40:45 +08001421 /* Validate and Read device device tree in the tags_addr */
Matthew Qin271927e2015-03-31 22:07:07 -04001422 if (check_aboot_addr_range_overlap(hdr->tags_addr, dtb_size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001423 {
1424 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1425 return -1;
1426 }
Shashank Mittal162244e2011-08-08 19:01:25 -07001427
Matthew Qin271927e2015-03-31 22:07:07 -04001428 memmove((void *)hdr->tags_addr, (char *)best_match_dt_addr, dtb_size);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001429 } else {
1430 /* Validate the tags_addr */
1431 if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual))
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001432 {
1433 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1434 return -1;
1435 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08001436 /*
1437 * If appended dev tree is found, update the atags with
1438 * memory address to the DTB appended location on RAM.
1439 * Else update with the atags address in the kernel header
1440 */
1441 void *dtb;
Ameya Thakur10a33452016-06-13 14:24:26 -07001442 dtb = dev_tree_appended(
1443 (void*)(image_addr + page_size +
1444 patched_kernel_hdr_size),
1445 hdr->kernel_size, dtb_offset,
1446 (void *)hdr->tags_addr);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001447 if (!dtb) {
1448 dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001449 return -1;
1450 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07001451 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08001452 #endif
Shashank Mittal23b8f422010-04-16 19:27:21 -07001453
Stanimir Varbanov69ec5462013-07-18 18:17:42 +03001454 if (boot_into_recovery && !device.is_unlocked && !device.is_tampered)
1455 target_load_ssd_keystore();
1456
Shashank Mittal23b8f422010-04-16 19:27:21 -07001457unified_boot:
Shashank Mittal23b8f422010-04-16 19:27:21 -07001458
Dima Zavin77e41f32013-03-06 16:10:43 -08001459 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07001460 (const char *)hdr->cmdline, board_machtype(),
Shashank Mittal23b8f422010-04-16 19:27:21 -07001461 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
1462
1463 return 0;
1464}
1465
Dima Zavin214cc642009-01-26 11:16:21 -08001466int boot_linux_from_flash(void)
1467{
1468 struct boot_img_hdr *hdr = (void*) buf;
Dima Zavin214cc642009-01-26 11:16:21 -08001469 struct ptentry *ptn;
1470 struct ptable *ptable;
1471 unsigned offset = 0;
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001472
Shashank Mittalcd98d472011-08-02 14:29:24 -07001473 unsigned char *image_addr = 0;
1474 unsigned kernel_actual;
1475 unsigned ramdisk_actual;
1476 unsigned imagesize_actual;
vijay kumar8f53e362015-11-24 13:38:11 +05301477 unsigned second_actual = 0;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001478
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001479#if DEVICE_TREE
1480 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -07001481 struct dt_entry dt_entry;
vijay kumar8f53e362015-11-24 13:38:11 +05301482 unsigned dt_table_offset;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001483 uint32_t dt_actual;
Deepa Dinamani19648b42013-09-05 17:05:55 -07001484 uint32_t dt_hdr_size;
vijay kumar8f53e362015-11-24 13:38:11 +05301485 unsigned int dtb_size = 0;
1486 unsigned char *best_match_dt_addr = NULL;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001487#endif
1488
David Ng183a7422009-12-07 14:55:21 -08001489 if (target_is_emmc_boot()) {
1490 hdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
1491 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1492 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
1493 return -1;
1494 }
1495 goto continue_boot;
1496 }
1497
Dima Zavin214cc642009-01-26 11:16:21 -08001498 ptable = flash_get_ptable();
1499 if (ptable == NULL) {
1500 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1501 return -1;
1502 }
1503
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001504 if(!boot_into_recovery)
1505 {
1506 ptn = ptable_find(ptable, "boot");
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001507
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001508 if (ptn == NULL) {
1509 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1510 return -1;
1511 }
1512 }
1513 else
1514 {
1515 ptn = ptable_find(ptable, "recovery");
1516 if (ptn == NULL) {
1517 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
1518 return -1;
1519 }
Dima Zavin214cc642009-01-26 11:16:21 -08001520 }
1521
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001522 if (flash_read(ptn, offset, buf, page_size)) {
Dima Zavin214cc642009-01-26 11:16:21 -08001523 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
1524 return -1;
1525 }
Dima Zavin214cc642009-01-26 11:16:21 -08001526
1527 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001528 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Dima Zavin214cc642009-01-26 11:16:21 -08001529 return -1;
1530 }
1531
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001532 if (hdr->page_size != page_size) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001533 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 -08001534 return -1;
1535 }
1536
vijay kumar287bb542015-09-29 13:01:52 +05301537 /* ensure commandline is terminated */
1538 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
1539
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001540 /*
1541 * Update the kernel/ramdisk/tags address if the boot image header
1542 * has default values, these default values come from mkbootimg when
1543 * the boot image is flashed using fastboot flash:raw
1544 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001545 update_ker_tags_rdisk_addr(hdr, false);
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001546
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001547 /* Get virtual addresses since the hdr saves physical addresses. */
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001548 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
1549 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
1550 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
1551
1552 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1553 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1554
1555 /* Check if the addresses in the header are valid. */
1556 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
1557 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
1558 {
1559 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
1560 return -1;
1561 }
1562
1563#ifndef DEVICE_TREE
1564 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1565 {
1566 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
1567 return -1;
1568 }
1569#endif
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001570
Shashank Mittalcd98d472011-08-02 14:29:24 -07001571 /* Authenticate Kernel */
Deepa Dinamani23b60d42013-06-24 18:10:52 -07001572 if(target_use_signed_kernel() && (!device.is_unlocked))
Shashank Mittalcd98d472011-08-02 14:29:24 -07001573 {
1574 image_addr = (unsigned char *)target_get_scratch_address();
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001575 offset = 0;
1576
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001577#if DEVICE_TREE
1578 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
vijay kumar966a9822015-12-09 18:36:09 +05301579
1580 if (UINT_MAX < ((uint64_t)kernel_actual + (uint64_t)ramdisk_actual+ (uint64_t)dt_actual + page_size)) {
1581 dprintf(CRITICAL, "Integer overflow detected in bootimage header fields\n");
1582 return -1;
1583 }
1584
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001585 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + dt_actual);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001586
1587 if (check_aboot_addr_range_overlap(hdr->tags_addr, hdr->dt_size))
1588 {
1589 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1590 return -1;
1591 }
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001592#else
vijay kumar966a9822015-12-09 18:36:09 +05301593 if (UINT_MAX < ((uint64_t)kernel_actual + (uint64_t)ramdisk_actual+ page_size)) {
1594 dprintf(CRITICAL, "Integer overflow detected in bootimage header fields\n");
1595 return -1;
1596 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07001597 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001598#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001599
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001600 dprintf(INFO, "Loading (%s) image (%d): start\n",
1601 (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001602 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -08001603
Shashank Mittalcd98d472011-08-02 14:29:24 -07001604 /* Read image without signature */
1605 if (flash_read(ptn, offset, (void *)image_addr, imagesize_actual))
1606 {
1607 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
1608 return -1;
1609 }
Dima Zavin214cc642009-01-26 11:16:21 -08001610
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001611 dprintf(INFO, "Loading (%s) image (%d): done\n",
1612 (!boot_into_recovery ? "boot" : "recovery"), imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001613 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Amol Jadib6be5c12012-11-14 13:39:51 -08001614
Shashank Mittalcd98d472011-08-02 14:29:24 -07001615 offset = imagesize_actual;
1616 /* Read signature */
1617 if (flash_read(ptn, offset, (void *)(image_addr + offset), page_size))
1618 {
1619 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001620 return -1;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001621 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07001622
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301623 verify_signed_bootimg((uint32_t)image_addr, imagesize_actual);
Shashank Mittalcd98d472011-08-02 14:29:24 -07001624
1625 /* Move kernel and ramdisk to correct address */
Matthew Qinbb7923d2015-02-09 10:56:09 +08001626 memmove((void*) hdr->kernel_addr, (char*) (image_addr + page_size), hdr->kernel_size);
1627 memmove((void*) hdr->ramdisk_addr, (char*) (image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001628#if DEVICE_TREE
vijay kumar8f53e362015-11-24 13:38:11 +05301629 if(hdr->dt_size != 0) {
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001630
vijay kumar8f53e362015-11-24 13:38:11 +05301631 dt_table_offset = ((uint32_t)image_addr + page_size + kernel_actual + ramdisk_actual + second_actual);
1632
1633 table = (struct dt_table*) dt_table_offset;
1634
1635 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0){
1636 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1637 return -1;
1638 }
1639
1640 /* Find index of device tree within device tree table */
1641 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
1642 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1643 return -1;
1644 }
1645
1646 /* Validate and Read device device tree in the "tags_add */
1647 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size)){
1648 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1649 return -1;
1650 }
1651
1652 best_match_dt_addr = (unsigned char *)table + dt_entry.offset;
1653 dtb_size = dt_entry.size;
1654 memmove((void *)hdr->tags_addr, (char *)best_match_dt_addr, dtb_size);
1655 }
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001656#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001657
1658 /* Make sure everything from scratch address is read before next step!*/
Shashank Mittala0032282011-08-26 14:50:11 -07001659 if(device.is_tampered)
Shashank Mittal162244e2011-08-08 19:01:25 -07001660 {
1661 write_device_info_flash(&device);
1662 }
Channagoud Kadabi5c86fe32012-02-16 10:58:48 +05301663#if USE_PCOM_SECBOOT
1664 set_tamper_flag(device.is_tampered);
1665#endif
Shashank Mittalcd98d472011-08-02 14:29:24 -07001666 }
1667 else
1668 {
Shashank Mittal162244e2011-08-08 19:01:25 -07001669 offset = page_size;
1670
Amol Jadib6be5c12012-11-14 13:39:51 -08001671 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1672 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1673 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
1674
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001675 dprintf(INFO, "Loading (%s) image (%d): start\n",
1676 (!boot_into_recovery ? "boot" : "recovery"), kernel_actual + ramdisk_actual);
1677
Amol Jadi492d5a52013-03-15 16:12:34 -07001678 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -08001679
1680 if (flash_read(ptn, offset, (void *)hdr->kernel_addr, kernel_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -07001681 dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
1682 return -1;
1683 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001684 offset += kernel_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001685
Amol Jadib6be5c12012-11-14 13:39:51 -08001686 if (flash_read(ptn, offset, (void *)hdr->ramdisk_addr, ramdisk_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -07001687 dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
1688 return -1;
1689 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001690 offset += ramdisk_actual;
1691
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001692 dprintf(INFO, "Loading (%s) image (%d): done\n",
1693 (!boot_into_recovery ? "boot" : "recovery"), kernel_actual + ramdisk_actual);
1694
Amol Jadi492d5a52013-03-15 16:12:34 -07001695 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001696
1697 if(hdr->second_size != 0) {
Amol Jadib6be5c12012-11-14 13:39:51 -08001698 offset += second_actual;
1699 /* Second image loading not implemented. */
1700 ASSERT(0);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001701 }
1702
1703#if DEVICE_TREE
1704 if(hdr->dt_size != 0) {
1705
1706 /* Read the device tree table into buffer */
1707 if(flash_read(ptn, offset, (void *) dt_buf, page_size)) {
1708 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
1709 return -1;
1710 }
1711
1712 table = (struct dt_table*) dt_buf;
1713
Deepa Dinamani19648b42013-09-05 17:05:55 -07001714 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001715 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1716 return -1;
1717 }
1718
P.V. Phani Kumar82916762016-03-09 09:20:19 +05301719 /* Its Error if, dt_hdr_size (table->num_entries * dt_entry size + Dev_Tree Header)
1720 goes beyound hdr->dt_size*/
1721 if (dt_hdr_size > ROUND_TO_PAGE(hdr->dt_size,hdr->page_size)) {
1722 dprintf(CRITICAL, "ERROR: Invalid Device Tree size \n");
1723 return -1;
1724 }
1725
Deepa Dinamani19648b42013-09-05 17:05:55 -07001726 table = (struct dt_table*) memalign(CACHE_LINE, dt_hdr_size);
1727 if (!table)
1728 return -1;
1729
1730 /* Read the entire device tree table into buffer */
1731 if(flash_read(ptn, offset, (void *)table, dt_hdr_size)) {
1732 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
1733 return -1;
1734 }
1735
1736
Joel Kingaa335dc2013-06-03 16:11:08 -07001737 /* Find index of device tree within device tree table */
1738 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001739 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1740 return -1;
1741 }
1742
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001743 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001744 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001745 {
1746 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1747 return -1;
1748 }
1749
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001750 /* Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001751 if(flash_read(ptn, offset + dt_entry.offset,
1752 (void *)hdr->tags_addr, dt_entry.size)) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001753 dprintf(CRITICAL, "ERROR: Cannot read device tree\n");
1754 return -1;
1755 }
1756 }
1757#endif
1758
Shashank Mittalcd98d472011-08-02 14:29:24 -07001759 }
David Ng183a7422009-12-07 14:55:21 -08001760continue_boot:
Dima Zavin214cc642009-01-26 11:16:21 -08001761
Dima Zavin214cc642009-01-26 11:16:21 -08001762 /* TODO: create/pass atags to kernel */
1763
Ajay Dudanie28a6072011-07-01 13:59:46 -07001764 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07001765 (const char *)hdr->cmdline, board_machtype(),
Dima Zavin214cc642009-01-26 11:16:21 -08001766 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
1767
1768 return 0;
1769}
Brian Swetland9c4c0752009-01-25 16:23:50 -08001770
Shashank Mittal162244e2011-08-08 19:01:25 -07001771void write_device_info_mmc(device_info *dev)
1772{
Shashank Mittal162244e2011-08-08 19:01:25 -07001773 unsigned long long ptn = 0;
1774 unsigned long long size;
1775 int index = INVALID_PTN;
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001776 uint32_t blocksize;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001777 uint8_t lun = 0;
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001778 uint32_t ret = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001779
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001780 if (devinfo_present)
1781 index = partition_get_index("devinfo");
1782 else
1783 index = partition_get_index("aboot");
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001784
Shashank Mittal162244e2011-08-08 19:01:25 -07001785 ptn = partition_get_offset(index);
1786 if(ptn == 0)
1787 {
1788 return;
1789 }
1790
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001791 lun = partition_get_lun(index);
1792 mmc_set_lun(lun);
1793
Shashank Mittal162244e2011-08-08 19:01:25 -07001794 size = partition_get_size(index);
1795
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001796 blocksize = mmc_get_device_blocksize();
1797
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001798 if (devinfo_present)
1799 ret = mmc_write(ptn, blocksize, (void *)info_buf);
1800 else
1801 ret = mmc_write((ptn + size - blocksize), blocksize, (void *)info_buf);
1802 if (ret)
Shashank Mittal162244e2011-08-08 19:01:25 -07001803 {
1804 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
Sridhar Parasurame1a97922015-07-27 15:31:22 -07001805 ASSERT(0);
Shashank Mittal162244e2011-08-08 19:01:25 -07001806 }
1807}
1808
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001809void read_device_info_mmc(struct device_info *info)
Shashank Mittal162244e2011-08-08 19:01:25 -07001810{
Shashank Mittal162244e2011-08-08 19:01:25 -07001811 unsigned long long ptn = 0;
1812 unsigned long long size;
1813 int index = INVALID_PTN;
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001814 uint32_t blocksize;
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001815 uint32_t ret = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001816
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001817 if ((index = partition_get_index("devinfo")) < 0)
1818 {
1819 devinfo_present = false;
1820 index = partition_get_index("aboot");
1821 }
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001822
Shashank Mittal162244e2011-08-08 19:01:25 -07001823 ptn = partition_get_offset(index);
1824 if(ptn == 0)
1825 {
1826 return;
1827 }
1828
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001829 mmc_set_lun(partition_get_lun(index));
1830
Shashank Mittal162244e2011-08-08 19:01:25 -07001831 size = partition_get_size(index);
1832
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001833 blocksize = mmc_get_device_blocksize();
1834
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001835 if (devinfo_present)
1836 ret = mmc_read(ptn, (void *)info_buf, blocksize);
1837 else
1838 ret = mmc_read((ptn + size - blocksize), (void *)info_buf, blocksize);
1839 if (ret)
Shashank Mittal162244e2011-08-08 19:01:25 -07001840 {
1841 dprintf(CRITICAL, "ERROR: Cannot read device info\n");
Sridhar Parasurame1a97922015-07-27 15:31:22 -07001842 ASSERT(0);
Shashank Mittal162244e2011-08-08 19:01:25 -07001843 }
Shashank Mittal162244e2011-08-08 19:01:25 -07001844}
1845
1846void write_device_info_flash(device_info *dev)
1847{
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001848 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
Shashank Mittal162244e2011-08-08 19:01:25 -07001849 struct ptentry *ptn;
1850 struct ptable *ptable;
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001851 if(info == NULL)
1852 {
1853 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
1854 ASSERT(0);
1855 }
1856 info_buf = info;
Shashank Mittal162244e2011-08-08 19:01:25 -07001857 ptable = flash_get_ptable();
1858 if (ptable == NULL)
1859 {
1860 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1861 return;
1862 }
1863
1864 ptn = ptable_find(ptable, "devinfo");
1865 if (ptn == NULL)
1866 {
Smita Ghosh670c8b82015-05-07 09:30:03 -07001867 dprintf(CRITICAL, "ERROR: No devinfo partition found\n");
Shashank Mittal162244e2011-08-08 19:01:25 -07001868 return;
1869 }
1870
1871 memcpy(info, dev, sizeof(device_info));
1872
1873 if (flash_write(ptn, 0, (void *)info_buf, page_size))
1874 {
1875 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1876 return;
1877 }
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001878 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07001879}
1880
vijay kumarc65876c2015-04-24 13:29:16 +05301881static int read_allow_oem_unlock(device_info *dev)
1882{
vijay kumarc65876c2015-04-24 13:29:16 +05301883 unsigned offset;
1884 int index;
1885 unsigned long long ptn;
1886 unsigned long long ptn_size;
1887 unsigned blocksize = mmc_get_device_blocksize();
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07001888 STACKBUF_DMA_ALIGN(buf, blocksize);
vijay kumarc65876c2015-04-24 13:29:16 +05301889
vijay kumarca2e6812015-07-08 20:28:25 +05301890 index = partition_get_index(frp_ptns[0]);
vijay kumarc65876c2015-04-24 13:29:16 +05301891 if (index == INVALID_PTN)
1892 {
vijay kumarca2e6812015-07-08 20:28:25 +05301893 index = partition_get_index(frp_ptns[1]);
1894 if (index == INVALID_PTN)
1895 {
1896 dprintf(CRITICAL, "Neither '%s' nor '%s' partition found\n", frp_ptns[0],frp_ptns[1]);
1897 return -1;
1898 }
vijay kumarc65876c2015-04-24 13:29:16 +05301899 }
1900
1901 ptn = partition_get_offset(index);
1902 ptn_size = partition_get_size(index);
1903 offset = ptn_size - blocksize;
1904
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07001905 if (mmc_read(ptn + offset, (void *)buf, blocksize))
vijay kumarc65876c2015-04-24 13:29:16 +05301906 {
1907 dprintf(CRITICAL, "Reading MMC failed\n");
1908 return -1;
1909 }
1910
1911 /*is_allow_unlock is a bool value stored at the LSB of last byte*/
1912 is_allow_unlock = buf[blocksize-1] & 0x01;
1913 return 0;
1914}
1915
1916static int write_allow_oem_unlock(bool allow_unlock)
1917{
vijay kumarc65876c2015-04-24 13:29:16 +05301918 unsigned offset;
vijay kumarc65876c2015-04-24 13:29:16 +05301919 int index;
1920 unsigned long long ptn;
1921 unsigned long long ptn_size;
1922 unsigned blocksize = mmc_get_device_blocksize();
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07001923 STACKBUF_DMA_ALIGN(buf, blocksize);
vijay kumarc65876c2015-04-24 13:29:16 +05301924
vijay kumarca2e6812015-07-08 20:28:25 +05301925 index = partition_get_index(frp_ptns[0]);
vijay kumarc65876c2015-04-24 13:29:16 +05301926 if (index == INVALID_PTN)
1927 {
vijay kumarca2e6812015-07-08 20:28:25 +05301928 index = partition_get_index(frp_ptns[1]);
1929 if (index == INVALID_PTN)
1930 {
1931 dprintf(CRITICAL, "Neither '%s' nor '%s' partition found\n", frp_ptns[0],frp_ptns[1]);
1932 return -1;
1933 }
vijay kumarc65876c2015-04-24 13:29:16 +05301934 }
1935
1936 ptn = partition_get_offset(index);
1937 ptn_size = partition_get_size(index);
1938 offset = ptn_size - blocksize;
1939
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07001940 if (mmc_read(ptn + offset, (void *)buf, blocksize))
vijay kumarc65876c2015-04-24 13:29:16 +05301941 {
1942 dprintf(CRITICAL, "Reading MMC failed\n");
1943 return -1;
1944 }
1945
1946 /*is_allow_unlock is a bool value stored at the LSB of last byte*/
1947 buf[blocksize-1] = allow_unlock;
1948 if (mmc_write(ptn + offset, blocksize, buf))
1949 {
1950 dprintf(CRITICAL, "Writing MMC failed\n");
1951 return -1;
1952 }
1953
1954 return 0;
1955}
1956
Shashank Mittal162244e2011-08-08 19:01:25 -07001957void read_device_info_flash(device_info *dev)
1958{
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001959 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
Shashank Mittal162244e2011-08-08 19:01:25 -07001960 struct ptentry *ptn;
1961 struct ptable *ptable;
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001962 if(info == NULL)
1963 {
1964 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
1965 ASSERT(0);
1966 }
1967 info_buf = info;
Shashank Mittal162244e2011-08-08 19:01:25 -07001968 ptable = flash_get_ptable();
1969 if (ptable == NULL)
1970 {
1971 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1972 return;
1973 }
1974
1975 ptn = ptable_find(ptable, "devinfo");
1976 if (ptn == NULL)
1977 {
Smita Ghosh670c8b82015-05-07 09:30:03 -07001978 dprintf(CRITICAL, "ERROR: No devinfo partition found\n");
Shashank Mittal162244e2011-08-08 19:01:25 -07001979 return;
1980 }
1981
1982 if (flash_read(ptn, 0, (void *)info_buf, page_size))
1983 {
1984 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1985 return;
1986 }
1987
1988 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
1989 {
Shashank Mittal162244e2011-08-08 19:01:25 -07001990 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
1991 info->is_unlocked = 0;
Shashank Mittala0032282011-08-26 14:50:11 -07001992 info->is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001993 write_device_info_flash(info);
1994 }
1995 memcpy(dev, info, sizeof(device_info));
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001996 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07001997}
1998
1999void write_device_info(device_info *dev)
2000{
2001 if(target_is_emmc_boot())
2002 {
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002003 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
2004 if(info == NULL)
2005 {
2006 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
2007 ASSERT(0);
2008 }
2009 info_buf = info;
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002010 memcpy(info, dev, sizeof(struct device_info));
2011
2012#if USE_RPMB_FOR_DEVINFO
Sridhar Parasurame1a97922015-07-27 15:31:22 -07002013 if (is_secure_boot_enable()) {
2014 if((write_device_info_rpmb((void*) info, PAGE_SIZE)) < 0)
2015 ASSERT(0);
2016 }
Channagoud Kadabic80cb492015-04-28 16:08:28 -07002017 else
2018 write_device_info_mmc(info);
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002019#else
2020 write_device_info_mmc(info);
2021#endif
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002022 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07002023 }
2024 else
2025 {
2026 write_device_info_flash(dev);
2027 }
2028}
2029
2030void read_device_info(device_info *dev)
2031{
2032 if(target_is_emmc_boot())
2033 {
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002034 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
2035 if(info == NULL)
2036 {
2037 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
2038 ASSERT(0);
2039 }
2040 info_buf = info;
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002041
2042#if USE_RPMB_FOR_DEVINFO
Sridhar Parasurame1a97922015-07-27 15:31:22 -07002043 if (is_secure_boot_enable()) {
2044 if((read_device_info_rpmb((void*) info, PAGE_SIZE)) < 0)
2045 ASSERT(0);
2046 }
Channagoud Kadabic80cb492015-04-28 16:08:28 -07002047 else
2048 read_device_info_mmc(info);
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002049#else
2050 read_device_info_mmc(info);
2051#endif
2052
2053 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
2054 {
2055 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
lijuang511a2b52015-08-14 20:50:51 +08002056 if (is_secure_boot_enable()) {
Channagoud Kadabi05f78ba2015-07-06 11:58:14 -07002057 info->is_unlocked = 0;
Parth Dixitddbc7352015-10-18 03:13:31 +05302058#if !VBOOT_MOTA
lijuang511a2b52015-08-14 20:50:51 +08002059 info->is_unlock_critical = 0;
Parth Dixitddbc7352015-10-18 03:13:31 +05302060#endif
lijuang511a2b52015-08-14 20:50:51 +08002061 } else {
Channagoud Kadabi2fda4092015-07-07 13:34:11 -07002062 info->is_unlocked = 1;
Parth Dixitddbc7352015-10-18 03:13:31 +05302063#if !VBOOT_MOTA
lijuang511a2b52015-08-14 20:50:51 +08002064 info->is_unlock_critical = 1;
Parth Dixitddbc7352015-10-18 03:13:31 +05302065#endif
lijuang511a2b52015-08-14 20:50:51 +08002066 }
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002067 info->is_tampered = 0;
2068 info->charger_screen_enabled = 0;
Parth Dixitddbc7352015-10-18 03:13:31 +05302069#if !VBOOT_MOTA
Sridhar Parasuram287e5062015-08-24 16:17:22 -07002070 info->verity_mode = 1; //enforcing by default
Parth Dixitddbc7352015-10-18 03:13:31 +05302071#endif
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002072 write_device_info(info);
2073 }
2074 memcpy(dev, info, sizeof(device_info));
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002075 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07002076 }
2077 else
2078 {
2079 read_device_info_flash(dev);
2080 }
2081}
2082
2083void reset_device_info()
2084{
2085 dprintf(ALWAYS, "reset_device_info called.");
Shashank Mittala0032282011-08-26 14:50:11 -07002086 device.is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07002087 write_device_info(&device);
2088}
2089
2090void set_device_root()
2091{
2092 dprintf(ALWAYS, "set_device_root called.");
Shashank Mittala0032282011-08-26 14:50:11 -07002093 device.is_tampered = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -07002094 write_device_info(&device);
2095}
2096
lijuang4ece1e72015-08-14 21:02:36 +08002097/* set device unlock value
2098 * Must check FRP before call this function
2099 * Need to wipe data when unlock status changed
2100 * type 0: oem unlock
2101 * type 1: unlock critical
2102 * status 0: unlock as false
2103 * status 1: lock as true
2104 */
2105void set_device_unlock_value(int type, bool status)
lijuang21f12f52015-08-22 16:22:19 +08002106{
lijuang4ece1e72015-08-14 21:02:36 +08002107 if (type == UNLOCK)
2108 device.is_unlocked = status;
Parth Dixitddbc7352015-10-18 03:13:31 +05302109#if !VBOOT_MOTA
lijuang4ece1e72015-08-14 21:02:36 +08002110 else if (type == UNLOCK_CRITICAL)
2111 device.is_unlock_critical = status;
Parth Dixitddbc7352015-10-18 03:13:31 +05302112#endif
lijuang4ece1e72015-08-14 21:02:36 +08002113 write_device_info(&device);
2114}
2115
2116static void set_device_unlock(int type, bool status)
2117{
2118 int is_unlocked = -1;
2119 char response[MAX_RSP_SIZE];
2120
2121 /* check device unlock status if it is as expected */
2122 if (type == UNLOCK)
2123 is_unlocked = device.is_unlocked;
Parth Dixitddbc7352015-10-18 03:13:31 +05302124#if !VBOOT_MOTA
lijuang4ece1e72015-08-14 21:02:36 +08002125 else if (type == UNLOCK_CRITICAL)
2126 is_unlocked = device.is_unlock_critical;
Parth Dixitddbc7352015-10-18 03:13:31 +05302127#endif
lijuang4ece1e72015-08-14 21:02:36 +08002128 if (is_unlocked == status) {
2129 snprintf(response, sizeof(response), "\tDevice already : %s", (status ? "unlocked!" : "locked!"));
2130 fastboot_info(response);
2131 fastboot_okay("");
2132 return;
2133 }
2134
2135 /* status is true, it means to unlock device */
2136 if (status) {
lijuang21f12f52015-08-22 16:22:19 +08002137 if(!is_allow_unlock) {
2138 fastboot_fail("oem unlock is not allowed");
2139 return;
2140 }
2141
lijuang4ece1e72015-08-14 21:02:36 +08002142#if FBCON_DISPLAY_MSG
lijuang9a7d3b92015-11-30 14:41:24 +08002143 display_unlock_menu(type);
lijuang4ece1e72015-08-14 21:02:36 +08002144 fastboot_okay("");
2145 return;
2146#else
2147 if (type == UNLOCK) {
2148 fastboot_fail("Need wipe userdata. Do 'fastboot oem unlock-go'");
2149 return;
2150 }
2151#endif
lijuang21f12f52015-08-22 16:22:19 +08002152 }
lijuang4ece1e72015-08-14 21:02:36 +08002153
2154 set_device_unlock_value(type, status);
2155
2156 /* wipe data */
2157 struct recovery_message msg;
2158
2159 snprintf(msg.recovery, sizeof(msg.recovery), "recovery\n--wipe_data");
2160 write_misc(0, &msg, sizeof(msg));
2161
2162 fastboot_okay("");
2163 reboot_device(RECOVERY_MODE);
lijuang21f12f52015-08-22 16:22:19 +08002164}
2165
lijuang511a2b52015-08-14 20:50:51 +08002166static bool critical_flash_allowed(const char * entry)
2167{
2168 uint32_t i = 0;
2169 if (entry == NULL)
2170 return false;
2171
2172 for (i = 0; i < ARRAY_SIZE(critical_flash_allowed_ptn); i++) {
2173 if(!strcmp(entry, critical_flash_allowed_ptn[i]))
2174 return true;
2175 }
2176 return false;
Matthew Qin271927e2015-03-31 22:07:07 -04002177}
2178
2179#if DEVICE_TREE
Amol Jadicb524072012-08-09 16:40:18 -07002180int copy_dtb(uint8_t *boot_image_start, unsigned int scratch_offset)
2181{
2182 uint32 dt_image_offset = 0;
Amol Jadicb524072012-08-09 16:40:18 -07002183 uint32_t n;
Amol Jadicb524072012-08-09 16:40:18 -07002184 struct dt_table *table;
2185 struct dt_entry dt_entry;
2186 uint32_t dt_hdr_size;
2187 unsigned int compressed_size = 0;
2188 unsigned int dtb_size = 0;
2189 unsigned int out_avai_len = 0;
2190 unsigned char *out_addr = NULL;
2191 unsigned char *best_match_dt_addr = NULL;
2192 int rc;
2193
2194 struct boot_img_hdr *hdr = (struct boot_img_hdr *) (boot_image_start);
2195
2196 if(hdr->dt_size != 0) {
2197 /* add kernel offset */
2198 dt_image_offset += page_size;
2199 n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07002200 dt_image_offset += n;
Amol Jadicb524072012-08-09 16:40:18 -07002201
Deepa Dinamani19648b42013-09-05 17:05:55 -07002202 /* add ramdisk offset */
Amol Jadicb524072012-08-09 16:40:18 -07002203 n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
2204 dt_image_offset += n;
2205
Joel Kingaa335dc2013-06-03 16:11:08 -07002206 /* add second offset */
2207 if(hdr->second_size != 0) {
Amol Jadicb524072012-08-09 16:40:18 -07002208 n = ROUND_TO_PAGE(hdr->second_size, page_mask);
2209 dt_image_offset += n;
2210 }
2211
Matthew Qin271927e2015-03-31 22:07:07 -04002212 /* offset now point to start of dt.img */
2213 table = (struct dt_table*)(boot_image_start + dt_image_offset);
2214
2215 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
2216 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
Matthew Qin0b15b322015-05-19 05:20:54 -04002217 return -1;
Matthew Qin271927e2015-03-31 22:07:07 -04002218 }
P.V. Phani Kumar82916762016-03-09 09:20:19 +05302219
2220 /* Its Error if, dt_hdr_size (table->num_entries * dt_entry size + Dev_Tree Header)
2221 goes beyound hdr->dt_size*/
2222 if (dt_hdr_size > ROUND_TO_PAGE(hdr->dt_size,hdr->page_size)) {
2223 dprintf(CRITICAL, "ERROR: Invalid Device Tree size \n");
2224 return -1;
2225 }
2226
Amol Jadicb524072012-08-09 16:40:18 -07002227 /* Find index of device tree within device tree table */
2228 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
2229 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
2230 return -1;
2231 }
2232
Matthew Qin271927e2015-03-31 22:07:07 -04002233 best_match_dt_addr = (unsigned char *)boot_image_start + dt_image_offset + dt_entry.offset;
2234 if (is_gzip_package(best_match_dt_addr, dt_entry.size))
2235 {
2236 out_addr = (unsigned char *)target_get_scratch_address() + scratch_offset;
2237 out_avai_len = target_get_max_flash_size() - scratch_offset;
2238 dprintf(INFO, "decompressing dtb: start\n");
2239 rc = decompress(best_match_dt_addr,
2240 dt_entry.size, out_addr, out_avai_len,
2241 &compressed_size, &dtb_size);
2242 if (rc)
2243 {
Matthew Qin0b15b322015-05-19 05:20:54 -04002244 dprintf(CRITICAL, "decompressing dtb failed!!!\n");
Matthew Qin271927e2015-03-31 22:07:07 -04002245 ASSERT(0);
2246 }
2247
Matthew Qin0b15b322015-05-19 05:20:54 -04002248 dprintf(INFO, "decompressing dtb: done\n");
Matthew Qin271927e2015-03-31 22:07:07 -04002249 best_match_dt_addr = out_addr;
2250 } else {
2251 dtb_size = dt_entry.size;
2252 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002253 /* Validate and Read device device tree in the "tags_add */
Matthew Qin271927e2015-03-31 22:07:07 -04002254 if (check_aboot_addr_range_overlap(hdr->tags_addr, dtb_size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002255 {
2256 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
2257 return -1;
2258 }
2259
Amol Jadicb524072012-08-09 16:40:18 -07002260 /* Read device device tree in the "tags_add */
Matthew Qin271927e2015-03-31 22:07:07 -04002261 memmove((void*) hdr->tags_addr, (void *)best_match_dt_addr, dtb_size);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002262 } else
2263 return -1;
Amol Jadicb524072012-08-09 16:40:18 -07002264
2265 /* Everything looks fine. Return success. */
2266 return 0;
2267}
2268#endif
2269
Brian Swetland9c4c0752009-01-25 16:23:50 -08002270void cmd_boot(const char *arg, void *data, unsigned sz)
2271{
Amit Blay8a510302015-08-17 09:20:01 +03002272#ifdef MDTP_SUPPORT
2273 static bool is_mdtp_activated = 0;
2274#endif /* MDTP_SUPPORT */
Brian Swetland9c4c0752009-01-25 16:23:50 -08002275 unsigned kernel_actual;
2276 unsigned ramdisk_actual;
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002277 uint32_t image_actual;
2278 uint32_t dt_actual = 0;
P.V. Phani Kumareecfd822016-03-09 20:09:03 +05302279 uint32_t sig_actual = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08002280 struct boot_img_hdr *hdr = NULL;
2281 struct kernel64_hdr *kptr = NULL;
Brian Swetland9c4c0752009-01-25 16:23:50 -08002282 char *ptr = ((char*) data);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002283 int ret = 0;
2284 uint8_t dtb_copied = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08002285 unsigned int out_len = 0;
2286 unsigned int out_avai_len = 0;
2287 unsigned char *out_addr = NULL;
2288 uint32_t dtb_offset = 0;
2289 unsigned char *kernel_start_addr = NULL;
2290 unsigned int kernel_size = 0;
Matthew Qin271927e2015-03-31 22:07:07 -04002291 unsigned int scratch_offset = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08002292
lijuang2008ff22016-03-07 17:56:27 +08002293#if FBCON_DISPLAY_MSG
2294 /* Exit keys' detection thread firstly */
2295 exit_menu_keys_detection();
2296#endif
2297
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002298#if VERIFIED_BOOT
Channagoud Kadabi6d5375e2015-06-23 17:15:42 -07002299 if(target_build_variant_user() && !device.is_unlocked)
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002300 {
2301 fastboot_fail("unlock device to use this command");
lijuang2008ff22016-03-07 17:56:27 +08002302 goto boot_failed;
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002303 }
2304#endif
2305
Brian Swetland9c4c0752009-01-25 16:23:50 -08002306 if (sz < sizeof(hdr)) {
2307 fastboot_fail("invalid bootimage header");
lijuang2008ff22016-03-07 17:56:27 +08002308 goto boot_failed;
Brian Swetland9c4c0752009-01-25 16:23:50 -08002309 }
2310
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002311 hdr = (struct boot_img_hdr *)data;
Brian Swetland9c4c0752009-01-25 16:23:50 -08002312
2313 /* ensure commandline is terminated */
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002314 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -08002315
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002316 if(target_is_emmc_boot() && hdr->page_size) {
2317 page_size = hdr->page_size;
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07002318 page_mask = page_size - 1;
2319 }
2320
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002321 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
2322 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002323#if DEVICE_TREE
2324 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
2325#endif
2326
2327 image_actual = ADD_OF(page_size, kernel_actual);
2328 image_actual = ADD_OF(image_actual, ramdisk_actual);
2329 image_actual = ADD_OF(image_actual, dt_actual);
2330
P.V. Phani Kumareecfd822016-03-09 20:09:03 +05302331 if (target_use_signed_kernel() && (!device.is_unlocked)) {
2332 /* Calculate the signature length from boot image */
2333 sig_actual = read_der_message_length(
2334 (unsigned char*)(data + image_actual),sz);
Channagoud Kadabi3d839012014-06-26 14:26:39 -07002335 image_actual = ADD_OF(image_actual, sig_actual);
P.V. Phani Kumareecfd822016-03-09 20:09:03 +05302336 }
Channagoud Kadabi3d839012014-06-26 14:26:39 -07002337
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002338 /* sz should have atleast raw boot image */
2339 if (image_actual > sz) {
Channagoud Kadabi3d839012014-06-26 14:26:39 -07002340 fastboot_fail("bootimage: incomplete or not signed");
lijuang2008ff22016-03-07 17:56:27 +08002341 goto boot_failed;
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002342 }
2343
Sridhar Parasuram361e4cd2015-09-24 11:19:19 -07002344 // Initialize boot state before trying to verify boot.img
2345#if VERIFIED_BOOT
Channagoud Kadabi699466e2015-11-03 12:37:42 -08002346 boot_verifier_init();
Channagoud Kadabibf6ce7d2015-09-17 13:25:35 -07002347 /* Handle overflow if the input image size is greater than
2348 * boot image buffer can hold
2349 */
2350 if ((target_get_max_flash_size() - (image_actual - sig_actual)) < page_size)
2351 {
2352 fastboot_fail("booimage: size is greater than boot image buffer can hold");
lijuang2008ff22016-03-07 17:56:27 +08002353 goto boot_failed;
Channagoud Kadabibf6ce7d2015-09-17 13:25:35 -07002354 }
Sridhar Parasuram361e4cd2015-09-24 11:19:19 -07002355#endif
2356
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002357 /* Verify the boot image
2358 * device & page_size are initialized in aboot_init
2359 */
Sridhar Parasuram7ae9fbc2015-09-17 09:56:30 -07002360 if (target_use_signed_kernel() && (!device.is_unlocked)) {
Channagoud Kadabi3d839012014-06-26 14:26:39 -07002361 /* Pass size excluding signature size, otherwise we would try to
2362 * access signature beyond its length
2363 */
2364 verify_signed_bootimg((uint32_t)data, (image_actual - sig_actual));
Sridhar Parasuram7ae9fbc2015-09-17 09:56:30 -07002365 }
Amit Blay4aa292f2015-04-28 21:55:59 +03002366#ifdef MDTP_SUPPORT
2367 else
2368 {
Shay Nachmani062aa3f2015-05-17 17:28:44 +03002369 /* fastboot boot is not allowed when MDTP is activated */
Amit Blay4aa292f2015-04-28 21:55:59 +03002370 mdtp_ext_partition_verification_t ext_partition;
Amit Blay8a510302015-08-17 09:20:01 +03002371
2372 if (!is_mdtp_activated) {
2373 ext_partition.partition = MDTP_PARTITION_NONE;
2374 mdtp_fwlock_verify_lock(&ext_partition);
2375 }
Amit Blay4aa292f2015-04-28 21:55:59 +03002376 }
Shay Nachmani062aa3f2015-05-17 17:28:44 +03002377
Amir Kotzer7c768c02016-04-13 09:08:05 +03002378 /* If mdtp state cannot be validate, block fastboot boot*/
2379 if(mdtp_activated(&is_mdtp_activated)){
2380 dprintf(CRITICAL, "mdtp_activated cannot validate state.\n");
2381 dprintf(CRITICAL, "Can not proceed with fastboot boot command.\n");
2382 goto boot_failed;
2383 }
Shay Nachmani062aa3f2015-05-17 17:28:44 +03002384 if(is_mdtp_activated){
2385 dprintf(CRITICAL, "fastboot boot command is not available.\n");
lijuang2008ff22016-03-07 17:56:27 +08002386 goto boot_failed;
Shay Nachmani062aa3f2015-05-17 17:28:44 +03002387 }
Amit Blay4aa292f2015-04-28 21:55:59 +03002388#endif /* MDTP_SUPPORT */
2389
Sridhar Parasuram361e4cd2015-09-24 11:19:19 -07002390#if VERIFIED_BOOT
Parth Dixitbc9b6492015-10-18 00:41:38 +05302391#if !VBOOT_MOTA
Sridhar Parasuram361e4cd2015-09-24 11:19:19 -07002392 // send root of trust
2393 if(!send_rot_command((uint32_t)device.is_unlocked))
2394 ASSERT(0);
2395#endif
Parth Dixitbc9b6492015-10-18 00:41:38 +05302396#endif
Channagoud Kadabia22144f2013-03-20 11:49:01 -07002397 /*
Matthew Qinbb7923d2015-02-09 10:56:09 +08002398 * Check if the kernel image is a gzip package. If yes, need to decompress it.
2399 * If not, continue booting.
2400 */
2401 if (is_gzip_package((unsigned char *)(data + page_size), hdr->kernel_size))
2402 {
2403 out_addr = (unsigned char *)target_get_scratch_address();
2404 out_addr = (unsigned char *)(out_addr + image_actual + page_size);
2405 out_avai_len = target_get_max_flash_size() - image_actual - page_size;
Matthew Qin0b15b322015-05-19 05:20:54 -04002406 dprintf(INFO, "decompressing kernel image: start\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08002407 ret = decompress((unsigned char *)(ptr + page_size),
2408 hdr->kernel_size, out_addr, out_avai_len,
2409 &dtb_offset, &out_len);
2410 if (ret)
2411 {
Matthew Qin0b15b322015-05-19 05:20:54 -04002412 dprintf(CRITICAL, "decompressing image failed!!!\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08002413 ASSERT(0);
2414 }
2415
Matthew Qin0b15b322015-05-19 05:20:54 -04002416 dprintf(INFO, "decompressing kernel image: done\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08002417 kptr = (struct kernel64_hdr *)out_addr;
2418 kernel_start_addr = out_addr;
2419 kernel_size = out_len;
2420 } else {
2421 kptr = (struct kernel64_hdr*)((char *)data + page_size);
2422 kernel_start_addr = (unsigned char *)((char *)data + page_size);
2423 kernel_size = hdr->kernel_size;
2424 }
2425
2426 /*
Channagoud Kadabia22144f2013-03-20 11:49:01 -07002427 * Update the kernel/ramdisk/tags address if the boot image header
2428 * has default values, these default values come from mkbootimg when
2429 * the boot image is flashed using fastboot flash:raw
2430 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08002431 update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr));
Dima Zavin3cadfff2013-03-21 14:30:48 -07002432
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002433 /* Get virtual addresses since the hdr saves physical addresses. */
2434 hdr->kernel_addr = VA(hdr->kernel_addr);
2435 hdr->ramdisk_addr = VA(hdr->ramdisk_addr);
2436 hdr->tags_addr = VA(hdr->tags_addr);
Brian Swetland9c4c0752009-01-25 16:23:50 -08002437
Matthew Qinbb7923d2015-02-09 10:56:09 +08002438 kernel_size = ROUND_TO_PAGE(kernel_size, page_mask);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002439 /* Check if the addresses in the header are valid. */
Matthew Qinbb7923d2015-02-09 10:56:09 +08002440 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_size) ||
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002441 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
2442 {
2443 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
lijuang2008ff22016-03-07 17:56:27 +08002444 goto boot_failed;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002445 }
2446
Amol Jadicb524072012-08-09 16:40:18 -07002447#if DEVICE_TREE
Matthew Qin271927e2015-03-31 22:07:07 -04002448 scratch_offset = image_actual + page_size + out_len;
Amol Jadicb524072012-08-09 16:40:18 -07002449 /* find correct dtb and copy it to right location */
Matthew Qin271927e2015-03-31 22:07:07 -04002450 ret = copy_dtb(data, scratch_offset);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002451
2452 dtb_copied = !ret ? 1 : 0;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002453#else
2454 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
2455 {
2456 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
lijuang2008ff22016-03-07 17:56:27 +08002457 goto boot_failed;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002458 }
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002459#endif
2460
2461 /* Load ramdisk & kernel */
2462 memmove((void*) hdr->ramdisk_addr, ptr + page_size + kernel_actual, hdr->ramdisk_size);
Matthew Qinbb7923d2015-02-09 10:56:09 +08002463 memmove((void*) hdr->kernel_addr, (char*) (kernel_start_addr), kernel_size);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002464
2465#if DEVICE_TREE
Matthew Qinbb7923d2015-02-09 10:56:09 +08002466 if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual))
2467 {
2468 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
lijuang2008ff22016-03-07 17:56:27 +08002469 goto boot_failed;
Matthew Qinbb7923d2015-02-09 10:56:09 +08002470 }
2471
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002472 /*
2473 * If dtb is not found look for appended DTB in the kernel.
2474 * If appended dev tree is found, update the atags with
2475 * memory address to the DTB appended location on RAM.
2476 * Else update with the atags address in the kernel header
2477 */
2478 if (!dtb_copied) {
2479 void *dtb;
Matthew Qinbb7923d2015-02-09 10:56:09 +08002480 dtb = dev_tree_appended((void*)(ptr + page_size),
2481 hdr->kernel_size, dtb_offset,
Dima Zavine63e5572013-05-03 12:23:06 -07002482 (void *)hdr->tags_addr);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002483 if (!dtb) {
2484 fastboot_fail("dtb not found");
lijuang2008ff22016-03-07 17:56:27 +08002485 goto boot_failed;
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002486 }
Amol Jadicb524072012-08-09 16:40:18 -07002487 }
2488#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08002489
2490 fastboot_okay("");
Amol Jadi7c4316c2013-10-07 14:19:26 -07002491 fastboot_stop();
Brian Swetland9c4c0752009-01-25 16:23:50 -08002492
Dima Zavin77e41f32013-03-06 16:10:43 -08002493 boot_linux((void*) hdr->kernel_addr, (void*) hdr->tags_addr,
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002494 (const char*) hdr->cmdline, board_machtype(),
2495 (void*) hdr->ramdisk_addr, hdr->ramdisk_size);
lijuang2008ff22016-03-07 17:56:27 +08002496
2497 /* fastboot already stop, it's no need to show fastboot menu */
2498 return;
2499boot_failed:
2500#if FBCON_DISPLAY_MSG
2501 /* revert to fastboot menu if boot failed */
2502 display_fastboot_menu();
2503#endif
2504 return;
Brian Swetland9c4c0752009-01-25 16:23:50 -08002505}
2506
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002507void cmd_erase_nand(const char *arg, void *data, unsigned sz)
Dima Zavin214cc642009-01-26 11:16:21 -08002508{
2509 struct ptentry *ptn;
2510 struct ptable *ptable;
2511
2512 ptable = flash_get_ptable();
2513 if (ptable == NULL) {
2514 fastboot_fail("partition table doesn't exist");
2515 return;
2516 }
2517
2518 ptn = ptable_find(ptable, arg);
2519 if (ptn == NULL) {
2520 fastboot_fail("unknown partition name");
2521 return;
2522 }
2523
2524 if (flash_erase(ptn)) {
2525 fastboot_fail("failed to erase partition");
2526 return;
2527 }
2528 fastboot_okay("");
2529}
2530
Bikas Gurungd48bd242010-09-04 19:54:32 -07002531
2532void cmd_erase_mmc(const char *arg, void *data, unsigned sz)
2533{
2534 unsigned long long ptn = 0;
Oliver Wangcee448d2013-10-22 18:40:13 +08002535 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07002536 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002537 uint8_t lun = 0;
Vijay Kumar Pendoti0b21f462016-05-02 17:09:18 +05302538 char *footer = NULL;
Bikas Gurungd48bd242010-09-04 19:54:32 -07002539
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002540#if VERIFIED_BOOT
2541 if(!strcmp(arg, KEYSTORE_PTN_NAME))
2542 {
2543 if(!device.is_unlocked)
2544 {
2545 fastboot_fail("unlock device to erase keystore");
2546 return;
2547 }
2548 }
2549#endif
2550
Kinson Chikf1a43512011-07-14 11:28:39 -07002551 index = partition_get_index(arg);
2552 ptn = partition_get_offset(index);
Oliver Wangcee448d2013-10-22 18:40:13 +08002553 size = partition_get_size(index);
Neeti Desaica8c9602011-10-06 11:40:00 -07002554
Kinson Chikf1a43512011-07-14 11:28:39 -07002555 if(ptn == 0) {
Neeti Desaica8c9602011-10-06 11:40:00 -07002556 fastboot_fail("Partition table doesn't exist\n");
Bikas Gurungd48bd242010-09-04 19:54:32 -07002557 return;
2558 }
Kun Liang2f1601a2013-08-12 16:29:54 +08002559
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002560 lun = partition_get_lun(index);
2561 mmc_set_lun(lun);
2562
Sridhar Parasuramd7957122015-02-27 11:33:40 -08002563 if (platform_boot_dev_isemmc())
2564 {
2565 if (mmc_erase_card(ptn, size)) {
2566 fastboot_fail("failed to erase partition\n");
2567 return;
2568 }
2569 } else {
2570 BUF_DMA_ALIGN(out, DEFAULT_ERASE_SIZE);
2571 size = partition_get_size(index);
2572 if (size > DEFAULT_ERASE_SIZE)
2573 size = DEFAULT_ERASE_SIZE;
Kun Liang2f1601a2013-08-12 16:29:54 +08002574
Sridhar Parasuramd7957122015-02-27 11:33:40 -08002575 /* Simple inefficient version of erase. Just writing
2576 0 in first several blocks */
2577 if (mmc_write(ptn , size, (unsigned int *)out)) {
2578 fastboot_fail("failed to erase partition");
2579 return;
2580 }
Vijay Kumar Pendoti0b21f462016-05-02 17:09:18 +05302581 /*Erase FDE metadata at the userdata footer*/
2582 if(!(strncmp(arg, "userdata", 8)))
2583 {
2584 footer = memalign(CACHE_LINE, FOOTER_SIZE);
2585 memset((void *)footer, 0, FOOTER_SIZE);
2586
2587 size = partition_get_size(index);
2588
2589 if (mmc_write((ptn + size) - FOOTER_SIZE , FOOTER_SIZE, (unsigned int *)footer)) {
2590 fastboot_fail("failed to erase userdata footer");
2591 free(footer);
2592 return;
2593 }
2594 free(footer);
2595 }
Bikas Gurungd48bd242010-09-04 19:54:32 -07002596 }
Sridhar Parasuram32b30662015-07-10 13:33:22 -07002597#if VERIFIED_BOOT
Parth Dixitbc9b6492015-10-18 00:41:38 +05302598#if !VBOOT_MOTA
Sridhar Parasuram32b30662015-07-10 13:33:22 -07002599 if(!(strncmp(arg, "userdata", 8)))
2600 if(send_delete_keys_to_tz())
2601 ASSERT(0);
2602#endif
Parth Dixitbc9b6492015-10-18 00:41:38 +05302603#endif
Bikas Gurungd48bd242010-09-04 19:54:32 -07002604 fastboot_okay("");
2605}
2606
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002607void cmd_erase(const char *arg, void *data, unsigned sz)
2608{
Channagoud Kadabice3cf422015-04-17 16:02:30 -07002609#if VERIFIED_BOOT
Channagoud Kadabi35297672015-06-13 11:09:49 -07002610 if (target_build_variant_user())
Channagoud Kadabice3cf422015-04-17 16:02:30 -07002611 {
Sridhar Parasuramc32d07d2015-07-12 10:57:48 -07002612 if(!device.is_unlocked)
Channagoud Kadabice3cf422015-04-17 16:02:30 -07002613 {
Channagoud Kadabi35297672015-06-13 11:09:49 -07002614 fastboot_fail("device is locked. Cannot erase");
Channagoud Kadabice3cf422015-04-17 16:02:30 -07002615 return;
2616 }
2617 }
2618#endif
2619
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002620 if(target_is_emmc_boot())
2621 cmd_erase_mmc(arg, data, sz);
2622 else
2623 cmd_erase_nand(arg, data, sz);
2624}
Bikas Gurungd48bd242010-09-04 19:54:32 -07002625
Channagoud Kadabiad259832015-05-29 11:14:17 -07002626static uint32_t aboot_get_secret_key()
2627{
2628 /* 0 is invalid secret key, update this implementation to return
2629 * device specific unique secret key
2630 */
2631 return 0;
2632}
2633
Ajay Dudani5c761132011-04-07 20:19:04 -07002634void cmd_flash_mmc_img(const char *arg, void *data, unsigned sz)
Shashank Mittal23b8f422010-04-16 19:27:21 -07002635{
2636 unsigned long long ptn = 0;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07002637 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07002638 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002639 char *token = NULL;
2640 char *pname = NULL;
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07002641 char *sp;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002642 uint8_t lun = 0;
2643 bool lun_set = false;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07002644
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07002645 token = strtok_r((char *)arg, ":", &sp);
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002646 pname = token;
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07002647 token = strtok_r(NULL, ":", &sp);
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002648 if(token)
2649 {
2650 lun = atoi(token);
2651 mmc_set_lun(lun);
2652 lun_set = true;
2653 }
2654
Mao Jinlong226f33a2014-07-04 17:24:10 +08002655 if (pname)
Greg Grisco6e754772011-06-23 12:19:39 -07002656 {
Channagoud Kadabiad259832015-05-29 11:14:17 -07002657 if (!strncmp(pname, "frp-unlock", strlen("frp-unlock")))
2658 {
2659 if (!aboot_frp_unlock(pname, data, sz))
2660 {
2661 fastboot_info("FRP unlock successful");
2662 fastboot_okay("");
2663 }
2664 else
2665 fastboot_fail("Secret key is invalid, please update the bootloader with secret key");
2666
2667 return;
2668 }
2669
Mao Jinlong226f33a2014-07-04 17:24:10 +08002670 if (!strcmp(pname, "partition"))
2671 {
2672 dprintf(INFO, "Attempt to write partition image.\n");
2673 if (write_partition(sz, (unsigned char *) data)) {
2674 fastboot_fail("failed to write partition");
Greg Grisco6e754772011-06-23 12:19:39 -07002675 return;
2676 }
2677 }
Mao Jinlong226f33a2014-07-04 17:24:10 +08002678 else
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002679 {
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002680#if VERIFIED_BOOT
2681 if(!strcmp(pname, KEYSTORE_PTN_NAME))
2682 {
2683 if(!device.is_unlocked)
2684 {
2685 fastboot_fail("unlock device to flash keystore");
2686 return;
2687 }
Gaurav Nebhwanic7313cc2015-12-15 22:25:04 +05302688 if(!boot_verify_validate_keystore((unsigned char *)data,sz))
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002689 {
2690 fastboot_fail("image is not a keystore file");
2691 return;
2692 }
2693 }
2694#endif
Mao Jinlong226f33a2014-07-04 17:24:10 +08002695 index = partition_get_index(pname);
2696 ptn = partition_get_offset(index);
2697 if(ptn == 0) {
2698 fastboot_fail("partition table doesn't exist");
2699 return;
2700 }
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002701
Mao Jinlong226f33a2014-07-04 17:24:10 +08002702 if (!strcmp(pname, "boot") || !strcmp(pname, "recovery")) {
2703 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
2704 fastboot_fail("image is not a boot image");
2705 return;
2706 }
2707 }
2708
2709 if(!lun_set)
2710 {
2711 lun = partition_get_lun(index);
2712 mmc_set_lun(lun);
2713 }
2714
2715 size = partition_get_size(index);
Gaurav Nebhwani4d2389c2016-03-17 21:10:05 +05302716 if (ROUND_TO_PAGE(sz, mmc_blocksize_mask) > size) {
Mao Jinlong226f33a2014-07-04 17:24:10 +08002717 fastboot_fail("size too large");
2718 return;
2719 }
2720 else if (mmc_write(ptn , sz, (unsigned int *)data)) {
2721 fastboot_fail("flash write failure");
2722 return;
2723 }
Greg Grisco6e754772011-06-23 12:19:39 -07002724 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07002725 }
2726 fastboot_okay("");
2727 return;
2728}
2729
Ajay Dudanide984792015-03-02 09:57:41 -08002730void cmd_flash_meta_img(const char *arg, void *data, unsigned sz)
2731{
2732 int i, images;
2733 meta_header_t *meta_header;
2734 img_header_entry_t *img_header_entry;
Parth Dixit3e2d3032016-03-04 17:11:52 +05302735 /*End of the image address*/
2736 uintptr_t data_end;
2737
2738 if( (UINT_MAX - sz) > (uintptr_t)data )
2739 data_end = (uintptr_t)data + sz;
2740 else
2741 {
2742 fastboot_fail("Cannot flash: image header corrupt");
2743 return;
2744 }
2745
2746 if( data_end < ((uintptr_t)data + sizeof(meta_header_t)))
2747 {
2748 fastboot_fail("Cannot flash: image header corrupt");
2749 return;
2750 }
Ajay Dudanide984792015-03-02 09:57:41 -08002751
lijuang8ee21882016-04-19 16:57:11 +08002752 /* If device is locked:
2753 * Forbid to flash image to avoid the device to bypass the image
2754 * which with "any" name other than bootloader. Because it maybe
2755 * a meta package of all partitions.
2756 */
2757#if VERIFIED_BOOT
2758 if (target_build_variant_user()) {
2759 if (!device.is_unlocked) {
2760 fastboot_fail("Device is locked, meta image flashing is not allowed");
2761 return;
2762 }
2763#if !VBOOT_MOTA
2764 if(!device.is_unlock_critical) {
2765 fastboot_fail("Device is critical locked, Meta image flashing is not allowed");
2766 return;
2767 }
2768#endif
2769 }
2770#endif
2771
Ajay Dudanide984792015-03-02 09:57:41 -08002772 meta_header = (meta_header_t*) data;
Parth Dixit3e2d3032016-03-04 17:11:52 +05302773 if( data_end < ((uintptr_t)data + meta_header->img_hdr_sz))
2774 {
2775 fastboot_fail("Cannot flash: image header corrupt");
2776 return;
2777 }
Ajay Dudanide984792015-03-02 09:57:41 -08002778 img_header_entry = (img_header_entry_t*) (data+sizeof(meta_header_t));
2779
2780 images = meta_header->img_hdr_sz / sizeof(img_header_entry_t);
2781
2782 for (i=0; i<images; i++) {
2783
2784 if((img_header_entry[i].ptn_name == NULL) ||
2785 (img_header_entry[i].start_offset == 0) ||
2786 (img_header_entry[i].size == 0))
2787 break;
2788
Parth Dixit3e2d3032016-03-04 17:11:52 +05302789 if( data_end < ((uintptr_t)data + img_header_entry[i].start_offset
2790 + img_header_entry[i].size) )
2791 {
2792 fastboot_fail("Cannot flash: image size mismatch");
2793 break;
2794 }
2795
Ajay Dudanide984792015-03-02 09:57:41 -08002796 cmd_flash_mmc_img(img_header_entry[i].ptn_name,
2797 (void *) data + img_header_entry[i].start_offset,
2798 img_header_entry[i].size);
2799 }
2800
Channagoud Kadabiaafbbfc2015-03-04 17:52:56 -08002801 if (!strncmp(arg, "bootloader", strlen("bootloader")))
2802 {
2803 strlcpy(device.bootloader_version, TARGET(BOARD), MAX_VERSION_LEN);
2804 strlcat(device.bootloader_version, "-", MAX_VERSION_LEN);
2805 strlcat(device.bootloader_version, meta_header->img_version, MAX_VERSION_LEN);
2806 }
2807 else
2808 {
2809 strlcpy(device.radio_version, TARGET(BOARD), MAX_VERSION_LEN);
2810 strlcat(device.radio_version, "-", MAX_VERSION_LEN);
2811 strlcat(device.radio_version, meta_header->img_version, MAX_VERSION_LEN);
2812 }
2813
2814 write_device_info(&device);
Ajay Dudanide984792015-03-02 09:57:41 -08002815 fastboot_okay("");
2816 return;
2817}
2818
Ajay Dudani5c761132011-04-07 20:19:04 -07002819void cmd_flash_mmc_sparse_img(const char *arg, void *data, unsigned sz)
2820{
2821 unsigned int chunk;
wufeng.jiang813dc352015-06-02 23:02:46 -04002822 uint64_t chunk_data_sz;
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002823 uint32_t *fill_buf = NULL;
2824 uint32_t fill_val;
Ajay Dudani5c761132011-04-07 20:19:04 -07002825 sparse_header_t *sparse_header;
2826 chunk_header_t *chunk_header;
Ajay Dudaniab18f022011-05-12 14:39:22 -07002827 uint32_t total_blocks = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07002828 unsigned long long ptn = 0;
Channagoud Kadabi65b91002011-10-11 17:34:33 +05302829 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07002830 int index = INVALID_PTN;
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05302831 uint32_t i;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002832 uint8_t lun = 0;
vijay kumar800255e2015-04-24 20:26:19 +05302833 /*End of the sparse image address*/
Parth Dixit64b1a482016-03-07 16:31:26 +05302834 uintptr_t data_end = (uintptr_t)data + sz;
Ajay Dudani5c761132011-04-07 20:19:04 -07002835
Kinson Chikf1a43512011-07-14 11:28:39 -07002836 index = partition_get_index(arg);
2837 ptn = partition_get_offset(index);
2838 if(ptn == 0) {
Ajay Dudani5c761132011-04-07 20:19:04 -07002839 fastboot_fail("partition table doesn't exist");
2840 return;
2841 }
2842
Channagoud Kadabi65b91002011-10-11 17:34:33 +05302843 size = partition_get_size(index);
Channagoud Kadabi65b91002011-10-11 17:34:33 +05302844
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002845 lun = partition_get_lun(index);
2846 mmc_set_lun(lun);
2847
vijay kumar800255e2015-04-24 20:26:19 +05302848 if (sz < sizeof(sparse_header_t)) {
2849 fastboot_fail("size too low");
2850 return;
2851 }
2852
Ajay Dudani5c761132011-04-07 20:19:04 -07002853 /* Read and skip over sparse image header */
2854 sparse_header = (sparse_header_t *) data;
vijay kumar800255e2015-04-24 20:26:19 +05302855
vijay kumar1321f342015-03-27 12:13:42 +05302856 if (((uint64_t)sparse_header->total_blks * (uint64_t)sparse_header->blk_sz) > size) {
Ajay Dudani876b3282012-12-21 14:12:17 -08002857 fastboot_fail("size too large");
2858 return;
2859 }
2860
vijay kumarde4fcf62015-04-23 13:05:49 +05302861 data += sizeof(sparse_header_t);
vijay kumar800255e2015-04-24 20:26:19 +05302862
Parth Dixit64b1a482016-03-07 16:31:26 +05302863 if (data_end < (uintptr_t)data) {
vijay kumar800255e2015-04-24 20:26:19 +05302864 fastboot_fail("buffer overreads occured due to invalid sparse header");
2865 return;
2866 }
2867
vijay kumarde4fcf62015-04-23 13:05:49 +05302868 if(sparse_header->file_hdr_sz != sizeof(sparse_header_t))
Ajay Dudani5c761132011-04-07 20:19:04 -07002869 {
vijay kumarde4fcf62015-04-23 13:05:49 +05302870 fastboot_fail("sparse header size mismatch");
2871 return;
Ajay Dudani5c761132011-04-07 20:19:04 -07002872 }
2873
Ajay Dudanib06c05f2011-05-12 14:46:10 -07002874 dprintf (SPEW, "=== Sparse Image Header ===\n");
2875 dprintf (SPEW, "magic: 0x%x\n", sparse_header->magic);
2876 dprintf (SPEW, "major_version: 0x%x\n", sparse_header->major_version);
2877 dprintf (SPEW, "minor_version: 0x%x\n", sparse_header->minor_version);
2878 dprintf (SPEW, "file_hdr_sz: %d\n", sparse_header->file_hdr_sz);
2879 dprintf (SPEW, "chunk_hdr_sz: %d\n", sparse_header->chunk_hdr_sz);
2880 dprintf (SPEW, "blk_sz: %d\n", sparse_header->blk_sz);
2881 dprintf (SPEW, "total_blks: %d\n", sparse_header->total_blks);
2882 dprintf (SPEW, "total_chunks: %d\n", sparse_header->total_chunks);
Ajay Dudani5c761132011-04-07 20:19:04 -07002883
2884 /* Start processing chunks */
2885 for (chunk=0; chunk<sparse_header->total_chunks; chunk++)
2886 {
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302887 /* Make sure the total image size does not exceed the partition size */
2888 if(((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz) >= size) {
2889 fastboot_fail("size too large");
2890 return;
2891 }
Ajay Dudani5c761132011-04-07 20:19:04 -07002892 /* Read and skip over chunk header */
2893 chunk_header = (chunk_header_t *) data;
2894 data += sizeof(chunk_header_t);
2895
Parth Dixit64b1a482016-03-07 16:31:26 +05302896 if (data_end < (uintptr_t)data) {
vijay kumar800255e2015-04-24 20:26:19 +05302897 fastboot_fail("buffer overreads occured due to invalid sparse header");
2898 return;
2899 }
2900
Ajay Dudani5c761132011-04-07 20:19:04 -07002901 dprintf (SPEW, "=== Chunk Header ===\n");
2902 dprintf (SPEW, "chunk_type: 0x%x\n", chunk_header->chunk_type);
2903 dprintf (SPEW, "chunk_data_sz: 0x%x\n", chunk_header->chunk_sz);
2904 dprintf (SPEW, "total_size: 0x%x\n", chunk_header->total_sz);
2905
vijay kumar800255e2015-04-24 20:26:19 +05302906 if(sparse_header->chunk_hdr_sz != sizeof(chunk_header_t))
Ajay Dudani5c761132011-04-07 20:19:04 -07002907 {
vijay kumar800255e2015-04-24 20:26:19 +05302908 fastboot_fail("chunk header size mismatch");
2909 return;
Ajay Dudani5c761132011-04-07 20:19:04 -07002910 }
2911
wufeng.jiang813dc352015-06-02 23:02:46 -04002912 if (!sparse_header->blk_sz ){
2913 fastboot_fail("Invalid block size\n");
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302914 return;
2915 }
2916
wufeng.jiang813dc352015-06-02 23:02:46 -04002917 chunk_data_sz = (uint64_t)sparse_header->blk_sz * chunk_header->chunk_sz;
2918
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302919 /* Make sure that the chunk size calculated from sparse image does not
2920 * exceed partition size
2921 */
2922 if ((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz + chunk_data_sz > size)
2923 {
2924 fastboot_fail("Chunk data size exceeds partition size");
2925 return;
2926 }
2927
Ajay Dudani5c761132011-04-07 20:19:04 -07002928 switch (chunk_header->chunk_type)
2929 {
2930 case CHUNK_TYPE_RAW:
wufeng.jiang813dc352015-06-02 23:02:46 -04002931 if((uint64_t)chunk_header->total_sz != ((uint64_t)sparse_header->chunk_hdr_sz +
Ajay Dudani5c761132011-04-07 20:19:04 -07002932 chunk_data_sz))
2933 {
2934 fastboot_fail("Bogus chunk size for chunk type Raw");
2935 return;
2936 }
2937
Parth Dixit64b1a482016-03-07 16:31:26 +05302938 if (data_end < (uintptr_t)data + chunk_data_sz) {
vijay kumar800255e2015-04-24 20:26:19 +05302939 fastboot_fail("buffer overreads occured due to invalid sparse header");
2940 return;
2941 }
2942
wufeng.jiang813dc352015-06-02 23:02:46 -04002943 /* chunk_header->total_sz is uint32,So chunk_data_sz is now less than 2^32
2944 otherwise it will return in the line above
2945 */
Ajay Dudaniab18f022011-05-12 14:39:22 -07002946 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
wufeng.jiang813dc352015-06-02 23:02:46 -04002947 (uint32_t)chunk_data_sz,
Ajay Dudaniab18f022011-05-12 14:39:22 -07002948 (unsigned int*)data))
Ajay Dudani5c761132011-04-07 20:19:04 -07002949 {
2950 fastboot_fail("flash write failure");
2951 return;
2952 }
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302953 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
2954 fastboot_fail("Bogus size for RAW chunk type");
2955 return;
2956 }
Ajay Dudani5c761132011-04-07 20:19:04 -07002957 total_blocks += chunk_header->chunk_sz;
wufeng.jiang813dc352015-06-02 23:02:46 -04002958 data += (uint32_t)chunk_data_sz;
Ajay Dudani5c761132011-04-07 20:19:04 -07002959 break;
2960
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002961 case CHUNK_TYPE_FILL:
2962 if(chunk_header->total_sz != (sparse_header->chunk_hdr_sz +
2963 sizeof(uint32_t)))
2964 {
2965 fastboot_fail("Bogus chunk size for chunk type FILL");
2966 return;
2967 }
2968
2969 fill_buf = (uint32_t *)memalign(CACHE_LINE, ROUNDUP(sparse_header->blk_sz, CACHE_LINE));
2970 if (!fill_buf)
2971 {
2972 fastboot_fail("Malloc failed for: CHUNK_TYPE_FILL");
2973 return;
2974 }
2975
Parth Dixit64b1a482016-03-07 16:31:26 +05302976 if (data_end < (uintptr_t)data + sizeof(uint32_t)) {
vijay kumar800255e2015-04-24 20:26:19 +05302977 fastboot_fail("buffer overreads occured due to invalid sparse header");
Gaurav Nebhwani163cbf82016-03-17 21:21:27 +05302978 free(fill_buf);
vijay kumar800255e2015-04-24 20:26:19 +05302979 return;
2980 }
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002981 fill_val = *(uint32_t *)data;
2982 data = (char *) data + sizeof(uint32_t);
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002983
2984 for (i = 0; i < (sparse_header->blk_sz / sizeof(fill_val)); i++)
2985 {
2986 fill_buf[i] = fill_val;
2987 }
2988
Gaurav Nebhwanie94cbe12016-03-17 21:16:34 +05302989 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz))
2990 {
2991 fastboot_fail("bogus size for chunk FILL type");
2992 free(fill_buf);
2993 return;
2994 }
2995
wufeng.jiang813dc352015-06-02 23:02:46 -04002996 for (i = 0; i < chunk_header->chunk_sz; i++)
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002997 {
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302998 /* Make sure that the data written to partition does not exceed partition size */
2999 if ((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz + sparse_header->blk_sz > size)
3000 {
3001 fastboot_fail("Chunk data size for fill type exceeds partition size");
Gaurav Nebhwani163cbf82016-03-17 21:21:27 +05303002 free(fill_buf);
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05303003 return;
3004 }
3005
Channagoud Kadabif627cf82013-09-09 14:08:20 -07003006 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
3007 sparse_header->blk_sz,
3008 fill_buf))
3009 {
3010 fastboot_fail("flash write failure");
3011 free(fill_buf);
3012 return;
3013 }
3014
3015 total_blocks++;
3016 }
3017
3018 free(fill_buf);
3019 break;
3020
Ajay Dudani5c761132011-04-07 20:19:04 -07003021 case CHUNK_TYPE_DONT_CARE:
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05303022 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
3023 fastboot_fail("bogus size for chunk DONT CARE type");
3024 return;
3025 }
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07003026 total_blocks += chunk_header->chunk_sz;
3027 break;
3028
Ajay Dudani5c761132011-04-07 20:19:04 -07003029 case CHUNK_TYPE_CRC:
3030 if(chunk_header->total_sz != sparse_header->chunk_hdr_sz)
3031 {
wufeng.jiang813dc352015-06-02 23:02:46 -04003032 fastboot_fail("Bogus chunk size for chunk type CRC");
Ajay Dudani5c761132011-04-07 20:19:04 -07003033 return;
3034 }
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05303035 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
3036 fastboot_fail("bogus size for chunk CRC type");
3037 return;
3038 }
Ajay Dudani5c761132011-04-07 20:19:04 -07003039 total_blocks += chunk_header->chunk_sz;
Parth Dixit64b1a482016-03-07 16:31:26 +05303040 if ((uintptr_t)data > UINT_MAX - chunk_data_sz) {
vijay kumar800255e2015-04-24 20:26:19 +05303041 fastboot_fail("integer overflow occured");
3042 return;
3043 }
wufeng.jiang813dc352015-06-02 23:02:46 -04003044 data += (uint32_t)chunk_data_sz;
Parth Dixit64b1a482016-03-07 16:31:26 +05303045 if (data_end < (uintptr_t)data) {
vijay kumar800255e2015-04-24 20:26:19 +05303046 fastboot_fail("buffer overreads occured due to invalid sparse header");
3047 return;
3048 }
Ajay Dudani5c761132011-04-07 20:19:04 -07003049 break;
3050
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07003051 default:
Channagoud Kadabif627cf82013-09-09 14:08:20 -07003052 dprintf(CRITICAL, "Unkown chunk type: %x\n",chunk_header->chunk_type);
Ajay Dudani5c761132011-04-07 20:19:04 -07003053 fastboot_fail("Unknown chunk type");
3054 return;
3055 }
3056 }
3057
Ajay Dudani0c6927b2011-05-18 11:12:16 -07003058 dprintf(INFO, "Wrote %d blocks, expected to write %d blocks\n",
3059 total_blocks, sparse_header->total_blks);
3060
3061 if(total_blocks != sparse_header->total_blks)
3062 {
3063 fastboot_fail("sparse image write failure");
3064 }
Ajay Dudani5c761132011-04-07 20:19:04 -07003065
3066 fastboot_okay("");
3067 return;
3068}
3069
3070void cmd_flash_mmc(const char *arg, void *data, unsigned sz)
3071{
3072 sparse_header_t *sparse_header;
Ajay Dudanide984792015-03-02 09:57:41 -08003073 meta_header_t *meta_header;
Ajay Dudani5c761132011-04-07 20:19:04 -07003074
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07003075#ifdef SSD_ENABLE
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -08003076 /* 8 Byte Magic + 2048 Byte xml + Encrypted Data */
3077 unsigned int *magic_number = (unsigned int *) data;
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08003078 int ret=0;
3079 uint32 major_version=0;
3080 uint32 minor_version=0;
3081
3082 ret = scm_svc_version(&major_version,&minor_version);
3083 if(!ret)
3084 {
3085 if(major_version >= 2)
3086 {
Amir Samuelovbb65ce02013-05-05 12:20:18 +03003087 if( !strcmp(arg, "ssd") || !strcmp(arg, "tqs") )
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08003088 {
3089 ret = encrypt_scm((uint32 **) &data, &sz);
3090 if (ret != 0) {
3091 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
3092 return;
3093 }
3094
Amir Samuelovbb65ce02013-05-05 12:20:18 +03003095 /* Protect only for SSD */
3096 if (!strcmp(arg, "ssd")) {
3097 ret = scm_protect_keystore((uint32 *) data, sz);
3098 if (ret != 0) {
3099 dprintf(CRITICAL, "ERROR: scm_protect_keystore Failed\n");
3100 return;
3101 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08003102 }
3103 }
3104 else
3105 {
3106 ret = decrypt_scm_v2((uint32 **) &data, &sz);
3107 if(ret != 0)
3108 {
3109 dprintf(CRITICAL,"ERROR: Decryption Failure\n");
3110 return;
3111 }
3112 }
3113 }
3114 else
3115 {
3116 if (magic_number[0] == DECRYPT_MAGIC_0 &&
3117 magic_number[1] == DECRYPT_MAGIC_1)
3118 {
3119 ret = decrypt_scm((uint32 **) &data, &sz);
3120 if (ret != 0) {
3121 dprintf(CRITICAL, "ERROR: Invalid secure image\n");
3122 return;
3123 }
3124 }
3125 else if (magic_number[0] == ENCRYPT_MAGIC_0 &&
3126 magic_number[1] == ENCRYPT_MAGIC_1)
3127 {
3128 ret = encrypt_scm((uint32 **) &data, &sz);
3129 if (ret != 0) {
3130 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
3131 return;
3132 }
3133 }
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07003134 }
3135 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08003136 else
Neeti Desai127b9e02012-03-20 16:11:23 -07003137 {
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08003138 dprintf(CRITICAL,"INVALID SVC Version\n");
3139 return;
Neeti Desai127b9e02012-03-20 16:11:23 -07003140 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08003141#endif /* SSD_ENABLE */
Neeti Desai127b9e02012-03-20 16:11:23 -07003142
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003143#if VERIFIED_BOOT
Channagoud Kadabi35297672015-06-13 11:09:49 -07003144 if (target_build_variant_user())
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003145 {
lijuang511a2b52015-08-14 20:50:51 +08003146 /* if device is locked:
3147 * common partition will not allow to be flashed
3148 * critical partition will allow to flash image.
3149 */
3150 if(!device.is_unlocked && !critical_flash_allowed(arg)) {
3151 fastboot_fail("Partition flashing is not allowed");
3152 return;
3153 }
Parth Dixitddbc7352015-10-18 03:13:31 +05303154#if !VBOOT_MOTA
lijuang511a2b52015-08-14 20:50:51 +08003155 /* if device critical is locked:
3156 * common partition will allow to be flashed
3157 * critical partition will not allow to flash image.
3158 */
3159 if(!device.is_unlock_critical && critical_flash_allowed(arg)) {
3160 fastboot_fail("Critical partition flashing is not allowed");
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003161 return;
3162 }
Parth Dixitddbc7352015-10-18 03:13:31 +05303163#endif
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003164 }
3165#endif
3166
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07003167 sparse_header = (sparse_header_t *) data;
Ajay Dudanide984792015-03-02 09:57:41 -08003168 meta_header = (meta_header_t *) data;
3169 if (sparse_header->magic == SPARSE_HEADER_MAGIC)
Ajay Dudani5c761132011-04-07 20:19:04 -07003170 cmd_flash_mmc_sparse_img(arg, data, sz);
Ajay Dudanide984792015-03-02 09:57:41 -08003171 else if (meta_header->magic == META_HEADER_MAGIC)
3172 cmd_flash_meta_img(arg, data, sz);
3173 else
3174 cmd_flash_mmc_img(arg, data, sz);
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07003175
3176#if VERIFIED_BOOT
Parth Dixitddbc7352015-10-18 03:13:31 +05303177#if !VBOOT_MOTA
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07003178 if((!strncmp(arg, "system", 6)) && !device.verity_mode)
3179 {
3180 // reset dm_verity mode to enforcing
3181 device.verity_mode = 1;
3182 write_device_info(&device);
3183 }
3184#endif
Parth Dixitddbc7352015-10-18 03:13:31 +05303185#endif
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07003186
Ajay Dudani5c761132011-04-07 20:19:04 -07003187 return;
3188}
3189
Tanya Brokhman84eaaf62015-02-24 21:57:06 +02003190void cmd_updatevol(const char *vol_name, void *data, unsigned sz)
3191{
3192 struct ptentry *sys_ptn;
3193 struct ptable *ptable;
3194
3195 ptable = flash_get_ptable();
3196 if (ptable == NULL) {
3197 fastboot_fail("partition table doesn't exist");
3198 return;
3199 }
3200
3201 sys_ptn = ptable_find(ptable, "system");
3202 if (sys_ptn == NULL) {
3203 fastboot_fail("system partition not found");
3204 return;
3205 }
3206
3207 sz = ROUND_TO_PAGE(sz, page_mask);
3208 if (update_ubi_vol(sys_ptn, vol_name, data, sz))
3209 fastboot_fail("update_ubi_vol failed");
3210 else
3211 fastboot_okay("");
3212}
3213
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003214void cmd_flash_nand(const char *arg, void *data, unsigned sz)
Dima Zavin214cc642009-01-26 11:16:21 -08003215{
3216 struct ptentry *ptn;
3217 struct ptable *ptable;
3218 unsigned extra = 0;
Gaurav Nebhwanid9730712016-05-06 14:28:29 +05303219 uint64_t partition_size = 0;
Dima Zavin214cc642009-01-26 11:16:21 -08003220
3221 ptable = flash_get_ptable();
3222 if (ptable == NULL) {
3223 fastboot_fail("partition table doesn't exist");
3224 return;
3225 }
3226
3227 ptn = ptable_find(ptable, arg);
3228 if (ptn == NULL) {
Tanya Brokhman84eaaf62015-02-24 21:57:06 +02003229 dprintf(INFO, "unknown partition name (%s). Trying updatevol\n",
3230 arg);
3231 cmd_updatevol(arg, data, sz);
Dima Zavin214cc642009-01-26 11:16:21 -08003232 return;
3233 }
3234
3235 if (!strcmp(ptn->name, "boot") || !strcmp(ptn->name, "recovery")) {
3236 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
3237 fastboot_fail("image is not a boot image");
3238 return;
3239 }
3240 }
3241
Amol Jadi5c61a952012-05-04 17:05:35 -07003242 if (!strcmp(ptn->name, "system")
Deepa Dinamani13e32c42012-03-12 14:34:17 -07003243 || !strcmp(ptn->name, "userdata")
3244 || !strcmp(ptn->name, "persist")
Sundarajan Srinivasanb063a852013-11-19 14:02:27 -08003245 || !strcmp(ptn->name, "recoveryfs")
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -08003246 || !strcmp(ptn->name, "modem"))
Tanya Brokhman1c94f1a2015-02-15 09:05:03 +02003247 extra = 1;
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -08003248 else
Shashank Mittaldcc2e352009-11-19 19:11:16 -08003249 sz = ROUND_TO_PAGE(sz, page_mask);
Dima Zavin214cc642009-01-26 11:16:21 -08003250
Gaurav Nebhwanid9730712016-05-06 14:28:29 +05303251 partition_size = (uint64_t)ptn->length * (uint64_t)flash_num_pages_per_blk() * (uint64_t)flash_page_size();
3252 if (partition_size > UINT_MAX) {
3253 fastboot_fail("Invalid partition size");
3254 return;
3255 }
3256
3257 if (sz > partition_size) {
3258 fastboot_fail("Image size too large");
3259 return;
3260 }
3261
Dima Zavin214cc642009-01-26 11:16:21 -08003262 dprintf(INFO, "writing %d bytes to '%s'\n", sz, ptn->name);
Tanya Brokhman1c94f1a2015-02-15 09:05:03 +02003263 if (!memcmp((void *)data, UBI_MAGIC, UBI_MAGIC_SIZE)) {
3264 if (flash_ubi_img(ptn, data, sz)) {
3265 fastboot_fail("flash write failure");
3266 return;
3267 }
3268 } else {
3269 if (flash_write(ptn, extra, data, sz)) {
3270 fastboot_fail("flash write failure");
3271 return;
3272 }
Dima Zavin214cc642009-01-26 11:16:21 -08003273 }
3274 dprintf(INFO, "partition '%s' updated\n", ptn->name);
3275 fastboot_okay("");
3276}
3277
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003278void cmd_flash(const char *arg, void *data, unsigned sz)
3279{
3280 if(target_is_emmc_boot())
3281 cmd_flash_mmc(arg, data, sz);
3282 else
3283 cmd_flash_nand(arg, data, sz);
3284}
3285
Dima Zavin214cc642009-01-26 11:16:21 -08003286void cmd_continue(const char *arg, void *data, unsigned sz)
3287{
3288 fastboot_okay("");
Amol Jadi7c4316c2013-10-07 14:19:26 -07003289 fastboot_stop();
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003290
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003291 if (target_is_emmc_boot())
3292 {
lijuanga40d6302015-07-20 20:10:13 +08003293#if FBCON_DISPLAY_MSG
lijuangde34d502016-02-26 16:04:50 +08003294 /* Exit keys' detection thread firstly */
3295 exit_menu_keys_detection();
lijuanga40d6302015-07-20 20:10:13 +08003296#endif
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003297 boot_linux_from_mmc();
3298 }
3299 else
3300 {
3301 boot_linux_from_flash();
3302 }
Dima Zavin214cc642009-01-26 11:16:21 -08003303}
3304
Chandan Uddaraju94183c02010-01-15 15:13:59 -08003305void cmd_reboot(const char *arg, void *data, unsigned sz)
3306{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003307 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08003308 fastboot_okay("");
3309 reboot_device(0);
3310}
3311
3312void cmd_reboot_bootloader(const char *arg, void *data, unsigned sz)
3313{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003314 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08003315 fastboot_okay("");
3316 reboot_device(FASTBOOT_MODE);
3317}
3318
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003319void cmd_oem_enable_charger_screen(const char *arg, void *data, unsigned size)
3320{
3321 dprintf(INFO, "Enabling charger screen check\n");
3322 device.charger_screen_enabled = 1;
3323 write_device_info(&device);
3324 fastboot_okay("");
3325}
3326
3327void cmd_oem_disable_charger_screen(const char *arg, void *data, unsigned size)
3328{
3329 dprintf(INFO, "Disabling charger screen check\n");
3330 device.charger_screen_enabled = 0;
3331 write_device_info(&device);
3332 fastboot_okay("");
3333}
3334
lijuanga25d8bb2015-09-16 13:11:52 +08003335void cmd_oem_off_mode_charger(const char *arg, void *data, unsigned size)
3336{
3337 char *p = NULL;
3338 const char *delim = " \t\n\r";
Parth Dixit407f15b2016-01-07 14:47:37 +05303339 char *sp;
lijuanga25d8bb2015-09-16 13:11:52 +08003340
3341 if (arg) {
Parth Dixit407f15b2016-01-07 14:47:37 +05303342 p = strtok_r((char *)arg, delim, &sp);
lijuanga25d8bb2015-09-16 13:11:52 +08003343 if (p) {
3344 if (!strncmp(p, "0", 1)) {
3345 device.charger_screen_enabled = 0;
3346 } else if (!strncmp(p, "1", 1)) {
3347 device.charger_screen_enabled = 1;
3348 }
3349 }
3350 }
3351
3352 /* update charger_screen_enabled value for getvar
3353 * command
3354 */
3355 snprintf(charger_screen_enabled, MAX_RSP_SIZE, "%d",
3356 device.charger_screen_enabled);
3357
3358 write_device_info(&device);
3359 fastboot_okay("");
3360}
3361
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05303362void cmd_oem_select_display_panel(const char *arg, void *data, unsigned size)
3363{
3364 dprintf(INFO, "Selecting display panel %s\n", arg);
3365 if (arg)
3366 strlcpy(device.display_panel, arg,
3367 sizeof(device.display_panel));
3368 write_device_info(&device);
3369 fastboot_okay("");
3370}
3371
Shashank Mittal162244e2011-08-08 19:01:25 -07003372void cmd_oem_unlock(const char *arg, void *data, unsigned sz)
3373{
lijuang4ece1e72015-08-14 21:02:36 +08003374 set_device_unlock(UNLOCK, TRUE);
vijay kumarc65876c2015-04-24 13:29:16 +05303375}
3376
3377void cmd_oem_unlock_go(const char *arg, void *data, unsigned sz)
3378{
lijuang4ece1e72015-08-14 21:02:36 +08003379 if(!device.is_unlocked) {
vijay kumarc65876c2015-04-24 13:29:16 +05303380 if(!is_allow_unlock) {
3381 fastboot_fail("oem unlock is not allowed");
3382 return;
3383 }
3384
lijuang4ece1e72015-08-14 21:02:36 +08003385 set_device_unlock_value(UNLOCK, TRUE);
vijay kumarc65876c2015-04-24 13:29:16 +05303386
lijuang4ece1e72015-08-14 21:02:36 +08003387 /* wipe data */
vijay kumarc65876c2015-04-24 13:29:16 +05303388 struct recovery_message msg;
lijuang4ece1e72015-08-14 21:02:36 +08003389
vijay kumarc65876c2015-04-24 13:29:16 +05303390 snprintf(msg.recovery, sizeof(msg.recovery), "recovery\n--wipe_data");
3391 write_misc(0, &msg, sizeof(msg));
3392
3393 fastboot_okay("");
3394 reboot_device(RECOVERY_MODE);
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003395 }
3396 fastboot_okay("");
3397}
3398
Channagoud Kadabiad259832015-05-29 11:14:17 -07003399static int aboot_frp_unlock(char *pname, void *data, unsigned sz)
3400{
3401 int ret = 1;
3402 uint32_t secret_key;
3403 char seckey_buffer[MAX_RSP_SIZE];
3404
3405 secret_key = aboot_get_secret_key();
3406 if (secret_key)
3407 {
3408 snprintf((char *) seckey_buffer, MAX_RSP_SIZE, "%x", secret_key);
3409 if (!memcmp((void *)data, (void *)seckey_buffer, sz))
3410 {
3411 is_allow_unlock = true;
3412 write_allow_oem_unlock(is_allow_unlock);
3413 ret = 0;
3414 }
3415 }
3416 return ret;
3417}
3418
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003419void cmd_oem_lock(const char *arg, void *data, unsigned sz)
3420{
lijuang4ece1e72015-08-14 21:02:36 +08003421 set_device_unlock(UNLOCK, FALSE);
Shashank Mittal162244e2011-08-08 19:01:25 -07003422}
3423
Shashank Mittala0032282011-08-26 14:50:11 -07003424void cmd_oem_devinfo(const char *arg, void *data, unsigned sz)
3425{
lijuang511a2b52015-08-14 20:50:51 +08003426 char response[MAX_RSP_SIZE];
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003427 snprintf(response, sizeof(response), "\tDevice tampered: %s", (device.is_tampered ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07003428 fastboot_info(response);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003429 snprintf(response, sizeof(response), "\tDevice unlocked: %s", (device.is_unlocked ? "true" : "false"));
3430 fastboot_info(response);
Parth Dixitddbc7352015-10-18 03:13:31 +05303431#if !VBOOT_MOTA
lijuang511a2b52015-08-14 20:50:51 +08003432 snprintf(response, sizeof(response), "\tDevice critical unlocked: %s", (device.is_unlock_critical ? "true" : "false"));
3433 fastboot_info(response);
Parth Dixitddbc7352015-10-18 03:13:31 +05303434#endif
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003435 snprintf(response, sizeof(response), "\tCharger screen enabled: %s", (device.charger_screen_enabled ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07003436 fastboot_info(response);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05303437 snprintf(response, sizeof(response), "\tDisplay panel: %s", (device.display_panel));
3438 fastboot_info(response);
Shashank Mittala0032282011-08-26 14:50:11 -07003439 fastboot_okay("");
3440}
3441
lijuang511a2b52015-08-14 20:50:51 +08003442void cmd_flashing_get_unlock_ability(const char *arg, void *data, unsigned sz)
3443{
3444 char response[MAX_RSP_SIZE];
3445 snprintf(response, sizeof(response), "\tget_unlock_ability: %d", is_allow_unlock);
3446 fastboot_info(response);
3447 fastboot_okay("");
3448}
3449
3450void cmd_flashing_lock_critical(const char *arg, void *data, unsigned sz)
3451{
lijuang4ece1e72015-08-14 21:02:36 +08003452 set_device_unlock(UNLOCK_CRITICAL, FALSE);
lijuang511a2b52015-08-14 20:50:51 +08003453}
3454
3455void cmd_flashing_unlock_critical(const char *arg, void *data, unsigned sz)
3456{
lijuang4ece1e72015-08-14 21:02:36 +08003457 set_device_unlock(UNLOCK_CRITICAL, TRUE);
lijuang511a2b52015-08-14 20:50:51 +08003458}
3459
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07003460void cmd_preflash(const char *arg, void *data, unsigned sz)
3461{
3462 fastboot_okay("");
3463}
3464
Mao Flynn7b379f32015-04-20 00:28:30 +08003465static uint8_t logo_header[LOGO_IMG_HEADER_SIZE];
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303466
Mao Flynn7b379f32015-04-20 00:28:30 +08003467int splash_screen_check_header(logo_img_header *header)
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303468{
Mao Flynn7b379f32015-04-20 00:28:30 +08003469 if (memcmp(header->magic, LOGO_IMG_MAGIC, 8))
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303470 return -1;
Mao Flynn7b379f32015-04-20 00:28:30 +08003471 if (header->width == 0 || header->height == 0)
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303472 return -1;
3473 return 0;
3474}
3475
Mao Flynn7b379f32015-04-20 00:28:30 +08003476int splash_screen_flash()
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003477{
3478 struct ptentry *ptn;
3479 struct ptable *ptable;
Mao Flynn7b379f32015-04-20 00:28:30 +08003480 struct logo_img_header *header;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003481 struct fbcon_config *fb_display = NULL;
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08003482
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303483 ptable = flash_get_ptable();
3484 if (ptable == NULL) {
Mao Flynn7b379f32015-04-20 00:28:30 +08003485 dprintf(CRITICAL, "ERROR: Partition table not found\n");
3486 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303487 }
Mao Flynn7b379f32015-04-20 00:28:30 +08003488
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303489 ptn = ptable_find(ptable, "splash");
3490 if (ptn == NULL) {
3491 dprintf(CRITICAL, "ERROR: splash Partition not found\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003492 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303493 }
Mao Flynn7b379f32015-04-20 00:28:30 +08003494 if (flash_read(ptn, 0, (void *)logo_header, LOGO_IMG_HEADER_SIZE)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303495 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003496 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303497 }
3498
Mao Flynn7b379f32015-04-20 00:28:30 +08003499 header = (struct logo_img_header *)logo_header;
3500 if (splash_screen_check_header(header)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303501 dprintf(CRITICAL, "ERROR: Boot image header invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003502 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303503 }
3504
3505 fb_display = fbcon_display();
3506 if (fb_display) {
Mao Flynn7b379f32015-04-20 00:28:30 +08003507 if (header->type && (header->blocks != 0)) { // RLE24 compressed data
3508 uint8_t *base = (uint8_t *) fb_display->base + LOGO_IMG_OFFSET;
3509
3510 /* if the logo is full-screen size, remove "fbcon_clear()" */
3511 if ((header->width != fb_display->width)
3512 || (header->height != fb_display->height))
3513 fbcon_clear();
3514
3515 if (flash_read(ptn + LOGO_IMG_HEADER_SIZE, 0,
3516 (uint32_t *)base,
3517 (header->blocks * 512))) {
3518 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
3519 return -1;
3520 }
3521 fbcon_extract_to_screen(header, base);
3522 return 0;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003523 }
Mao Flynn7b379f32015-04-20 00:28:30 +08003524
Veera Sundaram Sankaranc33416f2015-09-28 10:25:55 -07003525 if ((header->width > fb_display->width) || (header->height > fb_display->height)) {
3526 dprintf(CRITICAL, "Logo config greater than fb config. Fall back default logo\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003527 return -1;
3528 }
3529
Vineet Bajaj99291ed2014-09-09 12:29:46 +05303530 uint8_t *base = (uint8_t *) fb_display->base;
Mao Flynn7b379f32015-04-20 00:28:30 +08003531 if (flash_read(ptn + LOGO_IMG_HEADER_SIZE, 0,
3532 (uint32_t *)base,
3533 ((((header->width * header->height * fb_display->bpp/8) + 511) >> 9) << 9))) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303534 fbcon_clear();
Vineet Bajaj99291ed2014-09-09 12:29:46 +05303535 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003536 return -1;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003537 }
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303538 }
3539
Mao Flynn7b379f32015-04-20 00:28:30 +08003540 return 0;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303541}
3542
Mao Flynn7b379f32015-04-20 00:28:30 +08003543int splash_screen_mmc()
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303544{
3545 int index = INVALID_PTN;
3546 unsigned long long ptn = 0;
3547 struct fbcon_config *fb_display = NULL;
Mao Flynn7b379f32015-04-20 00:28:30 +08003548 struct logo_img_header *header;
Mao Flynn1893a7f2015-06-03 12:03:36 +08003549 uint32_t blocksize, realsize, readsize;
3550 uint8_t *base;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303551
3552 index = partition_get_index("splash");
3553 if (index == 0) {
3554 dprintf(CRITICAL, "ERROR: splash Partition table not found\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003555 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303556 }
3557
3558 ptn = partition_get_offset(index);
3559 if (ptn == 0) {
3560 dprintf(CRITICAL, "ERROR: splash Partition invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003561 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303562 }
3563
Mao Flynn1893a7f2015-06-03 12:03:36 +08003564 mmc_set_lun(partition_get_lun(index));
3565
3566 blocksize = mmc_get_device_blocksize();
3567 if (blocksize == 0) {
3568 dprintf(CRITICAL, "ERROR:splash Partition invalid blocksize\n");
3569 return -1;
3570 }
3571
3572 fb_display = fbcon_display();
Channagoud Kadabidaf10a62015-07-22 11:51:55 -07003573 if (!fb_display)
3574 {
3575 dprintf(CRITICAL, "ERROR: fb config is not allocated\n");
3576 return -1;
3577 }
3578
Mao Flynn1893a7f2015-06-03 12:03:36 +08003579 base = (uint8_t *) fb_display->base;
3580
3581 if (mmc_read(ptn, (uint32_t *)(base + LOGO_IMG_OFFSET), blocksize)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303582 dprintf(CRITICAL, "ERROR: Cannot read splash image header\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003583 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303584 }
3585
Mao Flynn1893a7f2015-06-03 12:03:36 +08003586 header = (struct logo_img_header *)(base + LOGO_IMG_OFFSET);
Mao Flynn7b379f32015-04-20 00:28:30 +08003587 if (splash_screen_check_header(header)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303588 dprintf(CRITICAL, "ERROR: Splash image header invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003589 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303590 }
3591
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303592 if (fb_display) {
Mao Flynn1893a7f2015-06-03 12:03:36 +08003593 if (header->type && (header->blocks != 0)) { /* 1 RLE24 compressed data */
3594 base += LOGO_IMG_OFFSET;
Mao Flynn7b379f32015-04-20 00:28:30 +08003595
Mao Flynn1893a7f2015-06-03 12:03:36 +08003596 realsize = header->blocks * 512;
3597 readsize = ROUNDUP((realsize + LOGO_IMG_HEADER_SIZE), blocksize) - blocksize;
3598
3599 /* if the logo is not full-screen size, clean screen */
Mao Flynn7b379f32015-04-20 00:28:30 +08003600 if ((header->width != fb_display->width)
3601 || (header->height != fb_display->height))
3602 fbcon_clear();
3603
Mao Flynn1893a7f2015-06-03 12:03:36 +08003604 if (mmc_read(ptn + blocksize, (uint32_t *)(base + blocksize), readsize)) {
Mao Flynn7b379f32015-04-20 00:28:30 +08003605 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
3606 return -1;
3607 }
Mao Flynn7b379f32015-04-20 00:28:30 +08003608
Mao Flynn1893a7f2015-06-03 12:03:36 +08003609 fbcon_extract_to_screen(header, (base + LOGO_IMG_HEADER_SIZE));
3610 } else { /* 2 Raw BGR data */
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303611
Veera Sundaram Sankaranc33416f2015-09-28 10:25:55 -07003612 if ((header->width > fb_display->width) || (header->height > fb_display->height)) {
3613 dprintf(CRITICAL, "Logo config greater than fb config. Fall back default logo\n");
Mao Flynn1893a7f2015-06-03 12:03:36 +08003614 return -1;
3615 }
3616
3617 realsize = header->width * header->height * fb_display->bpp / 8;
3618 readsize = ROUNDUP((realsize + LOGO_IMG_HEADER_SIZE), blocksize) - blocksize;
3619
3620 if (blocksize == LOGO_IMG_HEADER_SIZE) { /* read the content directly */
3621 if (mmc_read((ptn + LOGO_IMG_HEADER_SIZE), (uint32_t *)base, readsize)) {
3622 fbcon_clear();
3623 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
3624 return -1;
3625 }
3626 } else {
3627 if (mmc_read(ptn + blocksize ,
3628 (uint32_t *)(base + LOGO_IMG_OFFSET + blocksize), readsize)) {
3629 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
3630 return -1;
3631 }
3632 memmove(base, (base + LOGO_IMG_OFFSET + LOGO_IMG_HEADER_SIZE), realsize);
3633 }
3634 }
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303635 }
3636
Mao Flynn7b379f32015-04-20 00:28:30 +08003637 return 0;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303638}
3639
Mao Flynn7b379f32015-04-20 00:28:30 +08003640int fetch_image_from_partition()
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303641{
3642 if (target_is_emmc_boot()) {
3643 return splash_screen_mmc();
3644 } else {
3645 return splash_screen_flash();
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003646 }
3647}
3648
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07003649/* Get the size from partiton name */
3650static void get_partition_size(const char *arg, char *response)
3651{
3652 uint64_t ptn = 0;
3653 uint64_t size;
3654 int index = INVALID_PTN;
3655
3656 index = partition_get_index(arg);
3657
3658 if (index == INVALID_PTN)
3659 {
3660 dprintf(CRITICAL, "Invalid partition index\n");
3661 return;
3662 }
3663
3664 ptn = partition_get_offset(index);
3665
3666 if(!ptn)
3667 {
3668 dprintf(CRITICAL, "Invalid partition name %s\n", arg);
3669 return;
3670 }
3671
3672 size = partition_get_size(index);
3673
3674 snprintf(response, MAX_RSP_SIZE, "\t 0x%llx", size);
3675 return;
3676}
3677
3678/*
3679 * Publish the partition type & size info
3680 * fastboot getvar will publish the required information.
3681 * fastboot getvar partition_size:<partition_name>: partition size in hex
3682 * fastboot getvar partition_type:<partition_name>: partition type (ext/fat)
3683 */
3684static void publish_getvar_partition_info(struct getvar_partition_info *info, uint8_t num_parts)
3685{
3686 uint8_t i;
3687
3688 for (i = 0; i < num_parts; i++) {
3689 get_partition_size(info[i].part_name, info[i].size_response);
3690
3691 if (strlcat(info[i].getvar_size, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
3692 {
3693 dprintf(CRITICAL, "partition size name truncated\n");
3694 return;
3695 }
3696 if (strlcat(info[i].getvar_type, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
3697 {
3698 dprintf(CRITICAL, "partition type name truncated\n");
3699 return;
3700 }
3701
3702 /* publish partition size & type info */
3703 fastboot_publish((const char *) info[i].getvar_size, (const char *) info[i].size_response);
3704 fastboot_publish((const char *) info[i].getvar_type, (const char *) info[i].type_response);
3705 }
3706}
3707
lijuang4ece1e72015-08-14 21:02:36 +08003708void get_product_name(unsigned char *buf)
3709{
3710 snprintf((char*)buf, MAX_RSP_SIZE, "%s", TARGET(BOARD));
3711 return;
3712}
3713
3714void get_bootloader_version(unsigned char *buf)
3715{
3716 snprintf((char*)buf, MAX_RSP_SIZE, "%s", device.bootloader_version);
3717 return;
3718}
3719
3720void get_baseband_version(unsigned char *buf)
3721{
3722 snprintf((char*)buf, MAX_RSP_SIZE, "%s", device.radio_version);
3723 return;
3724}
3725
3726bool is_device_locked()
3727{
3728 return device.is_unlocked ? false:true;
3729}
3730
Amol Jadi5edf3552013-07-23 14:15:34 -07003731/* register commands and variables for fastboot */
3732void aboot_fastboot_register_commands(void)
3733{
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003734 int i;
lijuangf16461c2015-08-03 17:09:34 +08003735 char hw_platform_buf[MAX_RSP_SIZE];
Amol Jadi5edf3552013-07-23 14:15:34 -07003736
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003737 struct fastboot_cmd_desc cmd_list[] = {
lijuang511a2b52015-08-14 20:50:51 +08003738 /* By default the enabled list is empty. */
3739 {"", NULL},
3740 /* move commands enclosed within the below ifndef to here
3741 * if they need to be enabled in user build.
3742 */
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003743#ifndef DISABLE_FASTBOOT_CMDS
lijuang511a2b52015-08-14 20:50:51 +08003744 /* Register the following commands only for non-user builds */
3745 {"flash:", cmd_flash},
3746 {"erase:", cmd_erase},
3747 {"boot", cmd_boot},
3748 {"continue", cmd_continue},
3749 {"reboot", cmd_reboot},
3750 {"reboot-bootloader", cmd_reboot_bootloader},
3751 {"oem unlock", cmd_oem_unlock},
3752 {"oem unlock-go", cmd_oem_unlock_go},
3753 {"oem lock", cmd_oem_lock},
3754 {"flashing unlock", cmd_oem_unlock},
3755 {"flashing lock", cmd_oem_lock},
3756 {"flashing lock_critical", cmd_flashing_lock_critical},
3757 {"flashing unlock_critical", cmd_flashing_unlock_critical},
3758 {"flashing get_unlock_ability", cmd_flashing_get_unlock_ability},
3759 {"oem device-info", cmd_oem_devinfo},
3760 {"preflash", cmd_preflash},
3761 {"oem enable-charger-screen", cmd_oem_enable_charger_screen},
3762 {"oem disable-charger-screen", cmd_oem_disable_charger_screen},
lijuanga25d8bb2015-09-16 13:11:52 +08003763 {"oem off-mode-charge", cmd_oem_off_mode_charger},
lijuang511a2b52015-08-14 20:50:51 +08003764 {"oem select-display-panel", cmd_oem_select_display_panel},
Channagoud Kadabi736c4962015-08-21 11:56:52 -07003765#if UNITTEST_FW_SUPPORT
Channagoud Kadabid5ddf482015-09-28 10:31:35 -07003766 {"oem run-tests", cmd_oem_runtests},
Channagoud Kadabi736c4962015-08-21 11:56:52 -07003767#endif
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003768#endif
lijuang511a2b52015-08-14 20:50:51 +08003769 };
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003770
3771 int fastboot_cmds_count = sizeof(cmd_list)/sizeof(cmd_list[0]);
3772 for (i = 1; i < fastboot_cmds_count; i++)
3773 fastboot_register(cmd_list[i].name,cmd_list[i].cb);
3774
Amol Jadi5edf3552013-07-23 14:15:34 -07003775 /* publish variables and their values */
3776 fastboot_publish("product", TARGET(BOARD));
3777 fastboot_publish("kernel", "lk");
3778 fastboot_publish("serialno", sn_buf);
3779
3780 /*
3781 * partition info is supported only for emmc partitions
3782 * Calling this for NAND prints some error messages which
3783 * is harmless but misleading. Avoid calling this for NAND
3784 * devices.
3785 */
3786 if (target_is_emmc_boot())
3787 publish_getvar_partition_info(part_info, ARRAY_SIZE(part_info));
3788
3789 /* Max download size supported */
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003790 snprintf(max_download_size, MAX_RSP_SIZE, "\t0x%x",
3791 target_get_max_flash_size());
Amol Jadi5edf3552013-07-23 14:15:34 -07003792 fastboot_publish("max-download-size", (const char *) max_download_size);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003793 /* Is the charger screen check enabled */
3794 snprintf(charger_screen_enabled, MAX_RSP_SIZE, "%d",
3795 device.charger_screen_enabled);
3796 fastboot_publish("charger-screen-enabled",
3797 (const char *) charger_screen_enabled);
lijuanga25d8bb2015-09-16 13:11:52 +08003798 fastboot_publish("off-mode-charge", (const char *) charger_screen_enabled);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05303799 snprintf(panel_display_mode, MAX_RSP_SIZE, "%s",
3800 device.display_panel);
3801 fastboot_publish("display-panel",
3802 (const char *) panel_display_mode);
Channagoud Kadabiaafbbfc2015-03-04 17:52:56 -08003803 fastboot_publish("version-bootloader", (const char *) device.bootloader_version);
3804 fastboot_publish("version-baseband", (const char *) device.radio_version);
lijuangf16461c2015-08-03 17:09:34 +08003805 fastboot_publish("secure", is_secure_boot_enable()? "yes":"no");
3806 smem_get_hw_platform_name((unsigned char *) hw_platform_buf, sizeof(hw_platform_buf));
3807 snprintf(get_variant, MAX_RSP_SIZE, "%s %s", hw_platform_buf,
3808 target_is_emmc_boot()? "eMMC":"UFS");
3809 fastboot_publish("variant", (const char *) get_variant);
lijuang65c5a822015-08-29 16:35:36 +08003810#if CHECK_BAT_VOLTAGE
lijuang102dfa92015-10-09 18:31:03 +08003811 update_battery_status();
lijuang65c5a822015-08-29 16:35:36 +08003812 fastboot_publish("battery-voltage", (const char *) battery_voltage);
lijuang102dfa92015-10-09 18:31:03 +08003813 fastboot_publish("battery-soc-ok", (const char *) battery_soc_ok);
lijuang65c5a822015-08-29 16:35:36 +08003814#endif
Amol Jadi5edf3552013-07-23 14:15:34 -07003815}
3816
Brian Swetland9c4c0752009-01-25 16:23:50 -08003817void aboot_init(const struct app_descriptor *app)
3818{
Shashank Mittal4f99a882010-02-01 13:58:50 -08003819 unsigned reboot_mode = 0;
Chandan Uddarajubedca152010-06-02 23:05:15 -07003820
lijuang39831732016-01-08 17:49:02 +08003821 /* Initialise wdog to catch early lk crashes */
3822#if WDOG_SUPPORT
3823 msm_wdog_init();
3824#endif
3825
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07003826 /* Setup page size information for nv storage */
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003827 if (target_is_emmc_boot())
3828 {
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07003829 page_size = mmc_page_size();
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003830 page_mask = page_size - 1;
Gaurav Nebhwani4d2389c2016-03-17 21:10:05 +05303831 mmc_blocksize = mmc_get_device_blocksize();
3832 mmc_blocksize_mask = mmc_blocksize - 1;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003833 }
3834 else
3835 {
3836 page_size = flash_page_size();
3837 page_mask = page_size - 1;
3838 }
3839
Deepa Dinamani0e163a42013-05-24 17:08:15 -07003840 ASSERT((MEMBASE + MEMSIZE) > MEMBASE);
3841
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003842 read_device_info(&device);
vijay kumarc65876c2015-04-24 13:29:16 +05303843 read_allow_oem_unlock(&device);
Shashank Mittal162244e2011-08-08 19:01:25 -07003844
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08003845 /* Display splash screen if enabled */
3846#if DISPLAY_SPLASH_SCREEN
lijuang99c02d82015-02-13 19:04:34 +08003847#if NO_ALARM_DISPLAY
3848 if (!check_alarm_boot()) {
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08003849#endif
lijuang99c02d82015-02-13 19:04:34 +08003850 dprintf(SPEW, "Display Init: Start\n");
Ajay Singh Parmara7865a82015-04-16 21:27:52 -07003851#if DISPLAY_HDMI_PRIMARY
3852 if (!strlen(device.display_panel))
3853 strlcpy(device.display_panel, DISPLAY_PANEL_HDMI,
3854 sizeof(device.display_panel));
3855#endif
Channagoud Kadabi90869ce2015-04-27 11:15:14 -07003856#if ENABLE_WBC
Channagoud Kadabid801ac72015-08-26 11:21:51 -07003857 /* Wait if the display shutdown is in progress */
3858 while(pm_app_display_shutdown_in_prgs());
Channagoud Kadabi90869ce2015-04-27 11:15:14 -07003859 if (!pm_appsbl_display_init_done())
3860 target_display_init(device.display_panel);
3861 else
3862 display_image_on_screen();
3863#else
lijuang99c02d82015-02-13 19:04:34 +08003864 target_display_init(device.display_panel);
Channagoud Kadabi90869ce2015-04-27 11:15:14 -07003865#endif
lijuang99c02d82015-02-13 19:04:34 +08003866 dprintf(SPEW, "Display Init: Done\n");
3867#if NO_ALARM_DISPLAY
3868 }
3869#endif
3870#endif
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08003871
Greg Griscod6250552011-06-29 14:40:23 -07003872 target_serialno((unsigned char *) sn_buf);
Ajay Dudanib06c05f2011-05-12 14:46:10 -07003873 dprintf(SPEW,"serial number: %s\n",sn_buf);
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -08003874
Dhaval Patel223ec952013-07-18 14:49:44 -07003875 memset(display_panel_buf, '\0', MAX_PANEL_BUF_SIZE);
3876
Matthew Qindefd5562014-07-11 18:02:40 +08003877 /*
3878 * Check power off reason if user force reset,
3879 * if yes phone will do normal boot.
3880 */
3881 if (is_user_force_reset())
3882 goto normal_boot;
3883
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003884 /* Check if we should do something other than booting up */
Ameya Thakur0b9c2442013-05-10 13:22:56 -07003885 if (keys_get_state(KEY_VOLUMEUP) && keys_get_state(KEY_VOLUMEDOWN))
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07003886 {
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03003887 dprintf(ALWAYS,"dload mode key sequence detected\n");
lijuang395b5e62015-11-19 17:39:44 +08003888 reboot_device(EMERGENCY_DLOAD);
3889 dprintf(CRITICAL,"Failed to reboot into dload mode\n");
3890
Ameya Thakur0b9c2442013-05-10 13:22:56 -07003891 boot_into_fastboot = true;
3892 }
3893 if (!boot_into_fastboot)
3894 {
3895 if (keys_get_state(KEY_HOME) || keys_get_state(KEY_VOLUMEUP))
3896 boot_into_recovery = 1;
3897 if (!boot_into_recovery &&
3898 (keys_get_state(KEY_BACK) || keys_get_state(KEY_VOLUMEDOWN)))
Pavel Nedev5d91d412013-04-29 11:34:24 +03003899 boot_into_fastboot = true;
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07003900 }
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003901 #if NO_KEYPAD_DRIVER
Kinson Chik0b1c8162011-08-31 16:31:57 -07003902 if (fastboot_trigger())
Pavel Nedev5d91d412013-04-29 11:34:24 +03003903 boot_into_fastboot = true;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003904 #endif
Chandan Uddarajubedca152010-06-02 23:05:15 -07003905
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003906#if USE_PON_REBOOT_REG
3907 reboot_mode = check_hard_reboot_mode();
3908#else
Ajay Dudani77421292010-10-27 19:34:06 -07003909 reboot_mode = check_reboot_mode();
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003910#endif
3911 if (reboot_mode == RECOVERY_MODE)
3912 {
Ajay Dudani77421292010-10-27 19:34:06 -07003913 boot_into_recovery = 1;
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003914 }
3915 else if(reboot_mode == FASTBOOT_MODE)
3916 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03003917 boot_into_fastboot = true;
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003918 }
3919 else if(reboot_mode == ALARM_BOOT)
3920 {
Matthew Qind886f3c2014-01-17 16:52:01 +08003921 boot_reason_alarm = true;
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003922 }
Parth Dixit207573a2015-10-27 17:26:21 +05303923#if VERIFIED_BOOT
Parth Dixitddbc7352015-10-18 03:13:31 +05303924#if !VBOOT_MOTA
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003925 else if (reboot_mode == DM_VERITY_ENFORCING)
3926 {
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07003927 device.verity_mode = 1;
3928 write_device_info(&device);
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003929 }
3930 else if (reboot_mode == DM_VERITY_LOGGING)
3931 {
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07003932 device.verity_mode = 0;
3933 write_device_info(&device);
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003934 }
3935 else if (reboot_mode == DM_VERITY_KEYSCLEAR)
3936 {
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07003937 if(send_delete_keys_to_tz())
3938 ASSERT(0);
Ajay Dudani77421292010-10-27 19:34:06 -07003939 }
Parth Dixit207573a2015-10-27 17:26:21 +05303940#endif
Parth Dixitddbc7352015-10-18 03:13:31 +05303941#endif
Ajay Dudani77421292010-10-27 19:34:06 -07003942
Matthew Qindefd5562014-07-11 18:02:40 +08003943normal_boot:
Pavel Nedev5d91d412013-04-29 11:34:24 +03003944 if (!boot_into_fastboot)
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003945 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03003946 if (target_is_emmc_boot())
Shashank Mittala0032282011-08-26 14:50:11 -07003947 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03003948 if(emmc_recovery_init())
3949 dprintf(ALWAYS,"error in emmc_recovery_init\n");
3950 if(target_use_signed_kernel())
Shashank Mittala0032282011-08-26 14:50:11 -07003951 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03003952 if((device.is_unlocked) || (device.is_tampered))
3953 {
3954 #ifdef TZ_TAMPER_FUSE
3955 set_tamper_fuse_cmd();
3956 #endif
3957 #if USE_PCOM_SECBOOT
3958 set_tamper_flag(device.is_tampered);
3959 #endif
3960 }
Shashank Mittala0032282011-08-26 14:50:11 -07003961 }
Amit Blay6281ebc2015-01-11 14:44:08 +02003962
Pavel Nedev5d91d412013-04-29 11:34:24 +03003963 boot_linux_from_mmc();
Shashank Mittala0032282011-08-26 14:50:11 -07003964 }
Pavel Nedev5d91d412013-04-29 11:34:24 +03003965 else
3966 {
3967 recovery_init();
3968 #if USE_PCOM_SECBOOT
3969 if((device.is_unlocked) || (device.is_tampered))
3970 set_tamper_flag(device.is_tampered);
3971 #endif
3972 boot_linux_from_flash();
3973 }
3974 dprintf(CRITICAL, "ERROR: Could not do normal boot. Reverting "
3975 "to fastboot mode.\n");
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003976 }
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003977
Amol Jadi5edf3552013-07-23 14:15:34 -07003978 /* We are here means regular boot did not happen. Start fastboot. */
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07003979
Amol Jadi5edf3552013-07-23 14:15:34 -07003980 /* register aboot specific fastboot commands */
3981 aboot_fastboot_register_commands();
Amol Jadi57abe4c2011-05-24 15:47:27 -07003982
Amol Jadi5edf3552013-07-23 14:15:34 -07003983 /* dump partition table for debug info */
Kinson Chikf1a43512011-07-14 11:28:39 -07003984 partition_dump();
Amol Jadi5edf3552013-07-23 14:15:34 -07003985
3986 /* initialize and start fastboot */
3987 fastboot_init(target_get_scratch_address(), target_get_max_flash_size());
lijuang4ece1e72015-08-14 21:02:36 +08003988#if FBCON_DISPLAY_MSG
lijuang9a7d3b92015-11-30 14:41:24 +08003989 display_fastboot_menu();
lijuang4ece1e72015-08-14 21:02:36 +08003990#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08003991}
3992
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07003993uint32_t get_page_size()
3994{
3995 return page_size;
3996}
3997
Amir Samuelov57a6fa22013-06-05 16:36:43 +03003998/*
3999 * Calculated and save hash (SHA256) for non-signed boot image.
4000 *
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07004001 * @param image_addr - Boot image address
4002 * @param image_size - Size of the boot image
Amir Samuelov57a6fa22013-06-05 16:36:43 +03004003 *
4004 * @return int - 0 on success, negative value on failure.
4005 */
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07004006static int aboot_save_boot_hash_mmc(uint32_t image_addr, uint32_t image_size)
Amir Samuelov57a6fa22013-06-05 16:36:43 +03004007{
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07004008 unsigned int digest[8];
4009#if IMAGE_VERIF_ALGO_SHA1
4010 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA1;
4011#else
4012 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA256;
4013#endif
Amir Samuelov57a6fa22013-06-05 16:36:43 +03004014
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07004015 target_crypto_init_params();
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -08004016 hash_find((unsigned char *) image_addr, image_size, (unsigned char *)&digest, auth_algo);
Amir Samuelov57a6fa22013-06-05 16:36:43 +03004017
4018 save_kernel_hash_cmd(digest);
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07004019 dprintf(INFO, "aboot_save_boot_hash_mmc: imagesize_actual size %d bytes.\n", (int) image_size);
Amir Samuelov57a6fa22013-06-05 16:36:43 +03004020
4021 return 0;
4022}
4023
Brian Swetland9c4c0752009-01-25 16:23:50 -08004024APP_START(aboot)
4025 .init = aboot_init,
4026APP_END