blob: 1b94bd36a86cb0fe9746f35d18b8a3fb00a04a1e [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
Matthew Qin49e51fa2015-02-09 10:40:45 +08001307 /* Find index of device tree within device tree table */
1308 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
1309 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1310 return -1;
1311 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001312
Matthew Qin271927e2015-03-31 22:07:07 -04001313 if (is_gzip_package((unsigned char *)dt_table_offset + dt_entry.offset, dt_entry.size))
1314 {
1315 unsigned int compressed_size = 0;
1316 out_addr += out_len;
1317 out_avai_len -= out_len;
Matthew Qin0b15b322015-05-19 05:20:54 -04001318 dprintf(INFO, "decompressing dtb: start\n");
Matthew Qin271927e2015-03-31 22:07:07 -04001319 rc = decompress((unsigned char *)dt_table_offset + dt_entry.offset,
1320 dt_entry.size, out_addr, out_avai_len,
1321 &compressed_size, &dtb_size);
1322 if (rc)
1323 {
Matthew Qin0b15b322015-05-19 05:20:54 -04001324 dprintf(CRITICAL, "decompressing dtb failed!!!\n");
Matthew Qin271927e2015-03-31 22:07:07 -04001325 ASSERT(0);
1326 }
1327
Matthew Qin0b15b322015-05-19 05:20:54 -04001328 dprintf(INFO, "decompressing dtb: done\n");
Matthew Qin271927e2015-03-31 22:07:07 -04001329 best_match_dt_addr = out_addr;
1330 } else {
1331 best_match_dt_addr = (unsigned char *)dt_table_offset + dt_entry.offset;
1332 dtb_size = dt_entry.size;
1333 }
1334
Matthew Qin49e51fa2015-02-09 10:40:45 +08001335 /* Validate and Read device device tree in the tags_addr */
Matthew Qin271927e2015-03-31 22:07:07 -04001336 if (check_aboot_addr_range_overlap(hdr->tags_addr, dtb_size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001337 {
1338 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1339 return -1;
1340 }
Shashank Mittal162244e2011-08-08 19:01:25 -07001341
Matthew Qin271927e2015-03-31 22:07:07 -04001342 memmove((void *)hdr->tags_addr, (char *)best_match_dt_addr, dtb_size);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001343 } else {
1344 /* Validate the tags_addr */
1345 if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual))
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001346 {
1347 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1348 return -1;
1349 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08001350 /*
1351 * If appended dev tree is found, update the atags with
1352 * memory address to the DTB appended location on RAM.
1353 * Else update with the atags address in the kernel header
1354 */
1355 void *dtb;
Matthew Qinbb7923d2015-02-09 10:56:09 +08001356 dtb = dev_tree_appended((void*)(image_addr + page_size),
1357 hdr->kernel_size, dtb_offset,
Matthew Qin49e51fa2015-02-09 10:40:45 +08001358 (void *)hdr->tags_addr);
1359 if (!dtb) {
1360 dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001361 return -1;
1362 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07001363 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08001364 #endif
Shashank Mittal23b8f422010-04-16 19:27:21 -07001365
Stanimir Varbanov69ec5462013-07-18 18:17:42 +03001366 if (boot_into_recovery && !device.is_unlocked && !device.is_tampered)
1367 target_load_ssd_keystore();
1368
Shashank Mittal23b8f422010-04-16 19:27:21 -07001369unified_boot:
Shashank Mittal23b8f422010-04-16 19:27:21 -07001370
Dima Zavin77e41f32013-03-06 16:10:43 -08001371 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07001372 (const char *)hdr->cmdline, board_machtype(),
Shashank Mittal23b8f422010-04-16 19:27:21 -07001373 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
1374
1375 return 0;
1376}
1377
Dima Zavin214cc642009-01-26 11:16:21 -08001378int boot_linux_from_flash(void)
1379{
1380 struct boot_img_hdr *hdr = (void*) buf;
Dima Zavin214cc642009-01-26 11:16:21 -08001381 struct ptentry *ptn;
1382 struct ptable *ptable;
1383 unsigned offset = 0;
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001384
Shashank Mittalcd98d472011-08-02 14:29:24 -07001385 unsigned char *image_addr = 0;
1386 unsigned kernel_actual;
1387 unsigned ramdisk_actual;
1388 unsigned imagesize_actual;
vijay kumar8f53e362015-11-24 13:38:11 +05301389 unsigned second_actual = 0;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001390
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001391#if DEVICE_TREE
1392 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -07001393 struct dt_entry dt_entry;
vijay kumar8f53e362015-11-24 13:38:11 +05301394 unsigned dt_table_offset;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001395 uint32_t dt_actual;
Deepa Dinamani19648b42013-09-05 17:05:55 -07001396 uint32_t dt_hdr_size;
vijay kumar8f53e362015-11-24 13:38:11 +05301397 unsigned int dtb_size = 0;
1398 unsigned char *best_match_dt_addr = NULL;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001399#endif
1400
David Ng183a7422009-12-07 14:55:21 -08001401 if (target_is_emmc_boot()) {
1402 hdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
1403 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1404 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
1405 return -1;
1406 }
1407 goto continue_boot;
1408 }
1409
Dima Zavin214cc642009-01-26 11:16:21 -08001410 ptable = flash_get_ptable();
1411 if (ptable == NULL) {
1412 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1413 return -1;
1414 }
1415
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001416 if(!boot_into_recovery)
1417 {
1418 ptn = ptable_find(ptable, "boot");
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001419
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001420 if (ptn == NULL) {
1421 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1422 return -1;
1423 }
1424 }
1425 else
1426 {
1427 ptn = ptable_find(ptable, "recovery");
1428 if (ptn == NULL) {
1429 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
1430 return -1;
1431 }
Dima Zavin214cc642009-01-26 11:16:21 -08001432 }
1433
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001434 if (flash_read(ptn, offset, buf, page_size)) {
Dima Zavin214cc642009-01-26 11:16:21 -08001435 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
1436 return -1;
1437 }
Dima Zavin214cc642009-01-26 11:16:21 -08001438
1439 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001440 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Dima Zavin214cc642009-01-26 11:16:21 -08001441 return -1;
1442 }
1443
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001444 if (hdr->page_size != page_size) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001445 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 -08001446 return -1;
1447 }
1448
vijay kumar287bb542015-09-29 13:01:52 +05301449 /* ensure commandline is terminated */
1450 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
1451
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001452 /*
1453 * Update the kernel/ramdisk/tags address if the boot image header
1454 * has default values, these default values come from mkbootimg when
1455 * the boot image is flashed using fastboot flash:raw
1456 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001457 update_ker_tags_rdisk_addr(hdr, false);
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001458
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001459 /* Get virtual addresses since the hdr saves physical addresses. */
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001460 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
1461 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
1462 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
1463
1464 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1465 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1466
1467 /* Check if the addresses in the header are valid. */
1468 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
1469 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
1470 {
1471 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
1472 return -1;
1473 }
1474
1475#ifndef DEVICE_TREE
1476 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1477 {
1478 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
1479 return -1;
1480 }
1481#endif
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001482
Shashank Mittalcd98d472011-08-02 14:29:24 -07001483 /* Authenticate Kernel */
Deepa Dinamani23b60d42013-06-24 18:10:52 -07001484 if(target_use_signed_kernel() && (!device.is_unlocked))
Shashank Mittalcd98d472011-08-02 14:29:24 -07001485 {
1486 image_addr = (unsigned char *)target_get_scratch_address();
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001487 offset = 0;
1488
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001489#if DEVICE_TREE
1490 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
vijay kumar966a9822015-12-09 18:36:09 +05301491
1492 if (UINT_MAX < ((uint64_t)kernel_actual + (uint64_t)ramdisk_actual+ (uint64_t)dt_actual + page_size)) {
1493 dprintf(CRITICAL, "Integer overflow detected in bootimage header fields\n");
1494 return -1;
1495 }
1496
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001497 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + dt_actual);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001498
1499 if (check_aboot_addr_range_overlap(hdr->tags_addr, hdr->dt_size))
1500 {
1501 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1502 return -1;
1503 }
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001504#else
vijay kumar966a9822015-12-09 18:36:09 +05301505 if (UINT_MAX < ((uint64_t)kernel_actual + (uint64_t)ramdisk_actual+ page_size)) {
1506 dprintf(CRITICAL, "Integer overflow detected in bootimage header fields\n");
1507 return -1;
1508 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07001509 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001510#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001511
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001512 dprintf(INFO, "Loading (%s) image (%d): start\n",
1513 (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001514 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -08001515
Shashank Mittalcd98d472011-08-02 14:29:24 -07001516 /* Read image without signature */
1517 if (flash_read(ptn, offset, (void *)image_addr, imagesize_actual))
1518 {
1519 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
1520 return -1;
1521 }
Dima Zavin214cc642009-01-26 11:16:21 -08001522
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001523 dprintf(INFO, "Loading (%s) image (%d): done\n",
1524 (!boot_into_recovery ? "boot" : "recovery"), imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001525 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Amol Jadib6be5c12012-11-14 13:39:51 -08001526
Shashank Mittalcd98d472011-08-02 14:29:24 -07001527 offset = imagesize_actual;
1528 /* Read signature */
1529 if (flash_read(ptn, offset, (void *)(image_addr + offset), page_size))
1530 {
1531 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001532 return -1;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001533 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07001534
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301535 verify_signed_bootimg((uint32_t)image_addr, imagesize_actual);
Shashank Mittalcd98d472011-08-02 14:29:24 -07001536
1537 /* Move kernel and ramdisk to correct address */
Matthew Qinbb7923d2015-02-09 10:56:09 +08001538 memmove((void*) hdr->kernel_addr, (char*) (image_addr + page_size), hdr->kernel_size);
1539 memmove((void*) hdr->ramdisk_addr, (char*) (image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001540#if DEVICE_TREE
vijay kumar8f53e362015-11-24 13:38:11 +05301541 if(hdr->dt_size != 0) {
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001542
vijay kumar8f53e362015-11-24 13:38:11 +05301543 dt_table_offset = ((uint32_t)image_addr + page_size + kernel_actual + ramdisk_actual + second_actual);
1544
1545 table = (struct dt_table*) dt_table_offset;
1546
1547 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0){
1548 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1549 return -1;
1550 }
1551
1552 /* Find index of device tree within device tree table */
1553 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
1554 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1555 return -1;
1556 }
1557
1558 /* Validate and Read device device tree in the "tags_add */
1559 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size)){
1560 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1561 return -1;
1562 }
1563
1564 best_match_dt_addr = (unsigned char *)table + dt_entry.offset;
1565 dtb_size = dt_entry.size;
1566 memmove((void *)hdr->tags_addr, (char *)best_match_dt_addr, dtb_size);
1567 }
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001568#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001569
1570 /* Make sure everything from scratch address is read before next step!*/
Shashank Mittala0032282011-08-26 14:50:11 -07001571 if(device.is_tampered)
Shashank Mittal162244e2011-08-08 19:01:25 -07001572 {
1573 write_device_info_flash(&device);
1574 }
Channagoud Kadabi5c86fe32012-02-16 10:58:48 +05301575#if USE_PCOM_SECBOOT
1576 set_tamper_flag(device.is_tampered);
1577#endif
Shashank Mittalcd98d472011-08-02 14:29:24 -07001578 }
1579 else
1580 {
Shashank Mittal162244e2011-08-08 19:01:25 -07001581 offset = page_size;
1582
Amol Jadib6be5c12012-11-14 13:39:51 -08001583 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1584 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1585 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
1586
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001587 dprintf(INFO, "Loading (%s) image (%d): start\n",
1588 (!boot_into_recovery ? "boot" : "recovery"), kernel_actual + ramdisk_actual);
1589
Amol Jadi492d5a52013-03-15 16:12:34 -07001590 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -08001591
1592 if (flash_read(ptn, offset, (void *)hdr->kernel_addr, kernel_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -07001593 dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
1594 return -1;
1595 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001596 offset += kernel_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001597
Amol Jadib6be5c12012-11-14 13:39:51 -08001598 if (flash_read(ptn, offset, (void *)hdr->ramdisk_addr, ramdisk_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -07001599 dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
1600 return -1;
1601 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001602 offset += ramdisk_actual;
1603
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001604 dprintf(INFO, "Loading (%s) image (%d): done\n",
1605 (!boot_into_recovery ? "boot" : "recovery"), kernel_actual + ramdisk_actual);
1606
Amol Jadi492d5a52013-03-15 16:12:34 -07001607 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001608
1609 if(hdr->second_size != 0) {
Amol Jadib6be5c12012-11-14 13:39:51 -08001610 offset += second_actual;
1611 /* Second image loading not implemented. */
1612 ASSERT(0);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001613 }
1614
1615#if DEVICE_TREE
1616 if(hdr->dt_size != 0) {
1617
1618 /* Read the device tree table into buffer */
1619 if(flash_read(ptn, offset, (void *) dt_buf, page_size)) {
1620 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
1621 return -1;
1622 }
1623
1624 table = (struct dt_table*) dt_buf;
1625
Deepa Dinamani19648b42013-09-05 17:05:55 -07001626 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001627 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1628 return -1;
1629 }
1630
Deepa Dinamani19648b42013-09-05 17:05:55 -07001631 table = (struct dt_table*) memalign(CACHE_LINE, dt_hdr_size);
1632 if (!table)
1633 return -1;
1634
1635 /* Read the entire device tree table into buffer */
1636 if(flash_read(ptn, offset, (void *)table, dt_hdr_size)) {
1637 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
1638 return -1;
1639 }
1640
1641
Joel Kingaa335dc2013-06-03 16:11:08 -07001642 /* Find index of device tree within device tree table */
1643 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001644 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1645 return -1;
1646 }
1647
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001648 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001649 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001650 {
1651 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1652 return -1;
1653 }
1654
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001655 /* Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001656 if(flash_read(ptn, offset + dt_entry.offset,
1657 (void *)hdr->tags_addr, dt_entry.size)) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001658 dprintf(CRITICAL, "ERROR: Cannot read device tree\n");
1659 return -1;
1660 }
1661 }
1662#endif
1663
Shashank Mittalcd98d472011-08-02 14:29:24 -07001664 }
David Ng183a7422009-12-07 14:55:21 -08001665continue_boot:
Dima Zavin214cc642009-01-26 11:16:21 -08001666
Dima Zavin214cc642009-01-26 11:16:21 -08001667 /* TODO: create/pass atags to kernel */
1668
Ajay Dudanie28a6072011-07-01 13:59:46 -07001669 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07001670 (const char *)hdr->cmdline, board_machtype(),
Dima Zavin214cc642009-01-26 11:16:21 -08001671 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
1672
1673 return 0;
1674}
Brian Swetland9c4c0752009-01-25 16:23:50 -08001675
Shashank Mittal162244e2011-08-08 19:01:25 -07001676void write_device_info_mmc(device_info *dev)
1677{
Shashank Mittal162244e2011-08-08 19:01:25 -07001678 unsigned long long ptn = 0;
1679 unsigned long long size;
1680 int index = INVALID_PTN;
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001681 uint32_t blocksize;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001682 uint8_t lun = 0;
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001683 uint32_t ret = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001684
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001685 if (devinfo_present)
1686 index = partition_get_index("devinfo");
1687 else
1688 index = partition_get_index("aboot");
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001689
Shashank Mittal162244e2011-08-08 19:01:25 -07001690 ptn = partition_get_offset(index);
1691 if(ptn == 0)
1692 {
1693 return;
1694 }
1695
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001696 lun = partition_get_lun(index);
1697 mmc_set_lun(lun);
1698
Shashank Mittal162244e2011-08-08 19:01:25 -07001699 size = partition_get_size(index);
1700
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001701 blocksize = mmc_get_device_blocksize();
1702
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001703 if (devinfo_present)
1704 ret = mmc_write(ptn, blocksize, (void *)info_buf);
1705 else
1706 ret = mmc_write((ptn + size - blocksize), blocksize, (void *)info_buf);
1707 if (ret)
Shashank Mittal162244e2011-08-08 19:01:25 -07001708 {
1709 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
Sridhar Parasurame1a97922015-07-27 15:31:22 -07001710 ASSERT(0);
Shashank Mittal162244e2011-08-08 19:01:25 -07001711 }
1712}
1713
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001714void read_device_info_mmc(struct device_info *info)
Shashank Mittal162244e2011-08-08 19:01:25 -07001715{
Shashank Mittal162244e2011-08-08 19:01:25 -07001716 unsigned long long ptn = 0;
1717 unsigned long long size;
1718 int index = INVALID_PTN;
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001719 uint32_t blocksize;
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001720 uint32_t ret = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001721
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001722 if ((index = partition_get_index("devinfo")) < 0)
1723 {
1724 devinfo_present = false;
1725 index = partition_get_index("aboot");
1726 }
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001727
Shashank Mittal162244e2011-08-08 19:01:25 -07001728 ptn = partition_get_offset(index);
1729 if(ptn == 0)
1730 {
1731 return;
1732 }
1733
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001734 mmc_set_lun(partition_get_lun(index));
1735
Shashank Mittal162244e2011-08-08 19:01:25 -07001736 size = partition_get_size(index);
1737
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001738 blocksize = mmc_get_device_blocksize();
1739
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001740 if (devinfo_present)
1741 ret = mmc_read(ptn, (void *)info_buf, blocksize);
1742 else
1743 ret = mmc_read((ptn + size - blocksize), (void *)info_buf, blocksize);
1744 if (ret)
Shashank Mittal162244e2011-08-08 19:01:25 -07001745 {
1746 dprintf(CRITICAL, "ERROR: Cannot read device info\n");
Sridhar Parasurame1a97922015-07-27 15:31:22 -07001747 ASSERT(0);
Shashank Mittal162244e2011-08-08 19:01:25 -07001748 }
Shashank Mittal162244e2011-08-08 19:01:25 -07001749}
1750
1751void write_device_info_flash(device_info *dev)
1752{
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001753 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
Shashank Mittal162244e2011-08-08 19:01:25 -07001754 struct ptentry *ptn;
1755 struct ptable *ptable;
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001756 if(info == NULL)
1757 {
1758 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
1759 ASSERT(0);
1760 }
1761 info_buf = info;
Shashank Mittal162244e2011-08-08 19:01:25 -07001762 ptable = flash_get_ptable();
1763 if (ptable == NULL)
1764 {
1765 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1766 return;
1767 }
1768
1769 ptn = ptable_find(ptable, "devinfo");
1770 if (ptn == NULL)
1771 {
Smita Ghosh670c8b82015-05-07 09:30:03 -07001772 dprintf(CRITICAL, "ERROR: No devinfo partition found\n");
Shashank Mittal162244e2011-08-08 19:01:25 -07001773 return;
1774 }
1775
1776 memcpy(info, dev, sizeof(device_info));
1777
1778 if (flash_write(ptn, 0, (void *)info_buf, page_size))
1779 {
1780 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1781 return;
1782 }
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001783 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07001784}
1785
vijay kumarc65876c2015-04-24 13:29:16 +05301786static int read_allow_oem_unlock(device_info *dev)
1787{
vijay kumarc65876c2015-04-24 13:29:16 +05301788 unsigned offset;
1789 int index;
1790 unsigned long long ptn;
1791 unsigned long long ptn_size;
1792 unsigned blocksize = mmc_get_device_blocksize();
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07001793 STACKBUF_DMA_ALIGN(buf, blocksize);
vijay kumarc65876c2015-04-24 13:29:16 +05301794
vijay kumarca2e6812015-07-08 20:28:25 +05301795 index = partition_get_index(frp_ptns[0]);
vijay kumarc65876c2015-04-24 13:29:16 +05301796 if (index == INVALID_PTN)
1797 {
vijay kumarca2e6812015-07-08 20:28:25 +05301798 index = partition_get_index(frp_ptns[1]);
1799 if (index == INVALID_PTN)
1800 {
1801 dprintf(CRITICAL, "Neither '%s' nor '%s' partition found\n", frp_ptns[0],frp_ptns[1]);
1802 return -1;
1803 }
vijay kumarc65876c2015-04-24 13:29:16 +05301804 }
1805
1806 ptn = partition_get_offset(index);
1807 ptn_size = partition_get_size(index);
1808 offset = ptn_size - blocksize;
1809
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07001810 if (mmc_read(ptn + offset, (void *)buf, blocksize))
vijay kumarc65876c2015-04-24 13:29:16 +05301811 {
1812 dprintf(CRITICAL, "Reading MMC failed\n");
1813 return -1;
1814 }
1815
1816 /*is_allow_unlock is a bool value stored at the LSB of last byte*/
1817 is_allow_unlock = buf[blocksize-1] & 0x01;
1818 return 0;
1819}
1820
1821static int write_allow_oem_unlock(bool allow_unlock)
1822{
vijay kumarc65876c2015-04-24 13:29:16 +05301823 unsigned offset;
vijay kumarc65876c2015-04-24 13:29:16 +05301824 int index;
1825 unsigned long long ptn;
1826 unsigned long long ptn_size;
1827 unsigned blocksize = mmc_get_device_blocksize();
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07001828 STACKBUF_DMA_ALIGN(buf, blocksize);
vijay kumarc65876c2015-04-24 13:29:16 +05301829
vijay kumarca2e6812015-07-08 20:28:25 +05301830 index = partition_get_index(frp_ptns[0]);
vijay kumarc65876c2015-04-24 13:29:16 +05301831 if (index == INVALID_PTN)
1832 {
vijay kumarca2e6812015-07-08 20:28:25 +05301833 index = partition_get_index(frp_ptns[1]);
1834 if (index == INVALID_PTN)
1835 {
1836 dprintf(CRITICAL, "Neither '%s' nor '%s' partition found\n", frp_ptns[0],frp_ptns[1]);
1837 return -1;
1838 }
vijay kumarc65876c2015-04-24 13:29:16 +05301839 }
1840
1841 ptn = partition_get_offset(index);
1842 ptn_size = partition_get_size(index);
1843 offset = ptn_size - blocksize;
1844
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07001845 if (mmc_read(ptn + offset, (void *)buf, blocksize))
vijay kumarc65876c2015-04-24 13:29:16 +05301846 {
1847 dprintf(CRITICAL, "Reading MMC failed\n");
1848 return -1;
1849 }
1850
1851 /*is_allow_unlock is a bool value stored at the LSB of last byte*/
1852 buf[blocksize-1] = allow_unlock;
1853 if (mmc_write(ptn + offset, blocksize, buf))
1854 {
1855 dprintf(CRITICAL, "Writing MMC failed\n");
1856 return -1;
1857 }
1858
1859 return 0;
1860}
1861
Shashank Mittal162244e2011-08-08 19:01:25 -07001862void read_device_info_flash(device_info *dev)
1863{
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001864 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
Shashank Mittal162244e2011-08-08 19:01:25 -07001865 struct ptentry *ptn;
1866 struct ptable *ptable;
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001867 if(info == NULL)
1868 {
1869 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
1870 ASSERT(0);
1871 }
1872 info_buf = info;
Shashank Mittal162244e2011-08-08 19:01:25 -07001873 ptable = flash_get_ptable();
1874 if (ptable == NULL)
1875 {
1876 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1877 return;
1878 }
1879
1880 ptn = ptable_find(ptable, "devinfo");
1881 if (ptn == NULL)
1882 {
Smita Ghosh670c8b82015-05-07 09:30:03 -07001883 dprintf(CRITICAL, "ERROR: No devinfo partition found\n");
Shashank Mittal162244e2011-08-08 19:01:25 -07001884 return;
1885 }
1886
1887 if (flash_read(ptn, 0, (void *)info_buf, page_size))
1888 {
1889 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1890 return;
1891 }
1892
1893 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
1894 {
Shashank Mittal162244e2011-08-08 19:01:25 -07001895 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
1896 info->is_unlocked = 0;
Shashank Mittala0032282011-08-26 14:50:11 -07001897 info->is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001898 write_device_info_flash(info);
1899 }
1900 memcpy(dev, info, sizeof(device_info));
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001901 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07001902}
1903
1904void write_device_info(device_info *dev)
1905{
1906 if(target_is_emmc_boot())
1907 {
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001908 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
1909 if(info == NULL)
1910 {
1911 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
1912 ASSERT(0);
1913 }
1914 info_buf = info;
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001915 memcpy(info, dev, sizeof(struct device_info));
1916
1917#if USE_RPMB_FOR_DEVINFO
Sridhar Parasurame1a97922015-07-27 15:31:22 -07001918 if (is_secure_boot_enable()) {
1919 if((write_device_info_rpmb((void*) info, PAGE_SIZE)) < 0)
1920 ASSERT(0);
1921 }
Channagoud Kadabic80cb492015-04-28 16:08:28 -07001922 else
1923 write_device_info_mmc(info);
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001924#else
1925 write_device_info_mmc(info);
1926#endif
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001927 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07001928 }
1929 else
1930 {
1931 write_device_info_flash(dev);
1932 }
1933}
1934
1935void read_device_info(device_info *dev)
1936{
1937 if(target_is_emmc_boot())
1938 {
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001939 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
1940 if(info == NULL)
1941 {
1942 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
1943 ASSERT(0);
1944 }
1945 info_buf = info;
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001946
1947#if USE_RPMB_FOR_DEVINFO
Sridhar Parasurame1a97922015-07-27 15:31:22 -07001948 if (is_secure_boot_enable()) {
1949 if((read_device_info_rpmb((void*) info, PAGE_SIZE)) < 0)
1950 ASSERT(0);
1951 }
Channagoud Kadabic80cb492015-04-28 16:08:28 -07001952 else
1953 read_device_info_mmc(info);
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001954#else
1955 read_device_info_mmc(info);
1956#endif
1957
1958 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
1959 {
1960 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
lijuang511a2b52015-08-14 20:50:51 +08001961 if (is_secure_boot_enable()) {
Channagoud Kadabi05f78ba2015-07-06 11:58:14 -07001962 info->is_unlocked = 0;
Parth Dixitddbc7352015-10-18 03:13:31 +05301963#if !VBOOT_MOTA
lijuang511a2b52015-08-14 20:50:51 +08001964 info->is_unlock_critical = 0;
Parth Dixitddbc7352015-10-18 03:13:31 +05301965#endif
lijuang511a2b52015-08-14 20:50:51 +08001966 } else {
Channagoud Kadabi2fda4092015-07-07 13:34:11 -07001967 info->is_unlocked = 1;
Parth Dixitddbc7352015-10-18 03:13:31 +05301968#if !VBOOT_MOTA
lijuang511a2b52015-08-14 20:50:51 +08001969 info->is_unlock_critical = 1;
Parth Dixitddbc7352015-10-18 03:13:31 +05301970#endif
lijuang511a2b52015-08-14 20:50:51 +08001971 }
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001972 info->is_tampered = 0;
1973 info->charger_screen_enabled = 0;
Parth Dixitddbc7352015-10-18 03:13:31 +05301974#if !VBOOT_MOTA
Sridhar Parasuram287e5062015-08-24 16:17:22 -07001975 info->verity_mode = 1; //enforcing by default
Parth Dixitddbc7352015-10-18 03:13:31 +05301976#endif
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001977 write_device_info(info);
1978 }
1979 memcpy(dev, info, sizeof(device_info));
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001980 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07001981 }
1982 else
1983 {
1984 read_device_info_flash(dev);
1985 }
1986}
1987
1988void reset_device_info()
1989{
1990 dprintf(ALWAYS, "reset_device_info called.");
Shashank Mittala0032282011-08-26 14:50:11 -07001991 device.is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001992 write_device_info(&device);
1993}
1994
1995void set_device_root()
1996{
1997 dprintf(ALWAYS, "set_device_root called.");
Shashank Mittala0032282011-08-26 14:50:11 -07001998 device.is_tampered = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -07001999 write_device_info(&device);
2000}
2001
lijuang4ece1e72015-08-14 21:02:36 +08002002/* set device unlock value
2003 * Must check FRP before call this function
2004 * Need to wipe data when unlock status changed
2005 * type 0: oem unlock
2006 * type 1: unlock critical
2007 * status 0: unlock as false
2008 * status 1: lock as true
2009 */
2010void set_device_unlock_value(int type, bool status)
lijuang21f12f52015-08-22 16:22:19 +08002011{
lijuang4ece1e72015-08-14 21:02:36 +08002012 if (type == UNLOCK)
2013 device.is_unlocked = status;
Parth Dixitddbc7352015-10-18 03:13:31 +05302014#if !VBOOT_MOTA
lijuang4ece1e72015-08-14 21:02:36 +08002015 else if (type == UNLOCK_CRITICAL)
2016 device.is_unlock_critical = status;
Parth Dixitddbc7352015-10-18 03:13:31 +05302017#endif
lijuang4ece1e72015-08-14 21:02:36 +08002018 write_device_info(&device);
2019}
2020
2021static void set_device_unlock(int type, bool status)
2022{
2023 int is_unlocked = -1;
2024 char response[MAX_RSP_SIZE];
2025
2026 /* check device unlock status if it is as expected */
2027 if (type == UNLOCK)
2028 is_unlocked = device.is_unlocked;
Parth Dixitddbc7352015-10-18 03:13:31 +05302029#if !VBOOT_MOTA
lijuang4ece1e72015-08-14 21:02:36 +08002030 else if (type == UNLOCK_CRITICAL)
2031 is_unlocked = device.is_unlock_critical;
Parth Dixitddbc7352015-10-18 03:13:31 +05302032#endif
lijuang4ece1e72015-08-14 21:02:36 +08002033 if (is_unlocked == status) {
2034 snprintf(response, sizeof(response), "\tDevice already : %s", (status ? "unlocked!" : "locked!"));
2035 fastboot_info(response);
2036 fastboot_okay("");
2037 return;
2038 }
2039
2040 /* status is true, it means to unlock device */
2041 if (status) {
lijuang21f12f52015-08-22 16:22:19 +08002042 if(!is_allow_unlock) {
2043 fastboot_fail("oem unlock is not allowed");
2044 return;
2045 }
2046
lijuang4ece1e72015-08-14 21:02:36 +08002047#if FBCON_DISPLAY_MSG
lijuang9a7d3b92015-11-30 14:41:24 +08002048 display_unlock_menu(type);
lijuang4ece1e72015-08-14 21:02:36 +08002049 fastboot_okay("");
2050 return;
2051#else
2052 if (type == UNLOCK) {
2053 fastboot_fail("Need wipe userdata. Do 'fastboot oem unlock-go'");
2054 return;
2055 }
2056#endif
lijuang21f12f52015-08-22 16:22:19 +08002057 }
lijuang4ece1e72015-08-14 21:02:36 +08002058
2059 set_device_unlock_value(type, status);
2060
2061 /* wipe data */
2062 struct recovery_message msg;
2063
2064 snprintf(msg.recovery, sizeof(msg.recovery), "recovery\n--wipe_data");
2065 write_misc(0, &msg, sizeof(msg));
2066
2067 fastboot_okay("");
2068 reboot_device(RECOVERY_MODE);
lijuang21f12f52015-08-22 16:22:19 +08002069}
2070
lijuang511a2b52015-08-14 20:50:51 +08002071static bool critical_flash_allowed(const char * entry)
2072{
2073 uint32_t i = 0;
2074 if (entry == NULL)
2075 return false;
2076
2077 for (i = 0; i < ARRAY_SIZE(critical_flash_allowed_ptn); i++) {
2078 if(!strcmp(entry, critical_flash_allowed_ptn[i]))
2079 return true;
2080 }
2081 return false;
Matthew Qin271927e2015-03-31 22:07:07 -04002082}
2083
2084#if DEVICE_TREE
Amol Jadicb524072012-08-09 16:40:18 -07002085int copy_dtb(uint8_t *boot_image_start, unsigned int scratch_offset)
2086{
2087 uint32 dt_image_offset = 0;
Amol Jadicb524072012-08-09 16:40:18 -07002088 uint32_t n;
Amol Jadicb524072012-08-09 16:40:18 -07002089 struct dt_table *table;
2090 struct dt_entry dt_entry;
2091 uint32_t dt_hdr_size;
2092 unsigned int compressed_size = 0;
2093 unsigned int dtb_size = 0;
2094 unsigned int out_avai_len = 0;
2095 unsigned char *out_addr = NULL;
2096 unsigned char *best_match_dt_addr = NULL;
2097 int rc;
2098
2099 struct boot_img_hdr *hdr = (struct boot_img_hdr *) (boot_image_start);
2100
2101 if(hdr->dt_size != 0) {
2102 /* add kernel offset */
2103 dt_image_offset += page_size;
2104 n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07002105 dt_image_offset += n;
Amol Jadicb524072012-08-09 16:40:18 -07002106
Deepa Dinamani19648b42013-09-05 17:05:55 -07002107 /* add ramdisk offset */
Amol Jadicb524072012-08-09 16:40:18 -07002108 n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
2109 dt_image_offset += n;
2110
Joel Kingaa335dc2013-06-03 16:11:08 -07002111 /* add second offset */
2112 if(hdr->second_size != 0) {
Amol Jadicb524072012-08-09 16:40:18 -07002113 n = ROUND_TO_PAGE(hdr->second_size, page_mask);
2114 dt_image_offset += n;
2115 }
2116
Matthew Qin271927e2015-03-31 22:07:07 -04002117 /* offset now point to start of dt.img */
2118 table = (struct dt_table*)(boot_image_start + dt_image_offset);
2119
2120 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
2121 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
Matthew Qin0b15b322015-05-19 05:20:54 -04002122 return -1;
Matthew Qin271927e2015-03-31 22:07:07 -04002123 }
2124 /* Find index of device tree within device tree table */
2125 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
2126 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
2127 return -1;
Matthew Qin0b15b322015-05-19 05:20:54 -04002128 }
Matthew Qin271927e2015-03-31 22:07:07 -04002129
2130 best_match_dt_addr = (unsigned char *)boot_image_start + dt_image_offset + dt_entry.offset;
2131 if (is_gzip_package(best_match_dt_addr, dt_entry.size))
Matthew Qin0b15b322015-05-19 05:20:54 -04002132 {
Matthew Qin271927e2015-03-31 22:07:07 -04002133 out_addr = (unsigned char *)target_get_scratch_address() + scratch_offset;
2134 out_avai_len = target_get_max_flash_size() - scratch_offset;
2135 dprintf(INFO, "decompressing dtb: start\n");
2136 rc = decompress(best_match_dt_addr,
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002137 dt_entry.size, out_addr, out_avai_len,
Matthew Qin271927e2015-03-31 22:07:07 -04002138 &compressed_size, &dtb_size);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002139 if (rc)
2140 {
2141 dprintf(CRITICAL, "decompressing dtb failed!!!\n");
2142 ASSERT(0);
2143 }
Amol Jadicb524072012-08-09 16:40:18 -07002144
Matthew Qin271927e2015-03-31 22:07:07 -04002145 dprintf(INFO, "decompressing dtb: done\n");
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002146 best_match_dt_addr = out_addr;
2147 } else {
Amol Jadicb524072012-08-09 16:40:18 -07002148 dtb_size = dt_entry.size;
2149 }
2150 /* Validate and Read device device tree in the "tags_add */
2151 if (check_aboot_addr_range_overlap(hdr->tags_addr, dtb_size))
2152 {
2153 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
Brian Swetland9c4c0752009-01-25 16:23:50 -08002154 return -1;
2155 }
Amit Blay8a510302015-08-17 09:20:01 +03002156
2157 /* Read device device tree in the "tags_add */
2158 memmove((void*) hdr->tags_addr, (void *)best_match_dt_addr, dtb_size);
Brian Swetland9c4c0752009-01-25 16:23:50 -08002159 } else
2160 return -1;
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002161
2162 /* Everything looks fine. Return success. */
Channagoud Kadabi3d839012014-06-26 14:26:39 -07002163 return 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08002164}
2165#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08002166
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002167void cmd_boot(const char *arg, void *data, unsigned sz)
2168{
Matthew Qinbb7923d2015-02-09 10:56:09 +08002169#ifdef MDTP_SUPPORT
2170 static bool is_mdtp_activated = 0;
2171#endif /* MDTP_SUPPORT */
2172 unsigned kernel_actual;
2173 unsigned ramdisk_actual;
2174 uint32_t image_actual;
Matthew Qin271927e2015-03-31 22:07:07 -04002175 uint32_t dt_actual = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08002176 uint32_t sig_actual = SIGNATURE_SIZE;
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002177 struct boot_img_hdr *hdr = NULL;
Channagoud Kadabi6d5375e2015-06-23 17:15:42 -07002178 struct kernel64_hdr *kptr = NULL;
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002179 char *ptr = ((char*) data);
2180 int ret = 0;
2181 uint8_t dtb_copied = 0;
2182 unsigned int out_len = 0;
2183 unsigned int out_avai_len = 0;
2184 unsigned char *out_addr = NULL;
Brian Swetland9c4c0752009-01-25 16:23:50 -08002185 uint32_t dtb_offset = 0;
2186 unsigned char *kernel_start_addr = NULL;
2187 unsigned int kernel_size = 0;
2188 unsigned int scratch_offset = 0;
2189
lijuang2008ff22016-03-07 17:56:27 +08002190#if FBCON_DISPLAY_MSG
2191 /* Exit keys' detection thread firstly */
2192 exit_menu_keys_detection();
2193#endif
2194
Brian Swetland9c4c0752009-01-25 16:23:50 -08002195#if VERIFIED_BOOT
2196 if(target_build_variant_user() && !device.is_unlocked)
2197 {
2198 fastboot_fail("unlock device to use this command");
lijuang2008ff22016-03-07 17:56:27 +08002199 goto boot_failed;
Brian Swetland9c4c0752009-01-25 16:23:50 -08002200 }
2201#endif
2202
2203 if (sz < sizeof(hdr)) {
2204 fastboot_fail("invalid bootimage header");
lijuang2008ff22016-03-07 17:56:27 +08002205 goto boot_failed;
Brian Swetland9c4c0752009-01-25 16:23:50 -08002206 }
2207
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002208 hdr = (struct boot_img_hdr *)data;
Brian Swetland9c4c0752009-01-25 16:23:50 -08002209
2210 /* ensure commandline is terminated */
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002211 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -08002212
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002213 if(target_is_emmc_boot() && hdr->page_size) {
2214 page_size = hdr->page_size;
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07002215 page_mask = page_size - 1;
2216 }
2217
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002218 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
2219 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002220#if DEVICE_TREE
2221 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
2222#endif
2223
2224 image_actual = ADD_OF(page_size, kernel_actual);
2225 image_actual = ADD_OF(image_actual, ramdisk_actual);
2226 image_actual = ADD_OF(image_actual, dt_actual);
2227
Channagoud Kadabi3d839012014-06-26 14:26:39 -07002228 if (target_use_signed_kernel() && (!device.is_unlocked))
2229 image_actual = ADD_OF(image_actual, sig_actual);
2230
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002231 /* sz should have atleast raw boot image */
2232 if (image_actual > sz) {
Channagoud Kadabi3d839012014-06-26 14:26:39 -07002233 fastboot_fail("bootimage: incomplete or not signed");
lijuang2008ff22016-03-07 17:56:27 +08002234 goto boot_failed;
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002235 }
2236
Sridhar Parasuram361e4cd2015-09-24 11:19:19 -07002237 // Initialize boot state before trying to verify boot.img
2238#if VERIFIED_BOOT
Channagoud Kadabi699466e2015-11-03 12:37:42 -08002239 boot_verifier_init();
Channagoud Kadabibf6ce7d2015-09-17 13:25:35 -07002240 /* Handle overflow if the input image size is greater than
2241 * boot image buffer can hold
2242 */
2243 if ((target_get_max_flash_size() - (image_actual - sig_actual)) < page_size)
2244 {
2245 fastboot_fail("booimage: size is greater than boot image buffer can hold");
lijuang2008ff22016-03-07 17:56:27 +08002246 goto boot_failed;
Channagoud Kadabibf6ce7d2015-09-17 13:25:35 -07002247 }
Sridhar Parasuram361e4cd2015-09-24 11:19:19 -07002248#endif
2249
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002250 /* Verify the boot image
2251 * device & page_size are initialized in aboot_init
2252 */
Sridhar Parasuram7ae9fbc2015-09-17 09:56:30 -07002253 if (target_use_signed_kernel() && (!device.is_unlocked)) {
Channagoud Kadabi3d839012014-06-26 14:26:39 -07002254 /* Pass size excluding signature size, otherwise we would try to
2255 * access signature beyond its length
2256 */
2257 verify_signed_bootimg((uint32_t)data, (image_actual - sig_actual));
Sridhar Parasuram7ae9fbc2015-09-17 09:56:30 -07002258 }
Amit Blay4aa292f2015-04-28 21:55:59 +03002259#ifdef MDTP_SUPPORT
2260 else
2261 {
Shay Nachmani062aa3f2015-05-17 17:28:44 +03002262 /* fastboot boot is not allowed when MDTP is activated */
Amit Blay4aa292f2015-04-28 21:55:59 +03002263 mdtp_ext_partition_verification_t ext_partition;
Amit Blay8a510302015-08-17 09:20:01 +03002264
2265 if (!is_mdtp_activated) {
2266 ext_partition.partition = MDTP_PARTITION_NONE;
2267 mdtp_fwlock_verify_lock(&ext_partition);
2268 }
Amit Blay4aa292f2015-04-28 21:55:59 +03002269 }
Shay Nachmani062aa3f2015-05-17 17:28:44 +03002270
Shay Nachmani062aa3f2015-05-17 17:28:44 +03002271 mdtp_activated(&is_mdtp_activated);
2272 if(is_mdtp_activated){
2273 dprintf(CRITICAL, "fastboot boot command is not available.\n");
lijuang2008ff22016-03-07 17:56:27 +08002274 goto boot_failed;
Shay Nachmani062aa3f2015-05-17 17:28:44 +03002275 }
Amit Blay4aa292f2015-04-28 21:55:59 +03002276#endif /* MDTP_SUPPORT */
2277
Sridhar Parasuram361e4cd2015-09-24 11:19:19 -07002278#if VERIFIED_BOOT
Parth Dixitbc9b6492015-10-18 00:41:38 +05302279#if !VBOOT_MOTA
Sridhar Parasuram361e4cd2015-09-24 11:19:19 -07002280 // send root of trust
2281 if(!send_rot_command((uint32_t)device.is_unlocked))
2282 ASSERT(0);
2283#endif
Parth Dixitbc9b6492015-10-18 00:41:38 +05302284#endif
Channagoud Kadabia22144f2013-03-20 11:49:01 -07002285 /*
Matthew Qinbb7923d2015-02-09 10:56:09 +08002286 * Check if the kernel image is a gzip package. If yes, need to decompress it.
2287 * If not, continue booting.
2288 */
2289 if (is_gzip_package((unsigned char *)(data + page_size), hdr->kernel_size))
2290 {
2291 out_addr = (unsigned char *)target_get_scratch_address();
2292 out_addr = (unsigned char *)(out_addr + image_actual + page_size);
2293 out_avai_len = target_get_max_flash_size() - image_actual - page_size;
Matthew Qin0b15b322015-05-19 05:20:54 -04002294 dprintf(INFO, "decompressing kernel image: start\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08002295 ret = decompress((unsigned char *)(ptr + page_size),
2296 hdr->kernel_size, out_addr, out_avai_len,
2297 &dtb_offset, &out_len);
2298 if (ret)
2299 {
Matthew Qin0b15b322015-05-19 05:20:54 -04002300 dprintf(CRITICAL, "decompressing image failed!!!\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08002301 ASSERT(0);
2302 }
2303
Matthew Qin0b15b322015-05-19 05:20:54 -04002304 dprintf(INFO, "decompressing kernel image: done\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08002305 kptr = (struct kernel64_hdr *)out_addr;
2306 kernel_start_addr = out_addr;
2307 kernel_size = out_len;
2308 } else {
2309 kptr = (struct kernel64_hdr*)((char *)data + page_size);
2310 kernel_start_addr = (unsigned char *)((char *)data + page_size);
2311 kernel_size = hdr->kernel_size;
2312 }
2313
2314 /*
Channagoud Kadabia22144f2013-03-20 11:49:01 -07002315 * Update the kernel/ramdisk/tags address if the boot image header
2316 * has default values, these default values come from mkbootimg when
2317 * the boot image is flashed using fastboot flash:raw
2318 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08002319 update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr));
Dima Zavin3cadfff2013-03-21 14:30:48 -07002320
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002321 /* Get virtual addresses since the hdr saves physical addresses. */
2322 hdr->kernel_addr = VA(hdr->kernel_addr);
2323 hdr->ramdisk_addr = VA(hdr->ramdisk_addr);
2324 hdr->tags_addr = VA(hdr->tags_addr);
Brian Swetland9c4c0752009-01-25 16:23:50 -08002325
Matthew Qinbb7923d2015-02-09 10:56:09 +08002326 kernel_size = ROUND_TO_PAGE(kernel_size, page_mask);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002327 /* Check if the addresses in the header are valid. */
Matthew Qinbb7923d2015-02-09 10:56:09 +08002328 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_size) ||
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002329 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
2330 {
2331 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
lijuang2008ff22016-03-07 17:56:27 +08002332 goto boot_failed;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002333 }
2334
Amol Jadicb524072012-08-09 16:40:18 -07002335#if DEVICE_TREE
Matthew Qin271927e2015-03-31 22:07:07 -04002336 scratch_offset = image_actual + page_size + out_len;
Amol Jadicb524072012-08-09 16:40:18 -07002337 /* find correct dtb and copy it to right location */
Matthew Qin271927e2015-03-31 22:07:07 -04002338 ret = copy_dtb(data, scratch_offset);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002339
2340 dtb_copied = !ret ? 1 : 0;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002341#else
2342 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
2343 {
2344 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
lijuang2008ff22016-03-07 17:56:27 +08002345 goto boot_failed;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002346 }
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002347#endif
2348
2349 /* Load ramdisk & kernel */
2350 memmove((void*) hdr->ramdisk_addr, ptr + page_size + kernel_actual, hdr->ramdisk_size);
Matthew Qinbb7923d2015-02-09 10:56:09 +08002351 memmove((void*) hdr->kernel_addr, (char*) (kernel_start_addr), kernel_size);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002352
2353#if DEVICE_TREE
Matthew Qinbb7923d2015-02-09 10:56:09 +08002354 if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual))
2355 {
2356 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
lijuang2008ff22016-03-07 17:56:27 +08002357 goto boot_failed;
Matthew Qinbb7923d2015-02-09 10:56:09 +08002358 }
2359
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002360 /*
2361 * If dtb is not found look for appended DTB in the kernel.
2362 * If appended dev tree is found, update the atags with
2363 * memory address to the DTB appended location on RAM.
2364 * Else update with the atags address in the kernel header
2365 */
2366 if (!dtb_copied) {
2367 void *dtb;
Matthew Qinbb7923d2015-02-09 10:56:09 +08002368 dtb = dev_tree_appended((void*)(ptr + page_size),
2369 hdr->kernel_size, dtb_offset,
Dima Zavine63e5572013-05-03 12:23:06 -07002370 (void *)hdr->tags_addr);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002371 if (!dtb) {
2372 fastboot_fail("dtb not found");
lijuang2008ff22016-03-07 17:56:27 +08002373 goto boot_failed;
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002374 }
Amol Jadicb524072012-08-09 16:40:18 -07002375 }
2376#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08002377
2378 fastboot_okay("");
Amol Jadi7c4316c2013-10-07 14:19:26 -07002379 fastboot_stop();
Brian Swetland9c4c0752009-01-25 16:23:50 -08002380
Dima Zavin77e41f32013-03-06 16:10:43 -08002381 boot_linux((void*) hdr->kernel_addr, (void*) hdr->tags_addr,
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002382 (const char*) hdr->cmdline, board_machtype(),
2383 (void*) hdr->ramdisk_addr, hdr->ramdisk_size);
lijuang2008ff22016-03-07 17:56:27 +08002384
2385 /* fastboot already stop, it's no need to show fastboot menu */
2386 return;
2387boot_failed:
2388#if FBCON_DISPLAY_MSG
2389 /* revert to fastboot menu if boot failed */
2390 display_fastboot_menu();
2391#endif
2392 return;
Brian Swetland9c4c0752009-01-25 16:23:50 -08002393}
2394
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002395void cmd_erase_nand(const char *arg, void *data, unsigned sz)
Dima Zavin214cc642009-01-26 11:16:21 -08002396{
2397 struct ptentry *ptn;
2398 struct ptable *ptable;
2399
2400 ptable = flash_get_ptable();
2401 if (ptable == NULL) {
2402 fastboot_fail("partition table doesn't exist");
2403 return;
2404 }
2405
2406 ptn = ptable_find(ptable, arg);
2407 if (ptn == NULL) {
2408 fastboot_fail("unknown partition name");
2409 return;
2410 }
2411
2412 if (flash_erase(ptn)) {
2413 fastboot_fail("failed to erase partition");
2414 return;
2415 }
2416 fastboot_okay("");
2417}
2418
Bikas Gurungd48bd242010-09-04 19:54:32 -07002419
2420void cmd_erase_mmc(const char *arg, void *data, unsigned sz)
2421{
2422 unsigned long long ptn = 0;
Oliver Wangcee448d2013-10-22 18:40:13 +08002423 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07002424 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002425 uint8_t lun = 0;
Bikas Gurungd48bd242010-09-04 19:54:32 -07002426
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002427#if VERIFIED_BOOT
2428 if(!strcmp(arg, KEYSTORE_PTN_NAME))
2429 {
2430 if(!device.is_unlocked)
2431 {
2432 fastboot_fail("unlock device to erase keystore");
2433 return;
2434 }
2435 }
2436#endif
2437
Kinson Chikf1a43512011-07-14 11:28:39 -07002438 index = partition_get_index(arg);
2439 ptn = partition_get_offset(index);
Oliver Wangcee448d2013-10-22 18:40:13 +08002440 size = partition_get_size(index);
Neeti Desaica8c9602011-10-06 11:40:00 -07002441
Kinson Chikf1a43512011-07-14 11:28:39 -07002442 if(ptn == 0) {
Neeti Desaica8c9602011-10-06 11:40:00 -07002443 fastboot_fail("Partition table doesn't exist\n");
Bikas Gurungd48bd242010-09-04 19:54:32 -07002444 return;
2445 }
Kun Liang2f1601a2013-08-12 16:29:54 +08002446
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002447 lun = partition_get_lun(index);
2448 mmc_set_lun(lun);
2449
Sridhar Parasuramd7957122015-02-27 11:33:40 -08002450 if (platform_boot_dev_isemmc())
2451 {
2452 if (mmc_erase_card(ptn, size)) {
2453 fastboot_fail("failed to erase partition\n");
2454 return;
2455 }
2456 } else {
2457 BUF_DMA_ALIGN(out, DEFAULT_ERASE_SIZE);
2458 size = partition_get_size(index);
2459 if (size > DEFAULT_ERASE_SIZE)
2460 size = DEFAULT_ERASE_SIZE;
Kun Liang2f1601a2013-08-12 16:29:54 +08002461
Sridhar Parasuramd7957122015-02-27 11:33:40 -08002462 /* Simple inefficient version of erase. Just writing
2463 0 in first several blocks */
2464 if (mmc_write(ptn , size, (unsigned int *)out)) {
2465 fastboot_fail("failed to erase partition");
2466 return;
2467 }
Bikas Gurungd48bd242010-09-04 19:54:32 -07002468 }
Sridhar Parasuram32b30662015-07-10 13:33:22 -07002469#if VERIFIED_BOOT
Parth Dixitbc9b6492015-10-18 00:41:38 +05302470#if !VBOOT_MOTA
Sridhar Parasuram32b30662015-07-10 13:33:22 -07002471 if(!(strncmp(arg, "userdata", 8)))
2472 if(send_delete_keys_to_tz())
2473 ASSERT(0);
2474#endif
Parth Dixitbc9b6492015-10-18 00:41:38 +05302475#endif
Bikas Gurungd48bd242010-09-04 19:54:32 -07002476 fastboot_okay("");
2477}
2478
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002479void cmd_erase(const char *arg, void *data, unsigned sz)
2480{
Channagoud Kadabice3cf422015-04-17 16:02:30 -07002481#if VERIFIED_BOOT
Channagoud Kadabi35297672015-06-13 11:09:49 -07002482 if (target_build_variant_user())
Channagoud Kadabice3cf422015-04-17 16:02:30 -07002483 {
Sridhar Parasuramc32d07d2015-07-12 10:57:48 -07002484 if(!device.is_unlocked)
Channagoud Kadabice3cf422015-04-17 16:02:30 -07002485 {
Channagoud Kadabi35297672015-06-13 11:09:49 -07002486 fastboot_fail("device is locked. Cannot erase");
Channagoud Kadabice3cf422015-04-17 16:02:30 -07002487 return;
2488 }
2489 }
2490#endif
2491
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002492 if(target_is_emmc_boot())
2493 cmd_erase_mmc(arg, data, sz);
2494 else
2495 cmd_erase_nand(arg, data, sz);
2496}
Bikas Gurungd48bd242010-09-04 19:54:32 -07002497
Channagoud Kadabiad259832015-05-29 11:14:17 -07002498static uint32_t aboot_get_secret_key()
2499{
2500 /* 0 is invalid secret key, update this implementation to return
2501 * device specific unique secret key
2502 */
2503 return 0;
2504}
2505
Ajay Dudani5c761132011-04-07 20:19:04 -07002506void cmd_flash_mmc_img(const char *arg, void *data, unsigned sz)
Shashank Mittal23b8f422010-04-16 19:27:21 -07002507{
2508 unsigned long long ptn = 0;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07002509 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07002510 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002511 char *token = NULL;
2512 char *pname = NULL;
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07002513 char *sp;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002514 uint8_t lun = 0;
2515 bool lun_set = false;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07002516
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07002517 token = strtok_r((char *)arg, ":", &sp);
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002518 pname = token;
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07002519 token = strtok_r(NULL, ":", &sp);
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002520 if(token)
2521 {
2522 lun = atoi(token);
2523 mmc_set_lun(lun);
2524 lun_set = true;
2525 }
2526
Mao Jinlong226f33a2014-07-04 17:24:10 +08002527 if (pname)
Greg Grisco6e754772011-06-23 12:19:39 -07002528 {
Channagoud Kadabiad259832015-05-29 11:14:17 -07002529 if (!strncmp(pname, "frp-unlock", strlen("frp-unlock")))
2530 {
2531 if (!aboot_frp_unlock(pname, data, sz))
2532 {
2533 fastboot_info("FRP unlock successful");
2534 fastboot_okay("");
2535 }
2536 else
2537 fastboot_fail("Secret key is invalid, please update the bootloader with secret key");
2538
2539 return;
2540 }
2541
Mao Jinlong226f33a2014-07-04 17:24:10 +08002542 if (!strcmp(pname, "partition"))
2543 {
2544 dprintf(INFO, "Attempt to write partition image.\n");
2545 if (write_partition(sz, (unsigned char *) data)) {
2546 fastboot_fail("failed to write partition");
Greg Grisco6e754772011-06-23 12:19:39 -07002547 return;
2548 }
2549 }
Mao Jinlong226f33a2014-07-04 17:24:10 +08002550 else
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002551 {
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002552#if VERIFIED_BOOT
2553 if(!strcmp(pname, KEYSTORE_PTN_NAME))
2554 {
2555 if(!device.is_unlocked)
2556 {
2557 fastboot_fail("unlock device to flash keystore");
2558 return;
2559 }
Gaurav Nebhwanic7313cc2015-12-15 22:25:04 +05302560 if(!boot_verify_validate_keystore((unsigned char *)data,sz))
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002561 {
2562 fastboot_fail("image is not a keystore file");
2563 return;
2564 }
2565 }
2566#endif
Mao Jinlong226f33a2014-07-04 17:24:10 +08002567 index = partition_get_index(pname);
2568 ptn = partition_get_offset(index);
2569 if(ptn == 0) {
2570 fastboot_fail("partition table doesn't exist");
2571 return;
2572 }
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002573
Mao Jinlong226f33a2014-07-04 17:24:10 +08002574 if (!strcmp(pname, "boot") || !strcmp(pname, "recovery")) {
2575 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
2576 fastboot_fail("image is not a boot image");
2577 return;
2578 }
2579 }
2580
2581 if(!lun_set)
2582 {
2583 lun = partition_get_lun(index);
2584 mmc_set_lun(lun);
2585 }
2586
2587 size = partition_get_size(index);
2588 if (ROUND_TO_PAGE(sz,511) > size) {
2589 fastboot_fail("size too large");
2590 return;
2591 }
2592 else if (mmc_write(ptn , sz, (unsigned int *)data)) {
2593 fastboot_fail("flash write failure");
2594 return;
2595 }
Greg Grisco6e754772011-06-23 12:19:39 -07002596 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07002597 }
2598 fastboot_okay("");
2599 return;
2600}
2601
Ajay Dudanide984792015-03-02 09:57:41 -08002602void cmd_flash_meta_img(const char *arg, void *data, unsigned sz)
2603{
2604 int i, images;
2605 meta_header_t *meta_header;
2606 img_header_entry_t *img_header_entry;
2607
2608 meta_header = (meta_header_t*) data;
2609 img_header_entry = (img_header_entry_t*) (data+sizeof(meta_header_t));
2610
2611 images = meta_header->img_hdr_sz / sizeof(img_header_entry_t);
2612
2613 for (i=0; i<images; i++) {
2614
2615 if((img_header_entry[i].ptn_name == NULL) ||
2616 (img_header_entry[i].start_offset == 0) ||
2617 (img_header_entry[i].size == 0))
2618 break;
2619
2620 cmd_flash_mmc_img(img_header_entry[i].ptn_name,
2621 (void *) data + img_header_entry[i].start_offset,
2622 img_header_entry[i].size);
2623 }
2624
Channagoud Kadabiaafbbfc2015-03-04 17:52:56 -08002625 if (!strncmp(arg, "bootloader", strlen("bootloader")))
2626 {
2627 strlcpy(device.bootloader_version, TARGET(BOARD), MAX_VERSION_LEN);
2628 strlcat(device.bootloader_version, "-", MAX_VERSION_LEN);
2629 strlcat(device.bootloader_version, meta_header->img_version, MAX_VERSION_LEN);
2630 }
2631 else
2632 {
2633 strlcpy(device.radio_version, TARGET(BOARD), MAX_VERSION_LEN);
2634 strlcat(device.radio_version, "-", MAX_VERSION_LEN);
2635 strlcat(device.radio_version, meta_header->img_version, MAX_VERSION_LEN);
2636 }
2637
2638 write_device_info(&device);
Ajay Dudanide984792015-03-02 09:57:41 -08002639 fastboot_okay("");
2640 return;
2641}
2642
Ajay Dudani5c761132011-04-07 20:19:04 -07002643void cmd_flash_mmc_sparse_img(const char *arg, void *data, unsigned sz)
2644{
2645 unsigned int chunk;
wufeng.jiang813dc352015-06-02 23:02:46 -04002646 uint64_t chunk_data_sz;
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002647 uint32_t *fill_buf = NULL;
2648 uint32_t fill_val;
Ajay Dudani5c761132011-04-07 20:19:04 -07002649 sparse_header_t *sparse_header;
2650 chunk_header_t *chunk_header;
Ajay Dudaniab18f022011-05-12 14:39:22 -07002651 uint32_t total_blocks = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07002652 unsigned long long ptn = 0;
Channagoud Kadabi65b91002011-10-11 17:34:33 +05302653 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07002654 int index = INVALID_PTN;
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05302655 uint32_t i;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002656 uint8_t lun = 0;
vijay kumar800255e2015-04-24 20:26:19 +05302657 /*End of the sparse image address*/
2658 uint32_t data_end = (uint32_t)data + sz;
Ajay Dudani5c761132011-04-07 20:19:04 -07002659
Kinson Chikf1a43512011-07-14 11:28:39 -07002660 index = partition_get_index(arg);
2661 ptn = partition_get_offset(index);
2662 if(ptn == 0) {
Ajay Dudani5c761132011-04-07 20:19:04 -07002663 fastboot_fail("partition table doesn't exist");
2664 return;
2665 }
2666
Channagoud Kadabi65b91002011-10-11 17:34:33 +05302667 size = partition_get_size(index);
Channagoud Kadabi65b91002011-10-11 17:34:33 +05302668
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002669 lun = partition_get_lun(index);
2670 mmc_set_lun(lun);
2671
vijay kumar800255e2015-04-24 20:26:19 +05302672 if (sz < sizeof(sparse_header_t)) {
2673 fastboot_fail("size too low");
2674 return;
2675 }
2676
Ajay Dudani5c761132011-04-07 20:19:04 -07002677 /* Read and skip over sparse image header */
2678 sparse_header = (sparse_header_t *) data;
vijay kumar800255e2015-04-24 20:26:19 +05302679
vijay kumar1321f342015-03-27 12:13:42 +05302680 if (((uint64_t)sparse_header->total_blks * (uint64_t)sparse_header->blk_sz) > size) {
Ajay Dudani876b3282012-12-21 14:12:17 -08002681 fastboot_fail("size too large");
2682 return;
2683 }
2684
vijay kumarde4fcf62015-04-23 13:05:49 +05302685 data += sizeof(sparse_header_t);
vijay kumar800255e2015-04-24 20:26:19 +05302686
2687 if (data_end < (uint32_t)data) {
2688 fastboot_fail("buffer overreads occured due to invalid sparse header");
2689 return;
2690 }
2691
vijay kumarde4fcf62015-04-23 13:05:49 +05302692 if(sparse_header->file_hdr_sz != sizeof(sparse_header_t))
Ajay Dudani5c761132011-04-07 20:19:04 -07002693 {
vijay kumarde4fcf62015-04-23 13:05:49 +05302694 fastboot_fail("sparse header size mismatch");
2695 return;
Ajay Dudani5c761132011-04-07 20:19:04 -07002696 }
2697
Ajay Dudanib06c05f2011-05-12 14:46:10 -07002698 dprintf (SPEW, "=== Sparse Image Header ===\n");
2699 dprintf (SPEW, "magic: 0x%x\n", sparse_header->magic);
2700 dprintf (SPEW, "major_version: 0x%x\n", sparse_header->major_version);
2701 dprintf (SPEW, "minor_version: 0x%x\n", sparse_header->minor_version);
2702 dprintf (SPEW, "file_hdr_sz: %d\n", sparse_header->file_hdr_sz);
2703 dprintf (SPEW, "chunk_hdr_sz: %d\n", sparse_header->chunk_hdr_sz);
2704 dprintf (SPEW, "blk_sz: %d\n", sparse_header->blk_sz);
2705 dprintf (SPEW, "total_blks: %d\n", sparse_header->total_blks);
2706 dprintf (SPEW, "total_chunks: %d\n", sparse_header->total_chunks);
Ajay Dudani5c761132011-04-07 20:19:04 -07002707
2708 /* Start processing chunks */
2709 for (chunk=0; chunk<sparse_header->total_chunks; chunk++)
2710 {
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302711 /* Make sure the total image size does not exceed the partition size */
2712 if(((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz) >= size) {
2713 fastboot_fail("size too large");
2714 return;
2715 }
Ajay Dudani5c761132011-04-07 20:19:04 -07002716 /* Read and skip over chunk header */
2717 chunk_header = (chunk_header_t *) data;
2718 data += sizeof(chunk_header_t);
2719
vijay kumar800255e2015-04-24 20:26:19 +05302720 if (data_end < (uint32_t)data) {
2721 fastboot_fail("buffer overreads occured due to invalid sparse header");
2722 return;
2723 }
2724
Ajay Dudani5c761132011-04-07 20:19:04 -07002725 dprintf (SPEW, "=== Chunk Header ===\n");
2726 dprintf (SPEW, "chunk_type: 0x%x\n", chunk_header->chunk_type);
2727 dprintf (SPEW, "chunk_data_sz: 0x%x\n", chunk_header->chunk_sz);
2728 dprintf (SPEW, "total_size: 0x%x\n", chunk_header->total_sz);
2729
vijay kumar800255e2015-04-24 20:26:19 +05302730 if(sparse_header->chunk_hdr_sz != sizeof(chunk_header_t))
Ajay Dudani5c761132011-04-07 20:19:04 -07002731 {
vijay kumar800255e2015-04-24 20:26:19 +05302732 fastboot_fail("chunk header size mismatch");
2733 return;
Ajay Dudani5c761132011-04-07 20:19:04 -07002734 }
2735
wufeng.jiang813dc352015-06-02 23:02:46 -04002736 if (!sparse_header->blk_sz ){
2737 fastboot_fail("Invalid block size\n");
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302738 return;
2739 }
2740
wufeng.jiang813dc352015-06-02 23:02:46 -04002741 chunk_data_sz = (uint64_t)sparse_header->blk_sz * chunk_header->chunk_sz;
2742
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302743 /* Make sure that the chunk size calculated from sparse image does not
2744 * exceed partition size
2745 */
2746 if ((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz + chunk_data_sz > size)
2747 {
2748 fastboot_fail("Chunk data size exceeds partition size");
2749 return;
2750 }
2751
Ajay Dudani5c761132011-04-07 20:19:04 -07002752 switch (chunk_header->chunk_type)
2753 {
2754 case CHUNK_TYPE_RAW:
wufeng.jiang813dc352015-06-02 23:02:46 -04002755 if((uint64_t)chunk_header->total_sz != ((uint64_t)sparse_header->chunk_hdr_sz +
Ajay Dudani5c761132011-04-07 20:19:04 -07002756 chunk_data_sz))
2757 {
2758 fastboot_fail("Bogus chunk size for chunk type Raw");
2759 return;
2760 }
2761
vijay kumar800255e2015-04-24 20:26:19 +05302762 if (data_end < (uint32_t)data + chunk_data_sz) {
2763 fastboot_fail("buffer overreads occured due to invalid sparse header");
2764 return;
2765 }
2766
wufeng.jiang813dc352015-06-02 23:02:46 -04002767 /* chunk_header->total_sz is uint32,So chunk_data_sz is now less than 2^32
2768 otherwise it will return in the line above
2769 */
Ajay Dudaniab18f022011-05-12 14:39:22 -07002770 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
wufeng.jiang813dc352015-06-02 23:02:46 -04002771 (uint32_t)chunk_data_sz,
Ajay Dudaniab18f022011-05-12 14:39:22 -07002772 (unsigned int*)data))
Ajay Dudani5c761132011-04-07 20:19:04 -07002773 {
2774 fastboot_fail("flash write failure");
2775 return;
2776 }
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302777 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
2778 fastboot_fail("Bogus size for RAW chunk type");
2779 return;
2780 }
Ajay Dudani5c761132011-04-07 20:19:04 -07002781 total_blocks += chunk_header->chunk_sz;
wufeng.jiang813dc352015-06-02 23:02:46 -04002782 data += (uint32_t)chunk_data_sz;
Ajay Dudani5c761132011-04-07 20:19:04 -07002783 break;
2784
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002785 case CHUNK_TYPE_FILL:
2786 if(chunk_header->total_sz != (sparse_header->chunk_hdr_sz +
2787 sizeof(uint32_t)))
2788 {
2789 fastboot_fail("Bogus chunk size for chunk type FILL");
2790 return;
2791 }
2792
2793 fill_buf = (uint32_t *)memalign(CACHE_LINE, ROUNDUP(sparse_header->blk_sz, CACHE_LINE));
2794 if (!fill_buf)
2795 {
2796 fastboot_fail("Malloc failed for: CHUNK_TYPE_FILL");
2797 return;
2798 }
2799
vijay kumar800255e2015-04-24 20:26:19 +05302800 if (data_end < (uint32_t)data + sizeof(uint32_t)) {
2801 fastboot_fail("buffer overreads occured due to invalid sparse header");
2802 return;
2803 }
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002804 fill_val = *(uint32_t *)data;
2805 data = (char *) data + sizeof(uint32_t);
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002806
2807 for (i = 0; i < (sparse_header->blk_sz / sizeof(fill_val)); i++)
2808 {
2809 fill_buf[i] = fill_val;
2810 }
2811
wufeng.jiang813dc352015-06-02 23:02:46 -04002812 for (i = 0; i < chunk_header->chunk_sz; i++)
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002813 {
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302814 /* Make sure that the data written to partition does not exceed partition size */
2815 if ((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz + sparse_header->blk_sz > size)
2816 {
2817 fastboot_fail("Chunk data size for fill type exceeds partition size");
2818 return;
2819 }
2820
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002821 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
2822 sparse_header->blk_sz,
2823 fill_buf))
2824 {
2825 fastboot_fail("flash write failure");
2826 free(fill_buf);
2827 return;
2828 }
2829
2830 total_blocks++;
2831 }
2832
2833 free(fill_buf);
2834 break;
2835
Ajay Dudani5c761132011-04-07 20:19:04 -07002836 case CHUNK_TYPE_DONT_CARE:
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302837 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
2838 fastboot_fail("bogus size for chunk DONT CARE type");
2839 return;
2840 }
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07002841 total_blocks += chunk_header->chunk_sz;
2842 break;
2843
Ajay Dudani5c761132011-04-07 20:19:04 -07002844 case CHUNK_TYPE_CRC:
2845 if(chunk_header->total_sz != sparse_header->chunk_hdr_sz)
2846 {
wufeng.jiang813dc352015-06-02 23:02:46 -04002847 fastboot_fail("Bogus chunk size for chunk type CRC");
Ajay Dudani5c761132011-04-07 20:19:04 -07002848 return;
2849 }
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302850 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
2851 fastboot_fail("bogus size for chunk CRC type");
2852 return;
2853 }
Ajay Dudani5c761132011-04-07 20:19:04 -07002854 total_blocks += chunk_header->chunk_sz;
vijay kumar800255e2015-04-24 20:26:19 +05302855 if ((uint32_t)data > UINT_MAX - chunk_data_sz) {
2856 fastboot_fail("integer overflow occured");
2857 return;
2858 }
wufeng.jiang813dc352015-06-02 23:02:46 -04002859 data += (uint32_t)chunk_data_sz;
vijay kumar800255e2015-04-24 20:26:19 +05302860 if (data_end < (uint32_t)data) {
2861 fastboot_fail("buffer overreads occured due to invalid sparse header");
2862 return;
2863 }
Ajay Dudani5c761132011-04-07 20:19:04 -07002864 break;
2865
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07002866 default:
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002867 dprintf(CRITICAL, "Unkown chunk type: %x\n",chunk_header->chunk_type);
Ajay Dudani5c761132011-04-07 20:19:04 -07002868 fastboot_fail("Unknown chunk type");
2869 return;
2870 }
2871 }
2872
Ajay Dudani0c6927b2011-05-18 11:12:16 -07002873 dprintf(INFO, "Wrote %d blocks, expected to write %d blocks\n",
2874 total_blocks, sparse_header->total_blks);
2875
2876 if(total_blocks != sparse_header->total_blks)
2877 {
2878 fastboot_fail("sparse image write failure");
2879 }
Ajay Dudani5c761132011-04-07 20:19:04 -07002880
2881 fastboot_okay("");
2882 return;
2883}
2884
2885void cmd_flash_mmc(const char *arg, void *data, unsigned sz)
2886{
2887 sparse_header_t *sparse_header;
Ajay Dudanide984792015-03-02 09:57:41 -08002888 meta_header_t *meta_header;
Ajay Dudani5c761132011-04-07 20:19:04 -07002889
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07002890#ifdef SSD_ENABLE
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -08002891 /* 8 Byte Magic + 2048 Byte xml + Encrypted Data */
2892 unsigned int *magic_number = (unsigned int *) data;
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002893 int ret=0;
2894 uint32 major_version=0;
2895 uint32 minor_version=0;
2896
2897 ret = scm_svc_version(&major_version,&minor_version);
2898 if(!ret)
2899 {
2900 if(major_version >= 2)
2901 {
Amir Samuelovbb65ce02013-05-05 12:20:18 +03002902 if( !strcmp(arg, "ssd") || !strcmp(arg, "tqs") )
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002903 {
2904 ret = encrypt_scm((uint32 **) &data, &sz);
2905 if (ret != 0) {
2906 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
2907 return;
2908 }
2909
Amir Samuelovbb65ce02013-05-05 12:20:18 +03002910 /* Protect only for SSD */
2911 if (!strcmp(arg, "ssd")) {
2912 ret = scm_protect_keystore((uint32 *) data, sz);
2913 if (ret != 0) {
2914 dprintf(CRITICAL, "ERROR: scm_protect_keystore Failed\n");
2915 return;
2916 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002917 }
2918 }
2919 else
2920 {
2921 ret = decrypt_scm_v2((uint32 **) &data, &sz);
2922 if(ret != 0)
2923 {
2924 dprintf(CRITICAL,"ERROR: Decryption Failure\n");
2925 return;
2926 }
2927 }
2928 }
2929 else
2930 {
2931 if (magic_number[0] == DECRYPT_MAGIC_0 &&
2932 magic_number[1] == DECRYPT_MAGIC_1)
2933 {
2934 ret = decrypt_scm((uint32 **) &data, &sz);
2935 if (ret != 0) {
2936 dprintf(CRITICAL, "ERROR: Invalid secure image\n");
2937 return;
2938 }
2939 }
2940 else if (magic_number[0] == ENCRYPT_MAGIC_0 &&
2941 magic_number[1] == ENCRYPT_MAGIC_1)
2942 {
2943 ret = encrypt_scm((uint32 **) &data, &sz);
2944 if (ret != 0) {
2945 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
2946 return;
2947 }
2948 }
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07002949 }
2950 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002951 else
Neeti Desai127b9e02012-03-20 16:11:23 -07002952 {
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002953 dprintf(CRITICAL,"INVALID SVC Version\n");
2954 return;
Neeti Desai127b9e02012-03-20 16:11:23 -07002955 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002956#endif /* SSD_ENABLE */
Neeti Desai127b9e02012-03-20 16:11:23 -07002957
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002958#if VERIFIED_BOOT
Channagoud Kadabi35297672015-06-13 11:09:49 -07002959 if (target_build_variant_user())
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002960 {
lijuang511a2b52015-08-14 20:50:51 +08002961 /* if device is locked:
2962 * common partition will not allow to be flashed
2963 * critical partition will allow to flash image.
2964 */
2965 if(!device.is_unlocked && !critical_flash_allowed(arg)) {
2966 fastboot_fail("Partition flashing is not allowed");
2967 return;
2968 }
Parth Dixitddbc7352015-10-18 03:13:31 +05302969#if !VBOOT_MOTA
lijuang511a2b52015-08-14 20:50:51 +08002970 /* if device critical is locked:
2971 * common partition will allow to be flashed
2972 * critical partition will not allow to flash image.
2973 */
2974 if(!device.is_unlock_critical && critical_flash_allowed(arg)) {
2975 fastboot_fail("Critical partition flashing is not allowed");
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002976 return;
2977 }
Parth Dixitddbc7352015-10-18 03:13:31 +05302978#endif
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002979 }
2980#endif
2981
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07002982 sparse_header = (sparse_header_t *) data;
Ajay Dudanide984792015-03-02 09:57:41 -08002983 meta_header = (meta_header_t *) data;
2984 if (sparse_header->magic == SPARSE_HEADER_MAGIC)
Ajay Dudani5c761132011-04-07 20:19:04 -07002985 cmd_flash_mmc_sparse_img(arg, data, sz);
Ajay Dudanide984792015-03-02 09:57:41 -08002986 else if (meta_header->magic == META_HEADER_MAGIC)
2987 cmd_flash_meta_img(arg, data, sz);
2988 else
2989 cmd_flash_mmc_img(arg, data, sz);
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07002990
2991#if VERIFIED_BOOT
Parth Dixitddbc7352015-10-18 03:13:31 +05302992#if !VBOOT_MOTA
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07002993 if((!strncmp(arg, "system", 6)) && !device.verity_mode)
2994 {
2995 // reset dm_verity mode to enforcing
2996 device.verity_mode = 1;
2997 write_device_info(&device);
2998 }
2999#endif
Parth Dixitddbc7352015-10-18 03:13:31 +05303000#endif
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07003001
Ajay Dudani5c761132011-04-07 20:19:04 -07003002 return;
3003}
3004
Tanya Brokhman84eaaf62015-02-24 21:57:06 +02003005void cmd_updatevol(const char *vol_name, void *data, unsigned sz)
3006{
3007 struct ptentry *sys_ptn;
3008 struct ptable *ptable;
3009
3010 ptable = flash_get_ptable();
3011 if (ptable == NULL) {
3012 fastboot_fail("partition table doesn't exist");
3013 return;
3014 }
3015
3016 sys_ptn = ptable_find(ptable, "system");
3017 if (sys_ptn == NULL) {
3018 fastboot_fail("system partition not found");
3019 return;
3020 }
3021
3022 sz = ROUND_TO_PAGE(sz, page_mask);
3023 if (update_ubi_vol(sys_ptn, vol_name, data, sz))
3024 fastboot_fail("update_ubi_vol failed");
3025 else
3026 fastboot_okay("");
3027}
3028
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003029void cmd_flash_nand(const char *arg, void *data, unsigned sz)
Dima Zavin214cc642009-01-26 11:16:21 -08003030{
3031 struct ptentry *ptn;
3032 struct ptable *ptable;
3033 unsigned extra = 0;
3034
3035 ptable = flash_get_ptable();
3036 if (ptable == NULL) {
3037 fastboot_fail("partition table doesn't exist");
3038 return;
3039 }
3040
3041 ptn = ptable_find(ptable, arg);
3042 if (ptn == NULL) {
Tanya Brokhman84eaaf62015-02-24 21:57:06 +02003043 dprintf(INFO, "unknown partition name (%s). Trying updatevol\n",
3044 arg);
3045 cmd_updatevol(arg, data, sz);
Dima Zavin214cc642009-01-26 11:16:21 -08003046 return;
3047 }
3048
3049 if (!strcmp(ptn->name, "boot") || !strcmp(ptn->name, "recovery")) {
3050 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
3051 fastboot_fail("image is not a boot image");
3052 return;
3053 }
3054 }
3055
Amol Jadi5c61a952012-05-04 17:05:35 -07003056 if (!strcmp(ptn->name, "system")
Deepa Dinamani13e32c42012-03-12 14:34:17 -07003057 || !strcmp(ptn->name, "userdata")
3058 || !strcmp(ptn->name, "persist")
Sundarajan Srinivasanb063a852013-11-19 14:02:27 -08003059 || !strcmp(ptn->name, "recoveryfs")
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -08003060 || !strcmp(ptn->name, "modem"))
Tanya Brokhman1c94f1a2015-02-15 09:05:03 +02003061 extra = 1;
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -08003062 else
Shashank Mittaldcc2e352009-11-19 19:11:16 -08003063 sz = ROUND_TO_PAGE(sz, page_mask);
Dima Zavin214cc642009-01-26 11:16:21 -08003064
3065 dprintf(INFO, "writing %d bytes to '%s'\n", sz, ptn->name);
Tanya Brokhman1c94f1a2015-02-15 09:05:03 +02003066 if (!memcmp((void *)data, UBI_MAGIC, UBI_MAGIC_SIZE)) {
3067 if (flash_ubi_img(ptn, data, sz)) {
3068 fastboot_fail("flash write failure");
3069 return;
3070 }
3071 } else {
3072 if (flash_write(ptn, extra, data, sz)) {
3073 fastboot_fail("flash write failure");
3074 return;
3075 }
Dima Zavin214cc642009-01-26 11:16:21 -08003076 }
3077 dprintf(INFO, "partition '%s' updated\n", ptn->name);
3078 fastboot_okay("");
3079}
3080
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003081void cmd_flash(const char *arg, void *data, unsigned sz)
3082{
3083 if(target_is_emmc_boot())
3084 cmd_flash_mmc(arg, data, sz);
3085 else
3086 cmd_flash_nand(arg, data, sz);
3087}
3088
Dima Zavin214cc642009-01-26 11:16:21 -08003089void cmd_continue(const char *arg, void *data, unsigned sz)
3090{
3091 fastboot_okay("");
Amol Jadi7c4316c2013-10-07 14:19:26 -07003092 fastboot_stop();
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003093
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003094 if (target_is_emmc_boot())
3095 {
lijuanga40d6302015-07-20 20:10:13 +08003096#if FBCON_DISPLAY_MSG
lijuangde34d502016-02-26 16:04:50 +08003097 /* Exit keys' detection thread firstly */
3098 exit_menu_keys_detection();
lijuanga40d6302015-07-20 20:10:13 +08003099#endif
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003100 boot_linux_from_mmc();
3101 }
3102 else
3103 {
3104 boot_linux_from_flash();
3105 }
Dima Zavin214cc642009-01-26 11:16:21 -08003106}
3107
Chandan Uddaraju94183c02010-01-15 15:13:59 -08003108void cmd_reboot(const char *arg, void *data, unsigned sz)
3109{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003110 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08003111 fastboot_okay("");
3112 reboot_device(0);
3113}
3114
3115void cmd_reboot_bootloader(const char *arg, void *data, unsigned sz)
3116{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003117 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08003118 fastboot_okay("");
3119 reboot_device(FASTBOOT_MODE);
3120}
3121
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003122void cmd_oem_enable_charger_screen(const char *arg, void *data, unsigned size)
3123{
3124 dprintf(INFO, "Enabling charger screen check\n");
3125 device.charger_screen_enabled = 1;
3126 write_device_info(&device);
3127 fastboot_okay("");
3128}
3129
3130void cmd_oem_disable_charger_screen(const char *arg, void *data, unsigned size)
3131{
3132 dprintf(INFO, "Disabling charger screen check\n");
3133 device.charger_screen_enabled = 0;
3134 write_device_info(&device);
3135 fastboot_okay("");
3136}
3137
lijuanga25d8bb2015-09-16 13:11:52 +08003138void cmd_oem_off_mode_charger(const char *arg, void *data, unsigned size)
3139{
3140 char *p = NULL;
3141 const char *delim = " \t\n\r";
Parth Dixit407f15b2016-01-07 14:47:37 +05303142 char *sp;
lijuanga25d8bb2015-09-16 13:11:52 +08003143
3144 if (arg) {
Parth Dixit407f15b2016-01-07 14:47:37 +05303145 p = strtok_r((char *)arg, delim, &sp);
lijuanga25d8bb2015-09-16 13:11:52 +08003146 if (p) {
3147 if (!strncmp(p, "0", 1)) {
3148 device.charger_screen_enabled = 0;
3149 } else if (!strncmp(p, "1", 1)) {
3150 device.charger_screen_enabled = 1;
3151 }
3152 }
3153 }
3154
3155 /* update charger_screen_enabled value for getvar
3156 * command
3157 */
3158 snprintf(charger_screen_enabled, MAX_RSP_SIZE, "%d",
3159 device.charger_screen_enabled);
3160
3161 write_device_info(&device);
3162 fastboot_okay("");
3163}
3164
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05303165void cmd_oem_select_display_panel(const char *arg, void *data, unsigned size)
3166{
3167 dprintf(INFO, "Selecting display panel %s\n", arg);
3168 if (arg)
3169 strlcpy(device.display_panel, arg,
3170 sizeof(device.display_panel));
3171 write_device_info(&device);
3172 fastboot_okay("");
3173}
3174
Shashank Mittal162244e2011-08-08 19:01:25 -07003175void cmd_oem_unlock(const char *arg, void *data, unsigned sz)
3176{
lijuang4ece1e72015-08-14 21:02:36 +08003177 set_device_unlock(UNLOCK, TRUE);
vijay kumarc65876c2015-04-24 13:29:16 +05303178}
3179
3180void cmd_oem_unlock_go(const char *arg, void *data, unsigned sz)
3181{
lijuang4ece1e72015-08-14 21:02:36 +08003182 if(!device.is_unlocked) {
vijay kumarc65876c2015-04-24 13:29:16 +05303183 if(!is_allow_unlock) {
3184 fastboot_fail("oem unlock is not allowed");
3185 return;
3186 }
3187
lijuang4ece1e72015-08-14 21:02:36 +08003188 set_device_unlock_value(UNLOCK, TRUE);
vijay kumarc65876c2015-04-24 13:29:16 +05303189
lijuang4ece1e72015-08-14 21:02:36 +08003190 /* wipe data */
vijay kumarc65876c2015-04-24 13:29:16 +05303191 struct recovery_message msg;
lijuang4ece1e72015-08-14 21:02:36 +08003192
vijay kumarc65876c2015-04-24 13:29:16 +05303193 snprintf(msg.recovery, sizeof(msg.recovery), "recovery\n--wipe_data");
3194 write_misc(0, &msg, sizeof(msg));
3195
3196 fastboot_okay("");
3197 reboot_device(RECOVERY_MODE);
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003198 }
3199 fastboot_okay("");
3200}
3201
Channagoud Kadabiad259832015-05-29 11:14:17 -07003202static int aboot_frp_unlock(char *pname, void *data, unsigned sz)
3203{
3204 int ret = 1;
3205 uint32_t secret_key;
3206 char seckey_buffer[MAX_RSP_SIZE];
3207
3208 secret_key = aboot_get_secret_key();
3209 if (secret_key)
3210 {
3211 snprintf((char *) seckey_buffer, MAX_RSP_SIZE, "%x", secret_key);
3212 if (!memcmp((void *)data, (void *)seckey_buffer, sz))
3213 {
3214 is_allow_unlock = true;
3215 write_allow_oem_unlock(is_allow_unlock);
3216 ret = 0;
3217 }
3218 }
3219 return ret;
3220}
3221
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003222void cmd_oem_lock(const char *arg, void *data, unsigned sz)
3223{
lijuang4ece1e72015-08-14 21:02:36 +08003224 set_device_unlock(UNLOCK, FALSE);
Shashank Mittal162244e2011-08-08 19:01:25 -07003225}
3226
Shashank Mittala0032282011-08-26 14:50:11 -07003227void cmd_oem_devinfo(const char *arg, void *data, unsigned sz)
3228{
lijuang511a2b52015-08-14 20:50:51 +08003229 char response[MAX_RSP_SIZE];
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003230 snprintf(response, sizeof(response), "\tDevice tampered: %s", (device.is_tampered ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07003231 fastboot_info(response);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003232 snprintf(response, sizeof(response), "\tDevice unlocked: %s", (device.is_unlocked ? "true" : "false"));
3233 fastboot_info(response);
Parth Dixitddbc7352015-10-18 03:13:31 +05303234#if !VBOOT_MOTA
lijuang511a2b52015-08-14 20:50:51 +08003235 snprintf(response, sizeof(response), "\tDevice critical unlocked: %s", (device.is_unlock_critical ? "true" : "false"));
3236 fastboot_info(response);
Parth Dixitddbc7352015-10-18 03:13:31 +05303237#endif
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003238 snprintf(response, sizeof(response), "\tCharger screen enabled: %s", (device.charger_screen_enabled ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07003239 fastboot_info(response);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05303240 snprintf(response, sizeof(response), "\tDisplay panel: %s", (device.display_panel));
3241 fastboot_info(response);
Shashank Mittala0032282011-08-26 14:50:11 -07003242 fastboot_okay("");
3243}
3244
lijuang511a2b52015-08-14 20:50:51 +08003245void cmd_flashing_get_unlock_ability(const char *arg, void *data, unsigned sz)
3246{
3247 char response[MAX_RSP_SIZE];
3248 snprintf(response, sizeof(response), "\tget_unlock_ability: %d", is_allow_unlock);
3249 fastboot_info(response);
3250 fastboot_okay("");
3251}
3252
3253void cmd_flashing_lock_critical(const char *arg, void *data, unsigned sz)
3254{
lijuang4ece1e72015-08-14 21:02:36 +08003255 set_device_unlock(UNLOCK_CRITICAL, FALSE);
lijuang511a2b52015-08-14 20:50:51 +08003256}
3257
3258void cmd_flashing_unlock_critical(const char *arg, void *data, unsigned sz)
3259{
lijuang4ece1e72015-08-14 21:02:36 +08003260 set_device_unlock(UNLOCK_CRITICAL, TRUE);
lijuang511a2b52015-08-14 20:50:51 +08003261}
3262
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07003263void cmd_preflash(const char *arg, void *data, unsigned sz)
3264{
3265 fastboot_okay("");
3266}
3267
Mao Flynn7b379f32015-04-20 00:28:30 +08003268static uint8_t logo_header[LOGO_IMG_HEADER_SIZE];
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303269
Mao Flynn7b379f32015-04-20 00:28:30 +08003270int splash_screen_check_header(logo_img_header *header)
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303271{
Mao Flynn7b379f32015-04-20 00:28:30 +08003272 if (memcmp(header->magic, LOGO_IMG_MAGIC, 8))
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303273 return -1;
Mao Flynn7b379f32015-04-20 00:28:30 +08003274 if (header->width == 0 || header->height == 0)
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303275 return -1;
3276 return 0;
3277}
3278
Mao Flynn7b379f32015-04-20 00:28:30 +08003279int splash_screen_flash()
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003280{
3281 struct ptentry *ptn;
3282 struct ptable *ptable;
Mao Flynn7b379f32015-04-20 00:28:30 +08003283 struct logo_img_header *header;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003284 struct fbcon_config *fb_display = NULL;
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08003285
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303286 ptable = flash_get_ptable();
3287 if (ptable == NULL) {
Mao Flynn7b379f32015-04-20 00:28:30 +08003288 dprintf(CRITICAL, "ERROR: Partition table not found\n");
3289 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303290 }
Mao Flynn7b379f32015-04-20 00:28:30 +08003291
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303292 ptn = ptable_find(ptable, "splash");
3293 if (ptn == NULL) {
3294 dprintf(CRITICAL, "ERROR: splash Partition not found\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003295 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303296 }
Mao Flynn7b379f32015-04-20 00:28:30 +08003297 if (flash_read(ptn, 0, (void *)logo_header, LOGO_IMG_HEADER_SIZE)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303298 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003299 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303300 }
3301
Mao Flynn7b379f32015-04-20 00:28:30 +08003302 header = (struct logo_img_header *)logo_header;
3303 if (splash_screen_check_header(header)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303304 dprintf(CRITICAL, "ERROR: Boot image header invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003305 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303306 }
3307
3308 fb_display = fbcon_display();
3309 if (fb_display) {
Mao Flynn7b379f32015-04-20 00:28:30 +08003310 if (header->type && (header->blocks != 0)) { // RLE24 compressed data
3311 uint8_t *base = (uint8_t *) fb_display->base + LOGO_IMG_OFFSET;
3312
3313 /* if the logo is full-screen size, remove "fbcon_clear()" */
3314 if ((header->width != fb_display->width)
3315 || (header->height != fb_display->height))
3316 fbcon_clear();
3317
3318 if (flash_read(ptn + LOGO_IMG_HEADER_SIZE, 0,
3319 (uint32_t *)base,
3320 (header->blocks * 512))) {
3321 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
3322 return -1;
3323 }
3324 fbcon_extract_to_screen(header, base);
3325 return 0;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003326 }
Mao Flynn7b379f32015-04-20 00:28:30 +08003327
Veera Sundaram Sankaranc33416f2015-09-28 10:25:55 -07003328 if ((header->width > fb_display->width) || (header->height > fb_display->height)) {
3329 dprintf(CRITICAL, "Logo config greater than fb config. Fall back default logo\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003330 return -1;
3331 }
3332
Vineet Bajaj99291ed2014-09-09 12:29:46 +05303333 uint8_t *base = (uint8_t *) fb_display->base;
Mao Flynn7b379f32015-04-20 00:28:30 +08003334 if (flash_read(ptn + LOGO_IMG_HEADER_SIZE, 0,
3335 (uint32_t *)base,
3336 ((((header->width * header->height * fb_display->bpp/8) + 511) >> 9) << 9))) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303337 fbcon_clear();
Vineet Bajaj99291ed2014-09-09 12:29:46 +05303338 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003339 return -1;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003340 }
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303341 }
3342
Mao Flynn7b379f32015-04-20 00:28:30 +08003343 return 0;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303344}
3345
Mao Flynn7b379f32015-04-20 00:28:30 +08003346int splash_screen_mmc()
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303347{
3348 int index = INVALID_PTN;
3349 unsigned long long ptn = 0;
3350 struct fbcon_config *fb_display = NULL;
Mao Flynn7b379f32015-04-20 00:28:30 +08003351 struct logo_img_header *header;
Mao Flynn1893a7f2015-06-03 12:03:36 +08003352 uint32_t blocksize, realsize, readsize;
3353 uint8_t *base;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303354
3355 index = partition_get_index("splash");
3356 if (index == 0) {
3357 dprintf(CRITICAL, "ERROR: splash Partition table not found\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003358 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303359 }
3360
3361 ptn = partition_get_offset(index);
3362 if (ptn == 0) {
3363 dprintf(CRITICAL, "ERROR: splash Partition invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003364 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303365 }
3366
Mao Flynn1893a7f2015-06-03 12:03:36 +08003367 mmc_set_lun(partition_get_lun(index));
3368
3369 blocksize = mmc_get_device_blocksize();
3370 if (blocksize == 0) {
3371 dprintf(CRITICAL, "ERROR:splash Partition invalid blocksize\n");
3372 return -1;
3373 }
3374
3375 fb_display = fbcon_display();
Channagoud Kadabidaf10a62015-07-22 11:51:55 -07003376 if (!fb_display)
3377 {
3378 dprintf(CRITICAL, "ERROR: fb config is not allocated\n");
3379 return -1;
3380 }
3381
Mao Flynn1893a7f2015-06-03 12:03:36 +08003382 base = (uint8_t *) fb_display->base;
3383
3384 if (mmc_read(ptn, (uint32_t *)(base + LOGO_IMG_OFFSET), blocksize)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303385 dprintf(CRITICAL, "ERROR: Cannot read splash image header\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 header = (struct logo_img_header *)(base + LOGO_IMG_OFFSET);
Mao Flynn7b379f32015-04-20 00:28:30 +08003390 if (splash_screen_check_header(header)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303391 dprintf(CRITICAL, "ERROR: Splash image header invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003392 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303393 }
3394
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303395 if (fb_display) {
Mao Flynn1893a7f2015-06-03 12:03:36 +08003396 if (header->type && (header->blocks != 0)) { /* 1 RLE24 compressed data */
3397 base += LOGO_IMG_OFFSET;
Mao Flynn7b379f32015-04-20 00:28:30 +08003398
Mao Flynn1893a7f2015-06-03 12:03:36 +08003399 realsize = header->blocks * 512;
3400 readsize = ROUNDUP((realsize + LOGO_IMG_HEADER_SIZE), blocksize) - blocksize;
3401
3402 /* if the logo is not full-screen size, clean screen */
Mao Flynn7b379f32015-04-20 00:28:30 +08003403 if ((header->width != fb_display->width)
3404 || (header->height != fb_display->height))
3405 fbcon_clear();
3406
Mao Flynn1893a7f2015-06-03 12:03:36 +08003407 if (mmc_read(ptn + blocksize, (uint32_t *)(base + blocksize), readsize)) {
Mao Flynn7b379f32015-04-20 00:28:30 +08003408 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
3409 return -1;
3410 }
Mao Flynn7b379f32015-04-20 00:28:30 +08003411
Mao Flynn1893a7f2015-06-03 12:03:36 +08003412 fbcon_extract_to_screen(header, (base + LOGO_IMG_HEADER_SIZE));
3413 } else { /* 2 Raw BGR data */
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303414
Veera Sundaram Sankaranc33416f2015-09-28 10:25:55 -07003415 if ((header->width > fb_display->width) || (header->height > fb_display->height)) {
3416 dprintf(CRITICAL, "Logo config greater than fb config. Fall back default logo\n");
Mao Flynn1893a7f2015-06-03 12:03:36 +08003417 return -1;
3418 }
3419
3420 realsize = header->width * header->height * fb_display->bpp / 8;
3421 readsize = ROUNDUP((realsize + LOGO_IMG_HEADER_SIZE), blocksize) - blocksize;
3422
3423 if (blocksize == LOGO_IMG_HEADER_SIZE) { /* read the content directly */
3424 if (mmc_read((ptn + LOGO_IMG_HEADER_SIZE), (uint32_t *)base, readsize)) {
3425 fbcon_clear();
3426 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
3427 return -1;
3428 }
3429 } else {
3430 if (mmc_read(ptn + blocksize ,
3431 (uint32_t *)(base + LOGO_IMG_OFFSET + blocksize), readsize)) {
3432 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
3433 return -1;
3434 }
3435 memmove(base, (base + LOGO_IMG_OFFSET + LOGO_IMG_HEADER_SIZE), realsize);
3436 }
3437 }
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303438 }
3439
Mao Flynn7b379f32015-04-20 00:28:30 +08003440 return 0;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303441}
3442
Mao Flynn7b379f32015-04-20 00:28:30 +08003443int fetch_image_from_partition()
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303444{
3445 if (target_is_emmc_boot()) {
3446 return splash_screen_mmc();
3447 } else {
3448 return splash_screen_flash();
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003449 }
3450}
3451
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07003452/* Get the size from partiton name */
3453static void get_partition_size(const char *arg, char *response)
3454{
3455 uint64_t ptn = 0;
3456 uint64_t size;
3457 int index = INVALID_PTN;
3458
3459 index = partition_get_index(arg);
3460
3461 if (index == INVALID_PTN)
3462 {
3463 dprintf(CRITICAL, "Invalid partition index\n");
3464 return;
3465 }
3466
3467 ptn = partition_get_offset(index);
3468
3469 if(!ptn)
3470 {
3471 dprintf(CRITICAL, "Invalid partition name %s\n", arg);
3472 return;
3473 }
3474
3475 size = partition_get_size(index);
3476
3477 snprintf(response, MAX_RSP_SIZE, "\t 0x%llx", size);
3478 return;
3479}
3480
3481/*
3482 * Publish the partition type & size info
3483 * fastboot getvar will publish the required information.
3484 * fastboot getvar partition_size:<partition_name>: partition size in hex
3485 * fastboot getvar partition_type:<partition_name>: partition type (ext/fat)
3486 */
3487static void publish_getvar_partition_info(struct getvar_partition_info *info, uint8_t num_parts)
3488{
3489 uint8_t i;
3490
3491 for (i = 0; i < num_parts; i++) {
3492 get_partition_size(info[i].part_name, info[i].size_response);
3493
3494 if (strlcat(info[i].getvar_size, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
3495 {
3496 dprintf(CRITICAL, "partition size name truncated\n");
3497 return;
3498 }
3499 if (strlcat(info[i].getvar_type, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
3500 {
3501 dprintf(CRITICAL, "partition type name truncated\n");
3502 return;
3503 }
3504
3505 /* publish partition size & type info */
3506 fastboot_publish((const char *) info[i].getvar_size, (const char *) info[i].size_response);
3507 fastboot_publish((const char *) info[i].getvar_type, (const char *) info[i].type_response);
3508 }
3509}
3510
lijuang4ece1e72015-08-14 21:02:36 +08003511void get_product_name(unsigned char *buf)
3512{
3513 snprintf((char*)buf, MAX_RSP_SIZE, "%s", TARGET(BOARD));
3514 return;
3515}
3516
3517void get_bootloader_version(unsigned char *buf)
3518{
3519 snprintf((char*)buf, MAX_RSP_SIZE, "%s", device.bootloader_version);
3520 return;
3521}
3522
3523void get_baseband_version(unsigned char *buf)
3524{
3525 snprintf((char*)buf, MAX_RSP_SIZE, "%s", device.radio_version);
3526 return;
3527}
3528
3529bool is_device_locked()
3530{
3531 return device.is_unlocked ? false:true;
3532}
3533
Amol Jadi5edf3552013-07-23 14:15:34 -07003534/* register commands and variables for fastboot */
3535void aboot_fastboot_register_commands(void)
3536{
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003537 int i;
lijuangf16461c2015-08-03 17:09:34 +08003538 char hw_platform_buf[MAX_RSP_SIZE];
Amol Jadi5edf3552013-07-23 14:15:34 -07003539
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003540 struct fastboot_cmd_desc cmd_list[] = {
lijuang511a2b52015-08-14 20:50:51 +08003541 /* By default the enabled list is empty. */
3542 {"", NULL},
3543 /* move commands enclosed within the below ifndef to here
3544 * if they need to be enabled in user build.
3545 */
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003546#ifndef DISABLE_FASTBOOT_CMDS
lijuang511a2b52015-08-14 20:50:51 +08003547 /* Register the following commands only for non-user builds */
3548 {"flash:", cmd_flash},
3549 {"erase:", cmd_erase},
3550 {"boot", cmd_boot},
3551 {"continue", cmd_continue},
3552 {"reboot", cmd_reboot},
3553 {"reboot-bootloader", cmd_reboot_bootloader},
3554 {"oem unlock", cmd_oem_unlock},
3555 {"oem unlock-go", cmd_oem_unlock_go},
3556 {"oem lock", cmd_oem_lock},
3557 {"flashing unlock", cmd_oem_unlock},
3558 {"flashing lock", cmd_oem_lock},
3559 {"flashing lock_critical", cmd_flashing_lock_critical},
3560 {"flashing unlock_critical", cmd_flashing_unlock_critical},
3561 {"flashing get_unlock_ability", cmd_flashing_get_unlock_ability},
3562 {"oem device-info", cmd_oem_devinfo},
3563 {"preflash", cmd_preflash},
3564 {"oem enable-charger-screen", cmd_oem_enable_charger_screen},
3565 {"oem disable-charger-screen", cmd_oem_disable_charger_screen},
lijuanga25d8bb2015-09-16 13:11:52 +08003566 {"oem off-mode-charge", cmd_oem_off_mode_charger},
lijuang511a2b52015-08-14 20:50:51 +08003567 {"oem select-display-panel", cmd_oem_select_display_panel},
Channagoud Kadabi736c4962015-08-21 11:56:52 -07003568#if UNITTEST_FW_SUPPORT
Channagoud Kadabid5ddf482015-09-28 10:31:35 -07003569 {"oem run-tests", cmd_oem_runtests},
Channagoud Kadabi736c4962015-08-21 11:56:52 -07003570#endif
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003571#endif
lijuang511a2b52015-08-14 20:50:51 +08003572 };
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003573
3574 int fastboot_cmds_count = sizeof(cmd_list)/sizeof(cmd_list[0]);
3575 for (i = 1; i < fastboot_cmds_count; i++)
3576 fastboot_register(cmd_list[i].name,cmd_list[i].cb);
3577
Amol Jadi5edf3552013-07-23 14:15:34 -07003578 /* publish variables and their values */
3579 fastboot_publish("product", TARGET(BOARD));
3580 fastboot_publish("kernel", "lk");
3581 fastboot_publish("serialno", sn_buf);
3582
3583 /*
3584 * partition info is supported only for emmc partitions
3585 * Calling this for NAND prints some error messages which
3586 * is harmless but misleading. Avoid calling this for NAND
3587 * devices.
3588 */
3589 if (target_is_emmc_boot())
3590 publish_getvar_partition_info(part_info, ARRAY_SIZE(part_info));
3591
3592 /* Max download size supported */
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003593 snprintf(max_download_size, MAX_RSP_SIZE, "\t0x%x",
3594 target_get_max_flash_size());
Amol Jadi5edf3552013-07-23 14:15:34 -07003595 fastboot_publish("max-download-size", (const char *) max_download_size);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003596 /* Is the charger screen check enabled */
3597 snprintf(charger_screen_enabled, MAX_RSP_SIZE, "%d",
3598 device.charger_screen_enabled);
3599 fastboot_publish("charger-screen-enabled",
3600 (const char *) charger_screen_enabled);
lijuanga25d8bb2015-09-16 13:11:52 +08003601 fastboot_publish("off-mode-charge", (const char *) charger_screen_enabled);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05303602 snprintf(panel_display_mode, MAX_RSP_SIZE, "%s",
3603 device.display_panel);
3604 fastboot_publish("display-panel",
3605 (const char *) panel_display_mode);
Channagoud Kadabiaafbbfc2015-03-04 17:52:56 -08003606 fastboot_publish("version-bootloader", (const char *) device.bootloader_version);
3607 fastboot_publish("version-baseband", (const char *) device.radio_version);
lijuangf16461c2015-08-03 17:09:34 +08003608 fastboot_publish("secure", is_secure_boot_enable()? "yes":"no");
3609 smem_get_hw_platform_name((unsigned char *) hw_platform_buf, sizeof(hw_platform_buf));
3610 snprintf(get_variant, MAX_RSP_SIZE, "%s %s", hw_platform_buf,
3611 target_is_emmc_boot()? "eMMC":"UFS");
3612 fastboot_publish("variant", (const char *) get_variant);
lijuang65c5a822015-08-29 16:35:36 +08003613#if CHECK_BAT_VOLTAGE
lijuang102dfa92015-10-09 18:31:03 +08003614 update_battery_status();
lijuang65c5a822015-08-29 16:35:36 +08003615 fastboot_publish("battery-voltage", (const char *) battery_voltage);
lijuang102dfa92015-10-09 18:31:03 +08003616 fastboot_publish("battery-soc-ok", (const char *) battery_soc_ok);
lijuang65c5a822015-08-29 16:35:36 +08003617#endif
Amol Jadi5edf3552013-07-23 14:15:34 -07003618}
3619
Brian Swetland9c4c0752009-01-25 16:23:50 -08003620void aboot_init(const struct app_descriptor *app)
3621{
Shashank Mittal4f99a882010-02-01 13:58:50 -08003622 unsigned reboot_mode = 0;
Chandan Uddarajubedca152010-06-02 23:05:15 -07003623
lijuang39831732016-01-08 17:49:02 +08003624 /* Initialise wdog to catch early lk crashes */
3625#if WDOG_SUPPORT
3626 msm_wdog_init();
3627#endif
3628
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07003629 /* Setup page size information for nv storage */
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003630 if (target_is_emmc_boot())
3631 {
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07003632 page_size = mmc_page_size();
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003633 page_mask = page_size - 1;
3634 }
3635 else
3636 {
3637 page_size = flash_page_size();
3638 page_mask = page_size - 1;
3639 }
3640
Deepa Dinamani0e163a42013-05-24 17:08:15 -07003641 ASSERT((MEMBASE + MEMSIZE) > MEMBASE);
3642
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003643 read_device_info(&device);
vijay kumarc65876c2015-04-24 13:29:16 +05303644 read_allow_oem_unlock(&device);
Shashank Mittal162244e2011-08-08 19:01:25 -07003645
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08003646 /* Display splash screen if enabled */
3647#if DISPLAY_SPLASH_SCREEN
lijuang99c02d82015-02-13 19:04:34 +08003648#if NO_ALARM_DISPLAY
3649 if (!check_alarm_boot()) {
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08003650#endif
lijuang99c02d82015-02-13 19:04:34 +08003651 dprintf(SPEW, "Display Init: Start\n");
Channagoud Kadabi90869ce2015-04-27 11:15:14 -07003652#if ENABLE_WBC
Channagoud Kadabid801ac72015-08-26 11:21:51 -07003653 /* Wait if the display shutdown is in progress */
3654 while(pm_app_display_shutdown_in_prgs());
Channagoud Kadabi90869ce2015-04-27 11:15:14 -07003655 if (!pm_appsbl_display_init_done())
3656 target_display_init(device.display_panel);
3657 else
3658 display_image_on_screen();
3659#else
lijuang99c02d82015-02-13 19:04:34 +08003660 target_display_init(device.display_panel);
Channagoud Kadabi90869ce2015-04-27 11:15:14 -07003661#endif
lijuang99c02d82015-02-13 19:04:34 +08003662 dprintf(SPEW, "Display Init: Done\n");
3663#if NO_ALARM_DISPLAY
3664 }
3665#endif
3666#endif
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08003667
Greg Griscod6250552011-06-29 14:40:23 -07003668 target_serialno((unsigned char *) sn_buf);
Ajay Dudanib06c05f2011-05-12 14:46:10 -07003669 dprintf(SPEW,"serial number: %s\n",sn_buf);
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -08003670
Dhaval Patel223ec952013-07-18 14:49:44 -07003671 memset(display_panel_buf, '\0', MAX_PANEL_BUF_SIZE);
3672
Matthew Qindefd5562014-07-11 18:02:40 +08003673 /*
3674 * Check power off reason if user force reset,
3675 * if yes phone will do normal boot.
3676 */
3677 if (is_user_force_reset())
3678 goto normal_boot;
3679
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003680 /* Check if we should do something other than booting up */
Ameya Thakur0b9c2442013-05-10 13:22:56 -07003681 if (keys_get_state(KEY_VOLUMEUP) && keys_get_state(KEY_VOLUMEDOWN))
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07003682 {
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03003683 dprintf(ALWAYS,"dload mode key sequence detected\n");
lijuang395b5e62015-11-19 17:39:44 +08003684 reboot_device(EMERGENCY_DLOAD);
3685 dprintf(CRITICAL,"Failed to reboot into dload mode\n");
3686
Ameya Thakur0b9c2442013-05-10 13:22:56 -07003687 boot_into_fastboot = true;
3688 }
3689 if (!boot_into_fastboot)
3690 {
3691 if (keys_get_state(KEY_HOME) || keys_get_state(KEY_VOLUMEUP))
3692 boot_into_recovery = 1;
3693 if (!boot_into_recovery &&
3694 (keys_get_state(KEY_BACK) || keys_get_state(KEY_VOLUMEDOWN)))
Pavel Nedev5d91d412013-04-29 11:34:24 +03003695 boot_into_fastboot = true;
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07003696 }
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003697 #if NO_KEYPAD_DRIVER
Kinson Chik0b1c8162011-08-31 16:31:57 -07003698 if (fastboot_trigger())
Pavel Nedev5d91d412013-04-29 11:34:24 +03003699 boot_into_fastboot = true;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003700 #endif
Chandan Uddarajubedca152010-06-02 23:05:15 -07003701
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003702#if USE_PON_REBOOT_REG
3703 reboot_mode = check_hard_reboot_mode();
3704#else
Ajay Dudani77421292010-10-27 19:34:06 -07003705 reboot_mode = check_reboot_mode();
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003706#endif
3707 if (reboot_mode == RECOVERY_MODE)
3708 {
Ajay Dudani77421292010-10-27 19:34:06 -07003709 boot_into_recovery = 1;
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003710 }
3711 else if(reboot_mode == FASTBOOT_MODE)
3712 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03003713 boot_into_fastboot = true;
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003714 }
3715 else if(reboot_mode == ALARM_BOOT)
3716 {
Matthew Qind886f3c2014-01-17 16:52:01 +08003717 boot_reason_alarm = true;
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003718 }
Parth Dixit207573a2015-10-27 17:26:21 +05303719#if VERIFIED_BOOT
Parth Dixitddbc7352015-10-18 03:13:31 +05303720#if !VBOOT_MOTA
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003721 else if (reboot_mode == DM_VERITY_ENFORCING)
3722 {
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07003723 device.verity_mode = 1;
3724 write_device_info(&device);
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003725 }
3726 else if (reboot_mode == DM_VERITY_LOGGING)
3727 {
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07003728 device.verity_mode = 0;
3729 write_device_info(&device);
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003730 }
3731 else if (reboot_mode == DM_VERITY_KEYSCLEAR)
3732 {
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07003733 if(send_delete_keys_to_tz())
3734 ASSERT(0);
Ajay Dudani77421292010-10-27 19:34:06 -07003735 }
Parth Dixit207573a2015-10-27 17:26:21 +05303736#endif
Parth Dixitddbc7352015-10-18 03:13:31 +05303737#endif
Ajay Dudani77421292010-10-27 19:34:06 -07003738
Matthew Qindefd5562014-07-11 18:02:40 +08003739normal_boot:
Pavel Nedev5d91d412013-04-29 11:34:24 +03003740 if (!boot_into_fastboot)
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003741 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03003742 if (target_is_emmc_boot())
Shashank Mittala0032282011-08-26 14:50:11 -07003743 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03003744 if(emmc_recovery_init())
3745 dprintf(ALWAYS,"error in emmc_recovery_init\n");
3746 if(target_use_signed_kernel())
Shashank Mittala0032282011-08-26 14:50:11 -07003747 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03003748 if((device.is_unlocked) || (device.is_tampered))
3749 {
3750 #ifdef TZ_TAMPER_FUSE
3751 set_tamper_fuse_cmd();
3752 #endif
3753 #if USE_PCOM_SECBOOT
3754 set_tamper_flag(device.is_tampered);
3755 #endif
3756 }
Shashank Mittala0032282011-08-26 14:50:11 -07003757 }
Amit Blay6281ebc2015-01-11 14:44:08 +02003758
Pavel Nedev5d91d412013-04-29 11:34:24 +03003759 boot_linux_from_mmc();
Shashank Mittala0032282011-08-26 14:50:11 -07003760 }
Pavel Nedev5d91d412013-04-29 11:34:24 +03003761 else
3762 {
3763 recovery_init();
3764 #if USE_PCOM_SECBOOT
3765 if((device.is_unlocked) || (device.is_tampered))
3766 set_tamper_flag(device.is_tampered);
3767 #endif
3768 boot_linux_from_flash();
3769 }
3770 dprintf(CRITICAL, "ERROR: Could not do normal boot. Reverting "
3771 "to fastboot mode.\n");
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003772 }
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003773
Amol Jadi5edf3552013-07-23 14:15:34 -07003774 /* We are here means regular boot did not happen. Start fastboot. */
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07003775
Amol Jadi5edf3552013-07-23 14:15:34 -07003776 /* register aboot specific fastboot commands */
3777 aboot_fastboot_register_commands();
Amol Jadi57abe4c2011-05-24 15:47:27 -07003778
Amol Jadi5edf3552013-07-23 14:15:34 -07003779 /* dump partition table for debug info */
Kinson Chikf1a43512011-07-14 11:28:39 -07003780 partition_dump();
Amol Jadi5edf3552013-07-23 14:15:34 -07003781
3782 /* initialize and start fastboot */
3783 fastboot_init(target_get_scratch_address(), target_get_max_flash_size());
lijuang4ece1e72015-08-14 21:02:36 +08003784#if FBCON_DISPLAY_MSG
lijuang9a7d3b92015-11-30 14:41:24 +08003785 display_fastboot_menu();
lijuang4ece1e72015-08-14 21:02:36 +08003786#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08003787}
3788
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07003789uint32_t get_page_size()
3790{
3791 return page_size;
3792}
3793
Amir Samuelov57a6fa22013-06-05 16:36:43 +03003794/*
3795 * Calculated and save hash (SHA256) for non-signed boot image.
3796 *
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07003797 * @param image_addr - Boot image address
3798 * @param image_size - Size of the boot image
Amir Samuelov57a6fa22013-06-05 16:36:43 +03003799 *
3800 * @return int - 0 on success, negative value on failure.
3801 */
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07003802static int aboot_save_boot_hash_mmc(uint32_t image_addr, uint32_t image_size)
Amir Samuelov57a6fa22013-06-05 16:36:43 +03003803{
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07003804 unsigned int digest[8];
3805#if IMAGE_VERIF_ALGO_SHA1
3806 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA1;
3807#else
3808 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA256;
3809#endif
Amir Samuelov57a6fa22013-06-05 16:36:43 +03003810
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07003811 target_crypto_init_params();
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -08003812 hash_find((unsigned char *) image_addr, image_size, (unsigned char *)&digest, auth_algo);
Amir Samuelov57a6fa22013-06-05 16:36:43 +03003813
3814 save_kernel_hash_cmd(digest);
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07003815 dprintf(INFO, "aboot_save_boot_hash_mmc: imagesize_actual size %d bytes.\n", (int) image_size);
Amir Samuelov57a6fa22013-06-05 16:36:43 +03003816
3817 return 0;
3818}
3819
Brian Swetland9c4c0752009-01-25 16:23:50 -08003820APP_START(aboot)
3821 .init = aboot_init,
3822APP_END