blob: 26ccd5788ec5039ee24bd4d4ae63fe5d797df087 [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;
193static char ffbm_mode_string[FFBM_MODE_BUF_SIZE];
194static bool boot_into_ffbm;
vijay kumar870515d2015-08-31 16:37:24 +0530195static char *target_boot_params = NULL;
Matthew Qind886f3c2014-01-17 16:52:01 +0800196static bool boot_reason_alarm;
Channagoud Kadabi80a182b2015-03-11 17:04:23 -0700197static bool devinfo_present = true;
Channagoud Kadabi736c4962015-08-21 11:56:52 -0700198bool boot_into_fastboot = false;
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700199
Shashank Mittalcd98d472011-08-02 14:29:24 -0700200/* Assuming unauthorized kernel image by default */
201static int auth_kernel_img = 0;
Parth Dixit6f5822f2015-10-18 01:20:53 +0530202#if VBOOT_MOTA
203static device_info device = {DEVICE_MAGIC, 0, 0, 0, 0, {0}, {0},{0}};
204#else
lijuang511a2b52015-08-14 20:50:51 +0800205static device_info device = {DEVICE_MAGIC, 0, 0, 0, 0, {0}, {0},{0}, 1};
Parth Dixit6f5822f2015-10-18 01:20:53 +0530206#endif
vijay kumarc65876c2015-04-24 13:29:16 +0530207static bool is_allow_unlock = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -0700208
vijay kumarca2e6812015-07-08 20:28:25 +0530209static char frp_ptns[2][8] = {"config","frp"};
210
lijuang511a2b52015-08-14 20:50:51 +0800211static const char *critical_flash_allowed_ptn[] = {
212 "aboot",
213 "rpm",
214 "tz",
215 "sbl",
216 "sdi",
217 "sbl1",
218 "xbl",
219 "hyp",
220 "pmic",
221 "bootloader",
222 "devinfo",
223 "partition"};
224
Dima Zavin42168f22009-01-30 11:52:22 -0800225struct atag_ptbl_entry
226{
227 char name[16];
228 unsigned offset;
229 unsigned size;
230 unsigned flags;
231};
232
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700233/*
234 * Partition info, required to be published
235 * for fastboot
236 */
237struct getvar_partition_info {
238 const char part_name[MAX_GPT_NAME_SIZE]; /* Partition name */
239 char getvar_size[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for size */
240 char getvar_type[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for type */
241 char size_response[MAX_RSP_SIZE]; /* fastboot response for size */
242 char type_response[MAX_RSP_SIZE]; /* fastboot response for type */
243};
244
245/*
246 * Right now, we are publishing the info for only
247 * three partitions
248 */
249struct getvar_partition_info part_info[] =
250{
251 { "system" , "partition-size:", "partition-type:", "", "ext4" },
252 { "userdata", "partition-size:", "partition-type:", "", "ext4" },
253 { "cache" , "partition-size:", "partition-type:", "", "ext4" },
254};
255
256char max_download_size[MAX_RSP_SIZE];
Ameya Thakur11cf1a62013-08-05 12:44:48 -0700257char charger_screen_enabled[MAX_RSP_SIZE];
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800258char sn_buf[13];
Dhaval Patel223ec952013-07-18 14:49:44 -0700259char display_panel_buf[MAX_PANEL_BUF_SIZE];
Unnati Gandhi62c8ab82014-01-24 11:01:01 +0530260char panel_display_mode[MAX_RSP_SIZE];
lijuang102dfa92015-10-09 18:31:03 +0800261
262#if CHECK_BAT_VOLTAGE
lijuang65c5a822015-08-29 16:35:36 +0800263char battery_voltage[MAX_RSP_SIZE];
lijuang102dfa92015-10-09 18:31:03 +0800264char battery_soc_ok [MAX_RSP_SIZE];
265#endif
266
lijuangf16461c2015-08-03 17:09:34 +0800267char get_variant[MAX_RSP_SIZE];
Greg Griscod6250552011-06-29 14:40:23 -0700268
Greg Griscod2471ef2011-07-14 13:00:42 -0700269extern int emmc_recovery_init(void);
270
Kinson Chik0b1c8162011-08-31 16:31:57 -0700271#if NO_KEYPAD_DRIVER
272extern int fastboot_trigger(void);
273#endif
Greg Griscod2471ef2011-07-14 13:00:42 -0700274
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800275static void update_ker_tags_rdisk_addr(struct boot_img_hdr *hdr, bool is_arm64)
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700276{
277 /* overwrite the destination of specified for the project */
Channagoud Kadabi7042fa32013-04-26 16:44:14 -0700278#ifdef ABOOT_IGNORE_BOOT_HEADER_ADDRS
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800279 if (is_arm64)
280 hdr->kernel_addr = ABOOT_FORCE_KERNEL64_ADDR;
281 else
282 hdr->kernel_addr = ABOOT_FORCE_KERNEL_ADDR;
Channagoud Kadabi7042fa32013-04-26 16:44:14 -0700283 hdr->ramdisk_addr = ABOOT_FORCE_RAMDISK_ADDR;
284 hdr->tags_addr = ABOOT_FORCE_TAGS_ADDR;
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700285#endif
286}
287
Dima Zavin42168f22009-01-30 11:52:22 -0800288static void ptentry_to_tag(unsigned **ptr, struct ptentry *ptn)
289{
290 struct atag_ptbl_entry atag_ptn;
291
292 memcpy(atag_ptn.name, ptn->name, 16);
293 atag_ptn.name[15] = '\0';
294 atag_ptn.offset = ptn->start;
295 atag_ptn.size = ptn->length;
296 atag_ptn.flags = ptn->flags;
297 memcpy(*ptr, &atag_ptn, sizeof(struct atag_ptbl_entry));
298 *ptr += sizeof(struct atag_ptbl_entry) / sizeof(unsigned);
299}
Brian Swetland9c4c0752009-01-25 16:23:50 -0800300
lijuang102dfa92015-10-09 18:31:03 +0800301#if CHECK_BAT_VOLTAGE
302void update_battery_status(void)
303{
304 snprintf(battery_voltage,MAX_RSP_SIZE, "%d",target_get_battery_voltage());
305 snprintf(battery_soc_ok ,MAX_RSP_SIZE, "%s",target_battery_soc_ok()? "yes":"no");
306}
307#endif
308
Neeti Desaie245d492012-06-01 12:52:13 -0700309unsigned char *update_cmdline(const char * cmdline)
Brian Swetland9c4c0752009-01-25 16:23:50 -0800310{
David Ng183a7422009-12-07 14:55:21 -0800311 int cmdline_len = 0;
312 int have_cmdline = 0;
Amol Jadi168b7712012-03-06 16:15:00 -0800313 unsigned char *cmdline_final = NULL;
Neeti Desaie245d492012-06-01 12:52:13 -0700314 int pause_at_bootup = 0;
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800315 bool warm_boot = false;
Pavel Nedev5614d222013-06-17 18:01:02 +0300316 bool gpt_exists = partition_gpt_exists();
Joonwoo Park61112782013-10-02 19:50:39 -0700317 int have_target_boot_params = 0;
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700318 char *boot_dev_buf = NULL;
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200319 bool is_mdtp_activated = 0;
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700320#if VERIFIED_BOOT
Parth Dixitddbc7352015-10-18 03:13:31 +0530321#if !VBOOT_MOTA
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700322 uint32_t boot_state = boot_verify_get_state();
323#endif
Parth Dixitddbc7352015-10-18 03:13:31 +0530324#endif
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700325
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200326#ifdef MDTP_SUPPORT
327 mdtp_activated(&is_mdtp_activated);
328#endif /* MDTP_SUPPORT */
Dima Zavin42168f22009-01-30 11:52:22 -0800329
Brian Swetland9c4c0752009-01-25 16:23:50 -0800330 if (cmdline && cmdline[0]) {
David Ng183a7422009-12-07 14:55:21 -0800331 cmdline_len = strlen(cmdline);
332 have_cmdline = 1;
333 }
334 if (target_is_emmc_boot()) {
335 cmdline_len += strlen(emmc_cmdline);
Sridhar Parasuram7bd4aaf2015-02-12 11:14:38 -0800336#if USE_BOOTDEV_CMDLINE
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700337 boot_dev_buf = (char *) malloc(sizeof(char) * BOOT_DEV_MAX_LEN);
338 ASSERT(boot_dev_buf);
339 platform_boot_dev_cmdline(boot_dev_buf);
340 cmdline_len += strlen(boot_dev_buf);
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700341#endif
David Ng183a7422009-12-07 14:55:21 -0800342 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800343
344 cmdline_len += strlen(usb_sn_cmdline);
345 cmdline_len += strlen(sn_buf);
346
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700347#if VERIFIED_BOOT
Parth Dixitddbc7352015-10-18 03:13:31 +0530348#if !VBOOT_MOTA
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700349 cmdline_len += strlen(verified_state) + strlen(vbsn[boot_state].name);
Sridhar Parasuram7d8bb9a2015-09-23 18:21:19 -0700350 if ((device.verity_mode != 0 ) && (device.verity_mode != 1))
351 {
352 dprintf(CRITICAL, "Devinfo paritition possibly corrupted!!!. Please erase devinfo partition to continue booting\n");
353 ASSERT(0);
354 }
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700355 cmdline_len += strlen(verity_mode) + strlen(vbvm[device.verity_mode].name);
Parth Dixita5715a02015-10-29 12:25:10 +0530356 cmdline_len += strlen(keymaster_v1);
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700357#endif
Parth Dixitddbc7352015-10-18 03:13:31 +0530358#endif
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700359
Pavel Nedev5614d222013-06-17 18:01:02 +0300360 if (boot_into_recovery && gpt_exists)
361 cmdline_len += strlen(secondary_gpt_enable);
362
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200363 if(is_mdtp_activated)
364 cmdline_len += strlen(mdtp_activated_flag);
365
Pavel Nedev328ac822013-04-05 15:25:11 +0300366 if (boot_into_ffbm) {
367 cmdline_len += strlen(androidboot_mode);
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700368 cmdline_len += strlen(ffbm_mode_string);
Pavel Nedev898298c2013-02-27 12:36:09 -0800369 /* reduce kernel console messages to speed-up boot */
370 cmdline_len += strlen(loglevel);
Matthew Qind886f3c2014-01-17 16:52:01 +0800371 } else if (boot_reason_alarm) {
372 cmdline_len += strlen(alarmboot_cmdline);
Zhenhua Huang431dafa2015-06-30 16:13:37 +0800373 } else if ((target_build_variant_user() || device.charger_screen_enabled)
374 && target_pause_for_battery_charge()) {
David Ngf773dde2010-07-26 19:55:08 -0700375 pause_at_bootup = 1;
376 cmdline_len += strlen(battchg_pause);
377 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800378
Shashank Mittalcd98d472011-08-02 14:29:24 -0700379 if(target_use_signed_kernel() && auth_kernel_img) {
380 cmdline_len += strlen(auth_kernel);
381 }
382
Joonwoo Park61112782013-10-02 19:50:39 -0700383 if (get_target_boot_params(cmdline, boot_into_recovery ? "recoveryfs" :
384 "system",
vijay kumar870515d2015-08-31 16:37:24 +0530385 &target_boot_params) == 0) {
Joonwoo Park61112782013-10-02 19:50:39 -0700386 have_target_boot_params = 1;
387 cmdline_len += strlen(target_boot_params);
388 }
389
Ajay Dudanid04110c2011-01-17 23:55:07 -0800390 /* Determine correct androidboot.baseband to use */
391 switch(target_baseband())
392 {
393 case BASEBAND_APQ:
394 cmdline_len += strlen(baseband_apq);
395 break;
396
397 case BASEBAND_MSM:
398 cmdline_len += strlen(baseband_msm);
399 break;
400
401 case BASEBAND_CSFB:
402 cmdline_len += strlen(baseband_csfb);
403 break;
404
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800405 case BASEBAND_SVLTE2A:
406 cmdline_len += strlen(baseband_svlte2a);
Ajay Dudanid04110c2011-01-17 23:55:07 -0800407 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700408
409 case BASEBAND_MDM:
410 cmdline_len += strlen(baseband_mdm);
411 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700412
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800413 case BASEBAND_MDM2:
414 cmdline_len += strlen(baseband_mdm2);
415 break;
416
Amol Jadi5c61a952012-05-04 17:05:35 -0700417 case BASEBAND_SGLTE:
418 cmdline_len += strlen(baseband_sglte);
419 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530420
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800421 case BASEBAND_SGLTE2:
422 cmdline_len += strlen(baseband_sglte2);
423 break;
424
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530425 case BASEBAND_DSDA:
426 cmdline_len += strlen(baseband_dsda);
427 break;
Amol Jadi2a15a272013-01-22 12:03:36 -0800428
429 case BASEBAND_DSDA2:
430 cmdline_len += strlen(baseband_dsda2);
431 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800432 }
433
Lijuan Gao4a5b8322014-07-24 10:38:42 +0800434 if (cmdline) {
435 if ((strstr(cmdline, DISPLAY_DEFAULT_PREFIX) == NULL) &&
Padmanabhan Komandurubccbcdc2015-06-30 16:19:24 +0530436 target_display_panel_node(display_panel_buf,
437 MAX_PANEL_BUF_SIZE) &&
Lijuan Gao4a5b8322014-07-24 10:38:42 +0800438 strlen(display_panel_buf)) {
439 cmdline_len += strlen(display_panel_buf);
440 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700441 }
442
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800443 if (target_warm_boot()) {
444 warm_boot = true;
445 cmdline_len += strlen(warmboot_cmdline);
446 }
447
David Ng183a7422009-12-07 14:55:21 -0800448 if (cmdline_len > 0) {
449 const char *src;
Maria Yu52254c02014-07-04 16:14:54 +0800450 unsigned char *dst;
451
452 cmdline_final = (unsigned char*) malloc((cmdline_len + 4) & (~3));
453 ASSERT(cmdline_final != NULL);
vijay kumar287bb542015-09-29 13:01:52 +0530454 memset((void *)cmdline_final, 0, sizeof(*cmdline_final));
Maria Yu52254c02014-07-04 16:14:54 +0800455 dst = cmdline_final;
Neeti Desaie245d492012-06-01 12:52:13 -0700456
Amol Jadi168b7712012-03-06 16:15:00 -0800457 /* Save start ptr for debug print */
David Ng183a7422009-12-07 14:55:21 -0800458 if (have_cmdline) {
459 src = cmdline;
460 while ((*dst++ = *src++));
461 }
462 if (target_is_emmc_boot()) {
463 src = emmc_cmdline;
464 if (have_cmdline) --dst;
David Ngf773dde2010-07-26 19:55:08 -0700465 have_cmdline = 1;
466 while ((*dst++ = *src++));
Sridhar Parasuram7bd4aaf2015-02-12 11:14:38 -0800467#if USE_BOOTDEV_CMDLINE
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700468 src = boot_dev_buf;
469 if (have_cmdline) --dst;
470 while ((*dst++ = *src++));
471#endif
David Ngf773dde2010-07-26 19:55:08 -0700472 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800473
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700474#if VERIFIED_BOOT
Parth Dixitddbc7352015-10-18 03:13:31 +0530475#if !VBOOT_MOTA
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700476 src = verified_state;
477 if(have_cmdline) --dst;
478 have_cmdline = 1;
479 while ((*dst++ = *src++));
480 src = vbsn[boot_state].name;
481 if(have_cmdline) --dst;
482 while ((*dst++ = *src++));
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700483
Sridhar Parasuram7d8bb9a2015-09-23 18:21:19 -0700484 if ((device.verity_mode != 0 ) && (device.verity_mode != 1))
485 {
486 dprintf(CRITICAL, "Devinfo paritition possibly corrupted!!!. Please erase devinfo partition to continue booting\n");
487 ASSERT(0);
488 }
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700489 src = verity_mode;
490 if(have_cmdline) --dst;
491 while ((*dst++ = *src++));
492 src = vbvm[device.verity_mode].name;
493 if(have_cmdline) -- dst;
494 while ((*dst++ = *src++));
Parth Dixita5715a02015-10-29 12:25:10 +0530495 src = keymaster_v1;
496 if(have_cmdline) --dst;
497 while ((*dst++ = *src++));
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700498#endif
Parth Dixitddbc7352015-10-18 03:13:31 +0530499#endif
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800500 src = usb_sn_cmdline;
501 if (have_cmdline) --dst;
502 have_cmdline = 1;
503 while ((*dst++ = *src++));
504 src = sn_buf;
505 if (have_cmdline) --dst;
506 have_cmdline = 1;
507 while ((*dst++ = *src++));
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800508 if (warm_boot) {
509 if (have_cmdline) --dst;
510 src = warmboot_cmdline;
511 while ((*dst++ = *src++));
512 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800513
Pavel Nedev5614d222013-06-17 18:01:02 +0300514 if (boot_into_recovery && gpt_exists) {
515 src = secondary_gpt_enable;
516 if (have_cmdline) --dst;
517 while ((*dst++ = *src++));
518 }
519
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200520 if (is_mdtp_activated) {
521 src = mdtp_activated_flag;
522 if (have_cmdline) --dst;
523 while ((*dst++ = *src++));
524 }
525
Pavel Nedev328ac822013-04-05 15:25:11 +0300526 if (boot_into_ffbm) {
527 src = androidboot_mode;
528 if (have_cmdline) --dst;
529 while ((*dst++ = *src++));
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700530 src = ffbm_mode_string;
Pavel Nedev328ac822013-04-05 15:25:11 +0300531 if (have_cmdline) --dst;
532 while ((*dst++ = *src++));
Pavel Nedev898298c2013-02-27 12:36:09 -0800533 src = loglevel;
534 if (have_cmdline) --dst;
535 while ((*dst++ = *src++));
Matthew Qind886f3c2014-01-17 16:52:01 +0800536 } else if (boot_reason_alarm) {
537 src = alarmboot_cmdline;
538 if (have_cmdline) --dst;
539 while ((*dst++ = *src++));
Pavel Nedev328ac822013-04-05 15:25:11 +0300540 } else if (pause_at_bootup) {
David Ngf773dde2010-07-26 19:55:08 -0700541 src = battchg_pause;
542 if (have_cmdline) --dst;
David Ng183a7422009-12-07 14:55:21 -0800543 while ((*dst++ = *src++));
544 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800545
Shashank Mittalcd98d472011-08-02 14:29:24 -0700546 if(target_use_signed_kernel() && auth_kernel_img) {
547 src = auth_kernel;
548 if (have_cmdline) --dst;
549 while ((*dst++ = *src++));
550 }
551
Ajay Dudanid04110c2011-01-17 23:55:07 -0800552 switch(target_baseband())
553 {
554 case BASEBAND_APQ:
555 src = baseband_apq;
556 if (have_cmdline) --dst;
557 while ((*dst++ = *src++));
558 break;
559
560 case BASEBAND_MSM:
561 src = baseband_msm;
562 if (have_cmdline) --dst;
563 while ((*dst++ = *src++));
564 break;
565
566 case BASEBAND_CSFB:
567 src = baseband_csfb;
568 if (have_cmdline) --dst;
569 while ((*dst++ = *src++));
570 break;
571
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800572 case BASEBAND_SVLTE2A:
573 src = baseband_svlte2a;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800574 if (have_cmdline) --dst;
575 while ((*dst++ = *src++));
576 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700577
578 case BASEBAND_MDM:
579 src = baseband_mdm;
580 if (have_cmdline) --dst;
581 while ((*dst++ = *src++));
582 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700583
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800584 case BASEBAND_MDM2:
585 src = baseband_mdm2;
586 if (have_cmdline) --dst;
587 while ((*dst++ = *src++));
588 break;
589
Amol Jadi5c61a952012-05-04 17:05:35 -0700590 case BASEBAND_SGLTE:
591 src = baseband_sglte;
592 if (have_cmdline) --dst;
593 while ((*dst++ = *src++));
594 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530595
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800596 case BASEBAND_SGLTE2:
597 src = baseband_sglte2;
598 if (have_cmdline) --dst;
599 while ((*dst++ = *src++));
600 break;
601
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530602 case BASEBAND_DSDA:
603 src = baseband_dsda;
604 if (have_cmdline) --dst;
605 while ((*dst++ = *src++));
606 break;
Amol Jadi2a15a272013-01-22 12:03:36 -0800607
608 case BASEBAND_DSDA2:
609 src = baseband_dsda2;
610 if (have_cmdline) --dst;
611 while ((*dst++ = *src++));
612 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800613 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700614
615 if (strlen(display_panel_buf)) {
Dhaval Patel223ec952013-07-18 14:49:44 -0700616 src = display_panel_buf;
617 if (have_cmdline) --dst;
618 while ((*dst++ = *src++));
619 }
Joonwoo Park61112782013-10-02 19:50:39 -0700620
621 if (have_target_boot_params) {
622 if (have_cmdline) --dst;
623 src = target_boot_params;
624 while ((*dst++ = *src++));
vijay kumar870515d2015-08-31 16:37:24 +0530625 free(target_boot_params);
Joonwoo Park61112782013-10-02 19:50:39 -0700626 }
Neeti Desaie245d492012-06-01 12:52:13 -0700627 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700628
629
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700630 if (boot_dev_buf)
631 free(boot_dev_buf);
632
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -0800633 if (cmdline_final)
634 dprintf(INFO, "cmdline: %s\n", cmdline_final);
635 else
636 dprintf(INFO, "cmdline is NULL\n");
Neeti Desaie245d492012-06-01 12:52:13 -0700637 return cmdline_final;
638}
639
640unsigned *atag_core(unsigned *ptr)
641{
642 /* CORE */
643 *ptr++ = 2;
644 *ptr++ = 0x54410001;
645
646 return ptr;
647
648}
649
650unsigned *atag_ramdisk(unsigned *ptr, void *ramdisk,
651 unsigned ramdisk_size)
652{
653 if (ramdisk_size) {
654 *ptr++ = 4;
655 *ptr++ = 0x54420005;
656 *ptr++ = (unsigned)ramdisk;
657 *ptr++ = ramdisk_size;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800658 }
659
Neeti Desaie245d492012-06-01 12:52:13 -0700660 return ptr;
661}
662
663unsigned *atag_ptable(unsigned **ptr_addr)
664{
665 int i;
666 struct ptable *ptable;
667
668 if ((ptable = flash_get_ptable()) && (ptable->count != 0)) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700669 *(*ptr_addr)++ = 2 + (ptable->count * (sizeof(struct atag_ptbl_entry) /
670 sizeof(unsigned)));
Neeti Desaie245d492012-06-01 12:52:13 -0700671 *(*ptr_addr)++ = 0x4d534d70;
672 for (i = 0; i < ptable->count; ++i)
673 ptentry_to_tag(ptr_addr, ptable_get(ptable, i));
674 }
675
676 return (*ptr_addr);
677}
678
679unsigned *atag_cmdline(unsigned *ptr, const char *cmdline)
680{
681 int cmdline_length = 0;
682 int n;
Neeti Desaie245d492012-06-01 12:52:13 -0700683 char *dest;
684
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800685 cmdline_length = strlen((const char*)cmdline);
Neeti Desaie245d492012-06-01 12:52:13 -0700686 n = (cmdline_length + 4) & (~3);
687
688 *ptr++ = (n / 4) + 2;
689 *ptr++ = 0x54410009;
690 dest = (char *) ptr;
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800691 while ((*dest++ = *cmdline++));
Neeti Desaie245d492012-06-01 12:52:13 -0700692 ptr += (n / 4);
693
694 return ptr;
695}
696
697unsigned *atag_end(unsigned *ptr)
698{
Brian Swetland9c4c0752009-01-25 16:23:50 -0800699 /* END */
700 *ptr++ = 0;
701 *ptr++ = 0;
702
Neeti Desaie245d492012-06-01 12:52:13 -0700703 return ptr;
704}
705
706void generate_atags(unsigned *ptr, const char *cmdline,
707 void *ramdisk, unsigned ramdisk_size)
708{
709
710 ptr = atag_core(ptr);
711 ptr = atag_ramdisk(ptr, ramdisk, ramdisk_size);
712 ptr = target_atag_mem(ptr);
713
714 /* Skip NAND partition ATAGS for eMMC boot */
715 if (!target_is_emmc_boot()){
716 ptr = atag_ptable(&ptr);
717 }
718
719 ptr = atag_cmdline(ptr, cmdline);
720 ptr = atag_end(ptr);
721}
722
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700723typedef void entry_func_ptr(unsigned, unsigned, unsigned*);
Neeti Desaie245d492012-06-01 12:52:13 -0700724void boot_linux(void *kernel, unsigned *tags,
725 const char *cmdline, unsigned machtype,
726 void *ramdisk, unsigned ramdisk_size)
727{
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800728 unsigned char *final_cmdline;
Amol Jadib6be5c12012-11-14 13:39:51 -0800729#if DEVICE_TREE
Neeti Desai17379b82012-06-04 18:42:53 -0700730 int ret = 0;
Amol Jadib6be5c12012-11-14 13:39:51 -0800731#endif
732
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700733 void (*entry)(unsigned, unsigned, unsigned*) = (entry_func_ptr*)(PA((addr_t)kernel));
Deepa Dinamani25a9f762012-11-30 15:57:15 -0800734 uint32_t tags_phys = PA((addr_t)tags);
vijay kumar1a50a642015-11-16 12:41:15 +0530735 struct kernel64_hdr *kptr = ((struct kernel64_hdr*)(PA((addr_t)kernel)));
Deepa Dinamani25a9f762012-11-30 15:57:15 -0800736
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530737 ramdisk = (void *)PA((addr_t)ramdisk);
Neeti Desaie245d492012-06-01 12:52:13 -0700738
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800739 final_cmdline = update_cmdline((const char*)cmdline);
740
Neeti Desai17379b82012-06-04 18:42:53 -0700741#if DEVICE_TREE
Amol Jadib6be5c12012-11-14 13:39:51 -0800742 dprintf(INFO, "Updating device tree: start\n");
743
Neeti Desai17379b82012-06-04 18:42:53 -0700744 /* Update the Device Tree */
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530745 ret = update_device_tree((void *)tags,(const char *)final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -0700746 if(ret)
747 {
748 dprintf(CRITICAL, "ERROR: Updating Device Tree Failed \n");
749 ASSERT(0);
750 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800751 dprintf(INFO, "Updating device tree: done\n");
Neeti Desai17379b82012-06-04 18:42:53 -0700752#else
Neeti Desaie245d492012-06-01 12:52:13 -0700753 /* Generating the Atags */
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800754 generate_atags(tags, final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -0700755#endif
Neeti Desaie245d492012-06-01 12:52:13 -0700756
Maria Yu52254c02014-07-04 16:14:54 +0800757 free(final_cmdline);
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -0700758
759#if VERIFIED_BOOT
lijuangbdd9bb42016-03-01 18:22:17 +0800760#if !VBOOT_MOTA
761 if (device.verity_mode == 0) {
762#if FBCON_DISPLAY_MSG
763 display_bootverify_menu(DISPLAY_MENU_LOGGING);
764 wait_for_users_action();
765#else
766 dprintf(CRITICAL,
767 "The dm-verity is not started in enforcing mode.\nWait for 5 seconds before proceeding\n");
768 mdelay(5000);
769#endif
770 }
771
772#endif
773#endif
774
775#if VERIFIED_BOOT
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -0700776 /* Write protect the device info */
Channagoud Kadabi3bd9d1e2015-05-05 16:18:20 -0700777 if (!boot_into_recovery && target_build_variant_user() && devinfo_present && mmc_write_protect("devinfo", 1))
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -0700778 {
779 dprintf(INFO, "Failed to write protect dev info\n");
780 ASSERT(0);
781 }
782#endif
783
Aravind Venkateswaran8f076242014-02-25 16:25:30 -0800784 /* Turn off splash screen if enabled */
785#if DISPLAY_SPLASH_SCREEN
786 target_display_shutdown();
787#endif
788
Veera Sundaram Sankaran67ea0932015-09-25 10:09:30 -0700789 /* Perform target specific cleanup */
790 target_uninit();
Aravind Venkateswaran8f076242014-02-25 16:25:30 -0800791
Deepa Dinamani33734bc2013-03-06 12:16:06 -0800792 dprintf(INFO, "booting linux @ %p, ramdisk @ %p (%d), tags/device tree @ %p\n",
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530793 entry, ramdisk, ramdisk_size, (void *)tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800794
795 enter_critical_section();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700796
Amol Jadi4421e652011-06-16 15:00:48 -0700797 /* do any platform specific cleanup before kernel entry */
798 platform_uninit();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700799
Brian Swetland9c4c0752009-01-25 16:23:50 -0800800 arch_disable_cache(UCACHE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700801
Amol Jadi504f9fe2012-08-16 13:56:48 -0700802#if ARM_WITH_MMU
Brian Swetland9c4c0752009-01-25 16:23:50 -0800803 arch_disable_mmu();
Amol Jadi504f9fe2012-08-16 13:56:48 -0700804#endif
Amol Jadi492d5a52013-03-15 16:12:34 -0700805 bs_set_timestamp(BS_KERNEL_ENTRY);
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800806
807 if (IS_ARM64(kptr))
808 /* Jump to a 64bit kernel */
809 scm_elexec_call((paddr_t)kernel, tags_phys);
810 else
811 /* Jump to a 32bit kernel */
812 entry(0, machtype, (unsigned*)tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800813}
814
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700815/* Function to check if the memory address range falls within the aboot
816 * boundaries.
817 * start: Start of the memory region
818 * size: Size of the memory region
819 */
820int check_aboot_addr_range_overlap(uint32_t start, uint32_t size)
821{
822 /* Check for boundary conditions. */
Sundarajan Srinivasance2a0ea2013-12-16 17:02:56 -0800823 if ((UINT_MAX - start) < size)
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700824 return -1;
825
826 /* Check for memory overlap. */
827 if ((start < MEMBASE) && ((start + size) <= MEMBASE))
828 return 0;
Channagoud Kadabi94143912013-10-15 12:53:52 -0700829 else if (start >= (MEMBASE + MEMSIZE))
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700830 return 0;
831 else
832 return -1;
833}
834
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800835#define ROUND_TO_PAGE(x,y) (((x) + (y)) & (~(y)))
Brian Swetland9c4c0752009-01-25 16:23:50 -0800836
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800837BUF_DMA_ALIGN(buf, BOOT_IMG_MAX_PAGE_SIZE); //Equal to max-supported pagesize
Amol Jadib6be5c12012-11-14 13:39:51 -0800838#if DEVICE_TREE
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800839BUF_DMA_ALIGN(dt_buf, BOOT_IMG_MAX_PAGE_SIZE);
Amol Jadib6be5c12012-11-14 13:39:51 -0800840#endif
Dima Zavin214cc642009-01-26 11:16:21 -0800841
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700842static void verify_signed_bootimg(uint32_t bootimg_addr, uint32_t bootimg_size)
843{
844 int ret;
Channagoud Kadabia8c623f2015-01-13 14:48:48 -0800845
846#if !VERIFIED_BOOT
Sundarajan Srinivasance54d6e2013-11-11 12:45:00 -0800847#if IMAGE_VERIF_ALGO_SHA1
848 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA1;
849#else
850 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA256;
851#endif
Channagoud Kadabia8c623f2015-01-13 14:48:48 -0800852#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700853
854 /* Assume device is rooted at this time. */
855 device.is_tampered = 1;
856
857 dprintf(INFO, "Authenticating boot image (%d): start\n", bootimg_size);
858
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700859#if VERIFIED_BOOT
860 if(boot_into_recovery)
861 {
862 ret = boot_verify_image((unsigned char *)bootimg_addr,
Unnati Gandhi47051252015-03-02 15:21:09 +0530863 bootimg_size, "/recovery");
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700864 }
865 else
866 {
867 ret = boot_verify_image((unsigned char *)bootimg_addr,
Unnati Gandhi47051252015-03-02 15:21:09 +0530868 bootimg_size, "/boot");
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700869 }
870 boot_verify_print_state();
871#else
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700872 ret = image_verify((unsigned char *)bootimg_addr,
873 (unsigned char *)(bootimg_addr + bootimg_size),
874 bootimg_size,
Sundarajan Srinivasance54d6e2013-11-11 12:45:00 -0800875 auth_algo);
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700876#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700877 dprintf(INFO, "Authenticating boot image: done return value = %d\n", ret);
878
879 if (ret)
880 {
881 /* Authorized kernel */
882 device.is_tampered = 0;
Sundarajan Srinivasan3fb21f12013-09-16 18:36:15 -0700883 auth_kernel_img = 1;
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700884 }
885
Amit Blay4aa292f2015-04-28 21:55:59 +0300886#ifdef MDTP_SUPPORT
887 {
888 /* Verify MDTP lock.
889 * For boot & recovery partitions, use aboot's verification result.
890 */
891 mdtp_ext_partition_verification_t ext_partition;
892 ext_partition.partition = boot_into_recovery ? MDTP_PARTITION_RECOVERY : MDTP_PARTITION_BOOT;
893 ext_partition.integrity_state = device.is_tampered ? MDTP_PARTITION_STATE_INVALID : MDTP_PARTITION_STATE_VALID;
894 ext_partition.page_size = 0; /* Not needed since already validated */
895 ext_partition.image_addr = 0; /* Not needed since already validated */
896 ext_partition.image_size = 0; /* Not needed since already validated */
897 ext_partition.sig_avail = FALSE; /* Not needed since already validated */
898 mdtp_fwlock_verify_lock(&ext_partition);
899 }
900#endif /* MDTP_SUPPORT */
901
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700902#if USE_PCOM_SECBOOT
903 set_tamper_flag(device.is_tampered);
904#endif
905
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700906#if VERIFIED_BOOT
Sridhar Parasuram8b792422015-07-05 11:38:13 -0700907 switch(boot_verify_get_state())
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700908 {
Sridhar Parasuram8b792422015-07-05 11:38:13 -0700909 case RED:
lijuanga40d6302015-07-20 20:10:13 +0800910#if FBCON_DISPLAY_MSG
lijuang9a7d3b92015-11-30 14:41:24 +0800911 display_bootverify_menu(DISPLAY_MENU_RED);
lijuanga40d6302015-07-20 20:10:13 +0800912 wait_for_users_action();
913#else
914 dprintf(CRITICAL,
915 "Your device has failed verification and may not work properly.\nWait for 5 seconds before proceeding\n");
916 mdelay(5000);
917#endif
918
919 break;
Sridhar Parasuram8b792422015-07-05 11:38:13 -0700920 case YELLOW:
lijuanga40d6302015-07-20 20:10:13 +0800921#if FBCON_DISPLAY_MSG
lijuang9a7d3b92015-11-30 14:41:24 +0800922 display_bootverify_menu(DISPLAY_MENU_YELLOW);
lijuanga40d6302015-07-20 20:10:13 +0800923 wait_for_users_action();
924#else
925 dprintf(CRITICAL,
926 "Your device has loaded a different operating system.\nWait for 5 seconds before proceeding\n");
927 mdelay(5000);
928#endif
929 break;
Sridhar Parasuram8b792422015-07-05 11:38:13 -0700930 default:
lijuanga40d6302015-07-20 20:10:13 +0800931 break;
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700932 }
933#endif
Sridhar Parasuram8b792422015-07-05 11:38:13 -0700934#if !VERIFIED_BOOT
Unnati Gandhi1be04752015-03-27 19:41:53 +0530935 if(device.is_tampered)
936 {
937 write_device_info_mmc(&device);
938 #ifdef TZ_TAMPER_FUSE
939 set_tamper_fuse_cmd();
940 #endif
941 #ifdef ASSERT_ON_TAMPER
942 dprintf(CRITICAL, "Device is tampered. Asserting..\n");
943 ASSERT(0);
944 #endif
945 }
Sridhar Parasuram8b792422015-07-05 11:38:13 -0700946#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700947}
948
Maunik Shah0f3c8ac2014-03-02 17:47:58 +0530949static bool check_format_bit()
950{
951 bool ret = false;
952 int index;
953 uint64_t offset;
954 struct boot_selection_info *in = NULL;
955 char *buf = NULL;
956
957 index = partition_get_index("bootselect");
958 if (index == INVALID_PTN)
959 {
960 dprintf(INFO, "Unable to locate /bootselect partition\n");
961 return ret;
962 }
963 offset = partition_get_offset(index);
964 if(!offset)
965 {
966 dprintf(INFO, "partition /bootselect doesn't exist\n");
967 return ret;
968 }
969 buf = (char *) memalign(CACHE_LINE, ROUNDUP(page_size, CACHE_LINE));
970 ASSERT(buf);
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530971 if (mmc_read(offset, (uint32_t *)buf, page_size))
Maunik Shah0f3c8ac2014-03-02 17:47:58 +0530972 {
973 dprintf(INFO, "mmc read failure /bootselect %d\n", page_size);
974 free(buf);
975 return ret;
976 }
977 in = (struct boot_selection_info *) buf;
978 if ((in->signature == BOOTSELECT_SIGNATURE) &&
979 (in->version == BOOTSELECT_VERSION)) {
980 if ((in->state_info & BOOTSELECT_FORMAT) &&
981 !(in->state_info & BOOTSELECT_FACTORY))
982 ret = true;
983 } else {
984 dprintf(CRITICAL, "Signature: 0x%08x or version: 0x%08x mismatched of /bootselect\n",
985 in->signature, in->version);
986 ASSERT(0);
987 }
988 free(buf);
989 return ret;
990}
991
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700992void boot_verifier_init()
993{
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700994 uint32_t boot_state;
995 /* Check if device unlock */
996 if(device.is_unlocked)
997 {
998 boot_verify_send_event(DEV_UNLOCK);
999 boot_verify_print_state();
1000 dprintf(CRITICAL, "Device is unlocked! Skipping verification...\n");
1001 return;
1002 }
1003 else
1004 {
1005 boot_verify_send_event(BOOT_INIT);
1006 }
1007
1008 /* Initialize keystore */
1009 boot_state = boot_verify_keystore_init();
1010 if(boot_state == YELLOW)
1011 {
1012 boot_verify_print_state();
1013 dprintf(CRITICAL, "Keystore verification failed! Continuing anyways...\n");
1014 }
1015}
1016
Shashank Mittal23b8f422010-04-16 19:27:21 -07001017int boot_linux_from_mmc(void)
1018{
1019 struct boot_img_hdr *hdr = (void*) buf;
1020 struct boot_img_hdr *uhdr;
1021 unsigned offset = 0;
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07001022 int rcode;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001023 unsigned long long ptn = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001024 int index = INVALID_PTN;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001025
Shashank Mittalcd98d472011-08-02 14:29:24 -07001026 unsigned char *image_addr = 0;
1027 unsigned kernel_actual;
1028 unsigned ramdisk_actual;
1029 unsigned imagesize_actual;
Neeti Desai465491e2012-07-31 12:53:35 -07001030 unsigned second_actual = 0;
Neeti Desai465491e2012-07-31 12:53:35 -07001031
Matthew Qin271927e2015-03-31 22:07:07 -04001032 unsigned int dtb_size = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08001033 unsigned int out_len = 0;
1034 unsigned int out_avai_len = 0;
1035 unsigned char *out_addr = NULL;
1036 uint32_t dtb_offset = 0;
1037 unsigned char *kernel_start_addr = NULL;
1038 unsigned int kernel_size = 0;
1039 int rc;
1040
Neeti Desai465491e2012-07-31 12:53:35 -07001041#if DEVICE_TREE
1042 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -07001043 struct dt_entry dt_entry;
Neeti Desai465491e2012-07-31 12:53:35 -07001044 unsigned dt_table_offset;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001045 uint32_t dt_actual;
Deepa Dinamani19648b42013-09-05 17:05:55 -07001046 uint32_t dt_hdr_size;
Matthew Qin271927e2015-03-31 22:07:07 -04001047 unsigned char *best_match_dt_addr = NULL;
Neeti Desai465491e2012-07-31 12:53:35 -07001048#endif
Matthew Qin49e51fa2015-02-09 10:40:45 +08001049 struct kernel64_hdr *kptr = NULL;
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001050
Maunik Shah0f3c8ac2014-03-02 17:47:58 +05301051 if (check_format_bit())
1052 boot_into_recovery = 1;
1053
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07001054 if (!boot_into_recovery) {
1055 memset(ffbm_mode_string, '\0', sizeof(ffbm_mode_string));
1056 rcode = get_ffbm(ffbm_mode_string, sizeof(ffbm_mode_string));
1057 if (rcode <= 0) {
1058 boot_into_ffbm = false;
1059 if (rcode < 0)
1060 dprintf(CRITICAL,"failed to get ffbm cookie");
1061 } else
1062 boot_into_ffbm = true;
1063 } else
1064 boot_into_ffbm = false;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001065 uhdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
1066 if (!memcmp(uhdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1067 dprintf(INFO, "Unified boot method!\n");
1068 hdr = uhdr;
1069 goto unified_boot;
1070 }
Greg Griscod6250552011-06-29 14:40:23 -07001071 if (!boot_into_recovery) {
Kinson Chikf1a43512011-07-14 11:28:39 -07001072 index = partition_get_index("boot");
1073 ptn = partition_get_offset(index);
1074 if(ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -07001075 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1076 return -1;
1077 }
Kinson Chikf1a43512011-07-14 11:28:39 -07001078 }
1079 else {
1080 index = partition_get_index("recovery");
1081 ptn = partition_get_offset(index);
1082 if(ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -07001083 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
1084 return -1;
1085 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001086 }
Channagoud Kadabief0547c2015-02-10 12:57:38 -08001087 /* Set Lun for boot & recovery partitions */
1088 mmc_set_lun(partition_get_lun(index));
Shashank Mittal23b8f422010-04-16 19:27:21 -07001089
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301090 if (mmc_read(ptn + offset, (uint32_t *) buf, page_size)) {
Shashank Mittal23b8f422010-04-16 19:27:21 -07001091 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
1092 return -1;
1093 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001094
1095 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001096 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Shashank Mittal23b8f422010-04-16 19:27:21 -07001097 return -1;
1098 }
1099
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07001100 if (hdr->page_size && (hdr->page_size != page_size)) {
vijay kumar2e21b3a2014-06-26 17:40:15 +05301101
1102 if (hdr->page_size > BOOT_IMG_MAX_PAGE_SIZE) {
1103 dprintf(CRITICAL, "ERROR: Invalid page size\n");
1104 return -1;
1105 }
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07001106 page_size = hdr->page_size;
1107 page_mask = page_size - 1;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001108 }
1109
vijay kumar287bb542015-09-29 13:01:52 +05301110 /* ensure commandline is terminated */
1111 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
1112
Matthew Qin49e51fa2015-02-09 10:40:45 +08001113 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1114 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001115
Matthew Qin49e51fa2015-02-09 10:40:45 +08001116 image_addr = (unsigned char *)target_get_scratch_address();
1117
1118#if DEVICE_TREE
1119 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
1120 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + dt_actual);
1121#else
1122 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
1123#endif
1124
1125#if VERIFIED_BOOT
1126 boot_verifier_init();
1127#endif
1128
1129 if (check_aboot_addr_range_overlap((uint32_t) image_addr, imagesize_actual))
1130 {
1131 dprintf(CRITICAL, "Boot image buffer address overlaps with aboot addresses.\n");
1132 return -1;
1133 }
1134
Matthew Qinbb7923d2015-02-09 10:56:09 +08001135 /*
1136 * Update loading flow of bootimage to support compressed/uncompressed
1137 * bootimage on both 64bit and 32bit platform.
1138 * 1. Load bootimage from emmc partition onto DDR.
1139 * 2. Check if bootimage is gzip format. If yes, decompress compressed kernel
1140 * 3. Check kernel header and update kernel load addr for 64bit and 32bit
1141 * platform accordingly.
1142 * 4. Sanity Check on kernel_addr and ramdisk_addr and copy data.
1143 */
1144
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001145 dprintf(INFO, "Loading (%s) image (%d): start\n",
1146 (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001147 bs_set_timestamp(BS_KERNEL_LOAD_START);
1148
Matthew Qinbb7923d2015-02-09 10:56:09 +08001149 /* Read image without signature */
Matthew Qin49e51fa2015-02-09 10:40:45 +08001150 if (mmc_read(ptn + offset, (void *)image_addr, imagesize_actual))
1151 {
1152 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
1153 return -1;
1154 }
1155
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001156 dprintf(INFO, "Loading (%s) image (%d): done\n",
1157 (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual);
1158
Matthew Qin49e51fa2015-02-09 10:40:45 +08001159 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
1160
1161 /* Authenticate Kernel */
1162 dprintf(INFO, "use_signed_kernel=%d, is_unlocked=%d, is_tampered=%d.\n",
1163 (int) target_use_signed_kernel(),
1164 device.is_unlocked,
1165 device.is_tampered);
1166
Channagoud Kadabi736c4962015-08-21 11:56:52 -07001167 /* Change the condition a little bit to include the test framework support.
1168 * We would never reach this point if device is in fastboot mode, even if we did
1169 * that means we are in test mode, so execute kernel authentication part for the
1170 * tests */
Parth Dixitcb0c6082015-12-30 15:01:13 +05301171 if((target_use_signed_kernel() && (!device.is_unlocked)) || is_test_mode_enabled())
Matthew Qin49e51fa2015-02-09 10:40:45 +08001172 {
1173 offset = imagesize_actual;
1174 if (check_aboot_addr_range_overlap((uint32_t)image_addr + offset, page_size))
1175 {
1176 dprintf(CRITICAL, "Signature read buffer address overlaps with aboot addresses.\n");
1177 return -1;
1178 }
1179
1180 /* Read signature */
1181 if(mmc_read(ptn + offset, (void *)(image_addr + offset), page_size))
1182 {
1183 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
1184 return -1;
1185 }
1186
1187 verify_signed_bootimg((uint32_t)image_addr, imagesize_actual);
Channagoud Kadabi736c4962015-08-21 11:56:52 -07001188 /* The purpose of our test is done here */
Parth Dixitcb0c6082015-12-30 15:01:13 +05301189 if(is_test_mode_enabled() && auth_kernel_img)
Channagoud Kadabi736c4962015-08-21 11:56:52 -07001190 return 0;
Matthew Qin49e51fa2015-02-09 10:40:45 +08001191 } else {
1192 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
1193 #ifdef TZ_SAVE_KERNEL_HASH
1194 aboot_save_boot_hash_mmc((uint32_t) image_addr, imagesize_actual);
1195 #endif /* TZ_SAVE_KERNEL_HASH */
Amit Blay4aa292f2015-04-28 21:55:59 +03001196
1197#ifdef MDTP_SUPPORT
1198 {
1199 /* Verify MDTP lock.
1200 * For boot & recovery partitions, MDTP will use boot_verifier APIs,
1201 * since verification was skipped in aboot. The signature is not part of the loaded image.
1202 */
1203 mdtp_ext_partition_verification_t ext_partition;
1204 ext_partition.partition = boot_into_recovery ? MDTP_PARTITION_RECOVERY : MDTP_PARTITION_BOOT;
1205 ext_partition.integrity_state = MDTP_PARTITION_STATE_UNSET;
1206 ext_partition.page_size = page_size;
1207 ext_partition.image_addr = (uint32)image_addr;
1208 ext_partition.image_size = imagesize_actual;
1209 ext_partition.sig_avail = FALSE;
1210 mdtp_fwlock_verify_lock(&ext_partition);
1211 }
1212#endif /* MDTP_SUPPORT */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001213 }
1214
Sridhar Parasuram00bfedb2015-05-26 14:21:27 -07001215#if VERIFIED_BOOT
Reut Zysmanba8a9d52016-02-18 11:44:04 +02001216 if(boot_verify_get_state() == ORANGE)
1217 {
1218#if FBCON_DISPLAY_MSG
1219 display_bootverify_menu(DISPLAY_MENU_ORANGE);
1220 wait_for_users_action();
1221#else
1222 dprintf(CRITICAL,
1223 "Your device has been unlocked and can't be trusted.\nWait for 5 seconds before proceeding\n");
1224 mdelay(5000);
1225#endif
1226 }
1227#endif
1228
1229#if VERIFIED_BOOT
Parth Dixitbc9b6492015-10-18 00:41:38 +05301230#if !VBOOT_MOTA
Sridhar Parasuram00bfedb2015-05-26 14:21:27 -07001231 // send root of trust
Sridhar Parasuram96300dc2015-06-11 10:37:11 -07001232 if(!send_rot_command((uint32_t)device.is_unlocked))
Sridhar Parasuram00bfedb2015-05-26 14:21:27 -07001233 ASSERT(0);
1234#endif
Parth Dixitbc9b6492015-10-18 00:41:38 +05301235#endif
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001236 /*
Matthew Qinbb7923d2015-02-09 10:56:09 +08001237 * Check if the kernel image is a gzip package. If yes, need to decompress it.
1238 * If not, continue booting.
1239 */
1240 if (is_gzip_package((unsigned char *)(image_addr + page_size), hdr->kernel_size))
1241 {
1242 out_addr = (unsigned char *)(image_addr + imagesize_actual + page_size);
1243 out_avai_len = target_get_max_flash_size() - imagesize_actual - page_size;
Matthew Qin0b15b322015-05-19 05:20:54 -04001244 dprintf(INFO, "decompressing kernel image: start\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001245 rc = decompress((unsigned char *)(image_addr + page_size),
1246 hdr->kernel_size, out_addr, out_avai_len,
1247 &dtb_offset, &out_len);
1248 if (rc)
1249 {
Matthew Qin0b15b322015-05-19 05:20:54 -04001250 dprintf(CRITICAL, "decompressing kernel image failed!!!\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001251 ASSERT(0);
1252 }
1253
Matthew Qin0b15b322015-05-19 05:20:54 -04001254 dprintf(INFO, "decompressing kernel image: done\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001255 kptr = (struct kernel64_hdr *)out_addr;
1256 kernel_start_addr = out_addr;
1257 kernel_size = out_len;
1258 } else {
1259 kptr = (struct kernel64_hdr *)(image_addr + page_size);
1260 kernel_start_addr = (unsigned char *)(image_addr + page_size);
1261 kernel_size = hdr->kernel_size;
1262 }
1263
1264 /*
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001265 * Update the kernel/ramdisk/tags address if the boot image header
1266 * has default values, these default values come from mkbootimg when
1267 * the boot image is flashed using fastboot flash:raw
1268 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001269 update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr));
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001270
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001271 /* Get virtual addresses since the hdr saves physical addresses. */
1272 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
1273 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
1274 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001275
Matthew Qinbb7923d2015-02-09 10:56:09 +08001276 kernel_size = ROUND_TO_PAGE(kernel_size, page_mask);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001277 /* Check if the addresses in the header are valid. */
Matthew Qinbb7923d2015-02-09 10:56:09 +08001278 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_size) ||
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001279 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
1280 {
1281 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
1282 return -1;
1283 }
1284
1285#ifndef DEVICE_TREE
1286 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1287 {
1288 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
1289 return -1;
1290 }
1291#endif
1292
Matthew Qin49e51fa2015-02-09 10:40:45 +08001293 /* Move kernel, ramdisk and device tree to correct address */
Matthew Qinbb7923d2015-02-09 10:56:09 +08001294 memmove((void*) hdr->kernel_addr, kernel_start_addr, kernel_size);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001295 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001296
Matthew Qin49e51fa2015-02-09 10:40:45 +08001297 #if DEVICE_TREE
1298 if(hdr->dt_size) {
1299 dt_table_offset = ((uint32_t)image_addr + page_size + kernel_actual + ramdisk_actual + second_actual);
1300 table = (struct dt_table*) dt_table_offset;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001301
Matthew Qin49e51fa2015-02-09 10:40:45 +08001302 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
1303 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1304 return -1;
1305 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001306
P.V. Phani Kumar82916762016-03-09 09:20:19 +05301307 /* Its Error if, dt_hdr_size (table->num_entries * dt_entry size + Dev_Tree Header)
1308 goes beyound hdr->dt_size*/
1309 if (dt_hdr_size > ROUND_TO_PAGE(hdr->dt_size,hdr->page_size)) {
1310 dprintf(CRITICAL, "ERROR: Invalid Device Tree size \n");
1311 return -1;
1312 }
1313
Matthew Qin49e51fa2015-02-09 10:40:45 +08001314 /* Find index of device tree within device tree table */
1315 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
1316 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1317 return -1;
1318 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001319
Matthew Qin271927e2015-03-31 22:07:07 -04001320 if (is_gzip_package((unsigned char *)dt_table_offset + dt_entry.offset, dt_entry.size))
1321 {
1322 unsigned int compressed_size = 0;
1323 out_addr += out_len;
1324 out_avai_len -= out_len;
Matthew Qin0b15b322015-05-19 05:20:54 -04001325 dprintf(INFO, "decompressing dtb: start\n");
Matthew Qin271927e2015-03-31 22:07:07 -04001326 rc = decompress((unsigned char *)dt_table_offset + dt_entry.offset,
1327 dt_entry.size, out_addr, out_avai_len,
1328 &compressed_size, &dtb_size);
1329 if (rc)
1330 {
Matthew Qin0b15b322015-05-19 05:20:54 -04001331 dprintf(CRITICAL, "decompressing dtb failed!!!\n");
Matthew Qin271927e2015-03-31 22:07:07 -04001332 ASSERT(0);
1333 }
1334
Matthew Qin0b15b322015-05-19 05:20:54 -04001335 dprintf(INFO, "decompressing dtb: done\n");
Matthew Qin271927e2015-03-31 22:07:07 -04001336 best_match_dt_addr = out_addr;
1337 } else {
1338 best_match_dt_addr = (unsigned char *)dt_table_offset + dt_entry.offset;
1339 dtb_size = dt_entry.size;
1340 }
1341
Matthew Qin49e51fa2015-02-09 10:40:45 +08001342 /* Validate and Read device device tree in the tags_addr */
Matthew Qin271927e2015-03-31 22:07:07 -04001343 if (check_aboot_addr_range_overlap(hdr->tags_addr, dtb_size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001344 {
1345 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1346 return -1;
1347 }
Shashank Mittal162244e2011-08-08 19:01:25 -07001348
Matthew Qin271927e2015-03-31 22:07:07 -04001349 memmove((void *)hdr->tags_addr, (char *)best_match_dt_addr, dtb_size);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001350 } else {
1351 /* Validate the tags_addr */
1352 if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual))
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001353 {
1354 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1355 return -1;
1356 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08001357 /*
1358 * If appended dev tree is found, update the atags with
1359 * memory address to the DTB appended location on RAM.
1360 * Else update with the atags address in the kernel header
1361 */
1362 void *dtb;
Matthew Qinbb7923d2015-02-09 10:56:09 +08001363 dtb = dev_tree_appended((void*)(image_addr + page_size),
1364 hdr->kernel_size, dtb_offset,
Matthew Qin49e51fa2015-02-09 10:40:45 +08001365 (void *)hdr->tags_addr);
1366 if (!dtb) {
1367 dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001368 return -1;
1369 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07001370 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08001371 #endif
Shashank Mittal23b8f422010-04-16 19:27:21 -07001372
Stanimir Varbanov69ec5462013-07-18 18:17:42 +03001373 if (boot_into_recovery && !device.is_unlocked && !device.is_tampered)
1374 target_load_ssd_keystore();
1375
Shashank Mittal23b8f422010-04-16 19:27:21 -07001376unified_boot:
Shashank Mittal23b8f422010-04-16 19:27:21 -07001377
Dima Zavin77e41f32013-03-06 16:10:43 -08001378 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07001379 (const char *)hdr->cmdline, board_machtype(),
Shashank Mittal23b8f422010-04-16 19:27:21 -07001380 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
1381
1382 return 0;
1383}
1384
Dima Zavin214cc642009-01-26 11:16:21 -08001385int boot_linux_from_flash(void)
1386{
1387 struct boot_img_hdr *hdr = (void*) buf;
Dima Zavin214cc642009-01-26 11:16:21 -08001388 struct ptentry *ptn;
1389 struct ptable *ptable;
1390 unsigned offset = 0;
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001391
Shashank Mittalcd98d472011-08-02 14:29:24 -07001392 unsigned char *image_addr = 0;
1393 unsigned kernel_actual;
1394 unsigned ramdisk_actual;
1395 unsigned imagesize_actual;
vijay kumar8f53e362015-11-24 13:38:11 +05301396 unsigned second_actual = 0;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001397
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001398#if DEVICE_TREE
1399 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -07001400 struct dt_entry dt_entry;
vijay kumar8f53e362015-11-24 13:38:11 +05301401 unsigned dt_table_offset;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001402 uint32_t dt_actual;
Deepa Dinamani19648b42013-09-05 17:05:55 -07001403 uint32_t dt_hdr_size;
vijay kumar8f53e362015-11-24 13:38:11 +05301404 unsigned int dtb_size = 0;
1405 unsigned char *best_match_dt_addr = NULL;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001406#endif
1407
David Ng183a7422009-12-07 14:55:21 -08001408 if (target_is_emmc_boot()) {
1409 hdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
1410 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1411 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
1412 return -1;
1413 }
1414 goto continue_boot;
1415 }
1416
Dima Zavin214cc642009-01-26 11:16:21 -08001417 ptable = flash_get_ptable();
1418 if (ptable == NULL) {
1419 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1420 return -1;
1421 }
1422
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001423 if(!boot_into_recovery)
1424 {
1425 ptn = ptable_find(ptable, "boot");
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001426
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001427 if (ptn == NULL) {
1428 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1429 return -1;
1430 }
1431 }
1432 else
1433 {
1434 ptn = ptable_find(ptable, "recovery");
1435 if (ptn == NULL) {
1436 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
1437 return -1;
1438 }
Dima Zavin214cc642009-01-26 11:16:21 -08001439 }
1440
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001441 if (flash_read(ptn, offset, buf, page_size)) {
Dima Zavin214cc642009-01-26 11:16:21 -08001442 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
1443 return -1;
1444 }
Dima Zavin214cc642009-01-26 11:16:21 -08001445
1446 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001447 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Dima Zavin214cc642009-01-26 11:16:21 -08001448 return -1;
1449 }
1450
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001451 if (hdr->page_size != page_size) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001452 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 -08001453 return -1;
1454 }
1455
vijay kumar287bb542015-09-29 13:01:52 +05301456 /* ensure commandline is terminated */
1457 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
1458
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001459 /*
1460 * Update the kernel/ramdisk/tags address if the boot image header
1461 * has default values, these default values come from mkbootimg when
1462 * the boot image is flashed using fastboot flash:raw
1463 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001464 update_ker_tags_rdisk_addr(hdr, false);
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001465
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001466 /* Get virtual addresses since the hdr saves physical addresses. */
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001467 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
1468 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
1469 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
1470
1471 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1472 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1473
1474 /* Check if the addresses in the header are valid. */
1475 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
1476 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
1477 {
1478 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
1479 return -1;
1480 }
1481
1482#ifndef DEVICE_TREE
1483 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1484 {
1485 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
1486 return -1;
1487 }
1488#endif
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001489
Shashank Mittalcd98d472011-08-02 14:29:24 -07001490 /* Authenticate Kernel */
Deepa Dinamani23b60d42013-06-24 18:10:52 -07001491 if(target_use_signed_kernel() && (!device.is_unlocked))
Shashank Mittalcd98d472011-08-02 14:29:24 -07001492 {
1493 image_addr = (unsigned char *)target_get_scratch_address();
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001494 offset = 0;
1495
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001496#if DEVICE_TREE
1497 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
vijay kumar966a9822015-12-09 18:36:09 +05301498
1499 if (UINT_MAX < ((uint64_t)kernel_actual + (uint64_t)ramdisk_actual+ (uint64_t)dt_actual + page_size)) {
1500 dprintf(CRITICAL, "Integer overflow detected in bootimage header fields\n");
1501 return -1;
1502 }
1503
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001504 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + dt_actual);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001505
1506 if (check_aboot_addr_range_overlap(hdr->tags_addr, hdr->dt_size))
1507 {
1508 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1509 return -1;
1510 }
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001511#else
vijay kumar966a9822015-12-09 18:36:09 +05301512 if (UINT_MAX < ((uint64_t)kernel_actual + (uint64_t)ramdisk_actual+ page_size)) {
1513 dprintf(CRITICAL, "Integer overflow detected in bootimage header fields\n");
1514 return -1;
1515 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07001516 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001517#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001518
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001519 dprintf(INFO, "Loading (%s) image (%d): start\n",
1520 (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001521 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -08001522
Shashank Mittalcd98d472011-08-02 14:29:24 -07001523 /* Read image without signature */
1524 if (flash_read(ptn, offset, (void *)image_addr, imagesize_actual))
1525 {
1526 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
1527 return -1;
1528 }
Dima Zavin214cc642009-01-26 11:16:21 -08001529
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001530 dprintf(INFO, "Loading (%s) image (%d): done\n",
1531 (!boot_into_recovery ? "boot" : "recovery"), imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001532 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Amol Jadib6be5c12012-11-14 13:39:51 -08001533
Shashank Mittalcd98d472011-08-02 14:29:24 -07001534 offset = imagesize_actual;
1535 /* Read signature */
1536 if (flash_read(ptn, offset, (void *)(image_addr + offset), page_size))
1537 {
1538 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001539 return -1;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001540 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07001541
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301542 verify_signed_bootimg((uint32_t)image_addr, imagesize_actual);
Shashank Mittalcd98d472011-08-02 14:29:24 -07001543
1544 /* Move kernel and ramdisk to correct address */
Matthew Qinbb7923d2015-02-09 10:56:09 +08001545 memmove((void*) hdr->kernel_addr, (char*) (image_addr + page_size), hdr->kernel_size);
1546 memmove((void*) hdr->ramdisk_addr, (char*) (image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001547#if DEVICE_TREE
vijay kumar8f53e362015-11-24 13:38:11 +05301548 if(hdr->dt_size != 0) {
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001549
vijay kumar8f53e362015-11-24 13:38:11 +05301550 dt_table_offset = ((uint32_t)image_addr + page_size + kernel_actual + ramdisk_actual + second_actual);
1551
1552 table = (struct dt_table*) dt_table_offset;
1553
1554 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0){
1555 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1556 return -1;
1557 }
1558
1559 /* Find index of device tree within device tree table */
1560 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
1561 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1562 return -1;
1563 }
1564
1565 /* Validate and Read device device tree in the "tags_add */
1566 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size)){
1567 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1568 return -1;
1569 }
1570
1571 best_match_dt_addr = (unsigned char *)table + dt_entry.offset;
1572 dtb_size = dt_entry.size;
1573 memmove((void *)hdr->tags_addr, (char *)best_match_dt_addr, dtb_size);
1574 }
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001575#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001576
1577 /* Make sure everything from scratch address is read before next step!*/
Shashank Mittala0032282011-08-26 14:50:11 -07001578 if(device.is_tampered)
Shashank Mittal162244e2011-08-08 19:01:25 -07001579 {
1580 write_device_info_flash(&device);
1581 }
Channagoud Kadabi5c86fe32012-02-16 10:58:48 +05301582#if USE_PCOM_SECBOOT
1583 set_tamper_flag(device.is_tampered);
1584#endif
Shashank Mittalcd98d472011-08-02 14:29:24 -07001585 }
1586 else
1587 {
Shashank Mittal162244e2011-08-08 19:01:25 -07001588 offset = page_size;
1589
Amol Jadib6be5c12012-11-14 13:39:51 -08001590 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1591 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1592 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
1593
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001594 dprintf(INFO, "Loading (%s) image (%d): start\n",
1595 (!boot_into_recovery ? "boot" : "recovery"), kernel_actual + ramdisk_actual);
1596
Amol Jadi492d5a52013-03-15 16:12:34 -07001597 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -08001598
1599 if (flash_read(ptn, offset, (void *)hdr->kernel_addr, kernel_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -07001600 dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
1601 return -1;
1602 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001603 offset += kernel_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001604
Amol Jadib6be5c12012-11-14 13:39:51 -08001605 if (flash_read(ptn, offset, (void *)hdr->ramdisk_addr, ramdisk_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -07001606 dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
1607 return -1;
1608 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001609 offset += ramdisk_actual;
1610
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001611 dprintf(INFO, "Loading (%s) image (%d): done\n",
1612 (!boot_into_recovery ? "boot" : "recovery"), kernel_actual + ramdisk_actual);
1613
Amol Jadi492d5a52013-03-15 16:12:34 -07001614 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001615
1616 if(hdr->second_size != 0) {
Amol Jadib6be5c12012-11-14 13:39:51 -08001617 offset += second_actual;
1618 /* Second image loading not implemented. */
1619 ASSERT(0);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001620 }
1621
1622#if DEVICE_TREE
1623 if(hdr->dt_size != 0) {
1624
1625 /* Read the device tree table into buffer */
1626 if(flash_read(ptn, offset, (void *) dt_buf, page_size)) {
1627 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
1628 return -1;
1629 }
1630
1631 table = (struct dt_table*) dt_buf;
1632
Deepa Dinamani19648b42013-09-05 17:05:55 -07001633 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001634 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1635 return -1;
1636 }
1637
P.V. Phani Kumar82916762016-03-09 09:20:19 +05301638 /* Its Error if, dt_hdr_size (table->num_entries * dt_entry size + Dev_Tree Header)
1639 goes beyound hdr->dt_size*/
1640 if (dt_hdr_size > ROUND_TO_PAGE(hdr->dt_size,hdr->page_size)) {
1641 dprintf(CRITICAL, "ERROR: Invalid Device Tree size \n");
1642 return -1;
1643 }
1644
Deepa Dinamani19648b42013-09-05 17:05:55 -07001645 table = (struct dt_table*) memalign(CACHE_LINE, dt_hdr_size);
1646 if (!table)
1647 return -1;
1648
1649 /* Read the entire device tree table into buffer */
1650 if(flash_read(ptn, offset, (void *)table, dt_hdr_size)) {
1651 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
1652 return -1;
1653 }
1654
1655
Joel Kingaa335dc2013-06-03 16:11:08 -07001656 /* Find index of device tree within device tree table */
1657 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001658 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1659 return -1;
1660 }
1661
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001662 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001663 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001664 {
1665 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1666 return -1;
1667 }
1668
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001669 /* Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001670 if(flash_read(ptn, offset + dt_entry.offset,
1671 (void *)hdr->tags_addr, dt_entry.size)) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001672 dprintf(CRITICAL, "ERROR: Cannot read device tree\n");
1673 return -1;
1674 }
1675 }
1676#endif
1677
Shashank Mittalcd98d472011-08-02 14:29:24 -07001678 }
David Ng183a7422009-12-07 14:55:21 -08001679continue_boot:
Dima Zavin214cc642009-01-26 11:16:21 -08001680
Dima Zavin214cc642009-01-26 11:16:21 -08001681 /* TODO: create/pass atags to kernel */
1682
Ajay Dudanie28a6072011-07-01 13:59:46 -07001683 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07001684 (const char *)hdr->cmdline, board_machtype(),
Dima Zavin214cc642009-01-26 11:16:21 -08001685 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
1686
1687 return 0;
1688}
Brian Swetland9c4c0752009-01-25 16:23:50 -08001689
Shashank Mittal162244e2011-08-08 19:01:25 -07001690void write_device_info_mmc(device_info *dev)
1691{
Shashank Mittal162244e2011-08-08 19:01:25 -07001692 unsigned long long ptn = 0;
1693 unsigned long long size;
1694 int index = INVALID_PTN;
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001695 uint32_t blocksize;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001696 uint8_t lun = 0;
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001697 uint32_t ret = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001698
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001699 if (devinfo_present)
1700 index = partition_get_index("devinfo");
1701 else
1702 index = partition_get_index("aboot");
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001703
Shashank Mittal162244e2011-08-08 19:01:25 -07001704 ptn = partition_get_offset(index);
1705 if(ptn == 0)
1706 {
1707 return;
1708 }
1709
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001710 lun = partition_get_lun(index);
1711 mmc_set_lun(lun);
1712
Shashank Mittal162244e2011-08-08 19:01:25 -07001713 size = partition_get_size(index);
1714
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001715 blocksize = mmc_get_device_blocksize();
1716
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001717 if (devinfo_present)
1718 ret = mmc_write(ptn, blocksize, (void *)info_buf);
1719 else
1720 ret = mmc_write((ptn + size - blocksize), blocksize, (void *)info_buf);
1721 if (ret)
Shashank Mittal162244e2011-08-08 19:01:25 -07001722 {
1723 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
Sridhar Parasurame1a97922015-07-27 15:31:22 -07001724 ASSERT(0);
Shashank Mittal162244e2011-08-08 19:01:25 -07001725 }
1726}
1727
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001728void read_device_info_mmc(struct device_info *info)
Shashank Mittal162244e2011-08-08 19:01:25 -07001729{
Shashank Mittal162244e2011-08-08 19:01:25 -07001730 unsigned long long ptn = 0;
1731 unsigned long long size;
1732 int index = INVALID_PTN;
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001733 uint32_t blocksize;
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001734 uint32_t ret = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001735
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001736 if ((index = partition_get_index("devinfo")) < 0)
1737 {
1738 devinfo_present = false;
1739 index = partition_get_index("aboot");
1740 }
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001741
Shashank Mittal162244e2011-08-08 19:01:25 -07001742 ptn = partition_get_offset(index);
1743 if(ptn == 0)
1744 {
1745 return;
1746 }
1747
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001748 mmc_set_lun(partition_get_lun(index));
1749
Shashank Mittal162244e2011-08-08 19:01:25 -07001750 size = partition_get_size(index);
1751
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001752 blocksize = mmc_get_device_blocksize();
1753
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001754 if (devinfo_present)
1755 ret = mmc_read(ptn, (void *)info_buf, blocksize);
1756 else
1757 ret = mmc_read((ptn + size - blocksize), (void *)info_buf, blocksize);
1758 if (ret)
Shashank Mittal162244e2011-08-08 19:01:25 -07001759 {
1760 dprintf(CRITICAL, "ERROR: Cannot read device info\n");
Sridhar Parasurame1a97922015-07-27 15:31:22 -07001761 ASSERT(0);
Shashank Mittal162244e2011-08-08 19:01:25 -07001762 }
Shashank Mittal162244e2011-08-08 19:01:25 -07001763}
1764
1765void write_device_info_flash(device_info *dev)
1766{
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001767 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
Shashank Mittal162244e2011-08-08 19:01:25 -07001768 struct ptentry *ptn;
1769 struct ptable *ptable;
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001770 if(info == NULL)
1771 {
1772 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
1773 ASSERT(0);
1774 }
1775 info_buf = info;
Shashank Mittal162244e2011-08-08 19:01:25 -07001776 ptable = flash_get_ptable();
1777 if (ptable == NULL)
1778 {
1779 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1780 return;
1781 }
1782
1783 ptn = ptable_find(ptable, "devinfo");
1784 if (ptn == NULL)
1785 {
Smita Ghosh670c8b82015-05-07 09:30:03 -07001786 dprintf(CRITICAL, "ERROR: No devinfo partition found\n");
Shashank Mittal162244e2011-08-08 19:01:25 -07001787 return;
1788 }
1789
1790 memcpy(info, dev, sizeof(device_info));
1791
1792 if (flash_write(ptn, 0, (void *)info_buf, page_size))
1793 {
1794 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1795 return;
1796 }
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001797 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07001798}
1799
vijay kumarc65876c2015-04-24 13:29:16 +05301800static int read_allow_oem_unlock(device_info *dev)
1801{
vijay kumarc65876c2015-04-24 13:29:16 +05301802 unsigned offset;
1803 int index;
1804 unsigned long long ptn;
1805 unsigned long long ptn_size;
1806 unsigned blocksize = mmc_get_device_blocksize();
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07001807 STACKBUF_DMA_ALIGN(buf, blocksize);
vijay kumarc65876c2015-04-24 13:29:16 +05301808
vijay kumarca2e6812015-07-08 20:28:25 +05301809 index = partition_get_index(frp_ptns[0]);
vijay kumarc65876c2015-04-24 13:29:16 +05301810 if (index == INVALID_PTN)
1811 {
vijay kumarca2e6812015-07-08 20:28:25 +05301812 index = partition_get_index(frp_ptns[1]);
1813 if (index == INVALID_PTN)
1814 {
1815 dprintf(CRITICAL, "Neither '%s' nor '%s' partition found\n", frp_ptns[0],frp_ptns[1]);
1816 return -1;
1817 }
vijay kumarc65876c2015-04-24 13:29:16 +05301818 }
1819
1820 ptn = partition_get_offset(index);
1821 ptn_size = partition_get_size(index);
1822 offset = ptn_size - blocksize;
1823
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07001824 if (mmc_read(ptn + offset, (void *)buf, blocksize))
vijay kumarc65876c2015-04-24 13:29:16 +05301825 {
1826 dprintf(CRITICAL, "Reading MMC failed\n");
1827 return -1;
1828 }
1829
1830 /*is_allow_unlock is a bool value stored at the LSB of last byte*/
1831 is_allow_unlock = buf[blocksize-1] & 0x01;
1832 return 0;
1833}
1834
1835static int write_allow_oem_unlock(bool allow_unlock)
1836{
vijay kumarc65876c2015-04-24 13:29:16 +05301837 unsigned offset;
vijay kumarc65876c2015-04-24 13:29:16 +05301838 int index;
1839 unsigned long long ptn;
1840 unsigned long long ptn_size;
1841 unsigned blocksize = mmc_get_device_blocksize();
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07001842 STACKBUF_DMA_ALIGN(buf, blocksize);
vijay kumarc65876c2015-04-24 13:29:16 +05301843
vijay kumarca2e6812015-07-08 20:28:25 +05301844 index = partition_get_index(frp_ptns[0]);
vijay kumarc65876c2015-04-24 13:29:16 +05301845 if (index == INVALID_PTN)
1846 {
vijay kumarca2e6812015-07-08 20:28:25 +05301847 index = partition_get_index(frp_ptns[1]);
1848 if (index == INVALID_PTN)
1849 {
1850 dprintf(CRITICAL, "Neither '%s' nor '%s' partition found\n", frp_ptns[0],frp_ptns[1]);
1851 return -1;
1852 }
vijay kumarc65876c2015-04-24 13:29:16 +05301853 }
1854
1855 ptn = partition_get_offset(index);
1856 ptn_size = partition_get_size(index);
1857 offset = ptn_size - blocksize;
1858
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07001859 if (mmc_read(ptn + offset, (void *)buf, blocksize))
vijay kumarc65876c2015-04-24 13:29:16 +05301860 {
1861 dprintf(CRITICAL, "Reading MMC failed\n");
1862 return -1;
1863 }
1864
1865 /*is_allow_unlock is a bool value stored at the LSB of last byte*/
1866 buf[blocksize-1] = allow_unlock;
1867 if (mmc_write(ptn + offset, blocksize, buf))
1868 {
1869 dprintf(CRITICAL, "Writing MMC failed\n");
1870 return -1;
1871 }
1872
1873 return 0;
1874}
1875
Shashank Mittal162244e2011-08-08 19:01:25 -07001876void read_device_info_flash(device_info *dev)
1877{
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001878 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
Shashank Mittal162244e2011-08-08 19:01:25 -07001879 struct ptentry *ptn;
1880 struct ptable *ptable;
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001881 if(info == NULL)
1882 {
1883 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
1884 ASSERT(0);
1885 }
1886 info_buf = info;
Shashank Mittal162244e2011-08-08 19:01:25 -07001887 ptable = flash_get_ptable();
1888 if (ptable == NULL)
1889 {
1890 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1891 return;
1892 }
1893
1894 ptn = ptable_find(ptable, "devinfo");
1895 if (ptn == NULL)
1896 {
Smita Ghosh670c8b82015-05-07 09:30:03 -07001897 dprintf(CRITICAL, "ERROR: No devinfo partition found\n");
Shashank Mittal162244e2011-08-08 19:01:25 -07001898 return;
1899 }
1900
1901 if (flash_read(ptn, 0, (void *)info_buf, page_size))
1902 {
1903 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1904 return;
1905 }
1906
1907 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
1908 {
Shashank Mittal162244e2011-08-08 19:01:25 -07001909 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
1910 info->is_unlocked = 0;
Shashank Mittala0032282011-08-26 14:50:11 -07001911 info->is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001912 write_device_info_flash(info);
1913 }
1914 memcpy(dev, info, sizeof(device_info));
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001915 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07001916}
1917
1918void write_device_info(device_info *dev)
1919{
1920 if(target_is_emmc_boot())
1921 {
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001922 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
1923 if(info == NULL)
1924 {
1925 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
1926 ASSERT(0);
1927 }
1928 info_buf = info;
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001929 memcpy(info, dev, sizeof(struct device_info));
1930
1931#if USE_RPMB_FOR_DEVINFO
Sridhar Parasurame1a97922015-07-27 15:31:22 -07001932 if (is_secure_boot_enable()) {
1933 if((write_device_info_rpmb((void*) info, PAGE_SIZE)) < 0)
1934 ASSERT(0);
1935 }
Channagoud Kadabic80cb492015-04-28 16:08:28 -07001936 else
1937 write_device_info_mmc(info);
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001938#else
1939 write_device_info_mmc(info);
1940#endif
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001941 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07001942 }
1943 else
1944 {
1945 write_device_info_flash(dev);
1946 }
1947}
1948
1949void read_device_info(device_info *dev)
1950{
1951 if(target_is_emmc_boot())
1952 {
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001953 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
1954 if(info == NULL)
1955 {
1956 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
1957 ASSERT(0);
1958 }
1959 info_buf = info;
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001960
1961#if USE_RPMB_FOR_DEVINFO
Sridhar Parasurame1a97922015-07-27 15:31:22 -07001962 if (is_secure_boot_enable()) {
1963 if((read_device_info_rpmb((void*) info, PAGE_SIZE)) < 0)
1964 ASSERT(0);
1965 }
Channagoud Kadabic80cb492015-04-28 16:08:28 -07001966 else
1967 read_device_info_mmc(info);
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001968#else
1969 read_device_info_mmc(info);
1970#endif
1971
1972 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
1973 {
1974 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
lijuang511a2b52015-08-14 20:50:51 +08001975 if (is_secure_boot_enable()) {
Channagoud Kadabi05f78ba2015-07-06 11:58:14 -07001976 info->is_unlocked = 0;
Parth Dixitddbc7352015-10-18 03:13:31 +05301977#if !VBOOT_MOTA
lijuang511a2b52015-08-14 20:50:51 +08001978 info->is_unlock_critical = 0;
Parth Dixitddbc7352015-10-18 03:13:31 +05301979#endif
lijuang511a2b52015-08-14 20:50:51 +08001980 } else {
Channagoud Kadabi2fda4092015-07-07 13:34:11 -07001981 info->is_unlocked = 1;
Parth Dixitddbc7352015-10-18 03:13:31 +05301982#if !VBOOT_MOTA
lijuang511a2b52015-08-14 20:50:51 +08001983 info->is_unlock_critical = 1;
Parth Dixitddbc7352015-10-18 03:13:31 +05301984#endif
lijuang511a2b52015-08-14 20:50:51 +08001985 }
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001986 info->is_tampered = 0;
1987 info->charger_screen_enabled = 0;
Parth Dixitddbc7352015-10-18 03:13:31 +05301988#if !VBOOT_MOTA
Sridhar Parasuram287e5062015-08-24 16:17:22 -07001989 info->verity_mode = 1; //enforcing by default
Parth Dixitddbc7352015-10-18 03:13:31 +05301990#endif
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001991 write_device_info(info);
1992 }
1993 memcpy(dev, info, sizeof(device_info));
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001994 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07001995 }
1996 else
1997 {
1998 read_device_info_flash(dev);
1999 }
2000}
2001
2002void reset_device_info()
2003{
2004 dprintf(ALWAYS, "reset_device_info called.");
Shashank Mittala0032282011-08-26 14:50:11 -07002005 device.is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07002006 write_device_info(&device);
2007}
2008
2009void set_device_root()
2010{
2011 dprintf(ALWAYS, "set_device_root called.");
Shashank Mittala0032282011-08-26 14:50:11 -07002012 device.is_tampered = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -07002013 write_device_info(&device);
2014}
2015
lijuang4ece1e72015-08-14 21:02:36 +08002016/* set device unlock value
2017 * Must check FRP before call this function
2018 * Need to wipe data when unlock status changed
2019 * type 0: oem unlock
2020 * type 1: unlock critical
2021 * status 0: unlock as false
2022 * status 1: lock as true
2023 */
2024void set_device_unlock_value(int type, bool status)
lijuang21f12f52015-08-22 16:22:19 +08002025{
lijuang4ece1e72015-08-14 21:02:36 +08002026 if (type == UNLOCK)
2027 device.is_unlocked = status;
Parth Dixitddbc7352015-10-18 03:13:31 +05302028#if !VBOOT_MOTA
lijuang4ece1e72015-08-14 21:02:36 +08002029 else if (type == UNLOCK_CRITICAL)
2030 device.is_unlock_critical = status;
Parth Dixitddbc7352015-10-18 03:13:31 +05302031#endif
lijuang4ece1e72015-08-14 21:02:36 +08002032 write_device_info(&device);
2033}
2034
2035static void set_device_unlock(int type, bool status)
2036{
2037 int is_unlocked = -1;
2038 char response[MAX_RSP_SIZE];
2039
2040 /* check device unlock status if it is as expected */
2041 if (type == UNLOCK)
2042 is_unlocked = device.is_unlocked;
Parth Dixitddbc7352015-10-18 03:13:31 +05302043#if !VBOOT_MOTA
lijuang4ece1e72015-08-14 21:02:36 +08002044 else if (type == UNLOCK_CRITICAL)
2045 is_unlocked = device.is_unlock_critical;
Parth Dixitddbc7352015-10-18 03:13:31 +05302046#endif
lijuang4ece1e72015-08-14 21:02:36 +08002047 if (is_unlocked == status) {
2048 snprintf(response, sizeof(response), "\tDevice already : %s", (status ? "unlocked!" : "locked!"));
2049 fastboot_info(response);
2050 fastboot_okay("");
2051 return;
2052 }
2053
2054 /* status is true, it means to unlock device */
2055 if (status) {
lijuang21f12f52015-08-22 16:22:19 +08002056 if(!is_allow_unlock) {
2057 fastboot_fail("oem unlock is not allowed");
2058 return;
2059 }
2060
lijuang4ece1e72015-08-14 21:02:36 +08002061#if FBCON_DISPLAY_MSG
lijuang9a7d3b92015-11-30 14:41:24 +08002062 display_unlock_menu(type);
lijuang4ece1e72015-08-14 21:02:36 +08002063 fastboot_okay("");
2064 return;
2065#else
2066 if (type == UNLOCK) {
2067 fastboot_fail("Need wipe userdata. Do 'fastboot oem unlock-go'");
2068 return;
2069 }
2070#endif
lijuang21f12f52015-08-22 16:22:19 +08002071 }
lijuang4ece1e72015-08-14 21:02:36 +08002072
2073 set_device_unlock_value(type, status);
2074
2075 /* wipe data */
2076 struct recovery_message msg;
2077
2078 snprintf(msg.recovery, sizeof(msg.recovery), "recovery\n--wipe_data");
2079 write_misc(0, &msg, sizeof(msg));
2080
2081 fastboot_okay("");
2082 reboot_device(RECOVERY_MODE);
lijuang21f12f52015-08-22 16:22:19 +08002083}
2084
lijuang511a2b52015-08-14 20:50:51 +08002085static bool critical_flash_allowed(const char * entry)
2086{
2087 uint32_t i = 0;
2088 if (entry == NULL)
2089 return false;
2090
2091 for (i = 0; i < ARRAY_SIZE(critical_flash_allowed_ptn); i++) {
2092 if(!strcmp(entry, critical_flash_allowed_ptn[i]))
2093 return true;
2094 }
2095 return false;
Matthew Qin271927e2015-03-31 22:07:07 -04002096}
2097
2098#if DEVICE_TREE
Amol Jadicb524072012-08-09 16:40:18 -07002099int copy_dtb(uint8_t *boot_image_start, unsigned int scratch_offset)
2100{
2101 uint32 dt_image_offset = 0;
Amol Jadicb524072012-08-09 16:40:18 -07002102 uint32_t n;
Amol Jadicb524072012-08-09 16:40:18 -07002103 struct dt_table *table;
2104 struct dt_entry dt_entry;
2105 uint32_t dt_hdr_size;
2106 unsigned int compressed_size = 0;
2107 unsigned int dtb_size = 0;
2108 unsigned int out_avai_len = 0;
2109 unsigned char *out_addr = NULL;
2110 unsigned char *best_match_dt_addr = NULL;
2111 int rc;
2112
2113 struct boot_img_hdr *hdr = (struct boot_img_hdr *) (boot_image_start);
2114
2115 if(hdr->dt_size != 0) {
2116 /* add kernel offset */
2117 dt_image_offset += page_size;
2118 n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07002119 dt_image_offset += n;
Amol Jadicb524072012-08-09 16:40:18 -07002120
Deepa Dinamani19648b42013-09-05 17:05:55 -07002121 /* add ramdisk offset */
Amol Jadicb524072012-08-09 16:40:18 -07002122 n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
2123 dt_image_offset += n;
2124
Joel Kingaa335dc2013-06-03 16:11:08 -07002125 /* add second offset */
2126 if(hdr->second_size != 0) {
Amol Jadicb524072012-08-09 16:40:18 -07002127 n = ROUND_TO_PAGE(hdr->second_size, page_mask);
2128 dt_image_offset += n;
2129 }
2130
Matthew Qin271927e2015-03-31 22:07:07 -04002131 /* offset now point to start of dt.img */
2132 table = (struct dt_table*)(boot_image_start + dt_image_offset);
2133
2134 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
2135 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
Matthew Qin0b15b322015-05-19 05:20:54 -04002136 return -1;
Matthew Qin271927e2015-03-31 22:07:07 -04002137 }
P.V. Phani Kumar82916762016-03-09 09:20:19 +05302138
2139 /* Its Error if, dt_hdr_size (table->num_entries * dt_entry size + Dev_Tree Header)
2140 goes beyound hdr->dt_size*/
2141 if (dt_hdr_size > ROUND_TO_PAGE(hdr->dt_size,hdr->page_size)) {
2142 dprintf(CRITICAL, "ERROR: Invalid Device Tree size \n");
2143 return -1;
2144 }
2145
Amol Jadicb524072012-08-09 16:40:18 -07002146 /* Find index of device tree within device tree table */
2147 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
2148 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
2149 return -1;
2150 }
2151
Matthew Qin271927e2015-03-31 22:07:07 -04002152 best_match_dt_addr = (unsigned char *)boot_image_start + dt_image_offset + dt_entry.offset;
2153 if (is_gzip_package(best_match_dt_addr, dt_entry.size))
2154 {
2155 out_addr = (unsigned char *)target_get_scratch_address() + scratch_offset;
2156 out_avai_len = target_get_max_flash_size() - scratch_offset;
2157 dprintf(INFO, "decompressing dtb: start\n");
2158 rc = decompress(best_match_dt_addr,
2159 dt_entry.size, out_addr, out_avai_len,
2160 &compressed_size, &dtb_size);
2161 if (rc)
2162 {
Matthew Qin0b15b322015-05-19 05:20:54 -04002163 dprintf(CRITICAL, "decompressing dtb failed!!!\n");
Matthew Qin271927e2015-03-31 22:07:07 -04002164 ASSERT(0);
2165 }
2166
Matthew Qin0b15b322015-05-19 05:20:54 -04002167 dprintf(INFO, "decompressing dtb: done\n");
Matthew Qin271927e2015-03-31 22:07:07 -04002168 best_match_dt_addr = out_addr;
2169 } else {
2170 dtb_size = dt_entry.size;
2171 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002172 /* Validate and Read device device tree in the "tags_add */
Matthew Qin271927e2015-03-31 22:07:07 -04002173 if (check_aboot_addr_range_overlap(hdr->tags_addr, dtb_size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002174 {
2175 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
2176 return -1;
2177 }
2178
Amol Jadicb524072012-08-09 16:40:18 -07002179 /* Read device device tree in the "tags_add */
Matthew Qin271927e2015-03-31 22:07:07 -04002180 memmove((void*) hdr->tags_addr, (void *)best_match_dt_addr, dtb_size);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002181 } else
2182 return -1;
Amol Jadicb524072012-08-09 16:40:18 -07002183
2184 /* Everything looks fine. Return success. */
2185 return 0;
2186}
2187#endif
2188
Brian Swetland9c4c0752009-01-25 16:23:50 -08002189void cmd_boot(const char *arg, void *data, unsigned sz)
2190{
Amit Blay8a510302015-08-17 09:20:01 +03002191#ifdef MDTP_SUPPORT
2192 static bool is_mdtp_activated = 0;
2193#endif /* MDTP_SUPPORT */
Brian Swetland9c4c0752009-01-25 16:23:50 -08002194 unsigned kernel_actual;
2195 unsigned ramdisk_actual;
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002196 uint32_t image_actual;
2197 uint32_t dt_actual = 0;
Channagoud Kadabi3d839012014-06-26 14:26:39 -07002198 uint32_t sig_actual = SIGNATURE_SIZE;
Matthew Qinbb7923d2015-02-09 10:56:09 +08002199 struct boot_img_hdr *hdr = NULL;
2200 struct kernel64_hdr *kptr = NULL;
Brian Swetland9c4c0752009-01-25 16:23:50 -08002201 char *ptr = ((char*) data);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002202 int ret = 0;
2203 uint8_t dtb_copied = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08002204 unsigned int out_len = 0;
2205 unsigned int out_avai_len = 0;
2206 unsigned char *out_addr = NULL;
2207 uint32_t dtb_offset = 0;
2208 unsigned char *kernel_start_addr = NULL;
2209 unsigned int kernel_size = 0;
Matthew Qin271927e2015-03-31 22:07:07 -04002210 unsigned int scratch_offset = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08002211
lijuang2008ff22016-03-07 17:56:27 +08002212#if FBCON_DISPLAY_MSG
2213 /* Exit keys' detection thread firstly */
2214 exit_menu_keys_detection();
2215#endif
2216
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002217#if VERIFIED_BOOT
Channagoud Kadabi6d5375e2015-06-23 17:15:42 -07002218 if(target_build_variant_user() && !device.is_unlocked)
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002219 {
2220 fastboot_fail("unlock device to use this command");
lijuang2008ff22016-03-07 17:56:27 +08002221 goto boot_failed;
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002222 }
2223#endif
2224
Brian Swetland9c4c0752009-01-25 16:23:50 -08002225 if (sz < sizeof(hdr)) {
2226 fastboot_fail("invalid bootimage header");
lijuang2008ff22016-03-07 17:56:27 +08002227 goto boot_failed;
Brian Swetland9c4c0752009-01-25 16:23:50 -08002228 }
2229
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002230 hdr = (struct boot_img_hdr *)data;
Brian Swetland9c4c0752009-01-25 16:23:50 -08002231
2232 /* ensure commandline is terminated */
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002233 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -08002234
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002235 if(target_is_emmc_boot() && hdr->page_size) {
2236 page_size = hdr->page_size;
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07002237 page_mask = page_size - 1;
2238 }
2239
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002240 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
2241 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002242#if DEVICE_TREE
2243 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
2244#endif
2245
2246 image_actual = ADD_OF(page_size, kernel_actual);
2247 image_actual = ADD_OF(image_actual, ramdisk_actual);
2248 image_actual = ADD_OF(image_actual, dt_actual);
2249
Channagoud Kadabi3d839012014-06-26 14:26:39 -07002250 if (target_use_signed_kernel() && (!device.is_unlocked))
2251 image_actual = ADD_OF(image_actual, sig_actual);
2252
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002253 /* sz should have atleast raw boot image */
2254 if (image_actual > sz) {
Channagoud Kadabi3d839012014-06-26 14:26:39 -07002255 fastboot_fail("bootimage: incomplete or not signed");
lijuang2008ff22016-03-07 17:56:27 +08002256 goto boot_failed;
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002257 }
2258
Sridhar Parasuram361e4cd2015-09-24 11:19:19 -07002259 // Initialize boot state before trying to verify boot.img
2260#if VERIFIED_BOOT
Channagoud Kadabi699466e2015-11-03 12:37:42 -08002261 boot_verifier_init();
Channagoud Kadabibf6ce7d2015-09-17 13:25:35 -07002262 /* Handle overflow if the input image size is greater than
2263 * boot image buffer can hold
2264 */
2265 if ((target_get_max_flash_size() - (image_actual - sig_actual)) < page_size)
2266 {
2267 fastboot_fail("booimage: size is greater than boot image buffer can hold");
lijuang2008ff22016-03-07 17:56:27 +08002268 goto boot_failed;
Channagoud Kadabibf6ce7d2015-09-17 13:25:35 -07002269 }
Sridhar Parasuram361e4cd2015-09-24 11:19:19 -07002270#endif
2271
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002272 /* Verify the boot image
2273 * device & page_size are initialized in aboot_init
2274 */
Sridhar Parasuram7ae9fbc2015-09-17 09:56:30 -07002275 if (target_use_signed_kernel() && (!device.is_unlocked)) {
Channagoud Kadabi3d839012014-06-26 14:26:39 -07002276 /* Pass size excluding signature size, otherwise we would try to
2277 * access signature beyond its length
2278 */
2279 verify_signed_bootimg((uint32_t)data, (image_actual - sig_actual));
Sridhar Parasuram7ae9fbc2015-09-17 09:56:30 -07002280 }
Amit Blay4aa292f2015-04-28 21:55:59 +03002281#ifdef MDTP_SUPPORT
2282 else
2283 {
Shay Nachmani062aa3f2015-05-17 17:28:44 +03002284 /* fastboot boot is not allowed when MDTP is activated */
Amit Blay4aa292f2015-04-28 21:55:59 +03002285 mdtp_ext_partition_verification_t ext_partition;
Amit Blay8a510302015-08-17 09:20:01 +03002286
2287 if (!is_mdtp_activated) {
2288 ext_partition.partition = MDTP_PARTITION_NONE;
2289 mdtp_fwlock_verify_lock(&ext_partition);
2290 }
Amit Blay4aa292f2015-04-28 21:55:59 +03002291 }
Shay Nachmani062aa3f2015-05-17 17:28:44 +03002292
Shay Nachmani062aa3f2015-05-17 17:28:44 +03002293 mdtp_activated(&is_mdtp_activated);
2294 if(is_mdtp_activated){
2295 dprintf(CRITICAL, "fastboot boot command is not available.\n");
lijuang2008ff22016-03-07 17:56:27 +08002296 goto boot_failed;
Shay Nachmani062aa3f2015-05-17 17:28:44 +03002297 }
Amit Blay4aa292f2015-04-28 21:55:59 +03002298#endif /* MDTP_SUPPORT */
2299
Sridhar Parasuram361e4cd2015-09-24 11:19:19 -07002300#if VERIFIED_BOOT
Parth Dixitbc9b6492015-10-18 00:41:38 +05302301#if !VBOOT_MOTA
Sridhar Parasuram361e4cd2015-09-24 11:19:19 -07002302 // send root of trust
2303 if(!send_rot_command((uint32_t)device.is_unlocked))
2304 ASSERT(0);
2305#endif
Parth Dixitbc9b6492015-10-18 00:41:38 +05302306#endif
Channagoud Kadabia22144f2013-03-20 11:49:01 -07002307 /*
Matthew Qinbb7923d2015-02-09 10:56:09 +08002308 * Check if the kernel image is a gzip package. If yes, need to decompress it.
2309 * If not, continue booting.
2310 */
2311 if (is_gzip_package((unsigned char *)(data + page_size), hdr->kernel_size))
2312 {
2313 out_addr = (unsigned char *)target_get_scratch_address();
2314 out_addr = (unsigned char *)(out_addr + image_actual + page_size);
2315 out_avai_len = target_get_max_flash_size() - image_actual - page_size;
Matthew Qin0b15b322015-05-19 05:20:54 -04002316 dprintf(INFO, "decompressing kernel image: start\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08002317 ret = decompress((unsigned char *)(ptr + page_size),
2318 hdr->kernel_size, out_addr, out_avai_len,
2319 &dtb_offset, &out_len);
2320 if (ret)
2321 {
Matthew Qin0b15b322015-05-19 05:20:54 -04002322 dprintf(CRITICAL, "decompressing image failed!!!\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08002323 ASSERT(0);
2324 }
2325
Matthew Qin0b15b322015-05-19 05:20:54 -04002326 dprintf(INFO, "decompressing kernel image: done\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08002327 kptr = (struct kernel64_hdr *)out_addr;
2328 kernel_start_addr = out_addr;
2329 kernel_size = out_len;
2330 } else {
2331 kptr = (struct kernel64_hdr*)((char *)data + page_size);
2332 kernel_start_addr = (unsigned char *)((char *)data + page_size);
2333 kernel_size = hdr->kernel_size;
2334 }
2335
2336 /*
Channagoud Kadabia22144f2013-03-20 11:49:01 -07002337 * Update the kernel/ramdisk/tags address if the boot image header
2338 * has default values, these default values come from mkbootimg when
2339 * the boot image is flashed using fastboot flash:raw
2340 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08002341 update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr));
Dima Zavin3cadfff2013-03-21 14:30:48 -07002342
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002343 /* Get virtual addresses since the hdr saves physical addresses. */
2344 hdr->kernel_addr = VA(hdr->kernel_addr);
2345 hdr->ramdisk_addr = VA(hdr->ramdisk_addr);
2346 hdr->tags_addr = VA(hdr->tags_addr);
Brian Swetland9c4c0752009-01-25 16:23:50 -08002347
Matthew Qinbb7923d2015-02-09 10:56:09 +08002348 kernel_size = ROUND_TO_PAGE(kernel_size, page_mask);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002349 /* Check if the addresses in the header are valid. */
Matthew Qinbb7923d2015-02-09 10:56:09 +08002350 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_size) ||
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002351 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
2352 {
2353 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
lijuang2008ff22016-03-07 17:56:27 +08002354 goto boot_failed;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002355 }
2356
Amol Jadicb524072012-08-09 16:40:18 -07002357#if DEVICE_TREE
Matthew Qin271927e2015-03-31 22:07:07 -04002358 scratch_offset = image_actual + page_size + out_len;
Amol Jadicb524072012-08-09 16:40:18 -07002359 /* find correct dtb and copy it to right location */
Matthew Qin271927e2015-03-31 22:07:07 -04002360 ret = copy_dtb(data, scratch_offset);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002361
2362 dtb_copied = !ret ? 1 : 0;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002363#else
2364 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
2365 {
2366 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
lijuang2008ff22016-03-07 17:56:27 +08002367 goto boot_failed;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002368 }
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002369#endif
2370
2371 /* Load ramdisk & kernel */
2372 memmove((void*) hdr->ramdisk_addr, ptr + page_size + kernel_actual, hdr->ramdisk_size);
Matthew Qinbb7923d2015-02-09 10:56:09 +08002373 memmove((void*) hdr->kernel_addr, (char*) (kernel_start_addr), kernel_size);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002374
2375#if DEVICE_TREE
Matthew Qinbb7923d2015-02-09 10:56:09 +08002376 if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual))
2377 {
2378 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
lijuang2008ff22016-03-07 17:56:27 +08002379 goto boot_failed;
Matthew Qinbb7923d2015-02-09 10:56:09 +08002380 }
2381
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002382 /*
2383 * If dtb is not found look for appended DTB in the kernel.
2384 * If appended dev tree is found, update the atags with
2385 * memory address to the DTB appended location on RAM.
2386 * Else update with the atags address in the kernel header
2387 */
2388 if (!dtb_copied) {
2389 void *dtb;
Matthew Qinbb7923d2015-02-09 10:56:09 +08002390 dtb = dev_tree_appended((void*)(ptr + page_size),
2391 hdr->kernel_size, dtb_offset,
Dima Zavine63e5572013-05-03 12:23:06 -07002392 (void *)hdr->tags_addr);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002393 if (!dtb) {
2394 fastboot_fail("dtb not found");
lijuang2008ff22016-03-07 17:56:27 +08002395 goto boot_failed;
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002396 }
Amol Jadicb524072012-08-09 16:40:18 -07002397 }
2398#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08002399
2400 fastboot_okay("");
Amol Jadi7c4316c2013-10-07 14:19:26 -07002401 fastboot_stop();
Brian Swetland9c4c0752009-01-25 16:23:50 -08002402
Dima Zavin77e41f32013-03-06 16:10:43 -08002403 boot_linux((void*) hdr->kernel_addr, (void*) hdr->tags_addr,
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002404 (const char*) hdr->cmdline, board_machtype(),
2405 (void*) hdr->ramdisk_addr, hdr->ramdisk_size);
lijuang2008ff22016-03-07 17:56:27 +08002406
2407 /* fastboot already stop, it's no need to show fastboot menu */
2408 return;
2409boot_failed:
2410#if FBCON_DISPLAY_MSG
2411 /* revert to fastboot menu if boot failed */
2412 display_fastboot_menu();
2413#endif
2414 return;
Brian Swetland9c4c0752009-01-25 16:23:50 -08002415}
2416
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002417void cmd_erase_nand(const char *arg, void *data, unsigned sz)
Dima Zavin214cc642009-01-26 11:16:21 -08002418{
2419 struct ptentry *ptn;
2420 struct ptable *ptable;
2421
2422 ptable = flash_get_ptable();
2423 if (ptable == NULL) {
2424 fastboot_fail("partition table doesn't exist");
2425 return;
2426 }
2427
2428 ptn = ptable_find(ptable, arg);
2429 if (ptn == NULL) {
2430 fastboot_fail("unknown partition name");
2431 return;
2432 }
2433
2434 if (flash_erase(ptn)) {
2435 fastboot_fail("failed to erase partition");
2436 return;
2437 }
2438 fastboot_okay("");
2439}
2440
Bikas Gurungd48bd242010-09-04 19:54:32 -07002441
2442void cmd_erase_mmc(const char *arg, void *data, unsigned sz)
2443{
2444 unsigned long long ptn = 0;
Oliver Wangcee448d2013-10-22 18:40:13 +08002445 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07002446 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002447 uint8_t lun = 0;
Bikas Gurungd48bd242010-09-04 19:54:32 -07002448
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002449#if VERIFIED_BOOT
2450 if(!strcmp(arg, KEYSTORE_PTN_NAME))
2451 {
2452 if(!device.is_unlocked)
2453 {
2454 fastboot_fail("unlock device to erase keystore");
2455 return;
2456 }
2457 }
2458#endif
2459
Kinson Chikf1a43512011-07-14 11:28:39 -07002460 index = partition_get_index(arg);
2461 ptn = partition_get_offset(index);
Oliver Wangcee448d2013-10-22 18:40:13 +08002462 size = partition_get_size(index);
Neeti Desaica8c9602011-10-06 11:40:00 -07002463
Kinson Chikf1a43512011-07-14 11:28:39 -07002464 if(ptn == 0) {
Neeti Desaica8c9602011-10-06 11:40:00 -07002465 fastboot_fail("Partition table doesn't exist\n");
Bikas Gurungd48bd242010-09-04 19:54:32 -07002466 return;
2467 }
Kun Liang2f1601a2013-08-12 16:29:54 +08002468
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002469 lun = partition_get_lun(index);
2470 mmc_set_lun(lun);
2471
Sridhar Parasuramd7957122015-02-27 11:33:40 -08002472 if (platform_boot_dev_isemmc())
2473 {
2474 if (mmc_erase_card(ptn, size)) {
2475 fastboot_fail("failed to erase partition\n");
2476 return;
2477 }
2478 } else {
2479 BUF_DMA_ALIGN(out, DEFAULT_ERASE_SIZE);
2480 size = partition_get_size(index);
2481 if (size > DEFAULT_ERASE_SIZE)
2482 size = DEFAULT_ERASE_SIZE;
Kun Liang2f1601a2013-08-12 16:29:54 +08002483
Sridhar Parasuramd7957122015-02-27 11:33:40 -08002484 /* Simple inefficient version of erase. Just writing
2485 0 in first several blocks */
2486 if (mmc_write(ptn , size, (unsigned int *)out)) {
2487 fastboot_fail("failed to erase partition");
2488 return;
2489 }
Bikas Gurungd48bd242010-09-04 19:54:32 -07002490 }
Sridhar Parasuram32b30662015-07-10 13:33:22 -07002491#if VERIFIED_BOOT
Parth Dixitbc9b6492015-10-18 00:41:38 +05302492#if !VBOOT_MOTA
Sridhar Parasuram32b30662015-07-10 13:33:22 -07002493 if(!(strncmp(arg, "userdata", 8)))
2494 if(send_delete_keys_to_tz())
2495 ASSERT(0);
2496#endif
Parth Dixitbc9b6492015-10-18 00:41:38 +05302497#endif
Bikas Gurungd48bd242010-09-04 19:54:32 -07002498 fastboot_okay("");
2499}
2500
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002501void cmd_erase(const char *arg, void *data, unsigned sz)
2502{
Channagoud Kadabice3cf422015-04-17 16:02:30 -07002503#if VERIFIED_BOOT
Channagoud Kadabi35297672015-06-13 11:09:49 -07002504 if (target_build_variant_user())
Channagoud Kadabice3cf422015-04-17 16:02:30 -07002505 {
Sridhar Parasuramc32d07d2015-07-12 10:57:48 -07002506 if(!device.is_unlocked)
Channagoud Kadabice3cf422015-04-17 16:02:30 -07002507 {
Channagoud Kadabi35297672015-06-13 11:09:49 -07002508 fastboot_fail("device is locked. Cannot erase");
Channagoud Kadabice3cf422015-04-17 16:02:30 -07002509 return;
2510 }
2511 }
2512#endif
2513
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002514 if(target_is_emmc_boot())
2515 cmd_erase_mmc(arg, data, sz);
2516 else
2517 cmd_erase_nand(arg, data, sz);
2518}
Bikas Gurungd48bd242010-09-04 19:54:32 -07002519
Channagoud Kadabiad259832015-05-29 11:14:17 -07002520static uint32_t aboot_get_secret_key()
2521{
2522 /* 0 is invalid secret key, update this implementation to return
2523 * device specific unique secret key
2524 */
2525 return 0;
2526}
2527
Ajay Dudani5c761132011-04-07 20:19:04 -07002528void cmd_flash_mmc_img(const char *arg, void *data, unsigned sz)
Shashank Mittal23b8f422010-04-16 19:27:21 -07002529{
2530 unsigned long long ptn = 0;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07002531 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07002532 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002533 char *token = NULL;
2534 char *pname = NULL;
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07002535 char *sp;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002536 uint8_t lun = 0;
2537 bool lun_set = false;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07002538
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07002539 token = strtok_r((char *)arg, ":", &sp);
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002540 pname = token;
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07002541 token = strtok_r(NULL, ":", &sp);
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002542 if(token)
2543 {
2544 lun = atoi(token);
2545 mmc_set_lun(lun);
2546 lun_set = true;
2547 }
2548
Mao Jinlong226f33a2014-07-04 17:24:10 +08002549 if (pname)
Greg Grisco6e754772011-06-23 12:19:39 -07002550 {
Channagoud Kadabiad259832015-05-29 11:14:17 -07002551 if (!strncmp(pname, "frp-unlock", strlen("frp-unlock")))
2552 {
2553 if (!aboot_frp_unlock(pname, data, sz))
2554 {
2555 fastboot_info("FRP unlock successful");
2556 fastboot_okay("");
2557 }
2558 else
2559 fastboot_fail("Secret key is invalid, please update the bootloader with secret key");
2560
2561 return;
2562 }
2563
Mao Jinlong226f33a2014-07-04 17:24:10 +08002564 if (!strcmp(pname, "partition"))
2565 {
2566 dprintf(INFO, "Attempt to write partition image.\n");
2567 if (write_partition(sz, (unsigned char *) data)) {
2568 fastboot_fail("failed to write partition");
Greg Grisco6e754772011-06-23 12:19:39 -07002569 return;
2570 }
2571 }
Mao Jinlong226f33a2014-07-04 17:24:10 +08002572 else
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002573 {
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002574#if VERIFIED_BOOT
2575 if(!strcmp(pname, KEYSTORE_PTN_NAME))
2576 {
2577 if(!device.is_unlocked)
2578 {
2579 fastboot_fail("unlock device to flash keystore");
2580 return;
2581 }
Gaurav Nebhwanic7313cc2015-12-15 22:25:04 +05302582 if(!boot_verify_validate_keystore((unsigned char *)data,sz))
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002583 {
2584 fastboot_fail("image is not a keystore file");
2585 return;
2586 }
2587 }
2588#endif
Mao Jinlong226f33a2014-07-04 17:24:10 +08002589 index = partition_get_index(pname);
2590 ptn = partition_get_offset(index);
2591 if(ptn == 0) {
2592 fastboot_fail("partition table doesn't exist");
2593 return;
2594 }
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002595
Mao Jinlong226f33a2014-07-04 17:24:10 +08002596 if (!strcmp(pname, "boot") || !strcmp(pname, "recovery")) {
2597 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
2598 fastboot_fail("image is not a boot image");
2599 return;
2600 }
2601 }
2602
2603 if(!lun_set)
2604 {
2605 lun = partition_get_lun(index);
2606 mmc_set_lun(lun);
2607 }
2608
2609 size = partition_get_size(index);
2610 if (ROUND_TO_PAGE(sz,511) > size) {
2611 fastboot_fail("size too large");
2612 return;
2613 }
2614 else if (mmc_write(ptn , sz, (unsigned int *)data)) {
2615 fastboot_fail("flash write failure");
2616 return;
2617 }
Greg Grisco6e754772011-06-23 12:19:39 -07002618 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07002619 }
2620 fastboot_okay("");
2621 return;
2622}
2623
Ajay Dudanide984792015-03-02 09:57:41 -08002624void cmd_flash_meta_img(const char *arg, void *data, unsigned sz)
2625{
2626 int i, images;
2627 meta_header_t *meta_header;
2628 img_header_entry_t *img_header_entry;
2629
2630 meta_header = (meta_header_t*) data;
2631 img_header_entry = (img_header_entry_t*) (data+sizeof(meta_header_t));
2632
2633 images = meta_header->img_hdr_sz / sizeof(img_header_entry_t);
2634
2635 for (i=0; i<images; i++) {
2636
2637 if((img_header_entry[i].ptn_name == NULL) ||
2638 (img_header_entry[i].start_offset == 0) ||
2639 (img_header_entry[i].size == 0))
2640 break;
2641
2642 cmd_flash_mmc_img(img_header_entry[i].ptn_name,
2643 (void *) data + img_header_entry[i].start_offset,
2644 img_header_entry[i].size);
2645 }
2646
Channagoud Kadabiaafbbfc2015-03-04 17:52:56 -08002647 if (!strncmp(arg, "bootloader", strlen("bootloader")))
2648 {
2649 strlcpy(device.bootloader_version, TARGET(BOARD), MAX_VERSION_LEN);
2650 strlcat(device.bootloader_version, "-", MAX_VERSION_LEN);
2651 strlcat(device.bootloader_version, meta_header->img_version, MAX_VERSION_LEN);
2652 }
2653 else
2654 {
2655 strlcpy(device.radio_version, TARGET(BOARD), MAX_VERSION_LEN);
2656 strlcat(device.radio_version, "-", MAX_VERSION_LEN);
2657 strlcat(device.radio_version, meta_header->img_version, MAX_VERSION_LEN);
2658 }
2659
2660 write_device_info(&device);
Ajay Dudanide984792015-03-02 09:57:41 -08002661 fastboot_okay("");
2662 return;
2663}
2664
Ajay Dudani5c761132011-04-07 20:19:04 -07002665void cmd_flash_mmc_sparse_img(const char *arg, void *data, unsigned sz)
2666{
2667 unsigned int chunk;
wufeng.jiang813dc352015-06-02 23:02:46 -04002668 uint64_t chunk_data_sz;
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002669 uint32_t *fill_buf = NULL;
2670 uint32_t fill_val;
Ajay Dudani5c761132011-04-07 20:19:04 -07002671 sparse_header_t *sparse_header;
2672 chunk_header_t *chunk_header;
Ajay Dudaniab18f022011-05-12 14:39:22 -07002673 uint32_t total_blocks = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07002674 unsigned long long ptn = 0;
Channagoud Kadabi65b91002011-10-11 17:34:33 +05302675 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07002676 int index = INVALID_PTN;
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05302677 uint32_t i;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002678 uint8_t lun = 0;
vijay kumar800255e2015-04-24 20:26:19 +05302679 /*End of the sparse image address*/
2680 uint32_t data_end = (uint32_t)data + sz;
Ajay Dudani5c761132011-04-07 20:19:04 -07002681
Kinson Chikf1a43512011-07-14 11:28:39 -07002682 index = partition_get_index(arg);
2683 ptn = partition_get_offset(index);
2684 if(ptn == 0) {
Ajay Dudani5c761132011-04-07 20:19:04 -07002685 fastboot_fail("partition table doesn't exist");
2686 return;
2687 }
2688
Channagoud Kadabi65b91002011-10-11 17:34:33 +05302689 size = partition_get_size(index);
Channagoud Kadabi65b91002011-10-11 17:34:33 +05302690
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002691 lun = partition_get_lun(index);
2692 mmc_set_lun(lun);
2693
vijay kumar800255e2015-04-24 20:26:19 +05302694 if (sz < sizeof(sparse_header_t)) {
2695 fastboot_fail("size too low");
2696 return;
2697 }
2698
Ajay Dudani5c761132011-04-07 20:19:04 -07002699 /* Read and skip over sparse image header */
2700 sparse_header = (sparse_header_t *) data;
vijay kumar800255e2015-04-24 20:26:19 +05302701
vijay kumar1321f342015-03-27 12:13:42 +05302702 if (((uint64_t)sparse_header->total_blks * (uint64_t)sparse_header->blk_sz) > size) {
Ajay Dudani876b3282012-12-21 14:12:17 -08002703 fastboot_fail("size too large");
2704 return;
2705 }
2706
vijay kumarde4fcf62015-04-23 13:05:49 +05302707 data += sizeof(sparse_header_t);
vijay kumar800255e2015-04-24 20:26:19 +05302708
2709 if (data_end < (uint32_t)data) {
2710 fastboot_fail("buffer overreads occured due to invalid sparse header");
2711 return;
2712 }
2713
vijay kumarde4fcf62015-04-23 13:05:49 +05302714 if(sparse_header->file_hdr_sz != sizeof(sparse_header_t))
Ajay Dudani5c761132011-04-07 20:19:04 -07002715 {
vijay kumarde4fcf62015-04-23 13:05:49 +05302716 fastboot_fail("sparse header size mismatch");
2717 return;
Ajay Dudani5c761132011-04-07 20:19:04 -07002718 }
2719
Ajay Dudanib06c05f2011-05-12 14:46:10 -07002720 dprintf (SPEW, "=== Sparse Image Header ===\n");
2721 dprintf (SPEW, "magic: 0x%x\n", sparse_header->magic);
2722 dprintf (SPEW, "major_version: 0x%x\n", sparse_header->major_version);
2723 dprintf (SPEW, "minor_version: 0x%x\n", sparse_header->minor_version);
2724 dprintf (SPEW, "file_hdr_sz: %d\n", sparse_header->file_hdr_sz);
2725 dprintf (SPEW, "chunk_hdr_sz: %d\n", sparse_header->chunk_hdr_sz);
2726 dprintf (SPEW, "blk_sz: %d\n", sparse_header->blk_sz);
2727 dprintf (SPEW, "total_blks: %d\n", sparse_header->total_blks);
2728 dprintf (SPEW, "total_chunks: %d\n", sparse_header->total_chunks);
Ajay Dudani5c761132011-04-07 20:19:04 -07002729
2730 /* Start processing chunks */
2731 for (chunk=0; chunk<sparse_header->total_chunks; chunk++)
2732 {
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302733 /* Make sure the total image size does not exceed the partition size */
2734 if(((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz) >= size) {
2735 fastboot_fail("size too large");
2736 return;
2737 }
Ajay Dudani5c761132011-04-07 20:19:04 -07002738 /* Read and skip over chunk header */
2739 chunk_header = (chunk_header_t *) data;
2740 data += sizeof(chunk_header_t);
2741
vijay kumar800255e2015-04-24 20:26:19 +05302742 if (data_end < (uint32_t)data) {
2743 fastboot_fail("buffer overreads occured due to invalid sparse header");
2744 return;
2745 }
2746
Ajay Dudani5c761132011-04-07 20:19:04 -07002747 dprintf (SPEW, "=== Chunk Header ===\n");
2748 dprintf (SPEW, "chunk_type: 0x%x\n", chunk_header->chunk_type);
2749 dprintf (SPEW, "chunk_data_sz: 0x%x\n", chunk_header->chunk_sz);
2750 dprintf (SPEW, "total_size: 0x%x\n", chunk_header->total_sz);
2751
vijay kumar800255e2015-04-24 20:26:19 +05302752 if(sparse_header->chunk_hdr_sz != sizeof(chunk_header_t))
Ajay Dudani5c761132011-04-07 20:19:04 -07002753 {
vijay kumar800255e2015-04-24 20:26:19 +05302754 fastboot_fail("chunk header size mismatch");
2755 return;
Ajay Dudani5c761132011-04-07 20:19:04 -07002756 }
2757
wufeng.jiang813dc352015-06-02 23:02:46 -04002758 if (!sparse_header->blk_sz ){
2759 fastboot_fail("Invalid block size\n");
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302760 return;
2761 }
2762
wufeng.jiang813dc352015-06-02 23:02:46 -04002763 chunk_data_sz = (uint64_t)sparse_header->blk_sz * chunk_header->chunk_sz;
2764
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302765 /* Make sure that the chunk size calculated from sparse image does not
2766 * exceed partition size
2767 */
2768 if ((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz + chunk_data_sz > size)
2769 {
2770 fastboot_fail("Chunk data size exceeds partition size");
2771 return;
2772 }
2773
Ajay Dudani5c761132011-04-07 20:19:04 -07002774 switch (chunk_header->chunk_type)
2775 {
2776 case CHUNK_TYPE_RAW:
wufeng.jiang813dc352015-06-02 23:02:46 -04002777 if((uint64_t)chunk_header->total_sz != ((uint64_t)sparse_header->chunk_hdr_sz +
Ajay Dudani5c761132011-04-07 20:19:04 -07002778 chunk_data_sz))
2779 {
2780 fastboot_fail("Bogus chunk size for chunk type Raw");
2781 return;
2782 }
2783
vijay kumar800255e2015-04-24 20:26:19 +05302784 if (data_end < (uint32_t)data + chunk_data_sz) {
2785 fastboot_fail("buffer overreads occured due to invalid sparse header");
2786 return;
2787 }
2788
wufeng.jiang813dc352015-06-02 23:02:46 -04002789 /* chunk_header->total_sz is uint32,So chunk_data_sz is now less than 2^32
2790 otherwise it will return in the line above
2791 */
Ajay Dudaniab18f022011-05-12 14:39:22 -07002792 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
wufeng.jiang813dc352015-06-02 23:02:46 -04002793 (uint32_t)chunk_data_sz,
Ajay Dudaniab18f022011-05-12 14:39:22 -07002794 (unsigned int*)data))
Ajay Dudani5c761132011-04-07 20:19:04 -07002795 {
2796 fastboot_fail("flash write failure");
2797 return;
2798 }
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302799 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
2800 fastboot_fail("Bogus size for RAW chunk type");
2801 return;
2802 }
Ajay Dudani5c761132011-04-07 20:19:04 -07002803 total_blocks += chunk_header->chunk_sz;
wufeng.jiang813dc352015-06-02 23:02:46 -04002804 data += (uint32_t)chunk_data_sz;
Ajay Dudani5c761132011-04-07 20:19:04 -07002805 break;
2806
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002807 case CHUNK_TYPE_FILL:
2808 if(chunk_header->total_sz != (sparse_header->chunk_hdr_sz +
2809 sizeof(uint32_t)))
2810 {
2811 fastboot_fail("Bogus chunk size for chunk type FILL");
2812 return;
2813 }
2814
2815 fill_buf = (uint32_t *)memalign(CACHE_LINE, ROUNDUP(sparse_header->blk_sz, CACHE_LINE));
2816 if (!fill_buf)
2817 {
2818 fastboot_fail("Malloc failed for: CHUNK_TYPE_FILL");
2819 return;
2820 }
2821
vijay kumar800255e2015-04-24 20:26:19 +05302822 if (data_end < (uint32_t)data + sizeof(uint32_t)) {
2823 fastboot_fail("buffer overreads occured due to invalid sparse header");
2824 return;
2825 }
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002826 fill_val = *(uint32_t *)data;
2827 data = (char *) data + sizeof(uint32_t);
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002828
2829 for (i = 0; i < (sparse_header->blk_sz / sizeof(fill_val)); i++)
2830 {
2831 fill_buf[i] = fill_val;
2832 }
2833
wufeng.jiang813dc352015-06-02 23:02:46 -04002834 for (i = 0; i < chunk_header->chunk_sz; i++)
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002835 {
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302836 /* Make sure that the data written to partition does not exceed partition size */
2837 if ((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz + sparse_header->blk_sz > size)
2838 {
2839 fastboot_fail("Chunk data size for fill type exceeds partition size");
2840 return;
2841 }
2842
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002843 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
2844 sparse_header->blk_sz,
2845 fill_buf))
2846 {
2847 fastboot_fail("flash write failure");
2848 free(fill_buf);
2849 return;
2850 }
2851
2852 total_blocks++;
2853 }
2854
2855 free(fill_buf);
2856 break;
2857
Ajay Dudani5c761132011-04-07 20:19:04 -07002858 case CHUNK_TYPE_DONT_CARE:
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302859 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
2860 fastboot_fail("bogus size for chunk DONT CARE type");
2861 return;
2862 }
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07002863 total_blocks += chunk_header->chunk_sz;
2864 break;
2865
Ajay Dudani5c761132011-04-07 20:19:04 -07002866 case CHUNK_TYPE_CRC:
2867 if(chunk_header->total_sz != sparse_header->chunk_hdr_sz)
2868 {
wufeng.jiang813dc352015-06-02 23:02:46 -04002869 fastboot_fail("Bogus chunk size for chunk type CRC");
Ajay Dudani5c761132011-04-07 20:19:04 -07002870 return;
2871 }
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302872 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
2873 fastboot_fail("bogus size for chunk CRC type");
2874 return;
2875 }
Ajay Dudani5c761132011-04-07 20:19:04 -07002876 total_blocks += chunk_header->chunk_sz;
vijay kumar800255e2015-04-24 20:26:19 +05302877 if ((uint32_t)data > UINT_MAX - chunk_data_sz) {
2878 fastboot_fail("integer overflow occured");
2879 return;
2880 }
wufeng.jiang813dc352015-06-02 23:02:46 -04002881 data += (uint32_t)chunk_data_sz;
vijay kumar800255e2015-04-24 20:26:19 +05302882 if (data_end < (uint32_t)data) {
2883 fastboot_fail("buffer overreads occured due to invalid sparse header");
2884 return;
2885 }
Ajay Dudani5c761132011-04-07 20:19:04 -07002886 break;
2887
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07002888 default:
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002889 dprintf(CRITICAL, "Unkown chunk type: %x\n",chunk_header->chunk_type);
Ajay Dudani5c761132011-04-07 20:19:04 -07002890 fastboot_fail("Unknown chunk type");
2891 return;
2892 }
2893 }
2894
Ajay Dudani0c6927b2011-05-18 11:12:16 -07002895 dprintf(INFO, "Wrote %d blocks, expected to write %d blocks\n",
2896 total_blocks, sparse_header->total_blks);
2897
2898 if(total_blocks != sparse_header->total_blks)
2899 {
2900 fastboot_fail("sparse image write failure");
2901 }
Ajay Dudani5c761132011-04-07 20:19:04 -07002902
2903 fastboot_okay("");
2904 return;
2905}
2906
2907void cmd_flash_mmc(const char *arg, void *data, unsigned sz)
2908{
2909 sparse_header_t *sparse_header;
Ajay Dudanide984792015-03-02 09:57:41 -08002910 meta_header_t *meta_header;
Ajay Dudani5c761132011-04-07 20:19:04 -07002911
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07002912#ifdef SSD_ENABLE
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -08002913 /* 8 Byte Magic + 2048 Byte xml + Encrypted Data */
2914 unsigned int *magic_number = (unsigned int *) data;
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002915 int ret=0;
2916 uint32 major_version=0;
2917 uint32 minor_version=0;
2918
2919 ret = scm_svc_version(&major_version,&minor_version);
2920 if(!ret)
2921 {
2922 if(major_version >= 2)
2923 {
Amir Samuelovbb65ce02013-05-05 12:20:18 +03002924 if( !strcmp(arg, "ssd") || !strcmp(arg, "tqs") )
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002925 {
2926 ret = encrypt_scm((uint32 **) &data, &sz);
2927 if (ret != 0) {
2928 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
2929 return;
2930 }
2931
Amir Samuelovbb65ce02013-05-05 12:20:18 +03002932 /* Protect only for SSD */
2933 if (!strcmp(arg, "ssd")) {
2934 ret = scm_protect_keystore((uint32 *) data, sz);
2935 if (ret != 0) {
2936 dprintf(CRITICAL, "ERROR: scm_protect_keystore Failed\n");
2937 return;
2938 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002939 }
2940 }
2941 else
2942 {
2943 ret = decrypt_scm_v2((uint32 **) &data, &sz);
2944 if(ret != 0)
2945 {
2946 dprintf(CRITICAL,"ERROR: Decryption Failure\n");
2947 return;
2948 }
2949 }
2950 }
2951 else
2952 {
2953 if (magic_number[0] == DECRYPT_MAGIC_0 &&
2954 magic_number[1] == DECRYPT_MAGIC_1)
2955 {
2956 ret = decrypt_scm((uint32 **) &data, &sz);
2957 if (ret != 0) {
2958 dprintf(CRITICAL, "ERROR: Invalid secure image\n");
2959 return;
2960 }
2961 }
2962 else if (magic_number[0] == ENCRYPT_MAGIC_0 &&
2963 magic_number[1] == ENCRYPT_MAGIC_1)
2964 {
2965 ret = encrypt_scm((uint32 **) &data, &sz);
2966 if (ret != 0) {
2967 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
2968 return;
2969 }
2970 }
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07002971 }
2972 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002973 else
Neeti Desai127b9e02012-03-20 16:11:23 -07002974 {
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002975 dprintf(CRITICAL,"INVALID SVC Version\n");
2976 return;
Neeti Desai127b9e02012-03-20 16:11:23 -07002977 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002978#endif /* SSD_ENABLE */
Neeti Desai127b9e02012-03-20 16:11:23 -07002979
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002980#if VERIFIED_BOOT
Channagoud Kadabi35297672015-06-13 11:09:49 -07002981 if (target_build_variant_user())
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002982 {
lijuang511a2b52015-08-14 20:50:51 +08002983 /* if device is locked:
2984 * common partition will not allow to be flashed
2985 * critical partition will allow to flash image.
2986 */
2987 if(!device.is_unlocked && !critical_flash_allowed(arg)) {
2988 fastboot_fail("Partition flashing is not allowed");
2989 return;
2990 }
Parth Dixitddbc7352015-10-18 03:13:31 +05302991#if !VBOOT_MOTA
lijuang511a2b52015-08-14 20:50:51 +08002992 /* if device critical is locked:
2993 * common partition will allow to be flashed
2994 * critical partition will not allow to flash image.
2995 */
2996 if(!device.is_unlock_critical && critical_flash_allowed(arg)) {
2997 fastboot_fail("Critical partition flashing is not allowed");
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002998 return;
2999 }
Parth Dixitddbc7352015-10-18 03:13:31 +05303000#endif
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003001 }
3002#endif
3003
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07003004 sparse_header = (sparse_header_t *) data;
Ajay Dudanide984792015-03-02 09:57:41 -08003005 meta_header = (meta_header_t *) data;
3006 if (sparse_header->magic == SPARSE_HEADER_MAGIC)
Ajay Dudani5c761132011-04-07 20:19:04 -07003007 cmd_flash_mmc_sparse_img(arg, data, sz);
Ajay Dudanide984792015-03-02 09:57:41 -08003008 else if (meta_header->magic == META_HEADER_MAGIC)
3009 cmd_flash_meta_img(arg, data, sz);
3010 else
3011 cmd_flash_mmc_img(arg, data, sz);
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07003012
3013#if VERIFIED_BOOT
Parth Dixitddbc7352015-10-18 03:13:31 +05303014#if !VBOOT_MOTA
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07003015 if((!strncmp(arg, "system", 6)) && !device.verity_mode)
3016 {
3017 // reset dm_verity mode to enforcing
3018 device.verity_mode = 1;
3019 write_device_info(&device);
3020 }
3021#endif
Parth Dixitddbc7352015-10-18 03:13:31 +05303022#endif
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07003023
Ajay Dudani5c761132011-04-07 20:19:04 -07003024 return;
3025}
3026
Tanya Brokhman84eaaf62015-02-24 21:57:06 +02003027void cmd_updatevol(const char *vol_name, void *data, unsigned sz)
3028{
3029 struct ptentry *sys_ptn;
3030 struct ptable *ptable;
3031
3032 ptable = flash_get_ptable();
3033 if (ptable == NULL) {
3034 fastboot_fail("partition table doesn't exist");
3035 return;
3036 }
3037
3038 sys_ptn = ptable_find(ptable, "system");
3039 if (sys_ptn == NULL) {
3040 fastboot_fail("system partition not found");
3041 return;
3042 }
3043
3044 sz = ROUND_TO_PAGE(sz, page_mask);
3045 if (update_ubi_vol(sys_ptn, vol_name, data, sz))
3046 fastboot_fail("update_ubi_vol failed");
3047 else
3048 fastboot_okay("");
3049}
3050
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003051void cmd_flash_nand(const char *arg, void *data, unsigned sz)
Dima Zavin214cc642009-01-26 11:16:21 -08003052{
3053 struct ptentry *ptn;
3054 struct ptable *ptable;
3055 unsigned extra = 0;
3056
3057 ptable = flash_get_ptable();
3058 if (ptable == NULL) {
3059 fastboot_fail("partition table doesn't exist");
3060 return;
3061 }
3062
3063 ptn = ptable_find(ptable, arg);
3064 if (ptn == NULL) {
Tanya Brokhman84eaaf62015-02-24 21:57:06 +02003065 dprintf(INFO, "unknown partition name (%s). Trying updatevol\n",
3066 arg);
3067 cmd_updatevol(arg, data, sz);
Dima Zavin214cc642009-01-26 11:16:21 -08003068 return;
3069 }
3070
3071 if (!strcmp(ptn->name, "boot") || !strcmp(ptn->name, "recovery")) {
3072 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
3073 fastboot_fail("image is not a boot image");
3074 return;
3075 }
3076 }
3077
Amol Jadi5c61a952012-05-04 17:05:35 -07003078 if (!strcmp(ptn->name, "system")
Deepa Dinamani13e32c42012-03-12 14:34:17 -07003079 || !strcmp(ptn->name, "userdata")
3080 || !strcmp(ptn->name, "persist")
Sundarajan Srinivasanb063a852013-11-19 14:02:27 -08003081 || !strcmp(ptn->name, "recoveryfs")
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -08003082 || !strcmp(ptn->name, "modem"))
Tanya Brokhman1c94f1a2015-02-15 09:05:03 +02003083 extra = 1;
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -08003084 else
Shashank Mittaldcc2e352009-11-19 19:11:16 -08003085 sz = ROUND_TO_PAGE(sz, page_mask);
Dima Zavin214cc642009-01-26 11:16:21 -08003086
3087 dprintf(INFO, "writing %d bytes to '%s'\n", sz, ptn->name);
Tanya Brokhman1c94f1a2015-02-15 09:05:03 +02003088 if (!memcmp((void *)data, UBI_MAGIC, UBI_MAGIC_SIZE)) {
3089 if (flash_ubi_img(ptn, data, sz)) {
3090 fastboot_fail("flash write failure");
3091 return;
3092 }
3093 } else {
3094 if (flash_write(ptn, extra, data, sz)) {
3095 fastboot_fail("flash write failure");
3096 return;
3097 }
Dima Zavin214cc642009-01-26 11:16:21 -08003098 }
3099 dprintf(INFO, "partition '%s' updated\n", ptn->name);
3100 fastboot_okay("");
3101}
3102
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003103void cmd_flash(const char *arg, void *data, unsigned sz)
3104{
3105 if(target_is_emmc_boot())
3106 cmd_flash_mmc(arg, data, sz);
3107 else
3108 cmd_flash_nand(arg, data, sz);
3109}
3110
Dima Zavin214cc642009-01-26 11:16:21 -08003111void cmd_continue(const char *arg, void *data, unsigned sz)
3112{
3113 fastboot_okay("");
Amol Jadi7c4316c2013-10-07 14:19:26 -07003114 fastboot_stop();
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003115
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003116 if (target_is_emmc_boot())
3117 {
lijuanga40d6302015-07-20 20:10:13 +08003118#if FBCON_DISPLAY_MSG
lijuangde34d502016-02-26 16:04:50 +08003119 /* Exit keys' detection thread firstly */
3120 exit_menu_keys_detection();
lijuanga40d6302015-07-20 20:10:13 +08003121#endif
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003122 boot_linux_from_mmc();
3123 }
3124 else
3125 {
3126 boot_linux_from_flash();
3127 }
Dima Zavin214cc642009-01-26 11:16:21 -08003128}
3129
Chandan Uddaraju94183c02010-01-15 15:13:59 -08003130void cmd_reboot(const char *arg, void *data, unsigned sz)
3131{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003132 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08003133 fastboot_okay("");
3134 reboot_device(0);
3135}
3136
3137void cmd_reboot_bootloader(const char *arg, void *data, unsigned sz)
3138{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003139 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08003140 fastboot_okay("");
3141 reboot_device(FASTBOOT_MODE);
3142}
3143
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003144void cmd_oem_enable_charger_screen(const char *arg, void *data, unsigned size)
3145{
3146 dprintf(INFO, "Enabling charger screen check\n");
3147 device.charger_screen_enabled = 1;
3148 write_device_info(&device);
3149 fastboot_okay("");
3150}
3151
3152void cmd_oem_disable_charger_screen(const char *arg, void *data, unsigned size)
3153{
3154 dprintf(INFO, "Disabling charger screen check\n");
3155 device.charger_screen_enabled = 0;
3156 write_device_info(&device);
3157 fastboot_okay("");
3158}
3159
lijuanga25d8bb2015-09-16 13:11:52 +08003160void cmd_oem_off_mode_charger(const char *arg, void *data, unsigned size)
3161{
3162 char *p = NULL;
3163 const char *delim = " \t\n\r";
Parth Dixit407f15b2016-01-07 14:47:37 +05303164 char *sp;
lijuanga25d8bb2015-09-16 13:11:52 +08003165
3166 if (arg) {
Parth Dixit407f15b2016-01-07 14:47:37 +05303167 p = strtok_r((char *)arg, delim, &sp);
lijuanga25d8bb2015-09-16 13:11:52 +08003168 if (p) {
3169 if (!strncmp(p, "0", 1)) {
3170 device.charger_screen_enabled = 0;
3171 } else if (!strncmp(p, "1", 1)) {
3172 device.charger_screen_enabled = 1;
3173 }
3174 }
3175 }
3176
3177 /* update charger_screen_enabled value for getvar
3178 * command
3179 */
3180 snprintf(charger_screen_enabled, MAX_RSP_SIZE, "%d",
3181 device.charger_screen_enabled);
3182
3183 write_device_info(&device);
3184 fastboot_okay("");
3185}
3186
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05303187void cmd_oem_select_display_panel(const char *arg, void *data, unsigned size)
3188{
3189 dprintf(INFO, "Selecting display panel %s\n", arg);
3190 if (arg)
3191 strlcpy(device.display_panel, arg,
3192 sizeof(device.display_panel));
3193 write_device_info(&device);
3194 fastboot_okay("");
3195}
3196
Shashank Mittal162244e2011-08-08 19:01:25 -07003197void cmd_oem_unlock(const char *arg, void *data, unsigned sz)
3198{
lijuang4ece1e72015-08-14 21:02:36 +08003199 set_device_unlock(UNLOCK, TRUE);
vijay kumarc65876c2015-04-24 13:29:16 +05303200}
3201
3202void cmd_oem_unlock_go(const char *arg, void *data, unsigned sz)
3203{
lijuang4ece1e72015-08-14 21:02:36 +08003204 if(!device.is_unlocked) {
vijay kumarc65876c2015-04-24 13:29:16 +05303205 if(!is_allow_unlock) {
3206 fastboot_fail("oem unlock is not allowed");
3207 return;
3208 }
3209
lijuang4ece1e72015-08-14 21:02:36 +08003210 set_device_unlock_value(UNLOCK, TRUE);
vijay kumarc65876c2015-04-24 13:29:16 +05303211
lijuang4ece1e72015-08-14 21:02:36 +08003212 /* wipe data */
vijay kumarc65876c2015-04-24 13:29:16 +05303213 struct recovery_message msg;
lijuang4ece1e72015-08-14 21:02:36 +08003214
vijay kumarc65876c2015-04-24 13:29:16 +05303215 snprintf(msg.recovery, sizeof(msg.recovery), "recovery\n--wipe_data");
3216 write_misc(0, &msg, sizeof(msg));
3217
3218 fastboot_okay("");
3219 reboot_device(RECOVERY_MODE);
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003220 }
3221 fastboot_okay("");
3222}
3223
Channagoud Kadabiad259832015-05-29 11:14:17 -07003224static int aboot_frp_unlock(char *pname, void *data, unsigned sz)
3225{
3226 int ret = 1;
3227 uint32_t secret_key;
3228 char seckey_buffer[MAX_RSP_SIZE];
3229
3230 secret_key = aboot_get_secret_key();
3231 if (secret_key)
3232 {
3233 snprintf((char *) seckey_buffer, MAX_RSP_SIZE, "%x", secret_key);
3234 if (!memcmp((void *)data, (void *)seckey_buffer, sz))
3235 {
3236 is_allow_unlock = true;
3237 write_allow_oem_unlock(is_allow_unlock);
3238 ret = 0;
3239 }
3240 }
3241 return ret;
3242}
3243
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003244void cmd_oem_lock(const char *arg, void *data, unsigned sz)
3245{
lijuang4ece1e72015-08-14 21:02:36 +08003246 set_device_unlock(UNLOCK, FALSE);
Shashank Mittal162244e2011-08-08 19:01:25 -07003247}
3248
Shashank Mittala0032282011-08-26 14:50:11 -07003249void cmd_oem_devinfo(const char *arg, void *data, unsigned sz)
3250{
lijuang511a2b52015-08-14 20:50:51 +08003251 char response[MAX_RSP_SIZE];
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003252 snprintf(response, sizeof(response), "\tDevice tampered: %s", (device.is_tampered ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07003253 fastboot_info(response);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003254 snprintf(response, sizeof(response), "\tDevice unlocked: %s", (device.is_unlocked ? "true" : "false"));
3255 fastboot_info(response);
Parth Dixitddbc7352015-10-18 03:13:31 +05303256#if !VBOOT_MOTA
lijuang511a2b52015-08-14 20:50:51 +08003257 snprintf(response, sizeof(response), "\tDevice critical unlocked: %s", (device.is_unlock_critical ? "true" : "false"));
3258 fastboot_info(response);
Parth Dixitddbc7352015-10-18 03:13:31 +05303259#endif
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003260 snprintf(response, sizeof(response), "\tCharger screen enabled: %s", (device.charger_screen_enabled ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07003261 fastboot_info(response);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05303262 snprintf(response, sizeof(response), "\tDisplay panel: %s", (device.display_panel));
3263 fastboot_info(response);
Shashank Mittala0032282011-08-26 14:50:11 -07003264 fastboot_okay("");
3265}
3266
lijuang511a2b52015-08-14 20:50:51 +08003267void cmd_flashing_get_unlock_ability(const char *arg, void *data, unsigned sz)
3268{
3269 char response[MAX_RSP_SIZE];
3270 snprintf(response, sizeof(response), "\tget_unlock_ability: %d", is_allow_unlock);
3271 fastboot_info(response);
3272 fastboot_okay("");
3273}
3274
3275void cmd_flashing_lock_critical(const char *arg, void *data, unsigned sz)
3276{
lijuang4ece1e72015-08-14 21:02:36 +08003277 set_device_unlock(UNLOCK_CRITICAL, FALSE);
lijuang511a2b52015-08-14 20:50:51 +08003278}
3279
3280void cmd_flashing_unlock_critical(const char *arg, void *data, unsigned sz)
3281{
lijuang4ece1e72015-08-14 21:02:36 +08003282 set_device_unlock(UNLOCK_CRITICAL, TRUE);
lijuang511a2b52015-08-14 20:50:51 +08003283}
3284
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07003285void cmd_preflash(const char *arg, void *data, unsigned sz)
3286{
3287 fastboot_okay("");
3288}
3289
Mao Flynn7b379f32015-04-20 00:28:30 +08003290static uint8_t logo_header[LOGO_IMG_HEADER_SIZE];
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303291
Mao Flynn7b379f32015-04-20 00:28:30 +08003292int splash_screen_check_header(logo_img_header *header)
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303293{
Mao Flynn7b379f32015-04-20 00:28:30 +08003294 if (memcmp(header->magic, LOGO_IMG_MAGIC, 8))
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303295 return -1;
Mao Flynn7b379f32015-04-20 00:28:30 +08003296 if (header->width == 0 || header->height == 0)
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303297 return -1;
3298 return 0;
3299}
3300
Mao Flynn7b379f32015-04-20 00:28:30 +08003301int splash_screen_flash()
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003302{
3303 struct ptentry *ptn;
3304 struct ptable *ptable;
Mao Flynn7b379f32015-04-20 00:28:30 +08003305 struct logo_img_header *header;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003306 struct fbcon_config *fb_display = NULL;
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08003307
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303308 ptable = flash_get_ptable();
3309 if (ptable == NULL) {
Mao Flynn7b379f32015-04-20 00:28:30 +08003310 dprintf(CRITICAL, "ERROR: Partition table not found\n");
3311 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303312 }
Mao Flynn7b379f32015-04-20 00:28:30 +08003313
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303314 ptn = ptable_find(ptable, "splash");
3315 if (ptn == NULL) {
3316 dprintf(CRITICAL, "ERROR: splash Partition not found\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003317 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303318 }
Mao Flynn7b379f32015-04-20 00:28:30 +08003319 if (flash_read(ptn, 0, (void *)logo_header, LOGO_IMG_HEADER_SIZE)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303320 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003321 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303322 }
3323
Mao Flynn7b379f32015-04-20 00:28:30 +08003324 header = (struct logo_img_header *)logo_header;
3325 if (splash_screen_check_header(header)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303326 dprintf(CRITICAL, "ERROR: Boot image header invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003327 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303328 }
3329
3330 fb_display = fbcon_display();
3331 if (fb_display) {
Mao Flynn7b379f32015-04-20 00:28:30 +08003332 if (header->type && (header->blocks != 0)) { // RLE24 compressed data
3333 uint8_t *base = (uint8_t *) fb_display->base + LOGO_IMG_OFFSET;
3334
3335 /* if the logo is full-screen size, remove "fbcon_clear()" */
3336 if ((header->width != fb_display->width)
3337 || (header->height != fb_display->height))
3338 fbcon_clear();
3339
3340 if (flash_read(ptn + LOGO_IMG_HEADER_SIZE, 0,
3341 (uint32_t *)base,
3342 (header->blocks * 512))) {
3343 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
3344 return -1;
3345 }
3346 fbcon_extract_to_screen(header, base);
3347 return 0;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003348 }
Mao Flynn7b379f32015-04-20 00:28:30 +08003349
Veera Sundaram Sankaranc33416f2015-09-28 10:25:55 -07003350 if ((header->width > fb_display->width) || (header->height > fb_display->height)) {
3351 dprintf(CRITICAL, "Logo config greater than fb config. Fall back default logo\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003352 return -1;
3353 }
3354
Vineet Bajaj99291ed2014-09-09 12:29:46 +05303355 uint8_t *base = (uint8_t *) fb_display->base;
Mao Flynn7b379f32015-04-20 00:28:30 +08003356 if (flash_read(ptn + LOGO_IMG_HEADER_SIZE, 0,
3357 (uint32_t *)base,
3358 ((((header->width * header->height * fb_display->bpp/8) + 511) >> 9) << 9))) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303359 fbcon_clear();
Vineet Bajaj99291ed2014-09-09 12:29:46 +05303360 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003361 return -1;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003362 }
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303363 }
3364
Mao Flynn7b379f32015-04-20 00:28:30 +08003365 return 0;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303366}
3367
Mao Flynn7b379f32015-04-20 00:28:30 +08003368int splash_screen_mmc()
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303369{
3370 int index = INVALID_PTN;
3371 unsigned long long ptn = 0;
3372 struct fbcon_config *fb_display = NULL;
Mao Flynn7b379f32015-04-20 00:28:30 +08003373 struct logo_img_header *header;
Mao Flynn1893a7f2015-06-03 12:03:36 +08003374 uint32_t blocksize, realsize, readsize;
3375 uint8_t *base;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303376
3377 index = partition_get_index("splash");
3378 if (index == 0) {
3379 dprintf(CRITICAL, "ERROR: splash Partition table not found\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003380 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303381 }
3382
3383 ptn = partition_get_offset(index);
3384 if (ptn == 0) {
3385 dprintf(CRITICAL, "ERROR: splash Partition invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003386 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303387 }
3388
Mao Flynn1893a7f2015-06-03 12:03:36 +08003389 mmc_set_lun(partition_get_lun(index));
3390
3391 blocksize = mmc_get_device_blocksize();
3392 if (blocksize == 0) {
3393 dprintf(CRITICAL, "ERROR:splash Partition invalid blocksize\n");
3394 return -1;
3395 }
3396
3397 fb_display = fbcon_display();
Channagoud Kadabidaf10a62015-07-22 11:51:55 -07003398 if (!fb_display)
3399 {
3400 dprintf(CRITICAL, "ERROR: fb config is not allocated\n");
3401 return -1;
3402 }
3403
Mao Flynn1893a7f2015-06-03 12:03:36 +08003404 base = (uint8_t *) fb_display->base;
3405
3406 if (mmc_read(ptn, (uint32_t *)(base + LOGO_IMG_OFFSET), blocksize)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303407 dprintf(CRITICAL, "ERROR: Cannot read splash image header\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003408 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303409 }
3410
Mao Flynn1893a7f2015-06-03 12:03:36 +08003411 header = (struct logo_img_header *)(base + LOGO_IMG_OFFSET);
Mao Flynn7b379f32015-04-20 00:28:30 +08003412 if (splash_screen_check_header(header)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303413 dprintf(CRITICAL, "ERROR: Splash image header invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003414 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303415 }
3416
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303417 if (fb_display) {
Mao Flynn1893a7f2015-06-03 12:03:36 +08003418 if (header->type && (header->blocks != 0)) { /* 1 RLE24 compressed data */
3419 base += LOGO_IMG_OFFSET;
Mao Flynn7b379f32015-04-20 00:28:30 +08003420
Mao Flynn1893a7f2015-06-03 12:03:36 +08003421 realsize = header->blocks * 512;
3422 readsize = ROUNDUP((realsize + LOGO_IMG_HEADER_SIZE), blocksize) - blocksize;
3423
3424 /* if the logo is not full-screen size, clean screen */
Mao Flynn7b379f32015-04-20 00:28:30 +08003425 if ((header->width != fb_display->width)
3426 || (header->height != fb_display->height))
3427 fbcon_clear();
3428
Mao Flynn1893a7f2015-06-03 12:03:36 +08003429 if (mmc_read(ptn + blocksize, (uint32_t *)(base + blocksize), readsize)) {
Mao Flynn7b379f32015-04-20 00:28:30 +08003430 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
3431 return -1;
3432 }
Mao Flynn7b379f32015-04-20 00:28:30 +08003433
Mao Flynn1893a7f2015-06-03 12:03:36 +08003434 fbcon_extract_to_screen(header, (base + LOGO_IMG_HEADER_SIZE));
3435 } else { /* 2 Raw BGR data */
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303436
Veera Sundaram Sankaranc33416f2015-09-28 10:25:55 -07003437 if ((header->width > fb_display->width) || (header->height > fb_display->height)) {
3438 dprintf(CRITICAL, "Logo config greater than fb config. Fall back default logo\n");
Mao Flynn1893a7f2015-06-03 12:03:36 +08003439 return -1;
3440 }
3441
3442 realsize = header->width * header->height * fb_display->bpp / 8;
3443 readsize = ROUNDUP((realsize + LOGO_IMG_HEADER_SIZE), blocksize) - blocksize;
3444
3445 if (blocksize == LOGO_IMG_HEADER_SIZE) { /* read the content directly */
3446 if (mmc_read((ptn + LOGO_IMG_HEADER_SIZE), (uint32_t *)base, readsize)) {
3447 fbcon_clear();
3448 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
3449 return -1;
3450 }
3451 } else {
3452 if (mmc_read(ptn + blocksize ,
3453 (uint32_t *)(base + LOGO_IMG_OFFSET + blocksize), readsize)) {
3454 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
3455 return -1;
3456 }
3457 memmove(base, (base + LOGO_IMG_OFFSET + LOGO_IMG_HEADER_SIZE), realsize);
3458 }
3459 }
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303460 }
3461
Mao Flynn7b379f32015-04-20 00:28:30 +08003462 return 0;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303463}
3464
Mao Flynn7b379f32015-04-20 00:28:30 +08003465int fetch_image_from_partition()
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303466{
3467 if (target_is_emmc_boot()) {
3468 return splash_screen_mmc();
3469 } else {
3470 return splash_screen_flash();
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003471 }
3472}
3473
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07003474/* Get the size from partiton name */
3475static void get_partition_size(const char *arg, char *response)
3476{
3477 uint64_t ptn = 0;
3478 uint64_t size;
3479 int index = INVALID_PTN;
3480
3481 index = partition_get_index(arg);
3482
3483 if (index == INVALID_PTN)
3484 {
3485 dprintf(CRITICAL, "Invalid partition index\n");
3486 return;
3487 }
3488
3489 ptn = partition_get_offset(index);
3490
3491 if(!ptn)
3492 {
3493 dprintf(CRITICAL, "Invalid partition name %s\n", arg);
3494 return;
3495 }
3496
3497 size = partition_get_size(index);
3498
3499 snprintf(response, MAX_RSP_SIZE, "\t 0x%llx", size);
3500 return;
3501}
3502
3503/*
3504 * Publish the partition type & size info
3505 * fastboot getvar will publish the required information.
3506 * fastboot getvar partition_size:<partition_name>: partition size in hex
3507 * fastboot getvar partition_type:<partition_name>: partition type (ext/fat)
3508 */
3509static void publish_getvar_partition_info(struct getvar_partition_info *info, uint8_t num_parts)
3510{
3511 uint8_t i;
3512
3513 for (i = 0; i < num_parts; i++) {
3514 get_partition_size(info[i].part_name, info[i].size_response);
3515
3516 if (strlcat(info[i].getvar_size, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
3517 {
3518 dprintf(CRITICAL, "partition size name truncated\n");
3519 return;
3520 }
3521 if (strlcat(info[i].getvar_type, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
3522 {
3523 dprintf(CRITICAL, "partition type name truncated\n");
3524 return;
3525 }
3526
3527 /* publish partition size & type info */
3528 fastboot_publish((const char *) info[i].getvar_size, (const char *) info[i].size_response);
3529 fastboot_publish((const char *) info[i].getvar_type, (const char *) info[i].type_response);
3530 }
3531}
3532
lijuang4ece1e72015-08-14 21:02:36 +08003533void get_product_name(unsigned char *buf)
3534{
3535 snprintf((char*)buf, MAX_RSP_SIZE, "%s", TARGET(BOARD));
3536 return;
3537}
3538
3539void get_bootloader_version(unsigned char *buf)
3540{
3541 snprintf((char*)buf, MAX_RSP_SIZE, "%s", device.bootloader_version);
3542 return;
3543}
3544
3545void get_baseband_version(unsigned char *buf)
3546{
3547 snprintf((char*)buf, MAX_RSP_SIZE, "%s", device.radio_version);
3548 return;
3549}
3550
3551bool is_device_locked()
3552{
3553 return device.is_unlocked ? false:true;
3554}
3555
Amol Jadi5edf3552013-07-23 14:15:34 -07003556/* register commands and variables for fastboot */
3557void aboot_fastboot_register_commands(void)
3558{
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003559 int i;
lijuangf16461c2015-08-03 17:09:34 +08003560 char hw_platform_buf[MAX_RSP_SIZE];
Amol Jadi5edf3552013-07-23 14:15:34 -07003561
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003562 struct fastboot_cmd_desc cmd_list[] = {
lijuang511a2b52015-08-14 20:50:51 +08003563 /* By default the enabled list is empty. */
3564 {"", NULL},
3565 /* move commands enclosed within the below ifndef to here
3566 * if they need to be enabled in user build.
3567 */
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003568#ifndef DISABLE_FASTBOOT_CMDS
lijuang511a2b52015-08-14 20:50:51 +08003569 /* Register the following commands only for non-user builds */
3570 {"flash:", cmd_flash},
3571 {"erase:", cmd_erase},
3572 {"boot", cmd_boot},
3573 {"continue", cmd_continue},
3574 {"reboot", cmd_reboot},
3575 {"reboot-bootloader", cmd_reboot_bootloader},
3576 {"oem unlock", cmd_oem_unlock},
3577 {"oem unlock-go", cmd_oem_unlock_go},
3578 {"oem lock", cmd_oem_lock},
3579 {"flashing unlock", cmd_oem_unlock},
3580 {"flashing lock", cmd_oem_lock},
3581 {"flashing lock_critical", cmd_flashing_lock_critical},
3582 {"flashing unlock_critical", cmd_flashing_unlock_critical},
3583 {"flashing get_unlock_ability", cmd_flashing_get_unlock_ability},
3584 {"oem device-info", cmd_oem_devinfo},
3585 {"preflash", cmd_preflash},
3586 {"oem enable-charger-screen", cmd_oem_enable_charger_screen},
3587 {"oem disable-charger-screen", cmd_oem_disable_charger_screen},
lijuanga25d8bb2015-09-16 13:11:52 +08003588 {"oem off-mode-charge", cmd_oem_off_mode_charger},
lijuang511a2b52015-08-14 20:50:51 +08003589 {"oem select-display-panel", cmd_oem_select_display_panel},
Channagoud Kadabi736c4962015-08-21 11:56:52 -07003590#if UNITTEST_FW_SUPPORT
Channagoud Kadabid5ddf482015-09-28 10:31:35 -07003591 {"oem run-tests", cmd_oem_runtests},
Channagoud Kadabi736c4962015-08-21 11:56:52 -07003592#endif
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003593#endif
lijuang511a2b52015-08-14 20:50:51 +08003594 };
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003595
3596 int fastboot_cmds_count = sizeof(cmd_list)/sizeof(cmd_list[0]);
3597 for (i = 1; i < fastboot_cmds_count; i++)
3598 fastboot_register(cmd_list[i].name,cmd_list[i].cb);
3599
Amol Jadi5edf3552013-07-23 14:15:34 -07003600 /* publish variables and their values */
3601 fastboot_publish("product", TARGET(BOARD));
3602 fastboot_publish("kernel", "lk");
3603 fastboot_publish("serialno", sn_buf);
3604
3605 /*
3606 * partition info is supported only for emmc partitions
3607 * Calling this for NAND prints some error messages which
3608 * is harmless but misleading. Avoid calling this for NAND
3609 * devices.
3610 */
3611 if (target_is_emmc_boot())
3612 publish_getvar_partition_info(part_info, ARRAY_SIZE(part_info));
3613
3614 /* Max download size supported */
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003615 snprintf(max_download_size, MAX_RSP_SIZE, "\t0x%x",
3616 target_get_max_flash_size());
Amol Jadi5edf3552013-07-23 14:15:34 -07003617 fastboot_publish("max-download-size", (const char *) max_download_size);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003618 /* Is the charger screen check enabled */
3619 snprintf(charger_screen_enabled, MAX_RSP_SIZE, "%d",
3620 device.charger_screen_enabled);
3621 fastboot_publish("charger-screen-enabled",
3622 (const char *) charger_screen_enabled);
lijuanga25d8bb2015-09-16 13:11:52 +08003623 fastboot_publish("off-mode-charge", (const char *) charger_screen_enabled);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05303624 snprintf(panel_display_mode, MAX_RSP_SIZE, "%s",
3625 device.display_panel);
3626 fastboot_publish("display-panel",
3627 (const char *) panel_display_mode);
Channagoud Kadabiaafbbfc2015-03-04 17:52:56 -08003628 fastboot_publish("version-bootloader", (const char *) device.bootloader_version);
3629 fastboot_publish("version-baseband", (const char *) device.radio_version);
lijuangf16461c2015-08-03 17:09:34 +08003630 fastboot_publish("secure", is_secure_boot_enable()? "yes":"no");
3631 smem_get_hw_platform_name((unsigned char *) hw_platform_buf, sizeof(hw_platform_buf));
3632 snprintf(get_variant, MAX_RSP_SIZE, "%s %s", hw_platform_buf,
3633 target_is_emmc_boot()? "eMMC":"UFS");
3634 fastboot_publish("variant", (const char *) get_variant);
lijuang65c5a822015-08-29 16:35:36 +08003635#if CHECK_BAT_VOLTAGE
lijuang102dfa92015-10-09 18:31:03 +08003636 update_battery_status();
lijuang65c5a822015-08-29 16:35:36 +08003637 fastboot_publish("battery-voltage", (const char *) battery_voltage);
lijuang102dfa92015-10-09 18:31:03 +08003638 fastboot_publish("battery-soc-ok", (const char *) battery_soc_ok);
lijuang65c5a822015-08-29 16:35:36 +08003639#endif
Amol Jadi5edf3552013-07-23 14:15:34 -07003640}
3641
Brian Swetland9c4c0752009-01-25 16:23:50 -08003642void aboot_init(const struct app_descriptor *app)
3643{
Shashank Mittal4f99a882010-02-01 13:58:50 -08003644 unsigned reboot_mode = 0;
Chandan Uddarajubedca152010-06-02 23:05:15 -07003645
lijuang39831732016-01-08 17:49:02 +08003646 /* Initialise wdog to catch early lk crashes */
3647#if WDOG_SUPPORT
3648 msm_wdog_init();
3649#endif
3650
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07003651 /* Setup page size information for nv storage */
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003652 if (target_is_emmc_boot())
3653 {
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07003654 page_size = mmc_page_size();
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003655 page_mask = page_size - 1;
3656 }
3657 else
3658 {
3659 page_size = flash_page_size();
3660 page_mask = page_size - 1;
3661 }
3662
Deepa Dinamani0e163a42013-05-24 17:08:15 -07003663 ASSERT((MEMBASE + MEMSIZE) > MEMBASE);
3664
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003665 read_device_info(&device);
vijay kumarc65876c2015-04-24 13:29:16 +05303666 read_allow_oem_unlock(&device);
Shashank Mittal162244e2011-08-08 19:01:25 -07003667
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08003668 /* Display splash screen if enabled */
3669#if DISPLAY_SPLASH_SCREEN
lijuang99c02d82015-02-13 19:04:34 +08003670#if NO_ALARM_DISPLAY
3671 if (!check_alarm_boot()) {
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08003672#endif
lijuang99c02d82015-02-13 19:04:34 +08003673 dprintf(SPEW, "Display Init: Start\n");
Channagoud Kadabi90869ce2015-04-27 11:15:14 -07003674#if ENABLE_WBC
Channagoud Kadabid801ac72015-08-26 11:21:51 -07003675 /* Wait if the display shutdown is in progress */
3676 while(pm_app_display_shutdown_in_prgs());
Channagoud Kadabi90869ce2015-04-27 11:15:14 -07003677 if (!pm_appsbl_display_init_done())
3678 target_display_init(device.display_panel);
3679 else
3680 display_image_on_screen();
3681#else
lijuang99c02d82015-02-13 19:04:34 +08003682 target_display_init(device.display_panel);
Channagoud Kadabi90869ce2015-04-27 11:15:14 -07003683#endif
lijuang99c02d82015-02-13 19:04:34 +08003684 dprintf(SPEW, "Display Init: Done\n");
3685#if NO_ALARM_DISPLAY
3686 }
3687#endif
3688#endif
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08003689
Greg Griscod6250552011-06-29 14:40:23 -07003690 target_serialno((unsigned char *) sn_buf);
Ajay Dudanib06c05f2011-05-12 14:46:10 -07003691 dprintf(SPEW,"serial number: %s\n",sn_buf);
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -08003692
Dhaval Patel223ec952013-07-18 14:49:44 -07003693 memset(display_panel_buf, '\0', MAX_PANEL_BUF_SIZE);
3694
Matthew Qindefd5562014-07-11 18:02:40 +08003695 /*
3696 * Check power off reason if user force reset,
3697 * if yes phone will do normal boot.
3698 */
3699 if (is_user_force_reset())
3700 goto normal_boot;
3701
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003702 /* Check if we should do something other than booting up */
Ameya Thakur0b9c2442013-05-10 13:22:56 -07003703 if (keys_get_state(KEY_VOLUMEUP) && keys_get_state(KEY_VOLUMEDOWN))
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07003704 {
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03003705 dprintf(ALWAYS,"dload mode key sequence detected\n");
lijuang395b5e62015-11-19 17:39:44 +08003706 reboot_device(EMERGENCY_DLOAD);
3707 dprintf(CRITICAL,"Failed to reboot into dload mode\n");
3708
Ameya Thakur0b9c2442013-05-10 13:22:56 -07003709 boot_into_fastboot = true;
3710 }
3711 if (!boot_into_fastboot)
3712 {
3713 if (keys_get_state(KEY_HOME) || keys_get_state(KEY_VOLUMEUP))
3714 boot_into_recovery = 1;
3715 if (!boot_into_recovery &&
3716 (keys_get_state(KEY_BACK) || keys_get_state(KEY_VOLUMEDOWN)))
Pavel Nedev5d91d412013-04-29 11:34:24 +03003717 boot_into_fastboot = true;
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07003718 }
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003719 #if NO_KEYPAD_DRIVER
Kinson Chik0b1c8162011-08-31 16:31:57 -07003720 if (fastboot_trigger())
Pavel Nedev5d91d412013-04-29 11:34:24 +03003721 boot_into_fastboot = true;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003722 #endif
Chandan Uddarajubedca152010-06-02 23:05:15 -07003723
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003724#if USE_PON_REBOOT_REG
3725 reboot_mode = check_hard_reboot_mode();
3726#else
Ajay Dudani77421292010-10-27 19:34:06 -07003727 reboot_mode = check_reboot_mode();
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003728#endif
3729 if (reboot_mode == RECOVERY_MODE)
3730 {
Ajay Dudani77421292010-10-27 19:34:06 -07003731 boot_into_recovery = 1;
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003732 }
3733 else if(reboot_mode == FASTBOOT_MODE)
3734 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03003735 boot_into_fastboot = true;
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003736 }
3737 else if(reboot_mode == ALARM_BOOT)
3738 {
Matthew Qind886f3c2014-01-17 16:52:01 +08003739 boot_reason_alarm = true;
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003740 }
Parth Dixit207573a2015-10-27 17:26:21 +05303741#if VERIFIED_BOOT
Parth Dixitddbc7352015-10-18 03:13:31 +05303742#if !VBOOT_MOTA
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003743 else if (reboot_mode == DM_VERITY_ENFORCING)
3744 {
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07003745 device.verity_mode = 1;
3746 write_device_info(&device);
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003747 }
3748 else if (reboot_mode == DM_VERITY_LOGGING)
3749 {
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07003750 device.verity_mode = 0;
3751 write_device_info(&device);
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003752 }
3753 else if (reboot_mode == DM_VERITY_KEYSCLEAR)
3754 {
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07003755 if(send_delete_keys_to_tz())
3756 ASSERT(0);
Ajay Dudani77421292010-10-27 19:34:06 -07003757 }
Parth Dixit207573a2015-10-27 17:26:21 +05303758#endif
Parth Dixitddbc7352015-10-18 03:13:31 +05303759#endif
Ajay Dudani77421292010-10-27 19:34:06 -07003760
Matthew Qindefd5562014-07-11 18:02:40 +08003761normal_boot:
Pavel Nedev5d91d412013-04-29 11:34:24 +03003762 if (!boot_into_fastboot)
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003763 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03003764 if (target_is_emmc_boot())
Shashank Mittala0032282011-08-26 14:50:11 -07003765 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03003766 if(emmc_recovery_init())
3767 dprintf(ALWAYS,"error in emmc_recovery_init\n");
3768 if(target_use_signed_kernel())
Shashank Mittala0032282011-08-26 14:50:11 -07003769 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03003770 if((device.is_unlocked) || (device.is_tampered))
3771 {
3772 #ifdef TZ_TAMPER_FUSE
3773 set_tamper_fuse_cmd();
3774 #endif
3775 #if USE_PCOM_SECBOOT
3776 set_tamper_flag(device.is_tampered);
3777 #endif
3778 }
Shashank Mittala0032282011-08-26 14:50:11 -07003779 }
Amit Blay6281ebc2015-01-11 14:44:08 +02003780
Pavel Nedev5d91d412013-04-29 11:34:24 +03003781 boot_linux_from_mmc();
Shashank Mittala0032282011-08-26 14:50:11 -07003782 }
Pavel Nedev5d91d412013-04-29 11:34:24 +03003783 else
3784 {
3785 recovery_init();
3786 #if USE_PCOM_SECBOOT
3787 if((device.is_unlocked) || (device.is_tampered))
3788 set_tamper_flag(device.is_tampered);
3789 #endif
3790 boot_linux_from_flash();
3791 }
3792 dprintf(CRITICAL, "ERROR: Could not do normal boot. Reverting "
3793 "to fastboot mode.\n");
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003794 }
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003795
Amol Jadi5edf3552013-07-23 14:15:34 -07003796 /* We are here means regular boot did not happen. Start fastboot. */
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07003797
Amol Jadi5edf3552013-07-23 14:15:34 -07003798 /* register aboot specific fastboot commands */
3799 aboot_fastboot_register_commands();
Amol Jadi57abe4c2011-05-24 15:47:27 -07003800
Amol Jadi5edf3552013-07-23 14:15:34 -07003801 /* dump partition table for debug info */
Kinson Chikf1a43512011-07-14 11:28:39 -07003802 partition_dump();
Amol Jadi5edf3552013-07-23 14:15:34 -07003803
3804 /* initialize and start fastboot */
3805 fastboot_init(target_get_scratch_address(), target_get_max_flash_size());
lijuang4ece1e72015-08-14 21:02:36 +08003806#if FBCON_DISPLAY_MSG
lijuang9a7d3b92015-11-30 14:41:24 +08003807 display_fastboot_menu();
lijuang4ece1e72015-08-14 21:02:36 +08003808#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08003809}
3810
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07003811uint32_t get_page_size()
3812{
3813 return page_size;
3814}
3815
Amir Samuelov57a6fa22013-06-05 16:36:43 +03003816/*
3817 * Calculated and save hash (SHA256) for non-signed boot image.
3818 *
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07003819 * @param image_addr - Boot image address
3820 * @param image_size - Size of the boot image
Amir Samuelov57a6fa22013-06-05 16:36:43 +03003821 *
3822 * @return int - 0 on success, negative value on failure.
3823 */
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07003824static int aboot_save_boot_hash_mmc(uint32_t image_addr, uint32_t image_size)
Amir Samuelov57a6fa22013-06-05 16:36:43 +03003825{
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07003826 unsigned int digest[8];
3827#if IMAGE_VERIF_ALGO_SHA1
3828 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA1;
3829#else
3830 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA256;
3831#endif
Amir Samuelov57a6fa22013-06-05 16:36:43 +03003832
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07003833 target_crypto_init_params();
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -08003834 hash_find((unsigned char *) image_addr, image_size, (unsigned char *)&digest, auth_algo);
Amir Samuelov57a6fa22013-06-05 16:36:43 +03003835
3836 save_kernel_hash_cmd(digest);
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07003837 dprintf(INFO, "aboot_save_boot_hash_mmc: imagesize_actual size %d bytes.\n", (int) image_size);
Amir Samuelov57a6fa22013-06-05 16:36:43 +03003838
3839 return 0;
3840}
3841
Brian Swetland9c4c0752009-01-25 16:23:50 -08003842APP_START(aboot)
3843 .init = aboot_init,
3844APP_END