blob: 44076312bbf1b9b8f344cca857bcb821591dd924 [file] [log] [blame]
Brian Swetland9c4c0752009-01-25 16:23:50 -08001/*
2 * Copyright (c) 2009, Google Inc.
3 * All rights reserved.
4 *
lijuang9a7d3b92015-11-30 14:41:24 +08005 * Copyright (c) 2009-2016, The Linux Foundation. All rights reserved.
Brian Swetland9c4c0752009-01-25 16:23:50 -08006 *
Chandan Uddaraju5fa471a2009-12-02 17:31:34 -08007 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070014 * * Neither the name of The Linux Foundation nor
Chandan Uddaraju5fa471a2009-12-02 17:31:34 -080015 * the names of its contributors may be used to endorse or promote
16 * products derived from this software without specific prior written
17 * permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22 * NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 *
Brian Swetland9c4c0752009-01-25 16:23:50 -080031 */
32
33#include <app.h>
34#include <debug.h>
35#include <arch/arm.h>
Brian Swetland9c4c0752009-01-25 16:23:50 -080036#include <string.h>
Channagoud Kadabi132ff552013-04-19 14:34:44 -070037#include <stdlib.h>
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -070038#include <limits.h>
Brian Swetland9c4c0752009-01-25 16:23:50 -080039#include <kernel/thread.h>
40#include <arch/ops.h>
41
Dima Zavin214cc642009-01-26 11:16:21 -080042#include <dev/flash.h>
Tanya Brokhman1c94f1a2015-02-15 09:05:03 +020043#include <dev/flash-ubi.h>
Dima Zavin214cc642009-01-26 11:16:21 -080044#include <lib/ptable.h>
Dima Zavinb4283602009-01-26 16:36:57 -080045#include <dev/keys.h>
Shashank Mittal4f99a882010-02-01 13:58:50 -080046#include <dev/fbcon.h>
Ajay Dudanid04110c2011-01-17 23:55:07 -080047#include <baseband.h>
Greg Griscod6250552011-06-29 14:40:23 -070048#include <target.h>
49#include <mmc.h>
Kinson Chikf1a43512011-07-14 11:28:39 -070050#include <partition_parser.h>
Greg Griscod6250552011-06-29 14:40:23 -070051#include <platform.h>
Shashank Mittalcd98d472011-08-02 14:29:24 -070052#include <crypto_hash.h>
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070053#include <malloc.h>
Amol Jadi492d5a52013-03-15 16:12:34 -070054#include <boot_stats.h>
Amir Samuelov57a6fa22013-06-05 16:36:43 +030055#include <sha.h>
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -070056#include <platform/iomap.h>
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -070057#include <boot_device.h>
Shashank Mittald3e54dd2014-08-28 15:24:02 -070058#include <boot_verifier.h>
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +053059#include <image_verify.h>
Matthew Qinbb7923d2015-02-09 10:56:09 +080060#include <decompress.h>
Unnati Gandhi17b3bfc2015-05-11 12:58:16 +053061#include <platform/timer.h>
lijuang511a2b52015-08-14 20:50:51 +080062#include <sys/types.h>
Channagoud Kadabi036c6052015-02-09 15:19:59 -080063#if USE_RPMB_FOR_DEVINFO
64#include <rpmb.h>
65#endif
Dima Zavin214cc642009-01-26 11:16:21 -080066
Channagoud Kadabi90869ce2015-04-27 11:15:14 -070067#if ENABLE_WBC
68#include <pm_app_smbchg.h>
69#endif
70
Neeti Desai17379b82012-06-04 18:42:53 -070071#if DEVICE_TREE
72#include <libfdt.h>
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070073#include <dev_tree.h>
Neeti Desai17379b82012-06-04 18:42:53 -070074#endif
75
Aparna Mallavarapu118ccae2015-06-03 13:47:11 +053076#if WDOG_SUPPORT
77#include <wdog.h>
78#endif
79
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -070080#include <reboot.h>
Shashank Mittalcd98d472011-08-02 14:29:24 -070081#include "image_verify.h"
Shashank Mittal024c0332010-02-03 11:44:00 -080082#include "recovery.h"
Brian Swetland9c4c0752009-01-25 16:23:50 -080083#include "bootimg.h"
84#include "fastboot.h"
Ajay Dudani5c761132011-04-07 20:19:04 -070085#include "sparse_format.h"
Ajay Dudanide984792015-03-02 09:57:41 -080086#include "meta_format.h"
Greg Grisco6e754772011-06-23 12:19:39 -070087#include "mmc.h"
Shashank Mittal162244e2011-08-08 19:01:25 -070088#include "devinfo.h"
Neeti Desai465491e2012-07-31 12:53:35 -070089#include "board.h"
Shashank Mittal162244e2011-08-08 19:01:25 -070090#include "scm.h"
Amit Blay6281ebc2015-01-11 14:44:08 +020091#include "mdtp.h"
Sridhar Parasuram32b30662015-07-10 13:33:22 -070092#include "secapp_loader.h"
lijuanga40d6302015-07-20 20:10:13 +080093#include <menu_keys_detect.h>
94#include <display_menu.h>
Channagoud Kadabi736c4962015-08-21 11:56:52 -070095#include "fastboot_test.h"
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -070096
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070097extern bool target_use_signed_kernel(void);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070098extern void platform_uninit(void);
Channagoud Kadabi33defe22013-06-18 18:35:40 -070099extern void target_uninit(void);
Joonwoo Park61112782013-10-02 19:50:39 -0700100extern int get_target_boot_params(const char *cmdline, const char *part,
vijay kumar870515d2015-08-31 16:37:24 +0530101 char **buf);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700102
Sridhar Parasuram7e16d172015-07-05 11:35:23 -0700103void *info_buf;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700104void write_device_info_mmc(device_info *dev);
105void write_device_info_flash(device_info *dev);
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -0700106static int aboot_save_boot_hash_mmc(uint32_t image_addr, uint32_t image_size);
Channagoud Kadabiad259832015-05-29 11:14:17 -0700107static int aboot_frp_unlock(char *pname, void *data, unsigned sz);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700108
Sridhar Parasurame94e8152014-10-24 14:06:03 -0700109/* fastboot command function pointer */
110typedef void (*fastboot_cmd_fn) (const char *, void *, unsigned);
111
112struct fastboot_cmd_desc {
113 char * name;
114 fastboot_cmd_fn cb;
115};
116
Subbaraman Narayanamurthyeb92bcc2010-07-20 14:32:46 -0700117#define EXPAND(NAME) #NAME
118#define TARGET(NAME) EXPAND(NAME)
Brian Swetland2defe162009-08-18 14:35:59 -0700119
Ajay Dudanicd01f9b2010-02-23 21:13:04 -0800120#ifdef MEMBASE
121#define EMMC_BOOT_IMG_HEADER_ADDR (0xFF000+(MEMBASE))
122#else
David Ng183a7422009-12-07 14:55:21 -0800123#define EMMC_BOOT_IMG_HEADER_ADDR 0xFF000
Ajay Dudanicd01f9b2010-02-23 21:13:04 -0800124#endif
125
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700126#ifndef MEMSIZE
127#define MEMSIZE 1024*1024
128#endif
129
130#define MAX_TAGS_SIZE 1024
131
Kun Liang2f1601a2013-08-12 16:29:54 +0800132/* make 4096 as default size to ensure EFS,EXT4's erasing */
133#define DEFAULT_ERASE_SIZE 4096
Ujwal Patelc0b0a252015-08-16 14:05:35 -0700134#define MAX_PANEL_BUF_SIZE 196
Kun Liang2f1601a2013-08-12 16:29:54 +0800135
Dhaval Patelf83d73b2014-06-23 16:24:37 -0700136#define DISPLAY_DEFAULT_PREFIX "mdss_mdp"
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700137#define BOOT_DEV_MAX_LEN 64
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -0800138
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800139#define IS_ARM64(ptr) (ptr->magic_64 == KERNEL64_HDR_MAGIC) ? true : false
140
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -0700141#define ADD_OF(a, b) (UINT_MAX - b > a) ? (a + b) : UINT_MAX
142
Sridhar Parasuram7bd4aaf2015-02-12 11:14:38 -0800143#if USE_BOOTDEV_CMDLINE
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700144static const char *emmc_cmdline = " androidboot.bootdevice=";
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700145#else
David Ng183a7422009-12-07 14:55:21 -0800146static const char *emmc_cmdline = " androidboot.emmc=true";
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700147#endif
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800148static const char *usb_sn_cmdline = " androidboot.serialno=";
Pavel Nedev328ac822013-04-05 15:25:11 +0300149static const char *androidboot_mode = " androidboot.mode=";
Matthew Qind886f3c2014-01-17 16:52:01 +0800150static const char *alarmboot_cmdline = " androidboot.alarmboot=true";
Pavel Nedev898298c2013-02-27 12:36:09 -0800151static const char *loglevel = " quiet";
Ajay Dudanica3a33c2011-11-18 08:31:40 -0800152static const char *battchg_pause = " androidboot.mode=charger";
Shashank Mittalcd98d472011-08-02 14:29:24 -0700153static const char *auth_kernel = " androidboot.authorized_kernel=true";
Pavel Nedev5614d222013-06-17 18:01:02 +0300154static const char *secondary_gpt_enable = " gpt";
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200155static const char *mdtp_activated_flag = " mdtp";
David Ng183a7422009-12-07 14:55:21 -0800156
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800157static const char *baseband_apq = " androidboot.baseband=apq";
158static const char *baseband_msm = " androidboot.baseband=msm";
159static const char *baseband_csfb = " androidboot.baseband=csfb";
160static const char *baseband_svlte2a = " androidboot.baseband=svlte2a";
Ajay Dudani403bc492011-09-30 16:17:21 -0700161static const char *baseband_mdm = " androidboot.baseband=mdm";
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800162static const char *baseband_mdm2 = " androidboot.baseband=mdm2";
Amol Jadi5c61a952012-05-04 17:05:35 -0700163static const char *baseband_sglte = " androidboot.baseband=sglte";
Amol Jadi2a15a272013-01-22 12:03:36 -0800164static const char *baseband_dsda = " androidboot.baseband=dsda";
165static const char *baseband_dsda2 = " androidboot.baseband=dsda2";
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800166static const char *baseband_sglte2 = " androidboot.baseband=sglte2";
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800167static const char *warmboot_cmdline = " qpnp-power-on.warm_boot=1";
Ajay Dudanid04110c2011-01-17 23:55:07 -0800168
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700169#if VERIFIED_BOOT
Parth Dixitddbc7352015-10-18 03:13:31 +0530170#if !VBOOT_MOTA
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700171static const char *verity_mode = " androidboot.veritymode=";
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700172static const char *verified_state= " androidboot.verifiedbootstate=";
Parth Dixita5715a02015-10-29 12:25:10 +0530173static const char *keymaster_v1= " androidboot.keymaster=1";
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700174//indexed based on enum values, green is 0 by default
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700175
176struct verified_boot_verity_mode vbvm[] =
177{
178 {false, "logging"},
179 {true, "enforcing"},
180};
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700181struct verified_boot_state_name vbsn[] =
182{
183 {GREEN, "green"},
184 {ORANGE, "orange"},
185 {YELLOW,"yellow"},
186 {RED,"red" },
187};
188#endif
Parth Dixitddbc7352015-10-18 03:13:31 +0530189#endif
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700190
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700191static unsigned page_size = 0;
192static unsigned page_mask = 0;
Gaurav Nebhwani4d2389c2016-03-17 21:10:05 +0530193static unsigned mmc_blocksize = 0;
194static unsigned mmc_blocksize_mask = 0;
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700195static char ffbm_mode_string[FFBM_MODE_BUF_SIZE];
196static bool boot_into_ffbm;
vijay kumar870515d2015-08-31 16:37:24 +0530197static char *target_boot_params = NULL;
Matthew Qind886f3c2014-01-17 16:52:01 +0800198static bool boot_reason_alarm;
Channagoud Kadabi80a182b2015-03-11 17:04:23 -0700199static bool devinfo_present = true;
Channagoud Kadabi736c4962015-08-21 11:56:52 -0700200bool boot_into_fastboot = false;
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700201
Shashank Mittalcd98d472011-08-02 14:29:24 -0700202/* Assuming unauthorized kernel image by default */
203static int auth_kernel_img = 0;
Parth Dixit6f5822f2015-10-18 01:20:53 +0530204#if VBOOT_MOTA
205static device_info device = {DEVICE_MAGIC, 0, 0, 0, 0, {0}, {0},{0}};
206#else
lijuang511a2b52015-08-14 20:50:51 +0800207static device_info device = {DEVICE_MAGIC, 0, 0, 0, 0, {0}, {0},{0}, 1};
Parth Dixit6f5822f2015-10-18 01:20:53 +0530208#endif
vijay kumarc65876c2015-04-24 13:29:16 +0530209static bool is_allow_unlock = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -0700210
vijay kumarca2e6812015-07-08 20:28:25 +0530211static char frp_ptns[2][8] = {"config","frp"};
212
lijuang511a2b52015-08-14 20:50:51 +0800213static const char *critical_flash_allowed_ptn[] = {
214 "aboot",
215 "rpm",
216 "tz",
217 "sbl",
218 "sdi",
219 "sbl1",
220 "xbl",
221 "hyp",
222 "pmic",
223 "bootloader",
224 "devinfo",
225 "partition"};
226
Dima Zavin42168f22009-01-30 11:52:22 -0800227struct atag_ptbl_entry
228{
229 char name[16];
230 unsigned offset;
231 unsigned size;
232 unsigned flags;
233};
234
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700235/*
236 * Partition info, required to be published
237 * for fastboot
238 */
239struct getvar_partition_info {
240 const char part_name[MAX_GPT_NAME_SIZE]; /* Partition name */
241 char getvar_size[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for size */
242 char getvar_type[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for type */
243 char size_response[MAX_RSP_SIZE]; /* fastboot response for size */
244 char type_response[MAX_RSP_SIZE]; /* fastboot response for type */
245};
246
247/*
248 * Right now, we are publishing the info for only
249 * three partitions
250 */
251struct getvar_partition_info part_info[] =
252{
253 { "system" , "partition-size:", "partition-type:", "", "ext4" },
254 { "userdata", "partition-size:", "partition-type:", "", "ext4" },
255 { "cache" , "partition-size:", "partition-type:", "", "ext4" },
256};
257
258char max_download_size[MAX_RSP_SIZE];
Ameya Thakur11cf1a62013-08-05 12:44:48 -0700259char charger_screen_enabled[MAX_RSP_SIZE];
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800260char sn_buf[13];
Dhaval Patel223ec952013-07-18 14:49:44 -0700261char display_panel_buf[MAX_PANEL_BUF_SIZE];
Unnati Gandhi62c8ab82014-01-24 11:01:01 +0530262char panel_display_mode[MAX_RSP_SIZE];
lijuang102dfa92015-10-09 18:31:03 +0800263
264#if CHECK_BAT_VOLTAGE
lijuang65c5a822015-08-29 16:35:36 +0800265char battery_voltage[MAX_RSP_SIZE];
lijuang102dfa92015-10-09 18:31:03 +0800266char battery_soc_ok [MAX_RSP_SIZE];
267#endif
268
lijuangf16461c2015-08-03 17:09:34 +0800269char get_variant[MAX_RSP_SIZE];
Greg Griscod6250552011-06-29 14:40:23 -0700270
Greg Griscod2471ef2011-07-14 13:00:42 -0700271extern int emmc_recovery_init(void);
272
Kinson Chik0b1c8162011-08-31 16:31:57 -0700273#if NO_KEYPAD_DRIVER
274extern int fastboot_trigger(void);
275#endif
Greg Griscod2471ef2011-07-14 13:00:42 -0700276
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800277static void update_ker_tags_rdisk_addr(struct boot_img_hdr *hdr, bool is_arm64)
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700278{
279 /* overwrite the destination of specified for the project */
Channagoud Kadabi7042fa32013-04-26 16:44:14 -0700280#ifdef ABOOT_IGNORE_BOOT_HEADER_ADDRS
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800281 if (is_arm64)
282 hdr->kernel_addr = ABOOT_FORCE_KERNEL64_ADDR;
283 else
284 hdr->kernel_addr = ABOOT_FORCE_KERNEL_ADDR;
Channagoud Kadabi7042fa32013-04-26 16:44:14 -0700285 hdr->ramdisk_addr = ABOOT_FORCE_RAMDISK_ADDR;
286 hdr->tags_addr = ABOOT_FORCE_TAGS_ADDR;
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700287#endif
288}
289
Dima Zavin42168f22009-01-30 11:52:22 -0800290static void ptentry_to_tag(unsigned **ptr, struct ptentry *ptn)
291{
292 struct atag_ptbl_entry atag_ptn;
293
294 memcpy(atag_ptn.name, ptn->name, 16);
295 atag_ptn.name[15] = '\0';
296 atag_ptn.offset = ptn->start;
297 atag_ptn.size = ptn->length;
298 atag_ptn.flags = ptn->flags;
299 memcpy(*ptr, &atag_ptn, sizeof(struct atag_ptbl_entry));
300 *ptr += sizeof(struct atag_ptbl_entry) / sizeof(unsigned);
301}
Brian Swetland9c4c0752009-01-25 16:23:50 -0800302
lijuang102dfa92015-10-09 18:31:03 +0800303#if CHECK_BAT_VOLTAGE
304void update_battery_status(void)
305{
306 snprintf(battery_voltage,MAX_RSP_SIZE, "%d",target_get_battery_voltage());
307 snprintf(battery_soc_ok ,MAX_RSP_SIZE, "%s",target_battery_soc_ok()? "yes":"no");
308}
309#endif
310
Neeti Desaie245d492012-06-01 12:52:13 -0700311unsigned char *update_cmdline(const char * cmdline)
Brian Swetland9c4c0752009-01-25 16:23:50 -0800312{
David Ng183a7422009-12-07 14:55:21 -0800313 int cmdline_len = 0;
314 int have_cmdline = 0;
Amol Jadi168b7712012-03-06 16:15:00 -0800315 unsigned char *cmdline_final = NULL;
Neeti Desaie245d492012-06-01 12:52:13 -0700316 int pause_at_bootup = 0;
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800317 bool warm_boot = false;
Pavel Nedev5614d222013-06-17 18:01:02 +0300318 bool gpt_exists = partition_gpt_exists();
Joonwoo Park61112782013-10-02 19:50:39 -0700319 int have_target_boot_params = 0;
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700320 char *boot_dev_buf = NULL;
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200321 bool is_mdtp_activated = 0;
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700322#if VERIFIED_BOOT
Parth Dixitddbc7352015-10-18 03:13:31 +0530323#if !VBOOT_MOTA
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700324 uint32_t boot_state = boot_verify_get_state();
325#endif
Parth Dixitddbc7352015-10-18 03:13:31 +0530326#endif
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700327
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200328#ifdef MDTP_SUPPORT
329 mdtp_activated(&is_mdtp_activated);
330#endif /* MDTP_SUPPORT */
Dima Zavin42168f22009-01-30 11:52:22 -0800331
Brian Swetland9c4c0752009-01-25 16:23:50 -0800332 if (cmdline && cmdline[0]) {
David Ng183a7422009-12-07 14:55:21 -0800333 cmdline_len = strlen(cmdline);
334 have_cmdline = 1;
335 }
336 if (target_is_emmc_boot()) {
337 cmdline_len += strlen(emmc_cmdline);
Sridhar Parasuram7bd4aaf2015-02-12 11:14:38 -0800338#if USE_BOOTDEV_CMDLINE
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700339 boot_dev_buf = (char *) malloc(sizeof(char) * BOOT_DEV_MAX_LEN);
340 ASSERT(boot_dev_buf);
341 platform_boot_dev_cmdline(boot_dev_buf);
342 cmdline_len += strlen(boot_dev_buf);
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700343#endif
David Ng183a7422009-12-07 14:55:21 -0800344 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800345
346 cmdline_len += strlen(usb_sn_cmdline);
347 cmdline_len += strlen(sn_buf);
348
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700349#if VERIFIED_BOOT
Parth Dixitddbc7352015-10-18 03:13:31 +0530350#if !VBOOT_MOTA
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700351 cmdline_len += strlen(verified_state) + strlen(vbsn[boot_state].name);
Sridhar Parasuram7d8bb9a2015-09-23 18:21:19 -0700352 if ((device.verity_mode != 0 ) && (device.verity_mode != 1))
353 {
354 dprintf(CRITICAL, "Devinfo paritition possibly corrupted!!!. Please erase devinfo partition to continue booting\n");
355 ASSERT(0);
356 }
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700357 cmdline_len += strlen(verity_mode) + strlen(vbvm[device.verity_mode].name);
Parth Dixita5715a02015-10-29 12:25:10 +0530358 cmdline_len += strlen(keymaster_v1);
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700359#endif
Parth Dixitddbc7352015-10-18 03:13:31 +0530360#endif
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700361
Pavel Nedev5614d222013-06-17 18:01:02 +0300362 if (boot_into_recovery && gpt_exists)
363 cmdline_len += strlen(secondary_gpt_enable);
364
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200365 if(is_mdtp_activated)
366 cmdline_len += strlen(mdtp_activated_flag);
367
Pavel Nedev328ac822013-04-05 15:25:11 +0300368 if (boot_into_ffbm) {
369 cmdline_len += strlen(androidboot_mode);
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700370 cmdline_len += strlen(ffbm_mode_string);
Pavel Nedev898298c2013-02-27 12:36:09 -0800371 /* reduce kernel console messages to speed-up boot */
372 cmdline_len += strlen(loglevel);
Matthew Qind886f3c2014-01-17 16:52:01 +0800373 } else if (boot_reason_alarm) {
374 cmdline_len += strlen(alarmboot_cmdline);
Zhenhua Huang431dafa2015-06-30 16:13:37 +0800375 } else if ((target_build_variant_user() || device.charger_screen_enabled)
376 && target_pause_for_battery_charge()) {
David Ngf773dde2010-07-26 19:55:08 -0700377 pause_at_bootup = 1;
378 cmdline_len += strlen(battchg_pause);
379 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800380
Shashank Mittalcd98d472011-08-02 14:29:24 -0700381 if(target_use_signed_kernel() && auth_kernel_img) {
382 cmdline_len += strlen(auth_kernel);
383 }
384
Joonwoo Park61112782013-10-02 19:50:39 -0700385 if (get_target_boot_params(cmdline, boot_into_recovery ? "recoveryfs" :
386 "system",
vijay kumar870515d2015-08-31 16:37:24 +0530387 &target_boot_params) == 0) {
Joonwoo Park61112782013-10-02 19:50:39 -0700388 have_target_boot_params = 1;
389 cmdline_len += strlen(target_boot_params);
390 }
391
Ajay Dudanid04110c2011-01-17 23:55:07 -0800392 /* Determine correct androidboot.baseband to use */
393 switch(target_baseband())
394 {
395 case BASEBAND_APQ:
396 cmdline_len += strlen(baseband_apq);
397 break;
398
399 case BASEBAND_MSM:
400 cmdline_len += strlen(baseband_msm);
401 break;
402
403 case BASEBAND_CSFB:
404 cmdline_len += strlen(baseband_csfb);
405 break;
406
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800407 case BASEBAND_SVLTE2A:
408 cmdline_len += strlen(baseband_svlte2a);
Ajay Dudanid04110c2011-01-17 23:55:07 -0800409 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700410
411 case BASEBAND_MDM:
412 cmdline_len += strlen(baseband_mdm);
413 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700414
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800415 case BASEBAND_MDM2:
416 cmdline_len += strlen(baseband_mdm2);
417 break;
418
Amol Jadi5c61a952012-05-04 17:05:35 -0700419 case BASEBAND_SGLTE:
420 cmdline_len += strlen(baseband_sglte);
421 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530422
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800423 case BASEBAND_SGLTE2:
424 cmdline_len += strlen(baseband_sglte2);
425 break;
426
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530427 case BASEBAND_DSDA:
428 cmdline_len += strlen(baseband_dsda);
429 break;
Amol Jadi2a15a272013-01-22 12:03:36 -0800430
431 case BASEBAND_DSDA2:
432 cmdline_len += strlen(baseband_dsda2);
433 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800434 }
435
Lijuan Gao4a5b8322014-07-24 10:38:42 +0800436 if (cmdline) {
437 if ((strstr(cmdline, DISPLAY_DEFAULT_PREFIX) == NULL) &&
Padmanabhan Komandurubccbcdc2015-06-30 16:19:24 +0530438 target_display_panel_node(display_panel_buf,
439 MAX_PANEL_BUF_SIZE) &&
Lijuan Gao4a5b8322014-07-24 10:38:42 +0800440 strlen(display_panel_buf)) {
441 cmdline_len += strlen(display_panel_buf);
442 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700443 }
444
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800445 if (target_warm_boot()) {
446 warm_boot = true;
447 cmdline_len += strlen(warmboot_cmdline);
448 }
449
Channagoud Kadabi85c7ec32016-01-28 23:09:21 -0800450#if TARGET_CMDLINE_SUPPORT
451 char *target_cmdline_buf = malloc(TARGET_MAX_CMDLNBUF);
452 int target_cmd_line_len;
453 ASSERT(target_cmdline_buf);
454 target_cmd_line_len = target_update_cmdline(target_cmdline_buf);
455 cmdline_len += target_cmd_line_len;
456#endif
457
David Ng183a7422009-12-07 14:55:21 -0800458 if (cmdline_len > 0) {
459 const char *src;
Maria Yu52254c02014-07-04 16:14:54 +0800460 unsigned char *dst;
461
462 cmdline_final = (unsigned char*) malloc((cmdline_len + 4) & (~3));
463 ASSERT(cmdline_final != NULL);
vijay kumar287bb542015-09-29 13:01:52 +0530464 memset((void *)cmdline_final, 0, sizeof(*cmdline_final));
Maria Yu52254c02014-07-04 16:14:54 +0800465 dst = cmdline_final;
Neeti Desaie245d492012-06-01 12:52:13 -0700466
Amol Jadi168b7712012-03-06 16:15:00 -0800467 /* Save start ptr for debug print */
David Ng183a7422009-12-07 14:55:21 -0800468 if (have_cmdline) {
469 src = cmdline;
470 while ((*dst++ = *src++));
471 }
472 if (target_is_emmc_boot()) {
473 src = emmc_cmdline;
474 if (have_cmdline) --dst;
David Ngf773dde2010-07-26 19:55:08 -0700475 have_cmdline = 1;
476 while ((*dst++ = *src++));
Sridhar Parasuram7bd4aaf2015-02-12 11:14:38 -0800477#if USE_BOOTDEV_CMDLINE
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700478 src = boot_dev_buf;
479 if (have_cmdline) --dst;
480 while ((*dst++ = *src++));
481#endif
David Ngf773dde2010-07-26 19:55:08 -0700482 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800483
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700484#if VERIFIED_BOOT
Parth Dixitddbc7352015-10-18 03:13:31 +0530485#if !VBOOT_MOTA
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700486 src = verified_state;
487 if(have_cmdline) --dst;
488 have_cmdline = 1;
489 while ((*dst++ = *src++));
490 src = vbsn[boot_state].name;
491 if(have_cmdline) --dst;
492 while ((*dst++ = *src++));
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700493
Sridhar Parasuram7d8bb9a2015-09-23 18:21:19 -0700494 if ((device.verity_mode != 0 ) && (device.verity_mode != 1))
495 {
496 dprintf(CRITICAL, "Devinfo paritition possibly corrupted!!!. Please erase devinfo partition to continue booting\n");
497 ASSERT(0);
498 }
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700499 src = verity_mode;
500 if(have_cmdline) --dst;
501 while ((*dst++ = *src++));
502 src = vbvm[device.verity_mode].name;
503 if(have_cmdline) -- dst;
504 while ((*dst++ = *src++));
Parth Dixita5715a02015-10-29 12:25:10 +0530505 src = keymaster_v1;
506 if(have_cmdline) --dst;
507 while ((*dst++ = *src++));
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700508#endif
Parth Dixitddbc7352015-10-18 03:13:31 +0530509#endif
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800510 src = usb_sn_cmdline;
511 if (have_cmdline) --dst;
512 have_cmdline = 1;
513 while ((*dst++ = *src++));
514 src = sn_buf;
515 if (have_cmdline) --dst;
516 have_cmdline = 1;
517 while ((*dst++ = *src++));
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800518 if (warm_boot) {
519 if (have_cmdline) --dst;
520 src = warmboot_cmdline;
521 while ((*dst++ = *src++));
522 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800523
Pavel Nedev5614d222013-06-17 18:01:02 +0300524 if (boot_into_recovery && gpt_exists) {
525 src = secondary_gpt_enable;
526 if (have_cmdline) --dst;
527 while ((*dst++ = *src++));
528 }
529
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200530 if (is_mdtp_activated) {
531 src = mdtp_activated_flag;
532 if (have_cmdline) --dst;
533 while ((*dst++ = *src++));
534 }
535
Pavel Nedev328ac822013-04-05 15:25:11 +0300536 if (boot_into_ffbm) {
537 src = androidboot_mode;
538 if (have_cmdline) --dst;
539 while ((*dst++ = *src++));
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700540 src = ffbm_mode_string;
Pavel Nedev328ac822013-04-05 15:25:11 +0300541 if (have_cmdline) --dst;
542 while ((*dst++ = *src++));
Pavel Nedev898298c2013-02-27 12:36:09 -0800543 src = loglevel;
544 if (have_cmdline) --dst;
545 while ((*dst++ = *src++));
Matthew Qind886f3c2014-01-17 16:52:01 +0800546 } else if (boot_reason_alarm) {
547 src = alarmboot_cmdline;
548 if (have_cmdline) --dst;
549 while ((*dst++ = *src++));
Pavel Nedev328ac822013-04-05 15:25:11 +0300550 } else if (pause_at_bootup) {
David Ngf773dde2010-07-26 19:55:08 -0700551 src = battchg_pause;
552 if (have_cmdline) --dst;
David Ng183a7422009-12-07 14:55:21 -0800553 while ((*dst++ = *src++));
554 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800555
Shashank Mittalcd98d472011-08-02 14:29:24 -0700556 if(target_use_signed_kernel() && auth_kernel_img) {
557 src = auth_kernel;
558 if (have_cmdline) --dst;
559 while ((*dst++ = *src++));
560 }
561
Ajay Dudanid04110c2011-01-17 23:55:07 -0800562 switch(target_baseband())
563 {
564 case BASEBAND_APQ:
565 src = baseband_apq;
566 if (have_cmdline) --dst;
567 while ((*dst++ = *src++));
568 break;
569
570 case BASEBAND_MSM:
571 src = baseband_msm;
572 if (have_cmdline) --dst;
573 while ((*dst++ = *src++));
574 break;
575
576 case BASEBAND_CSFB:
577 src = baseband_csfb;
578 if (have_cmdline) --dst;
579 while ((*dst++ = *src++));
580 break;
581
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800582 case BASEBAND_SVLTE2A:
583 src = baseband_svlte2a;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800584 if (have_cmdline) --dst;
585 while ((*dst++ = *src++));
586 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700587
588 case BASEBAND_MDM:
589 src = baseband_mdm;
590 if (have_cmdline) --dst;
591 while ((*dst++ = *src++));
592 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700593
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800594 case BASEBAND_MDM2:
595 src = baseband_mdm2;
596 if (have_cmdline) --dst;
597 while ((*dst++ = *src++));
598 break;
599
Amol Jadi5c61a952012-05-04 17:05:35 -0700600 case BASEBAND_SGLTE:
601 src = baseband_sglte;
602 if (have_cmdline) --dst;
603 while ((*dst++ = *src++));
604 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530605
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800606 case BASEBAND_SGLTE2:
607 src = baseband_sglte2;
608 if (have_cmdline) --dst;
609 while ((*dst++ = *src++));
610 break;
611
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530612 case BASEBAND_DSDA:
613 src = baseband_dsda;
614 if (have_cmdline) --dst;
615 while ((*dst++ = *src++));
616 break;
Amol Jadi2a15a272013-01-22 12:03:36 -0800617
618 case BASEBAND_DSDA2:
619 src = baseband_dsda2;
620 if (have_cmdline) --dst;
621 while ((*dst++ = *src++));
622 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800623 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700624
625 if (strlen(display_panel_buf)) {
Dhaval Patel223ec952013-07-18 14:49:44 -0700626 src = display_panel_buf;
627 if (have_cmdline) --dst;
628 while ((*dst++ = *src++));
629 }
Joonwoo Park61112782013-10-02 19:50:39 -0700630
631 if (have_target_boot_params) {
632 if (have_cmdline) --dst;
633 src = target_boot_params;
634 while ((*dst++ = *src++));
vijay kumar870515d2015-08-31 16:37:24 +0530635 free(target_boot_params);
Joonwoo Park61112782013-10-02 19:50:39 -0700636 }
Channagoud Kadabi85c7ec32016-01-28 23:09:21 -0800637
638#if TARGET_CMDLINE_SUPPORT
639 if (target_cmdline_buf && target_cmd_line_len)
640 {
641 if (have_cmdline) --dst;
642 src = target_cmdline_buf;
643 while((*dst++ = *src++));
644 free(target_cmdline_buf);
645 }
646#endif
Neeti Desaie245d492012-06-01 12:52:13 -0700647 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700648
649
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700650 if (boot_dev_buf)
651 free(boot_dev_buf);
652
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -0800653 if (cmdline_final)
654 dprintf(INFO, "cmdline: %s\n", cmdline_final);
655 else
656 dprintf(INFO, "cmdline is NULL\n");
Neeti Desaie245d492012-06-01 12:52:13 -0700657 return cmdline_final;
658}
659
660unsigned *atag_core(unsigned *ptr)
661{
662 /* CORE */
663 *ptr++ = 2;
664 *ptr++ = 0x54410001;
665
666 return ptr;
667
668}
669
670unsigned *atag_ramdisk(unsigned *ptr, void *ramdisk,
671 unsigned ramdisk_size)
672{
673 if (ramdisk_size) {
674 *ptr++ = 4;
675 *ptr++ = 0x54420005;
676 *ptr++ = (unsigned)ramdisk;
677 *ptr++ = ramdisk_size;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800678 }
679
Neeti Desaie245d492012-06-01 12:52:13 -0700680 return ptr;
681}
682
683unsigned *atag_ptable(unsigned **ptr_addr)
684{
685 int i;
686 struct ptable *ptable;
687
688 if ((ptable = flash_get_ptable()) && (ptable->count != 0)) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700689 *(*ptr_addr)++ = 2 + (ptable->count * (sizeof(struct atag_ptbl_entry) /
690 sizeof(unsigned)));
Neeti Desaie245d492012-06-01 12:52:13 -0700691 *(*ptr_addr)++ = 0x4d534d70;
692 for (i = 0; i < ptable->count; ++i)
693 ptentry_to_tag(ptr_addr, ptable_get(ptable, i));
694 }
695
696 return (*ptr_addr);
697}
698
699unsigned *atag_cmdline(unsigned *ptr, const char *cmdline)
700{
701 int cmdline_length = 0;
702 int n;
Neeti Desaie245d492012-06-01 12:52:13 -0700703 char *dest;
704
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800705 cmdline_length = strlen((const char*)cmdline);
Neeti Desaie245d492012-06-01 12:52:13 -0700706 n = (cmdline_length + 4) & (~3);
707
708 *ptr++ = (n / 4) + 2;
709 *ptr++ = 0x54410009;
710 dest = (char *) ptr;
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800711 while ((*dest++ = *cmdline++));
Neeti Desaie245d492012-06-01 12:52:13 -0700712 ptr += (n / 4);
713
714 return ptr;
715}
716
717unsigned *atag_end(unsigned *ptr)
718{
Brian Swetland9c4c0752009-01-25 16:23:50 -0800719 /* END */
720 *ptr++ = 0;
721 *ptr++ = 0;
722
Neeti Desaie245d492012-06-01 12:52:13 -0700723 return ptr;
724}
725
726void generate_atags(unsigned *ptr, const char *cmdline,
727 void *ramdisk, unsigned ramdisk_size)
728{
vijay kumar21a37452015-12-29 16:23:21 +0530729 unsigned *orig_ptr = ptr;
Neeti Desaie245d492012-06-01 12:52:13 -0700730 ptr = atag_core(ptr);
731 ptr = atag_ramdisk(ptr, ramdisk, ramdisk_size);
732 ptr = target_atag_mem(ptr);
733
734 /* Skip NAND partition ATAGS for eMMC boot */
735 if (!target_is_emmc_boot()){
736 ptr = atag_ptable(&ptr);
737 }
738
vijay kumar21a37452015-12-29 16:23:21 +0530739 /*
740 * Atags size filled till + cmdline size + 1 unsigned for 4-byte boundary + 4 unsigned
741 * for atag identifier in atag_cmdline and atag_end should be with in MAX_TAGS_SIZE bytes
742 */
743 if (((ptr - orig_ptr) + strlen(cmdline) + 5 * sizeof(unsigned)) < MAX_TAGS_SIZE) {
744 ptr = atag_cmdline(ptr, cmdline);
745 ptr = atag_end(ptr);
746 }
747 else {
748 dprintf(CRITICAL,"Crossing ATAGs Max size allowed\n");
749 ASSERT(0);
750 }
Neeti Desaie245d492012-06-01 12:52:13 -0700751}
752
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700753typedef void entry_func_ptr(unsigned, unsigned, unsigned*);
Neeti Desaie245d492012-06-01 12:52:13 -0700754void boot_linux(void *kernel, unsigned *tags,
755 const char *cmdline, unsigned machtype,
756 void *ramdisk, unsigned ramdisk_size)
757{
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800758 unsigned char *final_cmdline;
Amol Jadib6be5c12012-11-14 13:39:51 -0800759#if DEVICE_TREE
Neeti Desai17379b82012-06-04 18:42:53 -0700760 int ret = 0;
Amol Jadib6be5c12012-11-14 13:39:51 -0800761#endif
762
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700763 void (*entry)(unsigned, unsigned, unsigned*) = (entry_func_ptr*)(PA((addr_t)kernel));
Deepa Dinamani25a9f762012-11-30 15:57:15 -0800764 uint32_t tags_phys = PA((addr_t)tags);
vijay kumar1a50a642015-11-16 12:41:15 +0530765 struct kernel64_hdr *kptr = ((struct kernel64_hdr*)(PA((addr_t)kernel)));
Deepa Dinamani25a9f762012-11-30 15:57:15 -0800766
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530767 ramdisk = (void *)PA((addr_t)ramdisk);
Neeti Desaie245d492012-06-01 12:52:13 -0700768
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800769 final_cmdline = update_cmdline((const char*)cmdline);
770
Neeti Desai17379b82012-06-04 18:42:53 -0700771#if DEVICE_TREE
Amol Jadib6be5c12012-11-14 13:39:51 -0800772 dprintf(INFO, "Updating device tree: start\n");
773
Neeti Desai17379b82012-06-04 18:42:53 -0700774 /* Update the Device Tree */
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530775 ret = update_device_tree((void *)tags,(const char *)final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -0700776 if(ret)
777 {
778 dprintf(CRITICAL, "ERROR: Updating Device Tree Failed \n");
779 ASSERT(0);
780 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800781 dprintf(INFO, "Updating device tree: done\n");
Neeti Desai17379b82012-06-04 18:42:53 -0700782#else
Neeti Desaie245d492012-06-01 12:52:13 -0700783 /* Generating the Atags */
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800784 generate_atags(tags, final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -0700785#endif
Neeti Desaie245d492012-06-01 12:52:13 -0700786
Maria Yu52254c02014-07-04 16:14:54 +0800787 free(final_cmdline);
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -0700788
789#if VERIFIED_BOOT
lijuangbdd9bb42016-03-01 18:22:17 +0800790#if !VBOOT_MOTA
791 if (device.verity_mode == 0) {
792#if FBCON_DISPLAY_MSG
793 display_bootverify_menu(DISPLAY_MENU_LOGGING);
794 wait_for_users_action();
795#else
796 dprintf(CRITICAL,
797 "The dm-verity is not started in enforcing mode.\nWait for 5 seconds before proceeding\n");
798 mdelay(5000);
799#endif
800 }
801
802#endif
803#endif
804
805#if VERIFIED_BOOT
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -0700806 /* Write protect the device info */
Channagoud Kadabi3bd9d1e2015-05-05 16:18:20 -0700807 if (!boot_into_recovery && target_build_variant_user() && devinfo_present && mmc_write_protect("devinfo", 1))
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -0700808 {
809 dprintf(INFO, "Failed to write protect dev info\n");
810 ASSERT(0);
811 }
812#endif
813
Aravind Venkateswaran8f076242014-02-25 16:25:30 -0800814 /* Turn off splash screen if enabled */
815#if DISPLAY_SPLASH_SCREEN
816 target_display_shutdown();
817#endif
818
Veera Sundaram Sankaran67ea0932015-09-25 10:09:30 -0700819 /* Perform target specific cleanup */
820 target_uninit();
Aravind Venkateswaran8f076242014-02-25 16:25:30 -0800821
Deepa Dinamani33734bc2013-03-06 12:16:06 -0800822 dprintf(INFO, "booting linux @ %p, ramdisk @ %p (%d), tags/device tree @ %p\n",
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530823 entry, ramdisk, ramdisk_size, (void *)tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800824
825 enter_critical_section();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700826
Amol Jadi4421e652011-06-16 15:00:48 -0700827 /* do any platform specific cleanup before kernel entry */
828 platform_uninit();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700829
Brian Swetland9c4c0752009-01-25 16:23:50 -0800830 arch_disable_cache(UCACHE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700831
Amol Jadi504f9fe2012-08-16 13:56:48 -0700832#if ARM_WITH_MMU
Brian Swetland9c4c0752009-01-25 16:23:50 -0800833 arch_disable_mmu();
Amol Jadi504f9fe2012-08-16 13:56:48 -0700834#endif
Amol Jadi492d5a52013-03-15 16:12:34 -0700835 bs_set_timestamp(BS_KERNEL_ENTRY);
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800836
837 if (IS_ARM64(kptr))
838 /* Jump to a 64bit kernel */
839 scm_elexec_call((paddr_t)kernel, tags_phys);
840 else
841 /* Jump to a 32bit kernel */
842 entry(0, machtype, (unsigned*)tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800843}
844
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700845/* Function to check if the memory address range falls within the aboot
846 * boundaries.
847 * start: Start of the memory region
848 * size: Size of the memory region
849 */
Vijay Kumar Pendoti9c002ad2016-03-09 13:52:45 +0530850int check_aboot_addr_range_overlap(uintptr_t start, uint32_t size)
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700851{
852 /* Check for boundary conditions. */
Sundarajan Srinivasance2a0ea2013-12-16 17:02:56 -0800853 if ((UINT_MAX - start) < size)
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700854 return -1;
855
856 /* Check for memory overlap. */
857 if ((start < MEMBASE) && ((start + size) <= MEMBASE))
858 return 0;
Channagoud Kadabi94143912013-10-15 12:53:52 -0700859 else if (start >= (MEMBASE + MEMSIZE))
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700860 return 0;
861 else
862 return -1;
863}
864
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800865#define ROUND_TO_PAGE(x,y) (((x) + (y)) & (~(y)))
Brian Swetland9c4c0752009-01-25 16:23:50 -0800866
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800867BUF_DMA_ALIGN(buf, BOOT_IMG_MAX_PAGE_SIZE); //Equal to max-supported pagesize
Amol Jadib6be5c12012-11-14 13:39:51 -0800868#if DEVICE_TREE
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800869BUF_DMA_ALIGN(dt_buf, BOOT_IMG_MAX_PAGE_SIZE);
Amol Jadib6be5c12012-11-14 13:39:51 -0800870#endif
Dima Zavin214cc642009-01-26 11:16:21 -0800871
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700872static void verify_signed_bootimg(uint32_t bootimg_addr, uint32_t bootimg_size)
873{
874 int ret;
Channagoud Kadabia8c623f2015-01-13 14:48:48 -0800875
876#if !VERIFIED_BOOT
Sundarajan Srinivasance54d6e2013-11-11 12:45:00 -0800877#if IMAGE_VERIF_ALGO_SHA1
878 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA1;
879#else
880 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA256;
881#endif
Channagoud Kadabia8c623f2015-01-13 14:48:48 -0800882#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700883
884 /* Assume device is rooted at this time. */
885 device.is_tampered = 1;
886
887 dprintf(INFO, "Authenticating boot image (%d): start\n", bootimg_size);
888
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700889#if VERIFIED_BOOT
890 if(boot_into_recovery)
891 {
892 ret = boot_verify_image((unsigned char *)bootimg_addr,
Unnati Gandhi47051252015-03-02 15:21:09 +0530893 bootimg_size, "/recovery");
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700894 }
895 else
896 {
897 ret = boot_verify_image((unsigned char *)bootimg_addr,
Unnati Gandhi47051252015-03-02 15:21:09 +0530898 bootimg_size, "/boot");
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700899 }
900 boot_verify_print_state();
901#else
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700902 ret = image_verify((unsigned char *)bootimg_addr,
903 (unsigned char *)(bootimg_addr + bootimg_size),
904 bootimg_size,
Sundarajan Srinivasance54d6e2013-11-11 12:45:00 -0800905 auth_algo);
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700906#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700907 dprintf(INFO, "Authenticating boot image: done return value = %d\n", ret);
908
909 if (ret)
910 {
911 /* Authorized kernel */
912 device.is_tampered = 0;
Sundarajan Srinivasan3fb21f12013-09-16 18:36:15 -0700913 auth_kernel_img = 1;
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700914 }
915
Amit Blay4aa292f2015-04-28 21:55:59 +0300916#ifdef MDTP_SUPPORT
917 {
918 /* Verify MDTP lock.
919 * For boot & recovery partitions, use aboot's verification result.
920 */
921 mdtp_ext_partition_verification_t ext_partition;
922 ext_partition.partition = boot_into_recovery ? MDTP_PARTITION_RECOVERY : MDTP_PARTITION_BOOT;
923 ext_partition.integrity_state = device.is_tampered ? MDTP_PARTITION_STATE_INVALID : MDTP_PARTITION_STATE_VALID;
924 ext_partition.page_size = 0; /* Not needed since already validated */
925 ext_partition.image_addr = 0; /* Not needed since already validated */
926 ext_partition.image_size = 0; /* Not needed since already validated */
927 ext_partition.sig_avail = FALSE; /* Not needed since already validated */
928 mdtp_fwlock_verify_lock(&ext_partition);
929 }
930#endif /* MDTP_SUPPORT */
931
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700932#if USE_PCOM_SECBOOT
933 set_tamper_flag(device.is_tampered);
934#endif
935
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700936#if VERIFIED_BOOT
Sridhar Parasuram8b792422015-07-05 11:38:13 -0700937 switch(boot_verify_get_state())
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700938 {
Sridhar Parasuram8b792422015-07-05 11:38:13 -0700939 case RED:
lijuanga40d6302015-07-20 20:10:13 +0800940#if FBCON_DISPLAY_MSG
lijuang9a7d3b92015-11-30 14:41:24 +0800941 display_bootverify_menu(DISPLAY_MENU_RED);
lijuanga40d6302015-07-20 20:10:13 +0800942 wait_for_users_action();
943#else
944 dprintf(CRITICAL,
945 "Your device has failed verification and may not work properly.\nWait for 5 seconds before proceeding\n");
946 mdelay(5000);
947#endif
948
949 break;
Sridhar Parasuram8b792422015-07-05 11:38:13 -0700950 case YELLOW:
lijuanga40d6302015-07-20 20:10:13 +0800951#if FBCON_DISPLAY_MSG
lijuang9a7d3b92015-11-30 14:41:24 +0800952 display_bootverify_menu(DISPLAY_MENU_YELLOW);
lijuanga40d6302015-07-20 20:10:13 +0800953 wait_for_users_action();
954#else
955 dprintf(CRITICAL,
956 "Your device has loaded a different operating system.\nWait for 5 seconds before proceeding\n");
957 mdelay(5000);
958#endif
959 break;
Sridhar Parasuram8b792422015-07-05 11:38:13 -0700960 default:
lijuanga40d6302015-07-20 20:10:13 +0800961 break;
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700962 }
963#endif
Sridhar Parasuram8b792422015-07-05 11:38:13 -0700964#if !VERIFIED_BOOT
Unnati Gandhi1be04752015-03-27 19:41:53 +0530965 if(device.is_tampered)
966 {
967 write_device_info_mmc(&device);
968 #ifdef TZ_TAMPER_FUSE
969 set_tamper_fuse_cmd();
970 #endif
971 #ifdef ASSERT_ON_TAMPER
972 dprintf(CRITICAL, "Device is tampered. Asserting..\n");
973 ASSERT(0);
974 #endif
975 }
Sridhar Parasuram8b792422015-07-05 11:38:13 -0700976#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700977}
978
Maunik Shah0f3c8ac2014-03-02 17:47:58 +0530979static bool check_format_bit()
980{
981 bool ret = false;
982 int index;
983 uint64_t offset;
984 struct boot_selection_info *in = NULL;
985 char *buf = NULL;
986
987 index = partition_get_index("bootselect");
988 if (index == INVALID_PTN)
989 {
990 dprintf(INFO, "Unable to locate /bootselect partition\n");
991 return ret;
992 }
993 offset = partition_get_offset(index);
994 if(!offset)
995 {
996 dprintf(INFO, "partition /bootselect doesn't exist\n");
997 return ret;
998 }
999 buf = (char *) memalign(CACHE_LINE, ROUNDUP(page_size, CACHE_LINE));
1000 ASSERT(buf);
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301001 if (mmc_read(offset, (uint32_t *)buf, page_size))
Maunik Shah0f3c8ac2014-03-02 17:47:58 +05301002 {
1003 dprintf(INFO, "mmc read failure /bootselect %d\n", page_size);
1004 free(buf);
1005 return ret;
1006 }
1007 in = (struct boot_selection_info *) buf;
1008 if ((in->signature == BOOTSELECT_SIGNATURE) &&
1009 (in->version == BOOTSELECT_VERSION)) {
1010 if ((in->state_info & BOOTSELECT_FORMAT) &&
1011 !(in->state_info & BOOTSELECT_FACTORY))
1012 ret = true;
1013 } else {
1014 dprintf(CRITICAL, "Signature: 0x%08x or version: 0x%08x mismatched of /bootselect\n",
1015 in->signature, in->version);
1016 ASSERT(0);
1017 }
1018 free(buf);
1019 return ret;
1020}
1021
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001022void boot_verifier_init()
1023{
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001024 uint32_t boot_state;
1025 /* Check if device unlock */
1026 if(device.is_unlocked)
1027 {
1028 boot_verify_send_event(DEV_UNLOCK);
1029 boot_verify_print_state();
1030 dprintf(CRITICAL, "Device is unlocked! Skipping verification...\n");
1031 return;
1032 }
1033 else
1034 {
1035 boot_verify_send_event(BOOT_INIT);
1036 }
1037
1038 /* Initialize keystore */
1039 boot_state = boot_verify_keystore_init();
1040 if(boot_state == YELLOW)
1041 {
1042 boot_verify_print_state();
1043 dprintf(CRITICAL, "Keystore verification failed! Continuing anyways...\n");
1044 }
1045}
1046
Shashank Mittal23b8f422010-04-16 19:27:21 -07001047int boot_linux_from_mmc(void)
1048{
1049 struct boot_img_hdr *hdr = (void*) buf;
1050 struct boot_img_hdr *uhdr;
1051 unsigned offset = 0;
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07001052 int rcode;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001053 unsigned long long ptn = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001054 int index = INVALID_PTN;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001055
Shashank Mittalcd98d472011-08-02 14:29:24 -07001056 unsigned char *image_addr = 0;
1057 unsigned kernel_actual;
1058 unsigned ramdisk_actual;
1059 unsigned imagesize_actual;
Neeti Desai465491e2012-07-31 12:53:35 -07001060 unsigned second_actual = 0;
Neeti Desai465491e2012-07-31 12:53:35 -07001061
Matthew Qin271927e2015-03-31 22:07:07 -04001062 unsigned int dtb_size = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08001063 unsigned int out_len = 0;
1064 unsigned int out_avai_len = 0;
1065 unsigned char *out_addr = NULL;
1066 uint32_t dtb_offset = 0;
1067 unsigned char *kernel_start_addr = NULL;
1068 unsigned int kernel_size = 0;
1069 int rc;
1070
Neeti Desai465491e2012-07-31 12:53:35 -07001071#if DEVICE_TREE
1072 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -07001073 struct dt_entry dt_entry;
Neeti Desai465491e2012-07-31 12:53:35 -07001074 unsigned dt_table_offset;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001075 uint32_t dt_actual;
Deepa Dinamani19648b42013-09-05 17:05:55 -07001076 uint32_t dt_hdr_size;
Matthew Qin271927e2015-03-31 22:07:07 -04001077 unsigned char *best_match_dt_addr = NULL;
Neeti Desai465491e2012-07-31 12:53:35 -07001078#endif
Matthew Qin49e51fa2015-02-09 10:40:45 +08001079 struct kernel64_hdr *kptr = NULL;
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001080
Maunik Shah0f3c8ac2014-03-02 17:47:58 +05301081 if (check_format_bit())
1082 boot_into_recovery = 1;
1083
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07001084 if (!boot_into_recovery) {
1085 memset(ffbm_mode_string, '\0', sizeof(ffbm_mode_string));
1086 rcode = get_ffbm(ffbm_mode_string, sizeof(ffbm_mode_string));
1087 if (rcode <= 0) {
1088 boot_into_ffbm = false;
1089 if (rcode < 0)
1090 dprintf(CRITICAL,"failed to get ffbm cookie");
1091 } else
1092 boot_into_ffbm = true;
1093 } else
1094 boot_into_ffbm = false;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001095 uhdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
1096 if (!memcmp(uhdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1097 dprintf(INFO, "Unified boot method!\n");
1098 hdr = uhdr;
1099 goto unified_boot;
1100 }
Greg Griscod6250552011-06-29 14:40:23 -07001101 if (!boot_into_recovery) {
Kinson Chikf1a43512011-07-14 11:28:39 -07001102 index = partition_get_index("boot");
1103 ptn = partition_get_offset(index);
1104 if(ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -07001105 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1106 return -1;
1107 }
Kinson Chikf1a43512011-07-14 11:28:39 -07001108 }
1109 else {
1110 index = partition_get_index("recovery");
1111 ptn = partition_get_offset(index);
1112 if(ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -07001113 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
1114 return -1;
1115 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001116 }
Channagoud Kadabief0547c2015-02-10 12:57:38 -08001117 /* Set Lun for boot & recovery partitions */
1118 mmc_set_lun(partition_get_lun(index));
Shashank Mittal23b8f422010-04-16 19:27:21 -07001119
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301120 if (mmc_read(ptn + offset, (uint32_t *) buf, page_size)) {
Shashank Mittal23b8f422010-04-16 19:27:21 -07001121 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
1122 return -1;
1123 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001124
1125 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001126 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Shashank Mittal23b8f422010-04-16 19:27:21 -07001127 return -1;
1128 }
1129
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07001130 if (hdr->page_size && (hdr->page_size != page_size)) {
vijay kumar2e21b3a2014-06-26 17:40:15 +05301131
1132 if (hdr->page_size > BOOT_IMG_MAX_PAGE_SIZE) {
1133 dprintf(CRITICAL, "ERROR: Invalid page size\n");
1134 return -1;
1135 }
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07001136 page_size = hdr->page_size;
1137 page_mask = page_size - 1;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001138 }
1139
vijay kumar287bb542015-09-29 13:01:52 +05301140 /* ensure commandline is terminated */
1141 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
1142
Matthew Qin49e51fa2015-02-09 10:40:45 +08001143 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1144 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001145
Matthew Qin49e51fa2015-02-09 10:40:45 +08001146 image_addr = (unsigned char *)target_get_scratch_address();
1147
1148#if DEVICE_TREE
1149 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
1150 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + dt_actual);
1151#else
1152 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
1153#endif
1154
1155#if VERIFIED_BOOT
1156 boot_verifier_init();
1157#endif
1158
Vijay Kumar Pendoti9c002ad2016-03-09 13:52:45 +05301159 if (check_aboot_addr_range_overlap((uintptr_t) image_addr, imagesize_actual))
Matthew Qin49e51fa2015-02-09 10:40:45 +08001160 {
1161 dprintf(CRITICAL, "Boot image buffer address overlaps with aboot addresses.\n");
1162 return -1;
1163 }
1164
Matthew Qinbb7923d2015-02-09 10:56:09 +08001165 /*
1166 * Update loading flow of bootimage to support compressed/uncompressed
1167 * bootimage on both 64bit and 32bit platform.
1168 * 1. Load bootimage from emmc partition onto DDR.
1169 * 2. Check if bootimage is gzip format. If yes, decompress compressed kernel
1170 * 3. Check kernel header and update kernel load addr for 64bit and 32bit
1171 * platform accordingly.
1172 * 4. Sanity Check on kernel_addr and ramdisk_addr and copy data.
1173 */
1174
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001175 dprintf(INFO, "Loading (%s) image (%d): start\n",
1176 (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001177 bs_set_timestamp(BS_KERNEL_LOAD_START);
1178
Gaurav Nebhwani43e2a462016-03-17 21:32:56 +05301179 if ((target_get_max_flash_size() - page_size) < imagesize_actual)
1180 {
1181 dprintf(CRITICAL, "booimage size is greater than DDR can hold\n");
1182 return -1;
1183 }
1184
Matthew Qinbb7923d2015-02-09 10:56:09 +08001185 /* Read image without signature */
Matthew Qin49e51fa2015-02-09 10:40:45 +08001186 if (mmc_read(ptn + offset, (void *)image_addr, imagesize_actual))
1187 {
1188 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
1189 return -1;
1190 }
1191
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001192 dprintf(INFO, "Loading (%s) image (%d): done\n",
1193 (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual);
1194
Matthew Qin49e51fa2015-02-09 10:40:45 +08001195 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
1196
1197 /* Authenticate Kernel */
1198 dprintf(INFO, "use_signed_kernel=%d, is_unlocked=%d, is_tampered=%d.\n",
1199 (int) target_use_signed_kernel(),
1200 device.is_unlocked,
1201 device.is_tampered);
1202
Channagoud Kadabi736c4962015-08-21 11:56:52 -07001203 /* Change the condition a little bit to include the test framework support.
1204 * We would never reach this point if device is in fastboot mode, even if we did
1205 * that means we are in test mode, so execute kernel authentication part for the
1206 * tests */
Parth Dixitcb0c6082015-12-30 15:01:13 +05301207 if((target_use_signed_kernel() && (!device.is_unlocked)) || is_test_mode_enabled())
Matthew Qin49e51fa2015-02-09 10:40:45 +08001208 {
1209 offset = imagesize_actual;
Vijay Kumar Pendoti9c002ad2016-03-09 13:52:45 +05301210 if (check_aboot_addr_range_overlap((uintptr_t)image_addr + offset, page_size))
Matthew Qin49e51fa2015-02-09 10:40:45 +08001211 {
1212 dprintf(CRITICAL, "Signature read buffer address overlaps with aboot addresses.\n");
1213 return -1;
1214 }
1215
1216 /* Read signature */
1217 if(mmc_read(ptn + offset, (void *)(image_addr + offset), page_size))
1218 {
1219 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
1220 return -1;
1221 }
1222
1223 verify_signed_bootimg((uint32_t)image_addr, imagesize_actual);
Channagoud Kadabi736c4962015-08-21 11:56:52 -07001224 /* The purpose of our test is done here */
Parth Dixitcb0c6082015-12-30 15:01:13 +05301225 if(is_test_mode_enabled() && auth_kernel_img)
Channagoud Kadabi736c4962015-08-21 11:56:52 -07001226 return 0;
Matthew Qin49e51fa2015-02-09 10:40:45 +08001227 } else {
1228 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
1229 #ifdef TZ_SAVE_KERNEL_HASH
1230 aboot_save_boot_hash_mmc((uint32_t) image_addr, imagesize_actual);
1231 #endif /* TZ_SAVE_KERNEL_HASH */
Amit Blay4aa292f2015-04-28 21:55:59 +03001232
1233#ifdef MDTP_SUPPORT
1234 {
1235 /* Verify MDTP lock.
1236 * For boot & recovery partitions, MDTP will use boot_verifier APIs,
1237 * since verification was skipped in aboot. The signature is not part of the loaded image.
1238 */
1239 mdtp_ext_partition_verification_t ext_partition;
1240 ext_partition.partition = boot_into_recovery ? MDTP_PARTITION_RECOVERY : MDTP_PARTITION_BOOT;
1241 ext_partition.integrity_state = MDTP_PARTITION_STATE_UNSET;
1242 ext_partition.page_size = page_size;
1243 ext_partition.image_addr = (uint32)image_addr;
1244 ext_partition.image_size = imagesize_actual;
1245 ext_partition.sig_avail = FALSE;
1246 mdtp_fwlock_verify_lock(&ext_partition);
1247 }
1248#endif /* MDTP_SUPPORT */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001249 }
1250
Sridhar Parasuram00bfedb2015-05-26 14:21:27 -07001251#if VERIFIED_BOOT
Reut Zysmanba8a9d52016-02-18 11:44:04 +02001252 if(boot_verify_get_state() == ORANGE)
1253 {
1254#if FBCON_DISPLAY_MSG
1255 display_bootverify_menu(DISPLAY_MENU_ORANGE);
1256 wait_for_users_action();
1257#else
1258 dprintf(CRITICAL,
1259 "Your device has been unlocked and can't be trusted.\nWait for 5 seconds before proceeding\n");
1260 mdelay(5000);
1261#endif
1262 }
1263#endif
1264
1265#if VERIFIED_BOOT
Parth Dixitbc9b6492015-10-18 00:41:38 +05301266#if !VBOOT_MOTA
Sridhar Parasuram00bfedb2015-05-26 14:21:27 -07001267 // send root of trust
Sridhar Parasuram96300dc2015-06-11 10:37:11 -07001268 if(!send_rot_command((uint32_t)device.is_unlocked))
Sridhar Parasuram00bfedb2015-05-26 14:21:27 -07001269 ASSERT(0);
1270#endif
Parth Dixitbc9b6492015-10-18 00:41:38 +05301271#endif
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001272 /*
Matthew Qinbb7923d2015-02-09 10:56:09 +08001273 * Check if the kernel image is a gzip package. If yes, need to decompress it.
1274 * If not, continue booting.
1275 */
1276 if (is_gzip_package((unsigned char *)(image_addr + page_size), hdr->kernel_size))
1277 {
1278 out_addr = (unsigned char *)(image_addr + imagesize_actual + page_size);
1279 out_avai_len = target_get_max_flash_size() - imagesize_actual - page_size;
Matthew Qin0b15b322015-05-19 05:20:54 -04001280 dprintf(INFO, "decompressing kernel image: start\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001281 rc = decompress((unsigned char *)(image_addr + page_size),
1282 hdr->kernel_size, out_addr, out_avai_len,
1283 &dtb_offset, &out_len);
1284 if (rc)
1285 {
Matthew Qin0b15b322015-05-19 05:20:54 -04001286 dprintf(CRITICAL, "decompressing kernel image failed!!!\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001287 ASSERT(0);
1288 }
1289
Matthew Qin0b15b322015-05-19 05:20:54 -04001290 dprintf(INFO, "decompressing kernel image: done\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001291 kptr = (struct kernel64_hdr *)out_addr;
1292 kernel_start_addr = out_addr;
1293 kernel_size = out_len;
1294 } else {
1295 kptr = (struct kernel64_hdr *)(image_addr + page_size);
1296 kernel_start_addr = (unsigned char *)(image_addr + page_size);
1297 kernel_size = hdr->kernel_size;
1298 }
1299
1300 /*
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001301 * Update the kernel/ramdisk/tags address if the boot image header
1302 * has default values, these default values come from mkbootimg when
1303 * the boot image is flashed using fastboot flash:raw
1304 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001305 update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr));
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001306
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001307 /* Get virtual addresses since the hdr saves physical addresses. */
1308 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
1309 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
1310 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001311
Matthew Qinbb7923d2015-02-09 10:56:09 +08001312 kernel_size = ROUND_TO_PAGE(kernel_size, page_mask);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001313 /* Check if the addresses in the header are valid. */
Matthew Qinbb7923d2015-02-09 10:56:09 +08001314 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_size) ||
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001315 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
1316 {
1317 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
1318 return -1;
1319 }
1320
1321#ifndef DEVICE_TREE
1322 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1323 {
1324 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
1325 return -1;
1326 }
1327#endif
1328
Matthew Qin49e51fa2015-02-09 10:40:45 +08001329 /* Move kernel, ramdisk and device tree to correct address */
Matthew Qinbb7923d2015-02-09 10:56:09 +08001330 memmove((void*) hdr->kernel_addr, kernel_start_addr, kernel_size);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001331 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001332
Matthew Qin49e51fa2015-02-09 10:40:45 +08001333 #if DEVICE_TREE
1334 if(hdr->dt_size) {
1335 dt_table_offset = ((uint32_t)image_addr + page_size + kernel_actual + ramdisk_actual + second_actual);
1336 table = (struct dt_table*) dt_table_offset;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001337
Matthew Qin49e51fa2015-02-09 10:40:45 +08001338 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
1339 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1340 return -1;
1341 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001342
P.V. Phani Kumar82916762016-03-09 09:20:19 +05301343 /* Its Error if, dt_hdr_size (table->num_entries * dt_entry size + Dev_Tree Header)
1344 goes beyound hdr->dt_size*/
1345 if (dt_hdr_size > ROUND_TO_PAGE(hdr->dt_size,hdr->page_size)) {
1346 dprintf(CRITICAL, "ERROR: Invalid Device Tree size \n");
1347 return -1;
1348 }
1349
Matthew Qin49e51fa2015-02-09 10:40:45 +08001350 /* Find index of device tree within device tree table */
1351 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
1352 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1353 return -1;
1354 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001355
P.V. Phani Kumar3c333202016-03-09 11:54:37 +05301356 if(dt_entry.offset > (UINT_MAX - dt_entry.size)) {
1357 dprintf(CRITICAL, "ERROR: Device tree contents are Invalid\n");
1358 return -1;
1359 }
1360
1361 /* Ensure we are not overshooting dt_size with the dt_entry selected */
1362 if ((dt_entry.offset + dt_entry.size) > hdr->dt_size) {
1363 dprintf(CRITICAL, "ERROR: Device tree contents are Invalid\n");
1364 return -1;
1365 }
1366
Matthew Qin271927e2015-03-31 22:07:07 -04001367 if (is_gzip_package((unsigned char *)dt_table_offset + dt_entry.offset, dt_entry.size))
1368 {
1369 unsigned int compressed_size = 0;
1370 out_addr += out_len;
1371 out_avai_len -= out_len;
Matthew Qin0b15b322015-05-19 05:20:54 -04001372 dprintf(INFO, "decompressing dtb: start\n");
Matthew Qin271927e2015-03-31 22:07:07 -04001373 rc = decompress((unsigned char *)dt_table_offset + dt_entry.offset,
1374 dt_entry.size, out_addr, out_avai_len,
1375 &compressed_size, &dtb_size);
1376 if (rc)
1377 {
Matthew Qin0b15b322015-05-19 05:20:54 -04001378 dprintf(CRITICAL, "decompressing dtb failed!!!\n");
Matthew Qin271927e2015-03-31 22:07:07 -04001379 ASSERT(0);
1380 }
1381
Matthew Qin0b15b322015-05-19 05:20:54 -04001382 dprintf(INFO, "decompressing dtb: done\n");
Matthew Qin271927e2015-03-31 22:07:07 -04001383 best_match_dt_addr = out_addr;
1384 } else {
1385 best_match_dt_addr = (unsigned char *)dt_table_offset + dt_entry.offset;
1386 dtb_size = dt_entry.size;
1387 }
1388
Matthew Qin49e51fa2015-02-09 10:40:45 +08001389 /* Validate and Read device device tree in the tags_addr */
Matthew Qin271927e2015-03-31 22:07:07 -04001390 if (check_aboot_addr_range_overlap(hdr->tags_addr, dtb_size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001391 {
1392 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1393 return -1;
1394 }
Shashank Mittal162244e2011-08-08 19:01:25 -07001395
Matthew Qin271927e2015-03-31 22:07:07 -04001396 memmove((void *)hdr->tags_addr, (char *)best_match_dt_addr, dtb_size);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001397 } else {
1398 /* Validate the tags_addr */
1399 if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual))
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001400 {
1401 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1402 return -1;
1403 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08001404 /*
1405 * If appended dev tree is found, update the atags with
1406 * memory address to the DTB appended location on RAM.
1407 * Else update with the atags address in the kernel header
1408 */
1409 void *dtb;
Matthew Qinbb7923d2015-02-09 10:56:09 +08001410 dtb = dev_tree_appended((void*)(image_addr + page_size),
1411 hdr->kernel_size, dtb_offset,
Matthew Qin49e51fa2015-02-09 10:40:45 +08001412 (void *)hdr->tags_addr);
1413 if (!dtb) {
1414 dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001415 return -1;
1416 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07001417 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08001418 #endif
Shashank Mittal23b8f422010-04-16 19:27:21 -07001419
Stanimir Varbanov69ec5462013-07-18 18:17:42 +03001420 if (boot_into_recovery && !device.is_unlocked && !device.is_tampered)
1421 target_load_ssd_keystore();
1422
Shashank Mittal23b8f422010-04-16 19:27:21 -07001423unified_boot:
Shashank Mittal23b8f422010-04-16 19:27:21 -07001424
Dima Zavin77e41f32013-03-06 16:10:43 -08001425 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07001426 (const char *)hdr->cmdline, board_machtype(),
Shashank Mittal23b8f422010-04-16 19:27:21 -07001427 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
1428
1429 return 0;
1430}
1431
Dima Zavin214cc642009-01-26 11:16:21 -08001432int boot_linux_from_flash(void)
1433{
1434 struct boot_img_hdr *hdr = (void*) buf;
Dima Zavin214cc642009-01-26 11:16:21 -08001435 struct ptentry *ptn;
1436 struct ptable *ptable;
1437 unsigned offset = 0;
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001438
Shashank Mittalcd98d472011-08-02 14:29:24 -07001439 unsigned char *image_addr = 0;
1440 unsigned kernel_actual;
1441 unsigned ramdisk_actual;
1442 unsigned imagesize_actual;
vijay kumar8f53e362015-11-24 13:38:11 +05301443 unsigned second_actual = 0;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001444
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001445#if DEVICE_TREE
1446 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -07001447 struct dt_entry dt_entry;
vijay kumar8f53e362015-11-24 13:38:11 +05301448 unsigned dt_table_offset;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001449 uint32_t dt_actual;
Deepa Dinamani19648b42013-09-05 17:05:55 -07001450 uint32_t dt_hdr_size;
vijay kumar8f53e362015-11-24 13:38:11 +05301451 unsigned int dtb_size = 0;
1452 unsigned char *best_match_dt_addr = NULL;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001453#endif
1454
David Ng183a7422009-12-07 14:55:21 -08001455 if (target_is_emmc_boot()) {
1456 hdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
1457 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1458 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
1459 return -1;
1460 }
1461 goto continue_boot;
1462 }
1463
Dima Zavin214cc642009-01-26 11:16:21 -08001464 ptable = flash_get_ptable();
1465 if (ptable == NULL) {
1466 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1467 return -1;
1468 }
1469
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001470 if(!boot_into_recovery)
1471 {
1472 ptn = ptable_find(ptable, "boot");
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001473
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001474 if (ptn == NULL) {
1475 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1476 return -1;
1477 }
1478 }
1479 else
1480 {
1481 ptn = ptable_find(ptable, "recovery");
1482 if (ptn == NULL) {
1483 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
1484 return -1;
1485 }
Dima Zavin214cc642009-01-26 11:16:21 -08001486 }
1487
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001488 if (flash_read(ptn, offset, buf, page_size)) {
Dima Zavin214cc642009-01-26 11:16:21 -08001489 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
1490 return -1;
1491 }
Dima Zavin214cc642009-01-26 11:16:21 -08001492
1493 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001494 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Dima Zavin214cc642009-01-26 11:16:21 -08001495 return -1;
1496 }
1497
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001498 if (hdr->page_size != page_size) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001499 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 -08001500 return -1;
1501 }
1502
vijay kumar287bb542015-09-29 13:01:52 +05301503 /* ensure commandline is terminated */
1504 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
1505
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001506 /*
1507 * Update the kernel/ramdisk/tags address if the boot image header
1508 * has default values, these default values come from mkbootimg when
1509 * the boot image is flashed using fastboot flash:raw
1510 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001511 update_ker_tags_rdisk_addr(hdr, false);
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001512
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001513 /* Get virtual addresses since the hdr saves physical addresses. */
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001514 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
1515 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
1516 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
1517
1518 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1519 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1520
1521 /* Check if the addresses in the header are valid. */
1522 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
1523 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
1524 {
1525 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
1526 return -1;
1527 }
1528
1529#ifndef DEVICE_TREE
1530 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1531 {
1532 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
1533 return -1;
1534 }
1535#endif
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001536
Shashank Mittalcd98d472011-08-02 14:29:24 -07001537 /* Authenticate Kernel */
Deepa Dinamani23b60d42013-06-24 18:10:52 -07001538 if(target_use_signed_kernel() && (!device.is_unlocked))
Shashank Mittalcd98d472011-08-02 14:29:24 -07001539 {
1540 image_addr = (unsigned char *)target_get_scratch_address();
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001541 offset = 0;
1542
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001543#if DEVICE_TREE
1544 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
vijay kumar966a9822015-12-09 18:36:09 +05301545
1546 if (UINT_MAX < ((uint64_t)kernel_actual + (uint64_t)ramdisk_actual+ (uint64_t)dt_actual + page_size)) {
1547 dprintf(CRITICAL, "Integer overflow detected in bootimage header fields\n");
1548 return -1;
1549 }
1550
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001551 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + dt_actual);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001552
1553 if (check_aboot_addr_range_overlap(hdr->tags_addr, hdr->dt_size))
1554 {
1555 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1556 return -1;
1557 }
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001558#else
vijay kumar966a9822015-12-09 18:36:09 +05301559 if (UINT_MAX < ((uint64_t)kernel_actual + (uint64_t)ramdisk_actual+ page_size)) {
1560 dprintf(CRITICAL, "Integer overflow detected in bootimage header fields\n");
1561 return -1;
1562 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07001563 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001564#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001565
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001566 dprintf(INFO, "Loading (%s) image (%d): start\n",
1567 (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001568 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -08001569
Shashank Mittalcd98d472011-08-02 14:29:24 -07001570 /* Read image without signature */
1571 if (flash_read(ptn, offset, (void *)image_addr, imagesize_actual))
1572 {
1573 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
1574 return -1;
1575 }
Dima Zavin214cc642009-01-26 11:16:21 -08001576
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001577 dprintf(INFO, "Loading (%s) image (%d): done\n",
1578 (!boot_into_recovery ? "boot" : "recovery"), imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001579 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Amol Jadib6be5c12012-11-14 13:39:51 -08001580
Shashank Mittalcd98d472011-08-02 14:29:24 -07001581 offset = imagesize_actual;
1582 /* Read signature */
1583 if (flash_read(ptn, offset, (void *)(image_addr + offset), page_size))
1584 {
1585 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001586 return -1;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001587 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07001588
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301589 verify_signed_bootimg((uint32_t)image_addr, imagesize_actual);
Shashank Mittalcd98d472011-08-02 14:29:24 -07001590
1591 /* Move kernel and ramdisk to correct address */
Matthew Qinbb7923d2015-02-09 10:56:09 +08001592 memmove((void*) hdr->kernel_addr, (char*) (image_addr + page_size), hdr->kernel_size);
1593 memmove((void*) hdr->ramdisk_addr, (char*) (image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001594#if DEVICE_TREE
vijay kumar8f53e362015-11-24 13:38:11 +05301595 if(hdr->dt_size != 0) {
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001596
vijay kumar8f53e362015-11-24 13:38:11 +05301597 dt_table_offset = ((uint32_t)image_addr + page_size + kernel_actual + ramdisk_actual + second_actual);
1598
1599 table = (struct dt_table*) dt_table_offset;
1600
1601 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0){
1602 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1603 return -1;
1604 }
1605
1606 /* Find index of device tree within device tree table */
1607 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
1608 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1609 return -1;
1610 }
1611
1612 /* Validate and Read device device tree in the "tags_add */
1613 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size)){
1614 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1615 return -1;
1616 }
1617
1618 best_match_dt_addr = (unsigned char *)table + dt_entry.offset;
1619 dtb_size = dt_entry.size;
1620 memmove((void *)hdr->tags_addr, (char *)best_match_dt_addr, dtb_size);
1621 }
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001622#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001623
1624 /* Make sure everything from scratch address is read before next step!*/
Shashank Mittala0032282011-08-26 14:50:11 -07001625 if(device.is_tampered)
Shashank Mittal162244e2011-08-08 19:01:25 -07001626 {
1627 write_device_info_flash(&device);
1628 }
Channagoud Kadabi5c86fe32012-02-16 10:58:48 +05301629#if USE_PCOM_SECBOOT
1630 set_tamper_flag(device.is_tampered);
1631#endif
Shashank Mittalcd98d472011-08-02 14:29:24 -07001632 }
1633 else
1634 {
Shashank Mittal162244e2011-08-08 19:01:25 -07001635 offset = page_size;
1636
Amol Jadib6be5c12012-11-14 13:39:51 -08001637 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1638 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1639 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
1640
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001641 dprintf(INFO, "Loading (%s) image (%d): start\n",
1642 (!boot_into_recovery ? "boot" : "recovery"), kernel_actual + ramdisk_actual);
1643
Amol Jadi492d5a52013-03-15 16:12:34 -07001644 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -08001645
1646 if (flash_read(ptn, offset, (void *)hdr->kernel_addr, kernel_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -07001647 dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
1648 return -1;
1649 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001650 offset += kernel_actual;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001651
Amol Jadib6be5c12012-11-14 13:39:51 -08001652 if (flash_read(ptn, offset, (void *)hdr->ramdisk_addr, ramdisk_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -07001653 dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
1654 return -1;
1655 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001656 offset += ramdisk_actual;
1657
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001658 dprintf(INFO, "Loading (%s) image (%d): done\n",
1659 (!boot_into_recovery ? "boot" : "recovery"), kernel_actual + ramdisk_actual);
1660
Amol Jadi492d5a52013-03-15 16:12:34 -07001661 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001662
1663 if(hdr->second_size != 0) {
Amol Jadib6be5c12012-11-14 13:39:51 -08001664 offset += second_actual;
1665 /* Second image loading not implemented. */
1666 ASSERT(0);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001667 }
1668
1669#if DEVICE_TREE
1670 if(hdr->dt_size != 0) {
1671
1672 /* Read the device tree table into buffer */
1673 if(flash_read(ptn, offset, (void *) dt_buf, page_size)) {
1674 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
1675 return -1;
1676 }
1677
1678 table = (struct dt_table*) dt_buf;
1679
Deepa Dinamani19648b42013-09-05 17:05:55 -07001680 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001681 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1682 return -1;
1683 }
1684
P.V. Phani Kumar82916762016-03-09 09:20:19 +05301685 /* Its Error if, dt_hdr_size (table->num_entries * dt_entry size + Dev_Tree Header)
1686 goes beyound hdr->dt_size*/
1687 if (dt_hdr_size > ROUND_TO_PAGE(hdr->dt_size,hdr->page_size)) {
1688 dprintf(CRITICAL, "ERROR: Invalid Device Tree size \n");
1689 return -1;
1690 }
1691
Deepa Dinamani19648b42013-09-05 17:05:55 -07001692 table = (struct dt_table*) memalign(CACHE_LINE, dt_hdr_size);
1693 if (!table)
1694 return -1;
1695
1696 /* Read the entire device tree table into buffer */
1697 if(flash_read(ptn, offset, (void *)table, dt_hdr_size)) {
1698 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
1699 return -1;
1700 }
1701
1702
Joel Kingaa335dc2013-06-03 16:11:08 -07001703 /* Find index of device tree within device tree table */
1704 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001705 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1706 return -1;
1707 }
1708
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001709 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001710 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001711 {
1712 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1713 return -1;
1714 }
1715
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001716 /* Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001717 if(flash_read(ptn, offset + dt_entry.offset,
1718 (void *)hdr->tags_addr, dt_entry.size)) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001719 dprintf(CRITICAL, "ERROR: Cannot read device tree\n");
1720 return -1;
1721 }
1722 }
1723#endif
1724
Shashank Mittalcd98d472011-08-02 14:29:24 -07001725 }
David Ng183a7422009-12-07 14:55:21 -08001726continue_boot:
Dima Zavin214cc642009-01-26 11:16:21 -08001727
Dima Zavin214cc642009-01-26 11:16:21 -08001728 /* TODO: create/pass atags to kernel */
1729
Ajay Dudanie28a6072011-07-01 13:59:46 -07001730 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07001731 (const char *)hdr->cmdline, board_machtype(),
Dima Zavin214cc642009-01-26 11:16:21 -08001732 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
1733
1734 return 0;
1735}
Brian Swetland9c4c0752009-01-25 16:23:50 -08001736
Shashank Mittal162244e2011-08-08 19:01:25 -07001737void write_device_info_mmc(device_info *dev)
1738{
Shashank Mittal162244e2011-08-08 19:01:25 -07001739 unsigned long long ptn = 0;
1740 unsigned long long size;
1741 int index = INVALID_PTN;
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001742 uint32_t blocksize;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001743 uint8_t lun = 0;
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001744 uint32_t ret = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001745
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001746 if (devinfo_present)
1747 index = partition_get_index("devinfo");
1748 else
1749 index = partition_get_index("aboot");
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001750
Shashank Mittal162244e2011-08-08 19:01:25 -07001751 ptn = partition_get_offset(index);
1752 if(ptn == 0)
1753 {
1754 return;
1755 }
1756
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001757 lun = partition_get_lun(index);
1758 mmc_set_lun(lun);
1759
Shashank Mittal162244e2011-08-08 19:01:25 -07001760 size = partition_get_size(index);
1761
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001762 blocksize = mmc_get_device_blocksize();
1763
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001764 if (devinfo_present)
1765 ret = mmc_write(ptn, blocksize, (void *)info_buf);
1766 else
1767 ret = mmc_write((ptn + size - blocksize), blocksize, (void *)info_buf);
1768 if (ret)
Shashank Mittal162244e2011-08-08 19:01:25 -07001769 {
1770 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
Sridhar Parasurame1a97922015-07-27 15:31:22 -07001771 ASSERT(0);
Shashank Mittal162244e2011-08-08 19:01:25 -07001772 }
1773}
1774
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001775void read_device_info_mmc(struct device_info *info)
Shashank Mittal162244e2011-08-08 19:01:25 -07001776{
Shashank Mittal162244e2011-08-08 19:01:25 -07001777 unsigned long long ptn = 0;
1778 unsigned long long size;
1779 int index = INVALID_PTN;
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001780 uint32_t blocksize;
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001781 uint32_t ret = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001782
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001783 if ((index = partition_get_index("devinfo")) < 0)
1784 {
1785 devinfo_present = false;
1786 index = partition_get_index("aboot");
1787 }
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001788
Shashank Mittal162244e2011-08-08 19:01:25 -07001789 ptn = partition_get_offset(index);
1790 if(ptn == 0)
1791 {
1792 return;
1793 }
1794
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001795 mmc_set_lun(partition_get_lun(index));
1796
Shashank Mittal162244e2011-08-08 19:01:25 -07001797 size = partition_get_size(index);
1798
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001799 blocksize = mmc_get_device_blocksize();
1800
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001801 if (devinfo_present)
1802 ret = mmc_read(ptn, (void *)info_buf, blocksize);
1803 else
1804 ret = mmc_read((ptn + size - blocksize), (void *)info_buf, blocksize);
1805 if (ret)
Shashank Mittal162244e2011-08-08 19:01:25 -07001806 {
1807 dprintf(CRITICAL, "ERROR: Cannot read device info\n");
Sridhar Parasurame1a97922015-07-27 15:31:22 -07001808 ASSERT(0);
Shashank Mittal162244e2011-08-08 19:01:25 -07001809 }
Shashank Mittal162244e2011-08-08 19:01:25 -07001810}
1811
1812void write_device_info_flash(device_info *dev)
1813{
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001814 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
Shashank Mittal162244e2011-08-08 19:01:25 -07001815 struct ptentry *ptn;
1816 struct ptable *ptable;
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001817 if(info == NULL)
1818 {
1819 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
1820 ASSERT(0);
1821 }
1822 info_buf = info;
Shashank Mittal162244e2011-08-08 19:01:25 -07001823 ptable = flash_get_ptable();
1824 if (ptable == NULL)
1825 {
1826 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1827 return;
1828 }
1829
1830 ptn = ptable_find(ptable, "devinfo");
1831 if (ptn == NULL)
1832 {
Smita Ghosh670c8b82015-05-07 09:30:03 -07001833 dprintf(CRITICAL, "ERROR: No devinfo partition found\n");
Shashank Mittal162244e2011-08-08 19:01:25 -07001834 return;
1835 }
1836
1837 memcpy(info, dev, sizeof(device_info));
1838
1839 if (flash_write(ptn, 0, (void *)info_buf, page_size))
1840 {
1841 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1842 return;
1843 }
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001844 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07001845}
1846
vijay kumarc65876c2015-04-24 13:29:16 +05301847static int read_allow_oem_unlock(device_info *dev)
1848{
vijay kumarc65876c2015-04-24 13:29:16 +05301849 unsigned offset;
1850 int index;
1851 unsigned long long ptn;
1852 unsigned long long ptn_size;
1853 unsigned blocksize = mmc_get_device_blocksize();
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07001854 STACKBUF_DMA_ALIGN(buf, blocksize);
vijay kumarc65876c2015-04-24 13:29:16 +05301855
vijay kumarca2e6812015-07-08 20:28:25 +05301856 index = partition_get_index(frp_ptns[0]);
vijay kumarc65876c2015-04-24 13:29:16 +05301857 if (index == INVALID_PTN)
1858 {
vijay kumarca2e6812015-07-08 20:28:25 +05301859 index = partition_get_index(frp_ptns[1]);
1860 if (index == INVALID_PTN)
1861 {
1862 dprintf(CRITICAL, "Neither '%s' nor '%s' partition found\n", frp_ptns[0],frp_ptns[1]);
1863 return -1;
1864 }
vijay kumarc65876c2015-04-24 13:29:16 +05301865 }
1866
1867 ptn = partition_get_offset(index);
1868 ptn_size = partition_get_size(index);
1869 offset = ptn_size - blocksize;
1870
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07001871 if (mmc_read(ptn + offset, (void *)buf, blocksize))
vijay kumarc65876c2015-04-24 13:29:16 +05301872 {
1873 dprintf(CRITICAL, "Reading MMC failed\n");
1874 return -1;
1875 }
1876
1877 /*is_allow_unlock is a bool value stored at the LSB of last byte*/
1878 is_allow_unlock = buf[blocksize-1] & 0x01;
1879 return 0;
1880}
1881
1882static int write_allow_oem_unlock(bool allow_unlock)
1883{
vijay kumarc65876c2015-04-24 13:29:16 +05301884 unsigned offset;
vijay kumarc65876c2015-04-24 13:29:16 +05301885 int index;
1886 unsigned long long ptn;
1887 unsigned long long ptn_size;
1888 unsigned blocksize = mmc_get_device_blocksize();
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07001889 STACKBUF_DMA_ALIGN(buf, blocksize);
vijay kumarc65876c2015-04-24 13:29:16 +05301890
vijay kumarca2e6812015-07-08 20:28:25 +05301891 index = partition_get_index(frp_ptns[0]);
vijay kumarc65876c2015-04-24 13:29:16 +05301892 if (index == INVALID_PTN)
1893 {
vijay kumarca2e6812015-07-08 20:28:25 +05301894 index = partition_get_index(frp_ptns[1]);
1895 if (index == INVALID_PTN)
1896 {
1897 dprintf(CRITICAL, "Neither '%s' nor '%s' partition found\n", frp_ptns[0],frp_ptns[1]);
1898 return -1;
1899 }
vijay kumarc65876c2015-04-24 13:29:16 +05301900 }
1901
1902 ptn = partition_get_offset(index);
1903 ptn_size = partition_get_size(index);
1904 offset = ptn_size - blocksize;
1905
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07001906 if (mmc_read(ptn + offset, (void *)buf, blocksize))
vijay kumarc65876c2015-04-24 13:29:16 +05301907 {
1908 dprintf(CRITICAL, "Reading MMC failed\n");
1909 return -1;
1910 }
1911
1912 /*is_allow_unlock is a bool value stored at the LSB of last byte*/
1913 buf[blocksize-1] = allow_unlock;
1914 if (mmc_write(ptn + offset, blocksize, buf))
1915 {
1916 dprintf(CRITICAL, "Writing MMC failed\n");
1917 return -1;
1918 }
1919
1920 return 0;
1921}
1922
Shashank Mittal162244e2011-08-08 19:01:25 -07001923void read_device_info_flash(device_info *dev)
1924{
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001925 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
Shashank Mittal162244e2011-08-08 19:01:25 -07001926 struct ptentry *ptn;
1927 struct ptable *ptable;
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001928 if(info == NULL)
1929 {
1930 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
1931 ASSERT(0);
1932 }
1933 info_buf = info;
Shashank Mittal162244e2011-08-08 19:01:25 -07001934 ptable = flash_get_ptable();
1935 if (ptable == NULL)
1936 {
1937 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1938 return;
1939 }
1940
1941 ptn = ptable_find(ptable, "devinfo");
1942 if (ptn == NULL)
1943 {
Smita Ghosh670c8b82015-05-07 09:30:03 -07001944 dprintf(CRITICAL, "ERROR: No devinfo partition found\n");
Shashank Mittal162244e2011-08-08 19:01:25 -07001945 return;
1946 }
1947
1948 if (flash_read(ptn, 0, (void *)info_buf, page_size))
1949 {
1950 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1951 return;
1952 }
1953
1954 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
1955 {
Shashank Mittal162244e2011-08-08 19:01:25 -07001956 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
1957 info->is_unlocked = 0;
Shashank Mittala0032282011-08-26 14:50:11 -07001958 info->is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001959 write_device_info_flash(info);
1960 }
1961 memcpy(dev, info, sizeof(device_info));
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001962 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07001963}
1964
1965void write_device_info(device_info *dev)
1966{
1967 if(target_is_emmc_boot())
1968 {
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001969 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
1970 if(info == NULL)
1971 {
1972 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
1973 ASSERT(0);
1974 }
1975 info_buf = info;
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001976 memcpy(info, dev, sizeof(struct device_info));
1977
1978#if USE_RPMB_FOR_DEVINFO
Sridhar Parasurame1a97922015-07-27 15:31:22 -07001979 if (is_secure_boot_enable()) {
1980 if((write_device_info_rpmb((void*) info, PAGE_SIZE)) < 0)
1981 ASSERT(0);
1982 }
Channagoud Kadabic80cb492015-04-28 16:08:28 -07001983 else
1984 write_device_info_mmc(info);
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001985#else
1986 write_device_info_mmc(info);
1987#endif
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001988 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07001989 }
1990 else
1991 {
1992 write_device_info_flash(dev);
1993 }
1994}
1995
1996void read_device_info(device_info *dev)
1997{
1998 if(target_is_emmc_boot())
1999 {
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002000 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
2001 if(info == NULL)
2002 {
2003 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
2004 ASSERT(0);
2005 }
2006 info_buf = info;
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002007
2008#if USE_RPMB_FOR_DEVINFO
Sridhar Parasurame1a97922015-07-27 15:31:22 -07002009 if (is_secure_boot_enable()) {
2010 if((read_device_info_rpmb((void*) info, PAGE_SIZE)) < 0)
2011 ASSERT(0);
2012 }
Channagoud Kadabic80cb492015-04-28 16:08:28 -07002013 else
2014 read_device_info_mmc(info);
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002015#else
2016 read_device_info_mmc(info);
2017#endif
2018
2019 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
2020 {
2021 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
lijuang511a2b52015-08-14 20:50:51 +08002022 if (is_secure_boot_enable()) {
Channagoud Kadabi05f78ba2015-07-06 11:58:14 -07002023 info->is_unlocked = 0;
Parth Dixitddbc7352015-10-18 03:13:31 +05302024#if !VBOOT_MOTA
lijuang511a2b52015-08-14 20:50:51 +08002025 info->is_unlock_critical = 0;
Parth Dixitddbc7352015-10-18 03:13:31 +05302026#endif
lijuang511a2b52015-08-14 20:50:51 +08002027 } else {
Channagoud Kadabi2fda4092015-07-07 13:34:11 -07002028 info->is_unlocked = 1;
Parth Dixitddbc7352015-10-18 03:13:31 +05302029#if !VBOOT_MOTA
lijuang511a2b52015-08-14 20:50:51 +08002030 info->is_unlock_critical = 1;
Parth Dixitddbc7352015-10-18 03:13:31 +05302031#endif
lijuang511a2b52015-08-14 20:50:51 +08002032 }
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002033 info->is_tampered = 0;
2034 info->charger_screen_enabled = 0;
Parth Dixitddbc7352015-10-18 03:13:31 +05302035#if !VBOOT_MOTA
Sridhar Parasuram287e5062015-08-24 16:17:22 -07002036 info->verity_mode = 1; //enforcing by default
Parth Dixitddbc7352015-10-18 03:13:31 +05302037#endif
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002038 write_device_info(info);
2039 }
2040 memcpy(dev, info, sizeof(device_info));
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002041 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07002042 }
2043 else
2044 {
2045 read_device_info_flash(dev);
2046 }
2047}
2048
2049void reset_device_info()
2050{
2051 dprintf(ALWAYS, "reset_device_info called.");
Shashank Mittala0032282011-08-26 14:50:11 -07002052 device.is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07002053 write_device_info(&device);
2054}
2055
2056void set_device_root()
2057{
2058 dprintf(ALWAYS, "set_device_root called.");
Shashank Mittala0032282011-08-26 14:50:11 -07002059 device.is_tampered = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -07002060 write_device_info(&device);
2061}
2062
lijuang4ece1e72015-08-14 21:02:36 +08002063/* set device unlock value
2064 * Must check FRP before call this function
2065 * Need to wipe data when unlock status changed
2066 * type 0: oem unlock
2067 * type 1: unlock critical
2068 * status 0: unlock as false
2069 * status 1: lock as true
2070 */
2071void set_device_unlock_value(int type, bool status)
lijuang21f12f52015-08-22 16:22:19 +08002072{
lijuang4ece1e72015-08-14 21:02:36 +08002073 if (type == UNLOCK)
2074 device.is_unlocked = status;
Parth Dixitddbc7352015-10-18 03:13:31 +05302075#if !VBOOT_MOTA
lijuang4ece1e72015-08-14 21:02:36 +08002076 else if (type == UNLOCK_CRITICAL)
2077 device.is_unlock_critical = status;
Parth Dixitddbc7352015-10-18 03:13:31 +05302078#endif
lijuang4ece1e72015-08-14 21:02:36 +08002079 write_device_info(&device);
2080}
2081
2082static void set_device_unlock(int type, bool status)
2083{
2084 int is_unlocked = -1;
2085 char response[MAX_RSP_SIZE];
2086
2087 /* check device unlock status if it is as expected */
2088 if (type == UNLOCK)
2089 is_unlocked = device.is_unlocked;
Parth Dixitddbc7352015-10-18 03:13:31 +05302090#if !VBOOT_MOTA
lijuang4ece1e72015-08-14 21:02:36 +08002091 else if (type == UNLOCK_CRITICAL)
2092 is_unlocked = device.is_unlock_critical;
Parth Dixitddbc7352015-10-18 03:13:31 +05302093#endif
lijuang4ece1e72015-08-14 21:02:36 +08002094 if (is_unlocked == status) {
2095 snprintf(response, sizeof(response), "\tDevice already : %s", (status ? "unlocked!" : "locked!"));
2096 fastboot_info(response);
2097 fastboot_okay("");
2098 return;
2099 }
2100
2101 /* status is true, it means to unlock device */
2102 if (status) {
lijuang21f12f52015-08-22 16:22:19 +08002103 if(!is_allow_unlock) {
2104 fastboot_fail("oem unlock is not allowed");
2105 return;
2106 }
2107
lijuang4ece1e72015-08-14 21:02:36 +08002108#if FBCON_DISPLAY_MSG
lijuang9a7d3b92015-11-30 14:41:24 +08002109 display_unlock_menu(type);
lijuang4ece1e72015-08-14 21:02:36 +08002110 fastboot_okay("");
2111 return;
2112#else
2113 if (type == UNLOCK) {
2114 fastboot_fail("Need wipe userdata. Do 'fastboot oem unlock-go'");
2115 return;
2116 }
2117#endif
lijuang21f12f52015-08-22 16:22:19 +08002118 }
lijuang4ece1e72015-08-14 21:02:36 +08002119
2120 set_device_unlock_value(type, status);
2121
2122 /* wipe data */
2123 struct recovery_message msg;
2124
2125 snprintf(msg.recovery, sizeof(msg.recovery), "recovery\n--wipe_data");
2126 write_misc(0, &msg, sizeof(msg));
2127
2128 fastboot_okay("");
2129 reboot_device(RECOVERY_MODE);
lijuang21f12f52015-08-22 16:22:19 +08002130}
2131
lijuang511a2b52015-08-14 20:50:51 +08002132static bool critical_flash_allowed(const char * entry)
2133{
2134 uint32_t i = 0;
2135 if (entry == NULL)
2136 return false;
2137
2138 for (i = 0; i < ARRAY_SIZE(critical_flash_allowed_ptn); i++) {
2139 if(!strcmp(entry, critical_flash_allowed_ptn[i]))
2140 return true;
2141 }
2142 return false;
Matthew Qin271927e2015-03-31 22:07:07 -04002143}
2144
2145#if DEVICE_TREE
Amol Jadicb524072012-08-09 16:40:18 -07002146int copy_dtb(uint8_t *boot_image_start, unsigned int scratch_offset)
2147{
2148 uint32 dt_image_offset = 0;
Amol Jadicb524072012-08-09 16:40:18 -07002149 uint32_t n;
Amol Jadicb524072012-08-09 16:40:18 -07002150 struct dt_table *table;
2151 struct dt_entry dt_entry;
2152 uint32_t dt_hdr_size;
2153 unsigned int compressed_size = 0;
2154 unsigned int dtb_size = 0;
2155 unsigned int out_avai_len = 0;
2156 unsigned char *out_addr = NULL;
2157 unsigned char *best_match_dt_addr = NULL;
2158 int rc;
2159
2160 struct boot_img_hdr *hdr = (struct boot_img_hdr *) (boot_image_start);
2161
2162 if(hdr->dt_size != 0) {
2163 /* add kernel offset */
2164 dt_image_offset += page_size;
2165 n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07002166 dt_image_offset += n;
Amol Jadicb524072012-08-09 16:40:18 -07002167
Deepa Dinamani19648b42013-09-05 17:05:55 -07002168 /* add ramdisk offset */
Amol Jadicb524072012-08-09 16:40:18 -07002169 n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
2170 dt_image_offset += n;
2171
Joel Kingaa335dc2013-06-03 16:11:08 -07002172 /* add second offset */
2173 if(hdr->second_size != 0) {
Amol Jadicb524072012-08-09 16:40:18 -07002174 n = ROUND_TO_PAGE(hdr->second_size, page_mask);
2175 dt_image_offset += n;
2176 }
2177
Matthew Qin271927e2015-03-31 22:07:07 -04002178 /* offset now point to start of dt.img */
2179 table = (struct dt_table*)(boot_image_start + dt_image_offset);
2180
2181 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
2182 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
Matthew Qin0b15b322015-05-19 05:20:54 -04002183 return -1;
Matthew Qin271927e2015-03-31 22:07:07 -04002184 }
P.V. Phani Kumar82916762016-03-09 09:20:19 +05302185
2186 /* Its Error if, dt_hdr_size (table->num_entries * dt_entry size + Dev_Tree Header)
2187 goes beyound hdr->dt_size*/
2188 if (dt_hdr_size > ROUND_TO_PAGE(hdr->dt_size,hdr->page_size)) {
2189 dprintf(CRITICAL, "ERROR: Invalid Device Tree size \n");
2190 return -1;
2191 }
2192
Amol Jadicb524072012-08-09 16:40:18 -07002193 /* Find index of device tree within device tree table */
2194 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
2195 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
2196 return -1;
2197 }
2198
Matthew Qin271927e2015-03-31 22:07:07 -04002199 best_match_dt_addr = (unsigned char *)boot_image_start + dt_image_offset + dt_entry.offset;
2200 if (is_gzip_package(best_match_dt_addr, dt_entry.size))
2201 {
2202 out_addr = (unsigned char *)target_get_scratch_address() + scratch_offset;
2203 out_avai_len = target_get_max_flash_size() - scratch_offset;
2204 dprintf(INFO, "decompressing dtb: start\n");
2205 rc = decompress(best_match_dt_addr,
2206 dt_entry.size, out_addr, out_avai_len,
2207 &compressed_size, &dtb_size);
2208 if (rc)
2209 {
Matthew Qin0b15b322015-05-19 05:20:54 -04002210 dprintf(CRITICAL, "decompressing dtb failed!!!\n");
Matthew Qin271927e2015-03-31 22:07:07 -04002211 ASSERT(0);
2212 }
2213
Matthew Qin0b15b322015-05-19 05:20:54 -04002214 dprintf(INFO, "decompressing dtb: done\n");
Matthew Qin271927e2015-03-31 22:07:07 -04002215 best_match_dt_addr = out_addr;
2216 } else {
2217 dtb_size = dt_entry.size;
2218 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002219 /* Validate and Read device device tree in the "tags_add */
Matthew Qin271927e2015-03-31 22:07:07 -04002220 if (check_aboot_addr_range_overlap(hdr->tags_addr, dtb_size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002221 {
2222 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
2223 return -1;
2224 }
2225
Amol Jadicb524072012-08-09 16:40:18 -07002226 /* Read device device tree in the "tags_add */
Matthew Qin271927e2015-03-31 22:07:07 -04002227 memmove((void*) hdr->tags_addr, (void *)best_match_dt_addr, dtb_size);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002228 } else
2229 return -1;
Amol Jadicb524072012-08-09 16:40:18 -07002230
2231 /* Everything looks fine. Return success. */
2232 return 0;
2233}
2234#endif
2235
Brian Swetland9c4c0752009-01-25 16:23:50 -08002236void cmd_boot(const char *arg, void *data, unsigned sz)
2237{
Amit Blay8a510302015-08-17 09:20:01 +03002238#ifdef MDTP_SUPPORT
2239 static bool is_mdtp_activated = 0;
2240#endif /* MDTP_SUPPORT */
Brian Swetland9c4c0752009-01-25 16:23:50 -08002241 unsigned kernel_actual;
2242 unsigned ramdisk_actual;
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002243 uint32_t image_actual;
2244 uint32_t dt_actual = 0;
P.V. Phani Kumareecfd822016-03-09 20:09:03 +05302245 uint32_t sig_actual = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08002246 struct boot_img_hdr *hdr = NULL;
2247 struct kernel64_hdr *kptr = NULL;
Brian Swetland9c4c0752009-01-25 16:23:50 -08002248 char *ptr = ((char*) data);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002249 int ret = 0;
2250 uint8_t dtb_copied = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08002251 unsigned int out_len = 0;
2252 unsigned int out_avai_len = 0;
2253 unsigned char *out_addr = NULL;
2254 uint32_t dtb_offset = 0;
2255 unsigned char *kernel_start_addr = NULL;
2256 unsigned int kernel_size = 0;
Matthew Qin271927e2015-03-31 22:07:07 -04002257 unsigned int scratch_offset = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08002258
lijuang2008ff22016-03-07 17:56:27 +08002259#if FBCON_DISPLAY_MSG
2260 /* Exit keys' detection thread firstly */
2261 exit_menu_keys_detection();
2262#endif
2263
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002264#if VERIFIED_BOOT
Channagoud Kadabi6d5375e2015-06-23 17:15:42 -07002265 if(target_build_variant_user() && !device.is_unlocked)
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002266 {
2267 fastboot_fail("unlock device to use this command");
lijuang2008ff22016-03-07 17:56:27 +08002268 goto boot_failed;
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002269 }
2270#endif
2271
Brian Swetland9c4c0752009-01-25 16:23:50 -08002272 if (sz < sizeof(hdr)) {
2273 fastboot_fail("invalid bootimage header");
lijuang2008ff22016-03-07 17:56:27 +08002274 goto boot_failed;
Brian Swetland9c4c0752009-01-25 16:23:50 -08002275 }
2276
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002277 hdr = (struct boot_img_hdr *)data;
Brian Swetland9c4c0752009-01-25 16:23:50 -08002278
2279 /* ensure commandline is terminated */
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002280 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -08002281
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002282 if(target_is_emmc_boot() && hdr->page_size) {
2283 page_size = hdr->page_size;
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07002284 page_mask = page_size - 1;
2285 }
2286
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002287 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
2288 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002289#if DEVICE_TREE
2290 dt_actual = ROUND_TO_PAGE(hdr->dt_size, page_mask);
2291#endif
2292
2293 image_actual = ADD_OF(page_size, kernel_actual);
2294 image_actual = ADD_OF(image_actual, ramdisk_actual);
2295 image_actual = ADD_OF(image_actual, dt_actual);
2296
P.V. Phani Kumareecfd822016-03-09 20:09:03 +05302297 if (target_use_signed_kernel() && (!device.is_unlocked)) {
2298 /* Calculate the signature length from boot image */
2299 sig_actual = read_der_message_length(
2300 (unsigned char*)(data + image_actual),sz);
Channagoud Kadabi3d839012014-06-26 14:26:39 -07002301 image_actual = ADD_OF(image_actual, sig_actual);
P.V. Phani Kumareecfd822016-03-09 20:09:03 +05302302 }
Channagoud Kadabi3d839012014-06-26 14:26:39 -07002303
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002304 /* sz should have atleast raw boot image */
2305 if (image_actual > sz) {
Channagoud Kadabi3d839012014-06-26 14:26:39 -07002306 fastboot_fail("bootimage: incomplete or not signed");
lijuang2008ff22016-03-07 17:56:27 +08002307 goto boot_failed;
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002308 }
2309
Sridhar Parasuram361e4cd2015-09-24 11:19:19 -07002310 // Initialize boot state before trying to verify boot.img
2311#if VERIFIED_BOOT
Channagoud Kadabi699466e2015-11-03 12:37:42 -08002312 boot_verifier_init();
Channagoud Kadabibf6ce7d2015-09-17 13:25:35 -07002313 /* Handle overflow if the input image size is greater than
2314 * boot image buffer can hold
2315 */
2316 if ((target_get_max_flash_size() - (image_actual - sig_actual)) < page_size)
2317 {
2318 fastboot_fail("booimage: size is greater than boot image buffer can hold");
lijuang2008ff22016-03-07 17:56:27 +08002319 goto boot_failed;
Channagoud Kadabibf6ce7d2015-09-17 13:25:35 -07002320 }
Sridhar Parasuram361e4cd2015-09-24 11:19:19 -07002321#endif
2322
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002323 /* Verify the boot image
2324 * device & page_size are initialized in aboot_init
2325 */
Sridhar Parasuram7ae9fbc2015-09-17 09:56:30 -07002326 if (target_use_signed_kernel() && (!device.is_unlocked)) {
Channagoud Kadabi3d839012014-06-26 14:26:39 -07002327 /* Pass size excluding signature size, otherwise we would try to
2328 * access signature beyond its length
2329 */
2330 verify_signed_bootimg((uint32_t)data, (image_actual - sig_actual));
Sridhar Parasuram7ae9fbc2015-09-17 09:56:30 -07002331 }
Amit Blay4aa292f2015-04-28 21:55:59 +03002332#ifdef MDTP_SUPPORT
2333 else
2334 {
Shay Nachmani062aa3f2015-05-17 17:28:44 +03002335 /* fastboot boot is not allowed when MDTP is activated */
Amit Blay4aa292f2015-04-28 21:55:59 +03002336 mdtp_ext_partition_verification_t ext_partition;
Amit Blay8a510302015-08-17 09:20:01 +03002337
2338 if (!is_mdtp_activated) {
2339 ext_partition.partition = MDTP_PARTITION_NONE;
2340 mdtp_fwlock_verify_lock(&ext_partition);
2341 }
Amit Blay4aa292f2015-04-28 21:55:59 +03002342 }
Shay Nachmani062aa3f2015-05-17 17:28:44 +03002343
Amir Kotzer7c768c02016-04-13 09:08:05 +03002344 /* If mdtp state cannot be validate, block fastboot boot*/
2345 if(mdtp_activated(&is_mdtp_activated)){
2346 dprintf(CRITICAL, "mdtp_activated cannot validate state.\n");
2347 dprintf(CRITICAL, "Can not proceed with fastboot boot command.\n");
2348 goto boot_failed;
2349 }
Shay Nachmani062aa3f2015-05-17 17:28:44 +03002350 if(is_mdtp_activated){
2351 dprintf(CRITICAL, "fastboot boot command is not available.\n");
lijuang2008ff22016-03-07 17:56:27 +08002352 goto boot_failed;
Shay Nachmani062aa3f2015-05-17 17:28:44 +03002353 }
Amit Blay4aa292f2015-04-28 21:55:59 +03002354#endif /* MDTP_SUPPORT */
2355
Sridhar Parasuram361e4cd2015-09-24 11:19:19 -07002356#if VERIFIED_BOOT
Parth Dixitbc9b6492015-10-18 00:41:38 +05302357#if !VBOOT_MOTA
Sridhar Parasuram361e4cd2015-09-24 11:19:19 -07002358 // send root of trust
2359 if(!send_rot_command((uint32_t)device.is_unlocked))
2360 ASSERT(0);
2361#endif
Parth Dixitbc9b6492015-10-18 00:41:38 +05302362#endif
Channagoud Kadabia22144f2013-03-20 11:49:01 -07002363 /*
Matthew Qinbb7923d2015-02-09 10:56:09 +08002364 * Check if the kernel image is a gzip package. If yes, need to decompress it.
2365 * If not, continue booting.
2366 */
2367 if (is_gzip_package((unsigned char *)(data + page_size), hdr->kernel_size))
2368 {
2369 out_addr = (unsigned char *)target_get_scratch_address();
2370 out_addr = (unsigned char *)(out_addr + image_actual + page_size);
2371 out_avai_len = target_get_max_flash_size() - image_actual - page_size;
Matthew Qin0b15b322015-05-19 05:20:54 -04002372 dprintf(INFO, "decompressing kernel image: start\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08002373 ret = decompress((unsigned char *)(ptr + page_size),
2374 hdr->kernel_size, out_addr, out_avai_len,
2375 &dtb_offset, &out_len);
2376 if (ret)
2377 {
Matthew Qin0b15b322015-05-19 05:20:54 -04002378 dprintf(CRITICAL, "decompressing image failed!!!\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08002379 ASSERT(0);
2380 }
2381
Matthew Qin0b15b322015-05-19 05:20:54 -04002382 dprintf(INFO, "decompressing kernel image: done\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08002383 kptr = (struct kernel64_hdr *)out_addr;
2384 kernel_start_addr = out_addr;
2385 kernel_size = out_len;
2386 } else {
2387 kptr = (struct kernel64_hdr*)((char *)data + page_size);
2388 kernel_start_addr = (unsigned char *)((char *)data + page_size);
2389 kernel_size = hdr->kernel_size;
2390 }
2391
2392 /*
Channagoud Kadabia22144f2013-03-20 11:49:01 -07002393 * Update the kernel/ramdisk/tags address if the boot image header
2394 * has default values, these default values come from mkbootimg when
2395 * the boot image is flashed using fastboot flash:raw
2396 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08002397 update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr));
Dima Zavin3cadfff2013-03-21 14:30:48 -07002398
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002399 /* Get virtual addresses since the hdr saves physical addresses. */
2400 hdr->kernel_addr = VA(hdr->kernel_addr);
2401 hdr->ramdisk_addr = VA(hdr->ramdisk_addr);
2402 hdr->tags_addr = VA(hdr->tags_addr);
Brian Swetland9c4c0752009-01-25 16:23:50 -08002403
Matthew Qinbb7923d2015-02-09 10:56:09 +08002404 kernel_size = ROUND_TO_PAGE(kernel_size, page_mask);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002405 /* Check if the addresses in the header are valid. */
Matthew Qinbb7923d2015-02-09 10:56:09 +08002406 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_size) ||
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002407 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
2408 {
2409 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
lijuang2008ff22016-03-07 17:56:27 +08002410 goto boot_failed;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002411 }
2412
Amol Jadicb524072012-08-09 16:40:18 -07002413#if DEVICE_TREE
Matthew Qin271927e2015-03-31 22:07:07 -04002414 scratch_offset = image_actual + page_size + out_len;
Amol Jadicb524072012-08-09 16:40:18 -07002415 /* find correct dtb and copy it to right location */
Matthew Qin271927e2015-03-31 22:07:07 -04002416 ret = copy_dtb(data, scratch_offset);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002417
2418 dtb_copied = !ret ? 1 : 0;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002419#else
2420 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
2421 {
2422 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
lijuang2008ff22016-03-07 17:56:27 +08002423 goto boot_failed;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002424 }
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002425#endif
2426
2427 /* Load ramdisk & kernel */
2428 memmove((void*) hdr->ramdisk_addr, ptr + page_size + kernel_actual, hdr->ramdisk_size);
Matthew Qinbb7923d2015-02-09 10:56:09 +08002429 memmove((void*) hdr->kernel_addr, (char*) (kernel_start_addr), kernel_size);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002430
2431#if DEVICE_TREE
Matthew Qinbb7923d2015-02-09 10:56:09 +08002432 if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual))
2433 {
2434 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
lijuang2008ff22016-03-07 17:56:27 +08002435 goto boot_failed;
Matthew Qinbb7923d2015-02-09 10:56:09 +08002436 }
2437
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002438 /*
2439 * If dtb is not found look for appended DTB in the kernel.
2440 * If appended dev tree is found, update the atags with
2441 * memory address to the DTB appended location on RAM.
2442 * Else update with the atags address in the kernel header
2443 */
2444 if (!dtb_copied) {
2445 void *dtb;
Matthew Qinbb7923d2015-02-09 10:56:09 +08002446 dtb = dev_tree_appended((void*)(ptr + page_size),
2447 hdr->kernel_size, dtb_offset,
Dima Zavine63e5572013-05-03 12:23:06 -07002448 (void *)hdr->tags_addr);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002449 if (!dtb) {
2450 fastboot_fail("dtb not found");
lijuang2008ff22016-03-07 17:56:27 +08002451 goto boot_failed;
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002452 }
Amol Jadicb524072012-08-09 16:40:18 -07002453 }
2454#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08002455
2456 fastboot_okay("");
Amol Jadi7c4316c2013-10-07 14:19:26 -07002457 fastboot_stop();
Brian Swetland9c4c0752009-01-25 16:23:50 -08002458
Dima Zavin77e41f32013-03-06 16:10:43 -08002459 boot_linux((void*) hdr->kernel_addr, (void*) hdr->tags_addr,
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002460 (const char*) hdr->cmdline, board_machtype(),
2461 (void*) hdr->ramdisk_addr, hdr->ramdisk_size);
lijuang2008ff22016-03-07 17:56:27 +08002462
2463 /* fastboot already stop, it's no need to show fastboot menu */
2464 return;
2465boot_failed:
2466#if FBCON_DISPLAY_MSG
2467 /* revert to fastboot menu if boot failed */
2468 display_fastboot_menu();
2469#endif
2470 return;
Brian Swetland9c4c0752009-01-25 16:23:50 -08002471}
2472
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002473void cmd_erase_nand(const char *arg, void *data, unsigned sz)
Dima Zavin214cc642009-01-26 11:16:21 -08002474{
2475 struct ptentry *ptn;
2476 struct ptable *ptable;
2477
2478 ptable = flash_get_ptable();
2479 if (ptable == NULL) {
2480 fastboot_fail("partition table doesn't exist");
2481 return;
2482 }
2483
2484 ptn = ptable_find(ptable, arg);
2485 if (ptn == NULL) {
2486 fastboot_fail("unknown partition name");
2487 return;
2488 }
2489
2490 if (flash_erase(ptn)) {
2491 fastboot_fail("failed to erase partition");
2492 return;
2493 }
2494 fastboot_okay("");
2495}
2496
Bikas Gurungd48bd242010-09-04 19:54:32 -07002497
2498void cmd_erase_mmc(const char *arg, void *data, unsigned sz)
2499{
2500 unsigned long long ptn = 0;
Oliver Wangcee448d2013-10-22 18:40:13 +08002501 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07002502 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002503 uint8_t lun = 0;
Bikas Gurungd48bd242010-09-04 19:54:32 -07002504
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002505#if VERIFIED_BOOT
2506 if(!strcmp(arg, KEYSTORE_PTN_NAME))
2507 {
2508 if(!device.is_unlocked)
2509 {
2510 fastboot_fail("unlock device to erase keystore");
2511 return;
2512 }
2513 }
2514#endif
2515
Kinson Chikf1a43512011-07-14 11:28:39 -07002516 index = partition_get_index(arg);
2517 ptn = partition_get_offset(index);
Oliver Wangcee448d2013-10-22 18:40:13 +08002518 size = partition_get_size(index);
Neeti Desaica8c9602011-10-06 11:40:00 -07002519
Kinson Chikf1a43512011-07-14 11:28:39 -07002520 if(ptn == 0) {
Neeti Desaica8c9602011-10-06 11:40:00 -07002521 fastboot_fail("Partition table doesn't exist\n");
Bikas Gurungd48bd242010-09-04 19:54:32 -07002522 return;
2523 }
Kun Liang2f1601a2013-08-12 16:29:54 +08002524
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002525 lun = partition_get_lun(index);
2526 mmc_set_lun(lun);
2527
Sridhar Parasuramd7957122015-02-27 11:33:40 -08002528 if (platform_boot_dev_isemmc())
2529 {
2530 if (mmc_erase_card(ptn, size)) {
2531 fastboot_fail("failed to erase partition\n");
2532 return;
2533 }
2534 } else {
2535 BUF_DMA_ALIGN(out, DEFAULT_ERASE_SIZE);
2536 size = partition_get_size(index);
2537 if (size > DEFAULT_ERASE_SIZE)
2538 size = DEFAULT_ERASE_SIZE;
Kun Liang2f1601a2013-08-12 16:29:54 +08002539
Sridhar Parasuramd7957122015-02-27 11:33:40 -08002540 /* Simple inefficient version of erase. Just writing
2541 0 in first several blocks */
2542 if (mmc_write(ptn , size, (unsigned int *)out)) {
2543 fastboot_fail("failed to erase partition");
2544 return;
2545 }
Bikas Gurungd48bd242010-09-04 19:54:32 -07002546 }
Sridhar Parasuram32b30662015-07-10 13:33:22 -07002547#if VERIFIED_BOOT
Parth Dixitbc9b6492015-10-18 00:41:38 +05302548#if !VBOOT_MOTA
Sridhar Parasuram32b30662015-07-10 13:33:22 -07002549 if(!(strncmp(arg, "userdata", 8)))
2550 if(send_delete_keys_to_tz())
2551 ASSERT(0);
2552#endif
Parth Dixitbc9b6492015-10-18 00:41:38 +05302553#endif
Bikas Gurungd48bd242010-09-04 19:54:32 -07002554 fastboot_okay("");
2555}
2556
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002557void cmd_erase(const char *arg, void *data, unsigned sz)
2558{
Channagoud Kadabice3cf422015-04-17 16:02:30 -07002559#if VERIFIED_BOOT
Channagoud Kadabi35297672015-06-13 11:09:49 -07002560 if (target_build_variant_user())
Channagoud Kadabice3cf422015-04-17 16:02:30 -07002561 {
Sridhar Parasuramc32d07d2015-07-12 10:57:48 -07002562 if(!device.is_unlocked)
Channagoud Kadabice3cf422015-04-17 16:02:30 -07002563 {
Channagoud Kadabi35297672015-06-13 11:09:49 -07002564 fastboot_fail("device is locked. Cannot erase");
Channagoud Kadabice3cf422015-04-17 16:02:30 -07002565 return;
2566 }
2567 }
2568#endif
2569
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002570 if(target_is_emmc_boot())
2571 cmd_erase_mmc(arg, data, sz);
2572 else
2573 cmd_erase_nand(arg, data, sz);
2574}
Bikas Gurungd48bd242010-09-04 19:54:32 -07002575
Channagoud Kadabiad259832015-05-29 11:14:17 -07002576static uint32_t aboot_get_secret_key()
2577{
2578 /* 0 is invalid secret key, update this implementation to return
2579 * device specific unique secret key
2580 */
2581 return 0;
2582}
2583
Ajay Dudani5c761132011-04-07 20:19:04 -07002584void cmd_flash_mmc_img(const char *arg, void *data, unsigned sz)
Shashank Mittal23b8f422010-04-16 19:27:21 -07002585{
2586 unsigned long long ptn = 0;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07002587 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07002588 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002589 char *token = NULL;
2590 char *pname = NULL;
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07002591 char *sp;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002592 uint8_t lun = 0;
2593 bool lun_set = false;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07002594
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07002595 token = strtok_r((char *)arg, ":", &sp);
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002596 pname = token;
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07002597 token = strtok_r(NULL, ":", &sp);
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002598 if(token)
2599 {
2600 lun = atoi(token);
2601 mmc_set_lun(lun);
2602 lun_set = true;
2603 }
2604
Mao Jinlong226f33a2014-07-04 17:24:10 +08002605 if (pname)
Greg Grisco6e754772011-06-23 12:19:39 -07002606 {
Channagoud Kadabiad259832015-05-29 11:14:17 -07002607 if (!strncmp(pname, "frp-unlock", strlen("frp-unlock")))
2608 {
2609 if (!aboot_frp_unlock(pname, data, sz))
2610 {
2611 fastboot_info("FRP unlock successful");
2612 fastboot_okay("");
2613 }
2614 else
2615 fastboot_fail("Secret key is invalid, please update the bootloader with secret key");
2616
2617 return;
2618 }
2619
Mao Jinlong226f33a2014-07-04 17:24:10 +08002620 if (!strcmp(pname, "partition"))
2621 {
2622 dprintf(INFO, "Attempt to write partition image.\n");
2623 if (write_partition(sz, (unsigned char *) data)) {
2624 fastboot_fail("failed to write partition");
Greg Grisco6e754772011-06-23 12:19:39 -07002625 return;
2626 }
2627 }
Mao Jinlong226f33a2014-07-04 17:24:10 +08002628 else
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002629 {
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002630#if VERIFIED_BOOT
2631 if(!strcmp(pname, KEYSTORE_PTN_NAME))
2632 {
2633 if(!device.is_unlocked)
2634 {
2635 fastboot_fail("unlock device to flash keystore");
2636 return;
2637 }
Gaurav Nebhwanic7313cc2015-12-15 22:25:04 +05302638 if(!boot_verify_validate_keystore((unsigned char *)data,sz))
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002639 {
2640 fastboot_fail("image is not a keystore file");
2641 return;
2642 }
2643 }
2644#endif
Mao Jinlong226f33a2014-07-04 17:24:10 +08002645 index = partition_get_index(pname);
2646 ptn = partition_get_offset(index);
2647 if(ptn == 0) {
2648 fastboot_fail("partition table doesn't exist");
2649 return;
2650 }
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002651
Mao Jinlong226f33a2014-07-04 17:24:10 +08002652 if (!strcmp(pname, "boot") || !strcmp(pname, "recovery")) {
2653 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
2654 fastboot_fail("image is not a boot image");
2655 return;
2656 }
2657 }
2658
2659 if(!lun_set)
2660 {
2661 lun = partition_get_lun(index);
2662 mmc_set_lun(lun);
2663 }
2664
2665 size = partition_get_size(index);
Gaurav Nebhwani4d2389c2016-03-17 21:10:05 +05302666 if (ROUND_TO_PAGE(sz, mmc_blocksize_mask) > size) {
Mao Jinlong226f33a2014-07-04 17:24:10 +08002667 fastboot_fail("size too large");
2668 return;
2669 }
2670 else if (mmc_write(ptn , sz, (unsigned int *)data)) {
2671 fastboot_fail("flash write failure");
2672 return;
2673 }
Greg Grisco6e754772011-06-23 12:19:39 -07002674 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07002675 }
2676 fastboot_okay("");
2677 return;
2678}
2679
Ajay Dudanide984792015-03-02 09:57:41 -08002680void cmd_flash_meta_img(const char *arg, void *data, unsigned sz)
2681{
2682 int i, images;
2683 meta_header_t *meta_header;
2684 img_header_entry_t *img_header_entry;
Parth Dixit3e2d3032016-03-04 17:11:52 +05302685 /*End of the image address*/
2686 uintptr_t data_end;
2687
2688 if( (UINT_MAX - sz) > (uintptr_t)data )
2689 data_end = (uintptr_t)data + sz;
2690 else
2691 {
2692 fastboot_fail("Cannot flash: image header corrupt");
2693 return;
2694 }
2695
2696 if( data_end < ((uintptr_t)data + sizeof(meta_header_t)))
2697 {
2698 fastboot_fail("Cannot flash: image header corrupt");
2699 return;
2700 }
Ajay Dudanide984792015-03-02 09:57:41 -08002701
lijuang8ee21882016-04-19 16:57:11 +08002702 /* If device is locked:
2703 * Forbid to flash image to avoid the device to bypass the image
2704 * which with "any" name other than bootloader. Because it maybe
2705 * a meta package of all partitions.
2706 */
2707#if VERIFIED_BOOT
2708 if (target_build_variant_user()) {
2709 if (!device.is_unlocked) {
2710 fastboot_fail("Device is locked, meta image flashing is not allowed");
2711 return;
2712 }
2713#if !VBOOT_MOTA
2714 if(!device.is_unlock_critical) {
2715 fastboot_fail("Device is critical locked, Meta image flashing is not allowed");
2716 return;
2717 }
2718#endif
2719 }
2720#endif
2721
Ajay Dudanide984792015-03-02 09:57:41 -08002722 meta_header = (meta_header_t*) data;
Parth Dixit3e2d3032016-03-04 17:11:52 +05302723 if( data_end < ((uintptr_t)data + meta_header->img_hdr_sz))
2724 {
2725 fastboot_fail("Cannot flash: image header corrupt");
2726 return;
2727 }
Ajay Dudanide984792015-03-02 09:57:41 -08002728 img_header_entry = (img_header_entry_t*) (data+sizeof(meta_header_t));
2729
2730 images = meta_header->img_hdr_sz / sizeof(img_header_entry_t);
2731
2732 for (i=0; i<images; i++) {
2733
2734 if((img_header_entry[i].ptn_name == NULL) ||
2735 (img_header_entry[i].start_offset == 0) ||
2736 (img_header_entry[i].size == 0))
2737 break;
2738
Parth Dixit3e2d3032016-03-04 17:11:52 +05302739 if( data_end < ((uintptr_t)data + img_header_entry[i].start_offset
2740 + img_header_entry[i].size) )
2741 {
2742 fastboot_fail("Cannot flash: image size mismatch");
2743 break;
2744 }
2745
Ajay Dudanide984792015-03-02 09:57:41 -08002746 cmd_flash_mmc_img(img_header_entry[i].ptn_name,
2747 (void *) data + img_header_entry[i].start_offset,
2748 img_header_entry[i].size);
2749 }
2750
Channagoud Kadabiaafbbfc2015-03-04 17:52:56 -08002751 if (!strncmp(arg, "bootloader", strlen("bootloader")))
2752 {
2753 strlcpy(device.bootloader_version, TARGET(BOARD), MAX_VERSION_LEN);
2754 strlcat(device.bootloader_version, "-", MAX_VERSION_LEN);
2755 strlcat(device.bootloader_version, meta_header->img_version, MAX_VERSION_LEN);
2756 }
2757 else
2758 {
2759 strlcpy(device.radio_version, TARGET(BOARD), MAX_VERSION_LEN);
2760 strlcat(device.radio_version, "-", MAX_VERSION_LEN);
2761 strlcat(device.radio_version, meta_header->img_version, MAX_VERSION_LEN);
2762 }
2763
2764 write_device_info(&device);
Ajay Dudanide984792015-03-02 09:57:41 -08002765 fastboot_okay("");
2766 return;
2767}
2768
Ajay Dudani5c761132011-04-07 20:19:04 -07002769void cmd_flash_mmc_sparse_img(const char *arg, void *data, unsigned sz)
2770{
2771 unsigned int chunk;
wufeng.jiang813dc352015-06-02 23:02:46 -04002772 uint64_t chunk_data_sz;
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002773 uint32_t *fill_buf = NULL;
2774 uint32_t fill_val;
Ajay Dudani5c761132011-04-07 20:19:04 -07002775 sparse_header_t *sparse_header;
2776 chunk_header_t *chunk_header;
Ajay Dudaniab18f022011-05-12 14:39:22 -07002777 uint32_t total_blocks = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07002778 unsigned long long ptn = 0;
Channagoud Kadabi65b91002011-10-11 17:34:33 +05302779 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07002780 int index = INVALID_PTN;
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05302781 uint32_t i;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002782 uint8_t lun = 0;
vijay kumar800255e2015-04-24 20:26:19 +05302783 /*End of the sparse image address*/
Parth Dixit64b1a482016-03-07 16:31:26 +05302784 uintptr_t data_end = (uintptr_t)data + sz;
Ajay Dudani5c761132011-04-07 20:19:04 -07002785
Kinson Chikf1a43512011-07-14 11:28:39 -07002786 index = partition_get_index(arg);
2787 ptn = partition_get_offset(index);
2788 if(ptn == 0) {
Ajay Dudani5c761132011-04-07 20:19:04 -07002789 fastboot_fail("partition table doesn't exist");
2790 return;
2791 }
2792
Channagoud Kadabi65b91002011-10-11 17:34:33 +05302793 size = partition_get_size(index);
Channagoud Kadabi65b91002011-10-11 17:34:33 +05302794
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002795 lun = partition_get_lun(index);
2796 mmc_set_lun(lun);
2797
vijay kumar800255e2015-04-24 20:26:19 +05302798 if (sz < sizeof(sparse_header_t)) {
2799 fastboot_fail("size too low");
2800 return;
2801 }
2802
Ajay Dudani5c761132011-04-07 20:19:04 -07002803 /* Read and skip over sparse image header */
2804 sparse_header = (sparse_header_t *) data;
vijay kumar800255e2015-04-24 20:26:19 +05302805
vijay kumar1321f342015-03-27 12:13:42 +05302806 if (((uint64_t)sparse_header->total_blks * (uint64_t)sparse_header->blk_sz) > size) {
Ajay Dudani876b3282012-12-21 14:12:17 -08002807 fastboot_fail("size too large");
2808 return;
2809 }
2810
vijay kumarde4fcf62015-04-23 13:05:49 +05302811 data += sizeof(sparse_header_t);
vijay kumar800255e2015-04-24 20:26:19 +05302812
Parth Dixit64b1a482016-03-07 16:31:26 +05302813 if (data_end < (uintptr_t)data) {
vijay kumar800255e2015-04-24 20:26:19 +05302814 fastboot_fail("buffer overreads occured due to invalid sparse header");
2815 return;
2816 }
2817
vijay kumarde4fcf62015-04-23 13:05:49 +05302818 if(sparse_header->file_hdr_sz != sizeof(sparse_header_t))
Ajay Dudani5c761132011-04-07 20:19:04 -07002819 {
vijay kumarde4fcf62015-04-23 13:05:49 +05302820 fastboot_fail("sparse header size mismatch");
2821 return;
Ajay Dudani5c761132011-04-07 20:19:04 -07002822 }
2823
Ajay Dudanib06c05f2011-05-12 14:46:10 -07002824 dprintf (SPEW, "=== Sparse Image Header ===\n");
2825 dprintf (SPEW, "magic: 0x%x\n", sparse_header->magic);
2826 dprintf (SPEW, "major_version: 0x%x\n", sparse_header->major_version);
2827 dprintf (SPEW, "minor_version: 0x%x\n", sparse_header->minor_version);
2828 dprintf (SPEW, "file_hdr_sz: %d\n", sparse_header->file_hdr_sz);
2829 dprintf (SPEW, "chunk_hdr_sz: %d\n", sparse_header->chunk_hdr_sz);
2830 dprintf (SPEW, "blk_sz: %d\n", sparse_header->blk_sz);
2831 dprintf (SPEW, "total_blks: %d\n", sparse_header->total_blks);
2832 dprintf (SPEW, "total_chunks: %d\n", sparse_header->total_chunks);
Ajay Dudani5c761132011-04-07 20:19:04 -07002833
2834 /* Start processing chunks */
2835 for (chunk=0; chunk<sparse_header->total_chunks; chunk++)
2836 {
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302837 /* Make sure the total image size does not exceed the partition size */
2838 if(((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz) >= size) {
2839 fastboot_fail("size too large");
2840 return;
2841 }
Ajay Dudani5c761132011-04-07 20:19:04 -07002842 /* Read and skip over chunk header */
2843 chunk_header = (chunk_header_t *) data;
2844 data += sizeof(chunk_header_t);
2845
Parth Dixit64b1a482016-03-07 16:31:26 +05302846 if (data_end < (uintptr_t)data) {
vijay kumar800255e2015-04-24 20:26:19 +05302847 fastboot_fail("buffer overreads occured due to invalid sparse header");
2848 return;
2849 }
2850
Ajay Dudani5c761132011-04-07 20:19:04 -07002851 dprintf (SPEW, "=== Chunk Header ===\n");
2852 dprintf (SPEW, "chunk_type: 0x%x\n", chunk_header->chunk_type);
2853 dprintf (SPEW, "chunk_data_sz: 0x%x\n", chunk_header->chunk_sz);
2854 dprintf (SPEW, "total_size: 0x%x\n", chunk_header->total_sz);
2855
vijay kumar800255e2015-04-24 20:26:19 +05302856 if(sparse_header->chunk_hdr_sz != sizeof(chunk_header_t))
Ajay Dudani5c761132011-04-07 20:19:04 -07002857 {
vijay kumar800255e2015-04-24 20:26:19 +05302858 fastboot_fail("chunk header size mismatch");
2859 return;
Ajay Dudani5c761132011-04-07 20:19:04 -07002860 }
2861
wufeng.jiang813dc352015-06-02 23:02:46 -04002862 if (!sparse_header->blk_sz ){
2863 fastboot_fail("Invalid block size\n");
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302864 return;
2865 }
2866
wufeng.jiang813dc352015-06-02 23:02:46 -04002867 chunk_data_sz = (uint64_t)sparse_header->blk_sz * chunk_header->chunk_sz;
2868
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302869 /* Make sure that the chunk size calculated from sparse image does not
2870 * exceed partition size
2871 */
2872 if ((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz + chunk_data_sz > size)
2873 {
2874 fastboot_fail("Chunk data size exceeds partition size");
2875 return;
2876 }
2877
Ajay Dudani5c761132011-04-07 20:19:04 -07002878 switch (chunk_header->chunk_type)
2879 {
2880 case CHUNK_TYPE_RAW:
wufeng.jiang813dc352015-06-02 23:02:46 -04002881 if((uint64_t)chunk_header->total_sz != ((uint64_t)sparse_header->chunk_hdr_sz +
Ajay Dudani5c761132011-04-07 20:19:04 -07002882 chunk_data_sz))
2883 {
2884 fastboot_fail("Bogus chunk size for chunk type Raw");
2885 return;
2886 }
2887
Parth Dixit64b1a482016-03-07 16:31:26 +05302888 if (data_end < (uintptr_t)data + chunk_data_sz) {
vijay kumar800255e2015-04-24 20:26:19 +05302889 fastboot_fail("buffer overreads occured due to invalid sparse header");
2890 return;
2891 }
2892
wufeng.jiang813dc352015-06-02 23:02:46 -04002893 /* chunk_header->total_sz is uint32,So chunk_data_sz is now less than 2^32
2894 otherwise it will return in the line above
2895 */
Ajay Dudaniab18f022011-05-12 14:39:22 -07002896 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
wufeng.jiang813dc352015-06-02 23:02:46 -04002897 (uint32_t)chunk_data_sz,
Ajay Dudaniab18f022011-05-12 14:39:22 -07002898 (unsigned int*)data))
Ajay Dudani5c761132011-04-07 20:19:04 -07002899 {
2900 fastboot_fail("flash write failure");
2901 return;
2902 }
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302903 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
2904 fastboot_fail("Bogus size for RAW chunk type");
2905 return;
2906 }
Ajay Dudani5c761132011-04-07 20:19:04 -07002907 total_blocks += chunk_header->chunk_sz;
wufeng.jiang813dc352015-06-02 23:02:46 -04002908 data += (uint32_t)chunk_data_sz;
Ajay Dudani5c761132011-04-07 20:19:04 -07002909 break;
2910
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002911 case CHUNK_TYPE_FILL:
2912 if(chunk_header->total_sz != (sparse_header->chunk_hdr_sz +
2913 sizeof(uint32_t)))
2914 {
2915 fastboot_fail("Bogus chunk size for chunk type FILL");
2916 return;
2917 }
2918
2919 fill_buf = (uint32_t *)memalign(CACHE_LINE, ROUNDUP(sparse_header->blk_sz, CACHE_LINE));
2920 if (!fill_buf)
2921 {
2922 fastboot_fail("Malloc failed for: CHUNK_TYPE_FILL");
2923 return;
2924 }
2925
Parth Dixit64b1a482016-03-07 16:31:26 +05302926 if (data_end < (uintptr_t)data + sizeof(uint32_t)) {
vijay kumar800255e2015-04-24 20:26:19 +05302927 fastboot_fail("buffer overreads occured due to invalid sparse header");
Gaurav Nebhwani163cbf82016-03-17 21:21:27 +05302928 free(fill_buf);
vijay kumar800255e2015-04-24 20:26:19 +05302929 return;
2930 }
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002931 fill_val = *(uint32_t *)data;
2932 data = (char *) data + sizeof(uint32_t);
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002933
2934 for (i = 0; i < (sparse_header->blk_sz / sizeof(fill_val)); i++)
2935 {
2936 fill_buf[i] = fill_val;
2937 }
2938
Gaurav Nebhwanie94cbe12016-03-17 21:16:34 +05302939 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz))
2940 {
2941 fastboot_fail("bogus size for chunk FILL type");
2942 free(fill_buf);
2943 return;
2944 }
2945
wufeng.jiang813dc352015-06-02 23:02:46 -04002946 for (i = 0; i < chunk_header->chunk_sz; i++)
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002947 {
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302948 /* Make sure that the data written to partition does not exceed partition size */
2949 if ((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz + sparse_header->blk_sz > size)
2950 {
2951 fastboot_fail("Chunk data size for fill type exceeds partition size");
Gaurav Nebhwani163cbf82016-03-17 21:21:27 +05302952 free(fill_buf);
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302953 return;
2954 }
2955
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002956 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
2957 sparse_header->blk_sz,
2958 fill_buf))
2959 {
2960 fastboot_fail("flash write failure");
2961 free(fill_buf);
2962 return;
2963 }
2964
2965 total_blocks++;
2966 }
2967
2968 free(fill_buf);
2969 break;
2970
Ajay Dudani5c761132011-04-07 20:19:04 -07002971 case CHUNK_TYPE_DONT_CARE:
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302972 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
2973 fastboot_fail("bogus size for chunk DONT CARE type");
2974 return;
2975 }
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07002976 total_blocks += chunk_header->chunk_sz;
2977 break;
2978
Ajay Dudani5c761132011-04-07 20:19:04 -07002979 case CHUNK_TYPE_CRC:
2980 if(chunk_header->total_sz != sparse_header->chunk_hdr_sz)
2981 {
wufeng.jiang813dc352015-06-02 23:02:46 -04002982 fastboot_fail("Bogus chunk size for chunk type CRC");
Ajay Dudani5c761132011-04-07 20:19:04 -07002983 return;
2984 }
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302985 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
2986 fastboot_fail("bogus size for chunk CRC type");
2987 return;
2988 }
Ajay Dudani5c761132011-04-07 20:19:04 -07002989 total_blocks += chunk_header->chunk_sz;
Parth Dixit64b1a482016-03-07 16:31:26 +05302990 if ((uintptr_t)data > UINT_MAX - chunk_data_sz) {
vijay kumar800255e2015-04-24 20:26:19 +05302991 fastboot_fail("integer overflow occured");
2992 return;
2993 }
wufeng.jiang813dc352015-06-02 23:02:46 -04002994 data += (uint32_t)chunk_data_sz;
Parth Dixit64b1a482016-03-07 16:31:26 +05302995 if (data_end < (uintptr_t)data) {
vijay kumar800255e2015-04-24 20:26:19 +05302996 fastboot_fail("buffer overreads occured due to invalid sparse header");
2997 return;
2998 }
Ajay Dudani5c761132011-04-07 20:19:04 -07002999 break;
3000
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07003001 default:
Channagoud Kadabif627cf82013-09-09 14:08:20 -07003002 dprintf(CRITICAL, "Unkown chunk type: %x\n",chunk_header->chunk_type);
Ajay Dudani5c761132011-04-07 20:19:04 -07003003 fastboot_fail("Unknown chunk type");
3004 return;
3005 }
3006 }
3007
Ajay Dudani0c6927b2011-05-18 11:12:16 -07003008 dprintf(INFO, "Wrote %d blocks, expected to write %d blocks\n",
3009 total_blocks, sparse_header->total_blks);
3010
3011 if(total_blocks != sparse_header->total_blks)
3012 {
3013 fastboot_fail("sparse image write failure");
3014 }
Ajay Dudani5c761132011-04-07 20:19:04 -07003015
3016 fastboot_okay("");
3017 return;
3018}
3019
3020void cmd_flash_mmc(const char *arg, void *data, unsigned sz)
3021{
3022 sparse_header_t *sparse_header;
Ajay Dudanide984792015-03-02 09:57:41 -08003023 meta_header_t *meta_header;
Ajay Dudani5c761132011-04-07 20:19:04 -07003024
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07003025#ifdef SSD_ENABLE
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -08003026 /* 8 Byte Magic + 2048 Byte xml + Encrypted Data */
3027 unsigned int *magic_number = (unsigned int *) data;
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08003028 int ret=0;
3029 uint32 major_version=0;
3030 uint32 minor_version=0;
3031
3032 ret = scm_svc_version(&major_version,&minor_version);
3033 if(!ret)
3034 {
3035 if(major_version >= 2)
3036 {
Amir Samuelovbb65ce02013-05-05 12:20:18 +03003037 if( !strcmp(arg, "ssd") || !strcmp(arg, "tqs") )
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08003038 {
3039 ret = encrypt_scm((uint32 **) &data, &sz);
3040 if (ret != 0) {
3041 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
3042 return;
3043 }
3044
Amir Samuelovbb65ce02013-05-05 12:20:18 +03003045 /* Protect only for SSD */
3046 if (!strcmp(arg, "ssd")) {
3047 ret = scm_protect_keystore((uint32 *) data, sz);
3048 if (ret != 0) {
3049 dprintf(CRITICAL, "ERROR: scm_protect_keystore Failed\n");
3050 return;
3051 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08003052 }
3053 }
3054 else
3055 {
3056 ret = decrypt_scm_v2((uint32 **) &data, &sz);
3057 if(ret != 0)
3058 {
3059 dprintf(CRITICAL,"ERROR: Decryption Failure\n");
3060 return;
3061 }
3062 }
3063 }
3064 else
3065 {
3066 if (magic_number[0] == DECRYPT_MAGIC_0 &&
3067 magic_number[1] == DECRYPT_MAGIC_1)
3068 {
3069 ret = decrypt_scm((uint32 **) &data, &sz);
3070 if (ret != 0) {
3071 dprintf(CRITICAL, "ERROR: Invalid secure image\n");
3072 return;
3073 }
3074 }
3075 else if (magic_number[0] == ENCRYPT_MAGIC_0 &&
3076 magic_number[1] == ENCRYPT_MAGIC_1)
3077 {
3078 ret = encrypt_scm((uint32 **) &data, &sz);
3079 if (ret != 0) {
3080 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
3081 return;
3082 }
3083 }
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07003084 }
3085 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08003086 else
Neeti Desai127b9e02012-03-20 16:11:23 -07003087 {
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08003088 dprintf(CRITICAL,"INVALID SVC Version\n");
3089 return;
Neeti Desai127b9e02012-03-20 16:11:23 -07003090 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08003091#endif /* SSD_ENABLE */
Neeti Desai127b9e02012-03-20 16:11:23 -07003092
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003093#if VERIFIED_BOOT
Channagoud Kadabi35297672015-06-13 11:09:49 -07003094 if (target_build_variant_user())
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003095 {
lijuang511a2b52015-08-14 20:50:51 +08003096 /* if device is locked:
3097 * common partition will not allow to be flashed
3098 * critical partition will allow to flash image.
3099 */
3100 if(!device.is_unlocked && !critical_flash_allowed(arg)) {
3101 fastboot_fail("Partition flashing is not allowed");
3102 return;
3103 }
Parth Dixitddbc7352015-10-18 03:13:31 +05303104#if !VBOOT_MOTA
lijuang511a2b52015-08-14 20:50:51 +08003105 /* if device critical is locked:
3106 * common partition will allow to be flashed
3107 * critical partition will not allow to flash image.
3108 */
3109 if(!device.is_unlock_critical && critical_flash_allowed(arg)) {
3110 fastboot_fail("Critical partition flashing is not allowed");
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003111 return;
3112 }
Parth Dixitddbc7352015-10-18 03:13:31 +05303113#endif
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003114 }
3115#endif
3116
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07003117 sparse_header = (sparse_header_t *) data;
Ajay Dudanide984792015-03-02 09:57:41 -08003118 meta_header = (meta_header_t *) data;
3119 if (sparse_header->magic == SPARSE_HEADER_MAGIC)
Ajay Dudani5c761132011-04-07 20:19:04 -07003120 cmd_flash_mmc_sparse_img(arg, data, sz);
Ajay Dudanide984792015-03-02 09:57:41 -08003121 else if (meta_header->magic == META_HEADER_MAGIC)
3122 cmd_flash_meta_img(arg, data, sz);
3123 else
3124 cmd_flash_mmc_img(arg, data, sz);
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07003125
3126#if VERIFIED_BOOT
Parth Dixitddbc7352015-10-18 03:13:31 +05303127#if !VBOOT_MOTA
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07003128 if((!strncmp(arg, "system", 6)) && !device.verity_mode)
3129 {
3130 // reset dm_verity mode to enforcing
3131 device.verity_mode = 1;
3132 write_device_info(&device);
3133 }
3134#endif
Parth Dixitddbc7352015-10-18 03:13:31 +05303135#endif
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07003136
Ajay Dudani5c761132011-04-07 20:19:04 -07003137 return;
3138}
3139
Tanya Brokhman84eaaf62015-02-24 21:57:06 +02003140void cmd_updatevol(const char *vol_name, void *data, unsigned sz)
3141{
3142 struct ptentry *sys_ptn;
3143 struct ptable *ptable;
3144
3145 ptable = flash_get_ptable();
3146 if (ptable == NULL) {
3147 fastboot_fail("partition table doesn't exist");
3148 return;
3149 }
3150
3151 sys_ptn = ptable_find(ptable, "system");
3152 if (sys_ptn == NULL) {
3153 fastboot_fail("system partition not found");
3154 return;
3155 }
3156
3157 sz = ROUND_TO_PAGE(sz, page_mask);
3158 if (update_ubi_vol(sys_ptn, vol_name, data, sz))
3159 fastboot_fail("update_ubi_vol failed");
3160 else
3161 fastboot_okay("");
3162}
3163
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003164void cmd_flash_nand(const char *arg, void *data, unsigned sz)
Dima Zavin214cc642009-01-26 11:16:21 -08003165{
3166 struct ptentry *ptn;
3167 struct ptable *ptable;
3168 unsigned extra = 0;
Gaurav Nebhwanid9730712016-05-06 14:28:29 +05303169 uint64_t partition_size = 0;
Dima Zavin214cc642009-01-26 11:16:21 -08003170
3171 ptable = flash_get_ptable();
3172 if (ptable == NULL) {
3173 fastboot_fail("partition table doesn't exist");
3174 return;
3175 }
3176
3177 ptn = ptable_find(ptable, arg);
3178 if (ptn == NULL) {
Tanya Brokhman84eaaf62015-02-24 21:57:06 +02003179 dprintf(INFO, "unknown partition name (%s). Trying updatevol\n",
3180 arg);
3181 cmd_updatevol(arg, data, sz);
Dima Zavin214cc642009-01-26 11:16:21 -08003182 return;
3183 }
3184
3185 if (!strcmp(ptn->name, "boot") || !strcmp(ptn->name, "recovery")) {
3186 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
3187 fastboot_fail("image is not a boot image");
3188 return;
3189 }
3190 }
3191
Amol Jadi5c61a952012-05-04 17:05:35 -07003192 if (!strcmp(ptn->name, "system")
Deepa Dinamani13e32c42012-03-12 14:34:17 -07003193 || !strcmp(ptn->name, "userdata")
3194 || !strcmp(ptn->name, "persist")
Sundarajan Srinivasanb063a852013-11-19 14:02:27 -08003195 || !strcmp(ptn->name, "recoveryfs")
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -08003196 || !strcmp(ptn->name, "modem"))
Tanya Brokhman1c94f1a2015-02-15 09:05:03 +02003197 extra = 1;
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -08003198 else
Shashank Mittaldcc2e352009-11-19 19:11:16 -08003199 sz = ROUND_TO_PAGE(sz, page_mask);
Dima Zavin214cc642009-01-26 11:16:21 -08003200
Gaurav Nebhwanid9730712016-05-06 14:28:29 +05303201 partition_size = (uint64_t)ptn->length * (uint64_t)flash_num_pages_per_blk() * (uint64_t)flash_page_size();
3202 if (partition_size > UINT_MAX) {
3203 fastboot_fail("Invalid partition size");
3204 return;
3205 }
3206
3207 if (sz > partition_size) {
3208 fastboot_fail("Image size too large");
3209 return;
3210 }
3211
Dima Zavin214cc642009-01-26 11:16:21 -08003212 dprintf(INFO, "writing %d bytes to '%s'\n", sz, ptn->name);
Tanya Brokhman1c94f1a2015-02-15 09:05:03 +02003213 if (!memcmp((void *)data, UBI_MAGIC, UBI_MAGIC_SIZE)) {
3214 if (flash_ubi_img(ptn, data, sz)) {
3215 fastboot_fail("flash write failure");
3216 return;
3217 }
3218 } else {
3219 if (flash_write(ptn, extra, data, sz)) {
3220 fastboot_fail("flash write failure");
3221 return;
3222 }
Dima Zavin214cc642009-01-26 11:16:21 -08003223 }
3224 dprintf(INFO, "partition '%s' updated\n", ptn->name);
3225 fastboot_okay("");
3226}
3227
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003228void cmd_flash(const char *arg, void *data, unsigned sz)
3229{
3230 if(target_is_emmc_boot())
3231 cmd_flash_mmc(arg, data, sz);
3232 else
3233 cmd_flash_nand(arg, data, sz);
3234}
3235
Dima Zavin214cc642009-01-26 11:16:21 -08003236void cmd_continue(const char *arg, void *data, unsigned sz)
3237{
3238 fastboot_okay("");
Amol Jadi7c4316c2013-10-07 14:19:26 -07003239 fastboot_stop();
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003240
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003241 if (target_is_emmc_boot())
3242 {
lijuanga40d6302015-07-20 20:10:13 +08003243#if FBCON_DISPLAY_MSG
lijuangde34d502016-02-26 16:04:50 +08003244 /* Exit keys' detection thread firstly */
3245 exit_menu_keys_detection();
lijuanga40d6302015-07-20 20:10:13 +08003246#endif
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003247 boot_linux_from_mmc();
3248 }
3249 else
3250 {
3251 boot_linux_from_flash();
3252 }
Dima Zavin214cc642009-01-26 11:16:21 -08003253}
3254
Chandan Uddaraju94183c02010-01-15 15:13:59 -08003255void cmd_reboot(const char *arg, void *data, unsigned sz)
3256{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003257 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08003258 fastboot_okay("");
3259 reboot_device(0);
3260}
3261
3262void cmd_reboot_bootloader(const char *arg, void *data, unsigned sz)
3263{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003264 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08003265 fastboot_okay("");
3266 reboot_device(FASTBOOT_MODE);
3267}
3268
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003269void cmd_oem_enable_charger_screen(const char *arg, void *data, unsigned size)
3270{
3271 dprintf(INFO, "Enabling charger screen check\n");
3272 device.charger_screen_enabled = 1;
3273 write_device_info(&device);
3274 fastboot_okay("");
3275}
3276
3277void cmd_oem_disable_charger_screen(const char *arg, void *data, unsigned size)
3278{
3279 dprintf(INFO, "Disabling charger screen check\n");
3280 device.charger_screen_enabled = 0;
3281 write_device_info(&device);
3282 fastboot_okay("");
3283}
3284
lijuanga25d8bb2015-09-16 13:11:52 +08003285void cmd_oem_off_mode_charger(const char *arg, void *data, unsigned size)
3286{
3287 char *p = NULL;
3288 const char *delim = " \t\n\r";
Parth Dixit407f15b2016-01-07 14:47:37 +05303289 char *sp;
lijuanga25d8bb2015-09-16 13:11:52 +08003290
3291 if (arg) {
Parth Dixit407f15b2016-01-07 14:47:37 +05303292 p = strtok_r((char *)arg, delim, &sp);
lijuanga25d8bb2015-09-16 13:11:52 +08003293 if (p) {
3294 if (!strncmp(p, "0", 1)) {
3295 device.charger_screen_enabled = 0;
3296 } else if (!strncmp(p, "1", 1)) {
3297 device.charger_screen_enabled = 1;
3298 }
3299 }
3300 }
3301
3302 /* update charger_screen_enabled value for getvar
3303 * command
3304 */
3305 snprintf(charger_screen_enabled, MAX_RSP_SIZE, "%d",
3306 device.charger_screen_enabled);
3307
3308 write_device_info(&device);
3309 fastboot_okay("");
3310}
3311
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05303312void cmd_oem_select_display_panel(const char *arg, void *data, unsigned size)
3313{
3314 dprintf(INFO, "Selecting display panel %s\n", arg);
3315 if (arg)
3316 strlcpy(device.display_panel, arg,
3317 sizeof(device.display_panel));
3318 write_device_info(&device);
3319 fastboot_okay("");
3320}
3321
Shashank Mittal162244e2011-08-08 19:01:25 -07003322void cmd_oem_unlock(const char *arg, void *data, unsigned sz)
3323{
lijuang4ece1e72015-08-14 21:02:36 +08003324 set_device_unlock(UNLOCK, TRUE);
vijay kumarc65876c2015-04-24 13:29:16 +05303325}
3326
3327void cmd_oem_unlock_go(const char *arg, void *data, unsigned sz)
3328{
lijuang4ece1e72015-08-14 21:02:36 +08003329 if(!device.is_unlocked) {
vijay kumarc65876c2015-04-24 13:29:16 +05303330 if(!is_allow_unlock) {
3331 fastboot_fail("oem unlock is not allowed");
3332 return;
3333 }
3334
lijuang4ece1e72015-08-14 21:02:36 +08003335 set_device_unlock_value(UNLOCK, TRUE);
vijay kumarc65876c2015-04-24 13:29:16 +05303336
lijuang4ece1e72015-08-14 21:02:36 +08003337 /* wipe data */
vijay kumarc65876c2015-04-24 13:29:16 +05303338 struct recovery_message msg;
lijuang4ece1e72015-08-14 21:02:36 +08003339
vijay kumarc65876c2015-04-24 13:29:16 +05303340 snprintf(msg.recovery, sizeof(msg.recovery), "recovery\n--wipe_data");
3341 write_misc(0, &msg, sizeof(msg));
3342
3343 fastboot_okay("");
3344 reboot_device(RECOVERY_MODE);
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003345 }
3346 fastboot_okay("");
3347}
3348
Channagoud Kadabiad259832015-05-29 11:14:17 -07003349static int aboot_frp_unlock(char *pname, void *data, unsigned sz)
3350{
3351 int ret = 1;
3352 uint32_t secret_key;
3353 char seckey_buffer[MAX_RSP_SIZE];
3354
3355 secret_key = aboot_get_secret_key();
3356 if (secret_key)
3357 {
3358 snprintf((char *) seckey_buffer, MAX_RSP_SIZE, "%x", secret_key);
3359 if (!memcmp((void *)data, (void *)seckey_buffer, sz))
3360 {
3361 is_allow_unlock = true;
3362 write_allow_oem_unlock(is_allow_unlock);
3363 ret = 0;
3364 }
3365 }
3366 return ret;
3367}
3368
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003369void cmd_oem_lock(const char *arg, void *data, unsigned sz)
3370{
lijuang4ece1e72015-08-14 21:02:36 +08003371 set_device_unlock(UNLOCK, FALSE);
Shashank Mittal162244e2011-08-08 19:01:25 -07003372}
3373
Shashank Mittala0032282011-08-26 14:50:11 -07003374void cmd_oem_devinfo(const char *arg, void *data, unsigned sz)
3375{
lijuang511a2b52015-08-14 20:50:51 +08003376 char response[MAX_RSP_SIZE];
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003377 snprintf(response, sizeof(response), "\tDevice tampered: %s", (device.is_tampered ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07003378 fastboot_info(response);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003379 snprintf(response, sizeof(response), "\tDevice unlocked: %s", (device.is_unlocked ? "true" : "false"));
3380 fastboot_info(response);
Parth Dixitddbc7352015-10-18 03:13:31 +05303381#if !VBOOT_MOTA
lijuang511a2b52015-08-14 20:50:51 +08003382 snprintf(response, sizeof(response), "\tDevice critical unlocked: %s", (device.is_unlock_critical ? "true" : "false"));
3383 fastboot_info(response);
Parth Dixitddbc7352015-10-18 03:13:31 +05303384#endif
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003385 snprintf(response, sizeof(response), "\tCharger screen enabled: %s", (device.charger_screen_enabled ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07003386 fastboot_info(response);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05303387 snprintf(response, sizeof(response), "\tDisplay panel: %s", (device.display_panel));
3388 fastboot_info(response);
Shashank Mittala0032282011-08-26 14:50:11 -07003389 fastboot_okay("");
3390}
3391
lijuang511a2b52015-08-14 20:50:51 +08003392void cmd_flashing_get_unlock_ability(const char *arg, void *data, unsigned sz)
3393{
3394 char response[MAX_RSP_SIZE];
3395 snprintf(response, sizeof(response), "\tget_unlock_ability: %d", is_allow_unlock);
3396 fastboot_info(response);
3397 fastboot_okay("");
3398}
3399
3400void cmd_flashing_lock_critical(const char *arg, void *data, unsigned sz)
3401{
lijuang4ece1e72015-08-14 21:02:36 +08003402 set_device_unlock(UNLOCK_CRITICAL, FALSE);
lijuang511a2b52015-08-14 20:50:51 +08003403}
3404
3405void cmd_flashing_unlock_critical(const char *arg, void *data, unsigned sz)
3406{
lijuang4ece1e72015-08-14 21:02:36 +08003407 set_device_unlock(UNLOCK_CRITICAL, TRUE);
lijuang511a2b52015-08-14 20:50:51 +08003408}
3409
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07003410void cmd_preflash(const char *arg, void *data, unsigned sz)
3411{
3412 fastboot_okay("");
3413}
3414
Mao Flynn7b379f32015-04-20 00:28:30 +08003415static uint8_t logo_header[LOGO_IMG_HEADER_SIZE];
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303416
Mao Flynn7b379f32015-04-20 00:28:30 +08003417int splash_screen_check_header(logo_img_header *header)
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303418{
Mao Flynn7b379f32015-04-20 00:28:30 +08003419 if (memcmp(header->magic, LOGO_IMG_MAGIC, 8))
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303420 return -1;
Mao Flynn7b379f32015-04-20 00:28:30 +08003421 if (header->width == 0 || header->height == 0)
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303422 return -1;
3423 return 0;
3424}
3425
Mao Flynn7b379f32015-04-20 00:28:30 +08003426int splash_screen_flash()
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003427{
3428 struct ptentry *ptn;
3429 struct ptable *ptable;
Mao Flynn7b379f32015-04-20 00:28:30 +08003430 struct logo_img_header *header;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003431 struct fbcon_config *fb_display = NULL;
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08003432
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303433 ptable = flash_get_ptable();
3434 if (ptable == NULL) {
Mao Flynn7b379f32015-04-20 00:28:30 +08003435 dprintf(CRITICAL, "ERROR: Partition table not found\n");
3436 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303437 }
Mao Flynn7b379f32015-04-20 00:28:30 +08003438
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303439 ptn = ptable_find(ptable, "splash");
3440 if (ptn == NULL) {
3441 dprintf(CRITICAL, "ERROR: splash Partition not found\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003442 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303443 }
Mao Flynn7b379f32015-04-20 00:28:30 +08003444 if (flash_read(ptn, 0, (void *)logo_header, LOGO_IMG_HEADER_SIZE)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303445 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003446 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303447 }
3448
Mao Flynn7b379f32015-04-20 00:28:30 +08003449 header = (struct logo_img_header *)logo_header;
3450 if (splash_screen_check_header(header)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303451 dprintf(CRITICAL, "ERROR: Boot image header invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003452 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303453 }
3454
3455 fb_display = fbcon_display();
3456 if (fb_display) {
Mao Flynn7b379f32015-04-20 00:28:30 +08003457 if (header->type && (header->blocks != 0)) { // RLE24 compressed data
3458 uint8_t *base = (uint8_t *) fb_display->base + LOGO_IMG_OFFSET;
3459
3460 /* if the logo is full-screen size, remove "fbcon_clear()" */
3461 if ((header->width != fb_display->width)
3462 || (header->height != fb_display->height))
3463 fbcon_clear();
3464
3465 if (flash_read(ptn + LOGO_IMG_HEADER_SIZE, 0,
3466 (uint32_t *)base,
3467 (header->blocks * 512))) {
3468 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
3469 return -1;
3470 }
3471 fbcon_extract_to_screen(header, base);
3472 return 0;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003473 }
Mao Flynn7b379f32015-04-20 00:28:30 +08003474
Veera Sundaram Sankaranc33416f2015-09-28 10:25:55 -07003475 if ((header->width > fb_display->width) || (header->height > fb_display->height)) {
3476 dprintf(CRITICAL, "Logo config greater than fb config. Fall back default logo\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003477 return -1;
3478 }
3479
Vineet Bajaj99291ed2014-09-09 12:29:46 +05303480 uint8_t *base = (uint8_t *) fb_display->base;
Mao Flynn7b379f32015-04-20 00:28:30 +08003481 if (flash_read(ptn + LOGO_IMG_HEADER_SIZE, 0,
3482 (uint32_t *)base,
3483 ((((header->width * header->height * fb_display->bpp/8) + 511) >> 9) << 9))) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303484 fbcon_clear();
Vineet Bajaj99291ed2014-09-09 12:29:46 +05303485 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003486 return -1;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003487 }
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303488 }
3489
Mao Flynn7b379f32015-04-20 00:28:30 +08003490 return 0;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303491}
3492
Mao Flynn7b379f32015-04-20 00:28:30 +08003493int splash_screen_mmc()
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303494{
3495 int index = INVALID_PTN;
3496 unsigned long long ptn = 0;
3497 struct fbcon_config *fb_display = NULL;
Mao Flynn7b379f32015-04-20 00:28:30 +08003498 struct logo_img_header *header;
Mao Flynn1893a7f2015-06-03 12:03:36 +08003499 uint32_t blocksize, realsize, readsize;
3500 uint8_t *base;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303501
3502 index = partition_get_index("splash");
3503 if (index == 0) {
3504 dprintf(CRITICAL, "ERROR: splash Partition table not found\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003505 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303506 }
3507
3508 ptn = partition_get_offset(index);
3509 if (ptn == 0) {
3510 dprintf(CRITICAL, "ERROR: splash Partition invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003511 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303512 }
3513
Mao Flynn1893a7f2015-06-03 12:03:36 +08003514 mmc_set_lun(partition_get_lun(index));
3515
3516 blocksize = mmc_get_device_blocksize();
3517 if (blocksize == 0) {
3518 dprintf(CRITICAL, "ERROR:splash Partition invalid blocksize\n");
3519 return -1;
3520 }
3521
3522 fb_display = fbcon_display();
Channagoud Kadabidaf10a62015-07-22 11:51:55 -07003523 if (!fb_display)
3524 {
3525 dprintf(CRITICAL, "ERROR: fb config is not allocated\n");
3526 return -1;
3527 }
3528
Mao Flynn1893a7f2015-06-03 12:03:36 +08003529 base = (uint8_t *) fb_display->base;
3530
3531 if (mmc_read(ptn, (uint32_t *)(base + LOGO_IMG_OFFSET), blocksize)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303532 dprintf(CRITICAL, "ERROR: Cannot read splash image header\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003533 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303534 }
3535
Mao Flynn1893a7f2015-06-03 12:03:36 +08003536 header = (struct logo_img_header *)(base + LOGO_IMG_OFFSET);
Mao Flynn7b379f32015-04-20 00:28:30 +08003537 if (splash_screen_check_header(header)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303538 dprintf(CRITICAL, "ERROR: Splash image header invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003539 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303540 }
3541
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303542 if (fb_display) {
Mao Flynn1893a7f2015-06-03 12:03:36 +08003543 if (header->type && (header->blocks != 0)) { /* 1 RLE24 compressed data */
3544 base += LOGO_IMG_OFFSET;
Mao Flynn7b379f32015-04-20 00:28:30 +08003545
Mao Flynn1893a7f2015-06-03 12:03:36 +08003546 realsize = header->blocks * 512;
3547 readsize = ROUNDUP((realsize + LOGO_IMG_HEADER_SIZE), blocksize) - blocksize;
3548
3549 /* if the logo is not full-screen size, clean screen */
Mao Flynn7b379f32015-04-20 00:28:30 +08003550 if ((header->width != fb_display->width)
3551 || (header->height != fb_display->height))
3552 fbcon_clear();
3553
Mao Flynn1893a7f2015-06-03 12:03:36 +08003554 if (mmc_read(ptn + blocksize, (uint32_t *)(base + blocksize), readsize)) {
Mao Flynn7b379f32015-04-20 00:28:30 +08003555 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
3556 return -1;
3557 }
Mao Flynn7b379f32015-04-20 00:28:30 +08003558
Mao Flynn1893a7f2015-06-03 12:03:36 +08003559 fbcon_extract_to_screen(header, (base + LOGO_IMG_HEADER_SIZE));
3560 } else { /* 2 Raw BGR data */
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303561
Veera Sundaram Sankaranc33416f2015-09-28 10:25:55 -07003562 if ((header->width > fb_display->width) || (header->height > fb_display->height)) {
3563 dprintf(CRITICAL, "Logo config greater than fb config. Fall back default logo\n");
Mao Flynn1893a7f2015-06-03 12:03:36 +08003564 return -1;
3565 }
3566
3567 realsize = header->width * header->height * fb_display->bpp / 8;
3568 readsize = ROUNDUP((realsize + LOGO_IMG_HEADER_SIZE), blocksize) - blocksize;
3569
3570 if (blocksize == LOGO_IMG_HEADER_SIZE) { /* read the content directly */
3571 if (mmc_read((ptn + LOGO_IMG_HEADER_SIZE), (uint32_t *)base, readsize)) {
3572 fbcon_clear();
3573 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
3574 return -1;
3575 }
3576 } else {
3577 if (mmc_read(ptn + blocksize ,
3578 (uint32_t *)(base + LOGO_IMG_OFFSET + blocksize), readsize)) {
3579 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
3580 return -1;
3581 }
3582 memmove(base, (base + LOGO_IMG_OFFSET + LOGO_IMG_HEADER_SIZE), realsize);
3583 }
3584 }
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303585 }
3586
Mao Flynn7b379f32015-04-20 00:28:30 +08003587 return 0;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303588}
3589
Mao Flynn7b379f32015-04-20 00:28:30 +08003590int fetch_image_from_partition()
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303591{
3592 if (target_is_emmc_boot()) {
3593 return splash_screen_mmc();
3594 } else {
3595 return splash_screen_flash();
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003596 }
3597}
3598
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07003599/* Get the size from partiton name */
3600static void get_partition_size(const char *arg, char *response)
3601{
3602 uint64_t ptn = 0;
3603 uint64_t size;
3604 int index = INVALID_PTN;
3605
3606 index = partition_get_index(arg);
3607
3608 if (index == INVALID_PTN)
3609 {
3610 dprintf(CRITICAL, "Invalid partition index\n");
3611 return;
3612 }
3613
3614 ptn = partition_get_offset(index);
3615
3616 if(!ptn)
3617 {
3618 dprintf(CRITICAL, "Invalid partition name %s\n", arg);
3619 return;
3620 }
3621
3622 size = partition_get_size(index);
3623
3624 snprintf(response, MAX_RSP_SIZE, "\t 0x%llx", size);
3625 return;
3626}
3627
3628/*
3629 * Publish the partition type & size info
3630 * fastboot getvar will publish the required information.
3631 * fastboot getvar partition_size:<partition_name>: partition size in hex
3632 * fastboot getvar partition_type:<partition_name>: partition type (ext/fat)
3633 */
3634static void publish_getvar_partition_info(struct getvar_partition_info *info, uint8_t num_parts)
3635{
3636 uint8_t i;
3637
3638 for (i = 0; i < num_parts; i++) {
3639 get_partition_size(info[i].part_name, info[i].size_response);
3640
3641 if (strlcat(info[i].getvar_size, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
3642 {
3643 dprintf(CRITICAL, "partition size name truncated\n");
3644 return;
3645 }
3646 if (strlcat(info[i].getvar_type, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
3647 {
3648 dprintf(CRITICAL, "partition type name truncated\n");
3649 return;
3650 }
3651
3652 /* publish partition size & type info */
3653 fastboot_publish((const char *) info[i].getvar_size, (const char *) info[i].size_response);
3654 fastboot_publish((const char *) info[i].getvar_type, (const char *) info[i].type_response);
3655 }
3656}
3657
lijuang4ece1e72015-08-14 21:02:36 +08003658void get_product_name(unsigned char *buf)
3659{
3660 snprintf((char*)buf, MAX_RSP_SIZE, "%s", TARGET(BOARD));
3661 return;
3662}
3663
3664void get_bootloader_version(unsigned char *buf)
3665{
3666 snprintf((char*)buf, MAX_RSP_SIZE, "%s", device.bootloader_version);
3667 return;
3668}
3669
3670void get_baseband_version(unsigned char *buf)
3671{
3672 snprintf((char*)buf, MAX_RSP_SIZE, "%s", device.radio_version);
3673 return;
3674}
3675
3676bool is_device_locked()
3677{
3678 return device.is_unlocked ? false:true;
3679}
3680
Amol Jadi5edf3552013-07-23 14:15:34 -07003681/* register commands and variables for fastboot */
3682void aboot_fastboot_register_commands(void)
3683{
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003684 int i;
lijuangf16461c2015-08-03 17:09:34 +08003685 char hw_platform_buf[MAX_RSP_SIZE];
Amol Jadi5edf3552013-07-23 14:15:34 -07003686
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003687 struct fastboot_cmd_desc cmd_list[] = {
lijuang511a2b52015-08-14 20:50:51 +08003688 /* By default the enabled list is empty. */
3689 {"", NULL},
3690 /* move commands enclosed within the below ifndef to here
3691 * if they need to be enabled in user build.
3692 */
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003693#ifndef DISABLE_FASTBOOT_CMDS
lijuang511a2b52015-08-14 20:50:51 +08003694 /* Register the following commands only for non-user builds */
3695 {"flash:", cmd_flash},
3696 {"erase:", cmd_erase},
3697 {"boot", cmd_boot},
3698 {"continue", cmd_continue},
3699 {"reboot", cmd_reboot},
3700 {"reboot-bootloader", cmd_reboot_bootloader},
3701 {"oem unlock", cmd_oem_unlock},
3702 {"oem unlock-go", cmd_oem_unlock_go},
3703 {"oem lock", cmd_oem_lock},
3704 {"flashing unlock", cmd_oem_unlock},
3705 {"flashing lock", cmd_oem_lock},
3706 {"flashing lock_critical", cmd_flashing_lock_critical},
3707 {"flashing unlock_critical", cmd_flashing_unlock_critical},
3708 {"flashing get_unlock_ability", cmd_flashing_get_unlock_ability},
3709 {"oem device-info", cmd_oem_devinfo},
3710 {"preflash", cmd_preflash},
3711 {"oem enable-charger-screen", cmd_oem_enable_charger_screen},
3712 {"oem disable-charger-screen", cmd_oem_disable_charger_screen},
lijuanga25d8bb2015-09-16 13:11:52 +08003713 {"oem off-mode-charge", cmd_oem_off_mode_charger},
lijuang511a2b52015-08-14 20:50:51 +08003714 {"oem select-display-panel", cmd_oem_select_display_panel},
Channagoud Kadabi736c4962015-08-21 11:56:52 -07003715#if UNITTEST_FW_SUPPORT
Channagoud Kadabid5ddf482015-09-28 10:31:35 -07003716 {"oem run-tests", cmd_oem_runtests},
Channagoud Kadabi736c4962015-08-21 11:56:52 -07003717#endif
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003718#endif
lijuang511a2b52015-08-14 20:50:51 +08003719 };
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003720
3721 int fastboot_cmds_count = sizeof(cmd_list)/sizeof(cmd_list[0]);
3722 for (i = 1; i < fastboot_cmds_count; i++)
3723 fastboot_register(cmd_list[i].name,cmd_list[i].cb);
3724
Amol Jadi5edf3552013-07-23 14:15:34 -07003725 /* publish variables and their values */
3726 fastboot_publish("product", TARGET(BOARD));
3727 fastboot_publish("kernel", "lk");
3728 fastboot_publish("serialno", sn_buf);
3729
3730 /*
3731 * partition info is supported only for emmc partitions
3732 * Calling this for NAND prints some error messages which
3733 * is harmless but misleading. Avoid calling this for NAND
3734 * devices.
3735 */
3736 if (target_is_emmc_boot())
3737 publish_getvar_partition_info(part_info, ARRAY_SIZE(part_info));
3738
3739 /* Max download size supported */
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003740 snprintf(max_download_size, MAX_RSP_SIZE, "\t0x%x",
3741 target_get_max_flash_size());
Amol Jadi5edf3552013-07-23 14:15:34 -07003742 fastboot_publish("max-download-size", (const char *) max_download_size);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003743 /* Is the charger screen check enabled */
3744 snprintf(charger_screen_enabled, MAX_RSP_SIZE, "%d",
3745 device.charger_screen_enabled);
3746 fastboot_publish("charger-screen-enabled",
3747 (const char *) charger_screen_enabled);
lijuanga25d8bb2015-09-16 13:11:52 +08003748 fastboot_publish("off-mode-charge", (const char *) charger_screen_enabled);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05303749 snprintf(panel_display_mode, MAX_RSP_SIZE, "%s",
3750 device.display_panel);
3751 fastboot_publish("display-panel",
3752 (const char *) panel_display_mode);
Channagoud Kadabiaafbbfc2015-03-04 17:52:56 -08003753 fastboot_publish("version-bootloader", (const char *) device.bootloader_version);
3754 fastboot_publish("version-baseband", (const char *) device.radio_version);
lijuangf16461c2015-08-03 17:09:34 +08003755 fastboot_publish("secure", is_secure_boot_enable()? "yes":"no");
3756 smem_get_hw_platform_name((unsigned char *) hw_platform_buf, sizeof(hw_platform_buf));
3757 snprintf(get_variant, MAX_RSP_SIZE, "%s %s", hw_platform_buf,
3758 target_is_emmc_boot()? "eMMC":"UFS");
3759 fastboot_publish("variant", (const char *) get_variant);
lijuang65c5a822015-08-29 16:35:36 +08003760#if CHECK_BAT_VOLTAGE
lijuang102dfa92015-10-09 18:31:03 +08003761 update_battery_status();
lijuang65c5a822015-08-29 16:35:36 +08003762 fastboot_publish("battery-voltage", (const char *) battery_voltage);
lijuang102dfa92015-10-09 18:31:03 +08003763 fastboot_publish("battery-soc-ok", (const char *) battery_soc_ok);
lijuang65c5a822015-08-29 16:35:36 +08003764#endif
Amol Jadi5edf3552013-07-23 14:15:34 -07003765}
3766
Brian Swetland9c4c0752009-01-25 16:23:50 -08003767void aboot_init(const struct app_descriptor *app)
3768{
Shashank Mittal4f99a882010-02-01 13:58:50 -08003769 unsigned reboot_mode = 0;
Chandan Uddarajubedca152010-06-02 23:05:15 -07003770
lijuang39831732016-01-08 17:49:02 +08003771 /* Initialise wdog to catch early lk crashes */
3772#if WDOG_SUPPORT
3773 msm_wdog_init();
3774#endif
3775
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07003776 /* Setup page size information for nv storage */
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003777 if (target_is_emmc_boot())
3778 {
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07003779 page_size = mmc_page_size();
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003780 page_mask = page_size - 1;
Gaurav Nebhwani4d2389c2016-03-17 21:10:05 +05303781 mmc_blocksize = mmc_get_device_blocksize();
3782 mmc_blocksize_mask = mmc_blocksize - 1;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003783 }
3784 else
3785 {
3786 page_size = flash_page_size();
3787 page_mask = page_size - 1;
3788 }
3789
Deepa Dinamani0e163a42013-05-24 17:08:15 -07003790 ASSERT((MEMBASE + MEMSIZE) > MEMBASE);
3791
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003792 read_device_info(&device);
vijay kumarc65876c2015-04-24 13:29:16 +05303793 read_allow_oem_unlock(&device);
Shashank Mittal162244e2011-08-08 19:01:25 -07003794
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08003795 /* Display splash screen if enabled */
3796#if DISPLAY_SPLASH_SCREEN
lijuang99c02d82015-02-13 19:04:34 +08003797#if NO_ALARM_DISPLAY
3798 if (!check_alarm_boot()) {
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08003799#endif
lijuang99c02d82015-02-13 19:04:34 +08003800 dprintf(SPEW, "Display Init: Start\n");
Channagoud Kadabi90869ce2015-04-27 11:15:14 -07003801#if ENABLE_WBC
Channagoud Kadabid801ac72015-08-26 11:21:51 -07003802 /* Wait if the display shutdown is in progress */
3803 while(pm_app_display_shutdown_in_prgs());
Channagoud Kadabi90869ce2015-04-27 11:15:14 -07003804 if (!pm_appsbl_display_init_done())
3805 target_display_init(device.display_panel);
3806 else
3807 display_image_on_screen();
3808#else
lijuang99c02d82015-02-13 19:04:34 +08003809 target_display_init(device.display_panel);
Channagoud Kadabi90869ce2015-04-27 11:15:14 -07003810#endif
lijuang99c02d82015-02-13 19:04:34 +08003811 dprintf(SPEW, "Display Init: Done\n");
3812#if NO_ALARM_DISPLAY
3813 }
3814#endif
3815#endif
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08003816
Greg Griscod6250552011-06-29 14:40:23 -07003817 target_serialno((unsigned char *) sn_buf);
Ajay Dudanib06c05f2011-05-12 14:46:10 -07003818 dprintf(SPEW,"serial number: %s\n",sn_buf);
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -08003819
Dhaval Patel223ec952013-07-18 14:49:44 -07003820 memset(display_panel_buf, '\0', MAX_PANEL_BUF_SIZE);
3821
Matthew Qindefd5562014-07-11 18:02:40 +08003822 /*
3823 * Check power off reason if user force reset,
3824 * if yes phone will do normal boot.
3825 */
3826 if (is_user_force_reset())
3827 goto normal_boot;
3828
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003829 /* Check if we should do something other than booting up */
Ameya Thakur0b9c2442013-05-10 13:22:56 -07003830 if (keys_get_state(KEY_VOLUMEUP) && keys_get_state(KEY_VOLUMEDOWN))
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07003831 {
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03003832 dprintf(ALWAYS,"dload mode key sequence detected\n");
lijuang395b5e62015-11-19 17:39:44 +08003833 reboot_device(EMERGENCY_DLOAD);
3834 dprintf(CRITICAL,"Failed to reboot into dload mode\n");
3835
Ameya Thakur0b9c2442013-05-10 13:22:56 -07003836 boot_into_fastboot = true;
3837 }
3838 if (!boot_into_fastboot)
3839 {
3840 if (keys_get_state(KEY_HOME) || keys_get_state(KEY_VOLUMEUP))
3841 boot_into_recovery = 1;
3842 if (!boot_into_recovery &&
3843 (keys_get_state(KEY_BACK) || keys_get_state(KEY_VOLUMEDOWN)))
Pavel Nedev5d91d412013-04-29 11:34:24 +03003844 boot_into_fastboot = true;
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07003845 }
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003846 #if NO_KEYPAD_DRIVER
Kinson Chik0b1c8162011-08-31 16:31:57 -07003847 if (fastboot_trigger())
Pavel Nedev5d91d412013-04-29 11:34:24 +03003848 boot_into_fastboot = true;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003849 #endif
Chandan Uddarajubedca152010-06-02 23:05:15 -07003850
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003851#if USE_PON_REBOOT_REG
3852 reboot_mode = check_hard_reboot_mode();
3853#else
Ajay Dudani77421292010-10-27 19:34:06 -07003854 reboot_mode = check_reboot_mode();
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003855#endif
3856 if (reboot_mode == RECOVERY_MODE)
3857 {
Ajay Dudani77421292010-10-27 19:34:06 -07003858 boot_into_recovery = 1;
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003859 }
3860 else if(reboot_mode == FASTBOOT_MODE)
3861 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03003862 boot_into_fastboot = true;
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003863 }
3864 else if(reboot_mode == ALARM_BOOT)
3865 {
Matthew Qind886f3c2014-01-17 16:52:01 +08003866 boot_reason_alarm = true;
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003867 }
Parth Dixit207573a2015-10-27 17:26:21 +05303868#if VERIFIED_BOOT
Parth Dixitddbc7352015-10-18 03:13:31 +05303869#if !VBOOT_MOTA
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003870 else if (reboot_mode == DM_VERITY_ENFORCING)
3871 {
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07003872 device.verity_mode = 1;
3873 write_device_info(&device);
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003874 }
3875 else if (reboot_mode == DM_VERITY_LOGGING)
3876 {
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07003877 device.verity_mode = 0;
3878 write_device_info(&device);
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003879 }
3880 else if (reboot_mode == DM_VERITY_KEYSCLEAR)
3881 {
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07003882 if(send_delete_keys_to_tz())
3883 ASSERT(0);
Ajay Dudani77421292010-10-27 19:34:06 -07003884 }
Parth Dixit207573a2015-10-27 17:26:21 +05303885#endif
Parth Dixitddbc7352015-10-18 03:13:31 +05303886#endif
Ajay Dudani77421292010-10-27 19:34:06 -07003887
Matthew Qindefd5562014-07-11 18:02:40 +08003888normal_boot:
Pavel Nedev5d91d412013-04-29 11:34:24 +03003889 if (!boot_into_fastboot)
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003890 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03003891 if (target_is_emmc_boot())
Shashank Mittala0032282011-08-26 14:50:11 -07003892 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03003893 if(emmc_recovery_init())
3894 dprintf(ALWAYS,"error in emmc_recovery_init\n");
3895 if(target_use_signed_kernel())
Shashank Mittala0032282011-08-26 14:50:11 -07003896 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03003897 if((device.is_unlocked) || (device.is_tampered))
3898 {
3899 #ifdef TZ_TAMPER_FUSE
3900 set_tamper_fuse_cmd();
3901 #endif
3902 #if USE_PCOM_SECBOOT
3903 set_tamper_flag(device.is_tampered);
3904 #endif
3905 }
Shashank Mittala0032282011-08-26 14:50:11 -07003906 }
Amit Blay6281ebc2015-01-11 14:44:08 +02003907
Pavel Nedev5d91d412013-04-29 11:34:24 +03003908 boot_linux_from_mmc();
Shashank Mittala0032282011-08-26 14:50:11 -07003909 }
Pavel Nedev5d91d412013-04-29 11:34:24 +03003910 else
3911 {
3912 recovery_init();
3913 #if USE_PCOM_SECBOOT
3914 if((device.is_unlocked) || (device.is_tampered))
3915 set_tamper_flag(device.is_tampered);
3916 #endif
3917 boot_linux_from_flash();
3918 }
3919 dprintf(CRITICAL, "ERROR: Could not do normal boot. Reverting "
3920 "to fastboot mode.\n");
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003921 }
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003922
Amol Jadi5edf3552013-07-23 14:15:34 -07003923 /* We are here means regular boot did not happen. Start fastboot. */
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07003924
Amol Jadi5edf3552013-07-23 14:15:34 -07003925 /* register aboot specific fastboot commands */
3926 aboot_fastboot_register_commands();
Amol Jadi57abe4c2011-05-24 15:47:27 -07003927
Amol Jadi5edf3552013-07-23 14:15:34 -07003928 /* dump partition table for debug info */
Kinson Chikf1a43512011-07-14 11:28:39 -07003929 partition_dump();
Amol Jadi5edf3552013-07-23 14:15:34 -07003930
3931 /* initialize and start fastboot */
3932 fastboot_init(target_get_scratch_address(), target_get_max_flash_size());
lijuang4ece1e72015-08-14 21:02:36 +08003933#if FBCON_DISPLAY_MSG
lijuang9a7d3b92015-11-30 14:41:24 +08003934 display_fastboot_menu();
lijuang4ece1e72015-08-14 21:02:36 +08003935#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08003936}
3937
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07003938uint32_t get_page_size()
3939{
3940 return page_size;
3941}
3942
Amir Samuelov57a6fa22013-06-05 16:36:43 +03003943/*
3944 * Calculated and save hash (SHA256) for non-signed boot image.
3945 *
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07003946 * @param image_addr - Boot image address
3947 * @param image_size - Size of the boot image
Amir Samuelov57a6fa22013-06-05 16:36:43 +03003948 *
3949 * @return int - 0 on success, negative value on failure.
3950 */
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07003951static int aboot_save_boot_hash_mmc(uint32_t image_addr, uint32_t image_size)
Amir Samuelov57a6fa22013-06-05 16:36:43 +03003952{
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07003953 unsigned int digest[8];
3954#if IMAGE_VERIF_ALGO_SHA1
3955 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA1;
3956#else
3957 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA256;
3958#endif
Amir Samuelov57a6fa22013-06-05 16:36:43 +03003959
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07003960 target_crypto_init_params();
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -08003961 hash_find((unsigned char *) image_addr, image_size, (unsigned char *)&digest, auth_algo);
Amir Samuelov57a6fa22013-06-05 16:36:43 +03003962
3963 save_kernel_hash_cmd(digest);
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07003964 dprintf(INFO, "aboot_save_boot_hash_mmc: imagesize_actual size %d bytes.\n", (int) image_size);
Amir Samuelov57a6fa22013-06-05 16:36:43 +03003965
3966 return 0;
3967}
3968
Brian Swetland9c4c0752009-01-25 16:23:50 -08003969APP_START(aboot)
3970 .init = aboot_init,
3971APP_END