blob: ae4aa6371e44e60f376b59a98472c3a59205942b [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
Shay Nachmani062aa3f2015-05-17 17:28:44 +03002316 mdtp_activated(&is_mdtp_activated);
2317 if(is_mdtp_activated){
2318 dprintf(CRITICAL, "fastboot boot command is not available.\n");
lijuang2008ff22016-03-07 17:56:27 +08002319 goto boot_failed;
Shay Nachmani062aa3f2015-05-17 17:28:44 +03002320 }
Amit Blay4aa292f2015-04-28 21:55:59 +03002321#endif /* MDTP_SUPPORT */
2322
Sridhar Parasuram361e4cd2015-09-24 11:19:19 -07002323#if VERIFIED_BOOT
Parth Dixitbc9b6492015-10-18 00:41:38 +05302324#if !VBOOT_MOTA
Sridhar Parasuram361e4cd2015-09-24 11:19:19 -07002325 // send root of trust
2326 if(!send_rot_command((uint32_t)device.is_unlocked))
2327 ASSERT(0);
2328#endif
Parth Dixitbc9b6492015-10-18 00:41:38 +05302329#endif
Channagoud Kadabia22144f2013-03-20 11:49:01 -07002330 /*
Matthew Qinbb7923d2015-02-09 10:56:09 +08002331 * Check if the kernel image is a gzip package. If yes, need to decompress it.
2332 * If not, continue booting.
2333 */
2334 if (is_gzip_package((unsigned char *)(data + page_size), hdr->kernel_size))
2335 {
2336 out_addr = (unsigned char *)target_get_scratch_address();
2337 out_addr = (unsigned char *)(out_addr + image_actual + page_size);
2338 out_avai_len = target_get_max_flash_size() - image_actual - page_size;
Matthew Qin0b15b322015-05-19 05:20:54 -04002339 dprintf(INFO, "decompressing kernel image: start\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08002340 ret = decompress((unsigned char *)(ptr + page_size),
2341 hdr->kernel_size, out_addr, out_avai_len,
2342 &dtb_offset, &out_len);
2343 if (ret)
2344 {
Matthew Qin0b15b322015-05-19 05:20:54 -04002345 dprintf(CRITICAL, "decompressing image failed!!!\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08002346 ASSERT(0);
2347 }
2348
Matthew Qin0b15b322015-05-19 05:20:54 -04002349 dprintf(INFO, "decompressing kernel image: done\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08002350 kptr = (struct kernel64_hdr *)out_addr;
2351 kernel_start_addr = out_addr;
2352 kernel_size = out_len;
2353 } else {
2354 kptr = (struct kernel64_hdr*)((char *)data + page_size);
2355 kernel_start_addr = (unsigned char *)((char *)data + page_size);
2356 kernel_size = hdr->kernel_size;
2357 }
2358
2359 /*
Channagoud Kadabia22144f2013-03-20 11:49:01 -07002360 * Update the kernel/ramdisk/tags address if the boot image header
2361 * has default values, these default values come from mkbootimg when
2362 * the boot image is flashed using fastboot flash:raw
2363 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08002364 update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr));
Dima Zavin3cadfff2013-03-21 14:30:48 -07002365
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002366 /* Get virtual addresses since the hdr saves physical addresses. */
2367 hdr->kernel_addr = VA(hdr->kernel_addr);
2368 hdr->ramdisk_addr = VA(hdr->ramdisk_addr);
2369 hdr->tags_addr = VA(hdr->tags_addr);
Brian Swetland9c4c0752009-01-25 16:23:50 -08002370
Matthew Qinbb7923d2015-02-09 10:56:09 +08002371 kernel_size = ROUND_TO_PAGE(kernel_size, page_mask);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002372 /* Check if the addresses in the header are valid. */
Matthew Qinbb7923d2015-02-09 10:56:09 +08002373 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_size) ||
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002374 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
2375 {
2376 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
lijuang2008ff22016-03-07 17:56:27 +08002377 goto boot_failed;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002378 }
2379
Amol Jadicb524072012-08-09 16:40:18 -07002380#if DEVICE_TREE
Matthew Qin271927e2015-03-31 22:07:07 -04002381 scratch_offset = image_actual + page_size + out_len;
Amol Jadicb524072012-08-09 16:40:18 -07002382 /* find correct dtb and copy it to right location */
Matthew Qin271927e2015-03-31 22:07:07 -04002383 ret = copy_dtb(data, scratch_offset);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002384
2385 dtb_copied = !ret ? 1 : 0;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002386#else
2387 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
2388 {
2389 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
lijuang2008ff22016-03-07 17:56:27 +08002390 goto boot_failed;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002391 }
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002392#endif
2393
2394 /* Load ramdisk & kernel */
2395 memmove((void*) hdr->ramdisk_addr, ptr + page_size + kernel_actual, hdr->ramdisk_size);
Matthew Qinbb7923d2015-02-09 10:56:09 +08002396 memmove((void*) hdr->kernel_addr, (char*) (kernel_start_addr), kernel_size);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002397
2398#if DEVICE_TREE
Matthew Qinbb7923d2015-02-09 10:56:09 +08002399 if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual))
2400 {
2401 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
lijuang2008ff22016-03-07 17:56:27 +08002402 goto boot_failed;
Matthew Qinbb7923d2015-02-09 10:56:09 +08002403 }
2404
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002405 /*
2406 * If dtb is not found look for appended DTB in the kernel.
2407 * If appended dev tree is found, update the atags with
2408 * memory address to the DTB appended location on RAM.
2409 * Else update with the atags address in the kernel header
2410 */
2411 if (!dtb_copied) {
2412 void *dtb;
Matthew Qinbb7923d2015-02-09 10:56:09 +08002413 dtb = dev_tree_appended((void*)(ptr + page_size),
2414 hdr->kernel_size, dtb_offset,
Dima Zavine63e5572013-05-03 12:23:06 -07002415 (void *)hdr->tags_addr);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002416 if (!dtb) {
2417 fastboot_fail("dtb not found");
lijuang2008ff22016-03-07 17:56:27 +08002418 goto boot_failed;
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002419 }
Amol Jadicb524072012-08-09 16:40:18 -07002420 }
2421#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08002422
2423 fastboot_okay("");
Amol Jadi7c4316c2013-10-07 14:19:26 -07002424 fastboot_stop();
Brian Swetland9c4c0752009-01-25 16:23:50 -08002425
Dima Zavin77e41f32013-03-06 16:10:43 -08002426 boot_linux((void*) hdr->kernel_addr, (void*) hdr->tags_addr,
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002427 (const char*) hdr->cmdline, board_machtype(),
2428 (void*) hdr->ramdisk_addr, hdr->ramdisk_size);
lijuang2008ff22016-03-07 17:56:27 +08002429
2430 /* fastboot already stop, it's no need to show fastboot menu */
2431 return;
2432boot_failed:
2433#if FBCON_DISPLAY_MSG
2434 /* revert to fastboot menu if boot failed */
2435 display_fastboot_menu();
2436#endif
2437 return;
Brian Swetland9c4c0752009-01-25 16:23:50 -08002438}
2439
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002440void cmd_erase_nand(const char *arg, void *data, unsigned sz)
Dima Zavin214cc642009-01-26 11:16:21 -08002441{
2442 struct ptentry *ptn;
2443 struct ptable *ptable;
2444
2445 ptable = flash_get_ptable();
2446 if (ptable == NULL) {
2447 fastboot_fail("partition table doesn't exist");
2448 return;
2449 }
2450
2451 ptn = ptable_find(ptable, arg);
2452 if (ptn == NULL) {
2453 fastboot_fail("unknown partition name");
2454 return;
2455 }
2456
2457 if (flash_erase(ptn)) {
2458 fastboot_fail("failed to erase partition");
2459 return;
2460 }
2461 fastboot_okay("");
2462}
2463
Bikas Gurungd48bd242010-09-04 19:54:32 -07002464
2465void cmd_erase_mmc(const char *arg, void *data, unsigned sz)
2466{
2467 unsigned long long ptn = 0;
Oliver Wangcee448d2013-10-22 18:40:13 +08002468 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07002469 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002470 uint8_t lun = 0;
Bikas Gurungd48bd242010-09-04 19:54:32 -07002471
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002472#if VERIFIED_BOOT
2473 if(!strcmp(arg, KEYSTORE_PTN_NAME))
2474 {
2475 if(!device.is_unlocked)
2476 {
2477 fastboot_fail("unlock device to erase keystore");
2478 return;
2479 }
2480 }
2481#endif
2482
Kinson Chikf1a43512011-07-14 11:28:39 -07002483 index = partition_get_index(arg);
2484 ptn = partition_get_offset(index);
Oliver Wangcee448d2013-10-22 18:40:13 +08002485 size = partition_get_size(index);
Neeti Desaica8c9602011-10-06 11:40:00 -07002486
Kinson Chikf1a43512011-07-14 11:28:39 -07002487 if(ptn == 0) {
Neeti Desaica8c9602011-10-06 11:40:00 -07002488 fastboot_fail("Partition table doesn't exist\n");
Bikas Gurungd48bd242010-09-04 19:54:32 -07002489 return;
2490 }
Kun Liang2f1601a2013-08-12 16:29:54 +08002491
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002492 lun = partition_get_lun(index);
2493 mmc_set_lun(lun);
2494
Sridhar Parasuramd7957122015-02-27 11:33:40 -08002495 if (platform_boot_dev_isemmc())
2496 {
2497 if (mmc_erase_card(ptn, size)) {
2498 fastboot_fail("failed to erase partition\n");
2499 return;
2500 }
2501 } else {
2502 BUF_DMA_ALIGN(out, DEFAULT_ERASE_SIZE);
2503 size = partition_get_size(index);
2504 if (size > DEFAULT_ERASE_SIZE)
2505 size = DEFAULT_ERASE_SIZE;
Kun Liang2f1601a2013-08-12 16:29:54 +08002506
Sridhar Parasuramd7957122015-02-27 11:33:40 -08002507 /* Simple inefficient version of erase. Just writing
2508 0 in first several blocks */
2509 if (mmc_write(ptn , size, (unsigned int *)out)) {
2510 fastboot_fail("failed to erase partition");
2511 return;
2512 }
Bikas Gurungd48bd242010-09-04 19:54:32 -07002513 }
Sridhar Parasuram32b30662015-07-10 13:33:22 -07002514#if VERIFIED_BOOT
Parth Dixitbc9b6492015-10-18 00:41:38 +05302515#if !VBOOT_MOTA
Sridhar Parasuram32b30662015-07-10 13:33:22 -07002516 if(!(strncmp(arg, "userdata", 8)))
2517 if(send_delete_keys_to_tz())
2518 ASSERT(0);
2519#endif
Parth Dixitbc9b6492015-10-18 00:41:38 +05302520#endif
Bikas Gurungd48bd242010-09-04 19:54:32 -07002521 fastboot_okay("");
2522}
2523
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002524void cmd_erase(const char *arg, void *data, unsigned sz)
2525{
Channagoud Kadabice3cf422015-04-17 16:02:30 -07002526#if VERIFIED_BOOT
Channagoud Kadabi35297672015-06-13 11:09:49 -07002527 if (target_build_variant_user())
Channagoud Kadabice3cf422015-04-17 16:02:30 -07002528 {
Sridhar Parasuramc32d07d2015-07-12 10:57:48 -07002529 if(!device.is_unlocked)
Channagoud Kadabice3cf422015-04-17 16:02:30 -07002530 {
Channagoud Kadabi35297672015-06-13 11:09:49 -07002531 fastboot_fail("device is locked. Cannot erase");
Channagoud Kadabice3cf422015-04-17 16:02:30 -07002532 return;
2533 }
2534 }
2535#endif
2536
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002537 if(target_is_emmc_boot())
2538 cmd_erase_mmc(arg, data, sz);
2539 else
2540 cmd_erase_nand(arg, data, sz);
2541}
Bikas Gurungd48bd242010-09-04 19:54:32 -07002542
Channagoud Kadabiad259832015-05-29 11:14:17 -07002543static uint32_t aboot_get_secret_key()
2544{
2545 /* 0 is invalid secret key, update this implementation to return
2546 * device specific unique secret key
2547 */
2548 return 0;
2549}
2550
Ajay Dudani5c761132011-04-07 20:19:04 -07002551void cmd_flash_mmc_img(const char *arg, void *data, unsigned sz)
Shashank Mittal23b8f422010-04-16 19:27:21 -07002552{
2553 unsigned long long ptn = 0;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07002554 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07002555 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002556 char *token = NULL;
2557 char *pname = NULL;
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07002558 char *sp;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002559 uint8_t lun = 0;
2560 bool lun_set = false;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07002561
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07002562 token = strtok_r((char *)arg, ":", &sp);
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002563 pname = token;
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07002564 token = strtok_r(NULL, ":", &sp);
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002565 if(token)
2566 {
2567 lun = atoi(token);
2568 mmc_set_lun(lun);
2569 lun_set = true;
2570 }
2571
Mao Jinlong226f33a2014-07-04 17:24:10 +08002572 if (pname)
Greg Grisco6e754772011-06-23 12:19:39 -07002573 {
Channagoud Kadabiad259832015-05-29 11:14:17 -07002574 if (!strncmp(pname, "frp-unlock", strlen("frp-unlock")))
2575 {
2576 if (!aboot_frp_unlock(pname, data, sz))
2577 {
2578 fastboot_info("FRP unlock successful");
2579 fastboot_okay("");
2580 }
2581 else
2582 fastboot_fail("Secret key is invalid, please update the bootloader with secret key");
2583
2584 return;
2585 }
2586
Mao Jinlong226f33a2014-07-04 17:24:10 +08002587 if (!strcmp(pname, "partition"))
2588 {
2589 dprintf(INFO, "Attempt to write partition image.\n");
2590 if (write_partition(sz, (unsigned char *) data)) {
2591 fastboot_fail("failed to write partition");
Greg Grisco6e754772011-06-23 12:19:39 -07002592 return;
2593 }
2594 }
Mao Jinlong226f33a2014-07-04 17:24:10 +08002595 else
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002596 {
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002597#if VERIFIED_BOOT
2598 if(!strcmp(pname, KEYSTORE_PTN_NAME))
2599 {
2600 if(!device.is_unlocked)
2601 {
2602 fastboot_fail("unlock device to flash keystore");
2603 return;
2604 }
Gaurav Nebhwanic7313cc2015-12-15 22:25:04 +05302605 if(!boot_verify_validate_keystore((unsigned char *)data,sz))
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002606 {
2607 fastboot_fail("image is not a keystore file");
2608 return;
2609 }
2610 }
2611#endif
Mao Jinlong226f33a2014-07-04 17:24:10 +08002612 index = partition_get_index(pname);
2613 ptn = partition_get_offset(index);
2614 if(ptn == 0) {
2615 fastboot_fail("partition table doesn't exist");
2616 return;
2617 }
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002618
Mao Jinlong226f33a2014-07-04 17:24:10 +08002619 if (!strcmp(pname, "boot") || !strcmp(pname, "recovery")) {
2620 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
2621 fastboot_fail("image is not a boot image");
2622 return;
2623 }
2624 }
2625
2626 if(!lun_set)
2627 {
2628 lun = partition_get_lun(index);
2629 mmc_set_lun(lun);
2630 }
2631
2632 size = partition_get_size(index);
Gaurav Nebhwani4d2389c2016-03-17 21:10:05 +05302633 if (ROUND_TO_PAGE(sz, mmc_blocksize_mask) > size) {
Mao Jinlong226f33a2014-07-04 17:24:10 +08002634 fastboot_fail("size too large");
2635 return;
2636 }
2637 else if (mmc_write(ptn , sz, (unsigned int *)data)) {
2638 fastboot_fail("flash write failure");
2639 return;
2640 }
Greg Grisco6e754772011-06-23 12:19:39 -07002641 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07002642 }
2643 fastboot_okay("");
2644 return;
2645}
2646
Ajay Dudanide984792015-03-02 09:57:41 -08002647void cmd_flash_meta_img(const char *arg, void *data, unsigned sz)
2648{
2649 int i, images;
2650 meta_header_t *meta_header;
2651 img_header_entry_t *img_header_entry;
2652
2653 meta_header = (meta_header_t*) data;
2654 img_header_entry = (img_header_entry_t*) (data+sizeof(meta_header_t));
2655
2656 images = meta_header->img_hdr_sz / sizeof(img_header_entry_t);
2657
2658 for (i=0; i<images; i++) {
2659
2660 if((img_header_entry[i].ptn_name == NULL) ||
2661 (img_header_entry[i].start_offset == 0) ||
2662 (img_header_entry[i].size == 0))
2663 break;
2664
2665 cmd_flash_mmc_img(img_header_entry[i].ptn_name,
2666 (void *) data + img_header_entry[i].start_offset,
2667 img_header_entry[i].size);
2668 }
2669
Channagoud Kadabiaafbbfc2015-03-04 17:52:56 -08002670 if (!strncmp(arg, "bootloader", strlen("bootloader")))
2671 {
2672 strlcpy(device.bootloader_version, TARGET(BOARD), MAX_VERSION_LEN);
2673 strlcat(device.bootloader_version, "-", MAX_VERSION_LEN);
2674 strlcat(device.bootloader_version, meta_header->img_version, MAX_VERSION_LEN);
2675 }
2676 else
2677 {
2678 strlcpy(device.radio_version, TARGET(BOARD), MAX_VERSION_LEN);
2679 strlcat(device.radio_version, "-", MAX_VERSION_LEN);
2680 strlcat(device.radio_version, meta_header->img_version, MAX_VERSION_LEN);
2681 }
2682
2683 write_device_info(&device);
Ajay Dudanide984792015-03-02 09:57:41 -08002684 fastboot_okay("");
2685 return;
2686}
2687
Ajay Dudani5c761132011-04-07 20:19:04 -07002688void cmd_flash_mmc_sparse_img(const char *arg, void *data, unsigned sz)
2689{
2690 unsigned int chunk;
wufeng.jiang813dc352015-06-02 23:02:46 -04002691 uint64_t chunk_data_sz;
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002692 uint32_t *fill_buf = NULL;
2693 uint32_t fill_val;
Ajay Dudani5c761132011-04-07 20:19:04 -07002694 sparse_header_t *sparse_header;
2695 chunk_header_t *chunk_header;
Ajay Dudaniab18f022011-05-12 14:39:22 -07002696 uint32_t total_blocks = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07002697 unsigned long long ptn = 0;
Channagoud Kadabi65b91002011-10-11 17:34:33 +05302698 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07002699 int index = INVALID_PTN;
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05302700 uint32_t i;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002701 uint8_t lun = 0;
vijay kumar800255e2015-04-24 20:26:19 +05302702 /*End of the sparse image address*/
Parth Dixit64b1a482016-03-07 16:31:26 +05302703 uintptr_t data_end = (uintptr_t)data + sz;
Ajay Dudani5c761132011-04-07 20:19:04 -07002704
Kinson Chikf1a43512011-07-14 11:28:39 -07002705 index = partition_get_index(arg);
2706 ptn = partition_get_offset(index);
2707 if(ptn == 0) {
Ajay Dudani5c761132011-04-07 20:19:04 -07002708 fastboot_fail("partition table doesn't exist");
2709 return;
2710 }
2711
Channagoud Kadabi65b91002011-10-11 17:34:33 +05302712 size = partition_get_size(index);
Channagoud Kadabi65b91002011-10-11 17:34:33 +05302713
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002714 lun = partition_get_lun(index);
2715 mmc_set_lun(lun);
2716
vijay kumar800255e2015-04-24 20:26:19 +05302717 if (sz < sizeof(sparse_header_t)) {
2718 fastboot_fail("size too low");
2719 return;
2720 }
2721
Ajay Dudani5c761132011-04-07 20:19:04 -07002722 /* Read and skip over sparse image header */
2723 sparse_header = (sparse_header_t *) data;
vijay kumar800255e2015-04-24 20:26:19 +05302724
vijay kumar1321f342015-03-27 12:13:42 +05302725 if (((uint64_t)sparse_header->total_blks * (uint64_t)sparse_header->blk_sz) > size) {
Ajay Dudani876b3282012-12-21 14:12:17 -08002726 fastboot_fail("size too large");
2727 return;
2728 }
2729
vijay kumarde4fcf62015-04-23 13:05:49 +05302730 data += sizeof(sparse_header_t);
vijay kumar800255e2015-04-24 20:26:19 +05302731
Parth Dixit64b1a482016-03-07 16:31:26 +05302732 if (data_end < (uintptr_t)data) {
vijay kumar800255e2015-04-24 20:26:19 +05302733 fastboot_fail("buffer overreads occured due to invalid sparse header");
2734 return;
2735 }
2736
vijay kumarde4fcf62015-04-23 13:05:49 +05302737 if(sparse_header->file_hdr_sz != sizeof(sparse_header_t))
Ajay Dudani5c761132011-04-07 20:19:04 -07002738 {
vijay kumarde4fcf62015-04-23 13:05:49 +05302739 fastboot_fail("sparse header size mismatch");
2740 return;
Ajay Dudani5c761132011-04-07 20:19:04 -07002741 }
2742
Ajay Dudanib06c05f2011-05-12 14:46:10 -07002743 dprintf (SPEW, "=== Sparse Image Header ===\n");
2744 dprintf (SPEW, "magic: 0x%x\n", sparse_header->magic);
2745 dprintf (SPEW, "major_version: 0x%x\n", sparse_header->major_version);
2746 dprintf (SPEW, "minor_version: 0x%x\n", sparse_header->minor_version);
2747 dprintf (SPEW, "file_hdr_sz: %d\n", sparse_header->file_hdr_sz);
2748 dprintf (SPEW, "chunk_hdr_sz: %d\n", sparse_header->chunk_hdr_sz);
2749 dprintf (SPEW, "blk_sz: %d\n", sparse_header->blk_sz);
2750 dprintf (SPEW, "total_blks: %d\n", sparse_header->total_blks);
2751 dprintf (SPEW, "total_chunks: %d\n", sparse_header->total_chunks);
Ajay Dudani5c761132011-04-07 20:19:04 -07002752
2753 /* Start processing chunks */
2754 for (chunk=0; chunk<sparse_header->total_chunks; chunk++)
2755 {
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302756 /* Make sure the total image size does not exceed the partition size */
2757 if(((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz) >= size) {
2758 fastboot_fail("size too large");
2759 return;
2760 }
Ajay Dudani5c761132011-04-07 20:19:04 -07002761 /* Read and skip over chunk header */
2762 chunk_header = (chunk_header_t *) data;
2763 data += sizeof(chunk_header_t);
2764
Parth Dixit64b1a482016-03-07 16:31:26 +05302765 if (data_end < (uintptr_t)data) {
vijay kumar800255e2015-04-24 20:26:19 +05302766 fastboot_fail("buffer overreads occured due to invalid sparse header");
2767 return;
2768 }
2769
Ajay Dudani5c761132011-04-07 20:19:04 -07002770 dprintf (SPEW, "=== Chunk Header ===\n");
2771 dprintf (SPEW, "chunk_type: 0x%x\n", chunk_header->chunk_type);
2772 dprintf (SPEW, "chunk_data_sz: 0x%x\n", chunk_header->chunk_sz);
2773 dprintf (SPEW, "total_size: 0x%x\n", chunk_header->total_sz);
2774
vijay kumar800255e2015-04-24 20:26:19 +05302775 if(sparse_header->chunk_hdr_sz != sizeof(chunk_header_t))
Ajay Dudani5c761132011-04-07 20:19:04 -07002776 {
vijay kumar800255e2015-04-24 20:26:19 +05302777 fastboot_fail("chunk header size mismatch");
2778 return;
Ajay Dudani5c761132011-04-07 20:19:04 -07002779 }
2780
wufeng.jiang813dc352015-06-02 23:02:46 -04002781 if (!sparse_header->blk_sz ){
2782 fastboot_fail("Invalid block size\n");
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302783 return;
2784 }
2785
wufeng.jiang813dc352015-06-02 23:02:46 -04002786 chunk_data_sz = (uint64_t)sparse_header->blk_sz * chunk_header->chunk_sz;
2787
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302788 /* Make sure that the chunk size calculated from sparse image does not
2789 * exceed partition size
2790 */
2791 if ((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz + chunk_data_sz > size)
2792 {
2793 fastboot_fail("Chunk data size exceeds partition size");
2794 return;
2795 }
2796
Ajay Dudani5c761132011-04-07 20:19:04 -07002797 switch (chunk_header->chunk_type)
2798 {
2799 case CHUNK_TYPE_RAW:
wufeng.jiang813dc352015-06-02 23:02:46 -04002800 if((uint64_t)chunk_header->total_sz != ((uint64_t)sparse_header->chunk_hdr_sz +
Ajay Dudani5c761132011-04-07 20:19:04 -07002801 chunk_data_sz))
2802 {
2803 fastboot_fail("Bogus chunk size for chunk type Raw");
2804 return;
2805 }
2806
Parth Dixit64b1a482016-03-07 16:31:26 +05302807 if (data_end < (uintptr_t)data + chunk_data_sz) {
vijay kumar800255e2015-04-24 20:26:19 +05302808 fastboot_fail("buffer overreads occured due to invalid sparse header");
2809 return;
2810 }
2811
wufeng.jiang813dc352015-06-02 23:02:46 -04002812 /* chunk_header->total_sz is uint32,So chunk_data_sz is now less than 2^32
2813 otherwise it will return in the line above
2814 */
Ajay Dudaniab18f022011-05-12 14:39:22 -07002815 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
wufeng.jiang813dc352015-06-02 23:02:46 -04002816 (uint32_t)chunk_data_sz,
Ajay Dudaniab18f022011-05-12 14:39:22 -07002817 (unsigned int*)data))
Ajay Dudani5c761132011-04-07 20:19:04 -07002818 {
2819 fastboot_fail("flash write failure");
2820 return;
2821 }
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302822 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
2823 fastboot_fail("Bogus size for RAW chunk type");
2824 return;
2825 }
Ajay Dudani5c761132011-04-07 20:19:04 -07002826 total_blocks += chunk_header->chunk_sz;
wufeng.jiang813dc352015-06-02 23:02:46 -04002827 data += (uint32_t)chunk_data_sz;
Ajay Dudani5c761132011-04-07 20:19:04 -07002828 break;
2829
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002830 case CHUNK_TYPE_FILL:
2831 if(chunk_header->total_sz != (sparse_header->chunk_hdr_sz +
2832 sizeof(uint32_t)))
2833 {
2834 fastboot_fail("Bogus chunk size for chunk type FILL");
2835 return;
2836 }
2837
2838 fill_buf = (uint32_t *)memalign(CACHE_LINE, ROUNDUP(sparse_header->blk_sz, CACHE_LINE));
2839 if (!fill_buf)
2840 {
2841 fastboot_fail("Malloc failed for: CHUNK_TYPE_FILL");
2842 return;
2843 }
2844
Parth Dixit64b1a482016-03-07 16:31:26 +05302845 if (data_end < (uintptr_t)data + sizeof(uint32_t)) {
vijay kumar800255e2015-04-24 20:26:19 +05302846 fastboot_fail("buffer overreads occured due to invalid sparse header");
Gaurav Nebhwani163cbf82016-03-17 21:21:27 +05302847 free(fill_buf);
vijay kumar800255e2015-04-24 20:26:19 +05302848 return;
2849 }
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002850 fill_val = *(uint32_t *)data;
2851 data = (char *) data + sizeof(uint32_t);
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002852
2853 for (i = 0; i < (sparse_header->blk_sz / sizeof(fill_val)); i++)
2854 {
2855 fill_buf[i] = fill_val;
2856 }
2857
Gaurav Nebhwanie94cbe12016-03-17 21:16:34 +05302858 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz))
2859 {
2860 fastboot_fail("bogus size for chunk FILL type");
2861 free(fill_buf);
2862 return;
2863 }
2864
wufeng.jiang813dc352015-06-02 23:02:46 -04002865 for (i = 0; i < chunk_header->chunk_sz; i++)
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002866 {
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302867 /* Make sure that the data written to partition does not exceed partition size */
2868 if ((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz + sparse_header->blk_sz > size)
2869 {
2870 fastboot_fail("Chunk data size for fill type exceeds partition size");
Gaurav Nebhwani163cbf82016-03-17 21:21:27 +05302871 free(fill_buf);
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302872 return;
2873 }
2874
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002875 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
2876 sparse_header->blk_sz,
2877 fill_buf))
2878 {
2879 fastboot_fail("flash write failure");
2880 free(fill_buf);
2881 return;
2882 }
2883
2884 total_blocks++;
2885 }
2886
2887 free(fill_buf);
2888 break;
2889
Ajay Dudani5c761132011-04-07 20:19:04 -07002890 case CHUNK_TYPE_DONT_CARE:
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302891 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
2892 fastboot_fail("bogus size for chunk DONT CARE type");
2893 return;
2894 }
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07002895 total_blocks += chunk_header->chunk_sz;
2896 break;
2897
Ajay Dudani5c761132011-04-07 20:19:04 -07002898 case CHUNK_TYPE_CRC:
2899 if(chunk_header->total_sz != sparse_header->chunk_hdr_sz)
2900 {
wufeng.jiang813dc352015-06-02 23:02:46 -04002901 fastboot_fail("Bogus chunk size for chunk type CRC");
Ajay Dudani5c761132011-04-07 20:19:04 -07002902 return;
2903 }
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302904 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
2905 fastboot_fail("bogus size for chunk CRC type");
2906 return;
2907 }
Ajay Dudani5c761132011-04-07 20:19:04 -07002908 total_blocks += chunk_header->chunk_sz;
Parth Dixit64b1a482016-03-07 16:31:26 +05302909 if ((uintptr_t)data > UINT_MAX - chunk_data_sz) {
vijay kumar800255e2015-04-24 20:26:19 +05302910 fastboot_fail("integer overflow occured");
2911 return;
2912 }
wufeng.jiang813dc352015-06-02 23:02:46 -04002913 data += (uint32_t)chunk_data_sz;
Parth Dixit64b1a482016-03-07 16:31:26 +05302914 if (data_end < (uintptr_t)data) {
vijay kumar800255e2015-04-24 20:26:19 +05302915 fastboot_fail("buffer overreads occured due to invalid sparse header");
2916 return;
2917 }
Ajay Dudani5c761132011-04-07 20:19:04 -07002918 break;
2919
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07002920 default:
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002921 dprintf(CRITICAL, "Unkown chunk type: %x\n",chunk_header->chunk_type);
Ajay Dudani5c761132011-04-07 20:19:04 -07002922 fastboot_fail("Unknown chunk type");
2923 return;
2924 }
2925 }
2926
Ajay Dudani0c6927b2011-05-18 11:12:16 -07002927 dprintf(INFO, "Wrote %d blocks, expected to write %d blocks\n",
2928 total_blocks, sparse_header->total_blks);
2929
2930 if(total_blocks != sparse_header->total_blks)
2931 {
2932 fastboot_fail("sparse image write failure");
2933 }
Ajay Dudani5c761132011-04-07 20:19:04 -07002934
2935 fastboot_okay("");
2936 return;
2937}
2938
2939void cmd_flash_mmc(const char *arg, void *data, unsigned sz)
2940{
2941 sparse_header_t *sparse_header;
Ajay Dudanide984792015-03-02 09:57:41 -08002942 meta_header_t *meta_header;
Ajay Dudani5c761132011-04-07 20:19:04 -07002943
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07002944#ifdef SSD_ENABLE
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -08002945 /* 8 Byte Magic + 2048 Byte xml + Encrypted Data */
2946 unsigned int *magic_number = (unsigned int *) data;
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002947 int ret=0;
2948 uint32 major_version=0;
2949 uint32 minor_version=0;
2950
2951 ret = scm_svc_version(&major_version,&minor_version);
2952 if(!ret)
2953 {
2954 if(major_version >= 2)
2955 {
Amir Samuelovbb65ce02013-05-05 12:20:18 +03002956 if( !strcmp(arg, "ssd") || !strcmp(arg, "tqs") )
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002957 {
2958 ret = encrypt_scm((uint32 **) &data, &sz);
2959 if (ret != 0) {
2960 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
2961 return;
2962 }
2963
Amir Samuelovbb65ce02013-05-05 12:20:18 +03002964 /* Protect only for SSD */
2965 if (!strcmp(arg, "ssd")) {
2966 ret = scm_protect_keystore((uint32 *) data, sz);
2967 if (ret != 0) {
2968 dprintf(CRITICAL, "ERROR: scm_protect_keystore Failed\n");
2969 return;
2970 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002971 }
2972 }
2973 else
2974 {
2975 ret = decrypt_scm_v2((uint32 **) &data, &sz);
2976 if(ret != 0)
2977 {
2978 dprintf(CRITICAL,"ERROR: Decryption Failure\n");
2979 return;
2980 }
2981 }
2982 }
2983 else
2984 {
2985 if (magic_number[0] == DECRYPT_MAGIC_0 &&
2986 magic_number[1] == DECRYPT_MAGIC_1)
2987 {
2988 ret = decrypt_scm((uint32 **) &data, &sz);
2989 if (ret != 0) {
2990 dprintf(CRITICAL, "ERROR: Invalid secure image\n");
2991 return;
2992 }
2993 }
2994 else if (magic_number[0] == ENCRYPT_MAGIC_0 &&
2995 magic_number[1] == ENCRYPT_MAGIC_1)
2996 {
2997 ret = encrypt_scm((uint32 **) &data, &sz);
2998 if (ret != 0) {
2999 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
3000 return;
3001 }
3002 }
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07003003 }
3004 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08003005 else
Neeti Desai127b9e02012-03-20 16:11:23 -07003006 {
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08003007 dprintf(CRITICAL,"INVALID SVC Version\n");
3008 return;
Neeti Desai127b9e02012-03-20 16:11:23 -07003009 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08003010#endif /* SSD_ENABLE */
Neeti Desai127b9e02012-03-20 16:11:23 -07003011
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003012#if VERIFIED_BOOT
Channagoud Kadabi35297672015-06-13 11:09:49 -07003013 if (target_build_variant_user())
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003014 {
lijuang511a2b52015-08-14 20:50:51 +08003015 /* if device is locked:
3016 * common partition will not allow to be flashed
3017 * critical partition will allow to flash image.
3018 */
3019 if(!device.is_unlocked && !critical_flash_allowed(arg)) {
3020 fastboot_fail("Partition flashing is not allowed");
3021 return;
3022 }
Parth Dixitddbc7352015-10-18 03:13:31 +05303023#if !VBOOT_MOTA
lijuang511a2b52015-08-14 20:50:51 +08003024 /* if device critical is locked:
3025 * common partition will allow to be flashed
3026 * critical partition will not allow to flash image.
3027 */
3028 if(!device.is_unlock_critical && critical_flash_allowed(arg)) {
3029 fastboot_fail("Critical partition flashing is not allowed");
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003030 return;
3031 }
Parth Dixitddbc7352015-10-18 03:13:31 +05303032#endif
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003033 }
3034#endif
3035
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07003036 sparse_header = (sparse_header_t *) data;
Ajay Dudanide984792015-03-02 09:57:41 -08003037 meta_header = (meta_header_t *) data;
3038 if (sparse_header->magic == SPARSE_HEADER_MAGIC)
Ajay Dudani5c761132011-04-07 20:19:04 -07003039 cmd_flash_mmc_sparse_img(arg, data, sz);
Ajay Dudanide984792015-03-02 09:57:41 -08003040 else if (meta_header->magic == META_HEADER_MAGIC)
3041 cmd_flash_meta_img(arg, data, sz);
3042 else
3043 cmd_flash_mmc_img(arg, data, sz);
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07003044
3045#if VERIFIED_BOOT
Parth Dixitddbc7352015-10-18 03:13:31 +05303046#if !VBOOT_MOTA
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07003047 if((!strncmp(arg, "system", 6)) && !device.verity_mode)
3048 {
3049 // reset dm_verity mode to enforcing
3050 device.verity_mode = 1;
3051 write_device_info(&device);
3052 }
3053#endif
Parth Dixitddbc7352015-10-18 03:13:31 +05303054#endif
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07003055
Ajay Dudani5c761132011-04-07 20:19:04 -07003056 return;
3057}
3058
Tanya Brokhman84eaaf62015-02-24 21:57:06 +02003059void cmd_updatevol(const char *vol_name, void *data, unsigned sz)
3060{
3061 struct ptentry *sys_ptn;
3062 struct ptable *ptable;
3063
3064 ptable = flash_get_ptable();
3065 if (ptable == NULL) {
3066 fastboot_fail("partition table doesn't exist");
3067 return;
3068 }
3069
3070 sys_ptn = ptable_find(ptable, "system");
3071 if (sys_ptn == NULL) {
3072 fastboot_fail("system partition not found");
3073 return;
3074 }
3075
3076 sz = ROUND_TO_PAGE(sz, page_mask);
3077 if (update_ubi_vol(sys_ptn, vol_name, data, sz))
3078 fastboot_fail("update_ubi_vol failed");
3079 else
3080 fastboot_okay("");
3081}
3082
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003083void cmd_flash_nand(const char *arg, void *data, unsigned sz)
Dima Zavin214cc642009-01-26 11:16:21 -08003084{
3085 struct ptentry *ptn;
3086 struct ptable *ptable;
3087 unsigned extra = 0;
3088
3089 ptable = flash_get_ptable();
3090 if (ptable == NULL) {
3091 fastboot_fail("partition table doesn't exist");
3092 return;
3093 }
3094
3095 ptn = ptable_find(ptable, arg);
3096 if (ptn == NULL) {
Tanya Brokhman84eaaf62015-02-24 21:57:06 +02003097 dprintf(INFO, "unknown partition name (%s). Trying updatevol\n",
3098 arg);
3099 cmd_updatevol(arg, data, sz);
Dima Zavin214cc642009-01-26 11:16:21 -08003100 return;
3101 }
3102
3103 if (!strcmp(ptn->name, "boot") || !strcmp(ptn->name, "recovery")) {
3104 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
3105 fastboot_fail("image is not a boot image");
3106 return;
3107 }
3108 }
3109
Amol Jadi5c61a952012-05-04 17:05:35 -07003110 if (!strcmp(ptn->name, "system")
Deepa Dinamani13e32c42012-03-12 14:34:17 -07003111 || !strcmp(ptn->name, "userdata")
3112 || !strcmp(ptn->name, "persist")
Sundarajan Srinivasanb063a852013-11-19 14:02:27 -08003113 || !strcmp(ptn->name, "recoveryfs")
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -08003114 || !strcmp(ptn->name, "modem"))
Tanya Brokhman1c94f1a2015-02-15 09:05:03 +02003115 extra = 1;
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -08003116 else
Shashank Mittaldcc2e352009-11-19 19:11:16 -08003117 sz = ROUND_TO_PAGE(sz, page_mask);
Dima Zavin214cc642009-01-26 11:16:21 -08003118
3119 dprintf(INFO, "writing %d bytes to '%s'\n", sz, ptn->name);
Tanya Brokhman1c94f1a2015-02-15 09:05:03 +02003120 if (!memcmp((void *)data, UBI_MAGIC, UBI_MAGIC_SIZE)) {
3121 if (flash_ubi_img(ptn, data, sz)) {
3122 fastboot_fail("flash write failure");
3123 return;
3124 }
3125 } else {
3126 if (flash_write(ptn, extra, data, sz)) {
3127 fastboot_fail("flash write failure");
3128 return;
3129 }
Dima Zavin214cc642009-01-26 11:16:21 -08003130 }
3131 dprintf(INFO, "partition '%s' updated\n", ptn->name);
3132 fastboot_okay("");
3133}
3134
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003135void cmd_flash(const char *arg, void *data, unsigned sz)
3136{
3137 if(target_is_emmc_boot())
3138 cmd_flash_mmc(arg, data, sz);
3139 else
3140 cmd_flash_nand(arg, data, sz);
3141}
3142
Dima Zavin214cc642009-01-26 11:16:21 -08003143void cmd_continue(const char *arg, void *data, unsigned sz)
3144{
3145 fastboot_okay("");
Amol Jadi7c4316c2013-10-07 14:19:26 -07003146 fastboot_stop();
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003147
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003148 if (target_is_emmc_boot())
3149 {
lijuanga40d6302015-07-20 20:10:13 +08003150#if FBCON_DISPLAY_MSG
lijuangde34d502016-02-26 16:04:50 +08003151 /* Exit keys' detection thread firstly */
3152 exit_menu_keys_detection();
lijuanga40d6302015-07-20 20:10:13 +08003153#endif
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003154 boot_linux_from_mmc();
3155 }
3156 else
3157 {
3158 boot_linux_from_flash();
3159 }
Dima Zavin214cc642009-01-26 11:16:21 -08003160}
3161
Chandan Uddaraju94183c02010-01-15 15:13:59 -08003162void cmd_reboot(const char *arg, void *data, unsigned sz)
3163{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003164 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08003165 fastboot_okay("");
3166 reboot_device(0);
3167}
3168
3169void cmd_reboot_bootloader(const char *arg, void *data, unsigned sz)
3170{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003171 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08003172 fastboot_okay("");
3173 reboot_device(FASTBOOT_MODE);
3174}
3175
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003176void cmd_oem_enable_charger_screen(const char *arg, void *data, unsigned size)
3177{
3178 dprintf(INFO, "Enabling charger screen check\n");
3179 device.charger_screen_enabled = 1;
3180 write_device_info(&device);
3181 fastboot_okay("");
3182}
3183
3184void cmd_oem_disable_charger_screen(const char *arg, void *data, unsigned size)
3185{
3186 dprintf(INFO, "Disabling charger screen check\n");
3187 device.charger_screen_enabled = 0;
3188 write_device_info(&device);
3189 fastboot_okay("");
3190}
3191
lijuanga25d8bb2015-09-16 13:11:52 +08003192void cmd_oem_off_mode_charger(const char *arg, void *data, unsigned size)
3193{
3194 char *p = NULL;
3195 const char *delim = " \t\n\r";
Parth Dixit407f15b2016-01-07 14:47:37 +05303196 char *sp;
lijuanga25d8bb2015-09-16 13:11:52 +08003197
3198 if (arg) {
Parth Dixit407f15b2016-01-07 14:47:37 +05303199 p = strtok_r((char *)arg, delim, &sp);
lijuanga25d8bb2015-09-16 13:11:52 +08003200 if (p) {
3201 if (!strncmp(p, "0", 1)) {
3202 device.charger_screen_enabled = 0;
3203 } else if (!strncmp(p, "1", 1)) {
3204 device.charger_screen_enabled = 1;
3205 }
3206 }
3207 }
3208
3209 /* update charger_screen_enabled value for getvar
3210 * command
3211 */
3212 snprintf(charger_screen_enabled, MAX_RSP_SIZE, "%d",
3213 device.charger_screen_enabled);
3214
3215 write_device_info(&device);
3216 fastboot_okay("");
3217}
3218
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05303219void cmd_oem_select_display_panel(const char *arg, void *data, unsigned size)
3220{
3221 dprintf(INFO, "Selecting display panel %s\n", arg);
3222 if (arg)
3223 strlcpy(device.display_panel, arg,
3224 sizeof(device.display_panel));
3225 write_device_info(&device);
3226 fastboot_okay("");
3227}
3228
Shashank Mittal162244e2011-08-08 19:01:25 -07003229void cmd_oem_unlock(const char *arg, void *data, unsigned sz)
3230{
lijuang4ece1e72015-08-14 21:02:36 +08003231 set_device_unlock(UNLOCK, TRUE);
vijay kumarc65876c2015-04-24 13:29:16 +05303232}
3233
3234void cmd_oem_unlock_go(const char *arg, void *data, unsigned sz)
3235{
lijuang4ece1e72015-08-14 21:02:36 +08003236 if(!device.is_unlocked) {
vijay kumarc65876c2015-04-24 13:29:16 +05303237 if(!is_allow_unlock) {
3238 fastboot_fail("oem unlock is not allowed");
3239 return;
3240 }
3241
lijuang4ece1e72015-08-14 21:02:36 +08003242 set_device_unlock_value(UNLOCK, TRUE);
vijay kumarc65876c2015-04-24 13:29:16 +05303243
lijuang4ece1e72015-08-14 21:02:36 +08003244 /* wipe data */
vijay kumarc65876c2015-04-24 13:29:16 +05303245 struct recovery_message msg;
lijuang4ece1e72015-08-14 21:02:36 +08003246
vijay kumarc65876c2015-04-24 13:29:16 +05303247 snprintf(msg.recovery, sizeof(msg.recovery), "recovery\n--wipe_data");
3248 write_misc(0, &msg, sizeof(msg));
3249
3250 fastboot_okay("");
3251 reboot_device(RECOVERY_MODE);
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003252 }
3253 fastboot_okay("");
3254}
3255
Channagoud Kadabiad259832015-05-29 11:14:17 -07003256static int aboot_frp_unlock(char *pname, void *data, unsigned sz)
3257{
3258 int ret = 1;
3259 uint32_t secret_key;
3260 char seckey_buffer[MAX_RSP_SIZE];
3261
3262 secret_key = aboot_get_secret_key();
3263 if (secret_key)
3264 {
3265 snprintf((char *) seckey_buffer, MAX_RSP_SIZE, "%x", secret_key);
3266 if (!memcmp((void *)data, (void *)seckey_buffer, sz))
3267 {
3268 is_allow_unlock = true;
3269 write_allow_oem_unlock(is_allow_unlock);
3270 ret = 0;
3271 }
3272 }
3273 return ret;
3274}
3275
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003276void cmd_oem_lock(const char *arg, void *data, unsigned sz)
3277{
lijuang4ece1e72015-08-14 21:02:36 +08003278 set_device_unlock(UNLOCK, FALSE);
Shashank Mittal162244e2011-08-08 19:01:25 -07003279}
3280
Shashank Mittala0032282011-08-26 14:50:11 -07003281void cmd_oem_devinfo(const char *arg, void *data, unsigned sz)
3282{
lijuang511a2b52015-08-14 20:50:51 +08003283 char response[MAX_RSP_SIZE];
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003284 snprintf(response, sizeof(response), "\tDevice tampered: %s", (device.is_tampered ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07003285 fastboot_info(response);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003286 snprintf(response, sizeof(response), "\tDevice unlocked: %s", (device.is_unlocked ? "true" : "false"));
3287 fastboot_info(response);
Parth Dixitddbc7352015-10-18 03:13:31 +05303288#if !VBOOT_MOTA
lijuang511a2b52015-08-14 20:50:51 +08003289 snprintf(response, sizeof(response), "\tDevice critical unlocked: %s", (device.is_unlock_critical ? "true" : "false"));
3290 fastboot_info(response);
Parth Dixitddbc7352015-10-18 03:13:31 +05303291#endif
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003292 snprintf(response, sizeof(response), "\tCharger screen enabled: %s", (device.charger_screen_enabled ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07003293 fastboot_info(response);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05303294 snprintf(response, sizeof(response), "\tDisplay panel: %s", (device.display_panel));
3295 fastboot_info(response);
Shashank Mittala0032282011-08-26 14:50:11 -07003296 fastboot_okay("");
3297}
3298
lijuang511a2b52015-08-14 20:50:51 +08003299void cmd_flashing_get_unlock_ability(const char *arg, void *data, unsigned sz)
3300{
3301 char response[MAX_RSP_SIZE];
3302 snprintf(response, sizeof(response), "\tget_unlock_ability: %d", is_allow_unlock);
3303 fastboot_info(response);
3304 fastboot_okay("");
3305}
3306
3307void cmd_flashing_lock_critical(const char *arg, void *data, unsigned sz)
3308{
lijuang4ece1e72015-08-14 21:02:36 +08003309 set_device_unlock(UNLOCK_CRITICAL, FALSE);
lijuang511a2b52015-08-14 20:50:51 +08003310}
3311
3312void cmd_flashing_unlock_critical(const char *arg, void *data, unsigned sz)
3313{
lijuang4ece1e72015-08-14 21:02:36 +08003314 set_device_unlock(UNLOCK_CRITICAL, TRUE);
lijuang511a2b52015-08-14 20:50:51 +08003315}
3316
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07003317void cmd_preflash(const char *arg, void *data, unsigned sz)
3318{
3319 fastboot_okay("");
3320}
3321
Mao Flynn7b379f32015-04-20 00:28:30 +08003322static uint8_t logo_header[LOGO_IMG_HEADER_SIZE];
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303323
Mao Flynn7b379f32015-04-20 00:28:30 +08003324int splash_screen_check_header(logo_img_header *header)
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303325{
Mao Flynn7b379f32015-04-20 00:28:30 +08003326 if (memcmp(header->magic, LOGO_IMG_MAGIC, 8))
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303327 return -1;
Mao Flynn7b379f32015-04-20 00:28:30 +08003328 if (header->width == 0 || header->height == 0)
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303329 return -1;
3330 return 0;
3331}
3332
Mao Flynn7b379f32015-04-20 00:28:30 +08003333int splash_screen_flash()
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003334{
3335 struct ptentry *ptn;
3336 struct ptable *ptable;
Mao Flynn7b379f32015-04-20 00:28:30 +08003337 struct logo_img_header *header;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003338 struct fbcon_config *fb_display = NULL;
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08003339
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303340 ptable = flash_get_ptable();
3341 if (ptable == NULL) {
Mao Flynn7b379f32015-04-20 00:28:30 +08003342 dprintf(CRITICAL, "ERROR: Partition table not found\n");
3343 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303344 }
Mao Flynn7b379f32015-04-20 00:28:30 +08003345
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303346 ptn = ptable_find(ptable, "splash");
3347 if (ptn == NULL) {
3348 dprintf(CRITICAL, "ERROR: splash Partition not found\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003349 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303350 }
Mao Flynn7b379f32015-04-20 00:28:30 +08003351 if (flash_read(ptn, 0, (void *)logo_header, LOGO_IMG_HEADER_SIZE)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303352 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003353 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303354 }
3355
Mao Flynn7b379f32015-04-20 00:28:30 +08003356 header = (struct logo_img_header *)logo_header;
3357 if (splash_screen_check_header(header)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303358 dprintf(CRITICAL, "ERROR: Boot image header invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003359 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303360 }
3361
3362 fb_display = fbcon_display();
3363 if (fb_display) {
Mao Flynn7b379f32015-04-20 00:28:30 +08003364 if (header->type && (header->blocks != 0)) { // RLE24 compressed data
3365 uint8_t *base = (uint8_t *) fb_display->base + LOGO_IMG_OFFSET;
3366
3367 /* if the logo is full-screen size, remove "fbcon_clear()" */
3368 if ((header->width != fb_display->width)
3369 || (header->height != fb_display->height))
3370 fbcon_clear();
3371
3372 if (flash_read(ptn + LOGO_IMG_HEADER_SIZE, 0,
3373 (uint32_t *)base,
3374 (header->blocks * 512))) {
3375 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
3376 return -1;
3377 }
3378 fbcon_extract_to_screen(header, base);
3379 return 0;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003380 }
Mao Flynn7b379f32015-04-20 00:28:30 +08003381
Veera Sundaram Sankaranc33416f2015-09-28 10:25:55 -07003382 if ((header->width > fb_display->width) || (header->height > fb_display->height)) {
3383 dprintf(CRITICAL, "Logo config greater than fb config. Fall back default logo\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003384 return -1;
3385 }
3386
Vineet Bajaj99291ed2014-09-09 12:29:46 +05303387 uint8_t *base = (uint8_t *) fb_display->base;
Mao Flynn7b379f32015-04-20 00:28:30 +08003388 if (flash_read(ptn + LOGO_IMG_HEADER_SIZE, 0,
3389 (uint32_t *)base,
3390 ((((header->width * header->height * fb_display->bpp/8) + 511) >> 9) << 9))) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303391 fbcon_clear();
Vineet Bajaj99291ed2014-09-09 12:29:46 +05303392 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003393 return -1;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003394 }
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303395 }
3396
Mao Flynn7b379f32015-04-20 00:28:30 +08003397 return 0;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303398}
3399
Mao Flynn7b379f32015-04-20 00:28:30 +08003400int splash_screen_mmc()
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303401{
3402 int index = INVALID_PTN;
3403 unsigned long long ptn = 0;
3404 struct fbcon_config *fb_display = NULL;
Mao Flynn7b379f32015-04-20 00:28:30 +08003405 struct logo_img_header *header;
Mao Flynn1893a7f2015-06-03 12:03:36 +08003406 uint32_t blocksize, realsize, readsize;
3407 uint8_t *base;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303408
3409 index = partition_get_index("splash");
3410 if (index == 0) {
3411 dprintf(CRITICAL, "ERROR: splash Partition table not found\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003412 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303413 }
3414
3415 ptn = partition_get_offset(index);
3416 if (ptn == 0) {
3417 dprintf(CRITICAL, "ERROR: splash Partition invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003418 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303419 }
3420
Mao Flynn1893a7f2015-06-03 12:03:36 +08003421 mmc_set_lun(partition_get_lun(index));
3422
3423 blocksize = mmc_get_device_blocksize();
3424 if (blocksize == 0) {
3425 dprintf(CRITICAL, "ERROR:splash Partition invalid blocksize\n");
3426 return -1;
3427 }
3428
3429 fb_display = fbcon_display();
Channagoud Kadabidaf10a62015-07-22 11:51:55 -07003430 if (!fb_display)
3431 {
3432 dprintf(CRITICAL, "ERROR: fb config is not allocated\n");
3433 return -1;
3434 }
3435
Mao Flynn1893a7f2015-06-03 12:03:36 +08003436 base = (uint8_t *) fb_display->base;
3437
3438 if (mmc_read(ptn, (uint32_t *)(base + LOGO_IMG_OFFSET), blocksize)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303439 dprintf(CRITICAL, "ERROR: Cannot read splash image header\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003440 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303441 }
3442
Mao Flynn1893a7f2015-06-03 12:03:36 +08003443 header = (struct logo_img_header *)(base + LOGO_IMG_OFFSET);
Mao Flynn7b379f32015-04-20 00:28:30 +08003444 if (splash_screen_check_header(header)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303445 dprintf(CRITICAL, "ERROR: Splash image header invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003446 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303447 }
3448
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303449 if (fb_display) {
Mao Flynn1893a7f2015-06-03 12:03:36 +08003450 if (header->type && (header->blocks != 0)) { /* 1 RLE24 compressed data */
3451 base += LOGO_IMG_OFFSET;
Mao Flynn7b379f32015-04-20 00:28:30 +08003452
Mao Flynn1893a7f2015-06-03 12:03:36 +08003453 realsize = header->blocks * 512;
3454 readsize = ROUNDUP((realsize + LOGO_IMG_HEADER_SIZE), blocksize) - blocksize;
3455
3456 /* if the logo is not full-screen size, clean screen */
Mao Flynn7b379f32015-04-20 00:28:30 +08003457 if ((header->width != fb_display->width)
3458 || (header->height != fb_display->height))
3459 fbcon_clear();
3460
Mao Flynn1893a7f2015-06-03 12:03:36 +08003461 if (mmc_read(ptn + blocksize, (uint32_t *)(base + blocksize), readsize)) {
Mao Flynn7b379f32015-04-20 00:28:30 +08003462 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
3463 return -1;
3464 }
Mao Flynn7b379f32015-04-20 00:28:30 +08003465
Mao Flynn1893a7f2015-06-03 12:03:36 +08003466 fbcon_extract_to_screen(header, (base + LOGO_IMG_HEADER_SIZE));
3467 } else { /* 2 Raw BGR data */
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303468
Veera Sundaram Sankaranc33416f2015-09-28 10:25:55 -07003469 if ((header->width > fb_display->width) || (header->height > fb_display->height)) {
3470 dprintf(CRITICAL, "Logo config greater than fb config. Fall back default logo\n");
Mao Flynn1893a7f2015-06-03 12:03:36 +08003471 return -1;
3472 }
3473
3474 realsize = header->width * header->height * fb_display->bpp / 8;
3475 readsize = ROUNDUP((realsize + LOGO_IMG_HEADER_SIZE), blocksize) - blocksize;
3476
3477 if (blocksize == LOGO_IMG_HEADER_SIZE) { /* read the content directly */
3478 if (mmc_read((ptn + LOGO_IMG_HEADER_SIZE), (uint32_t *)base, readsize)) {
3479 fbcon_clear();
3480 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
3481 return -1;
3482 }
3483 } else {
3484 if (mmc_read(ptn + blocksize ,
3485 (uint32_t *)(base + LOGO_IMG_OFFSET + blocksize), readsize)) {
3486 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
3487 return -1;
3488 }
3489 memmove(base, (base + LOGO_IMG_OFFSET + LOGO_IMG_HEADER_SIZE), realsize);
3490 }
3491 }
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303492 }
3493
Mao Flynn7b379f32015-04-20 00:28:30 +08003494 return 0;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303495}
3496
Mao Flynn7b379f32015-04-20 00:28:30 +08003497int fetch_image_from_partition()
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303498{
3499 if (target_is_emmc_boot()) {
3500 return splash_screen_mmc();
3501 } else {
3502 return splash_screen_flash();
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003503 }
3504}
3505
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07003506/* Get the size from partiton name */
3507static void get_partition_size(const char *arg, char *response)
3508{
3509 uint64_t ptn = 0;
3510 uint64_t size;
3511 int index = INVALID_PTN;
3512
3513 index = partition_get_index(arg);
3514
3515 if (index == INVALID_PTN)
3516 {
3517 dprintf(CRITICAL, "Invalid partition index\n");
3518 return;
3519 }
3520
3521 ptn = partition_get_offset(index);
3522
3523 if(!ptn)
3524 {
3525 dprintf(CRITICAL, "Invalid partition name %s\n", arg);
3526 return;
3527 }
3528
3529 size = partition_get_size(index);
3530
3531 snprintf(response, MAX_RSP_SIZE, "\t 0x%llx", size);
3532 return;
3533}
3534
3535/*
3536 * Publish the partition type & size info
3537 * fastboot getvar will publish the required information.
3538 * fastboot getvar partition_size:<partition_name>: partition size in hex
3539 * fastboot getvar partition_type:<partition_name>: partition type (ext/fat)
3540 */
3541static void publish_getvar_partition_info(struct getvar_partition_info *info, uint8_t num_parts)
3542{
3543 uint8_t i;
3544
3545 for (i = 0; i < num_parts; i++) {
3546 get_partition_size(info[i].part_name, info[i].size_response);
3547
3548 if (strlcat(info[i].getvar_size, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
3549 {
3550 dprintf(CRITICAL, "partition size name truncated\n");
3551 return;
3552 }
3553 if (strlcat(info[i].getvar_type, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
3554 {
3555 dprintf(CRITICAL, "partition type name truncated\n");
3556 return;
3557 }
3558
3559 /* publish partition size & type info */
3560 fastboot_publish((const char *) info[i].getvar_size, (const char *) info[i].size_response);
3561 fastboot_publish((const char *) info[i].getvar_type, (const char *) info[i].type_response);
3562 }
3563}
3564
lijuang4ece1e72015-08-14 21:02:36 +08003565void get_product_name(unsigned char *buf)
3566{
3567 snprintf((char*)buf, MAX_RSP_SIZE, "%s", TARGET(BOARD));
3568 return;
3569}
3570
3571void get_bootloader_version(unsigned char *buf)
3572{
3573 snprintf((char*)buf, MAX_RSP_SIZE, "%s", device.bootloader_version);
3574 return;
3575}
3576
3577void get_baseband_version(unsigned char *buf)
3578{
3579 snprintf((char*)buf, MAX_RSP_SIZE, "%s", device.radio_version);
3580 return;
3581}
3582
3583bool is_device_locked()
3584{
3585 return device.is_unlocked ? false:true;
3586}
3587
Amol Jadi5edf3552013-07-23 14:15:34 -07003588/* register commands and variables for fastboot */
3589void aboot_fastboot_register_commands(void)
3590{
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003591 int i;
lijuangf16461c2015-08-03 17:09:34 +08003592 char hw_platform_buf[MAX_RSP_SIZE];
Amol Jadi5edf3552013-07-23 14:15:34 -07003593
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003594 struct fastboot_cmd_desc cmd_list[] = {
lijuang511a2b52015-08-14 20:50:51 +08003595 /* By default the enabled list is empty. */
3596 {"", NULL},
3597 /* move commands enclosed within the below ifndef to here
3598 * if they need to be enabled in user build.
3599 */
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003600#ifndef DISABLE_FASTBOOT_CMDS
lijuang511a2b52015-08-14 20:50:51 +08003601 /* Register the following commands only for non-user builds */
3602 {"flash:", cmd_flash},
3603 {"erase:", cmd_erase},
3604 {"boot", cmd_boot},
3605 {"continue", cmd_continue},
3606 {"reboot", cmd_reboot},
3607 {"reboot-bootloader", cmd_reboot_bootloader},
3608 {"oem unlock", cmd_oem_unlock},
3609 {"oem unlock-go", cmd_oem_unlock_go},
3610 {"oem lock", cmd_oem_lock},
3611 {"flashing unlock", cmd_oem_unlock},
3612 {"flashing lock", cmd_oem_lock},
3613 {"flashing lock_critical", cmd_flashing_lock_critical},
3614 {"flashing unlock_critical", cmd_flashing_unlock_critical},
3615 {"flashing get_unlock_ability", cmd_flashing_get_unlock_ability},
3616 {"oem device-info", cmd_oem_devinfo},
3617 {"preflash", cmd_preflash},
3618 {"oem enable-charger-screen", cmd_oem_enable_charger_screen},
3619 {"oem disable-charger-screen", cmd_oem_disable_charger_screen},
lijuanga25d8bb2015-09-16 13:11:52 +08003620 {"oem off-mode-charge", cmd_oem_off_mode_charger},
lijuang511a2b52015-08-14 20:50:51 +08003621 {"oem select-display-panel", cmd_oem_select_display_panel},
Channagoud Kadabi736c4962015-08-21 11:56:52 -07003622#if UNITTEST_FW_SUPPORT
Channagoud Kadabid5ddf482015-09-28 10:31:35 -07003623 {"oem run-tests", cmd_oem_runtests},
Channagoud Kadabi736c4962015-08-21 11:56:52 -07003624#endif
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003625#endif
lijuang511a2b52015-08-14 20:50:51 +08003626 };
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003627
3628 int fastboot_cmds_count = sizeof(cmd_list)/sizeof(cmd_list[0]);
3629 for (i = 1; i < fastboot_cmds_count; i++)
3630 fastboot_register(cmd_list[i].name,cmd_list[i].cb);
3631
Amol Jadi5edf3552013-07-23 14:15:34 -07003632 /* publish variables and their values */
3633 fastboot_publish("product", TARGET(BOARD));
3634 fastboot_publish("kernel", "lk");
3635 fastboot_publish("serialno", sn_buf);
3636
3637 /*
3638 * partition info is supported only for emmc partitions
3639 * Calling this for NAND prints some error messages which
3640 * is harmless but misleading. Avoid calling this for NAND
3641 * devices.
3642 */
3643 if (target_is_emmc_boot())
3644 publish_getvar_partition_info(part_info, ARRAY_SIZE(part_info));
3645
3646 /* Max download size supported */
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003647 snprintf(max_download_size, MAX_RSP_SIZE, "\t0x%x",
3648 target_get_max_flash_size());
Amol Jadi5edf3552013-07-23 14:15:34 -07003649 fastboot_publish("max-download-size", (const char *) max_download_size);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003650 /* Is the charger screen check enabled */
3651 snprintf(charger_screen_enabled, MAX_RSP_SIZE, "%d",
3652 device.charger_screen_enabled);
3653 fastboot_publish("charger-screen-enabled",
3654 (const char *) charger_screen_enabled);
lijuanga25d8bb2015-09-16 13:11:52 +08003655 fastboot_publish("off-mode-charge", (const char *) charger_screen_enabled);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05303656 snprintf(panel_display_mode, MAX_RSP_SIZE, "%s",
3657 device.display_panel);
3658 fastboot_publish("display-panel",
3659 (const char *) panel_display_mode);
Channagoud Kadabiaafbbfc2015-03-04 17:52:56 -08003660 fastboot_publish("version-bootloader", (const char *) device.bootloader_version);
3661 fastboot_publish("version-baseband", (const char *) device.radio_version);
lijuangf16461c2015-08-03 17:09:34 +08003662 fastboot_publish("secure", is_secure_boot_enable()? "yes":"no");
3663 smem_get_hw_platform_name((unsigned char *) hw_platform_buf, sizeof(hw_platform_buf));
3664 snprintf(get_variant, MAX_RSP_SIZE, "%s %s", hw_platform_buf,
3665 target_is_emmc_boot()? "eMMC":"UFS");
3666 fastboot_publish("variant", (const char *) get_variant);
lijuang65c5a822015-08-29 16:35:36 +08003667#if CHECK_BAT_VOLTAGE
lijuang102dfa92015-10-09 18:31:03 +08003668 update_battery_status();
lijuang65c5a822015-08-29 16:35:36 +08003669 fastboot_publish("battery-voltage", (const char *) battery_voltage);
lijuang102dfa92015-10-09 18:31:03 +08003670 fastboot_publish("battery-soc-ok", (const char *) battery_soc_ok);
lijuang65c5a822015-08-29 16:35:36 +08003671#endif
Amol Jadi5edf3552013-07-23 14:15:34 -07003672}
3673
Brian Swetland9c4c0752009-01-25 16:23:50 -08003674void aboot_init(const struct app_descriptor *app)
3675{
Shashank Mittal4f99a882010-02-01 13:58:50 -08003676 unsigned reboot_mode = 0;
Chandan Uddarajubedca152010-06-02 23:05:15 -07003677
lijuang39831732016-01-08 17:49:02 +08003678 /* Initialise wdog to catch early lk crashes */
3679#if WDOG_SUPPORT
3680 msm_wdog_init();
3681#endif
3682
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07003683 /* Setup page size information for nv storage */
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003684 if (target_is_emmc_boot())
3685 {
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07003686 page_size = mmc_page_size();
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003687 page_mask = page_size - 1;
Gaurav Nebhwani4d2389c2016-03-17 21:10:05 +05303688 mmc_blocksize = mmc_get_device_blocksize();
3689 mmc_blocksize_mask = mmc_blocksize - 1;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003690 }
3691 else
3692 {
3693 page_size = flash_page_size();
3694 page_mask = page_size - 1;
3695 }
3696
Deepa Dinamani0e163a42013-05-24 17:08:15 -07003697 ASSERT((MEMBASE + MEMSIZE) > MEMBASE);
3698
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003699 read_device_info(&device);
vijay kumarc65876c2015-04-24 13:29:16 +05303700 read_allow_oem_unlock(&device);
Shashank Mittal162244e2011-08-08 19:01:25 -07003701
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08003702 /* Display splash screen if enabled */
3703#if DISPLAY_SPLASH_SCREEN
lijuang99c02d82015-02-13 19:04:34 +08003704#if NO_ALARM_DISPLAY
3705 if (!check_alarm_boot()) {
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08003706#endif
lijuang99c02d82015-02-13 19:04:34 +08003707 dprintf(SPEW, "Display Init: Start\n");
Channagoud Kadabi90869ce2015-04-27 11:15:14 -07003708#if ENABLE_WBC
Channagoud Kadabid801ac72015-08-26 11:21:51 -07003709 /* Wait if the display shutdown is in progress */
3710 while(pm_app_display_shutdown_in_prgs());
Channagoud Kadabi90869ce2015-04-27 11:15:14 -07003711 if (!pm_appsbl_display_init_done())
3712 target_display_init(device.display_panel);
3713 else
3714 display_image_on_screen();
3715#else
lijuang99c02d82015-02-13 19:04:34 +08003716 target_display_init(device.display_panel);
Channagoud Kadabi90869ce2015-04-27 11:15:14 -07003717#endif
lijuang99c02d82015-02-13 19:04:34 +08003718 dprintf(SPEW, "Display Init: Done\n");
3719#if NO_ALARM_DISPLAY
3720 }
3721#endif
3722#endif
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08003723
Greg Griscod6250552011-06-29 14:40:23 -07003724 target_serialno((unsigned char *) sn_buf);
Ajay Dudanib06c05f2011-05-12 14:46:10 -07003725 dprintf(SPEW,"serial number: %s\n",sn_buf);
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -08003726
Dhaval Patel223ec952013-07-18 14:49:44 -07003727 memset(display_panel_buf, '\0', MAX_PANEL_BUF_SIZE);
3728
Matthew Qindefd5562014-07-11 18:02:40 +08003729 /*
3730 * Check power off reason if user force reset,
3731 * if yes phone will do normal boot.
3732 */
3733 if (is_user_force_reset())
3734 goto normal_boot;
3735
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003736 /* Check if we should do something other than booting up */
Ameya Thakur0b9c2442013-05-10 13:22:56 -07003737 if (keys_get_state(KEY_VOLUMEUP) && keys_get_state(KEY_VOLUMEDOWN))
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07003738 {
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03003739 dprintf(ALWAYS,"dload mode key sequence detected\n");
lijuang395b5e62015-11-19 17:39:44 +08003740 reboot_device(EMERGENCY_DLOAD);
3741 dprintf(CRITICAL,"Failed to reboot into dload mode\n");
3742
Ameya Thakur0b9c2442013-05-10 13:22:56 -07003743 boot_into_fastboot = true;
3744 }
3745 if (!boot_into_fastboot)
3746 {
3747 if (keys_get_state(KEY_HOME) || keys_get_state(KEY_VOLUMEUP))
3748 boot_into_recovery = 1;
3749 if (!boot_into_recovery &&
3750 (keys_get_state(KEY_BACK) || keys_get_state(KEY_VOLUMEDOWN)))
Pavel Nedev5d91d412013-04-29 11:34:24 +03003751 boot_into_fastboot = true;
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07003752 }
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003753 #if NO_KEYPAD_DRIVER
Kinson Chik0b1c8162011-08-31 16:31:57 -07003754 if (fastboot_trigger())
Pavel Nedev5d91d412013-04-29 11:34:24 +03003755 boot_into_fastboot = true;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003756 #endif
Chandan Uddarajubedca152010-06-02 23:05:15 -07003757
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003758#if USE_PON_REBOOT_REG
3759 reboot_mode = check_hard_reboot_mode();
3760#else
Ajay Dudani77421292010-10-27 19:34:06 -07003761 reboot_mode = check_reboot_mode();
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003762#endif
3763 if (reboot_mode == RECOVERY_MODE)
3764 {
Ajay Dudani77421292010-10-27 19:34:06 -07003765 boot_into_recovery = 1;
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003766 }
3767 else if(reboot_mode == FASTBOOT_MODE)
3768 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03003769 boot_into_fastboot = true;
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003770 }
3771 else if(reboot_mode == ALARM_BOOT)
3772 {
Matthew Qind886f3c2014-01-17 16:52:01 +08003773 boot_reason_alarm = true;
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003774 }
Parth Dixit207573a2015-10-27 17:26:21 +05303775#if VERIFIED_BOOT
Parth Dixitddbc7352015-10-18 03:13:31 +05303776#if !VBOOT_MOTA
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003777 else if (reboot_mode == DM_VERITY_ENFORCING)
3778 {
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07003779 device.verity_mode = 1;
3780 write_device_info(&device);
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003781 }
3782 else if (reboot_mode == DM_VERITY_LOGGING)
3783 {
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07003784 device.verity_mode = 0;
3785 write_device_info(&device);
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003786 }
3787 else if (reboot_mode == DM_VERITY_KEYSCLEAR)
3788 {
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07003789 if(send_delete_keys_to_tz())
3790 ASSERT(0);
Ajay Dudani77421292010-10-27 19:34:06 -07003791 }
Parth Dixit207573a2015-10-27 17:26:21 +05303792#endif
Parth Dixitddbc7352015-10-18 03:13:31 +05303793#endif
Ajay Dudani77421292010-10-27 19:34:06 -07003794
Matthew Qindefd5562014-07-11 18:02:40 +08003795normal_boot:
Pavel Nedev5d91d412013-04-29 11:34:24 +03003796 if (!boot_into_fastboot)
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003797 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03003798 if (target_is_emmc_boot())
Shashank Mittala0032282011-08-26 14:50:11 -07003799 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03003800 if(emmc_recovery_init())
3801 dprintf(ALWAYS,"error in emmc_recovery_init\n");
3802 if(target_use_signed_kernel())
Shashank Mittala0032282011-08-26 14:50:11 -07003803 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03003804 if((device.is_unlocked) || (device.is_tampered))
3805 {
3806 #ifdef TZ_TAMPER_FUSE
3807 set_tamper_fuse_cmd();
3808 #endif
3809 #if USE_PCOM_SECBOOT
3810 set_tamper_flag(device.is_tampered);
3811 #endif
3812 }
Shashank Mittala0032282011-08-26 14:50:11 -07003813 }
Amit Blay6281ebc2015-01-11 14:44:08 +02003814
Pavel Nedev5d91d412013-04-29 11:34:24 +03003815 boot_linux_from_mmc();
Shashank Mittala0032282011-08-26 14:50:11 -07003816 }
Pavel Nedev5d91d412013-04-29 11:34:24 +03003817 else
3818 {
3819 recovery_init();
3820 #if USE_PCOM_SECBOOT
3821 if((device.is_unlocked) || (device.is_tampered))
3822 set_tamper_flag(device.is_tampered);
3823 #endif
3824 boot_linux_from_flash();
3825 }
3826 dprintf(CRITICAL, "ERROR: Could not do normal boot. Reverting "
3827 "to fastboot mode.\n");
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003828 }
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003829
Amol Jadi5edf3552013-07-23 14:15:34 -07003830 /* We are here means regular boot did not happen. Start fastboot. */
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07003831
Amol Jadi5edf3552013-07-23 14:15:34 -07003832 /* register aboot specific fastboot commands */
3833 aboot_fastboot_register_commands();
Amol Jadi57abe4c2011-05-24 15:47:27 -07003834
Amol Jadi5edf3552013-07-23 14:15:34 -07003835 /* dump partition table for debug info */
Kinson Chikf1a43512011-07-14 11:28:39 -07003836 partition_dump();
Amol Jadi5edf3552013-07-23 14:15:34 -07003837
3838 /* initialize and start fastboot */
3839 fastboot_init(target_get_scratch_address(), target_get_max_flash_size());
lijuang4ece1e72015-08-14 21:02:36 +08003840#if FBCON_DISPLAY_MSG
lijuang9a7d3b92015-11-30 14:41:24 +08003841 display_fastboot_menu();
lijuang4ece1e72015-08-14 21:02:36 +08003842#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08003843}
3844
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07003845uint32_t get_page_size()
3846{
3847 return page_size;
3848}
3849
Amir Samuelov57a6fa22013-06-05 16:36:43 +03003850/*
3851 * Calculated and save hash (SHA256) for non-signed boot image.
3852 *
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07003853 * @param image_addr - Boot image address
3854 * @param image_size - Size of the boot image
Amir Samuelov57a6fa22013-06-05 16:36:43 +03003855 *
3856 * @return int - 0 on success, negative value on failure.
3857 */
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07003858static int aboot_save_boot_hash_mmc(uint32_t image_addr, uint32_t image_size)
Amir Samuelov57a6fa22013-06-05 16:36:43 +03003859{
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07003860 unsigned int digest[8];
3861#if IMAGE_VERIF_ALGO_SHA1
3862 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA1;
3863#else
3864 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA256;
3865#endif
Amir Samuelov57a6fa22013-06-05 16:36:43 +03003866
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07003867 target_crypto_init_params();
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -08003868 hash_find((unsigned char *) image_addr, image_size, (unsigned char *)&digest, auth_algo);
Amir Samuelov57a6fa22013-06-05 16:36:43 +03003869
3870 save_kernel_hash_cmd(digest);
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07003871 dprintf(INFO, "aboot_save_boot_hash_mmc: imagesize_actual size %d bytes.\n", (int) image_size);
Amir Samuelov57a6fa22013-06-05 16:36:43 +03003872
3873 return 0;
3874}
3875
Brian Swetland9c4c0752009-01-25 16:23:50 -08003876APP_START(aboot)
3877 .init = aboot_init,
3878APP_END