blob: fea7b1fa7a519f324405de0f253fbe0aa2c72058 [file] [log] [blame]
Brian Swetland9c4c0752009-01-25 16:23:50 -08001/*
2 * Copyright (c) 2009, Google Inc.
3 * All rights reserved.
4 *
Kishor PK49831502017-04-21 17:55:43 +05305 * Copyright (c) 2009-2017, 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);
Parth Dixit54ac3bb2017-03-07 15:52:48 +0530108bool pwr_key_is_pressed = false;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700109
Sridhar Parasurame94e8152014-10-24 14:06:03 -0700110/* fastboot command function pointer */
111typedef void (*fastboot_cmd_fn) (const char *, void *, unsigned);
112
113struct fastboot_cmd_desc {
114 char * name;
115 fastboot_cmd_fn cb;
116};
117
Subbaraman Narayanamurthyeb92bcc2010-07-20 14:32:46 -0700118#define EXPAND(NAME) #NAME
119#define TARGET(NAME) EXPAND(NAME)
Brian Swetland2defe162009-08-18 14:35:59 -0700120
Ajay Singh Parmara7865a82015-04-16 21:27:52 -0700121#define DISPLAY_PANEL_HDMI "hdmi"
122
Ajay Dudanicd01f9b2010-02-23 21:13:04 -0800123#ifdef MEMBASE
124#define EMMC_BOOT_IMG_HEADER_ADDR (0xFF000+(MEMBASE))
125#else
David Ng183a7422009-12-07 14:55:21 -0800126#define EMMC_BOOT_IMG_HEADER_ADDR 0xFF000
Ajay Dudanicd01f9b2010-02-23 21:13:04 -0800127#endif
128
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700129#ifndef MEMSIZE
130#define MEMSIZE 1024*1024
131#endif
132
133#define MAX_TAGS_SIZE 1024
134
Kun Liang2f1601a2013-08-12 16:29:54 +0800135/* make 4096 as default size to ensure EFS,EXT4's erasing */
136#define DEFAULT_ERASE_SIZE 4096
Ujwal Patelc0b0a252015-08-16 14:05:35 -0700137#define MAX_PANEL_BUF_SIZE 196
Vijay Kumar Pendoti0b21f462016-05-02 17:09:18 +0530138#define FOOTER_SIZE 16384
Kun Liang2f1601a2013-08-12 16:29:54 +0800139
Dhaval Patelf83d73b2014-06-23 16:24:37 -0700140#define DISPLAY_DEFAULT_PREFIX "mdss_mdp"
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700141#define BOOT_DEV_MAX_LEN 64
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -0800142
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800143#define IS_ARM64(ptr) (ptr->magic_64 == KERNEL64_HDR_MAGIC) ? true : false
144
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -0700145#define ADD_OF(a, b) (UINT_MAX - b > a) ? (a + b) : UINT_MAX
146
Ameya Thakur10a33452016-06-13 14:24:26 -0700147//Size of the header that is used in case the boot image has
148//a uncompressed kernel + appended dtb
149#define PATCHED_KERNEL_HEADER_SIZE 20
150
151//String used to determine if the boot image has
152//a uncompressed kernel + appended dtb
153#define PATCHED_KERNEL_MAGIC "UNCOMPRESSED_IMG"
154
Sridhar Parasuram7bd4aaf2015-02-12 11:14:38 -0800155#if USE_BOOTDEV_CMDLINE
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700156static const char *emmc_cmdline = " androidboot.bootdevice=";
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700157#else
David Ng183a7422009-12-07 14:55:21 -0800158static const char *emmc_cmdline = " androidboot.emmc=true";
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700159#endif
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800160static const char *usb_sn_cmdline = " androidboot.serialno=";
Pavel Nedev328ac822013-04-05 15:25:11 +0300161static const char *androidboot_mode = " androidboot.mode=";
Matthew Qind886f3c2014-01-17 16:52:01 +0800162static const char *alarmboot_cmdline = " androidboot.alarmboot=true";
Pavel Nedev898298c2013-02-27 12:36:09 -0800163static const char *loglevel = " quiet";
Ajay Dudanica3a33c2011-11-18 08:31:40 -0800164static const char *battchg_pause = " androidboot.mode=charger";
Shashank Mittalcd98d472011-08-02 14:29:24 -0700165static const char *auth_kernel = " androidboot.authorized_kernel=true";
Pavel Nedev5614d222013-06-17 18:01:02 +0300166static const char *secondary_gpt_enable = " gpt";
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200167static const char *mdtp_activated_flag = " mdtp";
David Ng183a7422009-12-07 14:55:21 -0800168
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800169static const char *baseband_apq = " androidboot.baseband=apq";
170static const char *baseband_msm = " androidboot.baseband=msm";
171static const char *baseband_csfb = " androidboot.baseband=csfb";
172static const char *baseband_svlte2a = " androidboot.baseband=svlte2a";
Ajay Dudani403bc492011-09-30 16:17:21 -0700173static const char *baseband_mdm = " androidboot.baseband=mdm";
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800174static const char *baseband_mdm2 = " androidboot.baseband=mdm2";
Amol Jadi5c61a952012-05-04 17:05:35 -0700175static const char *baseband_sglte = " androidboot.baseband=sglte";
Amol Jadi2a15a272013-01-22 12:03:36 -0800176static const char *baseband_dsda = " androidboot.baseband=dsda";
177static const char *baseband_dsda2 = " androidboot.baseband=dsda2";
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800178static const char *baseband_sglte2 = " androidboot.baseband=sglte2";
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800179static const char *warmboot_cmdline = " qpnp-power-on.warm_boot=1";
Vijay Kumar Pendotib228cfc2016-06-13 20:15:23 +0530180static const char *baseband_apq_nowgr = " androidboot.baseband=baseband_apq_nowgr";
Ajay Dudanid04110c2011-01-17 23:55:07 -0800181
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700182#if VERIFIED_BOOT
Parth Dixitddbc7352015-10-18 03:13:31 +0530183#if !VBOOT_MOTA
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700184static const char *verity_mode = " androidboot.veritymode=";
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700185static const char *verified_state= " androidboot.verifiedbootstate=";
Parth Dixita5715a02015-10-29 12:25:10 +0530186static const char *keymaster_v1= " androidboot.keymaster=1";
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700187//indexed based on enum values, green is 0 by default
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700188
189struct verified_boot_verity_mode vbvm[] =
190{
Channagoud Kadabi86b0c112016-03-16 19:23:16 -0700191#if ENABLE_VB_ATTEST
192 {false, "eio"},
193#else
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700194 {false, "logging"},
Channagoud Kadabi86b0c112016-03-16 19:23:16 -0700195#endif
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700196 {true, "enforcing"},
197};
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700198struct verified_boot_state_name vbsn[] =
199{
200 {GREEN, "green"},
201 {ORANGE, "orange"},
202 {YELLOW,"yellow"},
203 {RED,"red" },
204};
205#endif
Parth Dixitddbc7352015-10-18 03:13:31 +0530206#endif
Channagoud Kadabi86b0c112016-03-16 19:23:16 -0700207/*As per spec delay wait time before shutdown in Red state*/
208#define DELAY_WAIT 30000
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700209static unsigned page_size = 0;
210static unsigned page_mask = 0;
Gaurav Nebhwani4d2389c2016-03-17 21:10:05 +0530211static unsigned mmc_blocksize = 0;
212static unsigned mmc_blocksize_mask = 0;
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700213static char ffbm_mode_string[FFBM_MODE_BUF_SIZE];
214static bool boot_into_ffbm;
vijay kumar870515d2015-08-31 16:37:24 +0530215static char *target_boot_params = NULL;
Matthew Qind886f3c2014-01-17 16:52:01 +0800216static bool boot_reason_alarm;
Channagoud Kadabi80a182b2015-03-11 17:04:23 -0700217static bool devinfo_present = true;
Channagoud Kadabi736c4962015-08-21 11:56:52 -0700218bool boot_into_fastboot = false;
Parth Dixit4097b622016-03-15 14:42:27 +0530219static uint32_t dt_size = 0;
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700220
Shashank Mittalcd98d472011-08-02 14:29:24 -0700221/* Assuming unauthorized kernel image by default */
222static int auth_kernel_img = 0;
Parth Dixit6f5822f2015-10-18 01:20:53 +0530223#if VBOOT_MOTA
224static device_info device = {DEVICE_MAGIC, 0, 0, 0, 0, {0}, {0},{0}};
225#else
lijuang511a2b52015-08-14 20:50:51 +0800226static device_info device = {DEVICE_MAGIC, 0, 0, 0, 0, {0}, {0},{0}, 1};
Parth Dixit6f5822f2015-10-18 01:20:53 +0530227#endif
vijay kumarc65876c2015-04-24 13:29:16 +0530228static bool is_allow_unlock = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -0700229
vijay kumarca2e6812015-07-08 20:28:25 +0530230static char frp_ptns[2][8] = {"config","frp"};
231
lijuang511a2b52015-08-14 20:50:51 +0800232static const char *critical_flash_allowed_ptn[] = {
233 "aboot",
234 "rpm",
235 "tz",
236 "sbl",
237 "sdi",
238 "sbl1",
239 "xbl",
240 "hyp",
241 "pmic",
242 "bootloader",
243 "devinfo",
244 "partition"};
245
Dima Zavin42168f22009-01-30 11:52:22 -0800246struct atag_ptbl_entry
247{
248 char name[16];
249 unsigned offset;
250 unsigned size;
251 unsigned flags;
252};
253
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -0700254/*
255 * Partition info, required to be published
256 * for fastboot
257 */
258struct getvar_partition_info {
259 const char part_name[MAX_GPT_NAME_SIZE]; /* Partition name */
260 char getvar_size[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for size */
261 char getvar_type[MAX_GET_VAR_NAME_SIZE]; /* fastboot get var name for type */
262 char size_response[MAX_RSP_SIZE]; /* fastboot response for size */
263 char type_response[MAX_RSP_SIZE]; /* fastboot response for type */
264};
265
266/*
267 * Right now, we are publishing the info for only
268 * three partitions
269 */
270struct getvar_partition_info part_info[] =
271{
272 { "system" , "partition-size:", "partition-type:", "", "ext4" },
273 { "userdata", "partition-size:", "partition-type:", "", "ext4" },
274 { "cache" , "partition-size:", "partition-type:", "", "ext4" },
275};
276
277char max_download_size[MAX_RSP_SIZE];
Ameya Thakur11cf1a62013-08-05 12:44:48 -0700278char charger_screen_enabled[MAX_RSP_SIZE];
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800279char sn_buf[13];
Dhaval Patel223ec952013-07-18 14:49:44 -0700280char display_panel_buf[MAX_PANEL_BUF_SIZE];
Unnati Gandhi62c8ab82014-01-24 11:01:01 +0530281char panel_display_mode[MAX_RSP_SIZE];
lijuang102dfa92015-10-09 18:31:03 +0800282
283#if CHECK_BAT_VOLTAGE
lijuang65c5a822015-08-29 16:35:36 +0800284char battery_voltage[MAX_RSP_SIZE];
lijuang102dfa92015-10-09 18:31:03 +0800285char battery_soc_ok [MAX_RSP_SIZE];
286#endif
287
lijuangf16461c2015-08-03 17:09:34 +0800288char get_variant[MAX_RSP_SIZE];
Greg Griscod6250552011-06-29 14:40:23 -0700289
Greg Griscod2471ef2011-07-14 13:00:42 -0700290extern int emmc_recovery_init(void);
291
Kinson Chik0b1c8162011-08-31 16:31:57 -0700292#if NO_KEYPAD_DRIVER
293extern int fastboot_trigger(void);
294#endif
Greg Griscod2471ef2011-07-14 13:00:42 -0700295
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800296static void update_ker_tags_rdisk_addr(struct boot_img_hdr *hdr, bool is_arm64)
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700297{
298 /* overwrite the destination of specified for the project */
Channagoud Kadabi7042fa32013-04-26 16:44:14 -0700299#ifdef ABOOT_IGNORE_BOOT_HEADER_ADDRS
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800300 if (is_arm64)
301 hdr->kernel_addr = ABOOT_FORCE_KERNEL64_ADDR;
302 else
303 hdr->kernel_addr = ABOOT_FORCE_KERNEL_ADDR;
Channagoud Kadabi7042fa32013-04-26 16:44:14 -0700304 hdr->ramdisk_addr = ABOOT_FORCE_RAMDISK_ADDR;
305 hdr->tags_addr = ABOOT_FORCE_TAGS_ADDR;
Channagoud Kadabia22144f2013-03-20 11:49:01 -0700306#endif
307}
308
Dima Zavin42168f22009-01-30 11:52:22 -0800309static void ptentry_to_tag(unsigned **ptr, struct ptentry *ptn)
310{
311 struct atag_ptbl_entry atag_ptn;
312
313 memcpy(atag_ptn.name, ptn->name, 16);
314 atag_ptn.name[15] = '\0';
315 atag_ptn.offset = ptn->start;
316 atag_ptn.size = ptn->length;
317 atag_ptn.flags = ptn->flags;
318 memcpy(*ptr, &atag_ptn, sizeof(struct atag_ptbl_entry));
319 *ptr += sizeof(struct atag_ptbl_entry) / sizeof(unsigned);
320}
Brian Swetland9c4c0752009-01-25 16:23:50 -0800321
lijuang102dfa92015-10-09 18:31:03 +0800322#if CHECK_BAT_VOLTAGE
323void update_battery_status(void)
324{
325 snprintf(battery_voltage,MAX_RSP_SIZE, "%d",target_get_battery_voltage());
326 snprintf(battery_soc_ok ,MAX_RSP_SIZE, "%s",target_battery_soc_ok()? "yes":"no");
327}
328#endif
329
Neeti Desaie245d492012-06-01 12:52:13 -0700330unsigned char *update_cmdline(const char * cmdline)
Brian Swetland9c4c0752009-01-25 16:23:50 -0800331{
David Ng183a7422009-12-07 14:55:21 -0800332 int cmdline_len = 0;
333 int have_cmdline = 0;
Amol Jadi168b7712012-03-06 16:15:00 -0800334 unsigned char *cmdline_final = NULL;
Neeti Desaie245d492012-06-01 12:52:13 -0700335 int pause_at_bootup = 0;
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800336 bool warm_boot = false;
Pavel Nedev5614d222013-06-17 18:01:02 +0300337 bool gpt_exists = partition_gpt_exists();
Joonwoo Park61112782013-10-02 19:50:39 -0700338 int have_target_boot_params = 0;
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700339 char *boot_dev_buf = NULL;
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200340 bool is_mdtp_activated = 0;
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700341#if VERIFIED_BOOT
Parth Dixitddbc7352015-10-18 03:13:31 +0530342#if !VBOOT_MOTA
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700343 uint32_t boot_state = boot_verify_get_state();
344#endif
Parth Dixitddbc7352015-10-18 03:13:31 +0530345#endif
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700346
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200347#ifdef MDTP_SUPPORT
348 mdtp_activated(&is_mdtp_activated);
349#endif /* MDTP_SUPPORT */
Dima Zavin42168f22009-01-30 11:52:22 -0800350
Brian Swetland9c4c0752009-01-25 16:23:50 -0800351 if (cmdline && cmdline[0]) {
David Ng183a7422009-12-07 14:55:21 -0800352 cmdline_len = strlen(cmdline);
353 have_cmdline = 1;
354 }
355 if (target_is_emmc_boot()) {
356 cmdline_len += strlen(emmc_cmdline);
Sridhar Parasuram7bd4aaf2015-02-12 11:14:38 -0800357#if USE_BOOTDEV_CMDLINE
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700358 boot_dev_buf = (char *) malloc(sizeof(char) * BOOT_DEV_MAX_LEN);
359 ASSERT(boot_dev_buf);
360 platform_boot_dev_cmdline(boot_dev_buf);
361 cmdline_len += strlen(boot_dev_buf);
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -0700362#endif
David Ng183a7422009-12-07 14:55:21 -0800363 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800364
365 cmdline_len += strlen(usb_sn_cmdline);
366 cmdline_len += strlen(sn_buf);
367
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700368#if VERIFIED_BOOT
Parth Dixitddbc7352015-10-18 03:13:31 +0530369#if !VBOOT_MOTA
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700370 cmdline_len += strlen(verified_state) + strlen(vbsn[boot_state].name);
Sridhar Parasuram7d8bb9a2015-09-23 18:21:19 -0700371 if ((device.verity_mode != 0 ) && (device.verity_mode != 1))
372 {
373 dprintf(CRITICAL, "Devinfo paritition possibly corrupted!!!. Please erase devinfo partition to continue booting\n");
374 ASSERT(0);
375 }
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700376 cmdline_len += strlen(verity_mode) + strlen(vbvm[device.verity_mode].name);
Parth Dixita5715a02015-10-29 12:25:10 +0530377 cmdline_len += strlen(keymaster_v1);
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700378#endif
Parth Dixitddbc7352015-10-18 03:13:31 +0530379#endif
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700380
Pavel Nedev5614d222013-06-17 18:01:02 +0300381 if (boot_into_recovery && gpt_exists)
382 cmdline_len += strlen(secondary_gpt_enable);
383
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200384 if(is_mdtp_activated)
385 cmdline_len += strlen(mdtp_activated_flag);
386
Pavel Nedev328ac822013-04-05 15:25:11 +0300387 if (boot_into_ffbm) {
388 cmdline_len += strlen(androidboot_mode);
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700389 cmdline_len += strlen(ffbm_mode_string);
Pavel Nedev898298c2013-02-27 12:36:09 -0800390 /* reduce kernel console messages to speed-up boot */
391 cmdline_len += strlen(loglevel);
Matthew Qind886f3c2014-01-17 16:52:01 +0800392 } else if (boot_reason_alarm) {
393 cmdline_len += strlen(alarmboot_cmdline);
Zhenhua Huang431dafa2015-06-30 16:13:37 +0800394 } else if ((target_build_variant_user() || device.charger_screen_enabled)
395 && target_pause_for_battery_charge()) {
David Ngf773dde2010-07-26 19:55:08 -0700396 pause_at_bootup = 1;
397 cmdline_len += strlen(battchg_pause);
398 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800399
Shashank Mittalcd98d472011-08-02 14:29:24 -0700400 if(target_use_signed_kernel() && auth_kernel_img) {
401 cmdline_len += strlen(auth_kernel);
402 }
403
Joonwoo Park61112782013-10-02 19:50:39 -0700404 if (get_target_boot_params(cmdline, boot_into_recovery ? "recoveryfs" :
405 "system",
vijay kumar870515d2015-08-31 16:37:24 +0530406 &target_boot_params) == 0) {
Joonwoo Park61112782013-10-02 19:50:39 -0700407 have_target_boot_params = 1;
408 cmdline_len += strlen(target_boot_params);
409 }
410
Ajay Dudanid04110c2011-01-17 23:55:07 -0800411 /* Determine correct androidboot.baseband to use */
412 switch(target_baseband())
413 {
414 case BASEBAND_APQ:
415 cmdline_len += strlen(baseband_apq);
416 break;
417
418 case BASEBAND_MSM:
419 cmdline_len += strlen(baseband_msm);
420 break;
421
422 case BASEBAND_CSFB:
423 cmdline_len += strlen(baseband_csfb);
424 break;
425
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800426 case BASEBAND_SVLTE2A:
427 cmdline_len += strlen(baseband_svlte2a);
Ajay Dudanid04110c2011-01-17 23:55:07 -0800428 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700429
430 case BASEBAND_MDM:
431 cmdline_len += strlen(baseband_mdm);
432 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700433
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800434 case BASEBAND_MDM2:
435 cmdline_len += strlen(baseband_mdm2);
436 break;
437
Amol Jadi5c61a952012-05-04 17:05:35 -0700438 case BASEBAND_SGLTE:
439 cmdline_len += strlen(baseband_sglte);
440 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530441
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800442 case BASEBAND_SGLTE2:
443 cmdline_len += strlen(baseband_sglte2);
444 break;
445
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530446 case BASEBAND_DSDA:
447 cmdline_len += strlen(baseband_dsda);
448 break;
Amol Jadi2a15a272013-01-22 12:03:36 -0800449
450 case BASEBAND_DSDA2:
451 cmdline_len += strlen(baseband_dsda2);
452 break;
Vijay Kumar Pendotib228cfc2016-06-13 20:15:23 +0530453 case BASEBAND_APQ_NOWGR:
454 cmdline_len += strlen(baseband_apq_nowgr);
455 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800456 }
457
Vladislav Levenetz3cd45242017-04-05 08:58:07 +0300458#if ENABLE_DISPLAY
Lijuan Gao4a5b8322014-07-24 10:38:42 +0800459 if (cmdline) {
460 if ((strstr(cmdline, DISPLAY_DEFAULT_PREFIX) == NULL) &&
Padmanabhan Komandurubccbcdc2015-06-30 16:19:24 +0530461 target_display_panel_node(display_panel_buf,
462 MAX_PANEL_BUF_SIZE) &&
Lijuan Gao4a5b8322014-07-24 10:38:42 +0800463 strlen(display_panel_buf)) {
464 cmdline_len += strlen(display_panel_buf);
465 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700466 }
Vladislav Levenetz3cd45242017-04-05 08:58:07 +0300467#endif
Dhaval Patel223ec952013-07-18 14:49:44 -0700468
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800469 if (target_warm_boot()) {
470 warm_boot = true;
471 cmdline_len += strlen(warmboot_cmdline);
472 }
473
Channagoud Kadabi85c7ec32016-01-28 23:09:21 -0800474#if TARGET_CMDLINE_SUPPORT
475 char *target_cmdline_buf = malloc(TARGET_MAX_CMDLNBUF);
476 int target_cmd_line_len;
477 ASSERT(target_cmdline_buf);
478 target_cmd_line_len = target_update_cmdline(target_cmdline_buf);
479 cmdline_len += target_cmd_line_len;
480#endif
481
David Ng183a7422009-12-07 14:55:21 -0800482 if (cmdline_len > 0) {
483 const char *src;
Maria Yu52254c02014-07-04 16:14:54 +0800484 unsigned char *dst;
485
486 cmdline_final = (unsigned char*) malloc((cmdline_len + 4) & (~3));
487 ASSERT(cmdline_final != NULL);
vijay kumar287bb542015-09-29 13:01:52 +0530488 memset((void *)cmdline_final, 0, sizeof(*cmdline_final));
Maria Yu52254c02014-07-04 16:14:54 +0800489 dst = cmdline_final;
Neeti Desaie245d492012-06-01 12:52:13 -0700490
Amol Jadi168b7712012-03-06 16:15:00 -0800491 /* Save start ptr for debug print */
David Ng183a7422009-12-07 14:55:21 -0800492 if (have_cmdline) {
493 src = cmdline;
494 while ((*dst++ = *src++));
495 }
496 if (target_is_emmc_boot()) {
497 src = emmc_cmdline;
498 if (have_cmdline) --dst;
David Ngf773dde2010-07-26 19:55:08 -0700499 have_cmdline = 1;
500 while ((*dst++ = *src++));
Sridhar Parasuram7bd4aaf2015-02-12 11:14:38 -0800501#if USE_BOOTDEV_CMDLINE
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700502 src = boot_dev_buf;
503 if (have_cmdline) --dst;
504 while ((*dst++ = *src++));
505#endif
David Ngf773dde2010-07-26 19:55:08 -0700506 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800507
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700508#if VERIFIED_BOOT
Parth Dixitddbc7352015-10-18 03:13:31 +0530509#if !VBOOT_MOTA
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700510 src = verified_state;
511 if(have_cmdline) --dst;
512 have_cmdline = 1;
513 while ((*dst++ = *src++));
514 src = vbsn[boot_state].name;
515 if(have_cmdline) --dst;
516 while ((*dst++ = *src++));
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700517
Sridhar Parasuram7d8bb9a2015-09-23 18:21:19 -0700518 if ((device.verity_mode != 0 ) && (device.verity_mode != 1))
519 {
520 dprintf(CRITICAL, "Devinfo paritition possibly corrupted!!!. Please erase devinfo partition to continue booting\n");
521 ASSERT(0);
522 }
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700523 src = verity_mode;
524 if(have_cmdline) --dst;
525 while ((*dst++ = *src++));
526 src = vbvm[device.verity_mode].name;
527 if(have_cmdline) -- dst;
528 while ((*dst++ = *src++));
Parth Dixita5715a02015-10-29 12:25:10 +0530529 src = keymaster_v1;
530 if(have_cmdline) --dst;
531 while ((*dst++ = *src++));
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700532#endif
Parth Dixitddbc7352015-10-18 03:13:31 +0530533#endif
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800534 src = usb_sn_cmdline;
535 if (have_cmdline) --dst;
536 have_cmdline = 1;
537 while ((*dst++ = *src++));
538 src = sn_buf;
539 if (have_cmdline) --dst;
540 have_cmdline = 1;
541 while ((*dst++ = *src++));
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800542 if (warm_boot) {
543 if (have_cmdline) --dst;
544 src = warmboot_cmdline;
545 while ((*dst++ = *src++));
546 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800547
Pavel Nedev5614d222013-06-17 18:01:02 +0300548 if (boot_into_recovery && gpt_exists) {
549 src = secondary_gpt_enable;
550 if (have_cmdline) --dst;
551 while ((*dst++ = *src++));
552 }
553
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200554 if (is_mdtp_activated) {
555 src = mdtp_activated_flag;
556 if (have_cmdline) --dst;
557 while ((*dst++ = *src++));
558 }
559
Pavel Nedev328ac822013-04-05 15:25:11 +0300560 if (boot_into_ffbm) {
561 src = androidboot_mode;
562 if (have_cmdline) --dst;
563 while ((*dst++ = *src++));
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700564 src = ffbm_mode_string;
Pavel Nedev328ac822013-04-05 15:25:11 +0300565 if (have_cmdline) --dst;
566 while ((*dst++ = *src++));
Pavel Nedev898298c2013-02-27 12:36:09 -0800567 src = loglevel;
568 if (have_cmdline) --dst;
569 while ((*dst++ = *src++));
Matthew Qind886f3c2014-01-17 16:52:01 +0800570 } else if (boot_reason_alarm) {
571 src = alarmboot_cmdline;
572 if (have_cmdline) --dst;
573 while ((*dst++ = *src++));
Pavel Nedev328ac822013-04-05 15:25:11 +0300574 } else if (pause_at_bootup) {
David Ngf773dde2010-07-26 19:55:08 -0700575 src = battchg_pause;
576 if (have_cmdline) --dst;
David Ng183a7422009-12-07 14:55:21 -0800577 while ((*dst++ = *src++));
578 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800579
Shashank Mittalcd98d472011-08-02 14:29:24 -0700580 if(target_use_signed_kernel() && auth_kernel_img) {
581 src = auth_kernel;
582 if (have_cmdline) --dst;
583 while ((*dst++ = *src++));
584 }
585
Ajay Dudanid04110c2011-01-17 23:55:07 -0800586 switch(target_baseband())
587 {
588 case BASEBAND_APQ:
589 src = baseband_apq;
590 if (have_cmdline) --dst;
591 while ((*dst++ = *src++));
592 break;
593
594 case BASEBAND_MSM:
595 src = baseband_msm;
596 if (have_cmdline) --dst;
597 while ((*dst++ = *src++));
598 break;
599
600 case BASEBAND_CSFB:
601 src = baseband_csfb;
602 if (have_cmdline) --dst;
603 while ((*dst++ = *src++));
604 break;
605
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800606 case BASEBAND_SVLTE2A:
607 src = baseband_svlte2a;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800608 if (have_cmdline) --dst;
609 while ((*dst++ = *src++));
610 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700611
612 case BASEBAND_MDM:
613 src = baseband_mdm;
614 if (have_cmdline) --dst;
615 while ((*dst++ = *src++));
616 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700617
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800618 case BASEBAND_MDM2:
619 src = baseband_mdm2;
620 if (have_cmdline) --dst;
621 while ((*dst++ = *src++));
622 break;
623
Amol Jadi5c61a952012-05-04 17:05:35 -0700624 case BASEBAND_SGLTE:
625 src = baseband_sglte;
626 if (have_cmdline) --dst;
627 while ((*dst++ = *src++));
628 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530629
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800630 case BASEBAND_SGLTE2:
631 src = baseband_sglte2;
632 if (have_cmdline) --dst;
633 while ((*dst++ = *src++));
634 break;
635
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530636 case BASEBAND_DSDA:
637 src = baseband_dsda;
638 if (have_cmdline) --dst;
639 while ((*dst++ = *src++));
640 break;
Amol Jadi2a15a272013-01-22 12:03:36 -0800641
642 case BASEBAND_DSDA2:
643 src = baseband_dsda2;
644 if (have_cmdline) --dst;
645 while ((*dst++ = *src++));
646 break;
Vijay Kumar Pendotib228cfc2016-06-13 20:15:23 +0530647 case BASEBAND_APQ_NOWGR:
648 src = baseband_apq_nowgr;
649 if (have_cmdline) --dst;
650 while ((*dst++ = *src++));
651 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800652 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700653
654 if (strlen(display_panel_buf)) {
Dhaval Patel223ec952013-07-18 14:49:44 -0700655 src = display_panel_buf;
656 if (have_cmdline) --dst;
657 while ((*dst++ = *src++));
658 }
Joonwoo Park61112782013-10-02 19:50:39 -0700659
660 if (have_target_boot_params) {
661 if (have_cmdline) --dst;
662 src = target_boot_params;
663 while ((*dst++ = *src++));
vijay kumar870515d2015-08-31 16:37:24 +0530664 free(target_boot_params);
Joonwoo Park61112782013-10-02 19:50:39 -0700665 }
Channagoud Kadabi85c7ec32016-01-28 23:09:21 -0800666
667#if TARGET_CMDLINE_SUPPORT
668 if (target_cmdline_buf && target_cmd_line_len)
669 {
670 if (have_cmdline) --dst;
671 src = target_cmdline_buf;
672 while((*dst++ = *src++));
673 free(target_cmdline_buf);
674 }
675#endif
Neeti Desaie245d492012-06-01 12:52:13 -0700676 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700677
678
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700679 if (boot_dev_buf)
680 free(boot_dev_buf);
681
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -0800682 if (cmdline_final)
683 dprintf(INFO, "cmdline: %s\n", cmdline_final);
684 else
685 dprintf(INFO, "cmdline is NULL\n");
Neeti Desaie245d492012-06-01 12:52:13 -0700686 return cmdline_final;
687}
688
689unsigned *atag_core(unsigned *ptr)
690{
691 /* CORE */
692 *ptr++ = 2;
693 *ptr++ = 0x54410001;
694
695 return ptr;
696
697}
698
699unsigned *atag_ramdisk(unsigned *ptr, void *ramdisk,
700 unsigned ramdisk_size)
701{
702 if (ramdisk_size) {
703 *ptr++ = 4;
704 *ptr++ = 0x54420005;
705 *ptr++ = (unsigned)ramdisk;
706 *ptr++ = ramdisk_size;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800707 }
708
Neeti Desaie245d492012-06-01 12:52:13 -0700709 return ptr;
710}
711
712unsigned *atag_ptable(unsigned **ptr_addr)
713{
714 int i;
715 struct ptable *ptable;
716
717 if ((ptable = flash_get_ptable()) && (ptable->count != 0)) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700718 *(*ptr_addr)++ = 2 + (ptable->count * (sizeof(struct atag_ptbl_entry) /
719 sizeof(unsigned)));
Neeti Desaie245d492012-06-01 12:52:13 -0700720 *(*ptr_addr)++ = 0x4d534d70;
721 for (i = 0; i < ptable->count; ++i)
722 ptentry_to_tag(ptr_addr, ptable_get(ptable, i));
723 }
724
725 return (*ptr_addr);
726}
727
728unsigned *atag_cmdline(unsigned *ptr, const char *cmdline)
729{
730 int cmdline_length = 0;
731 int n;
Neeti Desaie245d492012-06-01 12:52:13 -0700732 char *dest;
733
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800734 cmdline_length = strlen((const char*)cmdline);
Neeti Desaie245d492012-06-01 12:52:13 -0700735 n = (cmdline_length + 4) & (~3);
736
737 *ptr++ = (n / 4) + 2;
738 *ptr++ = 0x54410009;
739 dest = (char *) ptr;
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800740 while ((*dest++ = *cmdline++));
Neeti Desaie245d492012-06-01 12:52:13 -0700741 ptr += (n / 4);
742
743 return ptr;
744}
745
746unsigned *atag_end(unsigned *ptr)
747{
Brian Swetland9c4c0752009-01-25 16:23:50 -0800748 /* END */
749 *ptr++ = 0;
750 *ptr++ = 0;
751
Neeti Desaie245d492012-06-01 12:52:13 -0700752 return ptr;
753}
754
755void generate_atags(unsigned *ptr, const char *cmdline,
756 void *ramdisk, unsigned ramdisk_size)
757{
vijay kumar21a37452015-12-29 16:23:21 +0530758 unsigned *orig_ptr = ptr;
Neeti Desaie245d492012-06-01 12:52:13 -0700759 ptr = atag_core(ptr);
760 ptr = atag_ramdisk(ptr, ramdisk, ramdisk_size);
761 ptr = target_atag_mem(ptr);
762
763 /* Skip NAND partition ATAGS for eMMC boot */
764 if (!target_is_emmc_boot()){
765 ptr = atag_ptable(&ptr);
766 }
767
vijay kumar21a37452015-12-29 16:23:21 +0530768 /*
769 * Atags size filled till + cmdline size + 1 unsigned for 4-byte boundary + 4 unsigned
770 * for atag identifier in atag_cmdline and atag_end should be with in MAX_TAGS_SIZE bytes
771 */
772 if (((ptr - orig_ptr) + strlen(cmdline) + 5 * sizeof(unsigned)) < MAX_TAGS_SIZE) {
773 ptr = atag_cmdline(ptr, cmdline);
774 ptr = atag_end(ptr);
775 }
776 else {
777 dprintf(CRITICAL,"Crossing ATAGs Max size allowed\n");
778 ASSERT(0);
779 }
Neeti Desaie245d492012-06-01 12:52:13 -0700780}
781
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700782typedef void entry_func_ptr(unsigned, unsigned, unsigned*);
Neeti Desaie245d492012-06-01 12:52:13 -0700783void boot_linux(void *kernel, unsigned *tags,
784 const char *cmdline, unsigned machtype,
785 void *ramdisk, unsigned ramdisk_size)
786{
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800787 unsigned char *final_cmdline;
Amol Jadib6be5c12012-11-14 13:39:51 -0800788#if DEVICE_TREE
Neeti Desai17379b82012-06-04 18:42:53 -0700789 int ret = 0;
Amol Jadib6be5c12012-11-14 13:39:51 -0800790#endif
791
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700792 void (*entry)(unsigned, unsigned, unsigned*) = (entry_func_ptr*)(PA((addr_t)kernel));
Deepa Dinamani25a9f762012-11-30 15:57:15 -0800793 uint32_t tags_phys = PA((addr_t)tags);
vijay kumar1a50a642015-11-16 12:41:15 +0530794 struct kernel64_hdr *kptr = ((struct kernel64_hdr*)(PA((addr_t)kernel)));
Deepa Dinamani25a9f762012-11-30 15:57:15 -0800795
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530796 ramdisk = (void *)PA((addr_t)ramdisk);
Neeti Desaie245d492012-06-01 12:52:13 -0700797
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800798 final_cmdline = update_cmdline((const char*)cmdline);
799
Neeti Desai17379b82012-06-04 18:42:53 -0700800#if DEVICE_TREE
Amol Jadib6be5c12012-11-14 13:39:51 -0800801 dprintf(INFO, "Updating device tree: start\n");
802
Neeti Desai17379b82012-06-04 18:42:53 -0700803 /* Update the Device Tree */
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530804 ret = update_device_tree((void *)tags,(const char *)final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -0700805 if(ret)
806 {
807 dprintf(CRITICAL, "ERROR: Updating Device Tree Failed \n");
808 ASSERT(0);
809 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800810 dprintf(INFO, "Updating device tree: done\n");
Neeti Desai17379b82012-06-04 18:42:53 -0700811#else
Neeti Desaie245d492012-06-01 12:52:13 -0700812 /* Generating the Atags */
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800813 generate_atags(tags, final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -0700814#endif
Neeti Desaie245d492012-06-01 12:52:13 -0700815
Maria Yu52254c02014-07-04 16:14:54 +0800816 free(final_cmdline);
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -0700817
818#if VERIFIED_BOOT
lijuangbdd9bb42016-03-01 18:22:17 +0800819#if !VBOOT_MOTA
820 if (device.verity_mode == 0) {
821#if FBCON_DISPLAY_MSG
Channagoud Kadabi86b0c112016-03-16 19:23:16 -0700822#if ENABLE_VB_ATTEST
823 display_bootverify_menu(DISPLAY_MENU_EIO);
Parth Dixit54ac3bb2017-03-07 15:52:48 +0530824 wait_for_users_action();
825 if(!pwr_key_is_pressed)
826 shutdown_device();
Channagoud Kadabi86b0c112016-03-16 19:23:16 -0700827#else
lijuangbdd9bb42016-03-01 18:22:17 +0800828 display_bootverify_menu(DISPLAY_MENU_LOGGING);
Channagoud Kadabi86b0c112016-03-16 19:23:16 -0700829#endif
lijuangbdd9bb42016-03-01 18:22:17 +0800830 wait_for_users_action();
831#else
832 dprintf(CRITICAL,
833 "The dm-verity is not started in enforcing mode.\nWait for 5 seconds before proceeding\n");
834 mdelay(5000);
835#endif
836 }
837
838#endif
839#endif
840
841#if VERIFIED_BOOT
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -0700842 /* Write protect the device info */
Channagoud Kadabi3bd9d1e2015-05-05 16:18:20 -0700843 if (!boot_into_recovery && target_build_variant_user() && devinfo_present && mmc_write_protect("devinfo", 1))
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -0700844 {
845 dprintf(INFO, "Failed to write protect dev info\n");
846 ASSERT(0);
847 }
848#endif
849
Aravind Venkateswaran8f076242014-02-25 16:25:30 -0800850 /* Turn off splash screen if enabled */
851#if DISPLAY_SPLASH_SCREEN
852 target_display_shutdown();
853#endif
854
Veera Sundaram Sankaran67ea0932015-09-25 10:09:30 -0700855 /* Perform target specific cleanup */
856 target_uninit();
Aravind Venkateswaran8f076242014-02-25 16:25:30 -0800857
Deepa Dinamani33734bc2013-03-06 12:16:06 -0800858 dprintf(INFO, "booting linux @ %p, ramdisk @ %p (%d), tags/device tree @ %p\n",
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530859 entry, ramdisk, ramdisk_size, (void *)tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800860
861 enter_critical_section();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700862
Amol Jadi4421e652011-06-16 15:00:48 -0700863 /* do any platform specific cleanup before kernel entry */
864 platform_uninit();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700865
Brian Swetland9c4c0752009-01-25 16:23:50 -0800866 arch_disable_cache(UCACHE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700867
Amol Jadi504f9fe2012-08-16 13:56:48 -0700868#if ARM_WITH_MMU
Brian Swetland9c4c0752009-01-25 16:23:50 -0800869 arch_disable_mmu();
Amol Jadi504f9fe2012-08-16 13:56:48 -0700870#endif
Amol Jadi492d5a52013-03-15 16:12:34 -0700871 bs_set_timestamp(BS_KERNEL_ENTRY);
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800872
873 if (IS_ARM64(kptr))
874 /* Jump to a 64bit kernel */
875 scm_elexec_call((paddr_t)kernel, tags_phys);
876 else
877 /* Jump to a 32bit kernel */
878 entry(0, machtype, (unsigned*)tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800879}
880
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700881/* Function to check if the memory address range falls within the aboot
882 * boundaries.
883 * start: Start of the memory region
884 * size: Size of the memory region
885 */
Vijay Kumar Pendoti9c002ad2016-03-09 13:52:45 +0530886int check_aboot_addr_range_overlap(uintptr_t start, uint32_t size)
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700887{
888 /* Check for boundary conditions. */
Sundarajan Srinivasance2a0ea2013-12-16 17:02:56 -0800889 if ((UINT_MAX - start) < size)
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700890 return -1;
891
892 /* Check for memory overlap. */
893 if ((start < MEMBASE) && ((start + size) <= MEMBASE))
894 return 0;
Channagoud Kadabi94143912013-10-15 12:53:52 -0700895 else if (start >= (MEMBASE + MEMSIZE))
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700896 return 0;
897 else
898 return -1;
899}
900
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800901#define ROUND_TO_PAGE(x,y) (((x) + (y)) & (~(y)))
Brian Swetland9c4c0752009-01-25 16:23:50 -0800902
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800903BUF_DMA_ALIGN(buf, BOOT_IMG_MAX_PAGE_SIZE); //Equal to max-supported pagesize
Amol Jadib6be5c12012-11-14 13:39:51 -0800904#if DEVICE_TREE
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800905BUF_DMA_ALIGN(dt_buf, BOOT_IMG_MAX_PAGE_SIZE);
Amol Jadib6be5c12012-11-14 13:39:51 -0800906#endif
Dima Zavin214cc642009-01-26 11:16:21 -0800907
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700908static void verify_signed_bootimg(uint32_t bootimg_addr, uint32_t bootimg_size)
909{
910 int ret;
Channagoud Kadabia8c623f2015-01-13 14:48:48 -0800911
912#if !VERIFIED_BOOT
Sundarajan Srinivasance54d6e2013-11-11 12:45:00 -0800913#if IMAGE_VERIF_ALGO_SHA1
914 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA1;
915#else
916 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA256;
917#endif
Channagoud Kadabia8c623f2015-01-13 14:48:48 -0800918#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700919
920 /* Assume device is rooted at this time. */
921 device.is_tampered = 1;
922
923 dprintf(INFO, "Authenticating boot image (%d): start\n", bootimg_size);
924
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700925#if VERIFIED_BOOT
926 if(boot_into_recovery)
927 {
928 ret = boot_verify_image((unsigned char *)bootimg_addr,
Unnati Gandhi47051252015-03-02 15:21:09 +0530929 bootimg_size, "/recovery");
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700930 }
931 else
932 {
933 ret = boot_verify_image((unsigned char *)bootimg_addr,
Unnati Gandhi47051252015-03-02 15:21:09 +0530934 bootimg_size, "/boot");
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700935 }
936 boot_verify_print_state();
937#else
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700938 ret = image_verify((unsigned char *)bootimg_addr,
939 (unsigned char *)(bootimg_addr + bootimg_size),
940 bootimg_size,
Sundarajan Srinivasance54d6e2013-11-11 12:45:00 -0800941 auth_algo);
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700942#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700943 dprintf(INFO, "Authenticating boot image: done return value = %d\n", ret);
944
945 if (ret)
946 {
947 /* Authorized kernel */
948 device.is_tampered = 0;
Sundarajan Srinivasan3fb21f12013-09-16 18:36:15 -0700949 auth_kernel_img = 1;
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700950 }
951
Amit Blay4aa292f2015-04-28 21:55:59 +0300952#ifdef MDTP_SUPPORT
953 {
954 /* Verify MDTP lock.
955 * For boot & recovery partitions, use aboot's verification result.
956 */
957 mdtp_ext_partition_verification_t ext_partition;
958 ext_partition.partition = boot_into_recovery ? MDTP_PARTITION_RECOVERY : MDTP_PARTITION_BOOT;
959 ext_partition.integrity_state = device.is_tampered ? MDTP_PARTITION_STATE_INVALID : MDTP_PARTITION_STATE_VALID;
960 ext_partition.page_size = 0; /* Not needed since already validated */
961 ext_partition.image_addr = 0; /* Not needed since already validated */
962 ext_partition.image_size = 0; /* Not needed since already validated */
963 ext_partition.sig_avail = FALSE; /* Not needed since already validated */
964 mdtp_fwlock_verify_lock(&ext_partition);
965 }
966#endif /* MDTP_SUPPORT */
967
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700968#if USE_PCOM_SECBOOT
969 set_tamper_flag(device.is_tampered);
970#endif
971
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700972#if VERIFIED_BOOT
Sridhar Parasuram8b792422015-07-05 11:38:13 -0700973 switch(boot_verify_get_state())
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700974 {
Sridhar Parasuram8b792422015-07-05 11:38:13 -0700975 case RED:
lijuanga40d6302015-07-20 20:10:13 +0800976#if FBCON_DISPLAY_MSG
lijuang9a7d3b92015-11-30 14:41:24 +0800977 display_bootverify_menu(DISPLAY_MENU_RED);
Channagoud Kadabi86b0c112016-03-16 19:23:16 -0700978#if ENABLE_VB_ATTEST
979 mdelay(DELAY_WAIT);
980 shutdown_device();
981#else
lijuanga40d6302015-07-20 20:10:13 +0800982 wait_for_users_action();
Channagoud Kadabi86b0c112016-03-16 19:23:16 -0700983#endif
lijuanga40d6302015-07-20 20:10:13 +0800984#else
985 dprintf(CRITICAL,
986 "Your device has failed verification and may not work properly.\nWait for 5 seconds before proceeding\n");
987 mdelay(5000);
988#endif
989
990 break;
Sridhar Parasuram8b792422015-07-05 11:38:13 -0700991 case YELLOW:
lijuanga40d6302015-07-20 20:10:13 +0800992#if FBCON_DISPLAY_MSG
lijuang9a7d3b92015-11-30 14:41:24 +0800993 display_bootverify_menu(DISPLAY_MENU_YELLOW);
lijuanga40d6302015-07-20 20:10:13 +0800994 wait_for_users_action();
995#else
996 dprintf(CRITICAL,
997 "Your device has loaded a different operating system.\nWait for 5 seconds before proceeding\n");
998 mdelay(5000);
999#endif
1000 break;
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001001 default:
lijuanga40d6302015-07-20 20:10:13 +08001002 break;
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001003 }
1004#endif
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001005#if !VERIFIED_BOOT
Unnati Gandhi1be04752015-03-27 19:41:53 +05301006 if(device.is_tampered)
1007 {
1008 write_device_info_mmc(&device);
1009 #ifdef TZ_TAMPER_FUSE
1010 set_tamper_fuse_cmd();
1011 #endif
1012 #ifdef ASSERT_ON_TAMPER
1013 dprintf(CRITICAL, "Device is tampered. Asserting..\n");
1014 ASSERT(0);
1015 #endif
1016 }
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001017#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001018}
1019
Maunik Shah0f3c8ac2014-03-02 17:47:58 +05301020static bool check_format_bit()
1021{
1022 bool ret = false;
1023 int index;
1024 uint64_t offset;
1025 struct boot_selection_info *in = NULL;
1026 char *buf = NULL;
1027
1028 index = partition_get_index("bootselect");
1029 if (index == INVALID_PTN)
1030 {
1031 dprintf(INFO, "Unable to locate /bootselect partition\n");
1032 return ret;
1033 }
1034 offset = partition_get_offset(index);
1035 if(!offset)
1036 {
1037 dprintf(INFO, "partition /bootselect doesn't exist\n");
1038 return ret;
1039 }
1040 buf = (char *) memalign(CACHE_LINE, ROUNDUP(page_size, CACHE_LINE));
Mayank Grover48860402016-11-29 12:34:53 +05301041 mmc_set_lun(partition_get_lun(index));
Maunik Shah0f3c8ac2014-03-02 17:47:58 +05301042 ASSERT(buf);
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301043 if (mmc_read(offset, (uint32_t *)buf, page_size))
Maunik Shah0f3c8ac2014-03-02 17:47:58 +05301044 {
1045 dprintf(INFO, "mmc read failure /bootselect %d\n", page_size);
1046 free(buf);
1047 return ret;
1048 }
1049 in = (struct boot_selection_info *) buf;
1050 if ((in->signature == BOOTSELECT_SIGNATURE) &&
1051 (in->version == BOOTSELECT_VERSION)) {
1052 if ((in->state_info & BOOTSELECT_FORMAT) &&
1053 !(in->state_info & BOOTSELECT_FACTORY))
1054 ret = true;
1055 } else {
1056 dprintf(CRITICAL, "Signature: 0x%08x or version: 0x%08x mismatched of /bootselect\n",
1057 in->signature, in->version);
1058 ASSERT(0);
1059 }
1060 free(buf);
1061 return ret;
1062}
1063
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001064void boot_verifier_init()
1065{
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001066 uint32_t boot_state;
1067 /* Check if device unlock */
1068 if(device.is_unlocked)
1069 {
1070 boot_verify_send_event(DEV_UNLOCK);
1071 boot_verify_print_state();
1072 dprintf(CRITICAL, "Device is unlocked! Skipping verification...\n");
1073 return;
1074 }
1075 else
1076 {
1077 boot_verify_send_event(BOOT_INIT);
1078 }
1079
1080 /* Initialize keystore */
1081 boot_state = boot_verify_keystore_init();
1082 if(boot_state == YELLOW)
1083 {
1084 boot_verify_print_state();
1085 dprintf(CRITICAL, "Keystore verification failed! Continuing anyways...\n");
1086 }
1087}
1088
Shashank Mittal23b8f422010-04-16 19:27:21 -07001089int boot_linux_from_mmc(void)
1090{
1091 struct boot_img_hdr *hdr = (void*) buf;
1092 struct boot_img_hdr *uhdr;
1093 unsigned offset = 0;
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07001094 int rcode;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001095 unsigned long long ptn = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001096 int index = INVALID_PTN;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001097
Shashank Mittalcd98d472011-08-02 14:29:24 -07001098 unsigned char *image_addr = 0;
1099 unsigned kernel_actual;
1100 unsigned ramdisk_actual;
1101 unsigned imagesize_actual;
Neeti Desai465491e2012-07-31 12:53:35 -07001102 unsigned second_actual = 0;
Neeti Desai465491e2012-07-31 12:53:35 -07001103
Matthew Qin271927e2015-03-31 22:07:07 -04001104 unsigned int dtb_size = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08001105 unsigned int out_len = 0;
1106 unsigned int out_avai_len = 0;
1107 unsigned char *out_addr = NULL;
1108 uint32_t dtb_offset = 0;
1109 unsigned char *kernel_start_addr = NULL;
1110 unsigned int kernel_size = 0;
Ameya Thakur10a33452016-06-13 14:24:26 -07001111 unsigned int patched_kernel_hdr_size = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08001112 int rc;
Neeti Desai465491e2012-07-31 12:53:35 -07001113#if DEVICE_TREE
1114 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -07001115 struct dt_entry dt_entry;
Neeti Desai465491e2012-07-31 12:53:35 -07001116 unsigned dt_table_offset;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001117 uint32_t dt_actual;
Deepa Dinamani19648b42013-09-05 17:05:55 -07001118 uint32_t dt_hdr_size;
Matthew Qin271927e2015-03-31 22:07:07 -04001119 unsigned char *best_match_dt_addr = NULL;
Neeti Desai465491e2012-07-31 12:53:35 -07001120#endif
Matthew Qin49e51fa2015-02-09 10:40:45 +08001121 struct kernel64_hdr *kptr = NULL;
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001122
Maunik Shah0f3c8ac2014-03-02 17:47:58 +05301123 if (check_format_bit())
1124 boot_into_recovery = 1;
1125
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07001126 if (!boot_into_recovery) {
1127 memset(ffbm_mode_string, '\0', sizeof(ffbm_mode_string));
1128 rcode = get_ffbm(ffbm_mode_string, sizeof(ffbm_mode_string));
1129 if (rcode <= 0) {
1130 boot_into_ffbm = false;
1131 if (rcode < 0)
1132 dprintf(CRITICAL,"failed to get ffbm cookie");
1133 } else
1134 boot_into_ffbm = true;
1135 } else
1136 boot_into_ffbm = false;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001137 uhdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
1138 if (!memcmp(uhdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1139 dprintf(INFO, "Unified boot method!\n");
1140 hdr = uhdr;
1141 goto unified_boot;
1142 }
Greg Griscod6250552011-06-29 14:40:23 -07001143 if (!boot_into_recovery) {
Kinson Chikf1a43512011-07-14 11:28:39 -07001144 index = partition_get_index("boot");
1145 ptn = partition_get_offset(index);
1146 if(ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -07001147 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1148 return -1;
1149 }
Kinson Chikf1a43512011-07-14 11:28:39 -07001150 }
1151 else {
1152 index = partition_get_index("recovery");
1153 ptn = partition_get_offset(index);
1154 if(ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -07001155 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
1156 return -1;
1157 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001158 }
Channagoud Kadabief0547c2015-02-10 12:57:38 -08001159 /* Set Lun for boot & recovery partitions */
1160 mmc_set_lun(partition_get_lun(index));
Shashank Mittal23b8f422010-04-16 19:27:21 -07001161
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301162 if (mmc_read(ptn + offset, (uint32_t *) buf, page_size)) {
Shashank Mittal23b8f422010-04-16 19:27:21 -07001163 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
1164 return -1;
1165 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001166
1167 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001168 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Shashank Mittal23b8f422010-04-16 19:27:21 -07001169 return -1;
1170 }
1171
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07001172 if (hdr->page_size && (hdr->page_size != page_size)) {
vijay kumar2e21b3a2014-06-26 17:40:15 +05301173
1174 if (hdr->page_size > BOOT_IMG_MAX_PAGE_SIZE) {
1175 dprintf(CRITICAL, "ERROR: Invalid page size\n");
1176 return -1;
1177 }
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07001178 page_size = hdr->page_size;
1179 page_mask = page_size - 1;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001180 }
1181
vijay kumar287bb542015-09-29 13:01:52 +05301182 /* ensure commandline is terminated */
1183 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
1184
Matthew Qin49e51fa2015-02-09 10:40:45 +08001185 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1186 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001187
Matthew Qin49e51fa2015-02-09 10:40:45 +08001188 image_addr = (unsigned char *)target_get_scratch_address();
1189
1190#if DEVICE_TREE
Parth Dixit4097b622016-03-15 14:42:27 +05301191#ifndef OSVERSION_IN_BOOTIMAGE
1192 dt_size = hdr->dt_size;
1193#endif
1194 dt_actual = ROUND_TO_PAGE(dt_size, page_mask);
Vijay Kumar Pendoti208f9622016-06-09 19:34:01 +05301195 if (UINT_MAX < ((uint64_t)kernel_actual + (uint64_t)ramdisk_actual+ (uint64_t)dt_actual + page_size)) {
1196 dprintf(CRITICAL, "Integer overflow detected in bootimage header fields at %u in %s\n",__LINE__,__FILE__);
1197 return -1;
1198 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08001199 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + dt_actual);
1200#else
Vijay Kumar Pendoti208f9622016-06-09 19:34:01 +05301201 if (UINT_MAX < ((uint64_t)kernel_actual + (uint64_t)ramdisk_actual + page_size)) {
1202 dprintf(CRITICAL, "Integer overflow detected in bootimage header fields at %u in %s\n",__LINE__,__FILE__);
1203 return -1;
1204 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08001205 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
1206#endif
1207
1208#if VERIFIED_BOOT
1209 boot_verifier_init();
1210#endif
1211
Vijay Kumar Pendoti9c002ad2016-03-09 13:52:45 +05301212 if (check_aboot_addr_range_overlap((uintptr_t) image_addr, imagesize_actual))
Matthew Qin49e51fa2015-02-09 10:40:45 +08001213 {
1214 dprintf(CRITICAL, "Boot image buffer address overlaps with aboot addresses.\n");
1215 return -1;
1216 }
1217
Matthew Qinbb7923d2015-02-09 10:56:09 +08001218 /*
1219 * Update loading flow of bootimage to support compressed/uncompressed
1220 * bootimage on both 64bit and 32bit platform.
1221 * 1. Load bootimage from emmc partition onto DDR.
1222 * 2. Check if bootimage is gzip format. If yes, decompress compressed kernel
1223 * 3. Check kernel header and update kernel load addr for 64bit and 32bit
1224 * platform accordingly.
1225 * 4. Sanity Check on kernel_addr and ramdisk_addr and copy data.
1226 */
1227
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001228 dprintf(INFO, "Loading (%s) image (%d): start\n",
1229 (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001230 bs_set_timestamp(BS_KERNEL_LOAD_START);
1231
Gaurav Nebhwani43e2a462016-03-17 21:32:56 +05301232 if ((target_get_max_flash_size() - page_size) < imagesize_actual)
1233 {
1234 dprintf(CRITICAL, "booimage size is greater than DDR can hold\n");
1235 return -1;
1236 }
1237
Matthew Qinbb7923d2015-02-09 10:56:09 +08001238 /* Read image without signature */
Matthew Qin49e51fa2015-02-09 10:40:45 +08001239 if (mmc_read(ptn + offset, (void *)image_addr, imagesize_actual))
1240 {
1241 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
1242 return -1;
1243 }
1244
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001245 dprintf(INFO, "Loading (%s) image (%d): done\n",
1246 (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual);
1247
Matthew Qin49e51fa2015-02-09 10:40:45 +08001248 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
1249
1250 /* Authenticate Kernel */
1251 dprintf(INFO, "use_signed_kernel=%d, is_unlocked=%d, is_tampered=%d.\n",
1252 (int) target_use_signed_kernel(),
1253 device.is_unlocked,
1254 device.is_tampered);
1255
Channagoud Kadabi736c4962015-08-21 11:56:52 -07001256 /* Change the condition a little bit to include the test framework support.
1257 * We would never reach this point if device is in fastboot mode, even if we did
1258 * that means we are in test mode, so execute kernel authentication part for the
1259 * tests */
Parth Dixitcb0c6082015-12-30 15:01:13 +05301260 if((target_use_signed_kernel() && (!device.is_unlocked)) || is_test_mode_enabled())
Matthew Qin49e51fa2015-02-09 10:40:45 +08001261 {
1262 offset = imagesize_actual;
Vijay Kumar Pendoti9c002ad2016-03-09 13:52:45 +05301263 if (check_aboot_addr_range_overlap((uintptr_t)image_addr + offset, page_size))
Matthew Qin49e51fa2015-02-09 10:40:45 +08001264 {
1265 dprintf(CRITICAL, "Signature read buffer address overlaps with aboot addresses.\n");
1266 return -1;
1267 }
1268
1269 /* Read signature */
1270 if(mmc_read(ptn + offset, (void *)(image_addr + offset), page_size))
1271 {
1272 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
1273 return -1;
1274 }
1275
1276 verify_signed_bootimg((uint32_t)image_addr, imagesize_actual);
Channagoud Kadabi736c4962015-08-21 11:56:52 -07001277 /* The purpose of our test is done here */
Parth Dixitcb0c6082015-12-30 15:01:13 +05301278 if(is_test_mode_enabled() && auth_kernel_img)
Channagoud Kadabi736c4962015-08-21 11:56:52 -07001279 return 0;
Matthew Qin49e51fa2015-02-09 10:40:45 +08001280 } else {
1281 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
1282 #ifdef TZ_SAVE_KERNEL_HASH
1283 aboot_save_boot_hash_mmc((uint32_t) image_addr, imagesize_actual);
1284 #endif /* TZ_SAVE_KERNEL_HASH */
Amit Blay4aa292f2015-04-28 21:55:59 +03001285
1286#ifdef MDTP_SUPPORT
1287 {
1288 /* Verify MDTP lock.
1289 * For boot & recovery partitions, MDTP will use boot_verifier APIs,
1290 * since verification was skipped in aboot. The signature is not part of the loaded image.
1291 */
1292 mdtp_ext_partition_verification_t ext_partition;
1293 ext_partition.partition = boot_into_recovery ? MDTP_PARTITION_RECOVERY : MDTP_PARTITION_BOOT;
1294 ext_partition.integrity_state = MDTP_PARTITION_STATE_UNSET;
1295 ext_partition.page_size = page_size;
1296 ext_partition.image_addr = (uint32)image_addr;
1297 ext_partition.image_size = imagesize_actual;
1298 ext_partition.sig_avail = FALSE;
1299 mdtp_fwlock_verify_lock(&ext_partition);
1300 }
1301#endif /* MDTP_SUPPORT */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001302 }
1303
Sridhar Parasuram00bfedb2015-05-26 14:21:27 -07001304#if VERIFIED_BOOT
taozhang93088bd2016-11-16 15:50:46 +08001305 if((boot_verify_get_state() == ORANGE) && (!boot_into_ffbm))
Reut Zysmanba8a9d52016-02-18 11:44:04 +02001306 {
1307#if FBCON_DISPLAY_MSG
1308 display_bootverify_menu(DISPLAY_MENU_ORANGE);
1309 wait_for_users_action();
1310#else
1311 dprintf(CRITICAL,
1312 "Your device has been unlocked and can't be trusted.\nWait for 5 seconds before proceeding\n");
1313 mdelay(5000);
1314#endif
1315 }
1316#endif
1317
1318#if VERIFIED_BOOT
Parth Dixitbc9b6492015-10-18 00:41:38 +05301319#if !VBOOT_MOTA
Sridhar Parasuram00bfedb2015-05-26 14:21:27 -07001320 // send root of trust
Sridhar Parasuram96300dc2015-06-11 10:37:11 -07001321 if(!send_rot_command((uint32_t)device.is_unlocked))
Sridhar Parasuram00bfedb2015-05-26 14:21:27 -07001322 ASSERT(0);
1323#endif
Parth Dixitbc9b6492015-10-18 00:41:38 +05301324#endif
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001325 /*
Matthew Qinbb7923d2015-02-09 10:56:09 +08001326 * Check if the kernel image is a gzip package. If yes, need to decompress it.
1327 * If not, continue booting.
1328 */
1329 if (is_gzip_package((unsigned char *)(image_addr + page_size), hdr->kernel_size))
1330 {
1331 out_addr = (unsigned char *)(image_addr + imagesize_actual + page_size);
1332 out_avai_len = target_get_max_flash_size() - imagesize_actual - page_size;
Matthew Qin0b15b322015-05-19 05:20:54 -04001333 dprintf(INFO, "decompressing kernel image: start\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001334 rc = decompress((unsigned char *)(image_addr + page_size),
1335 hdr->kernel_size, out_addr, out_avai_len,
1336 &dtb_offset, &out_len);
1337 if (rc)
1338 {
Matthew Qin0b15b322015-05-19 05:20:54 -04001339 dprintf(CRITICAL, "decompressing kernel image failed!!!\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001340 ASSERT(0);
1341 }
1342
Matthew Qin0b15b322015-05-19 05:20:54 -04001343 dprintf(INFO, "decompressing kernel image: done\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001344 kptr = (struct kernel64_hdr *)out_addr;
1345 kernel_start_addr = out_addr;
1346 kernel_size = out_len;
1347 } else {
Ameya Thakur10a33452016-06-13 14:24:26 -07001348 dprintf(INFO, "Uncpmpressed kernel in use\n");
1349 if (!strncmp((char*)(image_addr + page_size),
1350 PATCHED_KERNEL_MAGIC,
1351 sizeof(PATCHED_KERNEL_MAGIC) - 1)) {
1352 dprintf(INFO, "Patched kernel detected\n");
1353 kptr = (struct kernel64_hdr *)(image_addr + page_size +
1354 PATCHED_KERNEL_HEADER_SIZE);
1355 //The size of the kernel is stored at start of kernel image + 16
1356 //The dtb would start just after the kernel
1357 dtb_offset = *((uint32_t*)((unsigned char*)
1358 (image_addr + page_size +
1359 sizeof(PATCHED_KERNEL_MAGIC) -
1360 1)));
1361 //The actual kernel starts after the 20 byte header.
1362 kernel_start_addr = (unsigned char*)(image_addr +
1363 page_size + PATCHED_KERNEL_HEADER_SIZE);
1364 kernel_size = hdr->kernel_size;
1365 patched_kernel_hdr_size = PATCHED_KERNEL_HEADER_SIZE;
1366 } else {
1367 dprintf(INFO, "Kernel image not patched..Unable to locate dt offset\n");
1368 kptr = (struct kernel64_hdr *)(image_addr + page_size);
1369 kernel_start_addr = (unsigned char *)(image_addr + page_size);
1370 kernel_size = hdr->kernel_size;
1371 }
Matthew Qinbb7923d2015-02-09 10:56:09 +08001372 }
1373
1374 /*
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001375 * Update the kernel/ramdisk/tags address if the boot image header
1376 * has default values, these default values come from mkbootimg when
1377 * the boot image is flashed using fastboot flash:raw
1378 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001379 update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr));
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001380
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001381 /* Get virtual addresses since the hdr saves physical addresses. */
1382 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
1383 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
1384 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001385
Matthew Qinbb7923d2015-02-09 10:56:09 +08001386 kernel_size = ROUND_TO_PAGE(kernel_size, page_mask);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001387 /* Check if the addresses in the header are valid. */
Matthew Qinbb7923d2015-02-09 10:56:09 +08001388 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_size) ||
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001389 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
1390 {
1391 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
1392 return -1;
1393 }
1394
1395#ifndef DEVICE_TREE
1396 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1397 {
1398 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
1399 return -1;
1400 }
1401#endif
1402
Matthew Qin49e51fa2015-02-09 10:40:45 +08001403 /* Move kernel, ramdisk and device tree to correct address */
Matthew Qinbb7923d2015-02-09 10:56:09 +08001404 memmove((void*) hdr->kernel_addr, kernel_start_addr, kernel_size);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001405 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001406
Matthew Qin49e51fa2015-02-09 10:40:45 +08001407 #if DEVICE_TREE
Parth Dixit4097b622016-03-15 14:42:27 +05301408 if(dt_size) {
Matthew Qin49e51fa2015-02-09 10:40:45 +08001409 dt_table_offset = ((uint32_t)image_addr + page_size + kernel_actual + ramdisk_actual + second_actual);
1410 table = (struct dt_table*) dt_table_offset;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001411
Matthew Qin49e51fa2015-02-09 10:40:45 +08001412 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
1413 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1414 return -1;
1415 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001416
P.V. Phani Kumar82916762016-03-09 09:20:19 +05301417 /* Its Error if, dt_hdr_size (table->num_entries * dt_entry size + Dev_Tree Header)
1418 goes beyound hdr->dt_size*/
Parth Dixit4097b622016-03-15 14:42:27 +05301419 if (dt_hdr_size > ROUND_TO_PAGE(dt_size,hdr->page_size)) {
P.V. Phani Kumar82916762016-03-09 09:20:19 +05301420 dprintf(CRITICAL, "ERROR: Invalid Device Tree size \n");
1421 return -1;
1422 }
1423
Matthew Qin49e51fa2015-02-09 10:40:45 +08001424 /* Find index of device tree within device tree table */
1425 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
1426 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1427 return -1;
1428 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001429
P.V. Phani Kumar3c333202016-03-09 11:54:37 +05301430 if(dt_entry.offset > (UINT_MAX - dt_entry.size)) {
1431 dprintf(CRITICAL, "ERROR: Device tree contents are Invalid\n");
1432 return -1;
1433 }
1434
1435 /* Ensure we are not overshooting dt_size with the dt_entry selected */
Parth Dixit4097b622016-03-15 14:42:27 +05301436 if ((dt_entry.offset + dt_entry.size) > dt_size) {
P.V. Phani Kumar3c333202016-03-09 11:54:37 +05301437 dprintf(CRITICAL, "ERROR: Device tree contents are Invalid\n");
1438 return -1;
1439 }
1440
Matthew Qin271927e2015-03-31 22:07:07 -04001441 if (is_gzip_package((unsigned char *)dt_table_offset + dt_entry.offset, dt_entry.size))
1442 {
1443 unsigned int compressed_size = 0;
1444 out_addr += out_len;
1445 out_avai_len -= out_len;
Matthew Qin0b15b322015-05-19 05:20:54 -04001446 dprintf(INFO, "decompressing dtb: start\n");
Matthew Qin271927e2015-03-31 22:07:07 -04001447 rc = decompress((unsigned char *)dt_table_offset + dt_entry.offset,
1448 dt_entry.size, out_addr, out_avai_len,
1449 &compressed_size, &dtb_size);
1450 if (rc)
1451 {
Matthew Qin0b15b322015-05-19 05:20:54 -04001452 dprintf(CRITICAL, "decompressing dtb failed!!!\n");
Matthew Qin271927e2015-03-31 22:07:07 -04001453 ASSERT(0);
1454 }
1455
Matthew Qin0b15b322015-05-19 05:20:54 -04001456 dprintf(INFO, "decompressing dtb: done\n");
Matthew Qin271927e2015-03-31 22:07:07 -04001457 best_match_dt_addr = out_addr;
1458 } else {
1459 best_match_dt_addr = (unsigned char *)dt_table_offset + dt_entry.offset;
1460 dtb_size = dt_entry.size;
1461 }
1462
Matthew Qin49e51fa2015-02-09 10:40:45 +08001463 /* Validate and Read device device tree in the tags_addr */
Matthew Qin271927e2015-03-31 22:07:07 -04001464 if (check_aboot_addr_range_overlap(hdr->tags_addr, dtb_size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001465 {
1466 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1467 return -1;
1468 }
Shashank Mittal162244e2011-08-08 19:01:25 -07001469
Matthew Qin271927e2015-03-31 22:07:07 -04001470 memmove((void *)hdr->tags_addr, (char *)best_match_dt_addr, dtb_size);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001471 } else {
1472 /* Validate the tags_addr */
1473 if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual))
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001474 {
1475 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1476 return -1;
1477 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08001478 /*
1479 * If appended dev tree is found, update the atags with
1480 * memory address to the DTB appended location on RAM.
1481 * Else update with the atags address in the kernel header
1482 */
1483 void *dtb;
Ameya Thakur10a33452016-06-13 14:24:26 -07001484 dtb = dev_tree_appended(
1485 (void*)(image_addr + page_size +
1486 patched_kernel_hdr_size),
1487 hdr->kernel_size, dtb_offset,
1488 (void *)hdr->tags_addr);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001489 if (!dtb) {
1490 dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001491 return -1;
1492 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07001493 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08001494 #endif
Shashank Mittal23b8f422010-04-16 19:27:21 -07001495
Stanimir Varbanov69ec5462013-07-18 18:17:42 +03001496 if (boot_into_recovery && !device.is_unlocked && !device.is_tampered)
1497 target_load_ssd_keystore();
1498
Shashank Mittal23b8f422010-04-16 19:27:21 -07001499unified_boot:
Shashank Mittal23b8f422010-04-16 19:27:21 -07001500
Dima Zavin77e41f32013-03-06 16:10:43 -08001501 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07001502 (const char *)hdr->cmdline, board_machtype(),
Shashank Mittal23b8f422010-04-16 19:27:21 -07001503 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
1504
1505 return 0;
1506}
1507
Dima Zavin214cc642009-01-26 11:16:21 -08001508int boot_linux_from_flash(void)
1509{
1510 struct boot_img_hdr *hdr = (void*) buf;
Dima Zavin214cc642009-01-26 11:16:21 -08001511 struct ptentry *ptn;
1512 struct ptable *ptable;
1513 unsigned offset = 0;
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001514
Shashank Mittalcd98d472011-08-02 14:29:24 -07001515 unsigned char *image_addr = 0;
1516 unsigned kernel_actual;
1517 unsigned ramdisk_actual;
1518 unsigned imagesize_actual;
vijay kumar8f53e362015-11-24 13:38:11 +05301519 unsigned second_actual = 0;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001520
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001521#if DEVICE_TREE
1522 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -07001523 struct dt_entry dt_entry;
vijay kumar8f53e362015-11-24 13:38:11 +05301524 unsigned dt_table_offset;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001525 uint32_t dt_actual;
Deepa Dinamani19648b42013-09-05 17:05:55 -07001526 uint32_t dt_hdr_size;
vijay kumar8f53e362015-11-24 13:38:11 +05301527 unsigned int dtb_size = 0;
1528 unsigned char *best_match_dt_addr = NULL;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001529#endif
1530
David Ng183a7422009-12-07 14:55:21 -08001531 if (target_is_emmc_boot()) {
1532 hdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
1533 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1534 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
1535 return -1;
1536 }
1537 goto continue_boot;
1538 }
1539
Dima Zavin214cc642009-01-26 11:16:21 -08001540 ptable = flash_get_ptable();
1541 if (ptable == NULL) {
1542 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1543 return -1;
1544 }
1545
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001546 if(!boot_into_recovery)
1547 {
1548 ptn = ptable_find(ptable, "boot");
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001549
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001550 if (ptn == NULL) {
1551 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1552 return -1;
1553 }
1554 }
1555 else
1556 {
1557 ptn = ptable_find(ptable, "recovery");
1558 if (ptn == NULL) {
1559 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
1560 return -1;
1561 }
Dima Zavin214cc642009-01-26 11:16:21 -08001562 }
1563
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001564 if (flash_read(ptn, offset, buf, page_size)) {
Dima Zavin214cc642009-01-26 11:16:21 -08001565 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
1566 return -1;
1567 }
Dima Zavin214cc642009-01-26 11:16:21 -08001568
1569 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001570 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Dima Zavin214cc642009-01-26 11:16:21 -08001571 return -1;
1572 }
1573
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001574 if (hdr->page_size != page_size) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001575 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 -08001576 return -1;
1577 }
1578
vijay kumar287bb542015-09-29 13:01:52 +05301579 /* ensure commandline is terminated */
1580 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
1581
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001582 /*
1583 * Update the kernel/ramdisk/tags address if the boot image header
1584 * has default values, these default values come from mkbootimg when
1585 * the boot image is flashed using fastboot flash:raw
1586 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001587 update_ker_tags_rdisk_addr(hdr, false);
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001588
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001589 /* Get virtual addresses since the hdr saves physical addresses. */
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001590 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
1591 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
1592 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
1593
1594 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1595 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1596
1597 /* Check if the addresses in the header are valid. */
1598 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
1599 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
1600 {
1601 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
1602 return -1;
1603 }
1604
1605#ifndef DEVICE_TREE
Vijay Kumar Pendotid3ed20e2016-09-20 00:34:46 +05301606 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1607 {
1608 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
1609 return -1;
1610 }
1611#else
1612
1613#ifndef OSVERSION_IN_BOOTIMAGE
1614 dt_size = hdr->dt_size;
1615#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001616#endif
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001617
Shashank Mittalcd98d472011-08-02 14:29:24 -07001618 /* Authenticate Kernel */
Deepa Dinamani23b60d42013-06-24 18:10:52 -07001619 if(target_use_signed_kernel() && (!device.is_unlocked))
Shashank Mittalcd98d472011-08-02 14:29:24 -07001620 {
1621 image_addr = (unsigned char *)target_get_scratch_address();
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001622 offset = 0;
1623
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001624#if DEVICE_TREE
Parth Dixit4097b622016-03-15 14:42:27 +05301625 dt_actual = ROUND_TO_PAGE(dt_size, page_mask);
vijay kumar966a9822015-12-09 18:36:09 +05301626 if (UINT_MAX < ((uint64_t)kernel_actual + (uint64_t)ramdisk_actual+ (uint64_t)dt_actual + page_size)) {
1627 dprintf(CRITICAL, "Integer overflow detected in bootimage header fields\n");
1628 return -1;
1629 }
1630
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001631 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + dt_actual);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001632
Parth Dixit4097b622016-03-15 14:42:27 +05301633 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001634 {
1635 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1636 return -1;
1637 }
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001638#else
vijay kumar966a9822015-12-09 18:36:09 +05301639 if (UINT_MAX < ((uint64_t)kernel_actual + (uint64_t)ramdisk_actual+ page_size)) {
1640 dprintf(CRITICAL, "Integer overflow detected in bootimage header fields\n");
1641 return -1;
1642 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07001643 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001644#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001645
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001646 dprintf(INFO, "Loading (%s) image (%d): start\n",
1647 (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001648 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -08001649
Vijay Kumar Pendotib3458362016-07-05 13:45:54 +05301650 if (UINT_MAX - page_size < imagesize_actual)
1651 {
1652 dprintf(CRITICAL,"Integer overflow detected in bootimage header fields %u %s\n", __LINE__,__func__);
1653 return -1;
1654 }
1655
1656 /*Check the availability of RAM before reading boot image + max signature length from flash*/
1657 if (target_get_max_flash_size() < (imagesize_actual + page_size))
1658 {
1659 dprintf(CRITICAL, "bootimage size is greater than DDR can hold\n");
1660 return -1;
1661 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07001662 /* Read image without signature */
1663 if (flash_read(ptn, offset, (void *)image_addr, imagesize_actual))
1664 {
1665 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
1666 return -1;
1667 }
Dima Zavin214cc642009-01-26 11:16:21 -08001668
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001669 dprintf(INFO, "Loading (%s) image (%d): done\n",
1670 (!boot_into_recovery ? "boot" : "recovery"), imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001671 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Amol Jadib6be5c12012-11-14 13:39:51 -08001672
Shashank Mittalcd98d472011-08-02 14:29:24 -07001673 offset = imagesize_actual;
1674 /* Read signature */
1675 if (flash_read(ptn, offset, (void *)(image_addr + offset), page_size))
1676 {
1677 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001678 return -1;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001679 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07001680
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301681 verify_signed_bootimg((uint32_t)image_addr, imagesize_actual);
Shashank Mittalcd98d472011-08-02 14:29:24 -07001682
1683 /* Move kernel and ramdisk to correct address */
Matthew Qinbb7923d2015-02-09 10:56:09 +08001684 memmove((void*) hdr->kernel_addr, (char*) (image_addr + page_size), hdr->kernel_size);
1685 memmove((void*) hdr->ramdisk_addr, (char*) (image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001686#if DEVICE_TREE
Parth Dixit4097b622016-03-15 14:42:27 +05301687 if(dt_size != 0) {
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001688
vijay kumar8f53e362015-11-24 13:38:11 +05301689 dt_table_offset = ((uint32_t)image_addr + page_size + kernel_actual + ramdisk_actual + second_actual);
1690
1691 table = (struct dt_table*) dt_table_offset;
1692
1693 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0){
1694 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1695 return -1;
1696 }
1697
1698 /* Find index of device tree within device tree table */
1699 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
1700 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1701 return -1;
1702 }
1703
1704 /* Validate and Read device device tree in the "tags_add */
1705 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size)){
1706 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1707 return -1;
1708 }
1709
Mayank Groverebf85ae2017-01-25 13:35:37 +05301710 if(dt_entry.offset > (UINT_MAX - dt_entry.size)) {
1711 dprintf(CRITICAL, "ERROR: Device tree contents are Invalid\n");
1712 return -1;
1713 }
1714
1715 /* Ensure we are not overshooting dt_size with the dt_entry selected */
1716 if ((dt_entry.offset + dt_entry.size) > dt_size) {
1717 dprintf(CRITICAL, "ERROR: Device tree contents are Invalid\n");
1718 return -1;
1719 }
1720
vijay kumar8f53e362015-11-24 13:38:11 +05301721 best_match_dt_addr = (unsigned char *)table + dt_entry.offset;
1722 dtb_size = dt_entry.size;
1723 memmove((void *)hdr->tags_addr, (char *)best_match_dt_addr, dtb_size);
1724 }
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001725#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001726
1727 /* Make sure everything from scratch address is read before next step!*/
Shashank Mittala0032282011-08-26 14:50:11 -07001728 if(device.is_tampered)
Shashank Mittal162244e2011-08-08 19:01:25 -07001729 {
1730 write_device_info_flash(&device);
1731 }
Channagoud Kadabi5c86fe32012-02-16 10:58:48 +05301732#if USE_PCOM_SECBOOT
1733 set_tamper_flag(device.is_tampered);
1734#endif
Shashank Mittalcd98d472011-08-02 14:29:24 -07001735 }
1736 else
1737 {
Shashank Mittal162244e2011-08-08 19:01:25 -07001738 offset = page_size;
1739
Amol Jadib6be5c12012-11-14 13:39:51 -08001740 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1741 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1742 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
1743
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001744 dprintf(INFO, "Loading (%s) image (%d): start\n",
1745 (!boot_into_recovery ? "boot" : "recovery"), kernel_actual + ramdisk_actual);
1746
Amol Jadi492d5a52013-03-15 16:12:34 -07001747 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -08001748
Vijay Kumar Pendoti47bc1282016-05-05 15:46:17 +05301749 if (UINT_MAX - offset < kernel_actual)
1750 {
1751 dprintf(CRITICAL, "ERROR: Integer overflow in boot image header %s\t%d\n",__func__,__LINE__);
1752 return -1;
1753 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001754 if (flash_read(ptn, offset, (void *)hdr->kernel_addr, kernel_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -07001755 dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
1756 return -1;
1757 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001758 offset += kernel_actual;
Vijay Kumar Pendoti47bc1282016-05-05 15:46:17 +05301759 if (UINT_MAX - offset < ramdisk_actual)
1760 {
1761 dprintf(CRITICAL, "ERROR: Integer overflow in boot image header %s\t%d\n",__func__,__LINE__);
1762 return -1;
1763 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001764 if (flash_read(ptn, offset, (void *)hdr->ramdisk_addr, ramdisk_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -07001765 dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
1766 return -1;
1767 }
Vijay Kumar Pendoti47bc1282016-05-05 15:46:17 +05301768
Amol Jadib6be5c12012-11-14 13:39:51 -08001769 offset += ramdisk_actual;
1770
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001771 dprintf(INFO, "Loading (%s) image (%d): done\n",
1772 (!boot_into_recovery ? "boot" : "recovery"), kernel_actual + ramdisk_actual);
1773
Amol Jadi492d5a52013-03-15 16:12:34 -07001774 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001775
1776 if(hdr->second_size != 0) {
Vijay Kumar Pendoti47bc1282016-05-05 15:46:17 +05301777 if (UINT_MAX - offset < second_actual)
1778 {
1779 dprintf(CRITICAL, "ERROR: Integer overflow in boot image header %s\t%d\n",__func__,__LINE__);
1780 return -1;
1781 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001782 offset += second_actual;
1783 /* Second image loading not implemented. */
1784 ASSERT(0);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001785 }
1786
1787#if DEVICE_TREE
Parth Dixit4097b622016-03-15 14:42:27 +05301788 if(dt_size != 0) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001789
1790 /* Read the device tree table into buffer */
1791 if(flash_read(ptn, offset, (void *) dt_buf, page_size)) {
1792 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
1793 return -1;
1794 }
1795
1796 table = (struct dt_table*) dt_buf;
1797
Deepa Dinamani19648b42013-09-05 17:05:55 -07001798 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001799 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1800 return -1;
1801 }
1802
P.V. Phani Kumar82916762016-03-09 09:20:19 +05301803 /* Its Error if, dt_hdr_size (table->num_entries * dt_entry size + Dev_Tree Header)
1804 goes beyound hdr->dt_size*/
Parth Dixit4097b622016-03-15 14:42:27 +05301805 if (dt_hdr_size > ROUND_TO_PAGE(dt_size,hdr->page_size)) {
P.V. Phani Kumar82916762016-03-09 09:20:19 +05301806 dprintf(CRITICAL, "ERROR: Invalid Device Tree size \n");
1807 return -1;
1808 }
1809
Deepa Dinamani19648b42013-09-05 17:05:55 -07001810 table = (struct dt_table*) memalign(CACHE_LINE, dt_hdr_size);
1811 if (!table)
1812 return -1;
1813
1814 /* Read the entire device tree table into buffer */
1815 if(flash_read(ptn, offset, (void *)table, dt_hdr_size)) {
1816 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
1817 return -1;
1818 }
1819
1820
Joel Kingaa335dc2013-06-03 16:11:08 -07001821 /* Find index of device tree within device tree table */
1822 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001823 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1824 return -1;
1825 }
1826
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001827 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001828 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001829 {
1830 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1831 return -1;
1832 }
1833
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001834 /* Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001835 if(flash_read(ptn, offset + dt_entry.offset,
1836 (void *)hdr->tags_addr, dt_entry.size)) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001837 dprintf(CRITICAL, "ERROR: Cannot read device tree\n");
1838 return -1;
1839 }
1840 }
1841#endif
1842
Shashank Mittalcd98d472011-08-02 14:29:24 -07001843 }
David Ng183a7422009-12-07 14:55:21 -08001844continue_boot:
Dima Zavin214cc642009-01-26 11:16:21 -08001845
Dima Zavin214cc642009-01-26 11:16:21 -08001846 /* TODO: create/pass atags to kernel */
1847
Ajay Dudanie28a6072011-07-01 13:59:46 -07001848 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07001849 (const char *)hdr->cmdline, board_machtype(),
Dima Zavin214cc642009-01-26 11:16:21 -08001850 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
1851
1852 return 0;
1853}
Brian Swetland9c4c0752009-01-25 16:23:50 -08001854
Shashank Mittal162244e2011-08-08 19:01:25 -07001855void write_device_info_mmc(device_info *dev)
1856{
Shashank Mittal162244e2011-08-08 19:01:25 -07001857 unsigned long long ptn = 0;
1858 unsigned long long size;
1859 int index = INVALID_PTN;
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001860 uint32_t blocksize;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001861 uint8_t lun = 0;
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001862 uint32_t ret = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001863
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001864 if (devinfo_present)
1865 index = partition_get_index("devinfo");
1866 else
1867 index = partition_get_index("aboot");
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001868
Shashank Mittal162244e2011-08-08 19:01:25 -07001869 ptn = partition_get_offset(index);
1870 if(ptn == 0)
1871 {
1872 return;
1873 }
1874
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001875 lun = partition_get_lun(index);
1876 mmc_set_lun(lun);
1877
Shashank Mittal162244e2011-08-08 19:01:25 -07001878 size = partition_get_size(index);
1879
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001880 blocksize = mmc_get_device_blocksize();
1881
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001882 if (devinfo_present)
1883 ret = mmc_write(ptn, blocksize, (void *)info_buf);
1884 else
1885 ret = mmc_write((ptn + size - blocksize), blocksize, (void *)info_buf);
1886 if (ret)
Shashank Mittal162244e2011-08-08 19:01:25 -07001887 {
1888 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
Sridhar Parasurame1a97922015-07-27 15:31:22 -07001889 ASSERT(0);
Shashank Mittal162244e2011-08-08 19:01:25 -07001890 }
1891}
1892
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001893void read_device_info_mmc(struct device_info *info)
Shashank Mittal162244e2011-08-08 19:01:25 -07001894{
Shashank Mittal162244e2011-08-08 19:01:25 -07001895 unsigned long long ptn = 0;
1896 unsigned long long size;
1897 int index = INVALID_PTN;
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001898 uint32_t blocksize;
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001899 uint32_t ret = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001900
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001901 if ((index = partition_get_index("devinfo")) < 0)
1902 {
1903 devinfo_present = false;
1904 index = partition_get_index("aboot");
1905 }
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001906
Shashank Mittal162244e2011-08-08 19:01:25 -07001907 ptn = partition_get_offset(index);
1908 if(ptn == 0)
1909 {
1910 return;
1911 }
1912
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001913 mmc_set_lun(partition_get_lun(index));
1914
Shashank Mittal162244e2011-08-08 19:01:25 -07001915 size = partition_get_size(index);
1916
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001917 blocksize = mmc_get_device_blocksize();
1918
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001919 if (devinfo_present)
1920 ret = mmc_read(ptn, (void *)info_buf, blocksize);
1921 else
1922 ret = mmc_read((ptn + size - blocksize), (void *)info_buf, blocksize);
1923 if (ret)
Shashank Mittal162244e2011-08-08 19:01:25 -07001924 {
1925 dprintf(CRITICAL, "ERROR: Cannot read device info\n");
Sridhar Parasurame1a97922015-07-27 15:31:22 -07001926 ASSERT(0);
Shashank Mittal162244e2011-08-08 19:01:25 -07001927 }
Shashank Mittal162244e2011-08-08 19:01:25 -07001928}
1929
1930void write_device_info_flash(device_info *dev)
1931{
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001932 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
Shashank Mittal162244e2011-08-08 19:01:25 -07001933 struct ptentry *ptn;
1934 struct ptable *ptable;
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001935 if(info == NULL)
1936 {
1937 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
1938 ASSERT(0);
1939 }
1940 info_buf = info;
Shashank Mittal162244e2011-08-08 19:01:25 -07001941 ptable = flash_get_ptable();
1942 if (ptable == NULL)
1943 {
1944 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1945 return;
1946 }
1947
1948 ptn = ptable_find(ptable, "devinfo");
1949 if (ptn == NULL)
1950 {
Smita Ghosh670c8b82015-05-07 09:30:03 -07001951 dprintf(CRITICAL, "ERROR: No devinfo partition found\n");
Shashank Mittal162244e2011-08-08 19:01:25 -07001952 return;
1953 }
1954
1955 memcpy(info, dev, sizeof(device_info));
1956
1957 if (flash_write(ptn, 0, (void *)info_buf, page_size))
1958 {
1959 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1960 return;
1961 }
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001962 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07001963}
1964
vijay kumarc65876c2015-04-24 13:29:16 +05301965static int read_allow_oem_unlock(device_info *dev)
1966{
vijay kumarc65876c2015-04-24 13:29:16 +05301967 unsigned offset;
1968 int index;
1969 unsigned long long ptn;
1970 unsigned long long ptn_size;
1971 unsigned blocksize = mmc_get_device_blocksize();
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07001972 STACKBUF_DMA_ALIGN(buf, blocksize);
vijay kumarc65876c2015-04-24 13:29:16 +05301973
vijay kumarca2e6812015-07-08 20:28:25 +05301974 index = partition_get_index(frp_ptns[0]);
vijay kumarc65876c2015-04-24 13:29:16 +05301975 if (index == INVALID_PTN)
1976 {
vijay kumarca2e6812015-07-08 20:28:25 +05301977 index = partition_get_index(frp_ptns[1]);
1978 if (index == INVALID_PTN)
1979 {
1980 dprintf(CRITICAL, "Neither '%s' nor '%s' partition found\n", frp_ptns[0],frp_ptns[1]);
1981 return -1;
1982 }
vijay kumarc65876c2015-04-24 13:29:16 +05301983 }
1984
1985 ptn = partition_get_offset(index);
1986 ptn_size = partition_get_size(index);
1987 offset = ptn_size - blocksize;
1988
Mayank Grover48860402016-11-29 12:34:53 +05301989 /* Set Lun for partition */
1990 mmc_set_lun(partition_get_lun(index));
1991
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07001992 if (mmc_read(ptn + offset, (void *)buf, blocksize))
vijay kumarc65876c2015-04-24 13:29:16 +05301993 {
1994 dprintf(CRITICAL, "Reading MMC failed\n");
1995 return -1;
1996 }
1997
1998 /*is_allow_unlock is a bool value stored at the LSB of last byte*/
1999 is_allow_unlock = buf[blocksize-1] & 0x01;
2000 return 0;
2001}
2002
2003static int write_allow_oem_unlock(bool allow_unlock)
2004{
vijay kumarc65876c2015-04-24 13:29:16 +05302005 unsigned offset;
vijay kumarc65876c2015-04-24 13:29:16 +05302006 int index;
2007 unsigned long long ptn;
2008 unsigned long long ptn_size;
2009 unsigned blocksize = mmc_get_device_blocksize();
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07002010 STACKBUF_DMA_ALIGN(buf, blocksize);
vijay kumarc65876c2015-04-24 13:29:16 +05302011
vijay kumarca2e6812015-07-08 20:28:25 +05302012 index = partition_get_index(frp_ptns[0]);
vijay kumarc65876c2015-04-24 13:29:16 +05302013 if (index == INVALID_PTN)
2014 {
vijay kumarca2e6812015-07-08 20:28:25 +05302015 index = partition_get_index(frp_ptns[1]);
2016 if (index == INVALID_PTN)
2017 {
2018 dprintf(CRITICAL, "Neither '%s' nor '%s' partition found\n", frp_ptns[0],frp_ptns[1]);
2019 return -1;
2020 }
vijay kumarc65876c2015-04-24 13:29:16 +05302021 }
2022
2023 ptn = partition_get_offset(index);
2024 ptn_size = partition_get_size(index);
2025 offset = ptn_size - blocksize;
Mayank Grover48860402016-11-29 12:34:53 +05302026 mmc_set_lun(partition_get_lun(index));
vijay kumarc65876c2015-04-24 13:29:16 +05302027
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07002028 if (mmc_read(ptn + offset, (void *)buf, blocksize))
vijay kumarc65876c2015-04-24 13:29:16 +05302029 {
2030 dprintf(CRITICAL, "Reading MMC failed\n");
2031 return -1;
2032 }
2033
2034 /*is_allow_unlock is a bool value stored at the LSB of last byte*/
2035 buf[blocksize-1] = allow_unlock;
2036 if (mmc_write(ptn + offset, blocksize, buf))
2037 {
2038 dprintf(CRITICAL, "Writing MMC failed\n");
2039 return -1;
2040 }
2041
2042 return 0;
2043}
2044
Shashank Mittal162244e2011-08-08 19:01:25 -07002045void read_device_info_flash(device_info *dev)
2046{
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002047 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
Shashank Mittal162244e2011-08-08 19:01:25 -07002048 struct ptentry *ptn;
2049 struct ptable *ptable;
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002050 if(info == NULL)
2051 {
2052 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
2053 ASSERT(0);
2054 }
2055 info_buf = info;
Shashank Mittal162244e2011-08-08 19:01:25 -07002056 ptable = flash_get_ptable();
2057 if (ptable == NULL)
2058 {
2059 dprintf(CRITICAL, "ERROR: Partition table not found\n");
2060 return;
2061 }
2062
2063 ptn = ptable_find(ptable, "devinfo");
2064 if (ptn == NULL)
2065 {
Smita Ghosh670c8b82015-05-07 09:30:03 -07002066 dprintf(CRITICAL, "ERROR: No devinfo partition found\n");
Shashank Mittal162244e2011-08-08 19:01:25 -07002067 return;
2068 }
2069
2070 if (flash_read(ptn, 0, (void *)info_buf, page_size))
2071 {
2072 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
2073 return;
2074 }
2075
2076 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
2077 {
Shashank Mittal162244e2011-08-08 19:01:25 -07002078 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
2079 info->is_unlocked = 0;
Shashank Mittala0032282011-08-26 14:50:11 -07002080 info->is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07002081 write_device_info_flash(info);
2082 }
2083 memcpy(dev, info, sizeof(device_info));
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002084 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07002085}
2086
2087void write_device_info(device_info *dev)
2088{
2089 if(target_is_emmc_boot())
2090 {
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002091 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
2092 if(info == NULL)
2093 {
2094 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
2095 ASSERT(0);
2096 }
2097 info_buf = info;
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002098 memcpy(info, dev, sizeof(struct device_info));
2099
2100#if USE_RPMB_FOR_DEVINFO
Sridhar Parasurame1a97922015-07-27 15:31:22 -07002101 if (is_secure_boot_enable()) {
2102 if((write_device_info_rpmb((void*) info, PAGE_SIZE)) < 0)
2103 ASSERT(0);
2104 }
Channagoud Kadabic80cb492015-04-28 16:08:28 -07002105 else
2106 write_device_info_mmc(info);
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002107#else
2108 write_device_info_mmc(info);
2109#endif
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002110 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07002111 }
2112 else
2113 {
2114 write_device_info_flash(dev);
2115 }
2116}
2117
2118void read_device_info(device_info *dev)
2119{
2120 if(target_is_emmc_boot())
2121 {
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002122 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
2123 if(info == NULL)
2124 {
2125 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
2126 ASSERT(0);
2127 }
2128 info_buf = info;
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002129
2130#if USE_RPMB_FOR_DEVINFO
Sridhar Parasurame1a97922015-07-27 15:31:22 -07002131 if (is_secure_boot_enable()) {
2132 if((read_device_info_rpmb((void*) info, PAGE_SIZE)) < 0)
2133 ASSERT(0);
2134 }
Channagoud Kadabic80cb492015-04-28 16:08:28 -07002135 else
2136 read_device_info_mmc(info);
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002137#else
2138 read_device_info_mmc(info);
2139#endif
2140
2141 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
2142 {
2143 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
lijuang511a2b52015-08-14 20:50:51 +08002144 if (is_secure_boot_enable()) {
Channagoud Kadabi05f78ba2015-07-06 11:58:14 -07002145 info->is_unlocked = 0;
Parth Dixitddbc7352015-10-18 03:13:31 +05302146#if !VBOOT_MOTA
lijuang511a2b52015-08-14 20:50:51 +08002147 info->is_unlock_critical = 0;
Parth Dixitddbc7352015-10-18 03:13:31 +05302148#endif
lijuang511a2b52015-08-14 20:50:51 +08002149 } else {
Channagoud Kadabi2fda4092015-07-07 13:34:11 -07002150 info->is_unlocked = 1;
Parth Dixitddbc7352015-10-18 03:13:31 +05302151#if !VBOOT_MOTA
lijuang511a2b52015-08-14 20:50:51 +08002152 info->is_unlock_critical = 1;
Parth Dixitddbc7352015-10-18 03:13:31 +05302153#endif
lijuang511a2b52015-08-14 20:50:51 +08002154 }
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002155 info->is_tampered = 0;
2156 info->charger_screen_enabled = 0;
Parth Dixitddbc7352015-10-18 03:13:31 +05302157#if !VBOOT_MOTA
Sridhar Parasuram287e5062015-08-24 16:17:22 -07002158 info->verity_mode = 1; //enforcing by default
Parth Dixitddbc7352015-10-18 03:13:31 +05302159#endif
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002160 write_device_info(info);
2161 }
2162 memcpy(dev, info, sizeof(device_info));
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002163 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07002164 }
2165 else
2166 {
2167 read_device_info_flash(dev);
2168 }
2169}
2170
2171void reset_device_info()
2172{
2173 dprintf(ALWAYS, "reset_device_info called.");
Shashank Mittala0032282011-08-26 14:50:11 -07002174 device.is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07002175 write_device_info(&device);
2176}
2177
2178void set_device_root()
2179{
2180 dprintf(ALWAYS, "set_device_root called.");
Shashank Mittala0032282011-08-26 14:50:11 -07002181 device.is_tampered = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -07002182 write_device_info(&device);
2183}
2184
lijuang4ece1e72015-08-14 21:02:36 +08002185/* set device unlock value
2186 * Must check FRP before call this function
2187 * Need to wipe data when unlock status changed
2188 * type 0: oem unlock
2189 * type 1: unlock critical
2190 * status 0: unlock as false
2191 * status 1: lock as true
2192 */
2193void set_device_unlock_value(int type, bool status)
lijuang21f12f52015-08-22 16:22:19 +08002194{
lijuang4ece1e72015-08-14 21:02:36 +08002195 if (type == UNLOCK)
2196 device.is_unlocked = status;
Parth Dixitddbc7352015-10-18 03:13:31 +05302197#if !VBOOT_MOTA
lijuang4ece1e72015-08-14 21:02:36 +08002198 else if (type == UNLOCK_CRITICAL)
2199 device.is_unlock_critical = status;
Parth Dixitddbc7352015-10-18 03:13:31 +05302200#endif
lijuang4ece1e72015-08-14 21:02:36 +08002201 write_device_info(&device);
2202}
2203
2204static void set_device_unlock(int type, bool status)
2205{
2206 int is_unlocked = -1;
2207 char response[MAX_RSP_SIZE];
2208
2209 /* check device unlock status if it is as expected */
2210 if (type == UNLOCK)
2211 is_unlocked = device.is_unlocked;
Parth Dixitddbc7352015-10-18 03:13:31 +05302212#if !VBOOT_MOTA
lijuang4ece1e72015-08-14 21:02:36 +08002213 else if (type == UNLOCK_CRITICAL)
2214 is_unlocked = device.is_unlock_critical;
Parth Dixitddbc7352015-10-18 03:13:31 +05302215#endif
lijuang4ece1e72015-08-14 21:02:36 +08002216 if (is_unlocked == status) {
2217 snprintf(response, sizeof(response), "\tDevice already : %s", (status ? "unlocked!" : "locked!"));
2218 fastboot_info(response);
2219 fastboot_okay("");
2220 return;
2221 }
2222
2223 /* status is true, it means to unlock device */
2224 if (status) {
lijuang21f12f52015-08-22 16:22:19 +08002225 if(!is_allow_unlock) {
2226 fastboot_fail("oem unlock is not allowed");
2227 return;
2228 }
2229
lijuang4ece1e72015-08-14 21:02:36 +08002230#if FBCON_DISPLAY_MSG
lijuang9a7d3b92015-11-30 14:41:24 +08002231 display_unlock_menu(type);
lijuang4ece1e72015-08-14 21:02:36 +08002232 fastboot_okay("");
2233 return;
2234#else
2235 if (type == UNLOCK) {
2236 fastboot_fail("Need wipe userdata. Do 'fastboot oem unlock-go'");
2237 return;
2238 }
2239#endif
lijuang21f12f52015-08-22 16:22:19 +08002240 }
lijuang4ece1e72015-08-14 21:02:36 +08002241
2242 set_device_unlock_value(type, status);
2243
2244 /* wipe data */
2245 struct recovery_message msg;
2246
2247 snprintf(msg.recovery, sizeof(msg.recovery), "recovery\n--wipe_data");
2248 write_misc(0, &msg, sizeof(msg));
2249
2250 fastboot_okay("");
2251 reboot_device(RECOVERY_MODE);
lijuang21f12f52015-08-22 16:22:19 +08002252}
2253
lijuang511a2b52015-08-14 20:50:51 +08002254static bool critical_flash_allowed(const char * entry)
2255{
2256 uint32_t i = 0;
2257 if (entry == NULL)
2258 return false;
2259
2260 for (i = 0; i < ARRAY_SIZE(critical_flash_allowed_ptn); i++) {
2261 if(!strcmp(entry, critical_flash_allowed_ptn[i]))
2262 return true;
2263 }
2264 return false;
Matthew Qin271927e2015-03-31 22:07:07 -04002265}
2266
2267#if DEVICE_TREE
Amol Jadicb524072012-08-09 16:40:18 -07002268int copy_dtb(uint8_t *boot_image_start, unsigned int scratch_offset)
2269{
2270 uint32 dt_image_offset = 0;
Amol Jadicb524072012-08-09 16:40:18 -07002271 uint32_t n;
Amol Jadicb524072012-08-09 16:40:18 -07002272 struct dt_table *table;
2273 struct dt_entry dt_entry;
2274 uint32_t dt_hdr_size;
2275 unsigned int compressed_size = 0;
2276 unsigned int dtb_size = 0;
2277 unsigned int out_avai_len = 0;
2278 unsigned char *out_addr = NULL;
2279 unsigned char *best_match_dt_addr = NULL;
2280 int rc;
2281
2282 struct boot_img_hdr *hdr = (struct boot_img_hdr *) (boot_image_start);
2283
Parth Dixit4097b622016-03-15 14:42:27 +05302284#ifndef OSVERSION_IN_BOOTIMAGE
2285 dt_size = hdr->dt_size;
2286#endif
2287
2288 if(dt_size != 0) {
Amol Jadicb524072012-08-09 16:40:18 -07002289 /* add kernel offset */
2290 dt_image_offset += page_size;
2291 n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
2292 dt_image_offset += n;
2293
2294 /* add ramdisk offset */
2295 n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
2296 dt_image_offset += n;
2297
2298 /* add second offset */
2299 if(hdr->second_size != 0) {
2300 n = ROUND_TO_PAGE(hdr->second_size, page_mask);
2301 dt_image_offset += n;
2302 }
2303
2304 /* offset now point to start of dt.img */
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07002305 table = (struct dt_table*)(boot_image_start + dt_image_offset);
Amol Jadicb524072012-08-09 16:40:18 -07002306
Deepa Dinamani19648b42013-09-05 17:05:55 -07002307 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Amol Jadicb524072012-08-09 16:40:18 -07002308 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
2309 return -1;
2310 }
P.V. Phani Kumar82916762016-03-09 09:20:19 +05302311
2312 /* Its Error if, dt_hdr_size (table->num_entries * dt_entry size + Dev_Tree Header)
2313 goes beyound hdr->dt_size*/
Parth Dixit4097b622016-03-15 14:42:27 +05302314 if (dt_hdr_size > ROUND_TO_PAGE(dt_size,hdr->page_size)) {
P.V. Phani Kumar82916762016-03-09 09:20:19 +05302315 dprintf(CRITICAL, "ERROR: Invalid Device Tree size \n");
2316 return -1;
2317 }
2318
Joel Kingaa335dc2013-06-03 16:11:08 -07002319 /* Find index of device tree within device tree table */
2320 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Amol Jadicb524072012-08-09 16:40:18 -07002321 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
2322 return -1;
2323 }
2324
Matthew Qin271927e2015-03-31 22:07:07 -04002325 best_match_dt_addr = (unsigned char *)boot_image_start + dt_image_offset + dt_entry.offset;
2326 if (is_gzip_package(best_match_dt_addr, dt_entry.size))
2327 {
2328 out_addr = (unsigned char *)target_get_scratch_address() + scratch_offset;
2329 out_avai_len = target_get_max_flash_size() - scratch_offset;
Matthew Qin0b15b322015-05-19 05:20:54 -04002330 dprintf(INFO, "decompressing dtb: start\n");
Matthew Qin271927e2015-03-31 22:07:07 -04002331 rc = decompress(best_match_dt_addr,
2332 dt_entry.size, out_addr, out_avai_len,
2333 &compressed_size, &dtb_size);
2334 if (rc)
2335 {
Matthew Qin0b15b322015-05-19 05:20:54 -04002336 dprintf(CRITICAL, "decompressing dtb failed!!!\n");
Matthew Qin271927e2015-03-31 22:07:07 -04002337 ASSERT(0);
2338 }
2339
Matthew Qin0b15b322015-05-19 05:20:54 -04002340 dprintf(INFO, "decompressing dtb: done\n");
Matthew Qin271927e2015-03-31 22:07:07 -04002341 best_match_dt_addr = out_addr;
2342 } else {
2343 dtb_size = dt_entry.size;
2344 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002345 /* Validate and Read device device tree in the "tags_add */
Matthew Qin271927e2015-03-31 22:07:07 -04002346 if (check_aboot_addr_range_overlap(hdr->tags_addr, dtb_size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002347 {
2348 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
2349 return -1;
2350 }
2351
Amol Jadicb524072012-08-09 16:40:18 -07002352 /* Read device device tree in the "tags_add */
Matthew Qin271927e2015-03-31 22:07:07 -04002353 memmove((void*) hdr->tags_addr, (void *)best_match_dt_addr, dtb_size);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002354 } else
2355 return -1;
Amol Jadicb524072012-08-09 16:40:18 -07002356
2357 /* Everything looks fine. Return success. */
2358 return 0;
2359}
2360#endif
2361
Brian Swetland9c4c0752009-01-25 16:23:50 -08002362void cmd_boot(const char *arg, void *data, unsigned sz)
2363{
Amit Blay8a510302015-08-17 09:20:01 +03002364#ifdef MDTP_SUPPORT
2365 static bool is_mdtp_activated = 0;
2366#endif /* MDTP_SUPPORT */
Brian Swetland9c4c0752009-01-25 16:23:50 -08002367 unsigned kernel_actual;
2368 unsigned ramdisk_actual;
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002369 uint32_t image_actual;
2370 uint32_t dt_actual = 0;
P.V. Phani Kumareecfd822016-03-09 20:09:03 +05302371 uint32_t sig_actual = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08002372 struct boot_img_hdr *hdr = NULL;
2373 struct kernel64_hdr *kptr = NULL;
Brian Swetland9c4c0752009-01-25 16:23:50 -08002374 char *ptr = ((char*) data);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002375 int ret = 0;
2376 uint8_t dtb_copied = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08002377 unsigned int out_len = 0;
2378 unsigned int out_avai_len = 0;
2379 unsigned char *out_addr = NULL;
2380 uint32_t dtb_offset = 0;
2381 unsigned char *kernel_start_addr = NULL;
2382 unsigned int kernel_size = 0;
Matthew Qin271927e2015-03-31 22:07:07 -04002383 unsigned int scratch_offset = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08002384
lijuang2008ff22016-03-07 17:56:27 +08002385#if FBCON_DISPLAY_MSG
2386 /* Exit keys' detection thread firstly */
2387 exit_menu_keys_detection();
2388#endif
2389
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002390#if VERIFIED_BOOT
Channagoud Kadabi6d5375e2015-06-23 17:15:42 -07002391 if(target_build_variant_user() && !device.is_unlocked)
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002392 {
2393 fastboot_fail("unlock device to use this command");
lijuang2008ff22016-03-07 17:56:27 +08002394 goto boot_failed;
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002395 }
2396#endif
2397
Brian Swetland9c4c0752009-01-25 16:23:50 -08002398 if (sz < sizeof(hdr)) {
2399 fastboot_fail("invalid bootimage header");
lijuang2008ff22016-03-07 17:56:27 +08002400 goto boot_failed;
Brian Swetland9c4c0752009-01-25 16:23:50 -08002401 }
2402
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002403 hdr = (struct boot_img_hdr *)data;
Brian Swetland9c4c0752009-01-25 16:23:50 -08002404
2405 /* ensure commandline is terminated */
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002406 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -08002407
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002408 if(target_is_emmc_boot() && hdr->page_size) {
2409 page_size = hdr->page_size;
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07002410 page_mask = page_size - 1;
2411 }
2412
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002413 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
2414 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002415#if DEVICE_TREE
Parth Dixit4097b622016-03-15 14:42:27 +05302416#ifndef OSVERSION_IN_BOOTIMAGE
2417 dt_size = hdr->dt_size;
2418#endif
2419 dt_actual = ROUND_TO_PAGE(dt_size, page_mask);
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002420#endif
2421
2422 image_actual = ADD_OF(page_size, kernel_actual);
2423 image_actual = ADD_OF(image_actual, ramdisk_actual);
2424 image_actual = ADD_OF(image_actual, dt_actual);
2425
P.V. Phani Kumareecfd822016-03-09 20:09:03 +05302426 if (target_use_signed_kernel() && (!device.is_unlocked)) {
2427 /* Calculate the signature length from boot image */
2428 sig_actual = read_der_message_length(
2429 (unsigned char*)(data + image_actual),sz);
Channagoud Kadabi3d839012014-06-26 14:26:39 -07002430 image_actual = ADD_OF(image_actual, sig_actual);
P.V. Phani Kumareecfd822016-03-09 20:09:03 +05302431 }
Channagoud Kadabi3d839012014-06-26 14:26:39 -07002432
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002433 /* sz should have atleast raw boot image */
2434 if (image_actual > sz) {
Channagoud Kadabi3d839012014-06-26 14:26:39 -07002435 fastboot_fail("bootimage: incomplete or not signed");
lijuang2008ff22016-03-07 17:56:27 +08002436 goto boot_failed;
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002437 }
2438
Sridhar Parasuram361e4cd2015-09-24 11:19:19 -07002439 // Initialize boot state before trying to verify boot.img
2440#if VERIFIED_BOOT
Channagoud Kadabi699466e2015-11-03 12:37:42 -08002441 boot_verifier_init();
Mayank Groverb337e932017-01-18 20:00:40 +05302442#endif
Channagoud Kadabibf6ce7d2015-09-17 13:25:35 -07002443 /* Handle overflow if the input image size is greater than
2444 * boot image buffer can hold
2445 */
2446 if ((target_get_max_flash_size() - (image_actual - sig_actual)) < page_size)
2447 {
2448 fastboot_fail("booimage: size is greater than boot image buffer can hold");
lijuang2008ff22016-03-07 17:56:27 +08002449 goto boot_failed;
Channagoud Kadabibf6ce7d2015-09-17 13:25:35 -07002450 }
Sridhar Parasuram361e4cd2015-09-24 11:19:19 -07002451
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002452 /* Verify the boot image
2453 * device & page_size are initialized in aboot_init
2454 */
Sridhar Parasuram7ae9fbc2015-09-17 09:56:30 -07002455 if (target_use_signed_kernel() && (!device.is_unlocked)) {
Channagoud Kadabi3d839012014-06-26 14:26:39 -07002456 /* Pass size excluding signature size, otherwise we would try to
2457 * access signature beyond its length
2458 */
2459 verify_signed_bootimg((uint32_t)data, (image_actual - sig_actual));
Sridhar Parasuram7ae9fbc2015-09-17 09:56:30 -07002460 }
Amit Blay4aa292f2015-04-28 21:55:59 +03002461#ifdef MDTP_SUPPORT
2462 else
2463 {
Shay Nachmani062aa3f2015-05-17 17:28:44 +03002464 /* fastboot boot is not allowed when MDTP is activated */
Amit Blay4aa292f2015-04-28 21:55:59 +03002465 mdtp_ext_partition_verification_t ext_partition;
Amit Blay8a510302015-08-17 09:20:01 +03002466
2467 if (!is_mdtp_activated) {
2468 ext_partition.partition = MDTP_PARTITION_NONE;
2469 mdtp_fwlock_verify_lock(&ext_partition);
2470 }
Amit Blay4aa292f2015-04-28 21:55:59 +03002471 }
Shay Nachmani062aa3f2015-05-17 17:28:44 +03002472
Amir Kotzer7c768c02016-04-13 09:08:05 +03002473 /* If mdtp state cannot be validate, block fastboot boot*/
2474 if(mdtp_activated(&is_mdtp_activated)){
2475 dprintf(CRITICAL, "mdtp_activated cannot validate state.\n");
2476 dprintf(CRITICAL, "Can not proceed with fastboot boot command.\n");
2477 goto boot_failed;
2478 }
Shay Nachmani062aa3f2015-05-17 17:28:44 +03002479 if(is_mdtp_activated){
2480 dprintf(CRITICAL, "fastboot boot command is not available.\n");
lijuang2008ff22016-03-07 17:56:27 +08002481 goto boot_failed;
Shay Nachmani062aa3f2015-05-17 17:28:44 +03002482 }
Amit Blay4aa292f2015-04-28 21:55:59 +03002483#endif /* MDTP_SUPPORT */
2484
Sridhar Parasuram361e4cd2015-09-24 11:19:19 -07002485#if VERIFIED_BOOT
Parth Dixitbc9b6492015-10-18 00:41:38 +05302486#if !VBOOT_MOTA
Sridhar Parasuram361e4cd2015-09-24 11:19:19 -07002487 // send root of trust
2488 if(!send_rot_command((uint32_t)device.is_unlocked))
2489 ASSERT(0);
2490#endif
Parth Dixitbc9b6492015-10-18 00:41:38 +05302491#endif
Channagoud Kadabia22144f2013-03-20 11:49:01 -07002492 /*
Matthew Qinbb7923d2015-02-09 10:56:09 +08002493 * Check if the kernel image is a gzip package. If yes, need to decompress it.
2494 * If not, continue booting.
2495 */
2496 if (is_gzip_package((unsigned char *)(data + page_size), hdr->kernel_size))
2497 {
2498 out_addr = (unsigned char *)target_get_scratch_address();
2499 out_addr = (unsigned char *)(out_addr + image_actual + page_size);
2500 out_avai_len = target_get_max_flash_size() - image_actual - page_size;
Matthew Qin0b15b322015-05-19 05:20:54 -04002501 dprintf(INFO, "decompressing kernel image: start\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08002502 ret = decompress((unsigned char *)(ptr + page_size),
2503 hdr->kernel_size, out_addr, out_avai_len,
2504 &dtb_offset, &out_len);
2505 if (ret)
2506 {
Matthew Qin0b15b322015-05-19 05:20:54 -04002507 dprintf(CRITICAL, "decompressing image failed!!!\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08002508 ASSERT(0);
2509 }
2510
Matthew Qin0b15b322015-05-19 05:20:54 -04002511 dprintf(INFO, "decompressing kernel image: done\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08002512 kptr = (struct kernel64_hdr *)out_addr;
2513 kernel_start_addr = out_addr;
2514 kernel_size = out_len;
2515 } else {
2516 kptr = (struct kernel64_hdr*)((char *)data + page_size);
2517 kernel_start_addr = (unsigned char *)((char *)data + page_size);
2518 kernel_size = hdr->kernel_size;
2519 }
2520
2521 /*
Channagoud Kadabia22144f2013-03-20 11:49:01 -07002522 * Update the kernel/ramdisk/tags address if the boot image header
2523 * has default values, these default values come from mkbootimg when
2524 * the boot image is flashed using fastboot flash:raw
2525 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08002526 update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr));
Dima Zavin3cadfff2013-03-21 14:30:48 -07002527
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002528 /* Get virtual addresses since the hdr saves physical addresses. */
2529 hdr->kernel_addr = VA(hdr->kernel_addr);
2530 hdr->ramdisk_addr = VA(hdr->ramdisk_addr);
2531 hdr->tags_addr = VA(hdr->tags_addr);
Brian Swetland9c4c0752009-01-25 16:23:50 -08002532
Matthew Qinbb7923d2015-02-09 10:56:09 +08002533 kernel_size = ROUND_TO_PAGE(kernel_size, page_mask);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002534 /* Check if the addresses in the header are valid. */
Matthew Qinbb7923d2015-02-09 10:56:09 +08002535 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_size) ||
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002536 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
2537 {
2538 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
lijuang2008ff22016-03-07 17:56:27 +08002539 goto boot_failed;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002540 }
2541
Amol Jadicb524072012-08-09 16:40:18 -07002542#if DEVICE_TREE
Matthew Qin271927e2015-03-31 22:07:07 -04002543 scratch_offset = image_actual + page_size + out_len;
Amol Jadicb524072012-08-09 16:40:18 -07002544 /* find correct dtb and copy it to right location */
Matthew Qin271927e2015-03-31 22:07:07 -04002545 ret = copy_dtb(data, scratch_offset);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002546
2547 dtb_copied = !ret ? 1 : 0;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002548#else
2549 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
2550 {
2551 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
lijuang2008ff22016-03-07 17:56:27 +08002552 goto boot_failed;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002553 }
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002554#endif
2555
2556 /* Load ramdisk & kernel */
2557 memmove((void*) hdr->ramdisk_addr, ptr + page_size + kernel_actual, hdr->ramdisk_size);
Matthew Qinbb7923d2015-02-09 10:56:09 +08002558 memmove((void*) hdr->kernel_addr, (char*) (kernel_start_addr), kernel_size);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002559
2560#if DEVICE_TREE
Matthew Qinbb7923d2015-02-09 10:56:09 +08002561 if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual))
2562 {
2563 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
lijuang2008ff22016-03-07 17:56:27 +08002564 goto boot_failed;
Matthew Qinbb7923d2015-02-09 10:56:09 +08002565 }
2566
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002567 /*
2568 * If dtb is not found look for appended DTB in the kernel.
2569 * If appended dev tree is found, update the atags with
2570 * memory address to the DTB appended location on RAM.
2571 * Else update with the atags address in the kernel header
2572 */
2573 if (!dtb_copied) {
2574 void *dtb;
Matthew Qinbb7923d2015-02-09 10:56:09 +08002575 dtb = dev_tree_appended((void*)(ptr + page_size),
2576 hdr->kernel_size, dtb_offset,
Dima Zavine63e5572013-05-03 12:23:06 -07002577 (void *)hdr->tags_addr);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002578 if (!dtb) {
2579 fastboot_fail("dtb not found");
lijuang2008ff22016-03-07 17:56:27 +08002580 goto boot_failed;
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002581 }
Amol Jadicb524072012-08-09 16:40:18 -07002582 }
2583#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08002584
2585 fastboot_okay("");
Amol Jadi7c4316c2013-10-07 14:19:26 -07002586 fastboot_stop();
Brian Swetland9c4c0752009-01-25 16:23:50 -08002587
Dima Zavin77e41f32013-03-06 16:10:43 -08002588 boot_linux((void*) hdr->kernel_addr, (void*) hdr->tags_addr,
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002589 (const char*) hdr->cmdline, board_machtype(),
2590 (void*) hdr->ramdisk_addr, hdr->ramdisk_size);
lijuang2008ff22016-03-07 17:56:27 +08002591
2592 /* fastboot already stop, it's no need to show fastboot menu */
2593 return;
2594boot_failed:
2595#if FBCON_DISPLAY_MSG
2596 /* revert to fastboot menu if boot failed */
2597 display_fastboot_menu();
2598#endif
2599 return;
Brian Swetland9c4c0752009-01-25 16:23:50 -08002600}
2601
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002602void cmd_erase_nand(const char *arg, void *data, unsigned sz)
Dima Zavin214cc642009-01-26 11:16:21 -08002603{
2604 struct ptentry *ptn;
2605 struct ptable *ptable;
2606
2607 ptable = flash_get_ptable();
2608 if (ptable == NULL) {
2609 fastboot_fail("partition table doesn't exist");
2610 return;
2611 }
2612
2613 ptn = ptable_find(ptable, arg);
2614 if (ptn == NULL) {
2615 fastboot_fail("unknown partition name");
2616 return;
2617 }
2618
2619 if (flash_erase(ptn)) {
2620 fastboot_fail("failed to erase partition");
2621 return;
2622 }
2623 fastboot_okay("");
2624}
2625
Bikas Gurungd48bd242010-09-04 19:54:32 -07002626
2627void cmd_erase_mmc(const char *arg, void *data, unsigned sz)
2628{
2629 unsigned long long ptn = 0;
Oliver Wangcee448d2013-10-22 18:40:13 +08002630 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07002631 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002632 uint8_t lun = 0;
Vijay Kumar Pendoti0b21f462016-05-02 17:09:18 +05302633 char *footer = NULL;
Bikas Gurungd48bd242010-09-04 19:54:32 -07002634
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002635#if VERIFIED_BOOT
2636 if(!strcmp(arg, KEYSTORE_PTN_NAME))
2637 {
2638 if(!device.is_unlocked)
2639 {
2640 fastboot_fail("unlock device to erase keystore");
2641 return;
2642 }
2643 }
2644#endif
2645
Kinson Chikf1a43512011-07-14 11:28:39 -07002646 index = partition_get_index(arg);
2647 ptn = partition_get_offset(index);
Oliver Wangcee448d2013-10-22 18:40:13 +08002648 size = partition_get_size(index);
Neeti Desaica8c9602011-10-06 11:40:00 -07002649
Kinson Chikf1a43512011-07-14 11:28:39 -07002650 if(ptn == 0) {
Neeti Desaica8c9602011-10-06 11:40:00 -07002651 fastboot_fail("Partition table doesn't exist\n");
Bikas Gurungd48bd242010-09-04 19:54:32 -07002652 return;
2653 }
Kun Liang2f1601a2013-08-12 16:29:54 +08002654
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002655 lun = partition_get_lun(index);
2656 mmc_set_lun(lun);
2657
Sridhar Parasuramd7957122015-02-27 11:33:40 -08002658 if (platform_boot_dev_isemmc())
2659 {
2660 if (mmc_erase_card(ptn, size)) {
2661 fastboot_fail("failed to erase partition\n");
2662 return;
2663 }
2664 } else {
2665 BUF_DMA_ALIGN(out, DEFAULT_ERASE_SIZE);
2666 size = partition_get_size(index);
2667 if (size > DEFAULT_ERASE_SIZE)
2668 size = DEFAULT_ERASE_SIZE;
Kun Liang2f1601a2013-08-12 16:29:54 +08002669
Sridhar Parasuramd7957122015-02-27 11:33:40 -08002670 /* Simple inefficient version of erase. Just writing
2671 0 in first several blocks */
2672 if (mmc_write(ptn , size, (unsigned int *)out)) {
2673 fastboot_fail("failed to erase partition");
2674 return;
2675 }
Vijay Kumar Pendoti0b21f462016-05-02 17:09:18 +05302676 /*Erase FDE metadata at the userdata footer*/
2677 if(!(strncmp(arg, "userdata", 8)))
2678 {
2679 footer = memalign(CACHE_LINE, FOOTER_SIZE);
2680 memset((void *)footer, 0, FOOTER_SIZE);
2681
2682 size = partition_get_size(index);
2683
2684 if (mmc_write((ptn + size) - FOOTER_SIZE , FOOTER_SIZE, (unsigned int *)footer)) {
2685 fastboot_fail("failed to erase userdata footer");
2686 free(footer);
2687 return;
2688 }
2689 free(footer);
2690 }
Bikas Gurungd48bd242010-09-04 19:54:32 -07002691 }
Sridhar Parasuram32b30662015-07-10 13:33:22 -07002692#if VERIFIED_BOOT
Parth Dixitbc9b6492015-10-18 00:41:38 +05302693#if !VBOOT_MOTA
Sridhar Parasuram32b30662015-07-10 13:33:22 -07002694 if(!(strncmp(arg, "userdata", 8)))
2695 if(send_delete_keys_to_tz())
2696 ASSERT(0);
2697#endif
Parth Dixitbc9b6492015-10-18 00:41:38 +05302698#endif
Bikas Gurungd48bd242010-09-04 19:54:32 -07002699 fastboot_okay("");
2700}
2701
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002702void cmd_erase(const char *arg, void *data, unsigned sz)
2703{
Channagoud Kadabice3cf422015-04-17 16:02:30 -07002704#if VERIFIED_BOOT
Channagoud Kadabi35297672015-06-13 11:09:49 -07002705 if (target_build_variant_user())
Channagoud Kadabice3cf422015-04-17 16:02:30 -07002706 {
Sridhar Parasuramc32d07d2015-07-12 10:57:48 -07002707 if(!device.is_unlocked)
Channagoud Kadabice3cf422015-04-17 16:02:30 -07002708 {
Channagoud Kadabi35297672015-06-13 11:09:49 -07002709 fastboot_fail("device is locked. Cannot erase");
Channagoud Kadabice3cf422015-04-17 16:02:30 -07002710 return;
2711 }
2712 }
2713#endif
2714
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002715 if(target_is_emmc_boot())
2716 cmd_erase_mmc(arg, data, sz);
2717 else
2718 cmd_erase_nand(arg, data, sz);
2719}
Bikas Gurungd48bd242010-09-04 19:54:32 -07002720
Channagoud Kadabiad259832015-05-29 11:14:17 -07002721static uint32_t aboot_get_secret_key()
2722{
2723 /* 0 is invalid secret key, update this implementation to return
2724 * device specific unique secret key
2725 */
2726 return 0;
2727}
2728
Ajay Dudani5c761132011-04-07 20:19:04 -07002729void cmd_flash_mmc_img(const char *arg, void *data, unsigned sz)
Shashank Mittal23b8f422010-04-16 19:27:21 -07002730{
2731 unsigned long long ptn = 0;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07002732 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07002733 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002734 char *token = NULL;
2735 char *pname = NULL;
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07002736 char *sp;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002737 uint8_t lun = 0;
2738 bool lun_set = false;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07002739
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07002740 token = strtok_r((char *)arg, ":", &sp);
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002741 pname = token;
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07002742 token = strtok_r(NULL, ":", &sp);
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002743 if(token)
2744 {
2745 lun = atoi(token);
2746 mmc_set_lun(lun);
2747 lun_set = true;
2748 }
2749
Mao Jinlong226f33a2014-07-04 17:24:10 +08002750 if (pname)
Greg Grisco6e754772011-06-23 12:19:39 -07002751 {
Channagoud Kadabiad259832015-05-29 11:14:17 -07002752 if (!strncmp(pname, "frp-unlock", strlen("frp-unlock")))
2753 {
2754 if (!aboot_frp_unlock(pname, data, sz))
2755 {
2756 fastboot_info("FRP unlock successful");
2757 fastboot_okay("");
2758 }
2759 else
2760 fastboot_fail("Secret key is invalid, please update the bootloader with secret key");
2761
2762 return;
2763 }
2764
Mao Jinlong226f33a2014-07-04 17:24:10 +08002765 if (!strcmp(pname, "partition"))
2766 {
2767 dprintf(INFO, "Attempt to write partition image.\n");
2768 if (write_partition(sz, (unsigned char *) data)) {
2769 fastboot_fail("failed to write partition");
Greg Grisco6e754772011-06-23 12:19:39 -07002770 return;
2771 }
2772 }
Mao Jinlong226f33a2014-07-04 17:24:10 +08002773 else
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002774 {
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002775#if VERIFIED_BOOT
2776 if(!strcmp(pname, KEYSTORE_PTN_NAME))
2777 {
2778 if(!device.is_unlocked)
2779 {
2780 fastboot_fail("unlock device to flash keystore");
2781 return;
2782 }
Gaurav Nebhwanic7313cc2015-12-15 22:25:04 +05302783 if(!boot_verify_validate_keystore((unsigned char *)data,sz))
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002784 {
2785 fastboot_fail("image is not a keystore file");
2786 return;
2787 }
2788 }
2789#endif
Mao Jinlong226f33a2014-07-04 17:24:10 +08002790 index = partition_get_index(pname);
2791 ptn = partition_get_offset(index);
2792 if(ptn == 0) {
2793 fastboot_fail("partition table doesn't exist");
2794 return;
2795 }
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002796
Mao Jinlong226f33a2014-07-04 17:24:10 +08002797 if (!strcmp(pname, "boot") || !strcmp(pname, "recovery")) {
2798 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
2799 fastboot_fail("image is not a boot image");
2800 return;
2801 }
2802 }
2803
2804 if(!lun_set)
2805 {
2806 lun = partition_get_lun(index);
2807 mmc_set_lun(lun);
2808 }
2809
2810 size = partition_get_size(index);
Gaurav Nebhwani4d2389c2016-03-17 21:10:05 +05302811 if (ROUND_TO_PAGE(sz, mmc_blocksize_mask) > size) {
Mao Jinlong226f33a2014-07-04 17:24:10 +08002812 fastboot_fail("size too large");
2813 return;
2814 }
2815 else if (mmc_write(ptn , sz, (unsigned int *)data)) {
2816 fastboot_fail("flash write failure");
2817 return;
2818 }
Greg Grisco6e754772011-06-23 12:19:39 -07002819 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07002820 }
2821 fastboot_okay("");
2822 return;
2823}
2824
Ajay Dudanide984792015-03-02 09:57:41 -08002825void cmd_flash_meta_img(const char *arg, void *data, unsigned sz)
2826{
2827 int i, images;
2828 meta_header_t *meta_header;
2829 img_header_entry_t *img_header_entry;
Parth Dixit3e2d3032016-03-04 17:11:52 +05302830 /*End of the image address*/
2831 uintptr_t data_end;
2832
2833 if( (UINT_MAX - sz) > (uintptr_t)data )
2834 data_end = (uintptr_t)data + sz;
2835 else
2836 {
2837 fastboot_fail("Cannot flash: image header corrupt");
2838 return;
2839 }
2840
2841 if( data_end < ((uintptr_t)data + sizeof(meta_header_t)))
2842 {
2843 fastboot_fail("Cannot flash: image header corrupt");
2844 return;
2845 }
Ajay Dudanide984792015-03-02 09:57:41 -08002846
lijuang8ee21882016-04-19 16:57:11 +08002847 /* If device is locked:
2848 * Forbid to flash image to avoid the device to bypass the image
2849 * which with "any" name other than bootloader. Because it maybe
2850 * a meta package of all partitions.
2851 */
2852#if VERIFIED_BOOT
2853 if (target_build_variant_user()) {
2854 if (!device.is_unlocked) {
2855 fastboot_fail("Device is locked, meta image flashing is not allowed");
2856 return;
2857 }
2858#if !VBOOT_MOTA
2859 if(!device.is_unlock_critical) {
2860 fastboot_fail("Device is critical locked, Meta image flashing is not allowed");
2861 return;
2862 }
2863#endif
2864 }
2865#endif
2866
Ajay Dudanide984792015-03-02 09:57:41 -08002867 meta_header = (meta_header_t*) data;
Parth Dixit3e2d3032016-03-04 17:11:52 +05302868 if( data_end < ((uintptr_t)data + meta_header->img_hdr_sz))
2869 {
2870 fastboot_fail("Cannot flash: image header corrupt");
2871 return;
2872 }
Ajay Dudanide984792015-03-02 09:57:41 -08002873 img_header_entry = (img_header_entry_t*) (data+sizeof(meta_header_t));
2874
2875 images = meta_header->img_hdr_sz / sizeof(img_header_entry_t);
2876
2877 for (i=0; i<images; i++) {
2878
2879 if((img_header_entry[i].ptn_name == NULL) ||
2880 (img_header_entry[i].start_offset == 0) ||
2881 (img_header_entry[i].size == 0))
2882 break;
Kishor PK49831502017-04-21 17:55:43 +05302883 if ((UINT_MAX - img_header_entry[i].start_offset) < (uintptr_t)data) {
2884 fastboot_fail("Integer overflow detected in start_offset of img");
2885 break;
2886 }
2887 else if ((UINT_MAX - (img_header_entry[i].start_offset + (uintptr_t)data)) < img_header_entry[i].size) {
2888 fastboot_fail("Integer overflow detected in size of img");
2889 break;
2890 }
Parth Dixit3e2d3032016-03-04 17:11:52 +05302891 if( data_end < ((uintptr_t)data + img_header_entry[i].start_offset
2892 + img_header_entry[i].size) )
2893 {
2894 fastboot_fail("Cannot flash: image size mismatch");
2895 break;
2896 }
2897
Ajay Dudanide984792015-03-02 09:57:41 -08002898 cmd_flash_mmc_img(img_header_entry[i].ptn_name,
2899 (void *) data + img_header_entry[i].start_offset,
2900 img_header_entry[i].size);
2901 }
2902
Channagoud Kadabiaafbbfc2015-03-04 17:52:56 -08002903 if (!strncmp(arg, "bootloader", strlen("bootloader")))
2904 {
2905 strlcpy(device.bootloader_version, TARGET(BOARD), MAX_VERSION_LEN);
2906 strlcat(device.bootloader_version, "-", MAX_VERSION_LEN);
2907 strlcat(device.bootloader_version, meta_header->img_version, MAX_VERSION_LEN);
2908 }
2909 else
2910 {
2911 strlcpy(device.radio_version, TARGET(BOARD), MAX_VERSION_LEN);
2912 strlcat(device.radio_version, "-", MAX_VERSION_LEN);
2913 strlcat(device.radio_version, meta_header->img_version, MAX_VERSION_LEN);
2914 }
2915
2916 write_device_info(&device);
Ajay Dudanide984792015-03-02 09:57:41 -08002917 fastboot_okay("");
2918 return;
2919}
2920
Ajay Dudani5c761132011-04-07 20:19:04 -07002921void cmd_flash_mmc_sparse_img(const char *arg, void *data, unsigned sz)
2922{
2923 unsigned int chunk;
wufeng.jiang813dc352015-06-02 23:02:46 -04002924 uint64_t chunk_data_sz;
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002925 uint32_t *fill_buf = NULL;
2926 uint32_t fill_val;
Ajay Dudani5c761132011-04-07 20:19:04 -07002927 sparse_header_t *sparse_header;
2928 chunk_header_t *chunk_header;
Ajay Dudaniab18f022011-05-12 14:39:22 -07002929 uint32_t total_blocks = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07002930 unsigned long long ptn = 0;
Channagoud Kadabi65b91002011-10-11 17:34:33 +05302931 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07002932 int index = INVALID_PTN;
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05302933 uint32_t i;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002934 uint8_t lun = 0;
vijay kumar800255e2015-04-24 20:26:19 +05302935 /*End of the sparse image address*/
Parth Dixit64b1a482016-03-07 16:31:26 +05302936 uintptr_t data_end = (uintptr_t)data + sz;
Ajay Dudani5c761132011-04-07 20:19:04 -07002937
Kinson Chikf1a43512011-07-14 11:28:39 -07002938 index = partition_get_index(arg);
2939 ptn = partition_get_offset(index);
2940 if(ptn == 0) {
Ajay Dudani5c761132011-04-07 20:19:04 -07002941 fastboot_fail("partition table doesn't exist");
2942 return;
2943 }
2944
Channagoud Kadabi65b91002011-10-11 17:34:33 +05302945 size = partition_get_size(index);
Channagoud Kadabi65b91002011-10-11 17:34:33 +05302946
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002947 lun = partition_get_lun(index);
2948 mmc_set_lun(lun);
2949
vijay kumar800255e2015-04-24 20:26:19 +05302950 if (sz < sizeof(sparse_header_t)) {
2951 fastboot_fail("size too low");
2952 return;
2953 }
2954
Ajay Dudani5c761132011-04-07 20:19:04 -07002955 /* Read and skip over sparse image header */
2956 sparse_header = (sparse_header_t *) data;
vijay kumar800255e2015-04-24 20:26:19 +05302957
vijay kumar1321f342015-03-27 12:13:42 +05302958 if (((uint64_t)sparse_header->total_blks * (uint64_t)sparse_header->blk_sz) > size) {
Ajay Dudani876b3282012-12-21 14:12:17 -08002959 fastboot_fail("size too large");
2960 return;
2961 }
2962
vijay kumarde4fcf62015-04-23 13:05:49 +05302963 data += sizeof(sparse_header_t);
vijay kumar800255e2015-04-24 20:26:19 +05302964
Parth Dixit64b1a482016-03-07 16:31:26 +05302965 if (data_end < (uintptr_t)data) {
vijay kumar800255e2015-04-24 20:26:19 +05302966 fastboot_fail("buffer overreads occured due to invalid sparse header");
2967 return;
2968 }
2969
vijay kumarde4fcf62015-04-23 13:05:49 +05302970 if(sparse_header->file_hdr_sz != sizeof(sparse_header_t))
Ajay Dudani5c761132011-04-07 20:19:04 -07002971 {
vijay kumarde4fcf62015-04-23 13:05:49 +05302972 fastboot_fail("sparse header size mismatch");
2973 return;
Ajay Dudani5c761132011-04-07 20:19:04 -07002974 }
2975
Ajay Dudanib06c05f2011-05-12 14:46:10 -07002976 dprintf (SPEW, "=== Sparse Image Header ===\n");
2977 dprintf (SPEW, "magic: 0x%x\n", sparse_header->magic);
2978 dprintf (SPEW, "major_version: 0x%x\n", sparse_header->major_version);
2979 dprintf (SPEW, "minor_version: 0x%x\n", sparse_header->minor_version);
2980 dprintf (SPEW, "file_hdr_sz: %d\n", sparse_header->file_hdr_sz);
2981 dprintf (SPEW, "chunk_hdr_sz: %d\n", sparse_header->chunk_hdr_sz);
2982 dprintf (SPEW, "blk_sz: %d\n", sparse_header->blk_sz);
2983 dprintf (SPEW, "total_blks: %d\n", sparse_header->total_blks);
2984 dprintf (SPEW, "total_chunks: %d\n", sparse_header->total_chunks);
Ajay Dudani5c761132011-04-07 20:19:04 -07002985
2986 /* Start processing chunks */
2987 for (chunk=0; chunk<sparse_header->total_chunks; chunk++)
2988 {
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302989 /* Make sure the total image size does not exceed the partition size */
2990 if(((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz) >= size) {
2991 fastboot_fail("size too large");
2992 return;
2993 }
Ajay Dudani5c761132011-04-07 20:19:04 -07002994 /* Read and skip over chunk header */
2995 chunk_header = (chunk_header_t *) data;
2996 data += sizeof(chunk_header_t);
2997
Parth Dixit64b1a482016-03-07 16:31:26 +05302998 if (data_end < (uintptr_t)data) {
vijay kumar800255e2015-04-24 20:26:19 +05302999 fastboot_fail("buffer overreads occured due to invalid sparse header");
3000 return;
3001 }
3002
Ajay Dudani5c761132011-04-07 20:19:04 -07003003 dprintf (SPEW, "=== Chunk Header ===\n");
3004 dprintf (SPEW, "chunk_type: 0x%x\n", chunk_header->chunk_type);
3005 dprintf (SPEW, "chunk_data_sz: 0x%x\n", chunk_header->chunk_sz);
3006 dprintf (SPEW, "total_size: 0x%x\n", chunk_header->total_sz);
3007
vijay kumar800255e2015-04-24 20:26:19 +05303008 if(sparse_header->chunk_hdr_sz != sizeof(chunk_header_t))
Ajay Dudani5c761132011-04-07 20:19:04 -07003009 {
vijay kumar800255e2015-04-24 20:26:19 +05303010 fastboot_fail("chunk header size mismatch");
3011 return;
Ajay Dudani5c761132011-04-07 20:19:04 -07003012 }
3013
wufeng.jiang813dc352015-06-02 23:02:46 -04003014 if (!sparse_header->blk_sz ){
3015 fastboot_fail("Invalid block size\n");
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05303016 return;
3017 }
3018
wufeng.jiang813dc352015-06-02 23:02:46 -04003019 chunk_data_sz = (uint64_t)sparse_header->blk_sz * chunk_header->chunk_sz;
3020
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05303021 /* Make sure that the chunk size calculated from sparse image does not
3022 * exceed partition size
3023 */
3024 if ((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz + chunk_data_sz > size)
3025 {
3026 fastboot_fail("Chunk data size exceeds partition size");
3027 return;
3028 }
3029
Ajay Dudani5c761132011-04-07 20:19:04 -07003030 switch (chunk_header->chunk_type)
3031 {
3032 case CHUNK_TYPE_RAW:
wufeng.jiang813dc352015-06-02 23:02:46 -04003033 if((uint64_t)chunk_header->total_sz != ((uint64_t)sparse_header->chunk_hdr_sz +
Ajay Dudani5c761132011-04-07 20:19:04 -07003034 chunk_data_sz))
3035 {
3036 fastboot_fail("Bogus chunk size for chunk type Raw");
3037 return;
3038 }
3039
Parth Dixit64b1a482016-03-07 16:31:26 +05303040 if (data_end < (uintptr_t)data + chunk_data_sz) {
vijay kumar800255e2015-04-24 20:26:19 +05303041 fastboot_fail("buffer overreads occured due to invalid sparse header");
3042 return;
3043 }
3044
wufeng.jiang813dc352015-06-02 23:02:46 -04003045 /* chunk_header->total_sz is uint32,So chunk_data_sz is now less than 2^32
3046 otherwise it will return in the line above
3047 */
Ajay Dudaniab18f022011-05-12 14:39:22 -07003048 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
wufeng.jiang813dc352015-06-02 23:02:46 -04003049 (uint32_t)chunk_data_sz,
Ajay Dudaniab18f022011-05-12 14:39:22 -07003050 (unsigned int*)data))
Ajay Dudani5c761132011-04-07 20:19:04 -07003051 {
3052 fastboot_fail("flash write failure");
3053 return;
3054 }
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05303055 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
3056 fastboot_fail("Bogus size for RAW chunk type");
3057 return;
3058 }
Ajay Dudani5c761132011-04-07 20:19:04 -07003059 total_blocks += chunk_header->chunk_sz;
wufeng.jiang813dc352015-06-02 23:02:46 -04003060 data += (uint32_t)chunk_data_sz;
Ajay Dudani5c761132011-04-07 20:19:04 -07003061 break;
3062
Channagoud Kadabif627cf82013-09-09 14:08:20 -07003063 case CHUNK_TYPE_FILL:
3064 if(chunk_header->total_sz != (sparse_header->chunk_hdr_sz +
3065 sizeof(uint32_t)))
3066 {
3067 fastboot_fail("Bogus chunk size for chunk type FILL");
3068 return;
3069 }
3070
3071 fill_buf = (uint32_t *)memalign(CACHE_LINE, ROUNDUP(sparse_header->blk_sz, CACHE_LINE));
3072 if (!fill_buf)
3073 {
3074 fastboot_fail("Malloc failed for: CHUNK_TYPE_FILL");
3075 return;
3076 }
3077
Parth Dixit64b1a482016-03-07 16:31:26 +05303078 if (data_end < (uintptr_t)data + sizeof(uint32_t)) {
vijay kumar800255e2015-04-24 20:26:19 +05303079 fastboot_fail("buffer overreads occured due to invalid sparse header");
Gaurav Nebhwani163cbf82016-03-17 21:21:27 +05303080 free(fill_buf);
vijay kumar800255e2015-04-24 20:26:19 +05303081 return;
3082 }
Channagoud Kadabif627cf82013-09-09 14:08:20 -07003083 fill_val = *(uint32_t *)data;
3084 data = (char *) data + sizeof(uint32_t);
Channagoud Kadabif627cf82013-09-09 14:08:20 -07003085
3086 for (i = 0; i < (sparse_header->blk_sz / sizeof(fill_val)); i++)
3087 {
3088 fill_buf[i] = fill_val;
3089 }
3090
Gaurav Nebhwanie94cbe12016-03-17 21:16:34 +05303091 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz))
3092 {
3093 fastboot_fail("bogus size for chunk FILL type");
3094 free(fill_buf);
3095 return;
3096 }
3097
wufeng.jiang813dc352015-06-02 23:02:46 -04003098 for (i = 0; i < chunk_header->chunk_sz; i++)
Channagoud Kadabif627cf82013-09-09 14:08:20 -07003099 {
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05303100 /* Make sure that the data written to partition does not exceed partition size */
3101 if ((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz + sparse_header->blk_sz > size)
3102 {
3103 fastboot_fail("Chunk data size for fill type exceeds partition size");
Gaurav Nebhwani163cbf82016-03-17 21:21:27 +05303104 free(fill_buf);
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05303105 return;
3106 }
3107
Channagoud Kadabif627cf82013-09-09 14:08:20 -07003108 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
3109 sparse_header->blk_sz,
3110 fill_buf))
3111 {
3112 fastboot_fail("flash write failure");
3113 free(fill_buf);
3114 return;
3115 }
3116
3117 total_blocks++;
3118 }
3119
3120 free(fill_buf);
3121 break;
3122
Ajay Dudani5c761132011-04-07 20:19:04 -07003123 case CHUNK_TYPE_DONT_CARE:
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05303124 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
3125 fastboot_fail("bogus size for chunk DONT CARE type");
3126 return;
3127 }
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07003128 total_blocks += chunk_header->chunk_sz;
3129 break;
3130
Ajay Dudani5c761132011-04-07 20:19:04 -07003131 case CHUNK_TYPE_CRC:
3132 if(chunk_header->total_sz != sparse_header->chunk_hdr_sz)
3133 {
wufeng.jiang813dc352015-06-02 23:02:46 -04003134 fastboot_fail("Bogus chunk size for chunk type CRC");
Ajay Dudani5c761132011-04-07 20:19:04 -07003135 return;
3136 }
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05303137 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
3138 fastboot_fail("bogus size for chunk CRC type");
3139 return;
3140 }
Ajay Dudani5c761132011-04-07 20:19:04 -07003141 total_blocks += chunk_header->chunk_sz;
Parth Dixit64b1a482016-03-07 16:31:26 +05303142 if ((uintptr_t)data > UINT_MAX - chunk_data_sz) {
vijay kumar800255e2015-04-24 20:26:19 +05303143 fastboot_fail("integer overflow occured");
3144 return;
3145 }
wufeng.jiang813dc352015-06-02 23:02:46 -04003146 data += (uint32_t)chunk_data_sz;
Parth Dixit64b1a482016-03-07 16:31:26 +05303147 if (data_end < (uintptr_t)data) {
vijay kumar800255e2015-04-24 20:26:19 +05303148 fastboot_fail("buffer overreads occured due to invalid sparse header");
3149 return;
3150 }
Ajay Dudani5c761132011-04-07 20:19:04 -07003151 break;
3152
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07003153 default:
Channagoud Kadabif627cf82013-09-09 14:08:20 -07003154 dprintf(CRITICAL, "Unkown chunk type: %x\n",chunk_header->chunk_type);
Ajay Dudani5c761132011-04-07 20:19:04 -07003155 fastboot_fail("Unknown chunk type");
3156 return;
3157 }
3158 }
3159
Ajay Dudani0c6927b2011-05-18 11:12:16 -07003160 dprintf(INFO, "Wrote %d blocks, expected to write %d blocks\n",
3161 total_blocks, sparse_header->total_blks);
3162
3163 if(total_blocks != sparse_header->total_blks)
3164 {
3165 fastboot_fail("sparse image write failure");
3166 }
Ajay Dudani5c761132011-04-07 20:19:04 -07003167
3168 fastboot_okay("");
3169 return;
3170}
3171
3172void cmd_flash_mmc(const char *arg, void *data, unsigned sz)
3173{
3174 sparse_header_t *sparse_header;
Ajay Dudanide984792015-03-02 09:57:41 -08003175 meta_header_t *meta_header;
Ajay Dudani5c761132011-04-07 20:19:04 -07003176
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07003177#ifdef SSD_ENABLE
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -08003178 /* 8 Byte Magic + 2048 Byte xml + Encrypted Data */
3179 unsigned int *magic_number = (unsigned int *) data;
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08003180 int ret=0;
3181 uint32 major_version=0;
3182 uint32 minor_version=0;
3183
3184 ret = scm_svc_version(&major_version,&minor_version);
3185 if(!ret)
3186 {
3187 if(major_version >= 2)
3188 {
Amir Samuelovbb65ce02013-05-05 12:20:18 +03003189 if( !strcmp(arg, "ssd") || !strcmp(arg, "tqs") )
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08003190 {
3191 ret = encrypt_scm((uint32 **) &data, &sz);
3192 if (ret != 0) {
3193 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
3194 return;
3195 }
3196
Amir Samuelovbb65ce02013-05-05 12:20:18 +03003197 /* Protect only for SSD */
3198 if (!strcmp(arg, "ssd")) {
3199 ret = scm_protect_keystore((uint32 *) data, sz);
3200 if (ret != 0) {
3201 dprintf(CRITICAL, "ERROR: scm_protect_keystore Failed\n");
3202 return;
3203 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08003204 }
3205 }
3206 else
3207 {
3208 ret = decrypt_scm_v2((uint32 **) &data, &sz);
3209 if(ret != 0)
3210 {
3211 dprintf(CRITICAL,"ERROR: Decryption Failure\n");
3212 return;
3213 }
3214 }
3215 }
3216 else
3217 {
3218 if (magic_number[0] == DECRYPT_MAGIC_0 &&
3219 magic_number[1] == DECRYPT_MAGIC_1)
3220 {
3221 ret = decrypt_scm((uint32 **) &data, &sz);
3222 if (ret != 0) {
3223 dprintf(CRITICAL, "ERROR: Invalid secure image\n");
3224 return;
3225 }
3226 }
3227 else if (magic_number[0] == ENCRYPT_MAGIC_0 &&
3228 magic_number[1] == ENCRYPT_MAGIC_1)
3229 {
3230 ret = encrypt_scm((uint32 **) &data, &sz);
3231 if (ret != 0) {
3232 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
3233 return;
3234 }
3235 }
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07003236 }
3237 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08003238 else
Neeti Desai127b9e02012-03-20 16:11:23 -07003239 {
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08003240 dprintf(CRITICAL,"INVALID SVC Version\n");
3241 return;
Neeti Desai127b9e02012-03-20 16:11:23 -07003242 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08003243#endif /* SSD_ENABLE */
Neeti Desai127b9e02012-03-20 16:11:23 -07003244
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003245#if VERIFIED_BOOT
Channagoud Kadabi35297672015-06-13 11:09:49 -07003246 if (target_build_variant_user())
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003247 {
lijuang511a2b52015-08-14 20:50:51 +08003248 /* if device is locked:
3249 * common partition will not allow to be flashed
3250 * critical partition will allow to flash image.
3251 */
3252 if(!device.is_unlocked && !critical_flash_allowed(arg)) {
3253 fastboot_fail("Partition flashing is not allowed");
3254 return;
3255 }
Parth Dixitddbc7352015-10-18 03:13:31 +05303256#if !VBOOT_MOTA
lijuang511a2b52015-08-14 20:50:51 +08003257 /* if device critical is locked:
3258 * common partition will allow to be flashed
3259 * critical partition will not allow to flash image.
3260 */
3261 if(!device.is_unlock_critical && critical_flash_allowed(arg)) {
3262 fastboot_fail("Critical partition flashing is not allowed");
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003263 return;
3264 }
Parth Dixitddbc7352015-10-18 03:13:31 +05303265#endif
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003266 }
3267#endif
3268
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07003269 sparse_header = (sparse_header_t *) data;
Ajay Dudanide984792015-03-02 09:57:41 -08003270 meta_header = (meta_header_t *) data;
3271 if (sparse_header->magic == SPARSE_HEADER_MAGIC)
Ajay Dudani5c761132011-04-07 20:19:04 -07003272 cmd_flash_mmc_sparse_img(arg, data, sz);
Ajay Dudanide984792015-03-02 09:57:41 -08003273 else if (meta_header->magic == META_HEADER_MAGIC)
3274 cmd_flash_meta_img(arg, data, sz);
3275 else
3276 cmd_flash_mmc_img(arg, data, sz);
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07003277
3278#if VERIFIED_BOOT
Parth Dixitddbc7352015-10-18 03:13:31 +05303279#if !VBOOT_MOTA
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07003280 if((!strncmp(arg, "system", 6)) && !device.verity_mode)
3281 {
3282 // reset dm_verity mode to enforcing
3283 device.verity_mode = 1;
3284 write_device_info(&device);
3285 }
3286#endif
Parth Dixitddbc7352015-10-18 03:13:31 +05303287#endif
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07003288
Ajay Dudani5c761132011-04-07 20:19:04 -07003289 return;
3290}
3291
Tanya Brokhman84eaaf62015-02-24 21:57:06 +02003292void cmd_updatevol(const char *vol_name, void *data, unsigned sz)
3293{
3294 struct ptentry *sys_ptn;
3295 struct ptable *ptable;
3296
3297 ptable = flash_get_ptable();
3298 if (ptable == NULL) {
3299 fastboot_fail("partition table doesn't exist");
3300 return;
3301 }
3302
3303 sys_ptn = ptable_find(ptable, "system");
3304 if (sys_ptn == NULL) {
3305 fastboot_fail("system partition not found");
3306 return;
3307 }
3308
3309 sz = ROUND_TO_PAGE(sz, page_mask);
3310 if (update_ubi_vol(sys_ptn, vol_name, data, sz))
3311 fastboot_fail("update_ubi_vol failed");
3312 else
3313 fastboot_okay("");
3314}
3315
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003316void cmd_flash_nand(const char *arg, void *data, unsigned sz)
Dima Zavin214cc642009-01-26 11:16:21 -08003317{
3318 struct ptentry *ptn;
3319 struct ptable *ptable;
3320 unsigned extra = 0;
Gaurav Nebhwanid9730712016-05-06 14:28:29 +05303321 uint64_t partition_size = 0;
Dima Zavin214cc642009-01-26 11:16:21 -08003322
3323 ptable = flash_get_ptable();
3324 if (ptable == NULL) {
3325 fastboot_fail("partition table doesn't exist");
3326 return;
3327 }
3328
3329 ptn = ptable_find(ptable, arg);
3330 if (ptn == NULL) {
Tanya Brokhman84eaaf62015-02-24 21:57:06 +02003331 dprintf(INFO, "unknown partition name (%s). Trying updatevol\n",
3332 arg);
3333 cmd_updatevol(arg, data, sz);
Dima Zavin214cc642009-01-26 11:16:21 -08003334 return;
3335 }
3336
3337 if (!strcmp(ptn->name, "boot") || !strcmp(ptn->name, "recovery")) {
3338 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
3339 fastboot_fail("image is not a boot image");
3340 return;
3341 }
3342 }
3343
Amol Jadi5c61a952012-05-04 17:05:35 -07003344 if (!strcmp(ptn->name, "system")
Deepa Dinamani13e32c42012-03-12 14:34:17 -07003345 || !strcmp(ptn->name, "userdata")
3346 || !strcmp(ptn->name, "persist")
Sundarajan Srinivasanb063a852013-11-19 14:02:27 -08003347 || !strcmp(ptn->name, "recoveryfs")
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -08003348 || !strcmp(ptn->name, "modem"))
Tanya Brokhman1c94f1a2015-02-15 09:05:03 +02003349 extra = 1;
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -08003350 else
Shashank Mittaldcc2e352009-11-19 19:11:16 -08003351 sz = ROUND_TO_PAGE(sz, page_mask);
Dima Zavin214cc642009-01-26 11:16:21 -08003352
Gaurav Nebhwanid9730712016-05-06 14:28:29 +05303353 partition_size = (uint64_t)ptn->length * (uint64_t)flash_num_pages_per_blk() * (uint64_t)flash_page_size();
3354 if (partition_size > UINT_MAX) {
3355 fastboot_fail("Invalid partition size");
3356 return;
3357 }
3358
3359 if (sz > partition_size) {
3360 fastboot_fail("Image size too large");
3361 return;
3362 }
3363
Dima Zavin214cc642009-01-26 11:16:21 -08003364 dprintf(INFO, "writing %d bytes to '%s'\n", sz, ptn->name);
Tanya Brokhman1c94f1a2015-02-15 09:05:03 +02003365 if (!memcmp((void *)data, UBI_MAGIC, UBI_MAGIC_SIZE)) {
3366 if (flash_ubi_img(ptn, data, sz)) {
3367 fastboot_fail("flash write failure");
3368 return;
3369 }
3370 } else {
3371 if (flash_write(ptn, extra, data, sz)) {
3372 fastboot_fail("flash write failure");
3373 return;
3374 }
Dima Zavin214cc642009-01-26 11:16:21 -08003375 }
3376 dprintf(INFO, "partition '%s' updated\n", ptn->name);
3377 fastboot_okay("");
3378}
3379
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003380void cmd_flash(const char *arg, void *data, unsigned sz)
3381{
3382 if(target_is_emmc_boot())
3383 cmd_flash_mmc(arg, data, sz);
3384 else
3385 cmd_flash_nand(arg, data, sz);
3386}
3387
Dima Zavin214cc642009-01-26 11:16:21 -08003388void cmd_continue(const char *arg, void *data, unsigned sz)
3389{
3390 fastboot_okay("");
Amol Jadi7c4316c2013-10-07 14:19:26 -07003391 fastboot_stop();
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003392
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003393 if (target_is_emmc_boot())
3394 {
lijuanga40d6302015-07-20 20:10:13 +08003395#if FBCON_DISPLAY_MSG
lijuangde34d502016-02-26 16:04:50 +08003396 /* Exit keys' detection thread firstly */
3397 exit_menu_keys_detection();
lijuanga40d6302015-07-20 20:10:13 +08003398#endif
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003399 boot_linux_from_mmc();
3400 }
3401 else
3402 {
3403 boot_linux_from_flash();
3404 }
Dima Zavin214cc642009-01-26 11:16:21 -08003405}
3406
Chandan Uddaraju94183c02010-01-15 15:13:59 -08003407void cmd_reboot(const char *arg, void *data, unsigned sz)
3408{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003409 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08003410 fastboot_okay("");
3411 reboot_device(0);
3412}
3413
3414void cmd_reboot_bootloader(const char *arg, void *data, unsigned sz)
3415{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003416 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08003417 fastboot_okay("");
3418 reboot_device(FASTBOOT_MODE);
3419}
3420
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003421void cmd_oem_enable_charger_screen(const char *arg, void *data, unsigned size)
3422{
3423 dprintf(INFO, "Enabling charger screen check\n");
3424 device.charger_screen_enabled = 1;
3425 write_device_info(&device);
3426 fastboot_okay("");
3427}
3428
3429void cmd_oem_disable_charger_screen(const char *arg, void *data, unsigned size)
3430{
3431 dprintf(INFO, "Disabling charger screen check\n");
3432 device.charger_screen_enabled = 0;
3433 write_device_info(&device);
3434 fastboot_okay("");
3435}
3436
lijuanga25d8bb2015-09-16 13:11:52 +08003437void cmd_oem_off_mode_charger(const char *arg, void *data, unsigned size)
3438{
3439 char *p = NULL;
3440 const char *delim = " \t\n\r";
Parth Dixit407f15b2016-01-07 14:47:37 +05303441 char *sp;
lijuanga25d8bb2015-09-16 13:11:52 +08003442
3443 if (arg) {
Parth Dixit407f15b2016-01-07 14:47:37 +05303444 p = strtok_r((char *)arg, delim, &sp);
lijuanga25d8bb2015-09-16 13:11:52 +08003445 if (p) {
3446 if (!strncmp(p, "0", 1)) {
3447 device.charger_screen_enabled = 0;
3448 } else if (!strncmp(p, "1", 1)) {
3449 device.charger_screen_enabled = 1;
3450 }
3451 }
3452 }
3453
3454 /* update charger_screen_enabled value for getvar
3455 * command
3456 */
3457 snprintf(charger_screen_enabled, MAX_RSP_SIZE, "%d",
3458 device.charger_screen_enabled);
3459
3460 write_device_info(&device);
3461 fastboot_okay("");
3462}
3463
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05303464void cmd_oem_select_display_panel(const char *arg, void *data, unsigned size)
3465{
3466 dprintf(INFO, "Selecting display panel %s\n", arg);
3467 if (arg)
3468 strlcpy(device.display_panel, arg,
3469 sizeof(device.display_panel));
3470 write_device_info(&device);
3471 fastboot_okay("");
3472}
3473
Shashank Mittal162244e2011-08-08 19:01:25 -07003474void cmd_oem_unlock(const char *arg, void *data, unsigned sz)
3475{
lijuang4ece1e72015-08-14 21:02:36 +08003476 set_device_unlock(UNLOCK, TRUE);
vijay kumarc65876c2015-04-24 13:29:16 +05303477}
3478
3479void cmd_oem_unlock_go(const char *arg, void *data, unsigned sz)
3480{
lijuang4ece1e72015-08-14 21:02:36 +08003481 if(!device.is_unlocked) {
vijay kumarc65876c2015-04-24 13:29:16 +05303482 if(!is_allow_unlock) {
3483 fastboot_fail("oem unlock is not allowed");
3484 return;
3485 }
3486
lijuang4ece1e72015-08-14 21:02:36 +08003487 set_device_unlock_value(UNLOCK, TRUE);
vijay kumarc65876c2015-04-24 13:29:16 +05303488
lijuang4ece1e72015-08-14 21:02:36 +08003489 /* wipe data */
vijay kumarc65876c2015-04-24 13:29:16 +05303490 struct recovery_message msg;
lijuang4ece1e72015-08-14 21:02:36 +08003491
vijay kumarc65876c2015-04-24 13:29:16 +05303492 snprintf(msg.recovery, sizeof(msg.recovery), "recovery\n--wipe_data");
3493 write_misc(0, &msg, sizeof(msg));
3494
3495 fastboot_okay("");
3496 reboot_device(RECOVERY_MODE);
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003497 }
3498 fastboot_okay("");
3499}
3500
Channagoud Kadabiad259832015-05-29 11:14:17 -07003501static int aboot_frp_unlock(char *pname, void *data, unsigned sz)
3502{
3503 int ret = 1;
3504 uint32_t secret_key;
3505 char seckey_buffer[MAX_RSP_SIZE];
3506
3507 secret_key = aboot_get_secret_key();
3508 if (secret_key)
3509 {
3510 snprintf((char *) seckey_buffer, MAX_RSP_SIZE, "%x", secret_key);
3511 if (!memcmp((void *)data, (void *)seckey_buffer, sz))
3512 {
3513 is_allow_unlock = true;
3514 write_allow_oem_unlock(is_allow_unlock);
3515 ret = 0;
3516 }
3517 }
3518 return ret;
3519}
3520
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003521void cmd_oem_lock(const char *arg, void *data, unsigned sz)
3522{
lijuang4ece1e72015-08-14 21:02:36 +08003523 set_device_unlock(UNLOCK, FALSE);
Shashank Mittal162244e2011-08-08 19:01:25 -07003524}
3525
Shashank Mittala0032282011-08-26 14:50:11 -07003526void cmd_oem_devinfo(const char *arg, void *data, unsigned sz)
3527{
lijuang511a2b52015-08-14 20:50:51 +08003528 char response[MAX_RSP_SIZE];
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003529 snprintf(response, sizeof(response), "\tDevice tampered: %s", (device.is_tampered ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07003530 fastboot_info(response);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003531 snprintf(response, sizeof(response), "\tDevice unlocked: %s", (device.is_unlocked ? "true" : "false"));
3532 fastboot_info(response);
Parth Dixitddbc7352015-10-18 03:13:31 +05303533#if !VBOOT_MOTA
lijuang511a2b52015-08-14 20:50:51 +08003534 snprintf(response, sizeof(response), "\tDevice critical unlocked: %s", (device.is_unlock_critical ? "true" : "false"));
3535 fastboot_info(response);
Parth Dixitddbc7352015-10-18 03:13:31 +05303536#endif
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003537 snprintf(response, sizeof(response), "\tCharger screen enabled: %s", (device.charger_screen_enabled ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07003538 fastboot_info(response);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05303539 snprintf(response, sizeof(response), "\tDisplay panel: %s", (device.display_panel));
3540 fastboot_info(response);
Shashank Mittala0032282011-08-26 14:50:11 -07003541 fastboot_okay("");
3542}
3543
lijuang511a2b52015-08-14 20:50:51 +08003544void cmd_flashing_get_unlock_ability(const char *arg, void *data, unsigned sz)
3545{
3546 char response[MAX_RSP_SIZE];
3547 snprintf(response, sizeof(response), "\tget_unlock_ability: %d", is_allow_unlock);
3548 fastboot_info(response);
3549 fastboot_okay("");
3550}
3551
3552void cmd_flashing_lock_critical(const char *arg, void *data, unsigned sz)
3553{
lijuang4ece1e72015-08-14 21:02:36 +08003554 set_device_unlock(UNLOCK_CRITICAL, FALSE);
lijuang511a2b52015-08-14 20:50:51 +08003555}
3556
3557void cmd_flashing_unlock_critical(const char *arg, void *data, unsigned sz)
3558{
lijuang4ece1e72015-08-14 21:02:36 +08003559 set_device_unlock(UNLOCK_CRITICAL, TRUE);
lijuang511a2b52015-08-14 20:50:51 +08003560}
3561
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07003562void cmd_preflash(const char *arg, void *data, unsigned sz)
3563{
3564 fastboot_okay("");
3565}
3566
Mao Flynn7b379f32015-04-20 00:28:30 +08003567static uint8_t logo_header[LOGO_IMG_HEADER_SIZE];
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303568
Mao Flynn7b379f32015-04-20 00:28:30 +08003569int splash_screen_check_header(logo_img_header *header)
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303570{
Mao Flynn7b379f32015-04-20 00:28:30 +08003571 if (memcmp(header->magic, LOGO_IMG_MAGIC, 8))
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303572 return -1;
Mao Flynn7b379f32015-04-20 00:28:30 +08003573 if (header->width == 0 || header->height == 0)
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303574 return -1;
3575 return 0;
3576}
3577
Mao Flynn7b379f32015-04-20 00:28:30 +08003578int splash_screen_flash()
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003579{
3580 struct ptentry *ptn;
3581 struct ptable *ptable;
Mao Flynn7b379f32015-04-20 00:28:30 +08003582 struct logo_img_header *header;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003583 struct fbcon_config *fb_display = NULL;
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08003584
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303585 ptable = flash_get_ptable();
3586 if (ptable == NULL) {
Mao Flynn7b379f32015-04-20 00:28:30 +08003587 dprintf(CRITICAL, "ERROR: Partition table not found\n");
3588 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303589 }
Mao Flynn7b379f32015-04-20 00:28:30 +08003590
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303591 ptn = ptable_find(ptable, "splash");
3592 if (ptn == NULL) {
3593 dprintf(CRITICAL, "ERROR: splash Partition not found\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003594 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303595 }
Mao Flynn7b379f32015-04-20 00:28:30 +08003596 if (flash_read(ptn, 0, (void *)logo_header, LOGO_IMG_HEADER_SIZE)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303597 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003598 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303599 }
3600
Mao Flynn7b379f32015-04-20 00:28:30 +08003601 header = (struct logo_img_header *)logo_header;
3602 if (splash_screen_check_header(header)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303603 dprintf(CRITICAL, "ERROR: Boot image header invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003604 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303605 }
3606
3607 fb_display = fbcon_display();
3608 if (fb_display) {
Mao Flynn7b379f32015-04-20 00:28:30 +08003609 if (header->type && (header->blocks != 0)) { // RLE24 compressed data
3610 uint8_t *base = (uint8_t *) fb_display->base + LOGO_IMG_OFFSET;
3611
3612 /* if the logo is full-screen size, remove "fbcon_clear()" */
3613 if ((header->width != fb_display->width)
3614 || (header->height != fb_display->height))
3615 fbcon_clear();
3616
3617 if (flash_read(ptn + LOGO_IMG_HEADER_SIZE, 0,
3618 (uint32_t *)base,
3619 (header->blocks * 512))) {
3620 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
3621 return -1;
3622 }
3623 fbcon_extract_to_screen(header, base);
3624 return 0;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003625 }
Mao Flynn7b379f32015-04-20 00:28:30 +08003626
Veera Sundaram Sankaranc33416f2015-09-28 10:25:55 -07003627 if ((header->width > fb_display->width) || (header->height > fb_display->height)) {
3628 dprintf(CRITICAL, "Logo config greater than fb config. Fall back default logo\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003629 return -1;
3630 }
3631
Vineet Bajaj99291ed2014-09-09 12:29:46 +05303632 uint8_t *base = (uint8_t *) fb_display->base;
Mao Flynn7b379f32015-04-20 00:28:30 +08003633 if (flash_read(ptn + LOGO_IMG_HEADER_SIZE, 0,
3634 (uint32_t *)base,
3635 ((((header->width * header->height * fb_display->bpp/8) + 511) >> 9) << 9))) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303636 fbcon_clear();
Vineet Bajaj99291ed2014-09-09 12:29:46 +05303637 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003638 return -1;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003639 }
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303640 }
3641
Mao Flynn7b379f32015-04-20 00:28:30 +08003642 return 0;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303643}
3644
Mao Flynn7b379f32015-04-20 00:28:30 +08003645int splash_screen_mmc()
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303646{
3647 int index = INVALID_PTN;
3648 unsigned long long ptn = 0;
3649 struct fbcon_config *fb_display = NULL;
Mao Flynn7b379f32015-04-20 00:28:30 +08003650 struct logo_img_header *header;
Mao Flynn1893a7f2015-06-03 12:03:36 +08003651 uint32_t blocksize, realsize, readsize;
3652 uint8_t *base;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303653
3654 index = partition_get_index("splash");
3655 if (index == 0) {
3656 dprintf(CRITICAL, "ERROR: splash Partition table not found\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003657 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303658 }
3659
3660 ptn = partition_get_offset(index);
3661 if (ptn == 0) {
3662 dprintf(CRITICAL, "ERROR: splash Partition invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003663 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303664 }
3665
Mao Flynn1893a7f2015-06-03 12:03:36 +08003666 mmc_set_lun(partition_get_lun(index));
3667
3668 blocksize = mmc_get_device_blocksize();
3669 if (blocksize == 0) {
3670 dprintf(CRITICAL, "ERROR:splash Partition invalid blocksize\n");
3671 return -1;
3672 }
3673
3674 fb_display = fbcon_display();
Channagoud Kadabidaf10a62015-07-22 11:51:55 -07003675 if (!fb_display)
3676 {
3677 dprintf(CRITICAL, "ERROR: fb config is not allocated\n");
3678 return -1;
3679 }
3680
Mao Flynn1893a7f2015-06-03 12:03:36 +08003681 base = (uint8_t *) fb_display->base;
3682
3683 if (mmc_read(ptn, (uint32_t *)(base + LOGO_IMG_OFFSET), blocksize)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303684 dprintf(CRITICAL, "ERROR: Cannot read splash image header\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003685 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303686 }
3687
Mao Flynn1893a7f2015-06-03 12:03:36 +08003688 header = (struct logo_img_header *)(base + LOGO_IMG_OFFSET);
Mao Flynn7b379f32015-04-20 00:28:30 +08003689 if (splash_screen_check_header(header)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303690 dprintf(CRITICAL, "ERROR: Splash image header invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003691 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303692 }
3693
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303694 if (fb_display) {
Mao Flynn1893a7f2015-06-03 12:03:36 +08003695 if (header->type && (header->blocks != 0)) { /* 1 RLE24 compressed data */
3696 base += LOGO_IMG_OFFSET;
Mao Flynn7b379f32015-04-20 00:28:30 +08003697
Mao Flynn1893a7f2015-06-03 12:03:36 +08003698 realsize = header->blocks * 512;
3699 readsize = ROUNDUP((realsize + LOGO_IMG_HEADER_SIZE), blocksize) - blocksize;
3700
3701 /* if the logo is not full-screen size, clean screen */
Mao Flynn7b379f32015-04-20 00:28:30 +08003702 if ((header->width != fb_display->width)
3703 || (header->height != fb_display->height))
3704 fbcon_clear();
3705
Mao Flynn1893a7f2015-06-03 12:03:36 +08003706 if (mmc_read(ptn + blocksize, (uint32_t *)(base + blocksize), readsize)) {
Mao Flynn7b379f32015-04-20 00:28:30 +08003707 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
3708 return -1;
3709 }
Mao Flynn7b379f32015-04-20 00:28:30 +08003710
Mao Flynn1893a7f2015-06-03 12:03:36 +08003711 fbcon_extract_to_screen(header, (base + LOGO_IMG_HEADER_SIZE));
3712 } else { /* 2 Raw BGR data */
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303713
Veera Sundaram Sankaranc33416f2015-09-28 10:25:55 -07003714 if ((header->width > fb_display->width) || (header->height > fb_display->height)) {
3715 dprintf(CRITICAL, "Logo config greater than fb config. Fall back default logo\n");
Mao Flynn1893a7f2015-06-03 12:03:36 +08003716 return -1;
3717 }
3718
3719 realsize = header->width * header->height * fb_display->bpp / 8;
3720 readsize = ROUNDUP((realsize + LOGO_IMG_HEADER_SIZE), blocksize) - blocksize;
3721
3722 if (blocksize == LOGO_IMG_HEADER_SIZE) { /* read the content directly */
3723 if (mmc_read((ptn + LOGO_IMG_HEADER_SIZE), (uint32_t *)base, readsize)) {
3724 fbcon_clear();
3725 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
3726 return -1;
3727 }
3728 } else {
3729 if (mmc_read(ptn + blocksize ,
3730 (uint32_t *)(base + LOGO_IMG_OFFSET + blocksize), readsize)) {
3731 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
3732 return -1;
3733 }
3734 memmove(base, (base + LOGO_IMG_OFFSET + LOGO_IMG_HEADER_SIZE), realsize);
3735 }
3736 }
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303737 }
3738
Mao Flynn7b379f32015-04-20 00:28:30 +08003739 return 0;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303740}
3741
Mao Flynn7b379f32015-04-20 00:28:30 +08003742int fetch_image_from_partition()
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303743{
3744 if (target_is_emmc_boot()) {
3745 return splash_screen_mmc();
3746 } else {
3747 return splash_screen_flash();
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003748 }
3749}
3750
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07003751/* Get the size from partiton name */
3752static void get_partition_size(const char *arg, char *response)
3753{
3754 uint64_t ptn = 0;
3755 uint64_t size;
3756 int index = INVALID_PTN;
3757
3758 index = partition_get_index(arg);
3759
3760 if (index == INVALID_PTN)
3761 {
3762 dprintf(CRITICAL, "Invalid partition index\n");
3763 return;
3764 }
3765
3766 ptn = partition_get_offset(index);
3767
3768 if(!ptn)
3769 {
3770 dprintf(CRITICAL, "Invalid partition name %s\n", arg);
3771 return;
3772 }
3773
3774 size = partition_get_size(index);
3775
3776 snprintf(response, MAX_RSP_SIZE, "\t 0x%llx", size);
3777 return;
3778}
3779
3780/*
3781 * Publish the partition type & size info
3782 * fastboot getvar will publish the required information.
3783 * fastboot getvar partition_size:<partition_name>: partition size in hex
3784 * fastboot getvar partition_type:<partition_name>: partition type (ext/fat)
3785 */
3786static void publish_getvar_partition_info(struct getvar_partition_info *info, uint8_t num_parts)
3787{
3788 uint8_t i;
3789
3790 for (i = 0; i < num_parts; i++) {
3791 get_partition_size(info[i].part_name, info[i].size_response);
3792
3793 if (strlcat(info[i].getvar_size, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
3794 {
3795 dprintf(CRITICAL, "partition size name truncated\n");
3796 return;
3797 }
3798 if (strlcat(info[i].getvar_type, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
3799 {
3800 dprintf(CRITICAL, "partition type name truncated\n");
3801 return;
3802 }
3803
3804 /* publish partition size & type info */
3805 fastboot_publish((const char *) info[i].getvar_size, (const char *) info[i].size_response);
3806 fastboot_publish((const char *) info[i].getvar_type, (const char *) info[i].type_response);
3807 }
3808}
3809
lijuang4ece1e72015-08-14 21:02:36 +08003810void get_product_name(unsigned char *buf)
3811{
3812 snprintf((char*)buf, MAX_RSP_SIZE, "%s", TARGET(BOARD));
3813 return;
3814}
3815
3816void get_bootloader_version(unsigned char *buf)
3817{
3818 snprintf((char*)buf, MAX_RSP_SIZE, "%s", device.bootloader_version);
3819 return;
3820}
3821
3822void get_baseband_version(unsigned char *buf)
3823{
3824 snprintf((char*)buf, MAX_RSP_SIZE, "%s", device.radio_version);
3825 return;
3826}
3827
3828bool is_device_locked()
3829{
3830 return device.is_unlocked ? false:true;
3831}
3832
Amol Jadi5edf3552013-07-23 14:15:34 -07003833/* register commands and variables for fastboot */
3834void aboot_fastboot_register_commands(void)
3835{
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003836 int i;
lijuangf16461c2015-08-03 17:09:34 +08003837 char hw_platform_buf[MAX_RSP_SIZE];
Amol Jadi5edf3552013-07-23 14:15:34 -07003838
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003839 struct fastboot_cmd_desc cmd_list[] = {
lijuang511a2b52015-08-14 20:50:51 +08003840 /* By default the enabled list is empty. */
3841 {"", NULL},
3842 /* move commands enclosed within the below ifndef to here
3843 * if they need to be enabled in user build.
3844 */
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003845#ifndef DISABLE_FASTBOOT_CMDS
lijuang511a2b52015-08-14 20:50:51 +08003846 /* Register the following commands only for non-user builds */
3847 {"flash:", cmd_flash},
3848 {"erase:", cmd_erase},
3849 {"boot", cmd_boot},
3850 {"continue", cmd_continue},
3851 {"reboot", cmd_reboot},
3852 {"reboot-bootloader", cmd_reboot_bootloader},
3853 {"oem unlock", cmd_oem_unlock},
3854 {"oem unlock-go", cmd_oem_unlock_go},
3855 {"oem lock", cmd_oem_lock},
3856 {"flashing unlock", cmd_oem_unlock},
3857 {"flashing lock", cmd_oem_lock},
3858 {"flashing lock_critical", cmd_flashing_lock_critical},
3859 {"flashing unlock_critical", cmd_flashing_unlock_critical},
3860 {"flashing get_unlock_ability", cmd_flashing_get_unlock_ability},
3861 {"oem device-info", cmd_oem_devinfo},
3862 {"preflash", cmd_preflash},
3863 {"oem enable-charger-screen", cmd_oem_enable_charger_screen},
3864 {"oem disable-charger-screen", cmd_oem_disable_charger_screen},
lijuanga25d8bb2015-09-16 13:11:52 +08003865 {"oem off-mode-charge", cmd_oem_off_mode_charger},
lijuang511a2b52015-08-14 20:50:51 +08003866 {"oem select-display-panel", cmd_oem_select_display_panel},
Channagoud Kadabi736c4962015-08-21 11:56:52 -07003867#if UNITTEST_FW_SUPPORT
Channagoud Kadabid5ddf482015-09-28 10:31:35 -07003868 {"oem run-tests", cmd_oem_runtests},
Channagoud Kadabi736c4962015-08-21 11:56:52 -07003869#endif
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003870#endif
lijuang511a2b52015-08-14 20:50:51 +08003871 };
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003872
3873 int fastboot_cmds_count = sizeof(cmd_list)/sizeof(cmd_list[0]);
3874 for (i = 1; i < fastboot_cmds_count; i++)
3875 fastboot_register(cmd_list[i].name,cmd_list[i].cb);
3876
Amol Jadi5edf3552013-07-23 14:15:34 -07003877 /* publish variables and their values */
3878 fastboot_publish("product", TARGET(BOARD));
3879 fastboot_publish("kernel", "lk");
3880 fastboot_publish("serialno", sn_buf);
3881
3882 /*
3883 * partition info is supported only for emmc partitions
3884 * Calling this for NAND prints some error messages which
3885 * is harmless but misleading. Avoid calling this for NAND
3886 * devices.
3887 */
3888 if (target_is_emmc_boot())
3889 publish_getvar_partition_info(part_info, ARRAY_SIZE(part_info));
3890
3891 /* Max download size supported */
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003892 snprintf(max_download_size, MAX_RSP_SIZE, "\t0x%x",
3893 target_get_max_flash_size());
Amol Jadi5edf3552013-07-23 14:15:34 -07003894 fastboot_publish("max-download-size", (const char *) max_download_size);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003895 /* Is the charger screen check enabled */
3896 snprintf(charger_screen_enabled, MAX_RSP_SIZE, "%d",
3897 device.charger_screen_enabled);
3898 fastboot_publish("charger-screen-enabled",
3899 (const char *) charger_screen_enabled);
lijuanga25d8bb2015-09-16 13:11:52 +08003900 fastboot_publish("off-mode-charge", (const char *) charger_screen_enabled);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05303901 snprintf(panel_display_mode, MAX_RSP_SIZE, "%s",
3902 device.display_panel);
3903 fastboot_publish("display-panel",
3904 (const char *) panel_display_mode);
Channagoud Kadabiaafbbfc2015-03-04 17:52:56 -08003905 fastboot_publish("version-bootloader", (const char *) device.bootloader_version);
3906 fastboot_publish("version-baseband", (const char *) device.radio_version);
lijuangf16461c2015-08-03 17:09:34 +08003907 fastboot_publish("secure", is_secure_boot_enable()? "yes":"no");
3908 smem_get_hw_platform_name((unsigned char *) hw_platform_buf, sizeof(hw_platform_buf));
3909 snprintf(get_variant, MAX_RSP_SIZE, "%s %s", hw_platform_buf,
3910 target_is_emmc_boot()? "eMMC":"UFS");
3911 fastboot_publish("variant", (const char *) get_variant);
lijuang65c5a822015-08-29 16:35:36 +08003912#if CHECK_BAT_VOLTAGE
lijuang102dfa92015-10-09 18:31:03 +08003913 update_battery_status();
lijuang65c5a822015-08-29 16:35:36 +08003914 fastboot_publish("battery-voltage", (const char *) battery_voltage);
lijuang102dfa92015-10-09 18:31:03 +08003915 fastboot_publish("battery-soc-ok", (const char *) battery_soc_ok);
lijuang65c5a822015-08-29 16:35:36 +08003916#endif
Amol Jadi5edf3552013-07-23 14:15:34 -07003917}
3918
Brian Swetland9c4c0752009-01-25 16:23:50 -08003919void aboot_init(const struct app_descriptor *app)
3920{
Shashank Mittal4f99a882010-02-01 13:58:50 -08003921 unsigned reboot_mode = 0;
Chandan Uddarajubedca152010-06-02 23:05:15 -07003922
lijuang39831732016-01-08 17:49:02 +08003923 /* Initialise wdog to catch early lk crashes */
3924#if WDOG_SUPPORT
3925 msm_wdog_init();
3926#endif
3927
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07003928 /* Setup page size information for nv storage */
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003929 if (target_is_emmc_boot())
3930 {
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07003931 page_size = mmc_page_size();
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003932 page_mask = page_size - 1;
Gaurav Nebhwani4d2389c2016-03-17 21:10:05 +05303933 mmc_blocksize = mmc_get_device_blocksize();
3934 mmc_blocksize_mask = mmc_blocksize - 1;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003935 }
3936 else
3937 {
3938 page_size = flash_page_size();
3939 page_mask = page_size - 1;
3940 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07003941 ASSERT((MEMBASE + MEMSIZE) > MEMBASE);
3942
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003943 read_device_info(&device);
vijay kumarc65876c2015-04-24 13:29:16 +05303944 read_allow_oem_unlock(&device);
Shashank Mittal162244e2011-08-08 19:01:25 -07003945
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08003946 /* Display splash screen if enabled */
3947#if DISPLAY_SPLASH_SCREEN
lijuang99c02d82015-02-13 19:04:34 +08003948#if NO_ALARM_DISPLAY
3949 if (!check_alarm_boot()) {
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08003950#endif
lijuang99c02d82015-02-13 19:04:34 +08003951 dprintf(SPEW, "Display Init: Start\n");
Ajay Singh Parmara7865a82015-04-16 21:27:52 -07003952#if DISPLAY_HDMI_PRIMARY
3953 if (!strlen(device.display_panel))
3954 strlcpy(device.display_panel, DISPLAY_PANEL_HDMI,
3955 sizeof(device.display_panel));
3956#endif
Channagoud Kadabi90869ce2015-04-27 11:15:14 -07003957#if ENABLE_WBC
Channagoud Kadabid801ac72015-08-26 11:21:51 -07003958 /* Wait if the display shutdown is in progress */
3959 while(pm_app_display_shutdown_in_prgs());
Channagoud Kadabi90869ce2015-04-27 11:15:14 -07003960 if (!pm_appsbl_display_init_done())
3961 target_display_init(device.display_panel);
3962 else
3963 display_image_on_screen();
3964#else
lijuang99c02d82015-02-13 19:04:34 +08003965 target_display_init(device.display_panel);
Channagoud Kadabi90869ce2015-04-27 11:15:14 -07003966#endif
lijuang99c02d82015-02-13 19:04:34 +08003967 dprintf(SPEW, "Display Init: Done\n");
3968#if NO_ALARM_DISPLAY
3969 }
3970#endif
3971#endif
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08003972
Greg Griscod6250552011-06-29 14:40:23 -07003973 target_serialno((unsigned char *) sn_buf);
Ajay Dudanib06c05f2011-05-12 14:46:10 -07003974 dprintf(SPEW,"serial number: %s\n",sn_buf);
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -08003975
Dhaval Patel223ec952013-07-18 14:49:44 -07003976 memset(display_panel_buf, '\0', MAX_PANEL_BUF_SIZE);
3977
Matthew Qindefd5562014-07-11 18:02:40 +08003978 /*
3979 * Check power off reason if user force reset,
3980 * if yes phone will do normal boot.
3981 */
3982 if (is_user_force_reset())
3983 goto normal_boot;
3984
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003985 /* Check if we should do something other than booting up */
Ameya Thakur0b9c2442013-05-10 13:22:56 -07003986 if (keys_get_state(KEY_VOLUMEUP) && keys_get_state(KEY_VOLUMEDOWN))
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07003987 {
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03003988 dprintf(ALWAYS,"dload mode key sequence detected\n");
lijuang395b5e62015-11-19 17:39:44 +08003989 reboot_device(EMERGENCY_DLOAD);
3990 dprintf(CRITICAL,"Failed to reboot into dload mode\n");
3991
Ameya Thakur0b9c2442013-05-10 13:22:56 -07003992 boot_into_fastboot = true;
3993 }
3994 if (!boot_into_fastboot)
3995 {
3996 if (keys_get_state(KEY_HOME) || keys_get_state(KEY_VOLUMEUP))
3997 boot_into_recovery = 1;
3998 if (!boot_into_recovery &&
3999 (keys_get_state(KEY_BACK) || keys_get_state(KEY_VOLUMEDOWN)))
Pavel Nedev5d91d412013-04-29 11:34:24 +03004000 boot_into_fastboot = true;
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07004001 }
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004002 #if NO_KEYPAD_DRIVER
Kinson Chik0b1c8162011-08-31 16:31:57 -07004003 if (fastboot_trigger())
Pavel Nedev5d91d412013-04-29 11:34:24 +03004004 boot_into_fastboot = true;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004005 #endif
Chandan Uddarajubedca152010-06-02 23:05:15 -07004006
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07004007#if USE_PON_REBOOT_REG
4008 reboot_mode = check_hard_reboot_mode();
4009#else
Ajay Dudani77421292010-10-27 19:34:06 -07004010 reboot_mode = check_reboot_mode();
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07004011#endif
4012 if (reboot_mode == RECOVERY_MODE)
4013 {
Ajay Dudani77421292010-10-27 19:34:06 -07004014 boot_into_recovery = 1;
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07004015 }
4016 else if(reboot_mode == FASTBOOT_MODE)
4017 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03004018 boot_into_fastboot = true;
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07004019 }
4020 else if(reboot_mode == ALARM_BOOT)
4021 {
Matthew Qind886f3c2014-01-17 16:52:01 +08004022 boot_reason_alarm = true;
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07004023 }
Parth Dixit207573a2015-10-27 17:26:21 +05304024#if VERIFIED_BOOT
Parth Dixitddbc7352015-10-18 03:13:31 +05304025#if !VBOOT_MOTA
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07004026 else if (reboot_mode == DM_VERITY_ENFORCING)
4027 {
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07004028 device.verity_mode = 1;
4029 write_device_info(&device);
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07004030 }
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07004031#if ENABLE_VB_ATTEST
4032 else if (reboot_mode == DM_VERITY_EIO)
4033#else
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07004034 else if (reboot_mode == DM_VERITY_LOGGING)
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07004035#endif
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07004036 {
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07004037 device.verity_mode = 0;
4038 write_device_info(&device);
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07004039 }
4040 else if (reboot_mode == DM_VERITY_KEYSCLEAR)
4041 {
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07004042 if(send_delete_keys_to_tz())
4043 ASSERT(0);
Ajay Dudani77421292010-10-27 19:34:06 -07004044 }
Parth Dixit207573a2015-10-27 17:26:21 +05304045#endif
Parth Dixitddbc7352015-10-18 03:13:31 +05304046#endif
Ajay Dudani77421292010-10-27 19:34:06 -07004047
Matthew Qindefd5562014-07-11 18:02:40 +08004048normal_boot:
Pavel Nedev5d91d412013-04-29 11:34:24 +03004049 if (!boot_into_fastboot)
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004050 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03004051 if (target_is_emmc_boot())
Shashank Mittala0032282011-08-26 14:50:11 -07004052 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03004053 if(emmc_recovery_init())
4054 dprintf(ALWAYS,"error in emmc_recovery_init\n");
4055 if(target_use_signed_kernel())
Shashank Mittala0032282011-08-26 14:50:11 -07004056 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03004057 if((device.is_unlocked) || (device.is_tampered))
4058 {
4059 #ifdef TZ_TAMPER_FUSE
4060 set_tamper_fuse_cmd();
4061 #endif
4062 #if USE_PCOM_SECBOOT
4063 set_tamper_flag(device.is_tampered);
4064 #endif
4065 }
Shashank Mittala0032282011-08-26 14:50:11 -07004066 }
Amit Blay6281ebc2015-01-11 14:44:08 +02004067
Pavel Nedev5d91d412013-04-29 11:34:24 +03004068 boot_linux_from_mmc();
Shashank Mittala0032282011-08-26 14:50:11 -07004069 }
Pavel Nedev5d91d412013-04-29 11:34:24 +03004070 else
4071 {
4072 recovery_init();
4073 #if USE_PCOM_SECBOOT
4074 if((device.is_unlocked) || (device.is_tampered))
4075 set_tamper_flag(device.is_tampered);
4076 #endif
4077 boot_linux_from_flash();
4078 }
4079 dprintf(CRITICAL, "ERROR: Could not do normal boot. Reverting "
4080 "to fastboot mode.\n");
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004081 }
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07004082
Amol Jadi5edf3552013-07-23 14:15:34 -07004083 /* We are here means regular boot did not happen. Start fastboot. */
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07004084
Amol Jadi5edf3552013-07-23 14:15:34 -07004085 /* register aboot specific fastboot commands */
4086 aboot_fastboot_register_commands();
Amol Jadi57abe4c2011-05-24 15:47:27 -07004087
Amol Jadi5edf3552013-07-23 14:15:34 -07004088 /* dump partition table for debug info */
Kinson Chikf1a43512011-07-14 11:28:39 -07004089 partition_dump();
Amol Jadi5edf3552013-07-23 14:15:34 -07004090
4091 /* initialize and start fastboot */
4092 fastboot_init(target_get_scratch_address(), target_get_max_flash_size());
lijuang4ece1e72015-08-14 21:02:36 +08004093#if FBCON_DISPLAY_MSG
lijuang9a7d3b92015-11-30 14:41:24 +08004094 display_fastboot_menu();
lijuang4ece1e72015-08-14 21:02:36 +08004095#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08004096}
4097
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07004098uint32_t get_page_size()
4099{
4100 return page_size;
4101}
4102
Amir Samuelov57a6fa22013-06-05 16:36:43 +03004103/*
4104 * Calculated and save hash (SHA256) for non-signed boot image.
4105 *
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07004106 * @param image_addr - Boot image address
4107 * @param image_size - Size of the boot image
Amir Samuelov57a6fa22013-06-05 16:36:43 +03004108 *
4109 * @return int - 0 on success, negative value on failure.
4110 */
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07004111static int aboot_save_boot_hash_mmc(uint32_t image_addr, uint32_t image_size)
Amir Samuelov57a6fa22013-06-05 16:36:43 +03004112{
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07004113 unsigned int digest[8];
4114#if IMAGE_VERIF_ALGO_SHA1
4115 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA1;
4116#else
4117 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA256;
4118#endif
Amir Samuelov57a6fa22013-06-05 16:36:43 +03004119
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07004120 target_crypto_init_params();
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -08004121 hash_find((unsigned char *) image_addr, image_size, (unsigned char *)&digest, auth_algo);
Amir Samuelov57a6fa22013-06-05 16:36:43 +03004122
4123 save_kernel_hash_cmd(digest);
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07004124 dprintf(INFO, "aboot_save_boot_hash_mmc: imagesize_actual size %d bytes.\n", (int) image_size);
Amir Samuelov57a6fa22013-06-05 16:36:43 +03004125
4126 return 0;
4127}
4128
Brian Swetland9c4c0752009-01-25 16:23:50 -08004129APP_START(aboot)
4130 .init = aboot_init,
4131APP_END