blob: 892ec72caa1c920c0509ab48c1d7cfb15f608d51 [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 Dudanicd01f9b2010-02-23 21:13:04 -0800120#ifdef MEMBASE
121#define EMMC_BOOT_IMG_HEADER_ADDR (0xFF000+(MEMBASE))
122#else
David Ng183a7422009-12-07 14:55:21 -0800123#define EMMC_BOOT_IMG_HEADER_ADDR 0xFF000
Ajay Dudanicd01f9b2010-02-23 21:13:04 -0800124#endif
125
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700126#ifndef MEMSIZE
127#define MEMSIZE 1024*1024
128#endif
129
130#define MAX_TAGS_SIZE 1024
131
Kun Liang2f1601a2013-08-12 16:29:54 +0800132/* make 4096 as default size to ensure EFS,EXT4's erasing */
133#define DEFAULT_ERASE_SIZE 4096
Ujwal Patelc0b0a252015-08-16 14:05:35 -0700134#define MAX_PANEL_BUF_SIZE 196
Kun Liang2f1601a2013-08-12 16:29:54 +0800135
Dhaval Patelf83d73b2014-06-23 16:24:37 -0700136#define DISPLAY_DEFAULT_PREFIX "mdss_mdp"
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700137#define BOOT_DEV_MAX_LEN 64
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -0800138
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800139#define IS_ARM64(ptr) (ptr->magic_64 == KERNEL64_HDR_MAGIC) ? true : false
140
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -0700141#define ADD_OF(a, b) (UINT_MAX - b > a) ? (a + b) : UINT_MAX
142
Sridhar Parasuram7bd4aaf2015-02-12 11:14:38 -0800143#if USE_BOOTDEV_CMDLINE
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700144static const char *emmc_cmdline = " androidboot.bootdevice=";
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700145#else
David Ng183a7422009-12-07 14:55:21 -0800146static const char *emmc_cmdline = " androidboot.emmc=true";
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700147#endif
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800148static const char *usb_sn_cmdline = " androidboot.serialno=";
Pavel Nedev328ac822013-04-05 15:25:11 +0300149static const char *androidboot_mode = " androidboot.mode=";
Matthew Qind886f3c2014-01-17 16:52:01 +0800150static const char *alarmboot_cmdline = " androidboot.alarmboot=true";
Pavel Nedev898298c2013-02-27 12:36:09 -0800151static const char *loglevel = " quiet";
Ajay Dudanica3a33c2011-11-18 08:31:40 -0800152static const char *battchg_pause = " androidboot.mode=charger";
Shashank Mittalcd98d472011-08-02 14:29:24 -0700153static const char *auth_kernel = " androidboot.authorized_kernel=true";
Pavel Nedev5614d222013-06-17 18:01:02 +0300154static const char *secondary_gpt_enable = " gpt";
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200155static const char *mdtp_activated_flag = " mdtp";
David Ng183a7422009-12-07 14:55:21 -0800156
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800157static const char *baseband_apq = " androidboot.baseband=apq";
158static const char *baseband_msm = " androidboot.baseband=msm";
159static const char *baseband_csfb = " androidboot.baseband=csfb";
160static const char *baseband_svlte2a = " androidboot.baseband=svlte2a";
Ajay Dudani403bc492011-09-30 16:17:21 -0700161static const char *baseband_mdm = " androidboot.baseband=mdm";
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800162static const char *baseband_mdm2 = " androidboot.baseband=mdm2";
Amol Jadi5c61a952012-05-04 17:05:35 -0700163static const char *baseband_sglte = " androidboot.baseband=sglte";
Amol Jadi2a15a272013-01-22 12:03:36 -0800164static const char *baseband_dsda = " androidboot.baseband=dsda";
165static const char *baseband_dsda2 = " androidboot.baseband=dsda2";
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800166static const char *baseband_sglte2 = " androidboot.baseband=sglte2";
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800167static const char *warmboot_cmdline = " qpnp-power-on.warm_boot=1";
Ajay Dudanid04110c2011-01-17 23:55:07 -0800168
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700169#if VERIFIED_BOOT
Parth Dixitddbc7352015-10-18 03:13:31 +0530170#if !VBOOT_MOTA
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700171static const char *verity_mode = " androidboot.veritymode=";
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700172static const char *verified_state= " androidboot.verifiedbootstate=";
Parth Dixita5715a02015-10-29 12:25:10 +0530173static const char *keymaster_v1= " androidboot.keymaster=1";
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700174//indexed based on enum values, green is 0 by default
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700175
176struct verified_boot_verity_mode vbvm[] =
177{
178 {false, "logging"},
179 {true, "enforcing"},
180};
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700181struct verified_boot_state_name vbsn[] =
182{
183 {GREEN, "green"},
184 {ORANGE, "orange"},
185 {YELLOW,"yellow"},
186 {RED,"red" },
187};
188#endif
Parth Dixitddbc7352015-10-18 03:13:31 +0530189#endif
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700190
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700191static unsigned page_size = 0;
192static unsigned page_mask = 0;
Gaurav Nebhwani4d2389c2016-03-17 21:10:05 +0530193static unsigned mmc_blocksize = 0;
194static unsigned mmc_blocksize_mask = 0;
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700195static char ffbm_mode_string[FFBM_MODE_BUF_SIZE];
196static bool boot_into_ffbm;
vijay kumar870515d2015-08-31 16:37:24 +0530197static char *target_boot_params = NULL;
Matthew Qind886f3c2014-01-17 16:52:01 +0800198static bool boot_reason_alarm;
Channagoud Kadabi80a182b2015-03-11 17:04:23 -0700199static bool devinfo_present = true;
Channagoud Kadabi736c4962015-08-21 11:56:52 -0700200bool boot_into_fastboot = false;
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700201
Shashank Mittalcd98d472011-08-02 14:29:24 -0700202/* Assuming unauthorized kernel image by default */
203static int auth_kernel_img = 0;
Parth Dixit6f5822f2015-10-18 01:20:53 +0530204#if VBOOT_MOTA
205static device_info device = {DEVICE_MAGIC, 0, 0, 0, 0, {0}, {0},{0}};
206#else
lijuang511a2b52015-08-14 20:50:51 +0800207static device_info device = {DEVICE_MAGIC, 0, 0, 0, 0, {0}, {0},{0}, 1};
Parth Dixit6f5822f2015-10-18 01:20:53 +0530208#endif
vijay kumarc65876c2015-04-24 13:29:16 +0530209static bool is_allow_unlock = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -0700210
vijay kumarca2e6812015-07-08 20:28:25 +0530211static char frp_ptns[2][8] = {"config","frp"};
212
lijuang511a2b52015-08-14 20:50:51 +0800213static const char *critical_flash_allowed_ptn[] = {
214 "aboot",
215 "rpm",
216 "tz",
217 "sbl",
218 "sdi",
219 "sbl1",
220 "xbl",
221 "hyp",
222 "pmic",
223 "bootloader",
224 "devinfo",
225 "partition"};
226
Dima Zavin42168f22009-01-30 11:52:22 -0800227struct atag_ptbl_entry
228{
229 char name[16];
230 unsigned offset;
231 unsigned size;
232 unsigned flags;
233};
234
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700235/*
236 * Partition info, required to be published
237 * for fastboot
238 */
239struct getvar_partition_info {
240 const char part_name[MAX_GPT_NAME_SIZE]; /* Partition name */
241 char getvar_size[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for size */
242 char getvar_type[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for type */
243 char size_response[MAX_RSP_SIZE]; /* fastboot response for size */
244 char type_response[MAX_RSP_SIZE]; /* fastboot response for type */
245};
246
247/*
248 * Right now, we are publishing the info for only
249 * three partitions
250 */
251struct getvar_partition_info part_info[] =
252{
253 { "system" , "partition-size:", "partition-type:", "", "ext4" },
254 { "userdata", "partition-size:", "partition-type:", "", "ext4" },
255 { "cache" , "partition-size:", "partition-type:", "", "ext4" },
256};
257
258char max_download_size[MAX_RSP_SIZE];
Ameya Thakur11cf1a62013-08-05 12:44:48 -0700259char charger_screen_enabled[MAX_RSP_SIZE];
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800260char sn_buf[13];
Dhaval Patel223ec952013-07-18 14:49:44 -0700261char display_panel_buf[MAX_PANEL_BUF_SIZE];
Unnati Gandhi62c8ab82014-01-24 11:01:01 +0530262char panel_display_mode[MAX_RSP_SIZE];
lijuang102dfa92015-10-09 18:31:03 +0800263
264#if CHECK_BAT_VOLTAGE
lijuang65c5a822015-08-29 16:35:36 +0800265char battery_voltage[MAX_RSP_SIZE];
lijuang102dfa92015-10-09 18:31:03 +0800266char battery_soc_ok [MAX_RSP_SIZE];
267#endif
268
lijuangf16461c2015-08-03 17:09:34 +0800269char get_variant[MAX_RSP_SIZE];
Greg Griscod6250552011-06-29 14:40:23 -0700270
Greg Griscod2471ef2011-07-14 13:00:42 -0700271extern int emmc_recovery_init(void);
272
Kinson Chik0b1c8162011-08-31 16:31:57 -0700273#if NO_KEYPAD_DRIVER
274extern int fastboot_trigger(void);
275#endif
Greg Griscod2471ef2011-07-14 13:00:42 -0700276
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800277static void update_ker_tags_rdisk_addr(struct boot_img_hdr *hdr, bool is_arm64)
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700278{
279 /* overwrite the destination of specified for the project */
Channagoud Kadabi7042fa32013-04-26 16:44:14 -0700280#ifdef ABOOT_IGNORE_BOOT_HEADER_ADDRS
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800281 if (is_arm64)
282 hdr->kernel_addr = ABOOT_FORCE_KERNEL64_ADDR;
283 else
284 hdr->kernel_addr = ABOOT_FORCE_KERNEL_ADDR;
Channagoud Kadabi7042fa32013-04-26 16:44:14 -0700285 hdr->ramdisk_addr = ABOOT_FORCE_RAMDISK_ADDR;
286 hdr->tags_addr = ABOOT_FORCE_TAGS_ADDR;
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700287#endif
288}
289
Dima Zavin42168f22009-01-30 11:52:22 -0800290static void ptentry_to_tag(unsigned **ptr, struct ptentry *ptn)
291{
292 struct atag_ptbl_entry atag_ptn;
293
294 memcpy(atag_ptn.name, ptn->name, 16);
295 atag_ptn.name[15] = '\0';
296 atag_ptn.offset = ptn->start;
297 atag_ptn.size = ptn->length;
298 atag_ptn.flags = ptn->flags;
299 memcpy(*ptr, &atag_ptn, sizeof(struct atag_ptbl_entry));
300 *ptr += sizeof(struct atag_ptbl_entry) / sizeof(unsigned);
301}
Brian Swetland9c4c0752009-01-25 16:23:50 -0800302
lijuang102dfa92015-10-09 18:31:03 +0800303#if CHECK_BAT_VOLTAGE
304void update_battery_status(void)
305{
306 snprintf(battery_voltage,MAX_RSP_SIZE, "%d",target_get_battery_voltage());
307 snprintf(battery_soc_ok ,MAX_RSP_SIZE, "%s",target_battery_soc_ok()? "yes":"no");
308}
309#endif
310
Neeti Desaie245d492012-06-01 12:52:13 -0700311unsigned char *update_cmdline(const char * cmdline)
Brian Swetland9c4c0752009-01-25 16:23:50 -0800312{
David Ng183a7422009-12-07 14:55:21 -0800313 int cmdline_len = 0;
314 int have_cmdline = 0;
Amol Jadi168b7712012-03-06 16:15:00 -0800315 unsigned char *cmdline_final = NULL;
Neeti Desaie245d492012-06-01 12:52:13 -0700316 int pause_at_bootup = 0;
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800317 bool warm_boot = false;
Pavel Nedev5614d222013-06-17 18:01:02 +0300318 bool gpt_exists = partition_gpt_exists();
Joonwoo Park61112782013-10-02 19:50:39 -0700319 int have_target_boot_params = 0;
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700320 char *boot_dev_buf = NULL;
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200321 bool is_mdtp_activated = 0;
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700322#if VERIFIED_BOOT
Parth Dixitddbc7352015-10-18 03:13:31 +0530323#if !VBOOT_MOTA
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700324 uint32_t boot_state = boot_verify_get_state();
325#endif
Parth Dixitddbc7352015-10-18 03:13:31 +0530326#endif
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700327
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200328#ifdef MDTP_SUPPORT
329 mdtp_activated(&is_mdtp_activated);
330#endif /* MDTP_SUPPORT */
Dima Zavin42168f22009-01-30 11:52:22 -0800331
Brian Swetland9c4c0752009-01-25 16:23:50 -0800332 if (cmdline && cmdline[0]) {
David Ng183a7422009-12-07 14:55:21 -0800333 cmdline_len = strlen(cmdline);
334 have_cmdline = 1;
335 }
336 if (target_is_emmc_boot()) {
337 cmdline_len += strlen(emmc_cmdline);
Sridhar Parasuram7bd4aaf2015-02-12 11:14:38 -0800338#if USE_BOOTDEV_CMDLINE
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700339 boot_dev_buf = (char *) malloc(sizeof(char) * BOOT_DEV_MAX_LEN);
340 ASSERT(boot_dev_buf);
341 platform_boot_dev_cmdline(boot_dev_buf);
342 cmdline_len += strlen(boot_dev_buf);
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700343#endif
David Ng183a7422009-12-07 14:55:21 -0800344 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800345
346 cmdline_len += strlen(usb_sn_cmdline);
347 cmdline_len += strlen(sn_buf);
348
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700349#if VERIFIED_BOOT
Parth Dixitddbc7352015-10-18 03:13:31 +0530350#if !VBOOT_MOTA
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700351 cmdline_len += strlen(verified_state) + strlen(vbsn[boot_state].name);
Sridhar Parasuram7d8bb9a2015-09-23 18:21:19 -0700352 if ((device.verity_mode != 0 ) && (device.verity_mode != 1))
353 {
354 dprintf(CRITICAL, "Devinfo paritition possibly corrupted!!!. Please erase devinfo partition to continue booting\n");
355 ASSERT(0);
356 }
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700357 cmdline_len += strlen(verity_mode) + strlen(vbvm[device.verity_mode].name);
Parth Dixita5715a02015-10-29 12:25:10 +0530358 cmdline_len += strlen(keymaster_v1);
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700359#endif
Parth Dixitddbc7352015-10-18 03:13:31 +0530360#endif
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700361
Pavel Nedev5614d222013-06-17 18:01:02 +0300362 if (boot_into_recovery && gpt_exists)
363 cmdline_len += strlen(secondary_gpt_enable);
364
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200365 if(is_mdtp_activated)
366 cmdline_len += strlen(mdtp_activated_flag);
367
Pavel Nedev328ac822013-04-05 15:25:11 +0300368 if (boot_into_ffbm) {
369 cmdline_len += strlen(androidboot_mode);
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700370 cmdline_len += strlen(ffbm_mode_string);
Pavel Nedev898298c2013-02-27 12:36:09 -0800371 /* reduce kernel console messages to speed-up boot */
372 cmdline_len += strlen(loglevel);
Matthew Qind886f3c2014-01-17 16:52:01 +0800373 } else if (boot_reason_alarm) {
374 cmdline_len += strlen(alarmboot_cmdline);
Zhenhua Huang431dafa2015-06-30 16:13:37 +0800375 } else if ((target_build_variant_user() || device.charger_screen_enabled)
376 && target_pause_for_battery_charge()) {
David Ngf773dde2010-07-26 19:55:08 -0700377 pause_at_bootup = 1;
378 cmdline_len += strlen(battchg_pause);
379 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800380
Shashank Mittalcd98d472011-08-02 14:29:24 -0700381 if(target_use_signed_kernel() && auth_kernel_img) {
382 cmdline_len += strlen(auth_kernel);
383 }
384
Joonwoo Park61112782013-10-02 19:50:39 -0700385 if (get_target_boot_params(cmdline, boot_into_recovery ? "recoveryfs" :
386 "system",
vijay kumar870515d2015-08-31 16:37:24 +0530387 &target_boot_params) == 0) {
Joonwoo Park61112782013-10-02 19:50:39 -0700388 have_target_boot_params = 1;
389 cmdline_len += strlen(target_boot_params);
390 }
391
Ajay Dudanid04110c2011-01-17 23:55:07 -0800392 /* Determine correct androidboot.baseband to use */
393 switch(target_baseband())
394 {
395 case BASEBAND_APQ:
396 cmdline_len += strlen(baseband_apq);
397 break;
398
399 case BASEBAND_MSM:
400 cmdline_len += strlen(baseband_msm);
401 break;
402
403 case BASEBAND_CSFB:
404 cmdline_len += strlen(baseband_csfb);
405 break;
406
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800407 case BASEBAND_SVLTE2A:
408 cmdline_len += strlen(baseband_svlte2a);
Ajay Dudanid04110c2011-01-17 23:55:07 -0800409 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700410
411 case BASEBAND_MDM:
412 cmdline_len += strlen(baseband_mdm);
413 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700414
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800415 case BASEBAND_MDM2:
416 cmdline_len += strlen(baseband_mdm2);
417 break;
418
Amol Jadi5c61a952012-05-04 17:05:35 -0700419 case BASEBAND_SGLTE:
420 cmdline_len += strlen(baseband_sglte);
421 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530422
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800423 case BASEBAND_SGLTE2:
424 cmdline_len += strlen(baseband_sglte2);
425 break;
426
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530427 case BASEBAND_DSDA:
428 cmdline_len += strlen(baseband_dsda);
429 break;
Amol Jadi2a15a272013-01-22 12:03:36 -0800430
431 case BASEBAND_DSDA2:
432 cmdline_len += strlen(baseband_dsda2);
433 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800434 }
435
Lijuan Gao4a5b8322014-07-24 10:38:42 +0800436 if (cmdline) {
437 if ((strstr(cmdline, DISPLAY_DEFAULT_PREFIX) == NULL) &&
Padmanabhan Komandurubccbcdc2015-06-30 16:19:24 +0530438 target_display_panel_node(display_panel_buf,
439 MAX_PANEL_BUF_SIZE) &&
Lijuan Gao4a5b8322014-07-24 10:38:42 +0800440 strlen(display_panel_buf)) {
441 cmdline_len += strlen(display_panel_buf);
442 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700443 }
444
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800445 if (target_warm_boot()) {
446 warm_boot = true;
447 cmdline_len += strlen(warmboot_cmdline);
448 }
449
David Ng183a7422009-12-07 14:55:21 -0800450 if (cmdline_len > 0) {
451 const char *src;
Maria Yu52254c02014-07-04 16:14:54 +0800452 unsigned char *dst;
453
454 cmdline_final = (unsigned char*) malloc((cmdline_len + 4) & (~3));
455 ASSERT(cmdline_final != NULL);
vijay kumar287bb542015-09-29 13:01:52 +0530456 memset((void *)cmdline_final, 0, sizeof(*cmdline_final));
Maria Yu52254c02014-07-04 16:14:54 +0800457 dst = cmdline_final;
Neeti Desaie245d492012-06-01 12:52:13 -0700458
Amol Jadi168b7712012-03-06 16:15:00 -0800459 /* Save start ptr for debug print */
David Ng183a7422009-12-07 14:55:21 -0800460 if (have_cmdline) {
461 src = cmdline;
462 while ((*dst++ = *src++));
463 }
464 if (target_is_emmc_boot()) {
465 src = emmc_cmdline;
466 if (have_cmdline) --dst;
David Ngf773dde2010-07-26 19:55:08 -0700467 have_cmdline = 1;
468 while ((*dst++ = *src++));
Sridhar Parasuram7bd4aaf2015-02-12 11:14:38 -0800469#if USE_BOOTDEV_CMDLINE
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700470 src = boot_dev_buf;
471 if (have_cmdline) --dst;
472 while ((*dst++ = *src++));
473#endif
David Ngf773dde2010-07-26 19:55:08 -0700474 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800475
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700476#if VERIFIED_BOOT
Parth Dixitddbc7352015-10-18 03:13:31 +0530477#if !VBOOT_MOTA
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700478 src = verified_state;
479 if(have_cmdline) --dst;
480 have_cmdline = 1;
481 while ((*dst++ = *src++));
482 src = vbsn[boot_state].name;
483 if(have_cmdline) --dst;
484 while ((*dst++ = *src++));
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700485
Sridhar Parasuram7d8bb9a2015-09-23 18:21:19 -0700486 if ((device.verity_mode != 0 ) && (device.verity_mode != 1))
487 {
488 dprintf(CRITICAL, "Devinfo paritition possibly corrupted!!!. Please erase devinfo partition to continue booting\n");
489 ASSERT(0);
490 }
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700491 src = verity_mode;
492 if(have_cmdline) --dst;
493 while ((*dst++ = *src++));
494 src = vbvm[device.verity_mode].name;
495 if(have_cmdline) -- dst;
496 while ((*dst++ = *src++));
Parth Dixita5715a02015-10-29 12:25:10 +0530497 src = keymaster_v1;
498 if(have_cmdline) --dst;
499 while ((*dst++ = *src++));
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700500#endif
Parth Dixitddbc7352015-10-18 03:13:31 +0530501#endif
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800502 src = usb_sn_cmdline;
503 if (have_cmdline) --dst;
504 have_cmdline = 1;
505 while ((*dst++ = *src++));
506 src = sn_buf;
507 if (have_cmdline) --dst;
508 have_cmdline = 1;
509 while ((*dst++ = *src++));
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800510 if (warm_boot) {
511 if (have_cmdline) --dst;
512 src = warmboot_cmdline;
513 while ((*dst++ = *src++));
514 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800515
Pavel Nedev5614d222013-06-17 18:01:02 +0300516 if (boot_into_recovery && gpt_exists) {
517 src = secondary_gpt_enable;
518 if (have_cmdline) --dst;
519 while ((*dst++ = *src++));
520 }
521
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200522 if (is_mdtp_activated) {
523 src = mdtp_activated_flag;
524 if (have_cmdline) --dst;
525 while ((*dst++ = *src++));
526 }
527
Pavel Nedev328ac822013-04-05 15:25:11 +0300528 if (boot_into_ffbm) {
529 src = androidboot_mode;
530 if (have_cmdline) --dst;
531 while ((*dst++ = *src++));
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700532 src = ffbm_mode_string;
Pavel Nedev328ac822013-04-05 15:25:11 +0300533 if (have_cmdline) --dst;
534 while ((*dst++ = *src++));
Pavel Nedev898298c2013-02-27 12:36:09 -0800535 src = loglevel;
536 if (have_cmdline) --dst;
537 while ((*dst++ = *src++));
Matthew Qind886f3c2014-01-17 16:52:01 +0800538 } else if (boot_reason_alarm) {
539 src = alarmboot_cmdline;
540 if (have_cmdline) --dst;
541 while ((*dst++ = *src++));
Pavel Nedev328ac822013-04-05 15:25:11 +0300542 } else if (pause_at_bootup) {
David Ngf773dde2010-07-26 19:55:08 -0700543 src = battchg_pause;
544 if (have_cmdline) --dst;
David Ng183a7422009-12-07 14:55:21 -0800545 while ((*dst++ = *src++));
546 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800547
Shashank Mittalcd98d472011-08-02 14:29:24 -0700548 if(target_use_signed_kernel() && auth_kernel_img) {
549 src = auth_kernel;
550 if (have_cmdline) --dst;
551 while ((*dst++ = *src++));
552 }
553
Ajay Dudanid04110c2011-01-17 23:55:07 -0800554 switch(target_baseband())
555 {
556 case BASEBAND_APQ:
557 src = baseband_apq;
558 if (have_cmdline) --dst;
559 while ((*dst++ = *src++));
560 break;
561
562 case BASEBAND_MSM:
563 src = baseband_msm;
564 if (have_cmdline) --dst;
565 while ((*dst++ = *src++));
566 break;
567
568 case BASEBAND_CSFB:
569 src = baseband_csfb;
570 if (have_cmdline) --dst;
571 while ((*dst++ = *src++));
572 break;
573
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800574 case BASEBAND_SVLTE2A:
575 src = baseband_svlte2a;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800576 if (have_cmdline) --dst;
577 while ((*dst++ = *src++));
578 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700579
580 case BASEBAND_MDM:
581 src = baseband_mdm;
582 if (have_cmdline) --dst;
583 while ((*dst++ = *src++));
584 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700585
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800586 case BASEBAND_MDM2:
587 src = baseband_mdm2;
588 if (have_cmdline) --dst;
589 while ((*dst++ = *src++));
590 break;
591
Amol Jadi5c61a952012-05-04 17:05:35 -0700592 case BASEBAND_SGLTE:
593 src = baseband_sglte;
594 if (have_cmdline) --dst;
595 while ((*dst++ = *src++));
596 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530597
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800598 case BASEBAND_SGLTE2:
599 src = baseband_sglte2;
600 if (have_cmdline) --dst;
601 while ((*dst++ = *src++));
602 break;
603
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530604 case BASEBAND_DSDA:
605 src = baseband_dsda;
606 if (have_cmdline) --dst;
607 while ((*dst++ = *src++));
608 break;
Amol Jadi2a15a272013-01-22 12:03:36 -0800609
610 case BASEBAND_DSDA2:
611 src = baseband_dsda2;
612 if (have_cmdline) --dst;
613 while ((*dst++ = *src++));
614 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800615 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700616
617 if (strlen(display_panel_buf)) {
Dhaval Patel223ec952013-07-18 14:49:44 -0700618 src = display_panel_buf;
619 if (have_cmdline) --dst;
620 while ((*dst++ = *src++));
621 }
Joonwoo Park61112782013-10-02 19:50:39 -0700622
623 if (have_target_boot_params) {
624 if (have_cmdline) --dst;
625 src = target_boot_params;
626 while ((*dst++ = *src++));
vijay kumar870515d2015-08-31 16:37:24 +0530627 free(target_boot_params);
Joonwoo Park61112782013-10-02 19:50:39 -0700628 }
Neeti Desaie245d492012-06-01 12:52:13 -0700629 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700630
631
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700632 if (boot_dev_buf)
633 free(boot_dev_buf);
634
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -0800635 if (cmdline_final)
636 dprintf(INFO, "cmdline: %s\n", cmdline_final);
637 else
638 dprintf(INFO, "cmdline is NULL\n");
Neeti Desaie245d492012-06-01 12:52:13 -0700639 return cmdline_final;
640}
641
642unsigned *atag_core(unsigned *ptr)
643{
644 /* CORE */
645 *ptr++ = 2;
646 *ptr++ = 0x54410001;
647
648 return ptr;
649
650}
651
652unsigned *atag_ramdisk(unsigned *ptr, void *ramdisk,
653 unsigned ramdisk_size)
654{
655 if (ramdisk_size) {
656 *ptr++ = 4;
657 *ptr++ = 0x54420005;
658 *ptr++ = (unsigned)ramdisk;
659 *ptr++ = ramdisk_size;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800660 }
661
Neeti Desaie245d492012-06-01 12:52:13 -0700662 return ptr;
663}
664
665unsigned *atag_ptable(unsigned **ptr_addr)
666{
667 int i;
668 struct ptable *ptable;
669
670 if ((ptable = flash_get_ptable()) && (ptable->count != 0)) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700671 *(*ptr_addr)++ = 2 + (ptable->count * (sizeof(struct atag_ptbl_entry) /
672 sizeof(unsigned)));
Neeti Desaie245d492012-06-01 12:52:13 -0700673 *(*ptr_addr)++ = 0x4d534d70;
674 for (i = 0; i < ptable->count; ++i)
675 ptentry_to_tag(ptr_addr, ptable_get(ptable, i));
676 }
677
678 return (*ptr_addr);
679}
680
681unsigned *atag_cmdline(unsigned *ptr, const char *cmdline)
682{
683 int cmdline_length = 0;
684 int n;
Neeti Desaie245d492012-06-01 12:52:13 -0700685 char *dest;
686
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800687 cmdline_length = strlen((const char*)cmdline);
Neeti Desaie245d492012-06-01 12:52:13 -0700688 n = (cmdline_length + 4) & (~3);
689
690 *ptr++ = (n / 4) + 2;
691 *ptr++ = 0x54410009;
692 dest = (char *) ptr;
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800693 while ((*dest++ = *cmdline++));
Neeti Desaie245d492012-06-01 12:52:13 -0700694 ptr += (n / 4);
695
696 return ptr;
697}
698
699unsigned *atag_end(unsigned *ptr)
700{
Brian Swetland9c4c0752009-01-25 16:23:50 -0800701 /* END */
702 *ptr++ = 0;
703 *ptr++ = 0;
704
Neeti Desaie245d492012-06-01 12:52:13 -0700705 return ptr;
706}
707
708void generate_atags(unsigned *ptr, const char *cmdline,
709 void *ramdisk, unsigned ramdisk_size)
710{
711
712 ptr = atag_core(ptr);
713 ptr = atag_ramdisk(ptr, ramdisk, ramdisk_size);
714 ptr = target_atag_mem(ptr);
715
716 /* Skip NAND partition ATAGS for eMMC boot */
717 if (!target_is_emmc_boot()){
718 ptr = atag_ptable(&ptr);
719 }
720
721 ptr = atag_cmdline(ptr, cmdline);
722 ptr = atag_end(ptr);
723}
724
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700725typedef void entry_func_ptr(unsigned, unsigned, unsigned*);
Neeti Desaie245d492012-06-01 12:52:13 -0700726void boot_linux(void *kernel, unsigned *tags,
727 const char *cmdline, unsigned machtype,
728 void *ramdisk, unsigned ramdisk_size)
729{
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800730 unsigned char *final_cmdline;
Amol Jadib6be5c12012-11-14 13:39:51 -0800731#if DEVICE_TREE
Neeti Desai17379b82012-06-04 18:42:53 -0700732 int ret = 0;
Amol Jadib6be5c12012-11-14 13:39:51 -0800733#endif
734
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700735 void (*entry)(unsigned, unsigned, unsigned*) = (entry_func_ptr*)(PA((addr_t)kernel));
Deepa Dinamani25a9f762012-11-30 15:57:15 -0800736 uint32_t tags_phys = PA((addr_t)tags);
vijay kumar1a50a642015-11-16 12:41:15 +0530737 struct kernel64_hdr *kptr = ((struct kernel64_hdr*)(PA((addr_t)kernel)));
Deepa Dinamani25a9f762012-11-30 15:57:15 -0800738
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530739 ramdisk = (void *)PA((addr_t)ramdisk);
Neeti Desaie245d492012-06-01 12:52:13 -0700740
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800741 final_cmdline = update_cmdline((const char*)cmdline);
742
Neeti Desai17379b82012-06-04 18:42:53 -0700743#if DEVICE_TREE
Amol Jadib6be5c12012-11-14 13:39:51 -0800744 dprintf(INFO, "Updating device tree: start\n");
745
Neeti Desai17379b82012-06-04 18:42:53 -0700746 /* Update the Device Tree */
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530747 ret = update_device_tree((void *)tags,(const char *)final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -0700748 if(ret)
749 {
750 dprintf(CRITICAL, "ERROR: Updating Device Tree Failed \n");
751 ASSERT(0);
752 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800753 dprintf(INFO, "Updating device tree: done\n");
Neeti Desai17379b82012-06-04 18:42:53 -0700754#else
Neeti Desaie245d492012-06-01 12:52:13 -0700755 /* Generating the Atags */
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800756 generate_atags(tags, final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -0700757#endif
Neeti Desaie245d492012-06-01 12:52:13 -0700758
Maria Yu52254c02014-07-04 16:14:54 +0800759 free(final_cmdline);
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -0700760
761#if VERIFIED_BOOT
lijuangbdd9bb42016-03-01 18:22:17 +0800762#if !VBOOT_MOTA
763 if (device.verity_mode == 0) {
764#if FBCON_DISPLAY_MSG
765 display_bootverify_menu(DISPLAY_MENU_LOGGING);
766 wait_for_users_action();
767#else
768 dprintf(CRITICAL,
769 "The dm-verity is not started in enforcing mode.\nWait for 5 seconds before proceeding\n");
770 mdelay(5000);
771#endif
772 }
773
774#endif
775#endif
776
777#if VERIFIED_BOOT
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -0700778 /* Write protect the device info */
Channagoud Kadabi3bd9d1e2015-05-05 16:18:20 -0700779 if (!boot_into_recovery && target_build_variant_user() && devinfo_present && mmc_write_protect("devinfo", 1))
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -0700780 {
781 dprintf(INFO, "Failed to write protect dev info\n");
782 ASSERT(0);
783 }
784#endif
785
Aravind Venkateswaran8f076242014-02-25 16:25:30 -0800786 /* Turn off splash screen if enabled */
787#if DISPLAY_SPLASH_SCREEN
788 target_display_shutdown();
789#endif
790
Veera Sundaram Sankaran67ea0932015-09-25 10:09:30 -0700791 /* Perform target specific cleanup */
792 target_uninit();
Aravind Venkateswaran8f076242014-02-25 16:25:30 -0800793
Deepa Dinamani33734bc2013-03-06 12:16:06 -0800794 dprintf(INFO, "booting linux @ %p, ramdisk @ %p (%d), tags/device tree @ %p\n",
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530795 entry, ramdisk, ramdisk_size, (void *)tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800796
797 enter_critical_section();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700798
Amol Jadi4421e652011-06-16 15:00:48 -0700799 /* do any platform specific cleanup before kernel entry */
800 platform_uninit();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700801
Brian Swetland9c4c0752009-01-25 16:23:50 -0800802 arch_disable_cache(UCACHE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700803
Amol Jadi504f9fe2012-08-16 13:56:48 -0700804#if ARM_WITH_MMU
Brian Swetland9c4c0752009-01-25 16:23:50 -0800805 arch_disable_mmu();
Amol Jadi504f9fe2012-08-16 13:56:48 -0700806#endif
Amol Jadi492d5a52013-03-15 16:12:34 -0700807 bs_set_timestamp(BS_KERNEL_ENTRY);
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800808
809 if (IS_ARM64(kptr))
810 /* Jump to a 64bit kernel */
811 scm_elexec_call((paddr_t)kernel, tags_phys);
812 else
813 /* Jump to a 32bit kernel */
814 entry(0, machtype, (unsigned*)tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800815}
816
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700817/* Function to check if the memory address range falls within the aboot
818 * boundaries.
819 * start: Start of the memory region
820 * size: Size of the memory region
821 */
Vijay Kumar Pendoti9c002ad2016-03-09 13:52:45 +0530822int check_aboot_addr_range_overlap(uintptr_t start, uint32_t size)
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700823{
824 /* Check for boundary conditions. */
Sundarajan Srinivasance2a0ea2013-12-16 17:02:56 -0800825 if ((UINT_MAX - start) < size)
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700826 return -1;
827
828 /* Check for memory overlap. */
829 if ((start < MEMBASE) && ((start + size) <= MEMBASE))
830 return 0;
Channagoud Kadabi94143912013-10-15 12:53:52 -0700831 else if (start >= (MEMBASE + MEMSIZE))
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700832 return 0;
833 else
834 return -1;
835}
836
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800837#define ROUND_TO_PAGE(x,y) (((x) + (y)) & (~(y)))
Brian Swetland9c4c0752009-01-25 16:23:50 -0800838
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800839BUF_DMA_ALIGN(buf, BOOT_IMG_MAX_PAGE_SIZE); //Equal to max-supported pagesize
Amol Jadib6be5c12012-11-14 13:39:51 -0800840#if DEVICE_TREE
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800841BUF_DMA_ALIGN(dt_buf, BOOT_IMG_MAX_PAGE_SIZE);
Amol Jadib6be5c12012-11-14 13:39:51 -0800842#endif
Dima Zavin214cc642009-01-26 11:16:21 -0800843
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700844static void verify_signed_bootimg(uint32_t bootimg_addr, uint32_t bootimg_size)
845{
846 int ret;
Channagoud Kadabia8c623f2015-01-13 14:48:48 -0800847
848#if !VERIFIED_BOOT
Sundarajan Srinivasance54d6e2013-11-11 12:45:00 -0800849#if IMAGE_VERIF_ALGO_SHA1
850 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA1;
851#else
852 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA256;
853#endif
Channagoud Kadabia8c623f2015-01-13 14:48:48 -0800854#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700855
856 /* Assume device is rooted at this time. */
857 device.is_tampered = 1;
858
859 dprintf(INFO, "Authenticating boot image (%d): start\n", bootimg_size);
860
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700861#if VERIFIED_BOOT
862 if(boot_into_recovery)
863 {
864 ret = boot_verify_image((unsigned char *)bootimg_addr,
Unnati Gandhi47051252015-03-02 15:21:09 +0530865 bootimg_size, "/recovery");
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700866 }
867 else
868 {
869 ret = boot_verify_image((unsigned char *)bootimg_addr,
Unnati Gandhi47051252015-03-02 15:21:09 +0530870 bootimg_size, "/boot");
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700871 }
872 boot_verify_print_state();
873#else
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700874 ret = image_verify((unsigned char *)bootimg_addr,
875 (unsigned char *)(bootimg_addr + bootimg_size),
876 bootimg_size,
Sundarajan Srinivasance54d6e2013-11-11 12:45:00 -0800877 auth_algo);
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700878#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700879 dprintf(INFO, "Authenticating boot image: done return value = %d\n", ret);
880
881 if (ret)
882 {
883 /* Authorized kernel */
884 device.is_tampered = 0;
Sundarajan Srinivasan3fb21f12013-09-16 18:36:15 -0700885 auth_kernel_img = 1;
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700886 }
887
Amit Blay4aa292f2015-04-28 21:55:59 +0300888#ifdef MDTP_SUPPORT
889 {
890 /* Verify MDTP lock.
891 * For boot & recovery partitions, use aboot's verification result.
892 */
893 mdtp_ext_partition_verification_t ext_partition;
894 ext_partition.partition = boot_into_recovery ? MDTP_PARTITION_RECOVERY : MDTP_PARTITION_BOOT;
895 ext_partition.integrity_state = device.is_tampered ? MDTP_PARTITION_STATE_INVALID : MDTP_PARTITION_STATE_VALID;
896 ext_partition.page_size = 0; /* Not needed since already validated */
897 ext_partition.image_addr = 0; /* Not needed since already validated */
898 ext_partition.image_size = 0; /* Not needed since already validated */
899 ext_partition.sig_avail = FALSE; /* Not needed since already validated */
900 mdtp_fwlock_verify_lock(&ext_partition);
901 }
902#endif /* MDTP_SUPPORT */
903
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700904#if USE_PCOM_SECBOOT
905 set_tamper_flag(device.is_tampered);
906#endif
907
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700908#if VERIFIED_BOOT
Sridhar Parasuram8b792422015-07-05 11:38:13 -0700909 switch(boot_verify_get_state())
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700910 {
Sridhar Parasuram8b792422015-07-05 11:38:13 -0700911 case RED:
lijuanga40d6302015-07-20 20:10:13 +0800912#if FBCON_DISPLAY_MSG
lijuang9a7d3b92015-11-30 14:41:24 +0800913 display_bootverify_menu(DISPLAY_MENU_RED);
lijuanga40d6302015-07-20 20:10:13 +0800914 wait_for_users_action();
915#else
916 dprintf(CRITICAL,
917 "Your device has failed verification and may not work properly.\nWait for 5 seconds before proceeding\n");
918 mdelay(5000);
919#endif
920
921 break;
Sridhar Parasuram8b792422015-07-05 11:38:13 -0700922 case YELLOW:
lijuanga40d6302015-07-20 20:10:13 +0800923#if FBCON_DISPLAY_MSG
lijuang9a7d3b92015-11-30 14:41:24 +0800924 display_bootverify_menu(DISPLAY_MENU_YELLOW);
lijuanga40d6302015-07-20 20:10:13 +0800925 wait_for_users_action();
926#else
927 dprintf(CRITICAL,
928 "Your device has loaded a different operating system.\nWait for 5 seconds before proceeding\n");
929 mdelay(5000);
930#endif
931 break;
Sridhar Parasuram8b792422015-07-05 11:38:13 -0700932 default:
lijuanga40d6302015-07-20 20:10:13 +0800933 break;
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700934 }
935#endif
Sridhar Parasuram8b792422015-07-05 11:38:13 -0700936#if !VERIFIED_BOOT
Unnati Gandhi1be04752015-03-27 19:41:53 +0530937 if(device.is_tampered)
938 {
939 write_device_info_mmc(&device);
940 #ifdef TZ_TAMPER_FUSE
941 set_tamper_fuse_cmd();
942 #endif
943 #ifdef ASSERT_ON_TAMPER
944 dprintf(CRITICAL, "Device is tampered. Asserting..\n");
945 ASSERT(0);
946 #endif
947 }
Sridhar Parasuram8b792422015-07-05 11:38:13 -0700948#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700949}
950
Maunik Shah0f3c8ac2014-03-02 17:47:58 +0530951static bool check_format_bit()
952{
953 bool ret = false;
954 int index;
955 uint64_t offset;
956 struct boot_selection_info *in = NULL;
957 char *buf = NULL;
958
959 index = partition_get_index("bootselect");
960 if (index == INVALID_PTN)
961 {
962 dprintf(INFO, "Unable to locate /bootselect partition\n");
963 return ret;
964 }
965 offset = partition_get_offset(index);
966 if(!offset)
967 {
968 dprintf(INFO, "partition /bootselect doesn't exist\n");
969 return ret;
970 }
971 buf = (char *) memalign(CACHE_LINE, ROUNDUP(page_size, CACHE_LINE));
972 ASSERT(buf);
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530973 if (mmc_read(offset, (uint32_t *)buf, page_size))
Maunik Shah0f3c8ac2014-03-02 17:47:58 +0530974 {
975 dprintf(INFO, "mmc read failure /bootselect %d\n", page_size);
976 free(buf);
977 return ret;
978 }
979 in = (struct boot_selection_info *) buf;
980 if ((in->signature == BOOTSELECT_SIGNATURE) &&
981 (in->version == BOOTSELECT_VERSION)) {
982 if ((in->state_info & BOOTSELECT_FORMAT) &&
983 !(in->state_info & BOOTSELECT_FACTORY))
984 ret = true;
985 } else {
986 dprintf(CRITICAL, "Signature: 0x%08x or version: 0x%08x mismatched of /bootselect\n",
987 in->signature, in->version);
988 ASSERT(0);
989 }
990 free(buf);
991 return ret;
992}
993
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700994void boot_verifier_init()
995{
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700996 uint32_t boot_state;
997 /* Check if device unlock */
998 if(device.is_unlocked)
999 {
1000 boot_verify_send_event(DEV_UNLOCK);
1001 boot_verify_print_state();
1002 dprintf(CRITICAL, "Device is unlocked! Skipping verification...\n");
1003 return;
1004 }
1005 else
1006 {
1007 boot_verify_send_event(BOOT_INIT);
1008 }
1009
1010 /* Initialize keystore */
1011 boot_state = boot_verify_keystore_init();
1012 if(boot_state == YELLOW)
1013 {
1014 boot_verify_print_state();
1015 dprintf(CRITICAL, "Keystore verification failed! Continuing anyways...\n");
1016 }
1017}
1018
Shashank Mittal23b8f422010-04-16 19:27:21 -07001019int boot_linux_from_mmc(void)
1020{
1021 struct boot_img_hdr *hdr = (void*) buf;
1022 struct boot_img_hdr *uhdr;
1023 unsigned offset = 0;
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07001024 int rcode;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001025 unsigned long long ptn = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001026 int index = INVALID_PTN;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001027
Shashank Mittalcd98d472011-08-02 14:29:24 -07001028 unsigned char *image_addr = 0;
1029 unsigned kernel_actual;
1030 unsigned ramdisk_actual;
1031 unsigned imagesize_actual;
Neeti Desai465491e2012-07-31 12:53:35 -07001032 unsigned second_actual = 0;
Neeti Desai465491e2012-07-31 12:53:35 -07001033
Matthew Qin271927e2015-03-31 22:07:07 -04001034 unsigned int dtb_size = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08001035 unsigned int out_len = 0;
1036 unsigned int out_avai_len = 0;
1037 unsigned char *out_addr = NULL;
1038 uint32_t dtb_offset = 0;
1039 unsigned char *kernel_start_addr = NULL;
1040 unsigned int kernel_size = 0;
1041 int rc;
1042
Neeti Desai465491e2012-07-31 12:53:35 -07001043#if DEVICE_TREE
1044 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -07001045 struct dt_entry dt_entry;
Neeti Desai465491e2012-07-31 12:53:35 -07001046 unsigned dt_table_offset;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001047 uint32_t dt_actual;
Deepa Dinamani19648b42013-09-05 17:05:55 -07001048 uint32_t dt_hdr_size;
Matthew Qin271927e2015-03-31 22:07:07 -04001049 unsigned char *best_match_dt_addr = NULL;
Neeti Desai465491e2012-07-31 12:53:35 -07001050#endif
Matthew Qin49e51fa2015-02-09 10:40:45 +08001051 struct kernel64_hdr *kptr = NULL;
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001052
Maunik Shah0f3c8ac2014-03-02 17:47:58 +05301053 if (check_format_bit())
1054 boot_into_recovery = 1;
1055
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07001056 if (!boot_into_recovery) {
1057 memset(ffbm_mode_string, '\0', sizeof(ffbm_mode_string));
1058 rcode = get_ffbm(ffbm_mode_string, sizeof(ffbm_mode_string));
1059 if (rcode <= 0) {
1060 boot_into_ffbm = false;
1061 if (rcode < 0)
1062 dprintf(CRITICAL,"failed to get ffbm cookie");
1063 } else
1064 boot_into_ffbm = true;
1065 } else
1066 boot_into_ffbm = false;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001067 uhdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
1068 if (!memcmp(uhdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1069 dprintf(INFO, "Unified boot method!\n");
1070 hdr = uhdr;
1071 goto unified_boot;
1072 }
Greg Griscod6250552011-06-29 14:40:23 -07001073 if (!boot_into_recovery) {
Kinson Chikf1a43512011-07-14 11:28:39 -07001074 index = partition_get_index("boot");
1075 ptn = partition_get_offset(index);
1076 if(ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -07001077 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1078 return -1;
1079 }
Kinson Chikf1a43512011-07-14 11:28:39 -07001080 }
1081 else {
1082 index = partition_get_index("recovery");
1083 ptn = partition_get_offset(index);
1084 if(ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -07001085 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
1086 return -1;
1087 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001088 }
Channagoud Kadabief0547c2015-02-10 12:57:38 -08001089 /* Set Lun for boot & recovery partitions */
1090 mmc_set_lun(partition_get_lun(index));
Shashank Mittal23b8f422010-04-16 19:27:21 -07001091
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301092 if (mmc_read(ptn + offset, (uint32_t *) buf, page_size)) {
Shashank Mittal23b8f422010-04-16 19:27:21 -07001093 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
1094 return -1;
1095 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001096
1097 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001098 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Shashank Mittal23b8f422010-04-16 19:27:21 -07001099 return -1;
1100 }
1101
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07001102 if (hdr->page_size && (hdr->page_size != page_size)) {
vijay kumar2e21b3a2014-06-26 17:40:15 +05301103
1104 if (hdr->page_size > BOOT_IMG_MAX_PAGE_SIZE) {
1105 dprintf(CRITICAL, "ERROR: Invalid page size\n");
1106 return -1;
1107 }
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07001108 page_size = hdr->page_size;
1109 page_mask = page_size - 1;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001110 }
1111
vijay kumar287bb542015-09-29 13:01:52 +05301112 /* ensure commandline is terminated */
1113 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
1114
Matthew Qin49e51fa2015-02-09 10:40:45 +08001115 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1116 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001117
Matthew Qin49e51fa2015-02-09 10:40:45 +08001118 image_addr = (unsigned char *)target_get_scratch_address();
1119
1120#if DEVICE_TREE
1121 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
1122 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + dt_actual);
1123#else
1124 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
1125#endif
1126
1127#if VERIFIED_BOOT
1128 boot_verifier_init();
1129#endif
1130
Vijay Kumar Pendoti9c002ad2016-03-09 13:52:45 +05301131 if (check_aboot_addr_range_overlap((uintptr_t) image_addr, imagesize_actual))
Matthew Qin49e51fa2015-02-09 10:40:45 +08001132 {
1133 dprintf(CRITICAL, "Boot image buffer address overlaps with aboot addresses.\n");
1134 return -1;
1135 }
1136
Matthew Qinbb7923d2015-02-09 10:56:09 +08001137 /*
1138 * Update loading flow of bootimage to support compressed/uncompressed
1139 * bootimage on both 64bit and 32bit platform.
1140 * 1. Load bootimage from emmc partition onto DDR.
1141 * 2. Check if bootimage is gzip format. If yes, decompress compressed kernel
1142 * 3. Check kernel header and update kernel load addr for 64bit and 32bit
1143 * platform accordingly.
1144 * 4. Sanity Check on kernel_addr and ramdisk_addr and copy data.
1145 */
1146
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001147 dprintf(INFO, "Loading (%s) image (%d): start\n",
1148 (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001149 bs_set_timestamp(BS_KERNEL_LOAD_START);
1150
Gaurav Nebhwani43e2a462016-03-17 21:32:56 +05301151 if ((target_get_max_flash_size() - page_size) < imagesize_actual)
1152 {
1153 dprintf(CRITICAL, "booimage size is greater than DDR can hold\n");
1154 return -1;
1155 }
1156
Matthew Qinbb7923d2015-02-09 10:56:09 +08001157 /* Read image without signature */
Matthew Qin49e51fa2015-02-09 10:40:45 +08001158 if (mmc_read(ptn + offset, (void *)image_addr, imagesize_actual))
1159 {
1160 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
1161 return -1;
1162 }
1163
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001164 dprintf(INFO, "Loading (%s) image (%d): done\n",
1165 (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual);
1166
Matthew Qin49e51fa2015-02-09 10:40:45 +08001167 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
1168
1169 /* Authenticate Kernel */
1170 dprintf(INFO, "use_signed_kernel=%d, is_unlocked=%d, is_tampered=%d.\n",
1171 (int) target_use_signed_kernel(),
1172 device.is_unlocked,
1173 device.is_tampered);
1174
Channagoud Kadabi736c4962015-08-21 11:56:52 -07001175 /* Change the condition a little bit to include the test framework support.
1176 * We would never reach this point if device is in fastboot mode, even if we did
1177 * that means we are in test mode, so execute kernel authentication part for the
1178 * tests */
Parth Dixitcb0c6082015-12-30 15:01:13 +05301179 if((target_use_signed_kernel() && (!device.is_unlocked)) || is_test_mode_enabled())
Matthew Qin49e51fa2015-02-09 10:40:45 +08001180 {
1181 offset = imagesize_actual;
Vijay Kumar Pendoti9c002ad2016-03-09 13:52:45 +05301182 if (check_aboot_addr_range_overlap((uintptr_t)image_addr + offset, page_size))
Matthew Qin49e51fa2015-02-09 10:40:45 +08001183 {
1184 dprintf(CRITICAL, "Signature read buffer address overlaps with aboot addresses.\n");
1185 return -1;
1186 }
1187
1188 /* Read signature */
1189 if(mmc_read(ptn + offset, (void *)(image_addr + offset), page_size))
1190 {
1191 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
1192 return -1;
1193 }
1194
1195 verify_signed_bootimg((uint32_t)image_addr, imagesize_actual);
Channagoud Kadabi736c4962015-08-21 11:56:52 -07001196 /* The purpose of our test is done here */
Parth Dixitcb0c6082015-12-30 15:01:13 +05301197 if(is_test_mode_enabled() && auth_kernel_img)
Channagoud Kadabi736c4962015-08-21 11:56:52 -07001198 return 0;
Matthew Qin49e51fa2015-02-09 10:40:45 +08001199 } else {
1200 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
1201 #ifdef TZ_SAVE_KERNEL_HASH
1202 aboot_save_boot_hash_mmc((uint32_t) image_addr, imagesize_actual);
1203 #endif /* TZ_SAVE_KERNEL_HASH */
Amit Blay4aa292f2015-04-28 21:55:59 +03001204
1205#ifdef MDTP_SUPPORT
1206 {
1207 /* Verify MDTP lock.
1208 * For boot & recovery partitions, MDTP will use boot_verifier APIs,
1209 * since verification was skipped in aboot. The signature is not part of the loaded image.
1210 */
1211 mdtp_ext_partition_verification_t ext_partition;
1212 ext_partition.partition = boot_into_recovery ? MDTP_PARTITION_RECOVERY : MDTP_PARTITION_BOOT;
1213 ext_partition.integrity_state = MDTP_PARTITION_STATE_UNSET;
1214 ext_partition.page_size = page_size;
1215 ext_partition.image_addr = (uint32)image_addr;
1216 ext_partition.image_size = imagesize_actual;
1217 ext_partition.sig_avail = FALSE;
1218 mdtp_fwlock_verify_lock(&ext_partition);
1219 }
1220#endif /* MDTP_SUPPORT */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001221 }
1222
Sridhar Parasuram00bfedb2015-05-26 14:21:27 -07001223#if VERIFIED_BOOT
Reut Zysmanba8a9d52016-02-18 11:44:04 +02001224 if(boot_verify_get_state() == ORANGE)
1225 {
1226#if FBCON_DISPLAY_MSG
1227 display_bootverify_menu(DISPLAY_MENU_ORANGE);
1228 wait_for_users_action();
1229#else
1230 dprintf(CRITICAL,
1231 "Your device has been unlocked and can't be trusted.\nWait for 5 seconds before proceeding\n");
1232 mdelay(5000);
1233#endif
1234 }
1235#endif
1236
1237#if VERIFIED_BOOT
Parth Dixitbc9b6492015-10-18 00:41:38 +05301238#if !VBOOT_MOTA
Sridhar Parasuram00bfedb2015-05-26 14:21:27 -07001239 // send root of trust
Sridhar Parasuram96300dc2015-06-11 10:37:11 -07001240 if(!send_rot_command((uint32_t)device.is_unlocked))
Sridhar Parasuram00bfedb2015-05-26 14:21:27 -07001241 ASSERT(0);
1242#endif
Parth Dixitbc9b6492015-10-18 00:41:38 +05301243#endif
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001244 /*
Matthew Qinbb7923d2015-02-09 10:56:09 +08001245 * Check if the kernel image is a gzip package. If yes, need to decompress it.
1246 * If not, continue booting.
1247 */
1248 if (is_gzip_package((unsigned char *)(image_addr + page_size), hdr->kernel_size))
1249 {
1250 out_addr = (unsigned char *)(image_addr + imagesize_actual + page_size);
1251 out_avai_len = target_get_max_flash_size() - imagesize_actual - page_size;
Matthew Qin0b15b322015-05-19 05:20:54 -04001252 dprintf(INFO, "decompressing kernel image: start\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001253 rc = decompress((unsigned char *)(image_addr + page_size),
1254 hdr->kernel_size, out_addr, out_avai_len,
1255 &dtb_offset, &out_len);
1256 if (rc)
1257 {
Matthew Qin0b15b322015-05-19 05:20:54 -04001258 dprintf(CRITICAL, "decompressing kernel image failed!!!\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001259 ASSERT(0);
1260 }
1261
Matthew Qin0b15b322015-05-19 05:20:54 -04001262 dprintf(INFO, "decompressing kernel image: done\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001263 kptr = (struct kernel64_hdr *)out_addr;
1264 kernel_start_addr = out_addr;
1265 kernel_size = out_len;
1266 } else {
1267 kptr = (struct kernel64_hdr *)(image_addr + page_size);
1268 kernel_start_addr = (unsigned char *)(image_addr + page_size);
1269 kernel_size = hdr->kernel_size;
1270 }
1271
1272 /*
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001273 * Update the kernel/ramdisk/tags address if the boot image header
1274 * has default values, these default values come from mkbootimg when
1275 * the boot image is flashed using fastboot flash:raw
1276 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001277 update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr));
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001278
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001279 /* Get virtual addresses since the hdr saves physical addresses. */
1280 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
1281 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
1282 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001283
Matthew Qinbb7923d2015-02-09 10:56:09 +08001284 kernel_size = ROUND_TO_PAGE(kernel_size, page_mask);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001285 /* Check if the addresses in the header are valid. */
Matthew Qinbb7923d2015-02-09 10:56:09 +08001286 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_size) ||
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001287 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
1288 {
1289 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
1290 return -1;
1291 }
1292
1293#ifndef DEVICE_TREE
1294 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1295 {
1296 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
1297 return -1;
1298 }
1299#endif
1300
Matthew Qin49e51fa2015-02-09 10:40:45 +08001301 /* Move kernel, ramdisk and device tree to correct address */
Matthew Qinbb7923d2015-02-09 10:56:09 +08001302 memmove((void*) hdr->kernel_addr, kernel_start_addr, kernel_size);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001303 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001304
Matthew Qin49e51fa2015-02-09 10:40:45 +08001305 #if DEVICE_TREE
1306 if(hdr->dt_size) {
1307 dt_table_offset = ((uint32_t)image_addr + page_size + kernel_actual + ramdisk_actual + second_actual);
1308 table = (struct dt_table*) dt_table_offset;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001309
Matthew Qin49e51fa2015-02-09 10:40:45 +08001310 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
1311 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1312 return -1;
1313 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001314
P.V. Phani Kumar82916762016-03-09 09:20:19 +05301315 /* Its Error if, dt_hdr_size (table->num_entries * dt_entry size + Dev_Tree Header)
1316 goes beyound hdr->dt_size*/
1317 if (dt_hdr_size > ROUND_TO_PAGE(hdr->dt_size,hdr->page_size)) {
1318 dprintf(CRITICAL, "ERROR: Invalid Device Tree size \n");
1319 return -1;
1320 }
1321
Matthew Qin49e51fa2015-02-09 10:40:45 +08001322 /* Find index of device tree within device tree table */
1323 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
1324 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1325 return -1;
1326 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001327
P.V. Phani Kumar3c333202016-03-09 11:54:37 +05301328 if(dt_entry.offset > (UINT_MAX - dt_entry.size)) {
1329 dprintf(CRITICAL, "ERROR: Device tree contents are Invalid\n");
1330 return -1;
1331 }
1332
1333 /* Ensure we are not overshooting dt_size with the dt_entry selected */
1334 if ((dt_entry.offset + dt_entry.size) > hdr->dt_size) {
1335 dprintf(CRITICAL, "ERROR: Device tree contents are Invalid\n");
1336 return -1;
1337 }
1338
Matthew Qin271927e2015-03-31 22:07:07 -04001339 if (is_gzip_package((unsigned char *)dt_table_offset + dt_entry.offset, dt_entry.size))
1340 {
1341 unsigned int compressed_size = 0;
1342 out_addr += out_len;
1343 out_avai_len -= out_len;
Matthew Qin0b15b322015-05-19 05:20:54 -04001344 dprintf(INFO, "decompressing dtb: start\n");
Matthew Qin271927e2015-03-31 22:07:07 -04001345 rc = decompress((unsigned char *)dt_table_offset + dt_entry.offset,
1346 dt_entry.size, out_addr, out_avai_len,
1347 &compressed_size, &dtb_size);
1348 if (rc)
1349 {
Matthew Qin0b15b322015-05-19 05:20:54 -04001350 dprintf(CRITICAL, "decompressing dtb failed!!!\n");
Matthew Qin271927e2015-03-31 22:07:07 -04001351 ASSERT(0);
1352 }
1353
Matthew Qin0b15b322015-05-19 05:20:54 -04001354 dprintf(INFO, "decompressing dtb: done\n");
Matthew Qin271927e2015-03-31 22:07:07 -04001355 best_match_dt_addr = out_addr;
1356 } else {
1357 best_match_dt_addr = (unsigned char *)dt_table_offset + dt_entry.offset;
1358 dtb_size = dt_entry.size;
1359 }
1360
Matthew Qin49e51fa2015-02-09 10:40:45 +08001361 /* Validate and Read device device tree in the tags_addr */
Matthew Qin271927e2015-03-31 22:07:07 -04001362 if (check_aboot_addr_range_overlap(hdr->tags_addr, dtb_size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001363 {
1364 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1365 return -1;
1366 }
Shashank Mittal162244e2011-08-08 19:01:25 -07001367
Matthew Qin271927e2015-03-31 22:07:07 -04001368 memmove((void *)hdr->tags_addr, (char *)best_match_dt_addr, dtb_size);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001369 } else {
1370 /* Validate the tags_addr */
1371 if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual))
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001372 {
1373 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1374 return -1;
1375 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08001376 /*
1377 * If appended dev tree is found, update the atags with
1378 * memory address to the DTB appended location on RAM.
1379 * Else update with the atags address in the kernel header
1380 */
1381 void *dtb;
Matthew Qinbb7923d2015-02-09 10:56:09 +08001382 dtb = dev_tree_appended((void*)(image_addr + page_size),
1383 hdr->kernel_size, dtb_offset,
Matthew Qin49e51fa2015-02-09 10:40:45 +08001384 (void *)hdr->tags_addr);
1385 if (!dtb) {
1386 dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001387 return -1;
1388 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07001389 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08001390 #endif
Shashank Mittal23b8f422010-04-16 19:27:21 -07001391
Stanimir Varbanov69ec5462013-07-18 18:17:42 +03001392 if (boot_into_recovery && !device.is_unlocked && !device.is_tampered)
1393 target_load_ssd_keystore();
1394
Shashank Mittal23b8f422010-04-16 19:27:21 -07001395unified_boot:
Shashank Mittal23b8f422010-04-16 19:27:21 -07001396
Dima Zavin77e41f32013-03-06 16:10:43 -08001397 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07001398 (const char *)hdr->cmdline, board_machtype(),
Shashank Mittal23b8f422010-04-16 19:27:21 -07001399 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
1400
1401 return 0;
1402}
1403
Dima Zavin214cc642009-01-26 11:16:21 -08001404int boot_linux_from_flash(void)
1405{
1406 struct boot_img_hdr *hdr = (void*) buf;
Dima Zavin214cc642009-01-26 11:16:21 -08001407 struct ptentry *ptn;
1408 struct ptable *ptable;
1409 unsigned offset = 0;
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001410
Shashank Mittalcd98d472011-08-02 14:29:24 -07001411 unsigned char *image_addr = 0;
1412 unsigned kernel_actual;
1413 unsigned ramdisk_actual;
1414 unsigned imagesize_actual;
vijay kumar8f53e362015-11-24 13:38:11 +05301415 unsigned second_actual = 0;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001416
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001417#if DEVICE_TREE
1418 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -07001419 struct dt_entry dt_entry;
vijay kumar8f53e362015-11-24 13:38:11 +05301420 unsigned dt_table_offset;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001421 uint32_t dt_actual;
Deepa Dinamani19648b42013-09-05 17:05:55 -07001422 uint32_t dt_hdr_size;
vijay kumar8f53e362015-11-24 13:38:11 +05301423 unsigned int dtb_size = 0;
1424 unsigned char *best_match_dt_addr = NULL;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001425#endif
1426
David Ng183a7422009-12-07 14:55:21 -08001427 if (target_is_emmc_boot()) {
1428 hdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
1429 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1430 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
1431 return -1;
1432 }
1433 goto continue_boot;
1434 }
1435
Dima Zavin214cc642009-01-26 11:16:21 -08001436 ptable = flash_get_ptable();
1437 if (ptable == NULL) {
1438 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1439 return -1;
1440 }
1441
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001442 if(!boot_into_recovery)
1443 {
1444 ptn = ptable_find(ptable, "boot");
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001445
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001446 if (ptn == NULL) {
1447 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1448 return -1;
1449 }
1450 }
1451 else
1452 {
1453 ptn = ptable_find(ptable, "recovery");
1454 if (ptn == NULL) {
1455 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
1456 return -1;
1457 }
Dima Zavin214cc642009-01-26 11:16:21 -08001458 }
1459
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001460 if (flash_read(ptn, offset, buf, page_size)) {
Dima Zavin214cc642009-01-26 11:16:21 -08001461 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
1462 return -1;
1463 }
Dima Zavin214cc642009-01-26 11:16:21 -08001464
1465 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001466 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Dima Zavin214cc642009-01-26 11:16:21 -08001467 return -1;
1468 }
1469
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001470 if (hdr->page_size != page_size) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001471 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 -08001472 return -1;
1473 }
1474
vijay kumar287bb542015-09-29 13:01:52 +05301475 /* ensure commandline is terminated */
1476 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
1477
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001478 /*
1479 * Update the kernel/ramdisk/tags address if the boot image header
1480 * has default values, these default values come from mkbootimg when
1481 * the boot image is flashed using fastboot flash:raw
1482 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001483 update_ker_tags_rdisk_addr(hdr, false);
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001484
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001485 /* Get virtual addresses since the hdr saves physical addresses. */
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001486 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
1487 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
1488 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
1489
1490 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1491 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1492
1493 /* Check if the addresses in the header are valid. */
1494 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
1495 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
1496 {
1497 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
1498 return -1;
1499 }
1500
1501#ifndef DEVICE_TREE
1502 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1503 {
1504 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
1505 return -1;
1506 }
1507#endif
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001508
Shashank Mittalcd98d472011-08-02 14:29:24 -07001509 /* Authenticate Kernel */
Deepa Dinamani23b60d42013-06-24 18:10:52 -07001510 if(target_use_signed_kernel() && (!device.is_unlocked))
Shashank Mittalcd98d472011-08-02 14:29:24 -07001511 {
1512 image_addr = (unsigned char *)target_get_scratch_address();
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001513 offset = 0;
1514
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001515#if DEVICE_TREE
1516 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
vijay kumar966a9822015-12-09 18:36:09 +05301517
1518 if (UINT_MAX < ((uint64_t)kernel_actual + (uint64_t)ramdisk_actual+ (uint64_t)dt_actual + page_size)) {
1519 dprintf(CRITICAL, "Integer overflow detected in bootimage header fields\n");
1520 return -1;
1521 }
1522
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001523 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + dt_actual);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001524
1525 if (check_aboot_addr_range_overlap(hdr->tags_addr, hdr->dt_size))
1526 {
1527 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1528 return -1;
1529 }
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001530#else
vijay kumar966a9822015-12-09 18:36:09 +05301531 if (UINT_MAX < ((uint64_t)kernel_actual + (uint64_t)ramdisk_actual+ page_size)) {
1532 dprintf(CRITICAL, "Integer overflow detected in bootimage header fields\n");
1533 return -1;
1534 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07001535 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001536#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001537
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001538 dprintf(INFO, "Loading (%s) image (%d): start\n",
1539 (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001540 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -08001541
Shashank Mittalcd98d472011-08-02 14:29:24 -07001542 /* Read image without signature */
1543 if (flash_read(ptn, offset, (void *)image_addr, imagesize_actual))
1544 {
1545 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
1546 return -1;
1547 }
Dima Zavin214cc642009-01-26 11:16:21 -08001548
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001549 dprintf(INFO, "Loading (%s) image (%d): done\n",
1550 (!boot_into_recovery ? "boot" : "recovery"), imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001551 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Amol Jadib6be5c12012-11-14 13:39:51 -08001552
Shashank Mittalcd98d472011-08-02 14:29:24 -07001553 offset = imagesize_actual;
1554 /* Read signature */
1555 if (flash_read(ptn, offset, (void *)(image_addr + offset), page_size))
1556 {
1557 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001558 return -1;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001559 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07001560
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301561 verify_signed_bootimg((uint32_t)image_addr, imagesize_actual);
Shashank Mittalcd98d472011-08-02 14:29:24 -07001562
1563 /* Move kernel and ramdisk to correct address */
Matthew Qinbb7923d2015-02-09 10:56:09 +08001564 memmove((void*) hdr->kernel_addr, (char*) (image_addr + page_size), hdr->kernel_size);
1565 memmove((void*) hdr->ramdisk_addr, (char*) (image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001566#if DEVICE_TREE
vijay kumar8f53e362015-11-24 13:38:11 +05301567 if(hdr->dt_size != 0) {
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001568
vijay kumar8f53e362015-11-24 13:38:11 +05301569 dt_table_offset = ((uint32_t)image_addr + page_size + kernel_actual + ramdisk_actual + second_actual);
1570
1571 table = (struct dt_table*) dt_table_offset;
1572
1573 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0){
1574 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1575 return -1;
1576 }
1577
1578 /* Find index of device tree within device tree table */
1579 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
1580 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1581 return -1;
1582 }
1583
1584 /* Validate and Read device device tree in the "tags_add */
1585 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size)){
1586 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1587 return -1;
1588 }
1589
1590 best_match_dt_addr = (unsigned char *)table + dt_entry.offset;
1591 dtb_size = dt_entry.size;
1592 memmove((void *)hdr->tags_addr, (char *)best_match_dt_addr, dtb_size);
1593 }
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001594#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001595
1596 /* Make sure everything from scratch address is read before next step!*/
Shashank Mittala0032282011-08-26 14:50:11 -07001597 if(device.is_tampered)
Shashank Mittal162244e2011-08-08 19:01:25 -07001598 {
1599 write_device_info_flash(&device);
1600 }
Channagoud Kadabi5c86fe32012-02-16 10:58:48 +05301601#if USE_PCOM_SECBOOT
1602 set_tamper_flag(device.is_tampered);
1603#endif
Shashank Mittalcd98d472011-08-02 14:29:24 -07001604 }
1605 else
1606 {
Shashank Mittal162244e2011-08-08 19:01:25 -07001607 offset = page_size;
1608
Amol Jadib6be5c12012-11-14 13:39:51 -08001609 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1610 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1611 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
1612
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001613 dprintf(INFO, "Loading (%s) image (%d): start\n",
1614 (!boot_into_recovery ? "boot" : "recovery"), kernel_actual + ramdisk_actual);
1615
Amol Jadi492d5a52013-03-15 16:12:34 -07001616 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -08001617
1618 if (flash_read(ptn, offset, (void *)hdr->kernel_addr, kernel_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -07001619 dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
1620 return -1;
1621 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001622 offset += kernel_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001623
Amol Jadib6be5c12012-11-14 13:39:51 -08001624 if (flash_read(ptn, offset, (void *)hdr->ramdisk_addr, ramdisk_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -07001625 dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
1626 return -1;
1627 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001628 offset += ramdisk_actual;
1629
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001630 dprintf(INFO, "Loading (%s) image (%d): done\n",
1631 (!boot_into_recovery ? "boot" : "recovery"), kernel_actual + ramdisk_actual);
1632
Amol Jadi492d5a52013-03-15 16:12:34 -07001633 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001634
1635 if(hdr->second_size != 0) {
Amol Jadib6be5c12012-11-14 13:39:51 -08001636 offset += second_actual;
1637 /* Second image loading not implemented. */
1638 ASSERT(0);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001639 }
1640
1641#if DEVICE_TREE
1642 if(hdr->dt_size != 0) {
1643
1644 /* Read the device tree table into buffer */
1645 if(flash_read(ptn, offset, (void *) dt_buf, page_size)) {
1646 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
1647 return -1;
1648 }
1649
1650 table = (struct dt_table*) dt_buf;
1651
Deepa Dinamani19648b42013-09-05 17:05:55 -07001652 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001653 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1654 return -1;
1655 }
1656
P.V. Phani Kumar82916762016-03-09 09:20:19 +05301657 /* Its Error if, dt_hdr_size (table->num_entries * dt_entry size + Dev_Tree Header)
1658 goes beyound hdr->dt_size*/
1659 if (dt_hdr_size > ROUND_TO_PAGE(hdr->dt_size,hdr->page_size)) {
1660 dprintf(CRITICAL, "ERROR: Invalid Device Tree size \n");
1661 return -1;
1662 }
1663
Deepa Dinamani19648b42013-09-05 17:05:55 -07001664 table = (struct dt_table*) memalign(CACHE_LINE, dt_hdr_size);
1665 if (!table)
1666 return -1;
1667
1668 /* Read the entire device tree table into buffer */
1669 if(flash_read(ptn, offset, (void *)table, dt_hdr_size)) {
1670 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
1671 return -1;
1672 }
1673
1674
Joel Kingaa335dc2013-06-03 16:11:08 -07001675 /* Find index of device tree within device tree table */
1676 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001677 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1678 return -1;
1679 }
1680
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001681 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001682 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001683 {
1684 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1685 return -1;
1686 }
1687
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001688 /* Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001689 if(flash_read(ptn, offset + dt_entry.offset,
1690 (void *)hdr->tags_addr, dt_entry.size)) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001691 dprintf(CRITICAL, "ERROR: Cannot read device tree\n");
1692 return -1;
1693 }
1694 }
1695#endif
1696
Shashank Mittalcd98d472011-08-02 14:29:24 -07001697 }
David Ng183a7422009-12-07 14:55:21 -08001698continue_boot:
Dima Zavin214cc642009-01-26 11:16:21 -08001699
Dima Zavin214cc642009-01-26 11:16:21 -08001700 /* TODO: create/pass atags to kernel */
1701
Ajay Dudanie28a6072011-07-01 13:59:46 -07001702 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07001703 (const char *)hdr->cmdline, board_machtype(),
Dima Zavin214cc642009-01-26 11:16:21 -08001704 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
1705
1706 return 0;
1707}
Brian Swetland9c4c0752009-01-25 16:23:50 -08001708
Shashank Mittal162244e2011-08-08 19:01:25 -07001709void write_device_info_mmc(device_info *dev)
1710{
Shashank Mittal162244e2011-08-08 19:01:25 -07001711 unsigned long long ptn = 0;
1712 unsigned long long size;
1713 int index = INVALID_PTN;
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001714 uint32_t blocksize;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001715 uint8_t lun = 0;
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001716 uint32_t ret = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001717
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001718 if (devinfo_present)
1719 index = partition_get_index("devinfo");
1720 else
1721 index = partition_get_index("aboot");
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001722
Shashank Mittal162244e2011-08-08 19:01:25 -07001723 ptn = partition_get_offset(index);
1724 if(ptn == 0)
1725 {
1726 return;
1727 }
1728
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001729 lun = partition_get_lun(index);
1730 mmc_set_lun(lun);
1731
Shashank Mittal162244e2011-08-08 19:01:25 -07001732 size = partition_get_size(index);
1733
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001734 blocksize = mmc_get_device_blocksize();
1735
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001736 if (devinfo_present)
1737 ret = mmc_write(ptn, blocksize, (void *)info_buf);
1738 else
1739 ret = mmc_write((ptn + size - blocksize), blocksize, (void *)info_buf);
1740 if (ret)
Shashank Mittal162244e2011-08-08 19:01:25 -07001741 {
1742 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
Sridhar Parasurame1a97922015-07-27 15:31:22 -07001743 ASSERT(0);
Shashank Mittal162244e2011-08-08 19:01:25 -07001744 }
1745}
1746
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001747void read_device_info_mmc(struct device_info *info)
Shashank Mittal162244e2011-08-08 19:01:25 -07001748{
Shashank Mittal162244e2011-08-08 19:01:25 -07001749 unsigned long long ptn = 0;
1750 unsigned long long size;
1751 int index = INVALID_PTN;
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001752 uint32_t blocksize;
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001753 uint32_t ret = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001754
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001755 if ((index = partition_get_index("devinfo")) < 0)
1756 {
1757 devinfo_present = false;
1758 index = partition_get_index("aboot");
1759 }
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001760
Shashank Mittal162244e2011-08-08 19:01:25 -07001761 ptn = partition_get_offset(index);
1762 if(ptn == 0)
1763 {
1764 return;
1765 }
1766
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001767 mmc_set_lun(partition_get_lun(index));
1768
Shashank Mittal162244e2011-08-08 19:01:25 -07001769 size = partition_get_size(index);
1770
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001771 blocksize = mmc_get_device_blocksize();
1772
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001773 if (devinfo_present)
1774 ret = mmc_read(ptn, (void *)info_buf, blocksize);
1775 else
1776 ret = mmc_read((ptn + size - blocksize), (void *)info_buf, blocksize);
1777 if (ret)
Shashank Mittal162244e2011-08-08 19:01:25 -07001778 {
1779 dprintf(CRITICAL, "ERROR: Cannot read device info\n");
Sridhar Parasurame1a97922015-07-27 15:31:22 -07001780 ASSERT(0);
Shashank Mittal162244e2011-08-08 19:01:25 -07001781 }
Shashank Mittal162244e2011-08-08 19:01:25 -07001782}
1783
1784void write_device_info_flash(device_info *dev)
1785{
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001786 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
Shashank Mittal162244e2011-08-08 19:01:25 -07001787 struct ptentry *ptn;
1788 struct ptable *ptable;
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001789 if(info == NULL)
1790 {
1791 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
1792 ASSERT(0);
1793 }
1794 info_buf = info;
Shashank Mittal162244e2011-08-08 19:01:25 -07001795 ptable = flash_get_ptable();
1796 if (ptable == NULL)
1797 {
1798 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1799 return;
1800 }
1801
1802 ptn = ptable_find(ptable, "devinfo");
1803 if (ptn == NULL)
1804 {
Smita Ghosh670c8b82015-05-07 09:30:03 -07001805 dprintf(CRITICAL, "ERROR: No devinfo partition found\n");
Shashank Mittal162244e2011-08-08 19:01:25 -07001806 return;
1807 }
1808
1809 memcpy(info, dev, sizeof(device_info));
1810
1811 if (flash_write(ptn, 0, (void *)info_buf, page_size))
1812 {
1813 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1814 return;
1815 }
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001816 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07001817}
1818
vijay kumarc65876c2015-04-24 13:29:16 +05301819static int read_allow_oem_unlock(device_info *dev)
1820{
vijay kumarc65876c2015-04-24 13:29:16 +05301821 unsigned offset;
1822 int index;
1823 unsigned long long ptn;
1824 unsigned long long ptn_size;
1825 unsigned blocksize = mmc_get_device_blocksize();
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07001826 STACKBUF_DMA_ALIGN(buf, blocksize);
vijay kumarc65876c2015-04-24 13:29:16 +05301827
vijay kumarca2e6812015-07-08 20:28:25 +05301828 index = partition_get_index(frp_ptns[0]);
vijay kumarc65876c2015-04-24 13:29:16 +05301829 if (index == INVALID_PTN)
1830 {
vijay kumarca2e6812015-07-08 20:28:25 +05301831 index = partition_get_index(frp_ptns[1]);
1832 if (index == INVALID_PTN)
1833 {
1834 dprintf(CRITICAL, "Neither '%s' nor '%s' partition found\n", frp_ptns[0],frp_ptns[1]);
1835 return -1;
1836 }
vijay kumarc65876c2015-04-24 13:29:16 +05301837 }
1838
1839 ptn = partition_get_offset(index);
1840 ptn_size = partition_get_size(index);
1841 offset = ptn_size - blocksize;
1842
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07001843 if (mmc_read(ptn + offset, (void *)buf, blocksize))
vijay kumarc65876c2015-04-24 13:29:16 +05301844 {
1845 dprintf(CRITICAL, "Reading MMC failed\n");
1846 return -1;
1847 }
1848
1849 /*is_allow_unlock is a bool value stored at the LSB of last byte*/
1850 is_allow_unlock = buf[blocksize-1] & 0x01;
1851 return 0;
1852}
1853
1854static int write_allow_oem_unlock(bool allow_unlock)
1855{
vijay kumarc65876c2015-04-24 13:29:16 +05301856 unsigned offset;
vijay kumarc65876c2015-04-24 13:29:16 +05301857 int index;
1858 unsigned long long ptn;
1859 unsigned long long ptn_size;
1860 unsigned blocksize = mmc_get_device_blocksize();
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07001861 STACKBUF_DMA_ALIGN(buf, blocksize);
vijay kumarc65876c2015-04-24 13:29:16 +05301862
vijay kumarca2e6812015-07-08 20:28:25 +05301863 index = partition_get_index(frp_ptns[0]);
vijay kumarc65876c2015-04-24 13:29:16 +05301864 if (index == INVALID_PTN)
1865 {
vijay kumarca2e6812015-07-08 20:28:25 +05301866 index = partition_get_index(frp_ptns[1]);
1867 if (index == INVALID_PTN)
1868 {
1869 dprintf(CRITICAL, "Neither '%s' nor '%s' partition found\n", frp_ptns[0],frp_ptns[1]);
1870 return -1;
1871 }
vijay kumarc65876c2015-04-24 13:29:16 +05301872 }
1873
1874 ptn = partition_get_offset(index);
1875 ptn_size = partition_get_size(index);
1876 offset = ptn_size - blocksize;
1877
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07001878 if (mmc_read(ptn + offset, (void *)buf, blocksize))
vijay kumarc65876c2015-04-24 13:29:16 +05301879 {
1880 dprintf(CRITICAL, "Reading MMC failed\n");
1881 return -1;
1882 }
1883
1884 /*is_allow_unlock is a bool value stored at the LSB of last byte*/
1885 buf[blocksize-1] = allow_unlock;
1886 if (mmc_write(ptn + offset, blocksize, buf))
1887 {
1888 dprintf(CRITICAL, "Writing MMC failed\n");
1889 return -1;
1890 }
1891
1892 return 0;
1893}
1894
Shashank Mittal162244e2011-08-08 19:01:25 -07001895void read_device_info_flash(device_info *dev)
1896{
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001897 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
Shashank Mittal162244e2011-08-08 19:01:25 -07001898 struct ptentry *ptn;
1899 struct ptable *ptable;
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001900 if(info == NULL)
1901 {
1902 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
1903 ASSERT(0);
1904 }
1905 info_buf = info;
Shashank Mittal162244e2011-08-08 19:01:25 -07001906 ptable = flash_get_ptable();
1907 if (ptable == NULL)
1908 {
1909 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1910 return;
1911 }
1912
1913 ptn = ptable_find(ptable, "devinfo");
1914 if (ptn == NULL)
1915 {
Smita Ghosh670c8b82015-05-07 09:30:03 -07001916 dprintf(CRITICAL, "ERROR: No devinfo partition found\n");
Shashank Mittal162244e2011-08-08 19:01:25 -07001917 return;
1918 }
1919
1920 if (flash_read(ptn, 0, (void *)info_buf, page_size))
1921 {
1922 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1923 return;
1924 }
1925
1926 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
1927 {
Shashank Mittal162244e2011-08-08 19:01:25 -07001928 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
1929 info->is_unlocked = 0;
Shashank Mittala0032282011-08-26 14:50:11 -07001930 info->is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001931 write_device_info_flash(info);
1932 }
1933 memcpy(dev, info, sizeof(device_info));
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001934 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07001935}
1936
1937void write_device_info(device_info *dev)
1938{
1939 if(target_is_emmc_boot())
1940 {
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001941 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
1942 if(info == NULL)
1943 {
1944 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
1945 ASSERT(0);
1946 }
1947 info_buf = info;
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001948 memcpy(info, dev, sizeof(struct device_info));
1949
1950#if USE_RPMB_FOR_DEVINFO
Sridhar Parasurame1a97922015-07-27 15:31:22 -07001951 if (is_secure_boot_enable()) {
1952 if((write_device_info_rpmb((void*) info, PAGE_SIZE)) < 0)
1953 ASSERT(0);
1954 }
Channagoud Kadabic80cb492015-04-28 16:08:28 -07001955 else
1956 write_device_info_mmc(info);
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001957#else
1958 write_device_info_mmc(info);
1959#endif
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001960 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07001961 }
1962 else
1963 {
1964 write_device_info_flash(dev);
1965 }
1966}
1967
1968void read_device_info(device_info *dev)
1969{
1970 if(target_is_emmc_boot())
1971 {
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001972 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
1973 if(info == NULL)
1974 {
1975 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
1976 ASSERT(0);
1977 }
1978 info_buf = info;
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001979
1980#if USE_RPMB_FOR_DEVINFO
Sridhar Parasurame1a97922015-07-27 15:31:22 -07001981 if (is_secure_boot_enable()) {
1982 if((read_device_info_rpmb((void*) info, PAGE_SIZE)) < 0)
1983 ASSERT(0);
1984 }
Channagoud Kadabic80cb492015-04-28 16:08:28 -07001985 else
1986 read_device_info_mmc(info);
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001987#else
1988 read_device_info_mmc(info);
1989#endif
1990
1991 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
1992 {
1993 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
lijuang511a2b52015-08-14 20:50:51 +08001994 if (is_secure_boot_enable()) {
Channagoud Kadabi05f78ba2015-07-06 11:58:14 -07001995 info->is_unlocked = 0;
Parth Dixitddbc7352015-10-18 03:13:31 +05301996#if !VBOOT_MOTA
lijuang511a2b52015-08-14 20:50:51 +08001997 info->is_unlock_critical = 0;
Parth Dixitddbc7352015-10-18 03:13:31 +05301998#endif
lijuang511a2b52015-08-14 20:50:51 +08001999 } else {
Channagoud Kadabi2fda4092015-07-07 13:34:11 -07002000 info->is_unlocked = 1;
Parth Dixitddbc7352015-10-18 03:13:31 +05302001#if !VBOOT_MOTA
lijuang511a2b52015-08-14 20:50:51 +08002002 info->is_unlock_critical = 1;
Parth Dixitddbc7352015-10-18 03:13:31 +05302003#endif
lijuang511a2b52015-08-14 20:50:51 +08002004 }
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002005 info->is_tampered = 0;
2006 info->charger_screen_enabled = 0;
Parth Dixitddbc7352015-10-18 03:13:31 +05302007#if !VBOOT_MOTA
Sridhar Parasuram287e5062015-08-24 16:17:22 -07002008 info->verity_mode = 1; //enforcing by default
Parth Dixitddbc7352015-10-18 03:13:31 +05302009#endif
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002010 write_device_info(info);
2011 }
2012 memcpy(dev, info, sizeof(device_info));
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002013 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07002014 }
2015 else
2016 {
2017 read_device_info_flash(dev);
2018 }
2019}
2020
2021void reset_device_info()
2022{
2023 dprintf(ALWAYS, "reset_device_info called.");
Shashank Mittala0032282011-08-26 14:50:11 -07002024 device.is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07002025 write_device_info(&device);
2026}
2027
2028void set_device_root()
2029{
2030 dprintf(ALWAYS, "set_device_root called.");
Shashank Mittala0032282011-08-26 14:50:11 -07002031 device.is_tampered = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -07002032 write_device_info(&device);
2033}
2034
lijuang4ece1e72015-08-14 21:02:36 +08002035/* set device unlock value
2036 * Must check FRP before call this function
2037 * Need to wipe data when unlock status changed
2038 * type 0: oem unlock
2039 * type 1: unlock critical
2040 * status 0: unlock as false
2041 * status 1: lock as true
2042 */
2043void set_device_unlock_value(int type, bool status)
lijuang21f12f52015-08-22 16:22:19 +08002044{
lijuang4ece1e72015-08-14 21:02:36 +08002045 if (type == UNLOCK)
2046 device.is_unlocked = status;
Parth Dixitddbc7352015-10-18 03:13:31 +05302047#if !VBOOT_MOTA
lijuang4ece1e72015-08-14 21:02:36 +08002048 else if (type == UNLOCK_CRITICAL)
2049 device.is_unlock_critical = status;
Parth Dixitddbc7352015-10-18 03:13:31 +05302050#endif
lijuang4ece1e72015-08-14 21:02:36 +08002051 write_device_info(&device);
2052}
2053
2054static void set_device_unlock(int type, bool status)
2055{
2056 int is_unlocked = -1;
2057 char response[MAX_RSP_SIZE];
2058
2059 /* check device unlock status if it is as expected */
2060 if (type == UNLOCK)
2061 is_unlocked = device.is_unlocked;
Parth Dixitddbc7352015-10-18 03:13:31 +05302062#if !VBOOT_MOTA
lijuang4ece1e72015-08-14 21:02:36 +08002063 else if (type == UNLOCK_CRITICAL)
2064 is_unlocked = device.is_unlock_critical;
Parth Dixitddbc7352015-10-18 03:13:31 +05302065#endif
lijuang4ece1e72015-08-14 21:02:36 +08002066 if (is_unlocked == status) {
2067 snprintf(response, sizeof(response), "\tDevice already : %s", (status ? "unlocked!" : "locked!"));
2068 fastboot_info(response);
2069 fastboot_okay("");
2070 return;
2071 }
2072
2073 /* status is true, it means to unlock device */
2074 if (status) {
lijuang21f12f52015-08-22 16:22:19 +08002075 if(!is_allow_unlock) {
2076 fastboot_fail("oem unlock is not allowed");
2077 return;
2078 }
2079
lijuang4ece1e72015-08-14 21:02:36 +08002080#if FBCON_DISPLAY_MSG
lijuang9a7d3b92015-11-30 14:41:24 +08002081 display_unlock_menu(type);
lijuang4ece1e72015-08-14 21:02:36 +08002082 fastboot_okay("");
2083 return;
2084#else
2085 if (type == UNLOCK) {
2086 fastboot_fail("Need wipe userdata. Do 'fastboot oem unlock-go'");
2087 return;
2088 }
2089#endif
lijuang21f12f52015-08-22 16:22:19 +08002090 }
lijuang4ece1e72015-08-14 21:02:36 +08002091
2092 set_device_unlock_value(type, status);
2093
2094 /* wipe data */
2095 struct recovery_message msg;
2096
2097 snprintf(msg.recovery, sizeof(msg.recovery), "recovery\n--wipe_data");
2098 write_misc(0, &msg, sizeof(msg));
2099
2100 fastboot_okay("");
2101 reboot_device(RECOVERY_MODE);
lijuang21f12f52015-08-22 16:22:19 +08002102}
2103
lijuang511a2b52015-08-14 20:50:51 +08002104static bool critical_flash_allowed(const char * entry)
2105{
2106 uint32_t i = 0;
2107 if (entry == NULL)
2108 return false;
2109
2110 for (i = 0; i < ARRAY_SIZE(critical_flash_allowed_ptn); i++) {
2111 if(!strcmp(entry, critical_flash_allowed_ptn[i]))
2112 return true;
2113 }
2114 return false;
Matthew Qin271927e2015-03-31 22:07:07 -04002115}
2116
2117#if DEVICE_TREE
Amol Jadicb524072012-08-09 16:40:18 -07002118int copy_dtb(uint8_t *boot_image_start, unsigned int scratch_offset)
2119{
2120 uint32 dt_image_offset = 0;
Amol Jadicb524072012-08-09 16:40:18 -07002121 uint32_t n;
Amol Jadicb524072012-08-09 16:40:18 -07002122 struct dt_table *table;
2123 struct dt_entry dt_entry;
2124 uint32_t dt_hdr_size;
2125 unsigned int compressed_size = 0;
2126 unsigned int dtb_size = 0;
2127 unsigned int out_avai_len = 0;
2128 unsigned char *out_addr = NULL;
2129 unsigned char *best_match_dt_addr = NULL;
2130 int rc;
2131
2132 struct boot_img_hdr *hdr = (struct boot_img_hdr *) (boot_image_start);
2133
2134 if(hdr->dt_size != 0) {
2135 /* add kernel offset */
2136 dt_image_offset += page_size;
2137 n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07002138 dt_image_offset += n;
Amol Jadicb524072012-08-09 16:40:18 -07002139
Deepa Dinamani19648b42013-09-05 17:05:55 -07002140 /* add ramdisk offset */
Amol Jadicb524072012-08-09 16:40:18 -07002141 n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
2142 dt_image_offset += n;
2143
Joel Kingaa335dc2013-06-03 16:11:08 -07002144 /* add second offset */
2145 if(hdr->second_size != 0) {
Amol Jadicb524072012-08-09 16:40:18 -07002146 n = ROUND_TO_PAGE(hdr->second_size, page_mask);
2147 dt_image_offset += n;
2148 }
2149
Matthew Qin271927e2015-03-31 22:07:07 -04002150 /* offset now point to start of dt.img */
2151 table = (struct dt_table*)(boot_image_start + dt_image_offset);
2152
2153 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
2154 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
Matthew Qin0b15b322015-05-19 05:20:54 -04002155 return -1;
Matthew Qin271927e2015-03-31 22:07:07 -04002156 }
P.V. Phani Kumar82916762016-03-09 09:20:19 +05302157
2158 /* Its Error if, dt_hdr_size (table->num_entries * dt_entry size + Dev_Tree Header)
2159 goes beyound hdr->dt_size*/
2160 if (dt_hdr_size > ROUND_TO_PAGE(hdr->dt_size,hdr->page_size)) {
2161 dprintf(CRITICAL, "ERROR: Invalid Device Tree size \n");
2162 return -1;
2163 }
2164
Amol Jadicb524072012-08-09 16:40:18 -07002165 /* Find index of device tree within device tree table */
2166 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
2167 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
2168 return -1;
2169 }
2170
Matthew Qin271927e2015-03-31 22:07:07 -04002171 best_match_dt_addr = (unsigned char *)boot_image_start + dt_image_offset + dt_entry.offset;
2172 if (is_gzip_package(best_match_dt_addr, dt_entry.size))
2173 {
2174 out_addr = (unsigned char *)target_get_scratch_address() + scratch_offset;
2175 out_avai_len = target_get_max_flash_size() - scratch_offset;
2176 dprintf(INFO, "decompressing dtb: start\n");
2177 rc = decompress(best_match_dt_addr,
2178 dt_entry.size, out_addr, out_avai_len,
2179 &compressed_size, &dtb_size);
2180 if (rc)
2181 {
Matthew Qin0b15b322015-05-19 05:20:54 -04002182 dprintf(CRITICAL, "decompressing dtb failed!!!\n");
Matthew Qin271927e2015-03-31 22:07:07 -04002183 ASSERT(0);
2184 }
2185
Matthew Qin0b15b322015-05-19 05:20:54 -04002186 dprintf(INFO, "decompressing dtb: done\n");
Matthew Qin271927e2015-03-31 22:07:07 -04002187 best_match_dt_addr = out_addr;
2188 } else {
2189 dtb_size = dt_entry.size;
2190 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002191 /* Validate and Read device device tree in the "tags_add */
Matthew Qin271927e2015-03-31 22:07:07 -04002192 if (check_aboot_addr_range_overlap(hdr->tags_addr, dtb_size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002193 {
2194 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
2195 return -1;
2196 }
2197
Amol Jadicb524072012-08-09 16:40:18 -07002198 /* Read device device tree in the "tags_add */
Matthew Qin271927e2015-03-31 22:07:07 -04002199 memmove((void*) hdr->tags_addr, (void *)best_match_dt_addr, dtb_size);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002200 } else
2201 return -1;
Amol Jadicb524072012-08-09 16:40:18 -07002202
2203 /* Everything looks fine. Return success. */
2204 return 0;
2205}
2206#endif
2207
Brian Swetland9c4c0752009-01-25 16:23:50 -08002208void cmd_boot(const char *arg, void *data, unsigned sz)
2209{
Amit Blay8a510302015-08-17 09:20:01 +03002210#ifdef MDTP_SUPPORT
2211 static bool is_mdtp_activated = 0;
2212#endif /* MDTP_SUPPORT */
Brian Swetland9c4c0752009-01-25 16:23:50 -08002213 unsigned kernel_actual;
2214 unsigned ramdisk_actual;
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002215 uint32_t image_actual;
2216 uint32_t dt_actual = 0;
P.V. Phani Kumareecfd822016-03-09 20:09:03 +05302217 uint32_t sig_actual = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08002218 struct boot_img_hdr *hdr = NULL;
2219 struct kernel64_hdr *kptr = NULL;
Brian Swetland9c4c0752009-01-25 16:23:50 -08002220 char *ptr = ((char*) data);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002221 int ret = 0;
2222 uint8_t dtb_copied = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08002223 unsigned int out_len = 0;
2224 unsigned int out_avai_len = 0;
2225 unsigned char *out_addr = NULL;
2226 uint32_t dtb_offset = 0;
2227 unsigned char *kernel_start_addr = NULL;
2228 unsigned int kernel_size = 0;
Matthew Qin271927e2015-03-31 22:07:07 -04002229 unsigned int scratch_offset = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08002230
lijuang2008ff22016-03-07 17:56:27 +08002231#if FBCON_DISPLAY_MSG
2232 /* Exit keys' detection thread firstly */
2233 exit_menu_keys_detection();
2234#endif
2235
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002236#if VERIFIED_BOOT
Channagoud Kadabi6d5375e2015-06-23 17:15:42 -07002237 if(target_build_variant_user() && !device.is_unlocked)
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002238 {
2239 fastboot_fail("unlock device to use this command");
lijuang2008ff22016-03-07 17:56:27 +08002240 goto boot_failed;
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002241 }
2242#endif
2243
Brian Swetland9c4c0752009-01-25 16:23:50 -08002244 if (sz < sizeof(hdr)) {
2245 fastboot_fail("invalid bootimage header");
lijuang2008ff22016-03-07 17:56:27 +08002246 goto boot_failed;
Brian Swetland9c4c0752009-01-25 16:23:50 -08002247 }
2248
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002249 hdr = (struct boot_img_hdr *)data;
Brian Swetland9c4c0752009-01-25 16:23:50 -08002250
2251 /* ensure commandline is terminated */
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002252 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -08002253
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002254 if(target_is_emmc_boot() && hdr->page_size) {
2255 page_size = hdr->page_size;
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07002256 page_mask = page_size - 1;
2257 }
2258
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002259 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
2260 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002261#if DEVICE_TREE
2262 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
2263#endif
2264
2265 image_actual = ADD_OF(page_size, kernel_actual);
2266 image_actual = ADD_OF(image_actual, ramdisk_actual);
2267 image_actual = ADD_OF(image_actual, dt_actual);
2268
P.V. Phani Kumareecfd822016-03-09 20:09:03 +05302269 if (target_use_signed_kernel() && (!device.is_unlocked)) {
2270 /* Calculate the signature length from boot image */
2271 sig_actual = read_der_message_length(
2272 (unsigned char*)(data + image_actual),sz);
Channagoud Kadabi3d839012014-06-26 14:26:39 -07002273 image_actual = ADD_OF(image_actual, sig_actual);
P.V. Phani Kumareecfd822016-03-09 20:09:03 +05302274 }
Channagoud Kadabi3d839012014-06-26 14:26:39 -07002275
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002276 /* sz should have atleast raw boot image */
2277 if (image_actual > sz) {
Channagoud Kadabi3d839012014-06-26 14:26:39 -07002278 fastboot_fail("bootimage: incomplete or not signed");
lijuang2008ff22016-03-07 17:56:27 +08002279 goto boot_failed;
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002280 }
2281
Sridhar Parasuram361e4cd2015-09-24 11:19:19 -07002282 // Initialize boot state before trying to verify boot.img
2283#if VERIFIED_BOOT
Channagoud Kadabi699466e2015-11-03 12:37:42 -08002284 boot_verifier_init();
Channagoud Kadabibf6ce7d2015-09-17 13:25:35 -07002285 /* Handle overflow if the input image size is greater than
2286 * boot image buffer can hold
2287 */
2288 if ((target_get_max_flash_size() - (image_actual - sig_actual)) < page_size)
2289 {
2290 fastboot_fail("booimage: size is greater than boot image buffer can hold");
lijuang2008ff22016-03-07 17:56:27 +08002291 goto boot_failed;
Channagoud Kadabibf6ce7d2015-09-17 13:25:35 -07002292 }
Sridhar Parasuram361e4cd2015-09-24 11:19:19 -07002293#endif
2294
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002295 /* Verify the boot image
2296 * device & page_size are initialized in aboot_init
2297 */
Sridhar Parasuram7ae9fbc2015-09-17 09:56:30 -07002298 if (target_use_signed_kernel() && (!device.is_unlocked)) {
Channagoud Kadabi3d839012014-06-26 14:26:39 -07002299 /* Pass size excluding signature size, otherwise we would try to
2300 * access signature beyond its length
2301 */
2302 verify_signed_bootimg((uint32_t)data, (image_actual - sig_actual));
Sridhar Parasuram7ae9fbc2015-09-17 09:56:30 -07002303 }
Amit Blay4aa292f2015-04-28 21:55:59 +03002304#ifdef MDTP_SUPPORT
2305 else
2306 {
Shay Nachmani062aa3f2015-05-17 17:28:44 +03002307 /* fastboot boot is not allowed when MDTP is activated */
Amit Blay4aa292f2015-04-28 21:55:59 +03002308 mdtp_ext_partition_verification_t ext_partition;
Amit Blay8a510302015-08-17 09:20:01 +03002309
2310 if (!is_mdtp_activated) {
2311 ext_partition.partition = MDTP_PARTITION_NONE;
2312 mdtp_fwlock_verify_lock(&ext_partition);
2313 }
Amit Blay4aa292f2015-04-28 21:55:59 +03002314 }
Shay Nachmani062aa3f2015-05-17 17:28:44 +03002315
Amir Kotzer7c768c02016-04-13 09:08:05 +03002316 /* If mdtp state cannot be validate, block fastboot boot*/
2317 if(mdtp_activated(&is_mdtp_activated)){
2318 dprintf(CRITICAL, "mdtp_activated cannot validate state.\n");
2319 dprintf(CRITICAL, "Can not proceed with fastboot boot command.\n");
2320 goto boot_failed;
2321 }
Shay Nachmani062aa3f2015-05-17 17:28:44 +03002322 if(is_mdtp_activated){
2323 dprintf(CRITICAL, "fastboot boot command is not available.\n");
lijuang2008ff22016-03-07 17:56:27 +08002324 goto boot_failed;
Shay Nachmani062aa3f2015-05-17 17:28:44 +03002325 }
Amit Blay4aa292f2015-04-28 21:55:59 +03002326#endif /* MDTP_SUPPORT */
2327
Sridhar Parasuram361e4cd2015-09-24 11:19:19 -07002328#if VERIFIED_BOOT
Parth Dixitbc9b6492015-10-18 00:41:38 +05302329#if !VBOOT_MOTA
Sridhar Parasuram361e4cd2015-09-24 11:19:19 -07002330 // send root of trust
2331 if(!send_rot_command((uint32_t)device.is_unlocked))
2332 ASSERT(0);
2333#endif
Parth Dixitbc9b6492015-10-18 00:41:38 +05302334#endif
Channagoud Kadabia22144f2013-03-20 11:49:01 -07002335 /*
Matthew Qinbb7923d2015-02-09 10:56:09 +08002336 * Check if the kernel image is a gzip package. If yes, need to decompress it.
2337 * If not, continue booting.
2338 */
2339 if (is_gzip_package((unsigned char *)(data + page_size), hdr->kernel_size))
2340 {
2341 out_addr = (unsigned char *)target_get_scratch_address();
2342 out_addr = (unsigned char *)(out_addr + image_actual + page_size);
2343 out_avai_len = target_get_max_flash_size() - image_actual - page_size;
Matthew Qin0b15b322015-05-19 05:20:54 -04002344 dprintf(INFO, "decompressing kernel image: start\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08002345 ret = decompress((unsigned char *)(ptr + page_size),
2346 hdr->kernel_size, out_addr, out_avai_len,
2347 &dtb_offset, &out_len);
2348 if (ret)
2349 {
Matthew Qin0b15b322015-05-19 05:20:54 -04002350 dprintf(CRITICAL, "decompressing image failed!!!\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08002351 ASSERT(0);
2352 }
2353
Matthew Qin0b15b322015-05-19 05:20:54 -04002354 dprintf(INFO, "decompressing kernel image: done\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08002355 kptr = (struct kernel64_hdr *)out_addr;
2356 kernel_start_addr = out_addr;
2357 kernel_size = out_len;
2358 } else {
2359 kptr = (struct kernel64_hdr*)((char *)data + page_size);
2360 kernel_start_addr = (unsigned char *)((char *)data + page_size);
2361 kernel_size = hdr->kernel_size;
2362 }
2363
2364 /*
Channagoud Kadabia22144f2013-03-20 11:49:01 -07002365 * Update the kernel/ramdisk/tags address if the boot image header
2366 * has default values, these default values come from mkbootimg when
2367 * the boot image is flashed using fastboot flash:raw
2368 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08002369 update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr));
Dima Zavin3cadfff2013-03-21 14:30:48 -07002370
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002371 /* Get virtual addresses since the hdr saves physical addresses. */
2372 hdr->kernel_addr = VA(hdr->kernel_addr);
2373 hdr->ramdisk_addr = VA(hdr->ramdisk_addr);
2374 hdr->tags_addr = VA(hdr->tags_addr);
Brian Swetland9c4c0752009-01-25 16:23:50 -08002375
Matthew Qinbb7923d2015-02-09 10:56:09 +08002376 kernel_size = ROUND_TO_PAGE(kernel_size, page_mask);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002377 /* Check if the addresses in the header are valid. */
Matthew Qinbb7923d2015-02-09 10:56:09 +08002378 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_size) ||
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002379 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
2380 {
2381 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
lijuang2008ff22016-03-07 17:56:27 +08002382 goto boot_failed;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002383 }
2384
Amol Jadicb524072012-08-09 16:40:18 -07002385#if DEVICE_TREE
Matthew Qin271927e2015-03-31 22:07:07 -04002386 scratch_offset = image_actual + page_size + out_len;
Amol Jadicb524072012-08-09 16:40:18 -07002387 /* find correct dtb and copy it to right location */
Matthew Qin271927e2015-03-31 22:07:07 -04002388 ret = copy_dtb(data, scratch_offset);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002389
2390 dtb_copied = !ret ? 1 : 0;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002391#else
2392 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
2393 {
2394 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
lijuang2008ff22016-03-07 17:56:27 +08002395 goto boot_failed;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002396 }
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002397#endif
2398
2399 /* Load ramdisk & kernel */
2400 memmove((void*) hdr->ramdisk_addr, ptr + page_size + kernel_actual, hdr->ramdisk_size);
Matthew Qinbb7923d2015-02-09 10:56:09 +08002401 memmove((void*) hdr->kernel_addr, (char*) (kernel_start_addr), kernel_size);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002402
2403#if DEVICE_TREE
Matthew Qinbb7923d2015-02-09 10:56:09 +08002404 if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual))
2405 {
2406 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
lijuang2008ff22016-03-07 17:56:27 +08002407 goto boot_failed;
Matthew Qinbb7923d2015-02-09 10:56:09 +08002408 }
2409
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002410 /*
2411 * If dtb is not found look for appended DTB in the kernel.
2412 * If appended dev tree is found, update the atags with
2413 * memory address to the DTB appended location on RAM.
2414 * Else update with the atags address in the kernel header
2415 */
2416 if (!dtb_copied) {
2417 void *dtb;
Matthew Qinbb7923d2015-02-09 10:56:09 +08002418 dtb = dev_tree_appended((void*)(ptr + page_size),
2419 hdr->kernel_size, dtb_offset,
Dima Zavine63e5572013-05-03 12:23:06 -07002420 (void *)hdr->tags_addr);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002421 if (!dtb) {
2422 fastboot_fail("dtb not found");
lijuang2008ff22016-03-07 17:56:27 +08002423 goto boot_failed;
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002424 }
Amol Jadicb524072012-08-09 16:40:18 -07002425 }
2426#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08002427
2428 fastboot_okay("");
Amol Jadi7c4316c2013-10-07 14:19:26 -07002429 fastboot_stop();
Brian Swetland9c4c0752009-01-25 16:23:50 -08002430
Dima Zavin77e41f32013-03-06 16:10:43 -08002431 boot_linux((void*) hdr->kernel_addr, (void*) hdr->tags_addr,
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002432 (const char*) hdr->cmdline, board_machtype(),
2433 (void*) hdr->ramdisk_addr, hdr->ramdisk_size);
lijuang2008ff22016-03-07 17:56:27 +08002434
2435 /* fastboot already stop, it's no need to show fastboot menu */
2436 return;
2437boot_failed:
2438#if FBCON_DISPLAY_MSG
2439 /* revert to fastboot menu if boot failed */
2440 display_fastboot_menu();
2441#endif
2442 return;
Brian Swetland9c4c0752009-01-25 16:23:50 -08002443}
2444
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002445void cmd_erase_nand(const char *arg, void *data, unsigned sz)
Dima Zavin214cc642009-01-26 11:16:21 -08002446{
2447 struct ptentry *ptn;
2448 struct ptable *ptable;
2449
2450 ptable = flash_get_ptable();
2451 if (ptable == NULL) {
2452 fastboot_fail("partition table doesn't exist");
2453 return;
2454 }
2455
2456 ptn = ptable_find(ptable, arg);
2457 if (ptn == NULL) {
2458 fastboot_fail("unknown partition name");
2459 return;
2460 }
2461
2462 if (flash_erase(ptn)) {
2463 fastboot_fail("failed to erase partition");
2464 return;
2465 }
2466 fastboot_okay("");
2467}
2468
Bikas Gurungd48bd242010-09-04 19:54:32 -07002469
2470void cmd_erase_mmc(const char *arg, void *data, unsigned sz)
2471{
2472 unsigned long long ptn = 0;
Oliver Wangcee448d2013-10-22 18:40:13 +08002473 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07002474 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002475 uint8_t lun = 0;
Bikas Gurungd48bd242010-09-04 19:54:32 -07002476
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002477#if VERIFIED_BOOT
2478 if(!strcmp(arg, KEYSTORE_PTN_NAME))
2479 {
2480 if(!device.is_unlocked)
2481 {
2482 fastboot_fail("unlock device to erase keystore");
2483 return;
2484 }
2485 }
2486#endif
2487
Kinson Chikf1a43512011-07-14 11:28:39 -07002488 index = partition_get_index(arg);
2489 ptn = partition_get_offset(index);
Oliver Wangcee448d2013-10-22 18:40:13 +08002490 size = partition_get_size(index);
Neeti Desaica8c9602011-10-06 11:40:00 -07002491
Kinson Chikf1a43512011-07-14 11:28:39 -07002492 if(ptn == 0) {
Neeti Desaica8c9602011-10-06 11:40:00 -07002493 fastboot_fail("Partition table doesn't exist\n");
Bikas Gurungd48bd242010-09-04 19:54:32 -07002494 return;
2495 }
Kun Liang2f1601a2013-08-12 16:29:54 +08002496
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002497 lun = partition_get_lun(index);
2498 mmc_set_lun(lun);
2499
Sridhar Parasuramd7957122015-02-27 11:33:40 -08002500 if (platform_boot_dev_isemmc())
2501 {
2502 if (mmc_erase_card(ptn, size)) {
2503 fastboot_fail("failed to erase partition\n");
2504 return;
2505 }
2506 } else {
2507 BUF_DMA_ALIGN(out, DEFAULT_ERASE_SIZE);
2508 size = partition_get_size(index);
2509 if (size > DEFAULT_ERASE_SIZE)
2510 size = DEFAULT_ERASE_SIZE;
Kun Liang2f1601a2013-08-12 16:29:54 +08002511
Sridhar Parasuramd7957122015-02-27 11:33:40 -08002512 /* Simple inefficient version of erase. Just writing
2513 0 in first several blocks */
2514 if (mmc_write(ptn , size, (unsigned int *)out)) {
2515 fastboot_fail("failed to erase partition");
2516 return;
2517 }
Bikas Gurungd48bd242010-09-04 19:54:32 -07002518 }
Sridhar Parasuram32b30662015-07-10 13:33:22 -07002519#if VERIFIED_BOOT
Parth Dixitbc9b6492015-10-18 00:41:38 +05302520#if !VBOOT_MOTA
Sridhar Parasuram32b30662015-07-10 13:33:22 -07002521 if(!(strncmp(arg, "userdata", 8)))
2522 if(send_delete_keys_to_tz())
2523 ASSERT(0);
2524#endif
Parth Dixitbc9b6492015-10-18 00:41:38 +05302525#endif
Bikas Gurungd48bd242010-09-04 19:54:32 -07002526 fastboot_okay("");
2527}
2528
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002529void cmd_erase(const char *arg, void *data, unsigned sz)
2530{
Channagoud Kadabice3cf422015-04-17 16:02:30 -07002531#if VERIFIED_BOOT
Channagoud Kadabi35297672015-06-13 11:09:49 -07002532 if (target_build_variant_user())
Channagoud Kadabice3cf422015-04-17 16:02:30 -07002533 {
Sridhar Parasuramc32d07d2015-07-12 10:57:48 -07002534 if(!device.is_unlocked)
Channagoud Kadabice3cf422015-04-17 16:02:30 -07002535 {
Channagoud Kadabi35297672015-06-13 11:09:49 -07002536 fastboot_fail("device is locked. Cannot erase");
Channagoud Kadabice3cf422015-04-17 16:02:30 -07002537 return;
2538 }
2539 }
2540#endif
2541
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002542 if(target_is_emmc_boot())
2543 cmd_erase_mmc(arg, data, sz);
2544 else
2545 cmd_erase_nand(arg, data, sz);
2546}
Bikas Gurungd48bd242010-09-04 19:54:32 -07002547
Channagoud Kadabiad259832015-05-29 11:14:17 -07002548static uint32_t aboot_get_secret_key()
2549{
2550 /* 0 is invalid secret key, update this implementation to return
2551 * device specific unique secret key
2552 */
2553 return 0;
2554}
2555
Ajay Dudani5c761132011-04-07 20:19:04 -07002556void cmd_flash_mmc_img(const char *arg, void *data, unsigned sz)
Shashank Mittal23b8f422010-04-16 19:27:21 -07002557{
2558 unsigned long long ptn = 0;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07002559 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07002560 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002561 char *token = NULL;
2562 char *pname = NULL;
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07002563 char *sp;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002564 uint8_t lun = 0;
2565 bool lun_set = false;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07002566
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07002567 token = strtok_r((char *)arg, ":", &sp);
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002568 pname = token;
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07002569 token = strtok_r(NULL, ":", &sp);
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002570 if(token)
2571 {
2572 lun = atoi(token);
2573 mmc_set_lun(lun);
2574 lun_set = true;
2575 }
2576
Mao Jinlong226f33a2014-07-04 17:24:10 +08002577 if (pname)
Greg Grisco6e754772011-06-23 12:19:39 -07002578 {
Channagoud Kadabiad259832015-05-29 11:14:17 -07002579 if (!strncmp(pname, "frp-unlock", strlen("frp-unlock")))
2580 {
2581 if (!aboot_frp_unlock(pname, data, sz))
2582 {
2583 fastboot_info("FRP unlock successful");
2584 fastboot_okay("");
2585 }
2586 else
2587 fastboot_fail("Secret key is invalid, please update the bootloader with secret key");
2588
2589 return;
2590 }
2591
Mao Jinlong226f33a2014-07-04 17:24:10 +08002592 if (!strcmp(pname, "partition"))
2593 {
2594 dprintf(INFO, "Attempt to write partition image.\n");
2595 if (write_partition(sz, (unsigned char *) data)) {
2596 fastboot_fail("failed to write partition");
Greg Grisco6e754772011-06-23 12:19:39 -07002597 return;
2598 }
2599 }
Mao Jinlong226f33a2014-07-04 17:24:10 +08002600 else
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002601 {
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002602#if VERIFIED_BOOT
2603 if(!strcmp(pname, KEYSTORE_PTN_NAME))
2604 {
2605 if(!device.is_unlocked)
2606 {
2607 fastboot_fail("unlock device to flash keystore");
2608 return;
2609 }
Gaurav Nebhwanic7313cc2015-12-15 22:25:04 +05302610 if(!boot_verify_validate_keystore((unsigned char *)data,sz))
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002611 {
2612 fastboot_fail("image is not a keystore file");
2613 return;
2614 }
2615 }
2616#endif
Mao Jinlong226f33a2014-07-04 17:24:10 +08002617 index = partition_get_index(pname);
2618 ptn = partition_get_offset(index);
2619 if(ptn == 0) {
2620 fastboot_fail("partition table doesn't exist");
2621 return;
2622 }
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002623
Mao Jinlong226f33a2014-07-04 17:24:10 +08002624 if (!strcmp(pname, "boot") || !strcmp(pname, "recovery")) {
2625 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
2626 fastboot_fail("image is not a boot image");
2627 return;
2628 }
2629 }
2630
2631 if(!lun_set)
2632 {
2633 lun = partition_get_lun(index);
2634 mmc_set_lun(lun);
2635 }
2636
2637 size = partition_get_size(index);
Gaurav Nebhwani4d2389c2016-03-17 21:10:05 +05302638 if (ROUND_TO_PAGE(sz, mmc_blocksize_mask) > size) {
Mao Jinlong226f33a2014-07-04 17:24:10 +08002639 fastboot_fail("size too large");
2640 return;
2641 }
2642 else if (mmc_write(ptn , sz, (unsigned int *)data)) {
2643 fastboot_fail("flash write failure");
2644 return;
2645 }
Greg Grisco6e754772011-06-23 12:19:39 -07002646 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07002647 }
2648 fastboot_okay("");
2649 return;
2650}
2651
Ajay Dudanide984792015-03-02 09:57:41 -08002652void cmd_flash_meta_img(const char *arg, void *data, unsigned sz)
2653{
2654 int i, images;
2655 meta_header_t *meta_header;
2656 img_header_entry_t *img_header_entry;
Parth Dixit3e2d3032016-03-04 17:11:52 +05302657 /*End of the image address*/
2658 uintptr_t data_end;
2659
2660 if( (UINT_MAX - sz) > (uintptr_t)data )
2661 data_end = (uintptr_t)data + sz;
2662 else
2663 {
2664 fastboot_fail("Cannot flash: image header corrupt");
2665 return;
2666 }
2667
2668 if( data_end < ((uintptr_t)data + sizeof(meta_header_t)))
2669 {
2670 fastboot_fail("Cannot flash: image header corrupt");
2671 return;
2672 }
Ajay Dudanide984792015-03-02 09:57:41 -08002673
lijuang8ee21882016-04-19 16:57:11 +08002674 /* If device is locked:
2675 * Forbid to flash image to avoid the device to bypass the image
2676 * which with "any" name other than bootloader. Because it maybe
2677 * a meta package of all partitions.
2678 */
2679#if VERIFIED_BOOT
2680 if (target_build_variant_user()) {
2681 if (!device.is_unlocked) {
2682 fastboot_fail("Device is locked, meta image flashing is not allowed");
2683 return;
2684 }
2685#if !VBOOT_MOTA
2686 if(!device.is_unlock_critical) {
2687 fastboot_fail("Device is critical locked, Meta image flashing is not allowed");
2688 return;
2689 }
2690#endif
2691 }
2692#endif
2693
Ajay Dudanide984792015-03-02 09:57:41 -08002694 meta_header = (meta_header_t*) data;
Parth Dixit3e2d3032016-03-04 17:11:52 +05302695 if( data_end < ((uintptr_t)data + meta_header->img_hdr_sz))
2696 {
2697 fastboot_fail("Cannot flash: image header corrupt");
2698 return;
2699 }
Ajay Dudanide984792015-03-02 09:57:41 -08002700 img_header_entry = (img_header_entry_t*) (data+sizeof(meta_header_t));
2701
2702 images = meta_header->img_hdr_sz / sizeof(img_header_entry_t);
2703
2704 for (i=0; i<images; i++) {
2705
2706 if((img_header_entry[i].ptn_name == NULL) ||
2707 (img_header_entry[i].start_offset == 0) ||
2708 (img_header_entry[i].size == 0))
2709 break;
2710
Parth Dixit3e2d3032016-03-04 17:11:52 +05302711 if( data_end < ((uintptr_t)data + img_header_entry[i].start_offset
2712 + img_header_entry[i].size) )
2713 {
2714 fastboot_fail("Cannot flash: image size mismatch");
2715 break;
2716 }
2717
Ajay Dudanide984792015-03-02 09:57:41 -08002718 cmd_flash_mmc_img(img_header_entry[i].ptn_name,
2719 (void *) data + img_header_entry[i].start_offset,
2720 img_header_entry[i].size);
2721 }
2722
Channagoud Kadabiaafbbfc2015-03-04 17:52:56 -08002723 if (!strncmp(arg, "bootloader", strlen("bootloader")))
2724 {
2725 strlcpy(device.bootloader_version, TARGET(BOARD), MAX_VERSION_LEN);
2726 strlcat(device.bootloader_version, "-", MAX_VERSION_LEN);
2727 strlcat(device.bootloader_version, meta_header->img_version, MAX_VERSION_LEN);
2728 }
2729 else
2730 {
2731 strlcpy(device.radio_version, TARGET(BOARD), MAX_VERSION_LEN);
2732 strlcat(device.radio_version, "-", MAX_VERSION_LEN);
2733 strlcat(device.radio_version, meta_header->img_version, MAX_VERSION_LEN);
2734 }
2735
2736 write_device_info(&device);
Ajay Dudanide984792015-03-02 09:57:41 -08002737 fastboot_okay("");
2738 return;
2739}
2740
Ajay Dudani5c761132011-04-07 20:19:04 -07002741void cmd_flash_mmc_sparse_img(const char *arg, void *data, unsigned sz)
2742{
2743 unsigned int chunk;
wufeng.jiang813dc352015-06-02 23:02:46 -04002744 uint64_t chunk_data_sz;
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002745 uint32_t *fill_buf = NULL;
2746 uint32_t fill_val;
Ajay Dudani5c761132011-04-07 20:19:04 -07002747 sparse_header_t *sparse_header;
2748 chunk_header_t *chunk_header;
Ajay Dudaniab18f022011-05-12 14:39:22 -07002749 uint32_t total_blocks = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07002750 unsigned long long ptn = 0;
Channagoud Kadabi65b91002011-10-11 17:34:33 +05302751 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07002752 int index = INVALID_PTN;
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05302753 uint32_t i;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002754 uint8_t lun = 0;
vijay kumar800255e2015-04-24 20:26:19 +05302755 /*End of the sparse image address*/
Parth Dixit64b1a482016-03-07 16:31:26 +05302756 uintptr_t data_end = (uintptr_t)data + sz;
Ajay Dudani5c761132011-04-07 20:19:04 -07002757
Kinson Chikf1a43512011-07-14 11:28:39 -07002758 index = partition_get_index(arg);
2759 ptn = partition_get_offset(index);
2760 if(ptn == 0) {
Ajay Dudani5c761132011-04-07 20:19:04 -07002761 fastboot_fail("partition table doesn't exist");
2762 return;
2763 }
2764
Channagoud Kadabi65b91002011-10-11 17:34:33 +05302765 size = partition_get_size(index);
Channagoud Kadabi65b91002011-10-11 17:34:33 +05302766
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002767 lun = partition_get_lun(index);
2768 mmc_set_lun(lun);
2769
vijay kumar800255e2015-04-24 20:26:19 +05302770 if (sz < sizeof(sparse_header_t)) {
2771 fastboot_fail("size too low");
2772 return;
2773 }
2774
Ajay Dudani5c761132011-04-07 20:19:04 -07002775 /* Read and skip over sparse image header */
2776 sparse_header = (sparse_header_t *) data;
vijay kumar800255e2015-04-24 20:26:19 +05302777
vijay kumar1321f342015-03-27 12:13:42 +05302778 if (((uint64_t)sparse_header->total_blks * (uint64_t)sparse_header->blk_sz) > size) {
Ajay Dudani876b3282012-12-21 14:12:17 -08002779 fastboot_fail("size too large");
2780 return;
2781 }
2782
vijay kumarde4fcf62015-04-23 13:05:49 +05302783 data += sizeof(sparse_header_t);
vijay kumar800255e2015-04-24 20:26:19 +05302784
Parth Dixit64b1a482016-03-07 16:31:26 +05302785 if (data_end < (uintptr_t)data) {
vijay kumar800255e2015-04-24 20:26:19 +05302786 fastboot_fail("buffer overreads occured due to invalid sparse header");
2787 return;
2788 }
2789
vijay kumarde4fcf62015-04-23 13:05:49 +05302790 if(sparse_header->file_hdr_sz != sizeof(sparse_header_t))
Ajay Dudani5c761132011-04-07 20:19:04 -07002791 {
vijay kumarde4fcf62015-04-23 13:05:49 +05302792 fastboot_fail("sparse header size mismatch");
2793 return;
Ajay Dudani5c761132011-04-07 20:19:04 -07002794 }
2795
Ajay Dudanib06c05f2011-05-12 14:46:10 -07002796 dprintf (SPEW, "=== Sparse Image Header ===\n");
2797 dprintf (SPEW, "magic: 0x%x\n", sparse_header->magic);
2798 dprintf (SPEW, "major_version: 0x%x\n", sparse_header->major_version);
2799 dprintf (SPEW, "minor_version: 0x%x\n", sparse_header->minor_version);
2800 dprintf (SPEW, "file_hdr_sz: %d\n", sparse_header->file_hdr_sz);
2801 dprintf (SPEW, "chunk_hdr_sz: %d\n", sparse_header->chunk_hdr_sz);
2802 dprintf (SPEW, "blk_sz: %d\n", sparse_header->blk_sz);
2803 dprintf (SPEW, "total_blks: %d\n", sparse_header->total_blks);
2804 dprintf (SPEW, "total_chunks: %d\n", sparse_header->total_chunks);
Ajay Dudani5c761132011-04-07 20:19:04 -07002805
2806 /* Start processing chunks */
2807 for (chunk=0; chunk<sparse_header->total_chunks; chunk++)
2808 {
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302809 /* Make sure the total image size does not exceed the partition size */
2810 if(((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz) >= size) {
2811 fastboot_fail("size too large");
2812 return;
2813 }
Ajay Dudani5c761132011-04-07 20:19:04 -07002814 /* Read and skip over chunk header */
2815 chunk_header = (chunk_header_t *) data;
2816 data += sizeof(chunk_header_t);
2817
Parth Dixit64b1a482016-03-07 16:31:26 +05302818 if (data_end < (uintptr_t)data) {
vijay kumar800255e2015-04-24 20:26:19 +05302819 fastboot_fail("buffer overreads occured due to invalid sparse header");
2820 return;
2821 }
2822
Ajay Dudani5c761132011-04-07 20:19:04 -07002823 dprintf (SPEW, "=== Chunk Header ===\n");
2824 dprintf (SPEW, "chunk_type: 0x%x\n", chunk_header->chunk_type);
2825 dprintf (SPEW, "chunk_data_sz: 0x%x\n", chunk_header->chunk_sz);
2826 dprintf (SPEW, "total_size: 0x%x\n", chunk_header->total_sz);
2827
vijay kumar800255e2015-04-24 20:26:19 +05302828 if(sparse_header->chunk_hdr_sz != sizeof(chunk_header_t))
Ajay Dudani5c761132011-04-07 20:19:04 -07002829 {
vijay kumar800255e2015-04-24 20:26:19 +05302830 fastboot_fail("chunk header size mismatch");
2831 return;
Ajay Dudani5c761132011-04-07 20:19:04 -07002832 }
2833
wufeng.jiang813dc352015-06-02 23:02:46 -04002834 if (!sparse_header->blk_sz ){
2835 fastboot_fail("Invalid block size\n");
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302836 return;
2837 }
2838
wufeng.jiang813dc352015-06-02 23:02:46 -04002839 chunk_data_sz = (uint64_t)sparse_header->blk_sz * chunk_header->chunk_sz;
2840
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302841 /* Make sure that the chunk size calculated from sparse image does not
2842 * exceed partition size
2843 */
2844 if ((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz + chunk_data_sz > size)
2845 {
2846 fastboot_fail("Chunk data size exceeds partition size");
2847 return;
2848 }
2849
Ajay Dudani5c761132011-04-07 20:19:04 -07002850 switch (chunk_header->chunk_type)
2851 {
2852 case CHUNK_TYPE_RAW:
wufeng.jiang813dc352015-06-02 23:02:46 -04002853 if((uint64_t)chunk_header->total_sz != ((uint64_t)sparse_header->chunk_hdr_sz +
Ajay Dudani5c761132011-04-07 20:19:04 -07002854 chunk_data_sz))
2855 {
2856 fastboot_fail("Bogus chunk size for chunk type Raw");
2857 return;
2858 }
2859
Parth Dixit64b1a482016-03-07 16:31:26 +05302860 if (data_end < (uintptr_t)data + chunk_data_sz) {
vijay kumar800255e2015-04-24 20:26:19 +05302861 fastboot_fail("buffer overreads occured due to invalid sparse header");
2862 return;
2863 }
2864
wufeng.jiang813dc352015-06-02 23:02:46 -04002865 /* chunk_header->total_sz is uint32,So chunk_data_sz is now less than 2^32
2866 otherwise it will return in the line above
2867 */
Ajay Dudaniab18f022011-05-12 14:39:22 -07002868 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
wufeng.jiang813dc352015-06-02 23:02:46 -04002869 (uint32_t)chunk_data_sz,
Ajay Dudaniab18f022011-05-12 14:39:22 -07002870 (unsigned int*)data))
Ajay Dudani5c761132011-04-07 20:19:04 -07002871 {
2872 fastboot_fail("flash write failure");
2873 return;
2874 }
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302875 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
2876 fastboot_fail("Bogus size for RAW chunk type");
2877 return;
2878 }
Ajay Dudani5c761132011-04-07 20:19:04 -07002879 total_blocks += chunk_header->chunk_sz;
wufeng.jiang813dc352015-06-02 23:02:46 -04002880 data += (uint32_t)chunk_data_sz;
Ajay Dudani5c761132011-04-07 20:19:04 -07002881 break;
2882
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002883 case CHUNK_TYPE_FILL:
2884 if(chunk_header->total_sz != (sparse_header->chunk_hdr_sz +
2885 sizeof(uint32_t)))
2886 {
2887 fastboot_fail("Bogus chunk size for chunk type FILL");
2888 return;
2889 }
2890
2891 fill_buf = (uint32_t *)memalign(CACHE_LINE, ROUNDUP(sparse_header->blk_sz, CACHE_LINE));
2892 if (!fill_buf)
2893 {
2894 fastboot_fail("Malloc failed for: CHUNK_TYPE_FILL");
2895 return;
2896 }
2897
Parth Dixit64b1a482016-03-07 16:31:26 +05302898 if (data_end < (uintptr_t)data + sizeof(uint32_t)) {
vijay kumar800255e2015-04-24 20:26:19 +05302899 fastboot_fail("buffer overreads occured due to invalid sparse header");
Gaurav Nebhwani163cbf82016-03-17 21:21:27 +05302900 free(fill_buf);
vijay kumar800255e2015-04-24 20:26:19 +05302901 return;
2902 }
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002903 fill_val = *(uint32_t *)data;
2904 data = (char *) data + sizeof(uint32_t);
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002905
2906 for (i = 0; i < (sparse_header->blk_sz / sizeof(fill_val)); i++)
2907 {
2908 fill_buf[i] = fill_val;
2909 }
2910
Gaurav Nebhwanie94cbe12016-03-17 21:16:34 +05302911 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz))
2912 {
2913 fastboot_fail("bogus size for chunk FILL type");
2914 free(fill_buf);
2915 return;
2916 }
2917
wufeng.jiang813dc352015-06-02 23:02:46 -04002918 for (i = 0; i < chunk_header->chunk_sz; i++)
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002919 {
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302920 /* Make sure that the data written to partition does not exceed partition size */
2921 if ((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz + sparse_header->blk_sz > size)
2922 {
2923 fastboot_fail("Chunk data size for fill type exceeds partition size");
Gaurav Nebhwani163cbf82016-03-17 21:21:27 +05302924 free(fill_buf);
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302925 return;
2926 }
2927
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002928 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
2929 sparse_header->blk_sz,
2930 fill_buf))
2931 {
2932 fastboot_fail("flash write failure");
2933 free(fill_buf);
2934 return;
2935 }
2936
2937 total_blocks++;
2938 }
2939
2940 free(fill_buf);
2941 break;
2942
Ajay Dudani5c761132011-04-07 20:19:04 -07002943 case CHUNK_TYPE_DONT_CARE:
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302944 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
2945 fastboot_fail("bogus size for chunk DONT CARE type");
2946 return;
2947 }
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07002948 total_blocks += chunk_header->chunk_sz;
2949 break;
2950
Ajay Dudani5c761132011-04-07 20:19:04 -07002951 case CHUNK_TYPE_CRC:
2952 if(chunk_header->total_sz != sparse_header->chunk_hdr_sz)
2953 {
wufeng.jiang813dc352015-06-02 23:02:46 -04002954 fastboot_fail("Bogus chunk size for chunk type CRC");
Ajay Dudani5c761132011-04-07 20:19:04 -07002955 return;
2956 }
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302957 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
2958 fastboot_fail("bogus size for chunk CRC type");
2959 return;
2960 }
Ajay Dudani5c761132011-04-07 20:19:04 -07002961 total_blocks += chunk_header->chunk_sz;
Parth Dixit64b1a482016-03-07 16:31:26 +05302962 if ((uintptr_t)data > UINT_MAX - chunk_data_sz) {
vijay kumar800255e2015-04-24 20:26:19 +05302963 fastboot_fail("integer overflow occured");
2964 return;
2965 }
wufeng.jiang813dc352015-06-02 23:02:46 -04002966 data += (uint32_t)chunk_data_sz;
Parth Dixit64b1a482016-03-07 16:31:26 +05302967 if (data_end < (uintptr_t)data) {
vijay kumar800255e2015-04-24 20:26:19 +05302968 fastboot_fail("buffer overreads occured due to invalid sparse header");
2969 return;
2970 }
Ajay Dudani5c761132011-04-07 20:19:04 -07002971 break;
2972
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07002973 default:
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002974 dprintf(CRITICAL, "Unkown chunk type: %x\n",chunk_header->chunk_type);
Ajay Dudani5c761132011-04-07 20:19:04 -07002975 fastboot_fail("Unknown chunk type");
2976 return;
2977 }
2978 }
2979
Ajay Dudani0c6927b2011-05-18 11:12:16 -07002980 dprintf(INFO, "Wrote %d blocks, expected to write %d blocks\n",
2981 total_blocks, sparse_header->total_blks);
2982
2983 if(total_blocks != sparse_header->total_blks)
2984 {
2985 fastboot_fail("sparse image write failure");
2986 }
Ajay Dudani5c761132011-04-07 20:19:04 -07002987
2988 fastboot_okay("");
2989 return;
2990}
2991
2992void cmd_flash_mmc(const char *arg, void *data, unsigned sz)
2993{
2994 sparse_header_t *sparse_header;
Ajay Dudanide984792015-03-02 09:57:41 -08002995 meta_header_t *meta_header;
Ajay Dudani5c761132011-04-07 20:19:04 -07002996
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07002997#ifdef SSD_ENABLE
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -08002998 /* 8 Byte Magic + 2048 Byte xml + Encrypted Data */
2999 unsigned int *magic_number = (unsigned int *) data;
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08003000 int ret=0;
3001 uint32 major_version=0;
3002 uint32 minor_version=0;
3003
3004 ret = scm_svc_version(&major_version,&minor_version);
3005 if(!ret)
3006 {
3007 if(major_version >= 2)
3008 {
Amir Samuelovbb65ce02013-05-05 12:20:18 +03003009 if( !strcmp(arg, "ssd") || !strcmp(arg, "tqs") )
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08003010 {
3011 ret = encrypt_scm((uint32 **) &data, &sz);
3012 if (ret != 0) {
3013 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
3014 return;
3015 }
3016
Amir Samuelovbb65ce02013-05-05 12:20:18 +03003017 /* Protect only for SSD */
3018 if (!strcmp(arg, "ssd")) {
3019 ret = scm_protect_keystore((uint32 *) data, sz);
3020 if (ret != 0) {
3021 dprintf(CRITICAL, "ERROR: scm_protect_keystore Failed\n");
3022 return;
3023 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08003024 }
3025 }
3026 else
3027 {
3028 ret = decrypt_scm_v2((uint32 **) &data, &sz);
3029 if(ret != 0)
3030 {
3031 dprintf(CRITICAL,"ERROR: Decryption Failure\n");
3032 return;
3033 }
3034 }
3035 }
3036 else
3037 {
3038 if (magic_number[0] == DECRYPT_MAGIC_0 &&
3039 magic_number[1] == DECRYPT_MAGIC_1)
3040 {
3041 ret = decrypt_scm((uint32 **) &data, &sz);
3042 if (ret != 0) {
3043 dprintf(CRITICAL, "ERROR: Invalid secure image\n");
3044 return;
3045 }
3046 }
3047 else if (magic_number[0] == ENCRYPT_MAGIC_0 &&
3048 magic_number[1] == ENCRYPT_MAGIC_1)
3049 {
3050 ret = encrypt_scm((uint32 **) &data, &sz);
3051 if (ret != 0) {
3052 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
3053 return;
3054 }
3055 }
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07003056 }
3057 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08003058 else
Neeti Desai127b9e02012-03-20 16:11:23 -07003059 {
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08003060 dprintf(CRITICAL,"INVALID SVC Version\n");
3061 return;
Neeti Desai127b9e02012-03-20 16:11:23 -07003062 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08003063#endif /* SSD_ENABLE */
Neeti Desai127b9e02012-03-20 16:11:23 -07003064
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003065#if VERIFIED_BOOT
Channagoud Kadabi35297672015-06-13 11:09:49 -07003066 if (target_build_variant_user())
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003067 {
lijuang511a2b52015-08-14 20:50:51 +08003068 /* if device is locked:
3069 * common partition will not allow to be flashed
3070 * critical partition will allow to flash image.
3071 */
3072 if(!device.is_unlocked && !critical_flash_allowed(arg)) {
3073 fastboot_fail("Partition flashing is not allowed");
3074 return;
3075 }
Parth Dixitddbc7352015-10-18 03:13:31 +05303076#if !VBOOT_MOTA
lijuang511a2b52015-08-14 20:50:51 +08003077 /* if device critical is locked:
3078 * common partition will allow to be flashed
3079 * critical partition will not allow to flash image.
3080 */
3081 if(!device.is_unlock_critical && critical_flash_allowed(arg)) {
3082 fastboot_fail("Critical partition flashing is not allowed");
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003083 return;
3084 }
Parth Dixitddbc7352015-10-18 03:13:31 +05303085#endif
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003086 }
3087#endif
3088
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07003089 sparse_header = (sparse_header_t *) data;
Ajay Dudanide984792015-03-02 09:57:41 -08003090 meta_header = (meta_header_t *) data;
3091 if (sparse_header->magic == SPARSE_HEADER_MAGIC)
Ajay Dudani5c761132011-04-07 20:19:04 -07003092 cmd_flash_mmc_sparse_img(arg, data, sz);
Ajay Dudanide984792015-03-02 09:57:41 -08003093 else if (meta_header->magic == META_HEADER_MAGIC)
3094 cmd_flash_meta_img(arg, data, sz);
3095 else
3096 cmd_flash_mmc_img(arg, data, sz);
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07003097
3098#if VERIFIED_BOOT
Parth Dixitddbc7352015-10-18 03:13:31 +05303099#if !VBOOT_MOTA
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07003100 if((!strncmp(arg, "system", 6)) && !device.verity_mode)
3101 {
3102 // reset dm_verity mode to enforcing
3103 device.verity_mode = 1;
3104 write_device_info(&device);
3105 }
3106#endif
Parth Dixitddbc7352015-10-18 03:13:31 +05303107#endif
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07003108
Ajay Dudani5c761132011-04-07 20:19:04 -07003109 return;
3110}
3111
Tanya Brokhman84eaaf62015-02-24 21:57:06 +02003112void cmd_updatevol(const char *vol_name, void *data, unsigned sz)
3113{
3114 struct ptentry *sys_ptn;
3115 struct ptable *ptable;
3116
3117 ptable = flash_get_ptable();
3118 if (ptable == NULL) {
3119 fastboot_fail("partition table doesn't exist");
3120 return;
3121 }
3122
3123 sys_ptn = ptable_find(ptable, "system");
3124 if (sys_ptn == NULL) {
3125 fastboot_fail("system partition not found");
3126 return;
3127 }
3128
3129 sz = ROUND_TO_PAGE(sz, page_mask);
3130 if (update_ubi_vol(sys_ptn, vol_name, data, sz))
3131 fastboot_fail("update_ubi_vol failed");
3132 else
3133 fastboot_okay("");
3134}
3135
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003136void cmd_flash_nand(const char *arg, void *data, unsigned sz)
Dima Zavin214cc642009-01-26 11:16:21 -08003137{
3138 struct ptentry *ptn;
3139 struct ptable *ptable;
3140 unsigned extra = 0;
3141
3142 ptable = flash_get_ptable();
3143 if (ptable == NULL) {
3144 fastboot_fail("partition table doesn't exist");
3145 return;
3146 }
3147
3148 ptn = ptable_find(ptable, arg);
3149 if (ptn == NULL) {
Tanya Brokhman84eaaf62015-02-24 21:57:06 +02003150 dprintf(INFO, "unknown partition name (%s). Trying updatevol\n",
3151 arg);
3152 cmd_updatevol(arg, data, sz);
Dima Zavin214cc642009-01-26 11:16:21 -08003153 return;
3154 }
3155
3156 if (!strcmp(ptn->name, "boot") || !strcmp(ptn->name, "recovery")) {
3157 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
3158 fastboot_fail("image is not a boot image");
3159 return;
3160 }
3161 }
3162
Amol Jadi5c61a952012-05-04 17:05:35 -07003163 if (!strcmp(ptn->name, "system")
Deepa Dinamani13e32c42012-03-12 14:34:17 -07003164 || !strcmp(ptn->name, "userdata")
3165 || !strcmp(ptn->name, "persist")
Sundarajan Srinivasanb063a852013-11-19 14:02:27 -08003166 || !strcmp(ptn->name, "recoveryfs")
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -08003167 || !strcmp(ptn->name, "modem"))
Tanya Brokhman1c94f1a2015-02-15 09:05:03 +02003168 extra = 1;
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -08003169 else
Shashank Mittaldcc2e352009-11-19 19:11:16 -08003170 sz = ROUND_TO_PAGE(sz, page_mask);
Dima Zavin214cc642009-01-26 11:16:21 -08003171
3172 dprintf(INFO, "writing %d bytes to '%s'\n", sz, ptn->name);
Tanya Brokhman1c94f1a2015-02-15 09:05:03 +02003173 if (!memcmp((void *)data, UBI_MAGIC, UBI_MAGIC_SIZE)) {
3174 if (flash_ubi_img(ptn, data, sz)) {
3175 fastboot_fail("flash write failure");
3176 return;
3177 }
3178 } else {
3179 if (flash_write(ptn, extra, data, sz)) {
3180 fastboot_fail("flash write failure");
3181 return;
3182 }
Dima Zavin214cc642009-01-26 11:16:21 -08003183 }
3184 dprintf(INFO, "partition '%s' updated\n", ptn->name);
3185 fastboot_okay("");
3186}
3187
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003188void cmd_flash(const char *arg, void *data, unsigned sz)
3189{
3190 if(target_is_emmc_boot())
3191 cmd_flash_mmc(arg, data, sz);
3192 else
3193 cmd_flash_nand(arg, data, sz);
3194}
3195
Dima Zavin214cc642009-01-26 11:16:21 -08003196void cmd_continue(const char *arg, void *data, unsigned sz)
3197{
3198 fastboot_okay("");
Amol Jadi7c4316c2013-10-07 14:19:26 -07003199 fastboot_stop();
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003200
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003201 if (target_is_emmc_boot())
3202 {
lijuanga40d6302015-07-20 20:10:13 +08003203#if FBCON_DISPLAY_MSG
lijuangde34d502016-02-26 16:04:50 +08003204 /* Exit keys' detection thread firstly */
3205 exit_menu_keys_detection();
lijuanga40d6302015-07-20 20:10:13 +08003206#endif
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003207 boot_linux_from_mmc();
3208 }
3209 else
3210 {
3211 boot_linux_from_flash();
3212 }
Dima Zavin214cc642009-01-26 11:16:21 -08003213}
3214
Chandan Uddaraju94183c02010-01-15 15:13:59 -08003215void cmd_reboot(const char *arg, void *data, unsigned sz)
3216{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003217 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08003218 fastboot_okay("");
3219 reboot_device(0);
3220}
3221
3222void cmd_reboot_bootloader(const char *arg, void *data, unsigned sz)
3223{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003224 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08003225 fastboot_okay("");
3226 reboot_device(FASTBOOT_MODE);
3227}
3228
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003229void cmd_oem_enable_charger_screen(const char *arg, void *data, unsigned size)
3230{
3231 dprintf(INFO, "Enabling charger screen check\n");
3232 device.charger_screen_enabled = 1;
3233 write_device_info(&device);
3234 fastboot_okay("");
3235}
3236
3237void cmd_oem_disable_charger_screen(const char *arg, void *data, unsigned size)
3238{
3239 dprintf(INFO, "Disabling charger screen check\n");
3240 device.charger_screen_enabled = 0;
3241 write_device_info(&device);
3242 fastboot_okay("");
3243}
3244
lijuanga25d8bb2015-09-16 13:11:52 +08003245void cmd_oem_off_mode_charger(const char *arg, void *data, unsigned size)
3246{
3247 char *p = NULL;
3248 const char *delim = " \t\n\r";
Parth Dixit407f15b2016-01-07 14:47:37 +05303249 char *sp;
lijuanga25d8bb2015-09-16 13:11:52 +08003250
3251 if (arg) {
Parth Dixit407f15b2016-01-07 14:47:37 +05303252 p = strtok_r((char *)arg, delim, &sp);
lijuanga25d8bb2015-09-16 13:11:52 +08003253 if (p) {
3254 if (!strncmp(p, "0", 1)) {
3255 device.charger_screen_enabled = 0;
3256 } else if (!strncmp(p, "1", 1)) {
3257 device.charger_screen_enabled = 1;
3258 }
3259 }
3260 }
3261
3262 /* update charger_screen_enabled value for getvar
3263 * command
3264 */
3265 snprintf(charger_screen_enabled, MAX_RSP_SIZE, "%d",
3266 device.charger_screen_enabled);
3267
3268 write_device_info(&device);
3269 fastboot_okay("");
3270}
3271
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05303272void cmd_oem_select_display_panel(const char *arg, void *data, unsigned size)
3273{
3274 dprintf(INFO, "Selecting display panel %s\n", arg);
3275 if (arg)
3276 strlcpy(device.display_panel, arg,
3277 sizeof(device.display_panel));
3278 write_device_info(&device);
3279 fastboot_okay("");
3280}
3281
Shashank Mittal162244e2011-08-08 19:01:25 -07003282void cmd_oem_unlock(const char *arg, void *data, unsigned sz)
3283{
lijuang4ece1e72015-08-14 21:02:36 +08003284 set_device_unlock(UNLOCK, TRUE);
vijay kumarc65876c2015-04-24 13:29:16 +05303285}
3286
3287void cmd_oem_unlock_go(const char *arg, void *data, unsigned sz)
3288{
lijuang4ece1e72015-08-14 21:02:36 +08003289 if(!device.is_unlocked) {
vijay kumarc65876c2015-04-24 13:29:16 +05303290 if(!is_allow_unlock) {
3291 fastboot_fail("oem unlock is not allowed");
3292 return;
3293 }
3294
lijuang4ece1e72015-08-14 21:02:36 +08003295 set_device_unlock_value(UNLOCK, TRUE);
vijay kumarc65876c2015-04-24 13:29:16 +05303296
lijuang4ece1e72015-08-14 21:02:36 +08003297 /* wipe data */
vijay kumarc65876c2015-04-24 13:29:16 +05303298 struct recovery_message msg;
lijuang4ece1e72015-08-14 21:02:36 +08003299
vijay kumarc65876c2015-04-24 13:29:16 +05303300 snprintf(msg.recovery, sizeof(msg.recovery), "recovery\n--wipe_data");
3301 write_misc(0, &msg, sizeof(msg));
3302
3303 fastboot_okay("");
3304 reboot_device(RECOVERY_MODE);
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003305 }
3306 fastboot_okay("");
3307}
3308
Channagoud Kadabiad259832015-05-29 11:14:17 -07003309static int aboot_frp_unlock(char *pname, void *data, unsigned sz)
3310{
3311 int ret = 1;
3312 uint32_t secret_key;
3313 char seckey_buffer[MAX_RSP_SIZE];
3314
3315 secret_key = aboot_get_secret_key();
3316 if (secret_key)
3317 {
3318 snprintf((char *) seckey_buffer, MAX_RSP_SIZE, "%x", secret_key);
3319 if (!memcmp((void *)data, (void *)seckey_buffer, sz))
3320 {
3321 is_allow_unlock = true;
3322 write_allow_oem_unlock(is_allow_unlock);
3323 ret = 0;
3324 }
3325 }
3326 return ret;
3327}
3328
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003329void cmd_oem_lock(const char *arg, void *data, unsigned sz)
3330{
lijuang4ece1e72015-08-14 21:02:36 +08003331 set_device_unlock(UNLOCK, FALSE);
Shashank Mittal162244e2011-08-08 19:01:25 -07003332}
3333
Shashank Mittala0032282011-08-26 14:50:11 -07003334void cmd_oem_devinfo(const char *arg, void *data, unsigned sz)
3335{
lijuang511a2b52015-08-14 20:50:51 +08003336 char response[MAX_RSP_SIZE];
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003337 snprintf(response, sizeof(response), "\tDevice tampered: %s", (device.is_tampered ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07003338 fastboot_info(response);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003339 snprintf(response, sizeof(response), "\tDevice unlocked: %s", (device.is_unlocked ? "true" : "false"));
3340 fastboot_info(response);
Parth Dixitddbc7352015-10-18 03:13:31 +05303341#if !VBOOT_MOTA
lijuang511a2b52015-08-14 20:50:51 +08003342 snprintf(response, sizeof(response), "\tDevice critical unlocked: %s", (device.is_unlock_critical ? "true" : "false"));
3343 fastboot_info(response);
Parth Dixitddbc7352015-10-18 03:13:31 +05303344#endif
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003345 snprintf(response, sizeof(response), "\tCharger screen enabled: %s", (device.charger_screen_enabled ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07003346 fastboot_info(response);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05303347 snprintf(response, sizeof(response), "\tDisplay panel: %s", (device.display_panel));
3348 fastboot_info(response);
Shashank Mittala0032282011-08-26 14:50:11 -07003349 fastboot_okay("");
3350}
3351
lijuang511a2b52015-08-14 20:50:51 +08003352void cmd_flashing_get_unlock_ability(const char *arg, void *data, unsigned sz)
3353{
3354 char response[MAX_RSP_SIZE];
3355 snprintf(response, sizeof(response), "\tget_unlock_ability: %d", is_allow_unlock);
3356 fastboot_info(response);
3357 fastboot_okay("");
3358}
3359
3360void cmd_flashing_lock_critical(const char *arg, void *data, unsigned sz)
3361{
lijuang4ece1e72015-08-14 21:02:36 +08003362 set_device_unlock(UNLOCK_CRITICAL, FALSE);
lijuang511a2b52015-08-14 20:50:51 +08003363}
3364
3365void cmd_flashing_unlock_critical(const char *arg, void *data, unsigned sz)
3366{
lijuang4ece1e72015-08-14 21:02:36 +08003367 set_device_unlock(UNLOCK_CRITICAL, TRUE);
lijuang511a2b52015-08-14 20:50:51 +08003368}
3369
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07003370void cmd_preflash(const char *arg, void *data, unsigned sz)
3371{
3372 fastboot_okay("");
3373}
3374
Mao Flynn7b379f32015-04-20 00:28:30 +08003375static uint8_t logo_header[LOGO_IMG_HEADER_SIZE];
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303376
Mao Flynn7b379f32015-04-20 00:28:30 +08003377int splash_screen_check_header(logo_img_header *header)
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303378{
Mao Flynn7b379f32015-04-20 00:28:30 +08003379 if (memcmp(header->magic, LOGO_IMG_MAGIC, 8))
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303380 return -1;
Mao Flynn7b379f32015-04-20 00:28:30 +08003381 if (header->width == 0 || header->height == 0)
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303382 return -1;
3383 return 0;
3384}
3385
Mao Flynn7b379f32015-04-20 00:28:30 +08003386int splash_screen_flash()
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003387{
3388 struct ptentry *ptn;
3389 struct ptable *ptable;
Mao Flynn7b379f32015-04-20 00:28:30 +08003390 struct logo_img_header *header;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003391 struct fbcon_config *fb_display = NULL;
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08003392
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303393 ptable = flash_get_ptable();
3394 if (ptable == NULL) {
Mao Flynn7b379f32015-04-20 00:28:30 +08003395 dprintf(CRITICAL, "ERROR: Partition table not found\n");
3396 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303397 }
Mao Flynn7b379f32015-04-20 00:28:30 +08003398
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303399 ptn = ptable_find(ptable, "splash");
3400 if (ptn == NULL) {
3401 dprintf(CRITICAL, "ERROR: splash Partition not found\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003402 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303403 }
Mao Flynn7b379f32015-04-20 00:28:30 +08003404 if (flash_read(ptn, 0, (void *)logo_header, LOGO_IMG_HEADER_SIZE)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303405 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003406 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303407 }
3408
Mao Flynn7b379f32015-04-20 00:28:30 +08003409 header = (struct logo_img_header *)logo_header;
3410 if (splash_screen_check_header(header)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303411 dprintf(CRITICAL, "ERROR: Boot image header invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003412 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303413 }
3414
3415 fb_display = fbcon_display();
3416 if (fb_display) {
Mao Flynn7b379f32015-04-20 00:28:30 +08003417 if (header->type && (header->blocks != 0)) { // RLE24 compressed data
3418 uint8_t *base = (uint8_t *) fb_display->base + LOGO_IMG_OFFSET;
3419
3420 /* if the logo is full-screen size, remove "fbcon_clear()" */
3421 if ((header->width != fb_display->width)
3422 || (header->height != fb_display->height))
3423 fbcon_clear();
3424
3425 if (flash_read(ptn + LOGO_IMG_HEADER_SIZE, 0,
3426 (uint32_t *)base,
3427 (header->blocks * 512))) {
3428 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
3429 return -1;
3430 }
3431 fbcon_extract_to_screen(header, base);
3432 return 0;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003433 }
Mao Flynn7b379f32015-04-20 00:28:30 +08003434
Veera Sundaram Sankaranc33416f2015-09-28 10:25:55 -07003435 if ((header->width > fb_display->width) || (header->height > fb_display->height)) {
3436 dprintf(CRITICAL, "Logo config greater than fb config. Fall back default logo\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003437 return -1;
3438 }
3439
Vineet Bajaj99291ed2014-09-09 12:29:46 +05303440 uint8_t *base = (uint8_t *) fb_display->base;
Mao Flynn7b379f32015-04-20 00:28:30 +08003441 if (flash_read(ptn + LOGO_IMG_HEADER_SIZE, 0,
3442 (uint32_t *)base,
3443 ((((header->width * header->height * fb_display->bpp/8) + 511) >> 9) << 9))) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303444 fbcon_clear();
Vineet Bajaj99291ed2014-09-09 12:29:46 +05303445 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003446 return -1;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003447 }
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303448 }
3449
Mao Flynn7b379f32015-04-20 00:28:30 +08003450 return 0;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303451}
3452
Mao Flynn7b379f32015-04-20 00:28:30 +08003453int splash_screen_mmc()
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303454{
3455 int index = INVALID_PTN;
3456 unsigned long long ptn = 0;
3457 struct fbcon_config *fb_display = NULL;
Mao Flynn7b379f32015-04-20 00:28:30 +08003458 struct logo_img_header *header;
Mao Flynn1893a7f2015-06-03 12:03:36 +08003459 uint32_t blocksize, realsize, readsize;
3460 uint8_t *base;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303461
3462 index = partition_get_index("splash");
3463 if (index == 0) {
3464 dprintf(CRITICAL, "ERROR: splash Partition table not found\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003465 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303466 }
3467
3468 ptn = partition_get_offset(index);
3469 if (ptn == 0) {
3470 dprintf(CRITICAL, "ERROR: splash Partition invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003471 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303472 }
3473
Mao Flynn1893a7f2015-06-03 12:03:36 +08003474 mmc_set_lun(partition_get_lun(index));
3475
3476 blocksize = mmc_get_device_blocksize();
3477 if (blocksize == 0) {
3478 dprintf(CRITICAL, "ERROR:splash Partition invalid blocksize\n");
3479 return -1;
3480 }
3481
3482 fb_display = fbcon_display();
Channagoud Kadabidaf10a62015-07-22 11:51:55 -07003483 if (!fb_display)
3484 {
3485 dprintf(CRITICAL, "ERROR: fb config is not allocated\n");
3486 return -1;
3487 }
3488
Mao Flynn1893a7f2015-06-03 12:03:36 +08003489 base = (uint8_t *) fb_display->base;
3490
3491 if (mmc_read(ptn, (uint32_t *)(base + LOGO_IMG_OFFSET), blocksize)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303492 dprintf(CRITICAL, "ERROR: Cannot read splash image header\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003493 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303494 }
3495
Mao Flynn1893a7f2015-06-03 12:03:36 +08003496 header = (struct logo_img_header *)(base + LOGO_IMG_OFFSET);
Mao Flynn7b379f32015-04-20 00:28:30 +08003497 if (splash_screen_check_header(header)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303498 dprintf(CRITICAL, "ERROR: Splash image header invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003499 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303500 }
3501
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303502 if (fb_display) {
Mao Flynn1893a7f2015-06-03 12:03:36 +08003503 if (header->type && (header->blocks != 0)) { /* 1 RLE24 compressed data */
3504 base += LOGO_IMG_OFFSET;
Mao Flynn7b379f32015-04-20 00:28:30 +08003505
Mao Flynn1893a7f2015-06-03 12:03:36 +08003506 realsize = header->blocks * 512;
3507 readsize = ROUNDUP((realsize + LOGO_IMG_HEADER_SIZE), blocksize) - blocksize;
3508
3509 /* if the logo is not full-screen size, clean screen */
Mao Flynn7b379f32015-04-20 00:28:30 +08003510 if ((header->width != fb_display->width)
3511 || (header->height != fb_display->height))
3512 fbcon_clear();
3513
Mao Flynn1893a7f2015-06-03 12:03:36 +08003514 if (mmc_read(ptn + blocksize, (uint32_t *)(base + blocksize), readsize)) {
Mao Flynn7b379f32015-04-20 00:28:30 +08003515 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
3516 return -1;
3517 }
Mao Flynn7b379f32015-04-20 00:28:30 +08003518
Mao Flynn1893a7f2015-06-03 12:03:36 +08003519 fbcon_extract_to_screen(header, (base + LOGO_IMG_HEADER_SIZE));
3520 } else { /* 2 Raw BGR data */
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303521
Veera Sundaram Sankaranc33416f2015-09-28 10:25:55 -07003522 if ((header->width > fb_display->width) || (header->height > fb_display->height)) {
3523 dprintf(CRITICAL, "Logo config greater than fb config. Fall back default logo\n");
Mao Flynn1893a7f2015-06-03 12:03:36 +08003524 return -1;
3525 }
3526
3527 realsize = header->width * header->height * fb_display->bpp / 8;
3528 readsize = ROUNDUP((realsize + LOGO_IMG_HEADER_SIZE), blocksize) - blocksize;
3529
3530 if (blocksize == LOGO_IMG_HEADER_SIZE) { /* read the content directly */
3531 if (mmc_read((ptn + LOGO_IMG_HEADER_SIZE), (uint32_t *)base, readsize)) {
3532 fbcon_clear();
3533 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
3534 return -1;
3535 }
3536 } else {
3537 if (mmc_read(ptn + blocksize ,
3538 (uint32_t *)(base + LOGO_IMG_OFFSET + blocksize), readsize)) {
3539 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
3540 return -1;
3541 }
3542 memmove(base, (base + LOGO_IMG_OFFSET + LOGO_IMG_HEADER_SIZE), realsize);
3543 }
3544 }
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303545 }
3546
Mao Flynn7b379f32015-04-20 00:28:30 +08003547 return 0;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303548}
3549
Mao Flynn7b379f32015-04-20 00:28:30 +08003550int fetch_image_from_partition()
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303551{
3552 if (target_is_emmc_boot()) {
3553 return splash_screen_mmc();
3554 } else {
3555 return splash_screen_flash();
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003556 }
3557}
3558
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07003559/* Get the size from partiton name */
3560static void get_partition_size(const char *arg, char *response)
3561{
3562 uint64_t ptn = 0;
3563 uint64_t size;
3564 int index = INVALID_PTN;
3565
3566 index = partition_get_index(arg);
3567
3568 if (index == INVALID_PTN)
3569 {
3570 dprintf(CRITICAL, "Invalid partition index\n");
3571 return;
3572 }
3573
3574 ptn = partition_get_offset(index);
3575
3576 if(!ptn)
3577 {
3578 dprintf(CRITICAL, "Invalid partition name %s\n", arg);
3579 return;
3580 }
3581
3582 size = partition_get_size(index);
3583
3584 snprintf(response, MAX_RSP_SIZE, "\t 0x%llx", size);
3585 return;
3586}
3587
3588/*
3589 * Publish the partition type & size info
3590 * fastboot getvar will publish the required information.
3591 * fastboot getvar partition_size:<partition_name>: partition size in hex
3592 * fastboot getvar partition_type:<partition_name>: partition type (ext/fat)
3593 */
3594static void publish_getvar_partition_info(struct getvar_partition_info *info, uint8_t num_parts)
3595{
3596 uint8_t i;
3597
3598 for (i = 0; i < num_parts; i++) {
3599 get_partition_size(info[i].part_name, info[i].size_response);
3600
3601 if (strlcat(info[i].getvar_size, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
3602 {
3603 dprintf(CRITICAL, "partition size name truncated\n");
3604 return;
3605 }
3606 if (strlcat(info[i].getvar_type, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
3607 {
3608 dprintf(CRITICAL, "partition type name truncated\n");
3609 return;
3610 }
3611
3612 /* publish partition size & type info */
3613 fastboot_publish((const char *) info[i].getvar_size, (const char *) info[i].size_response);
3614 fastboot_publish((const char *) info[i].getvar_type, (const char *) info[i].type_response);
3615 }
3616}
3617
lijuang4ece1e72015-08-14 21:02:36 +08003618void get_product_name(unsigned char *buf)
3619{
3620 snprintf((char*)buf, MAX_RSP_SIZE, "%s", TARGET(BOARD));
3621 return;
3622}
3623
3624void get_bootloader_version(unsigned char *buf)
3625{
3626 snprintf((char*)buf, MAX_RSP_SIZE, "%s", device.bootloader_version);
3627 return;
3628}
3629
3630void get_baseband_version(unsigned char *buf)
3631{
3632 snprintf((char*)buf, MAX_RSP_SIZE, "%s", device.radio_version);
3633 return;
3634}
3635
3636bool is_device_locked()
3637{
3638 return device.is_unlocked ? false:true;
3639}
3640
Amol Jadi5edf3552013-07-23 14:15:34 -07003641/* register commands and variables for fastboot */
3642void aboot_fastboot_register_commands(void)
3643{
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003644 int i;
lijuangf16461c2015-08-03 17:09:34 +08003645 char hw_platform_buf[MAX_RSP_SIZE];
Amol Jadi5edf3552013-07-23 14:15:34 -07003646
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003647 struct fastboot_cmd_desc cmd_list[] = {
lijuang511a2b52015-08-14 20:50:51 +08003648 /* By default the enabled list is empty. */
3649 {"", NULL},
3650 /* move commands enclosed within the below ifndef to here
3651 * if they need to be enabled in user build.
3652 */
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003653#ifndef DISABLE_FASTBOOT_CMDS
lijuang511a2b52015-08-14 20:50:51 +08003654 /* Register the following commands only for non-user builds */
3655 {"flash:", cmd_flash},
3656 {"erase:", cmd_erase},
3657 {"boot", cmd_boot},
3658 {"continue", cmd_continue},
3659 {"reboot", cmd_reboot},
3660 {"reboot-bootloader", cmd_reboot_bootloader},
3661 {"oem unlock", cmd_oem_unlock},
3662 {"oem unlock-go", cmd_oem_unlock_go},
3663 {"oem lock", cmd_oem_lock},
3664 {"flashing unlock", cmd_oem_unlock},
3665 {"flashing lock", cmd_oem_lock},
3666 {"flashing lock_critical", cmd_flashing_lock_critical},
3667 {"flashing unlock_critical", cmd_flashing_unlock_critical},
3668 {"flashing get_unlock_ability", cmd_flashing_get_unlock_ability},
3669 {"oem device-info", cmd_oem_devinfo},
3670 {"preflash", cmd_preflash},
3671 {"oem enable-charger-screen", cmd_oem_enable_charger_screen},
3672 {"oem disable-charger-screen", cmd_oem_disable_charger_screen},
lijuanga25d8bb2015-09-16 13:11:52 +08003673 {"oem off-mode-charge", cmd_oem_off_mode_charger},
lijuang511a2b52015-08-14 20:50:51 +08003674 {"oem select-display-panel", cmd_oem_select_display_panel},
Channagoud Kadabi736c4962015-08-21 11:56:52 -07003675#if UNITTEST_FW_SUPPORT
Channagoud Kadabid5ddf482015-09-28 10:31:35 -07003676 {"oem run-tests", cmd_oem_runtests},
Channagoud Kadabi736c4962015-08-21 11:56:52 -07003677#endif
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003678#endif
lijuang511a2b52015-08-14 20:50:51 +08003679 };
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003680
3681 int fastboot_cmds_count = sizeof(cmd_list)/sizeof(cmd_list[0]);
3682 for (i = 1; i < fastboot_cmds_count; i++)
3683 fastboot_register(cmd_list[i].name,cmd_list[i].cb);
3684
Amol Jadi5edf3552013-07-23 14:15:34 -07003685 /* publish variables and their values */
3686 fastboot_publish("product", TARGET(BOARD));
3687 fastboot_publish("kernel", "lk");
3688 fastboot_publish("serialno", sn_buf);
3689
3690 /*
3691 * partition info is supported only for emmc partitions
3692 * Calling this for NAND prints some error messages which
3693 * is harmless but misleading. Avoid calling this for NAND
3694 * devices.
3695 */
3696 if (target_is_emmc_boot())
3697 publish_getvar_partition_info(part_info, ARRAY_SIZE(part_info));
3698
3699 /* Max download size supported */
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003700 snprintf(max_download_size, MAX_RSP_SIZE, "\t0x%x",
3701 target_get_max_flash_size());
Amol Jadi5edf3552013-07-23 14:15:34 -07003702 fastboot_publish("max-download-size", (const char *) max_download_size);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003703 /* Is the charger screen check enabled */
3704 snprintf(charger_screen_enabled, MAX_RSP_SIZE, "%d",
3705 device.charger_screen_enabled);
3706 fastboot_publish("charger-screen-enabled",
3707 (const char *) charger_screen_enabled);
lijuanga25d8bb2015-09-16 13:11:52 +08003708 fastboot_publish("off-mode-charge", (const char *) charger_screen_enabled);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05303709 snprintf(panel_display_mode, MAX_RSP_SIZE, "%s",
3710 device.display_panel);
3711 fastboot_publish("display-panel",
3712 (const char *) panel_display_mode);
Channagoud Kadabiaafbbfc2015-03-04 17:52:56 -08003713 fastboot_publish("version-bootloader", (const char *) device.bootloader_version);
3714 fastboot_publish("version-baseband", (const char *) device.radio_version);
lijuangf16461c2015-08-03 17:09:34 +08003715 fastboot_publish("secure", is_secure_boot_enable()? "yes":"no");
3716 smem_get_hw_platform_name((unsigned char *) hw_platform_buf, sizeof(hw_platform_buf));
3717 snprintf(get_variant, MAX_RSP_SIZE, "%s %s", hw_platform_buf,
3718 target_is_emmc_boot()? "eMMC":"UFS");
3719 fastboot_publish("variant", (const char *) get_variant);
lijuang65c5a822015-08-29 16:35:36 +08003720#if CHECK_BAT_VOLTAGE
lijuang102dfa92015-10-09 18:31:03 +08003721 update_battery_status();
lijuang65c5a822015-08-29 16:35:36 +08003722 fastboot_publish("battery-voltage", (const char *) battery_voltage);
lijuang102dfa92015-10-09 18:31:03 +08003723 fastboot_publish("battery-soc-ok", (const char *) battery_soc_ok);
lijuang65c5a822015-08-29 16:35:36 +08003724#endif
Amol Jadi5edf3552013-07-23 14:15:34 -07003725}
3726
Brian Swetland9c4c0752009-01-25 16:23:50 -08003727void aboot_init(const struct app_descriptor *app)
3728{
Shashank Mittal4f99a882010-02-01 13:58:50 -08003729 unsigned reboot_mode = 0;
Chandan Uddarajubedca152010-06-02 23:05:15 -07003730
lijuang39831732016-01-08 17:49:02 +08003731 /* Initialise wdog to catch early lk crashes */
3732#if WDOG_SUPPORT
3733 msm_wdog_init();
3734#endif
3735
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07003736 /* Setup page size information for nv storage */
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003737 if (target_is_emmc_boot())
3738 {
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07003739 page_size = mmc_page_size();
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003740 page_mask = page_size - 1;
Gaurav Nebhwani4d2389c2016-03-17 21:10:05 +05303741 mmc_blocksize = mmc_get_device_blocksize();
3742 mmc_blocksize_mask = mmc_blocksize - 1;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003743 }
3744 else
3745 {
3746 page_size = flash_page_size();
3747 page_mask = page_size - 1;
3748 }
3749
Deepa Dinamani0e163a42013-05-24 17:08:15 -07003750 ASSERT((MEMBASE + MEMSIZE) > MEMBASE);
3751
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003752 read_device_info(&device);
vijay kumarc65876c2015-04-24 13:29:16 +05303753 read_allow_oem_unlock(&device);
Shashank Mittal162244e2011-08-08 19:01:25 -07003754
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08003755 /* Display splash screen if enabled */
3756#if DISPLAY_SPLASH_SCREEN
lijuang99c02d82015-02-13 19:04:34 +08003757#if NO_ALARM_DISPLAY
3758 if (!check_alarm_boot()) {
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08003759#endif
lijuang99c02d82015-02-13 19:04:34 +08003760 dprintf(SPEW, "Display Init: Start\n");
Channagoud Kadabi90869ce2015-04-27 11:15:14 -07003761#if ENABLE_WBC
Channagoud Kadabid801ac72015-08-26 11:21:51 -07003762 /* Wait if the display shutdown is in progress */
3763 while(pm_app_display_shutdown_in_prgs());
Channagoud Kadabi90869ce2015-04-27 11:15:14 -07003764 if (!pm_appsbl_display_init_done())
3765 target_display_init(device.display_panel);
3766 else
3767 display_image_on_screen();
3768#else
lijuang99c02d82015-02-13 19:04:34 +08003769 target_display_init(device.display_panel);
Channagoud Kadabi90869ce2015-04-27 11:15:14 -07003770#endif
lijuang99c02d82015-02-13 19:04:34 +08003771 dprintf(SPEW, "Display Init: Done\n");
3772#if NO_ALARM_DISPLAY
3773 }
3774#endif
3775#endif
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08003776
Greg Griscod6250552011-06-29 14:40:23 -07003777 target_serialno((unsigned char *) sn_buf);
Ajay Dudanib06c05f2011-05-12 14:46:10 -07003778 dprintf(SPEW,"serial number: %s\n",sn_buf);
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -08003779
Dhaval Patel223ec952013-07-18 14:49:44 -07003780 memset(display_panel_buf, '\0', MAX_PANEL_BUF_SIZE);
3781
Matthew Qindefd5562014-07-11 18:02:40 +08003782 /*
3783 * Check power off reason if user force reset,
3784 * if yes phone will do normal boot.
3785 */
3786 if (is_user_force_reset())
3787 goto normal_boot;
3788
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003789 /* Check if we should do something other than booting up */
Ameya Thakur0b9c2442013-05-10 13:22:56 -07003790 if (keys_get_state(KEY_VOLUMEUP) && keys_get_state(KEY_VOLUMEDOWN))
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07003791 {
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03003792 dprintf(ALWAYS,"dload mode key sequence detected\n");
lijuang395b5e62015-11-19 17:39:44 +08003793 reboot_device(EMERGENCY_DLOAD);
3794 dprintf(CRITICAL,"Failed to reboot into dload mode\n");
3795
Ameya Thakur0b9c2442013-05-10 13:22:56 -07003796 boot_into_fastboot = true;
3797 }
3798 if (!boot_into_fastboot)
3799 {
3800 if (keys_get_state(KEY_HOME) || keys_get_state(KEY_VOLUMEUP))
3801 boot_into_recovery = 1;
3802 if (!boot_into_recovery &&
3803 (keys_get_state(KEY_BACK) || keys_get_state(KEY_VOLUMEDOWN)))
Pavel Nedev5d91d412013-04-29 11:34:24 +03003804 boot_into_fastboot = true;
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07003805 }
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003806 #if NO_KEYPAD_DRIVER
Kinson Chik0b1c8162011-08-31 16:31:57 -07003807 if (fastboot_trigger())
Pavel Nedev5d91d412013-04-29 11:34:24 +03003808 boot_into_fastboot = true;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003809 #endif
Chandan Uddarajubedca152010-06-02 23:05:15 -07003810
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003811#if USE_PON_REBOOT_REG
3812 reboot_mode = check_hard_reboot_mode();
3813#else
Ajay Dudani77421292010-10-27 19:34:06 -07003814 reboot_mode = check_reboot_mode();
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003815#endif
3816 if (reboot_mode == RECOVERY_MODE)
3817 {
Ajay Dudani77421292010-10-27 19:34:06 -07003818 boot_into_recovery = 1;
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003819 }
3820 else if(reboot_mode == FASTBOOT_MODE)
3821 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03003822 boot_into_fastboot = true;
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003823 }
3824 else if(reboot_mode == ALARM_BOOT)
3825 {
Matthew Qind886f3c2014-01-17 16:52:01 +08003826 boot_reason_alarm = true;
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003827 }
Parth Dixit207573a2015-10-27 17:26:21 +05303828#if VERIFIED_BOOT
Parth Dixitddbc7352015-10-18 03:13:31 +05303829#if !VBOOT_MOTA
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003830 else if (reboot_mode == DM_VERITY_ENFORCING)
3831 {
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07003832 device.verity_mode = 1;
3833 write_device_info(&device);
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003834 }
3835 else if (reboot_mode == DM_VERITY_LOGGING)
3836 {
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07003837 device.verity_mode = 0;
3838 write_device_info(&device);
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003839 }
3840 else if (reboot_mode == DM_VERITY_KEYSCLEAR)
3841 {
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07003842 if(send_delete_keys_to_tz())
3843 ASSERT(0);
Ajay Dudani77421292010-10-27 19:34:06 -07003844 }
Parth Dixit207573a2015-10-27 17:26:21 +05303845#endif
Parth Dixitddbc7352015-10-18 03:13:31 +05303846#endif
Ajay Dudani77421292010-10-27 19:34:06 -07003847
Matthew Qindefd5562014-07-11 18:02:40 +08003848normal_boot:
Pavel Nedev5d91d412013-04-29 11:34:24 +03003849 if (!boot_into_fastboot)
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003850 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03003851 if (target_is_emmc_boot())
Shashank Mittala0032282011-08-26 14:50:11 -07003852 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03003853 if(emmc_recovery_init())
3854 dprintf(ALWAYS,"error in emmc_recovery_init\n");
3855 if(target_use_signed_kernel())
Shashank Mittala0032282011-08-26 14:50:11 -07003856 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03003857 if((device.is_unlocked) || (device.is_tampered))
3858 {
3859 #ifdef TZ_TAMPER_FUSE
3860 set_tamper_fuse_cmd();
3861 #endif
3862 #if USE_PCOM_SECBOOT
3863 set_tamper_flag(device.is_tampered);
3864 #endif
3865 }
Shashank Mittala0032282011-08-26 14:50:11 -07003866 }
Amit Blay6281ebc2015-01-11 14:44:08 +02003867
Pavel Nedev5d91d412013-04-29 11:34:24 +03003868 boot_linux_from_mmc();
Shashank Mittala0032282011-08-26 14:50:11 -07003869 }
Pavel Nedev5d91d412013-04-29 11:34:24 +03003870 else
3871 {
3872 recovery_init();
3873 #if USE_PCOM_SECBOOT
3874 if((device.is_unlocked) || (device.is_tampered))
3875 set_tamper_flag(device.is_tampered);
3876 #endif
3877 boot_linux_from_flash();
3878 }
3879 dprintf(CRITICAL, "ERROR: Could not do normal boot. Reverting "
3880 "to fastboot mode.\n");
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003881 }
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003882
Amol Jadi5edf3552013-07-23 14:15:34 -07003883 /* We are here means regular boot did not happen. Start fastboot. */
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07003884
Amol Jadi5edf3552013-07-23 14:15:34 -07003885 /* register aboot specific fastboot commands */
3886 aboot_fastboot_register_commands();
Amol Jadi57abe4c2011-05-24 15:47:27 -07003887
Amol Jadi5edf3552013-07-23 14:15:34 -07003888 /* dump partition table for debug info */
Kinson Chikf1a43512011-07-14 11:28:39 -07003889 partition_dump();
Amol Jadi5edf3552013-07-23 14:15:34 -07003890
3891 /* initialize and start fastboot */
3892 fastboot_init(target_get_scratch_address(), target_get_max_flash_size());
lijuang4ece1e72015-08-14 21:02:36 +08003893#if FBCON_DISPLAY_MSG
lijuang9a7d3b92015-11-30 14:41:24 +08003894 display_fastboot_menu();
lijuang4ece1e72015-08-14 21:02:36 +08003895#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08003896}
3897
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07003898uint32_t get_page_size()
3899{
3900 return page_size;
3901}
3902
Amir Samuelov57a6fa22013-06-05 16:36:43 +03003903/*
3904 * Calculated and save hash (SHA256) for non-signed boot image.
3905 *
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07003906 * @param image_addr - Boot image address
3907 * @param image_size - Size of the boot image
Amir Samuelov57a6fa22013-06-05 16:36:43 +03003908 *
3909 * @return int - 0 on success, negative value on failure.
3910 */
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07003911static int aboot_save_boot_hash_mmc(uint32_t image_addr, uint32_t image_size)
Amir Samuelov57a6fa22013-06-05 16:36:43 +03003912{
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07003913 unsigned int digest[8];
3914#if IMAGE_VERIF_ALGO_SHA1
3915 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA1;
3916#else
3917 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA256;
3918#endif
Amir Samuelov57a6fa22013-06-05 16:36:43 +03003919
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07003920 target_crypto_init_params();
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -08003921 hash_find((unsigned char *) image_addr, image_size, (unsigned char *)&digest, auth_algo);
Amir Samuelov57a6fa22013-06-05 16:36:43 +03003922
3923 save_kernel_hash_cmd(digest);
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07003924 dprintf(INFO, "aboot_save_boot_hash_mmc: imagesize_actual size %d bytes.\n", (int) image_size);
Amir Samuelov57a6fa22013-06-05 16:36:43 +03003925
3926 return 0;
3927}
3928
Brian Swetland9c4c0752009-01-25 16:23:50 -08003929APP_START(aboot)
3930 .init = aboot_init,
3931APP_END