blob: 7852e4a57bfb072eeb2f5a4f070c1ed3e8d85300 [file] [log] [blame]
Brian Swetland9c4c0752009-01-25 16:23:50 -08001/*
2 * Copyright (c) 2009, Google Inc.
3 * All rights reserved.
4 *
lijuang9a7d3b92015-11-30 14:41:24 +08005 * Copyright (c) 2009-2016, The Linux Foundation. All rights reserved.
Brian Swetland9c4c0752009-01-25 16:23:50 -08006 *
Chandan Uddaraju5fa471a2009-12-02 17:31:34 -08007 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070014 * * Neither the name of The Linux Foundation nor
Chandan Uddaraju5fa471a2009-12-02 17:31:34 -080015 * the names of its contributors may be used to endorse or promote
16 * products derived from this software without specific prior written
17 * permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22 * NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 *
Brian Swetland9c4c0752009-01-25 16:23:50 -080031 */
32
33#include <app.h>
34#include <debug.h>
35#include <arch/arm.h>
Brian Swetland9c4c0752009-01-25 16:23:50 -080036#include <string.h>
Channagoud Kadabi132ff552013-04-19 14:34:44 -070037#include <stdlib.h>
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -070038#include <limits.h>
Brian Swetland9c4c0752009-01-25 16:23:50 -080039#include <kernel/thread.h>
40#include <arch/ops.h>
41
Dima Zavin214cc642009-01-26 11:16:21 -080042#include <dev/flash.h>
Tanya Brokhman1c94f1a2015-02-15 09:05:03 +020043#include <dev/flash-ubi.h>
Dima Zavin214cc642009-01-26 11:16:21 -080044#include <lib/ptable.h>
Dima Zavinb4283602009-01-26 16:36:57 -080045#include <dev/keys.h>
Shashank Mittal4f99a882010-02-01 13:58:50 -080046#include <dev/fbcon.h>
Ajay Dudanid04110c2011-01-17 23:55:07 -080047#include <baseband.h>
Greg Griscod6250552011-06-29 14:40:23 -070048#include <target.h>
49#include <mmc.h>
Kinson Chikf1a43512011-07-14 11:28:39 -070050#include <partition_parser.h>
Greg Griscod6250552011-06-29 14:40:23 -070051#include <platform.h>
Shashank Mittalcd98d472011-08-02 14:29:24 -070052#include <crypto_hash.h>
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070053#include <malloc.h>
Amol Jadi492d5a52013-03-15 16:12:34 -070054#include <boot_stats.h>
Amir Samuelov57a6fa22013-06-05 16:36:43 +030055#include <sha.h>
Sundarajan Srinivasan3827a102013-09-10 13:57:40 -070056#include <platform/iomap.h>
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -070057#include <boot_device.h>
Shashank Mittald3e54dd2014-08-28 15:24:02 -070058#include <boot_verifier.h>
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +053059#include <image_verify.h>
Matthew Qinbb7923d2015-02-09 10:56:09 +080060#include <decompress.h>
Unnati Gandhi17b3bfc2015-05-11 12:58:16 +053061#include <platform/timer.h>
lijuang511a2b52015-08-14 20:50:51 +080062#include <sys/types.h>
Channagoud Kadabi036c6052015-02-09 15:19:59 -080063#if USE_RPMB_FOR_DEVINFO
64#include <rpmb.h>
65#endif
Dima Zavin214cc642009-01-26 11:16:21 -080066
Channagoud Kadabi90869ce2015-04-27 11:15:14 -070067#if ENABLE_WBC
68#include <pm_app_smbchg.h>
69#endif
70
Neeti Desai17379b82012-06-04 18:42:53 -070071#if DEVICE_TREE
72#include <libfdt.h>
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070073#include <dev_tree.h>
Neeti Desai17379b82012-06-04 18:42:53 -070074#endif
75
Aparna Mallavarapu118ccae2015-06-03 13:47:11 +053076#if WDOG_SUPPORT
77#include <wdog.h>
78#endif
79
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -070080#include <reboot.h>
Shashank Mittalcd98d472011-08-02 14:29:24 -070081#include "image_verify.h"
Shashank Mittal024c0332010-02-03 11:44:00 -080082#include "recovery.h"
Brian Swetland9c4c0752009-01-25 16:23:50 -080083#include "bootimg.h"
84#include "fastboot.h"
Ajay Dudani5c761132011-04-07 20:19:04 -070085#include "sparse_format.h"
Ajay Dudanide984792015-03-02 09:57:41 -080086#include "meta_format.h"
Greg Grisco6e754772011-06-23 12:19:39 -070087#include "mmc.h"
Shashank Mittal162244e2011-08-08 19:01:25 -070088#include "devinfo.h"
Neeti Desai465491e2012-07-31 12:53:35 -070089#include "board.h"
Shashank Mittal162244e2011-08-08 19:01:25 -070090#include "scm.h"
Amit Blay6281ebc2015-01-11 14:44:08 +020091#include "mdtp.h"
Sridhar Parasuram32b30662015-07-10 13:33:22 -070092#include "secapp_loader.h"
lijuanga40d6302015-07-20 20:10:13 +080093#include <menu_keys_detect.h>
94#include <display_menu.h>
Channagoud Kadabi736c4962015-08-21 11:56:52 -070095#include "fastboot_test.h"
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -070096
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070097extern bool target_use_signed_kernel(void);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -070098extern void platform_uninit(void);
Channagoud Kadabi33defe22013-06-18 18:35:40 -070099extern void target_uninit(void);
Joonwoo Park61112782013-10-02 19:50:39 -0700100extern int get_target_boot_params(const char *cmdline, const char *part,
vijay kumar870515d2015-08-31 16:37:24 +0530101 char **buf);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700102
Sridhar Parasuram7e16d172015-07-05 11:35:23 -0700103void *info_buf;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700104void write_device_info_mmc(device_info *dev);
105void write_device_info_flash(device_info *dev);
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -0700106static int aboot_save_boot_hash_mmc(uint32_t image_addr, uint32_t image_size);
Channagoud Kadabiad259832015-05-29 11:14:17 -0700107static int aboot_frp_unlock(char *pname, void *data, unsigned sz);
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
Lijuan Gao4a5b8322014-07-24 10:38:42 +0800458 if (cmdline) {
459 if ((strstr(cmdline, DISPLAY_DEFAULT_PREFIX) == NULL) &&
Padmanabhan Komandurubccbcdc2015-06-30 16:19:24 +0530460 target_display_panel_node(display_panel_buf,
461 MAX_PANEL_BUF_SIZE) &&
Lijuan Gao4a5b8322014-07-24 10:38:42 +0800462 strlen(display_panel_buf)) {
463 cmdline_len += strlen(display_panel_buf);
464 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700465 }
466
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800467 if (target_warm_boot()) {
468 warm_boot = true;
469 cmdline_len += strlen(warmboot_cmdline);
470 }
471
Channagoud Kadabi85c7ec32016-01-28 23:09:21 -0800472#if TARGET_CMDLINE_SUPPORT
473 char *target_cmdline_buf = malloc(TARGET_MAX_CMDLNBUF);
474 int target_cmd_line_len;
475 ASSERT(target_cmdline_buf);
476 target_cmd_line_len = target_update_cmdline(target_cmdline_buf);
477 cmdline_len += target_cmd_line_len;
478#endif
479
David Ng183a7422009-12-07 14:55:21 -0800480 if (cmdline_len > 0) {
481 const char *src;
Maria Yu52254c02014-07-04 16:14:54 +0800482 unsigned char *dst;
483
484 cmdline_final = (unsigned char*) malloc((cmdline_len + 4) & (~3));
485 ASSERT(cmdline_final != NULL);
vijay kumar287bb542015-09-29 13:01:52 +0530486 memset((void *)cmdline_final, 0, sizeof(*cmdline_final));
Maria Yu52254c02014-07-04 16:14:54 +0800487 dst = cmdline_final;
Neeti Desaie245d492012-06-01 12:52:13 -0700488
Amol Jadi168b7712012-03-06 16:15:00 -0800489 /* Save start ptr for debug print */
David Ng183a7422009-12-07 14:55:21 -0800490 if (have_cmdline) {
491 src = cmdline;
492 while ((*dst++ = *src++));
493 }
494 if (target_is_emmc_boot()) {
495 src = emmc_cmdline;
496 if (have_cmdline) --dst;
David Ngf773dde2010-07-26 19:55:08 -0700497 have_cmdline = 1;
498 while ((*dst++ = *src++));
Sridhar Parasuram7bd4aaf2015-02-12 11:14:38 -0800499#if USE_BOOTDEV_CMDLINE
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700500 src = boot_dev_buf;
501 if (have_cmdline) --dst;
502 while ((*dst++ = *src++));
503#endif
David Ngf773dde2010-07-26 19:55:08 -0700504 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800505
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700506#if VERIFIED_BOOT
Parth Dixitddbc7352015-10-18 03:13:31 +0530507#if !VBOOT_MOTA
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700508 src = verified_state;
509 if(have_cmdline) --dst;
510 have_cmdline = 1;
511 while ((*dst++ = *src++));
512 src = vbsn[boot_state].name;
513 if(have_cmdline) --dst;
514 while ((*dst++ = *src++));
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700515
Sridhar Parasuram7d8bb9a2015-09-23 18:21:19 -0700516 if ((device.verity_mode != 0 ) && (device.verity_mode != 1))
517 {
518 dprintf(CRITICAL, "Devinfo paritition possibly corrupted!!!. Please erase devinfo partition to continue booting\n");
519 ASSERT(0);
520 }
Sridhar Parasuramd69f7902015-07-10 13:31:17 -0700521 src = verity_mode;
522 if(have_cmdline) --dst;
523 while ((*dst++ = *src++));
524 src = vbvm[device.verity_mode].name;
525 if(have_cmdline) -- dst;
526 while ((*dst++ = *src++));
Parth Dixita5715a02015-10-29 12:25:10 +0530527 src = keymaster_v1;
528 if(have_cmdline) --dst;
529 while ((*dst++ = *src++));
Sridhar Parasuram4311b8e2015-05-28 17:01:59 -0700530#endif
Parth Dixitddbc7352015-10-18 03:13:31 +0530531#endif
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800532 src = usb_sn_cmdline;
533 if (have_cmdline) --dst;
534 have_cmdline = 1;
535 while ((*dst++ = *src++));
536 src = sn_buf;
537 if (have_cmdline) --dst;
538 have_cmdline = 1;
539 while ((*dst++ = *src++));
Hanumant Singh8e1ac232014-01-29 13:41:51 -0800540 if (warm_boot) {
541 if (have_cmdline) --dst;
542 src = warmboot_cmdline;
543 while ((*dst++ = *src++));
544 }
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -0800545
Pavel Nedev5614d222013-06-17 18:01:02 +0300546 if (boot_into_recovery && gpt_exists) {
547 src = secondary_gpt_enable;
548 if (have_cmdline) --dst;
549 while ((*dst++ = *src++));
550 }
551
Shay Nachmanibc10dfe2015-02-10 14:45:55 +0200552 if (is_mdtp_activated) {
553 src = mdtp_activated_flag;
554 if (have_cmdline) --dst;
555 while ((*dst++ = *src++));
556 }
557
Pavel Nedev328ac822013-04-05 15:25:11 +0300558 if (boot_into_ffbm) {
559 src = androidboot_mode;
560 if (have_cmdline) --dst;
561 while ((*dst++ = *src++));
Deepa Dinamani41fa8d62013-05-23 13:25:36 -0700562 src = ffbm_mode_string;
Pavel Nedev328ac822013-04-05 15:25:11 +0300563 if (have_cmdline) --dst;
564 while ((*dst++ = *src++));
Pavel Nedev898298c2013-02-27 12:36:09 -0800565 src = loglevel;
566 if (have_cmdline) --dst;
567 while ((*dst++ = *src++));
Matthew Qind886f3c2014-01-17 16:52:01 +0800568 } else if (boot_reason_alarm) {
569 src = alarmboot_cmdline;
570 if (have_cmdline) --dst;
571 while ((*dst++ = *src++));
Pavel Nedev328ac822013-04-05 15:25:11 +0300572 } else if (pause_at_bootup) {
David Ngf773dde2010-07-26 19:55:08 -0700573 src = battchg_pause;
574 if (have_cmdline) --dst;
David Ng183a7422009-12-07 14:55:21 -0800575 while ((*dst++ = *src++));
576 }
Ajay Dudanid04110c2011-01-17 23:55:07 -0800577
Shashank Mittalcd98d472011-08-02 14:29:24 -0700578 if(target_use_signed_kernel() && auth_kernel_img) {
579 src = auth_kernel;
580 if (have_cmdline) --dst;
581 while ((*dst++ = *src++));
582 }
583
Ajay Dudanid04110c2011-01-17 23:55:07 -0800584 switch(target_baseband())
585 {
586 case BASEBAND_APQ:
587 src = baseband_apq;
588 if (have_cmdline) --dst;
589 while ((*dst++ = *src++));
590 break;
591
592 case BASEBAND_MSM:
593 src = baseband_msm;
594 if (have_cmdline) --dst;
595 while ((*dst++ = *src++));
596 break;
597
598 case BASEBAND_CSFB:
599 src = baseband_csfb;
600 if (have_cmdline) --dst;
601 while ((*dst++ = *src++));
602 break;
603
Ajay Dudani6cff85e2011-02-04 16:02:16 -0800604 case BASEBAND_SVLTE2A:
605 src = baseband_svlte2a;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800606 if (have_cmdline) --dst;
607 while ((*dst++ = *src++));
608 break;
Ajay Dudani403bc492011-09-30 16:17:21 -0700609
610 case BASEBAND_MDM:
611 src = baseband_mdm;
612 if (have_cmdline) --dst;
613 while ((*dst++ = *src++));
614 break;
Amol Jadi5c61a952012-05-04 17:05:35 -0700615
Sundarajan Srinivasanaaa8aff2013-11-12 17:19:14 -0800616 case BASEBAND_MDM2:
617 src = baseband_mdm2;
618 if (have_cmdline) --dst;
619 while ((*dst++ = *src++));
620 break;
621
Amol Jadi5c61a952012-05-04 17:05:35 -0700622 case BASEBAND_SGLTE:
623 src = baseband_sglte;
624 if (have_cmdline) --dst;
625 while ((*dst++ = *src++));
626 break;
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530627
sundarajan srinivasanf8e9f3f2013-03-04 15:56:58 -0800628 case BASEBAND_SGLTE2:
629 src = baseband_sglte2;
630 if (have_cmdline) --dst;
631 while ((*dst++ = *src++));
632 break;
633
Channagoud Kadabi141f2982012-10-31 11:23:02 +0530634 case BASEBAND_DSDA:
635 src = baseband_dsda;
636 if (have_cmdline) --dst;
637 while ((*dst++ = *src++));
638 break;
Amol Jadi2a15a272013-01-22 12:03:36 -0800639
640 case BASEBAND_DSDA2:
641 src = baseband_dsda2;
642 if (have_cmdline) --dst;
643 while ((*dst++ = *src++));
644 break;
Vijay Kumar Pendotib228cfc2016-06-13 20:15:23 +0530645 case BASEBAND_APQ_NOWGR:
646 src = baseband_apq_nowgr;
647 if (have_cmdline) --dst;
648 while ((*dst++ = *src++));
649 break;
Ajay Dudanid04110c2011-01-17 23:55:07 -0800650 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700651
652 if (strlen(display_panel_buf)) {
Dhaval Patel223ec952013-07-18 14:49:44 -0700653 src = display_panel_buf;
654 if (have_cmdline) --dst;
655 while ((*dst++ = *src++));
656 }
Joonwoo Park61112782013-10-02 19:50:39 -0700657
658 if (have_target_boot_params) {
659 if (have_cmdline) --dst;
660 src = target_boot_params;
661 while ((*dst++ = *src++));
vijay kumar870515d2015-08-31 16:37:24 +0530662 free(target_boot_params);
Joonwoo Park61112782013-10-02 19:50:39 -0700663 }
Channagoud Kadabi85c7ec32016-01-28 23:09:21 -0800664
665#if TARGET_CMDLINE_SUPPORT
666 if (target_cmdline_buf && target_cmd_line_len)
667 {
668 if (have_cmdline) --dst;
669 src = target_cmdline_buf;
670 while((*dst++ = *src++));
671 free(target_cmdline_buf);
672 }
673#endif
Neeti Desaie245d492012-06-01 12:52:13 -0700674 }
Dhaval Patel223ec952013-07-18 14:49:44 -0700675
676
Sundarajan Srinivasane52065a2014-03-20 16:25:59 -0700677 if (boot_dev_buf)
678 free(boot_dev_buf);
679
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -0800680 if (cmdline_final)
681 dprintf(INFO, "cmdline: %s\n", cmdline_final);
682 else
683 dprintf(INFO, "cmdline is NULL\n");
Neeti Desaie245d492012-06-01 12:52:13 -0700684 return cmdline_final;
685}
686
687unsigned *atag_core(unsigned *ptr)
688{
689 /* CORE */
690 *ptr++ = 2;
691 *ptr++ = 0x54410001;
692
693 return ptr;
694
695}
696
697unsigned *atag_ramdisk(unsigned *ptr, void *ramdisk,
698 unsigned ramdisk_size)
699{
700 if (ramdisk_size) {
701 *ptr++ = 4;
702 *ptr++ = 0x54420005;
703 *ptr++ = (unsigned)ramdisk;
704 *ptr++ = ramdisk_size;
Brian Swetland9c4c0752009-01-25 16:23:50 -0800705 }
706
Neeti Desaie245d492012-06-01 12:52:13 -0700707 return ptr;
708}
709
710unsigned *atag_ptable(unsigned **ptr_addr)
711{
712 int i;
713 struct ptable *ptable;
714
715 if ((ptable = flash_get_ptable()) && (ptable->count != 0)) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700716 *(*ptr_addr)++ = 2 + (ptable->count * (sizeof(struct atag_ptbl_entry) /
717 sizeof(unsigned)));
Neeti Desaie245d492012-06-01 12:52:13 -0700718 *(*ptr_addr)++ = 0x4d534d70;
719 for (i = 0; i < ptable->count; ++i)
720 ptentry_to_tag(ptr_addr, ptable_get(ptable, i));
721 }
722
723 return (*ptr_addr);
724}
725
726unsigned *atag_cmdline(unsigned *ptr, const char *cmdline)
727{
728 int cmdline_length = 0;
729 int n;
Neeti Desaie245d492012-06-01 12:52:13 -0700730 char *dest;
731
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800732 cmdline_length = strlen((const char*)cmdline);
Neeti Desaie245d492012-06-01 12:52:13 -0700733 n = (cmdline_length + 4) & (~3);
734
735 *ptr++ = (n / 4) + 2;
736 *ptr++ = 0x54410009;
737 dest = (char *) ptr;
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800738 while ((*dest++ = *cmdline++));
Neeti Desaie245d492012-06-01 12:52:13 -0700739 ptr += (n / 4);
740
741 return ptr;
742}
743
744unsigned *atag_end(unsigned *ptr)
745{
Brian Swetland9c4c0752009-01-25 16:23:50 -0800746 /* END */
747 *ptr++ = 0;
748 *ptr++ = 0;
749
Neeti Desaie245d492012-06-01 12:52:13 -0700750 return ptr;
751}
752
753void generate_atags(unsigned *ptr, const char *cmdline,
754 void *ramdisk, unsigned ramdisk_size)
755{
vijay kumar21a37452015-12-29 16:23:21 +0530756 unsigned *orig_ptr = ptr;
Neeti Desaie245d492012-06-01 12:52:13 -0700757 ptr = atag_core(ptr);
758 ptr = atag_ramdisk(ptr, ramdisk, ramdisk_size);
759 ptr = target_atag_mem(ptr);
760
761 /* Skip NAND partition ATAGS for eMMC boot */
762 if (!target_is_emmc_boot()){
763 ptr = atag_ptable(&ptr);
764 }
765
vijay kumar21a37452015-12-29 16:23:21 +0530766 /*
767 * Atags size filled till + cmdline size + 1 unsigned for 4-byte boundary + 4 unsigned
768 * for atag identifier in atag_cmdline and atag_end should be with in MAX_TAGS_SIZE bytes
769 */
770 if (((ptr - orig_ptr) + strlen(cmdline) + 5 * sizeof(unsigned)) < MAX_TAGS_SIZE) {
771 ptr = atag_cmdline(ptr, cmdline);
772 ptr = atag_end(ptr);
773 }
774 else {
775 dprintf(CRITICAL,"Crossing ATAGs Max size allowed\n");
776 ASSERT(0);
777 }
Neeti Desaie245d492012-06-01 12:52:13 -0700778}
779
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700780typedef void entry_func_ptr(unsigned, unsigned, unsigned*);
Neeti Desaie245d492012-06-01 12:52:13 -0700781void boot_linux(void *kernel, unsigned *tags,
782 const char *cmdline, unsigned machtype,
783 void *ramdisk, unsigned ramdisk_size)
784{
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800785 unsigned char *final_cmdline;
Amol Jadib6be5c12012-11-14 13:39:51 -0800786#if DEVICE_TREE
Neeti Desai17379b82012-06-04 18:42:53 -0700787 int ret = 0;
Amol Jadib6be5c12012-11-14 13:39:51 -0800788#endif
789
Deepa Dinamani0bf2f442012-10-19 11:41:06 -0700790 void (*entry)(unsigned, unsigned, unsigned*) = (entry_func_ptr*)(PA((addr_t)kernel));
Deepa Dinamani25a9f762012-11-30 15:57:15 -0800791 uint32_t tags_phys = PA((addr_t)tags);
vijay kumar1a50a642015-11-16 12:41:15 +0530792 struct kernel64_hdr *kptr = ((struct kernel64_hdr*)(PA((addr_t)kernel)));
Deepa Dinamani25a9f762012-11-30 15:57:15 -0800793
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530794 ramdisk = (void *)PA((addr_t)ramdisk);
Neeti Desaie245d492012-06-01 12:52:13 -0700795
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800796 final_cmdline = update_cmdline((const char*)cmdline);
797
Neeti Desai17379b82012-06-04 18:42:53 -0700798#if DEVICE_TREE
Amol Jadib6be5c12012-11-14 13:39:51 -0800799 dprintf(INFO, "Updating device tree: start\n");
800
Neeti Desai17379b82012-06-04 18:42:53 -0700801 /* Update the Device Tree */
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530802 ret = update_device_tree((void *)tags,(const char *)final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -0700803 if(ret)
804 {
805 dprintf(CRITICAL, "ERROR: Updating Device Tree Failed \n");
806 ASSERT(0);
807 }
Amol Jadib6be5c12012-11-14 13:39:51 -0800808 dprintf(INFO, "Updating device tree: done\n");
Neeti Desai17379b82012-06-04 18:42:53 -0700809#else
Neeti Desaie245d492012-06-01 12:52:13 -0700810 /* Generating the Atags */
Amol Jadi10c7d1c2013-01-25 13:24:29 -0800811 generate_atags(tags, final_cmdline, ramdisk, ramdisk_size);
Neeti Desai17379b82012-06-04 18:42:53 -0700812#endif
Neeti Desaie245d492012-06-01 12:52:13 -0700813
Maria Yu52254c02014-07-04 16:14:54 +0800814 free(final_cmdline);
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -0700815
816#if VERIFIED_BOOT
lijuangbdd9bb42016-03-01 18:22:17 +0800817#if !VBOOT_MOTA
818 if (device.verity_mode == 0) {
819#if FBCON_DISPLAY_MSG
Channagoud Kadabi86b0c112016-03-16 19:23:16 -0700820#if ENABLE_VB_ATTEST
821 display_bootverify_menu(DISPLAY_MENU_EIO);
Parth Dixit54ac3bb2017-03-07 15:52:48 +0530822 wait_for_users_action();
823 if(!pwr_key_is_pressed)
824 shutdown_device();
Channagoud Kadabi86b0c112016-03-16 19:23:16 -0700825#else
lijuangbdd9bb42016-03-01 18:22:17 +0800826 display_bootverify_menu(DISPLAY_MENU_LOGGING);
Channagoud Kadabi86b0c112016-03-16 19:23:16 -0700827#endif
lijuangbdd9bb42016-03-01 18:22:17 +0800828 wait_for_users_action();
829#else
830 dprintf(CRITICAL,
831 "The dm-verity is not started in enforcing mode.\nWait for 5 seconds before proceeding\n");
832 mdelay(5000);
833#endif
834 }
835
836#endif
837#endif
838
839#if VERIFIED_BOOT
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -0700840 /* Write protect the device info */
Channagoud Kadabi3bd9d1e2015-05-05 16:18:20 -0700841 if (!boot_into_recovery && target_build_variant_user() && devinfo_present && mmc_write_protect("devinfo", 1))
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -0700842 {
843 dprintf(INFO, "Failed to write protect dev info\n");
844 ASSERT(0);
845 }
846#endif
847
Aravind Venkateswaran8f076242014-02-25 16:25:30 -0800848 /* Turn off splash screen if enabled */
849#if DISPLAY_SPLASH_SCREEN
850 target_display_shutdown();
851#endif
852
Veera Sundaram Sankaran67ea0932015-09-25 10:09:30 -0700853 /* Perform target specific cleanup */
854 target_uninit();
Aravind Venkateswaran8f076242014-02-25 16:25:30 -0800855
Deepa Dinamani33734bc2013-03-06 12:16:06 -0800856 dprintf(INFO, "booting linux @ %p, ramdisk @ %p (%d), tags/device tree @ %p\n",
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +0530857 entry, ramdisk, ramdisk_size, (void *)tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800858
859 enter_critical_section();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700860
Amol Jadi4421e652011-06-16 15:00:48 -0700861 /* do any platform specific cleanup before kernel entry */
862 platform_uninit();
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700863
Brian Swetland9c4c0752009-01-25 16:23:50 -0800864 arch_disable_cache(UCACHE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -0700865
Amol Jadi504f9fe2012-08-16 13:56:48 -0700866#if ARM_WITH_MMU
Brian Swetland9c4c0752009-01-25 16:23:50 -0800867 arch_disable_mmu();
Amol Jadi504f9fe2012-08-16 13:56:48 -0700868#endif
Amol Jadi492d5a52013-03-15 16:12:34 -0700869 bs_set_timestamp(BS_KERNEL_ENTRY);
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800870
871 if (IS_ARM64(kptr))
872 /* Jump to a 64bit kernel */
873 scm_elexec_call((paddr_t)kernel, tags_phys);
874 else
875 /* Jump to a 32bit kernel */
876 entry(0, machtype, (unsigned*)tags_phys);
Brian Swetland9c4c0752009-01-25 16:23:50 -0800877}
878
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700879/* Function to check if the memory address range falls within the aboot
880 * boundaries.
881 * start: Start of the memory region
882 * size: Size of the memory region
883 */
Vijay Kumar Pendoti9c002ad2016-03-09 13:52:45 +0530884int check_aboot_addr_range_overlap(uintptr_t start, uint32_t size)
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700885{
886 /* Check for boundary conditions. */
Sundarajan Srinivasance2a0ea2013-12-16 17:02:56 -0800887 if ((UINT_MAX - start) < size)
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700888 return -1;
889
890 /* Check for memory overlap. */
891 if ((start < MEMBASE) && ((start + size) <= MEMBASE))
892 return 0;
Channagoud Kadabi94143912013-10-15 12:53:52 -0700893 else if (start >= (MEMBASE + MEMSIZE))
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700894 return 0;
895 else
896 return -1;
897}
898
Shashank Mittaldcc2e352009-11-19 19:11:16 -0800899#define ROUND_TO_PAGE(x,y) (((x) + (y)) & (~(y)))
Brian Swetland9c4c0752009-01-25 16:23:50 -0800900
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800901BUF_DMA_ALIGN(buf, BOOT_IMG_MAX_PAGE_SIZE); //Equal to max-supported pagesize
Amol Jadib6be5c12012-11-14 13:39:51 -0800902#if DEVICE_TREE
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -0800903BUF_DMA_ALIGN(dt_buf, BOOT_IMG_MAX_PAGE_SIZE);
Amol Jadib6be5c12012-11-14 13:39:51 -0800904#endif
Dima Zavin214cc642009-01-26 11:16:21 -0800905
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700906static void verify_signed_bootimg(uint32_t bootimg_addr, uint32_t bootimg_size)
907{
908 int ret;
Channagoud Kadabia8c623f2015-01-13 14:48:48 -0800909
910#if !VERIFIED_BOOT
Sundarajan Srinivasance54d6e2013-11-11 12:45:00 -0800911#if IMAGE_VERIF_ALGO_SHA1
912 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA1;
913#else
914 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA256;
915#endif
Channagoud Kadabia8c623f2015-01-13 14:48:48 -0800916#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700917
918 /* Assume device is rooted at this time. */
919 device.is_tampered = 1;
920
921 dprintf(INFO, "Authenticating boot image (%d): start\n", bootimg_size);
922
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700923#if VERIFIED_BOOT
924 if(boot_into_recovery)
925 {
926 ret = boot_verify_image((unsigned char *)bootimg_addr,
Unnati Gandhi47051252015-03-02 15:21:09 +0530927 bootimg_size, "/recovery");
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700928 }
929 else
930 {
931 ret = boot_verify_image((unsigned char *)bootimg_addr,
Unnati Gandhi47051252015-03-02 15:21:09 +0530932 bootimg_size, "/boot");
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700933 }
934 boot_verify_print_state();
935#else
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700936 ret = image_verify((unsigned char *)bootimg_addr,
937 (unsigned char *)(bootimg_addr + bootimg_size),
938 bootimg_size,
Sundarajan Srinivasance54d6e2013-11-11 12:45:00 -0800939 auth_algo);
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700940#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700941 dprintf(INFO, "Authenticating boot image: done return value = %d\n", ret);
942
943 if (ret)
944 {
945 /* Authorized kernel */
946 device.is_tampered = 0;
Sundarajan Srinivasan3fb21f12013-09-16 18:36:15 -0700947 auth_kernel_img = 1;
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700948 }
949
Amit Blay4aa292f2015-04-28 21:55:59 +0300950#ifdef MDTP_SUPPORT
951 {
952 /* Verify MDTP lock.
953 * For boot & recovery partitions, use aboot's verification result.
954 */
955 mdtp_ext_partition_verification_t ext_partition;
956 ext_partition.partition = boot_into_recovery ? MDTP_PARTITION_RECOVERY : MDTP_PARTITION_BOOT;
957 ext_partition.integrity_state = device.is_tampered ? MDTP_PARTITION_STATE_INVALID : MDTP_PARTITION_STATE_VALID;
958 ext_partition.page_size = 0; /* Not needed since already validated */
959 ext_partition.image_addr = 0; /* Not needed since already validated */
960 ext_partition.image_size = 0; /* Not needed since already validated */
961 ext_partition.sig_avail = FALSE; /* Not needed since already validated */
962 mdtp_fwlock_verify_lock(&ext_partition);
963 }
964#endif /* MDTP_SUPPORT */
965
Deepa Dinamani0e163a42013-05-24 17:08:15 -0700966#if USE_PCOM_SECBOOT
967 set_tamper_flag(device.is_tampered);
968#endif
969
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700970#if VERIFIED_BOOT
Sridhar Parasuram8b792422015-07-05 11:38:13 -0700971 switch(boot_verify_get_state())
Shashank Mittald3e54dd2014-08-28 15:24:02 -0700972 {
Sridhar Parasuram8b792422015-07-05 11:38:13 -0700973 case RED:
lijuanga40d6302015-07-20 20:10:13 +0800974#if FBCON_DISPLAY_MSG
lijuang9a7d3b92015-11-30 14:41:24 +0800975 display_bootverify_menu(DISPLAY_MENU_RED);
Channagoud Kadabi86b0c112016-03-16 19:23:16 -0700976#if ENABLE_VB_ATTEST
977 mdelay(DELAY_WAIT);
978 shutdown_device();
979#else
lijuanga40d6302015-07-20 20:10:13 +0800980 wait_for_users_action();
Channagoud Kadabi86b0c112016-03-16 19:23:16 -0700981#endif
lijuanga40d6302015-07-20 20:10:13 +0800982#else
983 dprintf(CRITICAL,
984 "Your device has failed verification and may not work properly.\nWait for 5 seconds before proceeding\n");
985 mdelay(5000);
986#endif
987
988 break;
Sridhar Parasuram8b792422015-07-05 11:38:13 -0700989 case YELLOW:
lijuanga40d6302015-07-20 20:10:13 +0800990#if FBCON_DISPLAY_MSG
lijuang9a7d3b92015-11-30 14:41:24 +0800991 display_bootverify_menu(DISPLAY_MENU_YELLOW);
lijuanga40d6302015-07-20 20:10:13 +0800992 wait_for_users_action();
993#else
994 dprintf(CRITICAL,
995 "Your device has loaded a different operating system.\nWait for 5 seconds before proceeding\n");
996 mdelay(5000);
997#endif
998 break;
Sridhar Parasuram8b792422015-07-05 11:38:13 -0700999 default:
lijuanga40d6302015-07-20 20:10:13 +08001000 break;
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001001 }
1002#endif
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001003#if !VERIFIED_BOOT
Unnati Gandhi1be04752015-03-27 19:41:53 +05301004 if(device.is_tampered)
1005 {
1006 write_device_info_mmc(&device);
1007 #ifdef TZ_TAMPER_FUSE
1008 set_tamper_fuse_cmd();
1009 #endif
1010 #ifdef ASSERT_ON_TAMPER
1011 dprintf(CRITICAL, "Device is tampered. Asserting..\n");
1012 ASSERT(0);
1013 #endif
1014 }
Sridhar Parasuram8b792422015-07-05 11:38:13 -07001015#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001016}
1017
Maunik Shah0f3c8ac2014-03-02 17:47:58 +05301018static bool check_format_bit()
1019{
1020 bool ret = false;
1021 int index;
1022 uint64_t offset;
1023 struct boot_selection_info *in = NULL;
1024 char *buf = NULL;
1025
1026 index = partition_get_index("bootselect");
1027 if (index == INVALID_PTN)
1028 {
1029 dprintf(INFO, "Unable to locate /bootselect partition\n");
1030 return ret;
1031 }
1032 offset = partition_get_offset(index);
1033 if(!offset)
1034 {
1035 dprintf(INFO, "partition /bootselect doesn't exist\n");
1036 return ret;
1037 }
1038 buf = (char *) memalign(CACHE_LINE, ROUNDUP(page_size, CACHE_LINE));
Mayank Grover48860402016-11-29 12:34:53 +05301039 mmc_set_lun(partition_get_lun(index));
Maunik Shah0f3c8ac2014-03-02 17:47:58 +05301040 ASSERT(buf);
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301041 if (mmc_read(offset, (uint32_t *)buf, page_size))
Maunik Shah0f3c8ac2014-03-02 17:47:58 +05301042 {
1043 dprintf(INFO, "mmc read failure /bootselect %d\n", page_size);
1044 free(buf);
1045 return ret;
1046 }
1047 in = (struct boot_selection_info *) buf;
1048 if ((in->signature == BOOTSELECT_SIGNATURE) &&
1049 (in->version == BOOTSELECT_VERSION)) {
1050 if ((in->state_info & BOOTSELECT_FORMAT) &&
1051 !(in->state_info & BOOTSELECT_FACTORY))
1052 ret = true;
1053 } else {
1054 dprintf(CRITICAL, "Signature: 0x%08x or version: 0x%08x mismatched of /bootselect\n",
1055 in->signature, in->version);
1056 ASSERT(0);
1057 }
1058 free(buf);
1059 return ret;
1060}
1061
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001062void boot_verifier_init()
1063{
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001064 uint32_t boot_state;
1065 /* Check if device unlock */
1066 if(device.is_unlocked)
1067 {
1068 boot_verify_send_event(DEV_UNLOCK);
1069 boot_verify_print_state();
1070 dprintf(CRITICAL, "Device is unlocked! Skipping verification...\n");
1071 return;
1072 }
1073 else
1074 {
1075 boot_verify_send_event(BOOT_INIT);
1076 }
1077
1078 /* Initialize keystore */
1079 boot_state = boot_verify_keystore_init();
1080 if(boot_state == YELLOW)
1081 {
1082 boot_verify_print_state();
1083 dprintf(CRITICAL, "Keystore verification failed! Continuing anyways...\n");
1084 }
1085}
1086
Shashank Mittal23b8f422010-04-16 19:27:21 -07001087int boot_linux_from_mmc(void)
1088{
1089 struct boot_img_hdr *hdr = (void*) buf;
1090 struct boot_img_hdr *uhdr;
1091 unsigned offset = 0;
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07001092 int rcode;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001093 unsigned long long ptn = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07001094 int index = INVALID_PTN;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001095
Shashank Mittalcd98d472011-08-02 14:29:24 -07001096 unsigned char *image_addr = 0;
1097 unsigned kernel_actual;
1098 unsigned ramdisk_actual;
1099 unsigned imagesize_actual;
Neeti Desai465491e2012-07-31 12:53:35 -07001100 unsigned second_actual = 0;
Neeti Desai465491e2012-07-31 12:53:35 -07001101
Matthew Qin271927e2015-03-31 22:07:07 -04001102 unsigned int dtb_size = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08001103 unsigned int out_len = 0;
1104 unsigned int out_avai_len = 0;
1105 unsigned char *out_addr = NULL;
1106 uint32_t dtb_offset = 0;
1107 unsigned char *kernel_start_addr = NULL;
1108 unsigned int kernel_size = 0;
Ameya Thakur10a33452016-06-13 14:24:26 -07001109 unsigned int patched_kernel_hdr_size = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08001110 int rc;
Neeti Desai465491e2012-07-31 12:53:35 -07001111#if DEVICE_TREE
1112 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -07001113 struct dt_entry dt_entry;
Neeti Desai465491e2012-07-31 12:53:35 -07001114 unsigned dt_table_offset;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001115 uint32_t dt_actual;
Deepa Dinamani19648b42013-09-05 17:05:55 -07001116 uint32_t dt_hdr_size;
Matthew Qin271927e2015-03-31 22:07:07 -04001117 unsigned char *best_match_dt_addr = NULL;
Neeti Desai465491e2012-07-31 12:53:35 -07001118#endif
Matthew Qin49e51fa2015-02-09 10:40:45 +08001119 struct kernel64_hdr *kptr = NULL;
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001120
Maunik Shah0f3c8ac2014-03-02 17:47:58 +05301121 if (check_format_bit())
1122 boot_into_recovery = 1;
1123
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07001124 if (!boot_into_recovery) {
1125 memset(ffbm_mode_string, '\0', sizeof(ffbm_mode_string));
1126 rcode = get_ffbm(ffbm_mode_string, sizeof(ffbm_mode_string));
1127 if (rcode <= 0) {
1128 boot_into_ffbm = false;
1129 if (rcode < 0)
1130 dprintf(CRITICAL,"failed to get ffbm cookie");
1131 } else
1132 boot_into_ffbm = true;
1133 } else
1134 boot_into_ffbm = false;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001135 uhdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
1136 if (!memcmp(uhdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1137 dprintf(INFO, "Unified boot method!\n");
1138 hdr = uhdr;
1139 goto unified_boot;
1140 }
Greg Griscod6250552011-06-29 14:40:23 -07001141 if (!boot_into_recovery) {
Kinson Chikf1a43512011-07-14 11:28:39 -07001142 index = partition_get_index("boot");
1143 ptn = partition_get_offset(index);
1144 if(ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -07001145 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1146 return -1;
1147 }
Kinson Chikf1a43512011-07-14 11:28:39 -07001148 }
1149 else {
1150 index = partition_get_index("recovery");
1151 ptn = partition_get_offset(index);
1152 if(ptn == 0) {
Shashank Mittal85b91f62010-10-30 10:12:38 -07001153 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
1154 return -1;
1155 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001156 }
Channagoud Kadabief0547c2015-02-10 12:57:38 -08001157 /* Set Lun for boot & recovery partitions */
1158 mmc_set_lun(partition_get_lun(index));
Shashank Mittal23b8f422010-04-16 19:27:21 -07001159
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301160 if (mmc_read(ptn + offset, (uint32_t *) buf, page_size)) {
Shashank Mittal23b8f422010-04-16 19:27:21 -07001161 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
1162 return -1;
1163 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07001164
1165 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001166 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Shashank Mittal23b8f422010-04-16 19:27:21 -07001167 return -1;
1168 }
1169
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07001170 if (hdr->page_size && (hdr->page_size != page_size)) {
vijay kumar2e21b3a2014-06-26 17:40:15 +05301171
1172 if (hdr->page_size > BOOT_IMG_MAX_PAGE_SIZE) {
1173 dprintf(CRITICAL, "ERROR: Invalid page size\n");
1174 return -1;
1175 }
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07001176 page_size = hdr->page_size;
1177 page_mask = page_size - 1;
Shashank Mittal23b8f422010-04-16 19:27:21 -07001178 }
1179
vijay kumar287bb542015-09-29 13:01:52 +05301180 /* ensure commandline is terminated */
1181 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
1182
Matthew Qin49e51fa2015-02-09 10:40:45 +08001183 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1184 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001185
Matthew Qin49e51fa2015-02-09 10:40:45 +08001186 image_addr = (unsigned char *)target_get_scratch_address();
1187
1188#if DEVICE_TREE
Parth Dixit4097b622016-03-15 14:42:27 +05301189#ifndef OSVERSION_IN_BOOTIMAGE
1190 dt_size = hdr->dt_size;
1191#endif
1192 dt_actual = ROUND_TO_PAGE(dt_size, page_mask);
Vijay Kumar Pendoti208f9622016-06-09 19:34:01 +05301193 if (UINT_MAX < ((uint64_t)kernel_actual + (uint64_t)ramdisk_actual+ (uint64_t)dt_actual + page_size)) {
1194 dprintf(CRITICAL, "Integer overflow detected in bootimage header fields at %u in %s\n",__LINE__,__FILE__);
1195 return -1;
1196 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08001197 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + dt_actual);
1198#else
Vijay Kumar Pendoti208f9622016-06-09 19:34:01 +05301199 if (UINT_MAX < ((uint64_t)kernel_actual + (uint64_t)ramdisk_actual + page_size)) {
1200 dprintf(CRITICAL, "Integer overflow detected in bootimage header fields at %u in %s\n",__LINE__,__FILE__);
1201 return -1;
1202 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08001203 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
1204#endif
1205
1206#if VERIFIED_BOOT
1207 boot_verifier_init();
1208#endif
1209
Vijay Kumar Pendoti9c002ad2016-03-09 13:52:45 +05301210 if (check_aboot_addr_range_overlap((uintptr_t) image_addr, imagesize_actual))
Matthew Qin49e51fa2015-02-09 10:40:45 +08001211 {
1212 dprintf(CRITICAL, "Boot image buffer address overlaps with aboot addresses.\n");
1213 return -1;
1214 }
1215
Matthew Qinbb7923d2015-02-09 10:56:09 +08001216 /*
1217 * Update loading flow of bootimage to support compressed/uncompressed
1218 * bootimage on both 64bit and 32bit platform.
1219 * 1. Load bootimage from emmc partition onto DDR.
1220 * 2. Check if bootimage is gzip format. If yes, decompress compressed kernel
1221 * 3. Check kernel header and update kernel load addr for 64bit and 32bit
1222 * platform accordingly.
1223 * 4. Sanity Check on kernel_addr and ramdisk_addr and copy data.
1224 */
1225
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001226 dprintf(INFO, "Loading (%s) image (%d): start\n",
1227 (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001228 bs_set_timestamp(BS_KERNEL_LOAD_START);
1229
Gaurav Nebhwani43e2a462016-03-17 21:32:56 +05301230 if ((target_get_max_flash_size() - page_size) < imagesize_actual)
1231 {
1232 dprintf(CRITICAL, "booimage size is greater than DDR can hold\n");
1233 return -1;
1234 }
1235
Matthew Qinbb7923d2015-02-09 10:56:09 +08001236 /* Read image without signature */
Matthew Qin49e51fa2015-02-09 10:40:45 +08001237 if (mmc_read(ptn + offset, (void *)image_addr, imagesize_actual))
1238 {
1239 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
1240 return -1;
1241 }
1242
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001243 dprintf(INFO, "Loading (%s) image (%d): done\n",
1244 (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual);
1245
Matthew Qin49e51fa2015-02-09 10:40:45 +08001246 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
1247
1248 /* Authenticate Kernel */
1249 dprintf(INFO, "use_signed_kernel=%d, is_unlocked=%d, is_tampered=%d.\n",
1250 (int) target_use_signed_kernel(),
1251 device.is_unlocked,
1252 device.is_tampered);
1253
Channagoud Kadabi736c4962015-08-21 11:56:52 -07001254 /* Change the condition a little bit to include the test framework support.
1255 * We would never reach this point if device is in fastboot mode, even if we did
1256 * that means we are in test mode, so execute kernel authentication part for the
1257 * tests */
Parth Dixitcb0c6082015-12-30 15:01:13 +05301258 if((target_use_signed_kernel() && (!device.is_unlocked)) || is_test_mode_enabled())
Matthew Qin49e51fa2015-02-09 10:40:45 +08001259 {
1260 offset = imagesize_actual;
Vijay Kumar Pendoti9c002ad2016-03-09 13:52:45 +05301261 if (check_aboot_addr_range_overlap((uintptr_t)image_addr + offset, page_size))
Matthew Qin49e51fa2015-02-09 10:40:45 +08001262 {
1263 dprintf(CRITICAL, "Signature read buffer address overlaps with aboot addresses.\n");
1264 return -1;
1265 }
1266
1267 /* Read signature */
1268 if(mmc_read(ptn + offset, (void *)(image_addr + offset), page_size))
1269 {
1270 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
1271 return -1;
1272 }
1273
1274 verify_signed_bootimg((uint32_t)image_addr, imagesize_actual);
Channagoud Kadabi736c4962015-08-21 11:56:52 -07001275 /* The purpose of our test is done here */
Parth Dixitcb0c6082015-12-30 15:01:13 +05301276 if(is_test_mode_enabled() && auth_kernel_img)
Channagoud Kadabi736c4962015-08-21 11:56:52 -07001277 return 0;
Matthew Qin49e51fa2015-02-09 10:40:45 +08001278 } else {
1279 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
1280 #ifdef TZ_SAVE_KERNEL_HASH
1281 aboot_save_boot_hash_mmc((uint32_t) image_addr, imagesize_actual);
1282 #endif /* TZ_SAVE_KERNEL_HASH */
Amit Blay4aa292f2015-04-28 21:55:59 +03001283
1284#ifdef MDTP_SUPPORT
1285 {
1286 /* Verify MDTP lock.
1287 * For boot & recovery partitions, MDTP will use boot_verifier APIs,
1288 * since verification was skipped in aboot. The signature is not part of the loaded image.
1289 */
1290 mdtp_ext_partition_verification_t ext_partition;
1291 ext_partition.partition = boot_into_recovery ? MDTP_PARTITION_RECOVERY : MDTP_PARTITION_BOOT;
1292 ext_partition.integrity_state = MDTP_PARTITION_STATE_UNSET;
1293 ext_partition.page_size = page_size;
1294 ext_partition.image_addr = (uint32)image_addr;
1295 ext_partition.image_size = imagesize_actual;
1296 ext_partition.sig_avail = FALSE;
1297 mdtp_fwlock_verify_lock(&ext_partition);
1298 }
1299#endif /* MDTP_SUPPORT */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001300 }
1301
Sridhar Parasuram00bfedb2015-05-26 14:21:27 -07001302#if VERIFIED_BOOT
taozhang93088bd2016-11-16 15:50:46 +08001303 if((boot_verify_get_state() == ORANGE) && (!boot_into_ffbm))
Reut Zysmanba8a9d52016-02-18 11:44:04 +02001304 {
1305#if FBCON_DISPLAY_MSG
1306 display_bootverify_menu(DISPLAY_MENU_ORANGE);
1307 wait_for_users_action();
1308#else
1309 dprintf(CRITICAL,
1310 "Your device has been unlocked and can't be trusted.\nWait for 5 seconds before proceeding\n");
1311 mdelay(5000);
1312#endif
1313 }
1314#endif
1315
1316#if VERIFIED_BOOT
Parth Dixitbc9b6492015-10-18 00:41:38 +05301317#if !VBOOT_MOTA
Sridhar Parasuram00bfedb2015-05-26 14:21:27 -07001318 // send root of trust
Sridhar Parasuram96300dc2015-06-11 10:37:11 -07001319 if(!send_rot_command((uint32_t)device.is_unlocked))
Sridhar Parasuram00bfedb2015-05-26 14:21:27 -07001320 ASSERT(0);
1321#endif
Parth Dixitbc9b6492015-10-18 00:41:38 +05301322#endif
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001323 /*
Matthew Qinbb7923d2015-02-09 10:56:09 +08001324 * Check if the kernel image is a gzip package. If yes, need to decompress it.
1325 * If not, continue booting.
1326 */
1327 if (is_gzip_package((unsigned char *)(image_addr + page_size), hdr->kernel_size))
1328 {
1329 out_addr = (unsigned char *)(image_addr + imagesize_actual + page_size);
1330 out_avai_len = target_get_max_flash_size() - imagesize_actual - page_size;
Matthew Qin0b15b322015-05-19 05:20:54 -04001331 dprintf(INFO, "decompressing kernel image: start\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001332 rc = decompress((unsigned char *)(image_addr + page_size),
1333 hdr->kernel_size, out_addr, out_avai_len,
1334 &dtb_offset, &out_len);
1335 if (rc)
1336 {
Matthew Qin0b15b322015-05-19 05:20:54 -04001337 dprintf(CRITICAL, "decompressing kernel image failed!!!\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001338 ASSERT(0);
1339 }
1340
Matthew Qin0b15b322015-05-19 05:20:54 -04001341 dprintf(INFO, "decompressing kernel image: done\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08001342 kptr = (struct kernel64_hdr *)out_addr;
1343 kernel_start_addr = out_addr;
1344 kernel_size = out_len;
1345 } else {
Ameya Thakur10a33452016-06-13 14:24:26 -07001346 dprintf(INFO, "Uncpmpressed kernel in use\n");
1347 if (!strncmp((char*)(image_addr + page_size),
1348 PATCHED_KERNEL_MAGIC,
1349 sizeof(PATCHED_KERNEL_MAGIC) - 1)) {
1350 dprintf(INFO, "Patched kernel detected\n");
1351 kptr = (struct kernel64_hdr *)(image_addr + page_size +
1352 PATCHED_KERNEL_HEADER_SIZE);
1353 //The size of the kernel is stored at start of kernel image + 16
1354 //The dtb would start just after the kernel
1355 dtb_offset = *((uint32_t*)((unsigned char*)
1356 (image_addr + page_size +
1357 sizeof(PATCHED_KERNEL_MAGIC) -
1358 1)));
1359 //The actual kernel starts after the 20 byte header.
1360 kernel_start_addr = (unsigned char*)(image_addr +
1361 page_size + PATCHED_KERNEL_HEADER_SIZE);
1362 kernel_size = hdr->kernel_size;
1363 patched_kernel_hdr_size = PATCHED_KERNEL_HEADER_SIZE;
1364 } else {
1365 dprintf(INFO, "Kernel image not patched..Unable to locate dt offset\n");
1366 kptr = (struct kernel64_hdr *)(image_addr + page_size);
1367 kernel_start_addr = (unsigned char *)(image_addr + page_size);
1368 kernel_size = hdr->kernel_size;
1369 }
Matthew Qinbb7923d2015-02-09 10:56:09 +08001370 }
1371
1372 /*
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001373 * Update the kernel/ramdisk/tags address if the boot image header
1374 * has default values, these default values come from mkbootimg when
1375 * the boot image is flashed using fastboot flash:raw
1376 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001377 update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr));
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001378
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001379 /* Get virtual addresses since the hdr saves physical addresses. */
1380 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
1381 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
1382 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001383
Matthew Qinbb7923d2015-02-09 10:56:09 +08001384 kernel_size = ROUND_TO_PAGE(kernel_size, page_mask);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001385 /* Check if the addresses in the header are valid. */
Matthew Qinbb7923d2015-02-09 10:56:09 +08001386 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_size) ||
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001387 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
1388 {
1389 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
1390 return -1;
1391 }
1392
1393#ifndef DEVICE_TREE
1394 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1395 {
1396 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
1397 return -1;
1398 }
1399#endif
1400
Matthew Qin49e51fa2015-02-09 10:40:45 +08001401 /* Move kernel, ramdisk and device tree to correct address */
Matthew Qinbb7923d2015-02-09 10:56:09 +08001402 memmove((void*) hdr->kernel_addr, kernel_start_addr, kernel_size);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001403 memmove((void*) hdr->ramdisk_addr, (char *)(image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Shashank Mittald3e54dd2014-08-28 15:24:02 -07001404
Matthew Qin49e51fa2015-02-09 10:40:45 +08001405 #if DEVICE_TREE
Parth Dixit4097b622016-03-15 14:42:27 +05301406 if(dt_size) {
Matthew Qin49e51fa2015-02-09 10:40:45 +08001407 dt_table_offset = ((uint32_t)image_addr + page_size + kernel_actual + ramdisk_actual + second_actual);
1408 table = (struct dt_table*) dt_table_offset;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001409
Matthew Qin49e51fa2015-02-09 10:40:45 +08001410 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
1411 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1412 return -1;
1413 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001414
P.V. Phani Kumar82916762016-03-09 09:20:19 +05301415 /* Its Error if, dt_hdr_size (table->num_entries * dt_entry size + Dev_Tree Header)
1416 goes beyound hdr->dt_size*/
Parth Dixit4097b622016-03-15 14:42:27 +05301417 if (dt_hdr_size > ROUND_TO_PAGE(dt_size,hdr->page_size)) {
P.V. Phani Kumar82916762016-03-09 09:20:19 +05301418 dprintf(CRITICAL, "ERROR: Invalid Device Tree size \n");
1419 return -1;
1420 }
1421
Matthew Qin49e51fa2015-02-09 10:40:45 +08001422 /* Find index of device tree within device tree table */
1423 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
1424 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1425 return -1;
1426 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001427
P.V. Phani Kumar3c333202016-03-09 11:54:37 +05301428 if(dt_entry.offset > (UINT_MAX - dt_entry.size)) {
1429 dprintf(CRITICAL, "ERROR: Device tree contents are Invalid\n");
1430 return -1;
1431 }
1432
1433 /* Ensure we are not overshooting dt_size with the dt_entry selected */
Parth Dixit4097b622016-03-15 14:42:27 +05301434 if ((dt_entry.offset + dt_entry.size) > dt_size) {
P.V. Phani Kumar3c333202016-03-09 11:54:37 +05301435 dprintf(CRITICAL, "ERROR: Device tree contents are Invalid\n");
1436 return -1;
1437 }
1438
Matthew Qin271927e2015-03-31 22:07:07 -04001439 if (is_gzip_package((unsigned char *)dt_table_offset + dt_entry.offset, dt_entry.size))
1440 {
1441 unsigned int compressed_size = 0;
1442 out_addr += out_len;
1443 out_avai_len -= out_len;
Matthew Qin0b15b322015-05-19 05:20:54 -04001444 dprintf(INFO, "decompressing dtb: start\n");
Matthew Qin271927e2015-03-31 22:07:07 -04001445 rc = decompress((unsigned char *)dt_table_offset + dt_entry.offset,
1446 dt_entry.size, out_addr, out_avai_len,
1447 &compressed_size, &dtb_size);
1448 if (rc)
1449 {
Matthew Qin0b15b322015-05-19 05:20:54 -04001450 dprintf(CRITICAL, "decompressing dtb failed!!!\n");
Matthew Qin271927e2015-03-31 22:07:07 -04001451 ASSERT(0);
1452 }
1453
Matthew Qin0b15b322015-05-19 05:20:54 -04001454 dprintf(INFO, "decompressing dtb: done\n");
Matthew Qin271927e2015-03-31 22:07:07 -04001455 best_match_dt_addr = out_addr;
1456 } else {
1457 best_match_dt_addr = (unsigned char *)dt_table_offset + dt_entry.offset;
1458 dtb_size = dt_entry.size;
1459 }
1460
Matthew Qin49e51fa2015-02-09 10:40:45 +08001461 /* Validate and Read device device tree in the tags_addr */
Matthew Qin271927e2015-03-31 22:07:07 -04001462 if (check_aboot_addr_range_overlap(hdr->tags_addr, dtb_size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001463 {
1464 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1465 return -1;
1466 }
Shashank Mittal162244e2011-08-08 19:01:25 -07001467
Matthew Qin271927e2015-03-31 22:07:07 -04001468 memmove((void *)hdr->tags_addr, (char *)best_match_dt_addr, dtb_size);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001469 } else {
1470 /* Validate the tags_addr */
1471 if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual))
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001472 {
1473 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1474 return -1;
1475 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08001476 /*
1477 * If appended dev tree is found, update the atags with
1478 * memory address to the DTB appended location on RAM.
1479 * Else update with the atags address in the kernel header
1480 */
1481 void *dtb;
Ameya Thakur10a33452016-06-13 14:24:26 -07001482 dtb = dev_tree_appended(
1483 (void*)(image_addr + page_size +
1484 patched_kernel_hdr_size),
1485 hdr->kernel_size, dtb_offset,
1486 (void *)hdr->tags_addr);
Matthew Qin49e51fa2015-02-09 10:40:45 +08001487 if (!dtb) {
1488 dprintf(CRITICAL, "ERROR: Appended Device Tree Blob not found\n");
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07001489 return -1;
1490 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07001491 }
Matthew Qin49e51fa2015-02-09 10:40:45 +08001492 #endif
Shashank Mittal23b8f422010-04-16 19:27:21 -07001493
Stanimir Varbanov69ec5462013-07-18 18:17:42 +03001494 if (boot_into_recovery && !device.is_unlocked && !device.is_tampered)
1495 target_load_ssd_keystore();
1496
Shashank Mittal23b8f422010-04-16 19:27:21 -07001497unified_boot:
Shashank Mittal23b8f422010-04-16 19:27:21 -07001498
Dima Zavin77e41f32013-03-06 16:10:43 -08001499 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07001500 (const char *)hdr->cmdline, board_machtype(),
Shashank Mittal23b8f422010-04-16 19:27:21 -07001501 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
1502
1503 return 0;
1504}
1505
Dima Zavin214cc642009-01-26 11:16:21 -08001506int boot_linux_from_flash(void)
1507{
1508 struct boot_img_hdr *hdr = (void*) buf;
Dima Zavin214cc642009-01-26 11:16:21 -08001509 struct ptentry *ptn;
1510 struct ptable *ptable;
1511 unsigned offset = 0;
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001512
Shashank Mittalcd98d472011-08-02 14:29:24 -07001513 unsigned char *image_addr = 0;
1514 unsigned kernel_actual;
1515 unsigned ramdisk_actual;
1516 unsigned imagesize_actual;
vijay kumar8f53e362015-11-24 13:38:11 +05301517 unsigned second_actual = 0;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001518
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001519#if DEVICE_TREE
1520 struct dt_table *table;
Joel Kingaa335dc2013-06-03 16:11:08 -07001521 struct dt_entry dt_entry;
vijay kumar8f53e362015-11-24 13:38:11 +05301522 unsigned dt_table_offset;
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001523 uint32_t dt_actual;
Deepa Dinamani19648b42013-09-05 17:05:55 -07001524 uint32_t dt_hdr_size;
vijay kumar8f53e362015-11-24 13:38:11 +05301525 unsigned int dtb_size = 0;
1526 unsigned char *best_match_dt_addr = NULL;
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001527#endif
1528
David Ng183a7422009-12-07 14:55:21 -08001529 if (target_is_emmc_boot()) {
1530 hdr = (struct boot_img_hdr *)EMMC_BOOT_IMG_HEADER_ADDR;
1531 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
1532 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
1533 return -1;
1534 }
1535 goto continue_boot;
1536 }
1537
Dima Zavin214cc642009-01-26 11:16:21 -08001538 ptable = flash_get_ptable();
1539 if (ptable == NULL) {
1540 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1541 return -1;
1542 }
1543
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001544 if(!boot_into_recovery)
1545 {
1546 ptn = ptable_find(ptable, "boot");
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001547
Chandan Uddarajude85d3f2010-01-05 16:32:33 -08001548 if (ptn == NULL) {
1549 dprintf(CRITICAL, "ERROR: No boot partition found\n");
1550 return -1;
1551 }
1552 }
1553 else
1554 {
1555 ptn = ptable_find(ptable, "recovery");
1556 if (ptn == NULL) {
1557 dprintf(CRITICAL, "ERROR: No recovery partition found\n");
1558 return -1;
1559 }
Dima Zavin214cc642009-01-26 11:16:21 -08001560 }
1561
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001562 if (flash_read(ptn, offset, buf, page_size)) {
Dima Zavin214cc642009-01-26 11:16:21 -08001563 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
1564 return -1;
1565 }
Dima Zavin214cc642009-01-26 11:16:21 -08001566
1567 if (memcmp(hdr->magic, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001568 dprintf(CRITICAL, "ERROR: Invalid boot image header\n");
Dima Zavin214cc642009-01-26 11:16:21 -08001569 return -1;
1570 }
1571
Shashank Mittaldcc2e352009-11-19 19:11:16 -08001572 if (hdr->page_size != page_size) {
Kinson Chik kchik@codeaurora.org82e4ae62011-04-12 17:42:07 -07001573 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 -08001574 return -1;
1575 }
1576
vijay kumar287bb542015-09-29 13:01:52 +05301577 /* ensure commandline is terminated */
1578 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
1579
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001580 /*
1581 * Update the kernel/ramdisk/tags address if the boot image header
1582 * has default values, these default values come from mkbootimg when
1583 * the boot image is flashed using fastboot flash:raw
1584 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08001585 update_ker_tags_rdisk_addr(hdr, false);
Channagoud Kadabia22144f2013-03-20 11:49:01 -07001586
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001587 /* Get virtual addresses since the hdr saves physical addresses. */
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001588 hdr->kernel_addr = VA((addr_t)(hdr->kernel_addr));
1589 hdr->ramdisk_addr = VA((addr_t)(hdr->ramdisk_addr));
1590 hdr->tags_addr = VA((addr_t)(hdr->tags_addr));
1591
1592 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1593 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1594
1595 /* Check if the addresses in the header are valid. */
1596 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_actual) ||
1597 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
1598 {
1599 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
1600 return -1;
1601 }
1602
1603#ifndef DEVICE_TREE
Vijay Kumar Pendotid3ed20e2016-09-20 00:34:46 +05301604 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
1605 {
1606 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
1607 return -1;
1608 }
1609#else
1610
1611#ifndef OSVERSION_IN_BOOTIMAGE
1612 dt_size = hdr->dt_size;
1613#endif
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001614#endif
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07001615
Shashank Mittalcd98d472011-08-02 14:29:24 -07001616 /* Authenticate Kernel */
Deepa Dinamani23b60d42013-06-24 18:10:52 -07001617 if(target_use_signed_kernel() && (!device.is_unlocked))
Shashank Mittalcd98d472011-08-02 14:29:24 -07001618 {
1619 image_addr = (unsigned char *)target_get_scratch_address();
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001620 offset = 0;
1621
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001622#if DEVICE_TREE
Parth Dixit4097b622016-03-15 14:42:27 +05301623 dt_actual = ROUND_TO_PAGE(dt_size, page_mask);
vijay kumar966a9822015-12-09 18:36:09 +05301624 if (UINT_MAX < ((uint64_t)kernel_actual + (uint64_t)ramdisk_actual+ (uint64_t)dt_actual + page_size)) {
1625 dprintf(CRITICAL, "Integer overflow detected in bootimage header fields\n");
1626 return -1;
1627 }
1628
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001629 imagesize_actual = (page_size + kernel_actual + ramdisk_actual + dt_actual);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001630
Parth Dixit4097b622016-03-15 14:42:27 +05301631 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001632 {
1633 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1634 return -1;
1635 }
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001636#else
vijay kumar966a9822015-12-09 18:36:09 +05301637 if (UINT_MAX < ((uint64_t)kernel_actual + (uint64_t)ramdisk_actual+ page_size)) {
1638 dprintf(CRITICAL, "Integer overflow detected in bootimage header fields\n");
1639 return -1;
1640 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07001641 imagesize_actual = (page_size + kernel_actual + ramdisk_actual);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001642#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001643
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001644 dprintf(INFO, "Loading (%s) image (%d): start\n",
1645 (!boot_into_recovery ? "boot" : "recovery"),imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001646 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -08001647
Vijay Kumar Pendotib3458362016-07-05 13:45:54 +05301648 if (UINT_MAX - page_size < imagesize_actual)
1649 {
1650 dprintf(CRITICAL,"Integer overflow detected in bootimage header fields %u %s\n", __LINE__,__func__);
1651 return -1;
1652 }
1653
1654 /*Check the availability of RAM before reading boot image + max signature length from flash*/
1655 if (target_get_max_flash_size() < (imagesize_actual + page_size))
1656 {
1657 dprintf(CRITICAL, "bootimage size is greater than DDR can hold\n");
1658 return -1;
1659 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07001660 /* Read image without signature */
1661 if (flash_read(ptn, offset, (void *)image_addr, imagesize_actual))
1662 {
1663 dprintf(CRITICAL, "ERROR: Cannot read boot image\n");
1664 return -1;
1665 }
Dima Zavin214cc642009-01-26 11:16:21 -08001666
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001667 dprintf(INFO, "Loading (%s) image (%d): done\n",
1668 (!boot_into_recovery ? "boot" : "recovery"), imagesize_actual);
Amol Jadi492d5a52013-03-15 16:12:34 -07001669 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Amol Jadib6be5c12012-11-14 13:39:51 -08001670
Shashank Mittalcd98d472011-08-02 14:29:24 -07001671 offset = imagesize_actual;
1672 /* Read signature */
1673 if (flash_read(ptn, offset, (void *)(image_addr + offset), page_size))
1674 {
1675 dprintf(CRITICAL, "ERROR: Cannot read boot image signature\n");
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001676 return -1;
Shashank Mittalcd98d472011-08-02 14:29:24 -07001677 }
Shashank Mittalcd98d472011-08-02 14:29:24 -07001678
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05301679 verify_signed_bootimg((uint32_t)image_addr, imagesize_actual);
Shashank Mittalcd98d472011-08-02 14:29:24 -07001680
1681 /* Move kernel and ramdisk to correct address */
Matthew Qinbb7923d2015-02-09 10:56:09 +08001682 memmove((void*) hdr->kernel_addr, (char*) (image_addr + page_size), hdr->kernel_size);
1683 memmove((void*) hdr->ramdisk_addr, (char*) (image_addr + page_size + kernel_actual), hdr->ramdisk_size);
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001684#if DEVICE_TREE
Parth Dixit4097b622016-03-15 14:42:27 +05301685 if(dt_size != 0) {
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001686
vijay kumar8f53e362015-11-24 13:38:11 +05301687 dt_table_offset = ((uint32_t)image_addr + page_size + kernel_actual + ramdisk_actual + second_actual);
1688
1689 table = (struct dt_table*) dt_table_offset;
1690
1691 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0){
1692 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1693 return -1;
1694 }
1695
1696 /* Find index of device tree within device tree table */
1697 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
1698 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1699 return -1;
1700 }
1701
1702 /* Validate and Read device device tree in the "tags_add */
1703 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size)){
1704 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1705 return -1;
1706 }
1707
Mayank Groverebf85ae2017-01-25 13:35:37 +05301708 if(dt_entry.offset > (UINT_MAX - dt_entry.size)) {
1709 dprintf(CRITICAL, "ERROR: Device tree contents are Invalid\n");
1710 return -1;
1711 }
1712
1713 /* Ensure we are not overshooting dt_size with the dt_entry selected */
1714 if ((dt_entry.offset + dt_entry.size) > dt_size) {
1715 dprintf(CRITICAL, "ERROR: Device tree contents are Invalid\n");
1716 return -1;
1717 }
1718
vijay kumar8f53e362015-11-24 13:38:11 +05301719 best_match_dt_addr = (unsigned char *)table + dt_entry.offset;
1720 dtb_size = dt_entry.size;
1721 memmove((void *)hdr->tags_addr, (char *)best_match_dt_addr, dtb_size);
1722 }
Deepa Dinamani7aaf83d2012-12-21 11:27:01 -08001723#endif
Shashank Mittal162244e2011-08-08 19:01:25 -07001724
1725 /* Make sure everything from scratch address is read before next step!*/
Shashank Mittala0032282011-08-26 14:50:11 -07001726 if(device.is_tampered)
Shashank Mittal162244e2011-08-08 19:01:25 -07001727 {
1728 write_device_info_flash(&device);
1729 }
Channagoud Kadabi5c86fe32012-02-16 10:58:48 +05301730#if USE_PCOM_SECBOOT
1731 set_tamper_flag(device.is_tampered);
1732#endif
Shashank Mittalcd98d472011-08-02 14:29:24 -07001733 }
1734 else
1735 {
Shashank Mittal162244e2011-08-08 19:01:25 -07001736 offset = page_size;
1737
Amol Jadib6be5c12012-11-14 13:39:51 -08001738 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
1739 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
1740 second_actual = ROUND_TO_PAGE(hdr->second_size, page_mask);
1741
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001742 dprintf(INFO, "Loading (%s) image (%d): start\n",
1743 (!boot_into_recovery ? "boot" : "recovery"), kernel_actual + ramdisk_actual);
1744
Amol Jadi492d5a52013-03-15 16:12:34 -07001745 bs_set_timestamp(BS_KERNEL_LOAD_START);
Amol Jadib6be5c12012-11-14 13:39:51 -08001746
Vijay Kumar Pendoti47bc1282016-05-05 15:46:17 +05301747 if (UINT_MAX - offset < kernel_actual)
1748 {
1749 dprintf(CRITICAL, "ERROR: Integer overflow in boot image header %s\t%d\n",__func__,__LINE__);
1750 return -1;
1751 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001752 if (flash_read(ptn, offset, (void *)hdr->kernel_addr, kernel_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -07001753 dprintf(CRITICAL, "ERROR: Cannot read kernel image\n");
1754 return -1;
1755 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001756 offset += kernel_actual;
Vijay Kumar Pendoti47bc1282016-05-05 15:46:17 +05301757 if (UINT_MAX - offset < ramdisk_actual)
1758 {
1759 dprintf(CRITICAL, "ERROR: Integer overflow in boot image header %s\t%d\n",__func__,__LINE__);
1760 return -1;
1761 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001762 if (flash_read(ptn, offset, (void *)hdr->ramdisk_addr, ramdisk_actual)) {
Shashank Mittalcd98d472011-08-02 14:29:24 -07001763 dprintf(CRITICAL, "ERROR: Cannot read ramdisk image\n");
1764 return -1;
1765 }
Vijay Kumar Pendoti47bc1282016-05-05 15:46:17 +05301766
Amol Jadib6be5c12012-11-14 13:39:51 -08001767 offset += ramdisk_actual;
1768
Channagoud Kadabif0705b52015-08-20 14:16:08 -07001769 dprintf(INFO, "Loading (%s) image (%d): done\n",
1770 (!boot_into_recovery ? "boot" : "recovery"), kernel_actual + ramdisk_actual);
1771
Amol Jadi492d5a52013-03-15 16:12:34 -07001772 bs_set_timestamp(BS_KERNEL_LOAD_DONE);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001773
1774 if(hdr->second_size != 0) {
Vijay Kumar Pendoti47bc1282016-05-05 15:46:17 +05301775 if (UINT_MAX - offset < second_actual)
1776 {
1777 dprintf(CRITICAL, "ERROR: Integer overflow in boot image header %s\t%d\n",__func__,__LINE__);
1778 return -1;
1779 }
Amol Jadib6be5c12012-11-14 13:39:51 -08001780 offset += second_actual;
1781 /* Second image loading not implemented. */
1782 ASSERT(0);
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001783 }
1784
1785#if DEVICE_TREE
Parth Dixit4097b622016-03-15 14:42:27 +05301786 if(dt_size != 0) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001787
1788 /* Read the device tree table into buffer */
1789 if(flash_read(ptn, offset, (void *) dt_buf, page_size)) {
1790 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
1791 return -1;
1792 }
1793
1794 table = (struct dt_table*) dt_buf;
1795
Deepa Dinamani19648b42013-09-05 17:05:55 -07001796 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001797 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
1798 return -1;
1799 }
1800
P.V. Phani Kumar82916762016-03-09 09:20:19 +05301801 /* Its Error if, dt_hdr_size (table->num_entries * dt_entry size + Dev_Tree Header)
1802 goes beyound hdr->dt_size*/
Parth Dixit4097b622016-03-15 14:42:27 +05301803 if (dt_hdr_size > ROUND_TO_PAGE(dt_size,hdr->page_size)) {
P.V. Phani Kumar82916762016-03-09 09:20:19 +05301804 dprintf(CRITICAL, "ERROR: Invalid Device Tree size \n");
1805 return -1;
1806 }
1807
Deepa Dinamani19648b42013-09-05 17:05:55 -07001808 table = (struct dt_table*) memalign(CACHE_LINE, dt_hdr_size);
1809 if (!table)
1810 return -1;
1811
1812 /* Read the entire device tree table into buffer */
1813 if(flash_read(ptn, offset, (void *)table, dt_hdr_size)) {
1814 dprintf(CRITICAL, "ERROR: Cannot read the Device Tree Table\n");
1815 return -1;
1816 }
1817
1818
Joel Kingaa335dc2013-06-03 16:11:08 -07001819 /* Find index of device tree within device tree table */
1820 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001821 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
1822 return -1;
1823 }
1824
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001825 /* Validate and Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001826 if (check_aboot_addr_range_overlap(hdr->tags_addr, dt_entry.size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07001827 {
1828 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
1829 return -1;
1830 }
1831
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001832 /* Read device device tree in the "tags_add */
Joel Kingaa335dc2013-06-03 16:11:08 -07001833 if(flash_read(ptn, offset + dt_entry.offset,
1834 (void *)hdr->tags_addr, dt_entry.size)) {
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07001835 dprintf(CRITICAL, "ERROR: Cannot read device tree\n");
1836 return -1;
1837 }
1838 }
1839#endif
1840
Shashank Mittalcd98d472011-08-02 14:29:24 -07001841 }
David Ng183a7422009-12-07 14:55:21 -08001842continue_boot:
Dima Zavin214cc642009-01-26 11:16:21 -08001843
Dima Zavin214cc642009-01-26 11:16:21 -08001844 /* TODO: create/pass atags to kernel */
1845
Ajay Dudanie28a6072011-07-01 13:59:46 -07001846 boot_linux((void *)hdr->kernel_addr, (void *)hdr->tags_addr,
Dima Zavinbd3daa02013-03-26 11:06:44 -07001847 (const char *)hdr->cmdline, board_machtype(),
Dima Zavin214cc642009-01-26 11:16:21 -08001848 (void *)hdr->ramdisk_addr, hdr->ramdisk_size);
1849
1850 return 0;
1851}
Brian Swetland9c4c0752009-01-25 16:23:50 -08001852
Shashank Mittal162244e2011-08-08 19:01:25 -07001853void write_device_info_mmc(device_info *dev)
1854{
Shashank Mittal162244e2011-08-08 19:01:25 -07001855 unsigned long long ptn = 0;
1856 unsigned long long size;
1857 int index = INVALID_PTN;
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001858 uint32_t blocksize;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001859 uint8_t lun = 0;
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001860 uint32_t ret = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001861
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001862 if (devinfo_present)
1863 index = partition_get_index("devinfo");
1864 else
1865 index = partition_get_index("aboot");
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001866
Shashank Mittal162244e2011-08-08 19:01:25 -07001867 ptn = partition_get_offset(index);
1868 if(ptn == 0)
1869 {
1870 return;
1871 }
1872
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08001873 lun = partition_get_lun(index);
1874 mmc_set_lun(lun);
1875
Shashank Mittal162244e2011-08-08 19:01:25 -07001876 size = partition_get_size(index);
1877
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001878 blocksize = mmc_get_device_blocksize();
1879
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001880 if (devinfo_present)
1881 ret = mmc_write(ptn, blocksize, (void *)info_buf);
1882 else
1883 ret = mmc_write((ptn + size - blocksize), blocksize, (void *)info_buf);
1884 if (ret)
Shashank Mittal162244e2011-08-08 19:01:25 -07001885 {
1886 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
Sridhar Parasurame1a97922015-07-27 15:31:22 -07001887 ASSERT(0);
Shashank Mittal162244e2011-08-08 19:01:25 -07001888 }
1889}
1890
Channagoud Kadabi036c6052015-02-09 15:19:59 -08001891void read_device_info_mmc(struct device_info *info)
Shashank Mittal162244e2011-08-08 19:01:25 -07001892{
Shashank Mittal162244e2011-08-08 19:01:25 -07001893 unsigned long long ptn = 0;
1894 unsigned long long size;
1895 int index = INVALID_PTN;
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001896 uint32_t blocksize;
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001897 uint32_t ret = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07001898
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001899 if ((index = partition_get_index("devinfo")) < 0)
1900 {
1901 devinfo_present = false;
1902 index = partition_get_index("aboot");
1903 }
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001904
Shashank Mittal162244e2011-08-08 19:01:25 -07001905 ptn = partition_get_offset(index);
1906 if(ptn == 0)
1907 {
1908 return;
1909 }
1910
Channagoud Kadabi5d0371c2014-10-21 22:27:07 -07001911 mmc_set_lun(partition_get_lun(index));
1912
Shashank Mittal162244e2011-08-08 19:01:25 -07001913 size = partition_get_size(index);
1914
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07001915 blocksize = mmc_get_device_blocksize();
1916
Channagoud Kadabi80a182b2015-03-11 17:04:23 -07001917 if (devinfo_present)
1918 ret = mmc_read(ptn, (void *)info_buf, blocksize);
1919 else
1920 ret = mmc_read((ptn + size - blocksize), (void *)info_buf, blocksize);
1921 if (ret)
Shashank Mittal162244e2011-08-08 19:01:25 -07001922 {
1923 dprintf(CRITICAL, "ERROR: Cannot read device info\n");
Sridhar Parasurame1a97922015-07-27 15:31:22 -07001924 ASSERT(0);
Shashank Mittal162244e2011-08-08 19:01:25 -07001925 }
Shashank Mittal162244e2011-08-08 19:01:25 -07001926}
1927
1928void write_device_info_flash(device_info *dev)
1929{
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001930 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
Shashank Mittal162244e2011-08-08 19:01:25 -07001931 struct ptentry *ptn;
1932 struct ptable *ptable;
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001933 if(info == NULL)
1934 {
1935 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
1936 ASSERT(0);
1937 }
1938 info_buf = info;
Shashank Mittal162244e2011-08-08 19:01:25 -07001939 ptable = flash_get_ptable();
1940 if (ptable == NULL)
1941 {
1942 dprintf(CRITICAL, "ERROR: Partition table not found\n");
1943 return;
1944 }
1945
1946 ptn = ptable_find(ptable, "devinfo");
1947 if (ptn == NULL)
1948 {
Smita Ghosh670c8b82015-05-07 09:30:03 -07001949 dprintf(CRITICAL, "ERROR: No devinfo partition found\n");
Shashank Mittal162244e2011-08-08 19:01:25 -07001950 return;
1951 }
1952
1953 memcpy(info, dev, sizeof(device_info));
1954
1955 if (flash_write(ptn, 0, (void *)info_buf, page_size))
1956 {
1957 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
1958 return;
1959 }
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07001960 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07001961}
1962
vijay kumarc65876c2015-04-24 13:29:16 +05301963static int read_allow_oem_unlock(device_info *dev)
1964{
vijay kumarc65876c2015-04-24 13:29:16 +05301965 unsigned offset;
1966 int index;
1967 unsigned long long ptn;
1968 unsigned long long ptn_size;
1969 unsigned blocksize = mmc_get_device_blocksize();
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07001970 STACKBUF_DMA_ALIGN(buf, blocksize);
vijay kumarc65876c2015-04-24 13:29:16 +05301971
vijay kumarca2e6812015-07-08 20:28:25 +05301972 index = partition_get_index(frp_ptns[0]);
vijay kumarc65876c2015-04-24 13:29:16 +05301973 if (index == INVALID_PTN)
1974 {
vijay kumarca2e6812015-07-08 20:28:25 +05301975 index = partition_get_index(frp_ptns[1]);
1976 if (index == INVALID_PTN)
1977 {
1978 dprintf(CRITICAL, "Neither '%s' nor '%s' partition found\n", frp_ptns[0],frp_ptns[1]);
1979 return -1;
1980 }
vijay kumarc65876c2015-04-24 13:29:16 +05301981 }
1982
1983 ptn = partition_get_offset(index);
1984 ptn_size = partition_get_size(index);
1985 offset = ptn_size - blocksize;
1986
Mayank Grover48860402016-11-29 12:34:53 +05301987 /* Set Lun for partition */
1988 mmc_set_lun(partition_get_lun(index));
1989
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07001990 if (mmc_read(ptn + offset, (void *)buf, blocksize))
vijay kumarc65876c2015-04-24 13:29:16 +05301991 {
1992 dprintf(CRITICAL, "Reading MMC failed\n");
1993 return -1;
1994 }
1995
1996 /*is_allow_unlock is a bool value stored at the LSB of last byte*/
1997 is_allow_unlock = buf[blocksize-1] & 0x01;
1998 return 0;
1999}
2000
2001static int write_allow_oem_unlock(bool allow_unlock)
2002{
vijay kumarc65876c2015-04-24 13:29:16 +05302003 unsigned offset;
vijay kumarc65876c2015-04-24 13:29:16 +05302004 int index;
2005 unsigned long long ptn;
2006 unsigned long long ptn_size;
2007 unsigned blocksize = mmc_get_device_blocksize();
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07002008 STACKBUF_DMA_ALIGN(buf, blocksize);
vijay kumarc65876c2015-04-24 13:29:16 +05302009
vijay kumarca2e6812015-07-08 20:28:25 +05302010 index = partition_get_index(frp_ptns[0]);
vijay kumarc65876c2015-04-24 13:29:16 +05302011 if (index == INVALID_PTN)
2012 {
vijay kumarca2e6812015-07-08 20:28:25 +05302013 index = partition_get_index(frp_ptns[1]);
2014 if (index == INVALID_PTN)
2015 {
2016 dprintf(CRITICAL, "Neither '%s' nor '%s' partition found\n", frp_ptns[0],frp_ptns[1]);
2017 return -1;
2018 }
vijay kumarc65876c2015-04-24 13:29:16 +05302019 }
2020
2021 ptn = partition_get_offset(index);
2022 ptn_size = partition_get_size(index);
2023 offset = ptn_size - blocksize;
Mayank Grover48860402016-11-29 12:34:53 +05302024 mmc_set_lun(partition_get_lun(index));
vijay kumarc65876c2015-04-24 13:29:16 +05302025
Channagoud Kadabib1fe4062015-08-07 16:08:44 -07002026 if (mmc_read(ptn + offset, (void *)buf, blocksize))
vijay kumarc65876c2015-04-24 13:29:16 +05302027 {
2028 dprintf(CRITICAL, "Reading MMC failed\n");
2029 return -1;
2030 }
2031
2032 /*is_allow_unlock is a bool value stored at the LSB of last byte*/
2033 buf[blocksize-1] = allow_unlock;
2034 if (mmc_write(ptn + offset, blocksize, buf))
2035 {
2036 dprintf(CRITICAL, "Writing MMC failed\n");
2037 return -1;
2038 }
2039
2040 return 0;
2041}
2042
Shashank Mittal162244e2011-08-08 19:01:25 -07002043void read_device_info_flash(device_info *dev)
2044{
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002045 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
Shashank Mittal162244e2011-08-08 19:01:25 -07002046 struct ptentry *ptn;
2047 struct ptable *ptable;
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002048 if(info == NULL)
2049 {
2050 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
2051 ASSERT(0);
2052 }
2053 info_buf = info;
Shashank Mittal162244e2011-08-08 19:01:25 -07002054 ptable = flash_get_ptable();
2055 if (ptable == NULL)
2056 {
2057 dprintf(CRITICAL, "ERROR: Partition table not found\n");
2058 return;
2059 }
2060
2061 ptn = ptable_find(ptable, "devinfo");
2062 if (ptn == NULL)
2063 {
Smita Ghosh670c8b82015-05-07 09:30:03 -07002064 dprintf(CRITICAL, "ERROR: No devinfo partition found\n");
Shashank Mittal162244e2011-08-08 19:01:25 -07002065 return;
2066 }
2067
2068 if (flash_read(ptn, 0, (void *)info_buf, page_size))
2069 {
2070 dprintf(CRITICAL, "ERROR: Cannot write device info\n");
2071 return;
2072 }
2073
2074 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
2075 {
Shashank Mittal162244e2011-08-08 19:01:25 -07002076 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
2077 info->is_unlocked = 0;
Shashank Mittala0032282011-08-26 14:50:11 -07002078 info->is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07002079 write_device_info_flash(info);
2080 }
2081 memcpy(dev, info, sizeof(device_info));
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002082 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07002083}
2084
2085void write_device_info(device_info *dev)
2086{
2087 if(target_is_emmc_boot())
2088 {
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002089 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
2090 if(info == NULL)
2091 {
2092 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
2093 ASSERT(0);
2094 }
2095 info_buf = info;
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002096 memcpy(info, dev, sizeof(struct device_info));
2097
2098#if USE_RPMB_FOR_DEVINFO
Sridhar Parasurame1a97922015-07-27 15:31:22 -07002099 if (is_secure_boot_enable()) {
2100 if((write_device_info_rpmb((void*) info, PAGE_SIZE)) < 0)
2101 ASSERT(0);
2102 }
Channagoud Kadabic80cb492015-04-28 16:08:28 -07002103 else
2104 write_device_info_mmc(info);
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002105#else
2106 write_device_info_mmc(info);
2107#endif
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002108 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07002109 }
2110 else
2111 {
2112 write_device_info_flash(dev);
2113 }
2114}
2115
2116void read_device_info(device_info *dev)
2117{
2118 if(target_is_emmc_boot())
2119 {
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002120 struct device_info *info = memalign(PAGE_SIZE, ROUNDUP(BOOT_IMG_MAX_PAGE_SIZE, PAGE_SIZE));
2121 if(info == NULL)
2122 {
2123 dprintf(CRITICAL, "Failed to allocate memory for device info struct\n");
2124 ASSERT(0);
2125 }
2126 info_buf = info;
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002127
2128#if USE_RPMB_FOR_DEVINFO
Sridhar Parasurame1a97922015-07-27 15:31:22 -07002129 if (is_secure_boot_enable()) {
2130 if((read_device_info_rpmb((void*) info, PAGE_SIZE)) < 0)
2131 ASSERT(0);
2132 }
Channagoud Kadabic80cb492015-04-28 16:08:28 -07002133 else
2134 read_device_info_mmc(info);
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002135#else
2136 read_device_info_mmc(info);
2137#endif
2138
2139 if (memcmp(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE))
2140 {
2141 memcpy(info->magic, DEVICE_MAGIC, DEVICE_MAGIC_SIZE);
lijuang511a2b52015-08-14 20:50:51 +08002142 if (is_secure_boot_enable()) {
Channagoud Kadabi05f78ba2015-07-06 11:58:14 -07002143 info->is_unlocked = 0;
Parth Dixitddbc7352015-10-18 03:13:31 +05302144#if !VBOOT_MOTA
lijuang511a2b52015-08-14 20:50:51 +08002145 info->is_unlock_critical = 0;
Parth Dixitddbc7352015-10-18 03:13:31 +05302146#endif
lijuang511a2b52015-08-14 20:50:51 +08002147 } else {
Channagoud Kadabi2fda4092015-07-07 13:34:11 -07002148 info->is_unlocked = 1;
Parth Dixitddbc7352015-10-18 03:13:31 +05302149#if !VBOOT_MOTA
lijuang511a2b52015-08-14 20:50:51 +08002150 info->is_unlock_critical = 1;
Parth Dixitddbc7352015-10-18 03:13:31 +05302151#endif
lijuang511a2b52015-08-14 20:50:51 +08002152 }
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002153 info->is_tampered = 0;
2154 info->charger_screen_enabled = 0;
Parth Dixitddbc7352015-10-18 03:13:31 +05302155#if !VBOOT_MOTA
Sridhar Parasuram287e5062015-08-24 16:17:22 -07002156 info->verity_mode = 1; //enforcing by default
Parth Dixitddbc7352015-10-18 03:13:31 +05302157#endif
Channagoud Kadabi036c6052015-02-09 15:19:59 -08002158 write_device_info(info);
2159 }
2160 memcpy(dev, info, sizeof(device_info));
Sridhar Parasuram7e16d172015-07-05 11:35:23 -07002161 free(info);
Shashank Mittal162244e2011-08-08 19:01:25 -07002162 }
2163 else
2164 {
2165 read_device_info_flash(dev);
2166 }
2167}
2168
2169void reset_device_info()
2170{
2171 dprintf(ALWAYS, "reset_device_info called.");
Shashank Mittala0032282011-08-26 14:50:11 -07002172 device.is_tampered = 0;
Shashank Mittal162244e2011-08-08 19:01:25 -07002173 write_device_info(&device);
2174}
2175
2176void set_device_root()
2177{
2178 dprintf(ALWAYS, "set_device_root called.");
Shashank Mittala0032282011-08-26 14:50:11 -07002179 device.is_tampered = 1;
Shashank Mittal162244e2011-08-08 19:01:25 -07002180 write_device_info(&device);
2181}
2182
lijuang4ece1e72015-08-14 21:02:36 +08002183/* set device unlock value
2184 * Must check FRP before call this function
2185 * Need to wipe data when unlock status changed
2186 * type 0: oem unlock
2187 * type 1: unlock critical
2188 * status 0: unlock as false
2189 * status 1: lock as true
2190 */
2191void set_device_unlock_value(int type, bool status)
lijuang21f12f52015-08-22 16:22:19 +08002192{
lijuang4ece1e72015-08-14 21:02:36 +08002193 if (type == UNLOCK)
2194 device.is_unlocked = status;
Parth Dixitddbc7352015-10-18 03:13:31 +05302195#if !VBOOT_MOTA
lijuang4ece1e72015-08-14 21:02:36 +08002196 else if (type == UNLOCK_CRITICAL)
2197 device.is_unlock_critical = status;
Parth Dixitddbc7352015-10-18 03:13:31 +05302198#endif
lijuang4ece1e72015-08-14 21:02:36 +08002199 write_device_info(&device);
2200}
2201
2202static void set_device_unlock(int type, bool status)
2203{
2204 int is_unlocked = -1;
2205 char response[MAX_RSP_SIZE];
2206
2207 /* check device unlock status if it is as expected */
2208 if (type == UNLOCK)
2209 is_unlocked = device.is_unlocked;
Parth Dixitddbc7352015-10-18 03:13:31 +05302210#if !VBOOT_MOTA
lijuang4ece1e72015-08-14 21:02:36 +08002211 else if (type == UNLOCK_CRITICAL)
2212 is_unlocked = device.is_unlock_critical;
Parth Dixitddbc7352015-10-18 03:13:31 +05302213#endif
lijuang4ece1e72015-08-14 21:02:36 +08002214 if (is_unlocked == status) {
2215 snprintf(response, sizeof(response), "\tDevice already : %s", (status ? "unlocked!" : "locked!"));
2216 fastboot_info(response);
2217 fastboot_okay("");
2218 return;
2219 }
2220
2221 /* status is true, it means to unlock device */
2222 if (status) {
lijuang21f12f52015-08-22 16:22:19 +08002223 if(!is_allow_unlock) {
2224 fastboot_fail("oem unlock is not allowed");
2225 return;
2226 }
2227
lijuang4ece1e72015-08-14 21:02:36 +08002228#if FBCON_DISPLAY_MSG
lijuang9a7d3b92015-11-30 14:41:24 +08002229 display_unlock_menu(type);
lijuang4ece1e72015-08-14 21:02:36 +08002230 fastboot_okay("");
2231 return;
2232#else
2233 if (type == UNLOCK) {
2234 fastboot_fail("Need wipe userdata. Do 'fastboot oem unlock-go'");
2235 return;
2236 }
2237#endif
lijuang21f12f52015-08-22 16:22:19 +08002238 }
lijuang4ece1e72015-08-14 21:02:36 +08002239
2240 set_device_unlock_value(type, status);
2241
2242 /* wipe data */
2243 struct recovery_message msg;
2244
2245 snprintf(msg.recovery, sizeof(msg.recovery), "recovery\n--wipe_data");
2246 write_misc(0, &msg, sizeof(msg));
2247
2248 fastboot_okay("");
2249 reboot_device(RECOVERY_MODE);
lijuang21f12f52015-08-22 16:22:19 +08002250}
2251
lijuang511a2b52015-08-14 20:50:51 +08002252static bool critical_flash_allowed(const char * entry)
2253{
2254 uint32_t i = 0;
2255 if (entry == NULL)
2256 return false;
2257
2258 for (i = 0; i < ARRAY_SIZE(critical_flash_allowed_ptn); i++) {
2259 if(!strcmp(entry, critical_flash_allowed_ptn[i]))
2260 return true;
2261 }
2262 return false;
Matthew Qin271927e2015-03-31 22:07:07 -04002263}
2264
2265#if DEVICE_TREE
Amol Jadicb524072012-08-09 16:40:18 -07002266int copy_dtb(uint8_t *boot_image_start, unsigned int scratch_offset)
2267{
2268 uint32 dt_image_offset = 0;
Amol Jadicb524072012-08-09 16:40:18 -07002269 uint32_t n;
Amol Jadicb524072012-08-09 16:40:18 -07002270 struct dt_table *table;
2271 struct dt_entry dt_entry;
2272 uint32_t dt_hdr_size;
2273 unsigned int compressed_size = 0;
2274 unsigned int dtb_size = 0;
2275 unsigned int out_avai_len = 0;
2276 unsigned char *out_addr = NULL;
2277 unsigned char *best_match_dt_addr = NULL;
2278 int rc;
2279
2280 struct boot_img_hdr *hdr = (struct boot_img_hdr *) (boot_image_start);
2281
Parth Dixit4097b622016-03-15 14:42:27 +05302282#ifndef OSVERSION_IN_BOOTIMAGE
2283 dt_size = hdr->dt_size;
2284#endif
2285
2286 if(dt_size != 0) {
Amol Jadicb524072012-08-09 16:40:18 -07002287 /* add kernel offset */
2288 dt_image_offset += page_size;
2289 n = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
2290 dt_image_offset += n;
2291
2292 /* add ramdisk offset */
2293 n = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
2294 dt_image_offset += n;
2295
2296 /* add second offset */
2297 if(hdr->second_size != 0) {
2298 n = ROUND_TO_PAGE(hdr->second_size, page_mask);
2299 dt_image_offset += n;
2300 }
2301
2302 /* offset now point to start of dt.img */
Deepa Dinamani28c0ffe2012-09-24 11:45:21 -07002303 table = (struct dt_table*)(boot_image_start + dt_image_offset);
Amol Jadicb524072012-08-09 16:40:18 -07002304
Deepa Dinamani19648b42013-09-05 17:05:55 -07002305 if (dev_tree_validate(table, hdr->page_size, &dt_hdr_size) != 0) {
Amol Jadicb524072012-08-09 16:40:18 -07002306 dprintf(CRITICAL, "ERROR: Cannot validate Device Tree Table \n");
2307 return -1;
2308 }
P.V. Phani Kumar82916762016-03-09 09:20:19 +05302309
2310 /* Its Error if, dt_hdr_size (table->num_entries * dt_entry size + Dev_Tree Header)
2311 goes beyound hdr->dt_size*/
Parth Dixit4097b622016-03-15 14:42:27 +05302312 if (dt_hdr_size > ROUND_TO_PAGE(dt_size,hdr->page_size)) {
P.V. Phani Kumar82916762016-03-09 09:20:19 +05302313 dprintf(CRITICAL, "ERROR: Invalid Device Tree size \n");
2314 return -1;
2315 }
2316
Joel Kingaa335dc2013-06-03 16:11:08 -07002317 /* Find index of device tree within device tree table */
2318 if(dev_tree_get_entry_info(table, &dt_entry) != 0){
Amol Jadicb524072012-08-09 16:40:18 -07002319 dprintf(CRITICAL, "ERROR: Getting device tree address failed\n");
2320 return -1;
2321 }
2322
Matthew Qin271927e2015-03-31 22:07:07 -04002323 best_match_dt_addr = (unsigned char *)boot_image_start + dt_image_offset + dt_entry.offset;
2324 if (is_gzip_package(best_match_dt_addr, dt_entry.size))
2325 {
2326 out_addr = (unsigned char *)target_get_scratch_address() + scratch_offset;
2327 out_avai_len = target_get_max_flash_size() - scratch_offset;
Matthew Qin0b15b322015-05-19 05:20:54 -04002328 dprintf(INFO, "decompressing dtb: start\n");
Matthew Qin271927e2015-03-31 22:07:07 -04002329 rc = decompress(best_match_dt_addr,
2330 dt_entry.size, out_addr, out_avai_len,
2331 &compressed_size, &dtb_size);
2332 if (rc)
2333 {
Matthew Qin0b15b322015-05-19 05:20:54 -04002334 dprintf(CRITICAL, "decompressing dtb failed!!!\n");
Matthew Qin271927e2015-03-31 22:07:07 -04002335 ASSERT(0);
2336 }
2337
Matthew Qin0b15b322015-05-19 05:20:54 -04002338 dprintf(INFO, "decompressing dtb: done\n");
Matthew Qin271927e2015-03-31 22:07:07 -04002339 best_match_dt_addr = out_addr;
2340 } else {
2341 dtb_size = dt_entry.size;
2342 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002343 /* Validate and Read device device tree in the "tags_add */
Matthew Qin271927e2015-03-31 22:07:07 -04002344 if (check_aboot_addr_range_overlap(hdr->tags_addr, dtb_size))
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002345 {
2346 dprintf(CRITICAL, "Device tree addresses overlap with aboot addresses.\n");
2347 return -1;
2348 }
2349
Amol Jadicb524072012-08-09 16:40:18 -07002350 /* Read device device tree in the "tags_add */
Matthew Qin271927e2015-03-31 22:07:07 -04002351 memmove((void*) hdr->tags_addr, (void *)best_match_dt_addr, dtb_size);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002352 } else
2353 return -1;
Amol Jadicb524072012-08-09 16:40:18 -07002354
2355 /* Everything looks fine. Return success. */
2356 return 0;
2357}
2358#endif
2359
Brian Swetland9c4c0752009-01-25 16:23:50 -08002360void cmd_boot(const char *arg, void *data, unsigned sz)
2361{
Amit Blay8a510302015-08-17 09:20:01 +03002362#ifdef MDTP_SUPPORT
2363 static bool is_mdtp_activated = 0;
2364#endif /* MDTP_SUPPORT */
Brian Swetland9c4c0752009-01-25 16:23:50 -08002365 unsigned kernel_actual;
2366 unsigned ramdisk_actual;
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002367 uint32_t image_actual;
2368 uint32_t dt_actual = 0;
P.V. Phani Kumareecfd822016-03-09 20:09:03 +05302369 uint32_t sig_actual = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08002370 struct boot_img_hdr *hdr = NULL;
2371 struct kernel64_hdr *kptr = NULL;
Brian Swetland9c4c0752009-01-25 16:23:50 -08002372 char *ptr = ((char*) data);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002373 int ret = 0;
2374 uint8_t dtb_copied = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08002375 unsigned int out_len = 0;
2376 unsigned int out_avai_len = 0;
2377 unsigned char *out_addr = NULL;
2378 uint32_t dtb_offset = 0;
2379 unsigned char *kernel_start_addr = NULL;
2380 unsigned int kernel_size = 0;
Matthew Qin271927e2015-03-31 22:07:07 -04002381 unsigned int scratch_offset = 0;
Matthew Qinbb7923d2015-02-09 10:56:09 +08002382
lijuang2008ff22016-03-07 17:56:27 +08002383#if FBCON_DISPLAY_MSG
2384 /* Exit keys' detection thread firstly */
2385 exit_menu_keys_detection();
2386#endif
2387
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002388#if VERIFIED_BOOT
Channagoud Kadabi6d5375e2015-06-23 17:15:42 -07002389 if(target_build_variant_user() && !device.is_unlocked)
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002390 {
2391 fastboot_fail("unlock device to use this command");
lijuang2008ff22016-03-07 17:56:27 +08002392 goto boot_failed;
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002393 }
2394#endif
2395
Brian Swetland9c4c0752009-01-25 16:23:50 -08002396 if (sz < sizeof(hdr)) {
2397 fastboot_fail("invalid bootimage header");
lijuang2008ff22016-03-07 17:56:27 +08002398 goto boot_failed;
Brian Swetland9c4c0752009-01-25 16:23:50 -08002399 }
2400
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002401 hdr = (struct boot_img_hdr *)data;
Brian Swetland9c4c0752009-01-25 16:23:50 -08002402
2403 /* ensure commandline is terminated */
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002404 hdr->cmdline[BOOT_ARGS_SIZE-1] = 0;
Brian Swetland9c4c0752009-01-25 16:23:50 -08002405
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002406 if(target_is_emmc_boot() && hdr->page_size) {
2407 page_size = hdr->page_size;
Subbaraman Narayanamurthyfbe13a02010-09-10 11:51:12 -07002408 page_mask = page_size - 1;
2409 }
2410
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002411 kernel_actual = ROUND_TO_PAGE(hdr->kernel_size, page_mask);
2412 ramdisk_actual = ROUND_TO_PAGE(hdr->ramdisk_size, page_mask);
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002413#if DEVICE_TREE
Parth Dixit4097b622016-03-15 14:42:27 +05302414#ifndef OSVERSION_IN_BOOTIMAGE
2415 dt_size = hdr->dt_size;
2416#endif
2417 dt_actual = ROUND_TO_PAGE(dt_size, page_mask);
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002418#endif
2419
2420 image_actual = ADD_OF(page_size, kernel_actual);
2421 image_actual = ADD_OF(image_actual, ramdisk_actual);
2422 image_actual = ADD_OF(image_actual, dt_actual);
2423
P.V. Phani Kumareecfd822016-03-09 20:09:03 +05302424 if (target_use_signed_kernel() && (!device.is_unlocked)) {
2425 /* Calculate the signature length from boot image */
2426 sig_actual = read_der_message_length(
2427 (unsigned char*)(data + image_actual),sz);
Channagoud Kadabi3d839012014-06-26 14:26:39 -07002428 image_actual = ADD_OF(image_actual, sig_actual);
P.V. Phani Kumareecfd822016-03-09 20:09:03 +05302429 }
Channagoud Kadabi3d839012014-06-26 14:26:39 -07002430
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002431 /* sz should have atleast raw boot image */
2432 if (image_actual > sz) {
Channagoud Kadabi3d839012014-06-26 14:26:39 -07002433 fastboot_fail("bootimage: incomplete or not signed");
lijuang2008ff22016-03-07 17:56:27 +08002434 goto boot_failed;
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002435 }
2436
Sridhar Parasuram361e4cd2015-09-24 11:19:19 -07002437 // Initialize boot state before trying to verify boot.img
2438#if VERIFIED_BOOT
Channagoud Kadabi699466e2015-11-03 12:37:42 -08002439 boot_verifier_init();
Mayank Groverb337e932017-01-18 20:00:40 +05302440#endif
Channagoud Kadabibf6ce7d2015-09-17 13:25:35 -07002441 /* Handle overflow if the input image size is greater than
2442 * boot image buffer can hold
2443 */
2444 if ((target_get_max_flash_size() - (image_actual - sig_actual)) < page_size)
2445 {
2446 fastboot_fail("booimage: size is greater than boot image buffer can hold");
lijuang2008ff22016-03-07 17:56:27 +08002447 goto boot_failed;
Channagoud Kadabibf6ce7d2015-09-17 13:25:35 -07002448 }
Sridhar Parasuram361e4cd2015-09-24 11:19:19 -07002449
Channagoud Kadabifc3b31f2014-06-18 17:41:01 -07002450 /* Verify the boot image
2451 * device & page_size are initialized in aboot_init
2452 */
Sridhar Parasuram7ae9fbc2015-09-17 09:56:30 -07002453 if (target_use_signed_kernel() && (!device.is_unlocked)) {
Channagoud Kadabi3d839012014-06-26 14:26:39 -07002454 /* Pass size excluding signature size, otherwise we would try to
2455 * access signature beyond its length
2456 */
2457 verify_signed_bootimg((uint32_t)data, (image_actual - sig_actual));
Sridhar Parasuram7ae9fbc2015-09-17 09:56:30 -07002458 }
Amit Blay4aa292f2015-04-28 21:55:59 +03002459#ifdef MDTP_SUPPORT
2460 else
2461 {
Shay Nachmani062aa3f2015-05-17 17:28:44 +03002462 /* fastboot boot is not allowed when MDTP is activated */
Amit Blay4aa292f2015-04-28 21:55:59 +03002463 mdtp_ext_partition_verification_t ext_partition;
Amit Blay8a510302015-08-17 09:20:01 +03002464
2465 if (!is_mdtp_activated) {
2466 ext_partition.partition = MDTP_PARTITION_NONE;
2467 mdtp_fwlock_verify_lock(&ext_partition);
2468 }
Amit Blay4aa292f2015-04-28 21:55:59 +03002469 }
Shay Nachmani062aa3f2015-05-17 17:28:44 +03002470
Amir Kotzer7c768c02016-04-13 09:08:05 +03002471 /* If mdtp state cannot be validate, block fastboot boot*/
2472 if(mdtp_activated(&is_mdtp_activated)){
2473 dprintf(CRITICAL, "mdtp_activated cannot validate state.\n");
2474 dprintf(CRITICAL, "Can not proceed with fastboot boot command.\n");
2475 goto boot_failed;
2476 }
Shay Nachmani062aa3f2015-05-17 17:28:44 +03002477 if(is_mdtp_activated){
2478 dprintf(CRITICAL, "fastboot boot command is not available.\n");
lijuang2008ff22016-03-07 17:56:27 +08002479 goto boot_failed;
Shay Nachmani062aa3f2015-05-17 17:28:44 +03002480 }
Amit Blay4aa292f2015-04-28 21:55:59 +03002481#endif /* MDTP_SUPPORT */
2482
Sridhar Parasuram361e4cd2015-09-24 11:19:19 -07002483#if VERIFIED_BOOT
Parth Dixitbc9b6492015-10-18 00:41:38 +05302484#if !VBOOT_MOTA
Sridhar Parasuram361e4cd2015-09-24 11:19:19 -07002485 // send root of trust
2486 if(!send_rot_command((uint32_t)device.is_unlocked))
2487 ASSERT(0);
2488#endif
Parth Dixitbc9b6492015-10-18 00:41:38 +05302489#endif
Channagoud Kadabia22144f2013-03-20 11:49:01 -07002490 /*
Matthew Qinbb7923d2015-02-09 10:56:09 +08002491 * Check if the kernel image is a gzip package. If yes, need to decompress it.
2492 * If not, continue booting.
2493 */
2494 if (is_gzip_package((unsigned char *)(data + page_size), hdr->kernel_size))
2495 {
2496 out_addr = (unsigned char *)target_get_scratch_address();
2497 out_addr = (unsigned char *)(out_addr + image_actual + page_size);
2498 out_avai_len = target_get_max_flash_size() - image_actual - page_size;
Matthew Qin0b15b322015-05-19 05:20:54 -04002499 dprintf(INFO, "decompressing kernel image: start\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08002500 ret = decompress((unsigned char *)(ptr + page_size),
2501 hdr->kernel_size, out_addr, out_avai_len,
2502 &dtb_offset, &out_len);
2503 if (ret)
2504 {
Matthew Qin0b15b322015-05-19 05:20:54 -04002505 dprintf(CRITICAL, "decompressing image failed!!!\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08002506 ASSERT(0);
2507 }
2508
Matthew Qin0b15b322015-05-19 05:20:54 -04002509 dprintf(INFO, "decompressing kernel image: done\n");
Matthew Qinbb7923d2015-02-09 10:56:09 +08002510 kptr = (struct kernel64_hdr *)out_addr;
2511 kernel_start_addr = out_addr;
2512 kernel_size = out_len;
2513 } else {
2514 kptr = (struct kernel64_hdr*)((char *)data + page_size);
2515 kernel_start_addr = (unsigned char *)((char *)data + page_size);
2516 kernel_size = hdr->kernel_size;
2517 }
2518
2519 /*
Channagoud Kadabia22144f2013-03-20 11:49:01 -07002520 * Update the kernel/ramdisk/tags address if the boot image header
2521 * has default values, these default values come from mkbootimg when
2522 * the boot image is flashed using fastboot flash:raw
2523 */
Abhimanyu Kapur0f79d572014-02-19 22:03:02 -08002524 update_ker_tags_rdisk_addr(hdr, IS_ARM64(kptr));
Dima Zavin3cadfff2013-03-21 14:30:48 -07002525
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002526 /* Get virtual addresses since the hdr saves physical addresses. */
2527 hdr->kernel_addr = VA(hdr->kernel_addr);
2528 hdr->ramdisk_addr = VA(hdr->ramdisk_addr);
2529 hdr->tags_addr = VA(hdr->tags_addr);
Brian Swetland9c4c0752009-01-25 16:23:50 -08002530
Matthew Qinbb7923d2015-02-09 10:56:09 +08002531 kernel_size = ROUND_TO_PAGE(kernel_size, page_mask);
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002532 /* Check if the addresses in the header are valid. */
Matthew Qinbb7923d2015-02-09 10:56:09 +08002533 if (check_aboot_addr_range_overlap(hdr->kernel_addr, kernel_size) ||
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002534 check_aboot_addr_range_overlap(hdr->ramdisk_addr, ramdisk_actual))
2535 {
2536 dprintf(CRITICAL, "kernel/ramdisk addresses overlap with aboot addresses.\n");
lijuang2008ff22016-03-07 17:56:27 +08002537 goto boot_failed;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002538 }
2539
Amol Jadicb524072012-08-09 16:40:18 -07002540#if DEVICE_TREE
Matthew Qin271927e2015-03-31 22:07:07 -04002541 scratch_offset = image_actual + page_size + out_len;
Amol Jadicb524072012-08-09 16:40:18 -07002542 /* find correct dtb and copy it to right location */
Matthew Qin271927e2015-03-31 22:07:07 -04002543 ret = copy_dtb(data, scratch_offset);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002544
2545 dtb_copied = !ret ? 1 : 0;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002546#else
2547 if (check_aboot_addr_range_overlap(hdr->tags_addr, MAX_TAGS_SIZE))
2548 {
2549 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
lijuang2008ff22016-03-07 17:56:27 +08002550 goto boot_failed;
Deepa Dinamani0e163a42013-05-24 17:08:15 -07002551 }
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002552#endif
2553
2554 /* Load ramdisk & kernel */
2555 memmove((void*) hdr->ramdisk_addr, ptr + page_size + kernel_actual, hdr->ramdisk_size);
Matthew Qinbb7923d2015-02-09 10:56:09 +08002556 memmove((void*) hdr->kernel_addr, (char*) (kernel_start_addr), kernel_size);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002557
2558#if DEVICE_TREE
Matthew Qinbb7923d2015-02-09 10:56:09 +08002559 if (check_aboot_addr_range_overlap(hdr->tags_addr, kernel_actual))
2560 {
2561 dprintf(CRITICAL, "Tags addresses overlap with aboot addresses.\n");
lijuang2008ff22016-03-07 17:56:27 +08002562 goto boot_failed;
Matthew Qinbb7923d2015-02-09 10:56:09 +08002563 }
2564
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002565 /*
2566 * If dtb is not found look for appended DTB in the kernel.
2567 * If appended dev tree is found, update the atags with
2568 * memory address to the DTB appended location on RAM.
2569 * Else update with the atags address in the kernel header
2570 */
2571 if (!dtb_copied) {
2572 void *dtb;
Matthew Qinbb7923d2015-02-09 10:56:09 +08002573 dtb = dev_tree_appended((void*)(ptr + page_size),
2574 hdr->kernel_size, dtb_offset,
Dima Zavine63e5572013-05-03 12:23:06 -07002575 (void *)hdr->tags_addr);
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002576 if (!dtb) {
2577 fastboot_fail("dtb not found");
lijuang2008ff22016-03-07 17:56:27 +08002578 goto boot_failed;
Channagoud Kadabi8e9020f2013-04-16 11:24:32 -07002579 }
Amol Jadicb524072012-08-09 16:40:18 -07002580 }
2581#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08002582
2583 fastboot_okay("");
Amol Jadi7c4316c2013-10-07 14:19:26 -07002584 fastboot_stop();
Brian Swetland9c4c0752009-01-25 16:23:50 -08002585
Dima Zavin77e41f32013-03-06 16:10:43 -08002586 boot_linux((void*) hdr->kernel_addr, (void*) hdr->tags_addr,
Deepa Dinamani0bf2f442012-10-19 11:41:06 -07002587 (const char*) hdr->cmdline, board_machtype(),
2588 (void*) hdr->ramdisk_addr, hdr->ramdisk_size);
lijuang2008ff22016-03-07 17:56:27 +08002589
2590 /* fastboot already stop, it's no need to show fastboot menu */
2591 return;
2592boot_failed:
2593#if FBCON_DISPLAY_MSG
2594 /* revert to fastboot menu if boot failed */
2595 display_fastboot_menu();
2596#endif
2597 return;
Brian Swetland9c4c0752009-01-25 16:23:50 -08002598}
2599
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002600void cmd_erase_nand(const char *arg, void *data, unsigned sz)
Dima Zavin214cc642009-01-26 11:16:21 -08002601{
2602 struct ptentry *ptn;
2603 struct ptable *ptable;
2604
2605 ptable = flash_get_ptable();
2606 if (ptable == NULL) {
2607 fastboot_fail("partition table doesn't exist");
2608 return;
2609 }
2610
2611 ptn = ptable_find(ptable, arg);
2612 if (ptn == NULL) {
2613 fastboot_fail("unknown partition name");
2614 return;
2615 }
2616
2617 if (flash_erase(ptn)) {
2618 fastboot_fail("failed to erase partition");
2619 return;
2620 }
2621 fastboot_okay("");
2622}
2623
Bikas Gurungd48bd242010-09-04 19:54:32 -07002624
2625void cmd_erase_mmc(const char *arg, void *data, unsigned sz)
2626{
2627 unsigned long long ptn = 0;
Oliver Wangcee448d2013-10-22 18:40:13 +08002628 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07002629 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002630 uint8_t lun = 0;
Vijay Kumar Pendoti0b21f462016-05-02 17:09:18 +05302631 char *footer = NULL;
Bikas Gurungd48bd242010-09-04 19:54:32 -07002632
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002633#if VERIFIED_BOOT
2634 if(!strcmp(arg, KEYSTORE_PTN_NAME))
2635 {
2636 if(!device.is_unlocked)
2637 {
2638 fastboot_fail("unlock device to erase keystore");
2639 return;
2640 }
2641 }
2642#endif
2643
Kinson Chikf1a43512011-07-14 11:28:39 -07002644 index = partition_get_index(arg);
2645 ptn = partition_get_offset(index);
Oliver Wangcee448d2013-10-22 18:40:13 +08002646 size = partition_get_size(index);
Neeti Desaica8c9602011-10-06 11:40:00 -07002647
Kinson Chikf1a43512011-07-14 11:28:39 -07002648 if(ptn == 0) {
Neeti Desaica8c9602011-10-06 11:40:00 -07002649 fastboot_fail("Partition table doesn't exist\n");
Bikas Gurungd48bd242010-09-04 19:54:32 -07002650 return;
2651 }
Kun Liang2f1601a2013-08-12 16:29:54 +08002652
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002653 lun = partition_get_lun(index);
2654 mmc_set_lun(lun);
2655
Sridhar Parasuramd7957122015-02-27 11:33:40 -08002656 if (platform_boot_dev_isemmc())
2657 {
2658 if (mmc_erase_card(ptn, size)) {
2659 fastboot_fail("failed to erase partition\n");
2660 return;
2661 }
2662 } else {
2663 BUF_DMA_ALIGN(out, DEFAULT_ERASE_SIZE);
2664 size = partition_get_size(index);
2665 if (size > DEFAULT_ERASE_SIZE)
2666 size = DEFAULT_ERASE_SIZE;
Kun Liang2f1601a2013-08-12 16:29:54 +08002667
Sridhar Parasuramd7957122015-02-27 11:33:40 -08002668 /* Simple inefficient version of erase. Just writing
2669 0 in first several blocks */
2670 if (mmc_write(ptn , size, (unsigned int *)out)) {
2671 fastboot_fail("failed to erase partition");
2672 return;
2673 }
Vijay Kumar Pendoti0b21f462016-05-02 17:09:18 +05302674 /*Erase FDE metadata at the userdata footer*/
2675 if(!(strncmp(arg, "userdata", 8)))
2676 {
2677 footer = memalign(CACHE_LINE, FOOTER_SIZE);
2678 memset((void *)footer, 0, FOOTER_SIZE);
2679
2680 size = partition_get_size(index);
2681
2682 if (mmc_write((ptn + size) - FOOTER_SIZE , FOOTER_SIZE, (unsigned int *)footer)) {
2683 fastboot_fail("failed to erase userdata footer");
2684 free(footer);
2685 return;
2686 }
2687 free(footer);
2688 }
Bikas Gurungd48bd242010-09-04 19:54:32 -07002689 }
Sridhar Parasuram32b30662015-07-10 13:33:22 -07002690#if VERIFIED_BOOT
Parth Dixitbc9b6492015-10-18 00:41:38 +05302691#if !VBOOT_MOTA
Sridhar Parasuram32b30662015-07-10 13:33:22 -07002692 if(!(strncmp(arg, "userdata", 8)))
2693 if(send_delete_keys_to_tz())
2694 ASSERT(0);
2695#endif
Parth Dixitbc9b6492015-10-18 00:41:38 +05302696#endif
Bikas Gurungd48bd242010-09-04 19:54:32 -07002697 fastboot_okay("");
2698}
2699
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002700void cmd_erase(const char *arg, void *data, unsigned sz)
2701{
Channagoud Kadabice3cf422015-04-17 16:02:30 -07002702#if VERIFIED_BOOT
Channagoud Kadabi35297672015-06-13 11:09:49 -07002703 if (target_build_variant_user())
Channagoud Kadabice3cf422015-04-17 16:02:30 -07002704 {
Sridhar Parasuramc32d07d2015-07-12 10:57:48 -07002705 if(!device.is_unlocked)
Channagoud Kadabice3cf422015-04-17 16:02:30 -07002706 {
Channagoud Kadabi35297672015-06-13 11:09:49 -07002707 fastboot_fail("device is locked. Cannot erase");
Channagoud Kadabice3cf422015-04-17 16:02:30 -07002708 return;
2709 }
2710 }
2711#endif
2712
Sridhar Parasurame94e8152014-10-24 14:06:03 -07002713 if(target_is_emmc_boot())
2714 cmd_erase_mmc(arg, data, sz);
2715 else
2716 cmd_erase_nand(arg, data, sz);
2717}
Bikas Gurungd48bd242010-09-04 19:54:32 -07002718
Ajay Dudani5c761132011-04-07 20:19:04 -07002719void cmd_flash_mmc_img(const char *arg, void *data, unsigned sz)
Shashank Mittal23b8f422010-04-16 19:27:21 -07002720{
2721 unsigned long long ptn = 0;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07002722 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07002723 int index = INVALID_PTN;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002724 char *token = NULL;
2725 char *pname = NULL;
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07002726 char *sp;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002727 uint8_t lun = 0;
2728 bool lun_set = false;
Subbaraman Narayanamurthyc95b5b12010-08-31 13:19:48 -07002729
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07002730 token = strtok_r((char *)arg, ":", &sp);
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002731 pname = token;
Sridhar Parasuramefc133f2015-05-04 13:35:41 -07002732 token = strtok_r(NULL, ":", &sp);
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002733 if(token)
2734 {
2735 lun = atoi(token);
2736 mmc_set_lun(lun);
2737 lun_set = true;
2738 }
2739
Mao Jinlong226f33a2014-07-04 17:24:10 +08002740 if (pname)
Greg Grisco6e754772011-06-23 12:19:39 -07002741 {
Channagoud Kadabiad259832015-05-29 11:14:17 -07002742 if (!strncmp(pname, "frp-unlock", strlen("frp-unlock")))
2743 {
2744 if (!aboot_frp_unlock(pname, data, sz))
2745 {
2746 fastboot_info("FRP unlock successful");
2747 fastboot_okay("");
2748 }
2749 else
2750 fastboot_fail("Secret key is invalid, please update the bootloader with secret key");
2751
2752 return;
2753 }
2754
Mao Jinlong226f33a2014-07-04 17:24:10 +08002755 if (!strcmp(pname, "partition"))
2756 {
2757 dprintf(INFO, "Attempt to write partition image.\n");
2758 if (write_partition(sz, (unsigned char *) data)) {
2759 fastboot_fail("failed to write partition");
Greg Grisco6e754772011-06-23 12:19:39 -07002760 return;
2761 }
2762 }
Mao Jinlong226f33a2014-07-04 17:24:10 +08002763 else
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002764 {
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002765#if VERIFIED_BOOT
2766 if(!strcmp(pname, KEYSTORE_PTN_NAME))
2767 {
2768 if(!device.is_unlocked)
2769 {
2770 fastboot_fail("unlock device to flash keystore");
2771 return;
2772 }
Gaurav Nebhwanic7313cc2015-12-15 22:25:04 +05302773 if(!boot_verify_validate_keystore((unsigned char *)data,sz))
Shashank Mittald3e54dd2014-08-28 15:24:02 -07002774 {
2775 fastboot_fail("image is not a keystore file");
2776 return;
2777 }
2778 }
2779#endif
Mao Jinlong226f33a2014-07-04 17:24:10 +08002780 index = partition_get_index(pname);
2781 ptn = partition_get_offset(index);
2782 if(ptn == 0) {
2783 fastboot_fail("partition table doesn't exist");
2784 return;
2785 }
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002786
Mao Jinlong226f33a2014-07-04 17:24:10 +08002787 if (!strcmp(pname, "boot") || !strcmp(pname, "recovery")) {
2788 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
2789 fastboot_fail("image is not a boot image");
2790 return;
2791 }
2792 }
2793
2794 if(!lun_set)
2795 {
2796 lun = partition_get_lun(index);
2797 mmc_set_lun(lun);
2798 }
2799
2800 size = partition_get_size(index);
Gaurav Nebhwani4d2389c2016-03-17 21:10:05 +05302801 if (ROUND_TO_PAGE(sz, mmc_blocksize_mask) > size) {
Mao Jinlong226f33a2014-07-04 17:24:10 +08002802 fastboot_fail("size too large");
2803 return;
2804 }
2805 else if (mmc_write(ptn , sz, (unsigned int *)data)) {
2806 fastboot_fail("flash write failure");
2807 return;
2808 }
Greg Grisco6e754772011-06-23 12:19:39 -07002809 }
Shashank Mittal23b8f422010-04-16 19:27:21 -07002810 }
2811 fastboot_okay("");
2812 return;
2813}
2814
Ajay Dudanide984792015-03-02 09:57:41 -08002815void cmd_flash_meta_img(const char *arg, void *data, unsigned sz)
2816{
2817 int i, images;
2818 meta_header_t *meta_header;
2819 img_header_entry_t *img_header_entry;
Parth Dixit3e2d3032016-03-04 17:11:52 +05302820 /*End of the image address*/
2821 uintptr_t data_end;
2822
2823 if( (UINT_MAX - sz) > (uintptr_t)data )
2824 data_end = (uintptr_t)data + sz;
2825 else
2826 {
2827 fastboot_fail("Cannot flash: image header corrupt");
2828 return;
2829 }
2830
2831 if( data_end < ((uintptr_t)data + sizeof(meta_header_t)))
2832 {
2833 fastboot_fail("Cannot flash: image header corrupt");
2834 return;
2835 }
Ajay Dudanide984792015-03-02 09:57:41 -08002836
lijuang8ee21882016-04-19 16:57:11 +08002837 /* If device is locked:
2838 * Forbid to flash image to avoid the device to bypass the image
2839 * which with "any" name other than bootloader. Because it maybe
2840 * a meta package of all partitions.
2841 */
2842#if VERIFIED_BOOT
2843 if (target_build_variant_user()) {
2844 if (!device.is_unlocked) {
2845 fastboot_fail("Device is locked, meta image flashing is not allowed");
2846 return;
2847 }
2848#if !VBOOT_MOTA
2849 if(!device.is_unlock_critical) {
2850 fastboot_fail("Device is critical locked, Meta image flashing is not allowed");
2851 return;
2852 }
2853#endif
2854 }
2855#endif
2856
Ajay Dudanide984792015-03-02 09:57:41 -08002857 meta_header = (meta_header_t*) data;
Parth Dixit3e2d3032016-03-04 17:11:52 +05302858 if( data_end < ((uintptr_t)data + meta_header->img_hdr_sz))
2859 {
2860 fastboot_fail("Cannot flash: image header corrupt");
2861 return;
2862 }
Ajay Dudanide984792015-03-02 09:57:41 -08002863 img_header_entry = (img_header_entry_t*) (data+sizeof(meta_header_t));
2864
2865 images = meta_header->img_hdr_sz / sizeof(img_header_entry_t);
2866
2867 for (i=0; i<images; i++) {
2868
2869 if((img_header_entry[i].ptn_name == NULL) ||
2870 (img_header_entry[i].start_offset == 0) ||
2871 (img_header_entry[i].size == 0))
2872 break;
2873
Parth Dixit3e2d3032016-03-04 17:11:52 +05302874 if( data_end < ((uintptr_t)data + img_header_entry[i].start_offset
2875 + img_header_entry[i].size) )
2876 {
2877 fastboot_fail("Cannot flash: image size mismatch");
2878 break;
2879 }
2880
Ajay Dudanide984792015-03-02 09:57:41 -08002881 cmd_flash_mmc_img(img_header_entry[i].ptn_name,
2882 (void *) data + img_header_entry[i].start_offset,
2883 img_header_entry[i].size);
2884 }
2885
Channagoud Kadabiaafbbfc2015-03-04 17:52:56 -08002886 if (!strncmp(arg, "bootloader", strlen("bootloader")))
2887 {
2888 strlcpy(device.bootloader_version, TARGET(BOARD), MAX_VERSION_LEN);
2889 strlcat(device.bootloader_version, "-", MAX_VERSION_LEN);
2890 strlcat(device.bootloader_version, meta_header->img_version, MAX_VERSION_LEN);
2891 }
2892 else
2893 {
2894 strlcpy(device.radio_version, TARGET(BOARD), MAX_VERSION_LEN);
2895 strlcat(device.radio_version, "-", MAX_VERSION_LEN);
2896 strlcat(device.radio_version, meta_header->img_version, MAX_VERSION_LEN);
2897 }
2898
2899 write_device_info(&device);
Ajay Dudanide984792015-03-02 09:57:41 -08002900 fastboot_okay("");
2901 return;
2902}
2903
Ajay Dudani5c761132011-04-07 20:19:04 -07002904void cmd_flash_mmc_sparse_img(const char *arg, void *data, unsigned sz)
2905{
2906 unsigned int chunk;
wufeng.jiang813dc352015-06-02 23:02:46 -04002907 uint64_t chunk_data_sz;
Channagoud Kadabif627cf82013-09-09 14:08:20 -07002908 uint32_t *fill_buf = NULL;
2909 uint32_t fill_val;
Ajay Dudani5c761132011-04-07 20:19:04 -07002910 sparse_header_t *sparse_header;
2911 chunk_header_t *chunk_header;
Ajay Dudaniab18f022011-05-12 14:39:22 -07002912 uint32_t total_blocks = 0;
Ajay Dudani5c761132011-04-07 20:19:04 -07002913 unsigned long long ptn = 0;
Channagoud Kadabi65b91002011-10-11 17:34:33 +05302914 unsigned long long size = 0;
Kinson Chikf1a43512011-07-14 11:28:39 -07002915 int index = INVALID_PTN;
Unnati Gandhi0c8e7c52014-07-17 14:33:09 +05302916 uint32_t i;
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002917 uint8_t lun = 0;
vijay kumar800255e2015-04-24 20:26:19 +05302918 /*End of the sparse image address*/
Parth Dixit64b1a482016-03-07 16:31:26 +05302919 uintptr_t data_end = (uintptr_t)data + sz;
Ajay Dudani5c761132011-04-07 20:19:04 -07002920
Kinson Chikf1a43512011-07-14 11:28:39 -07002921 index = partition_get_index(arg);
2922 ptn = partition_get_offset(index);
2923 if(ptn == 0) {
Ajay Dudani5c761132011-04-07 20:19:04 -07002924 fastboot_fail("partition table doesn't exist");
2925 return;
2926 }
2927
Channagoud Kadabi65b91002011-10-11 17:34:33 +05302928 size = partition_get_size(index);
Channagoud Kadabi65b91002011-10-11 17:34:33 +05302929
Sundarajan Srinivasand3514bb2013-12-04 17:02:00 -08002930 lun = partition_get_lun(index);
2931 mmc_set_lun(lun);
2932
vijay kumar800255e2015-04-24 20:26:19 +05302933 if (sz < sizeof(sparse_header_t)) {
2934 fastboot_fail("size too low");
2935 return;
2936 }
2937
Ajay Dudani5c761132011-04-07 20:19:04 -07002938 /* Read and skip over sparse image header */
2939 sparse_header = (sparse_header_t *) data;
vijay kumar800255e2015-04-24 20:26:19 +05302940
vijay kumar1321f342015-03-27 12:13:42 +05302941 if (((uint64_t)sparse_header->total_blks * (uint64_t)sparse_header->blk_sz) > size) {
Ajay Dudani876b3282012-12-21 14:12:17 -08002942 fastboot_fail("size too large");
2943 return;
2944 }
2945
vijay kumarde4fcf62015-04-23 13:05:49 +05302946 data += sizeof(sparse_header_t);
vijay kumar800255e2015-04-24 20:26:19 +05302947
Parth Dixit64b1a482016-03-07 16:31:26 +05302948 if (data_end < (uintptr_t)data) {
vijay kumar800255e2015-04-24 20:26:19 +05302949 fastboot_fail("buffer overreads occured due to invalid sparse header");
2950 return;
2951 }
2952
vijay kumarde4fcf62015-04-23 13:05:49 +05302953 if(sparse_header->file_hdr_sz != sizeof(sparse_header_t))
Ajay Dudani5c761132011-04-07 20:19:04 -07002954 {
vijay kumarde4fcf62015-04-23 13:05:49 +05302955 fastboot_fail("sparse header size mismatch");
2956 return;
Ajay Dudani5c761132011-04-07 20:19:04 -07002957 }
2958
Ajay Dudanib06c05f2011-05-12 14:46:10 -07002959 dprintf (SPEW, "=== Sparse Image Header ===\n");
2960 dprintf (SPEW, "magic: 0x%x\n", sparse_header->magic);
2961 dprintf (SPEW, "major_version: 0x%x\n", sparse_header->major_version);
2962 dprintf (SPEW, "minor_version: 0x%x\n", sparse_header->minor_version);
2963 dprintf (SPEW, "file_hdr_sz: %d\n", sparse_header->file_hdr_sz);
2964 dprintf (SPEW, "chunk_hdr_sz: %d\n", sparse_header->chunk_hdr_sz);
2965 dprintf (SPEW, "blk_sz: %d\n", sparse_header->blk_sz);
2966 dprintf (SPEW, "total_blks: %d\n", sparse_header->total_blks);
2967 dprintf (SPEW, "total_chunks: %d\n", sparse_header->total_chunks);
Ajay Dudani5c761132011-04-07 20:19:04 -07002968
2969 /* Start processing chunks */
2970 for (chunk=0; chunk<sparse_header->total_chunks; chunk++)
2971 {
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302972 /* Make sure the total image size does not exceed the partition size */
2973 if(((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz) >= size) {
2974 fastboot_fail("size too large");
2975 return;
2976 }
Ajay Dudani5c761132011-04-07 20:19:04 -07002977 /* Read and skip over chunk header */
2978 chunk_header = (chunk_header_t *) data;
2979 data += sizeof(chunk_header_t);
2980
Parth Dixit64b1a482016-03-07 16:31:26 +05302981 if (data_end < (uintptr_t)data) {
vijay kumar800255e2015-04-24 20:26:19 +05302982 fastboot_fail("buffer overreads occured due to invalid sparse header");
2983 return;
2984 }
2985
Ajay Dudani5c761132011-04-07 20:19:04 -07002986 dprintf (SPEW, "=== Chunk Header ===\n");
2987 dprintf (SPEW, "chunk_type: 0x%x\n", chunk_header->chunk_type);
2988 dprintf (SPEW, "chunk_data_sz: 0x%x\n", chunk_header->chunk_sz);
2989 dprintf (SPEW, "total_size: 0x%x\n", chunk_header->total_sz);
2990
vijay kumar800255e2015-04-24 20:26:19 +05302991 if(sparse_header->chunk_hdr_sz != sizeof(chunk_header_t))
Ajay Dudani5c761132011-04-07 20:19:04 -07002992 {
vijay kumar800255e2015-04-24 20:26:19 +05302993 fastboot_fail("chunk header size mismatch");
2994 return;
Ajay Dudani5c761132011-04-07 20:19:04 -07002995 }
2996
wufeng.jiang813dc352015-06-02 23:02:46 -04002997 if (!sparse_header->blk_sz ){
2998 fastboot_fail("Invalid block size\n");
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05302999 return;
3000 }
3001
wufeng.jiang813dc352015-06-02 23:02:46 -04003002 chunk_data_sz = (uint64_t)sparse_header->blk_sz * chunk_header->chunk_sz;
3003
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05303004 /* Make sure that the chunk size calculated from sparse image does not
3005 * exceed partition size
3006 */
3007 if ((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz + chunk_data_sz > size)
3008 {
3009 fastboot_fail("Chunk data size exceeds partition size");
3010 return;
3011 }
3012
Ajay Dudani5c761132011-04-07 20:19:04 -07003013 switch (chunk_header->chunk_type)
3014 {
3015 case CHUNK_TYPE_RAW:
wufeng.jiang813dc352015-06-02 23:02:46 -04003016 if((uint64_t)chunk_header->total_sz != ((uint64_t)sparse_header->chunk_hdr_sz +
Ajay Dudani5c761132011-04-07 20:19:04 -07003017 chunk_data_sz))
3018 {
3019 fastboot_fail("Bogus chunk size for chunk type Raw");
3020 return;
3021 }
3022
Parth Dixit64b1a482016-03-07 16:31:26 +05303023 if (data_end < (uintptr_t)data + chunk_data_sz) {
vijay kumar800255e2015-04-24 20:26:19 +05303024 fastboot_fail("buffer overreads occured due to invalid sparse header");
3025 return;
3026 }
3027
wufeng.jiang813dc352015-06-02 23:02:46 -04003028 /* chunk_header->total_sz is uint32,So chunk_data_sz is now less than 2^32
3029 otherwise it will return in the line above
3030 */
Ajay Dudaniab18f022011-05-12 14:39:22 -07003031 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
wufeng.jiang813dc352015-06-02 23:02:46 -04003032 (uint32_t)chunk_data_sz,
Ajay Dudaniab18f022011-05-12 14:39:22 -07003033 (unsigned int*)data))
Ajay Dudani5c761132011-04-07 20:19:04 -07003034 {
3035 fastboot_fail("flash write failure");
3036 return;
3037 }
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05303038 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
3039 fastboot_fail("Bogus size for RAW chunk type");
3040 return;
3041 }
Ajay Dudani5c761132011-04-07 20:19:04 -07003042 total_blocks += chunk_header->chunk_sz;
wufeng.jiang813dc352015-06-02 23:02:46 -04003043 data += (uint32_t)chunk_data_sz;
Ajay Dudani5c761132011-04-07 20:19:04 -07003044 break;
3045
Channagoud Kadabif627cf82013-09-09 14:08:20 -07003046 case CHUNK_TYPE_FILL:
3047 if(chunk_header->total_sz != (sparse_header->chunk_hdr_sz +
3048 sizeof(uint32_t)))
3049 {
3050 fastboot_fail("Bogus chunk size for chunk type FILL");
3051 return;
3052 }
3053
3054 fill_buf = (uint32_t *)memalign(CACHE_LINE, ROUNDUP(sparse_header->blk_sz, CACHE_LINE));
3055 if (!fill_buf)
3056 {
3057 fastboot_fail("Malloc failed for: CHUNK_TYPE_FILL");
3058 return;
3059 }
3060
Parth Dixit64b1a482016-03-07 16:31:26 +05303061 if (data_end < (uintptr_t)data + sizeof(uint32_t)) {
vijay kumar800255e2015-04-24 20:26:19 +05303062 fastboot_fail("buffer overreads occured due to invalid sparse header");
Gaurav Nebhwani163cbf82016-03-17 21:21:27 +05303063 free(fill_buf);
vijay kumar800255e2015-04-24 20:26:19 +05303064 return;
3065 }
Channagoud Kadabif627cf82013-09-09 14:08:20 -07003066 fill_val = *(uint32_t *)data;
3067 data = (char *) data + sizeof(uint32_t);
Channagoud Kadabif627cf82013-09-09 14:08:20 -07003068
3069 for (i = 0; i < (sparse_header->blk_sz / sizeof(fill_val)); i++)
3070 {
3071 fill_buf[i] = fill_val;
3072 }
3073
Gaurav Nebhwanie94cbe12016-03-17 21:16:34 +05303074 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz))
3075 {
3076 fastboot_fail("bogus size for chunk FILL type");
3077 free(fill_buf);
3078 return;
3079 }
3080
wufeng.jiang813dc352015-06-02 23:02:46 -04003081 for (i = 0; i < chunk_header->chunk_sz; i++)
Channagoud Kadabif627cf82013-09-09 14:08:20 -07003082 {
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05303083 /* Make sure that the data written to partition does not exceed partition size */
3084 if ((uint64_t)total_blocks * (uint64_t)sparse_header->blk_sz + sparse_header->blk_sz > size)
3085 {
3086 fastboot_fail("Chunk data size for fill type exceeds partition size");
Gaurav Nebhwani163cbf82016-03-17 21:21:27 +05303087 free(fill_buf);
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05303088 return;
3089 }
3090
Channagoud Kadabif627cf82013-09-09 14:08:20 -07003091 if(mmc_write(ptn + ((uint64_t)total_blocks*sparse_header->blk_sz),
3092 sparse_header->blk_sz,
3093 fill_buf))
3094 {
3095 fastboot_fail("flash write failure");
3096 free(fill_buf);
3097 return;
3098 }
3099
3100 total_blocks++;
3101 }
3102
3103 free(fill_buf);
3104 break;
3105
Ajay Dudani5c761132011-04-07 20:19:04 -07003106 case CHUNK_TYPE_DONT_CARE:
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05303107 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
3108 fastboot_fail("bogus size for chunk DONT CARE type");
3109 return;
3110 }
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07003111 total_blocks += chunk_header->chunk_sz;
3112 break;
3113
Ajay Dudani5c761132011-04-07 20:19:04 -07003114 case CHUNK_TYPE_CRC:
3115 if(chunk_header->total_sz != sparse_header->chunk_hdr_sz)
3116 {
wufeng.jiang813dc352015-06-02 23:02:46 -04003117 fastboot_fail("Bogus chunk size for chunk type CRC");
Ajay Dudani5c761132011-04-07 20:19:04 -07003118 return;
3119 }
Aparna Mallavarapu2ae741a2015-03-28 01:41:08 +05303120 if(total_blocks > (UINT_MAX - chunk_header->chunk_sz)) {
3121 fastboot_fail("bogus size for chunk CRC type");
3122 return;
3123 }
Ajay Dudani5c761132011-04-07 20:19:04 -07003124 total_blocks += chunk_header->chunk_sz;
Parth Dixit64b1a482016-03-07 16:31:26 +05303125 if ((uintptr_t)data > UINT_MAX - chunk_data_sz) {
vijay kumar800255e2015-04-24 20:26:19 +05303126 fastboot_fail("integer overflow occured");
3127 return;
3128 }
wufeng.jiang813dc352015-06-02 23:02:46 -04003129 data += (uint32_t)chunk_data_sz;
Parth Dixit64b1a482016-03-07 16:31:26 +05303130 if (data_end < (uintptr_t)data) {
vijay kumar800255e2015-04-24 20:26:19 +05303131 fastboot_fail("buffer overreads occured due to invalid sparse header");
3132 return;
3133 }
Ajay Dudani5c761132011-04-07 20:19:04 -07003134 break;
3135
Kinson Chik kchik@codeaurora.orgda29b1e2011-05-06 17:36:39 -07003136 default:
Channagoud Kadabif627cf82013-09-09 14:08:20 -07003137 dprintf(CRITICAL, "Unkown chunk type: %x\n",chunk_header->chunk_type);
Ajay Dudani5c761132011-04-07 20:19:04 -07003138 fastboot_fail("Unknown chunk type");
3139 return;
3140 }
3141 }
3142
Ajay Dudani0c6927b2011-05-18 11:12:16 -07003143 dprintf(INFO, "Wrote %d blocks, expected to write %d blocks\n",
3144 total_blocks, sparse_header->total_blks);
3145
3146 if(total_blocks != sparse_header->total_blks)
3147 {
3148 fastboot_fail("sparse image write failure");
3149 }
Ajay Dudani5c761132011-04-07 20:19:04 -07003150
3151 fastboot_okay("");
3152 return;
3153}
3154
3155void cmd_flash_mmc(const char *arg, void *data, unsigned sz)
3156{
3157 sparse_header_t *sparse_header;
Ajay Dudanide984792015-03-02 09:57:41 -08003158 meta_header_t *meta_header;
Ajay Dudani5c761132011-04-07 20:19:04 -07003159
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07003160#ifdef SSD_ENABLE
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -08003161 /* 8 Byte Magic + 2048 Byte xml + Encrypted Data */
3162 unsigned int *magic_number = (unsigned int *) data;
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08003163 int ret=0;
3164 uint32 major_version=0;
3165 uint32 minor_version=0;
3166
3167 ret = scm_svc_version(&major_version,&minor_version);
3168 if(!ret)
3169 {
3170 if(major_version >= 2)
3171 {
Amir Samuelovbb65ce02013-05-05 12:20:18 +03003172 if( !strcmp(arg, "ssd") || !strcmp(arg, "tqs") )
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08003173 {
3174 ret = encrypt_scm((uint32 **) &data, &sz);
3175 if (ret != 0) {
3176 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
3177 return;
3178 }
3179
Amir Samuelovbb65ce02013-05-05 12:20:18 +03003180 /* Protect only for SSD */
3181 if (!strcmp(arg, "ssd")) {
3182 ret = scm_protect_keystore((uint32 *) data, sz);
3183 if (ret != 0) {
3184 dprintf(CRITICAL, "ERROR: scm_protect_keystore Failed\n");
3185 return;
3186 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08003187 }
3188 }
3189 else
3190 {
3191 ret = decrypt_scm_v2((uint32 **) &data, &sz);
3192 if(ret != 0)
3193 {
3194 dprintf(CRITICAL,"ERROR: Decryption Failure\n");
3195 return;
3196 }
3197 }
3198 }
3199 else
3200 {
3201 if (magic_number[0] == DECRYPT_MAGIC_0 &&
3202 magic_number[1] == DECRYPT_MAGIC_1)
3203 {
3204 ret = decrypt_scm((uint32 **) &data, &sz);
3205 if (ret != 0) {
3206 dprintf(CRITICAL, "ERROR: Invalid secure image\n");
3207 return;
3208 }
3209 }
3210 else if (magic_number[0] == ENCRYPT_MAGIC_0 &&
3211 magic_number[1] == ENCRYPT_MAGIC_1)
3212 {
3213 ret = encrypt_scm((uint32 **) &data, &sz);
3214 if (ret != 0) {
3215 dprintf(CRITICAL, "ERROR: Encryption Failure\n");
3216 return;
3217 }
3218 }
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07003219 }
3220 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08003221 else
Neeti Desai127b9e02012-03-20 16:11:23 -07003222 {
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08003223 dprintf(CRITICAL,"INVALID SVC Version\n");
3224 return;
Neeti Desai127b9e02012-03-20 16:11:23 -07003225 }
sundarajan srinivasand4b1c482013-02-27 14:15:43 -08003226#endif /* SSD_ENABLE */
Neeti Desai127b9e02012-03-20 16:11:23 -07003227
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003228#if VERIFIED_BOOT
Channagoud Kadabi35297672015-06-13 11:09:49 -07003229 if (target_build_variant_user())
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003230 {
lijuang511a2b52015-08-14 20:50:51 +08003231 /* if device is locked:
3232 * common partition will not allow to be flashed
3233 * critical partition will allow to flash image.
3234 */
3235 if(!device.is_unlocked && !critical_flash_allowed(arg)) {
3236 fastboot_fail("Partition flashing is not allowed");
3237 return;
3238 }
Parth Dixitddbc7352015-10-18 03:13:31 +05303239#if !VBOOT_MOTA
lijuang511a2b52015-08-14 20:50:51 +08003240 /* if device critical is locked:
3241 * common partition will allow to be flashed
3242 * critical partition will not allow to flash image.
3243 */
3244 if(!device.is_unlock_critical && critical_flash_allowed(arg)) {
3245 fastboot_fail("Critical partition flashing is not allowed");
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003246 return;
3247 }
Parth Dixitddbc7352015-10-18 03:13:31 +05303248#endif
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003249 }
3250#endif
3251
kchik@codeaurora.orgbce18ea2011-04-18 20:22:28 -07003252 sparse_header = (sparse_header_t *) data;
Ajay Dudanide984792015-03-02 09:57:41 -08003253 meta_header = (meta_header_t *) data;
3254 if (sparse_header->magic == SPARSE_HEADER_MAGIC)
Ajay Dudani5c761132011-04-07 20:19:04 -07003255 cmd_flash_mmc_sparse_img(arg, data, sz);
Ajay Dudanide984792015-03-02 09:57:41 -08003256 else if (meta_header->magic == META_HEADER_MAGIC)
3257 cmd_flash_meta_img(arg, data, sz);
3258 else
3259 cmd_flash_mmc_img(arg, data, sz);
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07003260
3261#if VERIFIED_BOOT
Parth Dixitddbc7352015-10-18 03:13:31 +05303262#if !VBOOT_MOTA
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07003263 if((!strncmp(arg, "system", 6)) && !device.verity_mode)
3264 {
3265 // reset dm_verity mode to enforcing
3266 device.verity_mode = 1;
3267 write_device_info(&device);
3268 }
3269#endif
Parth Dixitddbc7352015-10-18 03:13:31 +05303270#endif
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07003271
Ajay Dudani5c761132011-04-07 20:19:04 -07003272 return;
3273}
3274
Tanya Brokhman84eaaf62015-02-24 21:57:06 +02003275void cmd_updatevol(const char *vol_name, void *data, unsigned sz)
3276{
3277 struct ptentry *sys_ptn;
3278 struct ptable *ptable;
3279
3280 ptable = flash_get_ptable();
3281 if (ptable == NULL) {
3282 fastboot_fail("partition table doesn't exist");
3283 return;
3284 }
3285
3286 sys_ptn = ptable_find(ptable, "system");
3287 if (sys_ptn == NULL) {
3288 fastboot_fail("system partition not found");
3289 return;
3290 }
3291
3292 sz = ROUND_TO_PAGE(sz, page_mask);
3293 if (update_ubi_vol(sys_ptn, vol_name, data, sz))
3294 fastboot_fail("update_ubi_vol failed");
3295 else
3296 fastboot_okay("");
3297}
3298
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003299void cmd_flash_nand(const char *arg, void *data, unsigned sz)
Dima Zavin214cc642009-01-26 11:16:21 -08003300{
3301 struct ptentry *ptn;
3302 struct ptable *ptable;
3303 unsigned extra = 0;
Gaurav Nebhwanid9730712016-05-06 14:28:29 +05303304 uint64_t partition_size = 0;
Dima Zavin214cc642009-01-26 11:16:21 -08003305
3306 ptable = flash_get_ptable();
3307 if (ptable == NULL) {
3308 fastboot_fail("partition table doesn't exist");
3309 return;
3310 }
3311
3312 ptn = ptable_find(ptable, arg);
3313 if (ptn == NULL) {
Tanya Brokhman84eaaf62015-02-24 21:57:06 +02003314 dprintf(INFO, "unknown partition name (%s). Trying updatevol\n",
3315 arg);
3316 cmd_updatevol(arg, data, sz);
Dima Zavin214cc642009-01-26 11:16:21 -08003317 return;
3318 }
3319
3320 if (!strcmp(ptn->name, "boot") || !strcmp(ptn->name, "recovery")) {
3321 if (memcmp((void *)data, BOOT_MAGIC, BOOT_MAGIC_SIZE)) {
3322 fastboot_fail("image is not a boot image");
3323 return;
3324 }
3325 }
3326
Amol Jadi5c61a952012-05-04 17:05:35 -07003327 if (!strcmp(ptn->name, "system")
Deepa Dinamani13e32c42012-03-12 14:34:17 -07003328 || !strcmp(ptn->name, "userdata")
3329 || !strcmp(ptn->name, "persist")
Sundarajan Srinivasanb063a852013-11-19 14:02:27 -08003330 || !strcmp(ptn->name, "recoveryfs")
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -08003331 || !strcmp(ptn->name, "modem"))
Tanya Brokhman1c94f1a2015-02-15 09:05:03 +02003332 extra = 1;
Sundarajan Srinivasan595b71e2013-11-05 12:44:34 -08003333 else
Shashank Mittaldcc2e352009-11-19 19:11:16 -08003334 sz = ROUND_TO_PAGE(sz, page_mask);
Dima Zavin214cc642009-01-26 11:16:21 -08003335
Gaurav Nebhwanid9730712016-05-06 14:28:29 +05303336 partition_size = (uint64_t)ptn->length * (uint64_t)flash_num_pages_per_blk() * (uint64_t)flash_page_size();
3337 if (partition_size > UINT_MAX) {
3338 fastboot_fail("Invalid partition size");
3339 return;
3340 }
3341
3342 if (sz > partition_size) {
3343 fastboot_fail("Image size too large");
3344 return;
3345 }
3346
Dima Zavin214cc642009-01-26 11:16:21 -08003347 dprintf(INFO, "writing %d bytes to '%s'\n", sz, ptn->name);
Tanya Brokhman1c94f1a2015-02-15 09:05:03 +02003348 if (!memcmp((void *)data, UBI_MAGIC, UBI_MAGIC_SIZE)) {
3349 if (flash_ubi_img(ptn, data, sz)) {
3350 fastboot_fail("flash write failure");
3351 return;
3352 }
3353 } else {
3354 if (flash_write(ptn, extra, data, sz)) {
3355 fastboot_fail("flash write failure");
3356 return;
3357 }
Dima Zavin214cc642009-01-26 11:16:21 -08003358 }
3359 dprintf(INFO, "partition '%s' updated\n", ptn->name);
3360 fastboot_okay("");
3361}
3362
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003363void cmd_flash(const char *arg, void *data, unsigned sz)
3364{
3365 if(target_is_emmc_boot())
3366 cmd_flash_mmc(arg, data, sz);
3367 else
3368 cmd_flash_nand(arg, data, sz);
3369}
3370
Dima Zavin214cc642009-01-26 11:16:21 -08003371void cmd_continue(const char *arg, void *data, unsigned sz)
3372{
3373 fastboot_okay("");
Amol Jadi7c4316c2013-10-07 14:19:26 -07003374 fastboot_stop();
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003375
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003376 if (target_is_emmc_boot())
3377 {
lijuanga40d6302015-07-20 20:10:13 +08003378#if FBCON_DISPLAY_MSG
lijuangde34d502016-02-26 16:04:50 +08003379 /* Exit keys' detection thread firstly */
3380 exit_menu_keys_detection();
lijuanga40d6302015-07-20 20:10:13 +08003381#endif
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003382 boot_linux_from_mmc();
3383 }
3384 else
3385 {
3386 boot_linux_from_flash();
3387 }
Dima Zavin214cc642009-01-26 11:16:21 -08003388}
3389
Chandan Uddaraju94183c02010-01-15 15:13:59 -08003390void cmd_reboot(const char *arg, void *data, unsigned sz)
3391{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003392 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08003393 fastboot_okay("");
3394 reboot_device(0);
3395}
3396
3397void cmd_reboot_bootloader(const char *arg, void *data, unsigned sz)
3398{
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003399 dprintf(INFO, "rebooting the device\n");
Chandan Uddaraju94183c02010-01-15 15:13:59 -08003400 fastboot_okay("");
3401 reboot_device(FASTBOOT_MODE);
3402}
3403
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003404void cmd_oem_enable_charger_screen(const char *arg, void *data, unsigned size)
3405{
3406 dprintf(INFO, "Enabling charger screen check\n");
3407 device.charger_screen_enabled = 1;
3408 write_device_info(&device);
3409 fastboot_okay("");
3410}
3411
3412void cmd_oem_disable_charger_screen(const char *arg, void *data, unsigned size)
3413{
3414 dprintf(INFO, "Disabling charger screen check\n");
3415 device.charger_screen_enabled = 0;
3416 write_device_info(&device);
3417 fastboot_okay("");
3418}
3419
lijuanga25d8bb2015-09-16 13:11:52 +08003420void cmd_oem_off_mode_charger(const char *arg, void *data, unsigned size)
3421{
3422 char *p = NULL;
3423 const char *delim = " \t\n\r";
Parth Dixit407f15b2016-01-07 14:47:37 +05303424 char *sp;
lijuanga25d8bb2015-09-16 13:11:52 +08003425
3426 if (arg) {
Parth Dixit407f15b2016-01-07 14:47:37 +05303427 p = strtok_r((char *)arg, delim, &sp);
lijuanga25d8bb2015-09-16 13:11:52 +08003428 if (p) {
3429 if (!strncmp(p, "0", 1)) {
3430 device.charger_screen_enabled = 0;
3431 } else if (!strncmp(p, "1", 1)) {
3432 device.charger_screen_enabled = 1;
3433 }
3434 }
3435 }
3436
3437 /* update charger_screen_enabled value for getvar
3438 * command
3439 */
3440 snprintf(charger_screen_enabled, MAX_RSP_SIZE, "%d",
3441 device.charger_screen_enabled);
3442
3443 write_device_info(&device);
3444 fastboot_okay("");
3445}
3446
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05303447void cmd_oem_select_display_panel(const char *arg, void *data, unsigned size)
3448{
3449 dprintf(INFO, "Selecting display panel %s\n", arg);
3450 if (arg)
3451 strlcpy(device.display_panel, arg,
3452 sizeof(device.display_panel));
3453 write_device_info(&device);
3454 fastboot_okay("");
3455}
3456
Shashank Mittal162244e2011-08-08 19:01:25 -07003457void cmd_oem_unlock(const char *arg, void *data, unsigned sz)
3458{
lijuang4ece1e72015-08-14 21:02:36 +08003459 set_device_unlock(UNLOCK, TRUE);
vijay kumarc65876c2015-04-24 13:29:16 +05303460}
3461
3462void cmd_oem_unlock_go(const char *arg, void *data, unsigned sz)
3463{
lijuang4ece1e72015-08-14 21:02:36 +08003464 if(!device.is_unlocked) {
vijay kumarc65876c2015-04-24 13:29:16 +05303465 if(!is_allow_unlock) {
3466 fastboot_fail("oem unlock is not allowed");
3467 return;
3468 }
3469
lijuang4ece1e72015-08-14 21:02:36 +08003470 set_device_unlock_value(UNLOCK, TRUE);
vijay kumarc65876c2015-04-24 13:29:16 +05303471
lijuang4ece1e72015-08-14 21:02:36 +08003472 /* wipe data */
vijay kumarc65876c2015-04-24 13:29:16 +05303473 struct recovery_message msg;
lijuang4ece1e72015-08-14 21:02:36 +08003474
vijay kumarc65876c2015-04-24 13:29:16 +05303475 snprintf(msg.recovery, sizeof(msg.recovery), "recovery\n--wipe_data");
3476 write_misc(0, &msg, sizeof(msg));
3477
3478 fastboot_okay("");
3479 reboot_device(RECOVERY_MODE);
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003480 }
3481 fastboot_okay("");
3482}
3483
Channagoud Kadabiad259832015-05-29 11:14:17 -07003484static int aboot_frp_unlock(char *pname, void *data, unsigned sz)
3485{
Mayank Grover70f8b0e2017-05-17 11:49:00 +05303486 int ret=1;
3487 bool authentication_success=false;
Channagoud Kadabiad259832015-05-29 11:14:17 -07003488
Mayank Grover70f8b0e2017-05-17 11:49:00 +05303489 /*
3490 Authentication method not implemented.
3491
3492 OEM to implement, authentication system which on successful validataion,
3493 calls write_allow_oem_unlock() with is_allow_unlock.
3494 */
3495#if 0
3496 authentication_success = oem_specific_auth_mthd();
3497#endif
3498
3499 if (authentication_success)
Channagoud Kadabiad259832015-05-29 11:14:17 -07003500 {
Mayank Grover70f8b0e2017-05-17 11:49:00 +05303501 is_allow_unlock = true;
3502 write_allow_oem_unlock(is_allow_unlock);
3503 ret = 0;
Channagoud Kadabiad259832015-05-29 11:14:17 -07003504 }
3505 return ret;
3506}
3507
Shashank Mittald3e54dd2014-08-28 15:24:02 -07003508void cmd_oem_lock(const char *arg, void *data, unsigned sz)
3509{
lijuang4ece1e72015-08-14 21:02:36 +08003510 set_device_unlock(UNLOCK, FALSE);
Shashank Mittal162244e2011-08-08 19:01:25 -07003511}
3512
Shashank Mittala0032282011-08-26 14:50:11 -07003513void cmd_oem_devinfo(const char *arg, void *data, unsigned sz)
3514{
lijuang511a2b52015-08-14 20:50:51 +08003515 char response[MAX_RSP_SIZE];
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003516 snprintf(response, sizeof(response), "\tDevice tampered: %s", (device.is_tampered ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07003517 fastboot_info(response);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003518 snprintf(response, sizeof(response), "\tDevice unlocked: %s", (device.is_unlocked ? "true" : "false"));
3519 fastboot_info(response);
Parth Dixitddbc7352015-10-18 03:13:31 +05303520#if !VBOOT_MOTA
lijuang511a2b52015-08-14 20:50:51 +08003521 snprintf(response, sizeof(response), "\tDevice critical unlocked: %s", (device.is_unlock_critical ? "true" : "false"));
3522 fastboot_info(response);
Parth Dixitddbc7352015-10-18 03:13:31 +05303523#endif
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003524 snprintf(response, sizeof(response), "\tCharger screen enabled: %s", (device.charger_screen_enabled ? "true" : "false"));
Shashank Mittala0032282011-08-26 14:50:11 -07003525 fastboot_info(response);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05303526 snprintf(response, sizeof(response), "\tDisplay panel: %s", (device.display_panel));
3527 fastboot_info(response);
Shashank Mittala0032282011-08-26 14:50:11 -07003528 fastboot_okay("");
3529}
3530
lijuang511a2b52015-08-14 20:50:51 +08003531void cmd_flashing_get_unlock_ability(const char *arg, void *data, unsigned sz)
3532{
3533 char response[MAX_RSP_SIZE];
3534 snprintf(response, sizeof(response), "\tget_unlock_ability: %d", is_allow_unlock);
3535 fastboot_info(response);
3536 fastboot_okay("");
3537}
3538
3539void cmd_flashing_lock_critical(const char *arg, void *data, unsigned sz)
3540{
lijuang4ece1e72015-08-14 21:02:36 +08003541 set_device_unlock(UNLOCK_CRITICAL, FALSE);
lijuang511a2b52015-08-14 20:50:51 +08003542}
3543
3544void cmd_flashing_unlock_critical(const char *arg, void *data, unsigned sz)
3545{
lijuang4ece1e72015-08-14 21:02:36 +08003546 set_device_unlock(UNLOCK_CRITICAL, TRUE);
lijuang511a2b52015-08-14 20:50:51 +08003547}
3548
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07003549void cmd_preflash(const char *arg, void *data, unsigned sz)
3550{
3551 fastboot_okay("");
3552}
3553
Mao Flynn7b379f32015-04-20 00:28:30 +08003554static uint8_t logo_header[LOGO_IMG_HEADER_SIZE];
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303555
Mao Flynn7b379f32015-04-20 00:28:30 +08003556int splash_screen_check_header(logo_img_header *header)
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303557{
Mao Flynn7b379f32015-04-20 00:28:30 +08003558 if (memcmp(header->magic, LOGO_IMG_MAGIC, 8))
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303559 return -1;
Mao Flynn7b379f32015-04-20 00:28:30 +08003560 if (header->width == 0 || header->height == 0)
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303561 return -1;
3562 return 0;
3563}
3564
Mao Flynn7b379f32015-04-20 00:28:30 +08003565int splash_screen_flash()
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003566{
3567 struct ptentry *ptn;
3568 struct ptable *ptable;
Mao Flynn7b379f32015-04-20 00:28:30 +08003569 struct logo_img_header *header;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003570 struct fbcon_config *fb_display = NULL;
Channagoud Kadabib3ccf5c2014-12-03 12:39:29 -08003571
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303572 ptable = flash_get_ptable();
3573 if (ptable == NULL) {
Mao Flynn7b379f32015-04-20 00:28:30 +08003574 dprintf(CRITICAL, "ERROR: Partition table not found\n");
3575 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303576 }
Mao Flynn7b379f32015-04-20 00:28:30 +08003577
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303578 ptn = ptable_find(ptable, "splash");
3579 if (ptn == NULL) {
3580 dprintf(CRITICAL, "ERROR: splash Partition not found\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003581 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303582 }
Mao Flynn7b379f32015-04-20 00:28:30 +08003583 if (flash_read(ptn, 0, (void *)logo_header, LOGO_IMG_HEADER_SIZE)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303584 dprintf(CRITICAL, "ERROR: Cannot read boot image header\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003585 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303586 }
3587
Mao Flynn7b379f32015-04-20 00:28:30 +08003588 header = (struct logo_img_header *)logo_header;
3589 if (splash_screen_check_header(header)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303590 dprintf(CRITICAL, "ERROR: Boot image header invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003591 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303592 }
3593
3594 fb_display = fbcon_display();
3595 if (fb_display) {
Mao Flynn7b379f32015-04-20 00:28:30 +08003596 if (header->type && (header->blocks != 0)) { // RLE24 compressed data
3597 uint8_t *base = (uint8_t *) fb_display->base + LOGO_IMG_OFFSET;
3598
3599 /* if the logo is full-screen size, remove "fbcon_clear()" */
3600 if ((header->width != fb_display->width)
3601 || (header->height != fb_display->height))
3602 fbcon_clear();
3603
3604 if (flash_read(ptn + LOGO_IMG_HEADER_SIZE, 0,
3605 (uint32_t *)base,
3606 (header->blocks * 512))) {
3607 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
3608 return -1;
3609 }
3610 fbcon_extract_to_screen(header, base);
3611 return 0;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003612 }
Mao Flynn7b379f32015-04-20 00:28:30 +08003613
Veera Sundaram Sankaranc33416f2015-09-28 10:25:55 -07003614 if ((header->width > fb_display->width) || (header->height > fb_display->height)) {
3615 dprintf(CRITICAL, "Logo config greater than fb config. Fall back default logo\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003616 return -1;
3617 }
3618
Vineet Bajaj99291ed2014-09-09 12:29:46 +05303619 uint8_t *base = (uint8_t *) fb_display->base;
Mao Flynn7b379f32015-04-20 00:28:30 +08003620 if (flash_read(ptn + LOGO_IMG_HEADER_SIZE, 0,
3621 (uint32_t *)base,
3622 ((((header->width * header->height * fb_display->bpp/8) + 511) >> 9) << 9))) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303623 fbcon_clear();
Vineet Bajaj99291ed2014-09-09 12:29:46 +05303624 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003625 return -1;
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003626 }
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303627 }
3628
Mao Flynn7b379f32015-04-20 00:28:30 +08003629 return 0;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303630}
3631
Mao Flynn7b379f32015-04-20 00:28:30 +08003632int splash_screen_mmc()
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303633{
3634 int index = INVALID_PTN;
3635 unsigned long long ptn = 0;
3636 struct fbcon_config *fb_display = NULL;
Mao Flynn7b379f32015-04-20 00:28:30 +08003637 struct logo_img_header *header;
Mao Flynn1893a7f2015-06-03 12:03:36 +08003638 uint32_t blocksize, realsize, readsize;
3639 uint8_t *base;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303640
3641 index = partition_get_index("splash");
3642 if (index == 0) {
3643 dprintf(CRITICAL, "ERROR: splash Partition table not found\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003644 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303645 }
3646
3647 ptn = partition_get_offset(index);
3648 if (ptn == 0) {
3649 dprintf(CRITICAL, "ERROR: splash Partition invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003650 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303651 }
3652
Mao Flynn1893a7f2015-06-03 12:03:36 +08003653 mmc_set_lun(partition_get_lun(index));
3654
3655 blocksize = mmc_get_device_blocksize();
3656 if (blocksize == 0) {
3657 dprintf(CRITICAL, "ERROR:splash Partition invalid blocksize\n");
3658 return -1;
3659 }
3660
3661 fb_display = fbcon_display();
Channagoud Kadabidaf10a62015-07-22 11:51:55 -07003662 if (!fb_display)
3663 {
3664 dprintf(CRITICAL, "ERROR: fb config is not allocated\n");
3665 return -1;
3666 }
3667
Mao Flynn1893a7f2015-06-03 12:03:36 +08003668 base = (uint8_t *) fb_display->base;
3669
3670 if (mmc_read(ptn, (uint32_t *)(base + LOGO_IMG_OFFSET), blocksize)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303671 dprintf(CRITICAL, "ERROR: Cannot read splash image header\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003672 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303673 }
3674
Mao Flynn1893a7f2015-06-03 12:03:36 +08003675 header = (struct logo_img_header *)(base + LOGO_IMG_OFFSET);
Mao Flynn7b379f32015-04-20 00:28:30 +08003676 if (splash_screen_check_header(header)) {
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303677 dprintf(CRITICAL, "ERROR: Splash image header invalid\n");
Mao Flynn7b379f32015-04-20 00:28:30 +08003678 return -1;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303679 }
3680
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303681 if (fb_display) {
Mao Flynn1893a7f2015-06-03 12:03:36 +08003682 if (header->type && (header->blocks != 0)) { /* 1 RLE24 compressed data */
3683 base += LOGO_IMG_OFFSET;
Mao Flynn7b379f32015-04-20 00:28:30 +08003684
Mao Flynn1893a7f2015-06-03 12:03:36 +08003685 realsize = header->blocks * 512;
3686 readsize = ROUNDUP((realsize + LOGO_IMG_HEADER_SIZE), blocksize) - blocksize;
3687
3688 /* if the logo is not full-screen size, clean screen */
Mao Flynn7b379f32015-04-20 00:28:30 +08003689 if ((header->width != fb_display->width)
3690 || (header->height != fb_display->height))
3691 fbcon_clear();
3692
Mao Flynn1893a7f2015-06-03 12:03:36 +08003693 if (mmc_read(ptn + blocksize, (uint32_t *)(base + blocksize), readsize)) {
Mao Flynn7b379f32015-04-20 00:28:30 +08003694 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
3695 return -1;
3696 }
Mao Flynn7b379f32015-04-20 00:28:30 +08003697
Mao Flynn1893a7f2015-06-03 12:03:36 +08003698 fbcon_extract_to_screen(header, (base + LOGO_IMG_HEADER_SIZE));
3699 } else { /* 2 Raw BGR data */
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303700
Veera Sundaram Sankaranc33416f2015-09-28 10:25:55 -07003701 if ((header->width > fb_display->width) || (header->height > fb_display->height)) {
3702 dprintf(CRITICAL, "Logo config greater than fb config. Fall back default logo\n");
Mao Flynn1893a7f2015-06-03 12:03:36 +08003703 return -1;
3704 }
3705
3706 realsize = header->width * header->height * fb_display->bpp / 8;
3707 readsize = ROUNDUP((realsize + LOGO_IMG_HEADER_SIZE), blocksize) - blocksize;
3708
3709 if (blocksize == LOGO_IMG_HEADER_SIZE) { /* read the content directly */
3710 if (mmc_read((ptn + LOGO_IMG_HEADER_SIZE), (uint32_t *)base, readsize)) {
3711 fbcon_clear();
3712 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
3713 return -1;
3714 }
3715 } else {
3716 if (mmc_read(ptn + blocksize ,
3717 (uint32_t *)(base + LOGO_IMG_OFFSET + blocksize), readsize)) {
3718 dprintf(CRITICAL, "ERROR: Cannot read splash image from partition\n");
3719 return -1;
3720 }
3721 memmove(base, (base + LOGO_IMG_OFFSET + LOGO_IMG_HEADER_SIZE), realsize);
3722 }
3723 }
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303724 }
3725
Mao Flynn7b379f32015-04-20 00:28:30 +08003726 return 0;
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303727}
3728
Mao Flynn7b379f32015-04-20 00:28:30 +08003729int fetch_image_from_partition()
Aparna Mallavarapuad8c1ec2013-08-23 17:55:47 +05303730{
3731 if (target_is_emmc_boot()) {
3732 return splash_screen_mmc();
3733 } else {
3734 return splash_screen_flash();
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003735 }
3736}
3737
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07003738/* Get the size from partiton name */
3739static void get_partition_size(const char *arg, char *response)
3740{
3741 uint64_t ptn = 0;
3742 uint64_t size;
3743 int index = INVALID_PTN;
3744
3745 index = partition_get_index(arg);
3746
3747 if (index == INVALID_PTN)
3748 {
3749 dprintf(CRITICAL, "Invalid partition index\n");
3750 return;
3751 }
3752
3753 ptn = partition_get_offset(index);
3754
3755 if(!ptn)
3756 {
3757 dprintf(CRITICAL, "Invalid partition name %s\n", arg);
3758 return;
3759 }
3760
3761 size = partition_get_size(index);
3762
3763 snprintf(response, MAX_RSP_SIZE, "\t 0x%llx", size);
3764 return;
3765}
3766
3767/*
3768 * Publish the partition type & size info
3769 * fastboot getvar will publish the required information.
3770 * fastboot getvar partition_size:<partition_name>: partition size in hex
3771 * fastboot getvar partition_type:<partition_name>: partition type (ext/fat)
3772 */
3773static void publish_getvar_partition_info(struct getvar_partition_info *info, uint8_t num_parts)
3774{
3775 uint8_t i;
3776
3777 for (i = 0; i < num_parts; i++) {
3778 get_partition_size(info[i].part_name, info[i].size_response);
3779
3780 if (strlcat(info[i].getvar_size, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
3781 {
3782 dprintf(CRITICAL, "partition size name truncated\n");
3783 return;
3784 }
3785 if (strlcat(info[i].getvar_type, info[i].part_name, MAX_GET_VAR_NAME_SIZE) >= MAX_GET_VAR_NAME_SIZE)
3786 {
3787 dprintf(CRITICAL, "partition type name truncated\n");
3788 return;
3789 }
3790
3791 /* publish partition size & type info */
3792 fastboot_publish((const char *) info[i].getvar_size, (const char *) info[i].size_response);
3793 fastboot_publish((const char *) info[i].getvar_type, (const char *) info[i].type_response);
3794 }
3795}
3796
lijuang4ece1e72015-08-14 21:02:36 +08003797void get_product_name(unsigned char *buf)
3798{
3799 snprintf((char*)buf, MAX_RSP_SIZE, "%s", TARGET(BOARD));
3800 return;
3801}
3802
3803void get_bootloader_version(unsigned char *buf)
3804{
3805 snprintf((char*)buf, MAX_RSP_SIZE, "%s", device.bootloader_version);
3806 return;
3807}
3808
3809void get_baseband_version(unsigned char *buf)
3810{
3811 snprintf((char*)buf, MAX_RSP_SIZE, "%s", device.radio_version);
3812 return;
3813}
3814
3815bool is_device_locked()
3816{
3817 return device.is_unlocked ? false:true;
3818}
3819
Amol Jadi5edf3552013-07-23 14:15:34 -07003820/* register commands and variables for fastboot */
3821void aboot_fastboot_register_commands(void)
3822{
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003823 int i;
lijuangf16461c2015-08-03 17:09:34 +08003824 char hw_platform_buf[MAX_RSP_SIZE];
Amol Jadi5edf3552013-07-23 14:15:34 -07003825
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003826 struct fastboot_cmd_desc cmd_list[] = {
lijuang511a2b52015-08-14 20:50:51 +08003827 /* By default the enabled list is empty. */
3828 {"", NULL},
3829 /* move commands enclosed within the below ifndef to here
3830 * if they need to be enabled in user build.
3831 */
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003832#ifndef DISABLE_FASTBOOT_CMDS
lijuang511a2b52015-08-14 20:50:51 +08003833 /* Register the following commands only for non-user builds */
3834 {"flash:", cmd_flash},
3835 {"erase:", cmd_erase},
3836 {"boot", cmd_boot},
3837 {"continue", cmd_continue},
3838 {"reboot", cmd_reboot},
3839 {"reboot-bootloader", cmd_reboot_bootloader},
3840 {"oem unlock", cmd_oem_unlock},
3841 {"oem unlock-go", cmd_oem_unlock_go},
3842 {"oem lock", cmd_oem_lock},
3843 {"flashing unlock", cmd_oem_unlock},
3844 {"flashing lock", cmd_oem_lock},
3845 {"flashing lock_critical", cmd_flashing_lock_critical},
3846 {"flashing unlock_critical", cmd_flashing_unlock_critical},
3847 {"flashing get_unlock_ability", cmd_flashing_get_unlock_ability},
3848 {"oem device-info", cmd_oem_devinfo},
3849 {"preflash", cmd_preflash},
3850 {"oem enable-charger-screen", cmd_oem_enable_charger_screen},
3851 {"oem disable-charger-screen", cmd_oem_disable_charger_screen},
lijuanga25d8bb2015-09-16 13:11:52 +08003852 {"oem off-mode-charge", cmd_oem_off_mode_charger},
lijuang511a2b52015-08-14 20:50:51 +08003853 {"oem select-display-panel", cmd_oem_select_display_panel},
Channagoud Kadabi736c4962015-08-21 11:56:52 -07003854#if UNITTEST_FW_SUPPORT
Channagoud Kadabid5ddf482015-09-28 10:31:35 -07003855 {"oem run-tests", cmd_oem_runtests},
Channagoud Kadabi736c4962015-08-21 11:56:52 -07003856#endif
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003857#endif
lijuang511a2b52015-08-14 20:50:51 +08003858 };
Sridhar Parasurame94e8152014-10-24 14:06:03 -07003859
3860 int fastboot_cmds_count = sizeof(cmd_list)/sizeof(cmd_list[0]);
3861 for (i = 1; i < fastboot_cmds_count; i++)
3862 fastboot_register(cmd_list[i].name,cmd_list[i].cb);
3863
Amol Jadi5edf3552013-07-23 14:15:34 -07003864 /* publish variables and their values */
3865 fastboot_publish("product", TARGET(BOARD));
3866 fastboot_publish("kernel", "lk");
3867 fastboot_publish("serialno", sn_buf);
3868
3869 /*
3870 * partition info is supported only for emmc partitions
3871 * Calling this for NAND prints some error messages which
3872 * is harmless but misleading. Avoid calling this for NAND
3873 * devices.
3874 */
3875 if (target_is_emmc_boot())
3876 publish_getvar_partition_info(part_info, ARRAY_SIZE(part_info));
3877
3878 /* Max download size supported */
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003879 snprintf(max_download_size, MAX_RSP_SIZE, "\t0x%x",
3880 target_get_max_flash_size());
Amol Jadi5edf3552013-07-23 14:15:34 -07003881 fastboot_publish("max-download-size", (const char *) max_download_size);
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003882 /* Is the charger screen check enabled */
3883 snprintf(charger_screen_enabled, MAX_RSP_SIZE, "%d",
3884 device.charger_screen_enabled);
3885 fastboot_publish("charger-screen-enabled",
3886 (const char *) charger_screen_enabled);
lijuanga25d8bb2015-09-16 13:11:52 +08003887 fastboot_publish("off-mode-charge", (const char *) charger_screen_enabled);
Unnati Gandhi62c8ab82014-01-24 11:01:01 +05303888 snprintf(panel_display_mode, MAX_RSP_SIZE, "%s",
3889 device.display_panel);
3890 fastboot_publish("display-panel",
3891 (const char *) panel_display_mode);
Channagoud Kadabiaafbbfc2015-03-04 17:52:56 -08003892 fastboot_publish("version-bootloader", (const char *) device.bootloader_version);
3893 fastboot_publish("version-baseband", (const char *) device.radio_version);
lijuangf16461c2015-08-03 17:09:34 +08003894 fastboot_publish("secure", is_secure_boot_enable()? "yes":"no");
3895 smem_get_hw_platform_name((unsigned char *) hw_platform_buf, sizeof(hw_platform_buf));
3896 snprintf(get_variant, MAX_RSP_SIZE, "%s %s", hw_platform_buf,
3897 target_is_emmc_boot()? "eMMC":"UFS");
3898 fastboot_publish("variant", (const char *) get_variant);
lijuang65c5a822015-08-29 16:35:36 +08003899#if CHECK_BAT_VOLTAGE
lijuang102dfa92015-10-09 18:31:03 +08003900 update_battery_status();
lijuang65c5a822015-08-29 16:35:36 +08003901 fastboot_publish("battery-voltage", (const char *) battery_voltage);
lijuang102dfa92015-10-09 18:31:03 +08003902 fastboot_publish("battery-soc-ok", (const char *) battery_soc_ok);
lijuang65c5a822015-08-29 16:35:36 +08003903#endif
Amol Jadi5edf3552013-07-23 14:15:34 -07003904}
3905
Brian Swetland9c4c0752009-01-25 16:23:50 -08003906void aboot_init(const struct app_descriptor *app)
3907{
Shashank Mittal4f99a882010-02-01 13:58:50 -08003908 unsigned reboot_mode = 0;
Chandan Uddarajubedca152010-06-02 23:05:15 -07003909
lijuang39831732016-01-08 17:49:02 +08003910 /* Initialise wdog to catch early lk crashes */
3911#if WDOG_SUPPORT
3912 msm_wdog_init();
3913#endif
3914
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07003915 /* Setup page size information for nv storage */
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003916 if (target_is_emmc_boot())
3917 {
Channagoud Kadabi749b0f82013-09-10 22:36:02 -07003918 page_size = mmc_page_size();
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003919 page_mask = page_size - 1;
Gaurav Nebhwani4d2389c2016-03-17 21:10:05 +05303920 mmc_blocksize = mmc_get_device_blocksize();
3921 mmc_blocksize_mask = mmc_blocksize - 1;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003922 }
3923 else
3924 {
3925 page_size = flash_page_size();
3926 page_mask = page_size - 1;
3927 }
Deepa Dinamani0e163a42013-05-24 17:08:15 -07003928 ASSERT((MEMBASE + MEMSIZE) > MEMBASE);
3929
Ameya Thakur11cf1a62013-08-05 12:44:48 -07003930 read_device_info(&device);
vijay kumarc65876c2015-04-24 13:29:16 +05303931 read_allow_oem_unlock(&device);
Shashank Mittal162244e2011-08-08 19:01:25 -07003932
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08003933 /* Display splash screen if enabled */
3934#if DISPLAY_SPLASH_SCREEN
lijuang99c02d82015-02-13 19:04:34 +08003935#if NO_ALARM_DISPLAY
3936 if (!check_alarm_boot()) {
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08003937#endif
lijuang99c02d82015-02-13 19:04:34 +08003938 dprintf(SPEW, "Display Init: Start\n");
Ajay Singh Parmara7865a82015-04-16 21:27:52 -07003939#if DISPLAY_HDMI_PRIMARY
3940 if (!strlen(device.display_panel))
3941 strlcpy(device.display_panel, DISPLAY_PANEL_HDMI,
3942 sizeof(device.display_panel));
3943#endif
Channagoud Kadabi90869ce2015-04-27 11:15:14 -07003944#if ENABLE_WBC
Channagoud Kadabid801ac72015-08-26 11:21:51 -07003945 /* Wait if the display shutdown is in progress */
3946 while(pm_app_display_shutdown_in_prgs());
Channagoud Kadabi90869ce2015-04-27 11:15:14 -07003947 if (!pm_appsbl_display_init_done())
3948 target_display_init(device.display_panel);
3949 else
3950 display_image_on_screen();
3951#else
lijuang99c02d82015-02-13 19:04:34 +08003952 target_display_init(device.display_panel);
Channagoud Kadabi90869ce2015-04-27 11:15:14 -07003953#endif
lijuang99c02d82015-02-13 19:04:34 +08003954 dprintf(SPEW, "Display Init: Done\n");
3955#if NO_ALARM_DISPLAY
3956 }
3957#endif
3958#endif
Aravind Venkateswaran8f076242014-02-25 16:25:30 -08003959
Greg Griscod6250552011-06-29 14:40:23 -07003960 target_serialno((unsigned char *) sn_buf);
Ajay Dudanib06c05f2011-05-12 14:46:10 -07003961 dprintf(SPEW,"serial number: %s\n",sn_buf);
Subbaraman Narayanamurthyf17b4ae2011-02-16 20:19:56 -08003962
Dhaval Patel223ec952013-07-18 14:49:44 -07003963 memset(display_panel_buf, '\0', MAX_PANEL_BUF_SIZE);
3964
Matthew Qindefd5562014-07-11 18:02:40 +08003965 /*
3966 * Check power off reason if user force reset,
3967 * if yes phone will do normal boot.
3968 */
3969 if (is_user_force_reset())
3970 goto normal_boot;
3971
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07003972 /* Check if we should do something other than booting up */
Ameya Thakur0b9c2442013-05-10 13:22:56 -07003973 if (keys_get_state(KEY_VOLUMEUP) && keys_get_state(KEY_VOLUMEDOWN))
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07003974 {
Pavel Nedeva4c9d3a2013-05-15 14:42:34 +03003975 dprintf(ALWAYS,"dload mode key sequence detected\n");
lijuang395b5e62015-11-19 17:39:44 +08003976 reboot_device(EMERGENCY_DLOAD);
3977 dprintf(CRITICAL,"Failed to reboot into dload mode\n");
3978
Ameya Thakur0b9c2442013-05-10 13:22:56 -07003979 boot_into_fastboot = true;
3980 }
3981 if (!boot_into_fastboot)
3982 {
3983 if (keys_get_state(KEY_HOME) || keys_get_state(KEY_VOLUMEUP))
3984 boot_into_recovery = 1;
3985 if (!boot_into_recovery &&
3986 (keys_get_state(KEY_BACK) || keys_get_state(KEY_VOLUMEDOWN)))
Pavel Nedev5d91d412013-04-29 11:34:24 +03003987 boot_into_fastboot = true;
Subbaraman Narayanamurthy0e445b02011-06-19 21:34:46 -07003988 }
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003989 #if NO_KEYPAD_DRIVER
Kinson Chik0b1c8162011-08-31 16:31:57 -07003990 if (fastboot_trigger())
Pavel Nedev5d91d412013-04-29 11:34:24 +03003991 boot_into_fastboot = true;
Shashank Mittald8c42bf2010-06-09 15:44:28 -07003992 #endif
Chandan Uddarajubedca152010-06-02 23:05:15 -07003993
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003994#if USE_PON_REBOOT_REG
3995 reboot_mode = check_hard_reboot_mode();
3996#else
Ajay Dudani77421292010-10-27 19:34:06 -07003997 reboot_mode = check_reboot_mode();
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07003998#endif
3999 if (reboot_mode == RECOVERY_MODE)
4000 {
Ajay Dudani77421292010-10-27 19:34:06 -07004001 boot_into_recovery = 1;
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07004002 }
4003 else if(reboot_mode == FASTBOOT_MODE)
4004 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03004005 boot_into_fastboot = true;
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07004006 }
4007 else if(reboot_mode == ALARM_BOOT)
4008 {
Matthew Qind886f3c2014-01-17 16:52:01 +08004009 boot_reason_alarm = true;
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07004010 }
Parth Dixit207573a2015-10-27 17:26:21 +05304011#if VERIFIED_BOOT
Parth Dixitddbc7352015-10-18 03:13:31 +05304012#if !VBOOT_MOTA
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07004013 else if (reboot_mode == DM_VERITY_ENFORCING)
4014 {
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07004015 device.verity_mode = 1;
4016 write_device_info(&device);
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07004017 }
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07004018#if ENABLE_VB_ATTEST
4019 else if (reboot_mode == DM_VERITY_EIO)
4020#else
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07004021 else if (reboot_mode == DM_VERITY_LOGGING)
Channagoud Kadabi86b0c112016-03-16 19:23:16 -07004022#endif
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07004023 {
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07004024 device.verity_mode = 0;
4025 write_device_info(&device);
Channagoud Kadabicfcfcc52015-08-05 16:23:42 -07004026 }
4027 else if (reboot_mode == DM_VERITY_KEYSCLEAR)
4028 {
Sridhar Parasuramd69f7902015-07-10 13:31:17 -07004029 if(send_delete_keys_to_tz())
4030 ASSERT(0);
Ajay Dudani77421292010-10-27 19:34:06 -07004031 }
Parth Dixit207573a2015-10-27 17:26:21 +05304032#endif
Parth Dixitddbc7352015-10-18 03:13:31 +05304033#endif
Ajay Dudani77421292010-10-27 19:34:06 -07004034
Matthew Qindefd5562014-07-11 18:02:40 +08004035normal_boot:
Pavel Nedev5d91d412013-04-29 11:34:24 +03004036 if (!boot_into_fastboot)
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004037 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03004038 if (target_is_emmc_boot())
Shashank Mittala0032282011-08-26 14:50:11 -07004039 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03004040 if(emmc_recovery_init())
4041 dprintf(ALWAYS,"error in emmc_recovery_init\n");
4042 if(target_use_signed_kernel())
Shashank Mittala0032282011-08-26 14:50:11 -07004043 {
Pavel Nedev5d91d412013-04-29 11:34:24 +03004044 if((device.is_unlocked) || (device.is_tampered))
4045 {
4046 #ifdef TZ_TAMPER_FUSE
4047 set_tamper_fuse_cmd();
4048 #endif
4049 #if USE_PCOM_SECBOOT
4050 set_tamper_flag(device.is_tampered);
4051 #endif
4052 }
Shashank Mittala0032282011-08-26 14:50:11 -07004053 }
Amit Blay6281ebc2015-01-11 14:44:08 +02004054
Pavel Nedev5d91d412013-04-29 11:34:24 +03004055 boot_linux_from_mmc();
Shashank Mittala0032282011-08-26 14:50:11 -07004056 }
Pavel Nedev5d91d412013-04-29 11:34:24 +03004057 else
4058 {
4059 recovery_init();
4060 #if USE_PCOM_SECBOOT
4061 if((device.is_unlocked) || (device.is_tampered))
4062 set_tamper_flag(device.is_tampered);
4063 #endif
4064 boot_linux_from_flash();
4065 }
4066 dprintf(CRITICAL, "ERROR: Could not do normal boot. Reverting "
4067 "to fastboot mode.\n");
Shashank Mittald8c42bf2010-06-09 15:44:28 -07004068 }
Chandan Uddaraju2943fd62010-06-21 10:56:39 -07004069
Amol Jadi5edf3552013-07-23 14:15:34 -07004070 /* We are here means regular boot did not happen. Start fastboot. */
Channagoud Kadabi4c4884e2013-04-09 17:12:13 -07004071
Amol Jadi5edf3552013-07-23 14:15:34 -07004072 /* register aboot specific fastboot commands */
4073 aboot_fastboot_register_commands();
Amol Jadi57abe4c2011-05-24 15:47:27 -07004074
Amol Jadi5edf3552013-07-23 14:15:34 -07004075 /* dump partition table for debug info */
Kinson Chikf1a43512011-07-14 11:28:39 -07004076 partition_dump();
Amol Jadi5edf3552013-07-23 14:15:34 -07004077
4078 /* initialize and start fastboot */
4079 fastboot_init(target_get_scratch_address(), target_get_max_flash_size());
lijuang4ece1e72015-08-14 21:02:36 +08004080#if FBCON_DISPLAY_MSG
lijuang9a7d3b92015-11-30 14:41:24 +08004081 display_fastboot_menu();
lijuang4ece1e72015-08-14 21:02:36 +08004082#endif
Brian Swetland9c4c0752009-01-25 16:23:50 -08004083}
4084
Deepa Dinamani41fa8d62013-05-23 13:25:36 -07004085uint32_t get_page_size()
4086{
4087 return page_size;
4088}
4089
Amir Samuelov57a6fa22013-06-05 16:36:43 +03004090/*
4091 * Calculated and save hash (SHA256) for non-signed boot image.
4092 *
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07004093 * @param image_addr - Boot image address
4094 * @param image_size - Size of the boot image
Amir Samuelov57a6fa22013-06-05 16:36:43 +03004095 *
4096 * @return int - 0 on success, negative value on failure.
4097 */
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07004098static int aboot_save_boot_hash_mmc(uint32_t image_addr, uint32_t image_size)
Amir Samuelov57a6fa22013-06-05 16:36:43 +03004099{
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07004100 unsigned int digest[8];
4101#if IMAGE_VERIF_ALGO_SHA1
4102 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA1;
4103#else
4104 uint32_t auth_algo = CRYPTO_AUTH_ALG_SHA256;
4105#endif
Amir Samuelov57a6fa22013-06-05 16:36:43 +03004106
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07004107 target_crypto_init_params();
Veera Sundaram Sankaranf9915462014-12-09 11:54:59 -08004108 hash_find((unsigned char *) image_addr, image_size, (unsigned char *)&digest, auth_algo);
Amir Samuelov57a6fa22013-06-05 16:36:43 +03004109
4110 save_kernel_hash_cmd(digest);
Channagoud Kadabi82cf5c62014-08-24 22:17:34 -07004111 dprintf(INFO, "aboot_save_boot_hash_mmc: imagesize_actual size %d bytes.\n", (int) image_size);
Amir Samuelov57a6fa22013-06-05 16:36:43 +03004112
4113 return 0;
4114}
4115
Brian Swetland9c4c0752009-01-25 16:23:50 -08004116APP_START(aboot)
4117 .init = aboot_init,
4118APP_END