blob: 02b0bd3678d811dda65d2d6607ee7dc0f23aa462 [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
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002190#if VERIFIED_BOOT
Brian Swetland9c4c0752009-01-25 16:23:50 -08002191 if(target_build_variant_user() && !device.is_unlocked)
2192 {
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002193 fastboot_fail("unlock device to use this command");
Brian Swetland9c4c0752009-01-25 16:23:50 -08002194 return;
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002195 }
2196#endif
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07002197
2198 if (sz < sizeof(hdr)) {
2199 fastboot_fail("invalid bootimage header");
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002200 return;
2201 }
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002202
2203 hdr = (struct boot_img_hdr *)data;
2204
2205 /* ensure commandline is terminated */
2206 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
2207
2208 if(target_is_emmc_boot() && hdr->page_size) {
2209 page_size = hdr->page_size;
Channagoud Kadabi3d839012014-06-26 14:26:39 -07002210 page_mask = page_size - 1;
2211 }
2212
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002213 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
2214 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Channagoud Kadabi3d839012014-06-26 14:26:39 -07002215#if DEVICE_TREE
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002216 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
2217#endif
2218
2219 image_actual = ADD_OF(page_size, kernel_actual);
2220 image_actual = ADD_OF(image_actual, ramdisk_actual);
2221 image_actual = ADD_OF(image_actual, dt_actual);
2222
Channagoud Kadabi3d839012014-06-26 14:26:39 -07002223 if (target_use_signed_kernel() && (!device.is_unlocked))
2224 image_actual = ADD_OF(image_actual, sig_actual);
2225
2226 /* sz should have atleast raw boot image */
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002227 if (image_actual > sz) {
Amit Blay4aa292f2015-04-28 21:55:59 +03002228 fastboot_fail("bootimage: incomplete or not signed");
2229 return;
2230 }
Shay Nachmani062aa3f2015-05-17 17:28:44 +03002231
Sridhar Parasuram361e4cd2015-09-24 11:19:19 -07002232 // Initialize boot state before trying to verify boot.img
2233#if VERIFIED_BOOT
Channagoud Kadabi699466e2015-11-03 12:37:42 -08002234 boot_verifier_init();
Channagoud Kadabibf6ce7d2015-09-17 13:25:35 -07002235 /* Handle overflow if the input image size is greater than
2236 * boot image buffer can hold
2237 */
2238 if ((target_get_max_flash_size() - (image_actual - sig_actual)) < page_size)
2239 {
2240 fastboot_fail("booimage: size is greater than boot image buffer can hold");
2241 return;
2242 }
Sridhar Parasuram361e4cd2015-09-24 11:19:19 -07002243#endif
2244
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002245 /* Verify the boot image
2246 * device & page_size are initialized in aboot_init
Channagoud Kadabia22144f2013-03-20 11:49:01 -07002247 */
Sridhar Parasuram7ae9fbc2015-09-17 09:56:30 -07002248 if (target_use_signed_kernel() && (!device.is_unlocked)) {
Channagoud Kadabia22144f2013-03-20 11:49:01 -07002249 /* Pass size excluding signature size, otherwise we would try to
2250 * access signature beyond its length
2251 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08002252 verify_signed_bootimg((uint32_t)data, (image_actual - sig_actual));
Sridhar Parasuram7ae9fbc2015-09-17 09:56:30 -07002253 }
Amit Blay4aa292f2015-04-28 21:55:59 +03002254#ifdef MDTP_SUPPORT
2255 else
2256 {
Shay Nachmani062aa3f2015-05-17 17:28:44 +03002257 /* fastboot boot is not allowed when MDTP is activated */
Amit Blay4aa292f2015-04-28 21:55:59 +03002258 mdtp_ext_partition_verification_t ext_partition;
Amit Blay8a510302015-08-17 09:20:01 +03002259
2260 if (!is_mdtp_activated) {
2261 ext_partition.partition = MDTP_PARTITION_NONE;
2262 mdtp_fwlock_verify_lock(&ext_partition);
2263 }
Amit Blay4aa292f2015-04-28 21:55:59 +03002264 }
Shay Nachmani062aa3f2015-05-17 17:28:44 +03002265
Shay Nachmani062aa3f2015-05-17 17:28:44 +03002266 mdtp_activated(&is_mdtp_activated);
2267 if(is_mdtp_activated){
2268 dprintf(CRITICAL, "fastboot boot command is not available.\n");
2269 return;
2270 }
Amit Blay4aa292f2015-04-28 21:55:59 +03002271#endif /* MDTP_SUPPORT */
2272
Sridhar Parasuram361e4cd2015-09-24 11:19:19 -07002273#if VERIFIED_BOOT
Parth Dixitbc9b6492015-10-18 00:41:38 +05302274#if !VBOOT_MOTA
Sridhar Parasuram361e4cd2015-09-24 11:19:19 -07002275 // send root of trust
2276 if(!send_rot_command((uint32_t)device.is_unlocked))
2277 ASSERT(0);
2278#endif
Parth Dixitbc9b6492015-10-18 00:41:38 +05302279#endif
Channagoud Kadabia22144f2013-03-20 11:49:01 -07002280 /*
Matthew Qinbb7923d2015-02-09 10:56:09 +08002281 * Check if the kernel image is a gzip package. If yes, need to decompress it.
2282 * If not, continue booting.
2283 */
2284 if (is_gzip_package((unsigned char *)(data + page_size), hdr->kernel_size))
2285 {
2286 out_addr = (unsigned char *)target_get_scratch_address();
2287 out_addr = (unsigned char *)(out_addr + image_actual + page_size);
2288 out_avai_len = target_get_max_flash_size() - image_actual - page_size;
Matthew Qin0b15b322015-05-19 05:20:54 -04002289 dprintf(INFO, "decompressing kernel image: start\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08002290 ret = decompress((unsigned char *)(ptr + page_size),
2291 hdr->kernel_size, out_addr, out_avai_len,
2292 &dtb_offset, &out_len);
2293 if (ret)
2294 {
Matthew Qin0b15b322015-05-19 05:20:54 -04002295 dprintf(CRITICAL, "decompressing image failed!!!\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08002296 ASSERT(0);
2297 }
2298
Matthew Qin0b15b322015-05-19 05:20:54 -04002299 dprintf(INFO, "decompressing kernel image: done\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08002300 kptr = (struct kernel64_hdr *)out_addr;
2301 kernel_start_addr = out_addr;
2302 kernel_size = out_len;
2303 } else {
2304 kptr = (struct kernel64_hdr*)((char *)data + page_size);
2305 kernel_start_addr = (unsigned char *)((char *)data + page_size);
2306 kernel_size = hdr->kernel_size;
2307 }
2308
2309 /*
Channagoud Kadabia22144f2013-03-20 11:49:01 -07002310 * Update the kernel/ramdisk/tags address if the boot image header
2311 * has default values, these default values come from mkbootimg when
2312 * the boot image is flashed using fastboot flash:raw
2313 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08002314 update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr));
Dima Zavin3cadfff2013-03-21 14:30:48 -07002315
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002316 /* Get virtual addresses since the hdr saves physical addresses. */
2317 hdr->kernel_addr = VA(hdr->kernel_addr);
2318 hdr->ramdisk_addr = VA(hdr->ramdisk_addr);
2319 hdr->tags_addr = VA(hdr->tags_addr);
Brian Swetland9c4c0752009-01-25 16:23:50 -08002320
Matthew Qinbb7923d2015-02-09 10:56:09 +08002321 kernel_size = ROUND_TO_PAGE(kernel_size, page_mask);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002322 /* Check if the addresses in the header are valid. */
Matthew Qinbb7923d2015-02-09 10:56:09 +08002323 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_size) ||
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002324 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
2325 {
2326 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
Channagoud Kadabi881856c2013-12-03 11:19:20 -08002327 return;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002328 }
2329
Amol Jadicb524072012-08-09 16:40:18 -07002330#if DEVICE_TREE
Matthew Qin271927e2015-03-31 22:07:07 -04002331 scratch_offset = image_actual + page_size + out_len;
Amol Jadicb524072012-08-09 16:40:18 -07002332 /* find correct dtb and copy it to right location */
Matthew Qin271927e2015-03-31 22:07:07 -04002333 ret = copy_dtb(data, scratch_offset);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002334
2335 dtb_copied = !ret ? 1 : 0;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002336#else
2337 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
2338 {
2339 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
Channagoud Kadabi881856c2013-12-03 11:19:20 -08002340 return;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002341 }
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002342#endif
2343
2344 /* Load ramdisk & kernel */
2345 memmove((void*) hdr->ramdisk_addr, ptr + page_size + kernel_actual, hdr->ramdisk_size);
Matthew Qinbb7923d2015-02-09 10:56:09 +08002346 memmove((void*) hdr->kernel_addr, (char*) (kernel_start_addr), kernel_size);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002347
2348#if DEVICE_TREE
Matthew Qinbb7923d2015-02-09 10:56:09 +08002349 if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual))
2350 {
2351 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
2352 return;
2353 }
2354
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002355 /*
2356 * If dtb is not found look for appended DTB in the kernel.
2357 * If appended dev tree is found, update the atags with
2358 * memory address to the DTB appended location on RAM.
2359 * Else update with the atags address in the kernel header
2360 */
2361 if (!dtb_copied) {
2362 void *dtb;
Matthew Qinbb7923d2015-02-09 10:56:09 +08002363 dtb = dev_tree_appended((void*)(ptr + page_size),
2364 hdr->kernel_size, dtb_offset,
Dima Zavine63e5572013-05-03 12:23:06 -07002365 (void *)hdr->tags_addr);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002366 if (!dtb) {
2367 fastboot_fail("dtb not found");
2368 return;
2369 }
Amol Jadicb524072012-08-09 16:40:18 -07002370 }
2371#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08002372
2373 fastboot_okay("");
Amol Jadi7c4316c2013-10-07 14:19:26 -07002374 fastboot_stop();
Brian Swetland9c4c0752009-01-25 16:23:50 -08002375
Dima Zavin77e41f32013-03-06 16:10:43 -08002376 boot_linux((void*) hdr->kernel_addr, (void*) hdr->tags_addr,
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002377 (const char*) hdr->cmdline, board_machtype(),
2378 (void*) hdr->ramdisk_addr, hdr->ramdisk_size);
Brian Swetland9c4c0752009-01-25 16:23:50 -08002379}
2380
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002381void cmd_erase_nand(const char *arg, void *data, unsigned sz)
Dima Zavin214cc642009-01-26 11:16:21 -08002382{
2383 struct ptentry *ptn;
2384 struct ptable *ptable;
2385
2386 ptable = flash_get_ptable();
2387 if (ptable == NULL) {
2388 fastboot_fail("partition table doesn't exist");
2389 return;
2390 }
2391
2392 ptn = ptable_find(ptable, arg);
2393 if (ptn == NULL) {
2394 fastboot_fail("unknown partition name");
2395 return;
2396 }
2397
2398 if (flash_erase(ptn)) {
2399 fastboot_fail("failed to erase partition");
2400 return;
2401 }
2402 fastboot_okay("");
2403}
2404
Bikas Gurungd48bd242010-09-04 19:54:32 -07002405
2406void cmd_erase_mmc(const char *arg, void *data, unsigned sz)
2407{
2408 unsigned long long ptn = 0;
Oliver Wangcee448d2013-10-22 18:40:13 +08002409 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07002410 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002411 uint8_t lun = 0;
Bikas Gurungd48bd242010-09-04 19:54:32 -07002412
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002413#if VERIFIED_BOOT
2414 if(!strcmp(arg, KEYSTORE_PTN_NAME))
2415 {
2416 if(!device.is_unlocked)
2417 {
2418 fastboot_fail("unlock device to erase keystore");
2419 return;
2420 }
2421 }
2422#endif
2423
Kinson Chikf1a43512011-07-14 11:28:39 -07002424 index = partition_get_index(arg);
2425 ptn = partition_get_offset(index);
Oliver Wangcee448d2013-10-22 18:40:13 +08002426 size = partition_get_size(index);
Neeti Desaica8c9602011-10-06 11:40:00 -07002427
Kinson Chikf1a43512011-07-14 11:28:39 -07002428 if(ptn == 0) {
Neeti Desaica8c9602011-10-06 11:40:00 -07002429 fastboot_fail("Partition table doesn't exist\n");
Bikas Gurungd48bd242010-09-04 19:54:32 -07002430 return;
2431 }
Kun Liang2f1601a2013-08-12 16:29:54 +08002432
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002433 lun = partition_get_lun(index);
2434 mmc_set_lun(lun);
2435
Sridhar Parasuramd7957122015-02-27 11:33:40 -08002436 if (platform_boot_dev_isemmc())
2437 {
2438 if (mmc_erase_card(ptn, size)) {
2439 fastboot_fail("failed to erase partition\n");
2440 return;
2441 }
2442 } else {
2443 BUF_DMA_ALIGN(out, DEFAULT_ERASE_SIZE);
2444 size = partition_get_size(index);
2445 if (size > DEFAULT_ERASE_SIZE)
2446 size = DEFAULT_ERASE_SIZE;
Kun Liang2f1601a2013-08-12 16:29:54 +08002447
Sridhar Parasuramd7957122015-02-27 11:33:40 -08002448 /* Simple inefficient version of erase. Just writing
2449 0 in first several blocks */
2450 if (mmc_write(ptn , size, (unsigned int *)out)) {
2451 fastboot_fail("failed to erase partition");
2452 return;
2453 }
Bikas Gurungd48bd242010-09-04 19:54:32 -07002454 }
Sridhar Parasuram32b30662015-07-10 13:33:22 -07002455#if VERIFIED_BOOT
Parth Dixitbc9b6492015-10-18 00:41:38 +05302456#if !VBOOT_MOTA
Sridhar Parasuram32b30662015-07-10 13:33:22 -07002457 if(!(strncmp(arg, "userdata", 8)))
2458 if(send_delete_keys_to_tz())
2459 ASSERT(0);
2460#endif
Parth Dixitbc9b6492015-10-18 00:41:38 +05302461#endif
Bikas Gurungd48bd242010-09-04 19:54:32 -07002462 fastboot_okay("");
2463}
2464
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002465void cmd_erase(const char *arg, void *data, unsigned sz)
2466{
Channagoud Kadabice3cf422015-04-17 16:02:30 -07002467#if VERIFIED_BOOT
Channagoud Kadabi35297672015-06-13 11:09:49 -07002468 if (target_build_variant_user())
Channagoud Kadabice3cf422015-04-17 16:02:30 -07002469 {
Sridhar Parasuramc32d07d2015-07-12 10:57:48 -07002470 if(!device.is_unlocked)
Channagoud Kadabice3cf422015-04-17 16:02:30 -07002471 {
Channagoud Kadabi35297672015-06-13 11:09:49 -07002472 fastboot_fail("device is locked. Cannot erase");
Channagoud Kadabice3cf422015-04-17 16:02:30 -07002473 return;
2474 }
2475 }
2476#endif
2477
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002478 if(target_is_emmc_boot())
2479 cmd_erase_mmc(arg, data, sz);
2480 else
2481 cmd_erase_nand(arg, data, sz);
2482}
Bikas Gurungd48bd242010-09-04 19:54:32 -07002483
Channagoud Kadabiad259832015-05-29 11:14:17 -07002484static uint32_t aboot_get_secret_key()
2485{
2486 /* 0 is invalid secret key, update this implementation to return
2487 * device specific unique secret key
2488 */
2489 return 0;
2490}
2491
Ajay Dudani5c761132011-04-07 20:19:04 -07002492void cmd_flash_mmc_img(const char *arg, void *data, unsigned sz)
Shashank Mittal23b8f422010-04-16 19:27:21 -07002493{
2494 unsigned long long ptn = 0;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07002495 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07002496 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002497 char *token = NULL;
2498 char *pname = NULL;
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07002499 char *sp;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002500 uint8_t lun = 0;
2501 bool lun_set = false;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07002502
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07002503 token = strtok_r((char *)arg, ":", &sp);
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002504 pname = token;
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07002505 token = strtok_r(NULL, ":", &sp);
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002506 if(token)
2507 {
2508 lun = atoi(token);
2509 mmc_set_lun(lun);
2510 lun_set = true;
2511 }
2512
Mao Jinlong226f33a2014-07-04 17:24:10 +08002513 if (pname)
Greg Grisco6e754772011-06-23 12:19:39 -07002514 {
Channagoud Kadabiad259832015-05-29 11:14:17 -07002515 if (!strncmp(pname, "frp-unlock", strlen("frp-unlock")))
2516 {
2517 if (!aboot_frp_unlock(pname, data, sz))
2518 {
2519 fastboot_info("FRP unlock successful");
2520 fastboot_okay("");
2521 }
2522 else
2523 fastboot_fail("Secret key is invalid, please update the bootloader with secret key");
2524
2525 return;
2526 }
2527
Mao Jinlong226f33a2014-07-04 17:24:10 +08002528 if (!strcmp(pname, "partition"))
2529 {
2530 dprintf(INFO, "Attempt to write partition image.\n");
2531 if (write_partition(sz, (unsigned char *) data)) {
2532 fastboot_fail("failed to write partition");
Greg Grisco6e754772011-06-23 12:19:39 -07002533 return;
2534 }
2535 }
Mao Jinlong226f33a2014-07-04 17:24:10 +08002536 else
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002537 {
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002538#if VERIFIED_BOOT
2539 if(!strcmp(pname, KEYSTORE_PTN_NAME))
2540 {
2541 if(!device.is_unlocked)
2542 {
2543 fastboot_fail("unlock device to flash keystore");
2544 return;
2545 }
Gaurav Nebhwanic7313cc2015-12-15 22:25:04 +05302546 if(!boot_verify_validate_keystore((unsigned char *)data,sz))
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002547 {
2548 fastboot_fail("image is not a keystore file");
2549 return;
2550 }
2551 }
2552#endif
Mao Jinlong226f33a2014-07-04 17:24:10 +08002553 index = partition_get_index(pname);
2554 ptn = partition_get_offset(index);
2555 if(ptn == 0) {
2556 fastboot_fail("partition table doesn't exist");
2557 return;
2558 }
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002559
Mao Jinlong226f33a2014-07-04 17:24:10 +08002560 if (!strcmp(pname, "boot") || !strcmp(pname, "recovery")) {
2561 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
2562 fastboot_fail("image is not a boot image");
2563 return;
2564 }
2565 }
2566
2567 if(!lun_set)
2568 {
2569 lun = partition_get_lun(index);
2570 mmc_set_lun(lun);
2571 }
2572
2573 size = partition_get_size(index);
2574 if (ROUND_TO_PAGE(sz,511) > size) {
2575 fastboot_fail("size too large");
2576 return;
2577 }
2578 else if (mmc_write(ptn , sz, (unsigned int *)data)) {
2579 fastboot_fail("flash write failure");
2580 return;
2581 }
Greg Grisco6e754772011-06-23 12:19:39 -07002582 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07002583 }
2584 fastboot_okay("");
2585 return;
2586}
2587
Ajay Dudanide984792015-03-02 09:57:41 -08002588void cmd_flash_meta_img(const char *arg, void *data, unsigned sz)
2589{
2590 int i, images;
2591 meta_header_t *meta_header;
2592 img_header_entry_t *img_header_entry;
2593
2594 meta_header = (meta_header_t*) data;
2595 img_header_entry = (img_header_entry_t*) (data+sizeof(meta_header_t));
2596
2597 images = meta_header->img_hdr_sz / sizeof(img_header_entry_t);
2598
2599 for (i=0; i<images; i++) {
2600
2601 if((img_header_entry[i].ptn_name == NULL) ||
2602 (img_header_entry[i].start_offset == 0) ||
2603 (img_header_entry[i].size == 0))
2604 break;
2605
2606 cmd_flash_mmc_img(img_header_entry[i].ptn_name,
2607 (void *) data + img_header_entry[i].start_offset,
2608 img_header_entry[i].size);
2609 }
2610
Channagoud Kadabiaafbbfc2015-03-04 17:52:56 -08002611 if (!strncmp(arg, "bootloader", strlen("bootloader")))
2612 {
2613 strlcpy(device.bootloader_version, TARGET(BOARD), MAX_VERSION_LEN);
2614 strlcat(device.bootloader_version, "-", MAX_VERSION_LEN);
2615 strlcat(device.bootloader_version, meta_header->img_version, MAX_VERSION_LEN);
2616 }
2617 else
2618 {
2619 strlcpy(device.radio_version, TARGET(BOARD), MAX_VERSION_LEN);
2620 strlcat(device.radio_version, "-", MAX_VERSION_LEN);
2621 strlcat(device.radio_version, meta_header->img_version, MAX_VERSION_LEN);
2622 }
2623
2624 write_device_info(&device);
Ajay Dudanide984792015-03-02 09:57:41 -08002625 fastboot_okay("");
2626 return;
2627}
2628
Ajay Dudani5c761132011-04-07 20:19:04 -07002629void cmd_flash_mmc_sparse_img(const char *arg, void *data, unsigned sz)
2630{
2631 unsigned int chunk;
wufeng.jiang813dc352015-06-02 23:02:46 -04002632 uint64_t chunk_data_sz;
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002633 uint32_t *fill_buf = NULL;
2634 uint32_t fill_val;
Ajay Dudani5c761132011-04-07 20:19:04 -07002635 sparse_header_t *sparse_header;
2636 chunk_header_t *chunk_header;
Ajay Dudaniab18f022011-05-12 14:39:22 -07002637 uint32_t total_blocks = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07002638 unsigned long long ptn = 0;
Channagoud Kadabi65b91002011-10-11 17:34:33 +05302639 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07002640 int index = INVALID_PTN;
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05302641 uint32_t i;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002642 uint8_t lun = 0;
vijay kumar800255e2015-04-24 20:26:19 +05302643 /*End of the sparse image address*/
2644 uint32_t data_end = (uint32_t)data + sz;
Ajay Dudani5c761132011-04-07 20:19:04 -07002645
Kinson Chikf1a43512011-07-14 11:28:39 -07002646 index = partition_get_index(arg);
2647 ptn = partition_get_offset(index);
2648 if(ptn == 0) {
Ajay Dudani5c761132011-04-07 20:19:04 -07002649 fastboot_fail("partition table doesn't exist");
2650 return;
2651 }
2652
Channagoud Kadabi65b91002011-10-11 17:34:33 +05302653 size = partition_get_size(index);
Channagoud Kadabi65b91002011-10-11 17:34:33 +05302654
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002655 lun = partition_get_lun(index);
2656 mmc_set_lun(lun);
2657
vijay kumar800255e2015-04-24 20:26:19 +05302658 if (sz < sizeof(sparse_header_t)) {
2659 fastboot_fail("size too low");
2660 return;
2661 }
2662
Ajay Dudani5c761132011-04-07 20:19:04 -07002663 /* Read and skip over sparse image header */
2664 sparse_header = (sparse_header_t *) data;
vijay kumar800255e2015-04-24 20:26:19 +05302665
vijay kumar1321f342015-03-27 12:13:42 +05302666 if (((uint64_t)sparse_header->total_blks * (uint64_t)sparse_header->blk_sz) > size) {
Ajay Dudani876b3282012-12-21 14:12:17 -08002667 fastboot_fail("size too large");
2668 return;
2669 }
2670
vijay kumarde4fcf62015-04-23 13:05:49 +05302671 data += sizeof(sparse_header_t);
vijay kumar800255e2015-04-24 20:26:19 +05302672
2673 if (data_end < (uint32_t)data) {
2674 fastboot_fail("buffer overreads occured due to invalid sparse header");
2675 return;
2676 }
2677
vijay kumarde4fcf62015-04-23 13:05:49 +05302678 if(sparse_header->file_hdr_sz != sizeof(sparse_header_t))
Ajay Dudani5c761132011-04-07 20:19:04 -07002679 {
vijay kumarde4fcf62015-04-23 13:05:49 +05302680 fastboot_fail("sparse header size mismatch");
2681 return;
Ajay Dudani5c761132011-04-07 20:19:04 -07002682 }
2683
Ajay Dudanib06c05f2011-05-12 14:46:10 -07002684 dprintf (SPEW, "=== Sparse Image Header ===\n");
2685 dprintf (SPEW, "magic: 0x%x\n", sparse_header->magic);
2686 dprintf (SPEW, "major_version: 0x%x\n", sparse_header->major_version);
2687 dprintf (SPEW, "minor_version: 0x%x\n", sparse_header->minor_version);
2688 dprintf (SPEW, "file_hdr_sz: %d\n", sparse_header->file_hdr_sz);
2689 dprintf (SPEW, "chunk_hdr_sz: %d\n", sparse_header->chunk_hdr_sz);
2690 dprintf (SPEW, "blk_sz: %d\n", sparse_header->blk_sz);
2691 dprintf (SPEW, "total_blks: %d\n", sparse_header->total_blks);
2692 dprintf (SPEW, "total_chunks: %d\n", sparse_header->total_chunks);
Ajay Dudani5c761132011-04-07 20:19:04 -07002693
2694 /* Start processing chunks */
2695 for (chunk=0; chunk<sparse_header->total_chunks; chunk++)
2696 {
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302697 /* Make sure the total image size does not exceed the partition size */
2698 if(((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz) >= size) {
2699 fastboot_fail("size too large");
2700 return;
2701 }
Ajay Dudani5c761132011-04-07 20:19:04 -07002702 /* Read and skip over chunk header */
2703 chunk_header = (chunk_header_t *) data;
2704 data += sizeof(chunk_header_t);
2705
vijay kumar800255e2015-04-24 20:26:19 +05302706 if (data_end < (uint32_t)data) {
2707 fastboot_fail("buffer overreads occured due to invalid sparse header");
2708 return;
2709 }
2710
Ajay Dudani5c761132011-04-07 20:19:04 -07002711 dprintf (SPEW, "=== Chunk Header ===\n");
2712 dprintf (SPEW, "chunk_type: 0x%x\n", chunk_header->chunk_type);
2713 dprintf (SPEW, "chunk_data_sz: 0x%x\n", chunk_header->chunk_sz);
2714 dprintf (SPEW, "total_size: 0x%x\n", chunk_header->total_sz);
2715
vijay kumar800255e2015-04-24 20:26:19 +05302716 if(sparse_header->chunk_hdr_sz != sizeof(chunk_header_t))
Ajay Dudani5c761132011-04-07 20:19:04 -07002717 {
vijay kumar800255e2015-04-24 20:26:19 +05302718 fastboot_fail("chunk header size mismatch");
2719 return;
Ajay Dudani5c761132011-04-07 20:19:04 -07002720 }
2721
wufeng.jiang813dc352015-06-02 23:02:46 -04002722 if (!sparse_header->blk_sz ){
2723 fastboot_fail("Invalid block size\n");
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302724 return;
2725 }
2726
wufeng.jiang813dc352015-06-02 23:02:46 -04002727 chunk_data_sz = (uint64_t)sparse_header->blk_sz * chunk_header->chunk_sz;
2728
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302729 /* Make sure that the chunk size calculated from sparse image does not
2730 * exceed partition size
2731 */
2732 if ((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz + chunk_data_sz > size)
2733 {
2734 fastboot_fail("Chunk data size exceeds partition size");
2735 return;
2736 }
2737
Ajay Dudani5c761132011-04-07 20:19:04 -07002738 switch (chunk_header->chunk_type)
2739 {
2740 case CHUNK_TYPE_RAW:
wufeng.jiang813dc352015-06-02 23:02:46 -04002741 if((uint64_t)chunk_header->total_sz != ((uint64_t)sparse_header->chunk_hdr_sz +
Ajay Dudani5c761132011-04-07 20:19:04 -07002742 chunk_data_sz))
2743 {
2744 fastboot_fail("Bogus chunk size for chunk type Raw");
2745 return;
2746 }
2747
vijay kumar800255e2015-04-24 20:26:19 +05302748 if (data_end < (uint32_t)data + chunk_data_sz) {
2749 fastboot_fail("buffer overreads occured due to invalid sparse header");
2750 return;
2751 }
2752
wufeng.jiang813dc352015-06-02 23:02:46 -04002753 /* chunk_header->total_sz is uint32,So chunk_data_sz is now less than 2^32
2754 otherwise it will return in the line above
2755 */
Ajay Dudaniab18f022011-05-12 14:39:22 -07002756 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
wufeng.jiang813dc352015-06-02 23:02:46 -04002757 (uint32_t)chunk_data_sz,
Ajay Dudaniab18f022011-05-12 14:39:22 -07002758 (unsigned int*)data))
Ajay Dudani5c761132011-04-07 20:19:04 -07002759 {
2760 fastboot_fail("flash write failure");
2761 return;
2762 }
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302763 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
2764 fastboot_fail("Bogus size for RAW chunk type");
2765 return;
2766 }
Ajay Dudani5c761132011-04-07 20:19:04 -07002767 total_blocks += chunk_header->chunk_sz;
wufeng.jiang813dc352015-06-02 23:02:46 -04002768 data += (uint32_t)chunk_data_sz;
Ajay Dudani5c761132011-04-07 20:19:04 -07002769 break;
2770
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002771 case CHUNK_TYPE_FILL:
2772 if(chunk_header->total_sz != (sparse_header->chunk_hdr_sz +
2773 sizeof(uint32_t)))
2774 {
2775 fastboot_fail("Bogus chunk size for chunk type FILL");
2776 return;
2777 }
2778
2779 fill_buf = (uint32_t *)memalign(CACHE_LINE, ROUNDUP(sparse_header->blk_sz, CACHE_LINE));
2780 if (!fill_buf)
2781 {
2782 fastboot_fail("Malloc failed for: CHUNK_TYPE_FILL");
2783 return;
2784 }
2785
vijay kumar800255e2015-04-24 20:26:19 +05302786 if (data_end < (uint32_t)data + sizeof(uint32_t)) {
2787 fastboot_fail("buffer overreads occured due to invalid sparse header");
2788 return;
2789 }
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002790 fill_val = *(uint32_t *)data;
2791 data = (char *) data + sizeof(uint32_t);
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002792
2793 for (i = 0; i < (sparse_header->blk_sz / sizeof(fill_val)); i++)
2794 {
2795 fill_buf[i] = fill_val;
2796 }
2797
wufeng.jiang813dc352015-06-02 23:02:46 -04002798 for (i = 0; i < chunk_header->chunk_sz; i++)
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002799 {
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302800 /* Make sure that the data written to partition does not exceed partition size */
2801 if ((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz + sparse_header->blk_sz > size)
2802 {
2803 fastboot_fail("Chunk data size for fill type exceeds partition size");
2804 return;
2805 }
2806
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002807 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
2808 sparse_header->blk_sz,
2809 fill_buf))
2810 {
2811 fastboot_fail("flash write failure");
2812 free(fill_buf);
2813 return;
2814 }
2815
2816 total_blocks++;
2817 }
2818
2819 free(fill_buf);
2820 break;
2821
Ajay Dudani5c761132011-04-07 20:19:04 -07002822 case CHUNK_TYPE_DONT_CARE:
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302823 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
2824 fastboot_fail("bogus size for chunk DONT CARE type");
2825 return;
2826 }
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07002827 total_blocks += chunk_header->chunk_sz;
2828 break;
2829
Ajay Dudani5c761132011-04-07 20:19:04 -07002830 case CHUNK_TYPE_CRC:
2831 if(chunk_header->total_sz != sparse_header->chunk_hdr_sz)
2832 {
wufeng.jiang813dc352015-06-02 23:02:46 -04002833 fastboot_fail("Bogus chunk size for chunk type CRC");
Ajay Dudani5c761132011-04-07 20:19:04 -07002834 return;
2835 }
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302836 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
2837 fastboot_fail("bogus size for chunk CRC type");
2838 return;
2839 }
Ajay Dudani5c761132011-04-07 20:19:04 -07002840 total_blocks += chunk_header->chunk_sz;
vijay kumar800255e2015-04-24 20:26:19 +05302841 if ((uint32_t)data > UINT_MAX - chunk_data_sz) {
2842 fastboot_fail("integer overflow occured");
2843 return;
2844 }
wufeng.jiang813dc352015-06-02 23:02:46 -04002845 data += (uint32_t)chunk_data_sz;
vijay kumar800255e2015-04-24 20:26:19 +05302846 if (data_end < (uint32_t)data) {
2847 fastboot_fail("buffer overreads occured due to invalid sparse header");
2848 return;
2849 }
Ajay Dudani5c761132011-04-07 20:19:04 -07002850 break;
2851
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07002852 default:
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002853 dprintf(CRITICAL, "Unkown chunk type: %x\n",chunk_header->chunk_type);
Ajay Dudani5c761132011-04-07 20:19:04 -07002854 fastboot_fail("Unknown chunk type");
2855 return;
2856 }
2857 }
2858
Ajay Dudani0c6927b2011-05-18 11:12:16 -07002859 dprintf(INFO, "Wrote %d blocks, expected to write %d blocks\n",
2860 total_blocks, sparse_header->total_blks);
2861
2862 if(total_blocks != sparse_header->total_blks)
2863 {
2864 fastboot_fail("sparse image write failure");
2865 }
Ajay Dudani5c761132011-04-07 20:19:04 -07002866
2867 fastboot_okay("");
2868 return;
2869}
2870
2871void cmd_flash_mmc(const char *arg, void *data, unsigned sz)
2872{
2873 sparse_header_t *sparse_header;
Ajay Dudanide984792015-03-02 09:57:41 -08002874 meta_header_t *meta_header;
Ajay Dudani5c761132011-04-07 20:19:04 -07002875
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07002876#ifdef SSD_ENABLE
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -08002877 /* 8 Byte Magic + 2048 Byte xml + Encrypted Data */
2878 unsigned int *magic_number = (unsigned int *) data;
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002879 int ret=0;
2880 uint32 major_version=0;
2881 uint32 minor_version=0;
2882
2883 ret = scm_svc_version(&major_version,&minor_version);
2884 if(!ret)
2885 {
2886 if(major_version >= 2)
2887 {
Amir Samuelovbb65ce02013-05-05 12:20:18 +03002888 if( !strcmp(arg, "ssd") || !strcmp(arg, "tqs") )
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002889 {
2890 ret = encrypt_scm((uint32 **) &data, &sz);
2891 if (ret != 0) {
2892 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
2893 return;
2894 }
2895
Amir Samuelovbb65ce02013-05-05 12:20:18 +03002896 /* Protect only for SSD */
2897 if (!strcmp(arg, "ssd")) {
2898 ret = scm_protect_keystore((uint32 *) data, sz);
2899 if (ret != 0) {
2900 dprintf(CRITICAL, "ERROR: scm_protect_keystore Failed\n");
2901 return;
2902 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002903 }
2904 }
2905 else
2906 {
2907 ret = decrypt_scm_v2((uint32 **) &data, &sz);
2908 if(ret != 0)
2909 {
2910 dprintf(CRITICAL,"ERROR: Decryption Failure\n");
2911 return;
2912 }
2913 }
2914 }
2915 else
2916 {
2917 if (magic_number[0] == DECRYPT_MAGIC_0 &&
2918 magic_number[1] == DECRYPT_MAGIC_1)
2919 {
2920 ret = decrypt_scm((uint32 **) &data, &sz);
2921 if (ret != 0) {
2922 dprintf(CRITICAL, "ERROR: Invalid secure image\n");
2923 return;
2924 }
2925 }
2926 else if (magic_number[0] == ENCRYPT_MAGIC_0 &&
2927 magic_number[1] == ENCRYPT_MAGIC_1)
2928 {
2929 ret = encrypt_scm((uint32 **) &data, &sz);
2930 if (ret != 0) {
2931 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
2932 return;
2933 }
2934 }
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07002935 }
2936 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002937 else
Neeti Desai127b9e02012-03-20 16:11:23 -07002938 {
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002939 dprintf(CRITICAL,"INVALID SVC Version\n");
2940 return;
Neeti Desai127b9e02012-03-20 16:11:23 -07002941 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08002942#endif /* SSD_ENABLE */
Neeti Desai127b9e02012-03-20 16:11:23 -07002943
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002944#if VERIFIED_BOOT
Channagoud Kadabi35297672015-06-13 11:09:49 -07002945 if (target_build_variant_user())
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002946 {
lijuang511a2b52015-08-14 20:50:51 +08002947 /* if device is locked:
2948 * common partition will not allow to be flashed
2949 * critical partition will allow to flash image.
2950 */
2951 if(!device.is_unlocked && !critical_flash_allowed(arg)) {
2952 fastboot_fail("Partition flashing is not allowed");
2953 return;
2954 }
Parth Dixitddbc7352015-10-18 03:13:31 +05302955#if !VBOOT_MOTA
lijuang511a2b52015-08-14 20:50:51 +08002956 /* if device critical is locked:
2957 * common partition will allow to be flashed
2958 * critical partition will not allow to flash image.
2959 */
2960 if(!device.is_unlock_critical && critical_flash_allowed(arg)) {
2961 fastboot_fail("Critical partition flashing is not allowed");
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002962 return;
2963 }
Parth Dixitddbc7352015-10-18 03:13:31 +05302964#endif
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002965 }
2966#endif
2967
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07002968 sparse_header = (sparse_header_t *) data;
Ajay Dudanide984792015-03-02 09:57:41 -08002969 meta_header = (meta_header_t *) data;
2970 if (sparse_header->magic == SPARSE_HEADER_MAGIC)
Ajay Dudani5c761132011-04-07 20:19:04 -07002971 cmd_flash_mmc_sparse_img(arg, data, sz);
Ajay Dudanide984792015-03-02 09:57:41 -08002972 else if (meta_header->magic == META_HEADER_MAGIC)
2973 cmd_flash_meta_img(arg, data, sz);
2974 else
2975 cmd_flash_mmc_img(arg, data, sz);
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07002976
2977#if VERIFIED_BOOT
Parth Dixitddbc7352015-10-18 03:13:31 +05302978#if !VBOOT_MOTA
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07002979 if((!strncmp(arg, "system", 6)) && !device.verity_mode)
2980 {
2981 // reset dm_verity mode to enforcing
2982 device.verity_mode = 1;
2983 write_device_info(&device);
2984 }
2985#endif
Parth Dixitddbc7352015-10-18 03:13:31 +05302986#endif
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07002987
Ajay Dudani5c761132011-04-07 20:19:04 -07002988 return;
2989}
2990
Tanya Brokhman84eaaf62015-02-24 21:57:06 +02002991void cmd_updatevol(const char *vol_name, void *data, unsigned sz)
2992{
2993 struct ptentry *sys_ptn;
2994 struct ptable *ptable;
2995
2996 ptable = flash_get_ptable();
2997 if (ptable == NULL) {
2998 fastboot_fail("partition table doesn't exist");
2999 return;
3000 }
3001
3002 sys_ptn = ptable_find(ptable, "system");
3003 if (sys_ptn == NULL) {
3004 fastboot_fail("system partition not found");
3005 return;
3006 }
3007
3008 sz = ROUND_TO_PAGE(sz, page_mask);
3009 if (update_ubi_vol(sys_ptn, vol_name, data, sz))
3010 fastboot_fail("update_ubi_vol failed");
3011 else
3012 fastboot_okay("");
3013}
3014
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003015void cmd_flash_nand(const char *arg, void *data, unsigned sz)
Dima Zavin214cc642009-01-26 11:16:21 -08003016{
3017 struct ptentry *ptn;
3018 struct ptable *ptable;
3019 unsigned extra = 0;
3020
3021 ptable = flash_get_ptable();
3022 if (ptable == NULL) {
3023 fastboot_fail("partition table doesn't exist");
3024 return;
3025 }
3026
3027 ptn = ptable_find(ptable, arg);
3028 if (ptn == NULL) {
Tanya Brokhman84eaaf62015-02-24 21:57:06 +02003029 dprintf(INFO, "unknown partition name (%s). Trying updatevol\n",
3030 arg);
3031 cmd_updatevol(arg, data, sz);
Dima Zavin214cc642009-01-26 11:16:21 -08003032 return;
3033 }
3034
3035 if (!strcmp(ptn->name, "boot") || !strcmp(ptn->name, "recovery")) {
3036 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
3037 fastboot_fail("image is not a boot image");
3038 return;
3039 }
3040 }
3041
Amol Jadi5c61a952012-05-04 17:05:35 -07003042 if (!strcmp(ptn->name, "system")
Deepa Dinamani13e32c42012-03-12 14:34:17 -07003043 || !strcmp(ptn->name, "userdata")
3044 || !strcmp(ptn->name, "persist")
Sundarajan Srinivasanb063a852013-11-19 14:02:27 -08003045 || !strcmp(ptn->name, "recoveryfs")
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -08003046 || !strcmp(ptn->name, "modem"))
Tanya Brokhman1c94f1a2015-02-15 09:05:03 +02003047 extra = 1;
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -08003048 else
Shashank Mittaldcc2e352009-11-19 19:11:16 -08003049 sz = ROUND_TO_PAGE(sz, page_mask);
Dima Zavin214cc642009-01-26 11:16:21 -08003050
3051 dprintf(INFO, "writing %d bytes to '%s'\n", sz, ptn->name);
Tanya Brokhman1c94f1a2015-02-15 09:05:03 +02003052 if (!memcmp((void *)data, UBI_MAGIC, UBI_MAGIC_SIZE)) {
3053 if (flash_ubi_img(ptn, data, sz)) {
3054 fastboot_fail("flash write failure");
3055 return;
3056 }
3057 } else {
3058 if (flash_write(ptn, extra, data, sz)) {
3059 fastboot_fail("flash write failure");
3060 return;
3061 }
Dima Zavin214cc642009-01-26 11:16:21 -08003062 }
3063 dprintf(INFO, "partition '%s' updated\n", ptn->name);
3064 fastboot_okay("");
3065}
3066
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003067void cmd_flash(const char *arg, void *data, unsigned sz)
3068{
3069 if(target_is_emmc_boot())
3070 cmd_flash_mmc(arg, data, sz);
3071 else
3072 cmd_flash_nand(arg, data, sz);
3073}
3074
Dima Zavin214cc642009-01-26 11:16:21 -08003075void cmd_continue(const char *arg, void *data, unsigned sz)
3076{
3077 fastboot_okay("");
Amol Jadi7c4316c2013-10-07 14:19:26 -07003078 fastboot_stop();
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003079
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003080 if (target_is_emmc_boot())
3081 {
lijuanga40d6302015-07-20 20:10:13 +08003082#if FBCON_DISPLAY_MSG
lijuangde34d502016-02-26 16:04:50 +08003083 /* Exit keys' detection thread firstly */
3084 exit_menu_keys_detection();
lijuanga40d6302015-07-20 20:10:13 +08003085#endif
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003086 boot_linux_from_mmc();
3087 }
3088 else
3089 {
3090 boot_linux_from_flash();
3091 }
Dima Zavin214cc642009-01-26 11:16:21 -08003092}
3093
Chandan Uddaraju94183c02010-01-15 15:13:59 -08003094void cmd_reboot(const char *arg, void *data, unsigned sz)
3095{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003096 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08003097 fastboot_okay("");
3098 reboot_device(0);
3099}
3100
3101void cmd_reboot_bootloader(const char *arg, void *data, unsigned sz)
3102{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003103 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08003104 fastboot_okay("");
3105 reboot_device(FASTBOOT_MODE);
3106}
3107
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003108void cmd_oem_enable_charger_screen(const char *arg, void *data, unsigned size)
3109{
3110 dprintf(INFO, "Enabling charger screen check\n");
3111 device.charger_screen_enabled = 1;
3112 write_device_info(&device);
3113 fastboot_okay("");
3114}
3115
3116void cmd_oem_disable_charger_screen(const char *arg, void *data, unsigned size)
3117{
3118 dprintf(INFO, "Disabling charger screen check\n");
3119 device.charger_screen_enabled = 0;
3120 write_device_info(&device);
3121 fastboot_okay("");
3122}
3123
lijuanga25d8bb2015-09-16 13:11:52 +08003124void cmd_oem_off_mode_charger(const char *arg, void *data, unsigned size)
3125{
3126 char *p = NULL;
3127 const char *delim = " \t\n\r";
Parth Dixit407f15b2016-01-07 14:47:37 +05303128 char *sp;
lijuanga25d8bb2015-09-16 13:11:52 +08003129
3130 if (arg) {
Parth Dixit407f15b2016-01-07 14:47:37 +05303131 p = strtok_r((char *)arg, delim, &sp);
lijuanga25d8bb2015-09-16 13:11:52 +08003132 if (p) {
3133 if (!strncmp(p, "0", 1)) {
3134 device.charger_screen_enabled = 0;
3135 } else if (!strncmp(p, "1", 1)) {
3136 device.charger_screen_enabled = 1;
3137 }
3138 }
3139 }
3140
3141 /* update charger_screen_enabled value for getvar
3142 * command
3143 */
3144 snprintf(charger_screen_enabled, MAX_RSP_SIZE, "%d",
3145 device.charger_screen_enabled);
3146
3147 write_device_info(&device);
3148 fastboot_okay("");
3149}
3150
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05303151void cmd_oem_select_display_panel(const char *arg, void *data, unsigned size)
3152{
3153 dprintf(INFO, "Selecting display panel %s\n", arg);
3154 if (arg)
3155 strlcpy(device.display_panel, arg,
3156 sizeof(device.display_panel));
3157 write_device_info(&device);
3158 fastboot_okay("");
3159}
3160
Shashank Mittal162244e2011-08-08 19:01:25 -07003161void cmd_oem_unlock(const char *arg, void *data, unsigned sz)
3162{
lijuang4ece1e72015-08-14 21:02:36 +08003163 set_device_unlock(UNLOCK, TRUE);
vijay kumarc65876c2015-04-24 13:29:16 +05303164}
3165
3166void cmd_oem_unlock_go(const char *arg, void *data, unsigned sz)
3167{
lijuang4ece1e72015-08-14 21:02:36 +08003168 if(!device.is_unlocked) {
vijay kumarc65876c2015-04-24 13:29:16 +05303169 if(!is_allow_unlock) {
3170 fastboot_fail("oem unlock is not allowed");
3171 return;
3172 }
3173
lijuang4ece1e72015-08-14 21:02:36 +08003174 set_device_unlock_value(UNLOCK, TRUE);
vijay kumarc65876c2015-04-24 13:29:16 +05303175
lijuang4ece1e72015-08-14 21:02:36 +08003176 /* wipe data */
vijay kumarc65876c2015-04-24 13:29:16 +05303177 struct recovery_message msg;
lijuang4ece1e72015-08-14 21:02:36 +08003178
vijay kumarc65876c2015-04-24 13:29:16 +05303179 snprintf(msg.recovery, sizeof(msg.recovery), "recovery\n--wipe_data");
3180 write_misc(0, &msg, sizeof(msg));
3181
3182 fastboot_okay("");
3183 reboot_device(RECOVERY_MODE);
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003184 }
3185 fastboot_okay("");
3186}
3187
Channagoud Kadabiad259832015-05-29 11:14:17 -07003188static int aboot_frp_unlock(char *pname, void *data, unsigned sz)
3189{
3190 int ret = 1;
3191 uint32_t secret_key;
3192 char seckey_buffer[MAX_RSP_SIZE];
3193
3194 secret_key = aboot_get_secret_key();
3195 if (secret_key)
3196 {
3197 snprintf((char *) seckey_buffer, MAX_RSP_SIZE, "%x", secret_key);
3198 if (!memcmp((void *)data, (void *)seckey_buffer, sz))
3199 {
3200 is_allow_unlock = true;
3201 write_allow_oem_unlock(is_allow_unlock);
3202 ret = 0;
3203 }
3204 }
3205 return ret;
3206}
3207
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003208void cmd_oem_lock(const char *arg, void *data, unsigned sz)
3209{
lijuang4ece1e72015-08-14 21:02:36 +08003210 set_device_unlock(UNLOCK, FALSE);
Shashank Mittal162244e2011-08-08 19:01:25 -07003211}
3212
Shashank Mittala0032282011-08-26 14:50:11 -07003213void cmd_oem_devinfo(const char *arg, void *data, unsigned sz)
3214{
lijuang511a2b52015-08-14 20:50:51 +08003215 char response[MAX_RSP_SIZE];
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003216 snprintf(response, sizeof(response), "\tDevice tampered: %s", (device.is_tampered ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07003217 fastboot_info(response);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003218 snprintf(response, sizeof(response), "\tDevice unlocked: %s", (device.is_unlocked ? "true" : "false"));
3219 fastboot_info(response);
Parth Dixitddbc7352015-10-18 03:13:31 +05303220#if !VBOOT_MOTA
lijuang511a2b52015-08-14 20:50:51 +08003221 snprintf(response, sizeof(response), "\tDevice critical unlocked: %s", (device.is_unlock_critical ? "true" : "false"));
3222 fastboot_info(response);
Parth Dixitddbc7352015-10-18 03:13:31 +05303223#endif
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003224 snprintf(response, sizeof(response), "\tCharger screen enabled: %s", (device.charger_screen_enabled ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07003225 fastboot_info(response);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05303226 snprintf(response, sizeof(response), "\tDisplay panel: %s", (device.display_panel));
3227 fastboot_info(response);
Shashank Mittala0032282011-08-26 14:50:11 -07003228 fastboot_okay("");
3229}
3230
lijuang511a2b52015-08-14 20:50:51 +08003231void cmd_flashing_get_unlock_ability(const char *arg, void *data, unsigned sz)
3232{
3233 char response[MAX_RSP_SIZE];
3234 snprintf(response, sizeof(response), "\tget_unlock_ability: %d", is_allow_unlock);
3235 fastboot_info(response);
3236 fastboot_okay("");
3237}
3238
3239void cmd_flashing_lock_critical(const char *arg, void *data, unsigned sz)
3240{
lijuang4ece1e72015-08-14 21:02:36 +08003241 set_device_unlock(UNLOCK_CRITICAL, FALSE);
lijuang511a2b52015-08-14 20:50:51 +08003242}
3243
3244void cmd_flashing_unlock_critical(const char *arg, void *data, unsigned sz)
3245{
lijuang4ece1e72015-08-14 21:02:36 +08003246 set_device_unlock(UNLOCK_CRITICAL, TRUE);
lijuang511a2b52015-08-14 20:50:51 +08003247}
3248
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07003249void cmd_preflash(const char *arg, void *data, unsigned sz)
3250{
3251 fastboot_okay("");
3252}
3253
Mao Flynn7b379f32015-04-20 00:28:30 +08003254static uint8_t logo_header[LOGO_IMG_HEADER_SIZE];
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303255
Mao Flynn7b379f32015-04-20 00:28:30 +08003256int splash_screen_check_header(logo_img_header *header)
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303257{
Mao Flynn7b379f32015-04-20 00:28:30 +08003258 if (memcmp(header->magic, LOGO_IMG_MAGIC, 8))
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303259 return -1;
Mao Flynn7b379f32015-04-20 00:28:30 +08003260 if (header->width == 0 || header->height == 0)
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303261 return -1;
3262 return 0;
3263}
3264
Mao Flynn7b379f32015-04-20 00:28:30 +08003265int splash_screen_flash()
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003266{
3267 struct ptentry *ptn;
3268 struct ptable *ptable;
Mao Flynn7b379f32015-04-20 00:28:30 +08003269 struct logo_img_header *header;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003270 struct fbcon_config *fb_display = NULL;
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08003271
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303272 ptable = flash_get_ptable();
3273 if (ptable == NULL) {
Mao Flynn7b379f32015-04-20 00:28:30 +08003274 dprintf(CRITICAL, "ERROR: Partition table not found\n");
3275 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303276 }
Mao Flynn7b379f32015-04-20 00:28:30 +08003277
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303278 ptn = ptable_find(ptable, "splash");
3279 if (ptn == NULL) {
3280 dprintf(CRITICAL, "ERROR: splash Partition not found\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003281 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303282 }
Mao Flynn7b379f32015-04-20 00:28:30 +08003283 if (flash_read(ptn, 0, (void *)logo_header, LOGO_IMG_HEADER_SIZE)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303284 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003285 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303286 }
3287
Mao Flynn7b379f32015-04-20 00:28:30 +08003288 header = (struct logo_img_header *)logo_header;
3289 if (splash_screen_check_header(header)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303290 dprintf(CRITICAL, "ERROR: Boot image header invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003291 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303292 }
3293
3294 fb_display = fbcon_display();
3295 if (fb_display) {
Mao Flynn7b379f32015-04-20 00:28:30 +08003296 if (header->type && (header->blocks != 0)) { // RLE24 compressed data
3297 uint8_t *base = (uint8_t *) fb_display->base + LOGO_IMG_OFFSET;
3298
3299 /* if the logo is full-screen size, remove "fbcon_clear()" */
3300 if ((header->width != fb_display->width)
3301 || (header->height != fb_display->height))
3302 fbcon_clear();
3303
3304 if (flash_read(ptn + LOGO_IMG_HEADER_SIZE, 0,
3305 (uint32_t *)base,
3306 (header->blocks * 512))) {
3307 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
3308 return -1;
3309 }
3310 fbcon_extract_to_screen(header, base);
3311 return 0;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003312 }
Mao Flynn7b379f32015-04-20 00:28:30 +08003313
Veera Sundaram Sankaranc33416f2015-09-28 10:25:55 -07003314 if ((header->width > fb_display->width) || (header->height > fb_display->height)) {
3315 dprintf(CRITICAL, "Logo config greater than fb config. Fall back default logo\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003316 return -1;
3317 }
3318
Vineet Bajaj99291ed2014-09-09 12:29:46 +05303319 uint8_t *base = (uint8_t *) fb_display->base;
Mao Flynn7b379f32015-04-20 00:28:30 +08003320 if (flash_read(ptn + LOGO_IMG_HEADER_SIZE, 0,
3321 (uint32_t *)base,
3322 ((((header->width * header->height * fb_display->bpp/8) + 511) >> 9) << 9))) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303323 fbcon_clear();
Vineet Bajaj99291ed2014-09-09 12:29:46 +05303324 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003325 return -1;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003326 }
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303327 }
3328
Mao Flynn7b379f32015-04-20 00:28:30 +08003329 return 0;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303330}
3331
Mao Flynn7b379f32015-04-20 00:28:30 +08003332int splash_screen_mmc()
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303333{
3334 int index = INVALID_PTN;
3335 unsigned long long ptn = 0;
3336 struct fbcon_config *fb_display = NULL;
Mao Flynn7b379f32015-04-20 00:28:30 +08003337 struct logo_img_header *header;
Mao Flynn1893a7f2015-06-03 12:03:36 +08003338 uint32_t blocksize, realsize, readsize;
3339 uint8_t *base;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303340
3341 index = partition_get_index("splash");
3342 if (index == 0) {
3343 dprintf(CRITICAL, "ERROR: splash Partition table not found\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003344 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303345 }
3346
3347 ptn = partition_get_offset(index);
3348 if (ptn == 0) {
3349 dprintf(CRITICAL, "ERROR: splash Partition invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003350 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303351 }
3352
Mao Flynn1893a7f2015-06-03 12:03:36 +08003353 mmc_set_lun(partition_get_lun(index));
3354
3355 blocksize = mmc_get_device_blocksize();
3356 if (blocksize == 0) {
3357 dprintf(CRITICAL, "ERROR:splash Partition invalid blocksize\n");
3358 return -1;
3359 }
3360
3361 fb_display = fbcon_display();
Channagoud Kadabidaf10a62015-07-22 11:51:55 -07003362 if (!fb_display)
3363 {
3364 dprintf(CRITICAL, "ERROR: fb config is not allocated\n");
3365 return -1;
3366 }
3367
Mao Flynn1893a7f2015-06-03 12:03:36 +08003368 base = (uint8_t *) fb_display->base;
3369
3370 if (mmc_read(ptn, (uint32_t *)(base + LOGO_IMG_OFFSET), blocksize)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303371 dprintf(CRITICAL, "ERROR: Cannot read splash image header\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003372 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303373 }
3374
Mao Flynn1893a7f2015-06-03 12:03:36 +08003375 header = (struct logo_img_header *)(base + LOGO_IMG_OFFSET);
Mao Flynn7b379f32015-04-20 00:28:30 +08003376 if (splash_screen_check_header(header)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303377 dprintf(CRITICAL, "ERROR: Splash image header invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003378 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303379 }
3380
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303381 if (fb_display) {
Mao Flynn1893a7f2015-06-03 12:03:36 +08003382 if (header->type && (header->blocks != 0)) { /* 1 RLE24 compressed data */
3383 base += LOGO_IMG_OFFSET;
Mao Flynn7b379f32015-04-20 00:28:30 +08003384
Mao Flynn1893a7f2015-06-03 12:03:36 +08003385 realsize = header->blocks * 512;
3386 readsize = ROUNDUP((realsize + LOGO_IMG_HEADER_SIZE), blocksize) - blocksize;
3387
3388 /* if the logo is not full-screen size, clean screen */
Mao Flynn7b379f32015-04-20 00:28:30 +08003389 if ((header->width != fb_display->width)
3390 || (header->height != fb_display->height))
3391 fbcon_clear();
3392
Mao Flynn1893a7f2015-06-03 12:03:36 +08003393 if (mmc_read(ptn + blocksize, (uint32_t *)(base + blocksize), readsize)) {
Mao Flynn7b379f32015-04-20 00:28:30 +08003394 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
3395 return -1;
3396 }
Mao Flynn7b379f32015-04-20 00:28:30 +08003397
Mao Flynn1893a7f2015-06-03 12:03:36 +08003398 fbcon_extract_to_screen(header, (base + LOGO_IMG_HEADER_SIZE));
3399 } else { /* 2 Raw BGR data */
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303400
Veera Sundaram Sankaranc33416f2015-09-28 10:25:55 -07003401 if ((header->width > fb_display->width) || (header->height > fb_display->height)) {
3402 dprintf(CRITICAL, "Logo config greater than fb config. Fall back default logo\n");
Mao Flynn1893a7f2015-06-03 12:03:36 +08003403 return -1;
3404 }
3405
3406 realsize = header->width * header->height * fb_display->bpp / 8;
3407 readsize = ROUNDUP((realsize + LOGO_IMG_HEADER_SIZE), blocksize) - blocksize;
3408
3409 if (blocksize == LOGO_IMG_HEADER_SIZE) { /* read the content directly */
3410 if (mmc_read((ptn + LOGO_IMG_HEADER_SIZE), (uint32_t *)base, readsize)) {
3411 fbcon_clear();
3412 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
3413 return -1;
3414 }
3415 } else {
3416 if (mmc_read(ptn + blocksize ,
3417 (uint32_t *)(base + LOGO_IMG_OFFSET + blocksize), readsize)) {
3418 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
3419 return -1;
3420 }
3421 memmove(base, (base + LOGO_IMG_OFFSET + LOGO_IMG_HEADER_SIZE), realsize);
3422 }
3423 }
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303424 }
3425
Mao Flynn7b379f32015-04-20 00:28:30 +08003426 return 0;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303427}
3428
Mao Flynn7b379f32015-04-20 00:28:30 +08003429int fetch_image_from_partition()
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303430{
3431 if (target_is_emmc_boot()) {
3432 return splash_screen_mmc();
3433 } else {
3434 return splash_screen_flash();
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003435 }
3436}
3437
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07003438/* Get the size from partiton name */
3439static void get_partition_size(const char *arg, char *response)
3440{
3441 uint64_t ptn = 0;
3442 uint64_t size;
3443 int index = INVALID_PTN;
3444
3445 index = partition_get_index(arg);
3446
3447 if (index == INVALID_PTN)
3448 {
3449 dprintf(CRITICAL, "Invalid partition index\n");
3450 return;
3451 }
3452
3453 ptn = partition_get_offset(index);
3454
3455 if(!ptn)
3456 {
3457 dprintf(CRITICAL, "Invalid partition name %s\n", arg);
3458 return;
3459 }
3460
3461 size = partition_get_size(index);
3462
3463 snprintf(response, MAX_RSP_SIZE, "\t 0x%llx", size);
3464 return;
3465}
3466
3467/*
3468 * Publish the partition type & size info
3469 * fastboot getvar will publish the required information.
3470 * fastboot getvar partition_size:<partition_name>: partition size in hex
3471 * fastboot getvar partition_type:<partition_name>: partition type (ext/fat)
3472 */
3473static void publish_getvar_partition_info(struct getvar_partition_info *info, uint8_t num_parts)
3474{
3475 uint8_t i;
3476
3477 for (i = 0; i < num_parts; i++) {
3478 get_partition_size(info[i].part_name, info[i].size_response);
3479
3480 if (strlcat(info[i].getvar_size, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
3481 {
3482 dprintf(CRITICAL, "partition size name truncated\n");
3483 return;
3484 }
3485 if (strlcat(info[i].getvar_type, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
3486 {
3487 dprintf(CRITICAL, "partition type name truncated\n");
3488 return;
3489 }
3490
3491 /* publish partition size & type info */
3492 fastboot_publish((const char *) info[i].getvar_size, (const char *) info[i].size_response);
3493 fastboot_publish((const char *) info[i].getvar_type, (const char *) info[i].type_response);
3494 }
3495}
3496
lijuang4ece1e72015-08-14 21:02:36 +08003497void get_product_name(unsigned char *buf)
3498{
3499 snprintf((char*)buf, MAX_RSP_SIZE, "%s", TARGET(BOARD));
3500 return;
3501}
3502
3503void get_bootloader_version(unsigned char *buf)
3504{
3505 snprintf((char*)buf, MAX_RSP_SIZE, "%s", device.bootloader_version);
3506 return;
3507}
3508
3509void get_baseband_version(unsigned char *buf)
3510{
3511 snprintf((char*)buf, MAX_RSP_SIZE, "%s", device.radio_version);
3512 return;
3513}
3514
3515bool is_device_locked()
3516{
3517 return device.is_unlocked ? false:true;
3518}
3519
Amol Jadi5edf3552013-07-23 14:15:34 -07003520/* register commands and variables for fastboot */
3521void aboot_fastboot_register_commands(void)
3522{
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003523 int i;
lijuangf16461c2015-08-03 17:09:34 +08003524 char hw_platform_buf[MAX_RSP_SIZE];
Amol Jadi5edf3552013-07-23 14:15:34 -07003525
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003526 struct fastboot_cmd_desc cmd_list[] = {
lijuang511a2b52015-08-14 20:50:51 +08003527 /* By default the enabled list is empty. */
3528 {"", NULL},
3529 /* move commands enclosed within the below ifndef to here
3530 * if they need to be enabled in user build.
3531 */
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003532#ifndef DISABLE_FASTBOOT_CMDS
lijuang511a2b52015-08-14 20:50:51 +08003533 /* Register the following commands only for non-user builds */
3534 {"flash:", cmd_flash},
3535 {"erase:", cmd_erase},
3536 {"boot", cmd_boot},
3537 {"continue", cmd_continue},
3538 {"reboot", cmd_reboot},
3539 {"reboot-bootloader", cmd_reboot_bootloader},
3540 {"oem unlock", cmd_oem_unlock},
3541 {"oem unlock-go", cmd_oem_unlock_go},
3542 {"oem lock", cmd_oem_lock},
3543 {"flashing unlock", cmd_oem_unlock},
3544 {"flashing lock", cmd_oem_lock},
3545 {"flashing lock_critical", cmd_flashing_lock_critical},
3546 {"flashing unlock_critical", cmd_flashing_unlock_critical},
3547 {"flashing get_unlock_ability", cmd_flashing_get_unlock_ability},
3548 {"oem device-info", cmd_oem_devinfo},
3549 {"preflash", cmd_preflash},
3550 {"oem enable-charger-screen", cmd_oem_enable_charger_screen},
3551 {"oem disable-charger-screen", cmd_oem_disable_charger_screen},
lijuanga25d8bb2015-09-16 13:11:52 +08003552 {"oem off-mode-charge", cmd_oem_off_mode_charger},
lijuang511a2b52015-08-14 20:50:51 +08003553 {"oem select-display-panel", cmd_oem_select_display_panel},
Channagoud Kadabi736c4962015-08-21 11:56:52 -07003554#if UNITTEST_FW_SUPPORT
Channagoud Kadabid5ddf482015-09-28 10:31:35 -07003555 {"oem run-tests", cmd_oem_runtests},
Channagoud Kadabi736c4962015-08-21 11:56:52 -07003556#endif
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003557#endif
lijuang511a2b52015-08-14 20:50:51 +08003558 };
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003559
3560 int fastboot_cmds_count = sizeof(cmd_list)/sizeof(cmd_list[0]);
3561 for (i = 1; i < fastboot_cmds_count; i++)
3562 fastboot_register(cmd_list[i].name,cmd_list[i].cb);
3563
Amol Jadi5edf3552013-07-23 14:15:34 -07003564 /* publish variables and their values */
3565 fastboot_publish("product", TARGET(BOARD));
3566 fastboot_publish("kernel", "lk");
3567 fastboot_publish("serialno", sn_buf);
3568
3569 /*
3570 * partition info is supported only for emmc partitions
3571 * Calling this for NAND prints some error messages which
3572 * is harmless but misleading. Avoid calling this for NAND
3573 * devices.
3574 */
3575 if (target_is_emmc_boot())
3576 publish_getvar_partition_info(part_info, ARRAY_SIZE(part_info));
3577
3578 /* Max download size supported */
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003579 snprintf(max_download_size, MAX_RSP_SIZE, "\t0x%x",
3580 target_get_max_flash_size());
Amol Jadi5edf3552013-07-23 14:15:34 -07003581 fastboot_publish("max-download-size", (const char *) max_download_size);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003582 /* Is the charger screen check enabled */
3583 snprintf(charger_screen_enabled, MAX_RSP_SIZE, "%d",
3584 device.charger_screen_enabled);
3585 fastboot_publish("charger-screen-enabled",
3586 (const char *) charger_screen_enabled);
lijuanga25d8bb2015-09-16 13:11:52 +08003587 fastboot_publish("off-mode-charge", (const char *) charger_screen_enabled);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05303588 snprintf(panel_display_mode, MAX_RSP_SIZE, "%s",
3589 device.display_panel);
3590 fastboot_publish("display-panel",
3591 (const char *) panel_display_mode);
Channagoud Kadabiaafbbfc2015-03-04 17:52:56 -08003592 fastboot_publish("version-bootloader", (const char *) device.bootloader_version);
3593 fastboot_publish("version-baseband", (const char *) device.radio_version);
lijuangf16461c2015-08-03 17:09:34 +08003594 fastboot_publish("secure", is_secure_boot_enable()? "yes":"no");
3595 smem_get_hw_platform_name((unsigned char *) hw_platform_buf, sizeof(hw_platform_buf));
3596 snprintf(get_variant, MAX_RSP_SIZE, "%s %s", hw_platform_buf,
3597 target_is_emmc_boot()? "eMMC":"UFS");
3598 fastboot_publish("variant", (const char *) get_variant);
lijuang65c5a822015-08-29 16:35:36 +08003599#if CHECK_BAT_VOLTAGE
lijuang102dfa92015-10-09 18:31:03 +08003600 update_battery_status();
lijuang65c5a822015-08-29 16:35:36 +08003601 fastboot_publish("battery-voltage", (const char *) battery_voltage);
lijuang102dfa92015-10-09 18:31:03 +08003602 fastboot_publish("battery-soc-ok", (const char *) battery_soc_ok);
lijuang65c5a822015-08-29 16:35:36 +08003603#endif
Amol Jadi5edf3552013-07-23 14:15:34 -07003604}
3605
Brian Swetland9c4c0752009-01-25 16:23:50 -08003606void aboot_init(const struct app_descriptor *app)
3607{
Shashank Mittal4f99a882010-02-01 13:58:50 -08003608 unsigned reboot_mode = 0;
Chandan Uddarajubedca152010-06-02 23:05:15 -07003609
lijuang39831732016-01-08 17:49:02 +08003610 /* Initialise wdog to catch early lk crashes */
3611#if WDOG_SUPPORT
3612 msm_wdog_init();
3613#endif
3614
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07003615 /* Setup page size information for nv storage */
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003616 if (target_is_emmc_boot())
3617 {
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07003618 page_size = mmc_page_size();
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003619 page_mask = page_size - 1;
3620 }
3621 else
3622 {
3623 page_size = flash_page_size();
3624 page_mask = page_size - 1;
3625 }
3626
Deepa Dinamani0e163a42013-05-24 17:08:15 -07003627 ASSERT((MEMBASE + MEMSIZE) > MEMBASE);
3628
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003629 read_device_info(&device);
vijay kumarc65876c2015-04-24 13:29:16 +05303630 read_allow_oem_unlock(&device);
Shashank Mittal162244e2011-08-08 19:01:25 -07003631
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08003632 /* Display splash screen if enabled */
3633#if DISPLAY_SPLASH_SCREEN
lijuang99c02d82015-02-13 19:04:34 +08003634#if NO_ALARM_DISPLAY
3635 if (!check_alarm_boot()) {
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08003636#endif
lijuang99c02d82015-02-13 19:04:34 +08003637 dprintf(SPEW, "Display Init: Start\n");
Channagoud Kadabi90869ce2015-04-27 11:15:14 -07003638#if ENABLE_WBC
Channagoud Kadabid801ac72015-08-26 11:21:51 -07003639 /* Wait if the display shutdown is in progress */
3640 while(pm_app_display_shutdown_in_prgs());
Channagoud Kadabi90869ce2015-04-27 11:15:14 -07003641 if (!pm_appsbl_display_init_done())
3642 target_display_init(device.display_panel);
3643 else
3644 display_image_on_screen();
3645#else
lijuang99c02d82015-02-13 19:04:34 +08003646 target_display_init(device.display_panel);
Channagoud Kadabi90869ce2015-04-27 11:15:14 -07003647#endif
lijuang99c02d82015-02-13 19:04:34 +08003648 dprintf(SPEW, "Display Init: Done\n");
3649#if NO_ALARM_DISPLAY
3650 }
3651#endif
3652#endif
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08003653
Greg Griscod6250552011-06-29 14:40:23 -07003654 target_serialno((unsigned char *) sn_buf);
Ajay Dudanib06c05f2011-05-12 14:46:10 -07003655 dprintf(SPEW,"serial number: %s\n",sn_buf);
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -08003656
Dhaval Patel223ec952013-07-18 14:49:44 -07003657 memset(display_panel_buf, '\0', MAX_PANEL_BUF_SIZE);
3658
Matthew Qindefd5562014-07-11 18:02:40 +08003659 /*
3660 * Check power off reason if user force reset,
3661 * if yes phone will do normal boot.
3662 */
3663 if (is_user_force_reset())
3664 goto normal_boot;
3665
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003666 /* Check if we should do something other than booting up */
Ameya Thakur0b9c2442013-05-10 13:22:56 -07003667 if (keys_get_state(KEY_VOLUMEUP) && keys_get_state(KEY_VOLUMEDOWN))
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07003668 {
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03003669 dprintf(ALWAYS,"dload mode key sequence detected\n");
lijuang395b5e62015-11-19 17:39:44 +08003670 reboot_device(EMERGENCY_DLOAD);
3671 dprintf(CRITICAL,"Failed to reboot into dload mode\n");
3672
Ameya Thakur0b9c2442013-05-10 13:22:56 -07003673 boot_into_fastboot = true;
3674 }
3675 if (!boot_into_fastboot)
3676 {
3677 if (keys_get_state(KEY_HOME) || keys_get_state(KEY_VOLUMEUP))
3678 boot_into_recovery = 1;
3679 if (!boot_into_recovery &&
3680 (keys_get_state(KEY_BACK) || keys_get_state(KEY_VOLUMEDOWN)))
Pavel Nedev5d91d412013-04-29 11:34:24 +03003681 boot_into_fastboot = true;
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07003682 }
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003683 #if NO_KEYPAD_DRIVER
Kinson Chik0b1c8162011-08-31 16:31:57 -07003684 if (fastboot_trigger())
Pavel Nedev5d91d412013-04-29 11:34:24 +03003685 boot_into_fastboot = true;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003686 #endif
Chandan Uddarajubedca152010-06-02 23:05:15 -07003687
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003688#if USE_PON_REBOOT_REG
3689 reboot_mode = check_hard_reboot_mode();
3690#else
Ajay Dudani77421292010-10-27 19:34:06 -07003691 reboot_mode = check_reboot_mode();
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003692#endif
3693 if (reboot_mode == RECOVERY_MODE)
3694 {
Ajay Dudani77421292010-10-27 19:34:06 -07003695 boot_into_recovery = 1;
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003696 }
3697 else if(reboot_mode == FASTBOOT_MODE)
3698 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03003699 boot_into_fastboot = true;
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003700 }
3701 else if(reboot_mode == ALARM_BOOT)
3702 {
Matthew Qind886f3c2014-01-17 16:52:01 +08003703 boot_reason_alarm = true;
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003704 }
Parth Dixit207573a2015-10-27 17:26:21 +05303705#if VERIFIED_BOOT
Parth Dixitddbc7352015-10-18 03:13:31 +05303706#if !VBOOT_MOTA
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003707 else if (reboot_mode == DM_VERITY_ENFORCING)
3708 {
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07003709 device.verity_mode = 1;
3710 write_device_info(&device);
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003711 }
3712 else if (reboot_mode == DM_VERITY_LOGGING)
3713 {
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07003714 device.verity_mode = 0;
3715 write_device_info(&device);
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003716 }
3717 else if (reboot_mode == DM_VERITY_KEYSCLEAR)
3718 {
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07003719 if(send_delete_keys_to_tz())
3720 ASSERT(0);
Ajay Dudani77421292010-10-27 19:34:06 -07003721 }
Parth Dixit207573a2015-10-27 17:26:21 +05303722#endif
Parth Dixitddbc7352015-10-18 03:13:31 +05303723#endif
Ajay Dudani77421292010-10-27 19:34:06 -07003724
Matthew Qindefd5562014-07-11 18:02:40 +08003725normal_boot:
Pavel Nedev5d91d412013-04-29 11:34:24 +03003726 if (!boot_into_fastboot)
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003727 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03003728 if (target_is_emmc_boot())
Shashank Mittala0032282011-08-26 14:50:11 -07003729 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03003730 if(emmc_recovery_init())
3731 dprintf(ALWAYS,"error in emmc_recovery_init\n");
3732 if(target_use_signed_kernel())
Shashank Mittala0032282011-08-26 14:50:11 -07003733 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03003734 if((device.is_unlocked) || (device.is_tampered))
3735 {
3736 #ifdef TZ_TAMPER_FUSE
3737 set_tamper_fuse_cmd();
3738 #endif
3739 #if USE_PCOM_SECBOOT
3740 set_tamper_flag(device.is_tampered);
3741 #endif
3742 }
Shashank Mittala0032282011-08-26 14:50:11 -07003743 }
Amit Blay6281ebc2015-01-11 14:44:08 +02003744
Pavel Nedev5d91d412013-04-29 11:34:24 +03003745 boot_linux_from_mmc();
Shashank Mittala0032282011-08-26 14:50:11 -07003746 }
Pavel Nedev5d91d412013-04-29 11:34:24 +03003747 else
3748 {
3749 recovery_init();
3750 #if USE_PCOM_SECBOOT
3751 if((device.is_unlocked) || (device.is_tampered))
3752 set_tamper_flag(device.is_tampered);
3753 #endif
3754 boot_linux_from_flash();
3755 }
3756 dprintf(CRITICAL, "ERROR: Could not do normal boot. Reverting "
3757 "to fastboot mode.\n");
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003758 }
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003759
Amol Jadi5edf3552013-07-23 14:15:34 -07003760 /* We are here means regular boot did not happen. Start fastboot. */
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07003761
Amol Jadi5edf3552013-07-23 14:15:34 -07003762 /* register aboot specific fastboot commands */
3763 aboot_fastboot_register_commands();
Amol Jadi57abe4c2011-05-24 15:47:27 -07003764
Amol Jadi5edf3552013-07-23 14:15:34 -07003765 /* dump partition table for debug info */
Kinson Chikf1a43512011-07-14 11:28:39 -07003766 partition_dump();
Amol Jadi5edf3552013-07-23 14:15:34 -07003767
3768 /* initialize and start fastboot */
3769 fastboot_init(target_get_scratch_address(), target_get_max_flash_size());
lijuang4ece1e72015-08-14 21:02:36 +08003770#if FBCON_DISPLAY_MSG
lijuang9a7d3b92015-11-30 14:41:24 +08003771 display_fastboot_menu();
lijuang4ece1e72015-08-14 21:02:36 +08003772#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08003773}
3774
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07003775uint32_t get_page_size()
3776{
3777 return page_size;
3778}
3779
Amir Samuelov57a6fa22013-06-05 16:36:43 +03003780/*
3781 * Calculated and save hash (SHA256) for non-signed boot image.
3782 *
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07003783 * @param image_addr - Boot image address
3784 * @param image_size - Size of the boot image
Amir Samuelov57a6fa22013-06-05 16:36:43 +03003785 *
3786 * @return int - 0 on success, negative value on failure.
3787 */
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07003788static int aboot_save_boot_hash_mmc(uint32_t image_addr, uint32_t image_size)
Amir Samuelov57a6fa22013-06-05 16:36:43 +03003789{
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07003790 unsigned int digest[8];
3791#if IMAGE_VERIF_ALGO_SHA1
3792 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA1;
3793#else
3794 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA256;
3795#endif
Amir Samuelov57a6fa22013-06-05 16:36:43 +03003796
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07003797 target_crypto_init_params();
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -08003798 hash_find((unsigned char *) image_addr, image_size, (unsigned char *)&digest, auth_algo);
Amir Samuelov57a6fa22013-06-05 16:36:43 +03003799
3800 save_kernel_hash_cmd(digest);
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07003801 dprintf(INFO, "aboot_save_boot_hash_mmc: imagesize_actual size %d bytes.\n", (int) image_size);
Amir Samuelov57a6fa22013-06-05 16:36:43 +03003802
3803 return 0;
3804}
3805
Brian Swetland9c4c0752009-01-25 16:23:50 -08003806APP_START(aboot)
3807 .init = aboot_init,
3808APP_END